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