blob: 8ed460e3753ce5f691918e20a85b801fa922b61e [file] [log] [blame]
Daniel Borkmannda5bab02013-06-08 12:56:03 +02001/*
2 * IPV4 GSO/GRO offload support
3 * Linux INET implementation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * UDPv4 GSO support
11 */
12
13#include <linux/skbuff.h>
14#include <net/udp.h>
15#include <net/protocol.h>
16
Or Gerlitzb582ef02014-01-20 13:59:19 +020017static DEFINE_SPINLOCK(udp_offload_lock);
Shlomo Pongratza1d0cd82014-01-22 15:23:29 +020018static struct udp_offload_priv __rcu *udp_offload_base __read_mostly;
Or Gerlitzb582ef02014-01-20 13:59:19 +020019
Shlomo Pongratza664a4f2014-02-02 15:42:10 +020020#define udp_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&udp_offload_lock))
21
Or Gerlitzb582ef02014-01-20 13:59:19 +020022struct udp_offload_priv {
23 struct udp_offload *offload;
24 struct rcu_head rcu;
25 struct udp_offload_priv __rcu *next;
26};
27
Daniel Borkmannda5bab02013-06-08 12:56:03 +020028static int udp4_ufo_send_check(struct sk_buff *skb)
29{
30 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
31 return -EINVAL;
32
33 if (likely(!skb->encapsulation)) {
34 const struct iphdr *iph;
35 struct udphdr *uh;
36
37 iph = ip_hdr(skb);
38 uh = udp_hdr(skb);
39
40 uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
41 IPPROTO_UDP, 0);
42 skb->csum_start = skb_transport_header(skb) - skb->head;
43 skb->csum_offset = offsetof(struct udphdr, check);
44 skb->ip_summed = CHECKSUM_PARTIAL;
45 }
46
47 return 0;
48}
49
Tom Herbert155e0102014-07-13 19:49:56 -070050struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
51 netdev_features_t features)
52{
53 struct sk_buff *segs = ERR_PTR(-EINVAL);
54 u16 mac_offset = skb->mac_header;
55 int mac_len = skb->mac_len;
56 int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
57 __be16 protocol = skb->protocol;
58 netdev_features_t enc_features;
59 int udp_offset, outer_hlen;
60 unsigned int oldlen;
61 bool need_csum;
62
63 oldlen = (u16)~skb->len;
64
65 if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
66 goto out;
67
68 skb->encapsulation = 0;
69 __skb_pull(skb, tnl_hlen);
70 skb_reset_mac_header(skb);
71 skb_set_network_header(skb, skb_inner_network_offset(skb));
72 skb->mac_len = skb_inner_network_offset(skb);
73 skb->protocol = htons(ETH_P_TEB);
74
75 need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM);
76 if (need_csum)
77 skb->encap_hdr_csum = 1;
78
79 /* segment inner packet. */
80 enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
81 segs = skb_mac_gso_segment(skb, enc_features);
Himangi Saraogi27446442014-07-27 12:38:38 +053082 if (IS_ERR_OR_NULL(segs)) {
Tom Herbert155e0102014-07-13 19:49:56 -070083 skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
84 mac_len);
85 goto out;
86 }
87
88 outer_hlen = skb_tnl_header_len(skb);
89 udp_offset = outer_hlen - tnl_hlen;
90 skb = segs;
91 do {
92 struct udphdr *uh;
93 int len;
94
95 skb_reset_inner_headers(skb);
96 skb->encapsulation = 1;
97
98 skb->mac_len = mac_len;
99
100 skb_push(skb, outer_hlen);
101 skb_reset_mac_header(skb);
102 skb_set_network_header(skb, mac_len);
103 skb_set_transport_header(skb, udp_offset);
104 len = skb->len - udp_offset;
105 uh = udp_hdr(skb);
106 uh->len = htons(len);
107
108 if (need_csum) {
109 __be32 delta = htonl(oldlen + len);
110
111 uh->check = ~csum_fold((__force __wsum)
112 ((__force u32)uh->check +
113 (__force u32)delta));
114 uh->check = gso_make_checksum(skb, ~uh->check);
115
116 if (uh->check == 0)
117 uh->check = CSUM_MANGLED_0;
118 }
119
120 skb->protocol = protocol;
121 } while ((skb = skb->next));
122out:
123 return segs;
124}
125
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200126static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
127 netdev_features_t features)
128{
129 struct sk_buff *segs = ERR_PTR(-EINVAL);
130 unsigned int mss;
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800131 int offset;
132 __wsum csum;
133
134 if (skb->encapsulation &&
Tom Herbert0f4f4ff2014-06-04 17:20:16 -0700135 (skb_shinfo(skb)->gso_type &
136 (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800137 segs = skb_udp_tunnel_segment(skb, features);
138 goto out;
139 }
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200140
141 mss = skb_shinfo(skb)->gso_size;
142 if (unlikely(skb->len <= mss))
143 goto out;
144
145 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
146 /* Packet is from an untrusted source, reset gso_segs. */
147 int type = skb_shinfo(skb)->gso_type;
148
149 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
150 SKB_GSO_UDP_TUNNEL |
Tom Herbert0f4f4ff2014-06-04 17:20:16 -0700151 SKB_GSO_UDP_TUNNEL_CSUM |
Eric Dumazetcb32f512013-10-19 11:42:57 -0700152 SKB_GSO_IPIP |
Tom Herbert4749c092014-06-04 17:20:23 -0700153 SKB_GSO_GRE | SKB_GSO_GRE_CSUM |
154 SKB_GSO_MPLS) ||
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200155 !(type & (SKB_GSO_UDP))))
156 goto out;
157
158 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
159
160 segs = NULL;
161 goto out;
162 }
163
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800164 /* Do software UFO. Complete and fill in the UDP checksum as
165 * HW cannot do checksum of UDP packets sent as multiple
166 * IP fragments.
167 */
168 offset = skb_checksum_start_offset(skb);
169 csum = skb_checksum(skb, offset, skb->len - offset, 0);
170 offset += skb->csum_offset;
171 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
172 skb->ip_summed = CHECKSUM_NONE;
173
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200174 /* Fragment the skb. IP headers of the fragments are updated in
175 * inet_gso_segment()
176 */
Wei-Chun Chao7a7ffba2013-12-26 13:10:22 -0800177 segs = skb_segment(skb, features);
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200178out:
179 return segs;
180}
181
Or Gerlitzb582ef02014-01-20 13:59:19 +0200182int udp_add_offload(struct udp_offload *uo)
183{
Or Gerlitzb5aaab12014-01-29 18:08:59 +0200184 struct udp_offload_priv *new_offload = kzalloc(sizeof(*new_offload), GFP_ATOMIC);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200185
186 if (!new_offload)
187 return -ENOMEM;
188
189 new_offload->offload = uo;
190
191 spin_lock(&udp_offload_lock);
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200192 new_offload->next = udp_offload_base;
193 rcu_assign_pointer(udp_offload_base, new_offload);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200194 spin_unlock(&udp_offload_lock);
195
196 return 0;
197}
198EXPORT_SYMBOL(udp_add_offload);
199
200static void udp_offload_free_routine(struct rcu_head *head)
201{
202 struct udp_offload_priv *ou_priv = container_of(head, struct udp_offload_priv, rcu);
203 kfree(ou_priv);
204}
205
206void udp_del_offload(struct udp_offload *uo)
207{
208 struct udp_offload_priv __rcu **head = &udp_offload_base;
209 struct udp_offload_priv *uo_priv;
210
211 spin_lock(&udp_offload_lock);
212
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200213 uo_priv = udp_deref_protected(*head);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200214 for (; uo_priv != NULL;
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200215 uo_priv = udp_deref_protected(*head)) {
Or Gerlitzb582ef02014-01-20 13:59:19 +0200216 if (uo_priv->offload == uo) {
Shlomo Pongratza664a4f2014-02-02 15:42:10 +0200217 rcu_assign_pointer(*head,
218 udp_deref_protected(uo_priv->next));
Or Gerlitzb582ef02014-01-20 13:59:19 +0200219 goto unlock;
220 }
221 head = &uo_priv->next;
222 }
Shlomo Pongratza1d0cd82014-01-22 15:23:29 +0200223 pr_warn("udp_del_offload: didn't find offload for port %d\n", ntohs(uo->port));
Or Gerlitzb582ef02014-01-20 13:59:19 +0200224unlock:
225 spin_unlock(&udp_offload_lock);
226 if (uo_priv != NULL)
227 call_rcu(&uo_priv->rcu, udp_offload_free_routine);
228}
229EXPORT_SYMBOL(udp_del_offload);
230
Tom Herbert57c67ff2014-08-22 13:34:44 -0700231struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
232 struct udphdr *uh)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200233{
234 struct udp_offload_priv *uo_priv;
235 struct sk_buff *p, **pp = NULL;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700236 struct udphdr *uh2;
237 unsigned int off = skb_gro_offset(skb);
Or Gerlitzb582ef02014-01-20 13:59:19 +0200238 int flush = 1;
239
240 if (NAPI_GRO_CB(skb)->udp_mark ||
Tom Herbert57c67ff2014-08-22 13:34:44 -0700241 (!skb->encapsulation && !NAPI_GRO_CB(skb)->csum_valid))
Or Gerlitzb582ef02014-01-20 13:59:19 +0200242 goto out;
243
244 /* mark that this skb passed once through the udp gro layer */
245 NAPI_GRO_CB(skb)->udp_mark = 1;
Tom Herbert57c67ff2014-08-22 13:34:44 -0700246 NAPI_GRO_CB(skb)->encapsulation++;
Or Gerlitzb582ef02014-01-20 13:59:19 +0200247
248 rcu_read_lock();
249 uo_priv = rcu_dereference(udp_offload_base);
250 for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
251 if (uo_priv->offload->port == uh->dest &&
252 uo_priv->offload->callbacks.gro_receive)
253 goto unflush;
254 }
255 goto out_unlock;
256
257unflush:
258 flush = 0;
259
260 for (p = *head; p; p = p->next) {
261 if (!NAPI_GRO_CB(p)->same_flow)
262 continue;
263
264 uh2 = (struct udphdr *)(p->data + off);
Tom Herbert57c67ff2014-08-22 13:34:44 -0700265
266 /* Match ports and either checksums are either both zero
267 * or nonzero.
268 */
269 if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
270 (!uh->check ^ !uh2->check)) {
Or Gerlitzb582ef02014-01-20 13:59:19 +0200271 NAPI_GRO_CB(p)->same_flow = 0;
272 continue;
273 }
274 }
275
276 skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
Tom Herbert6bae1d42014-06-10 18:54:26 -0700277 skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
Or Gerlitzb582ef02014-01-20 13:59:19 +0200278 pp = uo_priv->offload->callbacks.gro_receive(head, skb);
279
280out_unlock:
281 rcu_read_unlock();
282out:
283 NAPI_GRO_CB(skb)->flush |= flush;
284 return pp;
285}
286
Tom Herbert57c67ff2014-08-22 13:34:44 -0700287static struct sk_buff **udp4_gro_receive(struct sk_buff **head,
288 struct sk_buff *skb)
289{
290 struct udphdr *uh = udp_gro_udphdr(skb);
291
292 /* Don't bother verifying checksum if we're going to flush anyway. */
293 if (unlikely(!uh) ||
294 (!NAPI_GRO_CB(skb)->flush &&
295 skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
296 inet_gro_compute_pseudo))) {
297 NAPI_GRO_CB(skb)->flush = 1;
298 return NULL;
299 }
300
301 return udp_gro_receive(head, skb, uh);
302}
303
304int udp_gro_complete(struct sk_buff *skb, int nhoff)
Or Gerlitzb582ef02014-01-20 13:59:19 +0200305{
306 struct udp_offload_priv *uo_priv;
307 __be16 newlen = htons(skb->len - nhoff);
308 struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
309 int err = -ENOSYS;
310
311 uh->len = newlen;
312
313 rcu_read_lock();
314
315 uo_priv = rcu_dereference(udp_offload_base);
316 for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) {
317 if (uo_priv->offload->port == uh->dest &&
318 uo_priv->offload->callbacks.gro_complete)
319 break;
320 }
321
322 if (uo_priv != NULL)
323 err = uo_priv->offload->callbacks.gro_complete(skb, nhoff + sizeof(struct udphdr));
324
325 rcu_read_unlock();
326 return err;
327}
328
Tom Herbert57c67ff2014-08-22 13:34:44 -0700329int udp4_gro_complete(struct sk_buff *skb, int nhoff)
330{
331 const struct iphdr *iph = ip_hdr(skb);
332 struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
333
334 if (uh->check)
335 uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
336 iph->daddr, 0);
337
338 return udp_gro_complete(skb, nhoff);
339}
340
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200341static const struct net_offload udpv4_offload = {
342 .callbacks = {
343 .gso_send_check = udp4_ufo_send_check,
344 .gso_segment = udp4_ufo_fragment,
Tom Herbert57c67ff2014-08-22 13:34:44 -0700345 .gro_receive = udp4_gro_receive,
346 .gro_complete = udp4_gro_complete,
Daniel Borkmannda5bab02013-06-08 12:56:03 +0200347 },
348};
349
350int __init udpv4_offload_init(void)
351{
352 return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
353}