Statistics
| Branch: | Revision:

iof-bird-daemon / lib / ip.h @ fe9f1a6d

History | View | Annotate | Download (12 KB)

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_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

    
30
#define IP4_NONE                _MI4(0)
31
#define IP6_NONE                _MI6(0,0,0,0)
32

    
33
#define IP4_MAX_PREFIX_LENGTH        32
34
#define IP6_MAX_PREFIX_LENGTH        128
35

    
36
#define IP4_MIN_MTU                576
37
#define IP6_MIN_MTU                1280
38

    
39
#define IP_PREC_INTERNET_CONTROL 0xc0
40

    
41
#define IP4_HEADER_LENGTH        20
42
#define IP6_HEADER_LENGTH        40
43
#define UDP_HEADER_LENGTH        8
44

    
45
#ifdef IPV6
46
#define MAX_PREFIX_LENGTH 128
47
#define BITS_PER_IP_ADDRESS 128
48
#define STD_ADDRESS_P_LENGTH 39
49
#else
50
#define MAX_PREFIX_LENGTH 32
51
#define BITS_PER_IP_ADDRESS 32
52
#define STD_ADDRESS_P_LENGTH 15
53
#endif
54

    
55

    
56
#ifdef DEBUGGING
57

    
58
typedef struct ip4_addr {
59
  u32 addr;
60
} ip4_addr;
61

    
62
#define _MI4(x) ((struct ip4_addr) { x })
63
#define _I(x) (x).addr
64

    
65
#else
66

    
67
typedef u32 ip4_addr;
68

    
69
#define _MI4(x) (x)
70
#define _I(x) (x)
71

    
72
#endif
73

    
74

    
75
typedef struct ip6_addr {
76
  u32 addr[4];
77
} ip6_addr;
78

    
79
#define _MI6(a,b,c,d) ((struct ip6_addr) {{ a, b, c, d }})
80
#define _I0(a) ((a).addr[0])
81
#define _I1(a) ((a).addr[1])
82
#define _I2(a) ((a).addr[2])
83
#define _I3(a) ((a).addr[3])
84

    
85

    
86
#ifdef IPV6
87

    
88
/* Structure ip_addr may contain both IPv4 and IPv6 addresses */
89
typedef ip6_addr ip_addr;
90
#define IPA_NONE IP6_NONE
91

    
92
#define ipa_from_ip4(x) _MI6(0,0,0xffff,_I(x))
93
#define ipa_from_ip6(x) x
94
#define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
95

    
96
#define ipa_to_ip4(x) _MI4(_I3(x))
97
#define ipa_to_ip6(x) x
98
#define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
99

    
100
#define ipa_is_ip4(a) ip6_is_v4mapped(a)
101

    
102
#else
103

    
104
/* Provisionary ip_addr definition same as ip4_addr */
105
typedef ip4_addr ip_addr;
106
#define IPA_NONE IP4_NONE
107

    
108
#define ipa_from_ip4(x) x
109
#define ipa_from_ip6(x) IPA_NONE
110
#define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
111

    
112
#define ipa_to_ip4(x) x
113
#define ipa_to_ip6(x) IP6_NONE
114
#define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
115

    
116
#define ipa_is_ip4(a) 1
117

    
118
#endif
119

    
120

    
121
/*
122
 *        Public constructors
123
 */
124

    
125
#define ip4_from_u32(x) _MI4(x)
126
#define ip4_to_u32(x) _I(x)
127

    
128
#define ip4_build(a,b,c,d) _MI4(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
129
#define ip6_build(a,b,c,d) _MI6(a,b,c,d)
130

    
131
#define ipa_build4(a,b,c,d) ipa_from_ip4(ip4_build(a,b,c,d))
132
#define ipa_build6(a,b,c,d) ipa_from_ip6(ip6_build(a,b,c,d))
133

    
134

    
135
/*
136
 *        Basic algebraic functions
137
 */
