arm64: dts: r8a7796: Add reset control properties for audio
[linux-2.6/btrfs-unstable.git] / net / ipv4 / esp4_offload.c
blobe0666016a7642c017b4693d32723245adc484982
1 /*
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.
12 * ESP GRO support
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>
22 #include <net/ip.h>
23 #include <net/xfrm.h>
24 #include <net/esp.h>
25 #include <linux/scatterlist.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <net/udp.h>
31 static struct sk_buff **esp4_gro_receive(struct sk_buff **head,
32 struct sk_buff *skb)
34 int offset = skb_gro_offset(skb);
35 struct xfrm_offload *xo;
36 struct xfrm_state *x;
37 __be32 seq;
38 __be32 spi;
39 int err;
41 skb_pull(skb, offset);
43 if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
44 goto out;
46 xo = xfrm_offload(skb);
47 if (!xo || !(xo->flags & CRYPTO_DONE)) {
48 err = secpath_set(skb);
49 if (err)
50 goto out;
52 if (skb->sp->len == XFRM_MAX_DEPTH)
53 goto out;
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);
58 if (!x)
59 goto out;
61 skb->sp->xvec[skb->sp->len++] = x;
62 skb->sp->olen++;
64 xo = xfrm_offload(skb);
65 if (!xo) {
66 xfrm_state_put(x);
67 goto out;
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);
83 out:
84 skb_push(skb, offset);
85 NAPI_GRO_CB(skb)->same_flow = 0;
86 NAPI_GRO_CB(skb)->flush = 1;
88 return NULL;
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);
105 xo->proto = proto;
108 static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
109 netdev_features_t features)
111 __u32 seq;
112 int err = 0;
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);
121 if (!xo)
122 goto out;
124 seq = xo->seq.low;
126 x = skb->sp->xvec[skb->sp->len - 1];
127 aead = x->data;
128 esph = ip_esp_hdr(skb);
130 if (esph->spi != x->id.spi)
131 goto out;
133 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
134 goto out;
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))
145 goto out;
147 __skb_pull(skb, skb->data - skb_mac_header(skb));
149 skb2 = segs;
150 do {
151 struct sk_buff *nskb = skb2->next;
153 xo = xfrm_offload(skb2);
154 xo->flags |= XFRM_GSO_SEGMENT;
155 xo->seq.low = seq;
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);
164 if (err) {
165 kfree_skb_list(segs);
166 return ERR_PTR(err);
169 if (!skb_is_gso(skb2))
170 seq++;
171 else
172 seq += skb_shinfo(skb2)->gso_segs;
174 skb_push(skb2, skb2->mac_len);
175 skb2 = nskb;
176 } while (skb2);
178 out:
179 return segs;
182 static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
184 struct crypto_aead *aead = x->data;
186 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
187 return -EINVAL;
189 skb->ip_summed = CHECKSUM_NONE;
191 return esp_input_done2(skb, 0);
194 static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
196 int err;
197 int alen;
198 int blksize;
199 struct xfrm_offload *xo;
200 struct ip_esp_hdr *esph;
201 struct crypto_aead *aead;
202 struct esp_info esp;
203 bool hw_offload = true;
205 esp.inplace = true;
207 xo = xfrm_offload(skb);
209 if (!xo)
210 return -EINVAL;
212 if (!(features & NETIF_F_HW_ESP) || !x->xso.offload_handle ||
213 (x->xso.dev != skb->dev)) {
214 xo->flags |= CRYPTO_FALLBACK;
215 hw_offload = false;
218 esp.proto = xo->proto;
220 /* skb is pure payload to encrypt */
222 aead = x->data;
223 alen = crypto_aead_authsize(aead);
225 esp.tfclen = 0;
226 /* XXX: Add support for tfc padding here. */
228 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
229 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
230 esp.plen = esp.clen - skb->len - esp.tfclen;
231 esp.tailen = esp.tfclen + esp.plen + alen;
233 esp.esph = ip_esp_hdr(skb);
236 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
237 esp.nfrags = esp_output_head(x, skb, &esp);
238 if (esp.nfrags < 0)
239 return esp.nfrags;
242 esph = esp.esph;
243 esph->spi = x->id.spi;
245 skb_push(skb, -skb_network_offset(skb));
247 if (xo->flags & XFRM_GSO_SEGMENT) {
248 esph->seq_no = htonl(xo->seq.low);
249 } else {
250 ip_hdr(skb)->tot_len = htons(skb->len);
251 ip_send_check(ip_hdr(skb));
254 if (hw_offload)
255 return 0;
257 esp.seqno = cpu_to_be64(xo->seq.low + ((u64)xo->seq.hi << 32));
259 err = esp_output_tail(x, skb, &esp);
260 if (err < 0)
261 return err;
263 secpath_reset(skb);
265 return 0;
268 static const struct net_offload esp4_offload = {
269 .callbacks = {
270 .gro_receive = esp4_gro_receive,
271 .gso_segment = esp4_gso_segment,
275 static const struct xfrm_type_offload esp_type_offload = {
276 .description = "ESP4 OFFLOAD",
277 .owner = THIS_MODULE,
278 .proto = IPPROTO_ESP,
279 .input_tail = esp_input_tail,
280 .xmit = esp_xmit,
281 .encap = esp4_gso_encap,
284 static int __init esp4_offload_init(void)
286 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
287 pr_info("%s: can't add xfrm type offload\n", __func__);
288 return -EAGAIN;
291 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
294 static void __exit esp4_offload_exit(void)
296 if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0)
297 pr_info("%s: can't remove xfrm type offload\n", __func__);
299 inet_del_offload(&esp4_offload, IPPROTO_ESP);
302 module_init(esp4_offload_init);
303 module_exit(esp4_offload_exit);
304 MODULE_LICENSE("GPL");
305 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");