Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ip6_zone.h Source File

ip6_zone.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  *
00004  * IPv6 address scopes, zones, and scoping policy.
00005  *
00006  * This header provides the means to implement support for IPv6 address scopes,
00007  * as per RFC 4007. An address scope can be either global or more constrained.
00008  * In lwIP, we say that an address "has a scope" or "is scoped" when its scope
00009  * is constrained, in which case the address is meaningful only in a specific
00010  * "zone." For unicast addresses, only link-local addresses have a scope; in
00011  * that case, the scope is the link. For multicast addresses, there are various
00012  * scopes defined by RFC 4007 and others. For any constrained scope, a system
00013  * must establish a (potentially one-to-many) mapping between zones and local
00014  * interfaces. For example, a link-local address is valid on only one link (its
00015  * zone). That link may be attached to one or more local interfaces. The
00016  * decisions on which scopes are constrained and the mapping between zones and
00017  * interfaces is together what we refer to as the "scoping policy" - more on
00018  * this in a bit.
00019  *
00020  * In lwIP, each IPv6 address has an associated zone index. This zone index may
00021  * be set to "no zone" (IP6_NO_ZONE, 0) or an actual zone. We say that an
00022  * address "has a zone" or "is zoned" when its zone index is *not* set to "no
00023  * zone." In lwIP, in principle, each address should be "properly zoned," which
00024  * means that if the address has a zone if and only if has a scope. As such, it
00025  * is a rule that an unscoped (e.g., global) address must never have a zone.
00026  * Even though one could argue that there is always one zone even for global
00027  * scopes, this rule exists for implementation simplicity. Violation of the
00028  * rule will trigger assertions or otherwise result in undesired behavior.
00029  *
00030  * Backward compatibility prevents us from requiring that applications always
00031  * provide properly zoned addresses. We do enforce the rule that the in the
00032  * lwIP link layer (everything below netif->output_ip6() and in particular ND6)
00033  * *all* addresses are properly zoned. Thus, on the output paths down the
00034  * stack, various places deal with the case of addresses that lack a zone.
00035  * Some of them are best-effort for efficiency (e.g. the PCB bind and connect
00036  * API calls' attempts to add missing zones); ultimately the IPv6 output
00037  * handler (@ref ip6_output_if_src) will set a zone if necessary.
00038  *
00039  * Aside from dealing with scoped addresses lacking a zone, a proper IPv6
00040  * implementation must also ensure that a packet with a scoped source and/or
00041  * destination address does not leave its zone. This is currently implemented
00042  * in the input and forward functions. However, for output, these checks are
00043  * deliberately omitted in order to keep the implementation lightweight. The
00044  * routing algorithm in @ref ip6_route will take decisions such that it will
00045  * not cause zone violations unless the application sets bad addresses, though.
00046  *
00047  * In terms of scoping policy, lwIP implements the default policy from RFC 4007
00048  * using macros in this file. This policy considers link-local unicast
00049  * addresses and (only) interface-local and link-local multicast addresses as
00050  * having a scope. For all these addresses, the zone is equal to the interface.
00051  * As shown below in this file, it is possible to implement a custom policy.
00052  */
00053 
00054 /*
00055  * Copyright (c) 2017 The MINIX 3 Project.
00056  * All rights reserved.
00057  *
00058  * Redistribution and use in source and binary forms, with or without modification,
00059  * are permitted provided that the following conditions are met:
00060  *
00061  * 1. Redistributions of source code must retain the above copyright notice,
00062  *    this list of conditions and the following disclaimer.
00063  * 2. Redistributions in binary form must reproduce the above copyright notice,
00064  *    this list of conditions and the following disclaimer in the documentation
00065  *    and/or other materials provided with the distribution.
00066  * 3. The name of the author may not be used to endorse or promote products
00067  *    derived from this software without specific prior written permission.
00068  *
00069  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00070  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00071  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00072  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00073  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00074  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00075  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00076  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00077  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00078  * OF SUCH DAMAGE.
00079  *
00080  * This file is part of the lwIP TCP/IP stack.
00081  *
00082  * Author: David van Moolenbroek <david@minix3.org>
00083  *
00084  */
00085 #ifndef LWIP_HDR_IP6_ZONE_H
00086 #define LWIP_HDR_IP6_ZONE_H
00087 
00088 #ifdef __cplusplus
00089 extern "C" {
00090 #endif
00091 
00092 /**
00093  * @defgroup ip6_zones IPv6 Zones
00094  * @ingroup ip6
00095  * @{
00096  */
00097 
00098 #if LWIP_IPV6  /* don't build if not configured for use in lwipopts.h */
00099 
00100 /** Identifier for "no zone". */
00101 #define IP6_NO_ZONE 0
00102 
00103 #if LWIP_IPV6_SCOPES
00104 
00105 /** Zone initializer for static IPv6 address initialization, including comma. */
00106 #define IPADDR6_ZONE_INIT , IP6_NO_ZONE
00107 
00108 /** Return the zone index of the given IPv6 address; possibly "no zone". */
00109 #define ip6_addr_zone(ip6addr) ((ip6addr)->zone)
00110 
00111 /** Does the given IPv6 address have a zone set? (0/1) */
00112 #define ip6_addr_has_zone(ip6addr) (ip6_addr_zone(ip6addr) != IP6_NO_ZONE)
00113 
00114 /** Set the zone field of an IPv6 address to a particular value. */
00115 #define ip6_addr_set_zone(ip6addr, zone_idx) ((ip6addr)->zone = (zone_idx))
00116 
00117 /** Clear the zone field of an IPv6 address, setting it to "no zone". */
00118 #define ip6_addr_clear_zone(ip6addr) ((ip6addr)->zone = IP6_NO_ZONE)
00119 
00120 /** Copy the zone field from the second IPv6 address to the first one. */
00121 #define ip6_addr_copy_zone(ip6addr1, ip6addr2) ((ip6addr1).zone = (ip6addr2).zone)
00122 
00123 /** Is the zone field of the given IPv6 address equal to the given zone index? (0/1) */
00124 #define ip6_addr_equals_zone(ip6addr, zone_idx) ((ip6addr)->zone == (zone_idx))
00125 
00126 /** Are the zone fields of the given IPv6 addresses equal? (0/1)
00127  * This macro must only be used on IPv6 addresses of the same scope. */
00128 #define ip6_addr_cmp_zone(ip6addr1, ip6addr2) ((ip6addr1)->zone == (ip6addr2)->zone)
00129 
00130 /** Symbolic constants for the 'type' parameters in some of the macros.
00131  * These exist for efficiency only, allowing the macros to avoid certain tests
00132  * when the address is known not to be of a certain type. Dead code elimination
00133  * will do the rest. IP6_MULTICAST is supported but currently not optimized.
00134  * @see ip6_addr_has_scope, ip6_addr_assign_zone, ip6_addr_lacks_zone.
00135  */
00136 enum lwip_ipv6_scope_type
00137 {
00138   /** Unknown */
00139   IP6_UNKNOWN   = 0,
00140   /** Unicast */
00141   IP6_UNICAST   = 1,
00142   /** Multicast */
00143   IP6_MULTICAST = 2
00144 };
00145 
00146 /** IPV6_CUSTOM_SCOPES: together, the following three macro definitions,
00147  * @ref ip6_addr_has_scope, @ref ip6_addr_assign_zone, and
00148  * @ref ip6_addr_test_zone, completely define the lwIP scoping policy.
00149  * The definitions below implement the default policy from RFC 4007 Sec. 6.
00150  * Should an implementation desire to implement a different policy, it can
00151  * define IPV6_CUSTOM_SCOPES to 1 and supply its own definitions for the three
00152  * macros instead.
00153  */
00154 #ifndef IPV6_CUSTOM_SCOPES
00155 #define IPV6_CUSTOM_SCOPES 0
00156 #endif /* !IPV6_CUSTOM_SCOPES */
00157 
00158 #if !IPV6_CUSTOM_SCOPES
00159 
00160 /**
00161  * Determine whether an IPv6 address has a constrained scope, and as such is
00162  * meaningful only if accompanied by a zone index to identify the scope's zone.
00163  * The given address type may be used to eliminate at compile time certain
00164  * checks that will evaluate to false at run time anyway.
00165  *
00166  * This default implementation follows the default model of RFC 4007, where
00167  * only interface-local and link-local scopes are defined.
00168  *
00169  * Even though the unicast loopback address does have an implied link-local
00170  * scope, in this implementation it does not have an explicitly assigned zone
00171  * index. As such it should not be tested for in this macro.
00172  *
00173  * @param ip6addr the IPv6 address (const); only its address part is examined.
00174  * @param type address type; see @ref lwip_ipv6_scope_type.
00175  * @return 1 if the address has a constrained scope, 0 if it does not.
00176  */
00177 #define ip6_addr_has_scope(ip6addr, type) \
00178   (ip6_addr_islinklocal(ip6addr) || (((type) != IP6_UNICAST) && \
00179    (ip6_addr_ismulticast_iflocal(ip6addr) || \
00180     ip6_addr_ismulticast_linklocal(ip6addr))))
00181 
00182 /**
00183  * Assign a zone index to an IPv6 address, based on a network interface. If the
00184  * given address has a scope, the assigned zone index is that scope's zone of
00185  * the given netif; otherwise, the assigned zone index is "no zone".
00186  *
00187  * This default implementation follows the default model of RFC 4007, where
00188  * only interface-local and link-local scopes are defined, and the zone index
00189  * of both of those scopes always equals the index of the network interface.
00190  * As such, this default implementation need not distinguish between different
00191  * constrained scopes when assigning the zone.
00192  *
00193  * @param ip6addr the IPv6 address; its address part is examined, and its zone
00194  *                index is assigned.
00195  * @param type address type; see @ref lwip_ipv6_scope_type.
00196  * @param netif the network interface (const).
00197  */
00198 #define ip6_addr_assign_zone(ip6addr, type, netif) \
00199     (ip6_addr_set_zone((ip6addr), \
00200       ip6_addr_has_scope((ip6addr), (type)) ? netif_get_index(netif) : 0))
00201 
00202 /**
00203  * Test whether an IPv6 address is "zone-compatible" with a network interface.
00204  * That is, test whether the network interface is part of the zone associated
00205  * with the address. For efficiency, this macro is only ever called if the
00206  * given address is either scoped or zoned, and thus, it need not test this.
00207  * If an address is scoped but not zoned, or zoned and not scoped, it is
00208  * considered not zone-compatible with any netif.
00209  *
00210  * This default implementation follows the default model of RFC 4007, where
00211  * only interface-local and link-local scopes are defined, and the zone index
00212  * of both of those scopes always equals the index of the network interface.
00213  * As such, there is always only one matching netif for a specific zone index,
00214  * but all call sites of this macro currently support multiple matching netifs
00215  * as well (at no additional expense in the common case).
00216  *
00217  * @param ip6addr the IPv6 address (const).
00218  * @param netif the network interface (const).
00219  * @return 1 if the address is scope-compatible with the netif, 0 if not.
00220  */
00221 #define ip6_addr_test_zone(ip6addr, netif) \
00222     (ip6_addr_equals_zone((ip6addr), netif_get_index(netif)))
00223 
00224 #endif /* !IPV6_CUSTOM_SCOPES */
00225 
00226 /** Does the given IPv6 address have a scope, and as such should also have a
00227  * zone to be meaningful, but does not actually have a zone? (0/1) */
00228 #define ip6_addr_lacks_zone(ip6addr, type) \
00229     (!ip6_addr_has_zone(ip6addr) && ip6_addr_has_scope((ip6addr), (type)))
00230 
00231 /**
00232  * Try to select a zone for a scoped address that does not yet have a zone.
00233  * Called from PCB bind and connect routines, for two reasons: 1) to save on
00234  * this (relatively expensive) selection for every individual packet route
00235  * operation and 2) to allow the application to obtain the selected zone from
00236  * the PCB as is customary for e.g. getsockname/getpeername BSD socket calls.
00237  *
00238  * Ideally, callers would always supply a properly zoned address, in which case
00239  * this function would not be needed. It exists both for compatibility with the
00240  * BSD socket API (which accepts zoneless destination addresses) and for
00241  * backward compatibility with pre-scoping lwIP code.
00242  *
00243  * It may be impossible to select a zone, e.g. if there are no netifs.  In that
00244  * case, the address's zone field will be left as is.
00245  *
00246  * @param dest the IPv6 address for which to select and set a zone.
00247  * @param src source IPv6 address (const); may be equal to dest.
00248  */
00249 #define ip6_addr_select_zone(dest, src) do { struct netif *selected_netif; \
00250   selected_netif = ip6_route((src), (dest)); \
00251   if (selected_netif != NULL) { \
00252     ip6_addr_assign_zone((dest), IP6_UNKNOWN, selected_netif); \
00253   } } while (0)
00254 
00255 /**
00256  * @}
00257  */
00258 
00259 #else /* LWIP_IPV6_SCOPES */
00260 
00261 #define IPADDR6_ZONE_INIT
00262 #define ip6_addr_zone(ip6addr) (IP6_NO_ZONE)
00263 #define ip6_addr_has_zone(ip6addr) (0)
00264 #define ip6_addr_set_zone(ip6addr, zone_idx)
00265 #define ip6_addr_clear_zone(ip6addr)
00266 #define ip6_addr_copy_zone(ip6addr1, ip6addr2)
00267 #define ip6_addr_equals_zone(ip6addr, zone_idx) (1)
00268 #define ip6_addr_cmp_zone(ip6addr1, ip6addr2) (1)
00269 #define IPV6_CUSTOM_SCOPES 0
00270 #define ip6_addr_has_scope(ip6addr, type) (0)
00271 #define ip6_addr_assign_zone(ip6addr, type, netif)
00272 #define ip6_addr_test_zone(ip6addr, netif) (1)
00273 #define ip6_addr_lacks_zone(ip6addr, type) (0)
00274 #define ip6_addr_select_zone(ip6addr, src)
00275 
00276 #endif /* LWIP_IPV6_SCOPES */
00277 
00278 #if LWIP_IPV6_SCOPES && LWIP_IPV6_SCOPES_DEBUG
00279 
00280 /** Verify that the given IPv6 address is properly zoned. */
00281 #define IP6_ADDR_ZONECHECK(ip6addr) LWIP_ASSERT("IPv6 zone check failed", \
00282     ip6_addr_has_scope(ip6addr, IP6_UNKNOWN) == ip6_addr_has_zone(ip6addr))
00283 
00284 /** Verify that the given IPv6 address is properly zoned for the given netif. */
00285 #define IP6_ADDR_ZONECHECK_NETIF(ip6addr, netif) LWIP_ASSERT("IPv6 netif zone check failed", \
00286     ip6_addr_has_scope(ip6addr, IP6_UNKNOWN) ? \
00287     (ip6_addr_has_zone(ip6addr) && \
00288      (((netif) == NULL) || ip6_addr_test_zone((ip6addr), (netif)))) : \
00289     !ip6_addr_has_zone(ip6addr))
00290 
00291 #else /* LWIP_IPV6_SCOPES && LWIP_IPV6_SCOPES_DEBUG */
00292 
00293 #define IP6_ADDR_ZONECHECK(ip6addr)
00294 #define IP6_ADDR_ZONECHECK_NETIF(ip6addr, netif)
00295 
00296 #endif /* LWIP_IPV6_SCOPES && LWIP_IPV6_SCOPES_DEBUG */
00297 
00298 #endif /* LWIP_IPV6 */
00299 
00300 #ifdef __cplusplus
00301 }
00302 #endif
00303 
00304 #endif /* LWIP_HDR_IP6_ZONE_H */