138

    
139
static inline int ip4_equal(ip4_addr a, ip4_addr b)
140
{ return _I(a) == _I(b); }
141

    
142
static inline int ip4_zero(ip4_addr a)
143
{ return _I(a) == 0; }
144

    
145
static inline int ip4_nonzero(ip4_addr a)
146
{ return _I(a) != 0; }
147

    
148
static inline ip4_addr ip4_and(ip4_addr a, ip4_addr b)
149
{ return _MI4(_I(a) & _I(b)); }
150

    
151
static inline ip4_addr ip4_or(ip4_addr a, ip4_addr b)
152
{ return _MI4(_I(a) | _I(b)); }
153

    
154
static inline ip4_addr ip4_xor(ip4_addr a, ip4_addr b)
155
{ return _MI4(_I(a) ^ _I(b)); }
156

    
157
static inline ip4_addr ip4_not(ip4_addr a)
158
{ return _MI4(~_I(a)); }
159

    
160

    
161
static inline int ip6_equal(ip6_addr a, ip6_addr b)
162
{ return _I0(a) == _I0(b) && _I1(a) == _I1(b) && _I2(a) == _I2(b) && _I3(a) == _I3(b); }
163

    
164
static inline int ip6_zero(ip6_addr a)
165
{ return  !_I0(a) && !_I1(a) && !_I2(a) && !_I3(a); }
166

    
167
static inline int ip6_nonzero(ip6_addr a)
168
{ return _I0(a) || _I1(a) || _I2(a) || _I3(a); }
169

    
170
static inline ip6_addr ip6_and(ip6_addr a, ip6_addr b)
171
{ return _MI6(_I0(a) & _I0(b), _I1(a) & _I1(b), _I2(a) & _I2(b), _I3(a) & _I3(b)); }
172

    
173
static inline ip6_addr ip6_or(ip6_addr a, ip6_addr b)
174
{ return _MI6(_I0(a) | _I0(b), _I1(a) | _I1(b), _I2(a) | _I2(b), _I3(a) | _I3(b)); }
175

    
176
static inline ip6_addr ip6_xor(ip6_addr a, ip6_addr b)
177
{ return _MI6(_I0(a) ^ _I0(b), _I1(a) ^ _I1(b), _I2(a) ^ _I2(b), _I3(a) ^ _I3(b)); }
178

    
179
static inline ip6_addr ip6_not(ip6_addr a)
180
{ return _MI6(~_I0(a), ~_I1(a), ~_I2(a), ~_I3(a)); }
181

    
182

    
183
#ifdef IPV6
184
#define ipa_equal(x,y) ip6_equal(x,y)
185
#define ipa_zero(x) ip6_zero(x)
186
#define ipa_nonzero(x) ip6_nonzero(x)
187
#define ipa_and(x,y) ip6_and(x,y)
188
#define ipa_or(x,y) ip6_or(x,y)
189
#define ipa_xor(x,y) ip6_xor(x,y)
190
#define ipa_not(x) ip6_not(x)
191
#else
192
#define ipa_equal(x,y) ip4_equal(x,y)
193
#define ipa_zero(x) ip4_zero(x)
194
#define ipa_nonzero(x) ip4_nonzero(x)
195
#define ipa_and(x,y) ip4_and(x,y)
196
#define ipa_or(x,y) ip4_or(x,y)
197
#define ipa_xor(x,y) ip4_xor(x,y)
198
#define ipa_not(x) ip4_not(x)
199
#endif
200

    
201

    
202

    
203
#ifdef IPV6
204
/*
205
 * A zero address is either a token for invalid/unused, or the prefix of default
206
 * routes. These functions should be used in the second case, where both IPv4
207
 * and IPv6 zero addresses should be checked.
208
 */
209

    
210
static inline int ipa_zero2(ip_addr a)
211
{ return  !_I0(a) && !_I1(a) && ((_I2(a) == 0) || (_I2(a) == 0xffff)) && !_I3(a); }
212

    
213
static inline int ipa_nonzero2(ip_addr a)
214
{ return _I0(a) || _I1(a) || ((_I2(a) != 0) && (_I2(a) != 0xffff)) || _I3(a); }
215

    
216
#else
217
#define ipa_zero2(x) ip4_zero(x)
218
#define ipa_nonzero2(x) ip4_nonzero(x)
219
#endif
220

    
221

    
222
/*
223
 *        Hash and compare functions
224
 */
