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