1 /* $NetBSD: ip_icmp.c,v 1.97 2005/11/10 13:40:38 christos Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * This code is derived from software contributed to The NetBSD Foundation 41 * by Jason R. Thorpe of Zembu Labs, Inc. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. All advertising materials mentioning features or use of this software 52 * must display the following acknowledgement: 53 * This product includes software developed by the NetBSD 54 * Foundation, Inc. and its contributors. 55 * 4. Neither the name of The NetBSD Foundation nor the names of its 56 * contributors may be used to endorse or promote products derived 57 * from this software without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 60 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 61 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 63 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 69 * POSSIBILITY OF SUCH DAMAGE. 70 */ 71 72 /* 73 * Copyright (c) 1982, 1986, 1988, 1993 74 * The Regents of the University of California. All rights reserved. 75 * 76 * Redistribution and use in source and binary forms, with or without 77 * modification, are permitted provided that the following conditions 78 * are met: 79 * 1. Redistributions of source code must retain the above copyright 80 * notice, this list of conditions and the following disclaimer. 81 * 2. Redistributions in binary form must reproduce the above copyright 82 * notice, this list of conditions and the following disclaimer in the 83 * documentation and/or other materials provided with the distribution. 84 * 3. Neither the name of the University nor the names of its contributors 85 * may be used to endorse or promote products derived from this software 86 * without specific prior written permission. 87 * 88 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 91 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 98 * SUCH DAMAGE. 99 * 100 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 101 */ 102 103 #include <sys/cdefs.h> 104 __KERNEL_RCSID(0, "$NetBSD: ip_icmp.c,v 1.97 2005/11/10 13:40:38 christos Exp $"); 105 106 #include "opt_ipsec.h" 107 108 #include <sys/param.h> 109 #include <sys/systm.h> 110 #include <sys/malloc.h> 111 #include <sys/mbuf.h> 112 #include <sys/protosw.h> 113 #include <sys/socket.h> 114 #include <sys/time.h> 115 #include <sys/kernel.h> 116 #include <sys/syslog.h> 117 #include <sys/sysctl.h> 118 119 #include <net/if.h> 120 #include <net/route.h> 121 122 #include <netinet/in.h> 123 #include <netinet/in_systm.h> 124 #include <netinet/in_var.h> 125 #include <netinet/ip.h> 126 #include <netinet/ip_icmp.h> 127 #include <netinet/ip_var.h> 128 #include <netinet/in_pcb.h> 129 #include <netinet/in_proto.h> 130 #include <netinet/icmp_var.h> 131 132 #ifdef IPSEC 133 #include <netinet6/ipsec.h> 134 #include <netkey/key.h> 135 #endif 136 137 #ifdef FAST_IPSEC 138 #include <netipsec/ipsec.h> 139 #include <netipsec/key.h> 140 #endif /* FAST_IPSEC*/ 141 142 #include <machine/stdarg.h> 143 144 /* 145 * ICMP routines: error generation, receive packet processing, and 146 * routines to turnaround packets back to the originator, and 147 * host table maintenance routines. 148 */ 149 150 int icmpmaskrepl = 0; 151 #ifdef ICMPPRINTFS 152 int icmpprintfs = 0; 153 #endif 154 int icmpreturndatabytes = 8; 155 156 struct icmpstat icmpstat; 157 158 /* 159 * List of callbacks to notify when Path MTU changes are made. 160 */ 161 struct icmp_mtudisc_callback { 162 LIST_ENTRY(icmp_mtudisc_callback) mc_list; 163 void (*mc_func)(struct in_addr); 164 }; 165 166 LIST_HEAD(, icmp_mtudisc_callback) icmp_mtudisc_callbacks = 167 LIST_HEAD_INITIALIZER(&icmp_mtudisc_callbacks); 168 169 #if 0 170 static int ip_next_mtu(int, int); 171 #else 172 /*static*/ int ip_next_mtu(int, int); 173 #endif 174 175 extern int icmperrppslim; 176 static int icmperrpps_count = 0; 177 static struct timeval icmperrppslim_last; 178 static int icmp_rediraccept = 1; 179 static int icmp_redirtimeout = 600; 180 static struct rttimer_queue *icmp_redirect_timeout_q = NULL; 181 182 static void icmp_mtudisc_timeout(struct rtentry *, struct rttimer *); 183 static void icmp_redirect_timeout(struct rtentry *, struct rttimer *); 184 185 static int icmp_ratelimit(const struct in_addr *, const int, const int); 186 187 188 void 189 icmp_init(void) 190 { 191 /* 192 * This is only useful if the user initializes redirtimeout to 193 * something other than zero. 194 */ 195 if (icmp_redirtimeout != 0) { 196 icmp_redirect_timeout_q = 197 rt_timer_queue_create(icmp_redirtimeout); 198 } 199 } 200 201 /* 202 * Register a Path MTU Discovery callback. 203 */ 204 void 205 icmp_mtudisc_callback_register(void (*func)(struct in_addr)) 206 { 207 struct icmp_mtudisc_callback *mc; 208 209 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL; 210 mc = LIST_NEXT(mc, mc_list)) { 211 if (mc->mc_func == func) 212 return; 213 } 214 215 mc = malloc(sizeof(*mc), M_PCB, M_NOWAIT); 216 if (mc == NULL) 217 panic("icmp_mtudisc_callback_register"); 218 219 mc->mc_func = func; 220 LIST_INSERT_HEAD(&icmp_mtudisc_callbacks, mc, mc_list); 221 } 222 223 /* 224 * Generate an error packet of type error 225 * in response to bad packet ip. 226 */ 227 void 228 icmp_error(struct mbuf *n, int type, int code, n_long dest, 229 int destmtu) 230 { 231 struct ip *oip = mtod(n, struct ip *), *nip; 232 unsigned oiplen = oip->ip_hl << 2; 233 struct icmp *icp; 234 struct mbuf *m; 235 unsigned icmplen, mblen; 236 237 #ifdef ICMPPRINTFS 238 if (icmpprintfs) 239 printf("icmp_error(%p, type:%d, code:%d)\n", oip, type, 240 code); 241 #endif 242 if (type != ICMP_REDIRECT) 243 icmpstat.icps_error++; 244 /* 245 * Don't send error if the original packet was encrypted. 246 * Don't send error if not the first fragment of message. 247 * Don't error if the old packet protocol was ICMP 248 * error message, only known informational types. 249 */ 250 if (n->m_flags & M_DECRYPTED) 251 goto freeit; 252 if (oip->ip_off &~ htons(IP_MF|IP_DF)) 253 goto freeit; 254 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 255 n->m_len >= oiplen + ICMP_MINLEN && 256 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) { 257 icmpstat.icps_oldicmp++; 258 goto freeit; 259 } 260 /* Don't send error in response to a multicast or broadcast packet */ 261 if (n->m_flags & (M_BCAST|M_MCAST)) 262 goto freeit; 263 264 /* 265 * First, do a rate limitation check. 266 */ 267 if (icmp_ratelimit(&oip->ip_src, type, code)) { 268 /* XXX stat */ 269 goto freeit; 270 } 271 272 /* 273 * Now, formulate icmp message 274 */ 275 icmplen = oiplen + min(icmpreturndatabytes, 276 ntohs(oip->ip_len) - oiplen); 277 /* 278 * Defend against mbuf chains shorter than oip->ip_len - oiplen: 279 */ 280 mblen = 0; 281 for (m = n; m && (mblen < icmplen); m = m->m_next) 282 mblen += m->m_len; 283 icmplen = min(mblen, icmplen); 284 285 /* 286 * As we are not required to return everything we have, 287 * we return whatever we can return at ease. 288 * 289 * Note that ICMP datagrams longer than 576 octets are out of spec 290 * according to RFC1812; the limit on icmpreturndatabytes below in 291 * icmp_sysctl will keep things below that limit. 292 */ 293 294 KASSERT(ICMP_MINLEN <= MCLBYTES); 295 296 if (icmplen + ICMP_MINLEN > MCLBYTES) 297 icmplen = MCLBYTES - ICMP_MINLEN; 298 299 m = m_gethdr(M_DONTWAIT, MT_HEADER); 300 if (m && (icmplen + ICMP_MINLEN > MHLEN)) { 301 MCLGET(m, M_DONTWAIT); 302 if ((m->m_flags & M_EXT) == 0) { 303 m_freem(m); 304 m = NULL; 305 } 306 } 307 if (m == NULL) 308 goto freeit; 309 MCLAIM(m, n->m_owner); 310 m->m_len = icmplen + ICMP_MINLEN; 311 if ((m->m_flags & M_EXT) == 0) 312 MH_ALIGN(m, m->m_len); 313 icp = mtod(m, struct icmp *); 314 if ((u_int)type > ICMP_MAXTYPE) 315 panic("icmp_error"); 316 icmpstat.icps_outhist[type]++; 317 icp->icmp_type = type; 318 if (type == ICMP_REDIRECT) 319 icp->icmp_gwaddr.s_addr = dest; 320 else { 321 icp->icmp_void = 0; 322 /* 323 * The following assignments assume an overlay with the 324 * zeroed icmp_void field. 325 */ 326 if (type == ICMP_PARAMPROB) { 327 icp->icmp_pptr = code; 328 code = 0; 329 } else if (type == ICMP_UNREACH && 330 code == ICMP_UNREACH_NEEDFRAG && destmtu) 331 icp->icmp_nextmtu = htons(destmtu); 332 } 333 334 icp->icmp_code = code; 335 m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); 336 337 /* 338 * Now, copy old ip header (without options) 339 * in front of icmp message. 340 */ 341 if (m->m_data - sizeof(struct ip) < m->m_pktdat) 342 panic("icmp len"); 343 m->m_data -= sizeof(struct ip); 344 m->m_len += sizeof(struct ip); 345 m->m_pkthdr.len = m->m_len; 346 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 347 nip = mtod(m, struct ip *); 348 /* ip_v set in ip_output */ 349 nip->ip_hl = sizeof(struct ip) >> 2; 350 nip->ip_tos = 0; 351 nip->ip_len = htons(m->m_len); 352 /* ip_id set in ip_output */ 353 nip->ip_off = htons(0); 354 /* ip_ttl set in icmp_reflect */ 355 nip->ip_p = IPPROTO_ICMP; 356 nip->ip_src = oip->ip_src; 357 nip->ip_dst = oip->ip_dst; 358 icmp_reflect(m); 359 360 freeit: 361 m_freem(n); 362 } 363 364 struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET }; 365 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET }; 366 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET }; 367 struct sockaddr_in icmpmask = { 8, 0 }; 368 369 /* 370 * Process a received ICMP message. 371 */ 372 void 373 icmp_input(struct mbuf *m, ...) 374 { 375 int proto; 376 struct icmp *icp; 377 struct ip *ip = mtod(m, struct ip *); 378 int icmplen; 379 int i; 380 struct in_ifaddr *ia; 381 void *(*ctlfunc)(int, struct sockaddr *, void *); 382 int code; 383 int hlen; 384 va_list ap; 385 struct rtentry *rt; 386 387 va_start(ap, m); 388 hlen = va_arg(ap, int); 389 proto = va_arg(ap, int); 390 va_end(ap); 391 392 /* 393 * Locate icmp structure in mbuf, and check 394 * that not corrupted and of at least minimum length. 395 */ 396 icmplen = ntohs(ip->ip_len) - hlen; 397 #ifdef ICMPPRINTFS 398 if (icmpprintfs) { 399 printf("icmp_input from `%s' to ", inet_ntoa(ip->ip_src)); 400 printf("`%s', len %d\n", inet_ntoa(ip->ip_dst), icmplen); 401 } 402 #endif 403 if (icmplen < ICMP_MINLEN) { 404 icmpstat.icps_tooshort++; 405 goto freeit; 406 } 407 i = hlen + min(icmplen, ICMP_ADVLENMIN); 408 if (m->m_len < i && (m = m_pullup(m, i)) == 0) { 409 icmpstat.icps_tooshort++; 410 return; 411 } 412 ip = mtod(m, struct ip *); 413 m->m_len -= hlen; 414 m->m_data += hlen; 415 icp = mtod(m, struct icmp *); 416 /* Don't need to assert alignment, here. */ 417 if (in_cksum(m, icmplen)) { 418 icmpstat.icps_checksum++; 419 goto freeit; 420 } 421 m->m_len += hlen; 422 m->m_data -= hlen; 423 424 #ifdef ICMPPRINTFS 425 /* 426 * Message type specific processing. 427 */ 428 if (icmpprintfs) 429 printf("icmp_input(type:%d, code:%d)\n", icp->icmp_type, 430 icp->icmp_code); 431 #endif 432 if (icp->icmp_type > ICMP_MAXTYPE) 433 goto raw; 434 icmpstat.icps_inhist[icp->icmp_type]++; 435 code = icp->icmp_code; 436 switch (icp->icmp_type) { 437 438 case ICMP_UNREACH: 439 switch (code) { 440 case ICMP_UNREACH_NET: 441 case ICMP_UNREACH_HOST: 442 case ICMP_UNREACH_PROTOCOL: 443 case ICMP_UNREACH_PORT: 444 case ICMP_UNREACH_SRCFAIL: 445 code += PRC_UNREACH_NET; 446 break; 447 448 case ICMP_UNREACH_NEEDFRAG: 449 code = PRC_MSGSIZE; 450 break; 451 452 case ICMP_UNREACH_NET_UNKNOWN: 453 case ICMP_UNREACH_NET_PROHIB: 454 case ICMP_UNREACH_TOSNET: 455 code = PRC_UNREACH_NET; 456 break; 457 458 case ICMP_UNREACH_HOST_UNKNOWN: 459 case ICMP_UNREACH_ISOLATED: 460 case ICMP_UNREACH_HOST_PROHIB: 461 case ICMP_UNREACH_TOSHOST: 462 code = PRC_UNREACH_HOST; 463 break; 464 465 default: 466 goto badcode; 467 } 468 goto deliver; 469 470 case ICMP_TIMXCEED: 471 if (code > 1) 472 goto badcode; 473 code += PRC_TIMXCEED_INTRANS; 474 goto deliver; 475 476 case ICMP_PARAMPROB: 477 if (code > 1) 478 goto badcode; 479 code = PRC_PARAMPROB; 480 goto deliver; 481 482 case ICMP_SOURCEQUENCH: 483 if (code) 484 goto badcode; 485 code = PRC_QUENCH; 486 goto deliver; 487 488 deliver: 489 /* 490 * Problem with datagram; advise higher level routines. 491 */ 492 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 493 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 494 icmpstat.icps_badlen++; 495 goto freeit; 496 } 497 if (IN_MULTICAST(icp->icmp_ip.ip_dst.s_addr)) 498 goto badcode; 499 #ifdef ICMPPRINTFS 500 if (icmpprintfs) 501 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 502 #endif 503 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 504 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; 505 if (ctlfunc) 506 (void) (*ctlfunc)(code, sintosa(&icmpsrc), 507 &icp->icmp_ip); 508 break; 509 510 badcode: 511 icmpstat.icps_badcode++; 512 break; 513 514 case ICMP_ECHO: 515 icp->icmp_type = ICMP_ECHOREPLY; 516 goto reflect; 517 518 case ICMP_TSTAMP: 519 if (icmplen < ICMP_TSLEN) { 520 icmpstat.icps_badlen++; 521 break; 522 } 523 icp->icmp_type = ICMP_TSTAMPREPLY; 524 icp->icmp_rtime = iptime(); 525 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 526 goto reflect; 527 528 case ICMP_MASKREQ: 529 if (icmpmaskrepl == 0) 530 break; 531 /* 532 * We are not able to respond with all ones broadcast 533 * unless we receive it over a point-to-point interface. 534 */ 535 if (icmplen < ICMP_MASKLEN) { 536 icmpstat.icps_badlen++; 537 break; 538 } 539 if (ip->ip_dst.s_addr == INADDR_BROADCAST || 540 in_nullhost(ip->ip_dst)) 541 icmpdst.sin_addr = ip->ip_src; 542 else 543 icmpdst.sin_addr = ip->ip_dst; 544 ia = ifatoia(ifaof_ifpforaddr(sintosa(&icmpdst), 545 m->m_pkthdr.rcvif)); 546 if (ia == 0) 547 break; 548 icp->icmp_type = ICMP_MASKREPLY; 549 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 550 if (in_nullhost(ip->ip_src)) { 551 if (ia->ia_ifp->if_flags & IFF_BROADCAST) 552 ip->ip_src = ia->ia_broadaddr.sin_addr; 553 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 554 ip->ip_src = ia->ia_dstaddr.sin_addr; 555 } 556 reflect: 557 icmpstat.icps_reflect++; 558 icmpstat.icps_outhist[icp->icmp_type]++; 559 icmp_reflect(m); 560 return; 561 562 case ICMP_REDIRECT: 563 if (code > 3) 564 goto badcode; 565 if (icmp_rediraccept == 0) 566 goto freeit; 567 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 568 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 569 icmpstat.icps_badlen++; 570 break; 571 } 572 /* 573 * Short circuit routing redirects to force 574 * immediate change in the kernel's routing 575 * tables. The message is also handed to anyone 576 * listening on a raw socket (e.g. the routing 577 * daemon for use in updating its tables). 578 */ 579 icmpgw.sin_addr = ip->ip_src; 580 icmpdst.sin_addr = icp->icmp_gwaddr; 581 #ifdef ICMPPRINTFS 582 if (icmpprintfs) { 583 printf("redirect dst `%s' to ", inet_ntoa(icp->icmp_ip.ip_dst)); 584 printf("`%s'\n", inet_ntoa(icp->icmp_gwaddr)); 585 } 586 #endif 587 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 588 rt = NULL; 589 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst), 590 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, 591 sintosa(&icmpgw), (struct rtentry **)&rt); 592 if (rt != NULL && icmp_redirtimeout != 0) { 593 i = rt_timer_add(rt, icmp_redirect_timeout, 594 icmp_redirect_timeout_q); 595 if (i) 596 log(LOG_ERR, "ICMP: redirect failed to " 597 "register timeout for route to %x, " 598 "code %d\n", 599 icp->icmp_ip.ip_dst.s_addr, i); 600 } 601 if (rt != NULL) 602 rtfree(rt); 603 604 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc)); 605 #if defined(IPSEC) || defined(FAST_IPSEC) 606 key_sa_routechange((struct sockaddr *)&icmpsrc); 607 #endif 608 break; 609 610 /* 611 * No kernel processing for the following; 612 * just fall through to send to raw listener. 613 */ 614 case ICMP_ECHOREPLY: 615 case ICMP_ROUTERADVERT: 616 case ICMP_ROUTERSOLICIT: 617 case ICMP_TSTAMPREPLY: 618 case ICMP_IREQREPLY: 619 case ICMP_MASKREPLY: 620 default: 621 break; 622 } 623 624 raw: 625 rip_input(m, hlen, proto); 626 return; 627 628 freeit: 629 m_freem(m); 630 return; 631 } 632 633 /* 634 * Reflect the ip packet back to the source 635 */ 636 void 637 icmp_reflect(struct mbuf *m) 638 { 639 struct ip *ip = mtod(m, struct ip *); 640 struct in_ifaddr *ia; 641 struct ifaddr *ifa; 642 struct sockaddr_in *sin = 0; 643 struct in_addr t; 644 struct mbuf *opts = 0; 645 int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 646 647 if (!in_canforward(ip->ip_src) && 648 ((ip->ip_src.s_addr & IN_CLASSA_NET) != 649 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) { 650 m_freem(m); /* Bad return address */ 651 goto done; /* ip_output() will check for broadcast */ 652 } 653 t = ip->ip_dst; 654 ip->ip_dst = ip->ip_src; 655 /* 656 * If the incoming packet was addressed directly to us, use 657 * dst as the src for the reply. Otherwise (broadcast or 658 * anonymous), use an address which corresponds to the 659 * incoming interface, with a preference for the address which 660 * corresponds to the route to the destination of the ICMP. 661 */ 662 663 /* Look for packet addressed to us */ 664 INADDR_TO_IA(t, ia); 665 666 /* look for packet sent to broadcast address */ 667 if (ia == NULL && m->m_pkthdr.rcvif && 668 (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) { 669 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) { 670 if (ifa->ifa_addr->sa_family != AF_INET) 671 continue; 672 if (in_hosteq(t,ifatoia(ifa)->ia_broadaddr.sin_addr)) { 673 ia = ifatoia(ifa); 674 break; 675 } 676 } 677 } 678 679 if (ia) 680 sin = &ia->ia_addr; 681 682 icmpdst.sin_addr = t; 683 684 /* 685 * if the packet is addressed somewhere else, compute the 686 * source address for packets routed back to the source, and 687 * use that, if it's an address on the interface which 688 * received the packet 689 */ 690 if (sin == (struct sockaddr_in *)0 && m->m_pkthdr.rcvif) { 691 struct sockaddr_in sin_dst; 692 struct route icmproute; 693 int errornum; 694 695 sin_dst.sin_family = AF_INET; 696 sin_dst.sin_len = sizeof(struct sockaddr_in); 697 sin_dst.sin_addr = ip->ip_dst; 698 bzero(&icmproute, sizeof(icmproute)); 699 errornum = 0; 700 sin = in_selectsrc(&sin_dst, &icmproute, 0, NULL, &errornum); 701 /* errornum is never used */ 702 if (icmproute.ro_rt) 703 RTFREE(icmproute.ro_rt); 704 /* check to make sure sin is a source address on rcvif */ 705 if (sin) { 706 t = sin->sin_addr; 707 sin = (struct sockaddr_in *)0; 708 INADDR_TO_IA(t, ia); 709 while (ia) { 710 if (ia->ia_ifp == m->m_pkthdr.rcvif) { 711 sin = &ia->ia_addr; 712 break; 713 } 714 NEXT_IA_WITH_SAME_ADDR(ia); 715 } 716 } 717 } 718 719 /* 720 * if it was not addressed to us, but the route doesn't go out 721 * the source interface, pick an address on the source 722 * interface. This can happen when routing is asymmetric, or 723 * when the incoming packet was encapsulated 724 */ 725 if (sin == (struct sockaddr_in *)0 && m->m_pkthdr.rcvif) { 726 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) { 727 if (ifa->ifa_addr->sa_family != AF_INET) 728 continue; 729 sin = &(ifatoia(ifa)->ia_addr); 730 break; 731 } 732 } 733 734 /* 735 * The following happens if the packet was not addressed to us, 736 * and was received on an interface with no IP address: 737 * We find the first AF_INET address on the first non-loopback 738 * interface. 739 */ 740 if (sin == (struct sockaddr_in *)0) 741 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 742 if (ia->ia_ifp->if_flags & IFF_LOOPBACK) 743 continue; 744 sin = &ia->ia_addr; 745 break; 746 } 747 748 /* 749 * If we still didn't find an address, punt. We could have an 750 * interface up (and receiving packets) with no address. 751 */ 752 if (sin == (struct sockaddr_in *)0) { 753 m_freem(m); 754 goto done; 755 } 756 757 ip->ip_src = sin->sin_addr; 758 ip->ip_ttl = MAXTTL; 759 760 if (optlen > 0) { 761 u_char *cp; 762 int opt, cnt; 763 u_int len; 764 765 /* 766 * Retrieve any source routing from the incoming packet; 767 * add on any record-route or timestamp options. 768 */ 769 cp = (u_char *) (ip + 1); 770 if ((opts = ip_srcroute()) == 0 && 771 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { 772 MCLAIM(opts, m->m_owner); 773 opts->m_len = sizeof(struct in_addr); 774 *mtod(opts, struct in_addr *) = zeroin_addr; 775 } 776 if (opts) { 777 #ifdef ICMPPRINTFS 778 if (icmpprintfs) 779 printf("icmp_reflect optlen %d rt %d => ", 780 optlen, opts->m_len); 781 #endif 782 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 783 opt = cp[IPOPT_OPTVAL]; 784 if (opt == IPOPT_EOL) 785 break; 786 if (opt == IPOPT_NOP) 787 len = 1; 788 else { 789 if (cnt < IPOPT_OLEN + sizeof(*cp)) 790 break; 791 len = cp[IPOPT_OLEN]; 792 if (len < IPOPT_OLEN + sizeof(*cp) || 793 len > cnt) 794 break; 795 } 796 /* 797 * Should check for overflow, but it "can't happen" 798 */ 799 if (opt == IPOPT_RR || opt == IPOPT_TS || 800 opt == IPOPT_SECURITY) { 801 bcopy((caddr_t)cp, 802 mtod(opts, caddr_t) + opts->m_len, len); 803 opts->m_len += len; 804 } 805 } 806 /* Terminate & pad, if necessary */ 807 if ((cnt = opts->m_len % 4) != 0) { 808 for (; cnt < 4; cnt++) { 809 *(mtod(opts, caddr_t) + opts->m_len) = 810 IPOPT_EOL; 811 opts->m_len++; 812 } 813 } 814 #ifdef ICMPPRINTFS 815 if (icmpprintfs) 816 printf("%d\n", opts->m_len); 817 #endif 818 } 819 /* 820 * Now strip out original options by copying rest of first 821 * mbuf's data back, and adjust the IP length. 822 */ 823 ip->ip_len = htons(ntohs(ip->ip_len) - optlen); 824 ip->ip_hl = sizeof(struct ip) >> 2; 825 m->m_len -= optlen; 826 if (m->m_flags & M_PKTHDR) 827 m->m_pkthdr.len -= optlen; 828 optlen += sizeof(struct ip); 829 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), 830 (unsigned)(m->m_len - sizeof(struct ip))); 831 } 832 m_tag_delete_nonpersistent(m); 833 m->m_flags &= ~(M_BCAST|M_MCAST); 834 835 /* 836 * Clear any in-bound checksum flags for this packet. 837 */ 838 if (m->m_flags & M_PKTHDR) 839 m->m_pkthdr.csum_flags = 0; 840 841 icmp_send(m, opts); 842 done: 843 if (opts) 844 (void)m_free(opts); 845 } 846 847 /* 848 * Send an icmp packet back to the ip level, 849 * after supplying a checksum. 850 */ 851 void 852 icmp_send(struct mbuf *m, struct mbuf *opts) 853 { 854 struct ip *ip = mtod(m, struct ip *); 855 int hlen; 856 struct icmp *icp; 857 858 hlen = ip->ip_hl << 2; 859 m->m_data += hlen; 860 m->m_len -= hlen; 861 icp = mtod(m, struct icmp *); 862 icp->icmp_cksum = 0; 863 icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen); 864 m->m_data -= hlen; 865 m->m_len += hlen; 866 #ifdef ICMPPRINTFS 867 if (icmpprintfs) { 868 printf("icmp_send to destination `%s' from ", inet_ntoa(ip->ip_dst)); 869 printf("`%s'\n", inet_ntoa(ip->ip_src)); 870 } 871 #endif 872 (void) ip_output(m, opts, NULL, 0, 873 (struct ip_moptions *)NULL, (struct socket *)NULL); 874 } 875 876 n_time 877 iptime(void) 878 { 879 struct timeval atv; 880 u_long t; 881 882 microtime(&atv); 883 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 884 return (htonl(t)); 885 } 886 887 /* 888 * sysctl helper routine for net.inet.icmp.returndatabytes. ensures 889 * that the new value is in the correct range. 890 */ 891 static int 892 sysctl_net_inet_icmp_returndatabytes(SYSCTLFN_ARGS) 893 { 894 int error, t; 895 struct sysctlnode node; 896 897 node = *rnode; 898 node.sysctl_data = &t; 899 t = icmpreturndatabytes; 900 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 901 if (error || newp == NULL) 902 return (error); 903 904 if (t < 8 || t > 512) 905 return (EINVAL); 906 icmpreturndatabytes = t; 907 908 return (0); 909 } 910 911 /* 912 * sysctl helper routine for net.inet.icmp.redirtimeout. ensures that 913 * the given value is not less than zero and then resets the timeout 914 * queue. 915 */ 916 static int 917 sysctl_net_inet_icmp_redirtimeout(SYSCTLFN_ARGS) 918 { 919 int error, tmp; 920 struct sysctlnode node; 921 922 node = *rnode; 923 node.sysctl_data = &tmp; 924 tmp = icmp_redirtimeout; 925 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 926 if (error || newp == NULL) 927 return (error); 928 if (tmp < 0) 929 return (EINVAL); 930 icmp_redirtimeout = tmp; 931 932 /* 933 * was it a *defined* side-effect that anyone even *reading* 934 * this value causes these things to happen? 935 */ 936 if (icmp_redirect_timeout_q != NULL) { 937 if (icmp_redirtimeout == 0) { 938 rt_timer_queue_destroy(icmp_redirect_timeout_q, 939 TRUE); 940 icmp_redirect_timeout_q = NULL; 941 } else { 942 rt_timer_queue_change(icmp_redirect_timeout_q, 943 icmp_redirtimeout); 944 } 945 } else if (icmp_redirtimeout > 0) { 946 icmp_redirect_timeout_q = 947 rt_timer_queue_create(icmp_redirtimeout); 948 } 949 950 return (0); 951 } 952 953 SYSCTL_SETUP(sysctl_net_inet_icmp_setup, "sysctl net.inet.icmp subtree setup") 954 { 955 956 sysctl_createv(clog, 0, NULL, NULL, 957 CTLFLAG_PERMANENT, 958 CTLTYPE_NODE, "net", NULL, 959 NULL, 0, NULL, 0, 960 CTL_NET, CTL_EOL); 961 sysctl_createv(clog, 0, NULL, NULL, 962 CTLFLAG_PERMANENT, 963 CTLTYPE_NODE, "inet", NULL, 964 NULL, 0, NULL, 0, 965 CTL_NET, PF_INET, CTL_EOL); 966 sysctl_createv(clog, 0, NULL, NULL, 967 CTLFLAG_PERMANENT, 968 CTLTYPE_NODE, "icmp", 969 SYSCTL_DESCR("ICMPv4 related settings"), 970 NULL, 0, NULL, 0, 971 CTL_NET, PF_INET, IPPROTO_ICMP, CTL_EOL); 972 973 sysctl_createv(clog, 0, NULL, NULL, 974 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 975 CTLTYPE_INT, "maskrepl", 976 SYSCTL_DESCR("Respond to ICMP_MASKREQ messages"), 977 NULL, 0, &icmpmaskrepl, 0, 978 CTL_NET, PF_INET, IPPROTO_ICMP, 979 ICMPCTL_MASKREPL, CTL_EOL); 980 sysctl_createv(clog, 0, NULL, NULL, 981 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 982 CTLTYPE_INT, "returndatabytes", 983 SYSCTL_DESCR("Number of bytes to return in an ICMP " 984 "error message"), 985 sysctl_net_inet_icmp_returndatabytes, 0, 986 &icmpreturndatabytes, 0, 987 CTL_NET, PF_INET, IPPROTO_ICMP, 988 ICMPCTL_RETURNDATABYTES, CTL_EOL); 989 sysctl_createv(clog, 0, NULL, NULL, 990 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 991 CTLTYPE_INT, "errppslimit", 992 SYSCTL_DESCR("Maximum number of outgoing ICMP error " 993 "messages per second"), 994 NULL, 0, &icmperrppslim, 0, 995 CTL_NET, PF_INET, IPPROTO_ICMP, 996 ICMPCTL_ERRPPSLIMIT, CTL_EOL); 997 sysctl_createv(clog, 0, NULL, NULL, 998 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 999 CTLTYPE_INT, "rediraccept", 1000 SYSCTL_DESCR("Accept ICMP_REDIRECT messages"), 1001 NULL, 0, &icmp_rediraccept, 0, 1002 CTL_NET, PF_INET, IPPROTO_ICMP, 1003 ICMPCTL_REDIRACCEPT, CTL_EOL); 1004 sysctl_createv(clog, 0, NULL, NULL, 1005 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1006 CTLTYPE_INT, "redirtimeout", 1007 SYSCTL_DESCR("Lifetime of ICMP_REDIRECT generated " 1008 "routes"), 1009 sysctl_net_inet_icmp_redirtimeout, 0, 1010 &icmp_redirtimeout, 0, 1011 CTL_NET, PF_INET, IPPROTO_ICMP, 1012 ICMPCTL_REDIRTIMEOUT, CTL_EOL); 1013 sysctl_createv(clog, 0, NULL, NULL, 1014 CTLFLAG_PERMANENT, 1015 CTLTYPE_STRUCT, "stats", 1016 SYSCTL_DESCR("ICMP statistics"), 1017 NULL, 0, &icmpstat, sizeof(icmpstat), 1018 CTL_NET, PF_INET, IPPROTO_ICMP, ICMPCTL_STATS, 1019 CTL_EOL); 1020 } 1021 1022 /* Table of common MTUs: */ 1023 1024 static const u_int mtu_table[] = { 1025 65535, 65280, 32000, 17914, 9180, 8166, 1026 4352, 2002, 1492, 1006, 508, 296, 68, 0 1027 }; 1028 1029 void 1030 icmp_mtudisc(struct icmp *icp, struct in_addr faddr) 1031 { 1032 struct icmp_mtudisc_callback *mc; 1033 struct sockaddr *dst = sintosa(&icmpsrc); 1034 struct rtentry *rt; 1035 u_long mtu = ntohs(icp->icmp_nextmtu); /* Why a long? IPv6 */ 1036 int error; 1037 1038 rt = rtalloc1(dst, 1); 1039 if (rt == 0) 1040 return; 1041 1042 /* If we didn't get a host route, allocate one */ 1043 1044 if ((rt->rt_flags & RTF_HOST) == 0) { 1045 struct rtentry *nrt; 1046 1047 error = rtrequest((int) RTM_ADD, dst, 1048 (struct sockaddr *) rt->rt_gateway, 1049 (struct sockaddr *) 0, 1050 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt); 1051 if (error) { 1052 rtfree(rt); 1053 return; 1054 } 1055 nrt->rt_rmx = rt->rt_rmx; 1056 rtfree(rt); 1057 rt = nrt; 1058 } 1059 error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q); 1060 if (error) { 1061 rtfree(rt); 1062 return; 1063 } 1064 1065 if (mtu == 0) { 1066 int i = 0; 1067 1068 mtu = ntohs(icp->icmp_ip.ip_len); 1069 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */ 1070 if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0) 1071 mtu -= (icp->icmp_ip.ip_hl << 2); 1072 1073 /* If we still can't guess a value, try the route */ 1074 1075 if (mtu == 0) { 1076 mtu = rt->rt_rmx.rmx_mtu; 1077 1078 /* If no route mtu, default to the interface mtu */ 1079 1080 if (mtu == 0) 1081 mtu = rt->rt_ifp->if_mtu; 1082 } 1083 1084 for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++) 1085 if (mtu > mtu_table[i]) { 1086 mtu = mtu_table[i]; 1087 break; 1088 } 1089 } 1090 1091 /* 1092 * XXX: RTV_MTU is overloaded, since the admin can set it 1093 * to turn off PMTU for a route, and the kernel can 1094 * set it to indicate a serious problem with PMTU 1095 * on a route. We should be using a separate flag 1096 * for the kernel to indicate this. 1097 */ 1098 1099 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) { 1100 if (mtu < 296 || mtu > rt->rt_ifp->if_mtu) 1101 rt->rt_rmx.rmx_locks |= RTV_MTU; 1102 else if (rt->rt_rmx.rmx_mtu > mtu || 1103 rt->rt_rmx.rmx_mtu == 0) { 1104 icmpstat.icps_pmtuchg++; 1105 rt->rt_rmx.rmx_mtu = mtu; 1106 } 1107 } 1108 1109 if (rt) 1110 rtfree(rt); 1111 1112 /* 1113 * Notify protocols that the MTU for this destination 1114 * has changed. 1115 */ 1116 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL; 1117 mc = LIST_NEXT(mc, mc_list)) 1118 (*mc->mc_func)(faddr); 1119 } 1120 1121 /* 1122 * Return the next larger or smaller MTU plateau (table from RFC 1191) 1123 * given current value MTU. If DIR is less than zero, a larger plateau 1124 * is returned; otherwise, a smaller value is returned. 1125 */ 1126 int 1127 ip_next_mtu(int mtu, int dir) /* XXX */ 1128 { 1129 int i; 1130 1131 for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) { 1132 if (mtu >= mtu_table[i]) 1133 break; 1134 } 1135 1136 if (dir < 0) { 1137 if (i == 0) { 1138 return 0; 1139 } else { 1140 return mtu_table[i - 1]; 1141 } 1142 } else { 1143 if (mtu_table[i] == 0) { 1144 return 0; 1145 } else if (mtu > mtu_table[i]) { 1146 return mtu_table[i]; 1147 } else { 1148 return mtu_table[i + 1]; 1149 } 1150 } 1151 } 1152 1153 static void 1154 icmp_mtudisc_timeout(struct rtentry *rt, struct rttimer *r) 1155 { 1156 if (rt == NULL) 1157 panic("icmp_mtudisc_timeout: bad route to timeout"); 1158 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) == 1159 (RTF_DYNAMIC | RTF_HOST)) { 1160 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt), 1161 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); 1162 } else { 1163 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) { 1164 rt->rt_rmx.rmx_mtu = 0; 1165 } 1166 } 1167 } 1168 1169 static void 1170 icmp_redirect_timeout(struct rtentry *rt, struct rttimer *r) 1171 { 1172 if (rt == NULL) 1173 panic("icmp_redirect_timeout: bad route to timeout"); 1174 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) == 1175 (RTF_DYNAMIC | RTF_HOST)) { 1176 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt), 1177 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); 1178 } 1179 } 1180 1181 /* 1182 * Perform rate limit check. 1183 * Returns 0 if it is okay to send the icmp packet. 1184 * Returns 1 if the router SHOULD NOT send this icmp packet due to rate 1185 * limitation. 1186 * 1187 * XXX per-destination/type check necessary? 1188 */ 1189 /* "type" and "code" are not used at this moment */ 1190 static int 1191 icmp_ratelimit(const struct in_addr *dst, const int type, const int code) 1192 { 1193 1194 /* PPS limit */ 1195 if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count, 1196 icmperrppslim)) { 1197 /* The packet is subject to rate limit */ 1198 return 1; 1199 } 1200 1201 /* okay to send */ 1202 return 0; 1203 } 1204