225

    
226
static inline uint ip4_hash(ip4_addr a)
227
{
228
  /* Returns a 16-bit value */
229
  u32 x = _I(a);
230
  x ^= x >> 16;
231
  x ^= x << 10;
232
  return x & 0xffff;
233
}
234

    
235
static inline u32 ip4_hash32(ip4_addr a)
236
{
237
  /* Returns a 32-bit value, although low-order bits are not mixed */
238
  u32 x = _I(a);
239
  x ^= x << 16;
240
  x ^= x << 12;
241
  return x;
242
}
243

    
244
static inline uint ip6_hash(ip6_addr a)
245
{
246
  /* Returns a 16-bit hash key */
247
  u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a);
248
  return (x ^ (x >> 16) ^ (x >> 8)) & 0xffff;
249
}
250

    
251
static inline u32 ip6_hash32(ip6_addr a)
252
{
253
  /* Returns a 32-bit hash key, although low-order bits are not mixed */
254
  u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a);
255
  return x ^ (x << 16) ^ (x << 24);
256
}
257

    
258
static inline int ip4_compare(ip4_addr a, ip4_addr b)
259
{ return (_I(a) > _I(b)) - (_I(a) < _I(b)); }
260

    
261
int ip6_compare(ip6_addr a, ip6_addr b);
262

    
263

    
264
#ifdef IPV6
265
#define ipa_hash(x) ip6_hash(x)
266
#define ipa_hash32(x) ip6_hash32(x)
267
#define ipa_compare(x,y) ip6_compare(x,y)
268
#else
269
#define ipa_hash(x) ip4_hash(x)
270
#define ipa_hash32(x) ip4_hash32(x)
271
#define ipa_compare(x,y) ip4_compare(x,y)
272
#endif
273

    
274

    
275
/*
276
 *        IP address classification
277
 */
278

    
279
/* Address class */
280
#define IADDR_INVALID                -1
281
#define IADDR_SCOPE_MASK               0xfff
282
#define IADDR_HOST                0x1000
283
#define IADDR_BROADCAST                0x2000
284
#define IADDR_MULTICAST                0x4000
285

    
286
/* Address scope */
287
#define SCOPE_HOST                0
288
#define SCOPE_LINK                1
289
#define SCOPE_SITE                2
290
#define SCOPE_ORGANIZATION        3
291
#define SCOPE_UNIVERSE                4
292
#define SCOPE_UNDEFINED                5
293

    
294
int ip4_classify(ip4_addr ad);
295
int ip6_classify(ip6_addr *a);
296

    
297
static inline int ip6_is_link_local(ip6_addr a)
298
{ return (_I0(a) & 0xffc00000) == 0xfe800000; }
299

    
300
static inline int ip6_is_v4mapped(ip6_addr a)
301
{ return _I0(a) == 0 && _I1(a) == 0 && _I2(a) == 0xffff; }
302

    
303
#ifdef IPV6
304
#define ipa_classify(x) ip6_classify(&(x))
305
#define ipa_is_link_local(x) ip6_is_link_local(x)
306
#else
307
#define ipa_classify(x) ip4_classify(x)
308
#define ipa_is_link_local(x) 0
309
#endif
310

    
311
static inline int ipa_classify_net(ip_addr a)
312
{ return ipa_zero2(a) ? (IADDR_HOST | SCOPE_UNIVERSE) : ipa_classify(a); }
313

    
314

    
315
/*
316
 *        Miscellaneous IP prefix manipulation
317
 */
