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