linux/include
Ido Schimmel 1fa3314c14 ipv4: Centralize TOS matching
The TOS field in the IPv4 flow information structure ('flowi4_tos') is
matched by the kernel against the TOS selector in IPv4 rules and routes.
The field is initialized differently by different call sites. Some treat
it as DSCP (RFC 2474) and initialize all six DSCP bits, some treat it as
RFC 1349 TOS and initialize it using RT_TOS() and some treat it as RFC
791 TOS and initialize it using IPTOS_RT_MASK.

What is common to all these call sites is that they all initialize the
lower three DSCP bits, which fits the TOS definition in the initial IPv4
specification (RFC 791).

Therefore, the kernel only allows configuring IPv4 FIB rules that match
on the lower three DSCP bits which are always guaranteed to be
initialized by all call sites:

 # ip -4 rule add tos 0x1c table 100
 # ip -4 rule add tos 0x3c table 100
 Error: Invalid tos.

While this works, it is unlikely to be very useful. RFC 791 that
initially defined the TOS and IP precedence fields was updated by RFC
2474 over twenty five years ago where these fields were replaced by a
single six bits DSCP field.

Extending FIB rules to match on DSCP can be done by adding a new DSCP
selector while maintaining the existing semantics of the TOS selector
for applications that rely on that.

A prerequisite for allowing FIB rules to match on DSCP is to adjust all
the call sites to initialize the high order DSCP bits and remove their
masking along the path to the core where the field is matched on.

However, making this change alone will result in a behavior change. For
example, a forwarded IPv4 packet with a DS field of 0xfc will no longer
match a FIB rule that was configured with 'tos 0x1c'.

This behavior change can be avoided by masking the upper three DSCP bits
in 'flowi4_tos' before comparing it against the TOS selectors in FIB
rules and routes.

Implement the above by adding a new function that checks whether a given
DSCP value matches the one specified in the IPv4 flow information
structure and invoke it from the three places that currently match on
'flowi4_tos'.

Use RT_TOS() for the masking of 'flowi4_tos' instead of IPTOS_RT_MASK
since the latter is not uAPI and we should be able to remove it at some
point.

Include <linux/ip.h> in <linux/in_route.h> since the former defines
IPTOS_TOS_MASK which is used in the definition of RT_TOS() in
<linux/in_route.h>.

No regressions in FIB tests:

 # ./fib_tests.sh
 [...]
 Tests passed: 218
 Tests failed:   0

And FIB rule tests:

 # ./fib_rule_tests.sh
 [...]
 Tests passed: 116
 Tests failed:   0

Signed-off-by: Ido Schimmel <idosch@nvidia.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
2024-08-20 14:57:08 +02:00
..
acpi Driver core changes for 6.11-rc1 2024-07-25 10:42:22 -07:00
asm-generic runtime constants: deal with old decrepit linkers 2024-08-03 08:38:45 -07:00
clocksource
crypto
drm drm/buddy: Add start address support to trim function 2024-08-07 18:19:00 -04:00
dt-bindings I3C for 6.11 2024-07-27 10:53:06 -07:00
keys
kunit
kvm
linux Merge branch '40GbE' of git://git.kernel.org/pub/scm/linux/kernel/git/tnguy/next-queue 2024-08-16 11:28:51 -07:00
math-emu
media
memory
misc
net ipv4: Centralize TOS matching 2024-08-20 14:57:08 +02:00
pcmcia
ras
rdma
rv
scsi Driver core changes for 6.11-rc1 2024-07-25 10:42:22 -07:00
soc sound updates for 6.11-rc1 2024-07-19 12:39:34 -07:00
sound ASoC: Fixes for v6.11 2024-08-09 09:58:07 +02:00
target
trace vfs-6.11-rc4.fixes 2024-08-14 09:06:28 -07:00
uapi ipv4: Centralize TOS matching 2024-08-20 14:57:08 +02:00
ufs scsi: ufs: core: Check LSDBS cap when !mcq 2024-07-15 22:51:06 -04:00
vdso Random number generator updates for Linux 6.11-rc1. 2024-07-24 10:29:50 -07:00
video
xen Driver core changes for 6.11-rc1 2024-07-25 10:42:22 -07:00