Add NET ROA4/6 structures

This commit is contained in:
Pavel Tvrdík 2016-01-07 18:17:16 +01:00
parent e691d16a63
commit de9b87f558
5 changed files with 101 additions and 7 deletions

View file

@ -269,7 +269,9 @@ trie_match_net(struct f_trie *t, const net_addr *n)
int add = 0; int add = 0;
switch (n->type) { switch (n->type) {
case NET_IP4: case NET_IP4:
case NET_VPN4: add = IP6_MAX_PREFIX_LENGTH - IP4_MAX_PREFIX_LENGTH; case NET_VPN4:
case NET_ROA4:
add = IP6_MAX_PREFIX_LENGTH - IP4_MAX_PREFIX_LENGTH;
} }
return trie_match_prefix(t, net_prefix(n), net_pxlen(n) + add); return trie_match_prefix(t, net_prefix(n), net_pxlen(n) + add);

View file

@ -8,14 +8,18 @@ const u16 net_addr_length[] = {
[NET_IP4] = sizeof(net_addr_ip4), [NET_IP4] = sizeof(net_addr_ip4),
[NET_IP6] = sizeof(net_addr_ip6), [NET_IP6] = sizeof(net_addr_ip6),
[NET_VPN4] = sizeof(net_addr_vpn4), [NET_VPN4] = sizeof(net_addr_vpn4),
[NET_VPN6] = sizeof(net_addr_vpn6) [NET_VPN6] = sizeof(net_addr_vpn6),
[NET_ROA4] = sizeof(net_addr_roa4),
[NET_ROA6] = sizeof(net_addr_roa6)
}; };
const u8 net_max_prefix_length[] = { const u8 net_max_prefix_length[] = {
[NET_IP4] = IP4_MAX_PREFIX_LENGTH, [NET_IP4] = IP4_MAX_PREFIX_LENGTH,
[NET_IP6] = IP6_MAX_PREFIX_LENGTH, [NET_IP6] = IP6_MAX_PREFIX_LENGTH,
[NET_VPN4] = IP4_MAX_PREFIX_LENGTH, [NET_VPN4] = IP4_MAX_PREFIX_LENGTH,
[NET_VPN6] = IP4_MAX_PREFIX_LENGTH [NET_VPN6] = IP6_MAX_PREFIX_LENGTH,
[NET_ROA4] = IP4_MAX_PREFIX_LENGTH,
[NET_ROA6] = IP6_MAX_PREFIX_LENGTH
}; };
const u16 net_max_text_length[] = { const u16 net_max_text_length[] = {
@ -23,6 +27,8 @@ const u16 net_max_text_length[] = {
[NET_IP6] = 43, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */ [NET_IP6] = 43, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
[NET_VPN4] = 40, /* "4294967296:4294967296 255.255.255.255/32" */ [NET_VPN4] = 40, /* "4294967296:4294967296 255.255.255.255/32" */
[NET_VPN6] = 65, /* "4294967296:4294967296 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */ [NET_VPN6] = 65, /* "4294967296:4294967296 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */
[NET_ROA4] = 32, /* "255.255.255.255/32 ASN:4294967295" */
[NET_ROA6] = 58, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128 ASN:4294967295" */
}; };
@ -41,6 +47,10 @@ net_format(const net_addr *N, char *buf, int buflen)
return bsnprintf(buf, buflen, "%u:%u %I4/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen); return bsnprintf(buf, buflen, "%u:%u %I4/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
case NET_VPN6: case NET_VPN6:
return bsnprintf(buf, buflen, "%u:%u %I6/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen); return bsnprintf(buf, buflen, "%u:%u %I6/%d", (u32) (n->vpn6.rd >> 32), (u32) n->vpn6.rd, n->vpn6.prefix, n->vpn6.pxlen);
case NET_ROA4:
return bsnprintf(buf, buflen, "%I4/%d ASN:%u", n->roa4.prefix, n->roa4.pxlen, n->roa4.asn);
case NET_ROA6:
return bsnprintf(buf, buflen, "%I6/%d ASN:%u", n->roa6.prefix, n->roa6.pxlen, n->roa6.asn);
} }
return 0; return 0;
@ -53,10 +63,12 @@ net_pxmask(const net_addr *a)
{ {
case NET_IP4: case NET_IP4:
case NET_VPN4: case NET_VPN4:
case NET_ROA4:
return ipa_from_ip4(ip4_mkmask(net4_pxlen(a))); return ipa_from_ip4(ip4_mkmask(net4_pxlen(a)));
case NET_IP6: case NET_IP6:
case NET_VPN6: case NET_VPN6:
case NET_ROA6:
return ipa_from_ip6(ip6_mkmask(net6_pxlen(a))); return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
default: default:
@ -80,6 +92,10 @@ net_compare(const net_addr *a, const net_addr *b)
return net_compare_vpn4((const net_addr_vpn4 *) a, (const net_addr_vpn4 *) b); return net_compare_vpn4((const net_addr_vpn4 *) a, (const net_addr_vpn4 *) b);
case NET_VPN6: case NET_VPN6:
return net_compare_vpn6((const net_addr_vpn6 *) a, (const net_addr_vpn6 *) b); return net_compare_vpn6((const net_addr_vpn6 *) a, (const net_addr_vpn6 *) b);
case NET_ROA4:
return net_compare_roa4((const net_addr_roa4 *) a, (const net_addr_roa4 *) b);
case NET_ROA6:
return net_compare_roa6((const net_addr_roa6 *) a, (const net_addr_roa6 *) b);
} }
return 0; return 0;
} }
@ -91,10 +107,12 @@ net_validate(const net_addr *N)
{ {
case NET_IP4: case NET_IP4:
case NET_VPN4: case NET_VPN4:
case NET_ROA4:
return net_validate_ip4((net_addr_ip4 *) N); return net_validate_ip4((net_addr_ip4 *) N);
case NET_IP6: case NET_IP6:
case NET_VPN6: case NET_VPN6:
case NET_ROA6:
return net_validate_ip6((net_addr_ip6 *) N); return net_validate_ip6((net_addr_ip6 *) N);
default: default:
@ -111,10 +129,12 @@ net_normalize(net_addr *N)
{ {
case NET_IP4: case NET_IP4:
case NET_VPN4: case NET_VPN4:
case NET_ROA4:
return net_normalize_ip4(&n->ip4); return net_normalize_ip4(&n->ip4);
case NET_IP6: case NET_IP6:
case NET_VPN6: case NET_VPN6:
case NET_ROA6:
return net_normalize_ip6(&n->ip6); return net_normalize_ip6(&n->ip6);
} }
} }
@ -128,10 +148,12 @@ net_classify(const net_addr *N)
{ {
case NET_IP4: case NET_IP4:
case NET_VPN4: case NET_VPN4:
case NET_ROA4:
return ip4_zero(n->ip4.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip4_classify(n->ip4.prefix); return ip4_zero(n->ip4.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip4_classify(n->ip4.prefix);
case NET_IP6: case NET_IP6:
case NET_VPN6: case NET_VPN6:
case NET_ROA6:
return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix); return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);
} }
@ -145,12 +167,14 @@ ipa_in_netX(const ip_addr a, const net_addr *n)
{ {
case NET_IP4: case NET_IP4:
case NET_VPN4: case NET_VPN4:
case NET_ROA4:
if (!ipa_is_ip4(a)) return 0; if (!ipa_is_ip4(a)) return 0;
return ip4_zero(ip4_and(ip4_xor(ipa_to_ip4(a), net4_prefix(n)), return ip4_zero(ip4_and(ip4_xor(ipa_to_ip4(a), net4_prefix(n)),
ip4_mkmask(net4_pxlen(n)))); ip4_mkmask(net4_pxlen(n))));
case NET_IP6: case NET_IP6:
case NET_VPN6: case NET_VPN6:
case NET_ROA6:
if (ipa_is_ip4(a)) return 0; if (ipa_is_ip4(a)) return 0;
return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)), return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)),
ip6_mkmask(net6_pxlen(n)))); ip6_mkmask(net6_pxlen(n))));

