Statistics
| Branch: | Revision:

iof-bird-daemon / lib / ip.h @ 04632fd7

History | View | Annotate | Download (9.17 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_MAX_TEXT_LENGTH        15        /* "255.255.255.255" */
37
#define IP6_MAX_TEXT_LENGTH        39        /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" */
38
#define IPA_MAX_TEXT_LENGTH        39
39

    
40
#define IP4_MIN_MTU                576
41
#define IP6_MIN_MTU                1280
42

    
43
#define IP_PREC_INTERNET_CONTROL 0xc0
44

    
45
#define IP4_HEADER_LENGTH        20
46
#define IP6_HEADER_LENGTH        40
47
#define UDP_HEADER_LENGTH        8
48

    
49

    
50
#ifdef DEBUGGING
51

    
52
typedef struct ip4_addr {
53
  u32 addr;
54
} ip4_addr;
55

    
56
#define _MI4(x) ((struct ip4_addr) { x })
57
#define _I(x) (x).addr
58

    
59
#else
60

    
61
typedef u32 ip4_addr;
62

    
63
#define _MI4(x) (x)
64
#define _I(x) (x)
65

    
66
#endif
67

    
68

    
69
typedef struct ip6_addr {
70
  u32 addr[4];
71
} ip6_addr;
72

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

    
79

    
80
/* Structure ip_addr may contain both IPv4 and IPv6 addresses */
81
typedef ip6_addr ip_addr;
82
#define IPA_NONE IP6_NONE
83

    
84
#define ipa_from_ip4(x) _MI6(0,0,0xffff,_I(x))
85
#define ipa_from_ip6(x) x
86
#define ipa_from_u32(x) ipa_from_ip4(ip4_from_u32(x))
87

    
88
#define ipa_to_ip4(x) _MI4(_I3(x))
89
#define ipa_to_ip6(x) x
90
#define ipa_to_u32(x) ip4_to_u32(ipa_to_ip4(x))
91

    
92
#define ipa_is_ip4(a) ip6_is_v4mapped(a)
93

    
94
#define IPA_NONE4 ipa_from_ip4(IP4_NONE)
95
#define IPA_NONE6 ipa_from_ip6(IP6_NONE)
96

    
97

    
98
/*
99
 *        Public constructors
100
 */
101

    
102
#define ip4_from_u32(x) _MI4(x)
103
#define ip4_to_u32(x) _I(x)
104

    
105
#define ip4_build(a,b,c,d) _MI4(((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
106
#define ip6_build(a,b,c,d) _MI6(a,b,c,d)
107

    
108
#define ipa_build4(a,b,c,d) ipa_from_ip4(ip4_build(a,b,c,d))
109
#define ipa_build6(a,b,c,d) ipa_from_ip6(ip6_build(a,b,c,d))
110

    
111

    
112
/*
113
 *        Basic algebraic functions
114
 */
115

    
116
static inline int ip4_equal(ip4_addr a, ip4_addr b)
117
{ return _I(a) == _I(b); }
118

    
119
static inline int ip4_zero(ip4_addr a)
120
{ return _I(a) == 0; }
121

    
122
static inline int ip4_nonzero(ip4_addr a)
123
{ return _I(a) != 0; }
124

    
125
static inline ip4_addr ip4_and(ip4_addr a, ip4_addr b)
126
{ return _MI4(_I(a) & _I(b)); }
127

    
128
static inline ip4_addr ip4_or(ip4_addr a, ip4_addr b)
129
{ return _MI4(_I(a) | _I(b)); }
130

    
131
static inline ip4_addr ip4_xor(ip4_addr a, ip4_addr b)
132
{ return _MI4(_I(a) ^ _I(b)); }
133

    
134
static inline ip4_addr ip4_not(ip4_addr a)
135
{ return _MI4(~_I(a)); }
136

    
137

    
138
static inline int ip6_equal(ip6_addr a, ip6_addr b)
139
{ return _I0(a) == _I0(b) && _I1(a) == _I1(b) && _I2(a) == _I2(b) && _I3(a) == _I3(b); }
140

    
141
static inline int ip6_zero(ip6_addr a)
142
{ return  !_I0(a) && !_I1(a) && !_I2(a) && !_I3(a); }
143

    
144
static inline int ip6_nonzero(ip6_addr a)
145
{ return _I0(a) || _I1(a) || _I2(a) || _I3(a); }
146

    
147
static inline ip6_addr ip6_and(ip6_addr a, ip6_addr b)
148
{ return _MI6(_I0(a) & _I0(b), _I1(a) & _I1(b), _I2(a) & _I2(b), _I3(a) & _I3(b)); }
149

    
150
static inline ip6_addr ip6_or(ip6_addr a, ip6_addr b)
151
{ return _MI6(_I0(a) | _I0(b), _I1(a) | _I1(b), _I2(a) | _I2(b), _I3(a) | _I3(b)); }
152

    
153
static inline ip6_addr ip6_xor(ip6_addr a, ip6_addr b)
154
{ return _MI6(_I0(a) ^ _I0(b), _I1(a) ^ _I1(b), _I2(a) ^ _I2(b), _I3(a) ^ _I3(b)); }
155

    
156
static inline ip6_addr ip6_not(ip6_addr a)
157
{ return _MI6(~_I0(a), ~_I1(a), ~_I2(a), ~_I3(a)); }
158

    
159

    
160
#define ipa_equal(x,y) ip6_equal(x,y)
161
#define ipa_zero(x) ip6_zero(x)
162
#define ipa_nonzero(x) ip6_nonzero(x)
163
#define ipa_and(x,y) ip6_and(x,y)
164
#define ipa_or(x,y) ip6_or(x,y)
165
#define ipa_xor(x,y) ip6_xor(x,y)
166
#define ipa_not(x) ip6_not(x)
167

    
168

    
169
/*
170
 * A zero address is either a token for invalid/unused, or the prefix of default
171
 * routes. These functions should be used in the second case, where both IPv4
172
 * and IPv6 zero addresses should be checked.
173
 */
174

    
175
static inline int ipa_zero2(ip_addr a)
176
{ return  !_I0(a) && !_I1(a) && ((_I2(a) == 0) || (_I2(a) == 0xffff)) && !_I3(a); }
177

    
178
static inline int ipa_nonzero2(ip_addr a)
179
{ return _I0(a) || _I1(a) || ((_I2(a) != 0) && (_I2(a) != 0xffff)) || _I3(a); }
180

    
181

    
182
/*
183
 *        Hash and compare functions
184
 */
185

    
186
static inline u32 ip4_hash(ip4_addr a)
187
{
188
  /* Returns a 32-bit value, although low-order bits are not mixed */
189
  u32 x = _I(a);
190
  x ^= x << 16;
191
  x ^= x << 12;
192
  return x;
193
}
194

    
195
static inline u32 ip6_hash(ip6_addr a)
196
{
197
  /* Returns a 32-bit hash key, although low-order bits are not mixed */
198
  u32 x = _I0(a) ^ _I1(a) ^ _I2(a) ^ _I3(a);
199
  return x ^ (x << 16) ^ (x << 24);
200
}
201

    
202
static inline int ip4_compare(ip4_addr a, ip4_addr b)
203
{ return (_I(a) > _I(b)) - (_I(a) < _I(b)); }
204

    
205
int ip6_compare(ip6_addr a, ip6_addr b);
206

    
207
#define ipa_hash(x) ip6_hash(x)
208
#define ipa_compare(x,y) ip6_compare(x,y)
209

    
210

    
211
/*
212
 *        IP address classification
213
 */
214

    
215
/* Address class */
216
#define IADDR_INVALID                -1
217
#define IADDR_SCOPE_MASK               0xfff
218
#define IADDR_HOST                0x1000
219
#define IADDR_BROADCAST                0x2000
220
#define IADDR_MULTICAST                0x4000
221

    
222
/* Address scope */
223
#define SCOPE_HOST                0
224
#define SCOPE_LINK                1
225
#define SCOPE_SITE                2
226
#define SCOPE_ORGANIZATION        3
227
#define SCOPE_UNIVERSE                4
228
#define SCOPE_UNDEFINED                5
229

    
230
int ip4_classify(ip4_addr ad);
231
int ip6_classify(ip6_addr *a);
232

    
233
static inline int ip6_is_link_local(ip6_addr a)
234
{ return (_I0(a) & 0xffc00000) == 0xfe800000; }
235

    
236
static inline int ip6_is_v4mapped(ip6_addr a)
237
{ return _I0(a) == 0 && _I1(a) == 0 && _I2(a) == 0xffff; }
238

    
239
#define ipa_classify(x) ip6_classify(&(x))
240
#define ipa_is_link_local(x) ip6_is_link_local(x)
241

    
242
/* XXXX remove */
243
static inline int ipa_classify_net(ip_addr a)
244
{ return ipa_zero2(a) ? (IADDR_HOST | SCOPE_UNIVERSE) : ipa_classify(a); }
245

    
246

    
247
/*
248
 *        Miscellaneous IP prefix manipulation
249
 */
250

    
251
static inline ip4_addr ip4_mkmask(uint n)
252
{ return _MI4(u32_mkmask(n)); }
253

    
254
static inline uint ip4_masklen(ip4_addr a)
255
{ return u32_masklen(_I(a)); }
256

    
257
ip6_addr ip6_mkmask(uint n);
258
uint ip6_masklen(ip6_addr *a);
259

    
260
/* ipX_pxlen() requires that x != y */
261
static inline uint ip4_pxlen(ip4_addr a, ip4_addr b)
262
{ return 31 - u32_log2(_I(a) ^ _I(b)); }
263

    
264
static inline uint ip6_pxlen(ip6_addr a, ip6_addr b)
265
{
266
  int i = 0;
267
  i += (a.addr[i] == b.addr[i]);
268
  i += (a.addr[i] == b.addr[i]);
269
  i += (a.addr[i] == b.addr[i]);
270
  i += (a.addr[i] == b.addr[i]);
271
  return 32 * i + 31 - u32_log2(a.addr[i] ^ b.addr[i]);
272
}
273

    
274
static inline u32 ip4_getbit(ip4_addr a, uint pos)
275
{ return _I(a) & (0x80000000 >> pos); }
276

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

    
280
static inline u32 ip4_setbit(ip4_addr *a, uint pos)
281
{ return _I(*a) |= (0x80000000 >> pos); }
282

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

    
286
static inline u32 ip4_clrbit(ip4_addr *a, uint pos)
287
{ return _I(*a) &= ~(0x80000000 >> pos); }
288

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

    
292
static inline ip4_addr ip4_opposite_m1(ip4_addr a)
293
{ return _MI4(_I(a) ^ 1); }
294

    
295
static inline ip4_addr ip4_opposite_m2(ip4_addr a)
296
{ return _MI4(_I(a) ^ 3); }
297

    
298
static inline ip6_addr ip6_opposite_m1(ip6_addr a)
299
{ return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 1); }
300

    
301
static inline ip6_addr ip6_opposite_m2(ip6_addr a)
302
{ return _MI6(_I0(a), _I1(a), _I2(a), _I3(a) ^ 3); }
303

    
304
ip4_addr ip4_class_mask(ip4_addr ad);
305

    
306
#define ipa_opposite_m1(x) ip6_opposite_m1(x)
307
#define ipa_opposite_m2(x) ip6_opposite_m2(x)
308

    
309

    
310
/*
311
 *        Host/network order conversions
312
 */
