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