Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * IPV4 GSO/GRO offload support |
| 3 | * Linux INET implementation |
| 4 | * |
| 5 | * Copyright (C) 2016 secunet Security Networks AG |
| 6 | * Author: Steffen Klassert <steffen.klassert@secunet.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms and conditions of the GNU General Public License, |
| 10 | * version 2, as published by the Free Software Foundation. |
| 11 | * |
| 12 | * ESP GRO support |
| 13 | */ |
| 14 | |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <net/protocol.h> |
| 18 | #include <crypto/aead.h> |
| 19 | #include <crypto/authenc.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <net/ip.h> |
| 23 | #include <net/xfrm.h> |
| 24 | #include <net/esp.h> |
| 25 | #include <linux/scatterlist.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <net/udp.h> |
| 30 | |
David Miller | d4546c2 | 2018-06-24 14:13:49 +0900 | [diff] [blame] | 31 | static struct sk_buff *esp4_gro_receive(struct list_head *head, |
| 32 | struct sk_buff *skb) |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 33 | { |
| 34 | int offset = skb_gro_offset(skb); |
| 35 | struct xfrm_offload *xo; |
| 36 | struct xfrm_state *x; |
| 37 | __be32 seq; |
| 38 | __be32 spi; |
| 39 | int err; |
| 40 | |
Steffen Klassert | 374d1b5 | 2018-01-05 08:35:47 +0100 | [diff] [blame] | 41 | if (!pskb_pull(skb, offset)) |
| 42 | return NULL; |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 43 | |
| 44 | if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0) |
| 45 | goto out; |
| 46 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 47 | xo = xfrm_offload(skb); |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 48 | if (!xo || !(xo->flags & CRYPTO_DONE)) { |
Florian Westphal | 0ca64da | 2018-12-18 17:15:18 +0100 | [diff] [blame] | 49 | struct sec_path *sp = secpath_set(skb); |
| 50 | |
| 51 | if (!sp) |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 52 | goto out; |
| 53 | |
Florian Westphal | 0ca64da | 2018-12-18 17:15:18 +0100 | [diff] [blame] | 54 | if (sp->len == XFRM_MAX_DEPTH) |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 55 | goto out; |
| 56 | |
| 57 | x = xfrm_state_lookup(dev_net(skb->dev), skb->mark, |
| 58 | (xfrm_address_t *)&ip_hdr(skb)->daddr, |
| 59 | spi, IPPROTO_ESP, AF_INET); |
| 60 | if (!x) |
| 61 | goto out; |
| 62 | |
Florian Westphal | 0ca64da | 2018-12-18 17:15:18 +0100 | [diff] [blame] | 63 | sp->xvec[sp->len++] = x; |
| 64 | sp->olen++; |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 65 | |
| 66 | xo = xfrm_offload(skb); |
| 67 | if (!xo) { |
| 68 | xfrm_state_put(x); |
| 69 | goto out; |
| 70 | } |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 71 | } |
Steffen Klassert | bcd1f8a | 2017-04-14 10:07:49 +0200 | [diff] [blame] | 72 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 73 | xo->flags |= XFRM_GRO; |
| 74 | |
| 75 | XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL; |
| 76 | XFRM_SPI_SKB_CB(skb)->family = AF_INET; |
| 77 | XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr); |
| 78 | XFRM_SPI_SKB_CB(skb)->seq = seq; |
| 79 | |
| 80 | /* We don't need to handle errors from xfrm_input, it does all |
| 81 | * the error handling and frees the resources on error. */ |
| 82 | xfrm_input(skb, IPPROTO_ESP, spi, -2); |
| 83 | |
| 84 | return ERR_PTR(-EINPROGRESS); |
| 85 | out: |
| 86 | skb_push(skb, offset); |
| 87 | NAPI_GRO_CB(skb)->same_flow = 0; |
| 88 | NAPI_GRO_CB(skb)->flush = 1; |
| 89 | |
| 90 | return NULL; |
| 91 | } |
| 92 | |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 93 | static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb) |
| 94 | { |
| 95 | struct ip_esp_hdr *esph; |
| 96 | struct iphdr *iph = ip_hdr(skb); |
| 97 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 98 | int proto = iph->protocol; |
| 99 | |
| 100 | skb_push(skb, -skb_network_offset(skb)); |
| 101 | esph = ip_esp_hdr(skb); |
| 102 | *skb_mac_header(skb) = IPPROTO_ESP; |
| 103 | |
| 104 | esph->spi = x->id.spi; |
| 105 | esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); |
| 106 | |
| 107 | xo->proto = proto; |
| 108 | } |
| 109 | |
Florian Westphal | 7613b92 | 2019-03-29 21:16:27 +0100 | [diff] [blame] | 110 | static struct sk_buff *xfrm4_tunnel_gso_segment(struct xfrm_state *x, |
| 111 | struct sk_buff *skb, |
| 112 | netdev_features_t features) |
| 113 | { |
| 114 | __skb_push(skb, skb->mac_len); |
| 115 | return skb_mac_gso_segment(skb, features); |
| 116 | } |
| 117 | |
| 118 | static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x, |
| 119 | struct sk_buff *skb, |
| 120 | netdev_features_t features) |
| 121 | { |
| 122 | const struct net_offload *ops; |
| 123 | struct sk_buff *segs = ERR_PTR(-EINVAL); |
| 124 | struct xfrm_offload *xo = xfrm_offload(skb); |
| 125 | |
| 126 | skb->transport_header += x->props.header_len; |
| 127 | ops = rcu_dereference(inet_offloads[xo->proto]); |
| 128 | if (likely(ops && ops->callbacks.gso_segment)) |
| 129 | segs = ops->callbacks.gso_segment(skb, features); |
| 130 | |
| 131 | return segs; |
| 132 | } |
| 133 | |
| 134 | static struct sk_buff *xfrm4_outer_mode_gso_segment(struct xfrm_state *x, |
| 135 | struct sk_buff *skb, |
| 136 | netdev_features_t features) |
| 137 | { |
Florian Westphal | c9500d7 | 2019-03-29 21:16:32 +0100 | [diff] [blame^] | 138 | switch (x->outer_mode.encap) { |
Florian Westphal | 7613b92 | 2019-03-29 21:16:27 +0100 | [diff] [blame] | 139 | case XFRM_MODE_TUNNEL: |
| 140 | return xfrm4_tunnel_gso_segment(x, skb, features); |
| 141 | case XFRM_MODE_TRANSPORT: |
| 142 | return xfrm4_transport_gso_segment(x, skb, features); |
| 143 | } |
| 144 | |
| 145 | return ERR_PTR(-EOPNOTSUPP); |
| 146 | } |
| 147 | |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 148 | static struct sk_buff *esp4_gso_segment(struct sk_buff *skb, |
| 149 | netdev_features_t features) |
| 150 | { |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 151 | struct xfrm_state *x; |
| 152 | struct ip_esp_hdr *esph; |
| 153 | struct crypto_aead *aead; |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 154 | netdev_features_t esp_features = features; |
| 155 | struct xfrm_offload *xo = xfrm_offload(skb); |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 156 | struct sec_path *sp; |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 157 | |
| 158 | if (!xo) |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 159 | return ERR_PTR(-EINVAL); |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 160 | |
Willem de Bruijn | 121d57a | 2018-01-19 09:29:18 -0500 | [diff] [blame] | 161 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP)) |
David S. Miller | 5ca1144 | 2018-01-23 13:49:06 -0500 | [diff] [blame] | 162 | return ERR_PTR(-EINVAL); |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 163 | |
Florian Westphal | 2294be0f | 2018-12-18 17:15:20 +0100 | [diff] [blame] | 164 | sp = skb_sec_path(skb); |
| 165 | x = sp->xvec[sp->len - 1]; |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 166 | aead = x->data; |
| 167 | esph = ip_esp_hdr(skb); |
| 168 | |
| 169 | if (esph->spi != x->id.spi) |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 170 | return ERR_PTR(-EINVAL); |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 171 | |
| 172 | if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead))) |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 173 | return ERR_PTR(-EINVAL); |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 174 | |
| 175 | __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)); |
| 176 | |
| 177 | skb->encap_hdr_csum = 1; |
| 178 | |
Boris Pismenny | 65fd2c2 | 2019-03-21 16:41:37 +0200 | [diff] [blame] | 179 | if ((!(skb->dev->gso_partial_features & NETIF_F_HW_ESP) && |
| 180 | !(features & NETIF_F_HW_ESP)) || x->xso.dev != skb->dev) |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 181 | esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK); |
Boris Pismenny | 65fd2c2 | 2019-03-21 16:41:37 +0200 | [diff] [blame] | 182 | else if (!(features & NETIF_F_HW_ESP_TX_CSUM) && |
| 183 | !(skb->dev->gso_partial_features & NETIF_F_HW_ESP_TX_CSUM)) |
Shannon Nelson | 5211fcf | 2018-02-26 14:28:19 -0800 | [diff] [blame] | 184 | esp_features = features & ~NETIF_F_CSUM_MASK; |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 185 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 186 | xo->flags |= XFRM_GSO_SEGMENT; |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 187 | |
Florian Westphal | 7613b92 | 2019-03-29 21:16:27 +0100 | [diff] [blame] | 188 | return xfrm4_outer_mode_gso_segment(x, skb, esp_features); |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 191 | static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb) |
| 192 | { |
| 193 | struct crypto_aead *aead = x->data; |
Ilan Tayari | ec9567a | 2017-08-01 12:49:04 +0300 | [diff] [blame] | 194 | struct xfrm_offload *xo = xfrm_offload(skb); |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 195 | |
| 196 | if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead))) |
| 197 | return -EINVAL; |
| 198 | |
Ilan Tayari | ec9567a | 2017-08-01 12:49:04 +0300 | [diff] [blame] | 199 | if (!(xo->flags & CRYPTO_DONE)) |
| 200 | skb->ip_summed = CHECKSUM_NONE; |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 201 | |
| 202 | return esp_input_done2(skb, 0); |
| 203 | } |
| 204 | |
| 205 | static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features) |
| 206 | { |
| 207 | int err; |
| 208 | int alen; |
| 209 | int blksize; |
| 210 | struct xfrm_offload *xo; |
| 211 | struct ip_esp_hdr *esph; |
| 212 | struct crypto_aead *aead; |
| 213 | struct esp_info esp; |
| 214 | bool hw_offload = true; |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 215 | __u32 seq; |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 216 | |
| 217 | esp.inplace = true; |
| 218 | |
| 219 | xo = xfrm_offload(skb); |
| 220 | |
| 221 | if (!xo) |
| 222 | return -EINVAL; |
| 223 | |
Boris Pismenny | 65fd2c2 | 2019-03-21 16:41:37 +0200 | [diff] [blame] | 224 | if ((!(features & NETIF_F_HW_ESP) && |
| 225 | !(skb->dev->gso_partial_features & NETIF_F_HW_ESP)) || |
| 226 | x->xso.dev != skb->dev) { |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 227 | xo->flags |= CRYPTO_FALLBACK; |
| 228 | hw_offload = false; |
| 229 | } |
| 230 | |
| 231 | esp.proto = xo->proto; |
| 232 | |
| 233 | /* skb is pure payload to encrypt */ |
| 234 | |
| 235 | aead = x->data; |
| 236 | alen = crypto_aead_authsize(aead); |
| 237 | |
| 238 | esp.tfclen = 0; |
| 239 | /* XXX: Add support for tfc padding here. */ |
| 240 | |
| 241 | blksize = ALIGN(crypto_aead_blocksize(aead), 4); |
| 242 | esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize); |
| 243 | esp.plen = esp.clen - skb->len - esp.tfclen; |
| 244 | esp.tailen = esp.tfclen + esp.plen + alen; |
| 245 | |
| 246 | esp.esph = ip_esp_hdr(skb); |
| 247 | |
| 248 | |
| 249 | if (!hw_offload || (hw_offload && !skb_is_gso(skb))) { |
| 250 | esp.nfrags = esp_output_head(x, skb, &esp); |
| 251 | if (esp.nfrags < 0) |
| 252 | return esp.nfrags; |
| 253 | } |
| 254 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 255 | seq = xo->seq.low; |
| 256 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 257 | esph = esp.esph; |
| 258 | esph->spi = x->id.spi; |
| 259 | |
| 260 | skb_push(skb, -skb_network_offset(skb)); |
| 261 | |
| 262 | if (xo->flags & XFRM_GSO_SEGMENT) { |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 263 | esph->seq_no = htonl(seq); |
| 264 | |
| 265 | if (!skb_is_gso(skb)) |
| 266 | xo->seq.low++; |
| 267 | else |
| 268 | xo->seq.low += skb_shinfo(skb)->gso_segs; |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Steffen Klassert | 3dca3f3 | 2017-12-20 10:41:31 +0100 | [diff] [blame] | 271 | esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32)); |
| 272 | |
| 273 | ip_hdr(skb)->tot_len = htons(skb->len); |
| 274 | ip_send_check(ip_hdr(skb)); |
| 275 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 276 | if (hw_offload) |
| 277 | return 0; |
| 278 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 279 | err = esp_output_tail(x, skb, &esp); |
Steffen Klassert | 4ff0308 | 2017-08-07 08:31:07 +0200 | [diff] [blame] | 280 | if (err) |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 281 | return err; |
| 282 | |
| 283 | secpath_reset(skb); |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 288 | static const struct net_offload esp4_offload = { |
| 289 | .callbacks = { |
| 290 | .gro_receive = esp4_gro_receive, |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 291 | .gso_segment = esp4_gso_segment, |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 292 | }, |
| 293 | }; |
| 294 | |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 295 | static const struct xfrm_type_offload esp_type_offload = { |
| 296 | .description = "ESP4 OFFLOAD", |
| 297 | .owner = THIS_MODULE, |
| 298 | .proto = IPPROTO_ESP, |
| 299 | .input_tail = esp_input_tail, |
| 300 | .xmit = esp_xmit, |
Steffen Klassert | 7862b40 | 2017-04-14 10:06:50 +0200 | [diff] [blame] | 301 | .encap = esp4_gso_encap, |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 302 | }; |
| 303 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 304 | static int __init esp4_offload_init(void) |
| 305 | { |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 306 | if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) { |
| 307 | pr_info("%s: can't add xfrm type offload\n", __func__); |
| 308 | return -EAGAIN; |
| 309 | } |
| 310 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 311 | return inet_add_offload(&esp4_offload, IPPROTO_ESP); |
| 312 | } |
| 313 | |
| 314 | static void __exit esp4_offload_exit(void) |
| 315 | { |
Steffen Klassert | fca11eb | 2017-04-14 10:06:33 +0200 | [diff] [blame] | 316 | if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0) |
| 317 | pr_info("%s: can't remove xfrm type offload\n", __func__); |
| 318 | |
Steffen Klassert | 7785bba | 2017-02-15 09:40:00 +0100 | [diff] [blame] | 319 | inet_del_offload(&esp4_offload, IPPROTO_ESP); |
| 320 | } |
| 321 | |
| 322 | module_init(esp4_offload_init); |
| 323 | module_exit(esp4_offload_exit); |
| 324 | MODULE_LICENSE("GPL"); |
| 325 | MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>"); |
Ilan Tayari | ffdb521 | 2017-08-01 12:49:08 +0300 | [diff] [blame] | 326 | MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP); |