]>
git.ipfire.org Git - thirdparty/bird.git/blob - lib/ip.h
2 * BIRD Internet Routing Daemon -- The Internet Protocol
4 * (c) 1998 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
12 #include "lib/endian.h"
13 #include "lib/string.h"
14 #include "lib/bitops.h"
15 #include "lib/unaligned.h"
18 #define IP4_OSPF_ALL_ROUTERS ipa_build4(224, 0, 0, 5)
19 #define IP4_OSPF_DES_ROUTERS ipa_build4(224, 0, 0, 6)
21 #define IP6_ALL_NODES ipa_build6(0xFF020000, 0, 0, 1)
22 #define IP6_ALL_ROUTERS ipa_build6(0xFF020000, 0, 0, 2)
23 #define IP6_OSPF_ALL_ROUTERS ipa_build6(0xFF020000, 0, 0, 5)
24 #define IP6_OSPF_DES_ROUTERS ipa_build6(0xFF020000, 0, 0, 6)
25 #define IP6_RIP_ROUTERS ipa_build6(0xFF020000, 0, 0, 9)
27 #define IP4_NONE _MI4(0)
28 #define IP6_NONE _MI6(0,0,0,0)
30 #define IP4_MIN_MTU 576
31 #define IP6_MIN_MTU 1280
33 #define IP_PREC_INTERNET_CONTROL 0xc0
37 #define MAX_PREFIX_LENGTH 128
38 #define BITS_PER_IP_ADDRESS 128
39 #define STD_ADDRESS_P_LENGTH 39
40 #define SIZE_OF_IP_HEADER 40
42 #define MAX_PREFIX_LENGTH 32
43 #define BITS_PER_IP_ADDRESS 32
44 #define STD_ADDRESS_P_LENGTH 15
45 #define SIZE_OF_IP_HEADER 24
51 typedef struct ip4_addr
{
55 #define _MI4(x) ((struct ip4_addr) { x })
56 #define _I(x) (x).addr
68 typedef struct ip6_addr
{
72 #define _MI6(a,b,c,d) ((struct ip6_addr) {{ a, b, c, d }})
73 #define _I0(a) ((a).addr[0])
74 #define _I1(a) ((a).addr[1])
75 #define _I2(a) ((a).addr[2])
76 #define _I3(a) ((a).addr[3])
81 /* Structure ip_addr may contain both IPv4 and IPv6 addresses */
82 typedef ip6_addr ip_addr
;
83 #define IPA_NONE IP6_NONE
85 #define ipa_from_ip4(x) _MI6(0,0,0xffff,_I(x))
86 #define ipa_from_ip6(x) x
87 #define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
89 #define ipa_to_ip4(x) _MI4(_I3(x))
90 #define ipa_to_ip6(x) x
91 #define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
93 #define ipa_is_ip4(a) ip6_is_v4mapped(a)
97 /* Provisionary ip_addr definition same as ip4_addr */
98 typedef ip4_addr ip_addr
;
99 #define IPA_NONE IP4_NONE
101 #define ipa_from_ip4(x) x
102 #define ipa_from_ip6(x) IPA_NONE
103 #define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
105 #define ipa_to_ip4(x) x
106 #define ipa_to_ip6(x) IP6_NONE
107 #define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
109 #define ipa_is_ip4(a) 1
115 * Public constructors
118 #define ip4_from_u32(x) _MI4(x)
119 #define ip4_to_u32(x) _I(x)
121 #define ip4_build(a,b,c,d) _MI4(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
122 #define ip6_build(a,b,c,d) _MI6(a,b,c,d)
124 #define ipa_build4(a,b,c,d) ipa_from_ip4(ip4_build(a,b,c,d))
125 #define ipa_build6(a,b,c,d) ipa_from_ip6(ip6_build(a,b,c,d))
129 * Basic algebraic functions
132 static inline int ip4_equal(ip4_addr a
, ip4_addr b
)
133 { return _I(a
) == _I(b
); }
135 static inline int ip4_zero(ip4_addr a
)
136 { return _I(a
) == 0; }
138 static inline int ip4_nonzero(ip4_addr a
)
139 { return _I(a
) != 0; }
141 static inline ip4_addr
ip4_and(ip4_addr a
, ip4_addr b
)
142 { return _MI4(_I(a
) & _I(b
)); }
144 static inline ip4_addr
ip4_or(ip4_addr a
, ip4_addr b
)
145 { return _MI4(_I(a
) | _I(b
)); }
147 static inline ip4_addr
ip4_xor(ip4_addr a
, ip4_addr b
)
148 { return _MI4(_I(a
) ^ _I(b
)); }
150 static inline ip4_addr
ip4_not(ip4_addr a
)
151 { return _MI4(~_I(a
)); }
154 static inline int ip6_equal(ip6_addr a
, ip6_addr b
)
155 { return _I0(a
) == _I0(b
) && _I1(a
) == _I1(b
) && _I2(a
) == _I2(b
) && _I3(a
) == _I3(b
); }
157 static inline int ip6_zero(ip6_addr a
)
158 { return !_I0(a
) && !_I1(a
) && !_I2(a
) && !_I3(a
); }
160 static inline int ip6_nonzero(ip6_addr a
)
161 { return _I0(a
) || _I1(a
) || _I2(a
) || _I3(a
); }
163 static inline ip6_addr
ip6_and(ip6_addr a
, ip6_addr b
)
164 { return _MI6(_I0(a
) & _I0(b
), _I1(a
) & _I1(b
), _I2(a
) & _I2(b
), _I3(a
) & _I3(b
)); }
166 static inline ip6_addr
ip6_or(ip6_addr a
, ip6_addr b
)
167 { return _MI6(_I0(a
) | _I0(b
), _I1(a
) | _I1(b
), _I2(a
) | _I2(b
), _I3(a
) | _I3(b
)); }
169 static inline ip6_addr
ip6_xor(ip6_addr a
, ip6_addr b
)
170 { return _MI6(_I0(a
) ^ _I0(b
), _I1(a
) ^ _I1(b
), _I2(a
) ^ _I2(b
), _I3(a
) ^ _I3(b
)); }
172 static inline ip6_addr
ip6_not(ip6_addr a
)
173 { return _MI6(~_I0(a
), ~_I1(a
), ~_I2(a
), ~_I3(a
)); }
177 #define ipa_equal(x,y) ip6_equal(x,y)
178 #define ipa_zero(x) ip6_zero(x)
179 #define ipa_nonzero(x) ip6_nonzero(x)
180 #define ipa_and(x,y) ip6_and(x,y)
181 #define ipa_or(x,y) ip6_or(x,y)
182 #define ipa_xor(x,y) ip6_xor(x,y)
183 #define ipa_not(x) ip6_not(x)
185 #define ipa_equal(x,y) ip4_equal(x,y)
186 #define ipa_zero(x) ip4_zero(x)
187 #define ipa_nonzero(x) ip4_nonzero(x)
188 #define ipa_and(x,y) ip4_and(x,y)
189 #define ipa_or(x,y) ip4_or(x,y)
190 #define ipa_xor(x,y) ip4_xor(x,y)
191 #define ipa_not(x) ip4_not(x)
198 * A zero address is either a token for invalid/unused, or the prefix of default
199 * routes. These functions should be used in the second case, where both IPv4
200 * and IPv6 zero addresses should be checked.
203 static inline int ipa_zero2(ip_addr a
)
204 { return !_I0(a
) && !_I1(a
) && ((_I2(a
) == 0) || (_I2(a
) == 0xffff)) && !_I3(a
); }
206 static inline int ipa_nonzero2(ip_addr a
)
207 { return _I0(a
) || _I1(a
) || ((_I2(a
) != 0) && (_I2(a
) != 0xffff)) || _I3(a
); }
210 #define ipa_zero2(x) ip4_zero(x)
211 #define ipa_nonzero2(x) ip4_nonzero(x)
216 * Hash and compare functions
219 static inline uint
ip4_hash(ip4_addr a
)
221 /* Returns a 16-bit value */
228 static inline u32
ip4_hash32(ip4_addr a
)
230 /* Returns a 32-bit value, although low-order bits are not mixed */
237 static inline uint
ip6_hash(ip6_addr a
)
239 /* Returns a 16-bit hash key */
240 u32 x
= _I0(a
) ^ _I1(a
) ^ _I2(a
) ^ _I3(a
);
241 return (x
^ (x
>> 16) ^ (x
>> 8)) & 0xffff;
244 static inline u32
ip6_hash32(ip6_addr a
)
246 /* Returns a 32-bit hash key, although low-order bits are not mixed */
247 u32 x
= _I0(a
) ^ _I1(a
) ^ _I2(a
) ^ _I3(a
);
248 return x
^ (x
<< 16) ^ (x
<< 24);
251 static inline int ip4_compare(ip4_addr a
, ip4_addr b
)
252 { return (_I(a
) > _I(b
)) - (_I(a
) < _I(b
)); }
254 int ip6_compare(ip6_addr a
, ip6_addr b
);
258 #define ipa_hash(x) ip6_hash(x)
259 #define ipa_hash32(x) ip6_hash32(x)
260 #define ipa_compare(x,y) ip6_compare(x,y)
262 #define ipa_hash(x) ip4_hash(x)
263 #define ipa_hash32(x) ip4_hash32(x)
264 #define ipa_compare(x,y) ip4_compare(x,y)
269 * IP address classification
273 #define IADDR_INVALID -1
274 #define IADDR_SCOPE_MASK 0xfff
275 #define IADDR_HOST 0x1000
276 #define IADDR_BROADCAST 0x2000
277 #define IADDR_MULTICAST 0x4000
283 #define SCOPE_ORGANIZATION 3
284 #define SCOPE_UNIVERSE 4
285 #define SCOPE_UNDEFINED 5
287 int ip4_classify(ip4_addr ad
);
288 int ip6_classify(ip6_addr
*a
);
290 static inline int ip6_is_link_local(ip6_addr a
)
291 { return (_I0(a
) & 0xffc00000) == 0xfe800000; }
293 static inline int ip6_is_v4mapped(ip6_addr a
)
294 { return _I0(a
) == 0 && _I1(a
) == 0 && _I2(a
) == 0xffff; }
297 #define ipa_classify(x) ip6_classify(&(x))
298 #define ipa_is_link_local(x) ip6_is_link_local(x)
300 #define ipa_classify(x) ip4_classify(x)
301 #define ipa_is_link_local(x) 0
304 static inline int ipa_classify_net(ip_addr a
)
305 { return ipa_zero2(a
) ? (IADDR_HOST
| SCOPE_UNIVERSE
) : ipa_classify(a
); }
309 * Miscellaneous IP prefix manipulation
312 static inline ip4_addr
ip4_mkmask(uint n
)
313 { return _MI4(u32_mkmask(n
)); }
315 static inline int ip4_masklen(ip4_addr a
)
316 { return u32_masklen(_I(a
)); }
318 ip6_addr
ip6_mkmask(uint n
);
319 int ip6_masklen(ip6_addr
*a
);
321 /* ipX_pxlen() requires that x != y */
322 static inline uint
ip4_pxlen(ip4_addr a
, ip4_addr b
)
323 { return 31 - u32_log2(_I(a
) ^ _I(b
)); }
325 static inline uint
ip6_pxlen(ip6_addr a
, ip6_addr b
)
328 i
+= (a
.addr
[i
] == b
.addr
[i
]);
329 i
+= (a
.addr
[i
] == b
.addr
[i
]);
330 i
+= (a
.addr
[i
] == b
.addr
[i
]);
331 i
+= (a
.addr
[i
] == b
.addr
[i
]);
332 return 32 * i
+ 31 - u32_log2(a
.addr
[i
] ^ b
.addr
[i
]);
335 static inline u32
ip4_getbit(ip4_addr a
, uint pos
)
336 { return _I(a
) & (0x80000000 >> pos
); }
338 static inline u32
ip6_getbit(ip6_addr a
, uint pos
)
339 { return a
.addr
[pos
/ 32] & (0x80000000 >> (pos
% 32)); }
341 static inline ip4_addr
ip4_opposite_m1(ip4_addr a
)
342 { return _MI4(_I(a
) ^ 1); }
344 static inline ip4_addr
ip4_opposite_m2(ip4_addr a
)
345 { return _MI4(_I(a
) ^ 3); }
347 static inline ip6_addr
ip6_opposite_m1(ip6_addr a
)
348 { return _MI6(_I0(a
), _I1(a
), _I2(a
), _I3(a
) ^ 1); }
350 static inline ip6_addr
ip6_opposite_m2(ip6_addr a
)
351 { return _MI6(_I0(a
), _I1(a
), _I2(a
), _I3(a
) ^ 3); }
353 ip4_addr
ip4_class_mask(ip4_addr ad
);
356 #define ipa_mkmask(x) ip6_mkmask(x)
357 #define ipa_masklen(x) ip6_masklen(&x)
358 #define ipa_pxlen(x,y) ip6_pxlen(x,y)
359 #define ipa_getbit(x,n) ip6_getbit(x,n)
360 #define ipa_opposite_m1(x) ip6_opposite_m1(x)
361 #define ipa_opposite_m2(x) ip6_opposite_m2(x)
363 #define ipa_mkmask(x) ip4_mkmask(x)
364 #define ipa_masklen(x) ip4_masklen(x)
365 #define ipa_pxlen(x,y) ip4_pxlen(x,y)
366 #define ipa_getbit(x,n) ip4_getbit(x,n)
367 #define ipa_opposite_m1(x) ip4_opposite_m1(x)
368 #define ipa_opposite_m2(x) ip4_opposite_m2(x)
373 * Host/network order conversions
376 static inline ip4_addr
ip4_hton(ip4_addr a
)
377 { return _MI4(htonl(_I(a
))); }
379 static inline ip4_addr
ip4_ntoh(ip4_addr a
)
380 { return _MI4(ntohl(_I(a
))); }
382 static inline ip6_addr
ip6_hton(ip6_addr a
)
383 { return _MI6(htonl(_I0(a
)), htonl(_I1(a
)), htonl(_I2(a
)), htonl(_I3(a
))); }
385 static inline ip6_addr
ip6_ntoh(ip6_addr a
)
386 { return _MI6(ntohl(_I0(a
)), ntohl(_I1(a
)), ntohl(_I2(a
)), ntohl(_I3(a
))); }
389 #define ipa_hton(x) x = ip6_hton(x)
390 #define ipa_ntoh(x) x = ip6_ntoh(x)
392 #define ipa_hton(x) x = ip4_hton(x)
393 #define ipa_ntoh(x) x = ip4_ntoh(x)
398 * Unaligned data access (in network order)
401 static inline ip4_addr
get_ip4(void *buf
)
403 return _MI4(get_u32(buf
));
406 static inline ip6_addr
get_ip6(void *buf
)
413 static inline void * put_ip4(void *buf
, ip4_addr a
)
419 static inline void * put_ip6(void *buf
, ip6_addr a
)
426 // XXXX these functions must be redesigned or removed
428 #define get_ipa(x) get_ip6(x)
429 #define put_ipa(x,y) put_ip6(x,y)
431 #define get_ipa(x) get_ip4(x)
432 #define put_ipa(x,y) put_ip4(x,y)
437 * Binary/text form conversions
440 char *ip4_ntop(ip4_addr a
, char *b
);
441 char *ip6_ntop(ip6_addr a
, char *b
);
443 static inline char * ip4_ntox(ip4_addr a
, char *b
)
444 { return b
+ bsprintf(b
, "%08x", _I(a
)); }
446 static inline char * ip6_ntox(ip6_addr a
, char *b
)
447 { return b
+ bsprintf(b
, "%08x.%08x.%08x.%08x", _I0(a
), _I1(a
), _I2(a
), _I3(a
)); }
449 int ip4_pton(char *a
, ip4_addr
*o
);
450 int ip6_pton(char *a
, ip6_addr
*o
);
452 // XXXX these functions must be redesigned or removed
454 #define ipa_ntop(x,y) ip6_ntop(x,y)
455 #define ipa_ntox(x,y) ip6_ntox(x,y)
456 #define ipa_pton(x,y) ip6_pton(x,y)
458 #define ipa_ntop(x,y) ip4_ntop(x,y)
459 #define ipa_ntox(x,y) ip4_ntox(x,y)
460 #define ipa_pton(x,y) ip4_pton(x,y)
470 #define ip_is_prefix(a,l) (!ipa_nonzero(ipa_and(a, ipa_not(ipa_mkmask(l)))))
471 #define ipa_in_net(x,n,p) (ipa_zero(ipa_and(ipa_xor((n),(x)),ipa_mkmask(p))))
472 #define net_in_net(n1,l1,n2,l2) (((l1) >= (l2)) && (ipa_zero(ipa_and(ipa_xor((n1),(n2)),ipa_mkmask(l2)))))
474 char *ip_scope_text(uint
);