313

    
314
static inline ip4_addr ip4_hton(ip4_addr a)
315
{ return _MI4(htonl(_I(a))); }
316

    
317
static inline ip4_addr ip4_ntoh(ip4_addr a)
318
{ return _MI4(ntohl(_I(a))); }
319

    
320
static inline ip6_addr ip6_hton(ip6_addr a)
321
{ return _MI6(htonl(_I0(a)), htonl(_I1(a)), htonl(_I2(a)), htonl(_I3(a))); }
322

    
323
static inline ip6_addr ip6_ntoh(ip6_addr a)
324
{ return _MI6(ntohl(_I0(a)), ntohl(_I1(a)), ntohl(_I2(a)), ntohl(_I3(a))); }
325

    
326

    
327
/*
328
 *        Unaligned data access (in network order)
329
 */
330

    
331
static inline ip4_addr get_ip4(void *buf)
332
{
333
  return _MI4(get_u32(buf));
334
}
335

    
336
static inline ip6_addr get_ip6(void *buf)
337
{
338
  ip6_addr a;
339
  memcpy(&a, buf, 16);
340
  return ip6_ntoh(a);
341
}
342

    
343
static inline void * put_ip4(void *buf, ip4_addr a)
344
{
345
  put_u32(buf, _I(a));
346
  return buf+4;
347
}
348

    
349
static inline void * put_ip6(void *buf, ip6_addr a)
350
{
351
  a = ip6_hton(a);
352
  memcpy(buf, &a, 16);
353
  return buf+16;
354
}
355

    
356

    
357
/*
358
 *        Binary/text form conversions
359
 */
360

    
361
char *ip4_ntop(ip4_addr a, char *b);
362
char *ip6_ntop(ip6_addr a, char *b);
363

    
364
static inline char * ip4_ntox(ip4_addr a, char *b)
365
{ return b + bsprintf(b, "%08x", _I(a)); }
366

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

    
370
int ip4_pton(const char *a, ip4_addr *o);
371
int ip6_pton(const char *a, ip6_addr *o);
372

    
373

    
374
/*
375
 *        Miscellaneous
376
 */
377

    
378
char *ip_scope_text(uint);
379

    
380
#endif