Skip to content

Commit 5ef31ea

Browse files
Richard GobertPaolo Abeni
authored andcommitted
net: gro: fix udp bad offset in socket lookup by adding {inner_}network_offset to napi_gro_cb
Commits a602456 ("udp: Add GRO functions to UDP socket") and 57c67ff ("udp: additional GRO support") introduce incorrect usage of {ip,ipv6}_hdr in the complete phase of gro. The functions always return skb->network_header, which in the case of encapsulated packets at the gro complete phase, is always set to the innermost L3 of the packet. That means that calling {ip,ipv6}_hdr for skbs which completed the GRO receive phase (both in gro_list and *_gro_complete) when parsing an encapsulated packet's _outer_ L3/L4 may return an unexpected value. This incorrect usage leads to a bug in GRO's UDP socket lookup. udp{4,6}_lib_lookup_skb functions use ip_hdr/ipv6_hdr respectively. These *_hdr functions return network_header which will point to the innermost L3, resulting in the wrong offset being used in __udp{4,6}_lib_lookup with encapsulated packets. This patch adds network_offset and inner_network_offset to napi_gro_cb, and makes sure both are set correctly. To fix the issue, network_offsets union is used inside napi_gro_cb, in which both the outer and the inner network offsets are saved. Reproduction example: Endpoint configuration example (fou + local address bind) # ip fou add port 6666 ipproto 4 # ip link add name tun1 type ipip remote 2.2.2.1 local 2.2.2.2 encap fou encap-dport 5555 encap-sport 6666 mode ipip # ip link set tun1 up # ip a add 1.1.1.2/24 dev tun1 Netperf TCP_STREAM result on net-next before patch is applied: net-next main, GRO enabled: $ netperf -H 1.1.1.2 -t TCP_STREAM -l 5 Recv Send Send Socket Socket Message Elapsed Size Size Size Time Throughput bytes bytes bytes secs. 10^6bits/sec 131072 16384 16384 5.28 2.37 net-next main, GRO disabled: $ netperf -H 1.1.1.2 -t TCP_STREAM -l 5 Recv Send Send Socket Socket Message Elapsed Size Size Size Time Throughput bytes bytes bytes secs. 10^6bits/sec 131072 16384 16384 5.01 2745.06 patch applied, GRO enabled: $ netperf -H 1.1.1.2 -t TCP_STREAM -l 5 Recv Send Send Socket Socket Message Elapsed Size Size Size Time Throughput bytes bytes bytes secs. 10^6bits/sec 131072 16384 16384 5.01 2877.38 Fixes: a602456 ("udp: Add GRO functions to UDP socket") Signed-off-by: Richard Gobert <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Reviewed-by: Willem de Bruijn <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent fc1092f commit 5ef31ea

File tree

9 files changed

+22
-4
lines changed

9 files changed

+22
-4
lines changed

include/net/gro.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ struct napi_gro_cb {
8787

8888
/* used to support CHECKSUM_COMPLETE for tunneling protocols */
8989
__wsum csum;
90+
91+
/* L3 offsets */
92+
union {
93+
struct {
94+
u16 network_offset;
95+
u16 inner_network_offset;
96+
};
97+
u16 network_offsets[2];
98+
};
9099
};
91100

92101
#define NAPI_GRO_CB(skb) ((struct napi_gro_cb *)(skb)->cb)

net/8021q/vlan_core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ static struct sk_buff *vlan_gro_receive(struct list_head *head,
478478
if (unlikely(!vhdr))
479479
goto out;
480480

481+
NAPI_GRO_CB(skb)->network_offsets[NAPI_GRO_CB(skb)->encap_mark] = hlen;
482+
481483
type = vhdr->h_vlan_encapsulated_proto;
482484

483485
ptype = gro_find_receive_by_type(type);

net/core/gro.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ static inline void skb_gro_reset_offset(struct sk_buff *skb, u32 nhoff)
371371
const skb_frag_t *frag0;
372372
unsigned int headlen;
373373

374+
NAPI_GRO_CB(skb)->network_offset = 0;
374375
NAPI_GRO_CB(skb)->data_offset = 0;
375376
headlen = skb_headlen(skb);
376377
NAPI_GRO_CB(skb)->frag0 = skb->data;

net/ipv4/af_inet.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,7 @@ struct sk_buff *inet_gro_receive(struct list_head *head, struct sk_buff *skb)
15721572
/* The above will be needed by the transport layer if there is one
15731573
* immediately following this IP hdr.
15741574
*/
1575+
NAPI_GRO_CB(skb)->inner_network_offset = off;
15751576

15761577
/* Note : No need to call skb_gro_postpull_rcsum() here,
15771578
* as we already checked checksum over ipv4 header was 0

net/ipv4/udp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,8 @@ static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
532532
struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb,
533533
__be16 sport, __be16 dport)
534534
{
535-
const struct iphdr *iph = ip_hdr(skb);
535+
const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
536+
const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
536537
struct net *net = dev_net(skb->dev);
537538
int iif, sdif;
538539

net/ipv4/udp_offload.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,8 @@ EXPORT_SYMBOL(udp_gro_complete);
718718

719719
INDIRECT_CALLABLE_SCOPE int udp4_gro_complete(struct sk_buff *skb, int nhoff)
720720
{
721-
const struct iphdr *iph = ip_hdr(skb);
721+
const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
722+
const struct iphdr *iph = (struct iphdr *)(skb->data + offset);
722723
struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
723724

724725
/* do fraglist only if there is no outer UDP encap (or we already processed it) */

net/ipv6/ip6_offload.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ INDIRECT_CALLABLE_SCOPE struct sk_buff *ipv6_gro_receive(struct list_head *head,
237237
goto out;
238238

239239
skb_set_network_header(skb, off);
240+
NAPI_GRO_CB(skb)->inner_network_offset = off;
240241

241242
flush += ntohs(iph->payload_len) != skb->len - hlen;
242243

net/ipv6/udp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
272272
struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
273273
__be16 sport, __be16 dport)
274274
{
275-
const struct ipv6hdr *iph = ipv6_hdr(skb);
275+
const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
276+
const struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + offset);
276277
struct net *net = dev_net(skb->dev);
277278
int iif, sdif;
278279

net/ipv6/udp_offload.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ struct sk_buff *udp6_gro_receive(struct list_head *head, struct sk_buff *skb)
164164

165165
INDIRECT_CALLABLE_SCOPE int udp6_gro_complete(struct sk_buff *skb, int nhoff)
166166
{
167-
const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
167+
const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
168+
const struct ipv6hdr *ipv6h = (struct ipv6hdr *)(skb->data + offset);
168169
struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
169170

170171
/* do fraglist only if there is no outer UDP encap (or we already processed it) */

0 commit comments

Comments
 (0)