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