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 | |
Tom Herbert | 8bce6d7 | 2014-09-29 20:22:29 -0700 | [diff] [blame] | 28 | static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb, |
| 29 | netdev_features_t features, |
| 30 | struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb, |
| 31 | netdev_features_t features), |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 32 | __be16 new_protocol, bool is_ipv6) |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 33 | { |
| 34 | struct sk_buff *segs = ERR_PTR(-EINVAL); |
| 35 | u16 mac_offset = skb->mac_header; |
| 36 | int mac_len = skb->mac_len; |
| 37 | int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb); |
| 38 | __be16 protocol = skb->protocol; |
| 39 | netdev_features_t enc_features; |
| 40 | int udp_offset, outer_hlen; |
| 41 | unsigned int oldlen; |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 42 | bool need_csum = !!(skb_shinfo(skb)->gso_type & |
| 43 | SKB_GSO_UDP_TUNNEL_CSUM); |
Tom Herbert | e585f23 | 2014-11-04 09:06:54 -0800 | [diff] [blame] | 44 | bool remcsum = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TUNNEL_REMCSUM); |
| 45 | bool offload_csum = false, dont_encap = (need_csum || remcsum); |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 46 | |
| 47 | oldlen = (u16)~skb->len; |
| 48 | |
| 49 | if (unlikely(!pskb_may_pull(skb, tnl_hlen))) |
| 50 | goto out; |
| 51 | |
| 52 | skb->encapsulation = 0; |
| 53 | __skb_pull(skb, tnl_hlen); |
| 54 | skb_reset_mac_header(skb); |
| 55 | skb_set_network_header(skb, skb_inner_network_offset(skb)); |
| 56 | skb->mac_len = skb_inner_network_offset(skb); |
Tom Herbert | 8bce6d7 | 2014-09-29 20:22:29 -0700 | [diff] [blame] | 57 | skb->protocol = new_protocol; |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 58 | skb->encap_hdr_csum = need_csum; |
Tom Herbert | e585f23 | 2014-11-04 09:06:54 -0800 | [diff] [blame] | 59 | skb->remcsum_offload = remcsum; |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 60 | |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 61 | /* Try to offload checksum if possible */ |
| 62 | offload_csum = !!(need_csum && |
| 63 | (skb->dev->features & |
| 64 | (is_ipv6 ? NETIF_F_V6_CSUM : NETIF_F_V4_CSUM))); |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 65 | |
| 66 | /* segment inner packet. */ |
Florian Westphal | 1e16aa3 | 2014-10-20 13:49:16 +0200 | [diff] [blame] | 67 | enc_features = skb->dev->hw_enc_features & features; |
Tom Herbert | 8bce6d7 | 2014-09-29 20:22:29 -0700 | [diff] [blame] | 68 | segs = gso_inner_segment(skb, enc_features); |
Himangi Saraogi | 2744644 | 2014-07-27 12:38:38 +0530 | [diff] [blame] | 69 | if (IS_ERR_OR_NULL(segs)) { |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 70 | skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset, |
| 71 | mac_len); |
| 72 | goto out; |
| 73 | } |
| 74 | |
| 75 | outer_hlen = skb_tnl_header_len(skb); |
| 76 | udp_offset = outer_hlen - tnl_hlen; |
| 77 | skb = segs; |
| 78 | do { |
| 79 | struct udphdr *uh; |
| 80 | int len; |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 81 | __be32 delta; |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 82 | |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 83 | if (dont_encap) { |
| 84 | skb->encapsulation = 0; |
| 85 | skb->ip_summed = CHECKSUM_NONE; |
| 86 | } else { |
| 87 | /* Only set up inner headers if we might be offloading |
| 88 | * inner checksum. |
| 89 | */ |
| 90 | skb_reset_inner_headers(skb); |
| 91 | skb->encapsulation = 1; |
| 92 | } |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 93 | |
| 94 | skb->mac_len = mac_len; |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 95 | skb->protocol = protocol; |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 96 | |
| 97 | skb_push(skb, outer_hlen); |
| 98 | skb_reset_mac_header(skb); |
| 99 | skb_set_network_header(skb, mac_len); |
| 100 | skb_set_transport_header(skb, udp_offset); |
| 101 | len = skb->len - udp_offset; |
| 102 | uh = udp_hdr(skb); |
| 103 | uh->len = htons(len); |
| 104 | |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 105 | if (!need_csum) |
| 106 | continue; |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 107 | |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 108 | delta = htonl(oldlen + len); |
| 109 | |
| 110 | uh->check = ~csum_fold((__force __wsum) |
| 111 | ((__force u32)uh->check + |
| 112 | (__force u32)delta)); |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 113 | if (offload_csum) { |
| 114 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 115 | skb->csum_start = skb_transport_header(skb) - skb->head; |
| 116 | skb->csum_offset = offsetof(struct udphdr, check); |
Tom Herbert | e585f23 | 2014-11-04 09:06:54 -0800 | [diff] [blame] | 117 | } else if (remcsum) { |
| 118 | /* Need to calculate checksum from scratch, |
| 119 | * inner checksums are never when doing |
| 120 | * remote_checksum_offload. |
| 121 | */ |
| 122 | |
| 123 | skb->csum = skb_checksum(skb, udp_offset, |
| 124 | skb->len - udp_offset, |
| 125 | 0); |
| 126 | uh->check = csum_fold(skb->csum); |
| 127 | if (uh->check == 0) |
| 128 | uh->check = CSUM_MANGLED_0; |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 129 | } else { |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 130 | uh->check = gso_make_checksum(skb, ~uh->check); |
| 131 | |
| 132 | if (uh->check == 0) |
| 133 | uh->check = CSUM_MANGLED_0; |
| 134 | } |
Tom Herbert | 155e010 | 2014-07-13 19:49:56 -0700 | [diff] [blame] | 135 | } while ((skb = skb->next)); |
| 136 | out: |
| 137 | return segs; |
| 138 | } |
| 139 | |
Tom Herbert | 8bce6d7 | 2014-09-29 20:22:29 -0700 | [diff] [blame] | 140 | struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb, |
| 141 | netdev_features_t features, |
| 142 | bool is_ipv6) |
| 143 | { |
| 144 | __be16 protocol = skb->protocol; |
| 145 | const struct net_offload **offloads; |
| 146 | const struct net_offload *ops; |
| 147 | struct sk_buff *segs = ERR_PTR(-EINVAL); |
| 148 | struct sk_buff *(*gso_inner_segment)(struct sk_buff *skb, |
| 149 | netdev_features_t features); |
| 150 | |
| 151 | rcu_read_lock(); |
| 152 | |
| 153 | switch (skb->inner_protocol_type) { |
| 154 | case ENCAP_TYPE_ETHER: |
| 155 | protocol = skb->inner_protocol; |
| 156 | gso_inner_segment = skb_mac_gso_segment; |
| 157 | break; |
| 158 | case ENCAP_TYPE_IPPROTO: |
| 159 | offloads = is_ipv6 ? inet6_offloads : inet_offloads; |
| 160 | ops = rcu_dereference(offloads[skb->inner_ipproto]); |
| 161 | if (!ops || !ops->callbacks.gso_segment) |
| 162 | goto out_unlock; |
| 163 | gso_inner_segment = ops->callbacks.gso_segment; |
| 164 | break; |
| 165 | default: |
| 166 | goto out_unlock; |
| 167 | } |
| 168 | |
| 169 | segs = __skb_udp_tunnel_segment(skb, features, gso_inner_segment, |
Tom Herbert | 4bcb877 | 2014-11-04 09:06:52 -0800 | [diff] [blame] | 170 | protocol, is_ipv6); |
Tom Herbert | 8bce6d7 | 2014-09-29 20:22:29 -0700 | [diff] [blame] | 171 | |
| 172 | out_unlock: |
| 173 | rcu_read_unlock(); |
| 174 | |
| 175 | return segs; |
| 176 | } |
| 177 | |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 178 | static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb, |
| 179 | netdev_features_t features) |
| 180 | { |
| 181 | struct sk_buff *segs = ERR_PTR(-EINVAL); |
| 182 | unsigned int mss; |
Wei-Chun Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 183 | __wsum csum; |
Tom Herbert | f71470b | 2014-09-20 14:52:29 -0700 | [diff] [blame] | 184 | struct udphdr *uh; |
| 185 | struct iphdr *iph; |
Wei-Chun Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 186 | |
| 187 | if (skb->encapsulation && |
Tom Herbert | 0f4f4ff | 2014-06-04 17:20:16 -0700 | [diff] [blame] | 188 | (skb_shinfo(skb)->gso_type & |
| 189 | (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))) { |
Tom Herbert | 8bce6d7 | 2014-09-29 20:22:29 -0700 | [diff] [blame] | 190 | segs = skb_udp_tunnel_segment(skb, features, false); |
Wei-Chun Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 191 | goto out; |
| 192 | } |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 193 | |
Tom Herbert | f71470b | 2014-09-20 14:52:29 -0700 | [diff] [blame] | 194 | if (!pskb_may_pull(skb, sizeof(struct udphdr))) |
| 195 | goto out; |
| 196 | |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 197 | mss = skb_shinfo(skb)->gso_size; |
| 198 | if (unlikely(skb->len <= mss)) |
| 199 | goto out; |
| 200 | |
| 201 | if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) { |
| 202 | /* Packet is from an untrusted source, reset gso_segs. */ |
| 203 | int type = skb_shinfo(skb)->gso_type; |
| 204 | |
| 205 | if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY | |
| 206 | SKB_GSO_UDP_TUNNEL | |
Tom Herbert | 0f4f4ff | 2014-06-04 17:20:16 -0700 | [diff] [blame] | 207 | SKB_GSO_UDP_TUNNEL_CSUM | |
Tom Herbert | e585f23 | 2014-11-04 09:06:54 -0800 | [diff] [blame] | 208 | SKB_GSO_TUNNEL_REMCSUM | |
Eric Dumazet | cb32f51 | 2013-10-19 11:42:57 -0700 | [diff] [blame] | 209 | SKB_GSO_IPIP | |
Pravin B Shelar | 59b93b4 | 2014-11-05 15:27:48 -0800 | [diff] [blame] | 210 | SKB_GSO_GRE | SKB_GSO_GRE_CSUM) || |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 211 | !(type & (SKB_GSO_UDP)))) |
| 212 | goto out; |
| 213 | |
| 214 | skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss); |
| 215 | |
| 216 | segs = NULL; |
| 217 | goto out; |
| 218 | } |
| 219 | |
Wei-Chun Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 220 | /* Do software UFO. Complete and fill in the UDP checksum as |
| 221 | * HW cannot do checksum of UDP packets sent as multiple |
| 222 | * IP fragments. |
| 223 | */ |
Tom Herbert | f71470b | 2014-09-20 14:52:29 -0700 | [diff] [blame] | 224 | |
| 225 | uh = udp_hdr(skb); |
| 226 | iph = ip_hdr(skb); |
| 227 | |
| 228 | uh->check = 0; |
| 229 | csum = skb_checksum(skb, 0, skb->len, 0); |
| 230 | uh->check = udp_v4_check(skb->len, iph->saddr, iph->daddr, csum); |
| 231 | if (uh->check == 0) |
| 232 | uh->check = CSUM_MANGLED_0; |
| 233 | |
Wei-Chun Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 234 | skb->ip_summed = CHECKSUM_NONE; |
| 235 | |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 236 | /* Fragment the skb. IP headers of the fragments are updated in |
| 237 | * inet_gso_segment() |
| 238 | */ |
Wei-Chun Chao | 7a7ffba | 2013-12-26 13:10:22 -0800 | [diff] [blame] | 239 | segs = skb_segment(skb, features); |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 240 | out: |
| 241 | return segs; |
| 242 | } |
| 243 | |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 244 | int udp_add_offload(struct udp_offload *uo) |
| 245 | { |
Or Gerlitz | b5aaab1 | 2014-01-29 18:08:59 +0200 | [diff] [blame] | 246 | struct udp_offload_priv *new_offload = kzalloc(sizeof(*new_offload), GFP_ATOMIC); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 247 | |
| 248 | if (!new_offload) |
| 249 | return -ENOMEM; |
| 250 | |
| 251 | new_offload->offload = uo; |
| 252 | |
| 253 | spin_lock(&udp_offload_lock); |
Shlomo Pongratz | a664a4f | 2014-02-02 15:42:10 +0200 | [diff] [blame] | 254 | new_offload->next = udp_offload_base; |
| 255 | rcu_assign_pointer(udp_offload_base, new_offload); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 256 | spin_unlock(&udp_offload_lock); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | EXPORT_SYMBOL(udp_add_offload); |
| 261 | |
| 262 | static void udp_offload_free_routine(struct rcu_head *head) |
| 263 | { |
| 264 | struct udp_offload_priv *ou_priv = container_of(head, struct udp_offload_priv, rcu); |
| 265 | kfree(ou_priv); |
| 266 | } |
| 267 | |
| 268 | void udp_del_offload(struct udp_offload *uo) |
| 269 | { |
| 270 | struct udp_offload_priv __rcu **head = &udp_offload_base; |
| 271 | struct udp_offload_priv *uo_priv; |
| 272 | |
| 273 | spin_lock(&udp_offload_lock); |
| 274 | |
Shlomo Pongratz | a664a4f | 2014-02-02 15:42:10 +0200 | [diff] [blame] | 275 | uo_priv = udp_deref_protected(*head); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 276 | for (; uo_priv != NULL; |
Shlomo Pongratz | a664a4f | 2014-02-02 15:42:10 +0200 | [diff] [blame] | 277 | uo_priv = udp_deref_protected(*head)) { |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 278 | if (uo_priv->offload == uo) { |
Shlomo Pongratz | a664a4f | 2014-02-02 15:42:10 +0200 | [diff] [blame] | 279 | rcu_assign_pointer(*head, |
| 280 | udp_deref_protected(uo_priv->next)); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 281 | goto unlock; |
| 282 | } |
| 283 | head = &uo_priv->next; |
| 284 | } |
Shlomo Pongratz | a1d0cd8 | 2014-01-22 15:23:29 +0200 | [diff] [blame] | 285 | 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] | 286 | unlock: |
| 287 | spin_unlock(&udp_offload_lock); |
| 288 | if (uo_priv != NULL) |
| 289 | call_rcu(&uo_priv->rcu, udp_offload_free_routine); |
| 290 | } |
| 291 | EXPORT_SYMBOL(udp_del_offload); |
| 292 | |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame] | 293 | struct sk_buff **udp_gro_receive(struct sk_buff **head, struct sk_buff *skb, |
| 294 | struct udphdr *uh) |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 295 | { |
| 296 | struct udp_offload_priv *uo_priv; |
| 297 | struct sk_buff *p, **pp = NULL; |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame] | 298 | struct udphdr *uh2; |
| 299 | unsigned int off = skb_gro_offset(skb); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 300 | int flush = 1; |
| 301 | |
| 302 | if (NAPI_GRO_CB(skb)->udp_mark || |
Tom Herbert | 662880f | 2014-08-27 21:26:56 -0700 | [diff] [blame] | 303 | (skb->ip_summed != CHECKSUM_PARTIAL && |
| 304 | NAPI_GRO_CB(skb)->csum_cnt == 0 && |
| 305 | !NAPI_GRO_CB(skb)->csum_valid)) |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 306 | goto out; |
| 307 | |
| 308 | /* mark that this skb passed once through the udp gro layer */ |
| 309 | NAPI_GRO_CB(skb)->udp_mark = 1; |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 310 | |
| 311 | rcu_read_lock(); |
| 312 | uo_priv = rcu_dereference(udp_offload_base); |
| 313 | for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) { |
| 314 | if (uo_priv->offload->port == uh->dest && |
| 315 | uo_priv->offload->callbacks.gro_receive) |
| 316 | goto unflush; |
| 317 | } |
| 318 | goto out_unlock; |
| 319 | |
| 320 | unflush: |
| 321 | flush = 0; |
| 322 | |
| 323 | for (p = *head; p; p = p->next) { |
| 324 | if (!NAPI_GRO_CB(p)->same_flow) |
| 325 | continue; |
| 326 | |
| 327 | uh2 = (struct udphdr *)(p->data + off); |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame] | 328 | |
| 329 | /* Match ports and either checksums are either both zero |
| 330 | * or nonzero. |
| 331 | */ |
| 332 | if ((*(u32 *)&uh->source != *(u32 *)&uh2->source) || |
| 333 | (!uh->check ^ !uh2->check)) { |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 334 | NAPI_GRO_CB(p)->same_flow = 0; |
| 335 | continue; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */ |
Tom Herbert | 6bae1d4 | 2014-06-10 18:54:26 -0700 | [diff] [blame] | 340 | skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr)); |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 341 | NAPI_GRO_CB(skb)->proto = uo_priv->offload->ipproto; |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 342 | pp = uo_priv->offload->callbacks.gro_receive(head, skb, |
| 343 | uo_priv->offload); |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 344 | |
| 345 | out_unlock: |
| 346 | rcu_read_unlock(); |
| 347 | out: |
| 348 | NAPI_GRO_CB(skb)->flush |= flush; |
| 349 | return pp; |
| 350 | } |
| 351 | |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame] | 352 | static struct sk_buff **udp4_gro_receive(struct sk_buff **head, |
| 353 | struct sk_buff *skb) |
| 354 | { |
| 355 | struct udphdr *uh = udp_gro_udphdr(skb); |
| 356 | |
Tom Herbert | 2abb7cd | 2014-08-31 15:12:43 -0700 | [diff] [blame] | 357 | if (unlikely(!uh)) |
| 358 | goto flush; |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame] | 359 | |
Tom Herbert | 2abb7cd | 2014-08-31 15:12:43 -0700 | [diff] [blame] | 360 | /* Don't bother verifying checksum if we're going to flush anyway. */ |
Scott Wood | 2d8f7e2 | 2014-09-10 21:23:18 -0500 | [diff] [blame] | 361 | if (NAPI_GRO_CB(skb)->flush) |
Tom Herbert | 2abb7cd | 2014-08-31 15:12:43 -0700 | [diff] [blame] | 362 | goto skip; |
| 363 | |
| 364 | if (skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check, |
| 365 | inet_gro_compute_pseudo)) |
| 366 | goto flush; |
| 367 | else if (uh->check) |
| 368 | skb_gro_checksum_try_convert(skb, IPPROTO_UDP, uh->check, |
| 369 | inet_gro_compute_pseudo); |
| 370 | skip: |
Tom Herbert | efc98d0 | 2014-10-03 15:48:08 -0700 | [diff] [blame] | 371 | NAPI_GRO_CB(skb)->is_ipv6 = 0; |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame] | 372 | return udp_gro_receive(head, skb, uh); |
Tom Herbert | 2abb7cd | 2014-08-31 15:12:43 -0700 | [diff] [blame] | 373 | |
| 374 | flush: |
| 375 | NAPI_GRO_CB(skb)->flush = 1; |
| 376 | return NULL; |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | int udp_gro_complete(struct sk_buff *skb, int nhoff) |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 380 | { |
| 381 | struct udp_offload_priv *uo_priv; |
| 382 | __be16 newlen = htons(skb->len - nhoff); |
| 383 | struct udphdr *uh = (struct udphdr *)(skb->data + nhoff); |
| 384 | int err = -ENOSYS; |
| 385 | |
| 386 | uh->len = newlen; |
| 387 | |
| 388 | rcu_read_lock(); |
| 389 | |
| 390 | uo_priv = rcu_dereference(udp_offload_base); |
| 391 | for (; uo_priv != NULL; uo_priv = rcu_dereference(uo_priv->next)) { |
| 392 | if (uo_priv->offload->port == uh->dest && |
| 393 | uo_priv->offload->callbacks.gro_complete) |
| 394 | break; |
| 395 | } |
| 396 | |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 397 | if (uo_priv != NULL) { |
| 398 | NAPI_GRO_CB(skb)->proto = uo_priv->offload->ipproto; |
Tom Herbert | a2b12f3 | 2015-01-12 17:00:37 -0800 | [diff] [blame] | 399 | err = uo_priv->offload->callbacks.gro_complete(skb, |
| 400 | nhoff + sizeof(struct udphdr), |
| 401 | uo_priv->offload); |
Tom Herbert | afe9332 | 2014-09-17 12:25:57 -0700 | [diff] [blame] | 402 | } |
Or Gerlitz | b582ef0 | 2014-01-20 13:59:19 +0200 | [diff] [blame] | 403 | |
| 404 | rcu_read_unlock(); |
| 405 | return err; |
| 406 | } |
| 407 | |
Eric Dumazet | 72bb17b | 2014-09-09 08:29:12 -0700 | [diff] [blame] | 408 | static int udp4_gro_complete(struct sk_buff *skb, int nhoff) |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame] | 409 | { |
| 410 | const struct iphdr *iph = ip_hdr(skb); |
| 411 | struct udphdr *uh = (struct udphdr *)(skb->data + nhoff); |
| 412 | |
| 413 | if (uh->check) |
| 414 | uh->check = ~udp_v4_check(skb->len - nhoff, iph->saddr, |
| 415 | iph->daddr, 0); |
| 416 | |
| 417 | return udp_gro_complete(skb, nhoff); |
| 418 | } |
| 419 | |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 420 | static const struct net_offload udpv4_offload = { |
| 421 | .callbacks = { |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 422 | .gso_segment = udp4_ufo_fragment, |
Tom Herbert | 57c67ff | 2014-08-22 13:34:44 -0700 | [diff] [blame] | 423 | .gro_receive = udp4_gro_receive, |
| 424 | .gro_complete = udp4_gro_complete, |
Daniel Borkmann | da5bab0 | 2013-06-08 12:56:03 +0200 | [diff] [blame] | 425 | }, |
| 426 | }; |
| 427 | |
| 428 | int __init udpv4_offload_init(void) |
| 429 | { |
| 430 | return inet_add_offload(&udpv4_offload, IPPROTO_UDP); |
| 431 | } |