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