]> git.ipfire.org Git - thirdparty/bird.git/blame - lib/net.h
Add mock-up function for unit tests
[thirdparty/bird.git] / lib / net.h
CommitLineData
fe9f1a6d
OZ
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
de9b87f5
PT
20#define NET_ROA4 5
21#define NET_ROA6 6
77234bbb
OZ
22#define NET_FLOW4 7
23#define NET_FLOW6 8
be17805c
OZ
24#define NET_IP6_SADR 9
25#define NET_MPLS 10
26#define NET_MAX 11
fe9f1a6d 27
f4a60a9b
OZ
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)
286e2011
OZ
32#define NB_ROA4 (1 << NET_ROA4)
33#define NB_ROA6 (1 << NET_ROA6)
77234bbb
OZ
34#define NB_FLOW4 (1 << NET_FLOW4)
35#define NB_FLOW6 (1 << NET_FLOW6)
be17805c 36#define NB_IP6_SADR (1 << NET_IP6_SADR)
33ad6e01 37#define NB_MPLS (1 << NET_MPLS)
f4a60a9b
OZ
38
39#define NB_IP (NB_IP4 | NB_IP6)
1e37e35c
OZ
40#define NB_VPN (NB_VPN4 | NB_VPN6)
41#define NB_FLOW (NB_FLOW4 | NB_FLOW6)
be17805c 42#define NB_DEST (NB_IP | NB_IP6_SADR | NB_VPN | NB_MPLS)
f4a60a9b
OZ
43#define NB_ANY 0xffffffff
44
45
fe9f1a6d
OZ
46typedef struct net_addr {
47 u8 type;
48 u8 pxlen;
49 u16 length;
67a2eb91 50 u8 data[20];
fe9f1a6d 51 u64 align[0];
fe9f1a6d
OZ
52} net_addr;
53
54typedef struct net_addr_ip4 {
55 u8 type;
56 u8 pxlen;
57 u16 length;
58 ip4_addr prefix;
59} net_addr_ip4;
60
61typedef struct net_addr_ip6 {
62 u8 type;
63 u8 pxlen;
64 u16 length;
65 ip6_addr prefix;
66} net_addr_ip6;
67
68typedef 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
76typedef struct net_addr_vpn6 {
77 u8 type;
78 u8 pxlen;
79 u16 length;
80 ip6_addr prefix;
67a2eb91 81 u32 padding;
fe9f1a6d
OZ
82 u64 rd;
83} net_addr_vpn6;
84
de9b87f5
PT
85typedef struct net_addr_roa4 {
86 u8 type;
87 u8 pxlen;
88 u16 length;
89 ip4_addr prefix;
f9d729ab 90 u32 max_pxlen;
de9b87f5 91 u32 asn;
de9b87f5
PT
92} net_addr_roa4;
93
94typedef struct net_addr_roa6 {
95 u8 type;
96 u8 pxlen;
97 u16 length;
98 ip6_addr prefix;
f9d729ab 99 u32 max_pxlen;
de9b87f5 100 u32 asn;
de9b87f5 101} net_addr_roa6;
fe9f1a6d 102
77234bbb
OZ
103typedef 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
111typedef 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
33ad6e01
JMM
119typedef struct net_addr_mpls {
120 u8 type;
121 u8 pxlen;
122 u16 length;
123 u32 label;
124} net_addr_mpls;
125
be17805c
OZ
126typedef 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
fe9f1a6d
OZ
135typedef 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;
de9b87f5
PT
141 net_addr_roa4 roa4;
142 net_addr_roa6 roa6;
77234bbb
OZ
143 net_addr_flow4 flow4;
144 net_addr_flow6 flow6;
be17805c 145 net_addr_ip6_sadr ip6_sadr;
33ad6e01 146 net_addr_mpls mpls;
fe9f1a6d
OZ
147} net_addr_union;
148
149
f4a60a9b 150extern const char * const net_label[];
fe9f1a6d 151extern const u16 net_addr_length[];
7fd4143e
JMM
152extern const u8 net_max_prefix_length[];
153extern const u16 net_max_text_length[];
154
77234bbb 155#define NET_MAX_TEXT_LENGTH 256
7fd4143e 156
fe9f1a6d
OZ
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) \
67a2eb91 168 ((net_addr_vpn6) { NET_VPN6, pxlen, sizeof(net_addr_vpn6), prefix, 0, rd })
fe9f1a6d 169
a820ae10
PT
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
77234bbb
OZ
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
be17805c
OZ
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
33ad6e01
JMM
185#define NET_ADDR_MPLS(label) \
186 ((net_addr_mpls) { NET_MPLS, 20, sizeof(net_addr_mpls), label })
a820ae10 187
fe9f1a6d
OZ
188
189static inline void net_fill_ip4(net_addr *a, ip4_addr prefix, uint pxlen)
190{ *(net_addr_ip4 *)a = NET_ADDR_IP4(prefix, pxlen); }
191
192static inline void net_fill_ip6(net_addr *a, ip6_addr prefix, uint pxlen)
193{ *(net_addr_ip6 *)a = NET_ADDR_IP6(prefix, pxlen); }
194
195static 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
198static 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
a820ae10
PT
201static 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
204static 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
be17805c
OZ
207static 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
33ad6e01
JMM
210static inline void net_fill_mpls(net_addr *a, u32 label)
211{ *(net_addr_mpls *)a = NET_ADDR_MPLS(label); }
212
fe9f1a6d
OZ
213static 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
04632fd7
OZ
221static 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
77234bbb
OZ
229static 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
236static 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}
f4a60a9b 242
be17805c
OZ
243/* Make NET_IP6_SADR from NET_IP6, assuming there is enough space */
244static 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
f4a60a9b
OZ
253static inline int net_val_match(u8 type, u32 mask)
254{ return !!((1 << type) & mask); }
255
256static inline int net_type_match(const net_addr *a, u32 mask)
257{ return net_val_match(a->type, mask); }
258
04632fd7
OZ
259static inline int net_is_ip(const net_addr *a)
260{ return (a->type == NET_IP4) || (a->type == NET_IP6); }
261
7fc55925
OZ
262static inline int net_is_vpn(const net_addr *a)
263{ return (a->type == NET_VPN4) || (a->type == NET_VPN6); }
264
69ae5784
PT
265static inline int net_is_roa(const net_addr *a)
266{ return (a->type == NET_ROA4) || (a->type == NET_ROA6); }
267
7fc55925
OZ
268static inline int net_is_flow(const net_addr *a)
269{ return (a->type == NET_FLOW4) || (a->type == NET_FLOW6); }
8c9986d3 270
7fd4143e 271
fe9f1a6d
OZ
272static inline ip4_addr net4_prefix(const net_addr *a)
273{ return ((net_addr_ip4 *) a)->prefix; }
274
275static inline ip6_addr net6_prefix(const net_addr *a)
276{ return ((net_addr_ip6 *) a)->prefix; }
277
278static inline ip_addr net_prefix(const net_addr *a)
279{
280 switch (a->type)
281 {
282 case NET_IP4:
de9b87f5
PT
283 case NET_VPN4:
284 case NET_ROA4:
77234bbb 285 case NET_FLOW4:
de9b87f5
PT
286 return ipa_from_ip4(net4_prefix(a));
287
fe9f1a6d 288 case NET_IP6:
de9b87f5
PT
289 case NET_VPN6:
290 case NET_ROA6:
77234bbb 291 case NET_FLOW6:
be17805c 292 case NET_IP6_SADR:
de9b87f5
PT
293 return ipa_from_ip6(net6_prefix(a));
294
33ad6e01 295 case NET_MPLS:
de9b87f5
PT
296 default:
297 return IPA_NONE;
fe9f1a6d
OZ
298 }
299}
300
d14f8c3c
JMM
301static 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
fe9f1a6d
OZ
309static inline uint net4_pxlen(const net_addr *a)
310{ return a->pxlen; }
311
312static inline uint net6_pxlen(const net_addr *a)
313{ return a->pxlen; }
314
315static inline uint net_pxlen(const net_addr *a)
316{ return a->pxlen; }
317
9b136840
JMM
318ip_addr net_pxmask(const net_addr *a);
319
8c9986d3
JMM
320static 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
fe9f1a6d
OZ
332
333static inline int net_equal(const net_addr *a, const net_addr *b)
334{ return (a->length == b->length) && !memcmp(a, b, a->length); }
335
336static 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
339static 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
342static 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
345static 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
de9b87f5
PT
348static 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
351static 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
77234bbb
OZ
354static 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
357static 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
be17805c
OZ
360static 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
62e64905
OZ
363static 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
77234bbb 366
286e2011
OZ
367static 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
370static 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
be17805c
OZ
373static 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
376static 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
fe9f1a6d
OZ
379
380static inline int net_zero_ip4(const net_addr_ip4 *a)
381{ return !a->pxlen && ip4_zero(a->prefix); }
382
383static inline int net_zero_ip6(const net_addr_ip6 *a)
384{ return !a->pxlen && ip6_zero(a->prefix); }
385
386static inline int net_zero_vpn4(const net_addr_vpn4 *a)
387{ return !a->pxlen && ip4_zero(a->prefix) && !a->rd; }
388
389static inline int net_zero_vpn6(const net_addr_vpn6 *a)
390{ return !a->pxlen && ip6_zero(a->prefix) && !a->rd; }
391
de9b87f5 392static inline int net_zero_roa4(const net_addr_roa4 *a)
a820ae10 393{ return !a->pxlen && ip4_zero(a->prefix) && !a->max_pxlen && !a->asn; }
de9b87f5
PT
394
395static inline int net_zero_roa6(const net_addr_roa6 *a)
a820ae10 396{ return !a->pxlen && ip6_zero(a->prefix) && !a->max_pxlen && !a->asn; }
de9b87f5 397
77234bbb 398static inline int net_zero_flow4(const net_addr_flow4 *a)
ac48e72b 399{ return !a->pxlen && ip4_zero(a->prefix) && (a->length == sizeof(net_addr_flow4)); }
77234bbb
OZ
400
401static inline int net_zero_flow6(const net_addr_flow6 *a)
ac48e72b 402{ return !a->pxlen && ip6_zero(a->prefix) && (a->length == sizeof(net_addr_flow6)); }
77234bbb 403
33ad6e01
JMM
404static inline int net_zero_mpls(const net_addr_mpls *a)
405{ return !a->label; }
77234bbb 406
fe9f1a6d 407
5e173e9f
JMM
408static 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
411static 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
414static 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
417static 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
de9b87f5 420static inline int net_compare_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
a820ae10 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); }
de9b87f5
PT
422
423static inline int net_compare_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
a820ae10 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); }
de9b87f5 425
77234bbb
OZ
426static 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
429static 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
be17805c
OZ
432static 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
33ad6e01
JMM
439static inline int net_compare_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
440{ return uint_cmp(a->label, b->label); }
441
5e173e9f
JMM
442int net_compare(const net_addr *a, const net_addr *b);
443
444
fe9f1a6d
OZ
445static inline void net_copy(net_addr *dst, const net_addr *src)
446{ memcpy(dst, src, src->length); }
447
448static inline void net_copy_ip4(net_addr_ip4 *dst, const net_addr_ip4 *src)
449{ memcpy(dst, src, sizeof(net_addr_ip4)); }
450
451static inline void net_copy_ip6(net_addr_ip6 *dst, const net_addr_ip6 *src)
452{ memcpy(dst, src, sizeof(net_addr_ip6)); }
453
454static inline void net_copy_vpn4(net_addr_vpn4 *dst, const net_addr_vpn4 *src)
455{ memcpy(dst, src, sizeof(net_addr_vpn4)); }
456
457static inline void net_copy_vpn6(net_addr_vpn6 *dst, const net_addr_vpn6 *src)
458{ memcpy(dst, src, sizeof(net_addr_vpn6)); }
459
de9b87f5
PT
460static inline void net_copy_roa4(net_addr_roa4 *dst, const net_addr_roa4 *src)
461{ memcpy(dst, src, sizeof(net_addr_roa4)); }
462
463static inline void net_copy_roa6(net_addr_roa6 *dst, const net_addr_roa6 *src)
464{ memcpy(dst, src, sizeof(net_addr_roa6)); }
465
77234bbb
OZ
466static inline void net_copy_flow4(net_addr_flow4 *dst, const net_addr_flow4 *src)
467{ memcpy(dst, src, src->length); }
468
469static inline void net_copy_flow6(net_addr_flow6 *dst, const net_addr_flow6 *src)
470{ memcpy(dst, src, src->length); }
471
be17805c
OZ
472static 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
33ad6e01
JMM
475static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src)
476{ memcpy(dst, src, sizeof(net_addr_mpls)); }
fe9f1a6d 477
62e64905
OZ
478
479/* XXXX */
480static inline u32 u64_hash(u64 a)
481{ return u32_hash(a); }
482
fe9f1a6d 483static inline u32 net_hash_ip4(const net_addr_ip4 *n)
04632fd7 484{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
fe9f1a6d
OZ
485
486static inline u32 net_hash_ip6(const net_addr_ip6 *n)
04632fd7 487{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
fe9f1a6d 488
fe9f1a6d 489static inline u32 net_hash_vpn4(const net_addr_vpn4 *n)
04632fd7 490{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
fe9f1a6d
OZ
491
492static inline u32 net_hash_vpn6(const net_addr_vpn6 *n)
04632fd7 493{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
fe9f1a6d 494
de9b87f5 495static inline u32 net_hash_roa4(const net_addr_roa4 *n)
a820ae10 496{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
de9b87f5
PT
497
498static inline u32 net_hash_roa6(const net_addr_roa6 *n)
a820ae10 499{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
de9b87f5 500
77234bbb
OZ
501static inline u32 net_hash_flow4(const net_addr_flow4 *n)
502{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
503
504static inline u32 net_hash_flow6(const net_addr_flow6 *n)
505{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
506
be17805c
OZ
507static inline u32 net_hash_ip6_sadr(const net_addr_ip6_sadr *n)
508{ return net_hash_ip6((net_addr_ip6 *) n); }
509
33ad6e01
JMM
510static inline u32 net_hash_mpls(const net_addr_mpls *n)
511{ return n->label; }
512
d15b0b0a
OZ
513u32 net_hash(const net_addr *a);
514
fe9f1a6d 515
4278abfe
OZ
516static 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
522static inline int net_validate_px6(const ip6_addr prefix, uint pxlen)
5e173e9f 523{
4278abfe
OZ
524 return (pxlen <= IP6_MAX_PREFIX_LENGTH) &&
525 ip6_zero(ip6_and(prefix, ip6_not(ip6_mkmask(pxlen))));
5e173e9f
JMM
526}
527
4278abfe
OZ
528static inline int net_validate_ip4(const net_addr_ip4 *n)
529{ return net_validate_px4(n->prefix, n->pxlen); }
530
5e173e9f 531static inline int net_validate_ip6(const net_addr_ip6 *n)
4278abfe
OZ
532{ return net_validate_px6(n->prefix, n->pxlen); }
533
534static inline int net_validate_vpn4(const net_addr_vpn4 *n)
535{ return net_validate_px4(n->prefix, n->pxlen); }
536
537static inline int net_validate_vpn6(const net_addr_vpn6 *n)
538{ return net_validate_px6(n->prefix, n->pxlen); }
539
540static inline int net_validate_roa4(const net_addr_roa4 *n)
5e173e9f 541{
4278abfe
OZ
542 return net_validate_px4(n->prefix, n->pxlen) &&
543 (n->pxlen <= n->max_pxlen) && (n->max_pxlen <= IP4_MAX_PREFIX_LENGTH);
5e173e9f
JMM
544}
545
4278abfe 546static inline int net_validate_roa6(const net_addr_roa6 *n)
33ad6e01 547{
4278abfe
OZ
548 return net_validate_px6(n->prefix, n->pxlen) &&
549 (n->pxlen <= n->max_pxlen) && (n->max_pxlen <= IP6_MAX_PREFIX_LENGTH);
33ad6e01
JMM
550}
551
4278abfe
OZ
552// FIXME: Better check, call flow_validate?
553static inline int net_validate_flow4(const net_addr_flow4 *n)
554{ return net_validate_px4(n->prefix, n->pxlen); }
555
556static inline int net_validate_flow6(const net_addr_flow6 *n)
557{ return net_validate_px6(n->prefix, n->pxlen); }
558
559static inline int net_validate_mpls(const net_addr_mpls *n)
560{ return n->label < (1 << 20); }
561
be17805c
OZ
562static 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
5e173e9f
JMM
565int net_validate(const net_addr *N);
566
567
fe9f1a6d
OZ
568static inline void net_normalize_ip4(net_addr_ip4 *n)
569{ n->prefix = ip4_and(n->prefix, ip4_mkmask(n->pxlen)); }
570
571static inline void net_normalize_ip6(net_addr_ip6 *n)
572{ n->prefix = ip6_and(n->prefix, ip6_mkmask(n->pxlen)); }
573
1e37e35c
OZ
574static inline void net_normalize_vpn4(net_addr_vpn4 *n)
575{ net_normalize_ip4((net_addr_ip4 *) n); }
576
577static inline void net_normalize_vpn6(net_addr_vpn6 *n)
578{ net_normalize_ip6((net_addr_ip6 *) n); }
579
be17805c
OZ
580static 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
fe9f1a6d
OZ
586void net_normalize(net_addr *N);
587
9b136840 588
fe9f1a6d 589int net_classify(const net_addr *N);
9b136840 590int net_format(const net_addr *N, char *buf, int buflen);
8c9986d3 591int rd_format(const u64 rd, char *buf, int buflen);
fe9f1a6d 592
be17805c
OZ
593static 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))); }
4ff15a75 595
be17805c
OZ
596static 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
599static inline int ipa_in_net_ip4(ip4_addr a, const net_addr_ip4 *n)
600{ return ipa_in_px4(a, n->prefix, n->pxlen); }
4ff15a75
OZ
601
602static inline int ipa_in_net_ip6(ip6_addr a, const net_addr_ip6 *n)
be17805c
OZ
603{ return ipa_in_px6(a, n->prefix, n->pxlen); }
604
605static 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); }
4ff15a75
OZ
607
608static inline int net_in_net_ip6(const net_addr_ip6 *a, const net_addr_ip6 *b)
be17805c
OZ
609{ return (a->pxlen >= b->pxlen) && ipa_in_px6(a->prefix, b->prefix, b->pxlen); }
610
611static 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
614static 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); }
4ff15a75 616
d44e686e
OZ
617int ipa_in_netX(const ip_addr A, const net_addr *N);
618int net_in_netX(const net_addr *A, const net_addr *N);
619
67a2eb91 620void net_init(void);
fe9f1a6d
OZ
621
622#endif