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