The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/ip6_output.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 292baea532bfc67452e19878eaa0e394


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