diff options
author | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-12-07 15:36:15 +0100 |
---|---|---|
committer | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-12-07 15:54:19 +0100 |
commit | 77234bbbde6bc328871af695e4450e6773adbafa (patch) | |
tree | 0ed60508b521eba6af6c4b852df09fdf8c659154 /lib/net.h | |
parent | b94e5e58dbd33f4d2b9d721c51a9c8c4d8f77bea (diff) |
Basic flow specification support (RFC 5575)
Add flow4/flow6 network and rt-table type and operations, config grammar
and static protocol support.
Squashed flowspec branch from Pavel Tvrdik.
Diffstat (limited to 'lib/net.h')
-rw-r--r-- | lib/net.h | 79 |
1 files changed, 77 insertions, 2 deletions
@@ -19,7 +19,9 @@ #define NET_VPN6 4 #define NET_ROA4 5 #define NET_ROA6 6 -#define NET_MAX 7 +#define NET_FLOW4 7 +#define NET_FLOW6 8 +#define NET_MAX 9 #define NB_IP4 (1 << NET_IP4) #define NB_IP6 (1 << NET_IP6) @@ -27,6 +29,8 @@ #define NB_VPN6 (1 << NET_VPN6) #define NB_ROA4 (1 << NET_ROA4) #define NB_ROA6 (1 << NET_ROA6) +#define NB_FLOW4 (1 << NET_FLOW4) +#define NB_FLOW6 (1 << NET_FLOW6) #define NB_IP (NB_IP4 | NB_IP6) #define NB_ANY 0xffffffff @@ -88,6 +92,22 @@ typedef struct net_addr_roa6 { u32 asn; } net_addr_roa6; +typedef struct net_addr_flow4 { + u8 type; + u8 pxlen; + u16 length; + ip4_addr prefix; + byte data[0]; +} net_addr_flow4; + +typedef struct net_addr_flow6 { + u8 type; + u8 pxlen; + u16 length; + ip6_addr prefix; + byte data[0]; +} net_addr_flow6; + typedef union net_addr_union { net_addr n; net_addr_ip4 ip4; @@ -96,6 +116,8 @@ typedef union net_addr_union { net_addr_vpn6 vpn6; net_addr_roa4 roa4; net_addr_roa6 roa6; + net_addr_flow4 flow4; + net_addr_flow6 flow6; } net_addr_union; @@ -104,7 +126,7 @@ extern const u16 net_addr_length[]; extern const u8 net_max_prefix_length[]; extern const u16 net_max_text_length[]; -#define NET_MAX_TEXT_LENGTH 65 +#define NET_MAX_TEXT_LENGTH 256 #define NET_ADDR_IP4(prefix,pxlen) \ @@ -125,6 +147,12 @@ 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_FLOW4(prefix,pxlen,dlen) \ + ((net_addr_flow4) { NET_FLOW4, pxlen, sizeof(net_addr_ip4) + dlen, prefix }) + +#define NET_ADDR_FLOW6(prefix,pxlen,dlen) \ + ((net_addr_flow6) { NET_FLOW6, pxlen, sizeof(net_addr_ip6) + dlen, prefix }) + static inline void net_fill_ip4(net_addr *a, ip4_addr prefix, uint pxlen) @@ -161,6 +189,19 @@ static inline void net_fill_ip_host(net_addr *a, ip_addr prefix) net_fill_ip6(a, ipa_to_ip6(prefix), IP6_MAX_PREFIX_LENGTH); } +static inline void net_fill_flow4(net_addr *a, ip4_addr prefix, uint pxlen, byte *data, uint dlen) +{ + net_addr_flow4 *f = (void *) a; + *f = NET_ADDR_FLOW4(prefix, pxlen, dlen); + memcpy(f->data, data, dlen); +} + +static inline void net_fill_flow6(net_addr *a, ip6_addr prefix, uint pxlen, byte *data, uint dlen) +{ + net_addr_flow6 *f = (void *) a; + *f = NET_ADDR_FLOW6(prefix, pxlen, dlen); + memcpy(f->data, data, dlen); +} static inline int net_val_match(u8 type, u32 mask) { return !!((1 << type) & mask); } @@ -188,11 +229,13 @@ static inline ip_addr net_prefix(const net_addr *a) case NET_IP4: case NET_VPN4: case NET_ROA4: + case NET_FLOW4: return ipa_from_ip4(net4_prefix(a)); case NET_IP6: case NET_VPN6: case NET_ROA6: + case NET_FLOW6: return ipa_from_ip6(net6_prefix(a)); default: @@ -233,6 +276,13 @@ 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_flow4(const net_addr_flow4 *a, const net_addr_flow4 *b) +{ return net_equal((const net_addr *) a, (const net_addr *) b); } + +static inline int net_equal_flow6(const net_addr_flow6 *a, const net_addr_flow6 *b) +{ return net_equal((const net_addr *) a, (const net_addr *) b); } + + 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); } @@ -258,6 +308,13 @@ 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_flow4(const net_addr_flow4 *a) +{ return !a->pxlen && ip4_zero(a->prefix) && !a->data; } + +static inline int net_zero_flow6(const net_addr_flow6 *a) +{ return !a->pxlen && ip6_zero(a->prefix) && !a->data; } + + 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); } @@ -277,6 +334,12 @@ 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_flow4(const net_addr_flow4 *a, const net_addr_flow4 *b) +{ 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)); } + +static inline int net_compare_flow6(const net_addr_flow6 *a, const net_addr_flow6 *b) +{ 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)); } + int net_compare(const net_addr *a, const net_addr *b); @@ -301,6 +364,12 @@ 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_flow4(net_addr_flow4 *dst, const net_addr_flow4 *src) +{ memcpy(dst, src, src->length); } + +static inline void net_copy_flow6(net_addr_flow6 *dst, const net_addr_flow6 *src) +{ memcpy(dst, src, src->length); } + static inline u32 net_hash_ip4(const net_addr_ip4 *n) { return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); } @@ -324,6 +393,12 @@ 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_flow4(const net_addr_flow4 *n) +{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); } + +static inline u32 net_hash_flow6(const net_addr_flow6 *n) +{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); } + u32 net_hash(const net_addr *a); |