Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / lib / net.h @ 6b3f1a54

History | View | Annotate | Download (16.7 KB)

1
/*
2
 *        BIRD Internet Routing Daemon -- Network addresses
3
 *
4
 *        (c) 2015 Ondrej Zajicek <santiago@crfreenet.org>
5
 *        (c) 2015 CZ.NIC z.s.p.o.
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 */
9

    
10
#ifndef _BIRD_NET_H_
11
#define _BIRD_NET_H_
12

    
13
#include "lib/ip.h"
14

    
15

    
16
#define NET_IP4                1
17
#define NET_IP6                2
18
#define NET_VPN4        3
19
#define NET_VPN6        4
20
#define NET_ROA4        5
21
#define NET_ROA6        6
22
#define NET_FLOW4        7
23
#define NET_FLOW6        8
24
#define NET_MPLS        9
25
#define NET_MAX                10
26

    
27
#define NB_IP4                (1 << NET_IP4)
28
#define NB_IP6                (1 << NET_IP6)
29
#define NB_VPN4                (1 << NET_VPN4)
30
#define NB_VPN6                (1 << NET_VPN6)
31
#define NB_ROA4                (1 << NET_ROA4)
32
#define NB_ROA6                (1 << NET_ROA6)
33
#define NB_FLOW4        (1 << NET_FLOW4)
34
#define NB_FLOW6        (1 << NET_FLOW6)
35
#define NB_MPLS                (1 << NET_MPLS)
36

    
37
#define NB_IP                (NB_IP4 | NB_IP6)
38
#define NB_VPN                (NB_VPN4 | NB_VPN6)
39
#define NB_FLOW                (NB_FLOW4 | NB_FLOW6)
40
#define NB_DEST                (NB_IP | NB_VPN | NB_MPLS)
41
#define NB_ANY                0xffffffff
42

    
43

    
44
typedef struct net_addr {
45
  u8 type;
46
  u8 pxlen;
47
  u16 length;
48
  u8 data[20];
49
  u64 align[0];
50
} net_addr;
51

    
52
typedef struct net_addr_ip4 {
53
  u8 type;
54
  u8 pxlen;
55
  u16 length;
56
  ip4_addr prefix;
57
} net_addr_ip4;
58

    
59
typedef struct net_addr_ip6 {
60
  u8 type;
61
  u8 pxlen;
62
  u16 length;
63
  ip6_addr prefix;
64
} net_addr_ip6;
65

    
66
typedef struct net_addr_vpn4 {
67
  u8 type;
68
  u8 pxlen;
69
  u16 length;
70
  ip4_addr prefix;
71
  u64 rd;
72
} net_addr_vpn4;
73

    
74
typedef struct net_addr_vpn6 {
75
  u8 type;
76
  u8 pxlen;
77
  u16 length;
78
  ip6_addr prefix;
79
  u32 padding;
80
  u64 rd;
81
} net_addr_vpn6;
82

    
83
typedef struct net_addr_roa4 {
84
  u8 type;
85
  u8 pxlen;
86
  u16 length;
87
  ip4_addr prefix;
88
  u32 max_pxlen;
89
  u32 asn;
90
} net_addr_roa4;
91

    
92
typedef struct net_addr_roa6 {
93
  u8 type;
94
  u8 pxlen;
95
  u16 length;
96
  ip6_addr prefix;
97
  u32 max_pxlen;
98
  u32 asn;
99
} net_addr_roa6;
100

    
101
typedef struct net_addr_flow4 {
102
  u8 type;
103
  u8 pxlen;
104
  u16 length;
105
  ip4_addr prefix;
106
  byte data[0];
107
} net_addr_flow4;
108

    
109
typedef struct net_addr_flow6 {
110
  u8 type;
111
  u8 pxlen;
112
  u16 length;
113
  ip6_addr prefix;
114
  byte data[0];
115
} net_addr_flow6;
116

    
117
typedef struct net_addr_mpls {
118
  u8 type;
119
  u8 pxlen;
120
  u16 length;
121
  u32 label;
122
} net_addr_mpls;
123

    
124
typedef union net_addr_union {
125
  net_addr n;
126
  net_addr_ip4 ip4;
127
  net_addr_ip6 ip6;
128
  net_addr_vpn4 vpn4;
129
  net_addr_vpn6 vpn6;
130
  net_addr_roa4 roa4;
131
  net_addr_roa6 roa6;
132
  net_addr_flow4 flow4;
133
  net_addr_flow6 flow6;
134
  net_addr_mpls mpls;
135
} net_addr_union;
136

    
137

    
138
extern const char * const net_label[];
139
extern const u16 net_addr_length[];
140
extern const u8  net_max_prefix_length[];
141
extern const u16 net_max_text_length[];
142

    
143
#define NET_MAX_TEXT_LENGTH        256
144

    
145

    
146
#define NET_ADDR_IP4(prefix,pxlen) \
147
  ((net_addr_ip4) { NET_IP4, pxlen, sizeof(net_addr_ip4), prefix })
