Skip to contentSkip to conversation

net: ipv6: Fix ping to link-local addresses.

PATCH Superseded Lorenzo Colitti 6 participants 22 messages Aug 8, 2016 – Aug 16, 2016

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ipv6: Fix ping to link-local addresses.

ping_v6_sendmsg never sets flowi6_oif, so it is not possible to ping an IPv6 address on a different interface. Instead, it sets flowi6_iif, which is incorrect but harmless. Also, it returns an error if a passed-in scope ID doesn't match sk_bound_dev_if.

Get rid of the error, stop setting flowi6_iif, and support various ways of setting oif in the same priority order used by udpv6_sendmsg.

Tested: https://android-review.googlesource.com/#/c/254470/

---
 net/ipv6/ping.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
net/ipv6/ping.c +15 -14
index fed40d1..eabf1ea 100644
@@ -55,7 +55,7 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	struct icmp6hdr user_icmph;
 	int addr_type;
 	struct in6_addr *daddr;
-	int iif = 0;
+	int oif = 0;
 	struct flowi6 fl6;
 	int err;
 	struct dst_entry *dst;
@@ -78,23 +78,28 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		if (u->sin6_family != AF_INET6) {
 			return -EAFNOSUPPORT;
 		}
-		if (sk->sk_bound_dev_if &&
-		    sk->sk_bound_dev_if != u->sin6_scope_id) {
-			return -EINVAL;
-		}
 		daddr = &(u->sin6_addr);
-		iif = u->sin6_scope_id;
+		if (__ipv6_addr_needs_scope_id(ipv6_addr_type(daddr)))
+			oif = u->sin6_scope_id;
 	} else {
 		if (sk->sk_state != TCP_ESTABLISHED)
 			return -EDESTADDRREQ;
 		daddr = &sk->sk_v6_daddr;
 	}
 
-	if (!iif)
-		iif = sk->sk_bound_dev_if;
+	if (!oif)
+		oif = sk->sk_bound_dev_if;
+
+	if (!oif)
+		oif = np->sticky_pktinfo.ipi6_ifindex;
+
+	if (!oif && ipv6_addr_is_multicast(daddr))
+		fl6.flowi6_oif = np->mcast_oif;
+	else if (!oif)
+		oif = np->ucast_oif;
 
 	addr_type = ipv6_addr_type(daddr);
-	if (__ipv6_addr_needs_scope_id(addr_type) && !iif)
+	if (__ipv6_addr_needs_scope_id(addr_type) && !oif)
 		return -EINVAL;
 	if (addr_type & IPV6_ADDR_MAPPED)
 		return -EINVAL;
@@ -106,16 +111,12 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	fl6.flowi6_proto = IPPROTO_ICMPV6;
 	fl6.saddr = np->saddr;
 	fl6.daddr = *daddr;
+	fl6.flowi6_oif = oif;
 	fl6.flowi6_mark = sk->sk_mark;
 	fl6.fl6_icmp_type = user_icmph.icmp6_type;
 	fl6.fl6_icmp_code = user_icmph.icmp6_code;
 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
 
-	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
-		fl6.flowi6_oif = np->mcast_oif;
-	else if (!fl6.flowi6_oif)
-		fl6.flowi6_oif = np->ucast_oif;
-
 	ipc6.tclass = np->tclass;
 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
 
Signature · 1 line
2.8.0.rc3.226.g39d4020
#1 lore raw
Quoted 29 lines · Lorenzo Colitti

ping_v6_sendmsg never sets flowi6_oif, so it is not possible to ping an IPv6 address on a different interface. Instead, it sets flowi6_iif, which is incorrect but harmless. Also, it returns an error if a passed-in scope ID doesn't match sk_bound_dev_if.

Get rid of the error, stop setting flowi6_iif, and support various ways of setting oif in the same priority order used by udpv6_sendmsg.

Tested: https://android-review.googlesource.com/#/c/254470/

---
 net/ipv6/ping.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
net/ipv6/ping.c +1 -1
index fed40d1..eabf1ea 100644
@@ -55,7 +55,7 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	struct icmp6hdr user_icmph;
 	int addr_type;
 	struct in6_addr *daddr;
