138#endif 139 140#ifdef IPSTEALTH 141static int ipstealth = 0; 142SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 143 &ipstealth, 0, ""); 144#endif 145 146 147/* Firewall hooks */ 148ip_fw_chk_t *ip_fw_chk_ptr; 149ip_fw_ctl_t *ip_fw_ctl_ptr; 150 151#ifdef DUMMYNET 152ip_dn_ctl_t *ip_dn_ctl_ptr; 153#endif 154 155#if defined(IPFILTER_LKM) || defined(IPFILTER) 156int iplattach __P((void)); 157int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL; 158#endif 159 160 161/* 162 * We need to save the IP options in case a protocol wants to respond 163 * to an incoming packet over the same route if the packet got here 164 * using IP source routing. This allows connection establishment and 165 * maintenance when the remote end is on a network that is not known 166 * to us. 167 */ 168static int ip_nhops = 0; 169static struct ip_srcrt { 170 struct in_addr dst; /* final destination */ 171 char nop; /* one NOP to align */ 172 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 173 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 174} ip_srcrt; 175 176#ifdef IPDIVERT 177/* 178 * Shared variable between ip_input() and ip_reass() to communicate 179 * about which packets, once assembled from fragments, get diverted, 180 * and to which port. 181 */ 182static u_short frag_divert_port; 183#endif 184 185struct sockaddr_in *ip_fw_fwd_addr; 186 187static void save_rte __P((u_char *, struct in_addr)); 188static int ip_dooptions __P((struct mbuf *)); 189static void ip_forward __P((struct mbuf *, int)); 190static void ip_freef __P((struct ipq *)); 191static struct ip * 192 ip_reass __P((struct mbuf *, struct ipq *, struct ipq *)); 193static struct in_ifaddr * 194 ip_rtaddr __P((struct in_addr)); 195static void ipintr __P((void)); 196/* 197 * IP initialization: fill in IP protocol switch table. 198 * All protocols not implemented in kernel go to raw IP protocol handler. 199 */ 200void 201ip_init() 202{ 203 register struct protosw *pr; 204 register int i; 205 206 TAILQ_INIT(&in_ifaddrhead); 207 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 208 if (pr == 0) 209 panic("ip_init"); 210 for (i = 0; i < IPPROTO_MAX; i++) 211 ip_protox[i] = pr - inetsw; 212 for (pr = inetdomain.dom_protosw; 213 pr < inetdomain.dom_protoswNPROTOSW; pr++) 214 if (pr->pr_domain->dom_family == PF_INET && 215 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 216 ip_protox[pr->pr_protocol] = pr - inetsw; 217 218 for (i = 0; i < IPREASS_NHASH; i++) 219 ipq[i].next = ipq[i].prev = &ipq[i]; 220 221 maxnipq = nmbclusters/4; 222 223 ip_id = time_second & 0xffff; 224 ipintrq.ifq_maxlen = ipqmaxlen; 225#ifdef IPFILTER 226 iplattach(); 227#endif 228 229} 230 231static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 232static struct route ipforward_rt; 233 234/* 235 * Ip input routine. Checksum and byte swap header. If fragmented 236 * try to reassemble. Process options. Pass to next level. 237 */ 238void 239ip_input(struct mbuf *m) 240{ 241 struct ip *ip; 242 struct ipq *fp; 243 struct in_ifaddr *ia; 244 int i, hlen, mff; 245 u_short sum; 246#ifndef IPDIVERT /* dummy variable for the firewall code to play with */ 247 u_short ip_divert_cookie = 0 ; 248#endif 249 struct ip_fw_chain *rule = NULL ; 250 251#if defined(IPFIREWALL) && defined(DUMMYNET) 252 /* 253 * dummynet packet are prepended a vestigial mbuf with 254 * m_type = MT_DUMMYNET and m_data pointing to the matching 255 * rule. 256 */ 257 if (m->m_type == MT_DUMMYNET) { 258 struct mbuf *m0 = m ; 259 rule = (struct ip_fw_chain *)(m->m_data) ; 260 m = m->m_next ; 261 free(m0, M_IPFW); 262 ip = mtod(m, struct ip *); 263 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 264 goto iphack ; 265 } else 266 rule = NULL ; 267#endif 268 269#ifdef DIAGNOSTIC 270 if (m == NULL || (m->m_flags & M_PKTHDR) == 0) 271 panic("ip_input no HDR"); 272#endif 273 ipstat.ips_total++; 274 275 if (m->m_pkthdr.len < sizeof(struct ip)) 276 goto tooshort; 277 278 if (m->m_len < sizeof (struct ip) && 279 (m = m_pullup(m, sizeof (struct ip))) == 0) { 280 ipstat.ips_toosmall++; 281 return; 282 } 283 ip = mtod(m, struct ip *); 284 285 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { 286 ipstat.ips_badvers++; 287 goto bad; 288 } 289 290 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 291 if (hlen < sizeof(struct ip)) { /* minimum header length */ 292 ipstat.ips_badhlen++; 293 goto bad; 294 } 295 if (hlen > m->m_len) { 296 if ((m = m_pullup(m, hlen)) == 0) { 297 ipstat.ips_badhlen++; 298 return; 299 } 300 ip = mtod(m, struct ip *); 301 } 302 if (hlen == sizeof(struct ip)) { 303 sum = in_cksum_hdr(ip); 304 } else { 305 sum = in_cksum(m, hlen); 306 } 307 if (sum) { 308 ipstat.ips_badsum++; 309 goto bad; 310 } 311 312 /* 313 * Convert fields to host representation. 314 */ 315 NTOHS(ip->ip_len); 316 if (ip->ip_len < hlen) { 317 ipstat.ips_badlen++; 318 goto bad; 319 } 320 NTOHS(ip->ip_id); 321 NTOHS(ip->ip_off); 322 323 /* 324 * Check that the amount of data in the buffers 325 * is as at least much as the IP header would have us expect. 326 * Trim mbufs if longer than we expect. 327 * Drop packet if shorter than we expect. 328 */ 329 if (m->m_pkthdr.len < ip->ip_len) { 330tooshort: 331 ipstat.ips_tooshort++; 332 goto bad; 333 } 334 if (m->m_pkthdr.len > ip->ip_len) { 335 if (m->m_len == m->m_pkthdr.len) { 336 m->m_len = ip->ip_len; 337 m->m_pkthdr.len = ip->ip_len; 338 } else 339 m_adj(m, ip->ip_len - m->m_pkthdr.len); 340 } 341 /* 342 * IpHack's section. 343 * Right now when no processing on packet has done 344 * and it is still fresh out of network we do our black 345 * deals with it. 346 * - Firewall: deny/allow/divert 347 * - Xlate: translate packet's addr/port (NAT). 348 * - Pipe: pass pkt through dummynet. 349 * - Wrap: fake packet's addr/port <unimpl.> 350 * - Encapsulate: put it in another IP and send out. <unimp.> 351 */ 352 353#if defined(IPFIREWALL) && defined(DUMMYNET) 354iphack: 355#endif 356#if defined(IPFILTER) || defined(IPFILTER_LKM) 357 /* 358 * Check if we want to allow this packet to be processed. 359 * Consider it to be bad if not. 360 */ 361 if (fr_checkp) { 362 struct mbuf *m1 = m; 363 364 if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1) 365 return; 366 ip = mtod(m = m1, struct ip *); 367 } 368#endif 369 if (ip_fw_chk_ptr) { 370#ifdef IPFIREWALL_FORWARD 371 /* 372 * If we've been forwarded from the output side, then 373 * skip the firewall a second time 374 */ 375 if (ip_fw_fwd_addr) 376 goto ours; 377#endif /* IPFIREWALL_FORWARD */ 378 i = (*ip_fw_chk_ptr)(&ip, hlen, NULL, &ip_divert_cookie, 379 &m, &rule, &ip_fw_fwd_addr); 380 /* 381 * see the comment in ip_output for the return values 382 * produced by the firewall. 383 */ 384 if (!m) /* packet discarded by firewall */ 385 return ; 386 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */ 387 goto pass ; 388#ifdef DUMMYNET 389 if (i & 0x10000) { 390 /* send packet to the appropriate pipe */ 391 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule); 392 return ; 393 } 394#endif 395#ifdef IPDIVERT 396 if (i > 0 && i < 0x10000) { 397 /* Divert packet */ 398 frag_divert_port = i & 0xffff ; 399 goto ours; 400 } 401#endif 402#ifdef IPFIREWALL_FORWARD 403 if (i == 0 && ip_fw_fwd_addr != NULL) 404 goto pass ; 405#endif 406 /* 407 * if we get here, the packet must be dropped 408 */ 409 m_freem(m); 410 return; 411 } 412pass: 413 414 /* 415 * Process options and, if not destined for us, 416 * ship it on. ip_dooptions returns 1 when an 417 * error was detected (causing an icmp message 418 * to be sent and the original packet to be freed). 419 */ 420 ip_nhops = 0; /* for source routed packets */ 421 if (hlen > sizeof (struct ip) && ip_dooptions(m)) { 422#ifdef IPFIREWALL_FORWARD 423 ip_fw_fwd_addr = NULL; 424#endif 425 return; 426 } 427 428 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 429 * matter if it is destined to another node, or whether it is 430 * a multicast one, RSVP wants it! and prevents it from being forwarded 431 * anywhere else. Also checks if the rsvp daemon is running before 432 * grabbing the packet. 433 */ 434 if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 435 goto ours; 436 437 /* 438 * Check our list of addresses, to see if the packet is for us. 439 * If we don't have any addresses, assume any unicast packet 440 * we receive might be for us (and let the upper layers deal 441 * with it). 442 */ 443 if (TAILQ_EMPTY(&in_ifaddrhead) && 444 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 445 goto ours; 446 447 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia; 448 ia = TAILQ_NEXT(ia, ia_link)) { 449#define satosin(sa) ((struct sockaddr_in *)(sa)) 450 451#ifdef BOOTP_COMPAT 452 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 453 goto ours; 454#endif 455#ifdef IPFIREWALL_FORWARD 456 /* 457 * If the addr to forward to is one of ours, we pretend to 458 * be the destination for this packet. 459 */ 460 if (ip_fw_fwd_addr == NULL) { 461 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 462 goto ours; 463 } else if (IA_SIN(ia)->sin_addr.s_addr == 464 ip_fw_fwd_addr->sin_addr.s_addr) 465 goto ours; 466#else 467 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 468 goto ours; 469#endif 470 if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) { 471 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 472 ip->ip_dst.s_addr) 473 goto ours; 474 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 475 goto ours; 476 } 477 } 478 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 479 struct in_multi *inm; 480 if (ip_mrouter) { 481 /* 482 * If we are acting as a multicast router, all 483 * incoming multicast packets are passed to the 484 * kernel-level multicast forwarding function. 485 * The packet is returned (relatively) intact; if 486 * ip_mforward() returns a non-zero value, the packet 487 * must be discarded, else it may be accepted below. 488 * 489 * (The IP ident field is put in the same byte order 490 * as expected when ip_mforward() is called from 491 * ip_output().) 492 */ 493 ip->ip_id = htons(ip->ip_id); 494 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 495 ipstat.ips_cantforward++; 496 m_freem(m); 497 return; 498 } 499 ip->ip_id = ntohs(ip->ip_id); 500 501 /* 502 * The process-level routing demon needs to receive 503 * all multicast IGMP packets, whether or not this 504 * host belongs to their destination groups. 505 */ 506 if (ip->ip_p == IPPROTO_IGMP) 507 goto ours; 508 ipstat.ips_forward++; 509 } 510 /* 511 * See if we belong to the destination multicast group on the 512 * arrival interface. 513 */ 514 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 515 if (inm == NULL) { 516 ipstat.ips_notmember++; 517 m_freem(m); 518 return; 519 } 520 goto ours; 521 } 522 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 523 goto ours; 524 if (ip->ip_dst.s_addr == INADDR_ANY) 525 goto ours; 526 527 /* 528 * Not for us; forward if possible and desirable. 529 */ 530 if (ipforwarding == 0) { 531 ipstat.ips_cantforward++; 532 m_freem(m); 533 } else 534 ip_forward(m, 0); 535#ifdef IPFIREWALL_FORWARD 536 ip_fw_fwd_addr = NULL; 537#endif 538 return; 539 540ours: 541 542 /* 543 * If offset or IP_MF are set, must reassemble. 544 * Otherwise, nothing need be done. 545 * (We could look in the reassembly queue to see 546 * if the packet was previously fragmented, 547 * but it's not worth the time; just let them time out.) 548 */ 549 if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) { 550 if (m->m_flags & M_EXT) { /* XXX */ 551 if ((m = m_pullup(m, hlen)) == 0) { 552 ipstat.ips_toosmall++; 553#ifdef IPDIVERT 554 frag_divert_port = 0; 555 ip_divert_cookie = 0; 556#endif 557#ifdef IPFIREWALL_FORWARD 558 ip_fw_fwd_addr = NULL; 559#endif 560 return; 561 } 562 ip = mtod(m, struct ip *); 563 } 564 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 565 /* 566 * Look for queue of fragments 567 * of this datagram. 568 */ 569 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next) 570 if (ip->ip_id == fp->ipq_id && 571 ip->ip_src.s_addr == fp->ipq_src.s_addr && 572 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 573 ip->ip_p == fp->ipq_p) 574 goto found; 575 576 fp = 0; 577 578 /* check if there's a place for the new queue */ 579 if (nipq > maxnipq) { 580 /* 581 * drop something from the tail of the current queue 582 * before proceeding further 583 */ 584 if (ipq[sum].prev == &ipq[sum]) { /* gak */ 585 for (i = 0; i < IPREASS_NHASH; i++) { 586 if (ipq[i].prev != &ipq[i]) { 587 ip_freef(ipq[i].prev); 588 break; 589 } 590 } 591 } else 592 ip_freef(ipq[sum].prev); 593 } 594found: 595 /* 596 * Adjust ip_len to not reflect header, 597 * set ip_mff if more fragments are expected, 598 * convert offset of this to bytes. 599 */ 600 ip->ip_len -= hlen; 601 mff = (ip->ip_off & IP_MF) != 0; 602 if (mff) { 603 /* 604 * Make sure that fragments have a data length 605 * that's a non-zero multiple of 8 bytes. 606 */ 607 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 608 ipstat.ips_toosmall++; /* XXX */ 609 goto bad; 610 } 611 m->m_flags |= M_FRAG; 612 } 613 ip->ip_off <<= 3; 614 615 /* 616 * If datagram marked as having more fragments 617 * or if this is not the first fragment, 618 * attempt reassembly; if it succeeds, proceed. 619 */ 620 if (mff || ip->ip_off) { 621 ipstat.ips_fragments++; 622 m->m_pkthdr.header = ip; 623 ip = ip_reass(m, fp, &ipq[sum]); 624 if (ip == 0) { 625#ifdef IPFIREWALL_FORWARD 626 ip_fw_fwd_addr = NULL; 627#endif 628 return; 629 } 630 /* Get the length of the reassembled packets header */ 631 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 632 ipstat.ips_reassembled++; 633 m = dtom(ip); 634#ifdef IPDIVERT 635 if (frag_divert_port) { 636 ip->ip_len += hlen; 637 HTONS(ip->ip_len); 638 HTONS(ip->ip_off); 639 HTONS(ip->ip_id); 640 ip->ip_sum = 0; 641 ip->ip_sum = in_cksum_hdr(ip); 642 NTOHS(ip->ip_id); 643 NTOHS(ip->ip_off); 644 NTOHS(ip->ip_len); 645 ip->ip_len -= hlen; 646 } 647#endif 648 } else 649 if (fp) 650 ip_freef(fp); 651 } else 652 ip->ip_len -= hlen; 653 654#ifdef IPDIVERT 655 /* 656 * Divert reassembled packets to the divert protocol if required 657 * If divert port is null then cookie should be too, 658 * so we shouldn't need to clear them here. Assume ip_divert does so. 659 */ 660 if (frag_divert_port) { 661 ipstat.ips_delivered++; 662 ip_divert_port = frag_divert_port; 663 frag_divert_port = 0; 664 (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, hlen); 665 return; 666 } 667 668 /* Don't let packets divert themselves */ 669 if (ip->ip_p == IPPROTO_DIVERT) { 670 ipstat.ips_noproto++; 671 goto bad; 672 } 673 674#endif 675 676 /* 677 * Switch out to protocol's input routine. 678 */ 679 ipstat.ips_delivered++; 680 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 681#ifdef IPFIREWALL_FORWARD 682 ip_fw_fwd_addr = NULL; /* tcp needed it */ 683#endif 684 return; 685bad: 686#ifdef IPFIREWALL_FORWARD 687 ip_fw_fwd_addr = NULL; 688#endif 689 m_freem(m); 690} 691 692/* 693 * IP software interrupt routine - to go away sometime soon 694 */ 695static void 696ipintr(void) 697{ 698 int s; 699 struct mbuf *m; 700 701 while(1) { 702 s = splimp(); 703 IF_DEQUEUE(&ipintrq, m); 704 splx(s); 705 if (m == 0) 706 return; 707 ip_input(m); 708 } 709} 710 711NETISR_SET(NETISR_IP, ipintr); 712 713/* 714 * Take incoming datagram fragment and try to 715 * reassemble it into whole datagram. If a chain for 716 * reassembly of this datagram already exists, then it 717 * is given as fp; otherwise have to make a chain. 718 */ 719static struct ip * 720ip_reass(m, fp, where) 721 register struct mbuf *m; 722 register struct ipq *fp; 723 struct ipq *where; 724{ 725 struct ip *ip = mtod(m, struct ip *); 726 register struct mbuf *p = 0, *q, *nq; 727 struct mbuf *t; 728 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 729 int i, next; 730 731 /* 732 * Presence of header sizes in mbufs 733 * would confuse code below. 734 */ 735 m->m_data += hlen; 736 m->m_len -= hlen; 737 738 /* 739 * If first fragment to arrive, create a reassembly queue. 740 */ 741 if (fp == 0) { 742 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 743 goto dropfrag; 744 fp = mtod(t, struct ipq *); 745 insque(fp, where); 746 nipq++; 747 fp->ipq_ttl = IPFRAGTTL; 748 fp->ipq_p = ip->ip_p; 749 fp->ipq_id = ip->ip_id; 750 fp->ipq_src = ip->ip_src; 751 fp->ipq_dst = ip->ip_dst; 752 fp->ipq_frags = m; 753 m->m_nextpkt = NULL; 754#ifdef IPDIVERT 755 fp->ipq_divert = 0; 756 fp->ipq_div_cookie = 0; 757#endif 758 goto inserted; 759 } 760 761#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 762 763 /* 764 * Find a segment which begins after this one does. 765 */ 766 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 767 if (GETIP(q)->ip_off > ip->ip_off) 768 break; 769 770 /* 771 * If there is a preceding segment, it may provide some of 772 * our data already. If so, drop the data from the incoming 773 * segment. If it provides all of our data, drop us, otherwise 774 * stick new segment in the proper place. 775 */ 776 if (p) { 777 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 778 if (i > 0) { 779 if (i >= ip->ip_len) 780 goto dropfrag; 781 m_adj(dtom(ip), i); 782 ip->ip_off += i; 783 ip->ip_len -= i; 784 } 785 m->m_nextpkt = p->m_nextpkt; 786 p->m_nextpkt = m; 787 } else { 788 m->m_nextpkt = fp->ipq_frags; 789 fp->ipq_frags = m; 790 } 791 792 /* 793 * While we overlap succeeding segments trim them or, 794 * if they are completely covered, dequeue them. 795 */ 796 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 797 q = nq) { 798 i = (ip->ip_off + ip->ip_len) - 799 GETIP(q)->ip_off; 800 if (i < GETIP(q)->ip_len) { 801 GETIP(q)->ip_len -= i; 802 GETIP(q)->ip_off += i; 803 m_adj(q, i); 804 break; 805 } 806 nq = q->m_nextpkt; 807 m->m_nextpkt = nq; 808 m_freem(q); 809 } 810 811inserted: 812 813#ifdef IPDIVERT 814 /* 815 * Any fragment diverting causes the whole packet to divert 816 */ 817 if (frag_divert_port) { 818 fp->ipq_divert = frag_divert_port; 819 fp->ipq_div_cookie = ip_divert_cookie; 820 } 821 frag_divert_port = 0; 822 ip_divert_cookie = 0; 823#endif 824 825 /* 826 * Check for complete reassembly. 827 */ 828 next = 0; 829 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 830 if (GETIP(q)->ip_off != next) 831 return (0); 832 next += GETIP(q)->ip_len; 833 } 834 /* Make sure the last packet didn't have the IP_MF flag */ 835 if (p->m_flags & M_FRAG) 836 return (0); 837 838 /* 839 * Reassembly is complete. Make sure the packet is a sane size. 840 */ 841 q = fp->ipq_frags; 842 ip = GETIP(q); 843 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { 844 ipstat.ips_toolong++; 845 ip_freef(fp); 846 return (0); 847 } 848 849 /* 850 * Concatenate fragments. 851 */ 852 m = q; 853 t = m->m_next; 854 m->m_next = 0; 855 m_cat(m, t); 856 nq = q->m_nextpkt; 857 q->m_nextpkt = 0; 858 for (q = nq; q != NULL; q = nq) { 859 nq = q->m_nextpkt; 860 q->m_nextpkt = NULL; 861 m_cat(m, q); 862 } 863 864#ifdef IPDIVERT 865 /* 866 * extract divert port for packet, if any 867 */ 868 frag_divert_port = fp->ipq_divert; 869 ip_divert_cookie = fp->ipq_div_cookie; 870#endif 871 872 /* 873 * Create header for new ip packet by 874 * modifying header of first packet; 875 * dequeue and discard fragment reassembly header. 876 * Make header visible. 877 */ 878 ip->ip_len = next; 879 ip->ip_src = fp->ipq_src; 880 ip->ip_dst = fp->ipq_dst; 881 remque(fp); 882 nipq--; 883 (void) m_free(dtom(fp)); 884 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); 885 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); 886 /* some debugging cruft by sklower, below, will go away soon */ 887 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 888 register int plen = 0; 889 for (t = m; m; m = m->m_next) 890 plen += m->m_len; 891 t->m_pkthdr.len = plen; 892 } 893 return (ip); 894 895dropfrag: 896#ifdef IPDIVERT 897 frag_divert_port = 0; 898 ip_divert_cookie = 0; 899#endif 900 ipstat.ips_fragdropped++; 901 m_freem(m); 902 return (0); 903 904#undef GETIP 905} 906 907/* 908 * Free a fragment reassembly header and all 909 * associated datagrams. 910 */ 911static void 912ip_freef(fp) 913 struct ipq *fp; 914{ 915 register struct mbuf *q; 916 917 while (fp->ipq_frags) { 918 q = fp->ipq_frags; 919 fp->ipq_frags = q->m_nextpkt; 920 m_freem(q); 921 } 922 remque(fp); 923 (void) m_free(dtom(fp)); 924 nipq--; 925} 926 927/* 928 * IP timer processing; 929 * if a timer expires on a reassembly 930 * queue, discard it. 931 */ 932void 933ip_slowtimo() 934{ 935 register struct ipq *fp; 936 int s = splnet(); 937 int i; 938 939 for (i = 0; i < IPREASS_NHASH; i++) { 940 fp = ipq[i].next; 941 if (fp == 0) 942 continue; 943 while (fp != &ipq[i]) { 944 --fp->ipq_ttl; 945 fp = fp->next; 946 if (fp->prev->ipq_ttl == 0) { 947 ipstat.ips_fragtimeout++; 948 ip_freef(fp->prev); 949 } 950 } 951 } 952 ipflow_slowtimo(); 953 splx(s); 954} 955 956/* 957 * Drain off all datagram fragments. 958 */ 959void 960ip_drain() 961{ 962 int i; 963 964 for (i = 0; i < IPREASS_NHASH; i++) { 965 while (ipq[i].next != &ipq[i]) { 966 ipstat.ips_fragdropped++; 967 ip_freef(ipq[i].next); 968 } 969 } 970 in_rtqdrain(); 971} 972 973/* 974 * Do option processing on a datagram, 975 * possibly discarding it if bad options are encountered, 976 * or forwarding it if source-routed. 977 * Returns 1 if packet has been forwarded/freed, 978 * 0 if the packet should be processed further. 979 */ 980static int 981ip_dooptions(m) 982 struct mbuf *m; 983{ 984 register struct ip *ip = mtod(m, struct ip *); 985 register u_char *cp; 986 register struct ip_timestamp *ipt; 987 register struct in_ifaddr *ia; 988 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 989 struct in_addr *sin, dst; 990 n_time ntime; 991 992 dst = ip->ip_dst; 993 cp = (u_char *)(ip + 1); 994 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 995 for (; cnt > 0; cnt -= optlen, cp += optlen) { 996 opt = cp[IPOPT_OPTVAL]; 997 if (opt == IPOPT_EOL) 998 break; 999 if (opt == IPOPT_NOP) 1000 optlen = 1; 1001 else { 1002 optlen = cp[IPOPT_OLEN]; 1003 if (optlen <= 0 || optlen > cnt) { 1004 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1005 goto bad; 1006 } 1007 } 1008 switch (opt) { 1009 1010 default: 1011 break; 1012 1013 /* 1014 * Source routing with record. 1015 * Find interface with current destination address. 1016 * If none on this machine then drop if strictly routed, 1017 * or do nothing if loosely routed. 1018 * Record interface address and bring up next address 1019 * component. If strictly routed make sure next 1020 * address is on directly accessible net. 1021 */ 1022 case IPOPT_LSRR: 1023 case IPOPT_SSRR: 1024 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1025 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1026 goto bad; 1027 } 1028 ipaddr.sin_addr = ip->ip_dst; 1029 ia = (struct in_ifaddr *) 1030 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1031 if (ia == 0) { 1032 if (opt == IPOPT_SSRR) { 1033 type = ICMP_UNREACH; 1034 code = ICMP_UNREACH_SRCFAIL; 1035 goto bad; 1036 } 1037 if (!ip_dosourceroute) 1038 goto nosourcerouting; 1039 /* 1040 * Loose routing, and not at next destination 1041 * yet; nothing to do except forward. 1042 */ 1043 break; 1044 } 1045 off--; /* 0 origin */ 1046 if (off > optlen - sizeof(struct in_addr)) { 1047 /* 1048 * End of source route. Should be for us. 1049 */ 1050 if (!ip_acceptsourceroute) 1051 goto nosourcerouting; 1052 save_rte(cp, ip->ip_src); 1053 break; 1054 } 1055 1056 if (!ip_dosourceroute) { 1057 if (ipforwarding) { 1058 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1059 /* 1060 * Acting as a router, so generate ICMP 1061 */ 1062nosourcerouting: 1063 strcpy(buf, inet_ntoa(ip->ip_dst)); 1064 log(LOG_WARNING, 1065 "attempted source route from %s to %s\n", 1066 inet_ntoa(ip->ip_src), buf); 1067 type = ICMP_UNREACH; 1068 code = ICMP_UNREACH_SRCFAIL; 1069 goto bad; 1070 } else { 1071 /* 1072 * Not acting as a router, so silently drop. 1073 */ 1074 ipstat.ips_cantforward++; 1075 m_freem(m); 1076 return (1); 1077 } 1078 } 1079 1080 /* 1081 * locate outgoing interface 1082 */ 1083 (void)memcpy(&ipaddr.sin_addr, cp + off, 1084 sizeof(ipaddr.sin_addr)); 1085 1086 if (opt == IPOPT_SSRR) { 1087#define INA struct in_ifaddr * 1088#define SA struct sockaddr * 1089 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1090 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1091 } else 1092 ia = ip_rtaddr(ipaddr.sin_addr); 1093 if (ia == 0) { 1094 type = ICMP_UNREACH; 1095 code = ICMP_UNREACH_SRCFAIL; 1096 goto bad; 1097 } 1098 ip->ip_dst = ipaddr.sin_addr; 1099 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1100 sizeof(struct in_addr)); 1101 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1102 /* 1103 * Let ip_intr's mcast routing check handle mcast pkts 1104 */ 1105 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1106 break; 1107 1108 case IPOPT_RR: 1109 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1110 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1111 goto bad; 1112 } 1113 /* 1114 * If no space remains, ignore. 1115 */ 1116 off--; /* 0 origin */ 1117 if (off > optlen - sizeof(struct in_addr)) 1118 break; 1119 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1120 sizeof(ipaddr.sin_addr)); 1121 /* 1122 * locate outgoing interface; if we're the destination, 1123 * use the incoming interface (should be same). 1124 */ 1125 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1126 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1127 type = ICMP_UNREACH; 1128 code = ICMP_UNREACH_HOST; 1129 goto bad; 1130 } 1131 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1132 sizeof(struct in_addr)); 1133 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1134 break; 1135 1136 case IPOPT_TS: 1137 code = cp - (u_char *)ip; 1138 ipt = (struct ip_timestamp *)cp; 1139 if (ipt->ipt_len < 5) 1140 goto bad; 1141 if (ipt->ipt_ptr > ipt->ipt_len - sizeof(int32_t)) { 1142 if (++ipt->ipt_oflw == 0) 1143 goto bad; 1144 break; 1145 } 1146 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 1147 switch (ipt->ipt_flg) { 1148 1149 case IPOPT_TS_TSONLY: 1150 break; 1151 1152 case IPOPT_TS_TSANDADDR: 1153 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1154 sizeof(struct in_addr) > ipt->ipt_len) 1155 goto bad; 1156 ipaddr.sin_addr = dst; 1157 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1158 m->m_pkthdr.rcvif); 1159 if (ia == 0) 1160 continue; 1161 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1162 sizeof(struct in_addr)); 1163 ipt->ipt_ptr += sizeof(struct in_addr); 1164 break; 1165 1166 case IPOPT_TS_PRESPEC: 1167 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1168 sizeof(struct in_addr) > ipt->ipt_len) 1169 goto bad; 1170 (void)memcpy(&ipaddr.sin_addr, sin, 1171 sizeof(struct in_addr)); 1172 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1173 continue; 1174 ipt->ipt_ptr += sizeof(struct in_addr); 1175 break; 1176 1177 default: 1178 goto bad; 1179 } 1180 ntime = iptime(); 1181 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, 1182 sizeof(n_time)); 1183 ipt->ipt_ptr += sizeof(n_time); 1184 } 1185 } 1186 if (forward && ipforwarding) { 1187 ip_forward(m, 1); 1188 return (1); 1189 } 1190 return (0); 1191bad: 1192 ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */ 1193 icmp_error(m, type, code, 0, 0); 1194 ipstat.ips_badoptions++; 1195 return (1); 1196} 1197 1198/* 1199 * Given address of next destination (final or next hop), 1200 * return internet address info of interface to be used to get there. 1201 */ 1202static struct in_ifaddr * 1203ip_rtaddr(dst) 1204 struct in_addr dst; 1205{ 1206 register struct sockaddr_in *sin; 1207 1208 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 1209 1210 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 1211 if (ipforward_rt.ro_rt) { 1212 RTFREE(ipforward_rt.ro_rt); 1213 ipforward_rt.ro_rt = 0; 1214 } 1215 sin->sin_family = AF_INET; 1216 sin->sin_len = sizeof(*sin); 1217 sin->sin_addr = dst; 1218 1219 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1220 } 1221 if (ipforward_rt.ro_rt == 0) 1222 return ((struct in_ifaddr *)0); 1223 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 1224} 1225 1226/* 1227 * Save incoming source route for use in replies, 1228 * to be picked up later by ip_srcroute if the receiver is interested. 1229 */ 1230void 1231save_rte(option, dst) 1232 u_char *option; 1233 struct in_addr dst; 1234{ 1235 unsigned olen; 1236 1237 olen = option[IPOPT_OLEN]; 1238#ifdef DIAGNOSTIC 1239 if (ipprintfs) 1240 printf("save_rte: olen %d\n", olen); 1241#endif 1242 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1243 return; 1244 bcopy(option, ip_srcrt.srcopt, olen); 1245 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1246 ip_srcrt.dst = dst; 1247} 1248 1249/* 1250 * Retrieve incoming source route for use in replies, 1251 * in the same form used by setsockopt. 1252 * The first hop is placed before the options, will be removed later. 1253 */ 1254struct mbuf * 1255ip_srcroute() 1256{ 1257 register struct in_addr *p, *q; 1258 register struct mbuf *m; 1259 1260 if (ip_nhops == 0) 1261 return ((struct mbuf *)0); 1262 m = m_get(M_DONTWAIT, MT_HEADER); 1263 if (m == 0) 1264 return ((struct mbuf *)0); 1265 1266#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1267 1268 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1269 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1270 OPTSIZ; 1271#ifdef DIAGNOSTIC 1272 if (ipprintfs) 1273 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1274#endif 1275 1276 /* 1277 * First save first hop for return route 1278 */ 1279 p = &ip_srcrt.route[ip_nhops - 1]; 1280 *(mtod(m, struct in_addr *)) = *p--; 1281#ifdef DIAGNOSTIC 1282 if (ipprintfs) 1283 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1284#endif 1285 1286 /* 1287 * Copy option fields and padding (nop) to mbuf. 1288 */ 1289 ip_srcrt.nop = IPOPT_NOP; 1290 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1291 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1292 &ip_srcrt.nop, OPTSIZ); 1293 q = (struct in_addr *)(mtod(m, caddr_t) + 1294 sizeof(struct in_addr) + OPTSIZ); 1295#undef OPTSIZ 1296 /* 1297 * Record return path as an IP source route, 1298 * reversing the path (pointers are now aligned). 1299 */ 1300 while (p >= ip_srcrt.route) { 1301#ifdef DIAGNOSTIC 1302 if (ipprintfs) 1303 printf(" %lx", (u_long)ntohl(q->s_addr)); 1304#endif 1305 *q++ = *p--; 1306 } 1307 /* 1308 * Last hop goes to final destination. 1309 */ 1310 *q = ip_srcrt.dst; 1311#ifdef DIAGNOSTIC 1312 if (ipprintfs) 1313 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1314#endif 1315 return (m); 1316} 1317 1318/* 1319 * Strip out IP options, at higher 1320 * level protocol in the kernel. 1321 * Second argument is buffer to which options 1322 * will be moved, and return value is their length. 1323 * XXX should be deleted; last arg currently ignored. 1324 */ 1325void 1326ip_stripoptions(m, mopt) 1327 register struct mbuf *m; 1328 struct mbuf *mopt; 1329{ 1330 register int i; 1331 struct ip *ip = mtod(m, struct ip *); 1332 register caddr_t opts; 1333 int olen; 1334 1335 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1336 opts = (caddr_t)(ip + 1); 1337 i = m->m_len - (sizeof (struct ip) + olen); 1338 bcopy(opts + olen, opts, (unsigned)i); 1339 m->m_len -= olen; 1340 if (m->m_flags & M_PKTHDR) 1341 m->m_pkthdr.len -= olen; 1342 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1343} 1344 1345u_char inetctlerrmap[PRC_NCMDS] = { 1346 0, 0, 0, 0, 1347 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1348 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1349 EMSGSIZE, EHOSTUNREACH, 0, 0, 1350 0, 0, 0, 0, 1351 ENOPROTOOPT 1352}; 1353 1354/* 1355 * Forward a packet. If some error occurs return the sender 1356 * an icmp packet. Note we can't always generate a meaningful 1357 * icmp message because icmp doesn't have a large enough repertoire 1358 * of codes and types. 1359 * 1360 * If not forwarding, just drop the packet. This could be confusing 1361 * if ipforwarding was zero but some routing protocol was advancing 1362 * us as a gateway to somewhere. However, we must let the routing 1363 * protocol deal with that. 1364 * 1365 * The srcrt parameter indicates whether the packet is being forwarded 1366 * via a source route. 1367 */ 1368static void 1369ip_forward(m, srcrt) 1370 struct mbuf *m; 1371 int srcrt; 1372{ 1373 register struct ip *ip = mtod(m, struct ip *); 1374 register struct sockaddr_in *sin; 1375 register struct rtentry *rt; 1376 int error, type = 0, code = 0; 1377 struct mbuf *mcopy; 1378 n_long dest; 1379 struct ifnet *destifp; 1380 1381 dest = 0; 1382#ifdef DIAGNOSTIC 1383 if (ipprintfs) 1384 printf("forward: src %lx dst %lx ttl %x\n", 1385 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr, 1386 ip->ip_ttl); 1387#endif 1388 1389 1390 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1391 ipstat.ips_cantforward++; 1392 m_freem(m); 1393 return; 1394 } 1395 HTONS(ip->ip_id); 1396#ifdef IPSTEALTH 1397 if (!ipstealth) { 1398#endif 1399 if (ip->ip_ttl <= IPTTLDEC) { 1400 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1401 dest, 0); 1402 return; 1403 } 1404 ip->ip_ttl -= IPTTLDEC; 1405#ifdef IPSTEALTH 1406 } 1407#endif 1408 1409 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 1410 if ((rt = ipforward_rt.ro_rt) == 0 || 1411 ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 1412 if (ipforward_rt.ro_rt) { 1413 RTFREE(ipforward_rt.ro_rt); 1414 ipforward_rt.ro_rt = 0; 1415 } 1416 sin->sin_family = AF_INET; 1417 sin->sin_len = sizeof(*sin); 1418 sin->sin_addr = ip->ip_dst; 1419 1420 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1421 if (ipforward_rt.ro_rt == 0) { 1422 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1423 return; 1424 } 1425 rt = ipforward_rt.ro_rt; 1426 } 1427 1428 /* 1429 * Save at most 64 bytes of the packet in case 1430 * we need to generate an ICMP message to the src. 1431 */ 1432 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 1433 1434 /* 1435 * If forwarding packet using same interface that it came in on, 1436 * perhaps should send a redirect to sender to shortcut a hop. 1437 * Only send redirect if source is sending directly to us, 1438 * and if packet was not source routed (or has any options). 1439 * Also, don't send redirect if forwarding using a default route 1440 * or a route modified by a redirect. 1441 */ 1442#define satosin(sa) ((struct sockaddr_in *)(sa)) 1443 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1444 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1445 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1446 ipsendredirects && !srcrt) { 1447#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1448 u_long src = ntohl(ip->ip_src.s_addr); 1449 1450 if (RTA(rt) && 1451 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1452 if (rt->rt_flags & RTF_GATEWAY) 1453 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1454 else 1455 dest = ip->ip_dst.s_addr; 1456 /* Router requirements says to only send host redirects */ 1457 type = ICMP_REDIRECT; 1458 code = ICMP_REDIRECT_HOST; 1459#ifdef DIAGNOSTIC 1460 if (ipprintfs) 1461 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1462#endif 1463 } 1464 } 1465 1466 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1467 IP_FORWARDING, 0); 1468 if (error) 1469 ipstat.ips_cantforward++; 1470 else { 1471 ipstat.ips_forward++; 1472 if (type) 1473 ipstat.ips_redirectsent++; 1474 else { 1475 if (mcopy) { 1476 ipflow_create(&ipforward_rt, mcopy); 1477 m_freem(mcopy); 1478 } 1479 return; 1480 } 1481 } 1482 if (mcopy == NULL) 1483 return; 1484 destifp = NULL; 1485 1486 switch (error) { 1487 1488 case 0: /* forwarded, but need redirect */ 1489 /* type, code set above */ 1490 break; 1491 1492 case ENETUNREACH: /* shouldn't happen, checked above */ 1493 case EHOSTUNREACH: 1494 case ENETDOWN: 1495 case EHOSTDOWN: 1496 default: 1497 type = ICMP_UNREACH; 1498 code = ICMP_UNREACH_HOST; 1499 break; 1500 1501 case EMSGSIZE: 1502 type = ICMP_UNREACH; 1503 code = ICMP_UNREACH_NEEDFRAG; 1504 if (ipforward_rt.ro_rt) 1505 destifp = ipforward_rt.ro_rt->rt_ifp; 1506 ipstat.ips_cantfrag++; 1507 break; 1508 1509 case ENOBUFS: 1510 type = ICMP_SOURCEQUENCH; 1511 code = 0; 1512 break; 1513 } 1514 icmp_error(mcopy, type, code, dest, destifp); 1515} 1516 1517void 1518ip_savecontrol(inp, mp, ip, m) 1519 register struct inpcb *inp; 1520 register struct mbuf **mp; 1521 register struct ip *ip; 1522 register struct mbuf *m; 1523{ 1524 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1525 struct timeval tv; 1526 1527 microtime(&tv); 1528 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1529 SCM_TIMESTAMP, SOL_SOCKET); 1530 if (*mp) 1531 mp = &(*mp)->m_next; 1532 } 1533 if (inp->inp_flags & INP_RECVDSTADDR) { 1534 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1535 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1536 if (*mp) 1537 mp = &(*mp)->m_next; 1538 } 1539#ifdef notyet 1540 /* XXX 1541 * Moving these out of udp_input() made them even more broken 1542 * than they already were. 1543 */ 1544 /* options were tossed already */ 1545 if (inp->inp_flags & INP_RECVOPTS) { 1546 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1547 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1548 if (*mp) 1549 mp = &(*mp)->m_next; 1550 } 1551 /* ip_srcroute doesn't do what we want here, need to fix */ 1552 if (inp->inp_flags & INP_RECVRETOPTS) { 1553 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1554 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1555 if (*mp) 1556 mp = &(*mp)->m_next; 1557 } 1558#endif 1559 if (inp->inp_flags & INP_RECVIF) { 1560 struct ifnet *ifp; 1561 struct sdlbuf { 1562 struct sockaddr_dl sdl; 1563 u_char pad[32]; 1564 } sdlbuf; 1565 struct sockaddr_dl *sdp; 1566 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1567 1568 if (((ifp = m->m_pkthdr.rcvif)) 1569 && ( ifp->if_index && (ifp->if_index <= if_index))) { 1570 sdp = (struct sockaddr_dl *)(ifnet_addrs 1571 [ifp->if_index - 1]->ifa_addr); 1572 /* 1573 * Change our mind and don't try copy. 1574 */ 1575 if ((sdp->sdl_family != AF_LINK) 1576 || (sdp->sdl_len > sizeof(sdlbuf))) { 1577 goto makedummy; 1578 } 1579 bcopy(sdp, sdl2, sdp->sdl_len); 1580 } else { 1581makedummy: 1582 sdl2->sdl_len 1583 = offsetof(struct sockaddr_dl, sdl_data[0]); 1584 sdl2->sdl_family = AF_LINK; 1585 sdl2->sdl_index = 0; 1586 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1587 } 1588 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 1589 IP_RECVIF, IPPROTO_IP); 1590 if (*mp) 1591 mp = &(*mp)->m_next; 1592 } 1593} 1594 1595int 1596ip_rsvp_init(struct socket *so) 1597{ 1598 if (so->so_type != SOCK_RAW || 1599 so->so_proto->pr_protocol != IPPROTO_RSVP) 1600 return EOPNOTSUPP; 1601 1602 if (ip_rsvpd != NULL) 1603 return EADDRINUSE; 1604 1605 ip_rsvpd = so; 1606 /* 1607 * This may seem silly, but we need to be sure we don't over-increment 1608 * the RSVP counter, in case something slips up. 1609 */ 1610 if (!ip_rsvp_on) { 1611 ip_rsvp_on = 1; 1612 rsvp_on++; 1613 } 1614 1615 return 0; 1616} 1617 1618int 1619ip_rsvp_done(void) 1620{ 1621 ip_rsvpd = NULL; 1622 /* 1623 * This may seem silly, but we need to be sure we don't over-decrement 1624 * the RSVP counter, in case something slips up. 1625 */ 1626 if (ip_rsvp_on) { 1627 ip_rsvp_on = 0; 1628 rsvp_on--; 1629 } 1630 return 0; 1631}
| 139#endif 140 141#ifdef IPSTEALTH 142static int ipstealth = 0; 143SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 144 &ipstealth, 0, ""); 145#endif 146 147 148/* Firewall hooks */ 149ip_fw_chk_t *ip_fw_chk_ptr; 150ip_fw_ctl_t *ip_fw_ctl_ptr; 151 152#ifdef DUMMYNET 153ip_dn_ctl_t *ip_dn_ctl_ptr; 154#endif 155 156#if defined(IPFILTER_LKM) || defined(IPFILTER) 157int iplattach __P((void)); 158int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL; 159#endif 160 161 162/* 163 * We need to save the IP options in case a protocol wants to respond 164 * to an incoming packet over the same route if the packet got here 165 * using IP source routing. This allows connection establishment and 166 * maintenance when the remote end is on a network that is not known 167 * to us. 168 */ 169static int ip_nhops = 0; 170static struct ip_srcrt { 171 struct in_addr dst; /* final destination */ 172 char nop; /* one NOP to align */ 173 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 174 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 175} ip_srcrt; 176 177#ifdef IPDIVERT 178/* 179 * Shared variable between ip_input() and ip_reass() to communicate 180 * about which packets, once assembled from fragments, get diverted, 181 * and to which port. 182 */ 183static u_short frag_divert_port; 184#endif 185 186struct sockaddr_in *ip_fw_fwd_addr; 187 188static void save_rte __P((u_char *, struct in_addr)); 189static int ip_dooptions __P((struct mbuf *)); 190static void ip_forward __P((struct mbuf *, int)); 191static void ip_freef __P((struct ipq *)); 192static struct ip * 193 ip_reass __P((struct mbuf *, struct ipq *, struct ipq *)); 194static struct in_ifaddr * 195 ip_rtaddr __P((struct in_addr)); 196static void ipintr __P((void)); 197/* 198 * IP initialization: fill in IP protocol switch table. 199 * All protocols not implemented in kernel go to raw IP protocol handler. 200 */ 201void 202ip_init() 203{ 204 register struct protosw *pr; 205 register int i; 206 207 TAILQ_INIT(&in_ifaddrhead); 208 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 209 if (pr == 0) 210 panic("ip_init"); 211 for (i = 0; i < IPPROTO_MAX; i++) 212 ip_protox[i] = pr - inetsw; 213 for (pr = inetdomain.dom_protosw; 214 pr < inetdomain.dom_protoswNPROTOSW; pr++) 215 if (pr->pr_domain->dom_family == PF_INET && 216 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 217 ip_protox[pr->pr_protocol] = pr - inetsw; 218 219 for (i = 0; i < IPREASS_NHASH; i++) 220 ipq[i].next = ipq[i].prev = &ipq[i]; 221 222 maxnipq = nmbclusters/4; 223 224 ip_id = time_second & 0xffff; 225 ipintrq.ifq_maxlen = ipqmaxlen; 226#ifdef IPFILTER 227 iplattach(); 228#endif 229 230} 231 232static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 233static struct route ipforward_rt; 234 235/* 236 * Ip input routine. Checksum and byte swap header. If fragmented 237 * try to reassemble. Process options. Pass to next level. 238 */ 239void 240ip_input(struct mbuf *m) 241{ 242 struct ip *ip; 243 struct ipq *fp; 244 struct in_ifaddr *ia; 245 int i, hlen, mff; 246 u_short sum; 247#ifndef IPDIVERT /* dummy variable for the firewall code to play with */ 248 u_short ip_divert_cookie = 0 ; 249#endif 250 struct ip_fw_chain *rule = NULL ; 251 252#if defined(IPFIREWALL) && defined(DUMMYNET) 253 /* 254 * dummynet packet are prepended a vestigial mbuf with 255 * m_type = MT_DUMMYNET and m_data pointing to the matching 256 * rule. 257 */ 258 if (m->m_type == MT_DUMMYNET) { 259 struct mbuf *m0 = m ; 260 rule = (struct ip_fw_chain *)(m->m_data) ; 261 m = m->m_next ; 262 free(m0, M_IPFW); 263 ip = mtod(m, struct ip *); 264 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 265 goto iphack ; 266 } else 267 rule = NULL ; 268#endif 269 270#ifdef DIAGNOSTIC 271 if (m == NULL || (m->m_flags & M_PKTHDR) == 0) 272 panic("ip_input no HDR"); 273#endif 274 ipstat.ips_total++; 275 276 if (m->m_pkthdr.len < sizeof(struct ip)) 277 goto tooshort; 278 279 if (m->m_len < sizeof (struct ip) && 280 (m = m_pullup(m, sizeof (struct ip))) == 0) { 281 ipstat.ips_toosmall++; 282 return; 283 } 284 ip = mtod(m, struct ip *); 285 286 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { 287 ipstat.ips_badvers++; 288 goto bad; 289 } 290 291 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 292 if (hlen < sizeof(struct ip)) { /* minimum header length */ 293 ipstat.ips_badhlen++; 294 goto bad; 295 } 296 if (hlen > m->m_len) { 297 if ((m = m_pullup(m, hlen)) == 0) { 298 ipstat.ips_badhlen++; 299 return; 300 } 301 ip = mtod(m, struct ip *); 302 } 303 if (hlen == sizeof(struct ip)) { 304 sum = in_cksum_hdr(ip); 305 } else { 306 sum = in_cksum(m, hlen); 307 } 308 if (sum) { 309 ipstat.ips_badsum++; 310 goto bad; 311 } 312 313 /* 314 * Convert fields to host representation. 315 */ 316 NTOHS(ip->ip_len); 317 if (ip->ip_len < hlen) { 318 ipstat.ips_badlen++; 319 goto bad; 320 } 321 NTOHS(ip->ip_id); 322 NTOHS(ip->ip_off); 323 324 /* 325 * Check that the amount of data in the buffers 326 * is as at least much as the IP header would have us expect. 327 * Trim mbufs if longer than we expect. 328 * Drop packet if shorter than we expect. 329 */ 330 if (m->m_pkthdr.len < ip->ip_len) { 331tooshort: 332 ipstat.ips_tooshort++; 333 goto bad; 334 } 335 if (m->m_pkthdr.len > ip->ip_len) { 336 if (m->m_len == m->m_pkthdr.len) { 337 m->m_len = ip->ip_len; 338 m->m_pkthdr.len = ip->ip_len; 339 } else 340 m_adj(m, ip->ip_len - m->m_pkthdr.len); 341 } 342 /* 343 * IpHack's section. 344 * Right now when no processing on packet has done 345 * and it is still fresh out of network we do our black 346 * deals with it. 347 * - Firewall: deny/allow/divert 348 * - Xlate: translate packet's addr/port (NAT). 349 * - Pipe: pass pkt through dummynet. 350 * - Wrap: fake packet's addr/port <unimpl.> 351 * - Encapsulate: put it in another IP and send out. <unimp.> 352 */ 353 354#if defined(IPFIREWALL) && defined(DUMMYNET) 355iphack: 356#endif 357#if defined(IPFILTER) || defined(IPFILTER_LKM) 358 /* 359 * Check if we want to allow this packet to be processed. 360 * Consider it to be bad if not. 361 */ 362 if (fr_checkp) { 363 struct mbuf *m1 = m; 364 365 if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1) 366 return; 367 ip = mtod(m = m1, struct ip *); 368 } 369#endif 370 if (ip_fw_chk_ptr) { 371#ifdef IPFIREWALL_FORWARD 372 /* 373 * If we've been forwarded from the output side, then 374 * skip the firewall a second time 375 */ 376 if (ip_fw_fwd_addr) 377 goto ours; 378#endif /* IPFIREWALL_FORWARD */ 379 i = (*ip_fw_chk_ptr)(&ip, hlen, NULL, &ip_divert_cookie, 380 &m, &rule, &ip_fw_fwd_addr); 381 /* 382 * see the comment in ip_output for the return values 383 * produced by the firewall. 384 */ 385 if (!m) /* packet discarded by firewall */ 386 return ; 387 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */ 388 goto pass ; 389#ifdef DUMMYNET 390 if (i & 0x10000) { 391 /* send packet to the appropriate pipe */ 392 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule); 393 return ; 394 } 395#endif 396#ifdef IPDIVERT 397 if (i > 0 && i < 0x10000) { 398 /* Divert packet */ 399 frag_divert_port = i & 0xffff ; 400 goto ours; 401 } 402#endif 403#ifdef IPFIREWALL_FORWARD 404 if (i == 0 && ip_fw_fwd_addr != NULL) 405 goto pass ; 406#endif 407 /* 408 * if we get here, the packet must be dropped 409 */ 410 m_freem(m); 411 return; 412 } 413pass: 414 415 /* 416 * Process options and, if not destined for us, 417 * ship it on. ip_dooptions returns 1 when an 418 * error was detected (causing an icmp message 419 * to be sent and the original packet to be freed). 420 */ 421 ip_nhops = 0; /* for source routed packets */ 422 if (hlen > sizeof (struct ip) && ip_dooptions(m)) { 423#ifdef IPFIREWALL_FORWARD 424 ip_fw_fwd_addr = NULL; 425#endif 426 return; 427 } 428 429 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 430 * matter if it is destined to another node, or whether it is 431 * a multicast one, RSVP wants it! and prevents it from being forwarded 432 * anywhere else. Also checks if the rsvp daemon is running before 433 * grabbing the packet. 434 */ 435 if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 436 goto ours; 437 438 /* 439 * Check our list of addresses, to see if the packet is for us. 440 * If we don't have any addresses, assume any unicast packet 441 * we receive might be for us (and let the upper layers deal 442 * with it). 443 */ 444 if (TAILQ_EMPTY(&in_ifaddrhead) && 445 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 446 goto ours; 447 448 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia; 449 ia = TAILQ_NEXT(ia, ia_link)) { 450#define satosin(sa) ((struct sockaddr_in *)(sa)) 451 452#ifdef BOOTP_COMPAT 453 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 454 goto ours; 455#endif 456#ifdef IPFIREWALL_FORWARD 457 /* 458 * If the addr to forward to is one of ours, we pretend to 459 * be the destination for this packet. 460 */ 461 if (ip_fw_fwd_addr == NULL) { 462 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 463 goto ours; 464 } else if (IA_SIN(ia)->sin_addr.s_addr == 465 ip_fw_fwd_addr->sin_addr.s_addr) 466 goto ours; 467#else 468 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 469 goto ours; 470#endif 471 if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) { 472 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 473 ip->ip_dst.s_addr) 474 goto ours; 475 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 476 goto ours; 477 } 478 } 479 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 480 struct in_multi *inm; 481 if (ip_mrouter) { 482 /* 483 * If we are acting as a multicast router, all 484 * incoming multicast packets are passed to the 485 * kernel-level multicast forwarding function. 486 * The packet is returned (relatively) intact; if 487 * ip_mforward() returns a non-zero value, the packet 488 * must be discarded, else it may be accepted below. 489 * 490 * (The IP ident field is put in the same byte order 491 * as expected when ip_mforward() is called from 492 * ip_output().) 493 */ 494 ip->ip_id = htons(ip->ip_id); 495 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 496 ipstat.ips_cantforward++; 497 m_freem(m); 498 return; 499 } 500 ip->ip_id = ntohs(ip->ip_id); 501 502 /* 503 * The process-level routing demon needs to receive 504 * all multicast IGMP packets, whether or not this 505 * host belongs to their destination groups. 506 */ 507 if (ip->ip_p == IPPROTO_IGMP) 508 goto ours; 509 ipstat.ips_forward++; 510 } 511 /* 512 * See if we belong to the destination multicast group on the 513 * arrival interface. 514 */ 515 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 516 if (inm == NULL) { 517 ipstat.ips_notmember++; 518 m_freem(m); 519 return; 520 } 521 goto ours; 522 } 523 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 524 goto ours; 525 if (ip->ip_dst.s_addr == INADDR_ANY) 526 goto ours; 527 528 /* 529 * Not for us; forward if possible and desirable. 530 */ 531 if (ipforwarding == 0) { 532 ipstat.ips_cantforward++; 533 m_freem(m); 534 } else 535 ip_forward(m, 0); 536#ifdef IPFIREWALL_FORWARD 537 ip_fw_fwd_addr = NULL; 538#endif 539 return; 540 541ours: 542 543 /* 544 * If offset or IP_MF are set, must reassemble. 545 * Otherwise, nothing need be done. 546 * (We could look in the reassembly queue to see 547 * if the packet was previously fragmented, 548 * but it's not worth the time; just let them time out.) 549 */ 550 if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) { 551 if (m->m_flags & M_EXT) { /* XXX */ 552 if ((m = m_pullup(m, hlen)) == 0) { 553 ipstat.ips_toosmall++; 554#ifdef IPDIVERT 555 frag_divert_port = 0; 556 ip_divert_cookie = 0; 557#endif 558#ifdef IPFIREWALL_FORWARD 559 ip_fw_fwd_addr = NULL; 560#endif 561 return; 562 } 563 ip = mtod(m, struct ip *); 564 } 565 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 566 /* 567 * Look for queue of fragments 568 * of this datagram. 569 */ 570 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next) 571 if (ip->ip_id == fp->ipq_id && 572 ip->ip_src.s_addr == fp->ipq_src.s_addr && 573 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 574 ip->ip_p == fp->ipq_p) 575 goto found; 576 577 fp = 0; 578 579 /* check if there's a place for the new queue */ 580 if (nipq > maxnipq) { 581 /* 582 * drop something from the tail of the current queue 583 * before proceeding further 584 */ 585 if (ipq[sum].prev == &ipq[sum]) { /* gak */ 586 for (i = 0; i < IPREASS_NHASH; i++) { 587 if (ipq[i].prev != &ipq[i]) { 588 ip_freef(ipq[i].prev); 589 break; 590 } 591 } 592 } else 593 ip_freef(ipq[sum].prev); 594 } 595found: 596 /* 597 * Adjust ip_len to not reflect header, 598 * set ip_mff if more fragments are expected, 599 * convert offset of this to bytes. 600 */ 601 ip->ip_len -= hlen; 602 mff = (ip->ip_off & IP_MF) != 0; 603 if (mff) { 604 /* 605 * Make sure that fragments have a data length 606 * that's a non-zero multiple of 8 bytes. 607 */ 608 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 609 ipstat.ips_toosmall++; /* XXX */ 610 goto bad; 611 } 612 m->m_flags |= M_FRAG; 613 } 614 ip->ip_off <<= 3; 615 616 /* 617 * If datagram marked as having more fragments 618 * or if this is not the first fragment, 619 * attempt reassembly; if it succeeds, proceed. 620 */ 621 if (mff || ip->ip_off) { 622 ipstat.ips_fragments++; 623 m->m_pkthdr.header = ip; 624 ip = ip_reass(m, fp, &ipq[sum]); 625 if (ip == 0) { 626#ifdef IPFIREWALL_FORWARD 627 ip_fw_fwd_addr = NULL; 628#endif 629 return; 630 } 631 /* Get the length of the reassembled packets header */ 632 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 633 ipstat.ips_reassembled++; 634 m = dtom(ip); 635#ifdef IPDIVERT 636 if (frag_divert_port) { 637 ip->ip_len += hlen; 638 HTONS(ip->ip_len); 639 HTONS(ip->ip_off); 640 HTONS(ip->ip_id); 641 ip->ip_sum = 0; 642 ip->ip_sum = in_cksum_hdr(ip); 643 NTOHS(ip->ip_id); 644 NTOHS(ip->ip_off); 645 NTOHS(ip->ip_len); 646 ip->ip_len -= hlen; 647 } 648#endif 649 } else 650 if (fp) 651 ip_freef(fp); 652 } else 653 ip->ip_len -= hlen; 654 655#ifdef IPDIVERT 656 /* 657 * Divert reassembled packets to the divert protocol if required 658 * If divert port is null then cookie should be too, 659 * so we shouldn't need to clear them here. Assume ip_divert does so. 660 */ 661 if (frag_divert_port) { 662 ipstat.ips_delivered++; 663 ip_divert_port = frag_divert_port; 664 frag_divert_port = 0; 665 (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, hlen); 666 return; 667 } 668 669 /* Don't let packets divert themselves */ 670 if (ip->ip_p == IPPROTO_DIVERT) { 671 ipstat.ips_noproto++; 672 goto bad; 673 } 674 675#endif 676 677 /* 678 * Switch out to protocol's input routine. 679 */ 680 ipstat.ips_delivered++; 681 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 682#ifdef IPFIREWALL_FORWARD 683 ip_fw_fwd_addr = NULL; /* tcp needed it */ 684#endif 685 return; 686bad: 687#ifdef IPFIREWALL_FORWARD 688 ip_fw_fwd_addr = NULL; 689#endif 690 m_freem(m); 691} 692 693/* 694 * IP software interrupt routine - to go away sometime soon 695 */ 696static void 697ipintr(void) 698{ 699 int s; 700 struct mbuf *m; 701 702 while(1) { 703 s = splimp(); 704 IF_DEQUEUE(&ipintrq, m); 705 splx(s); 706 if (m == 0) 707 return; 708 ip_input(m); 709 } 710} 711 712NETISR_SET(NETISR_IP, ipintr); 713 714/* 715 * Take incoming datagram fragment and try to 716 * reassemble it into whole datagram. If a chain for 717 * reassembly of this datagram already exists, then it 718 * is given as fp; otherwise have to make a chain. 719 */ 720static struct ip * 721ip_reass(m, fp, where) 722 register struct mbuf *m; 723 register struct ipq *fp; 724 struct ipq *where; 725{ 726 struct ip *ip = mtod(m, struct ip *); 727 register struct mbuf *p = 0, *q, *nq; 728 struct mbuf *t; 729 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 730 int i, next; 731 732 /* 733 * Presence of header sizes in mbufs 734 * would confuse code below. 735 */ 736 m->m_data += hlen; 737 m->m_len -= hlen; 738 739 /* 740 * If first fragment to arrive, create a reassembly queue. 741 */ 742 if (fp == 0) { 743 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 744 goto dropfrag; 745 fp = mtod(t, struct ipq *); 746 insque(fp, where); 747 nipq++; 748 fp->ipq_ttl = IPFRAGTTL; 749 fp->ipq_p = ip->ip_p; 750 fp->ipq_id = ip->ip_id; 751 fp->ipq_src = ip->ip_src; 752 fp->ipq_dst = ip->ip_dst; 753 fp->ipq_frags = m; 754 m->m_nextpkt = NULL; 755#ifdef IPDIVERT 756 fp->ipq_divert = 0; 757 fp->ipq_div_cookie = 0; 758#endif 759 goto inserted; 760 } 761 762#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 763 764 /* 765 * Find a segment which begins after this one does. 766 */ 767 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 768 if (GETIP(q)->ip_off > ip->ip_off) 769 break; 770 771 /* 772 * If there is a preceding segment, it may provide some of 773 * our data already. If so, drop the data from the incoming 774 * segment. If it provides all of our data, drop us, otherwise 775 * stick new segment in the proper place. 776 */ 777 if (p) { 778 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 779 if (i > 0) { 780 if (i >= ip->ip_len) 781 goto dropfrag; 782 m_adj(dtom(ip), i); 783 ip->ip_off += i; 784 ip->ip_len -= i; 785 } 786 m->m_nextpkt = p->m_nextpkt; 787 p->m_nextpkt = m; 788 } else { 789 m->m_nextpkt = fp->ipq_frags; 790 fp->ipq_frags = m; 791 } 792 793 /* 794 * While we overlap succeeding segments trim them or, 795 * if they are completely covered, dequeue them. 796 */ 797 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 798 q = nq) { 799 i = (ip->ip_off + ip->ip_len) - 800 GETIP(q)->ip_off; 801 if (i < GETIP(q)->ip_len) { 802 GETIP(q)->ip_len -= i; 803 GETIP(q)->ip_off += i; 804 m_adj(q, i); 805 break; 806 } 807 nq = q->m_nextpkt; 808 m->m_nextpkt = nq; 809 m_freem(q); 810 } 811 812inserted: 813 814#ifdef IPDIVERT 815 /* 816 * Any fragment diverting causes the whole packet to divert 817 */ 818 if (frag_divert_port) { 819 fp->ipq_divert = frag_divert_port; 820 fp->ipq_div_cookie = ip_divert_cookie; 821 } 822 frag_divert_port = 0; 823 ip_divert_cookie = 0; 824#endif 825 826 /* 827 * Check for complete reassembly. 828 */ 829 next = 0; 830 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 831 if (GETIP(q)->ip_off != next) 832 return (0); 833 next += GETIP(q)->ip_len; 834 } 835 /* Make sure the last packet didn't have the IP_MF flag */ 836 if (p->m_flags & M_FRAG) 837 return (0); 838 839 /* 840 * Reassembly is complete. Make sure the packet is a sane size. 841 */ 842 q = fp->ipq_frags; 843 ip = GETIP(q); 844 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { 845 ipstat.ips_toolong++; 846 ip_freef(fp); 847 return (0); 848 } 849 850 /* 851 * Concatenate fragments. 852 */ 853 m = q; 854 t = m->m_next; 855 m->m_next = 0; 856 m_cat(m, t); 857 nq = q->m_nextpkt; 858 q->m_nextpkt = 0; 859 for (q = nq; q != NULL; q = nq) { 860 nq = q->m_nextpkt; 861 q->m_nextpkt = NULL; 862 m_cat(m, q); 863 } 864 865#ifdef IPDIVERT 866 /* 867 * extract divert port for packet, if any 868 */ 869 frag_divert_port = fp->ipq_divert; 870 ip_divert_cookie = fp->ipq_div_cookie; 871#endif 872 873 /* 874 * Create header for new ip packet by 875 * modifying header of first packet; 876 * dequeue and discard fragment reassembly header. 877 * Make header visible. 878 */ 879 ip->ip_len = next; 880 ip->ip_src = fp->ipq_src; 881 ip->ip_dst = fp->ipq_dst; 882 remque(fp); 883 nipq--; 884 (void) m_free(dtom(fp)); 885 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); 886 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); 887 /* some debugging cruft by sklower, below, will go away soon */ 888 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 889 register int plen = 0; 890 for (t = m; m; m = m->m_next) 891 plen += m->m_len; 892 t->m_pkthdr.len = plen; 893 } 894 return (ip); 895 896dropfrag: 897#ifdef IPDIVERT 898 frag_divert_port = 0; 899 ip_divert_cookie = 0; 900#endif 901 ipstat.ips_fragdropped++; 902 m_freem(m); 903 return (0); 904 905#undef GETIP 906} 907 908/* 909 * Free a fragment reassembly header and all 910 * associated datagrams. 911 */ 912static void 913ip_freef(fp) 914 struct ipq *fp; 915{ 916 register struct mbuf *q; 917 918 while (fp->ipq_frags) { 919 q = fp->ipq_frags; 920 fp->ipq_frags = q->m_nextpkt; 921 m_freem(q); 922 } 923 remque(fp); 924 (void) m_free(dtom(fp)); 925 nipq--; 926} 927 928/* 929 * IP timer processing; 930 * if a timer expires on a reassembly 931 * queue, discard it. 932 */ 933void 934ip_slowtimo() 935{ 936 register struct ipq *fp; 937 int s = splnet(); 938 int i; 939 940 for (i = 0; i < IPREASS_NHASH; i++) { 941 fp = ipq[i].next; 942 if (fp == 0) 943 continue; 944 while (fp != &ipq[i]) { 945 --fp->ipq_ttl; 946 fp = fp->next; 947 if (fp->prev->ipq_ttl == 0) { 948 ipstat.ips_fragtimeout++; 949 ip_freef(fp->prev); 950 } 951 } 952 } 953 ipflow_slowtimo(); 954 splx(s); 955} 956 957/* 958 * Drain off all datagram fragments. 959 */ 960void 961ip_drain() 962{ 963 int i; 964 965 for (i = 0; i < IPREASS_NHASH; i++) { 966 while (ipq[i].next != &ipq[i]) { 967 ipstat.ips_fragdropped++; 968 ip_freef(ipq[i].next); 969 } 970 } 971 in_rtqdrain(); 972} 973 974/* 975 * Do option processing on a datagram, 976 * possibly discarding it if bad options are encountered, 977 * or forwarding it if source-routed. 978 * Returns 1 if packet has been forwarded/freed, 979 * 0 if the packet should be processed further. 980 */ 981static int 982ip_dooptions(m) 983 struct mbuf *m; 984{ 985 register struct ip *ip = mtod(m, struct ip *); 986 register u_char *cp; 987 register struct ip_timestamp *ipt; 988 register struct in_ifaddr *ia; 989 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 990 struct in_addr *sin, dst; 991 n_time ntime; 992 993 dst = ip->ip_dst; 994 cp = (u_char *)(ip + 1); 995 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 996 for (; cnt > 0; cnt -= optlen, cp += optlen) { 997 opt = cp[IPOPT_OPTVAL]; 998 if (opt == IPOPT_EOL) 999 break; 1000 if (opt == IPOPT_NOP) 1001 optlen = 1; 1002 else { 1003 optlen = cp[IPOPT_OLEN]; 1004 if (optlen <= 0 || optlen > cnt) { 1005 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1006 goto bad; 1007 } 1008 } 1009 switch (opt) { 1010 1011 default: 1012 break; 1013 1014 /* 1015 * Source routing with record. 1016 * Find interface with current destination address. 1017 * If none on this machine then drop if strictly routed, 1018 * or do nothing if loosely routed. 1019 * Record interface address and bring up next address 1020 * component. If strictly routed make sure next 1021 * address is on directly accessible net. 1022 */ 1023 case IPOPT_LSRR: 1024 case IPOPT_SSRR: 1025 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1026 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1027 goto bad; 1028 } 1029 ipaddr.sin_addr = ip->ip_dst; 1030 ia = (struct in_ifaddr *) 1031 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1032 if (ia == 0) { 1033 if (opt == IPOPT_SSRR) { 1034 type = ICMP_UNREACH; 1035 code = ICMP_UNREACH_SRCFAIL; 1036 goto bad; 1037 } 1038 if (!ip_dosourceroute) 1039 goto nosourcerouting; 1040 /* 1041 * Loose routing, and not at next destination 1042 * yet; nothing to do except forward. 1043 */ 1044 break; 1045 } 1046 off--; /* 0 origin */ 1047 if (off > optlen - sizeof(struct in_addr)) { 1048 /* 1049 * End of source route. Should be for us. 1050 */ 1051 if (!ip_acceptsourceroute) 1052 goto nosourcerouting; 1053 save_rte(cp, ip->ip_src); 1054 break; 1055 } 1056 1057 if (!ip_dosourceroute) { 1058 if (ipforwarding) { 1059 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1060 /* 1061 * Acting as a router, so generate ICMP 1062 */ 1063nosourcerouting: 1064 strcpy(buf, inet_ntoa(ip->ip_dst)); 1065 log(LOG_WARNING, 1066 "attempted source route from %s to %s\n", 1067 inet_ntoa(ip->ip_src), buf); 1068 type = ICMP_UNREACH; 1069 code = ICMP_UNREACH_SRCFAIL; 1070 goto bad; 1071 } else { 1072 /* 1073 * Not acting as a router, so silently drop. 1074 */ 1075 ipstat.ips_cantforward++; 1076 m_freem(m); 1077 return (1); 1078 } 1079 } 1080 1081 /* 1082 * locate outgoing interface 1083 */ 1084 (void)memcpy(&ipaddr.sin_addr, cp + off, 1085 sizeof(ipaddr.sin_addr)); 1086 1087 if (opt == IPOPT_SSRR) { 1088#define INA struct in_ifaddr * 1089#define SA struct sockaddr * 1090 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1091 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1092 } else 1093 ia = ip_rtaddr(ipaddr.sin_addr); 1094 if (ia == 0) { 1095 type = ICMP_UNREACH; 1096 code = ICMP_UNREACH_SRCFAIL; 1097 goto bad; 1098 } 1099 ip->ip_dst = ipaddr.sin_addr; 1100 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1101 sizeof(struct in_addr)); 1102 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1103 /* 1104 * Let ip_intr's mcast routing check handle mcast pkts 1105 */ 1106 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1107 break; 1108 1109 case IPOPT_RR: 1110 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1111 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1112 goto bad; 1113 } 1114 /* 1115 * If no space remains, ignore. 1116 */ 1117 off--; /* 0 origin */ 1118 if (off > optlen - sizeof(struct in_addr)) 1119 break; 1120 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1121 sizeof(ipaddr.sin_addr)); 1122 /* 1123 * locate outgoing interface; if we're the destination, 1124 * use the incoming interface (should be same). 1125 */ 1126 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1127 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1128 type = ICMP_UNREACH; 1129 code = ICMP_UNREACH_HOST; 1130 goto bad; 1131 } 1132 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1133 sizeof(struct in_addr)); 1134 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1135 break; 1136 1137 case IPOPT_TS: 1138 code = cp - (u_char *)ip; 1139 ipt = (struct ip_timestamp *)cp; 1140 if (ipt->ipt_len < 5) 1141 goto bad; 1142 if (ipt->ipt_ptr > ipt->ipt_len - sizeof(int32_t)) { 1143 if (++ipt->ipt_oflw == 0) 1144 goto bad; 1145 break; 1146 } 1147 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 1148 switch (ipt->ipt_flg) { 1149 1150 case IPOPT_TS_TSONLY: 1151 break; 1152 1153 case IPOPT_TS_TSANDADDR: 1154 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1155 sizeof(struct in_addr) > ipt->ipt_len) 1156 goto bad; 1157 ipaddr.sin_addr = dst; 1158 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1159 m->m_pkthdr.rcvif); 1160 if (ia == 0) 1161 continue; 1162 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1163 sizeof(struct in_addr)); 1164 ipt->ipt_ptr += sizeof(struct in_addr); 1165 break; 1166 1167 case IPOPT_TS_PRESPEC: 1168 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1169 sizeof(struct in_addr) > ipt->ipt_len) 1170 goto bad; 1171 (void)memcpy(&ipaddr.sin_addr, sin, 1172 sizeof(struct in_addr)); 1173 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1174 continue; 1175 ipt->ipt_ptr += sizeof(struct in_addr); 1176 break; 1177 1178 default: 1179 goto bad; 1180 } 1181 ntime = iptime(); 1182 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, 1183 sizeof(n_time)); 1184 ipt->ipt_ptr += sizeof(n_time); 1185 } 1186 } 1187 if (forward && ipforwarding) { 1188 ip_forward(m, 1); 1189 return (1); 1190 } 1191 return (0); 1192bad: 1193 ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */ 1194 icmp_error(m, type, code, 0, 0); 1195 ipstat.ips_badoptions++; 1196 return (1); 1197} 1198 1199/* 1200 * Given address of next destination (final or next hop), 1201 * return internet address info of interface to be used to get there. 1202 */ 1203static struct in_ifaddr * 1204ip_rtaddr(dst) 1205 struct in_addr dst; 1206{ 1207 register struct sockaddr_in *sin; 1208 1209 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 1210 1211 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 1212 if (ipforward_rt.ro_rt) { 1213 RTFREE(ipforward_rt.ro_rt); 1214 ipforward_rt.ro_rt = 0; 1215 } 1216 sin->sin_family = AF_INET; 1217 sin->sin_len = sizeof(*sin); 1218 sin->sin_addr = dst; 1219 1220 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1221 } 1222 if (ipforward_rt.ro_rt == 0) 1223 return ((struct in_ifaddr *)0); 1224 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 1225} 1226 1227/* 1228 * Save incoming source route for use in replies, 1229 * to be picked up later by ip_srcroute if the receiver is interested. 1230 */ 1231void 1232save_rte(option, dst) 1233 u_char *option; 1234 struct in_addr dst; 1235{ 1236 unsigned olen; 1237 1238 olen = option[IPOPT_OLEN]; 1239#ifdef DIAGNOSTIC 1240 if (ipprintfs) 1241 printf("save_rte: olen %d\n", olen); 1242#endif 1243 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1244 return; 1245 bcopy(option, ip_srcrt.srcopt, olen); 1246 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1247 ip_srcrt.dst = dst; 1248} 1249 1250/* 1251 * Retrieve incoming source route for use in replies, 1252 * in the same form used by setsockopt. 1253 * The first hop is placed before the options, will be removed later. 1254 */ 1255struct mbuf * 1256ip_srcroute() 1257{ 1258 register struct in_addr *p, *q; 1259 register struct mbuf *m; 1260 1261 if (ip_nhops == 0) 1262 return ((struct mbuf *)0); 1263 m = m_get(M_DONTWAIT, MT_HEADER); 1264 if (m == 0) 1265 return ((struct mbuf *)0); 1266 1267#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1268 1269 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1270 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1271 OPTSIZ; 1272#ifdef DIAGNOSTIC 1273 if (ipprintfs) 1274 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1275#endif 1276 1277 /* 1278 * First save first hop for return route 1279 */ 1280 p = &ip_srcrt.route[ip_nhops - 1]; 1281 *(mtod(m, struct in_addr *)) = *p--; 1282#ifdef DIAGNOSTIC 1283 if (ipprintfs) 1284 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1285#endif 1286 1287 /* 1288 * Copy option fields and padding (nop) to mbuf. 1289 */ 1290 ip_srcrt.nop = IPOPT_NOP; 1291 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1292 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1293 &ip_srcrt.nop, OPTSIZ); 1294 q = (struct in_addr *)(mtod(m, caddr_t) + 1295 sizeof(struct in_addr) + OPTSIZ); 1296#undef OPTSIZ 1297 /* 1298 * Record return path as an IP source route, 1299 * reversing the path (pointers are now aligned). 1300 */ 1301 while (p >= ip_srcrt.route) { 1302#ifdef DIAGNOSTIC 1303 if (ipprintfs) 1304 printf(" %lx", (u_long)ntohl(q->s_addr)); 1305#endif 1306 *q++ = *p--; 1307 } 1308 /* 1309 * Last hop goes to final destination. 1310 */ 1311 *q = ip_srcrt.dst; 1312#ifdef DIAGNOSTIC 1313 if (ipprintfs) 1314 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1315#endif 1316 return (m); 1317} 1318 1319/* 1320 * Strip out IP options, at higher 1321 * level protocol in the kernel. 1322 * Second argument is buffer to which options 1323 * will be moved, and return value is their length. 1324 * XXX should be deleted; last arg currently ignored. 1325 */ 1326void 1327ip_stripoptions(m, mopt) 1328 register struct mbuf *m; 1329 struct mbuf *mopt; 1330{ 1331 register int i; 1332 struct ip *ip = mtod(m, struct ip *); 1333 register caddr_t opts; 1334 int olen; 1335 1336 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1337 opts = (caddr_t)(ip + 1); 1338 i = m->m_len - (sizeof (struct ip) + olen); 1339 bcopy(opts + olen, opts, (unsigned)i); 1340 m->m_len -= olen; 1341 if (m->m_flags & M_PKTHDR) 1342 m->m_pkthdr.len -= olen; 1343 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1344} 1345 1346u_char inetctlerrmap[PRC_NCMDS] = { 1347 0, 0, 0, 0, 1348 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1349 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1350 EMSGSIZE, EHOSTUNREACH, 0, 0, 1351 0, 0, 0, 0, 1352 ENOPROTOOPT 1353}; 1354 1355/* 1356 * Forward a packet. If some error occurs return the sender 1357 * an icmp packet. Note we can't always generate a meaningful 1358 * icmp message because icmp doesn't have a large enough repertoire 1359 * of codes and types. 1360 * 1361 * If not forwarding, just drop the packet. This could be confusing 1362 * if ipforwarding was zero but some routing protocol was advancing 1363 * us as a gateway to somewhere. However, we must let the routing 1364 * protocol deal with that. 1365 * 1366 * The srcrt parameter indicates whether the packet is being forwarded 1367 * via a source route. 1368 */ 1369static void 1370ip_forward(m, srcrt) 1371 struct mbuf *m; 1372 int srcrt; 1373{ 1374 register struct ip *ip = mtod(m, struct ip *); 1375 register struct sockaddr_in *sin; 1376 register struct rtentry *rt; 1377 int error, type = 0, code = 0; 1378 struct mbuf *mcopy; 1379 n_long dest; 1380 struct ifnet *destifp; 1381 1382 dest = 0; 1383#ifdef DIAGNOSTIC 1384 if (ipprintfs) 1385 printf("forward: src %lx dst %lx ttl %x\n", 1386 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr, 1387 ip->ip_ttl); 1388#endif 1389 1390 1391 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1392 ipstat.ips_cantforward++; 1393 m_freem(m); 1394 return; 1395 } 1396 HTONS(ip->ip_id); 1397#ifdef IPSTEALTH 1398 if (!ipstealth) { 1399#endif 1400 if (ip->ip_ttl <= IPTTLDEC) { 1401 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1402 dest, 0); 1403 return; 1404 } 1405 ip->ip_ttl -= IPTTLDEC; 1406#ifdef IPSTEALTH 1407 } 1408#endif 1409 1410 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 1411 if ((rt = ipforward_rt.ro_rt) == 0 || 1412 ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 1413 if (ipforward_rt.ro_rt) { 1414 RTFREE(ipforward_rt.ro_rt); 1415 ipforward_rt.ro_rt = 0; 1416 } 1417 sin->sin_family = AF_INET; 1418 sin->sin_len = sizeof(*sin); 1419 sin->sin_addr = ip->ip_dst; 1420 1421 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1422 if (ipforward_rt.ro_rt == 0) { 1423 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1424 return; 1425 } 1426 rt = ipforward_rt.ro_rt; 1427 } 1428 1429 /* 1430 * Save at most 64 bytes of the packet in case 1431 * we need to generate an ICMP message to the src. 1432 */ 1433 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 1434 1435 /* 1436 * If forwarding packet using same interface that it came in on, 1437 * perhaps should send a redirect to sender to shortcut a hop. 1438 * Only send redirect if source is sending directly to us, 1439 * and if packet was not source routed (or has any options). 1440 * Also, don't send redirect if forwarding using a default route 1441 * or a route modified by a redirect. 1442 */ 1443#define satosin(sa) ((struct sockaddr_in *)(sa)) 1444 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1445 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1446 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1447 ipsendredirects && !srcrt) { 1448#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1449 u_long src = ntohl(ip->ip_src.s_addr); 1450 1451 if (RTA(rt) && 1452 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1453 if (rt->rt_flags & RTF_GATEWAY) 1454 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1455 else 1456 dest = ip->ip_dst.s_addr; 1457 /* Router requirements says to only send host redirects */ 1458 type = ICMP_REDIRECT; 1459 code = ICMP_REDIRECT_HOST; 1460#ifdef DIAGNOSTIC 1461 if (ipprintfs) 1462 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1463#endif 1464 } 1465 } 1466 1467 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1468 IP_FORWARDING, 0); 1469 if (error) 1470 ipstat.ips_cantforward++; 1471 else { 1472 ipstat.ips_forward++; 1473 if (type) 1474 ipstat.ips_redirectsent++; 1475 else { 1476 if (mcopy) { 1477 ipflow_create(&ipforward_rt, mcopy); 1478 m_freem(mcopy); 1479 } 1480 return; 1481 } 1482 } 1483 if (mcopy == NULL) 1484 return; 1485 destifp = NULL; 1486 1487 switch (error) { 1488 1489 case 0: /* forwarded, but need redirect */ 1490 /* type, code set above */ 1491 break; 1492 1493 case ENETUNREACH: /* shouldn't happen, checked above */ 1494 case EHOSTUNREACH: 1495 case ENETDOWN: 1496 case EHOSTDOWN: 1497 default: 1498 type = ICMP_UNREACH; 1499 code = ICMP_UNREACH_HOST; 1500 break; 1501 1502 case EMSGSIZE: 1503 type = ICMP_UNREACH; 1504 code = ICMP_UNREACH_NEEDFRAG; 1505 if (ipforward_rt.ro_rt) 1506 destifp = ipforward_rt.ro_rt->rt_ifp; 1507 ipstat.ips_cantfrag++; 1508 break; 1509 1510 case ENOBUFS: 1511 type = ICMP_SOURCEQUENCH; 1512 code = 0; 1513 break; 1514 } 1515 icmp_error(mcopy, type, code, dest, destifp); 1516} 1517 1518void 1519ip_savecontrol(inp, mp, ip, m) 1520 register struct inpcb *inp; 1521 register struct mbuf **mp; 1522 register struct ip *ip; 1523 register struct mbuf *m; 1524{ 1525 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1526 struct timeval tv; 1527 1528 microtime(&tv); 1529 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1530 SCM_TIMESTAMP, SOL_SOCKET); 1531 if (*mp) 1532 mp = &(*mp)->m_next; 1533 } 1534 if (inp->inp_flags & INP_RECVDSTADDR) { 1535 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1536 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1537 if (*mp) 1538 mp = &(*mp)->m_next; 1539 } 1540#ifdef notyet 1541 /* XXX 1542 * Moving these out of udp_input() made them even more broken 1543 * than they already were. 1544 */ 1545 /* options were tossed already */ 1546 if (inp->inp_flags & INP_RECVOPTS) { 1547 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1548 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1549 if (*mp) 1550 mp = &(*mp)->m_next; 1551 } 1552 /* ip_srcroute doesn't do what we want here, need to fix */ 1553 if (inp->inp_flags & INP_RECVRETOPTS) { 1554 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1555 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1556 if (*mp) 1557 mp = &(*mp)->m_next; 1558 } 1559#endif 1560 if (inp->inp_flags & INP_RECVIF) { 1561 struct ifnet *ifp; 1562 struct sdlbuf { 1563 struct sockaddr_dl sdl; 1564 u_char pad[32]; 1565 } sdlbuf; 1566 struct sockaddr_dl *sdp; 1567 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1568 1569 if (((ifp = m->m_pkthdr.rcvif)) 1570 && ( ifp->if_index && (ifp->if_index <= if_index))) { 1571 sdp = (struct sockaddr_dl *)(ifnet_addrs 1572 [ifp->if_index - 1]->ifa_addr); 1573 /* 1574 * Change our mind and don't try copy. 1575 */ 1576 if ((sdp->sdl_family != AF_LINK) 1577 || (sdp->sdl_len > sizeof(sdlbuf))) { 1578 goto makedummy; 1579 } 1580 bcopy(sdp, sdl2, sdp->sdl_len); 1581 } else { 1582makedummy: 1583 sdl2->sdl_len 1584 = offsetof(struct sockaddr_dl, sdl_data[0]); 1585 sdl2->sdl_family = AF_LINK; 1586 sdl2->sdl_index = 0; 1587 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1588 } 1589 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 1590 IP_RECVIF, IPPROTO_IP); 1591 if (*mp) 1592 mp = &(*mp)->m_next; 1593 } 1594} 1595 1596int 1597ip_rsvp_init(struct socket *so) 1598{ 1599 if (so->so_type != SOCK_RAW || 1600 so->so_proto->pr_protocol != IPPROTO_RSVP) 1601 return EOPNOTSUPP; 1602 1603 if (ip_rsvpd != NULL) 1604 return EADDRINUSE; 1605 1606 ip_rsvpd = so; 1607 /* 1608 * This may seem silly, but we need to be sure we don't over-increment 1609 * the RSVP counter, in case something slips up. 1610 */ 1611 if (!ip_rsvp_on) { 1612 ip_rsvp_on = 1; 1613 rsvp_on++; 1614 } 1615 1616 return 0; 1617} 1618 1619int 1620ip_rsvp_done(void) 1621{ 1622 ip_rsvpd = NULL; 1623 /* 1624 * This may seem silly, but we need to be sure we don't over-decrement 1625 * the RSVP counter, in case something slips up. 1626 */ 1627 if (ip_rsvp_on) { 1628 ip_rsvp_on = 0; 1629 rsvp_on--; 1630 } 1631 return 0; 1632}
|