318

    
319
static inline ip4_addr ip4_mkmask(uint n)
320
{ return _MI4(u32_mkmask(n)); }
321

    
322
static inline uint ip4_masklen(ip4_addr a)
323
{ return u32_masklen(_I(a)); }
324

    
325
ip6_addr ip6_mkmask(uint n);
326
uint ip6_masklen(ip6_addr *a);
327

    
328
/* ipX_pxlen() requires that x != y */
329
static inline uint ip4_pxlen(ip4_addr a, ip4_addr b)
330
{ return 31 - u32_log2(_I(a) ^ _I(b)); }
331

    
332
static inline uint ip6_pxlen(ip6_addr a, ip6_addr b)
333
{
334
  int i = 0;
335
  i += (a.addr[i] == b.addr[i]);
336
  i += (a.addr[i] == b.addr[i]);
337
  i += (a.addr[i] == b.addr[i]);
338
  i += (a.addr[i] == b.addr[i]);
339
  return 32 * i + 31 - u32_log2(a.addr[i] ^ b.addr[i]);
340
}
341

    
342
static inline u32 ip4_getbit(ip4_addr a, uint pos)
343
{ return _I(a) & (0x80000000 >> pos); }
344

    
345
static inline u32 ip6_getbit(ip6_addr a, uint pos)
346
{ return a.addr[pos / 32] & (0x80000000 >> (pos % 32)); }
347

    
348
static inline u32 ip4_setbit(ip4_addr *a, uint pos)
349
{ return _I(*a) |= (0x80000000 >> pos); }
350

    
351
static inline u32 ip6_setbit(ip6_addr *a, uint pos)
352
{ return a->addr[pos / 32] |= (0x80000000 >> (pos % 32)); }
353

    
354
static inline u32 ip4_clrbit(ip4_addr *a, uint pos)
355
{ return _I(*a) &= ~(0x80000000 >> pos); }
356

    
357
static inline u32 ip6_clrbit(ip6_addr *a, uint pos)
358
{ return a->addr[pos / 32] &= ~(0x80000000 >> (pos % 32)); }
359

    
360
static inline ip4_addr ip4_opposite_m1(ip4_addr a)
361
{ return _MI4(_I(a) ^ 1); }
362

    
363
static inline ip4_addr ip4_opposite_m2(ip4_addr a)
364
{ return _MI4(_I(a) ^ 3); }
365

    
366
static inline ip6_addr ip6_opposite_m1(ip6_addr a)
367
{ return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 1); }
368

    
369
static inline ip6_addr ip6_opposite_m2(ip6_addr a)
370
{ return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 3); }
371

    
372
ip4_addr ip4_class_mask(ip4_addr ad);
373

    
374
#ifdef IPV6
375
#define ipa_mkmask(x) ip6_mkmask(x)
376
#define ipa_masklen(x) ip6_masklen(&x)
377
#define ipa_pxlen(x,y) ip6_pxlen(x,y)
378
#define ipa_getbit(x,n) ip6_getbit(x,n)
379
#define ipa_setbit(x,n) ip6_setbit(x,n)
380
#define ipa_clrbit(x,n) ip6_clrbit(x,n)
381
#define ipa_opposite_m1(x) ip6_opposite_m1(x)
382
#define ipa_opposite_m2(x) ip6_opposite_m2(x)
383
#else
384
#define ipa_mkmask(x) ip4_mkmask(x)
385
#define ipa_masklen(x) ip4_masklen(x)
386
#define ipa_pxlen(x,y) ip4_pxlen(x,y)
387
#define ipa_getbit(x,n) ip4_getbit(x,n)
388
#define ipa_setbit(x,n) ip4_setbit(x,n)
389
#define ipa_clrbit(x,n) ip4_clrbit(x,n)
390
#define ipa_opposite_m1(x) ip4_opposite_m1(x)
391
#define ipa_opposite_m2(x) ip4_opposite_m2(x)
392
#endif
393

    
394

    
395
/*
396
 *        Host/network order conversions
397
 */