-	int iif = 0;
+	int oif = 0;
 	struct flowi6 fl6;
 	int err;
 	struct dst_entry *dst;

net/ipv6/ping.c static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) · line 84

Quoted 7 lines
net/ipv6/ping.c +0 -4
@@ -78,23 +78,28 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 		if (u->sin6_family != AF_INET6) {
 			return -EAFNOSUPPORT;
 		}
-		if (sk->sk_bound_dev_if &&
-		    sk->sk_bound_dev_if != u->sin6_scope_id) {
-			return -EINVAL;
-		}

Hmm, sk->sk_bound_dev_if always has highest prio for the selection of the output interface. Thus this code made sense to me.

Bye,
Hannes

#2 lore raw
Quoted 7 lines · Hannes Frederic Sowa

net/ipv6/ping.c static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) · line 84

Quoted 4 lines
net/ipv6/ping.c +0 -4
-             if (sk->sk_bound_dev_if &&
-                 sk->sk_bound_dev_if != u->sin6_scope_id) {
-                     return -EINVAL;
-             }

Hmm, sk->sk_bound_dev_if always has highest prio for the selection of the output interface. Thus this code made sense to me.

Removing it is consistent with the other sendmsg functions such as udpv6_sendmsg or rawv6_sendmsg.

There is similar code in __ip6_datagram_connect, but that seems a bit different because that code also *sets* sk_bound_dev_if.

Personally I think it's better for pingv6_sendmsg be consistent with the other *_sendmsg functions than with ip6_datagram_connect, and thus the code should be removed. But I don't feel particularly strongly about it.

#4 lore raw
In reply to Lorenzo Colitti

net/ipv6/ping.c

Quoted 19 lines · Lorenzo Colitti

ping_v6_sendmsg never sets flowi6_oif, so it is not possible to ping an IPv6 address on a different interface. Instead, it sets flowi6_iif, which is incorrect but harmless. Also, it returns an error if a passed-in scope ID doesn't match sk_bound_dev_if.

Get rid of the error, stop setting flowi6_iif, and support various ways of setting oif in the same priority order used by udpv6_sendmsg.

Tested: https://android-review.googlesource.com/#/c/254470/

---
 net/ipv6/ping.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
net/ipv6/ping.c +0 -0
index fed40d1..eabf1ea 100644

-----8<-----

Quoted 9 lines
net/ipv6/ping.c +1 -0
@@ -106,16 +111,12 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 	fl6.flowi6_proto = IPPROTO_ICMPV6;
 	fl6.saddr = np->saddr;
 	fl6.daddr = *daddr;
+	fl6.flowi6_oif = oif;
 	fl6.flowi6_mark = sk->sk_mark;
 	fl6.fl6_icmp_type = user_icmph.icmp6_type;
 	fl6.fl6_icmp_code = user_icmph.icmp6_code;

net/ipv6/ping.c static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) · line 122

Quoted 10 lines
net/ipv6/ping.c +0 -5
 	security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
 
-	if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
-		fl6.flowi6_oif = np->mcast_oif;
-	else if (!fl6.flowi6_oif)
-		fl6.flowi6_oif = np->ucast_oif;
-
 	ipc6.tclass = np->tclass;
 	fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
 

That code removal is contrary to your patch description regarding flowi6_oif.

#3 lore raw
In reply to Lorenzo Colitti

Hi,

2016-08-08 23:45 GMT+09:00 Lorenzo Colitti <[email protected]>:

Quoted 13 lines
Quoted 7 lines · Hannes Frederic Sowa

net/ipv6/ping.c static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) · line 84

Quoted 4 lines
net/ipv6/ping.c +0 -4
-             if (sk->sk_bound_dev_if &&
-                 sk->sk_bound_dev_if != u->sin6_scope_id) {
-                     return -EINVAL;
-             }

Hmm, sk->sk_bound_dev_if always has highest prio for the selection of the output interface. Thus this code made sense to me.

Removing it is consistent with the other sendmsg functions such as udpv6_sendmsg or rawv6_sendmsg.

Quoted 7 lines

There is similar code in __ip6_datagram_connect, but that seems a bit different because that code also *sets* sk_bound_dev_if.

Personally I think it's better for pingv6_sendmsg be consistent with the other *_sendmsg functions than with ip6_datagram_connect, and thus the code should be removed. But I don't feel particularly strongly about it.

