1 /* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 34 * $FreeBSD$ 35 */ 36 37 #include "opt_ipsec.h" 38 #include "opt_mac.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/mac.h> 43 #include <sys/mbuf.h> 44 #include <sys/protosw.h> 45 #include <sys/socket.h> 46 #include <sys/time.h> 47 #include <sys/kernel.h> 48 #include <sys/sysctl.h> 49 50 #include <net/if.h> 51 #include <net/if_types.h> 52 #include <net/route.h> 53 54 #include <netinet/in.h> 55 #include <netinet/in_systm.h> 56 #include <netinet/in_var.h> 57 #include <netinet/ip.h> 58 #include <netinet/ip_icmp.h> 59 #include <netinet/ip_var.h> 60 #include <netinet/icmp_var.h> 61 62 #ifdef IPSEC 63 #include <netinet6/ipsec.h> 64 #include <netkey/key.h> 65 #endif 66 67 #ifdef FAST_IPSEC 68 #include <netipsec/ipsec.h> 69 #include <netipsec/key.h> 70 #define IPSEC 71 #endif 72 73 #include <machine/in_cksum.h> 74 75 /* 76 * ICMP routines: error generation, receive packet processing, and 77 * routines to turnaround packets back to the originator, and 78 * host table maintenance routines. 79 */ 80 81 static struct icmpstat icmpstat; 82 SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RW, 83 &icmpstat, icmpstat, ""); 84 85 static int icmpmaskrepl = 0; 86 SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW, 87 &icmpmaskrepl, 0, "Reply to ICMP Address Mask Request packets."); 88 89 static u_int icmpmaskfake = 0; 90 SYSCTL_UINT(_net_inet_icmp, OID_AUTO, maskfake, CTLFLAG_RW, 91 &icmpmaskfake, 0, "Fake reply to ICMP Address Mask Request packets."); 92 93 static int drop_redirect = 0; 94 SYSCTL_INT(_net_inet_icmp, OID_AUTO, drop_redirect, CTLFLAG_RW, 95 &drop_redirect, 0, ""); 96 97 static int log_redirect = 0; 98 SYSCTL_INT(_net_inet_icmp, OID_AUTO, log_redirect, CTLFLAG_RW, 99 &log_redirect, 0, ""); 100 101 static int icmplim = 200; 102 SYSCTL_INT(_net_inet_icmp, ICMPCTL_ICMPLIM, icmplim, CTLFLAG_RW, 103 &icmplim, 0, ""); 104 105 static int icmplim_output = 1; 106 SYSCTL_INT(_net_inet_icmp, OID_AUTO, icmplim_output, CTLFLAG_RW, 107 &icmplim_output, 0, ""); 108 109 /* 110 * ICMP broadcast echo sysctl 111 */ 112 113 static int icmpbmcastecho = 0; 114 SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW, 115 &icmpbmcastecho, 0, ""); 116 117 118 #ifdef ICMPPRINTFS 119 int icmpprintfs = 0; 120 #endif 121 122 static void icmp_reflect(struct mbuf *); 123 static void icmp_send(struct mbuf *, struct mbuf *, struct route *); 124 static int ip_next_mtu(int, int); 125 126 extern struct protosw inetsw[]; 127 128 /* 129 * Generate an error packet of type error 130 * in response to bad packet ip. 131 */ 132 void 133 icmp_error(n, type, code, dest, destifp) 134 struct mbuf *n; 135 int type, code; 136 n_long dest; 137 struct ifnet *destifp; 138 { 139 register struct ip *oip = mtod(n, struct ip *), *nip; 140 register unsigned oiplen = oip->ip_hl << 2; 141 register struct icmp *icp; 142 register struct mbuf *m; 143 unsigned icmplen; 144 145 #ifdef ICMPPRINTFS 146 if (icmpprintfs) 147 printf("icmp_error(%p, %x, %d)\n", oip, type, code); 148 #endif 149 if (type != ICMP_REDIRECT) 150 icmpstat.icps_error++; 151 /* 152 * Don't send error if not the first fragment of message. 153 * Don't error if the old packet protocol was ICMP 154 * error message, only known informational types. 155 */ 156 if (oip->ip_off &~ (IP_MF|IP_DF)) 157 goto freeit; 158 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 159 n->m_len >= oiplen + ICMP_MINLEN && 160 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) { 161 icmpstat.icps_oldicmp++; 162 goto freeit; 163 } 164 /* Don't send error in response to a multicast or broadcast packet */ 165 if (n->m_flags & (M_BCAST|M_MCAST)) 166 goto freeit; 167 /* 168 * First, formulate icmp message 169 */ 170 m = m_gethdr(M_DONTWAIT, MT_HEADER); 171 if (m == NULL) 172 goto freeit; 173 #ifdef MAC 174 mac_create_mbuf_netlayer(n, m); 175 #endif 176 icmplen = min(oiplen + 8, oip->ip_len); 177 if (icmplen < sizeof(struct ip)) 178 panic("icmp_error: bad length"); 179 m->m_len = icmplen + ICMP_MINLEN; 180 MH_ALIGN(m, m->m_len); 181 icp = mtod(m, struct icmp *); 182 if ((u_int)type > ICMP_MAXTYPE) 183 panic("icmp_error"); 184 icmpstat.icps_outhist[type]++; 185 icp->icmp_type = type; 186 if (type == ICMP_REDIRECT) 187 icp->icmp_gwaddr.s_addr = dest; 188 else { 189 icp->icmp_void = 0; 190 /* 191 * The following assignments assume an overlay with the 192 * zeroed icmp_void field. 193 */ 194 if (type == ICMP_PARAMPROB) { 195 icp->icmp_pptr = code; 196 code = 0; 197 } else if (type == ICMP_UNREACH && 198 code == ICMP_UNREACH_NEEDFRAG && destifp) { 199 icp->icmp_nextmtu = htons(destifp->if_mtu); 200 } 201 } 202 203 icp->icmp_code = code; 204 m_copydata(n, 0, icmplen, (caddr_t)&icp->icmp_ip); 205 nip = &icp->icmp_ip; 206 207 /* 208 * Convert fields to network representation. 209 */ 210 nip->ip_len = htons(nip->ip_len); 211 nip->ip_off = htons(nip->ip_off); 212 213 /* 214 * Now, copy old ip header (without options) 215 * in front of icmp message. 216 */ 217 if (m->m_data - sizeof(struct ip) < m->m_pktdat) 218 panic("icmp len"); 219 m->m_data -= sizeof(struct ip); 220 m->m_len += sizeof(struct ip); 221 m->m_pkthdr.len = m->m_len; 222 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 223 nip = mtod(m, struct ip *); 224 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); 225 nip->ip_len = m->m_len; 226 nip->ip_v = IPVERSION; 227 nip->ip_hl = 5; 228 nip->ip_p = IPPROTO_ICMP; 229 nip->ip_tos = 0; 230 icmp_reflect(m); 231 232 freeit: 233 m_freem(n); 234 } 235 236 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET }; 237 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET }; 238 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET }; 239 240 /* 241 * Process a received ICMP message. 242 */ 243 void 244 icmp_input(m, off) 245 register struct mbuf *m; 246 int off; 247 { 248 int hlen = off; 249 register struct icmp *icp; 250 register struct ip *ip = mtod(m, struct ip *); 251 int icmplen = ip->ip_len; 252 register int i; 253 struct in_ifaddr *ia; 254 void (*ctlfunc)(int, struct sockaddr *, void *); 255 int code; 256 257 /* 258 * Locate icmp structure in mbuf, and check 259 * that not corrupted and of at least minimum length. 260 */ 261 #ifdef ICMPPRINTFS 262 if (icmpprintfs) { 263 char buf[4 * sizeof "123"]; 264 strcpy(buf, inet_ntoa(ip->ip_src)); 265 printf("icmp_input from %s to %s, len %d\n", 266 buf, inet_ntoa(ip->ip_dst), icmplen); 267 } 268 #endif 269 if (icmplen < ICMP_MINLEN) { 270 icmpstat.icps_tooshort++; 271 goto freeit; 272 } 273 i = hlen + min(icmplen, ICMP_ADVLENMIN); 274 if (m->m_len < i && (m = m_pullup(m, i)) == 0) { 275 icmpstat.icps_tooshort++; 276 return; 277 } 278 ip = mtod(m, struct ip *); 279 m->m_len -= hlen; 280 m->m_data += hlen; 281 icp = mtod(m, struct icmp *); 282 if (in_cksum(m, icmplen)) { 283 icmpstat.icps_checksum++; 284 goto freeit; 285 } 286 m->m_len += hlen; 287 m->m_data -= hlen; 288 289 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 290 /* 291 * Deliver very specific ICMP type only. 292 */ 293 switch (icp->icmp_type) { 294 case ICMP_UNREACH: 295 case ICMP_TIMXCEED: 296 break; 297 default: 298 goto freeit; 299 } 300 } 301 302 #ifdef ICMPPRINTFS 303 if (icmpprintfs) 304 printf("icmp_input, type %d code %d\n", icp->icmp_type, 305 icp->icmp_code); 306 #endif 307 308 /* 309 * Message type specific processing. 310 */ 311 if (icp->icmp_type > ICMP_MAXTYPE) 312 goto raw; 313 icmpstat.icps_inhist[icp->icmp_type]++; 314 code = icp->icmp_code; 315 switch (icp->icmp_type) { 316 317 case ICMP_UNREACH: 318 switch (code) { 319 case ICMP_UNREACH_NET: 320 case ICMP_UNREACH_HOST: 321 case ICMP_UNREACH_SRCFAIL: 322 case ICMP_UNREACH_NET_UNKNOWN: 323 case ICMP_UNREACH_HOST_UNKNOWN: 324 case ICMP_UNREACH_ISOLATED: 325 case ICMP_UNREACH_TOSNET: 326 case ICMP_UNREACH_TOSHOST: 327 case ICMP_UNREACH_HOST_PRECEDENCE: 328 case ICMP_UNREACH_PRECEDENCE_CUTOFF: 329 code = PRC_UNREACH_NET; 330 break; 331 332 case ICMP_UNREACH_NEEDFRAG: 333 code = PRC_MSGSIZE; 334 break; 335 336 /* 337 * RFC 1122, Sections 3.2.2.1 and 4.2.3.9. 338 * Treat subcodes 2,3 as immediate RST 339 */ 340 case ICMP_UNREACH_PROTOCOL: 341 case ICMP_UNREACH_PORT: 342 code = PRC_UNREACH_PORT; 343 break; 344 345 case ICMP_UNREACH_NET_PROHIB: 346 case ICMP_UNREACH_HOST_PROHIB: 347 case ICMP_UNREACH_FILTER_PROHIB: 348 code = PRC_UNREACH_ADMIN_PROHIB; 349 break; 350 351 default: 352 goto badcode; 353 } 354 goto deliver; 355 356 case ICMP_TIMXCEED: 357 if (code > 1) 358 goto badcode; 359 code += PRC_TIMXCEED_INTRANS; 360 goto deliver; 361 362 case ICMP_PARAMPROB: 363 if (code > 1) 364 goto badcode; 365 code = PRC_PARAMPROB; 366 goto deliver; 367 368 case ICMP_SOURCEQUENCH: 369 if (code) 370 goto badcode; 371 code = PRC_QUENCH; 372 deliver: 373 /* 374 * Problem with datagram; advise higher level routines. 375 */ 376 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 377 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 378 icmpstat.icps_badlen++; 379 goto freeit; 380 } 381 icp->icmp_ip.ip_len = ntohs(icp->icmp_ip.ip_len); 382 /* Discard ICMP's in response to multicast packets */ 383 if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) 384 goto badcode; 385 #ifdef ICMPPRINTFS 386 if (icmpprintfs) 387 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 388 #endif 389 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 390 #if 1 391 /* 392 * MTU discovery: 393 * If we got a needfrag and there is a host route to the 394 * original destination, and the MTU is not locked, then 395 * set the MTU in the route to the suggested new value 396 * (if given) and then notify as usual. The ULPs will 397 * notice that the MTU has changed and adapt accordingly. 398 * If no new MTU was suggested, then we guess a new one 399 * less than the current value. If the new MTU is 400 * unreasonably small (arbitrarily set at 296), then 401 * we reset the MTU to the interface value and enable the 402 * lock bit, indicating that we are no longer doing MTU 403 * discovery. 404 */ 405 if (code == PRC_MSGSIZE) { 406 struct rtentry *rt; 407 int mtu; 408 409 rt = rtalloc1((struct sockaddr *)&icmpsrc, 0, 410 RTF_CLONING | RTF_PRCLONING); 411 if (rt && (rt->rt_flags & RTF_HOST) 412 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 413 mtu = ntohs(icp->icmp_nextmtu); 414 if (!mtu) 415 mtu = ip_next_mtu(rt->rt_rmx.rmx_mtu, 416 1); 417 #ifdef DEBUG_MTUDISC 418 printf("MTU for %s reduced to %d\n", 419 inet_ntoa(icmpsrc.sin_addr), mtu); 420 #endif 421 if (mtu < 296) { 422 /* rt->rt_rmx.rmx_mtu = 423 rt->rt_ifp->if_mtu; */ 424 rt->rt_rmx.rmx_locks |= RTV_MTU; 425 } else if (rt->rt_rmx.rmx_mtu > mtu) { 426 rt->rt_rmx.rmx_mtu = mtu; 427 } 428 } 429 if (rt) 430 RTFREE(rt); 431 } 432 433 #endif 434 /* 435 * XXX if the packet contains [IPv4 AH TCP], we can't make a 436 * notification to TCP layer. 437 */ 438 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; 439 if (ctlfunc) 440 (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, 441 (void *)&icp->icmp_ip); 442 break; 443 444 badcode: 445 icmpstat.icps_badcode++; 446 break; 447 448 case ICMP_ECHO: 449 if (!icmpbmcastecho 450 && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 451 icmpstat.icps_bmcastecho++; 452 break; 453 } 454 icp->icmp_type = ICMP_ECHOREPLY; 455 if (badport_bandlim(BANDLIM_ICMP_ECHO) < 0) 456 goto freeit; 457 else 458 goto reflect; 459 460 case ICMP_TSTAMP: 461 if (!icmpbmcastecho 462 && (m->m_flags & (M_MCAST | M_BCAST)) != 0) { 463 icmpstat.icps_bmcasttstamp++; 464 break; 465 } 466 if (icmplen < ICMP_TSLEN) { 467 icmpstat.icps_badlen++; 468 break; 469 } 470 icp->icmp_type = ICMP_TSTAMPREPLY; 471 icp->icmp_rtime = iptime(); 472 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 473 if (badport_bandlim(BANDLIM_ICMP_TSTAMP) < 0) 474 goto freeit; 475 else 476 goto reflect; 477 478 case ICMP_MASKREQ: 479 if (icmpmaskrepl == 0) 480 break; 481 /* 482 * We are not able to respond with all ones broadcast 483 * unless we receive it over a point-to-point interface. 484 */ 485 if (icmplen < ICMP_MASKLEN) 486 break; 487 switch (ip->ip_dst.s_addr) { 488 489 case INADDR_BROADCAST: 490 case INADDR_ANY: 491 icmpdst.sin_addr = ip->ip_src; 492 break; 493 494 default: 495 icmpdst.sin_addr = ip->ip_dst; 496 } 497 ia = (struct in_ifaddr *)ifaof_ifpforaddr( 498 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); 499 if (ia == 0) 500 break; 501 if (ia->ia_ifp == 0) 502 break; 503 icp->icmp_type = ICMP_MASKREPLY; 504 if (icmpmaskfake == 0) 505 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 506 else 507 icp->icmp_mask = icmpmaskfake; 508 if (ip->ip_src.s_addr == 0) { 509 if (ia->ia_ifp->if_flags & IFF_BROADCAST) 510 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; 511 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 512 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; 513 } 514 reflect: 515 ip->ip_len += hlen; /* since ip_input deducts this */ 516 icmpstat.icps_reflect++; 517 icmpstat.icps_outhist[icp->icmp_type]++; 518 icmp_reflect(m); 519 return; 520 521 case ICMP_REDIRECT: 522 if (log_redirect) { 523 u_long src, dst, gw; 524 525 src = ntohl(ip->ip_src.s_addr); 526 dst = ntohl(icp->icmp_ip.ip_dst.s_addr); 527 gw = ntohl(icp->icmp_gwaddr.s_addr); 528 printf("icmp redirect from %d.%d.%d.%d: " 529 "%d.%d.%d.%d => %d.%d.%d.%d\n", 530 (int)(src >> 24), (int)((src >> 16) & 0xff), 531 (int)((src >> 8) & 0xff), (int)(src & 0xff), 532 (int)(dst >> 24), (int)((dst >> 16) & 0xff), 533 (int)((dst >> 8) & 0xff), (int)(dst & 0xff), 534 (int)(gw >> 24), (int)((gw >> 16) & 0xff), 535 (int)((gw >> 8) & 0xff), (int)(gw & 0xff)); 536 } 537 if (drop_redirect) 538 break; 539 if (code > 3) 540 goto badcode; 541 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 542 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) { 543 icmpstat.icps_badlen++; 544 break; 545 } 546 /* 547 * Short circuit routing redirects to force 548 * immediate change in the kernel's routing 549 * tables. The message is also handed to anyone 550 * listening on a raw socket (e.g. the routing 551 * daemon for use in updating its tables). 552 */ 553 icmpgw.sin_addr = ip->ip_src; 554 icmpdst.sin_addr = icp->icmp_gwaddr; 555 #ifdef ICMPPRINTFS 556 if (icmpprintfs) { 557 char buf[4 * sizeof "123"]; 558 strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst)); 559 560 printf("redirect dst %s to %s\n", 561 buf, inet_ntoa(icp->icmp_gwaddr)); 562 } 563 #endif 564 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 565 rtredirect((struct sockaddr *)&icmpsrc, 566 (struct sockaddr *)&icmpdst, 567 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, 568 (struct sockaddr *)&icmpgw, (struct rtentry **)0); 569 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc); 570 #ifdef IPSEC 571 key_sa_routechange((struct sockaddr *)&icmpsrc); 572 #endif 573 break; 574 575 /* 576 * No kernel processing for the following; 577 * just fall through to send to raw listener. 578 */ 579 case ICMP_ECHOREPLY: 580 case ICMP_ROUTERADVERT: 581 case ICMP_ROUTERSOLICIT: 582 case ICMP_TSTAMPREPLY: 583 case ICMP_IREQREPLY: 584 case ICMP_MASKREPLY: 585 default: 586 break; 587 } 588 589 raw: 590 rip_input(m, off); 591 return; 592 593 freeit: 594 m_freem(m); 595 } 596 597 /* 598 * Reflect the ip packet back to the source 599 */ 600 static void 601 icmp_reflect(m) 602 struct mbuf *m; 603 { 604 struct ip *ip = mtod(m, struct ip *); 605 struct ifaddr *ifa; 606 struct in_ifaddr *ia; 607 struct in_addr t; 608 struct mbuf *opts = 0; 609 int optlen = (ip->ip_hl << 2) - sizeof(struct ip); 610 struct route *ro = NULL, rt; 611 612 if (!in_canforward(ip->ip_src) && 613 ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) != 614 (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) { 615 m_freem(m); /* Bad return address */ 616 icmpstat.icps_badaddr++; 617 goto done; /* Ip_output() will check for broadcast */ 618 } 619 t = ip->ip_dst; 620 ip->ip_dst = ip->ip_src; 621 ro = &rt; 622 bzero(ro, sizeof(*ro)); 623 /* 624 * If the incoming packet was addressed directly to us, 625 * use dst as the src for the reply. Otherwise (broadcast 626 * or anonymous), use the address which corresponds 627 * to the incoming interface. 628 */ 629 LIST_FOREACH(ia, INADDR_HASH(t.s_addr), ia_hash) 630 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) 631 goto match; 632 if (m->m_pkthdr.rcvif != NULL && 633 m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { 634 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 635 if (ifa->ifa_addr->sa_family != AF_INET) 636 continue; 637 ia = ifatoia(ifa); 638 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 639 t.s_addr) 640 goto match; 641 } 642 } 643 ia = ip_rtaddr(ip->ip_dst, ro); 644 /* We need a route to do anything useful. */ 645 if (ia == NULL) { 646 m_freem(m); 647 icmpstat.icps_noroute++; 648 goto done; 649 } 650 match: 651 t = IA_SIN(ia)->sin_addr; 652 ip->ip_src = t; 653 ip->ip_ttl = ip_defttl; 654 655 if (optlen > 0) { 656 register u_char *cp; 657 int opt, cnt; 658 u_int len; 659 660 /* 661 * Retrieve any source routing from the incoming packet; 662 * add on any record-route or timestamp options. 663 */ 664 cp = (u_char *) (ip + 1); 665 if ((opts = ip_srcroute()) == 0 && 666 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { 667 opts->m_len = sizeof(struct in_addr); 668 mtod(opts, struct in_addr *)->s_addr = 0; 669 } 670 if (opts) { 671 #ifdef ICMPPRINTFS 672 if (icmpprintfs) 673 printf("icmp_reflect optlen %d rt %d => ", 674 optlen, opts->m_len); 675 #endif 676 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 677 opt = cp[IPOPT_OPTVAL]; 678 if (opt == IPOPT_EOL) 679 break; 680 if (opt == IPOPT_NOP) 681 len = 1; 682 else { 683 if (cnt < IPOPT_OLEN + sizeof(*cp)) 684 break; 685 len = cp[IPOPT_OLEN]; 686 if (len < IPOPT_OLEN + sizeof(*cp) || 687 len > cnt) 688 break; 689 } 690 /* 691 * Should check for overflow, but it "can't happen" 692 */ 693 if (opt == IPOPT_RR || opt == IPOPT_TS || 694 opt == IPOPT_SECURITY) { 695 bcopy((caddr_t)cp, 696 mtod(opts, caddr_t) + opts->m_len, len); 697 opts->m_len += len; 698 } 699 } 700 /* Terminate & pad, if necessary */ 701 cnt = opts->m_len % 4; 702 if (cnt) { 703 for (; cnt < 4; cnt++) { 704 *(mtod(opts, caddr_t) + opts->m_len) = 705 IPOPT_EOL; 706 opts->m_len++; 707 } 708 } 709 #ifdef ICMPPRINTFS 710 if (icmpprintfs) 711 printf("%d\n", opts->m_len); 712 #endif 713 } 714 /* 715 * Now strip out original options by copying rest of first 716 * mbuf's data back, and adjust the IP length. 717 */ 718 ip->ip_len -= optlen; 719 ip->ip_v = IPVERSION; 720 ip->ip_hl = 5; 721 m->m_len -= optlen; 722 if (m->m_flags & M_PKTHDR) 723 m->m_pkthdr.len -= optlen; 724 optlen += sizeof(struct ip); 725 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), 726 (unsigned)(m->m_len - sizeof(struct ip))); 727 } 728 m->m_flags &= ~(M_BCAST|M_MCAST); 729 icmp_send(m, opts, ro); 730 done: 731 if (opts) 732 (void)m_free(opts); 733 if (ro && ro->ro_rt) 734 RTFREE(ro->ro_rt); 735 } 736 737 /* 738 * Send an icmp packet back to the ip level, 739 * after supplying a checksum. 740 */ 741 static void 742 icmp_send(m, opts, rt) 743 register struct mbuf *m; 744 struct mbuf *opts; 745 struct route *rt; 746 { 747 register struct ip *ip = mtod(m, struct ip *); 748 register int hlen; 749 register struct icmp *icp; 750 751 hlen = ip->ip_hl << 2; 752 m->m_data += hlen; 753 m->m_len -= hlen; 754 icp = mtod(m, struct icmp *); 755 icp->icmp_cksum = 0; 756 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); 757 m->m_data -= hlen; 758 m->m_len += hlen; 759 m->m_pkthdr.rcvif = (struct ifnet *)0; 760 #ifdef ICMPPRINTFS 761 if (icmpprintfs) { 762 char buf[4 * sizeof "123"]; 763 strcpy(buf, inet_ntoa(ip->ip_dst)); 764 printf("icmp_send dst %s src %s\n", 765 buf, inet_ntoa(ip->ip_src)); 766 } 767 #endif 768 (void) ip_output(m, opts, rt, 0, NULL, NULL); 769 } 770 771 n_time 772 iptime() 773 { 774 struct timeval atv; 775 u_long t; 776 777 getmicrotime(&atv); 778 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 779 return (htonl(t)); 780 } 781 782 #if 1 783 /* 784 * Return the next larger or smaller MTU plateau (table from RFC 1191) 785 * given current value MTU. If DIR is less than zero, a larger plateau 786 * is returned; otherwise, a smaller value is returned. 787 */ 788 static int 789 ip_next_mtu(mtu, dir) 790 int mtu; 791 int dir; 792 { 793 static int mtutab[] = { 794 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296, 795 68, 0 796 }; 797 int i; 798 799 for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) { 800 if (mtu >= mtutab[i]) 801 break; 802 } 803 804 if (dir < 0) { 805 if (i == 0) { 806 return 0; 807 } else { 808 return mtutab[i - 1]; 809 } 810 } else { 811 if (mtutab[i] == 0) { 812 return 0; 813 } else if(mtu > mtutab[i]) { 814 return mtutab[i]; 815 } else { 816 return mtutab[i + 1]; 817 } 818 } 819 } 820 #endif 821 822 823 /* 824 * badport_bandlim() - check for ICMP bandwidth limit 825 * 826 * Return 0 if it is ok to send an ICMP error response, -1 if we have 827 * hit our bandwidth limit and it is not ok. 828 * 829 * If icmplim is <= 0, the feature is disabled and 0 is returned. 830 * 831 * For now we separate the TCP and UDP subsystems w/ different 'which' 832 * values. We may eventually remove this separation (and simplify the 833 * code further). 834 * 835 * Note that the printing of the error message is delayed so we can 836 * properly print the icmp error rate that the system was trying to do 837 * (i.e. 22000/100 pps, etc...). This can cause long delays in printing 838 * the 'final' error, but it doesn't make sense to solve the printing 839 * delay with more complex code. 840 */ 841 842 int 843 badport_bandlim(int which) 844 { 845 #define N(a) (sizeof (a) / sizeof (a[0])) 846 static struct rate { 847 const char *type; 848 struct timeval lasttime; 849 int curpps;; 850 } rates[BANDLIM_MAX+1] = { 851 { "icmp unreach response" }, 852 { "icmp ping response" }, 853 { "icmp tstamp response" }, 854 { "closed port RST response" }, 855 { "open port RST response" } 856 }; 857 858 /* 859 * Return ok status if feature disabled or argument out of range. 860 */ 861 if (icmplim > 0 && (u_int) which < N(rates)) { 862 struct rate *r = &rates[which]; 863 int opps = r->curpps; 864 865 if (!ppsratecheck(&r->lasttime, &r->curpps, icmplim)) 866 return -1; /* discard packet */ 867 /* 868 * If we've dropped below the threshold after having 869 * rate-limited traffic print the message. This preserves 870 * the previous behaviour at the expense of added complexity. 871 */ 872 if (icmplim_output && opps > icmplim) 873 printf("Limiting %s from %d to %d packets/sec\n", 874 r->type, opps, icmplim); 875 } 876 return 0; /* okay to send packet */ 877 #undef N 878 } 879