net: Add sockaddr_inet unified address structure
cover net: Add sockaddr_inet unified address structure
Hi!
Repeating patch 1, as it has the rationale:
There are cases in networking (e.g. wireguard, sctp) where a union is used to provide coverage for either IPv4 or IPv6 network addresses, and they include an embedded "struct sockaddr" as well (for "sa_family" and raw "sa_data" access). The current struct sockaddr contains a flexible array, which means these unions should not be further embedded in other structs because they do not technically have a fixed size (and are generating warnings for the coming -Wflexible-array-not-at-end flag addition). But the future changes to make struct sockaddr a fixed size (i.e. with a 14 byte sa_data member) make the "sa_data" uses with an IPv6 address a potential place for the compiler to get upset about object size mismatches. Therefore, we need a sockaddr that cleanly provides both an sa_family member and an appropriately fixed-sized sa_data member that does not bloat member usage via the potential alternative of sockaddr_storage to cover both IPv4 and IPv6, to avoid unseemly churn in the affected code bases.
Introduce sockaddr_inet as a unified structure for holding both IPv4 and IPv6 addresses (i.e. large enough to accommodate sockaddr_in6).
The structure is defined in linux/in6.h since its max size is sized based on sockaddr_in6 and provides a more specific alternative to the generic sockaddr_storage for IPv4 with IPv6 address family handling.
The "sa_family" member doesn't use the sa_family_t type to avoid needing layer violating header inclusions.
Also includes the replacements for wireguard and sctp.
Thanks,
-Kees
Kees Cook (3):
ipv6: Add sockaddr_inet unified address structure
wireguard: peer: Replace sockaddr with sockaddr_inet
sctp: Replace sockaddr with sockaddr_inet in sctp_addr union
drivers/net/wireguard/peer.h | 2 +- include/linux/in6.h | 7 +++++++ include/net/sctp/structs.h | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 ipv6: Add sockaddr_inet unified address structure
There are cases in networking (e.g. wireguard, sctp) where a union is used to provide coverage for either IPv4 or IPv6 network addresses, and they include an embedded "struct sockaddr" as well (for "sa_family" and raw "sa_data" access). The current struct sockaddr contains a flexible array, which means these unions should not be further embedded in other structs because they do not technically have a fixed size (and are generating warnings for the coming -Wflexible-array-not-at-end flag addition). But the future changes to make struct sockaddr a fixed size (i.e. with a 14 byte sa_data member) make the "sa_data" uses with an IPv6 address a potential place for the compiler to get upset about object size mismatches. Therefore, we need a sockaddr that cleanly provides both an sa_family member and an appropriately fixed-sized sa_data member that does not bloat member usage via the potential alternative of sockaddr_storage to cover both IPv4 and IPv6, to avoid unseemly churn in the affected code bases.
Introduce sockaddr_inet as a unified structure for holding both IPv4 and IPv6 addresses (i.e. large enough to accommodate sockaddr_in6).
The structure is defined in linux/in6.h since its max size is sized based on sockaddr_in6 and provides a more specific alternative to the generic sockaddr_storage for IPv4 with IPv6 address family handling.
The "sa_family" member doesn't use the sa_family_t type to avoid needing layer violating header inclusions.
--- include/linux/in6.h | 7 +++++++ 1 file changed, 7 insertions(+)
include/linux/in6.h +7 -0
@@ -18,6 +18,13 @@#include <uapi/linux/in6.h> +/* Large enough to hold both sockaddr_in and sockaddr_in6. */ +struct sockaddr_inet { +unsigned short sa_family; +char sa_data[sizeof (struct sockaddr_in6 ) - +sizeof (unsigned short )]; +}; +/* IPv6 Wildcard Address (::) and Loopback Address (::1) defined in RFC2553 * NOTE: Be aware the IN6ADDR_* constants and in6addr_* externals are defined * in network byte order, not in host byte order as are the IPv4 equivalents
Signature · 1 line
2.34.1
PATCH 2/3 wireguard: peer: Replace sockaddr with sockaddr_inet
In reply to Kees CookAs part of the removal of the variably-sized sockaddr for kernel internals, replace struct sockaddr with sockaddr_inet in the endpoint union.
No binary changes; the union size remains unchanged due to sockaddr_inet matching the size of sockaddr_in6.
---
Cc: "Jason A. Donenfeld" <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
--- drivers/net/wireguard/peer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireguard/peer.h +1 -1
@@ -20,7 +20,7 @@ struct wg_device;struct endpoint {union { -struct sockaddr addr; +struct sockaddr_inet addr;/* Large enough for both address families */ struct sockaddr_in addr4;struct sockaddr_in6 addr6; };
Signature · 1 line
2.34.1
PATCH 3/3 sctp: Replace sockaddr with sockaddr_inet in sctp_addr union
In reply to Kees CookAs part of the removal of the variably-sized sockaddr for kernel internals, replace struct sockaddr with sockaddr_inet in the sctp_addr union.
No binary changes; the union size remains unchanged due to sockaddr_inet matching the size of sockaddr_in6.
---
Cc: Marcelo Ricardo Leitner <[email protected]>
Cc: Xin Long <[email protected]>
Cc: "David S. Miller" <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: Jakub Kicinski <[email protected]>
Cc: Paolo Abeni <[email protected]>
Cc: Simon Horman <[email protected]>
Cc: <[email protected]>
Cc: <[email protected]>
--- include/net/sctp/structs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/sctp/structs.h +1 -1
@@ -51,9 +51,9 @@* We should wean ourselves off this. */ union sctp_addr { +struct sockaddr_inet sa;/* Large enough for both address families */ struct sockaddr_in v4;struct sockaddr_in6 v6; -struct sockaddr sa; };/* Forward declarations for data structures. */
Signature · 1 line
2.34.1
Simon Horman
reviewer+ Iwashima-san and Willem
This series looks like something you should review
Quoted 49 lines · Kees Cook
Hi!
Repeating patch 1, as it has the rationale:
There are cases in networking (e.g. wireguard, sctp) where a union is used to provide coverage for either IPv4 or IPv6 network addresses, and they include an embedded "struct sockaddr" as well (for "
sa_family" and raw "sa_data" access). The current struct sockaddr contains a flexible array, which means these unions should not be further embedded in other structs because they do not technically have a fixed size (and are generating warnings for the coming -Wflexible-array-not-at-end flag addition). But the future changes to make struct sockaddr a fixed size (i.e. with a 14 bytesa_datamember) make the "sa_data" uses with an IPv6 address a potential place for the compiler to get upset about object size mismatches. Therefore, we need a sockaddr that cleanly provides both ansa_familymember and an appropriately fixed-sizedsa_datamember that does not bloat member usage via the potential alternative ofsockaddr_storageto cover both IPv4 and IPv6, to avoid unseemly churn in the affected code bases.Introduce
sockaddr_inetas a unified structure for holding both IPv4 and IPv6 addresses (i.e. large enough to accommodatesockaddr_in6).The structure is defined in
linux/in6.hsince its max size is sized based onsockaddr_in6and provides a more specific alternative to the genericsockaddr_storagefor IPv4 with IPv6 address family handling.The "
sa_family" member doesn't use thesa_family_ttype to avoid needing layer violating header inclusions.Also includes the replacements for wireguard and sctp.
Thanks,
-Kees
Kees Cook (3):
ipv6: Addsockaddr_inetunified address structure
wireguard: peer: Replace sockaddr withsockaddr_inet
sctp: Replace sockaddr withsockaddr_inetinsctp_addruniondrivers/net/wireguard/peer.h | 2 +- include/linux/in6.h | 7 +++++++ include/net/sctp/structs.h | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-)Signature · 1 line
2.34.1
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 20 lines · On Tue, 22 Jul 2025 10:18:30 -0700 you wrote:
Hi!
Repeating patch 1, as it has the rationale:
There are cases in networking (e.g. wireguard, sctp) where a union is used to provide coverage for either IPv4 or IPv6 network addresses, and they include an embedded "struct sockaddr" as well (for "
sa_family" and raw "sa_data" access). The current struct sockaddr contains a flexible array, which means these unions should not be further embedded in other structs because they do not technically have a fixed size (and are generating warnings for the coming -Wflexible-array-not-at-end flag addition). But the future changes to make struct sockaddr a fixed size (i.e. with a 14 bytesa_datamember) make the "sa_data" uses with an IPv6 address a potential place for the compiler to get upset about object size mismatches. Therefore, we need a sockaddr that cleanly provides both ansa_familymember and an appropriately fixed-sizedsa_datamember that does not bloat member usage via the potential alternative ofsockaddr_storageto cover both IPv4 and IPv6, to avoid unseemly churn in the affected code bases.
Quoted 1 line
[...]
Here is the summary with links:
- [net-next,1/3] ipv6: Add sockaddr_inet unified address structure
https://git.kernel.org/netdev/net-next/c/463deed51796 - [net-next,2/3] wireguard: peer: Replace sockaddr with sockaddr_inet https://git.kernel.org/netdev/net-next/c/9203e0a82c0b - [net-next,3/3] sctp: Replace sockaddr with sockaddr_inet in sctp_addr union https://git.kernel.org/netdev/net-next/c/511d10b4c2f9
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html