xref: /freebsd/sys/netinet6/raw_ip6.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
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$
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 
117 extern struct	inpcbhead ripcb;
118 extern struct	inpcbinfo ripcbinfo;
119 extern u_long	rip_sendspace;
120 extern u_long	rip_recvspace;
121 
122 struct rip6stat rip6stat;
123 
124 /*
125  * Hooks for multicast forwarding.
126  */
127 struct socket *ip6_mrouter = NULL;
128 int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
129 int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
130 int (*ip6_mrouter_done)(void);
131 int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
132 int (*mrt6_ioctl)(int, caddr_t);
133 
134 /*
135  * Setup generic address and protocol structures
136  * for raw_input routine, then pass them along with
137  * mbuf chain.
138  */
139 int
140 rip6_input(mp, offp, proto)
141 	struct	mbuf **mp;
142 	int	*offp, proto;
143 {
144 	struct mbuf *m = *mp;
145 	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
146 	register struct inpcb *in6p;
147 	struct inpcb *last = 0;
148 	struct mbuf *opts = NULL;
149 	struct sockaddr_in6 fromsa;
150 
151 	rip6stat.rip6s_ipackets++;
152 
153 	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
154 		/* XXX send icmp6 host/port unreach? */
155 		m_freem(m);
156 		return IPPROTO_DONE;
157 	}
158 
159 	init_sin6(&fromsa, m); /* general init */
160 
161 	INP_INFO_RLOCK(&ripcbinfo);
162 	LIST_FOREACH(in6p, &ripcb, inp_list) {
163 		INP_LOCK(in6p);
164 		if ((in6p->in6p_vflag & INP_IPV6) == 0) {
165 docontinue:
166 			INP_UNLOCK(in6p);
167 			continue;
168 		}
169 		if (in6p->in6p_ip6_nxt &&
170 		    in6p->in6p_ip6_nxt != proto)
171 			goto docontinue;
172 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
173 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
174 			goto docontinue;
175 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
176 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
177 			goto docontinue;
178 		if (in6p->in6p_cksum != -1) {
179 			rip6stat.rip6s_isum++;
180 			if (in6_cksum(m, proto, *offp,
181 			    m->m_pkthdr.len - *offp)) {
182 				rip6stat.rip6s_badsum++;
183 				goto docontinue;
184 			}
185 		}
186 		if (last) {
187 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
188 
189 #if defined(IPSEC) || defined(FAST_IPSEC)
190 			/*
191 			 * Check AH/ESP integrity.
192 			 */
193 			if (n && ipsec6_in_reject(n, last)) {
194 				m_freem(n);
195 #ifdef IPSEC
196 				ipsec6stat.in_polvio++;
197 #endif /*IPSEC*/
198 				/* do not inject data into pcb */
199 			} else
200 #endif /*IPSEC || FAST_IPSEC*/
201 			if (n) {
202 				if (last->in6p_flags & IN6P_CONTROLOPTS ||
203 				    last->in6p_socket->so_options & SO_TIMESTAMP)
204 					ip6_savecontrol(last, n, &opts);
205 				/* strip intermediate headers */
206 				m_adj(n, *offp);
207 				if (sbappendaddr(&last->in6p_socket->so_rcv,
208 						(struct sockaddr *)&fromsa,
209 						 n, opts) == 0) {
210 					m_freem(n);
211 					if (opts)
212 						m_freem(opts);
213 					rip6stat.rip6s_fullsock++;
214 				} else
215 					sorwakeup(last->in6p_socket);
216 				opts = NULL;
217 			}
218 			INP_UNLOCK(last);
219 		}
220 		last = in6p;
221 	}
222 #if defined(IPSEC) || defined(FAST_IPSEC)
223 	/*
224 	 * Check AH/ESP integrity.
225 	 */
226 	if (last && ipsec6_in_reject(m, last)) {
227 		m_freem(m);
228 #ifdef IPSEC
229 		ipsec6stat.in_polvio++;
230 #endif /*IPSEC*/
231 		ip6stat.ip6s_delivered--;
232 		/* do not inject data into pcb */
233 		INP_UNLOCK(last);
234 	} else
235 #endif /*IPSEC || FAST_IPSEC*/
236 	if (last) {
237 		if (last->in6p_flags & IN6P_CONTROLOPTS ||
238 		    last->in6p_socket->so_options & SO_TIMESTAMP)
239 			ip6_savecontrol(last, m, &opts);
240 		/* strip intermediate headers */
241 		m_adj(m, *offp);
242 		if (sbappendaddr(&last->in6p_socket->so_rcv,
243 				(struct sockaddr *)&fromsa, m, opts) == 0) {
244 			m_freem(m);
245 			if (opts)
246 				m_freem(opts);
247 			rip6stat.rip6s_fullsock++;
248 		} else
249 			sorwakeup(last->in6p_socket);
250 		INP_UNLOCK(last);
251 	} else {
252 		rip6stat.rip6s_nosock++;
253 		if (m->m_flags & M_MCAST)
254 			rip6stat.rip6s_nosockmcast++;
255 		if (proto == IPPROTO_NONE)
256 			m_freem(m);
257 		else {
258 			char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
259 			icmp6_error(m, ICMP6_PARAM_PROB,
260 				    ICMP6_PARAMPROB_NEXTHEADER,
261 				    prvnxtp - mtod(m, char *));
262 		}
263 		ip6stat.ip6s_delivered--;
264 	}
265 	INP_INFO_RUNLOCK(&ripcbinfo);
266 	return IPPROTO_DONE;
267 }
268 
269 void
270 rip6_ctlinput(cmd, sa, d)
271 	int cmd;
272 	struct sockaddr *sa;
273 	void *d;
274 {
275 	struct ip6_hdr *ip6;
276 	struct mbuf *m;
277 	int off = 0;
278 	struct ip6ctlparam *ip6cp = NULL;
279 	const struct sockaddr_in6 *sa6_src = NULL;
280 	void *cmdarg;
281 	struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange;
282 
283 	if (sa->sa_family != AF_INET6 ||
284 	    sa->sa_len != sizeof(struct sockaddr_in6))
285 		return;
286 
287 	if ((unsigned)cmd >= PRC_NCMDS)
288 		return;
289 	if (PRC_IS_REDIRECT(cmd))
290 		notify = in6_rtchange, d = NULL;
291 	else if (cmd == PRC_HOSTDEAD)
292 		d = NULL;
293 	else if (inet6ctlerrmap[cmd] == 0)
294 		return;
295 
296 	/* if the parameter is from icmp6, decode it. */
297 	if (d != NULL) {
298 		ip6cp = (struct ip6ctlparam *)d;
299 		m = ip6cp->ip6c_m;
300 		ip6 = ip6cp->ip6c_ip6;
301 		off = ip6cp->ip6c_off;
302 		cmdarg = ip6cp->ip6c_cmdarg;
303 		sa6_src = ip6cp->ip6c_src;
304 	} else {
305 		m = NULL;
306 		ip6 = NULL;
307 		cmdarg = NULL;
308 		sa6_src = &sa6_any;
309 	}
310 
311 	(void) in6_pcbnotify(&ripcbinfo, sa, 0,
312 			     (const struct sockaddr *)sa6_src,
313 			     0, cmd, cmdarg, notify);
314 }
315 
316 /*
317  * Generate IPv6 header and pass packet to ip6_output.
318  * Tack on options user may have setup with control call.
319  */
320 int
321 #if __STDC__
322 rip6_output(struct mbuf *m, ...)
323 #else
324 rip6_output(m, va_alist)
325 	struct mbuf *m;
326 	va_dcl
327 #endif
328 {
329 	struct mbuf *control;
330 	struct socket *so;
331 	struct sockaddr_in6 *dstsock;
332 	struct in6_addr *dst;
333 	struct ip6_hdr *ip6;
334 	struct inpcb *in6p;
335 	u_int	plen = m->m_pkthdr.len;
336 	int error = 0;
337 	struct ip6_pktopts opt, *optp;
338 	struct ifnet *oifp = NULL;
339 	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
340 	int priv = 0;
341 	int scope_ambiguous = 0;
342 	struct in6_addr *in6a;
343 	va_list ap;
344 
345 	va_start(ap, m);
346 	so = va_arg(ap, struct socket *);
347 	dstsock = va_arg(ap, struct sockaddr_in6 *);
348 	control = va_arg(ap, struct mbuf *);
349 	va_end(ap);
350 
351 	in6p = sotoin6pcb(so);
352 	INP_LOCK(in6p);
353 
354 	priv = 0;
355 	if (suser_cred(so->so_cred, SUSER_ALLOWJAIL) == 0)
356 		priv = 1;
357 	dst = &dstsock->sin6_addr;
358 	if (control) {
359 		if ((error = ip6_setpktopts(control, &opt,
360 		    in6p->in6p_outputopts, priv, so->so_proto->pr_protocol))
361 		    != 0) {
362 			goto bad;
363 		}
364 		optp = &opt;
365 	} else
366 		optp = in6p->in6p_outputopts;
367 
368 	/*
369 	 * Check and convert scope zone ID into internal form.
370 	 * XXX: we may still need to determine the zone later.
371 	 */
372 	if (!(so->so_state & SS_ISCONNECTED)) {
373 		if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
374 			scope_ambiguous = 1;
375 		if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
376 			goto bad;
377 	}
378 
379 	/*
380 	 * For an ICMPv6 packet, we should know its type and code
381 	 * to update statistics.
382 	 */
383 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
384 		struct icmp6_hdr *icmp6;
385 		if (m->m_len < sizeof(struct icmp6_hdr) &&
386 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
387 			error = ENOBUFS;
388 			goto bad;
389 		}
390 		icmp6 = mtod(m, struct icmp6_hdr *);
391 		type = icmp6->icmp6_type;
392 		code = icmp6->icmp6_code;
393 	}
394 
395 	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
396 	if (m == NULL) {
397 		error = ENOBUFS;
398 		goto bad;
399 	}
400 	ip6 = mtod(m, struct ip6_hdr *);
401 
402 	/*
403 	 * Source address selection.
404 	 */
405 	if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL,
406 	    &in6p->in6p_laddr, &oifp, &error)) == NULL) {
407 		if (error == 0)
408 			error = EADDRNOTAVAIL;
409 		goto bad;
410 	}
411 	ip6->ip6_src = *in6a;
412 
413 	if (oifp && scope_ambiguous) {
414 		/*
415 		 * Application should provide a proper zone ID or the use of
416 		 * default zone IDs should be enabled.  Unfortunately, some
417 		 * applications do not behave as it should, so we need a
418 		 * workaround.  Even if an appropriate ID is not determined
419 		 * (when it's required), if we can determine the outgoing
420 		 * interface. determine the zone ID based on the interface.
421 		 */
422 		error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
423 		if (error != 0)
424 			goto bad;
425 	}
426 	ip6->ip6_dst = dstsock->sin6_addr;
427 
428 	/* fill in the rest of the IPv6 header fields */
429 	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
430 		(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
431 	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
432 		(IPV6_VERSION & IPV6_VERSION_MASK);
433 	/* ip6_plen will be filled in ip6_output, so not fill it here. */
434 	ip6->ip6_nxt = in6p->in6p_ip6_nxt;
435 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
436 
437 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
438 	    in6p->in6p_cksum != -1) {
439 		struct mbuf *n;
440 		int off;
441 		u_int16_t *p;
442 
443 		/* compute checksum */
444 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
445 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
446 		else
447 			off = in6p->in6p_cksum;
448 		if (plen < off + 1) {
449 			error = EINVAL;
450 			goto bad;
451 		}
452 		off += sizeof(struct ip6_hdr);
453 
454 		n = m;
455 		while (n && n->m_len <= off) {
456 			off -= n->m_len;
457 			n = n->m_next;
458 		}
459 		if (!n)
460 			goto bad;
461 		p = (u_int16_t *)(mtod(n, caddr_t) + off);
462 		*p = 0;
463 		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
464 	}
465 
466 	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
467 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
468 		if (oifp)
469 			icmp6_ifoutstat_inc(oifp, type, code);
470 		icmp6stat.icp6s_outhist[type]++;
471 	} else
472 		rip6stat.rip6s_opackets++;
473 
474 	goto freectl;
475 
476  bad:
477 	if (m)
478 		m_freem(m);
479 
480  freectl:
481 	if (control) {
482 		ip6_clearpktopts(&opt, -1);
483 		m_freem(control);
484 	}
485 	INP_UNLOCK(in6p);
486 	return (error);
487 }
488 
489 /*
490  * Raw IPv6 socket option processing.
491  */
492 int
493 rip6_ctloutput(so, sopt)
494 	struct socket *so;
495 	struct sockopt *sopt;
496 {
497 	int error;
498 
499 	if (sopt->sopt_level == IPPROTO_ICMPV6)
500 		/*
501 		 * XXX: is it better to call icmp6_ctloutput() directly
502 		 * from protosw?
503 		 */
504 		return (icmp6_ctloutput(so, sopt));
505 	else if (sopt->sopt_level != IPPROTO_IPV6)
506 		return (EINVAL);
507 
508 	error = 0;
509 
510 	switch (sopt->sopt_dir) {
511 	case SOPT_GET:
512 		switch (sopt->sopt_name) {
513 		case MRT6_INIT:
514 		case MRT6_DONE:
515 		case MRT6_ADD_MIF:
516 		case MRT6_DEL_MIF:
517 		case MRT6_ADD_MFC:
518 		case MRT6_DEL_MFC:
519 		case MRT6_PIM:
520 			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
521 			    EOPNOTSUPP;
522 			break;
523 		case IPV6_CHECKSUM:
524 			error = ip6_raw_ctloutput(so, sopt);
525 			break;
526 		default:
527 			error = ip6_ctloutput(so, sopt);
528 			break;
529 		}
530 		break;
531 
532 	case SOPT_SET:
533 		switch (sopt->sopt_name) {
534 		case MRT6_INIT:
535 		case MRT6_DONE:
536 		case MRT6_ADD_MIF:
537 		case MRT6_DEL_MIF:
538 		case MRT6_ADD_MFC:
539 		case MRT6_DEL_MFC:
540 		case MRT6_PIM:
541 			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
542 			    EOPNOTSUPP;
543 			break;
544 		case IPV6_CHECKSUM:
545 			error = ip6_raw_ctloutput(so, sopt);
546 			break;
547 		default:
548 			error = ip6_ctloutput(so, sopt);
549 			break;
550 		}
551 		break;
552 	}
553 
554 	return (error);
555 }
556 
557 static int
558 rip6_attach(struct socket *so, int proto, struct thread *td)
559 {
560 	struct inpcb *inp;
561 	struct icmp6_filter *filter;
562 	int error;
563 
564 	inp = sotoinpcb(so);
565 	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
566 	if (td && (error = suser(td)) != 0)
567 		return error;
568 	error = soreserve(so, rip_sendspace, rip_recvspace);
569 	if (error)
570 		return error;
571 	MALLOC(filter, struct icmp6_filter *,
572 	       sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
573 	if (filter == NULL)
574 		return ENOMEM;
575 	INP_INFO_WLOCK(&ripcbinfo);
576 	error = in_pcballoc(so, &ripcbinfo);
577 	if (error) {
578 		INP_INFO_WUNLOCK(&ripcbinfo);
579 		FREE(filter, M_PCB);
580 		return error;
581 	}
582 	inp = (struct inpcb *)so->so_pcb;
583 	INP_INFO_WUNLOCK(&ripcbinfo);
584 	inp->inp_vflag |= INP_IPV6;
585 	inp->in6p_ip6_nxt = (long)proto;
586 	inp->in6p_hops = -1;	/* use kernel default */
587 	inp->in6p_cksum = -1;
588 	inp->in6p_icmp6filt = filter;
589 	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
590 	INP_UNLOCK(inp);
591 	return 0;
592 }
593 
594 static void
595 rip6_detach(struct socket *so)
596 {
597 	struct inpcb *inp;
598 
599 	inp = sotoinpcb(so);
600 	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
601 
602 	if (so == ip6_mrouter && ip6_mrouter_done)
603 		ip6_mrouter_done();
604 	/* xxx: RSVP */
605 	INP_INFO_WLOCK(&ripcbinfo);
606 	INP_LOCK(inp);
607 	if (inp->in6p_icmp6filt) {
608 		FREE(inp->in6p_icmp6filt, M_PCB);
609 		inp->in6p_icmp6filt = NULL;
610 	}
611 	in6_pcbdetach(inp);
612 	in6_pcbfree(inp);
613 	INP_INFO_WUNLOCK(&ripcbinfo);
614 }
615 
616 /* XXXRW: This can't ever be called. */
617 static void
618 rip6_abort(struct socket *so)
619 {
620 	struct inpcb *inp;
621 
622 	inp = sotoinpcb(so);
623 	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
624 
625 	soisdisconnected(so);
626 }
627 
628 static void
629 rip6_close(struct socket *so)
630 {
631 	struct inpcb *inp;
632 
633 	inp = sotoinpcb(so);
634 	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
635 
636 	soisdisconnected(so);
637 }
638 
639 static int
640 rip6_disconnect(struct socket *so)
641 {
642 	struct inpcb *inp = sotoinpcb(so);
643 
644 	if ((so->so_state & SS_ISCONNECTED) == 0)
645 		return ENOTCONN;
646 	inp->in6p_faddr = in6addr_any;
647 	rip6_abort(so);
648 	return (0);
649 }
650 
651 static int
652 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
653 {
654 	struct inpcb *inp = sotoinpcb(so);
655 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
656 	struct ifaddr *ia = NULL;
657 	int error = 0;
658 
659 	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
660 	if (nam->sa_len != sizeof(*addr))
661 		return EINVAL;
662 	if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
663 		return EADDRNOTAVAIL;
664 	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
665 		return(error);
666 
667 	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
668 	    (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
669 		return EADDRNOTAVAIL;
670 	if (ia &&
671 	    ((struct in6_ifaddr *)ia)->ia6_flags &
672 	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
673 	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
674 		return (EADDRNOTAVAIL);
675 	}
676 	INP_INFO_WLOCK(&ripcbinfo);
677 	INP_LOCK(inp);
678 	inp->in6p_laddr = addr->sin6_addr;
679 	INP_UNLOCK(inp);
680 	INP_INFO_WUNLOCK(&ripcbinfo);
681 	return 0;
682 }
683 
684 static int
685 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
686 {
687 	struct inpcb *inp = sotoinpcb(so);
688 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
689 	struct in6_addr *in6a = NULL;
690 	struct ifnet *ifp = NULL;
691 	int error = 0, scope_ambiguous = 0;
692 
693 	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
694 	if (nam->sa_len != sizeof(*addr))
695 		return EINVAL;
696 	if (TAILQ_EMPTY(&ifnet))
697 		return EADDRNOTAVAIL;
698 	if (addr->sin6_family != AF_INET6)
699 		return EAFNOSUPPORT;
700 
701 	/*
702 	 * Application should provide a proper zone ID or the use of
703 	 * default zone IDs should be enabled.  Unfortunately, some
704 	 * applications do not behave as it should, so we need a
705 	 * workaround.  Even if an appropriate ID is not determined,
706 	 * we'll see if we can determine the outgoing interface.  If we
707 	 * can, determine the zone ID based on the interface below.
708 	 */
709 	if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
710 		scope_ambiguous = 1;
711 	if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
712 		return(error);
713 
714 	INP_INFO_WLOCK(&ripcbinfo);
715 	INP_LOCK(inp);
716 	/* Source address selection. XXX: need pcblookup? */
717 	in6a = in6_selectsrc(addr, inp->in6p_outputopts,
718 			     inp->in6p_moptions, NULL,
719 			     &inp->in6p_laddr, &ifp, &error);
720 	if (in6a == NULL) {
721 		INP_UNLOCK(inp);
722 		INP_INFO_WUNLOCK(&ripcbinfo);
723 		return (error ? error : EADDRNOTAVAIL);
724 	}
725 
726 	/* XXX: see above */
727 	if (ifp && scope_ambiguous &&
728 	    (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
729 		INP_UNLOCK(inp);
730 		INP_INFO_WUNLOCK(&ripcbinfo);
731 		return(error);
732 	}
733 	inp->in6p_faddr = addr->sin6_addr;
734 	inp->in6p_laddr = *in6a;
735 	soisconnected(so);
736 	INP_UNLOCK(inp);
737 	INP_INFO_WUNLOCK(&ripcbinfo);
738 	return 0;
739 }
740 
741 static int
742 rip6_shutdown(struct socket *so)
743 {
744 	struct inpcb *inp;
745 
746 	inp = sotoinpcb(so);
747 	KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
748 	INP_LOCK(inp);
749 	socantsendmore(so);
750 	INP_UNLOCK(inp);
751 	return 0;
752 }
753 
754 static int
755 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
756 	 struct mbuf *control, struct thread *td)
757 {
758 	struct inpcb *inp = sotoinpcb(so);
759 	struct sockaddr_in6 tmp;
760 	struct sockaddr_in6 *dst;
761 	int ret;
762 
763 	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
764 	INP_INFO_WLOCK(&ripcbinfo);
765 	/* always copy sockaddr to avoid overwrites */
766 	/* Unlocked read. */
767 	if (so->so_state & SS_ISCONNECTED) {
768 		if (nam) {
769 			INP_INFO_WUNLOCK(&ripcbinfo);
770 			m_freem(m);
771 			return EISCONN;
772 		}
773 		/* XXX */
774 		bzero(&tmp, sizeof(tmp));
775 		tmp.sin6_family = AF_INET6;
776 		tmp.sin6_len = sizeof(struct sockaddr_in6);
777 		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
778 		      sizeof(struct in6_addr));
779 		dst = &tmp;
780 	} else {
781 		if (nam == NULL) {
782 			INP_INFO_WUNLOCK(&ripcbinfo);
783 			m_freem(m);
784 			return ENOTCONN;
785 		}
786 		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
787 			INP_INFO_WUNLOCK(&ripcbinfo);
788 			m_freem(m);
789 			return(EINVAL);
790 		}
791 		tmp = *(struct sockaddr_in6 *)nam;
792 		dst = &tmp;
793 
794 		if (dst->sin6_family == AF_UNSPEC) {
795 			/*
796 			 * XXX: we allow this case for backward
797 			 * compatibility to buggy applications that
798 			 * rely on old (and wrong) kernel behavior.
799 			 */
800 			log(LOG_INFO, "rip6 SEND: address family is "
801 			    "unspec. Assume AF_INET6\n");
802 			dst->sin6_family = AF_INET6;
803 		} else if (dst->sin6_family != AF_INET6) {
804 			INP_INFO_WUNLOCK(&ripcbinfo);
805 			m_freem(m);
806 			return(EAFNOSUPPORT);
807 		}
808 	}
809 	ret = rip6_output(m, so, dst, control);
810 	INP_INFO_WUNLOCK(&ripcbinfo);
811 	return (ret);
812 }
813 
814 struct pr_usrreqs rip6_usrreqs = {
815 	.pru_abort =		rip6_abort,
816 	.pru_attach =		rip6_attach,
817 	.pru_bind =		rip6_bind,
818 	.pru_connect =		rip6_connect,
819 	.pru_control =		in6_control,
820 	.pru_detach =		rip6_detach,
821 	.pru_disconnect =	rip6_disconnect,
822 	.pru_peeraddr =		in6_setpeeraddr,
823 	.pru_send =		rip6_send,
824 	.pru_shutdown =		rip6_shutdown,
825 	.pru_sockaddr =		in6_setsockaddr,
826 	.pru_close =		rip6_close,
827 };
828