Following must be met, at least, IMHO.
- SO_BINDTODEVICE requires "root", which sets sk_bound_dev_if.
- sin6_scope_id and sk_bound_dev_if should match (if the address it link-local address), or each or both should equal to 0.

I think it would make more sense if former setting wins...

--yoshfuji

#5 lore raw
In reply to David Ahern
Quoted 7 lines · David Ahern

net/ipv6/ping.c static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len) · line 118

Quoted 5 lines
net/ipv6/ping.c +0 -5
-     if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
-             fl6.flowi6_oif = np->mcast_oif;
-     else if (!fl6.flowi6_oif)
-             fl6.flowi6_oif = np->ucast_oif;
-

That code removal is contrary to your patch description regarding flowi6_oif.

Which code removal? The one I quote above? That code wasn't removed, it was moved to above the initialization of flowi6.

#6 lore raw
Quoted 7 lines · Lorenzo Colitti
Quoted 6 lines · David Ahern
Quoted 5 lines
net/ipv6/ping.c +0 -5
-     if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
-             fl6.flowi6_oif = np->mcast_oif;
-     else if (!fl6.flowi6_oif)
-             fl6.flowi6_oif = np->ucast_oif;
-
Quoted 5 lines
Quoted 1 line

That code removal is contrary to your patch description regarding flowi6_oif.

Which code removal? The one I quote above? That code wasn't removed, it was moved to above the initialization of flowi6.

Your description states:
"ping_v6_sendmsg never sets flowi6_oif, so it is not possible to ping an IPv6 address on a different interface."

That code snippet above contradicts that -- flowi6_oif is set in ping_v6_sendmsg.

You are making a different change than just setting flowi6_oif.

maintainer
#8 lore raw
In reply to Lorenzo Colitti

From: Lorenzo Colitti <[email protected]>
Date: Mon, 8 Aug 2016 16:42:07 +0900

Quoted 11 lines

ping_v6_sendmsg never sets flowi6_oif, so it is not possible to ping an IPv6 address on a different interface. Instead, it sets flowi6_iif, which is incorrect but harmless. Also, it returns an error if a passed-in scope ID doesn't match sk_bound_dev_if.

Get rid of the error, stop setting flowi6_iif, and support various ways of setting oif in the same priority order used by udpv6_sendmsg.

Tested: https://android-review.googlesource.com/#/c/254470/

We should always give sk_bound_dev_if the highest priority.

Also, we should amend, not delete, the check against the scope ID in the sockaddr. As explained by YOSHIFUJI Hideaki.

#7 lore raw
In reply to David Ahern
Quoted 5 lines · David Ahern

Your description states:
"ping_v6_sendmsg never sets flowi6_oif, so it is not possible to ping an IPv6 address on a different interface."

That code snippet above contradicts that -- flowi6_oif is set in ping_v6_sendmsg.

Ah, yes, thanks. Will fix the commit message.

#9 lore raw
In reply to David Miller
Quoted 4 lines · David Miller

We should always give sk_bound_dev_if the highest priority.

Also, we should amend, not delete, the check against the scope ID in the sockaddr. As explained by YOSHIFUJI Hideaki.

Sure, I can do that.

Note that pretty much every sendmsg codepath allows other data to take precedence over sk_bound_dev_if:

- udpv6_sendmsg: if sin6_scope_id specified on a scoped address
- rawv6_sendmsg: if sin6_scope_id specified on a scoped address
- l2tp_ip6_sendmsg: if sin6_scope_id specified on a scoped address
- ip_cmsg_send: if IP_PKTINFO or IPV6_PKTINFO specified

What should I do about those? -EINVAL? Ignore the conflicting data? Leave as is?

maintainer
#10 lore raw

From: Lorenzo Colitti <[email protected]>
Date: Tue, 9 Aug 2016 10:00:25 +0900

Quoted 9 lines

Note that pretty much every sendmsg codepath allows other data to take precedence over sk_bound_dev_if:

- udpv6_sendmsg: if sin6_scope_id specified on a scoped address
- rawv6_sendmsg: if sin6_scope_id specified on a scoped address
- l2tp_ip6_sendmsg: if sin6_scope_id specified on a scoped address
- ip_cmsg_send: if IP_PKTINFO or IPV6_PKTINFO specified

