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_output.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, 1990, 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_output.c 8.3 (Berkeley) 1/21/94
   34  * $FreeBSD$
   35  */
   36 
   37 #define _IP_VHL
   38 
   39 #include "opt_ipfw.h"
   40 #include "opt_ipdn.h"
   41 #include "opt_ipdivert.h"
   42 #include "opt_ipfilter.h"
   43 #include "opt_ipsec.h"
   44 #include "opt_random_ip_id.h"
   45 #include "opt_mbuf_stress_test.h"
   46 
   47 #include <sys/param.h>
   48 #include <sys/systm.h>
   49 #include <sys/kernel.h>
   50 #include <sys/malloc.h>
   51 #include <sys/mbuf.h>
   52 #include <sys/protosw.h>
   53 #include <sys/socket.h>
   54 #include <sys/socketvar.h>
   55 #include <sys/proc.h>
   56 #include <sys/sysctl.h>
   57 
   58 #include <net/if.h>
   59 #include <net/route.h>
   60 
   61 #include <netinet/in.h>
   62 #include <netinet/in_systm.h>
   63 #include <netinet/ip.h>
   64 #include <netinet/in_pcb.h>
   65 #include <netinet/in_var.h>
   66 #include <netinet/ip_var.h>
   67 
   68 #include <machine/in_cksum.h>
   69 
   70 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
   71 
   72 #ifdef IPSEC
   73 #include <netinet6/ipsec.h>
   74 #include <netkey/key.h>
   75 #ifdef IPSEC_DEBUG
   76 #include <netkey/key_debug.h>
   77 #else
   78 #define KEYDEBUG(lev,arg)
   79 #endif
   80 #endif /*IPSEC*/
   81 
   82 #ifdef FAST_IPSEC
   83 #include <netipsec/ipsec.h>
   84 #include <netipsec/xform.h>
   85 #include <netipsec/key.h>
   86 #endif /*FAST_IPSEC*/
   87 
   88 #include <netinet/ip_fw.h>
   89 #include <netinet/ip_dummynet.h>
   90 
   91 #define print_ip(x, a, y)        printf("%s %d.%d.%d.%d%s",\
   92                                 x, (ntohl(a.s_addr)>>24)&0xFF,\
   93                                   (ntohl(a.s_addr)>>16)&0xFF,\
   94                                   (ntohl(a.s_addr)>>8)&0xFF,\
   95                                   (ntohl(a.s_addr))&0xFF, y);
   96 
   97 u_short ip_id;
   98 
   99 #ifdef MBUF_STRESS_TEST
  100 int mbuf_frag_size = 0;
  101 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
  102         &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
  103 #endif
  104 
  105 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
  106 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
  107 static void     ip_mloopback
  108         (struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
  109 static int      ip_getmoptions
  110         (struct sockopt *, struct ip_moptions *);
  111 static int      ip_pcbopts(int, struct mbuf **, struct mbuf *);
  112 static int      ip_setmoptions
  113         (struct sockopt *, struct ip_moptions **);
  114 
  115 int     ip_optcopy(struct ip *, struct ip *);
  116 extern int (*fr_checkp) (struct ip *, int, struct ifnet *, int, struct mbuf **);
  117 
  118 
  119 extern  struct protosw inetsw[];
  120 
  121 /*
  122  * IP output.  The packet in mbuf chain m contains a skeletal IP
  123  * header (with len, off, ttl, proto, tos, src, dst).
  124  * The mbuf chain containing the packet will be freed.
  125  * The mbuf opt, if present, will not be freed.
  126  */
  127 int
  128 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro,
  129         int flags, struct ip_moptions *imo, struct inpcb *inp)
  130 {
  131         struct ip *ip;
  132         struct ifnet *ifp = NULL;       /* keep compiler happy */
  133         struct mbuf *m;
  134         int hlen = sizeof (struct ip);
  135         int len, off, error = 0;
  136         struct sockaddr_in *dst = NULL; /* keep compiler happy */
  137         struct in_ifaddr *ia = NULL;
  138         int isbroadcast, sw_csum;
  139         struct in_addr pkt_dst;
  140 #ifdef IPSEC
  141         struct route iproute;
  142         struct secpolicy *sp = NULL;
  143         struct socket *so = inp ? inp->inp_socket : NULL;
  144 #endif
  145 #ifdef FAST_IPSEC
  146         struct route iproute;
  147         struct m_tag *mtag;
  148         struct secpolicy *sp = NULL;
  149         struct tdb_ident *tdbi;
  150         int s;
  151 #endif /* FAST_IPSEC */
  152         struct ip_fw_args args;
  153         int src_was_INADDR_ANY = 0;     /* as the name says... */
  154 
  155         args.eh = NULL;
  156         args.rule = NULL;
  157         args.next_hop = NULL;
  158         args.divert_rule = 0;                   /* divert cookie */
  159 
  160         /* Grab info from MT_TAG mbufs prepended to the chain. */
  161         for (; m0 && m0->m_type == MT_TAG; m0 = m0->m_next) {
  162                 switch(m0->_m_tag_id) {
  163                 default:
  164                         printf("ip_output: unrecognised MT_TAG tag %d\n",
  165                             m0->_m_tag_id);
  166                         break;
  167 
  168                 case PACKET_TAG_DUMMYNET:
  169                         /*
  170                          * the packet was already tagged, so part of the
  171                          * processing was already done, and we need to go down.
  172                          * Get parameters from the header.
  173                          */
  174                         args.rule = ((struct dn_pkt *)m0)->rule;
  175                         opt = NULL ;
  176                         ro = & ( ((struct dn_pkt *)m0)->ro ) ;
  177                         imo = NULL ;
  178                         dst = ((struct dn_pkt *)m0)->dn_dst ;
  179                         ifp = ((struct dn_pkt *)m0)->ifp ;
  180                         flags = ((struct dn_pkt *)m0)->flags ;
  181                         break;
  182 
  183                 case PACKET_TAG_DIVERT:
  184                         args.divert_rule = (int)m0->m_data & 0xffff;
  185                         break;
  186 
  187                 case PACKET_TAG_IPFORWARD:
  188                         args.next_hop = (struct sockaddr_in *)m0->m_data;
  189                         break;
  190                 }
  191         }
  192         m = m0;
  193 
  194         KASSERT(!m || (m->m_flags & M_PKTHDR) != 0, ("ip_output: no HDR"));
  195 #ifndef FAST_IPSEC
  196         KASSERT(ro != NULL, ("ip_output: no route, proto %d",
  197             mtod(m, struct ip *)->ip_p));
  198 #endif
  199 
  200         if (args.rule != NULL) {        /* dummynet already saw us */
  201                 ip = mtod(m, struct ip *);
  202                 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
  203                 if (ro->ro_rt)
  204                         ia = ifatoia(ro->ro_rt->rt_ifa);
  205                 goto sendit;
  206         }
  207 
  208         if (opt) {
  209                 len = 0;
  210                 m = ip_insertoptions(m, opt, &len);
  211                 if (len != 0)
  212                         hlen = len;
  213         }
  214         ip = mtod(m, struct ip *);
  215         pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst;
  216 
  217         /*
  218          * Fill in IP header.
  219          */
  220         if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
  221                 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
  222                 ip->ip_off &= IP_DF;
  223 #ifdef RANDOM_IP_ID
  224                 ip->ip_id = ip_randomid();
  225 #else
  226                 ip->ip_id = htons(ip_id++);
  227 #endif
  228                 ipstat.ips_localout++;
  229         } else {
  230                 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
  231         }
  232 
  233 #ifdef FAST_IPSEC
  234         if (ro == NULL) {
  235                 ro = &iproute;
  236                 bzero(ro, sizeof (*ro));
  237         }
  238 #endif /* FAST_IPSEC */
  239         dst = (struct sockaddr_in *)&ro->ro_dst;
  240         /*
  241          * If there is a cached route,
  242          * check that it is to the same destination
  243          * and is still up.  If not, free it and try again.
  244          * The address family should also be checked in case of sharing the
  245          * cache with IPv6.
  246          */
  247         if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
  248                           dst->sin_family != AF_INET ||
  249                           dst->sin_addr.s_addr != pkt_dst.s_addr)) {
  250                 RTFREE(ro->ro_rt);
  251                 ro->ro_rt = (struct rtentry *)0;
  252         }
  253         if (ro->ro_rt == 0) {
  254                 bzero(dst, sizeof(*dst));
  255                 dst->sin_family = AF_INET;
  256                 dst->sin_len = sizeof(*dst);
  257                 dst->sin_addr = pkt_dst;
  258         }
  259         /*
  260          * If routing to interface only,
  261          * short circuit routing lookup.
  262          */
  263         if (flags & IP_ROUTETOIF) {
  264                 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
  265                     (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
  266                         ipstat.ips_noroute++;
  267                         error = ENETUNREACH;
  268                         goto bad;
  269                 }
  270                 ifp = ia->ia_ifp;
  271                 ip->ip_ttl = 1;
  272                 isbroadcast = in_broadcast(dst->sin_addr, ifp);
  273         } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
  274             imo != NULL && imo->imo_multicast_ifp != NULL) {
  275                 /*
  276                  * Bypass the normal routing lookup for multicast
  277                  * packets if the interface is specified.
  278                  */
  279                 ifp = imo->imo_multicast_ifp;
  280                 IFP_TO_IA(ifp, ia);
  281                 isbroadcast = 0;        /* fool gcc */
  282         } else {
  283                 /*
  284                  * If this is the case, we probably don't want to allocate
  285                  * a protocol-cloned route since we didn't get one from the
  286                  * ULP.  This lets TCP do its thing, while not burdening
  287                  * forwarding or ICMP with the overhead of cloning a route.
  288                  * Of course, we still want to do any cloning requested by
  289                  * the link layer, as this is probably required in all cases
  290                  * for correct operation (as it is for ARP).
  291                  */
  292                 if (ro->ro_rt == 0)
  293                         rtalloc_ign(ro, RTF_PRCLONING);
  294                 if (ro->ro_rt == 0) {
  295                         ipstat.ips_noroute++;
  296                         error = EHOSTUNREACH;
  297                         goto bad;
  298                 }
  299                 ia = ifatoia(ro->ro_rt->rt_ifa);
  300                 ifp = ro->ro_rt->rt_ifp;
  301                 ro->ro_rt->rt_use++;
  302                 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
  303                         dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
  304                 if (ro->ro_rt->rt_flags & RTF_HOST)
  305                         isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
  306                 else
  307                         isbroadcast = in_broadcast(dst->sin_addr, ifp);
  308         }
  309         if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
  310                 struct in_multi *inm;
  311 
  312                 m->m_flags |= M_MCAST;
  313                 /*
  314                  * IP destination address is multicast.  Make sure "dst"
  315                  * still points to the address in "ro".  (It may have been
  316                  * changed to point to a gateway address, above.)
  317                  */
  318                 dst = (struct sockaddr_in *)&ro->ro_dst;
  319                 /*
  320                  * See if the caller provided any multicast options
  321                  */
  322                 if (imo != NULL) {
  323                         ip->ip_ttl = imo->imo_multicast_ttl;
  324                         if (imo->imo_multicast_vif != -1)
  325                                 ip->ip_src.s_addr =
  326                                     ip_mcast_src ?
  327                                     ip_mcast_src(imo->imo_multicast_vif) :
  328                                     INADDR_ANY;
  329                 } else
  330                         ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
  331                 /*
  332                  * Confirm that the outgoing interface supports multicast.
  333                  */
  334                 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
  335                         if ((ifp->if_flags & IFF_MULTICAST) == 0) {
  336                                 ipstat.ips_noroute++;
  337                                 error = ENETUNREACH;
  338                                 goto bad;
  339                         }
  340                 }
  341                 /*
  342                  * If source address not specified yet, use address
  343                  * of outgoing interface.
  344                  */
  345                 if (ip->ip_src.s_addr == INADDR_ANY) {
  346                         /* Interface may have no addresses. */
  347                         if (ia != NULL)
  348                                 ip->ip_src = IA_SIN(ia)->sin_addr;
  349                 }
  350 
  351                 IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
  352                 if (inm != NULL &&
  353                    (imo == NULL || imo->imo_multicast_loop)) {
  354                         /*
  355                          * If we belong to the destination multicast group
  356                          * on the outgoing interface, and the caller did not
  357                          * forbid loopback, loop back a copy.
  358                          */
  359                         ip_mloopback(ifp, m, dst, hlen);
  360                 }
  361                 else {
  362                         /*
  363                          * If we are acting as a multicast router, perform
  364                          * multicast forwarding as if the packet had just
  365                          * arrived on the interface to which we are about
  366                          * to send.  The multicast forwarding function
  367                          * recursively calls this function, using the
  368                          * IP_FORWARDING flag to prevent infinite recursion.
  369                          *
  370                          * Multicasts that are looped back by ip_mloopback(),
  371                          * above, will be forwarded by the ip_input() routine,
  372                          * if necessary.
  373                          */
  374                         if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
  375                                 /*
  376                                  * If rsvp daemon is not running, do not
  377                                  * set ip_moptions. This ensures that the packet
  378                                  * is multicast and not just sent down one link
  379                                  * as prescribed by rsvpd.
  380                                  */
  381                                 if (!rsvp_on)
  382                                         imo = NULL;
  383                                 if (ip_mforward &&
  384                                     ip_mforward(ip, ifp, m, imo) != 0) {
  385                                         m_freem(m);
  386                                         goto done;
  387                                 }
  388                         }
  389                 }
  390 
  391                 /*
  392                  * Multicasts with a time-to-live of zero may be looped-
  393                  * back, above, but must not be transmitted on a network.
  394                  * Also, multicasts addressed to the loopback interface
  395                  * are not sent -- the above call to ip_mloopback() will
  396                  * loop back a copy if this host actually belongs to the
  397                  * destination group on the loopback interface.
  398                  */
  399                 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
  400                         m_freem(m);
  401                         goto done;
  402                 }
  403 
  404                 goto sendit;
  405         }
  406 #ifndef notdef
  407         /*
  408          * If the source address is not specified yet, use the address
  409          * of the outoing interface. In case, keep note we did that, so
  410          * if the the firewall changes the next-hop causing the output
  411          * interface to change, we can fix that.
  412          */
  413         if (ip->ip_src.s_addr == INADDR_ANY) {
  414                 /* Interface may have no addresses. */
  415                 if (ia != NULL) {
  416                         ip->ip_src = IA_SIN(ia)->sin_addr;
  417                         src_was_INADDR_ANY = 1;
  418                 }
  419         }
  420 #endif /* notdef */
  421         /*
  422          * Verify that we have any chance at all of being able to queue
  423          *      the packet or packet fragments
  424          */
  425         if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
  426                 ifp->if_snd.ifq_maxlen) {
  427                         error = ENOBUFS;
  428                         ipstat.ips_odropped++;
  429                         goto bad;
  430         }
  431 
  432         /*
  433          * Look for broadcast address and
  434          * verify user is allowed to send
  435          * such a packet.
  436          */
  437         if (isbroadcast) {
  438                 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
  439                         error = EADDRNOTAVAIL;
  440                         goto bad;
  441                 }
  442                 if ((flags & IP_ALLOWBROADCAST) == 0) {
  443                         error = EACCES;
  444                         goto bad;
  445                 }
  446                 /* don't allow broadcast messages to be fragmented */
  447                 if (ip->ip_len > ifp->if_mtu) {
  448                         error = EMSGSIZE;
  449                         goto bad;
  450                 }
  451                 if (flags & IP_SENDONES)
  452                         ip->ip_dst.s_addr = INADDR_BROADCAST;
  453                 m->m_flags |= M_BCAST;
  454         } else {
  455                 m->m_flags &= ~M_BCAST;
  456         }
  457 
  458 sendit:
  459 #ifdef IPSEC
  460         /* get SP for this packet */
  461         if (so == NULL)
  462                 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
  463         else
  464                 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
  465 
  466         if (sp == NULL) {
  467                 ipsecstat.out_inval++;
  468                 goto bad;
  469         }
  470 
  471         error = 0;
  472 
  473         /* check policy */
  474         switch (sp->policy) {
  475         case IPSEC_POLICY_DISCARD:
  476                 /*
  477                  * This packet is just discarded.
  478                  */
  479                 ipsecstat.out_polvio++;
  480                 goto bad;
  481 
  482         case IPSEC_POLICY_BYPASS:
  483         case IPSEC_POLICY_NONE:
  484         case IPSEC_POLICY_TCP:
  485                 /* no need to do IPsec. */
  486                 goto skip_ipsec;
  487         
  488         case IPSEC_POLICY_IPSEC:
  489                 if (sp->req == NULL) {
  490                         /* acquire a policy */
  491                         error = key_spdacquire(sp);
  492                         goto bad;
  493                 }
  494                 break;
  495 
  496         case IPSEC_POLICY_ENTRUST:
  497         default:
  498                 printf("ip_output: Invalid policy found. %d\n", sp->policy);
  499         }
  500     {
  501         struct ipsec_output_state state;
  502         bzero(&state, sizeof(state));
  503         state.m = m;
  504         if (flags & IP_ROUTETOIF) {
  505                 state.ro = &iproute;
  506                 bzero(&iproute, sizeof(iproute));
  507         } else
  508                 state.ro = ro;
  509         state.dst = (struct sockaddr *)dst;
  510 
  511         ip->ip_sum = 0;
  512 
  513         /*
  514          * XXX
  515          * delayed checksums are not currently compatible with IPsec
  516          */
  517         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  518                 in_delayed_cksum(m);
  519                 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
  520         }
  521 
  522         ip->ip_len = htons(ip->ip_len);
  523         ip->ip_off = htons(ip->ip_off);
  524 
  525         error = ipsec4_output(&state, sp, flags);
  526 
  527         m = state.m;
  528         if (flags & IP_ROUTETOIF) {
  529                 /*
  530                  * if we have tunnel mode SA, we may need to ignore
  531                  * IP_ROUTETOIF.
  532                  */
  533                 if (state.ro != &iproute || state.ro->ro_rt != NULL) {
  534                         flags &= ~IP_ROUTETOIF;
  535                         ro = state.ro;
  536                 }
  537         } else
  538                 ro = state.ro;
  539         dst = (struct sockaddr_in *)state.dst;
  540         if (error) {
  541                 /* mbuf is already reclaimed in ipsec4_output. */
  542                 m0 = NULL;
  543                 switch (error) {
  544                 case EHOSTUNREACH:
  545                 case ENETUNREACH:
  546                 case EMSGSIZE:
  547                 case ENOBUFS:
  548                 case ENOMEM:
  549                         break;
  550                 default:
  551                         printf("ip4_output (ipsec): error code %d\n", error);
  552                         /*fall through*/
  553                 case ENOENT:
  554                         /* don't show these error codes to the user */
  555                         error = 0;
  556                         break;
  557                 }
  558                 goto bad;
  559         }
  560     }
  561 
  562         /* be sure to update variables that are affected by ipsec4_output() */
  563         ip = mtod(m, struct ip *);
  564 #ifdef _IP_VHL
  565         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
  566 #else
  567         hlen = ip->ip_hl << 2;
  568 #endif
  569         if (ro->ro_rt == NULL) {
  570                 if ((flags & IP_ROUTETOIF) == 0) {
  571                         printf("ip_output: "
  572                                 "can't update route after IPsec processing\n");
  573                         error = EHOSTUNREACH;   /*XXX*/
  574                         goto bad;
  575                 }
  576         } else {
  577                 ia = ifatoia(ro->ro_rt->rt_ifa);
  578                 ifp = ro->ro_rt->rt_ifp;
  579         }
  580 
  581         /* make it flipped, again. */
  582         ip->ip_len = ntohs(ip->ip_len);
  583         ip->ip_off = ntohs(ip->ip_off);
  584 skip_ipsec:
  585 #endif /*IPSEC*/
  586 #ifdef FAST_IPSEC
  587         /*
  588          * Check the security policy (SP) for the packet and, if
  589          * required, do IPsec-related processing.  There are two
  590          * cases here; the first time a packet is sent through
  591          * it will be untagged and handled by ipsec4_checkpolicy.
  592          * If the packet is resubmitted to ip_output (e.g. after
  593          * AH, ESP, etc. processing), there will be a tag to bypass
  594          * the lookup and related policy checking.
  595          */
  596         mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
  597         s = splnet();
  598         if (mtag != NULL) {
  599                 tdbi = (struct tdb_ident *)(mtag + 1);
  600                 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
  601                 if (sp == NULL)
  602                         error = -EINVAL;        /* force silent drop */
  603                 m_tag_delete(m, mtag);
  604         } else {
  605                 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
  606                                         &error, inp);
  607         }
  608         /*
  609          * There are four return cases:
  610          *    sp != NULL                    apply IPsec policy
  611          *    sp == NULL, error == 0        no IPsec handling needed
  612          *    sp == NULL, error == -EINVAL  discard packet w/o error
  613          *    sp == NULL, error != 0        discard packet, report error
  614          */
  615         if (sp != NULL) {
  616                 /* Loop detection, check if ipsec processing already done */
  617                 KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
  618                 for (mtag = m_tag_first(m); mtag != NULL;
  619                      mtag = m_tag_next(m, mtag)) {
  620                         if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
  621                                 continue;
  622                         if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
  623                             mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
  624                                 continue;
  625                         /*
  626                          * Check if policy has an SA associated with it.
  627                          * This can happen when an SP has yet to acquire
  628                          * an SA; e.g. on first reference.  If it occurs,
  629                          * then we let ipsec4_process_packet do its thing.
  630                          */
  631                         if (sp->req->sav == NULL)
  632                                 break;
  633                         tdbi = (struct tdb_ident *)(mtag + 1);
  634                         if (tdbi->spi == sp->req->sav->spi &&
  635                             tdbi->proto == sp->req->sav->sah->saidx.proto &&
  636                             bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
  637                                  sizeof (union sockaddr_union)) == 0) {
  638                                 /*
  639                                  * No IPsec processing is needed, free
  640                                  * reference to SP.
  641                                  *
  642                                  * NB: null pointer to avoid free at
  643                                  *     done: below.
  644                                  */
  645                                 KEY_FREESP(&sp), sp = NULL;
  646                                 splx(s);
  647                                 goto spd_done;
  648                         }
  649                 }
  650 
  651                 /*
  652                  * Do delayed checksums now because we send before
  653                  * this is done in the normal processing path.
  654                  */
  655                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  656                         in_delayed_cksum(m);
  657                         m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
  658                 }
  659 
  660                 ip->ip_len = htons(ip->ip_len);
  661                 ip->ip_off = htons(ip->ip_off);
  662 
  663                 /* NB: callee frees mbuf */
  664                 error = ipsec4_process_packet(m, sp->req, flags, 0);
  665                 /*
  666                  * Preserve KAME behaviour: ENOENT can be returned
  667                  * when an SA acquire is in progress.  Don't propagate
  668                  * this to user-level; it confuses applications.
  669                  *
  670                  * XXX this will go away when the SADB is redone.
  671                  */
  672                 if (error == ENOENT)
  673                         error = 0;
  674                 splx(s);
  675                 goto done;
  676         } else {
  677                 splx(s);
  678 
  679                 if (error != 0) {
  680                         /*
  681                          * Hack: -EINVAL is used to signal that a packet
  682                          * should be silently discarded.  This is typically
  683                          * because we asked key management for an SA and
  684                          * it was delayed (e.g. kicked up to IKE).
  685                          */
  686                         if (error == -EINVAL)
  687                                 error = 0;
  688                         goto bad;
  689                 } else {
  690                         /* No IPsec processing for this packet. */
  691                 }
  692 #ifdef notyet
  693                 /*
  694                  * If deferred crypto processing is needed, check that
  695                  * the interface supports it.
  696                  */ 
  697                 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
  698                 if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
  699                         /* notify IPsec to do its own crypto */
  700                         ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
  701                         error = EHOSTUNREACH;
  702                         goto bad;
  703                 }
  704 #endif
  705         }
  706 spd_done:
  707 #endif /* FAST_IPSEC */
  708         /*
  709          * IpHack's section.
  710          * - Xlate: translate packet's addr/port (NAT).
  711          * - Firewall: deny/allow/etc.
  712          * - Wrap: fake packet's addr/port <unimpl.>
  713          * - Encapsulate: put it in another IP and send out. <unimp.>
  714          */ 
  715         if (fr_checkp) {
  716                 struct  mbuf    *m1 = m;
  717 
  718                 if ((error = (*fr_checkp)(ip, hlen, ifp, 1, &m1)) || !m1)
  719                         goto done;
  720                 ip = mtod(m = m1, struct ip *);
  721         }
  722 
  723         /*
  724          * Check with the firewall...
  725          * but not if we are already being fwd'd from a firewall.
  726          */
  727         if (fw_enable && IPFW_LOADED && !args.next_hop) {
  728                 struct sockaddr_in *old = dst;
  729 
  730                 args.m = m;
  731                 args.next_hop = dst;
  732                 args.oif = ifp;
  733                 off = ip_fw_chk_ptr(&args);
  734                 m = args.m;
  735                 dst = args.next_hop;
  736 
  737                 /*
  738                  * On return we must do the following:
  739                  * m == NULL    -> drop the pkt (old interface, deprecated)
  740                  * (off & IP_FW_PORT_DENY_FLAG) -> drop the pkt (new interface)
  741                  * 1<=off<= 0xffff              -> DIVERT
  742                  * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe
  743                  * (off & IP_FW_PORT_TEE_FLAG)  -> TEE the packet
  744                  * dst != old                   -> IPFIREWALL_FORWARD
  745                  * off==0, dst==old             -> accept
  746                  * If some of the above modules are not compiled in, then
  747                  * we should't have to check the corresponding condition
  748                  * (because the ipfw control socket should not accept
  749                  * unsupported rules), but better play safe and drop
  750                  * packets in case of doubt.
  751                  */
  752                 if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
  753                         if (m)
  754                                 m_freem(m);
  755                         error = EACCES;
  756                         goto done;
  757                 }
  758                 ip = mtod(m, struct ip *);
  759                 if (off == 0 && dst == old)             /* common case */
  760                         goto pass;
  761                 if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) {
  762                         /*
  763                          * pass the pkt to dummynet. Need to include
  764                          * pipe number, m, ifp, ro, dst because these are
  765                          * not recomputed in the next pass.
  766                          * All other parameters have been already used and
  767                          * so they are not needed anymore. 
  768                          * XXX note: if the ifp or ro entry are deleted
  769                          * while a pkt is in dummynet, we are in trouble!
  770                          */ 
  771                         args.ro = ro;
  772                         args.dst = dst;
  773                         args.flags = flags;
  774 
  775                         error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT,
  776                                 &args);
  777                         goto done;
  778                 }
  779 #ifdef IPDIVERT
  780                 if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
  781                         struct mbuf *clone = NULL;
  782 
  783                         /* Clone packet if we're doing a 'tee' */
  784                         if ((off & IP_FW_PORT_TEE_FLAG) != 0)
  785                                 clone = m_dup(m, M_DONTWAIT);
  786 
  787                         /*
  788                          * XXX
  789                          * delayed checksums are not currently compatible
  790                          * with divert sockets.
  791                          */
  792                         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  793                                 in_delayed_cksum(m);
  794                                 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
  795                         }
  796 
  797                         /* Restore packet header fields to original values */
  798                         ip->ip_len = htons(ip->ip_len);
  799                         ip->ip_off = htons(ip->ip_off);
  800 
  801                         /* Deliver packet to divert input routine */
  802                         divert_packet(m, 0, off & 0xffff, args.divert_rule);
  803 
  804                         /* If 'tee', continue with original packet */
  805                         if (clone != NULL) {
  806                                 m = clone;
  807                                 ip = mtod(m, struct ip *);
  808                                 goto pass;
  809                         }
  810                         goto done;
  811                 }
  812 #endif
  813 
  814                 /* IPFIREWALL_FORWARD */
  815                 /*
  816                  * Check dst to make sure it is directly reachable on the
  817                  * interface we previously thought it was.
  818                  * If it isn't (which may be likely in some situations) we have
  819                  * to re-route it (ie, find a route for the next-hop and the
  820                  * associated interface) and set them here. This is nested
  821                  * forwarding which in most cases is undesirable, except where
  822                  * such control is nigh impossible. So we do it here.
  823                  * And I'm babbling.
  824                  */
  825                 if (off == 0 && old != dst) { /* FORWARD, dst has changed */
  826 #if 0
  827                         /*
  828                          * XXX To improve readability, this block should be
  829                          * changed into a function call as below:
  830                          */
  831                         error = ip_ipforward(&m, &dst, &ifp);
  832                         if (error)
  833                                 goto bad;
  834                         if (m == NULL) /* ip_input consumed the mbuf */
  835                                 goto done;
  836 #else
  837                         struct in_ifaddr *ia;
  838 
  839                         /*
  840                          * XXX sro_fwd below is static, and a pointer
  841                          * to it gets passed to routines downstream.
  842                          * This could have surprisingly bad results in
  843                          * practice, because its content is overwritten
  844                          * by subsequent packets.
  845                          */
  846                         /* There must be a better way to do this next line... */
  847                         static struct route sro_fwd;
  848                         struct route *ro_fwd = &sro_fwd;
  849 
  850 #if 0
  851                         print_ip("IPFIREWALL_FORWARD: New dst ip: ",
  852                             dst->sin_addr, "\n");
  853 #endif
  854 
  855                         /*
  856                          * We need to figure out if we have been forwarded
  857                          * to a local socket. If so, then we should somehow 
  858                          * "loop back" to ip_input, and get directed to the
  859                          * PCB as if we had received this packet. This is
  860                          * because it may be dificult to identify the packets
  861                          * you want to forward until they are being output
  862                          * and have selected an interface. (e.g. locally
  863                          * initiated packets) If we used the loopback inteface,
  864                          * we would not be able to control what happens 
  865                          * as the packet runs through ip_input() as
  866                          * it is done through a ISR.
  867                          */
  868                         LIST_FOREACH(ia,
  869                             INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
  870                                 /*
  871                                  * If the addr to forward to is one
  872                                  * of ours, we pretend to
  873                                  * be the destination for this packet.
  874                                  */
  875                                 if (IA_SIN(ia)->sin_addr.s_addr ==
  876                                                  dst->sin_addr.s_addr)
  877                                         break;
  878                         }
  879                         if (ia) {       /* tell ip_input "dont filter" */
  880                                 struct m_hdr tag;
  881 
  882                                 tag.mh_type = MT_TAG;
  883                                 tag.mh_flags = PACKET_TAG_IPFORWARD;
  884                                 tag.mh_data = (caddr_t)args.next_hop;
  885                                 tag.mh_next = m;
  886 
  887                                 if (m->m_pkthdr.rcvif == NULL)
  888                                         m->m_pkthdr.rcvif = ifunit("lo0");
  889                                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  890                                         m->m_pkthdr.csum_flags |=
  891                                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
  892                                         m->m_pkthdr.csum_data = 0xffff;
  893                                 }
  894                                 m->m_pkthdr.csum_flags |=
  895                                     CSUM_IP_CHECKED | CSUM_IP_VALID;
  896                                 ip->ip_len = htons(ip->ip_len);
  897                                 ip->ip_off = htons(ip->ip_off);
  898                                 ip_input((struct mbuf *)&tag);
  899                                 goto done;
  900                         }
  901                         /* Some of the logic for this was
  902                          * nicked from above.
  903                          *
  904                          * This rewrites the cached route in a local PCB.
  905                          * Is this what we want to do?
  906                          */
  907                         bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
  908 
  909                         ro_fwd->ro_rt = 0;
  910                         rtalloc_ign(ro_fwd, RTF_PRCLONING);
  911 
  912                         if (ro_fwd->ro_rt == 0) {
  913                                 ipstat.ips_noroute++;
  914                                 error = EHOSTUNREACH;
  915                                 goto bad;
  916                         }
  917 
  918                         ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
  919                         ifp = ro_fwd->ro_rt->rt_ifp;
  920                         ro_fwd->ro_rt->rt_use++;
  921                         if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
  922                                 dst = (struct sockaddr_in *)
  923                                         ro_fwd->ro_rt->rt_gateway;
  924                         if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
  925                                 isbroadcast =
  926                                     (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
  927                         else
  928                                 isbroadcast = in_broadcast(dst->sin_addr, ifp);
  929                         if (ro->ro_rt)
  930                                 RTFREE(ro->ro_rt);
  931                         ro->ro_rt = ro_fwd->ro_rt;
  932                         dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
  933 
  934 #endif  /* ... block to be put into a function */
  935                         /*
  936                          * If we added a default src ip earlier,
  937                          * which would have been gotten from the-then
  938                          * interface, do it again, from the new one.
  939                          */
  940                         if (src_was_INADDR_ANY)
  941                                 ip->ip_src = IA_SIN(ia)->sin_addr;
  942                         goto pass ;
  943                 }
  944 
  945                 /*
  946                  * if we get here, none of the above matches, and 
  947                  * we have to drop the pkt
  948                  */
  949                 m_freem(m);
  950                 error = EACCES; /* not sure this is the right error msg */
  951                 goto done;
  952         }
  953 
  954 pass:
  955         /* 127/8 must not appear on wire - RFC1122. */
  956         if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
  957             (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
  958                 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
  959                         ipstat.ips_badaddr++;
  960                         error = EADDRNOTAVAIL;
  961                         goto bad;
  962                 }
  963         }
  964 
  965         m->m_pkthdr.csum_flags |= CSUM_IP;
  966         sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
  967         if (sw_csum & CSUM_DELAY_DATA) {
  968                 in_delayed_cksum(m);
  969                 sw_csum &= ~CSUM_DELAY_DATA;
  970         }
  971         m->m_pkthdr.csum_flags &= ifp->if_hwassist;
  972 
  973         /*
  974          * If small enough for interface, or the interface will take
  975          * care of the fragmentation for us, can just send directly.
  976          */
  977         if (ip->ip_len <= ifp->if_mtu || ifp->if_hwassist & CSUM_FRAGMENT) {
  978                 ip->ip_len = htons(ip->ip_len);
  979                 ip->ip_off = htons(ip->ip_off);
  980                 ip->ip_sum = 0;
  981                 if (sw_csum & CSUM_DELAY_IP) {
  982                         if (ip->ip_vhl == IP_VHL_BORING) {
  983                                 ip->ip_sum = in_cksum_hdr(ip);
  984                         } else {
  985                                 ip->ip_sum = in_cksum(m, hlen);
  986                         }
  987                 }
  988 
  989                 /* Record statistics for this interface address. */
  990                 if (!(flags & IP_FORWARDING) && ia) {
  991                         ia->ia_ifa.if_opackets++;
  992                         ia->ia_ifa.if_obytes += m->m_pkthdr.len;
  993                 }
  994 
  995 #ifdef IPSEC
  996                 /* clean ipsec history once it goes out of the node */
  997                 ipsec_delaux(m);
  998 #endif
  999 
 1000 #ifdef MBUF_STRESS_TEST
 1001                 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
 1002                         m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
 1003 #endif
 1004                 error = (*ifp->if_output)(ifp, m,
 1005                                 (struct sockaddr *)dst, ro->ro_rt);
 1006                 goto done;
 1007         }
 1008 
 1009         if (ip->ip_off & IP_DF) {
 1010                 error = EMSGSIZE;
 1011                 /*
 1012                  * This case can happen if the user changed the MTU
 1013                  * of an interface after enabling IP on it.  Because
 1014                  * most netifs don't keep track of routes pointing to
 1015                  * them, there is no way for one to update all its
 1016                  * routes when the MTU is changed.
 1017                  */
 1018                 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
 1019                     !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) &&
 1020                     (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
 1021                         ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
 1022                 }
 1023                 ipstat.ips_cantfrag++;
 1024                 goto bad;
 1025         }
 1026 
 1027         /*
 1028          * Too large for interface; fragment if possible. If successful,
 1029          * on return, m will point to a list of packets to be sent.
 1030          */
 1031         error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
 1032         if (error)
 1033                 goto bad;
 1034         for (; m; m = m0) {
 1035                 m0 = m->m_nextpkt;
 1036                 m->m_nextpkt = 0;
 1037 #ifdef IPSEC
 1038                 /* clean ipsec history once it goes out of the node */
 1039                 ipsec_delaux(m);
 1040 #endif
 1041                 if (error == 0) {
 1042                         /* Record statistics for this interface address. */
 1043                         if (ia != NULL) {
 1044                                 ia->ia_ifa.if_opackets++;
 1045                                 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
 1046                         }
 1047                         
 1048                         error = (*ifp->if_output)(ifp, m,
 1049                             (struct sockaddr *)dst, ro->ro_rt);
 1050                 } else
 1051                         m_freem(m);
 1052         }
 1053 
 1054         if (error == 0)
 1055                 ipstat.ips_fragmented++;
 1056 
 1057 done:
 1058 #ifdef IPSEC
 1059         if (ro == &iproute && ro->ro_rt) {
 1060                 RTFREE(ro->ro_rt);
 1061                 ro->ro_rt = NULL;
 1062         }
 1063         if (sp != NULL) {
 1064                 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
 1065                         printf("DP ip_output call free SP:%p\n", sp));
 1066                 key_freesp(sp);
 1067         }
 1068 #endif
 1069 #ifdef FAST_IPSEC
 1070         if (ro == &iproute && ro->ro_rt) {
 1071                 RTFREE(ro->ro_rt);
 1072                 ro->ro_rt = NULL;
 1073         }
 1074         if (sp != NULL)
 1075                 KEY_FREESP(&sp);
 1076 #endif
 1077         return (error);
 1078 bad:
 1079         m_freem(m);
 1080         goto done;
 1081 }
 1082 
 1083 /*
 1084  * Create a chain of fragments which fit the given mtu. m_frag points to the
 1085  * mbuf to be fragmented; on return it points to the chain with the fragments.
 1086  * Return 0 if no error. If error, m_frag may contain a partially built
 1087  * chain of fragments that should be freed by the caller.
 1088  *
 1089  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
 1090  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
 1091  */
 1092 int
 1093 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
 1094             u_long if_hwassist_flags, int sw_csum)
 1095 {
 1096         int error = 0;
 1097         int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
 1098         int len = (mtu - hlen) & ~7;    /* size of payload in each fragment */
 1099         int off;
 1100         struct mbuf *m0 = *m_frag;      /* the original packet          */
 1101         int firstlen;
 1102         struct mbuf **mnext;
 1103         int nfrags;
 1104 
 1105         if (ip->ip_off & IP_DF) {       /* Fragmentation not allowed */
 1106                 ipstat.ips_cantfrag++;
 1107                 return EMSGSIZE;
 1108         }
 1109 
 1110         /*
 1111          * Must be able to put at least 8 bytes per fragment.
 1112          */
 1113         if (len < 8)
 1114                 return EMSGSIZE;
 1115 
 1116         /*
 1117          * If the interface will not calculate checksums on
 1118          * fragmented packets, then do it here.
 1119          */
 1120         if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
 1121             (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
 1122                 in_delayed_cksum(m0);
 1123                 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
 1124         }
 1125 
 1126         if (len > PAGE_SIZE) {
 1127                 /* 
 1128                  * Fragment large datagrams such that each segment 
 1129                  * contains a multiple of PAGE_SIZE amount of data, 
 1130                  * plus headers. This enables a receiver to perform 
 1131                  * page-flipping zero-copy optimizations.
 1132                  *
 1133                  * XXX When does this help given that sender and receiver
 1134                  * could have different page sizes, and also mtu could
 1135                  * be less than the receiver's page size ?
 1136                  */
 1137                 int newlen;
 1138                 struct mbuf *m;
 1139 
 1140                 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
 1141                         off += m->m_len;
 1142 
 1143                 /*
 1144                  * firstlen (off - hlen) must be aligned on an 
 1145                  * 8-byte boundary
 1146                  */
 1147                 if (off < hlen)
 1148                         goto smart_frag_failure;
 1149                 off = ((off - hlen) & ~7) + hlen;
 1150                 newlen = (~PAGE_MASK) & mtu;
 1151                 if ((newlen + sizeof (struct ip)) > mtu) {
 1152                         /* we failed, go back the default */
 1153 smart_frag_failure:
 1154                         newlen = len;
 1155                         off = hlen + len;
 1156                 }
 1157                 len = newlen;
 1158 
 1159         } else {
 1160                 off = hlen + len;
 1161         }
 1162 
 1163         firstlen = off - hlen;
 1164         mnext = &m0->m_nextpkt;         /* pointer to next packet */
 1165 
 1166         /*
 1167          * Loop through length of segment after first fragment,
 1168          * make new header and copy data of each part and link onto chain.
 1169          * Here, m0 is the original packet, m is the fragment being created.
 1170          * The fragments are linked off the m_nextpkt of the original
 1171          * packet, which after processing serves as the first fragment.
 1172          */
 1173         for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
 1174                 struct ip *mhip;        /* ip header on the fragment */
 1175                 struct mbuf *m;
 1176                 int mhlen = sizeof (struct ip);
 1177 
 1178                 MGETHDR(m, M_DONTWAIT, MT_HEADER);
 1179                 if (m == 0) {
 1180                         error = ENOBUFS;
 1181                         ipstat.ips_odropped++;
 1182                         goto done;
 1183                 }
 1184                 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
 1185                 /*
 1186                  * In the first mbuf, leave room for the link header, then
 1187                  * copy the original IP header including options. The payload
 1188                  * goes into an additional mbuf chain returned by m_copy().
 1189                  */
 1190                 m->m_data += max_linkhdr;
 1191                 mhip = mtod(m, struct ip *);
 1192                 *mhip = *ip;
 1193                 if (hlen > sizeof (struct ip)) {
 1194                         mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
 1195                         mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
 1196                 }
 1197                 m->m_len = mhlen;
 1198                 /* XXX do we need to add ip->ip_off below ? */
 1199                 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
 1200                 if (off + len >= ip->ip_len) {  /* last fragment */
 1201                         len = ip->ip_len - off;
 1202                         m->m_flags |= M_LASTFRAG;
 1203                 } else
 1204                         mhip->ip_off |= IP_MF;
 1205                 mhip->ip_len = htons((u_short)(len + mhlen));
 1206                 m->m_next = m_copy(m0, off, len);
 1207                 if (m->m_next == 0) {           /* copy failed */
 1208                         m_free(m);
 1209                         error = ENOBUFS;        /* ??? */
 1210                         ipstat.ips_odropped++;
 1211                         goto done;
 1212                 }
 1213                 m->m_pkthdr.len = mhlen + len;
 1214                 m->m_pkthdr.rcvif = (struct ifnet *)0;
 1215                 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
 1216                 mhip->ip_off = htons(mhip->ip_off);
 1217                 mhip->ip_sum = 0;
 1218                 if (sw_csum & CSUM_DELAY_IP)
 1219                         mhip->ip_sum = in_cksum(m, mhlen);
 1220                 *mnext = m;
 1221                 mnext = &m->m_nextpkt;
 1222         }
 1223         ipstat.ips_ofragments += nfrags;
 1224 
 1225         /* set first marker for fragment chain */
 1226         m0->m_flags |= M_FIRSTFRAG | M_FRAG;
 1227         m0->m_pkthdr.csum_data = nfrags;
 1228 
 1229         /*
 1230          * Update first fragment by trimming what's been copied out
 1231          * and updating header.
 1232          */
 1233         m_adj(m0, hlen + firstlen - ip->ip_len);
 1234         m0->m_pkthdr.len = hlen + firstlen;
 1235         ip->ip_len = htons((u_short)m0->m_pkthdr.len);
 1236         ip->ip_off |= IP_MF;
 1237         ip->ip_off = htons(ip->ip_off);
 1238         ip->ip_sum = 0;
 1239         if (sw_csum & CSUM_DELAY_IP)
 1240                 ip->ip_sum = in_cksum(m0, hlen);
 1241 
 1242 done:
 1243         *m_frag = m0;
 1244         return error;
 1245 }
 1246 
 1247 void
 1248 in_delayed_cksum(struct mbuf *m)
 1249 {
 1250         struct ip *ip;
 1251         u_short csum, offset;
 1252 
 1253         ip = mtod(m, struct ip *);
 1254         offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
 1255         csum = in_cksum_skip(m, ip->ip_len, offset);
 1256         if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
 1257                 csum = 0xffff;
 1258         offset += m->m_pkthdr.csum_data;        /* checksum offset */
 1259 
 1260         if (offset + sizeof(u_short) > m->m_len) {
 1261                 printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
 1262                     m->m_len, offset, ip->ip_p);
 1263                 /*
 1264                  * XXX
 1265                  * this shouldn't happen, but if it does, the
 1266                  * correct behavior may be to insert the checksum
 1267                  * in the existing chain instead of rearranging it.
 1268                  */
 1269                 m = m_pullup(m, offset + sizeof(u_short));
 1270         }
 1271         *(u_short *)(m->m_data + offset) = csum;
 1272 }
 1273 
 1274 /*
 1275  * Insert IP options into preformed packet.
 1276  * Adjust IP destination as required for IP source routing,
 1277  * as indicated by a non-zero in_addr at the start of the options.
 1278  *
 1279  * XXX This routine assumes that the packet has no options in place.
 1280  */
 1281 static struct mbuf *
 1282 ip_insertoptions(m, opt, phlen)
 1283         register struct mbuf *m;
 1284         struct mbuf *opt;
 1285         int *phlen;
 1286 {
 1287         register struct ipoption *p = mtod(opt, struct ipoption *);
 1288         struct mbuf *n;
 1289         register struct ip *ip = mtod(m, struct ip *);
 1290         unsigned optlen;
 1291 
 1292         optlen = opt->m_len - sizeof(p->ipopt_dst);
 1293         if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) {
 1294                 *phlen = 0;
 1295                 return (m);             /* XXX should fail */
 1296         }
 1297         if (p->ipopt_dst.s_addr)
 1298                 ip->ip_dst = p->ipopt_dst;
 1299         if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
 1300                 MGETHDR(n, M_DONTWAIT, MT_HEADER);
 1301                 if (n == 0) {
 1302                         *phlen = 0;
 1303                         return (m);
 1304                 }
 1305                 n->m_pkthdr.rcvif = (struct ifnet *)0;
 1306                 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
 1307                 m->m_len -= sizeof(struct ip);
 1308                 m->m_data += sizeof(struct ip);
 1309                 n->m_next = m;
 1310                 m = n;
 1311                 m->m_len = optlen + sizeof(struct ip);
 1312                 m->m_data += max_linkhdr;
 1313                 (void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
 1314         } else {
 1315                 m->m_data -= optlen;
 1316                 m->m_len += optlen;
 1317                 m->m_pkthdr.len += optlen;
 1318                 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
 1319         }
 1320         ip = mtod(m, struct ip *);
 1321         bcopy(p->ipopt_list, ip + 1, optlen);
 1322         *phlen = sizeof(struct ip) + optlen;
 1323         ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
 1324         ip->ip_len += optlen;
 1325         return (m);
 1326 }
 1327 
 1328 /*
 1329  * Copy options from ip to jp,
 1330  * omitting those not copied during fragmentation.
 1331  */
 1332 int
 1333 ip_optcopy(ip, jp)
 1334         struct ip *ip, *jp;
 1335 {
 1336         register u_char *cp, *dp;
 1337         int opt, optlen, cnt;
 1338 
 1339         cp = (u_char *)(ip + 1);
 1340         dp = (u_char *)(jp + 1);
 1341         cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
 1342         for (; cnt > 0; cnt -= optlen, cp += optlen) {
 1343                 opt = cp[0];
 1344                 if (opt == IPOPT_EOL)
 1345                         break;
 1346                 if (opt == IPOPT_NOP) {
 1347                         /* Preserve for IP mcast tunnel's LSRR alignment. */
 1348                         *dp++ = IPOPT_NOP;
 1349                         optlen = 1;
 1350                         continue;
 1351                 }
 1352 
 1353                 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
 1354                     ("ip_optcopy: malformed ipv4 option"));
 1355                 optlen = cp[IPOPT_OLEN];
 1356                 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
 1357                     ("ip_optcopy: malformed ipv4 option"));
 1358 
 1359                 /* bogus lengths should have been caught by ip_dooptions */
 1360                 if (optlen > cnt)
 1361                         optlen = cnt;
 1362                 if (IPOPT_COPIED(opt)) {
 1363                         bcopy(cp, dp, optlen);
 1364                         dp += optlen;
 1365                 }
 1366         }
 1367         for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
 1368                 *dp++ = IPOPT_EOL;
 1369         return (optlen);
 1370 }
 1371 
 1372 /*
 1373  * IP socket option processing.
 1374  */
 1375 int
 1376 ip_ctloutput(so, sopt)
 1377         struct socket *so;
 1378         struct sockopt *sopt;
 1379 {
 1380         struct  inpcb *inp = sotoinpcb(so);
 1381         int     error, optval;
 1382 
 1383         error = optval = 0;
 1384         if (sopt->sopt_level != IPPROTO_IP) {
 1385                 return (EINVAL);
 1386         }
 1387 
 1388         switch (sopt->sopt_dir) {
 1389         case SOPT_SET:
 1390                 switch (sopt->sopt_name) {
 1391                 case IP_OPTIONS:
 1392 #ifdef notyet
 1393                 case IP_RETOPTS:
 1394 #endif
 1395                 {
 1396                         struct mbuf *m;
 1397                         if (sopt->sopt_valsize > MLEN) {
 1398                                 error = EMSGSIZE;
 1399                                 break;
 1400                         }
 1401                         MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_HEADER);
 1402                         if (m == 0) {
 1403                                 error = ENOBUFS;
 1404                                 break;
 1405                         }
 1406                         m->m_len = sopt->sopt_valsize;
 1407                         error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
 1408                                             m->m_len);
 1409                         
 1410                         return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
 1411                                            m));
 1412                 }
 1413 
 1414                 case IP_TOS:
 1415                 case IP_TTL:
 1416                 case IP_RECVOPTS:
 1417                 case IP_RECVRETOPTS:
 1418                 case IP_RECVDSTADDR:
 1419                 case IP_RECVIF:
 1420                 case IP_FAITH:
 1421                 case IP_ONESBCAST:
 1422                         error = sooptcopyin(sopt, &optval, sizeof optval,
 1423                                             sizeof optval);
 1424                         if (error)
 1425                                 break;
 1426 
 1427                         switch (sopt->sopt_name) {
 1428                         case IP_TOS:
 1429                                 inp->inp_ip_tos = optval;
 1430                                 break;
 1431 
 1432                         case IP_TTL:
 1433                                 inp->inp_ip_ttl = optval;
 1434                                 break;
 1435 #define OPTSET(bit) \
 1436         if (optval) \
 1437                 inp->inp_flags |= bit; \
 1438         else \
 1439                 inp->inp_flags &= ~bit;
 1440 
 1441                         case IP_RECVOPTS:
 1442                                 OPTSET(INP_RECVOPTS);
 1443                                 break;
 1444 
 1445                         case IP_RECVRETOPTS:
 1446                                 OPTSET(INP_RECVRETOPTS);
 1447                                 break;
 1448 
 1449                         case IP_RECVDSTADDR:
 1450                                 OPTSET(INP_RECVDSTADDR);
 1451                                 break;
 1452 
 1453                         case IP_RECVIF:
 1454                                 OPTSET(INP_RECVIF);
 1455                                 break;
 1456 
 1457                         case IP_FAITH:
 1458                                 OPTSET(INP_FAITH);
 1459                                 break;
 1460 
 1461                         case IP_ONESBCAST:
 1462                                 OPTSET(INP_ONESBCAST);
 1463                                 break;
 1464                         }
 1465                         break;
 1466 #undef OPTSET
 1467 
 1468                 case IP_MULTICAST_IF:
 1469                 case IP_MULTICAST_VIF:
 1470                 case IP_MULTICAST_TTL:
 1471                 case IP_MULTICAST_LOOP:
 1472                 case IP_ADD_MEMBERSHIP:
 1473                 case IP_DROP_MEMBERSHIP:
 1474                         error = ip_setmoptions(sopt, &inp->inp_moptions);
 1475                         break;
 1476 
 1477                 case IP_PORTRANGE:
 1478                         error = sooptcopyin(sopt, &optval, sizeof optval,
 1479                                             sizeof optval);
 1480                         if (error)
 1481                                 break;
 1482 
 1483                         switch (optval) {
 1484                         case IP_PORTRANGE_DEFAULT:
 1485                                 inp->inp_flags &= ~(INP_LOWPORT);
 1486                                 inp->inp_flags &= ~(INP_HIGHPORT);
 1487                                 break;
 1488 
 1489                         case IP_PORTRANGE_HIGH:
 1490                                 inp->inp_flags &= ~(INP_LOWPORT);
 1491                                 inp->inp_flags |= INP_HIGHPORT;
 1492                                 break;
 1493 
 1494                         case IP_PORTRANGE_LOW:
 1495                                 inp->inp_flags &= ~(INP_HIGHPORT);
 1496                                 inp->inp_flags |= INP_LOWPORT;
 1497                                 break;
 1498 
 1499                         default:
 1500                                 error = EINVAL;
 1501                                 break;
 1502                         }
 1503                         break;
 1504 
 1505 #if defined(IPSEC) || defined(FAST_IPSEC)
 1506                 case IP_IPSEC_POLICY:
 1507                 {
 1508                         caddr_t req;
 1509                         size_t len = 0;
 1510                         int priv;
 1511                         struct mbuf *m;
 1512                         int optname;
 1513 
 1514                         if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
 1515                                 break;
 1516                         if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
 1517                                 break;
 1518                         priv = (sopt->sopt_p != NULL &&
 1519                                 suser(sopt->sopt_p) != 0) ? 0 : 1;
 1520                         req = mtod(m, caddr_t);
 1521                         len = m->m_len;
 1522                         optname = sopt->sopt_name;
 1523                         error = ipsec4_set_policy(inp, optname, req, len, priv);
 1524                         m_freem(m);
 1525                         break;
 1526                 }
 1527 #endif /*IPSEC*/
 1528 
 1529                 default:
 1530                         error = ENOPROTOOPT;
 1531                         break;
 1532                 }
 1533                 break;
 1534 
 1535         case SOPT_GET:
 1536                 switch (sopt->sopt_name) {
 1537                 case IP_OPTIONS:
 1538                 case IP_RETOPTS:
 1539                         if (inp->inp_options)
 1540                                 error = sooptcopyout(sopt, 
 1541                                                      mtod(inp->inp_options,
 1542                                                           char *),
 1543                                                      inp->inp_options->m_len);
 1544                         else
 1545                                 sopt->sopt_valsize = 0;
 1546                         break;
 1547 
 1548                 case IP_TOS:
 1549                 case IP_TTL:
 1550                 case IP_RECVOPTS:
 1551                 case IP_RECVRETOPTS:
 1552                 case IP_RECVDSTADDR:
 1553                 case IP_RECVIF:
 1554                 case IP_PORTRANGE:
 1555                 case IP_FAITH:
 1556                 case IP_ONESBCAST:
 1557                         switch (sopt->sopt_name) {
 1558 
 1559                         case IP_TOS:
 1560                                 optval = inp->inp_ip_tos;
 1561                                 break;
 1562 
 1563                         case IP_TTL:
 1564                                 optval = inp->inp_ip_ttl;
 1565                                 break;
 1566 
 1567 #define OPTBIT(bit)     (inp->inp_flags & bit ? 1 : 0)
 1568 
 1569                         case IP_RECVOPTS:
 1570                                 optval = OPTBIT(INP_RECVOPTS);
 1571                                 break;
 1572 
 1573                         case IP_RECVRETOPTS:
 1574                                 optval = OPTBIT(INP_RECVRETOPTS);
 1575                                 break;
 1576 
 1577                         case IP_RECVDSTADDR:
 1578                                 optval = OPTBIT(INP_RECVDSTADDR);
 1579                                 break;
 1580 
 1581                         case IP_RECVIF:
 1582                                 optval = OPTBIT(INP_RECVIF);
 1583                                 break;
 1584 
 1585                         case IP_PORTRANGE:
 1586                                 if (inp->inp_flags & INP_HIGHPORT)
 1587                                         optval = IP_PORTRANGE_HIGH;
 1588                                 else if (inp->inp_flags & INP_LOWPORT)
 1589                                         optval = IP_PORTRANGE_LOW;
 1590                                 else
 1591                                         optval = 0;
 1592                                 break;
 1593 
 1594                         case IP_FAITH:
 1595                                 optval = OPTBIT(INP_FAITH);
 1596                                 break;
 1597 
 1598                         case IP_ONESBCAST:
 1599                                 optval = OPTBIT(INP_ONESBCAST);
 1600                                 break;
 1601                         }
 1602                         error = sooptcopyout(sopt, &optval, sizeof optval);
 1603                         break;
 1604 
 1605                 case IP_MULTICAST_IF:
 1606                 case IP_MULTICAST_VIF:
 1607                 case IP_MULTICAST_TTL:
 1608                 case IP_MULTICAST_LOOP:
 1609                 case IP_ADD_MEMBERSHIP:
 1610                 case IP_DROP_MEMBERSHIP:
 1611                         error = ip_getmoptions(sopt, inp->inp_moptions);
 1612                         break;
 1613 
 1614 #if defined(IPSEC) || defined(FAST_IPSEC)
 1615                 case IP_IPSEC_POLICY:
 1616                 {
 1617                         struct mbuf *m = NULL;
 1618                         caddr_t req = NULL;
 1619                         size_t len = 0;
 1620 
 1621                         if (m != 0) {
 1622                                 req = mtod(m, caddr_t);
 1623                                 len = m->m_len;
 1624                         }
 1625                         error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
 1626                         if (error == 0)
 1627                                 error = soopt_mcopyout(sopt, m); /* XXX */
 1628                         if (error == 0)
 1629                                 m_freem(m);
 1630                         break;
 1631                 }
 1632 #endif /*IPSEC*/
 1633 
 1634                 default:
 1635                         error = ENOPROTOOPT;
 1636                         break;
 1637                 }
 1638                 break;
 1639         }
 1640         return (error);
 1641 }
 1642 
 1643 /*
 1644  * Set up IP options in pcb for insertion in output packets.
 1645  * Store in mbuf with pointer in pcbopt, adding pseudo-option
 1646  * with destination address if source routed.
 1647  */
 1648 static int
 1649 ip_pcbopts(optname, pcbopt, m)
 1650         int optname;
 1651         struct mbuf **pcbopt;
 1652         register struct mbuf *m;
 1653 {
 1654         register int cnt, optlen;
 1655         register u_char *cp;
 1656         u_char opt;
 1657 
 1658         /* turn off any old options */
 1659         if (*pcbopt)
 1660                 (void)m_free(*pcbopt);
 1661         *pcbopt = 0;
 1662         if (m == (struct mbuf *)0 || m->m_len == 0) {
 1663                 /*
 1664                  * Only turning off any previous options.
 1665                  */
 1666                 if (m)
 1667                         (void)m_free(m);
 1668                 return (0);
 1669         }
 1670 
 1671         if (m->m_len % sizeof(int32_t))
 1672                 goto bad;
 1673         /*
 1674          * IP first-hop destination address will be stored before
 1675          * actual options; move other options back
 1676          * and clear it when none present.
 1677          */
 1678         if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
 1679                 goto bad;
 1680         cnt = m->m_len;
 1681         m->m_len += sizeof(struct in_addr);
 1682         cp = mtod(m, u_char *) + sizeof(struct in_addr);
 1683         ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
 1684         bzero(mtod(m, caddr_t), sizeof(struct in_addr));
 1685 
 1686         for (; cnt > 0; cnt -= optlen, cp += optlen) {
 1687                 opt = cp[IPOPT_OPTVAL];
 1688                 if (opt == IPOPT_EOL)
 1689                         break;
 1690                 if (opt == IPOPT_NOP)
 1691                         optlen = 1;
 1692                 else {
 1693                         if (cnt < IPOPT_OLEN + sizeof(*cp))
 1694                                 goto bad;
 1695                         optlen = cp[IPOPT_OLEN];
 1696                         if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
 1697                                 goto bad;
 1698                 }
 1699                 switch (opt) {
 1700 
 1701                 default:
 1702                         break;
 1703 
 1704                 case IPOPT_LSRR:
 1705                 case IPOPT_SSRR:
 1706                         /*
 1707                          * user process specifies route as:
 1708                          *      ->A->B->C->D
 1709                          * D must be our final destination (but we can't
 1710                          * check that since we may not have connected yet).
 1711                          * A is first hop destination, which doesn't appear in
 1712                          * actual IP option, but is stored before the options.
 1713                          */
 1714                         if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
 1715                                 goto bad;
 1716                         m->m_len -= sizeof(struct in_addr);
 1717                         cnt -= sizeof(struct in_addr);
 1718                         optlen -= sizeof(struct in_addr);
 1719                         cp[IPOPT_OLEN] = optlen;
 1720                         /*
 1721                          * Move first hop before start of options.
 1722                          */
 1723                         bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
 1724                             sizeof(struct in_addr));
 1725                         /*
 1726                          * Then copy rest of options back
 1727                          * to close up the deleted entry.
 1728                          */
 1729                         ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
 1730                             sizeof(struct in_addr)),
 1731                             (caddr_t)&cp[IPOPT_OFFSET+1],
 1732                             (unsigned)cnt - (IPOPT_MINOFF - 1));
 1733                         break;
 1734                 }
 1735         }
 1736         if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
 1737                 goto bad;
 1738         *pcbopt = m;
 1739         return (0);
 1740 
 1741 bad:
 1742         (void)m_free(m);
 1743         return (EINVAL);
 1744 }
 1745 
 1746 /*
 1747  * XXX
 1748  * The whole multicast option thing needs to be re-thought.
 1749  * Several of these options are equally applicable to non-multicast
 1750  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
 1751  * standard option (IP_TTL).
 1752  */
 1753 
 1754 /*
 1755  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
 1756  */
 1757 static struct ifnet *
 1758 ip_multicast_if(a, ifindexp)
 1759         struct in_addr *a;
 1760         int *ifindexp;
 1761 {
 1762         int ifindex;
 1763         struct ifnet *ifp;
 1764 
 1765         if (ifindexp)
 1766                 *ifindexp = 0;
 1767         if (ntohl(a->s_addr) >> 24 == 0) {
 1768                 ifindex = ntohl(a->s_addr) & 0xffffff;
 1769                 if (ifindex < 0 || if_index < ifindex)
 1770                         return NULL;
 1771                 ifp = ifindex2ifnet[ifindex];
 1772                 if (ifindexp)
 1773                         *ifindexp = ifindex;
 1774         } else {
 1775                 INADDR_TO_IFP(*a, ifp);
 1776         }
 1777         return ifp;
 1778 }
 1779 
 1780 /*
 1781  * Set the IP multicast options in response to user setsockopt().
 1782  */
 1783 static int
 1784 ip_setmoptions(sopt, imop)
 1785         struct sockopt *sopt;
 1786         struct ip_moptions **imop;
 1787 {
 1788         int error = 0;
 1789         int i;
 1790         struct in_addr addr;
 1791         struct ip_mreq mreq;
 1792         struct ifnet *ifp;
 1793         struct ip_moptions *imo = *imop;
 1794         struct route ro;
 1795         struct sockaddr_in *dst;
 1796         int ifindex;
 1797         int s;
 1798 
 1799         if (imo == NULL) {
 1800                 /*
 1801                  * No multicast option buffer attached to the pcb;
 1802                  * allocate one and initialize to default values.
 1803                  */
 1804                 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
 1805                     M_WAITOK);
 1806 
 1807                 if (imo == NULL)
 1808                         return (ENOBUFS);
 1809                 *imop = imo;
 1810                 imo->imo_multicast_ifp = NULL;
 1811                 imo->imo_multicast_addr.s_addr = INADDR_ANY;
 1812                 imo->imo_multicast_vif = -1;
 1813                 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
 1814                 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
 1815                 imo->imo_num_memberships = 0;
 1816         }
 1817 
 1818         switch (sopt->sopt_name) {
 1819         /* store an index number for the vif you wanna use in the send */
 1820         case IP_MULTICAST_VIF:
 1821                 if (legal_vif_num == 0) {
 1822                         error = EOPNOTSUPP;
 1823                         break;
 1824                 }
 1825                 error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
 1826                 if (error)
 1827                         break;
 1828                 if (!legal_vif_num(i) && (i != -1)) {
 1829                         error = EINVAL;
 1830                         break;
 1831                 }
 1832                 imo->imo_multicast_vif = i;
 1833                 break;
 1834 
 1835         case IP_MULTICAST_IF:
 1836                 /*
 1837                  * Select the interface for outgoing multicast packets.
 1838                  */
 1839                 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
 1840                 if (error)
 1841                         break;
 1842                 /*
 1843                  * INADDR_ANY is used to remove a previous selection.
 1844                  * When no interface is selected, a default one is
 1845                  * chosen every time a multicast packet is sent.
 1846                  */
 1847                 if (addr.s_addr == INADDR_ANY) {
 1848                         imo->imo_multicast_ifp = NULL;
 1849                         break;
 1850                 }
 1851                 /*
 1852                  * The selected interface is identified by its local
 1853                  * IP address.  Find the interface and confirm that
 1854                  * it supports multicasting.
 1855                  */
 1856                 s = splimp();
 1857                 ifp = ip_multicast_if(&addr, &ifindex);
 1858                 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
 1859                         splx(s);
 1860                         error = EADDRNOTAVAIL;
 1861                         break;
 1862                 }
 1863                 imo->imo_multicast_ifp = ifp;
 1864                 if (ifindex)
 1865                         imo->imo_multicast_addr = addr;
 1866                 else
 1867                         imo->imo_multicast_addr.s_addr = INADDR_ANY;
 1868                 splx(s);
 1869                 break;
 1870 
 1871         case IP_MULTICAST_TTL:
 1872                 /*
 1873                  * Set the IP time-to-live for outgoing multicast packets.
 1874                  * The original multicast API required a char argument,
 1875                  * which is inconsistent with the rest of the socket API.
 1876                  * We allow either a char or an int.
 1877                  */
 1878                 if (sopt->sopt_valsize == 1) {
 1879                         u_char ttl;
 1880                         error = sooptcopyin(sopt, &ttl, 1, 1);
 1881                         if (error)
 1882                                 break;
 1883                         imo->imo_multicast_ttl = ttl;
 1884                 } else {
 1885                         u_int ttl;
 1886                         error = sooptcopyin(sopt, &ttl, sizeof ttl, 
 1887                                             sizeof ttl);
 1888                         if (error)
 1889                                 break;
 1890                         if (ttl > 255)
 1891                                 error = EINVAL;
 1892                         else
 1893                                 imo->imo_multicast_ttl = ttl;
 1894                 }
 1895                 break;
 1896 
 1897         case IP_MULTICAST_LOOP:
 1898                 /*
 1899                  * Set the loopback flag for outgoing multicast packets.
 1900                  * Must be zero or one.  The original multicast API required a
 1901                  * char argument, which is inconsistent with the rest
 1902                  * of the socket API.  We allow either a char or an int.
 1903                  */
 1904                 if (sopt->sopt_valsize == 1) {
 1905                         u_char loop;
 1906                         error = sooptcopyin(sopt, &loop, 1, 1);
 1907                         if (error)
 1908                                 break;
 1909                         imo->imo_multicast_loop = !!loop;
 1910                 } else {
 1911                         u_int loop;
 1912                         error = sooptcopyin(sopt, &loop, sizeof loop,
 1913                                             sizeof loop);
 1914                         if (error)
 1915                                 break;
 1916                         imo->imo_multicast_loop = !!loop;
 1917                 }
 1918                 break;
 1919 
 1920         case IP_ADD_MEMBERSHIP:
 1921                 /*
 1922                  * Add a multicast group membership.
 1923                  * Group must be a valid IP multicast address.
 1924                  */
 1925                 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
 1926                 if (error)
 1927                         break;
 1928 
 1929                 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
 1930                         error = EINVAL;
 1931                         break;
 1932                 }
 1933                 s = splimp();
 1934                 /*
 1935                  * If no interface address was provided, use the interface of
 1936                  * the route to the given multicast address.
 1937                  */
 1938                 if (mreq.imr_interface.s_addr == INADDR_ANY) {
 1939                         bzero((caddr_t)&ro, sizeof(ro));
 1940                         dst = (struct sockaddr_in *)&ro.ro_dst;
 1941                         dst->sin_len = sizeof(*dst);
 1942                         dst->sin_family = AF_INET;
 1943                         dst->sin_addr = mreq.imr_multiaddr;
 1944                         rtalloc(&ro);
 1945                         if (ro.ro_rt == NULL) {
 1946                                 error = EADDRNOTAVAIL;
 1947                                 splx(s);
 1948                                 break;
 1949                         }
 1950                         ifp = ro.ro_rt->rt_ifp;
 1951                         rtfree(ro.ro_rt);
 1952                 }
 1953                 else {
 1954                         ifp = ip_multicast_if(&mreq.imr_interface, NULL);
 1955                 }
 1956 
 1957                 /*
 1958                  * See if we found an interface, and confirm that it
 1959                  * supports multicast.
 1960                  */
 1961                 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
 1962                         error = EADDRNOTAVAIL;
 1963                         splx(s);
 1964                         break;
 1965                 }
 1966                 /*
 1967                  * See if the membership already exists or if all the
 1968                  * membership slots are full.
 1969                  */
 1970                 for (i = 0; i < imo->imo_num_memberships; ++i) {
 1971                         if (imo->imo_membership[i]->inm_ifp == ifp &&
 1972                             imo->imo_membership[i]->inm_addr.s_addr
 1973                                                 == mreq.imr_multiaddr.s_addr)
 1974                                 break;
 1975                 }
 1976                 if (i < imo->imo_num_memberships) {
 1977                         error = EADDRINUSE;
 1978                         splx(s);
 1979                         break;
 1980                 }
 1981                 if (i == IP_MAX_MEMBERSHIPS) {
 1982                         error = ETOOMANYREFS;
 1983                         splx(s);
 1984                         break;
 1985                 }
 1986                 /*
 1987                  * Everything looks good; add a new record to the multicast
 1988                  * address list for the given interface.
 1989                  */
 1990                 if ((imo->imo_membership[i] =
 1991                     in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
 1992                         error = ENOBUFS;
 1993                         splx(s);
 1994                         break;
 1995                 }
 1996                 ++imo->imo_num_memberships;
 1997                 splx(s);
 1998                 break;
 1999 
 2000         case IP_DROP_MEMBERSHIP:
 2001                 /*
 2002                  * Drop a multicast group membership.
 2003                  * Group must be a valid IP multicast address.
 2004                  */
 2005                 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
 2006                 if (error)
 2007                         break;
 2008 
 2009                 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
 2010                         error = EINVAL;
 2011                         break;
 2012                 }
 2013 
 2014                 s = splimp();
 2015                 /*
 2016                  * If an interface address was specified, get a pointer
 2017                  * to its ifnet structure.
 2018                  */
 2019                 if (mreq.imr_interface.s_addr == INADDR_ANY)
 2020                         ifp = NULL;
 2021                 else {
 2022                         ifp = ip_multicast_if(&mreq.imr_interface, NULL);
 2023                         if (ifp == NULL) {
 2024                                 error = EADDRNOTAVAIL;
 2025                                 splx(s);
 2026                                 break;
 2027                         }
 2028                 }
 2029                 /*
 2030                  * Find the membership in the membership array.
 2031                  */
 2032                 for (i = 0; i < imo->imo_num_memberships; ++i) {
 2033                         if ((ifp == NULL ||
 2034                              imo->imo_membership[i]->inm_ifp == ifp) &&
 2035                              imo->imo_membership[i]->inm_addr.s_addr ==
 2036                              mreq.imr_multiaddr.s_addr)
 2037                                 break;
 2038                 }
 2039                 if (i == imo->imo_num_memberships) {
 2040                         error = EADDRNOTAVAIL;
 2041                         splx(s);
 2042                         break;
 2043                 }
 2044                 /*
 2045                  * Give up the multicast address record to which the
 2046                  * membership points.
 2047                  */
 2048                 in_delmulti(imo->imo_membership[i]);
 2049                 /*
 2050                  * Remove the gap in the membership array.
 2051                  */
 2052                 for (++i; i < imo->imo_num_memberships; ++i)
 2053                         imo->imo_membership[i-1] = imo->imo_membership[i];
 2054                 --imo->imo_num_memberships;
 2055                 splx(s);
 2056                 break;
 2057 
 2058         default:
 2059                 error = EOPNOTSUPP;
 2060                 break;
 2061         }
 2062 
 2063         /*
 2064          * If all options have default values, no need to keep the mbuf.
 2065          */
 2066         if (imo->imo_multicast_ifp == NULL &&
 2067             imo->imo_multicast_vif == -1 &&
 2068             imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
 2069             imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
 2070             imo->imo_num_memberships == 0) {
 2071                 free(*imop, M_IPMOPTS);
 2072                 *imop = NULL;
 2073         }
 2074 
 2075         return (error);
 2076 }
 2077 
 2078 /*
 2079  * Return the IP multicast options in response to user getsockopt().
 2080  */
 2081 static int
 2082 ip_getmoptions(sopt, imo)
 2083         struct sockopt *sopt;
 2084         register struct ip_moptions *imo;
 2085 {
 2086         struct in_addr addr;
 2087         struct in_ifaddr *ia;
 2088         int error, optval;
 2089         u_char coptval;
 2090 
 2091         error = 0;
 2092         switch (sopt->sopt_name) {
 2093         case IP_MULTICAST_VIF: 
 2094                 if (imo != NULL)
 2095                         optval = imo->imo_multicast_vif;
 2096                 else
 2097                         optval = -1;
 2098                 error = sooptcopyout(sopt, &optval, sizeof optval);
 2099                 break;
 2100 
 2101         case IP_MULTICAST_IF:
 2102                 if (imo == NULL || imo->imo_multicast_ifp == NULL)
 2103                         addr.s_addr = INADDR_ANY;
 2104                 else if (imo->imo_multicast_addr.s_addr) {
 2105                         /* return the value user has set */
 2106                         addr = imo->imo_multicast_addr;
 2107                 } else {
 2108                         IFP_TO_IA(imo->imo_multicast_ifp, ia);
 2109                         addr.s_addr = (ia == NULL) ? INADDR_ANY
 2110                                 : IA_SIN(ia)->sin_addr.s_addr;
 2111                 }
 2112                 error = sooptcopyout(sopt, &addr, sizeof addr);
 2113                 break;
 2114 
 2115         case IP_MULTICAST_TTL:
 2116                 if (imo == 0)
 2117                         optval = coptval = IP_DEFAULT_MULTICAST_TTL;
 2118                 else
 2119                         optval = coptval = imo->imo_multicast_ttl;
 2120                 if (sopt->sopt_valsize == 1)
 2121                         error = sooptcopyout(sopt, &coptval, 1);
 2122                 else
 2123                         error = sooptcopyout(sopt, &optval, sizeof optval);
 2124                 break;
 2125 
 2126         case IP_MULTICAST_LOOP:
 2127                 if (imo == 0)
 2128                         optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
 2129                 else
 2130                         optval = coptval = imo->imo_multicast_loop;
 2131                 if (sopt->sopt_valsize == 1)
 2132                         error = sooptcopyout(sopt, &coptval, 1);
 2133                 else
 2134                         error = sooptcopyout(sopt, &optval, sizeof optval);
 2135                 break;
 2136 
 2137         default:
 2138                 error = ENOPROTOOPT;
 2139                 break;
 2140         }
 2141         return (error);
 2142 }
 2143 
 2144 /*
 2145  * Discard the IP multicast options.
 2146  */
 2147 void
 2148 ip_freemoptions(imo)
 2149         register struct ip_moptions *imo;
 2150 {
 2151         register int i;
 2152 
 2153         if (imo != NULL) {
 2154                 for (i = 0; i < imo->imo_num_memberships; ++i)
 2155                         in_delmulti(imo->imo_membership[i]);
 2156                 free(imo, M_IPMOPTS);
 2157         }
 2158 }
 2159 
 2160 /*
 2161  * Routine called from ip_output() to loop back a copy of an IP multicast
 2162  * packet to the input queue of a specified interface.  Note that this
 2163  * calls the output routine of the loopback "driver", but with an interface
 2164  * pointer that might NOT be a loopback interface -- evil, but easier than
 2165  * replicating that code here.
 2166  */
 2167 static void
 2168 ip_mloopback(ifp, m, dst, hlen)
 2169         struct ifnet *ifp;
 2170         register struct mbuf *m;
 2171         register struct sockaddr_in *dst;
 2172         int hlen;
 2173 {
 2174         register struct ip *ip;
 2175         struct mbuf *copym;
 2176 
 2177         copym = m_copy(m, 0, M_COPYALL);
 2178         if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
 2179                 copym = m_pullup(copym, hlen);
 2180         if (copym != NULL) {
 2181                 /* If needed, compute the checksum and mark it as valid. */
 2182                 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
 2183                         in_delayed_cksum(copym);
 2184                         copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
 2185                         copym->m_pkthdr.csum_flags |=
 2186                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
 2187                         copym->m_pkthdr.csum_data = 0xffff;
 2188                 }
 2189                 /*
 2190                  * We don't bother to fragment if the IP length is greater
 2191                  * than the interface's MTU.  Can this possibly matter?
 2192                  */
 2193                 ip = mtod(copym, struct ip *);
 2194                 ip->ip_len = htons(ip->ip_len);
 2195                 ip->ip_off = htons(ip->ip_off);
 2196                 ip->ip_sum = 0;
 2197                 if (ip->ip_vhl == IP_VHL_BORING) {
 2198                         ip->ip_sum = in_cksum_hdr(ip);
 2199                 } else {
 2200                         ip->ip_sum = in_cksum(copym, hlen);
 2201                 }
 2202                 /*
 2203                  * NB:
 2204                  * It's not clear whether there are any lingering
 2205                  * reentrancy problems in other areas which might
 2206                  * be exposed by using ip_input directly (in
 2207                  * particular, everything which modifies the packet
 2208                  * in-place).  Yet another option is using the
 2209                  * protosw directly to deliver the looped back
 2210                  * packet.  For the moment, we'll err on the side
 2211                  * of safety by using if_simloop().
 2212                  */
 2213 #if 1 /* XXX */
 2214                 if (dst->sin_family != AF_INET) {
 2215                         printf("ip_mloopback: bad address family %d\n",
 2216                                                 dst->sin_family);
 2217                         dst->sin_family = AF_INET;
 2218                 }
 2219 #endif
 2220 
 2221 #ifdef notdef
 2222                 copym->m_pkthdr.rcvif = ifp;
 2223                 ip_input(copym);
 2224 #else
 2225                 if_simloop(ifp, copym, dst->sin_family, 0);
 2226 #endif
 2227         }
 2228 }

Cache object: 21e8deb08c99050afc99ecd3ac7abee8


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