Skip to contentSkip to conversation

inet: better const qualifier awareness

PATCH Applied Eric Dumazet 3 participants 11 messages Mar 16, 2023 – Mar 17, 2023

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v2 cover inet: better const qualifier awareness

inet_sk() can be changed to propagate const qualifier, thanks to container_of_const()

Following patches in this series add more const qualifiers.

Other helpers like tcp_sk(), udp_sk(), raw_sk(), ... will be handled in following series.

Eric Dumazet (8):
inet: preserve const qualifier in inet_sk()
ipv4: constify ip_mc_sf_allow() socket argument
udp: constify __udp_is_mcast_sock() socket argument
ipv6: constify inet6_mc_check()
udp6: constify __udp_v6_is_mcast_sock() socket argument
ipv6: raw: constify raw_v6_match() socket argument
ipv4: raw: constify raw_v4_match() socket argument
inet_diag: constify raw_lookup() socket argument

 include/linux/igmp.h        | 2 +-
 include/net/addrconf.h      | 2 +-
 include/net/inet_sock.h     | 5 +----
 include/net/raw.h           | 2 +-
 include/net/rawv6.h         | 2 +-
 include/trace/events/sock.h | 4 ++--
 include/trace/events/tcp.h  | 2 +-
 net/ipv4/igmp.c             | 4 ++--
 net/ipv4/ip_output.c        | 5 +++--
 net/ipv4/raw.c              | 4 ++--
 net/ipv4/raw_diag.c         | 2 +-
 net/ipv4/udp.c              | 4 ++--
 net/ipv6/mcast.c            | 8 ++++----
 net/ipv6/ping.c             | 2 +-
 net/ipv6/raw.c              | 2 +-
 net/ipv6/udp.c              | 6 +++---
 net/mptcp/sockopt.c         | 2 +-
 security/lsm_audit.c        | 4 ++--
 18 files changed, 30 insertions(+), 32 deletions(-)
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
#1 lore raw

PATCH v2 1/8 inet: preserve const qualifier in inet_sk()

We can change inet_sk() to propagate const qualifier of its argument.

This should avoid some potential errors caused by accidental (const -> not_const) promotion.

Other helpers like tcp_sk(), udp_sk(), raw_sk() will be handled in separate patch series.

v2: use container_of_const() as advised by Jakub and Linus

---
 include/net/inet_sock.h     | 5 +----
 include/trace/events/sock.h | 4 ++--
 include/trace/events/tcp.h  | 2 +-
 net/ipv4/ip_output.c        | 5 +++--
 net/ipv6/ping.c             | 2 +-
 net/ipv6/udp.c              | 2 +-
 net/mptcp/sockopt.c         | 2 +-
 security/lsm_audit.c        | 4 ++--
 8 files changed, 12 insertions(+), 14 deletions(-)
include/net/inet_sock.h +1 -4
index 51857117ac0995cee20c1e62752c470d2a473fa8..caa20a9055310f5ef108f9b1bb43214a3d598b9e 100644
@@ -305,10 +305,7 @@ static inline struct sock *skb_to_full_sk(const struct sk_buff *skb)
 	return sk_to_full_sk(skb->sk);
 }
 
