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