blob: 80c4ea0e12f48a57ca2c746df0fa07c0e9772cbb [file] [log] [blame]
Thomas Gleixner75a6faf2019-06-01 10:08:37 +02001// SPDX-License-Identifier: GPL-2.0-only
Steffen Klassert7785bba2017-02-15 09:40:00 +01002/*
3 * IPV4 GSO/GRO offload support
4 * Linux INET implementation
5 *
6 * Copyright (C) 2016 secunet Security Networks AG
7 * Author: Steffen Klassert <steffen.klassert@secunet.com>
8 *
Steffen Klassert7785bba2017-02-15 09:40:00 +01009 * ESP GRO support
10 */
11
12#include <linux/skbuff.h>
13#include <linux/init.h>
14#include <net/protocol.h>
15#include <crypto/aead.h>
16#include <crypto/authenc.h>
17#include <linux/err.h>
18#include <linux/module.h>
Eric Dumazet47210312021-11-15 09:05:51 -080019#include <net/gro.h>
Eric Dumazetd457a0e2023-06-08 19:17:37 +000020#include <net/gso.h>
Steffen Klassert7785bba2017-02-15 09:40:00 +010021#include <net/ip.h>
22#include <net/xfrm.h>
23#include <net/esp.h>
24#include <linux/scatterlist.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <net/udp.h>
29
David Millerd4546c22018-06-24 14:13:49 +090030static struct sk_buff *esp4_gro_receive(struct list_head *head,
31 struct sk_buff *skb)
Steffen Klassert7785bba2017-02-15 09:40:00 +010032{
33 int offset = skb_gro_offset(skb);
34 struct xfrm_offload *xo;
35 struct xfrm_state *x;
Steffen Klassert172bf002023-10-04 15:05:27 +020036 int encap_type = 0;
Steffen Klassert7785bba2017-02-15 09:40:00 +010037 __be32 seq;
38 __be32 spi;
Steffen Klassert7785bba2017-02-15 09:40:00 +010039
Steffen Klassert374d1b52018-01-05 08:35:47 +010040 if (!pskb_pull(skb, offset))
41 return NULL;
Steffen Klassert7785bba2017-02-15 09:40:00 +010042
Yang Li335a2a12021-04-25 18:14:32 +080043 if (xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq) != 0)
Steffen Klassert7785bba2017-02-15 09:40:00 +010044 goto out;
45
Steffen Klassert7785bba2017-02-15 09:40:00 +010046 xo = xfrm_offload(skb);
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020047 if (!xo || !(xo->flags & CRYPTO_DONE)) {
Florian Westphal0ca64da2018-12-18 17:15:18 +010048 struct sec_path *sp = secpath_set(skb);
49
50 if (!sp)
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020051 goto out;
52
Florian Westphal0ca64da2018-12-18 17:15:18 +010053 if (sp->len == XFRM_MAX_DEPTH)
Myungho Jung6ed69182019-03-07 10:23:08 +090054 goto out_reset;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020055
56 x = xfrm_state_lookup(dev_net(skb->dev), skb->mark,
57 (xfrm_address_t *)&ip_hdr(skb)->daddr,
58 spi, IPPROTO_ESP, AF_INET);
Antony Antony15f5fe92024-06-11 08:32:15 +020059
60 if (unlikely(x && x->dir && x->dir != XFRM_SA_DIR_IN)) {
61 /* non-offload path will record the error and audit log */
62 xfrm_state_put(x);
63 x = NULL;
64 }
65
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020066 if (!x)
Myungho Jung6ed69182019-03-07 10:23:08 +090067 goto out_reset;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020068
Ulrich Weber4e4362d2020-01-15 12:11:29 +010069 skb->mark = xfrm_smark_get(skb->mark, x);
70
Florian Westphal0ca64da2018-12-18 17:15:18 +010071 sp->xvec[sp->len++] = x;
72 sp->olen++;
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020073
74 xo = xfrm_offload(skb);
Xin Longdb876682020-04-10 17:08:24 +080075 if (!xo)
Myungho Jung6ed69182019-03-07 10:23:08 +090076 goto out_reset;
Steffen Klassert7785bba2017-02-15 09:40:00 +010077 }
Steffen Klassertbcd1f8a2017-04-14 10:07:49 +020078
Steffen Klassert7785bba2017-02-15 09:40:00 +010079 xo->flags |= XFRM_GRO;
80
Steffen Klassert172bf002023-10-04 15:05:27 +020081 if (NAPI_GRO_CB(skb)->proto == IPPROTO_UDP)
82 encap_type = UDP_ENCAP_ESPINUDP;
83
Steffen Klassert7785bba2017-02-15 09:40:00 +010084 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
85 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
86 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
87 XFRM_SPI_SKB_CB(skb)->seq = seq;
88
89 /* We don't need to handle errors from xfrm_input, it does all
90 * the error handling and frees the resources on error. */
Steffen Klassert172bf002023-10-04 15:05:27 +020091 xfrm_input(skb, IPPROTO_ESP, spi, encap_type);
Steffen Klassert7785bba2017-02-15 09:40:00 +010092
93 return ERR_PTR(-EINPROGRESS);
Myungho Jung6ed69182019-03-07 10:23:08 +090094out_reset:
95 secpath_reset(skb);
Steffen Klassert7785bba2017-02-15 09:40:00 +010096out:
97 skb_push(skb, offset);
98 NAPI_GRO_CB(skb)->same_flow = 0;
99 NAPI_GRO_CB(skb)->flush = 1;
100
101 return NULL;
102}
103
Steffen Klassert7862b402017-04-14 10:06:50 +0200104static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
105{
106 struct ip_esp_hdr *esph;
107 struct iphdr *iph = ip_hdr(skb);
108 struct xfrm_offload *xo = xfrm_offload(skb);
109 int proto = iph->protocol;
110
111 skb_push(skb, -skb_network_offset(skb));
112 esph = ip_esp_hdr(skb);
113 *skb_mac_header(skb) = IPPROTO_ESP;
114
115 esph->spi = x->id.spi;
116 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
117
118 xo->proto = proto;
119}
120
Florian Westphal7613b922019-03-29 21:16:27 +0100121static struct sk_buff *xfrm4_tunnel_gso_segment(struct xfrm_state *x,
122 struct sk_buff *skb,
123 netdev_features_t features)
124{
Sabrina Dubroca26dbd662022-08-25 17:16:51 +0200125 __be16 type = x->inner_mode.family == AF_INET6 ? htons(ETH_P_IPV6)
126 : htons(ETH_P_IP);
127
128 return skb_eth_gso_segment(skb, features, type);
Florian Westphal7613b922019-03-29 21:16:27 +0100129}
130
131static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x,
132 struct sk_buff *skb,
133 netdev_features_t features)
134{
135 const struct net_offload *ops;
136 struct sk_buff *segs = ERR_PTR(-EINVAL);
137 struct xfrm_offload *xo = xfrm_offload(skb);
138
139 skb->transport_header += x->props.header_len;
140 ops = rcu_dereference(inet_offloads[xo->proto]);
141 if (likely(ops && ops->callbacks.gso_segment))
142 segs = ops->callbacks.gso_segment(skb, features);
143
144 return segs;
145}
146
Xin Long384a46e2020-03-26 17:02:29 +0800147static struct sk_buff *xfrm4_beet_gso_segment(struct xfrm_state *x,
148 struct sk_buff *skb,
149 netdev_features_t features)
150{
151 struct xfrm_offload *xo = xfrm_offload(skb);
152 struct sk_buff *segs = ERR_PTR(-EINVAL);
153 const struct net_offload *ops;
Xin Long6f297062020-04-19 16:11:02 +0800154 u8 proto = xo->proto;
Xin Long384a46e2020-03-26 17:02:29 +0800155
156 skb->transport_header += x->props.header_len;
157
Xin Long3ffb93b2020-05-18 13:35:19 +0800158 if (x->sel.family != AF_INET6) {
159 if (proto == IPPROTO_BEETPH) {
160 struct ip_beet_phdr *ph =
161 (struct ip_beet_phdr *)skb->data;
Xin Long384a46e2020-03-26 17:02:29 +0800162
Xin Long3ffb93b2020-05-18 13:35:19 +0800163 skb->transport_header += ph->hdrlen * 8;
164 proto = ph->nexthdr;
165 } else {
166 skb->transport_header -= IPV4_BEET_PHMAXLEN;
167 }
168 } else {
Xin Long6f297062020-04-19 16:11:02 +0800169 __be16 frag;
170
171 skb->transport_header +=
172 ipv6_skip_exthdr(skb, 0, &proto, &frag);
173 if (proto == IPPROTO_TCP)
174 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
Xin Long384a46e2020-03-26 17:02:29 +0800175 }
176
Steffen Klassert053c8fd2022-03-07 13:11:40 +0100177 if (proto == IPPROTO_IPV6)
178 skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
179
Xin Long384a46e2020-03-26 17:02:29 +0800180 __skb_pull(skb, skb_transport_offset(skb));
181 ops = rcu_dereference(inet_offloads[proto]);
182 if (likely(ops && ops->callbacks.gso_segment))
183 segs = ops->callbacks.gso_segment(skb, features);
184
185 return segs;
186}
187
Florian Westphal7613b922019-03-29 21:16:27 +0100188static struct sk_buff *xfrm4_outer_mode_gso_segment(struct xfrm_state *x,
189 struct sk_buff *skb,
190 netdev_features_t features)
191{
Florian Westphalc9500d72019-03-29 21:16:32 +0100192 switch (x->outer_mode.encap) {
Florian Westphal7613b922019-03-29 21:16:27 +0100193 case XFRM_MODE_TUNNEL:
194 return xfrm4_tunnel_gso_segment(x, skb, features);
195 case XFRM_MODE_TRANSPORT:
196 return xfrm4_transport_gso_segment(x, skb, features);
Xin Long384a46e2020-03-26 17:02:29 +0800197 case XFRM_MODE_BEET:
198 return xfrm4_beet_gso_segment(x, skb, features);
Florian Westphal7613b922019-03-29 21:16:27 +0100199 }
200
201 return ERR_PTR(-EOPNOTSUPP);
202}
203
Steffen Klassert7862b402017-04-14 10:06:50 +0200204static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
205 netdev_features_t features)
206{
Steffen Klassert7862b402017-04-14 10:06:50 +0200207 struct xfrm_state *x;
208 struct ip_esp_hdr *esph;
209 struct crypto_aead *aead;
Steffen Klassert7862b402017-04-14 10:06:50 +0200210 netdev_features_t esp_features = features;
211 struct xfrm_offload *xo = xfrm_offload(skb);
Florian Westphal2294be0f2018-12-18 17:15:20 +0100212 struct sec_path *sp;
Steffen Klassert7862b402017-04-14 10:06:50 +0200213
214 if (!xo)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100215 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200216
Willem de Bruijn121d57a2018-01-19 09:29:18 -0500217 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
David S. Miller5ca11442018-01-23 13:49:06 -0500218 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200219
Florian Westphal2294be0f2018-12-18 17:15:20 +0100220 sp = skb_sec_path(skb);
221 x = sp->xvec[sp->len - 1];
Steffen Klassert7862b402017-04-14 10:06:50 +0200222 aead = x->data;
223 esph = ip_esp_hdr(skb);
224
225 if (esph->spi != x->id.spi)
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100226 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200227
228 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100229 return ERR_PTR(-EINVAL);
Steffen Klassert7862b402017-04-14 10:06:50 +0200230
231 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
232
233 skb->encap_hdr_csum = 1;
234
Boris Pismenny65fd2c22019-03-21 16:41:37 +0200235 if ((!(skb->dev->gso_partial_features & NETIF_F_HW_ESP) &&
236 !(features & NETIF_F_HW_ESP)) || x->xso.dev != skb->dev)
Xin Long154deab2021-03-19 15:35:07 +0800237 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK |
238 NETIF_F_SCTP_CRC);
Boris Pismenny65fd2c22019-03-21 16:41:37 +0200239 else if (!(features & NETIF_F_HW_ESP_TX_CSUM) &&
240 !(skb->dev->gso_partial_features & NETIF_F_HW_ESP_TX_CSUM))
Xin Long154deab2021-03-19 15:35:07 +0800241 esp_features = features & ~(NETIF_F_CSUM_MASK |
242 NETIF_F_SCTP_CRC);
Steffen Klassert7862b402017-04-14 10:06:50 +0200243
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100244 xo->flags |= XFRM_GSO_SEGMENT;
Steffen Klassert7862b402017-04-14 10:06:50 +0200245
Florian Westphal7613b922019-03-29 21:16:27 +0100246 return xfrm4_outer_mode_gso_segment(x, skb, esp_features);
Steffen Klassert7862b402017-04-14 10:06:50 +0200247}
248
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200249static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
250{
251 struct crypto_aead *aead = x->data;
Ilan Tayariec9567a2017-08-01 12:49:04 +0300252 struct xfrm_offload *xo = xfrm_offload(skb);
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200253
254 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
255 return -EINVAL;
256
Ilan Tayariec9567a2017-08-01 12:49:04 +0300257 if (!(xo->flags & CRYPTO_DONE))
258 skb->ip_summed = CHECKSUM_NONE;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200259
260 return esp_input_done2(skb, 0);
261}
262
263static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
264{
265 int err;
266 int alen;
267 int blksize;
268 struct xfrm_offload *xo;
269 struct ip_esp_hdr *esph;
270 struct crypto_aead *aead;
271 struct esp_info esp;
272 bool hw_offload = true;
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100273 __u32 seq;
Mike Yu447bc4b2024-07-12 10:51:25 +0800274 int encap_type = 0;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200275
276 esp.inplace = true;
277
278 xo = xfrm_offload(skb);
279
280 if (!xo)
281 return -EINVAL;
282
Boris Pismenny65fd2c22019-03-21 16:41:37 +0200283 if ((!(features & NETIF_F_HW_ESP) &&
284 !(skb->dev->gso_partial_features & NETIF_F_HW_ESP)) ||
285 x->xso.dev != skb->dev) {
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200286 xo->flags |= CRYPTO_FALLBACK;
287 hw_offload = false;
288 }
289
290 esp.proto = xo->proto;
291
292 /* skb is pure payload to encrypt */
293
294 aead = x->data;
295 alen = crypto_aead_authsize(aead);
296
297 esp.tfclen = 0;
298 /* XXX: Add support for tfc padding here. */
299
300 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
301 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
302 esp.plen = esp.clen - skb->len - esp.tfclen;
303 esp.tailen = esp.tfclen + esp.plen + alen;
304
305 esp.esph = ip_esp_hdr(skb);
306
Mike Yu447bc4b2024-07-12 10:51:25 +0800307 if (x->encap)
308 encap_type = x->encap->encap_type;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200309
Mike Yu447bc4b2024-07-12 10:51:25 +0800310 if (!hw_offload || !skb_is_gso(skb) || (hw_offload && encap_type == UDP_ENCAP_ESPINUDP)) {
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200311 esp.nfrags = esp_output_head(x, skb, &esp);
312 if (esp.nfrags < 0)
313 return esp.nfrags;
314 }
315
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100316 seq = xo->seq.low;
317
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200318 esph = esp.esph;
319 esph->spi = x->id.spi;
320
321 skb_push(skb, -skb_network_offset(skb));
322
323 if (xo->flags & XFRM_GSO_SEGMENT) {
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100324 esph->seq_no = htonl(seq);
325
326 if (!skb_is_gso(skb))
327 xo->seq.low++;
328 else
329 xo->seq.low += skb_shinfo(skb)->gso_segs;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200330 }
331
Christian Langrock4b549cc2022-10-17 08:34:47 +0200332 if (xo->seq.low < seq)
333 xo->seq.hi++;
334
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100335 esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32));
336
Mike Yu447bc4b2024-07-12 10:51:25 +0800337 if (hw_offload && encap_type == UDP_ENCAP_ESPINUDP) {
338 /* In the XFRM stack, the encapsulation protocol is set to iphdr->protocol by
339 * setting *skb_mac_header(skb) (see esp_output_udp_encap()) where skb->mac_header
340 * points to iphdr->protocol (see xfrm4_tunnel_encap_add()).
341 * However, in esp_xmit(), skb->mac_header doesn't point to iphdr->protocol.
342 * Therefore, the protocol field needs to be corrected.
343 */
344 ip_hdr(skb)->protocol = IPPROTO_UDP;
345
346 esph->seq_no = htonl(seq);
347 }
348
Steffen Klassert3dca3f32017-12-20 10:41:31 +0100349 ip_hdr(skb)->tot_len = htons(skb->len);
350 ip_send_check(ip_hdr(skb));
351
Steffen Klassertc7dbf4c2021-03-26 09:44:48 +0100352 if (hw_offload) {
353 if (!skb_ext_add(skb, SKB_EXT_SEC_PATH))
354 return -ENOMEM;
355
356 xo = xfrm_offload(skb);
357 if (!xo)
358 return -EINVAL;
359
360 xo->flags |= XFRM_XMIT;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200361 return 0;
Steffen Klassertc7dbf4c2021-03-26 09:44:48 +0100362 }
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200363
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200364 err = esp_output_tail(x, skb, &esp);
Steffen Klassert4ff03082017-08-07 08:31:07 +0200365 if (err)
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200366 return err;
367
368 secpath_reset(skb);
369
Sebastian Andrzej Siewiorf015b902023-06-14 12:02:02 +0200370 if (skb_needs_linearize(skb, skb->dev->features) &&
371 __skb_linearize(skb))
372 return -ENOMEM;
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200373 return 0;
374}
375
Steffen Klassert7785bba2017-02-15 09:40:00 +0100376static const struct net_offload esp4_offload = {
377 .callbacks = {
378 .gro_receive = esp4_gro_receive,
Steffen Klassert7862b402017-04-14 10:06:50 +0200379 .gso_segment = esp4_gso_segment,
Steffen Klassert7785bba2017-02-15 09:40:00 +0100380 },
381};
382
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200383static const struct xfrm_type_offload esp_type_offload = {
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200384 .owner = THIS_MODULE,
385 .proto = IPPROTO_ESP,
386 .input_tail = esp_input_tail,
387 .xmit = esp_xmit,
Steffen Klassert7862b402017-04-14 10:06:50 +0200388 .encap = esp4_gso_encap,
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200389};
390
Steffen Klassert7785bba2017-02-15 09:40:00 +0100391static int __init esp4_offload_init(void)
392{
Steffen Klassertfca11eb2017-04-14 10:06:33 +0200393 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
394 pr_info("%s: can't add xfrm type offload\n", __func__);
395 return -EAGAIN;
396 }
397
Steffen Klassert7785bba2017-02-15 09:40:00 +0100398 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
399}
400
401static void __exit esp4_offload_exit(void)
402{
Florian Westphal4f518e82019-05-03 17:46:19 +0200403 xfrm_unregister_type_offload(&esp_type_offload, AF_INET);
Steffen Klassert7785bba2017-02-15 09:40:00 +0100404 inet_del_offload(&esp4_offload, IPPROTO_ESP);
405}
406
407module_init(esp4_offload_init);
408module_exit(esp4_offload_exit);
409MODULE_LICENSE("GPL");
410MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
Ilan Tayariffdb5212017-08-01 12:49:08 +0300411MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);
Rob Gill67c20de2020-06-20 02:08:25 +0000412MODULE_DESCRIPTION("IPV4 GSO/GRO offload support");