88#ifdef DUMMYNET 89#include <netinet/ip_dummynet.h> 90#endif 91 92int rsvp_on = 0; 93static int ip_rsvp_on; 94struct socket *ip_rsvpd; 95 96int ipforwarding = 0; 97SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 98 &ipforwarding, 0, "Enable IP forwarding between interfaces"); 99 100static int ipsendredirects = 1; /* XXX */ 101SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 102 &ipsendredirects, 0, "Enable sending IP redirects"); 103 104int ip_defttl = IPDEFTTL; 105SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 106 &ip_defttl, 0, "Maximum TTL on IP packets"); 107 108static int ip_dosourceroute = 0; 109SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 110 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); 111 112static int ip_acceptsourceroute = 0; 113SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 114 CTLFLAG_RW, &ip_acceptsourceroute, 0, 115 "Enable accepting source routed IP packets"); 116 117static int ip_keepfaith = 0; 118SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, 119 &ip_keepfaith, 0, 120 "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 121 122static int ip_nfragpackets = 0; 123static int ip_maxfragpackets; /* initialized in ip_init() */ 124SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW, 125 &ip_maxfragpackets, 0, 126 "Maximum number of IPv4 fragment reassembly queue entries"); 127 128/* 129 * XXX - Setting ip_checkinterface mostly implements the receive side of 130 * the Strong ES model described in RFC 1122, but since the routing table 131 * and transmit implementation do not implement the Strong ES model, 132 * setting this to 1 results in an odd hybrid. 133 * 134 * XXX - ip_checkinterface currently must be disabled if you use ipnat 135 * to translate the destination address to another local interface. 136 * 137 * XXX - ip_checkinterface must be disabled if you add IP aliases 138 * to the loopback interface instead of the interface where the 139 * packets for those addresses are received. 140 */ 141static int ip_checkinterface = 1; 142SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW, 143 &ip_checkinterface, 0, "Verify packet arrives on correct interface"); 144 145#ifdef DIAGNOSTIC 146static int ipprintfs = 0; 147#endif 148 149extern struct domain inetdomain; 150extern struct protosw inetsw[]; 151u_char ip_protox[IPPROTO_MAX]; 152static int ipqmaxlen = IFQ_MAXLEN; 153struct in_ifaddrhead in_ifaddrhead; /* first inet address */ 154SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW, 155 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue"); 156SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 157 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue"); 158 159struct ipstat ipstat; 160SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW, 161 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)"); 162 163/* Packet reassembly stuff */ 164#define IPREASS_NHASH_LOG2 6 165#define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) 166#define IPREASS_HMASK (IPREASS_NHASH - 1) 167#define IPREASS_HASH(x,y) \ 168 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) 169 170static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH]; 171static int nipq = 0; /* total # of reass queues */ 172static int maxnipq; 173const int ipintrq_present = 1; 174 175#ifdef IPCTL_DEFMTU 176SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 177 &ip_mtu, 0, "Default MTU"); 178#endif 179 180#ifdef IPSTEALTH 181static int ipstealth = 0; 182SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 183 &ipstealth, 0, ""); 184#endif 185 186 187/* Firewall hooks */ 188ip_fw_chk_t *ip_fw_chk_ptr; 189ip_fw_ctl_t *ip_fw_ctl_ptr; 190int fw_enable = 1 ; 191 192#ifdef DUMMYNET 193ip_dn_ctl_t *ip_dn_ctl_ptr; 194#endif 195 196 197/* 198 * We need to save the IP options in case a protocol wants to respond 199 * to an incoming packet over the same route if the packet got here 200 * using IP source routing. This allows connection establishment and 201 * maintenance when the remote end is on a network that is not known 202 * to us. 203 */ 204static int ip_nhops = 0; 205static struct ip_srcrt { 206 struct in_addr dst; /* final destination */ 207 char nop; /* one NOP to align */ 208 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 209 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 210} ip_srcrt; 211 212struct sockaddr_in *ip_fw_fwd_addr; 213 214static void save_rte __P((u_char *, struct in_addr)); 215static int ip_dooptions __P((struct mbuf *)); 216static void ip_forward __P((struct mbuf *, int)); 217static void ip_freef __P((struct ipqhead *, struct ipq *)); 218#ifdef IPDIVERT 219static struct mbuf *ip_reass __P((struct mbuf *, struct ipqhead *, struct ipq *, u_int32_t *, u_int16_t *)); 220#else 221static struct mbuf *ip_reass __P((struct mbuf *, struct ipqhead *, struct ipq *)); 222#endif 223static struct in_ifaddr *ip_rtaddr __P((struct in_addr)); 224static void ipintr __P((void)); 225 226/* 227 * IP initialization: fill in IP protocol switch table. 228 * All protocols not implemented in kernel go to raw IP protocol handler. 229 */ 230void 231ip_init() 232{ 233 register struct protosw *pr; 234 register int i; 235 236 TAILQ_INIT(&in_ifaddrhead); 237 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 238 if (pr == 0) 239 panic("ip_init"); 240 for (i = 0; i < IPPROTO_MAX; i++) 241 ip_protox[i] = pr - inetsw; 242 for (pr = inetdomain.dom_protosw; 243 pr < inetdomain.dom_protoswNPROTOSW; pr++) 244 if (pr->pr_domain->dom_family == PF_INET && 245 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 246 ip_protox[pr->pr_protocol] = pr - inetsw; 247 248 for (i = 0; i < IPREASS_NHASH; i++) 249 TAILQ_INIT(&ipq[i]); 250 251 maxnipq = nmbclusters / 4; 252 ip_maxfragpackets = nmbclusters / 4; 253 254#ifndef RANDOM_IP_ID 255 ip_id = time_second & 0xffff; 256#endif 257 ipintrq.ifq_maxlen = ipqmaxlen; 258 mtx_init(&ipintrq.ifq_mtx, "ip_inq", MTX_DEF); 259 260 register_netisr(NETISR_IP, ipintr); 261} 262 263static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 264struct route ipforward_rt; 265 266/* 267 * Ip input routine. Checksum and byte swap header. If fragmented 268 * try to reassemble. Process options. Pass to next level. 269 */ 270void 271ip_input(struct mbuf *m) 272{ 273 struct ip *ip; 274 struct ipq *fp; 275 struct in_ifaddr *ia = NULL; 276 int i, hlen, checkif; 277 u_short sum; 278 u_int16_t divert_cookie; /* firewall cookie */ 279 struct in_addr pkt_dst; 280#ifdef IPDIVERT 281 u_int32_t divert_info = 0; /* packet divert/tee info */ 282#endif 283 struct ip_fw_chain *rule = NULL; 284#ifdef PFIL_HOOKS 285 struct packet_filter_hook *pfh; 286 struct mbuf *m0; 287 int rv; 288#endif /* PFIL_HOOKS */ 289 290#ifdef IPDIVERT 291 /* Get and reset firewall cookie */ 292 divert_cookie = ip_divert_cookie; 293 ip_divert_cookie = 0; 294#else 295 divert_cookie = 0; 296#endif 297 298#if defined(IPFIREWALL) && defined(DUMMYNET) 299 /* 300 * dummynet packet are prepended a vestigial mbuf with 301 * m_type = MT_DUMMYNET and m_data pointing to the matching 302 * rule. 303 */ 304 if (m->m_type == MT_DUMMYNET) { 305 rule = (struct ip_fw_chain *)(m->m_data) ; 306 m = m->m_next ; 307 ip = mtod(m, struct ip *); 308 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 309 goto iphack ; 310 } else 311 rule = NULL ; 312#endif 313 314#ifdef DIAGNOSTIC 315 if (m == NULL || (m->m_flags & M_PKTHDR) == 0) 316 panic("ip_input no HDR"); 317#endif 318 ipstat.ips_total++; 319 320 if (m->m_pkthdr.len < sizeof(struct ip)) 321 goto tooshort; 322 323 if (m->m_len < sizeof (struct ip) && 324 (m = m_pullup(m, sizeof (struct ip))) == 0) { 325 ipstat.ips_toosmall++; 326 return; 327 } 328 ip = mtod(m, struct ip *); 329 330 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { 331 ipstat.ips_badvers++; 332 goto bad; 333 } 334 335 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 336 if (hlen < sizeof(struct ip)) { /* minimum header length */ 337 ipstat.ips_badhlen++; 338 goto bad; 339 } 340 if (hlen > m->m_len) { 341 if ((m = m_pullup(m, hlen)) == 0) { 342 ipstat.ips_badhlen++; 343 return; 344 } 345 ip = mtod(m, struct ip *); 346 } 347 348 /* 127/8 must not appear on wire - RFC1122 */ 349 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 350 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 351 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { 352 ipstat.ips_badaddr++; 353 goto bad; 354 } 355 } 356 357 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { 358 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); 359 } else { 360 if (hlen == sizeof(struct ip)) { 361 sum = in_cksum_hdr(ip); 362 } else { 363 sum = in_cksum(m, hlen); 364 } 365 } 366 if (sum) { 367 ipstat.ips_badsum++; 368 goto bad; 369 } 370 371 /* 372 * Convert fields to host representation. 373 */ 374 NTOHS(ip->ip_len); 375 if (ip->ip_len < hlen) { 376 ipstat.ips_badlen++; 377 goto bad; 378 } 379 NTOHS(ip->ip_off); 380 381 /* 382 * Check that the amount of data in the buffers 383 * is as at least much as the IP header would have us expect. 384 * Trim mbufs if longer than we expect. 385 * Drop packet if shorter than we expect. 386 */ 387 if (m->m_pkthdr.len < ip->ip_len) { 388tooshort: 389 ipstat.ips_tooshort++; 390 goto bad; 391 } 392 if (m->m_pkthdr.len > ip->ip_len) { 393 if (m->m_len == m->m_pkthdr.len) { 394 m->m_len = ip->ip_len; 395 m->m_pkthdr.len = ip->ip_len; 396 } else 397 m_adj(m, ip->ip_len - m->m_pkthdr.len); 398 } 399 400#ifdef IPSEC 401 if (ipsec_gethist(m, NULL)) 402 goto pass; 403#endif 404 405 /* 406 * IpHack's section. 407 * Right now when no processing on packet has done 408 * and it is still fresh out of network we do our black 409 * deals with it. 410 * - Firewall: deny/allow/divert 411 * - Xlate: translate packet's addr/port (NAT). 412 * - Pipe: pass pkt through dummynet. 413 * - Wrap: fake packet's addr/port <unimpl.> 414 * - Encapsulate: put it in another IP and send out. <unimp.> 415 */ 416 417#if defined(IPFIREWALL) && defined(DUMMYNET) 418iphack: 419#endif 420 421#ifdef PFIL_HOOKS 422 /* 423 * Run through list of hooks for input packets. If there are any 424 * filters which require that additional packets in the flow are 425 * not fast-forwarded, they must clear the M_CANFASTFWD flag. 426 * Note that filters must _never_ set this flag, as another filter 427 * in the list may have previously cleared it. 428 */ 429 m0 = m; 430 pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh); 431 for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link)) 432 if (pfh->pfil_func) { 433 rv = pfh->pfil_func(ip, hlen, 434 m->m_pkthdr.rcvif, 0, &m0); 435 if (rv) 436 return; 437 m = m0; 438 if (m == NULL) 439 return; 440 ip = mtod(m, struct ip *); 441 } 442#endif /* PFIL_HOOKS */ 443 444 if (fw_enable && ip_fw_chk_ptr) { 445#ifdef IPFIREWALL_FORWARD 446 /* 447 * If we've been forwarded from the output side, then 448 * skip the firewall a second time 449 */ 450 if (ip_fw_fwd_addr) 451 goto ours; 452#endif /* IPFIREWALL_FORWARD */ 453 /* 454 * See the comment in ip_output for the return values 455 * produced by the firewall. 456 */ 457 i = (*ip_fw_chk_ptr)(&ip, 458 hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr); 459 if (i & IP_FW_PORT_DENY_FLAG) { /* XXX new interface-denied */ 460 if (m) 461 m_freem(m); 462 return ; 463 } 464 if (m == NULL) { /* Packet discarded by firewall */ 465 static int __debug=10; 466 if (__debug >0) { 467 printf("firewall returns NULL, please update!\n"); 468 __debug-- ; 469 } 470 return; 471 } 472 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */ 473 goto pass; 474#ifdef DUMMYNET 475 if ((i & IP_FW_PORT_DYNT_FLAG) != 0) { 476 /* Send packet to the appropriate pipe */ 477 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule, 478 0); 479 return; 480 } 481#endif 482#ifdef IPDIVERT 483 if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) { 484 /* Divert or tee packet */ 485 divert_info = i; 486 goto ours; 487 } 488#endif 489#ifdef IPFIREWALL_FORWARD 490 if (i == 0 && ip_fw_fwd_addr != NULL) 491 goto pass; 492#endif 493 /* 494 * if we get here, the packet must be dropped 495 */ 496 m_freem(m); 497 return; 498 } 499pass: 500 501 /* 502 * Process options and, if not destined for us, 503 * ship it on. ip_dooptions returns 1 when an 504 * error was detected (causing an icmp message 505 * to be sent and the original packet to be freed). 506 */ 507 ip_nhops = 0; /* for source routed packets */ 508 if (hlen > sizeof (struct ip) && ip_dooptions(m)) { 509#ifdef IPFIREWALL_FORWARD 510 ip_fw_fwd_addr = NULL; 511#endif 512 return; 513 } 514 515 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 516 * matter if it is destined to another node, or whether it is 517 * a multicast one, RSVP wants it! and prevents it from being forwarded 518 * anywhere else. Also checks if the rsvp daemon is running before 519 * grabbing the packet. 520 */ 521 if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 522 goto ours; 523 524 /* 525 * Check our list of addresses, to see if the packet is for us. 526 * If we don't have any addresses, assume any unicast packet 527 * we receive might be for us (and let the upper layers deal 528 * with it). 529 */ 530 if (TAILQ_EMPTY(&in_ifaddrhead) && 531 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 532 goto ours; 533 534 /* 535 * Cache the destination address of the packet; this may be 536 * changed by use of 'ipfw fwd'. 537 */ 538 pkt_dst = ip_fw_fwd_addr == NULL ? 539 ip->ip_dst : ip_fw_fwd_addr->sin_addr; 540 541 /* 542 * Enable a consistency check between the destination address 543 * and the arrival interface for a unicast packet (the RFC 1122 544 * strong ES model) if IP forwarding is disabled and the packet 545 * is not locally generated and the packet is not subject to 546 * 'ipfw fwd'. 547 * 548 * XXX - Checking also should be disabled if the destination 549 * address is ipnat'ed to a different interface. 550 * 551 * XXX - Checking is incompatible with IP aliases added 552 * to the loopback interface instead of the interface where 553 * the packets are received. 554 */ 555 checkif = ip_checkinterface && (ipforwarding == 0) &&
|
646 /* 647 * Not for us; forward if possible and desirable. 648 */ 649 if (ipforwarding == 0) { 650 ipstat.ips_cantforward++; 651 m_freem(m); 652 } else 653 ip_forward(m, 0); 654#ifdef IPFIREWALL_FORWARD 655 ip_fw_fwd_addr = NULL; 656#endif 657 return; 658 659ours: 660 /* Count the packet in the ip address stats */ 661 if (ia != NULL) { 662 ia->ia_ifa.if_ipackets++; 663 ia->ia_ifa.if_ibytes += m->m_pkthdr.len; 664 } 665 666 /* 667 * If offset or IP_MF are set, must reassemble. 668 * Otherwise, nothing need be done. 669 * (We could look in the reassembly queue to see 670 * if the packet was previously fragmented, 671 * but it's not worth the time; just let them time out.) 672 */ 673 if (ip->ip_off & (IP_MF | IP_OFFMASK)) { 674 675 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 676 /* 677 * Look for queue of fragments 678 * of this datagram. 679 */ 680 TAILQ_FOREACH(fp, &ipq[sum], ipq_list) 681 if (ip->ip_id == fp->ipq_id && 682 ip->ip_src.s_addr == fp->ipq_src.s_addr && 683 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 684 ip->ip_p == fp->ipq_p) 685 goto found; 686 687 fp = 0; 688 689 /* check if there's a place for the new queue */ 690 if (nipq > maxnipq) { 691 /* 692 * drop something from the tail of the current queue 693 * before proceeding further 694 */ 695 struct ipq *q = TAILQ_LAST(&ipq[sum], ipqhead); 696 if (q == NULL) { /* gak */ 697 for (i = 0; i < IPREASS_NHASH; i++) { 698 struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead); 699 if (r) { 700 ip_freef(&ipq[i], r); 701 break; 702 } 703 } 704 } else 705 ip_freef(&ipq[sum], q); 706 } 707found: 708 /* 709 * Adjust ip_len to not reflect header, 710 * convert offset of this to bytes. 711 */ 712 ip->ip_len -= hlen; 713 if (ip->ip_off & IP_MF) { 714 /* 715 * Make sure that fragments have a data length 716 * that's a non-zero multiple of 8 bytes. 717 */ 718 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 719 ipstat.ips_toosmall++; /* XXX */ 720 goto bad; 721 } 722 m->m_flags |= M_FRAG; 723 } 724 ip->ip_off <<= 3; 725 726 /* 727 * Attempt reassembly; if it succeeds, proceed. 728 */ 729 ipstat.ips_fragments++; 730 m->m_pkthdr.header = ip; 731#ifdef IPDIVERT 732 m = ip_reass(m, 733 &ipq[sum], fp, &divert_info, &divert_cookie); 734#else 735 m = ip_reass(m, &ipq[sum], fp); 736#endif 737 if (m == 0) { 738#ifdef IPFIREWALL_FORWARD 739 ip_fw_fwd_addr = NULL; 740#endif 741 return; 742 } 743 ipstat.ips_reassembled++; 744 ip = mtod(m, struct ip *); 745 /* Get the header length of the reassembled packet */ 746 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 747#ifdef IPDIVERT 748 /* Restore original checksum before diverting packet */ 749 if (divert_info != 0) { 750 ip->ip_len += hlen; 751 HTONS(ip->ip_len); 752 HTONS(ip->ip_off); 753 ip->ip_sum = 0; 754 if (hlen == sizeof(struct ip)) 755 ip->ip_sum = in_cksum_hdr(ip); 756 else 757 ip->ip_sum = in_cksum(m, hlen); 758 NTOHS(ip->ip_off); 759 NTOHS(ip->ip_len); 760 ip->ip_len -= hlen; 761 } 762#endif 763 } else 764 ip->ip_len -= hlen; 765 766#ifdef IPDIVERT 767 /* 768 * Divert or tee packet to the divert protocol if required. 769 * 770 * If divert_info is zero then cookie should be too, so we shouldn't 771 * need to clear them here. Assume divert_packet() does so also. 772 */ 773 if (divert_info != 0) { 774 struct mbuf *clone = NULL; 775 776 /* Clone packet if we're doing a 'tee' */ 777 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0) 778 clone = m_dup(m, M_DONTWAIT); 779 780 /* Restore packet header fields to original values */ 781 ip->ip_len += hlen; 782 HTONS(ip->ip_len); 783 HTONS(ip->ip_off); 784 785 /* Deliver packet to divert input routine */ 786 ip_divert_cookie = divert_cookie; 787 divert_packet(m, 1, divert_info & 0xffff); 788 ipstat.ips_delivered++; 789 790 /* If 'tee', continue with original packet */ 791 if (clone == NULL) 792 return; 793 m = clone; 794 ip = mtod(m, struct ip *); 795 } 796#endif 797 798#ifdef IPSEC 799 /* 800 * enforce IPsec policy checking if we are seeing last header. 801 * note that we do not visit this with protocols with pcb layer 802 * code - like udp/tcp/raw ip. 803 */ 804 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 && 805 ipsec4_in_reject(m, NULL)) { 806 ipsecstat.in_polvio++; 807 goto bad; 808 } 809#endif 810 811 /* 812 * Switch out to protocol's input routine. 813 */ 814 ipstat.ips_delivered++; 815 { 816 int off = hlen; 817 818 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off); 819#ifdef IPFIREWALL_FORWARD 820 ip_fw_fwd_addr = NULL; /* tcp needed it */ 821#endif 822 return; 823 } 824bad: 825#ifdef IPFIREWALL_FORWARD 826 ip_fw_fwd_addr = NULL; 827#endif 828 m_freem(m); 829} 830 831/* 832 * IP software interrupt routine - to go away sometime soon 833 */ 834static void 835ipintr(void) 836{ 837 struct mbuf *m; 838 839 while (1) { 840 IF_DEQUEUE(&ipintrq, m); 841 if (m == 0) 842 return; 843 ip_input(m); 844 } 845} 846 847/* 848 * Take incoming datagram fragment and try to reassemble it into 849 * whole datagram. If a chain for reassembly of this datagram already 850 * exists, then it is given as fp; otherwise have to make a chain. 851 * 852 * When IPDIVERT enabled, keep additional state with each packet that 853 * tells us if we need to divert or tee the packet we're building. 854 */ 855 856static struct mbuf * 857#ifdef IPDIVERT 858ip_reass(m, head, fp, divinfo, divcookie) 859#else 860ip_reass(m, head, fp) 861#endif 862 struct mbuf *m; 863 struct ipqhead *head; 864 struct ipq *fp; 865#ifdef IPDIVERT 866 u_int32_t *divinfo; 867 u_int16_t *divcookie; 868#endif 869{ 870 struct ip *ip = mtod(m, struct ip *); 871 register struct mbuf *p, *q, *nq; 872 struct mbuf *t; 873 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 874 int i, next; 875 876 /* 877 * Presence of header sizes in mbufs 878 * would confuse code below. 879 */ 880 m->m_data += hlen; 881 m->m_len -= hlen; 882 883 /* 884 * If first fragment to arrive, create a reassembly queue. 885 */ 886 if (fp == 0) { 887 /* 888 * Enforce upper bound on number of fragmented packets 889 * for which we attempt reassembly; 890 * If maxfrag is 0, never accept fragments. 891 * If maxfrag is -1, accept all fragments without limitation. 892 */ 893 if ((ip_maxfragpackets >= 0) && (ip_nfragpackets >= ip_maxfragpackets)) 894 goto dropfrag; 895 ip_nfragpackets++; 896 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 897 goto dropfrag; 898 fp = mtod(t, struct ipq *); 899 TAILQ_INSERT_HEAD(head, fp, ipq_list); 900 nipq++; 901 fp->ipq_ttl = IPFRAGTTL; 902 fp->ipq_p = ip->ip_p; 903 fp->ipq_id = ip->ip_id; 904 fp->ipq_src = ip->ip_src; 905 fp->ipq_dst = ip->ip_dst; 906 fp->ipq_frags = m; 907 m->m_nextpkt = NULL; 908#ifdef IPDIVERT 909 fp->ipq_div_info = 0; 910 fp->ipq_div_cookie = 0; 911#endif 912 goto inserted; 913 } 914 915#define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 916 917 /* 918 * Find a segment which begins after this one does. 919 */ 920 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 921 if (GETIP(q)->ip_off > ip->ip_off) 922 break; 923 924 /* 925 * If there is a preceding segment, it may provide some of 926 * our data already. If so, drop the data from the incoming 927 * segment. If it provides all of our data, drop us, otherwise 928 * stick new segment in the proper place. 929 * 930 * If some of the data is dropped from the the preceding 931 * segment, then it's checksum is invalidated. 932 */ 933 if (p) { 934 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 935 if (i > 0) { 936 if (i >= ip->ip_len) 937 goto dropfrag; 938 m_adj(m, i); 939 m->m_pkthdr.csum_flags = 0; 940 ip->ip_off += i; 941 ip->ip_len -= i; 942 } 943 m->m_nextpkt = p->m_nextpkt; 944 p->m_nextpkt = m; 945 } else { 946 m->m_nextpkt = fp->ipq_frags; 947 fp->ipq_frags = m; 948 } 949 950 /* 951 * While we overlap succeeding segments trim them or, 952 * if they are completely covered, dequeue them. 953 */ 954 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 955 q = nq) { 956 i = (ip->ip_off + ip->ip_len) - 957 GETIP(q)->ip_off; 958 if (i < GETIP(q)->ip_len) { 959 GETIP(q)->ip_len -= i; 960 GETIP(q)->ip_off += i; 961 m_adj(q, i); 962 q->m_pkthdr.csum_flags = 0; 963 break; 964 } 965 nq = q->m_nextpkt; 966 m->m_nextpkt = nq; 967 m_freem(q); 968 } 969 970inserted: 971 972#ifdef IPDIVERT 973 /* 974 * Transfer firewall instructions to the fragment structure. 975 * Any fragment diverting causes the whole packet to divert. 976 */ 977 fp->ipq_div_info = *divinfo; 978 fp->ipq_div_cookie = *divcookie; 979 *divinfo = 0; 980 *divcookie = 0; 981#endif 982 983 /* 984 * Check for complete reassembly. 985 */ 986 next = 0; 987 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 988 if (GETIP(q)->ip_off != next) 989 return (0); 990 next += GETIP(q)->ip_len; 991 } 992 /* Make sure the last packet didn't have the IP_MF flag */ 993 if (p->m_flags & M_FRAG) 994 return (0); 995 996 /* 997 * Reassembly is complete. Make sure the packet is a sane size. 998 */ 999 q = fp->ipq_frags; 1000 ip = GETIP(q); 1001 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { 1002 ipstat.ips_toolong++; 1003 ip_freef(head, fp); 1004 return (0); 1005 } 1006 1007 /* 1008 * Concatenate fragments. 1009 */ 1010 m = q; 1011 t = m->m_next; 1012 m->m_next = 0; 1013 m_cat(m, t); 1014 nq = q->m_nextpkt; 1015 q->m_nextpkt = 0; 1016 for (q = nq; q != NULL; q = nq) { 1017 nq = q->m_nextpkt; 1018 q->m_nextpkt = NULL; 1019 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; 1020 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; 1021 m_cat(m, q); 1022 } 1023 1024#ifdef IPDIVERT 1025 /* 1026 * Extract firewall instructions from the fragment structure. 1027 */ 1028 *divinfo = fp->ipq_div_info; 1029 *divcookie = fp->ipq_div_cookie; 1030#endif 1031 1032 /* 1033 * Create header for new ip packet by 1034 * modifying header of first packet; 1035 * dequeue and discard fragment reassembly header. 1036 * Make header visible. 1037 */ 1038 ip->ip_len = next; 1039 ip->ip_src = fp->ipq_src; 1040 ip->ip_dst = fp->ipq_dst; 1041 TAILQ_REMOVE(head, fp, ipq_list); 1042 nipq--; 1043 (void) m_free(dtom(fp)); 1044 ip_nfragpackets--; 1045 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); 1046 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); 1047 /* some debugging cruft by sklower, below, will go away soon */ 1048 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 1049 register int plen = 0; 1050 for (t = m; t; t = t->m_next) 1051 plen += t->m_len; 1052 m->m_pkthdr.len = plen; 1053 } 1054 return (m); 1055 1056dropfrag: 1057#ifdef IPDIVERT 1058 *divinfo = 0; 1059 *divcookie = 0; 1060#endif 1061 ipstat.ips_fragdropped++; 1062 m_freem(m); 1063 return (0); 1064 1065#undef GETIP 1066} 1067 1068/* 1069 * Free a fragment reassembly header and all 1070 * associated datagrams. 1071 */ 1072static void 1073ip_freef(fhp, fp) 1074 struct ipqhead *fhp; 1075 struct ipq *fp; 1076{ 1077 register struct mbuf *q; 1078 1079 while (fp->ipq_frags) { 1080 q = fp->ipq_frags; 1081 fp->ipq_frags = q->m_nextpkt; 1082 m_freem(q); 1083 } 1084 TAILQ_REMOVE(fhp, fp, ipq_list); 1085 (void) m_free(dtom(fp)); 1086 ip_nfragpackets--; 1087 nipq--; 1088} 1089 1090/* 1091 * IP timer processing; 1092 * if a timer expires on a reassembly 1093 * queue, discard it. 1094 */ 1095void 1096ip_slowtimo() 1097{ 1098 register struct ipq *fp; 1099 int s = splnet(); 1100 int i; 1101 1102 for (i = 0; i < IPREASS_NHASH; i++) { 1103 for(fp = TAILQ_FIRST(&ipq[i]); fp;) { 1104 struct ipq *fpp; 1105 1106 fpp = fp; 1107 fp = TAILQ_NEXT(fp, ipq_list); 1108 if(--fpp->ipq_ttl == 0) { 1109 ipstat.ips_fragtimeout++; 1110 ip_freef(&ipq[i], fpp); 1111 } 1112 } 1113 } 1114 /* 1115 * If we are over the maximum number of fragments 1116 * (due to the limit being lowered), drain off 1117 * enough to get down to the new limit. 1118 */ 1119 for (i = 0; i < IPREASS_NHASH; i++) { 1120 if (ip_maxfragpackets >= 0) { 1121 while (ip_nfragpackets > ip_maxfragpackets && 1122 !TAILQ_EMPTY(&ipq[i])) { 1123 ipstat.ips_fragdropped++; 1124 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i])); 1125 } 1126 } 1127 } 1128 ipflow_slowtimo(); 1129 splx(s); 1130} 1131 1132/* 1133 * Drain off all datagram fragments. 1134 */ 1135void 1136ip_drain() 1137{ 1138 int i; 1139 1140 for (i = 0; i < IPREASS_NHASH; i++) { 1141 while(!TAILQ_EMPTY(&ipq[i])) { 1142 ipstat.ips_fragdropped++; 1143 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i])); 1144 } 1145 } 1146 in_rtqdrain(); 1147} 1148 1149/* 1150 * Do option processing on a datagram, 1151 * possibly discarding it if bad options are encountered, 1152 * or forwarding it if source-routed. 1153 * Returns 1 if packet has been forwarded/freed, 1154 * 0 if the packet should be processed further. 1155 */ 1156static int 1157ip_dooptions(m) 1158 struct mbuf *m; 1159{ 1160 register struct ip *ip = mtod(m, struct ip *); 1161 register u_char *cp; 1162 register struct ip_timestamp *ipt; 1163 register struct in_ifaddr *ia; 1164 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1165 struct in_addr *sin, dst; 1166 n_time ntime; 1167 1168 dst = ip->ip_dst; 1169 cp = (u_char *)(ip + 1); 1170 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1171 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1172 opt = cp[IPOPT_OPTVAL]; 1173 if (opt == IPOPT_EOL) 1174 break; 1175 if (opt == IPOPT_NOP) 1176 optlen = 1; 1177 else { 1178 if (cnt < IPOPT_OLEN + sizeof(*cp)) { 1179 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1180 goto bad; 1181 } 1182 optlen = cp[IPOPT_OLEN]; 1183 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { 1184 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1185 goto bad; 1186 } 1187 } 1188 switch (opt) { 1189 1190 default: 1191 break; 1192 1193 /* 1194 * Source routing with record. 1195 * Find interface with current destination address. 1196 * If none on this machine then drop if strictly routed, 1197 * or do nothing if loosely routed. 1198 * Record interface address and bring up next address 1199 * component. If strictly routed make sure next 1200 * address is on directly accessible net. 1201 */ 1202 case IPOPT_LSRR: 1203 case IPOPT_SSRR: 1204 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1205 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1206 goto bad; 1207 } 1208 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1209 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1210 goto bad; 1211 } 1212 ipaddr.sin_addr = ip->ip_dst; 1213 ia = (struct in_ifaddr *) 1214 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1215 if (ia == 0) { 1216 if (opt == IPOPT_SSRR) { 1217 type = ICMP_UNREACH; 1218 code = ICMP_UNREACH_SRCFAIL; 1219 goto bad; 1220 } 1221 if (!ip_dosourceroute) 1222 goto nosourcerouting; 1223 /* 1224 * Loose routing, and not at next destination 1225 * yet; nothing to do except forward. 1226 */ 1227 break; 1228 } 1229 off--; /* 0 origin */ 1230 if (off > optlen - (int)sizeof(struct in_addr)) { 1231 /* 1232 * End of source route. Should be for us. 1233 */ 1234 if (!ip_acceptsourceroute) 1235 goto nosourcerouting; 1236 save_rte(cp, ip->ip_src); 1237 break; 1238 } 1239 1240 if (!ip_dosourceroute) { 1241 if (ipforwarding) { 1242 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1243 /* 1244 * Acting as a router, so generate ICMP 1245 */ 1246nosourcerouting: 1247 strcpy(buf, inet_ntoa(ip->ip_dst)); 1248 log(LOG_WARNING, 1249 "attempted source route from %s to %s\n", 1250 inet_ntoa(ip->ip_src), buf); 1251 type = ICMP_UNREACH; 1252 code = ICMP_UNREACH_SRCFAIL; 1253 goto bad; 1254 } else { 1255 /* 1256 * Not acting as a router, so silently drop. 1257 */ 1258 ipstat.ips_cantforward++; 1259 m_freem(m); 1260 return (1); 1261 } 1262 } 1263 1264 /* 1265 * locate outgoing interface 1266 */ 1267 (void)memcpy(&ipaddr.sin_addr, cp + off, 1268 sizeof(ipaddr.sin_addr)); 1269 1270 if (opt == IPOPT_SSRR) { 1271#define INA struct in_ifaddr * 1272#define SA struct sockaddr * 1273 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1274 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1275 } else 1276 ia = ip_rtaddr(ipaddr.sin_addr); 1277 if (ia == 0) { 1278 type = ICMP_UNREACH; 1279 code = ICMP_UNREACH_SRCFAIL; 1280 goto bad; 1281 } 1282 ip->ip_dst = ipaddr.sin_addr; 1283 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1284 sizeof(struct in_addr)); 1285 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1286 /* 1287 * Let ip_intr's mcast routing check handle mcast pkts 1288 */ 1289 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1290 break; 1291 1292 case IPOPT_RR: 1293 if (optlen < IPOPT_OFFSET + sizeof(*cp)) { 1294 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1295 goto bad; 1296 } 1297 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1298 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1299 goto bad; 1300 } 1301 /* 1302 * If no space remains, ignore. 1303 */ 1304 off--; /* 0 origin */ 1305 if (off > optlen - (int)sizeof(struct in_addr)) 1306 break; 1307 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1308 sizeof(ipaddr.sin_addr)); 1309 /* 1310 * locate outgoing interface; if we're the destination, 1311 * use the incoming interface (should be same). 1312 */ 1313 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1314 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1315 type = ICMP_UNREACH; 1316 code = ICMP_UNREACH_HOST; 1317 goto bad; 1318 } 1319 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1320 sizeof(struct in_addr)); 1321 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1322 break; 1323 1324 case IPOPT_TS: 1325 code = cp - (u_char *)ip; 1326 ipt = (struct ip_timestamp *)cp; 1327 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) { 1328 code = (u_char *)&ipt->ipt_len - (u_char *)ip; 1329 goto bad; 1330 } 1331 if (ipt->ipt_ptr < 5) { 1332 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip; 1333 goto bad; 1334 } 1335 if (ipt->ipt_ptr > 1336 ipt->ipt_len - (int)sizeof(int32_t)) { 1337 if (++ipt->ipt_oflw == 0) { 1338 code = (u_char *)&ipt->ipt_ptr - 1339 (u_char *)ip; 1340 goto bad; 1341 } 1342 break; 1343 } 1344 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 1345 switch (ipt->ipt_flg) { 1346 1347 case IPOPT_TS_TSONLY: 1348 break; 1349 1350 case IPOPT_TS_TSANDADDR: 1351 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1352 sizeof(struct in_addr) > ipt->ipt_len) { 1353 code = (u_char *)&ipt->ipt_ptr - 1354 (u_char *)ip; 1355 goto bad; 1356 } 1357 ipaddr.sin_addr = dst; 1358 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1359 m->m_pkthdr.rcvif); 1360 if (ia == 0) 1361 continue; 1362 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1363 sizeof(struct in_addr)); 1364 ipt->ipt_ptr += sizeof(struct in_addr); 1365 break; 1366 1367 case IPOPT_TS_PRESPEC: 1368 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1369 sizeof(struct in_addr) > ipt->ipt_len) { 1370 code = (u_char *)&ipt->ipt_ptr - 1371 (u_char *)ip; 1372 goto bad; 1373 } 1374 (void)memcpy(&ipaddr.sin_addr, sin, 1375 sizeof(struct in_addr)); 1376 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1377 continue; 1378 ipt->ipt_ptr += sizeof(struct in_addr); 1379 break; 1380 1381 default: 1382 /* XXX can't take &ipt->ipt_flg */ 1383 code = (u_char *)&ipt->ipt_ptr - 1384 (u_char *)ip + 1; 1385 goto bad; 1386 } 1387 ntime = iptime(); 1388 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, 1389 sizeof(n_time)); 1390 ipt->ipt_ptr += sizeof(n_time); 1391 } 1392 } 1393 if (forward && ipforwarding) { 1394 ip_forward(m, 1); 1395 return (1); 1396 } 1397 return (0); 1398bad: 1399 icmp_error(m, type, code, 0, 0); 1400 ipstat.ips_badoptions++; 1401 return (1); 1402} 1403 1404/* 1405 * Given address of next destination (final or next hop), 1406 * return internet address info of interface to be used to get there. 1407 */ 1408static struct in_ifaddr * 1409ip_rtaddr(dst) 1410 struct in_addr dst; 1411{ 1412 register struct sockaddr_in *sin; 1413 1414 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 1415 1416 if (ipforward_rt.ro_rt == 0 || 1417 !(ipforward_rt.ro_rt->rt_flags & RTF_UP) || 1418 dst.s_addr != sin->sin_addr.s_addr) { 1419 if (ipforward_rt.ro_rt) { 1420 RTFREE(ipforward_rt.ro_rt); 1421 ipforward_rt.ro_rt = 0; 1422 } 1423 sin->sin_family = AF_INET; 1424 sin->sin_len = sizeof(*sin); 1425 sin->sin_addr = dst; 1426 1427 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1428 } 1429 if (ipforward_rt.ro_rt == 0) 1430 return ((struct in_ifaddr *)0); 1431 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 1432} 1433 1434/* 1435 * Save incoming source route for use in replies, 1436 * to be picked up later by ip_srcroute if the receiver is interested. 1437 */ 1438void 1439save_rte(option, dst) 1440 u_char *option; 1441 struct in_addr dst; 1442{ 1443 unsigned olen; 1444 1445 olen = option[IPOPT_OLEN]; 1446#ifdef DIAGNOSTIC 1447 if (ipprintfs) 1448 printf("save_rte: olen %d\n", olen); 1449#endif 1450 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1451 return; 1452 bcopy(option, ip_srcrt.srcopt, olen); 1453 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1454 ip_srcrt.dst = dst; 1455} 1456 1457/* 1458 * Retrieve incoming source route for use in replies, 1459 * in the same form used by setsockopt. 1460 * The first hop is placed before the options, will be removed later. 1461 */ 1462struct mbuf * 1463ip_srcroute() 1464{ 1465 register struct in_addr *p, *q; 1466 register struct mbuf *m; 1467 1468 if (ip_nhops == 0) 1469 return ((struct mbuf *)0); 1470 m = m_get(M_DONTWAIT, MT_HEADER); 1471 if (m == 0) 1472 return ((struct mbuf *)0); 1473 1474#define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1475 1476 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1477 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1478 OPTSIZ; 1479#ifdef DIAGNOSTIC 1480 if (ipprintfs) 1481 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1482#endif 1483 1484 /* 1485 * First save first hop for return route 1486 */ 1487 p = &ip_srcrt.route[ip_nhops - 1]; 1488 *(mtod(m, struct in_addr *)) = *p--; 1489#ifdef DIAGNOSTIC 1490 if (ipprintfs) 1491 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1492#endif 1493 1494 /* 1495 * Copy option fields and padding (nop) to mbuf. 1496 */ 1497 ip_srcrt.nop = IPOPT_NOP; 1498 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1499 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1500 &ip_srcrt.nop, OPTSIZ); 1501 q = (struct in_addr *)(mtod(m, caddr_t) + 1502 sizeof(struct in_addr) + OPTSIZ); 1503#undef OPTSIZ 1504 /* 1505 * Record return path as an IP source route, 1506 * reversing the path (pointers are now aligned). 1507 */ 1508 while (p >= ip_srcrt.route) { 1509#ifdef DIAGNOSTIC 1510 if (ipprintfs) 1511 printf(" %lx", (u_long)ntohl(q->s_addr)); 1512#endif 1513 *q++ = *p--; 1514 } 1515 /* 1516 * Last hop goes to final destination. 1517 */ 1518 *q = ip_srcrt.dst; 1519#ifdef DIAGNOSTIC 1520 if (ipprintfs) 1521 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1522#endif 1523 return (m); 1524} 1525 1526/* 1527 * Strip out IP options, at higher 1528 * level protocol in the kernel. 1529 * Second argument is buffer to which options 1530 * will be moved, and return value is their length. 1531 * XXX should be deleted; last arg currently ignored. 1532 */ 1533void 1534ip_stripoptions(m, mopt) 1535 register struct mbuf *m; 1536 struct mbuf *mopt; 1537{ 1538 register int i; 1539 struct ip *ip = mtod(m, struct ip *); 1540 register caddr_t opts; 1541 int olen; 1542 1543 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1544 opts = (caddr_t)(ip + 1); 1545 i = m->m_len - (sizeof (struct ip) + olen); 1546 bcopy(opts + olen, opts, (unsigned)i); 1547 m->m_len -= olen; 1548 if (m->m_flags & M_PKTHDR) 1549 m->m_pkthdr.len -= olen; 1550 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1551} 1552 1553u_char inetctlerrmap[PRC_NCMDS] = { 1554 0, 0, 0, 0, 1555 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1556 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1557 EMSGSIZE, EHOSTUNREACH, 0, 0, 1558 0, 0, 0, 0, 1559 ENOPROTOOPT, ECONNREFUSED 1560}; 1561 1562/* 1563 * Forward a packet. If some error occurs return the sender 1564 * an icmp packet. Note we can't always generate a meaningful 1565 * icmp message because icmp doesn't have a large enough repertoire 1566 * of codes and types. 1567 * 1568 * If not forwarding, just drop the packet. This could be confusing 1569 * if ipforwarding was zero but some routing protocol was advancing 1570 * us as a gateway to somewhere. However, we must let the routing 1571 * protocol deal with that. 1572 * 1573 * The srcrt parameter indicates whether the packet is being forwarded 1574 * via a source route. 1575 */ 1576static void 1577ip_forward(m, srcrt) 1578 struct mbuf *m; 1579 int srcrt; 1580{ 1581 register struct ip *ip = mtod(m, struct ip *); 1582 register struct rtentry *rt; 1583 int error, type = 0, code = 0; 1584 struct mbuf *mcopy; 1585 n_long dest; 1586 struct ifnet *destifp; 1587#ifdef IPSEC 1588 struct ifnet dummyifp; 1589#endif 1590 1591 dest = 0; 1592#ifdef DIAGNOSTIC 1593 if (ipprintfs) 1594 printf("forward: src %lx dst %lx ttl %x\n", 1595 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr, 1596 ip->ip_ttl); 1597#endif 1598 1599 1600 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1601 ipstat.ips_cantforward++; 1602 m_freem(m); 1603 return; 1604 } 1605#ifdef IPSTEALTH 1606 if (!ipstealth) { 1607#endif 1608 if (ip->ip_ttl <= IPTTLDEC) { 1609 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1610 dest, 0); 1611 return; 1612 } 1613#ifdef IPSTEALTH 1614 } 1615#endif 1616 1617 if (ip_rtaddr(ip->ip_dst) == 0) { 1618 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1619 return; 1620 } else 1621 rt = ipforward_rt.ro_rt; 1622 1623 /* 1624 * Save the IP header and at most 8 bytes of the payload, 1625 * in case we need to generate an ICMP message to the src. 1626 * 1627 * We don't use m_copy() because it might return a reference 1628 * to a shared cluster. Both this function and ip_output() 1629 * assume exclusive access to the IP header in `m', so any 1630 * data in a cluster may change before we reach icmp_error(). 1631 */ 1632 MGET(mcopy, M_DONTWAIT, m->m_type); 1633 if (mcopy != NULL) { 1634 M_COPY_PKTHDR(mcopy, m); 1635 mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8, 1636 (int)ip->ip_len); 1637 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); 1638 } 1639 1640#ifdef IPSTEALTH 1641 if (!ipstealth) { 1642#endif 1643 ip->ip_ttl -= IPTTLDEC; 1644#ifdef IPSTEALTH 1645 } 1646#endif 1647 1648 /* 1649 * If forwarding packet using same interface that it came in on, 1650 * perhaps should send a redirect to sender to shortcut a hop. 1651 * Only send redirect if source is sending directly to us, 1652 * and if packet was not source routed (or has any options). 1653 * Also, don't send redirect if forwarding using a default route 1654 * or a route modified by a redirect. 1655 */ 1656#define satosin(sa) ((struct sockaddr_in *)(sa)) 1657 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1658 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1659 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1660 ipsendredirects && !srcrt) { 1661#define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1662 u_long src = ntohl(ip->ip_src.s_addr); 1663 1664 if (RTA(rt) && 1665 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1666 if (rt->rt_flags & RTF_GATEWAY) 1667 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1668 else 1669 dest = ip->ip_dst.s_addr; 1670 /* Router requirements says to only send host redirects */ 1671 type = ICMP_REDIRECT; 1672 code = ICMP_REDIRECT_HOST; 1673#ifdef DIAGNOSTIC 1674 if (ipprintfs) 1675 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1676#endif 1677 } 1678 } 1679 1680 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1681 IP_FORWARDING, 0); 1682 if (error) 1683 ipstat.ips_cantforward++; 1684 else { 1685 ipstat.ips_forward++; 1686 if (type) 1687 ipstat.ips_redirectsent++; 1688 else { 1689 if (mcopy) { 1690 ipflow_create(&ipforward_rt, mcopy); 1691 m_freem(mcopy); 1692 } 1693 return; 1694 } 1695 } 1696 if (mcopy == NULL) 1697 return; 1698 destifp = NULL; 1699 1700 switch (error) { 1701 1702 case 0: /* forwarded, but need redirect */ 1703 /* type, code set above */ 1704 break; 1705 1706 case ENETUNREACH: /* shouldn't happen, checked above */ 1707 case EHOSTUNREACH: 1708 case ENETDOWN: 1709 case EHOSTDOWN: 1710 default: 1711 type = ICMP_UNREACH; 1712 code = ICMP_UNREACH_HOST; 1713 break; 1714 1715 case EMSGSIZE: 1716 type = ICMP_UNREACH; 1717 code = ICMP_UNREACH_NEEDFRAG; 1718#ifndef IPSEC 1719 if (ipforward_rt.ro_rt) 1720 destifp = ipforward_rt.ro_rt->rt_ifp; 1721#else 1722 /* 1723 * If the packet is routed over IPsec tunnel, tell the 1724 * originator the tunnel MTU. 1725 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1726 * XXX quickhack!!! 1727 */ 1728 if (ipforward_rt.ro_rt) { 1729 struct secpolicy *sp = NULL; 1730 int ipsecerror; 1731 int ipsechdr; 1732 struct route *ro; 1733 1734 sp = ipsec4_getpolicybyaddr(mcopy, 1735 IPSEC_DIR_OUTBOUND, 1736 IP_FORWARDING, 1737 &ipsecerror); 1738 1739 if (sp == NULL) 1740 destifp = ipforward_rt.ro_rt->rt_ifp; 1741 else { 1742 /* count IPsec header size */ 1743 ipsechdr = ipsec4_hdrsiz(mcopy, 1744 IPSEC_DIR_OUTBOUND, 1745 NULL); 1746 1747 /* 1748 * find the correct route for outer IPv4 1749 * header, compute tunnel MTU. 1750 * 1751 * XXX BUG ALERT 1752 * The "dummyifp" code relies upon the fact 1753 * that icmp_error() touches only ifp->if_mtu. 1754 */ 1755 /*XXX*/ 1756 destifp = NULL; 1757 if (sp->req != NULL 1758 && sp->req->sav != NULL 1759 && sp->req->sav->sah != NULL) { 1760 ro = &sp->req->sav->sah->sa_route; 1761 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1762 dummyifp.if_mtu = 1763 ro->ro_rt->rt_ifp->if_mtu; 1764 dummyifp.if_mtu -= ipsechdr; 1765 destifp = &dummyifp; 1766 } 1767 } 1768 1769 key_freesp(sp); 1770 } 1771 } 1772#endif /*IPSEC*/ 1773 ipstat.ips_cantfrag++; 1774 break; 1775 1776 case ENOBUFS: 1777 type = ICMP_SOURCEQUENCH; 1778 code = 0; 1779 break; 1780 1781 case EACCES: /* ipfw denied packet */ 1782 m_freem(mcopy); 1783 return; 1784 } 1785 icmp_error(mcopy, type, code, dest, destifp); 1786} 1787 1788void 1789ip_savecontrol(inp, mp, ip, m) 1790 register struct inpcb *inp; 1791 register struct mbuf **mp; 1792 register struct ip *ip; 1793 register struct mbuf *m; 1794{ 1795 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1796 struct timeval tv; 1797 1798 microtime(&tv); 1799 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1800 SCM_TIMESTAMP, SOL_SOCKET); 1801 if (*mp) 1802 mp = &(*mp)->m_next; 1803 } 1804 if (inp->inp_flags & INP_RECVDSTADDR) { 1805 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1806 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1807 if (*mp) 1808 mp = &(*mp)->m_next; 1809 } 1810#ifdef notyet 1811 /* XXX 1812 * Moving these out of udp_input() made them even more broken 1813 * than they already were. 1814 */ 1815 /* options were tossed already */ 1816 if (inp->inp_flags & INP_RECVOPTS) { 1817 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1818 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1819 if (*mp) 1820 mp = &(*mp)->m_next; 1821 } 1822 /* ip_srcroute doesn't do what we want here, need to fix */ 1823 if (inp->inp_flags & INP_RECVRETOPTS) { 1824 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1825 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1826 if (*mp) 1827 mp = &(*mp)->m_next; 1828 } 1829#endif 1830 if (inp->inp_flags & INP_RECVIF) { 1831 struct ifnet *ifp; 1832 struct sdlbuf { 1833 struct sockaddr_dl sdl; 1834 u_char pad[32]; 1835 } sdlbuf; 1836 struct sockaddr_dl *sdp; 1837 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1838 1839 if (((ifp = m->m_pkthdr.rcvif)) 1840 && ( ifp->if_index && (ifp->if_index <= if_index))) { 1841 sdp = (struct sockaddr_dl *) 1842 (ifaddr_byindex(ifp->if_index)->ifa_addr); 1843 /* 1844 * Change our mind and don't try copy. 1845 */ 1846 if ((sdp->sdl_family != AF_LINK) 1847 || (sdp->sdl_len > sizeof(sdlbuf))) { 1848 goto makedummy; 1849 } 1850 bcopy(sdp, sdl2, sdp->sdl_len); 1851 } else { 1852makedummy: 1853 sdl2->sdl_len 1854 = offsetof(struct sockaddr_dl, sdl_data[0]); 1855 sdl2->sdl_family = AF_LINK; 1856 sdl2->sdl_index = 0; 1857 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1858 } 1859 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 1860 IP_RECVIF, IPPROTO_IP); 1861 if (*mp) 1862 mp = &(*mp)->m_next; 1863 } 1864} 1865 1866int 1867ip_rsvp_init(struct socket *so) 1868{ 1869 if (so->so_type != SOCK_RAW || 1870 so->so_proto->pr_protocol != IPPROTO_RSVP) 1871 return EOPNOTSUPP; 1872 1873 if (ip_rsvpd != NULL) 1874 return EADDRINUSE; 1875 1876 ip_rsvpd = so; 1877 /* 1878 * This may seem silly, but we need to be sure we don't over-increment 1879 * the RSVP counter, in case something slips up. 1880 */ 1881 if (!ip_rsvp_on) { 1882 ip_rsvp_on = 1; 1883 rsvp_on++; 1884 } 1885 1886 return 0; 1887} 1888 1889int 1890ip_rsvp_done(void) 1891{ 1892 ip_rsvpd = NULL; 1893 /* 1894 * This may seem silly, but we need to be sure we don't over-decrement 1895 * the RSVP counter, in case something slips up. 1896 */ 1897 if (ip_rsvp_on) { 1898 ip_rsvp_on = 0; 1899 rsvp_on--; 1900 } 1901 return 0; 1902}
|