]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/ip.h
unsigned [int] -> uint
[thirdparty/bird.git] / lib / ip.h
1 /*
2 * BIRD Internet Routing Daemon -- The Internet Protocol
3 *
4 * (c) 1998 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #ifndef _BIRD_IP_H_
10 #define _BIRD_IP_H_
11
12 #include "lib/endian.h"
13 #include "lib/string.h"
14 #include "lib/bitops.h"
15 #include "lib/unaligned.h"
16
17
18 #define IP4_OSPF_ALL_ROUTERS ipa_build4(224, 0, 0, 5)
19 #define IP4_OSPF_DES_ROUTERS ipa_build4(224, 0, 0, 6)
20
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)
26
27 #define IP4_NONE _MI4(0)
28 #define IP6_NONE _MI6(0,0,0,0)
29
30 #define IP4_MIN_MTU 576
31 #define IP6_MIN_MTU 1280
32
33 #define IP_PREC_INTERNET_CONTROL 0xc0
34
35
36 #ifdef IPV6
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
41 #else
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
46 #endif
47
48
49 #ifdef DEBUGGING
50
51 typedef struct ip4_addr {
52 u32 addr;
53 } ip4_addr;
54
55 #define _MI4(x) ((struct ip4_addr) { x })
56 #define _I(x) (x).addr
57
58 #else
59
60 typedef u32 ip4_addr;
61
62 #define _MI4(x) (x)
63 #define _I(x) (x)
64
65 #endif
66
67
68 typedef struct ip6_addr {
69 u32 addr[4];
70 } ip6_addr;
71
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])
77
78
79 #ifdef IPV6
80
81 /* Structure ip_addr may contain both IPv4 and IPv6 addresses */
82 typedef ip6_addr ip_addr;
83 #define IPA_NONE IP6_NONE
84
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))
88
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))
92
93 #define ipa_is_ip4(a) ip6_is_v4mapped(a)
94
95 #else
96
97 /* Provisionary ip_addr definition same as ip4_addr */
98 typedef ip4_addr ip_addr;
99 #define IPA_NONE IP4_NONE
100
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))
104
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))
108
109 #define ipa_is_ip4(a) 1
110
111 #endif
112
113
114 /*
115 * Public constructors
116 */
117
118 #define ip4_from_u32(x) _MI4(x)
119 #define ip4_to_u32(x) _I(x)
120
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)
123
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))
126
127
128 /*
129 * Basic algebraic functions
130 */
131
132 static inline int ip4_equal(ip4_addr a, ip4_addr b)
133 { return _I(a) == _I(b); }
134
135 static inline int ip4_zero(ip4_addr a)
136 { return _I(a) == 0; }
137
138 static inline int ip4_nonzero(ip4_addr a)
139 { return _I(a) != 0; }
140
141 static inline ip4_addr ip4_and(ip4_addr a, ip4_addr b)
142 { return _MI4(_I(a) & _I(b)); }
143
144 static inline ip4_addr ip4_or(ip4_addr a, ip4_addr b)
145 { return _MI4(_I(a) | _I(b)); }
146
147 static inline ip4_addr ip4_xor(ip4_addr a, ip4_addr b)
148 { return _MI4(_I(a) ^ _I(b)); }
149
150 static inline ip4_addr ip4_not(ip4_addr a)
151 { return _MI4(~_I(a)); }
152
153
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); }
156
157 static inline int ip6_zero(ip6_addr a)
158 { return !_I0(a) && !_I1(a) && !_I2(a) && !_I3(a); }
159
160 static inline int ip6_nonzero(ip6_addr a)
161 { return _I0(a) || _I1(a) || _I2(a) || _I3(a); }
162
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)); }
165
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)); }
168
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)); }
171
172 static inline ip6_addr ip6_not(ip6_addr a)
173 { return _MI6(~_I0(a), ~_I1(a), ~_I2(a), ~_I3(a)); }
174
175
176 #ifdef IPV6
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)
184 #else
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)
192 #endif
193
194
195
196 #ifdef IPV6
197 /*
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.
201 */
202
203 static inline int ipa_zero2(ip_addr a)
204 { return !_I0(a) && !_I1(a) && ((_I2(a) == 0) || (_I2(a) == 0xffff)) && !_I3(a); }
205
206 static inline int ipa_nonzero2(ip_addr a)
207 { return _I0(a) || _I1(a) || ((_I2(a) != 0) && (_I2(a) != 0xffff)) || _I3(a); }
208
209 #else
210 #define ipa_zero2(x) ip4_zero(x)
211 #define ipa_nonzero2(x) ip4_nonzero(x)
212 #endif
213
214
215 /*
216 * Hash and compare functions
217 */
218
219 static inline uint ip4_hash(ip4_addr a)
220 {
221 /* Returns a 16-bit value */
222 u32 x = _I(a);
223 x ^= x >> 16;
224 x ^= x << 10;
225 return x & 0xffff;
226 }
227
228 static inline u32 ip4_hash32(ip4_addr a)
229 {
230 /* Returns a 32-bit value, although low-order bits are not mixed */
231 u32 x = _I(a);
232 x ^= x << 16;
233 x ^= x << 12;
234 return x;
235 }
236
237 static inline uint ip6_hash(ip6_addr a)
238 {
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;
242 }
243
244 static inline u32 ip6_hash32(ip6_addr a)
245 {
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);
249 }
250
251 static inline int ip4_compare(ip4_addr a, ip4_addr b)
252 { return (_I(a) > _I(b)) - (_I(a) < _I(b)); }
253
254 int ip6_compare(ip6_addr a, ip6_addr b);
255
256
257 #ifdef IPV6
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)
261 #else
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)
265 #endif
266
267
268 /*
269 * IP address classification
270 */
271
272 /* Address class */
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
278
279 /* Address scope */
280 #define SCOPE_HOST 0
281 #define SCOPE_LINK 1
282 #define SCOPE_SITE 2
283 #define SCOPE_ORGANIZATION 3
284 #define SCOPE_UNIVERSE 4
285 #define SCOPE_UNDEFINED 5
286
287 int ip4_classify(ip4_addr ad);
288 int ip6_classify(ip6_addr *a);
289
290 static inline int ip6_is_link_local(ip6_addr a)
291 { return (_I0(a) & 0xffc00000) == 0xfe800000; }
292
293 static inline int ip6_is_v4mapped(ip6_addr a)
294 { return _I0(a) == 0 && _I1(a) == 0 && _I2(a) == 0xffff; }
295
296 #ifdef IPV6
297 #define ipa_classify(x) ip6_classify(&(x))
298 #define ipa_is_link_local(x) ip6_is_link_local(x)
299 #else
300 #define ipa_classify(x) ip4_classify(x)
301 #define ipa_is_link_local(x) 0
302 #endif
303
304 static inline int ipa_classify_net(ip_addr a)
305 { return ipa_zero2(a) ? (IADDR_HOST | SCOPE_UNIVERSE) : ipa_classify(a); }
306
307
308 /*
309 * Miscellaneous IP prefix manipulation
310 */
311
312 static inline ip4_addr ip4_mkmask(uint n)
313 { return _MI4(u32_mkmask(n)); }
314
315 static inline int ip4_masklen(ip4_addr a)
316 { return u32_masklen(_I(a)); }
317
318 ip6_addr ip6_mkmask(uint n);
319 int ip6_masklen(ip6_addr *a);
320
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)); }
324
325 static inline uint ip6_pxlen(ip6_addr a, ip6_addr b)
326 {
327 int i = 0;
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]);
333 }
334
335 static inline u32 ip4_getbit(ip4_addr a, uint pos)
336 { return _I(a) & (0x80000000 >> pos); }
337
338 static inline u32 ip6_getbit(ip6_addr a, uint pos)
339 { return a.addr[pos / 32] & (0x80000000 >> (pos % 32)); }
340
341 static inline ip4_addr ip4_opposite_m1(ip4_addr a)
342 { return _MI4(_I(a) ^ 1); }
343
344 static inline ip4_addr ip4_opposite_m2(ip4_addr a)
345 { return _MI4(_I(a) ^ 3); }
346
347 static inline ip6_addr ip6_opposite_m1(ip6_addr a)
348 { return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 1); }
349
350 static inline ip6_addr ip6_opposite_m2(ip6_addr a)
351 { return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 3); }
352
353 ip4_addr ip4_class_mask(ip4_addr ad);
354
355 #ifdef IPV6
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)
362 #else
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)
369 #endif
370
371
372 /*
373 * Host/network order conversions
374 */
375
376 static inline ip4_addr ip4_hton(ip4_addr a)
377 { return _MI4(htonl(_I(a))); }
378
379 static inline ip4_addr ip4_ntoh(ip4_addr a)
380 { return _MI4(ntohl(_I(a))); }
381
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))); }
384
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))); }
387
388 #ifdef IPV6
389 #define ipa_hton(x) x = ip6_hton(x)
390 #define ipa_ntoh(x) x = ip6_ntoh(x)
391 #else
392 #define ipa_hton(x) x = ip4_hton(x)
393 #define ipa_ntoh(x) x = ip4_ntoh(x)
394 #endif
395
396
397 /*
398 * Unaligned data access (in network order)
399 */
400
401 static inline ip4_addr get_ip4(void *buf)
402 {
403 return _MI4(get_u32(buf));
404 }
405
406 static inline ip6_addr get_ip6(void *buf)
407 {
408 ip6_addr a;
409 memcpy(&a, buf, 16);
410 return ip6_ntoh(a);
411 }
412
413 static inline void * put_ip4(void *buf, ip4_addr a)
414 {
415 put_u32(buf, _I(a));
416 return buf+4;
417 }
418
419 static inline void * put_ip6(void *buf, ip6_addr a)
420 {
421 a = ip6_hton(a);
422 memcpy(buf, &a, 16);
423 return buf+16;
424 }
425
426 // XXXX these functions must be redesigned or removed
427 #ifdef IPV6
428 #define get_ipa(x) get_ip6(x)
429 #define put_ipa(x,y) put_ip6(x,y)
430 #else
431 #define get_ipa(x) get_ip4(x)
432 #define put_ipa(x,y) put_ip4(x,y)
433 #endif
434
435
436 /*
437 * Binary/text form conversions
438 */
439
440 char *ip4_ntop(ip4_addr a, char *b);
441 char *ip6_ntop(ip6_addr a, char *b);
442
443 static inline char * ip4_ntox(ip4_addr a, char *b)
444 { return b + bsprintf(b, "%08x", _I(a)); }
445
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)); }
448
449 int ip4_pton(char *a, ip4_addr *o);
450 int ip6_pton(char *a, ip6_addr *o);
451
452 // XXXX these functions must be redesigned or removed
453 #ifdef IPV6
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)
457 #else
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)
461 #endif
462
463
464 /*
465 * Miscellaneous
466 */
467
468 // XXXX review this
469
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)))))
473
474 char *ip_scope_text(uint);
475
476 struct prefix {
477 ip_addr addr;
478 uint len;
479 };
480
481
482 #endif