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