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