]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/ip.h
0e232f97955a2e1e5661b66112776663ec41736b
[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 "sysdep/unix/endian.h"
13 #include "lib/string.h"
14 #include "lib/bitops.h"
15 #include "lib/unaligned.h"
16
17
18 #define IP4_ALL_NODES ipa_build4(224, 0, 0, 1)
19 #define IP4_ALL_ROUTERS ipa_build4(224, 0, 0, 2)
20 #define IP4_OSPF_ALL_ROUTERS ipa_build4(224, 0, 0, 5)
21 #define IP4_OSPF_DES_ROUTERS ipa_build4(224, 0, 0, 6)
22 #define IP4_RIP_ROUTERS ipa_build4(224, 0, 0, 9)
23
24 #define IP6_ALL_NODES ipa_build6(0xFF020000, 0, 0, 1)
25 #define IP6_ALL_ROUTERS ipa_build6(0xFF020000, 0, 0, 2)
26 #define IP6_OSPF_ALL_ROUTERS ipa_build6(0xFF020000, 0, 0, 5)
27 #define IP6_OSPF_DES_ROUTERS ipa_build6(0xFF020000, 0, 0, 6)
28 #define IP6_RIP_ROUTERS ipa_build6(0xFF020000, 0, 0, 9)
29 #define IP6_BABEL_ROUTERS ipa_build6(0xFF020000, 0, 0, 0x00010006)
30
31 #define IP4_NONE _MI4(0)
32 #define IP6_NONE _MI6(0,0,0,0)
33
34 #define IP4_MAX_PREFIX_LENGTH 32
35 #define IP6_MAX_PREFIX_LENGTH 128
36
37 #define IP4_MAX_TEXT_LENGTH 15 /* "255.255.255.255" */
38 #define IP6_MAX_TEXT_LENGTH 39 /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" */
39 #define IPA_MAX_TEXT_LENGTH 39
40
41 #define IP4_MIN_MTU 576
42 #define IP6_MIN_MTU 1280
43
44 #define IP_PREC_INTERNET_CONTROL 0xc0
45
46 #define IP4_HEADER_LENGTH 20
47 #define IP6_HEADER_LENGTH 40
48 #define UDP_HEADER_LENGTH 8
49
50 #define MPLS_NULL 3
51
52
53 /* IANA Address Family Numbers */
54 /* https://www.iana.org/assignments/address-family-numbers/address-family-numbers.xhtml */
55 /* Would use AF_ prefix, but that collides with POSIX address family numbers */
56 #define AFI_IPV4 1
57 #define AFI_IPV6 2
58
59
60 #ifdef DEBUGGING
61
62 typedef struct ip4_addr {
63 u32 addr;
64 } ip4_addr;
65
66 #define _MI4(x) ((struct ip4_addr) { x })
67 #define _I(x) (x).addr
68
69 #else
70
71 typedef u32 ip4_addr;
72
73 #define _MI4(x) ((u32) (x))
74 #define _I(x) (x)
75
76 #endif
77
78
79 typedef struct ip6_addr {
80 u32 addr[4];
81 } ip6_addr;
82
83 #define _MI6(a,b,c,d) ((struct ip6_addr) {{ a, b, c, d }})
84 #define _I0(a) ((a).addr[0])
85 #define _I1(a) ((a).addr[1])
86 #define _I2(a) ((a).addr[2])
87 #define _I3(a) ((a).addr[3])
88
89
90 /* Structure ip_addr may contain both IPv4 and IPv6 addresses */
91 typedef ip6_addr ip_addr;
92 #define IPA_NONE IP6_NONE
93
94 #define ipa_from_ip4(x) _MI6(0,0,0xffff,_I(x))
95 #define ipa_from_ip6(x) x
96 #define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
97
98 #define ipa_to_ip4(x) _MI4(_I3(x))
99 #define ipa_to_ip6(x) x
100 #define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
101
102 #define ipa_is_ip4(a) ip6_is_v4mapped(a)
103 #define ipa_is_ip6(a) (! ip6_is_v4mapped(a))
104
105 #define IPA_NONE4 ipa_from_ip4(IP4_NONE)
106 #define IPA_NONE6 ipa_from_ip6(IP6_NONE)
107
108
109 /*
110 * Public constructors
111 */
112
113 #define ip4_from_u32(x) _MI4(x)
114 #define ip4_to_u32(x) _I(x)
115
116 #define ip4_build(a,b,c,d) _MI4(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
117 #define ip6_build(a,b,c,d) _MI6(a,b,c,d)
118
119 #define ipa_build4(a,b,c,d) ipa_from_ip4(ip4_build(a,b,c,d))
120 #define ipa_build6(a,b,c,d) ipa_from_ip6(ip6_build(a,b,c,d))
121
122
123 /*
124 * Basic algebraic functions
125 */
126
127 static inline int ip4_equal(ip4_addr a, ip4_addr b)
128 { return _I(a) == _I(b); }
129
130 static inline int ip4_zero(ip4_addr a)
131 { return _I(a) == 0; }
132
133 static inline int ip4_nonzero(ip4_addr a)
134 { return _I(a) != 0; }
135
136 static inline ip4_addr ip4_and(ip4_addr a, ip4_addr b)
137 { return _MI4(_I(a) & _I(b)); }
138
139 static inline ip4_addr ip4_or(ip4_addr a, ip4_addr b)
140 { return _MI4(_I(a) | _I(b)); }
141
142 static inline ip4_addr ip4_xor(ip4_addr a, ip4_addr b)
143 { return _MI4(_I(a) ^ _I(b)); }
144
145 static inline ip4_addr ip4_not(ip4_addr a)
146 { return _MI4(~_I(a)); }
147
148
149 static inline int ip6_equal(ip6_addr a, ip6_addr b)
150 { return _I0(a) == _I0(b) && _I1(a) == _I1(b) && _I2(a) == _I2(b) && _I3(a) == _I3(b); }
151
152 static inline int ip6_zero(ip6_addr a)
153 { return !_I0(a) && !_I1(a) && !_I2(a) && !_I3(a); }
154
155 static inline int ip6_nonzero(ip6_addr a)
156 { return _I0(a) || _I1(a) || _I2(a) || _I3(a); }
157
158 static inline ip6_addr ip6_and(ip6_addr a, ip6_addr b)
159 { return _MI6(_I0(a) & _I0(b), _I1(a) & _I1(b), _I2(a) & _I2(b), _I3(a) & _I3(b)); }
160
161 static inline ip6_addr ip6_or(ip6_addr a, ip6_addr b)
162 { return _MI6(_I0(a) | _I0(b), _I1(a) | _I1(b), _I2(a) | _I2(b), _I3(a) | _I3(b)); }
163
164 static inline ip6_addr ip6_xor(ip6_addr a, ip6_addr b)
165 { return _MI6(_I0(a) ^ _I0(b), _I1(a) ^ _I1(b), _I2(a) ^ _I2(b), _I3(a) ^ _I3(b)); }
166
167 static inline ip6_addr ip6_not(ip6_addr a)
168 { return _MI6(~_I0(a), ~_I1(a), ~_I2(a), ~_I3(a)); }
169
170
171 #define ipa_equal(x,y) ip6_equal(x,y)
172 #define ipa_zero(x) ip6_zero(x)
173 #define ipa_nonzero(x) ip6_nonzero(x)
174 #define ipa_and(x,y) ip6_and(x,y)
175 #define ipa_or(x,y) ip6_or(x,y)
176 #define ipa_xor(x,y) ip6_xor(x,y)
177 #define ipa_not(x) ip6_not(x)
178
179
180 /*
181 * A zero address is either a token for invalid/unused, or the prefix of default
182 * routes. These functions should be used in the second case, where both IPv4
183 * and IPv6 zero addresses should be checked.
184 */
185
186 static inline int ipa_zero2(ip_addr a)
187 { return !_I0(a) && !_I1(a) && ((_I2(a) == 0) || (_I2(a) == 0xffff)) && !_I3(a); }
188
189 static inline int ipa_nonzero2(ip_addr a)
190 { return _I0(a) || _I1(a) || ((_I2(a) != 0) && (_I2(a) != 0xffff)) || _I3(a); }
191
192
193 /*
194 * Hash and compare functions
195 */
196
197 static inline u64 ip4_hash0(ip4_addr a, u32 p, u64 acc)
198 { return (acc + _I(a)) * p; }
199
200 static inline u32 ip4_hash(ip4_addr a)
201 {
202 // return hash_value(ip4_hash0(a, HASH_PARAM, 0));
203
204 /* For some reason, the old hash works slightly better */
205 return u32_hash(_I(a));
206 }
207
208 static inline u64 ip6_hash0(ip6_addr a, u32 p, u64 acc)
209 {
210 acc += _I0(a); acc *= p;
211 acc += _I1(a); acc *= p;
212 acc += _I2(a); acc *= p;
213 acc += _I3(a); acc *= p;
214 return acc;
215 }
216
217 static inline u32 ip6_hash(ip6_addr a)
218 {
219 // return hash_value(ip6_hash0(a, HASH_PARAM, 0));
220
221 /* Just use the expanded form */
222 u64 acc =
223 _I0(a) * HASH_PARAM4 +
224 _I1(a) * HASH_PARAM3 +
225 _I2(a) * HASH_PARAM2 +
226 _I3(a) * HASH_PARAM1;
227 return hash_value(acc);
228 }
229
230 static inline int ip4_compare(ip4_addr a, ip4_addr b)
231 { return (_I(a) > _I(b)) - (_I(a) < _I(b)); }
232
233 int ip6_compare(ip6_addr a, ip6_addr b);
234
235 #define ipa_hash(x) ip6_hash(x)
236 #define ipa_compare(x,y) ip6_compare(x,y)
237
238
239 /*
240 * IP address classification
241 */
242
243 /* Address class */
244 #define IADDR_INVALID -1
245 #define IADDR_SCOPE_MASK 0xfff
246 #define IADDR_HOST 0x1000
247 #define IADDR_BROADCAST 0x2000
248 #define IADDR_MULTICAST 0x4000
249
250 /* Address scope */
251 #define SCOPE_HOST 0
252 #define SCOPE_LINK 1
253 #define SCOPE_SITE 2
254 #define SCOPE_ORGANIZATION 3
255 #define SCOPE_UNIVERSE 4
256 #define SCOPE_UNDEFINED 5
257
258 int ip4_classify(ip4_addr ad);
259 int ip6_classify(ip6_addr *a);
260
261 static inline int ip6_is_link_local(ip6_addr a)
262 { return (_I0(a) & 0xffc00000) == 0xfe800000; }
263
264 static inline int ip6_is_v4mapped(ip6_addr a)
265 { return _I0(a) == 0 && _I1(a) == 0 && _I2(a) == 0xffff; }
266
267 #define ipa_classify(x) ip6_classify(&(x))
268 #define ipa_is_link_local(x) ip6_is_link_local(x)
269
270 static inline int ip4_is_unicast(ip4_addr a)
271 { return _I(a) < 0xe0000000; }
272
273 /* XXXX remove */
274 static inline int ipa_classify_net(ip_addr a)
275 { return ipa_zero2(a) ? (IADDR_HOST | SCOPE_UNIVERSE) : ipa_classify(a); }
276
277
278 /*
279 * Miscellaneous IP prefix manipulation
280 */
281
282 static inline ip4_addr ip4_mkmask(uint n)
283 { return _MI4(u32_mkmask(n)); }
284
285 static inline uint ip4_masklen(ip4_addr a)
286 { return u32_masklen(_I(a)); }
287
288 ip6_addr ip6_mkmask(uint n);
289 uint ip6_masklen(ip6_addr *a);
290
291 /* ipX_pxlen() requires that x != y */
292 static inline uint ip4_pxlen(ip4_addr a, ip4_addr b)
293 { return 31 - u32_log2(_I(a) ^ _I(b)); }
294
295 static inline uint ip6_pxlen(ip6_addr a, ip6_addr b)
296 {
297 int i = 0;
298 i += (a.addr[i] == b.addr[i]);
299 i += (a.addr[i] == b.addr[i]);
300 i += (a.addr[i] == b.addr[i]);
301 i += (a.addr[i] == b.addr[i]);
302 return 32 * i + 31 - u32_log2(a.addr[i] ^ b.addr[i]);
303 }
304
305 static inline int ip4_prefix_equal(ip4_addr a, ip4_addr b, uint n)
306 {
307 return (_I(a) ^ _I(b)) < ((u64) 1 << (32 - n));
308 }
309
310 static inline int ip6_prefix_equal(ip6_addr a, ip6_addr b, uint n)
311 {
312 uint n0 = n / 32;
313 uint n1 = n % 32;
314
315 return
316 ((n0 <= 0) || (_I0(a) == _I0(b))) &&
317 ((n0 <= 1) || (_I1(a) == _I1(b))) &&
318 ((n0 <= 2) || (_I2(a) == _I2(b))) &&
319 ((n0 <= 3) || (_I3(a) == _I3(b))) &&
320 (!n1 || ((a.addr[n0] ^ b.addr[n0]) < (1u << (32 - n1))));
321 }
322
323 static inline u32 ip4_getbit(ip4_addr a, uint pos)
324 { return (_I(a) >> (31 - pos)) & 1; }
325
326 static inline u32 ip4_getbits(ip4_addr a, uint pos, uint n)
327 { return (_I(a) >> ((32 - n) - pos)) & ((1u << n) - 1); }
328
329 static inline u32 ip6_getbit(ip6_addr a, uint pos)
330 { return (a.addr[pos / 32] >> (31 - (pos % 32))) & 0x1; }
331
332 static inline u32 ip6_getbits(ip6_addr a, uint pos, uint n)
333 { return (a.addr[pos / 32] >> ((32 - n) - (pos % 32))) & ((1u << n) - 1); }
334
335 static inline u32 ip4_setbit(ip4_addr *a, uint pos)
336 { return _I(*a) |= (0x80000000 >> pos); }
337
338 static inline u32 ip6_setbit(ip6_addr *a, uint pos)
339 { return a->addr[pos / 32] |= (0x80000000 >> (pos % 32)); }
340
341 static inline u32 ip4_clrbit(ip4_addr *a, uint pos)
342 { return _I(*a) &= ~(0x80000000 >> pos); }
343
344 static inline u32 ip6_clrbit(ip6_addr *a, uint pos)
345 { return a->addr[pos / 32] &= ~(0x80000000 >> (pos % 32)); }
346
347 static inline ip4_addr ip4_setbits(ip4_addr a, uint pos, uint val)
348 { _I(a) |= val << (31 - pos); return a; }
349
350 static inline ip6_addr ip6_setbits(ip6_addr a, uint pos, uint val)
351 { a.addr[pos / 32] |= val << (31 - pos % 32); return a; }
352
353
354 static inline ip4_addr ip4_opposite_m1(ip4_addr a)
355 { return _MI4(_I(a) ^ 1); }
356
357 static inline ip4_addr ip4_opposite_m2(ip4_addr a)
358 { return _MI4(_I(a) ^ 3); }
359
360 static inline ip6_addr ip6_opposite_m1(ip6_addr a)
361 { return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 1); }
362
363 static inline ip6_addr ip6_opposite_m2(ip6_addr a)
364 { return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 3); }
365
366 ip4_addr ip4_class_mask(ip4_addr ad);
367
368 #define ipa_opposite_m1(x) ip6_opposite_m1(x)
369 #define ipa_opposite_m2(x) ip6_opposite_m2(x)
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 #define MPLS_MAX_LABEL_STACK 8
389 typedef struct mpls_label_stack {
390 uint len;
391 u32 stack[MPLS_MAX_LABEL_STACK];
392 } mpls_label_stack;
393
394 static inline int
395 mpls_get(const char *buf, int buflen, u32 *stack)
396 {
397 for (int i=0; (i<MPLS_MAX_LABEL_STACK) && (i*4+3 < buflen); i++)
398 {
399 u32 s = get_u32(buf + i*4);
400 stack[i] = s >> 12;
401 if (s & 0x100)
402 return i+1;
403 }
404 return -1;
405 }
406
407 static inline int
408 mpls_put(char *buf, int len, u32 *stack)
409 {
410 for (int i=0; i<len; i++)
411 put_u32(buf + i*4, stack[i] << 12 | (i+1 == len ? 0x100 : 0));
412
413 return len*4;
414 }
415
416 /*
417 * Unaligned data access (in network order)
418 */
419
420 static inline ip4_addr get_ip4(const void *buf)
421 {
422 return _MI4(get_u32(buf));
423 }
424
425 static inline ip6_addr get_ip6(const void *buf)
426 {
427 ip6_addr a;
428 memcpy(&a, buf, 16);
429 return ip6_ntoh(a);
430 }
431
432 static inline void * put_ip4(void *buf, ip4_addr a)
433 {
434 put_u32(buf, _I(a));
435 return buf+4;
436 }
437
438 static inline void * put_ip6(void *buf, ip6_addr a)
439 {
440 a = ip6_hton(a);
441 memcpy(buf, &a, 16);
442 return buf+16;
443 }
444
445
446 /*
447 * Binary/text form conversions
448 */
449
450 char *ip4_ntop(ip4_addr a, char *b);
451 char *ip6_ntop(ip6_addr a, char *b);
452
453 static inline char * ip4_ntox(ip4_addr a, char *b)
454 { return b + bsprintf(b, "%08x", _I(a)); }
455
456 static inline char * ip6_ntox(ip6_addr a, char *b)
457 { return b + bsprintf(b, "%08x.%08x.%08x.%08x", _I0(a), _I1(a), _I2(a), _I3(a)); }
458
459 int ip4_pton(const char *a, ip4_addr *o);
460 int ip6_pton(const char *a, ip6_addr *o);
461
462
463 /*
464 * Miscellaneous
465 */
466
467 char *ip_scope_text(uint);
468
469 #endif