xref: /openbsd-src/sys/netinet6/raw_ip6.c (revision 5e3c7963eb248119b7dfd4b0defad58a7d9cd306)
1 /*	$OpenBSD: raw_ip6.c,v 1.133 2018/11/09 13:26:12 claudio Exp $	*/
2 /*	$KAME: raw_ip6.c,v 1.69 2001/03/04 15:55:44 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1988, 1993
35  *	The Regents of the University of California.  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 "pf.h"
65 
66 #include <sys/param.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.h>
69 #include <sys/socket.h>
70 #include <sys/protosw.h>
71 #include <sys/socketvar.h>
72 #include <sys/errno.h>
73 #include <sys/systm.h>
74 #include <sys/sysctl.h>
75 
76 #include <net/if.h>
77 #include <net/if_var.h>
78 #include <net/route.h>
79 
80 #include <netinet/in.h>
81 #include <netinet6/in6_var.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
84 #ifdef MROUTING
85 #include <netinet6/ip6_mroute.h>
86 #endif
87 #include <netinet/icmp6.h>
88 #include <netinet/ip.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet6/nd6.h>
91 #include <netinet6/ip6protosw.h>
92 #include <netinet6/raw_ip6.h>
93 
94 #if NPF > 0
95 #include <net/pfvar.h>
96 #endif
97 
98 #include <sys/stdarg.h>
99 
100 /*
101  * Raw interface to IP6 protocol.
102  */
103 
104 struct	inpcbtable rawin6pcbtable;
105 
106 struct cpumem *rip6counters;
107 
108 /*
109  * Initialize raw connection block queue.
110  */
111 void
112 rip6_init(void)
113 {
114 	in_pcbinit(&rawin6pcbtable, 1);
115 	rip6counters = counters_alloc(rip6s_ncounters);
116 }
117 
118 int
119 rip6_input(struct mbuf **mp, int *offp, int proto, int af)
120 {
121 	struct mbuf *m = *mp;
122 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
123 	struct inpcb *in6p;
124 	struct inpcb *last = NULL;
125 	struct in6_addr *key;
126 	struct sockaddr_in6 rip6src;
127 	struct mbuf *opts = NULL;
128 
129 	KASSERT(af == AF_INET6);
130 
131 	if (proto != IPPROTO_ICMPV6)
132 		rip6stat_inc(rip6s_ipackets);
133 
134 	bzero(&rip6src, sizeof(rip6src));
135 	rip6src.sin6_len = sizeof(struct sockaddr_in6);
136 	rip6src.sin6_family = AF_INET6;
137 	/* KAME hack: recover scopeid */
138 	in6_recoverscope(&rip6src, &ip6->ip6_src);
139 
140 	key = &ip6->ip6_dst;
141 #if NPF > 0
142 	if (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
143 		struct pf_divert *divert;
144 
145 		divert = pf_find_divert(m);
146 		KASSERT(divert != NULL);
147 		switch (divert->type) {
148 		case PF_DIVERT_TO:
149 			key = &divert->addr.v6;
150 			break;
151 		case PF_DIVERT_REPLY:
152 			break;
153 		default:
154 			panic("%s: unknown divert type %d, mbuf %p, divert %p",
155 			    __func__, divert->type, m, divert);
156 		}
157 	}
158 #endif
159 	NET_ASSERT_LOCKED();
160 	TAILQ_FOREACH(in6p, &rawin6pcbtable.inpt_queue, inp_queue) {
161 		if (in6p->inp_socket->so_state & SS_CANTRCVMORE)
162 			continue;
163 		if (!(in6p->inp_flags & INP_IPV6))
164 			continue;
165 		if ((in6p->inp_ipv6.ip6_nxt || proto == IPPROTO_ICMPV6) &&
166 		    in6p->inp_ipv6.ip6_nxt != proto)
167 			continue;
168 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->inp_laddr6) &&
169 		    !IN6_ARE_ADDR_EQUAL(&in6p->inp_laddr6, key))
170 			continue;
171 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->inp_faddr6) &&
172 		    !IN6_ARE_ADDR_EQUAL(&in6p->inp_faddr6, &ip6->ip6_src))
173 			continue;
174 		if (proto == IPPROTO_ICMPV6 && in6p->inp_icmp6filt) {
175 			struct icmp6_hdr *icmp6;
176 
177 			IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, *offp,
178 			    sizeof(*icmp6));
179 			if (icmp6 == NULL)
180 				return IPPROTO_DONE;
181 			if (ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
182 			    in6p->inp_icmp6filt))
183 				continue;
184 		}
185 		if (proto != IPPROTO_ICMPV6 && in6p->inp_cksum6 != -1) {
186 			rip6stat_inc(rip6s_isum);
187 			if (in6_cksum(m, proto, *offp,
188 			    m->m_pkthdr.len - *offp)) {
189 				rip6stat_inc(rip6s_badsum);
190 				continue;
191 			}
192 		}
193 		if (last) {
194 			struct	mbuf *n;
195 			if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
196 				if (last->inp_flags & IN6P_CONTROLOPTS)
197 					ip6_savecontrol(last, n, &opts);
198 				/* strip intermediate headers */
199 				m_adj(n, *offp);
200 				if (sbappendaddr(last->inp_socket,
201 				    &last->inp_socket->so_rcv,
202 				    sin6tosa(&rip6src), n, opts) == 0) {
203 					/* should notify about lost packet */
204 					m_freem(n);
205 					m_freem(opts);
206 					rip6stat_inc(rip6s_fullsock);
207 				} else
208 					sorwakeup(last->inp_socket);
209 				opts = NULL;
210 			}
211 		}
212 		last = in6p;
213 	}
214 	if (last) {
215 		if (last->inp_flags & IN6P_CONTROLOPTS)
216 			ip6_savecontrol(last, m, &opts);
217 		/* strip intermediate headers */
218 		m_adj(m, *offp);
219 		if (sbappendaddr(last->inp_socket, &last->inp_socket->so_rcv,
220 		    sin6tosa(&rip6src), m, opts) == 0) {
221 			m_freem(m);
222 			m_freem(opts);
223 			rip6stat_inc(rip6s_fullsock);
224 		} else
225 			sorwakeup(last->inp_socket);
226 	} else {
227 		struct counters_ref ref;
228 		uint64_t *counters;
229 
230 		if (proto != IPPROTO_ICMPV6) {
231 			rip6stat_inc(rip6s_nosock);
232 			if (m->m_flags & M_MCAST)
233 				rip6stat_inc(rip6s_nosockmcast);
234 		}
235 		if (proto == IPPROTO_NONE || proto == IPPROTO_ICMPV6) {
236 			m_freem(m);
237 		} else {
238 			int prvnxt = ip6_get_prevhdr(m, *offp);
239 
240 			icmp6_error(m, ICMP6_PARAM_PROB,
241 			    ICMP6_PARAMPROB_NEXTHEADER, prvnxt);
242 		}
243 		counters = counters_enter(&ref, ip6counters);
244 		counters[ip6s_delivered]--;
245 		counters_leave(&ref, ip6counters);
246 	}
247 	return IPPROTO_DONE;
248 }
249 
250 void
251 rip6_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *d)
252 {
253 	struct ip6_hdr *ip6;
254 	struct ip6ctlparam *ip6cp = NULL;
255 	struct sockaddr_in6 *sa6 = satosin6(sa);
256 	const struct sockaddr_in6 *sa6_src = NULL;
257 	void *cmdarg;
258 	void (*notify)(struct inpcb *, int) = in_rtchange;
259 	int nxt;
260 
261 	if (sa->sa_family != AF_INET6 ||
262 	    sa->sa_len != sizeof(struct sockaddr_in6))
263 		return;
264 
265 	if ((unsigned)cmd >= PRC_NCMDS)
266 		return;
267 	if (PRC_IS_REDIRECT(cmd))
268 		notify = in_rtchange, d = NULL;
269 	else if (cmd == PRC_HOSTDEAD)
270 		d = NULL;
271 	else if (cmd == PRC_MSGSIZE)
272 		; /* special code is present, see below */
273 	else if (inet6ctlerrmap[cmd] == 0)
274 		return;
275 
276 	/* if the parameter is from icmp6, decode it. */
277 	if (d != NULL) {
278 		ip6cp = (struct ip6ctlparam *)d;
279 		ip6 = ip6cp->ip6c_ip6;
280 		cmdarg = ip6cp->ip6c_cmdarg;
281 		sa6_src = ip6cp->ip6c_src;
282 		nxt = ip6cp->ip6c_nxt;
283 	} else {
284 		ip6 = NULL;
285 		cmdarg = NULL;
286 		sa6_src = &sa6_any;
287 		nxt = -1;
288 	}
289 
290 	if (ip6 && cmd == PRC_MSGSIZE) {
291 		int valid = 0;
292 		struct inpcb *in6p;
293 
294 		/*
295 		 * Check to see if we have a valid raw IPv6 socket
296 		 * corresponding to the address in the ICMPv6 message
297 		 * payload, and the protocol (ip6_nxt) meets the socket.
298 		 * XXX chase extension headers, or pass final nxt value
299 		 * from icmp6_notify_error()
300 		 */
301 		in6p = in6_pcbhashlookup(&rawin6pcbtable, &sa6->sin6_addr, 0,
302 		    &sa6_src->sin6_addr, 0, rdomain);
303 
304 		if (in6p && in6p->inp_ipv6.ip6_nxt &&
305 		    in6p->inp_ipv6.ip6_nxt == nxt)
306 			valid++;
307 
308 		/*
309 		 * Depending on the value of "valid" and routing table
310 		 * size (mtudisc_{hi,lo}wat), we will:
311 		 * - recalculate the new MTU and create the
312 		 *   corresponding routing entry, or
313 		 * - ignore the MTU change notification.
314 		 */
315 		icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
316 
317 		/*
318 		 * regardless of if we called icmp6_mtudisc_update(),
319 		 * we need to call in6_pcbnotify(), to notify path
320 		 * MTU change to the userland (2292bis-02), because
321 		 * some unconnected sockets may share the same
322 		 * destination and want to know the path MTU.
323 		 */
324 	}
325 
326 	(void) in6_pcbnotify(&rawin6pcbtable, sa6, 0,
327 	    sa6_src, 0, rdomain, cmd, cmdarg, notify);
328 }
329 
330 /*
331  * Generate IPv6 header and pass packet to ip6_output.
332  * Tack on options user may have setup with control call.
333  */
334 int
335 rip6_output(struct mbuf *m, struct socket *so, struct sockaddr *dstaddr,
336     struct mbuf *control)
337 {
338 	struct in6_addr *dst;
339 	struct ip6_hdr *ip6;
340 	struct inpcb *in6p;
341 	u_int	plen = m->m_pkthdr.len;
342 	int error = 0;
343 	struct ip6_pktopts opt, *optp = NULL, *origoptp;
344 	int type;		/* for ICMPv6 output statistics only */
345 	int priv = 0;
346 	int flags;
347 
348 	in6p = sotoinpcb(so);
349 
350 	priv = 0;
351 	if ((so->so_state & SS_PRIV) != 0)
352 		priv = 1;
353 	if (control) {
354 		if ((error = ip6_setpktopts(control, &opt,
355 		    in6p->inp_outputopts6,
356 		    priv, so->so_proto->pr_protocol)) != 0)
357 			goto bad;
358 		optp = &opt;
359 	} else
360 		optp = in6p->inp_outputopts6;
361 
362 	if (dstaddr->sa_family != AF_INET6) {
363 		error = EAFNOSUPPORT;
364 		goto bad;
365 	}
366 	dst = &satosin6(dstaddr)->sin6_addr;
367 	if (IN6_IS_ADDR_V4MAPPED(dst)) {
368 		error = EADDRNOTAVAIL;
369 		goto bad;
370 	}
371 
372 	/*
373 	 * For an ICMPv6 packet, we should know its type and code
374 	 * to update statistics.
375 	 */
376 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
377 		struct icmp6_hdr *icmp6;
378 		if (m->m_len < sizeof(struct icmp6_hdr) &&
379 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
380 			error = ENOBUFS;
381 			goto bad;
382 		}
383 		icmp6 = mtod(m, struct icmp6_hdr *);
384 		type = icmp6->icmp6_type;
385 	}
386 
387 	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
388 	if (!m) {
389 		error = ENOBUFS;
390 		goto bad;
391 	}
392 	ip6 = mtod(m, struct ip6_hdr *);
393 
394 	/*
395 	 * Next header might not be ICMP6 but use its pseudo header anyway.
396 	 */
397 	ip6->ip6_dst = *dst;
398 
399 	/* KAME hack: embed scopeid */
400 	origoptp = in6p->inp_outputopts6;
401 	in6p->inp_outputopts6 = optp;
402 	if (in6_embedscope(&ip6->ip6_dst, satosin6(dstaddr), in6p) != 0) {
403 		error = EINVAL;
404 		goto bad;
405 	}
406 	in6p->inp_outputopts6 = origoptp;
407 
408 	/*
409 	 * Source address selection.
410 	 */
411 	{
412 		struct in6_addr *in6a;
413 
414 		error = in6_pcbselsrc(&in6a, satosin6(dstaddr), in6p, optp);
415 		if (error)
416 			goto bad;
417 
418 		ip6->ip6_src = *in6a;
419 	}
420 
421 	ip6->ip6_flow = in6p->inp_flowinfo & IPV6_FLOWINFO_MASK;
422 	ip6->ip6_vfc  &= ~IPV6_VERSION_MASK;
423 	ip6->ip6_vfc  |= IPV6_VERSION;
424 #if 0				/* ip6_plen will be filled in ip6_output. */
425 	ip6->ip6_plen  = htons((u_short)plen);
426 #endif
427 	ip6->ip6_nxt   = in6p->inp_ipv6.ip6_nxt;
428 	ip6->ip6_hlim = in6_selecthlim(in6p);
429 
430 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
431 	    in6p->inp_cksum6 != -1) {
432 		struct mbuf *n;
433 		int off;
434 		u_int16_t *sump;
435 		int sumoff;
436 
437 		/* compute checksum */
438 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
439 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
440 		else
441 			off = in6p->inp_cksum6;
442 		if (plen < off + 1) {
443 			error = EINVAL;
444 			goto bad;
445 		}
446 		off += sizeof(struct ip6_hdr);
447 
448 		n = m_pulldown(m, off, sizeof(*sump), &sumoff);
449 		if (n == NULL) {
450 			m = NULL;
451 			error = ENOBUFS;
452 			goto bad;
453 		}
454 		sump = (u_int16_t *)(mtod(n, caddr_t) + sumoff);
455 		*sump = 0;
456 		*sump = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
457 	}
458 
459 	flags = 0;
460 	if (in6p->inp_flags & IN6P_MINMTU)
461 		flags |= IPV6_MINMTU;
462 
463 	/* force routing table */
464 	m->m_pkthdr.ph_rtableid = in6p->inp_rtableid;
465 
466 #if NPF > 0
467 	if (in6p->inp_socket->so_state & SS_ISCONNECTED &&
468 	    so->so_proto->pr_protocol != IPPROTO_ICMPV6)
469 		pf_mbuf_link_inpcb(m, in6p);
470 #endif
471 
472 	error = ip6_output(m, optp, &in6p->inp_route6, flags,
473 	    in6p->inp_moptions6, in6p);
474 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
475 		icmp6stat_inc(icp6s_outhist + type);
476 	} else
477 		rip6stat_inc(rip6s_opackets);
478 
479 	goto freectl;
480 
481  bad:
482 	m_freem(m);
483 
484  freectl:
485 	if (control) {
486 		ip6_clearpktopts(&opt, -1);
487 		m_freem(control);
488 	}
489 	return (error);
490 }
491 
492 /*
493  * Raw IPv6 socket option processing.
494  */
495 int
496 rip6_ctloutput(int op, struct socket *so, int level, int optname,
497     struct mbuf *m)
498 {
499 #ifdef MROUTING
500 	int error;
501 #endif
502 
503 	switch (level) {
504 	case IPPROTO_IPV6:
505 		switch (optname) {
506 #ifdef MROUTING
507 		case MRT6_INIT:
508 		case MRT6_DONE:
509 		case MRT6_ADD_MIF:
510 		case MRT6_DEL_MIF:
511 		case MRT6_ADD_MFC:
512 		case MRT6_DEL_MFC:
513 			if (op == PRCO_SETOPT) {
514 				error = ip6_mrouter_set(optname, so, m);
515 			} else if (op == PRCO_GETOPT)
516 				error = ip6_mrouter_get(optname, so, m);
517 			else
518 				error = EINVAL;
519 			return (error);
520 #endif
521 		case IPV6_CHECKSUM:
522 			return (ip6_raw_ctloutput(op, so, level, optname, m));
523 		default:
524 			return (ip6_ctloutput(op, so, level, optname, m));
525 		}
526 
527 	case IPPROTO_ICMPV6:
528 		/*
529 		 * XXX: is it better to call icmp6_ctloutput() directly
530 		 * from protosw?
531 		 */
532 		return (icmp6_ctloutput(op, so, level, optname, m));
533 
534 	default:
535 		return EINVAL;
536 	}
537 }
538 
539 extern	u_long rip6_sendspace;
540 extern	u_long rip6_recvspace;
541 
542 int
543 rip6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
544 	struct mbuf *control, struct proc *p)
545 {
546 	struct inpcb *in6p;
547 	int error = 0;
548 
549 	if (req == PRU_CONTROL)
550 		return (in6_control(so, (u_long)m, (caddr_t)nam,
551 		    (struct ifnet *)control));
552 
553 	soassertlocked(so);
554 
555 	in6p = sotoinpcb(so);
556 	if (in6p == NULL) {
557 		error = EINVAL;
558 		goto release;
559 	}
560 
561 	switch (req) {
562 	case PRU_DISCONNECT:
563 		if ((so->so_state & SS_ISCONNECTED) == 0) {
564 			error = ENOTCONN;
565 			break;
566 		}
567 		in6p->inp_faddr6 = in6addr_any;
568 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
569 		break;
570 
571 	case PRU_ABORT:
572 		soisdisconnected(so);
573 		if (in6p == NULL)
574 			panic("rip6_detach");
575 #ifdef MROUTING
576 		if (so == ip6_mrouter[in6p->inp_rtableid])
577 			ip6_mrouter_done(so);
578 #endif
579 		free(in6p->inp_icmp6filt, M_PCB, sizeof(struct icmp6_filter));
580 		in6p->inp_icmp6filt = NULL;
581 
582 		in_pcbdetach(in6p);
583 		break;
584 
585 	case PRU_BIND:
586 	    {
587 		struct sockaddr_in6 *addr;
588 
589 		if ((error = in6_nam2sin6(nam, &addr)))
590 			break;
591 		/*
592 		 * Make sure to not enter in_pcblookup_local(), local ports
593 		 * are non-sensical for raw sockets.
594 		 */
595 		addr->sin6_port = 0;
596 
597 		if ((error = in6_pcbaddrisavail(in6p, addr, 0, p)))
598 			break;
599 
600 		in6p->inp_laddr6 = addr->sin6_addr;
601 		break;
602 	    }
603 
604 	case PRU_CONNECT:
605 	{
606 		struct sockaddr_in6 *addr;
607 		struct in6_addr *in6a = NULL;
608 
609 		if ((error = in6_nam2sin6(nam, &addr)))
610 			break;
611 		/* Source address selection. XXX: need pcblookup? */
612 		error = in6_pcbselsrc(&in6a, addr, in6p, in6p->inp_outputopts6);
613 		if (error)
614 			break;
615 		in6p->inp_laddr6 = *in6a;
616 		in6p->inp_faddr6 = addr->sin6_addr;
617 		soisconnected(so);
618 		break;
619 	}
620 
621 	case PRU_CONNECT2:
622 		error = EOPNOTSUPP;
623 		break;
624 
625 	/*
626 	 * Mark the connection as being incapable of futther input.
627 	 */
628 	case PRU_SHUTDOWN:
629 		socantsendmore(so);
630 		break;
631 	/*
632 	 * Ship a packet out. The appropriate raw output
633 	 * routine handles any messaging necessary.
634 	 */
635 	case PRU_SEND:
636 	{
637 		struct sockaddr_in6 dst;
638 
639 		/* always copy sockaddr to avoid overwrites */
640 		memset(&dst, 0, sizeof(dst));
641 		dst.sin6_family = AF_INET6;
642 		dst.sin6_len = sizeof(dst);
643 		if (so->so_state & SS_ISCONNECTED) {
644 			if (nam) {
645 				error = EISCONN;
646 				break;
647 			}
648 			dst.sin6_addr = in6p->inp_faddr6;
649 		} else {
650 			struct sockaddr_in6 *addr6;
651 
652 			if (nam == NULL) {
653 				error = ENOTCONN;
654 				break;
655 			}
656 			if ((error = in6_nam2sin6(nam, &addr6)))
657 				break;
658 			dst.sin6_addr = addr6->sin6_addr;
659 			dst.sin6_scope_id = addr6->sin6_scope_id;
660 		}
661 		error = rip6_output(m, so, sin6tosa(&dst), control);
662 		control = NULL;
663 		m = NULL;
664 		break;
665 	}
666 
667 	case PRU_SENSE:
668 		/*
669 		 * stat: don't bother with a blocksize
670 		 */
671 		return (0);
672 	/*
673 	 * Not supported.
674 	 */
675 	case PRU_LISTEN:
676 	case PRU_ACCEPT:
677 	case PRU_SENDOOB:
678 		error = EOPNOTSUPP;
679 		break;
680 
681 	case PRU_RCVD:
682 	case PRU_RCVOOB:
683 		return (EOPNOTSUPP);	/* do not free mbuf's */
684 
685 	case PRU_SOCKADDR:
686 		in6_setsockaddr(in6p, nam);
687 		break;
688 
689 	case PRU_PEERADDR:
690 		in6_setpeeraddr(in6p, nam);
691 		break;
692 
693 	default:
694 		panic("rip6_usrreq");
695 	}
696 release:
697 	m_freem(control);
698 	m_freem(m);
699 	return (error);
700 }
701 
702 int
703 rip6_attach(struct socket *so, int proto)
704 {
705 	struct inpcb *in6p;
706 	int error;
707 
708 	if (so->so_pcb)
709 		panic("rip6_attach");
710 	if ((so->so_state & SS_PRIV) == 0)
711 		return (EACCES);
712 	if (proto < 0 || proto >= IPPROTO_MAX)
713 		return EPROTONOSUPPORT;
714 
715 	if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)))
716 		return error;
717 	NET_ASSERT_LOCKED();
718 	if ((error = in_pcballoc(so, &rawin6pcbtable)))
719 		return error;
720 
721 	in6p = sotoinpcb(so);
722 	in6p->inp_ipv6.ip6_nxt = proto;
723 	in6p->inp_cksum6 = -1;
724 
725 	in6p->inp_icmp6filt = malloc(sizeof(struct icmp6_filter),
726 	    M_PCB, M_NOWAIT);
727 	if (in6p->inp_icmp6filt == NULL) {
728 		in_pcbdetach(in6p);
729 		return ENOMEM;
730 	}
731 	ICMP6_FILTER_SETPASSALL(in6p->inp_icmp6filt);
732 	return 0;
733 }
734 
735 int
736 rip6_detach(struct socket *so)
737 {
738 	struct inpcb *in6p = sotoinpcb(so);
739 
740 	soassertlocked(so);
741 
742 	if (in6p == NULL)
743 		panic("rip6_detach");
744 #ifdef MROUTING
745 	if (so == ip6_mrouter[in6p->inp_rtableid])
746 		ip6_mrouter_done(so);
747 #endif
748 	free(in6p->inp_icmp6filt, M_PCB, sizeof(struct icmp6_filter));
749 	in6p->inp_icmp6filt = NULL;
750 
751 	in_pcbdetach(in6p);
752 
753 	return (0);
754 }
755 
756 int
757 rip6_sysctl_rip6stat(void *oldp, size_t *oldplen, void *newp)
758 {
759 	struct rip6stat rip6stat;
760 
761 	CTASSERT(sizeof(rip6stat) == rip6s_ncounters * sizeof(uint64_t));
762 	counters_read(ip6counters, (uint64_t *)&rip6stat, rip6s_ncounters);
763 
764 	return (sysctl_rdstruct(oldp, oldplen, newp,
765 	    &rip6stat, sizeof(rip6stat)));
766 }
767 
768 int
769 rip6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
770     void *newp, size_t newlen)
771 {
772 	/* All sysctl names at this level are terminal. */
773 	if (namelen != 1)
774 		return ENOTDIR;
775 
776 	switch (name[0]) {
777 	case RIPV6CTL_STATS:
778 		return (rip6_sysctl_rip6stat(oldp, oldlenp, newp));
779 	default:
780 		return (EOPNOTSUPP);
781 	}
782 	/* NOTREACHED */
783 }
784