fib_rules: Fix iif / oif matching on L3 master device
Subsystems (5)
Ido Schimmel
maintainercover fib_rules: Fix iif / oif matching on L3 master device
Patch #1 fixes a recently reported regression regarding FIB rules that match on iif / oif being a VRF device.
Patch #2 adds test cases to the FIB rules selftest.
Ido Schimmel (2):
net: fib_rules: Fix iif / oif matching on L3 master device
selftests: fib_rule_tests: Add VRF match tests
include/net/fib_rules.h | 2 + include/net/flow.h | 1 + include/net/l3mdev.h | 27 +++++++++++ net/core/fib_rules.c | 48 +++++++++++++++---- net/l3mdev/l3mdev.c | 4 +- tools/testing/selftests/net/fib_rule_tests.sh | 34 +++++++++++++ 6 files changed, 107 insertions(+), 9 deletions(-)
Signature · 1 line
2.49.0
Ido Schimmel
maintainerPATCH 1/2 net: fib_rules: Fix iif / oif matching on L3 master device
Before commit 40867d74c374 ("net: Add l3mdev index to flow struct and avoid oif reset for port devices") it was possible to use FIB rules to match on a L3 domain. This was done by having a FIB rule match on iif / oif being a L3 master device. It worked because prior to the FIB rule lookup the iif / oif fields in the flow structure were reset to the index of the L3 master device to which the input / output device was enslaved to.
The above scheme made it impossible to match on the original input / output device. Therefore, cited commit stopped overwriting the iif / oif fields in the flow structure and instead stored the index of the enslaving L3 master device in a new field ('flowi_l3mdev') in the flow structure.
While the change enabled new use cases, it broke the original use case of matching on a L3 domain. Fix this by interpreting the iif / oif matching on a L3 master device as a match against the L3 domain. In other words, if the iif / oif in the FIB rule points to a L3 master device, compare the provided index against 'flowi_l3mdev' rather than 'flowi_{i,o}if'.
Before cited commit, a FIB rule that matched on 'iif vrf1' would only match incoming traffic from devices enslaved to 'vrf1'. With the proposed change (i.e., comparing against 'flowi_l3mdev'), the rule would also match traffic originating from a socket bound to 'vrf1'. Avoid that by adding a new flow flag ('FLOWI_FLAG_L3MDEV_OIF') that indicates if the L3 domain was derived from the output interface or the input interface (when not set) and take this flag into account when evaluating the FIB rule against the flow structure.
Avoid unnecessary checks in the data path by detecting that a rule matches on a L3 master device when the rule is installed and marking it as such.
Tested using the following script [1].
Output before 40867d74c374 (v5.4.291):
default dev dummy1 table 100 scope link
default dev dummy1 table 200 scope link
Output after 40867d74c374:
default dev dummy1 table 300 scope link
default dev dummy1 table 300 scope link
Output with this patch:
default dev dummy1 table 100 scope link
default dev dummy1 table 200 scope link
[1]
#!/bin/bash
ip link add name vrf1 up type vrf table 10 ip link add name dummy1 up master vrf1 type dummy
sysctl -wq net.ipv4.conf.all.forwarding=1
sysctl -wq net.ipv4.conf.all.rp_filter=0
ip route add table 100 default dev dummy1
ip route add table 200 default dev dummy1
ip route add table 300 default dev dummy1
ip rule add prio 0 oif vrf1 table 100
ip rule add prio 1 iif vrf1 table 200
ip rule add prio 2 table 300
ip route get 192.0.2.1 oif dummy1 fibmatch
ip route get 192.0.2.1 iif dummy1 from 198.51.100.1 fibmatch
--- include/net/fib_rules.h | 2 ++ include/net/flow.h | 1 + include/net/l3mdev.h | 27 +++++++++++++++++++++++ net/core/fib_rules.c | 48 ++++++++++++++++++++++++++++++++++------- net/l3mdev/l3mdev.c | 4 +++- 5 files changed, 73 insertions(+), 9 deletions(-)
include/net/fib_rules.h +2 -0
@@ -45,6 +45,8 @@ struct fib_rule {struct fib_rule_port_range dport_range;u16 sport_mask;u16 dport_mask; +u8 iif_is_l3_master; +u8 oif_is_l3_master;struct rcu_head rcu; };
include/net/flow.h +1 -0
@@ -38,6 +38,7 @@ struct flowi_common {__u8 flowic_flags;#define FLOWI_FLAG_ANYSRC 0x01#define FLOWI_FLAG_KNOWN_NH 0x02 +#define FLOWI_FLAG_L3MDEV_OIF 0x04__u32 flowic_secid;kuid_t flowic_uid;__u32 flowic_multipath_hash;
include/net/l3mdev.h +27 -0
@@ -59,6 +59,20 @@ int l3mdev_ifindex_lookup_by_table_id(enum l3mdev_type l3type, struct net *net,int l3mdev_fib_rule_match (struct net *net,struct flowi *fl,struct fib_lookup_arg *arg); +static inline +bool l3mdev_fib_rule_iif_match (const struct flowi *fl,int iifindex) +{ +return !(fl->flowi_flags &FLOWI_FLAG_L3MDEV_OIF ) && + fl->flowi_l3mdev == iifindex; +} + +static inline +bool l3mdev_fib_rule_oif_match (const struct flowi *fl,int oifindex) +{ +return fl->flowi_flags &FLOWI_FLAG_L3MDEV_OIF && + fl->flowi_l3mdev == oifindex; +} +void l3mdev_update_flow (struct net *net,struct flowi *fl);int l3mdev_master_ifindex_rcu (const struct net_device *dev);
@@ -327,6 +341,19 @@ int l3mdev_fib_rule_match(struct net *net, struct flowi *fl, {return 1 ; } + +static inline +bool l3mdev_fib_rule_iif_match (const struct flowi *fl,int iifindex) +{ +return false; +} + +static inline +bool l3mdev_fib_rule_oif_match (const struct flowi *fl,int oifindex) +{ +return false; +} +static inline void l3mdev_update_flow (struct net *net,struct flowi *fl) {
net/core/fib_rules.c +40 -8
94 lines not on this page · raw message
net/l3mdev/l3mdev.c +3 -1
@@ -277,8 +277,10 @@ void l3mdev_update_flow(struct net *net, struct flowi *fl)if (fl->flowi_oif) { dev =dev_get_by_index_rcu (net, fl->flowi_oif);if (dev) { -if (!fl->flowi_l3mdev) +if (!fl->flowi_l3mdev) { fl->flowi_l3mdev =l3mdev_master_ifindex_rcu (dev); + fl->flowi_flags |=FLOWI_FLAG_L3MDEV_OIF ; + }/* oif set to L3mdev directs lookup to its table; * reset to avoid oif match in fib_lookup
Signature · 1 line
2.49.0
Re: [PATCH net 1/2] net: fib_rules: Fix iif / oif matching on L3 master device
Quoted 75 lines · Ido Schimmel
Before commit
40867d74c374("net: Add l3mdev index to flow struct and avoid oif reset for port devices") it was possible to use FIB rules to match on a L3 domain. This was done by having a FIB rule match on iif / oif being a L3 master device. It worked because prior to the FIB rule lookup the iif / oif fields in the flow structure were reset to the index of the L3 master device to which the input / output device was enslaved to.The above scheme made it impossible to match on the original input / output device. Therefore, cited commit stopped overwriting the iif / oif fields in the flow structure and instead stored the index of the enslaving L3 master device in a new field ('
flowi_l3mdev') in the flow structure.While the change enabled new use cases, it broke the original use case of matching on a L3 domain. Fix this by interpreting the iif / oif matching on a L3 master device as a match against the L3 domain. In other words, if the iif / oif in the FIB rule points to a L3 master device, compare the provided index against '
flowi_l3mdev' rather than 'flowi_{i,o}if'.Before cited commit, a FIB rule that matched on 'iif vrf1' would only match incoming traffic from devices enslaved to 'vrf1'. With the proposed change (i.e., comparing against '
flowi_l3mdev'), the rule would also match traffic originating from a socket bound to 'vrf1'. Avoid that by adding a new flow flag ('FLOWI_FLAG_L3MDEV_OIF') that indicates if the L3 domain was derived from the output interface or the input interface (when not set) and take this flag into account when evaluating the FIB rule against the flow structure.Avoid unnecessary checks in the data path by detecting that a rule matches on a L3 master device when the rule is installed and marking it as such.
Tested using the following script [1].
Output before
40867d74c374(v5.4.291):default dev dummy1 table 100 scope link
default dev dummy1 table 200 scope linkOutput after
40867d74c374:default dev dummy1 table 300 scope link
default dev dummy1 table 300 scope linkOutput with this patch:
default dev dummy1 table 100 scope link
default dev dummy1 table 200 scope link[1]
#!/bin/baship link add name vrf1 up type vrf table 10 ip link add name dummy1 up master vrf1 type dummy
sysctl -wq net.ipv4.conf.all.forwarding=1
sysctl -wq net.ipv4.conf.all.rp_filter=0ip route add table 100 default dev dummy1
ip route add table 200 default dev dummy1
ip route add table 300 default dev dummy1ip rule add prio 0 oif vrf1 table 100
ip rule add prio 1 iif vrf1 table 200
ip rule add prio 2 table 300ip route get 192.0.2.1 oif dummy1 fibmatch
ip route get 192.0.2.1 iif dummy1 from 198.51.100.1 fibmatchFixes 40867d74c374 ("net: Add l3mdev index to flow struct and avoid oif reset for port devices")Reported-by hanhuihui <[email protected]>Signed-off-by Ido Schimmel <[email protected]>
Quoted 8 lines
--- include/net/fib_rules.h | 2 ++ include/net/flow.h | 1 + include/net/l3mdev.h | 27 +++++++++++++++++++++++ net/core/fib_rules.c | 48 ++++++++++++++++++++++++++++++++++------- net/l3mdev/l3mdev.c | 4 +++- 5 files changed, 73 insertions(+), 9 deletions(-)
Ido Schimmel
maintainerPATCH 2/2 selftests: fib_rule_tests: Add VRF match tests
In reply to Ido SchimmelAdd tests for FIB rules that match on iif / oif being a VRF device. Test both good and bad flows.
With previous patch ("net: fib_rules: Fix iif / oif matching on L3 master device"):
# ./fib_rule_tests.sh [...] Tests passed: 328 Tests failed: 0
Without it:
# ./fib_rule_tests.sh [...] Tests passed: 324 Tests failed: 4
--- tools/testing/selftests/net/fib_rule_tests.sh | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+)
tools/testing/selftests/net/fib_rule_tests.sh +34 -0
@@ -359,6 +359,23 @@ fib_rule6_test() "$getnomatch" "iif flowlabel masked redirect to table" \ "iif flowlabel masked no redirect to table" fi + + $IP link show dev $DEV | grep -q vrf0 + if [ $? -eq 0 ]; then + match="oif vrf0" + getmatch="oif $DEV" + getnomatch="oif lo" + fib_rule6_test_match_n_redirect "$match" "$getmatch" \ + "$getnomatch" "VRF oif redirect to table" \ + "VRF oif no redirect to table" + + match="from $SRC_IP6 iif vrf0" + getmatch="from $SRC_IP6 iif $DEV" + getnomatch="from $SRC_IP6 iif lo" + fib_rule6_test_match_n_redirect "$match" "$getmatch" \ + "$getnomatch" "VRF iif redirect to table" \ + "VRF iif no redirect to table" + fi } fib_rule6_vrf_test()
@@ -635,6 +652,23 @@ fib_rule4_test() "$getnomatch" "iif dscp masked redirect to table" \ "iif dscp masked no redirect to table" fi + + $IP link show dev $DEV | grep -q vrf0 + if [ $? -eq 0 ]; then + match="oif vrf0" + getmatch="oif $DEV" + getnomatch="oif lo" + fib_rule4_test_match_n_redirect "$match" "$getmatch" \ + "$getnomatch" "VRF oif redirect to table" \ + "VRF oif no redirect to table" + + match="from $SRC_IP iif vrf0" + getmatch="from $SRC_IP iif $DEV" + getnomatch="from $SRC_IP iif lo" + fib_rule4_test_match_n_redirect "$match" "$getmatch" \ + "$getnomatch" "VRF iif redirect to table" \ + "VRF iif no redirect to table" + fi } fib_rule4_vrf_test()
Signature · 1 line
2.49.0
Re: [PATCH net 2/2] selftests: fib_rule_tests: Add VRF match tests
Quoted 18 lines · Ido Schimmel
Add tests for FIB rules that match on iif / oif being a VRF device. Test both good and bad flows.
With previous patch ("net:
fib_rules: Fix iif / oif matching on L3 master device"):# ./fib_rule_tests.sh [...] Tests passed: 328 Tests failed: 0Without it:
# ./fib_rule_tests.sh [...] Tests passed: 324 Tests failed: 4
Quoted 5 lines
Signed-off-by Ido Schimmel <[email protected]>--- tools/testing/selftests/net/fib_rule_tests.sh | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+)
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Mon, 14 Apr 2025 20:20:20 +0300 you wrote:
Patch #1 fixes a recently reported regression regarding FIB rules that match on iif / oif being a VRF device.
Patch #2 adds test cases to the FIB rules selftest.
Ido Schimmel (2):
net:fib_rules: Fix iif / oif matching on L3 master device
selftests:fib_rule_tests: Add VRF match tests[...]
Here is the summary with links:
- [net,1/2] net: fib_rules: Fix iif / oif matching on L3 master device
https://git.kernel.org/netdev/net/c/2d300ce0b783 - [net,2/2] selftests: fib_rule_tests: Add VRF match tests https://git.kernel.org/netdev/net/c/f9c87590ed6a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html