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