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 skb
->mark
= xfrm_smark_get(skb
->mark
, x
);
62 sp
->xvec
[sp
->len
++] = x
;
65 xo
= xfrm_offload(skb
);
72 xo
->flags
|= XFRM_GRO
;
74 XFRM_TUNNEL_SKB_CB(skb
)->tunnel
.ip4
= NULL
;
75 XFRM_SPI_SKB_CB(skb
)->family
= AF_INET
;
76 XFRM_SPI_SKB_CB(skb
)->daddroff
= offsetof(struct iphdr
, daddr
);
77 XFRM_SPI_SKB_CB(skb
)->seq
= seq
;
79 /* We don't need to handle errors from xfrm_input, it does all
80 * the error handling and frees the resources on error. */
81 xfrm_input(skb
, IPPROTO_ESP
, spi
, -2);
83 return ERR_PTR(-EINPROGRESS
);
87 skb_push(skb
, offset
);
88 NAPI_GRO_CB(skb
)->same_flow
= 0;
89 NAPI_GRO_CB(skb
)->flush
= 1;
94 static void esp4_gso_encap(struct xfrm_state
*x
, struct sk_buff
*skb
)
96 struct ip_esp_hdr
*esph
;
97 struct iphdr
*iph
= ip_hdr(skb
);
98 struct xfrm_offload
*xo
= xfrm_offload(skb
);
99 int proto
= iph
->protocol
;
101 skb_push(skb
, -skb_network_offset(skb
));
102 esph
= ip_esp_hdr(skb
);
103 *skb_mac_header(skb
) = IPPROTO_ESP
;
105 esph
->spi
= x
->id
.spi
;
106 esph
->seq_no
= htonl(XFRM_SKB_CB(skb
)->seq
.output
.low
);
111 static struct sk_buff
*xfrm4_tunnel_gso_segment(struct xfrm_state
*x
,
113 netdev_features_t features
)
115 __skb_push(skb
, skb
->mac_len
);
116 return skb_mac_gso_segment(skb
, features
);
119 static struct sk_buff
*xfrm4_transport_gso_segment(struct xfrm_state
*x
,
121 netdev_features_t features
)
123 const struct net_offload
*ops
;
124 struct sk_buff
*segs
= ERR_PTR(-EINVAL
);
125 struct xfrm_offload
*xo
= xfrm_offload(skb
);
127 skb
->transport_header
+= x
->props
.header_len
;
128 ops
= rcu_dereference(inet_offloads
[xo
->proto
]);
129 if (likely(ops
&& ops
->callbacks
.gso_segment
))
130 segs
= ops
->callbacks
.gso_segment(skb
, features
);
135 static struct sk_buff
*xfrm4_outer_mode_gso_segment(struct xfrm_state
*x
,
137 netdev_features_t features
)
139 switch (x
->outer_mode
.encap
) {
140 case XFRM_MODE_TUNNEL
:
141 return xfrm4_tunnel_gso_segment(x
, skb
, features
);
142 case XFRM_MODE_TRANSPORT
:
143 return xfrm4_transport_gso_segment(x
, skb
, features
);
146 return ERR_PTR(-EOPNOTSUPP
);
149 static struct sk_buff
*esp4_gso_segment(struct sk_buff
*skb
,
150 netdev_features_t features
)
152 struct xfrm_state
*x
;
153 struct ip_esp_hdr
*esph
;
154 struct crypto_aead
*aead
;
155 netdev_features_t esp_features
= features
;
156 struct xfrm_offload
*xo
= xfrm_offload(skb
);
160 return ERR_PTR(-EINVAL
);
162 if (!(skb_shinfo(skb
)->gso_type
& SKB_GSO_ESP
))
163 return ERR_PTR(-EINVAL
);
165 sp
= skb_sec_path(skb
);
166 x
= sp
->xvec
[sp
->len
- 1];
168 esph
= ip_esp_hdr(skb
);
170 if (esph
->spi
!= x
->id
.spi
)
171 return ERR_PTR(-EINVAL
);
173 if (!pskb_may_pull(skb
, sizeof(*esph
) + crypto_aead_ivsize(aead
)))
174 return ERR_PTR(-EINVAL
);
176 __skb_pull(skb
, sizeof(*esph
) + crypto_aead_ivsize(aead
));
178 skb
->encap_hdr_csum
= 1;
180 if ((!(skb
->dev
->gso_partial_features
& NETIF_F_HW_ESP
) &&
181 !(features
& NETIF_F_HW_ESP
)) || x
->xso
.dev
!= skb
->dev
)
182 esp_features
= features
& ~(NETIF_F_SG
| NETIF_F_CSUM_MASK
);
183 else if (!(features
& NETIF_F_HW_ESP_TX_CSUM
) &&
184 !(skb
->dev
->gso_partial_features
& NETIF_F_HW_ESP_TX_CSUM
))
185 esp_features
= features
& ~NETIF_F_CSUM_MASK
;
187 xo
->flags
|= XFRM_GSO_SEGMENT
;
189 return xfrm4_outer_mode_gso_segment(x
, skb
, esp_features
);
192 static int esp_input_tail(struct xfrm_state
*x
, struct sk_buff
*skb
)
194 struct crypto_aead
*aead
= x
->data
;
195 struct xfrm_offload
*xo
= xfrm_offload(skb
);
197 if (!pskb_may_pull(skb
, sizeof(struct ip_esp_hdr
) + crypto_aead_ivsize(aead
)))
200 if (!(xo
->flags
& CRYPTO_DONE
))
201 skb
->ip_summed
= CHECKSUM_NONE
;
203 return esp_input_done2(skb
, 0);
206 static int esp_xmit(struct xfrm_state
*x
, struct sk_buff
*skb
, netdev_features_t features
)
211 struct xfrm_offload
*xo
;
212 struct ip_esp_hdr
*esph
;
213 struct crypto_aead
*aead
;
215 bool hw_offload
= true;
220 xo
= xfrm_offload(skb
);
225 if ((!(features
& NETIF_F_HW_ESP
) &&
226 !(skb
->dev
->gso_partial_features
& NETIF_F_HW_ESP
)) ||
227 x
->xso
.dev
!= skb
->dev
) {
228 xo
->flags
|= CRYPTO_FALLBACK
;
232 esp
.proto
= xo
->proto
;
234 /* skb is pure payload to encrypt */
237 alen
= crypto_aead_authsize(aead
);
240 /* XXX: Add support for tfc padding here. */
242 blksize
= ALIGN(crypto_aead_blocksize(aead
), 4);
243 esp
.clen
= ALIGN(skb
->len
+ 2 + esp
.tfclen
, blksize
);
244 esp
.plen
= esp
.clen
- skb
->len
- esp
.tfclen
;
245 esp
.tailen
= esp
.tfclen
+ esp
.plen
+ alen
;
247 esp
.esph
= ip_esp_hdr(skb
);
250 if (!hw_offload
|| (hw_offload
&& !skb_is_gso(skb
))) {
251 esp
.nfrags
= esp_output_head(x
, skb
, &esp
);
259 esph
->spi
= x
->id
.spi
;
261 skb_push(skb
, -skb_network_offset(skb
));
263 if (xo
->flags
& XFRM_GSO_SEGMENT
) {
264 esph
->seq_no
= htonl(seq
);
266 if (!skb_is_gso(skb
))
269 xo
->seq
.low
+= skb_shinfo(skb
)->gso_segs
;
272 esp
.seqno
= cpu_to_be64(seq
+ ((u64
)xo
->seq
.hi
<< 32));
274 ip_hdr(skb
)->tot_len
= htons(skb
->len
);
275 ip_send_check(ip_hdr(skb
));
280 err
= esp_output_tail(x
, skb
, &esp
);
289 static const struct net_offload esp4_offload
= {
291 .gro_receive
= esp4_gro_receive
,
292 .gso_segment
= esp4_gso_segment
,
296 static const struct xfrm_type_offload esp_type_offload
= {
297 .description
= "ESP4 OFFLOAD",
298 .owner
= THIS_MODULE
,
299 .proto
= IPPROTO_ESP
,
300 .input_tail
= esp_input_tail
,
302 .encap
= esp4_gso_encap
,
305 static int __init
esp4_offload_init(void)
307 if (xfrm_register_type_offload(&esp_type_offload
, AF_INET
) < 0) {
308 pr_info("%s: can't add xfrm type offload\n", __func__
);
312 return inet_add_offload(&esp4_offload
, IPPROTO_ESP
);
315 static void __exit
esp4_offload_exit(void)
317 xfrm_unregister_type_offload(&esp_type_offload
, AF_INET
);
318 inet_del_offload(&esp4_offload
, IPPROTO_ESP
);
321 module_init(esp4_offload_init
);
322 module_exit(esp4_offload_exit
);
323 MODULE_LICENSE("GPL");
324 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
325 MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET
, XFRM_PROTO_ESP
);