2 * IPV4 GSO/GRO offload support
3 * Linux INET implementation
5 * Copyright (C) 2016 secunet Security Networks AG
6 * Author: Steffen Klassert <steffen.klassert@secunet.com>
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.
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>
25 #include <linux/scatterlist.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
31 static struct sk_buff **esp4_gro_receive(struct sk_buff **head,
34 int offset = skb_gro_offset(skb);
35 struct xfrm_offload *xo;
41 skb_pull(skb, offset);
43 if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
46 xo = xfrm_offload(skb);
47 if (!xo || !(xo->flags & CRYPTO_DONE)) {
48 err = secpath_set(skb);
52 if (skb->sp->len == XFRM_MAX_DEPTH)
55 x = xfrm_state_lookup(dev_net(skb->dev), skb->mark,
56 (xfrm_address_t *)&ip_hdr(skb)->daddr,
57 spi, IPPROTO_ESP, AF_INET);
61 skb->sp->xvec[skb->sp->len++] = x;
64 xo = xfrm_offload(skb);
71 xo->flags |= XFRM_GRO;
73 XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
74 XFRM_SPI_SKB_CB(skb)->family = AF_INET;
75 XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
76 XFRM_SPI_SKB_CB(skb)->seq = seq;
78 /* We don't need to handle errors from xfrm_input, it does all
79 * the error handling and frees the resources on error. */
80 xfrm_input(skb, IPPROTO_ESP, spi, -2);
82 return ERR_PTR(-EINPROGRESS);
84 skb_push(skb, offset);
85 NAPI_GRO_CB(skb)->same_flow = 0;
86 NAPI_GRO_CB(skb)->flush = 1;
91 static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
93 struct ip_esp_hdr *esph;
94 struct iphdr *iph = ip_hdr(skb);
95 struct xfrm_offload *xo = xfrm_offload(skb);
96 int proto = iph->protocol;
98 skb_push(skb, -skb_network_offset(skb));
99 esph = ip_esp_hdr(skb);
100 *skb_mac_header(skb) = IPPROTO_ESP;
102 esph->spi = x->id.spi;
103 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
108 static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
109 netdev_features_t features)
113 struct sk_buff *skb2;
114 struct xfrm_state *x;
115 struct ip_esp_hdr *esph;
116 struct crypto_aead *aead;
117 struct sk_buff *segs = ERR_PTR(-EINVAL);
118 netdev_features_t esp_features = features;
119 struct xfrm_offload *xo = xfrm_offload(skb);
126 x = skb->sp->xvec[skb->sp->len - 1];
128 esph = ip_esp_hdr(skb);
130 if (esph->spi != x->id.spi)
133 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
136 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
138 skb->encap_hdr_csum = 1;
140 if (!(features & NETIF_F_HW_ESP))
141 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
143 segs = x->outer_mode->gso_segment(x, skb, esp_features);
144 if (IS_ERR_OR_NULL(segs))
147 __skb_pull(skb, skb->data - skb_mac_header(skb));
151 struct sk_buff *nskb = skb2->next;
153 xo = xfrm_offload(skb2);
154 xo->flags |= XFRM_GSO_SEGMENT;
156 xo->seq.hi = xfrm_replay_seqhi(x, seq);
158 if(!(features & NETIF_F_HW_ESP))
159 xo->flags |= CRYPTO_FALLBACK;
161 x->outer_mode->xmit(x, skb2);
163 err = x->type_offload->xmit(x, skb2, esp_features);
165 kfree_skb_list(segs);
169 if (!skb_is_gso(skb2))
172 seq += skb_shinfo(skb2)->gso_segs;
174 skb_push(skb2, skb2->mac_len);
182 static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
184 struct crypto_aead *aead = x->data;
185 struct xfrm_offload *xo = xfrm_offload(skb);
187 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
190 if (!(xo->flags & CRYPTO_DONE))
191 skb->ip_summed = CHECKSUM_NONE;
193 return esp_input_done2(skb, 0);
196 static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
201 struct xfrm_offload *xo;
202 struct ip_esp_hdr *esph;
203 struct crypto_aead *aead;
205 bool hw_offload = true;
209 xo = xfrm_offload(skb);
214 if (!(features & NETIF_F_HW_ESP) || !x->xso.offload_handle ||
215 (x->xso.dev != skb->dev)) {
216 xo->flags |= CRYPTO_FALLBACK;
220 esp.proto = xo->proto;
222 /* skb is pure payload to encrypt */
225 alen = crypto_aead_authsize(aead);
228 /* XXX: Add support for tfc padding here. */
230 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
231 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
232 esp.plen = esp.clen - skb->len - esp.tfclen;
233 esp.tailen = esp.tfclen + esp.plen + alen;
235 esp.esph = ip_esp_hdr(skb);
238 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
239 esp.nfrags = esp_output_head(x, skb, &esp);
245 esph->spi = x->id.spi;
247 skb_push(skb, -skb_network_offset(skb));
249 if (xo->flags & XFRM_GSO_SEGMENT) {
250 esph->seq_no = htonl(xo->seq.low);
252 ip_hdr(skb)->tot_len = htons(skb->len);
253 ip_send_check(ip_hdr(skb));
259 esp.seqno = cpu_to_be64(xo->seq.low + ((u64)xo->seq.hi << 32));
261 err = esp_output_tail(x, skb, &esp);
270 static const struct net_offload esp4_offload = {
272 .gro_receive = esp4_gro_receive,
273 .gso_segment = esp4_gso_segment,
277 static const struct xfrm_type_offload esp_type_offload = {
278 .description = "ESP4 OFFLOAD",
279 .owner = THIS_MODULE,
280 .proto = IPPROTO_ESP,
281 .input_tail = esp_input_tail,
283 .encap = esp4_gso_encap,
286 static int __init esp4_offload_init(void)
288 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
289 pr_info("%s: can't add xfrm type offload\n", __func__);
293 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
296 static void __exit esp4_offload_exit(void)
298 if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0)
299 pr_info("%s: can't remove xfrm type offload\n", __func__);
301 inet_del_offload(&esp4_offload, IPPROTO_ESP);
304 module_init(esp4_offload_init);
305 module_exit(esp4_offload_exit);
306 MODULE_LICENSE("GPL");
307 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
308 MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);