148

    
149
#define NET_ADDR_IP6(prefix,pxlen) \
150
  ((net_addr_ip6) { NET_IP6, pxlen, sizeof(net_addr_ip6), prefix })
151

    
152
#define NET_ADDR_VPN4(prefix,pxlen,rd) \
153
  ((net_addr_vpn4) { NET_VPN4, pxlen, sizeof(net_addr_vpn4), prefix, rd })
154

    
155
#define NET_ADDR_VPN6(prefix,pxlen,rd) \
156
  ((net_addr_vpn6) { NET_VPN6, pxlen, sizeof(net_addr_vpn6), prefix, 0, rd })
157

    
158
#define NET_ADDR_ROA4(prefix,pxlen,max_pxlen,asn) \
159
  ((net_addr_roa4) { NET_ROA4, pxlen, sizeof(net_addr_roa4), prefix, max_pxlen, asn })
160

    
161
#define NET_ADDR_ROA6(prefix,pxlen,max_pxlen,asn) \
162
  ((net_addr_roa6) { NET_ROA6, pxlen, sizeof(net_addr_roa6), prefix, max_pxlen, asn })
163

    
164
#define NET_ADDR_FLOW4(prefix,pxlen,dlen) \
165
  ((net_addr_flow4) { NET_FLOW4, pxlen, sizeof(net_addr_ip4) + dlen, prefix })
166

    
167
#define NET_ADDR_FLOW6(prefix,pxlen,dlen) \
168
  ((net_addr_flow6) { NET_FLOW6, pxlen, sizeof(net_addr_ip6) + dlen, prefix })
169

    
170
#define NET_ADDR_MPLS(label) \
171
  ((net_addr_mpls) { NET_MPLS, 20, sizeof(net_addr_mpls), label })
