Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (9.74 KB)

1 c40e05a0 Martin Mares
/*
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 7152e5ef Jan Moskyto Matejka
#include "sysdep/unix/endian.h"
13 88a183c6 Ondrej Zajicek
#include "lib/string.h"
14
#include "lib/bitops.h"
15
#include "lib/unaligned.h"
16
17
18 8465dccb Ondrej Zajicek (work)
#define IP4_ALL_NODES                ipa_build4(224, 0, 0, 1)
19
#define IP4_ALL_ROUTERS                ipa_build4(224, 0, 0, 2)
20 88a183c6 Ondrej Zajicek
#define IP4_OSPF_ALL_ROUTERS        ipa_build4(224, 0, 0, 5)
21
#define IP4_OSPF_DES_ROUTERS        ipa_build4(224, 0, 0, 6)
22 8465dccb Ondrej Zajicek (work)
#define IP4_RIP_ROUTERS                ipa_build4(224, 0, 0, 9)
23 88a183c6 Ondrej Zajicek
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 937e75d8 Ondrej Zajicek (work)
#define IP6_BABEL_ROUTERS        ipa_build6(0xFF020000, 0, 0, 0x00010006)
30 88a183c6 Ondrej Zajicek
31
#define IP4_NONE                _MI4(0)
32
#define IP6_NONE                _MI6(0,0,0,0)
33
34 fe9f1a6d Ondrej Zajicek (work)
#define IP4_MAX_PREFIX_LENGTH        32
35
#define IP6_MAX_PREFIX_LENGTH        128
36
37 7fd4143e Jan Moskyto Matejka
#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 88a183c6 Ondrej Zajicek
#define IP4_MIN_MTU                576
42
#define IP6_MIN_MTU                1280
43
44
#define IP_PREC_INTERNET_CONTROL 0xc0
45
46 8465dccb Ondrej Zajicek (work)
#define IP4_HEADER_LENGTH        20
47
#define IP6_HEADER_LENGTH        40
48
#define UDP_HEADER_LENGTH        8
49
50 88a183c6 Ondrej Zajicek
51
#ifdef DEBUGGING
52
53
typedef struct ip4_addr {
54
  u32 addr;
55
} ip4_addr;
56
57
#define _MI4(x) ((struct ip4_addr) { x })
58
#define _I(x) (x).addr
59
60
#else
61
62
typedef u32 ip4_addr;
63
64
#define _MI4(x) (x)
65
#define _I(x) (x)
66
67
#endif
68
69
70
typedef struct ip6_addr {
71
  u32 addr[4];
72
} ip6_addr;
73
74
#define _MI6(a,b,c,d) ((struct ip6_addr) {{ a, b, c, d }})
75
#define _I0(a) ((a).addr[0])
76
#define _I1(a) ((a).addr[1])
77
#define _I2(a) ((a).addr[2])
78
#define _I3(a) ((a).addr[3])
79
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 d15b0b0a Ondrej Zajicek (work)
#define ipa_is_ip6(a) (! ip6_is_v4mapped(a))
95 88a183c6 Ondrej Zajicek
96 04632fd7 Ondrej Zajicek (work)
#define IPA_NONE4 ipa_from_ip4(IP4_NONE)
97
#define IPA_NONE6 ipa_from_ip6(IP6_NONE)
98
99 88a183c6 Ondrej Zajicek
100
/*
101
 *        Public constructors
102
 */
103
104
#define ip4_from_u32(x) _MI4(x)
105
#define ip4_to_u32(x) _I(x)
106
107
#define ip4_build(a,b,c,d) _MI4(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
108
#define ip6_build(a,b,c,d) _MI6(a,b,c,d)
109
110
#define ipa_build4(a,b,c,d) ipa_from_ip4(ip4_build(a,b,c,d))
111
#define ipa_build6(a,b,c,d) ipa_from_ip6(ip6_build(a,b,c,d))
112
113
114
/*
115
 *        Basic algebraic functions
116
 */