What should I do about those? -EINVAL? Ignore the conflicting data? Leave as is?

That's a good point, I guess this needs some more thought.

#11 lore raw
Quoted 6 lines · David Miller

From: Lorenzo Colitti <[email protected]>
Date: Tue, 9 Aug 2016 10:00:25 +0900

Quoted 3 lines

Note that pretty much every sendmsg codepath allows other data to take precedence over sk_bound_dev_if:

Quoted 8 lines
Quoted 6 lines

- udpv6_sendmsg: if sin6_scope_id specified on a scoped address
- rawv6_sendmsg: if sin6_scope_id specified on a scoped address
- l2tp_ip6_sendmsg: if sin6_scope_id specified on a scoped address
- ip_cmsg_send: if IP_PKTINFO or IPV6_PKTINFO specified

What should I do about those? -EINVAL? Ignore the conflicting data? Leave as is?

That's a good point, I guess this needs some more thought.

I could see a point of view that says when bound_if is in play sending to destinations on/via other interfaces--by any mechanism--should effectively get ENETUNREACH (or something).

That does seem like I would involve changing some existing behavior, though.

#12 lore raw
Quoted 12 lines · Erik Kline
Quoted 11 lines · David Miller

From: Lorenzo Colitti <[email protected]>
Date: Tue, 9 Aug 2016 10:00:25 +0900

Quoted 8 lines

Note that pretty much every sendmsg codepath allows other data to take precedence over sk_bound_dev_if:

- udpv6_sendmsg: if sin6_scope_id specified on a scoped address
- rawv6_sendmsg: if sin6_scope_id specified on a scoped address
- l2tp_ip6_sendmsg: if sin6_scope_id specified on a scoped address
- ip_cmsg_send: if IP_PKTINFO or IPV6_PKTINFO specified

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What should I do about those? -EINVAL? Ignore the conflicting data? Leave as is?

That's a good point, I guess this needs some more thought.

I could see a point of view that says when bound_if is in play sending to destinations on/via other interfaces--by any mechanism--should effectively get ENETUNREACH (or something).

+1

Quoted 3 lines

That does seem like I would involve changing some existing behavior, though.

The use of sin6_scope_id and SO_BINDTODEVICE with different interfaces is incorrect and should be rejected.

Signature · 2 lines
Hideaki Yoshifuji <[email protected]>
Technical Division, MIRACLE LINUX CORPORATION
#13 lore raw
Quoted 23 lines · YOSHIFUJI Hideaki
Quoted 12 lines · Erik Kline
Quoted 11 lines · David Miller

From: Lorenzo Colitti <[email protected]>
Date: Tue, 9 Aug 2016 10:00:25 +0900

Quoted 8 lines

Note that pretty much every sendmsg codepath allows other data to take precedence over sk_bound_dev_if:

- udpv6_sendmsg: if sin6_scope_id specified on a scoped address
- rawv6_sendmsg: if sin6_scope_id specified on a scoped address
- l2tp_ip6_sendmsg: if sin6_scope_id specified on a scoped address
- ip_cmsg_send: if IP_PKTINFO or IPV6_PKTINFO specified

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What should I do about those? -EINVAL? Ignore the conflicting data? Leave as is?

That's a good point, I guess this needs some more thought.

I could see a point of view that says when bound_if is in play sending to destinations on/via other interfaces--by any mechanism--should effectively get ENETUNREACH (or something).

Quoted 8 lines

+1

Quoted 3 lines

That does seem like I would involve changing some existing behavior, though.

The use of sin6_scope_id and SO_BINDTODEVICE with different interfaces is incorrect and should be rejected.

I agree, I would actually change the behavior at this point, as it also could have security consequences from a network pov.

Bye,
Hannes

#14 lore raw
Quoted 5 lines · Hannes Frederic Sowa
Quoted 2 lines

The use of sin6_scope_id and SO_BINDTODEVICE with different interfaces is incorrect and should be rejected.

I agree, I would actually change the behavior at this point, as it also could have security consequences from a network pov.

I can send out a patch to do that. Is it something that can be done in net, or does it need to go into net-next?

