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