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