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

Cache object: bd3c88265ea7b8ff67a7f3d7db62e939


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