Skip to content

Commit c6af0c2

Browse files
wdebruijdavem330
authored andcommitted
ip: support SO_MARK cmsg
Enable setting skb->mark for UDP and RAW sockets using cmsg. This is analogous to existing support for TOS, TTL, txtime, etc. Packet sockets already support this as of commit c7d39e3 ("packet: support per-packet fwmark for af_packet sendmsg"). Similar to other fields, implement by 1. initialize the sockcm_cookie.mark from socket option sk_mark 2. optionally overwrite this in ip_cmsg_send/ip6_datagram_send_ctl 3. initialize inet_cork.mark from sockcm_cookie.mark 4. initialize each (usually just one) skb->mark from inet_cork.mark Step 1 is handled in one location for most protocols by ipcm_init_sk as of commit 3517820 ("ipv4: ipcm_cookie initializers"). Signed-off-by: Willem de Bruijn <willemb@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 823eb2a commit c6af0c2

9 files changed

Lines changed: 15 additions & 8 deletions

File tree

‎include/net/inet_sock.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ struct inet_cork {
160160
char priority;
161161
__u16 gso_size;
162162
u64 transmit_time;
163+
u32 mark;
163164
};
164165

165166
struct inet_cork_full {

‎include/net/ip.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
8888
{
8989
ipcm_init(ipcm);
9090

91+
ipcm->sockc.mark = inet->sk.sk_mark;
9192
ipcm->sockc.tsflags = inet->sk.sk_tsflags;
9293
ipcm->oif = inet->sk.sk_bound_dev_if;
9394
ipcm->addr = inet->inet_saddr;

‎net/ipv4/ip_output.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,7 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
12661266
cork->length = 0;
12671267
cork->ttl = ipc->ttl;
12681268
cork->tos = ipc->tos;
1269+
cork->mark = ipc->sockc.mark;
12691270
cork->priority = ipc->priority;
12701271
cork->transmit_time = ipc->sockc.transmit_time;
12711272
cork->tx_flags = 0;
@@ -1529,7 +1530,7 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
15291530
}
15301531

15311532
skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
1532-
skb->mark = sk->sk_mark;
1533+
skb->mark = cork->mark;
15331534
skb->tstamp = cork->transmit_time;
15341535
/*
15351536
* Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec

‎net/ipv4/ping.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
781781
} else if (!ipc.oif)
782782
ipc.oif = inet->uc_index;
783783

784-
flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
784+
flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos,
785785
RT_SCOPE_UNIVERSE, sk->sk_protocol,
786786
inet_sk_flowi_flags(sk), faddr, saddr, 0, 0,
787787
sk->sk_uid);

‎net/ipv4/raw.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
375375
skb_reserve(skb, hlen);
376376

377377
skb->priority = sk->sk_priority;
378-
skb->mark = sk->sk_mark;
378+
skb->mark = sockc->mark;
379379
skb->tstamp = sockc->transmit_time;
380380
skb_dst_set(skb, &rt->dst);
381381
*rtp = NULL;
@@ -623,7 +623,7 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
623623
}
624624
}
625625

626-
flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
626+
flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos,
627627
RT_SCOPE_UNIVERSE,
628628
hdrincl ? IPPROTO_RAW : sk->sk_protocol,
629629
inet_sk_flowi_flags(sk) |

‎net/ipv4/udp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
11301130

11311131
fl4 = &fl4_stack;
11321132

1133-
flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
1133+
flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos,
11341134
RT_SCOPE_UNIVERSE, sk->sk_protocol,
11351135
flow_flags,
11361136
faddr, saddr, dport, inet->inet_sport,

‎net/ipv6/ip6_output.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,7 @@ static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
12941294
cork->base.fragsize = mtu;
12951295
cork->base.gso_size = ipc6->gso_size;
12961296
cork->base.tx_flags = 0;
1297+
cork->base.mark = ipc6->sockc.mark;
12971298
sock_tx_timestamp(sk, ipc6->sockc.tsflags, &cork->base.tx_flags);
12981299

12991300
if (dst_allfrag(xfrm_dst_path(&rt->dst)))
@@ -1764,7 +1765,7 @@ struct sk_buff *__ip6_make_skb(struct sock *sk,
17641765
hdr->daddr = *final_dst;
17651766

17661767
skb->priority = sk->sk_priority;
1767-
skb->mark = sk->sk_mark;
1768+
skb->mark = cork->base.mark;
17681769

17691770
skb->tstamp = cork->base.transmit_time;
17701771

‎net/ipv6/raw.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
646646

647647
skb->protocol = htons(ETH_P_IPV6);
648648
skb->priority = sk->sk_priority;
649-
skb->mark = sk->sk_mark;
649+
skb->mark = sockc->mark;
650650
skb->tstamp = sockc->transmit_time;
651651

652652
skb_put(skb, length);
@@ -810,6 +810,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
810810

811811
ipcm6_init(&ipc6);
812812
ipc6.sockc.tsflags = sk->sk_tsflags;
813+
ipc6.sockc.mark = sk->sk_mark;
813814

814815
if (sin6) {
815816
if (addr_len < SIN6_LEN_RFC2133)
@@ -891,6 +892,7 @@ static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
891892
opt = ipv6_fixup_options(&opt_space, opt);
892893

893894
fl6.flowi6_proto = proto;
895+
fl6.flowi6_mark = ipc6.sockc.mark;
894896

895897
if (!hdrincl) {
896898
rfv.msg = msg;

‎net/ipv6/udp.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
12301230
ipcm6_init(&ipc6);
12311231
ipc6.gso_size = up->gso_size;
12321232
ipc6.sockc.tsflags = sk->sk_tsflags;
1233+
ipc6.sockc.mark = sk->sk_mark;
12331234

12341235
/* destination address check */
12351236
if (sin6) {
@@ -1352,7 +1353,7 @@ int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
13521353
if (!fl6.flowi6_oif)
13531354
fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
13541355

1355-
fl6.flowi6_mark = sk->sk_mark;
1356+
fl6.flowi6_mark = ipc6.sockc.mark;
13561357
fl6.flowi6_uid = sk->sk_uid;
13571358

13581359
if (msg->msg_controllen) {

0 commit comments

Comments
 (0)