raw_ip6.c revision 1.58 1 /* $NetBSD: raw_ip6.c,v 1.58 2003/08/22 22:00:41 itojun 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.58 2003/08/22 22:00:41 itojun 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 flags = 0;
505 #ifdef IN6P_MINMTU
506 if (in6p->in6p_flags & IN6P_MINMTU)
507 flags |= IPV6_MINMTU;
508 #endif
509
510 error = ip6_output(m, optp, &in6p->in6p_route, flags,
511 in6p->in6p_moptions, so, &oifp);
512 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
513 if (oifp)
514 icmp6_ifoutstat_inc(oifp, type, code);
515 icmp6stat.icp6s_outhist[type]++;
516 } else
517 rip6stat.rip6s_opackets++;
518
519 goto freectl;
520
521 bad:
522 if (m)
523 m_freem(m);
524
525 freectl:
526 if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
527 RTFREE(optp->ip6po_route.ro_rt);
528 if (control)
529 m_freem(control);
530 return (error);
531 }
532
533 /*
534 * Raw IPv6 socket option processing.
535 */
536 int
537 rip6_ctloutput(op, so, level, optname, mp)
538 int op;
539 struct socket *so;
540 int level, optname;
541 struct mbuf **mp;
542 {
543 int error = 0;
544
545 switch (level) {
546 case IPPROTO_IPV6:
547 switch (optname) {
548 case MRT6_INIT:
549 case MRT6_DONE:
550 case MRT6_ADD_MIF:
551 case MRT6_DEL_MIF:
552 case MRT6_ADD_MFC:
553 case MRT6_DEL_MFC:
554 case MRT6_PIM:
555 if (op == PRCO_SETOPT) {
556 error = ip6_mrouter_set(optname, so, *mp);
557 if (*mp)
558 (void)m_free(*mp);
559 } else if (op == PRCO_GETOPT)
560 error = ip6_mrouter_get(optname, so, mp);
561 else
562 error = EINVAL;
563 return (error);
564 case IPV6_CHECKSUM:
565 return (ip6_raw_ctloutput(op, so, level, optname, mp));
566 default:
567 return (ip6_ctloutput(op, so, level, optname, mp));
568 }
569
570 case IPPROTO_ICMPV6:
571 /*
572 * XXX: is it better to call icmp6_ctloutput() directly
573 * from protosw?
574 */
575 return (icmp6_ctloutput(op, so, level, optname, mp));
576
577 default:
578 if (op == PRCO_SETOPT && *mp)
579 m_free(*mp);
580 return EINVAL;
581 }
582 }
583
584 extern u_long rip6_sendspace;
585 extern u_long rip6_recvspace;
586
587 int
588 rip6_usrreq(so, req, m, nam, control, p)
589 struct socket *so;
590 int req;
591 struct mbuf *m, *nam, *control;
592 struct proc *p;
593 {
594 struct in6pcb *in6p = sotoin6pcb(so);
595 int s;
596 int error = 0;
597 int priv;
598
599 priv = 0;
600 if (p && !suser(p->p_ucred, &p->p_acflag))
601 priv++;
602
603 if (req == PRU_CONTROL)
604 return (in6_control(so, (u_long)m, (caddr_t)nam,
605 (struct ifnet *)control, p));
606
607 if (req == PRU_PURGEIF) {
608 in6_pcbpurgeif0(&rawin6pcb, (struct ifnet *)control);
609 in6_purgeif((struct ifnet *)control);
610 in6_pcbpurgeif(&rawin6pcb, (struct ifnet *)control);
611 return (0);
612 }
613
614 switch (req) {
615 case PRU_ATTACH:
616 if (in6p)
617 panic("rip6_attach");
618 if (!priv) {
619 error = EACCES;
620 break;
621 }
622 s = splsoftnet();
623 if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) {
624 splx(s);
625 break;
626 }
627 if ((error = in6_pcballoc(so, &rawin6pcb)) != 0)
628 {
629 splx(s);
630 break;
631 }
632 splx(s);
633 in6p = sotoin6pcb(so);
634 in6p->in6p_ip6.ip6_nxt = (long)nam;
635 in6p->in6p_cksum = -1;
636
637 MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
638 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
639 if (in6p->in6p_icmp6filt == NULL) {
640 in6_pcbdetach(in6p);
641 error = ENOMEM;
642 break;
643 }
644 ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
645 break;
646
647 case PRU_DISCONNECT:
648 if ((so->so_state & SS_ISCONNECTED) == 0) {
649 error = ENOTCONN;
650 break;
651 }
652 in6p->in6p_faddr = in6addr_any;
653 so->so_state &= ~SS_ISCONNECTED; /* XXX */
654 break;
655
656 case PRU_ABORT:
657 soisdisconnected(so);
658 /* Fallthrough */
659 case PRU_DETACH:
660 if (in6p == 0)
661 panic("rip6_detach");
662 if (so == ip6_mrouter)
663 ip6_mrouter_done();
664 /* xxx: RSVP */
665 if (in6p->in6p_icmp6filt) {
666 FREE(in6p->in6p_icmp6filt, M_PCB);
667 in6p->in6p_icmp6filt = NULL;
668 }
669 in6_pcbdetach(in6p);
670 break;
671
672 case PRU_BIND:
673 {
674 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
675 struct ifaddr *ia = NULL;
676
677 if (nam->m_len != sizeof(*addr)) {
678 error = EINVAL;
679 break;
680 }
681 if ((ifnet.tqh_first == 0) || (addr->sin6_family != AF_INET6)) {
682 error = EADDRNOTAVAIL;
683 break;
684 }
685 #ifdef ENABLE_DEFAULT_SCOPE
686 if (addr->sin6_scope_id == 0) /* not change if specified */
687 addr->sin6_scope_id =
688 scope6_addr2default(&addr->sin6_addr);
689 #endif
690 /* KAME hack: embed scopeid */
691 if (in6_embedscope(&addr->sin6_addr, addr, in6p, NULL) != 0)
692 return EINVAL;
693 #ifndef SCOPEDROUTING
694 addr->sin6_scope_id = 0; /* for ifa_ifwithaddr */
695 #endif
696
697 /*
698 * we don't support mapped address here, it would confuse
699 * users so reject it
700 */
701 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
702 error = EADDRNOTAVAIL;
703 break;
704 }
705 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
706 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
707 error = EADDRNOTAVAIL;
708 break;
709 }
710 if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
711 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
712 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
713 error = EADDRNOTAVAIL;
714 break;
715 }
716 in6p->in6p_laddr = addr->sin6_addr;
717 break;
718 }
719
720 case PRU_CONNECT:
721 {
722 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
723 struct in6_addr *in6a = NULL;
724 #ifdef ENABLE_DEFAULT_SCOPE
725 struct sockaddr_in6 sin6;
726 #endif
727
728 if (nam->m_len != sizeof(*addr)) {
729 error = EINVAL;
730 break;
731 }
732 if (ifnet.tqh_first == 0)
733 {
734 error = EADDRNOTAVAIL;
735 break;
736 }
737 if (addr->sin6_family != AF_INET6) {
738 error = EAFNOSUPPORT;
739 break;
740 }
741
742 #ifdef ENABLE_DEFAULT_SCOPE
743 if (addr->sin6_scope_id == 0) {
744 /* protect *addr */
745 sin6 = *addr;
746 addr = &sin6;
747 addr->sin6_scope_id =
748 scope6_addr2default(&addr->sin6_addr);
749 }
750 #endif
751
752 /* Source address selection. XXX: need pcblookup? */
753 in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
754 in6p->in6p_moptions, &in6p->in6p_route,
755 &in6p->in6p_laddr, &error);
756 if (in6a == NULL) {
757 if (error == 0)
758 error = EADDRNOTAVAIL;
759 break;
760 }
761 in6p->in6p_laddr = *in6a;
762 in6p->in6p_faddr = addr->sin6_addr;
763 soisconnected(so);
764 break;
765 }
766
767 case PRU_CONNECT2:
768 error = EOPNOTSUPP;
769 break;
770
771 /*
772 * Mark the connection as being incapable of futther input.
773 */
774 case PRU_SHUTDOWN:
775 socantsendmore(so);
776 break;
777 /*
778 * Ship a packet out. The appropriate raw output
779 * routine handles any messaging necessary.
780 */
781 case PRU_SEND:
782 {
783 struct sockaddr_in6 tmp;
784 struct sockaddr_in6 *dst;
785
786 /* always copy sockaddr to avoid overwrites */
787 if (so->so_state & SS_ISCONNECTED) {
788 if (nam) {
789 error = EISCONN;
790 break;
791 }
792 /* XXX */
793 bzero(&tmp, sizeof(tmp));
794 tmp.sin6_family = AF_INET6;
795 tmp.sin6_len = sizeof(struct sockaddr_in6);
796 bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
797 sizeof(struct in6_addr));
798 dst = &tmp;
799 } else {
800 if (nam == NULL) {
801 error = ENOTCONN;
802 break;
803 }
804 if (nam->m_len != sizeof(tmp)) {
805 error = EINVAL;
806 break;
807 }
808
809 tmp = *mtod(nam, struct sockaddr_in6 *);
810 dst = &tmp;
811
812 if (dst->sin6_family != AF_INET6) {
813 error = EAFNOSUPPORT;
814 break;
815 }
816 }
817 #ifdef ENABLE_DEFAULT_SCOPE
818 if (dst->sin6_scope_id == 0) {
819 dst->sin6_scope_id =
820 scope6_addr2default(&dst->sin6_addr);
821 }
822 #endif
823 error = rip6_output(m, so, dst, control);
824 m = NULL;
825 break;
826 }
827
828 case PRU_SENSE:
829 /*
830 * stat: don't bother with a blocksize
831 */
832 return (0);
833 /*
834 * Not supported.
835 */
836 case PRU_RCVOOB:
837 case PRU_RCVD:
838 case PRU_LISTEN:
839 case PRU_ACCEPT:
840 case PRU_SENDOOB:
841 error = EOPNOTSUPP;
842 break;
843
844 case PRU_SOCKADDR:
845 in6_setsockaddr(in6p, nam);
846 break;
847
848 case PRU_PEERADDR:
849 in6_setpeeraddr(in6p, nam);
850 break;
851
852 default:
853 panic("rip6_usrreq");
854 }
855 if (m != NULL)
856 m_freem(m);
857 return (error);
858 }
859