Commit 959d5c11 authored by Eric Dumazet's avatar Eric Dumazet Committed by David S. Miller

inet: implement lockless getsockopt(IP_UNICAST_IF)

Add missing READ_ONCE() annotations when reading inet->uc_index

Implementing getsockopt(IP_UNICAST_IF) locklessly seems possible,
the setsockopt() part might not be possible at the moment.
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Reviewed-by: default avatarDavid Ahern <dsahern@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 3523bc91
...@@ -43,7 +43,7 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len ...@@ -43,7 +43,7 @@ int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len
if (!saddr) if (!saddr)
saddr = inet->mc_addr; saddr = inet->mc_addr;
} else if (!oif) { } else if (!oif) {
oif = inet->uc_index; oif = READ_ONCE(inet->uc_index);
} }
fl4 = &inet->cork.fl.u.ip4; fl4 = &inet->cork.fl.u.ip4;
rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr, oif, rt = ip_route_connect(fl4, usin->sin_addr.s_addr, saddr, oif,
......
...@@ -1113,7 +1113,7 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname, ...@@ -1113,7 +1113,7 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
ifindex = (__force int)ntohl((__force __be32)val); ifindex = (__force int)ntohl((__force __be32)val);
if (ifindex == 0) { if (ifindex == 0) {
inet->uc_index = 0; WRITE_ONCE(inet->uc_index, 0);
err = 0; err = 0;
break; break;
} }
...@@ -1130,7 +1130,7 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname, ...@@ -1130,7 +1130,7 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
if (sk->sk_bound_dev_if && midx != sk->sk_bound_dev_if) if (sk->sk_bound_dev_if && midx != sk->sk_bound_dev_if)
break; break;
inet->uc_index = ifindex; WRITE_ONCE(inet->uc_index, ifindex);
err = 0; err = 0;
break; break;
} }
...@@ -1633,6 +1633,9 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname, ...@@ -1633,6 +1633,9 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname,
return -ENOTCONN; return -ENOTCONN;
goto copyval; goto copyval;
} }
case IP_UNICAST_IF:
val = (__force int)htonl((__u32) READ_ONCE(inet->uc_index));
goto copyval;
} }
if (needs_rtnl) if (needs_rtnl)
...@@ -1640,9 +1643,6 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname, ...@@ -1640,9 +1643,6 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname,
sockopt_lock_sock(sk); sockopt_lock_sock(sk);
switch (optname) { switch (optname) {
case IP_UNICAST_IF:
val = (__force int)htonl((__u32) inet->uc_index);
break;
case IP_MULTICAST_IF: case IP_MULTICAST_IF:
{ {
struct in_addr addr; struct in_addr addr;
......
...@@ -777,7 +777,7 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ...@@ -777,7 +777,7 @@ static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
if (!saddr) if (!saddr)
saddr = inet->mc_addr; saddr = inet->mc_addr;
} else if (!ipc.oif) } else if (!ipc.oif)
ipc.oif = inet->uc_index; ipc.oif = READ_ONCE(inet->uc_index);
flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, scope, flowi4_init_output(&fl4, ipc.oif, ipc.sockc.mark, tos, scope,
sk->sk_protocol, inet_sk_flowi_flags(sk), faddr, sk->sk_protocol, inet_sk_flowi_flags(sk), faddr,
......
...@@ -482,7 +482,7 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ...@@ -482,7 +482,7 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
int free = 0; int free = 0;
__be32 daddr; __be32 daddr;
__be32 saddr; __be32 saddr;
int err; int uc_index, err;
struct ip_options_data opt_copy; struct ip_options_data opt_copy;
struct raw_frag_vec rfv; struct raw_frag_vec rfv;
int hdrincl; int hdrincl;
...@@ -576,24 +576,25 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ...@@ -576,24 +576,25 @@ static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
tos = get_rttos(&ipc, inet); tos = get_rttos(&ipc, inet);
scope = ip_sendmsg_scope(inet, &ipc, msg); scope = ip_sendmsg_scope(inet, &ipc, msg);
uc_index = READ_ONCE(inet->uc_index);
if (ipv4_is_multicast(daddr)) { if (ipv4_is_multicast(daddr)) {
if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
ipc.oif = inet->mc_index; ipc.oif = inet->mc_index;
if (!saddr) if (!saddr)
saddr = inet->mc_addr; saddr = inet->mc_addr;
} else if (!ipc.oif) { } else if (!ipc.oif) {
ipc.oif = inet->uc_index; ipc.oif = uc_index;
} else if (ipv4_is_lbcast(daddr) && inet->uc_index) { } else if (ipv4_is_lbcast(daddr) && uc_index) {
/* oif is set, packet is to local broadcast /* oif is set, packet is to local broadcast
* and uc_index is set. oif is most likely set * and uc_index is set. oif is most likely set
* by sk_bound_dev_if. If uc_index != oif check if the * by sk_bound_dev_if. If uc_index != oif check if the
* oif is an L3 master and uc_index is an L3 slave. * oif is an L3 master and uc_index is an L3 slave.
* If so, we want to allow the send using the uc_index. * If so, we want to allow the send using the uc_index.
*/ */
if (ipc.oif != inet->uc_index && if (ipc.oif != uc_index &&
ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk),
inet->uc_index)) { uc_index)) {
ipc.oif = inet->uc_index; ipc.oif = uc_index;
} }
} }
......
...@@ -1055,6 +1055,7 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ...@@ -1055,6 +1055,7 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
int (*getfrag)(void *, char *, int, int, int, struct sk_buff *); int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
struct sk_buff *skb; struct sk_buff *skb;
struct ip_options_data opt_copy; struct ip_options_data opt_copy;
int uc_index;
if (len > 0xFFFF) if (len > 0xFFFF)
return -EMSGSIZE; return -EMSGSIZE;
...@@ -1173,6 +1174,7 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ...@@ -1173,6 +1174,7 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
if (scope == RT_SCOPE_LINK) if (scope == RT_SCOPE_LINK)
connected = 0; connected = 0;
uc_index = READ_ONCE(inet->uc_index);
if (ipv4_is_multicast(daddr)) { if (ipv4_is_multicast(daddr)) {
if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif)) if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
ipc.oif = inet->mc_index; ipc.oif = inet->mc_index;
...@@ -1180,18 +1182,18 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) ...@@ -1180,18 +1182,18 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
saddr = inet->mc_addr; saddr = inet->mc_addr;
connected = 0; connected = 0;
} else if (!ipc.oif) { } else if (!ipc.oif) {
ipc.oif = inet->uc_index; ipc.oif = uc_index;
} else if (ipv4_is_lbcast(daddr) && inet->uc_index) { } else if (ipv4_is_lbcast(daddr) && uc_index) {
/* oif is set, packet is to local broadcast and /* oif is set, packet is to local broadcast and
* uc_index is set. oif is most likely set * uc_index is set. oif is most likely set
* by sk_bound_dev_if. If uc_index != oif check if the * by sk_bound_dev_if. If uc_index != oif check if the
* oif is an L3 master and uc_index is an L3 slave. * oif is an L3 master and uc_index is an L3 slave.
* If so, we want to allow the send using the uc_index. * If so, we want to allow the send using the uc_index.
*/ */
if (ipc.oif != inet->uc_index && if (ipc.oif != uc_index &&
ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk), ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk),
inet->uc_index)) { uc_index)) {
ipc.oif = inet->uc_index; ipc.oif = uc_index;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment