diff mbox series

[net-next,v2,1/2] Introduce sk_set_prio_allowed helper function

Message ID 20241102125136.5030-2-annaemesenyiri@gmail.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series support SO_PRIORITY cmsg | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 3 this patch: 3
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: horms@kernel.org
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 23 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 9 this patch: 9
netdev/source_inline success Was 0 now: 0
netdev/contest fail net-next-2024-11-02--18-00 (tests: 781)

Commit Message

Anna Nyiri Nov. 2, 2024, 12:51 p.m. UTC
Simplifies the priority setting permissions through
`sk_set_prio_allowed` function. No functional changes.

Suggested-by: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
Signed-off-by: Anna Emese Nyiri <annaemesenyiri@gmail.com>
---
 net/core/sock.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

Comments

Willem de Bruijn Nov. 3, 2024, 1:30 a.m. UTC | #1
Anna Emese Nyiri wrote:
> Simplifies the priority setting permissions through
> `sk_set_prio_allowed` function. No functional changes.

tiny, only because asking for changes in patch 2/2: please use
imperative mood: "Simplify". And maybe mention that this is in
anticipation of a second caller in a following patch.
 
> Suggested-by: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
> Signed-off-by: Anna Emese Nyiri <annaemesenyiri@gmail.com>
> ---
>  net/core/sock.c | 11 ++++++++---
>  1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/net/core/sock.c b/net/core/sock.c
> index 7f398bd07fb7..5ecf6f1a470c 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -454,6 +454,13 @@ static int sock_set_timeout(long *timeo_p, sockptr_t optval, int optlen,
>  	return 0;
>  }
>  
> +static bool sk_set_prio_allowed(const struct sock *sk, int val)
> +{
> +	return ((val >= TC_PRIO_BESTEFFORT && val <= TC_PRIO_INTERACTIVE) ||
> +		sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) ||
> +		sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN));
> +}
> +
>  static bool sock_needs_netstamp(const struct sock *sk)
>  {
>  	switch (sk->sk_family) {
> @@ -1187,9 +1194,7 @@ int sk_setsockopt(struct sock *sk, int level, int optname,
>  	/* handle options which do not require locking the socket. */
>  	switch (optname) {
>  	case SO_PRIORITY:
> -		if ((val >= 0 && val <= 6) ||
> -		    sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) ||
> -		    sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
> +		if (sk_set_prio_allowed(sk, val)) {
>  			sock_set_priority(sk, val);
>  			return 0;
>  		}
> -- 
> 2.43.0
>
diff mbox series

Patch

diff --git a/net/core/sock.c b/net/core/sock.c
index 7f398bd07fb7..5ecf6f1a470c 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -454,6 +454,13 @@  static int sock_set_timeout(long *timeo_p, sockptr_t optval, int optlen,
 	return 0;
 }
 
+static bool sk_set_prio_allowed(const struct sock *sk, int val)
+{
+	return ((val >= TC_PRIO_BESTEFFORT && val <= TC_PRIO_INTERACTIVE) ||
+		sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) ||
+		sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN));
+}
+
 static bool sock_needs_netstamp(const struct sock *sk)
 {
 	switch (sk->sk_family) {
@@ -1187,9 +1194,7 @@  int sk_setsockopt(struct sock *sk, int level, int optname,
 	/* handle options which do not require locking the socket. */
 	switch (optname) {
 	case SO_PRIORITY:
-		if ((val >= 0 && val <= 6) ||
-		    sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) ||
-		    sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
+		if (sk_set_prio_allowed(sk, val)) {
 			sock_set_priority(sk, val);
 			return 0;
 		}