Annotation of src/sys/netinet6/ip6_input.c, Revision 1.129
1.129 ! joerg 1: /* $NetBSD: ip6_input.c,v 1.128 2009/09/16 15:23:05 pooka Exp $ */
1.40 itojun 2: /* $KAME: ip6_input.c,v 1.188 2001/03/29 05:34:31 itojun Exp $ */
1.3 thorpej 3:
1.2 itojun 4: /*
5: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6: * All rights reserved.
1.21 itojun 7: *
1.2 itojun 8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. Neither the name of the project nor the names of its contributors
17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
1.21 itojun 19: *
1.2 itojun 20: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: /*
34: * Copyright (c) 1982, 1986, 1988, 1993
35: * The Regents of the University of California. All rights reserved.
36: *
37: * Redistribution and use in source and binary forms, with or without
38: * modification, are permitted provided that the following conditions
39: * are met:
40: * 1. Redistributions of source code must retain the above copyright
41: * notice, this list of conditions and the following disclaimer.
42: * 2. Redistributions in binary form must reproduce the above copyright
43: * notice, this list of conditions and the following disclaimer in the
44: * documentation and/or other materials provided with the distribution.
1.65 agc 45: * 3. Neither the name of the University nor the names of its contributors
1.2 itojun 46: * may be used to endorse or promote products derived from this software
47: * without specific prior written permission.
48: *
49: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59: * SUCH DAMAGE.
60: *
61: * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
62: */
1.48 lukem 63:
64: #include <sys/cdefs.h>
1.129 ! joerg 65: __KERNEL_RCSID(0, "$NetBSD: ip6_input.c,v 1.128 2009/09/16 15:23:05 pooka Exp $");
1.2 itojun 66:
1.129 ! joerg 67: #include "opt_gateway.h"
1.2 itojun 68: #include "opt_inet.h"
1.85 rpaulo 69: #include "opt_inet6.h"
1.4 thorpej 70: #include "opt_ipsec.h"
1.15 darrenr 71: #include "opt_pfil_hooks.h"
1.123 christos 72: #include "opt_compat_netbsd.h"
1.2 itojun 73:
74: #include <sys/param.h>
75: #include <sys/systm.h>
76: #include <sys/malloc.h>
77: #include <sys/mbuf.h>
78: #include <sys/domain.h>
79: #include <sys/protosw.h>
80: #include <sys/socket.h>
81: #include <sys/socketvar.h>
82: #include <sys/errno.h>
83: #include <sys/time.h>
84: #include <sys/kernel.h>
85: #include <sys/syslog.h>
86: #include <sys/proc.h>
1.46 simonb 87: #include <sys/sysctl.h>
1.2 itojun 88:
89: #include <net/if.h>
90: #include <net/if_types.h>
91: #include <net/if_dl.h>
92: #include <net/route.h>
93: #include <net/netisr.h>
1.15 darrenr 94: #ifdef PFIL_HOOKS
95: #include <net/pfil.h>
96: #endif
1.2 itojun 97:
98: #include <netinet/in.h>
1.9 itojun 99: #include <netinet/in_systm.h>
100: #ifdef INET
101: #include <netinet/ip.h>
102: #include <netinet/ip_icmp.h>
1.45 itojun 103: #endif /* INET */
1.14 itojun 104: #include <netinet/ip6.h>
1.2 itojun 105: #include <netinet6/in6_var.h>
1.11 itojun 106: #include <netinet6/ip6_var.h>
1.116 thorpej 107: #include <netinet6/ip6_private.h>
1.2 itojun 108: #include <netinet6/in6_pcb.h>
1.14 itojun 109: #include <netinet/icmp6.h>
1.81 rpaulo 110: #include <netinet6/scope6_var.h>
1.2 itojun 111: #include <netinet6/in6_ifattach.h>
112: #include <netinet6/nd6.h>
113:
1.37 itojun 114: #ifdef IPSEC
115: #include <netinet6/ipsec.h>
1.117 thorpej 116: #include <netinet6/ipsec_private.h>
1.37 itojun 117: #endif
118:
1.94 degroote 119: #ifdef FAST_IPSEC
120: #include <netipsec/ipsec.h>
121: #include <netipsec/ipsec6.h>
122: #include <netipsec/key.h>
123: #endif /* FAST_IPSEC */
124:
1.123 christos 125: #ifdef COMPAT_50
126: #include <compat/sys/time.h>
127: #include <compat/sys/socket.h>
128: #endif
129:
1.2 itojun 130: #include <netinet6/ip6protosw.h>
131:
132: #include "faith.h"
133: #include "gif.h"
1.50 itojun 134:
135: #if NGIF > 0
136: #include <netinet6/in6_gif.h>
137: #endif
1.2 itojun 138:
1.9 itojun 139: #include <net/net_osdep.h>
140:
1.2 itojun 141: extern struct domain inet6domain;
142:
143: u_char ip6_protox[IPPROTO_MAX];
144: static int ip6qmaxlen = IFQ_MAXLEN;
145: struct in6_ifaddr *in6_ifaddr;
146: struct ifqueue ip6intrq;
147:
1.108 ad 148: extern callout_t in6_tmpaddrtimer_ch;
1.83 rpaulo 149:
1.2 itojun 150: int ip6_forward_srcrt; /* XXX */
151: int ip6_sourcecheck; /* XXX */
152: int ip6_sourcecheck_interval; /* XXX */
1.9 itojun 153:
1.29 thorpej 154: #ifdef PFIL_HOOKS
155: struct pfil_head inet6_pfil_hook;
156: #endif
157:
1.116 thorpej 158: percpu_t *ip6stat_percpu;
1.2 itojun 159:
1.111 dyoung 160: static void ip6_init2(void *);
1.112 dyoung 161: static struct m_tag *ip6_setdstifaddr(struct mbuf *, const struct in6_ifaddr *);
1.2 itojun 162:
1.111 dyoung 163: static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *);
164: static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int);
1.128 pooka 165: static void sysctl_net_inet6_ip6_setup(struct sysctllog **);
1.2 itojun 166:
167: /*
168: * IP6 initialization: fill in IP6 protocol switch table.
169: * All protocols not implemented in kernel go to raw IP6 protocol handler.
170: */
171: void
1.114 matt 172: ip6_init(void)
1.2 itojun 173: {
1.78 christos 174: const struct ip6protosw *pr;
1.35 itojun 175: int i;
1.2 itojun 176:
1.128 pooka 177: sysctl_net_inet6_ip6_setup(NULL);
1.78 christos 178: pr = (const struct ip6protosw *)pffindproto(PF_INET6, IPPROTO_RAW, SOCK_RAW);
1.2 itojun 179: if (pr == 0)
180: panic("ip6_init");
181: for (i = 0; i < IPPROTO_MAX; i++)
182: ip6_protox[i] = pr - inet6sw;
1.78 christos 183: for (pr = (const struct ip6protosw *)inet6domain.dom_protosw;
184: pr < (const struct ip6protosw *)inet6domain.dom_protoswNPROTOSW; pr++)
1.2 itojun 185: if (pr->pr_domain->dom_family == PF_INET6 &&
186: pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
187: ip6_protox[pr->pr_protocol] = pr - inet6sw;
188: ip6intrq.ifq_maxlen = ip6qmaxlen;
1.81 rpaulo 189: scope6_init();
190: addrsel_policy_init();
1.2 itojun 191: nd6_init();
192: frag6_init();
1.83 rpaulo 193: ip6_desync_factor = arc4random() % MAX_TEMP_DESYNC_FACTOR;
1.2 itojun 194:
195: ip6_init2((void *)0);
1.98 liamjfoy 196: #ifdef GATEWAY
1.99 liamjfoy 197: ip6flow_init(ip6_hashsize);
1.98 liamjfoy 198: #endif
1.29 thorpej 199:
200: #ifdef PFIL_HOOKS
201: /* Register our Packet Filter hook. */
1.33 thorpej 202: inet6_pfil_hook.ph_type = PFIL_TYPE_AF;
203: inet6_pfil_hook.ph_af = AF_INET6;
1.29 thorpej 204: i = pfil_head_register(&inet6_pfil_hook);
205: if (i != 0)
206: printf("ip6_init: WARNING: unable to register pfil hook, "
207: "error %d\n", i);
208: #endif /* PFIL_HOOKS */
1.116 thorpej 209:
210: ip6stat_percpu = percpu_alloc(sizeof(uint64_t) * IP6_NSTATS);
1.2 itojun 211: }
212:
213: static void
1.90 christos 214: ip6_init2(void *dummy)
1.2 itojun 215: {
216:
217: /* nd6_timer_init */
1.118 ad 218: callout_init(&nd6_timer_ch, CALLOUT_MPSAFE);
1.19 thorpej 219: callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
1.83 rpaulo 220:
221: /* timer for regeneranation of temporary addresses randomize ID */
1.118 ad 222: callout_init(&in6_tmpaddrtimer_ch, CALLOUT_MPSAFE);
1.83 rpaulo 223: callout_reset(&in6_tmpaddrtimer_ch,
224: (ip6_temp_preferred_lifetime - ip6_desync_factor -
225: ip6_temp_regen_advance) * hz,
226: in6_tmpaddrtimer, NULL);
1.2 itojun 227: }
228:
229: /*
230: * IP6 input interrupt handling. Just pass the packet to ip6_input.
231: */
232: void
1.114 matt 233: ip6intr(void)
1.2 itojun 234: {
235: int s;
236: struct mbuf *m;
237:
1.118 ad 238: mutex_enter(softnet_lock);
239: KERNEL_LOCK(1, NULL);
1.2 itojun 240: for (;;) {
1.41 thorpej 241: s = splnet();
1.2 itojun 242: IF_DEQUEUE(&ip6intrq, m);
243: splx(s);
244: if (m == 0)
1.118 ad 245: break;
1.83 rpaulo 246: /* drop the packet if IPv6 operation is disabled on the IF */
247: if ((ND_IFINFO(m->m_pkthdr.rcvif)->flags & ND6_IFF_IFDISABLED)) {
248: m_freem(m);
1.118 ad 249: break;
1.83 rpaulo 250: }
1.2 itojun 251: ip6_input(m);
252: }
1.118 ad 253: KERNEL_UNLOCK_ONE(NULL);
254: mutex_exit(softnet_lock);
1.2 itojun 255: }
256:
1.103 dyoung 257: extern struct route ip6_forward_rt;
1.2 itojun 258:
259: void
1.96 dyoung 260: ip6_input(struct mbuf *m)
1.2 itojun 261: {
1.9 itojun 262: struct ip6_hdr *ip6;
1.105 dyoung 263: int hit, off = sizeof(struct ip6_hdr), nest;
1.2 itojun 264: u_int32_t plen;
1.5 itojun 265: u_int32_t rtalert = ~0;
1.104 yamt 266: int nxt, ours = 0, rh_present = 0;
1.9 itojun 267: struct ifnet *deliverifp = NULL;
1.64 itojun 268: int srcrt = 0;
1.105 dyoung 269: const struct rtentry *rt;
270: union {
271: struct sockaddr dst;
272: struct sockaddr_in6 dst6;
273: } u;
1.94 degroote 274: #ifdef FAST_IPSEC
275: struct m_tag *mtag;
276: struct tdb_ident *tdbi;
277: struct secpolicy *sp;
278: int s, error;
279: #endif
1.2 itojun 280:
281: #ifdef IPSEC
282: /*
283: * should the inner packet be considered authentic?
284: * see comment in ah4_input().
285: */
1.49 itojun 286: m->m_flags &= ~M_AUTHIPHDR;
287: m->m_flags &= ~M_AUTHIPDGM;
1.2 itojun 288: #endif
1.9 itojun 289:
1.2 itojun 290: /*
1.81 rpaulo 291: * make sure we don't have onion peering information into m_tag.
292: */
293: ip6_delaux(m);
294:
295: /*
1.44 itojun 296: * mbuf statistics
1.2 itojun 297: */
298: if (m->m_flags & M_EXT) {
299: if (m->m_next)
1.116 thorpej 300: IP6_STATINC(IP6_STAT_MEXT2M);
1.2 itojun 301: else
1.116 thorpej 302: IP6_STATINC(IP6_STAT_MEXT1);
1.2 itojun 303: } else {
1.115 thorpej 304: #define M2MMAX 32
1.2 itojun 305: if (m->m_next) {
1.9 itojun 306: if (m->m_flags & M_LOOP) {
1.116 thorpej 307: /*XXX*/ IP6_STATINC(IP6_STAT_M2M + lo0ifp->if_index);
1.115 thorpej 308: } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) {
1.116 thorpej 309: IP6_STATINC(IP6_STAT_M2M +
310: m->m_pkthdr.rcvif->if_index);
1.115 thorpej 311: } else
1.116 thorpej 312: IP6_STATINC(IP6_STAT_M2M);
1.2 itojun 313: } else
1.116 thorpej 314: IP6_STATINC(IP6_STAT_M1);
1.40 itojun 315: #undef M2MMAX
1.2 itojun 316: }
317:
1.9 itojun 318: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive);
1.116 thorpej 319: IP6_STATINC(IP6_STAT_TOTAL);
1.9 itojun 320:
1.57 thorpej 321: /*
322: * If the IPv6 header is not aligned, slurp it up into a new
323: * mbuf with space for link headers, in the event we forward
1.81 rpaulo 324: * it. Otherwise, if it is aligned, make sure the entire base
1.57 thorpej 325: * IPv6 header is in the first mbuf of the chain.
326: */
1.97 christos 327: if (IP6_HDR_ALIGNED_P(mtod(m, void *)) == 0) {
1.57 thorpej 328: struct ifnet *inifp = m->m_pkthdr.rcvif;
329: if ((m = m_copyup(m, sizeof(struct ip6_hdr),
330: (max_linkhdr + 3) & ~3)) == NULL) {
331: /* XXXJRT new stat, please */
1.116 thorpej 332: IP6_STATINC(IP6_STAT_TOOSMALL);
1.57 thorpej 333: in6_ifstat_inc(inifp, ifs6_in_hdrerr);
334: return;
335: }
336: } else if (__predict_false(m->m_len < sizeof(struct ip6_hdr))) {
337: struct ifnet *inifp = m->m_pkthdr.rcvif;
1.55 itojun 338: if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
1.116 thorpej 339: IP6_STATINC(IP6_STAT_TOOSMALL);
1.9 itojun 340: in6_ifstat_inc(inifp, ifs6_in_hdrerr);
341: return;
342: }
1.2 itojun 343: }
344:
345: ip6 = mtod(m, struct ip6_hdr *);
346:
347: if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
1.116 thorpej 348: IP6_STATINC(IP6_STAT_BADVERS);
1.9 itojun 349: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
1.2 itojun 350: goto bad;
351: }
1.15 darrenr 352:
1.98 liamjfoy 353: #if defined(IPSEC)
354: /* IPv6 fast forwarding is not compatible with IPsec. */
355: m->m_flags &= ~M_CANFASTFWD;
356: #else
357: /*
358: * Assume that we can create a fast-forward IP flow entry
359: * based on this packet.
360: */
361: m->m_flags |= M_CANFASTFWD;
362: #endif
363:
1.15 darrenr 364: #ifdef PFIL_HOOKS
365: /*
366: * Run through list of hooks for input packets. If there are any
367: * filters which require that additional packets in the flow are
368: * not fast-forwarded, they must clear the M_CANFASTFWD flag.
369: * Note that filters must _never_ set this flag, as another filter
370: * in the list may have previously cleared it.
371: */
1.39 itojun 372: /*
373: * let ipfilter look at packet on the wire,
374: * not the decapsulated packet.
375: */
376: #ifdef IPSEC
1.42 itojun 377: if (!ipsec_getnhist(m))
1.94 degroote 378: #elif defined(FAST_IPSEC)
379: if (!ipsec_indone(m))
1.39 itojun 380: #else
381: if (1)
382: #endif
383: {
1.64 itojun 384: struct in6_addr odst;
385:
386: odst = ip6->ip6_dst;
1.39 itojun 387: if (pfil_run_hooks(&inet6_pfil_hook, &m, m->m_pkthdr.rcvif,
388: PFIL_IN) != 0)
389: return;
390: if (m == NULL)
391: return;
392: ip6 = mtod(m, struct ip6_hdr *);
1.64 itojun 393: srcrt = !IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst);
1.39 itojun 394: }
1.15 darrenr 395: #endif /* PFIL_HOOKS */
396:
1.116 thorpej 397: IP6_STATINC(IP6_STAT_NXTHIST + ip6->ip6_nxt);
1.2 itojun 398:
1.30 thorpej 399: #ifdef ALTQ
400: if (altq_input != NULL && (*altq_input)(m, AF_INET6) == 0) {
401: /* packet is dropped by traffic conditioner */
402: return;
1.9 itojun 403: }
404: #endif
405:
1.2 itojun 406: /*
1.44 itojun 407: * Check against address spoofing/corruption.
1.2 itojun 408: */
409: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) ||
410: IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) {
1.55 itojun 411: /*
412: * XXX: "badscope" is not very suitable for a multicast source.
413: */
1.116 thorpej 414: IP6_STATINC(IP6_STAT_BADSCOPE);
1.9 itojun 415: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
1.2 itojun 416: goto bad;
417: }
1.13 itojun 418: /*
1.44 itojun 419: * The following check is not documented in specs. A malicious
420: * party may be able to use IPv4 mapped addr to confuse tcp/udp stack
421: * and bypass security checks (act as if it was from 127.0.0.1 by using
422: * IPv6 src ::ffff:127.0.0.1). Be cautious.
1.35 itojun 423: *
1.44 itojun 424: * This check chokes if we are in an SIIT cloud. As none of BSDs
425: * support IPv4-less kernel compilation, we cannot support SIIT
426: * environment at all. So, it makes more sense for us to reject any
427: * malicious packets for non-SIIT environment, than try to do a
1.52 wiz 428: * partial support for SIIT environment.
1.13 itojun 429: */
430: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
431: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
1.116 thorpej 432: IP6_STATINC(IP6_STAT_BADSCOPE);
1.13 itojun 433: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
434: goto bad;
435: }
1.17 itojun 436: #if 0
1.13 itojun 437: /*
1.17 itojun 438: * Reject packets with IPv4 compatible addresses (auto tunnel).
439: *
440: * The code forbids auto tunnel relay case in RFC1933 (the check is
441: * stronger than RFC1933). We may want to re-enable it if mech-xx
442: * is revised to forbid relaying case.
1.13 itojun 443: */
444: if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) ||
445: IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) {
1.116 thorpej 446: IP6_STATINC(IP6_STAT_BADSCOPE);
1.13 itojun 447: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
448: goto bad;
449: }
450: #endif
1.35 itojun 451:
1.2 itojun 452: /*
1.82 yamt 453: * Disambiguate address scope zones (if there is ambiguity).
454: * We first make sure that the original source or destination address
455: * is not in our internal form for scoped addresses. Such addresses
456: * are not necessarily invalid spec-wise, but we cannot accept them due
457: * to the usage conflict.
458: * in6_setscope() then also checks and rejects the cases where src or
459: * dst are the loopback address and the receiving interface
460: * is not loopback.
461: */
1.88 bouyer 462: if (__predict_false(
463: m_makewritable(&m, 0, sizeof(struct ip6_hdr), M_DONTWAIT)))
464: goto bad;
465: ip6 = mtod(m, struct ip6_hdr *);
1.82 yamt 466: if (in6_clearscope(&ip6->ip6_src) || in6_clearscope(&ip6->ip6_dst)) {
1.116 thorpej 467: IP6_STATINC(IP6_STAT_BADSCOPE); /* XXX */
1.82 yamt 468: goto bad;
469: }
470: if (in6_setscope(&ip6->ip6_src, m->m_pkthdr.rcvif, NULL) ||
471: in6_setscope(&ip6->ip6_dst, m->m_pkthdr.rcvif, NULL)) {
1.116 thorpej 472: IP6_STATINC(IP6_STAT_BADSCOPE);
1.82 yamt 473: goto bad;
474: }
475:
476: /*
1.2 itojun 477: * Multicast check
478: */
479: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
480: struct in6_multi *in6m = 0;
1.9 itojun 481:
482: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast);
1.2 itojun 483: /*
484: * See if we belong to the destination multicast group on the
485: * arrival interface.
486: */
487: IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m);
488: if (in6m)
489: ours = 1;
490: else if (!ip6_mrouter) {
1.116 thorpej 491: uint64_t *ip6s = IP6_STAT_GETREF();
492: ip6s[IP6_STAT_NOTMEMBER]++;
493: ip6s[IP6_STAT_CANTFORWARD]++;
494: IP6_STAT_PUTREF();
1.9 itojun 495: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2 itojun 496: goto bad;
497: }
1.9 itojun 498: deliverifp = m->m_pkthdr.rcvif;
1.2 itojun 499: goto hbhcheck;
500: }
501:
1.105 dyoung 502: sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
503:
1.2 itojun 504: /*
505: * Unicast check
506: */
1.105 dyoung 507: rt = rtcache_lookup2(&ip6_forward_rt, &u.dst, 1, &hit);
508: if (hit)
1.116 thorpej 509: IP6_STATINC(IP6_STAT_FORWARD_CACHEHIT);
1.93 joerg 510: else
1.116 thorpej 511: IP6_STATINC(IP6_STAT_FORWARD_CACHEMISS);
1.22 itojun 512:
1.109 dyoung 513: #define rt6_getkey(__rt) satocsin6(rt_getkey(__rt))
1.2 itojun 514:
515: /*
516: * Accept the packet if the forwarding interface to the destination
517: * according to the routing table is the loopback interface,
518: * unless the associated route has a gateway.
519: * Note that this approach causes to accept a packet if there is a
520: * route to the loopback interface for the destination of the packet.
521: * But we think it's even useful in some situations, e.g. when using
522: * a special daemon which wants to intercept the packet.
523: */
1.105 dyoung 524: if (rt != NULL &&
525: (rt->rt_flags & (RTF_HOST|RTF_GATEWAY)) == RTF_HOST &&
526: !(rt->rt_flags & RTF_CLONED) &&
1.2 itojun 527: #if 0
528: /*
529: * The check below is redundant since the comparison of
530: * the destination and the key of the rtentry has
531: * already done through looking up the routing table.
532: */
1.109 dyoung 533: IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &rt6_getkey(rt)->sin6_addr) &&
1.2 itojun 534: #endif
1.105 dyoung 535: rt->rt_ifp->if_type == IFT_LOOP) {
536: struct in6_ifaddr *ia6 = (struct in6_ifaddr *)rt->rt_ifa;
1.2 itojun 537: if (ia6->ia6_flags & IN6_IFF_ANYCAST)
538: m->m_flags |= M_ANYCAST6;
1.24 itojun 539: /*
540: * packets to a tentative, duplicated, or somehow invalid
541: * address must not be accepted.
542: */
1.2 itojun 543: if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) {
1.24 itojun 544: /* this address is ready */
1.2 itojun 545: ours = 1;
1.9 itojun 546: deliverifp = ia6->ia_ifp; /* correct? */
1.2 itojun 547: goto hbhcheck;
548: } else {
1.24 itojun 549: /* address is not ready, so discard the packet. */
1.34 itojun 550: nd6log((LOG_INFO,
1.27 itojun 551: "ip6_input: packet to an unready address %s->%s\n",
1.24 itojun 552: ip6_sprintf(&ip6->ip6_src),
1.34 itojun 553: ip6_sprintf(&ip6->ip6_dst)));
1.24 itojun 554:
555: goto bad;
1.2 itojun 556: }
557: }
558:
559: /*
1.55 itojun 560: * FAITH (Firewall Aided Internet Translator)
1.2 itojun 561: */
562: #if defined(NFAITH) && 0 < NFAITH
563: if (ip6_keepfaith) {
1.105 dyoung 564: if (rt != NULL && rt->rt_ifp != NULL &&
565: rt->rt_ifp->if_type == IFT_FAITH) {
1.2 itojun 566: /* XXX do we need more sanity checks? */
567: ours = 1;
1.105 dyoung 568: deliverifp = rt->rt_ifp; /* faith */
1.9 itojun 569: goto hbhcheck;
570: }
571: }
572: #endif
573:
574: #if 0
575: {
576: /*
577: * Last resort: check in6_ifaddr for incoming interface.
578: * The code is here until I update the "goto ours hack" code above
579: * working right.
580: */
581: struct ifaddr *ifa;
1.113 dyoung 582: IFADDR_FOREACH(ifa, m->m_pkthdr.rcvif) {
1.9 itojun 583: if (ifa->ifa_addr == NULL)
584: continue; /* just for safety */
585: if (ifa->ifa_addr->sa_family != AF_INET6)
586: continue;
587: if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ip6->ip6_dst)) {
588: ours = 1;
589: deliverifp = ifa->ifa_ifp;
1.2 itojun 590: goto hbhcheck;
591: }
592: }
1.9 itojun 593: }
1.2 itojun 594: #endif
595:
596: /*
597: * Now there is no reason to process the packet if it's not our own
598: * and we're not a router.
599: */
600: if (!ip6_forwarding) {
1.116 thorpej 601: IP6_STATINC(IP6_STAT_CANTFORWARD);
1.9 itojun 602: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
1.2 itojun 603: goto bad;
604: }
605:
606: hbhcheck:
607: /*
1.81 rpaulo 608: * record address information into m_tag, if we don't have one yet.
609: * note that we are unable to record it, if the address is not listed
610: * as our interface address (e.g. multicast addresses, addresses
611: * within FAITH prefixes and such).
612: */
1.111 dyoung 613: if (deliverifp && ip6_getdstifaddr(m) == NULL) {
1.81 rpaulo 614: struct in6_ifaddr *ia6;
615:
616: ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
1.111 dyoung 617: if (ia6 != NULL && ip6_setdstifaddr(m, ia6) == NULL) {
618: /*
619: * XXX maybe we should drop the packet here,
620: * as we could not provide enough information
621: * to the upper layers.
622: */
1.81 rpaulo 623: }
624: }
625:
626: /*
1.2 itojun 627: * Process Hop-by-Hop options header if it's contained.
628: * m may be modified in ip6_hopopts_input().
629: * If a JumboPayload option is included, plen will also be modified.
630: */
631: plen = (u_int32_t)ntohs(ip6->ip6_plen);
632: if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9 itojun 633: struct ip6_hbh *hbh;
634:
635: if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) {
636: #if 0 /*touches NULL pointer*/
637: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
638: #endif
1.2 itojun 639: return; /* m have already been freed */
1.9 itojun 640: }
1.22 itojun 641:
1.2 itojun 642: /* adjust pointer */
643: ip6 = mtod(m, struct ip6_hdr *);
1.22 itojun 644:
645: /*
1.49 itojun 646: * if the payload length field is 0 and the next header field
1.22 itojun 647: * indicates Hop-by-Hop Options header, then a Jumbo Payload
648: * option MUST be included.
649: */
650: if (ip6->ip6_plen == 0 && plen == 0) {
651: /*
652: * Note that if a valid jumbo payload option is
1.83 rpaulo 653: * contained, ip6_hopopts_input() must set a valid
1.55 itojun 654: * (non-zero) payload length to the variable plen.
1.22 itojun 655: */
1.116 thorpej 656: IP6_STATINC(IP6_STAT_BADOPTIONS);
1.22 itojun 657: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard);
658: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
659: icmp6_error(m, ICMP6_PARAM_PROB,
660: ICMP6_PARAMPROB_HEADER,
1.97 christos 661: (char *)&ip6->ip6_plen - (char *)ip6);
1.22 itojun 662: return;
663: }
1.9 itojun 664: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
665: sizeof(struct ip6_hbh));
666: if (hbh == NULL) {
1.116 thorpej 667: IP6_STATINC(IP6_STAT_TOOSHORT);
1.9 itojun 668: return;
669: }
1.57 thorpej 670: KASSERT(IP6_HDR_ALIGNED_P(hbh));
1.9 itojun 671: nxt = hbh->ip6h_nxt;
1.2 itojun 672:
673: /*
674: * accept the packet if a router alert option is included
675: * and we act as an IPv6 router.
676: */
1.5 itojun 677: if (rtalert != ~0 && ip6_forwarding)
1.2 itojun 678: ours = 1;
679: } else
680: nxt = ip6->ip6_nxt;
681:
682: /*
683: * Check that the amount of data in the buffers
684: * is as at least much as the IPv6 header would have us expect.
685: * Trim mbufs if longer than we expect.
686: * Drop packet if shorter than we expect.
687: */
688: if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
1.116 thorpej 689: IP6_STATINC(IP6_STAT_TOOSHORT);
1.9 itojun 690: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.2 itojun 691: goto bad;
692: }
693: if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
694: if (m->m_len == m->m_pkthdr.len) {
695: m->m_len = sizeof(struct ip6_hdr) + plen;
696: m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
697: } else
698: m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len);
699: }
700:
701: /*
702: * Forward if desirable.
703: */
704: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
705: /*
706: * If we are acting as a multicast router, all
707: * incoming multicast packets are passed to the
708: * kernel-level multicast forwarding function.
709: * The packet is returned (relatively) intact; if
710: * ip6_mforward() returns a non-zero value, the packet
711: * must be discarded, else it may be accepted below.
712: */
713: if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) {
1.116 thorpej 714: IP6_STATINC(IP6_STAT_CANTFORWARD);
1.2 itojun 715: m_freem(m);
716: return;
717: }
718: if (!ours) {
719: m_freem(m);
720: return;
721: }
1.22 itojun 722: } else if (!ours) {
1.64 itojun 723: ip6_forward(m, srcrt);
1.2 itojun 724: return;
1.56 itojun 725: }
1.25 itojun 726:
727: ip6 = mtod(m, struct ip6_hdr *);
728:
729: /*
730: * Malicious party may be able to use IPv4 mapped addr to confuse
731: * tcp/udp stack and bypass security checks (act as if it was from
732: * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious.
733: *
734: * For SIIT end node behavior, you may want to disable the check.
735: * However, you will become vulnerable to attacks using IPv4 mapped
736: * source.
737: */
738: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
739: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
1.116 thorpej 740: IP6_STATINC(IP6_STAT_BADSCOPE);
1.25 itojun 741: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr);
742: goto bad;
743: }
1.2 itojun 744:
745: /*
746: * Tell launch routine the next header
747: */
1.12 itojun 748: #ifdef IFA_STATS
1.28 itojun 749: if (deliverifp != NULL) {
1.9 itojun 750: struct in6_ifaddr *ia6;
751: ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst);
752: if (ia6)
753: ia6->ia_ifa.ifa_data.ifad_inbytes += m->m_pkthdr.len;
754: }
755: #endif
1.116 thorpej 756: IP6_STATINC(IP6_STAT_DELIVERED);
1.9 itojun 757: in6_ifstat_inc(deliverifp, ifs6_in_deliver);
1.2 itojun 758: nest = 0;
1.40 itojun 759:
1.104 yamt 760: rh_present = 0;
1.2 itojun 761: while (nxt != IPPROTO_DONE) {
762: if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
1.116 thorpej 763: IP6_STATINC(IP6_STAT_TOOMANYHDR);
1.104 yamt 764: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr);
1.2 itojun 765: goto bad;
766: }
1.8 itojun 767:
768: /*
769: * protection against faulty packet - there should be
770: * more sanity checks in header chain processing.
771: */
772: if (m->m_pkthdr.len < off) {
1.116 thorpej 773: IP6_STATINC(IP6_STAT_TOOSHORT);
1.9 itojun 774: in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated);
1.8 itojun 775: goto bad;
776: }
777:
1.104 yamt 778: if (nxt == IPPROTO_ROUTING) {
779: if (rh_present++) {
780: in6_ifstat_inc(m->m_pkthdr.rcvif,
781: ifs6_in_hdrerr);
1.116 thorpej 782: IP6_STATINC(IP6_STAT_BADOPTIONS);
1.104 yamt 783: goto bad;
784: }
785: }
786:
1.37 itojun 787: #ifdef IPSEC
788: /*
789: * enforce IPsec policy checking if we are seeing last header.
790: * note that we do not visit this with protocols with pcb layer
791: * code - like udp/tcp/raw ip.
792: */
793: if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
794: ipsec6_in_reject(m, NULL)) {
1.117 thorpej 795: IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
1.37 itojun 796: goto bad;
797: }
798: #endif
1.94 degroote 799: #ifdef FAST_IPSEC
800: /*
801: * enforce IPsec policy checking if we are seeing last header.
802: * note that we do not visit this with protocols with pcb layer
803: * code - like udp/tcp/raw ip.
804: */
805: if ((inet6sw[ip_protox[nxt]].pr_flags & PR_LASTHDR) != 0) {
806: /*
807: * Check if the packet has already had IPsec processing
808: * done. If so, then just pass it along. This tag gets
809: * set during AH, ESP, etc. input handling, before the
810: * packet is returned to the ip input queue for delivery.
811: */
812: mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
813: s = splsoftnet();
814: if (mtag != NULL) {
815: tdbi = (struct tdb_ident *)(mtag + 1);
816: sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
817: } else {
818: sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
819: IP_FORWARDING, &error);
820: }
821: if (sp != NULL) {
822: /*
823: * Check security policy against packet attributes.
824: */
825: error = ipsec_in_reject(sp, m);
826: KEY_FREESP(&sp);
827: } else {
828: /* XXX error stat??? */
829: error = EINVAL;
830: DPRINTF(("ip6_input: no SP, packet discarded\n"));/*XXX*/
831: }
832: splx(s);
833: if (error)
834: goto bad;
835: }
836: #endif /* FAST_IPSEC */
837:
1.56 itojun 838:
1.2 itojun 839: nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &off, nxt);
840: }
841: return;
842: bad:
843: m_freem(m);
844: }
845:
846: /*
1.81 rpaulo 847: * set/grab in6_ifaddr correspond to IPv6 destination address.
848: */
849: static struct m_tag *
1.112 dyoung 850: ip6_setdstifaddr(struct mbuf *m, const struct in6_ifaddr *ia)
1.81 rpaulo 851: {
852: struct m_tag *mtag;
853:
854: mtag = ip6_addaux(m);
1.112 dyoung 855: if (mtag != NULL) {
856: struct ip6aux *ip6a;
857:
858: ip6a = (struct ip6aux *)(mtag + 1);
859: in6_setscope(&ip6a->ip6a_src, ia->ia_ifp, &ip6a->ip6a_scope_id);
860: ip6a->ip6a_src = ia->ia_addr.sin6_addr;
861: ip6a->ip6a_flags = ia->ia6_flags;
862: }
1.81 rpaulo 863: return mtag; /* NULL if failed to set */
864: }
865:
1.112 dyoung 866: const struct ip6aux *
1.107 christos 867: ip6_getdstifaddr(struct mbuf *m)
1.81 rpaulo 868: {
869: struct m_tag *mtag;
870:
871: mtag = ip6_findaux(m);
1.112 dyoung 872: if (mtag != NULL)
873: return (struct ip6aux *)(mtag + 1);
1.81 rpaulo 874: else
875: return NULL;
876: }
877:
878: /*
1.2 itojun 879: * Hop-by-Hop options header processing. If a valid jumbo payload option is
880: * included, the real payload length will be stored in plenp.
1.107 christos 881: *
882: * rtalertp - XXX: should be stored more smart way
1.2 itojun 883: */
884: static int
1.107 christos 885: ip6_hopopts_input(u_int32_t *plenp, u_int32_t *rtalertp,
886: struct mbuf **mp, int *offp)
1.2 itojun 887: {
1.35 itojun 888: struct mbuf *m = *mp;
1.2 itojun 889: int off = *offp, hbhlen;
890: struct ip6_hbh *hbh;
891:
892: /* validation of the length of the header */
1.9 itojun 893: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m,
894: sizeof(struct ip6_hdr), sizeof(struct ip6_hbh));
895: if (hbh == NULL) {
1.116 thorpej 896: IP6_STATINC(IP6_STAT_TOOSHORT);
1.9 itojun 897: return -1;
898: }
899: hbhlen = (hbh->ip6h_len + 1) << 3;
900: IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr),
901: hbhlen);
902: if (hbh == NULL) {
1.116 thorpej 903: IP6_STATINC(IP6_STAT_TOOSHORT);
1.9 itojun 904: return -1;
905: }
1.57 thorpej 906: KASSERT(IP6_HDR_ALIGNED_P(hbh));
1.2 itojun 907: off += hbhlen;
908: hbhlen -= sizeof(struct ip6_hbh);
909:
910: if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh),
911: hbhlen, rtalertp, plenp) < 0)
1.58 itojun 912: return (-1);
1.2 itojun 913:
914: *offp = off;
915: *mp = m;
1.58 itojun 916: return (0);
1.2 itojun 917: }
918:
919: /*
920: * Search header for all Hop-by-hop options and process each option.
921: * This function is separate from ip6_hopopts_input() in order to
922: * handle a case where the sending node itself process its hop-by-hop
923: * options header. In such a case, the function is called from ip6_output().
1.55 itojun 924: *
925: * The function assumes that hbh header is located right after the IPv6 header
926: * (RFC2460 p7), opthead is pointer into data content in m, and opthead to
927: * opthead + hbhlen is located in continuous memory region.
1.2 itojun 928: */
929: int
1.107 christos 930: ip6_process_hopopts(struct mbuf *m, u_int8_t *opthead, int hbhlen,
931: u_int32_t *rtalertp, u_int32_t *plenp)
1.2 itojun 932: {
933: struct ip6_hdr *ip6;
934: int optlen = 0;
935: u_int8_t *opt = opthead;
936: u_int16_t rtalert_val;
1.22 itojun 937: u_int32_t jumboplen;
1.55 itojun 938: const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh);
1.2 itojun 939:
940: for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) {
1.35 itojun 941: switch (*opt) {
942: case IP6OPT_PAD1:
943: optlen = 1;
944: break;
945: case IP6OPT_PADN:
946: if (hbhlen < IP6OPT_MINLEN) {
1.116 thorpej 947: IP6_STATINC(IP6_STAT_TOOSMALL);
1.35 itojun 948: goto bad;
949: }
950: optlen = *(opt + 1) + 2;
951: break;
952: case IP6OPT_RTALERT:
953: /* XXX may need check for alignment */
954: if (hbhlen < IP6OPT_RTALERT_LEN) {
1.116 thorpej 955: IP6_STATINC(IP6_STAT_TOOSMALL);
1.35 itojun 956: goto bad;
957: }
958: if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) {
1.55 itojun 959: /* XXX stat */
960: icmp6_error(m, ICMP6_PARAM_PROB,
961: ICMP6_PARAMPROB_HEADER,
962: erroff + opt + 1 - opthead);
963: return (-1);
1.35 itojun 964: }
965: optlen = IP6OPT_RTALERT_LEN;
1.126 tsutsui 966: memcpy((void *)&rtalert_val, (void *)(opt + 2), 2);
1.35 itojun 967: *rtalertp = ntohs(rtalert_val);
968: break;
969: case IP6OPT_JUMBO:
1.22 itojun 970: /* XXX may need check for alignment */
971: if (hbhlen < IP6OPT_JUMBO_LEN) {
1.116 thorpej 972: IP6_STATINC(IP6_STAT_TOOSMALL);
1.22 itojun 973: goto bad;
974: }
1.35 itojun 975: if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) {
1.55 itojun 976: /* XXX stat */
977: icmp6_error(m, ICMP6_PARAM_PROB,
978: ICMP6_PARAMPROB_HEADER,
979: erroff + opt + 1 - opthead);
980: return (-1);
1.35 itojun 981: }
1.22 itojun 982: optlen = IP6OPT_JUMBO_LEN;
983:
984: /*
985: * IPv6 packets that have non 0 payload length
1.35 itojun 986: * must not contain a jumbo payload option.
1.22 itojun 987: */
988: ip6 = mtod(m, struct ip6_hdr *);
989: if (ip6->ip6_plen) {
1.116 thorpej 990: IP6_STATINC(IP6_STAT_BADOPTIONS);
1.22 itojun 991: icmp6_error(m, ICMP6_PARAM_PROB,
1.55 itojun 992: ICMP6_PARAMPROB_HEADER,
993: erroff + opt - opthead);
994: return (-1);
1.22 itojun 995: }
1.2 itojun 996:
1.22 itojun 997: /*
998: * We may see jumbolen in unaligned location, so
999: * we'd need to perform bcopy().
1000: */
1.126 tsutsui 1001: memcpy(&jumboplen, opt + 2, sizeof(jumboplen));
1.22 itojun 1002: jumboplen = (u_int32_t)htonl(jumboplen);
1003:
1004: #if 1
1005: /*
1006: * if there are multiple jumbo payload options,
1007: * *plenp will be non-zero and the packet will be
1008: * rejected.
1009: * the behavior may need some debate in ipngwg -
1010: * multiple options does not make sense, however,
1011: * there's no explicit mention in specification.
1012: */
1013: if (*plenp != 0) {
1.116 thorpej 1014: IP6_STATINC(IP6_STAT_BADOPTIONS);
1.22 itojun 1015: icmp6_error(m, ICMP6_PARAM_PROB,
1.55 itojun 1016: ICMP6_PARAMPROB_HEADER,
1017: erroff + opt + 2 - opthead);
1018: return (-1);
1.22 itojun 1019: }
1.8 itojun 1020: #endif
1.2 itojun 1021:
1.22 itojun 1022: /*
1023: * jumbo payload length must be larger than 65535.
1024: */
1025: if (jumboplen <= IPV6_MAXPACKET) {
1.116 thorpej 1026: IP6_STATINC(IP6_STAT_BADOPTIONS);
1.22 itojun 1027: icmp6_error(m, ICMP6_PARAM_PROB,
1.55 itojun 1028: ICMP6_PARAMPROB_HEADER,
1029: erroff + opt + 2 - opthead);
1030: return (-1);
1.22 itojun 1031: }
1032: *plenp = jumboplen;
1033:
1034: break;
1.35 itojun 1035: default: /* unknown option */
1036: if (hbhlen < IP6OPT_MINLEN) {
1.116 thorpej 1037: IP6_STATINC(IP6_STAT_TOOSMALL);
1.35 itojun 1038: goto bad;
1039: }
1.55 itojun 1040: optlen = ip6_unknown_opt(opt, m,
1041: erroff + opt - opthead);
1042: if (optlen == -1)
1043: return (-1);
1.35 itojun 1044: optlen += 2;
1045: break;
1.2 itojun 1046: }
1047: }
1048:
1.55 itojun 1049: return (0);
1.2 itojun 1050:
1051: bad:
1052: m_freem(m);
1.55 itojun 1053: return (-1);
1.2 itojun 1054: }
1055:
1056: /*
1057: * Unknown option processing.
1058: * The third argument `off' is the offset from the IPv6 header to the option,
1059: * which is necessary if the IPv6 header the and option header and IPv6 header
1060: * is not continuous in order to return an ICMPv6 error.
1061: */
1062: int
1.107 christos 1063: ip6_unknown_opt(u_int8_t *optp, struct mbuf *m, int off)
1.2 itojun 1064: {
1065: struct ip6_hdr *ip6;
1066:
1.35 itojun 1067: switch (IP6OPT_TYPE(*optp)) {
1068: case IP6OPT_TYPE_SKIP: /* ignore the option */
1.58 itojun 1069: return ((int)*(optp + 1));
1.35 itojun 1070: case IP6OPT_TYPE_DISCARD: /* silently discard */
1071: m_freem(m);
1.58 itojun 1072: return (-1);
1.35 itojun 1073: case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */
1.116 thorpej 1074: IP6_STATINC(IP6_STAT_BADOPTIONS);
1.35 itojun 1075: icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off);
1.58 itojun 1076: return (-1);
1.35 itojun 1077: case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */
1.116 thorpej 1078: IP6_STATINC(IP6_STAT_BADOPTIONS);
1.35 itojun 1079: ip6 = mtod(m, struct ip6_hdr *);
1080: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
1081: (m->m_flags & (M_BCAST|M_MCAST)))
1082: m_freem(m);
1083: else
1084: icmp6_error(m, ICMP6_PARAM_PROB,
1085: ICMP6_PARAMPROB_OPTION, off);
1.58 itojun 1086: return (-1);
1.2 itojun 1087: }
1088:
1089: m_freem(m); /* XXX: NOTREACHED */
1.58 itojun 1090: return (-1);
1.2 itojun 1091: }
1092:
1093: /*
1.9 itojun 1094: * Create the "control" list for this pcb.
1095: *
1096: * The routine will be called from upper layer handlers like tcp6_input().
1097: * Thus the routine assumes that the caller (tcp6_input) have already
1098: * called IP6_EXTHDR_CHECK() and all the extension headers are located in the
1099: * very first mbuf on the mbuf chain.
1100: * We may want to add some infinite loop prevention or sanity checks for safety.
1101: * (This applies only when you are using KAME mbuf chain restriction, i.e.
1102: * you are using IP6_EXTHDR_CHECK() not m_pulldown())
1.2 itojun 1103: */
1104: void
1.107 christos 1105: ip6_savecontrol(struct in6pcb *in6p, struct mbuf **mp,
1106: struct ip6_hdr *ip6, struct mbuf *m)
1.2 itojun 1107: {
1.85 rpaulo 1108: #ifdef RFC2292
1109: #define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1110: #else
1111: #define IS2292(x, y) (y)
1112: #endif
1113:
1.123 christos 1114: if (in6p->in6p_socket->so_options & SO_TIMESTAMP
1115: #ifdef SO_OTIMESTAMP
1116: || in6p->in6p_socket->so_options & SO_OTIMESTAMP
1117: #endif
1118: ) {
1.2 itojun 1119: struct timeval tv;
1120:
1121: microtime(&tv);
1.123 christos 1122: #ifdef SO_OTIMESTAMP
1123: if (in6p->in6p_socket->so_options & SO_OTIMESTAMP) {
1124: struct timeval50 tv50;
1125: timeval_to_timeval50(&tv, &tv50);
1126: *mp = sbcreatecontrol((void *) &tv50, sizeof(tv50),
1127: SCM_OTIMESTAMP, SOL_SOCKET);
1128: } else
1129: #endif
1.97 christos 1130: *mp = sbcreatecontrol((void *) &tv, sizeof(tv),
1.63 itojun 1131: SCM_TIMESTAMP, SOL_SOCKET);
1.2 itojun 1132: if (*mp)
1133: mp = &(*mp)->m_next;
1134: }
1.87 rpaulo 1135:
1136: /* some OSes call this logic with IPv4 packet, for SO_TIMESTAMP */
1137: if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION)
1138: return;
1139:
1.2 itojun 1140: /* RFC 2292 sec. 5 */
1.35 itojun 1141: if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) {
1.2 itojun 1142: struct in6_pktinfo pi6;
1.85 rpaulo 1143:
1.126 tsutsui 1144: memcpy(&pi6.ipi6_addr, &ip6->ip6_dst, sizeof(struct in6_addr));
1.81 rpaulo 1145: in6_clearscope(&pi6.ipi6_addr); /* XXX */
1.84 christos 1146: pi6.ipi6_ifindex = m->m_pkthdr.rcvif ?
1147: m->m_pkthdr.rcvif->if_index : 0;
1.97 christos 1148: *mp = sbcreatecontrol((void *) &pi6,
1.85 rpaulo 1149: sizeof(struct in6_pktinfo),
1150: IS2292(IPV6_2292PKTINFO, IPV6_PKTINFO), IPPROTO_IPV6);
1.2 itojun 1151: if (*mp)
1152: mp = &(*mp)->m_next;
1153: }
1.85 rpaulo 1154:
1.2 itojun 1155: if (in6p->in6p_flags & IN6P_HOPLIMIT) {
1156: int hlim = ip6->ip6_hlim & 0xff;
1.85 rpaulo 1157:
1.97 christos 1158: *mp = sbcreatecontrol((void *) &hlim, sizeof(int),
1.85 rpaulo 1159: IS2292(IPV6_2292HOPLIMIT, IPV6_HOPLIMIT), IPPROTO_IPV6);
1160: if (*mp)
1161: mp = &(*mp)->m_next;
1162: }
1163:
1164: if ((in6p->in6p_flags & IN6P_TCLASS) != 0) {
1165: u_int32_t flowinfo;
1166: int tclass;
1167:
1168: flowinfo = (u_int32_t)ntohl(ip6->ip6_flow & IPV6_FLOWINFO_MASK);
1169: flowinfo >>= 20;
1170:
1171: tclass = flowinfo & 0xff;
1.97 christos 1172: *mp = sbcreatecontrol((void *)&tclass, sizeof(tclass),
1.85 rpaulo 1173: IPV6_TCLASS, IPPROTO_IPV6);
1174:
1.2 itojun 1175: if (*mp)
1176: mp = &(*mp)->m_next;
1177: }
1178:
1179: /*
1.75 itojun 1180: * IPV6_HOPOPTS socket option. Recall that we required super-user
1181: * privilege for the option (see ip6_ctloutput), but it might be too
1182: * strict, since there might be some hop-by-hop options which can be
1183: * returned to normal user.
1.85 rpaulo 1184: * See also RFC3542 section 8 (or RFC2292 section 6).
1.2 itojun 1185: */
1.75 itojun 1186: if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) {
1.2 itojun 1187: /*
1188: * Check if a hop-by-hop options header is contatined in the
1189: * received packet, and if so, store the options as ancillary
1190: * data. Note that a hop-by-hop options header must be
1191: * just after the IPv6 header, which fact is assured through
1192: * the IPv6 input processing.
1193: */
1.78 christos 1194: struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
1195: if (xip6->ip6_nxt == IPPROTO_HOPOPTS) {
1.9 itojun 1196: struct ip6_hbh *hbh;
1197: int hbhlen;
1.62 itojun 1198: struct mbuf *ext;
1.9 itojun 1199:
1.62 itojun 1200: ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr),
1.78 christos 1201: xip6->ip6_nxt);
1.62 itojun 1202: if (ext == NULL) {
1.116 thorpej 1203: IP6_STATINC(IP6_STAT_TOOSHORT);
1.9 itojun 1204: return;
1205: }
1.62 itojun 1206: hbh = mtod(ext, struct ip6_hbh *);
1.9 itojun 1207: hbhlen = (hbh->ip6h_len + 1) << 3;
1.62 itojun 1208: if (hbhlen != ext->m_len) {
1209: m_freem(ext);
1.116 thorpej 1210: IP6_STATINC(IP6_STAT_TOOSHORT);
1.9 itojun 1211: return;
1212: }
1.2 itojun 1213:
1214: /*
1215: * XXX: We copy whole the header even if a jumbo
1216: * payload option is included, which option is to
1217: * be removed before returning in the RFC 2292.
1.85 rpaulo 1218: * Note: this constraint is removed in RFC3542.
1.2 itojun 1219: */
1.97 christos 1220: *mp = sbcreatecontrol((void *)hbh, hbhlen,
1.85 rpaulo 1221: IS2292(IPV6_2292HOPOPTS, IPV6_HOPOPTS),
1222: IPPROTO_IPV6);
1.2 itojun 1223: if (*mp)
1224: mp = &(*mp)->m_next;
1.62 itojun 1225: m_freem(ext);
1.2 itojun 1226: }
1227: }
1228:
1229: /* IPV6_DSTOPTS and IPV6_RTHDR socket options */
1230: if (in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDR)) {
1.78 christos 1231: struct ip6_hdr *xip6 = mtod(m, struct ip6_hdr *);
1232: int nxt = xip6->ip6_nxt, off = sizeof(struct ip6_hdr);
1.2 itojun 1233:
1234: /*
1235: * Search for destination options headers or routing
1236: * header(s) through the header chain, and stores each
1237: * header as ancillary data.
1238: * Note that the order of the headers remains in
1239: * the chain of ancillary data.
1240: */
1.86 rpaulo 1241: for (;;) { /* is explicit loop prevention necessary? */
1.62 itojun 1242: struct ip6_ext *ip6e = NULL;
1.9 itojun 1243: int elen;
1.62 itojun 1244: struct mbuf *ext = NULL;
1.9 itojun 1245:
1.62 itojun 1246: /*
1247: * if it is not an extension header, don't try to
1248: * pull it from the chain.
1249: */
1250: switch (nxt) {
1251: case IPPROTO_DSTOPTS:
1252: case IPPROTO_ROUTING:
1253: case IPPROTO_HOPOPTS:
1254: case IPPROTO_AH: /* is it possible? */
1255: break;
1256: default:
1257: goto loopend;
1258: }
1259:
1260: ext = ip6_pullexthdr(m, off, nxt);
1261: if (ext == NULL) {
1.116 thorpej 1262: IP6_STATINC(IP6_STAT_TOOSHORT);
1.9 itojun 1263: return;
1264: }
1.62 itojun 1265: ip6e = mtod(ext, struct ip6_ext *);
1.9 itojun 1266: if (nxt == IPPROTO_AH)
1267: elen = (ip6e->ip6e_len + 2) << 2;
1268: else
1269: elen = (ip6e->ip6e_len + 1) << 3;
1.62 itojun 1270: if (elen != ext->m_len) {
1271: m_freem(ext);
1.116 thorpej 1272: IP6_STATINC(IP6_STAT_TOOSHORT);
1.9 itojun 1273: return;
1274: }
1.57 thorpej 1275: KASSERT(IP6_HDR_ALIGNED_P(ip6e));
1.2 itojun 1276:
1.35 itojun 1277: switch (nxt) {
1.62 itojun 1278: case IPPROTO_DSTOPTS:
1.127 martin 1279: if (!(in6p->in6p_flags & IN6P_DSTOPTS))
1.35 itojun 1280: break;
1281:
1.97 christos 1282: *mp = sbcreatecontrol((void *)ip6e, elen,
1.85 rpaulo 1283: IS2292(IPV6_2292DSTOPTS, IPV6_DSTOPTS),
1284: IPPROTO_IPV6);
1.35 itojun 1285: if (*mp)
1286: mp = &(*mp)->m_next;
1287: break;
1288:
1289: case IPPROTO_ROUTING:
1.127 martin 1290: if (!(in6p->in6p_flags & IN6P_RTHDR))
1.35 itojun 1291: break;
1292:
1.97 christos 1293: *mp = sbcreatecontrol((void *)ip6e, elen,
1.85 rpaulo 1294: IS2292(IPV6_2292RTHDR, IPV6_RTHDR),
1295: IPPROTO_IPV6);
1.35 itojun 1296: if (*mp)
1297: mp = &(*mp)->m_next;
1298: break;
1299:
1.62 itojun 1300: case IPPROTO_HOPOPTS:
1301: case IPPROTO_AH: /* is it possible? */
1302: break;
1303:
1.35 itojun 1304: default:
1305: /*
1.62 itojun 1306: * other cases have been filtered in the above.
1307: * none will visit this case. here we supply
1308: * the code just in case (nxt overwritten or
1309: * other cases).
1.35 itojun 1310: */
1.62 itojun 1311: m_freem(ext);
1.35 itojun 1312: goto loopend;
1313:
1.2 itojun 1314: }
1315:
1316: /* proceed with the next header. */
1.9 itojun 1317: off += elen;
1.2 itojun 1318: nxt = ip6e->ip6e_nxt;
1.62 itojun 1319: ip6e = NULL;
1320: m_freem(ext);
1321: ext = NULL;
1.2 itojun 1322: }
1323: loopend:
1.62 itojun 1324: ;
1.2 itojun 1325: }
1.62 itojun 1326: }
1.85 rpaulo 1327: #undef IS2292
1328:
1329:
1330: void
1.95 dyoung 1331: ip6_notify_pmtu(struct in6pcb *in6p, const struct sockaddr_in6 *dst,
1332: uint32_t *mtu)
1.85 rpaulo 1333: {
1334: struct socket *so;
1335: struct mbuf *m_mtu;
1336: struct ip6_mtuinfo mtuctl;
1337:
1338: so = in6p->in6p_socket;
1339:
1340: if (mtu == NULL)
1341: return;
1342:
1343: #ifdef DIAGNOSTIC
1344: if (so == NULL) /* I believe this is impossible */
1345: panic("ip6_notify_pmtu: socket is NULL");
1346: #endif
1347:
1348: memset(&mtuctl, 0, sizeof(mtuctl)); /* zero-clear for safety */
1349: mtuctl.ip6m_mtu = *mtu;
1350: mtuctl.ip6m_addr = *dst;
1351: if (sa6_recoverscope(&mtuctl.ip6m_addr))
1352: return;
1353:
1.97 christos 1354: if ((m_mtu = sbcreatecontrol((void *)&mtuctl, sizeof(mtuctl),
1.85 rpaulo 1355: IPV6_PATHMTU, IPPROTO_IPV6)) == NULL)
1356: return;
1357:
1.95 dyoung 1358: if (sbappendaddr(&so->so_rcv, (const struct sockaddr *)dst, NULL, m_mtu)
1.85 rpaulo 1359: == 0) {
1360: m_freem(m_mtu);
1361: /* XXX: should count statistics */
1362: } else
1363: sorwakeup(so);
1364:
1365: return;
1366: }
1.62 itojun 1367:
1368: /*
1369: * pull single extension header from mbuf chain. returns single mbuf that
1370: * contains the result, or NULL on error.
1371: */
1372: static struct mbuf *
1.107 christos 1373: ip6_pullexthdr(struct mbuf *m, size_t off, int nxt)
1.62 itojun 1374: {
1375: struct ip6_ext ip6e;
1376: size_t elen;
1377: struct mbuf *n;
1378:
1379: #ifdef DIAGNOSTIC
1380: switch (nxt) {
1381: case IPPROTO_DSTOPTS:
1382: case IPPROTO_ROUTING:
1383: case IPPROTO_HOPOPTS:
1384: case IPPROTO_AH: /* is it possible? */
1385: break;
1386: default:
1387: printf("ip6_pullexthdr: invalid nxt=%d\n", nxt);
1.2 itojun 1388: }
1.62 itojun 1389: #endif
1.2 itojun 1390:
1.97 christos 1391: m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1.62 itojun 1392: if (nxt == IPPROTO_AH)
1393: elen = (ip6e.ip6e_len + 2) << 2;
1394: else
1395: elen = (ip6e.ip6e_len + 1) << 3;
1396:
1397: MGET(n, M_DONTWAIT, MT_DATA);
1398: if (n && elen >= MLEN) {
1399: MCLGET(n, M_DONTWAIT);
1400: if ((n->m_flags & M_EXT) == 0) {
1401: m_free(n);
1402: n = NULL;
1403: }
1404: }
1405: if (!n)
1406: return NULL;
1407:
1408: n->m_len = 0;
1409: if (elen >= M_TRAILINGSPACE(n)) {
1410: m_free(n);
1411: return NULL;
1412: }
1413:
1.97 christos 1414: m_copydata(m, off, elen, mtod(n, void *));
1.62 itojun 1415: n->m_len = elen;
1416: return n;
1.2 itojun 1417: }
1418:
1419: /*
1420: * Get pointer to the previous header followed by the header
1421: * currently processed.
1422: * XXX: This function supposes that
1423: * M includes all headers,
1424: * the next header field and the header length field of each header
1425: * are valid, and
1426: * the sum of each header length equals to OFF.
1427: * Because of these assumptions, this function must be called very
1428: * carefully. Moreover, it will not be used in the near future when
1429: * we develop `neater' mechanism to process extension headers.
1430: */
1.58 itojun 1431: u_int8_t *
1.107 christos 1432: ip6_get_prevhdr(struct mbuf *m, int off)
1.2 itojun 1433: {
1434: struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1435:
1436: if (off == sizeof(struct ip6_hdr))
1.58 itojun 1437: return (&ip6->ip6_nxt);
1.2 itojun 1438: else {
1439: int len, nxt;
1440: struct ip6_ext *ip6e = NULL;
1441:
1442: nxt = ip6->ip6_nxt;
1443: len = sizeof(struct ip6_hdr);
1444: while (len < off) {
1.97 christos 1445: ip6e = (struct ip6_ext *)(mtod(m, char *) + len);
1.2 itojun 1446:
1.35 itojun 1447: switch (nxt) {
1.2 itojun 1448: case IPPROTO_FRAGMENT:
1449: len += sizeof(struct ip6_frag);
1450: break;
1451: case IPPROTO_AH:
1452: len += (ip6e->ip6e_len + 2) << 2;
1453: break;
1454: default:
1455: len += (ip6e->ip6e_len + 1) << 3;
1456: break;
1457: }
1458: nxt = ip6e->ip6e_nxt;
1459: }
1460: if (ip6e)
1.58 itojun 1461: return (&ip6e->ip6e_nxt);
1.2 itojun 1462: else
1463: return NULL;
1.18 itojun 1464: }
1465: }
1466:
1467: /*
1468: * get next header offset. m will be retained.
1469: */
1470: int
1.107 christos 1471: ip6_nexthdr(struct mbuf *m, int off, int proto, int *nxtp)
1.18 itojun 1472: {
1473: struct ip6_hdr ip6;
1474: struct ip6_ext ip6e;
1475: struct ip6_frag fh;
1476:
1477: /* just in case */
1478: if (m == NULL)
1479: panic("ip6_nexthdr: m == NULL");
1480: if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off)
1481: return -1;
1482:
1483: switch (proto) {
1484: case IPPROTO_IPV6:
1.83 rpaulo 1485: /* do not chase beyond intermediate IPv6 headers */
1486: if (off != 0)
1487: return -1;
1.18 itojun 1488: if (m->m_pkthdr.len < off + sizeof(ip6))
1489: return -1;
1.97 christos 1490: m_copydata(m, off, sizeof(ip6), (void *)&ip6);
1.18 itojun 1491: if (nxtp)
1492: *nxtp = ip6.ip6_nxt;
1493: off += sizeof(ip6);
1494: return off;
1495:
1496: case IPPROTO_FRAGMENT:
1497: /*
1498: * terminate parsing if it is not the first fragment,
1499: * it does not make sense to parse through it.
1500: */
1501: if (m->m_pkthdr.len < off + sizeof(fh))
1502: return -1;
1.97 christos 1503: m_copydata(m, off, sizeof(fh), (void *)&fh);
1.67 itojun 1504: if ((fh.ip6f_offlg & IP6F_OFF_MASK) != 0)
1.18 itojun 1505: return -1;
1506: if (nxtp)
1507: *nxtp = fh.ip6f_nxt;
1508: off += sizeof(struct ip6_frag);
1509: return off;
1510:
1511: case IPPROTO_AH:
1512: if (m->m_pkthdr.len < off + sizeof(ip6e))
1513: return -1;
1.97 christos 1514: m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1.18 itojun 1515: if (nxtp)
1516: *nxtp = ip6e.ip6e_nxt;
1517: off += (ip6e.ip6e_len + 2) << 2;
1.47 itojun 1518: if (m->m_pkthdr.len < off)
1519: return -1;
1.18 itojun 1520: return off;
1521:
1522: case IPPROTO_HOPOPTS:
1523: case IPPROTO_ROUTING:
1524: case IPPROTO_DSTOPTS:
1525: if (m->m_pkthdr.len < off + sizeof(ip6e))
1526: return -1;
1.97 christos 1527: m_copydata(m, off, sizeof(ip6e), (void *)&ip6e);
1.18 itojun 1528: if (nxtp)
1529: *nxtp = ip6e.ip6e_nxt;
1530: off += (ip6e.ip6e_len + 1) << 3;
1.47 itojun 1531: if (m->m_pkthdr.len < off)
1532: return -1;
1.18 itojun 1533: return off;
1534:
1535: case IPPROTO_NONE:
1536: case IPPROTO_ESP:
1537: case IPPROTO_IPCOMP:
1538: /* give up */
1539: return -1;
1540:
1541: default:
1542: return -1;
1543: }
1544: }
1545:
1546: /*
1547: * get offset for the last header in the chain. m will be kept untainted.
1548: */
1549: int
1.107 christos 1550: ip6_lasthdr(struct mbuf *m, int off, int proto, int *nxtp)
1.18 itojun 1551: {
1552: int newoff;
1553: int nxt;
1554:
1555: if (!nxtp) {
1556: nxt = -1;
1557: nxtp = &nxt;
1558: }
1.86 rpaulo 1559: for (;;) {
1.18 itojun 1560: newoff = ip6_nexthdr(m, off, proto, nxtp);
1561: if (newoff < 0)
1562: return off;
1563: else if (newoff < off)
1564: return -1; /* invalid */
1565: else if (newoff == off)
1566: return newoff;
1567:
1568: off = newoff;
1569: proto = *nxtp;
1.2 itojun 1570: }
1571: }
1572:
1.81 rpaulo 1573: struct m_tag *
1.107 christos 1574: ip6_addaux(struct mbuf *m)
1.81 rpaulo 1575: {
1576: struct m_tag *mtag;
1577:
1578: mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
1579: if (!mtag) {
1580: mtag = m_tag_get(PACKET_TAG_INET6, sizeof(struct ip6aux),
1581: M_NOWAIT);
1582: if (mtag) {
1583: m_tag_prepend(m, mtag);
1.124 cegger 1584: memset(mtag + 1, 0, sizeof(struct ip6aux));
1.81 rpaulo 1585: }
1586: }
1587: return mtag;
1588: }
1589:
1590: struct m_tag *
1.107 christos 1591: ip6_findaux(struct mbuf *m)
1.81 rpaulo 1592: {
1593: struct m_tag *mtag;
1594:
1595: mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
1596: return mtag;
1597: }
1598:
1599: void
1.107 christos 1600: ip6_delaux(struct mbuf *m)
1.81 rpaulo 1601: {
1602: struct m_tag *mtag;
1603:
1604: mtag = m_tag_find(m, PACKET_TAG_INET6, NULL);
1605: if (mtag)
1606: m_tag_delete(m, mtag);
1607: }
1608:
1.98 liamjfoy 1609: #ifdef GATEWAY
1610: /*
1611: * sysctl helper routine for net.inet.ip6.maxflows. Since
1612: * we could reduce this value, call ip6flow_reap();
1613: */
1614: static int
1.101 liamjfoy 1615: sysctl_net_inet6_ip6_maxflows(SYSCTLFN_ARGS)
1.98 liamjfoy 1616: {
1.121 simonb 1617: int error;
1.98 liamjfoy 1618:
1.121 simonb 1619: error = sysctl_lookup(SYSCTLFN_CALL(rnode));
1620: if (error || newp == NULL)
1621: return (error);
1.98 liamjfoy 1622:
1.121 simonb 1623: mutex_enter(softnet_lock);
1.122 matt 1624: KERNEL_LOCK(1, NULL);
1.120 matt 1625:
1.121 simonb 1626: ip6flow_reap(0);
1.120 matt 1627:
1628: KERNEL_UNLOCK_ONE(NULL);
1.121 simonb 1629: mutex_exit(softnet_lock);
1.98 liamjfoy 1630:
1.121 simonb 1631: return (0);
1.98 liamjfoy 1632: }
1.99 liamjfoy 1633:
1634: static int
1635: sysctl_net_inet6_ip6_hashsize(SYSCTLFN_ARGS)
1636: {
1637: int error, tmp;
1638: struct sysctlnode node;
1639:
1640: node = *rnode;
1641: tmp = ip6_hashsize;
1642: node.sysctl_data = &tmp;
1643: error = sysctl_lookup(SYSCTLFN_CALL(&node));
1644: if (error || newp == NULL)
1645: return (error);
1646:
1647: if ((tmp & (tmp - 1)) == 0 && tmp != 0) {
1648: /*
1649: * Can only fail due to malloc()
1650: */
1.120 matt 1651: mutex_enter(softnet_lock);
1.122 matt 1652: KERNEL_LOCK(1, NULL);
1.120 matt 1653:
1654: error = ip6flow_invalidate_all(tmp);
1655:
1656: KERNEL_UNLOCK_ONE(NULL);
1657: mutex_exit(softnet_lock);
1.99 liamjfoy 1658: } else {
1659: /*
1660: * EINVAL if not a power of 2
1.121 simonb 1661: */
1.120 matt 1662: error = EINVAL;
1.99 liamjfoy 1663: }
1664:
1.120 matt 1665: return error;
1.99 liamjfoy 1666: }
1.98 liamjfoy 1667: #endif /* GATEWAY */
1668:
1.2 itojun 1669: /*
1670: * System control for IP6
1671: */
1672:
1.120 matt 1673: const u_char inet6ctlerrmap[PRC_NCMDS] = {
1.2 itojun 1674: 0, 0, 0, 0,
1675: 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1676: EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1677: EMSGSIZE, EHOSTUNREACH, 0, 0,
1678: 0, 0, 0, 0,
1679: ENOPROTOOPT
1680: };
1681:
1.116 thorpej 1682: static int
1683: sysctl_net_inet6_ip6_stats(SYSCTLFN_ARGS)
1684: {
1685:
1.119 thorpej 1686: return (NETSTAT_SYSCTL(ip6stat_percpu, IP6_NSTATS));
1.116 thorpej 1687: }
1688:
1.128 pooka 1689: static void
1690: sysctl_net_inet6_ip6_setup(struct sysctllog **clog)
1.2 itojun 1691: {
1.85 rpaulo 1692: #ifdef RFC2292
1693: #define IS2292(x, y) ((in6p->in6p_flags & IN6P_RFC2292) ? (x) : (y))
1694: #else
1695: #define IS2292(x, y) (y)
1696: #endif
1.26 itojun 1697:
1.73 atatat 1698: sysctl_createv(clog, 0, NULL, NULL,
1699: CTLFLAG_PERMANENT,
1.70 atatat 1700: CTLTYPE_NODE, "net", NULL,
1701: NULL, 0, NULL, 0,
1702: CTL_NET, CTL_EOL);
1.73 atatat 1703: sysctl_createv(clog, 0, NULL, NULL,
1704: CTLFLAG_PERMANENT,
1.74 atatat 1705: CTLTYPE_NODE, "inet6",
1706: SYSCTL_DESCR("PF_INET6 related settings"),
1.70 atatat 1707: NULL, 0, NULL, 0,
1708: CTL_NET, PF_INET6, CTL_EOL);
1.73 atatat 1709: sysctl_createv(clog, 0, NULL, NULL,
1710: CTLFLAG_PERMANENT,
1.74 atatat 1711: CTLTYPE_NODE, "ip6",
1712: SYSCTL_DESCR("IPv6 related settings"),
1.70 atatat 1713: NULL, 0, NULL, 0,
1714: CTL_NET, PF_INET6, IPPROTO_IPV6, CTL_EOL);
1715:
1.73 atatat 1716: sysctl_createv(clog, 0, NULL, NULL,
1717: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1718: CTLTYPE_INT, "forwarding",
1719: SYSCTL_DESCR("Enable forwarding of INET6 datagrams"),
1.70 atatat 1720: NULL, 0, &ip6_forwarding, 0,
1721: CTL_NET, PF_INET6, IPPROTO_IPV6,
1722: IPV6CTL_FORWARDING, CTL_EOL);
1.73 atatat 1723: sysctl_createv(clog, 0, NULL, NULL,
1724: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1725: CTLTYPE_INT, "redirect",
1726: SYSCTL_DESCR("Enable sending of ICMPv6 redirect messages"),
1.70 atatat 1727: NULL, 0, &ip6_sendredirects, 0,
1728: CTL_NET, PF_INET6, IPPROTO_IPV6,
1729: IPV6CTL_SENDREDIRECTS, CTL_EOL);
1.73 atatat 1730: sysctl_createv(clog, 0, NULL, NULL,
1731: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1732: CTLTYPE_INT, "hlim",
1733: SYSCTL_DESCR("Hop limit for an INET6 datagram"),
1.70 atatat 1734: NULL, 0, &ip6_defhlim, 0,
1735: CTL_NET, PF_INET6, IPPROTO_IPV6,
1736: IPV6CTL_DEFHLIM, CTL_EOL);
1737: #ifdef notyet
1.73 atatat 1738: sysctl_createv(clog, 0, NULL, NULL,
1739: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70 atatat 1740: CTLTYPE_INT, "mtu", NULL,
1741: NULL, 0, &, 0,
1742: CTL_NET, PF_INET6, IPPROTO_IPV6,
1743: IPV6CTL_DEFMTU, CTL_EOL);
1744: #endif
1745: #ifdef __no_idea__
1.73 atatat 1746: sysctl_createv(clog, 0, NULL, NULL,
1747: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70 atatat 1748: CTLTYPE_INT, "forwsrcrt", NULL,
1749: NULL, 0, &?, 0,
1750: CTL_NET, PF_INET6, IPPROTO_IPV6,
1751: IPV6CTL_FORWSRCRT, CTL_EOL);
1.73 atatat 1752: sysctl_createv(clog, 0, NULL, NULL,
1753: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70 atatat 1754: CTLTYPE_STRUCT, "mrtstats", NULL,
1755: NULL, 0, &?, sizeof(?),
1756: CTL_NET, PF_INET6, IPPROTO_IPV6,
1757: IPV6CTL_MRTSTATS, CTL_EOL);
1.73 atatat 1758: sysctl_createv(clog, 0, NULL, NULL,
1759: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70 atatat 1760: CTLTYPE_?, "mrtproto", NULL,
1761: NULL, 0, &?, sizeof(?),
1762: CTL_NET, PF_INET6, IPPROTO_IPV6,
1763: IPV6CTL_MRTPROTO, CTL_EOL);
1764: #endif
1.73 atatat 1765: sysctl_createv(clog, 0, NULL, NULL,
1766: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1767: CTLTYPE_INT, "maxfragpackets",
1768: SYSCTL_DESCR("Maximum number of fragments to buffer "
1769: "for reassembly"),
1.70 atatat 1770: NULL, 0, &ip6_maxfragpackets, 0,
1771: CTL_NET, PF_INET6, IPPROTO_IPV6,
1772: IPV6CTL_MAXFRAGPACKETS, CTL_EOL);
1773: #ifdef __no_idea__
1.73 atatat 1774: sysctl_createv(clog, 0, NULL, NULL,
1775: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70 atatat 1776: CTLTYPE_INT, "sourcecheck", NULL,
1777: NULL, 0, &?, 0,
1778: CTL_NET, PF_INET6, IPPROTO_IPV6,
1779: IPV6CTL_SOURCECHECK, CTL_EOL);
1.73 atatat 1780: sysctl_createv(clog, 0, NULL, NULL,
1781: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70 atatat 1782: CTLTYPE_INT, "sourcecheck_logint", NULL,
1783: NULL, 0, &?, 0,
1784: CTL_NET, PF_INET6, IPPROTO_IPV6,
1785: IPV6CTL_SOURCECHECK_LOGINT, CTL_EOL);
1786: #endif
1.73 atatat 1787: sysctl_createv(clog, 0, NULL, NULL,
1788: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1789: CTLTYPE_INT, "accept_rtadv",
1790: SYSCTL_DESCR("Accept router advertisements"),
1.70 atatat 1791: NULL, 0, &ip6_accept_rtadv, 0,
1792: CTL_NET, PF_INET6, IPPROTO_IPV6,
1793: IPV6CTL_ACCEPT_RTADV, CTL_EOL);
1.73 atatat 1794: sysctl_createv(clog, 0, NULL, NULL,
1795: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1796: CTLTYPE_INT, "keepfaith",
1797: SYSCTL_DESCR("Activate faith interface"),
1.70 atatat 1798: NULL, 0, &ip6_keepfaith, 0,
1799: CTL_NET, PF_INET6, IPPROTO_IPV6,
1800: IPV6CTL_KEEPFAITH, CTL_EOL);
1.73 atatat 1801: sysctl_createv(clog, 0, NULL, NULL,
1802: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1803: CTLTYPE_INT, "log_interval",
1804: SYSCTL_DESCR("Minumum interval between logging "
1805: "unroutable packets"),
1.70 atatat 1806: NULL, 0, &ip6_log_interval, 0,
1807: CTL_NET, PF_INET6, IPPROTO_IPV6,
1808: IPV6CTL_LOG_INTERVAL, CTL_EOL);
1.73 atatat 1809: sysctl_createv(clog, 0, NULL, NULL,
1810: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1811: CTLTYPE_INT, "hdrnestlimit",
1812: SYSCTL_DESCR("Maximum number of nested IPv6 headers"),
1.70 atatat 1813: NULL, 0, &ip6_hdrnestlimit, 0,
1814: CTL_NET, PF_INET6, IPPROTO_IPV6,
1815: IPV6CTL_HDRNESTLIMIT, CTL_EOL);
1.73 atatat 1816: sysctl_createv(clog, 0, NULL, NULL,
1817: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1818: CTLTYPE_INT, "dad_count",
1819: SYSCTL_DESCR("Number of Duplicate Address Detection "
1820: "probes to send"),
1.70 atatat 1821: NULL, 0, &ip6_dad_count, 0,
1822: CTL_NET, PF_INET6, IPPROTO_IPV6,
1823: IPV6CTL_DAD_COUNT, CTL_EOL);
1.73 atatat 1824: sysctl_createv(clog, 0, NULL, NULL,
1825: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1826: CTLTYPE_INT, "auto_flowlabel",
1827: SYSCTL_DESCR("Assign random IPv6 flow labels"),
1.70 atatat 1828: NULL, 0, &ip6_auto_flowlabel, 0,
1829: CTL_NET, PF_INET6, IPPROTO_IPV6,
1830: IPV6CTL_AUTO_FLOWLABEL, CTL_EOL);
1.73 atatat 1831: sysctl_createv(clog, 0, NULL, NULL,
1832: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1833: CTLTYPE_INT, "defmcasthlim",
1834: SYSCTL_DESCR("Default multicast hop limit"),
1.70 atatat 1835: NULL, 0, &ip6_defmcasthlim, 0,
1836: CTL_NET, PF_INET6, IPPROTO_IPV6,
1837: IPV6CTL_DEFMCASTHLIM, CTL_EOL);
1.51 itojun 1838: #if NGIF > 0
1.73 atatat 1839: sysctl_createv(clog, 0, NULL, NULL,
1840: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1841: CTLTYPE_INT, "gifhlim",
1842: SYSCTL_DESCR("Default hop limit for a gif tunnel datagram"),
1.70 atatat 1843: NULL, 0, &ip6_gif_hlim, 0,
1844: CTL_NET, PF_INET6, IPPROTO_IPV6,
1845: IPV6CTL_GIF_HLIM, CTL_EOL);
1846: #endif /* NGIF */
1.73 atatat 1847: sysctl_createv(clog, 0, NULL, NULL,
1848: CTLFLAG_PERMANENT,
1.74 atatat 1849: CTLTYPE_STRING, "kame_version",
1850: SYSCTL_DESCR("KAME Version"),
1.78 christos 1851: NULL, 0, __UNCONST(__KAME_VERSION), 0,
1.70 atatat 1852: CTL_NET, PF_INET6, IPPROTO_IPV6,
1853: IPV6CTL_KAME_VERSION, CTL_EOL);
1.73 atatat 1854: sysctl_createv(clog, 0, NULL, NULL,
1855: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1856: CTLTYPE_INT, "use_deprecated",
1857: SYSCTL_DESCR("Allow use of deprecated addresses as "
1858: "source addresses"),
1.70 atatat 1859: NULL, 0, &ip6_use_deprecated, 0,
1860: CTL_NET, PF_INET6, IPPROTO_IPV6,
1861: IPV6CTL_USE_DEPRECATED, CTL_EOL);
1.73 atatat 1862: sysctl_createv(clog, 0, NULL, NULL,
1863: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.70 atatat 1864: CTLTYPE_INT, "rr_prune", NULL,
1865: NULL, 0, &ip6_rr_prune, 0,
1866: CTL_NET, PF_INET6, IPPROTO_IPV6,
1867: IPV6CTL_RR_PRUNE, CTL_EOL);
1.73 atatat 1868: sysctl_createv(clog, 0, NULL, NULL,
1869: CTLFLAG_PERMANENT
1.70 atatat 1870: #ifndef INET6_BINDV6ONLY
1.73 atatat 1871: |CTLFLAG_READWRITE,
1.70 atatat 1872: #endif
1.74 atatat 1873: CTLTYPE_INT, "v6only",
1874: SYSCTL_DESCR("Disallow PF_INET6 sockets from connecting "
1875: "to PF_INET sockets"),
1.70 atatat 1876: NULL, 0, &ip6_v6only, 0,
1877: CTL_NET, PF_INET6, IPPROTO_IPV6,
1878: IPV6CTL_V6ONLY, CTL_EOL);
1.73 atatat 1879: sysctl_createv(clog, 0, NULL, NULL,
1880: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1881: CTLTYPE_INT, "anonportmin",
1882: SYSCTL_DESCR("Lowest ephemeral port number to assign"),
1.70 atatat 1883: sysctl_net_inet_ip_ports, 0, &ip6_anonportmin, 0,
1884: CTL_NET, PF_INET6, IPPROTO_IPV6,
1885: IPV6CTL_ANONPORTMIN, CTL_EOL);
1.73 atatat 1886: sysctl_createv(clog, 0, NULL, NULL,
1887: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1888: CTLTYPE_INT, "anonportmax",
1889: SYSCTL_DESCR("Highest ephemeral port number to assign"),
1.70 atatat 1890: sysctl_net_inet_ip_ports, 0, &ip6_anonportmax, 0,
1891: CTL_NET, PF_INET6, IPPROTO_IPV6,
1892: IPV6CTL_ANONPORTMAX, CTL_EOL);
1.26 itojun 1893: #ifndef IPNOPRIVPORTS
1.73 atatat 1894: sysctl_createv(clog, 0, NULL, NULL,
1895: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1896: CTLTYPE_INT, "lowportmin",
1897: SYSCTL_DESCR("Lowest privileged ephemeral port number "
1898: "to assign"),
1.70 atatat 1899: sysctl_net_inet_ip_ports, 0, &ip6_lowportmin, 0,
1900: CTL_NET, PF_INET6, IPPROTO_IPV6,
1901: IPV6CTL_LOWPORTMIN, CTL_EOL);
1.73 atatat 1902: sysctl_createv(clog, 0, NULL, NULL,
1903: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1904: CTLTYPE_INT, "lowportmax",
1905: SYSCTL_DESCR("Highest privileged ephemeral port number "
1906: "to assign"),
1.70 atatat 1907: sysctl_net_inet_ip_ports, 0, &ip6_lowportmax, 0,
1908: CTL_NET, PF_INET6, IPPROTO_IPV6,
1909: IPV6CTL_LOWPORTMAX, CTL_EOL);
1910: #endif /* IPNOPRIVPORTS */
1.73 atatat 1911: sysctl_createv(clog, 0, NULL, NULL,
1912: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.83 rpaulo 1913: CTLTYPE_INT, "use_tempaddr",
1914: SYSCTL_DESCR("Use temporary address"),
1915: NULL, 0, &ip6_use_tempaddr, 0,
1916: CTL_NET, PF_INET6, IPPROTO_IPV6,
1917: CTL_CREATE, CTL_EOL);
1918: sysctl_createv(clog, 0, NULL, NULL,
1919: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1920: CTLTYPE_INT, "temppltime",
1921: SYSCTL_DESCR("preferred lifetime of a temporary address"),
1922: NULL, 0, &ip6_temp_preferred_lifetime, 0,
1923: CTL_NET, PF_INET6, IPPROTO_IPV6,
1924: CTL_CREATE, CTL_EOL);
1925: sysctl_createv(clog, 0, NULL, NULL,
1926: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1927: CTLTYPE_INT, "tempvltime",
1928: SYSCTL_DESCR("valid lifetime of a temporary address"),
1929: NULL, 0, &ip6_temp_valid_lifetime, 0,
1930: CTL_NET, PF_INET6, IPPROTO_IPV6,
1931: CTL_CREATE, CTL_EOL);
1932: sysctl_createv(clog, 0, NULL, NULL,
1933: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.74 atatat 1934: CTLTYPE_INT, "maxfrags",
1935: SYSCTL_DESCR("Maximum fragments in reassembly queue"),
1.70 atatat 1936: NULL, 0, &ip6_maxfrags, 0,
1937: CTL_NET, PF_INET6, IPPROTO_IPV6,
1938: IPV6CTL_MAXFRAGS, CTL_EOL);
1.79 rpaulo 1939: sysctl_createv(clog, 0, NULL, NULL,
1940: CTLFLAG_PERMANENT,
1941: CTLTYPE_STRUCT, "stats",
1942: SYSCTL_DESCR("IPv6 statistics"),
1.116 thorpej 1943: sysctl_net_inet6_ip6_stats, 0, NULL, 0,
1.79 rpaulo 1944: CTL_NET, PF_INET6, IPPROTO_IPV6,
1945: IPV6CTL_STATS, CTL_EOL);
1.81 rpaulo 1946: sysctl_createv(clog, 0, NULL, NULL,
1947: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1948: CTLTYPE_INT, "use_defaultzone",
1949: SYSCTL_DESCR("Whether to use the default scope zones"),
1950: NULL, 0, &ip6_use_defzone, 0,
1951: CTL_NET, PF_INET6, IPPROTO_IPV6,
1952: IPV6CTL_USE_DEFAULTZONE, CTL_EOL);
1.83 rpaulo 1953: sysctl_createv(clog, 0, NULL, NULL,
1954: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1955: CTLTYPE_INT, "mcast_pmtu",
1956: SYSCTL_DESCR("Enable pMTU discovery for multicast packet"),
1957: NULL, 0, &ip6_mcast_pmtu, 0,
1958: CTL_NET, PF_INET6, IPPROTO_IPV6,
1959: CTL_CREATE, CTL_EOL);
1.98 liamjfoy 1960: #ifdef GATEWAY
1961: sysctl_createv(clog, 0, NULL, NULL,
1962: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1963: CTLTYPE_INT, "maxflows",
1964: SYSCTL_DESCR("Number of flows for fast forwarding (IPv6)"),
1.101 liamjfoy 1965: sysctl_net_inet6_ip6_maxflows, 0, &ip6_maxflows, 0,
1.98 liamjfoy 1966: CTL_NET, PF_INET6, IPPROTO_IPV6,
1967: CTL_CREATE, CTL_EOL);
1.99 liamjfoy 1968: sysctl_createv(clog, 0, NULL, NULL,
1969: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1970: CTLTYPE_INT, "hashsize",
1971: SYSCTL_DESCR("Size of hash table for fast forwarding (IPv6)"),
1972: sysctl_net_inet6_ip6_hashsize, 0, &ip6_hashsize, 0,
1973: CTL_NET, PF_INET6, IPPROTO_IPV6,
1974: CTL_CREATE, CTL_EOL);
1.98 liamjfoy 1975: #endif
1.2 itojun 1976: }
1.116 thorpej 1977:
1978: void
1979: ip6_statinc(u_int stat)
1980: {
1981:
1982: KASSERT(stat < IP6_NSTATS);
1983: IP6_STATINC(stat);
1984: }
CVSweb <webmaster@jp.NetBSD.org>