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