raw_ip6.c revision 1.42.6.1 1 /* $NetBSD: raw_ip6.c,v 1.42.6.1 2004/06/14 02:48:00 jmc 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. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.42.6.1 2004/06/14 02:48:00 jmc Exp $");
70
71 #include "opt_ipsec.h"
72
73 #include <sys/param.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/protosw.h>
78 #include <sys/socketvar.h>
79 #include <sys/errno.h>
80 #include <sys/systm.h>
81 #include <sys/proc.h>
82
83 #include <net/if.h>
84 #include <net/route.h>
85 #include <net/if_types.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_mroute.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/in6_pcb.h>
94 #include <netinet6/nd6.h>
95 #include <netinet6/ip6protosw.h>
96 #ifdef ENABLE_DEFAULT_SCOPE
97 #include <netinet6/scope6_var.h>
98 #endif
99 #include <netinet6/raw_ip6.h>
100
101 #ifdef IPSEC
102 #include <netinet6/ipsec.h>
103 #endif /* IPSEC */
104
105 #include <machine/stdarg.h>
106
107 #include "faith.h"
108 #if defined(NFAITH) && 0 < NFAITH
109 #include <net/if_faith.h>
110 #endif
111
112 struct in6pcb rawin6pcb;
113 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
114
115 /*
116 * Raw interface to IP6 protocol.
117 */
118
119 struct rip6stat rip6stat;
120
121 /*
122 * Initialize raw connection block queue.
123 */
124 void
125 rip6_init()
126 {
127 rawin6pcb.in6p_next = rawin6pcb.in6p_prev = &rawin6pcb;
128 }
129
130 /*
131 * Setup generic address and protocol structures
132 * for raw_input routine, then pass them along with
133 * mbuf chain.
134 */
135 int
136 rip6_input(mp, offp, proto)
137 struct mbuf **mp;
138 int *offp, proto;
139 {
140 struct mbuf *m = *mp;
141 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
142 struct in6pcb *in6p;
143 struct in6pcb *last = NULL;
144 struct sockaddr_in6 rip6src;
145 struct mbuf *opts = NULL;
146
147 rip6stat.rip6s_ipackets++;
148
149 #if defined(NFAITH) && 0 < NFAITH
150 if (faithprefix(&ip6->ip6_dst)) {
151 /* send icmp6 host unreach? */
152 m_freem(m);
153 return IPPROTO_DONE;
154 }
155 #endif
156
157 /* Be proactive about malicious use of IPv4 mapped address */
158 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
159 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
160 /* XXX stat */
161 m_freem(m);
162 return IPPROTO_DONE;
163 }
164
165 bzero(&rip6src, sizeof(rip6src));
166 rip6src.sin6_len = sizeof(struct sockaddr_in6);
167 rip6src.sin6_family = AF_INET6;
168 #if 0 /* XXX inbound flowlabel */
169 rip6src.sin6_flowinfo = ip6->ip6_flow & IPV6_FLOWINFO_MASK;
170 #endif
171 /* KAME hack: recover scopeid */
172 (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
173
174 for (in6p = rawin6pcb.in6p_next;
175 in6p != &rawin6pcb; in6p = in6p->in6p_next)
176 {
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, ip6->ip6_nxt, *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 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
207 if (last->in6p_flags & IN6P_CONTROLOPTS)
208 ip6_savecontrol(last, &opts, ip6, n);
209 /* strip intermediate headers */
210 m_adj(n, *offp);
211 if (sbappendaddr(&last->in6p_socket->so_rcv,
212 (struct sockaddr *)&rip6src,
213 n, opts) == 0) {
214 /* should notify about lost packet */
215 m_freem(n);
216 if (opts)
217 m_freem(opts);
218 rip6stat.rip6s_fullsock++;
219 } else
220 sorwakeup(last->in6p_socket);
221 opts = NULL;
222 }
223 }
224 last = in6p;
225 }
226 #ifdef IPSEC
227 /*
228 * Check AH/ESP integrity.
229 */
230 if (last && ipsec6_in_reject(m, last)) {
231 m_freem(m);
232 ipsec6stat.in_polvio++;
233 ip6stat.ip6s_delivered--;
234 /* do not inject data into pcb */
235 } else
236 #endif /* IPSEC */
237 if (last) {
238 if (last->in6p_flags & IN6P_CONTROLOPTS)
239 ip6_savecontrol(last, &opts, ip6, m);
240 /* strip intermediate headers */
241 m_adj(m, *offp);
242 if (sbappendaddr(&last->in6p_socket->so_rcv,
243 (struct sockaddr *)&rip6src, m, opts) == 0) {
244 m_freem(m);
245 if (opts)
246 m_freem(opts);
247 rip6stat.rip6s_fullsock++;
248 } else
249 sorwakeup(last->in6p_socket);
250 } else {
251 rip6stat.rip6s_nosock++;
252 if (m->m_flags & M_MCAST)
253 rip6stat.rip6s_nosockmcast++;
254 if (proto == IPPROTO_NONE)
255 m_freem(m);
256 else {
257 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
258 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
259 icmp6_error(m, ICMP6_PARAM_PROB,
260 ICMP6_PARAMPROB_NEXTHEADER,
261 prvnxtp - mtod(m, char *));
262 }
263 ip6stat.ip6s_delivered--;
264 }
265 return IPPROTO_DONE;
266 }
267
268 void
269 rip6_ctlinput(cmd, sa, d)
270 int cmd;
271 struct sockaddr *sa;
272 void *d;
273 {
274 struct ip6_hdr *ip6;
275 struct mbuf *m;
276 int off;
277 struct ip6ctlparam *ip6cp = NULL;
278 const struct sockaddr_in6 *sa6_src = NULL;
279 void *cmdarg;
280 void (*notify) __P((struct in6pcb *, int)) = in6_rtchange;
281 int nxt;
282
283 if (sa->sa_family != AF_INET6 ||
284 sa->sa_len != sizeof(struct sockaddr_in6))
285 return;
286
287 if ((unsigned)cmd >= PRC_NCMDS)
288 return;
289 if (PRC_IS_REDIRECT(cmd))
290 notify = in6_rtchange, d = NULL;
291 else if (cmd == PRC_HOSTDEAD)
292 d = NULL;
293 else if (cmd == PRC_MSGSIZE)
294 ; /* special code is present, see below */
295 else if (inet6ctlerrmap[cmd] == 0)
296 return;
297
298 /* if the parameter is from icmp6, decode it. */
299 if (d != NULL) {
300 ip6cp = (struct ip6ctlparam *)d;
301 m = ip6cp->ip6c_m;
302 ip6 = ip6cp->ip6c_ip6;
303 off = ip6cp->ip6c_off;
304 cmdarg = ip6cp->ip6c_cmdarg;
305 sa6_src = ip6cp->ip6c_src;
306 nxt = ip6cp->ip6c_nxt;
307 } else {
308 m = NULL;
309 ip6 = NULL;
310 cmdarg = NULL;
311 sa6_src = &sa6_any;
312 nxt = -1;
313 }
314
315 if (ip6 && cmd == PRC_MSGSIZE) {
316 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
317 int valid = 0;
318 struct in6pcb *in6p;
319
320 /*
321 * Check to see if we have a valid raw IPv6 socket
322 * corresponding to the address in the ICMPv6 message
323 * payload, and the protocol (ip6_nxt) meets the socket.
324 * XXX chase extension headers, or pass final nxt value
325 * from icmp6_notify_error()
326 */
327 in6p = NULL;
328 in6p = in6_pcblookup_connect(&rawin6pcb,
329 &sa6->sin6_addr, 0,
330 (struct in6_addr *)&sa6_src->sin6_addr, 0, 0);
331 #if 0
332 if (!in6p) {
333 /*
334 * As the use of sendto(2) is fairly popular,
335 * we may want to allow non-connected pcb too.
336 * But it could be too weak against attacks...
337 * We should at least check if the local
338 * address (= s) is really ours.
339 */
340 in6p = in6_pcblookup_bind(&rawin6pcb,
341 &sa6->sin6_addr, 0, 0))
342 }
343 #endif
344
345 if (in6p && in6p->in6p_ip6.ip6_nxt &&
346 in6p->in6p_ip6.ip6_nxt == nxt)
347 valid++;
348
349 /*
350 * Depending on the value of "valid" and routing table
351 * size (mtudisc_{hi,lo}wat), we will:
352 * - recalcurate the new MTU and create the
353 * corresponding routing entry, or
354 * - ignore the MTU change notification.
355 */
356 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
357
358 /*
359 * regardless of if we called icmp6_mtudisc_update(),
360 * we need to call in6_pcbnotify(), to notify path
361 * MTU change to the userland (2292bis-02), because
362 * some unconnected sockets may share the same
363 * destination and want to know the path MTU.
364 */
365 }
366
367 (void) in6_pcbnotify(&rawin6pcb, sa, 0,
368 (struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
369 }
370
371 /*
372 * Generate IPv6 header and pass packet to ip6_output.
373 * Tack on options user may have setup with control call.
374 */
375 int
376 #if __STDC__
377 rip6_output(struct mbuf *m, ...)
378 #else
379 rip6_output(m, va_alist)
380 struct mbuf *m;
381 va_dcl
382 #endif
383 {
384 struct socket *so;
385 struct sockaddr_in6 *dstsock;
386 struct mbuf *control;
387 struct in6_addr *dst;
388 struct ip6_hdr *ip6;
389 struct in6pcb *in6p;
390 u_int plen = m->m_pkthdr.len;
391 int error = 0;
392 struct ip6_pktopts opt, *optp = NULL, *origoptp;
393 struct ifnet *oifp = NULL;
394 int type, code; /* for ICMPv6 output statistics only */
395 int priv = 0;
396 va_list ap;
397 int flags;
398
399 va_start(ap, m);
400 so = va_arg(ap, struct socket *);
401 dstsock = va_arg(ap, struct sockaddr_in6 *);
402 control = va_arg(ap, struct mbuf *);
403 va_end(ap);
404
405 in6p = sotoin6pcb(so);
406
407 priv = 0;
408 {
409 struct proc *p = curproc; /* XXX */
410
411 if (p && !suser(p->p_ucred, &p->p_acflag))
412 priv = 1;
413 }
414 dst = &dstsock->sin6_addr;
415 if (control) {
416 if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
417 goto bad;
418 optp = &opt;
419 } else
420 optp = in6p->in6p_outputopts;
421
422 /*
423 * For an ICMPv6 packet, we should know its type and code
424 * to update statistics.
425 */
426 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
427 struct icmp6_hdr *icmp6;
428 if (m->m_len < sizeof(struct icmp6_hdr) &&
429 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
430 error = ENOBUFS;
431 goto bad;
432 }
433 icmp6 = mtod(m, struct icmp6_hdr *);
434 type = icmp6->icmp6_type;
435 code = icmp6->icmp6_code;
436 }
437
438 M_PREPEND(m, sizeof(*ip6), M_WAIT);
439 ip6 = mtod(m, struct ip6_hdr *);
440
441 /*
442 * Next header might not be ICMP6 but use its pseudo header anyway.
443 */
444 ip6->ip6_dst = *dst;
445
446 /* KAME hack: embed scopeid */
447 origoptp = in6p->in6p_outputopts;
448 in6p->in6p_outputopts = optp;
449 if (in6_embedscope(&ip6->ip6_dst, dstsock, in6p, &oifp) != 0) {
450 error = EINVAL;
451 goto bad;
452 }
453 in6p->in6p_outputopts = origoptp;
454
455 /*
456 * Source address selection.
457 */
458 {
459 struct in6_addr *in6a;
460
461 if ((in6a = in6_selectsrc(dstsock, optp,
462 in6p->in6p_moptions,
463 &in6p->in6p_route,
464 &in6p->in6p_laddr,
465 &error)) == 0) {
466 if (error == 0)
467 error = EADDRNOTAVAIL;
468 goto bad;
469 }
470 ip6->ip6_src = *in6a;
471 if (in6p->in6p_route.ro_rt) {
472 /* what if oifp contradicts ? */
473 oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
474 }
475 }
476
477 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
478 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
479 ip6->ip6_vfc |= IPV6_VERSION;
480 #if 0 /* ip6_plen will be filled in ip6_output. */
481 ip6->ip6_plen = htons((u_short)plen);
482 #endif
483 ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt;
484 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
485
486 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
487 in6p->in6p_cksum != -1) {
488 int off;
489 u_int16_t sum;
490
491 #define offsetof(type, member) ((size_t)(&((type *)0)->member)) /* XXX */
492
493 /* compute checksum */
494 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
495 off = offsetof(struct icmp6_hdr, icmp6_cksum);
496 else
497 off = in6p->in6p_cksum;
498 if (plen < off + 1) {
499 error = EINVAL;
500 goto bad;
501 }
502 off += sizeof(struct ip6_hdr);
503
504 sum = 0;
505 m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
506 sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
507 m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
508 }
509
510 #ifdef IPSEC
511 if (ipsec_setsocket(m, so) != 0) {
512 error = ENOBUFS;
513 goto bad;
514 }
515 #endif /* IPSEC */
516
517 flags = 0;
518 if (in6p->in6p_flags & IN6P_MINMTU)
519 flags |= IPV6_MINMTU;
520
521 error = ip6_output(m, optp, &in6p->in6p_route, flags,
522 in6p->in6p_moptions, &oifp);
523 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
524 if (oifp)
525 icmp6_ifoutstat_inc(oifp, type, code);
526 icmp6stat.icp6s_outhist[type]++;
527 } else
528 rip6stat.rip6s_opackets++;
529
530 goto freectl;
531
532 bad:
533 if (m)
534 m_freem(m);
535
536 freectl:
537 if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
538 RTFREE(optp->ip6po_route.ro_rt);
539 if (control)
540 m_freem(control);
541 return(error);
542 }
543
544 /*
545 * Raw IPv6 socket option processing.
546 */
547 int
548 rip6_ctloutput(op, so, level, optname, mp)
549 int op;
550 struct socket *so;
551 int level, optname;
552 struct mbuf **mp;
553 {
554 int error = 0;
555 int optval;
556 struct in6pcb *in6p;
557 struct mbuf *m;
558 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
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 /*
581 * for ICMPv6 sockets, no modification allowed for
582 * checksum offset, permit "no change" values to
583 * help existing apps.
584 */
585 in6p = sotoin6pcb(so);
586 error = 0;
587 if (op == PRCO_SETOPT) {
588 m = *mp;
589 if (m && m->m_len == sizeof(int)) {
590 optval = *mtod(m, int *);
591 if (so->so_proto->pr_protocol ==
592 IPPROTO_ICMPV6) {
593 if (optval != icmp6off)
594 error = EINVAL;
595 } else
596 in6p->in6p_cksum = optval;
597 } else
598 error = EINVAL;
599 if (m)
600 m_free(m);
601 } else if (op == PRCO_GETOPT) {
602 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
603 optval = icmp6off;
604 else
605 optval = in6p->in6p_cksum;
606 *mp = m = m_get(M_WAIT, MT_SOOPTS);
607 m->m_len = sizeof(int);
608 *mtod(m, int *) = optval;
609 } else
610 error = EINVAL;
611 return error;
612 default:
613 return (ip6_ctloutput(op, so, level, optname, mp));
614 }
615
616 case IPPROTO_ICMPV6:
617 /*
618 * XXX: is it better to call icmp6_ctloutput() directly
619 * from protosw?
620 */
621 return(icmp6_ctloutput(op, so, level, optname, mp));
622
623 default:
624 if (op == PRCO_SETOPT && *mp)
625 m_free(*mp);
626 return EINVAL;
627 }
628 }
629
630 extern u_long rip6_sendspace;
631 extern u_long rip6_recvspace;
632
633 int
634 rip6_usrreq(so, req, m, nam, control, p)
635 struct socket *so;
636 int req;
637 struct mbuf *m, *nam, *control;
638 struct proc *p;
639 {
640 struct in6pcb *in6p = sotoin6pcb(so);
641 int s;
642 int error = 0;
643 /* extern struct socket *ip6_mrouter; */ /* xxx */
644 int priv;
645
646 priv = 0;
647 if (p && !suser(p->p_ucred, &p->p_acflag))
648 priv++;
649
650 if (req == PRU_CONTROL)
651 return (in6_control(so, (u_long)m, (caddr_t)nam,
652 (struct ifnet *)control, p));
653
654 if (req == PRU_PURGEIF) {
655 in6_pcbpurgeif0(&rawin6pcb, (struct ifnet *)control);
656 in6_purgeif((struct ifnet *)control);
657 in6_pcbpurgeif(&rawin6pcb, (struct ifnet *)control);
658 return (0);
659 }
660
661 switch (req) {
662 case PRU_ATTACH:
663 if (in6p)
664 panic("rip6_attach");
665 if (!priv) {
666 error = EACCES;
667 break;
668 }
669 s = splsoftnet();
670 if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) {
671 splx(s);
672 break;
673 }
674 if ((error = in6_pcballoc(so, &rawin6pcb)) != 0)
675 {
676 splx(s);
677 break;
678 }
679 splx(s);
680 in6p = sotoin6pcb(so);
681 in6p->in6p_ip6.ip6_nxt = (long)nam;
682 in6p->in6p_cksum = -1;
683
684 MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
685 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
686 if (in6p->in6p_icmp6filt == NULL) {
687 in6_pcbdetach(in6p);
688 error = ENOMEM;
689 break;
690 }
691 ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
692 break;
693
694 case PRU_DISCONNECT:
695 if ((so->so_state & SS_ISCONNECTED) == 0) {
696 error = ENOTCONN;
697 break;
698 }
699 in6p->in6p_faddr = in6addr_any;
700 so->so_state &= ~SS_ISCONNECTED; /* XXX */
701 break;
702
703 case PRU_ABORT:
704 soisdisconnected(so);
705 /* Fallthrough */
706 case PRU_DETACH:
707 if (in6p == 0)
708 panic("rip6_detach");
709 if (so == ip6_mrouter)
710 ip6_mrouter_done();
711 /* xxx: RSVP */
712 if (in6p->in6p_icmp6filt) {
713 FREE(in6p->in6p_icmp6filt, M_PCB);
714 in6p->in6p_icmp6filt = NULL;
715 }
716 in6_pcbdetach(in6p);
717 break;
718
719 case PRU_BIND:
720 {
721 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
722 struct ifaddr *ia = NULL;
723
724 if (nam->m_len != sizeof(*addr)) {
725 error = EINVAL;
726 break;
727 }
728 if ((ifnet.tqh_first == 0) || (addr->sin6_family != AF_INET6)) {
729 error = EADDRNOTAVAIL;
730 break;
731 }
732 #ifdef ENABLE_DEFAULT_SCOPE
733 if (addr->sin6_scope_id == 0) /* not change if specified */
734 addr->sin6_scope_id =
735 scope6_addr2default(&addr->sin6_addr);
736 #endif
737 /* KAME hack: embed scopeid */
738 if (in6_embedscope(&addr->sin6_addr, addr, in6p, NULL) != 0)
739 return EINVAL;
740 #ifndef SCOPEDROUTING
741 addr->sin6_scope_id = 0; /* for ifa_ifwithaddr */
742 #endif
743
744 /*
745 * we don't support mapped address here, it would confuse
746 * users so reject it
747 */
748 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
749 error = EADDRNOTAVAIL;
750 break;
751 }
752 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
753 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
754 error = EADDRNOTAVAIL;
755 break;
756 }
757 if (ia &&
758 ((struct in6_ifaddr *)ia)->ia6_flags &
759 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
760 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
761 error = EADDRNOTAVAIL;
762 break;
763 }
764 in6p->in6p_laddr = addr->sin6_addr;
765 break;
766 }
767
768 case PRU_CONNECT:
769 {
770 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
771 struct in6_addr *in6a = NULL;
772 #ifdef ENABLE_DEFAULT_SCOPE
773 struct sockaddr_in6 sin6;
774 #endif
775
776 if (nam->m_len != sizeof(*addr)) {
777 error = EINVAL;
778 break;
779 }
780 if (ifnet.tqh_first == 0)
781 {
782 error = EADDRNOTAVAIL;
783 break;
784 }
785 if (addr->sin6_family != AF_INET6) {
786 error = EAFNOSUPPORT;
787 break;
788 }
789
790 #ifdef ENABLE_DEFAULT_SCOPE
791 if (addr->sin6_scope_id == 0) {
792 /* protect *addr */
793 sin6 = *addr;
794 addr = &sin6;
795 addr->sin6_scope_id =
796 scope6_addr2default(&addr->sin6_addr);
797 }
798 #endif
799
800 /* Source address selection. XXX: need pcblookup? */
801 in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
802 in6p->in6p_moptions,
803 &in6p->in6p_route,
804 &in6p->in6p_laddr,
805 &error);
806 if (in6a == NULL) {
807 if (error == 0)
808 error = EADDRNOTAVAIL;
809 break;
810 }
811 in6p->in6p_laddr = *in6a;
812 in6p->in6p_faddr = addr->sin6_addr;
813 soisconnected(so);
814 break;
815 }
816
817 case PRU_CONNECT2:
818 error = EOPNOTSUPP;
819 break;
820
821 /*
822 * Mark the connection as being incapable of futther input.
823 */
824 case PRU_SHUTDOWN:
825 socantsendmore(so);
826 break;
827 /*
828 * Ship a packet out. The appropriate raw output
829 * routine handles any messaging necessary.
830 */
831 case PRU_SEND:
832 {
833 struct sockaddr_in6 tmp;
834 struct sockaddr_in6 *dst;
835
836 /* always copy sockaddr to avoid overwrites */
837 if (so->so_state & SS_ISCONNECTED) {
838 if (nam) {
839 error = EISCONN;
840 break;
841 }
842 /* XXX */
843 bzero(&tmp, sizeof(tmp));
844 tmp.sin6_family = AF_INET6;
845 tmp.sin6_len = sizeof(struct sockaddr_in6);
846 bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
847 sizeof(struct in6_addr));
848 dst = &tmp;
849 } else {
850 if (nam == NULL) {
851 error = ENOTCONN;
852 break;
853 }
854 if (nam->m_len != sizeof(tmp)) {
855 error = EINVAL;
856 break;
857 }
858
859 tmp = *mtod(nam, struct sockaddr_in6 *);
860 dst = &tmp;
861
862 if (dst->sin6_family != AF_INET6) {
863 error = EAFNOSUPPORT;
864 break;
865 }
866 }
867 #ifdef ENABLE_DEFAULT_SCOPE
868 if (dst->sin6_scope_id == 0) {
869 dst->sin6_scope_id =
870 scope6_addr2default(&dst->sin6_addr);
871 }
872 #endif
873 error = rip6_output(m, so, dst, control);
874 m = NULL;
875 break;
876 }
877
878 case PRU_SENSE:
879 /*
880 * stat: don't bother with a blocksize
881 */
882 return(0);
883 /*
884 * Not supported.
885 */
886 case PRU_RCVOOB:
887 case PRU_RCVD:
888 case PRU_LISTEN:
889 case PRU_ACCEPT:
890 case PRU_SENDOOB:
891 error = EOPNOTSUPP;
892 break;
893
894 case PRU_SOCKADDR:
895 in6_setsockaddr(in6p, nam);
896 break;
897
898 case PRU_PEERADDR:
899 in6_setpeeraddr(in6p, nam);
900 break;
901
902 default:
903 panic("rip6_usrreq");
904 }
905 if (m != NULL)
906 m_freem(m);
907 return(error);
908 }
909