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

Cache object: 8f517656d206fc6a1f86d2ef81e275b4


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