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