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

Cache object: e58a34baef4945427e3092f6d3c35610


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