All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anna Emese Nyiri <annaemesenyiri@gmail.com>
To: netdev@vger.kernel.org
Cc: fejes@inf.elte.hu, annaemesenyiri@gmail.com, edumazet@google.com,
	kuba@kernel.org, pabeni@redhat.com,
	willemdebruijn.kernel@gmail.com
Subject: [PATCH net-next v3 1/3] net: Introduce sk_set_prio_allowed helper function
Date: Thu,  7 Nov 2024 14:22:29 +0100	[thread overview]
Message-ID: <20241107132231.9271-2-annaemesenyiri@gmail.com> (raw)
In-Reply-To: <20241107132231.9271-1-annaemesenyiri@gmail.com>

Simplify priority setting permissions with the `sk_set_prio_allowed`
function, centralizing the validation logic. This change is made in
anticipation of a second caller in a following patch.
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(-)

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


  reply	other threads:[~2024-11-07 13:23 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-07 13:22 [PATCH net-next v3 0/3] SO_PRIORITY cmsg patch summary Anna Emese Nyiri
2024-11-07 13:22 ` Anna Emese Nyiri [this message]
2024-11-07 13:48   ` [PATCH net-next v3 1/3] net: Introduce sk_set_prio_allowed helper function Eric Dumazet
2024-11-07 15:55   ` Willem de Bruijn
2024-11-07 13:22 ` [PATCH net-next v3 2/3] net: support SO_PRIORITY cmsg Anna Emese Nyiri
2024-11-07 13:50   ` Eric Dumazet
2024-11-07 16:17   ` Willem de Bruijn
2024-11-07 13:22 ` [PATCH net-next v3 3/3] selftest: net: test SO_PRIORITY ancillary data with cmsg_sender Anna Emese Nyiri
2024-11-07 17:22   ` Willem de Bruijn
2024-11-08 14:46     ` Willem de Bruijn
2024-11-09 18:54       ` Ferenc Fejes
2024-11-09 19:56         ` Willem de Bruijn
2024-11-10 13:27           ` Ido Schimmel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241107132231.9271-2-annaemesenyiri@gmail.com \
    --to=annaemesenyiri@gmail.com \
    --cc=edumazet@google.com \
    --cc=fejes@inf.elte.hu \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemdebruijn.kernel@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.