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