raw_ip6.c revision 1.75.2.3 1 /* $NetBSD: raw_ip6.c,v 1.75.2.3 2006/02/23 16:54:41 rpaulo 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.75.2.3 2006/02/23 16:54:41 rpaulo Exp $");
66
67 #include "opt_inet.h"
68 #include "opt_ipsec.h"
69
70 #include <sys/param.h>
71 #include <sys/sysctl.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/socket.h>
75 #include <sys/protosw.h>
76 #include <sys/socketvar.h>
77 #include <sys/errno.h>
78 #include <sys/systm.h>
79 #include <sys/proc.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/in_pcb.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/ip6_mroute.h>
91 #include <netinet/icmp6.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 #include <machine/stdarg.h>
102
103 #include "faith.h"
104 #if defined(NFAITH) && 0 < NFAITH
105 #include <net/if_faith.h>
106 #endif
107
108 extern struct inpcbtable rawcbtable;
109 struct inpcbtable raw6cbtable;
110 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
111
112 /*
113 * Raw interface to IP6 protocol.
114 */
115
116 struct rip6stat rip6stat;
117
118 /*
119 * Initialize raw connection block queue.
120 */
121 void
122 rip6_init()
123 {
124
125 in_pcbinit(&raw6cbtable, 1, 1);
126 }
127
128 /*
129 * Setup generic address and protocol structures
130 * for raw_input routine, then pass them along with
131 * mbuf chain.
132 */
133 int
134 rip6_input(mp, offp, proto)
135 struct mbuf **mp;
136 int *offp, proto;
137 {
138 struct mbuf *m = *mp;
139 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
140 struct inpcb *inp;
141 struct inpcb *last = NULL;
142 struct sockaddr_in6 rip6src;
143 struct mbuf *opts = NULL;
144
145 rip6stat.rip6s_ipackets++;
146
147 #if defined(NFAITH) && 0 < NFAITH
148 if (faithprefix(&ip6->ip6_dst)) {
149 /* send icmp6 host unreach? */
150 m_freem(m);
151 return IPPROTO_DONE;
152 }
153 #endif
154
155 /* Be proactive about malicious use of IPv4 mapped address */
156 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
157 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
158 /* XXX stat */
159 m_freem(m);
160 return IPPROTO_DONE;
161 }
162
163 bzero(&rip6src, sizeof(rip6src));
164 rip6src.sin6_len = sizeof(struct sockaddr_in6);
165 rip6src.sin6_family = AF_INET6;
166 rip6src.sin6_addr = ip6->ip6_src;
167 if (sa6_recoverscope(&rip6src) != 0) {
168 /* XXX: should be impossible. */
169 m_freem(m);
170 return IPPROTO_DONE;
171 }
172
173 CIRCLEQ_FOREACH(inp, &raw6cbtable.inpt_queue, inp_queue) {
174 if (inp->inp_af != AF_INET6)
175 continue;
176 if (inp->in6p_ip6.ip6_nxt &&
177 inp->in6p_ip6.ip6_nxt != proto)
178 continue;
179 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
180 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst))
181 continue;
182 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
183 !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src))
184 continue;
185 if (inp->in6p_cksum != -1) {
186 rip6stat.rip6s_isum++;
187 if (in6_cksum(m, proto, *offp,
188 m->m_pkthdr.len - *offp)) {
189 rip6stat.rip6s_badsum++;
190 continue;
191 }
192 }
193 if (last) {
194 struct mbuf *n;
195
196 #ifdef IPSEC
197 /*
198 * Check AH/ESP integrity.
199 */
200 if (ipsec6_in_reject(m, last)) {
201 ipsec6stat.in_polvio++;
202 /* do not inject data into pcb */
203 } else
204 #endif /* IPSEC */
205 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
206 if (last->inp_flags & IN6P_CONTROLOPTS)
207 ip6_savecontrol(last, &opts, ip6, n);
208 /* strip intermediate headers */
209 m_adj(n, *offp);
210 if (sbappendaddr(&last->inp_socket->so_rcv,
211 (struct sockaddr *)&rip6src, n, opts) == 0) {
212 /* should notify about lost packet */
213 m_freem(n);
214 if (opts)
215 m_freem(opts);
216 rip6stat.rip6s_fullsock++;
217 } else
218 sorwakeup(last->inp_socket);
219 opts = NULL;
220 }
221 }
222 last = inp;
223 }
224 #ifdef IPSEC
225 /*
226 * Check AH/ESP integrity.
227 */
228 if (last && ipsec6_in_reject(m, last)) {
229 m_freem(m);
230 ipsec6stat.in_polvio++;
231 ip6stat.ip6s_delivered--;
232 /* do not inject data into pcb */
233 } else
234 #endif /* IPSEC */
235 if (last) {
236 if (last->inp_flags & IN6P_CONTROLOPTS)
237 ip6_savecontrol(last, &opts, ip6, m);
238 /* strip intermediate headers */
239 m_adj(m, *offp);
240 if (sbappendaddr(&last->inp_socket->so_rcv,
241 (struct sockaddr *)&rip6src, m, opts) == 0) {
242 m_freem(m);
243 if (opts)
244 m_freem(opts);
245 rip6stat.rip6s_fullsock++;
246 } else
247 sorwakeup(last->inp_socket);
248 } else {
249 rip6stat.rip6s_nosock++;
250 if (m->m_flags & M_MCAST)
251 rip6stat.rip6s_nosockmcast++;
252 if (proto == IPPROTO_NONE)
253 m_freem(m);
254 else {
255 u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
256 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
257 icmp6_error(m, ICMP6_PARAM_PROB,
258 ICMP6_PARAMPROB_NEXTHEADER,
259 prvnxtp - mtod(m, u_int8_t *));
260 }
261 ip6stat.ip6s_delivered--;
262 }
263 return IPPROTO_DONE;
264 }
265
266 void
267 rip6_ctlinput(cmd, sa, d)
268 int cmd;
269 struct sockaddr *sa;
270 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) __P((struct inpcb *, int)) = in6_rtchange;
277 int nxt;
278
279 if (sa->sa_family != AF_INET6 ||
280 sa->sa_len != sizeof(struct sockaddr_in6))
281 return;
282
283 if ((unsigned)cmd >= PRC_NCMDS)
284 return;
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;
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 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
310 int valid = 0;
311 struct inpcb *inp;
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 inp = NULL;
321 inp = in6_pcblookup_connect(&raw6cbtable, &sa6->sin6_addr, 0,
322 (const struct in6_addr *)&sa6_src->sin6_addr, 0, 0);
323 #if 0
324 if (!inp) {
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 inp = in6_pcblookup_bind(&raw6cbtable,
333 &sa6->sin6_addr, 0, 0);
334 }
335 #endif
336
337 if (inp && inp->in6p_ip6.ip6_nxt &&
338 inp->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
353 * MTU change to the userland (2292bis-02), because
354 * some unconnected sockets may share the same
355 * destination and want to know the path MTU.
356 */
357 }
358
359 (void) in6_pcbnotify(&raw6cbtable, sa, 0,
360 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
361 }
362
363 /*
364 * Generate IPv6 header and pass packet to ip6_output.
365 * Tack on options user may have setup with control call.
366 */
367 int
368 #if __STDC__
369 rip6_output(struct mbuf *m, ...)
370 #else
371 rip6_output(m, va_alist)
372 struct mbuf *m;
373 va_dcl
374 #endif
375 {
376 struct socket *so;
377 struct sockaddr_in6 *dstsock;
378 struct mbuf *control;
379 struct in6_addr *dst;
380 struct ip6_hdr *ip6;
381 struct inpcb *inp;
382 u_int plen = m->m_pkthdr.len;
383 int error = 0;
384 struct ip6_pktopts opt, *optp = NULL;
385 struct ifnet *oifp = NULL;
386 int type, code; /* for ICMPv6 output statistics only */
387 int priv = 0;
388 int scope_ambiguous = 0;
389 struct in6_addr *in6a;
390 va_list ap;
391 int flags;
392
393 va_start(ap, m);
394 so = va_arg(ap, struct socket *);
395 dstsock = va_arg(ap, struct sockaddr_in6 *);
396 control = va_arg(ap, struct mbuf *);
397 va_end(ap);
398
399 inp = sotoinpcb(so);
400
401 priv = 0;
402 if (curproc && !suser(curproc->p_ucred, &curproc->p_acflag))
403 priv = 1;
404
405 dst = &dstsock->sin6_addr;
406 if (control) {
407 if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
408 goto bad;
409 optp = &opt;
410 } else
411 optp = inp->in6p_outputopts;
412
413 /*
414 * Check and convert scope zone ID into internal form.
415 * XXX: we may still need to determine the zone later.
416 */
417 if (!(so->so_state & SS_ISCONNECTED)) {
418 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
419 scope_ambiguous = 1;
420 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
421 goto bad;
422 }
423
424 /*
425 * For an ICMPv6 packet, we should know its type and code
426 * to update statistics.
427 */
428 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
429 struct icmp6_hdr *icmp6;
430 if (m->m_len < sizeof(struct icmp6_hdr) &&
431 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
432 error = ENOBUFS;
433 goto bad;
434 }
435 icmp6 = mtod(m, struct icmp6_hdr *);
436 type = icmp6->icmp6_type;
437 code = icmp6->icmp6_code;
438 } else {
439 type = 0;
440 code = 0;
441 }
442
443 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
444 if (!m) {
445 error = ENOBUFS;
446 goto bad;
447 }
448 ip6 = mtod(m, struct ip6_hdr *);
449
450 /*
451 * Next header might not be ICMP6 but use its pseudo header anyway.
452 */
453 ip6->ip6_dst = *dst;
454
455 /*
456 * Source address selection.
457 */
458 if ((in6a = in6_selectsrc(dstsock, optp, inp->in6p_moptions,
459 &inp->in6p_route, &inp->in6p_laddr, &oifp, &error)) == 0) {
460 if (error == 0)
461 error = EADDRNOTAVAIL;
462 goto bad;
463 }
464 ip6->ip6_src = *in6a;
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 = inp->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 = inp->in6p_ip6.ip6_nxt;
487 ip6->ip6_hlim = in6_selecthlim(inp, oifp);
488
489 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
490 inp->in6p_cksum != -1) {
491 int off;
492 u_int16_t sum;
493
494 /* compute checksum */
495 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
496 off = offsetof(struct icmp6_hdr, icmp6_cksum);
497 else
498 off = inp->in6p_cksum;
499 if (plen < off + 1) {
500 error = EINVAL;
501 goto bad;
502 }
503 off += sizeof(struct ip6_hdr);
504
505 sum = 0;
506 m = m_copyback_cow(m, off, sizeof(sum), (caddr_t)&sum,
507 M_DONTWAIT);
508 if (m == NULL) {
509 error = ENOBUFS;
510 goto bad;
511 }
512 sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
513 m = m_copyback_cow(m, off, sizeof(sum), (caddr_t)&sum,
514 M_DONTWAIT);
515 if (m == NULL) {
516 error = ENOBUFS;
517 goto bad;
518 }
519 }
520
521 flags = 0;
522 if (inp->inp_flags & IN6P_MINMTU)
523 flags |= IPV6_MINMTU;
524
525 error = ip6_output(m, optp, &inp->in6p_route, flags,
526 inp->in6p_moptions, so, &oifp);
527 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
528 if (oifp)
529 icmp6_ifoutstat_inc(oifp, type, code);
530 icmp6stat.icp6s_outhist[type]++;
531 } else
532 rip6stat.rip6s_opackets++;
533
534 goto freectl;
535
536 bad:
537 if (m)
538 m_freem(m);
539
540 freectl:
541 if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
542 RTFREE(optp->ip6po_route.ro_rt);
543 if (control)
544 m_freem(control);
545 return (error);
546 }
547
548 /*
549 * Raw IPv6 socket option processing.
550 */
551 int
552 rip6_ctloutput(op, so, level, optname, mp)
553 int op;
554 struct socket *so;
555 int level, optname;
556 struct mbuf **mp;
557 {
558 int error = 0;
559
560 switch (level) {
561 case IPPROTO_IPV6:
562 switch (optname) {
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(optname, so, *mp);
572 if (*mp)
573 (void)m_free(*mp);
574 } else if (op == PRCO_GETOPT)
575 error = ip6_mrouter_get(optname, so, mp);
576 else
577 error = EINVAL;
578 return (error);
579 case IPV6_CHECKSUM:
580 return (ip6_raw_ctloutput(op, so, level, optname, mp));
581 default:
582 return (ip6_ctloutput(op, so, level, optname, mp));
583 }
584
585 case IPPROTO_ICMPV6:
586 /*
587 * XXX: is it better to call icmp6_ctloutput() directly
588 * from protosw?
589 */
590 return (icmp6_ctloutput(op, so, level, optname, mp));
591
592 default:
593 if (op == PRCO_SETOPT && *mp)
594 m_free(*mp);
595 return EINVAL;
596 }
597 }
598
599 extern u_long rip6_sendspace;
600 extern u_long rip6_recvspace;
601
602 int
603 rip6_usrreq(so, req, m, nam, control, l)
604 struct socket *so;
605 int req;
606 struct mbuf *m, *nam, *control;
607 struct lwp *l;
608 {
609 struct inpcb *inp = sotoinpcb(so);
610 struct proc *p;
611 int s;
612 int error = 0;
613 int priv;
614
615 priv = 0;
616 p = l ? l->l_proc : NULL;
617 if (p && !suser(p->p_ucred, &p->p_acflag))
618 priv++;
619
620 if (req == PRU_CONTROL)
621 return (in6_control(so, (u_long)m, (caddr_t)nam,
622 (struct ifnet *)control, p));
623
624 if (req == PRU_PURGEIF) {
625 in6_pcbpurgeif0(&raw6cbtable, (struct ifnet *)control);
626 in6_purgeif((struct ifnet *)control);
627 in6_pcbpurgeif(&raw6cbtable, (struct ifnet *)control);
628 return (0);
629 }
630
631 switch (req) {
632 case PRU_ATTACH:
633 if (inp)
634 panic("rip6_attach");
635 if (!priv) {
636 error = EACCES;
637 break;
638 }
639 s = splsoftnet();
640 if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) {
641 splx(s);
642 break;
643 }
644 if ((error = in_pcballoc(so, &raw6cbtable)) != 0)
645 {
646 splx(s);
647 break;
648 }
649 splx(s);
650 inp = sotoinpcb(so);
651 inp->in6p_ip6.ip6_nxt = (long)nam;
652 inp->in6p_cksum = -1;
653
654 MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
655 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
656 if (inp->in6p_icmp6filt == NULL) {
657 in6_pcbdetach(inp);
658 error = ENOMEM;
659 break;
660 }
661 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
662 break;
663
664 case PRU_DISCONNECT:
665 if ((so->so_state & SS_ISCONNECTED) == 0) {
666 error = ENOTCONN;
667 break;
668 }
669 inp->in6p_faddr = in6addr_any;
670 so->so_state &= ~SS_ISCONNECTED; /* XXX */
671 break;
672
673 case PRU_ABORT:
674 soisdisconnected(so);
675 /* Fallthrough */
676 case PRU_DETACH:
677 if (inp == 0)
678 panic("rip6_detach");
679 if (so == ip6_mrouter)
680 ip6_mrouter_done();
681 /* xxx: RSVP */
682 if (inp->in6p_icmp6filt) {
683 FREE(inp->in6p_icmp6filt, M_PCB);
684 inp->in6p_icmp6filt = NULL;
685 }
686 in6_pcbdetach(inp);
687 break;
688
689 case PRU_BIND:
690 {
691 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
692 struct ifaddr *ia = NULL;
693
694 if (nam->m_len != sizeof(*addr)) {
695 error = EINVAL;
696 break;
697 }
698 if ((ifnet.tqh_first == 0) || (addr->sin6_family != AF_INET6)) {
699 error = EADDRNOTAVAIL;
700 break;
701 }
702 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
703 break;
704
705 /*
706 * we don't support mapped address here, it would confuse
707 * users so reject it
708 */
709 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
710 error = EADDRNOTAVAIL;
711 break;
712 }
713 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
714 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
715 error = EADDRNOTAVAIL;
716 break;
717 }
718 if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
719 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
720 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
721 error = EADDRNOTAVAIL;
722 break;
723 }
724 inp->in6p_laddr = addr->sin6_addr;
725 break;
726 }
727
728 case PRU_CONNECT:
729 {
730 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
731 struct in6_addr *in6a = NULL;
732 struct ifnet *ifp = NULL;
733 int scope_ambiguous = 0;
734
735 if (nam->m_len != sizeof(*addr)) {
736 error = EINVAL;
737 break;
738 }
739 if (ifnet.tqh_first == 0)
740 {
741 error = EADDRNOTAVAIL;
742 break;
743 }
744 if (addr->sin6_family != AF_INET6) {
745 error = EAFNOSUPPORT;
746 break;
747 }
748
749 /*
750 * Application should provide a proper zone ID or the use of
751 * default zone IDs should be enabled. Unfortunately, some
752 * applications do not behave as it should, so we need a
753 * workaround. Even if an appropriate ID is not determined,
754 * we'll see if we can determine the outgoing interface. If we
755 * can, determine the zone ID based on the interface below.
756 */
757 if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
758 scope_ambiguous = 1;
759 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
760 return(error);
761
762 /* Source address selection. XXX: need pcblookup? */
763 in6a = in6_selectsrc(addr, inp->in6p_outputopts,
764 inp->in6p_moptions, &inp->in6p_route,
765 &inp->in6p_laddr, &ifp, &error);
766 if (in6a == NULL) {
767 if (error == 0)
768 error = EADDRNOTAVAIL;
769 break;
770 }
771 /* XXX: see above */
772 if (ifp && scope_ambiguous &&
773 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
774 break;
775 }
776 inp->in6p_laddr = *in6a;
777 inp->in6p_faddr = addr->sin6_addr;
778 soisconnected(so);
779 break;
780 }
781
782 case PRU_CONNECT2:
783 error = EOPNOTSUPP;
784 break;
785
786 /*
787 * Mark the connection as being incapable of futther input.
788 */
789 case PRU_SHUTDOWN:
790 socantsendmore(so);
791 break;
792 /*
793 * Ship a packet out. The appropriate raw output
794 * routine handles any messaging necessary.
795 */
796 case PRU_SEND:
797 {
798 struct sockaddr_in6 tmp;
799 struct sockaddr_in6 *dst;
800
801 /* always copy sockaddr to avoid overwrites */
802 if (so->so_state & SS_ISCONNECTED) {
803 if (nam) {
804 error = EISCONN;
805 break;
806 }
807 /* XXX */
808 bzero(&tmp, sizeof(tmp));
809 tmp.sin6_family = AF_INET6;
810 tmp.sin6_len = sizeof(struct sockaddr_in6);
811 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
812 sizeof(struct in6_addr));
813 dst = &tmp;
814 } else {
815 if (nam == NULL) {
816 error = ENOTCONN;
817 break;
818 }
819 if (nam->m_len != sizeof(tmp)) {
820 error = EINVAL;
821 break;
822 }
823
824 tmp = *mtod(nam, struct sockaddr_in6 *);
825 dst = &tmp;
826
827 if (dst->sin6_family != AF_INET6) {
828 error = EAFNOSUPPORT;
829 break;
830 }
831 }
832 error = rip6_output(m, so, dst, control);
833 m = NULL;
834 break;
835 }
836
837 case PRU_SENSE:
838 /*
839 * stat: don't bother with a blocksize
840 */
841 return (0);
842 /*
843 * Not supported.
844 */
845 case PRU_RCVOOB:
846 case PRU_RCVD:
847 case PRU_LISTEN:
848 case PRU_ACCEPT:
849 case PRU_SENDOOB:
850 error = EOPNOTSUPP;
851 break;
852
853 case PRU_SOCKADDR:
854 in6_setsockaddr(inp, nam);
855 break;
856
857 case PRU_PEERADDR:
858 in6_setpeeraddr(inp, nam);
859 break;
860
861 default:
862 panic("rip6_usrreq");
863 }
864 if (m != NULL)
865 m_freem(m);
866 return (error);
867 }
868
869 SYSCTL_SETUP(sysctl_net_inet6_raw6_setup, "sysctl net.inet6.raw6 subtree setup")
870 {
871
872 sysctl_createv(clog, 0, NULL, NULL,
873 CTLFLAG_PERMANENT,
874 CTLTYPE_NODE, "net", NULL,
875 NULL, 0, NULL, 0,
876 CTL_NET, CTL_EOL);
877 sysctl_createv(clog, 0, NULL, NULL,
878 CTLFLAG_PERMANENT,
879 CTLTYPE_NODE, "inet6", NULL,
880 NULL, 0, NULL, 0,
881 CTL_NET, PF_INET6, CTL_EOL);
882 sysctl_createv(clog, 0, NULL, NULL,
883 CTLFLAG_PERMANENT,
884 CTLTYPE_NODE, "raw6",
885 SYSCTL_DESCR("Raw IPv6 settings"),
886 NULL, 0, NULL, 0,
887 CTL_NET, PF_INET6, IPPROTO_RAW, CTL_EOL);
888
889 sysctl_createv(clog, 0, NULL, NULL,
890 CTLFLAG_PERMANENT,
891 CTLTYPE_STRUCT, "pcblist",
892 SYSCTL_DESCR("Raw IPv6 control block list"),
893 sysctl_inpcblist, 0, &raw6cbtable, 0,
894 CTL_NET, PF_INET6, IPPROTO_RAW,
895 CTL_CREATE, CTL_EOL);
896 sysctl_createv(clog, 0, NULL, NULL,
897 CTLFLAG_PERMANENT,
898 CTLTYPE_STRUCT, "stats",
899 SYSCTL_DESCR("Raw IPv6 statistics"),
900 NULL, 0, &rip6stat, sizeof(rip6stat),
901 CTL_NET, PF_INET6, IPPROTO_RAW, RAW6CTL_STATS,
902 CTL_EOL);
903 }
904