net: initialize mark in sockcm_init
authorWillem de Bruijn <willemb@google.com>
Fri, 14 Feb 2025 22:26:59 +0000 (17:26 -0500)
committerJakub Kicinski <kuba@kernel.org>
Wed, 19 Feb 2025 02:27:19 +0000 (18:27 -0800)
Avoid open coding initialization of sockcm fields.
Avoid reading the sk_priority field twice.

This ensures all callers, existing and future, will correctly try a
cmsg passed mark before sk_mark.

This patch extends support for cmsg mark to:
packet_spkt and packet_tpacket and net/can/raw.c.

This patch extends support for cmsg priority to:
packet_spkt and packet_tpacket.

Signed-off-by: Willem de Bruijn <willemb@google.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
Link: https://patch.msgid.link/20250214222720.3205500-3-willemdebruijn.kernel@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/sock.h
net/can/raw.c
net/packet/af_packet.c

index 60ebf3c..fac65ed 100644 (file)
@@ -1829,6 +1829,7 @@ static inline void sockcm_init(struct sockcm_cookie *sockc,
                               const struct sock *sk)
 {
        *sockc = (struct sockcm_cookie) {
+               .mark = READ_ONCE(sk->sk_mark),
                .tsflags = READ_ONCE(sk->sk_tsflags),
                .priority = READ_ONCE(sk->sk_priority),
        };
index 46e8ed9..9b1d5f0 100644 (file)
@@ -963,7 +963,7 @@ static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
 
        skb->dev = dev;
        skb->priority = sockc.priority;
-       skb->mark = READ_ONCE(sk->sk_mark);
+       skb->mark = sockc.mark;
        skb->tstamp = sockc.transmit_time;
 
        skb_setup_tx_timestamp(skb, &sockc);
index c131e5c..3e9ddf7 100644 (file)
@@ -2102,8 +2102,8 @@ retry:
 
        skb->protocol = proto;
        skb->dev = dev;
-       skb->priority = READ_ONCE(sk->sk_priority);
-       skb->mark = READ_ONCE(sk->sk_mark);
+       skb->priority = sockc.priority;
+       skb->mark = sockc.mark;
        skb_set_delivery_type_by_clockid(skb, sockc.transmit_time, sk->sk_clockid);
        skb_setup_tx_timestamp(skb, &sockc);
 
@@ -2634,8 +2634,8 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
 
        skb->protocol = proto;
        skb->dev = dev;
-       skb->priority = READ_ONCE(po->sk.sk_priority);
-       skb->mark = READ_ONCE(po->sk.sk_mark);
+       skb->priority = sockc->priority;
+       skb->mark = sockc->mark;
        skb_set_delivery_type_by_clockid(skb, sockc->transmit_time, po->sk.sk_clockid);
        skb_setup_tx_timestamp(skb, sockc);
        skb_zcopy_set_nouarg(skb, ph.raw);
@@ -3039,7 +3039,6 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
                goto out_unlock;
 
        sockcm_init(&sockc, sk);
-       sockc.mark = READ_ONCE(sk->sk_mark);
        if (msg->msg_controllen) {
                err = sock_cmsg_send(sk, msg, &sockc);
                if (unlikely(err))