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