xref: /nrf52832-nimble/rt-thread/components/net/lwip-2.1.0/src/include/lwip/ip6_zone.h (revision 104654410c56c573564690304ae786df310c91fc)
1*10465441SEvalZero /**
2*10465441SEvalZero  * @file
3*10465441SEvalZero  *
4*10465441SEvalZero  * IPv6 address scopes, zones, and scoping policy.
5*10465441SEvalZero  *
6*10465441SEvalZero  * This header provides the means to implement support for IPv6 address scopes,
7*10465441SEvalZero  * as per RFC 4007. An address scope can be either global or more constrained.
8*10465441SEvalZero  * In lwIP, we say that an address "has a scope" or "is scoped" when its scope
9*10465441SEvalZero  * is constrained, in which case the address is meaningful only in a specific
10*10465441SEvalZero  * "zone." For unicast addresses, only link-local addresses have a scope; in
11*10465441SEvalZero  * that case, the scope is the link. For multicast addresses, there are various
12*10465441SEvalZero  * scopes defined by RFC 4007 and others. For any constrained scope, a system
13*10465441SEvalZero  * must establish a (potentially one-to-many) mapping between zones and local
14*10465441SEvalZero  * interfaces. For example, a link-local address is valid on only one link (its
15*10465441SEvalZero  * zone). That link may be attached to one or more local interfaces. The
16*10465441SEvalZero  * decisions on which scopes are constrained and the mapping between zones and
17*10465441SEvalZero  * interfaces is together what we refer to as the "scoping policy" - more on
18*10465441SEvalZero  * this in a bit.
19*10465441SEvalZero  *
20*10465441SEvalZero  * In lwIP, each IPv6 address has an associated zone index. This zone index may
21*10465441SEvalZero  * be set to "no zone" (IP6_NO_ZONE, 0) or an actual zone. We say that an
22*10465441SEvalZero  * address "has a zone" or "is zoned" when its zone index is *not* set to "no
23*10465441SEvalZero  * zone." In lwIP, in principle, each address should be "properly zoned," which
24*10465441SEvalZero  * means that if the address has a zone if and only if has a scope. As such, it
25*10465441SEvalZero  * is a rule that an unscoped (e.g., global) address must never have a zone.
26*10465441SEvalZero  * Even though one could argue that there is always one zone even for global
27*10465441SEvalZero  * scopes, this rule exists for implementation simplicity. Violation of the
28*10465441SEvalZero  * rule will trigger assertions or otherwise result in undesired behavior.
29*10465441SEvalZero  *
30*10465441SEvalZero  * Backward compatibility prevents us from requiring that applications always
31*10465441SEvalZero  * provide properly zoned addresses. We do enforce the rule that the in the
32*10465441SEvalZero  * lwIP link layer (everything below netif->output_ip6() and in particular ND6)
33*10465441SEvalZero  * *all* addresses are properly zoned. Thus, on the output paths down the
34*10465441SEvalZero  * stack, various places deal with the case of addresses that lack a zone.
35*10465441SEvalZero  * Some of them are best-effort for efficiency (e.g. the PCB bind and connect
36*10465441SEvalZero  * API calls' attempts to add missing zones); ultimately the IPv6 output
37*10465441SEvalZero  * handler (@ref ip6_output_if_src) will set a zone if necessary.
38*10465441SEvalZero  *
39*10465441SEvalZero  * Aside from dealing with scoped addresses lacking a zone, a proper IPv6
40*10465441SEvalZero  * implementation must also ensure that a packet with a scoped source and/or
41*10465441SEvalZero  * destination address does not leave its zone. This is currently implemented
42*10465441SEvalZero  * in the input and forward functions. However, for output, these checks are
43*10465441SEvalZero  * deliberately omitted in order to keep the implementation lightweight. The
44*10465441SEvalZero  * routing algorithm in @ref ip6_route will take decisions such that it will
45*10465441SEvalZero  * not cause zone violations unless the application sets bad addresses, though.
46*10465441SEvalZero  *
47*10465441SEvalZero  * In terms of scoping policy, lwIP implements the default policy from RFC 4007
48*10465441SEvalZero  * using macros in this file. This policy considers link-local unicast
49*10465441SEvalZero  * addresses and (only) interface-local and link-local multicast addresses as
50*10465441SEvalZero  * having a scope. For all these addresses, the zone is equal to the interface.
51*10465441SEvalZero  * As shown below in this file, it is possible to implement a custom policy.
52*10465441SEvalZero  */
53*10465441SEvalZero 
54*10465441SEvalZero /*
55*10465441SEvalZero  * Copyright (c) 2017 The MINIX 3 Project.
56*10465441SEvalZero  * All rights reserved.
57*10465441SEvalZero  *
58*10465441SEvalZero  * Redistribution and use in source and binary forms, with or without modification,
59*10465441SEvalZero  * are permitted provided that the following conditions are met:
60*10465441SEvalZero  *
61*10465441SEvalZero  * 1. Redistributions of source code must retain the above copyright notice,
62*10465441SEvalZero  *    this list of conditions and the following disclaimer.
63*10465441SEvalZero  * 2. Redistributions in binary form must reproduce the above copyright notice,
64*10465441SEvalZero  *    this list of conditions and the following disclaimer in the documentation
65*10465441SEvalZero  *    and/or other materials provided with the distribution.
66*10465441SEvalZero  * 3. The name of the author may not be used to endorse or promote products
67*10465441SEvalZero  *    derived from this software without specific prior written permission.
68*10465441SEvalZero  *
69*10465441SEvalZero  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
70*10465441SEvalZero  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71*10465441SEvalZero  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
72*10465441SEvalZero  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
73*10465441SEvalZero  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
74*10465441SEvalZero  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
75*10465441SEvalZero  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
76*10465441SEvalZero  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
77*10465441SEvalZero  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
78*10465441SEvalZero  * OF SUCH DAMAGE.
79*10465441SEvalZero  *
80*10465441SEvalZero  * This file is part of the lwIP TCP/IP stack.
81*10465441SEvalZero  *
82*10465441SEvalZero  * Author: David van Moolenbroek <[email protected]>
83*10465441SEvalZero  *
84*10465441SEvalZero  */
85*10465441SEvalZero #ifndef LWIP_HDR_IP6_ZONE_H
86*10465441SEvalZero #define LWIP_HDR_IP6_ZONE_H
87*10465441SEvalZero 
88*10465441SEvalZero #ifdef __cplusplus
89*10465441SEvalZero extern "C" {
90*10465441SEvalZero #endif
91*10465441SEvalZero 
92*10465441SEvalZero /**
93*10465441SEvalZero  * @defgroup ip6_zones IPv6 Zones
94*10465441SEvalZero  * @ingroup ip6
95*10465441SEvalZero  * @{
96*10465441SEvalZero  */
97*10465441SEvalZero 
98*10465441SEvalZero #if LWIP_IPV6  /* don't build if not configured for use in lwipopts.h */
99*10465441SEvalZero 
100*10465441SEvalZero /** Identifier for "no zone". */
101*10465441SEvalZero #define IP6_NO_ZONE 0
102*10465441SEvalZero 
103*10465441SEvalZero #if LWIP_IPV6_SCOPES
104*10465441SEvalZero 
105*10465441SEvalZero /** Zone initializer for static IPv6 address initialization, including comma. */
106*10465441SEvalZero #define IPADDR6_ZONE_INIT , IP6_NO_ZONE
107*10465441SEvalZero 
108*10465441SEvalZero /** Return the zone index of the given IPv6 address; possibly "no zone". */
109*10465441SEvalZero #define ip6_addr_zone(ip6addr) ((ip6addr)->zone)
110*10465441SEvalZero 
111*10465441SEvalZero /** Does the given IPv6 address have a zone set? (0/1) */
112*10465441SEvalZero #define ip6_addr_has_zone(ip6addr) (ip6_addr_zone(ip6addr) != IP6_NO_ZONE)
113*10465441SEvalZero 
114*10465441SEvalZero /** Set the zone field of an IPv6 address to a particular value. */
115*10465441SEvalZero #define ip6_addr_set_zone(ip6addr, zone_idx) ((ip6addr)->zone = (zone_idx))
116*10465441SEvalZero 
117*10465441SEvalZero /** Clear the zone field of an IPv6 address, setting it to "no zone". */
118*10465441SEvalZero #define ip6_addr_clear_zone(ip6addr) ((ip6addr)->zone = IP6_NO_ZONE)
119*10465441SEvalZero 
120*10465441SEvalZero /** Copy the zone field from the second IPv6 address to the first one. */
121*10465441SEvalZero #define ip6_addr_copy_zone(ip6addr1, ip6addr2) ((ip6addr1).zone = (ip6addr2).zone)
122*10465441SEvalZero 
123*10465441SEvalZero /** Is the zone field of the given IPv6 address equal to the given zone index? (0/1) */
124*10465441SEvalZero #define ip6_addr_equals_zone(ip6addr, zone_idx) ((ip6addr)->zone == (zone_idx))
125*10465441SEvalZero 
126*10465441SEvalZero /** Are the zone fields of the given IPv6 addresses equal? (0/1)
127*10465441SEvalZero  * This macro must only be used on IPv6 addresses of the same scope. */
128*10465441SEvalZero #define ip6_addr_cmp_zone(ip6addr1, ip6addr2) ((ip6addr1)->zone == (ip6addr2)->zone)
129*10465441SEvalZero 
130*10465441SEvalZero /** Symbolic constants for the 'type' parameters in some of the macros.
131*10465441SEvalZero  * These exist for efficiency only, allowing the macros to avoid certain tests
132*10465441SEvalZero  * when the address is known not to be of a certain type. Dead code elimination
133*10465441SEvalZero  * will do the rest. IP6_MULTICAST is supported but currently not optimized.
134*10465441SEvalZero  * @see ip6_addr_has_scope, ip6_addr_assign_zone, ip6_addr_lacks_zone.
135*10465441SEvalZero  */
136*10465441SEvalZero enum lwip_ipv6_scope_type
137*10465441SEvalZero {
138*10465441SEvalZero   /** Unknown */
139*10465441SEvalZero   IP6_UNKNOWN   = 0,
140*10465441SEvalZero   /** Unicast */
141*10465441SEvalZero   IP6_UNICAST   = 1,
142*10465441SEvalZero   /** Multicast */
143*10465441SEvalZero   IP6_MULTICAST = 2
144*10465441SEvalZero };
145*10465441SEvalZero 
146*10465441SEvalZero /** IPV6_CUSTOM_SCOPES: together, the following three macro definitions,
147*10465441SEvalZero  * @ref ip6_addr_has_scope, @ref ip6_addr_assign_zone, and
148*10465441SEvalZero  * @ref ip6_addr_test_zone, completely define the lwIP scoping policy.
149*10465441SEvalZero  * The definitions below implement the default policy from RFC 4007 Sec. 6.
150*10465441SEvalZero  * Should an implementation desire to implement a different policy, it can
151*10465441SEvalZero  * define IPV6_CUSTOM_SCOPES to 1 and supply its own definitions for the three
152*10465441SEvalZero  * macros instead.
153*10465441SEvalZero  */
154*10465441SEvalZero #ifndef IPV6_CUSTOM_SCOPES
155*10465441SEvalZero #define IPV6_CUSTOM_SCOPES 0
156*10465441SEvalZero #endif /* !IPV6_CUSTOM_SCOPES */
157*10465441SEvalZero 
158*10465441SEvalZero #if !IPV6_CUSTOM_SCOPES
159*10465441SEvalZero 
160*10465441SEvalZero /**
161*10465441SEvalZero  * Determine whether an IPv6 address has a constrained scope, and as such is
162*10465441SEvalZero  * meaningful only if accompanied by a zone index to identify the scope's zone.
163*10465441SEvalZero  * The given address type may be used to eliminate at compile time certain
164*10465441SEvalZero  * checks that will evaluate to false at run time anyway.
165*10465441SEvalZero  *
166*10465441SEvalZero  * This default implementation follows the default model of RFC 4007, where
167*10465441SEvalZero  * only interface-local and link-local scopes are defined.
168*10465441SEvalZero  *
169*10465441SEvalZero  * Even though the unicast loopback address does have an implied link-local
170*10465441SEvalZero  * scope, in this implementation it does not have an explicitly assigned zone
171*10465441SEvalZero  * index. As such it should not be tested for in this macro.
172*10465441SEvalZero  *
173*10465441SEvalZero  * @param ip6addr the IPv6 address (const); only its address part is examined.
174*10465441SEvalZero  * @param type address type; see @ref lwip_ipv6_scope_type.
175*10465441SEvalZero  * @return 1 if the address has a constrained scope, 0 if it does not.
176*10465441SEvalZero  */
177*10465441SEvalZero #define ip6_addr_has_scope(ip6addr, type) \
178*10465441SEvalZero   (ip6_addr_islinklocal(ip6addr) || (((type) != IP6_UNICAST) && \
179*10465441SEvalZero    (ip6_addr_ismulticast_iflocal(ip6addr) || \
180*10465441SEvalZero     ip6_addr_ismulticast_linklocal(ip6addr))))
181*10465441SEvalZero 
182*10465441SEvalZero /**
183*10465441SEvalZero  * Assign a zone index to an IPv6 address, based on a network interface. If the
184*10465441SEvalZero  * given address has a scope, the assigned zone index is that scope's zone of
185*10465441SEvalZero  * the given netif; otherwise, the assigned zone index is "no zone".
186*10465441SEvalZero  *
187*10465441SEvalZero  * This default implementation follows the default model of RFC 4007, where
188*10465441SEvalZero  * only interface-local and link-local scopes are defined, and the zone index
189*10465441SEvalZero  * of both of those scopes always equals the index of the network interface.
190*10465441SEvalZero  * As such, this default implementation need not distinguish between different
191*10465441SEvalZero  * constrained scopes when assigning the zone.
192*10465441SEvalZero  *
193*10465441SEvalZero  * @param ip6addr the IPv6 address; its address part is examined, and its zone
194*10465441SEvalZero  *                index is assigned.
195*10465441SEvalZero  * @param type address type; see @ref lwip_ipv6_scope_type.
196*10465441SEvalZero  * @param netif the network interface (const).
197*10465441SEvalZero  */
198*10465441SEvalZero #define ip6_addr_assign_zone(ip6addr, type, netif) \
199*10465441SEvalZero     (ip6_addr_set_zone((ip6addr), \
200*10465441SEvalZero       ip6_addr_has_scope((ip6addr), (type)) ? netif_get_index(netif) : 0))
201*10465441SEvalZero 
202*10465441SEvalZero /**
203*10465441SEvalZero  * Test whether an IPv6 address is "zone-compatible" with a network interface.
204*10465441SEvalZero  * That is, test whether the network interface is part of the zone associated
205*10465441SEvalZero  * with the address. For efficiency, this macro is only ever called if the
206*10465441SEvalZero  * given address is either scoped or zoned, and thus, it need not test this.
207*10465441SEvalZero  * If an address is scoped but not zoned, or zoned and not scoped, it is
208*10465441SEvalZero  * considered not zone-compatible with any netif.
209*10465441SEvalZero  *
210*10465441SEvalZero  * This default implementation follows the default model of RFC 4007, where
211*10465441SEvalZero  * only interface-local and link-local scopes are defined, and the zone index
212*10465441SEvalZero  * of both of those scopes always equals the index of the network interface.
213*10465441SEvalZero  * As such, there is always only one matching netif for a specific zone index,
214*10465441SEvalZero  * but all call sites of this macro currently support multiple matching netifs
215*10465441SEvalZero  * as well (at no additional expense in the common case).
216*10465441SEvalZero  *
217*10465441SEvalZero  * @param ip6addr the IPv6 address (const).
218*10465441SEvalZero  * @param netif the network interface (const).
219*10465441SEvalZero  * @return 1 if the address is scope-compatible with the netif, 0 if not.
220*10465441SEvalZero  */
221*10465441SEvalZero #define ip6_addr_test_zone(ip6addr, netif) \
222*10465441SEvalZero     (ip6_addr_equals_zone((ip6addr), netif_get_index(netif)))
223*10465441SEvalZero 
224*10465441SEvalZero #endif /* !IPV6_CUSTOM_SCOPES */
225*10465441SEvalZero 
226*10465441SEvalZero /** Does the given IPv6 address have a scope, and as such should also have a
227*10465441SEvalZero  * zone to be meaningful, but does not actually have a zone? (0/1) */
228*10465441SEvalZero #define ip6_addr_lacks_zone(ip6addr, type) \
229*10465441SEvalZero     (!ip6_addr_has_zone(ip6addr) && ip6_addr_has_scope((ip6addr), (type)))
230*10465441SEvalZero 
231*10465441SEvalZero /**
232*10465441SEvalZero  * Try to select a zone for a scoped address that does not yet have a zone.
233*10465441SEvalZero  * Called from PCB bind and connect routines, for two reasons: 1) to save on
234*10465441SEvalZero  * this (relatively expensive) selection for every individual packet route
235*10465441SEvalZero  * operation and 2) to allow the application to obtain the selected zone from
236*10465441SEvalZero  * the PCB as is customary for e.g. getsockname/getpeername BSD socket calls.
237*10465441SEvalZero  *
238*10465441SEvalZero  * Ideally, callers would always supply a properly zoned address, in which case
239*10465441SEvalZero  * this function would not be needed. It exists both for compatibility with the
240*10465441SEvalZero  * BSD socket API (which accepts zoneless destination addresses) and for
241*10465441SEvalZero  * backward compatibility with pre-scoping lwIP code.
242*10465441SEvalZero  *
243*10465441SEvalZero  * It may be impossible to select a zone, e.g. if there are no netifs.  In that
244*10465441SEvalZero  * case, the address's zone field will be left as is.
245*10465441SEvalZero  *
246*10465441SEvalZero  * @param dest the IPv6 address for which to select and set a zone.
247*10465441SEvalZero  * @param src source IPv6 address (const); may be equal to dest.
248*10465441SEvalZero  */
249*10465441SEvalZero #define ip6_addr_select_zone(dest, src) do { struct netif *selected_netif; \
250*10465441SEvalZero   selected_netif = ip6_route((src), (dest)); \
251*10465441SEvalZero   if (selected_netif != NULL) { \
252*10465441SEvalZero     ip6_addr_assign_zone((dest), IP6_UNKNOWN, selected_netif); \
253*10465441SEvalZero   } } while (0)
254*10465441SEvalZero 
255*10465441SEvalZero /**
256*10465441SEvalZero  * @}
257*10465441SEvalZero  */
258*10465441SEvalZero 
259*10465441SEvalZero #else /* LWIP_IPV6_SCOPES */
260*10465441SEvalZero 
261*10465441SEvalZero #define IPADDR6_ZONE_INIT
262*10465441SEvalZero #define ip6_addr_zone(ip6addr) (IP6_NO_ZONE)
263*10465441SEvalZero #define ip6_addr_has_zone(ip6addr) (0)
264*10465441SEvalZero #define ip6_addr_set_zone(ip6addr, zone_idx)
265*10465441SEvalZero #define ip6_addr_clear_zone(ip6addr)
266*10465441SEvalZero #define ip6_addr_copy_zone(ip6addr1, ip6addr2)
267*10465441SEvalZero #define ip6_addr_equals_zone(ip6addr, zone_idx) (1)
268*10465441SEvalZero #define ip6_addr_cmp_zone(ip6addr1, ip6addr2) (1)
269*10465441SEvalZero #define IPV6_CUSTOM_SCOPES 0
270*10465441SEvalZero #define ip6_addr_has_scope(ip6addr, type) (0)
271*10465441SEvalZero #define ip6_addr_assign_zone(ip6addr, type, netif)
272*10465441SEvalZero #define ip6_addr_test_zone(ip6addr, netif) (1)
273*10465441SEvalZero #define ip6_addr_lacks_zone(ip6addr, type) (0)
274*10465441SEvalZero #define ip6_addr_select_zone(ip6addr, src)
275*10465441SEvalZero 
276*10465441SEvalZero #endif /* LWIP_IPV6_SCOPES */
277*10465441SEvalZero 
278*10465441SEvalZero #if LWIP_IPV6_SCOPES && LWIP_IPV6_SCOPES_DEBUG
279*10465441SEvalZero 
280*10465441SEvalZero /** Verify that the given IPv6 address is properly zoned. */
281*10465441SEvalZero #define IP6_ADDR_ZONECHECK(ip6addr) LWIP_ASSERT("IPv6 zone check failed", \
282*10465441SEvalZero     ip6_addr_has_scope(ip6addr, IP6_UNKNOWN) == ip6_addr_has_zone(ip6addr))
283*10465441SEvalZero 
284*10465441SEvalZero /** Verify that the given IPv6 address is properly zoned for the given netif. */
285*10465441SEvalZero #define IP6_ADDR_ZONECHECK_NETIF(ip6addr, netif) LWIP_ASSERT("IPv6 netif zone check failed", \
286*10465441SEvalZero     ip6_addr_has_scope(ip6addr, IP6_UNKNOWN) ? \
287*10465441SEvalZero     (ip6_addr_has_zone(ip6addr) && \
288*10465441SEvalZero      (((netif) == NULL) || ip6_addr_test_zone((ip6addr), (netif)))) : \
289*10465441SEvalZero     !ip6_addr_has_zone(ip6addr))
290*10465441SEvalZero 
291*10465441SEvalZero #else /* LWIP_IPV6_SCOPES && LWIP_IPV6_SCOPES_DEBUG */
292*10465441SEvalZero 
293*10465441SEvalZero #define IP6_ADDR_ZONECHECK(ip6addr)
294*10465441SEvalZero #define IP6_ADDR_ZONECHECK_NETIF(ip6addr, netif)
295*10465441SEvalZero 
296*10465441SEvalZero #endif /* LWIP_IPV6_SCOPES && LWIP_IPV6_SCOPES_DEBUG */
297*10465441SEvalZero 
298*10465441SEvalZero #endif /* LWIP_IPV6 */
299*10465441SEvalZero 
300*10465441SEvalZero #ifdef __cplusplus
301*10465441SEvalZero }
302*10465441SEvalZero #endif
303*10465441SEvalZero 
304*10465441SEvalZero #endif /* LWIP_HDR_IP6_ZONE_H */
305