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

Cache object: 46bf6f8e0cc478b9efcf0f49bc6c98f9


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