From 1daf6f4615226a2cdd9523a80d70736af4a9f3c0 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Thu, 29 Aug 2024 19:58:45 +1000 Subject: conf, fwd: Make ephemeral port logic more flexible "Ephemeral" ports are those which the kernel may allocate as local port numbers for outgoing connections or datagrams. Because of that, they're generally not good choices for listening servers to bind to. Thefore when using -t all, -u all or exclude-only ranges, we map only non-ephemeral ports. Our logic for this is a bit rigid though: we assume the ephemeral ports are always a fixed range at the top of the port number space. We also assume PORT_EPHEMERAL_MIN is a multiple of 8, or we won't set the forward bitmap correctly. Make the logic in conf.c more flexible, using a helper moved into fwd.[ch], although we don't change which ports we consider ephemeral (yet). The new handling is undoubtedly more computationally expensive, but since it's a once-off operation at start off, I don't think it really matters. Signed-off-by: David Gibson Reviewed-by: Laurent Vivier Signed-off-by: Stefano Brivio --- fwd.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'fwd.c') diff --git a/fwd.c b/fwd.c index 2a0452f..8fa312a 100644 --- a/fwd.c +++ b/fwd.c @@ -27,6 +27,23 @@ #include "lineread.h" #include "flow_table.h" +/* Empheral port range: values from RFC 6335 */ +static const in_port_t fwd_ephemeral_min = (1 << 15) + (1 << 14); +static const in_port_t fwd_ephemeral_max = NUM_PORTS - 1; + +/** + * fwd_port_is_ephemeral() - Is port number ephemeral? + * @port: Port number + * + * Return: true if @port is ephemeral, that is may be allocated by the kernel as + * a local port for outgoing connections or datagrams, but should not be + * used for binding services to. + */ +bool fwd_port_is_ephemeral(in_port_t port) +{ + return (port >= fwd_ephemeral_min) && (port <= fwd_ephemeral_max); +} + /* See enum in kernel's include/net/tcp_states.h */ #define UDP_LISTEN 0x07 #define TCP_LISTEN 0x0a -- cgit v1.2.3