1 /* $NetBSD: ip_icmp.c,v 1.93 2005/07/19 17:00:02 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.93 2005/07/19 17:00:02 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 struct ifnet *destifp) 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(%x, %d, %d)\n", oip, type, code); 240 #endif 241 if (type != ICMP_REDIRECT) 242 icmpstat.icps_error++; 243 /* 244 * Don't send error if the original packet was encrypted. 245 * Don't send error if not the first fragment of message. 246 * Don't error if the old packet protocol was ICMP 247 * error message, only known informational types. 248 */ 249 if (n->m_flags & M_DECRYPTED) 250 goto freeit; 251 if (oip->ip_off &~ htons(IP_MF|IP_DF)) 252 goto freeit; 253 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 254 n->m_len >= oiplen + ICMP_MINLEN && 255 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) { 256 icmpstat.icps_oldicmp++; 257 goto freeit; 258 } 259 /* Don't send error in response to a multicast or broadcast packet */ 260 if (n->m_flags & (M_BCAST|M_MCAST)) 261 goto freeit; 262 263 /* 264 * First, do a rate limitation check. 265 */ 266 if (icmp_ratelimit(&oip->ip_src, type, code)) { 267 /* XXX stat */ 268 goto freeit; 269 } 270 271 /* 272 * Now, formulate icmp message 273 */ 274 icmplen = oiplen + min(icmpreturndatabytes, 275 ntohs(oip->ip_len) - oiplen); 276 /* 277 * Defend against mbuf chains shorter than oip->ip_len - oiplen: 278 */ 279 mblen = 0; 280 for (m = n; m && (mblen < icmplen); m = m->m_next) 281 mblen += m->m_len; 282 icmplen = min(mblen, icmplen); 283 284 /* 285 * As we are not required to return everything we have, 286 * we return whatever we can return at ease. 287 * 288 * Note that ICMP datagrams longer than 576 octets are out of spec 289 * according to RFC1812; the limit on icmpreturndatabytes below in 290 * icmp_sysctl will keep things below that limit. 291 */ 292 293 KASSERT(ICMP_MINLEN <= MCLBYTES); 294 295 if (icmplen + ICMP_MINLEN > MCLBYTES) 296 icmplen = MCLBYTES - ICMP_MINLEN; 297 298 m = m_gethdr(M_DONTWAIT, MT_HEADER); 299 if (m && (icmplen + ICMP_MINLEN > MHLEN)) { 300 MCLGET(m, M_DONTWAIT); 301 if ((m->m_flags & M_EXT) == 0) { 302 m_freem(m); 303 m = NULL; 304 } 305 } 306 if (m == NULL) 307 goto freeit; 308 MCLAIM(m, n->m_owner); 309 m->m_len = icmplen + ICMP_MINLEN; 310 if ((m->m_flags & M_EXT) == 0) 311 MH_ALIGN(m, m->m_len); 312 icp = mtod(m, struct icmp *); 313 if ((u_int)type > ICMP_MAXTYPE) 314 panic("icmp_error"); 315 icmpstat.icps_outhist[type]++; 316 icp->icmp_type = type; 317 if (type == ICMP_REDIRECT) 318 icp->icmp_gwaddr.s_addr = dest; 319 else { 320 icp->icmp_void = 0; 321 /* 322 * The following assignments assume an overlay with the 323 * zeroed icmp_void field. 324 */ 325 if (type == ICMP_PARAMPROB) { 326 icp->icmp_pptr = code; 327 code = 0; 328 } else if (type == ICMP_UNREACH && 329 code == ICMP_UNREACH_NEEDFRAG && destifp) 330 icp->icmp_nextmtu = htons(destifp->if_mtu); 331 } 332 333 icp->icmp_code = code; 334 m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); 335 nip = &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 %x to %x, len %d\n", 400 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), 401 icmplen); 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 %x to %x\n", icp->icmp_ip.ip_dst, 584 icp->icmp_gwaddr); 585 #endif 586 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 587 rt = NULL; 588 rtredirect(sintosa(&icmpsrc), sintosa(&icmpdst), 589 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, 590 sintosa(&icmpgw), (struct rtentry **)&rt); 591 if (rt != NULL && icmp_redirtimeout != 0) { 592 i = rt_timer_add(rt, icmp_redirect_timeout, 593 icmp_redirect_timeout_q); 594 if (i) 595 log(LOG_ERR, "ICMP: redirect failed to " 596 "register timeout for route to %x, " 597 "code %d\n", 598 icp->icmp_ip.ip_dst.s_addr, i); 599 } 600 if (rt != NULL) 601 rtfree(rt); 602 603 pfctlinput(PRC_REDIRECT_HOST, sintosa(&icmpsrc)); 604 #if defined(IPSEC) || defined(FAST_IPSEC) 605 key_sa_routechange((struct sockaddr *)&icmpsrc); 606 #endif 607 break; 608 609 /* 610 * No kernel processing for the following; 611 * just fall through to send to raw listener. 612 */ 613 case ICMP_ECHOREPLY: 614 case ICMP_ROUTERADVERT: 615 case ICMP_ROUTERSOLICIT: 616 case ICMP_TSTAMPREPLY: 617 case ICMP_IREQREPLY: 618 case ICMP_MASKREPLY: 619 default: 620 break; 621 } 622 623 raw: 624 rip_input(m, hlen, proto); 625 return; 626 627 freeit: 628 m_freem(m); 629 return; 630 } 631 632 /* 633 * Reflect the ip packet back to the source 634 */ 635 void 636 icmp_reflect(struct mbuf *m) 637 { 638 struct ip *ip = mtod(m, struct ip *); 639 struct in_ifaddr *ia; 640 struct ifaddr *ifa; 641 struct sockaddr_in *sin = 0; 642 struct in_addr t; 643 struct mbuf *opts = 0; 644 int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 645 646 if (!in_canforward(ip->ip_src) && 647 ((ip->ip_src.s_addr & IN_CLASSA_NET) != 648 htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) { 649 m_freem(m); /* Bad return address */ 650 goto done; /* ip_output() will check for broadcast */ 651 } 652 t = ip->ip_dst; 653 ip->ip_dst = ip->ip_src; 654 /* 655 * If the incoming packet was addressed directly to us, use 656 * dst as the src for the reply. Otherwise (broadcast or 657 * anonymous), use an address which corresponds to the 658 * incoming interface, with a preference for the address which 659 * corresponds to the route to the destination of the ICMP. 660 */ 661 662 /* Look for packet addressed to us */ 663 INADDR_TO_IA(t, ia); 664 665 /* look for packet sent to broadcast address */ 666 if (ia == NULL && m->m_pkthdr.rcvif && 667 (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST)) { 668 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) { 669 if (ifa->ifa_addr->sa_family != AF_INET) 670 continue; 671 if (in_hosteq(t,ifatoia(ifa)->ia_broadaddr.sin_addr)) { 672 ia = ifatoia(ifa); 673 break; 674 } 675 } 676 } 677 678 if (ia) 679 sin = &ia->ia_addr; 680 681 icmpdst.sin_addr = t; 682 683 /* 684 * if the packet is addressed somewhere else, compute the 685 * source address for packets routed back to the source, and 686 * use that, if it's an address on the interface which 687 * received the packet 688 */ 689 if (sin == (struct sockaddr_in *)0 && m->m_pkthdr.rcvif) { 690 struct sockaddr_in sin_dst; 691 struct route icmproute; 692 int errornum; 693 694 sin_dst.sin_family = AF_INET; 695 sin_dst.sin_len = sizeof(struct sockaddr_in); 696 sin_dst.sin_addr = ip->ip_dst; 697 bzero(&icmproute, sizeof(icmproute)); 698 errornum = 0; 699 sin = in_selectsrc(&sin_dst, &icmproute, 0, NULL, &errornum); 700 /* errornum is never used */ 701 if (icmproute.ro_rt) 702 RTFREE(icmproute.ro_rt); 703 /* check to make sure sin is a source address on rcvif */ 704 if (sin) { 705 t = sin->sin_addr; 706 sin = (struct sockaddr_in *)0; 707 INADDR_TO_IA(t, ia); 708 while (ia) { 709 if (ia->ia_ifp == m->m_pkthdr.rcvif) { 710 sin = &ia->ia_addr; 711 break; 712 } 713 NEXT_IA_WITH_SAME_ADDR(ia); 714 } 715 } 716 } 717 718 /* 719 * if it was not addressed to us, but the route doesn't go out 720 * the source interface, pick an address on the source 721 * interface. This can happen when routing is asymmetric, or 722 * when the incoming packet was encapsulated 723 */ 724 if (sin == (struct sockaddr_in *)0 && m->m_pkthdr.rcvif) { 725 IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) { 726 if (ifa->ifa_addr->sa_family != AF_INET) 727 continue; 728 sin = &(ifatoia(ifa)->ia_addr); 729 break; 730 } 731 } 732 733 /* 734 * The following happens if the packet was not addressed to us, 735 * and was received on an interface with no IP address: 736 * We find the first AF_INET address on the first non-loopback 737 * interface. 738 */ 739 if (sin == (struct sockaddr_in *)0) 740 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) { 741 if (ia->ia_ifp->if_flags & IFF_LOOPBACK) 742 continue; 743 sin = &ia->ia_addr; 744 break; 745 } 746 747 /* 748 * If we still didn't find an address, punt. We could have an 749 * interface up (and receiving packets) with no address. 750 */ 751 if (sin == (struct sockaddr_in *)0) { 752 m_freem(m); 753 goto done; 754 } 755 756 ip->ip_src = sin->sin_addr; 757 ip->ip_ttl = MAXTTL; 758 759 if (optlen > 0) { 760 u_char *cp; 761 int opt, cnt; 762 u_int len; 763 764 /* 765 * Retrieve any source routing from the incoming packet; 766 * add on any record-route or timestamp options. 767 */ 768 cp = (u_char *) (ip + 1); 769 if ((opts = ip_srcroute()) == 0 && 770 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { 771 MCLAIM(opts, m->m_owner); 772 opts->m_len = sizeof(struct in_addr); 773 *mtod(opts, struct in_addr *) = zeroin_addr; 774 } 775 if (opts) { 776 #ifdef ICMPPRINTFS 777 if (icmpprintfs) 778 printf("icmp_reflect optlen %d rt %d => ", 779 optlen, opts->m_len); 780 #endif 781 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 782 opt = cp[IPOPT_OPTVAL]; 783 if (opt == IPOPT_EOL) 784 break; 785 if (opt == IPOPT_NOP) 786 len = 1; 787 else { 788 if (cnt < IPOPT_OLEN + sizeof(*cp)) 789 break; 790 len = cp[IPOPT_OLEN]; 791 if (len < IPOPT_OLEN + sizeof(*cp) || 792 len > cnt) 793 break; 794 } 795 /* 796 * Should check for overflow, but it "can't happen" 797 */ 798 if (opt == IPOPT_RR || opt == IPOPT_TS || 799 opt == IPOPT_SECURITY) { 800 bcopy((caddr_t)cp, 801 mtod(opts, caddr_t) + opts->m_len, len); 802 opts->m_len += len; 803 } 804 } 805 /* Terminate & pad, if necessary */ 806 if ((cnt = opts->m_len % 4) != 0) { 807 for (; cnt < 4; cnt++) { 808 *(mtod(opts, caddr_t) + opts->m_len) = 809 IPOPT_EOL; 810 opts->m_len++; 811 } 812 } 813 #ifdef ICMPPRINTFS 814 if (icmpprintfs) 815 printf("%d\n", opts->m_len); 816 #endif 817 } 818 /* 819 * Now strip out original options by copying rest of first 820 * mbuf's data back, and adjust the IP length. 821 */ 822 ip->ip_len = htons(ntohs(ip->ip_len) - optlen); 823 ip->ip_hl = sizeof(struct ip) >> 2; 824 m->m_len -= optlen; 825 if (m->m_flags & M_PKTHDR) 826 m->m_pkthdr.len -= optlen; 827 optlen += sizeof(struct ip); 828 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), 829 (unsigned)(m->m_len - sizeof(struct ip))); 830 } 831 m_tag_delete_nonpersistent(m); 832 m->m_flags &= ~(M_BCAST|M_MCAST); 833 834 /* 835 * Clear any in-bound checksum flags for this packet. 836 */ 837 if (m->m_flags & M_PKTHDR) 838 m->m_pkthdr.csum_flags = 0; 839 840 icmp_send(m, opts); 841 done: 842 if (opts) 843 (void)m_free(opts); 844 } 845 846 /* 847 * Send an icmp packet back to the ip level, 848 * after supplying a checksum. 849 */ 850 void 851 icmp_send(struct mbuf *m, struct mbuf *opts) 852 { 853 struct ip *ip = mtod(m, struct ip *); 854 int hlen; 855 struct icmp *icp; 856 857 hlen = ip->ip_hl << 2; 858 m->m_data += hlen; 859 m->m_len -= hlen; 860 icp = mtod(m, struct icmp *); 861 icp->icmp_cksum = 0; 862 icp->icmp_cksum = in_cksum(m, ntohs(ip->ip_len) - hlen); 863 m->m_data -= hlen; 864 m->m_len += hlen; 865 #ifdef ICMPPRINTFS 866 if (icmpprintfs) 867 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src); 868 #endif 869 (void) ip_output(m, opts, NULL, 0, 870 (struct ip_moptions *)NULL, (struct socket *)NULL); 871 } 872 873 n_time 874 iptime(void) 875 { 876 struct timeval atv; 877 u_long t; 878 879 microtime(&atv); 880 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 881 return (htonl(t)); 882 } 883 884 /* 885 * sysctl helper routine for net.inet.icmp.returndatabytes. ensures 886 * that the new value is in the correct range. 887 */ 888 static int 889 sysctl_net_inet_icmp_returndatabytes(SYSCTLFN_ARGS) 890 { 891 int error, t; 892 struct sysctlnode node; 893 894 node = *rnode; 895 node.sysctl_data = &t; 896 t = icmpreturndatabytes; 897 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 898 if (error || newp == NULL) 899 return (error); 900 901 if (t < 8 || t > 512) 902 return (EINVAL); 903 icmpreturndatabytes = t; 904 905 return (0); 906 } 907 908 /* 909 * sysctl helper routine for net.inet.icmp.redirtimeout. ensures that 910 * the given value is not less than zero and then resets the timeout 911 * queue. 912 */ 913 static int 914 sysctl_net_inet_icmp_redirtimeout(SYSCTLFN_ARGS) 915 { 916 int error, tmp; 917 struct sysctlnode node; 918 919 node = *rnode; 920 node.sysctl_data = &tmp; 921 tmp = icmp_redirtimeout; 922 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 923 if (error || newp == NULL) 924 return (error); 925 if (tmp < 0) 926 return (EINVAL); 927 icmp_redirtimeout = tmp; 928 929 /* 930 * was it a *defined* side-effect that anyone even *reading* 931 * this value causes these things to happen? 932 */ 933 if (icmp_redirect_timeout_q != NULL) { 934 if (icmp_redirtimeout == 0) { 935 rt_timer_queue_destroy(icmp_redirect_timeout_q, 936 TRUE); 937 icmp_redirect_timeout_q = NULL; 938 } else { 939 rt_timer_queue_change(icmp_redirect_timeout_q, 940 icmp_redirtimeout); 941 } 942 } else if (icmp_redirtimeout > 0) { 943 icmp_redirect_timeout_q = 944 rt_timer_queue_create(icmp_redirtimeout); 945 } 946 947 return (0); 948 } 949 950 SYSCTL_SETUP(sysctl_net_inet_icmp_setup, "sysctl net.inet.icmp subtree setup") 951 { 952 953 sysctl_createv(clog, 0, NULL, NULL, 954 CTLFLAG_PERMANENT, 955 CTLTYPE_NODE, "net", NULL, 956 NULL, 0, NULL, 0, 957 CTL_NET, CTL_EOL); 958 sysctl_createv(clog, 0, NULL, NULL, 959 CTLFLAG_PERMANENT, 960 CTLTYPE_NODE, "inet", NULL, 961 NULL, 0, NULL, 0, 962 CTL_NET, PF_INET, CTL_EOL); 963 sysctl_createv(clog, 0, NULL, NULL, 964 CTLFLAG_PERMANENT, 965 CTLTYPE_NODE, "icmp", 966 SYSCTL_DESCR("ICMPv4 related settings"), 967 NULL, 0, NULL, 0, 968 CTL_NET, PF_INET, IPPROTO_ICMP, CTL_EOL); 969 970 sysctl_createv(clog, 0, NULL, NULL, 971 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 972 CTLTYPE_INT, "maskrepl", 973 SYSCTL_DESCR("Respond to ICMP_MASKREQ messages"), 974 NULL, 0, &icmpmaskrepl, 0, 975 CTL_NET, PF_INET, IPPROTO_ICMP, 976 ICMPCTL_MASKREPL, CTL_EOL); 977 sysctl_createv(clog, 0, NULL, NULL, 978 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 979 CTLTYPE_INT, "returndatabytes", 980 SYSCTL_DESCR("Number of bytes to return in an ICMP " 981 "error message"), 982 sysctl_net_inet_icmp_returndatabytes, 0, 983 &icmpreturndatabytes, 0, 984 CTL_NET, PF_INET, IPPROTO_ICMP, 985 ICMPCTL_RETURNDATABYTES, CTL_EOL); 986 sysctl_createv(clog, 0, NULL, NULL, 987 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 988 CTLTYPE_INT, "errppslimit", 989 SYSCTL_DESCR("Maximum number of outgoing ICMP error " 990 "messages per second"), 991 NULL, 0, &icmperrppslim, 0, 992 CTL_NET, PF_INET, IPPROTO_ICMP, 993 ICMPCTL_ERRPPSLIMIT, CTL_EOL); 994 sysctl_createv(clog, 0, NULL, NULL, 995 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 996 CTLTYPE_INT, "rediraccept", 997 SYSCTL_DESCR("Accept ICMP_REDIRECT messages"), 998 NULL, 0, &icmp_rediraccept, 0, 999 CTL_NET, PF_INET, IPPROTO_ICMP, 1000 ICMPCTL_REDIRACCEPT, CTL_EOL); 1001 sysctl_createv(clog, 0, NULL, NULL, 1002 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1003 CTLTYPE_INT, "redirtimeout", 1004 SYSCTL_DESCR("Lifetime of ICMP_REDIRECT generated " 1005 "routes"), 1006 sysctl_net_inet_icmp_redirtimeout, 0, 1007 &icmp_redirtimeout, 0, 1008 CTL_NET, PF_INET, IPPROTO_ICMP, 1009 ICMPCTL_REDIRTIMEOUT, CTL_EOL); 1010 } 1011 1012 /* Table of common MTUs: */ 1013 1014 static const u_int mtu_table[] = { 1015 65535, 65280, 32000, 17914, 9180, 8166, 1016 4352, 2002, 1492, 1006, 508, 296, 68, 0 1017 }; 1018 1019 void 1020 icmp_mtudisc(struct icmp *icp, struct in_addr faddr) 1021 { 1022 struct icmp_mtudisc_callback *mc; 1023 struct sockaddr *dst = sintosa(&icmpsrc); 1024 struct rtentry *rt; 1025 u_long mtu = ntohs(icp->icmp_nextmtu); /* Why a long? IPv6 */ 1026 int error; 1027 1028 rt = rtalloc1(dst, 1); 1029 if (rt == 0) 1030 return; 1031 1032 /* If we didn't get a host route, allocate one */ 1033 1034 if ((rt->rt_flags & RTF_HOST) == 0) { 1035 struct rtentry *nrt; 1036 1037 error = rtrequest((int) RTM_ADD, dst, 1038 (struct sockaddr *) rt->rt_gateway, 1039 (struct sockaddr *) 0, 1040 RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt); 1041 if (error) { 1042 rtfree(rt); 1043 return; 1044 } 1045 nrt->rt_rmx = rt->rt_rmx; 1046 rtfree(rt); 1047 rt = nrt; 1048 } 1049 error = rt_timer_add(rt, icmp_mtudisc_timeout, ip_mtudisc_timeout_q); 1050 if (error) { 1051 rtfree(rt); 1052 return; 1053 } 1054 1055 if (mtu == 0) { 1056 int i = 0; 1057 1058 mtu = ntohs(icp->icmp_ip.ip_len); 1059 /* Some 4.2BSD-based routers incorrectly adjust the ip_len */ 1060 if (mtu > rt->rt_rmx.rmx_mtu && rt->rt_rmx.rmx_mtu != 0) 1061 mtu -= (icp->icmp_ip.ip_hl << 2); 1062 1063 /* If we still can't guess a value, try the route */ 1064 1065 if (mtu == 0) { 1066 mtu = rt->rt_rmx.rmx_mtu; 1067 1068 /* If no route mtu, default to the interface mtu */ 1069 1070 if (mtu == 0) 1071 mtu = rt->rt_ifp->if_mtu; 1072 } 1073 1074 for (i = 0; i < sizeof(mtu_table) / sizeof(mtu_table[0]); i++) 1075 if (mtu > mtu_table[i]) { 1076 mtu = mtu_table[i]; 1077 break; 1078 } 1079 } 1080 1081 /* 1082 * XXX: RTV_MTU is overloaded, since the admin can set it 1083 * to turn off PMTU for a route, and the kernel can 1084 * set it to indicate a serious problem with PMTU 1085 * on a route. We should be using a separate flag 1086 * for the kernel to indicate this. 1087 */ 1088 1089 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) { 1090 if (mtu < 296 || mtu > rt->rt_ifp->if_mtu) 1091 rt->rt_rmx.rmx_locks |= RTV_MTU; 1092 else if (rt->rt_rmx.rmx_mtu > mtu || 1093 rt->rt_rmx.rmx_mtu == 0) { 1094 icmpstat.icps_pmtuchg++; 1095 rt->rt_rmx.rmx_mtu = mtu; 1096 } 1097 } 1098 1099 if (rt) 1100 rtfree(rt); 1101 1102 /* 1103 * Notify protocols that the MTU for this destination 1104 * has changed. 1105 */ 1106 for (mc = LIST_FIRST(&icmp_mtudisc_callbacks); mc != NULL; 1107 mc = LIST_NEXT(mc, mc_list)) 1108 (*mc->mc_func)(faddr); 1109 } 1110 1111 /* 1112 * Return the next larger or smaller MTU plateau (table from RFC 1191) 1113 * given current value MTU. If DIR is less than zero, a larger plateau 1114 * is returned; otherwise, a smaller value is returned. 1115 */ 1116 int 1117 ip_next_mtu(int mtu, int dir) /* XXX */ 1118 { 1119 int i; 1120 1121 for (i = 0; i < (sizeof mtu_table) / (sizeof mtu_table[0]); i++) { 1122 if (mtu >= mtu_table[i]) 1123 break; 1124 } 1125 1126 if (dir < 0) { 1127 if (i == 0) { 1128 return 0; 1129 } else { 1130 return mtu_table[i - 1]; 1131 } 1132 } else { 1133 if (mtu_table[i] == 0) { 1134 return 0; 1135 } else if (mtu > mtu_table[i]) { 1136 return mtu_table[i]; 1137 } else { 1138 return mtu_table[i + 1]; 1139 } 1140 } 1141 } 1142 1143 static void 1144 icmp_mtudisc_timeout(struct rtentry *rt, struct rttimer *r) 1145 { 1146 if (rt == NULL) 1147 panic("icmp_mtudisc_timeout: bad route to timeout"); 1148 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) == 1149 (RTF_DYNAMIC | RTF_HOST)) { 1150 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt), 1151 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); 1152 } else { 1153 if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) { 1154 rt->rt_rmx.rmx_mtu = 0; 1155 } 1156 } 1157 } 1158 1159 static void 1160 icmp_redirect_timeout(struct rtentry *rt, struct rttimer *r) 1161 { 1162 if (rt == NULL) 1163 panic("icmp_redirect_timeout: bad route to timeout"); 1164 if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) == 1165 (RTF_DYNAMIC | RTF_HOST)) { 1166 rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt), 1167 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0); 1168 } 1169 } 1170 1171 /* 1172 * Perform rate limit check. 1173 * Returns 0 if it is okay to send the icmp packet. 1174 * Returns 1 if the router SHOULD NOT send this icmp packet due to rate 1175 * limitation. 1176 * 1177 * XXX per-destination/type check necessary? 1178 */ 1179 /* "type" and "code" are not used at this moment */ 1180 static int 1181 icmp_ratelimit(const struct in_addr *dst, const int type, const int code) 1182 { 1183 1184 /* PPS limit */ 1185 if (!ppsratecheck(&icmperrppslim_last, &icmperrpps_count, 1186 icmperrppslim)) { 1187 /* The packet is subject to rate limit */ 1188 return 1; 1189 } 1190 1191 /* okay to send */ 1192 return 0; 1193 } 1194