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