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