[PATCH 1/4] netifd: vxlan: handle srcport range

Johannes Kimmel fff at bareminimum.eu
Thu Sep 3 22:59:40 EDT 2020


This adds adds the ability to set the source port range for vxlan
interfaces.

By default vxlans will use a random port within the ephermal range as
source ports for packets. This is done to aid scaleability within a
datacenter.

But with these defaults it's impossible to punch through NATs or
traverese most stateful firewalls easily. One solution is to fix the
srcport to the same as dstport.

If only srcportmin is specified, then srcportmax is set in a way that
outgoing packets will only use srcportmin.

If a range is to be specified, srcportmin and srcportmax have to be
specified. srcportmax is exclusive.

If only srcportmax is specified, the value is ignored and defaults are
used.

Signed-off-by: Johannes Kimmel <fff at bareminimum.eu>
---
 system-linux.c | 26 ++++++++++++++++++++++++++
 system.c       |  2 ++
 system.h       |  2 ++
 3 files changed, 30 insertions(+)

diff --git a/system-linux.c b/system-linux.c
index c5583e0..5ff8749 100644
--- a/system-linux.c
+++ b/system-linux.c
@@ -3184,6 +3184,32 @@ static int system_add_vxlan(const char *name, const unsigned int link, struct bl
 	}
 	nla_put_u16(msg, IFLA_VXLAN_PORT, htons(port));
 
+	if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMIN])) {
+		struct ifla_vxlan_port_range srcports = {0,0};
+
+		uint32_t low = blobmsg_get_u32(cur);
+		if (low < 1 || low > 65535 - 1) {
+			ret = -EINVAL;
+			goto failure;
+		}
+
+		srcports.low = htons((uint16_t) low);
+		srcports.high = htons((uint16_t) (low+1));
+
+		if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMAX])) {
+			uint32_t high = blobmsg_get_u32(cur);
+			if (high < 1 || high > 65535) {
+				ret = -EINVAL;
+				goto failure;
+			}
+			if (high > low) {
+				srcports.high = htons((uint16_t) high);
+			}
+		}
+
+		nla_put(msg, IFLA_VXLAN_PORT_RANGE, sizeof(srcports), &srcports);
+	}
+
 	if ((cur = tb_data[VXLAN_DATA_ATTR_RXCSUM])) {
 		bool rxcsum = blobmsg_get_bool(cur);
 		nla_put_u8(msg, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, !rxcsum);
diff --git a/system.c b/system.c
index bbdfef7..4133e55 100644
--- a/system.c
+++ b/system.c
@@ -38,6 +38,8 @@ static const struct blobmsg_policy vxlan_data_attrs[__VXLAN_DATA_ATTR_MAX] = {
 	[VXLAN_DATA_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
 	[VXLAN_DATA_ATTR_RXCSUM] = { .name = "rxcsum", .type = BLOBMSG_TYPE_BOOL },
 	[VXLAN_DATA_ATTR_TXCSUM] = { .name = "txcsum", .type = BLOBMSG_TYPE_BOOL },
+	[VXLAN_DATA_ATTR_SRCPORTMIN] = { .name = "srcportmin", .type = BLOBMSG_TYPE_INT32 },
+	[VXLAN_DATA_ATTR_SRCPORTMAX] = { .name = "srcportmax", .type = BLOBMSG_TYPE_INT32 },
 };
 
 const struct uci_blob_param_list vxlan_data_attr_list = {
diff --git a/system.h b/system.h
index 015987f..bf9e1d7 100644
--- a/system.h
+++ b/system.h
@@ -44,6 +44,8 @@ enum vxlan_data {
 	VXLAN_DATA_ATTR_MACADDR,
 	VXLAN_DATA_ATTR_RXCSUM,
 	VXLAN_DATA_ATTR_TXCSUM,
+	VXLAN_DATA_ATTR_SRCPORTMIN,
+	VXLAN_DATA_ATTR_SRCPORTMAX,
 	__VXLAN_DATA_ATTR_MAX
 };
 
-- 
2.28.0




More information about the openwrt-devel mailing list