Statistics
| Branch: | Revision:

iof-bird-daemon / lib / ip.h @ 5e173e9f

History | View | Annotate | Download (11.8 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 STD_ADDRESS_P_LENGTH 39
47
#else
48
#define STD_ADDRESS_P_LENGTH 15
49
#endif
50

    
51

    
52
#ifdef DEBUGGING
53

    
54
typedef struct ip4_addr {
55
  u32 addr;
56
} ip4_addr;
57

    
58
#define _MI4(x) ((struct ip4_addr) { x })
59
#define _I(x) (x).addr
60

    
61
#else
62

    
63
typedef u32 ip4_addr;
64

    
65
#define _MI4(x) (x)
66
#define _I(x) (x)
67

    
68
#endif
69

    
70

    
71
typedef struct ip6_addr {
72
  u32 addr[4];
73
} ip6_addr;
74

    
75
#define _MI6(a,b,c,d) ((struct ip6_addr) {{ a, b, c, d }})
76
#define _I0(a) ((a).addr[0])
77
#define _I1(a) ((a).addr[1])
78
#define _I2(a) ((a).addr[2])
79
#define _I3(a) ((a).addr[3])
80

    
81

    
82
#ifdef IPV6
83

    
84
/* Structure ip_addr may contain both IPv4 and IPv6 addresses */
85
typedef ip6_addr ip_addr;
86
#define IPA_NONE IP6_NONE
87

    
88
#define ipa_from_ip4(x) _MI6(0,0,0xffff,_I(x))
89
#define ipa_from_ip6(x) x
90
#define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
91

    
92
#define ipa_to_ip4(x) _MI4(_I3(x))
93
#define ipa_to_ip6(x) x
94
#define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
95

    
96
#define ipa_is_ip4(a) ip6_is_v4mapped(a)
97

    
98
#else
99

    
100
/* Provisionary ip_addr definition same as ip4_addr */
101
typedef ip4_addr ip_addr;
102
#define IPA_NONE IP4_NONE
103

    
104
#define ipa_from_ip4(x) x
105
#define ipa_from_ip6(x) IPA_NONE
106
#define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
107

    
108
#define ipa_to_ip4(x) x
109
#define ipa_to_ip6(x) IP6_NONE
110
#define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
111

    
112
#define ipa_is_ip4(a) 1
113

    
114
#endif
115

    
116

    
117
/*
118
 *        Public constructors
119
 */
120

    
121
#define ip4_from_u32(x) _MI4(x)
122
#define ip4_to_u32(x) _I(x)
123

    
124
#define ip4_build(a,b,c,d) _MI4(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
125
#define ip6_build(a,b,c,d) _MI6(a,b,c,d)
126

    
127
#define ipa_build4(a,b,c,d) ipa_from_ip4(ip4_build(a,b,c,d))
128
#define ipa_build6(a,b,c,d) ipa_from_ip6(ip6_build(a,b,c,d))
129

    
130

    
131
/*
132
 *        Basic algebraic functions
133
 */
134

    
135
static inline int ip4_equal(ip4_addr a, ip4_addr b)
136
{ return _I(a) == _I(b); }
137

    
138
static inline int ip4_zero(ip4_addr a)
139
{ return _I(a) == 0; }
140

    
141
static inline int ip4_nonzero(ip4_addr a)
142
{ return _I(a) != 0; }
143

    
144
static inline ip4_addr ip4_and(ip4_addr a, ip4_addr b)
145
{ return _MI4(_I(a) & _I(b)); }
146

    
147
static inline ip4_addr ip4_or(ip4_addr a, ip4_addr b)
148
{ return _MI4(_I(a) | _I(b)); }
149

    
150
static inline ip4_addr ip4_xor(ip4_addr a, ip4_addr b)
151
{ return _MI4(_I(a) ^ _I(b)); }
152

    
153
static inline ip4_addr ip4_not(ip4_addr a)
154
{ return _MI4(~_I(a)); }
155

    
156

    
157
static inline int ip6_equal(ip6_addr a, ip6_addr b)
158
{ return _I0(a) == _I0(b) && _I1(a) == _I1(b) && _I2(a) == _I2(b) && _I3(a) == _I3(b); }
159

    
160
static inline int ip6_zero(ip6_addr a)
161
{ return  !_I0(a) && !_I1(a) && !_I2(a) && !_I3(a); }
162

    
163
static inline int ip6_nonzero(ip6_addr a)
164
{ return _I0(a) || _I1(a) || _I2(a) || _I3(a); }
165

    
166
static inline ip6_addr ip6_and(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_or(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_xor(ip6_addr a, ip6_addr b)
173
{ return _MI6(_I0(a) ^ _I0(b), _I1(a) ^ _I1(b), _I2(a) ^ _I2(b), _I3(a) ^ _I3(b)); }
174

    
175
static inline ip6_addr ip6_not(ip6_addr a)
176
{ return _MI6(~_I0(a), ~_I1(a), ~_I2(a), ~_I3(a)); }
177

    
178

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

    
197

    
198

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

    
206
static inline int ipa_zero2(ip_addr a)
207
{ return  !_I0(a) && !_I1(a) && ((_I2(a) == 0) || (_I2(a) == 0xffff)) && !_I3(a); }
208

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

    
212
#else
213
#define ipa_zero2(x) ip4_zero(x)
214
#define ipa_nonzero2(x) ip4_nonzero(x)
215
#endif
216

    
217

    
218
/*
219
 *        Hash and compare functions
220
 */
221

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

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

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

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

    
254
static inline int ip4_compare(ip4_addr a, ip4_addr b)
255
{ return (_I(a) > _I(b)) - (_I(a) < _I(b)); }
256

    
257
int ip6_compare(ip6_addr a, ip6_addr b);
258

    
259

    
260
#ifdef IPV6
261
#define ipa_hash(x) ip6_hash(x)
262
#define ipa_hash32(x) ip6_hash32(x)
263
#define ipa_compare(x,y) ip6_compare(x,y)
264
#else
265
#define ipa_hash(x) ip4_hash(x)
266
#define ipa_hash32(x) ip4_hash32(x)
267
#define ipa_compare(x,y) ip4_compare(x,y)
268
#endif
269

    
270

    
271
/*
272
 *        IP address classification
273
 */
274

    
275
/* Address class */
276
#define IADDR_INVALID                -1
277
#define IADDR_SCOPE_MASK               0xfff
278
#define IADDR_HOST                0x1000
279
#define IADDR_BROADCAST                0x2000
280
#define IADDR_MULTICAST                0x4000
281

    
282
/* Address scope */
283
#define SCOPE_HOST                0
284
#define SCOPE_LINK                1
285
#define SCOPE_SITE                2
286
#define SCOPE_ORGANIZATION        3
287
#define SCOPE_UNIVERSE                4
288
#define SCOPE_UNDEFINED                5
289

    
290
int ip4_classify(ip4_addr ad);
291
int ip6_classify(ip6_addr *a);
292

    
293
static inline int ip6_is_link_local(ip6_addr a)
294
{ return (_I0(a) & 0xffc00000) == 0xfe800000; }
295

    
296
static inline int ip6_is_v4mapped(ip6_addr a)
297
{ return _I0(a) == 0 && _I1(a) == 0 && _I2(a) == 0xffff; }
298

    
299
#ifdef IPV6
300
#define ipa_classify(x) ip6_classify(&(x))
301
#define ipa_is_link_local(x) ip6_is_link_local(x)
302
#else
303
#define ipa_classify(x) ip4_classify(x)
304
#define ipa_is_link_local(x) 0
305
#endif
306

    
307
static inline int ipa_classify_net(ip_addr a)
308
{ return ipa_zero2(a) ? (IADDR_HOST | SCOPE_UNIVERSE) : ipa_classify(a); }
309

    
310

    
311
/*
312
 *        Miscellaneous IP prefix manipulation
313
 */
314

    
315
static inline ip4_addr ip4_mkmask(uint n)
316
{ return _MI4(u32_mkmask(n)); }
317

    
318
static inline uint ip4_masklen(ip4_addr a)
319
{ return u32_masklen(_I(a)); }
320

    
321
ip6_addr ip6_mkmask(uint n);
322
uint ip6_masklen(ip6_addr *a);
323

    
324
/* ipX_pxlen() requires that x != y */
325
static inline uint ip4_pxlen(ip4_addr a, ip4_addr b)
326
{ return 31 - u32_log2(_I(a) ^ _I(b)); }
327

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

    
338
static inline u32 ip4_getbit(ip4_addr a, uint pos)
339
{ return _I(a) & (0x80000000 >> pos); }
340

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

    
344
static inline u32 ip4_setbit(ip4_addr *a, uint pos)
345
{ return _I(*a) |= (0x80000000 >> pos); }
346

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

    
350
static inline u32 ip4_clrbit(ip4_addr *a, uint pos)
351
{ return _I(*a) &= ~(0x80000000 >> pos); }
352

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

    
356
static inline ip4_addr ip4_opposite_m1(ip4_addr a)
357
{ return _MI4(_I(a) ^ 1); }
358

    
359
static inline ip4_addr ip4_opposite_m2(ip4_addr a)
360
{ return _MI4(_I(a) ^ 3); }
361

    
362
static inline ip6_addr ip6_opposite_m1(ip6_addr a)
363
{ return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 1); }
364

    
365
static inline ip6_addr ip6_opposite_m2(ip6_addr a)
366
{ return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 3); }
367

    
368
ip4_addr ip4_class_mask(ip4_addr ad);
369

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

    
390

    
391
/*
392
 *        Host/network order conversions
393
 */
394

    
395
static inline ip4_addr ip4_hton(ip4_addr a)
396
{ return _MI4(htonl(_I(a))); }
397

    
398
static inline ip4_addr ip4_ntoh(ip4_addr a)
399
{ return _MI4(ntohl(_I(a))); }
400

    
401
static inline ip6_addr ip6_hton(ip6_addr a)
402
{ return _MI6(htonl(_I0(a)), htonl(_I1(a)), htonl(_I2(a)), htonl(_I3(a))); }
403

    
404
static inline ip6_addr ip6_ntoh(ip6_addr a)
405
{ return _MI6(ntohl(_I0(a)), ntohl(_I1(a)), ntohl(_I2(a)), ntohl(_I3(a))); }
406

    
407
#ifdef IPV6
408
#define ipa_hton(x) x = ip6_hton(x)
409
#define ipa_ntoh(x) x = ip6_ntoh(x)
410
#else
411
#define ipa_hton(x) x = ip4_hton(x)
412
#define ipa_ntoh(x) x = ip4_ntoh(x)
413
#endif
414

    
415

    
416
/*
417
 *        Unaligned data access (in network order)
418
 */
419

    
420
static inline ip4_addr get_ip4(void *buf)
421
{
422
  return _MI4(get_u32(buf));
423
}
424

    
425
static inline ip6_addr get_ip6(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
// XXXX these functions must be redesigned or removed
446
#ifdef IPV6
447
#define get_ipa(x) get_ip6(x)
448
#define put_ipa(x,y) put_ip6(x,y)
449
#else
450
#define get_ipa(x) get_ip4(x)
451
#define put_ipa(x,y) put_ip4(x,y)
452
#endif
453

    
454

    
455
/*
456
 *        Binary/text form conversions
457
 */
458

    
459
char *ip4_ntop(ip4_addr a, char *b);
460
char *ip6_ntop(ip6_addr a, char *b);
461

    
462
static inline char * ip4_ntox(ip4_addr a, char *b)
463
{ return b + bsprintf(b, "%08x", _I(a)); }
464

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

    
468
int ip4_pton(const char *a, ip4_addr *o);
469
int ip6_pton(const char *a, ip6_addr *o);
470

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

    
482

    
483
/*
484
 *        Miscellaneous
485
 */
486

    
487
// XXXX review this
488

    
489
#define ip_is_prefix(a,l) (!ipa_nonzero(ipa_and(a, ipa_not(ipa_mkmask(l)))))
490
#define ipa_in_net(x,n,p) (ipa_zero(ipa_and(ipa_xor((n),(x)),ipa_mkmask(p))))
491
#define net_in_net(n1,l1,n2,l2) (((l1) >= (l2)) && (ipa_zero(ipa_and(ipa_xor((n1),(n2)),ipa_mkmask(l2)))))
492

    
493
char *ip_scope_text(uint);
494

    
495
#endif