blob: 8756e0e790d2a94a5b4a587c3bc3de0673baf2c4 [file] [log] [blame]
Steffen Klassert7785bba2017-02-15 09:40:00 +01001/*
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 Millerd4546c22018-06-24 14:13:49 +090031static struct sk_buff *esp4_gro_receive(struct list_head *head,
32 struct sk_buff *skb)
Steffen Klassert7785bba2017-02-15 09:40:00 +010033{
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 Klassert374d1b52018-01-05 08:35:47 +010041 if (!pskb_pull(skb, offset))
42 return NULL;
Steffen Klassert7785bba2017-02-15 09:40:00 +010043
44 if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
45 goto out;
46
Steffen Klassert7785bba2017-02-15 09:40:00 +010047 xo = xfrm_offload(skb);
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020048 if (!xo || !(xo->flags & CRYPTO_DONE)) {
Florian Westphal0ca64da2018-12-18 17:15:18 +010049 struct sec_path *sp = secpath_set(skb);
50
51 if (!sp)
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020052 goto out;
53
Florian Westphal0ca64da2018-12-18 17:15:18 +010054 if (sp->len == XFRM_MAX_DEPTH)
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020055 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 Westphal0ca64da2018-12-18 17:15:18 +010063 sp->xvec[sp->len++] = x;
64 sp->olen++;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020065
66 xo = xfrm_offload(skb);
67 if (!xo) {
68 xfrm_state_put(x);
69 goto out;
70 }
Steffen Klassert7785bba2017-02-15 09:40:00 +010071 }
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020072
Steffen Klassert7785bba2017-02-15 09:40:00 +010073 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);
85out:
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 Klassert7862b402017-04-14 10:06:50 +020093static 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
110static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
111 netdev_features_t features)
112{
Steffen Klassert7862b402017-04-14 10:06:50 +0200113 struct xfrm_state *x;
114 struct ip_esp_hdr *esph;
115 struct crypto_aead *aead;
Steffen Klassert7862b402017-04-14 10:06:50 +0200116 netdev_features_t esp_features = features;
117 struct xfrm_offload *xo = xfrm_offload(skb);
Florian Westphal2294be0f2018-12-18 17:15:20 +0100118 struct sec_path *sp;
Steffen Klassert7862b402017-04-14 10:06:50 +0200119
120 if (!xo)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100121 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200122
Willem de Bruijn121d57a2018-01-19 09:29:18 -0500123 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
David S. Miller5ca11442018-01-23 13:49:06 -0500124 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200125
Florian Westphal2294be0f2018-12-18 17:15:20 +0100126 sp = skb_sec_path(skb);
127 x = sp->xvec[sp->len - 1];
Steffen Klassert7862b402017-04-14 10:06:50 +0200128 aead = x->data;
129 esph = ip_esp_hdr(skb);
130
131 if (esph->spi != x->id.spi)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100132 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200133
134 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100135 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200136
137 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
138
139 skb->encap_hdr_csum = 1;
140
Shannon Nelsonfcb662d2018-06-26 14:19:10 -0700141 if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev)
Steffen Klassert7862b402017-04-14 10:06:50 +0200142 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
Shannon Nelson5211fcf2018-02-26 14:28:19 -0800143 else if (!(features & NETIF_F_HW_ESP_TX_CSUM))
144 esp_features = features & ~NETIF_F_CSUM_MASK;
Steffen Klassert7862b402017-04-14 10:06:50 +0200145
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100146 xo->flags |= XFRM_GSO_SEGMENT;
Steffen Klassert7862b402017-04-14 10:06:50 +0200147
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100148 return x->outer_mode->gso_segment(x, skb, esp_features);
Steffen Klassert7862b402017-04-14 10:06:50 +0200149}
150
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200151static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
152{
153 struct crypto_aead *aead = x->data;
Ilan Tayariec9567a2017-08-01 12:49:04 +0300154 struct xfrm_offload *xo = xfrm_offload(skb);
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200155
156 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
157 return -EINVAL;
158
Ilan Tayariec9567a2017-08-01 12:49:04 +0300159 if (!(xo->flags & CRYPTO_DONE))
160 skb->ip_summed = CHECKSUM_NONE;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200161
162 return esp_input_done2(skb, 0);
163}
164
165static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
166{
167 int err;
168 int alen;
169 int blksize;
170 struct xfrm_offload *xo;
171 struct ip_esp_hdr *esph;
172 struct crypto_aead *aead;
173 struct esp_info esp;
174 bool hw_offload = true;
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100175 __u32 seq;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200176
177 esp.inplace = true;
178
179 xo = xfrm_offload(skb);
180
181 if (!xo)
182 return -EINVAL;
183
Shannon Nelsonfcb662d2018-06-26 14:19:10 -0700184 if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev) {
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200185 xo->flags |= CRYPTO_FALLBACK;
186 hw_offload = false;
187 }
188
189 esp.proto = xo->proto;
190
191 /* skb is pure payload to encrypt */
192
193 aead = x->data;
194 alen = crypto_aead_authsize(aead);
195
196 esp.tfclen = 0;
197 /* XXX: Add support for tfc padding here. */
198
199 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
200 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
201 esp.plen = esp.clen - skb->len - esp.tfclen;
202 esp.tailen = esp.tfclen + esp.plen + alen;
203
204 esp.esph = ip_esp_hdr(skb);
205
206
207 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
208 esp.nfrags = esp_output_head(x, skb, &esp);
209 if (esp.nfrags < 0)
210 return esp.nfrags;
211 }
212
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100213 seq = xo->seq.low;
214
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200215 esph = esp.esph;
216 esph->spi = x->id.spi;
217
218 skb_push(skb, -skb_network_offset(skb));
219
220 if (xo->flags & XFRM_GSO_SEGMENT) {
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100221 esph->seq_no = htonl(seq);
222
223 if (!skb_is_gso(skb))
224 xo->seq.low++;
225 else
226 xo->seq.low += skb_shinfo(skb)->gso_segs;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200227 }
228
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100229 esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32));
230
231 ip_hdr(skb)->tot_len = htons(skb->len);
232 ip_send_check(ip_hdr(skb));
233
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200234 if (hw_offload)
235 return 0;
236
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200237 err = esp_output_tail(x, skb, &esp);
Steffen Klassert4ff03082017-08-07 08:31:07 +0200238 if (err)
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200239 return err;
240
241 secpath_reset(skb);
242
243 return 0;
244}
245
Steffen Klassert7785bba2017-02-15 09:40:00 +0100246static const struct net_offload esp4_offload = {
247 .callbacks = {
248 .gro_receive = esp4_gro_receive,
Steffen Klassert7862b402017-04-14 10:06:50 +0200249 .gso_segment = esp4_gso_segment,
Steffen Klassert7785bba2017-02-15 09:40:00 +0100250 },
251};
252
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200253static const struct xfrm_type_offload esp_type_offload = {
254 .description = "ESP4 OFFLOAD",
255 .owner = THIS_MODULE,
256 .proto = IPPROTO_ESP,
257 .input_tail = esp_input_tail,
258 .xmit = esp_xmit,
Steffen Klassert7862b402017-04-14 10:06:50 +0200259 .encap = esp4_gso_encap,
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200260};
261
Steffen Klassert7785bba2017-02-15 09:40:00 +0100262static int __init esp4_offload_init(void)
263{
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200264 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
265 pr_info("%s: can't add xfrm type offload\n", __func__);
266 return -EAGAIN;
267 }
268
Steffen Klassert7785bba2017-02-15 09:40:00 +0100269 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
270}
271
272static void __exit esp4_offload_exit(void)
273{
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200274 if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0)
275 pr_info("%s: can't remove xfrm type offload\n", __func__);
276
Steffen Klassert7785bba2017-02-15 09:40:00 +0100277 inet_del_offload(&esp4_offload, IPPROTO_ESP);
278}
279
280module_init(esp4_offload_init);
281module_exit(esp4_offload_exit);
282MODULE_LICENSE("GPL");
283MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
Ilan Tayariffdb5212017-08-01 12:49:08 +0300284MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);