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

Cache object: 7a618bf2316cd2e6586528b8d0237b53


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