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

Cache object: 010f62eb64336ca74f6b43a12cce381b


[ 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.