Skip to content

Add IP_LOCAL_PORT_RANGE socket option

Jakub Sitnicki netdev selinux 6 versions – Applied

Status from this message: This series was applied to netdev/net-next.git (master)

Get series with b4
b4 am [email protected] change-id 20221221-sockopt-port-range-e142de700f4d

Versions newest first

  1. v6 Latest2 patches 2 repliesApplied 2/2 inferred link
  2. v52 patches 4 repliesSuperseded inferred link
  3. v42 patches 6 repliesSuperseded inferred link
Earlier versions · 3
VersionPostedPatchesRepliesStatus
v32 patches2 repliesSuperseded
v22 patches6 repliesSuperseded
v12 patches3 repliesSuperseded

Changes in v6 from the cover letter

v5: https://lore.kernel.org/r/[email protected]

* Move changelog in individual patches below the trailer. (Leon)

Patches in v6 All 2 applied

#Subject and filesSizeRepliesApplied
1/2inet: Add IP_LOCAL_PORT_RANGE socket optioninclude/net/inet_sock.h include/net/ip.h include/uapi/linux/in.h +5 more+51 -61 replyApplied
2/2selftests/net: Cover the IP_LOCAL_PORT_RANGE socket optiontools/testing/selftests/net/Makefile tools/testing/selftests/net/ip_local_port_range.c tools/testing/selftests/net/ip_local_port_range.sh+454 -00 repliesApplied

Reviews trailers per patch

All 2 patches have a Reviewed-by or Acked-by.

PatchMarek MajkowskiKuniyuki IwashimaEric Dumazet
1/2R-b R-b R-b
2/2R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.

Read the v6 thread

Cover letter [PATCH net-next v6 0/2] Add IP_LOCAL_PORT_RANGE socket option

This patch set is a follow up to the "How to share IPv4 addresses by partitioning the port space" talk given at LPC 2022 [1].

Please see patch #1 for the motivation & the use case description. Patch #2 adds tests exercising the new option in various scenarios.

Documentation
-------------

Proposed update to the ip(7) man-page:

IP_LOCAL_PORT_RANGE (since Linux X.Y)
Set or get the per-socket default local port range. This option can be used to clamp down the global local port range, defined by the ip_local_port_range /proc interface described below, for a given socket.

The option takes an uint32_t value with the high 16 bits set to the upper range bound, and the low 16 bits set to the lower range bound. Range bounds are inclusive. The 16-bit values should be in host byte order.

The lower bound has to be less than the upper bound when both bounds are not zero. Otherwise, setting the option fails with EINVAL.

If either bound is outside of the global local port range, or is zero, then that bound has no effect.

To reset the setting, pass zero as both the upper and the lower bound.

Interaction with SELinux bind() hook
------------------------------------

SELinux bind() hook - selinux_socket_bind() - performs a permission check if the requested local port number lies outside of the netns ephemeral port range.

The proposed socket option cannot be used change the ephemeral port range to extend beyond the per-netns port range, as set by net.ipv4.ip_local_port_range.

Hence, there is no interaction with SELinux, AFAICT.

Changelog:
---------

v5 -> v6:
v5: https://lore.kernel.org/r/[email protected]

* Move changelog in individual patches below the trailer. (Leon)

v4 -> v5:
v4: https://lore.kernel.org/r/[email protected]

* Code changes called out in individual patches.

v3 -> v4:
v3: https://lore.kernel.org/r/[email protected]

* Highlight that port bounds should be in host byte order. (Neal)

v2 -> v3:
v2: https://lore.kernel.org/r/[email protected]

* Describe interaction considerations with SELinux.
* Code changes called out in individual patches.

v1 -> v2:
v1: https://lore.kernel.org/netdev/[email protected]/

* Fix the corner case when the per-socket range doesn't overlap with the per-netns range. Fallback correctly to the per-netns range. (Kuniyuki)

* selftests: Instead of iterating over socket families (ip4, ip6) and types (tcp, udp), generate tests for each combo from a template. This keeps the code indentation level down and makes tests more granular.

* Rewrite man-page prose:
- explain how to unset the option,
- document when EINVAL is returned.

RFC -> v1
RFC: https://lore.kernel.org/netdev/[email protected]/

* Allow either the high bound or the low bound, or both, to be zero
* Add getsockopt support
* Add selftests

Links:
------

[1]: https://lpc.events/event/16/contributions/1349/

To: [email protected]

Cc "David S. Miller" <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Paolo Abeni <[email protected]>
Cc Kuniyuki Iwashima <[email protected]>
Cc Neal Cardwell <[email protected]>
Cc Leon Romanovsky <[email protected]>
Cc Paul Moore <[email protected]>
Cc Stephen Smalley <[email protected]>
Cc Eric Paris <[email protected]>

---
Jakub Sitnicki (2):
inet: Add IP_LOCAL_PORT_RANGE socket option selftests/net: Cover the IP_LOCAL_PORT_RANGE socket option

 include/net/inet_sock.h                            |   4 +
 include/net/ip.h                                   |   3 +-
 include/uapi/linux/in.h                            |   1 +
 net/ipv4/inet_connection_sock.c                    |  25 +-
 net/ipv4/inet_hashtables.c                         |   2 +-
 net/ipv4/ip_sockglue.c                             |  18 +
 net/ipv4/udp.c                                     |   2 +-
 net/sctp/socket.c                                  |   2 +-
 tools/testing/selftests/net/Makefile               |   2 +
 tools/testing/selftests/net/ip_local_port_range.c  | 447 +++++++++++++++++++++
 tools/testing/selftests/net/ip_local_port_range.sh |   5 +
 11 files changed, 505 insertions(+), 6 deletions(-)