xref: /linux/net/ipv4/esp4_offload.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
175a6faf6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27785bba2SSteffen Klassert /*
37785bba2SSteffen Klassert  * IPV4 GSO/GRO offload support
47785bba2SSteffen Klassert  * Linux INET implementation
57785bba2SSteffen Klassert  *
67785bba2SSteffen Klassert  * Copyright (C) 2016 secunet Security Networks AG
77785bba2SSteffen Klassert  * Author: Steffen Klassert <steffen.klassert@secunet.com>
87785bba2SSteffen Klassert  *
97785bba2SSteffen Klassert  * ESP GRO support
107785bba2SSteffen Klassert  */
117785bba2SSteffen Klassert 
127785bba2SSteffen Klassert #include <linux/skbuff.h>
137785bba2SSteffen Klassert #include <linux/init.h>
147785bba2SSteffen Klassert #include <net/protocol.h>
157785bba2SSteffen Klassert #include <crypto/aead.h>
167785bba2SSteffen Klassert #include <crypto/authenc.h>
177785bba2SSteffen Klassert #include <linux/err.h>
187785bba2SSteffen Klassert #include <linux/module.h>
194721031cSEric Dumazet #include <net/gro.h>
20d457a0e3SEric Dumazet #include <net/gso.h>
217785bba2SSteffen Klassert #include <net/ip.h>
227785bba2SSteffen Klassert #include <net/xfrm.h>
237785bba2SSteffen Klassert #include <net/esp.h>
247785bba2SSteffen Klassert #include <linux/scatterlist.h>
257785bba2SSteffen Klassert #include <linux/kernel.h>
267785bba2SSteffen Klassert #include <linux/slab.h>
277785bba2SSteffen Klassert #include <linux/spinlock.h>
287785bba2SSteffen Klassert #include <net/udp.h>
297785bba2SSteffen Klassert 
esp4_gro_receive(struct list_head * head,struct sk_buff * skb)30d4546c25SDavid Miller static struct sk_buff *esp4_gro_receive(struct list_head *head,
317785bba2SSteffen Klassert 					struct sk_buff *skb)
327785bba2SSteffen Klassert {
337785bba2SSteffen Klassert 	int offset = skb_gro_offset(skb);
347785bba2SSteffen Klassert 	struct xfrm_offload *xo;
357785bba2SSteffen Klassert 	struct xfrm_state *x;
36172bf009SSteffen Klassert 	int encap_type = 0;
377785bba2SSteffen Klassert 	__be32 seq;
387785bba2SSteffen Klassert 	__be32 spi;
397785bba2SSteffen Klassert 
40374d1b5aSSteffen Klassert 	if (!pskb_pull(skb, offset))
41374d1b5aSSteffen Klassert 		return NULL;
427785bba2SSteffen Klassert 
43335a2a1fSYang Li 	if (xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq) != 0)
447785bba2SSteffen Klassert 		goto out;
457785bba2SSteffen Klassert 
46bcd1f8a4SSteffen Klassert 	xo = xfrm_offload(skb);
47bcd1f8a4SSteffen Klassert 	if (!xo || !(xo->flags & CRYPTO_DONE)) {
480ca64da1SFlorian Westphal 		struct sec_path *sp = secpath_set(skb);
490ca64da1SFlorian Westphal 
500ca64da1SFlorian Westphal 		if (!sp)
517785bba2SSteffen Klassert 			goto out;
527785bba2SSteffen Klassert 
530ca64da1SFlorian Westphal 		if (sp->len == XFRM_MAX_DEPTH)
546ed69184SMyungho Jung 			goto out_reset;
557785bba2SSteffen Klassert 
567785bba2SSteffen Klassert 		x = xfrm_state_lookup(dev_net(skb->dev), skb->mark,
577785bba2SSteffen Klassert 				      (xfrm_address_t *)&ip_hdr(skb)->daddr,
587785bba2SSteffen Klassert 				      spi, IPPROTO_ESP, AF_INET);
5915f5fe9eSAntony Antony 
6015f5fe9eSAntony Antony 		if (unlikely(x && x->dir && x->dir != XFRM_SA_DIR_IN)) {
6115f5fe9eSAntony Antony 			/* non-offload path will record the error and audit log */
6215f5fe9eSAntony Antony 			xfrm_state_put(x);
6315f5fe9eSAntony Antony 			x = NULL;
6415f5fe9eSAntony Antony 		}
6515f5fe9eSAntony Antony 
667785bba2SSteffen Klassert 		if (!x)
676ed69184SMyungho Jung 			goto out_reset;
687785bba2SSteffen Klassert 
694e4362d2SUlrich Weber 		skb->mark = xfrm_smark_get(skb->mark, x);
704e4362d2SUlrich Weber 
710ca64da1SFlorian Westphal 		sp->xvec[sp->len++] = x;
720ca64da1SFlorian Westphal 		sp->olen++;
737785bba2SSteffen Klassert 
747785bba2SSteffen Klassert 		xo = xfrm_offload(skb);
75db87668aSXin Long 		if (!xo)
766ed69184SMyungho Jung 			goto out_reset;
777785bba2SSteffen Klassert 	}
78bcd1f8a4SSteffen Klassert 
797785bba2SSteffen Klassert 	xo->flags |= XFRM_GRO;
807785bba2SSteffen Klassert 
81172bf009SSteffen Klassert 	if (NAPI_GRO_CB(skb)->proto == IPPROTO_UDP)
82172bf009SSteffen Klassert 		encap_type = UDP_ENCAP_ESPINUDP;
83172bf009SSteffen Klassert 
847785bba2SSteffen Klassert 	XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
857785bba2SSteffen Klassert 	XFRM_SPI_SKB_CB(skb)->family = AF_INET;
867785bba2SSteffen Klassert 	XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
877785bba2SSteffen Klassert 	XFRM_SPI_SKB_CB(skb)->seq = seq;
887785bba2SSteffen Klassert 
897785bba2SSteffen Klassert 	/* We don't need to handle errors from xfrm_input, it does all
907785bba2SSteffen Klassert 	 * the error handling and frees the resources on error. */
91172bf009SSteffen Klassert 	xfrm_input(skb, IPPROTO_ESP, spi, encap_type);
927785bba2SSteffen Klassert 
937785bba2SSteffen Klassert 	return ERR_PTR(-EINPROGRESS);
946ed69184SMyungho Jung out_reset:
956ed69184SMyungho Jung 	secpath_reset(skb);
967785bba2SSteffen Klassert out:
977785bba2SSteffen Klassert 	skb_push(skb, offset);
987785bba2SSteffen Klassert 	NAPI_GRO_CB(skb)->same_flow = 0;
997785bba2SSteffen Klassert 	NAPI_GRO_CB(skb)->flush = 1;
1007785bba2SSteffen Klassert 
1017785bba2SSteffen Klassert 	return NULL;
1027785bba2SSteffen Klassert }
1037785bba2SSteffen Klassert 
esp4_gso_encap(struct xfrm_state * x,struct sk_buff * skb)1047862b405SSteffen Klassert static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
1057862b405SSteffen Klassert {
1067862b405SSteffen Klassert 	struct ip_esp_hdr *esph;
1077862b405SSteffen Klassert 	struct iphdr *iph = ip_hdr(skb);
1087862b405SSteffen Klassert 	struct xfrm_offload *xo = xfrm_offload(skb);
1097862b405SSteffen Klassert 	int proto = iph->protocol;
1107862b405SSteffen Klassert 
1117862b405SSteffen Klassert 	skb_push(skb, -skb_network_offset(skb));
1127862b405SSteffen Klassert 	esph = ip_esp_hdr(skb);
1137862b405SSteffen Klassert 	*skb_mac_header(skb) = IPPROTO_ESP;
1147862b405SSteffen Klassert 
1157862b405SSteffen Klassert 	esph->spi = x->id.spi;
1167862b405SSteffen Klassert 	esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
1177862b405SSteffen Klassert 
1187862b405SSteffen Klassert 	xo->proto = proto;
1197862b405SSteffen Klassert }
1207862b405SSteffen Klassert 
xfrm4_tunnel_gso_segment(struct xfrm_state * x,struct sk_buff * skb,netdev_features_t features)1217613b92bSFlorian Westphal static struct sk_buff *xfrm4_tunnel_gso_segment(struct xfrm_state *x,
1227613b92bSFlorian Westphal 						struct sk_buff *skb,
1237613b92bSFlorian Westphal 						netdev_features_t features)
1247613b92bSFlorian Westphal {
12526dbd66eSSabrina Dubroca 	__be16 type = x->inner_mode.family == AF_INET6 ? htons(ETH_P_IPV6)
12626dbd66eSSabrina Dubroca 						       : htons(ETH_P_IP);
12726dbd66eSSabrina Dubroca 
12826dbd66eSSabrina Dubroca 	return skb_eth_gso_segment(skb, features, type);
1297613b92bSFlorian Westphal }
1307613b92bSFlorian Westphal 
xfrm4_transport_gso_segment(struct xfrm_state * x,struct sk_buff * skb,netdev_features_t features)1317613b92bSFlorian Westphal static struct sk_buff *xfrm4_transport_gso_segment(struct xfrm_state *x,
1327613b92bSFlorian Westphal 						   struct sk_buff *skb,
1337613b92bSFlorian Westphal 						   netdev_features_t features)
1347613b92bSFlorian Westphal {
1357613b92bSFlorian Westphal 	const struct net_offload *ops;
1367613b92bSFlorian Westphal 	struct sk_buff *segs = ERR_PTR(-EINVAL);
1377613b92bSFlorian Westphal 	struct xfrm_offload *xo = xfrm_offload(skb);
1387613b92bSFlorian Westphal 
1397613b92bSFlorian Westphal 	skb->transport_header += x->props.header_len;
1407613b92bSFlorian Westphal 	ops = rcu_dereference(inet_offloads[xo->proto]);
1417613b92bSFlorian Westphal 	if (likely(ops && ops->callbacks.gso_segment))
1427613b92bSFlorian Westphal 		segs = ops->callbacks.gso_segment(skb, features);
1437613b92bSFlorian Westphal 
1447613b92bSFlorian Westphal 	return segs;
1457613b92bSFlorian Westphal }
1467613b92bSFlorian Westphal 
xfrm4_beet_gso_segment(struct xfrm_state * x,struct sk_buff * skb,netdev_features_t features)147384a46eaSXin Long static struct sk_buff *xfrm4_beet_gso_segment(struct xfrm_state *x,
148384a46eaSXin Long 					      struct sk_buff *skb,
149384a46eaSXin Long 					      netdev_features_t features)
150384a46eaSXin Long {
151384a46eaSXin Long 	struct xfrm_offload *xo = xfrm_offload(skb);
152384a46eaSXin Long 	struct sk_buff *segs = ERR_PTR(-EINVAL);
153384a46eaSXin Long 	const struct net_offload *ops;
1546f297068SXin Long 	u8 proto = xo->proto;
155384a46eaSXin Long 
156384a46eaSXin Long 	skb->transport_header += x->props.header_len;
157384a46eaSXin Long 
1583ffb93baSXin Long 	if (x->sel.family != AF_INET6) {
159384a46eaSXin Long 		if (proto == IPPROTO_BEETPH) {
1603ffb93baSXin Long 			struct ip_beet_phdr *ph =
1613ffb93baSXin Long 				(struct ip_beet_phdr *)skb->data;
162384a46eaSXin Long 
163384a46eaSXin Long 			skb->transport_header += ph->hdrlen * 8;
164384a46eaSXin Long 			proto = ph->nexthdr;
1653ffb93baSXin Long 		} else {
1663ffb93baSXin Long 			skb->transport_header -= IPV4_BEET_PHMAXLEN;
1673ffb93baSXin Long 		}
1683ffb93baSXin Long 	} else {
1696f297068SXin Long 		__be16 frag;
1706f297068SXin Long 
1716f297068SXin Long 		skb->transport_header +=
1726f297068SXin Long 			ipv6_skip_exthdr(skb, 0, &proto, &frag);
1736f297068SXin Long 		if (proto == IPPROTO_TCP)
174384a46eaSXin Long 			skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
175384a46eaSXin Long 	}
176384a46eaSXin Long 
177053c8fdfSSteffen Klassert 	if (proto == IPPROTO_IPV6)
178053c8fdfSSteffen Klassert 		skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
179053c8fdfSSteffen Klassert 
180384a46eaSXin Long 	__skb_pull(skb, skb_transport_offset(skb));
181384a46eaSXin Long 	ops = rcu_dereference(inet_offloads[proto]);
182384a46eaSXin Long 	if (likely(ops && ops->callbacks.gso_segment))
183384a46eaSXin Long 		segs = ops->callbacks.gso_segment(skb, features);
184384a46eaSXin Long 
185384a46eaSXin Long 	return segs;
186384a46eaSXin Long }
187384a46eaSXin Long 
xfrm4_outer_mode_gso_segment(struct xfrm_state * x,struct sk_buff * skb,netdev_features_t features)1887613b92bSFlorian Westphal static struct sk_buff *xfrm4_outer_mode_gso_segment(struct xfrm_state *x,
1897613b92bSFlorian Westphal 						    struct sk_buff *skb,
1907613b92bSFlorian Westphal 						    netdev_features_t features)
1917613b92bSFlorian Westphal {
192c9500d7bSFlorian Westphal 	switch (x->outer_mode.encap) {
1937613b92bSFlorian Westphal 	case XFRM_MODE_TUNNEL:
1947613b92bSFlorian Westphal 		return xfrm4_tunnel_gso_segment(x, skb, features);
1957613b92bSFlorian Westphal 	case XFRM_MODE_TRANSPORT:
1967613b92bSFlorian Westphal 		return xfrm4_transport_gso_segment(x, skb, features);
197384a46eaSXin Long 	case XFRM_MODE_BEET:
198384a46eaSXin Long 		return xfrm4_beet_gso_segment(x, skb, features);
1997613b92bSFlorian Westphal 	}
2007613b92bSFlorian Westphal 
2017613b92bSFlorian Westphal 	return ERR_PTR(-EOPNOTSUPP);
2027613b92bSFlorian Westphal }
2037613b92bSFlorian Westphal 
esp4_gso_segment(struct sk_buff * skb,netdev_features_t features)2047862b405SSteffen Klassert static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
2057862b405SSteffen Klassert 				        netdev_features_t features)
2067862b405SSteffen Klassert {
2077862b405SSteffen Klassert 	struct xfrm_state *x;
2087862b405SSteffen Klassert 	struct ip_esp_hdr *esph;
2097862b405SSteffen Klassert 	struct crypto_aead *aead;
2107862b405SSteffen Klassert 	netdev_features_t esp_features = features;
2117862b405SSteffen Klassert 	struct xfrm_offload *xo = xfrm_offload(skb);
2122294be0fSFlorian Westphal 	struct sec_path *sp;
2137862b405SSteffen Klassert 
2147862b405SSteffen Klassert 	if (!xo)
2153dca3f38SSteffen Klassert 		return ERR_PTR(-EINVAL);
2167862b405SSteffen Klassert 
217121d57afSWillem de Bruijn 	if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
2185ca11440SDavid S. Miller 		return ERR_PTR(-EINVAL);
2197862b405SSteffen Klassert 
2202294be0fSFlorian Westphal 	sp = skb_sec_path(skb);
2212294be0fSFlorian Westphal 	x = sp->xvec[sp->len - 1];
2227862b405SSteffen Klassert 	aead = x->data;
2237862b405SSteffen Klassert 	esph = ip_esp_hdr(skb);
2247862b405SSteffen Klassert 
2257862b405SSteffen Klassert 	if (esph->spi != x->id.spi)
2263dca3f38SSteffen Klassert 		return ERR_PTR(-EINVAL);
2277862b405SSteffen Klassert 
2287862b405SSteffen Klassert 	if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
2293dca3f38SSteffen Klassert 		return ERR_PTR(-EINVAL);
2307862b405SSteffen Klassert 
2317862b405SSteffen Klassert 	__skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
2327862b405SSteffen Klassert 
2337862b405SSteffen Klassert 	skb->encap_hdr_csum = 1;
2347862b405SSteffen Klassert 
23565fd2c2aSBoris Pismenny 	if ((!(skb->dev->gso_partial_features & NETIF_F_HW_ESP) &&
23665fd2c2aSBoris Pismenny 	     !(features & NETIF_F_HW_ESP)) || x->xso.dev != skb->dev)
237154deab6SXin Long 		esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK |
238154deab6SXin Long 					    NETIF_F_SCTP_CRC);
23965fd2c2aSBoris Pismenny 	else if (!(features & NETIF_F_HW_ESP_TX_CSUM) &&
24065fd2c2aSBoris Pismenny 		 !(skb->dev->gso_partial_features & NETIF_F_HW_ESP_TX_CSUM))
241154deab6SXin Long 		esp_features = features & ~(NETIF_F_CSUM_MASK |
242154deab6SXin Long 					    NETIF_F_SCTP_CRC);
2437862b405SSteffen Klassert 
2447862b405SSteffen Klassert 	xo->flags |= XFRM_GSO_SEGMENT;
2457862b405SSteffen Klassert 
2467613b92bSFlorian Westphal 	return xfrm4_outer_mode_gso_segment(x, skb, esp_features);
2477862b405SSteffen Klassert }
2487862b405SSteffen Klassert 
esp_input_tail(struct xfrm_state * x,struct sk_buff * skb)249fca11ebdSSteffen Klassert static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
250fca11ebdSSteffen Klassert {
251fca11ebdSSteffen Klassert 	struct crypto_aead *aead = x->data;
252ec9567a9SIlan Tayari 	struct xfrm_offload *xo = xfrm_offload(skb);
253fca11ebdSSteffen Klassert 
254fca11ebdSSteffen Klassert 	if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
255fca11ebdSSteffen Klassert 		return -EINVAL;
256fca11ebdSSteffen Klassert 
257ec9567a9SIlan Tayari 	if (!(xo->flags & CRYPTO_DONE))
258fca11ebdSSteffen Klassert 		skb->ip_summed = CHECKSUM_NONE;
259fca11ebdSSteffen Klassert 
260fca11ebdSSteffen Klassert 	return esp_input_done2(skb, 0);
261fca11ebdSSteffen Klassert }
262fca11ebdSSteffen Klassert 
esp_xmit(struct xfrm_state * x,struct sk_buff * skb,netdev_features_t features)263fca11ebdSSteffen Klassert static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb,  netdev_features_t features)
264fca11ebdSSteffen Klassert {
265fca11ebdSSteffen Klassert 	int err;
266fca11ebdSSteffen Klassert 	int alen;
267fca11ebdSSteffen Klassert 	int blksize;
268fca11ebdSSteffen Klassert 	struct xfrm_offload *xo;
269fca11ebdSSteffen Klassert 	struct ip_esp_hdr *esph;
270fca11ebdSSteffen Klassert 	struct crypto_aead *aead;
271fca11ebdSSteffen Klassert 	struct esp_info esp;
272fca11ebdSSteffen Klassert 	bool hw_offload = true;
2733dca3f38SSteffen Klassert 	__u32 seq;
274*447bc4b1SMike Yu 	int encap_type = 0;
275fca11ebdSSteffen Klassert 
276fca11ebdSSteffen Klassert 	esp.inplace = true;
277fca11ebdSSteffen Klassert 
278fca11ebdSSteffen Klassert 	xo = xfrm_offload(skb);
279fca11ebdSSteffen Klassert 
280fca11ebdSSteffen Klassert 	if (!xo)
281fca11ebdSSteffen Klassert 		return -EINVAL;
282fca11ebdSSteffen Klassert 
28365fd2c2aSBoris Pismenny 	if ((!(features & NETIF_F_HW_ESP) &&
28465fd2c2aSBoris Pismenny 	     !(skb->dev->gso_partial_features & NETIF_F_HW_ESP)) ||
28565fd2c2aSBoris Pismenny 	    x->xso.dev != skb->dev) {
286fca11ebdSSteffen Klassert 		xo->flags |= CRYPTO_FALLBACK;
287fca11ebdSSteffen Klassert 		hw_offload = false;
288fca11ebdSSteffen Klassert 	}
289fca11ebdSSteffen Klassert 
290fca11ebdSSteffen Klassert 	esp.proto = xo->proto;
291fca11ebdSSteffen Klassert 
292fca11ebdSSteffen Klassert 	/* skb is pure payload to encrypt */
293fca11ebdSSteffen Klassert 
294fca11ebdSSteffen Klassert 	aead = x->data;
295fca11ebdSSteffen Klassert 	alen = crypto_aead_authsize(aead);
296fca11ebdSSteffen Klassert 
297fca11ebdSSteffen Klassert 	esp.tfclen = 0;
298fca11ebdSSteffen Klassert 	/* XXX: Add support for tfc padding here. */
299fca11ebdSSteffen Klassert 
300fca11ebdSSteffen Klassert 	blksize = ALIGN(crypto_aead_blocksize(aead), 4);
301fca11ebdSSteffen Klassert 	esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
302fca11ebdSSteffen Klassert 	esp.plen = esp.clen - skb->len - esp.tfclen;
303fca11ebdSSteffen Klassert 	esp.tailen = esp.tfclen + esp.plen + alen;
304fca11ebdSSteffen Klassert 
305fca11ebdSSteffen Klassert 	esp.esph = ip_esp_hdr(skb);
306fca11ebdSSteffen Klassert 
307*447bc4b1SMike Yu 	if (x->encap)
308*447bc4b1SMike Yu 		encap_type = x->encap->encap_type;
309fca11ebdSSteffen Klassert 
310*447bc4b1SMike Yu 	if (!hw_offload || !skb_is_gso(skb) || (hw_offload && encap_type == UDP_ENCAP_ESPINUDP)) {
311fca11ebdSSteffen Klassert 		esp.nfrags = esp_output_head(x, skb, &esp);
312fca11ebdSSteffen Klassert 		if (esp.nfrags < 0)
313fca11ebdSSteffen Klassert 			return esp.nfrags;
314fca11ebdSSteffen Klassert 	}
315fca11ebdSSteffen Klassert 
3163dca3f38SSteffen Klassert 	seq = xo->seq.low;
3173dca3f38SSteffen Klassert 
318fca11ebdSSteffen Klassert 	esph = esp.esph;
319fca11ebdSSteffen Klassert 	esph->spi = x->id.spi;
320fca11ebdSSteffen Klassert 
321fca11ebdSSteffen Klassert 	skb_push(skb, -skb_network_offset(skb));
322fca11ebdSSteffen Klassert 
323fca11ebdSSteffen Klassert 	if (xo->flags & XFRM_GSO_SEGMENT) {
3243dca3f38SSteffen Klassert 		esph->seq_no = htonl(seq);
3253dca3f38SSteffen Klassert 
3263dca3f38SSteffen Klassert 		if (!skb_is_gso(skb))
3273dca3f38SSteffen Klassert 			xo->seq.low++;
3283dca3f38SSteffen Klassert 		else
3293dca3f38SSteffen Klassert 			xo->seq.low += skb_shinfo(skb)->gso_segs;
3303dca3f38SSteffen Klassert 	}
3313dca3f38SSteffen Klassert 
3324b549cccSChristian Langrock 	if (xo->seq.low < seq)
3334b549cccSChristian Langrock 		xo->seq.hi++;
3344b549cccSChristian Langrock 
3353dca3f38SSteffen Klassert 	esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32));
3363dca3f38SSteffen Klassert 
337*447bc4b1SMike Yu 	if (hw_offload && encap_type == UDP_ENCAP_ESPINUDP) {
338*447bc4b1SMike Yu 		/* In the XFRM stack, the encapsulation protocol is set to iphdr->protocol by
339*447bc4b1SMike Yu 		 * setting *skb_mac_header(skb) (see esp_output_udp_encap()) where skb->mac_header
340*447bc4b1SMike Yu 		 * points to iphdr->protocol (see xfrm4_tunnel_encap_add()).
341*447bc4b1SMike Yu 		 * However, in esp_xmit(), skb->mac_header doesn't point to iphdr->protocol.
342*447bc4b1SMike Yu 		 * Therefore, the protocol field needs to be corrected.
343*447bc4b1SMike Yu 		 */
344*447bc4b1SMike Yu 		ip_hdr(skb)->protocol = IPPROTO_UDP;
345*447bc4b1SMike Yu 
346*447bc4b1SMike Yu 		esph->seq_no = htonl(seq);
347*447bc4b1SMike Yu 	}
348*447bc4b1SMike Yu 
349fca11ebdSSteffen Klassert 	ip_hdr(skb)->tot_len = htons(skb->len);
350fca11ebdSSteffen Klassert 	ip_send_check(ip_hdr(skb));
351fca11ebdSSteffen Klassert 
352c7dbf4c0SSteffen Klassert 	if (hw_offload) {
353c7dbf4c0SSteffen Klassert 		if (!skb_ext_add(skb, SKB_EXT_SEC_PATH))
354c7dbf4c0SSteffen Klassert 			return -ENOMEM;
355c7dbf4c0SSteffen Klassert 
356c7dbf4c0SSteffen Klassert 		xo = xfrm_offload(skb);
357c7dbf4c0SSteffen Klassert 		if (!xo)
358c7dbf4c0SSteffen Klassert 			return -EINVAL;
359c7dbf4c0SSteffen Klassert 
360c7dbf4c0SSteffen Klassert 		xo->flags |= XFRM_XMIT;
361fca11ebdSSteffen Klassert 		return 0;
362c7dbf4c0SSteffen Klassert 	}
363fca11ebdSSteffen Klassert 
364fca11ebdSSteffen Klassert 	err = esp_output_tail(x, skb, &esp);
3654ff0308fSSteffen Klassert 	if (err)
366fca11ebdSSteffen Klassert 		return err;
367fca11ebdSSteffen Klassert 
368fca11ebdSSteffen Klassert 	secpath_reset(skb);
369fca11ebdSSteffen Klassert 
370f015b900SSebastian Andrzej Siewior 	if (skb_needs_linearize(skb, skb->dev->features) &&
371f015b900SSebastian Andrzej Siewior 	    __skb_linearize(skb))
372f015b900SSebastian Andrzej Siewior 		return -ENOMEM;
373fca11ebdSSteffen Klassert 	return 0;
374fca11ebdSSteffen Klassert }
375fca11ebdSSteffen Klassert 
3767785bba2SSteffen Klassert static const struct net_offload esp4_offload = {
3777785bba2SSteffen Klassert 	.callbacks = {
3787785bba2SSteffen Klassert 		.gro_receive = esp4_gro_receive,
3797862b405SSteffen Klassert 		.gso_segment = esp4_gso_segment,
3807785bba2SSteffen Klassert 	},
3817785bba2SSteffen Klassert };
3827785bba2SSteffen Klassert 
383fca11ebdSSteffen Klassert static const struct xfrm_type_offload esp_type_offload = {
384fca11ebdSSteffen Klassert 	.owner		= THIS_MODULE,
385fca11ebdSSteffen Klassert 	.proto	     	= IPPROTO_ESP,
386fca11ebdSSteffen Klassert 	.input_tail	= esp_input_tail,
387fca11ebdSSteffen Klassert 	.xmit		= esp_xmit,
3887862b405SSteffen Klassert 	.encap		= esp4_gso_encap,
389fca11ebdSSteffen Klassert };
390fca11ebdSSteffen Klassert 
esp4_offload_init(void)3917785bba2SSteffen Klassert static int __init esp4_offload_init(void)
3927785bba2SSteffen Klassert {
393fca11ebdSSteffen Klassert 	if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
394fca11ebdSSteffen Klassert 		pr_info("%s: can't add xfrm type offload\n", __func__);
395fca11ebdSSteffen Klassert 		return -EAGAIN;
396fca11ebdSSteffen Klassert 	}
397fca11ebdSSteffen Klassert 
3987785bba2SSteffen Klassert 	return inet_add_offload(&esp4_offload, IPPROTO_ESP);
3997785bba2SSteffen Klassert }
4007785bba2SSteffen Klassert 
esp4_offload_exit(void)4017785bba2SSteffen Klassert static void __exit esp4_offload_exit(void)
4027785bba2SSteffen Klassert {
4034f518e80SFlorian Westphal 	xfrm_unregister_type_offload(&esp_type_offload, AF_INET);
4047785bba2SSteffen Klassert 	inet_del_offload(&esp4_offload, IPPROTO_ESP);
4057785bba2SSteffen Klassert }
4067785bba2SSteffen Klassert 
4077785bba2SSteffen Klassert module_init(esp4_offload_init);
4087785bba2SSteffen Klassert module_exit(esp4_offload_exit);
4097785bba2SSteffen Klassert MODULE_LICENSE("GPL");
4107785bba2SSteffen Klassert MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
411ffdb5211SIlan Tayari MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);
41267c20de3SRob Gill MODULE_DESCRIPTION("IPV4 GSO/GRO offload support");
413