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

Cache object: d278eede6962799326aa3edc23957ee6


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