Skip to contentSkip to conversation

netfilter: nftables: tcp mss mangling support

PATCH In review Florian Westphal 3 participants 9 messages Aug 8, 2017 – Aug 19, 2017

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter: nftables: tcp mss mangling support

This series adds the needed kernel parts to support tcp mss mangling. First two patches rework exthdr so we don't have to copy-paste too much, patch 3 adds tcp option mangling support.

Last patch allows to retrieve path tcpmss via rt expression, this is so we can support iptables TCPMSS --clamp-to-pmtu by combining the two, i.e.:

nft add rule inet mangle forward tcp option mss set rt mss

author maintainer
#1 lore raw

PATCH 1/4 netfilter: exthdr: factor out tcp option access

---
 net/netfilter/nft_exthdr.c | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)
net/netfilter/nft_exthdr.c +21 -12
index 1ec49fe5845f..921c95f2c583 100644
@@ -61,6 +61,26 @@ static void nft_exthdr_ipv6_eval(const struct nft_expr *expr,
 	regs->verdict.code = NFT_BREAK;
 }
 
+static void *
+nft_tcp_header_pointer(const struct nft_pktinfo *pkt,
+		       unsigned int len, void *buffer, unsigned int *tcphdr_len)
+{
+	struct tcphdr *tcph;
+
+	if (!pkt->tprot_set || pkt->tprot != IPPROTO_TCP)
+		return NULL;
+
+	tcph = skb_header_pointer(pkt->skb, pkt->xt.thoff, sizeof(*tcph), buffer);
+	if (!tcph)
+		return NULL;
+
+	*tcphdr_len = __tcp_hdrlen(tcph);
+	if (*tcphdr_len < sizeof(*tcph) || *tcphdr_len > len)
+		return NULL;
+
+	return skb_header_pointer(pkt->skb, pkt->xt.thoff, *tcphdr_len, buffer);
+}
+
 static void nft_exthdr_tcp_eval(const struct nft_expr *expr,
 				struct nft_regs *regs,
 				const struct nft_pktinfo *pkt)
@@ -72,18 +92,7 @@ static void nft_exthdr_tcp_eval(const struct nft_expr *expr,
 	struct tcphdr *tcph;
 	u8 *opt;
 
-	if (!pkt->tprot_set || pkt->tprot != IPPROTO_TCP)
-		goto err;
-
-	tcph = skb_header_pointer(pkt->skb, pkt->xt.thoff, sizeof(*tcph), buff);
-	if (!tcph)
-		goto err;
-
-	tcphdr_len = __tcp_hdrlen(tcph);
-	if (tcphdr_len < sizeof(*tcph))
-		goto err;
-
-	tcph = skb_header_pointer(pkt->skb, pkt->xt.thoff, tcphdr_len, buff);
+	tcph = nft_tcp_header_pointer(pkt, sizeof(buff), buff, &tcphdr_len);
 	if (!tcph)
 		goto err;
 
Signature · 1 line
2.13.0
author maintainer
#2 lore raw

PATCH 2/4 netfilter: exthdr: split netlink dump function

In reply to Florian Westphal

so eval and uncoming eval_set versions can reuse a common helper.

---
 net/netfilter/nft_exthdr.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
net/netfilter/nft_exthdr.c +11 -5
index 921c95f2c583..e3a6eebe7e0c 100644
@@ -180,12 +180,8 @@ static int nft_exthdr_init(const struct nft_ctx *ctx,
 					   NFT_DATA_VALUE, priv->len);
 }
 
-static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
+static int nft_exthdr_dump_common(struct sk_buff *skb, const struct nft_exthdr *priv)
 {
-	const struct nft_exthdr *priv = nft_expr_priv(expr);
-
-	if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
-		goto nla_put_failure;
 	if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
 		goto nla_put_failure;
 	if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
@@ -202,6 +198,16 @@ static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
 	return -1;
 }
 
