xref: /freebsd/sys/netinet6/raw_ip6.c (revision 780fb4a2)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1982, 1986, 1988, 1993
34  *	The Regents of the University of California.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)raw_ip.c	8.2 (Berkeley) 1/4/94
62  */
63 
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
66 
67 #include "opt_ipsec.h"
68 #include "opt_inet6.h"
69 
70 #include <sys/param.h>
71 #include <sys/errno.h>
72 #include <sys/jail.h>
73 #include <sys/kernel.h>
74 #include <sys/lock.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/priv.h>
78 #include <sys/proc.h>
79 #include <sys/protosw.h>
80 #include <sys/signalvar.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/sx.h>
84 #include <sys/syslog.h>
85 
86 #include <net/if.h>
87 #include <net/if_var.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
90 #include <net/vnet.h>
91 
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_pcb.h>
96 
97 #include <netinet/icmp6.h>
98 #include <netinet/ip6.h>
99 #include <netinet/ip_var.h>
100 #include <netinet6/ip6protosw.h>
101 #include <netinet6/ip6_mroute.h>
102 #include <netinet6/in6_pcb.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/nd6.h>
105 #include <netinet6/raw_ip6.h>
106 #include <netinet6/scope6_var.h>
107 #include <netinet6/send.h>
108 
109 #include <netipsec/ipsec_support.h>
110 
111 #include <machine/stdarg.h>
112 
113 #define	satosin6(sa)	((struct sockaddr_in6 *)(sa))
114 #define	ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
115 
116 /*
117  * Raw interface to IP6 protocol.
118  */
119 
120 VNET_DECLARE(struct inpcbhead, ripcb);
121 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
122 #define	V_ripcb				VNET(ripcb)
123 #define	V_ripcbinfo			VNET(ripcbinfo)
124 
125 extern u_long	rip_sendspace;
126 extern u_long	rip_recvspace;
127 
128 VNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat);
129 VNET_PCPUSTAT_SYSINIT(rip6stat);
130 
131 #ifdef VIMAGE
132 VNET_PCPUSTAT_SYSUNINIT(rip6stat);
133 #endif /* VIMAGE */
134 
135 /*
136  * Hooks for multicast routing. They all default to NULL, so leave them not
137  * initialized and rely on BSS being set to 0.
138  */
139 
140 /*
141  * The socket used to communicate with the multicast routing daemon.
142  */
143 VNET_DEFINE(struct socket *, ip6_mrouter);
144 
145 /*
146  * The various mrouter functions.
147  */
148 int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
149 int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
150 int (*ip6_mrouter_done)(void);
151 int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
152 int (*mrt6_ioctl)(u_long, caddr_t);
153 
154 /*
155  * Setup generic address and protocol structures for raw_input routine, then
156  * pass them along with mbuf chain.
157  */
158 int
159 rip6_input(struct mbuf **mp, int *offp, int proto)
160 {
161 	struct ifnet *ifp;
162 	struct mbuf *m = *mp;
163 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
164 	struct inpcb *in6p;
165 	struct inpcb *last = NULL;
166 	struct mbuf *opts = NULL;
167 	struct sockaddr_in6 fromsa;
168 	struct epoch_tracker et;
169 
170 	RIP6STAT_INC(rip6s_ipackets);
171 
172 	init_sin6(&fromsa, m, 0); /* general init */
173 
174 	ifp = m->m_pkthdr.rcvif;
175 
176 	INP_INFO_RLOCK_ET(&V_ripcbinfo, et);
177 	CK_LIST_FOREACH(in6p, &V_ripcb, inp_list) {
178 		/* XXX inp locking */
179 		if ((in6p->inp_vflag & INP_IPV6) == 0)
180 			continue;
181 		if (in6p->inp_ip_p &&
182 		    in6p->inp_ip_p != proto)
183 			continue;
184 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
185 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
186 			continue;
187 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
188 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
189 			continue;
190 		if (jailed_without_vnet(in6p->inp_cred)) {
191 			/*
192 			 * Allow raw socket in jail to receive multicast;
193 			 * assume process had PRIV_NETINET_RAW at attach,
194 			 * and fall through into normal filter path if so.
195 			 */
196 			if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
197 			    prison_check_ip6(in6p->inp_cred,
198 			    &ip6->ip6_dst) != 0)
199 				continue;
200 		}
201 		INP_RLOCK(in6p);
202 		if (in6p->in6p_cksum != -1) {
203 			RIP6STAT_INC(rip6s_isum);
204 			if (in6_cksum(m, proto, *offp,
205 			    m->m_pkthdr.len - *offp)) {
206 				INP_RUNLOCK(in6p);
207 				RIP6STAT_INC(rip6s_badsum);
208 				continue;
209 			}
210 		}
211 		/*
212 		 * If this raw socket has multicast state, and we
213 		 * have received a multicast, check if this socket
214 		 * should receive it, as multicast filtering is now
215 		 * the responsibility of the transport layer.
216 		 */
217 		if (in6p->in6p_moptions &&
218 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
219 			/*
220 			 * If the incoming datagram is for MLD, allow it
221 			 * through unconditionally to the raw socket.
222 			 *
223 			 * Use the M_RTALERT_MLD flag to check for MLD
224 			 * traffic without having to inspect the mbuf chain
225 			 * more deeply, as all MLDv1/v2 host messages MUST
226 			 * contain the Router Alert option.
227 			 *
228 			 * In the case of MLDv1, we may not have explicitly
229 			 * joined the group, and may have set IFF_ALLMULTI
230 			 * on the interface. im6o_mc_filter() may discard
231 			 * control traffic we actually need to see.
232 			 *
233 			 * Userland multicast routing daemons should continue
234 			 * filter the control traffic appropriately.
235 			 */
236 			int blocked;
237 
238 			blocked = MCAST_PASS;
239 			if ((m->m_flags & M_RTALERT_MLD) == 0) {
240 				struct sockaddr_in6 mcaddr;
241 
242 				bzero(&mcaddr, sizeof(struct sockaddr_in6));
243 				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
244 				mcaddr.sin6_family = AF_INET6;
245 				mcaddr.sin6_addr = ip6->ip6_dst;
246 
247 				blocked = im6o_mc_filter(in6p->in6p_moptions,
248 				    ifp,
249 				    (struct sockaddr *)&mcaddr,
250 				    (struct sockaddr *)&fromsa);
251 			}
252 			if (blocked != MCAST_PASS) {
253 				IP6STAT_INC(ip6s_notmember);
254 				INP_RUNLOCK(in6p);
255 				continue;
256 			}
257 		}
258 		if (last != NULL) {
259 			struct mbuf *n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
260 
261 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
262 			/*
263 			 * Check AH/ESP integrity.
264 			 */
265 			if (IPSEC_ENABLED(ipv6)) {
266 				if (n != NULL &&
267 				    IPSEC_CHECK_POLICY(ipv6, n, last) != 0) {
268 					m_freem(n);
269 					/* Do not inject data into pcb. */
270 					n = NULL;
271 				}
272 			}
273 #endif /* IPSEC */
274 			if (n) {
275 				if (last->inp_flags & INP_CONTROLOPTS ||
276 				    last->inp_socket->so_options & SO_TIMESTAMP)
277 					ip6_savecontrol(last, n, &opts);
278 				/* strip intermediate headers */
279 				m_adj(n, *offp);
280 				if (sbappendaddr(&last->inp_socket->so_rcv,
281 						(struct sockaddr *)&fromsa,
282 						 n, opts) == 0) {
283 					m_freem(n);
284 					if (opts)
285 						m_freem(opts);
286 					RIP6STAT_INC(rip6s_fullsock);
287 				} else
288 					sorwakeup(last->inp_socket);
289 				opts = NULL;
290 			}
291 			INP_RUNLOCK(last);
292 		}
293 		last = in6p;
294 	}
295 	INP_INFO_RUNLOCK_ET(&V_ripcbinfo, et);
296 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
297 	/*
298 	 * Check AH/ESP integrity.
299 	 */
300 	if (IPSEC_ENABLED(ipv6) && last != NULL &&
301 	    IPSEC_CHECK_POLICY(ipv6, m, last) != 0) {
302 		m_freem(m);
303 		IP6STAT_DEC(ip6s_delivered);
304 		/* Do not inject data into pcb. */
305 		INP_RUNLOCK(last);
306 	} else
307 #endif /* IPSEC */
308 	if (last != NULL) {
309 		if (last->inp_flags & INP_CONTROLOPTS ||
310 		    last->inp_socket->so_options & SO_TIMESTAMP)
311 			ip6_savecontrol(last, m, &opts);
312 		/* Strip intermediate headers. */
313 		m_adj(m, *offp);
314 		if (sbappendaddr(&last->inp_socket->so_rcv,
315 		    (struct sockaddr *)&fromsa, m, opts) == 0) {
316 			m_freem(m);
317 			if (opts)
318 				m_freem(opts);
319 			RIP6STAT_INC(rip6s_fullsock);
320 		} else
321 			sorwakeup(last->inp_socket);
322 		INP_RUNLOCK(last);
323 	} else {
324 		RIP6STAT_INC(rip6s_nosock);
325 		if (m->m_flags & M_MCAST)
326 			RIP6STAT_INC(rip6s_nosockmcast);
327 		if (proto == IPPROTO_NONE)
328 			m_freem(m);
329 		else
330 			icmp6_error(m, ICMP6_PARAM_PROB,
331 			    ICMP6_PARAMPROB_NEXTHEADER,
332 			    ip6_get_prevhdr(m, *offp));
333 		IP6STAT_DEC(ip6s_delivered);
334 	}
335 	return (IPPROTO_DONE);
336 }
337 
338 void
339 rip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
340 {
341 	struct ip6ctlparam *ip6cp = NULL;
342 	const struct sockaddr_in6 *sa6_src = NULL;
343 	void *cmdarg;
344 	struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
345 
346 	if (sa->sa_family != AF_INET6 ||
347 	    sa->sa_len != sizeof(struct sockaddr_in6))
348 		return;
349 
350 	if ((unsigned)cmd >= PRC_NCMDS)
351 		return;
352 	if (PRC_IS_REDIRECT(cmd))
353 		notify = in6_rtchange, d = NULL;
354 	else if (cmd == PRC_HOSTDEAD)
355 		d = NULL;
356 	else if (inet6ctlerrmap[cmd] == 0)
357 		return;
358 
359 	/*
360 	 * If the parameter is from icmp6, decode it.
361 	 */
362 	if (d != NULL) {
363 		ip6cp = (struct ip6ctlparam *)d;
364 		cmdarg = ip6cp->ip6c_cmdarg;
365 		sa6_src = ip6cp->ip6c_src;
366 	} else {
367 		cmdarg = NULL;
368 		sa6_src = &sa6_any;
369 	}
370 
371 	(void) in6_pcbnotify(&V_ripcbinfo, sa, 0,
372 	    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
373 }
374 
375 /*
376  * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
377  * may have setup with control call.
378  */
379 int
380 rip6_output(struct mbuf *m, struct socket *so, ...)
381 {
382 	struct mbuf *control;
383 	struct m_tag *mtag;
384 	struct sockaddr_in6 *dstsock;
385 	struct ip6_hdr *ip6;
386 	struct inpcb *in6p;
387 	u_int	plen = m->m_pkthdr.len;
388 	int error = 0;
389 	struct ip6_pktopts opt, *optp;
390 	struct ifnet *oifp = NULL;
391 	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
392 	int scope_ambiguous = 0;
393 	int use_defzone = 0;
394 	int hlim = 0;
395 	struct in6_addr in6a;
396 	va_list ap;
397 
398 	va_start(ap, so);
399 	dstsock = va_arg(ap, struct sockaddr_in6 *);
400 	control = va_arg(ap, struct mbuf *);
401 	va_end(ap);
402 
403 	in6p = sotoinpcb(so);
404 	INP_WLOCK(in6p);
405 
406 	if (control != NULL) {
407 		if ((error = ip6_setpktopts(control, &opt,
408 		    in6p->in6p_outputopts, so->so_cred,
409 		    so->so_proto->pr_protocol)) != 0) {
410 			goto bad;
411 		}
412 		optp = &opt;
413 	} else
414 		optp = in6p->in6p_outputopts;
415 
416 	/*
417 	 * Check and convert scope zone ID into internal form.
418 	 *
419 	 * XXX: we may still need to determine the zone later.
420 	 */
421 	if (!(so->so_state & SS_ISCONNECTED)) {
422 		if (!optp || !optp->ip6po_pktinfo ||
423 		    !optp->ip6po_pktinfo->ipi6_ifindex)
424 			use_defzone = V_ip6_use_defzone;
425 		if (dstsock->sin6_scope_id == 0 && !use_defzone)
426 			scope_ambiguous = 1;
427 		if ((error = sa6_embedscope(dstsock, use_defzone)) != 0)
428 			goto bad;
429 	}
430 
431 	/*
432 	 * For an ICMPv6 packet, we should know its type and code to update
433 	 * statistics.
434 	 */
435 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
436 		struct icmp6_hdr *icmp6;
437 		if (m->m_len < sizeof(struct icmp6_hdr) &&
438 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
439 			error = ENOBUFS;
440 			goto bad;
441 		}
442 		icmp6 = mtod(m, struct icmp6_hdr *);
443 		type = icmp6->icmp6_type;
444 		code = icmp6->icmp6_code;
445 	}
446 
447 	M_PREPEND(m, sizeof(*ip6), M_NOWAIT);
448 	if (m == NULL) {
449 		error = ENOBUFS;
450 		goto bad;
451 	}
452 	ip6 = mtod(m, struct ip6_hdr *);
453 
454 	/*
455 	 * Source address selection.
456 	 */
457 	error = in6_selectsrc_socket(dstsock, optp, in6p, so->so_cred,
458 	    scope_ambiguous, &in6a, &hlim);
459 
460 	if (error)
461 		goto bad;
462 	error = prison_check_ip6(in6p->inp_cred, &in6a);
463 	if (error != 0)
464 		goto bad;
465 	ip6->ip6_src = in6a;
466 
467 	ip6->ip6_dst = dstsock->sin6_addr;
468 
469 	/*
470 	 * Fill in the rest of the IPv6 header fields.
471 	 */
472 	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
473 	    (in6p->inp_flow & IPV6_FLOWINFO_MASK);
474 	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
475 	    (IPV6_VERSION & IPV6_VERSION_MASK);
476 
477 	/*
478 	 * ip6_plen will be filled in ip6_output, so not fill it here.
479 	 */
480 	ip6->ip6_nxt = in6p->inp_ip_p;
481 	ip6->ip6_hlim = hlim;
482 
483 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
484 	    in6p->in6p_cksum != -1) {
485 		struct mbuf *n;
486 		int off;
487 		u_int16_t *p;
488 
489 		/* Compute checksum. */
490 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
491 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
492 		else
493 			off = in6p->in6p_cksum;
494 		if (plen < off + 1) {
495 			error = EINVAL;
496 			goto bad;
497 		}
498 		off += sizeof(struct ip6_hdr);
499 
500 		n = m;
501 		while (n && n->m_len <= off) {
502 			off -= n->m_len;
503 			n = n->m_next;
504 		}
505 		if (!n)
506 			goto bad;
507 		p = (u_int16_t *)(mtod(n, caddr_t) + off);
508 		*p = 0;
509 		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
510 	}
511 
512 	/*
513 	 * Send RA/RS messages to user land for protection, before sending
514 	 * them to rtadvd/rtsol.
515 	 */
516 	if ((send_sendso_input_hook != NULL) &&
517 	    so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
518 		switch (type) {
519 		case ND_ROUTER_ADVERT:
520 		case ND_ROUTER_SOLICIT:
521 			mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
522 				sizeof(unsigned short), M_NOWAIT);
523 			if (mtag == NULL)
524 				goto bad;
525 			m_tag_prepend(m, mtag);
526 		}
527 	}
528 
529 	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
530 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
531 		if (oifp)
532 			icmp6_ifoutstat_inc(oifp, type, code);
533 		ICMP6STAT_INC(icp6s_outhist[type]);
534 	} else
535 		RIP6STAT_INC(rip6s_opackets);
536 
537 	goto freectl;
538 
539  bad:
540 	if (m)
541 		m_freem(m);
542 
543  freectl:
544 	if (control != NULL) {
545 		ip6_clearpktopts(&opt, -1);
546 		m_freem(control);
547 	}
548 	INP_WUNLOCK(in6p);
549 	return (error);
550 }
551 
552 /*
553  * Raw IPv6 socket option processing.
554  */
555 int
556 rip6_ctloutput(struct socket *so, struct sockopt *sopt)
557 {
558 	struct inpcb *inp;
559 	int error;
560 
561 	if (sopt->sopt_level == IPPROTO_ICMPV6)
562 		/*
563 		 * XXX: is it better to call icmp6_ctloutput() directly
564 		 * from protosw?
565 		 */
566 		return (icmp6_ctloutput(so, sopt));
567 	else if (sopt->sopt_level != IPPROTO_IPV6) {
568 		if (sopt->sopt_level == SOL_SOCKET &&
569 		    sopt->sopt_name == SO_SETFIB) {
570 			inp = sotoinpcb(so);
571 			INP_WLOCK(inp);
572 			inp->inp_inc.inc_fibnum = so->so_fibnum;
573 			INP_WUNLOCK(inp);
574 			return (0);
575 		}
576 		return (EINVAL);
577 	}
578 
579 	error = 0;
580 
581 	switch (sopt->sopt_dir) {
582 	case SOPT_GET:
583 		switch (sopt->sopt_name) {
584 		case MRT6_INIT:
585 		case MRT6_DONE:
586 		case MRT6_ADD_MIF:
587 		case MRT6_DEL_MIF:
588 		case MRT6_ADD_MFC:
589 		case MRT6_DEL_MFC:
590 		case MRT6_PIM:
591 			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
592 			    EOPNOTSUPP;
593 			break;
594 		case IPV6_CHECKSUM:
595 			error = ip6_raw_ctloutput(so, sopt);
596 			break;
597 		default:
598 			error = ip6_ctloutput(so, sopt);
599 			break;
600 		}
601 		break;
602 
603 	case SOPT_SET:
604 		switch (sopt->sopt_name) {
605 		case MRT6_INIT:
606 		case MRT6_DONE:
607 		case MRT6_ADD_MIF:
608 		case MRT6_DEL_MIF:
609 		case MRT6_ADD_MFC:
610 		case MRT6_DEL_MFC:
611 		case MRT6_PIM:
612 			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
613 			    EOPNOTSUPP;
614 			break;
615 		case IPV6_CHECKSUM:
616 			error = ip6_raw_ctloutput(so, sopt);
617 			break;
618 		default:
619 			error = ip6_ctloutput(so, sopt);
620 			break;
621 		}
622 		break;
623 	}
624 
625 	return (error);
626 }
627 
628 static int
629 rip6_attach(struct socket *so, int proto, struct thread *td)
630 {
631 	struct inpcb *inp;
632 	struct icmp6_filter *filter;
633 	int error;
634 
635 	inp = sotoinpcb(so);
636 	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
637 
638 	error = priv_check(td, PRIV_NETINET_RAW);
639 	if (error)
640 		return (error);
641 	error = soreserve(so, rip_sendspace, rip_recvspace);
642 	if (error)
643 		return (error);
644 	filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
645 	if (filter == NULL)
646 		return (ENOMEM);
647 	INP_INFO_WLOCK(&V_ripcbinfo);
648 	error = in_pcballoc(so, &V_ripcbinfo);
649 	if (error) {
650 		INP_INFO_WUNLOCK(&V_ripcbinfo);
651 		free(filter, M_PCB);
652 		return (error);
653 	}
654 	inp = (struct inpcb *)so->so_pcb;
655 	INP_INFO_WUNLOCK(&V_ripcbinfo);
656 	inp->inp_vflag |= INP_IPV6;
657 	inp->inp_ip_p = (long)proto;
658 	inp->in6p_hops = -1;	/* use kernel default */
659 	inp->in6p_cksum = -1;
660 	inp->in6p_icmp6filt = filter;
661 	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
662 	INP_WUNLOCK(inp);
663 	return (0);
664 }
665 
666 static void
667 rip6_detach(struct socket *so)
668 {
669 	struct inpcb *inp;
670 
671 	inp = sotoinpcb(so);
672 	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
673 
674 	if (so == V_ip6_mrouter && ip6_mrouter_done)
675 		ip6_mrouter_done();
676 	/* xxx: RSVP */
677 	INP_INFO_WLOCK(&V_ripcbinfo);
678 	INP_WLOCK(inp);
679 	free(inp->in6p_icmp6filt, M_PCB);
680 	in_pcbdetach(inp);
681 	in_pcbfree(inp);
682 	INP_INFO_WUNLOCK(&V_ripcbinfo);
683 }
684 
685 /* XXXRW: This can't ever be called. */
686 static void
687 rip6_abort(struct socket *so)
688 {
689 	struct inpcb *inp;
690 
691 	inp = sotoinpcb(so);
692 	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
693 
694 	soisdisconnected(so);
695 }
696 
697 static void
698 rip6_close(struct socket *so)
699 {
700 	struct inpcb *inp;
701 
702 	inp = sotoinpcb(so);
703 	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
704 
705 	soisdisconnected(so);
706 }
707 
708 static int
709 rip6_disconnect(struct socket *so)
710 {
711 	struct inpcb *inp;
712 
713 	inp = sotoinpcb(so);
714 	KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
715 
716 	if ((so->so_state & SS_ISCONNECTED) == 0)
717 		return (ENOTCONN);
718 	inp->in6p_faddr = in6addr_any;
719 	rip6_abort(so);
720 	return (0);
721 }
722 
723 static int
724 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
725 {
726 	struct inpcb *inp;
727 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
728 	struct ifaddr *ifa = NULL;
729 	int error = 0;
730 
731 	inp = sotoinpcb(so);
732 	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
733 
734 	if (nam->sa_len != sizeof(*addr))
735 		return (EINVAL);
736 	if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
737 		return (error);
738 	if (CK_STAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
739 		return (EADDRNOTAVAIL);
740 	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
741 		return (error);
742 
743 	NET_EPOCH_ENTER();
744 	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
745 	    (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) {
746 		NET_EPOCH_EXIT();
747 		return (EADDRNOTAVAIL);
748 	}
749 	if (ifa != NULL &&
750 	    ((struct in6_ifaddr *)ifa)->ia6_flags &
751 	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
752 	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
753 		NET_EPOCH_EXIT();
754 		return (EADDRNOTAVAIL);
755 	}
756 	NET_EPOCH_EXIT();
757 	INP_INFO_WLOCK(&V_ripcbinfo);
758 	INP_WLOCK(inp);
759 	inp->in6p_laddr = addr->sin6_addr;
760 	INP_WUNLOCK(inp);
761 	INP_INFO_WUNLOCK(&V_ripcbinfo);
762 	return (0);
763 }
764 
765 static int
766 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
767 {
768 	struct inpcb *inp;
769 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
770 	struct in6_addr in6a;
771 	int error = 0, scope_ambiguous = 0;
772 
773 	inp = sotoinpcb(so);
774 	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
775 
776 	if (nam->sa_len != sizeof(*addr))
777 		return (EINVAL);
778 	if (CK_STAILQ_EMPTY(&V_ifnet))
779 		return (EADDRNOTAVAIL);
780 	if (addr->sin6_family != AF_INET6)
781 		return (EAFNOSUPPORT);
782 
783 	/*
784 	 * Application should provide a proper zone ID or the use of default
785 	 * zone IDs should be enabled.  Unfortunately, some applications do
786 	 * not behave as it should, so we need a workaround.  Even if an
787 	 * appropriate ID is not determined, we'll see if we can determine
788 	 * the outgoing interface.  If we can, determine the zone ID based on
789 	 * the interface below.
790 	 */
791 	if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
792 		scope_ambiguous = 1;
793 	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
794 		return (error);
795 
796 	INP_INFO_WLOCK(&V_ripcbinfo);
797 	INP_WLOCK(inp);
798 	/* Source address selection. XXX: need pcblookup? */
799 	error = in6_selectsrc_socket(addr, inp->in6p_outputopts,
800 	    inp, so->so_cred, scope_ambiguous, &in6a, NULL);
801 	if (error) {
802 		INP_WUNLOCK(inp);
803 		INP_INFO_WUNLOCK(&V_ripcbinfo);
804 		return (error);
805 	}
806 
807 	inp->in6p_faddr = addr->sin6_addr;
808 	inp->in6p_laddr = in6a;
809 	soisconnected(so);
810 	INP_WUNLOCK(inp);
811 	INP_INFO_WUNLOCK(&V_ripcbinfo);
812 	return (0);
813 }
814 
815 static int
816 rip6_shutdown(struct socket *so)
817 {
818 	struct inpcb *inp;
819 
820 	inp = sotoinpcb(so);
821 	KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
822 
823 	INP_WLOCK(inp);
824 	socantsendmore(so);
825 	INP_WUNLOCK(inp);
826 	return (0);
827 }
828 
829 static int
830 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
831     struct mbuf *control, struct thread *td)
832 {
833 	struct inpcb *inp;
834 	struct sockaddr_in6 tmp;
835 	struct sockaddr_in6 *dst;
836 	int ret;
837 
838 	inp = sotoinpcb(so);
839 	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
840 
841 	/* Always copy sockaddr to avoid overwrites. */
842 	/* Unlocked read. */
843 	if (so->so_state & SS_ISCONNECTED) {
844 		if (nam) {
845 			m_freem(m);
846 			return (EISCONN);
847 		}
848 		/* XXX */
849 		bzero(&tmp, sizeof(tmp));
850 		tmp.sin6_family = AF_INET6;
851 		tmp.sin6_len = sizeof(struct sockaddr_in6);
852 		INP_RLOCK(inp);
853 		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
854 		    sizeof(struct in6_addr));
855 		INP_RUNLOCK(inp);
856 		dst = &tmp;
857 	} else {
858 		if (nam == NULL) {
859 			m_freem(m);
860 			return (ENOTCONN);
861 		}
862 		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
863 			m_freem(m);
864 			return (EINVAL);
865 		}
866 		tmp = *(struct sockaddr_in6 *)nam;
867 		dst = &tmp;
868 
869 		if (dst->sin6_family == AF_UNSPEC) {
870 			/*
871 			 * XXX: we allow this case for backward
872 			 * compatibility to buggy applications that
873 			 * rely on old (and wrong) kernel behavior.
874 			 */
875 			log(LOG_INFO, "rip6 SEND: address family is "
876 			    "unspec. Assume AF_INET6\n");
877 			dst->sin6_family = AF_INET6;
878 		} else if (dst->sin6_family != AF_INET6) {
879 			m_freem(m);
880 			return(EAFNOSUPPORT);
881 		}
882 	}
883 	ret = rip6_output(m, so, dst, control);
884 	return (ret);
885 }
886 
887 struct pr_usrreqs rip6_usrreqs = {
888 	.pru_abort =		rip6_abort,
889 	.pru_attach =		rip6_attach,
890 	.pru_bind =		rip6_bind,
891 	.pru_connect =		rip6_connect,
892 	.pru_control =		in6_control,
893 	.pru_detach =		rip6_detach,
894 	.pru_disconnect =	rip6_disconnect,
895 	.pru_peeraddr =		in6_getpeeraddr,
896 	.pru_send =		rip6_send,
897 	.pru_shutdown =		rip6_shutdown,
898 	.pru_sockaddr =		in6_getsockaddr,
899 	.pru_close =		rip6_close,
900 };
901