diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/net.c | 15 | ||||
-rw-r--r-- | lib/net.h | 43 |
2 files changed, 57 insertions, 1 deletions
@@ -16,6 +16,7 @@ const char * const net_label[] = { [NET_FLOW6] = "flow6", [NET_IP6_SADR]= "ipv6-sadr", [NET_MPLS] = "mpls", + [NET_ASPA] = "aspa", }; const u16 net_addr_length[] = { @@ -29,6 +30,7 @@ const u16 net_addr_length[] = { [NET_FLOW6] = 0, [NET_IP6_SADR]= sizeof(net_addr_ip6_sadr), [NET_MPLS] = sizeof(net_addr_mpls), + [NET_ASPA] = sizeof(net_addr_aspa), }; const u8 net_max_prefix_length[] = { @@ -42,6 +44,7 @@ const u8 net_max_prefix_length[] = { [NET_FLOW6] = IP6_MAX_PREFIX_LENGTH, [NET_IP6_SADR]= IP6_MAX_PREFIX_LENGTH, [NET_MPLS] = 0, + [NET_ASPA] = 0, }; const u16 net_max_text_length[] = { @@ -55,6 +58,7 @@ const u16 net_max_text_length[] = { [NET_FLOW6] = 0, /* "flow6 { ... }" */ [NET_IP6_SADR]= 92, /* "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128 from ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" */ [NET_MPLS] = 7, /* "1048575" */ + [NET_ASPA] = 10, /* "4294967295" */ }; /* There should be no implicit padding in net_addr structures */ @@ -69,6 +73,7 @@ STATIC_ASSERT(sizeof(net_addr_flow4) == 8); STATIC_ASSERT(sizeof(net_addr_flow6) == 20); STATIC_ASSERT(sizeof(net_addr_ip6_sadr) == 40); STATIC_ASSERT(sizeof(net_addr_mpls) == 8); +STATIC_ASSERT(sizeof(net_addr_aspa) == 8); int @@ -123,6 +128,8 @@ net_format(const net_addr *N, char *buf, int buflen) return bsnprintf(buf, buflen, "%I6/%d from %I6/%d", n->ip6_sadr.dst_prefix, n->ip6_sadr.dst_pxlen, n->ip6_sadr.src_prefix, n->ip6_sadr.src_pxlen); case NET_MPLS: return bsnprintf(buf, buflen, "%u", n->mpls.label); + case NET_ASPA: + return bsnprintf(buf, buflen, "%u", n->aspa.asn); } bug("unknown network type"); @@ -147,6 +154,7 @@ net_pxmask(const net_addr *a) return ipa_from_ip6(ip6_mkmask(net6_pxlen(a))); case NET_MPLS: + case NET_ASPA: default: return IPA_NONE; } @@ -180,6 +188,8 @@ net_compare(const net_addr *a, const net_addr *b) return net_compare_ip6_sadr((const net_addr_ip6_sadr *) a, (const net_addr_ip6_sadr *) b); case NET_MPLS: return net_compare_mpls((const net_addr_mpls *) a, (const net_addr_mpls *) b); + case NET_ASPA: + return net_compare_aspa((const net_addr_aspa *) a, (const net_addr_aspa *) b); } return 0; } @@ -201,6 +211,7 @@ net_hash(const net_addr *n) case NET_FLOW6: return NET_HASH(n, flow6); case NET_IP6_SADR: return NET_HASH(n, ip6_sadr); case NET_MPLS: return NET_HASH(n, mpls); + case NET_ASPA: return NET_HASH(n, aspa); default: bug("invalid type"); } } @@ -223,6 +234,7 @@ net_validate(const net_addr *n) case NET_FLOW6: return NET_VALIDATE(n, flow6); case NET_IP6_SADR: return NET_VALIDATE(n, ip6_sadr); case NET_MPLS: return NET_VALIDATE(n, mpls); + case NET_ASPA: return NET_VALIDATE(n, aspa); default: return 0; } } @@ -250,6 +262,7 @@ net_normalize(net_addr *N) return net_normalize_ip6_sadr(&n->ip6_sadr); case NET_MPLS: + case NET_ASPA: return; } } @@ -277,6 +290,7 @@ net_classify(const net_addr *N) return ip6_zero(n->ip6_sadr.dst_prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6_sadr.dst_prefix); case NET_MPLS: + case NET_ASPA: return IADDR_HOST | SCOPE_UNIVERSE; } @@ -310,6 +324,7 @@ ipa_in_netX(const ip_addr a, const net_addr *n) ip6_mkmask(net6_pxlen(n)))); case NET_MPLS: + case NET_ASPA: default: return 0; } @@ -23,7 +23,8 @@ #define NET_FLOW6 8 #define NET_IP6_SADR 9 #define NET_MPLS 10 -#define NET_MAX 11 +#define NET_ASPA 11 +#define NET_MAX 12 #define NB_IP4 (1 << NET_IP4) #define NB_IP6 (1 << NET_IP6) @@ -35,6 +36,7 @@ #define NB_FLOW6 (1 << NET_FLOW6) #define NB_IP6_SADR (1 << NET_IP6_SADR) #define NB_MPLS (1 << NET_MPLS) +#define NB_ASPA (1 << NET_ASPA) #define NB_IP (NB_IP4 | NB_IP6) #define NB_VPN (NB_VPN4 | NB_VPN6) @@ -124,6 +126,13 @@ typedef struct net_addr_mpls { u32 label; } net_addr_mpls; +typedef struct net_addr_aspa { + u8 type; + u8 pxlen; + u16 length; + u32 asn; +} net_addr_aspa; + typedef struct net_addr_ip6_sadr { u8 type; u8 dst_pxlen; @@ -145,6 +154,7 @@ typedef union net_addr_union { net_addr_flow6 flow6; net_addr_ip6_sadr ip6_sadr; net_addr_mpls mpls; + net_addr_aspa aspa; } net_addr_union; @@ -183,6 +193,12 @@ extern const u16 net_max_text_length[]; #define NET_ADDR_IP6_SADR(dst_prefix,dst_pxlen,src_prefix,src_pxlen) \ ((net_addr_ip6_sadr) { NET_IP6_SADR, dst_pxlen, sizeof(net_addr_ip6_sadr), dst_prefix, src_pxlen, src_prefix }) +#define NET_ADDR_ASPA(asn) \ + ((net_addr_aspa) { NET_ASPA, 32, sizeof(net_addr_aspa), asn }) + +#define NET_ADDR_ASPA_EXISTS(asn) NET_ADDR_ASPA(asn, asn) +#define NET_ADDR_ASPA_TRANSIT(asn) NET_ADDR_ASPA(asn, 0) + #define NET_ADDR_MPLS(label) \ ((net_addr_mpls) { NET_MPLS, 20, sizeof(net_addr_mpls), label }) @@ -211,6 +227,9 @@ static inline void net_fill_ip6_sadr(net_addr *a, ip6_addr dst_prefix, uint dst_ static inline void net_fill_mpls(net_addr *a, u32 label) { *(net_addr_mpls *)a = NET_ADDR_MPLS(label); } +static inline void net_fill_aspa(net_addr *a, u32 asn) +{ *(net_addr_aspa *)a = NET_ADDR_ASPA(asn); } + static inline void net_fill_ipa(net_addr *a, ip_addr prefix, uint pxlen) { if (ipa_is_ip4(prefix)) @@ -269,6 +288,9 @@ static inline int net_is_roa(const net_addr *a) static inline int net_is_flow(const net_addr *a) { return (a->type == NET_FLOW4) || (a->type == NET_FLOW6); } +static inline int net_is_aspa(const net_addr *a) +{ return (a->type == NET_ASPA); } + static inline int net_is_sadr(const net_addr *a) { return (a->type == NET_IP6_SADR); } @@ -296,6 +318,7 @@ static inline ip_addr net_prefix(const net_addr *a) return ipa_from_ip6(net6_prefix(a)); case NET_MPLS: + case NET_ASPA: default: return IPA_NONE; } @@ -366,6 +389,9 @@ static inline int net_equal_ip6_sadr(const net_addr_ip6_sadr *a, const net_addr_ 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_equal_aspa(const net_addr_aspa *a, const net_addr_aspa *b) +{ return !memcmp(a, b, sizeof(net_addr_aspa)); } + static inline int net_equal_prefix_roa4(const net_addr_roa4 *a, const net_addr_roa4 *b) { return ip4_equal(a->prefix, b->prefix) && (a->pxlen == b->pxlen); } @@ -407,6 +433,9 @@ static inline int net_zero_flow6(const net_addr_flow6 *a) static inline int net_zero_mpls(const net_addr_mpls *a) { return !a->label; } +static inline int net_zero_aspa(const net_addr_aspa *a) +{ return !a->asn; } + 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); } @@ -442,6 +471,9 @@ static inline int net_compare_ip6_sadr(const net_addr_ip6_sadr *a, const net_add static inline int net_compare_mpls(const net_addr_mpls *a, const net_addr_mpls *b) { return uint_cmp(a->label, b->label); } +static inline int net_compare_aspa(const net_addr_aspa *a, const net_addr_aspa *b) +{ return uint_cmp(a->asn, b->asn); } + int net_compare(const net_addr *a, const net_addr *b); @@ -478,6 +510,9 @@ static inline void net_copy_ip6_sadr(net_addr_ip6_sadr *dst, const net_addr_ip6_ static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src) { memcpy(dst, src, sizeof(net_addr_mpls)); } +static inline void net_copy_aspa(net_addr_aspa *dst, const net_addr_aspa *src) +{ memcpy(dst, src, sizeof(net_addr_aspa)); } + static inline u32 px4_hash(ip4_addr prefix, u32 pxlen) { return ip4_hash(prefix) ^ (pxlen << 26); } @@ -521,6 +556,9 @@ static inline u32 net_hash_ip6_sadr(const net_addr_ip6_sadr *n) static inline u32 net_hash_mpls(const net_addr_mpls *n) { return u32_hash(n->label); } +static inline u32 net_hash_aspa(const net_addr_aspa *n) +{ return u32_hash(n->asn); } + u32 net_hash(const net_addr *a); @@ -570,6 +608,9 @@ static inline int net_validate_flow6(const net_addr_flow6 *n) static inline int net_validate_mpls(const net_addr_mpls *n) { return n->label < (1 << 20); } +static inline int net_validate_aspa(const net_addr_aspa *n) +{ return n->asn > 0; } + static inline int net_validate_ip6_sadr(const net_addr_ip6_sadr *n) { return net_validate_px6(n->dst_prefix, n->dst_pxlen) && net_validate_px6(n->src_prefix, n->src_pxlen); } |