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