117
118
static inline int ip4_equal(ip4_addr a, ip4_addr b)
119
{ return _I(a) == _I(b); }
120
121
static inline int ip4_zero(ip4_addr a)
122
{ return _I(a) == 0; }
123
124
static inline int ip4_nonzero(ip4_addr a)
125
{ return _I(a) != 0; }
126
127
static inline ip4_addr ip4_and(ip4_addr a, ip4_addr b)
128
{ return _MI4(_I(a) & _I(b)); }
129
130
static inline ip4_addr ip4_or(ip4_addr a, ip4_addr b)
131
{ return _MI4(_I(a) | _I(b)); }
132
133
static inline ip4_addr ip4_xor(ip4_addr a, ip4_addr b)
134
{ return _MI4(_I(a) ^ _I(b)); }
135
136
static inline ip4_addr ip4_not(ip4_addr a)
137
{ return _MI4(~_I(a)); }
138
139
140
static inline int ip6_equal(ip6_addr a, ip6_addr b)
141
{ return _I0(a) == _I0(b) && _I1(a) == _I1(b) && _I2(a) == _I2(b) && _I3(a) == _I3(b); }
142
143
static inline int ip6_zero(ip6_addr a)
144
{ return  !_I0(a) && !_I1(a) && !_I2(a) && !_I3(a); }
145
146
static inline int ip6_nonzero(ip6_addr a)
147
{ return _I0(a) || _I1(a) || _I2(a) || _I3(a); }
148
149
static inline ip6_addr ip6_and(ip6_addr a, ip6_addr b)
150
{ return _MI6(_I0(a) & _I0(b), _I1(a) & _I1(b), _I2(a) & _I2(b), _I3(a) & _I3(b)); }
151
152
static inline ip6_addr ip6_or(ip6_addr a, ip6_addr b)
153
{ return _MI6(_I0(a) | _I0(b), _I1(a) | _I1(b), _I2(a) | _I2(b), _I3(a) | _I3(b)); }
154
155
static inline ip6_addr ip6_xor(ip6_addr a, ip6_addr b)
156
{ return _MI6(_I0(a) ^ _I0(b), _I1(a) ^ _I1(b), _I2(a) ^ _I2(b), _I3(a) ^ _I3(b)); }
157
158
static inline ip6_addr ip6_not(ip6_addr a)
159
{ return _MI6(~_I0(a), ~_I1(a), ~_I2(a), ~_I3(a)); }
160
161
162
#define ipa_equal(x,y) ip6_equal(x,y)
163
#define ipa_zero(x) ip6_zero(x)
164
#define ipa_nonzero(x) ip6_nonzero(x)
165
#define ipa_and(x,y) ip6_and(x,y)
166
#define ipa_or(x,y) ip6_or(x,y)
167
#define ipa_xor(x,y) ip6_xor(x,y)
168
#define ipa_not(x) ip6_not(x)
169
170
171
/*
172
 * A zero address is either a token for invalid/unused, or the prefix of default
173
 * routes. These functions should be used in the second case, where both IPv4
174
 * and IPv6 zero addresses should be checked.
175
 */
176
177
static inline int ipa_zero2(ip_addr a)
178
{ return  !_I0(a) && !_I1(a) && ((_I2(a) == 0) || (_I2(a) == 0xffff)) && !_I3(a); }
179
180
static inline int ipa_nonzero2(ip_addr a)
181
{ return _I0(a) || _I1(a) || ((_I2(a) != 0) && (_I2(a) != 0xffff)) || _I3(a); }
182
183
184
/*
185
 *        Hash and compare functions
186
 */
187
188 04632fd7 Ondrej Zajicek (work)
static inline u32 ip4_hash(ip4_addr a)
189 88a183c6 Ondrej Zajicek
{
190
  /* Returns a 32-bit value, although low-order bits are not mixed */
191
  u32 x = _I(a);
192
  x ^= x << 16;
193
  x ^= x << 12;
194
  return x;
195
}
196
197 04632fd7 Ondrej Zajicek (work)
static inline u32 ip6_hash(ip6_addr a)
198 88a183c6 Ondrej Zajicek
{
199
  /* Returns a 32-bit hash key, although low-order bits are not mixed */
200
  u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a);
201
  return x ^ (x << 16) ^ (x << 24);
202
}
203
204
static inline int ip4_compare(ip4_addr a, ip4_addr b)
205
{ return (_I(a) > _I(b)) - (_I(a) < _I(b)); }
206
207
int ip6_compare(ip6_addr a, ip6_addr b);
208
209
#define ipa_hash(x) ip6_hash(x)
210
#define ipa_compare(x,y) ip6_compare(x,y)
211 c40e05a0 Martin Mares
212 0fe3b28b Martin Mares
213 18c8241a Martin Mares
/*
214 88a183c6 Ondrej Zajicek
 *        IP address classification
215 18c8241a Martin Mares
 */
