raw_ip6.c revision 1.157.2.6 1 /* $NetBSD: raw_ip6.c,v 1.157.2.6 2023/03/23 12:08:39 martin 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.157.2.6 2023/03/23 12:08:39 martin Exp $");
66
67 #ifdef _KERNEL_OPT
68 #include "opt_ipsec.h"
69 #include "opt_net_mpsafe.h"
70 #endif
71
72 #include <sys/param.h>
73 #include <sys/sysctl.h>
74 #include <sys/mbuf.h>
75 #include <sys/socket.h>
76 #include <sys/protosw.h>
77 #include <sys/socketvar.h>
78 #include <sys/systm.h>
79 #include <sys/proc.h>
80 #include <sys/kauth.h>
81 #include <sys/kmem.h>
82
83 #include <net/if.h>
84 #include <net/if_types.h>
85 #include <net/net_stats.h>
86
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/ip6_private.h>
92 #include <netinet6/ip6_mroute.h>
93 #include <netinet/icmp6.h>
94 #include <netinet6/icmp6_private.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/ip6protosw.h>
97 #include <netinet6/scope6_var.h>
98 #include <netinet6/raw_ip6.h>
99
100 #ifdef IPSEC
101 #include <netipsec/ipsec.h>
102 #include <netipsec/ipsec_var.h>
103 #include <netipsec/ipsec_private.h>
104 #include <netipsec/ipsec6.h>
105 #endif
106
107 #include "faith.h"
108 #if defined(NFAITH) && 0 < NFAITH
109 #include <net/if_faith.h>
110 #endif
111
112 extern struct inpcbtable rawcbtable;
113 struct inpcbtable raw6cbtable;
114 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
115
116 /*
117 * Raw interface to IP6 protocol.
118 */
119
120 static percpu_t *rip6stat_percpu;
121
122 #define RIP6_STATINC(x) _NET_STATINC(rip6stat_percpu, x)
123
124 static void sysctl_net_inet6_raw6_setup(struct sysctllog **);
125
126 /*
127 * Initialize raw connection block queue.
128 */
129 void
130 rip6_init(void)
131 {
132
133 sysctl_net_inet6_raw6_setup(NULL);
134 in6_pcbinit(&raw6cbtable, 1, 1);
135
136 rip6stat_percpu = percpu_alloc(sizeof(uint64_t) * RIP6_NSTATS);
137 }
138
139 /*
140 * Setup generic address and protocol structures
141 * for raw_input routine, then pass them along with
142 * mbuf chain.
143 */
144 int
145 rip6_input(struct mbuf **mp, int *offp, int proto)
146 {
147 struct mbuf *m = *mp;
148 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
149 struct inpcb_hdr *inph;
150 struct in6pcb *in6p;
151 struct in6pcb *last = NULL;
152 struct sockaddr_in6 rip6src;
153 struct mbuf *opts = NULL;
154
155 RIP6_STATINC(RIP6_STAT_IPACKETS);
156
157 #if defined(NFAITH) && 0 < NFAITH
158 if (faithprefix(&ip6->ip6_dst)) {
159 /* send icmp6 host unreach? */
160 m_freem(m);
161 return IPPROTO_DONE;
162 }
163 #endif
164
165 /* Be proactive about malicious use of IPv4 mapped address */
166 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
167 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
168 /* XXX stat */
169 m_freem(m);
170 return IPPROTO_DONE;
171 }
172
173 sockaddr_in6_init(&rip6src, &ip6->ip6_src, 0, 0, 0);
174 if (sa6_recoverscope(&rip6src) != 0) {
175 /* XXX: should be impossible. */
176 m_freem(m);
177 return IPPROTO_DONE;
178 }
179
180 TAILQ_FOREACH(inph, &raw6cbtable.inpt_queue, inph_queue) {
181 in6p = (struct in6pcb *)inph;
182 if (in6p->in6p_af != AF_INET6)
183 continue;
184 if (in6p->in6p_ip6.ip6_nxt &&
185 in6p->in6p_ip6.ip6_nxt != proto)
186 continue;
187 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
188 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
189 continue;
190 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
191 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
192 continue;
193 if (in6p->in6p_cksum != -1) {
194 RIP6_STATINC(RIP6_STAT_ISUM);
195 /*
196 * Although in6_cksum() does not need the position of
197 * the checksum field for verification, enforce that it
198 * is located within the packet. Userland has given
199 * a checksum offset, a packet too short for that is
200 * invalid. Avoid overflow with user supplied offset.
201 */
202 if (m->m_pkthdr.len < *offp + 2 ||
203 m->m_pkthdr.len - *offp - 2 < in6p->in6p_cksum ||
204 in6_cksum(m, proto, *offp,
205 m->m_pkthdr.len - *offp)) {
206 RIP6_STATINC(RIP6_STAT_BADSUM);
207 continue;
208 }
209 }
210 if (last) {
211 struct mbuf *n;
212
213 #ifdef IPSEC
214 /*
215 * Check AH/ESP integrity
216 */
217 if (!ipsec_used ||
218 (ipsec_used && !ipsec6_in_reject(m, last)))
219 #endif /* IPSEC */
220 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
221 if (last->in6p_flags & IN6P_CONTROLOPTS)
222 ip6_savecontrol(last, &opts, ip6, n);
223 /* strip intermediate headers */
224 m_adj(n, *offp);
225 if (sbappendaddr(&last->in6p_socket->so_rcv,
226 sin6tosa(&rip6src), n, opts) == 0) {
227 soroverflow(last->in6p_socket);
228 m_freem(n);
229 if (opts)
230 m_freem(opts);
231 RIP6_STATINC(RIP6_STAT_FULLSOCK);
232 } else
233 sorwakeup(last->in6p_socket);
234 opts = NULL;
235 }
236 }
237 last = in6p;
238 }
239 #ifdef IPSEC
240 if (ipsec_used && last && ipsec6_in_reject(m, last)) {
241 m_freem(m);
242 /*
243 * XXX ipsec6_in_reject update stat if there is an error
244 * so we just need to update stats by hand in the case of last is
245 * NULL
246 */
247 if (!last)
248 IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
249 IP6_STATDEC(IP6_STAT_DELIVERED);
250 /* do not inject data into pcb */
251 } else
252 #endif /* IPSEC */
253 if (last) {
254 if (last->in6p_flags & IN6P_CONTROLOPTS)
255 ip6_savecontrol(last, &opts, ip6, m);
256 /* strip intermediate headers */
257 m_adj(m, *offp);
258 if (sbappendaddr(&last->in6p_socket->so_rcv,
259 sin6tosa(&rip6src), m, opts) == 0) {
260 soroverflow(last->in6p_socket);
261 m_freem(m);
262 if (opts)
263 m_freem(opts);
264 RIP6_STATINC(RIP6_STAT_FULLSOCK);
265 } else
266 sorwakeup(last->in6p_socket);
267 } else {
268 RIP6_STATINC(RIP6_STAT_NOSOCK);
269 if (m->m_flags & M_MCAST)
270 RIP6_STATINC(RIP6_STAT_NOSOCKMCAST);
271 if (proto == IPPROTO_NONE)
272 m_freem(m);
273 else {
274 int s;
275 struct ifnet *rcvif = m_get_rcvif(m, &s);
276 const int prvnxt = ip6_get_prevhdr(m, *offp);
277 in6_ifstat_inc(rcvif, ifs6_in_protounknown);
278 m_put_rcvif(rcvif, &s);
279 icmp6_error(m, ICMP6_PARAM_PROB,
280 ICMP6_PARAMPROB_NEXTHEADER,
281 prvnxt);
282 }
283 IP6_STATDEC(IP6_STAT_DELIVERED);
284 }
285 return IPPROTO_DONE;
286 }
287
288 void *
289 rip6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
290 {
291 struct ip6_hdr *ip6;
292 struct ip6ctlparam *ip6cp = NULL;
293 const struct sockaddr_in6 *sa6_src = NULL;
294 void *cmdarg;
295 void (*notify)(struct in6pcb *, int) = in6_rtchange;
296 int nxt;
297
298 if (sa->sa_family != AF_INET6 ||
299 sa->sa_len != sizeof(struct sockaddr_in6))
300 return NULL;
301
302 if ((unsigned)cmd >= PRC_NCMDS)
303 return NULL;
304 if (PRC_IS_REDIRECT(cmd))
305 notify = in6_rtchange, d = NULL;
306 else if (cmd == PRC_HOSTDEAD)
307 d = NULL;
308 else if (cmd == PRC_MSGSIZE)
309 ; /* special code is present, see below */
310 else if (inet6ctlerrmap[cmd] == 0)
311 return NULL;
312
313 /* if the parameter is from icmp6, decode it. */
314 if (d != NULL) {
315 ip6cp = (struct ip6ctlparam *)d;
316 ip6 = ip6cp->ip6c_ip6;
317 cmdarg = ip6cp->ip6c_cmdarg;
318 sa6_src = ip6cp->ip6c_src;
319 nxt = ip6cp->ip6c_nxt;
320 } else {
321 ip6 = NULL;
322 cmdarg = NULL;
323 sa6_src = &sa6_any;
324 nxt = -1;
325 }
326
327 if (ip6 && cmd == PRC_MSGSIZE) {
328 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
329 int valid = 0;
330 struct in6pcb *in6p;
331
332 /*
333 * Check to see if we have a valid raw IPv6 socket
334 * corresponding to the address in the ICMPv6 message
335 * payload, and the protocol (ip6_nxt) meets the socket.
336 * XXX chase extension headers, or pass final nxt value
337 * from icmp6_notify_error()
338 */
339 in6p = NULL;
340 in6p = in6_pcblookup_connect(&raw6cbtable, &sa6->sin6_addr, 0,
341 (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0, 0);
342 #if 0
343 if (!in6p) {
344 /*
345 * As the use of sendto(2) is fairly popular,
346 * we may want to allow non-connected pcb too.
347 * But it could be too weak against attacks...
348 * We should at least check if the local
349 * address (= s) is really ours.
350 */
351 in6p = in6_pcblookup_bind(&raw6cbtable,
352 &sa6->sin6_addr, 0, 0);
353 }
354 #endif
355
356 if (in6p && in6p->in6p_ip6.ip6_nxt &&
357 in6p->in6p_ip6.ip6_nxt == nxt)
358 valid++;
359
360 /*
361 * Depending on the value of "valid" and routing table
362 * size (mtudisc_{hi,lo}wat), we will:
363 * - recalculate the new MTU and create the
364 * corresponding routing entry, or
365 * - ignore the MTU change notification.
366 */
367 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
368
369 /*
370 * regardless of if we called icmp6_mtudisc_update(),
371 * we need to call in6_pcbnotify(), to notify path MTU
372 * change to the userland (RFC3542), because some
373 * unconnected sockets may share the same destination
374 * and want to know the path MTU.
375 */
376 }
377
378 (void) in6_pcbnotify(&raw6cbtable, sa, 0,
379 sin6tocsa(sa6_src), 0, cmd, cmdarg, notify);
380 return NULL;
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 * const so,
389 struct sockaddr_in6 * const dstsock, struct mbuf * const 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 scope_ambiguous = 0;
400 int bound = curlwp_bind();
401 struct psref psref;
402
403 in6p = sotoin6pcb(so);
404
405 dst = &dstsock->sin6_addr;
406 if (control) {
407 if ((error = ip6_setpktopts(control, &opt,
408 in6p->in6p_outputopts,
409 kauth_cred_get(), so->so_proto->pr_protocol)) != 0) {
410 goto bad;
411 }
412 optp = &opt;
413 } else
414 optp = in6p->in6p_outputopts;
415
416 /*
417 * Check and convert scope zone ID into internal form.
418 * XXX: we may still need to determine the zone later.
419 */
420 if (!(so->so_state & SS_ISCONNECTED)) {
421 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
422 scope_ambiguous = 1;
423 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
424 goto bad;
425 }
426
427 /*
428 * For an ICMPv6 packet, we should know its type and code
429 * to update statistics.
430 */
431 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
432 struct icmp6_hdr *icmp6;
433 if (m->m_len < sizeof(struct icmp6_hdr) &&
434 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
435 error = ENOBUFS;
436 goto bad;
437 }
438 icmp6 = mtod(m, struct icmp6_hdr *);
439 type = icmp6->icmp6_type;
440 code = icmp6->icmp6_code;
441 } else {
442 type = 0;
443 code = 0;
444 }
445
446 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
447 if (!m) {
448 error = ENOBUFS;
449 goto bad;
450 }
451 ip6 = mtod(m, struct ip6_hdr *);
452
453 /*
454 * Next header might not be ICMP6 but use its pseudo header anyway.
455 */
456 ip6->ip6_dst = *dst;
457
458 /*
459 * Source address selection.
460 */
461 error = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
462 &in6p->in6p_route, &in6p->in6p_laddr, &oifp, &psref, &ip6->ip6_src);
463 if (error != 0)
464 goto bad;
465
466 if (oifp && scope_ambiguous) {
467 /*
468 * Application should provide a proper zone ID or the use of
469 * default zone IDs should be enabled. Unfortunately, some
470 * applications do not behave as it should, so we need a
471 * workaround. Even if an appropriate ID is not determined
472 * (when it's required), if we can determine the outgoing
473 * interface. determine the zone ID based on the interface.
474 */
475 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
476 if (error != 0)
477 goto bad;
478 }
479 ip6->ip6_dst = dstsock->sin6_addr;
480
481 /* fill in the rest of the IPv6 header fields */
482 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
483 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
484 ip6->ip6_vfc |= IPV6_VERSION;
485 /* ip6_plen will be filled in ip6_output, so not fill it here. */
486 ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt;
487 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
488
489 if_put(oifp, &psref);
490 oifp = NULL;
491
492 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
493 in6p->in6p_cksum != -1) {
494 const uint8_t nxt = ip6->ip6_nxt;
495 int off;
496 u_int16_t sum;
497
498 /* compute checksum */
499 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
500 off = offsetof(struct icmp6_hdr, icmp6_cksum);
501 else
502 off = in6p->in6p_cksum;
503 if (plen < 2 || plen - 2 < off) {
504 error = EINVAL;
505 goto bad;
506 }
507 off += sizeof(struct ip6_hdr);
508
509 sum = 0;
510 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
511 M_DONTWAIT);
512 if (m == NULL) {
513 error = ENOBUFS;
514 goto bad;
515 }
516 sum = in6_cksum(m, nxt, sizeof(*ip6), plen);
517 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
518 M_DONTWAIT);
519 if (m == NULL) {
520 error = ENOBUFS;
521 goto bad;
522 }
523 }
524
525 {
526 struct ifnet *ret_oifp = NULL;
527
528 error = ip6_output(m, optp, &in6p->in6p_route, 0,
529 in6p->in6p_moptions, in6p, &ret_oifp);
530 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
531 if (ret_oifp)
532 icmp6_ifoutstat_inc(ret_oifp, type, code);
533 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
534 } else
535 RIP6_STATINC(RIP6_STAT_OPACKETS);
536 }
537
538 goto freectl;
539
540 bad:
541 if (m)
542 m_freem(m);
543
544 freectl:
545 if (control) {
546 ip6_clearpktopts(&opt, -1);
547 m_freem(control);
548 }
549 if_put(oifp, &psref);
550 curlwp_bindx(bound);
551 return error;
552 }
553
554 /*
555 * Raw IPv6 socket option processing.
556 */
557 int
558 rip6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
559 {
560 int error = 0;
561
562 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
563 int optval;
564
565 /* need to fiddle w/ opt(IPPROTO_IPV6, IPV6_CHECKSUM)? */
566 if (op == PRCO_GETOPT) {
567 optval = 1;
568 error = sockopt_set(sopt, &optval, sizeof(optval));
569 } else if (op == PRCO_SETOPT) {
570 error = sockopt_getint(sopt, &optval);
571 if (error)
572 goto out;
573 if (optval == 0)
574 error = EINVAL;
575 }
576
577 goto out;
578 } else if (sopt->sopt_level != IPPROTO_IPV6)
579 return ip6_ctloutput(op, so, sopt);
580
581 switch (sopt->sopt_name) {
582 case MRT6_INIT:
583 case MRT6_DONE:
584 case MRT6_ADD_MIF:
585 case MRT6_DEL_MIF:
586 case MRT6_ADD_MFC:
587 case MRT6_DEL_MFC:
588 case MRT6_PIM:
589 if (op == PRCO_SETOPT)
590 error = ip6_mrouter_set(so, sopt);
591 else if (op == PRCO_GETOPT)
592 error = ip6_mrouter_get(so, sopt);
593 else
594 error = EINVAL;
595 break;
596 case IPV6_CHECKSUM:
597 return ip6_raw_ctloutput(op, so, sopt);
598 default:
599 return ip6_ctloutput(op, so, sopt);
600 }
601 out:
602 return error;
603 }
604
605 extern u_long rip6_sendspace;
606 extern u_long rip6_recvspace;
607
608 int
609 rip6_attach(struct socket *so, int proto)
610 {
611 struct in6pcb *in6p;
612 int s, error;
613
614 KASSERT(sotoin6pcb(so) == NULL);
615 sosetlock(so);
616
617 error = kauth_authorize_network(curlwp->l_cred,
618 KAUTH_NETWORK_SOCKET, KAUTH_REQ_NETWORK_SOCKET_RAWSOCK,
619 KAUTH_ARG(AF_INET6),
620 KAUTH_ARG(SOCK_RAW),
621 KAUTH_ARG(so->so_proto->pr_protocol));
622 if (error) {
623 return error;
624 }
625 s = splsoftnet();
626 error = soreserve(so, rip6_sendspace, rip6_recvspace);
627 if (error) {
628 splx(s);
629 return error;
630 }
631 if ((error = in6_pcballoc(so, &raw6cbtable)) != 0) {
632 splx(s);
633 return error;
634 }
635 splx(s);
636 in6p = sotoin6pcb(so);
637 in6p->in6p_ip6.ip6_nxt = proto;
638 in6p->in6p_cksum = -1;
639
640 in6p->in6p_icmp6filt = kmem_alloc(sizeof(struct icmp6_filter), KM_SLEEP);
641 ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
642 KASSERT(solocked(so));
643 return error;
644 }
645
646 static void
647 rip6_detach(struct socket *so)
648 {
649 struct in6pcb *in6p = sotoin6pcb(so);
650
651 KASSERT(solocked(so));
652 KASSERT(in6p != NULL);
653
654 if (so == ip6_mrouter) {
655 ip6_mrouter_done();
656 }
657 /* xxx: RSVP */
658 if (in6p->in6p_icmp6filt != NULL) {
659 kmem_free(in6p->in6p_icmp6filt, sizeof(struct icmp6_filter));
660 in6p->in6p_icmp6filt = NULL;
661 }
662 in6_pcbdetach(in6p);
663 }
664
665 static int
666 rip6_accept(struct socket *so, struct sockaddr *nam)
667 {
668 KASSERT(solocked(so));
669
670 return EOPNOTSUPP;
671 }
672
673 static int
674 rip6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
675 {
676 struct in6pcb *in6p = sotoin6pcb(so);
677 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
678 struct ifaddr *ifa = NULL;
679 int error = 0;
680 int s;
681
682 KASSERT(solocked(so));
683 KASSERT(in6p != NULL);
684 KASSERT(nam != NULL);
685
686 if (addr->sin6_len != sizeof(*addr))
687 return EINVAL;
688 if (IFNET_READER_EMPTY() || addr->sin6_family != AF_INET6)
689 return EADDRNOTAVAIL;
690
691 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
692 return error;
693
694 /*
695 * we don't support mapped address here, it would confuse
696 * users so reject it
697 */
698 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr))
699 return EADDRNOTAVAIL;
700 s = pserialize_read_enter();
701 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
702 (ifa = ifa_ifwithaddr(sin6tosa(addr))) == NULL) {
703 error = EADDRNOTAVAIL;
704 goto out;
705 }
706 if (ifa && (ifatoia6(ifa))->ia6_flags &
707 (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) {
708 error = EADDRNOTAVAIL;
709 goto out;
710 }
711
712 in6p->in6p_laddr = addr->sin6_addr;
713 error = 0;
714 out:
715 pserialize_read_exit(s);
716 return error;
717 }
718
719 static int
720 rip6_listen(struct socket *so, struct lwp *l)
721 {
722 KASSERT(solocked(so));
723
724 return EOPNOTSUPP;
725 }
726
727 static int
728 rip6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
729 {
730 struct in6pcb *in6p = sotoin6pcb(so);
731 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
732 struct in6_addr in6a;
733 struct ifnet *ifp = NULL;
734 int scope_ambiguous = 0;
735 int error = 0;
736 struct psref psref;
737 int bound;
738
739 KASSERT(solocked(so));
740 KASSERT(in6p != NULL);
741 KASSERT(nam != NULL);
742
743 if (IFNET_READER_EMPTY())
744 return EADDRNOTAVAIL;
745 if (addr->sin6_family != AF_INET6)
746 return EAFNOSUPPORT;
747
748 /*
749 * Application should provide a proper zone ID or the use of
750 * default zone IDs should be enabled. Unfortunately, some
751 * applications do not behave as it should, so we need a
752 * workaround. Even if an appropriate ID is not determined,
753 * we'll see if we can determine the outgoing interface. If we
754 * can, determine the zone ID based on the interface below.
755 */
756 if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
757 scope_ambiguous = 1;
758 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
759 return error;
760
761 bound = curlwp_bind();
762 /* Source address selection. XXX: need pcblookup? */
763 error = in6_selectsrc(addr, in6p->in6p_outputopts,
764 in6p->in6p_moptions, &in6p->in6p_route,
765 &in6p->in6p_laddr, &ifp, &psref, &in6a);
766 if (error != 0)
767 goto out;
768 /* XXX: see above */
769 if (ifp && scope_ambiguous &&
770 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
771 goto out;
772 }
773 in6p->in6p_laddr = in6a;
774 in6p->in6p_faddr = addr->sin6_addr;
775 soisconnected(so);
776 out:
777 if_put(ifp, &psref);
778 curlwp_bindx(bound);
779 return error;
780 }
781
782 static int
783 rip6_connect2(struct socket *so, struct socket *so2)
784 {
785 KASSERT(solocked(so));
786
787 return EOPNOTSUPP;
788 }
789
790 static int
791 rip6_disconnect(struct socket *so)
792 {
793 struct in6pcb *in6p = sotoin6pcb(so);
794
795 KASSERT(solocked(so));
796 KASSERT(in6p != NULL);
797
798 if ((so->so_state & SS_ISCONNECTED) == 0)
799 return ENOTCONN;
800
801 in6p->in6p_faddr = in6addr_any;
802 so->so_state &= ~SS_ISCONNECTED; /* XXX */
803 return 0;
804 }
805
806 static int
807 rip6_shutdown(struct socket *so)
808 {
809 KASSERT(solocked(so));
810
811 /*
812 * Mark the connection as being incapable of futther input.
813 */
814 socantsendmore(so);
815 return 0;
816 }
817
818 static int
819 rip6_abort(struct socket *so)
820 {
821 KASSERT(solocked(so));
822
823 soisdisconnected(so);
824 rip6_detach(so);
825 return 0;
826 }
827
828 static int
829 rip6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
830 {
831 return in6_control(so, cmd, nam, ifp);
832 }
833
834 static int
835 rip6_stat(struct socket *so, struct stat *ub)
836 {
837 KASSERT(solocked(so));
838
839 /* stat: don't bother with a blocksize */
840 return 0;
841 }
842
843 static int
844 rip6_peeraddr(struct socket *so, struct sockaddr *nam)
845 {
846 KASSERT(solocked(so));
847 KASSERT(sotoin6pcb(so) != NULL);
848 KASSERT(nam != NULL);
849
850 in6_setpeeraddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
851 return 0;
852 }
853
854 static int
855 rip6_sockaddr(struct socket *so, struct sockaddr *nam)
856 {
857 KASSERT(solocked(so));
858 KASSERT(sotoin6pcb(so) != NULL);
859 KASSERT(nam != NULL);
860
861 in6_setsockaddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
862 return 0;
863 }
864
865 static int
866 rip6_rcvd(struct socket *so, int flags, struct lwp *l)
867 {
868 KASSERT(solocked(so));
869
870 return EOPNOTSUPP;
871 }
872
873 static int
874 rip6_recvoob(struct socket *so, struct mbuf *m, int flags)
875 {
876 KASSERT(solocked(so));
877
878 return EOPNOTSUPP;
879 }
880
881 static int
882 rip6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
883 struct mbuf *control, struct lwp *l)
884 {
885 struct in6pcb *in6p = sotoin6pcb(so);
886 struct sockaddr_in6 tmp;
887 struct sockaddr_in6 *dst;
888 int error = 0;
889
890 KASSERT(solocked(so));
891 KASSERT(in6p != NULL);
892 KASSERT(m != NULL);
893
894 /*
895 * Ship a packet out. The appropriate raw output
896 * routine handles any messaging necessary.
897 */
898
899 /* always copy sockaddr to avoid overwrites */
900 if (so->so_state & SS_ISCONNECTED) {
901 if (nam) {
902 error = EISCONN;
903 goto release;
904 }
905 /* XXX */
906 sockaddr_in6_init(&tmp, &in6p->in6p_faddr, 0, 0, 0);
907 dst = &tmp;
908 } else {
909 if (nam == NULL) {
910 error = ENOTCONN;
911 goto release;
912 }
913 tmp = *(struct sockaddr_in6 *)nam;
914 dst = &tmp;
915
916 if (dst->sin6_family != AF_INET6) {
917 error = EAFNOSUPPORT;
918 goto release;
919 }
920 }
921 error = rip6_output(m, so, dst, control);
922 m = NULL;
923
924 release:
925 if (m)
926 m_freem(m);
927
928 return error;
929 }
930
931 static int
932 rip6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
933 {
934 KASSERT(solocked(so));
935
936 m_freem(m);
937 m_freem(control);
938
939 return EOPNOTSUPP;
940 }
941
942 static int
943 rip6_purgeif(struct socket *so, struct ifnet *ifp)
944 {
945
946 mutex_enter(softnet_lock);
947 in6_pcbpurgeif0(&raw6cbtable, ifp);
948 #ifdef NET_MPSAFE
949 mutex_exit(softnet_lock);
950 #endif
951 in6_purgeif(ifp);
952 #ifdef NET_MPSAFE
953 mutex_enter(softnet_lock);
954 #endif
955 in6_pcbpurgeif(&raw6cbtable, ifp);
956 mutex_exit(softnet_lock);
957
958 return 0;
959 }
960
961 static int
962 sysctl_net_inet6_raw6_stats(SYSCTLFN_ARGS)
963 {
964
965 return (NETSTAT_SYSCTL(rip6stat_percpu, RIP6_NSTATS));
966 }
967
968 static void
969 sysctl_net_inet6_raw6_setup(struct sysctllog **clog)
970 {
971
972 sysctl_createv(clog, 0, NULL, NULL,
973 CTLFLAG_PERMANENT,
974 CTLTYPE_NODE, "inet6", NULL,
975 NULL, 0, NULL, 0,
976 CTL_NET, PF_INET6, CTL_EOL);
977 sysctl_createv(clog, 0, NULL, NULL,
978 CTLFLAG_PERMANENT,
979 CTLTYPE_NODE, "raw6",
980 SYSCTL_DESCR("Raw IPv6 settings"),
981 NULL, 0, NULL, 0,
982 CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL);
983
984 sysctl_createv(clog, 0, NULL, NULL,
985 CTLFLAG_PERMANENT,
986 CTLTYPE_STRUCT, "pcblist",
987 SYSCTL_DESCR("Raw IPv6 control block list"),
988 sysctl_inpcblist, 0, &raw6cbtable, 0,
989 CTL_NET, PF_INET6, IPPROTO_RAW,
990 CTL_CREATE, CTL_EOL);
991 sysctl_createv(clog, 0, NULL, NULL,
992 CTLFLAG_PERMANENT,
993 CTLTYPE_STRUCT, "stats",
994 SYSCTL_DESCR("Raw IPv6 statistics"),
995 sysctl_net_inet6_raw6_stats, 0, NULL, 0,
996 CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS,
997 CTL_EOL);
998 }
999
1000 PR_WRAP_USRREQS(rip6)
1001 #define rip6_attach rip6_attach_wrapper
1002 #define rip6_detach rip6_detach_wrapper
1003 #define rip6_accept rip6_accept_wrapper
1004 #define rip6_bind rip6_bind_wrapper
1005 #define rip6_listen rip6_listen_wrapper
1006 #define rip6_connect rip6_connect_wrapper
1007 #define rip6_connect2 rip6_connect2_wrapper
1008 #define rip6_disconnect rip6_disconnect_wrapper
1009 #define rip6_shutdown rip6_shutdown_wrapper
1010 #define rip6_abort rip6_abort_wrapper
1011 #define rip6_ioctl rip6_ioctl_wrapper
1012 #define rip6_stat rip6_stat_wrapper
1013 #define rip6_peeraddr rip6_peeraddr_wrapper
1014 #define rip6_sockaddr rip6_sockaddr_wrapper
1015 #define rip6_rcvd rip6_rcvd_wrapper
1016 #define rip6_recvoob rip6_recvoob_wrapper
1017 #define rip6_send rip6_send_wrapper
1018 #define rip6_sendoob rip6_sendoob_wrapper
1019 #define rip6_purgeif rip6_purgeif_wrapper
1020
1021 const struct pr_usrreqs rip6_usrreqs = {
1022 .pr_attach = rip6_attach,
1023 .pr_detach = rip6_detach,
1024 .pr_accept = rip6_accept,
1025 .pr_bind = rip6_bind,
1026 .pr_listen = rip6_listen,
1027 .pr_connect = rip6_connect,
1028 .pr_connect2 = rip6_connect2,
1029 .pr_disconnect = rip6_disconnect,
1030 .pr_shutdown = rip6_shutdown,
1031 .pr_abort = rip6_abort,
1032 .pr_ioctl = rip6_ioctl,
1033 .pr_stat = rip6_stat,
1034 .pr_peeraddr = rip6_peeraddr,
1035 .pr_sockaddr = rip6_sockaddr,
1036 .pr_rcvd = rip6_rcvd,
1037 .pr_recvoob = rip6_recvoob,
1038 .pr_send = rip6_send,
1039 .pr_sendoob = rip6_sendoob,
1040 .pr_purgeif = rip6_purgeif,
1041 };
1042