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