Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
afd46503 JP |
2 | #define pr_fmt(fmt) "IPsec: " fmt |
3 | ||
38320c70 HX |
4 | #include <crypto/aead.h> |
5 | #include <crypto/authenc.h> | |
6b7326c8 | 6 | #include <linux/err.h> |
1da177e4 LT |
7 | #include <linux/module.h> |
8 | #include <net/ip.h> | |
9 | #include <net/xfrm.h> | |
10 | #include <net/esp.h> | |
72998d8c | 11 | #include <linux/scatterlist.h> |
a02a6422 | 12 | #include <linux/kernel.h> |
1da177e4 | 13 | #include <linux/pfkeyv2.h> |
38320c70 HX |
14 | #include <linux/rtnetlink.h> |
15 | #include <linux/slab.h> | |
b7c6538c | 16 | #include <linux/spinlock.h> |
2017a72c | 17 | #include <linux/in6.h> |
1da177e4 | 18 | #include <net/icmp.h> |
14c85021 | 19 | #include <net/protocol.h> |
1da177e4 | 20 | #include <net/udp.h> |
e27cca96 SD |
21 | #include <net/tcp.h> |
22 | #include <net/espintcp.h> | |
1da177e4 | 23 | |
cac2661c SK |
24 | #include <linux/highmem.h> |
25 | ||
38320c70 HX |
26 | struct esp_skb_cb { |
27 | struct xfrm_skb_cb xfrm; | |
28 | void *tmp; | |
29 | }; | |
30 | ||
962fcef3 HX |
31 | struct esp_output_extra { |
32 | __be32 seqhi; | |
33 | u32 esphoff; | |
34 | }; | |
35 | ||
38320c70 HX |
36 | #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0])) |
37 | ||
38 | /* | |
39 | * Allocate an AEAD request structure with extra space for SG and IV. | |
40 | * | |
41 | * For alignment considerations the IV is placed at the front, followed | |
42 | * by the request and finally the SG list. | |
43 | * | |
44 | * TODO: Use spare space in skb for this where possible. | |
45 | */ | |
962fcef3 | 46 | static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen) |
38320c70 HX |
47 | { |
48 | unsigned int len; | |
49 | ||
962fcef3 | 50 | len = extralen; |
0dc49e9b SK |
51 | |
52 | len += crypto_aead_ivsize(aead); | |
53 | ||
38320c70 HX |
54 | if (len) { |
55 | len += crypto_aead_alignmask(aead) & | |
56 | ~(crypto_tfm_ctx_alignment() - 1); | |
57 | len = ALIGN(len, crypto_tfm_ctx_alignment()); | |
58 | } | |
59 | ||
7021b2e1 | 60 | len += sizeof(struct aead_request) + crypto_aead_reqsize(aead); |
38320c70 HX |
61 | len = ALIGN(len, __alignof__(struct scatterlist)); |
62 | ||
63 | len += sizeof(struct scatterlist) * nfrags; | |
64 | ||
65 | return kmalloc(len, GFP_ATOMIC); | |
66 | } | |
67 | ||
962fcef3 | 68 | static inline void *esp_tmp_extra(void *tmp) |
0dc49e9b | 69 | { |
962fcef3 | 70 | return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra)); |
0dc49e9b | 71 | } |
962fcef3 HX |
72 | |
73 | static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen) | |
38320c70 HX |
74 | { |
75 | return crypto_aead_ivsize(aead) ? | |
962fcef3 HX |
76 | PTR_ALIGN((u8 *)tmp + extralen, |
77 | crypto_aead_alignmask(aead) + 1) : tmp + extralen; | |
38320c70 HX |
78 | } |
79 | ||
38320c70 HX |
80 | static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv) |
81 | { | |
82 | struct aead_request *req; | |
83 | ||
84 | req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead), | |
85 | crypto_tfm_ctx_alignment()); | |
86 | aead_request_set_tfm(req, aead); | |
87 | return req; | |
88 | } | |
89 | ||
90 | static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead, | |
91 | struct aead_request *req) | |
92 | { | |
93 | return (void *)ALIGN((unsigned long)(req + 1) + | |
94 | crypto_aead_reqsize(aead), | |
95 | __alignof__(struct scatterlist)); | |
96 | } | |
97 | ||
cac2661c SK |
98 | static void esp_ssg_unref(struct xfrm_state *x, void *tmp) |
99 | { | |
cac2661c SK |
100 | struct crypto_aead *aead = x->data; |
101 | int extralen = 0; | |
102 | u8 *iv; | |
103 | struct aead_request *req; | |
104 | struct scatterlist *sg; | |
105 | ||
106 | if (x->props.flags & XFRM_STATE_ESN) | |
f8fdadef | 107 | extralen += sizeof(struct esp_output_extra); |
cac2661c | 108 | |
cac2661c SK |
109 | iv = esp_tmp_iv(aead, tmp, extralen); |
110 | req = esp_tmp_req(aead, iv); | |
111 | ||
112 | /* Unref skb_frag_pages in the src scatterlist if necessary. | |
113 | * Skip the first sg which comes from skb->data. | |
114 | */ | |
115 | if (req->src != req->dst) | |
116 | for (sg = sg_next(req->src); sg; sg = sg_next(sg)) | |
117 | put_page(sg_page(sg)); | |
118 | } | |
119 | ||
e27cca96 SD |
120 | #ifdef CONFIG_INET_ESPINTCP |
121 | struct esp_tcp_sk { | |
122 | struct sock *sk; | |
123 | struct rcu_head rcu; | |
124 | }; | |
125 | ||
126 | static void esp_free_tcp_sk(struct rcu_head *head) | |
127 | { | |
128 | struct esp_tcp_sk *esk = container_of(head, struct esp_tcp_sk, rcu); | |
129 | ||
130 | sock_put(esk->sk); | |
131 | kfree(esk); | |
132 | } | |
133 | ||
134 | static struct sock *esp_find_tcp_sk(struct xfrm_state *x) | |
135 | { | |
136 | struct xfrm_encap_tmpl *encap = x->encap; | |
137 | struct esp_tcp_sk *esk; | |
138 | __be16 sport, dport; | |
139 | struct sock *nsk; | |
140 | struct sock *sk; | |
141 | ||
142 | sk = rcu_dereference(x->encap_sk); | |
143 | if (sk && sk->sk_state == TCP_ESTABLISHED) | |
144 | return sk; | |
145 | ||
146 | spin_lock_bh(&x->lock); | |
147 | sport = encap->encap_sport; | |
148 | dport = encap->encap_dport; | |
149 | nsk = rcu_dereference_protected(x->encap_sk, | |
150 | lockdep_is_held(&x->lock)); | |
151 | if (sk && sk == nsk) { | |
152 | esk = kmalloc(sizeof(*esk), GFP_ATOMIC); | |
153 | if (!esk) { | |
154 | spin_unlock_bh(&x->lock); | |
155 | return ERR_PTR(-ENOMEM); | |
156 | } | |
157 | RCU_INIT_POINTER(x->encap_sk, NULL); | |
158 | esk->sk = sk; | |
159 | call_rcu(&esk->rcu, esp_free_tcp_sk); | |
160 | } | |
161 | spin_unlock_bh(&x->lock); | |
162 | ||
163 | sk = inet_lookup_established(xs_net(x), &tcp_hashinfo, x->id.daddr.a4, | |
164 | dport, x->props.saddr.a4, sport, 0); | |
165 | if (!sk) | |
166 | return ERR_PTR(-ENOENT); | |
167 | ||
168 | if (!tcp_is_ulp_esp(sk)) { | |
169 | sock_put(sk); | |
170 | return ERR_PTR(-EINVAL); | |
171 | } | |
172 | ||
173 | spin_lock_bh(&x->lock); | |
174 | nsk = rcu_dereference_protected(x->encap_sk, | |
175 | lockdep_is_held(&x->lock)); | |
176 | if (encap->encap_sport != sport || | |
177 | encap->encap_dport != dport) { | |
178 | sock_put(sk); | |
179 | sk = nsk ?: ERR_PTR(-EREMCHG); | |
180 | } else if (sk == nsk) { | |
181 | sock_put(sk); | |
182 | } else { | |
183 | rcu_assign_pointer(x->encap_sk, sk); | |
184 | } | |
185 | spin_unlock_bh(&x->lock); | |
186 | ||
187 | return sk; | |
188 | } | |
189 | ||
190 | static int esp_output_tcp_finish(struct xfrm_state *x, struct sk_buff *skb) | |
191 | { | |
192 | struct sock *sk; | |
193 | int err; | |
194 | ||
195 | rcu_read_lock(); | |
196 | ||
197 | sk = esp_find_tcp_sk(x); | |
198 | err = PTR_ERR_OR_ZERO(sk); | |
199 | if (err) | |
200 | goto out; | |
201 | ||
202 | bh_lock_sock(sk); | |
203 | if (sock_owned_by_user(sk)) | |
204 | err = espintcp_queue_out(sk, skb); | |
205 | else | |
206 | err = espintcp_push_skb(sk, skb); | |
207 | bh_unlock_sock(sk); | |
208 | ||
209 | out: | |
210 | rcu_read_unlock(); | |
211 | return err; | |
212 | } | |
213 | ||
214 | static int esp_output_tcp_encap_cb(struct net *net, struct sock *sk, | |
215 | struct sk_buff *skb) | |
216 | { | |
217 | struct dst_entry *dst = skb_dst(skb); | |
218 | struct xfrm_state *x = dst->xfrm; | |
219 | ||
220 | return esp_output_tcp_finish(x, skb); | |
221 | } | |
222 | ||
223 | static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb) | |
224 | { | |
225 | int err; | |
226 | ||
227 | local_bh_disable(); | |
228 | err = xfrm_trans_queue_net(xs_net(x), skb, esp_output_tcp_encap_cb); | |
229 | local_bh_enable(); | |
230 | ||
231 | /* EINPROGRESS just happens to do the right thing. It | |
232 | * actually means that the skb has been consumed and | |
233 | * isn't coming back. | |
234 | */ | |
235 | return err ?: -EINPROGRESS; | |
236 | } | |
237 | #else | |
238 | static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb) | |
239 | { | |
240 | kfree_skb(skb); | |
241 | ||
242 | return -EOPNOTSUPP; | |
243 | } | |
244 | #endif | |
245 | ||
38320c70 HX |
246 | static void esp_output_done(struct crypto_async_request *base, int err) |
247 | { | |
248 | struct sk_buff *skb = base->data; | |
f53c7239 | 249 | struct xfrm_offload *xo = xfrm_offload(skb); |
cac2661c | 250 | void *tmp; |
f53c7239 SK |
251 | struct xfrm_state *x; |
252 | ||
2294be0f FW |
253 | if (xo && (xo->flags & XFRM_DEV_RESUME)) { |
254 | struct sec_path *sp = skb_sec_path(skb); | |
255 | ||
256 | x = sp->xvec[sp->len - 1]; | |
257 | } else { | |
f53c7239 | 258 | x = skb_dst(skb)->xfrm; |
2294be0f | 259 | } |
38320c70 | 260 | |
cac2661c SK |
261 | tmp = ESP_SKB_CB(skb)->tmp; |
262 | esp_ssg_unref(x, tmp); | |
263 | kfree(tmp); | |
f53c7239 SK |
264 | |
265 | if (xo && (xo->flags & XFRM_DEV_RESUME)) { | |
266 | if (err) { | |
267 | XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR); | |
268 | kfree_skb(skb); | |
269 | return; | |
270 | } | |
271 | ||
272 | skb_push(skb, skb->data - skb_mac_header(skb)); | |
273 | secpath_reset(skb); | |
274 | xfrm_dev_resume(skb); | |
275 | } else { | |
e27cca96 SD |
276 | if (!err && |
277 | x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP) | |
278 | esp_output_tail_tcp(x, skb); | |
279 | else | |
9ab1265d | 280 | xfrm_output_resume(skb->sk, skb, err); |
f53c7239 | 281 | } |
38320c70 HX |
282 | } |
283 | ||
7021b2e1 HX |
284 | /* Move ESP header back into place. */ |
285 | static void esp_restore_header(struct sk_buff *skb, unsigned int offset) | |
286 | { | |
287 | struct ip_esp_hdr *esph = (void *)(skb->data + offset); | |
288 | void *tmp = ESP_SKB_CB(skb)->tmp; | |
962fcef3 | 289 | __be32 *seqhi = esp_tmp_extra(tmp); |
7021b2e1 HX |
290 | |
291 | esph->seq_no = esph->spi; | |
292 | esph->spi = *seqhi; | |
293 | } | |
294 | ||
295 | static void esp_output_restore_header(struct sk_buff *skb) | |
296 | { | |
962fcef3 HX |
297 | void *tmp = ESP_SKB_CB(skb)->tmp; |
298 | struct esp_output_extra *extra = esp_tmp_extra(tmp); | |
299 | ||
300 | esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff - | |
301 | sizeof(__be32)); | |
7021b2e1 HX |
302 | } |
303 | ||
cac2661c | 304 | static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb, |
fca11ebd | 305 | struct xfrm_state *x, |
cac2661c SK |
306 | struct ip_esp_hdr *esph, |
307 | struct esp_output_extra *extra) | |
308 | { | |
cac2661c | 309 | /* For ESN we move the header forward by 4 bytes to |
cbd801b3 | 310 | * accommodate the high bits. We will move it back after |
cac2661c SK |
311 | * encryption. |
312 | */ | |
313 | if ((x->props.flags & XFRM_STATE_ESN)) { | |
7862b405 SK |
314 | __u32 seqhi; |
315 | struct xfrm_offload *xo = xfrm_offload(skb); | |
316 | ||
317 | if (xo) | |
318 | seqhi = xo->seq.hi; | |
319 | else | |
320 | seqhi = XFRM_SKB_CB(skb)->seq.output.hi; | |
321 | ||
cac2661c SK |
322 | extra->esphoff = (unsigned char *)esph - |
323 | skb_transport_header(skb); | |
324 | esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4); | |
325 | extra->seqhi = esph->spi; | |
7862b405 | 326 | esph->seq_no = htonl(seqhi); |
cac2661c SK |
327 | } |
328 | ||
329 | esph->spi = x->id.spi; | |
330 | ||
331 | return esph; | |
332 | } | |
333 | ||
7021b2e1 HX |
334 | static void esp_output_done_esn(struct crypto_async_request *base, int err) |
335 | { | |
336 | struct sk_buff *skb = base->data; | |
337 | ||
338 | esp_output_restore_header(skb); | |
339 | esp_output_done(base, err); | |
340 | } | |
341 | ||
eecd227a SD |
342 | static struct ip_esp_hdr *esp_output_udp_encap(struct sk_buff *skb, |
343 | int encap_type, | |
344 | struct esp_info *esp, | |
345 | __be16 sport, | |
346 | __be16 dport) | |
1da177e4 | 347 | { |
fca11ebd SK |
348 | struct udphdr *uh; |
349 | __be32 *udpdata32; | |
8dfb4eba | 350 | unsigned int len; |
fca11ebd | 351 | |
8dfb4eba | 352 | len = skb->len + esp->tailen - skb_transport_offset(skb); |
e27cca96 | 353 | if (len + sizeof(struct iphdr) > IP_MAX_MTU) |
eecd227a | 354 | return ERR_PTR(-EMSGSIZE); |
8dfb4eba | 355 | |
eecd227a | 356 | uh = (struct udphdr *)esp->esph; |
fca11ebd SK |
357 | uh->source = sport; |
358 | uh->dest = dport; | |
8dfb4eba | 359 | uh->len = htons(len); |
fca11ebd SK |
360 | uh->check = 0; |
361 | ||
eecd227a SD |
362 | *skb_mac_header(skb) = IPPROTO_UDP; |
363 | ||
364 | if (encap_type == UDP_ENCAP_ESPINUDP_NON_IKE) { | |
365 | udpdata32 = (__be32 *)(uh + 1); | |
366 | udpdata32[0] = udpdata32[1] = 0; | |
367 | return (struct ip_esp_hdr *)(udpdata32 + 2); | |
368 | } | |
369 | ||
370 | return (struct ip_esp_hdr *)(uh + 1); | |
371 | } | |
372 | ||
e27cca96 SD |
373 | #ifdef CONFIG_INET_ESPINTCP |
374 | static struct ip_esp_hdr *esp_output_tcp_encap(struct xfrm_state *x, | |
375 | struct sk_buff *skb, | |
376 | struct esp_info *esp) | |
377 | { | |
378 | __be16 *lenp = (void *)esp->esph; | |
379 | struct ip_esp_hdr *esph; | |
380 | unsigned int len; | |
381 | struct sock *sk; | |
382 | ||
383 | len = skb->len + esp->tailen - skb_transport_offset(skb); | |
384 | if (len > IP_MAX_MTU) | |
385 | return ERR_PTR(-EMSGSIZE); | |
386 | ||
387 | rcu_read_lock(); | |
388 | sk = esp_find_tcp_sk(x); | |
389 | rcu_read_unlock(); | |
390 | ||
391 | if (IS_ERR(sk)) | |
392 | return ERR_CAST(sk); | |
393 | ||
394 | *lenp = htons(len); | |
395 | esph = (struct ip_esp_hdr *)(lenp + 1); | |
396 | ||
397 | return esph; | |
398 | } | |
399 | #else | |
400 | static struct ip_esp_hdr *esp_output_tcp_encap(struct xfrm_state *x, | |
401 | struct sk_buff *skb, | |
402 | struct esp_info *esp) | |
403 | { | |
404 | return ERR_PTR(-EOPNOTSUPP); | |
405 | } | |
406 | #endif | |
407 | ||
eecd227a SD |
408 | static int esp_output_encap(struct xfrm_state *x, struct sk_buff *skb, |
409 | struct esp_info *esp) | |
410 | { | |
411 | struct xfrm_encap_tmpl *encap = x->encap; | |
412 | struct ip_esp_hdr *esph; | |
413 | __be16 sport, dport; | |
414 | int encap_type; | |
415 | ||
416 | spin_lock_bh(&x->lock); | |
417 | sport = encap->encap_sport; | |
418 | dport = encap->encap_dport; | |
419 | encap_type = encap->encap_type; | |
420 | spin_unlock_bh(&x->lock); | |
421 | ||
fca11ebd SK |
422 | switch (encap_type) { |
423 | default: | |
424 | case UDP_ENCAP_ESPINUDP: | |
fca11ebd | 425 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
eecd227a | 426 | esph = esp_output_udp_encap(skb, encap_type, esp, sport, dport); |
fca11ebd | 427 | break; |
e27cca96 SD |
428 | case TCP_ENCAP_ESPINTCP: |
429 | esph = esp_output_tcp_encap(x, skb, esp); | |
430 | break; | |
d979e20f | 431 | } |
0dc49e9b | 432 | |
eecd227a SD |
433 | if (IS_ERR(esph)) |
434 | return PTR_ERR(esph); | |
435 | ||
fca11ebd | 436 | esp->esph = esph; |
8dfb4eba SD |
437 | |
438 | return 0; | |
fca11ebd | 439 | } |
0dc49e9b | 440 | |
fca11ebd SK |
441 | int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp) |
442 | { | |
443 | u8 *tail; | |
fca11ebd | 444 | int nfrags; |
0e78a873 | 445 | int esph_offset; |
fca11ebd SK |
446 | struct page *page; |
447 | struct sk_buff *trailer; | |
448 | int tailen = esp->tailen; | |
1da177e4 | 449 | |
e27cca96 | 450 | /* this is non-NULL only with TCP/UDP Encapsulation */ |
8dfb4eba | 451 | if (x->encap) { |
eecd227a | 452 | int err = esp_output_encap(x, skb, esp); |
8dfb4eba SD |
453 | |
454 | if (err < 0) | |
455 | return err; | |
456 | } | |
1da177e4 | 457 | |
cac2661c | 458 | if (!skb_cloned(skb)) { |
54ffd790 | 459 | if (tailen <= skb_tailroom(skb)) { |
cac2661c SK |
460 | nfrags = 1; |
461 | trailer = skb; | |
462 | tail = skb_tail_pointer(trailer); | |
1da177e4 | 463 | |
cac2661c SK |
464 | goto skip_cow; |
465 | } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS) | |
466 | && !skb_has_frag_list(skb)) { | |
467 | int allocsize; | |
468 | struct sock *sk = skb->sk; | |
469 | struct page_frag *pfrag = &x->xfrag; | |
7021b2e1 | 470 | |
fca11ebd SK |
471 | esp->inplace = false; |
472 | ||
cac2661c SK |
473 | allocsize = ALIGN(tailen, L1_CACHE_BYTES); |
474 | ||
475 | spin_lock_bh(&x->lock); | |
476 | ||
477 | if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) { | |
478 | spin_unlock_bh(&x->lock); | |
479 | goto cow; | |
480 | } | |
481 | ||
482 | page = pfrag->page; | |
483 | get_page(page); | |
484 | ||
9bd6b629 | 485 | tail = page_address(page) + pfrag->offset; |
cac2661c | 486 | |
fca11ebd | 487 | esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto); |
cac2661c | 488 | |
cac2661c SK |
489 | nfrags = skb_shinfo(skb)->nr_frags; |
490 | ||
491 | __skb_fill_page_desc(skb, nfrags, page, pfrag->offset, | |
492 | tailen); | |
493 | skb_shinfo(skb)->nr_frags = ++nfrags; | |
494 | ||
495 | pfrag->offset = pfrag->offset + allocsize; | |
36ff0dd3 SK |
496 | |
497 | spin_unlock_bh(&x->lock); | |
498 | ||
cac2661c SK |
499 | nfrags++; |
500 | ||
501 | skb->len += tailen; | |
502 | skb->data_len += tailen; | |
503 | skb->truesize += tailen; | |
09db5124 | 504 | if (sk && sk_fullsock(sk)) |
14afee4b | 505 | refcount_add(tailen, &sk->sk_wmem_alloc); |
cac2661c | 506 | |
fca11ebd | 507 | goto out; |
cac2661c | 508 | } |
7021b2e1 HX |
509 | } |
510 | ||
cac2661c | 511 | cow: |
0e78a873 SK |
512 | esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb); |
513 | ||
fca11ebd SK |
514 | nfrags = skb_cow_data(skb, tailen, &trailer); |
515 | if (nfrags < 0) | |
516 | goto out; | |
cac2661c | 517 | tail = skb_tail_pointer(trailer); |
0e78a873 | 518 | esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset); |
cac2661c SK |
519 | |
520 | skip_cow: | |
fca11ebd SK |
521 | esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto); |
522 | pskb_put(skb, trailer, tailen); | |
cac2661c | 523 | |
fca11ebd SK |
524 | out: |
525 | return nfrags; | |
526 | } | |
527 | EXPORT_SYMBOL_GPL(esp_output_head); | |
528 | ||
529 | int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp) | |
530 | { | |
531 | u8 *iv; | |
532 | int alen; | |
533 | void *tmp; | |
534 | int ivlen; | |
535 | int assoclen; | |
536 | int extralen; | |
537 | struct page *page; | |
538 | struct ip_esp_hdr *esph; | |
539 | struct crypto_aead *aead; | |
540 | struct aead_request *req; | |
541 | struct scatterlist *sg, *dsg; | |
542 | struct esp_output_extra *extra; | |
543 | int err = -ENOMEM; | |
544 | ||
545 | assoclen = sizeof(struct ip_esp_hdr); | |
546 | extralen = 0; | |
547 | ||
548 | if (x->props.flags & XFRM_STATE_ESN) { | |
549 | extralen += sizeof(*extra); | |
550 | assoclen += sizeof(__be32); | |
551 | } | |
7021b2e1 | 552 | |
fca11ebd SK |
553 | aead = x->data; |
554 | alen = crypto_aead_authsize(aead); | |
555 | ivlen = crypto_aead_ivsize(aead); | |
556 | ||
557 | tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen); | |
e892d2d4 | 558 | if (!tmp) |
cac2661c | 559 | goto error; |
cac2661c SK |
560 | |
561 | extra = esp_tmp_extra(tmp); | |
562 | iv = esp_tmp_iv(aead, tmp, extralen); | |
563 | req = esp_tmp_req(aead, iv); | |
564 | sg = esp_req_sg(aead, req); | |
cac2661c | 565 | |
fca11ebd SK |
566 | if (esp->inplace) |
567 | dsg = sg; | |
568 | else | |
569 | dsg = &sg[esp->nfrags]; | |
cac2661c | 570 | |
fca11ebd SK |
571 | esph = esp_output_set_extra(skb, x, esp->esph, extra); |
572 | esp->esph = esph; | |
573 | ||
574 | sg_init_table(sg, esp->nfrags); | |
3f297707 JD |
575 | err = skb_to_sgvec(skb, sg, |
576 | (unsigned char *)esph - skb->data, | |
577 | assoclen + ivlen + esp->clen + alen); | |
578 | if (unlikely(err < 0)) | |
e6194923 | 579 | goto error_free; |
fca11ebd SK |
580 | |
581 | if (!esp->inplace) { | |
582 | int allocsize; | |
583 | struct page_frag *pfrag = &x->xfrag; | |
584 | ||
585 | allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES); | |
586 | ||
587 | spin_lock_bh(&x->lock); | |
588 | if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) { | |
589 | spin_unlock_bh(&x->lock); | |
e6194923 | 590 | goto error_free; |
fca11ebd SK |
591 | } |
592 | ||
593 | skb_shinfo(skb)->nr_frags = 1; | |
594 | ||
595 | page = pfrag->page; | |
596 | get_page(page); | |
597 | /* replace page frags in skb with new page */ | |
598 | __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len); | |
599 | pfrag->offset = pfrag->offset + allocsize; | |
600 | spin_unlock_bh(&x->lock); | |
601 | ||
602 | sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1); | |
3f297707 JD |
603 | err = skb_to_sgvec(skb, dsg, |
604 | (unsigned char *)esph - skb->data, | |
605 | assoclen + ivlen + esp->clen + alen); | |
606 | if (unlikely(err < 0)) | |
e6194923 | 607 | goto error_free; |
fca11ebd | 608 | } |
0dc49e9b | 609 | |
cac2661c SK |
610 | if ((x->props.flags & XFRM_STATE_ESN)) |
611 | aead_request_set_callback(req, 0, esp_output_done_esn, skb); | |
612 | else | |
613 | aead_request_set_callback(req, 0, esp_output_done, skb); | |
614 | ||
fca11ebd | 615 | aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv); |
7021b2e1 HX |
616 | aead_request_set_ad(req, assoclen); |
617 | ||
7021b2e1 | 618 | memset(iv, 0, ivlen); |
fca11ebd | 619 | memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8), |
7021b2e1 | 620 | min(ivlen, 8)); |
38320c70 HX |
621 | |
622 | ESP_SKB_CB(skb)->tmp = tmp; | |
7021b2e1 HX |
623 | err = crypto_aead_encrypt(req); |
624 | ||
625 | switch (err) { | |
626 | case -EINPROGRESS: | |
38320c70 | 627 | goto error; |
1da177e4 | 628 | |
068c2e70 | 629 | case -ENOSPC: |
38320c70 | 630 | err = NET_XMIT_DROP; |
7021b2e1 HX |
631 | break; |
632 | ||
633 | case 0: | |
634 | if ((x->props.flags & XFRM_STATE_ESN)) | |
635 | esp_output_restore_header(skb); | |
636 | } | |
1da177e4 | 637 | |
cac2661c SK |
638 | if (sg != dsg) |
639 | esp_ssg_unref(x, tmp); | |
b7c6538c | 640 | |
e27cca96 SD |
641 | if (!err && x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP) |
642 | err = esp_output_tail_tcp(x, skb); | |
643 | ||
e6194923 SK |
644 | error_free: |
645 | kfree(tmp); | |
1da177e4 LT |
646 | error: |
647 | return err; | |
648 | } | |
fca11ebd SK |
649 | EXPORT_SYMBOL_GPL(esp_output_tail); |
650 | ||
651 | static int esp_output(struct xfrm_state *x, struct sk_buff *skb) | |
652 | { | |
653 | int alen; | |
654 | int blksize; | |
655 | struct ip_esp_hdr *esph; | |
656 | struct crypto_aead *aead; | |
657 | struct esp_info esp; | |
658 | ||
659 | esp.inplace = true; | |
660 | ||
661 | esp.proto = *skb_mac_header(skb); | |
662 | *skb_mac_header(skb) = IPPROTO_ESP; | |
663 | ||
664 | /* skb is pure payload to encrypt */ | |
665 | ||
666 | aead = x->data; | |
667 | alen = crypto_aead_authsize(aead); | |
668 | ||
669 | esp.tfclen = 0; | |
670 | if (x->tfcpad) { | |
671 | struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb); | |
672 | u32 padto; | |
673 | ||
b515d263 | 674 | padto = min(x->tfcpad, __xfrm_state_mtu(x, dst->child_mtu_cached)); |
fca11ebd SK |
675 | if (skb->len < padto) |
676 | esp.tfclen = padto - skb->len; | |
677 | } | |
678 | blksize = ALIGN(crypto_aead_blocksize(aead), 4); | |
679 | esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize); | |
680 | esp.plen = esp.clen - skb->len - esp.tfclen; | |
681 | esp.tailen = esp.tfclen + esp.plen + alen; | |
682 | ||
683 | esp.esph = ip_esp_hdr(skb); | |
684 | ||
685 | esp.nfrags = esp_output_head(x, skb, &esp); | |
686 | if (esp.nfrags < 0) | |
687 | return esp.nfrags; | |
688 | ||
689 | esph = esp.esph; | |
690 | esph->spi = x->id.spi; | |
691 | ||
692 | esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low); | |
693 | esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low + | |
694 | ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32)); | |
695 | ||
696 | skb_push(skb, -skb_network_offset(skb)); | |
697 | ||
698 | return esp_output_tail(x, skb, &esp); | |
699 | } | |
1da177e4 | 700 | |
47ebcc0b YK |
701 | static inline int esp_remove_trailer(struct sk_buff *skb) |
702 | { | |
703 | struct xfrm_state *x = xfrm_input_state(skb); | |
704 | struct xfrm_offload *xo = xfrm_offload(skb); | |
705 | struct crypto_aead *aead = x->data; | |
706 | int alen, hlen, elen; | |
707 | int padlen, trimlen; | |
708 | __wsum csumdiff; | |
709 | u8 nexthdr[2]; | |
710 | int ret; | |
711 | ||
712 | alen = crypto_aead_authsize(aead); | |
713 | hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead); | |
714 | elen = skb->len - hlen; | |
715 | ||
716 | if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) { | |
717 | ret = xo->proto; | |
718 | goto out; | |
719 | } | |
720 | ||
721 | if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2)) | |
722 | BUG(); | |
723 | ||
724 | ret = -EINVAL; | |
725 | padlen = nexthdr[0]; | |
726 | if (padlen + 2 + alen >= elen) { | |
727 | net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n", | |
728 | padlen + 2, elen - alen); | |
729 | goto out; | |
730 | } | |
731 | ||
732 | trimlen = alen + padlen + 2; | |
733 | if (skb->ip_summed == CHECKSUM_COMPLETE) { | |
734 | csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0); | |
735 | skb->csum = csum_block_sub(skb->csum, csumdiff, | |
736 | skb->len - trimlen); | |
737 | } | |
738 | pskb_trim(skb, skb->len - trimlen); | |
739 | ||
740 | ret = nexthdr[1]; | |
741 | ||
742 | out: | |
743 | return ret; | |
744 | } | |
745 | ||
fca11ebd | 746 | int esp_input_done2(struct sk_buff *skb, int err) |
1da177e4 | 747 | { |
b71d1d42 | 748 | const struct iphdr *iph; |
38320c70 | 749 | struct xfrm_state *x = xfrm_input_state(skb); |
d77e38e6 | 750 | struct xfrm_offload *xo = xfrm_offload(skb); |
1c5ad13f | 751 | struct crypto_aead *aead = x->data; |
38320c70 | 752 | int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead); |
31a4ab93 | 753 | int ihl; |
1da177e4 | 754 | |
1d9bfacd | 755 | if (!xo || !(xo->flags & CRYPTO_DONE)) |
d77e38e6 | 756 | kfree(ESP_SKB_CB(skb)->tmp); |
0ebea8ef | 757 | |
6b7326c8 | 758 | if (unlikely(err)) |
668dc8af | 759 | goto out; |
1da177e4 | 760 | |
47ebcc0b YK |
761 | err = esp_remove_trailer(skb); |
762 | if (unlikely(err < 0)) | |
4bf05ece | 763 | goto out; |
1da177e4 | 764 | |
eddc9ec5 | 765 | iph = ip_hdr(skb); |
31a4ab93 HX |
766 | ihl = iph->ihl * 4; |
767 | ||
752c1f4c HX |
768 | if (x->encap) { |
769 | struct xfrm_encap_tmpl *encap = x->encap; | |
e27cca96 | 770 | struct tcphdr *th = (void *)(skb_network_header(skb) + ihl); |
d56f90a7 | 771 | struct udphdr *uh = (void *)(skb_network_header(skb) + ihl); |
25f6802b SD |
772 | __be16 source; |
773 | ||
774 | switch (x->encap->encap_type) { | |
e27cca96 SD |
775 | case TCP_ENCAP_ESPINTCP: |
776 | source = th->source; | |
777 | break; | |
25f6802b SD |
778 | case UDP_ENCAP_ESPINUDP: |
779 | case UDP_ENCAP_ESPINUDP_NON_IKE: | |
780 | source = uh->source; | |
781 | break; | |
782 | default: | |
783 | WARN_ON_ONCE(1); | |
784 | err = -EINVAL; | |
785 | goto out; | |
786 | } | |
752c1f4c HX |
787 | |
788 | /* | |
789 | * 1) if the NAT-T peer's IP or port changed then | |
790 | * advertize the change to the keying daemon. | |
791 | * This is an inbound SA, so just compare | |
792 | * SRC ports. | |
793 | */ | |
794 | if (iph->saddr != x->props.saddr.a4 || | |
25f6802b | 795 | source != encap->encap_sport) { |
752c1f4c HX |
796 | xfrm_address_t ipaddr; |
797 | ||
798 | ipaddr.a4 = iph->saddr; | |
25f6802b | 799 | km_new_mapping(x, &ipaddr, source); |
e905a9ed | 800 | |
752c1f4c HX |
801 | /* XXX: perhaps add an extra |
802 | * policy check here, to see | |
803 | * if we should allow or | |
804 | * reject a packet from a | |
805 | * different source | |
806 | * address/port. | |
807 | */ | |
1da177e4 | 808 | } |
e905a9ed | 809 | |
752c1f4c HX |
810 | /* |
811 | * 2) ignore UDP/TCP checksums in case | |
812 | * of NAT-T in Transport Mode, or | |
813 | * perform other post-processing fixes | |
814 | * as per draft-ietf-ipsec-udp-encaps-06, | |
815 | * section 3.1.2 | |
816 | */ | |
8bd17075 | 817 | if (x->props.mode == XFRM_MODE_TRANSPORT) |
752c1f4c | 818 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
1da177e4 LT |
819 | } |
820 | ||
ec9567a9 | 821 | skb_pull_rcsum(skb, hlen); |
7143dfac LR |
822 | if (x->props.mode == XFRM_MODE_TUNNEL) |
823 | skb_reset_transport_header(skb); | |
824 | else | |
825 | skb_set_transport_header(skb, -ihl); | |
4bf05ece | 826 | |
38320c70 HX |
827 | /* RFC4303: Drop dummy packets without any error */ |
828 | if (err == IPPROTO_NONE) | |
829 | err = -EINVAL; | |
830 | ||
831 | out: | |
832 | return err; | |
833 | } | |
fca11ebd | 834 | EXPORT_SYMBOL_GPL(esp_input_done2); |
38320c70 HX |
835 | |
836 | static void esp_input_done(struct crypto_async_request *base, int err) | |
837 | { | |
838 | struct sk_buff *skb = base->data; | |
839 | ||
840 | xfrm_input_resume(skb, esp_input_done2(skb, err)); | |
841 | } | |
842 | ||
7021b2e1 HX |
843 | static void esp_input_restore_header(struct sk_buff *skb) |
844 | { | |
845 | esp_restore_header(skb, 0); | |
846 | __skb_pull(skb, 4); | |
847 | } | |
848 | ||
cac2661c SK |
849 | static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi) |
850 | { | |
851 | struct xfrm_state *x = xfrm_input_state(skb); | |
60aa8046 | 852 | struct ip_esp_hdr *esph; |
cac2661c SK |
853 | |
854 | /* For ESN we move the header forward by 4 bytes to | |
cbd801b3 | 855 | * accommodate the high bits. We will move it back after |
cac2661c SK |
856 | * decryption. |
857 | */ | |
858 | if ((x->props.flags & XFRM_STATE_ESN)) { | |
d58ff351 | 859 | esph = skb_push(skb, 4); |
cac2661c SK |
860 | *seqhi = esph->spi; |
861 | esph->spi = esph->seq_no; | |
862 | esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi; | |
863 | } | |
864 | } | |
865 | ||
7021b2e1 HX |
866 | static void esp_input_done_esn(struct crypto_async_request *base, int err) |
867 | { | |
868 | struct sk_buff *skb = base->data; | |
869 | ||
870 | esp_input_restore_header(skb); | |
871 | esp_input_done(base, err); | |
872 | } | |
873 | ||
38320c70 HX |
874 | /* |
875 | * Note: detecting truncated vs. non-truncated authentication data is very | |
876 | * expensive, so we only support truncated data, which is the recommended | |
877 | * and common case. | |
878 | */ | |
879 | static int esp_input(struct xfrm_state *x, struct sk_buff *skb) | |
880 | { | |
1c5ad13f | 881 | struct crypto_aead *aead = x->data; |
38320c70 HX |
882 | struct aead_request *req; |
883 | struct sk_buff *trailer; | |
7021b2e1 | 884 | int ivlen = crypto_aead_ivsize(aead); |
0c05f983 | 885 | int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen; |
38320c70 | 886 | int nfrags; |
0dc49e9b | 887 | int assoclen; |
0dc49e9b SK |
888 | int seqhilen; |
889 | __be32 *seqhi; | |
38320c70 HX |
890 | void *tmp; |
891 | u8 *iv; | |
892 | struct scatterlist *sg; | |
38320c70 HX |
893 | int err = -EINVAL; |
894 | ||
0c05f983 | 895 | if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen)) |
38320c70 HX |
896 | goto out; |
897 | ||
898 | if (elen <= 0) | |
899 | goto out; | |
900 | ||
0c05f983 | 901 | assoclen = sizeof(struct ip_esp_hdr); |
0dc49e9b SK |
902 | seqhilen = 0; |
903 | ||
904 | if (x->props.flags & XFRM_STATE_ESN) { | |
0dc49e9b SK |
905 | seqhilen += sizeof(__be32); |
906 | assoclen += seqhilen; | |
907 | } | |
908 | ||
cac2661c SK |
909 | if (!skb_cloned(skb)) { |
910 | if (!skb_is_nonlinear(skb)) { | |
911 | nfrags = 1; | |
912 | ||
913 | goto skip_cow; | |
914 | } else if (!skb_has_frag_list(skb)) { | |
915 | nfrags = skb_shinfo(skb)->nr_frags; | |
916 | nfrags++; | |
917 | ||
918 | goto skip_cow; | |
919 | } | |
920 | } | |
921 | ||
922 | err = skb_cow_data(skb, 0, &trailer); | |
923 | if (err < 0) | |
924 | goto out; | |
925 | ||
926 | nfrags = err; | |
927 | ||
928 | skip_cow: | |
38320c70 | 929 | err = -ENOMEM; |
7021b2e1 | 930 | tmp = esp_alloc_tmp(aead, nfrags, seqhilen); |
38320c70 HX |
931 | if (!tmp) |
932 | goto out; | |
933 | ||
934 | ESP_SKB_CB(skb)->tmp = tmp; | |
962fcef3 | 935 | seqhi = esp_tmp_extra(tmp); |
0dc49e9b | 936 | iv = esp_tmp_iv(aead, tmp, seqhilen); |
38320c70 | 937 | req = esp_tmp_req(aead, iv); |
7021b2e1 | 938 | sg = esp_req_sg(aead, req); |
38320c70 | 939 | |
cac2661c | 940 | esp_input_set_header(skb, seqhi); |
38320c70 | 941 | |
cac2661c | 942 | sg_init_table(sg, nfrags); |
3f297707 | 943 | err = skb_to_sgvec(skb, sg, 0, skb->len); |
e6194923 SK |
944 | if (unlikely(err < 0)) { |
945 | kfree(tmp); | |
3f297707 | 946 | goto out; |
e6194923 | 947 | } |
38320c70 | 948 | |
cac2661c | 949 | skb->ip_summed = CHECKSUM_NONE; |
0dc49e9b | 950 | |
cac2661c | 951 | if ((x->props.flags & XFRM_STATE_ESN)) |
7021b2e1 | 952 | aead_request_set_callback(req, 0, esp_input_done_esn, skb); |
cac2661c SK |
953 | else |
954 | aead_request_set_callback(req, 0, esp_input_done, skb); | |
7021b2e1 HX |
955 | |
956 | aead_request_set_crypt(req, sg, sg, elen + ivlen, iv); | |
957 | aead_request_set_ad(req, assoclen); | |
38320c70 HX |
958 | |
959 | err = crypto_aead_decrypt(req); | |
960 | if (err == -EINPROGRESS) | |
961 | goto out; | |
962 | ||
7021b2e1 HX |
963 | if ((x->props.flags & XFRM_STATE_ESN)) |
964 | esp_input_restore_header(skb); | |
965 | ||
38320c70 | 966 | err = esp_input_done2(skb, err); |
1da177e4 LT |
967 | |
968 | out: | |
668dc8af | 969 | return err; |
1da177e4 LT |
970 | } |
971 | ||
827789cb | 972 | static int esp4_err(struct sk_buff *skb, u32 info) |
1da177e4 | 973 | { |
4fb236ba | 974 | struct net *net = dev_net(skb->dev); |
b71d1d42 | 975 | const struct iphdr *iph = (const struct iphdr *)skb->data; |
d9319100 | 976 | struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2)); |
1da177e4 LT |
977 | struct xfrm_state *x; |
978 | ||
55be7a9c DM |
979 | switch (icmp_hdr(skb)->type) { |
980 | case ICMP_DEST_UNREACH: | |
981 | if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED) | |
827789cb | 982 | return 0; |
79121184 | 983 | break; |
55be7a9c DM |
984 | case ICMP_REDIRECT: |
985 | break; | |
986 | default: | |
827789cb | 987 | return 0; |
55be7a9c | 988 | } |
1da177e4 | 989 | |
b71d1d42 ED |
990 | x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr, |
991 | esph->spi, IPPROTO_ESP, AF_INET); | |
1da177e4 | 992 | if (!x) |
827789cb | 993 | return 0; |
55be7a9c | 994 | |
387aa65a | 995 | if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH) |
d888f396 | 996 | ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ESP); |
387aa65a | 997 | else |
1042caa7 | 998 | ipv4_redirect(skb, net, 0, IPPROTO_ESP); |
1da177e4 | 999 | xfrm_state_put(x); |
827789cb SK |
1000 | |
1001 | return 0; | |
1da177e4 LT |
1002 | } |
1003 | ||
1004 | static void esp_destroy(struct xfrm_state *x) | |
1005 | { | |
1c5ad13f | 1006 | struct crypto_aead *aead = x->data; |
1da177e4 | 1007 | |
1c5ad13f | 1008 | if (!aead) |
1da177e4 LT |
1009 | return; |
1010 | ||
1c5ad13f | 1011 | crypto_free_aead(aead); |
1da177e4 LT |
1012 | } |
1013 | ||
1a6509d9 | 1014 | static int esp_init_aead(struct xfrm_state *x) |
1da177e4 | 1015 | { |
7021b2e1 | 1016 | char aead_name[CRYPTO_MAX_ALG_NAME]; |
1a6509d9 HX |
1017 | struct crypto_aead *aead; |
1018 | int err; | |
1019 | ||
7021b2e1 HX |
1020 | err = -ENAMETOOLONG; |
1021 | if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", | |
1022 | x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME) | |
1023 | goto error; | |
1024 | ||
f58869c4 | 1025 | aead = crypto_alloc_aead(aead_name, 0, 0); |
1a6509d9 HX |
1026 | err = PTR_ERR(aead); |
1027 | if (IS_ERR(aead)) | |
1028 | goto error; | |
1029 | ||
1c5ad13f | 1030 | x->data = aead; |
1a6509d9 HX |
1031 | |
1032 | err = crypto_aead_setkey(aead, x->aead->alg_key, | |
1033 | (x->aead->alg_key_len + 7) / 8); | |
1034 | if (err) | |
1035 | goto error; | |
1036 | ||
1037 | err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8); | |
1038 | if (err) | |
1039 | goto error; | |
1040 | ||
1041 | error: | |
1042 | return err; | |
1043 | } | |
1044 | ||
1045 | static int esp_init_authenc(struct xfrm_state *x) | |
1046 | { | |
38320c70 HX |
1047 | struct crypto_aead *aead; |
1048 | struct crypto_authenc_key_param *param; | |
1049 | struct rtattr *rta; | |
1050 | char *key; | |
1051 | char *p; | |
1052 | char authenc_name[CRYPTO_MAX_ALG_NAME]; | |
38320c70 HX |
1053 | unsigned int keylen; |
1054 | int err; | |
1da177e4 | 1055 | |
1a6509d9 | 1056 | err = -EINVAL; |
51456b29 | 1057 | if (!x->ealg) |
1a6509d9 | 1058 | goto error; |
38320c70 | 1059 | |
1a6509d9 | 1060 | err = -ENAMETOOLONG; |
0dc49e9b SK |
1061 | |
1062 | if ((x->props.flags & XFRM_STATE_ESN)) { | |
1063 | if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, | |
7021b2e1 HX |
1064 | "%s%sauthencesn(%s,%s)%s", |
1065 | x->geniv ?: "", x->geniv ? "(" : "", | |
0dc49e9b | 1066 | x->aalg ? x->aalg->alg_name : "digest_null", |
7021b2e1 HX |
1067 | x->ealg->alg_name, |
1068 | x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) | |
0dc49e9b SK |
1069 | goto error; |
1070 | } else { | |
1071 | if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, | |
7021b2e1 HX |
1072 | "%s%sauthenc(%s,%s)%s", |
1073 | x->geniv ?: "", x->geniv ? "(" : "", | |
0dc49e9b | 1074 | x->aalg ? x->aalg->alg_name : "digest_null", |
7021b2e1 HX |
1075 | x->ealg->alg_name, |
1076 | x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME) | |
0dc49e9b SK |
1077 | goto error; |
1078 | } | |
38320c70 | 1079 | |
f58869c4 | 1080 | aead = crypto_alloc_aead(authenc_name, 0, 0); |
38320c70 HX |
1081 | err = PTR_ERR(aead); |
1082 | if (IS_ERR(aead)) | |
1083 | goto error; | |
1084 | ||
1c5ad13f | 1085 | x->data = aead; |
38320c70 HX |
1086 | |
1087 | keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) + | |
1088 | (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param)); | |
1089 | err = -ENOMEM; | |
1090 | key = kmalloc(keylen, GFP_KERNEL); | |
1091 | if (!key) | |
1092 | goto error; | |
1093 | ||
1094 | p = key; | |
1095 | rta = (void *)p; | |
1096 | rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM; | |
1097 | rta->rta_len = RTA_LENGTH(sizeof(*param)); | |
1098 | param = RTA_DATA(rta); | |
1099 | p += RTA_SPACE(sizeof(*param)); | |
1100 | ||
1da177e4 LT |
1101 | if (x->aalg) { |
1102 | struct xfrm_algo_desc *aalg_desc; | |
1103 | ||
38320c70 HX |
1104 | memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8); |
1105 | p += (x->aalg->alg_key_len + 7) / 8; | |
1da177e4 LT |
1106 | |
1107 | aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); | |
1108 | BUG_ON(!aalg_desc); | |
1109 | ||
38320c70 | 1110 | err = -EINVAL; |
45083497 | 1111 | if (aalg_desc->uinfo.auth.icv_fullbits / 8 != |
38320c70 | 1112 | crypto_aead_authsize(aead)) { |
45083497 JP |
1113 | pr_info("ESP: %s digestsize %u != %hu\n", |
1114 | x->aalg->alg_name, | |
1115 | crypto_aead_authsize(aead), | |
1116 | aalg_desc->uinfo.auth.icv_fullbits / 8); | |
38320c70 | 1117 | goto free_key; |
1da177e4 LT |
1118 | } |
1119 | ||
38320c70 | 1120 | err = crypto_aead_setauthsize( |
8f8a088c | 1121 | aead, x->aalg->alg_trunc_len / 8); |
38320c70 HX |
1122 | if (err) |
1123 | goto free_key; | |
1da177e4 | 1124 | } |
4b7137ff | 1125 | |
38320c70 HX |
1126 | param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8); |
1127 | memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8); | |
1128 | ||
1129 | err = crypto_aead_setkey(aead, key, keylen); | |
1130 | ||
1131 | free_key: | |
1132 | kfree(key); | |
1133 | ||
1a6509d9 HX |
1134 | error: |
1135 | return err; | |
1136 | } | |
1137 | ||
1138 | static int esp_init_state(struct xfrm_state *x) | |
1139 | { | |
1a6509d9 HX |
1140 | struct crypto_aead *aead; |
1141 | u32 align; | |
1142 | int err; | |
1143 | ||
1c5ad13f | 1144 | x->data = NULL; |
1a6509d9 HX |
1145 | |
1146 | if (x->aead) | |
1147 | err = esp_init_aead(x); | |
1148 | else | |
1149 | err = esp_init_authenc(x); | |
1150 | ||
38320c70 | 1151 | if (err) |
1da177e4 | 1152 | goto error; |
38320c70 | 1153 | |
1c5ad13f | 1154 | aead = x->data; |
1a6509d9 | 1155 | |
38320c70 HX |
1156 | x->props.header_len = sizeof(struct ip_esp_hdr) + |
1157 | crypto_aead_ivsize(aead); | |
7e49e6de | 1158 | if (x->props.mode == XFRM_MODE_TUNNEL) |
1da177e4 | 1159 | x->props.header_len += sizeof(struct iphdr); |
eb49e630 | 1160 | else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6) |
ac758e3c | 1161 | x->props.header_len += IPV4_BEET_PHMAXLEN; |
1da177e4 LT |
1162 | if (x->encap) { |
1163 | struct xfrm_encap_tmpl *encap = x->encap; | |
1164 | ||
1165 | switch (encap->encap_type) { | |
1166 | default: | |
bcfd09f7 | 1167 | err = -EINVAL; |
1da177e4 LT |
1168 | goto error; |
1169 | case UDP_ENCAP_ESPINUDP: | |
1170 | x->props.header_len += sizeof(struct udphdr); | |
1171 | break; | |
1172 | case UDP_ENCAP_ESPINUDP_NON_IKE: | |
1173 | x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32); | |
1174 | break; | |
e27cca96 SD |
1175 | #ifdef CONFIG_INET_ESPINTCP |
1176 | case TCP_ENCAP_ESPINTCP: | |
1177 | /* only the length field, TCP encap is done by | |
1178 | * the socket | |
1179 | */ | |
1180 | x->props.header_len += 2; | |
1181 | break; | |
1182 | #endif | |
1da177e4 LT |
1183 | } |
1184 | } | |
38320c70 HX |
1185 | |
1186 | align = ALIGN(crypto_aead_blocksize(aead), 4); | |
1c5ad13f | 1187 | x->props.trailer_len = align + 1 + crypto_aead_authsize(aead); |
1da177e4 LT |
1188 | |
1189 | error: | |
38320c70 | 1190 | return err; |
1da177e4 LT |
1191 | } |
1192 | ||
827789cb SK |
1193 | static int esp4_rcv_cb(struct sk_buff *skb, int err) |
1194 | { | |
1195 | return 0; | |
1196 | } | |
1197 | ||
533cb5b0 | 1198 | static const struct xfrm_type esp_type = |
1da177e4 | 1199 | { |
1da177e4 LT |
1200 | .owner = THIS_MODULE, |
1201 | .proto = IPPROTO_ESP, | |
436a0a40 | 1202 | .flags = XFRM_TYPE_REPLAY_PROT, |
1da177e4 LT |
1203 | .init_state = esp_init_state, |
1204 | .destructor = esp_destroy, | |
1da177e4 | 1205 | .input = esp_input, |
fca11ebd | 1206 | .output = esp_output, |
1da177e4 LT |
1207 | }; |
1208 | ||
827789cb | 1209 | static struct xfrm4_protocol esp4_protocol = { |
1da177e4 | 1210 | .handler = xfrm4_rcv, |
827789cb SK |
1211 | .input_handler = xfrm_input, |
1212 | .cb_handler = esp4_rcv_cb, | |
1da177e4 | 1213 | .err_handler = esp4_err, |
827789cb | 1214 | .priority = 0, |
1da177e4 LT |
1215 | }; |
1216 | ||
1217 | static int __init esp4_init(void) | |
1218 | { | |
1da177e4 | 1219 | if (xfrm_register_type(&esp_type, AF_INET) < 0) { |
058bd4d2 | 1220 | pr_info("%s: can't add xfrm type\n", __func__); |
1da177e4 LT |
1221 | return -EAGAIN; |
1222 | } | |
827789cb | 1223 | if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) { |
058bd4d2 | 1224 | pr_info("%s: can't add protocol\n", __func__); |
1da177e4 LT |
1225 | xfrm_unregister_type(&esp_type, AF_INET); |
1226 | return -EAGAIN; | |
1227 | } | |
1228 | return 0; | |
1229 | } | |
1230 | ||
1231 | static void __exit esp4_fini(void) | |
1232 | { | |
827789cb | 1233 | if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0) |
058bd4d2 | 1234 | pr_info("%s: can't remove protocol\n", __func__); |
4f518e80 | 1235 | xfrm_unregister_type(&esp_type, AF_INET); |
1da177e4 LT |
1236 | } |
1237 | ||
1238 | module_init(esp4_init); | |
1239 | module_exit(esp4_fini); | |
1240 | MODULE_LICENSE("GPL"); | |
d3d6dd3a | 1241 | MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP); |