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