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