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