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 list_head *head,
34 int offset = skb_gro_offset(skb);
35 struct xfrm_offload *xo;
41 if (!pskb_pull(skb, offset))
44 if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
47 xo = xfrm_offload(skb);
48 if (!xo || !(xo->flags & CRYPTO_DONE)) {
49 struct sec_path *sp = secpath_set(skb);
54 if (sp->len == XFRM_MAX_DEPTH)
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);
63 sp->xvec[sp->len++] = x;
66 xo = xfrm_offload(skb);
73 xo->flags |= XFRM_GRO;
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;
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);
84 return ERR_PTR(-EINPROGRESS);
88 skb_push(skb, offset);
89 NAPI_GRO_CB(skb)->same_flow = 0;
90 NAPI_GRO_CB(skb)->flush = 1;
95 static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
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;
102 skb_push(skb, -skb_network_offset(skb));
103 esph = ip_esp_hdr(skb);
104 *skb_mac_header(skb) = IPPROTO_ESP;
106 esph->spi = x->id.spi;
107 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
112 static struct sk_buff *xfrm4_tunnel_gso_segment(struct xfrm_state *x,
114 netdev_features_t features)
116 __skb_push(skb, skb->mac_len);
117 return skb_mac_gso_segment(skb, features);
120 static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x,
122 netdev_features_t features)
124 const struct net_offload *ops;
125 struct sk_buff *segs = ERR_PTR(-EINVAL);
126 struct xfrm_offload *xo = xfrm_offload(skb);
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);
136 static struct sk_buff *xfrm4_outer_mode_gso_segment(struct xfrm_state *x,
138 netdev_features_t features)
140 switch (x->outer_mode.encap) {
141 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);
147 return ERR_PTR(-EOPNOTSUPP);
150 static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
151 netdev_features_t features)
153 struct xfrm_state *x;
154 struct ip_esp_hdr *esph;
155 struct crypto_aead *aead;
156 netdev_features_t esp_features = features;
157 struct xfrm_offload *xo = xfrm_offload(skb);
161 return ERR_PTR(-EINVAL);
163 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
164 return ERR_PTR(-EINVAL);
166 sp = skb_sec_path(skb);
167 x = sp->xvec[sp->len - 1];
169 esph = ip_esp_hdr(skb);
171 if (esph->spi != x->id.spi)
172 return ERR_PTR(-EINVAL);
174 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
175 return ERR_PTR(-EINVAL);
177 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
179 skb->encap_hdr_csum = 1;
181 if ((!(skb->dev->gso_partial_features & NETIF_F_HW_ESP) &&
182 !(features & NETIF_F_HW_ESP)) || x->xso.dev != skb->dev)
183 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
184 else if (!(features & NETIF_F_HW_ESP_TX_CSUM) &&
185 !(skb->dev->gso_partial_features & NETIF_F_HW_ESP_TX_CSUM))
186 esp_features = features & ~NETIF_F_CSUM_MASK;
188 xo->flags |= XFRM_GSO_SEGMENT;
190 return xfrm4_outer_mode_gso_segment(x, skb, esp_features);
193 static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
195 struct crypto_aead *aead = x->data;
196 struct xfrm_offload *xo = xfrm_offload(skb);
198 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
201 if (!(xo->flags & CRYPTO_DONE))
202 skb->ip_summed = CHECKSUM_NONE;
204 return esp_input_done2(skb, 0);
207 static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
212 struct xfrm_offload *xo;
213 struct ip_esp_hdr *esph;
214 struct crypto_aead *aead;
216 bool hw_offload = true;
221 xo = xfrm_offload(skb);
226 if ((!(features & NETIF_F_HW_ESP) &&
227 !(skb->dev->gso_partial_features & NETIF_F_HW_ESP)) ||
228 x->xso.dev != skb->dev) {
229 xo->flags |= CRYPTO_FALLBACK;
233 esp.proto = xo->proto;
235 /* skb is pure payload to encrypt */
238 alen = crypto_aead_authsize(aead);
241 /* XXX: Add support for tfc padding here. */
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;
248 esp.esph = ip_esp_hdr(skb);
251 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
252 esp.nfrags = esp_output_head(x, skb, &esp);
260 esph->spi = x->id.spi;
262 skb_push(skb, -skb_network_offset(skb));
264 if (xo->flags & XFRM_GSO_SEGMENT) {
265 esph->seq_no = htonl(seq);
267 if (!skb_is_gso(skb))
270 xo->seq.low += skb_shinfo(skb)->gso_segs;
273 esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32));
275 ip_hdr(skb)->tot_len = htons(skb->len);
276 ip_send_check(ip_hdr(skb));
281 err = esp_output_tail(x, skb, &esp);
290 static const struct net_offload esp4_offload = {
292 .gro_receive = esp4_gro_receive,
293 .gso_segment = esp4_gso_segment,
297 static 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,
303 .encap = esp4_gso_encap,
306 static int __init esp4_offload_init(void)
308 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
309 pr_info("%s: can't add xfrm type offload\n", __func__);
313 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
316 static void __exit esp4_offload_exit(void)
318 if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0)
319 pr_info("%s: can't remove xfrm type offload\n", __func__);
321 inet_del_offload(&esp4_offload, IPPROTO_ESP);
324 module_init(esp4_offload_init);
325 module_exit(esp4_offload_exit);
326 MODULE_LICENSE("GPL");
327 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
328 MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);