View file

@ -17,7 +17,9 @@
#define NET_IP6 2 #define NET_IP6 2
#define NET_VPN4 3 #define NET_VPN4 3
#define NET_VPN6 4 #define NET_VPN6 4
#define NET_MAX 5 #define NET_ROA4 5
#define NET_ROA6 6
#define NET_MAX 7
typedef struct net_addr { typedef struct net_addr {
u8 type; u8 type;
@ -57,6 +59,23 @@ typedef struct net_addr_vpn6 {
u64 rd; u64 rd;
} net_addr_vpn6; } net_addr_vpn6;
typedef struct net_addr_roa4 {
u8 type;
u8 pxlen;
u16 length;
ip4_addr prefix;
u32 asn;
u8 src;
} net_addr_roa4;
typedef struct net_addr_roa6 {
u8 type;
u8 pxlen;
u16 length;
ip6_addr prefix;
u32 asn;
u8 src;
} net_addr_roa6;
typedef union net_addr_union { typedef union net_addr_union {
net_addr n; net_addr n;
@ -64,6 +83,8 @@ typedef union net_addr_union {
net_addr_ip6 ip6; net_addr_ip6 ip6;
net_addr_vpn4 vpn4; net_addr_vpn4 vpn4;
net_addr_vpn6 vpn6; net_addr_vpn6 vpn6;
net_addr_roa4 roa4;
net_addr_roa6 roa6;
} net_addr_union; } net_addr_union;
@ -130,10 +151,17 @@ static inline ip_addr net_prefix(const net_addr *a)
switch (a->type) switch (a->type)
{ {
case NET_IP4: case NET_IP4:
case NET_VPN4: return ipa_from_ip4(net4_prefix(a)); case NET_VPN4:
case NET_ROA4:
return ipa_from_ip4(net4_prefix(a));
case NET_IP6: case NET_IP6:
case NET_VPN6: return ipa_from_ip6(net6_prefix(a)); case NET_VPN6:
default: return IPA_NONE; case NET_ROA6:
return ipa_from_ip6(net6_prefix(a));
default:
return IPA_NONE;
} }
} }
@ -164,6 +192,12 @@ static inline int net_equal_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *b)
static inline int net_equal_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b) static inline int net_equal_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
{ return !memcmp(a, b, sizeof(net_addr_vpn6)); } { return !memcmp(a, b, sizeof(net_addr_vpn6)); }
static inline int net_equal_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
{ return !memcmp(a, b, sizeof(net_addr_roa4)); }
static inline int net_equal_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
{ return !memcmp(a, b, sizeof(net_addr_roa6)); }
static inline int net_zero_ip4(const net_addr_ip4 *a) static inline int net_zero_ip4(const net_addr_ip4 *a)
{ return !a->pxlen && ip4_zero(a->prefix); } { return !a->pxlen && ip4_zero(a->prefix); }
@ -177,6 +211,12 @@ static inline int net_zero_vpn4(const net_addr_vpn4 *a)
static inline int net_zero_vpn6(const net_addr_vpn6 *a) static inline int net_zero_vpn6(const net_addr_vpn6 *a)
{ return !a->pxlen && ip6_zero(a->prefix) && !a->rd; } { return !a->pxlen && ip6_zero(a->prefix) && !a->rd; }
static inline int net_zero_roa4(const net_addr_roa4 *a)
{ return !a->pxlen && ip4_zero(a->prefix) && !a->asn; }
static inline int net_zero_roa6(const net_addr_roa6 *a)
{ return !a->pxlen && ip6_zero(a->prefix) && !a->asn; }
static inline int net_compare_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b) static inline int net_compare_ip4(const net_addr_ip4 *a, const net_addr_ip4 *b)
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); } { return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
@ -190,6 +230,12 @@ static inline int net_compare_vpn4(const net_addr_vpn4 *a, const net_addr_vpn4 *
static inline int net_compare_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b) static inline int net_compare_vpn6(const net_addr_vpn6 *a, const net_addr_vpn6 *b)
{ return u64_cmp(a->rd, b->rd) ?: ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); } { return u64_cmp(a->rd, b->rd) ?: ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen); }
static inline int net_compare_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b)
{ return ip4_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->asn, b->asn); }
static inline int net_compare_roa6(const net_addr_roa6 *a, const net_addr_roa6 *b)
{ return ip6_compare(a->prefix, b->prefix) ?: uint_cmp(a->pxlen, b->pxlen) ?: uint_cmp(a->asn, b->asn); }
int net_compare(const net_addr *a, const net_addr *b); int net_compare(const net_addr *a, const net_addr *b);
@ -208,6 +254,12 @@ static inline void net_copy_vpn4(net_addr_vpn4 *dst, const net_addr_vpn4 *src)
static inline void net_copy_vpn6(net_addr_vpn6 *dst, const net_addr_vpn6 *src) static inline void net_copy_vpn6(net_addr_vpn6 *dst, const net_addr_vpn6 *src)
{ memcpy(dst, src, sizeof(net_addr_vpn6)); } { memcpy(dst, src, sizeof(net_addr_vpn6)); }
static inline void net_copy_roa4(net_addr_roa4 *dst, const net_addr_roa4 *src)
{ memcpy(dst, src, sizeof(net_addr_roa4)); }
static inline void net_copy_roa6(net_addr_roa6 *dst, const net_addr_roa6 *src)
{ memcpy(dst, src, sizeof(net_addr_roa6)); }
static inline u32 net_hash_ip4(const net_addr_ip4 *n) static inline u32 net_hash_ip4(const net_addr_ip4 *n)
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); } { return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
@ -225,6 +277,12 @@ static inline u32 net_hash_vpn4(const net_addr_vpn4 *n)
static inline u32 net_hash_vpn6(const net_addr_vpn6 *n) static inline u32 net_hash_vpn6(const net_addr_vpn6 *n)
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); } { return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
static inline u32 net_hash_roa4(const net_addr_roa4 *n)
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u32_hash(n->asn); }
static inline u32 net_hash_roa6(const net_addr_roa6 *n)
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u32_hash(n->asn); }
static inline int net_validate_ip4(const net_addr_ip4 *n) static inline int net_validate_ip4(const net_addr_ip4 *n)
{ {

View file

@ -189,6 +189,8 @@ fib_hash(struct fib *f, const net_addr *a)
case NET_IP6: return FIB_HASH(f, a, ip6); case NET_IP6: return FIB_HASH(f, a, ip6);
case NET_VPN4: return FIB_HASH(f, a, vpn4); case NET_VPN4: return FIB_HASH(f, a, vpn4);
case NET_VPN6: return FIB_HASH(f, a, vpn6); case NET_VPN6: return FIB_HASH(f, a, vpn6);
case NET_ROA4: return FIB_HASH(f, a, roa4);
case NET_ROA6: return FIB_HASH(f, a, roa6);
default: bug("invalid type"); default: bug("invalid type");
} }
} }
@ -212,6 +214,8 @@ fib_find(struct fib *f, const net_addr *a)
case NET_IP6: return FIB_FIND(f, a, ip6); case NET_IP6: return FIB_FIND(f, a, ip6);
case NET_VPN4: return FIB_FIND(f, a, vpn4); case NET_VPN4: return FIB_FIND(f, a, vpn4);
case NET_VPN6: return FIB_FIND(f, a, vpn6); case NET_VPN6: return FIB_FIND(f, a, vpn6);
case NET_ROA4: return FIB_FIND(f, a, roa4);
case NET_ROA6: return FIB_FIND(f, a, roa6);
default: bug("invalid type"); default: bug("invalid type");
} }
} }
@ -225,6 +229,8 @@ fib_insert(struct fib *f, const net_addr *a, struct fib_node *e)
case NET_IP6: FIB_INSERT(f, a, e, ip6); return; case NET_IP6: FIB_INSERT(f, a, e, ip6); return;
case NET_VPN4: FIB_INSERT(f, a, e, vpn4); return; case NET_VPN4: FIB_INSERT(f, a, e, vpn4); return;
case NET_VPN6: FIB_INSERT(f, a, e, vpn6); return; case NET_VPN6: FIB_INSERT(f, a, e, vpn6); return;
case NET_ROA4: FIB_INSERT(f, a, e, roa4); return;
case NET_ROA6: FIB_INSERT(f, a, e, roa6); return;
default: bug("invalid type"); default: bug("invalid type");
} }
} }
@ -314,10 +320,12 @@ fib_route(struct fib *f, const net_addr *n)
{ {
case NET_IP4: case NET_IP4:
case NET_VPN4: case NET_VPN4:
case NET_ROA4:
return fib_route_ip4(f, (net_addr_ip4 *) n0); return fib_route_ip4(f, (net_addr_ip4 *) n0);
case NET_IP6: case NET_IP6:
case NET_VPN6: case NET_VPN6:
case NET_ROA6:
return fib_route_ip6(f, (net_addr_ip6 *) n0); return fib_route_ip6(f, (net_addr_ip6 *) n0);
default: default:

View file

@ -112,10 +112,12 @@ net_route(rtable *tab, const net_addr *n)
{ {
case NET_IP4: case NET_IP4:
case NET_VPN4: case NET_VPN4:
case NET_ROA4:
return net_route_ip4(&tab->fib, (net_addr_ip4 *) n0); return net_route_ip4(&tab->fib, (net_addr_ip4 *) n0);
case NET_IP6: case NET_IP6:
case NET_VPN6: case NET_VPN6:
case NET_ROA6:
return net_route_ip6(&tab->fib, (net_addr_ip6 *) n0); return net_route_ip6(&tab->fib, (net_addr_ip6 *) n0);
default: default: