Annotation of src/sys/netinet6/ip6_input.c, Revision 1.37.2.10
1.37.2.10! nathanw 1: /* $NetBSD: ip6_input.c,v 1.37.2.9 2002/06/20 03:49:19 nathanw Exp $ */
1.37.2.3 nathanw 2: /* $KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 itojun Exp $ */
1.3 thorpej 3:
1.2 itojun 4: /*
5: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6: * All rights reserved.
1.21 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.21 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: * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
66: */
67:
1.37.2.7 nathanw 68: #include <sys/cdefs.h>
1.37.2.10! nathanw 69: __KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.37.2.9 2002/06/20 03:49:19 nathanw Exp $");
1.37.2.7 nathanw 70:
1.2 itojun 71: #include "opt_inet.h"
1.4 thorpej 72: #include "opt_ipsec.h"
1.15 darrenr 73: #include "opt_pfil_hooks.h"
1.2 itojun 74:
75: #include <sys/param.h>
76: #include <sys/systm.h>
77: #include <sys/malloc.h>
78: #include <sys/mbuf.h>
79: #include <sys/domain.h>
80: #include <sys/protosw.h>
81: #include <sys/socket.h>
82: #include <sys/socketvar.h>
83: #include <sys/errno.h>
84: #include <sys/time.h>
85: #include <sys/kernel.h>
86: #include <sys/syslog.h>
1.37.2.1 nathanw 87: #include <sys/lwp.h>
1.2 itojun 88: #include <sys/proc.h>
1.37.2.7 nathanw 89: #include <sys/sysctl.h>
1.2 itojun 90:
91: #include <net/if.h>
92: #include <net/if_types.h>
93: #include <net/if_dl.h>
94: #include <net/route.h>
95: #include <net/netisr.h>
1.15 darrenr 96: #ifdef PFIL_HOOKS
97: #include <net/pfil.h>
98: #endif
1.2 itojun 99:
100: #include <netinet/in.h>
1.9 itojun 101: #include <netinet/in_systm.h>
102: #ifdef INET
103: #include <netinet/ip.h>
104: #include <netinet/ip_icmp.h>
1.37.2.7 nathanw 105: #endif /* INET */
1.14 itojun 106: #include <netinet/ip6.h>
1.2 itojun 107: #include <netinet6/in6_var.h>
1.11 itojun 108: #include <netinet6/ip6_var.h>
1.2 itojun 109: #include <netinet6/in6_pcb.h>
1.14 itojun 110: #include <netinet/icmp6.h>
1.2 itojun 111: #include <netinet6/in6_ifattach.h>
112: #include <netinet6/nd6.h>
113:
1.37 itojun 114: #ifdef IPSEC
115: #include <netinet6/ipsec.h>
116: #endif
117:
1.2 itojun 118: #include <netinet6/ip6protosw.h>
119:
120: /* we need it for NLOOP. */
121: #include "loop.h"
122: #include "faith.h"
123: #include "gif.h"
124: #include "bpfilter.h"
125:
1.37.2.8 nathanw 126: #if NGIF > 0
127: #include <netinet6/in6_gif.h>
128: #endif
129:
1.9 itojun 130: #include <net/net_osdep.h>
131:
1.2 itojun 132: extern struct domain inet6domain;
133:
134: u_char ip6_protox[IPPROTO_MAX];
135: static int ip6qmaxlen = IFQ_MAXLEN;
136: struct in6_ifaddr *in6_ifaddr;
137: struct ifqueue ip6intrq;
138:
139: extern struct ifnet loif[NLOOP];
140: int ip6_forward_srcrt; /* XXX */
141: int ip6_sourcecheck; /* XXX */
142: int ip6_sourcecheck_interval; /* XXX */
1.9 itojun 143:
1.29 thorpej 144: #ifdef PFIL_HOOKS
145: struct pfil_head inet6_pfil_hook;
146: #endif
147:
1.2 itojun 148: struct ip6stat ip6stat;
149:
150: static void ip6_init2 __P((void *));
151:
1.5 itojun 152: static int ip6_hopopts_input __P((u_int32_t *, u_int32_t *, struct mbuf **, int *));
1.2 itojun 153:
154: /*
155: * IP6 initialization: fill in IP6 protocol switch table.
156: * All protocols not implemented in kernel go to raw IP6 protocol handler.
157: */
158: void
159: ip6_init()
160: {
1.35 itojun 161: struct ip6protosw *pr;
162: int i;
1.2 itojun 163:
164: pr = (struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
165: if (pr == 0)
166: panic("ip6_init");
167: for (i = 0; i < IPPROTO_MAX; i++)
168: ip6_protox[i] = pr - inet6sw;
169: for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
170: pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
171: if (pr->pr_domain->dom_family == PF_INET6 &&
172: pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
173: ip6_protox[pr->pr_protocol] = pr - inet6sw;
174: ip6intrq.ifq_maxlen = ip6qmaxlen;
175: nd6_init();
176: frag6_init();
1.37.2.9 nathanw 177: ip6_flow_seq = arc4random();
1.2 itojun 178:
179: ip6_init2((void *)0);
1.29 thorpej 180:
181: #ifdef PFIL_HOOKS
182: /* Register our Packet Filter hook. */
1.33 thorpej 183: inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
184: inet6_pfil_hook.ph_af = AF_INET6;
1.29 thorpej 185: i = pfil_head_register(&inet6_pfil_hook);
186: if (i != 0)
187: printf("ip6_init: WARNING: unable to register pfil hook, "
188: "error %d\n", i);
189: #endif /* PFIL_HOOKS */
1.2 itojun 190: }
191:
192: static void
193: ip6_init2(dummy)
194: void *dummy;
195: {
196:
197: /* nd6_timer_init */
1.19 thorpej 198: callout_init(&nd6_timer_ch);
199: callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
1.2 itojun 200: }
201:
202: /*
203: * IP6 input interrupt handling. Just pass the packet to ip6_input.
204: */
205: void
206: ip6intr()
207: {
208: int s;
209: struct mbuf *m;
210:
211: for (;;) {
1.37.2.4 nathanw 212: s = splnet();
1.2 itojun 213: IF_DEQUEUE(&ip6intrq, m);
214: splx(s);
215: if (m == 0)
216: return;
217: ip6_input(m);
218: }
219: }
220:
221: extern struct route_in6 ip6_forward_rt;
222:
223: void
224: ip6_input(m)
225: struct mbuf *m;
226: {
1.9 itojun 227: struct ip6_hdr *ip6;
1.2 itojun 228: int off = sizeof(struct ip6_hdr), nest;
229: u_int32_t plen;
1.5 itojun 230: u_int32_t rtalert = ~0;
1.2 itojun 231: int nxt, ours = 0;
1.9 itojun 232: struct ifnet *deliverifp = NULL;
1.2 itojun 233:
234: #ifdef IPSEC
235: /*
236: * should the inner packet be considered authentic?
237: * see comment in ah4_input().
238: */
1.37.2.8 nathanw 239: m->m_flags &= ~M_AUTHIPHDR;
240: m->m_flags &= ~M_AUTHIPDGM;
1.2 itojun 241: #endif
1.9 itojun 242:
1.2 itojun 243: /*
1.37.2.6 nathanw 244: * mbuf statistics
1.2 itojun 245: */
246: if (m->m_flags & M_EXT) {
247: if (m->m_next)
248: ip6stat.ip6s_mext2m++;
249: else
250: ip6stat.ip6s_mext1++;
251: } else {
1.37.2.3 nathanw 252: #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0]))
1.2 itojun 253: if (m->m_next) {
1.9 itojun 254: if (m->m_flags & M_LOOP) {
1.37.2.6 nathanw 255: ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */
1.37.2.3 nathanw 256: } else if (m->m_pkthdr.rcvif->if_index < M2MMAX)
1.2 itojun 257: ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++;
258: else
259: ip6stat.ip6s_m2m[0]++;
260: } else
261: ip6stat.ip6s_m1++;
1.37.2.3 nathanw 262: #undef M2MMAX
1.2 itojun 263: }
264:
1.9 itojun 265: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
266: ip6stat.ip6s_total++;
267:
268: #ifndef PULLDOWN_TEST
269: /* XXX is the line really necessary? */
1.2 itojun 270: IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), /*nothing*/);
1.9 itojun 271: #endif
1.2 itojun 272:
1.9 itojun 273: if (m->m_len < sizeof(struct ip6_hdr)) {
274: struct ifnet *inifp;
275: inifp = m->m_pkthdr.rcvif;
1.37.2.9 nathanw 276: if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
1.9 itojun 277: ip6stat.ip6s_toosmall++;
278: in6_ifstat_inc(inifp, ifs6_in_hdrerr);
279: return;
280: }
1.2 itojun 281: }
282:
283: ip6 = mtod(m, struct ip6_hdr *);
284:
285: if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
286: ip6stat.ip6s_badvers++;
1.9 itojun 287: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
1.2 itojun 288: goto bad;
289: }
1.15 darrenr 290:
291: #ifdef PFIL_HOOKS
292: /*
293: * Run through list of hooks for input packets. If there are any
294: * filters which require that additional packets in the flow are
295: * not fast-forwarded, they must clear the M_CANFASTFWD flag.
296: * Note that filters must _never_ set this flag, as another filter
297: * in the list may have previously cleared it.
298: */
1.37.2.3 nathanw 299: /*
300: * let ipfilter look at packet on the wire,
301: * not the decapsulated packet.
302: */
303: #ifdef IPSEC
1.37.2.5 nathanw 304: if (!ipsec_getnhist(m))
1.37.2.3 nathanw 305: #else
306: if (1)
307: #endif
308: {
309: if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif,
310: PFIL_IN) != 0)
311: return;
312: if (m == NULL)
313: return;
314: ip6 = mtod(m, struct ip6_hdr *);
315: }
1.15 darrenr 316: #endif /* PFIL_HOOKS */
317:
1.2 itojun 318: ip6stat.ip6s_nxthist[ip6->ip6_nxt]++;
319:
1.30 thorpej 320: #ifdef ALTQ
321: if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
322: /* packet is dropped by traffic conditioner */
323: return;
1.9 itojun 324: }
325: #endif
326:
1.2 itojun 327: /*
1.37.2.6 nathanw 328: * Check against address spoofing/corruption.
1.2 itojun 329: */
330: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
331: IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
1.37.2.9 nathanw 332: /*
333: * XXX: "badscope" is not very suitable for a multicast source.
334: */
1.2 itojun 335: ip6stat.ip6s_badscope++;
1.9 itojun 336: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2 itojun 337: goto bad;
338: }
1.13 itojun 339: /*
1.37.2.6 nathanw 340: * The following check is not documented in specs. A malicious
341: * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
342: * and bypass security checks (act as if it was from 127.0.0.1 by using
343: * IPv6 src ::ffff:127.0.0.1). Be cautious.
1.35 itojun 344: *
1.37.2.6 nathanw 345: * This check chokes if we are in an SIIT cloud. As none of BSDs
346: * support IPv4-less kernel compilation, we cannot support SIIT
347: * environment at all. So, it makes more sense for us to reject any
348: * malicious packets for non-SIIT environment, than try to do a
1.37.2.9 nathanw 349: * partial support for SIIT environment.
1.13 itojun 350: */
351: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
352: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
353: ip6stat.ip6s_badscope++;
354: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
355: goto bad;
356: }
1.17 itojun 357: #if 0
1.13 itojun 358: /*
1.17 itojun 359: * Reject packets with IPv4 compatible addresses (auto tunnel).
360: *
361: * The code forbids auto tunnel relay case in RFC1933 (the check is
362: * stronger than RFC1933). We may want to re-enable it if mech-xx
363: * is revised to forbid relaying case.
1.13 itojun 364: */
365: if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
366: IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
367: ip6stat.ip6s_badscope++;
368: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
369: goto bad;
370: }
371: #endif
1.35 itojun 372:
1.2 itojun 373: if (IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) ||
374: IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) {
375: if (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) {
376: ours = 1;
1.9 itojun 377: deliverifp = m->m_pkthdr.rcvif;
1.2 itojun 378: goto hbhcheck;
379: } else {
380: ip6stat.ip6s_badscope++;
1.9 itojun 381: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2 itojun 382: goto bad;
383: }
384: }
385:
1.37.2.3 nathanw 386: /* drop packets if interface ID portion is already filled */
387: if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
388: if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) &&
389: ip6->ip6_src.s6_addr16[1]) {
390: ip6stat.ip6s_badscope++;
391: goto bad;
392: }
393: if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
394: ip6->ip6_dst.s6_addr16[1]) {
395: ip6stat.ip6s_badscope++;
396: goto bad;
397: }
1.2 itojun 398: }
399:
1.37.2.3 nathanw 400: if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
401: ip6->ip6_src.s6_addr16[1]
402: = htons(m->m_pkthdr.rcvif->if_index);
403: if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
404: ip6->ip6_dst.s6_addr16[1]
405: = htons(m->m_pkthdr.rcvif->if_index);
406:
407: /*
408: * We use rt->rt_ifp to determine if the address is ours or not.
409: * If rt_ifp is lo0, the address is ours.
410: * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0,
411: * so any address under fe80::%lo0/64 will be mistakenly considered
412: * local. The special case is supplied to handle the case properly
413: * by actually looking at interface addresses
414: * (using in6ifa_ifpwithaddr).
415: */
416: if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 &&
417: IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) {
418: if (!in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst)) {
419: icmp6_error(m, ICMP6_DST_UNREACH,
420: ICMP6_DST_UNREACH_ADDR, 0);
421: /* m is already freed */
422: return;
1.21 itojun 423: }
1.37.2.3 nathanw 424:
425: ours = 1;
426: deliverifp = m->m_pkthdr.rcvif;
427: goto hbhcheck;
1.9 itojun 428: }
429:
1.2 itojun 430: /*
431: * Multicast check
432: */
433: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
434: struct in6_multi *in6m = 0;
1.9 itojun 435:
436: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
1.2 itojun 437: /*
438: * See if we belong to the destination multicast group on the
439: * arrival interface.
440: */
441: IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
442: if (in6m)
443: ours = 1;
444: else if (!ip6_mrouter) {
445: ip6stat.ip6s_notmember++;
446: ip6stat.ip6s_cantforward++;
1.9 itojun 447: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2 itojun 448: goto bad;
449: }
1.9 itojun 450: deliverifp = m->m_pkthdr.rcvif;
1.2 itojun 451: goto hbhcheck;
452: }
453:
454: /*
455: * Unicast check
456: */
1.22 itojun 457: if (ip6_forward_rt.ro_rt != NULL &&
1.37.2.9 nathanw 458: (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 &&
1.22 itojun 459: IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1.35 itojun 460: &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr))
1.25 itojun 461: ip6stat.ip6s_forward_cachehit++;
1.22 itojun 462: else {
1.35 itojun 463: struct sockaddr_in6 *dst6;
464:
1.2 itojun 465: if (ip6_forward_rt.ro_rt) {
1.22 itojun 466: /* route is down or destination is different */
1.25 itojun 467: ip6stat.ip6s_forward_cachemiss++;
1.2 itojun 468: RTFREE(ip6_forward_rt.ro_rt);
469: ip6_forward_rt.ro_rt = 0;
470: }
1.22 itojun 471:
1.2 itojun 472: bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6));
1.35 itojun 473: dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst;
474: dst6->sin6_len = sizeof(struct sockaddr_in6);
475: dst6->sin6_family = AF_INET6;
476: dst6->sin6_addr = ip6->ip6_dst;
1.2 itojun 477:
1.9 itojun 478: rtalloc((struct route *)&ip6_forward_rt);
1.2 itojun 479: }
480:
481: #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key))
482:
483: /*
484: * Accept the packet if the forwarding interface to the destination
485: * according to the routing table is the loopback interface,
486: * unless the associated route has a gateway.
487: * Note that this approach causes to accept a packet if there is a
488: * route to the loopback interface for the destination of the packet.
489: * But we think it's even useful in some situations, e.g. when using
490: * a special daemon which wants to intercept the packet.
491: */
492: if (ip6_forward_rt.ro_rt &&
493: (ip6_forward_rt.ro_rt->rt_flags &
494: (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
1.37.2.9 nathanw 495: !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) &&
1.2 itojun 496: #if 0
497: /*
498: * The check below is redundant since the comparison of
499: * the destination and the key of the rtentry has
500: * already done through looking up the routing table.
501: */
502: IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1.37.2.9 nathanw 503: &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) &&
1.2 itojun 504: #endif
1.9 itojun 505: ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) {
1.2 itojun 506: struct in6_ifaddr *ia6 =
507: (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa;
508: if (ia6->ia6_flags & IN6_IFF_ANYCAST)
509: m->m_flags |= M_ANYCAST6;
1.24 itojun 510: /*
511: * packets to a tentative, duplicated, or somehow invalid
512: * address must not be accepted.
513: */
1.2 itojun 514: if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
1.24 itojun 515: /* this address is ready */
1.2 itojun 516: ours = 1;
1.9 itojun 517: deliverifp = ia6->ia_ifp; /* correct? */
1.2 itojun 518: goto hbhcheck;
519: } else {
1.24 itojun 520: /* address is not ready, so discard the packet. */
1.34 itojun 521: nd6log((LOG_INFO,
1.27 itojun 522: "ip6_input: packet to an unready address %s->%s\n",
1.24 itojun 523: ip6_sprintf(&ip6->ip6_src),
1.34 itojun 524: ip6_sprintf(&ip6->ip6_dst)));
1.24 itojun 525:
526: goto bad;
1.2 itojun 527: }
528: }
529:
530: /*
1.37.2.9 nathanw 531: * FAITH (Firewall Aided Internet Translator)
1.2 itojun 532: */
533: #if defined(NFAITH) && 0 < NFAITH
534: if (ip6_keepfaith) {
535: if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp
536: && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) {
537: /* XXX do we need more sanity checks? */
538: ours = 1;
1.37.2.7 nathanw 539: deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */
1.9 itojun 540: goto hbhcheck;
541: }
542: }
543: #endif
544:
545: #if 0
546: {
547: /*
548: * Last resort: check in6_ifaddr for incoming interface.
549: * The code is here until I update the "goto ours hack" code above
550: * working right.
551: */
552: struct ifaddr *ifa;
553: for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
554: ifa;
555: ifa = ifa->ifa_list.tqe_next) {
556: if (ifa->ifa_addr == NULL)
557: continue; /* just for safety */
558: if (ifa->ifa_addr->sa_family != AF_INET6)
559: continue;
560: if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
561: ours = 1;
562: deliverifp = ifa->ifa_ifp;
1.2 itojun 563: goto hbhcheck;
564: }
565: }
1.9 itojun 566: }
1.2 itojun 567: #endif
568:
569: /*
570: * Now there is no reason to process the packet if it's not our own
571: * and we're not a router.
572: */
573: if (!ip6_forwarding) {
574: ip6stat.ip6s_cantforward++;
1.9 itojun 575: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2 itojun 576: goto bad;
577: }
578:
579: hbhcheck:
580: /*
581: * Process Hop-by-Hop options header if it's contained.
582: * m may be modified in ip6_hopopts_input().
583: * If a JumboPayload option is included, plen will also be modified.
584: */
585: plen = (u_int32_t)ntohs(ip6->ip6_plen);
586: if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9 itojun 587: struct ip6_hbh *hbh;
588:
589: if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
590: #if 0 /*touches NULL pointer*/
591: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
592: #endif
1.2 itojun 593: return; /* m have already been freed */
1.9 itojun 594: }
1.22 itojun 595:
1.2 itojun 596: /* adjust pointer */
597: ip6 = mtod(m, struct ip6_hdr *);
1.22 itojun 598:
599: /*
1.37.2.8 nathanw 600: * if the payload length field is 0 and the next header field
1.22 itojun 601: * indicates Hop-by-Hop Options header, then a Jumbo Payload
602: * option MUST be included.
603: */
604: if (ip6->ip6_plen == 0 && plen == 0) {
605: /*
606: * Note that if a valid jumbo payload option is
607: * contained, ip6_hoptops_input() must set a valid
1.37.2.9 nathanw 608: * (non-zero) payload length to the variable plen.
1.22 itojun 609: */
610: ip6stat.ip6s_badoptions++;
611: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
612: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
613: icmp6_error(m, ICMP6_PARAM_PROB,
614: ICMP6_PARAMPROB_HEADER,
615: (caddr_t)&ip6->ip6_plen - (caddr_t)ip6);
616: return;
617: }
1.9 itojun 618: #ifndef PULLDOWN_TEST
619: /* ip6_hopopts_input() ensures that mbuf is contiguous */
620: hbh = (struct ip6_hbh *)(ip6 + 1);
621: #else
622: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
623: sizeof(struct ip6_hbh));
624: if (hbh == NULL) {
625: ip6stat.ip6s_tooshort++;
626: return;
627: }
628: #endif
629: nxt = hbh->ip6h_nxt;
1.2 itojun 630:
631: /*
632: * accept the packet if a router alert option is included
633: * and we act as an IPv6 router.
634: */
1.5 itojun 635: if (rtalert != ~0 && ip6_forwarding)
1.2 itojun 636: ours = 1;
637: } else
638: nxt = ip6->ip6_nxt;
639:
640: /*
641: * Check that the amount of data in the buffers
642: * is as at least much as the IPv6 header would have us expect.
643: * Trim mbufs if longer than we expect.
644: * Drop packet if shorter than we expect.
645: */
646: if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
647: ip6stat.ip6s_tooshort++;
1.9 itojun 648: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.2 itojun 649: goto bad;
650: }
651: if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
652: if (m->m_len == m->m_pkthdr.len) {
653: m->m_len = sizeof(struct ip6_hdr) + plen;
654: m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
655: } else
656: m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
657: }
658:
659: /*
660: * Forward if desirable.
661: */
662: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
663: /*
664: * If we are acting as a multicast router, all
665: * incoming multicast packets are passed to the
666: * kernel-level multicast forwarding function.
667: * The packet is returned (relatively) intact; if
668: * ip6_mforward() returns a non-zero value, the packet
669: * must be discarded, else it may be accepted below.
670: */
671: if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
672: ip6stat.ip6s_cantforward++;
673: m_freem(m);
674: return;
675: }
676: if (!ours) {
677: m_freem(m);
678: return;
679: }
1.22 itojun 680: } else if (!ours) {
1.2 itojun 681: ip6_forward(m, 0);
682: return;
1.37.2.9 nathanw 683: }
1.25 itojun 684:
685: ip6 = mtod(m, struct ip6_hdr *);
686:
687: /*
688: * Malicious party may be able to use IPv4 mapped addr to confuse
689: * tcp/udp stack and bypass security checks (act as if it was from
690: * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
691: *
692: * For SIIT end node behavior, you may want to disable the check.
693: * However, you will become vulnerable to attacks using IPv4 mapped
694: * source.
695: */
696: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
697: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
698: ip6stat.ip6s_badscope++;
699: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
700: goto bad;
701: }
1.2 itojun 702:
703: /*
704: * Tell launch routine the next header
705: */
1.12 itojun 706: #ifdef IFA_STATS
1.28 itojun 707: if (deliverifp != NULL) {
1.9 itojun 708: struct in6_ifaddr *ia6;
709: ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
710: if (ia6)
711: ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
712: }
713: #endif
1.2 itojun 714: ip6stat.ip6s_delivered++;
1.9 itojun 715: in6_ifstat_inc(deliverifp, ifs6_in_deliver);
1.2 itojun 716: nest = 0;
1.37.2.3 nathanw 717:
1.2 itojun 718: while (nxt != IPPROTO_DONE) {
719: if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
720: ip6stat.ip6s_toomanyhdr++;
721: goto bad;
722: }
1.8 itojun 723:
724: /*
725: * protection against faulty packet - there should be
726: * more sanity checks in header chain processing.
727: */
728: if (m->m_pkthdr.len < off) {
729: ip6stat.ip6s_tooshort++;
1.9 itojun 730: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.8 itojun 731: goto bad;
732: }
733:
1.37 itojun 734: #ifdef IPSEC
735: /*
736: * enforce IPsec policy checking if we are seeing last header.
737: * note that we do not visit this with protocols with pcb layer
738: * code - like udp/tcp/raw ip.
739: */
740: if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
741: ipsec6_in_reject(m, NULL)) {
742: ipsec6stat.in_polvio++;
743: goto bad;
744: }
745: #endif
1.37.2.9 nathanw 746:
1.2 itojun 747: nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
748: }
749: return;
750: bad:
751: m_freem(m);
752: }
753:
754: /*
755: * Hop-by-Hop options header processing. If a valid jumbo payload option is
756: * included, the real payload length will be stored in plenp.
757: */
758: static int
759: ip6_hopopts_input(plenp, rtalertp, mp, offp)
760: u_int32_t *plenp;
1.5 itojun 761: u_int32_t *rtalertp; /* XXX: should be stored more smart way */
1.2 itojun 762: struct mbuf **mp;
763: int *offp;
764: {
1.35 itojun 765: struct mbuf *m = *mp;
1.2 itojun 766: int off = *offp, hbhlen;
767: struct ip6_hbh *hbh;
768: u_int8_t *opt;
769:
770: /* validation of the length of the header */
1.9 itojun 771: #ifndef PULLDOWN_TEST
1.2 itojun 772: IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), -1);
773: hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
774: hbhlen = (hbh->ip6h_len + 1) << 3;
775:
776: IP6_EXTHDR_CHECK(m, off, hbhlen, -1);
777: hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off);
1.9 itojun 778: #else
779: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
780: sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
781: if (hbh == NULL) {
782: ip6stat.ip6s_tooshort++;
783: return -1;
784: }
785: hbhlen = (hbh->ip6h_len + 1) << 3;
786: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
787: hbhlen);
788: if (hbh == NULL) {
789: ip6stat.ip6s_tooshort++;
790: return -1;
791: }
792: #endif
1.2 itojun 793: off += hbhlen;
794: hbhlen -= sizeof(struct ip6_hbh);
795: opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh);
796:
797: if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
798: hbhlen, rtalertp, plenp) < 0)
799: return(-1);
800:
801: *offp = off;
802: *mp = m;
803: return(0);
804: }
805:
806: /*
807: * Search header for all Hop-by-hop options and process each option.
808: * This function is separate from ip6_hopopts_input() in order to
809: * handle a case where the sending node itself process its hop-by-hop
810: * options header. In such a case, the function is called from ip6_output().
1.37.2.9 nathanw 811: *
812: * The function assumes that hbh header is located right after the IPv6 header
813: * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
814: * opthead + hbhlen is located in continuous memory region.
1.2 itojun 815: */
816: int
817: ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp)
818: struct mbuf *m;
819: u_int8_t *opthead;
820: int hbhlen;
1.5 itojun 821: u_int32_t *rtalertp;
1.2 itojun 822: u_int32_t *plenp;
823: {
824: struct ip6_hdr *ip6;
825: int optlen = 0;
826: u_int8_t *opt = opthead;
827: u_int16_t rtalert_val;
1.22 itojun 828: u_int32_t jumboplen;
1.37.2.9 nathanw 829: const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1.2 itojun 830:
831: for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1.35 itojun 832: switch (*opt) {
833: case IP6OPT_PAD1:
834: optlen = 1;
835: break;
836: case IP6OPT_PADN:
837: if (hbhlen < IP6OPT_MINLEN) {
838: ip6stat.ip6s_toosmall++;
839: goto bad;
840: }
841: optlen = *(opt + 1) + 2;
842: break;
843: case IP6OPT_RTALERT:
844: /* XXX may need check for alignment */
845: if (hbhlen < IP6OPT_RTALERT_LEN) {
846: ip6stat.ip6s_toosmall++;
847: goto bad;
848: }
849: if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1.37.2.9 nathanw 850: /* XXX stat */
851: icmp6_error(m, ICMP6_PARAM_PROB,
852: ICMP6_PARAMPROB_HEADER,
853: erroff + opt + 1 - opthead);
854: return (-1);
1.35 itojun 855: }
856: optlen = IP6OPT_RTALERT_LEN;
857: bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2);
858: *rtalertp = ntohs(rtalert_val);
859: break;
860: case IP6OPT_JUMBO:
1.22 itojun 861: /* XXX may need check for alignment */
862: if (hbhlen < IP6OPT_JUMBO_LEN) {
863: ip6stat.ip6s_toosmall++;
864: goto bad;
865: }
1.35 itojun 866: if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1.37.2.9 nathanw 867: /* XXX stat */
868: icmp6_error(m, ICMP6_PARAM_PROB,
869: ICMP6_PARAMPROB_HEADER,
870: erroff + opt + 1 - opthead);
871: return (-1);
1.35 itojun 872: }
1.22 itojun 873: optlen = IP6OPT_JUMBO_LEN;
874:
875: /*
876: * IPv6 packets that have non 0 payload length
1.35 itojun 877: * must not contain a jumbo payload option.
1.22 itojun 878: */
879: ip6 = mtod(m, struct ip6_hdr *);
880: if (ip6->ip6_plen) {
881: ip6stat.ip6s_badoptions++;
882: icmp6_error(m, ICMP6_PARAM_PROB,
1.37.2.9 nathanw 883: ICMP6_PARAMPROB_HEADER,
884: erroff + opt - opthead);
885: return (-1);
1.22 itojun 886: }
1.2 itojun 887:
1.22 itojun 888: /*
889: * We may see jumbolen in unaligned location, so
890: * we'd need to perform bcopy().
891: */
892: bcopy(opt + 2, &jumboplen, sizeof(jumboplen));
893: jumboplen = (u_int32_t)htonl(jumboplen);
894:
895: #if 1
896: /*
897: * if there are multiple jumbo payload options,
898: * *plenp will be non-zero and the packet will be
899: * rejected.
900: * the behavior may need some debate in ipngwg -
901: * multiple options does not make sense, however,
902: * there's no explicit mention in specification.
903: */
904: if (*plenp != 0) {
905: ip6stat.ip6s_badoptions++;
906: icmp6_error(m, ICMP6_PARAM_PROB,
1.37.2.9 nathanw 907: ICMP6_PARAMPROB_HEADER,
908: erroff + opt + 2 - opthead);
909: return (-1);
1.22 itojun 910: }
1.8 itojun 911: #endif
1.2 itojun 912:
1.22 itojun 913: /*
914: * jumbo payload length must be larger than 65535.
915: */
916: if (jumboplen <= IPV6_MAXPACKET) {
917: ip6stat.ip6s_badoptions++;
918: icmp6_error(m, ICMP6_PARAM_PROB,
1.37.2.9 nathanw 919: ICMP6_PARAMPROB_HEADER,
920: erroff + opt + 2 - opthead);
921: return (-1);
1.22 itojun 922: }
923: *plenp = jumboplen;
924:
925: break;
1.35 itojun 926: default: /* unknown option */
927: if (hbhlen < IP6OPT_MINLEN) {
928: ip6stat.ip6s_toosmall++;
929: goto bad;
930: }
1.37.2.9 nathanw 931: optlen = ip6_unknown_opt(opt, m,
932: erroff + opt - opthead);
933: if (optlen == -1)
934: return (-1);
1.35 itojun 935: optlen += 2;
936: break;
1.2 itojun 937: }
938: }
939:
1.37.2.9 nathanw 940: return (0);
1.2 itojun 941:
942: bad:
943: m_freem(m);
1.37.2.9 nathanw 944: return (-1);
1.2 itojun 945: }
946:
947: /*
948: * Unknown option processing.
949: * The third argument `off' is the offset from the IPv6 header to the option,
950: * which is necessary if the IPv6 header the and option header and IPv6 header
951: * is not continuous in order to return an ICMPv6 error.
952: */
953: int
954: ip6_unknown_opt(optp, m, off)
955: u_int8_t *optp;
956: struct mbuf *m;
957: int off;
958: {
959: struct ip6_hdr *ip6;
960:
1.35 itojun 961: switch (IP6OPT_TYPE(*optp)) {
962: case IP6OPT_TYPE_SKIP: /* ignore the option */
963: return((int)*(optp + 1));
964: case IP6OPT_TYPE_DISCARD: /* silently discard */
965: m_freem(m);
966: return(-1);
967: case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
968: ip6stat.ip6s_badoptions++;
969: icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
970: return(-1);
971: case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
972: ip6stat.ip6s_badoptions++;
973: ip6 = mtod(m, struct ip6_hdr *);
974: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
975: (m->m_flags & (M_BCAST|M_MCAST)))
976: m_freem(m);
977: else
978: icmp6_error(m, ICMP6_PARAM_PROB,
979: ICMP6_PARAMPROB_OPTION, off);
980: return(-1);
1.2 itojun 981: }
982:
983: m_freem(m); /* XXX: NOTREACHED */
984: return(-1);
985: }
986:
987: /*
1.9 itojun 988: * Create the "control" list for this pcb.
989: *
990: * The routine will be called from upper layer handlers like tcp6_input().
991: * Thus the routine assumes that the caller (tcp6_input) have already
992: * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
993: * very first mbuf on the mbuf chain.
994: * We may want to add some infinite loop prevention or sanity checks for safety.
995: * (This applies only when you are using KAME mbuf chain restriction, i.e.
996: * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2 itojun 997: */
998: void
999: ip6_savecontrol(in6p, mp, ip6, m)
1.35 itojun 1000: struct in6pcb *in6p;
1001: struct mbuf **mp;
1002: struct ip6_hdr *ip6;
1003: struct mbuf *m;
1.2 itojun 1004: {
1.37.2.10! nathanw 1005: struct proc *p = curproc; /* XXX */
1.9 itojun 1006: int privileged;
1.2 itojun 1007:
1.9 itojun 1008: privileged = 0;
1009: if (p && !suser(p->p_ucred, &p->p_acflag))
1010: privileged++;
1011:
1012: #ifdef SO_TIMESTAMP
1.2 itojun 1013: if (in6p->in6p_socket->so_options & SO_TIMESTAMP) {
1014: struct timeval tv;
1015:
1016: microtime(&tv);
1017: *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1018: SCM_TIMESTAMP, SOL_SOCKET);
1019: if (*mp)
1020: mp = &(*mp)->m_next;
1021: }
1.9 itojun 1022: #endif
1.2 itojun 1023: if (in6p->in6p_flags & IN6P_RECVDSTADDR) {
1024: *mp = sbcreatecontrol((caddr_t) &ip6->ip6_dst,
1025: sizeof(struct in6_addr), IPV6_RECVDSTADDR,
1026: IPPROTO_IPV6);
1027: if (*mp)
1028: mp = &(*mp)->m_next;
1029: }
1030:
1031: #ifdef noyet
1032: /* options were tossed above */
1033: if (in6p->in6p_flags & IN6P_RECVOPTS)
1034: /* broken */
1035: /* ip6_srcroute doesn't do what we want here, need to fix */
1036: if (in6p->in6p_flags & IPV6P_RECVRETOPTS)
1037: /* broken */
1038: #endif
1039:
1040: /* RFC 2292 sec. 5 */
1.35 itojun 1041: if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1.2 itojun 1042: struct in6_pktinfo pi6;
1043: bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr));
1044: if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr))
1045: pi6.ipi6_addr.s6_addr16[1] = 0;
1046: pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif)
1047: ? m->m_pkthdr.rcvif->if_index
1048: : 0;
1049: *mp = sbcreatecontrol((caddr_t) &pi6,
1050: sizeof(struct in6_pktinfo), IPV6_PKTINFO,
1051: IPPROTO_IPV6);
1052: if (*mp)
1053: mp = &(*mp)->m_next;
1054: }
1055: if (in6p->in6p_flags & IN6P_HOPLIMIT) {
1056: int hlim = ip6->ip6_hlim & 0xff;
1057: *mp = sbcreatecontrol((caddr_t) &hlim,
1058: sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6);
1059: if (*mp)
1060: mp = &(*mp)->m_next;
1061: }
1062: /* IN6P_NEXTHOP - for outgoing packet only */
1063:
1064: /*
1065: * IPV6_HOPOPTS socket option. We require super-user privilege
1066: * for the option, but it might be too strict, since there might
1067: * be some hop-by-hop options which can be returned to normal user.
1068: * See RFC 2292 section 6.
1069: */
1.35 itojun 1070: if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0 && privileged) {
1.2 itojun 1071: /*
1072: * Check if a hop-by-hop options header is contatined in the
1073: * received packet, and if so, store the options as ancillary
1074: * data. Note that a hop-by-hop options header must be
1075: * just after the IPv6 header, which fact is assured through
1076: * the IPv6 input processing.
1077: */
1078: struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1079: if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9 itojun 1080: struct ip6_hbh *hbh;
1081: int hbhlen;
1082:
1083: #ifndef PULLDOWN_TEST
1084: hbh = (struct ip6_hbh *)(ip6 + 1);
1085: hbhlen = (hbh->ip6h_len + 1) << 3;
1086: #else
1087: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1088: sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
1089: if (hbh == NULL) {
1090: ip6stat.ip6s_tooshort++;
1091: return;
1092: }
1093: hbhlen = (hbh->ip6h_len + 1) << 3;
1094: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
1095: sizeof(struct ip6_hdr), hbhlen);
1096: if (hbh == NULL) {
1097: ip6stat.ip6s_tooshort++;
1098: return;
1099: }
1100: #endif
1.2 itojun 1101:
1102: /*
1103: * XXX: We copy whole the header even if a jumbo
1104: * payload option is included, which option is to
1105: * be removed before returning in the RFC 2292.
1106: * But it's too painful operation...
1107: */
1.9 itojun 1108: *mp = sbcreatecontrol((caddr_t)hbh, hbhlen,
1.2 itojun 1109: IPV6_HOPOPTS, IPPROTO_IPV6);
1110: if (*mp)
1111: mp = &(*mp)->m_next;
1112: }
1113: }
1114:
1115: /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1116: if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1117: struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1118: int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr);;
1119:
1120: /*
1121: * Search for destination options headers or routing
1122: * header(s) through the header chain, and stores each
1123: * header as ancillary data.
1124: * Note that the order of the headers remains in
1125: * the chain of ancillary data.
1126: */
1.35 itojun 1127: while (1) { /* is explicit loop prevention necessary? */
1.9 itojun 1128: struct ip6_ext *ip6e;
1129: int elen;
1130:
1131: #ifndef PULLDOWN_TEST
1132: ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off);
1133: if (nxt == IPPROTO_AH)
1134: elen = (ip6e->ip6e_len + 2) << 2;
1135: else
1136: elen = (ip6e->ip6e_len + 1) << 3;
1137: #else
1138: IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off,
1139: sizeof(struct ip6_ext));
1140: if (ip6e == NULL) {
1141: ip6stat.ip6s_tooshort++;
1142: return;
1143: }
1144: if (nxt == IPPROTO_AH)
1145: elen = (ip6e->ip6e_len + 2) << 2;
1146: else
1147: elen = (ip6e->ip6e_len + 1) << 3;
1148: IP6_EXTHDR_GET(ip6e, struct ip6_ext *, m, off, elen);
1149: if (ip6e == NULL) {
1150: ip6stat.ip6s_tooshort++;
1151: return;
1152: }
1153: #endif
1.2 itojun 1154:
1.35 itojun 1155: switch (nxt) {
1156: case IPPROTO_DSTOPTS:
1157: if (!in6p->in6p_flags & IN6P_DSTOPTS)
1158: break;
1159:
1160: /*
1161: * We also require super-user privilege for
1162: * the option.
1163: * See the comments on IN6_HOPOPTS.
1164: */
1165: if (!privileged)
1166: break;
1167:
1168: *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1169: IPV6_DSTOPTS,
1170: IPPROTO_IPV6);
1171: if (*mp)
1172: mp = &(*mp)->m_next;
1173: break;
1174:
1175: case IPPROTO_ROUTING:
1176: if (!in6p->in6p_flags & IN6P_RTHDR)
1177: break;
1178:
1179: *mp = sbcreatecontrol((caddr_t)ip6e, elen,
1180: IPV6_RTHDR,
1181: IPPROTO_IPV6);
1182: if (*mp)
1183: mp = &(*mp)->m_next;
1184: break;
1185:
1186: case IPPROTO_UDP:
1187: case IPPROTO_TCP:
1188: case IPPROTO_ICMPV6:
1189: default:
1190: /*
1191: * stop search if we encounter an upper
1192: * layer protocol headers.
1193: */
1194: goto loopend;
1195:
1196: case IPPROTO_HOPOPTS:
1197: case IPPROTO_AH: /* is it possible? */
1198: break;
1.2 itojun 1199: }
1200:
1201: /* proceed with the next header. */
1.9 itojun 1202: off += elen;
1.2 itojun 1203: nxt = ip6e->ip6e_nxt;
1204: }
1205: loopend:
1.36 cgd 1206: ;
1.2 itojun 1207: }
1.9 itojun 1208: if ((in6p->in6p_flags & IN6P_HOPOPTS) && privileged) {
1.2 itojun 1209: /* to be done */
1210: }
1.9 itojun 1211: if ((in6p->in6p_flags & IN6P_DSTOPTS) && privileged) {
1.2 itojun 1212: /* to be done */
1213: }
1214: /* IN6P_RTHDR - to be done */
1215:
1216: }
1217:
1218: /*
1219: * Get pointer to the previous header followed by the header
1220: * currently processed.
1221: * XXX: This function supposes that
1222: * M includes all headers,
1223: * the next header field and the header length field of each header
1224: * are valid, and
1225: * the sum of each header length equals to OFF.
1226: * Because of these assumptions, this function must be called very
1227: * carefully. Moreover, it will not be used in the near future when
1228: * we develop `neater' mechanism to process extension headers.
1229: */
1230: char *
1231: ip6_get_prevhdr(m, off)
1232: struct mbuf *m;
1233: int off;
1234: {
1235: struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1236:
1237: if (off == sizeof(struct ip6_hdr))
1238: return(&ip6->ip6_nxt);
1239: else {
1240: int len, nxt;
1241: struct ip6_ext *ip6e = NULL;
1242:
1243: nxt = ip6->ip6_nxt;
1244: len = sizeof(struct ip6_hdr);
1245: while (len < off) {
1246: ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len);
1247:
1.35 itojun 1248: switch (nxt) {
1.2 itojun 1249: case IPPROTO_FRAGMENT:
1250: len += sizeof(struct ip6_frag);
1251: break;
1252: case IPPROTO_AH:
1253: len += (ip6e->ip6e_len + 2) << 2;
1254: break;
1255: default:
1256: len += (ip6e->ip6e_len + 1) << 3;
1257: break;
1258: }
1259: nxt = ip6e->ip6e_nxt;
1260: }
1261: if (ip6e)
1262: return(&ip6e->ip6e_nxt);
1263: else
1264: return NULL;
1.18 itojun 1265: }
1266: }
1267:
1268: /*
1269: * get next header offset. m will be retained.
1270: */
1271: int
1272: ip6_nexthdr(m, off, proto, nxtp)
1273: struct mbuf *m;
1274: int off;
1275: int proto;
1276: int *nxtp;
1277: {
1278: struct ip6_hdr ip6;
1279: struct ip6_ext ip6e;
1280: struct ip6_frag fh;
1281:
1282: /* just in case */
1283: if (m == NULL)
1284: panic("ip6_nexthdr: m == NULL");
1285: if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1286: return -1;
1287:
1288: switch (proto) {
1289: case IPPROTO_IPV6:
1290: if (m->m_pkthdr.len < off + sizeof(ip6))
1291: return -1;
1292: m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6);
1293: if (nxtp)
1294: *nxtp = ip6.ip6_nxt;
1295: off += sizeof(ip6);
1296: return off;
1297:
1298: case IPPROTO_FRAGMENT:
1299: /*
1300: * terminate parsing if it is not the first fragment,
1301: * it does not make sense to parse through it.
1302: */
1303: if (m->m_pkthdr.len < off + sizeof(fh))
1304: return -1;
1305: m_copydata(m, off, sizeof(fh), (caddr_t)&fh);
1306: if ((ntohs(fh.ip6f_offlg) & IP6F_OFF_MASK) != 0)
1307: return -1;
1308: if (nxtp)
1309: *nxtp = fh.ip6f_nxt;
1310: off += sizeof(struct ip6_frag);
1311: return off;
1312:
1313: case IPPROTO_AH:
1314: if (m->m_pkthdr.len < off + sizeof(ip6e))
1315: return -1;
1316: m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1317: if (nxtp)
1318: *nxtp = ip6e.ip6e_nxt;
1319: off += (ip6e.ip6e_len + 2) << 2;
1.37.2.7 nathanw 1320: if (m->m_pkthdr.len < off)
1321: return -1;
1.18 itojun 1322: return off;
1323:
1324: case IPPROTO_HOPOPTS:
1325: case IPPROTO_ROUTING:
1326: case IPPROTO_DSTOPTS:
1327: if (m->m_pkthdr.len < off + sizeof(ip6e))
1328: return -1;
1329: m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e);
1330: if (nxtp)
1331: *nxtp = ip6e.ip6e_nxt;
1332: off += (ip6e.ip6e_len + 1) << 3;
1.37.2.7 nathanw 1333: if (m->m_pkthdr.len < off)
1334: return -1;
1.18 itojun 1335: return off;
1336:
1337: case IPPROTO_NONE:
1338: case IPPROTO_ESP:
1339: case IPPROTO_IPCOMP:
1340: /* give up */
1341: return -1;
1342:
1343: default:
1344: return -1;
1345: }
1346:
1347: return -1;
1348: }
1349:
1350: /*
1351: * get offset for the last header in the chain. m will be kept untainted.
1352: */
1353: int
1354: ip6_lasthdr(m, off, proto, nxtp)
1355: struct mbuf *m;
1356: int off;
1357: int proto;
1358: int *nxtp;
1359: {
1360: int newoff;
1361: int nxt;
1362:
1363: if (!nxtp) {
1364: nxt = -1;
1365: nxtp = &nxt;
1366: }
1367: while (1) {
1368: newoff = ip6_nexthdr(m, off, proto, nxtp);
1369: if (newoff < 0)
1370: return off;
1371: else if (newoff < off)
1372: return -1; /* invalid */
1373: else if (newoff == off)
1374: return newoff;
1375:
1376: off = newoff;
1377: proto = *nxtp;
1.2 itojun 1378: }
1379: }
1380:
1381: /*
1382: * System control for IP6
1383: */
1384:
1385: u_char inet6ctlerrmap[PRC_NCMDS] = {
1386: 0, 0, 0, 0,
1387: 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1388: EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1389: EMSGSIZE, EHOSTUNREACH, 0, 0,
1390: 0, 0, 0, 0,
1391: ENOPROTOOPT
1392: };
1393:
1394: int
1395: ip6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1396: int *name;
1397: u_int namelen;
1398: void *oldp;
1399: size_t *oldlenp;
1400: void *newp;
1401: size_t newlen;
1402: {
1.26 itojun 1403: int old, error;
1404:
1.2 itojun 1405: /* All sysctl names at this level are terminal. */
1406: if (namelen != 1)
1407: return ENOTDIR;
1408:
1409: switch (name[0]) {
1410:
1411: case IPV6CTL_FORWARDING:
1412: return sysctl_int(oldp, oldlenp, newp, newlen,
1413: &ip6_forwarding);
1414: case IPV6CTL_SENDREDIRECTS:
1415: return sysctl_int(oldp, oldlenp, newp, newlen,
1416: &ip6_sendredirects);
1417: case IPV6CTL_DEFHLIM:
1418: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_defhlim);
1419: case IPV6CTL_MAXFRAGPACKETS:
1420: return sysctl_int(oldp, oldlenp, newp, newlen,
1421: &ip6_maxfragpackets);
1422: case IPV6CTL_ACCEPT_RTADV:
1423: return sysctl_int(oldp, oldlenp, newp, newlen,
1424: &ip6_accept_rtadv);
1425: case IPV6CTL_KEEPFAITH:
1426: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_keepfaith);
1427: case IPV6CTL_LOG_INTERVAL:
1428: return sysctl_int(oldp, oldlenp, newp, newlen,
1429: &ip6_log_interval);
1430: case IPV6CTL_HDRNESTLIMIT:
1431: return sysctl_int(oldp, oldlenp, newp, newlen,
1432: &ip6_hdrnestlimit);
1433: case IPV6CTL_DAD_COUNT:
1434: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_dad_count);
1435: case IPV6CTL_AUTO_FLOWLABEL:
1436: return sysctl_int(oldp, oldlenp, newp, newlen,
1437: &ip6_auto_flowlabel);
1438: case IPV6CTL_DEFMCASTHLIM:
1439: return sysctl_int(oldp, oldlenp, newp, newlen,
1440: &ip6_defmcasthlim);
1.37.2.8 nathanw 1441: #if NGIF > 0
1.2 itojun 1442: case IPV6CTL_GIF_HLIM:
1443: return sysctl_int(oldp, oldlenp, newp, newlen,
1444: &ip6_gif_hlim);
1.37.2.8 nathanw 1445: #endif
1.2 itojun 1446: case IPV6CTL_KAME_VERSION:
1447: return sysctl_rdstring(oldp, oldlenp, newp, __KAME_VERSION);
1.9 itojun 1448: case IPV6CTL_USE_DEPRECATED:
1449: return sysctl_int(oldp, oldlenp, newp, newlen,
1450: &ip6_use_deprecated);
1.10 itojun 1451: case IPV6CTL_RR_PRUNE:
1452: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_rr_prune);
1.37.2.6 nathanw 1453: case IPV6CTL_V6ONLY:
1454: #ifdef INET6_BINDV6ONLY
1455: return sysctl_rdint(oldp, oldlenp, newp, ip6_v6only);
1456: #else
1457: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_v6only);
1.26 itojun 1458: #endif
1459: case IPV6CTL_ANONPORTMIN:
1460: old = ip6_anonportmin;
1461: error = sysctl_int(oldp, oldlenp, newp, newlen,
1462: &ip6_anonportmin);
1463: if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmin < 0 ||
1464: ip6_anonportmin > 65535
1465: #ifndef IPNOPRIVPORTS
1466: || ip6_anonportmin < IPV6PORT_RESERVED
1467: #endif
1468: ) {
1469: ip6_anonportmin = old;
1470: return (EINVAL);
1471: }
1472: return (error);
1473: case IPV6CTL_ANONPORTMAX:
1474: old = ip6_anonportmax;
1475: error = sysctl_int(oldp, oldlenp, newp, newlen,
1476: &ip6_anonportmax);
1477: if (ip6_anonportmin >= ip6_anonportmax || ip6_anonportmax < 0 ||
1478: ip6_anonportmax > 65535
1479: #ifndef IPNOPRIVPORTS
1480: || ip6_anonportmax < IPV6PORT_RESERVED
1481: #endif
1482: ) {
1483: ip6_anonportmax = old;
1484: return (EINVAL);
1485: }
1486: return (error);
1487: #ifndef IPNOPRIVPORTS
1488: case IPV6CTL_LOWPORTMIN:
1489: old = ip6_lowportmin;
1490: error = sysctl_int(oldp, oldlenp, newp, newlen,
1491: &ip6_lowportmin);
1492: if (ip6_lowportmin >= ip6_lowportmax ||
1493: ip6_lowportmin > IPV6PORT_RESERVEDMAX ||
1494: ip6_lowportmin < IPV6PORT_RESERVEDMIN) {
1495: ip6_lowportmin = old;
1496: return (EINVAL);
1497: }
1498: return (error);
1499: case IPV6CTL_LOWPORTMAX:
1500: old = ip6_lowportmax;
1501: error = sysctl_int(oldp, oldlenp, newp, newlen,
1502: &ip6_lowportmax);
1503: if (ip6_lowportmin >= ip6_lowportmax ||
1504: ip6_lowportmax > IPV6PORT_RESERVEDMAX ||
1505: ip6_lowportmax < IPV6PORT_RESERVEDMIN) {
1506: ip6_lowportmax = old;
1507: return (EINVAL);
1508: }
1509: return (error);
1.11 itojun 1510: #endif
1.37.2.9 nathanw 1511: case IPV6CTL_MAXFRAGS:
1512: return sysctl_int(oldp, oldlenp, newp, newlen, &ip6_maxfrags);
1.2 itojun 1513: default:
1514: return EOPNOTSUPP;
1515: }
1516: /* NOTREACHED */
1517: }
CVSweb <webmaster@jp.NetBSD.org>