-static inline struct inet_sock *inet_sk(const struct sock *sk)
-{
-	return (struct inet_sock *)sk;
-}
+#define inet_sk(ptr) container_of_const(ptr, struct inet_sock, sk)
 
 static inline void __inet_sk_copy_descendant(struct sock *sk_to,
 					     const struct sock *sk_from,
include/trace/events/sock.h +2 -2
index 03d19fc562f872d68cfb2349797f0924cfba718b..fd206a6ab5b85a8eb50fc9303abc28d74c50ea07 100644
@@ -158,7 +158,7 @@ TRACE_EVENT(inet_sock_set_state,
 	),
 
 	TP_fast_assign(
-		struct inet_sock *inet = inet_sk(sk);
+		const struct inet_sock *inet = inet_sk(sk);
 		struct in6_addr *pin6;
 		__be32 *p32;
 
@@ -222,7 +222,7 @@ TRACE_EVENT(inet_sk_error_report,
 	),
 
 	TP_fast_assign(
-		struct inet_sock *inet = inet_sk(sk);
+		const struct inet_sock *inet = inet_sk(sk);
 		struct in6_addr *pin6;
 		__be32 *p32;
 
include/trace/events/tcp.h +1 -1
index 901b440238d5fc203b78cff1081b4b7e1e2eb4bd..bf06db8d2046c4a7f59070b724ce6fc7762f9d4b 100644
@@ -67,7 +67,7 @@ DECLARE_EVENT_CLASS(tcp_event_sk_skb,
 	),
 
 	TP_fast_assign(
-		struct inet_sock *inet = inet_sk(sk);
+		const struct inet_sock *inet = inet_sk(sk);
 		__be32 *p32;
 
 		__entry->skbaddr = skb;
net/ipv4/ip_output.c +3 -2
index e7bef36ce26f5b5e5503eaf14319b2465b779598..cb04dbad9ea474fcaa4b5ba31c4a37299c4e1a8d 100644
@@ -129,7 +129,8 @@ int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
 }
 EXPORT_SYMBOL_GPL(ip_local_out);
 
-static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
+static inline int ip_select_ttl(const struct inet_sock *inet,
+				const struct dst_entry *dst)
 {
 	int ttl = inet->uc_ttl;
 
@@ -146,7 +147,7 @@ int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
 			  __be32 saddr, __be32 daddr, struct ip_options_rcu *opt,
 			  u8 tos)
 {
-	struct inet_sock *inet = inet_sk(sk);
+	const struct inet_sock *inet = inet_sk(sk);
 	struct rtable *rt = skb_rtable(skb);
 	struct net *net = sock_net(sk);
 	struct iphdr *iph;
net/ipv6/ping.c +1 -1
index 808983bc2ec9f92be7d13237fb7d52b6423ccf23..c4835dbdfcff7912465713cdac9914b0f5585972 100644
@@ -237,7 +237,7 @@ static int ping_v6_seq_show(struct seq_file *seq, void *v)
 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
 	} else {
 		int bucket = ((struct ping_iter_state *) seq->private)->bucket;
-		struct inet_sock *inet = inet_sk(v);
+		struct inet_sock *inet = inet_sk((struct sock *)v);
 		__u16 srcp = ntohs(inet->inet_sport);
 		__u16 destp = ntohs(inet->inet_dport);
 		ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
net/ipv6/udp.c +1 -1
index 9fb2f33ee3a76a09bbe15a9aaf1371a804f91ee2..ab4ae886235ac9557219c901c5041adfa8b026ef 100644
@@ -1708,7 +1708,7 @@ int udp6_seq_show(struct seq_file *seq, void *v)
 		seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
 	} else {
 		int bucket = ((struct udp_iter_state *)seq->private)->bucket;
-		struct inet_sock *inet = inet_sk(v);
+		const struct inet_sock *inet = inet_sk((const struct sock *)v);
 		__u16 srcp = ntohs(inet->inet_sport);
 		__u16 destp = ntohs(inet->inet_dport);
 		__ip6_dgram_sock_seq_show(seq, v, srcp, destp,
net/mptcp/sockopt.c +1 -1
index 8a9656248b0f09e78ebf3d6d787449f33505b81f..5cef4d3d21ac824ab6d3a5ee8be3bd00cce63925 100644
@@ -1046,7 +1046,7 @@ static int mptcp_getsockopt_tcpinfo(struct mptcp_sock *msk, char __user *optval,
 
 static void mptcp_get_sub_addrs(const struct sock *sk, struct mptcp_subflow_addrs *a)
 {
-	struct inet_sock *inet = inet_sk(sk);
+	const struct inet_sock *inet = inet_sk(sk);
 
 	memset(a, 0, sizeof(*a));
 
security/lsm_audit.c +2 -2
index a7355b4b9bb86d173f94e43109148bc0ea27d1b3..00d3bdd386e294ecd562bfa8ce502bf179ad32d9 100644
@@ -317,7 +317,7 @@ static void dump_common_audit_data(struct audit_buffer *ab,
 
 			switch (sk->sk_family) {
 			case AF_INET: {
-				struct inet_sock *inet = inet_sk(sk);
+				const struct inet_sock *inet = inet_sk(sk);
 
 				print_ipv4_addr(ab, inet->inet_rcv_saddr,
 						inet->inet_sport,
@@ -329,7 +329,7 @@ static void dump_common_audit_data(struct audit_buffer *ab,
 			}
 #if IS_ENABLED(CONFIG_IPV6)
 			case AF_INET6: {
-				struct inet_sock *inet = inet_sk(sk);
+				const struct inet_sock *inet = inet_sk(sk);
 
 				print_ipv6_addr(ab, &sk->sk_v6_rcv_saddr,
 						inet->inet_sport,
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
#2 lore raw

PATCH v2 2/8 ipv4: constify ip_mc_sf_allow() socket argument

In reply to Eric Dumazet

This clarifies ip_mc_sf_allow() intent.

---
 include/linux/igmp.h | 2 +-
 net/ipv4/igmp.c      | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/igmp.h +1 -1
index b19d3284551f083d0eec3353cd8ec1f486ae4b42..ebf4349a53af7888104e8c5fe0d7af0e5604ae69 100644
@@ -122,7 +122,7 @@ extern int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
 			sockptr_t optval, sockptr_t optlen);
 extern int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
 			sockptr_t optval, size_t offset);
-extern int ip_mc_sf_allow(struct sock *sk, __be32 local, __be32 rmt,
+extern int ip_mc_sf_allow(const struct sock *sk, __be32 local, __be32 rmt,
 			  int dif, int sdif);
 extern void ip_mc_init_dev(struct in_device *);
 extern void ip_mc_destroy_dev(struct in_device *);
net/ipv4/igmp.c +2 -2
index c920aa9a62a988bf91a5420e59eb5878c271bf9a..48ff5f13e7979dc00da60b466ee2e74ddce0891b 100644
@@ -2638,10 +2638,10 @@ int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
 /*
  * check if a multicast source filter allows delivery for a given <src,dst,intf>
  */
-int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
+int ip_mc_sf_allow(const struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
 		   int dif, int sdif)
 {
-	struct inet_sock *inet = inet_sk(sk);
+	const struct inet_sock *inet = inet_sk(sk);
 	struct ip_mc_socklist *pmc;
 	struct ip_sf_socklist *psl;
 	int i;
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
#3 lore raw

PATCH v2 3/8 udp: constify __udp_is_mcast_sock() socket argument

In reply to Eric Dumazet

This clarifies __udp_is_mcast_sock() intent.

---
 net/ipv4/udp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/udp.c +2 -2
index dc8feb54d835f0824aa6833e36db34f686c456ec..aa32afd871ee50968f7bb8152401be60dece1454 100644
@@ -578,12 +578,12 @@ struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
 EXPORT_SYMBOL_GPL(udp4_lib_lookup);
 #endif
 
-static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
+static inline bool __udp_is_mcast_sock(struct net *net, const struct sock *sk,
 				       __be16 loc_port, __be32 loc_addr,
 				       __be16 rmt_port, __be32 rmt_addr,
 				       int dif, int sdif, unsigned short hnum)
 {
-	struct inet_sock *inet = inet_sk(sk);
+	const struct inet_sock *inet = inet_sk(sk);
 
 	if (!net_eq(sock_net(sk), net) ||
 	    udp_sk(sk)->udp_port_hash != hnum ||
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
#4 lore raw

PATCH v2 4/8 ipv6: constify inet6_mc_check()

In reply to Eric Dumazet

inet6_mc_check() is essentially a read-only function.

---
 include/net/addrconf.h | 2 +-
 net/ipv6/mcast.c       | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
include/net/addrconf.h +1 -1
index c04f359655b86feed2b4b42cc69b90c63088238a..82da55101b5a30b2a5512d964429d2c5f73d03fd 100644
@@ -223,7 +223,7 @@ int ipv6_sock_mc_drop(struct sock *sk, int ifindex,
 		      const struct in6_addr *addr);
 void __ipv6_sock_mc_close(struct sock *sk);
 void ipv6_sock_mc_close(struct sock *sk);
-bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
+bool inet6_mc_check(const struct sock *sk, const struct in6_addr *mc_addr,
 		    const struct in6_addr *src_addr);
 
 int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr);
net/ipv6/mcast.c +4 -4
index 1c02160cf7a4c54f0d8687b8368e5f6151ab0bce..714cdc9e2b8edfb925a061a722c38b37b1c6088e 100644
@@ -627,12 +627,12 @@ int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
 	return 0;
 }
 
-bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr,
+bool inet6_mc_check(const struct sock *sk, const struct in6_addr *mc_addr,
 		    const struct in6_addr *src_addr)
 {
-	struct ipv6_pinfo *np = inet6_sk(sk);
-	struct ipv6_mc_socklist *mc;
-	struct ip6_sf_socklist *psl;
+	const struct ipv6_pinfo *np = inet6_sk(sk);
+	const struct ipv6_mc_socklist *mc;
+	const struct ip6_sf_socklist *psl;
 	bool rv = true;
 
 	rcu_read_lock();
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
#5 lore raw

PATCH v2 5/8 udp6: constify __udp_v6_is_mcast_sock() socket argument

In reply to Eric Dumazet

This clarifies __udp_v6_is_mcast_sock() intent.

---
 net/ipv6/udp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv6/udp.c +2 -2
index ab4ae886235ac9557219c901c5041adfa8b026ef..d350e57c479299e732bd3595c1964acddde2d876 100644
@@ -805,12 +805,12 @@ static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
 	return 0;
 }
 
-static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
+static bool __udp_v6_is_mcast_sock(struct net *net, const struct sock *sk,
 				   __be16 loc_port, const struct in6_addr *loc_addr,
 				   __be16 rmt_port, const struct in6_addr *rmt_addr,
 				   int dif, int sdif, unsigned short hnum)
 {
-	struct inet_sock *inet = inet_sk(sk);
+	const struct inet_sock *inet = inet_sk(sk);
 
 	if (!net_eq(sock_net(sk), net))
 		return false;
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
#6 lore raw

PATCH v2 6/8 ipv6: raw: constify raw_v6_match() socket argument

In reply to Eric Dumazet

This clarifies raw_v6_match() intent.

---
 include/net/rawv6.h | 2 +-
 net/ipv6/raw.c      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/net/rawv6.h +1 -1
index bc70909625f60dcd819f50a258841d20e5ba0c68..82810cbe37984437569186783f39f166e89cb9b8 100644
@@ -6,7 +6,7 @@
 #include <net/raw.h>
 
 extern struct raw_hashinfo raw_v6_hashinfo;
-bool raw_v6_match(struct net *net, struct sock *sk, unsigned short num,
+bool raw_v6_match(struct net *net, const struct sock *sk, unsigned short num,
 		  const struct in6_addr *loc_addr,
 		  const struct in6_addr *rmt_addr, int dif, int sdif);
 
net/ipv6/raw.c +1 -1
index bac9ba747bdecf8df24acb6c980a822d8f237403..6ac2f2690c44c96e9ac4cb7368ee7abdbeaf4334 100644
@@ -64,7 +64,7 @@
 struct raw_hashinfo raw_v6_hashinfo;
 EXPORT_SYMBOL_GPL(raw_v6_hashinfo);
 
-bool raw_v6_match(struct net *net, struct sock *sk, unsigned short num,
+bool raw_v6_match(struct net *net, const struct sock *sk, unsigned short num,
 		  const struct in6_addr *loc_addr,
 		  const struct in6_addr *rmt_addr, int dif, int sdif)
 {
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
#7 lore raw

PATCH v2 7/8 ipv4: raw: constify raw_v4_match() socket argument

In reply to Eric Dumazet

This clarifies raw_v4_match() intent.

---
 include/net/raw.h | 2 +-
 net/ipv4/raw.c    | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/net/raw.h +1 -1
index 2c004c20ed996d1dbe07f2c8d25edd2ce03cca03..7ad15830cf38460f1fae3a187986d74faef6dd1d 100644
@@ -22,7 +22,7 @@
 extern struct proto raw_prot;
 
 extern struct raw_hashinfo raw_v4_hashinfo;
-bool raw_v4_match(struct net *net, struct sock *sk, unsigned short num,
+bool raw_v4_match(struct net *net, const struct sock *sk, unsigned short num,
 		  __be32 raddr, __be32 laddr, int dif, int sdif);
 
 int raw_abort(struct sock *sk, int err);
net/ipv4/raw.c +2 -2
index 94df935ee0c5a83a4b1393653b79ac6060b4f12a..3cf68695b40ddc0e79c8fbd62f317048cf5c88e3 100644
@@ -116,10 +116,10 @@ void raw_unhash_sk(struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(raw_unhash_sk);
 
-bool raw_v4_match(struct net *net, struct sock *sk, unsigned short num,
+bool raw_v4_match(struct net *net, const struct sock *sk, unsigned short num,
 		  __be32 raddr, __be32 laddr, int dif, int sdif)
 {
-	struct inet_sock *inet = inet_sk(sk);
+	const struct inet_sock *inet = inet_sk(sk);
 
 	if (net_eq(sock_net(sk), net) && inet->inet_num == num	&&
 	    !(inet->inet_daddr && inet->inet_daddr != raddr) 	&&
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
#8 lore raw

PATCH v2 8/8 inet_diag: constify raw_lookup() socket argument

In reply to Eric Dumazet

Now both raw_v4_match() and raw_v6_match() accept a const socket, raw_lookup() can do the same to clarify its role.

---
 net/ipv4/raw_diag.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/raw_diag.c +1 -1
index 999321834b94a8f7f2a4996575b7cfaafb6fa2b7..bca49a844f01083cdfdade6f52852d48ddb36d70 100644
@@ -34,7 +34,7 @@ raw_get_hashinfo(const struct inet_diag_req_v2 *r)
  * use helper to figure it out.
  */
 
-static bool raw_lookup(struct net *net, struct sock *sk,
+static bool raw_lookup(struct net *net, const struct sock *sk,
 		       const struct inet_diag_req_v2 *req)
 {
 	struct inet_diag_req_raw *r = (void *)req;
Signature · 1 line
2.40.0.rc2.332.ga46443480c-goog
maintainer Reviewed-by on the v2 series
#9 lore raw
In reply to Eric Dumazet
Quoted 29 lines · Eric Dumazet

inet_sk() can be changed to propagate const qualifier, thanks to container_of_const()

Following patches in this series add more const qualifiers.

Other helpers like tcp_sk(), udp_sk(), raw_sk(), ... will be handled in following series.

Eric Dumazet (8):
inet: preserve const qualifier in inet_sk()
ipv4: constify ip_mc_sf_allow() socket argument
udp: constify __udp_is_mcast_sock() socket argument
ipv6: constify inet6_mc_check()
udp6: constify __udp_v6_is_mcast_sock() socket argument
ipv6: raw: constify raw_v6_match() socket argument
ipv4: raw: constify raw_v4_match() socket argument
inet_diag: constify raw_lookup() socket argument

 include/linux/igmp.h        | 2 +-
 include/net/addrconf.h      | 2 +-
 include/net/inet_sock.h     | 5 +----
 include/net/raw.h           | 2 +-
 include/net/rawv6.h         | 2 +-
 include/trace/events/sock.h | 4 ++--
 include/trace/events/tcp.h  | 2 +-
 net/ipv4/igmp.c             | 4 ++--
 net/ipv4/ip_output.c        | 5 +++--
 net/ipv4/raw.c              | 4 ++--
 net/ipv4/raw_diag.c         | 2 +-
Quoted 9 lines
 net/ipv4/udp.c              | 4 ++--
 net/ipv6/mcast.c            | 8 ++++----
 net/ipv6/ping.c             | 2 +-
 net/ipv6/raw.c              | 2 +-
 net/ipv6/udp.c              | 6 +++---
 net/mptcp/sockopt.c         | 2 +-
 security/lsm_audit.c        | 4 ++--
 18 files changed, 30 insertions(+), 32 deletions(-)

For the set:

#10 lore raw
In reply to Eric Dumazet
This series was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Quoted 9 lines · On Thu, 16 Mar 2023 15:31:54 +0000 you wrote:

inet_sk() can be changed to propagate const qualifier, thanks to container_of_const()

Following patches in this series add more const qualifiers.

Other helpers like tcp_sk(), udp_sk(), raw_sk(), ... will be handled in following series.

[...]

Here is the summary with links:
- [v2,net-next,1/8] inet: preserve const qualifier in inet_sk()

    https://git.kernel.org/netdev/net-next/c/abc17a11ed29
  - [v2,net-next,2/8] ipv4: constify ip_mc_sf_allow() socket argument
    https://git.kernel.org/netdev/net-next/c/33e972bdf0b0
  - [v2,net-next,3/8] udp: constify __udp_is_mcast_sock() socket argument
    https://git.kernel.org/netdev/net-next/c/a0a989d30075
  - [v2,net-next,4/8] ipv6: constify inet6_mc_check()
    https://git.kernel.org/netdev/net-next/c/66eb554c6449
  - [v2,net-next,5/8] udp6: constify __udp_v6_is_mcast_sock() socket argument
    https://git.kernel.org/netdev/net-next/c/dc3731bad8e1
  - [v2,net-next,6/8] ipv6: raw: constify raw_v6_match() socket argument
    https://git.kernel.org/netdev/net-next/c/db6af4fdb150
  - [v2,net-next,7/8] ipv4: raw: constify raw_v4_match() socket argument
    https://git.kernel.org/netdev/net-next/c/0a8c2568209e
  - [v2,net-next,8/8] inet_diag: constify raw_lookup() socket argument
    https://git.kernel.org/netdev/net-next/c/736c8b52c8ad

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html