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