#15 lore raw
In reply to Erik Kline
Quoted 12 lines · Erik Kline
Quoted 11 lines · David Miller

From: Lorenzo Colitti <[email protected]>
Date: Tue, 9 Aug 2016 10:00:25 +0900

Quoted 8 lines

Note that pretty much every sendmsg codepath allows other data to take precedence over sk_bound_dev_if:

- udpv6_sendmsg: if sin6_scope_id specified on a scoped address
- rawv6_sendmsg: if sin6_scope_id specified on a scoped address
- l2tp_ip6_sendmsg: if sin6_scope_id specified on a scoped address
- ip_cmsg_send: if IP_PKTINFO or IPV6_PKTINFO specified

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

What should I do about those? -EINVAL? Ignore the conflicting data? Leave as is?

That's a good point, I guess this needs some more thought.

I could see a point of view that says when bound_if is in play sending to destinations on/via other interfaces--by any mechanism--should effectively get ENETUNREACH (or something).

VRF uses this capability to send on an enslaved interface. ie., socket is bound to VRF device to limit packets to that L3 domain and then uses PKTINFO to force a packet out a particular interface.

#16 lore raw
Quoted 15 lines · David Ahern
Quoted 14 lines · Erik Kline
Quoted 13 lines · David Miller

From: Lorenzo Colitti <[email protected]>
Date: Tue, 9 Aug 2016 10:00:25 +0900

Quoted 9 lines

Note that pretty much every sendmsg codepath allows other data to take precedence over sk_bound_dev_if:

- udpv6_sendmsg: if sin6_scope_id specified on a scoped address
- rawv6_sendmsg: if sin6_scope_id specified on a scoped address
- l2tp_ip6_sendmsg: if sin6_scope_id specified on a scoped address
- ip_cmsg_send: if IP_PKTINFO or IPV6_PKTINFO specified

What should I do about those? -EINVAL? Ignore the conflicting data? Leave as is?

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

That's a good point, I guess this needs some more thought.

I could see a point of view that says when bound_if is in play sending to destinations on/via other interfaces--by any mechanism--should effectively get ENETUNREACH (or something).

VRF uses this capability to send on an enslaved interface. ie., socket is bound to VRF device to limit packets to that L3 domain and then uses PKTINFO to force a packet out a particular interface.

We could extend our code to allow enslave devices, maybe.

Signature · 2 lines
Hideaki Yoshifuji <[email protected]>
Technical Division, MIRACLE LINUX CORPORATION
#17 lore raw
Quoted 8 lines · YOSHIFUJI Hideaki
Quoted 6 lines
Quoted 4 lines

I could see a point of view that says when bound_if is in play sending to destinations on/via other interfaces--by any mechanism--should effectively get ENETUNREACH (or something).

VRF uses this capability to send on an enslaved interface. ie., socket is bound to VRF device to limit packets to that L3 domain and then uses PKTINFO to force a packet out a particular interface.

We could extend our code to allow enslave devices, maybe.

So something like this, then?

static inline bool inet_check_bound_oif(const struct sock *sk, int oif)

{
        if (!oif || !sk->sk_bound_dev_if || oif == sk->sk_bound_dev_if)
                return true;
#ifdef CONFIG_NET_L3_MASTER_DEV
        return l3mdev_master_ifindex_by_index(sock_net(sk), oif) ==
                sk->sk_bound_dev_if;

#endif

        return false;
}

and then in the various sendmsg functions:

        if (!inet_check_bound_oif(sk, oif))
                return -EINVAL;
#18 lore raw
Quoted 18 lines · Lorenzo Colitti
Quoted 8 lines · YOSHIFUJI Hideaki
Quoted 6 lines
Quoted 4 lines

I could see a point of view that says when bound_if is in play sending to destinations on/via other interfaces--by any mechanism--should effectively get ENETUNREACH (or something).

VRF uses this capability to send on an enslaved interface. ie., socket is bound to VRF device to limit packets to that L3 domain and then uses PKTINFO to force a packet out a particular interface.

We could extend our code to allow enslave devices, maybe.

So something like this, then?

static inline bool inet_check_bound_oif(const struct sock *sk, int oif)

