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

Cache object: 2e13bef08b5ab038f962bc935a45ed3a


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