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