Skip to contentSkip to conversation

netfilter: Prepare netfilter to future .flowi4_tos conversion.

PATCH In review Guillaume Nault 2 participants 7 messages Nov 14, 2024 – Nov 15, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover netfilter: Prepare netfilter to future .flowi4_tos conversion.

There are multiple occasions where Netfilter code needs to perform route lookups and initialise struct flowi4. As we're in the process of converting the .flowi4_tos field to dscp_t, we need to convert the users so that they have a dscp_t value at hand, rather than a __u8.

All netfilter users get the DSCP (TOS) value from IPv4 packet headers. So we just need to use the new ip4h_dscp() helper to get a dscp_t variable.

Converting .flowi4_tos to dscp_t will allow to detect regressions where ECN bits are mistakenly treated as DSCP when doing route lookups.

Guillaume Nault (5):
netfilter: ipv4: Convert ip_route_me_harder() to dscp_t.
netfilter: flow_offload: Convert nft_flow_route() to dscp_t.
netfilter: rpfilter: Convert rpfilter_mt() to dscp_t.
netfilter: nft_fib: Convert nft_fib4_eval() to dscp_t.
netfilter: nf_dup4: Convert nf_dup_ipv4_route() to dscp_t.

 net/ipv4/netfilter.c              | 2 +-
 net/ipv4/netfilter/ipt_rpfilter.c | 2 +-
 net/ipv4/netfilter/nf_dup_ipv4.c  | 2 +-
 net/ipv4/netfilter/nft_fib_ipv4.c | 3 ++-
 net/netfilter/nft_flow_offload.c  | 4 ++--
 5 files changed, 7 insertions(+), 6 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/5 netfilter: ipv4: Convert ip_route_me_harder() to dscp_t.

Use ip4h_dscp()instead of reading iph->tos directly.

ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.

---
 net/ipv4/netfilter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/netfilter.c +1 -1
