raw_ip6.c revision 1.160 1 /* $NetBSD: raw_ip6.c,v 1.160 2018/01/30 14:49:25 maxv 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.160 2018/01/30 14:49:25 maxv 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 if (in6_cksum(m, proto, *offp,
196 m->m_pkthdr.len - *offp)) {
197 RIP6_STATINC(RIP6_STAT_BADSUM);
198 continue;
199 }
200 }
201 if (last) {
202 struct mbuf *n;
203
204 #ifdef IPSEC
205 /*
206 * Check AH/ESP integrity
207 */
208 if (!ipsec_used ||
209 (ipsec_used && !ipsec6_in_reject(m, last)))
210 #endif
211 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
212 if (last->in6p_flags & IN6P_CONTROLOPTS)
213 ip6_savecontrol(last, &opts, ip6, n);
214 /* strip intermediate headers */
215 m_adj(n, *offp);
216 if (sbappendaddr(&last->in6p_socket->so_rcv,
217 sin6tosa(&rip6src), n, opts) == 0) {
218 /* should notify about lost packet */
219 m_freem(n);
220 if (opts)
221 m_freem(opts);
222 RIP6_STATINC(RIP6_STAT_FULLSOCK);
223 } else
224 sorwakeup(last->in6p_socket);
225 opts = NULL;
226 }
227 }
228 last = in6p;
229 }
230
231 #ifdef IPSEC
232 if (ipsec_used && last && ipsec6_in_reject(m, last)) {
233 m_freem(m);
234 IP6_STATDEC(IP6_STAT_DELIVERED);
235 /* do not inject data into pcb */
236 } else
237 #endif
238 if (last) {
239 if (last->in6p_flags & IN6P_CONTROLOPTS)
240 ip6_savecontrol(last, &opts, ip6, m);
241 /* strip intermediate headers */
242 m_adj(m, *offp);
243 if (sbappendaddr(&last->in6p_socket->so_rcv,
244 sin6tosa(&rip6src), m, opts) == 0) {
245 m_freem(m);
246 if (opts)
247 m_freem(opts);
248 RIP6_STATINC(RIP6_STAT_FULLSOCK);
249 } else
250 sorwakeup(last->in6p_socket);
251 } else {
252 RIP6_STATINC(RIP6_STAT_NOSOCK);
253 if (m->m_flags & M_MCAST)
254 RIP6_STATINC(RIP6_STAT_NOSOCKMCAST);
255 if (proto == IPPROTO_NONE)
256 m_freem(m);
257 else {
258 int s;
259 struct ifnet *rcvif = m_get_rcvif(m, &s);
260 const int prvnxt = ip6_get_prevhdr(m, *offp);
261 in6_ifstat_inc(rcvif, ifs6_in_protounknown);
262 m_put_rcvif(rcvif, &s);
263 icmp6_error(m, ICMP6_PARAM_PROB,
264 ICMP6_PARAMPROB_NEXTHEADER,
265 prvnxt);
266 }
267 IP6_STATDEC(IP6_STAT_DELIVERED);
268 }
269 return IPPROTO_DONE;
270 }
271
272 void *
273 rip6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
274 {
275 struct ip6_hdr *ip6;
276 struct ip6ctlparam *ip6cp = NULL;
277 const struct sockaddr_in6 *sa6_src = NULL;
278 void *cmdarg;
279 void (*notify)(struct in6pcb *, int) = in6_rtchange;
280 int nxt;
281
282 if (sa->sa_family != AF_INET6 ||
283 sa->sa_len != sizeof(struct sockaddr_in6))
284 return NULL;
285
286 if ((unsigned)cmd >= PRC_NCMDS)
287 return NULL;
288 if (PRC_IS_REDIRECT(cmd))
289 notify = in6_rtchange, d = NULL;
290 else if (cmd == PRC_HOSTDEAD)
291 d = NULL;
292 else if (cmd == PRC_MSGSIZE)
293 ; /* special code is present, see below */
294 else if (inet6ctlerrmap[cmd] == 0)
295 return NULL;
296
297 /* if the parameter is from icmp6, decode it. */
298 if (d != NULL) {
299 ip6cp = (struct ip6ctlparam *)d;
300 ip6 = ip6cp->ip6c_ip6;
301 cmdarg = ip6cp->ip6c_cmdarg;
302 sa6_src = ip6cp->ip6c_src;
303 nxt = ip6cp->ip6c_nxt;
304 } else {
305 ip6 = NULL;
306 cmdarg = NULL;
307 sa6_src = &sa6_any;
308 nxt = -1;
309 }
310
311 if (ip6 && cmd == PRC_MSGSIZE) {
312 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
313 int valid = 0;
314 struct in6pcb *in6p;
315
316 /*
317 * Check to see if we have a valid raw IPv6 socket
318 * corresponding to the address in the ICMPv6 message
319 * payload, and the protocol (ip6_nxt) meets the socket.
320 * XXX chase extension headers, or pass final nxt value
321 * from icmp6_notify_error()
322 */
323 in6p = NULL;
324 in6p = in6_pcblookup_connect(&raw6cbtable, &sa6->sin6_addr, 0,
325 (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0, 0);
326 #if 0
327 if (!in6p) {
328 /*
329 * As the use of sendto(2) is fairly popular,
330 * we may want to allow non-connected pcb too.
331 * But it could be too weak against attacks...
332 * We should at least check if the local
333 * address (= s) is really ours.
334 */
335 in6p = in6_pcblookup_bind(&raw6cbtable,
336 &sa6->sin6_addr, 0, 0);
337 }
338 #endif
339
340 if (in6p && in6p->in6p_ip6.ip6_nxt &&
341 in6p->in6p_ip6.ip6_nxt == nxt)
342 valid++;
343
344 /*
345 * Depending on the value of "valid" and routing table
346 * size (mtudisc_{hi,lo}wat), we will:
347 * - recalculate the new MTU and create the
348 * corresponding routing entry, or
349 * - ignore the MTU change notification.
350 */
351 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
352
353 /*
354 * regardless of if we called icmp6_mtudisc_update(),
355 * we need to call in6_pcbnotify(), to notify path MTU
356 * change to the userland (RFC3542), because some
357 * unconnected sockets may share the same destination
358 * and want to know the path MTU.
359 */
360 }
361
362 (void) in6_pcbnotify(&raw6cbtable, sa, 0,
363 sin6tocsa(sa6_src), 0, cmd, cmdarg, notify);
364 return NULL;
365 }
366
367 /*
368 * Generate IPv6 header and pass packet to ip6_output.
369 * Tack on options user may have setup with control call.
370 */
371 int
372 rip6_output(struct mbuf *m, struct socket * const so,
373 struct sockaddr_in6 * const dstsock, struct mbuf * const control)
374 {
375 struct in6_addr *dst;
376 struct ip6_hdr *ip6;
377 struct in6pcb *in6p;
378 u_int plen = m->m_pkthdr.len;
379 int error = 0;
380 struct ip6_pktopts opt, *optp = NULL;
381 struct ifnet *oifp = NULL;
382 int type, code; /* for ICMPv6 output statistics only */
383 int scope_ambiguous = 0;
384 int bound = curlwp_bind();
385 struct psref psref;
386
387 in6p = sotoin6pcb(so);
388
389 dst = &dstsock->sin6_addr;
390 if (control) {
391 if ((error = ip6_setpktopts(control, &opt,
392 in6p->in6p_outputopts,
393 kauth_cred_get(), so->so_proto->pr_protocol)) != 0) {
394 goto bad;
395 }
396 optp = &opt;
397 } else
398 optp = in6p->in6p_outputopts;
399
400 /*
401 * Check and convert scope zone ID into internal form.
402 * XXX: we may still need to determine the zone later.
403 */
404 if (!(so->so_state & SS_ISCONNECTED)) {
405 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
406 scope_ambiguous = 1;
407 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
408 goto bad;
409 }
410
411 /*
412 * For an ICMPv6 packet, we should know its type and code
413 * to update statistics.
414 */
415 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
416 struct icmp6_hdr *icmp6;
417 if (m->m_len < sizeof(struct icmp6_hdr) &&
418 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
419 error = ENOBUFS;
420 goto bad;
421 }
422 icmp6 = mtod(m, struct icmp6_hdr *);
423 type = icmp6->icmp6_type;
424 code = icmp6->icmp6_code;
425 } else {
426 type = 0;
427 code = 0;
428 }
429
430 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
431 if (!m) {
432 error = ENOBUFS;
433 goto bad;
434 }
435 ip6 = mtod(m, struct ip6_hdr *);
436
437 /*
438 * Next header might not be ICMP6 but use its pseudo header anyway.
439 */
440 ip6->ip6_dst = *dst;
441
442 /*
443 * Source address selection.
444 */
445 error = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
446 &in6p->in6p_route, &in6p->in6p_laddr, &oifp, &psref, &ip6->ip6_src);
447 if (error != 0)
448 goto bad;
449
450 if (oifp && scope_ambiguous) {
451 /*
452 * Application should provide a proper zone ID or the use of
453 * default zone IDs should be enabled. Unfortunately, some
454 * applications do not behave as it should, so we need a
455 * workaround. Even if an appropriate ID is not determined
456 * (when it's required), if we can determine the outgoing
457 * interface. determine the zone ID based on the interface.
458 */
459 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
460 if (error != 0)
461 goto bad;
462 }
463 ip6->ip6_dst = dstsock->sin6_addr;
464
465 /* fill in the rest of the IPv6 header fields */
466 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
467 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
468 ip6->ip6_vfc |= IPV6_VERSION;
469 /* ip6_plen will be filled in ip6_output, so not fill it here. */
470 ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt;
471 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
472
473 if_put(oifp, &psref);
474 oifp = NULL;
475
476 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
477 in6p->in6p_cksum != -1) {
478 int off;
479 u_int16_t sum;
480
481 /* compute checksum */
482 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
483 off = offsetof(struct icmp6_hdr, icmp6_cksum);
484 else
485 off = in6p->in6p_cksum;
486 if (plen < off + 1) {
487 error = EINVAL;
488 goto bad;
489 }
490 off += sizeof(struct ip6_hdr);
491
492 sum = 0;
493 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
494 M_DONTWAIT);
495 if (m == NULL) {
496 error = ENOBUFS;
497 goto bad;
498 }
499 sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
500 m = m_copyback_cow(m, off, sizeof(sum), (void *)&sum,
501 M_DONTWAIT);
502 if (m == NULL) {
503 error = ENOBUFS;
504 goto bad;
505 }
506 }
507
508 {
509 struct ifnet *ret_oifp = NULL;
510
511 error = ip6_output(m, optp, &in6p->in6p_route, 0,
512 in6p->in6p_moptions, in6p, &ret_oifp);
513 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
514 if (ret_oifp)
515 icmp6_ifoutstat_inc(ret_oifp, type, code);
516 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
517 } else
518 RIP6_STATINC(RIP6_STAT_OPACKETS);
519 }
520
521 goto freectl;
522
523 bad:
524 if (m)
525 m_freem(m);
526
527 freectl:
528 if (control) {
529 ip6_clearpktopts(&opt, -1);
530 m_freem(control);
531 }
532 if_put(oifp, &psref);
533 curlwp_bindx(bound);
534 return error;
535 }
536
537 /*
538 * Raw IPv6 socket option processing.
539 */
540 int
541 rip6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
542 {
543 int error = 0;
544
545 if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER) {
546 int optval;
547
548 /* need to fiddle w/ opt(IPPROTO_IPV6, IPV6_CHECKSUM)? */
549 if (op == PRCO_GETOPT) {
550 optval = 1;
551 error = sockopt_set(sopt, &optval, sizeof(optval));
552 } else if (op == PRCO_SETOPT) {
553 error = sockopt_getint(sopt, &optval);
554 if (error)
555 goto out;
556 if (optval == 0)
557 error = EINVAL;
558 }
559
560 goto out;
561 } else if (sopt->sopt_level != IPPROTO_IPV6)
562 return ip6_ctloutput(op, so, sopt);
563
564 switch (sopt->sopt_name) {
565 case MRT6_INIT:
566 case MRT6_DONE:
567 case MRT6_ADD_MIF:
568 case MRT6_DEL_MIF:
569 case MRT6_ADD_MFC:
570 case MRT6_DEL_MFC:
571 case MRT6_PIM:
572 if (op == PRCO_SETOPT)
573 error = ip6_mrouter_set(so, sopt);
574 else if (op == PRCO_GETOPT)
575 error = ip6_mrouter_get(so, sopt);
576 else
577 error = EINVAL;
578 break;
579 case IPV6_CHECKSUM:
580 return ip6_raw_ctloutput(op, so, sopt);
581 default:
582 return ip6_ctloutput(op, so, sopt);
583 }
584 out:
585 return error;
586 }
587
588 extern u_long rip6_sendspace;
589 extern u_long rip6_recvspace;
590
591 int
592 rip6_attach(struct socket *so, int proto)
593 {
594 struct in6pcb *in6p;
595 int s, error;
596
597 KASSERT(sotoin6pcb(so) == NULL);
598 sosetlock(so);
599
600 error = kauth_authorize_network(curlwp->l_cred,
601 KAUTH_NETWORK_SOCKET, KAUTH_REQ_NETWORK_SOCKET_RAWSOCK,
602 KAUTH_ARG(AF_INET6),
603 KAUTH_ARG(SOCK_RAW),
604 KAUTH_ARG(so->so_proto->pr_protocol));
605 if (error) {
606 return error;
607 }
608 s = splsoftnet();
609 error = soreserve(so, rip6_sendspace, rip6_recvspace);
610 if (error) {
611 splx(s);
612 return error;
613 }
614 if ((error = in6_pcballoc(so, &raw6cbtable)) != 0) {
615 splx(s);
616 return error;
617 }
618 splx(s);
619 in6p = sotoin6pcb(so);
620 in6p->in6p_ip6.ip6_nxt = proto;
621 in6p->in6p_cksum = -1;
622
623 in6p->in6p_icmp6filt = kmem_alloc(sizeof(struct icmp6_filter), KM_SLEEP);
624 ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
625 KASSERT(solocked(so));
626 return error;
627 }
628
629 static void
630 rip6_detach(struct socket *so)
631 {
632 struct in6pcb *in6p = sotoin6pcb(so);
633
634 KASSERT(solocked(so));
635 KASSERT(in6p != NULL);
636
637 if (so == ip6_mrouter) {
638 ip6_mrouter_done();
639 }
640 /* xxx: RSVP */
641 if (in6p->in6p_icmp6filt != NULL) {
642 kmem_free(in6p->in6p_icmp6filt, sizeof(struct icmp6_filter));
643 in6p->in6p_icmp6filt = NULL;
644 }
645 in6_pcbdetach(in6p);
646 }
647
648 static int
649 rip6_accept(struct socket *so, struct sockaddr *nam)
650 {
651 KASSERT(solocked(so));
652
653 return EOPNOTSUPP;
654 }
655
656 static int
657 rip6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
658 {
659 struct in6pcb *in6p = sotoin6pcb(so);
660 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
661 struct ifaddr *ifa = NULL;
662 int error = 0;
663 int s;
664
665 KASSERT(solocked(so));
666 KASSERT(in6p != NULL);
667 KASSERT(nam != NULL);
668
669 if (addr->sin6_len != sizeof(*addr))
670 return EINVAL;
671 if (IFNET_READER_EMPTY() || addr->sin6_family != AF_INET6)
672 return EADDRNOTAVAIL;
673
674 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
675 return error;
676
677 /*
678 * we don't support mapped address here, it would confuse
679 * users so reject it
680 */
681 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr))
682 return EADDRNOTAVAIL;
683 s = pserialize_read_enter();
684 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
685 (ifa = ifa_ifwithaddr(sin6tosa(addr))) == NULL) {
686 error = EADDRNOTAVAIL;
687 goto out;
688 }
689 if (ifa && (ifatoia6(ifa))->ia6_flags &
690 (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED)) {
691 error = EADDRNOTAVAIL;
692 goto out;
693 }
694
695 in6p->in6p_laddr = addr->sin6_addr;
696 error = 0;
697 out:
698 pserialize_read_exit(s);
699 return error;
700 }
701
702 static int
703 rip6_listen(struct socket *so, struct lwp *l)
704 {
705 KASSERT(solocked(so));
706
707 return EOPNOTSUPP;
708 }
709
710 static int
711 rip6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
712 {
713 struct in6pcb *in6p = sotoin6pcb(so);
714 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
715 struct in6_addr in6a;
716 struct ifnet *ifp = NULL;
717 int scope_ambiguous = 0;
718 int error = 0;
719 struct psref psref;
720 int bound;
721
722 KASSERT(solocked(so));
723 KASSERT(in6p != NULL);
724 KASSERT(nam != NULL);
725
726 if (IFNET_READER_EMPTY())
727 return EADDRNOTAVAIL;
728 if (addr->sin6_family != AF_INET6)
729 return EAFNOSUPPORT;
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 }
904 error = rip6_output(m, so, dst, control);
905 m = NULL;
906
907 release:
908 if (m)
909 m_freem(m);
910
911 return error;
912 }
913
914 static int
915 rip6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
916 {
917 KASSERT(solocked(so));
918
919 if (m)
920 m_freem(m);
921
922 return EOPNOTSUPP;
923 }
924
925 static int
926 rip6_purgeif(struct socket *so, struct ifnet *ifp)
927 {
928
929 mutex_enter(softnet_lock);
930 in6_pcbpurgeif0(&raw6cbtable, ifp);
931 #ifdef NET_MPSAFE
932 mutex_exit(softnet_lock);
933 #endif
934 in6_purgeif(ifp);
935 #ifdef NET_MPSAFE
936 mutex_enter(softnet_lock);
937 #endif
938 in6_pcbpurgeif(&raw6cbtable, ifp);
939 mutex_exit(softnet_lock);
940
941 return 0;
942 }
943
944 static int
945 sysctl_net_inet6_raw6_stats(SYSCTLFN_ARGS)
946 {
947
948 return (NETSTAT_SYSCTL(rip6stat_percpu, RIP6_NSTATS));
949 }
950
951 static void
952 sysctl_net_inet6_raw6_setup(struct sysctllog **clog)
953 {
954
955 sysctl_createv(clog, 0, NULL, NULL,
956 CTLFLAG_PERMANENT,
957 CTLTYPE_NODE, "inet6", NULL,
958 NULL, 0, NULL, 0,
959 CTL_NET, PF_INET6, CTL_EOL);
960 sysctl_createv(clog, 0, NULL, NULL,
961 CTLFLAG_PERMANENT,
962 CTLTYPE_NODE, "raw6",
963 SYSCTL_DESCR("Raw IPv6 settings"),
964 NULL, 0, NULL, 0,
965 CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL);
966
967 sysctl_createv(clog, 0, NULL, NULL,
968 CTLFLAG_PERMANENT,
969 CTLTYPE_STRUCT, "pcblist",
970 SYSCTL_DESCR("Raw IPv6 control block list"),
971 sysctl_inpcblist, 0, &raw6cbtable, 0,
972 CTL_NET, PF_INET6, IPPROTO_RAW,
973 CTL_CREATE, CTL_EOL);
974 sysctl_createv(clog, 0, NULL, NULL,
975 CTLFLAG_PERMANENT,
976 CTLTYPE_STRUCT, "stats",
977 SYSCTL_DESCR("Raw IPv6 statistics"),
978 sysctl_net_inet6_raw6_stats, 0, NULL, 0,
979 CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS,
980 CTL_EOL);
981 }
982
983 PR_WRAP_USRREQS(rip6)
984 #define rip6_attach rip6_attach_wrapper
985 #define rip6_detach rip6_detach_wrapper
986 #define rip6_accept rip6_accept_wrapper
987 #define rip6_bind rip6_bind_wrapper
988 #define rip6_listen rip6_listen_wrapper
989 #define rip6_connect rip6_connect_wrapper
990 #define rip6_connect2 rip6_connect2_wrapper
991 #define rip6_disconnect rip6_disconnect_wrapper
992 #define rip6_shutdown rip6_shutdown_wrapper
993 #define rip6_abort rip6_abort_wrapper
994 #define rip6_ioctl rip6_ioctl_wrapper
995 #define rip6_stat rip6_stat_wrapper
996 #define rip6_peeraddr rip6_peeraddr_wrapper
997 #define rip6_sockaddr rip6_sockaddr_wrapper
998 #define rip6_rcvd rip6_rcvd_wrapper
999 #define rip6_recvoob rip6_recvoob_wrapper
1000 #define rip6_send rip6_send_wrapper
1001 #define rip6_sendoob rip6_sendoob_wrapper
1002 #define rip6_purgeif rip6_purgeif_wrapper
1003
1004 const struct pr_usrreqs rip6_usrreqs = {
1005 .pr_attach = rip6_attach,
1006 .pr_detach = rip6_detach,
1007 .pr_accept = rip6_accept,
1008 .pr_bind = rip6_bind,
1009 .pr_listen = rip6_listen,
1010 .pr_connect = rip6_connect,
1011 .pr_connect2 = rip6_connect2,
1012 .pr_disconnect = rip6_disconnect,
1013 .pr_shutdown = rip6_shutdown,
1014 .pr_abort = rip6_abort,
1015 .pr_ioctl = rip6_ioctl,
1016 .pr_stat = rip6_stat,
1017 .pr_peeraddr = rip6_peeraddr,
1018 .pr_sockaddr = rip6_sockaddr,
1019 .pr_rcvd = rip6_rcvd,
1020 .pr_recvoob = rip6_recvoob,
1021 .pr_send = rip6_send,
1022 .pr_sendoob = rip6_sendoob,
1023 .pr_purgeif = rip6_purgeif,
1024 };
1025