blob: 8edcfa66d1e5ed0f224ba271d6898f1837fc5c76 [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)
Myungho Jung6ed69182019-03-07 10:23:08 +090055 goto out_reset;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020056
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)
Myungho Jung6ed69182019-03-07 10:23:08 +090061 goto out_reset;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020062
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);
Myungho Jung6ed69182019-03-07 10:23:08 +090069 goto out_reset;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020070 }
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);
Myungho Jung6ed69182019-03-07 10:23:08 +090085out_reset:
86 secpath_reset(skb);
Steffen Klassert7785bba2017-02-15 09:40:00 +010087out:
88 skb_push(skb, offset);
89 NAPI_GRO_CB(skb)->same_flow = 0;
90 NAPI_GRO_CB(skb)->flush = 1;
91
92 return NULL;
93}
94
Steffen Klassert7862b402017-04-14 10:06:50 +020095static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
96{
97 struct ip_esp_hdr *esph;
98 struct iphdr *iph = ip_hdr(skb);
99 struct xfrm_offload *xo = xfrm_offload(skb);
100 int proto = iph->protocol;
101
102 skb_push(skb, -skb_network_offset(skb));
103 esph = ip_esp_hdr(skb);
104 *skb_mac_header(skb) = IPPROTO_ESP;
105
106 esph->spi = x->id.spi;
107 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
108
109 xo->proto = proto;
110}
111
Florian Westphal7613b922019-03-29 21:16:27 +0100112static struct sk_buff *xfrm4_tunnel_gso_segment(struct xfrm_state *x,
113 struct sk_buff *skb,
114 netdev_features_t features)
115{
116 __skb_push(skb, skb->mac_len);
117 return skb_mac_gso_segment(skb, features);
118}
119
120static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x,
121 struct sk_buff *skb,
122 netdev_features_t features)
123{
124 const struct net_offload *ops;
125 struct sk_buff *segs = ERR_PTR(-EINVAL);
126 struct xfrm_offload *xo = xfrm_offload(skb);
127
128 skb->transport_header += x->props.header_len;
129 ops = rcu_dereference(inet_offloads[xo->proto]);
130 if (likely(ops && ops->callbacks.gso_segment))
131 segs = ops->callbacks.gso_segment(skb, features);
132
133 return segs;
134}
135
136static struct sk_buff *xfrm4_outer_mode_gso_segment(struct xfrm_state *x,
137 struct sk_buff *skb,
138 netdev_features_t features)
139{
Florian Westphalc9500d72019-03-29 21:16:32 +0100140 switch (x->outer_mode.encap) {
Florian Westphal7613b922019-03-29 21:16:27 +0100141 case XFRM_MODE_TUNNEL:
142 return xfrm4_tunnel_gso_segment(x, skb, features);
143 case XFRM_MODE_TRANSPORT:
144 return xfrm4_transport_gso_segment(x, skb, features);
145 }
146
147 return ERR_PTR(-EOPNOTSUPP);
148}
149
Steffen Klassert7862b402017-04-14 10:06:50 +0200150static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
151 netdev_features_t features)
152{
Steffen Klassert7862b402017-04-14 10:06:50 +0200153 struct xfrm_state *x;
154 struct ip_esp_hdr *esph;
155 struct crypto_aead *aead;
Steffen Klassert7862b402017-04-14 10:06:50 +0200156 netdev_features_t esp_features = features;
157 struct xfrm_offload *xo = xfrm_offload(skb);
Florian Westphal2294be0f2018-12-18 17:15:20 +0100158 struct sec_path *sp;
Steffen Klassert7862b402017-04-14 10:06:50 +0200159
160 if (!xo)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100161 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200162
Willem de Bruijn121d57a2018-01-19 09:29:18 -0500163 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
David S. Miller5ca11442018-01-23 13:49:06 -0500164 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200165
Florian Westphal2294be0f2018-12-18 17:15:20 +0100166 sp = skb_sec_path(skb);
167 x = sp->xvec[sp->len - 1];
Steffen Klassert7862b402017-04-14 10:06:50 +0200168 aead = x->data;
169 esph = ip_esp_hdr(skb);
170
171 if (esph->spi != x->id.spi)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100172 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200173
174 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100175 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200176
177 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
178
179 skb->encap_hdr_csum = 1;
180
Boris Pismenny65fd2c22019-03-21 16:41:37 +0200181 if ((!(skb->dev->gso_partial_features & NETIF_F_HW_ESP) &&
182 !(features & NETIF_F_HW_ESP)) || x->xso.dev != skb->dev)
Steffen Klassert7862b402017-04-14 10:06:50 +0200183 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
Boris Pismenny65fd2c22019-03-21 16:41:37 +0200184 else if (!(features & NETIF_F_HW_ESP_TX_CSUM) &&
185 !(skb->dev->gso_partial_features & NETIF_F_HW_ESP_TX_CSUM))
Shannon Nelson5211fcf2018-02-26 14:28:19 -0800186 esp_features = features & ~NETIF_F_CSUM_MASK;
Steffen Klassert7862b402017-04-14 10:06:50 +0200187
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100188 xo->flags |= XFRM_GSO_SEGMENT;
Steffen Klassert7862b402017-04-14 10:06:50 +0200189
Florian Westphal7613b922019-03-29 21:16:27 +0100190 return xfrm4_outer_mode_gso_segment(x, skb, esp_features);
Steffen Klassert7862b402017-04-14 10:06:50 +0200191}
192
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200193static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
194{
195 struct crypto_aead *aead = x->data;
Ilan Tayariec9567a2017-08-01 12:49:04 +0300196 struct xfrm_offload *xo = xfrm_offload(skb);
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200197
198 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
199 return -EINVAL;
200
Ilan Tayariec9567a2017-08-01 12:49:04 +0300201 if (!(xo->flags & CRYPTO_DONE))
202 skb->ip_summed = CHECKSUM_NONE;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200203
204 return esp_input_done2(skb, 0);
205}
206
207static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
208{
209 int err;
210 int alen;
211 int blksize;
212 struct xfrm_offload *xo;
213 struct ip_esp_hdr *esph;
214 struct crypto_aead *aead;
215 struct esp_info esp;
216 bool hw_offload = true;
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100217 __u32 seq;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200218
219 esp.inplace = true;
220
221 xo = xfrm_offload(skb);
222
223 if (!xo)
224 return -EINVAL;
225
Boris Pismenny65fd2c22019-03-21 16:41:37 +0200226 if ((!(features & NETIF_F_HW_ESP) &&
227 !(skb->dev->gso_partial_features & NETIF_F_HW_ESP)) ||
228 x->xso.dev != skb->dev) {
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200229 xo->flags |= CRYPTO_FALLBACK;
230 hw_offload = false;
231 }
232
233 esp.proto = xo->proto;
234
235 /* skb is pure payload to encrypt */
236
237 aead = x->data;
238 alen = crypto_aead_authsize(aead);
239
240 esp.tfclen = 0;
241 /* XXX: Add support for tfc padding here. */
242
243 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
244 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
245 esp.plen = esp.clen - skb->len - esp.tfclen;
246 esp.tailen = esp.tfclen + esp.plen + alen;
247
248 esp.esph = ip_esp_hdr(skb);
249
250
251 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
252 esp.nfrags = esp_output_head(x, skb, &esp);
253 if (esp.nfrags < 0)
254 return esp.nfrags;
255 }
256
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100257 seq = xo->seq.low;
258
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200259 esph = esp.esph;
260 esph->spi = x->id.spi;
261
262 skb_push(skb, -skb_network_offset(skb));
263
264 if (xo->flags & XFRM_GSO_SEGMENT) {
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100265 esph->seq_no = htonl(seq);
266
267 if (!skb_is_gso(skb))
268 xo->seq.low++;
269 else
270 xo->seq.low += skb_shinfo(skb)->gso_segs;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200271 }
272
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100273 esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32));
274
275 ip_hdr(skb)->tot_len = htons(skb->len);
276 ip_send_check(ip_hdr(skb));
277
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200278 if (hw_offload)
279 return 0;
280
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200281 err = esp_output_tail(x, skb, &esp);
Steffen Klassert4ff03082017-08-07 08:31:07 +0200282 if (err)
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200283 return err;
284
285 secpath_reset(skb);
286
287 return 0;
288}
289
Steffen Klassert7785bba2017-02-15 09:40:00 +0100290static const struct net_offload esp4_offload = {
291 .callbacks = {
292 .gro_receive = esp4_gro_receive,
Steffen Klassert7862b402017-04-14 10:06:50 +0200293 .gso_segment = esp4_gso_segment,
Steffen Klassert7785bba2017-02-15 09:40:00 +0100294 },
295};
296
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200297static const struct xfrm_type_offload esp_type_offload = {
298 .description = "ESP4 OFFLOAD",
299 .owner = THIS_MODULE,
300 .proto = IPPROTO_ESP,
301 .input_tail = esp_input_tail,
302 .xmit = esp_xmit,
Steffen Klassert7862b402017-04-14 10:06:50 +0200303 .encap = esp4_gso_encap,
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200304};
305
Steffen Klassert7785bba2017-02-15 09:40:00 +0100306static int __init esp4_offload_init(void)
307{
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200308 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
309 pr_info("%s: can't add xfrm type offload\n", __func__);
310 return -EAGAIN;
311 }
312
Steffen Klassert7785bba2017-02-15 09:40:00 +0100313 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
314}
315
316static void __exit esp4_offload_exit(void)
317{
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200318 if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0)
319 pr_info("%s: can't remove xfrm type offload\n", __func__);
320
Steffen Klassert7785bba2017-02-15 09:40:00 +0100321 inet_del_offload(&esp4_offload, IPPROTO_ESP);
322}
323
324module_init(esp4_offload_init);
325module_exit(esp4_offload_exit);
326MODULE_LICENSE("GPL");
327MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
Ilan Tayariffdb5212017-08-01 12:49:08 +0300328MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);