398

    
399
static inline ip4_addr ip4_hton(ip4_addr a)
400
{ return _MI4(htonl(_I(a))); }
401

    
402
static inline ip4_addr ip4_ntoh(ip4_addr a)
403
{ return _MI4(ntohl(_I(a))); }
404

    
405
static inline ip6_addr ip6_hton(ip6_addr a)
406
{ return _MI6(htonl(_I0(a)), htonl(_I1(a)), htonl(_I2(a)), htonl(_I3(a))); }
407

    
408
static inline ip6_addr ip6_ntoh(ip6_addr a)
409
{ return _MI6(ntohl(_I0(a)), ntohl(_I1(a)), ntohl(_I2(a)), ntohl(_I3(a))); }
410

    
411
#ifdef IPV6
412
#define ipa_hton(x) x = ip6_hton(x)
413
#define ipa_ntoh(x) x = ip6_ntoh(x)
414
#else
415
#define ipa_hton(x) x = ip4_hton(x)
416
#define ipa_ntoh(x) x = ip4_ntoh(x)
417
#endif
418

    
419

    
420
/*
421
 *        Unaligned data access (in network order)
422
 */
423

    
424
static inline ip4_addr get_ip4(void *buf)
425
{
426
  return _MI4(get_u32(buf));
427
}
428

    
429
static inline ip6_addr get_ip6(void *buf)
430
{
431
  ip6_addr a;
432
  memcpy(&a, buf, 16);
433
  return ip6_ntoh(a);
434
}
435

    
436
static inline void * put_ip4(void *buf, ip4_addr a)
437
{
438
  put_u32(buf, _I(a));
439
  return buf+4;
440
}
441

    
442
static inline void * put_ip6(void *buf, ip6_addr a)
443
{
444
  a = ip6_hton(a);
445
  memcpy(buf, &a, 16);
446
  return buf+16;
447
}
448

    
449
// XXXX these functions must be redesigned or removed
450
#ifdef IPV6
451
#define get_ipa(x) get_ip6(x)
452
#define put_ipa(x,y) put_ip6(x,y)
453
#else
454
#define get_ipa(x) get_ip4(x)
455
#define put_ipa(x,y) put_ip4(x,y)
456
#endif
457

    
458

    
459
/*
460
 *        Binary/text form conversions
461
 */
462

    
463
char *ip4_ntop(ip4_addr a, char *b);
464
char *ip6_ntop(ip6_addr a, char *b);
465

    
466
static inline char * ip4_ntox(ip4_addr a, char *b)
467
{ return b + bsprintf(b, "%08x", _I(a)); }
468

    
469
static inline char * ip6_ntox(ip6_addr a, char *b)
470
{ return b + bsprintf(b, "%08x.%08x.%08x.%08x", _I0(a), _I1(a), _I2(a), _I3(a)); }
471

    
472
int ip4_pton(char *a, ip4_addr *o);
473
int ip6_pton(char *a, ip6_addr *o);
474

    
475
// XXXX these functions must be redesigned or removed
476
#ifdef IPV6
477
#define ipa_ntop(x,y) ip6_ntop(x,y)
478
#define ipa_ntox(x,y) ip6_ntox(x,y)
479
#define ipa_pton(x,y) ip6_pton(x,y)
480
#else
481
#define ipa_ntop(x,y) ip4_ntop(x,y)
482
#define ipa_ntox(x,y) ip4_ntox(x,y)
483
#define ipa_pton(x,y) ip4_pton(x,y)
484
#endif
485

    
486

    
487
/*
488
 *        Miscellaneous
489
 */
490

    
491
// XXXX review this
492

    
493
#define ip_is_prefix(a,l) (!ipa_nonzero(ipa_and(a, ipa_not(ipa_mkmask(l)))))
494
#define ipa_in_net(x,n,p) (ipa_zero(ipa_and(ipa_xor((n),(x)),ipa_mkmask(p))))
495
#define net_in_net(n1,l1,n2,l2) (((l1) >= (l2)) && (ipa_zero(ipa_and(ipa_xor((n1),(n2)),ipa_mkmask(l2)))))
496

    
497
char *ip_scope_text(uint);
498

    
499
struct prefix {
500
  ip_addr addr;
501
  uint len;
502
};
503

    
504

    
505
#endif