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