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

Cache object: 393395bc1bea5d1d366d3a1d76d75827


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