Commit 79a4e215 authored by Ido Schimmel's avatar Ido Schimmel Committed by Jakub Kicinski
Browse files

ipv4: fib_rules: Add port mask matching



Extend IPv4 FIB rules to match on source and destination ports using a
mask. Note that the mask is only set when not matching on a range.

Reviewed-by: default avatarPetr Machata <petrm@nvidia.com>
Signed-off-by: default avatarIdo Schimmel <idosch@nvidia.com>
Reviewed-by: default avatarGuillaume Nault <gnault@redhat.com>
Reviewed-by: default avatarDavid Ahern <dsahern@kernel.org>
Link: https://patch.msgid.link/20250217134109.311176-4-idosch@nvidia.com


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent da766594
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -148,6 +148,17 @@ static inline bool fib_rule_port_inrange(const struct fib_rule_port_range *a,
		ntohs(port) <= a->end;
}

static inline bool fib_rule_port_match(const struct fib_rule_port_range *range,
				       u16 port_mask, __be16 port)
{
	if ((range->start ^ ntohs(port)) & port_mask)
		return false;
	if (!port_mask && fib_rule_port_range_set(range) &&
	    !fib_rule_port_inrange(range, port))
		return false;
	return true;
}

static inline bool fib_rule_port_range_valid(const struct fib_rule_port_range *a)
{
	return a->start != 0 && a->end != 0 && a->end < 0xffff &&
+4 −4
Original line number Diff line number Diff line
@@ -201,12 +201,12 @@ INDIRECT_CALLABLE_SCOPE int fib4_rule_match(struct fib_rule *rule,
	if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto))
		return 0;

	if (fib_rule_port_range_set(&rule->sport_range) &&
	    !fib_rule_port_inrange(&rule->sport_range, fl4->fl4_sport))
	if (!fib_rule_port_match(&rule->sport_range, rule->sport_mask,
				 fl4->fl4_sport))
		return 0;

	if (fib_rule_port_range_set(&rule->dport_range) &&
	    !fib_rule_port_inrange(&rule->dport_range, fl4->fl4_dport))
	if (!fib_rule_port_match(&rule->dport_range, rule->dport_mask,
				 fl4->fl4_dport))
		return 0;

	return 1;