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