Annotation of src/sys/netinet6/raw_ip6.c, Revision 1.43
1.43 ! itojun 1: /* $NetBSD: raw_ip6.c,v 1.42 2002/03/19 01:21:19 itojun Exp $ */
1.34 itojun 2: /* $KAME: raw_ip6.c,v 1.82 2001/07/23 18:57:56 jinmei Exp $ */
1.3 thorpej 3:
1.2 itojun 4: /*
5: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6: * All rights reserved.
1.24 itojun 7: *
1.2 itojun 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.
1.24 itojun 19: *
1.2 itojun 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: */
1.39 lukem 67:
68: #include <sys/cdefs.h>
1.43 ! itojun 69: __KERNEL_RCSID(0, "$NetBSD: raw_ip6.c,v 1.42 2002/03/19 01:21:19 itojun Exp $");
1.6 thorpej 70:
71: #include "opt_ipsec.h"
1.2 itojun 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>
1.19 itojun 89: #include <netinet/ip6.h>
1.2 itojun 90: #include <netinet6/ip6_var.h>
91: #include <netinet6/ip6_mroute.h>
1.19 itojun 92: #include <netinet/icmp6.h>
1.2 itojun 93: #include <netinet6/in6_pcb.h>
94: #include <netinet6/nd6.h>
1.20 itojun 95: #include <netinet6/ip6protosw.h>
1.24 itojun 96: #ifdef ENABLE_DEFAULT_SCOPE
97: #include <netinet6/scope6_var.h>
98: #endif
1.37 itojun 99: #include <netinet6/raw_ip6.h>
1.2 itojun 100:
101: #ifdef IPSEC
102: #include <netinet6/ipsec.h>
1.38 itojun 103: #endif /* IPSEC */
1.2 itojun 104:
105: #include <machine/stdarg.h>
106:
107: #include "faith.h"
1.32 itojun 108: #if defined(NFAITH) && 0 < NFAITH
109: #include <net/if_faith.h>
110: #endif
1.2 itojun 111:
112: struct in6pcb rawin6pcb;
113: #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
114:
115: /*
116: * Raw interface to IP6 protocol.
117: */
118:
1.37 itojun 119: struct rip6stat rip6stat;
120:
1.2 itojun 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;
1.27 itojun 141: struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
142: struct in6pcb *in6p;
1.2 itojun 143: struct in6pcb *last = NULL;
144: struct sockaddr_in6 rip6src;
145: struct mbuf *opts = NULL;
146:
1.37 itojun 147: rip6stat.rip6s_ipackets++;
148:
1.2 itojun 149: #if defined(NFAITH) && 0 < NFAITH
1.32 itojun 150: if (faithprefix(&ip6->ip6_dst)) {
151: /* send icmp6 host unreach? */
152: m_freem(m);
153: return IPPROTO_DONE;
1.2 itojun 154: }
155: #endif
1.14 itojun 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:
1.2 itojun 165: bzero(&rip6src, sizeof(rip6src));
166: rip6src.sin6_len = sizeof(struct sockaddr_in6);
167: rip6src.sin6_family = AF_INET6;
1.36 itojun 168: #if 0 /* XXX inbound flowlabel */
1.24 itojun 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);
1.2 itojun 173:
174: for (in6p = rawin6pcb.in6p_next;
1.28 itojun 175: in6p != &rawin6pcb; in6p = in6p->in6p_next)
176: {
1.2 itojun 177: if (in6p->in6p_ip6.ip6_nxt &&
178: in6p->in6p_ip6.ip6_nxt != proto)
179: continue;
1.12 itojun 180: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1.37 itojun 181: !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1.2 itojun 182: continue;
1.12 itojun 183: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1.37 itojun 184: !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1.2 itojun 185: continue;
1.37 itojun 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: }
1.2 itojun 193: }
194: if (last) {
195: struct mbuf *n;
1.30 itojun 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
1.38 itojun 205: #endif /* IPSEC */
1.2 itojun 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,
1.43 ! itojun 212: (struct sockaddr *)&rip6src, n, opts) == 0) {
1.2 itojun 213: /* should notify about lost packet */
214: m_freem(n);
215: if (opts)
216: m_freem(opts);
1.37 itojun 217: rip6stat.rip6s_fullsock++;
1.2 itojun 218: } else
219: sorwakeup(last->in6p_socket);
220: opts = NULL;
221: }
222: }
223: last = in6p;
224: }
1.30 itojun 225: #ifdef IPSEC
226: /*
227: * Check AH/ESP integrity.
228: */
229: if (last && ipsec6_in_reject(m, last)) {
230: m_freem(m);
231: ipsec6stat.in_polvio++;
232: ip6stat.ip6s_delivered--;
233: /* do not inject data into pcb */
234: } else
1.38 itojun 235: #endif /* IPSEC */
1.2 itojun 236: if (last) {
237: if (last->in6p_flags & IN6P_CONTROLOPTS)
238: ip6_savecontrol(last, &opts, ip6, m);
239: /* strip intermediate headers */
240: m_adj(m, *offp);
241: if (sbappendaddr(&last->in6p_socket->so_rcv,
1.43 ! itojun 242: (struct sockaddr *)&rip6src, m, opts) == 0) {
1.2 itojun 243: m_freem(m);
244: if (opts)
245: m_freem(opts);
1.37 itojun 246: rip6stat.rip6s_fullsock++;
1.2 itojun 247: } else
248: sorwakeup(last->in6p_socket);
249: } else {
1.37 itojun 250: rip6stat.rip6s_nosock++;
251: if (m->m_flags & M_MCAST)
252: rip6stat.rip6s_nosockmcast++;
1.2 itojun 253: if (proto == IPPROTO_NONE)
254: m_freem(m);
255: else {
256: char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
1.27 itojun 257: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
1.2 itojun 258: icmp6_error(m, ICMP6_PARAM_PROB,
1.43 ! itojun 259: ICMP6_PARAMPROB_NEXTHEADER,
! 260: prvnxtp - mtod(m, char *));
1.2 itojun 261: }
262: ip6stat.ip6s_delivered--;
263: }
264: return IPPROTO_DONE;
265: }
266:
1.20 itojun 267: void
268: rip6_ctlinput(cmd, sa, d)
269: int cmd;
270: struct sockaddr *sa;
271: void *d;
272: {
1.27 itojun 273: struct ip6_hdr *ip6;
1.20 itojun 274: struct mbuf *m;
275: int off;
1.29 itojun 276: struct ip6ctlparam *ip6cp = NULL;
277: const struct sockaddr_in6 *sa6_src = NULL;
278: void *cmdarg;
1.21 itojun 279: void (*notify) __P((struct in6pcb *, int)) = in6_rtchange;
1.29 itojun 280: int nxt;
1.20 itojun 281:
282: if (sa->sa_family != AF_INET6 ||
283: sa->sa_len != sizeof(struct sockaddr_in6))
284: return;
285:
1.21 itojun 286: if ((unsigned)cmd >= PRC_NCMDS)
287: return;
288: if (PRC_IS_REDIRECT(cmd))
289: notify = in6_rtchange, d = NULL;
290: else if (cmd == PRC_HOSTDEAD)
291: d = NULL;
1.29 itojun 292: else if (cmd == PRC_MSGSIZE)
293: ; /* special code is present, see below */
1.21 itojun 294: else if (inet6ctlerrmap[cmd] == 0)
1.20 itojun 295: return;
296:
297: /* if the parameter is from icmp6, decode it. */
298: if (d != NULL) {
1.29 itojun 299: ip6cp = (struct ip6ctlparam *)d;
1.20 itojun 300: m = ip6cp->ip6c_m;
301: ip6 = ip6cp->ip6c_ip6;
302: off = ip6cp->ip6c_off;
1.29 itojun 303: cmdarg = ip6cp->ip6c_cmdarg;
304: sa6_src = ip6cp->ip6c_src;
305: nxt = ip6cp->ip6c_nxt;
1.20 itojun 306: } else {
307: m = NULL;
308: ip6 = NULL;
1.29 itojun 309: cmdarg = NULL;
310: sa6_src = &sa6_any;
311: nxt = -1;
1.20 itojun 312: }
313:
1.29 itojun 314: if (ip6 && cmd == PRC_MSGSIZE) {
315: struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
316: int valid = 0;
317: struct in6pcb *in6p;
1.20 itojun 318:
319: /*
1.29 itojun 320: * Check to see if we have a valid raw IPv6 socket
321: * corresponding to the address in the ICMPv6 message
322: * payload, and the protocol (ip6_nxt) meets the socket.
323: * XXX chase extension headers, or pass final nxt value
324: * from icmp6_notify_error()
1.20 itojun 325: */
1.29 itojun 326: in6p = NULL;
1.43 ! itojun 327: in6p = in6_pcblookup_connect(&rawin6pcb, &sa6->sin6_addr, 0,
1.29 itojun 328: (struct in6_addr *)&sa6_src->sin6_addr, 0, 0);
329: #if 0
330: if (!in6p) {
331: /*
332: * As the use of sendto(2) is fairly popular,
333: * we may want to allow non-connected pcb too.
334: * But it could be too weak against attacks...
335: * We should at least check if the local
336: * address (= s) is really ours.
337: */
338: in6p = in6_pcblookup_bind(&rawin6pcb,
339: &sa6->sin6_addr, 0, 0))
340: }
341: #endif
342:
343: if (in6p && in6p->in6p_ip6.ip6_nxt &&
344: in6p->in6p_ip6.ip6_nxt == nxt)
345: valid++;
1.20 itojun 346:
1.29 itojun 347: /*
348: * Depending on the value of "valid" and routing table
349: * size (mtudisc_{hi,lo}wat), we will:
350: * - recalcurate the new MTU and create the
351: * corresponding routing entry, or
352: * - ignore the MTU change notification.
353: */
354: icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
1.20 itojun 355:
1.29 itojun 356: /*
357: * regardless of if we called icmp6_mtudisc_update(),
358: * we need to call in6_pcbnotify(), to notify path
359: * MTU change to the userland (2292bis-02), because
360: * some unconnected sockets may share the same
361: * destination and want to know the path MTU.
362: */
1.20 itojun 363: }
1.29 itojun 364:
365: (void) in6_pcbnotify(&rawin6pcb, sa, 0,
366: (struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
1.20 itojun 367: }
368:
1.2 itojun 369: /*
370: * Generate IPv6 header and pass packet to ip6_output.
371: * Tack on options user may have setup with control call.
372: */
373: int
374: #if __STDC__
375: rip6_output(struct mbuf *m, ...)
376: #else
377: rip6_output(m, va_alist)
378: struct mbuf *m;
379: va_dcl
380: #endif
381: {
382: struct socket *so;
383: struct sockaddr_in6 *dstsock;
384: struct mbuf *control;
385: struct in6_addr *dst;
386: struct ip6_hdr *ip6;
387: struct in6pcb *in6p;
388: u_int plen = m->m_pkthdr.len;
389: int error = 0;
1.24 itojun 390: struct ip6_pktopts opt, *optp = NULL, *origoptp;
1.2 itojun 391: struct ifnet *oifp = NULL;
1.12 itojun 392: int type, code; /* for ICMPv6 output statistics only */
1.20 itojun 393: int priv = 0;
1.2 itojun 394: va_list ap;
1.27 itojun 395: int flags;
1.2 itojun 396:
397: va_start(ap, m);
398: so = va_arg(ap, struct socket *);
399: dstsock = va_arg(ap, struct sockaddr_in6 *);
400: control = va_arg(ap, struct mbuf *);
401: va_end(ap);
402:
403: in6p = sotoin6pcb(so);
404:
1.12 itojun 405: priv = 0;
406: {
407: struct proc *p = curproc; /* XXX */
408:
409: if (p && !suser(p->p_ucred, &p->p_acflag))
410: priv = 1;
411: }
1.2 itojun 412: dst = &dstsock->sin6_addr;
413: if (control) {
414: if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
415: goto bad;
416: optp = &opt;
417: } else
418: optp = in6p->in6p_outputopts;
419:
1.12 itojun 420: /*
421: * For an ICMPv6 packet, we should know its type and code
422: * to update statistics.
423: */
424: if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
425: struct icmp6_hdr *icmp6;
426: if (m->m_len < sizeof(struct icmp6_hdr) &&
427: (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
428: error = ENOBUFS;
429: goto bad;
430: }
431: icmp6 = mtod(m, struct icmp6_hdr *);
432: type = icmp6->icmp6_type;
433: code = icmp6->icmp6_code;
434: }
435:
1.2 itojun 436: M_PREPEND(m, sizeof(*ip6), M_WAIT);
437: ip6 = mtod(m, struct ip6_hdr *);
438:
439: /*
440: * Next header might not be ICMP6 but use its pseudo header anyway.
441: */
442: ip6->ip6_dst = *dst;
443:
1.24 itojun 444: /* KAME hack: embed scopeid */
445: origoptp = in6p->in6p_outputopts;
446: in6p->in6p_outputopts = optp;
447: if (in6_embedscope(&ip6->ip6_dst, dstsock, in6p, &oifp) != 0) {
448: error = EINVAL;
449: goto bad;
1.2 itojun 450: }
1.24 itojun 451: in6p->in6p_outputopts = origoptp;
1.2 itojun 452:
1.12 itojun 453: /*
454: * Source address selection.
455: */
456: {
1.2 itojun 457: struct in6_addr *in6a;
458:
1.43 ! itojun 459: if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
! 460: &in6p->in6p_route, &in6p->in6p_laddr, &error)) == 0) {
1.2 itojun 461: if (error == 0)
462: error = EADDRNOTAVAIL;
463: goto bad;
464: }
465: ip6->ip6_src = *in6a;
1.24 itojun 466: if (in6p->in6p_route.ro_rt) {
467: /* what if oifp contradicts ? */
1.2 itojun 468: oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
1.24 itojun 469: }
1.12 itojun 470: }
1.2 itojun 471:
472: ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
1.20 itojun 473: ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
474: ip6->ip6_vfc |= IPV6_VERSION;
1.2 itojun 475: #if 0 /* ip6_plen will be filled in ip6_output. */
476: ip6->ip6_plen = htons((u_short)plen);
477: #endif
478: ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt;
1.12 itojun 479: ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
1.2 itojun 480:
481: if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
482: in6p->in6p_cksum != -1) {
483: int off;
1.31 itojun 484: u_int16_t sum;
1.2 itojun 485:
486: #define offsetof(type, member) ((size_t)(&((type *)0)->member)) /* XXX */
487:
488: /* compute checksum */
489: if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
490: off = offsetof(struct icmp6_hdr, icmp6_cksum);
491: else
492: off = in6p->in6p_cksum;
493: if (plen < off + 1) {
494: error = EINVAL;
495: goto bad;
496: }
497: off += sizeof(struct ip6_hdr);
498:
1.31 itojun 499: sum = 0;
500: m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
501: sum = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
502: m_copyback(m, off, sizeof(sum), (caddr_t)&sum);
1.2 itojun 503: }
504:
1.24 itojun 505: #ifdef IPSEC
1.26 itojun 506: if (ipsec_setsocket(m, so) != 0) {
507: error = ENOBUFS;
508: goto bad;
509: }
1.38 itojun 510: #endif /* IPSEC */
1.27 itojun 511:
512: flags = 0;
1.41 itojun 513: #ifdef IN6P_MINMTU
1.27 itojun 514: if (in6p->in6p_flags & IN6P_MINMTU)
515: flags |= IPV6_MINMTU;
516: #endif
1.24 itojun 517:
1.27 itojun 518: error = ip6_output(m, optp, &in6p->in6p_route, flags,
519: in6p->in6p_moptions, &oifp);
1.12 itojun 520: if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
521: if (oifp)
522: icmp6_ifoutstat_inc(oifp, type, code);
523: icmp6stat.icp6s_outhist[type]++;
1.37 itojun 524: } else
525: rip6stat.rip6s_opackets++;
1.2 itojun 526:
527: goto freectl;
528:
529: bad:
530: if (m)
531: m_freem(m);
532:
533: freectl:
534: if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
535: RTFREE(optp->ip6po_route.ro_rt);
536: if (control)
537: m_freem(control);
538: return(error);
539: }
540:
1.40 itojun 541: /*
1.2 itojun 542: * Raw IPv6 socket option processing.
1.40 itojun 543: */
1.2 itojun 544: int
1.36 itojun 545: rip6_ctloutput(op, so, level, optname, mp)
1.2 itojun 546: int op;
547: struct socket *so;
548: int level, optname;
1.36 itojun 549: struct mbuf **mp;
1.2 itojun 550: {
551: int error = 0;
552:
1.27 itojun 553: switch (level) {
554: case IPPROTO_IPV6:
555: switch (optname) {
556: case MRT6_INIT:
557: case MRT6_DONE:
558: case MRT6_ADD_MIF:
559: case MRT6_DEL_MIF:
560: case MRT6_ADD_MFC:
561: case MRT6_DEL_MFC:
562: case MRT6_PIM:
563: if (op == PRCO_SETOPT) {
1.36 itojun 564: error = ip6_mrouter_set(optname, so, *mp);
565: if (*mp)
566: (void)m_free(*mp);
567: } else if (op == PRCO_GETOPT)
568: error = ip6_mrouter_get(optname, so, mp);
569: else
570: error = EINVAL;
571: return (error);
572: case IPV6_CHECKSUM:
1.43 ! itojun 573: return (ip6_raw_ctloutput(op, so, level, optname, mp));
1.36 itojun 574: default:
575: return (ip6_ctloutput(op, so, level, optname, mp));
1.27 itojun 576: }
577:
578: case IPPROTO_ICMPV6:
579: /*
580: * XXX: is it better to call icmp6_ctloutput() directly
581: * from protosw?
582: */
1.43 ! itojun 583: return (icmp6_ctloutput(op, so, level, optname, mp));
1.27 itojun 584:
585: default:
1.36 itojun 586: if (op == PRCO_SETOPT && *mp)
587: m_free(*mp);
588: return EINVAL;
1.2 itojun 589: }
590: }
591:
592: extern u_long rip6_sendspace;
593: extern u_long rip6_recvspace;
594:
595: int
596: rip6_usrreq(so, req, m, nam, control, p)
1.27 itojun 597: struct socket *so;
1.2 itojun 598: int req;
599: struct mbuf *m, *nam, *control;
600: struct proc *p;
601: {
1.27 itojun 602: struct in6pcb *in6p = sotoin6pcb(so);
1.2 itojun 603: int s;
604: int error = 0;
605: /* extern struct socket *ip6_mrouter; */ /* xxx */
1.12 itojun 606: int priv;
607:
608: priv = 0;
609: if (p && !suser(p->p_ucred, &p->p_acflag))
610: priv++;
1.2 itojun 611:
612: if (req == PRU_CONTROL)
613: return (in6_control(so, (u_long)m, (caddr_t)nam,
614: (struct ifnet *)control, p));
1.17 thorpej 615:
1.18 thorpej 616: if (req == PRU_PURGEIF) {
1.33 itojun 617: in6_pcbpurgeif0(&rawin6pcb, (struct ifnet *)control);
1.18 thorpej 618: in6_purgeif((struct ifnet *)control);
619: in6_pcbpurgeif(&rawin6pcb, (struct ifnet *)control);
1.17 thorpej 620: return (0);
621: }
1.2 itojun 622:
623: switch (req) {
624: case PRU_ATTACH:
625: if (in6p)
626: panic("rip6_attach");
1.12 itojun 627: if (!priv) {
1.2 itojun 628: error = EACCES;
629: break;
630: }
1.4 itojun 631: s = splsoftnet();
1.27 itojun 632: if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) {
633: splx(s);
634: break;
635: }
636: if ((error = in6_pcballoc(so, &rawin6pcb)) != 0)
637: {
1.2 itojun 638: splx(s);
639: break;
640: }
641: splx(s);
642: in6p = sotoin6pcb(so);
1.5 itojun 643: in6p->in6p_ip6.ip6_nxt = (long)nam;
1.2 itojun 644: in6p->in6p_cksum = -1;
645:
646: MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
1.43 ! itojun 647: sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
1.12 itojun 648: if (in6p->in6p_icmp6filt == NULL) {
649: in6_pcbdetach(in6p);
650: error = ENOMEM;
651: break;
652: }
1.2 itojun 653: ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
654: break;
655:
656: case PRU_DISCONNECT:
657: if ((so->so_state & SS_ISCONNECTED) == 0) {
658: error = ENOTCONN;
659: break;
660: }
661: in6p->in6p_faddr = in6addr_any;
662: so->so_state &= ~SS_ISCONNECTED; /* XXX */
663: break;
664:
665: case PRU_ABORT:
666: soisdisconnected(so);
667: /* Fallthrough */
668: case PRU_DETACH:
669: if (in6p == 0)
670: panic("rip6_detach");
671: if (so == ip6_mrouter)
672: ip6_mrouter_done();
673: /* xxx: RSVP */
674: if (in6p->in6p_icmp6filt) {
675: FREE(in6p->in6p_icmp6filt, M_PCB);
676: in6p->in6p_icmp6filt = NULL;
677: }
678: in6_pcbdetach(in6p);
679: break;
680:
681: case PRU_BIND:
682: {
683: struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
684: struct ifaddr *ia = NULL;
685:
686: if (nam->m_len != sizeof(*addr)) {
687: error = EINVAL;
688: break;
689: }
1.24 itojun 690: if ((ifnet.tqh_first == 0) || (addr->sin6_family != AF_INET6)) {
691: error = EADDRNOTAVAIL;
692: break;
693: }
694: #ifdef ENABLE_DEFAULT_SCOPE
695: if (addr->sin6_scope_id == 0) /* not change if specified */
696: addr->sin6_scope_id =
1.43 ! itojun 697: scope6_addr2default(&addr->sin6_addr);
1.24 itojun 698: #endif
1.34 itojun 699: /* KAME hack: embed scopeid */
700: if (in6_embedscope(&addr->sin6_addr, addr, in6p, NULL) != 0)
701: return EINVAL;
702: #ifndef SCOPEDROUTING
703: addr->sin6_scope_id = 0; /* for ifa_ifwithaddr */
704: #endif
705:
1.23 itojun 706: /*
707: * we don't support mapped address here, it would confuse
708: * users so reject it
709: */
710: if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
711: error = EADDRNOTAVAIL;
712: break;
713: }
1.24 itojun 714: if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
715: (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
1.2 itojun 716: error = EADDRNOTAVAIL;
717: break;
718: }
719: if (ia &&
720: ((struct in6_ifaddr *)ia)->ia6_flags &
721: (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
722: IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
723: error = EADDRNOTAVAIL;
724: break;
725: }
726: in6p->in6p_laddr = addr->sin6_addr;
727: break;
728: }
1.36 itojun 729:
1.2 itojun 730: case PRU_CONNECT:
1.40 itojun 731: {
1.2 itojun 732: struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
733: struct in6_addr *in6a = NULL;
1.24 itojun 734: #ifdef ENABLE_DEFAULT_SCOPE
735: struct sockaddr_in6 sin6;
736: #endif
1.2 itojun 737:
738: if (nam->m_len != sizeof(*addr)) {
739: error = EINVAL;
740: break;
741: }
1.24 itojun 742: if (ifnet.tqh_first == 0)
743: {
1.2 itojun 744: error = EADDRNOTAVAIL;
745: break;
746: }
747: if (addr->sin6_family != AF_INET6) {
748: error = EAFNOSUPPORT;
749: break;
750: }
751:
1.24 itojun 752: #ifdef ENABLE_DEFAULT_SCOPE
753: if (addr->sin6_scope_id == 0) {
754: /* protect *addr */
755: sin6 = *addr;
756: addr = &sin6;
757: addr->sin6_scope_id =
758: scope6_addr2default(&addr->sin6_addr);
759: }
760: #endif
761:
1.2 itojun 762: /* Source address selection. XXX: need pcblookup? */
1.12 itojun 763: in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
1.43 ! itojun 764: in6p->in6p_moptions, &in6p->in6p_route,
! 765: &in6p->in6p_laddr, &error);
1.12 itojun 766: if (in6a == NULL) {
1.2 itojun 767: if (error == 0)
768: error = EADDRNOTAVAIL;
769: break;
770: }
771: in6p->in6p_laddr = *in6a;
772: in6p->in6p_faddr = addr->sin6_addr;
773: soisconnected(so);
774: break;
1.40 itojun 775: }
1.2 itojun 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:
1.40 itojun 792: {
1.2 itojun 793: struct sockaddr_in6 tmp;
794: struct sockaddr_in6 *dst;
795:
1.24 itojun 796: /* always copy sockaddr to avoid overwrites */
1.2 itojun 797: if (so->so_state & SS_ISCONNECTED) {
798: if (nam) {
799: error = EISCONN;
800: break;
801: }
802: /* XXX */
803: bzero(&tmp, sizeof(tmp));
804: tmp.sin6_family = AF_INET6;
805: tmp.sin6_len = sizeof(struct sockaddr_in6);
806: bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
807: sizeof(struct in6_addr));
808: dst = &tmp;
809: } else {
810: if (nam == NULL) {
811: error = ENOTCONN;
812: break;
813: }
1.42 itojun 814: if (nam->m_len != sizeof(tmp)) {
815: error = EINVAL;
816: break;
817: }
818:
1.24 itojun 819: tmp = *mtod(nam, struct sockaddr_in6 *);
820: dst = &tmp;
1.42 itojun 821:
822: if (dst->sin6_family != AF_INET6) {
823: error = EAFNOSUPPORT;
824: break;
825: }
1.2 itojun 826: }
1.24 itojun 827: #ifdef ENABLE_DEFAULT_SCOPE
828: if (dst->sin6_scope_id == 0) {
829: dst->sin6_scope_id =
1.43 ! itojun 830: scope6_addr2default(&dst->sin6_addr);
1.24 itojun 831: }
832: #endif
1.2 itojun 833: error = rip6_output(m, so, dst, control);
834: m = NULL;
835: break;
1.40 itojun 836: }
1.2 itojun 837:
838: case PRU_SENSE:
839: /*
840: * stat: don't bother with a blocksize
841: */
842: return(0);
843: /*
844: * Not supported.
845: */
846: case PRU_RCVOOB:
847: case PRU_RCVD:
848: case PRU_LISTEN:
849: case PRU_ACCEPT:
850: case PRU_SENDOOB:
851: error = EOPNOTSUPP;
852: break;
853:
854: case PRU_SOCKADDR:
855: in6_setsockaddr(in6p, nam);
856: break;
857:
858: case PRU_PEERADDR:
859: in6_setpeeraddr(in6p, nam);
860: break;
861:
862: default:
863: panic("rip6_usrreq");
864: }
865: if (m != NULL)
866: m_freem(m);
867: return(error);
868: }
CVSweb <webmaster@jp.NetBSD.org>