8 * Copyright (c) 2010 Inico Technologies Ltd.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
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.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Ivan Delamer <delamer@inicotech.com>
37 * Structs and macros for handling IPv6 addresses.
39 * Please coordinate changes and requests with Ivan Delamer
40 * <delamer@inicotech.com>
42 #ifndef LWIP_HDR_IP6_ADDR_H
43 #define LWIP_HDR_IP6_ADDR_H
48 #if LWIP_IPV6 /* don't build if not configured for use in lwipopts.h */
56 /** This is the aligned version of ip6_addr_t,
57 used as local variable, on the stack, etc. */
63 typedef struct ip6_addr ip6_addr_t;
65 /** Set an IPv6 partial address given by byte-parts */
66 #define IP6_ADDR_PART(ip6addr, index, a,b,c,d) \
67 (ip6addr)->addr[index] = PP_HTONL(LWIP_MAKEU32(a,b,c,d))
69 /** Set a full IPv6 address by passing the 4 u32_t indices in network byte order
70 (use PP_HTONL() for constants) */
71 #define IP6_ADDR(ip6addr, idx0, idx1, idx2, idx3) do { \
72 (ip6addr)->addr[0] = idx0; \
73 (ip6addr)->addr[1] = idx1; \
74 (ip6addr)->addr[2] = idx2; \
75 (ip6addr)->addr[3] = idx3; } while(0)
77 /** Access address in 16-bit block */
78 #define IP6_ADDR_BLOCK1(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[0]) >> 16) & 0xffff))
79 /** Access address in 16-bit block */
80 #define IP6_ADDR_BLOCK2(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[0])) & 0xffff))
81 /** Access address in 16-bit block */
82 #define IP6_ADDR_BLOCK3(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[1]) >> 16) & 0xffff))
83 /** Access address in 16-bit block */
84 #define IP6_ADDR_BLOCK4(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[1])) & 0xffff))
85 /** Access address in 16-bit block */
86 #define IP6_ADDR_BLOCK5(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[2]) >> 16) & 0xffff))
87 /** Access address in 16-bit block */
88 #define IP6_ADDR_BLOCK6(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[2])) & 0xffff))
89 /** Access address in 16-bit block */
90 #define IP6_ADDR_BLOCK7(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[3]) >> 16) & 0xffff))
91 /** Access address in 16-bit block */
92 #define IP6_ADDR_BLOCK8(ip6addr) ((u16_t)((lwip_htonl((ip6addr)->addr[3])) & 0xffff))
94 /** Copy IPv6 address - faster than ip6_addr_set: no NULL check */
95 #define ip6_addr_copy(dest, src) do{(dest).addr[0] = (src).addr[0]; \
96 (dest).addr[1] = (src).addr[1]; \
97 (dest).addr[2] = (src).addr[2]; \
98 (dest).addr[3] = (src).addr[3];}while(0)
99 /** Safely copy one IPv6 address to another (src may be NULL) */
100 #define ip6_addr_set(dest, src) do{(dest)->addr[0] = (src) == NULL ? 0 : (src)->addr[0]; \
101 (dest)->addr[1] = (src) == NULL ? 0 : (src)->addr[1]; \
102 (dest)->addr[2] = (src) == NULL ? 0 : (src)->addr[2]; \
103 (dest)->addr[3] = (src) == NULL ? 0 : (src)->addr[3];}while(0)
105 /** Set complete address to zero */
106 #define ip6_addr_set_zero(ip6addr) do{(ip6addr)->addr[0] = 0; \
107 (ip6addr)->addr[1] = 0; \
108 (ip6addr)->addr[2] = 0; \
109 (ip6addr)->addr[3] = 0;}while(0)
111 /** Set address to ipv6 'any' (no need for lwip_htonl()) */
112 #define ip6_addr_set_any(ip6addr) ip6_addr_set_zero(ip6addr)
113 /** Set address to ipv6 loopback address */
114 #define ip6_addr_set_loopback(ip6addr) do{(ip6addr)->addr[0] = 0; \
115 (ip6addr)->addr[1] = 0; \
116 (ip6addr)->addr[2] = 0; \
117 (ip6addr)->addr[3] = PP_HTONL(0x00000001UL);}while(0)
118 /** Safely copy one IPv6 address to another and change byte order
119 * from host- to network-order. */
120 #define ip6_addr_set_hton(dest, src) do{(dest)->addr[0] = (src) == NULL ? 0 : lwip_htonl((src)->addr[0]); \
121 (dest)->addr[1] = (src) == NULL ? 0 : lwip_htonl((src)->addr[1]); \
122 (dest)->addr[2] = (src) == NULL ? 0 : lwip_htonl((src)->addr[2]); \
123 (dest)->addr[3] = (src) == NULL ? 0 : lwip_htonl((src)->addr[3]);}while(0)
127 * Determine if two IPv6 address are on the same network.
129 * @arg addr1 IPv6 address 1
130 * @arg addr2 IPv6 address 2
131 * @return !0 if the network identifiers of both address match
133 #define ip6_addr_netcmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \
134 ((addr1)->addr[1] == (addr2)->addr[1]))
136 #define ip6_addr_net48cmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \
137 (((addr1)->addr[1] & PP_HTONL(0xffff0000UL)) == \
138 ((addr2)->addr[1] & PP_HTONL(0xffff0000UL))))
140 /* Exact-host comparison *after* ip6_addr_netcmp() succeeded, for efficiency. */
141 #define ip6_addr_nethostcmp(addr1, addr2) (((addr1)->addr[2] == (addr2)->addr[2]) && \
142 ((addr1)->addr[3] == (addr2)->addr[3]))
144 #define ip6_addr_cmp(addr1, addr2) (((addr1)->addr[0] == (addr2)->addr[0]) && \
145 ((addr1)->addr[1] == (addr2)->addr[1]) && \
146 ((addr1)->addr[2] == (addr2)->addr[2]) && \
147 ((addr1)->addr[3] == (addr2)->addr[3]))
149 #define ip6_get_subnet_id(ip6addr) (lwip_htonl((ip6addr)->addr[1]) & 0x0000ffffUL)
151 #define ip6_addr_isany_val(ip6addr) (((ip6addr).addr[0] == 0) && \
152 ((ip6addr).addr[1] == 0) && \
153 ((ip6addr).addr[2] == 0) && \
154 ((ip6addr).addr[3] == 0))
155 #define ip6_addr_isany(ip6addr) (((ip6addr) == NULL) || ip6_addr_isany_val(*(ip6addr)))
157 #define ip6_addr_isloopback(ip6addr) (((ip6addr)->addr[0] == 0UL) && \
158 ((ip6addr)->addr[1] == 0UL) && \
159 ((ip6addr)->addr[2] == 0UL) && \
160 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL)))
162 #define ip6_addr_isglobal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xe0000000UL)) == PP_HTONL(0x20000000UL))
164 #define ip6_addr_islinklocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xffc00000UL)) == PP_HTONL(0xfe800000UL))
166 #define ip6_addr_issitelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xffc00000UL)) == PP_HTONL(0xfec00000UL))
168 #define ip6_addr_isuniquelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xfe000000UL)) == PP_HTONL(0xfc000000UL))
170 #define ip6_addr_isipv4mappedipv6(ip6addr) (((ip6addr)->addr[0] == 0) && ((ip6addr)->addr[1] == 0) && (((ip6addr)->addr[2]) == PP_HTONL(0x0000FFFFUL)))
172 #define ip6_addr_ismulticast(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff000000UL)) == PP_HTONL(0xff000000UL))
173 #define ip6_addr_multicast_transient_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00100000UL))
174 #define ip6_addr_multicast_prefix_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00200000UL))
175 #define ip6_addr_multicast_rendezvous_flag(ip6addr) ((ip6addr)->addr[0] & PP_HTONL(0x00400000UL))
176 #define ip6_addr_multicast_scope(ip6addr) ((lwip_htonl((ip6addr)->addr[0]) >> 16) & 0xf)
177 #define IP6_MULTICAST_SCOPE_RESERVED 0x0
178 #define IP6_MULTICAST_SCOPE_RESERVED0 0x0
179 #define IP6_MULTICAST_SCOPE_INTERFACE_LOCAL 0x1
180 #define IP6_MULTICAST_SCOPE_LINK_LOCAL 0x2
181 #define IP6_MULTICAST_SCOPE_RESERVED3 0x3
182 #define IP6_MULTICAST_SCOPE_ADMIN_LOCAL 0x4
183 #define IP6_MULTICAST_SCOPE_SITE_LOCAL 0x5
184 #define IP6_MULTICAST_SCOPE_ORGANIZATION_LOCAL 0x8
185 #define IP6_MULTICAST_SCOPE_GLOBAL 0xe
186 #define IP6_MULTICAST_SCOPE_RESERVEDF 0xf
187 #define ip6_addr_ismulticast_iflocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff010000UL))
188 #define ip6_addr_ismulticast_linklocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff020000UL))
189 #define ip6_addr_ismulticast_adminlocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff040000UL))
190 #define ip6_addr_ismulticast_sitelocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff050000UL))
191 #define ip6_addr_ismulticast_orglocal(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff080000UL))
192 #define ip6_addr_ismulticast_global(ip6addr) (((ip6addr)->addr[0] & PP_HTONL(0xff8f0000UL)) == PP_HTONL(0xff0e0000UL))
194 /* @todo define get/set for well-know multicast addresses, e.g. ff02::1 */
195 #define ip6_addr_isallnodes_iflocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff010000UL)) && \
196 ((ip6addr)->addr[1] == 0UL) && \
197 ((ip6addr)->addr[2] == 0UL) && \
198 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL)))
200 #define ip6_addr_isallnodes_linklocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \
201 ((ip6addr)->addr[1] == 0UL) && \
202 ((ip6addr)->addr[2] == 0UL) && \
203 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL)))
204 #define ip6_addr_isallnodes_networklocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff030000UL)) && \
205 ((ip6addr)->addr[1] == 0UL) && \
206 ((ip6addr)->addr[2] == 0UL) && \
207 ((ip6addr)->addr[3] == PP_HTONL(0x00000001UL)))
208 #define ip6_addr_set_allnodes_linklocal(ip6addr) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \
209 (ip6addr)->addr[1] = 0; \
210 (ip6addr)->addr[2] = 0; \
211 (ip6addr)->addr[3] = PP_HTONL(0x00000001UL);}while(0)
213 #define ip6_addr_isallrouters_linklocal(ip6addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \
214 ((ip6addr)->addr[1] == 0UL) && \
215 ((ip6addr)->addr[2] == 0UL) && \
216 ((ip6addr)->addr[3] == PP_HTONL(0x00000002UL)))
217 #define ip6_addr_set_allrouters_linklocal(ip6addr) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \
218 (ip6addr)->addr[1] = 0; \
219 (ip6addr)->addr[2] = 0; \
220 (ip6addr)->addr[3] = PP_HTONL(0x00000002UL);}while(0)
222 #define ip6_addr_issolicitednode(ip6addr) ( ((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \
223 ((ip6addr)->addr[2] == PP_HTONL(0x00000001UL)) && \
224 (((ip6addr)->addr[3] & PP_HTONL(0xff000000UL)) == PP_HTONL(0xff000000UL)) )
226 #define ip6_addr_set_solicitednode(ip6addr, if_id) do{(ip6addr)->addr[0] = PP_HTONL(0xff020000UL); \
227 (ip6addr)->addr[1] = 0; \
228 (ip6addr)->addr[2] = PP_HTONL(0x00000001UL); \
229 (ip6addr)->addr[3] = (PP_HTONL(0xff000000UL) | (if_id));}while(0)
231 #define ip6_addr_cmp_solicitednode(ip6addr, sn_addr) (((ip6addr)->addr[0] == PP_HTONL(0xff020000UL)) && \
232 ((ip6addr)->addr[1] == 0) && \
233 ((ip6addr)->addr[2] == PP_HTONL(0x00000001UL)) && \
234 ((ip6addr)->addr[3] == (PP_HTONL(0xff000000UL) | (sn_addr)->addr[3])))
236 /* IPv6 address states. */
237 #define IP6_ADDR_INVALID 0x00
238 #define IP6_ADDR_TENTATIVE 0x08
239 #define IP6_ADDR_TENTATIVE_1 0x09 /* 1 probe sent */
240 #define IP6_ADDR_TENTATIVE_2 0x0a /* 2 probes sent */
241 #define IP6_ADDR_TENTATIVE_3 0x0b /* 3 probes sent */
242 #define IP6_ADDR_TENTATIVE_4 0x0c /* 4 probes sent */
243 #define IP6_ADDR_TENTATIVE_5 0x0d /* 5 probes sent */
244 #define IP6_ADDR_TENTATIVE_6 0x0e /* 6 probes sent */
245 #define IP6_ADDR_TENTATIVE_7 0x0f /* 7 probes sent */
246 #define IP6_ADDR_VALID 0x10 /* This bit marks an address as valid (preferred or deprecated) */
247 #define IP6_ADDR_PREFERRED 0x30
248 #define IP6_ADDR_DEPRECATED 0x10 /* Same as VALID (valid but not preferred) */
249 #define IP6_ADDR_DUPLICATED 0x40 /* Failed DAD test, not valid */
251 #define IP6_ADDR_TENTATIVE_COUNT_MASK 0x07 /* 1-7 probes sent */
253 #define ip6_addr_isinvalid(addr_state) (addr_state == IP6_ADDR_INVALID)
254 #define ip6_addr_istentative(addr_state) (addr_state & IP6_ADDR_TENTATIVE)
255 #define ip6_addr_isvalid(addr_state) (addr_state & IP6_ADDR_VALID) /* Include valid, preferred, and deprecated. */
256 #define ip6_addr_ispreferred(addr_state) (addr_state == IP6_ADDR_PREFERRED)
257 #define ip6_addr_isdeprecated(addr_state) (addr_state == IP6_ADDR_DEPRECATED)
258 #define ip6_addr_isduplicated(addr_state) (addr_state == IP6_ADDR_DUPLICATED)
260 #if LWIP_IPV6_ADDRESS_LIFETIMES
261 #define IP6_ADDR_LIFE_STATIC (0)
262 #define IP6_ADDR_LIFE_INFINITE (0xffffffffUL)
263 #define ip6_addr_life_isstatic(addr_life) ((addr_life) == IP6_ADDR_LIFE_STATIC)
264 #define ip6_addr_life_isinfinite(addr_life) ((addr_life) == IP6_ADDR_LIFE_INFINITE)
265 #endif /* LWIP_IPV6_ADDRESS_LIFETIMES */
267 #define ip6_addr_debug_print_parts(debug, a, b, c, d, e, f, g, h) \
268 LWIP_DEBUGF(debug, ("%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F ":%" X16_F, \
269 a, b, c, d, e, f, g, h))
270 #define ip6_addr_debug_print(debug, ipaddr) \
271 ip6_addr_debug_print_parts(debug, \
272 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK1(ipaddr) : 0), \
273 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK2(ipaddr) : 0), \
274 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK3(ipaddr) : 0), \
275 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK4(ipaddr) : 0), \
276 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK5(ipaddr) : 0), \
277 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK6(ipaddr) : 0), \
278 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK7(ipaddr) : 0), \
279 (u16_t)((ipaddr) != NULL ? IP6_ADDR_BLOCK8(ipaddr) : 0))
280 #define ip6_addr_debug_print_val(debug, ipaddr) \
281 ip6_addr_debug_print_parts(debug, \
282 IP6_ADDR_BLOCK1(&(ipaddr)), \
283 IP6_ADDR_BLOCK2(&(ipaddr)), \
284 IP6_ADDR_BLOCK3(&(ipaddr)), \
285 IP6_ADDR_BLOCK4(&(ipaddr)), \
286 IP6_ADDR_BLOCK5(&(ipaddr)), \
287 IP6_ADDR_BLOCK6(&(ipaddr)), \
288 IP6_ADDR_BLOCK7(&(ipaddr)), \
289 IP6_ADDR_BLOCK8(&(ipaddr)))
291 #define IP6ADDR_STRLEN_MAX 46
293 int ip6addr_aton(const char *cp, ip6_addr_t *addr);
294 /** returns ptr to static buffer; not reentrant! */
295 char *ip6addr_ntoa(const ip6_addr_t *addr);
296 char *ip6addr_ntoa_r(const ip6_addr_t *addr, char *buf, int buflen);
304 #endif /* LWIP_IPV6 */
306 #endif /* LWIP_HDR_IP6_ADDR_H */