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