Skip to contentSkip to conversation

sock: Reset dst when changing sk_mark via setsockopt

PATCH Superseded David Barmann 2 participants 3 messages Nov 8, 2018

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sock: Reset dst when changing sk_mark via setsockopt

When setting the SO_MARK socket option, the dst needs to be reset so that a new route lookup is performed.

This fixes the case where an application wants to change routing by setting a new sk_mark. If this is done after some packets have already been sent, the dst is cached and has no effect.

---
 net/core/sock.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/core/sock.c +6 -2
index 6fcc4bc07d19..187badac24a3 100644
@@ -950,10 +950,14 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
 			clear_bit(SOCK_PASSSEC, &sock->flags);
 		break;
 	case SO_MARK:
-		if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
+		if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
 			ret = -EPERM;
-		else
+		} else {
+			struct dst_entry *dst = sk_dst_get(sk);
 			sk->sk_mark = val;
+			sk_dst_reset(sk);
+			dst_release(dst);
+		}
 		break;
 
 	case SO_RXQ_OVFL:
Signature · 1 line
2.14.4
#1 lore raw
Quoted 18 lines · David Barmann

When setting the SO_MARK socket option, the dst needs to be reset so that a new route lookup is performed.

This fixes the case where an application wants to change routing by setting a new sk_mark. If this is done after some packets have already been sent, the dst is cached and has no effect.

---
 net/core/sock.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/core/sock.c +0 -0
index 6fcc4bc07d19..187badac24a3 100644
@@ -950,10 +950,14 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
 			clear_bit(SOCK_PASSSEC, &sock->flags);

net/core/sock.c int sock_setsockopt(struct socket *sock, int level, int optname, · line 958

Quoted 10 lines
net/core/sock.c +4 -2
 		break;
 	case SO_MARK:
-		if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
+		if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
 			ret = -EPERM;
-		else
+		} else {
+			struct dst_entry *dst = sk_dst_get(sk);
 			sk->sk_mark = val;
+			sk_dst_reset(sk);

sk_dst_get() and dst_release() seems extra overhead ?

net/ipv4/ip_sockglue.c do_ip_setsockopt() has a similar handling for IP_TOS, and it only calls sk_dst_reset() (If the new TOS is different than the current one)

So I would suggest :

	if (!ns_capable(...)) {
        	ret = -EPERM;
	} else if (val != sk->sk_mark) {
		sk->sk_mark = val;
		sk_dst_reset(sk);
	}
#2 lore raw
Quoted 23 lines · David Barmann

net/core/sock.c int sock_setsockopt(struct socket *sock, int level, int optname, · line 958

Quoted 13 lines

When setting the SO_MARK socket option, the dst needs to be reset so that a new route lookup is performed.

This fixes the case where an application wants to change routing by setting a new sk_mark. If this is done after some packets have already been sent, the dst is cached and has no effect.

net/core/sock.c +3 -0
+		} else {
+			struct dst_entry *dst = sk_dst_get(sk);
 			sk->sk_mark = val;
+			sk_dst_reset(sk);

sk_dst_get() and dst_release() seems extra overhead ?

net/ipv4/ip_sockglue.c do_ip_setsockopt() has a similar handling for IP_TOS, and it only calls sk_dst_reset() (If the new TOS is different than the current one)

Quoted 8 lines

So I would suggest :

	if (!ns_capable(...)) {
        	ret = -EPERM;
	} else if (val != sk->sk_mark) {
		sk->sk_mark = val;
		sk_dst_reset(sk);
	}

Thanks Eric, I'll redo that and resubmit.

-David