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 * $Id: ip_icmp.c,v 1.28 1997/08/25 01:25:31 wollman Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/mbuf.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/time.h> 43 #include <sys/kernel.h> 44 #include <sys/sysctl.h> 45 46 #include <net/if.h> 47 #include <net/route.h> 48 49 #define _IP_VHL 50 #include <netinet/in.h> 51 #include <netinet/in_systm.h> 52 #include <netinet/in_var.h> 53 #include <netinet/ip.h> 54 #include <netinet/ip_icmp.h> 55 #include <netinet/ip_var.h> 56 #include <netinet/icmp_var.h> 57 58 /* 59 * ICMP routines: error generation, receive packet processing, and 60 * routines to turnaround packets back to the originator, and 61 * host table maintenance routines. 62 */ 63 64 static struct icmpstat icmpstat; 65 SYSCTL_STRUCT(_net_inet_icmp, ICMPCTL_STATS, stats, CTLFLAG_RD, 66 &icmpstat, icmpstat, ""); 67 68 static int icmpmaskrepl = 0; 69 SYSCTL_INT(_net_inet_icmp, ICMPCTL_MASKREPL, maskrepl, CTLFLAG_RW, 70 &icmpmaskrepl, 0, ""); 71 72 static int icmpbmcastecho = 1; 73 SYSCTL_INT(_net_inet_icmp, OID_AUTO, bmcastecho, CTLFLAG_RW, &icmpbmcastecho, 74 0, ""); 75 76 #ifdef ICMPPRINTFS 77 int icmpprintfs = 0; 78 #endif 79 80 static void icmp_reflect __P((struct mbuf *)); 81 static void icmp_send __P((struct mbuf *, struct mbuf *)); 82 static int ip_next_mtu __P((int, int)); 83 84 extern struct protosw inetsw[]; 85 86 /* 87 * Generate an error packet of type error 88 * in response to bad packet ip. 89 */ 90 void 91 icmp_error(n, type, code, dest, destifp) 92 struct mbuf *n; 93 int type, code; 94 n_long dest; 95 struct ifnet *destifp; 96 { 97 register struct ip *oip = mtod(n, struct ip *), *nip; 98 register unsigned oiplen = IP_VHL_HL(oip->ip_vhl) << 2; 99 register struct icmp *icp; 100 register struct mbuf *m; 101 unsigned icmplen; 102 103 #ifdef ICMPPRINTFS 104 if (icmpprintfs) 105 printf("icmp_error(%p, %x, %d)\n", oip, type, code); 106 #endif 107 if (type != ICMP_REDIRECT) 108 icmpstat.icps_error++; 109 /* 110 * Don't send error if not the first fragment of message. 111 * Don't error if the old packet protocol was ICMP 112 * error message, only known informational types. 113 */ 114 if (oip->ip_off &~ (IP_MF|IP_DF)) 115 goto freeit; 116 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT && 117 n->m_len >= oiplen + ICMP_MINLEN && 118 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) { 119 icmpstat.icps_oldicmp++; 120 goto freeit; 121 } 122 /* Don't send error in response to a multicast or broadcast packet */ 123 if (n->m_flags & (M_BCAST|M_MCAST)) 124 goto freeit; 125 /* 126 * First, formulate icmp message 127 */ 128 m = m_gethdr(M_DONTWAIT, MT_HEADER); 129 if (m == NULL) 130 goto freeit; 131 icmplen = oiplen + min(8, oip->ip_len); 132 m->m_len = icmplen + ICMP_MINLEN; 133 MH_ALIGN(m, m->m_len); 134 icp = mtod(m, struct icmp *); 135 if ((u_int)type > ICMP_MAXTYPE) 136 panic("icmp_error"); 137 icmpstat.icps_outhist[type]++; 138 icp->icmp_type = type; 139 if (type == ICMP_REDIRECT) 140 icp->icmp_gwaddr.s_addr = dest; 141 else { 142 icp->icmp_void = 0; 143 /* 144 * The following assignments assume an overlay with the 145 * zeroed icmp_void field. 146 */ 147 if (type == ICMP_PARAMPROB) { 148 icp->icmp_pptr = code; 149 code = 0; 150 } else if (type == ICMP_UNREACH && 151 code == ICMP_UNREACH_NEEDFRAG && destifp) { 152 icp->icmp_nextmtu = htons(destifp->if_mtu); 153 } 154 } 155 156 icp->icmp_code = code; 157 bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen); 158 nip = &icp->icmp_ip; 159 nip->ip_len = htons((u_short)(nip->ip_len + oiplen)); 160 161 /* 162 * Now, copy old ip header (without options) 163 * in front of icmp message. 164 */ 165 if (m->m_data - sizeof(struct ip) < m->m_pktdat) 166 panic("icmp len"); 167 m->m_data -= sizeof(struct ip); 168 m->m_len += sizeof(struct ip); 169 m->m_pkthdr.len = m->m_len; 170 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif; 171 nip = mtod(m, struct ip *); 172 bcopy((caddr_t)oip, (caddr_t)nip, sizeof(struct ip)); 173 nip->ip_len = m->m_len; 174 nip->ip_vhl = IP_VHL_BORING; 175 nip->ip_p = IPPROTO_ICMP; 176 nip->ip_tos = 0; 177 icmp_reflect(m); 178 179 freeit: 180 m_freem(n); 181 } 182 183 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET }; 184 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET }; 185 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET }; 186 187 /* 188 * Process a received ICMP message. 189 */ 190 void 191 icmp_input(m, hlen) 192 register struct mbuf *m; 193 int hlen; 194 { 195 register struct icmp *icp; 196 register struct ip *ip = mtod(m, struct ip *); 197 int icmplen = ip->ip_len; 198 register int i; 199 struct in_ifaddr *ia; 200 void (*ctlfunc) __P((int, struct sockaddr *, void *)); 201 int code; 202 203 /* 204 * Locate icmp structure in mbuf, and check 205 * that not corrupted and of at least minimum length. 206 */ 207 #ifdef ICMPPRINTFS 208 if (icmpprintfs) { 209 char buf[4 * sizeof "123"]; 210 strcpy(buf, inet_ntoa(ip->ip_src)); 211 printf("icmp_input from %s to %s, len %d\n", 212 buf, inet_ntoa(ip->ip_dst), icmplen); 213 } 214 #endif 215 if (icmplen < ICMP_MINLEN) { 216 icmpstat.icps_tooshort++; 217 goto freeit; 218 } 219 i = hlen + min(icmplen, ICMP_ADVLENMIN); 220 if (m->m_len < i && (m = m_pullup(m, i)) == 0) { 221 icmpstat.icps_tooshort++; 222 return; 223 } 224 ip = mtod(m, struct ip *); 225 m->m_len -= hlen; 226 m->m_data += hlen; 227 icp = mtod(m, struct icmp *); 228 if (in_cksum(m, icmplen)) { 229 icmpstat.icps_checksum++; 230 goto freeit; 231 } 232 m->m_len += hlen; 233 m->m_data -= hlen; 234 235 #ifdef ICMPPRINTFS 236 if (icmpprintfs) 237 printf("icmp_input, type %d code %d\n", icp->icmp_type, 238 icp->icmp_code); 239 #endif 240 241 /* 242 * Message type specific processing. 243 */ 244 if (icp->icmp_type > ICMP_MAXTYPE) 245 goto raw; 246 icmpstat.icps_inhist[icp->icmp_type]++; 247 code = icp->icmp_code; 248 switch (icp->icmp_type) { 249 250 case ICMP_UNREACH: 251 switch (code) { 252 case ICMP_UNREACH_NET: 253 case ICMP_UNREACH_HOST: 254 case ICMP_UNREACH_PROTOCOL: 255 case ICMP_UNREACH_PORT: 256 case ICMP_UNREACH_SRCFAIL: 257 code += PRC_UNREACH_NET; 258 break; 259 260 case ICMP_UNREACH_NEEDFRAG: 261 code = PRC_MSGSIZE; 262 break; 263 264 case ICMP_UNREACH_NET_UNKNOWN: 265 case ICMP_UNREACH_NET_PROHIB: 266 case ICMP_UNREACH_TOSNET: 267 code = PRC_UNREACH_NET; 268 break; 269 270 case ICMP_UNREACH_HOST_UNKNOWN: 271 case ICMP_UNREACH_ISOLATED: 272 case ICMP_UNREACH_HOST_PROHIB: 273 case ICMP_UNREACH_TOSHOST: 274 code = PRC_UNREACH_HOST; 275 break; 276 277 case ICMP_UNREACH_FILTER_PROHIB: 278 case ICMP_UNREACH_HOST_PRECEDENCE: 279 case ICMP_UNREACH_PRECEDENCE_CUTOFF: 280 code = PRC_UNREACH_PORT; 281 break; 282 283 default: 284 goto badcode; 285 } 286 goto deliver; 287 288 case ICMP_TIMXCEED: 289 if (code > 1) 290 goto badcode; 291 code += PRC_TIMXCEED_INTRANS; 292 goto deliver; 293 294 case ICMP_PARAMPROB: 295 if (code > 1) 296 goto badcode; 297 code = PRC_PARAMPROB; 298 goto deliver; 299 300 case ICMP_SOURCEQUENCH: 301 if (code) 302 goto badcode; 303 code = PRC_QUENCH; 304 deliver: 305 /* 306 * Problem with datagram; advise higher level routines. 307 */ 308 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 309 IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) { 310 icmpstat.icps_badlen++; 311 goto freeit; 312 } 313 NTOHS(icp->icmp_ip.ip_len); 314 /* Discard ICMP's in response to multicast packets */ 315 if (IN_MULTICAST(ntohl(icp->icmp_ip.ip_dst.s_addr))) 316 goto badcode; 317 #ifdef ICMPPRINTFS 318 if (icmpprintfs) 319 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p); 320 #endif 321 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 322 #if 1 323 /* 324 * MTU discovery: 325 * If we got a needfrag and there is a host route to the 326 * original destination, and the MTU is not locked, then 327 * set the MTU in the route to the suggested new value 328 * (if given) and then notify as usual. The ULPs will 329 * notice that the MTU has changed and adapt accordingly. 330 * If no new MTU was suggested, then we guess a new one 331 * less than the current value. If the new MTU is 332 * unreasonably small (arbitrarily set at 296), then 333 * we reset the MTU to the interface value and enable the 334 * lock bit, indicating that we are no longer doing MTU 335 * discovery. 336 */ 337 if (code == PRC_MSGSIZE) { 338 struct rtentry *rt; 339 int mtu; 340 341 rt = rtalloc1((struct sockaddr *)&icmpsrc, 0, 342 RTF_CLONING | RTF_PRCLONING); 343 if (rt && (rt->rt_flags & RTF_HOST) 344 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { 345 mtu = ntohs(icp->icmp_nextmtu); 346 if (!mtu) 347 mtu = ip_next_mtu(rt->rt_rmx.rmx_mtu, 348 1); 349 #ifdef DEBUG_MTUDISC 350 printf("MTU for %s reduced to %d\n", 351 inet_ntoa(icmpsrc.sin_addr), mtu); 352 #endif 353 if (mtu < 296) { 354 /* rt->rt_rmx.rmx_mtu = 355 rt->rt_ifp->if_mtu; */ 356 rt->rt_rmx.rmx_locks |= RTV_MTU; 357 } else if (rt->rt_rmx.rmx_mtu > mtu) { 358 rt->rt_rmx.rmx_mtu = mtu; 359 } 360 } 361 if (rt) 362 RTFREE(rt); 363 } 364 365 #endif 366 ctlfunc = inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput; 367 if (ctlfunc) 368 (*ctlfunc)(code, (struct sockaddr *)&icmpsrc, 369 (void *)&icp->icmp_ip); 370 break; 371 372 badcode: 373 icmpstat.icps_badcode++; 374 break; 375 376 case ICMP_ECHO: 377 if (!icmpbmcastecho 378 && (m->m_flags & (M_MCAST | M_BCAST)) != 0 379 && IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 380 icmpstat.icps_bmcastecho++; 381 break; 382 } 383 icp->icmp_type = ICMP_ECHOREPLY; 384 goto reflect; 385 386 case ICMP_TSTAMP: 387 if (!icmpbmcastecho 388 && (m->m_flags & (M_MCAST | M_BCAST)) != 0 389 && IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 390 icmpstat.icps_bmcasttstamp++; 391 break; 392 } 393 if (icmplen < ICMP_TSLEN) { 394 icmpstat.icps_badlen++; 395 break; 396 } 397 icp->icmp_type = ICMP_TSTAMPREPLY; 398 icp->icmp_rtime = iptime(); 399 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */ 400 goto reflect; 401 402 case ICMP_MASKREQ: 403 #define satosin(sa) ((struct sockaddr_in *)(sa)) 404 if (icmpmaskrepl == 0) 405 break; 406 /* 407 * We are not able to respond with all ones broadcast 408 * unless we receive it over a point-to-point interface. 409 */ 410 if (icmplen < ICMP_MASKLEN) 411 break; 412 switch (ip->ip_dst.s_addr) { 413 414 case INADDR_BROADCAST: 415 case INADDR_ANY: 416 icmpdst.sin_addr = ip->ip_src; 417 break; 418 419 default: 420 icmpdst.sin_addr = ip->ip_dst; 421 } 422 ia = (struct in_ifaddr *)ifaof_ifpforaddr( 423 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); 424 if (ia == 0) 425 break; 426 if (ia->ia_ifp == 0) 427 break; 428 icp->icmp_type = ICMP_MASKREPLY; 429 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr; 430 if (ip->ip_src.s_addr == 0) { 431 if (ia->ia_ifp->if_flags & IFF_BROADCAST) 432 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr; 433 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) 434 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr; 435 } 436 reflect: 437 ip->ip_len += hlen; /* since ip_input deducts this */ 438 icmpstat.icps_reflect++; 439 icmpstat.icps_outhist[icp->icmp_type]++; 440 icmp_reflect(m); 441 return; 442 443 case ICMP_REDIRECT: 444 if (code > 3) 445 goto badcode; 446 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) || 447 IP_VHL_HL(icp->icmp_ip.ip_vhl) < (sizeof(struct ip) >> 2)) { 448 icmpstat.icps_badlen++; 449 break; 450 } 451 /* 452 * Short circuit routing redirects to force 453 * immediate change in the kernel's routing 454 * tables. The message is also handed to anyone 455 * listening on a raw socket (e.g. the routing 456 * daemon for use in updating its tables). 457 */ 458 icmpgw.sin_addr = ip->ip_src; 459 icmpdst.sin_addr = icp->icmp_gwaddr; 460 #ifdef ICMPPRINTFS 461 if (icmpprintfs) { 462 char buf[4 * sizeof "123"]; 463 strcpy(buf, inet_ntoa(icp->icmp_ip.ip_dst)); 464 465 printf("redirect dst %s to %s\n", 466 buf, inet_ntoa(icp->icmp_gwaddr)); 467 } 468 #endif 469 icmpsrc.sin_addr = icp->icmp_ip.ip_dst; 470 rtredirect((struct sockaddr *)&icmpsrc, 471 (struct sockaddr *)&icmpdst, 472 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST, 473 (struct sockaddr *)&icmpgw, (struct rtentry **)0); 474 pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&icmpsrc); 475 break; 476 477 /* 478 * No kernel processing for the following; 479 * just fall through to send to raw listener. 480 */ 481 case ICMP_ECHOREPLY: 482 case ICMP_ROUTERADVERT: 483 case ICMP_ROUTERSOLICIT: 484 case ICMP_TSTAMPREPLY: 485 case ICMP_IREQREPLY: 486 case ICMP_MASKREPLY: 487 default: 488 break; 489 } 490 491 raw: 492 rip_input(m, hlen); 493 return; 494 495 freeit: 496 m_freem(m); 497 } 498 499 /* 500 * Reflect the ip packet back to the source 501 */ 502 static void 503 icmp_reflect(m) 504 struct mbuf *m; 505 { 506 register struct ip *ip = mtod(m, struct ip *); 507 register struct in_ifaddr *ia; 508 struct in_addr t; 509 struct mbuf *opts = 0; 510 int optlen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip); 511 512 if (!in_canforward(ip->ip_src) && 513 ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) != 514 (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))) { 515 m_freem(m); /* Bad return address */ 516 goto done; /* Ip_output() will check for broadcast */ 517 } 518 t = ip->ip_dst; 519 ip->ip_dst = ip->ip_src; 520 /* 521 * If the incoming packet was addressed directly to us, 522 * use dst as the src for the reply. Otherwise (broadcast 523 * or anonymous), use the address which corresponds 524 * to the incoming interface. 525 */ 526 for (ia = in_ifaddrhead.tqh_first; ia; ia = ia->ia_link.tqe_next) { 527 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr) 528 break; 529 if (ia->ia_ifp && (ia->ia_ifp->if_flags & IFF_BROADCAST) && 530 t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr) 531 break; 532 } 533 icmpdst.sin_addr = t; 534 if ((ia == (struct in_ifaddr *)0) && m->m_pkthdr.rcvif) 535 ia = (struct in_ifaddr *)ifaof_ifpforaddr( 536 (struct sockaddr *)&icmpdst, m->m_pkthdr.rcvif); 537 /* 538 * The following happens if the packet was not addressed to us, 539 * and was received on an interface with no IP address. 540 */ 541 if (ia == (struct in_ifaddr *)0) 542 ia = in_ifaddrhead.tqh_first; 543 t = IA_SIN(ia)->sin_addr; 544 ip->ip_src = t; 545 ip->ip_ttl = MAXTTL; 546 547 if (optlen > 0) { 548 register u_char *cp; 549 int opt, cnt; 550 u_int len; 551 552 /* 553 * Retrieve any source routing from the incoming packet; 554 * add on any record-route or timestamp options. 555 */ 556 cp = (u_char *) (ip + 1); 557 if ((opts = ip_srcroute()) == 0 && 558 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) { 559 opts->m_len = sizeof(struct in_addr); 560 mtod(opts, struct in_addr *)->s_addr = 0; 561 } 562 if (opts) { 563 #ifdef ICMPPRINTFS 564 if (icmpprintfs) 565 printf("icmp_reflect optlen %d rt %d => ", 566 optlen, opts->m_len); 567 #endif 568 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) { 569 opt = cp[IPOPT_OPTVAL]; 570 if (opt == IPOPT_EOL) 571 break; 572 if (opt == IPOPT_NOP) 573 len = 1; 574 else { 575 len = cp[IPOPT_OLEN]; 576 if (len <= 0 || len > cnt) 577 break; 578 } 579 /* 580 * Should check for overflow, but it "can't happen" 581 */ 582 if (opt == IPOPT_RR || opt == IPOPT_TS || 583 opt == IPOPT_SECURITY) { 584 bcopy((caddr_t)cp, 585 mtod(opts, caddr_t) + opts->m_len, len); 586 opts->m_len += len; 587 } 588 } 589 /* Terminate & pad, if necessary */ 590 cnt = opts->m_len % 4; 591 if (cnt) { 592 for (; cnt < 4; cnt++) { 593 *(mtod(opts, caddr_t) + opts->m_len) = 594 IPOPT_EOL; 595 opts->m_len++; 596 } 597 } 598 #ifdef ICMPPRINTFS 599 if (icmpprintfs) 600 printf("%d\n", opts->m_len); 601 #endif 602 } 603 /* 604 * Now strip out original options by copying rest of first 605 * mbuf's data back, and adjust the IP length. 606 */ 607 ip->ip_len -= optlen; 608 ip->ip_vhl = IP_VHL_BORING; 609 m->m_len -= optlen; 610 if (m->m_flags & M_PKTHDR) 611 m->m_pkthdr.len -= optlen; 612 optlen += sizeof(struct ip); 613 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1), 614 (unsigned)(m->m_len - sizeof(struct ip))); 615 } 616 m->m_flags &= ~(M_BCAST|M_MCAST); 617 icmp_send(m, opts); 618 done: 619 if (opts) 620 (void)m_free(opts); 621 } 622 623 /* 624 * Send an icmp packet back to the ip level, 625 * after supplying a checksum. 626 */ 627 static void 628 icmp_send(m, opts) 629 register struct mbuf *m; 630 struct mbuf *opts; 631 { 632 register struct ip *ip = mtod(m, struct ip *); 633 register int hlen; 634 register struct icmp *icp; 635 struct route ro; 636 637 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 638 m->m_data += hlen; 639 m->m_len -= hlen; 640 icp = mtod(m, struct icmp *); 641 icp->icmp_cksum = 0; 642 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen); 643 m->m_data -= hlen; 644 m->m_len += hlen; 645 #ifdef ICMPPRINTFS 646 if (icmpprintfs) { 647 char buf[4 * sizeof "123"]; 648 strcpy(buf, inet_ntoa(ip->ip_dst)); 649 printf("icmp_send dst %s src %s\n", 650 buf, inet_ntoa(ip->ip_src)); 651 } 652 #endif 653 bzero(&ro, sizeof ro); 654 (void) ip_output(m, opts, &ro, 0, NULL); 655 if (ro.ro_rt) 656 RTFREE(ro.ro_rt); 657 } 658 659 n_time 660 iptime() 661 { 662 struct timeval atv; 663 u_long t; 664 665 microtime(&atv); 666 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000; 667 return (htonl(t)); 668 } 669 670 #if 1 671 /* 672 * Return the next larger or smaller MTU plateau (table from RFC 1191) 673 * given current value MTU. If DIR is less than zero, a larger plateau 674 * is returned; otherwise, a smaller value is returned. 675 */ 676 static int 677 ip_next_mtu(mtu, dir) 678 int mtu; 679 int dir; 680 { 681 static int mtutab[] = { 682 65535, 32000, 17914, 8166, 4352, 2002, 1492, 1006, 508, 296, 683 68, 0 684 }; 685 int i; 686 687 for (i = 0; i < (sizeof mtutab) / (sizeof mtutab[0]); i++) { 688 if (mtu >= mtutab[i]) 689 break; 690 } 691 692 if (dir < 0) { 693 if (i == 0) { 694 return 0; 695 } else { 696 return mtutab[i - 1]; 697 } 698 } else { 699 if (mtutab[i] == 0) { 700 return 0; 701 } else if(mtu > mtutab[i]) { 702 return mtutab[i]; 703 } else { 704 return mtutab[i + 1]; 705 } 706 } 707 } 708 #endif 709