Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 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 | |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 17 | static DEFINE_SPINLOCK(udp_offload_lock); |
Shlomo Pongratz | a1d0cd8 | 2014-01-22 15:23:29 +0200 | [diff] [blame] | 18 | static struct udp_offload_priv __rcu *udp_offload_base __read_mostly; |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 19 | |
Shlomo Pongratz | a664a4f | 2014-02-02 15:42:10 +0200 | [diff] [blame] | 20 | #define udp_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&udp_offload_lock)) |
| 21 | |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 22 | struct udp_offload_priv { |
| 23 | struct udp_offload *offload; |
| 24 | struct rcu_head rcu; |
| 25 | struct udp_offload_priv __rcu *next; |
| 26 | }; |
| 27 | |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 28 | static 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 Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 50 | struct 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 Saraogi | 2744644 | 2014-07-27 12:38:38 +0530 | [diff] [blame] | 82 | if (IS_ERR_OR_NULL(segs)) { |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 83 | 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)); |
| 122 | out: |
| 123 | return segs; |
| 124 | } |
| 125 | |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 126 | static 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 Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 131 | int offset; |
| 132 | __wsum csum; |
| 133 | |
| 134 | if (skb->encapsulation && |
Tom Herbert | 0f4f4ff | 2014-06-04 17:20:16 -0700 | [diff] [blame] | 135 | (skb_shinfo(skb)->gso_type & |
| 136 | (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) { |
Wei-Chun Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 137 | segs = skb_udp_tunnel_segment(skb, features); |
| 138 | goto out; |
| 139 | } |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 140 | |
| 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 Herbert | 0f4f4ff | 2014-06-04 17:20:16 -0700 | [diff] [blame] | 151 | SKB_GSO_UDP_TUNNEL_CSUM | |
Eric Dumazet | cb32f51 | 2013-10-19 11:42:57 -0700 | [diff] [blame] | 152 | SKB_GSO_IPIP | |
Tom Herbert | 4749c09 | 2014-06-04 17:20:23 -0700 | [diff] [blame] | 153 | SKB_GSO_GRE | SKB_GSO_GRE_CSUM | |
| 154 | SKB_GSO_MPLS) || |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 155 | !(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 Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 164 | /* 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 Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 174 | /* Fragment the skb. IP headers of the fragments are updated in |
| 175 | * inet_gso_segment() |
| 176 | */ |
Wei-Chun Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 177 | segs = skb_segment(skb, features); |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 178 | out: |
| 179 | return segs; |
| 180 | } |
| 181 | |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 182 | int udp_add_offload(struct udp_offload *uo) |
| 183 | { |
Or Gerlitz | b5aaab1 | 2014-01-29 18:08:59 +0200 | [diff] [blame] | 184 | struct udp_offload_priv *new_offload = kzalloc(sizeof(*new_offload), GFP_ATOMIC); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 185 | |
| 186 | if (!new_offload) |
| 187 | return -ENOMEM; |
| 188 | |
| 189 | new_offload->offload = uo; |
| 190 | |
| 191 | spin_lock(&udp_offload_lock); |
Shlomo Pongratz | a664a4f | 2014-02-02 15:42:10 +0200 | [diff] [blame] | 192 | new_offload->next = udp_offload_base; |
| 193 | rcu_assign_pointer(udp_offload_base, new_offload); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 194 | spin_unlock(&udp_offload_lock); |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | EXPORT_SYMBOL(udp_add_offload); |
| 199 | |
| 200 | static 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 | |
| 206 | void 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 Pongratz | a664a4f | 2014-02-02 15:42:10 +0200 | [diff] [blame] | 213 | uo_priv = udp_deref_protected(*head); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 214 | for (; uo_priv != NULL; |
Shlomo Pongratz | a664a4f | 2014-02-02 15:42:10 +0200 | [diff] [blame] | 215 | uo_priv = udp_deref_protected(*head)) { |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 216 | if (uo_priv->offload == uo) { |
Shlomo Pongratz | a664a4f | 2014-02-02 15:42:10 +0200 | [diff] [blame] | 217 | rcu_assign_pointer(*head, |
| 218 | udp_deref_protected(uo_priv->next)); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 219 | goto unlock; |
| 220 | } |
| 221 | head = &uo_priv->next; |
| 222 | } |
Shlomo Pongratz | a1d0cd8 | 2014-01-22 15:23:29 +0200 | [diff] [blame] | 223 | pr_warn("udp_del_offload: didn't find offload for port %d\n", ntohs(uo->port)); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 224 | unlock: |
| 225 | spin_unlock(&udp_offload_lock); |
| 226 | if (uo_priv != NULL) |
| 227 | call_rcu(&uo_priv->rcu, udp_offload_free_routine); |
| 228 | } |
| 229 | EXPORT_SYMBOL(udp_del_offload); |
| 230 | |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame^] | 231 | struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb, |
| 232 | struct udphdr *uh) |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 233 | { |
| 234 | struct udp_offload_priv *uo_priv; |
| 235 | struct sk_buff *p, **pp = NULL; |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame^] | 236 | struct udphdr *uh2; |
| 237 | unsigned int off = skb_gro_offset(skb); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 238 | int flush = 1; |
| 239 | |
| 240 | if (NAPI_GRO_CB(skb)->udp_mark || |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame^] | 241 | (!skb->encapsulation && !NAPI_GRO_CB(skb)->csum_valid)) |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 242 | goto out; |
| 243 | |
| 244 | /* mark that this skb passed once through the udp gro layer */ |
| 245 | NAPI_GRO_CB(skb)->udp_mark = 1; |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame^] | 246 | NAPI_GRO_CB(skb)->encapsulation++; |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 247 | |
| 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 | |
| 257 | unflush: |
| 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 Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame^] | 265 | |
| 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 Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 271 | 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 Herbert | 6bae1d4 | 2014-06-10 18:54:26 -0700 | [diff] [blame] | 277 | skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr)); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 278 | pp = uo_priv->offload->callbacks.gro_receive(head, skb); |
| 279 | |
| 280 | out_unlock: |
| 281 | rcu_read_unlock(); |
| 282 | out: |
| 283 | NAPI_GRO_CB(skb)->flush |= flush; |
| 284 | return pp; |
| 285 | } |
| 286 | |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame^] | 287 | static 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 | |
| 304 | int udp_gro_complete(struct sk_buff *skb, int nhoff) |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 305 | { |
| 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 Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame^] | 329 | int 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 Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 341 | static const struct net_offload udpv4_offload = { |
| 342 | .callbacks = { |
| 343 | .gso_send_check = udp4_ufo_send_check, |
| 344 | .gso_segment = udp4_ufo_fragment, |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame^] | 345 | .gro_receive = udp4_gro_receive, |
| 346 | .gro_complete = udp4_gro_complete, |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 347 | }, |
| 348 | }; |
| 349 | |
| 350 | int __init udpv4_offload_init(void) |
| 351 | { |
| 352 | return inet_add_offload(&udpv4_offload, IPPROTO_UDP); |
| 353 | } |