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/bsd/netinet6/ip6_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 /*      $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.43 2002/10/31 19:45:48 ume Exp $    */
    2 /*      $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $    */
    3 
    4 /*
    5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 /*
   34  * Copyright (c) 1982, 1986, 1988, 1990, 1993
   35  *      The Regents of the University of California.  All rights reserved.
   36  *
   37  * Redistribution and use in source and binary forms, with or without
   38  * modification, are permitted provided that the following conditions
   39  * are met:
   40  * 1. Redistributions of source code must retain the above copyright
   41  *    notice, this list of conditions and the following disclaimer.
   42  * 2. Redistributions in binary form must reproduce the above copyright
   43  *    notice, this list of conditions and the following disclaimer in the
   44  *    documentation and/or other materials provided with the distribution.
   45  * 3. All advertising materials mentioning features or use of this software
   46  *    must display the following acknowledgement:
   47  *      This product includes software developed by the University of
   48  *      California, Berkeley and its contributors.
   49  * 4. Neither the name of the University nor the names of its contributors
   50  *    may be used to endorse or promote products derived from this software
   51  *    without specific prior written permission.
   52  *
   53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   63  * SUCH DAMAGE.
   64  *
   65  *      @(#)ip_output.c 8.3 (Berkeley) 1/21/94
   66  */
   67 /*
   68  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
   69  * support for mandatory and extensible security protections.  This notice
   70  * is included in support of clause 2.2 (b) of the Apple Public License,
   71  * Version 2.0.
   72  */
   73 
   74 #include <sys/param.h>
   75 #include <sys/malloc.h>
   76 #include <sys/mbuf.h>
   77 #include <sys/errno.h>
   78 #include <sys/protosw.h>
   79 #include <sys/socket.h>
   80 #include <sys/socketvar.h>
   81 #include <sys/systm.h>
   82 #include <sys/kernel.h>
   83 #include <sys/proc.h>
   84 #include <sys/kauth.h>
   85 
   86 #include <net/if.h>
   87 #include <net/route.h>
   88 #include <net/dlil.h>
   89 
   90 #include <netinet/in.h>
   91 #include <netinet/in_var.h>
   92 #include <netinet/ip_var.h>
   93 #include <netinet6/in6_var.h>
   94 #include <netinet/ip6.h>
   95 #include <netinet/icmp6.h>
   96 #include <netinet6/ip6_var.h>
   97 #include <netinet/in_pcb.h>
   98 #include <netinet6/nd6.h>
   99 
  100 #if IPSEC
  101 #include <netinet6/ipsec.h>
  102 #if INET6
  103 #include <netinet6/ipsec6.h>
  104 #endif
  105 #include <netkey/key.h>
  106 extern int ipsec_bypass;
  107 #endif /* IPSEC */
  108 extern lck_mtx_t *nd6_mutex;
  109 
  110 #if CONFIG_MACF_NET
  111 #include <security/mac.h>
  112 #endif /* MAC_NET */
  113 
  114 #include <netinet6/ip6_fw.h>
  115 
  116 #include <net/net_osdep.h>
  117 
  118 #include <netinet/kpi_ipfilter_var.h>
  119 
  120 #ifndef __APPLE__
  121 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
  122 #endif
  123 
  124 
  125 extern u_long  route_generation;
  126 
  127 struct ip6_exthdrs {
  128         struct mbuf *ip6e_ip6;
  129         struct mbuf *ip6e_hbh;
  130         struct mbuf *ip6e_dest1;
  131         struct mbuf *ip6e_rthdr;
  132         struct mbuf *ip6e_dest2;
  133 };
  134 
  135 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
  136                             struct socket *, struct sockopt *sopt);
  137 static int ip6_setmoptions(int, struct inpcb *, struct mbuf *);
  138 static int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf **);
  139 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
  140 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
  141                                   struct ip6_frag **);
  142 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
  143 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
  144 
  145 extern int ip_createmoptions(struct ip_moptions **imop);
  146 extern int ip_addmembership(struct ip_moptions *imo, struct ip_mreq *mreq);
  147 extern int ip_dropmembership(struct ip_moptions *imo, struct ip_mreq *mreq);
  148 extern lck_mtx_t *ip6_mutex;
  149 
  150 /*
  151  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
  152  * header (with pri, len, nxt, hlim, src, dst).
  153  * This function may modify ver and hlim only.
  154  * The mbuf chain containing the packet will be freed.
  155  * The mbuf opt, if present, will not be freed.
  156  *
  157  * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
  158  * nd_ifinfo.linkmtu is u_int32_t.  so we use u_long to hold largest one,
  159  * which is rt_rmx.rmx_mtu.
  160  */
  161 int
  162 ip6_output(
  163         struct mbuf *m0,
  164         struct ip6_pktopts *opt,
  165         struct route_in6 *ro,
  166         int flags,
  167         struct ip6_moptions *im6o,
  168         struct ifnet **ifpp,    /* XXX: just for statistics */
  169         int    locked)          
  170 {
  171         struct ip6_hdr *ip6, *mhip6;
  172         struct ifnet *ifp, *origifp;
  173         struct mbuf *m = m0;
  174         int hlen, tlen, len, off;
  175         struct route_in6 ip6route;
  176         struct sockaddr_in6 *dst;
  177         int error = 0;
  178         struct in6_ifaddr *ia = NULL;
  179         u_long mtu;
  180         u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
  181         struct ip6_exthdrs exthdrs;
  182         struct in6_addr finaldst;
  183         struct route_in6 *ro_pmtu = NULL;
  184         int hdrsplit = 0;
  185         int needipsec = 0;
  186         ipfilter_t inject_filter_ref;
  187         
  188 #if IPSEC
  189         int needipsectun = 0;
  190         struct socket *so = NULL;
  191         struct secpolicy *sp = NULL;
  192 
  193         if (!locked)
  194                 lck_mtx_lock(ip6_mutex);
  195         /* for AH processing. stupid to have "socket" variable in IP layer... */
  196         if (ipsec_bypass == 0)
  197         {
  198                 so = ipsec_getsocket(m);
  199                 (void)ipsec_setsocket(m, NULL);
  200         }
  201 #endif /* IPSEC */
  202 
  203         ip6 = mtod(m, struct ip6_hdr *);
  204         inject_filter_ref = ipf_get_inject_filter(m);
  205 
  206 #define MAKE_EXTHDR(hp, mp)                                             \
  207     do {                                                                \
  208         if (hp) {                                                       \
  209                 struct ip6_ext *eh = (struct ip6_ext *)(hp);            \
  210                 error = ip6_copyexthdr((mp), (caddr_t)(hp),             \
  211                                        ((eh)->ip6e_len + 1) << 3);      \
  212                 if (error)                                              \
  213                         goto freehdrs;                                  \
  214         }                                                               \
  215     } while (0)
  216         
  217         bzero(&exthdrs, sizeof(exthdrs));
  218         
  219         if (opt) {
  220                 /* Hop-by-Hop options header */
  221                 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
  222                 /* Destination options header(1st part) */
  223                 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
  224                 /* Routing header */
  225                 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
  226                 /* Destination options header(2nd part) */
  227                 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
  228         }
  229 
  230 #if IPSEC
  231         if (ipsec_bypass != 0)
  232                 goto skip_ipsec;
  233         
  234         /* get a security policy for this packet */
  235         if (so == NULL)
  236                 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
  237         else
  238                 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
  239 
  240         if (sp == NULL) {
  241                 IPSEC_STAT_INCREMENT(ipsec6stat.out_inval);
  242                 goto freehdrs;
  243         }
  244 
  245         error = 0;
  246 
  247         /* check policy */
  248         switch (sp->policy) {
  249         case IPSEC_POLICY_DISCARD:
  250         case IPSEC_POLICY_GENERATE:
  251                 /*
  252                  * This packet is just discarded.
  253                  */
  254                 IPSEC_STAT_INCREMENT(ipsec6stat.out_polvio);
  255                 goto freehdrs;
  256 
  257         case IPSEC_POLICY_BYPASS:
  258         case IPSEC_POLICY_NONE:
  259                 /* no need to do IPsec. */
  260                 needipsec = 0;
  261                 break;
  262         
  263         case IPSEC_POLICY_IPSEC:
  264                 if (sp->req == NULL) {
  265                         /* acquire a policy */
  266                         error = key_spdacquire(sp);
  267                         goto freehdrs;
  268                 }
  269                 needipsec = 1;
  270                 break;
  271 
  272         case IPSEC_POLICY_ENTRUST:
  273         default:
  274                 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
  275         }
  276         skip_ipsec:
  277 #endif /* IPSEC */
  278 
  279         /*
  280          * Calculate the total length of the extension header chain.
  281          * Keep the length of the unfragmentable part for fragmentation.
  282          */
  283         optlen = 0;
  284         if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
  285         if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
  286         if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
  287         unfragpartlen = optlen + sizeof(struct ip6_hdr);
  288         /* NOTE: we don't add AH/ESP length here. do that later. */
  289         if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
  290 
  291         /*
  292          * If we need IPsec, or there is at least one extension header,
  293          * separate IP6 header from the payload.
  294          */
  295         if ((needipsec || optlen) && !hdrsplit) {
  296                 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
  297                         m = NULL;
  298                         goto freehdrs;
  299                 }
  300                 m = exthdrs.ip6e_ip6;
  301                 hdrsplit++;
  302         }
  303 
  304         /* adjust pointer */
  305         ip6 = mtod(m, struct ip6_hdr *);
  306 
  307         /* adjust mbuf packet header length */
  308         m->m_pkthdr.len += optlen;
  309         plen = m->m_pkthdr.len - sizeof(*ip6);
  310 
  311         /* If this is a jumbo payload, insert a jumbo payload option. */
  312         if (plen > IPV6_MAXPACKET) {
  313                 if (!hdrsplit) {
  314                         if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
  315                                 m = NULL;
  316                                 goto freehdrs;
  317                         }
  318                         m = exthdrs.ip6e_ip6;
  319                         hdrsplit++;
  320                 }
  321                 /* adjust pointer */
  322                 ip6 = mtod(m, struct ip6_hdr *);
  323                 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
  324                         goto freehdrs;
  325                 ip6->ip6_plen = 0;
  326         } else
  327                 ip6->ip6_plen = htons(plen);
  328 
  329         /*
  330          * Concatenate headers and fill in next header fields.
  331          * Here we have, on "m"
  332          *      IPv6 payload
  333          * and we insert headers accordingly.  Finally, we should be getting:
  334          *      IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
  335          *
  336          * during the header composing process, "m" points to IPv6 header.
  337          * "mprev" points to an extension header prior to esp.
  338          */
  339         {
  340                 u_char *nexthdrp = &ip6->ip6_nxt;
  341                 struct mbuf *mprev = m;
  342 
  343                 /*
  344                  * we treat dest2 specially.  this makes IPsec processing
  345                  * much easier.  the goal here is to make mprev point the
  346                  * mbuf prior to dest2.
  347                  *
  348                  * result: IPv6 dest2 payload
  349                  * m and mprev will point to IPv6 header.
  350                  */
  351                 if (exthdrs.ip6e_dest2) {
  352                         if (!hdrsplit)
  353                                 panic("assumption failed: hdr not split");
  354                         exthdrs.ip6e_dest2->m_next = m->m_next;
  355                         m->m_next = exthdrs.ip6e_dest2;
  356                         *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
  357                         ip6->ip6_nxt = IPPROTO_DSTOPTS;
  358                 }
  359 
  360 #define MAKE_CHAIN(m, mp, p, i)\
  361     do {\
  362         if (m) {\
  363                 if (!hdrsplit) \
  364                         panic("assumption failed: hdr not split"); \
  365                 *mtod((m), u_char *) = *(p);\
  366                 *(p) = (i);\
  367                 p = mtod((m), u_char *);\
  368                 (m)->m_next = (mp)->m_next;\
  369                 (mp)->m_next = (m);\
  370                 (mp) = (m);\
  371         }\
  372     } while (0)
  373                 /*
  374                  * result: IPv6 hbh dest1 rthdr dest2 payload
  375                  * m will point to IPv6 header.  mprev will point to the
  376                  * extension header prior to dest2 (rthdr in the above case).
  377                  */
  378                 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
  379                            nexthdrp, IPPROTO_HOPOPTS);
  380                 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
  381                            nexthdrp, IPPROTO_DSTOPTS);
  382                 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
  383                            nexthdrp, IPPROTO_ROUTING);
  384 
  385                 if (!TAILQ_EMPTY(&ipv6_filters)) {
  386                         struct ipfilter *filter;
  387                         int seen = (inject_filter_ref == 0);
  388                         int     fixscope = 0;
  389                         struct ipf_pktopts *ippo = 0, ipf_pktopts;
  390                                                 
  391                         if (im6o != NULL && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  392                                 ippo = &ipf_pktopts;
  393                                 ippo->ippo_flags = IPPOF_MCAST_OPTS;
  394                                 ippo->ippo_mcast_ifnet = im6o->im6o_multicast_ifp;
  395                                 ippo->ippo_mcast_ttl = im6o->im6o_multicast_hlim;
  396                                 ippo->ippo_mcast_loop = im6o->im6o_multicast_loop;
  397                         }
  398 
  399                         /* Hack: embed the scope_id in the destination */
  400                         if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
  401                                 (ip6->ip6_dst.s6_addr16[1] == 0) && (ro != NULL)) {
  402                                 fixscope = 1;
  403                                 ip6->ip6_dst.s6_addr16[1] = htons(ro->ro_dst.sin6_scope_id);
  404                         }
  405                         {
  406                                 lck_mtx_unlock(ip6_mutex);
  407                                 ipf_ref();
  408                                 TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) {
  409                                         /*
  410                                          * No need to proccess packet twice if we've 
  411                                          * already seen it
  412                                          */
  413                                         if (seen == 0) {
  414                                                 if ((struct ipfilter *)inject_filter_ref == filter)
  415                                                         seen = 1;
  416                                         } else if (filter->ipf_filter.ipf_output) {
  417                                                 errno_t result;
  418                                                 
  419                                                 result = filter->ipf_filter.ipf_output(filter->ipf_filter.cookie, (mbuf_t*)&m, ippo);
  420                                                 if (result == EJUSTRETURN) {
  421                                                         ipf_unref();
  422                                                         locked = 1; /* Don't want to take lock to unlock it right away */
  423                                                         goto done;
  424                                                 }
  425                                                 if (result != 0) {
  426                                                         ipf_unref();
  427                                                         locked = 1; /* Don't want to take lock to unlock it right away */
  428                                                         goto bad;
  429                                                 }
  430                                         }
  431                                 }
  432                                 ipf_unref();
  433                                 lck_mtx_lock(ip6_mutex);
  434                         }
  435                         ip6 = mtod(m, struct ip6_hdr *);
  436                         /* Hack: cleanup embedded scope_id if we put it there */
  437                         if (fixscope)
  438                                 ip6->ip6_dst.s6_addr16[1] = 0;
  439                 }
  440 
  441 #if IPSEC
  442                 if (!needipsec)
  443                         goto skip_ipsec2;
  444 
  445                 /*
  446                  * pointers after IPsec headers are not valid any more.
  447                  * other pointers need a great care too.
  448                  * (IPsec routines should not mangle mbufs prior to AH/ESP)
  449                  */
  450                 exthdrs.ip6e_dest2 = NULL;
  451 
  452             {
  453                 struct ip6_rthdr *rh = NULL;
  454                 int segleft_org = 0;
  455                 struct ipsec_output_state state;
  456 
  457                 if (exthdrs.ip6e_rthdr) {
  458                         rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
  459                         segleft_org = rh->ip6r_segleft;
  460                         rh->ip6r_segleft = 0;
  461                 }
  462 
  463                 bzero(&state, sizeof(state));
  464                 state.m = m;
  465                 lck_mtx_unlock(ip6_mutex);
  466                 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
  467                         &needipsectun);
  468                 lck_mtx_lock(ip6_mutex);
  469                 m = state.m;
  470                 if (error) {
  471                         /* mbuf is already reclaimed in ipsec6_output_trans. */
  472                         m = NULL;
  473                         switch (error) {
  474                         case EHOSTUNREACH:
  475                         case ENETUNREACH:
  476                         case EMSGSIZE:
  477                         case ENOBUFS:
  478                         case ENOMEM:
  479                                 break;
  480                         default:
  481                                 printf("ip6_output (ipsec): error code %d\n", error);
  482                                 /* fall through */
  483                         case ENOENT:
  484                                 /* don't show these error codes to the user */
  485                                 error = 0;
  486                                 break;
  487                         }
  488                         goto bad;
  489                 }
  490                 if (exthdrs.ip6e_rthdr) {
  491                         /* ah6_output doesn't modify mbuf chain */
  492                         rh->ip6r_segleft = segleft_org;
  493                 }
  494             }
  495 skip_ipsec2:;
  496 #endif
  497         }
  498 
  499         /*
  500          * If there is a routing header, replace destination address field
  501          * with the first hop of the routing header.
  502          */
  503         if (exthdrs.ip6e_rthdr) {
  504                 struct ip6_rthdr *rh =
  505                         (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
  506                                                   struct ip6_rthdr *));
  507                 struct ip6_rthdr0 *rh0;
  508 
  509                 finaldst = ip6->ip6_dst;
  510                 switch (rh->ip6r_type) {
  511                 case IPV6_RTHDR_TYPE_0:
  512                          rh0 = (struct ip6_rthdr0 *)rh;
  513                          ip6->ip6_dst = rh0->ip6r0_addr[0];
  514                          bcopy((caddr_t)&rh0->ip6r0_addr[1],
  515                                (caddr_t)&rh0->ip6r0_addr[0],
  516                                sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
  517                                  );
  518                          rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
  519                          break;
  520                 default:        /* is it possible? */
  521                          error = EINVAL;
  522                          goto bad;
  523                 }
  524         }
  525 
  526         /* Source address validation */
  527         if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
  528             (flags & IPV6_DADOUTPUT) == 0) {
  529                 error = EOPNOTSUPP;
  530                 ip6stat.ip6s_badscope++;
  531                 goto bad;
  532         }
  533         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
  534                 error = EOPNOTSUPP;
  535                 ip6stat.ip6s_badscope++;
  536                 goto bad;
  537         }
  538 
  539         ip6stat.ip6s_localout++;
  540 
  541         /*
  542          * Route packet.
  543          */
  544         if (ro == 0) {
  545                 ro = &ip6route;
  546                 bzero((caddr_t)ro, sizeof(*ro));
  547         }
  548         ro_pmtu = ro;
  549         if (opt && opt->ip6po_rthdr)
  550                 ro = &opt->ip6po_route;
  551         dst = (struct sockaddr_in6 *)&ro->ro_dst;
  552         /*
  553          * If there is a cached route,
  554          * check that it is to the same destination
  555          * and is still up. If not, free it and try again.
  556          */
  557         if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
  558                          dst->sin6_family != AF_INET6 ||
  559                          !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst) ||
  560                          ro->ro_rt->generation_id != route_generation)) {
  561                 rtfree(ro->ro_rt);
  562                 ro->ro_rt = (struct rtentry *)0;
  563         }
  564         if (ro->ro_rt == 0) {
  565                 bzero(dst, sizeof(*dst));
  566                 dst->sin6_family = AF_INET6;
  567                 dst->sin6_len = sizeof(struct sockaddr_in6);
  568                 dst->sin6_addr = ip6->ip6_dst;
  569 #if SCOPEDROUTING
  570                 /* XXX: sin6_scope_id should already be fixed at this point */
  571                 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
  572                         dst->sin6_scope_id = ntohs(dst->sin6_addr.s6_addr16[1]);
  573 #endif
  574         }
  575 #if IPSEC
  576         if (needipsec && needipsectun) {
  577                 struct ipsec_output_state state;
  578                 int tunneledv4 = 0;
  579 
  580                 /*
  581                  * All the extension headers will become inaccessible
  582                  * (since they can be encrypted).
  583                  * Don't panic, we need no more updates to extension headers
  584                  * on inner IPv6 packet (since they are now encapsulated).
  585                  *
  586                  * IPv6 [ESP|AH] IPv6 [extension headers] payload
  587                  */
  588                 bzero(&exthdrs, sizeof(exthdrs));
  589                 exthdrs.ip6e_ip6 = m;
  590 
  591                 bzero(&state, sizeof(state));
  592                 state.m = m;
  593                 state.ro = (struct route *)ro;
  594                 state.dst = (struct sockaddr *)dst;
  595                 lck_mtx_unlock(ip6_mutex);
  596                 error = ipsec6_output_tunnel(&state, sp, flags, &tunneledv4);
  597                 lck_mtx_lock(ip6_mutex);
  598                 if (tunneledv4) /* tunneled in IPv4 - packet is gone */
  599                         goto done;
  600                 m = state.m;
  601                 ro = (struct route_in6 *)state.ro;
  602                 dst = (struct sockaddr_in6 *)state.dst;
  603                 if (error) {
  604                         /* mbuf is already reclaimed in ipsec6_output_tunnel. */
  605                         m0 = m = NULL;
  606                         m = NULL;
  607                         switch (error) {
  608                         case EHOSTUNREACH:
  609                         case ENETUNREACH:
  610                         case EMSGSIZE:
  611                         case ENOBUFS:
  612                         case ENOMEM:
  613                                 break;
  614                         default:
  615                                 printf("ip6_output (ipsec): error code %d\n", error);
  616                                 /* fall through */
  617                         case ENOENT:
  618                                 /* don't show these error codes to the user */
  619                                 error = 0;
  620                                 break;
  621                         }
  622                         goto bad;
  623                 }
  624 
  625                 exthdrs.ip6e_ip6 = m;
  626         }
  627 #endif /* IPSEC */
  628 
  629         if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  630                 /* Unicast */
  631 
  632 #define ifatoia6(ifa)   ((struct in6_ifaddr *)(ifa))
  633 #define sin6tosa(sin6)  ((struct sockaddr *)(sin6))
  634                 /* xxx
  635                  * interface selection comes here
  636                  * if an interface is specified from an upper layer,
  637                  * ifp must point it.
  638                  */
  639                 lck_mtx_lock(rt_mtx);
  640                 if (ro->ro_rt == 0) {
  641                         /*
  642                          * non-bsdi always clone routes, if parent is
  643                          * PRF_CLONING.
  644                          */
  645                         rtalloc_ign_locked((struct route *)ro, 0UL);
  646                 }
  647                 if (ro->ro_rt == 0) {
  648                         ip6stat.ip6s_noroute++;
  649                         error = EHOSTUNREACH;
  650                         lck_mtx_unlock(rt_mtx);
  651                         /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
  652                         goto bad;
  653                 }
  654                 ia = ifatoia6(ro->ro_rt->rt_ifa);
  655                 ifp = ro->ro_rt->rt_ifp;
  656                 ro->ro_rt->rt_use++;
  657                 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
  658                         dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
  659                 lck_mtx_unlock(rt_mtx);
  660                 m->m_flags &= ~(M_BCAST | M_MCAST);     /* just in case */
  661 
  662                 in6_ifstat_inc(ifp, ifs6_out_request);
  663 
  664                 /*
  665                  * Check if the outgoing interface conflicts with
  666                  * the interface specified by ifi6_ifindex (if specified).
  667                  * Note that loopback interface is always okay.
  668                  * (this may happen when we are sending a packet to one of
  669                  *  our own addresses.)
  670                  */
  671                 if (opt && opt->ip6po_pktinfo
  672                  && opt->ip6po_pktinfo->ipi6_ifindex) {
  673                         if (!(ifp->if_flags & IFF_LOOPBACK)
  674                          && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
  675                                 ip6stat.ip6s_noroute++;
  676                                 in6_ifstat_inc(ifp, ifs6_out_discard);
  677                                 error = EHOSTUNREACH;
  678                                 goto bad;
  679                         }
  680                 }
  681 
  682                 if (opt && opt->ip6po_hlim != -1)
  683                         ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
  684         } else {
  685                 /* Multicast */
  686                 struct  in6_multi *in6m;
  687 
  688                 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
  689 
  690                 /*
  691                  * See if the caller provided any multicast options
  692                  */
  693                 ifp = NULL;
  694                 if (im6o != NULL) {
  695                         ip6->ip6_hlim = im6o->im6o_multicast_hlim;
  696                         if (im6o->im6o_multicast_ifp != NULL)
  697                                 ifp = im6o->im6o_multicast_ifp;
  698                 } else
  699                         ip6->ip6_hlim = ip6_defmcasthlim;
  700 
  701                 /*
  702                  * See if the caller provided the outgoing interface
  703                  * as an ancillary data.
  704                  * Boundary check for ifindex is assumed to be already done.
  705                  */
  706                 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
  707                         ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
  708 
  709                 /*
  710                  * If the destination is a node-local scope multicast,
  711                  * the packet should be loop-backed only.
  712                  */
  713                 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
  714                         /*
  715                          * If the outgoing interface is already specified,
  716                          * it should be a loopback interface.
  717                          */
  718                         if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
  719                                 ip6stat.ip6s_badscope++;
  720                                 error = ENETUNREACH; /* XXX: better error? */
  721                                 /* XXX correct ifp? */
  722                                 in6_ifstat_inc(ifp, ifs6_out_discard);
  723                                 goto bad;
  724                         } else {
  725                                 ifp = lo_ifp;
  726                         }
  727                 }
  728 
  729                 if (opt && opt->ip6po_hlim != -1)
  730                         ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
  731 
  732                 /*
  733                  * If caller did not provide an interface lookup a
  734                  * default in the routing table.  This is either a
  735                  * default for the speicfied group (i.e. a host
  736                  * route), or a multicast default (a route for the
  737                  * ``net'' ff00::/8).
  738                  */
  739                 if (ifp == NULL) {
  740                         lck_mtx_lock(rt_mtx);
  741                         if (ro->ro_rt == 0) {
  742                                 ro->ro_rt = rtalloc1_locked(
  743                                     (struct sockaddr *)&ro->ro_dst, 0, 0UL);
  744                         }
  745                         if (ro->ro_rt == 0) {
  746                                 ip6stat.ip6s_noroute++;
  747                                 lck_mtx_unlock(rt_mtx);
  748                                 error = EHOSTUNREACH;
  749                                 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
  750                                 goto bad;
  751                         }
  752                         ia = ifatoia6(ro->ro_rt->rt_ifa);
  753                         ifp = ro->ro_rt->rt_ifp;
  754                         ro->ro_rt->rt_use++;
  755                         lck_mtx_unlock(rt_mtx);
  756                 }
  757 
  758                 if ((flags & IPV6_FORWARDING) == 0)
  759                         in6_ifstat_inc(ifp, ifs6_out_request);
  760                 in6_ifstat_inc(ifp, ifs6_out_mcast);
  761 
  762                 /*
  763                  * Confirm that the outgoing interface supports multicast.
  764                  */
  765                 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
  766                         ip6stat.ip6s_noroute++;
  767                         in6_ifstat_inc(ifp, ifs6_out_discard);
  768                         error = ENETUNREACH;
  769                         goto bad;
  770                 }
  771                 ifnet_lock_shared(ifp);
  772                 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
  773                 ifnet_lock_done(ifp);
  774                 if (in6m != NULL &&
  775                    (im6o == NULL || im6o->im6o_multicast_loop)) {
  776                         /*
  777                          * If we belong to the destination multicast group
  778                          * on the outgoing interface, and the caller did not
  779                          * forbid loopback, loop back a copy.
  780                          */
  781                         ip6_mloopback(ifp, m, dst);
  782                 } else {
  783                         /*
  784                          * If we are acting as a multicast router, perform
  785                          * multicast forwarding as if the packet had just
  786                          * arrived on the interface to which we are about
  787                          * to send.  The multicast forwarding function
  788                          * recursively calls this function, using the
  789                          * IPV6_FORWARDING flag to prevent infinite recursion.
  790                          *
  791                          * Multicasts that are looped back by ip6_mloopback(),
  792                          * above, will be forwarded by the ip6_input() routine,
  793                          * if necessary.
  794                          */
  795                         if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
  796                                 if (ip6_mforward(ip6, ifp, m) != 0) {
  797                                         m_freem(m);
  798                                         goto done;
  799                                 }
  800                         }
  801                 }
  802                 /*
  803                  * Multicasts with a hoplimit of zero may be looped back,
  804                  * above, but must not be transmitted on a network.
  805                  * Also, multicasts addressed to the loopback interface
  806                  * are not sent -- the above call to ip6_mloopback() will
  807                  * loop back a copy if this host actually belongs to the
  808                  * destination group on the loopback interface.
  809                  */
  810                 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
  811                         m_freem(m);
  812                         goto done;
  813                 }
  814         }
  815 
  816         /*
  817          * Fill the outgoing inteface to tell the upper layer
  818          * to increment per-interface statistics.
  819          */
  820         if (ifpp)
  821                 *ifpp = ifp;
  822 
  823         /*
  824          * Determine path MTU.
  825          */
  826         if (ro_pmtu != ro) {
  827                 /* The first hop and the final destination may differ. */
  828                 struct sockaddr_in6 *sin6_fin =
  829                         (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
  830                 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
  831                                        !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr,
  832                                                            &finaldst))) {
  833                         rtfree(ro_pmtu->ro_rt);
  834                         ro_pmtu->ro_rt = (struct rtentry *)0;
  835                 }
  836                 if (ro_pmtu->ro_rt == 0) {
  837                         bzero(sin6_fin, sizeof(*sin6_fin));
  838                         sin6_fin->sin6_family = AF_INET6;
  839                         sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
  840                         sin6_fin->sin6_addr = finaldst;
  841 
  842                         rtalloc((struct route *)ro_pmtu);
  843                 }
  844         }
  845         if (ro_pmtu->ro_rt != NULL) {
  846                 u_int32_t ifmtu = IN6_LINKMTU(ifp);
  847 
  848                 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
  849                 if (mtu > ifmtu || mtu == 0) {
  850                         /*
  851                          * The MTU on the route is larger than the MTU on
  852                          * the interface!  This shouldn't happen, unless the
  853                          * MTU of the interface has been changed after the
  854                          * interface was brought up.  Change the MTU in the
  855                          * route to match the interface MTU (as long as the
  856                          * field isn't locked).
  857                          *
  858                          * if MTU on the route is 0, we need to fix the MTU.
  859                          * this case happens with path MTU discovery timeouts.
  860                          */
  861                          mtu = ifmtu;
  862                          if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
  863                                  ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
  864                 }
  865         } else {
  866                 mtu = IN6_LINKMTU(ifp);
  867         }
  868 
  869         /*
  870          * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
  871          */
  872         if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU)
  873                 mtu = IPV6_MMTU;
  874 
  875         /* Fake scoped addresses */
  876         if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
  877                 /*
  878                  * If source or destination address is a scoped address, and
  879                  * the packet is going to be sent to a loopback interface,
  880                  * we should keep the original interface.
  881                  */
  882 
  883                 /*
  884                  * XXX: this is a very experimental and temporary solution.
  885                  * We eventually have sockaddr_in6 and use the sin6_scope_id
  886                  * field of the structure here.
  887                  * We rely on the consistency between two scope zone ids
  888                  * of source and destination, which should already be assured.
  889                  * Larger scopes than link will be supported in the future. 
  890                  */
  891                 origifp = NULL;
  892                 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
  893                         origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
  894                 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
  895                         origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
  896                 /*
  897                  * XXX: origifp can be NULL even in those two cases above.
  898                  * For example, if we remove the (only) link-local address
  899                  * from the loopback interface, and try to send a link-local
  900                  * address without link-id information.  Then the source
  901                  * address is ::1, and the destination address is the
  902                  * link-local address with its s6_addr16[1] being zero.
  903                  * What is worse, if the packet goes to the loopback interface
  904                  * by a default rejected route, the null pointer would be
  905                  * passed to looutput, and the kernel would hang.
  906                  * The following last resort would prevent such disaster.
  907                  */
  908                 if (origifp == NULL)
  909                         origifp = ifp;
  910         }
  911         else
  912                 origifp = ifp;
  913 #ifndef SCOPEDROUTING
  914         /*
  915          * clear embedded scope identifiers if necessary.
  916          * in6_clearscope will touch the addresses only when necessary.
  917          */
  918         in6_clearscope(&ip6->ip6_src);
  919         in6_clearscope(&ip6->ip6_dst);
  920 #endif
  921 
  922         /*
  923          * Check with the firewall...
  924          */
  925         if (ip6_fw_enable && ip6_fw_chk_ptr) {
  926                 u_short port = 0;
  927                 m->m_pkthdr.rcvif = NULL;       /* XXX */
  928                 /* If ipfw says divert, we have to just drop packet */
  929                 if (ip6_fw_chk_ptr(&ip6, ifp, &port, &m)) {
  930                         m_freem(m);
  931                         goto done;
  932                 }
  933                 if (!m) {
  934                         error = EACCES;
  935                         goto done;
  936                 }
  937         }
  938 
  939         /*
  940          * If the outgoing packet contains a hop-by-hop options header,
  941          * it must be examined and processed even by the source node.
  942          * (RFC 2460, section 4.)
  943          */
  944         if (exthdrs.ip6e_hbh) {
  945                 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
  946                 u_int32_t dummy1; /* XXX unused */
  947                 u_int32_t dummy2; /* XXX unused */
  948 
  949 #if DIAGNOSTIC
  950                 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
  951                         panic("ip6e_hbh is not continuous");
  952 #endif
  953                 /*
  954                  *  XXX: if we have to send an ICMPv6 error to the sender,
  955                  *       we need the M_LOOP flag since icmp6_error() expects
  956                  *       the IPv6 and the hop-by-hop options header are
  957                  *       continuous unless the flag is set.
  958                  */
  959                 m->m_flags |= M_LOOP;
  960                 m->m_pkthdr.rcvif = ifp;
  961                 if (ip6_process_hopopts(m,
  962                                         (u_int8_t *)(hbh + 1),
  963                                         ((hbh->ip6h_len + 1) << 3) -
  964                                         sizeof(struct ip6_hbh),
  965                                         &dummy1, &dummy2) < 0) {
  966                         /* m was already freed at this point */
  967                         error = EINVAL;/* better error? */
  968                         goto done;
  969                 }
  970                 m->m_flags &= ~M_LOOP; /* XXX */
  971                 m->m_pkthdr.rcvif = NULL;
  972         }
  973 
  974         /*
  975          * Send the packet to the outgoing interface.
  976          * If necessary, do IPv6 fragmentation before sending.
  977          */
  978         tlen = m->m_pkthdr.len;
  979         if (tlen <= mtu
  980 #if notyet
  981             /*
  982              * On any link that cannot convey a 1280-octet packet in one piece,
  983              * link-specific fragmentation and reassembly must be provided at
  984              * a layer below IPv6. [RFC 2460, sec.5]
  985              * Thus if the interface has ability of link-level fragmentation,
  986              * we can just send the packet even if the packet size is
  987              * larger than the link's MTU.
  988              * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
  989              */
  990         
  991             || ifp->if_flags & IFF_FRAGMENTABLE
  992 #endif
  993             )
  994         {
  995                 /* Record statistics for this interface address. */
  996                 if (ia && !(flags & IPV6_FORWARDING)) {
  997 #ifndef __APPLE__
  998                         ia->ia_ifa.if_opackets++;
  999                         ia->ia_ifa.if_obytes += m->m_pkthdr.len;
 1000 #endif
 1001                 }
 1002 #ifdef IPSEC
 1003                 /* clean ipsec history once it goes out of the node */
 1004                 ipsec_delaux(m);
 1005 #endif
 1006 
 1007                 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt, 1);
 1008                 goto done;
 1009         } else if (mtu < IPV6_MMTU) {
 1010                 /*
 1011                  * note that path MTU is never less than IPV6_MMTU
 1012                  * (see icmp6_input).
 1013                  */
 1014                 error = EMSGSIZE;
 1015                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
 1016                 goto bad;
 1017         } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
 1018                 error = EMSGSIZE;
 1019                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
 1020                 goto bad;
 1021         } else {
 1022                 struct mbuf **mnext, *m_frgpart;
 1023                 struct ip6_frag *ip6f = NULL;
 1024                 u_int32_t id = htonl(ip6_id++);
 1025                 u_char nextproto;
 1026 
 1027                 /*
 1028                  * Too large for the destination or interface;
 1029                  * fragment if possible.
 1030                  * Must be able to put at least 8 bytes per fragment.
 1031                  */
 1032                 hlen = unfragpartlen;
 1033                 if (mtu > IPV6_MAXPACKET)
 1034                         mtu = IPV6_MAXPACKET;
 1035 
 1036                 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
 1037                 if (len < 8) {
 1038                         error = EMSGSIZE;
 1039                         in6_ifstat_inc(ifp, ifs6_out_fragfail);
 1040                         goto bad;
 1041                 }
 1042 
 1043                 mnext = &m->m_nextpkt;
 1044 
 1045                 /*
 1046                  * Change the next header field of the last header in the
 1047                  * unfragmentable part.
 1048                  */
 1049                 if (exthdrs.ip6e_rthdr) {
 1050                         nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
 1051                         *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
 1052                 } else if (exthdrs.ip6e_dest1) {
 1053                         nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
 1054                         *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
 1055                 } else if (exthdrs.ip6e_hbh) {
 1056                         nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
 1057                         *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
 1058                 } else {
 1059                         nextproto = ip6->ip6_nxt;
 1060                         ip6->ip6_nxt = IPPROTO_FRAGMENT;
 1061                 }
 1062 
 1063                 /*
 1064                  * Loop through length of segment after first fragment,
 1065                  * make new header and copy data of each part and link onto
 1066                  * chain.
 1067                  */
 1068                 m0 = m;
 1069                 for (off = hlen; off < tlen; off += len) {
 1070                         MGETHDR(m, M_DONTWAIT, MT_HEADER);      /* MAC-OK */
 1071                         if (!m) {
 1072                                 error = ENOBUFS;
 1073                                 ip6stat.ip6s_odropped++;
 1074                                 goto sendorfree;
 1075                         }
 1076                         m->m_pkthdr.rcvif = NULL;
 1077                         m->m_flags = m0->m_flags & M_COPYFLAGS;
 1078                         *mnext = m;
 1079                         mnext = &m->m_nextpkt;
 1080                         m->m_data += max_linkhdr;
 1081                         mhip6 = mtod(m, struct ip6_hdr *);
 1082                         *mhip6 = *ip6;
 1083                         m->m_len = sizeof(*mhip6);
 1084                         error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
 1085                         if (error) {
 1086                                 ip6stat.ip6s_odropped++;
 1087                                 goto sendorfree;
 1088                         }
 1089                         ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
 1090                         if (off + len >= tlen)
 1091                                 len = tlen - off;
 1092                         else
 1093                                 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
 1094                         mhip6->ip6_plen = htons((u_short)(len + hlen +
 1095                                                           sizeof(*ip6f) -
 1096                                                           sizeof(struct ip6_hdr)));
 1097                         if ((m_frgpart = m_copy(m0, off, len)) == 0) {
 1098                                 error = ENOBUFS;
 1099                                 ip6stat.ip6s_odropped++;
 1100                                 goto sendorfree;
 1101                         }
 1102                         m_cat(m, m_frgpart);
 1103                         m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
 1104                         m->m_pkthdr.rcvif = 0;
 1105                         m->m_pkthdr.socket_id = m0->m_pkthdr.socket_id;
 1106 #ifdef __darwin8_notyet
 1107 #if CONFIG_MACF_NET
 1108                         mac_create_fragment(m0, m);
 1109 #endif
 1110 #endif
 1111                         ip6f->ip6f_reserved = 0;
 1112                         ip6f->ip6f_ident = id;
 1113                         ip6f->ip6f_nxt = nextproto;
 1114                         ip6stat.ip6s_ofragments++;
 1115                         in6_ifstat_inc(ifp, ifs6_out_fragcreat);
 1116                 }
 1117 
 1118                 in6_ifstat_inc(ifp, ifs6_out_fragok);
 1119         }
 1120 
 1121         /*
 1122          * Remove leading garbages.
 1123          */
 1124 sendorfree:
 1125         m = m0->m_nextpkt;
 1126         m0->m_nextpkt = 0;
 1127         m_freem(m0);
 1128         for (m0 = m; m; m = m0) {
 1129                 m0 = m->m_nextpkt;
 1130                 m->m_nextpkt = 0;
 1131                 if (error == 0) {
 1132                         /* Record statistics for this interface address. */
 1133                         if (ia) {
 1134 #ifndef __APPLE__
 1135                                 ia->ia_ifa.if_opackets++;
 1136                                 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
 1137 #endif
 1138                         }
 1139 #if IPSEC
 1140                         /* clean ipsec history once it goes out of the node */
 1141                         ipsec_delaux(m);
 1142 #endif
 1143                         error = nd6_output(ifp, origifp, m, dst, ro->ro_rt, 1);
 1144 
 1145                 } else
 1146                         m_freem(m);
 1147         }
 1148 
 1149         if (error == 0)
 1150                 ip6stat.ip6s_fragmented++;
 1151 
 1152 done:
 1153         if (!locked)
 1154                 lck_mtx_unlock(ip6_mutex);
 1155         if (ro == &ip6route && ro->ro_rt) { /* brace necessary for rtfree */
 1156                 rtfree(ro->ro_rt);
 1157         } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
 1158                 rtfree(ro_pmtu->ro_rt);
 1159         }
 1160 
 1161 #if IPSEC
 1162         if (sp != NULL)
 1163                 key_freesp(sp, KEY_SADB_UNLOCKED);
 1164 #endif /* IPSEC */
 1165 
 1166         return(error);
 1167 
 1168 freehdrs:
 1169         m_freem(exthdrs.ip6e_hbh);      /* m_freem will check if mbuf is 0 */
 1170         m_freem(exthdrs.ip6e_dest1);
 1171         m_freem(exthdrs.ip6e_rthdr);
 1172         m_freem(exthdrs.ip6e_dest2);
 1173         /* fall through */
 1174 bad:
 1175         m_freem(m);
 1176         goto done;
 1177 }
 1178 
 1179 static int
 1180 ip6_copyexthdr(mp, hdr, hlen)
 1181         struct mbuf **mp;
 1182         caddr_t hdr;
 1183         int hlen;
 1184 {
 1185         struct mbuf *m;
 1186 
 1187         if (hlen > MCLBYTES)
 1188                 return(ENOBUFS); /* XXX */
 1189 
 1190         MGET(m, M_DONTWAIT, MT_DATA);
 1191         if (!m)
 1192                 return(ENOBUFS);
 1193 
 1194         if (hlen > MLEN) {
 1195                 MCLGET(m, M_DONTWAIT);
 1196                 if ((m->m_flags & M_EXT) == 0) {
 1197                         m_free(m);
 1198                         return(ENOBUFS);
 1199                 }
 1200         }
 1201         m->m_len = hlen;
 1202         if (hdr)
 1203                 bcopy(hdr, mtod(m, caddr_t), hlen);
 1204 
 1205         *mp = m;
 1206         return(0);
 1207 }
 1208 
 1209 /*
 1210  * Insert jumbo payload option.
 1211  */
 1212 static int
 1213 ip6_insert_jumboopt(exthdrs, plen)
 1214         struct ip6_exthdrs *exthdrs;
 1215         u_int32_t plen;
 1216 {
 1217         struct mbuf *mopt;
 1218         u_char *optbuf;
 1219         u_int32_t v;
 1220 
 1221 #define JUMBOOPTLEN     8       /* length of jumbo payload option and padding */
 1222 
 1223         /*
 1224          * If there is no hop-by-hop options header, allocate new one.
 1225          * If there is one but it doesn't have enough space to store the
 1226          * jumbo payload option, allocate a cluster to store the whole options.
 1227          * Otherwise, use it to store the options.
 1228          */
 1229         if (exthdrs->ip6e_hbh == 0) {
 1230                 MGET(mopt, M_DONTWAIT, MT_DATA);
 1231                 if (mopt == 0)
 1232                         return(ENOBUFS);
 1233                 mopt->m_len = JUMBOOPTLEN;
 1234                 optbuf = mtod(mopt, u_char *);
 1235                 optbuf[1] = 0;  /* = ((JUMBOOPTLEN) >> 3) - 1 */
 1236                 exthdrs->ip6e_hbh = mopt;
 1237         } else {
 1238                 struct ip6_hbh *hbh;
 1239 
 1240                 mopt = exthdrs->ip6e_hbh;
 1241                 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
 1242                         /*
 1243                          * XXX assumption:
 1244                          * - exthdrs->ip6e_hbh is not referenced from places
 1245                          *   other than exthdrs.
 1246                          * - exthdrs->ip6e_hbh is not an mbuf chain.
 1247                          */
 1248                         int oldoptlen = mopt->m_len;
 1249                         struct mbuf *n;
 1250 
 1251                         /*
 1252                          * XXX: give up if the whole (new) hbh header does
 1253                          * not fit even in an mbuf cluster.
 1254                          */
 1255                         if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
 1256                                 return(ENOBUFS);
 1257 
 1258                         /*
 1259                          * As a consequence, we must always prepare a cluster
 1260                          * at this point.
 1261                          */
 1262                         MGET(n, M_DONTWAIT, MT_DATA);
 1263                         if (n) {
 1264                                 MCLGET(n, M_DONTWAIT);
 1265                                 if ((n->m_flags & M_EXT) == 0) {
 1266                                         m_freem(n);
 1267                                         n = NULL;
 1268                                 }
 1269                         }
 1270                         if (!n)
 1271                                 return(ENOBUFS);
 1272                         n->m_len = oldoptlen + JUMBOOPTLEN;
 1273                         bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
 1274                               oldoptlen);
 1275                         optbuf = (u_char *) (mtod(n, caddr_t) + oldoptlen);
 1276                         m_freem(mopt);
 1277                         mopt = exthdrs->ip6e_hbh = n;
 1278                 } else {
 1279                         optbuf = mtod(mopt, u_char *) + mopt->m_len;
 1280                         mopt->m_len += JUMBOOPTLEN;
 1281                 }
 1282                 optbuf[0] = IP6OPT_PADN;
 1283                 optbuf[1] = 1;
 1284 
 1285                 /*
 1286                  * Adjust the header length according to the pad and
 1287                  * the jumbo payload option.
 1288                  */
 1289                 hbh = mtod(mopt, struct ip6_hbh *);
 1290                 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
 1291         }
 1292 
 1293         /* fill in the option. */
 1294         optbuf[2] = IP6OPT_JUMBO;
 1295         optbuf[3] = 4;
 1296         v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
 1297         bcopy(&v, &optbuf[4], sizeof(u_int32_t));
 1298 
 1299         /* finally, adjust the packet header length */
 1300         exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
 1301 
 1302         return(0);
 1303 #undef JUMBOOPTLEN
 1304 }
 1305 
 1306 /*
 1307  * Insert fragment header and copy unfragmentable header portions.
 1308  */
 1309 static int
 1310 ip6_insertfraghdr(m0, m, hlen, frghdrp)
 1311         struct mbuf *m0, *m;
 1312         int hlen;
 1313         struct ip6_frag **frghdrp;
 1314 {
 1315         struct mbuf *n, *mlast;
 1316 
 1317         if (hlen > sizeof(struct ip6_hdr)) {
 1318                 n = m_copym(m0, sizeof(struct ip6_hdr),
 1319                             hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
 1320                 if (n == 0)
 1321                         return(ENOBUFS);
 1322                 m->m_next = n;
 1323         } else
 1324                 n = m;
 1325 
 1326         /* Search for the last mbuf of unfragmentable part. */
 1327         for (mlast = n; mlast->m_next; mlast = mlast->m_next)
 1328                 ;
 1329 
 1330         if ((mlast->m_flags & M_EXT) == 0 &&
 1331             M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
 1332                 /* use the trailing space of the last mbuf for the fragment hdr */
 1333                 *frghdrp =
 1334                         (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
 1335                 mlast->m_len += sizeof(struct ip6_frag);
 1336                 m->m_pkthdr.len += sizeof(struct ip6_frag);
 1337         } else {
 1338                 /* allocate a new mbuf for the fragment header */
 1339                 struct mbuf *mfrg;
 1340 
 1341                 MGET(mfrg, M_DONTWAIT, MT_DATA);
 1342                 if (mfrg == 0)
 1343                         return(ENOBUFS);
 1344                 mfrg->m_len = sizeof(struct ip6_frag);
 1345                 *frghdrp = mtod(mfrg, struct ip6_frag *);
 1346                 mlast->m_next = mfrg;
 1347         }
 1348 
 1349         return(0);
 1350 }
 1351 
 1352 extern int load_ipfw(void);
 1353 
 1354 /*
 1355  * IP6 socket option processing.
 1356  */
 1357 int
 1358 ip6_ctloutput(so, sopt)
 1359         struct socket *so;
 1360         struct sockopt *sopt;
 1361 {
 1362         int privileged;
 1363         struct inpcb *in6p = sotoinpcb(so);
 1364         int error, optval;
 1365         int level, op = -1, optname = 0;
 1366         int optlen = 0;
 1367         struct proc *p = NULL;
 1368 
 1369         level = error = optval = 0;
 1370         if (sopt == NULL) 
 1371                 panic("ip6_ctloutput: arg soopt is NULL");
 1372         else {
 1373                 level = sopt->sopt_level;
 1374                 op = sopt->sopt_dir;
 1375                 optname = sopt->sopt_name;
 1376                 optlen = sopt->sopt_valsize;
 1377                 p = sopt->sopt_p;
 1378         }
 1379 
 1380         privileged = (p == 0 || proc_suser(p)) ? 0 : 1;
 1381 
 1382         if (level == IPPROTO_IPV6) {
 1383                 switch (op) {
 1384 
 1385                 case SOPT_SET:
 1386                         switch (optname) {
 1387                         case IPV6_PKTOPTIONS:
 1388                         {
 1389                                 struct mbuf *m;
 1390 
 1391                                 if (sopt->sopt_valsize > MCLBYTES) {
 1392                                         error = EMSGSIZE;
 1393                                         break;
 1394                                 }
 1395                                 error = soopt_getm(sopt, &m); /* XXX */
 1396                                 if (error != 0)
 1397                                         break;
 1398                                 error = soopt_mcopyin(sopt, m); /* XXX */
 1399                                 if (error != 0)
 1400                                         break;
 1401                                 error = ip6_pcbopts(&in6p->in6p_outputopts,
 1402                                                     m, so, sopt);
 1403                                 m_freem(m); /* XXX */
 1404                                 break;
 1405                         }
 1406 
 1407                         /*
 1408                          * Use of some Hop-by-Hop options or some
 1409                          * Destination options, might require special
 1410                          * privilege.  That is, normal applications
 1411                          * (without special privilege) might be forbidden
 1412                          * from setting certain options in outgoing packets,
 1413                          * and might never see certain options in received
 1414                          * packets. [RFC 2292 Section 6]
 1415                          * KAME specific note:
 1416                          *  KAME prevents non-privileged users from sending or
 1417                          *  receiving ANY hbh/dst options in order to avoid
 1418                          *  overhead of parsing options in the kernel.
 1419                          */
 1420                         case IPV6_UNICAST_HOPS:
 1421                         case IPV6_CHECKSUM:
 1422                         case IPV6_FAITH:
 1423 
 1424                         case IPV6_V6ONLY:
 1425                                 if (optlen != sizeof(int)) {
 1426                                         error = EINVAL;
 1427                                         break;
 1428                                 }
 1429                                 error = sooptcopyin(sopt, &optval,
 1430                                         sizeof optval, sizeof optval);
 1431                                 if (error)
 1432                                         break;
 1433                                 switch (optname) {
 1434 
 1435                                 case IPV6_UNICAST_HOPS:
 1436                                         if (optval < -1 || optval >= 256)
 1437                                                 error = EINVAL;
 1438                                         else {
 1439                                                 /* -1 = kernel default */
 1440                                                 in6p->in6p_hops = optval;
 1441 
 1442                                                 if ((in6p->in6p_vflag &
 1443                                                      INP_IPV4) != 0)
 1444                                                         in6p->inp_ip_ttl = optval;
 1445                                         }
 1446                                         break;
 1447 #define OPTSET(bit) \
 1448 do { \
 1449         if (optval) \
 1450                 in6p->in6p_flags |= (bit); \
 1451         else \
 1452                 in6p->in6p_flags &= ~(bit); \
 1453 } while (0)
 1454 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
 1455 
 1456                                 case IPV6_CHECKSUM:
 1457                                         in6p->in6p_cksum = optval;
 1458                                         break;
 1459 
 1460                                 case IPV6_FAITH:
 1461                                         OPTSET(IN6P_FAITH);
 1462                                         break;
 1463 
 1464                                 case IPV6_V6ONLY:
 1465                                         /*
 1466                                          * make setsockopt(IPV6_V6ONLY)
 1467                                          * available only prior to bind(2).
 1468                                          * see ipng mailing list, Jun 22 2001.
 1469                                          */
 1470                                         if (in6p->in6p_lport ||
 1471                                             !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
 1472                                         {
 1473                                                 error = EINVAL;
 1474                                                 break;
 1475                                         }
 1476                                         OPTSET(IN6P_IPV6_V6ONLY);
 1477                                         if (optval)
 1478                                                 in6p->in6p_vflag &= ~INP_IPV4;
 1479                                         else
 1480                                                 in6p->in6p_vflag |= INP_IPV4;
 1481                                         break;
 1482                                 }
 1483                                 break;
 1484 
 1485                         case IPV6_PKTINFO:
 1486                         case IPV6_HOPLIMIT:
 1487                         case IPV6_HOPOPTS:
 1488                         case IPV6_DSTOPTS:
 1489                         case IPV6_RTHDR:
 1490                                 /* RFC 2292 */
 1491                                 if (optlen != sizeof(int)) {
 1492                                         error = EINVAL;
 1493                                         break;
 1494                                 }
 1495                                 error = sooptcopyin(sopt, &optval,
 1496                                         sizeof optval, sizeof optval);
 1497                                 if (error)
 1498                                         break;
 1499                                 switch (optname) {
 1500                                 case IPV6_PKTINFO:
 1501                                         OPTSET(IN6P_PKTINFO);
 1502                                         break;
 1503                                 case IPV6_HOPLIMIT:
 1504                                         OPTSET(IN6P_HOPLIMIT);
 1505                                         break;
 1506                                 case IPV6_HOPOPTS:
 1507                                         /*
 1508                                          * Check super-user privilege.
 1509                                          * See comments for IPV6_RECVHOPOPTS.
 1510                                          */
 1511                                         if (!privileged)
 1512                                                 return(EPERM);
 1513                                         OPTSET(IN6P_HOPOPTS);
 1514                                         break;
 1515                                 case IPV6_DSTOPTS:
 1516                                         if (!privileged)
 1517                                                 return(EPERM);
 1518                                         OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
 1519                                         break;
 1520                                 case IPV6_RTHDR:
 1521                                         OPTSET(IN6P_RTHDR);
 1522                                         break;
 1523                                 }
 1524                                 break;
 1525 #undef OPTSET
 1526 
 1527                         case IPV6_MULTICAST_IF:
 1528                         case IPV6_MULTICAST_HOPS:
 1529                         case IPV6_MULTICAST_LOOP:
 1530                         case IPV6_JOIN_GROUP:
 1531                         case IPV6_LEAVE_GROUP:
 1532                             {
 1533                                 struct mbuf *m;
 1534                                 if (sopt->sopt_valsize > MLEN) {
 1535                                         error = EMSGSIZE;
 1536                                         break;
 1537                                 }
 1538                                 /* XXX */
 1539                                 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_HEADER);
 1540                                 if (m == 0) {
 1541                                         error = ENOBUFS;
 1542                                         break;
 1543                                 }
 1544                                 m->m_len = sopt->sopt_valsize;
 1545                                 error = sooptcopyin(sopt, mtod(m, char *),
 1546                                                     m->m_len, m->m_len);
 1547                                 error = ip6_setmoptions(sopt->sopt_name, in6p, m);
 1548                                 (void)m_free(m);
 1549                             }
 1550                                 break;
 1551 
 1552                         case IPV6_PORTRANGE:
 1553                                 error = sooptcopyin(sopt, &optval,
 1554                                     sizeof optval, sizeof optval);
 1555                                 if (error)
 1556                                         break;
 1557 
 1558                                 switch (optval) {
 1559                                 case IPV6_PORTRANGE_DEFAULT:
 1560                                         in6p->in6p_flags &= ~(IN6P_LOWPORT);
 1561                                         in6p->in6p_flags &= ~(IN6P_HIGHPORT);
 1562                                         break;
 1563 
 1564                                 case IPV6_PORTRANGE_HIGH:
 1565                                         in6p->in6p_flags &= ~(IN6P_LOWPORT);
 1566                                         in6p->in6p_flags |= IN6P_HIGHPORT;
 1567                                         break;
 1568 
 1569                                 case IPV6_PORTRANGE_LOW:
 1570                                         in6p->in6p_flags &= ~(IN6P_HIGHPORT);
 1571                                         in6p->in6p_flags |= IN6P_LOWPORT;
 1572                                         break;
 1573 
 1574                                 default:
 1575                                         error = EINVAL;
 1576                                         break;
 1577                                 }
 1578                                 break;
 1579 
 1580 #if IPSEC
 1581                         case IPV6_IPSEC_POLICY:
 1582                             {
 1583                                 caddr_t req = NULL;
 1584                                 size_t len = 0;
 1585                                 struct mbuf *m;
 1586 
 1587                                 if (sopt->sopt_valsize > MCLBYTES) {
 1588                                         error = EMSGSIZE;
 1589                                         break;
 1590                                 }
 1591                                 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
 1592                                         break;
 1593                                 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
 1594                                         break;
 1595                                 if (m) {
 1596                                         req = mtod(m, caddr_t);
 1597                                         len = m->m_len;
 1598                                 }
 1599                                 error = ipsec6_set_policy(in6p, optname, req,
 1600                                                           len, privileged);
 1601                                 m_freem(m);
 1602                             }
 1603                                 break;
 1604 #endif /* KAME IPSEC */
 1605 
 1606 #if IPFIREWALL
 1607                         case IPV6_FW_ADD:
 1608                         case IPV6_FW_DEL:
 1609                         case IPV6_FW_FLUSH:
 1610                         case IPV6_FW_ZERO:
 1611                                 {
 1612                                 if (ip6_fw_ctl_ptr == NULL && load_ipfw() != 0)
 1613                                         return EINVAL;
 1614 
 1615                                 error = (*ip6_fw_ctl_ptr)(sopt);
 1616                                 }
 1617                                 break;
 1618 #endif /* IPFIREWALL */
 1619 
 1620                         default:
 1621                                 error = ENOPROTOOPT;
 1622                                 break;
 1623                         }
 1624                         break;
 1625 
 1626                 case SOPT_GET:
 1627                         switch (optname) {
 1628 
 1629                         case IPV6_PKTOPTIONS:
 1630                                 if (in6p->in6p_options) {
 1631                                         struct mbuf *m;
 1632                                         m = m_copym(in6p->in6p_options,
 1633                                             0, M_COPYALL, M_WAIT);
 1634                                         error = soopt_mcopyout(sopt, m);
 1635                                         if (error == 0)
 1636                                                 m_freem(m);
 1637                                 } else
 1638                                         sopt->sopt_valsize = 0;
 1639                                 break;
 1640 
 1641                         case IPV6_UNICAST_HOPS:
 1642                         case IPV6_CHECKSUM:
 1643 
 1644                         case IPV6_FAITH:
 1645                         case IPV6_V6ONLY:
 1646                         case IPV6_PORTRANGE:
 1647                                 switch (optname) {
 1648 
 1649                                 case IPV6_UNICAST_HOPS:
 1650                                         optval = in6p->in6p_hops;
 1651                                         break;
 1652 
 1653                                 case IPV6_CHECKSUM:
 1654                                         optval = in6p->in6p_cksum;
 1655                                         break;
 1656 
 1657                                 case IPV6_FAITH:
 1658                                         optval = OPTBIT(IN6P_FAITH);
 1659                                         break;
 1660 
 1661                                 case IPV6_V6ONLY:
 1662                                         optval = OPTBIT(IN6P_IPV6_V6ONLY);
 1663                                         break;
 1664 
 1665                                 case IPV6_PORTRANGE:
 1666                                     {
 1667                                         int flags;
 1668                                         flags = in6p->in6p_flags;
 1669                                         if (flags & IN6P_HIGHPORT)
 1670                                                 optval = IPV6_PORTRANGE_HIGH;
 1671                                         else if (flags & IN6P_LOWPORT)
 1672                                                 optval = IPV6_PORTRANGE_LOW;
 1673                                         else
 1674                                                 optval = 0;
 1675                                         break;
 1676                                     }
 1677                                 }
 1678                                 error = sooptcopyout(sopt, &optval,
 1679                                         sizeof optval);
 1680                                 break;
 1681 
 1682                         case IPV6_PKTINFO:
 1683                         case IPV6_HOPLIMIT:
 1684                         case IPV6_HOPOPTS:
 1685                         case IPV6_RTHDR:
 1686                         case IPV6_DSTOPTS:
 1687                                 if ((optname == IPV6_HOPOPTS ||
 1688                                     optname == IPV6_DSTOPTS) &&
 1689                                     !privileged)
 1690                                         return(EPERM);
 1691                                 switch (optname) {
 1692                                 case IPV6_PKTINFO:
 1693                                         optval = OPTBIT(IN6P_PKTINFO);
 1694                                         break;
 1695                                 case IPV6_HOPLIMIT:
 1696                                         optval = OPTBIT(IN6P_HOPLIMIT);
 1697                                         break;
 1698                                 case IPV6_HOPOPTS:
 1699                                         if (!privileged)
 1700                                                 return(EPERM);
 1701                                         optval = OPTBIT(IN6P_HOPOPTS);
 1702                                         break;
 1703                                 case IPV6_RTHDR:
 1704                                         optval = OPTBIT(IN6P_RTHDR);
 1705                                         break;
 1706                                 case IPV6_DSTOPTS:
 1707                                         if (!privileged)
 1708                                                 return(EPERM);
 1709                                         optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
 1710                                         break;
 1711                                 }
 1712                                 error = sooptcopyout(sopt, &optval,
 1713                                         sizeof optval);
 1714                                 break;
 1715 
 1716                         case IPV6_MULTICAST_IF:
 1717                         case IPV6_MULTICAST_HOPS:
 1718                         case IPV6_MULTICAST_LOOP:
 1719                         case IPV6_JOIN_GROUP:
 1720                         case IPV6_LEAVE_GROUP:
 1721                             {
 1722                                 struct mbuf *m;
 1723                                 error = ip6_getmoptions(sopt->sopt_name,
 1724                                                 in6p->in6p_moptions, &m);
 1725                                 if (error == 0)
 1726                                         error = sooptcopyout(sopt,
 1727                                                 mtod(m, char *), m->m_len);
 1728                                 m_freem(m);
 1729                             }
 1730                                 break;
 1731 
 1732 #if IPSEC
 1733                         case IPV6_IPSEC_POLICY:
 1734                           {
 1735                                 caddr_t req = NULL;
 1736                                 size_t len = 0;
 1737                                 struct mbuf *m = NULL;
 1738                                 struct mbuf **mp = &m;
 1739 
 1740                                 if (sopt->sopt_valsize > MCLBYTES) {
 1741                                         error = EMSGSIZE;
 1742                                         break;
 1743                                 }
 1744                                 error = soopt_getm(sopt, &m); /* XXX */
 1745                                 if (error != 0)
 1746                                         break;
 1747                                 error = soopt_mcopyin(sopt, m); /* XXX */
 1748                                 if (error != 0)
 1749                                         break;
 1750                                 if (m) {
 1751                                         req = mtod(m, caddr_t);
 1752                                         len = m->m_len;
 1753                                 }
 1754                                 error = ipsec6_get_policy(in6p, req, len, mp);
 1755                                 if (error == 0)
 1756                                         error = soopt_mcopyout(sopt, m); /*XXX*/
 1757                                 if (error == 0 && m)
 1758                                         m_freem(m);
 1759                                 break;
 1760                           }
 1761 #endif /* KAME IPSEC */
 1762 
 1763 #if IPFIREWALL
 1764                         case IPV6_FW_GET:
 1765                                 {
 1766                                 if (ip6_fw_ctl_ptr == NULL && load_ipfw() != 0)
 1767                                         return EINVAL;
 1768 
 1769                                 error = (*ip6_fw_ctl_ptr)(sopt);
 1770                                 }
 1771                                 break;
 1772 #endif /* IPFIREWALL */
 1773 
 1774                         default:
 1775                                 error = ENOPROTOOPT;
 1776                                 break;
 1777                         }
 1778                         break;
 1779                 }
 1780         } else {
 1781                 error = EINVAL;
 1782         }
 1783         return(error);
 1784 }
 1785 
 1786 /*
 1787  * Set up IP6 options in pcb for insertion in output packets or
 1788  * specifying behavior of outgoing packets.
 1789  */
 1790 static int
 1791 ip6_pcbopts(
 1792         struct ip6_pktopts **pktopt,
 1793         struct mbuf *m,
 1794         __unused struct socket *so,
 1795         struct sockopt *sopt)
 1796 {
 1797         struct ip6_pktopts *opt = *pktopt;
 1798         int error = 0;
 1799         struct proc *p = sopt->sopt_p;
 1800         int priv = 0;
 1801 
 1802         /* turn off any old options. */
 1803         if (opt) {
 1804 #if DIAGNOSTIC
 1805                 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
 1806                     opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
 1807                     opt->ip6po_rhinfo.ip6po_rhi_rthdr)
 1808                         printf("ip6_pcbopts: all specified options are cleared.\n");
 1809 #endif
 1810                 ip6_clearpktopts(opt, 1, -1);
 1811         } else
 1812                 opt = _MALLOC(sizeof(*opt), M_IP6OPT, M_WAITOK);
 1813         *pktopt = NULL;
 1814 
 1815         if (!m || m->m_len == 0) {
 1816                 /*
 1817                  * Only turning off any previous options, regardless of
 1818                  * whether the opt is just created or given.
 1819                  */
 1820                 if (opt)
 1821                         FREE(opt, M_IP6OPT);
 1822                 return(0);
 1823         }
 1824 
 1825         /*  set options specified by user. */
 1826         if (p && !proc_suser(p))
 1827                 priv = 1;
 1828         if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) {
 1829                 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */
 1830                 FREE(opt, M_IP6OPT);
 1831                 return(error);
 1832         }
 1833         *pktopt = opt;
 1834         return(0);
 1835 }
 1836 
 1837 /*
 1838  * initialize ip6_pktopts.  beware that there are non-zero default values in
 1839  * the struct.
 1840  */
 1841 void
 1842 init_ip6pktopts(opt)
 1843         struct ip6_pktopts *opt;
 1844 {
 1845 
 1846         bzero(opt, sizeof(*opt));
 1847         opt->ip6po_hlim = -1;   /* -1 means default hop limit */
 1848 }
 1849 
 1850 void
 1851 ip6_clearpktopts(pktopt, needfree, optname)
 1852         struct ip6_pktopts *pktopt;
 1853         int needfree, optname;
 1854 {
 1855         if (pktopt == NULL)
 1856                 return;
 1857 
 1858         if (optname == -1) {
 1859                 if (needfree && pktopt->ip6po_pktinfo)
 1860                         FREE(pktopt->ip6po_pktinfo, M_IP6OPT);
 1861                 pktopt->ip6po_pktinfo = NULL;
 1862         }
 1863         if (optname == -1)
 1864                 pktopt->ip6po_hlim = -1;
 1865         if (optname == -1) {
 1866                 if (needfree && pktopt->ip6po_nexthop)
 1867                         FREE(pktopt->ip6po_nexthop, M_IP6OPT);
 1868                 pktopt->ip6po_nexthop = NULL;
 1869         }
 1870         if (optname == -1) {
 1871                 if (needfree && pktopt->ip6po_hbh)
 1872                         FREE(pktopt->ip6po_hbh, M_IP6OPT);
 1873                 pktopt->ip6po_hbh = NULL;
 1874         }
 1875         if (optname == -1) {
 1876                 if (needfree && pktopt->ip6po_dest1)
 1877                         FREE(pktopt->ip6po_dest1, M_IP6OPT);
 1878                 pktopt->ip6po_dest1 = NULL;
 1879         }
 1880         if (optname == -1) {
 1881                 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
 1882                         FREE(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
 1883                 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
 1884                 if (pktopt->ip6po_route.ro_rt) {
 1885                         rtfree(pktopt->ip6po_route.ro_rt);
 1886                         pktopt->ip6po_route.ro_rt = NULL;
 1887                 }
 1888         }
 1889         if (optname == -1) {
 1890                 if (needfree && pktopt->ip6po_dest2)
 1891                         FREE(pktopt->ip6po_dest2, M_IP6OPT);
 1892                 pktopt->ip6po_dest2 = NULL;
 1893         }
 1894 }
 1895 
 1896 #define PKTOPT_EXTHDRCPY(type) \
 1897 do {\
 1898         if (src->type) {\
 1899                 int hlen =\
 1900                         (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
 1901                 dst->type = _MALLOC(hlen, M_IP6OPT, canwait);\
 1902                 if (dst->type == NULL && canwait == M_NOWAIT)\
 1903                         goto bad;\
 1904                 bcopy(src->type, dst->type, hlen);\
 1905         }\
 1906 } while (0)
 1907 
 1908 struct ip6_pktopts *
 1909 ip6_copypktopts(src, canwait)
 1910         struct ip6_pktopts *src;
 1911         int canwait;
 1912 {
 1913         struct ip6_pktopts *dst;
 1914 
 1915         if (src == NULL) {
 1916                 printf("ip6_clearpktopts: invalid argument\n");
 1917                 return(NULL);
 1918         }
 1919 
 1920         dst = _MALLOC(sizeof(*dst), M_IP6OPT, canwait);
 1921         if (dst == NULL && canwait == M_NOWAIT)
 1922                 return (NULL);
 1923         bzero(dst, sizeof(*dst));
 1924 
 1925         dst->ip6po_hlim = src->ip6po_hlim;
 1926         if (src->ip6po_pktinfo) {
 1927                 dst->ip6po_pktinfo = _MALLOC(sizeof(*dst->ip6po_pktinfo),
 1928                                             M_IP6OPT, canwait);
 1929                 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
 1930                         goto bad;
 1931                 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
 1932         }
 1933         if (src->ip6po_nexthop) {
 1934                 dst->ip6po_nexthop = _MALLOC(src->ip6po_nexthop->sa_len,
 1935                                             M_IP6OPT, canwait);
 1936                 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
 1937                         goto bad;
 1938                 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
 1939                       src->ip6po_nexthop->sa_len);
 1940         }
 1941         PKTOPT_EXTHDRCPY(ip6po_hbh);
 1942         PKTOPT_EXTHDRCPY(ip6po_dest1);
 1943         PKTOPT_EXTHDRCPY(ip6po_dest2);
 1944         PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
 1945         return(dst);
 1946 
 1947   bad:
 1948         if (dst->ip6po_pktinfo) FREE(dst->ip6po_pktinfo, M_IP6OPT);
 1949         if (dst->ip6po_nexthop) FREE(dst->ip6po_nexthop, M_IP6OPT);
 1950         if (dst->ip6po_hbh) FREE(dst->ip6po_hbh, M_IP6OPT);
 1951         if (dst->ip6po_dest1) FREE(dst->ip6po_dest1, M_IP6OPT);
 1952         if (dst->ip6po_dest2) FREE(dst->ip6po_dest2, M_IP6OPT);
 1953         if (dst->ip6po_rthdr) FREE(dst->ip6po_rthdr, M_IP6OPT);
 1954         FREE(dst, M_IP6OPT);
 1955         return(NULL);
 1956 }
 1957 #undef PKTOPT_EXTHDRCPY
 1958 
 1959 void
 1960 ip6_freepcbopts(pktopt)
 1961         struct ip6_pktopts *pktopt;
 1962 {
 1963         if (pktopt == NULL)
 1964                 return;
 1965 
 1966         ip6_clearpktopts(pktopt, 1, -1);
 1967 
 1968         FREE(pktopt, M_IP6OPT);
 1969 }
 1970 
 1971 /*
 1972  * Set the IP6 multicast options in response to user setsockopt().
 1973  */
 1974 static int
 1975 ip6_setmoptions(
 1976         int optname,
 1977         struct inpcb* in6p,
 1978         struct mbuf *m)
 1979 {
 1980         int error = 0;
 1981         u_int loop, ifindex;
 1982         struct ipv6_mreq *mreq;
 1983         struct ifnet *ifp;
 1984         struct ip6_moptions **im6op = &in6p->in6p_moptions;
 1985         struct ip6_moptions *im6o = *im6op;
 1986         struct ip_moptions *imo;
 1987         struct route_in6 ro;
 1988         struct sockaddr_in6 *dst;
 1989         struct in6_multi_mship *imm;
 1990 
 1991         if (im6o == NULL) {
 1992                 /*
 1993                  * No multicast option buffer attached to the pcb;
 1994                  * allocate one and initialize to default values.
 1995                  */
 1996                 im6o = (struct ip6_moptions *)
 1997                         _MALLOC(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
 1998 
 1999                 if (im6o == NULL)
 2000                         return(ENOBUFS);
 2001                 *im6op = im6o;
 2002                 im6o->im6o_multicast_ifp = NULL;
 2003                 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
 2004                 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
 2005                 LIST_INIT(&im6o->im6o_memberships);
 2006         }
 2007         
 2008         if (in6p->inp_moptions == NULL) {
 2009                 /*
 2010                  * No IPv4 multicast option buffer attached to the pcb;
 2011                  * call ip_createmoptions to allocate one and initialize
 2012                  * to default values.
 2013                  */
 2014                 error = ip_createmoptions(&in6p->inp_moptions);
 2015                 if (error != 0)
 2016                         return error;
 2017         }
 2018         imo = in6p->inp_moptions;
 2019 
 2020         switch (optname) {
 2021 
 2022         case IPV6_MULTICAST_IF:
 2023                 /*
 2024                  * Select the interface for outgoing multicast packets.
 2025                  */
 2026                 if (m == NULL || m->m_len != sizeof(u_int)) {
 2027                         error = EINVAL;
 2028                         break;
 2029                 }
 2030                 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
 2031                 if (ifindex < 0 || if_index < ifindex) {
 2032                         error = ENXIO;  /* XXX EINVAL? */
 2033                         break;
 2034                 }
 2035                 ifp = ifindex2ifnet[ifindex];
 2036                 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
 2037                         error = EADDRNOTAVAIL;
 2038                         break;
 2039                 }
 2040                 im6o->im6o_multicast_ifp = ifp;
 2041                 imo->imo_multicast_ifp = ifp;
 2042                 break;
 2043 
 2044         case IPV6_MULTICAST_HOPS:
 2045             {
 2046                 /*
 2047                  * Set the IP6 hoplimit for outgoing multicast packets.
 2048                  */
 2049                 int optval;
 2050                 if (m == NULL || m->m_len != sizeof(int)) {
 2051                         error = EINVAL;
 2052                         break;
 2053                 }
 2054                 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
 2055                 if (optval < -1 || optval >= 256)
 2056                         error = EINVAL;
 2057                 else if (optval == -1) {
 2058                         im6o->im6o_multicast_hlim = ip6_defmcasthlim;
 2059                         imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
 2060                 } else {
 2061                         im6o->im6o_multicast_hlim = optval;
 2062                         imo->imo_multicast_ttl = optval;
 2063                 }
 2064                 break;
 2065             }
 2066 
 2067         case IPV6_MULTICAST_LOOP:
 2068                 /*
 2069                  * Set the loopback flag for outgoing multicast packets.
 2070                  * Must be zero or one.
 2071                  */
 2072                 if (m == NULL || m->m_len != sizeof(u_int)) {
 2073                         error = EINVAL;
 2074                         break;
 2075                 }
 2076                 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
 2077                 if (loop > 1) {
 2078                         error = EINVAL;
 2079                         break;
 2080                 }
 2081                 im6o->im6o_multicast_loop = loop;
 2082                 imo->imo_multicast_loop = loop;
 2083                 break;
 2084 
 2085         case IPV6_JOIN_GROUP:
 2086                 /*
 2087                  * Add a multicast group membership.
 2088                  * Group must be a valid IP6 multicast address.
 2089                  */
 2090                 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
 2091                         error = EINVAL;
 2092                         break;
 2093                 }
 2094                 mreq = mtod(m, struct ipv6_mreq *);
 2095                 /*
 2096                  * If the interface is specified, validate it.
 2097                  */
 2098                 if (mreq->ipv6mr_interface < 0
 2099                  || if_index < mreq->ipv6mr_interface) {
 2100                         error = ENXIO;  /* XXX EINVAL? */
 2101                         break;
 2102                 }
 2103                 
 2104                 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
 2105                         /*
 2106                          * We use the unspecified address to specify to accept
 2107                          * all multicast addresses. Only super user is allowed
 2108                          * to do this.
 2109                          */
 2110                         if (suser(kauth_cred_get(), 0))
 2111                         {
 2112                                 error = EACCES;
 2113                                 break;
 2114                         }
 2115                 } else if (IN6_IS_ADDR_V4MAPPED(&mreq->ipv6mr_multiaddr)) {
 2116                         struct ip_mreq v4req;
 2117                         
 2118                         v4req.imr_multiaddr.s_addr = mreq->ipv6mr_multiaddr.s6_addr32[3];
 2119                         v4req.imr_interface.s_addr = INADDR_ANY;
 2120                         
 2121                         /* Find an IPv4 address on the specified interface. */
 2122                         if (mreq->ipv6mr_interface != 0) {
 2123                                 struct in_ifaddr *ifa;
 2124 
 2125                                 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
 2126 
 2127                                 lck_mtx_lock(rt_mtx);
 2128                                 TAILQ_FOREACH(ifa, &in_ifaddrhead, ia_link) {
 2129                                         if (ifa->ia_ifp == ifp) {
 2130                                                 v4req.imr_interface = IA_SIN(ifa)->sin_addr;
 2131                                                 break;
 2132                                         }
 2133                                 }
 2134                                 lck_mtx_unlock(rt_mtx);
 2135                                 
 2136                                 if (v4req.imr_multiaddr.s_addr == 0) {
 2137                                         /* Interface has no IPv4 address. */
 2138                                         error = EINVAL;
 2139                                         break;
 2140                                 }
 2141                         }
 2142                         
 2143                         error = ip_addmembership(imo, &v4req);
 2144                         break;
 2145                 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
 2146                         error = EINVAL;
 2147                         break;
 2148                 }
 2149                 /*
 2150                  * If no interface was explicitly specified, choose an
 2151                  * appropriate one according to the given multicast address.
 2152                  */
 2153                 if (mreq->ipv6mr_interface == 0) {
 2154                         /*
 2155                          * If the multicast address is in node-local scope,
 2156                          * the interface should be a loopback interface.
 2157                          * Otherwise, look up the routing table for the
 2158                          * address, and choose the outgoing interface.
 2159                          *   XXX: is it a good approach?
 2160                          */
 2161                         if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
 2162                                 ifp = lo_ifp;
 2163                         } else {
 2164                                 ro.ro_rt = NULL;
 2165                                 dst = (struct sockaddr_in6 *)&ro.ro_dst;
 2166                                 bzero(dst, sizeof(*dst));
 2167                                 dst->sin6_len = sizeof(struct sockaddr_in6);
 2168                                 dst->sin6_family = AF_INET6;
 2169                                 dst->sin6_addr = mreq->ipv6mr_multiaddr;
 2170                                 rtalloc((struct route *)&ro);
 2171                                 if (ro.ro_rt == NULL) {
 2172                                         error = EADDRNOTAVAIL;
 2173                                         break;
 2174                                 }
 2175                                 ifp = ro.ro_rt->rt_ifp;
 2176                                 rtfree(ro.ro_rt);
 2177                         }
 2178                 } else
 2179                         ifp = ifindex2ifnet[mreq->ipv6mr_interface];
 2180 
 2181                 /*
 2182                  * See if we found an interface, and confirm that it
 2183                  * supports multicast
 2184                  */
 2185                 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
 2186                         error = EADDRNOTAVAIL;
 2187                         break;
 2188                 }
 2189                 /*
 2190                  * Put interface index into the multicast address,
 2191                  * if the address has link-local scope.
 2192                  */
 2193                 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
 2194                         mreq->ipv6mr_multiaddr.s6_addr16[1]
 2195                                 = htons(mreq->ipv6mr_interface);
 2196                 }
 2197                 /*
 2198                  * See if the membership already exists.
 2199                  */
 2200                 lck_mtx_lock(nd6_mutex);
 2201                 for (imm = im6o->im6o_memberships.lh_first;
 2202                      imm != NULL; imm = imm->i6mm_chain.le_next)
 2203                         if (imm->i6mm_maddr->in6m_ifp == ifp &&
 2204                             IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
 2205                                                &mreq->ipv6mr_multiaddr))
 2206                                 break;
 2207                 if (imm != NULL) {
 2208                         error = EADDRINUSE;
 2209                         lck_mtx_unlock(nd6_mutex);
 2210                         break;
 2211                 }
 2212                 /*
 2213                  * Everything looks good; add a new record to the multicast
 2214                  * address list for the given interface.
 2215                  */
 2216                 imm = _MALLOC(sizeof(*imm), M_IPMADDR, M_WAITOK);
 2217                 if (imm == NULL) {
 2218                         error = ENOBUFS;
 2219                         lck_mtx_unlock(nd6_mutex);
 2220                         break;
 2221                 }
 2222                 if ((imm->i6mm_maddr =
 2223                      in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error, 1)) == NULL) {
 2224                         FREE(imm, M_IPMADDR);
 2225                         lck_mtx_unlock(nd6_mutex);
 2226                         break;
 2227                 }
 2228                 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
 2229                 lck_mtx_unlock(nd6_mutex);
 2230                 break;
 2231 
 2232         case IPV6_LEAVE_GROUP:
 2233                 /*
 2234                  * Drop a multicast group membership.
 2235                  * Group must be a valid IP6 multicast address.
 2236                  */
 2237                 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
 2238                         error = EINVAL;
 2239                         break;
 2240                 }
 2241                 mreq = mtod(m, struct ipv6_mreq *);
 2242                 /*
 2243                  * If an interface address was specified, get a pointer
 2244                  * to its ifnet structure.
 2245                  */
 2246                 if (mreq->ipv6mr_interface < 0
 2247                  || if_index < mreq->ipv6mr_interface) {
 2248                         error = ENXIO;  /* XXX EINVAL? */
 2249                         break;
 2250                 }
 2251                 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
 2252                 
 2253                 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
 2254                         if (suser(kauth_cred_get(), 0)) {
 2255                                 error = EACCES;
 2256                                 break;
 2257                         }
 2258                 } else if (IN6_IS_ADDR_V4MAPPED(&mreq->ipv6mr_multiaddr)) {
 2259                         struct ip_mreq v4req;
 2260                         
 2261                         v4req.imr_multiaddr.s_addr = mreq->ipv6mr_multiaddr.s6_addr32[3];
 2262                         v4req.imr_interface.s_addr = INADDR_ANY;
 2263                         
 2264                         if (ifp != NULL) {
 2265                                 struct in_ifaddr *ifa;
 2266                                 
 2267                                 lck_mtx_lock(rt_mtx);
 2268                                 TAILQ_FOREACH(ifa, &in_ifaddrhead, ia_link) {
 2269                                         if (ifa->ia_ifp == ifp) {
 2270                                                 v4req.imr_interface = IA_SIN(ifa)->sin_addr;
 2271                                                 break;
 2272                                         }
 2273                                 }
 2274                                 lck_mtx_unlock(rt_mtx);
 2275                         }
 2276                         
 2277                         error = ip_dropmembership(imo, &v4req);
 2278                         break;
 2279                 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
 2280                         error = EINVAL;
 2281                         break;
 2282                 }
 2283                 /*
 2284                  * Put interface index into the multicast address,
 2285                  * if the address has link-local scope.
 2286                  */
 2287                 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
 2288                         mreq->ipv6mr_multiaddr.s6_addr16[1]
 2289                                 = htons(mreq->ipv6mr_interface);
 2290                 }
 2291                 /*
 2292                  * Find the membership in the membership list.
 2293                  */
 2294                 lck_mtx_lock(nd6_mutex);
 2295                 for (imm = im6o->im6o_memberships.lh_first;
 2296                      imm != NULL; imm = imm->i6mm_chain.le_next) {
 2297                         if ((ifp == NULL ||
 2298                              imm->i6mm_maddr->in6m_ifp == ifp) &&
 2299                             IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
 2300                                                &mreq->ipv6mr_multiaddr))
 2301                                 break;
 2302                 }
 2303                 if (imm == NULL) {
 2304                         /* Unable to resolve interface */
 2305                         error = EADDRNOTAVAIL;
 2306                         lck_mtx_unlock(nd6_mutex);
 2307                         break;
 2308                 }
 2309                 /*
 2310                  * Give up the multicast address record to which the
 2311                  * membership points.
 2312                  */
 2313                 LIST_REMOVE(imm, i6mm_chain);
 2314                 in6_delmulti(imm->i6mm_maddr, 1);
 2315                 lck_mtx_unlock(nd6_mutex);
 2316                 FREE(imm, M_IPMADDR);
 2317                 break;
 2318 
 2319         default:
 2320                 error = EOPNOTSUPP;
 2321                 break;
 2322         }
 2323 
 2324         /*
 2325          * If all options have default values, no need to keep the mbuf.
 2326          */
 2327         lck_mtx_lock(nd6_mutex);
 2328         if (im6o->im6o_multicast_ifp == NULL &&
 2329             im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
 2330             im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
 2331             im6o->im6o_memberships.lh_first == NULL) {
 2332                 FREE(*im6op, M_IPMOPTS);
 2333                 *im6op = NULL;
 2334         }
 2335         if (imo->imo_multicast_ifp == NULL &&
 2336             imo->imo_multicast_vif == -1 &&
 2337             imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
 2338             imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
 2339             imo->imo_num_memberships == 0) {
 2340                 ip_freemoptions(imo);
 2341                 in6p->inp_moptions = 0;
 2342         }
 2343         lck_mtx_unlock(nd6_mutex);
 2344 
 2345         return(error);
 2346 }
 2347 
 2348 /*
 2349  * Return the IP6 multicast options in response to user getsockopt().
 2350  */
 2351 static int
 2352 ip6_getmoptions(optname, im6o, mp)
 2353         int optname;
 2354         struct ip6_moptions *im6o;
 2355         struct mbuf **mp;
 2356 {
 2357         u_int *hlim, *loop, *ifindex;
 2358 
 2359         *mp = m_get(M_WAIT, MT_HEADER);         /*XXX*/
 2360 
 2361         switch (optname) {
 2362 
 2363         case IPV6_MULTICAST_IF:
 2364                 ifindex = mtod(*mp, u_int *);
 2365                 (*mp)->m_len = sizeof(u_int);
 2366                 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
 2367                         *ifindex = 0;
 2368                 else
 2369                         *ifindex = im6o->im6o_multicast_ifp->if_index;
 2370                 return(0);
 2371 
 2372         case IPV6_MULTICAST_HOPS:
 2373                 hlim = mtod(*mp, u_int *);
 2374                 (*mp)->m_len = sizeof(u_int);
 2375                 if (im6o == NULL)
 2376                         *hlim = ip6_defmcasthlim;
 2377                 else
 2378                         *hlim = im6o->im6o_multicast_hlim;
 2379                 return(0);
 2380 
 2381         case IPV6_MULTICAST_LOOP:
 2382                 loop = mtod(*mp, u_int *);
 2383                 (*mp)->m_len = sizeof(u_int);
 2384                 if (im6o == NULL)
 2385                         *loop = ip6_defmcasthlim;
 2386                 else
 2387                         *loop = im6o->im6o_multicast_loop;
 2388                 return(0);
 2389 
 2390         default:
 2391                 return(EOPNOTSUPP);
 2392         }
 2393 }
 2394 
 2395 /*
 2396  * Discard the IP6 multicast options.
 2397  */
 2398 void
 2399 ip6_freemoptions(im6o)
 2400         struct ip6_moptions *im6o;
 2401 {
 2402         struct in6_multi_mship *imm;
 2403 
 2404         if (im6o == NULL)
 2405                 return;
 2406         
 2407         lck_mtx_lock(nd6_mutex);
 2408         while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
 2409                 LIST_REMOVE(imm, i6mm_chain);
 2410                 if (imm->i6mm_maddr)
 2411                         in6_delmulti(imm->i6mm_maddr, 1);
 2412                 FREE(imm, M_IPMADDR);
 2413         }
 2414         lck_mtx_unlock(nd6_mutex);
 2415         FREE(im6o, M_IPMOPTS);
 2416 }
 2417 
 2418 /*
 2419  * Set IPv6 outgoing packet options based on advanced API.
 2420  */
 2421 int
 2422 ip6_setpktoptions(control, opt, priv, needcopy)
 2423         struct mbuf *control;
 2424         struct ip6_pktopts *opt;
 2425         int priv, needcopy;
 2426 {
 2427         struct cmsghdr *cm = 0;
 2428 
 2429         if (control == 0 || opt == 0)
 2430                 return(EINVAL);
 2431 
 2432         init_ip6pktopts(opt);
 2433 
 2434         /*
 2435          * XXX: Currently, we assume all the optional information is stored
 2436          * in a single mbuf.
 2437          */
 2438         if (control->m_next)
 2439                 return(EINVAL);
 2440 
 2441         for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
 2442                      control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
 2443                 cm = mtod(control, struct cmsghdr *);
 2444                 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
 2445                         return(EINVAL);
 2446                 if (cm->cmsg_level != IPPROTO_IPV6)
 2447                         continue;
 2448 
 2449                 /*
 2450                  * XXX should check if RFC2292 API is mixed with 2292bis API
 2451                  */
 2452                 switch (cm->cmsg_type) {
 2453                 case IPV6_PKTINFO:
 2454                         if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
 2455                                 return(EINVAL);
 2456                         if (needcopy) {
 2457                                 /* XXX: Is it really WAITOK? */
 2458                                 opt->ip6po_pktinfo =
 2459                                         _MALLOC(sizeof(struct in6_pktinfo),
 2460                                                M_IP6OPT, M_WAITOK);
 2461                                 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo,
 2462                                     sizeof(struct in6_pktinfo));
 2463                         } else
 2464                                 opt->ip6po_pktinfo =
 2465                                         (struct in6_pktinfo *)CMSG_DATA(cm);
 2466                         if (opt->ip6po_pktinfo->ipi6_ifindex &&
 2467                             IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
 2468                                 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
 2469                                         htons(opt->ip6po_pktinfo->ipi6_ifindex);
 2470 
 2471                         if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
 2472                          || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
 2473                                 return(ENXIO);
 2474                         }
 2475 
 2476                         /*
 2477                          * Check if the requested source address is indeed a
 2478                          * unicast address assigned to the node, and can be
 2479                          * used as the packet's source address.
 2480                          */
 2481                         if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
 2482                                 struct in6_ifaddr *ia6;
 2483                                 struct sockaddr_in6 sin6;
 2484 
 2485                                 bzero(&sin6, sizeof(sin6));
 2486                                 sin6.sin6_len = sizeof(sin6);
 2487                                 sin6.sin6_family = AF_INET6;
 2488                                 sin6.sin6_addr =
 2489                                         opt->ip6po_pktinfo->ipi6_addr;
 2490                                 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
 2491                                 if (ia6 == NULL ||
 2492                                     (ia6->ia6_flags & (IN6_IFF_ANYCAST |
 2493                                                        IN6_IFF_NOTREADY)) != 0) {
 2494                                         if (ia6) ifafree(&ia6->ia_ifa);
 2495                                         return(EADDRNOTAVAIL);
 2496                                 }
 2497                                 ifafree(&ia6->ia_ifa);
 2498                                 ia6 = NULL;
 2499                         }
 2500                         break;
 2501 
 2502                 case IPV6_HOPLIMIT:
 2503                         if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
 2504                                 return(EINVAL);
 2505 
 2506                         opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
 2507                         if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
 2508                                 return(EINVAL);
 2509                         break;
 2510 
 2511                 case IPV6_NEXTHOP:
 2512                         if (!priv)
 2513                                 return(EPERM);
 2514 
 2515                         if (cm->cmsg_len < sizeof(u_char) ||
 2516                             /* check if cmsg_len is large enough for sa_len */
 2517                             cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
 2518                                 return(EINVAL);
 2519 
 2520                         if (needcopy) {
 2521                                 opt->ip6po_nexthop =
 2522                                         _MALLOC(*CMSG_DATA(cm),
 2523                                                M_IP6OPT, M_WAITOK);
 2524                                 bcopy(CMSG_DATA(cm),
 2525                                       opt->ip6po_nexthop,
 2526                                       *CMSG_DATA(cm));
 2527                         } else
 2528                                 opt->ip6po_nexthop =
 2529                                         (struct sockaddr *)CMSG_DATA(cm);
 2530                         break;
 2531 
 2532                 case IPV6_HOPOPTS:
 2533                 {
 2534                         struct ip6_hbh *hbh;
 2535                         int hbhlen;
 2536 
 2537                         if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
 2538                                 return(EINVAL);
 2539                         hbh = (struct ip6_hbh *)CMSG_DATA(cm);
 2540                         hbhlen = (hbh->ip6h_len + 1) << 3;
 2541                         if (cm->cmsg_len != CMSG_LEN(hbhlen))
 2542                                 return(EINVAL);
 2543 
 2544                         if (needcopy) {
 2545                                 opt->ip6po_hbh =
 2546                                         _MALLOC(hbhlen, M_IP6OPT, M_WAITOK);
 2547                                 bcopy(hbh, opt->ip6po_hbh, hbhlen);
 2548                         } else
 2549                                 opt->ip6po_hbh = hbh;
 2550                         break;
 2551                 }
 2552 
 2553                 case IPV6_DSTOPTS:
 2554                 {
 2555                         struct ip6_dest *dest, **newdest;
 2556                         int destlen;
 2557 
 2558                         if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
 2559                                 return(EINVAL);
 2560                         dest = (struct ip6_dest *)CMSG_DATA(cm);
 2561                         destlen = (dest->ip6d_len + 1) << 3;
 2562                         if (cm->cmsg_len != CMSG_LEN(destlen))
 2563                                 return(EINVAL);
 2564 
 2565                         /* 
 2566                          * The old advacned API is ambiguous on this
 2567                          * point. Our approach is to determine the
 2568                          * position based according to the existence
 2569                          * of a routing header. Note, however, that
 2570                          * this depends on the order of the extension
 2571                          * headers in the ancillary data; the 1st part
 2572                          * of the destination options header must
 2573                          * appear before the routing header in the
 2574                          * ancillary data, too.
 2575                          * RFC2292bis solved the ambiguity by
 2576                          * introducing separate cmsg types.
 2577                          */
 2578                         if (opt->ip6po_rthdr == NULL)
 2579                                 newdest = &opt->ip6po_dest1;
 2580                         else
 2581                                 newdest = &opt->ip6po_dest2;
 2582 
 2583                         if (needcopy) {
 2584                                 *newdest = _MALLOC(destlen, M_IP6OPT, M_WAITOK);
 2585                                 bcopy(dest, *newdest, destlen);
 2586                         } else
 2587                                 *newdest = dest;
 2588 
 2589                         break;
 2590                 }
 2591 
 2592                 case IPV6_RTHDR:
 2593                 {
 2594                         struct ip6_rthdr *rth;
 2595                         int rthlen;
 2596 
 2597                         if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
 2598                                 return(EINVAL);
 2599                         rth = (struct ip6_rthdr *)CMSG_DATA(cm);
 2600                         rthlen = (rth->ip6r_len + 1) << 3;
 2601                         if (cm->cmsg_len != CMSG_LEN(rthlen))
 2602                                 return(EINVAL);
 2603 
 2604                         switch (rth->ip6r_type) {
 2605                         case IPV6_RTHDR_TYPE_0:
 2606                                 /* must contain one addr */
 2607                                 if (rth->ip6r_len == 0)
 2608                                         return(EINVAL);
 2609                                 /* length must be even */
 2610                                 if (rth->ip6r_len % 2)
 2611                                         return(EINVAL);
 2612                                 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
 2613                                         return(EINVAL);
 2614                                 break;
 2615                         default:
 2616                                 return(EINVAL); /* not supported */
 2617                         }
 2618 
 2619                         if (needcopy) {
 2620                                 opt->ip6po_rthdr = _MALLOC(rthlen, M_IP6OPT,
 2621                                                           M_WAITOK);
 2622                                 bcopy(rth, opt->ip6po_rthdr, rthlen);
 2623                         } else
 2624                                 opt->ip6po_rthdr = rth;
 2625 
 2626                         break;
 2627                 }
 2628 
 2629                 default:
 2630                         return(ENOPROTOOPT);
 2631                 }
 2632         }
 2633 
 2634         return(0);
 2635 }
 2636 
 2637 /*
 2638  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
 2639  * packet to the input queue of a specified interface.  Note that this
 2640  * calls the output routine of the loopback "driver", but with an interface
 2641  * pointer that might NOT be &loif -- easier than replicating that code here.
 2642  */
 2643 void
 2644 ip6_mloopback(
 2645         struct ifnet *ifp,
 2646         struct mbuf *m,
 2647         struct sockaddr_in6 *dst)
 2648 {
 2649         struct mbuf *copym;
 2650         struct ip6_hdr *ip6;
 2651 
 2652         copym = m_copy(m, 0, M_COPYALL);
 2653         if (copym == NULL)
 2654                 return;
 2655 
 2656         /*
 2657          * Make sure to deep-copy IPv6 header portion in case the data
 2658          * is in an mbuf cluster, so that we can safely override the IPv6
 2659          * header portion later.
 2660          */
 2661         if ((copym->m_flags & M_EXT) != 0 ||
 2662             copym->m_len < sizeof(struct ip6_hdr)) {
 2663                 copym = m_pullup(copym, sizeof(struct ip6_hdr));
 2664                 if (copym == NULL)
 2665                         return;
 2666         }
 2667 
 2668 #if DIAGNOSTIC
 2669         if (copym->m_len < sizeof(*ip6)) {
 2670                 m_freem(copym);
 2671                 return;
 2672         }
 2673 #endif
 2674 
 2675         ip6 = mtod(copym, struct ip6_hdr *);
 2676 #ifndef SCOPEDROUTING
 2677         /*
 2678          * clear embedded scope identifiers if necessary.
 2679          * in6_clearscope will touch the addresses only when necessary.
 2680          */
 2681         in6_clearscope(&ip6->ip6_src);
 2682         in6_clearscope(&ip6->ip6_dst);
 2683 #endif
 2684 
 2685 #ifdef __APPLE__
 2686 
 2687         /* Makes sure the HW checksum flags are cleaned before sending the packet */
 2688 
 2689         copym->m_pkthdr.rcvif = 0;
 2690         copym->m_pkthdr.csum_data = 0;
 2691         copym->m_pkthdr.csum_flags = 0;
 2692 
 2693         if (lo_ifp) {
 2694                 copym->m_pkthdr.rcvif = ifp;
 2695                 lck_mtx_unlock(ip6_mutex);
 2696                 dlil_output(lo_ifp, PF_INET6, copym, 0, (struct sockaddr *)dst, 0);
 2697                 lck_mtx_lock(ip6_mutex);
 2698         } else
 2699                 m_free(copym);
 2700 #else
 2701         (void)if_simloop(ifp, copym, dst->sin6_family, NULL);
 2702 #endif
 2703 }
 2704 
 2705 /*
 2706  * Chop IPv6 header off from the payload.
 2707  */
 2708 static int
 2709 ip6_splithdr(m, exthdrs)
 2710         struct mbuf *m;
 2711         struct ip6_exthdrs *exthdrs;
 2712 {
 2713         struct mbuf *mh;
 2714         struct ip6_hdr *ip6;
 2715 
 2716         ip6 = mtod(m, struct ip6_hdr *);
 2717         if (m->m_len > sizeof(*ip6)) {
 2718                 MGETHDR(mh, M_DONTWAIT, MT_HEADER);     /* MAC-OK */
 2719                 if (mh == 0) {
 2720                         m_freem(m);
 2721                         return ENOBUFS;
 2722                 }
 2723                 M_COPY_PKTHDR(mh, m);
 2724                 MH_ALIGN(mh, sizeof(*ip6));
 2725                 m->m_flags &= ~M_PKTHDR;
 2726                 m->m_len -= sizeof(*ip6);
 2727                 m->m_data += sizeof(*ip6);
 2728                 mh->m_next = m;
 2729                 m = mh;
 2730                 m->m_len = sizeof(*ip6);
 2731                 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
 2732         }
 2733         exthdrs->ip6e_ip6 = m;
 2734         return 0;
 2735 }
 2736 
 2737 /*
 2738  * Compute IPv6 extension header length.
 2739  */
 2740 int
 2741 ip6_optlen(in6p)
 2742         struct in6pcb *in6p;
 2743 {
 2744         int len;
 2745 
 2746         if (!in6p->in6p_outputopts)
 2747                 return 0;
 2748 
 2749         len = 0;
 2750 #define elen(x) \
 2751     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
 2752 
 2753         len += elen(in6p->in6p_outputopts->ip6po_hbh);
 2754         if (in6p->in6p_outputopts->ip6po_rthdr)
 2755                 /* dest1 is valid with rthdr only */
 2756                 len += elen(in6p->in6p_outputopts->ip6po_dest1);
 2757         len += elen(in6p->in6p_outputopts->ip6po_rthdr);
 2758         len += elen(in6p->in6p_outputopts->ip6po_dest2);
 2759         return len;
 2760 #undef elen
 2761 }
 2762 

Cache object: e0f501f0cb726d956bbdaef8fd906828


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