216
217 88a183c6 Ondrej Zajicek
/* Address class */
218 18c8241a Martin Mares
#define IADDR_INVALID                -1
219
#define IADDR_SCOPE_MASK               0xfff
220
#define IADDR_HOST                0x1000
221
#define IADDR_BROADCAST                0x2000
222
#define IADDR_MULTICAST                0x4000
223
224 88a183c6 Ondrej Zajicek
/* Address scope */
225
#define SCOPE_HOST                0
226
#define SCOPE_LINK                1
227
#define SCOPE_SITE                2
228
#define SCOPE_ORGANIZATION        3
229
#define SCOPE_UNIVERSE                4
230
#define SCOPE_UNDEFINED                5
231
232
int ip4_classify(ip4_addr ad);
233
int ip6_classify(ip6_addr *a);
234
235
static inline int ip6_is_link_local(ip6_addr a)
236
{ return (_I0(a) & 0xffc00000) == 0xfe800000; }
237
238
static inline int ip6_is_v4mapped(ip6_addr a)
239
{ return _I0(a) == 0 && _I1(a) == 0 && _I2(a) == 0xffff; }
240
241
#define ipa_classify(x) ip6_classify(&(x))
242
#define ipa_is_link_local(x) ip6_is_link_local(x)
243
244 0bf95f99 Ondrej Zajicek (work)
/* XXXX remove */
245 88a183c6 Ondrej Zajicek
static inline int ipa_classify_net(ip_addr a)
246
{ return ipa_zero2(a) ? (IADDR_HOST | SCOPE_UNIVERSE) : ipa_classify(a); }
247
248
249 18c8241a Martin Mares
/*
250 88a183c6 Ondrej Zajicek
 *        Miscellaneous IP prefix manipulation
251 18c8241a Martin Mares
 */
252
253 88a183c6 Ondrej Zajicek
static inline ip4_addr ip4_mkmask(uint n)
254
{ return _MI4(u32_mkmask(n)); }
255
256 fe9f1a6d Ondrej Zajicek (work)
static inline uint ip4_masklen(ip4_addr a)
257 88a183c6 Ondrej Zajicek
{ return u32_masklen(_I(a)); }
258
259
ip6_addr ip6_mkmask(uint n);
260 fe9f1a6d Ondrej Zajicek (work)
uint ip6_masklen(ip6_addr *a);
261 88a183c6 Ondrej Zajicek
262
/* ipX_pxlen() requires that x != y */
263
static inline uint ip4_pxlen(ip4_addr a, ip4_addr b)
264
{ return 31 - u32_log2(_I(a) ^ _I(b)); }
265
266
static inline uint ip6_pxlen(ip6_addr a, ip6_addr b)
267
{
268
  int i = 0;
269
  i += (a.addr[i] == b.addr[i]);
270
  i += (a.addr[i] == b.addr[i]);
271
  i += (a.addr[i] == b.addr[i]);
272
  i += (a.addr[i] == b.addr[i]);
273
  return 32 * i + 31 - u32_log2(a.addr[i] ^ b.addr[i]);
274
}
275
276
static inline u32 ip4_getbit(ip4_addr a, uint pos)
277
{ return _I(a) & (0x80000000 >> pos); }
278
279
static inline u32 ip6_getbit(ip6_addr a, uint pos)
280
{ return a.addr[pos / 32] & (0x80000000 >> (pos % 32)); }
281
282 fe9f1a6d Ondrej Zajicek (work)
static inline u32 ip4_setbit(ip4_addr *a, uint pos)
283
{ return _I(*a) |= (0x80000000 >> pos); }
284
285
static inline u32 ip6_setbit(ip6_addr *a, uint pos)
286
{ return a->addr[pos / 32] |= (0x80000000 >> (pos % 32)); }
287
288
static inline u32 ip4_clrbit(ip4_addr *a, uint pos)
289
{ return _I(*a) &= ~(0x80000000 >> pos); }
290
291
static inline u32 ip6_clrbit(ip6_addr *a, uint pos)
292
{ return a->addr[pos / 32] &= ~(0x80000000 >> (pos % 32)); }
293
294 88a183c6 Ondrej Zajicek
static inline ip4_addr ip4_opposite_m1(ip4_addr a)
295
{ return _MI4(_I(a) ^ 1); }
296
297
static inline ip4_addr ip4_opposite_m2(ip4_addr a)
298
{ return _MI4(_I(a) ^ 3); }
299
300
static inline ip6_addr ip6_opposite_m1(ip6_addr a)
301
{ return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 1); }
302
303
static inline ip6_addr ip6_opposite_m2(ip6_addr a)
304
{ return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 3); }
305
306
ip4_addr ip4_class_mask(ip4_addr ad);
307
308
#define ipa_opposite_m1(x) ip6_opposite_m1(x)
309
#define ipa_opposite_m2(x) ip6_opposite_m2(x)
310 18c8241a Martin Mares
311 ea32afb7 Martin Mares
312 25697773 Martin Mares
/*
313 88a183c6 Ondrej Zajicek
 *        Host/network order conversions
314 25697773 Martin Mares
 */
