Blame view

net/ipv4/gre_offload.c 7.04 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
c50cd3578   Daniel Borkmann   net: gre: move GS...
2
3
4
5
  /*
   *	IPV4 GSO/GRO offload support
   *	Linux INET implementation
   *
c50cd3578   Daniel Borkmann   net: gre: move GS...
6
7
8
9
   *	GRE GSO support
   */
  
  #include <linux/skbuff.h>
cf1722837   Paul Gortmaker   net/ipv4: don't u...
10
  #include <linux/init.h>
c50cd3578   Daniel Borkmann   net: gre: move GS...
11
12
  #include <net/protocol.h>
  #include <net/gre.h>
c50cd3578   Daniel Borkmann   net: gre: move GS...
13
14
15
  static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
  				       netdev_features_t features)
  {
2e598af71   Alexander Duyck   gre: Use GSO flag...
16
  	int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
786a9368b   Lorenzo Bianconi   net: gre: recompu...
17
  	bool need_csum, need_recompute_csum, gso_partial;
c50cd3578   Daniel Borkmann   net: gre: move GS...
18
  	struct sk_buff *segs = ERR_PTR(-EINVAL);
7a7ffbabf   Wei-Chun Chao   ipv4: fix tunnele...
19
  	u16 mac_offset = skb->mac_header;
c50cd3578   Daniel Borkmann   net: gre: move GS...
20
  	__be16 protocol = skb->protocol;
2e598af71   Alexander Duyck   gre: Use GSO flag...
21
22
  	u16 mac_len = skb->mac_len;
  	int gre_offset, outer_hlen;
c50cd3578   Daniel Borkmann   net: gre: move GS...
23

53e503989   Tom Herbert   net: Remove gso_s...
24
25
  	if (!skb->encapsulation)
  		goto out;
2e598af71   Alexander Duyck   gre: Use GSO flag...
26
  	if (unlikely(tnl_hlen < sizeof(struct gre_base_hdr)))
c50cd3578   Daniel Borkmann   net: gre: move GS...
27
  		goto out;
2e598af71   Alexander Duyck   gre: Use GSO flag...
28
  	if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
b884b1a46   Neal Cardwell   gre_offload: simp...
29
  		goto out;
c50cd3578   Daniel Borkmann   net: gre: move GS...
30
  	/* setup inner skb. */
c50cd3578   Daniel Borkmann   net: gre: move GS...
31
  	skb->encapsulation = 0;
5197f3499   Alexander Duyck   net: Reset encap_...
32
  	SKB_GSO_CB(skb)->encap_level = 0;
2e598af71   Alexander Duyck   gre: Use GSO flag...
33
  	__skb_pull(skb, tnl_hlen);
c50cd3578   Daniel Borkmann   net: gre: move GS...
34
35
36
  	skb_reset_mac_header(skb);
  	skb_set_network_header(skb, skb_inner_network_offset(skb));
  	skb->mac_len = skb_inner_network_offset(skb);
387203524   Alexander Duyck   gre: Use inner_pr...
37
  	skb->protocol = skb->inner_protocol;
c50cd3578   Daniel Borkmann   net: gre: move GS...
38

2e598af71   Alexander Duyck   gre: Use GSO flag...
39
  	need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_GRE_CSUM);
786a9368b   Lorenzo Bianconi   net: gre: recompu...
40
  	need_recompute_csum = skb->csum_not_inet;
2e598af71   Alexander Duyck   gre: Use GSO flag...
41
  	skb->encap_hdr_csum = need_csum;
bef3c6c93   Alexander Duyck   net: Drop unecess...
42
  	features &= skb->dev->hw_enc_features;
c50cd3578   Daniel Borkmann   net: gre: move GS...
43
  	/* segment inner packet. */
bef3c6c93   Alexander Duyck   net: Drop unecess...
44
  	segs = skb_mac_gso_segment(skb, features);
5a8dbf03d   Himangi Saraogi   net/ipv4: Use IS_...
45
  	if (IS_ERR_OR_NULL(segs)) {
2e598af71   Alexander Duyck   gre: Use GSO flag...
46
47
  		skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
  				     mac_len);
