From 59ab6f0fcc02143e14baab2b5b8ae114f42258e0 Mon Sep 17 00:00:00 2001 From: Jan Moskyto Matejka Date: Wed, 2 Mar 2016 14:37:18 +0100 Subject: [PATCH] MPLS: added net_addr_mpls variant of net_addr --- lib/net.c | 24 +++++++++++++++++++++--- lib/net.h | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/lib/net.c b/lib/net.c index dbeb545f..f21de0ef 100644 --- a/lib/net.c +++ b/lib/net.c @@ -8,7 +8,8 @@ const char * const net_label[] = { [NET_IP4] = "ipv4", [NET_IP6] = "ipv6", [NET_VPN4] = "vpn4", - [NET_VPN6] = "vpn6" + [NET_VPN6] = "vpn6", + [NET_MPLS] = "mpls", }; const u16 net_addr_length[] = { @@ -17,7 +18,8 @@ const u16 net_addr_length[] = { [NET_VPN4] = sizeof(net_addr_vpn4), [NET_VPN6] = sizeof(net_addr_vpn6), [NET_ROA4] = sizeof(net_addr_roa4), - [NET_ROA6] = sizeof(net_addr_roa6) + [NET_ROA6] = sizeof(net_addr_roa6), + [NET_MPLS] = sizeof(net_addr_mpls), }; const u8 net_max_prefix_length[] = { @@ -26,7 +28,8 @@ const u8 net_max_prefix_length[] = { [NET_VPN4] = IP4_MAX_PREFIX_LENGTH, [NET_VPN6] = IP6_MAX_PREFIX_LENGTH, [NET_ROA4] = IP4_MAX_PREFIX_LENGTH, - [NET_ROA6] = IP6_MAX_PREFIX_LENGTH + [NET_ROA6] = IP6_MAX_PREFIX_LENGTH, + [NET_MPLS] = 0 }; const u16 net_max_text_length[] = { @@ -36,6 +39,7 @@ const u16 net_max_text_length[] = { [NET_VPN6] = 65, /* "4294967296:4294967296 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */ [NET_ROA4] = 34, /* "255.255.255.255/32-32 AS4294967295" */ [NET_ROA6] = 60, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128-128 AS4294967295" */ + [NET_MPLS] = 7, /* "1048575" */ }; @@ -73,6 +77,8 @@ net_format(const net_addr *N, char *buf, int buflen) return bsnprintf(buf, buflen, "%I4/%u-%u AS%u", n->roa4.prefix, n->roa4.pxlen, n->roa4.max_pxlen, n->roa4.asn); case NET_ROA6: return bsnprintf(buf, buflen, "%I6/%u-%u AS%u", n->roa6.prefix, n->roa6.pxlen, n->roa6.max_pxlen, n->roa6.asn); + case NET_MPLS: + return bsnprintf(buf, buflen, "%u", n->mpls.label); } bug("unknown network type"); @@ -93,6 +99,7 @@ net_pxmask(const net_addr *a) case NET_ROA6: return ipa_from_ip6(ip6_mkmask(net6_pxlen(a))); + case NET_MPLS: default: return IPA_NONE; } @@ -118,6 +125,8 @@ net_compare(const net_addr *a, const net_addr *b) 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); + case NET_MPLS: + return net_compare_mpls((const net_addr_mpls *) a, (const net_addr_mpls *) b); } return 0; } @@ -137,6 +146,9 @@ net_validate(const net_addr *N) case NET_ROA6: return net_validate_ip6((net_addr_ip6 *) N); + case NET_MPLS: + return net_validate_mpls((net_addr_mpls *) N); + default: return 0; } @@ -158,6 +170,9 @@ net_normalize(net_addr *N) case NET_VPN6: case NET_ROA6: return net_normalize_ip6(&n->ip6); + + case NET_MPLS: + return; } } @@ -177,6 +192,8 @@ net_classify(const net_addr *N) case NET_VPN6: case NET_ROA6: return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix); + + /* classify probably not needed for NET_MPLS */ } return IADDR_INVALID; @@ -201,6 +218,7 @@ ipa_in_netX(const ip_addr a, const net_addr *n) return ip6_zero(ip6_and(ip6_xor(ipa_to_ip6(a), net6_prefix(n)), ip6_mkmask(net6_pxlen(n)))); + case NET_MPLS: default: return 0; } diff --git a/lib/net.h b/lib/net.h index fbce2811..a853042e 100644 --- a/lib/net.h +++ b/lib/net.h @@ -19,12 +19,14 @@ #define NET_VPN6 4 #define NET_ROA4 5 #define NET_ROA6 6 -#define NET_MAX 7 +#define NET_MPLS 7 +#define NET_MAX 8 #define NB_IP4 (1 << NET_IP4) #define NB_IP6 (1 << NET_IP6) #define NB_VPN4 (1 << NET_VPN4) #define NB_VPN6 (1 << NET_VPN6) +#define NB_MPLS (1 << NET_MPLS) #define NB_IP (NB_IP4 | NB_IP6) #define NB_ANY 0xffffffff @@ -86,6 +88,13 @@ typedef struct net_addr_roa6 { u32 asn; } net_addr_roa6; +typedef struct net_addr_mpls { + u8 type; + u8 pxlen; + u16 length; + u32 label; +} net_addr_mpls; + typedef union net_addr_union { net_addr n; net_addr_ip4 ip4; @@ -94,6 +103,7 @@ typedef union net_addr_union { net_addr_vpn6 vpn6; net_addr_roa4 roa4; net_addr_roa6 roa6; + net_addr_mpls mpls; } net_addr_union; @@ -123,6 +133,8 @@ extern const u16 net_max_text_length[]; #define NET_ADDR_ROA6(prefix,pxlen,max_pxlen,asn) \ ((net_addr_roa6) { NET_ROA6, pxlen, sizeof(net_addr_roa6), prefix, max_pxlen, asn }) +#define NET_ADDR_MPLS(label) \ + ((net_addr_mpls) { NET_MPLS, 0, sizeof(net_addr_mpls), label }) static inline void net_fill_ip4(net_addr *a, ip4_addr prefix, uint pxlen) @@ -143,6 +155,9 @@ static inline void net_fill_roa4(net_addr *a, ip4_addr prefix, uint pxlen, uint static inline void net_fill_roa6(net_addr *a, ip6_addr prefix, uint pxlen, uint max_pxlen, u32 asn) { *(net_addr_roa6 *)a = NET_ADDR_ROA6(prefix, pxlen, max_pxlen, asn); } +static inline void net_fill_mpls(net_addr *a, u32 label) +{ *(net_addr_mpls *)a = NET_ADDR_MPLS(label); } + static inline void net_fill_ipa(net_addr *a, ip_addr prefix, uint pxlen) { if (ipa_is_ip4(prefix)) @@ -190,6 +205,7 @@ static inline ip_addr net_prefix(const net_addr *a) case NET_ROA6: return ipa_from_ip6(net6_prefix(a)); + case NET_MPLS: default: return IPA_NONE; } @@ -228,6 +244,9 @@ static inline int net_equal_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b) 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_equal_mpls(const net_addr_mpls *a, const net_addr_mpls *b) +{ return !memcmp(a, b, sizeof(net_addr_mpls)); } + static inline int net_zero_ip4(const net_addr_ip4 *a) { return !a->pxlen && ip4_zero(a->prefix); } @@ -247,6 +266,9 @@ static inline int net_zero_roa4(const net_addr_roa4 *a) static inline int net_zero_roa6(const net_addr_roa6 *a) { return !a->pxlen && ip6_zero(a->prefix) && !a->max_pxlen && !a->asn; } +static inline int net_zero_mpls(const net_addr_mpls *a) +{ return !a->label; } + 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); } @@ -266,6 +288,9 @@ static inline int net_compare_roa4(const net_addr_roa4 *a, const net_addr_roa4 * 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->max_pxlen, b->max_pxlen) ?: uint_cmp(a->asn, b->asn); } +static inline int net_compare_mpls(const net_addr_mpls *a, const net_addr_mpls *b) +{ return uint_cmp(a->label, b->label); } + int net_compare(const net_addr *a, const net_addr *b); @@ -290,6 +315,8 @@ static inline void net_copy_roa4(net_addr_roa4 *dst, const net_addr_roa4 *src) static inline void net_copy_roa6(net_addr_roa6 *dst, const net_addr_roa6 *src) { memcpy(dst, src, sizeof(net_addr_roa6)); } +static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src) +{ memcpy(dst, src, sizeof(net_addr_mpls)); } static inline u32 net_hash_ip4(const net_addr_ip4 *n) { return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); } @@ -313,6 +340,8 @@ static inline u32 net_hash_roa4(const net_addr_roa4 *n) static inline u32 net_hash_roa6(const net_addr_roa6 *n) { return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); } +static inline u32 net_hash_mpls(const net_addr_mpls *n) +{ return n->label; } static inline int net_validate_ip4(const net_addr_ip4 *n) { @@ -326,6 +355,11 @@ static inline int net_validate_ip6(const net_addr_ip6 *n) ip6_zero(ip6_and(n->prefix, ip6_not(ip6_mkmask(n->pxlen)))); } +static inline int net_validate_mpls(const net_addr_mpls *n) +{ + return n->label < (1<<20); +} + int net_validate(const net_addr *N);