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