{
        if (!oif || !sk->sk_bound_dev_if || oif == sk->sk_bound_dev_if)
                return true;
Quoted 7 lines
#ifdef CONFIG_NET_L3_MASTER_DEV
        return l3mdev_master_ifindex_by_index(sock_net(sk), oif) ==
                sk->sk_bound_dev_if;

#endif

        return false;
}

The ifdef is not needed; it's already in the l3mdev macros.

	int midx;

	if (!oif || !sk->sk_bound_dev_if || oif == sk->sk_bound_dev_if)
		return true;

	midx = l3mdev_master_ifindex_by_index(sock_net(sk), oif);
	if (midx && sk->sk_bound_dev_if == midx)
		return true;

	return false;
Quoted 6 lines

and then in the various sendmsg functions:

        if (!inet_check_bound_oif(sk, oif))
                return -EINVAL;
#19 lore raw
In reply to Lorenzo Colitti
Quoted 23 lines · Lorenzo Colitti
Quoted 8 lines · YOSHIFUJI Hideaki
Quoted 6 lines
Quoted 4 lines

I could see a point of view that says when bound_if is in play sending to destinations on/via other interfaces--by any mechanism--should effectively get ENETUNREACH (or something).

VRF uses this capability to send on an enslaved interface. ie., socket is bound to VRF device to limit packets to that L3 domain and then uses PKTINFO to force a packet out a particular interface.

We could extend our code to allow enslave devices, maybe.

So something like this, then?

static inline bool inet_check_bound_oif(const struct sock *sk, int oif)

{
        if (!oif || !sk->sk_bound_dev_if || oif == sk->sk_bound_dev_if)
                return true;
#ifdef CONFIG_NET_L3_MASTER_DEV
        return l3mdev_master_ifindex_by_index(sock_net(sk), oif) ==
                sk->sk_bound_dev_if;

#endif

Quoted 8 lines
        return false;
}

and then in the various sendmsg functions:

        if (!inet_check_bound_oif(sk, oif))
                return -EINVAL;

Yes, something like that.

--yoshfuji

#20 lore raw
Quoted 8 lines · YOSHIFUJI Hideaki
Quoted 6 lines

and then in the various sendmsg functions:

        if (!inet_check_bound_oif(sk, oif))
                return -EINVAL;

Yes, something like that.

There's another complication. inet6_bind and raw_bind take sin6_scope_id and assign it to sk_bound_dev_if:

        if (addr_len >= sizeof(struct sockaddr_in6) &&
            addr->sin6_scope_id) {
                if (addr->sin6_scope_id != sk
                        /* Override any existing binding, if another
                         * one is supplied by user.
                         */
                         sk->sk_bound_dev_if = addr->sin6_scope_id;
                }

The reason they do this is that the only place in the socket to score the scope ID is sk_bound_dev_if. The scope ID has to be stored in the socket, because it's the only way to ensure the semantics of scoped addresses, where the address without the scope ID is not unique, and thus the scope ID is effectively part of the address. For example:

1. A socket bound to fe80::1%eth0 and a socket bound to fe80::1%wlan0 must never see each other's packets. This means that things like udp6_lib_lookup must take the scope ID into account.
2. Calling getpeername() on a socket that's bound to fe80::1%eth0 must return eth0's ifindex in sin6_scope_id.

Unless we add a scope ID field to the socket, changing this behaviour would cause substantial breakage. It's perfectly legal to bind a socket to fe80::1%eth0 and then fe80::2%wlan0, for example. So we can't just say that sk_bound_dev_if must always take precedence on sin6_scope_id.

I also don't see how the VRF behaviour where sk_bound_dev_if sets the master interface and pktinto selects the slave interface" can be made to work at all in the presence of scoped addresses. I don't see any way to support a socket bound to fe80::1%eth0 and a socket bound to fe80::1%wlan0 in the same VRF.

#21 lore raw
Quoted 5 lines · Lorenzo Colitti

I also don't see how the VRF behaviour where sk_bound_dev_if sets the master interface and pktinto selects the slave interface" can be made to work at all in the presence of scoped addresses. I don't see any way to support a socket bound to fe80::1%eth0 and a socket bound to fe80::1%wlan0 in the same VRF.

It does not work with sk_bound_dev_if set; works fine without it.