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