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