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