315
316 88a183c6 Ondrej Zajicek
static inline ip4_addr ip4_hton(ip4_addr a)
317
{ return _MI4(htonl(_I(a))); }
318
319
static inline ip4_addr ip4_ntoh(ip4_addr a)
320
{ return _MI4(ntohl(_I(a))); }
321
322
static inline ip6_addr ip6_hton(ip6_addr a)
323
{ return _MI6(htonl(_I0(a)), htonl(_I1(a)), htonl(_I2(a)), htonl(_I3(a))); }
324
325
static inline ip6_addr ip6_ntoh(ip6_addr a)
326
{ return _MI6(ntohl(_I0(a)), ntohl(_I1(a)), ntohl(_I2(a)), ntohl(_I3(a))); }
327
328 d14f8c3c Jan Moskyto Matejka
#define MPLS_MAX_LABEL_STACK 8
329
static inline int
330
mpls_get(const char *buf, int buflen, u32 *stack)
331
{
332
  for (int i=0; (i<MPLS_MAX_LABEL_STACK) && (i*4+3 < buflen); i++)
333
  {
334
    u32 s = get_u32(buf + i*4);
335
    stack[i] = s >> 12;
336
    if (s & 0x100)
337
      return i+1;
338
  }
339
  return -1;
340
}
341
342
static inline int
343
mpls_put(char *buf, int len, u32 *stack)
344
{
345
  for (int i=0; i<len; i++)
346
    put_u32(buf + i*4, stack[i] << 12 | (i+1 == len ? 0x100 : 0));
347
348
  return len*4;
349
}
350 88a183c6 Ondrej Zajicek
351
/*
352
 *        Unaligned data access (in network order)
353
 */
354
355
static inline ip4_addr get_ip4(void *buf)
356
{
357
  return _MI4(get_u32(buf));
358
}
359
360
static inline ip6_addr get_ip6(void *buf)
361
{
362
  ip6_addr a;
363
  memcpy(&a, buf, 16);
364
  return ip6_ntoh(a);
365
}
366
367
static inline void * put_ip4(void *buf, ip4_addr a)
368
{
369
  put_u32(buf, _I(a));
370
  return buf+4;
371
}
372
373
static inline void * put_ip6(void *buf, ip6_addr a)
374
{
375
  a = ip6_hton(a);
376
  memcpy(buf, &a, 16);
377
  return buf+16;
378
}
379
380
381
/*
382
 *        Binary/text form conversions
383
 */
384
385
char *ip4_ntop(ip4_addr a, char *b);
386
char *ip6_ntop(ip6_addr a, char *b);
387
388
static inline char * ip4_ntox(ip4_addr a, char *b)
389
{ return b + bsprintf(b, "%08x", _I(a)); }
390
391
static inline char * ip6_ntox(ip6_addr a, char *b)
392
{ return b + bsprintf(b, "%08x.%08x.%08x.%08x", _I0(a), _I1(a), _I2(a), _I3(a)); }
393
394 e422ca0f Ondrej Zajicek (work)
int ip4_pton(const char *a, ip4_addr *o);
395
int ip6_pton(const char *a, ip6_addr *o);
396 88a183c6 Ondrej Zajicek
397 25697773 Martin Mares
398 6b5e06ab Martin Mares
/*
399 88a183c6 Ondrej Zajicek
 *        Miscellaneous
400 6b5e06ab Martin Mares
 */
401
402 ae80a2de Pavel TvrdĂ­k
char *ip_scope_text(uint);
403 88a183c6 Ondrej Zajicek
404 c40e05a0 Martin Mares
#endif