+static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
+{
+	const struct nft_exthdr *priv = nft_expr_priv(expr);
+
+	if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
+		return -1;
+
+	return nft_exthdr_dump_common(skb, priv);
+}
+
 static struct nft_expr_type nft_exthdr_type;
 static const struct nft_expr_ops nft_exthdr_ipv6_ops = {
 	.type		= &nft_exthdr_type,
Signature · 1 line
2.13.0
author maintainer
#3 lore raw

PATCH 3/4 netfilter: exthdr: tcp option set support

In reply to Florian Westphal

This allows setting 2 and 4 byte quantities in the tcp option space. Main purpose is to allow native replacement for xt_TCPMSS to work around pmtu blackholes.

Writes to kind and len are now allowed at the moment, it does not seem useful to do this as it causes corruption of the tcp option space.

We can always lift this restriction later if a use-case appears.

---
 include/uapi/linux/netfilter/nf_tables.h |   4 +-
 net/netfilter/nft_exthdr.c               | 164 ++++++++++++++++++++++++++++++-
 2 files changed, 165 insertions(+), 3 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +3 -1
index be25cf69295b..40fd199f7531 100644
@@ -732,7 +732,8 @@ enum nft_exthdr_op {
  * @NFTA_EXTHDR_OFFSET: extension header offset (NLA_U32)
  * @NFTA_EXTHDR_LEN: extension header length (NLA_U32)
  * @NFTA_EXTHDR_FLAGS: extension header flags (NLA_U32)
- * @NFTA_EXTHDR_OP: option match type (NLA_U8)
+ * @NFTA_EXTHDR_OP: option match type (NLA_U32)
+ * @NFTA_EXTHDR_SREG: option match type (NLA_U32)
  */
 enum nft_exthdr_attributes {
 	NFTA_EXTHDR_UNSPEC,
@@ -742,6 +743,7 @@ enum nft_exthdr_attributes {
 	NFTA_EXTHDR_LEN,
 	NFTA_EXTHDR_FLAGS,
 	NFTA_EXTHDR_OP,
+	NFTA_EXTHDR_SREG,
 	__NFTA_EXTHDR_MAX
 };
 #define NFTA_EXTHDR_MAX		(__NFTA_EXTHDR_MAX - 1)
net/netfilter/nft_exthdr.c +162 -2

210 lines not on this page · raw message

Signature · 1 line
2.13.0
author maintainer
#4 lore raw

PATCH 4/4 netfilter: rt: add support to fetch path mss

In reply to Florian Westphal

to be used in combination with tcp option set support to mimic iptables TCPMSS --clamp-mss-to-pmtu.

---
 include/uapi/linux/netfilter/nf_tables.h |  2 +
 net/netfilter/nft_rt.c                   | 65 ++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 40fd199f7531..b49da72efa68 100644
@@ -811,11 +811,13 @@ enum nft_meta_keys {
  * @NFT_RT_CLASSID: realm value of packet's route (skb->dst->tclassid)
  * @NFT_RT_NEXTHOP4: routing nexthop for IPv4
  * @NFT_RT_NEXTHOP6: routing nexthop for IPv6
+ * @NFT_RT_TCPMSS: fetch current path tcp mss
  */
 enum nft_rt_keys {
 	NFT_RT_CLASSID,
 	NFT_RT_NEXTHOP4,
 	NFT_RT_NEXTHOP6,
+	NFT_RT_TCPMSS,
 };
 
 /**
net/netfilter/nft_rt.c +65 -0

95 lines not on this page · raw message

Signature · 1 line
2.13.0
#5 lore raw

Re: [PATCH nf-next 4/4] netfilter: rt: add support to fetch path mss

Quoted 49 lines · Florian Westphal

to be used in combination with tcp option set support to mimic iptables TCPMSS --clamp-mss-to-pmtu.

---
 include/uapi/linux/netfilter/nf_tables.h |  2 +
 net/netfilter/nft_rt.c                   | 65 ++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 40fd199f7531..b49da72efa68 100644
@@ -811,11 +811,13 @@ enum nft_meta_keys {
  * @NFT_RT_CLASSID: realm value of packet's route (skb->dst->tclassid)
  * @NFT_RT_NEXTHOP4: routing nexthop for IPv4
  * @NFT_RT_NEXTHOP6: routing nexthop for IPv6
+ * @NFT_RT_TCPMSS: fetch current path tcp mss
  */
 enum nft_rt_keys {
 	NFT_RT_CLASSID,
 	NFT_RT_NEXTHOP4,
 	NFT_RT_NEXTHOP6,
+	NFT_RT_TCPMSS,
 };
 
 /**
net/netfilter/nft_rt.c +14 -0
index c7383d8f88d0..69ed601d6fc6 100644
@@ -23,6 +23,41 @@ struct nft_rt {
 	enum nft_registers	dreg:8;
 };
 
+static u16 get_tcpmss(const struct nft_pktinfo *pkt, const struct dst_entry *skbdst)
+{
+	u32 minlen = sizeof(struct ipv6hdr), mtu = dst_mtu(skbdst);
+	const struct sk_buff *skb = pkt->skb;
+	const struct nf_afinfo *ai;
+	struct dst_entry *dst;
+	struct flowi fl;
+
+	memset(&fl, 0, sizeof(fl));
+
+	switch (nft_pf(pkt)) {
+	case NFPROTO_IPV4:
+		fl.u.ip4.daddr = ip_hdr(skb)->saddr;
+		minlen = sizeof(struct iphdr);

net/netfilter/nft_rt.c struct nft_rt { · line 49

Quoted 10 lines
net/netfilter/nft_rt.c +10 -0
+		break;
+	case NFPROTO_IPV6:
+		fl.u.ip6.daddr = ipv6_hdr(skb)->saddr;
+		break;
+	}
+
+	ai = nf_get_afinfo(nft_pf(pkt));
+	if (ai)
+		ai->route(nft_net(pkt), &dst, &fl, false);
+

if ai is NULL,

dst is not initialized and might contain garbage.

Quoted 9 lines
net/netfilter/nft_rt.c +9 -0
+	if (dst) {
+		mtu = min(mtu, dst_mtu(dst));
+		dst_release(dst);
+	}
+
+	if (mtu <= minlen || mtu > 0xffff)
+		return TCP_MSS_DEFAULT;
+
+	return mtu - minlen;
author maintainer
#6 lore raw

Re: [PATCH nf-next 4/4] netfilter: rt: add support to fetch path mss

Quoted 3 lines · Eric Dumazet
Quoted 2 lines · Florian Westphal
net/netfilter/nft_rt.c +2 -0
+	struct dst_entry *dst;
+	struct flowi fl;

[..]

Quoted 8 lines

net/netfilter/nft_rt.c struct nft_rt { · line 49

Quoted 4 lines
net/netfilter/nft_rt.c +4 -0
+	ai = nf_get_afinfo(nft_pf(pkt));
+	if (ai)
+		ai->route(nft_net(pkt), &dst, &fl, false);
+

if ai is NULL,

dst is not initialized and might contain garbage.

Right, thanks for pointing this out, I sent a v2.

author maintainer
#7 lore raw

PATCH v2 4/4 netfilter: rt: add support to fetch path mss

In reply to Florian Westphal

to be used in combination with tcp option set support to mimic iptables TCPMSS --clamp-mss-to-pmtu.

v2: Eric Dumazet points out dst must be initialized.

---
 include/uapi/linux/netfilter/nf_tables.h |  2 +
 net/netfilter/nft_rt.c                   | 66 ++++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
index 40fd199f7531..b49da72efa68 100644
@@ -811,11 +811,13 @@ enum nft_meta_keys {
  * @NFT_RT_CLASSID: realm value of packet's route (skb->dst->tclassid)
  * @NFT_RT_NEXTHOP4: routing nexthop for IPv4
  * @NFT_RT_NEXTHOP6: routing nexthop for IPv6
+ * @NFT_RT_TCPMSS: fetch current path tcp mss
  */
 enum nft_rt_keys {
 	NFT_RT_CLASSID,
 	NFT_RT_NEXTHOP4,
 	NFT_RT_NEXTHOP6,
+	NFT_RT_TCPMSS,
 };
 
 /**
net/netfilter/nft_rt.c +66 -0

96 lines not on this page · raw message

Signature · 1 line
2.13.0
#8 lore raw
In reply to Florian Westphal
Quoted 8 lines · Florian Westphal

This series adds the needed kernel parts to support tcp mss mangling. First two patches rework exthdr so we don't have to copy-paste too much, patch 3 adds tcp option mangling support.

Last patch allows to retrieve path tcpmss via rt expression, this is so we can support iptables TCPMSS --clamp-to-pmtu by combining the two, i.e.:

nft add rule inet mangle forward tcp option mss set rt mss

Series applied, thanks Florian.

Please, post your userspace patchset.