udp: Add GRO functions to UDP socket
[cascardo/linux.git] / net / ipv4 / udp_offload.c
1 /*
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
17 static DEFINE_SPINLOCK(udp_offload_lock);
18 static struct udp_offload_priv __rcu *udp_offload_base __read_mostly;
19
20 #define udp_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&udp_offload_lock))
21
22 struct udp_offload_priv {
23         struct udp_offload      *offload;
24         possible_net_t  net;
25         struct rcu_head         rcu;
26         struct udp_offload_priv __rcu *next;
27 };
28
29 static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb,
30         netdev_features_t features,
31         struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
32                                              netdev_features_t features),
33         __be16 new_protocol, bool is_ipv6)
34 {
35         int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
36         bool remcsum, need_csum, offload_csum, ufo;
37         struct sk_buff *segs = ERR_PTR(-EINVAL);
38         struct udphdr *uh = udp_hdr(skb);
39         u16 mac_offset = skb->mac_header;
40         __be16 protocol = skb->protocol;
41         u16 mac_len = skb->mac_len;
42         int udp_offset, outer_hlen;
43         __wsum partial;
44
45         if (unlikely(!pskb_may_pull(skb, tnl_hlen)))
46                 goto out;
47
48         /* Adjust partial header checksum to negate old length.
49          * We cannot rely on the value contained in uh->len as it is
50          * possible that the actual value exceeds the boundaries of the
51          * 16 bit length field due to the header being added outside of an
52          * IP or IPv6 frame that was already limited to 64K - 1.
53          */
54         partial = csum_sub(csum_unfold(uh->check),
55                            (__force __wsum)htonl(skb->len));
56
57         /* setup inner skb. */
58         skb->encapsulation = 0;
59         SKB_GSO_CB(skb)->encap_level = 0;
60         __skb_pull(skb, tnl_hlen);
61         skb_reset_mac_header(skb);
62         skb_set_network_header(skb, skb_inner_network_offset(skb));
63         skb->mac_len = skb_inner_network_offset(skb);
64         skb->protocol = new_protocol;
65
66         need_csum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL_CSUM);
67         skb->encap_hdr_csum = need_csum;
68
69         remcsum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TUNNEL_REMCSUM);
70         skb->remcsum_offload = remcsum;
71
72         ufo = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
73
74         /* Try to offload checksum if possible */
75         offload_csum = !!(need_csum &&
76                           (skb->dev->features &
77                            (is_ipv6 ? (NETIF_F_HW_CSUM | NETIF_F_IPV6_CSUM) :
78                                       (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM))));
79
80         features &= skb->dev->hw_enc_features;
81
82         /* The only checksum offload we care about from here on out is the
83          * outer one so strip the existing checksum feature flags and
84          * instead set the flag based on our outer checksum offload value.
85          */
86         if (remcsum || ufo) {
87                 features &= ~NETIF_F_CSUM_MASK;
88                 if (!need_csum || offload_csum)
89                         features |= NETIF_F_HW_CSUM;
90         }
91
92         /* segment inner packet. */
93         segs = gso_inner_segment(skb, features);
94         if (IS_ERR_OR_NULL(segs)) {
95                 skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
96                                      mac_len);
97                 goto out;
98         }
99
100         outer_hlen = skb_tnl_header_len(skb);
101         udp_offset = outer_hlen - tnl_hlen;
102         skb = segs;
103         do {
104                 __be16 len;
105
106                 if (remcsum)
107                         skb->ip_summed = CHECKSUM_NONE;
108
109                 /* Set up inner headers if we are offloading inner checksum */
110                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
111                         skb_reset_inner_headers(skb);
112                         skb->encapsulation = 1;
113                 }
114
115                 skb->mac_len = mac_len;
116                 skb->protocol = protocol;
117
118                 __skb_push(skb, outer_hlen);
119                 skb_reset_mac_header(skb);
120                 skb_set_network_header(skb, mac_len);
121                 skb_set_transport_header(skb, udp_offset);
122                 len = htons(skb->len - udp_offset);
123                 uh = udp_hdr(skb);
124                 uh->len = len;
125
126                 if (!need_csum)
127                         continue;
128
129                 uh->check = ~csum_fold(csum_add(partial, (__force __wsum)len));
130
131                 if (skb->encapsulation || !offload_csum) {
132                         uh->check = gso_make_checksum(skb, ~uh->check);
133                         if (uh->check == 0)
134                                 uh->check = CSUM_MANGLED_0;
135                 } else {
136                         skb->ip_summed = CHECKSUM_PARTIAL;
137                         skb->csum_start = skb_transport_header(skb) - skb->head;
138                         skb->csum_offset = offsetof(struct udphdr, check);
139                 }
140         } while ((skb = skb->next));
141 out:
142         return segs;
143 }
144
145 struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
146                                        netdev_features_t features,
147                                        bool is_ipv6)
148 {
149         __be16 protocol = skb->protocol;
150         const struct net_offload **offloads;
151         const struct net_offload *ops;
152         struct sk_buff *segs = ERR_PTR(-EINVAL);
153         struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb,
154                                              netdev_features_t features);
155
156         rcu_read_lock();
157
158         switch (skb->inner_protocol_type) {
159         case ENCAP_TYPE_ETHER:
160                 protocol = skb->inner_protocol;
161                 gso_inner_segment = skb_mac_gso_segment;
162                 break;
163         case ENCAP_TYPE_IPPROTO:
164                 offloads = is_ipv6 ? inet6_offloads : inet_offloads;
165                 ops = rcu_dereference(offloads[skb->inner_ipproto]);
166                 if (!ops || !ops->callbacks.gso_segment)
167                         goto out_unlock;
168                 gso_inner_segment = ops->callbacks.gso_segment;
169                 break;
170         default:
171                 goto out_unlock;
172         }
173
174         segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment,
175                                         protocol, is_ipv6);
176
177 out_unlock:
178         rcu_read_unlock();
179
180         return segs;
181 }
182 EXPORT_SYMBOL(skb_udp_tunnel_segment);
183
184 static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
185                                          netdev_features_t features)
186 {
187         struct sk_buff *segs = ERR_PTR(-EINVAL);
188         unsigned int mss;
189         __wsum csum;
190         struct udphdr *uh;
191         struct iphdr *iph;
192
193         if (skb->encapsulation &&
194             (skb_shinfo(skb)->gso_type &
195              (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) {
196                 segs = skb_udp_tunnel_segment(skb, features, false);
197                 goto out;
198         }
199
200         if (!pskb_may_pull(skb, sizeof(struct udphdr)))
201                 goto out;
202
203         mss = skb_shinfo(skb)->gso_size;
204         if (unlikely(skb->len <= mss))
205                 goto out;
206
207         if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
208                 /* Packet is from an untrusted source, reset gso_segs. */
209                 int type = skb_shinfo(skb)->gso_type;
210
211                 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
212                                       SKB_GSO_UDP_TUNNEL |
213                                       SKB_GSO_UDP_TUNNEL_CSUM |
214                                       SKB_GSO_TUNNEL_REMCSUM |
215                                       SKB_GSO_IPIP |
216                                       SKB_GSO_GRE | SKB_GSO_GRE_CSUM) ||
217                              !(type & (SKB_GSO_UDP))))
218                         goto out;
219
220                 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
221
222                 segs = NULL;
223                 goto out;
224         }
225
226         /* Do software UFO. Complete and fill in the UDP checksum as
227          * HW cannot do checksum of UDP packets sent as multiple
228          * IP fragments.
229          */
230
231         uh = udp_hdr(skb);
232         iph = ip_hdr(skb);
233
234         uh->check = 0;
235         csum = skb_checksum(skb, 0, skb->len, 0);
236         uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum);
237         if (uh->check == 0)
238                 uh->check = CSUM_MANGLED_0;
239
240         skb->ip_summed = CHECKSUM_NONE;
241
242         /* If there is no outer header we can fake a checksum offload
243          * due to the fact that we have already done the checksum in
244          * software prior to segmenting the frame.
245          */
246         if (!skb->encap_hdr_csum)
247                 features |= NETIF_F_HW_CSUM;
248
249         /* Fragment the skb. IP headers of the fragments are updated in
250          * inet_gso_segment()
251          */
252         segs = skb_segment(skb, features);
253 out:
254         return segs;
255 }
256
257 int udp_add_offload(struct net *net, struct udp_offload *uo)
258 {
259         struct udp_offload_priv *new_offload = kzalloc(sizeof(*new_offload), GFP_ATOMIC);
260
261         if (!new_offload)
262                 return -ENOMEM;
263
264         write_pnet(&new_offload->net, net);
265         new_offload->offload = uo;
266
267         spin_lock(&udp_offload_lock);
268         new_offload->next = udp_offload_base;
269         rcu_assign_pointer(udp_offload_base, new_offload);
270         spin_unlock(&udp_offload_lock);
271
272         return 0;
273 }
274 EXPORT_SYMBOL(udp_add_offload);
275
276 static void udp_offload_free_routine(struct rcu_head *head)
277 {
278         struct udp_offload_priv *ou_priv = container_of(head, struct udp_offload_priv, rcu);
279         kfree(ou_priv);
280 }
281
282 void udp_del_offload(struct udp_offload *uo)
283 {
284         struct udp_offload_priv __rcu **head = &udp_offload_base;
285         struct udp_offload_priv *uo_priv;
286
287         spin_lock(&udp_offload_lock);
288
289         uo_priv = udp_deref_protected(*head);
290         for (; uo_priv != NULL;
291              uo_priv = udp_deref_protected(*head)) {
292                 if (uo_priv->offload == uo) {
293                         rcu_assign_pointer(*head,
294                                            udp_deref_protected(uo_priv->next));
295                         goto unlock;
296                 }
297                 head = &uo_priv->next;
298         }
299         pr_warn("udp_del_offload: didn't find offload for port %d\n", ntohs(uo->port));
300 unlock:
301         spin_unlock(&udp_offload_lock);
302         if (uo_priv)
303                 call_rcu(&uo_priv->rcu, udp_offload_free_routine);
304 }
305 EXPORT_SYMBOL(udp_del_offload);
306
307 struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb,
308                                  struct udphdr *uh, udp_lookup_t lookup)
309 {
310         struct sk_buff *p, **pp = NULL;
311         struct udphdr *uh2;
312         unsigned int off = skb_gro_offset(skb);
313         int flush = 1;
314         struct sock *sk;
315
316         if (NAPI_GRO_CB(skb)->encap_mark ||
317             (skb->ip_summed != CHECKSUM_PARTIAL &&
318              NAPI_GRO_CB(skb)->csum_cnt == 0 &&
319              !NAPI_GRO_CB(skb)->csum_valid))
320                 goto out;
321
322         /* mark that this skb passed once through the tunnel gro layer */
323         NAPI_GRO_CB(skb)->encap_mark = 1;
324
325         rcu_read_lock();
326         sk = (*lookup)(skb, uh->source, uh->dest);
327
328         if (sk && udp_sk(sk)->gro_receive)
329                 goto unflush;
330
331         goto out_unlock;
332
333 unflush:
334         flush = 0;
335
336         for (p = *head; p; p = p->next) {
337                 if (!NAPI_GRO_CB(p)->same_flow)
338                         continue;
339
340                 uh2 = (struct udphdr   *)(p->data + off);
341
342                 /* Match ports and either checksums are either both zero
343                  * or nonzero.
344                  */
345                 if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) ||
346                     (!uh->check ^ !uh2->check)) {
347                         NAPI_GRO_CB(p)->same_flow = 0;
348                         continue;
349                 }
350         }
351
352         skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */
353         skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr));
354         pp = udp_sk(sk)->gro_receive(sk, head, skb);
355
356 out_unlock:
357         rcu_read_unlock();
358 out:
359         NAPI_GRO_CB(skb)->flush |= flush;
360         return pp;
361 }
362 EXPORT_SYMBOL(udp_gro_receive);
363
364 static struct sk_buff **udp4_gro_receive(struct sk_buff **head,
365                                          struct sk_buff *skb)
366 {
367         struct udphdr *uh = udp_gro_udphdr(skb);
368
369         if (unlikely(!uh))
370                 goto flush;
371
372         /* Don't bother verifying checksum if we're going to flush anyway. */
373         if (NAPI_GRO_CB(skb)->flush)
374                 goto skip;
375
376         if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
377                                                  inet_gro_compute_pseudo))
378                 goto flush;
379         else if (uh->check)
380                 skb_gro_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
381                                              inet_gro_compute_pseudo);
382 skip:
383         NAPI_GRO_CB(skb)->is_ipv6 = 0;
384         return udp_gro_receive(head, skb, uh, udp4_lib_lookup_skb);
385
386 flush:
387         NAPI_GRO_CB(skb)->flush = 1;
388         return NULL;
389 }
390
391 int udp_gro_complete(struct sk_buff *skb, int nhoff,
392                      udp_lookup_t lookup)
393 {
394         __be16 newlen = htons(skb->len - nhoff);
395         struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
396         int err = -ENOSYS;
397         struct sock *sk;
398
399         uh->len = newlen;
400
401         rcu_read_lock();
402         sk = (*lookup)(skb, uh->source, uh->dest);
403         if (sk && udp_sk(sk)->gro_complete)
404                 err = udp_sk(sk)->gro_complete(sk, skb,
405                                 nhoff + sizeof(struct udphdr));
406         rcu_read_unlock();
407
408         if (skb->remcsum_offload)
409                 skb_shinfo(skb)->gso_type |= SKB_GSO_TUNNEL_REMCSUM;
410
411         skb->encapsulation = 1;
412         skb_set_inner_mac_header(skb, nhoff + sizeof(struct udphdr));
413
414         return err;
415 }
416 EXPORT_SYMBOL(udp_gro_complete);
417
418 static int udp4_gro_complete(struct sk_buff *skb, int nhoff)
419 {
420         const struct iphdr *iph = ip_hdr(skb);
421         struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
422
423         if (uh->check) {
424                 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
425                 uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr,
426                                           iph->daddr, 0);
427         } else {
428                 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
429         }
430
431         return udp_gro_complete(skb, nhoff, udp4_lib_lookup_skb);
432 }
433
434 static const struct net_offload udpv4_offload = {
435         .callbacks = {
436                 .gso_segment = udp4_ufo_fragment,
437                 .gro_receive  = udp4_gro_receive,
438                 .gro_complete = udp4_gro_complete,
439         },
440 };
441
442 int __init udpv4_offload_init(void)
443 {
444         return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
445 }