]> git.ipfire.org Git - thirdparty/bird.git/blob - lib/net.h
Add mock-up function for unit tests
[thirdparty/bird.git] / lib / net.h
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_IP6_SADR 9
25 #define NET_MPLS 10
26 #define NET_MAX 11
27
28 #define NB_IP4 (1 << NET_IP4)
29 #define NB_IP6 (1 << NET_IP6)
30 #define NB_VPN4 (1 << NET_VPN4)
31 #define NB_VPN6 (1 << NET_VPN6)
32 #define NB_ROA4 (1 << NET_ROA4)
33 #define NB_ROA6 (1 << NET_ROA6)
34 #define NB_FLOW4 (1 << NET_FLOW4)
35 #define NB_FLOW6 (1 << NET_FLOW6)
36 #define NB_IP6_SADR (1 << NET_IP6_SADR)
37 #define NB_MPLS (1 << NET_MPLS)
38
39 #define NB_IP (NB_IP4 | NB_IP6)
40 #define NB_VPN (NB_VPN4 | NB_VPN6)
41 #define NB_FLOW (NB_FLOW4 | NB_FLOW6)
42 #define NB_DEST (NB_IP | NB_IP6_SADR | NB_VPN | NB_MPLS)
43 #define NB_ANY 0xffffffff
44
45
46 typedef struct net_addr {
47 u8 type;
48 u8 pxlen;
49 u16 length;
50 u8 data[20];
51 u64 align[0];
52 } net_addr;
53
54 typedef struct net_addr_ip4 {
55 u8 type;
56 u8 pxlen;
57 u16 length;
58 ip4_addr prefix;
59 } net_addr_ip4;
60
61 typedef struct net_addr_ip6 {
62 u8 type;
63 u8 pxlen;
64 u16 length;
65 ip6_addr prefix;
66 } net_addr_ip6;
67
68 typedef struct net_addr_vpn4 {
69 u8 type;
70 u8 pxlen;
71 u16 length;
72 ip4_addr prefix;
73 u64 rd;
74 } net_addr_vpn4;
75
76 typedef struct net_addr_vpn6 {
77 u8 type;
78 u8 pxlen;
79 u16 length;
80 ip6_addr prefix;
81 u32 padding;
82 u64 rd;
83 } net_addr_vpn6;
84
85 typedef struct net_addr_roa4 {
86 u8 type;
87 u8 pxlen;
88 u16 length;
89 ip4_addr prefix;
90 u32 max_pxlen;
91 u32 asn;
92 } net_addr_roa4;
93
94 typedef struct net_addr_roa6 {
95 u8 type;
96 u8 pxlen;
97 u16 length;
98 ip6_addr prefix;
99 u32 max_pxlen;
100 u32 asn;
101 } net_addr_roa6;
102
103 typedef struct net_addr_flow4 {
104 u8 type;
105 u8 pxlen;
106 u16 length;
107 ip4_addr prefix;
108 byte data[0];
109 } net_addr_flow4;
110
111 typedef struct net_addr_flow6 {
112 u8 type;
113 u8 pxlen;
114 u16 length;
115 ip6_addr prefix;
116 byte data[0];
117 } net_addr_flow6;
118
119 typedef struct net_addr_mpls {
120 u8 type;
121 u8 pxlen;
122 u16 length;
123 u32 label;
124 } net_addr_mpls;
125
126 typedef struct net_addr_ip6_sadr {
127 u8 type;
128 u8 dst_pxlen;
129 u16 length;
130 ip6_addr dst_prefix;
131 s32 src_pxlen; /* s32 to avoid padding */
132 ip6_addr src_prefix;
133 } net_addr_ip6_sadr;
134
135 typedef union net_addr_union {
136 net_addr n;
137 net_addr_ip4 ip4;
138 net_addr_ip6 ip6;
139 net_addr_vpn4 vpn4;
140 net_addr_vpn6 vpn6;
141 net_addr_roa4 roa4;
142 net_addr_roa6 roa6;
143 net_addr_flow4 flow4;
144 net_addr_flow6 flow6;
145 net_addr_ip6_sadr ip6_sadr;
146 net_addr_mpls mpls;
147 } net_addr_union;
148
149
150 extern const char * const net_label[];
151 extern const u16 net_addr_length[];
152 extern const u8 net_max_prefix_length[];
153 extern const u16 net_max_text_length[];
154
155 #define NET_MAX_TEXT_LENGTH 256
156
157
158 #define NET_ADDR_IP4(prefix,pxlen) \
159 ((net_addr_ip4) { NET_IP4, pxlen, sizeof(net_addr_ip4), prefix })
160
161 #define NET_ADDR_IP6(prefix,pxlen) \
162 ((net_addr_ip6) { NET_IP6, pxlen, sizeof(net_addr_ip6), prefix })
163
164 #define NET_ADDR_VPN4(prefix,pxlen,rd) \
165 ((net_addr_vpn4) { NET_VPN4, pxlen, sizeof(net_addr_vpn4), prefix, rd })
166
167 #define NET_ADDR_VPN6(prefix,pxlen,rd) \
168 ((net_addr_vpn6) { NET_VPN6, pxlen, sizeof(net_addr_vpn6), prefix, 0, rd })
169
170 #define NET_ADDR_ROA4(prefix,pxlen,max_pxlen,asn) \
171 ((net_addr_roa4) { NET_ROA4, pxlen, sizeof(net_addr_roa4), prefix, max_pxlen, asn })
172
173 #define NET_ADDR_ROA6(prefix,pxlen,max_pxlen,asn) \
174 ((net_addr_roa6) { NET_ROA6, pxlen, sizeof(net_addr_roa6), prefix, max_pxlen, asn })
175
176 #define NET_ADDR_FLOW4(prefix,pxlen,dlen) \
177 ((net_addr_flow4) { NET_FLOW4, pxlen, sizeof(net_addr_ip4) + dlen, prefix })
178
179 #define NET_ADDR_FLOW6(prefix,pxlen,dlen) \
180 ((net_addr_flow6) { NET_FLOW6, pxlen, sizeof(net_addr_ip6) + dlen, prefix })
181
182 #define NET_ADDR_IP6_SADR(dst_prefix,dst_pxlen,src_prefix,src_pxlen) \
183 ((net_addr_ip6_sadr) { NET_IP6_SADR, dst_pxlen, sizeof(net_addr_ip6_sadr), dst_prefix, src_pxlen, src_prefix })
184
185 #define NET_ADDR_MPLS(label) \
186 ((net_addr_mpls) { NET_MPLS, 20, sizeof(net_addr_mpls), label })
187
188
189 static inline void net_fill_ip4(net_addr *a, ip4_addr prefix, uint pxlen)
190 { *(net_addr_ip4 *)a = NET_ADDR_IP4(prefix, pxlen); }
191
192 static inline void net_fill_ip6(net_addr *a, ip6_addr prefix, uint pxlen)
193 { *(net_addr_ip6 *)a = NET_ADDR_IP6(prefix, pxlen); }
194
195 static inline void net_fill_vpn4(net_addr *a, ip4_addr prefix, uint pxlen, u64 rd)
196 { *(net_addr_vpn4 *)a = NET_ADDR_VPN4(prefix, pxlen, rd); }
197
198 static inline void net_fill_vpn6(net_addr *a, ip6_addr prefix, uint pxlen, u64 rd)
199 { *(net_addr_vpn6 *)a = NET_ADDR_VPN6(prefix, pxlen, rd); }
200
201 static inline void net_fill_roa4(net_addr *a, ip4_addr prefix, uint pxlen, uint max_pxlen, u32 asn)
202 { *(net_addr_roa4 *)a = NET_ADDR_ROA4(prefix, pxlen, max_pxlen, asn); }
203
204 static inline void net_fill_roa6(net_addr *a, ip6_addr prefix, uint pxlen, uint max_pxlen, u32 asn)
205 { *(net_addr_roa6 *)a = NET_ADDR_ROA6(prefix, pxlen, max_pxlen, asn); }
206
207 static inline void net_fill_ip6_sadr(net_addr *a, ip6_addr dst_prefix, uint dst_pxlen, ip6_addr src_prefix, uint src_pxlen)
208 { *(net_addr_ip6_sadr *)a = NET_ADDR_IP6_SADR(dst_prefix, dst_pxlen, src_prefix, src_pxlen); }
209
210 static inline void net_fill_mpls(net_addr *a, u32 label)
211 { *(net_addr_mpls *)a = NET_ADDR_MPLS(label); }
212
213 static inline void net_fill_ipa(net_addr *a, ip_addr prefix, uint pxlen)
214 {
215 if (ipa_is_ip4(prefix))
216 net_fill_ip4(a, ipa_to_ip4(prefix), pxlen);
217 else
218 net_fill_ip6(a, ipa_to_ip6(prefix), pxlen);
219 }
220
221 static inline void net_fill_ip_host(net_addr *a, ip_addr prefix)
222 {
223 if (ipa_is_ip4(prefix))
224 net_fill_ip4(a, ipa_to_ip4(prefix), IP4_MAX_PREFIX_LENGTH);
225 else
226 net_fill_ip6(a, ipa_to_ip6(prefix), IP6_MAX_PREFIX_LENGTH);
227 }
228
229 static inline void net_fill_flow4(net_addr *a, ip4_addr prefix, uint pxlen, byte *data, uint dlen)
230 {
231 net_addr_flow4 *f = (void *) a;
232 *f = NET_ADDR_FLOW4(prefix, pxlen, dlen);
233 memcpy(f->data, data, dlen);
234 }
235
236 static inline void net_fill_flow6(net_addr *a, ip6_addr prefix, uint pxlen, byte *data, uint dlen)
237 {
238 net_addr_flow6 *f = (void *) a;
239 *f = NET_ADDR_FLOW6(prefix, pxlen, dlen);
240 memcpy(f->data, data, dlen);
241 }
242
243 /* Make NET_IP6_SADR from NET_IP6, assuming there is enough space */
244 static inline void net_make_ip6_sadr(net_addr *a)
245 {
246 net_addr_ip6_sadr *n = (void *) a;
247 n->type = NET_IP6_SADR;
248 n->length = sizeof(net_addr_ip6_sadr);
249 n->src_pxlen = 0;
250 n->src_prefix = IP6_NONE;
251 }
252
253 static inline int net_val_match(u8 type, u32 mask)
254 { return !!((1 << type) & mask); }
255
256 static inline int net_type_match(const net_addr *a, u32 mask)
257 { return net_val_match(a->type, mask); }
258
259 static inline int net_is_ip(const net_addr *a)
260 { return (a->type == NET_IP4) || (a->type == NET_IP6); }
261
262 static inline int net_is_vpn(const net_addr *a)
263 { return (a->type == NET_VPN4) || (a->type == NET_VPN6); }
264
265 static inline int net_is_roa(const net_addr *a)
266 { return (a->type == NET_ROA4) || (a->type == NET_ROA6); }
267
268 static inline int net_is_flow(const net_addr *a)
269 { return (a->type == NET_FLOW4) || (a->type == NET_FLOW6); }
270
271
272 static inline ip4_addr net4_prefix(const net_addr *a)
273 { return ((net_addr_ip4 *) a)->prefix; }
274
275 static inline ip6_addr net6_prefix(const net_addr *a)
276 { return ((net_addr_ip6 *) a)->prefix; }
277
278 static inline ip_addr net_prefix(const net_addr *a)
279 {
280 switch (a->type)
281 {
282 case NET_IP4:
283 case NET_VPN4:
284 case NET_ROA4:
285 case NET_FLOW4:
286 return ipa_from_ip4(net4_prefix(a));
287
288 case NET_IP6:
289 case NET_VPN6:
290 case NET_ROA6:
291 case NET_FLOW6:
292 case NET_IP6_SADR:
293 return ipa_from_ip6(net6_prefix(a));
294
295 case NET_MPLS:
296 default:
297 return IPA_NONE;
298 }
299 }
300
301 static inline u32 net_mpls(const net_addr *a)
302 {
303 if (a->type == NET_MPLS)
304 return ((net_addr_mpls *) a)->label;
305
306 bug("Can't call net_mpls on non-mpls net_addr");
307 }
308
309 static inline uint net4_pxlen(const net_addr *a)
310 { return a->pxlen; }
311
312 static inline uint net6_pxlen(const net_addr *a)
313 { return a->pxlen; }
314
315 static inline uint net_pxlen(const net_addr *a)
316 { return a->pxlen; }
317
318 ip_addr net_pxmask(const net_addr *a);
319
320 static inline u64 net_rd(const net_addr *a)
321 {
322 switch (a->type)
323 {
324 case NET_VPN4:
325 return ((net_addr_vpn4 *)a)->rd;
326 case NET_VPN6:
327 return ((net_addr_vpn6 *)a)->rd;
328 }
329 return 0;
330 }
331
332
333 static inline int net_equal(const net_addr *a, const net_addr *b)
334 { return (a->length == b->length) && !memcmp(a, b, a->length); }
335
336 static inline int net_equal_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
337 { return !memcmp(a, b, sizeof(net_addr_ip4)); }
338
339 static inline int net_equal_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
340 { return !memcmp(a, b, sizeof(net_addr_ip6)); }
341
342 static inline int net_equal_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b)
343 { return !memcmp(a, b, sizeof(net_addr_vpn4)); }
344
345 static inline int net_equal_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
346 { return !memcmp(a, b, sizeof(net_addr_vpn6)); }
347
348 static inline int net_equal_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
349 { return !memcmp(a, b, sizeof(net_addr_roa4)); }
350
351 static inline int net_equal_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
352 { return !memcmp(a, b, sizeof(net_addr_roa6)); }
353
354 static inline int net_equal_flow4(const net_addr_flow4 *a, const net_addr_flow4 *b)
355 { return net_equal((const net_addr *) a, (const net_addr *) b); }
356
357 static inline int net_equal_flow6(const net_addr_flow6 *a, const net_addr_flow6 *b)
358 { return net_equal((const net_addr *) a, (const net_addr *) b); }
359
360 static inline int net_equal_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
361 { return !memcmp(a, b, sizeof(net_addr_ip6_sadr)); }
362
363 static inline int net_equal_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
364 { return !memcmp(a, b, sizeof(net_addr_mpls)); }
365
366
367 static inline int net_equal_prefix_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
368 { return ip4_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); }
369
370 static inline int net_equal_prefix_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
371 { return ip6_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); }
372
373 static inline int net_equal_dst_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
374 { return ip6_equal(a->dst_prefix, b->dst_prefix) && (a->dst_pxlen == b->dst_pxlen); }
375
376 static inline int net_equal_src_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
377 { return ip6_equal(a->src_prefix, b->src_prefix) && (a->src_pxlen == b->src_pxlen); }
378
379
380 static inline int net_zero_ip4(const net_addr_ip4 *a)
381 { return !a->pxlen && ip4_zero(a->prefix); }
382
383 static inline int net_zero_ip6(const net_addr_ip6 *a)
384 { return !a->pxlen && ip6_zero(a->prefix); }
385
386 static inline int net_zero_vpn4(const net_addr_vpn4 *a)
387 { return !a->pxlen && ip4_zero(a->prefix) && !a->rd; }
388
389 static inline int net_zero_vpn6(const net_addr_vpn6 *a)
390 { return !a->pxlen && ip6_zero(a->prefix) && !a->rd; }
391
392 static inline int net_zero_roa4(const net_addr_roa4 *a)
393 { return !a->pxlen && ip4_zero(a->prefix) && !a->max_pxlen && !a->asn; }
394
395 static inline int net_zero_roa6(const net_addr_roa6 *a)
396 { return !a->pxlen && ip6_zero(a->prefix) && !a->max_pxlen && !a->asn; }
397
398 static inline int net_zero_flow4(const net_addr_flow4 *a)
399 { return !a->pxlen && ip4_zero(a->prefix) && (a->length == sizeof(net_addr_flow4)); }
400
401 static inline int net_zero_flow6(const net_addr_flow6 *a)
402 { return !a->pxlen && ip6_zero(a->prefix) && (a->length == sizeof(net_addr_flow6)); }
403
404 static inline int net_zero_mpls(const net_addr_mpls *a)
405 { return !a->label; }
406
407
408 static inline int net_compare_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
409 { return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
410
411 static inline int net_compare_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
412 { return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
413
414 static inline int net_compare_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b)
415 { return u64_cmp(a->rd, b->rd) ?: ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
416
417 static inline int net_compare_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
418 { return u64_cmp(a->rd, b->rd) ?: ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
419
420 static inline int net_compare_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
421 { 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); }
422
423 static inline int net_compare_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
424 { 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); }
425
426 static inline int net_compare_flow4(const net_addr_flow4 *a, const net_addr_flow4 *b)
427 { 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)); }
428
429 static inline int net_compare_flow6(const net_addr_flow6 *a, const net_addr_flow6 *b)
430 { 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)); }
431
432 static inline int net_compare_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
433 {
434 return
435 ip6_compare(a->dst_prefix, b->dst_prefix) ?: uint_cmp(a->dst_pxlen, b->dst_pxlen) ?:
436 ip6_compare(a->src_prefix, b->src_prefix) ?: uint_cmp(a->src_pxlen, b->src_pxlen);
437 }
438
439 static inline int net_compare_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
440 { return uint_cmp(a->label, b->label); }
441
442 int net_compare(const net_addr *a, const net_addr *b);
443
444
445 static inline void net_copy(net_addr *dst, const net_addr *src)
446 { memcpy(dst, src, src->length); }
447
448 static inline void net_copy_ip4(net_addr_ip4 *dst, const net_addr_ip4 *src)
449 { memcpy(dst, src, sizeof(net_addr_ip4)); }
450
451 static inline void net_copy_ip6(net_addr_ip6 *dst, const net_addr_ip6 *src)
452 { memcpy(dst, src, sizeof(net_addr_ip6)); }
453
454 static inline void net_copy_vpn4(net_addr_vpn4 *dst, const net_addr_vpn4 *src)
455 { memcpy(dst, src, sizeof(net_addr_vpn4)); }
456
457 static inline void net_copy_vpn6(net_addr_vpn6 *dst, const net_addr_vpn6 *src)
458 { memcpy(dst, src, sizeof(net_addr_vpn6)); }
459
460 static inline void net_copy_roa4(net_addr_roa4 *dst, const net_addr_roa4 *src)
461 { memcpy(dst, src, sizeof(net_addr_roa4)); }
462
463 static inline void net_copy_roa6(net_addr_roa6 *dst, const net_addr_roa6 *src)
464 { memcpy(dst, src, sizeof(net_addr_roa6)); }
465
466 static inline void net_copy_flow4(net_addr_flow4 *dst, const net_addr_flow4 *src)
467 { memcpy(dst, src, src->length); }
468
469 static inline void net_copy_flow6(net_addr_flow6 *dst, const net_addr_flow6 *src)
470 { memcpy(dst, src, src->length); }
471
472 static inline void net_copy_ip6_sadr(net_addr_ip6_sadr *dst, const net_addr_ip6_sadr *src)
473 { memcpy(dst, src, sizeof(net_addr_ip6_sadr)); }
474
475 static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src)
476 { memcpy(dst, src, sizeof(net_addr_mpls)); }
477
478
479 /* XXXX */
480 static inline u32 u64_hash(u64 a)
481 { return u32_hash(a); }
482
483 static inline u32 net_hash_ip4(const net_addr_ip4 *n)
484 { return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
485
486 static inline u32 net_hash_ip6(const net_addr_ip6 *n)
487 { return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
488
489 static inline u32 net_hash_vpn4(const net_addr_vpn4 *n)
490 { return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
491
492 static inline u32 net_hash_vpn6(const net_addr_vpn6 *n)
493 { return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
494
495 static inline u32 net_hash_roa4(const net_addr_roa4 *n)
496 { return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
497
498 static inline u32 net_hash_roa6(const net_addr_roa6 *n)
499 { return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
500
501 static inline u32 net_hash_flow4(const net_addr_flow4 *n)
502 { return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
503
504 static inline u32 net_hash_flow6(const net_addr_flow6 *n)
505 { return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
506
507 static inline u32 net_hash_ip6_sadr(const net_addr_ip6_sadr *n)
508 { return net_hash_ip6((net_addr_ip6 *) n); }
509
510 static inline u32 net_hash_mpls(const net_addr_mpls *n)
511 { return n->label; }
512
513 u32 net_hash(const net_addr *a);
514
515
516 static inline int net_validate_px4(const ip4_addr prefix, uint pxlen)
517 {
518 return (pxlen <= IP4_MAX_PREFIX_LENGTH) &&
519 ip4_zero(ip4_and(prefix, ip4_not(ip4_mkmask(pxlen))));
520 }
521
522 static inline int net_validate_px6(const ip6_addr prefix, uint pxlen)
523 {
524 return (pxlen <= IP6_MAX_PREFIX_LENGTH) &&
525 ip6_zero(ip6_and(prefix, ip6_not(ip6_mkmask(pxlen))));
526 }
527
528 static inline int net_validate_ip4(const net_addr_ip4 *n)
529 { return net_validate_px4(n->prefix, n->pxlen); }
530
531 static inline int net_validate_ip6(const net_addr_ip6 *n)
532 { return net_validate_px6(n->prefix, n->pxlen); }
533
534 static inline int net_validate_vpn4(const net_addr_vpn4 *n)
535 { return net_validate_px4(n->prefix, n->pxlen); }
536
537 static inline int net_validate_vpn6(const net_addr_vpn6 *n)
538 { return net_validate_px6(n->prefix, n->pxlen); }
539
540 static inline int net_validate_roa4(const net_addr_roa4 *n)
541 {
542 return net_validate_px4(n->prefix, n->pxlen) &&
543 (n->pxlen <= n->max_pxlen) && (n->max_pxlen <= IP4_MAX_PREFIX_LENGTH);
544 }
545
546 static inline int net_validate_roa6(const net_addr_roa6 *n)
547 {
548 return net_validate_px6(n->prefix, n->pxlen) &&
549 (n->pxlen <= n->max_pxlen) && (n->max_pxlen <= IP6_MAX_PREFIX_LENGTH);
550 }
551
552 // FIXME: Better check, call flow_validate?
553 static inline int net_validate_flow4(const net_addr_flow4 *n)
554 { return net_validate_px4(n->prefix, n->pxlen); }
555
556 static inline int net_validate_flow6(const net_addr_flow6 *n)
557 { return net_validate_px6(n->prefix, n->pxlen); }
558
559 static inline int net_validate_mpls(const net_addr_mpls *n)
560 { return n->label < (1 << 20); }
561
562 static inline int net_validate_ip6_sadr(const net_addr_ip6_sadr *n)
563 { return net_validate_px6(n->dst_prefix, n->dst_pxlen) && net_validate_px6(n->src_prefix, n->src_pxlen); }
564
565 int net_validate(const net_addr *N);
566
567
568 static inline void net_normalize_ip4(net_addr_ip4 *n)
569 { n->prefix = ip4_and(n->prefix, ip4_mkmask(n->pxlen)); }
570
571 static inline void net_normalize_ip6(net_addr_ip6 *n)
572 { n->prefix = ip6_and(n->prefix, ip6_mkmask(n->pxlen)); }
573
574 static inline void net_normalize_vpn4(net_addr_vpn4 *n)
575 { net_normalize_ip4((net_addr_ip4 *) n); }
576
577 static inline void net_normalize_vpn6(net_addr_vpn6 *n)
578 { net_normalize_ip6((net_addr_ip6 *) n); }
579
580 static inline void net_normalize_ip6_sadr(net_addr_ip6_sadr *n)
581 {
582 n->dst_prefix = ip6_and(n->dst_prefix, ip6_mkmask(n->dst_pxlen));
583 n->src_prefix = ip6_and(n->src_prefix, ip6_mkmask(n->src_pxlen));
584 }
585
586 void net_normalize(net_addr *N);
587
588
589 int net_classify(const net_addr *N);
590 int net_format(const net_addr *N, char *buf, int buflen);
591 int rd_format(const u64 rd, char *buf, int buflen);
592
593 static inline int ipa_in_px4(ip4_addr a, ip4_addr prefix, uint pxlen)
594 { return ip4_zero(ip4_and(ip4_xor(a, prefix), ip4_mkmask(pxlen))); }
595
596 static inline int ipa_in_px6(ip6_addr a, ip6_addr prefix, uint pxlen)
597 { return ip6_zero(ip6_and(ip6_xor(a, prefix), ip6_mkmask(pxlen))); }
598
599 static inline int ipa_in_net_ip4(ip4_addr a, const net_addr_ip4 *n)
600 { return ipa_in_px4(a, n->prefix, n->pxlen); }
601
602 static inline int ipa_in_net_ip6(ip6_addr a, const net_addr_ip6 *n)
603 { return ipa_in_px6(a, n->prefix, n->pxlen); }
604
605 static inline int net_in_net_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
606 { return (a->pxlen >= b->pxlen) && ipa_in_px4(a->prefix, b->prefix, b->pxlen); }
607
608 static inline int net_in_net_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
609 { return (a->pxlen >= b->pxlen) && ipa_in_px6(a->prefix, b->prefix, b->pxlen); }
610
611 static inline int net_in_net_dst_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
612 { return (a->dst_pxlen >= b->dst_pxlen) && ipa_in_px6(a->dst_prefix, b->dst_prefix, b->dst_pxlen); }
613
614 static inline int net_in_net_src_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ip6_sadr *b)
615 { return (a->src_pxlen >= b->src_pxlen) && ipa_in_px6(a->src_prefix, b->src_prefix, b->src_pxlen); }
616
617 int ipa_in_netX(const ip_addr A, const net_addr *N);
618 int net_in_netX(const net_addr *A, const net_addr *N);
619
620 void net_init(void);
621
622 #endif