172

    
173

    
174
static inline void net_fill_ip4(net_addr *a, ip4_addr prefix, uint pxlen)
175
{ *(net_addr_ip4 *)a = NET_ADDR_IP4(prefix, pxlen); }
176

    
177
static inline void net_fill_ip6(net_addr *a, ip6_addr prefix, uint pxlen)
178
{ *(net_addr_ip6 *)a = NET_ADDR_IP6(prefix, pxlen); }
179

    
180
static inline void net_fill_vpn4(net_addr *a, ip4_addr prefix, uint pxlen, u64 rd)
181
{ *(net_addr_vpn4 *)a = NET_ADDR_VPN4(prefix, pxlen, rd); }
182

    
183
static inline void net_fill_vpn6(net_addr *a, ip6_addr prefix, uint pxlen, u64 rd)
184
{ *(net_addr_vpn6 *)a = NET_ADDR_VPN6(prefix, pxlen, rd); }
185

    
186
static inline void net_fill_roa4(net_addr *a, ip4_addr prefix, uint pxlen, uint max_pxlen, u32 asn)
187
{ *(net_addr_roa4 *)a = NET_ADDR_ROA4(prefix, pxlen, max_pxlen, asn); }
188

    
189
static inline void net_fill_roa6(net_addr *a, ip6_addr prefix, uint pxlen, uint max_pxlen, u32 asn)
190
{ *(net_addr_roa6 *)a = NET_ADDR_ROA6(prefix, pxlen, max_pxlen, asn); }
191

    
192
static inline void net_fill_mpls(net_addr *a, u32 label)
193
{ *(net_addr_mpls *)a = NET_ADDR_MPLS(label); }
194

    
195
static inline void net_fill_ipa(net_addr *a, ip_addr prefix, uint pxlen)
196
{
197
  if (ipa_is_ip4(prefix))
198
    net_fill_ip4(a, ipa_to_ip4(prefix), pxlen);
199
  else
200
    net_fill_ip6(a, ipa_to_ip6(prefix), pxlen);
201
}
202

    
203
static inline void net_fill_ip_host(net_addr *a, ip_addr prefix)
204
{
205
  if (ipa_is_ip4(prefix))
206
    net_fill_ip4(a, ipa_to_ip4(prefix), IP4_MAX_PREFIX_LENGTH);
207
  else
208
    net_fill_ip6(a, ipa_to_ip6(prefix), IP6_MAX_PREFIX_LENGTH);
209
}
210

    
211
static inline void net_fill_flow4(net_addr *a, ip4_addr prefix, uint pxlen, byte *data, uint dlen)
212
{
213
  net_addr_flow4 *f = (void *) a;
214
  *f = NET_ADDR_FLOW4(prefix, pxlen, dlen);
215
  memcpy(f->data, data, dlen);
216
}
217

    
218
static inline void net_fill_flow6(net_addr *a, ip6_addr prefix, uint pxlen, byte *data, uint dlen)
219
{
220
  net_addr_flow6 *f = (void *) a;
221
  *f = NET_ADDR_FLOW6(prefix, pxlen, dlen);
222
  memcpy(f->data, data, dlen);
223
}
224

    
225
static inline int net_val_match(u8 type, u32 mask)
226
{ return !!((1 << type) & mask); }
227

    
228
static inline int net_type_match(const net_addr *a, u32 mask)
229
{ return net_val_match(a->type, mask); }
230

    
231
static inline int net_is_ip(const net_addr *a)
232
{ return (a->type == NET_IP4) || (a->type == NET_IP6); }
233

    
234
static inline int net_is_vpn(const net_addr *a)
235
{ return (a->type == NET_VPN4) || (a->type == NET_VPN6); }
236

    
237
static inline int net_is_roa(const net_addr *a)
238
{ return (a->type == NET_ROA4) || (a->type == NET_ROA6); }
239

    
240
static inline int net_is_flow(const net_addr *a)
241
{ return (a->type == NET_FLOW4) || (a->type == NET_FLOW6); }
242

    
243

    
244
static inline ip4_addr net4_prefix(const net_addr *a)
245
{ return ((net_addr_ip4 *) a)->prefix; }
246

    
247
static inline ip6_addr net6_prefix(const net_addr *a)
248
{ return ((net_addr_ip6 *) a)->prefix; }
249

    
250
static inline ip_addr net_prefix(const net_addr *a)
251
{
252
  switch (a->type)
253
  {
254
  case NET_IP4:
255
  case NET_VPN4:
256
  case NET_ROA4:
257
  case NET_FLOW4:
258
    return ipa_from_ip4(net4_prefix(a));
259

    
260
  case NET_IP6:
261
  case NET_VPN6:
262
  case NET_ROA6:
263
  case NET_FLOW6:
264
    return ipa_from_ip6(net6_prefix(a));
265

    
266
  case NET_MPLS:
267
  default:
268
    return IPA_NONE;
269
  }
270
}
271

    
272
static inline u32 net_mpls(const net_addr *a)
273
{
274
  if (a->type == NET_MPLS)
275
    return ((net_addr_mpls *) a)->label;
276

    
277
  bug("Can't call net_mpls on non-mpls net_addr");
278
}
279

    
280
static inline uint net4_pxlen(const net_addr *a)
281
{ return a->pxlen; }
282

    
283
static inline uint net6_pxlen(const net_addr *a)
284
{ return a->pxlen; }
285

    
286
static inline uint net_pxlen(const net_addr *a)
287
{ return a->pxlen; }
288

    
289
ip_addr net_pxmask(const net_addr *a);
290

    
291
static inline u64 net_rd(const net_addr *a)
292
{
293
  switch (a->type)
294
  {
295
  case NET_VPN4:
296
    return ((net_addr_vpn4 *)a)->rd;
297
  case NET_VPN6:
298
    return ((net_addr_vpn6 *)a)->rd;
299
  }
300
  return 0;
301
}
302

    
303

    
304
static inline int net_equal(const net_addr *a, const net_addr *b)
305
{ return (a->length == b->length) && !memcmp(a, b, a->length); }
306

    
307
static inline int net_equal_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
308
{ return !memcmp(a, b, sizeof(net_addr_ip4)); }
309

    
310
static inline int net_equal_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
311
{ return !memcmp(a, b, sizeof(net_addr_ip6)); }
312

    
313
static inline int net_equal_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b)
314
{ return !memcmp(a, b, sizeof(net_addr_vpn4)); }
315

    
316
static inline int net_equal_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
317
{ return !memcmp(a, b, sizeof(net_addr_vpn6)); }
318

    
319
static inline int net_equal_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
320
{ return !memcmp(a, b, sizeof(net_addr_roa4)); }
321

    
322
static inline int net_equal_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
323
{ return !memcmp(a, b, sizeof(net_addr_roa6)); }
324

    
325
static inline int net_equal_flow4(const net_addr_flow4 *a, const net_addr_flow4 *b)
326
{ return net_equal((const net_addr *) a, (const net_addr *) b); }
327

    
328
static inline int net_equal_flow6(const net_addr_flow6 *a, const net_addr_flow6 *b)
329
{ return net_equal((const net_addr *) a, (const net_addr *) b); }
330

    
331
static inline int net_equal_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
332
{ return !memcmp(a, b, sizeof(net_addr_mpls)); }
333

    
334

    
335
static inline int net_equal_prefix_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
336
{ return ip4_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); }
337

    
338
static inline int net_equal_prefix_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
339
{ return ip6_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); }
340

    
341

    
342
static inline int net_zero_ip4(const net_addr_ip4 *a)
343
{ return !a->pxlen && ip4_zero(a->prefix); }
344

    
345
static inline int net_zero_ip6(const net_addr_ip6 *a)
346
{ return !a->pxlen && ip6_zero(a->prefix); }
347

    
348
static inline int net_zero_vpn4(const net_addr_vpn4 *a)
349
{ return !a->pxlen && ip4_zero(a->prefix) && !a->rd; }
350

    
351
static inline int net_zero_vpn6(const net_addr_vpn6 *a)
352
{ return !a->pxlen && ip6_zero(a->prefix) && !a->rd; }
353

    
354
static inline int net_zero_roa4(const net_addr_roa4 *a)
355
{ return !a->pxlen && ip4_zero(a->prefix) && !a->max_pxlen && !a->asn; }
356

    
357
static inline int net_zero_roa6(const net_addr_roa6 *a)
358
{ return !a->pxlen && ip6_zero(a->prefix) && !a->max_pxlen && !a->asn; }
359

    
360
static inline int net_zero_flow4(const net_addr_flow4 *a)
361
{ return !a->pxlen && ip4_zero(a->prefix) && (a->length == sizeof(net_addr_flow4)); }
362

    
363
static inline int net_zero_flow6(const net_addr_flow6 *a)
364
{ return !a->pxlen && ip6_zero(a->prefix) && (a->length == sizeof(net_addr_flow6)); }
365

    
366
static inline int net_zero_mpls(const net_addr_mpls *a)
367
{ return !a->label; }
368

    
369

    
370
static inline int net_compare_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
371
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
372

    
373
static inline int net_compare_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
374
{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
375

    
376
static inline int net_compare_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b)
377
{ return u64_cmp(a->rd, b->rd) ?: ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
378

    
379
static inline int net_compare_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
380
{ return u64_cmp(a->rd, b->rd) ?: ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
381

    
382
static inline int net_compare_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
383
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->max_pxlen, b->max_pxlen) ?: uint_cmp(a->asn, b->asn); }
384

    
385
static inline int net_compare_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
386
{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->max_pxlen, b->max_pxlen) ?: uint_cmp(a->asn, b->asn); }
387

    
388
static inline int net_compare_flow4(const net_addr_flow4 *a, const net_addr_flow4 *b)
389
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->length, b->length) ?: memcmp(a->data, b->data, a->length - sizeof(net_addr_flow4)); }
390

    
391
static inline int net_compare_flow6(const net_addr_flow6 *a, const net_addr_flow6 *b)
392
{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->length, b->length) ?: memcmp(a->data, b->data, a->length - sizeof(net_addr_flow6)); }
393

    
394
static inline int net_compare_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
395
{ return uint_cmp(a->label, b->label); }
396

    
397
int net_compare(const net_addr *a, const net_addr *b);
398

    
399

    
400
static inline void net_copy(net_addr *dst, const net_addr *src)
401
{ memcpy(dst, src, src->length); }
402

    
403
static inline void net_copy_ip4(net_addr_ip4 *dst, const net_addr_ip4 *src)
404
{ memcpy(dst, src, sizeof(net_addr_ip4)); }
405

    
406
static inline void net_copy_ip6(net_addr_ip6 *dst, const net_addr_ip6 *src)
407
{ memcpy(dst, src, sizeof(net_addr_ip6)); }
408

    
409
static inline void net_copy_vpn4(net_addr_vpn4 *dst, const net_addr_vpn4 *src)
410
{ memcpy(dst, src, sizeof(net_addr_vpn4)); }
411

    
412
static inline void net_copy_vpn6(net_addr_vpn6 *dst, const net_addr_vpn6 *src)
413
{ memcpy(dst, src, sizeof(net_addr_vpn6)); }
414

    
415
static inline void net_copy_roa4(net_addr_roa4 *dst, const net_addr_roa4 *src)
416
{ memcpy(dst, src, sizeof(net_addr_roa4)); }
417

    
418
static inline void net_copy_roa6(net_addr_roa6 *dst, const net_addr_roa6 *src)
419
{ memcpy(dst, src, sizeof(net_addr_roa6)); }
420

    
421
static inline void net_copy_flow4(net_addr_flow4 *dst, const net_addr_flow4 *src)
422
{ memcpy(dst, src, src->length); }
423

    
424
static inline void net_copy_flow6(net_addr_flow6 *dst, const net_addr_flow6 *src)
425
{ memcpy(dst, src, src->length); }
426

    
427
static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src)
428
{ memcpy(dst, src, sizeof(net_addr_mpls)); }
429

    
430

    
431
/* XXXX */
432
static inline u32 u64_hash(u64 a)
433
{ return u32_hash(a); }
434

    
435
static inline u32 net_hash_ip4(const net_addr_ip4 *n)
436
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
437

    
438
static inline u32 net_hash_ip6(const net_addr_ip6 *n)
439
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
440

    
441
static inline u32 net_hash_vpn4(const net_addr_vpn4 *n)
442
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
443

    
444
static inline u32 net_hash_vpn6(const net_addr_vpn6 *n)
445
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
446

    
447
static inline u32 net_hash_roa4(const net_addr_roa4 *n)
448
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
449

    
450
static inline u32 net_hash_roa6(const net_addr_roa6 *n)
451
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
452

    
453
static inline u32 net_hash_flow4(const net_addr_flow4 *n)
454
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
455

    
456
static inline u32 net_hash_flow6(const net_addr_flow6 *n)
457
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
458

    
459
static inline u32 net_hash_mpls(const net_addr_mpls *n)
460
{ return n->label; }
461

    
462
u32 net_hash(const net_addr *a);
463

    
464

    
465
static inline int net_validate_px4(const ip4_addr prefix, uint pxlen)
466
{
467
  return (pxlen <= IP4_MAX_PREFIX_LENGTH) &&
468
    ip4_zero(ip4_and(prefix, ip4_not(ip4_mkmask(pxlen))));
469
}
470

    
471
static inline int net_validate_px6(const ip6_addr prefix, uint pxlen)
472
{
473
  return (pxlen <= IP6_MAX_PREFIX_LENGTH) &&
474
    ip6_zero(ip6_and(prefix, ip6_not(ip6_mkmask(pxlen))));
475
}
476

    
477
static inline int net_validate_ip4(const net_addr_ip4 *n)
478
{ return net_validate_px4(n->prefix, n->pxlen); }
479

    
480
static inline int net_validate_ip6(const net_addr_ip6 *n)
481
{ return net_validate_px6(n->prefix, n->pxlen); }
482

    
483
static inline int net_validate_vpn4(const net_addr_vpn4 *n)
484
{ return net_validate_px4(n->prefix, n->pxlen); }
485

    
486
static inline int net_validate_vpn6(const net_addr_vpn6 *n)
487
{ return  net_validate_px6(n->prefix, n->pxlen); }
488

    
489
static inline int net_validate_roa4(const net_addr_roa4 *n)
490
{
491
  return net_validate_px4(n->prefix, n->pxlen) &&
492
     (n->pxlen <= n->max_pxlen) && (n->max_pxlen <= IP4_MAX_PREFIX_LENGTH);
493
}
494

    
495
static inline int net_validate_roa6(const net_addr_roa6 *n)
496
{
497
  return net_validate_px6(n->prefix, n->pxlen) &&
498
    (n->pxlen <= n->max_pxlen) && (n->max_pxlen <= IP6_MAX_PREFIX_LENGTH);
499
}
500

    
501
// FIXME: Better check, call flow_validate?
502
static inline int net_validate_flow4(const net_addr_flow4 *n)
503
{ return net_validate_px4(n->prefix, n->pxlen); }
504

    
505
static inline int net_validate_flow6(const net_addr_flow6 *n)
506
{ return net_validate_px6(n->prefix, n->pxlen); }
507

    
508
static inline int net_validate_mpls(const net_addr_mpls *n)
509
{ return n->label < (1 << 20); }
510

    
511
int net_validate(const net_addr *N);
512

    
513

    
514
static inline void net_normalize_ip4(net_addr_ip4 *n)
515
{ n->prefix = ip4_and(n->prefix, ip4_mkmask(n->pxlen)); }
516

    
517
static inline void net_normalize_ip6(net_addr_ip6 *n)
518
{ n->prefix = ip6_and(n->prefix, ip6_mkmask(n->pxlen)); }
519

    
520
static inline void net_normalize_vpn4(net_addr_vpn4 *n)
521
{ net_normalize_ip4((net_addr_ip4 *) n); }
522

    
523
static inline void net_normalize_vpn6(net_addr_vpn6 *n)
524
{ net_normalize_ip6((net_addr_ip6 *) n); }
525

    
526
void net_normalize(net_addr *N);
527

    
528

    
529
int net_classify(const net_addr *N);
530
int net_format(const net_addr *N, char *buf, int buflen);
531
int rd_format(const u64 rd, char *buf, int buflen);
532

    
533
static inline int ipa_in_net_ip4(ip4_addr a, const net_addr_ip4 *n)
534
{ return ip4_zero(ip4_and(ip4_xor(a, n->prefix), ip4_mkmask(n->pxlen))); }
535

    
536
static inline int net_in_net_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
537
{ return (a->pxlen >= b->pxlen) && ipa_in_net_ip4(a->prefix, b); }
538

    
539
static inline int ipa_in_net_ip6(ip6_addr a, const net_addr_ip6 *n)
540
{ return ip6_zero(ip6_and(ip6_xor(a, n->prefix), ip6_mkmask(n->pxlen))); }
541

    
542
static inline int net_in_net_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
543
{ return (a->pxlen >= b->pxlen) && ipa_in_net_ip6(a->prefix, b); }
544

    
545
int ipa_in_netX(const ip_addr A, const net_addr *N);
546
int net_in_netX(const net_addr *A, const net_addr *N);
547

    
548
void net_init(void);
549

    
550
#endif