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/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 /*-
    2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
   30  */
   31 
   32 /*-
   33  * Copyright (c) 1982, 1986, 1988, 1990, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   35  *
   36  * Redistribution and use in source and binary forms, with or without
   37  * modification, are permitted provided that the following conditions
   38  * are met:
   39  * 1. Redistributions of source code must retain the above copyright
   40  *    notice, this list of conditions and the following disclaimer.
   41  * 2. Redistributions in binary form must reproduce the above copyright
   42  *    notice, this list of conditions and the following disclaimer in the
   43  *    documentation and/or other materials provided with the distribution.
   44  * 4. Neither the name of the University nor the names of its contributors
   45  *    may be used to endorse or promote products derived from this software
   46  *    without specific prior written permission.
   47  *
   48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   58  * SUCH DAMAGE.
   59  *
   60  *      @(#)ip_output.c 8.3 (Berkeley) 1/21/94
   61  */
   62 
   63 #include <sys/cdefs.h>
   64 __FBSDID("$FreeBSD: releng/9.0/sys/netinet6/ip6_output.c 227428 2011-11-10 20:28:30Z trociny $");
   65 
   66 #include "opt_inet.h"
   67 #include "opt_inet6.h"
   68 #include "opt_ipfw.h"
   69 #include "opt_ipsec.h"
   70 #include "opt_sctp.h"
   71 #include "opt_route.h"
   72 
   73 #include <sys/param.h>
   74 #include <sys/kernel.h>
   75 #include <sys/malloc.h>
   76 #include <sys/mbuf.h>
   77 #include <sys/errno.h>
   78 #include <sys/priv.h>
   79 #include <sys/proc.h>
   80 #include <sys/protosw.h>
   81 #include <sys/socket.h>
   82 #include <sys/socketvar.h>
   83 #include <sys/syslog.h>
   84 #include <sys/ucred.h>
   85 
   86 #include <net/if.h>
   87 #include <net/netisr.h>
   88 #include <net/route.h>
   89 #include <net/pfil.h>
   90 #include <net/vnet.h>
   91 
   92 #include <netinet/in.h>
   93 #include <netinet/in_var.h>
   94 #include <netinet/ip_var.h>
   95 #include <netinet6/in6_var.h>
   96 #include <netinet/ip6.h>
   97 #include <netinet/icmp6.h>
   98 #include <netinet6/ip6_var.h>
   99 #include <netinet/in_pcb.h>
  100 #include <netinet/tcp_var.h>
  101 #include <netinet6/nd6.h>
  102 
  103 #ifdef IPSEC
  104 #include <netipsec/ipsec.h>
  105 #include <netipsec/ipsec6.h>
  106 #include <netipsec/key.h>
  107 #include <netinet6/ip6_ipsec.h>
  108 #endif /* IPSEC */
  109 #ifdef SCTP
  110 #include <netinet/sctp.h>
  111 #include <netinet/sctp_crc32.h>
  112 #endif
  113 
  114 #include <netinet6/ip6protosw.h>
  115 #include <netinet6/scope6_var.h>
  116 
  117 #ifdef FLOWTABLE
  118 #include <net/flowtable.h>
  119 #endif
  120 
  121 extern int in6_mcast_loop;
  122 
  123 struct ip6_exthdrs {
  124         struct mbuf *ip6e_ip6;
  125         struct mbuf *ip6e_hbh;
  126         struct mbuf *ip6e_dest1;
  127         struct mbuf *ip6e_rthdr;
  128         struct mbuf *ip6e_dest2;
  129 };
  130 
  131 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
  132                            struct ucred *, int));
  133 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
  134         struct socket *, struct sockopt *));
  135 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
  136 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *,
  137         struct ucred *, int, int, int));
  138 
  139 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
  140 static int ip6_insertfraghdr __P((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 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
  145         struct ifnet *, struct in6_addr *, u_long *, int *));
  146 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
  147 
  148 
  149 /*
  150  * Make an extension header from option data.  hp is the source, and
  151  * mp is the destination.
  152  */
  153 #define MAKE_EXTHDR(hp, mp)                                             \
  154     do {                                                                \
  155         if (hp) {                                                       \
  156                 struct ip6_ext *eh = (struct ip6_ext *)(hp);            \
  157                 error = ip6_copyexthdr((mp), (caddr_t)(hp),             \
  158                     ((eh)->ip6e_len + 1) << 3);                         \
  159                 if (error)                                              \
  160                         goto freehdrs;                                  \
  161         }                                                               \
  162     } while (/*CONSTCOND*/ 0)
  163 
  164 /*
  165  * Form a chain of extension headers.
  166  * m is the extension header mbuf
  167  * mp is the previous mbuf in the chain
  168  * p is the next header
  169  * i is the type of option.
  170  */
  171 #define MAKE_CHAIN(m, mp, p, i)\
  172     do {\
  173         if (m) {\
  174                 if (!hdrsplit) \
  175                         panic("assumption failed: hdr not split"); \
  176                 *mtod((m), u_char *) = *(p);\
  177                 *(p) = (i);\
  178                 p = mtod((m), u_char *);\
  179                 (m)->m_next = (mp)->m_next;\
  180                 (mp)->m_next = (m);\
  181                 (mp) = (m);\
  182         }\
  183     } while (/*CONSTCOND*/ 0)
  184 
  185 /*
  186  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
  187  * header (with pri, len, nxt, hlim, src, dst).
  188  * This function may modify ver and hlim only.
  189  * The mbuf chain containing the packet will be freed.
  190  * The mbuf opt, if present, will not be freed.
  191  *
  192  * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
  193  * nd_ifinfo.linkmtu is u_int32_t.  so we use u_long to hold largest one,
  194  * which is rt_rmx.rmx_mtu.
  195  *
  196  * ifpp - XXX: just for statistics
  197  */
  198 int
  199 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
  200     struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
  201     struct ifnet **ifpp, struct inpcb *inp)
  202 {
  203         struct ip6_hdr *ip6, *mhip6;
  204         struct ifnet *ifp, *origifp;
  205         struct mbuf *m = m0;
  206         struct mbuf *mprev = NULL;
  207         int hlen, tlen, len, off;
  208         struct route_in6 ip6route;
  209         struct rtentry *rt = NULL;
  210         struct sockaddr_in6 *dst, src_sa, dst_sa;
  211         struct in6_addr odst;
  212         int error = 0;
  213         struct in6_ifaddr *ia = NULL;
  214         u_long mtu;
  215         int alwaysfrag, dontfrag;
  216         u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
  217         struct ip6_exthdrs exthdrs;
  218         struct in6_addr finaldst, src0, dst0;
  219         u_int32_t zone;
  220         struct route_in6 *ro_pmtu = NULL;
  221         int flevalid = 0;
  222         int hdrsplit = 0;
  223         int needipsec = 0;
  224 #ifdef SCTP
  225         int sw_csum;
  226 #endif
  227 #ifdef IPSEC
  228         struct ipsec_output_state state;
  229         struct ip6_rthdr *rh = NULL;
  230         int needipsectun = 0;
  231         int segleft_org = 0;
  232         struct secpolicy *sp = NULL;
  233 #endif /* IPSEC */
  234 #ifdef IPFIREWALL_FORWARD
  235         struct m_tag *fwd_tag;
  236 #endif
  237 
  238         ip6 = mtod(m, struct ip6_hdr *);
  239         if (ip6 == NULL) {
  240                 printf ("ip6 is NULL");
  241                 goto bad;
  242         }
  243 
  244         finaldst = ip6->ip6_dst;
  245         bzero(&exthdrs, sizeof(exthdrs));
  246         if (opt) {
  247                 /* Hop-by-Hop options header */
  248                 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
  249                 /* Destination options header(1st part) */
  250                 if (opt->ip6po_rthdr) {
  251                         /*
  252                          * Destination options header(1st part)
  253                          * This only makes sense with a routing header.
  254                          * See Section 9.2 of RFC 3542.
  255                          * Disabling this part just for MIP6 convenience is
  256                          * a bad idea.  We need to think carefully about a
  257                          * way to make the advanced API coexist with MIP6
  258                          * options, which might automatically be inserted in
  259                          * the kernel.
  260                          */
  261                         MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
  262                 }
  263                 /* Routing header */
  264                 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
  265                 /* Destination options header(2nd part) */
  266                 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
  267         }
  268 
  269         /*
  270          * IPSec checking which handles several cases.
  271          * FAST IPSEC: We re-injected the packet.
  272          */
  273 #ifdef IPSEC
  274         switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp))
  275         {
  276         case 1:                 /* Bad packet */
  277                 goto freehdrs;
  278         case -1:                /* Do IPSec */
  279                 needipsec = 1;
  280         case 0:                 /* No IPSec */
  281         default:
  282                 break;
  283         }
  284 #endif /* IPSEC */
  285 
  286         /*
  287          * Calculate the total length of the extension header chain.
  288          * Keep the length of the unfragmentable part for fragmentation.
  289          */
  290         optlen = 0;
  291         if (exthdrs.ip6e_hbh)
  292                 optlen += exthdrs.ip6e_hbh->m_len;
  293         if (exthdrs.ip6e_dest1)
  294                 optlen += exthdrs.ip6e_dest1->m_len;
  295         if (exthdrs.ip6e_rthdr)
  296                 optlen += exthdrs.ip6e_rthdr->m_len;
  297         unfragpartlen = optlen + sizeof(struct ip6_hdr);
  298 
  299         /* NOTE: we don't add AH/ESP length here. do that later. */
  300         if (exthdrs.ip6e_dest2)
  301                 optlen += exthdrs.ip6e_dest2->m_len;
  302 
  303         /*
  304          * If we need IPsec, or there is at least one extension header,
  305          * separate IP6 header from the payload.
  306          */
  307         if ((needipsec || optlen) && !hdrsplit) {
  308                 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
  309                         m = NULL;
  310                         goto freehdrs;
  311                 }
  312                 m = exthdrs.ip6e_ip6;
  313                 hdrsplit++;
  314         }
  315 
  316         /* adjust pointer */
  317         ip6 = mtod(m, struct ip6_hdr *);
  318 
  319         /* adjust mbuf packet header length */
  320         m->m_pkthdr.len += optlen;
  321         plen = m->m_pkthdr.len - sizeof(*ip6);
  322 
  323         /* If this is a jumbo payload, insert a jumbo payload option. */
  324         if (plen > IPV6_MAXPACKET) {
  325                 if (!hdrsplit) {
  326                         if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
  327                                 m = NULL;
  328                                 goto freehdrs;
  329                         }
  330                         m = exthdrs.ip6e_ip6;
  331                         hdrsplit++;
  332                 }
  333                 /* adjust pointer */
  334                 ip6 = mtod(m, struct ip6_hdr *);
  335                 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
  336                         goto freehdrs;
  337                 ip6->ip6_plen = 0;
  338         } else
  339                 ip6->ip6_plen = htons(plen);
  340 
  341         /*
  342          * Concatenate headers and fill in next header fields.
  343          * Here we have, on "m"
  344          *      IPv6 payload
  345          * and we insert headers accordingly.  Finally, we should be getting:
  346          *      IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
  347          *
  348          * during the header composing process, "m" points to IPv6 header.
  349          * "mprev" points to an extension header prior to esp.
  350          */
  351         u_char *nexthdrp = &ip6->ip6_nxt;
  352         mprev = m;
  353 
  354         /*
  355          * we treat dest2 specially.  this makes IPsec processing
  356          * much easier.  the goal here is to make mprev point the
  357          * mbuf prior to dest2.
  358          *
  359          * result: IPv6 dest2 payload
  360          * m and mprev will point to IPv6 header.
  361          */
  362         if (exthdrs.ip6e_dest2) {
  363                 if (!hdrsplit)
  364                         panic("assumption failed: hdr not split");
  365                 exthdrs.ip6e_dest2->m_next = m->m_next;
  366                 m->m_next = exthdrs.ip6e_dest2;
  367                 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
  368                 ip6->ip6_nxt = IPPROTO_DSTOPTS;
  369         }
  370 
  371         /*
  372          * result: IPv6 hbh dest1 rthdr dest2 payload
  373          * m will point to IPv6 header.  mprev will point to the
  374          * extension header prior to dest2 (rthdr in the above case).
  375          */
  376         MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
  377         MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
  378                    IPPROTO_DSTOPTS);
  379         MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
  380                    IPPROTO_ROUTING);
  381 
  382 #ifdef IPSEC
  383         if (!needipsec)
  384                 goto skip_ipsec2;
  385 
  386         /*
  387          * pointers after IPsec headers are not valid any more.
  388          * other pointers need a great care too.
  389          * (IPsec routines should not mangle mbufs prior to AH/ESP)
  390          */
  391         exthdrs.ip6e_dest2 = NULL;
  392 
  393         if (exthdrs.ip6e_rthdr) {
  394                 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
  395                 segleft_org = rh->ip6r_segleft;
  396                 rh->ip6r_segleft = 0;
  397         }
  398 
  399         bzero(&state, sizeof(state));
  400         state.m = m;
  401         error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
  402                                     &needipsectun);
  403         m = state.m;
  404         if (error == EJUSTRETURN) {
  405                 /*
  406                  * We had a SP with a level of 'use' and no SA. We
  407                  * will just continue to process the packet without
  408                  * IPsec processing.
  409                  */
  410                 ;
  411         } else if (error) {
  412                 /* mbuf is already reclaimed in ipsec6_output_trans. */
  413                 m = NULL;
  414                 switch (error) {
  415                 case EHOSTUNREACH:
  416                 case ENETUNREACH:
  417                 case EMSGSIZE:
  418                 case ENOBUFS:
  419                 case ENOMEM:
  420                         break;
  421                 default:
  422                         printf("[%s:%d] (ipsec): error code %d\n",
  423                             __func__, __LINE__, error);
  424                         /* FALLTHROUGH */
  425                 case ENOENT:
  426                         /* don't show these error codes to the user */
  427                         error = 0;
  428                         break;
  429                 }
  430                 goto bad;
  431         } else if (!needipsectun) {
  432                 /*
  433                  * In the FAST IPSec case we have already
  434                  * re-injected the packet and it has been freed
  435                  * by the ipsec_done() function.  So, just clean
  436                  * up after ourselves.
  437                  */
  438                 m = NULL;
  439                 goto done;
  440         }
  441         if (exthdrs.ip6e_rthdr) {
  442                 /* ah6_output doesn't modify mbuf chain */
  443                 rh->ip6r_segleft = segleft_org;
  444         }
  445 skip_ipsec2:;
  446 #endif /* IPSEC */
  447 
  448         /*
  449          * If there is a routing header, discard the packet.
  450          */
  451         if (exthdrs.ip6e_rthdr) {
  452                  error = EINVAL;
  453                  goto bad;
  454         }
  455 
  456         /* Source address validation */
  457         if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
  458             (flags & IPV6_UNSPECSRC) == 0) {
  459                 error = EOPNOTSUPP;
  460                 V_ip6stat.ip6s_badscope++;
  461                 goto bad;
  462         }
  463         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
  464                 error = EOPNOTSUPP;
  465                 V_ip6stat.ip6s_badscope++;
  466                 goto bad;
  467         }
  468 
  469         V_ip6stat.ip6s_localout++;
  470 
  471         /*
  472          * Route packet.
  473          */
  474         if (ro == 0) {
  475                 ro = &ip6route;
  476                 bzero((caddr_t)ro, sizeof(*ro));
  477         }
  478         ro_pmtu = ro;
  479         if (opt && opt->ip6po_rthdr)
  480                 ro = &opt->ip6po_route;
  481         dst = (struct sockaddr_in6 *)&ro->ro_dst;
  482 #ifdef FLOWTABLE
  483         if (ro == &ip6route) {
  484                 struct flentry *fle;
  485                 
  486                 /*
  487                  * The flow table returns route entries valid for up to 30
  488                  * seconds; we rely on the remainder of ip_output() taking no
  489                  * longer than that long for the stability of ro_rt.  The
  490                  * flow ID assignment must have happened before this point.
  491                  */
  492                 if ((fle = flowtable_lookup_mbuf(V_ip6_ft, m, AF_INET6)) != NULL) {
  493                         flow_to_route_in6(fle, ro);
  494                         if (ro->ro_rt != NULL && ro->ro_lle != NULL)
  495                                 flevalid = 1;
  496                 }
  497         }
  498 #endif  
  499 again:
  500         /*
  501          * if specified, try to fill in the traffic class field.
  502          * do not override if a non-zero value is already set.
  503          * we check the diffserv field and the ecn field separately.
  504          */
  505         if (opt && opt->ip6po_tclass >= 0) {
  506                 int mask = 0;
  507 
  508                 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
  509                         mask |= 0xfc;
  510                 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
  511                         mask |= 0x03;
  512                 if (mask != 0)
  513                         ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
  514         }
  515 
  516         /* fill in or override the hop limit field, if necessary. */
  517         if (opt && opt->ip6po_hlim != -1)
  518                 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
  519         else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  520                 if (im6o != NULL)
  521                         ip6->ip6_hlim = im6o->im6o_multicast_hlim;
  522                 else
  523                         ip6->ip6_hlim = V_ip6_defmcasthlim;
  524         }
  525 
  526 #ifdef IPSEC
  527         /*
  528          * We may re-inject packets into the stack here.
  529          */
  530         if (needipsec && needipsectun) {
  531                 struct ipsec_output_state state;
  532 
  533                 /*
  534                  * All the extension headers will become inaccessible
  535                  * (since they can be encrypted).
  536                  * Don't panic, we need no more updates to extension headers
  537                  * on inner IPv6 packet (since they are now encapsulated).
  538                  *
  539                  * IPv6 [ESP|AH] IPv6 [extension headers] payload
  540                  */
  541                 bzero(&exthdrs, sizeof(exthdrs));
  542                 exthdrs.ip6e_ip6 = m;
  543 
  544                 bzero(&state, sizeof(state));
  545                 state.m = m;
  546                 state.ro = (struct route *)ro;
  547                 state.dst = (struct sockaddr *)dst;
  548 
  549                 error = ipsec6_output_tunnel(&state, sp, flags);
  550 
  551                 m = state.m;
  552                 ro = (struct route_in6 *)state.ro;
  553                 dst = (struct sockaddr_in6 *)state.dst;
  554                 if (error == EJUSTRETURN) {
  555                         /*
  556                          * We had a SP with a level of 'use' and no SA. We
  557                          * will just continue to process the packet without
  558                          * IPsec processing.
  559                          */
  560                         ;
  561                 } else if (error) {
  562                         /* mbuf is already reclaimed in ipsec6_output_tunnel. */
  563                         m0 = m = NULL;
  564                         m = NULL;
  565                         switch (error) {
  566                         case EHOSTUNREACH:
  567                         case ENETUNREACH:
  568                         case EMSGSIZE:
  569                         case ENOBUFS:
  570                         case ENOMEM:
  571                                 break;
  572                         default:
  573                                 printf("[%s:%d] (ipsec): error code %d\n",
  574                                     __func__, __LINE__, error);
  575                                 /* FALLTHROUGH */
  576                         case ENOENT:
  577                                 /* don't show these error codes to the user */
  578                                 error = 0;
  579                                 break;
  580                         }
  581                         goto bad;
  582                 } else {
  583                         /*
  584                          * In the FAST IPSec case we have already
  585                          * re-injected the packet and it has been freed
  586                          * by the ipsec_done() function.  So, just clean
  587                          * up after ourselves.
  588                          */
  589                         m = NULL;
  590                         goto done;
  591                 }
  592 
  593                 exthdrs.ip6e_ip6 = m;
  594         }
  595 #endif /* IPSEC */
  596 
  597         /* adjust pointer */
  598         ip6 = mtod(m, struct ip6_hdr *);
  599 
  600         bzero(&dst_sa, sizeof(dst_sa));
  601         dst_sa.sin6_family = AF_INET6;
  602         dst_sa.sin6_len = sizeof(dst_sa);
  603         dst_sa.sin6_addr = ip6->ip6_dst;
  604         if (flevalid) {
  605                 rt = ro->ro_rt;
  606                 ifp = ro->ro_rt->rt_ifp;
  607         } else if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
  608             &ifp, &rt)) != 0) {
  609                 switch (error) {
  610                 case EHOSTUNREACH:
  611                         V_ip6stat.ip6s_noroute++;
  612                         break;
  613                 case EADDRNOTAVAIL:
  614                 default:
  615                         break; /* XXX statistics? */
  616                 }
  617                 if (ifp != NULL)
  618                         in6_ifstat_inc(ifp, ifs6_out_discard);
  619                 goto bad;
  620         }
  621         if (rt == NULL) {
  622                 /*
  623                  * If in6_selectroute() does not return a route entry,
  624                  * dst may not have been updated.
  625                  */
  626                 *dst = dst_sa;  /* XXX */
  627         }
  628 
  629         /*
  630          * then rt (for unicast) and ifp must be non-NULL valid values.
  631          */
  632         if ((flags & IPV6_FORWARDING) == 0) {
  633                 /* XXX: the FORWARDING flag can be set for mrouting. */
  634                 in6_ifstat_inc(ifp, ifs6_out_request);
  635         }
  636         if (rt != NULL) {
  637                 ia = (struct in6_ifaddr *)(rt->rt_ifa);
  638                 rt->rt_use++;
  639         }
  640 
  641 
  642         /*
  643          * The outgoing interface must be in the zone of source and
  644          * destination addresses.  
  645          */
  646         origifp = ifp;
  647 
  648         src0 = ip6->ip6_src;
  649         if (in6_setscope(&src0, origifp, &zone))
  650                 goto badscope;
  651         bzero(&src_sa, sizeof(src_sa));
  652         src_sa.sin6_family = AF_INET6;
  653         src_sa.sin6_len = sizeof(src_sa);
  654         src_sa.sin6_addr = ip6->ip6_src;
  655         if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
  656                 goto badscope;
  657 
  658         dst0 = ip6->ip6_dst;
  659         if (in6_setscope(&dst0, origifp, &zone))
  660                 goto badscope;
  661         /* re-initialize to be sure */
  662         bzero(&dst_sa, sizeof(dst_sa));
  663         dst_sa.sin6_family = AF_INET6;
  664         dst_sa.sin6_len = sizeof(dst_sa);
  665         dst_sa.sin6_addr = ip6->ip6_dst;
  666         if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
  667                 goto badscope;
  668         }
  669 
  670         /* We should use ia_ifp to support the case of 
  671          * sending packets to an address of our own.
  672          */
  673         if (ia != NULL && ia->ia_ifp)
  674                 ifp = ia->ia_ifp;
  675 
  676         /* scope check is done. */
  677         goto routefound;
  678 
  679   badscope:
  680         V_ip6stat.ip6s_badscope++;
  681         in6_ifstat_inc(origifp, ifs6_out_discard);
  682         if (error == 0)
  683                 error = EHOSTUNREACH; /* XXX */
  684         goto bad;
  685 
  686   routefound:
  687         if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  688                 if (opt && opt->ip6po_nextroute.ro_rt) {
  689                         /*
  690                          * The nexthop is explicitly specified by the
  691                          * application.  We assume the next hop is an IPv6
  692                          * address.
  693                          */
  694                         dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
  695                 }
  696                 else if ((rt->rt_flags & RTF_GATEWAY))
  697                         dst = (struct sockaddr_in6 *)rt->rt_gateway;
  698         }
  699 
  700         if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  701                 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
  702         } else {
  703                 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
  704                 in6_ifstat_inc(ifp, ifs6_out_mcast);
  705                 /*
  706                  * Confirm that the outgoing interface supports multicast.
  707                  */
  708                 if (!(ifp->if_flags & IFF_MULTICAST)) {
  709                         V_ip6stat.ip6s_noroute++;
  710                         in6_ifstat_inc(ifp, ifs6_out_discard);
  711                         error = ENETUNREACH;
  712                         goto bad;
  713                 }
  714                 if ((im6o == NULL && in6_mcast_loop) ||
  715                     (im6o && im6o->im6o_multicast_loop)) {
  716                         /*
  717                          * Loop back multicast datagram if not expressly
  718                          * forbidden to do so, even if we have not joined
  719                          * the address; protocols will filter it later,
  720                          * thus deferring a hash lookup and lock acquisition
  721                          * at the expense of an m_copym().
  722                          */
  723                         ip6_mloopback(ifp, m, dst);
  724                 } else {
  725                         /*
  726                          * If we are acting as a multicast router, perform
  727                          * multicast forwarding as if the packet had just
  728                          * arrived on the interface to which we are about
  729                          * to send.  The multicast forwarding function
  730                          * recursively calls this function, using the
  731                          * IPV6_FORWARDING flag to prevent infinite recursion.
  732                          *
  733                          * Multicasts that are looped back by ip6_mloopback(),
  734                          * above, will be forwarded by the ip6_input() routine,
  735                          * if necessary.
  736                          */
  737                         if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
  738                                 /*
  739                                  * XXX: ip6_mforward expects that rcvif is NULL
  740                                  * when it is called from the originating path.
  741                                  * However, it is not always the case, since
  742                                  * some versions of MGETHDR() does not
  743                                  * initialize the field.
  744                                  */
  745                                 m->m_pkthdr.rcvif = NULL;
  746                                 if (ip6_mforward(ip6, ifp, m) != 0) {
  747                                         m_freem(m);
  748                                         goto done;
  749                                 }
  750                         }
  751                 }
  752                 /*
  753                  * Multicasts with a hoplimit of zero may be looped back,
  754                  * above, but must not be transmitted on a network.
  755                  * Also, multicasts addressed to the loopback interface
  756                  * are not sent -- the above call to ip6_mloopback() will
  757                  * loop back a copy if this host actually belongs to the
  758                  * destination group on the loopback interface.
  759                  */
  760                 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
  761                     IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
  762                         m_freem(m);
  763                         goto done;
  764                 }
  765         }
  766 
  767         /*
  768          * Fill the outgoing inteface to tell the upper layer
  769          * to increment per-interface statistics.
  770          */
  771         if (ifpp)
  772                 *ifpp = ifp;
  773 
  774         /* Determine path MTU. */
  775         if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
  776             &alwaysfrag)) != 0)
  777                 goto bad;
  778 
  779         /*
  780          * The caller of this function may specify to use the minimum MTU
  781          * in some cases.
  782          * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
  783          * setting.  The logic is a bit complicated; by default, unicast
  784          * packets will follow path MTU while multicast packets will be sent at
  785          * the minimum MTU.  If IP6PO_MINMTU_ALL is specified, all packets
  786          * including unicast ones will be sent at the minimum MTU.  Multicast
  787          * packets will always be sent at the minimum MTU unless
  788          * IP6PO_MINMTU_DISABLE is explicitly specified.
  789          * See RFC 3542 for more details.
  790          */
  791         if (mtu > IPV6_MMTU) {
  792                 if ((flags & IPV6_MINMTU))
  793                         mtu = IPV6_MMTU;
  794                 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
  795                         mtu = IPV6_MMTU;
  796                 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
  797                          (opt == NULL ||
  798                           opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
  799                         mtu = IPV6_MMTU;
  800                 }
  801         }
  802 
  803         /*
  804          * clear embedded scope identifiers if necessary.
  805          * in6_clearscope will touch the addresses only when necessary.
  806          */
  807         in6_clearscope(&ip6->ip6_src);
  808         in6_clearscope(&ip6->ip6_dst);
  809 
  810         /*
  811          * If the outgoing packet contains a hop-by-hop options header,
  812          * it must be examined and processed even by the source node.
  813          * (RFC 2460, section 4.)
  814          */
  815         if (exthdrs.ip6e_hbh) {
  816                 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
  817                 u_int32_t dummy; /* XXX unused */
  818                 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
  819 
  820 #ifdef DIAGNOSTIC
  821                 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
  822                         panic("ip6e_hbh is not contiguous");
  823 #endif
  824                 /*
  825                  *  XXX: if we have to send an ICMPv6 error to the sender,
  826                  *       we need the M_LOOP flag since icmp6_error() expects
  827                  *       the IPv6 and the hop-by-hop options header are
  828                  *       contiguous unless the flag is set.
  829                  */
  830                 m->m_flags |= M_LOOP;
  831                 m->m_pkthdr.rcvif = ifp;
  832                 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
  833                     ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
  834                     &dummy, &plen) < 0) {
  835                         /* m was already freed at this point */
  836                         error = EINVAL;/* better error? */
  837                         goto done;
  838                 }
  839                 m->m_flags &= ~M_LOOP; /* XXX */
  840                 m->m_pkthdr.rcvif = NULL;
  841         }
  842 
  843         /* Jump over all PFIL processing if hooks are not active. */
  844         if (!PFIL_HOOKED(&V_inet6_pfil_hook))
  845                 goto passout;
  846 
  847         odst = ip6->ip6_dst;
  848         /* Run through list of hooks for output packets. */
  849         error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
  850         if (error != 0 || m == NULL)
  851                 goto done;
  852         ip6 = mtod(m, struct ip6_hdr *);
  853 
  854         /* See if destination IP address was changed by packet filter. */
  855         if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
  856                 m->m_flags |= M_SKIP_FIREWALL;
  857                 /* If destination is now ourself drop to ip6_input(). */
  858                 if (in6_localip(&ip6->ip6_dst)) {
  859                         m->m_flags |= M_FASTFWD_OURS;
  860                         if (m->m_pkthdr.rcvif == NULL)
  861                                 m->m_pkthdr.rcvif = V_loif;
  862                         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  863                                 m->m_pkthdr.csum_flags |=
  864                                     CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
  865                                 m->m_pkthdr.csum_data = 0xffff;
  866                         }
  867                         m->m_pkthdr.csum_flags |=
  868                             CSUM_IP_CHECKED | CSUM_IP_VALID;
  869 #ifdef SCTP
  870                         if (m->m_pkthdr.csum_flags & CSUM_SCTP)
  871                                 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
  872 #endif
  873                         error = netisr_queue(NETISR_IPV6, m);
  874                         goto done;
  875                 } else
  876                         goto again;     /* Redo the routing table lookup. */
  877         }
  878 
  879 #ifdef IPFIREWALL_FORWARD
  880         /* See if local, if yes, send it to netisr. */
  881         if (m->m_flags & M_FASTFWD_OURS) {
  882                 if (m->m_pkthdr.rcvif == NULL)
  883                         m->m_pkthdr.rcvif = V_loif;
  884                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  885                         m->m_pkthdr.csum_flags |=
  886                             CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
  887                         m->m_pkthdr.csum_data = 0xffff;
  888                 }
  889 #ifdef SCTP
  890                 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
  891                 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
  892 #endif   
  893                 error = netisr_queue(NETISR_IPV6, m);
  894                 goto done;
  895         }
  896         /* Or forward to some other address? */
  897         fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
  898         if (fwd_tag) {
  899                 dst = (struct sockaddr_in6 *)&ro->ro_dst;
  900                 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6));
  901                 m->m_flags |= M_SKIP_FIREWALL;
  902                 m_tag_delete(m, fwd_tag);
  903                 goto again;
  904         }
  905 #endif /* IPFIREWALL_FORWARD */
  906 
  907 passout:
  908         /*
  909          * Send the packet to the outgoing interface.
  910          * If necessary, do IPv6 fragmentation before sending.
  911          *
  912          * the logic here is rather complex:
  913          * 1: normal case (dontfrag == 0, alwaysfrag == 0)
  914          * 1-a: send as is if tlen <= path mtu
  915          * 1-b: fragment if tlen > path mtu
  916          *
  917          * 2: if user asks us not to fragment (dontfrag == 1)
  918          * 2-a: send as is if tlen <= interface mtu
  919          * 2-b: error if tlen > interface mtu
  920          *
  921          * 3: if we always need to attach fragment header (alwaysfrag == 1)
  922          *      always fragment
  923          *
  924          * 4: if dontfrag == 1 && alwaysfrag == 1
  925          *      error, as we cannot handle this conflicting request
  926          */
  927 #ifdef SCTP
  928         sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
  929         if (sw_csum & CSUM_SCTP) {
  930                 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
  931                 sw_csum &= ~CSUM_SCTP;
  932         }
  933 #endif
  934         tlen = m->m_pkthdr.len;
  935 
  936         if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
  937                 dontfrag = 1;
  938         else
  939                 dontfrag = 0;
  940         if (dontfrag && alwaysfrag) {   /* case 4 */
  941                 /* conflicting request - can't transmit */
  942                 error = EMSGSIZE;
  943                 goto bad;
  944         }
  945         if (dontfrag && tlen > IN6_LINKMTU(ifp)) {      /* case 2-b */
  946                 /*
  947                  * Even if the DONTFRAG option is specified, we cannot send the
  948                  * packet when the data length is larger than the MTU of the
  949                  * outgoing interface.
  950                  * Notify the error by sending IPV6_PATHMTU ancillary data as
  951                  * well as returning an error code (the latter is not described
  952                  * in the API spec.)
  953                  */
  954                 u_int32_t mtu32;
  955                 struct ip6ctlparam ip6cp;
  956 
  957                 mtu32 = (u_int32_t)mtu;
  958                 bzero(&ip6cp, sizeof(ip6cp));
  959                 ip6cp.ip6c_cmdarg = (void *)&mtu32;
  960                 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
  961                     (void *)&ip6cp);
  962 
  963                 error = EMSGSIZE;
  964                 goto bad;
  965         }
  966 
  967         /*
  968          * transmit packet without fragmentation
  969          */
  970         if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
  971                 struct in6_ifaddr *ia6;
  972 
  973                 ip6 = mtod(m, struct ip6_hdr *);
  974                 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
  975                 if (ia6) {
  976                         /* Record statistics for this interface address. */
  977                         ia6->ia_ifa.if_opackets++;
  978                         ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
  979                         ifa_free(&ia6->ia_ifa);
  980                 }
  981                 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
  982                 goto done;
  983         }
  984 
  985         /*
  986          * try to fragment the packet.  case 1-b and 3
  987          */
  988         if (mtu < IPV6_MMTU) {
  989                 /* path MTU cannot be less than IPV6_MMTU */
  990                 error = EMSGSIZE;
  991                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
  992                 goto bad;
  993         } else if (ip6->ip6_plen == 0) {
  994                 /* jumbo payload cannot be fragmented */
  995                 error = EMSGSIZE;
  996                 in6_ifstat_inc(ifp, ifs6_out_fragfail);
  997                 goto bad;
  998         } else {
  999                 struct mbuf **mnext, *m_frgpart;
 1000                 struct ip6_frag *ip6f;
 1001                 u_int32_t id = htonl(ip6_randomid());
 1002                 u_char nextproto;
 1003 
 1004                 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
 1005 
 1006                 /*
 1007                  * Too large for the destination or interface;
 1008                  * fragment if possible.
 1009                  * Must be able to put at least 8 bytes per fragment.
 1010                  */
 1011                 hlen = unfragpartlen;
 1012                 if (mtu > IPV6_MAXPACKET)
 1013                         mtu = IPV6_MAXPACKET;
 1014 
 1015                 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
 1016                 if (len < 8) {
 1017                         error = EMSGSIZE;
 1018                         in6_ifstat_inc(ifp, ifs6_out_fragfail);
 1019                         goto bad;
 1020                 }
 1021 
 1022                 /*
 1023                  * Verify that we have any chance at all of being able to queue
 1024                  *      the packet or packet fragments
 1025                  */
 1026                 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
 1027                     < tlen  /* - hlen */)) {
 1028                         error = ENOBUFS;
 1029                         V_ip6stat.ip6s_odropped++;
 1030                         goto bad;
 1031                 }
 1032 
 1033                 mnext = &m->m_nextpkt;
 1034 
 1035                 /*
 1036                  * Change the next header field of the last header in the
 1037                  * unfragmentable part.
 1038                  */
 1039                 if (exthdrs.ip6e_rthdr) {
 1040                         nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
 1041                         *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
 1042                 } else if (exthdrs.ip6e_dest1) {
 1043                         nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
 1044                         *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
 1045                 } else if (exthdrs.ip6e_hbh) {
 1046                         nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
 1047                         *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
 1048                 } else {
 1049                         nextproto = ip6->ip6_nxt;
 1050                         ip6->ip6_nxt = IPPROTO_FRAGMENT;
 1051                 }
 1052 
 1053                 /*
 1054                  * Loop through length of segment after first fragment,
 1055                  * make new header and copy data of each part and link onto
 1056                  * chain.
 1057                  */
 1058                 m0 = m;
 1059                 for (off = hlen; off < tlen; off += len) {
 1060                         MGETHDR(m, M_DONTWAIT, MT_HEADER);
 1061                         if (!m) {
 1062                                 error = ENOBUFS;
 1063                                 V_ip6stat.ip6s_odropped++;
 1064                                 goto sendorfree;
 1065                         }
 1066                         m->m_pkthdr.rcvif = NULL;
 1067                         m->m_flags = m0->m_flags & M_COPYFLAGS;
 1068                         *mnext = m;
 1069                         mnext = &m->m_nextpkt;
 1070                         m->m_data += max_linkhdr;
 1071                         mhip6 = mtod(m, struct ip6_hdr *);
 1072                         *mhip6 = *ip6;
 1073                         m->m_len = sizeof(*mhip6);
 1074                         error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
 1075                         if (error) {
 1076                                 V_ip6stat.ip6s_odropped++;
 1077                                 goto sendorfree;
 1078                         }
 1079                         ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
 1080                         if (off + len >= tlen)
 1081                                 len = tlen - off;
 1082                         else
 1083                                 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
 1084                         mhip6->ip6_plen = htons((u_short)(len + hlen +
 1085                             sizeof(*ip6f) - sizeof(struct ip6_hdr)));
 1086                         if ((m_frgpart = m_copy(m0, off, len)) == 0) {
 1087                                 error = ENOBUFS;
 1088                                 V_ip6stat.ip6s_odropped++;
 1089                                 goto sendorfree;
 1090                         }
 1091                         m_cat(m, m_frgpart);
 1092                         m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
 1093                         m->m_pkthdr.rcvif = NULL;
 1094                         ip6f->ip6f_reserved = 0;
 1095                         ip6f->ip6f_ident = id;
 1096                         ip6f->ip6f_nxt = nextproto;
 1097                         V_ip6stat.ip6s_ofragments++;
 1098                         in6_ifstat_inc(ifp, ifs6_out_fragcreat);
 1099                 }
 1100 
 1101                 in6_ifstat_inc(ifp, ifs6_out_fragok);
 1102         }
 1103 
 1104         /*
 1105          * Remove leading garbages.
 1106          */
 1107 sendorfree:
 1108         m = m0->m_nextpkt;
 1109         m0->m_nextpkt = 0;
 1110         m_freem(m0);
 1111         for (m0 = m; m; m = m0) {
 1112                 m0 = m->m_nextpkt;
 1113                 m->m_nextpkt = 0;
 1114                 if (error == 0) {
 1115                         /* Record statistics for this interface address. */
 1116                         if (ia) {
 1117                                 ia->ia_ifa.if_opackets++;
 1118                                 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
 1119                         }
 1120                         error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
 1121                 } else
 1122                         m_freem(m);
 1123         }
 1124 
 1125         if (error == 0)
 1126                 V_ip6stat.ip6s_fragmented++;
 1127 
 1128 done:
 1129         if (ro == &ip6route && ro->ro_rt && flevalid == 0) {
 1130                 /* brace necessary for RTFREE */
 1131                 RTFREE(ro->ro_rt);
 1132         } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt &&
 1133             ((flevalid == 0) || (ro_pmtu != ro))) {
 1134                 RTFREE(ro_pmtu->ro_rt);
 1135         }
 1136 #ifdef IPSEC
 1137         if (sp != NULL)
 1138                 KEY_FREESP(&sp);
 1139 #endif
 1140 
 1141         return (error);
 1142 
 1143 freehdrs:
 1144         m_freem(exthdrs.ip6e_hbh);      /* m_freem will check if mbuf is 0 */
 1145         m_freem(exthdrs.ip6e_dest1);
 1146         m_freem(exthdrs.ip6e_rthdr);
 1147         m_freem(exthdrs.ip6e_dest2);
 1148         /* FALLTHROUGH */
 1149 bad:
 1150         if (m)
 1151                 m_freem(m);
 1152         goto done;
 1153 }
 1154 
 1155 static int
 1156 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
 1157 {
 1158         struct mbuf *m;
 1159 
 1160         if (hlen > MCLBYTES)
 1161                 return (ENOBUFS); /* XXX */
 1162 
 1163         MGET(m, M_DONTWAIT, MT_DATA);
 1164         if (!m)
 1165                 return (ENOBUFS);
 1166 
 1167         if (hlen > MLEN) {
 1168                 MCLGET(m, M_DONTWAIT);
 1169                 if ((m->m_flags & M_EXT) == 0) {
 1170                         m_free(m);
 1171                         return (ENOBUFS);
 1172                 }
 1173         }
 1174         m->m_len = hlen;
 1175         if (hdr)
 1176                 bcopy(hdr, mtod(m, caddr_t), hlen);
 1177 
 1178         *mp = m;
 1179         return (0);
 1180 }
 1181 
 1182 /*
 1183  * Insert jumbo payload option.
 1184  */
 1185 static int
 1186 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
 1187 {
 1188         struct mbuf *mopt;
 1189         u_char *optbuf;
 1190         u_int32_t v;
 1191 
 1192 #define JUMBOOPTLEN     8       /* length of jumbo payload option and padding */
 1193 
 1194         /*
 1195          * If there is no hop-by-hop options header, allocate new one.
 1196          * If there is one but it doesn't have enough space to store the
 1197          * jumbo payload option, allocate a cluster to store the whole options.
 1198          * Otherwise, use it to store the options.
 1199          */
 1200         if (exthdrs->ip6e_hbh == 0) {
 1201                 MGET(mopt, M_DONTWAIT, MT_DATA);
 1202                 if (mopt == 0)
 1203                         return (ENOBUFS);
 1204                 mopt->m_len = JUMBOOPTLEN;
 1205                 optbuf = mtod(mopt, u_char *);
 1206                 optbuf[1] = 0;  /* = ((JUMBOOPTLEN) >> 3) - 1 */
 1207                 exthdrs->ip6e_hbh = mopt;
 1208         } else {
 1209                 struct ip6_hbh *hbh;
 1210 
 1211                 mopt = exthdrs->ip6e_hbh;
 1212                 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
 1213                         /*
 1214                          * XXX assumption:
 1215                          * - exthdrs->ip6e_hbh is not referenced from places
 1216                          *   other than exthdrs.
 1217                          * - exthdrs->ip6e_hbh is not an mbuf chain.
 1218                          */
 1219                         int oldoptlen = mopt->m_len;
 1220                         struct mbuf *n;
 1221 
 1222                         /*
 1223                          * XXX: give up if the whole (new) hbh header does
 1224                          * not fit even in an mbuf cluster.
 1225                          */
 1226                         if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
 1227                                 return (ENOBUFS);
 1228 
 1229                         /*
 1230                          * As a consequence, we must always prepare a cluster
 1231                          * at this point.
 1232                          */
 1233                         MGET(n, M_DONTWAIT, MT_DATA);
 1234                         if (n) {
 1235                                 MCLGET(n, M_DONTWAIT);
 1236                                 if ((n->m_flags & M_EXT) == 0) {
 1237                                         m_freem(n);
 1238                                         n = NULL;
 1239                                 }
 1240                         }
 1241                         if (!n)
 1242                                 return (ENOBUFS);
 1243                         n->m_len = oldoptlen + JUMBOOPTLEN;
 1244                         bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
 1245                             oldoptlen);
 1246                         optbuf = mtod(n, caddr_t) + oldoptlen;
 1247                         m_freem(mopt);
 1248                         mopt = exthdrs->ip6e_hbh = n;
 1249                 } else {
 1250                         optbuf = mtod(mopt, u_char *) + mopt->m_len;
 1251                         mopt->m_len += JUMBOOPTLEN;
 1252                 }
 1253                 optbuf[0] = IP6OPT_PADN;
 1254                 optbuf[1] = 1;
 1255 
 1256                 /*
 1257                  * Adjust the header length according to the pad and
 1258                  * the jumbo payload option.
 1259                  */
 1260                 hbh = mtod(mopt, struct ip6_hbh *);
 1261                 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
 1262         }
 1263 
 1264         /* fill in the option. */
 1265         optbuf[2] = IP6OPT_JUMBO;
 1266         optbuf[3] = 4;
 1267         v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
 1268         bcopy(&v, &optbuf[4], sizeof(u_int32_t));
 1269 
 1270         /* finally, adjust the packet header length */
 1271         exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
 1272 
 1273         return (0);
 1274 #undef JUMBOOPTLEN
 1275 }
 1276 
 1277 /*
 1278  * Insert fragment header and copy unfragmentable header portions.
 1279  */
 1280 static int
 1281 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
 1282     struct ip6_frag **frghdrp)
 1283 {
 1284         struct mbuf *n, *mlast;
 1285 
 1286         if (hlen > sizeof(struct ip6_hdr)) {
 1287                 n = m_copym(m0, sizeof(struct ip6_hdr),
 1288                     hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
 1289                 if (n == 0)
 1290                         return (ENOBUFS);
 1291                 m->m_next = n;
 1292         } else
 1293                 n = m;
 1294 
 1295         /* Search for the last mbuf of unfragmentable part. */
 1296         for (mlast = n; mlast->m_next; mlast = mlast->m_next)
 1297                 ;
 1298 
 1299         if ((mlast->m_flags & M_EXT) == 0 &&
 1300             M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
 1301                 /* use the trailing space of the last mbuf for the fragment hdr */
 1302                 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
 1303                     mlast->m_len);
 1304                 mlast->m_len += sizeof(struct ip6_frag);
 1305                 m->m_pkthdr.len += sizeof(struct ip6_frag);
 1306         } else {
 1307                 /* allocate a new mbuf for the fragment header */
 1308                 struct mbuf *mfrg;
 1309 
 1310                 MGET(mfrg, M_DONTWAIT, MT_DATA);
 1311                 if (mfrg == 0)
 1312                         return (ENOBUFS);
 1313                 mfrg->m_len = sizeof(struct ip6_frag);
 1314                 *frghdrp = mtod(mfrg, struct ip6_frag *);
 1315                 mlast->m_next = mfrg;
 1316         }
 1317 
 1318         return (0);
 1319 }
 1320 
 1321 static int
 1322 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
 1323     struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
 1324     int *alwaysfragp)
 1325 {
 1326         u_int32_t mtu = 0;
 1327         int alwaysfrag = 0;
 1328         int error = 0;
 1329 
 1330         if (ro_pmtu != ro) {
 1331                 /* The first hop and the final destination may differ. */
 1332                 struct sockaddr_in6 *sa6_dst =
 1333                     (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
 1334                 if (ro_pmtu->ro_rt &&
 1335                     ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
 1336                      !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
 1337                         RTFREE(ro_pmtu->ro_rt);
 1338                         ro_pmtu->ro_rt = (struct rtentry *)NULL;
 1339                 }
 1340                 if (ro_pmtu->ro_rt == NULL) {
 1341                         bzero(sa6_dst, sizeof(*sa6_dst));
 1342                         sa6_dst->sin6_family = AF_INET6;
 1343                         sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
 1344                         sa6_dst->sin6_addr = *dst;
 1345 
 1346                         rtalloc((struct route *)ro_pmtu);
 1347                 }
 1348         }
 1349         if (ro_pmtu->ro_rt) {
 1350                 u_int32_t ifmtu;
 1351                 struct in_conninfo inc;
 1352 
 1353                 bzero(&inc, sizeof(inc));
 1354                 inc.inc_flags |= INC_ISIPV6;
 1355                 inc.inc6_faddr = *dst;
 1356 
 1357                 if (ifp == NULL)
 1358                         ifp = ro_pmtu->ro_rt->rt_ifp;
 1359                 ifmtu = IN6_LINKMTU(ifp);
 1360                 mtu = tcp_hc_getmtu(&inc);
 1361                 if (mtu)
 1362                         mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
 1363                 else
 1364                         mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
 1365                 if (mtu == 0)
 1366                         mtu = ifmtu;
 1367                 else if (mtu < IPV6_MMTU) {
 1368                         /*
 1369                          * RFC2460 section 5, last paragraph:
 1370                          * if we record ICMPv6 too big message with
 1371                          * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
 1372                          * or smaller, with framgent header attached.
 1373                          * (fragment header is needed regardless from the
 1374                          * packet size, for translators to identify packets)
 1375                          */
 1376                         alwaysfrag = 1;
 1377                         mtu = IPV6_MMTU;
 1378                 } else if (mtu > ifmtu) {
 1379                         /*
 1380                          * The MTU on the route is larger than the MTU on
 1381                          * the interface!  This shouldn't happen, unless the
 1382                          * MTU of the interface has been changed after the
 1383                          * interface was brought up.  Change the MTU in the
 1384                          * route to match the interface MTU (as long as the
 1385                          * field isn't locked).
 1386                          */
 1387                         mtu = ifmtu;
 1388                         ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
 1389                 }
 1390         } else if (ifp) {
 1391                 mtu = IN6_LINKMTU(ifp);
 1392         } else
 1393                 error = EHOSTUNREACH; /* XXX */
 1394 
 1395         *mtup = mtu;
 1396         if (alwaysfragp)
 1397                 *alwaysfragp = alwaysfrag;
 1398         return (error);
 1399 }
 1400 
 1401 /*
 1402  * IP6 socket option processing.
 1403  */
 1404 int
 1405 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
 1406 {
 1407         int optdatalen, uproto;
 1408         void *optdata;
 1409         struct inpcb *in6p = sotoinpcb(so);
 1410         int error, optval;
 1411         int level, op, optname;
 1412         int optlen;
 1413         struct thread *td;
 1414 
 1415         level = sopt->sopt_level;
 1416         op = sopt->sopt_dir;
 1417         optname = sopt->sopt_name;
 1418         optlen = sopt->sopt_valsize;
 1419         td = sopt->sopt_td;
 1420         error = 0;
 1421         optval = 0;
 1422         uproto = (int)so->so_proto->pr_protocol;
 1423 
 1424         if (level != IPPROTO_IPV6) {
 1425                 error = EINVAL;
 1426 
 1427                 if (sopt->sopt_level == SOL_SOCKET &&
 1428                     sopt->sopt_dir == SOPT_SET) {
 1429                         switch (sopt->sopt_name) {
 1430                         case SO_REUSEADDR:
 1431                                 INP_WLOCK(in6p);
 1432                                 if (IN_MULTICAST(ntohl(in6p->inp_laddr.s_addr))) {
 1433                                         if ((so->so_options &
 1434                                             (SO_REUSEADDR | SO_REUSEPORT)) != 0)
 1435                                                 in6p->inp_flags2 |= INP_REUSEPORT;
 1436                                         else
 1437                                                 in6p->inp_flags2 &= ~INP_REUSEPORT;
 1438                                 }
 1439                                 INP_WUNLOCK(in6p);
 1440                                 error = 0;
 1441                                 break;
 1442                         case SO_REUSEPORT:
 1443                                 INP_WLOCK(in6p);
 1444                                 if ((so->so_options & SO_REUSEPORT) != 0)
 1445                                         in6p->inp_flags2 |= INP_REUSEPORT;
 1446                                 else
 1447                                         in6p->inp_flags2 &= ~INP_REUSEPORT;
 1448                                 INP_WUNLOCK(in6p);
 1449                                 error = 0;
 1450                                 break;
 1451                         default:
 1452                                 break;
 1453                         }
 1454                 }
 1455         } else {                /* level == IPPROTO_IPV6 */
 1456                 switch (op) {
 1457 
 1458                 case SOPT_SET:
 1459                         switch (optname) {
 1460                         case IPV6_2292PKTOPTIONS:
 1461 #ifdef IPV6_PKTOPTIONS
 1462                         case IPV6_PKTOPTIONS:
 1463 #endif
 1464                         {
 1465                                 struct mbuf *m;
 1466 
 1467                                 error = soopt_getm(sopt, &m); /* XXX */
 1468                                 if (error != 0)
 1469                                         break;
 1470                                 error = soopt_mcopyin(sopt, m); /* XXX */
 1471                                 if (error != 0)
 1472                                         break;
 1473                                 error = ip6_pcbopts(&in6p->in6p_outputopts,
 1474                                                     m, so, sopt);
 1475                                 m_freem(m); /* XXX */
 1476                                 break;
 1477                         }
 1478 
 1479                         /*
 1480                          * Use of some Hop-by-Hop options or some
 1481                          * Destination options, might require special
 1482                          * privilege.  That is, normal applications
 1483                          * (without special privilege) might be forbidden
 1484                          * from setting certain options in outgoing packets,
 1485                          * and might never see certain options in received
 1486                          * packets. [RFC 2292 Section 6]
 1487                          * KAME specific note:
 1488                          *  KAME prevents non-privileged users from sending or
 1489                          *  receiving ANY hbh/dst options in order to avoid
 1490                          *  overhead of parsing options in the kernel.
 1491                          */
 1492                         case IPV6_RECVHOPOPTS:
 1493                         case IPV6_RECVDSTOPTS:
 1494                         case IPV6_RECVRTHDRDSTOPTS:
 1495                                 if (td != NULL) {
 1496                                         error = priv_check(td,
 1497                                             PRIV_NETINET_SETHDROPTS);
 1498                                         if (error)
 1499                                                 break;
 1500                                 }
 1501                                 /* FALLTHROUGH */
 1502                         case IPV6_UNICAST_HOPS:
 1503                         case IPV6_HOPLIMIT:
 1504                         case IPV6_FAITH:
 1505 
 1506                         case IPV6_RECVPKTINFO:
 1507                         case IPV6_RECVHOPLIMIT:
 1508                         case IPV6_RECVRTHDR:
 1509                         case IPV6_RECVPATHMTU:
 1510                         case IPV6_RECVTCLASS:
 1511                         case IPV6_V6ONLY:
 1512                         case IPV6_AUTOFLOWLABEL:
 1513                         case IPV6_BINDANY:
 1514                                 if (optname == IPV6_BINDANY && td != NULL) {
 1515                                         error = priv_check(td,
 1516                                             PRIV_NETINET_BINDANY);
 1517                                         if (error)
 1518                                                 break;
 1519                                 }
 1520 
 1521                                 if (optlen != sizeof(int)) {
 1522                                         error = EINVAL;
 1523                                         break;
 1524                                 }
 1525                                 error = sooptcopyin(sopt, &optval,
 1526                                         sizeof optval, sizeof optval);
 1527                                 if (error)
 1528                                         break;
 1529                                 switch (optname) {
 1530 
 1531                                 case IPV6_UNICAST_HOPS:
 1532                                         if (optval < -1 || optval >= 256)
 1533                                                 error = EINVAL;
 1534                                         else {
 1535                                                 /* -1 = kernel default */
 1536                                                 in6p->in6p_hops = optval;
 1537                                                 if ((in6p->inp_vflag &
 1538                                                      INP_IPV4) != 0)
 1539                                                         in6p->inp_ip_ttl = optval;
 1540                                         }
 1541                                         break;
 1542 #define OPTSET(bit) \
 1543 do { \
 1544         if (optval) \
 1545                 in6p->inp_flags |= (bit); \
 1546         else \
 1547                 in6p->inp_flags &= ~(bit); \
 1548 } while (/*CONSTCOND*/ 0)
 1549 #define OPTSET2292(bit) \
 1550 do { \
 1551         in6p->inp_flags |= IN6P_RFC2292; \
 1552         if (optval) \
 1553                 in6p->inp_flags |= (bit); \
 1554         else \
 1555                 in6p->inp_flags &= ~(bit); \
 1556 } while (/*CONSTCOND*/ 0)
 1557 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
 1558 
 1559                                 case IPV6_RECVPKTINFO:
 1560                                         /* cannot mix with RFC2292 */
 1561                                         if (OPTBIT(IN6P_RFC2292)) {
 1562                                                 error = EINVAL;
 1563                                                 break;
 1564                                         }
 1565                                         OPTSET(IN6P_PKTINFO);
 1566                                         break;
 1567 
 1568                                 case IPV6_HOPLIMIT:
 1569                                 {
 1570                                         struct ip6_pktopts **optp;
 1571 
 1572                                         /* cannot mix with RFC2292 */
 1573                                         if (OPTBIT(IN6P_RFC2292)) {
 1574                                                 error = EINVAL;
 1575                                                 break;
 1576                                         }
 1577                                         optp = &in6p->in6p_outputopts;
 1578                                         error = ip6_pcbopt(IPV6_HOPLIMIT,
 1579                                             (u_char *)&optval, sizeof(optval),
 1580                                             optp, (td != NULL) ? td->td_ucred :
 1581                                             NULL, uproto);
 1582                                         break;
 1583                                 }
 1584 
 1585                                 case IPV6_RECVHOPLIMIT:
 1586                                         /* cannot mix with RFC2292 */
 1587                                         if (OPTBIT(IN6P_RFC2292)) {
 1588                                                 error = EINVAL;
 1589                                                 break;
 1590                                         }
 1591                                         OPTSET(IN6P_HOPLIMIT);
 1592                                         break;
 1593 
 1594                                 case IPV6_RECVHOPOPTS:
 1595                                         /* cannot mix with RFC2292 */
 1596                                         if (OPTBIT(IN6P_RFC2292)) {
 1597                                                 error = EINVAL;
 1598                                                 break;
 1599                                         }
 1600                                         OPTSET(IN6P_HOPOPTS);
 1601                                         break;
 1602 
 1603                                 case IPV6_RECVDSTOPTS:
 1604                                         /* cannot mix with RFC2292 */
 1605                                         if (OPTBIT(IN6P_RFC2292)) {
 1606                                                 error = EINVAL;
 1607                                                 break;
 1608                                         }
 1609                                         OPTSET(IN6P_DSTOPTS);
 1610                                         break;
 1611 
 1612                                 case IPV6_RECVRTHDRDSTOPTS:
 1613                                         /* cannot mix with RFC2292 */
 1614                                         if (OPTBIT(IN6P_RFC2292)) {
 1615                                                 error = EINVAL;
 1616                                                 break;
 1617                                         }
 1618                                         OPTSET(IN6P_RTHDRDSTOPTS);
 1619                                         break;
 1620 
 1621                                 case IPV6_RECVRTHDR:
 1622                                         /* cannot mix with RFC2292 */
 1623                                         if (OPTBIT(IN6P_RFC2292)) {
 1624                                                 error = EINVAL;
 1625                                                 break;
 1626                                         }
 1627                                         OPTSET(IN6P_RTHDR);
 1628                                         break;
 1629 
 1630                                 case IPV6_FAITH:
 1631                                         OPTSET(INP_FAITH);
 1632                                         break;
 1633 
 1634                                 case IPV6_RECVPATHMTU:
 1635                                         /*
 1636                                          * We ignore this option for TCP
 1637                                          * sockets.
 1638                                          * (RFC3542 leaves this case
 1639                                          * unspecified.)
 1640                                          */
 1641                                         if (uproto != IPPROTO_TCP)
 1642                                                 OPTSET(IN6P_MTU);
 1643                                         break;
 1644 
 1645                                 case IPV6_V6ONLY:
 1646                                         /*
 1647                                          * make setsockopt(IPV6_V6ONLY)
 1648                                          * available only prior to bind(2).
 1649                                          * see ipng mailing list, Jun 22 2001.
 1650                                          */
 1651                                         if (in6p->inp_lport ||
 1652                                             !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
 1653                                                 error = EINVAL;
 1654                                                 break;
 1655                                         }
 1656                                         OPTSET(IN6P_IPV6_V6ONLY);
 1657                                         if (optval)
 1658                                                 in6p->inp_vflag &= ~INP_IPV4;
 1659                                         else
 1660                                                 in6p->inp_vflag |= INP_IPV4;
 1661                                         break;
 1662                                 case IPV6_RECVTCLASS:
 1663                                         /* cannot mix with RFC2292 XXX */
 1664                                         if (OPTBIT(IN6P_RFC2292)) {
 1665                                                 error = EINVAL;
 1666                                                 break;
 1667                                         }
 1668                                         OPTSET(IN6P_TCLASS);
 1669                                         break;
 1670                                 case IPV6_AUTOFLOWLABEL:
 1671                                         OPTSET(IN6P_AUTOFLOWLABEL);
 1672                                         break;
 1673 
 1674                                 case IPV6_BINDANY:
 1675                                         OPTSET(INP_BINDANY);
 1676                                         break;
 1677                                 }
 1678                                 break;
 1679 
 1680                         case IPV6_TCLASS:
 1681                         case IPV6_DONTFRAG:
 1682                         case IPV6_USE_MIN_MTU:
 1683                         case IPV6_PREFER_TEMPADDR:
 1684                                 if (optlen != sizeof(optval)) {
 1685                                         error = EINVAL;
 1686                                         break;
 1687                                 }
 1688                                 error = sooptcopyin(sopt, &optval,
 1689                                         sizeof optval, sizeof optval);
 1690                                 if (error)
 1691                                         break;
 1692                                 {
 1693                                         struct ip6_pktopts **optp;
 1694                                         optp = &in6p->in6p_outputopts;
 1695                                         error = ip6_pcbopt(optname,
 1696                                             (u_char *)&optval, sizeof(optval),
 1697                                             optp, (td != NULL) ? td->td_ucred :
 1698                                             NULL, uproto);
 1699                                         break;
 1700                                 }
 1701 
 1702                         case IPV6_2292PKTINFO:
 1703                         case IPV6_2292HOPLIMIT:
 1704                         case IPV6_2292HOPOPTS:
 1705                         case IPV6_2292DSTOPTS:
 1706                         case IPV6_2292RTHDR:
 1707                                 /* RFC 2292 */
 1708                                 if (optlen != sizeof(int)) {
 1709                                         error = EINVAL;
 1710                                         break;
 1711                                 }
 1712                                 error = sooptcopyin(sopt, &optval,
 1713                                         sizeof optval, sizeof optval);
 1714                                 if (error)
 1715                                         break;
 1716                                 switch (optname) {
 1717                                 case IPV6_2292PKTINFO:
 1718                                         OPTSET2292(IN6P_PKTINFO);
 1719                                         break;
 1720                                 case IPV6_2292HOPLIMIT:
 1721                                         OPTSET2292(IN6P_HOPLIMIT);
 1722                                         break;
 1723                                 case IPV6_2292HOPOPTS:
 1724                                         /*
 1725                                          * Check super-user privilege.
 1726                                          * See comments for IPV6_RECVHOPOPTS.
 1727                                          */
 1728                                         if (td != NULL) {
 1729                                                 error = priv_check(td,
 1730                                                     PRIV_NETINET_SETHDROPTS);
 1731                                                 if (error)
 1732                                                         return (error);
 1733                                         }
 1734                                         OPTSET2292(IN6P_HOPOPTS);
 1735                                         break;
 1736                                 case IPV6_2292DSTOPTS:
 1737                                         if (td != NULL) {
 1738                                                 error = priv_check(td,
 1739                                                     PRIV_NETINET_SETHDROPTS);
 1740                                                 if (error)
 1741                                                         return (error);
 1742                                         }
 1743                                         OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
 1744                                         break;
 1745                                 case IPV6_2292RTHDR:
 1746                                         OPTSET2292(IN6P_RTHDR);
 1747                                         break;
 1748                                 }
 1749                                 break;
 1750                         case IPV6_PKTINFO:
 1751                         case IPV6_HOPOPTS:
 1752                         case IPV6_RTHDR:
 1753                         case IPV6_DSTOPTS:
 1754                         case IPV6_RTHDRDSTOPTS:
 1755                         case IPV6_NEXTHOP:
 1756                         {
 1757                                 /* new advanced API (RFC3542) */
 1758                                 u_char *optbuf;
 1759                                 u_char optbuf_storage[MCLBYTES];
 1760                                 int optlen;
 1761                                 struct ip6_pktopts **optp;
 1762 
 1763                                 /* cannot mix with RFC2292 */
 1764                                 if (OPTBIT(IN6P_RFC2292)) {
 1765                                         error = EINVAL;
 1766                                         break;
 1767                                 }
 1768 
 1769                                 /*
 1770                                  * We only ensure valsize is not too large
 1771                                  * here.  Further validation will be done
 1772                                  * later.
 1773                                  */
 1774                                 error = sooptcopyin(sopt, optbuf_storage,
 1775                                     sizeof(optbuf_storage), 0);
 1776                                 if (error)
 1777                                         break;
 1778                                 optlen = sopt->sopt_valsize;
 1779                                 optbuf = optbuf_storage;
 1780                                 optp = &in6p->in6p_outputopts;
 1781                                 error = ip6_pcbopt(optname, optbuf, optlen,
 1782                                     optp, (td != NULL) ? td->td_ucred : NULL,
 1783                                     uproto);
 1784                                 break;
 1785                         }
 1786 #undef OPTSET
 1787 
 1788                         case IPV6_MULTICAST_IF:
 1789                         case IPV6_MULTICAST_HOPS:
 1790                         case IPV6_MULTICAST_LOOP:
 1791                         case IPV6_JOIN_GROUP:
 1792                         case IPV6_LEAVE_GROUP:
 1793                         case IPV6_MSFILTER:
 1794                         case MCAST_BLOCK_SOURCE:
 1795                         case MCAST_UNBLOCK_SOURCE:
 1796                         case MCAST_JOIN_GROUP:
 1797                         case MCAST_LEAVE_GROUP:
 1798                         case MCAST_JOIN_SOURCE_GROUP:
 1799                         case MCAST_LEAVE_SOURCE_GROUP:
 1800                                 error = ip6_setmoptions(in6p, sopt);
 1801                                 break;
 1802 
 1803                         case IPV6_PORTRANGE:
 1804                                 error = sooptcopyin(sopt, &optval,
 1805                                     sizeof optval, sizeof optval);
 1806                                 if (error)
 1807                                         break;
 1808 
 1809                                 switch (optval) {
 1810                                 case IPV6_PORTRANGE_DEFAULT:
 1811                                         in6p->inp_flags &= ~(INP_LOWPORT);
 1812                                         in6p->inp_flags &= ~(INP_HIGHPORT);
 1813                                         break;
 1814 
 1815                                 case IPV6_PORTRANGE_HIGH:
 1816                                         in6p->inp_flags &= ~(INP_LOWPORT);
 1817                                         in6p->inp_flags |= INP_HIGHPORT;
 1818                                         break;
 1819 
 1820                                 case IPV6_PORTRANGE_LOW:
 1821                                         in6p->inp_flags &= ~(INP_HIGHPORT);
 1822                                         in6p->inp_flags |= INP_LOWPORT;
 1823                                         break;
 1824 
 1825                                 default:
 1826                                         error = EINVAL;
 1827                                         break;
 1828                                 }
 1829                                 break;
 1830 
 1831 #ifdef IPSEC
 1832                         case IPV6_IPSEC_POLICY:
 1833                         {
 1834                                 caddr_t req;
 1835                                 struct mbuf *m;
 1836 
 1837                                 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
 1838                                         break;
 1839                                 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
 1840                                         break;
 1841                                 req = mtod(m, caddr_t);
 1842                                 error = ipsec_set_policy(in6p, optname, req,
 1843                                     m->m_len, (sopt->sopt_td != NULL) ?
 1844                                     sopt->sopt_td->td_ucred : NULL);
 1845                                 m_freem(m);
 1846                                 break;
 1847                         }
 1848 #endif /* IPSEC */
 1849 
 1850                         default:
 1851                                 error = ENOPROTOOPT;
 1852                                 break;
 1853                         }
 1854                         break;
 1855 
 1856                 case SOPT_GET:
 1857                         switch (optname) {
 1858 
 1859                         case IPV6_2292PKTOPTIONS:
 1860 #ifdef IPV6_PKTOPTIONS
 1861                         case IPV6_PKTOPTIONS:
 1862 #endif
 1863                                 /*
 1864                                  * RFC3542 (effectively) deprecated the
 1865                                  * semantics of the 2292-style pktoptions.
 1866                                  * Since it was not reliable in nature (i.e.,
 1867                                  * applications had to expect the lack of some
 1868                                  * information after all), it would make sense
 1869                                  * to simplify this part by always returning
 1870                                  * empty data.
 1871                                  */
 1872                                 sopt->sopt_valsize = 0;
 1873                                 break;
 1874 
 1875                         case IPV6_RECVHOPOPTS:
 1876                         case IPV6_RECVDSTOPTS:
 1877                         case IPV6_RECVRTHDRDSTOPTS:
 1878                         case IPV6_UNICAST_HOPS:
 1879                         case IPV6_RECVPKTINFO:
 1880                         case IPV6_RECVHOPLIMIT:
 1881                         case IPV6_RECVRTHDR:
 1882                         case IPV6_RECVPATHMTU:
 1883 
 1884                         case IPV6_FAITH:
 1885                         case IPV6_V6ONLY:
 1886                         case IPV6_PORTRANGE:
 1887                         case IPV6_RECVTCLASS:
 1888                         case IPV6_AUTOFLOWLABEL:
 1889                         case IPV6_BINDANY:
 1890                                 switch (optname) {
 1891 
 1892                                 case IPV6_RECVHOPOPTS:
 1893                                         optval = OPTBIT(IN6P_HOPOPTS);
 1894                                         break;
 1895 
 1896                                 case IPV6_RECVDSTOPTS:
 1897                                         optval = OPTBIT(IN6P_DSTOPTS);
 1898                                         break;
 1899 
 1900                                 case IPV6_RECVRTHDRDSTOPTS:
 1901                                         optval = OPTBIT(IN6P_RTHDRDSTOPTS);
 1902                                         break;
 1903 
 1904                                 case IPV6_UNICAST_HOPS:
 1905                                         optval = in6p->in6p_hops;
 1906                                         break;
 1907 
 1908                                 case IPV6_RECVPKTINFO:
 1909                                         optval = OPTBIT(IN6P_PKTINFO);
 1910                                         break;
 1911 
 1912                                 case IPV6_RECVHOPLIMIT:
 1913                                         optval = OPTBIT(IN6P_HOPLIMIT);
 1914                                         break;
 1915 
 1916                                 case IPV6_RECVRTHDR:
 1917                                         optval = OPTBIT(IN6P_RTHDR);
 1918                                         break;
 1919 
 1920                                 case IPV6_RECVPATHMTU:
 1921                                         optval = OPTBIT(IN6P_MTU);
 1922                                         break;
 1923 
 1924                                 case IPV6_FAITH:
 1925                                         optval = OPTBIT(INP_FAITH);
 1926                                         break;
 1927 
 1928                                 case IPV6_V6ONLY:
 1929                                         optval = OPTBIT(IN6P_IPV6_V6ONLY);
 1930                                         break;
 1931 
 1932                                 case IPV6_PORTRANGE:
 1933                                     {
 1934                                         int flags;
 1935                                         flags = in6p->inp_flags;
 1936                                         if (flags & INP_HIGHPORT)
 1937                                                 optval = IPV6_PORTRANGE_HIGH;
 1938                                         else if (flags & INP_LOWPORT)
 1939                                                 optval = IPV6_PORTRANGE_LOW;
 1940                                         else
 1941                                                 optval = 0;
 1942                                         break;
 1943                                     }
 1944                                 case IPV6_RECVTCLASS:
 1945                                         optval = OPTBIT(IN6P_TCLASS);
 1946                                         break;
 1947 
 1948                                 case IPV6_AUTOFLOWLABEL:
 1949                                         optval = OPTBIT(IN6P_AUTOFLOWLABEL);
 1950                                         break;
 1951 
 1952                                 case IPV6_BINDANY:
 1953                                         optval = OPTBIT(INP_BINDANY);
 1954                                         break;
 1955                                 }
 1956                                 if (error)
 1957                                         break;
 1958                                 error = sooptcopyout(sopt, &optval,
 1959                                         sizeof optval);
 1960                                 break;
 1961 
 1962                         case IPV6_PATHMTU:
 1963                         {
 1964                                 u_long pmtu = 0;
 1965                                 struct ip6_mtuinfo mtuinfo;
 1966                                 struct route_in6 sro;
 1967 
 1968                                 bzero(&sro, sizeof(sro));
 1969 
 1970                                 if (!(so->so_state & SS_ISCONNECTED))
 1971                                         return (ENOTCONN);
 1972                                 /*
 1973                                  * XXX: we dot not consider the case of source
 1974                                  * routing, or optional information to specify
 1975                                  * the outgoing interface.
 1976                                  */
 1977                                 error = ip6_getpmtu(&sro, NULL, NULL,
 1978                                     &in6p->in6p_faddr, &pmtu, NULL);
 1979                                 if (sro.ro_rt)
 1980                                         RTFREE(sro.ro_rt);
 1981                                 if (error)
 1982                                         break;
 1983                                 if (pmtu > IPV6_MAXPACKET)
 1984                                         pmtu = IPV6_MAXPACKET;
 1985 
 1986                                 bzero(&mtuinfo, sizeof(mtuinfo));
 1987                                 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
 1988                                 optdata = (void *)&mtuinfo;
 1989                                 optdatalen = sizeof(mtuinfo);
 1990                                 error = sooptcopyout(sopt, optdata,
 1991                                     optdatalen);
 1992                                 break;
 1993                         }
 1994 
 1995                         case IPV6_2292PKTINFO:
 1996                         case IPV6_2292HOPLIMIT:
 1997                         case IPV6_2292HOPOPTS:
 1998                         case IPV6_2292RTHDR:
 1999                         case IPV6_2292DSTOPTS:
 2000                                 switch (optname) {
 2001                                 case IPV6_2292PKTINFO:
 2002                                         optval = OPTBIT(IN6P_PKTINFO);
 2003                                         break;
 2004                                 case IPV6_2292HOPLIMIT:
 2005                                         optval = OPTBIT(IN6P_HOPLIMIT);
 2006                                         break;
 2007                                 case IPV6_2292HOPOPTS:
 2008                                         optval = OPTBIT(IN6P_HOPOPTS);
 2009                                         break;
 2010                                 case IPV6_2292RTHDR:
 2011                                         optval = OPTBIT(IN6P_RTHDR);
 2012                                         break;
 2013                                 case IPV6_2292DSTOPTS:
 2014                                         optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
 2015                                         break;
 2016                                 }
 2017                                 error = sooptcopyout(sopt, &optval,
 2018                                     sizeof optval);
 2019                                 break;
 2020                         case IPV6_PKTINFO:
 2021                         case IPV6_HOPOPTS:
 2022                         case IPV6_RTHDR:
 2023                         case IPV6_DSTOPTS:
 2024                         case IPV6_RTHDRDSTOPTS:
 2025                         case IPV6_NEXTHOP:
 2026                         case IPV6_TCLASS:
 2027                         case IPV6_DONTFRAG:
 2028                         case IPV6_USE_MIN_MTU:
 2029                         case IPV6_PREFER_TEMPADDR:
 2030                                 error = ip6_getpcbopt(in6p->in6p_outputopts,
 2031                                     optname, sopt);
 2032                                 break;
 2033 
 2034                         case IPV6_MULTICAST_IF:
 2035                         case IPV6_MULTICAST_HOPS:
 2036                         case IPV6_MULTICAST_LOOP:
 2037                         case IPV6_MSFILTER:
 2038                                 error = ip6_getmoptions(in6p, sopt);
 2039                                 break;
 2040 
 2041 #ifdef IPSEC
 2042                         case IPV6_IPSEC_POLICY:
 2043                           {
 2044                                 caddr_t req = NULL;
 2045                                 size_t len = 0;
 2046                                 struct mbuf *m = NULL;
 2047                                 struct mbuf **mp = &m;
 2048                                 size_t ovalsize = sopt->sopt_valsize;
 2049                                 caddr_t oval = (caddr_t)sopt->sopt_val;
 2050 
 2051                                 error = soopt_getm(sopt, &m); /* XXX */
 2052                                 if (error != 0)
 2053                                         break;
 2054                                 error = soopt_mcopyin(sopt, m); /* XXX */
 2055                                 if (error != 0)
 2056                                         break;
 2057                                 sopt->sopt_valsize = ovalsize;
 2058                                 sopt->sopt_val = oval;
 2059                                 if (m) {
 2060                                         req = mtod(m, caddr_t);
 2061                                         len = m->m_len;
 2062                                 }
 2063                                 error = ipsec_get_policy(in6p, req, len, mp);
 2064                                 if (error == 0)
 2065                                         error = soopt_mcopyout(sopt, m); /* XXX */
 2066                                 if (error == 0 && m)
 2067                                         m_freem(m);
 2068                                 break;
 2069                           }
 2070 #endif /* IPSEC */
 2071 
 2072                         default:
 2073                                 error = ENOPROTOOPT;
 2074                                 break;
 2075                         }
 2076                         break;
 2077                 }
 2078         }
 2079         return (error);
 2080 }
 2081 
 2082 int
 2083 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
 2084 {
 2085         int error = 0, optval, optlen;
 2086         const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
 2087         struct inpcb *in6p = sotoinpcb(so);
 2088         int level, op, optname;
 2089 
 2090         level = sopt->sopt_level;
 2091         op = sopt->sopt_dir;
 2092         optname = sopt->sopt_name;
 2093         optlen = sopt->sopt_valsize;
 2094 
 2095         if (level != IPPROTO_IPV6) {
 2096                 return (EINVAL);
 2097         }
 2098 
 2099         switch (optname) {
 2100         case IPV6_CHECKSUM:
 2101                 /*
 2102                  * For ICMPv6 sockets, no modification allowed for checksum
 2103                  * offset, permit "no change" values to help existing apps.
 2104                  *
 2105                  * RFC3542 says: "An attempt to set IPV6_CHECKSUM
 2106                  * for an ICMPv6 socket will fail."
 2107                  * The current behavior does not meet RFC3542.
 2108                  */
 2109                 switch (op) {
 2110                 case SOPT_SET:
 2111                         if (optlen != sizeof(int)) {
 2112                                 error = EINVAL;
 2113                                 break;
 2114                         }
 2115                         error = sooptcopyin(sopt, &optval, sizeof(optval),
 2116                                             sizeof(optval));
 2117                         if (error)
 2118                                 break;
 2119                         if ((optval % 2) != 0) {
 2120                                 /* the API assumes even offset values */
 2121                                 error = EINVAL;
 2122                         } else if (so->so_proto->pr_protocol ==
 2123                             IPPROTO_ICMPV6) {
 2124                                 if (optval != icmp6off)
 2125                                         error = EINVAL;
 2126                         } else
 2127                                 in6p->in6p_cksum = optval;
 2128                         break;
 2129 
 2130                 case SOPT_GET:
 2131                         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
 2132                                 optval = icmp6off;
 2133                         else
 2134                                 optval = in6p->in6p_cksum;
 2135 
 2136                         error = sooptcopyout(sopt, &optval, sizeof(optval));
 2137                         break;
 2138 
 2139                 default:
 2140                         error = EINVAL;
 2141                         break;
 2142                 }
 2143                 break;
 2144 
 2145         default:
 2146                 error = ENOPROTOOPT;
 2147                 break;
 2148         }
 2149 
 2150         return (error);
 2151 }
 2152 
 2153 /*
 2154  * Set up IP6 options in pcb for insertion in output packets or
 2155  * specifying behavior of outgoing packets.
 2156  */
 2157 static int
 2158 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
 2159     struct socket *so, struct sockopt *sopt)
 2160 {
 2161         struct ip6_pktopts *opt = *pktopt;
 2162         int error = 0;
 2163         struct thread *td = sopt->sopt_td;
 2164 
 2165         /* turn off any old options. */
 2166         if (opt) {
 2167 #ifdef DIAGNOSTIC
 2168                 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
 2169                     opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
 2170                     opt->ip6po_rhinfo.ip6po_rhi_rthdr)
 2171                         printf("ip6_pcbopts: all specified options are cleared.\n");
 2172 #endif
 2173                 ip6_clearpktopts(opt, -1);
 2174         } else
 2175                 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
 2176         *pktopt = NULL;
 2177 
 2178         if (!m || m->m_len == 0) {
 2179                 /*
 2180                  * Only turning off any previous options, regardless of
 2181                  * whether the opt is just created or given.
 2182                  */
 2183                 free(opt, M_IP6OPT);
 2184                 return (0);
 2185         }
 2186 
 2187         /*  set options specified by user. */
 2188         if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
 2189             td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
 2190                 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
 2191                 free(opt, M_IP6OPT);
 2192                 return (error);
 2193         }
 2194         *pktopt = opt;
 2195         return (0);
 2196 }
 2197 
 2198 /*
 2199  * initialize ip6_pktopts.  beware that there are non-zero default values in
 2200  * the struct.
 2201  */
 2202 void
 2203 ip6_initpktopts(struct ip6_pktopts *opt)
 2204 {
 2205 
 2206         bzero(opt, sizeof(*opt));
 2207         opt->ip6po_hlim = -1;   /* -1 means default hop limit */
 2208         opt->ip6po_tclass = -1; /* -1 means default traffic class */
 2209         opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
 2210         opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
 2211 }
 2212 
 2213 static int
 2214 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
 2215     struct ucred *cred, int uproto)
 2216 {
 2217         struct ip6_pktopts *opt;
 2218 
 2219         if (*pktopt == NULL) {
 2220                 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
 2221                     M_WAITOK);
 2222                 ip6_initpktopts(*pktopt);
 2223         }
 2224         opt = *pktopt;
 2225 
 2226         return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
 2227 }
 2228 
 2229 static int
 2230 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
 2231 {
 2232         void *optdata = NULL;
 2233         int optdatalen = 0;
 2234         struct ip6_ext *ip6e;
 2235         int error = 0;
 2236         struct in6_pktinfo null_pktinfo;
 2237         int deftclass = 0, on;
 2238         int defminmtu = IP6PO_MINMTU_MCASTONLY;
 2239         int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
 2240 
 2241         switch (optname) {
 2242         case IPV6_PKTINFO:
 2243                 if (pktopt && pktopt->ip6po_pktinfo)
 2244                         optdata = (void *)pktopt->ip6po_pktinfo;
 2245                 else {
 2246                         /* XXX: we don't have to do this every time... */
 2247                         bzero(&null_pktinfo, sizeof(null_pktinfo));
 2248                         optdata = (void *)&null_pktinfo;
 2249                 }
 2250                 optdatalen = sizeof(struct in6_pktinfo);
 2251                 break;
 2252         case IPV6_TCLASS:
 2253                 if (pktopt && pktopt->ip6po_tclass >= 0)
 2254                         optdata = (void *)&pktopt->ip6po_tclass;
 2255                 else
 2256                         optdata = (void *)&deftclass;
 2257                 optdatalen = sizeof(int);
 2258                 break;
 2259         case IPV6_HOPOPTS:
 2260                 if (pktopt && pktopt->ip6po_hbh) {
 2261                         optdata = (void *)pktopt->ip6po_hbh;
 2262                         ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
 2263                         optdatalen = (ip6e->ip6e_len + 1) << 3;
 2264                 }
 2265                 break;
 2266         case IPV6_RTHDR:
 2267                 if (pktopt && pktopt->ip6po_rthdr) {
 2268                         optdata = (void *)pktopt->ip6po_rthdr;
 2269                         ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
 2270                         optdatalen = (ip6e->ip6e_len + 1) << 3;
 2271                 }
 2272                 break;
 2273         case IPV6_RTHDRDSTOPTS:
 2274                 if (pktopt && pktopt->ip6po_dest1) {
 2275                         optdata = (void *)pktopt->ip6po_dest1;
 2276                         ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
 2277                         optdatalen = (ip6e->ip6e_len + 1) << 3;
 2278                 }
 2279                 break;
 2280         case IPV6_DSTOPTS:
 2281                 if (pktopt && pktopt->ip6po_dest2) {
 2282                         optdata = (void *)pktopt->ip6po_dest2;
 2283                         ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
 2284                         optdatalen = (ip6e->ip6e_len + 1) << 3;
 2285                 }
 2286                 break;
 2287         case IPV6_NEXTHOP:
 2288                 if (pktopt && pktopt->ip6po_nexthop) {
 2289                         optdata = (void *)pktopt->ip6po_nexthop;
 2290                         optdatalen = pktopt->ip6po_nexthop->sa_len;
 2291                 }
 2292                 break;
 2293         case IPV6_USE_MIN_MTU:
 2294                 if (pktopt)
 2295                         optdata = (void *)&pktopt->ip6po_minmtu;
 2296                 else
 2297                         optdata = (void *)&defminmtu;
 2298                 optdatalen = sizeof(int);
 2299                 break;
 2300         case IPV6_DONTFRAG:
 2301                 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
 2302                         on = 1;
 2303                 else
 2304                         on = 0;
 2305                 optdata = (void *)&on;
 2306                 optdatalen = sizeof(on);
 2307                 break;
 2308         case IPV6_PREFER_TEMPADDR:
 2309                 if (pktopt)
 2310                         optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
 2311                 else
 2312                         optdata = (void *)&defpreftemp;
 2313                 optdatalen = sizeof(int);
 2314                 break;
 2315         default:                /* should not happen */
 2316 #ifdef DIAGNOSTIC
 2317                 panic("ip6_getpcbopt: unexpected option\n");
 2318 #endif
 2319                 return (ENOPROTOOPT);
 2320         }
 2321 
 2322         error = sooptcopyout(sopt, optdata, optdatalen);
 2323 
 2324         return (error);
 2325 }
 2326 
 2327 void
 2328 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
 2329 {
 2330         if (pktopt == NULL)
 2331                 return;
 2332 
 2333         if (optname == -1 || optname == IPV6_PKTINFO) {
 2334                 if (pktopt->ip6po_pktinfo)
 2335                         free(pktopt->ip6po_pktinfo, M_IP6OPT);
 2336                 pktopt->ip6po_pktinfo = NULL;
 2337         }
 2338         if (optname == -1 || optname == IPV6_HOPLIMIT)
 2339                 pktopt->ip6po_hlim = -1;
 2340         if (optname == -1 || optname == IPV6_TCLASS)
 2341                 pktopt->ip6po_tclass = -1;
 2342         if (optname == -1 || optname == IPV6_NEXTHOP) {
 2343                 if (pktopt->ip6po_nextroute.ro_rt) {
 2344                         RTFREE(pktopt->ip6po_nextroute.ro_rt);
 2345                         pktopt->ip6po_nextroute.ro_rt = NULL;
 2346                 }
 2347                 if (pktopt->ip6po_nexthop)
 2348                         free(pktopt->ip6po_nexthop, M_IP6OPT);
 2349                 pktopt->ip6po_nexthop = NULL;
 2350         }
 2351         if (optname == -1 || optname == IPV6_HOPOPTS) {
 2352                 if (pktopt->ip6po_hbh)
 2353                         free(pktopt->ip6po_hbh, M_IP6OPT);
 2354                 pktopt->ip6po_hbh = NULL;
 2355         }
 2356         if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
 2357                 if (pktopt->ip6po_dest1)
 2358                         free(pktopt->ip6po_dest1, M_IP6OPT);
 2359                 pktopt->ip6po_dest1 = NULL;
 2360         }
 2361         if (optname == -1 || optname == IPV6_RTHDR) {
 2362                 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
 2363                         free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
 2364                 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
 2365                 if (pktopt->ip6po_route.ro_rt) {
 2366                         RTFREE(pktopt->ip6po_route.ro_rt);
 2367                         pktopt->ip6po_route.ro_rt = NULL;
 2368                 }
 2369         }
 2370         if (optname == -1 || optname == IPV6_DSTOPTS) {
 2371                 if (pktopt->ip6po_dest2)
 2372                         free(pktopt->ip6po_dest2, M_IP6OPT);
 2373                 pktopt->ip6po_dest2 = NULL;
 2374         }
 2375 }
 2376 
 2377 #define PKTOPT_EXTHDRCPY(type) \
 2378 do {\
 2379         if (src->type) {\
 2380                 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
 2381                 dst->type = malloc(hlen, M_IP6OPT, canwait);\
 2382                 if (dst->type == NULL && canwait == M_NOWAIT)\
 2383                         goto bad;\
 2384                 bcopy(src->type, dst->type, hlen);\
 2385         }\
 2386 } while (/*CONSTCOND*/ 0)
 2387 
 2388 static int
 2389 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
 2390 {
 2391         if (dst == NULL || src == NULL)  {
 2392                 printf("ip6_clearpktopts: invalid argument\n");
 2393                 return (EINVAL);
 2394         }
 2395 
 2396         dst->ip6po_hlim = src->ip6po_hlim;
 2397         dst->ip6po_tclass = src->ip6po_tclass;
 2398         dst->ip6po_flags = src->ip6po_flags;
 2399         dst->ip6po_minmtu = src->ip6po_minmtu;
 2400         dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
 2401         if (src->ip6po_pktinfo) {
 2402                 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
 2403                     M_IP6OPT, canwait);
 2404                 if (dst->ip6po_pktinfo == NULL)
 2405                         goto bad;
 2406                 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
 2407         }
 2408         if (src->ip6po_nexthop) {
 2409                 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
 2410                     M_IP6OPT, canwait);
 2411                 if (dst->ip6po_nexthop == NULL)
 2412                         goto bad;
 2413                 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
 2414                     src->ip6po_nexthop->sa_len);
 2415         }
 2416         PKTOPT_EXTHDRCPY(ip6po_hbh);
 2417         PKTOPT_EXTHDRCPY(ip6po_dest1);
 2418         PKTOPT_EXTHDRCPY(ip6po_dest2);
 2419         PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
 2420         return (0);
 2421 
 2422   bad:
 2423         ip6_clearpktopts(dst, -1);
 2424         return (ENOBUFS);
 2425 }
 2426 #undef PKTOPT_EXTHDRCPY
 2427 
 2428 struct ip6_pktopts *
 2429 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
 2430 {
 2431         int error;
 2432         struct ip6_pktopts *dst;
 2433 
 2434         dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
 2435         if (dst == NULL)
 2436                 return (NULL);
 2437         ip6_initpktopts(dst);
 2438 
 2439         if ((error = copypktopts(dst, src, canwait)) != 0) {
 2440                 free(dst, M_IP6OPT);
 2441                 return (NULL);
 2442         }
 2443 
 2444         return (dst);
 2445 }
 2446 
 2447 void
 2448 ip6_freepcbopts(struct ip6_pktopts *pktopt)
 2449 {
 2450         if (pktopt == NULL)
 2451                 return;
 2452 
 2453         ip6_clearpktopts(pktopt, -1);
 2454 
 2455         free(pktopt, M_IP6OPT);
 2456 }
 2457 
 2458 /*
 2459  * Set IPv6 outgoing packet options based on advanced API.
 2460  */
 2461 int
 2462 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
 2463     struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
 2464 {
 2465         struct cmsghdr *cm = 0;
 2466 
 2467         if (control == NULL || opt == NULL)
 2468                 return (EINVAL);
 2469 
 2470         ip6_initpktopts(opt);
 2471         if (stickyopt) {
 2472                 int error;
 2473 
 2474                 /*
 2475                  * If stickyopt is provided, make a local copy of the options
 2476                  * for this particular packet, then override them by ancillary
 2477                  * objects.
 2478                  * XXX: copypktopts() does not copy the cached route to a next
 2479                  * hop (if any).  This is not very good in terms of efficiency,
 2480                  * but we can allow this since this option should be rarely
 2481                  * used.
 2482                  */
 2483                 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
 2484                         return (error);
 2485         }
 2486 
 2487         /*
 2488          * XXX: Currently, we assume all the optional information is stored
 2489          * in a single mbuf.
 2490          */
 2491         if (control->m_next)
 2492                 return (EINVAL);
 2493 
 2494         for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
 2495             control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
 2496                 int error;
 2497 
 2498                 if (control->m_len < CMSG_LEN(0))
 2499                         return (EINVAL);
 2500 
 2501                 cm = mtod(control, struct cmsghdr *);
 2502                 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
 2503                         return (EINVAL);
 2504                 if (cm->cmsg_level != IPPROTO_IPV6)
 2505                         continue;
 2506 
 2507                 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
 2508                     cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
 2509                 if (error)
 2510                         return (error);
 2511         }
 2512 
 2513         return (0);
 2514 }
 2515 
 2516 /*
 2517  * Set a particular packet option, as a sticky option or an ancillary data
 2518  * item.  "len" can be 0 only when it's a sticky option.
 2519  * We have 4 cases of combination of "sticky" and "cmsg":
 2520  * "sticky=0, cmsg=0": impossible
 2521  * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
 2522  * "sticky=1, cmsg=0": RFC3542 socket option
 2523  * "sticky=1, cmsg=1": RFC2292 socket option
 2524  */
 2525 static int
 2526 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
 2527     struct ucred *cred, int sticky, int cmsg, int uproto)
 2528 {
 2529         int minmtupolicy, preftemp;
 2530         int error;
 2531 
 2532         if (!sticky && !cmsg) {
 2533 #ifdef DIAGNOSTIC
 2534                 printf("ip6_setpktopt: impossible case\n");
 2535 #endif
 2536                 return (EINVAL);
 2537         }
 2538 
 2539         /*
 2540          * IPV6_2292xxx is for backward compatibility to RFC2292, and should
 2541          * not be specified in the context of RFC3542.  Conversely,
 2542          * RFC3542 types should not be specified in the context of RFC2292.
 2543          */
 2544         if (!cmsg) {
 2545                 switch (optname) {
 2546                 case IPV6_2292PKTINFO:
 2547                 case IPV6_2292HOPLIMIT:
 2548                 case IPV6_2292NEXTHOP:
 2549                 case IPV6_2292HOPOPTS:
 2550                 case IPV6_2292DSTOPTS:
 2551                 case IPV6_2292RTHDR:
 2552                 case IPV6_2292PKTOPTIONS:
 2553                         return (ENOPROTOOPT);
 2554                 }
 2555         }
 2556         if (sticky && cmsg) {
 2557                 switch (optname) {
 2558                 case IPV6_PKTINFO:
 2559                 case IPV6_HOPLIMIT:
 2560                 case IPV6_NEXTHOP:
 2561                 case IPV6_HOPOPTS:
 2562                 case IPV6_DSTOPTS:
 2563                 case IPV6_RTHDRDSTOPTS:
 2564                 case IPV6_RTHDR:
 2565                 case IPV6_USE_MIN_MTU:
 2566                 case IPV6_DONTFRAG:
 2567                 case IPV6_TCLASS:
 2568                 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
 2569                         return (ENOPROTOOPT);
 2570                 }
 2571         }
 2572 
 2573         switch (optname) {
 2574         case IPV6_2292PKTINFO:
 2575         case IPV6_PKTINFO:
 2576         {
 2577                 struct ifnet *ifp = NULL;
 2578                 struct in6_pktinfo *pktinfo;
 2579 
 2580                 if (len != sizeof(struct in6_pktinfo))
 2581                         return (EINVAL);
 2582 
 2583                 pktinfo = (struct in6_pktinfo *)buf;
 2584 
 2585                 /*
 2586                  * An application can clear any sticky IPV6_PKTINFO option by
 2587                  * doing a "regular" setsockopt with ipi6_addr being
 2588                  * in6addr_any and ipi6_ifindex being zero.
 2589                  * [RFC 3542, Section 6]
 2590                  */
 2591                 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
 2592                     pktinfo->ipi6_ifindex == 0 &&
 2593                     IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
 2594                         ip6_clearpktopts(opt, optname);
 2595                         break;
 2596                 }
 2597 
 2598                 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
 2599                     sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
 2600                         return (EINVAL);
 2601                 }
 2602 
 2603                 /* validate the interface index if specified. */
 2604                 if (pktinfo->ipi6_ifindex > V_if_index ||
 2605                     pktinfo->ipi6_ifindex < 0) {
 2606                          return (ENXIO);
 2607                 }
 2608                 if (pktinfo->ipi6_ifindex) {
 2609                         ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
 2610                         if (ifp == NULL)
 2611                                 return (ENXIO);
 2612                 }
 2613 
 2614                 /*
 2615                  * We store the address anyway, and let in6_selectsrc()
 2616                  * validate the specified address.  This is because ipi6_addr
 2617                  * may not have enough information about its scope zone, and
 2618                  * we may need additional information (such as outgoing
 2619                  * interface or the scope zone of a destination address) to
 2620                  * disambiguate the scope.
 2621                  * XXX: the delay of the validation may confuse the
 2622                  * application when it is used as a sticky option.
 2623                  */
 2624                 if (opt->ip6po_pktinfo == NULL) {
 2625                         opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
 2626                             M_IP6OPT, M_NOWAIT);
 2627                         if (opt->ip6po_pktinfo == NULL)
 2628                                 return (ENOBUFS);
 2629                 }
 2630                 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
 2631                 break;
 2632         }
 2633 
 2634         case IPV6_2292HOPLIMIT:
 2635         case IPV6_HOPLIMIT:
 2636         {
 2637                 int *hlimp;
 2638 
 2639                 /*
 2640                  * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
 2641                  * to simplify the ordering among hoplimit options.
 2642                  */
 2643                 if (optname == IPV6_HOPLIMIT && sticky)
 2644                         return (ENOPROTOOPT);
 2645 
 2646                 if (len != sizeof(int))
 2647                         return (EINVAL);
 2648                 hlimp = (int *)buf;
 2649                 if (*hlimp < -1 || *hlimp > 255)
 2650                         return (EINVAL);
 2651 
 2652                 opt->ip6po_hlim = *hlimp;
 2653                 break;
 2654         }
 2655 
 2656         case IPV6_TCLASS:
 2657         {
 2658                 int tclass;
 2659 
 2660                 if (len != sizeof(int))
 2661                         return (EINVAL);
 2662                 tclass = *(int *)buf;
 2663                 if (tclass < -1 || tclass > 255)
 2664                         return (EINVAL);
 2665 
 2666                 opt->ip6po_tclass = tclass;
 2667                 break;
 2668         }
 2669 
 2670         case IPV6_2292NEXTHOP:
 2671         case IPV6_NEXTHOP:
 2672                 if (cred != NULL) {
 2673                         error = priv_check_cred(cred,
 2674                             PRIV_NETINET_SETHDROPTS, 0);
 2675                         if (error)
 2676                                 return (error);
 2677                 }
 2678 
 2679                 if (len == 0) { /* just remove the option */
 2680                         ip6_clearpktopts(opt, IPV6_NEXTHOP);
 2681                         break;
 2682                 }
 2683 
 2684                 /* check if cmsg_len is large enough for sa_len */
 2685                 if (len < sizeof(struct sockaddr) || len < *buf)
 2686                         return (EINVAL);
 2687 
 2688                 switch (((struct sockaddr *)buf)->sa_family) {
 2689                 case AF_INET6:
 2690                 {
 2691                         struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
 2692                         int error;
 2693 
 2694                         if (sa6->sin6_len != sizeof(struct sockaddr_in6))
 2695                                 return (EINVAL);
 2696 
 2697                         if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
 2698                             IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
 2699                                 return (EINVAL);
 2700                         }
 2701                         if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
 2702                             != 0) {
 2703                                 return (error);
 2704                         }
 2705                         break;
 2706                 }
 2707                 case AF_LINK:   /* should eventually be supported */
 2708                 default:
 2709                         return (EAFNOSUPPORT);
 2710                 }
 2711 
 2712                 /* turn off the previous option, then set the new option. */
 2713                 ip6_clearpktopts(opt, IPV6_NEXTHOP);
 2714                 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
 2715                 if (opt->ip6po_nexthop == NULL)
 2716                         return (ENOBUFS);
 2717                 bcopy(buf, opt->ip6po_nexthop, *buf);
 2718                 break;
 2719 
 2720         case IPV6_2292HOPOPTS:
 2721         case IPV6_HOPOPTS:
 2722         {
 2723                 struct ip6_hbh *hbh;
 2724                 int hbhlen;
 2725 
 2726                 /*
 2727                  * XXX: We don't allow a non-privileged user to set ANY HbH
 2728                  * options, since per-option restriction has too much
 2729                  * overhead.
 2730                  */
 2731                 if (cred != NULL) {
 2732                         error = priv_check_cred(cred,
 2733                             PRIV_NETINET_SETHDROPTS, 0);
 2734                         if (error)
 2735                                 return (error);
 2736                 }
 2737 
 2738                 if (len == 0) {
 2739                         ip6_clearpktopts(opt, IPV6_HOPOPTS);
 2740                         break;  /* just remove the option */
 2741                 }
 2742 
 2743                 /* message length validation */
 2744                 if (len < sizeof(struct ip6_hbh))
 2745                         return (EINVAL);
 2746                 hbh = (struct ip6_hbh *)buf;
 2747                 hbhlen = (hbh->ip6h_len + 1) << 3;
 2748                 if (len != hbhlen)
 2749                         return (EINVAL);
 2750 
 2751                 /* turn off the previous option, then set the new option. */
 2752                 ip6_clearpktopts(opt, IPV6_HOPOPTS);
 2753                 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
 2754                 if (opt->ip6po_hbh == NULL)
 2755                         return (ENOBUFS);
 2756                 bcopy(hbh, opt->ip6po_hbh, hbhlen);
 2757 
 2758                 break;
 2759         }
 2760 
 2761         case IPV6_2292DSTOPTS:
 2762         case IPV6_DSTOPTS:
 2763         case IPV6_RTHDRDSTOPTS:
 2764         {
 2765                 struct ip6_dest *dest, **newdest = NULL;
 2766                 int destlen;
 2767 
 2768                 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
 2769                         error = priv_check_cred(cred,
 2770                             PRIV_NETINET_SETHDROPTS, 0);
 2771                         if (error)
 2772                                 return (error);
 2773                 }
 2774 
 2775                 if (len == 0) {
 2776                         ip6_clearpktopts(opt, optname);
 2777                         break;  /* just remove the option */
 2778                 }
 2779 
 2780                 /* message length validation */
 2781                 if (len < sizeof(struct ip6_dest))
 2782                         return (EINVAL);
 2783                 dest = (struct ip6_dest *)buf;
 2784                 destlen = (dest->ip6d_len + 1) << 3;
 2785                 if (len != destlen)
 2786                         return (EINVAL);
 2787 
 2788                 /*
 2789                  * Determine the position that the destination options header
 2790                  * should be inserted; before or after the routing header.
 2791                  */
 2792                 switch (optname) {
 2793                 case IPV6_2292DSTOPTS:
 2794                         /*
 2795                          * The old advacned API is ambiguous on this point.
 2796                          * Our approach is to determine the position based
 2797                          * according to the existence of a routing header.
 2798                          * Note, however, that this depends on the order of the
 2799                          * extension headers in the ancillary data; the 1st
 2800                          * part of the destination options header must appear
 2801                          * before the routing header in the ancillary data,
 2802                          * too.
 2803                          * RFC3542 solved the ambiguity by introducing
 2804                          * separate ancillary data or option types.
 2805                          */
 2806                         if (opt->ip6po_rthdr == NULL)
 2807                                 newdest = &opt->ip6po_dest1;
 2808                         else
 2809                                 newdest = &opt->ip6po_dest2;
 2810                         break;
 2811                 case IPV6_RTHDRDSTOPTS:
 2812                         newdest = &opt->ip6po_dest1;
 2813                         break;
 2814                 case IPV6_DSTOPTS:
 2815                         newdest = &opt->ip6po_dest2;
 2816                         break;
 2817                 }
 2818 
 2819                 /* turn off the previous option, then set the new option. */
 2820                 ip6_clearpktopts(opt, optname);
 2821                 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
 2822                 if (*newdest == NULL)
 2823                         return (ENOBUFS);
 2824                 bcopy(dest, *newdest, destlen);
 2825 
 2826                 break;
 2827         }
 2828 
 2829         case IPV6_2292RTHDR:
 2830         case IPV6_RTHDR:
 2831         {
 2832                 struct ip6_rthdr *rth;
 2833                 int rthlen;
 2834 
 2835                 if (len == 0) {
 2836                         ip6_clearpktopts(opt, IPV6_RTHDR);
 2837                         break;  /* just remove the option */
 2838                 }
 2839 
 2840                 /* message length validation */
 2841                 if (len < sizeof(struct ip6_rthdr))
 2842                         return (EINVAL);
 2843                 rth = (struct ip6_rthdr *)buf;
 2844                 rthlen = (rth->ip6r_len + 1) << 3;
 2845                 if (len != rthlen)
 2846                         return (EINVAL);
 2847 
 2848                 switch (rth->ip6r_type) {
 2849                 case IPV6_RTHDR_TYPE_0:
 2850                         if (rth->ip6r_len == 0) /* must contain one addr */
 2851                                 return (EINVAL);
 2852                         if (rth->ip6r_len % 2) /* length must be even */
 2853                                 return (EINVAL);
 2854                         if (rth->ip6r_len / 2 != rth->ip6r_segleft)
 2855                                 return (EINVAL);
 2856                         break;
 2857                 default:
 2858                         return (EINVAL);        /* not supported */
 2859                 }
 2860 
 2861                 /* turn off the previous option */
 2862                 ip6_clearpktopts(opt, IPV6_RTHDR);
 2863                 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
 2864                 if (opt->ip6po_rthdr == NULL)
 2865                         return (ENOBUFS);
 2866                 bcopy(rth, opt->ip6po_rthdr, rthlen);
 2867 
 2868                 break;
 2869         }
 2870 
 2871         case IPV6_USE_MIN_MTU:
 2872                 if (len != sizeof(int))
 2873                         return (EINVAL);
 2874                 minmtupolicy = *(int *)buf;
 2875                 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
 2876                     minmtupolicy != IP6PO_MINMTU_DISABLE &&
 2877                     minmtupolicy != IP6PO_MINMTU_ALL) {
 2878                         return (EINVAL);
 2879                 }
 2880                 opt->ip6po_minmtu = minmtupolicy;
 2881                 break;
 2882 
 2883         case IPV6_DONTFRAG:
 2884                 if (len != sizeof(int))
 2885                         return (EINVAL);
 2886 
 2887                 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
 2888                         /*
 2889                          * we ignore this option for TCP sockets.
 2890                          * (RFC3542 leaves this case unspecified.)
 2891                          */
 2892                         opt->ip6po_flags &= ~IP6PO_DONTFRAG;
 2893                 } else
 2894                         opt->ip6po_flags |= IP6PO_DONTFRAG;
 2895                 break;
 2896 
 2897         case IPV6_PREFER_TEMPADDR:
 2898                 if (len != sizeof(int))
 2899                         return (EINVAL);
 2900                 preftemp = *(int *)buf;
 2901                 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
 2902                     preftemp != IP6PO_TEMPADDR_NOTPREFER &&
 2903                     preftemp != IP6PO_TEMPADDR_PREFER) {
 2904                         return (EINVAL);
 2905                 }
 2906                 opt->ip6po_prefer_tempaddr = preftemp;
 2907                 break;
 2908 
 2909         default:
 2910                 return (ENOPROTOOPT);
 2911         } /* end of switch */
 2912 
 2913         return (0);
 2914 }
 2915 
 2916 /*
 2917  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
 2918  * packet to the input queue of a specified interface.  Note that this
 2919  * calls the output routine of the loopback "driver", but with an interface
 2920  * pointer that might NOT be &loif -- easier than replicating that code here.
 2921  */
 2922 void
 2923 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
 2924 {
 2925         struct mbuf *copym;
 2926         struct ip6_hdr *ip6;
 2927 
 2928         copym = m_copy(m, 0, M_COPYALL);
 2929         if (copym == NULL)
 2930                 return;
 2931 
 2932         /*
 2933          * Make sure to deep-copy IPv6 header portion in case the data
 2934          * is in an mbuf cluster, so that we can safely override the IPv6
 2935          * header portion later.
 2936          */
 2937         if ((copym->m_flags & M_EXT) != 0 ||
 2938             copym->m_len < sizeof(struct ip6_hdr)) {
 2939                 copym = m_pullup(copym, sizeof(struct ip6_hdr));
 2940                 if (copym == NULL)
 2941                         return;
 2942         }
 2943 
 2944 #ifdef DIAGNOSTIC
 2945         if (copym->m_len < sizeof(*ip6)) {
 2946                 m_freem(copym);
 2947                 return;
 2948         }
 2949 #endif
 2950 
 2951         ip6 = mtod(copym, struct ip6_hdr *);
 2952         /*
 2953          * clear embedded scope identifiers if necessary.
 2954          * in6_clearscope will touch the addresses only when necessary.
 2955          */
 2956         in6_clearscope(&ip6->ip6_src);
 2957         in6_clearscope(&ip6->ip6_dst);
 2958 
 2959         (void)if_simloop(ifp, copym, dst->sin6_family, 0);
 2960 }
 2961 
 2962 /*
 2963  * Chop IPv6 header off from the payload.
 2964  */
 2965 static int
 2966 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
 2967 {
 2968         struct mbuf *mh;
 2969         struct ip6_hdr *ip6;
 2970 
 2971         ip6 = mtod(m, struct ip6_hdr *);
 2972         if (m->m_len > sizeof(*ip6)) {
 2973                 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
 2974                 if (mh == 0) {
 2975                         m_freem(m);
 2976                         return ENOBUFS;
 2977                 }
 2978                 M_MOVE_PKTHDR(mh, m);
 2979                 MH_ALIGN(mh, sizeof(*ip6));
 2980                 m->m_len -= sizeof(*ip6);
 2981                 m->m_data += sizeof(*ip6);
 2982                 mh->m_next = m;
 2983                 m = mh;
 2984                 m->m_len = sizeof(*ip6);
 2985                 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
 2986         }
 2987         exthdrs->ip6e_ip6 = m;
 2988         return 0;
 2989 }
 2990 
 2991 /*
 2992  * Compute IPv6 extension header length.
 2993  */
 2994 int
 2995 ip6_optlen(struct inpcb *in6p)
 2996 {
 2997         int len;
 2998 
 2999         if (!in6p->in6p_outputopts)
 3000                 return 0;
 3001 
 3002         len = 0;
 3003 #define elen(x) \
 3004     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
 3005 
 3006         len += elen(in6p->in6p_outputopts->ip6po_hbh);
 3007         if (in6p->in6p_outputopts->ip6po_rthdr)
 3008                 /* dest1 is valid with rthdr only */
 3009                 len += elen(in6p->in6p_outputopts->ip6po_dest1);
 3010         len += elen(in6p->in6p_outputopts->ip6po_rthdr);
 3011         len += elen(in6p->in6p_outputopts->ip6po_dest2);
 3012         return len;
 3013 #undef elen
 3014 }

Cache object: 4f6c9a778d66b5d3d2f08ba4cf726a40


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