Home | History | Annotate | Line # | Download | only in netinet6
raw_ip6.c revision 1.22.2.1
      1  1.22.2.1  minoura /*	$NetBSD: raw_ip6.c,v 1.22.2.1 2000/06/22 17:10:05 minoura Exp $	*/
      2  1.22.2.1  minoura /*	$KAME: raw_ip6.c,v 1.28 2000/05/28 23:25:07 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.2   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.2   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, 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.2   itojun  * 3. All advertising materials mentioning features or use of this software
     46       1.2   itojun  *    must display the following acknowledgement:
     47       1.2   itojun  *	This product includes software developed by the University of
     48       1.2   itojun  *	California, Berkeley and its contributors.
     49       1.2   itojun  * 4. Neither the name of the University nor the names of its contributors
     50       1.2   itojun  *    may be used to endorse or promote products derived from this software
     51       1.2   itojun  *    without specific prior written permission.
     52       1.2   itojun  *
     53       1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54       1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55       1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56       1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57       1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58       1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59       1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60       1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61       1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62       1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63       1.2   itojun  * SUCH DAMAGE.
     64       1.2   itojun  *
     65       1.2   itojun  *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
     66       1.2   itojun  */
     67       1.6  thorpej 
     68       1.6  thorpej #include "opt_ipsec.h"
     69       1.2   itojun 
     70       1.2   itojun #include <sys/param.h>
     71       1.2   itojun #include <sys/malloc.h>
     72       1.2   itojun #include <sys/mbuf.h>
     73       1.2   itojun #include <sys/socket.h>
     74       1.2   itojun #include <sys/protosw.h>
     75       1.2   itojun #include <sys/socketvar.h>
     76       1.2   itojun #include <sys/errno.h>
     77       1.2   itojun #include <sys/systm.h>
     78       1.2   itojun #include <sys/proc.h>
     79       1.2   itojun 
     80       1.2   itojun #include <net/if.h>
     81       1.2   itojun #include <net/route.h>
     82       1.2   itojun #include <net/if_types.h>
     83       1.2   itojun 
     84       1.2   itojun #include <netinet/in.h>
     85       1.2   itojun #include <netinet/in_var.h>
     86      1.19   itojun #include <netinet/ip6.h>
     87       1.2   itojun #include <netinet6/ip6_var.h>
     88       1.2   itojun #include <netinet6/ip6_mroute.h>
     89      1.19   itojun #include <netinet/icmp6.h>
     90       1.2   itojun #include <netinet6/in6_pcb.h>
     91       1.2   itojun #include <netinet6/nd6.h>
     92      1.20   itojun #include <netinet6/ip6protosw.h>
     93       1.2   itojun 
     94       1.2   itojun #ifdef IPSEC
     95       1.2   itojun #include <netinet6/ipsec.h>
     96       1.2   itojun #endif /*IPSEC*/
     97       1.2   itojun 
     98       1.2   itojun #include <machine/stdarg.h>
     99       1.2   itojun 
    100       1.2   itojun #include "faith.h"
    101       1.2   itojun 
    102       1.2   itojun struct	in6pcb rawin6pcb;
    103       1.2   itojun #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
    104       1.2   itojun 
    105       1.2   itojun /*
    106       1.2   itojun  * Raw interface to IP6 protocol.
    107       1.2   itojun  */
    108       1.2   itojun 
    109       1.2   itojun /*
    110       1.2   itojun  * Initialize raw connection block queue.
    111       1.2   itojun  */
    112       1.2   itojun void
    113       1.2   itojun rip6_init()
    114       1.2   itojun {
    115       1.2   itojun 	rawin6pcb.in6p_next = rawin6pcb.in6p_prev = &rawin6pcb;
    116       1.2   itojun }
    117       1.2   itojun 
    118       1.2   itojun /*
    119       1.2   itojun  * Setup generic address and protocol structures
    120       1.2   itojun  * for raw_input routine, then pass them along with
    121       1.2   itojun  * mbuf chain.
    122       1.2   itojun  */
    123       1.2   itojun int
    124       1.2   itojun rip6_input(mp, offp, proto)
    125       1.2   itojun 	struct	mbuf **mp;
    126       1.2   itojun 	int	*offp, proto;
    127       1.2   itojun {
    128       1.2   itojun 	struct mbuf *m = *mp;
    129       1.2   itojun 	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    130       1.2   itojun 	register struct in6pcb *in6p;
    131       1.2   itojun 	struct in6pcb *last = NULL;
    132       1.2   itojun 	struct sockaddr_in6 rip6src;
    133       1.2   itojun 	struct mbuf *opts = NULL;
    134       1.2   itojun 
    135       1.2   itojun #if defined(NFAITH) && 0 < NFAITH
    136       1.2   itojun 	if (m->m_pkthdr.rcvif) {
    137       1.2   itojun 		if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
    138       1.2   itojun 			/* send icmp6 host unreach? */
    139       1.2   itojun 			m_freem(m);
    140       1.2   itojun 			return IPPROTO_DONE;
    141       1.2   itojun 		}
    142       1.2   itojun 	}
    143       1.2   itojun #endif
    144      1.14   itojun 
    145      1.14   itojun 	/* Be proactive about malicious use of IPv4 mapped address */
    146      1.14   itojun 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    147      1.14   itojun 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    148      1.14   itojun 		/* XXX stat */
    149      1.14   itojun 		m_freem(m);
    150      1.14   itojun 		return IPPROTO_DONE;
    151      1.14   itojun 	}
    152      1.14   itojun 
    153       1.2   itojun 	bzero(&rip6src, sizeof(rip6src));
    154       1.2   itojun 	rip6src.sin6_len = sizeof(struct sockaddr_in6);
    155       1.2   itojun 	rip6src.sin6_family = AF_INET6;
    156       1.2   itojun 	rip6src.sin6_addr = ip6->ip6_src;
    157       1.2   itojun 	if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
    158       1.2   itojun 		rip6src.sin6_addr.s6_addr16[1] = 0;
    159       1.2   itojun 	if (m->m_pkthdr.rcvif) {
    160       1.2   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
    161       1.2   itojun 			rip6src.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
    162       1.2   itojun 		else
    163       1.2   itojun 			rip6src.sin6_scope_id = 0;
    164       1.2   itojun 	} else
    165       1.2   itojun 		rip6src.sin6_scope_id = 0;
    166       1.2   itojun 
    167       1.2   itojun 	for (in6p = rawin6pcb.in6p_next;
    168       1.2   itojun 	     in6p != &rawin6pcb; in6p = in6p->in6p_next) {
    169       1.2   itojun 		if (in6p->in6p_ip6.ip6_nxt &&
    170       1.2   itojun 		    in6p->in6p_ip6.ip6_nxt != proto)
    171       1.2   itojun 			continue;
    172      1.12   itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
    173       1.2   itojun 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
    174       1.2   itojun 			continue;
    175      1.12   itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
    176       1.2   itojun 		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
    177       1.2   itojun 			continue;
    178       1.2   itojun 		if (in6p->in6p_cksum != -1
    179       1.7   itojun 		 && in6_cksum(m, ip6->ip6_nxt, *offp, m->m_pkthdr.len - *offp))
    180       1.7   itojun 		{
    181       1.2   itojun 			/* XXX bark something */
    182       1.2   itojun 			continue;
    183       1.2   itojun 		}
    184       1.2   itojun 		if (last) {
    185       1.2   itojun 			struct	mbuf *n;
    186       1.2   itojun 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    187       1.2   itojun 				if (last->in6p_flags & IN6P_CONTROLOPTS)
    188       1.2   itojun 					ip6_savecontrol(last, &opts, ip6, n);
    189       1.2   itojun 				/* strip intermediate headers */
    190       1.2   itojun 				m_adj(n, *offp);
    191       1.2   itojun 				if (sbappendaddr(&last->in6p_socket->so_rcv,
    192       1.2   itojun 						(struct sockaddr *)&rip6src,
    193       1.2   itojun 						 n, opts) == 0) {
    194       1.2   itojun 					/* should notify about lost packet */
    195       1.2   itojun 					m_freem(n);
    196       1.2   itojun 					if (opts)
    197       1.2   itojun 						m_freem(opts);
    198       1.2   itojun 				} else
    199       1.2   itojun 					sorwakeup(last->in6p_socket);
    200       1.2   itojun 				opts = NULL;
    201       1.2   itojun 			}
    202       1.2   itojun 		}
    203       1.2   itojun 		last = in6p;
    204       1.2   itojun 	}
    205       1.2   itojun 	if (last) {
    206       1.2   itojun 		if (last->in6p_flags & IN6P_CONTROLOPTS)
    207       1.2   itojun 			ip6_savecontrol(last, &opts, ip6, m);
    208       1.2   itojun 		/* strip intermediate headers */
    209       1.2   itojun 		m_adj(m, *offp);
    210       1.2   itojun 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    211       1.2   itojun 				(struct sockaddr *)&rip6src, m, opts) == 0) {
    212       1.2   itojun 			m_freem(m);
    213       1.2   itojun 			if (opts)
    214       1.2   itojun 				m_freem(opts);
    215       1.2   itojun 		} else
    216       1.2   itojun 			sorwakeup(last->in6p_socket);
    217       1.2   itojun 	} else {
    218       1.2   itojun 		if (proto == IPPROTO_NONE)
    219       1.2   itojun 			m_freem(m);
    220       1.2   itojun 		else {
    221       1.2   itojun 			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
    222       1.2   itojun 			icmp6_error(m, ICMP6_PARAM_PROB,
    223       1.2   itojun 				    ICMP6_PARAMPROB_NEXTHEADER,
    224       1.2   itojun 				    prvnxtp - mtod(m, char *));
    225       1.2   itojun 		}
    226       1.2   itojun 		ip6stat.ip6s_delivered--;
    227       1.2   itojun 	}
    228       1.2   itojun 	return IPPROTO_DONE;
    229       1.2   itojun }
    230       1.2   itojun 
    231      1.20   itojun void
    232      1.20   itojun rip6_ctlinput(cmd, sa, d)
    233      1.20   itojun 	int cmd;
    234      1.20   itojun 	struct sockaddr *sa;
    235      1.20   itojun 	void *d;
    236      1.20   itojun {
    237      1.20   itojun 	struct sockaddr_in6 sa6;
    238      1.20   itojun 	register struct ip6_hdr *ip6;
    239      1.20   itojun 	struct mbuf *m;
    240      1.20   itojun 	int off;
    241      1.21   itojun 	void (*notify) __P((struct in6pcb *, int)) = in6_rtchange;
    242      1.20   itojun 
    243      1.20   itojun 	if (sa->sa_family != AF_INET6 ||
    244      1.20   itojun 	    sa->sa_len != sizeof(struct sockaddr_in6))
    245      1.20   itojun 		return;
    246      1.20   itojun 
    247      1.21   itojun 	if ((unsigned)cmd >= PRC_NCMDS)
    248      1.21   itojun 		return;
    249      1.21   itojun 	if (PRC_IS_REDIRECT(cmd))
    250      1.21   itojun 		notify = in6_rtchange, d = NULL;
    251      1.21   itojun 	else if (cmd == PRC_HOSTDEAD)
    252      1.21   itojun 		d = NULL;
    253      1.21   itojun 	else if (inet6ctlerrmap[cmd] == 0)
    254      1.20   itojun 		return;
    255      1.20   itojun 
    256      1.20   itojun 	/* if the parameter is from icmp6, decode it. */
    257      1.20   itojun 	if (d != NULL) {
    258      1.20   itojun 		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
    259      1.20   itojun 		m = ip6cp->ip6c_m;
    260      1.20   itojun 		ip6 = ip6cp->ip6c_ip6;
    261      1.20   itojun 		off = ip6cp->ip6c_off;
    262      1.20   itojun 	} else {
    263      1.20   itojun 		m = NULL;
    264      1.20   itojun 		ip6 = NULL;
    265      1.20   itojun 	}
    266      1.20   itojun 
    267      1.20   itojun 	/* translate addresses into internal form */
    268      1.20   itojun 	sa6 = *(struct sockaddr_in6 *)sa;
    269      1.20   itojun 	if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) && m && m->m_pkthdr.rcvif)
    270      1.20   itojun 		sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
    271      1.20   itojun 
    272      1.20   itojun 	if (ip6) {
    273      1.20   itojun 		/*
    274      1.20   itojun 		 * XXX: We assume that when IPV6 is non NULL,
    275      1.20   itojun 		 * M and OFF are valid.
    276      1.20   itojun 		 */
    277      1.20   itojun 		struct in6_addr s;
    278      1.20   itojun 
    279      1.20   itojun 		/* translate addresses into internal form */
    280      1.20   itojun 		memcpy(&s, &ip6->ip6_src, sizeof(s));
    281      1.20   itojun 		if (IN6_IS_ADDR_LINKLOCAL(&s))
    282      1.20   itojun 			s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
    283      1.20   itojun 
    284      1.20   itojun 		(void) in6_pcbnotify(&rawin6pcb, (struct sockaddr *)&sa6,
    285      1.21   itojun 					0, &s, 0, cmd, notify);
    286      1.20   itojun 	} else {
    287      1.20   itojun 		(void) in6_pcbnotify(&rawin6pcb, (struct sockaddr *)&sa6, 0,
    288      1.21   itojun 					&zeroin6_addr, 0, cmd, notify);
    289      1.20   itojun 	}
    290      1.20   itojun }
    291      1.20   itojun 
    292       1.2   itojun /*
    293       1.2   itojun  * Generate IPv6 header and pass packet to ip6_output.
    294       1.2   itojun  * Tack on options user may have setup with control call.
    295       1.2   itojun  */
    296       1.2   itojun int
    297       1.2   itojun #if __STDC__
    298       1.2   itojun rip6_output(struct mbuf *m, ...)
    299       1.2   itojun #else
    300       1.2   itojun rip6_output(m, va_alist)
    301       1.2   itojun 	struct mbuf *m;
    302       1.2   itojun 	va_dcl
    303       1.2   itojun #endif
    304       1.2   itojun {
    305       1.2   itojun 	struct socket *so;
    306       1.2   itojun 	struct sockaddr_in6 *dstsock;
    307       1.2   itojun 	struct mbuf *control;
    308       1.2   itojun 	struct in6_addr *dst;
    309       1.2   itojun 	struct ip6_hdr *ip6;
    310       1.2   itojun 	struct in6pcb *in6p;
    311       1.2   itojun 	u_int	plen = m->m_pkthdr.len;
    312       1.2   itojun 	int error = 0;
    313       1.2   itojun 	struct ip6_pktopts opt, *optp = NULL;
    314       1.2   itojun 	struct ifnet *oifp = NULL;
    315      1.12   itojun 	int type, code;		/* for ICMPv6 output statistics only */
    316      1.20   itojun 	int priv = 0;
    317       1.2   itojun 	va_list ap;
    318       1.2   itojun 
    319       1.2   itojun 	va_start(ap, m);
    320       1.2   itojun 	so = va_arg(ap, struct socket *);
    321       1.2   itojun 	dstsock = va_arg(ap, struct sockaddr_in6 *);
    322       1.2   itojun 	control = va_arg(ap, struct mbuf *);
    323       1.2   itojun 	va_end(ap);
    324       1.2   itojun 
    325       1.2   itojun 	in6p = sotoin6pcb(so);
    326       1.2   itojun 
    327      1.12   itojun 	priv = 0;
    328      1.12   itojun     {
    329      1.12   itojun 	struct proc *p = curproc;	/* XXX */
    330      1.12   itojun 
    331      1.12   itojun 	if (p && !suser(p->p_ucred, &p->p_acflag))
    332      1.12   itojun 		priv = 1;
    333      1.12   itojun     }
    334       1.2   itojun 	dst = &dstsock->sin6_addr;
    335       1.2   itojun 	if (control) {
    336       1.2   itojun 		if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
    337       1.2   itojun 			goto bad;
    338       1.2   itojun 		optp = &opt;
    339       1.2   itojun 	} else
    340       1.2   itojun 		optp = in6p->in6p_outputopts;
    341       1.2   itojun 
    342      1.12   itojun 	/*
    343      1.12   itojun 	 * For an ICMPv6 packet, we should know its type and code
    344      1.12   itojun 	 * to update statistics.
    345      1.12   itojun 	 */
    346      1.12   itojun 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    347      1.12   itojun 		struct icmp6_hdr *icmp6;
    348      1.12   itojun 		if (m->m_len < sizeof(struct icmp6_hdr) &&
    349      1.12   itojun 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
    350      1.12   itojun 			error = ENOBUFS;
    351      1.12   itojun 			goto bad;
    352      1.12   itojun 		}
    353      1.12   itojun 		icmp6 = mtod(m, struct icmp6_hdr *);
    354      1.12   itojun 		type = icmp6->icmp6_type;
    355      1.12   itojun 		code = icmp6->icmp6_code;
    356      1.12   itojun 	}
    357      1.12   itojun 
    358       1.2   itojun 	M_PREPEND(m, sizeof(*ip6), M_WAIT);
    359       1.2   itojun 	ip6 = mtod(m, struct ip6_hdr *);
    360       1.2   itojun 
    361       1.2   itojun 	/*
    362       1.2   itojun 	 * Next header might not be ICMP6 but use its pseudo header anyway.
    363       1.2   itojun 	 */
    364       1.2   itojun 	ip6->ip6_dst = *dst;
    365       1.2   itojun 
    366       1.2   itojun 	/*
    367       1.2   itojun 	 * If the scope of the destination is link-local, embed the interface
    368       1.2   itojun 	 * index in the address.
    369       1.2   itojun 	 *
    370       1.2   itojun 	 * XXX advanced-api value overrides sin6_scope_id
    371       1.2   itojun 	 */
    372      1.12   itojun 	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst) ||
    373      1.12   itojun 	    IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) {
    374       1.2   itojun 		struct in6_pktinfo *pi;
    375       1.2   itojun 
    376       1.2   itojun 		/*
    377       1.2   itojun 		 * XXX Boundary check is assumed to be already done in
    378      1.12   itojun 		 * ip6_setpktoptions().
    379       1.2   itojun 		 */
    380       1.2   itojun 		if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
    381       1.2   itojun 			ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
    382       1.2   itojun 			oifp = ifindex2ifnet[pi->ipi6_ifindex];
    383       1.2   itojun 		}
    384       1.2   itojun 		else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
    385       1.2   itojun 			 in6p->in6p_moptions &&
    386       1.2   itojun 			 in6p->in6p_moptions->im6o_multicast_ifp) {
    387      1.12   itojun 			oifp = in6p->in6p_moptions->im6o_multicast_ifp;
    388      1.12   itojun 			ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
    389       1.2   itojun 		} else if (dstsock->sin6_scope_id) {
    390       1.2   itojun 			/* boundary check */
    391       1.2   itojun 			if (dstsock->sin6_scope_id < 0
    392       1.2   itojun 			 || if_index < dstsock->sin6_scope_id) {
    393       1.2   itojun 				error = ENXIO;  /* XXX EINVAL? */
    394       1.2   itojun 				goto bad;
    395       1.2   itojun 			}
    396       1.2   itojun 			ip6->ip6_dst.s6_addr16[1]
    397       1.2   itojun 				= htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
    398       1.2   itojun 		}
    399       1.2   itojun 	}
    400       1.2   itojun 
    401      1.12   itojun 	/*
    402      1.12   itojun 	 * Source address selection.
    403      1.12   itojun 	 */
    404      1.12   itojun 	{
    405       1.2   itojun 		struct in6_addr *in6a;
    406       1.2   itojun 
    407       1.2   itojun 		if ((in6a = in6_selectsrc(dstsock, optp,
    408       1.2   itojun 					  in6p->in6p_moptions,
    409       1.2   itojun 					  &in6p->in6p_route,
    410      1.12   itojun 					  &in6p->in6p_laddr,
    411       1.2   itojun 					  &error)) == 0) {
    412       1.2   itojun 			if (error == 0)
    413       1.2   itojun 				error = EADDRNOTAVAIL;
    414       1.2   itojun 			goto bad;
    415       1.2   itojun 		}
    416       1.2   itojun 		ip6->ip6_src = *in6a;
    417       1.2   itojun 		if (in6p->in6p_route.ro_rt)
    418       1.2   itojun 			oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
    419      1.12   itojun 	}
    420       1.2   itojun 
    421       1.2   itojun 	ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
    422      1.20   itojun 	ip6->ip6_vfc  &= ~IPV6_VERSION_MASK;
    423      1.20   itojun 	ip6->ip6_vfc  |= IPV6_VERSION;
    424       1.2   itojun #if 0				/* ip6_plen will be filled in ip6_output. */
    425       1.2   itojun 	ip6->ip6_plen  = htons((u_short)plen);
    426       1.2   itojun #endif
    427       1.2   itojun 	ip6->ip6_nxt   = in6p->in6p_ip6.ip6_nxt;
    428      1.12   itojun 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
    429       1.2   itojun 
    430       1.2   itojun 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
    431       1.2   itojun 	    in6p->in6p_cksum != -1) {
    432       1.2   itojun 		struct mbuf *n;
    433       1.2   itojun 		int off;
    434       1.2   itojun 		u_int16_t *p;
    435       1.2   itojun 
    436       1.2   itojun #define	offsetof(type, member)	((size_t)(&((type *)0)->member)) /* XXX */
    437       1.2   itojun 
    438       1.2   itojun 		/* compute checksum */
    439       1.2   itojun 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
    440       1.2   itojun 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
    441       1.2   itojun 		else
    442       1.2   itojun 			off = in6p->in6p_cksum;
    443       1.2   itojun 		if (plen < off + 1) {
    444       1.2   itojun 			error = EINVAL;
    445       1.2   itojun 			goto bad;
    446       1.2   itojun 		}
    447       1.2   itojun 		off += sizeof(struct ip6_hdr);
    448       1.2   itojun 
    449       1.2   itojun 		n = m;
    450       1.2   itojun 		while (n && n->m_len <= off) {
    451       1.2   itojun 			off -= n->m_len;
    452       1.2   itojun 			n = n->m_next;
    453       1.2   itojun 		}
    454       1.2   itojun 		if (!n)
    455       1.2   itojun 			goto bad;
    456       1.2   itojun 		p = (u_int16_t *)(mtod(n, caddr_t) + off);
    457       1.2   itojun 		*p = 0;
    458       1.2   itojun 		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
    459       1.2   itojun 	}
    460       1.2   itojun 
    461      1.12   itojun 	error = ip6_output(m, optp, &in6p->in6p_route, 0, in6p->in6p_moptions,
    462      1.12   itojun 			   &oifp);
    463      1.12   itojun 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
    464      1.12   itojun 		if (oifp)
    465      1.12   itojun 			icmp6_ifoutstat_inc(oifp, type, code);
    466      1.12   itojun 		icmp6stat.icp6s_outhist[type]++;
    467      1.12   itojun 	}
    468       1.2   itojun 
    469       1.2   itojun 	goto freectl;
    470       1.2   itojun 
    471       1.2   itojun  bad:
    472       1.2   itojun 	if (m)
    473       1.2   itojun 		m_freem(m);
    474       1.2   itojun 
    475       1.2   itojun  freectl:
    476       1.2   itojun 	if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
    477       1.2   itojun 		RTFREE(optp->ip6po_route.ro_rt);
    478       1.2   itojun 	if (control)
    479       1.2   itojun 		m_freem(control);
    480       1.2   itojun 	return(error);
    481       1.2   itojun }
    482       1.2   itojun 
    483       1.2   itojun /*
    484       1.2   itojun  * Raw IPv6 socket option processing.
    485       1.2   itojun  */
    486       1.2   itojun int
    487       1.2   itojun rip6_ctloutput(op, so, level, optname, m)
    488       1.2   itojun 	int op;
    489       1.2   itojun 	struct socket *so;
    490       1.2   itojun 	int level, optname;
    491       1.2   itojun 	struct mbuf **m;
    492       1.2   itojun {
    493       1.2   itojun 	int error = 0;
    494       1.2   itojun 
    495       1.2   itojun 	switch(level) {
    496       1.2   itojun 	 case IPPROTO_IPV6:
    497       1.2   itojun 		 switch(optname) {
    498       1.2   itojun 		  case MRT6_INIT:
    499       1.2   itojun 		  case MRT6_DONE:
    500       1.2   itojun 		  case MRT6_ADD_MIF:
    501       1.2   itojun 		  case MRT6_DEL_MIF:
    502       1.2   itojun 		  case MRT6_ADD_MFC:
    503       1.2   itojun 		  case MRT6_DEL_MFC:
    504       1.2   itojun 		  case MRT6_PIM:
    505       1.2   itojun 			  if (op == PRCO_SETOPT) {
    506       1.2   itojun 				  error = ip6_mrouter_set(optname, so, *m);
    507       1.2   itojun 				  if (*m)
    508       1.2   itojun 					  (void)m_free(*m);
    509       1.2   itojun 			  } else if (op == PRCO_GETOPT) {
    510       1.2   itojun 				  error = ip6_mrouter_get(optname, so, m);
    511       1.2   itojun 			  } else
    512       1.2   itojun 				  error = EINVAL;
    513       1.2   itojun 			  return (error);
    514       1.2   itojun 		 }
    515       1.2   itojun 		 return (ip6_ctloutput(op, so, level, optname, m));
    516       1.2   itojun 		 /* NOTREACHED */
    517       1.2   itojun 
    518       1.2   itojun 	 case IPPROTO_ICMPV6:
    519       1.2   itojun 		 /*
    520       1.2   itojun 		  * XXX: is it better to call icmp6_ctloutput() directly
    521       1.2   itojun 		  * from protosw?
    522       1.2   itojun 		  */
    523       1.2   itojun 		 return(icmp6_ctloutput(op, so, level, optname, m));
    524       1.2   itojun 
    525       1.2   itojun 	 default:
    526       1.2   itojun 		 if (op == PRCO_SETOPT && *m)
    527       1.2   itojun 			 (void)m_free(*m);
    528       1.2   itojun 		 return(EINVAL);
    529       1.2   itojun 	}
    530       1.2   itojun }
    531       1.2   itojun 
    532       1.2   itojun extern	u_long rip6_sendspace;
    533       1.2   itojun extern	u_long rip6_recvspace;
    534       1.2   itojun 
    535       1.2   itojun int
    536       1.2   itojun rip6_usrreq(so, req, m, nam, control, p)
    537       1.2   itojun 	register struct socket *so;
    538       1.2   itojun 	int req;
    539       1.2   itojun 	struct mbuf *m, *nam, *control;
    540       1.2   itojun 	struct proc *p;
    541       1.2   itojun {
    542       1.2   itojun 	register struct in6pcb *in6p = sotoin6pcb(so);
    543       1.2   itojun 	int s;
    544       1.2   itojun 	int error = 0;
    545       1.2   itojun /*	extern	struct socket *ip6_mrouter; */ /* xxx */
    546      1.12   itojun 	int priv;
    547      1.12   itojun 
    548      1.12   itojun 	priv = 0;
    549      1.12   itojun 	if (p && !suser(p->p_ucred, &p->p_acflag))
    550      1.12   itojun 		priv++;
    551       1.2   itojun 
    552       1.2   itojun 	if (req == PRU_CONTROL)
    553       1.2   itojun 		return (in6_control(so, (u_long)m, (caddr_t)nam,
    554       1.2   itojun 				    (struct ifnet *)control, p));
    555      1.17  thorpej 
    556      1.18  thorpej 	if (req == PRU_PURGEIF) {
    557      1.18  thorpej 		in6_purgeif((struct ifnet *)control);
    558      1.18  thorpej 		in6_pcbpurgeif(&rawin6pcb, (struct ifnet *)control);
    559      1.17  thorpej 		return (0);
    560      1.17  thorpej 	}
    561       1.2   itojun 
    562       1.2   itojun 	switch (req) {
    563       1.2   itojun 	case PRU_ATTACH:
    564       1.2   itojun 		if (in6p)
    565       1.2   itojun 			panic("rip6_attach");
    566      1.12   itojun 		if (!priv) {
    567       1.2   itojun 			error = EACCES;
    568       1.2   itojun 			break;
    569       1.2   itojun 		}
    570       1.4   itojun 		s = splsoftnet();
    571       1.2   itojun 		if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) ||
    572       1.2   itojun 		    (error = in6_pcballoc(so, &rawin6pcb))) {
    573       1.2   itojun 			splx(s);
    574       1.2   itojun 			break;
    575       1.2   itojun 		}
    576       1.2   itojun 		splx(s);
    577       1.2   itojun 		in6p = sotoin6pcb(so);
    578       1.5   itojun 		in6p->in6p_ip6.ip6_nxt = (long)nam;
    579       1.2   itojun 		in6p->in6p_cksum = -1;
    580       1.2   itojun #ifdef IPSEC
    581      1.16   itojun 		error = ipsec_init_policy(so, &in6p->in6p_sp);
    582      1.12   itojun 		if (error != 0) {
    583      1.11   itojun 			in6_pcbdetach(in6p);
    584       1.2   itojun 			break;
    585      1.11   itojun 		}
    586       1.2   itojun #endif /*IPSEC*/
    587       1.2   itojun 
    588       1.2   itojun 		MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
    589       1.2   itojun 			sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
    590      1.12   itojun 		if (in6p->in6p_icmp6filt == NULL) {
    591      1.12   itojun 			in6_pcbdetach(in6p);
    592      1.12   itojun 			error = ENOMEM;
    593      1.12   itojun 			break;
    594      1.12   itojun 		}
    595       1.2   itojun 		ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
    596       1.2   itojun 		break;
    597       1.2   itojun 
    598       1.2   itojun 	case PRU_DISCONNECT:
    599       1.2   itojun 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    600       1.2   itojun 			error = ENOTCONN;
    601       1.2   itojun 			break;
    602       1.2   itojun 		}
    603       1.2   itojun 		in6p->in6p_faddr = in6addr_any;
    604       1.2   itojun 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    605       1.2   itojun 		break;
    606       1.2   itojun 
    607       1.2   itojun 	case PRU_ABORT:
    608       1.2   itojun 		soisdisconnected(so);
    609       1.2   itojun 		/* Fallthrough */
    610       1.2   itojun 	case PRU_DETACH:
    611       1.2   itojun 		if (in6p == 0)
    612       1.2   itojun 			panic("rip6_detach");
    613       1.2   itojun 		if (so == ip6_mrouter)
    614       1.2   itojun 			ip6_mrouter_done();
    615       1.2   itojun 		/* xxx: RSVP */
    616       1.2   itojun 		if (in6p->in6p_icmp6filt) {
    617       1.2   itojun 			FREE(in6p->in6p_icmp6filt, M_PCB);
    618       1.2   itojun 			in6p->in6p_icmp6filt = NULL;
    619       1.2   itojun 		}
    620       1.2   itojun 		in6_pcbdetach(in6p);
    621       1.2   itojun 		break;
    622       1.2   itojun 
    623       1.2   itojun 	case PRU_BIND:
    624       1.2   itojun 	    {
    625       1.2   itojun 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    626       1.2   itojun 		struct ifaddr *ia = NULL;
    627       1.2   itojun 
    628       1.2   itojun 		if (nam->m_len != sizeof(*addr)) {
    629       1.2   itojun 			error = EINVAL;
    630       1.2   itojun 			break;
    631       1.2   itojun 		}
    632  1.22.2.1  minoura 
    633  1.22.2.1  minoura 		/*
    634  1.22.2.1  minoura 		 * we don't support mapped address here, it would confuse
    635  1.22.2.1  minoura 		 * users so reject it
    636  1.22.2.1  minoura 		 */
    637  1.22.2.1  minoura 		if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
    638  1.22.2.1  minoura 			error = EADDRNOTAVAIL;
    639  1.22.2.1  minoura 			break;
    640  1.22.2.1  minoura 		}
    641  1.22.2.1  minoura 
    642       1.2   itojun 		if ((ifnet.tqh_first == 0) ||
    643       1.2   itojun 		   (addr->sin6_family != AF_INET6) ||
    644      1.12   itojun 		   (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
    645       1.2   itojun 		    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)) {
    646       1.2   itojun 			error = EADDRNOTAVAIL;
    647       1.2   itojun 			break;
    648       1.2   itojun 		}
    649       1.2   itojun 		if (ia &&
    650       1.2   itojun 		    ((struct in6_ifaddr *)ia)->ia6_flags &
    651       1.2   itojun 		    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    652       1.2   itojun 		     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    653       1.2   itojun 			error = EADDRNOTAVAIL;
    654       1.2   itojun 			break;
    655       1.2   itojun 		}
    656       1.2   itojun 		in6p->in6p_laddr = addr->sin6_addr;
    657       1.2   itojun 		break;
    658       1.2   itojun 	    }
    659       1.2   itojun 
    660       1.2   itojun 	case PRU_CONNECT:
    661       1.2   itojun 	    {
    662       1.2   itojun 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
    663       1.2   itojun 		struct in6_addr *in6a = NULL;
    664       1.2   itojun 
    665       1.2   itojun 		if (nam->m_len != sizeof(*addr)) {
    666       1.2   itojun 			error = EINVAL;
    667       1.2   itojun 			break;
    668       1.2   itojun 		}
    669       1.2   itojun 		if (ifnet.tqh_first == 0) {
    670       1.2   itojun 			error = EADDRNOTAVAIL;
    671       1.2   itojun 			break;
    672       1.2   itojun 		}
    673       1.2   itojun 		if (addr->sin6_family != AF_INET6) {
    674       1.2   itojun 			error = EAFNOSUPPORT;
    675       1.2   itojun 			break;
    676       1.2   itojun 		}
    677       1.2   itojun 
    678       1.2   itojun 		/* Source address selection. XXX: need pcblookup? */
    679      1.12   itojun 		in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
    680      1.12   itojun 				     in6p->in6p_moptions,
    681      1.12   itojun 				     &in6p->in6p_route,
    682      1.12   itojun 				     &in6p->in6p_laddr,
    683      1.12   itojun 				     &error);
    684      1.12   itojun 		if (in6a == NULL) {
    685       1.2   itojun 			if (error == 0)
    686       1.2   itojun 				error = EADDRNOTAVAIL;
    687       1.2   itojun 			break;
    688       1.2   itojun 		}
    689       1.2   itojun 		in6p->in6p_laddr = *in6a;
    690       1.2   itojun 		in6p->in6p_faddr = addr->sin6_addr;
    691       1.2   itojun 		soisconnected(so);
    692       1.2   itojun 		break;
    693       1.2   itojun 	    }
    694       1.2   itojun 
    695       1.2   itojun 	case PRU_CONNECT2:
    696       1.2   itojun 		error = EOPNOTSUPP;
    697       1.2   itojun 		break;
    698       1.2   itojun 
    699       1.2   itojun 	/*
    700       1.2   itojun 	 * Mark the connection as being incapable of futther input.
    701       1.2   itojun 	 */
    702       1.2   itojun 	case PRU_SHUTDOWN:
    703       1.2   itojun 		socantsendmore(so);
    704       1.2   itojun 		break;
    705       1.2   itojun 	/*
    706       1.2   itojun 	 * Ship a packet out. The appropriate raw output
    707       1.2   itojun 	 * routine handles any messaging necessary.
    708       1.2   itojun 	 */
    709       1.2   itojun 	case PRU_SEND:
    710       1.2   itojun 	    {
    711       1.2   itojun 		struct sockaddr_in6 tmp;
    712       1.2   itojun 		struct sockaddr_in6 *dst;
    713       1.2   itojun 
    714       1.2   itojun 		if (so->so_state & SS_ISCONNECTED) {
    715       1.2   itojun 			if (nam) {
    716       1.2   itojun 				error = EISCONN;
    717       1.2   itojun 				break;
    718       1.2   itojun 			}
    719       1.2   itojun 			/* XXX */
    720       1.2   itojun 			bzero(&tmp, sizeof(tmp));
    721       1.2   itojun 			tmp.sin6_family = AF_INET6;
    722       1.2   itojun 			tmp.sin6_len = sizeof(struct sockaddr_in6);
    723       1.2   itojun 			bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
    724       1.2   itojun 				sizeof(struct in6_addr));
    725       1.2   itojun 			dst = &tmp;
    726       1.2   itojun 		} else {
    727       1.2   itojun 			if (nam == NULL) {
    728       1.2   itojun 				error = ENOTCONN;
    729       1.2   itojun 				break;
    730       1.2   itojun 			}
    731       1.2   itojun 			dst = mtod(nam, struct sockaddr_in6 *);
    732       1.2   itojun 		}
    733       1.2   itojun 		error = rip6_output(m, so, dst, control);
    734       1.2   itojun 		m = NULL;
    735       1.2   itojun 		break;
    736       1.2   itojun 	    }
    737       1.2   itojun 
    738       1.2   itojun 	case PRU_SENSE:
    739       1.2   itojun 		/*
    740       1.2   itojun 		 * stat: don't bother with a blocksize
    741       1.2   itojun 		 */
    742       1.2   itojun 		return(0);
    743       1.2   itojun 	/*
    744       1.2   itojun 	 * Not supported.
    745       1.2   itojun 	 */
    746       1.2   itojun 	case PRU_RCVOOB:
    747       1.2   itojun 	case PRU_RCVD:
    748       1.2   itojun 	case PRU_LISTEN:
    749       1.2   itojun 	case PRU_ACCEPT:
    750       1.2   itojun 	case PRU_SENDOOB:
    751       1.2   itojun 		error = EOPNOTSUPP;
    752       1.2   itojun 		break;
    753       1.2   itojun 
    754       1.2   itojun 	case PRU_SOCKADDR:
    755       1.2   itojun 		in6_setsockaddr(in6p, nam);
    756       1.2   itojun 		break;
    757       1.2   itojun 
    758       1.2   itojun 	case PRU_PEERADDR:
    759       1.2   itojun 		in6_setpeeraddr(in6p, nam);
    760       1.2   itojun 		break;
    761       1.2   itojun 
    762       1.2   itojun 	default:
    763       1.2   itojun 		panic("rip6_usrreq");
    764       1.2   itojun 	}
    765       1.2   itojun 	if (m != NULL)
    766       1.2   itojun 		m_freem(m);
    767       1.2   itojun 	return(error);
    768       1.2   itojun }
    769