2 * IPV4 GSO/GRO offload support
3 * Linux INET implementation
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.
13 #include <linux/skbuff.h>
15 #include <net/protocol.h>
17 static DEFINE_SPINLOCK(udp_offload_lock
);
18 static struct udp_offload_priv __rcu
*udp_offload_base __read_mostly
;
20 #define udp_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&udp_offload_lock))
22 struct udp_offload_priv
{
23 struct udp_offload
*offload
;
26 struct udp_offload_priv __rcu
*next
;
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
)
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
;
45 if (unlikely(!pskb_may_pull(skb
, tnl_hlen
)))
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.
54 partial
= csum_sub(csum_unfold(uh
->check
),
55 (__force __wsum
)htonl(skb
->len
));
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
;
66 need_csum
= !!(skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP_TUNNEL_CSUM
);
67 skb
->encap_hdr_csum
= need_csum
;
69 remcsum
= !!(skb_shinfo(skb
)->gso_type
& SKB_GSO_TUNNEL_REMCSUM
);
70 skb
->remcsum_offload
= remcsum
;
72 ufo
= !!(skb_shinfo(skb
)->gso_type
& SKB_GSO_UDP
);
74 /* Try to offload checksum if possible */
75 offload_csum
= !!(need_csum
&&
77 (is_ipv6
? (NETIF_F_HW_CSUM
| NETIF_F_IPV6_CSUM
) :
78 (NETIF_F_HW_CSUM
| NETIF_F_IP_CSUM
))));
80 features
&= skb
->dev
->hw_enc_features
;
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.
87 features
&= ~NETIF_F_CSUM_MASK
;
88 if (!need_csum
|| offload_csum
)
89 features
|= NETIF_F_HW_CSUM
;
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
,
100 outer_hlen
= skb_tnl_header_len(skb
);
101 udp_offset
= outer_hlen
- tnl_hlen
;
107 skb
->ip_summed
= CHECKSUM_NONE
;
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;
115 skb
->mac_len
= mac_len
;
116 skb
->protocol
= protocol
;
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
);
129 uh
->check
= ~csum_fold(csum_add(partial
, (__force __wsum
)len
));
131 if (skb
->encapsulation
|| !offload_csum
) {
132 uh
->check
= gso_make_checksum(skb
, ~uh
->check
);
134 uh
->check
= CSUM_MANGLED_0
;
136 skb
->ip_summed
= CHECKSUM_PARTIAL
;
137 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
138 skb
->csum_offset
= offsetof(struct udphdr
, check
);
140 } while ((skb
= skb
->next
));
145 struct sk_buff
*skb_udp_tunnel_segment(struct sk_buff
*skb
,
146 netdev_features_t features
,
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
);
158 switch (skb
->inner_protocol_type
) {
159 case ENCAP_TYPE_ETHER
:
160 protocol
= skb
->inner_protocol
;
161 gso_inner_segment
= skb_mac_gso_segment
;
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
)
168 gso_inner_segment
= ops
->callbacks
.gso_segment
;
174 segs
= __skb_udp_tunnel_segment(skb
, features
, gso_inner_segment
,
183 static struct sk_buff
*udp4_ufo_fragment(struct sk_buff
*skb
,
184 netdev_features_t features
)
186 struct sk_buff
*segs
= ERR_PTR(-EINVAL
);
192 if (skb
->encapsulation
&&
193 (skb_shinfo(skb
)->gso_type
&
194 (SKB_GSO_UDP_TUNNEL
|SKB_GSO_UDP_TUNNEL_CSUM
))) {
195 segs
= skb_udp_tunnel_segment(skb
, features
, false);
199 if (!pskb_may_pull(skb
, sizeof(struct udphdr
)))
202 mss
= skb_shinfo(skb
)->gso_size
;
203 if (unlikely(skb
->len
<= mss
))
206 if (skb_gso_ok(skb
, features
| NETIF_F_GSO_ROBUST
)) {
207 /* Packet is from an untrusted source, reset gso_segs. */
208 int type
= skb_shinfo(skb
)->gso_type
;
210 if (unlikely(type
& ~(SKB_GSO_UDP
| SKB_GSO_DODGY
|
212 SKB_GSO_UDP_TUNNEL_CSUM
|
213 SKB_GSO_TUNNEL_REMCSUM
|
215 SKB_GSO_GRE
| SKB_GSO_GRE_CSUM
) ||
216 !(type
& (SKB_GSO_UDP
))))
219 skb_shinfo(skb
)->gso_segs
= DIV_ROUND_UP(skb
->len
, mss
);
225 /* Do software UFO. Complete and fill in the UDP checksum as
226 * HW cannot do checksum of UDP packets sent as multiple
234 csum
= skb_checksum(skb
, 0, skb
->len
, 0);
235 uh
->check
= udp_v4_check(skb
->len
, iph
->saddr
, iph
->daddr
, csum
);
237 uh
->check
= CSUM_MANGLED_0
;
239 skb
->ip_summed
= CHECKSUM_NONE
;
241 /* If there is no outer header we can fake a checksum offload
242 * due to the fact that we have already done the checksum in
243 * software prior to segmenting the frame.
245 if (!skb
->encap_hdr_csum
)
246 features
|= NETIF_F_HW_CSUM
;
248 /* Fragment the skb. IP headers of the fragments are updated in
251 segs
= skb_segment(skb
, features
);
256 int udp_add_offload(struct net
*net
, struct udp_offload
*uo
)
258 struct udp_offload_priv
*new_offload
= kzalloc(sizeof(*new_offload
), GFP_ATOMIC
);
263 write_pnet(&new_offload
->net
, net
);
264 new_offload
->offload
= uo
;
266 spin_lock(&udp_offload_lock
);
267 new_offload
->next
= udp_offload_base
;
268 rcu_assign_pointer(udp_offload_base
, new_offload
);
269 spin_unlock(&udp_offload_lock
);
273 EXPORT_SYMBOL(udp_add_offload
);
275 static void udp_offload_free_routine(struct rcu_head
*head
)
277 struct udp_offload_priv
*ou_priv
= container_of(head
, struct udp_offload_priv
, rcu
);
281 void udp_del_offload(struct udp_offload
*uo
)
283 struct udp_offload_priv __rcu
**head
= &udp_offload_base
;
284 struct udp_offload_priv
*uo_priv
;
286 spin_lock(&udp_offload_lock
);
288 uo_priv
= udp_deref_protected(*head
);
289 for (; uo_priv
!= NULL
;
290 uo_priv
= udp_deref_protected(*head
)) {
291 if (uo_priv
->offload
== uo
) {
292 rcu_assign_pointer(*head
,
293 udp_deref_protected(uo_priv
->next
));
296 head
= &uo_priv
->next
;
298 pr_warn("udp_del_offload: didn't find offload for port %d\n", ntohs(uo
->port
));
300 spin_unlock(&udp_offload_lock
);
302 call_rcu(&uo_priv
->rcu
, udp_offload_free_routine
);
304 EXPORT_SYMBOL(udp_del_offload
);
306 struct sk_buff
**udp_gro_receive(struct sk_buff
**head
, struct sk_buff
*skb
,
309 struct udp_offload_priv
*uo_priv
;
310 struct sk_buff
*p
, **pp
= NULL
;
312 unsigned int off
= skb_gro_offset(skb
);
315 if (NAPI_GRO_CB(skb
)->encap_mark
||
316 (skb
->ip_summed
!= CHECKSUM_PARTIAL
&&
317 NAPI_GRO_CB(skb
)->csum_cnt
== 0 &&
318 !NAPI_GRO_CB(skb
)->csum_valid
))
321 /* mark that this skb passed once through the tunnel gro layer */
322 NAPI_GRO_CB(skb
)->encap_mark
= 1;
325 uo_priv
= rcu_dereference(udp_offload_base
);
326 for (; uo_priv
!= NULL
; uo_priv
= rcu_dereference(uo_priv
->next
)) {
327 if (net_eq(read_pnet(&uo_priv
->net
), dev_net(skb
->dev
)) &&
328 uo_priv
->offload
->port
== uh
->dest
&&
329 uo_priv
->offload
->callbacks
.gro_receive
)
337 for (p
= *head
; p
; p
= p
->next
) {
338 if (!NAPI_GRO_CB(p
)->same_flow
)
341 uh2
= (struct udphdr
*)(p
->data
+ off
);
343 /* Match ports and either checksums are either both zero
346 if ((*(u32
*)&uh
->source
!= *(u32
*)&uh2
->source
) ||
347 (!uh
->check
^ !uh2
->check
)) {
348 NAPI_GRO_CB(p
)->same_flow
= 0;
353 skb_gro_pull(skb
, sizeof(struct udphdr
)); /* pull encapsulating udp header */
354 skb_gro_postpull_rcsum(skb
, uh
, sizeof(struct udphdr
));
355 NAPI_GRO_CB(skb
)->proto
= uo_priv
->offload
->ipproto
;
356 pp
= uo_priv
->offload
->callbacks
.gro_receive(head
, skb
,
362 NAPI_GRO_CB(skb
)->flush
|= flush
;
366 static struct sk_buff
**udp4_gro_receive(struct sk_buff
**head
,
369 struct udphdr
*uh
= udp_gro_udphdr(skb
);
374 /* Don't bother verifying checksum if we're going to flush anyway. */
375 if (NAPI_GRO_CB(skb
)->flush
)
378 if (skb_gro_checksum_validate_zero_check(skb
, IPPROTO_UDP
, uh
->check
,
379 inet_gro_compute_pseudo
))
382 skb_gro_checksum_try_convert(skb
, IPPROTO_UDP
, uh
->check
,
383 inet_gro_compute_pseudo
);
385 NAPI_GRO_CB(skb
)->is_ipv6
= 0;
386 return udp_gro_receive(head
, skb
, uh
);
389 NAPI_GRO_CB(skb
)->flush
= 1;
393 int udp_gro_complete(struct sk_buff
*skb
, int nhoff
)
395 struct udp_offload_priv
*uo_priv
;
396 __be16 newlen
= htons(skb
->len
- nhoff
);
397 struct udphdr
*uh
= (struct udphdr
*)(skb
->data
+ nhoff
);
404 uo_priv
= rcu_dereference(udp_offload_base
);
405 for (; uo_priv
!= NULL
; uo_priv
= rcu_dereference(uo_priv
->next
)) {
406 if (net_eq(read_pnet(&uo_priv
->net
), dev_net(skb
->dev
)) &&
407 uo_priv
->offload
->port
== uh
->dest
&&
408 uo_priv
->offload
->callbacks
.gro_complete
)
413 NAPI_GRO_CB(skb
)->proto
= uo_priv
->offload
->ipproto
;
414 err
= uo_priv
->offload
->callbacks
.gro_complete(skb
,
415 nhoff
+ sizeof(struct udphdr
),
421 if (skb
->remcsum_offload
)
422 skb_shinfo(skb
)->gso_type
|= SKB_GSO_TUNNEL_REMCSUM
;
424 skb
->encapsulation
= 1;
425 skb_set_inner_mac_header(skb
, nhoff
+ sizeof(struct udphdr
));
430 static int udp4_gro_complete(struct sk_buff
*skb
, int nhoff
)
432 const struct iphdr
*iph
= ip_hdr(skb
);
433 struct udphdr
*uh
= (struct udphdr
*)(skb
->data
+ nhoff
);
436 skb_shinfo(skb
)->gso_type
|= SKB_GSO_UDP_TUNNEL_CSUM
;
437 uh
->check
= ~udp_v4_check(skb
->len
- nhoff
, iph
->saddr
,
440 skb_shinfo(skb
)->gso_type
|= SKB_GSO_UDP_TUNNEL
;
443 return udp_gro_complete(skb
, nhoff
);
446 static const struct net_offload udpv4_offload
= {
448 .gso_segment
= udp4_ufo_fragment
,
449 .gro_receive
= udp4_gro_receive
,
450 .gro_complete
= udp4_gro_complete
,
454 int __init
udpv4_offload_init(void)
456 return inet_add_offload(&udpv4_offload
, IPPROTO_UDP
);