1 // SPDX-License-Identifier: GPL-2.0-only
3 * IPV4 GSO/GRO offload support
4 * Linux INET implementation
6 * Copyright (C) 2016 secunet Security Networks AG
7 * Author: Steffen Klassert <steffen.klassert@secunet.com>
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>
22 #include <linux/scatterlist.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
28 static struct sk_buff
*esp4_gro_receive(struct list_head
*head
,
31 int offset
= skb_gro_offset(skb
);
32 struct xfrm_offload
*xo
;
38 if (!pskb_pull(skb
, offset
))
41 if ((err
= xfrm_parse_spi(skb
, IPPROTO_ESP
, &spi
, &seq
)) != 0)
44 xo
= xfrm_offload(skb
);
45 if (!xo
|| !(xo
->flags
& CRYPTO_DONE
)) {
46 struct sec_path
*sp
= secpath_set(skb
);
51 if (sp
->len
== XFRM_MAX_DEPTH
)
54 x
= xfrm_state_lookup(dev_net(skb
->dev
), skb
->mark
,
55 (xfrm_address_t
*)&ip_hdr(skb
)->daddr
,
56 spi
, IPPROTO_ESP
, AF_INET
);
60 sp
->xvec
[sp
->len
++] = x
;
63 xo
= xfrm_offload(skb
);
70 xo
->flags
|= XFRM_GRO
;
72 XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
= NULL
;
73 XFRM_SPI_SKB_CB(skb
)->family
= AF_INET
;
74 XFRM_SPI_SKB_CB(skb
)->daddroff
= offsetof(struct iphdr
, daddr
);
75 XFRM_SPI_SKB_CB(skb
)->seq
= seq
;
77 /* We don't need to handle errors from xfrm_input, it does all
78 * the error handling and frees the resources on error. */
79 xfrm_input(skb
, IPPROTO_ESP
, spi
, -2);
81 return ERR_PTR(-EINPROGRESS
);
85 skb_push(skb
, offset
);
86 NAPI_GRO_CB(skb
)->same_flow
= 0;
87 NAPI_GRO_CB(skb
)->flush
= 1;
92 static void esp4_gso_encap(struct xfrm_state
*x
, struct sk_buff
*skb
)
94 struct ip_esp_hdr
*esph
;
95 struct iphdr
*iph
= ip_hdr(skb
);
96 struct xfrm_offload
*xo
= xfrm_offload(skb
);
97 int proto
= iph
->protocol
;
99 skb_push(skb
, -skb_network_offset(skb
));
100 esph
= ip_esp_hdr(skb
);
101 *skb_mac_header(skb
) = IPPROTO_ESP
;
103 esph
->spi
= x
->id
.spi
;
104 esph
->seq_no
= htonl(XFRM_SKB_CB(skb
)->seq
.output
.low
);
109 static struct sk_buff
*xfrm4_tunnel_gso_segment(struct xfrm_state
*x
,
111 netdev_features_t features
)
113 __skb_push(skb
, skb
->mac_len
);
114 return skb_mac_gso_segment(skb
, features
);
117 static struct sk_buff
*xfrm4_transport_gso_segment(struct xfrm_state
*x
,
119 netdev_features_t features
)
121 const struct net_offload
*ops
;
122 struct sk_buff
*segs
= ERR_PTR(-EINVAL
);
123 struct xfrm_offload
*xo
= xfrm_offload(skb
);
125 skb
->transport_header
+= x
->props
.header_len
;
126 ops
= rcu_dereference(inet_offloads
[xo
->proto
]);
127 if (likely(ops
&& ops
->callbacks
.gso_segment
))
128 segs
= ops
->callbacks
.gso_segment(skb
, features
);
133 static struct sk_buff
*xfrm4_outer_mode_gso_segment(struct xfrm_state
*x
,
135 netdev_features_t features
)
137 switch (x
->outer_mode
.encap
) {
138 case XFRM_MODE_TUNNEL
:
139 return xfrm4_tunnel_gso_segment(x
, skb
, features
);
140 case XFRM_MODE_TRANSPORT
:
141 return xfrm4_transport_gso_segment(x
, skb
, features
);
144 return ERR_PTR(-EOPNOTSUPP
);
147 static struct sk_buff
*esp4_gso_segment(struct sk_buff
*skb
,
148 netdev_features_t features
)
150 struct xfrm_state
*x
;
151 struct ip_esp_hdr
*esph
;
152 struct crypto_aead
*aead
;
153 netdev_features_t esp_features
= features
;
154 struct xfrm_offload
*xo
= xfrm_offload(skb
);
158 return ERR_PTR(-EINVAL
);
160 if (!(skb_shinfo(skb
)->gso_type
& SKB_GSO_ESP
))
161 return ERR_PTR(-EINVAL
);
163 sp
= skb_sec_path(skb
);
164 x
= sp
->xvec
[sp
->len
- 1];
166 esph
= ip_esp_hdr(skb
);
168 if (esph
->spi
!= x
->id
.spi
)
169 return ERR_PTR(-EINVAL
);
171 if (!pskb_may_pull(skb
, sizeof(*esph
) + crypto_aead_ivsize(aead
)))
172 return ERR_PTR(-EINVAL
);
174 __skb_pull(skb
, sizeof(*esph
) + crypto_aead_ivsize(aead
));
176 skb
->encap_hdr_csum
= 1;
178 if ((!(skb
->dev
->gso_partial_features
& NETIF_F_HW_ESP
) &&
179 !(features
& NETIF_F_HW_ESP
)) || x
->xso
.dev
!= skb
->dev
)
180 esp_features
= features
& ~(NETIF_F_SG
| NETIF_F_CSUM_MASK
);
181 else if (!(features
& NETIF_F_HW_ESP_TX_CSUM
) &&
182 !(skb
->dev
->gso_partial_features
& NETIF_F_HW_ESP_TX_CSUM
))
183 esp_features
= features
& ~NETIF_F_CSUM_MASK
;
185 xo
->flags
|= XFRM_GSO_SEGMENT
;
187 return xfrm4_outer_mode_gso_segment(x
, skb
, esp_features
);
190 static int esp_input_tail(struct xfrm_state
*x
, struct sk_buff
*skb
)
192 struct crypto_aead
*aead
= x
->data
;
193 struct xfrm_offload
*xo
= xfrm_offload(skb
);
195 if (!pskb_may_pull(skb
, sizeof(struct ip_esp_hdr
) + crypto_aead_ivsize(aead
)))
198 if (!(xo
->flags
& CRYPTO_DONE
))
199 skb
->ip_summed
= CHECKSUM_NONE
;
201 return esp_input_done2(skb
, 0);
204 static int esp_xmit(struct xfrm_state
*x
, struct sk_buff
*skb
, netdev_features_t features
)
209 struct xfrm_offload
*xo
;
210 struct ip_esp_hdr
*esph
;
211 struct crypto_aead
*aead
;
213 bool hw_offload
= true;
218 xo
= xfrm_offload(skb
);
223 if ((!(features
& NETIF_F_HW_ESP
) &&
224 !(skb
->dev
->gso_partial_features
& NETIF_F_HW_ESP
)) ||
225 x
->xso
.dev
!= skb
->dev
) {
226 xo
->flags
|= CRYPTO_FALLBACK
;
230 esp
.proto
= xo
->proto
;
232 /* skb is pure payload to encrypt */
235 alen
= crypto_aead_authsize(aead
);
238 /* XXX: Add support for tfc padding here. */
240 blksize
= ALIGN(crypto_aead_blocksize(aead
), 4);
241 esp
.clen
= ALIGN(skb
->len
+ 2 + esp
.tfclen
, blksize
);
242 esp
.plen
= esp
.clen
- skb
->len
- esp
.tfclen
;
243 esp
.tailen
= esp
.tfclen
+ esp
.plen
+ alen
;
245 esp
.esph
= ip_esp_hdr(skb
);
248 if (!hw_offload
|| (hw_offload
&& !skb_is_gso(skb
))) {
249 esp
.nfrags
= esp_output_head(x
, skb
, &esp
);
257 esph
->spi
= x
->id
.spi
;
259 skb_push(skb
, -skb_network_offset(skb
));
261 if (xo
->flags
& XFRM_GSO_SEGMENT
) {
262 esph
->seq_no
= htonl(seq
);
264 if (!skb_is_gso(skb
))
267 xo
->seq
.low
+= skb_shinfo(skb
)->gso_segs
;
270 esp
.seqno
= cpu_to_be64(seq
+ ((u64
)xo
->seq
.hi
<< 32));
272 ip_hdr(skb
)->tot_len
= htons(skb
->len
);
273 ip_send_check(ip_hdr(skb
));
278 err
= esp_output_tail(x
, skb
, &esp
);
287 static const struct net_offload esp4_offload
= {
289 .gro_receive
= esp4_gro_receive
,
290 .gso_segment
= esp4_gso_segment
,
294 static const struct xfrm_type_offload esp_type_offload
= {
295 .description
= "ESP4 OFFLOAD",
296 .owner
= THIS_MODULE
,
297 .proto
= IPPROTO_ESP
,
298 .input_tail
= esp_input_tail
,
300 .encap
= esp4_gso_encap
,
303 static int __init
esp4_offload_init(void)
305 if (xfrm_register_type_offload(&esp_type_offload
, AF_INET
) < 0) {
306 pr_info("%s: can't add xfrm type offload\n", __func__
);
310 return inet_add_offload(&esp4_offload
, IPPROTO_ESP
);
313 static void __exit
esp4_offload_exit(void)
315 xfrm_unregister_type_offload(&esp_type_offload
, AF_INET
);
316 inet_del_offload(&esp4_offload
, IPPROTO_ESP
);
319 module_init(esp4_offload_init
);
320 module_exit(esp4_offload_exit
);
321 MODULE_LICENSE("GPL");
322 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
323 MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET
, XFRM_PROTO_ESP
);