xref: /openbsd-src/sys/netinet6/raw_ip6.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: raw_ip6.c,v 1.44 2011/11/24 17:39:55 sperreault 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 <sys/param.h>
65 #include <sys/malloc.h>
66 #include <sys/mbuf.h>
67 #include <sys/socket.h>
68 #include <sys/protosw.h>
69 #include <sys/socketvar.h>
70 #include <sys/errno.h>
71 #include <sys/systm.h>
72 #include <sys/proc.h>
73 #include <sys/sysctl.h>
74 
75 #include <net/if.h>
76 #include <net/route.h>
77 #include <net/if_types.h>
78 
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip6.h>
82 #include <netinet6/ip6_var.h>
83 #ifdef MROUTING
84 #include <netinet6/ip6_mroute.h>
85 #endif
86 #include <netinet/icmp6.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet6/nd6.h>
91 #include <netinet6/ip6protosw.h>
92 #ifdef ENABLE_DEFAULT_SCOPE
93 #include <netinet6/scope6_var.h>
94 #endif
95 #include <netinet6/raw_ip6.h>
96 
97 #include <sys/stdarg.h>
98 
99 #include "faith.h"
100 
101 /*
102  * Raw interface to IP6 protocol.
103  */
104 /* inpcb members */
105 #define in6pcb		inpcb
106 #define in6p_laddr	inp_laddr6
107 #define in6p_faddr	inp_faddr6
108 #define in6p_icmp6filt	inp_icmp6filt
109 #define in6p_route	inp_route6
110 #define in6p_socket	inp_socket
111 #define in6p_flags	inp_flags
112 #define in6p_moptions	inp_moptions6
113 #define in6p_outputopts	inp_outputopts6
114 #define in6p_ip6	inp_ipv6
115 #define in6p_flowinfo	inp_flowinfo
116 #define in6p_sp		inp_sp
117 #define in6p_next	inp_next
118 #define in6p_prev	inp_prev
119 /* macro names */
120 #define sotoin6pcb	sotoinpcb
121 /* function names */
122 #define in6_pcbdetach	in_pcbdetach
123 #define in6_rtchange	in_rtchange
124 
125 struct	inpcbtable rawin6pcbtable;
126 #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
127 
128 struct rip6stat rip6stat;
129 
130 /*
131  * Initialize raw connection block queue.
132  */
133 void
134 rip6_init()
135 {
136 
137 	in_pcbinit(&rawin6pcbtable, 1);
138 }
139 
140 /*
141  * Setup generic address and protocol structures
142  * for raw_input routine, then pass them along with
143  * mbuf chain.
144  */
145 int
146 rip6_input(struct mbuf **mp, int *offp, int proto)
147 {
148 	struct mbuf *m = *mp;
149 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
150 	struct in6pcb *in6p;
151 	struct in6pcb *last = NULL;
152 	struct sockaddr_in6 rip6src;
153 	struct mbuf *opts = NULL;
154 
155 	rip6stat.rip6s_ipackets++;
156 
157 #if defined(NFAITH) && 0 < NFAITH
158 	if (m->m_pkthdr.rcvif) {
159 		if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
160 			/* send icmp6 host unreach? */
161 			m_freem(m);
162 			return IPPROTO_DONE;
163 		}
164 	}
165 #endif
166 
167 	/* Be proactive about malicious use of IPv4 mapped address */
168 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
169 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
170 		/* XXX stat */
171 		m_freem(m);
172 		return IPPROTO_DONE;
173 	}
174 
175 	bzero(&rip6src, sizeof(rip6src));
176 	rip6src.sin6_len = sizeof(struct sockaddr_in6);
177 	rip6src.sin6_family = AF_INET6;
178 	/* KAME hack: recover scopeid */
179 	(void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
180 
181 	CIRCLEQ_FOREACH(in6p, &rawin6pcbtable.inpt_queue, inp_queue) {
182 		if (in6p->in6p_socket->so_state & SS_CANTRCVMORE)
183 			continue;
184 		if (!(in6p->in6p_flags & INP_IPV6))
185 			continue;
186 		if (in6p->in6p_ip6.ip6_nxt &&
187 		    in6p->in6p_ip6.ip6_nxt != proto)
188 			continue;
189 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
190 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
191 			continue;
192 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
193 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
194 			continue;
195 		if (in6p->in6p_cksum != -1) {
196 			rip6stat.rip6s_isum++;
197 			if (in6_cksum(m, proto, *offp,
198 			    m->m_pkthdr.len - *offp)) {
199 				rip6stat.rip6s_badsum++;
200 				continue;
201 			}
202 		}
203 		if (last) {
204 			struct	mbuf *n;
205 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
206 				if (last->in6p_flags & IN6P_CONTROLOPTS)
207 					ip6_savecontrol(last, n, &opts);
208 				/* strip intermediate headers */
209 				m_adj(n, *offp);
210 				if (sbappendaddr(&last->in6p_socket->so_rcv,
211 				    (struct sockaddr *)&rip6src, n, opts) == 0) {
212 					/* should notify about lost packet */
213 					m_freem(n);
214 					if (opts)
215 						m_freem(opts);
216 					rip6stat.rip6s_fullsock++;
217 				} else
218 					sorwakeup(last->in6p_socket);
219 				opts = NULL;
220 			}
221 		}
222 		last = in6p;
223 	}
224 	if (last) {
225 		if (last->in6p_flags & IN6P_CONTROLOPTS)
226 			ip6_savecontrol(last, m, &opts);
227 		/* strip intermediate headers */
228 		m_adj(m, *offp);
229 		if (sbappendaddr(&last->in6p_socket->so_rcv,
230 		    (struct sockaddr *)&rip6src, m, opts) == 0) {
231 			m_freem(m);
232 			if (opts)
233 				m_freem(opts);
234 			rip6stat.rip6s_fullsock++;
235 		} else
236 			sorwakeup(last->in6p_socket);
237 	} else {
238 		rip6stat.rip6s_nosock++;
239 		if (m->m_flags & M_MCAST)
240 			rip6stat.rip6s_nosockmcast++;
241 		if (proto == IPPROTO_NONE)
242 			m_freem(m);
243 		else {
244 			u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
245 			in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
246 			icmp6_error(m, ICMP6_PARAM_PROB,
247 			    ICMP6_PARAMPROB_NEXTHEADER,
248 			    prvnxtp - mtod(m, u_int8_t *));
249 		}
250 		ip6stat.ip6s_delivered--;
251 	}
252 	return IPPROTO_DONE;
253 }
254 
255 void
256 rip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
257 {
258 	struct ip6_hdr *ip6;
259 	struct ip6ctlparam *ip6cp = NULL;
260 	const struct sockaddr_in6 *sa6_src = NULL;
261 	void *cmdarg;
262 	void (*notify)(struct in6pcb *, int) = in6_rtchange;
263 	int nxt;
264 
265 	if (sa->sa_family != AF_INET6 ||
266 	    sa->sa_len != sizeof(struct sockaddr_in6))
267 		return;
268 
269 	if ((unsigned)cmd >= PRC_NCMDS)
270 		return;
271 	if (PRC_IS_REDIRECT(cmd))
272 		notify = in6_rtchange, d = NULL;
273 	else if (cmd == PRC_HOSTDEAD)
274 		d = NULL;
275 	else if (cmd == PRC_MSGSIZE)
276 		; /* special code is present, see below */
277 	else if (inet6ctlerrmap[cmd] == 0)
278 		return;
279 
280 	/* if the parameter is from icmp6, decode it. */
281 	if (d != NULL) {
282 		ip6cp = (struct ip6ctlparam *)d;
283 		ip6 = ip6cp->ip6c_ip6;
284 		cmdarg = ip6cp->ip6c_cmdarg;
285 		sa6_src = ip6cp->ip6c_src;
286 		nxt = ip6cp->ip6c_nxt;
287 	} else {
288 		ip6 = NULL;
289 		cmdarg = NULL;
290 		sa6_src = &sa6_any;
291 		nxt = -1;
292 	}
293 
294 	if (ip6 && cmd == PRC_MSGSIZE) {
295 		struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
296 		int valid = 0;
297 		struct in6pcb *in6p;
298 
299 		/*
300 		 * Check to see if we have a valid raw IPv6 socket
301 		 * corresponding to the address in the ICMPv6 message
302 		 * payload, and the protocol (ip6_nxt) meets the socket.
303 		 * XXX chase extension headers, or pass final nxt value
304 		 * from icmp6_notify_error()
305 		 */
306 		in6p = NULL;
307 		in6p = in6_pcbhashlookup(&rawin6pcbtable, &sa6->sin6_addr, 0,
308 		    (struct in6_addr *)&sa6_src->sin6_addr, 0);
309 #if 0
310 		if (!in6p) {
311 			/*
312 			 * As the use of sendto(2) is fairly popular,
313 			 * we may want to allow non-connected pcb too.
314 			 * But it could be too weak against attacks...
315 			 * We should at least check if the local
316 			 * address (= s) is really ours.
317 			 */
318 			in6p = in_pcblookup(&rawin6pcbtable, &sa6->sin6_addr, 0,
319 			    (struct in6_addr *)&sa6_src->sin6_addr, 0,
320 			    INPLOOKUP_WILDCARD | INPLOOKUP_IPV6);
321 		}
322 #endif
323 
324 		if (in6p && in6p->in6p_ip6.ip6_nxt &&
325 		    in6p->in6p_ip6.ip6_nxt == nxt)
326 			valid++;
327 
328 		/*
329 		 * Depending on the value of "valid" and routing table
330 		 * size (mtudisc_{hi,lo}wat), we will:
331 		 * - recalculate the new MTU and create the
332 		 *   corresponding routing entry, or
333 		 * - ignore the MTU change notification.
334 		 */
335 		icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
336 
337 		/*
338 		 * regardless of if we called icmp6_mtudisc_update(),
339 		 * we need to call in6_pcbnotify(), to notify path
340 		 * MTU change to the userland (2292bis-02), because
341 		 * some unconnected sockets may share the same
342 		 * destination and want to know the path MTU.
343 		 */
344 	}
345 
346 	(void) in6_pcbnotify(&rawin6pcbtable, sa, 0,
347 	    (struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
348 }
349 
350 /*
351  * Generate IPv6 header and pass packet to ip6_output.
352  * Tack on options user may have setup with control call.
353  */
354 int
355 rip6_output(struct mbuf *m, ...)
356 {
357 	struct socket *so;
358 	struct sockaddr_in6 *dstsock;
359 	struct mbuf *control;
360 	struct in6_addr *dst;
361 	struct ip6_hdr *ip6;
362 	struct in6pcb *in6p;
363 	u_int	plen = m->m_pkthdr.len;
364 	int error = 0;
365 	struct ip6_pktopts opt, *optp = NULL, *origoptp;
366 	struct ifnet *oifp = NULL;
367 	int type, code;		/* for ICMPv6 output statistics only */
368 	int priv = 0;
369 	va_list ap;
370 	int flags;
371 
372 	va_start(ap, m);
373 	so = va_arg(ap, struct socket *);
374 	dstsock = va_arg(ap, struct sockaddr_in6 *);
375 	control = va_arg(ap, struct mbuf *);
376 	va_end(ap);
377 
378 	in6p = sotoin6pcb(so);
379 
380 	priv = 0;
381 	if ((so->so_state & SS_PRIV) != 0)
382 		priv = 1;
383 	dst = &dstsock->sin6_addr;
384 	if (control) {
385 		if ((error = ip6_setpktopts(control, &opt,
386 		    in6p->in6p_outputopts,
387 		    priv, so->so_proto->pr_protocol)) != 0)
388 			goto bad;
389 		optp = &opt;
390 	} else
391 		optp = in6p->in6p_outputopts;
392 
393 	/*
394 	 * For an ICMPv6 packet, we should know its type and code
395 	 * to update statistics.
396 	 */
397 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
398 		struct icmp6_hdr *icmp6;
399 		if (m->m_len < sizeof(struct icmp6_hdr) &&
400 		    (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
401 			error = ENOBUFS;
402 			goto bad;
403 		}
404 		icmp6 = mtod(m, struct icmp6_hdr *);
405 		type = icmp6->icmp6_type;
406 		code = icmp6->icmp6_code;
407 	}
408 
409 	M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
410 	if (!m) {
411 		error = ENOBUFS;
412 		goto bad;
413 	}
414 	ip6 = mtod(m, struct ip6_hdr *);
415 
416 	/*
417 	 * Next header might not be ICMP6 but use its pseudo header anyway.
418 	 */
419 	ip6->ip6_dst = *dst;
420 
421 	/* KAME hack: embed scopeid */
422 	origoptp = in6p->in6p_outputopts;
423 	in6p->in6p_outputopts = optp;
424 	if (in6_embedscope(&ip6->ip6_dst, dstsock, in6p, &oifp) != 0) {
425 		error = EINVAL;
426 		goto bad;
427 	}
428 	in6p->in6p_outputopts = origoptp;
429 
430 	/*
431 	 * Source address selection.
432 	 */
433 	{
434 		struct in6_addr *in6a;
435 
436 		if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
437 		    &in6p->in6p_route, &in6p->in6p_laddr, &error,
438 		    in6p->inp_rtableid)) == 0) {
439 			if (error == 0)
440 				error = EADDRNOTAVAIL;
441 			goto bad;
442 		}
443 		ip6->ip6_src = *in6a;
444 		if (in6p->in6p_route.ro_rt &&
445 		    in6p->in6p_route.ro_rt->rt_flags & RTF_UP)
446 			oifp = in6p->in6p_route.ro_rt->rt_ifp;
447 	}
448 
449 	ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
450 	ip6->ip6_vfc  &= ~IPV6_VERSION_MASK;
451 	ip6->ip6_vfc  |= IPV6_VERSION;
452 #if 0				/* ip6_plen will be filled in ip6_output. */
453 	ip6->ip6_plen  = htons((u_short)plen);
454 #endif
455 	ip6->ip6_nxt   = in6p->in6p_ip6.ip6_nxt;
456 	ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
457 
458 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
459 	    in6p->in6p_cksum != -1) {
460 		struct mbuf *n;
461 		int off;
462 		u_int16_t *sump;
463 		int sumoff;
464 
465 		/* compute checksum */
466 		if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
467 			off = offsetof(struct icmp6_hdr, icmp6_cksum);
468 		else
469 			off = in6p->in6p_cksum;
470 		if (plen < off + 1) {
471 			error = EINVAL;
472 			goto bad;
473 		}
474 		off += sizeof(struct ip6_hdr);
475 
476 		n = m_pulldown(m, off, sizeof(*sump), &sumoff);
477 		if (n == NULL) {
478 			m = NULL;
479 			error = ENOBUFS;
480 			goto bad;
481 		}
482 		sump = (u_int16_t *)(mtod(n, caddr_t) + sumoff);
483 		*sump = 0;
484 		*sump = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
485 	}
486 
487 	flags = 0;
488 	if (in6p->in6p_flags & IN6P_MINMTU)
489 		flags |= IPV6_MINMTU;
490 
491 	/* force routing domain */
492 	m->m_pkthdr.rdomain = in6p->inp_rtableid;
493 
494 	error = ip6_output(m, optp, &in6p->in6p_route, flags,
495 	    in6p->in6p_moptions, &oifp, in6p);
496 	if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
497 		if (oifp)
498 			icmp6_ifoutstat_inc(oifp, type, code);
499 		icmp6stat.icp6s_outhist[type]++;
500 	} else
501 		rip6stat.rip6s_opackets++;
502 
503 	goto freectl;
504 
505  bad:
506 	if (m)
507 		m_freem(m);
508 
509  freectl:
510 	if (control) {
511 		ip6_clearpktopts(&opt, -1);
512 		m_freem(control);
513 	}
514 	return (error);
515 }
516 
517 /*
518  * Raw IPv6 socket option processing.
519  */
520 int
521 rip6_ctloutput(int op, struct socket *so, int level, int optname,
522 	struct mbuf **mp)
523 {
524 #ifdef MROUTING
525 	int error = 0;
526 #endif
527 
528 	switch (level) {
529 	case IPPROTO_IPV6:
530 		switch (optname) {
531 #ifdef MROUTING
532 		case MRT6_INIT:
533 		case MRT6_DONE:
534 		case MRT6_ADD_MIF:
535 		case MRT6_DEL_MIF:
536 		case MRT6_ADD_MFC:
537 		case MRT6_DEL_MFC:
538 		case MRT6_PIM:
539 			if (op == PRCO_SETOPT) {
540 				error = ip6_mrouter_set(optname, so, *mp);
541 				if (*mp)
542 					(void)m_free(*mp);
543 			} else if (op == PRCO_GETOPT)
544 				error = ip6_mrouter_get(optname, so, mp);
545 			else
546 				error = EINVAL;
547 			return (error);
548 #endif
549 		case IPV6_CHECKSUM:
550 			return (ip6_raw_ctloutput(op, so, level, optname, mp));
551 		default:
552 			return (ip6_ctloutput(op, so, level, optname, mp));
553 		}
554 
555 	case IPPROTO_ICMPV6:
556 		/*
557 		 * XXX: is it better to call icmp6_ctloutput() directly
558 		 * from protosw?
559 		 */
560 		return (icmp6_ctloutput(op, so, level, optname, mp));
561 
562 	default:
563 		if (op == PRCO_SETOPT && *mp)
564 			m_free(*mp);
565 		return EINVAL;
566 	}
567 }
568 
569 extern	u_long rip6_sendspace;
570 extern	u_long rip6_recvspace;
571 
572 int
573 rip6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
574 	struct mbuf *control, struct proc *p)
575 {
576 	struct in6pcb *in6p = sotoin6pcb(so);
577 	int s;
578 	int error = 0;
579 	int priv;
580 
581 	priv = 0;
582 	if ((so->so_state & SS_PRIV) != 0)
583 		priv++;
584 
585 	if (req == PRU_CONTROL)
586 		return (in6_control(so, (u_long)m, (caddr_t)nam,
587 		    (struct ifnet *)control, p));
588 
589 	switch (req) {
590 	case PRU_ATTACH:
591 		if (in6p)
592 			panic("rip6_attach");
593 		if (!priv) {
594 			error = EACCES;
595 			break;
596 		}
597 		s = splsoftnet();
598 		if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) {
599 			splx(s);
600 			break;
601 		}
602 		if ((error = in_pcballoc(so, &rawin6pcbtable)) != 0)
603 		{
604 			splx(s);
605 			break;
606 		}
607 		splx(s);
608 		in6p = sotoin6pcb(so);
609 		in6p->in6p_ip6.ip6_nxt = (long)nam;
610 		in6p->in6p_cksum = -1;
611 
612 		in6p->in6p_icmp6filt = malloc(sizeof(struct icmp6_filter),
613 		    M_PCB, M_NOWAIT);
614 		if (in6p->in6p_icmp6filt == NULL) {
615 			in6_pcbdetach(in6p);
616 			error = ENOMEM;
617 			break;
618 		}
619 		ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
620 		break;
621 
622 	case PRU_DISCONNECT:
623 		if ((so->so_state & SS_ISCONNECTED) == 0) {
624 			error = ENOTCONN;
625 			break;
626 		}
627 		in6p->in6p_faddr = in6addr_any;
628 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
629 		break;
630 
631 	case PRU_ABORT:
632 		soisdisconnected(so);
633 		/* FALLTHROUGH */
634 	case PRU_DETACH:
635 		if (in6p == 0)
636 			panic("rip6_detach");
637 #ifdef MROUTING
638 		if (so == ip6_mrouter)
639 			ip6_mrouter_done();
640 #endif
641 		/* xxx: RSVP */
642 		if (in6p->in6p_icmp6filt) {
643 			free(in6p->in6p_icmp6filt, M_PCB);
644 			in6p->in6p_icmp6filt = NULL;
645 		}
646 		in6_pcbdetach(in6p);
647 		break;
648 
649 	case PRU_BIND:
650 	    {
651 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
652 		struct ifaddr *ia = NULL;
653 
654 		if (nam->m_len != sizeof(*addr)) {
655 			error = EINVAL;
656 			break;
657 		}
658 		if (TAILQ_EMPTY(&ifnet) || (addr->sin6_family != AF_INET6)) {
659 			error = EADDRNOTAVAIL;
660 			break;
661 		}
662 #ifdef ENABLE_DEFAULT_SCOPE
663 		if (addr->sin6_scope_id == 0)	/* not change if specified  */
664 			addr->sin6_scope_id =
665 			    scope6_addr2default(&addr->sin6_addr);
666 #endif
667 		/*
668 		 * we don't support mapped address here, it would confuse
669 		 * users so reject it
670 		 */
671 		if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
672 			error = EADDRNOTAVAIL;
673 			break;
674 		}
675 		/*
676 		 * Currently, ifa_ifwithaddr tends to fail for a link-local
677 		 * address, since it implicitly expects that the link ID
678 		 * for the address is embedded in the sin6_addr part.
679 		 * For now, we'd rather keep this "as is". We'll eventually fix
680 		 * this in a more natural way.
681 		 */
682 		if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
683 		    (ia = ifa_ifwithaddr((struct sockaddr *)addr,
684 		    in6p->inp_rtableid)) == 0) {
685 			error = EADDRNOTAVAIL;
686 			break;
687 		}
688 		if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
689 		    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
690 		     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
691 			error = EADDRNOTAVAIL;
692 			break;
693 		}
694 		in6p->in6p_laddr = addr->sin6_addr;
695 		break;
696 	    }
697 
698 	case PRU_CONNECT:
699 	{
700 		struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
701 		struct in6_addr *in6a = NULL;
702 #ifdef ENABLE_DEFAULT_SCOPE
703 		struct sockaddr_in6 sin6;
704 #endif
705 
706 		if (nam->m_len != sizeof(*addr)) {
707 			error = EINVAL;
708 			break;
709 		}
710 		if (TAILQ_EMPTY(&ifnet)) {
711 			error = EADDRNOTAVAIL;
712 			break;
713 		}
714 		if (addr->sin6_family != AF_INET6) {
715 			error = EAFNOSUPPORT;
716 			break;
717 		}
718 
719 #ifdef ENABLE_DEFAULT_SCOPE
720 		if (addr->sin6_scope_id == 0) {
721 			/* protect *addr */
722 			sin6 = *addr;
723 			addr = &sin6;
724 			addr->sin6_scope_id =
725 			    scope6_addr2default(&addr->sin6_addr);
726 		}
727 #endif
728 
729 		/* Source address selection. XXX: need pcblookup? */
730 		in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
731 		    in6p->in6p_moptions, &in6p->in6p_route,
732 		    &in6p->in6p_laddr, &error, in6p->inp_rtableid);
733 		if (in6a == NULL) {
734 			if (error == 0)
735 				error = EADDRNOTAVAIL;
736 			break;
737 		}
738 		in6p->in6p_laddr = *in6a;
739 		in6p->in6p_faddr = addr->sin6_addr;
740 		soisconnected(so);
741 		break;
742 	}
743 
744 	case PRU_CONNECT2:
745 		error = EOPNOTSUPP;
746 		break;
747 
748 	/*
749 	 * Mark the connection as being incapable of futther input.
750 	 */
751 	case PRU_SHUTDOWN:
752 		socantsendmore(so);
753 		break;
754 	/*
755 	 * Ship a packet out. The appropriate raw output
756 	 * routine handles any messaging necessary.
757 	 */
758 	case PRU_SEND:
759 	{
760 		struct sockaddr_in6 tmp;
761 		struct sockaddr_in6 *dst;
762 
763 		/* always copy sockaddr to avoid overwrites */
764 		if (so->so_state & SS_ISCONNECTED) {
765 			if (nam) {
766 				error = EISCONN;
767 				break;
768 			}
769 			/* XXX */
770 			bzero(&tmp, sizeof(tmp));
771 			tmp.sin6_family = AF_INET6;
772 			tmp.sin6_len = sizeof(struct sockaddr_in6);
773 			bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
774 			    sizeof(struct in6_addr));
775 			dst = &tmp;
776 		} else {
777 			if (nam == NULL) {
778 				error = ENOTCONN;
779 				break;
780 			}
781 			if (nam->m_len != sizeof(tmp)) {
782 				error = EINVAL;
783 				break;
784 			}
785 
786 			tmp = *mtod(nam, struct sockaddr_in6 *);
787 			dst = &tmp;
788 
789 			if (dst->sin6_family != AF_INET6) {
790 				error = EAFNOSUPPORT;
791 				break;
792 			}
793 		}
794 #ifdef ENABLE_DEFAULT_SCOPE
795 		if (dst->sin6_scope_id == 0) {
796 			dst->sin6_scope_id =
797 			    scope6_addr2default(&dst->sin6_addr);
798 		}
799 #endif
800 		error = rip6_output(m, so, dst, control);
801 		m = NULL;
802 		break;
803 	}
804 
805 	case PRU_SENSE:
806 		/*
807 		 * stat: don't bother with a blocksize
808 		 */
809 		return (0);
810 	/*
811 	 * Not supported.
812 	 */
813 	case PRU_RCVOOB:
814 	case PRU_RCVD:
815 	case PRU_LISTEN:
816 	case PRU_ACCEPT:
817 	case PRU_SENDOOB:
818 		error = EOPNOTSUPP;
819 		break;
820 
821 	case PRU_SOCKADDR:
822 		in6_setsockaddr(in6p, nam);
823 		break;
824 
825 	case PRU_PEERADDR:
826 		in6_setpeeraddr(in6p, nam);
827 		break;
828 
829 	default:
830 		panic("rip6_usrreq");
831 	}
832 	if (m != NULL)
833 		m_freem(m);
834 	return (error);
835 }
836 
837 int
838 rip6_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
839     void *newp, size_t newlen)
840 {
841 	/* All sysctl names at this level are terminal. */
842 	if (namelen != 1)
843 		return ENOTDIR;
844 
845 	switch (name[0]) {
846 	case RIPV6CTL_STATS:
847 		if (newp != NULL)
848 			return (EPERM);
849 		return (sysctl_struct(oldp, oldlenp, newp, newlen,
850 		    &rip6stat, sizeof(rip6stat)));
851 	default:
852 		return (EOPNOTSUPP);
853 	}
854 	/* NOTREACHED */
855 }
856