1 /** 2 * @file 3 * 4 * IPv6 addresses. 5 */ 6 7 /* 8 * Copyright (c) 2010 Inico Technologies Ltd. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without modification, 12 * are permitted provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, 15 * this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright notice, 17 * this list of conditions and the following disclaimer in the documentation 18 * and/or other materials provided with the distribution. 19 * 3. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 31 * OF SUCH DAMAGE. 32 * 33 * This file is part of the lwIP TCP/IP stack. 34 * 35 * Author: Ivan Delamer <delamer@inicotech.com> 36 * 37 * Structs and macros for handling IPv6 addresses. 38 * 39 * Please coordinate changes and requests with Ivan Delamer 40 * <delamer@inicotech.com> 41 */ 42 #ifndef LWIP_HDR_IP6_ADDR_H 43 #define LWIP_HDR_IP6_ADDR_H 44 45 #include "lwip/opt.h" 46 47 #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */ 48 49 50 #ifdef __cplusplus 51 extern "C" { 52 #endif 53 54 55 /** This is the aligned version of ip6_addr_t, 56 used as local variable, on the stack, etc. */ 57 struct ip6_addr { 58 u32_t addr[4]; 59 }; 60 61 /** This is the packed version of ip6_addr_t, 62 used in network headers that are itself packed */ 63 #ifdef PACK_STRUCT_USE_INCLUDES 64 # include "arch/bpstruct.h" 65 #endif 66 PACK_STRUCT_BEGIN 67 struct ip6_addr_packed { 68 PACK_STRUCT_FIELD(u32_t addr[4]); 69 } PACK_STRUCT_STRUCT; 70 PACK_STRUCT_END 71 #ifdef PACK_STRUCT_USE_INCLUDES 72 # include "arch/epstruct.h" 73 #endif 74 75 /** IPv6 address */ 76 typedef struct ip6_addr ip6_addr_t; 77 typedef struct ip6_addr_packed ip6_addr_p_t; 78 79 80 #if BYTE_ORDER == BIG_ENDIAN 81 /** Set an IPv6 partial address given by byte-parts. */ 82 #define IP6_ADDR_PART(ip6addr, index, a,b,c,d) \ 83 (ip6addr)->addr[index] = ((u32_t)((a) & 0xff) << 24) | \ 84 ((u32_t)((b) & 0xff) << 16) | \ 85 ((u32_t)((c) & 0xff) << 8) | \ 86 (u32_t)((d) & 0xff) 87 #else 88 /** Set an IPv6 partial address given by byte-parts. 89 Little-endian version, stored in network order (no lwip_htonl). */ 90 #define IP6_ADDR_PART(ip6addr, index, a,b,c,d) \ 91 (ip6addr)->addr[index] = ((u32_t)((d) & 0xff) << 24) | \ 92 ((u32_t)((c) & 0xff) << 16) | \ 93 ((u32_t)((b) & 0xff) << 8) | \ 94 (u32_t)((a) & 0xff) 95 #endif 96 97 /** Set a full IPv6 address by passing the 4 u32_t indices in network byte order 98 (use PP_HTONL() for constants) */ 99 #define IP6_ADDR(ip6addr, idx0, idx1, idx2, idx3) do { \ 100 (ip6addr)->addr[0] = idx0; \ 101 (ip6addr)->addr[1] = idx1; \ 102 (ip6addr)->addr[2] = idx2; \ 103 (ip6addr)->addr[3] = idx3; } while(0) 104 105 /** Access address in 16-bit block */ 106 #define IP6_ADDR_BLOCK1(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[0]) >> 16) & 0xffff)) 107 /** Access address in 16-bit block */ 108 #define IP6_ADDR_BLOCK2(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[0])) & 0xffff)) 109 /** Access address in 16-bit block */ 110 #define IP6_ADDR_BLOCK3(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[1]) >> 16) & 0xffff)) 111 /** Access address in 16-bit block */ 112 #define IP6_ADDR_BLOCK4(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[1])) & 0xffff)) 113 /** Access address in 16-bit block */ 114 #define IP6_ADDR_BLOCK5(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[2]) >> 16) & 0xffff)) 115 /** Access address in 16-bit block */ 116 #define IP6_ADDR_BLOCK6(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[2])) & 0xffff)) 117 /** Access address in 16-bit block */ 118 #define IP6_ADDR_BLOCK7(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[3]) >> 16) & 0xffff)) 119 /** Access address in 16-bit block */ 120 #define IP6_ADDR_BLOCK8(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[3])) & 0xffff)) 121 122 /** Copy IPv6 address - faster than ip6_addr_set: no NULL check */ 123 #define ip6_addr_copy(dest, src) do{(dest).addr[0] = (src).addr[0]; \ 124 (dest).addr[1] = (src).addr[1]; \ 125 (dest).addr[2] = (src).addr[2]; \ 126 (dest).addr[3] = (src).addr[3];}while(0) 127 /** Safely copy one IPv6 address to another (src may be NULL) */ 128 #define ip6_addr_set(dest, src) do{(dest)->addr[0] = (src) == NULL ? 0 : (src)->addr[0]; \ 129 (dest)->addr[1] = (src) == NULL ? 0 : (src)->addr[1]; \ 130 (dest)->addr[2] = (src) == NULL ? 0 : (src)->addr[2]; \ 131 (dest)->addr[3] = (src) == NULL ? 0 : (src)->addr[3];}while(0) 132 133 /** Set complete address to zero */ 134 #define ip6_addr_set_zero(ip6addr) do{(ip6addr)->addr[0] = 0; \ 135 (ip6addr)->addr[1] = 0; \ 136 (ip6addr)->addr[2] = 0; \ 137 (ip6addr)->addr[3] = 0;}while(0) 138 139 /** Set address to ipv6 'any' (no need for lwip_htonl()) */ 140 #define ip6_addr_set_any(ip6addr) ip6_addr_set_zero(ip6addr) 141 /** Set address to ipv6 loopback address */ 142 #define ip6_addr_set_loopback(ip6addr) do{(ip6addr)->addr[0] = 0; \ 143 (ip6addr)->addr[1] = 0; \ 144 (ip6addr)->addr[2] = 0; \ 145 (ip6addr)->addr[3] = PP_HTONL(0x00000001UL);}while(0) 146 /** Safely copy one IPv6 address to another and change byte order 147 * from host- to network-order. */ 148 #define ip6_addr_set_hton(dest, src) do{(dest)->addr[0] = (src) == NULL ? 0 : lwip_htonl((src)->addr[0]); \ 149 (dest)->addr[1] = (src) == NULL ? 0 : lwip_htonl((src)->addr[1]); \ 150 (dest)->addr[2] = (src) == NULL ? 0 : lwip_htonl((src)->addr[2]); \ 151 (dest)->addr[3] = (src) == NULL ? 0 : lwip_htonl((src)->addr[3]);}while(0) 152 153 154 /** 155 * Determine if two IPv6 address are on the same network. 156 * 157 * @arg addr1 IPv6 address 1 158 * @arg addr2 IPv6 address 2 159 * @return !0 if the network identifiers of both address match 160 */ 161 #define ip6_addr_netcmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \ 162 ((addr1)->addr[1] == (addr2)->addr[1])) 163 164 #define ip6_addr_cmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \ 165 ((addr1)->addr[1] == (addr2)->addr[1]) && \ 166 ((addr1)->addr[2] == (addr2)->addr[2]) && \ 167 ((addr1)->addr[3] == (addr2)->addr[3])) 168 169 #define ip6_get_subnet_id(ip6addr) (lwip_htonl((ip6addr)->addr[2]) & 0x0000ffffUL) 170 171 #define ip6_addr_isany_val(ip6addr) (((ip6addr).addr[0] == 0) && \ 172 ((ip6addr).addr[1] == 0) && \ 173 ((ip6addr).addr[2] == 0) && \ 174 ((ip6addr).addr[3] == 0)) 175 #define ip6_addr_isany(ip6addr) (((ip6addr) == NULL) || ip6_addr_isany_val(*(ip6addr))) 176 177 #define ip6_addr_isloopback(ip6addr) (((ip6addr)->addr[0] == 0UL) && \ 178 ((ip6addr)->addr[1] == 0UL) && \ 179 ((ip6addr)->addr[2] == 0UL) && \ 180 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL))) 181 182 #define ip6_addr_isglobal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xe0000000UL)) == PP_HTONL(0x20000000UL)) 183 184 #define ip6_addr_islinklocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xffc00000UL)) == PP_HTONL(0xfe800000UL)) 185 186 #define ip6_addr_issitelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xffc00000UL)) == PP_HTONL(0xfec00000UL)) 187 188 #define ip6_addr_isuniquelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xfe000000UL)) == PP_HTONL(0xfc000000UL)) 189 190 #define ip6_addr_isipv6mappedipv4(ip6addr) (((ip6addr)->addr[0] == 0) && ((ip6addr)->addr[1] == 0) && (((ip6addr)->addr[2]) == PP_HTONL(0x0000FFFFUL))) 191 192 #define ip6_addr_ismulticast(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff000000UL)) == PP_HTONL(0xff000000UL)) 193 #define ip6_addr_multicast_transient_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00100000UL)) 194 #define ip6_addr_multicast_prefix_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00200000UL)) 195 #define ip6_addr_multicast_rendezvous_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00400000UL)) 196 #define ip6_addr_multicast_scope(ip6addr) ((lwip_htonl((ip6addr)->addr[0]) >> 16) & 0xf) 197 #define IP6_MULTICAST_SCOPE_RESERVED 0x0 198 #define IP6_MULTICAST_SCOPE_RESERVED0 0x0 199 #define IP6_MULTICAST_SCOPE_INTERFACE_LOCAL 0x1 200 #define IP6_MULTICAST_SCOPE_LINK_LOCAL 0x2 201 #define IP6_MULTICAST_SCOPE_RESERVED3 0x3 202 #define IP6_MULTICAST_SCOPE_ADMIN_LOCAL 0x4 203 #define IP6_MULTICAST_SCOPE_SITE_LOCAL 0x5 204 #define IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL 0x8 205 #define IP6_MULTICAST_SCOPE_GLOBAL 0xe 206 #define IP6_MULTICAST_SCOPE_RESERVEDF 0xf 207 #define ip6_addr_ismulticast_iflocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff010000UL)) 208 #define ip6_addr_ismulticast_linklocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff020000UL)) 209 #define ip6_addr_ismulticast_adminlocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff040000UL)) 210 #define ip6_addr_ismulticast_sitelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff050000UL)) 211 #define ip6_addr_ismulticast_orglocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff080000UL)) 212 #define ip6_addr_ismulticast_global(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff0e0000UL)) 213 214 /* @todo define get/set for well-know multicast addresses, e.g. ff02::1 */ 215 #define ip6_addr_isallnodes_iflocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff010000UL)) && \ 216 ((ip6addr)->addr[1] == 0UL) && \ 217 ((ip6addr)->addr[2] == 0UL) && \ 218 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL))) 219 220 #define ip6_addr_isallnodes_linklocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \ 221 ((ip6addr)->addr[1] == 0UL) && \ 222 ((ip6addr)->addr[2] == 0UL) && \ 223 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL))) 224 #define ip6_addr_set_allnodes_linklocal(ip6addr) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \ 225 (ip6addr)->addr[1] = 0; \ 226 (ip6addr)->addr[2] = 0; \ 227 (ip6addr)->addr[3] = PP_HTONL(0x00000001UL);}while(0) 228 229 #define ip6_addr_isallrouters_linklocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \ 230 ((ip6addr)->addr[1] == 0UL) && \ 231 ((ip6addr)->addr[2] == 0UL) && \ 232 ((ip6addr)->addr[3] == PP_HTONL(0x00000002UL))) 233 #define ip6_addr_set_allrouters_linklocal(ip6addr) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \ 234 (ip6addr)->addr[1] = 0; \ 235 (ip6addr)->addr[2] = 0; \ 236 (ip6addr)->addr[3] = PP_HTONL(0x00000002UL);}while(0) 237 238 #define ip6_addr_issolicitednode(ip6addr) ( ((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \ 239 ((ip6addr)->addr[2] == PP_HTONL(0x00000001UL)) && \ 240 (((ip6addr)->addr[3] & PP_HTONL(0xff000000UL)) == PP_HTONL(0xff000000UL)) ) 241 242 #define ip6_addr_set_solicitednode(ip6addr, if_id) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \ 243 (ip6addr)->addr[1] = 0; \ 244 (ip6addr)->addr[2] = PP_HTONL(0x00000001UL); \ 245 (ip6addr)->addr[3] = (PP_HTONL(0xff000000UL) | (if_id));}while(0) 246 247 #define ip6_addr_cmp_solicitednode(ip6addr, sn_addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \ 248 ((ip6addr)->addr[1] == 0) && \ 249 ((ip6addr)->addr[2] == PP_HTONL(0x00000001UL)) && \ 250 ((ip6addr)->addr[3] == (PP_HTONL(0xff000000UL) | (sn_addr)->addr[3]))) 251 252 /* IPv6 address states. */ 253 #define IP6_ADDR_INVALID 0x00 254 #define IP6_ADDR_TENTATIVE 0x08 255 #define IP6_ADDR_TENTATIVE_1 0x09 /* 1 probe sent */ 256 #define IP6_ADDR_TENTATIVE_2 0x0a /* 2 probes sent */ 257 #define IP6_ADDR_TENTATIVE_3 0x0b /* 3 probes sent */ 258 #define IP6_ADDR_TENTATIVE_4 0x0c /* 4 probes sent */ 259 #define IP6_ADDR_TENTATIVE_5 0x0d /* 5 probes sent */ 260 #define IP6_ADDR_TENTATIVE_6 0x0e /* 6 probes sent */ 261 #define IP6_ADDR_TENTATIVE_7 0x0f /* 7 probes sent */ 262 #define IP6_ADDR_VALID 0x10 /* This bit marks an address as valid (preferred or deprecated) */ 263 #define IP6_ADDR_PREFERRED 0x30 264 #define IP6_ADDR_DEPRECATED 0x10 /* Same as VALID (valid but not preferred) */ 265 266 #define IP6_ADDR_TENTATIVE_COUNT_MASK 0x07 /* 1-7 probes sent */ 267 268 #define ip6_addr_isinvalid(addr_state) (addr_state == IP6_ADDR_INVALID) 269 #define ip6_addr_istentative(addr_state) (addr_state & IP6_ADDR_TENTATIVE) 270 #define ip6_addr_isvalid(addr_state) (addr_state & IP6_ADDR_VALID) /* Include valid, preferred, and deprecated. */ 271 #define ip6_addr_ispreferred(addr_state) (addr_state == IP6_ADDR_PREFERRED) 272 #define ip6_addr_isdeprecated(addr_state) (addr_state == IP6_ADDR_DEPRECATED) 273 274 #define ip6_addr_debug_print_parts(debug, a, b, c, d, e, f, g, h) \ 275 LWIP_DEBUGF(debug, ("%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F, \ 276 a, b, c, d, e, f, g, h)) 277 #define ip6_addr_debug_print(debug, ipaddr) \ 278 ip6_addr_debug_print_parts(debug, \ 279 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK1(ipaddr) : 0), \ 280 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK2(ipaddr) : 0), \ 281 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK3(ipaddr) : 0), \ 282 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK4(ipaddr) : 0), \ 283 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK5(ipaddr) : 0), \ 284 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK6(ipaddr) : 0), \ 285 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK7(ipaddr) : 0), \ 286 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK8(ipaddr) : 0)) 287 #define ip6_addr_debug_print_val(debug, ipaddr) \ 288 ip6_addr_debug_print_parts(debug, \ 289 IP6_ADDR_BLOCK1(&(ipaddr)), \ 290 IP6_ADDR_BLOCK2(&(ipaddr)), \ 291 IP6_ADDR_BLOCK3(&(ipaddr)), \ 292 IP6_ADDR_BLOCK4(&(ipaddr)), \ 293 IP6_ADDR_BLOCK5(&(ipaddr)), \ 294 IP6_ADDR_BLOCK6(&(ipaddr)), \ 295 IP6_ADDR_BLOCK7(&(ipaddr)), \ 296 IP6_ADDR_BLOCK8(&(ipaddr))) 297 298 #define IP6ADDR_STRLEN_MAX 46 299 300 int ip6addr_aton(const char *cp, ip6_addr_t *addr); 301 /** returns ptr to static buffer; not reentrant! */ 302 char *ip6addr_ntoa(const ip6_addr_t *addr); 303 char *ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen); 304 305 306 307 #ifdef __cplusplus 308 } 309 #endif 310 311 #endif /* LWIP_IPV6 */ 312 313 #endif /* LWIP_HDR_IP6_ADDR_H */ 314