raw_ip6.c revision 148385
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 148385 2005-07-25 12:31:43Z 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 * 4. Neither the name of the University nor the names of its contributors
45 *    may be used to endorse or promote products derived from this software
46 *    without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
61 */
62
63#include "opt_ipsec.h"
64#include "opt_inet6.h"
65
66#include <sys/param.h>
67#include <sys/errno.h>
68#include <sys/lock.h>
69#include <sys/malloc.h>
70#include <sys/mbuf.h>
71#include <sys/proc.h>
72#include <sys/protosw.h>
73#include <sys/signalvar.h>
74#include <sys/socket.h>
75#include <sys/socketvar.h>
76#include <sys/sx.h>
77#include <sys/systm.h>
78#include <sys/syslog.h>
79
80#include <net/if.h>
81#include <net/if_types.h>
82#include <net/route.h>
83
84#include <netinet/in.h>
85#include <netinet/in_var.h>
86#include <netinet/in_systm.h>
87#include <netinet/icmp6.h>
88#include <netinet/in_pcb.h>
89#include <netinet/ip6.h>
90#include <netinet6/ip6protosw.h>
91#include <netinet6/ip6_mroute.h>
92#include <netinet6/in6_pcb.h>
93#include <netinet6/ip6_var.h>
94#include <netinet6/nd6.h>
95#include <netinet6/raw_ip6.h>
96#include <netinet6/scope6_var.h>
97
98#ifdef IPSEC
99#include <netinet6/ipsec.h>
100#include <netinet6/ipsec6.h>
101#endif /*IPSEC*/
102
103#ifdef FAST_IPSEC
104#include <netipsec/ipsec.h>
105#include <netipsec/ipsec6.h>
106#endif /* FAST_IPSEC */
107
108#include <machine/stdarg.h>
109
110#define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
111#define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
112
113/*
114 * Raw interface to IP6 protocol.
115 */
116
117extern struct	inpcbhead ripcb;
118extern struct	inpcbinfo ripcbinfo;
119extern u_long	rip_sendspace;
120extern u_long	rip_recvspace;
121
122struct rip6stat rip6stat;
123
124/*
125 * Setup generic address and protocol structures
126 * for raw_input routine, then pass them along with
127 * mbuf chain.
128 */
129int
130rip6_input(mp, offp, proto)
131	struct	mbuf **mp;
132	int	*offp, proto;
133{
134	struct mbuf *m = *mp;
135	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
136	register struct inpcb *in6p;
137	struct inpcb *last = 0;
138	struct mbuf *opts = NULL;
139	struct sockaddr_in6 fromsa;
140
141	rip6stat.rip6s_ipackets++;
142
143	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
144		/* XXX send icmp6 host/port unreach? */
145		m_freem(m);
146		return IPPROTO_DONE;
147	}
148
149	init_sin6(&fromsa, m); /* general init */
150
151	INP_INFO_RLOCK(&ripcbinfo);
152	LIST_FOREACH(in6p, &ripcb, inp_list) {
153		INP_LOCK(in6p);
154		if ((in6p->in6p_vflag & INP_IPV6) == 0) {
155docontinue:
156			INP_UNLOCK(in6p);
157			continue;
158		}
159		if (in6p->in6p_ip6_nxt &&
160		    in6p->in6p_ip6_nxt != proto)
161			goto docontinue;
162		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
163		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
164			goto docontinue;
165		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
166		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
167			goto docontinue;
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				goto docontinue;
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			INP_UNLOCK(last);
209		}
210		last = in6p;
211	}
212#if defined(IPSEC) || defined(FAST_IPSEC)
213	/*
214	 * Check AH/ESP integrity.
215	 */
216	if (last && ipsec6_in_reject(m, last)) {
217		m_freem(m);
218#ifdef IPSEC
219		ipsec6stat.in_polvio++;
220#endif /*IPSEC*/
221		ip6stat.ip6s_delivered--;
222		/* do not inject data into pcb */
223	} else
224#endif /*IPSEC || FAST_IPSEC*/
225	if (last) {
226		if (last->in6p_flags & IN6P_CONTROLOPTS ||
227		    last->in6p_socket->so_options & SO_TIMESTAMP)
228			ip6_savecontrol(last, m, &opts);
229		/* strip intermediate headers */
230		m_adj(m, *offp);
231		if (sbappendaddr(&last->in6p_socket->so_rcv,
232				(struct sockaddr *)&fromsa, m, opts) == 0) {
233			m_freem(m);
234			if (opts)
235				m_freem(opts);
236			rip6stat.rip6s_fullsock++;
237		} else
238			sorwakeup(last->in6p_socket);
239		INP_UNLOCK(last);
240	} else {
241		rip6stat.rip6s_nosock++;
242		if (m->m_flags & M_MCAST)
243			rip6stat.rip6s_nosockmcast++;
244		if (proto == IPPROTO_NONE)
245			m_freem(m);
246		else {
247			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
248			icmp6_error(m, ICMP6_PARAM_PROB,
249				    ICMP6_PARAMPROB_NEXTHEADER,
250				    prvnxtp - mtod(m, char *));
251		}
252		ip6stat.ip6s_delivered--;
253	}
254	INP_INFO_RUNLOCK(&ripcbinfo);
255	return IPPROTO_DONE;
256}
257
258void
259rip6_ctlinput(cmd, sa, d)
260	int cmd;
261	struct sockaddr *sa;
262	void *d;
263{
264	struct ip6_hdr *ip6;
265	struct mbuf *m;
266	int off = 0;
267	struct ip6ctlparam *ip6cp = NULL;
268	const struct sockaddr_in6 *sa6_src = NULL;
269	void *cmdarg;
270	struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange;
271
272	if (sa->sa_family != AF_INET6 ||
273	    sa->sa_len != sizeof(struct sockaddr_in6))
274		return;
275
276	if ((unsigned)cmd >= PRC_NCMDS)
277		return;
278	if (PRC_IS_REDIRECT(cmd))
279		notify = in6_rtchange, d = NULL;
280	else if (cmd == PRC_HOSTDEAD)
281		d = NULL;
282	else if (inet6ctlerrmap[cmd] == 0)
283		return;
284
285	/* if the parameter is from icmp6, decode it. */
286	if (d != NULL) {
287		ip6cp = (struct ip6ctlparam *)d;
288		m = ip6cp->ip6c_m;
289		ip6 = ip6cp->ip6c_ip6;
290		off = ip6cp->ip6c_off;
291		cmdarg = ip6cp->ip6c_cmdarg;
292		sa6_src = ip6cp->ip6c_src;
293	} else {
294		m = NULL;
295		ip6 = NULL;
296		cmdarg = NULL;
297		sa6_src = &sa6_any;
298	}
299
300	(void) in6_pcbnotify(&ripcbinfo, sa, 0,
301			     (const struct sockaddr *)sa6_src,
302			     0, cmd, cmdarg, notify);
303}
304
305/*
306 * Generate IPv6 header and pass packet to ip6_output.
307 * Tack on options user may have setup with control call.
308 */
309int
310#if __STDC__
311rip6_output(struct mbuf *m, ...)
312#else
313rip6_output(m, va_alist)
314	struct mbuf *m;
315	va_dcl
316#endif
317{
318	struct mbuf *control;
319	struct socket *so;
320	struct sockaddr_in6 *dstsock;
321	struct in6_addr *dst;
322	struct ip6_hdr *ip6;
323	struct inpcb *in6p;
324	u_int	plen = m->m_pkthdr.len;
325	int error = 0;
326	struct ip6_pktopts opt, *optp;
327	struct ifnet *oifp = NULL;
328	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
329	int priv = 0;
330	int scope_ambiguous = 0;
331	struct in6_addr *in6a;
332	va_list ap;
333
334	va_start(ap, m);
335	so = va_arg(ap, struct socket *);
336	dstsock = va_arg(ap, struct sockaddr_in6 *);
337	control = va_arg(ap, struct mbuf *);
338	va_end(ap);
339
340	in6p = sotoin6pcb(so);
341	INP_LOCK(in6p);
342
343	priv = 0;
344	if (so->so_cred->cr_uid == 0)
345		priv = 1;
346	dst = &dstsock->sin6_addr;
347	if (control) {
348		if ((error = ip6_setpktopts(control, &opt,
349		    in6p->in6p_outputopts, priv, so->so_proto->pr_protocol))
350		    != 0) {
351			goto bad;
352		}
353		optp = &opt;
354	} else
355		optp = in6p->in6p_outputopts;
356
357	/*
358	 * Check and convert scope zone ID into internal form.
359	 * XXX: we may still need to determine the zone later.
360	 */
361	if (!(so->so_state & SS_ISCONNECTED)) {
362		if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
363			scope_ambiguous = 1;
364		if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
365			goto bad;
366	}
367
368	/*
369	 * For an ICMPv6 packet, we should know its type and code
370	 * to update statistics.
371	 */
372	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
373		struct icmp6_hdr *icmp6;
374		if (m->m_len < sizeof(struct icmp6_hdr) &&
375		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
376			error = ENOBUFS;
377			goto bad;
378		}
379		icmp6 = mtod(m, struct icmp6_hdr *);
380		type = icmp6->icmp6_type;
381		code = icmp6->icmp6_code;
382	}
383
384	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
385	if (m == NULL) {
386		error = ENOBUFS;
387		goto bad;
388	}
389	ip6 = mtod(m, struct ip6_hdr *);
390
391	/*
392	 * Source address selection.
393	 */
394	if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL,
395	    &in6p->in6p_laddr, &oifp, &error)) == NULL) {
396		if (error == 0)
397			error = EADDRNOTAVAIL;
398		goto bad;
399	}
400	ip6->ip6_src = *in6a;
401
402	if (oifp && scope_ambiguous) {
403		/*
404		 * Application should provide a proper zone ID or the use of
405		 * default zone IDs should be enabled.  Unfortunately, some
406		 * applications do not behave as it should, so we need a
407		 * workaround.  Even if an appropriate ID is not determined
408		 * (when it's required), if we can determine the outgoing
409		 * interface. determine the zone ID based on the interface.
410		 */
411		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
412		if (error != 0)
413			goto bad;
414	}
415	ip6->ip6_dst = dstsock->sin6_addr;
416
417	/* fill in the rest of the IPv6 header fields */
418	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
419		(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
420	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
421		(IPV6_VERSION & IPV6_VERSION_MASK);
422	/* ip6_plen will be filled in ip6_output, so not fill it here. */
423	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
424	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
425
426	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
427	    in6p->in6p_cksum != -1) {
428		struct mbuf *n;
429		int off;
430		u_int16_t *p;
431
432		/* compute checksum */
433		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
434			off = offsetof(struct icmp6_hdr, icmp6_cksum);
435		else
436			off = in6p->in6p_cksum;
437		if (plen < off + 1) {
438			error = EINVAL;
439			goto bad;
440		}
441		off += sizeof(struct ip6_hdr);
442
443		n = m;
444		while (n && n->m_len <= off) {
445			off -= n->m_len;
446			n = n->m_next;
447		}
448		if (!n)
449			goto bad;
450		p = (u_int16_t *)(mtod(n, caddr_t) + off);
451		*p = 0;
452		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
453	}
454
455	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
456	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
457		if (oifp)
458			icmp6_ifoutstat_inc(oifp, type, code);
459		icmp6stat.icp6s_outhist[type]++;
460	} else
461		rip6stat.rip6s_opackets++;
462
463	goto freectl;
464
465 bad:
466	if (m)
467		m_freem(m);
468
469 freectl:
470	if (control) {
471		ip6_clearpktopts(&opt, -1);
472		m_freem(control);
473	}
474	INP_UNLOCK(in6p);
475	return (error);
476}
477
478/*
479 * Raw IPv6 socket option processing.
480 */
481int
482rip6_ctloutput(so, sopt)
483	struct socket *so;
484	struct sockopt *sopt;
485{
486	int error;
487
488	if (sopt->sopt_level == IPPROTO_ICMPV6)
489		/*
490		 * XXX: is it better to call icmp6_ctloutput() directly
491		 * from protosw?
492		 */
493		return (icmp6_ctloutput(so, sopt));
494	else if (sopt->sopt_level != IPPROTO_IPV6)
495		return (EINVAL);
496
497	error = 0;
498
499	switch (sopt->sopt_dir) {
500	case SOPT_GET:
501		switch (sopt->sopt_name) {
502		case MRT6_INIT:
503		case MRT6_DONE:
504		case MRT6_ADD_MIF:
505		case MRT6_DEL_MIF:
506		case MRT6_ADD_MFC:
507		case MRT6_DEL_MFC:
508		case MRT6_PIM:
509			error = ip6_mrouter_get(so, sopt);
510			break;
511		case IPV6_CHECKSUM:
512			error = ip6_raw_ctloutput(so, sopt);
513			break;
514		default:
515			error = ip6_ctloutput(so, sopt);
516			break;
517		}
518		break;
519
520	case SOPT_SET:
521		switch (sopt->sopt_name) {
522		case MRT6_INIT:
523		case MRT6_DONE:
524		case MRT6_ADD_MIF:
525		case MRT6_DEL_MIF:
526		case MRT6_ADD_MFC:
527		case MRT6_DEL_MFC:
528		case MRT6_PIM:
529			error = ip6_mrouter_set(so, sopt);
530			break;
531		case IPV6_CHECKSUM:
532			error = ip6_raw_ctloutput(so, sopt);
533			break;
534		default:
535			error = ip6_ctloutput(so, sopt);
536			break;
537		}
538		break;
539	}
540
541	return (error);
542}
543
544static int
545rip6_attach(struct socket *so, int proto, struct thread *td)
546{
547	struct inpcb *inp;
548	struct icmp6_filter *filter;
549	int error, s;
550
551	INP_INFO_WLOCK(&ripcbinfo);
552	inp = sotoinpcb(so);
553	if (inp) {
554		INP_INFO_WUNLOCK(&ripcbinfo);
555		panic("rip6_attach");
556	}
557	if (td && (error = suser(td)) != 0) {
558		INP_INFO_WUNLOCK(&ripcbinfo);
559		return error;
560	}
561	error = soreserve(so, rip_sendspace, rip_recvspace);
562	if (error) {
563		INP_INFO_WUNLOCK(&ripcbinfo);
564		return error;
565	}
566	MALLOC(filter, struct icmp6_filter *,
567	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
568	if (filter == NULL)
569		return ENOMEM;
570	s = splnet();
571	error = in_pcballoc(so, &ripcbinfo, "raw6inp");
572	splx(s);
573	if (error) {
574		INP_INFO_WUNLOCK(&ripcbinfo);
575		FREE(filter, M_PCB);
576		return error;
577	}
578	inp = (struct inpcb *)so->so_pcb;
579	INP_LOCK(inp);
580	INP_INFO_WUNLOCK(&ripcbinfo);
581	inp->inp_vflag |= INP_IPV6;
582	inp->in6p_ip6_nxt = (long)proto;
583	inp->in6p_hops = -1;	/* use kernel default */
584	inp->in6p_cksum = -1;
585	inp->in6p_icmp6filt = filter;
586	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
587	INP_UNLOCK(inp);
588	return 0;
589}
590
591static int
592rip6_detach(struct socket *so)
593{
594	struct inpcb *inp;
595
596	INP_INFO_WLOCK(&ripcbinfo);
597	inp = sotoinpcb(so);
598	if (inp == 0) {
599		INP_INFO_WUNLOCK(&ripcbinfo);
600		panic("rip6_detach");
601	}
602	/* xxx: RSVP */
603	if (so == ip6_mrouter)
604		ip6_mrouter_done();
605	if (inp->in6p_icmp6filt) {
606		FREE(inp->in6p_icmp6filt, M_PCB);
607		inp->in6p_icmp6filt = NULL;
608	}
609	INP_LOCK(inp);
610	in6_pcbdetach(inp);
611	INP_INFO_WUNLOCK(&ripcbinfo);
612	return 0;
613}
614
615static int
616rip6_abort(struct socket *so)
617{
618	soisdisconnected(so);
619	return rip6_detach(so);
620}
621
622static int
623rip6_disconnect(struct socket *so)
624{
625	struct inpcb *inp = sotoinpcb(so);
626
627	if ((so->so_state & SS_ISCONNECTED) == 0)
628		return ENOTCONN;
629	inp->in6p_faddr = in6addr_any;
630	return rip6_abort(so);
631}
632
633static int
634rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
635{
636	struct inpcb *inp = sotoinpcb(so);
637	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
638	struct ifaddr *ia = NULL;
639	int error = 0;
640
641	if (nam->sa_len != sizeof(*addr))
642		return EINVAL;
643	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
644		return EADDRNOTAVAIL;
645	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
646		return(error);
647
648	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
649	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
650		return EADDRNOTAVAIL;
651	if (ia &&
652	    ((struct in6_ifaddr *)ia)->ia6_flags &
653	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
654	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
655		return (EADDRNOTAVAIL);
656	}
657	INP_INFO_WLOCK(&ripcbinfo);
658	INP_LOCK(inp);
659	inp->in6p_laddr = addr->sin6_addr;
660	INP_UNLOCK(inp);
661	INP_INFO_WUNLOCK(&ripcbinfo);
662	return 0;
663}
664
665static int
666rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
667{
668	struct inpcb *inp = sotoinpcb(so);
669	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
670	struct in6_addr *in6a = NULL;
671	struct ifnet *ifp = NULL;
672	int error = 0, scope_ambiguous = 0;
673
674	if (nam->sa_len != sizeof(*addr))
675		return EINVAL;
676	if (TAILQ_EMPTY(&ifnet))
677		return EADDRNOTAVAIL;
678	if (addr->sin6_family != AF_INET6)
679		return EAFNOSUPPORT;
680
681	/*
682	 * Application should provide a proper zone ID or the use of
683	 * default zone IDs should be enabled.  Unfortunately, some
684	 * applications do not behave as it should, so we need a
685	 * workaround.  Even if an appropriate ID is not determined,
686	 * we'll see if we can determine the outgoing interface.  If we
687	 * can, determine the zone ID based on the interface below.
688	 */
689	if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
690		scope_ambiguous = 1;
691	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
692		return(error);
693
694	INP_INFO_WLOCK(&ripcbinfo);
695	INP_LOCK(inp);
696	/* Source address selection. XXX: need pcblookup? */
697	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
698			     inp->in6p_moptions, NULL,
699			     &inp->in6p_laddr, &ifp, &error);
700	if (in6a == NULL) {
701		INP_UNLOCK(inp);
702		INP_INFO_WUNLOCK(&ripcbinfo);
703		return (error ? error : EADDRNOTAVAIL);
704	}
705
706	/* XXX: see above */
707	if (ifp && scope_ambiguous &&
708	    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
709		INP_UNLOCK(inp);
710		INP_INFO_WUNLOCK(&ripcbinfo);
711		return(error);
712	}
713	inp->in6p_faddr = addr->sin6_addr;
714	inp->in6p_laddr = *in6a;
715	soisconnected(so);
716	INP_UNLOCK(inp);
717	INP_INFO_WUNLOCK(&ripcbinfo);
718	return 0;
719}
720
721static int
722rip6_shutdown(struct socket *so)
723{
724	struct inpcb *inp;
725
726	INP_INFO_RLOCK(&ripcbinfo);
727	inp = sotoinpcb(so);
728	INP_LOCK(inp);
729	INP_INFO_RUNLOCK(&ripcbinfo);
730	socantsendmore(so);
731	INP_UNLOCK(inp);
732	return 0;
733}
734
735static int
736rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
737	 struct mbuf *control, struct thread *td)
738{
739	struct inpcb *inp = sotoinpcb(so);
740	struct sockaddr_in6 tmp;
741	struct sockaddr_in6 *dst;
742	int ret;
743
744	INP_INFO_WLOCK(&ripcbinfo);
745	/* always copy sockaddr to avoid overwrites */
746	/* Unlocked read. */
747	if (so->so_state & SS_ISCONNECTED) {
748		if (nam) {
749			INP_INFO_WUNLOCK(&ripcbinfo);
750			m_freem(m);
751			return EISCONN;
752		}
753		/* XXX */
754		bzero(&tmp, sizeof(tmp));
755		tmp.sin6_family = AF_INET6;
756		tmp.sin6_len = sizeof(struct sockaddr_in6);
757		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
758		      sizeof(struct in6_addr));
759		dst = &tmp;
760	} else {
761		if (nam == NULL) {
762			INP_INFO_WUNLOCK(&ripcbinfo);
763			m_freem(m);
764			return ENOTCONN;
765		}
766		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
767			INP_INFO_WUNLOCK(&ripcbinfo);
768			m_freem(m);
769			return(EINVAL);
770		}
771		tmp = *(struct sockaddr_in6 *)nam;
772		dst = &tmp;
773
774		if (dst->sin6_family == AF_UNSPEC) {
775			/*
776			 * XXX: we allow this case for backward
777			 * compatibility to buggy applications that
778			 * rely on old (and wrong) kernel behavior.
779			 */
780			log(LOG_INFO, "rip6 SEND: address family is "
781			    "unspec. Assume AF_INET6\n");
782			dst->sin6_family = AF_INET6;
783		} else if (dst->sin6_family != AF_INET6) {
784			INP_INFO_WUNLOCK(&ripcbinfo);
785			m_freem(m);
786			return(EAFNOSUPPORT);
787		}
788	}
789	ret = rip6_output(m, so, dst, control);
790	INP_INFO_WUNLOCK(&ripcbinfo);
791	return (ret);
792}
793
794struct pr_usrreqs rip6_usrreqs = {
795	.pru_abort =		rip6_abort,
796	.pru_attach =		rip6_attach,
797	.pru_bind =		rip6_bind,
798	.pru_connect =		rip6_connect,
799	.pru_control =		in6_control,
800	.pru_detach =		rip6_detach,
801	.pru_disconnect =	rip6_disconnect,
802	.pru_peeraddr =		in6_setpeeraddr,
803	.pru_send =		rip6_send,
804	.pru_shutdown =		rip6_shutdown,
805	.pru_sockaddr =		in6_setsockaddr,
806};
807