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