raw_ip6.c revision 125941
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 * $FreeBSD: head/sys/netinet6/raw_ip6.c 125941 2004-02-17 14:02:37Z ume $
30 */
31
32/*
33 * Copyright (c) 1982, 1986, 1988, 1993
34 *	The Regents of the University of California.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 *    must display the following acknowledgement:
46 *	This product includes software developed by the University of
47 *	California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 *    may be used to endorse or promote products derived from this software
50 *    without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
65 */
66
67#include "opt_ipsec.h"
68#include "opt_inet6.h"
69
70#include <sys/param.h>
71#include <sys/errno.h>
72#include <sys/lock.h>
73#include <sys/malloc.h>
74#include <sys/mbuf.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/systm.h>
82
83#include <net/if.h>
84#include <net/if_types.h>
85#include <net/route.h>
86
87#include <netinet/in.h>
88#include <netinet/in_var.h>
89#include <netinet/in_systm.h>
90#include <netinet/icmp6.h>
91#include <netinet/in_pcb.h>
92#include <netinet/ip6.h>
93#include <netinet6/ip6protosw.h>
94#include <netinet6/ip6_mroute.h>
95#include <netinet6/in6_pcb.h>
96#include <netinet6/ip6_var.h>
97#include <netinet6/nd6.h>
98#include <netinet6/raw_ip6.h>
99#ifdef ENABLE_DEFAULT_SCOPE
100#include <netinet6/scope6_var.h>
101#endif
102
103#ifdef IPSEC
104#include <netinet6/ipsec.h>
105#include <netinet6/ipsec6.h>
106#endif /*IPSEC*/
107
108#ifdef FAST_IPSEC
109#include <netipsec/ipsec.h>
110#include <netipsec/ipsec6.h>
111#endif /* FAST_IPSEC */
112
113#include <machine/stdarg.h>
114
115#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
116#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
117
118/*
119 * Raw interface to IP6 protocol.
120 */
121
122extern struct	inpcbhead ripcb;
123extern struct	inpcbinfo ripcbinfo;
124extern u_long	rip_sendspace;
125extern u_long	rip_recvspace;
126
127struct rip6stat rip6stat;
128
129/*
130 * Setup generic address and protocol structures
131 * for raw_input routine, then pass them along with
132 * mbuf chain.
133 */
134int
135rip6_input(mp, offp, proto)
136	struct	mbuf **mp;
137	int	*offp, proto;
138{
139	struct mbuf *m = *mp;
140	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
141	register struct inpcb *in6p;
142	struct inpcb *last = 0;
143	struct mbuf *opts = NULL;
144	struct sockaddr_in6 fromsa;
145
146	rip6stat.rip6s_ipackets++;
147
148	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
149		/* XXX send icmp6 host/port unreach? */
150		m_freem(m);
151		return IPPROTO_DONE;
152	}
153
154	init_sin6(&fromsa, m); /* general init */
155
156	LIST_FOREACH(in6p, &ripcb, inp_list) {
157		if ((in6p->in6p_vflag & INP_IPV6) == 0)
158			continue;
159		if (in6p->in6p_ip6_nxt &&
160		    in6p->in6p_ip6_nxt != proto)
161			continue;
162		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
163		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
164			continue;
165		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
166		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
167			continue;
168		if (in6p->in6p_cksum != -1) {
169			rip6stat.rip6s_isum++;
170			if (in6_cksum(m, ip6->ip6_nxt, *offp,
171			    m->m_pkthdr.len - *offp)) {
172				rip6stat.rip6s_badsum++;
173				continue;
174			}
175		}
176		if (last) {
177			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
178
179#if defined(IPSEC) || defined(FAST_IPSEC)
180			/*
181			 * Check AH/ESP integrity.
182			 */
183			if (n && ipsec6_in_reject(n, last)) {
184				m_freem(n);
185#ifdef IPSEC
186				ipsec6stat.in_polvio++;
187#endif /*IPSEC*/
188				/* do not inject data into pcb */
189			} else
190#endif /*IPSEC || FAST_IPSEC*/
191			if (n) {
192				if (last->in6p_flags & IN6P_CONTROLOPTS ||
193				    last->in6p_socket->so_options & SO_TIMESTAMP)
194					ip6_savecontrol(last, n, &opts);
195				/* strip intermediate headers */
196				m_adj(n, *offp);
197				if (sbappendaddr(&last->in6p_socket->so_rcv,
198						(struct sockaddr *)&fromsa,
199						 n, opts) == 0) {
200					m_freem(n);
201					if (opts)
202						m_freem(opts);
203					rip6stat.rip6s_fullsock++;
204				} else
205					sorwakeup(last->in6p_socket);
206				opts = NULL;
207			}
208		}
209		last = in6p;
210	}
211#if defined(IPSEC) || defined(FAST_IPSEC)
212	/*
213	 * Check AH/ESP integrity.
214	 */
215	if (last && ipsec6_in_reject(m, last)) {
216		m_freem(m);
217#ifdef IPSEC
218		ipsec6stat.in_polvio++;
219#endif /*IPSEC*/
220		ip6stat.ip6s_delivered--;
221		/* do not inject data into pcb */
222	} else
223#endif /*IPSEC || FAST_IPSEC*/
224	if (last) {
225		if (last->in6p_flags & IN6P_CONTROLOPTS ||
226		    last->in6p_socket->so_options & SO_TIMESTAMP)
227			ip6_savecontrol(last, m, &opts);
228		/* strip intermediate headers */
229		m_adj(m, *offp);
230		if (sbappendaddr(&last->in6p_socket->so_rcv,
231				(struct sockaddr *)&fromsa, m, opts) == 0) {
232			m_freem(m);
233			if (opts)
234				m_freem(opts);
235			rip6stat.rip6s_fullsock++;
236		} else
237			sorwakeup(last->in6p_socket);
238	} else {
239		rip6stat.rip6s_nosock++;
240		if (m->m_flags & M_MCAST)
241			rip6stat.rip6s_nosockmcast++;
242		if (proto == IPPROTO_NONE)
243			m_freem(m);
244		else {
245			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
246			icmp6_error(m, ICMP6_PARAM_PROB,
247				    ICMP6_PARAMPROB_NEXTHEADER,
248				    prvnxtp - mtod(m, char *));
249		}
250		ip6stat.ip6s_delivered--;
251	}
252	return IPPROTO_DONE;
253}
254
255void
256rip6_ctlinput(cmd, sa, d)
257	int cmd;
258	struct sockaddr *sa;
259	void *d;
260{
261	struct ip6_hdr *ip6;
262	struct mbuf *m;
263	int off = 0;
264	struct ip6ctlparam *ip6cp = NULL;
265	const struct sockaddr_in6 *sa6_src = NULL;
266	void *cmdarg;
267	struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange;
268
269	if (sa->sa_family != AF_INET6 ||
270	    sa->sa_len != sizeof(struct sockaddr_in6))
271		return;
272
273	if ((unsigned)cmd >= PRC_NCMDS)
274		return;
275	if (PRC_IS_REDIRECT(cmd))
276		notify = in6_rtchange, d = NULL;
277	else if (cmd == PRC_HOSTDEAD)
278		d = NULL;
279	else if (inet6ctlerrmap[cmd] == 0)
280		return;
281
282	/* if the parameter is from icmp6, decode it. */
283	if (d != NULL) {
284		ip6cp = (struct ip6ctlparam *)d;
285		m = ip6cp->ip6c_m;
286		ip6 = ip6cp->ip6c_ip6;
287		off = ip6cp->ip6c_off;
288		cmdarg = ip6cp->ip6c_cmdarg;
289		sa6_src = ip6cp->ip6c_src;
290	} else {
291		m = NULL;
292		ip6 = NULL;
293		cmdarg = NULL;
294		sa6_src = &sa6_any;
295	}
296
297	(void) in6_pcbnotify(&ripcb, sa, 0, (const struct sockaddr *)sa6_src,
298			     0, cmd, cmdarg, notify);
299}
300
301/*
302 * Generate IPv6 header and pass packet to ip6_output.
303 * Tack on options user may have setup with control call.
304 */
305int
306#if __STDC__
307rip6_output(struct mbuf *m, ...)
308#else
309rip6_output(m, va_alist)
310	struct mbuf *m;
311	va_dcl
312#endif
313{
314	struct mbuf *control;
315	struct socket *so;
316	struct sockaddr_in6 *dstsock;
317	struct in6_addr *dst;
318	struct ip6_hdr *ip6;
319	struct inpcb *in6p;
320	u_int	plen = m->m_pkthdr.len;
321	int error = 0;
322	struct ip6_pktopts opt, *stickyopt = NULL;
323	struct ifnet *oifp = NULL;
324	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
325	int priv = 0;
326	struct in6_addr *in6a;
327	va_list ap;
328
329	va_start(ap, m);
330	so = va_arg(ap, struct socket *);
331	dstsock = va_arg(ap, struct sockaddr_in6 *);
332	control = va_arg(ap, struct mbuf *);
333	va_end(ap);
334
335	in6p = sotoin6pcb(so);
336	stickyopt = in6p->in6p_outputopts;
337
338	priv = 0;
339	if (so->so_cred->cr_uid == 0)
340		priv = 1;
341	dst = &dstsock->sin6_addr;
342	if (control) {
343		if ((error = ip6_setpktoptions(control, &opt,
344					       stickyopt, priv, 0,
345					       so->so_proto->pr_protocol))
346		    != 0) {
347			goto bad;
348		}
349		in6p->in6p_outputopts = &opt;
350	}
351
352	/*
353	 * For an ICMPv6 packet, we should know its type and code
354	 * to update statistics.
355	 */
356	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
357		struct icmp6_hdr *icmp6;
358		if (m->m_len < sizeof(struct icmp6_hdr) &&
359		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
360			error = ENOBUFS;
361			goto bad;
362		}
363		icmp6 = mtod(m, struct icmp6_hdr *);
364		type = icmp6->icmp6_type;
365		code = icmp6->icmp6_code;
366	}
367
368	M_PREPEND(m, sizeof(*ip6), M_TRYWAIT);
369	ip6 = mtod(m, struct ip6_hdr *);
370
371	/*
372	 * Next header might not be ICMP6 but use its pseudo header anyway.
373	 */
374	ip6->ip6_dst = *dst;
375
376	/*
377	 * If the scope of the destination is link-local, embed the interface
378	 * index in the address.
379	 *
380	 * XXX advanced-api value overrides sin6_scope_id
381	 */
382	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
383		struct in6_pktinfo *pi;
384
385		/*
386		 * XXX Boundary check is assumed to be already done in
387		 * ip6_setpktoptions().
388		 */
389		if (in6p->in6p_outputopts &&
390		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
391		    pi->ipi6_ifindex) {
392			ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
393			oifp = ifnet_byindex(pi->ipi6_ifindex);
394		} else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
395			 in6p->in6p_moptions &&
396			 in6p->in6p_moptions->im6o_multicast_ifp) {
397			oifp = in6p->in6p_moptions->im6o_multicast_ifp;
398			ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
399		} else if (dstsock->sin6_scope_id) {
400			/* boundary check */
401			if (dstsock->sin6_scope_id < 0 ||
402			    if_index < dstsock->sin6_scope_id) {
403				error = ENXIO;  /* XXX EINVAL? */
404				goto bad;
405			}
406			ip6->ip6_dst.s6_addr16[1] =
407			    htons(dstsock->sin6_scope_id & 0xffff); /* XXX */
408		}
409	}
410
411	/*
412	 * Source address selection.
413	 */
414	if ((in6a = in6_selectsrc(dstsock, in6p->in6p_outputopts,
415	    in6p->in6p_moptions, NULL, &in6p->in6p_laddr, &error)) == 0) {
416		if (error == 0)
417			error = EADDRNOTAVAIL;
418		goto bad;
419	}
420	ip6->ip6_src = *in6a;
421	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
422		(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
423	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
424		(IPV6_VERSION & IPV6_VERSION_MASK);
425	/* ip6_plen will be filled in ip6_output, so not fill it here. */
426	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
427	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
428
429	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
430	    in6p->in6p_cksum != -1) {
431		struct mbuf *n;
432		int off;
433		u_int16_t *p;
434
435		/* compute checksum */
436		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
437			off = offsetof(struct icmp6_hdr, icmp6_cksum);
438		else
439			off = in6p->in6p_cksum;
440		if (plen < off + 1) {
441			error = EINVAL;
442			goto bad;
443		}
444		off += sizeof(struct ip6_hdr);
445
446		n = m;
447		while (n && n->m_len <= off) {
448			off -= n->m_len;
449			n = n->m_next;
450		}
451		if (!n)
452			goto bad;
453		p = (u_int16_t *)(mtod(n, caddr_t) + off);
454		*p = 0;
455		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
456	}
457
458	error = ip6_output(m, in6p->in6p_outputopts, NULL, 0,
459			   in6p->in6p_moptions, &oifp, in6p);
460	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
461		if (oifp)
462			icmp6_ifoutstat_inc(oifp, type, code);
463		icmp6stat.icp6s_outhist[type]++;
464	} else
465		rip6stat.rip6s_opackets++;
466
467	goto freectl;
468
469 bad:
470	if (m)
471		m_freem(m);
472
473 freectl:
474	if (control) {
475		ip6_clearpktopts(in6p->in6p_outputopts, -1);
476		in6p->in6p_outputopts = stickyopt;
477		m_freem(control);
478	}
479	return (error);
480}
481
482/*
483 * Raw IPv6 socket option processing.
484 */
485int
486rip6_ctloutput(so, sopt)
487	struct socket *so;
488	struct sockopt *sopt;
489{
490	int error;
491
492	if (sopt->sopt_level == IPPROTO_ICMPV6)
493		/*
494		 * XXX: is it better to call icmp6_ctloutput() directly
495		 * from protosw?
496		 */
497		return (icmp6_ctloutput(so, sopt));
498	else if (sopt->sopt_level != IPPROTO_IPV6)
499		return (EINVAL);
500
501	error = 0;
502
503	switch (sopt->sopt_dir) {
504	case SOPT_GET:
505		switch (sopt->sopt_name) {
506		case MRT6_INIT:
507		case MRT6_DONE:
508		case MRT6_ADD_MIF:
509		case MRT6_DEL_MIF:
510		case MRT6_ADD_MFC:
511		case MRT6_DEL_MFC:
512		case MRT6_PIM:
513			error = ip6_mrouter_get(so, sopt);
514			break;
515		case IPV6_CHECKSUM:
516			error = ip6_raw_ctloutput(so, sopt);
517			break;
518		default:
519			error = ip6_ctloutput(so, sopt);
520			break;
521		}
522		break;
523
524	case SOPT_SET:
525		switch (sopt->sopt_name) {
526		case MRT6_INIT:
527		case MRT6_DONE:
528		case MRT6_ADD_MIF:
529		case MRT6_DEL_MIF:
530		case MRT6_ADD_MFC:
531		case MRT6_DEL_MFC:
532		case MRT6_PIM:
533			error = ip6_mrouter_set(so, sopt);
534			break;
535		case IPV6_CHECKSUM:
536			error = ip6_raw_ctloutput(so, sopt);
537			break;
538		default:
539			error = ip6_ctloutput(so, sopt);
540			break;
541		}
542		break;
543	}
544
545	return (error);
546}
547
548static int
549rip6_attach(struct socket *so, int proto, struct thread *td)
550{
551	struct inpcb *inp;
552	int error, s;
553
554	inp = sotoinpcb(so);
555	if (inp)
556		panic("rip6_attach");
557	if (td && (error = suser(td)) != 0)
558		return error;
559
560	error = soreserve(so, rip_sendspace, rip_recvspace);
561	if (error)
562		return error;
563	s = splnet();
564	error = in_pcballoc(so, &ripcbinfo, td, "raw6inp");
565	splx(s);
566	if (error)
567		return error;
568	inp = (struct inpcb *)so->so_pcb;
569	inp->inp_vflag |= INP_IPV6;
570	inp->in6p_ip6_nxt = (long)proto;
571	inp->in6p_hops = -1;	/* use kernel default */
572	inp->in6p_cksum = -1;
573	MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
574	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
575	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
576	return 0;
577}
578
579static int
580rip6_detach(struct socket *so)
581{
582	struct inpcb *inp;
583
584	inp = sotoinpcb(so);
585	if (inp == 0)
586		panic("rip6_detach");
587	/* xxx: RSVP */
588	if (so == ip6_mrouter)
589		ip6_mrouter_done();
590	if (inp->in6p_icmp6filt) {
591		FREE(inp->in6p_icmp6filt, M_PCB);
592		inp->in6p_icmp6filt = NULL;
593	}
594	in6_pcbdetach(inp);
595	return 0;
596}
597
598static int
599rip6_abort(struct socket *so)
600{
601	soisdisconnected(so);
602	return rip6_detach(so);
603}
604
605static int
606rip6_disconnect(struct socket *so)
607{
608	struct inpcb *inp = sotoinpcb(so);
609
610	if ((so->so_state & SS_ISCONNECTED) == 0)
611		return ENOTCONN;
612	inp->in6p_faddr = in6addr_any;
613	return rip6_abort(so);
614}
615
616static int
617rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
618{
619	struct inpcb *inp = sotoinpcb(so);
620	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
621	struct ifaddr *ia = NULL;
622
623	if (nam->sa_len != sizeof(*addr))
624		return EINVAL;
625	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
626		return EADDRNOTAVAIL;
627#ifdef ENABLE_DEFAULT_SCOPE
628	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
629		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
630	}
631#endif
632	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
633	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
634		return EADDRNOTAVAIL;
635	if (ia &&
636	    ((struct in6_ifaddr *)ia)->ia6_flags &
637	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
638	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
639		return (EADDRNOTAVAIL);
640	}
641	inp->in6p_laddr = addr->sin6_addr;
642	return 0;
643}
644
645static int
646rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
647{
648	struct inpcb *inp = sotoinpcb(so);
649	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
650	struct in6_addr *in6a = NULL;
651	int error = 0;
652#ifdef ENABLE_DEFAULT_SCOPE
653	struct sockaddr_in6 tmp;
654#endif
655
656	if (nam->sa_len != sizeof(*addr))
657		return EINVAL;
658	if (TAILQ_EMPTY(&ifnet))
659		return EADDRNOTAVAIL;
660	if (addr->sin6_family != AF_INET6)
661		return EAFNOSUPPORT;
662#ifdef ENABLE_DEFAULT_SCOPE
663	if (addr->sin6_scope_id == 0) {	/* not change if specified  */
664		/* avoid overwrites */
665		tmp = *addr;
666		addr = &tmp;
667		addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
668	}
669#endif
670	/* Source address selection. XXX: need pcblookup? */
671	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
672			     inp->in6p_moptions, NULL,
673			     &inp->in6p_laddr, &error);
674	if (in6a == NULL)
675		return (error ? error : EADDRNOTAVAIL);
676	inp->in6p_laddr = *in6a;
677	inp->in6p_faddr = addr->sin6_addr;
678	soisconnected(so);
679	return 0;
680}
681
682static int
683rip6_shutdown(struct socket *so)
684{
685	socantsendmore(so);
686	return 0;
687}
688
689static int
690rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
691	 struct mbuf *control, struct thread *td)
692{
693	struct inpcb *inp = sotoinpcb(so);
694	struct sockaddr_in6 tmp;
695	struct sockaddr_in6 *dst;
696
697	/* always copy sockaddr to avoid overwrites */
698	if (so->so_state & SS_ISCONNECTED) {
699		if (nam) {
700			m_freem(m);
701			return EISCONN;
702		}
703		/* XXX */
704		bzero(&tmp, sizeof(tmp));
705		tmp.sin6_family = AF_INET6;
706		tmp.sin6_len = sizeof(struct sockaddr_in6);
707		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
708		      sizeof(struct in6_addr));
709		dst = &tmp;
710	} else {
711		if (nam == NULL) {
712			m_freem(m);
713			return ENOTCONN;
714		}
715		tmp = *(struct sockaddr_in6 *)nam;
716		dst = &tmp;
717	}
718#ifdef ENABLE_DEFAULT_SCOPE
719	if (dst->sin6_scope_id == 0) {	/* not change if specified  */
720		dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
721	}
722#endif
723	return rip6_output(m, so, dst, control);
724}
725
726struct pr_usrreqs rip6_usrreqs = {
727	rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect,
728	pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect,
729	pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp,
730	pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown,
731	in6_setsockaddr, sosend, soreceive, sopoll, pru_sosetlabel_null
732};
733