summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/alloca.h2
-rw-r--r--lib/buffer.h5
-rw-r--r--lib/buffer_test.c20
-rw-r--r--lib/ip.h27
-rw-r--r--lib/net.c55
-rw-r--r--lib/net.h70
6 files changed, 165 insertions, 14 deletions
diff --git a/lib/alloca.h b/lib/alloca.h
index f0d61bb4..e5557cdb 100644
--- a/lib/alloca.h
+++ b/lib/alloca.h
@@ -15,4 +15,6 @@
#include <stdlib.h>
#endif
+#define allocz(len) ({ void *_x = alloca(len); memset(_x, 0, len); _x; })
+
#endif
diff --git a/lib/buffer.h b/lib/buffer.h
index a8b11951..6fc18852 100644
--- a/lib/buffer.h
+++ b/lib/buffer.h
@@ -14,7 +14,7 @@
#include "sysdep/config.h"
#define BUFFER(type) struct { type *data; uint used, size; }
-
+#define BUFFER_TYPE(v) typeof(* (v).data)
#define BUFFER_SIZE(v) ((v).size * sizeof(* (v).data))
#define BUFFER_INIT(v,pool,isize) \
@@ -46,6 +46,9 @@
#define BUFFER_FLUSH(v) ({ (v).used = 0; })
+#define BUFFER_WALK(v,n) \
+ for (BUFFER_TYPE(v) *_n = (v).data, n; _n < ((v).data + (v).used) && (n = *_n, 1); _n++)
+
#define BUFFER_SHALLOW_COPY(dst, src) \
({ \
(dst).used = (src).used; \
diff --git a/lib/buffer_test.c b/lib/buffer_test.c
index 55179e82..5b7de330 100644
--- a/lib/buffer_test.c
+++ b/lib/buffer_test.c
@@ -133,6 +133,25 @@ t_buffer_flush(void)
return 1;
}
+static int
+t_buffer_walk(void)
+{
+ int i;
+
+ init_buffer();
+ fill_expected_array();
+ for (i = 0; i < MAX_NUM; i++)
+ BUFFER_PUSH(buf) = expected[i];
+
+ i = 0;
+ BUFFER_WALK(buf, v)
+ bt_assert(v == expected[i++]);
+
+ bt_assert(i == MAX_NUM);
+
+ return 1;
+}
+
int
main(int argc, char *argv[])
{
@@ -142,6 +161,7 @@ main(int argc, char *argv[])
bt_test_suite(t_buffer_pop, "Fill whole buffer (PUSH), a half of elements POP and PUSH new elements");
bt_test_suite(t_buffer_resize, "Init a small buffer and try overfill");
bt_test_suite(t_buffer_flush, "Fill and flush all elements");
+ bt_test_suite(t_buffer_walk, "Fill and walk through buffer");
return bt_exit_value();
}
diff --git a/lib/ip.h b/lib/ip.h
index 86750675..5cfce1f1 100644
--- a/lib/ip.h
+++ b/lib/ip.h
@@ -325,6 +325,33 @@ static inline ip6_addr ip6_hton(ip6_addr a)
static inline ip6_addr ip6_ntoh(ip6_addr a)
{ return _MI6(ntohl(_I0(a)), ntohl(_I1(a)), ntohl(_I2(a)), ntohl(_I3(a))); }
+#define MPLS_MAX_LABEL_STACK 8
+typedef struct mpls_label_stack {
+ uint len;
+ u32 stack[MPLS_MAX_LABEL_STACK];
+} mpls_label_stack;
+
+static inline int
+mpls_get(const char *buf, int buflen, u32 *stack)
+{
+ for (int i=0; (i<MPLS_MAX_LABEL_STACK) && (i*4+3 < buflen); i++)
+ {
+ u32 s = get_u32(buf + i*4);
+ stack[i] = s >> 12;
+ if (s & 0x100)
+ return i+1;
+ }
+ return -1;
+}
+
+static inline int
+mpls_put(char *buf, int len, u32 *stack)
+{
+ for (int i=0; i<len; i++)
+ put_u32(buf + i*4, stack[i] << 12 | (i+1 == len ? 0x100 : 0));
+
+ return len*4;
+}
/*
* Unaligned data access (in network order)
diff --git a/lib/net.c b/lib/net.c
index f283e79f..c29ed299 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -13,7 +13,8 @@ const char * const net_label[] = {
[NET_ROA4] = "roa4",
[NET_ROA6] = "roa6",
[NET_FLOW4] = "flow4",
- [NET_FLOW6] = "flow6"
+ [NET_FLOW6] = "flow6",
+ [NET_MPLS] = "mpls",
};
const u16 net_addr_length[] = {
@@ -24,7 +25,8 @@ const u16 net_addr_length[] = {
[NET_ROA4] = sizeof(net_addr_roa4),
[NET_ROA6] = sizeof(net_addr_roa6),
[NET_FLOW4] = 0,
- [NET_FLOW6] = 0
+ [NET_FLOW6] = 0,
+ [NET_MPLS] = sizeof(net_addr_mpls),
};
const u8 net_max_prefix_length[] = {
@@ -35,7 +37,8 @@ const u8 net_max_prefix_length[] = {
[NET_ROA4] = IP4_MAX_PREFIX_LENGTH,
[NET_ROA6] = IP6_MAX_PREFIX_LENGTH,
[NET_FLOW4] = IP4_MAX_PREFIX_LENGTH,
- [NET_FLOW6] = IP6_MAX_PREFIX_LENGTH
+ [NET_FLOW6] = IP6_MAX_PREFIX_LENGTH,
+ [NET_MPLS] = 0,
};
const u16 net_max_text_length[] = {
@@ -46,14 +49,28 @@ const u16 net_max_text_length[] = {
[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_FLOW4] = 0, /* "flow4 { ... }" */
- [NET_FLOW6] = 0 /* "flow6 { ... }" */
+ [NET_FLOW6] = 0, /* "flow6 { ... }" */
+ [NET_MPLS] = 7, /* "1048575" */
};
int
+rd_format(const u64 rd, char *buf, int buflen)
+{
+ switch (rd >> 48)
+ {
+ case 0: return bsnprintf(buf, buflen, "0:%u:%u", (u32) (rd >> 32), (u32) rd);
+ case 1: return bsnprintf(buf, buflen, "1:%I4:%u", ip4_from_u32(rd >> 16), (u32) (rd & 0xffff));
+ case 2: return bsnprintf(buf, buflen, "2:%u:%u", (u32) (rd >> 16), (u32) (rd & 0xffff));
+ default: return bsnprintf(buf, buflen, "X:%08x:%08x", (u32) (rd >> 32), (u32) rd);
+ }
+}
+
+int
net_format(const net_addr *N, char *buf, int buflen)
{
net_addr_union *n = (void *) N;
+ buf[0] = 0;
switch (n->n.type)
{
@@ -62,9 +79,18 @@ net_format(const net_addr *N, char *buf, int buflen)
case NET_IP6:
return bsnprintf(buf, buflen, "%I6/%d", n->ip6.prefix, n->ip6.pxlen);
case NET_VPN4:
- return bsnprintf(buf, buflen, "%u:%u %I4/%d", (u32) (n->vpn4.rd >> 32), (u32) n->vpn4.rd, n->vpn4.prefix, n->vpn4.pxlen);
+ {
+ int c = rd_format(n->vpn4.rd, buf, buflen);
+ ADVANCE(buf, buflen, c);
+ return bsnprintf(buf, buflen, " %I4/%d", n->vpn4.prefix, n->vpn4.pxlen);
+ }
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);
+ {
+ /* XXX: RD format is specified for VPN4; not found any for VPN6, reusing the same as for VPN4 */
+ int c = rd_format(n->vpn6.rd, buf, buflen);
+ ADVANCE(buf, buflen, c);
+ return bsnprintf(buf, buflen, " %I6/%d", n->vpn6.prefix, n->vpn6.pxlen);
+ }
case NET_ROA4:
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:
@@ -73,9 +99,11 @@ net_format(const net_addr *N, char *buf, int buflen)
return flow4_net_format(buf, buflen, &n->flow4);
case NET_FLOW6:
return flow6_net_format(buf, buflen, &n->flow6);
+ case NET_MPLS:
+ return bsnprintf(buf, buflen, "%u", n->mpls.label);
}
- return 0;
+ bug("unknown network type");
}
ip_addr
@@ -95,6 +123,7 @@ net_pxmask(const net_addr *a)
case NET_FLOW6:
return ipa_from_ip6(ip6_mkmask(net6_pxlen(a)));
+ case NET_MPLS:
default:
return IPA_NONE;
}
@@ -124,6 +153,8 @@ net_compare(const net_addr *a, const net_addr *b)
return net_compare_flow4((const net_addr_flow4 *) a, (const net_addr_flow4 *) b);
case NET_FLOW6:
return net_compare_flow6((const net_addr_flow6 *) a, (const net_addr_flow6 *) b);
+ case NET_MPLS:
+ return net_compare_mpls((const net_addr_mpls *) a, (const net_addr_mpls *) b);
}
return 0;
}
@@ -165,6 +196,9 @@ net_validate(const net_addr *N)
case NET_FLOW6:
return net_validate_ip6((net_addr_ip6 *) N);
+ case NET_MPLS:
+ return net_validate_mpls((net_addr_mpls *) N);
+
default:
return 0;
}
@@ -188,6 +222,9 @@ net_normalize(net_addr *N)
case NET_ROA6:
case NET_FLOW6:
return net_normalize_ip6(&n->ip6);
+
+ case NET_MPLS:
+ return;
}
}
@@ -209,6 +246,9 @@ net_classify(const net_addr *N)
case NET_ROA6:
case NET_FLOW6:
return ip6_zero(n->ip6.prefix) ? (IADDR_HOST | SCOPE_UNIVERSE) : ip6_classify(&n->ip6.prefix);
+
+ case NET_MPLS:
+ return IADDR_HOST | SCOPE_UNIVERSE;
}
return IADDR_INVALID;
@@ -235,6 +275,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 f07e6b24..16a18479 100644
--- a/lib/net.h
+++ b/lib/net.h
@@ -21,7 +21,8 @@
#define NET_ROA6 6
#define NET_FLOW4 7
#define NET_FLOW6 8
-#define NET_MAX 9
+#define NET_MPLS 9
+#define NET_MAX 10
#define NB_IP4 (1 << NET_IP4)
#define NB_IP6 (1 << NET_IP6)
@@ -31,6 +32,7 @@
#define NB_ROA6 (1 << NET_ROA6)
#define NB_FLOW4 (1 << NET_FLOW4)
#define NB_FLOW6 (1 << NET_FLOW6)
+#define NB_MPLS (1 << NET_MPLS)
#define NB_IP (NB_IP4 | NB_IP6)
#define NB_ANY 0xffffffff
@@ -108,6 +110,13 @@ typedef struct net_addr_flow6 {
byte data[0];
} net_addr_flow6;
+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;
@@ -118,6 +127,7 @@ typedef union net_addr_union {
net_addr_roa6 roa6;
net_addr_flow4 flow4;
net_addr_flow6 flow6;
+ net_addr_mpls mpls;
} net_addr_union;
@@ -153,6 +163,8 @@ extern const u16 net_max_text_length[];
#define NET_ADDR_FLOW6(prefix,pxlen,dlen) \
((net_addr_flow6) { NET_FLOW6, pxlen, sizeof(net_addr_ip6) + dlen, prefix })
+#define NET_ADDR_MPLS(label) \
+ ((net_addr_mpls) { NET_MPLS, 20, sizeof(net_addr_mpls), label })
static inline void net_fill_ip4(net_addr *a, ip4_addr prefix, uint pxlen)
@@ -173,6 +185,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))
@@ -215,6 +230,9 @@ static inline int net_is_ip(const net_addr *a)
static inline int net_is_roa(const net_addr *a)
{ return (a->type == NET_ROA4) || (a->type == NET_ROA6); }
+static inline int net_is_vpn(const net_addr *a)
+{ return (a->type == NET_VPN4) || (a->type == NET_VPN6); }
+
static inline ip4_addr net4_prefix(const net_addr *a)
{ return ((net_addr_ip4 *) a)->prefix; }
@@ -238,11 +256,20 @@ static inline ip_addr net_prefix(const net_addr *a)
case NET_FLOW6:
return ipa_from_ip6(net6_prefix(a));
+ case NET_MPLS:
default:
return IPA_NONE;
}
}
+static inline u32 net_mpls(const net_addr *a)
+{
+ if (a->type == NET_MPLS)
+ return ((net_addr_mpls *) a)->label;
+
+ bug("Can't call net_mpls on non-mpls net_addr");
+}
+
static inline uint net4_pxlen(const net_addr *a)
{ return a->pxlen; }
@@ -254,6 +281,18 @@ static inline uint net_pxlen(const net_addr *a)
ip_addr net_pxmask(const net_addr *a);
+static inline u64 net_rd(const net_addr *a)
+{
+ switch (a->type)
+ {
+ case NET_VPN4:
+ return ((net_addr_vpn4 *)a)->rd;
+ case NET_VPN6:
+ return ((net_addr_vpn6 *)a)->rd;
+ }
+ return 0;
+}
+
static inline int net_equal(const net_addr *a, const net_addr *b)
{ return (a->length == b->length) && !memcmp(a, b, a->length); }
@@ -282,6 +321,9 @@ static inline int net_equal_flow4(const net_addr_flow4 *a, const net_addr_flow4
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_mpls(const net_addr_mpls *a, const net_addr_mpls *b)
+{ return !memcmp(a, b, sizeof(net_addr_mpls)); }
+
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); }
@@ -314,6 +356,8 @@ static inline int net_zero_flow4(const net_addr_flow4 *a)
static inline int net_zero_flow6(const net_addr_flow6 *a)
{ return !a->pxlen && ip6_zero(a->prefix) && !a->data; }
+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)
@@ -340,6 +384,9 @@ static inline int net_compare_flow4(const net_addr_flow4 *a, const net_addr_flow
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)); }
+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);
@@ -370,6 +417,13 @@ static inline void net_copy_flow4(net_addr_flow4 *dst, const net_addr_flow4 *src
static inline void net_copy_flow6(net_addr_flow6 *dst, const net_addr_flow6 *src)
{ memcpy(dst, src, src->length); }
+static inline void net_copy_mpls(net_addr_mpls *dst, const net_addr_mpls *src)
+{ memcpy(dst, src, sizeof(net_addr_mpls)); }
+
+
+/* XXXX */
+static inline u32 u64_hash(u64 a)
+{ return u32_hash(a); }
static inline u32 net_hash_ip4(const net_addr_ip4 *n)
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
@@ -377,10 +431,6 @@ static inline u32 net_hash_ip4(const net_addr_ip4 *n)
static inline u32 net_hash_ip6(const net_addr_ip6 *n)
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
-/* XXXX */
-static inline u32 u64_hash(u64 a)
-{ return u32_hash(a); }
-
static inline u32 net_hash_vpn4(const net_addr_vpn4 *n)
{ return ip4_hash(n->prefix) ^ ((u32) n->pxlen << 26) ^ u64_hash(n->rd); }
@@ -399,6 +449,9 @@ static inline u32 net_hash_flow4(const net_addr_flow4 *n)
static inline u32 net_hash_flow6(const net_addr_flow6 *n)
{ return ip6_hash(n->prefix) ^ ((u32) n->pxlen << 26); }
+static inline u32 net_hash_mpls(const net_addr_mpls *n)
+{ return n->label; }
+
u32 net_hash(const net_addr *a);
@@ -414,6 +467,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);
@@ -428,7 +486,7 @@ void net_normalize(net_addr *N);
int net_classify(const net_addr *N);
int net_format(const net_addr *N, char *buf, int buflen);
-
+int rd_format(const u64 rd, char *buf, int buflen);
int ipa_in_netX(const ip_addr A, const net_addr *N);
int net_in_netX(const net_addr *A, const net_addr *N);