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