net: Require exact match for TCP socket lookups if dif is l3mdev
Subsystems (5)
PATCH v2 1/1 net: Require exact match for TCP socket lookups if dif is l3mdev
Currently, socket lookups for l3mdev (vrf) use cases can match a socket that is bound to a port but not a device (ie., a global socket). If the sysctl tcp_l3mdev_accept is not set this leads to ack packets going out based on the main table even though the packet came in from an L3 domain. The end result is that the connection does not establish creating confusion for users since the service is running and a socket shows in ss output. Fix by requiring an exact dif to sk_bound_dev_if match if the skb came through an interface enslaved to an l3mdev device and the tcp_l3mdev_accept is not set.
skb's through an l3mdev interface are marked by setting a flag in the inet{6}_skb_parm. The IPv6 variant is already set. This patch adds the flag for IPv4. Marking the skb avoids a device lookup on the dif.
The inet_skb_parm struct currently has a 1-byte hold following the flags, so the flags is expanded to u16 without increasing the size of the struct. This is needed to add another flag.
---
v2
- reordered the checks in inet_exact_dif_match per Eric's comment
- changed the l3mdev determination from looking up the dif to using a flag set on the skb which is much faster
drivers/net/vrf.c | 2 ++ include/linux/ipv6.h | 10 ++++++++++ include/net/ip.h | 13 ++++++++++++- net/ipv4/inet_hashtables.c | 7 ++++--- net/ipv6/inet6_hashtables.c | 7 ++++--- 5 files changed, 32 insertions(+), 7 deletions(-)
drivers/net/vrf.c +2 -0
@@ -956,6 +956,7 @@ static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,if (skb->pkt_type ==PACKET_LOOPBACK ) { skb->dev = vrf_dev; skb->skb_iif = vrf_dev->ifindex; +IP6CB (skb)->flags |=IP6SKB_L3SLAVE ; skb->pkt_type =PACKET_HOST ; goto out; }
@@ -996,6 +997,7 @@ static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev, { skb->dev = vrf_dev; skb->skb_iif = vrf_dev->ifindex; +IPCB (skb)->flags |=IPSKB_L3SLAVE ;/* loopback traffic; do not push through packet taps again. * Reset pkt_type for upper layers to process skb
include/linux/ipv6.h +10 -0
@@ -144,6 +144,16 @@ static inline int inet6_iif(const struct sk_buff *skb)return l3_slave ? skb->skb_iif :IP6CB (skb)->iif; } +static inline bool inet6_exact_dif_match (struct net *net,struct sk_buff *skb) +{ +#ifdef CONFIG_NET_L3_MASTER_DEV +if (!net->ipv4.sysctl_tcp_l3mdev_accept && +IP6CB (skb)->flags &IP6SKB_L3SLAVE ) +return true; +#endif +return false; +} +struct tcp6_request_sock {struct tcp_request_sock tcp6rsk_tcp; };
include/net/ip.h +12 -1
@@ -38,7 +38,7 @@ struct sock;struct inet_skb_parm {int iif;struct ip_options opt;/* Compiled IP options */ -unsigned char flags; +__u16 flags;#define IPSKB_FORWARDED BIT(0)#define IPSKB_XFRM_TUNNEL_SIZE BIT(1)
@@ -48,6 +48,7 @@ struct inet_skb_parm {#define IPSKB_DOREDIRECT BIT(5)#define IPSKB_FRAG_PMTU BIT(6)#define IPSKB_FRAG_SEGS BIT(7) +#define IPSKB_L3SLAVE BIT(8)u16 frag_max_size; };
@@ -71,6 +72,16 @@ struct ipcm_cookie {#define IPCB (skb) ((struct inet_skb_parm*)((skb)->cb))#define PKTINFO_SKB_CB (skb) ((struct in_pktinfo *)((skb)->cb)) +static inline bool inet_exact_dif_match (struct net *net,struct sk_buff *skb) +{ +#ifdef CONFIG_NET_L3_MASTER_DEV +if (!net->ipv4.sysctl_tcp_l3mdev_accept && +IPCB (skb)->flags &IPSKB_L3SLAVE ) +return true; +#endif +return false; +} +struct ip_ra_chain {struct ip_ra_chain __rcu *next;struct sock *sk;
net/ipv4/inet_hashtables.c +4 -3
@@ -172,7 +172,7 @@ EXPORT_SYMBOL_GPL(__inet_inherit_port);static inline int compute_score (struct sock *sk,struct net *net,const unsigned short hnum,const __be32 daddr, -const int dif) +const int dif,bool exact_dif) {int score =-1 ;struct inet_sock *inet =inet_sk (sk);
@@ -186,7 +186,7 @@ static inline int compute_score(struct sock *sk, struct net *net,return -1 ; score +=4 ; } -if (sk->sk_bound_dev_if) { +if (sk->sk_bound_dev_if || exact_dif) {if (sk->sk_bound_dev_if != dif)return -1 ; score +=4 ;
@@ -215,11 +215,12 @@ struct sock *__inet_lookup_listener(struct net *net,unsigned int hash =inet_lhashfn (net, hnum);struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];int score, hiscore =0 , matches =0 , reuseport =0 ; +bool exact_dif =inet_exact_dif_match (net, skb);struct sock *sk, *result =NULL ;u32 phash =0 ;sk_for_each_rcu (sk, &ilb->head) { - score =compute_score (sk, net, hnum, daddr, dif); + score =compute_score (sk, net, hnum, daddr, dif, exact_dif);if (score > hiscore) { reuseport = sk->sk_reuseport;if (reuseport) {
net/ipv6/inet6_hashtables.c +4 -3
@@ -96,7 +96,7 @@ EXPORT_SYMBOL(__inet6_lookup_established);static inline int compute_score (struct sock *sk,struct net *net,const unsigned short hnum,const struct in6_addr *daddr, -const int dif) +const int dif,bool exact_dif) {int score =-1 ;
@@ -109,7 +109,7 @@ static inline int compute_score(struct sock *sk, struct net *net,return -1 ; score++; } -if (sk->sk_bound_dev_if) { +if (sk->sk_bound_dev_if || exact_dif) {if (sk->sk_bound_dev_if != dif)return -1 ; score++;
@@ -131,11 +131,12 @@ struct sock *inet6_lookup_listener(struct net *net,unsigned int hash =inet_lhashfn (net, hnum);struct inet_listen_hashbucket *ilb = &hashinfo->listening_hash[hash];int score, hiscore =0 , matches =0 , reuseport =0 ; +bool exact_dif =inet6_exact_dif_match (net, skb);struct sock *sk, *result =NULL ;u32 phash =0 ;sk_for_each (sk, &ilb->head) { - score =compute_score (sk, net, hnum, daddr, dif); + score =compute_score (sk, net, hnum, daddr, dif, exact_dif);if (score > hiscore) { reuseport = sk->sk_reuseport;if (reuseport) {
Signature · 1 line
2.1.4
Quoted 58 lines · David Ahern
Currently, socket lookups for l3mdev (vrf) use cases can match a socket that is bound to a port but not a device (ie., a global socket). If the sysctl
tcp_l3mdev_acceptis not set this leads to ack packets going out based on the main table even though the packet came in from an L3 domain. The end result is that the connection does not establish creating confusion for users since the service is running and a socket shows in ss output. Fix by requiring an exact dif tosk_bound_dev_ifmatch if the skb came through an interface enslaved to an l3mdev device and thetcp_l3mdev_acceptis not set.skb's through an l3mdev interface are marked by setting a flag in the inet{6}_skb_parm. The IPv6 variant is already set. This patch adds the flag for IPv4. Marking the skb avoids a device lookup on the dif.
The
inet_skb_parmstruct currently has a 1-byte hold following the flags, so the flags is expanded to u16 without increasing the size of the struct. This is needed to add another flag.Fixes 193125dbd8eb ("net: Introduce VRF device driver")Signed-off-by David Ahern <[email protected]>---
v2
- reordered the checks ininet_exact_dif_matchper Eric's comment
- changed the l3mdev determination from looking up the dif to using a flag set on the skb which is much fasterdrivers/net/vrf.c | 2 ++ include/linux/ipv6.h | 10 ++++++++++ include/net/ip.h | 13 ++++++++++++- net/ipv4/inet_hashtables.c | 7 ++++--- net/ipv6/inet6_hashtables.c | 7 ++++--- 5 files changed, 32 insertions(+), 7 deletions(-)drivers/net/vrf.c +2
-0@@ -956,6 +956,7 @@ static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,if (skb->pkt_type ==PACKET_LOOPBACK ) { skb->dev = vrf_dev; skb->skb_iif = vrf_dev->ifindex; +IP6CB (skb)->flags |=IP6SKB_L3SLAVE ; skb->pkt_type =PACKET_HOST ; goto out; }@@ -996,6 +997,7 @@ static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev, { skb->dev = vrf_dev; skb->skb_iif = vrf_dev->ifindex; +IPCB (skb)->flags |=IPSKB_L3SLAVE ;/* loopback traffic; do not push through packet taps again. * Reset pkt_type for upper layers to process skb include/linux/ipv6.h +0
-0
include/linux/ipv6.h static inline int inet6_iif(const struct sk_buff *skb) · line 151
Quoted 8 lines
include/linux/ipv6.h +5
-0@@ -144,6 +144,16 @@ static inline int inet6_iif(const struct sk_buff *skb)return l3_slave ? skb->skb_iif :IP6CB (skb)->iif; } +static inline bool inet6_exact_dif_match (struct net *net,struct sk_buff *skb) +{ +#ifdef CONFIG_NET_L3_MASTER_DEV +if (!net->ipv4.sysctl_tcp_l3mdev_accept && +IP6CB (skb)->flags &IP6SKB_L3SLAVE )
There is a catch here.
TCP moves IP6CB() in a different location.
Reference :
971f10eca186 ("tcp: better TCP_SKB_CB layout to reduce cache line misses")
Problem is that the lookup can happen from IP early demux, before TCP moved IP{6}CB around.
So you might need to let the caller pass IP6CB(skb)->flags (or TCP_SKB_CB(skb)->header.h6.flags ) instead of skb since inet6_exact_dif_match() does not know where to fetch the flags.
Same issue for IPv4.
Quoted 6 lines · Eric Dumazet
There is a catch here.
TCP movesIP6CB()in a different location.Reference :
971f10eca186("tcp: betterTCP_SKB_CBlayout to reduce cache line misses")
thanks for the reference.
Quoted 2 lines
Problem is that the lookup can happen from IP early demux, before TCP moved IP{6}CB around.
For TCP we only need the exact_dif match for listen sockets, so early demux does not apply.
Quoted 6 lines
So you might need to let the caller pass IP6CB(skb)->flags (or TCP_SKB_CB(skb)->header.h6.flags ) instead of skb since
inet6_exact_dif_match()does not know where to fetch the flags.Same issue for IPv4.
I'll update the match functions to pull from TCP_SKB_CB instead of IP6CB and make a note of the above.
Thanks for the review
Quoted 7 lines · David Ahern
Quoted 5 lines
So you might need to let the caller pass IP6CB(skb)->flags (or TCP_SKB_CB(skb)->header.h6.flags ) instead of skb since
inet6_exact_dif_match()does not know where to fetch the flags.Same issue for IPv4.
I'll update the match functions to pull from
TCP_SKB_CBinstead of IP6CB and make a note of the above.
IPv6 does the move after the socket lookup where IPv4 does it before.