xfrm: change secpath_set to return secpath struct, not error value
[linux-2.6-block.git] / net / ipv4 / esp4_offload.c
CommitLineData
7785bba2
SK
1/*
2 * IPV4 GSO/GRO offload support
3 * Linux INET implementation
4 *
5 * Copyright (C) 2016 secunet Security Networks AG
6 * Author: Steffen Klassert <steffen.klassert@secunet.com>
7 *
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.
11 *
12 * ESP GRO support
13 */
14
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>
30
d4546c25
DM
31static struct sk_buff *esp4_gro_receive(struct list_head *head,
32 struct sk_buff *skb)
7785bba2
SK
33{
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;
40
374d1b5a
SK
41 if (!pskb_pull(skb, offset))
42 return NULL;
7785bba2
SK
43
44 if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
45 goto out;
46
bcd1f8a4
SK
47 xo = xfrm_offload(skb);
48 if (!xo || !(xo->flags & CRYPTO_DONE)) {
0ca64da1
FW
49 struct sec_path *sp = secpath_set(skb);
50
51 if (!sp)
bcd1f8a4 52 goto out;
7785bba2 53
0ca64da1 54 if (sp->len == XFRM_MAX_DEPTH)
bcd1f8a4 55 goto out;
7785bba2 56
bcd1f8a4
SK
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);
60 if (!x)
61 goto out;
7785bba2 62
0ca64da1
FW
63 sp->xvec[sp->len++] = x;
64 sp->olen++;
7785bba2 65
bcd1f8a4
SK
66 xo = xfrm_offload(skb);
67 if (!xo) {
68 xfrm_state_put(x);
69 goto out;
70 }
7785bba2 71 }
bcd1f8a4 72
7785bba2
SK
73 xo->flags |= XFRM_GRO;
74
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;
79
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);
83
84 return ERR_PTR(-EINPROGRESS);
85out:
86 skb_push(skb, offset);
87 NAPI_GRO_CB(skb)->same_flow = 0;
88 NAPI_GRO_CB(skb)->flush = 1;
89
90 return NULL;
91}
92
7862b405
SK
93static void esp4_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
94{
95 struct ip_esp_hdr *esph;
96 struct iphdr *iph = ip_hdr(skb);
97 struct xfrm_offload *xo = xfrm_offload(skb);
98 int proto = iph->protocol;
99
100 skb_push(skb, -skb_network_offset(skb));
101 esph = ip_esp_hdr(skb);
102 *skb_mac_header(skb) = IPPROTO_ESP;
103
104 esph->spi = x->id.spi;
105 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
106
107 xo->proto = proto;
108}
109
110static struct sk_buff *esp4_gso_segment(struct sk_buff *skb,
111 netdev_features_t features)
112{
7862b405
SK
113 struct xfrm_state *x;
114 struct ip_esp_hdr *esph;
115 struct crypto_aead *aead;
7862b405
SK
116 netdev_features_t esp_features = features;
117 struct xfrm_offload *xo = xfrm_offload(skb);
118
119 if (!xo)
3dca3f38 120 return ERR_PTR(-EINVAL);
7862b405 121
121d57af 122 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
5ca11440 123 return ERR_PTR(-EINVAL);
7862b405
SK
124
125 x = skb->sp->xvec[skb->sp->len - 1];
126 aead = x->data;
127 esph = ip_esp_hdr(skb);
128
129 if (esph->spi != x->id.spi)
3dca3f38 130 return ERR_PTR(-EINVAL);
7862b405
SK
131
132 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
3dca3f38 133 return ERR_PTR(-EINVAL);
7862b405
SK
134
135 __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
136
137 skb->encap_hdr_csum = 1;
138
fcb662de 139 if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev)
7862b405 140 esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
5211fcfb
SN
141 else if (!(features & NETIF_F_HW_ESP_TX_CSUM))
142 esp_features = features & ~NETIF_F_CSUM_MASK;
7862b405 143
3dca3f38 144 xo->flags |= XFRM_GSO_SEGMENT;
7862b405 145
3dca3f38 146 return x->outer_mode->gso_segment(x, skb, esp_features);
7862b405
SK
147}
148
fca11ebd
SK
149static int esp_input_tail(struct xfrm_state *x, struct sk_buff *skb)
150{
151 struct crypto_aead *aead = x->data;
ec9567a9 152 struct xfrm_offload *xo = xfrm_offload(skb);
fca11ebd
SK
153
154 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
155 return -EINVAL;
156
ec9567a9
IT
157 if (!(xo->flags & CRYPTO_DONE))
158 skb->ip_summed = CHECKSUM_NONE;
fca11ebd
SK
159
160 return esp_input_done2(skb, 0);
161}
162
163static int esp_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
164{
165 int err;
166 int alen;
167 int blksize;
168 struct xfrm_offload *xo;
169 struct ip_esp_hdr *esph;
170 struct crypto_aead *aead;
171 struct esp_info esp;
172 bool hw_offload = true;
3dca3f38 173 __u32 seq;
fca11ebd
SK
174
175 esp.inplace = true;
176
177 xo = xfrm_offload(skb);
178
179 if (!xo)
180 return -EINVAL;
181
fcb662de 182 if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev) {
fca11ebd
SK
183 xo->flags |= CRYPTO_FALLBACK;
184 hw_offload = false;
185 }
186
187 esp.proto = xo->proto;
188
189 /* skb is pure payload to encrypt */
190
191 aead = x->data;
192 alen = crypto_aead_authsize(aead);
193
194 esp.tfclen = 0;
195 /* XXX: Add support for tfc padding here. */
196
197 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
198 esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
199 esp.plen = esp.clen - skb->len - esp.tfclen;
200 esp.tailen = esp.tfclen + esp.plen + alen;
201
202 esp.esph = ip_esp_hdr(skb);
203
204
205 if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
206 esp.nfrags = esp_output_head(x, skb, &esp);
207 if (esp.nfrags < 0)
208 return esp.nfrags;
209 }
210
3dca3f38
SK
211 seq = xo->seq.low;
212
fca11ebd
SK
213 esph = esp.esph;
214 esph->spi = x->id.spi;
215
216 skb_push(skb, -skb_network_offset(skb));
217
218 if (xo->flags & XFRM_GSO_SEGMENT) {
3dca3f38
SK
219 esph->seq_no = htonl(seq);
220
221 if (!skb_is_gso(skb))
222 xo->seq.low++;
223 else
224 xo->seq.low += skb_shinfo(skb)->gso_segs;
fca11ebd
SK
225 }
226
3dca3f38
SK
227 esp.seqno = cpu_to_be64(seq + ((u64)xo->seq.hi << 32));
228
229 ip_hdr(skb)->tot_len = htons(skb->len);
230 ip_send_check(ip_hdr(skb));
231
fca11ebd
SK
232 if (hw_offload)
233 return 0;
234
fca11ebd 235 err = esp_output_tail(x, skb, &esp);
4ff0308f 236 if (err)
fca11ebd
SK
237 return err;
238
239 secpath_reset(skb);
240
241 return 0;
242}
243
7785bba2
SK
244static const struct net_offload esp4_offload = {
245 .callbacks = {
246 .gro_receive = esp4_gro_receive,
7862b405 247 .gso_segment = esp4_gso_segment,
7785bba2
SK
248 },
249};
250
fca11ebd
SK
251static const struct xfrm_type_offload esp_type_offload = {
252 .description = "ESP4 OFFLOAD",
253 .owner = THIS_MODULE,
254 .proto = IPPROTO_ESP,
255 .input_tail = esp_input_tail,
256 .xmit = esp_xmit,
7862b405 257 .encap = esp4_gso_encap,
fca11ebd
SK
258};
259
7785bba2
SK
260static int __init esp4_offload_init(void)
261{
fca11ebd
SK
262 if (xfrm_register_type_offload(&esp_type_offload, AF_INET) < 0) {
263 pr_info("%s: can't add xfrm type offload\n", __func__);
264 return -EAGAIN;
265 }
266
7785bba2
SK
267 return inet_add_offload(&esp4_offload, IPPROTO_ESP);
268}
269
270static void __exit esp4_offload_exit(void)
271{
fca11ebd
SK
272 if (xfrm_unregister_type_offload(&esp_type_offload, AF_INET) < 0)
273 pr_info("%s: can't remove xfrm type offload\n", __func__);
274
7785bba2
SK
275 inet_del_offload(&esp4_offload, IPPROTO_ESP);
276}
277
278module_init(esp4_offload_init);
279module_exit(esp4_offload_exit);
280MODULE_LICENSE("GPL");
281MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
ffdb5211 282MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET, XFRM_PROTO_ESP);