c50cd3578   Daniel Borkmann   net: gre: move GS...
48
  		goto out;
7a7ffbabf   Wei-Chun Chao   ipv4: fix tunnele...
49
  	}
c50cd3578   Daniel Borkmann   net: gre: move GS...
50

07b26c945   Steffen Klassert   gso: Support part...
51
  	gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
2e598af71   Alexander Duyck   gre: Use GSO flag...
52
53
  	outer_hlen = skb_tnl_header_len(skb);
  	gre_offset = outer_hlen - tnl_hlen;
c50cd3578   Daniel Borkmann   net: gre: move GS...
54
  	skb = segs;
c50cd3578   Daniel Borkmann   net: gre: move GS...
55
  	do {
387203524   Alexander Duyck   gre: Use inner_pr...
56
  		struct gre_base_hdr *greh;
802ab55ad   Alexander Duyck   GSO: Support part...
57
  		__sum16 *pcsum;
c50cd3578   Daniel Borkmann   net: gre: move GS...
58

224638766   Alexander Duyck   GSO: Provide soft...
59
60
61
62
63
  		/* Set up inner headers if we are offloading inner checksum */
  		if (skb->ip_summed == CHECKSUM_PARTIAL) {
  			skb_reset_inner_headers(skb);
  			skb->encapsulation = 1;
  		}
cdbaa0bb2   Alexander Duyck   gso: Update tunne...
64

c50cd3578   Daniel Borkmann   net: gre: move GS...
65
66
  		skb->mac_len = mac_len;
  		skb->protocol = protocol;
2e598af71   Alexander Duyck   gre: Use GSO flag...
67
68
69
70
71
72
73
74
75
76
  
  		__skb_push(skb, outer_hlen);
  		skb_reset_mac_header(skb);
  		skb_set_network_header(skb, mac_len);
  		skb_set_transport_header(skb, gre_offset);
  
  		if (!need_csum)
  			continue;
  
  		greh = (struct gre_base_hdr *)skb_transport_header(skb);
802ab55ad   Alexander Duyck   GSO: Support part...
77
  		pcsum = (__sum16 *)(greh + 1);
3d0241d57   Alexey Kodanev   gso: fix payload ...
78
  		if (gso_partial && skb_is_gso(skb)) {
802ab55ad   Alexander Duyck   GSO: Support part...
79
80
81
82
83
84
85
86
87
88
89
90
91
  			unsigned int partial_adj;
  
  			/* Adjust checksum to account for the fact that
  			 * the partial checksum is based on actual size
  			 * whereas headers should be based on MSS size.
  			 */
  			partial_adj = skb->len + skb_headroom(skb) -
  				      SKB_GSO_CB(skb)->data_offset -
  				      skb_shinfo(skb)->gso_size;
  			*pcsum = ~csum_fold((__force __wsum)htonl(partial_adj));
  		} else {
  			*pcsum = 0;
  		}
2e598af71   Alexander Duyck   gre: Use GSO flag...
92

802ab55ad   Alexander Duyck   GSO: Support part...
93
  		*(pcsum + 1) = 0;
786a9368b   Lorenzo Bianconi   net: gre: recompu...
94
95
96
97
98
99
100
101
102
  		if (need_recompute_csum && !skb_is_gso(skb)) {
  			__wsum csum;
  
  			csum = skb_checksum(skb, gre_offset,
  					    skb->len - gre_offset, 0);
  			*pcsum = csum_fold(csum);
  		} else {
  			*pcsum = gso_make_checksum(skb, 0);
  		}
c50cd3578   Daniel Borkmann   net: gre: move GS...
103
104
105
106
  	} while ((skb = skb->next));
  out:
  	return segs;
  }
