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