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>
23 #include <linux/scatterlist.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
29 static struct sk_buff *esp4_gro_receive(struct list_head *head,
32 int offset = skb_gro_offset(skb);
33 struct xfrm_offload *xo;
38 if (!pskb_pull(skb, offset))
41 if (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);
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 __be16 type = x->inner_mode.family == AF_INET6 ? htons(ETH_P_IPV6)
116 return skb_eth_gso_segment(skb, features, type);
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_beet_gso_segment(struct xfrm_state *x,
137 netdev_features_t features)
139 struct xfrm_offload *xo = xfrm_offload(skb);
140 struct sk_buff *segs = ERR_PTR(-EINVAL);
141 const struct net_offload *ops;
142 u8 proto = xo->proto;
144 skb->transport_header += x->props.header_len;
146 if (x->sel.family != AF_INET6) {
147 if (proto == IPPROTO_BEETPH) {
148 struct ip_beet_phdr *ph =
149 (struct ip_beet_phdr *)skb->data;
151 skb->transport_header += ph->hdrlen * 8;
154 skb->transport_header -= IPV4_BEET_PHMAXLEN;
159 skb->transport_header +=
160 ipv6_skip_exthdr(skb, 0, &proto, &frag);
161 if (proto == IPPROTO_TCP)
162 skb_shinfo(skb)->gso_type |= SKB_GSO_TCPV4;
165 if (proto == IPPROTO_IPV6)
166 skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
168 __skb_pull(skb, skb_transport_offset(skb));
169 ops = rcu_dereference(inet_offloads[proto]);
170 if (likely(ops && ops->callbacks.gso_segment))
171 segs = ops->callbacks.gso_segment(skb, features);
176 static struct sk_buff *xfrm4_outer_mode_gso_segment(struct xfrm_state *x,
178 netdev_features_t features)
180 switch (x->outer_mode.encap) {
181 case XFRM_MODE_TUNNEL:
182 return xfrm4_tunnel_gso_segment(x, skb, features);
183 case XFRM_MODE_TRANSPORT:
184 return xfrm4_transport_gso_segment(x, skb, features);
186 return xfrm4_beet_gso_segment(x, skb, features);
189 return ERR_PTR(-EOPNOTSUPP);
192 static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
193 netdev_features_t features)
195 struct xfrm_state *x;
196 struct ip_esp_hdr *esph;
197 struct crypto_aead *aead;
198 netdev_features_t esp_features = features;
199 struct xfrm_offload *xo = xfrm_offload(skb);
203 return ERR_PTR(-EINVAL);
205 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
206 return ERR_PTR(-EINVAL);
208 sp = skb_sec_path(skb);
209 x = sp->xvec[sp->len - 1];
211 esph = ip_esp_hdr(skb);
213 if (esph->spi != x->id.spi)
214 return ERR_PTR(-EINVAL);
216 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
217 return ERR_PTR(-EINVAL);
219 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
221 skb->encap_hdr_csum = 1;
223 if ((!(skb->dev->gso_partial_features & NETIF_F_HW_ESP) &&
224 !(features & NETIF_F_HW_ESP)) || x->xso.dev != skb->dev)
225 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK |
227 else if (!(features & NETIF_F_HW_ESP_TX_CSUM) &&
228 !(skb->dev->gso_partial_features & NETIF_F_HW_ESP_TX_CSUM))
229 esp_features = features & ~(NETIF_F_CSUM_MASK |
232 xo->flags |= XFRM_GSO_SEGMENT;
234 return xfrm4_outer_mode_gso_segment(x, skb, esp_features);
237 static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
239 struct crypto_aead *aead = x->data;
240 struct xfrm_offload *xo = xfrm_offload(skb);
242 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
245 if (!(xo->flags & CRYPTO_DONE))
246 skb->ip_summed = CHECKSUM_NONE;
248 return esp_input_done2(skb, 0);
251 static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
256 struct xfrm_offload *xo;
257 struct ip_esp_hdr *esph;
258 struct crypto_aead *aead;
260 bool hw_offload = true;
265 xo = xfrm_offload(skb);
270 if ((!(features & NETIF_F_HW_ESP) &&
271 !(skb->dev->gso_partial_features & NETIF_F_HW_ESP)) ||
272 x->xso.dev != skb->dev) {
273 xo->flags |= CRYPTO_FALLBACK;
277 esp.proto = xo->proto;
279 /* skb is pure payload to encrypt */
282 alen = crypto_aead_authsize(aead);
285 /* XXX: Add support for tfc padding here. */
287 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
288 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
289 esp.plen = esp.clen - skb->len - esp.tfclen;
290 esp.tailen = esp.tfclen + esp.plen + alen;
292 esp.esph = ip_esp_hdr(skb);
295 if (!hw_offload || !skb_is_gso(skb)) {
296 esp.nfrags = esp_output_head(x, skb, &esp);
304 esph->spi = x->id.spi;
306 skb_push(skb, -skb_network_offset(skb));
308 if (xo->flags & XFRM_GSO_SEGMENT) {
309 esph->seq_no = htonl(seq);
311 if (!skb_is_gso(skb))
314 xo->seq.low += skb_shinfo(skb)->gso_segs;
317 if (xo->seq.low < seq)
320 esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32));
322 ip_hdr(skb)->tot_len = htons(skb->len);
323 ip_send_check(ip_hdr(skb));
326 if (!skb_ext_add(skb, SKB_EXT_SEC_PATH))
329 xo = xfrm_offload(skb);
333 xo->flags |= XFRM_XMIT;
337 err = esp_output_tail(x, skb, &esp);
346 static const struct net_offload esp4_offload = {
348 .gro_receive = esp4_gro_receive,
349 .gso_segment = esp4_gso_segment,
353 static const struct xfrm_type_offload esp_type_offload = {
354 .owner = THIS_MODULE,
355 .proto = IPPROTO_ESP,
356 .input_tail = esp_input_tail,
358 .encap = esp4_gso_encap,
361 static int __init esp4_offload_init(void)
363 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
364 pr_info("%s: can't add xfrm type offload\n", __func__);
368 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
371 static void __exit esp4_offload_exit(void)
373 xfrm_unregister_type_offload(&esp_type_offload, AF_INET);
374 inet_del_offload(&esp4_offload, IPPROTO_ESP);
377 module_init(esp4_offload_init);
378 module_exit(esp4_offload_exit);
379 MODULE_LICENSE("GPL");
380 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
381 MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);
382 MODULE_DESCRIPTION("IPV4 GSO/GRO offload support");