d4546c250   David Miller   net: Convert GRO ...
107
108
  static struct sk_buff *gre_gro_receive(struct list_head *head,
  				       struct sk_buff *skb)
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
109
  {
d4546c250   David Miller   net: Convert GRO ...
110
  	struct sk_buff *pp = NULL;
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
111
112
113
114
115
116
117
  	struct sk_buff *p;
  	const struct gre_base_hdr *greh;
  	unsigned int hlen, grehlen;
  	unsigned int off;
  	int flush = 1;
  	struct packet_offload *ptype;
  	__be16 type;
fac8e0f57   Jesse Gross   tunnels: Don't ap...
118
119
120
121
  	if (NAPI_GRO_CB(skb)->encap_mark)
  		goto out;
  
  	NAPI_GRO_CB(skb)->encap_mark = 1;
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  	off = skb_gro_offset(skb);
  	hlen = off + sizeof(*greh);
  	greh = skb_gro_header_fast(skb, off);
  	if (skb_gro_header_hard(skb, hlen)) {
  		greh = skb_gro_header_slow(skb, hlen, off);
  		if (unlikely(!greh))
  			goto out;
  	}
  
  	/* Only support version 0 and K (key), C (csum) flags. Note that
  	 * although the support for the S (seq#) flag can be added easily
  	 * for GRO, this is problematic for GSO hence can not be enabled
  	 * here because a GRO pkt may end up in the forwarding path, thus
  	 * requiring GSO support to break it up correctly.
  	 */
  	if ((greh->flags & ~(GRE_KEY|GRE_CSUM)) != 0)
  		goto out;
a0ca153f9   Alexander Duyck   GRE: Disable segm...
139
140
141
142
143
144
145
  	/* We can only support GRE_CSUM if we can track the location of
  	 * the GRE header.  In the case of FOU/GUE we cannot because the
  	 * outer UDP header displaces the GRE header leaving us in a state
  	 * of limbo.
  	 */
  	if ((greh->flags & GRE_CSUM) && NAPI_GRO_CB(skb)->is_fou)
  		goto out;
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
146
147
148
149
  	type = greh->protocol;
  
  	rcu_read_lock();
  	ptype = gro_find_receive_by_type(type);
51456b291   Ian Morris   ipv4: coding styl...
150
  	if (!ptype)
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  		goto out_unlock;
  
  	grehlen = GRE_HEADER_SECTION;
  
  	if (greh->flags & GRE_KEY)
  		grehlen += GRE_HEADER_SECTION;
  
  	if (greh->flags & GRE_CSUM)
  		grehlen += GRE_HEADER_SECTION;
  
  	hlen = off + grehlen;
  	if (skb_gro_header_hard(skb, hlen)) {
  		greh = skb_gro_header_slow(skb, hlen, off);
  		if (unlikely(!greh))
  			goto out_unlock;
  	}
758f75d1f   Tom Herbert   gre: call skb_gro...
167
168
  
  	/* Don't bother verifying checksum if we're going to flush anyway. */
884d338c0   Tom Herbert   gre: Add support ...
169
170
  	if ((greh->flags & GRE_CSUM) && !NAPI_GRO_CB(skb)->flush) {
  		if (skb_gro_checksum_simple_validate(skb))
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
171
  			goto out_unlock;
758f75d1f   Tom Herbert   gre: call skb_gro...
172

884d338c0   Tom Herbert   gre: Add support ...
173
174
175
  		skb_gro_checksum_try_convert(skb, IPPROTO_GRE, 0,
  					     null_compute_pseudo);
  	}
d4546c250   David Miller   net: Convert GRO ...
176
  	list_for_each_entry(p, head, list) {
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  		const struct gre_base_hdr *greh2;
  
  		if (!NAPI_GRO_CB(p)->same_flow)
  			continue;
  
  		/* The following checks are needed to ensure only pkts
  		 * from the same tunnel are considered for aggregation.
  		 * The criteria for "the same tunnel" includes:
  		 * 1) same version (we only support version 0 here)
  		 * 2) same protocol (we only support ETH_P_IP for now)
  		 * 3) same set of flags
  		 * 4) same key if the key field is present.
  		 */
  		greh2 = (struct gre_base_hdr *)(p->data + off);
  
  		if (greh2->flags != greh->flags ||
  		    greh2->protocol != greh->protocol) {
  			NAPI_GRO_CB(p)->same_flow = 0;
  			continue;
  		}
  		if (greh->flags & GRE_KEY) {
  			/* compare keys */
  			if (*(__be32 *)(greh2+1) != *(__be32 *)(greh+1)) {
  				NAPI_GRO_CB(p)->same_flow = 0;
  				continue;
  			}
  		}
  	}
  
  	skb_gro_pull(skb, grehlen);
  
  	/* Adjusted NAPI_GRO_CB(skb)->csum after skb_gro_pull()*/
  	skb_gro_postpull_rcsum(skb, greh, grehlen);
fcd91dd44   Sabrina Dubroca   net: add recursio...
210
  	pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
c194cf93c   Alexander Duyck   gro: Defer cleari...
211
  	flush = 0;
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
212
213
214
215
  
  out_unlock:
  	rcu_read_unlock();
  out:
603d4cf8f   Sabrina Dubroca   net: fix use-afte...
216
  	skb_gro_flush_final(skb, pp, flush);
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
217
218
219
  
  	return pp;
  }
