The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/ip_input.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)ip_input.c  8.2 (Berkeley) 1/4/94
   30  * $FreeBSD$
   31  */
   32 
   33 #include "opt_bootp.h"
   34 #include "opt_ipfw.h"
   35 #include "opt_ipstealth.h"
   36 #include "opt_ipsec.h"
   37 #include "opt_mac.h"
   38 #include "opt_carp.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 #include <sys/callout.h>
   43 #include <sys/mac.h>
   44 #include <sys/mbuf.h>
   45 #include <sys/malloc.h>
   46 #include <sys/domain.h>
   47 #include <sys/protosw.h>
   48 #include <sys/socket.h>
   49 #include <sys/time.h>
   50 #include <sys/kernel.h>
   51 #include <sys/syslog.h>
   52 #include <sys/sysctl.h>
   53 
   54 #include <net/pfil.h>
   55 #include <net/if.h>
   56 #include <net/if_types.h>
   57 #include <net/if_var.h>
   58 #include <net/if_dl.h>
   59 #include <net/route.h>
   60 #include <net/netisr.h>
   61 
   62 #include <netinet/in.h>
   63 #include <netinet/in_systm.h>
   64 #include <netinet/in_var.h>
   65 #include <netinet/ip.h>
   66 #include <netinet/in_pcb.h>
   67 #include <netinet/ip_var.h>
   68 #include <netinet/ip_icmp.h>
   69 #include <machine/in_cksum.h>
   70 #ifdef DEV_CARP
   71 #include <netinet/ip_carp.h>
   72 #endif
   73 
   74 #include <sys/socketvar.h>
   75 
   76 /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
   77 #include <netinet/ip_fw.h>
   78 #include <netinet/ip_dummynet.h>
   79 
   80 #ifdef IPSEC
   81 #include <netinet6/ipsec.h>
   82 #include <netkey/key.h>
   83 #endif
   84 
   85 #ifdef FAST_IPSEC
   86 #include <netipsec/ipsec.h>
   87 #include <netipsec/key.h>
   88 #endif
   89 
   90 int rsvp_on = 0;
   91 
   92 int     ipforwarding = 0;
   93 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
   94     &ipforwarding, 0, "Enable IP forwarding between interfaces");
   95 
   96 static int      ipsendredirects = 1; /* XXX */
   97 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
   98     &ipsendredirects, 0, "Enable sending IP redirects");
   99 
  100 int     ip_defttl = IPDEFTTL;
  101 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
  102     &ip_defttl, 0, "Maximum TTL on IP packets");
  103 
  104 static int      ip_dosourceroute = 0;
  105 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
  106     &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
  107 
  108 static int      ip_acceptsourceroute = 0;
  109 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 
  110     CTLFLAG_RW, &ip_acceptsourceroute, 0, 
  111     "Enable accepting source routed IP packets");
  112 
  113 int             ip_doopts = 1;  /* 0 = ignore, 1 = process, 2 = reject */
  114 SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW,
  115     &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)");
  116 
  117 static int      ip_keepfaith = 0;
  118 SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
  119         &ip_keepfaith,  0,
  120         "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
  121 
  122 static int      ip_sendsourcequench = 0;
  123 SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
  124         &ip_sendsourcequench, 0,
  125         "Enable the transmission of source quench packets");
  126 
  127 int     ip_do_randomid = 0;
  128 SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
  129         &ip_do_randomid, 0,
  130         "Assign random ip_id values");
  131 
  132 /*
  133  * XXX - Setting ip_checkinterface mostly implements the receive side of
  134  * the Strong ES model described in RFC 1122, but since the routing table
  135  * and transmit implementation do not implement the Strong ES model,
  136  * setting this to 1 results in an odd hybrid.
  137  *
  138  * XXX - ip_checkinterface currently must be disabled if you use ipnat
  139  * to translate the destination address to another local interface.
  140  *
  141  * XXX - ip_checkinterface must be disabled if you add IP aliases
  142  * to the loopback interface instead of the interface where the
  143  * packets for those addresses are received.
  144  */
  145 static int      ip_checkinterface = 0;
  146 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
  147     &ip_checkinterface, 0, "Verify packet arrives on correct interface");
  148 
  149 #ifdef DIAGNOSTIC
  150 static int      ipprintfs = 0;
  151 #endif
  152 
  153 struct pfil_head inet_pfil_hook;        /* Packet filter hooks */
  154 
  155 static struct   ifqueue ipintrq;
  156 static int      ipqmaxlen = IFQ_MAXLEN;
  157 
  158 extern  struct domain inetdomain;
  159 extern  struct protosw inetsw[];
  160 u_char  ip_protox[IPPROTO_MAX];
  161 struct  in_ifaddrhead in_ifaddrhead;            /* first inet address */
  162 struct  in_ifaddrhashhead *in_ifaddrhashtbl;    /* inet addr hash table  */
  163 u_long  in_ifaddrhmask;                         /* mask for hash table */
  164 
  165 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
  166     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
  167 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
  168     &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
  169 
  170 struct ipstat ipstat;
  171 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
  172     &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
  173 
  174 /*
  175  * IP datagram reassembly.
  176  */
  177 #define IPREASS_NHASH_LOG2      6
  178 #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
  179 #define IPREASS_HMASK           (IPREASS_NHASH - 1)
  180 #define IPREASS_HASH(x,y) \
  181         (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
  182 
  183 static uma_zone_t ipq_zone;
  184 static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
  185 static struct mtx ipqlock;
  186 
  187 #define IPQ_LOCK()      mtx_lock(&ipqlock)
  188 #define IPQ_UNLOCK()    mtx_unlock(&ipqlock)
  189 #define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
  190 #define IPQ_LOCK_ASSERT()       mtx_assert(&ipqlock, MA_OWNED)
  191 
  192 static void     maxnipq_update(void);
  193 static void     ipq_zone_change(void *);
  194 
  195 static int      maxnipq;        /* Administrative limit on # reass queues. */
  196 static int      nipq = 0;       /* Total # of reass queues */
  197 SYSCTL_INT(_net_inet_ip, OID_AUTO, fragpackets, CTLFLAG_RD, &nipq, 0,
  198         "Current number of IPv4 fragment reassembly queue entries");
  199 
  200 static int      maxfragsperpacket;
  201 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
  202         &maxfragsperpacket, 0,
  203         "Maximum number of IPv4 fragments allowed per packet");
  204 
  205 struct callout  ipport_tick_callout;
  206 
  207 #ifdef IPCTL_DEFMTU
  208 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
  209     &ip_mtu, 0, "Default MTU");
  210 #endif
  211 
  212 #ifdef IPSTEALTH
  213 int     ipstealth = 0;
  214 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
  215     &ipstealth, 0, "");
  216 #endif
  217 
  218 /*
  219  * ipfw_ether and ipfw_bridge hooks.
  220  * XXX: Temporary until those are converted to pfil_hooks as well.
  221  */
  222 ip_fw_chk_t *ip_fw_chk_ptr = NULL;
  223 ip_dn_io_t *ip_dn_io_ptr = NULL;
  224 int fw_enable = 1;
  225 int fw_one_pass = 1;
  226 
  227 /*
  228  * XXX this is ugly.  IP options source routing magic.
  229  */
  230 struct ipoptrt {
  231         struct  in_addr dst;                    /* final destination */
  232         char    nop;                            /* one NOP to align */
  233         char    srcopt[IPOPT_OFFSET + 1];       /* OPTVAL, OLEN and OFFSET */
  234         struct  in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
  235 };
  236 
  237 struct ipopt_tag {
  238         struct  m_tag tag;
  239         int     ip_nhops;
  240         struct  ipoptrt ip_srcrt;
  241 };
  242 
  243 static void     save_rte(struct mbuf *, u_char *, struct in_addr);
  244 static int      ip_dooptions(struct mbuf *m, int);
  245 static void     ip_forward(struct mbuf *m, int srcrt);
  246 static void     ip_freef(struct ipqhead *, struct ipq *);
  247 
  248 /*
  249  * IP initialization: fill in IP protocol switch table.
  250  * All protocols not implemented in kernel go to raw IP protocol handler.
  251  */
  252 void
  253 ip_init()
  254 {
  255         register struct protosw *pr;
  256         register int i;
  257 
  258         TAILQ_INIT(&in_ifaddrhead);
  259         in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask);
  260         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
  261         if (pr == NULL)
  262                 panic("ip_init: PF_INET not found");
  263 
  264         /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
  265         for (i = 0; i < IPPROTO_MAX; i++)
  266                 ip_protox[i] = pr - inetsw;
  267         /*
  268          * Cycle through IP protocols and put them into the appropriate place
  269          * in ip_protox[].
  270          */
  271         for (pr = inetdomain.dom_protosw;
  272             pr < inetdomain.dom_protoswNPROTOSW; pr++)
  273                 if (pr->pr_domain->dom_family == PF_INET &&
  274                     pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
  275                         /* Be careful to only index valid IP protocols. */
  276                         if (pr->pr_protocol < IPPROTO_MAX)
  277                                 ip_protox[pr->pr_protocol] = pr - inetsw;
  278                 }
  279 
  280         /* Initialize packet filter hooks. */
  281         inet_pfil_hook.ph_type = PFIL_TYPE_AF;
  282         inet_pfil_hook.ph_af = AF_INET;
  283         if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
  284                 printf("%s: WARNING: unable to register pfil hook, "
  285                         "error %d\n", __func__, i);
  286 
  287         /* Initialize IP reassembly queue. */
  288         IPQ_LOCK_INIT();
  289         for (i = 0; i < IPREASS_NHASH; i++)
  290             TAILQ_INIT(&ipq[i]);
  291         maxnipq = nmbclusters / 32;
  292         maxfragsperpacket = 16;
  293         ipq_zone = uma_zcreate("ipq", sizeof(struct ipq), NULL, NULL, NULL,
  294             NULL, UMA_ALIGN_PTR, 0);
  295         maxnipq_update();
  296 
  297         /* Start ipport_tick. */
  298         callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
  299         ipport_tick(NULL);
  300         EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
  301                 SHUTDOWN_PRI_DEFAULT);
  302         EVENTHANDLER_REGISTER(nmbclusters_change, ipq_zone_change,
  303                 NULL, EVENTHANDLER_PRI_ANY);
  304 
  305         /* Initialize various other remaining things. */
  306         ip_id = time_second & 0xffff;
  307         ipintrq.ifq_maxlen = ipqmaxlen;
  308         mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
  309         netisr_register(NETISR_IP, ip_input, &ipintrq, NETISR_MPSAFE);
  310 }
  311 
  312 void ip_fini(xtp)
  313         void *xtp;
  314 {
  315         callout_stop(&ipport_tick_callout);
  316 }
  317 
  318 /*
  319  * Ip input routine.  Checksum and byte swap header.  If fragmented
  320  * try to reassemble.  Process options.  Pass to next level.
  321  */
  322 void
  323 ip_input(struct mbuf *m)
  324 {
  325         struct ip *ip = NULL;
  326         struct in_ifaddr *ia = NULL;
  327         struct ifaddr *ifa;
  328         int    checkif, hlen = 0;
  329         u_short sum;
  330         int dchg = 0;                           /* dest changed after fw */
  331         struct in_addr odst;                    /* original dst address */
  332 #ifdef FAST_IPSEC
  333         struct m_tag *mtag;
  334         struct tdb_ident *tdbi;
  335         struct secpolicy *sp;
  336         int s, error;
  337 #endif /* FAST_IPSEC */
  338 
  339         M_ASSERTPKTHDR(m);
  340         
  341         if (m->m_flags & M_FASTFWD_OURS) {
  342                 /*
  343                  * Firewall or NAT changed destination to local.
  344                  * We expect ip_len and ip_off to be in host byte order.
  345                  */
  346                 m->m_flags &= ~M_FASTFWD_OURS;
  347                 /* Set up some basics that will be used later. */
  348                 ip = mtod(m, struct ip *);
  349                 hlen = ip->ip_hl << 2;
  350                 goto ours;
  351         }
  352 
  353         ipstat.ips_total++;
  354 
  355         if (m->m_pkthdr.len < sizeof(struct ip))
  356                 goto tooshort;
  357 
  358         if (m->m_len < sizeof (struct ip) &&
  359             (m = m_pullup(m, sizeof (struct ip))) == NULL) {
  360                 ipstat.ips_toosmall++;
  361                 return;
  362         }
  363         ip = mtod(m, struct ip *);
  364 
  365         if (ip->ip_v != IPVERSION) {
  366                 ipstat.ips_badvers++;
  367                 goto bad;
  368         }
  369 
  370         hlen = ip->ip_hl << 2;
  371         if (hlen < sizeof(struct ip)) { /* minimum header length */
  372                 ipstat.ips_badhlen++;
  373                 goto bad;
  374         }
  375         if (hlen > m->m_len) {
  376                 if ((m = m_pullup(m, hlen)) == NULL) {
  377                         ipstat.ips_badhlen++;
  378                         return;
  379                 }
  380                 ip = mtod(m, struct ip *);
  381         }
  382 
  383         /* 127/8 must not appear on wire - RFC1122 */
  384         if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
  385             (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
  386                 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
  387                         ipstat.ips_badaddr++;
  388                         goto bad;
  389                 }
  390         }
  391 
  392         if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
  393                 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
  394         } else {
  395                 if (hlen == sizeof(struct ip)) {
  396                         sum = in_cksum_hdr(ip);
  397                 } else {
  398                         sum = in_cksum(m, hlen);
  399                 }
  400         }
  401         if (sum) {
  402                 ipstat.ips_badsum++;
  403                 goto bad;
  404         }
  405 
  406 #ifdef ALTQ
  407         if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
  408                 /* packet is dropped by traffic conditioner */
  409                 return;
  410 #endif
  411 
  412         /*
  413          * Convert fields to host representation.
  414          */
  415         ip->ip_len = ntohs(ip->ip_len);
  416         if (ip->ip_len < hlen) {
  417                 ipstat.ips_badlen++;
  418                 goto bad;
  419         }
  420         ip->ip_off = ntohs(ip->ip_off);
  421 
  422         /*
  423          * Check that the amount of data in the buffers
  424          * is as at least much as the IP header would have us expect.
  425          * Trim mbufs if longer than we expect.
  426          * Drop packet if shorter than we expect.
  427          */
  428         if (m->m_pkthdr.len < ip->ip_len) {
  429 tooshort:
  430                 ipstat.ips_tooshort++;
  431                 goto bad;
  432         }
  433         if (m->m_pkthdr.len > ip->ip_len) {
  434                 if (m->m_len == m->m_pkthdr.len) {
  435                         m->m_len = ip->ip_len;
  436                         m->m_pkthdr.len = ip->ip_len;
  437                 } else
  438                         m_adj(m, ip->ip_len - m->m_pkthdr.len);
  439         }
  440 #if defined(IPSEC) && !defined(IPSEC_FILTERGIF)
  441         /*
  442          * Bypass packet filtering for packets from a tunnel (gif).
  443          */
  444         if (ipsec_getnhist(m))
  445                 goto passin;
  446 #endif
  447 #if defined(FAST_IPSEC) && !defined(IPSEC_FILTERGIF)
  448         /*
  449          * Bypass packet filtering for packets from a tunnel (gif).
  450          */
  451         if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
  452                 goto passin;
  453 #endif
  454 
  455         /*
  456          * Run through list of hooks for input packets.
  457          *
  458          * NB: Beware of the destination address changing (e.g.
  459          *     by NAT rewriting).  When this happens, tell
  460          *     ip_forward to do the right thing.
  461          */
  462 
  463         /* Jump over all PFIL processing if hooks are not active. */
  464         if (inet_pfil_hook.ph_busy_count == -1)
  465                 goto passin;
  466 
  467         odst = ip->ip_dst;
  468         if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
  469             PFIL_IN, NULL) != 0)
  470                 return;
  471         if (m == NULL)                  /* consumed by filter */
  472                 return;
  473 
  474         ip = mtod(m, struct ip *);
  475         dchg = (odst.s_addr != ip->ip_dst.s_addr);
  476 
  477 #ifdef IPFIREWALL_FORWARD
  478         if (m->m_flags & M_FASTFWD_OURS) {
  479                 m->m_flags &= ~M_FASTFWD_OURS;
  480                 goto ours;
  481         }
  482         if ((dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL)) != 0) {
  483                 /*
  484                  * Directly ship on the packet.  This allows to forward packets
  485                  * that were destined for us to some other directly connected
  486                  * host.
  487                  */
  488                 ip_forward(m, dchg);
  489                 return;
  490         }
  491 #endif /* IPFIREWALL_FORWARD */
  492 
  493 passin:
  494         /*
  495          * Process options and, if not destined for us,
  496          * ship it on.  ip_dooptions returns 1 when an
  497          * error was detected (causing an icmp message
  498          * to be sent and the original packet to be freed).
  499          */
  500         if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
  501                 return;
  502 
  503         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
  504          * matter if it is destined to another node, or whether it is 
  505          * a multicast one, RSVP wants it! and prevents it from being forwarded
  506          * anywhere else. Also checks if the rsvp daemon is running before
  507          * grabbing the packet.
  508          */
  509         if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 
  510                 goto ours;
  511 
  512         /*
  513          * Check our list of addresses, to see if the packet is for us.
  514          * If we don't have any addresses, assume any unicast packet
  515          * we receive might be for us (and let the upper layers deal
  516          * with it).
  517          */
  518         if (TAILQ_EMPTY(&in_ifaddrhead) &&
  519             (m->m_flags & (M_MCAST|M_BCAST)) == 0)
  520                 goto ours;
  521 
  522         /*
  523          * Enable a consistency check between the destination address
  524          * and the arrival interface for a unicast packet (the RFC 1122
  525          * strong ES model) if IP forwarding is disabled and the packet
  526          * is not locally generated and the packet is not subject to
  527          * 'ipfw fwd'.
  528          *
  529          * XXX - Checking also should be disabled if the destination
  530          * address is ipnat'ed to a different interface.
  531          *
  532          * XXX - Checking is incompatible with IP aliases added
  533          * to the loopback interface instead of the interface where
  534          * the packets are received.
  535          *
  536          * XXX - This is the case for carp vhost IPs as well so we
  537          * insert a workaround. If the packet got here, we already
  538          * checked with carp_iamatch() and carp_forus().
  539          */
  540         checkif = ip_checkinterface && (ipforwarding == 0) && 
  541             m->m_pkthdr.rcvif != NULL &&
  542             ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
  543 #ifdef DEV_CARP
  544             !m->m_pkthdr.rcvif->if_carp &&
  545 #endif
  546             (dchg == 0);
  547 
  548         /*
  549          * Check for exact addresses in the hash bucket.
  550          */
  551         LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
  552                 /*
  553                  * If the address matches, verify that the packet
  554                  * arrived via the correct interface if checking is
  555                  * enabled.
  556                  */
  557                 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr && 
  558                     (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
  559                         goto ours;
  560         }
  561         /*
  562          * Check for broadcast addresses.
  563          *
  564          * Only accept broadcast packets that arrive via the matching
  565          * interface.  Reception of forwarded directed broadcasts would
  566          * be handled via ip_forward() and ether_output() with the loopback
  567          * into the stack for SIMPLEX interfaces handled by ether_output().
  568          */
  569         if (m->m_pkthdr.rcvif != NULL &&
  570             m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
  571                 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
  572                         if (ifa->ifa_addr->sa_family != AF_INET)
  573                                 continue;
  574                         ia = ifatoia(ifa);
  575                         if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
  576                             ip->ip_dst.s_addr)
  577                                 goto ours;
  578                         if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
  579                                 goto ours;
  580 #ifdef BOOTP_COMPAT
  581                         if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
  582                                 goto ours;
  583 #endif
  584                 }
  585         }
  586         /* RFC 3927 2.7: Do not forward datagrams for 169.254.0.0/16. */
  587         if (IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
  588                 ipstat.ips_cantforward++;
  589                 m_freem(m);
  590                 return;
  591         }
  592         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
  593                 struct in_multi *inm;
  594                 if (ip_mrouter) {
  595                         /*
  596                          * If we are acting as a multicast router, all
  597                          * incoming multicast packets are passed to the
  598                          * kernel-level multicast forwarding function.
  599                          * The packet is returned (relatively) intact; if
  600                          * ip_mforward() returns a non-zero value, the packet
  601                          * must be discarded, else it may be accepted below.
  602                          */
  603                         if (ip_mforward &&
  604                             ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
  605                                 ipstat.ips_cantforward++;
  606                                 m_freem(m);
  607                                 return;
  608                         }
  609 
  610                         /*
  611                          * The process-level routing daemon needs to receive
  612                          * all multicast IGMP packets, whether or not this
  613                          * host belongs to their destination groups.
  614                          */
  615                         if (ip->ip_p == IPPROTO_IGMP)
  616                                 goto ours;
  617                         ipstat.ips_forward++;
  618                 }
  619                 /*
  620                  * See if we belong to the destination multicast group on the
  621                  * arrival interface.
  622                  */
  623                 IN_MULTI_LOCK();
  624                 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
  625                 IN_MULTI_UNLOCK();
  626                 if (inm == NULL) {
  627                         ipstat.ips_notmember++;
  628                         m_freem(m);
  629                         return;
  630                 }
  631                 goto ours;
  632         }
  633         if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
  634                 goto ours;
  635         if (ip->ip_dst.s_addr == INADDR_ANY)
  636                 goto ours;
  637 
  638         /*
  639          * FAITH(Firewall Aided Internet Translator)
  640          */
  641         if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
  642                 if (ip_keepfaith) {
  643                         if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 
  644                                 goto ours;
  645                 }
  646                 m_freem(m);
  647                 return;
  648         }
  649 
  650         /*
  651          * Not for us; forward if possible and desirable.
  652          */
  653         if (ipforwarding == 0) {
  654                 ipstat.ips_cantforward++;
  655                 m_freem(m);
  656         } else {
  657 #ifdef IPSEC
  658                 /*
  659                  * Enforce inbound IPsec SPD.
  660                  */
  661                 if (ipsec4_in_reject(m, NULL)) {
  662                         ipsecstat.in_polvio++;
  663                         goto bad;
  664                 }
  665 #endif /* IPSEC */
  666 #ifdef FAST_IPSEC
  667                 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
  668                 s = splnet();
  669                 if (mtag != NULL) {
  670                         tdbi = (struct tdb_ident *)(mtag + 1);
  671                         sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
  672                 } else {
  673                         sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
  674                                                    IP_FORWARDING, &error);   
  675                 }
  676                 if (sp == NULL) {       /* NB: can happen if error */
  677                         splx(s);
  678                         /*XXX error stat???*/
  679                         DPRINTF(("ip_input: no SP for forwarding\n"));  /*XXX*/
  680                         goto bad;
  681                 }
  682 
  683                 /*
  684                  * Check security policy against packet attributes.
  685                  */
  686                 error = ipsec_in_reject(sp, m);
  687                 KEY_FREESP(&sp);
  688                 splx(s);
  689                 if (error) {
  690                         ipstat.ips_cantforward++;
  691                         goto bad;
  692                 }
  693 #endif /* FAST_IPSEC */
  694                 ip_forward(m, dchg);
  695         }
  696         return;
  697 
  698 ours:
  699 #ifdef IPSTEALTH
  700         /*
  701          * IPSTEALTH: Process non-routing options only
  702          * if the packet is destined for us.
  703          */
  704         if (ipstealth && hlen > sizeof (struct ip) &&
  705             ip_dooptions(m, 1))
  706                 return;
  707 #endif /* IPSTEALTH */
  708 
  709         /* Count the packet in the ip address stats */
  710         if (ia != NULL) {
  711                 ia->ia_ifa.if_ipackets++;
  712                 ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
  713         }
  714 
  715         /*
  716          * Attempt reassembly; if it succeeds, proceed.
  717          * ip_reass() will return a different mbuf.
  718          */
  719         if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
  720                 m = ip_reass(m);
  721                 if (m == NULL)
  722                         return;
  723                 ip = mtod(m, struct ip *);
  724                 /* Get the header length of the reassembled packet */
  725                 hlen = ip->ip_hl << 2;
  726         }
  727 
  728         /*
  729          * Further protocols expect the packet length to be w/o the
  730          * IP header.
  731          */
  732         ip->ip_len -= hlen;
  733 
  734 #ifdef IPSEC
  735         /*
  736          * enforce IPsec policy checking if we are seeing last header.
  737          * note that we do not visit this with protocols with pcb layer
  738          * code - like udp/tcp/raw ip.
  739          */
  740         if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
  741             ipsec4_in_reject(m, NULL)) {
  742                 ipsecstat.in_polvio++;
  743                 goto bad;
  744         }
  745 #endif
  746 #if FAST_IPSEC
  747         /*
  748          * enforce IPsec policy checking if we are seeing last header.
  749          * note that we do not visit this with protocols with pcb layer
  750          * code - like udp/tcp/raw ip.
  751          */
  752         if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
  753                 /*
  754                  * Check if the packet has already had IPsec processing
  755                  * done.  If so, then just pass it along.  This tag gets
  756                  * set during AH, ESP, etc. input handling, before the
  757                  * packet is returned to the ip input queue for delivery.
  758                  */ 
  759                 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
  760                 s = splnet();
  761                 if (mtag != NULL) {
  762                         tdbi = (struct tdb_ident *)(mtag + 1);
  763                         sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
  764                 } else {
  765                         sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
  766                                                    IP_FORWARDING, &error);   
  767                 }
  768                 if (sp != NULL) {
  769                         /*
  770                          * Check security policy against packet attributes.
  771                          */
  772                         error = ipsec_in_reject(sp, m);
  773                         KEY_FREESP(&sp);
  774                 } else {
  775                         /* XXX error stat??? */
  776                         error = EINVAL;
  777 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
  778                         goto bad;
  779                 }
  780                 splx(s);
  781                 if (error)
  782                         goto bad;
  783         }
  784 #endif /* FAST_IPSEC */
  785 
  786         /*
  787          * Switch out to protocol's input routine.
  788          */
  789         ipstat.ips_delivered++;
  790 
  791         (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
  792         return;
  793 bad:
  794         m_freem(m);
  795 }
  796 
  797 /*
  798  * After maxnipq has been updated, propagate the change to UMA.  The UMA zone
  799  * max has slightly different semantics than the sysctl, for historical
  800  * reasons.
  801  */
  802 static void
  803 maxnipq_update(void)
  804 {
  805 
  806         /*
  807          * -1 for unlimited allocation.
  808          */
  809         if (maxnipq < 0)
  810                 uma_zone_set_max(ipq_zone, 0);
  811         /*
  812          * Positive number for specific bound.
  813          */
  814         if (maxnipq > 0)
  815                 uma_zone_set_max(ipq_zone, maxnipq);
  816         /*
  817          * Zero specifies no further fragment queue allocation -- set the
  818          * bound very low, but rely on implementation elsewhere to actually
  819          * prevent allocation and reclaim current queues.
  820          */
  821         if (maxnipq == 0)
  822                 uma_zone_set_max(ipq_zone, 1);
  823 }
  824 
  825 static void
  826 ipq_zone_change(void *tag)
  827 {
  828 
  829         if (maxnipq > 0 && maxnipq < (nmbclusters / 32)) {
  830                 maxnipq = nmbclusters / 32;
  831                 maxnipq_update();
  832         }
  833 }
  834 
  835 static int
  836 sysctl_maxnipq(SYSCTL_HANDLER_ARGS)
  837 {
  838         int error, i;
  839 
  840         i = maxnipq;
  841         error = sysctl_handle_int(oidp, &i, 0, req);
  842         if (error || !req->newptr)
  843                 return (error);
  844 
  845         /*
  846          * XXXRW: Might be a good idea to sanity check the argument and place
  847          * an extreme upper bound.
  848          */
  849         if (i < -1)
  850                 return (EINVAL);
  851         maxnipq = i;
  852         maxnipq_update();
  853         return (0);
  854 }
  855 
  856 SYSCTL_PROC(_net_inet_ip, OID_AUTO, maxfragpackets, CTLTYPE_INT|CTLFLAG_RW,
  857     NULL, 0, sysctl_maxnipq, "I",
  858     "Maximum number of IPv4 fragment reassembly queue entries");
  859 
  860 /*
  861  * Take incoming datagram fragment and try to reassemble it into
  862  * whole datagram.  If the argument is the first fragment or one
  863  * in between the function will return NULL and store the mbuf
  864  * in the fragment chain.  If the argument is the last fragment
  865  * the packet will be reassembled and the pointer to the new
  866  * mbuf returned for further processing.  Only m_tags attached
  867  * to the first packet/fragment are preserved.
  868  * The IP header is *NOT* adjusted out of iplen.
  869  */
  870 
  871 struct mbuf *
  872 ip_reass(struct mbuf *m)
  873 {
  874         struct ip *ip;
  875         struct mbuf *p, *q, *nq, *t;
  876         struct ipq *fp = NULL;
  877         struct ipqhead *head;
  878         int i, hlen, next;
  879         u_int8_t ecn, ecn0;
  880         u_short hash;
  881 
  882         /* If maxnipq or maxfragsperpacket are 0, never accept fragments. */
  883         if (maxnipq == 0 || maxfragsperpacket == 0) {
  884                 ipstat.ips_fragments++;
  885                 ipstat.ips_fragdropped++;
  886                 m_freem(m);
  887                 return (NULL);
  888         }
  889 
  890         ip = mtod(m, struct ip *);
  891         hlen = ip->ip_hl << 2;
  892 
  893         hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
  894         head = &ipq[hash];
  895         IPQ_LOCK();
  896 
  897         /*
  898          * Look for queue of fragments
  899          * of this datagram.
  900          */
  901         TAILQ_FOREACH(fp, head, ipq_list)
  902                 if (ip->ip_id == fp->ipq_id &&
  903                     ip->ip_src.s_addr == fp->ipq_src.s_addr &&
  904                     ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
  905 #ifdef MAC
  906                     mac_fragment_match(m, fp) &&
  907 #endif
  908                     ip->ip_p == fp->ipq_p)
  909                         goto found;
  910 
  911         fp = NULL;
  912 
  913         /*
  914          * Attempt to trim the number of allocated fragment queues if it
  915          * exceeds the administrative limit.
  916          */
  917         if ((nipq > maxnipq) && (maxnipq > 0)) {
  918                 /*
  919                  * drop something from the tail of the current queue
  920                  * before proceeding further
  921                  */
  922                 struct ipq *q = TAILQ_LAST(head, ipqhead);
  923                 if (q == NULL) {   /* gak */
  924                         for (i = 0; i < IPREASS_NHASH; i++) {
  925                                 struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead);
  926                                 if (r) {
  927                                         ipstat.ips_fragtimeout += r->ipq_nfrags;
  928                                         ip_freef(&ipq[i], r);
  929                                         break;
  930                                 }
  931                         }
  932                 } else {
  933                         ipstat.ips_fragtimeout += q->ipq_nfrags;
  934                         ip_freef(head, q);
  935                 }
  936         }
  937 
  938 found:
  939         /*
  940          * Adjust ip_len to not reflect header,
  941          * convert offset of this to bytes.
  942          */
  943         ip->ip_len -= hlen;
  944         if (ip->ip_off & IP_MF) {
  945                 /*
  946                  * Make sure that fragments have a data length
  947                  * that's a non-zero multiple of 8 bytes.
  948                  */
  949                 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
  950                         ipstat.ips_toosmall++; /* XXX */
  951                         goto dropfrag;
  952                 }
  953                 m->m_flags |= M_FRAG;
  954         } else
  955                 m->m_flags &= ~M_FRAG;
  956         ip->ip_off <<= 3;
  957 
  958 
  959         /*
  960          * Attempt reassembly; if it succeeds, proceed.
  961          * ip_reass() will return a different mbuf.
  962          */
  963         ipstat.ips_fragments++;
  964         m->m_pkthdr.header = ip;
  965 
  966         /* Previous ip_reass() started here. */
  967         /*
  968          * Presence of header sizes in mbufs
  969          * would confuse code below.
  970          */
  971         m->m_data += hlen;
  972         m->m_len -= hlen;
  973 
  974         /*
  975          * If first fragment to arrive, create a reassembly queue.
  976          */
  977         if (fp == NULL) {
  978                 fp = uma_zalloc(ipq_zone, M_NOWAIT);
  979                 if (fp == NULL)
  980                         goto dropfrag;
  981 #ifdef MAC
  982                 if (mac_init_ipq(fp, M_NOWAIT) != 0) {
  983                         uma_zfree(ipq_zone, fp);
  984                         fp = NULL;
  985                         goto dropfrag;
  986                 }
  987                 mac_create_ipq(m, fp);
  988 #endif
  989                 TAILQ_INSERT_HEAD(head, fp, ipq_list);
  990                 nipq++;
  991                 fp->ipq_nfrags = 1;
  992                 fp->ipq_ttl = IPFRAGTTL;
  993                 fp->ipq_p = ip->ip_p;
  994                 fp->ipq_id = ip->ip_id;
  995                 fp->ipq_src = ip->ip_src;
  996                 fp->ipq_dst = ip->ip_dst;
  997                 fp->ipq_frags = m;
  998                 m->m_nextpkt = NULL;
  999                 goto done;
 1000         } else {
 1001                 fp->ipq_nfrags++;
 1002 #ifdef MAC
 1003                 mac_update_ipq(m, fp);
 1004 #endif
 1005         }
 1006 
 1007 #define GETIP(m)        ((struct ip*)((m)->m_pkthdr.header))
 1008 
 1009         /*
 1010          * Handle ECN by comparing this segment with the first one;
 1011          * if CE is set, do not lose CE.
 1012          * drop if CE and not-ECT are mixed for the same packet.
 1013          */
 1014         ecn = ip->ip_tos & IPTOS_ECN_MASK;
 1015         ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
 1016         if (ecn == IPTOS_ECN_CE) {
 1017                 if (ecn0 == IPTOS_ECN_NOTECT)
 1018                         goto dropfrag;
 1019                 if (ecn0 != IPTOS_ECN_CE)
 1020                         GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
 1021         }
 1022         if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
 1023                 goto dropfrag;
 1024 
 1025         /*
 1026          * Find a segment which begins after this one does.
 1027          */
 1028         for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
 1029                 if (GETIP(q)->ip_off > ip->ip_off)
 1030                         break;
 1031 
 1032         /*
 1033          * If there is a preceding segment, it may provide some of
 1034          * our data already.  If so, drop the data from the incoming
 1035          * segment.  If it provides all of our data, drop us, otherwise
 1036          * stick new segment in the proper place.
 1037          *
 1038          * If some of the data is dropped from the the preceding
 1039          * segment, then it's checksum is invalidated.
 1040          */
 1041         if (p) {
 1042                 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
 1043                 if (i > 0) {
 1044                         if (i >= ip->ip_len)
 1045                                 goto dropfrag;
 1046                         m_adj(m, i);
 1047                         m->m_pkthdr.csum_flags = 0;
 1048                         ip->ip_off += i;
 1049                         ip->ip_len -= i;
 1050                 }
 1051                 m->m_nextpkt = p->m_nextpkt;
 1052                 p->m_nextpkt = m;
 1053         } else {
 1054                 m->m_nextpkt = fp->ipq_frags;
 1055                 fp->ipq_frags = m;
 1056         }
 1057 
 1058         /*
 1059          * While we overlap succeeding segments trim them or,
 1060          * if they are completely covered, dequeue them.
 1061          */
 1062         for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
 1063              q = nq) {
 1064                 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
 1065                 if (i < GETIP(q)->ip_len) {
 1066                         GETIP(q)->ip_len -= i;
 1067                         GETIP(q)->ip_off += i;
 1068                         m_adj(q, i);
 1069                         q->m_pkthdr.csum_flags = 0;
 1070                         break;
 1071                 }
 1072                 nq = q->m_nextpkt;
 1073                 m->m_nextpkt = nq;
 1074                 ipstat.ips_fragdropped++;
 1075                 fp->ipq_nfrags--;
 1076                 m_freem(q);
 1077         }
 1078 
 1079         /*
 1080          * Check for complete reassembly and perform frag per packet
 1081          * limiting.
 1082          *
 1083          * Frag limiting is performed here so that the nth frag has
 1084          * a chance to complete the packet before we drop the packet.
 1085          * As a result, n+1 frags are actually allowed per packet, but
 1086          * only n will ever be stored. (n = maxfragsperpacket.)
 1087          *
 1088          */
 1089         next = 0;
 1090         for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
 1091                 if (GETIP(q)->ip_off != next) {
 1092                         if (fp->ipq_nfrags > maxfragsperpacket) {
 1093                                 ipstat.ips_fragdropped += fp->ipq_nfrags;
 1094                                 ip_freef(head, fp);
 1095                         }
 1096                         goto done;
 1097                 }
 1098                 next += GETIP(q)->ip_len;
 1099         }
 1100         /* Make sure the last packet didn't have the IP_MF flag */
 1101         if (p->m_flags & M_FRAG) {
 1102                 if (fp->ipq_nfrags > maxfragsperpacket) {
 1103                         ipstat.ips_fragdropped += fp->ipq_nfrags;
 1104                         ip_freef(head, fp);
 1105                 }
 1106                 goto done;
 1107         }
 1108 
 1109         /*
 1110          * Reassembly is complete.  Make sure the packet is a sane size.
 1111          */
 1112         q = fp->ipq_frags;
 1113         ip = GETIP(q);
 1114         if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
 1115                 ipstat.ips_toolong++;
 1116                 ipstat.ips_fragdropped += fp->ipq_nfrags;
 1117                 ip_freef(head, fp);
 1118                 goto done;
 1119         }
 1120 
 1121         /*
 1122          * Concatenate fragments.
 1123          */
 1124         m = q;
 1125         t = m->m_next;
 1126         m->m_next = NULL;
 1127         m_cat(m, t);
 1128         nq = q->m_nextpkt;
 1129         q->m_nextpkt = NULL;
 1130         for (q = nq; q != NULL; q = nq) {
 1131                 nq = q->m_nextpkt;
 1132                 q->m_nextpkt = NULL;
 1133                 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
 1134                 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
 1135                 m_cat(m, q);
 1136         }
 1137         /*
 1138          * In order to do checksumming faster we do 'end-around carry' here
 1139          * (and not in for{} loop), though it implies we are not going to
 1140          * reassemble more than 64k fragments.
 1141          */
 1142         m->m_pkthdr.csum_data =
 1143             (m->m_pkthdr.csum_data & 0xffff) + (m->m_pkthdr.csum_data >> 16);
 1144 #ifdef MAC
 1145         mac_create_datagram_from_ipq(fp, m);
 1146         mac_destroy_ipq(fp);
 1147 #endif
 1148 
 1149         /*
 1150          * Create header for new ip packet by modifying header of first
 1151          * packet;  dequeue and discard fragment reassembly header.
 1152          * Make header visible.
 1153          */
 1154         ip->ip_len = (ip->ip_hl << 2) + next;
 1155         ip->ip_src = fp->ipq_src;
 1156         ip->ip_dst = fp->ipq_dst;
 1157         TAILQ_REMOVE(head, fp, ipq_list);
 1158         nipq--;
 1159         uma_zfree(ipq_zone, fp);
 1160         m->m_len += (ip->ip_hl << 2);
 1161         m->m_data -= (ip->ip_hl << 2);
 1162         /* some debugging cruft by sklower, below, will go away soon */
 1163         if (m->m_flags & M_PKTHDR)      /* XXX this should be done elsewhere */
 1164                 m_fixhdr(m);
 1165         ipstat.ips_reassembled++;
 1166         IPQ_UNLOCK();
 1167         return (m);
 1168 
 1169 dropfrag:
 1170         ipstat.ips_fragdropped++;
 1171         if (fp != NULL)
 1172                 fp->ipq_nfrags--;
 1173         m_freem(m);
 1174 done:
 1175         IPQ_UNLOCK();
 1176         return (NULL);
 1177 
 1178 #undef GETIP
 1179 }
 1180 
 1181 /*
 1182  * Free a fragment reassembly header and all
 1183  * associated datagrams.
 1184  */
 1185 static void
 1186 ip_freef(fhp, fp)
 1187         struct ipqhead *fhp;
 1188         struct ipq *fp;
 1189 {
 1190         register struct mbuf *q;
 1191 
 1192         IPQ_LOCK_ASSERT();
 1193 
 1194         while (fp->ipq_frags) {
 1195                 q = fp->ipq_frags;
 1196                 fp->ipq_frags = q->m_nextpkt;
 1197                 m_freem(q);
 1198         }
 1199         TAILQ_REMOVE(fhp, fp, ipq_list);
 1200         uma_zfree(ipq_zone, fp);
 1201         nipq--;
 1202 }
 1203 
 1204 /*
 1205  * IP timer processing;
 1206  * if a timer expires on a reassembly
 1207  * queue, discard it.
 1208  */
 1209 void
 1210 ip_slowtimo()
 1211 {
 1212         register struct ipq *fp;
 1213         int i;
 1214 
 1215         IPQ_LOCK();
 1216         for (i = 0; i < IPREASS_NHASH; i++) {
 1217                 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
 1218                         struct ipq *fpp;
 1219 
 1220                         fpp = fp;
 1221                         fp = TAILQ_NEXT(fp, ipq_list);
 1222                         if(--fpp->ipq_ttl == 0) {
 1223                                 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
 1224                                 ip_freef(&ipq[i], fpp);
 1225                         }
 1226                 }
 1227         }
 1228         /*
 1229          * If we are over the maximum number of fragments
 1230          * (due to the limit being lowered), drain off
 1231          * enough to get down to the new limit.
 1232          */
 1233         if (maxnipq >= 0 && nipq > maxnipq) {
 1234                 for (i = 0; i < IPREASS_NHASH; i++) {
 1235                         while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
 1236                                 ipstat.ips_fragdropped +=
 1237                                     TAILQ_FIRST(&ipq[i])->ipq_nfrags;
 1238                                 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
 1239                         }
 1240                 }
 1241         }
 1242         IPQ_UNLOCK();
 1243 }
 1244 
 1245 /*
 1246  * Drain off all datagram fragments.
 1247  */
 1248 void
 1249 ip_drain()
 1250 {
 1251         int     i;
 1252 
 1253         IPQ_LOCK();
 1254         for (i = 0; i < IPREASS_NHASH; i++) {
 1255                 while(!TAILQ_EMPTY(&ipq[i])) {
 1256                         ipstat.ips_fragdropped +=
 1257                             TAILQ_FIRST(&ipq[i])->ipq_nfrags;
 1258                         ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
 1259                 }
 1260         }
 1261         IPQ_UNLOCK();
 1262         in_rtqdrain();
 1263 }
 1264 
 1265 /*
 1266  * The protocol to be inserted into ip_protox[] must be already registered
 1267  * in inetsw[], either statically or through pf_proto_register().
 1268  */
 1269 int
 1270 ipproto_register(u_char ipproto)
 1271 {
 1272         struct protosw *pr;
 1273 
 1274         /* Sanity checks. */
 1275         if (ipproto == 0)
 1276                 return (EPROTONOSUPPORT);
 1277 
 1278         /*
 1279          * The protocol slot must not be occupied by another protocol
 1280          * already.  An index pointing to IPPROTO_RAW is unused.
 1281          */
 1282         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
 1283         if (pr == NULL)
 1284                 return (EPFNOSUPPORT);
 1285         if (ip_protox[ipproto] != pr - inetsw)  /* IPPROTO_RAW */
 1286                 return (EEXIST);
 1287 
 1288         /* Find the protocol position in inetsw[] and set the index. */
 1289         for (pr = inetdomain.dom_protosw;
 1290              pr < inetdomain.dom_protoswNPROTOSW; pr++) {
 1291                 if (pr->pr_domain->dom_family == PF_INET &&
 1292                     pr->pr_protocol && pr->pr_protocol == ipproto) {
 1293                         /* Be careful to only index valid IP protocols. */
 1294                         if (pr->pr_protocol < IPPROTO_MAX) {
 1295                                 ip_protox[pr->pr_protocol] = pr - inetsw;
 1296                                 return (0);
 1297                         } else
 1298                                 return (EINVAL);
 1299                 }
 1300         }
 1301         return (EPROTONOSUPPORT);
 1302 }
 1303 
 1304 int
 1305 ipproto_unregister(u_char ipproto)
 1306 {
 1307         struct protosw *pr;
 1308 
 1309         /* Sanity checks. */
 1310         if (ipproto == 0)
 1311                 return (EPROTONOSUPPORT);
 1312 
 1313         /* Check if the protocol was indeed registered. */
 1314         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
 1315         if (pr == NULL)
 1316                 return (EPFNOSUPPORT);
 1317         if (ip_protox[ipproto] == pr - inetsw)  /* IPPROTO_RAW */
 1318                 return (ENOENT);
 1319 
 1320         /* Reset the protocol slot to IPPROTO_RAW. */
 1321         ip_protox[ipproto] = pr - inetsw;
 1322         return (0);
 1323 }
 1324 
 1325 
 1326 /*
 1327  * Do option processing on a datagram,
 1328  * possibly discarding it if bad options are encountered,
 1329  * or forwarding it if source-routed.
 1330  * The pass argument is used when operating in the IPSTEALTH
 1331  * mode to tell what options to process:
 1332  * [LS]SRR (pass 0) or the others (pass 1).
 1333  * The reason for as many as two passes is that when doing IPSTEALTH,
 1334  * non-routing options should be processed only if the packet is for us.
 1335  * Returns 1 if packet has been forwarded/freed,
 1336  * 0 if the packet should be processed further.
 1337  */
 1338 static int
 1339 ip_dooptions(struct mbuf *m, int pass)
 1340 {
 1341         struct ip *ip = mtod(m, struct ip *);
 1342         u_char *cp;
 1343         struct in_ifaddr *ia;
 1344         int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
 1345         struct in_addr *sin, dst;
 1346         n_time ntime;
 1347         struct  sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
 1348 
 1349         /* ignore or reject packets with IP options */
 1350         if (ip_doopts == 0)
 1351                 return 0;
 1352         else if (ip_doopts == 2) {
 1353                 type = ICMP_UNREACH;
 1354                 code = ICMP_UNREACH_FILTER_PROHIB;
 1355                 goto bad;
 1356         }
 1357 
 1358         dst = ip->ip_dst;
 1359         cp = (u_char *)(ip + 1);
 1360         cnt = (ip->ip_hl << 2) - sizeof (struct ip);
 1361         for (; cnt > 0; cnt -= optlen, cp += optlen) {
 1362                 opt = cp[IPOPT_OPTVAL];
 1363                 if (opt == IPOPT_EOL)
 1364                         break;
 1365                 if (opt == IPOPT_NOP)
 1366                         optlen = 1;
 1367                 else {
 1368                         if (cnt < IPOPT_OLEN + sizeof(*cp)) {
 1369                                 code = &cp[IPOPT_OLEN] - (u_char *)ip;
 1370                                 goto bad;
 1371                         }
 1372                         optlen = cp[IPOPT_OLEN];
 1373                         if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
 1374                                 code = &cp[IPOPT_OLEN] - (u_char *)ip;
 1375                                 goto bad;
 1376                         }
 1377                 }
 1378                 switch (opt) {
 1379 
 1380                 default:
 1381                         break;
 1382 
 1383                 /*
 1384                  * Source routing with record.
 1385                  * Find interface with current destination address.
 1386                  * If none on this machine then drop if strictly routed,
 1387                  * or do nothing if loosely routed.
 1388                  * Record interface address and bring up next address
 1389                  * component.  If strictly routed make sure next
 1390                  * address is on directly accessible net.
 1391                  */
 1392                 case IPOPT_LSRR:
 1393                 case IPOPT_SSRR:
 1394 #ifdef IPSTEALTH
 1395                         if (ipstealth && pass > 0)
 1396                                 break;
 1397 #endif
 1398                         if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
 1399                                 code = &cp[IPOPT_OLEN] - (u_char *)ip;
 1400                                 goto bad;
 1401                         }
 1402                         if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
 1403                                 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
 1404                                 goto bad;
 1405                         }
 1406                         ipaddr.sin_addr = ip->ip_dst;
 1407                         ia = (struct in_ifaddr *)
 1408                                 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
 1409                         if (ia == NULL) {
 1410                                 if (opt == IPOPT_SSRR) {
 1411                                         type = ICMP_UNREACH;
 1412                                         code = ICMP_UNREACH_SRCFAIL;
 1413                                         goto bad;
 1414                                 }
 1415                                 if (!ip_dosourceroute)
 1416                                         goto nosourcerouting;
 1417                                 /*
 1418                                  * Loose routing, and not at next destination
 1419                                  * yet; nothing to do except forward.
 1420                                  */
 1421                                 break;
 1422                         }
 1423                         off--;                  /* 0 origin */
 1424                         if (off > optlen - (int)sizeof(struct in_addr)) {
 1425                                 /*
 1426                                  * End of source route.  Should be for us.
 1427                                  */
 1428                                 if (!ip_acceptsourceroute)
 1429                                         goto nosourcerouting;
 1430                                 save_rte(m, cp, ip->ip_src);
 1431                                 break;
 1432                         }
 1433 #ifdef IPSTEALTH
 1434                         if (ipstealth)
 1435                                 goto dropit;
 1436 #endif
 1437                         if (!ip_dosourceroute) {
 1438                                 if (ipforwarding) {
 1439                                         char buf[16]; /* aaa.bbb.ccc.ddd\0 */
 1440                                         /*
 1441                                          * Acting as a router, so generate ICMP
 1442                                          */
 1443 nosourcerouting:
 1444                                         strcpy(buf, inet_ntoa(ip->ip_dst));
 1445                                         log(LOG_WARNING, 
 1446                                             "attempted source route from %s to %s\n",
 1447                                             inet_ntoa(ip->ip_src), buf);
 1448                                         type = ICMP_UNREACH;
 1449                                         code = ICMP_UNREACH_SRCFAIL;
 1450                                         goto bad;
 1451                                 } else {
 1452                                         /*
 1453                                          * Not acting as a router, so silently drop.
 1454                                          */
 1455 #ifdef IPSTEALTH
 1456 dropit:
 1457 #endif
 1458                                         ipstat.ips_cantforward++;
 1459                                         m_freem(m);
 1460                                         return (1);
 1461                                 }
 1462                         }
 1463 
 1464                         /*
 1465                          * locate outgoing interface
 1466                          */
 1467                         (void)memcpy(&ipaddr.sin_addr, cp + off,
 1468                             sizeof(ipaddr.sin_addr));
 1469 
 1470                         if (opt == IPOPT_SSRR) {
 1471 #define INA     struct in_ifaddr *
 1472 #define SA      struct sockaddr *
 1473                             if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL)
 1474                                 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
 1475                         } else
 1476                                 ia = ip_rtaddr(ipaddr.sin_addr);
 1477                         if (ia == NULL) {
 1478                                 type = ICMP_UNREACH;
 1479                                 code = ICMP_UNREACH_SRCFAIL;
 1480                                 goto bad;
 1481                         }
 1482                         ip->ip_dst = ipaddr.sin_addr;
 1483                         (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
 1484                             sizeof(struct in_addr));
 1485                         cp[IPOPT_OFFSET] += sizeof(struct in_addr);
 1486                         /*
 1487                          * Let ip_intr's mcast routing check handle mcast pkts
 1488                          */
 1489                         forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
 1490                         break;
 1491 
 1492                 case IPOPT_RR:
 1493 #ifdef IPSTEALTH
 1494                         if (ipstealth && pass == 0)
 1495                                 break;
 1496 #endif
 1497                         if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
 1498                                 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
 1499                                 goto bad;
 1500                         }
 1501                         if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
 1502                                 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
 1503                                 goto bad;
 1504                         }
 1505                         /*
 1506                          * If no space remains, ignore.
 1507                          */
 1508                         off--;                  /* 0 origin */
 1509                         if (off > optlen - (int)sizeof(struct in_addr))
 1510                                 break;
 1511                         (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
 1512                             sizeof(ipaddr.sin_addr));
 1513                         /*
 1514                          * locate outgoing interface; if we're the destination,
 1515                          * use the incoming interface (should be same).
 1516                          */
 1517                         if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL &&
 1518                             (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
 1519                                 type = ICMP_UNREACH;
 1520                                 code = ICMP_UNREACH_HOST;
 1521                                 goto bad;
 1522                         }
 1523                         (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
 1524                             sizeof(struct in_addr));
 1525                         cp[IPOPT_OFFSET] += sizeof(struct in_addr);
 1526                         break;
 1527 
 1528                 case IPOPT_TS:
 1529 #ifdef IPSTEALTH
 1530                         if (ipstealth && pass == 0)
 1531                                 break;
 1532 #endif
 1533                         code = cp - (u_char *)ip;
 1534                         if (optlen < 4 || optlen > 40) {
 1535                                 code = &cp[IPOPT_OLEN] - (u_char *)ip;
 1536                                 goto bad;
 1537                         }
 1538                         if ((off = cp[IPOPT_OFFSET]) < 5) {
 1539                                 code = &cp[IPOPT_OLEN] - (u_char *)ip;
 1540                                 goto bad;
 1541                         }
 1542                         if (off > optlen - (int)sizeof(int32_t)) {
 1543                                 cp[IPOPT_OFFSET + 1] += (1 << 4);
 1544                                 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
 1545                                         code = &cp[IPOPT_OFFSET] - (u_char *)ip;
 1546                                         goto bad;
 1547                                 }
 1548                                 break;
 1549                         }
 1550                         off--;                          /* 0 origin */
 1551                         sin = (struct in_addr *)(cp + off);
 1552                         switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
 1553 
 1554                         case IPOPT_TS_TSONLY:
 1555                                 break;
 1556 
 1557                         case IPOPT_TS_TSANDADDR:
 1558                                 if (off + sizeof(n_time) +
 1559                                     sizeof(struct in_addr) > optlen) {
 1560                                         code = &cp[IPOPT_OFFSET] - (u_char *)ip;
 1561                                         goto bad;
 1562                                 }
 1563                                 ipaddr.sin_addr = dst;
 1564                                 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
 1565                                                             m->m_pkthdr.rcvif);
 1566                                 if (ia == NULL)
 1567                                         continue;
 1568                                 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
 1569                                     sizeof(struct in_addr));
 1570                                 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
 1571                                 off += sizeof(struct in_addr);
 1572                                 break;
 1573 
 1574                         case IPOPT_TS_PRESPEC:
 1575                                 if (off + sizeof(n_time) +
 1576                                     sizeof(struct in_addr) > optlen) {
 1577                                         code = &cp[IPOPT_OFFSET] - (u_char *)ip;
 1578                                         goto bad;
 1579                                 }
 1580                                 (void)memcpy(&ipaddr.sin_addr, sin,
 1581                                     sizeof(struct in_addr));
 1582                                 if (ifa_ifwithaddr((SA)&ipaddr) == NULL)
 1583                                         continue;
 1584                                 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
 1585                                 off += sizeof(struct in_addr);
 1586                                 break;
 1587 
 1588                         default:
 1589                                 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
 1590                                 goto bad;
 1591                         }
 1592                         ntime = iptime();
 1593                         (void)memcpy(cp + off, &ntime, sizeof(n_time));
 1594                         cp[IPOPT_OFFSET] += sizeof(n_time);
 1595                 }
 1596         }
 1597         if (forward && ipforwarding) {
 1598                 ip_forward(m, 1);
 1599                 return (1);
 1600         }
 1601         return (0);
 1602 bad:
 1603         icmp_error(m, type, code, 0, 0);
 1604         ipstat.ips_badoptions++;
 1605         return (1);
 1606 }
 1607 
 1608 /*
 1609  * Given address of next destination (final or next hop),
 1610  * return internet address info of interface to be used to get there.
 1611  */
 1612 struct in_ifaddr *
 1613 ip_rtaddr(dst)
 1614         struct in_addr dst;
 1615 {
 1616         struct route sro;
 1617         struct sockaddr_in *sin;
 1618         struct in_ifaddr *ifa;
 1619 
 1620         bzero(&sro, sizeof(sro));
 1621         sin = (struct sockaddr_in *)&sro.ro_dst;
 1622         sin->sin_family = AF_INET;
 1623         sin->sin_len = sizeof(*sin);
 1624         sin->sin_addr = dst;
 1625         rtalloc_ign(&sro, RTF_CLONING);
 1626 
 1627         if (sro.ro_rt == NULL)
 1628                 return (NULL);
 1629 
 1630         ifa = ifatoia(sro.ro_rt->rt_ifa);
 1631         RTFREE(sro.ro_rt);
 1632         return (ifa);
 1633 }
 1634 
 1635 /*
 1636  * Save incoming source route for use in replies,
 1637  * to be picked up later by ip_srcroute if the receiver is interested.
 1638  */
 1639 static void
 1640 save_rte(m, option, dst)
 1641         struct mbuf *m;
 1642         u_char *option;
 1643         struct in_addr dst;
 1644 {
 1645         unsigned olen;
 1646         struct ipopt_tag *opts;
 1647 
 1648         opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
 1649                                         sizeof(struct ipopt_tag), M_NOWAIT);
 1650         if (opts == NULL)
 1651                 return;
 1652 
 1653         olen = option[IPOPT_OLEN];
 1654 #ifdef DIAGNOSTIC
 1655         if (ipprintfs)
 1656                 printf("save_rte: olen %d\n", olen);
 1657 #endif
 1658         if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
 1659                 m_tag_free((struct m_tag *)opts);
 1660                 return;
 1661         }
 1662         bcopy(option, opts->ip_srcrt.srcopt, olen);
 1663         opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
 1664         opts->ip_srcrt.dst = dst;
 1665         m_tag_prepend(m, (struct m_tag *)opts);
 1666 }
 1667 
 1668 /*
 1669  * Retrieve incoming source route for use in replies,
 1670  * in the same form used by setsockopt.
 1671  * The first hop is placed before the options, will be removed later.
 1672  */
 1673 struct mbuf *
 1674 ip_srcroute(m0)
 1675         struct mbuf *m0;
 1676 {
 1677         register struct in_addr *p, *q;
 1678         register struct mbuf *m;
 1679         struct ipopt_tag *opts;
 1680 
 1681         opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
 1682         if (opts == NULL)
 1683                 return (NULL);
 1684 
 1685         if (opts->ip_nhops == 0)
 1686                 return (NULL);
 1687         m = m_get(M_DONTWAIT, MT_DATA);
 1688         if (m == NULL)
 1689                 return (NULL);
 1690 
 1691 #define OPTSIZ  (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
 1692 
 1693         /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
 1694         m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
 1695             sizeof(struct in_addr) + OPTSIZ;
 1696 #ifdef DIAGNOSTIC
 1697         if (ipprintfs)
 1698                 printf("ip_srcroute: nhops %d mlen %d", opts->ip_nhops, m->m_len);
 1699 #endif
 1700 
 1701         /*
 1702          * First save first hop for return route
 1703          */
 1704         p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
 1705         *(mtod(m, struct in_addr *)) = *p--;
 1706 #ifdef DIAGNOSTIC
 1707         if (ipprintfs)
 1708                 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
 1709 #endif
 1710 
 1711         /*
 1712          * Copy option fields and padding (nop) to mbuf.
 1713          */
 1714         opts->ip_srcrt.nop = IPOPT_NOP;
 1715         opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
 1716         (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
 1717             &(opts->ip_srcrt.nop), OPTSIZ);
 1718         q = (struct in_addr *)(mtod(m, caddr_t) +
 1719             sizeof(struct in_addr) + OPTSIZ);
 1720 #undef OPTSIZ
 1721         /*
 1722          * Record return path as an IP source route,
 1723          * reversing the path (pointers are now aligned).
 1724          */
 1725         while (p >= opts->ip_srcrt.route) {
 1726 #ifdef DIAGNOSTIC
 1727                 if (ipprintfs)
 1728                         printf(" %lx", (u_long)ntohl(q->s_addr));
 1729 #endif
 1730                 *q++ = *p--;
 1731         }
 1732         /*
 1733          * Last hop goes to final destination.
 1734          */
 1735         *q = opts->ip_srcrt.dst;
 1736 #ifdef DIAGNOSTIC
 1737         if (ipprintfs)
 1738                 printf(" %lx\n", (u_long)ntohl(q->s_addr));
 1739 #endif
 1740         m_tag_delete(m0, (struct m_tag *)opts);
 1741         return (m);
 1742 }
 1743 
 1744 /*
 1745  * Strip out IP options, at higher
 1746  * level protocol in the kernel.
 1747  * Second argument is buffer to which options
 1748  * will be moved, and return value is their length.
 1749  * XXX should be deleted; last arg currently ignored.
 1750  */
 1751 void
 1752 ip_stripoptions(m, mopt)
 1753         register struct mbuf *m;
 1754         struct mbuf *mopt;
 1755 {
 1756         register int i;
 1757         struct ip *ip = mtod(m, struct ip *);
 1758         register caddr_t opts;
 1759         int olen;
 1760 
 1761         olen = (ip->ip_hl << 2) - sizeof (struct ip);
 1762         opts = (caddr_t)(ip + 1);
 1763         i = m->m_len - (sizeof (struct ip) + olen);
 1764         bcopy(opts + olen, opts, (unsigned)i);
 1765         m->m_len -= olen;
 1766         if (m->m_flags & M_PKTHDR)
 1767                 m->m_pkthdr.len -= olen;
 1768         ip->ip_v = IPVERSION;
 1769         ip->ip_hl = sizeof(struct ip) >> 2;
 1770 }
 1771 
 1772 u_char inetctlerrmap[PRC_NCMDS] = {
 1773         0,              0,              0,              0,
 1774         0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
 1775         EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
 1776         EMSGSIZE,       EHOSTUNREACH,   0,              0,
 1777         0,              0,              EHOSTUNREACH,   0,
 1778         ENOPROTOOPT,    ECONNREFUSED
 1779 };
 1780 
 1781 /*
 1782  * Forward a packet.  If some error occurs return the sender
 1783  * an icmp packet.  Note we can't always generate a meaningful
 1784  * icmp message because icmp doesn't have a large enough repertoire
 1785  * of codes and types.
 1786  *
 1787  * If not forwarding, just drop the packet.  This could be confusing
 1788  * if ipforwarding was zero but some routing protocol was advancing
 1789  * us as a gateway to somewhere.  However, we must let the routing
 1790  * protocol deal with that.
 1791  *
 1792  * The srcrt parameter indicates whether the packet is being forwarded
 1793  * via a source route.
 1794  */
 1795 void
 1796 ip_forward(struct mbuf *m, int srcrt)
 1797 {
 1798         struct ip *ip = mtod(m, struct ip *);
 1799         struct in_ifaddr *ia = NULL;
 1800         struct mbuf *mcopy;
 1801         struct in_addr dest;
 1802         int error, type = 0, code = 0, mtu = 0;
 1803 
 1804 #ifdef DIAGNOSTIC
 1805         if (ipprintfs)
 1806                 printf("forward: src %lx dst %lx ttl %x\n",
 1807                     (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
 1808                     ip->ip_ttl);
 1809 #endif
 1810 
 1811 
 1812         if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
 1813                 ipstat.ips_cantforward++;
 1814                 m_freem(m);
 1815                 return;
 1816         }
 1817 #ifdef IPSTEALTH
 1818         if (!ipstealth) {
 1819 #endif
 1820                 if (ip->ip_ttl <= IPTTLDEC) {
 1821                         icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
 1822                             0, 0);
 1823                         return;
 1824                 }
 1825 #ifdef IPSTEALTH
 1826         }
 1827 #endif
 1828 
 1829         ia = ip_rtaddr(ip->ip_dst);
 1830         if (!srcrt && ia == NULL) {
 1831                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
 1832                 return;
 1833         }
 1834 
 1835         /*
 1836          * Save the IP header and at most 8 bytes of the payload,
 1837          * in case we need to generate an ICMP message to the src.
 1838          *
 1839          * XXX this can be optimized a lot by saving the data in a local
 1840          * buffer on the stack (72 bytes at most), and only allocating the
 1841          * mbuf if really necessary. The vast majority of the packets
 1842          * are forwarded without having to send an ICMP back (either
 1843          * because unnecessary, or because rate limited), so we are
 1844          * really we are wasting a lot of work here.
 1845          *
 1846          * We don't use m_copy() because it might return a reference
 1847          * to a shared cluster. Both this function and ip_output()
 1848          * assume exclusive access to the IP header in `m', so any
 1849          * data in a cluster may change before we reach icmp_error().
 1850          */
 1851         MGET(mcopy, M_DONTWAIT, m->m_type);
 1852         if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
 1853                 /*
 1854                  * It's probably ok if the pkthdr dup fails (because
 1855                  * the deep copy of the tag chain failed), but for now
 1856                  * be conservative and just discard the copy since
 1857                  * code below may some day want the tags.
 1858                  */
 1859                 m_free(mcopy);
 1860                 mcopy = NULL;
 1861         }
 1862         if (mcopy != NULL) {
 1863                 mcopy->m_len = imin((ip->ip_hl << 2) + 8,
 1864                     (int)ip->ip_len);
 1865                 mcopy->m_pkthdr.len = mcopy->m_len;
 1866                 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
 1867         }
 1868 
 1869 #ifdef IPSTEALTH
 1870         if (!ipstealth) {
 1871 #endif
 1872                 ip->ip_ttl -= IPTTLDEC;
 1873 #ifdef IPSTEALTH
 1874         }
 1875 #endif
 1876 
 1877         /*
 1878          * If forwarding packet using same interface that it came in on,
 1879          * perhaps should send a redirect to sender to shortcut a hop.
 1880          * Only send redirect if source is sending directly to us,
 1881          * and if packet was not source routed (or has any options).
 1882          * Also, don't send redirect if forwarding using a default route
 1883          * or a route modified by a redirect.
 1884          */
 1885         dest.s_addr = 0;
 1886         if (!srcrt && ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
 1887                 struct sockaddr_in *sin;
 1888                 struct route ro;
 1889                 struct rtentry *rt;
 1890 
 1891                 bzero(&ro, sizeof(ro));
 1892                 sin = (struct sockaddr_in *)&ro.ro_dst;
 1893                 sin->sin_family = AF_INET;
 1894                 sin->sin_len = sizeof(*sin);
 1895                 sin->sin_addr = ip->ip_dst;
 1896                 rtalloc_ign(&ro, RTF_CLONING);
 1897 
 1898                 rt = ro.ro_rt;
 1899 
 1900                 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
 1901                     satosin(rt_key(rt))->sin_addr.s_addr != 0) {
 1902 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
 1903                         u_long src = ntohl(ip->ip_src.s_addr);
 1904 
 1905                         if (RTA(rt) &&
 1906                             (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
 1907                                 if (rt->rt_flags & RTF_GATEWAY)
 1908                                         dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
 1909                                 else
 1910                                         dest.s_addr = ip->ip_dst.s_addr;
 1911                                 /* Router requirements says to only send host redirects */
 1912                                 type = ICMP_REDIRECT;
 1913                                 code = ICMP_REDIRECT_HOST;
 1914 #ifdef DIAGNOSTIC
 1915                                 if (ipprintfs)
 1916                                         printf("redirect (%d) to %lx\n", code, (u_long)dest.s_addr);
 1917 #endif
 1918                         }
 1919                 }
 1920                 if (rt)
 1921                         RTFREE(rt);
 1922         }
 1923 
 1924         error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
 1925         if (error)
 1926                 ipstat.ips_cantforward++;
 1927         else {
 1928                 ipstat.ips_forward++;
 1929                 if (type)
 1930                         ipstat.ips_redirectsent++;
 1931                 else {
 1932                         if (mcopy)
 1933                                 m_freem(mcopy);
 1934                         return;
 1935                 }
 1936         }
 1937         if (mcopy == NULL)
 1938                 return;
 1939 
 1940         switch (error) {
 1941 
 1942         case 0:                         /* forwarded, but need redirect */
 1943                 /* type, code set above */
 1944                 break;
 1945 
 1946         case ENETUNREACH:               /* shouldn't happen, checked above */
 1947         case EHOSTUNREACH:
 1948         case ENETDOWN:
 1949         case EHOSTDOWN:
 1950         default:
 1951                 type = ICMP_UNREACH;
 1952                 code = ICMP_UNREACH_HOST;
 1953                 break;
 1954 
 1955         case EMSGSIZE:
 1956                 type = ICMP_UNREACH;
 1957                 code = ICMP_UNREACH_NEEDFRAG;
 1958 #if defined(IPSEC) || defined(FAST_IPSEC)
 1959                 /*
 1960                  * If the packet is routed over IPsec tunnel, tell the
 1961                  * originator the tunnel MTU.
 1962                  *      tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
 1963                  * XXX quickhack!!!
 1964                  */
 1965                 {
 1966                         struct secpolicy *sp = NULL;
 1967                         int ipsecerror;
 1968                         int ipsechdr;
 1969                         struct route *ro;
 1970 
 1971 #ifdef IPSEC
 1972                         sp = ipsec4_getpolicybyaddr(mcopy,
 1973                                                     IPSEC_DIR_OUTBOUND,
 1974                                                     IP_FORWARDING,
 1975                                                     &ipsecerror);
 1976 #else /* FAST_IPSEC */
 1977                         sp = ipsec_getpolicybyaddr(mcopy,
 1978                                                    IPSEC_DIR_OUTBOUND,
 1979                                                    IP_FORWARDING,
 1980                                                    &ipsecerror);
 1981 #endif
 1982                         if (sp != NULL) {
 1983                                 /* count IPsec header size */
 1984                                 ipsechdr = ipsec4_hdrsiz(mcopy,
 1985                                                          IPSEC_DIR_OUTBOUND,
 1986                                                          NULL);
 1987 
 1988                                 /*
 1989                                  * find the correct route for outer IPv4
 1990                                  * header, compute tunnel MTU.
 1991                                  */
 1992                                 if (sp->req != NULL
 1993                                  && sp->req->sav != NULL
 1994                                  && sp->req->sav->sah != NULL) {
 1995                                         ro = &sp->req->sav->sah->sa_route;
 1996                                         if (ro->ro_rt && ro->ro_rt->rt_ifp) {
 1997                                                 mtu =
 1998                                                     ro->ro_rt->rt_rmx.rmx_mtu ?
 1999                                                     ro->ro_rt->rt_rmx.rmx_mtu :
 2000                                                     ro->ro_rt->rt_ifp->if_mtu;
 2001                                                 mtu -= ipsechdr;
 2002                                         }
 2003                                 }
 2004 
 2005 #ifdef IPSEC
 2006                                 key_freesp(sp);
 2007 #else /* FAST_IPSEC */
 2008                                 KEY_FREESP(&sp);
 2009 #endif
 2010                         }
 2011                 }
 2012 #endif /*IPSEC || FAST_IPSEC*/
 2013                 /*
 2014                  * If the MTU wasn't set before use the interface mtu or
 2015                  * fall back to the next smaller mtu step compared to the
 2016                  * current packet size.
 2017                  */
 2018                 if (mtu == 0) {
 2019                         if (ia != NULL)
 2020                                 mtu = ia->ia_ifp->if_mtu;
 2021                         else
 2022                                 mtu = ip_next_mtu(ip->ip_len, 0);
 2023                 }
 2024                 ipstat.ips_cantfrag++;
 2025                 break;
 2026 
 2027         case ENOBUFS:
 2028                 /*
 2029                  * A router should not generate ICMP_SOURCEQUENCH as
 2030                  * required in RFC1812 Requirements for IP Version 4 Routers.
 2031                  * Source quench could be a big problem under DoS attacks,
 2032                  * or if the underlying interface is rate-limited.
 2033                  * Those who need source quench packets may re-enable them
 2034                  * via the net.inet.ip.sendsourcequench sysctl.
 2035                  */
 2036                 if (ip_sendsourcequench == 0) {
 2037                         m_freem(mcopy);
 2038                         return;
 2039                 } else {
 2040                         type = ICMP_SOURCEQUENCH;
 2041                         code = 0;
 2042                 }
 2043                 break;
 2044 
 2045         case EACCES:                    /* ipfw denied packet */
 2046                 m_freem(mcopy);
 2047                 return;
 2048         }
 2049         icmp_error(mcopy, type, code, dest.s_addr, mtu);
 2050 }
 2051 
 2052 void
 2053 ip_savecontrol(inp, mp, ip, m)
 2054         register struct inpcb *inp;
 2055         register struct mbuf **mp;
 2056         register struct ip *ip;
 2057         register struct mbuf *m;
 2058 {
 2059         if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
 2060                 struct bintime bt;
 2061 
 2062                 bintime(&bt);
 2063                 if (inp->inp_socket->so_options & SO_BINTIME) {
 2064                         *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
 2065                         SCM_BINTIME, SOL_SOCKET);
 2066                         if (*mp)
 2067                                 mp = &(*mp)->m_next;
 2068                 }
 2069                 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
 2070                         struct timeval tv;
 2071 
 2072                         bintime2timeval(&bt, &tv);
 2073                         *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
 2074                                 SCM_TIMESTAMP, SOL_SOCKET);
 2075                         if (*mp)
 2076                                 mp = &(*mp)->m_next;
 2077                 }
 2078         }
 2079         if (inp->inp_flags & INP_RECVDSTADDR) {
 2080                 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
 2081                     sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
 2082                 if (*mp)
 2083                         mp = &(*mp)->m_next;
 2084         }
 2085         if (inp->inp_flags & INP_RECVTTL) {
 2086                 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
 2087                     sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
 2088                 if (*mp)
 2089                         mp = &(*mp)->m_next;
 2090         }
 2091 #ifdef notyet
 2092         /* XXX
 2093          * Moving these out of udp_input() made them even more broken
 2094          * than they already were.
 2095          */
 2096         /* options were tossed already */
 2097         if (inp->inp_flags & INP_RECVOPTS) {
 2098                 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
 2099                     sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
 2100                 if (*mp)
 2101                         mp = &(*mp)->m_next;
 2102         }
 2103         /* ip_srcroute doesn't do what we want here, need to fix */
 2104         if (inp->inp_flags & INP_RECVRETOPTS) {
 2105                 *mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
 2106                     sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
 2107                 if (*mp)
 2108                         mp = &(*mp)->m_next;
 2109         }
 2110 #endif
 2111         if (inp->inp_flags & INP_RECVIF) {
 2112                 struct ifnet *ifp;
 2113                 struct sdlbuf {
 2114                         struct sockaddr_dl sdl;
 2115                         u_char  pad[32];
 2116                 } sdlbuf;
 2117                 struct sockaddr_dl *sdp;
 2118                 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
 2119 
 2120                 if (((ifp = m->m_pkthdr.rcvif)) 
 2121                 && ( ifp->if_index && (ifp->if_index <= if_index))) {
 2122                         sdp = (struct sockaddr_dl *)
 2123                             (ifaddr_byindex(ifp->if_index)->ifa_addr);
 2124                         /*
 2125                          * Change our mind and don't try copy.
 2126                          */
 2127                         if ((sdp->sdl_family != AF_LINK)
 2128                         || (sdp->sdl_len > sizeof(sdlbuf))) {
 2129                                 goto makedummy;
 2130                         }
 2131                         bcopy(sdp, sdl2, sdp->sdl_len);
 2132                 } else {
 2133 makedummy:      
 2134                         sdl2->sdl_len
 2135                                 = offsetof(struct sockaddr_dl, sdl_data[0]);
 2136                         sdl2->sdl_family = AF_LINK;
 2137                         sdl2->sdl_index = 0;
 2138                         sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
 2139                 }
 2140                 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
 2141                         IP_RECVIF, IPPROTO_IP);
 2142                 if (*mp)
 2143                         mp = &(*mp)->m_next;
 2144         }
 2145 }
 2146 
 2147 /*
 2148  * XXX these routines are called from the upper part of the kernel.
 2149  * They need to be locked when we remove Giant.
 2150  *
 2151  * They could also be moved to ip_mroute.c, since all the RSVP
 2152  *  handling is done there already.
 2153  */
 2154 static int ip_rsvp_on;
 2155 struct socket *ip_rsvpd;
 2156 int
 2157 ip_rsvp_init(struct socket *so)
 2158 {
 2159         if (so->so_type != SOCK_RAW ||
 2160             so->so_proto->pr_protocol != IPPROTO_RSVP)
 2161                 return EOPNOTSUPP;
 2162 
 2163         if (ip_rsvpd != NULL)
 2164                 return EADDRINUSE;
 2165 
 2166         ip_rsvpd = so;
 2167         /*
 2168          * This may seem silly, but we need to be sure we don't over-increment
 2169          * the RSVP counter, in case something slips up.
 2170          */
 2171         if (!ip_rsvp_on) {
 2172                 ip_rsvp_on = 1;
 2173                 rsvp_on++;
 2174         }
 2175 
 2176         return 0;
 2177 }
 2178 
 2179 int
 2180 ip_rsvp_done(void)
 2181 {
 2182         ip_rsvpd = NULL;
 2183         /*
 2184          * This may seem silly, but we need to be sure we don't over-decrement
 2185          * the RSVP counter, in case something slips up.
 2186          */
 2187         if (ip_rsvp_on) {
 2188                 ip_rsvp_on = 0;
 2189                 rsvp_on--;
 2190         }
 2191         return 0;
 2192 }
 2193 
 2194 void
 2195 rsvp_input(struct mbuf *m, int off)     /* XXX must fixup manually */
 2196 {
 2197         if (rsvp_input_p) { /* call the real one if loaded */
 2198                 rsvp_input_p(m, off);
 2199                 return;
 2200         }
 2201 
 2202         /* Can still get packets with rsvp_on = 0 if there is a local member
 2203          * of the group to which the RSVP packet is addressed.  But in this
 2204          * case we want to throw the packet away.
 2205          */
 2206         
 2207         if (!rsvp_on) {
 2208                 m_freem(m);
 2209                 return;
 2210         }
 2211 
 2212         if (ip_rsvpd != NULL) { 
 2213                 rip_input(m, off);
 2214                 return;
 2215         }
 2216         /* Drop the packet */
 2217         m_freem(m);
 2218 }

Cache object: 58f23684e6748df7c06f540dc3e0299a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.