1 #define pr_fmt(fmt) "IPsec: " fmt
3 #include <crypto/aead.h>
4 #include <crypto/authenc.h>
6 #include <linux/module.h>
10 #include <linux/scatterlist.h>
11 #include <linux/kernel.h>
12 #include <linux/pfkeyv2.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/in6.h>
18 #include <net/protocol.h>
21 #include <linux/highmem.h>
24 struct xfrm_skb_cb xfrm
;
28 struct esp_output_extra
{
33 #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
35 static u32
esp4_get_mtu(struct xfrm_state
*x
, int mtu
);
38 * Allocate an AEAD request structure with extra space for SG and IV.
40 * For alignment considerations the IV is placed at the front, followed
41 * by the request and finally the SG list.
43 * TODO: Use spare space in skb for this where possible.
45 static void *esp_alloc_tmp(struct crypto_aead
*aead
, int nfrags
, int extralen
)
51 len
+= crypto_aead_ivsize(aead
);
54 len
+= crypto_aead_alignmask(aead
) &
55 ~(crypto_tfm_ctx_alignment() - 1);
56 len
= ALIGN(len
, crypto_tfm_ctx_alignment());
59 len
+= sizeof(struct aead_request
) + crypto_aead_reqsize(aead
);
60 len
= ALIGN(len
, __alignof__(struct scatterlist
));
62 len
+= sizeof(struct scatterlist
) * nfrags
;
64 return kmalloc(len
, GFP_ATOMIC
);
67 static inline void *esp_tmp_extra(void *tmp
)
69 return PTR_ALIGN(tmp
, __alignof__(struct esp_output_extra
));
72 static inline u8
*esp_tmp_iv(struct crypto_aead
*aead
, void *tmp
, int extralen
)
74 return crypto_aead_ivsize(aead
) ?
75 PTR_ALIGN((u8
*)tmp
+ extralen
,
76 crypto_aead_alignmask(aead
) + 1) : tmp
+ extralen
;
79 static inline struct aead_request
*esp_tmp_req(struct crypto_aead
*aead
, u8
*iv
)
81 struct aead_request
*req
;
83 req
= (void *)PTR_ALIGN(iv
+ crypto_aead_ivsize(aead
),
84 crypto_tfm_ctx_alignment());
85 aead_request_set_tfm(req
, aead
);
89 static inline struct scatterlist
*esp_req_sg(struct crypto_aead
*aead
,
90 struct aead_request
*req
)
92 return (void *)ALIGN((unsigned long)(req
+ 1) +
93 crypto_aead_reqsize(aead
),
94 __alignof__(struct scatterlist
));
97 static void esp_ssg_unref(struct xfrm_state
*x
, void *tmp
)
99 struct esp_output_extra
*extra
= esp_tmp_extra(tmp
);
100 struct crypto_aead
*aead
= x
->data
;
103 struct aead_request
*req
;
104 struct scatterlist
*sg
;
106 if (x
->props
.flags
& XFRM_STATE_ESN
)
107 extralen
+= sizeof(*extra
);
109 extra
= esp_tmp_extra(tmp
);
110 iv
= esp_tmp_iv(aead
, tmp
, extralen
);
111 req
= esp_tmp_req(aead
, iv
);
113 /* Unref skb_frag_pages in the src scatterlist if necessary.
114 * Skip the first sg which comes from skb->data.
116 if (req
->src
!= req
->dst
)
117 for (sg
= sg_next(req
->src
); sg
; sg
= sg_next(sg
))
118 put_page(sg_page(sg
));
121 static void esp_output_done(struct crypto_async_request
*base
, int err
)
123 struct sk_buff
*skb
= base
->data
;
125 struct dst_entry
*dst
= skb_dst(skb
);
126 struct xfrm_state
*x
= dst
->xfrm
;
128 tmp
= ESP_SKB_CB(skb
)->tmp
;
129 esp_ssg_unref(x
, tmp
);
131 xfrm_output_resume(skb
, err
);
134 /* Move ESP header back into place. */
135 static void esp_restore_header(struct sk_buff
*skb
, unsigned int offset
)
137 struct ip_esp_hdr
*esph
= (void *)(skb
->data
+ offset
);
138 void *tmp
= ESP_SKB_CB(skb
)->tmp
;
139 __be32
*seqhi
= esp_tmp_extra(tmp
);
141 esph
->seq_no
= esph
->spi
;
145 static void esp_output_restore_header(struct sk_buff
*skb
)
147 void *tmp
= ESP_SKB_CB(skb
)->tmp
;
148 struct esp_output_extra
*extra
= esp_tmp_extra(tmp
);
150 esp_restore_header(skb
, skb_transport_offset(skb
) + extra
->esphoff
-
154 static struct ip_esp_hdr
*esp_output_set_extra(struct sk_buff
*skb
,
155 struct xfrm_state
*x
,
156 struct ip_esp_hdr
*esph
,
157 struct esp_output_extra
*extra
)
159 /* For ESN we move the header forward by 4 bytes to
160 * accomodate the high bits. We will move it back after
163 if ((x
->props
.flags
& XFRM_STATE_ESN
)) {
165 struct xfrm_offload
*xo
= xfrm_offload(skb
);
170 seqhi
= XFRM_SKB_CB(skb
)->seq
.output
.hi
;
172 extra
->esphoff
= (unsigned char *)esph
-
173 skb_transport_header(skb
);
174 esph
= (struct ip_esp_hdr
*)((unsigned char *)esph
- 4);
175 extra
->seqhi
= esph
->spi
;
176 esph
->seq_no
= htonl(seqhi
);
179 esph
->spi
= x
->id
.spi
;
184 static void esp_output_done_esn(struct crypto_async_request
*base
, int err
)
186 struct sk_buff
*skb
= base
->data
;
188 esp_output_restore_header(skb
);
189 esp_output_done(base
, err
);
192 static void esp_output_fill_trailer(u8
*tail
, int tfclen
, int plen
, __u8 proto
)
194 /* Fill padding... */
196 memset(tail
, 0, tfclen
);
201 for (i
= 0; i
< plen
- 2; i
++)
204 tail
[plen
- 2] = plen
- 2;
205 tail
[plen
- 1] = proto
;
208 static void esp_output_udp_encap(struct xfrm_state
*x
, struct sk_buff
*skb
, struct esp_info
*esp
)
214 struct xfrm_encap_tmpl
*encap
= x
->encap
;
215 struct ip_esp_hdr
*esph
= esp
->esph
;
217 spin_lock_bh(&x
->lock
);
218 sport
= encap
->encap_sport
;
219 dport
= encap
->encap_dport
;
220 encap_type
= encap
->encap_type
;
221 spin_unlock_bh(&x
->lock
);
223 uh
= (struct udphdr
*)esph
;
226 uh
->len
= htons(skb
->len
+ esp
->tailen
227 - skb_transport_offset(skb
));
230 switch (encap_type
) {
232 case UDP_ENCAP_ESPINUDP
:
233 esph
= (struct ip_esp_hdr
*)(uh
+ 1);
235 case UDP_ENCAP_ESPINUDP_NON_IKE
:
236 udpdata32
= (__be32
*)(uh
+ 1);
237 udpdata32
[0] = udpdata32
[1] = 0;
238 esph
= (struct ip_esp_hdr
*)(udpdata32
+ 2);
242 *skb_mac_header(skb
) = IPPROTO_UDP
;
246 int esp_output_head(struct xfrm_state
*x
, struct sk_buff
*skb
, struct esp_info
*esp
)
253 struct sk_buff
*trailer
;
254 int tailen
= esp
->tailen
;
256 /* this is non-NULL only with UDP Encapsulation */
258 esp_output_udp_encap(x
, skb
, esp
);
260 if (!skb_cloned(skb
)) {
261 if (tailen
<= skb_availroom(skb
)) {
264 tail
= skb_tail_pointer(trailer
);
267 } else if ((skb_shinfo(skb
)->nr_frags
< MAX_SKB_FRAGS
)
268 && !skb_has_frag_list(skb
)) {
270 struct sock
*sk
= skb
->sk
;
271 struct page_frag
*pfrag
= &x
->xfrag
;
273 esp
->inplace
= false;
275 allocsize
= ALIGN(tailen
, L1_CACHE_BYTES
);
277 spin_lock_bh(&x
->lock
);
279 if (unlikely(!skb_page_frag_refill(allocsize
, pfrag
, GFP_ATOMIC
))) {
280 spin_unlock_bh(&x
->lock
);
287 vaddr
= kmap_atomic(page
);
289 tail
= vaddr
+ pfrag
->offset
;
291 esp_output_fill_trailer(tail
, esp
->tfclen
, esp
->plen
, esp
->proto
);
293 kunmap_atomic(vaddr
);
295 spin_unlock_bh(&x
->lock
);
297 nfrags
= skb_shinfo(skb
)->nr_frags
;
299 __skb_fill_page_desc(skb
, nfrags
, page
, pfrag
->offset
,
301 skb_shinfo(skb
)->nr_frags
= ++nfrags
;
303 pfrag
->offset
= pfrag
->offset
+ allocsize
;
307 skb
->data_len
+= tailen
;
308 skb
->truesize
+= tailen
;
310 refcount_add(tailen
, &sk
->sk_wmem_alloc
);
317 esph_offset
= (unsigned char *)esp
->esph
- skb_transport_header(skb
);
319 nfrags
= skb_cow_data(skb
, tailen
, &trailer
);
322 tail
= skb_tail_pointer(trailer
);
323 esp
->esph
= (struct ip_esp_hdr
*)(skb_transport_header(skb
) + esph_offset
);
326 esp_output_fill_trailer(tail
, esp
->tfclen
, esp
->plen
, esp
->proto
);
327 pskb_put(skb
, trailer
, tailen
);
332 EXPORT_SYMBOL_GPL(esp_output_head
);
334 int esp_output_tail(struct xfrm_state
*x
, struct sk_buff
*skb
, struct esp_info
*esp
)
343 struct ip_esp_hdr
*esph
;
344 struct crypto_aead
*aead
;
345 struct aead_request
*req
;
346 struct scatterlist
*sg
, *dsg
;
347 struct esp_output_extra
*extra
;
350 assoclen
= sizeof(struct ip_esp_hdr
);
353 if (x
->props
.flags
& XFRM_STATE_ESN
) {
354 extralen
+= sizeof(*extra
);
355 assoclen
+= sizeof(__be32
);
359 alen
= crypto_aead_authsize(aead
);
360 ivlen
= crypto_aead_ivsize(aead
);
362 tmp
= esp_alloc_tmp(aead
, esp
->nfrags
+ 2, extralen
);
366 extra
= esp_tmp_extra(tmp
);
367 iv
= esp_tmp_iv(aead
, tmp
, extralen
);
368 req
= esp_tmp_req(aead
, iv
);
369 sg
= esp_req_sg(aead
, req
);
374 dsg
= &sg
[esp
->nfrags
];
376 esph
= esp_output_set_extra(skb
, x
, esp
->esph
, extra
);
379 sg_init_table(sg
, esp
->nfrags
);
380 err
= skb_to_sgvec(skb
, sg
,
381 (unsigned char *)esph
- skb
->data
,
382 assoclen
+ ivlen
+ esp
->clen
+ alen
);
383 if (unlikely(err
< 0))
388 struct page_frag
*pfrag
= &x
->xfrag
;
390 allocsize
= ALIGN(skb
->data_len
, L1_CACHE_BYTES
);
392 spin_lock_bh(&x
->lock
);
393 if (unlikely(!skb_page_frag_refill(allocsize
, pfrag
, GFP_ATOMIC
))) {
394 spin_unlock_bh(&x
->lock
);
398 skb_shinfo(skb
)->nr_frags
= 1;
402 /* replace page frags in skb with new page */
403 __skb_fill_page_desc(skb
, 0, page
, pfrag
->offset
, skb
->data_len
);
404 pfrag
->offset
= pfrag
->offset
+ allocsize
;
405 spin_unlock_bh(&x
->lock
);
407 sg_init_table(dsg
, skb_shinfo(skb
)->nr_frags
+ 1);
408 err
= skb_to_sgvec(skb
, dsg
,
409 (unsigned char *)esph
- skb
->data
,
410 assoclen
+ ivlen
+ esp
->clen
+ alen
);
411 if (unlikely(err
< 0))
415 if ((x
->props
.flags
& XFRM_STATE_ESN
))
416 aead_request_set_callback(req
, 0, esp_output_done_esn
, skb
);
418 aead_request_set_callback(req
, 0, esp_output_done
, skb
);
420 aead_request_set_crypt(req
, sg
, dsg
, ivlen
+ esp
->clen
, iv
);
421 aead_request_set_ad(req
, assoclen
);
423 memset(iv
, 0, ivlen
);
424 memcpy(iv
+ ivlen
- min(ivlen
, 8), (u8
*)&esp
->seqno
+ 8 - min(ivlen
, 8),
427 ESP_SKB_CB(skb
)->tmp
= tmp
;
428 err
= crypto_aead_encrypt(req
);
439 if ((x
->props
.flags
& XFRM_STATE_ESN
))
440 esp_output_restore_header(skb
);
444 esp_ssg_unref(x
, tmp
);
451 EXPORT_SYMBOL_GPL(esp_output_tail
);
453 static int esp_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
457 struct ip_esp_hdr
*esph
;
458 struct crypto_aead
*aead
;
463 esp
.proto
= *skb_mac_header(skb
);
464 *skb_mac_header(skb
) = IPPROTO_ESP
;
466 /* skb is pure payload to encrypt */
469 alen
= crypto_aead_authsize(aead
);
473 struct xfrm_dst
*dst
= (struct xfrm_dst
*)skb_dst(skb
);
476 padto
= min(x
->tfcpad
, esp4_get_mtu(x
, dst
->child_mtu_cached
));
477 if (skb
->len
< padto
)
478 esp
.tfclen
= padto
- skb
->len
;
480 blksize
= ALIGN(crypto_aead_blocksize(aead
), 4);
481 esp
.clen
= ALIGN(skb
->len
+ 2 + esp
.tfclen
, blksize
);
482 esp
.plen
= esp
.clen
- skb
->len
- esp
.tfclen
;
483 esp
.tailen
= esp
.tfclen
+ esp
.plen
+ alen
;
485 esp
.esph
= ip_esp_hdr(skb
);
487 esp
.nfrags
= esp_output_head(x
, skb
, &esp
);
492 esph
->spi
= x
->id
.spi
;
494 esph
->seq_no
= htonl(XFRM_SKB_CB(skb
)->seq
.output
.low
);
495 esp
.seqno
= cpu_to_be64(XFRM_SKB_CB(skb
)->seq
.output
.low
+
496 ((u64
)XFRM_SKB_CB(skb
)->seq
.output
.hi
<< 32));
498 skb_push(skb
, -skb_network_offset(skb
));
500 return esp_output_tail(x
, skb
, &esp
);
503 int esp_input_done2(struct sk_buff
*skb
, int err
)
505 const struct iphdr
*iph
;
506 struct xfrm_state
*x
= xfrm_input_state(skb
);
507 struct xfrm_offload
*xo
= xfrm_offload(skb
);
508 struct crypto_aead
*aead
= x
->data
;
509 int alen
= crypto_aead_authsize(aead
);
510 int hlen
= sizeof(struct ip_esp_hdr
) + crypto_aead_ivsize(aead
);
511 int elen
= skb
->len
- hlen
;
516 if (!xo
|| (xo
&& !(xo
->flags
& CRYPTO_DONE
)))
517 kfree(ESP_SKB_CB(skb
)->tmp
);
522 if (skb_copy_bits(skb
, skb
->len
-alen
-2, nexthdr
, 2))
527 if (padlen
+ 2 + alen
>= elen
)
530 /* ... check padding bits here. Silly. :-) */
536 struct xfrm_encap_tmpl
*encap
= x
->encap
;
537 struct udphdr
*uh
= (void *)(skb_network_header(skb
) + ihl
);
540 * 1) if the NAT-T peer's IP or port changed then
541 * advertize the change to the keying daemon.
542 * This is an inbound SA, so just compare
545 if (iph
->saddr
!= x
->props
.saddr
.a4
||
546 uh
->source
!= encap
->encap_sport
) {
547 xfrm_address_t ipaddr
;
549 ipaddr
.a4
= iph
->saddr
;
550 km_new_mapping(x
, &ipaddr
, uh
->source
);
552 /* XXX: perhaps add an extra
553 * policy check here, to see
554 * if we should allow or
555 * reject a packet from a
562 * 2) ignore UDP/TCP checksums in case
563 * of NAT-T in Transport Mode, or
564 * perform other post-processing fixes
565 * as per draft-ietf-ipsec-udp-encaps-06,
568 if (x
->props
.mode
== XFRM_MODE_TRANSPORT
)
569 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
572 pskb_trim(skb
, skb
->len
- alen
- padlen
- 2);
573 __skb_pull(skb
, hlen
);
574 if (x
->props
.mode
== XFRM_MODE_TUNNEL
)
575 skb_reset_transport_header(skb
);
577 skb_set_transport_header(skb
, -ihl
);
581 /* RFC4303: Drop dummy packets without any error */
582 if (err
== IPPROTO_NONE
)
588 EXPORT_SYMBOL_GPL(esp_input_done2
);
590 static void esp_input_done(struct crypto_async_request
*base
, int err
)
592 struct sk_buff
*skb
= base
->data
;
594 xfrm_input_resume(skb
, esp_input_done2(skb
, err
));
597 static void esp_input_restore_header(struct sk_buff
*skb
)
599 esp_restore_header(skb
, 0);
603 static void esp_input_set_header(struct sk_buff
*skb
, __be32
*seqhi
)
605 struct xfrm_state
*x
= xfrm_input_state(skb
);
606 struct ip_esp_hdr
*esph
= (struct ip_esp_hdr
*)skb
->data
;
608 /* For ESN we move the header forward by 4 bytes to
609 * accomodate the high bits. We will move it back after
612 if ((x
->props
.flags
& XFRM_STATE_ESN
)) {
613 esph
= skb_push(skb
, 4);
615 esph
->spi
= esph
->seq_no
;
616 esph
->seq_no
= XFRM_SKB_CB(skb
)->seq
.input
.hi
;
620 static void esp_input_done_esn(struct crypto_async_request
*base
, int err
)
622 struct sk_buff
*skb
= base
->data
;
624 esp_input_restore_header(skb
);
625 esp_input_done(base
, err
);
629 * Note: detecting truncated vs. non-truncated authentication data is very
630 * expensive, so we only support truncated data, which is the recommended
633 static int esp_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
635 struct ip_esp_hdr
*esph
;
636 struct crypto_aead
*aead
= x
->data
;
637 struct aead_request
*req
;
638 struct sk_buff
*trailer
;
639 int ivlen
= crypto_aead_ivsize(aead
);
640 int elen
= skb
->len
- sizeof(*esph
) - ivlen
;
647 struct scatterlist
*sg
;
650 if (!pskb_may_pull(skb
, sizeof(*esph
) + ivlen
))
656 assoclen
= sizeof(*esph
);
659 if (x
->props
.flags
& XFRM_STATE_ESN
) {
660 seqhilen
+= sizeof(__be32
);
661 assoclen
+= seqhilen
;
664 if (!skb_cloned(skb
)) {
665 if (!skb_is_nonlinear(skb
)) {
669 } else if (!skb_has_frag_list(skb
)) {
670 nfrags
= skb_shinfo(skb
)->nr_frags
;
677 err
= skb_cow_data(skb
, 0, &trailer
);
685 tmp
= esp_alloc_tmp(aead
, nfrags
, seqhilen
);
689 ESP_SKB_CB(skb
)->tmp
= tmp
;
690 seqhi
= esp_tmp_extra(tmp
);
691 iv
= esp_tmp_iv(aead
, tmp
, seqhilen
);
692 req
= esp_tmp_req(aead
, iv
);
693 sg
= esp_req_sg(aead
, req
);
695 esp_input_set_header(skb
, seqhi
);
697 sg_init_table(sg
, nfrags
);
698 err
= skb_to_sgvec(skb
, sg
, 0, skb
->len
);
699 if (unlikely(err
< 0)) {
704 skb
->ip_summed
= CHECKSUM_NONE
;
706 if ((x
->props
.flags
& XFRM_STATE_ESN
))
707 aead_request_set_callback(req
, 0, esp_input_done_esn
, skb
);
709 aead_request_set_callback(req
, 0, esp_input_done
, skb
);
711 aead_request_set_crypt(req
, sg
, sg
, elen
+ ivlen
, iv
);
712 aead_request_set_ad(req
, assoclen
);
714 err
= crypto_aead_decrypt(req
);
715 if (err
== -EINPROGRESS
)
718 if ((x
->props
.flags
& XFRM_STATE_ESN
))
719 esp_input_restore_header(skb
);
721 err
= esp_input_done2(skb
, err
);
727 static u32
esp4_get_mtu(struct xfrm_state
*x
, int mtu
)
729 struct crypto_aead
*aead
= x
->data
;
730 u32 blksize
= ALIGN(crypto_aead_blocksize(aead
), 4);
731 unsigned int net_adj
;
733 switch (x
->props
.mode
) {
734 case XFRM_MODE_TRANSPORT
:
736 net_adj
= sizeof(struct iphdr
);
738 case XFRM_MODE_TUNNEL
:
745 return ((mtu
- x
->props
.header_len
- crypto_aead_authsize(aead
) -
746 net_adj
) & ~(blksize
- 1)) + net_adj
- 2;
749 static int esp4_err(struct sk_buff
*skb
, u32 info
)
751 struct net
*net
= dev_net(skb
->dev
);
752 const struct iphdr
*iph
= (const struct iphdr
*)skb
->data
;
753 struct ip_esp_hdr
*esph
= (struct ip_esp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
754 struct xfrm_state
*x
;
756 switch (icmp_hdr(skb
)->type
) {
757 case ICMP_DEST_UNREACH
:
758 if (icmp_hdr(skb
)->code
!= ICMP_FRAG_NEEDED
)
766 x
= xfrm_state_lookup(net
, skb
->mark
, (const xfrm_address_t
*)&iph
->daddr
,
767 esph
->spi
, IPPROTO_ESP
, AF_INET
);
771 if (icmp_hdr(skb
)->type
== ICMP_DEST_UNREACH
)
772 ipv4_update_pmtu(skb
, net
, info
, 0, 0, IPPROTO_ESP
, 0);
774 ipv4_redirect(skb
, net
, 0, 0, IPPROTO_ESP
, 0);
780 static void esp_destroy(struct xfrm_state
*x
)
782 struct crypto_aead
*aead
= x
->data
;
787 crypto_free_aead(aead
);
790 static int esp_init_aead(struct xfrm_state
*x
)
792 char aead_name
[CRYPTO_MAX_ALG_NAME
];
793 struct crypto_aead
*aead
;
798 if (snprintf(aead_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)",
799 x
->geniv
, x
->aead
->alg_name
) >= CRYPTO_MAX_ALG_NAME
)
802 if (x
->xso
.offload_handle
)
803 mask
|= CRYPTO_ALG_ASYNC
;
805 aead
= crypto_alloc_aead(aead_name
, 0, mask
);
812 err
= crypto_aead_setkey(aead
, x
->aead
->alg_key
,
813 (x
->aead
->alg_key_len
+ 7) / 8);
817 err
= crypto_aead_setauthsize(aead
, x
->aead
->alg_icv_len
/ 8);
825 static int esp_init_authenc(struct xfrm_state
*x
)
827 struct crypto_aead
*aead
;
828 struct crypto_authenc_key_param
*param
;
832 char authenc_name
[CRYPTO_MAX_ALG_NAME
];
843 if ((x
->props
.flags
& XFRM_STATE_ESN
)) {
844 if (snprintf(authenc_name
, CRYPTO_MAX_ALG_NAME
,
845 "%s%sauthencesn(%s,%s)%s",
846 x
->geniv
?: "", x
->geniv
? "(" : "",
847 x
->aalg
? x
->aalg
->alg_name
: "digest_null",
849 x
->geniv
? ")" : "") >= CRYPTO_MAX_ALG_NAME
)
852 if (snprintf(authenc_name
, CRYPTO_MAX_ALG_NAME
,
853 "%s%sauthenc(%s,%s)%s",
854 x
->geniv
?: "", x
->geniv
? "(" : "",
855 x
->aalg
? x
->aalg
->alg_name
: "digest_null",
857 x
->geniv
? ")" : "") >= CRYPTO_MAX_ALG_NAME
)
861 if (x
->xso
.offload_handle
)
862 mask
|= CRYPTO_ALG_ASYNC
;
864 aead
= crypto_alloc_aead(authenc_name
, 0, mask
);
871 keylen
= (x
->aalg
? (x
->aalg
->alg_key_len
+ 7) / 8 : 0) +
872 (x
->ealg
->alg_key_len
+ 7) / 8 + RTA_SPACE(sizeof(*param
));
874 key
= kmalloc(keylen
, GFP_KERNEL
);
880 rta
->rta_type
= CRYPTO_AUTHENC_KEYA_PARAM
;
881 rta
->rta_len
= RTA_LENGTH(sizeof(*param
));
882 param
= RTA_DATA(rta
);
883 p
+= RTA_SPACE(sizeof(*param
));
886 struct xfrm_algo_desc
*aalg_desc
;
888 memcpy(p
, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+ 7) / 8);
889 p
+= (x
->aalg
->alg_key_len
+ 7) / 8;
891 aalg_desc
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
895 if (aalg_desc
->uinfo
.auth
.icv_fullbits
/ 8 !=
896 crypto_aead_authsize(aead
)) {
897 pr_info("ESP: %s digestsize %u != %hu\n",
899 crypto_aead_authsize(aead
),
900 aalg_desc
->uinfo
.auth
.icv_fullbits
/ 8);
904 err
= crypto_aead_setauthsize(
905 aead
, x
->aalg
->alg_trunc_len
/ 8);
910 param
->enckeylen
= cpu_to_be32((x
->ealg
->alg_key_len
+ 7) / 8);
911 memcpy(p
, x
->ealg
->alg_key
, (x
->ealg
->alg_key_len
+ 7) / 8);
913 err
= crypto_aead_setkey(aead
, key
, keylen
);
922 static int esp_init_state(struct xfrm_state
*x
)
924 struct crypto_aead
*aead
;
931 err
= esp_init_aead(x
);
933 err
= esp_init_authenc(x
);
940 x
->props
.header_len
= sizeof(struct ip_esp_hdr
) +
941 crypto_aead_ivsize(aead
);
942 if (x
->props
.mode
== XFRM_MODE_TUNNEL
)
943 x
->props
.header_len
+= sizeof(struct iphdr
);
944 else if (x
->props
.mode
== XFRM_MODE_BEET
&& x
->sel
.family
!= AF_INET6
)
945 x
->props
.header_len
+= IPV4_BEET_PHMAXLEN
;
947 struct xfrm_encap_tmpl
*encap
= x
->encap
;
949 switch (encap
->encap_type
) {
952 case UDP_ENCAP_ESPINUDP
:
953 x
->props
.header_len
+= sizeof(struct udphdr
);
955 case UDP_ENCAP_ESPINUDP_NON_IKE
:
956 x
->props
.header_len
+= sizeof(struct udphdr
) + 2 * sizeof(u32
);
961 align
= ALIGN(crypto_aead_blocksize(aead
), 4);
962 x
->props
.trailer_len
= align
+ 1 + crypto_aead_authsize(aead
);
968 static int esp4_rcv_cb(struct sk_buff
*skb
, int err
)
973 static const struct xfrm_type esp_type
=
975 .description
= "ESP4",
976 .owner
= THIS_MODULE
,
977 .proto
= IPPROTO_ESP
,
978 .flags
= XFRM_TYPE_REPLAY_PROT
,
979 .init_state
= esp_init_state
,
980 .destructor
= esp_destroy
,
981 .get_mtu
= esp4_get_mtu
,
983 .output
= esp_output
,
986 static struct xfrm4_protocol esp4_protocol
= {
987 .handler
= xfrm4_rcv
,
988 .input_handler
= xfrm_input
,
989 .cb_handler
= esp4_rcv_cb
,
990 .err_handler
= esp4_err
,
994 static int __init
esp4_init(void)
996 if (xfrm_register_type(&esp_type
, AF_INET
) < 0) {
997 pr_info("%s: can't add xfrm type\n", __func__
);
1000 if (xfrm4_protocol_register(&esp4_protocol
, IPPROTO_ESP
) < 0) {
1001 pr_info("%s: can't add protocol\n", __func__
);
1002 xfrm_unregister_type(&esp_type
, AF_INET
);
1008 static void __exit
esp4_fini(void)
1010 if (xfrm4_protocol_deregister(&esp4_protocol
, IPPROTO_ESP
) < 0)
1011 pr_info("%s: can't remove protocol\n", __func__
);
1012 if (xfrm_unregister_type(&esp_type
, AF_INET
) < 0)
1013 pr_info("%s: can't remove xfrm type\n", __func__
);
1016 module_init(esp4_init
);
1017 module_exit(esp4_fini
);
1018 MODULE_LICENSE("GPL");
1019 MODULE_ALIAS_XFRM_TYPE(AF_INET
, XFRM_PROTO_ESP
);