d10dbad2a   Wei Yongjun   gre_offload: fix ...
220
  static int gre_gro_complete(struct sk_buff *skb, int nhoff)
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
221
222
223
224
225
226
  {
  	struct gre_base_hdr *greh = (struct gre_base_hdr *)(skb->data + nhoff);
  	struct packet_offload *ptype;
  	unsigned int grehlen = sizeof(*greh);
  	int err = -ENOENT;
  	__be16 type;
c3caf1192   Jerry Chu   net-gre-gro: Fix ...
227
228
  	skb->encapsulation = 1;
  	skb_shinfo(skb)->gso_type = SKB_GSO_GRE;
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
229
230
231
232
233
234
235
236
237
  	type = greh->protocol;
  	if (greh->flags & GRE_KEY)
  		grehlen += GRE_HEADER_SECTION;
  
  	if (greh->flags & GRE_CSUM)
  		grehlen += GRE_HEADER_SECTION;
  
  	rcu_read_lock();
  	ptype = gro_find_complete_by_type(type);
00db41243   Ian Morris   ipv4: coding styl...
238
  	if (ptype)
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
239
240
241
  		err = ptype->callbacks.gro_complete(skb, nhoff + grehlen);
  
  	rcu_read_unlock();
6fb2a7567   Tom Herbert   gre: Set inner ma...
242
243
  
  	skb_set_inner_mac_header(skb, nhoff + grehlen);
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
244
245
  	return err;
  }
c50cd3578   Daniel Borkmann   net: gre: move GS...
246
247
  static const struct net_offload gre_offload = {
  	.callbacks = {
c50cd3578   Daniel Borkmann   net: gre: move GS...
248
  		.gso_segment = gre_gso_segment,
bf5a755f5   Jerry Chu   net-gre-gro: Add ...
249
250
  		.gro_receive = gre_gro_receive,
  		.gro_complete = gre_gro_complete,
c50cd3578   Daniel Borkmann   net: gre: move GS...
251
252
  	},
  };
438e38fad   Eric Dumazet   gre_offload: stat...
253
  static int __init gre_offload_init(void)
c50cd3578   Daniel Borkmann   net: gre: move GS...
254
  {
e0c20967c   Alexander Duyck   GRE: Add support ...
255
256
257
258
259
260
261
262
263
264
265
266
267
  	int err;
  
  	err = inet_add_offload(&gre_offload, IPPROTO_GRE);
  #if IS_ENABLED(CONFIG_IPV6)
  	if (err)
  		return err;
  
  	err = inet6_add_offload(&gre_offload, IPPROTO_GRE);
  	if (err)
  		inet_del_offload(&gre_offload, IPPROTO_GRE);
  #endif
  
  	return err;
c50cd3578   Daniel Borkmann   net: gre: move GS...
268
  }
cf1722837   Paul Gortmaker   net/ipv4: don't u...
269
  device_initcall(gre_offload_init);