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