raw_ip6.c revision 194777
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*-
31 * Copyright (c) 1982, 1986, 1988, 1993
32 *	The Regents of the University of California.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 *    notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 *    notice, this list of conditions and the following disclaimer in the
42 *    documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 *    may be used to endorse or promote products derived from this software
45 *    without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
60 */
61
62#include <sys/cdefs.h>
63__FBSDID("$FreeBSD: head/sys/netinet6/raw_ip6.c 194777 2009-06-23 22:08:55Z bz $");
64
65#include "opt_ipsec.h"
66#include "opt_inet6.h"
67
68#include <sys/param.h>
69#include <sys/errno.h>
70#include <sys/jail.h>
71#include <sys/lock.h>
72#include <sys/malloc.h>
73#include <sys/mbuf.h>
74#include <sys/priv.h>
75#include <sys/proc.h>
76#include <sys/protosw.h>
77#include <sys/signalvar.h>
78#include <sys/socket.h>
79#include <sys/socketvar.h>
80#include <sys/sx.h>
81#include <sys/syslog.h>
82#include <sys/vimage.h>
83
84#include <net/if.h>
85#include <net/if_types.h>
86#include <net/route.h>
87#include <net/vnet.h>
88
89#include <netinet/in.h>
90#include <netinet/in_var.h>
91#include <netinet/in_systm.h>
92#include <netinet/in_pcb.h>
93#include <netinet/vinet.h>
94
95#include <netinet/icmp6.h>
96#include <netinet/ip6.h>
97#include <netinet6/ip6protosw.h>
98#include <netinet6/ip6_mroute.h>
99#include <netinet6/in6_pcb.h>
100#include <netinet6/ip6_var.h>
101#include <netinet6/nd6.h>
102#include <netinet6/raw_ip6.h>
103#include <netinet6/scope6_var.h>
104#include <netinet6/vinet6.h>
105
106#ifdef IPSEC
107#include <netipsec/ipsec.h>
108#include <netipsec/ipsec6.h>
109#endif /* IPSEC */
110
111#include <machine/stdarg.h>
112
113#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
114#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
115
116/*
117 * Raw interface to IP6 protocol.
118 */
119
120#ifdef VIMAGE_GLOBALS
121extern struct	inpcbhead ripcb;
122extern struct	inpcbinfo ripcbinfo;
123struct rip6stat rip6stat;
124#endif
125
126extern u_long	rip_sendspace;
127extern u_long	rip_recvspace;
128
129/*
130 * Hooks for multicast routing. They all default to NULL, so leave them not
131 * initialized and rely on BSS being set to 0.
132 */
133
134/*
135 * The socket used to communicate with the multicast routing daemon.
136 */
137#ifdef VIMAGE_GLOBALS
138struct socket *ip6_mrouter;
139#endif
140
141/*
142 * The various mrouter functions.
143 */
144int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
145int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
146int (*ip6_mrouter_done)(void);
147int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
148int (*mrt6_ioctl)(u_long, caddr_t);
149
150/*
151 * Setup generic address and protocol structures for raw_input routine, then
152 * pass them along with mbuf chain.
153 */
154int
155rip6_input(struct mbuf **mp, int *offp, int proto)
156{
157	INIT_VNET_INET(curvnet);
158	INIT_VNET_INET6(curvnet);
159#ifdef IPSEC
160	INIT_VNET_IPSEC(curvnet);
161#endif
162	struct ifnet *ifp;
163	struct mbuf *m = *mp;
164	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
165	register struct inpcb *in6p;
166	struct inpcb *last = 0;
167	struct mbuf *opts = NULL;
168	struct sockaddr_in6 fromsa;
169
170	V_rip6stat.rip6s_ipackets++;
171
172	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
173		/* XXX Send icmp6 host/port unreach? */
174		m_freem(m);
175		return (IPPROTO_DONE);
176	}
177
178	init_sin6(&fromsa, m); /* general init */
179
180	ifp = m->m_pkthdr.rcvif;
181
182	INP_INFO_RLOCK(&V_ripcbinfo);
183	LIST_FOREACH(in6p, &V_ripcb, inp_list) {
184		/* XXX inp locking */
185		if ((in6p->inp_vflag & INP_IPV6) == 0)
186			continue;
187		if (in6p->inp_ip_p &&
188		    in6p->inp_ip_p != proto)
189			continue;
190		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
191		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
192			continue;
193		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
194		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
195			continue;
196		if (jailed(in6p->inp_cred)) {
197			/*
198			 * Allow raw socket in jail to receive multicast;
199			 * assume process had PRIV_NETINET_RAW at attach,
200			 * and fall through into normal filter path if so.
201			 */
202			if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
203			    prison_check_ip6(in6p->inp_cred,
204			    &ip6->ip6_dst) != 0)
205				continue;
206		}
207		if (in6p->in6p_cksum != -1) {
208			V_rip6stat.rip6s_isum++;
209			if (in6_cksum(m, proto, *offp,
210			    m->m_pkthdr.len - *offp)) {
211				INP_RUNLOCK(in6p);
212				V_rip6stat.rip6s_badsum++;
213				continue;
214			}
215		}
216		INP_RLOCK(in6p);
217		/*
218		 * If this raw socket has multicast state, and we
219		 * have received a multicast, check if this socket
220		 * should receive it, as multicast filtering is now
221		 * the responsibility of the transport layer.
222		 */
223		if (in6p->in6p_moptions &&
224		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
225			struct sockaddr_in6 mcaddr;
226			int blocked;
227
228			bzero(&mcaddr, sizeof(struct sockaddr_in6));
229			mcaddr.sin6_len = sizeof(struct sockaddr_in6);
230			mcaddr.sin6_family = AF_INET6;
231			mcaddr.sin6_addr = ip6->ip6_dst;
232
233			blocked = im6o_mc_filter(in6p->in6p_moptions, ifp,
234			    (struct sockaddr *)&mcaddr,
235			    (struct sockaddr *)&fromsa);
236			if (blocked != MCAST_PASS) {
237				IP6STAT_INC(ip6s_notmember);
238				continue;
239			}
240		}
241		if (last != NULL) {
242			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
243
244#ifdef IPSEC
245			/*
246			 * Check AH/ESP integrity.
247			 */
248			if (n && ipsec6_in_reject(n, last)) {
249				m_freem(n);
250				V_ipsec6stat.in_polvio++;
251				/* Do not inject data into pcb. */
252			} else
253#endif /* IPSEC */
254			if (n) {
255				if (last->inp_flags & INP_CONTROLOPTS ||
256				    last->inp_socket->so_options & SO_TIMESTAMP)
257					ip6_savecontrol(last, n, &opts);
258				/* strip intermediate headers */
259				m_adj(n, *offp);
260				if (sbappendaddr(&last->inp_socket->so_rcv,
261						(struct sockaddr *)&fromsa,
262						 n, opts) == 0) {
263					m_freem(n);
264					if (opts)
265						m_freem(opts);
266					V_rip6stat.rip6s_fullsock++;
267				} else
268					sorwakeup(last->inp_socket);
269				opts = NULL;
270			}
271			INP_RUNLOCK(last);
272		}
273		last = in6p;
274	}
275	INP_INFO_RUNLOCK(&V_ripcbinfo);
276#ifdef IPSEC
277	/*
278	 * Check AH/ESP integrity.
279	 */
280	if ((last != NULL) && ipsec6_in_reject(m, last)) {
281		m_freem(m);
282		V_ipsec6stat.in_polvio++;
283		V_ip6stat.ip6s_delivered--;
284		/* Do not inject data into pcb. */
285		INP_RUNLOCK(last);
286	} else
287#endif /* IPSEC */
288	if (last != NULL) {
289		if (last->inp_flags & INP_CONTROLOPTS ||
290		    last->inp_socket->so_options & SO_TIMESTAMP)
291			ip6_savecontrol(last, m, &opts);
292		/* Strip intermediate headers. */
293		m_adj(m, *offp);
294		if (sbappendaddr(&last->inp_socket->so_rcv,
295		    (struct sockaddr *)&fromsa, m, opts) == 0) {
296			m_freem(m);
297			if (opts)
298				m_freem(opts);
299			V_rip6stat.rip6s_fullsock++;
300		} else
301			sorwakeup(last->inp_socket);
302		INP_RUNLOCK(last);
303	} else {
304		V_rip6stat.rip6s_nosock++;
305		if (m->m_flags & M_MCAST)
306			V_rip6stat.rip6s_nosockmcast++;
307		if (proto == IPPROTO_NONE)
308			m_freem(m);
309		else {
310			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
311			icmp6_error(m, ICMP6_PARAM_PROB,
312			    ICMP6_PARAMPROB_NEXTHEADER,
313			    prvnxtp - mtod(m, char *));
314		}
315		V_ip6stat.ip6s_delivered--;
316	}
317	return (IPPROTO_DONE);
318}
319
320void
321rip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
322{
323	INIT_VNET_INET(curvnet);
324	struct ip6_hdr *ip6;
325	struct mbuf *m;
326	int off = 0;
327	struct ip6ctlparam *ip6cp = NULL;
328	const struct sockaddr_in6 *sa6_src = NULL;
329	void *cmdarg;
330	struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
331
332	if (sa->sa_family != AF_INET6 ||
333	    sa->sa_len != sizeof(struct sockaddr_in6))
334		return;
335
336	if ((unsigned)cmd >= PRC_NCMDS)
337		return;
338	if (PRC_IS_REDIRECT(cmd))
339		notify = in6_rtchange, d = NULL;
340	else if (cmd == PRC_HOSTDEAD)
341		d = NULL;
342	else if (inet6ctlerrmap[cmd] == 0)
343		return;
344
345	/*
346	 * If the parameter is from icmp6, decode it.
347	 */
348	if (d != NULL) {
349		ip6cp = (struct ip6ctlparam *)d;
350		m = ip6cp->ip6c_m;
351		ip6 = ip6cp->ip6c_ip6;
352		off = ip6cp->ip6c_off;
353		cmdarg = ip6cp->ip6c_cmdarg;
354		sa6_src = ip6cp->ip6c_src;
355	} else {
356		m = NULL;
357		ip6 = NULL;
358		cmdarg = NULL;
359		sa6_src = &sa6_any;
360	}
361
362	(void) in6_pcbnotify(&V_ripcbinfo, sa, 0,
363	    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
364}
365
366/*
367 * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
368 * may have setup with control call.
369 */
370int
371#if __STDC__
372rip6_output(struct mbuf *m, ...)
373#else
374rip6_output(m, va_alist)
375	struct mbuf *m;
376	va_dcl
377#endif
378{
379	INIT_VNET_INET6(curvnet);
380	struct mbuf *control;
381	struct socket *so;
382	struct sockaddr_in6 *dstsock;
383	struct in6_addr *dst;
384	struct ip6_hdr *ip6;
385	struct inpcb *in6p;
386	u_int	plen = m->m_pkthdr.len;
387	int error = 0;
388	struct ip6_pktopts opt, *optp;
389	struct ifnet *oifp = NULL;
390	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
391	int scope_ambiguous = 0;
392	struct in6_addr in6a;
393	va_list ap;
394
395	va_start(ap, m);
396	so = va_arg(ap, struct socket *);
397	dstsock = va_arg(ap, struct sockaddr_in6 *);
398	control = va_arg(ap, struct mbuf *);
399	va_end(ap);
400
401	in6p = sotoinpcb(so);
402	INP_WLOCK(in6p);
403
404	dst = &dstsock->sin6_addr;
405	if (control != NULL) {
406		if ((error = ip6_setpktopts(control, &opt,
407		    in6p->in6p_outputopts, so->so_cred,
408		    so->so_proto->pr_protocol)) != 0) {
409			goto bad;
410		}
411		optp = &opt;
412	} else
413		optp = in6p->in6p_outputopts;
414
415	/*
416	 * Check and convert scope zone ID into internal form.
417	 *
418	 * XXX: we may still need to determine the zone later.
419	 */
420	if (!(so->so_state & SS_ISCONNECTED)) {
421		if (dstsock->sin6_scope_id == 0 && !V_ip6_use_defzone)
422			scope_ambiguous = 1;
423		if ((error = sa6_embedscope(dstsock, V_ip6_use_defzone)) != 0)
424			goto bad;
425	}
426
427	/*
428	 * For an ICMPv6 packet, we should know its type and code to update
429	 * statistics.
430	 */
431	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
432		struct icmp6_hdr *icmp6;
433		if (m->m_len < sizeof(struct icmp6_hdr) &&
434		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
435			error = ENOBUFS;
436			goto bad;
437		}
438		icmp6 = mtod(m, struct icmp6_hdr *);
439		type = icmp6->icmp6_type;
440		code = icmp6->icmp6_code;
441	}
442
443	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
444	if (m == NULL) {
445		error = ENOBUFS;
446		goto bad;
447	}
448	ip6 = mtod(m, struct ip6_hdr *);
449
450	/*
451	 * Source address selection.
452	 */
453	error = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred,
454	    &oifp, &in6a);
455	if (error)
456		goto bad;
457	error = prison_get_ip6(in6p->inp_cred, &in6a);
458	if (error != 0)
459		goto bad;
460	ip6->ip6_src = in6a;
461
462	if (oifp && scope_ambiguous) {
463		/*
464		 * Application should provide a proper zone ID or the use of
465		 * default zone IDs should be enabled.  Unfortunately, some
466		 * applications do not behave as it should, so we need a
467		 * workaround.  Even if an appropriate ID is not determined
468		 * (when it's required), if we can determine the outgoing
469		 * interface. determine the zone ID based on the interface.
470		 */
471		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
472		if (error != 0)
473			goto bad;
474	}
475	ip6->ip6_dst = dstsock->sin6_addr;
476
477	/*
478	 * Fill in the rest of the IPv6 header fields.
479	 */
480	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
481	    (in6p->inp_flow & IPV6_FLOWINFO_MASK);
482	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
483	    (IPV6_VERSION & IPV6_VERSION_MASK);
484
485	/*
486	 * ip6_plen will be filled in ip6_output, so not fill it here.
487	 */
488	ip6->ip6_nxt = in6p->inp_ip_p;
489	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
490
491	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
492	    in6p->in6p_cksum != -1) {
493		struct mbuf *n;
494		int off;
495		u_int16_t *p;
496
497		/* Compute checksum. */
498		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
499			off = offsetof(struct icmp6_hdr, icmp6_cksum);
500		else
501			off = in6p->in6p_cksum;
502		if (plen < off + 1) {
503			error = EINVAL;
504			goto bad;
505		}
506		off += sizeof(struct ip6_hdr);
507
508		n = m;
509		while (n && n->m_len <= off) {
510			off -= n->m_len;
511			n = n->m_next;
512		}
513		if (!n)
514			goto bad;
515		p = (u_int16_t *)(mtod(n, caddr_t) + off);
516		*p = 0;
517		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
518	}
519
520	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
521	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
522		if (oifp)
523			icmp6_ifoutstat_inc(oifp, type, code);
524		ICMP6STAT_INC(icp6s_outhist[type]);
525	} else
526		V_rip6stat.rip6s_opackets++;
527
528	goto freectl;
529
530 bad:
531	if (m)
532		m_freem(m);
533
534 freectl:
535	if (control != NULL) {
536		ip6_clearpktopts(&opt, -1);
537		m_freem(control);
538	}
539	INP_WUNLOCK(in6p);
540	return (error);
541}
542
543/*
544 * Raw IPv6 socket option processing.
545 */
546int
547rip6_ctloutput(struct socket *so, struct sockopt *sopt)
548{
549	int error;
550
551	if (sopt->sopt_level == IPPROTO_ICMPV6)
552		/*
553		 * XXX: is it better to call icmp6_ctloutput() directly
554		 * from protosw?
555		 */
556		return (icmp6_ctloutput(so, sopt));
557	else if (sopt->sopt_level != IPPROTO_IPV6)
558		return (EINVAL);
559
560	error = 0;
561
562	switch (sopt->sopt_dir) {
563	case SOPT_GET:
564		switch (sopt->sopt_name) {
565		case MRT6_INIT:
566		case MRT6_DONE:
567		case MRT6_ADD_MIF:
568		case MRT6_DEL_MIF:
569		case MRT6_ADD_MFC:
570		case MRT6_DEL_MFC:
571		case MRT6_PIM:
572			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
573			    EOPNOTSUPP;
574			break;
575		case IPV6_CHECKSUM:
576			error = ip6_raw_ctloutput(so, sopt);
577			break;
578		default:
579			error = ip6_ctloutput(so, sopt);
580			break;
581		}
582		break;
583
584	case SOPT_SET:
585		switch (sopt->sopt_name) {
586		case MRT6_INIT:
587		case MRT6_DONE:
588		case MRT6_ADD_MIF:
589		case MRT6_DEL_MIF:
590		case MRT6_ADD_MFC:
591		case MRT6_DEL_MFC:
592		case MRT6_PIM:
593			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
594			    EOPNOTSUPP;
595			break;
596		case IPV6_CHECKSUM:
597			error = ip6_raw_ctloutput(so, sopt);
598			break;
599		default:
600			error = ip6_ctloutput(so, sopt);
601			break;
602		}
603		break;
604	}
605
606	return (error);
607}
608
609static int
610rip6_attach(struct socket *so, int proto, struct thread *td)
611{
612	INIT_VNET_INET(so->so_vnet);
613	struct inpcb *inp;
614	struct icmp6_filter *filter;
615	int error;
616
617	inp = sotoinpcb(so);
618	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
619
620	error = priv_check(td, PRIV_NETINET_RAW);
621	if (error)
622		return (error);
623	error = soreserve(so, rip_sendspace, rip_recvspace);
624	if (error)
625		return (error);
626	filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
627	if (filter == NULL)
628		return (ENOMEM);
629	INP_INFO_WLOCK(&V_ripcbinfo);
630	error = in_pcballoc(so, &V_ripcbinfo);
631	if (error) {
632		INP_INFO_WUNLOCK(&V_ripcbinfo);
633		free(filter, M_PCB);
634		return (error);
635	}
636	inp = (struct inpcb *)so->so_pcb;
637	INP_INFO_WUNLOCK(&V_ripcbinfo);
638	inp->inp_vflag |= INP_IPV6;
639	inp->inp_ip_p = (long)proto;
640	inp->in6p_hops = -1;	/* use kernel default */
641	inp->in6p_cksum = -1;
642	inp->in6p_icmp6filt = filter;
643	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
644	INP_WUNLOCK(inp);
645	return (0);
646}
647
648static void
649rip6_detach(struct socket *so)
650{
651	INIT_VNET_INET(so->so_vnet);
652	INIT_VNET_INET6(so->so_vnet);
653	struct inpcb *inp;
654
655	inp = sotoinpcb(so);
656	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
657
658	if (so == V_ip6_mrouter && ip6_mrouter_done)
659		ip6_mrouter_done();
660	/* xxx: RSVP */
661	INP_INFO_WLOCK(&V_ripcbinfo);
662	INP_WLOCK(inp);
663	free(inp->in6p_icmp6filt, M_PCB);
664	in_pcbdetach(inp);
665	in_pcbfree(inp);
666	INP_INFO_WUNLOCK(&V_ripcbinfo);
667}
668
669/* XXXRW: This can't ever be called. */
670static void
671rip6_abort(struct socket *so)
672{
673	struct inpcb *inp;
674
675	inp = sotoinpcb(so);
676	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
677
678	soisdisconnected(so);
679}
680
681static void
682rip6_close(struct socket *so)
683{
684	struct inpcb *inp;
685
686	inp = sotoinpcb(so);
687	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
688
689	soisdisconnected(so);
690}
691
692static int
693rip6_disconnect(struct socket *so)
694{
695	struct inpcb *inp;
696
697	inp = sotoinpcb(so);
698	KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
699
700	if ((so->so_state & SS_ISCONNECTED) == 0)
701		return (ENOTCONN);
702	inp->in6p_faddr = in6addr_any;
703	rip6_abort(so);
704	return (0);
705}
706
707static int
708rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
709{
710	INIT_VNET_NET(so->so_vnet);
711	INIT_VNET_INET(so->so_vnet);
712	INIT_VNET_INET6(so->so_vnet);
713	struct inpcb *inp;
714	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
715	struct ifaddr *ifa = NULL;
716	int error = 0;
717
718	inp = sotoinpcb(so);
719	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
720
721	if (nam->sa_len != sizeof(*addr))
722		return (EINVAL);
723	if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
724		return (error);
725	if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
726		return (EADDRNOTAVAIL);
727	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
728		return (error);
729
730	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
731	    (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL)
732		return (EADDRNOTAVAIL);
733	if (ifa != NULL &&
734	    ((struct in6_ifaddr *)ifa)->ia6_flags &
735	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
736	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
737		ifa_free(ifa);
738		return (EADDRNOTAVAIL);
739	}
740	if (ifa != NULL)
741		ifa_free(ifa);
742	INP_INFO_WLOCK(&V_ripcbinfo);
743	INP_WLOCK(inp);
744	inp->in6p_laddr = addr->sin6_addr;
745	INP_WUNLOCK(inp);
746	INP_INFO_WUNLOCK(&V_ripcbinfo);
747	return (0);
748}
749
750static int
751rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
752{
753	INIT_VNET_NET(so->so_vnet);
754	INIT_VNET_INET(so->so_vnet);
755	INIT_VNET_INET6(so->so_vnet);
756	struct inpcb *inp;
757	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
758	struct in6_addr in6a;
759	struct ifnet *ifp = NULL;
760	int error = 0, scope_ambiguous = 0;
761
762	inp = sotoinpcb(so);
763	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
764
765	if (nam->sa_len != sizeof(*addr))
766		return (EINVAL);
767	if (TAILQ_EMPTY(&V_ifnet))
768		return (EADDRNOTAVAIL);
769	if (addr->sin6_family != AF_INET6)
770		return (EAFNOSUPPORT);
771
772	/*
773	 * Application should provide a proper zone ID or the use of default
774	 * zone IDs should be enabled.  Unfortunately, some applications do
775	 * not behave as it should, so we need a workaround.  Even if an
776	 * appropriate ID is not determined, we'll see if we can determine
777	 * the outgoing interface.  If we can, determine the zone ID based on
778	 * the interface below.
779	 */
780	if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
781		scope_ambiguous = 1;
782	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
783		return (error);
784
785	INP_INFO_WLOCK(&V_ripcbinfo);
786	INP_WLOCK(inp);
787	/* Source address selection. XXX: need pcblookup? */
788	error = in6_selectsrc(addr, inp->in6p_outputopts,
789	    inp, NULL, so->so_cred, &ifp, &in6a);
790	if (error) {
791		INP_WUNLOCK(inp);
792		INP_INFO_WUNLOCK(&V_ripcbinfo);
793		return (error);
794	}
795
796	/* XXX: see above */
797	if (ifp && scope_ambiguous &&
798	    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
799		INP_WUNLOCK(inp);
800		INP_INFO_WUNLOCK(&V_ripcbinfo);
801		return (error);
802	}
803	inp->in6p_faddr = addr->sin6_addr;
804	inp->in6p_laddr = in6a;
805	soisconnected(so);
806	INP_WUNLOCK(inp);
807	INP_INFO_WUNLOCK(&V_ripcbinfo);
808	return (0);
809}
810
811static int
812rip6_shutdown(struct socket *so)
813{
814	struct inpcb *inp;
815
816	inp = sotoinpcb(so);
817	KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
818
819	INP_WLOCK(inp);
820	socantsendmore(so);
821	INP_WUNLOCK(inp);
822	return (0);
823}
824
825static int
826rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
827    struct mbuf *control, struct thread *td)
828{
829	struct inpcb *inp;
830	struct sockaddr_in6 tmp;
831	struct sockaddr_in6 *dst;
832	int ret;
833
834	inp = sotoinpcb(so);
835	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
836
837	/* Always copy sockaddr to avoid overwrites. */
838	/* Unlocked read. */
839	if (so->so_state & SS_ISCONNECTED) {
840		if (nam) {
841			m_freem(m);
842			return (EISCONN);
843		}
844		/* XXX */
845		bzero(&tmp, sizeof(tmp));
846		tmp.sin6_family = AF_INET6;
847		tmp.sin6_len = sizeof(struct sockaddr_in6);
848		INP_RLOCK(inp);
849		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
850		    sizeof(struct in6_addr));
851		INP_RUNLOCK(inp);
852		dst = &tmp;
853	} else {
854		if (nam == NULL) {
855			m_freem(m);
856			return (ENOTCONN);
857		}
858		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
859			m_freem(m);
860			return (EINVAL);
861		}
862		tmp = *(struct sockaddr_in6 *)nam;
863		dst = &tmp;
864
865		if (dst->sin6_family == AF_UNSPEC) {
866			/*
867			 * XXX: we allow this case for backward
868			 * compatibility to buggy applications that
869			 * rely on old (and wrong) kernel behavior.
870			 */
871			log(LOG_INFO, "rip6 SEND: address family is "
872			    "unspec. Assume AF_INET6\n");
873			dst->sin6_family = AF_INET6;
874		} else if (dst->sin6_family != AF_INET6) {
875			m_freem(m);
876			return(EAFNOSUPPORT);
877		}
878	}
879	ret = rip6_output(m, so, dst, control);
880	return (ret);
881}
882
883struct pr_usrreqs rip6_usrreqs = {
884	.pru_abort =		rip6_abort,
885	.pru_attach =		rip6_attach,
886	.pru_bind =		rip6_bind,
887	.pru_connect =		rip6_connect,
888	.pru_control =		in6_control,
889	.pru_detach =		rip6_detach,
890	.pru_disconnect =	rip6_disconnect,
891	.pru_peeraddr =		in6_getpeeraddr,
892	.pru_send =		rip6_send,
893	.pru_shutdown =		rip6_shutdown,
894	.pru_sockaddr =		in6_getsockaddr,
895	.pru_close =		rip6_close,
896};
897