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