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