1 /* 2 * Copyright (c) 1982, 1986, 1988, 1993 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ip_input.c 7.25 (Berkeley) 02/12/93 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/malloc.h> 13 #include <sys/mbuf.h> 14 #include <sys/domain.h> 15 #include <sys/protosw.h> 16 #include <sys/socket.h> 17 #include <sys/errno.h> 18 #include <sys/time.h> 19 #include <sys/kernel.h> 20 21 #include <net/if.h> 22 #include <net/route.h> 23 24 #include <netinet/in.h> 25 #include <netinet/in_systm.h> 26 #include <netinet/ip.h> 27 #include <netinet/in_pcb.h> 28 #include <netinet/in_var.h> 29 #include <netinet/ip_var.h> 30 #include <netinet/ip_icmp.h> 31 32 #ifndef IPFORWARDING 33 #ifdef GATEWAY 34 #define IPFORWARDING 1 /* forward IP packets not for us */ 35 #else /* GATEWAY */ 36 #define IPFORWARDING 0 /* don't forward IP packets not for us */ 37 #endif /* GATEWAY */ 38 #endif /* IPFORWARDING */ 39 #ifndef IPSENDREDIRECTS 40 #define IPSENDREDIRECTS 1 41 #endif 42 int ipforwarding = IPFORWARDING; 43 int ipsendredirects = IPSENDREDIRECTS; 44 #ifdef DIAGNOSTIC 45 int ipprintfs = 0; 46 #endif 47 48 extern struct domain inetdomain; 49 extern struct protosw inetsw[]; 50 u_char ip_protox[IPPROTO_MAX]; 51 int ipqmaxlen = IFQ_MAXLEN; 52 struct in_ifaddr *in_ifaddr; /* first inet address */ 53 54 /* 55 * We need to save the IP options in case a protocol wants to respond 56 * to an incoming packet over the same route if the packet got here 57 * using IP source routing. This allows connection establishment and 58 * maintenance when the remote end is on a network that is not known 59 * to us. 60 */ 61 int ip_nhops = 0; 62 static struct ip_srcrt { 63 struct in_addr dst; /* final destination */ 64 char nop; /* one NOP to align */ 65 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 66 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 67 } ip_srcrt; 68 69 #ifdef GATEWAY 70 extern int if_index; 71 u_long *ip_ifmatrix; 72 #endif 73 74 /* 75 * IP initialization: fill in IP protocol switch table. 76 * All protocols not implemented in kernel go to raw IP protocol handler. 77 */ 78 ip_init() 79 { 80 register struct protosw *pr; 81 register int i; 82 83 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 84 if (pr == 0) 85 panic("ip_init"); 86 for (i = 0; i < IPPROTO_MAX; i++) 87 ip_protox[i] = pr - inetsw; 88 for (pr = inetdomain.dom_protosw; 89 pr < inetdomain.dom_protoswNPROTOSW; pr++) 90 if (pr->pr_domain->dom_family == PF_INET && 91 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 92 ip_protox[pr->pr_protocol] = pr - inetsw; 93 ipq.next = ipq.prev = &ipq; 94 ip_id = time.tv_sec & 0xffff; 95 ipintrq.ifq_maxlen = ipqmaxlen; 96 #ifdef GATEWAY 97 i = (if_index + 1) * (if_index + 1) * sizeof (u_long); 98 ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK); 99 bzero((char *)ip_ifmatrix, i); 100 #endif 101 } 102 103 struct ip *ip_reass(); 104 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 105 struct route ipforward_rt; 106 107 /* 108 * Ip input routine. Checksum and byte swap header. If fragmented 109 * try to reassemble. Process options. Pass to next level. 110 */ 111 ipintr() 112 { 113 register struct ip *ip; 114 register struct mbuf *m; 115 register struct ipq *fp; 116 register struct in_ifaddr *ia; 117 int hlen, s; 118 119 next: 120 /* 121 * Get next datagram off input queue and get IP header 122 * in first mbuf. 123 */ 124 s = splimp(); 125 IF_DEQUEUE(&ipintrq, m); 126 splx(s); 127 if (m == 0) 128 return; 129 #ifdef DIAGNOSTIC 130 if ((m->m_flags & M_PKTHDR) == 0) 131 panic("ipintr no HDR"); 132 #endif 133 /* 134 * If no IP addresses have been set yet but the interfaces 135 * are receiving, can't do anything with incoming packets yet. 136 */ 137 if (in_ifaddr == NULL) 138 goto bad; 139 ipstat.ips_total++; 140 if (m->m_len < sizeof (struct ip) && 141 (m = m_pullup(m, sizeof (struct ip))) == 0) { 142 ipstat.ips_toosmall++; 143 goto next; 144 } 145 ip = mtod(m, struct ip *); 146 if (ip->ip_v != IPVERSION) { 147 ipstat.ips_badvers++; 148 goto bad; 149 } 150 hlen = ip->ip_hl << 2; 151 if (hlen < sizeof(struct ip)) { /* minimum header length */ 152 ipstat.ips_badhlen++; 153 goto bad; 154 } 155 if (hlen > m->m_len) { 156 if ((m = m_pullup(m, hlen)) == 0) { 157 ipstat.ips_badhlen++; 158 goto next; 159 } 160 ip = mtod(m, struct ip *); 161 } 162 if (ip->ip_sum = in_cksum(m, hlen)) { 163 ipstat.ips_badsum++; 164 goto bad; 165 } 166 167 /* 168 * Convert fields to host representation. 169 */ 170 NTOHS(ip->ip_len); 171 if (ip->ip_len < hlen) { 172 ipstat.ips_badlen++; 173 goto bad; 174 } 175 NTOHS(ip->ip_id); 176 NTOHS(ip->ip_off); 177 178 /* 179 * Check that the amount of data in the buffers 180 * is as at least much as the IP header would have us expect. 181 * Trim mbufs if longer than we expect. 182 * Drop packet if shorter than we expect. 183 */ 184 if (m->m_pkthdr.len < ip->ip_len) { 185 ipstat.ips_tooshort++; 186 goto bad; 187 } 188 if (m->m_pkthdr.len > ip->ip_len) { 189 if (m->m_len == m->m_pkthdr.len) { 190 m->m_len = ip->ip_len; 191 m->m_pkthdr.len = ip->ip_len; 192 } else 193 m_adj(m, ip->ip_len - m->m_pkthdr.len); 194 } 195 196 /* 197 * Process options and, if not destined for us, 198 * ship it on. ip_dooptions returns 1 when an 199 * error was detected (causing an icmp message 200 * to be sent and the original packet to be freed). 201 */ 202 ip_nhops = 0; /* for source routed packets */ 203 if (hlen > sizeof (struct ip) && ip_dooptions(m)) 204 goto next; 205 206 /* 207 * Check our list of addresses, to see if the packet is for us. 208 */ 209 for (ia = in_ifaddr; ia; ia = ia->ia_next) { 210 #define satosin(sa) ((struct sockaddr_in *)(sa)) 211 212 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 213 goto ours; 214 if ( 215 #ifdef DIRECTED_BROADCAST 216 ia->ia_ifp == m->m_pkthdr.rcvif && 217 #endif 218 (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 219 u_long t; 220 221 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 222 ip->ip_dst.s_addr) 223 goto ours; 224 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 225 goto ours; 226 /* 227 * Look for all-0's host part (old broadcast addr), 228 * either for subnet or net. 229 */ 230 t = ntohl(ip->ip_dst.s_addr); 231 if (t == ia->ia_subnet) 232 goto ours; 233 if (t == ia->ia_net) 234 goto ours; 235 } 236 } 237 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 238 struct in_multi *inm; 239 #ifdef MROUTING 240 extern struct socket *ip_mrouter; 241 242 if (ip_mrouter) { 243 /* 244 * If we are acting as a multicast router, all 245 * incoming multicast packets are passed to the 246 * kernel-level multicast forwarding function. 247 * The packet is returned (relatively) intact; if 248 * ip_mforward() returns a non-zero value, the packet 249 * must be discarded, else it may be accepted below. 250 * 251 * (The IP ident field is put in the same byte order 252 * as expected when ip_mforward() is called from 253 * ip_output().) 254 */ 255 ip->ip_id = htons(ip->ip_id); 256 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) { 257 ipstat.ips_cantforward++; 258 m_freem(m); 259 goto next; 260 } 261 ip->ip_id = ntohs(ip->ip_id); 262 263 /* 264 * The process-level routing demon needs to receive 265 * all multicast IGMP packets, whether or not this 266 * host belongs to their destination groups. 267 */ 268 if (ip->ip_p == IPPROTO_IGMP) 269 goto ours; 270 ipstat.ips_forward++; 271 } 272 #endif 273 /* 274 * See if we belong to the destination multicast group on the 275 * arrival interface. 276 */ 277 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 278 if (inm == NULL) { 279 ipstat.ips_cantforward++; 280 m_freem(m); 281 goto next; 282 } 283 goto ours; 284 } 285 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 286 goto ours; 287 if (ip->ip_dst.s_addr == INADDR_ANY) 288 goto ours; 289 290 /* 291 * Not for us; forward if possible and desirable. 292 */ 293 if (ipforwarding == 0) { 294 ipstat.ips_cantforward++; 295 m_freem(m); 296 } else 297 ip_forward(m, 0); 298 goto next; 299 300 ours: 301 /* 302 * If offset or IP_MF are set, must reassemble. 303 * Otherwise, nothing need be done. 304 * (We could look in the reassembly queue to see 305 * if the packet was previously fragmented, 306 * but it's not worth the time; just let them time out.) 307 */ 308 if (ip->ip_off &~ IP_DF) { 309 if (m->m_flags & M_EXT) { /* XXX */ 310 if ((m = m_pullup(m, sizeof (struct ip))) == 0) { 311 ipstat.ips_toosmall++; 312 goto next; 313 } 314 ip = mtod(m, struct ip *); 315 } 316 /* 317 * Look for queue of fragments 318 * of this datagram. 319 */ 320 for (fp = ipq.next; fp != &ipq; fp = fp->next) 321 if (ip->ip_id == fp->ipq_id && 322 ip->ip_src.s_addr == fp->ipq_src.s_addr && 323 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 324 ip->ip_p == fp->ipq_p) 325 goto found; 326 fp = 0; 327 found: 328 329 /* 330 * Adjust ip_len to not reflect header, 331 * set ip_mff if more fragments are expected, 332 * convert offset of this to bytes. 333 */ 334 ip->ip_len -= hlen; 335 ((struct ipasfrag *)ip)->ipf_mff &= ~1; 336 if (ip->ip_off & IP_MF) 337 ((struct ipasfrag *)ip)->ipf_mff |= 1; 338 ip->ip_off <<= 3; 339 340 /* 341 * If datagram marked as having more fragments 342 * or if this is not the first fragment, 343 * attempt reassembly; if it succeeds, proceed. 344 */ 345 if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) { 346 ipstat.ips_fragments++; 347 ip = ip_reass((struct ipasfrag *)ip, fp); 348 if (ip == 0) 349 goto next; 350 ipstat.ips_reassembled++; 351 m = dtom(ip); 352 } else 353 if (fp) 354 ip_freef(fp); 355 } else 356 ip->ip_len -= hlen; 357 358 /* 359 * Switch out to protocol's input routine. 360 */ 361 ipstat.ips_delivered++; 362 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 363 goto next; 364 bad: 365 m_freem(m); 366 goto next; 367 } 368 369 /* 370 * Take incoming datagram fragment and try to 371 * reassemble it into whole datagram. If a chain for 372 * reassembly of this datagram already exists, then it 373 * is given as fp; otherwise have to make a chain. 374 */ 375 struct ip * 376 ip_reass(ip, fp) 377 register struct ipasfrag *ip; 378 register struct ipq *fp; 379 { 380 register struct mbuf *m = dtom(ip); 381 register struct ipasfrag *q; 382 struct mbuf *t; 383 int hlen = ip->ip_hl << 2; 384 int i, next; 385 386 /* 387 * Presence of header sizes in mbufs 388 * would confuse code below. 389 */ 390 m->m_data += hlen; 391 m->m_len -= hlen; 392 393 /* 394 * If first fragment to arrive, create a reassembly queue. 395 */ 396 if (fp == 0) { 397 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 398 goto dropfrag; 399 fp = mtod(t, struct ipq *); 400 insque(fp, &ipq); 401 fp->ipq_ttl = IPFRAGTTL; 402 fp->ipq_p = ip->ip_p; 403 fp->ipq_id = ip->ip_id; 404 fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 405 fp->ipq_src = ((struct ip *)ip)->ip_src; 406 fp->ipq_dst = ((struct ip *)ip)->ip_dst; 407 q = (struct ipasfrag *)fp; 408 goto insert; 409 } 410 411 /* 412 * Find a segment which begins after this one does. 413 */ 414 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 415 if (q->ip_off > ip->ip_off) 416 break; 417 418 /* 419 * If there is a preceding segment, it may provide some of 420 * our data already. If so, drop the data from the incoming 421 * segment. If it provides all of our data, drop us. 422 */ 423 if (q->ipf_prev != (struct ipasfrag *)fp) { 424 i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 425 if (i > 0) { 426 if (i >= ip->ip_len) 427 goto dropfrag; 428 m_adj(dtom(ip), i); 429 ip->ip_off += i; 430 ip->ip_len -= i; 431 } 432 } 433 434 /* 435 * While we overlap succeeding segments trim them or, 436 * if they are completely covered, dequeue them. 437 */ 438 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 439 i = (ip->ip_off + ip->ip_len) - q->ip_off; 440 if (i < q->ip_len) { 441 q->ip_len -= i; 442 q->ip_off += i; 443 m_adj(dtom(q), i); 444 break; 445 } 446 q = q->ipf_next; 447 m_freem(dtom(q->ipf_prev)); 448 ip_deq(q->ipf_prev); 449 } 450 451 insert: 452 /* 453 * Stick new segment in its place; 454 * check for complete reassembly. 455 */ 456 ip_enq(ip, q->ipf_prev); 457 next = 0; 458 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 459 if (q->ip_off != next) 460 return (0); 461 next += q->ip_len; 462 } 463 if (q->ipf_prev->ipf_mff & 1) 464 return (0); 465 466 /* 467 * Reassembly is complete; concatenate fragments. 468 */ 469 q = fp->ipq_next; 470 m = dtom(q); 471 t = m->m_next; 472 m->m_next = 0; 473 m_cat(m, t); 474 q = q->ipf_next; 475 while (q != (struct ipasfrag *)fp) { 476 t = dtom(q); 477 q = q->ipf_next; 478 m_cat(m, t); 479 } 480 481 /* 482 * Create header for new ip packet by 483 * modifying header of first packet; 484 * dequeue and discard fragment reassembly header. 485 * Make header visible. 486 */ 487 ip = fp->ipq_next; 488 ip->ip_len = next; 489 ip->ipf_mff &= ~1; 490 ((struct ip *)ip)->ip_src = fp->ipq_src; 491 ((struct ip *)ip)->ip_dst = fp->ipq_dst; 492 remque(fp); 493 (void) m_free(dtom(fp)); 494 m = dtom(ip); 495 m->m_len += (ip->ip_hl << 2); 496 m->m_data -= (ip->ip_hl << 2); 497 /* some debugging cruft by sklower, below, will go away soon */ 498 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 499 register int plen = 0; 500 for (t = m; m; m = m->m_next) 501 plen += m->m_len; 502 t->m_pkthdr.len = plen; 503 } 504 return ((struct ip *)ip); 505 506 dropfrag: 507 ipstat.ips_fragdropped++; 508 m_freem(m); 509 return (0); 510 } 511 512 /* 513 * Free a fragment reassembly header and all 514 * associated datagrams. 515 */ 516 ip_freef(fp) 517 struct ipq *fp; 518 { 519 register struct ipasfrag *q, *p; 520 521 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 522 p = q->ipf_next; 523 ip_deq(q); 524 m_freem(dtom(q)); 525 } 526 remque(fp); 527 (void) m_free(dtom(fp)); 528 } 529 530 /* 531 * Put an ip fragment on a reassembly chain. 532 * Like insque, but pointers in middle of structure. 533 */ 534 ip_enq(p, prev) 535 register struct ipasfrag *p, *prev; 536 { 537 538 p->ipf_prev = prev; 539 p->ipf_next = prev->ipf_next; 540 prev->ipf_next->ipf_prev = p; 541 prev->ipf_next = p; 542 } 543 544 /* 545 * To ip_enq as remque is to insque. 546 */ 547 ip_deq(p) 548 register struct ipasfrag *p; 549 { 550 551 p->ipf_prev->ipf_next = p->ipf_next; 552 p->ipf_next->ipf_prev = p->ipf_prev; 553 } 554 555 /* 556 * IP timer processing; 557 * if a timer expires on a reassembly 558 * queue, discard it. 559 */ 560 ip_slowtimo() 561 { 562 register struct ipq *fp; 563 int s = splnet(); 564 565 fp = ipq.next; 566 if (fp == 0) { 567 splx(s); 568 return; 569 } 570 while (fp != &ipq) { 571 --fp->ipq_ttl; 572 fp = fp->next; 573 if (fp->prev->ipq_ttl == 0) { 574 ipstat.ips_fragtimeout++; 575 ip_freef(fp->prev); 576 } 577 } 578 splx(s); 579 } 580 581 /* 582 * Drain off all datagram fragments. 583 */ 584 ip_drain() 585 { 586 587 while (ipq.next != &ipq) { 588 ipstat.ips_fragdropped++; 589 ip_freef(ipq.next); 590 } 591 } 592 593 extern struct in_ifaddr *ifptoia(); 594 struct in_ifaddr *ip_rtaddr(); 595 596 /* 597 * Do option processing on a datagram, 598 * possibly discarding it if bad options are encountered, 599 * or forwarding it if source-routed. 600 * Returns 1 if packet has been forwarded/freed, 601 * 0 if the packet should be processed further. 602 */ 603 ip_dooptions(m) 604 struct mbuf *m; 605 { 606 register struct ip *ip = mtod(m, struct ip *); 607 register u_char *cp; 608 register struct ip_timestamp *ipt; 609 register struct in_ifaddr *ia; 610 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 611 struct in_addr *sin; 612 n_time ntime; 613 614 cp = (u_char *)(ip + 1); 615 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 616 for (; cnt > 0; cnt -= optlen, cp += optlen) { 617 opt = cp[IPOPT_OPTVAL]; 618 if (opt == IPOPT_EOL) 619 break; 620 if (opt == IPOPT_NOP) 621 optlen = 1; 622 else { 623 optlen = cp[IPOPT_OLEN]; 624 if (optlen <= 0 || optlen > cnt) { 625 code = &cp[IPOPT_OLEN] - (u_char *)ip; 626 goto bad; 627 } 628 } 629 switch (opt) { 630 631 default: 632 break; 633 634 /* 635 * Source routing with record. 636 * Find interface with current destination address. 637 * If none on this machine then drop if strictly routed, 638 * or do nothing if loosely routed. 639 * Record interface address and bring up next address 640 * component. If strictly routed make sure next 641 * address is on directly accessible net. 642 */ 643 case IPOPT_LSRR: 644 case IPOPT_SSRR: 645 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 646 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 647 goto bad; 648 } 649 ipaddr.sin_addr = ip->ip_dst; 650 ia = (struct in_ifaddr *) 651 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 652 if (ia == 0) { 653 if (opt == IPOPT_SSRR) { 654 type = ICMP_UNREACH; 655 code = ICMP_UNREACH_SRCFAIL; 656 goto bad; 657 } 658 /* 659 * Loose routing, and not at next destination 660 * yet; nothing to do except forward. 661 */ 662 break; 663 } 664 off--; /* 0 origin */ 665 if (off > optlen - sizeof(struct in_addr)) { 666 /* 667 * End of source route. Should be for us. 668 */ 669 save_rte(cp, ip->ip_src); 670 break; 671 } 672 /* 673 * locate outgoing interface 674 */ 675 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 676 sizeof(ipaddr.sin_addr)); 677 if (opt == IPOPT_SSRR) { 678 #define INA struct in_ifaddr * 679 #define SA struct sockaddr * 680 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 681 ia = in_iaonnetof(in_netof(ipaddr.sin_addr)); 682 } else 683 ia = ip_rtaddr(ipaddr.sin_addr); 684 if (ia == 0) { 685 type = ICMP_UNREACH; 686 code = ICMP_UNREACH_SRCFAIL; 687 goto bad; 688 } 689 ip->ip_dst = ipaddr.sin_addr; 690 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 691 (caddr_t)(cp + off), sizeof(struct in_addr)); 692 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 693 /* 694 * Let ip_intr's mcast routing check handle mcast pkts 695 */ 696 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 697 break; 698 699 case IPOPT_RR: 700 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 701 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 702 goto bad; 703 } 704 /* 705 * If no space remains, ignore. 706 */ 707 off--; /* 0 origin */ 708 if (off > optlen - sizeof(struct in_addr)) 709 break; 710 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 711 sizeof(ipaddr.sin_addr)); 712 /* 713 * locate outgoing interface; if we're the destination, 714 * use the incoming interface (should be same). 715 */ 716 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 717 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 718 type = ICMP_UNREACH; 719 code = ICMP_UNREACH_HOST; 720 goto bad; 721 } 722 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 723 (caddr_t)(cp + off), sizeof(struct in_addr)); 724 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 725 break; 726 727 case IPOPT_TS: 728 code = cp - (u_char *)ip; 729 ipt = (struct ip_timestamp *)cp; 730 if (ipt->ipt_len < 5) 731 goto bad; 732 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 733 if (++ipt->ipt_oflw == 0) 734 goto bad; 735 break; 736 } 737 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 738 switch (ipt->ipt_flg) { 739 740 case IPOPT_TS_TSONLY: 741 break; 742 743 case IPOPT_TS_TSANDADDR: 744 if (ipt->ipt_ptr + sizeof(n_time) + 745 sizeof(struct in_addr) > ipt->ipt_len) 746 goto bad; 747 ia = ifptoia(m->m_pkthdr.rcvif); 748 bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 749 (caddr_t)sin, sizeof(struct in_addr)); 750 ipt->ipt_ptr += sizeof(struct in_addr); 751 break; 752 753 case IPOPT_TS_PRESPEC: 754 if (ipt->ipt_ptr + sizeof(n_time) + 755 sizeof(struct in_addr) > ipt->ipt_len) 756 goto bad; 757 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 758 sizeof(struct in_addr)); 759 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 760 continue; 761 ipt->ipt_ptr += sizeof(struct in_addr); 762 break; 763 764 default: 765 goto bad; 766 } 767 ntime = iptime(); 768 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 769 sizeof(n_time)); 770 ipt->ipt_ptr += sizeof(n_time); 771 } 772 } 773 if (forward) { 774 ip_forward(m, 1); 775 return (1); 776 } 777 return (0); 778 bad: 779 ip->ip_len -= ip->ip_hl << 2; /* XXX icmp_error adds in hdr length */ 780 icmp_error(m, type, code); 781 ipstat.ips_badoptions++; 782 return (1); 783 } 784 785 /* 786 * Given address of next destination (final or next hop), 787 * return internet address info of interface to be used to get there. 788 */ 789 struct in_ifaddr * 790 ip_rtaddr(dst) 791 struct in_addr dst; 792 { 793 register struct sockaddr_in *sin; 794 795 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 796 797 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 798 if (ipforward_rt.ro_rt) { 799 RTFREE(ipforward_rt.ro_rt); 800 ipforward_rt.ro_rt = 0; 801 } 802 sin->sin_family = AF_INET; 803 sin->sin_len = sizeof(*sin); 804 sin->sin_addr = dst; 805 806 rtalloc(&ipforward_rt); 807 } 808 if (ipforward_rt.ro_rt == 0) 809 return ((struct in_ifaddr *)0); 810 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 811 } 812 813 /* 814 * Save incoming source route for use in replies, 815 * to be picked up later by ip_srcroute if the receiver is interested. 816 */ 817 save_rte(option, dst) 818 u_char *option; 819 struct in_addr dst; 820 { 821 unsigned olen; 822 823 olen = option[IPOPT_OLEN]; 824 #ifdef DIAGNOSTIC 825 if (ipprintfs) 826 printf("save_rte: olen %d\n", olen); 827 #endif 828 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 829 return; 830 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 831 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 832 ip_srcrt.dst = dst; 833 } 834 835 /* 836 * Retrieve incoming source route for use in replies, 837 * in the same form used by setsockopt. 838 * The first hop is placed before the options, will be removed later. 839 */ 840 struct mbuf * 841 ip_srcroute() 842 { 843 register struct in_addr *p, *q; 844 register struct mbuf *m; 845 846 if (ip_nhops == 0) 847 return ((struct mbuf *)0); 848 m = m_get(M_DONTWAIT, MT_SOOPTS); 849 if (m == 0) 850 return ((struct mbuf *)0); 851 852 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 853 854 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 855 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 856 OPTSIZ; 857 #ifdef DIAGNOSTIC 858 if (ipprintfs) 859 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 860 #endif 861 862 /* 863 * First save first hop for return route 864 */ 865 p = &ip_srcrt.route[ip_nhops - 1]; 866 *(mtod(m, struct in_addr *)) = *p--; 867 #ifdef DIAGNOSTIC 868 if (ipprintfs) 869 printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr)); 870 #endif 871 872 /* 873 * Copy option fields and padding (nop) to mbuf. 874 */ 875 ip_srcrt.nop = IPOPT_NOP; 876 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 877 bcopy((caddr_t)&ip_srcrt.nop, 878 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 879 q = (struct in_addr *)(mtod(m, caddr_t) + 880 sizeof(struct in_addr) + OPTSIZ); 881 #undef OPTSIZ 882 /* 883 * Record return path as an IP source route, 884 * reversing the path (pointers are now aligned). 885 */ 886 while (p >= ip_srcrt.route) { 887 #ifdef DIAGNOSTIC 888 if (ipprintfs) 889 printf(" %lx", ntohl(q->s_addr)); 890 #endif 891 *q++ = *p--; 892 } 893 /* 894 * Last hop goes to final destination. 895 */ 896 *q = ip_srcrt.dst; 897 #ifdef DIAGNOSTIC 898 if (ipprintfs) 899 printf(" %lx\n", ntohl(q->s_addr)); 900 #endif 901 return (m); 902 } 903 904 /* 905 * Strip out IP options, at higher 906 * level protocol in the kernel. 907 * Second argument is buffer to which options 908 * will be moved, and return value is their length. 909 * XXX should be deleted; last arg currently ignored. 910 */ 911 ip_stripoptions(m, mopt) 912 register struct mbuf *m; 913 struct mbuf *mopt; 914 { 915 register int i; 916 struct ip *ip = mtod(m, struct ip *); 917 register caddr_t opts; 918 int olen; 919 920 olen = (ip->ip_hl<<2) - sizeof (struct ip); 921 opts = (caddr_t)(ip + 1); 922 i = m->m_len - (sizeof (struct ip) + olen); 923 bcopy(opts + olen, opts, (unsigned)i); 924 m->m_len -= olen; 925 if (m->m_flags & M_PKTHDR) 926 m->m_pkthdr.len -= olen; 927 ip->ip_hl = sizeof(struct ip) >> 2; 928 } 929 930 u_char inetctlerrmap[PRC_NCMDS] = { 931 0, 0, 0, 0, 932 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 933 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 934 EMSGSIZE, EHOSTUNREACH, 0, 0, 935 0, 0, 0, 0, 936 ENOPROTOOPT 937 }; 938 939 /* 940 * Forward a packet. If some error occurs return the sender 941 * an icmp packet. Note we can't always generate a meaningful 942 * icmp message because icmp doesn't have a large enough repertoire 943 * of codes and types. 944 * 945 * If not forwarding, just drop the packet. This could be confusing 946 * if ipforwarding was zero but some routing protocol was advancing 947 * us as a gateway to somewhere. However, we must let the routing 948 * protocol deal with that. 949 * 950 * The srcrt parameter indicates whether the packet is being forwarded 951 * via a source route. 952 */ 953 ip_forward(m, srcrt) 954 struct mbuf *m; 955 int srcrt; 956 { 957 register struct ip *ip = mtod(m, struct ip *); 958 register struct sockaddr_in *sin; 959 register struct rtentry *rt; 960 int error, type = 0, code; 961 struct mbuf *mcopy; 962 struct in_addr dest; 963 struct ifnet *destifp; 964 965 dest.s_addr = 0; 966 #ifdef DIAGNOSTIC 967 if (ipprintfs) 968 printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 969 ip->ip_dst, ip->ip_ttl); 970 #endif 971 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 972 ipstat.ips_cantforward++; 973 m_freem(m); 974 return; 975 } 976 HTONS(ip->ip_id); 977 if (ip->ip_ttl <= IPTTLDEC) { 978 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest); 979 return; 980 } 981 ip->ip_ttl -= IPTTLDEC; 982 983 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 984 if ((rt = ipforward_rt.ro_rt) == 0 || 985 ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 986 if (ipforward_rt.ro_rt) { 987 RTFREE(ipforward_rt.ro_rt); 988 ipforward_rt.ro_rt = 0; 989 } 990 sin->sin_family = AF_INET; 991 sin->sin_len = sizeof(*sin); 992 sin->sin_addr = ip->ip_dst; 993 994 rtalloc(&ipforward_rt); 995 if (ipforward_rt.ro_rt == 0) { 996 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest); 997 return; 998 } 999 rt = ipforward_rt.ro_rt; 1000 } 1001 1002 /* 1003 * Save at most 64 bytes of the packet in case 1004 * we need to generate an ICMP message to the src. 1005 */ 1006 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 1007 1008 #ifdef GATEWAY 1009 ip_ifmatrix[rt->rt_ifp->if_index + 1010 if_index * m->m_pkthdr.rcvif->if_index]++; 1011 #endif 1012 /* 1013 * If forwarding packet using same interface that it came in on, 1014 * perhaps should send a redirect to sender to shortcut a hop. 1015 * Only send redirect if source is sending directly to us, 1016 * and if packet was not source routed (or has any options). 1017 * Also, don't send redirect if forwarding using a default route 1018 * or a route modified by a redirect. 1019 */ 1020 #define satosin(sa) ((struct sockaddr_in *)(sa)) 1021 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1022 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1023 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1024 ipsendredirects && !srcrt) { 1025 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1026 u_long src = ntohl(ip->ip_src.s_addr); 1027 u_long dst = ntohl(ip->ip_dst.s_addr); 1028 1029 if (RTA(rt) && 1030 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1031 if (rt->rt_flags & RTF_GATEWAY) 1032 dest = satosin(rt->rt_gateway)->sin_addr; 1033 else 1034 dest = ip->ip_dst; 1035 /* 1036 * If the destination is reached by a route to host, 1037 * is on a subnet of a local net, or is directly 1038 * on the attached net (!), use host redirect. 1039 * (We may be the correct first hop for other subnets.) 1040 */ 1041 type = ICMP_REDIRECT; 1042 if ((rt->rt_flags & RTF_HOST) || 1043 (rt->rt_flags & RTF_GATEWAY) == 0) 1044 code = ICMP_REDIRECT_HOST; 1045 else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask && 1046 (dst & RTA(rt)->ia_netmask) == RTA(rt)->ia_net) 1047 code = ICMP_REDIRECT_HOST; 1048 else 1049 code = ICMP_REDIRECT_NET; 1050 #ifdef DIAGNOSTIC 1051 if (ipprintfs) 1052 printf("redirect (%d) to %x\n", code, dest.s_addr); 1053 #endif 1054 } 1055 } 1056 1057 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING, 0); 1058 if (error) 1059 ipstat.ips_cantforward++; 1060 else { 1061 ipstat.ips_forward++; 1062 if (type) 1063 ipstat.ips_redirectsent++; 1064 else { 1065 if (mcopy) 1066 m_freem(mcopy); 1067 return; 1068 } 1069 } 1070 if (mcopy == NULL) 1071 return; 1072 destifp = NULL; 1073 1074 switch (error) { 1075 1076 case 0: /* forwarded, but need redirect */ 1077 /* type, code set above */ 1078 break; 1079 1080 case ENETUNREACH: /* shouldn't happen, checked above */ 1081 case EHOSTUNREACH: 1082 case ENETDOWN: 1083 case EHOSTDOWN: 1084 default: 1085 type = ICMP_UNREACH; 1086 code = ICMP_UNREACH_HOST; 1087 break; 1088 1089 case EMSGSIZE: 1090 type = ICMP_UNREACH; 1091 code = ICMP_UNREACH_NEEDFRAG; 1092 if (ipforward_rt.ro_rt) 1093 destifp = ipforward_rt.ro_rt->rt_ifp; 1094 ipstat.ips_cantfrag++; 1095 break; 1096 1097 case ENOBUFS: 1098 type = ICMP_SOURCEQUENCH; 1099 code = 0; 1100 break; 1101 } 1102 icmp_error(mcopy, type, code, dest, destifp); 1103 } 1104