index e0aab66cd925..08bc3f2c0078 100644
@@ -44,7 +44,7 @@ int ip_route_me_harder(struct net *net, struct sock *sk, struct sk_buff *skb, un
 	 */
 	fl4.daddr = iph->daddr;
 	fl4.saddr = saddr;
-	fl4.flowi4_tos = iph->tos & INET_DSCP_MASK;
+	fl4.flowi4_tos = inet_dscp_to_dsfield(ip4h_dscp(iph));
 	fl4.flowi4_oif = sk ? sk->sk_bound_dev_if : 0;
 	fl4.flowi4_l3mdev = l3mdev_master_ifindex(dev);
 	fl4.flowi4_mark = skb->mark;
Signature · 1 line
2.39.2
#2 lore raw

PATCH 2/5 netfilter: flow_offload: Convert nft_flow_route() to dscp_t.

In reply to Guillaume Nault

Use ip4h_dscp()instead of reading ip_hdr()->tos directly.

ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.

Also, remove the comment about the net/ip.h include file, since it's now required for the ip4h_dscp() helper too.

---
 net/netfilter/nft_flow_offload.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nft_flow_offload.c +2 -2
index 65199c23c75c..3b474d235663 100644
@@ -8,7 +8,7 @@
 #include <linux/spinlock.h>
 #include <linux/netfilter/nf_conntrack_common.h>
 #include <linux/netfilter/nf_tables.h>
-#include <net/ip.h> /* for ipv4 options. */
+#include <net/ip.h>
 #include <net/inet_dscp.h>
 #include <net/netfilter/nf_tables.h>
 #include <net/netfilter/nf_tables_core.h>
@@ -236,7 +236,7 @@ static int nft_flow_route(const struct nft_pktinfo *pkt,
 		fl.u.ip4.saddr = ct->tuplehash[!dir].tuple.src.u3.ip;
 		fl.u.ip4.flowi4_oif = nft_in(pkt)->ifindex;
 		fl.u.ip4.flowi4_iif = this_dst->dev->ifindex;
-		fl.u.ip4.flowi4_tos = ip_hdr(pkt->skb)->tos & INET_DSCP_MASK;
+		fl.u.ip4.flowi4_tos = inet_dscp_to_dsfield(ip4h_dscp(ip_hdr(pkt->skb)));
 		fl.u.ip4.flowi4_mark = pkt->skb->mark;
 		fl.u.ip4.flowi4_flags = FLOWI_FLAG_ANYSRC;
 		break;
Signature · 1 line
2.39.2
#3 lore raw

PATCH 3/5 netfilter: rpfilter: Convert rpfilter_mt() to dscp_t.

In reply to Guillaume Nault

Use ip4h_dscp() instead of reading iph->tos directly.

ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.

---
 net/ipv4/netfilter/ipt_rpfilter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/netfilter/ipt_rpfilter.c +1 -1
index 1ce7a1655b97..a27782d7653e 100644
@@ -76,7 +76,7 @@ static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
 	flow.daddr = iph->saddr;
 	flow.saddr = rpfilter_get_saddr(iph->daddr);
 	flow.flowi4_mark = info->flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
-	flow.flowi4_tos = iph->tos & INET_DSCP_MASK;
+	flow.flowi4_tos = inet_dscp_to_dsfield(ip4h_dscp(iph));
 	flow.flowi4_scope = RT_SCOPE_UNIVERSE;
 	flow.flowi4_l3mdev = l3mdev_master_ifindex_rcu(xt_in(par));
 	flow.flowi4_uid = sock_net_uid(xt_net(par), NULL);
Signature · 1 line
2.39.2
#4 lore raw

PATCH 4/5 netfilter: nft_fib: Convert nft_fib4_eval() to dscp_t.

In reply to Guillaume Nault

Use ip4h_dscp() instead of reading iph->tos directly.

ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.

---
 net/ipv4/netfilter/nft_fib_ipv4.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/netfilter/nft_fib_ipv4.c +2 -1
index 09fff5d424ef..625adbc42037 100644
@@ -11,6 +11,7 @@
 #include <net/netfilter/nft_fib.h>
 
 #include <net/inet_dscp.h>
+#include <net/ip.h>
 #include <net/ip_fib.h>
 #include <net/route.h>
 
@@ -107,7 +108,7 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	if (priv->flags & NFTA_FIB_F_MARK)
 		fl4.flowi4_mark = pkt->skb->mark;
 
-	fl4.flowi4_tos = iph->tos & INET_DSCP_MASK;
+	fl4.flowi4_tos = inet_dscp_to_dsfield(ip4h_dscp(iph));
 
 	if (priv->flags & NFTA_FIB_F_DADDR) {
 		fl4.daddr = iph->daddr;
Signature · 1 line
2.39.2
#5 lore raw

PATCH 5/5 netfilter: nf_dup4: Convert nf_dup_ipv4_route() to dscp_t.

In reply to Guillaume Nault

Use ip4h_dscp() instead of reading iph->tos directly.

ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.

---
 net/ipv4/netfilter/nf_dup_ipv4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/netfilter/nf_dup_ipv4.c +1 -1
index ec94ee1051c7..25e1e8eb18dd 100644
@@ -33,7 +33,7 @@ static bool nf_dup_ipv4_route(struct net *net, struct sk_buff *skb,
 		fl4.flowi4_oif = oif;
 
 	fl4.daddr = gw->s_addr;
-	fl4.flowi4_tos = iph->tos & INET_DSCP_MASK;
+	fl4.flowi4_tos = inet_dscp_to_dsfield(ip4h_dscp(iph));
 	fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
 	fl4.flowi4_flags = FLOWI_FLAG_KNOWN_NH;
 	rt = ip_route_output_key(net, &fl4);
Signature · 1 line
2.39.2
#6 lore raw
In reply to Guillaume Nault
Quoted 11 lines · Guillaume Nault

There are multiple occasions where Netfilter code needs to perform route lookups and initialise struct flowi4. As we're in the process of converting the .flowi4_tos field to dscp_t, we need to convert the users so that they have a dscp_t value at hand, rather than a __u8.

All netfilter users get the DSCP (TOS) value from IPv4 packet headers. So we just need to use the new ip4h_dscp() helper to get a dscp_t variable.

Converting .flowi4_tos to dscp_t will allow to detect regressions where ECN bits are mistakenly treated as DSCP when doing route lookups.

Series applied, thanks