xref: /freebsd/sys/netinet6/raw_ip6.c (revision f39bffc62c1395bde25d152c7f68fdf7cbaab414)
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 
169 	RIP6STAT_INC(rip6s_ipackets);
170 
171 	init_sin6(&fromsa, m, 0); /* general init */
172 
173 	ifp = m->m_pkthdr.rcvif;
174 
175 	INP_INFO_RLOCK(&V_ripcbinfo);
176 	CK_LIST_FOREACH(in6p, &V_ripcb, inp_list) {
177 		/* XXX inp locking */
178 		if ((in6p->inp_vflag & INP_IPV6) == 0)
179 			continue;
180 		if (in6p->inp_ip_p &&
181 		    in6p->inp_ip_p != proto)
182 			continue;
183 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
184 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
185 			continue;
186 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
187 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
188 			continue;
189 		if (jailed_without_vnet(in6p->inp_cred)) {
190 			/*
191 			 * Allow raw socket in jail to receive multicast;
192 			 * assume process had PRIV_NETINET_RAW at attach,
193 			 * and fall through into normal filter path if so.
194 			 */
195 			if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
196 			    prison_check_ip6(in6p->inp_cred,
197 			    &ip6->ip6_dst) != 0)
198 				continue;
199 		}
200 		INP_RLOCK(in6p);
201 		if (in6p->in6p_cksum != -1) {
202 			RIP6STAT_INC(rip6s_isum);
203 			if (in6_cksum(m, proto, *offp,
204 			    m->m_pkthdr.len - *offp)) {
205 				INP_RUNLOCK(in6p);
206 				RIP6STAT_INC(rip6s_badsum);
207 				continue;
208 			}
209 		}
210 		/*
211 		 * If this raw socket has multicast state, and we
212 		 * have received a multicast, check if this socket
213 		 * should receive it, as multicast filtering is now
214 		 * the responsibility of the transport layer.
215 		 */
216 		if (in6p->in6p_moptions &&
217 		    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
218 			/*
219 			 * If the incoming datagram is for MLD, allow it
220 			 * through unconditionally to the raw socket.
221 			 *
222 			 * Use the M_RTALERT_MLD flag to check for MLD
223 			 * traffic without having to inspect the mbuf chain
224 			 * more deeply, as all MLDv1/v2 host messages MUST
225 			 * contain the Router Alert option.
226 			 *
227 			 * In the case of MLDv1, we may not have explicitly
228 			 * joined the group, and may have set IFF_ALLMULTI
229 			 * on the interface. im6o_mc_filter() may discard
230 			 * control traffic we actually need to see.
231 			 *
232 			 * Userland multicast routing daemons should continue
233 			 * filter the control traffic appropriately.
234 			 */
235 			int blocked;
236 
237 			blocked = MCAST_PASS;
238 			if ((m->m_flags & M_RTALERT_MLD) == 0) {
239 				struct sockaddr_in6 mcaddr;
240 
241 				bzero(&mcaddr, sizeof(struct sockaddr_in6));
242 				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
243 				mcaddr.sin6_family = AF_INET6;
244 				mcaddr.sin6_addr = ip6->ip6_dst;
245 
246 				blocked = im6o_mc_filter(in6p->in6p_moptions,
247 				    ifp,
248 				    (struct sockaddr *)&mcaddr,
249 				    (struct sockaddr *)&fromsa);
250 			}
251 			if (blocked != MCAST_PASS) {
252 				IP6STAT_INC(ip6s_notmember);
253 				INP_RUNLOCK(in6p);
254 				continue;
255 			}
256 		}
257 		if (last != NULL) {
258 			struct mbuf *n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
259 
260 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
261 			/*
262 			 * Check AH/ESP integrity.
263 			 */
264 			if (IPSEC_ENABLED(ipv6)) {
265 				if (n != NULL &&
266 				    IPSEC_CHECK_POLICY(ipv6, n, last) != 0) {
267 					m_freem(n);
268 					/* Do not inject data into pcb. */
269 					n = NULL;
270 				}
271 			}
272 #endif /* IPSEC */
273 			if (n) {
274 				if (last->inp_flags & INP_CONTROLOPTS ||
275 				    last->inp_socket->so_options & SO_TIMESTAMP)
276 					ip6_savecontrol(last, n, &opts);
277 				/* strip intermediate headers */
278 				m_adj(n, *offp);
279 				if (sbappendaddr(&last->inp_socket->so_rcv,
280 						(struct sockaddr *)&fromsa,
281 						 n, opts) == 0) {
282 					m_freem(n);
283 					if (opts)
284 						m_freem(opts);
285 					RIP6STAT_INC(rip6s_fullsock);
286 				} else
287 					sorwakeup(last->inp_socket);
288 				opts = NULL;
289 			}
290 			INP_RUNLOCK(last);
291 		}
292 		last = in6p;
293 	}
294 	INP_INFO_RUNLOCK(&V_ripcbinfo);
295 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
296 	/*
297 	 * Check AH/ESP integrity.
298 	 */
299 	if (IPSEC_ENABLED(ipv6) && last != NULL &&
300 	    IPSEC_CHECK_POLICY(ipv6, m, last) != 0) {
301 		m_freem(m);
302 		IP6STAT_DEC(ip6s_delivered);
303 		/* Do not inject data into pcb. */
304 		INP_RUNLOCK(last);
305 	} else
306 #endif /* IPSEC */
307 	if (last != NULL) {
308 		if (last->inp_flags & INP_CONTROLOPTS ||
309 		    last->inp_socket->so_options & SO_TIMESTAMP)
310 			ip6_savecontrol(last, m, &opts);
311 		/* Strip intermediate headers. */
312 		m_adj(m, *offp);
313 		if (sbappendaddr(&last->inp_socket->so_rcv,
314 		    (struct sockaddr *)&fromsa, m, opts) == 0) {
315 			m_freem(m);
316 			if (opts)
317 				m_freem(opts);
318 			RIP6STAT_INC(rip6s_fullsock);
319 		} else
320 			sorwakeup(last->inp_socket);
321 		INP_RUNLOCK(last);
322 	} else {
323 		RIP6STAT_INC(rip6s_nosock);
324 		if (m->m_flags & M_MCAST)
325 			RIP6STAT_INC(rip6s_nosockmcast);
326 		if (proto == IPPROTO_NONE)
327 			m_freem(m);
328 		else
329 			icmp6_error(m, ICMP6_PARAM_PROB,
330 			    ICMP6_PARAMPROB_NEXTHEADER,
331 			    ip6_get_prevhdr(m, *offp));
332 		IP6STAT_DEC(ip6s_delivered);
333 	}
334 	return (IPPROTO_DONE);
335 }
336 
337 void
338 rip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
339 {
340 	struct ip6ctlparam *ip6cp = NULL;
341 	const struct sockaddr_in6 *sa6_src = NULL;
342 	void *cmdarg;
343 	struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
344 
345 	if (sa->sa_family != AF_INET6 ||
346 	    sa->sa_len != sizeof(struct sockaddr_in6))
347 		return;
348 
349 	if ((unsigned)cmd >= PRC_NCMDS)
350 		return;
351 	if (PRC_IS_REDIRECT(cmd))
352 		notify = in6_rtchange, d = NULL;
353 	else if (cmd == PRC_HOSTDEAD)
354 		d = NULL;
355 	else if (inet6ctlerrmap[cmd] == 0)
356 		return;
357 
358 	/*
359 	 * If the parameter is from icmp6, decode it.
360 	 */
361 	if (d != NULL) {
362 		ip6cp = (struct ip6ctlparam *)d;
363 		cmdarg = ip6cp->ip6c_cmdarg;
364 		sa6_src = ip6cp->ip6c_src;
365 	} else {
366 		cmdarg = NULL;
367 		sa6_src = &sa6_any;
368 	}
369 
370 	(void) in6_pcbnotify(&V_ripcbinfo, sa, 0,
371 	    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
372 }
373 
374 /*
375  * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
376  * may have setup with control call.
377  */
378 int
379 rip6_output(struct mbuf *m, struct socket *so, ...)
380 {
381 	struct mbuf *control;
382 	struct m_tag *mtag;
383 	struct sockaddr_in6 *dstsock;
384 	struct ip6_hdr *ip6;
385 	struct inpcb *in6p;
386 	u_int	plen = m->m_pkthdr.len;
387 	int error = 0;
388 	struct ip6_pktopts opt, *optp;
389 	struct ifnet *oifp = NULL;
390 	int type = 0, code = 0;		/* for ICMPv6 output statistics only */
391 	int scope_ambiguous = 0;
392 	int use_defzone = 0;
393 	int hlim = 0;
394 	struct in6_addr in6a;
395 	va_list ap;
396 
397 	va_start(ap, so);
398 	dstsock = va_arg(ap, struct sockaddr_in6 *);
399 	control = va_arg(ap, struct mbuf *);
400 	va_end(ap);
401 
402 	in6p = sotoinpcb(so);
403 	INP_WLOCK(in6p);
404 
405 	if (control != NULL) {
406 		if ((error = ip6_setpktopts(control, &opt,
407 		    in6p->in6p_outputopts, so->so_cred,
408 		    so->so_proto->pr_protocol)) != 0) {
409 			goto bad;
410 		}
411 		optp = &opt;
412 	} else
413 		optp = in6p->in6p_outputopts;
414 
415 	/*
416 	 * Check and convert scope zone ID into internal form.
417 	 *
418 	 * XXX: we may still need to determine the zone later.
419 	 */
420 	if (!(so->so_state & SS_ISCONNECTED)) {
421 		if (!optp || !optp->ip6po_pktinfo ||
422 		    !optp->ip6po_pktinfo->ipi6_ifindex)
423 			use_defzone = V_ip6_use_defzone;
424 		if (dstsock->sin6_scope_id == 0 && !use_defzone)
425 			scope_ambiguous = 1;
426 		if ((error = sa6_embedscope(dstsock, use_defzone)) != 0)
427 			goto bad;
428 	}
429 
430 	/*
431 	 * For an ICMPv6 packet, we should know its type and code to update
432 	 * statistics.
433 	 */
434 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
435 		struct icmp6_hdr *icmp6;
436 		if (m->m_len < sizeof(struct icmp6_hdr) &&
437 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
438 			error = ENOBUFS;
439 			goto bad;
440 		}
441 		icmp6 = mtod(m, struct icmp6_hdr *);
442 		type = icmp6->icmp6_type;
443 		code = icmp6->icmp6_code;
444 	}
445 
446 	M_PREPEND(m, sizeof(*ip6), M_NOWAIT);
447 	if (m == NULL) {
448 		error = ENOBUFS;
449 		goto bad;
450 	}
451 	ip6 = mtod(m, struct ip6_hdr *);
452 
453 	/*
454 	 * Source address selection.
455 	 */
456 	error = in6_selectsrc_socket(dstsock, optp, in6p, so->so_cred,
457 	    scope_ambiguous, &in6a, &hlim);
458 
459 	if (error)
460 		goto bad;
461 	error = prison_check_ip6(in6p->inp_cred, &in6a);
462 	if (error != 0)
463 		goto bad;
464 	ip6->ip6_src = in6a;
465 
466 	ip6->ip6_dst = dstsock->sin6_addr;
467 
468 	/*
469 	 * Fill in the rest of the IPv6 header fields.
470 	 */
471 	ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
472 	    (in6p->inp_flow & IPV6_FLOWINFO_MASK);
473 	ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
474 	    (IPV6_VERSION & IPV6_VERSION_MASK);
475 
476 	/*
477 	 * ip6_plen will be filled in ip6_output, so not fill it here.
478 	 */
479 	ip6->ip6_nxt = in6p->inp_ip_p;
480 	ip6->ip6_hlim = hlim;
481 
482 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
483 	    in6p->in6p_cksum != -1) {
484 		struct mbuf *n;
485 		int off;
486 		u_int16_t *p;
487 
488 		/* Compute checksum. */
489 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
490 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
491 		else
492 			off = in6p->in6p_cksum;
493 		if (plen < off + 1) {
494 			error = EINVAL;
495 			goto bad;
496 		}
497 		off += sizeof(struct ip6_hdr);
498 
499 		n = m;
500 		while (n && n->m_len <= off) {
501 			off -= n->m_len;
502 			n = n->m_next;
503 		}
504 		if (!n)
505 			goto bad;
506 		p = (u_int16_t *)(mtod(n, caddr_t) + off);
507 		*p = 0;
508 		*p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
509 	}
510 
511 	/*
512 	 * Send RA/RS messages to user land for protection, before sending
513 	 * them to rtadvd/rtsol.
514 	 */
515 	if ((send_sendso_input_hook != NULL) &&
516 	    so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
517 		switch (type) {
518 		case ND_ROUTER_ADVERT:
519 		case ND_ROUTER_SOLICIT:
520 			mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
521 				sizeof(unsigned short), M_NOWAIT);
522 			if (mtag == NULL)
523 				goto bad;
524 			m_tag_prepend(m, mtag);
525 		}
526 	}
527 
528 	error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
529 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
530 		if (oifp)
531 			icmp6_ifoutstat_inc(oifp, type, code);
532 		ICMP6STAT_INC(icp6s_outhist[type]);
533 	} else
534 		RIP6STAT_INC(rip6s_opackets);
535 
536 	goto freectl;
537 
538  bad:
539 	if (m)
540 		m_freem(m);
541 
542  freectl:
543 	if (control != NULL) {
544 		ip6_clearpktopts(&opt, -1);
545 		m_freem(control);
546 	}
547 	INP_WUNLOCK(in6p);
548 	return (error);
549 }
550 
551 /*
552  * Raw IPv6 socket option processing.
553  */
554 int
555 rip6_ctloutput(struct socket *so, struct sockopt *sopt)
556 {
557 	struct inpcb *inp;
558 	int error;
559 
560 	if (sopt->sopt_level == IPPROTO_ICMPV6)
561 		/*
562 		 * XXX: is it better to call icmp6_ctloutput() directly
563 		 * from protosw?
564 		 */
565 		return (icmp6_ctloutput(so, sopt));
566 	else if (sopt->sopt_level != IPPROTO_IPV6) {
567 		if (sopt->sopt_level == SOL_SOCKET &&
568 		    sopt->sopt_name == SO_SETFIB) {
569 			inp = sotoinpcb(so);
570 			INP_WLOCK(inp);
571 			inp->inp_inc.inc_fibnum = so->so_fibnum;
572 			INP_WUNLOCK(inp);
573 			return (0);
574 		}
575 		return (EINVAL);
576 	}
577 
578 	error = 0;
579 
580 	switch (sopt->sopt_dir) {
581 	case SOPT_GET:
582 		switch (sopt->sopt_name) {
583 		case MRT6_INIT:
584 		case MRT6_DONE:
585 		case MRT6_ADD_MIF:
586 		case MRT6_DEL_MIF:
587 		case MRT6_ADD_MFC:
588 		case MRT6_DEL_MFC:
589 		case MRT6_PIM:
590 			error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
591 			    EOPNOTSUPP;
592 			break;
593 		case IPV6_CHECKSUM:
594 			error = ip6_raw_ctloutput(so, sopt);
595 			break;
596 		default:
597 			error = ip6_ctloutput(so, sopt);
598 			break;
599 		}
600 		break;
601 
602 	case SOPT_SET:
603 		switch (sopt->sopt_name) {
604 		case MRT6_INIT:
605 		case MRT6_DONE:
606 		case MRT6_ADD_MIF:
607 		case MRT6_DEL_MIF:
608 		case MRT6_ADD_MFC:
609 		case MRT6_DEL_MFC:
610 		case MRT6_PIM:
611 			error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
612 			    EOPNOTSUPP;
613 			break;
614 		case IPV6_CHECKSUM:
615 			error = ip6_raw_ctloutput(so, sopt);
616 			break;
617 		default:
618 			error = ip6_ctloutput(so, sopt);
619 			break;
620 		}
621 		break;
622 	}
623 
624 	return (error);
625 }
626 
627 static int
628 rip6_attach(struct socket *so, int proto, struct thread *td)
629 {
630 	struct inpcb *inp;
631 	struct icmp6_filter *filter;
632 	int error;
633 
634 	inp = sotoinpcb(so);
635 	KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
636 
637 	error = priv_check(td, PRIV_NETINET_RAW);
638 	if (error)
639 		return (error);
640 	error = soreserve(so, rip_sendspace, rip_recvspace);
641 	if (error)
642 		return (error);
643 	filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
644 	if (filter == NULL)
645 		return (ENOMEM);
646 	INP_INFO_WLOCK(&V_ripcbinfo);
647 	error = in_pcballoc(so, &V_ripcbinfo);
648 	if (error) {
649 		INP_INFO_WUNLOCK(&V_ripcbinfo);
650 		free(filter, M_PCB);
651 		return (error);
652 	}
653 	inp = (struct inpcb *)so->so_pcb;
654 	INP_INFO_WUNLOCK(&V_ripcbinfo);
655 	inp->inp_vflag |= INP_IPV6;
656 	inp->inp_ip_p = (long)proto;
657 	inp->in6p_hops = -1;	/* use kernel default */
658 	inp->in6p_cksum = -1;
659 	inp->in6p_icmp6filt = filter;
660 	ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
661 	INP_WUNLOCK(inp);
662 	return (0);
663 }
664 
665 static void
666 rip6_detach(struct socket *so)
667 {
668 	struct inpcb *inp;
669 
670 	inp = sotoinpcb(so);
671 	KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
672 
673 	if (so == V_ip6_mrouter && ip6_mrouter_done)
674 		ip6_mrouter_done();
675 	/* xxx: RSVP */
676 	INP_INFO_WLOCK(&V_ripcbinfo);
677 	INP_WLOCK(inp);
678 	free(inp->in6p_icmp6filt, M_PCB);
679 	in_pcbdetach(inp);
680 	in_pcbfree(inp);
681 	INP_INFO_WUNLOCK(&V_ripcbinfo);
682 }
683 
684 /* XXXRW: This can't ever be called. */
685 static void
686 rip6_abort(struct socket *so)
687 {
688 	struct inpcb *inp;
689 
690 	inp = sotoinpcb(so);
691 	KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
692 
693 	soisdisconnected(so);
694 }
695 
696 static void
697 rip6_close(struct socket *so)
698 {
699 	struct inpcb *inp;
700 
701 	inp = sotoinpcb(so);
702 	KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
703 
704 	soisdisconnected(so);
705 }
706 
707 static int
708 rip6_disconnect(struct socket *so)
709 {
710 	struct inpcb *inp;
711 
712 	inp = sotoinpcb(so);
713 	KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
714 
715 	if ((so->so_state & SS_ISCONNECTED) == 0)
716 		return (ENOTCONN);
717 	inp->in6p_faddr = in6addr_any;
718 	rip6_abort(so);
719 	return (0);
720 }
721 
722 static int
723 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
724 {
725 	struct inpcb *inp;
726 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
727 	struct ifaddr *ifa = NULL;
728 	int error = 0;
729 
730 	inp = sotoinpcb(so);
731 	KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
732 
733 	if (nam->sa_len != sizeof(*addr))
734 		return (EINVAL);
735 	if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
736 		return (error);
737 	if (CK_STAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
738 		return (EADDRNOTAVAIL);
739 	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
740 		return (error);
741 
742 	NET_EPOCH_ENTER();
743 	if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
744 	    (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) {
745 		NET_EPOCH_EXIT();
746 		return (EADDRNOTAVAIL);
747 	}
748 	if (ifa != NULL &&
749 	    ((struct in6_ifaddr *)ifa)->ia6_flags &
750 	    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
751 	     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
752 		NET_EPOCH_EXIT();
753 		return (EADDRNOTAVAIL);
754 	}
755 	NET_EPOCH_EXIT();
756 	INP_INFO_WLOCK(&V_ripcbinfo);
757 	INP_WLOCK(inp);
758 	inp->in6p_laddr = addr->sin6_addr;
759 	INP_WUNLOCK(inp);
760 	INP_INFO_WUNLOCK(&V_ripcbinfo);
761 	return (0);
762 }
763 
764 static int
765 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
766 {
767 	struct inpcb *inp;
768 	struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
769 	struct in6_addr in6a;
770 	int error = 0, scope_ambiguous = 0;
771 
772 	inp = sotoinpcb(so);
773 	KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
774 
775 	if (nam->sa_len != sizeof(*addr))
776 		return (EINVAL);
777 	if (CK_STAILQ_EMPTY(&V_ifnet))
778 		return (EADDRNOTAVAIL);
779 	if (addr->sin6_family != AF_INET6)
780 		return (EAFNOSUPPORT);
781 
782 	/*
783 	 * Application should provide a proper zone ID or the use of default
784 	 * zone IDs should be enabled.  Unfortunately, some applications do
785 	 * not behave as it should, so we need a workaround.  Even if an
786 	 * appropriate ID is not determined, we'll see if we can determine
787 	 * the outgoing interface.  If we can, determine the zone ID based on
788 	 * the interface below.
789 	 */
790 	if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
791 		scope_ambiguous = 1;
792 	if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
793 		return (error);
794 
795 	INP_INFO_WLOCK(&V_ripcbinfo);
796 	INP_WLOCK(inp);
797 	/* Source address selection. XXX: need pcblookup? */
798 	error = in6_selectsrc_socket(addr, inp->in6p_outputopts,
799 	    inp, so->so_cred, scope_ambiguous, &in6a, NULL);
800 	if (error) {
801 		INP_WUNLOCK(inp);
802 		INP_INFO_WUNLOCK(&V_ripcbinfo);
803 		return (error);
804 	}
805 
806 	inp->in6p_faddr = addr->sin6_addr;
807 	inp->in6p_laddr = in6a;
808 	soisconnected(so);
809 	INP_WUNLOCK(inp);
810 	INP_INFO_WUNLOCK(&V_ripcbinfo);
811 	return (0);
812 }
813 
814 static int
815 rip6_shutdown(struct socket *so)
816 {
817 	struct inpcb *inp;
818 
819 	inp = sotoinpcb(so);
820 	KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
821 
822 	INP_WLOCK(inp);
823 	socantsendmore(so);
824 	INP_WUNLOCK(inp);
825 	return (0);
826 }
827 
828 static int
829 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
830     struct mbuf *control, struct thread *td)
831 {
832 	struct inpcb *inp;
833 	struct sockaddr_in6 tmp;
834 	struct sockaddr_in6 *dst;
835 	int ret;
836 
837 	inp = sotoinpcb(so);
838 	KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
839 
840 	/* Always copy sockaddr to avoid overwrites. */
841 	/* Unlocked read. */
842 	if (so->so_state & SS_ISCONNECTED) {
843 		if (nam) {
844 			m_freem(m);
845 			return (EISCONN);
846 		}
847 		/* XXX */
848 		bzero(&tmp, sizeof(tmp));
849 		tmp.sin6_family = AF_INET6;
850 		tmp.sin6_len = sizeof(struct sockaddr_in6);
851 		INP_RLOCK(inp);
852 		bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
853 		    sizeof(struct in6_addr));
854 		INP_RUNLOCK(inp);
855 		dst = &tmp;
856 	} else {
857 		if (nam == NULL) {
858 			m_freem(m);
859 			return (ENOTCONN);
860 		}
861 		if (nam->sa_len != sizeof(struct sockaddr_in6)) {
862 			m_freem(m);
863 			return (EINVAL);
864 		}
865 		tmp = *(struct sockaddr_in6 *)nam;
866 		dst = &tmp;
867 
868 		if (dst->sin6_family == AF_UNSPEC) {
869 			/*
870 			 * XXX: we allow this case for backward
871 			 * compatibility to buggy applications that
872 			 * rely on old (and wrong) kernel behavior.
873 			 */
874 			log(LOG_INFO, "rip6 SEND: address family is "
875 			    "unspec. Assume AF_INET6\n");
876 			dst->sin6_family = AF_INET6;
877 		} else if (dst->sin6_family != AF_INET6) {
878 			m_freem(m);
879 			return(EAFNOSUPPORT);
880 		}
881 	}
882 	ret = rip6_output(m, so, dst, control);
883 	return (ret);
884 }
885 
886 struct pr_usrreqs rip6_usrreqs = {
887 	.pru_abort =		rip6_abort,
888 	.pru_attach =		rip6_attach,
889 	.pru_bind =		rip6_bind,
890 	.pru_connect =		rip6_connect,
891 	.pru_control =		in6_control,
892 	.pru_detach =		rip6_detach,
893 	.pru_disconnect =	rip6_disconnect,
894 	.pru_peeraddr =		in6_getpeeraddr,
895 	.pru_send =		rip6_send,
896 	.pru_shutdown =		rip6_shutdown,
897 	.pru_sockaddr =		in6_getsockaddr,
898 	.pru_close =		rip6_close,
899 };
900