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