raw_ip6.c revision 1.94 1 /* $NetBSD: raw_ip6.c,v 1.94 2008/04/15 03:57:04 thorpej 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.94 2008/04/15 03:57:04 thorpej Exp $");
66
67 #include "opt_ipsec.h"
68
69 #include <sys/param.h>
70 #include <sys/sysctl.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 #include <sys/kauth.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_private.h>
90 #include <netinet6/ip6_mroute.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/icmp6_private.h>
93 #include <netinet6/in6_pcb.h>
94 #include <netinet6/nd6.h>
95 #include <netinet6/ip6protosw.h>
96 #include <netinet6/scope6_var.h>
97 #include <netinet6/raw_ip6.h>
98
99 #ifdef IPSEC
100 #include <netinet6/ipsec.h>
101 #endif /* IPSEC */
102
103 #ifdef FAST_IPSEC
104 #include <netipsec/ipsec.h>
105 #include <netipsec/ipsec_var.h> /* XXX ipsecstat namespace */
106 #include <netipsec/ipsec6.h>
107 #endif
108
109 #include "faith.h"
110 #if defined(NFAITH) && 0 < NFAITH
111 #include <net/if_faith.h>
112 #endif
113
114 extern struct inpcbtable rawcbtable;
115 struct inpcbtable raw6cbtable;
116 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
117
118 /*
119 * Raw interface to IP6 protocol.
120 */
121
122 struct rip6stat rip6stat;
123
124 /*
125 * Initialize raw connection block queue.
126 */
127 void
128 rip6_init()
129 {
130
131 in6_pcbinit(&raw6cbtable, 1, 1);
132 }
133
134 /*
135 * Setup generic address and protocol structures
136 * for raw_input routine, then pass them along with
137 * mbuf chain.
138 */
139 int
140 rip6_input(struct mbuf **mp, int *offp, int proto)
141 {
142 struct mbuf *m = *mp;
143 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
144 struct inpcb_hdr *inph;
145 struct in6pcb *in6p;
146 struct in6pcb *last = NULL;
147 struct sockaddr_in6 rip6src;
148 struct mbuf *opts = NULL;
149
150 rip6stat.rip6s_ipackets++;
151
152 #if defined(NFAITH) && 0 < NFAITH
153 if (faithprefix(&ip6->ip6_dst)) {
154 /* send icmp6 host unreach? */
155 m_freem(m);
156 return IPPROTO_DONE;
157 }
158 #endif
159
160 /* Be proactive about malicious use of IPv4 mapped address */
161 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
162 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
163 /* XXX stat */
164 m_freem(m);
165 return IPPROTO_DONE;
166 }
167
168 sockaddr_in6_init(&rip6src, &ip6->ip6_src, 0, 0, 0);
169 if (sa6_recoverscope(&rip6src) != 0) {
170 /* XXX: should be impossible. */
171 m_freem(m);
172 return IPPROTO_DONE;
173 }
174
175 CIRCLEQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) {
176 in6p = (struct in6pcb *)inph;
177 if (in6p->in6p_af != AF_INET6)
178 continue;
179 if (in6p->in6p_ip6.ip6_nxt &&
180 in6p->in6p_ip6.ip6_nxt != proto)
181 continue;
182 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
183 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
184 continue;
185 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
186 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
187 continue;
188 if (in6p->in6p_cksum != -1) {
189 rip6stat.rip6s_isum++;
190 if (in6_cksum(m, proto, *offp,
191 m->m_pkthdr.len - *offp)) {
192 rip6stat.rip6s_badsum++;
193 continue;
194 }
195 }
196 if (last) {
197 struct mbuf *n;
198
199 #ifdef IPSEC
200 /*
201 * Check AH/ESP integrity.
202 */
203 if (ipsec6_in_reject(m, last)) {
204 ipsec6stat.in_polvio++;
205 /* do not inject data into pcb */
206 } else
207 #endif /* IPSEC */
208 #ifdef FAST_IPSEC
209 /*
210 * Check AH/ESP integrity
211 */
212 if (!ipsec6_in_reject(m,last))
213 #endif /* FAST_IPSEC */
214 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
215 if (last->in6p_flags & IN6P_CONTROLOPTS)
216 ip6_savecontrol(last, &opts, ip6, n);
217 /* strip intermediate headers */
218 m_adj(n, *offp);
219 if (sbappendaddr(&last->in6p_socket->so_rcv,
220 (struct sockaddr *)&rip6src, n, opts) == 0) {
221 /* should notify about lost packet */
222 m_freem(n);
223 if (opts)
224 m_freem(opts);
225 rip6stat.rip6s_fullsock++;
226 } else
227 sorwakeup(last->in6p_socket);
228 opts = NULL;
229 }
230 }
231 last = in6p;
232 }
233 #ifdef IPSEC
234 /*
235 * Check AH/ESP integrity.
236 */
237 if (last && ipsec6_in_reject(m, last)) {
238 m_freem(m);
239 ipsec6stat.in_polvio++;
240 IP6_STATDEC(IP6_STAT_DELIVERED);
241 /* do not inject data into pcb */
242 } else
243 #endif /* IPSEC */
244 #ifdef FAST_IPSEC
245 if (last && ipsec6_in_reject(m, last)) {
246 m_freem(m);
247 /*
248 * XXX ipsec6_in_reject update stat if there is an error
249 * so we just need to update stats by hand in the case of last is
250 * NULL
251 */
252 if (!last)
253 ipsec6stat.in_polvio++;
254 IP6_STATDEC(IP6_STAT_DELIVERED);
255 /* do not inject data into pcb */
256 } else
257 #endif /* FAST_IPSEC */
258 if (last) {
259 if (last->in6p_flags & IN6P_CONTROLOPTS)
260 ip6_savecontrol(last, &opts, ip6, m);
261 /* strip intermediate headers */
262 m_adj(m, *offp);
263 if (sbappendaddr(&last->in6p_socket->so_rcv,
264 (struct sockaddr *)&rip6src, m, opts) == 0) {
265 m_freem(m);
266 if (opts)
267 m_freem(opts);
268 rip6stat.rip6s_fullsock++;
269 } else
270 sorwakeup(last->in6p_socket);
271 } else {
272 rip6stat.rip6s_nosock++;
273 if (m->m_flags & M_MCAST)
274 rip6stat.rip6s_nosockmcast++;
275 if (proto == IPPROTO_NONE)
276 m_freem(m);
277 else {
278 u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
279 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
280 icmp6_error(m, ICMP6_PARAM_PROB,
281 ICMP6_PARAMPROB_NEXTHEADER,
282 prvnxtp - mtod(m, u_int8_t *));
283 }
284 IP6_STATDEC(IP6_STAT_DELIVERED);
285 }
286 return IPPROTO_DONE;
287 }
288
289 void
290 rip6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
291 {
292 struct ip6_hdr *ip6;
293 struct ip6ctlparam *ip6cp = NULL;
294 const struct sockaddr_in6 *sa6_src = NULL;
295 void *cmdarg;
296 void (*notify)(struct in6pcb *, int) = in6_rtchange;
297 int nxt;
298
299 if (sa->sa_family != AF_INET6 ||
300 sa->sa_len != sizeof(struct sockaddr_in6))
301 return;
302
303 if ((unsigned)cmd >= PRC_NCMDS)
304 return;
305 if (PRC_IS_REDIRECT(cmd))
306 notify = in6_rtchange, d = NULL;
307 else if (cmd == PRC_HOSTDEAD)
308 d = NULL;
309 else if (cmd == PRC_MSGSIZE)
310 ; /* special code is present, see below */
311 else if (inet6ctlerrmap[cmd] == 0)
312 return;
313
314 /* if the parameter is from icmp6, decode it. */
315 if (d != NULL) {
316 ip6cp = (struct ip6ctlparam *)d;
317 ip6 = ip6cp->ip6c_ip6;
318 cmdarg = ip6cp->ip6c_cmdarg;
319 sa6_src = ip6cp->ip6c_src;
320 nxt = ip6cp->ip6c_nxt;
321 } else {
322 ip6 = NULL;
323 cmdarg = NULL;
324 sa6_src = &sa6_any;
325 nxt = -1;
326 }
327
328 if (ip6 && cmd == PRC_MSGSIZE) {
329 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
330 int valid = 0;
331 struct in6pcb *in6p;
332
333 /*
334 * Check to see if we have a valid raw IPv6 socket
335 * corresponding to the address in the ICMPv6 message
336 * payload, and the protocol (ip6_nxt) meets the socket.
337 * XXX chase extension headers, or pass final nxt value
338 * from icmp6_notify_error()
339 */
340 in6p = NULL;
341 in6p = in6_pcblookup_connect(&raw6cbtable, &sa6->sin6_addr, 0,
342 (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0);
343 #if 0
344 if (!in6p) {
345 /*
346 * As the use of sendto(2) is fairly popular,
347 * we may want to allow non-connected pcb too.
348 * But it could be too weak against attacks...
349 * We should at least check if the local
350 * address (= s) is really ours.
351 */
352 in6p = in6_pcblookup_bind(&raw6cbtable,
353 &sa6->sin6_addr, 0, 0);
354 }
355 #endif
356
357 if (in6p && in6p->in6p_ip6.ip6_nxt &&
358 in6p->in6p_ip6.ip6_nxt == nxt)
359 valid++;
360
361 /*
362 * Depending on the value of "valid" and routing table
363 * size (mtudisc_{hi,lo}wat), we will:
364 * - recalculate the new MTU and create the
365 * corresponding routing entry, or
366 * - ignore the MTU change notification.
367 */
368 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
369
370 /*
371 * regardless of if we called icmp6_mtudisc_update(),
372 * we need to call in6_pcbnotify(), to notify path MTU
373 * change to the userland (RFC3542), because some
374 * unconnected sockets may share the same destination
375 * and want to know the path MTU.
376 */
377 }
378
379 (void) in6_pcbnotify(&raw6cbtable, sa, 0,
380 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
381 }
382
383 /*
384 * Generate IPv6 header and pass packet to ip6_output.
385 * Tack on options user may have setup with control call.
386 */
387 int
388 rip6_output(struct mbuf *m, struct socket *so, struct sockaddr_in6 *dstsock,
389 struct mbuf *control)
390 {
391 struct in6_addr *dst;
392 struct ip6_hdr *ip6;
393 struct in6pcb *in6p;
394 u_int plen = m->m_pkthdr.len;
395 int error = 0;
396 struct ip6_pktopts opt, *optp = NULL;
397 struct ifnet *oifp = NULL;
398 int type, code; /* for ICMPv6 output statistics only */
399 int priv = 0;
400 int scope_ambiguous = 0;
401 struct in6_addr *in6a;
402
403 in6p = sotoin6pcb(so);
404
405 priv = 0;
406 if (curlwp && !kauth_authorize_generic(curlwp->l_cred,
407 KAUTH_GENERIC_ISSUSER, NULL))
408 priv = 1;
409
410 dst = &dstsock->sin6_addr;
411 if (control) {
412 if ((error = ip6_setpktopts(control, &opt,
413 in6p->in6p_outputopts,
414 priv, so->so_proto->pr_protocol)) != 0) {
415 goto bad;
416 }
417 optp = &opt;
418 } else
419 optp = in6p->in6p_outputopts;
420
421 /*
422 * Check and convert scope zone ID into internal form.
423 * XXX: we may still need to determine the zone later.
424 */
425 if (!(so->so_state & SS_ISCONNECTED)) {
426 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
427 scope_ambiguous = 1;
428 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
429 goto bad;
430 }
431
432 /*
433 * For an ICMPv6 packet, we should know its type and code
434 * to update statistics.
435 */
436 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
437 struct icmp6_hdr *icmp6;
438 if (m->m_len < sizeof(struct icmp6_hdr) &&
439 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
440 error = ENOBUFS;
441 goto bad;
442 }
443 icmp6 = mtod(m, struct icmp6_hdr *);
444 type = icmp6->icmp6_type;
445 code = icmp6->icmp6_code;
446 } else {
447 type = 0;
448 code = 0;
449 }
450
451 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
452 if (!m) {
453 error = ENOBUFS;
454 goto bad;
455 }
456 ip6 = mtod(m, struct ip6_hdr *);
457
458 /*
459 * Next header might not be ICMP6 but use its pseudo header anyway.
460 */
461 ip6->ip6_dst = *dst;
462
463 /*
464 * Source address selection.
465 */
466 if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
467 (struct route *)&in6p->in6p_route, &in6p->in6p_laddr, &oifp,
468 &error)) == 0) {
469 if (error == 0)
470 error = EADDRNOTAVAIL;
471 goto bad;
472 }
473 ip6->ip6_src = *in6a;
474
475 if (oifp && scope_ambiguous) {
476 /*
477 * Application should provide a proper zone ID or the use of
478 * default zone IDs should be enabled. Unfortunately, some
479 * applications do not behave as it should, so we need a
480 * workaround. Even if an appropriate ID is not determined
481 * (when it's required), if we can determine the outgoing
482 * interface. determine the zone ID based on the interface.
483 */
484 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
485 if (error != 0)
486 goto bad;
487 }
488 ip6->ip6_dst = dstsock->sin6_addr;
489
490 /* fill in the rest of the IPv6 header fields */
491 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
492 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
493 ip6->ip6_vfc |= IPV6_VERSION;
494 /* ip6_plen will be filled in ip6_output, so not fill it here. */
495 ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt;
496 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
497
498 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
499 in6p->in6p_cksum != -1) {
500 int off;
501 u_int16_t sum;
502
503 /* compute checksum */
504 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
505 off = offsetof(struct icmp6_hdr, icmp6_cksum);
506 else
507 off = in6p->in6p_cksum;
508 if (plen < off + 1) {
509 error = EINVAL;
510 goto bad;
511 }
512 off += sizeof(struct ip6_hdr);
513
514 sum = 0;
515 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
516 M_DONTWAIT);
517 if (m == NULL) {
518 error = ENOBUFS;
519 goto bad;
520 }
521 sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
522 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
523 M_DONTWAIT);
524 if (m == NULL) {
525 error = ENOBUFS;
526 goto bad;
527 }
528 }
529
530 error = ip6_output(m, optp, &in6p->in6p_route, 0,
531 in6p->in6p_moptions, so, &oifp);
532 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
533 if (oifp)
534 icmp6_ifoutstat_inc(oifp, type, code);
535 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
536 } else
537 rip6stat.rip6s_opackets++;
538
539 goto freectl;
540
541 bad:
542 if (m)
543 m_freem(m);
544
545 freectl:
546 if (control) {
547 ip6_clearpktopts(&opt, -1);
548 m_freem(control);
549 }
550 return error;
551 }
552
553 /*
554 * Raw IPv6 socket option processing.
555 */
556 int
557 rip6_ctloutput(int op, struct socket *so, int level, int optname,
558 struct mbuf **mp)
559 {
560 int error = 0;
561
562 if (level == SOL_SOCKET && optname == SO_NOHEADER) {
563 /* need to fiddle w/ opt(IPPROTO_IPV6, IPV6_CHECKSUM)? */
564 if (op == PRCO_GETOPT) {
565 *mp = m_intopt(so, 1);
566 return 0;
567 } else if (*mp == NULL || (*mp)->m_len != sizeof(int))
568 error = EINVAL;
569 else if (*mtod(*mp, int *) == 0)
570 error = EINVAL;
571 goto free_m;
572 } else if (level != IPPROTO_IPV6)
573 return ip6_ctloutput(op, so, level, optname, mp);
574
575 switch (optname) {
576 case MRT6_INIT:
577 case MRT6_DONE:
578 case MRT6_ADD_MIF:
579 case MRT6_DEL_MIF:
580 case MRT6_ADD_MFC:
581 case MRT6_DEL_MFC:
582 case MRT6_PIM:
583 if (op == PRCO_SETOPT)
584 error = ip6_mrouter_set(optname, so, *mp);
585 else if (op == PRCO_GETOPT)
586 error = ip6_mrouter_get(optname, so, mp);
587 else
588 error = EINVAL;
589 break;
590 case IPV6_CHECKSUM:
591 return ip6_raw_ctloutput(op, so, level, optname, mp);
592 default:
593 return ip6_ctloutput(op, so, level, optname, mp);
594 }
595 free_m:
596 if (op == PRCO_SETOPT && *mp != NULL)
597 m_free(*mp);
598 return error;
599 }
600
601 extern u_long rip6_sendspace;
602 extern u_long rip6_recvspace;
603
604 int
605 rip6_usrreq(struct socket *so, int req, struct mbuf *m,
606 struct mbuf *nam, struct mbuf *control, struct lwp *l)
607 {
608 struct in6pcb *in6p = sotoin6pcb(so);
609 int s;
610 int error = 0;
611 int priv;
612
613 priv = 0;
614 if (l && !kauth_authorize_generic(l->l_cred,
615 KAUTH_GENERIC_ISSUSER, NULL))
616 priv++;
617
618 if (req == PRU_CONTROL)
619 return in6_control(so, (u_long)m, (void *)nam,
620 (struct ifnet *)control, l);
621
622 if (req == PRU_PURGEIF) {
623 in6_pcbpurgeif0(&raw6cbtable, (struct ifnet *)control);
624 in6_purgeif((struct ifnet *)control);
625 in6_pcbpurgeif(&raw6cbtable, (struct ifnet *)control);
626 return 0;
627 }
628
629 switch (req) {
630 case PRU_ATTACH:
631 if (in6p != NULL)
632 panic("rip6_attach");
633 if (!priv) {
634 error = EACCES;
635 break;
636 }
637 s = splsoftnet();
638 error = soreserve(so, rip6_sendspace, rip6_recvspace);
639 if (error != 0) {
640 splx(s);
641 break;
642 }
643 if ((error = in6_pcballoc(so, &raw6cbtable)) != 0) {
644 splx(s);
645 break;
646 }
647 splx(s);
648 in6p = sotoin6pcb(so);
649 in6p->in6p_ip6.ip6_nxt = (long)nam;
650 in6p->in6p_cksum = -1;
651
652 MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
653 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
654 if (in6p->in6p_icmp6filt == NULL) {
655 in6_pcbdetach(in6p);
656 error = ENOMEM;
657 break;
658 }
659 ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
660 break;
661
662 case PRU_DISCONNECT:
663 if ((so->so_state & SS_ISCONNECTED) == 0) {
664 error = ENOTCONN;
665 break;
666 }
667 in6p->in6p_faddr = in6addr_any;
668 so->so_state &= ~SS_ISCONNECTED; /* XXX */
669 break;
670
671 case PRU_ABORT:
672 soisdisconnected(so);
673 /* Fallthrough */
674 case PRU_DETACH:
675 if (in6p == NULL)
676 panic("rip6_detach");
677 if (so == ip6_mrouter)
678 ip6_mrouter_done();
679 /* xxx: RSVP */
680 if (in6p->in6p_icmp6filt != NULL) {
681 FREE(in6p->in6p_icmp6filt, M_PCB);
682 in6p->in6p_icmp6filt = NULL;
683 }
684 in6_pcbdetach(in6p);
685 break;
686
687 case PRU_BIND:
688 {
689 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
690 struct ifaddr *ia = NULL;
691
692 if (nam->m_len != sizeof(*addr)) {
693 error = EINVAL;
694 break;
695 }
696 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6) {
697 error = EADDRNOTAVAIL;
698 break;
699 }
700 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
701 break;
702
703 /*
704 * we don't support mapped address here, it would confuse
705 * users so reject it
706 */
707 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
708 error = EADDRNOTAVAIL;
709 break;
710 }
711 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
712 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
713 error = EADDRNOTAVAIL;
714 break;
715 }
716 if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
717 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
718 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
719 error = EADDRNOTAVAIL;
720 break;
721 }
722 in6p->in6p_laddr = addr->sin6_addr;
723 break;
724 }
725
726 case PRU_CONNECT:
727 {
728 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
729 struct in6_addr *in6a = NULL;
730 struct ifnet *ifp = NULL;
731 int scope_ambiguous = 0;
732
733 if (nam->m_len != sizeof(*addr)) {
734 error = EINVAL;
735 break;
736 }
737 if (TAILQ_EMPTY(&ifnet)) {
738 error = EADDRNOTAVAIL;
739 break;
740 }
741 if (addr->sin6_family != AF_INET6) {
742 error = EAFNOSUPPORT;
743 break;
744 }
745
746 /*
747 * Application should provide a proper zone ID or the use of
748 * default zone IDs should be enabled. Unfortunately, some
749 * applications do not behave as it should, so we need a
750 * workaround. Even if an appropriate ID is not determined,
751 * we'll see if we can determine the outgoing interface. If we
752 * can, determine the zone ID based on the interface below.
753 */
754 if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
755 scope_ambiguous = 1;
756 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
757 return error;
758
759 /* Source address selection. XXX: need pcblookup? */
760 in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
761 in6p->in6p_moptions, (struct route *)&in6p->in6p_route,
762 &in6p->in6p_laddr, &ifp, &error);
763 if (in6a == NULL) {
764 if (error == 0)
765 error = EADDRNOTAVAIL;
766 break;
767 }
768 /* XXX: see above */
769 if (ifp && scope_ambiguous &&
770 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
771 break;
772 }
773 in6p->in6p_laddr = *in6a;
774 in6p->in6p_faddr = addr->sin6_addr;
775 soisconnected(so);
776 break;
777 }
778
779 case PRU_CONNECT2:
780 error = EOPNOTSUPP;
781 break;
782
783 /*
784 * Mark the connection as being incapable of futther input.
785 */
786 case PRU_SHUTDOWN:
787 socantsendmore(so);
788 break;
789 /*
790 * Ship a packet out. The appropriate raw output
791 * routine handles any messaging necessary.
792 */
793 case PRU_SEND:
794 {
795 struct sockaddr_in6 tmp;
796 struct sockaddr_in6 *dst;
797
798 /* always copy sockaddr to avoid overwrites */
799 if (so->so_state & SS_ISCONNECTED) {
800 if (nam) {
801 error = EISCONN;
802 break;
803 }
804 /* XXX */
805 sockaddr_in6_init(&tmp, &in6p->in6p_faddr, 0, 0, 0);
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 error = rip6_output(m, so, dst, control);
826 m = NULL;
827 break;
828 }
829
830 case PRU_SENSE:
831 /*
832 * stat: don't bother with a blocksize
833 */
834 return 0;
835 /*
836 * Not supported.
837 */
838 case PRU_RCVOOB:
839 case PRU_RCVD:
840 case PRU_LISTEN:
841 case PRU_ACCEPT:
842 case PRU_SENDOOB:
843 error = EOPNOTSUPP;
844 break;
845
846 case PRU_SOCKADDR:
847 in6_setsockaddr(in6p, nam);
848 break;
849
850 case PRU_PEERADDR:
851 in6_setpeeraddr(in6p, nam);
852 break;
853
854 default:
855 panic("rip6_usrreq");
856 }
857 if (m != NULL)
858 m_freem(m);
859 return error;
860 }
861
862 SYSCTL_SETUP(sysctl_net_inet6_raw6_setup, "sysctl net.inet6.raw6 subtree setup")
863 {
864
865 sysctl_createv(clog, 0, NULL, NULL,
866 CTLFLAG_PERMANENT,
867 CTLTYPE_NODE, "net", NULL,
868 NULL, 0, NULL, 0,
869 CTL_NET, CTL_EOL);
870 sysctl_createv(clog, 0, NULL, NULL,
871 CTLFLAG_PERMANENT,
872 CTLTYPE_NODE, "inet6", NULL,
873 NULL, 0, NULL, 0,
874 CTL_NET, PF_INET6, CTL_EOL);
875 sysctl_createv(clog, 0, NULL, NULL,
876 CTLFLAG_PERMANENT,
877 CTLTYPE_NODE, "raw6",
878 SYSCTL_DESCR("Raw IPv6 settings"),
879 NULL, 0, NULL, 0,
880 CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL);
881
882 sysctl_createv(clog, 0, NULL, NULL,
883 CTLFLAG_PERMANENT,
884 CTLTYPE_STRUCT, "pcblist",
885 SYSCTL_DESCR("Raw IPv6 control block list"),
886 sysctl_inpcblist, 0, &raw6cbtable, 0,
887 CTL_NET, PF_INET6, IPPROTO_RAW,
888 CTL_CREATE, CTL_EOL);
889 sysctl_createv(clog, 0, NULL, NULL,
890 CTLFLAG_PERMANENT,
891 CTLTYPE_STRUCT, "stats",
892 SYSCTL_DESCR("Raw IPv6 statistics"),
893 NULL, 0, &rip6stat, sizeof(rip6stat),
894 CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS,
895 CTL_EOL);
896 }
897