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