1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) "IPsec: " fmt
4 #include <crypto/aead.h>
5 #include <crypto/authenc.h>
7 #include <linux/module.h>
11 #include <linux/scatterlist.h>
12 #include <linux/kernel.h>
13 #include <linux/pfkeyv2.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/in6.h>
19 #include <net/protocol.h>
22 #include <linux/highmem.h>
25 struct xfrm_skb_cb xfrm
;
29 struct esp_output_extra
{
34 #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
36 static u32
esp4_get_mtu(struct xfrm_state
*x
, int mtu
);
39 * Allocate an AEAD request structure with extra space for SG and IV.
41 * For alignment considerations the IV is placed at the front, followed
42 * by the request and finally the SG list.
44 * TODO: Use spare space in skb for this where possible.
46 static void *esp_alloc_tmp(struct crypto_aead
*aead
, int nfrags
, int extralen
)
52 len
+= crypto_aead_ivsize(aead
);
55 len
+= crypto_aead_alignmask(aead
) &
56 ~(crypto_tfm_ctx_alignment() - 1);
57 len
= ALIGN(len
, crypto_tfm_ctx_alignment());
60 len
+= sizeof(struct aead_request
) + crypto_aead_reqsize(aead
);
61 len
= ALIGN(len
, __alignof__(struct scatterlist
));
63 len
+= sizeof(struct scatterlist
) * nfrags
;
65 return kmalloc(len
, GFP_ATOMIC
);
68 static inline void *esp_tmp_extra(void *tmp
)
70 return PTR_ALIGN(tmp
, __alignof__(struct esp_output_extra
));
73 static inline u8
*esp_tmp_iv(struct crypto_aead
*aead
, void *tmp
, int extralen
)
75 return crypto_aead_ivsize(aead
) ?
76 PTR_ALIGN((u8
*)tmp
+ extralen
,
77 crypto_aead_alignmask(aead
) + 1) : tmp
+ extralen
;
80 static inline struct aead_request
*esp_tmp_req(struct crypto_aead
*aead
, u8
*iv
)
82 struct aead_request
*req
;
84 req
= (void *)PTR_ALIGN(iv
+ crypto_aead_ivsize(aead
),
85 crypto_tfm_ctx_alignment());
86 aead_request_set_tfm(req
, aead
);
90 static inline struct scatterlist
*esp_req_sg(struct crypto_aead
*aead
,
91 struct aead_request
*req
)
93 return (void *)ALIGN((unsigned long)(req
+ 1) +
94 crypto_aead_reqsize(aead
),
95 __alignof__(struct scatterlist
));
98 static void esp_ssg_unref(struct xfrm_state
*x
, void *tmp
)
100 struct esp_output_extra
*extra
= esp_tmp_extra(tmp
);
101 struct crypto_aead
*aead
= x
->data
;
104 struct aead_request
*req
;
105 struct scatterlist
*sg
;
107 if (x
->props
.flags
& XFRM_STATE_ESN
)
108 extralen
+= sizeof(*extra
);
110 extra
= esp_tmp_extra(tmp
);
111 iv
= esp_tmp_iv(aead
, tmp
, extralen
);
112 req
= esp_tmp_req(aead
, iv
);
114 /* Unref skb_frag_pages in the src scatterlist if necessary.
115 * Skip the first sg which comes from skb->data.
117 if (req
->src
!= req
->dst
)
118 for (sg
= sg_next(req
->src
); sg
; sg
= sg_next(sg
))
119 put_page(sg_page(sg
));
122 static void esp_output_done(struct crypto_async_request
*base
, int err
)
124 struct sk_buff
*skb
= base
->data
;
125 struct xfrm_offload
*xo
= xfrm_offload(skb
);
127 struct xfrm_state
*x
;
129 if (xo
&& (xo
->flags
& XFRM_DEV_RESUME
)) {
130 struct sec_path
*sp
= skb_sec_path(skb
);
132 x
= sp
->xvec
[sp
->len
- 1];
134 x
= skb_dst(skb
)->xfrm
;
137 tmp
= ESP_SKB_CB(skb
)->tmp
;
138 esp_ssg_unref(x
, tmp
);
141 if (xo
&& (xo
->flags
& XFRM_DEV_RESUME
)) {
143 XFRM_INC_STATS(xs_net(x
), LINUX_MIB_XFRMOUTSTATEPROTOERROR
);
148 skb_push(skb
, skb
->data
- skb_mac_header(skb
));
150 xfrm_dev_resume(skb
);
152 xfrm_output_resume(skb
, err
);
156 /* Move ESP header back into place. */
157 static void esp_restore_header(struct sk_buff
*skb
, unsigned int offset
)
159 struct ip_esp_hdr
*esph
= (void *)(skb
->data
+ offset
);
160 void *tmp
= ESP_SKB_CB(skb
)->tmp
;
161 __be32
*seqhi
= esp_tmp_extra(tmp
);
163 esph
->seq_no
= esph
->spi
;
167 static void esp_output_restore_header(struct sk_buff
*skb
)
169 void *tmp
= ESP_SKB_CB(skb
)->tmp
;
170 struct esp_output_extra
*extra
= esp_tmp_extra(tmp
);
172 esp_restore_header(skb
, skb_transport_offset(skb
) + extra
->esphoff
-
176 static struct ip_esp_hdr
*esp_output_set_extra(struct sk_buff
*skb
,
177 struct xfrm_state
*x
,
178 struct ip_esp_hdr
*esph
,
179 struct esp_output_extra
*extra
)
181 /* For ESN we move the header forward by 4 bytes to
182 * accomodate the high bits. We will move it back after
185 if ((x
->props
.flags
& XFRM_STATE_ESN
)) {
187 struct xfrm_offload
*xo
= xfrm_offload(skb
);
192 seqhi
= XFRM_SKB_CB(skb
)->seq
.output
.hi
;
194 extra
->esphoff
= (unsigned char *)esph
-
195 skb_transport_header(skb
);
196 esph
= (struct ip_esp_hdr
*)((unsigned char *)esph
- 4);
197 extra
->seqhi
= esph
->spi
;
198 esph
->seq_no
= htonl(seqhi
);
201 esph
->spi
= x
->id
.spi
;
206 static void esp_output_done_esn(struct crypto_async_request
*base
, int err
)
208 struct sk_buff
*skb
= base
->data
;
210 esp_output_restore_header(skb
);
211 esp_output_done(base
, err
);
214 static void esp_output_fill_trailer(u8
*tail
, int tfclen
, int plen
, __u8 proto
)
216 /* Fill padding... */
218 memset(tail
, 0, tfclen
);
223 for (i
= 0; i
< plen
- 2; i
++)
226 tail
[plen
- 2] = plen
- 2;
227 tail
[plen
- 1] = proto
;
230 static int esp_output_udp_encap(struct xfrm_state
*x
, struct sk_buff
*skb
, struct esp_info
*esp
)
236 struct xfrm_encap_tmpl
*encap
= x
->encap
;
237 struct ip_esp_hdr
*esph
= esp
->esph
;
240 spin_lock_bh(&x
->lock
);
241 sport
= encap
->encap_sport
;
242 dport
= encap
->encap_dport
;
243 encap_type
= encap
->encap_type
;
244 spin_unlock_bh(&x
->lock
);
246 len
= skb
->len
+ esp
->tailen
- skb_transport_offset(skb
);
247 if (len
+ sizeof(struct iphdr
) >= IP_MAX_MTU
)
250 uh
= (struct udphdr
*)esph
;
253 uh
->len
= htons(len
);
256 switch (encap_type
) {
258 case UDP_ENCAP_ESPINUDP
:
259 esph
= (struct ip_esp_hdr
*)(uh
+ 1);
261 case UDP_ENCAP_ESPINUDP_NON_IKE
:
262 udpdata32
= (__be32
*)(uh
+ 1);
263 udpdata32
[0] = udpdata32
[1] = 0;
264 esph
= (struct ip_esp_hdr
*)(udpdata32
+ 2);
268 *skb_mac_header(skb
) = IPPROTO_UDP
;
274 int esp_output_head(struct xfrm_state
*x
, struct sk_buff
*skb
, struct esp_info
*esp
)
281 struct sk_buff
*trailer
;
282 int tailen
= esp
->tailen
;
284 /* this is non-NULL only with UDP Encapsulation */
286 int err
= esp_output_udp_encap(x
, skb
, esp
);
292 if (!skb_cloned(skb
)) {
293 if (tailen
<= skb_tailroom(skb
)) {
296 tail
= skb_tail_pointer(trailer
);
299 } else if ((skb_shinfo(skb
)->nr_frags
< MAX_SKB_FRAGS
)
300 && !skb_has_frag_list(skb
)) {
302 struct sock
*sk
= skb
->sk
;
303 struct page_frag
*pfrag
= &x
->xfrag
;
305 esp
->inplace
= false;
307 allocsize
= ALIGN(tailen
, L1_CACHE_BYTES
);
309 spin_lock_bh(&x
->lock
);
311 if (unlikely(!skb_page_frag_refill(allocsize
, pfrag
, GFP_ATOMIC
))) {
312 spin_unlock_bh(&x
->lock
);
319 vaddr
= kmap_atomic(page
);
321 tail
= vaddr
+ pfrag
->offset
;
323 esp_output_fill_trailer(tail
, esp
->tfclen
, esp
->plen
, esp
->proto
);
325 kunmap_atomic(vaddr
);
327 nfrags
= skb_shinfo(skb
)->nr_frags
;
329 __skb_fill_page_desc(skb
, nfrags
, page
, pfrag
->offset
,
331 skb_shinfo(skb
)->nr_frags
= ++nfrags
;
333 pfrag
->offset
= pfrag
->offset
+ allocsize
;
335 spin_unlock_bh(&x
->lock
);
340 skb
->data_len
+= tailen
;
341 skb
->truesize
+= tailen
;
342 if (sk
&& sk_fullsock(sk
))
343 refcount_add(tailen
, &sk
->sk_wmem_alloc
);
350 esph_offset
= (unsigned char *)esp
->esph
- skb_transport_header(skb
);
352 nfrags
= skb_cow_data(skb
, tailen
, &trailer
);
355 tail
= skb_tail_pointer(trailer
);
356 esp
->esph
= (struct ip_esp_hdr
*)(skb_transport_header(skb
) + esph_offset
);
359 esp_output_fill_trailer(tail
, esp
->tfclen
, esp
->plen
, esp
->proto
);
360 pskb_put(skb
, trailer
, tailen
);
365 EXPORT_SYMBOL_GPL(esp_output_head
);
367 int esp_output_tail(struct xfrm_state
*x
, struct sk_buff
*skb
, struct esp_info
*esp
)
376 struct ip_esp_hdr
*esph
;
377 struct crypto_aead
*aead
;
378 struct aead_request
*req
;
379 struct scatterlist
*sg
, *dsg
;
380 struct esp_output_extra
*extra
;
383 assoclen
= sizeof(struct ip_esp_hdr
);
386 if (x
->props
.flags
& XFRM_STATE_ESN
) {
387 extralen
+= sizeof(*extra
);
388 assoclen
+= sizeof(__be32
);
392 alen
= crypto_aead_authsize(aead
);
393 ivlen
= crypto_aead_ivsize(aead
);
395 tmp
= esp_alloc_tmp(aead
, esp
->nfrags
+ 2, extralen
);
399 extra
= esp_tmp_extra(tmp
);
400 iv
= esp_tmp_iv(aead
, tmp
, extralen
);
401 req
= esp_tmp_req(aead
, iv
);
402 sg
= esp_req_sg(aead
, req
);
407 dsg
= &sg
[esp
->nfrags
];
409 esph
= esp_output_set_extra(skb
, x
, esp
->esph
, extra
);
412 sg_init_table(sg
, esp
->nfrags
);
413 err
= skb_to_sgvec(skb
, sg
,
414 (unsigned char *)esph
- skb
->data
,
415 assoclen
+ ivlen
+ esp
->clen
+ alen
);
416 if (unlikely(err
< 0))
421 struct page_frag
*pfrag
= &x
->xfrag
;
423 allocsize
= ALIGN(skb
->data_len
, L1_CACHE_BYTES
);
425 spin_lock_bh(&x
->lock
);
426 if (unlikely(!skb_page_frag_refill(allocsize
, pfrag
, GFP_ATOMIC
))) {
427 spin_unlock_bh(&x
->lock
);
431 skb_shinfo(skb
)->nr_frags
= 1;
435 /* replace page frags in skb with new page */
436 __skb_fill_page_desc(skb
, 0, page
, pfrag
->offset
, skb
->data_len
);
437 pfrag
->offset
= pfrag
->offset
+ allocsize
;
438 spin_unlock_bh(&x
->lock
);
440 sg_init_table(dsg
, skb_shinfo(skb
)->nr_frags
+ 1);
441 err
= skb_to_sgvec(skb
, dsg
,
442 (unsigned char *)esph
- skb
->data
,
443 assoclen
+ ivlen
+ esp
->clen
+ alen
);
444 if (unlikely(err
< 0))
448 if ((x
->props
.flags
& XFRM_STATE_ESN
))
449 aead_request_set_callback(req
, 0, esp_output_done_esn
, skb
);
451 aead_request_set_callback(req
, 0, esp_output_done
, skb
);
453 aead_request_set_crypt(req
, sg
, dsg
, ivlen
+ esp
->clen
, iv
);
454 aead_request_set_ad(req
, assoclen
);
456 memset(iv
, 0, ivlen
);
457 memcpy(iv
+ ivlen
- min(ivlen
, 8), (u8
*)&esp
->seqno
+ 8 - min(ivlen
, 8),
460 ESP_SKB_CB(skb
)->tmp
= tmp
;
461 err
= crypto_aead_encrypt(req
);
472 if ((x
->props
.flags
& XFRM_STATE_ESN
))
473 esp_output_restore_header(skb
);
477 esp_ssg_unref(x
, tmp
);
484 EXPORT_SYMBOL_GPL(esp_output_tail
);
486 static int esp_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
490 struct ip_esp_hdr
*esph
;
491 struct crypto_aead
*aead
;
496 esp
.proto
= *skb_mac_header(skb
);
497 *skb_mac_header(skb
) = IPPROTO_ESP
;
499 /* skb is pure payload to encrypt */
502 alen
= crypto_aead_authsize(aead
);
506 struct xfrm_dst
*dst
= (struct xfrm_dst
*)skb_dst(skb
);
509 padto
= min(x
->tfcpad
, esp4_get_mtu(x
, dst
->child_mtu_cached
));
510 if (skb
->len
< padto
)
511 esp
.tfclen
= padto
- skb
->len
;
513 blksize
= ALIGN(crypto_aead_blocksize(aead
), 4);
514 esp
.clen
= ALIGN(skb
->len
+ 2 + esp
.tfclen
, blksize
);
515 esp
.plen
= esp
.clen
- skb
->len
- esp
.tfclen
;
516 esp
.tailen
= esp
.tfclen
+ esp
.plen
+ alen
;
518 esp
.esph
= ip_esp_hdr(skb
);
520 esp
.nfrags
= esp_output_head(x
, skb
, &esp
);
525 esph
->spi
= x
->id
.spi
;
527 esph
->seq_no
= htonl(XFRM_SKB_CB(skb
)->seq
.output
.low
);
528 esp
.seqno
= cpu_to_be64(XFRM_SKB_CB(skb
)->seq
.output
.low
+
529 ((u64
)XFRM_SKB_CB(skb
)->seq
.output
.hi
<< 32));
531 skb_push(skb
, -skb_network_offset(skb
));
533 return esp_output_tail(x
, skb
, &esp
);
536 static inline int esp_remove_trailer(struct sk_buff
*skb
)
538 struct xfrm_state
*x
= xfrm_input_state(skb
);
539 struct xfrm_offload
*xo
= xfrm_offload(skb
);
540 struct crypto_aead
*aead
= x
->data
;
541 int alen
, hlen
, elen
;
547 alen
= crypto_aead_authsize(aead
);
548 hlen
= sizeof(struct ip_esp_hdr
) + crypto_aead_ivsize(aead
);
549 elen
= skb
->len
- hlen
;
551 if (xo
&& (xo
->flags
& XFRM_ESP_NO_TRAILER
)) {
556 if (skb_copy_bits(skb
, skb
->len
- alen
- 2, nexthdr
, 2))
561 if (padlen
+ 2 + alen
>= elen
) {
562 net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
563 padlen
+ 2, elen
- alen
);
567 trimlen
= alen
+ padlen
+ 2;
568 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
569 csumdiff
= skb_checksum(skb
, skb
->len
- trimlen
, trimlen
, 0);
570 skb
->csum
= csum_block_sub(skb
->csum
, csumdiff
,
573 pskb_trim(skb
, skb
->len
- trimlen
);
581 int esp_input_done2(struct sk_buff
*skb
, int err
)
583 const struct iphdr
*iph
;
584 struct xfrm_state
*x
= xfrm_input_state(skb
);
585 struct xfrm_offload
*xo
= xfrm_offload(skb
);
586 struct crypto_aead
*aead
= x
->data
;
587 int hlen
= sizeof(struct ip_esp_hdr
) + crypto_aead_ivsize(aead
);
590 if (!xo
|| (xo
&& !(xo
->flags
& CRYPTO_DONE
)))
591 kfree(ESP_SKB_CB(skb
)->tmp
);
596 err
= esp_remove_trailer(skb
);
597 if (unlikely(err
< 0))
604 struct xfrm_encap_tmpl
*encap
= x
->encap
;
605 struct udphdr
*uh
= (void *)(skb_network_header(skb
) + ihl
);
608 * 1) if the NAT-T peer's IP or port changed then
609 * advertize the change to the keying daemon.
610 * This is an inbound SA, so just compare
613 if (iph
->saddr
!= x
->props
.saddr
.a4
||
614 uh
->source
!= encap
->encap_sport
) {
615 xfrm_address_t ipaddr
;
617 ipaddr
.a4
= iph
->saddr
;
618 km_new_mapping(x
, &ipaddr
, uh
->source
);
620 /* XXX: perhaps add an extra
621 * policy check here, to see
622 * if we should allow or
623 * reject a packet from a
630 * 2) ignore UDP/TCP checksums in case
631 * of NAT-T in Transport Mode, or
632 * perform other post-processing fixes
633 * as per draft-ietf-ipsec-udp-encaps-06,
636 if (x
->props
.mode
== XFRM_MODE_TRANSPORT
)
637 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
640 skb_pull_rcsum(skb
, hlen
);
641 if (x
->props
.mode
== XFRM_MODE_TUNNEL
)
642 skb_reset_transport_header(skb
);
644 skb_set_transport_header(skb
, -ihl
);
646 /* RFC4303: Drop dummy packets without any error */
647 if (err
== IPPROTO_NONE
)
653 EXPORT_SYMBOL_GPL(esp_input_done2
);
655 static void esp_input_done(struct crypto_async_request
*base
, int err
)
657 struct sk_buff
*skb
= base
->data
;
659 xfrm_input_resume(skb
, esp_input_done2(skb
, err
));
662 static void esp_input_restore_header(struct sk_buff
*skb
)
664 esp_restore_header(skb
, 0);
668 static void esp_input_set_header(struct sk_buff
*skb
, __be32
*seqhi
)
670 struct xfrm_state
*x
= xfrm_input_state(skb
);
671 struct ip_esp_hdr
*esph
;
673 /* For ESN we move the header forward by 4 bytes to
674 * accomodate the high bits. We will move it back after
677 if ((x
->props
.flags
& XFRM_STATE_ESN
)) {
678 esph
= skb_push(skb
, 4);
680 esph
->spi
= esph
->seq_no
;
681 esph
->seq_no
= XFRM_SKB_CB(skb
)->seq
.input
.hi
;
685 static void esp_input_done_esn(struct crypto_async_request
*base
, int err
)
687 struct sk_buff
*skb
= base
->data
;
689 esp_input_restore_header(skb
);
690 esp_input_done(base
, err
);
694 * Note: detecting truncated vs. non-truncated authentication data is very
695 * expensive, so we only support truncated data, which is the recommended
698 static int esp_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
700 struct crypto_aead
*aead
= x
->data
;
701 struct aead_request
*req
;
702 struct sk_buff
*trailer
;
703 int ivlen
= crypto_aead_ivsize(aead
);
704 int elen
= skb
->len
- sizeof(struct ip_esp_hdr
) - ivlen
;
711 struct scatterlist
*sg
;
714 if (!pskb_may_pull(skb
, sizeof(struct ip_esp_hdr
) + ivlen
))
720 assoclen
= sizeof(struct ip_esp_hdr
);
723 if (x
->props
.flags
& XFRM_STATE_ESN
) {
724 seqhilen
+= sizeof(__be32
);
725 assoclen
+= seqhilen
;
728 if (!skb_cloned(skb
)) {
729 if (!skb_is_nonlinear(skb
)) {
733 } else if (!skb_has_frag_list(skb
)) {
734 nfrags
= skb_shinfo(skb
)->nr_frags
;
741 err
= skb_cow_data(skb
, 0, &trailer
);
749 tmp
= esp_alloc_tmp(aead
, nfrags
, seqhilen
);
753 ESP_SKB_CB(skb
)->tmp
= tmp
;
754 seqhi
= esp_tmp_extra(tmp
);
755 iv
= esp_tmp_iv(aead
, tmp
, seqhilen
);
756 req
= esp_tmp_req(aead
, iv
);
757 sg
= esp_req_sg(aead
, req
);
759 esp_input_set_header(skb
, seqhi
);
761 sg_init_table(sg
, nfrags
);
762 err
= skb_to_sgvec(skb
, sg
, 0, skb
->len
);
763 if (unlikely(err
< 0)) {
768 skb
->ip_summed
= CHECKSUM_NONE
;
770 if ((x
->props
.flags
& XFRM_STATE_ESN
))
771 aead_request_set_callback(req
, 0, esp_input_done_esn
, skb
);
773 aead_request_set_callback(req
, 0, esp_input_done
, skb
);
775 aead_request_set_crypt(req
, sg
, sg
, elen
+ ivlen
, iv
);
776 aead_request_set_ad(req
, assoclen
);
778 err
= crypto_aead_decrypt(req
);
779 if (err
== -EINPROGRESS
)
782 if ((x
->props
.flags
& XFRM_STATE_ESN
))
783 esp_input_restore_header(skb
);
785 err
= esp_input_done2(skb
, err
);
791 static u32
esp4_get_mtu(struct xfrm_state
*x
, int mtu
)
793 struct crypto_aead
*aead
= x
->data
;
794 u32 blksize
= ALIGN(crypto_aead_blocksize(aead
), 4);
795 unsigned int net_adj
;
797 switch (x
->props
.mode
) {
798 case XFRM_MODE_TRANSPORT
:
800 net_adj
= sizeof(struct iphdr
);
802 case XFRM_MODE_TUNNEL
:
809 return ((mtu
- x
->props
.header_len
- crypto_aead_authsize(aead
) -
810 net_adj
) & ~(blksize
- 1)) + net_adj
- 2;
813 static int esp4_err(struct sk_buff
*skb
, u32 info
)
815 struct net
*net
= dev_net(skb
->dev
);
816 const struct iphdr
*iph
= (const struct iphdr
*)skb
->data
;
817 struct ip_esp_hdr
*esph
= (struct ip_esp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
818 struct xfrm_state
*x
;
820 switch (icmp_hdr(skb
)->type
) {
821 case ICMP_DEST_UNREACH
:
822 if (icmp_hdr(skb
)->code
!= ICMP_FRAG_NEEDED
)
830 x
= xfrm_state_lookup(net
, skb
->mark
, (const xfrm_address_t
*)&iph
->daddr
,
831 esph
->spi
, IPPROTO_ESP
, AF_INET
);
835 if (icmp_hdr(skb
)->type
== ICMP_DEST_UNREACH
)
836 ipv4_update_pmtu(skb
, net
, info
, 0, IPPROTO_ESP
);
838 ipv4_redirect(skb
, net
, 0, IPPROTO_ESP
);
844 static void esp_destroy(struct xfrm_state
*x
)
846 struct crypto_aead
*aead
= x
->data
;
851 crypto_free_aead(aead
);
854 static int esp_init_aead(struct xfrm_state
*x
)
856 char aead_name
[CRYPTO_MAX_ALG_NAME
];
857 struct crypto_aead
*aead
;
861 if (snprintf(aead_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)",
862 x
->geniv
, x
->aead
->alg_name
) >= CRYPTO_MAX_ALG_NAME
)
865 aead
= crypto_alloc_aead(aead_name
, 0, 0);
872 err
= crypto_aead_setkey(aead
, x
->aead
->alg_key
,
873 (x
->aead
->alg_key_len
+ 7) / 8);
877 err
= crypto_aead_setauthsize(aead
, x
->aead
->alg_icv_len
/ 8);
885 static int esp_init_authenc(struct xfrm_state
*x
)
887 struct crypto_aead
*aead
;
888 struct crypto_authenc_key_param
*param
;
892 char authenc_name
[CRYPTO_MAX_ALG_NAME
];
902 if ((x
->props
.flags
& XFRM_STATE_ESN
)) {
903 if (snprintf(authenc_name
, CRYPTO_MAX_ALG_NAME
,
904 "%s%sauthencesn(%s,%s)%s",
905 x
->geniv
?: "", x
->geniv
? "(" : "",
906 x
->aalg
? x
->aalg
->alg_name
: "digest_null",
908 x
->geniv
? ")" : "") >= CRYPTO_MAX_ALG_NAME
)
911 if (snprintf(authenc_name
, CRYPTO_MAX_ALG_NAME
,
912 "%s%sauthenc(%s,%s)%s",
913 x
->geniv
?: "", x
->geniv
? "(" : "",
914 x
->aalg
? x
->aalg
->alg_name
: "digest_null",
916 x
->geniv
? ")" : "") >= CRYPTO_MAX_ALG_NAME
)
920 aead
= crypto_alloc_aead(authenc_name
, 0, 0);
927 keylen
= (x
->aalg
? (x
->aalg
->alg_key_len
+ 7) / 8 : 0) +
928 (x
->ealg
->alg_key_len
+ 7) / 8 + RTA_SPACE(sizeof(*param
));
930 key
= kmalloc(keylen
, GFP_KERNEL
);
936 rta
->rta_type
= CRYPTO_AUTHENC_KEYA_PARAM
;
937 rta
->rta_len
= RTA_LENGTH(sizeof(*param
));
938 param
= RTA_DATA(rta
);
939 p
+= RTA_SPACE(sizeof(*param
));
942 struct xfrm_algo_desc
*aalg_desc
;
944 memcpy(p
, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+ 7) / 8);
945 p
+= (x
->aalg
->alg_key_len
+ 7) / 8;
947 aalg_desc
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
951 if (aalg_desc
->uinfo
.auth
.icv_fullbits
/ 8 !=
952 crypto_aead_authsize(aead
)) {
953 pr_info("ESP: %s digestsize %u != %hu\n",
955 crypto_aead_authsize(aead
),
956 aalg_desc
->uinfo
.auth
.icv_fullbits
/ 8);
960 err
= crypto_aead_setauthsize(
961 aead
, x
->aalg
->alg_trunc_len
/ 8);
966 param
->enckeylen
= cpu_to_be32((x
->ealg
->alg_key_len
+ 7) / 8);
967 memcpy(p
, x
->ealg
->alg_key
, (x
->ealg
->alg_key_len
+ 7) / 8);
969 err
= crypto_aead_setkey(aead
, key
, keylen
);
978 static int esp_init_state(struct xfrm_state
*x
)
980 struct crypto_aead
*aead
;
987 err
= esp_init_aead(x
);
989 err
= esp_init_authenc(x
);
996 x
->props
.header_len
= sizeof(struct ip_esp_hdr
) +
997 crypto_aead_ivsize(aead
);
998 if (x
->props
.mode
== XFRM_MODE_TUNNEL
)
999 x
->props
.header_len
+= sizeof(struct iphdr
);
1000 else if (x
->props
.mode
== XFRM_MODE_BEET
&& x
->sel
.family
!= AF_INET6
)
1001 x
->props
.header_len
+= IPV4_BEET_PHMAXLEN
;
1003 struct xfrm_encap_tmpl
*encap
= x
->encap
;
1005 switch (encap
->encap_type
) {
1009 case UDP_ENCAP_ESPINUDP
:
1010 x
->props
.header_len
+= sizeof(struct udphdr
);
1012 case UDP_ENCAP_ESPINUDP_NON_IKE
:
1013 x
->props
.header_len
+= sizeof(struct udphdr
) + 2 * sizeof(u32
);
1018 align
= ALIGN(crypto_aead_blocksize(aead
), 4);
1019 x
->props
.trailer_len
= align
+ 1 + crypto_aead_authsize(aead
);
1025 static int esp4_rcv_cb(struct sk_buff
*skb
, int err
)
1030 static const struct xfrm_type esp_type
=
1032 .description
= "ESP4",
1033 .owner
= THIS_MODULE
,
1034 .proto
= IPPROTO_ESP
,
1035 .flags
= XFRM_TYPE_REPLAY_PROT
,
1036 .init_state
= esp_init_state
,
1037 .destructor
= esp_destroy
,
1038 .get_mtu
= esp4_get_mtu
,
1040 .output
= esp_output
,
1043 static struct xfrm4_protocol esp4_protocol
= {
1044 .handler
= xfrm4_rcv
,
1045 .input_handler
= xfrm_input
,
1046 .cb_handler
= esp4_rcv_cb
,
1047 .err_handler
= esp4_err
,
1051 static int __init
esp4_init(void)
1053 if (xfrm_register_type(&esp_type
, AF_INET
) < 0) {
1054 pr_info("%s: can't add xfrm type\n", __func__
);
1057 if (xfrm4_protocol_register(&esp4_protocol
, IPPROTO_ESP
) < 0) {
1058 pr_info("%s: can't add protocol\n", __func__
);
1059 xfrm_unregister_type(&esp_type
, AF_INET
);
1065 static void __exit
esp4_fini(void)
1067 if (xfrm4_protocol_deregister(&esp4_protocol
, IPPROTO_ESP
) < 0)
1068 pr_info("%s: can't remove protocol\n", __func__
);
1069 if (xfrm_unregister_type(&esp_type
, AF_INET
) < 0)
1070 pr_info("%s: can't remove xfrm type\n", __func__
);
1073 module_init(esp4_init
);
1074 module_exit(esp4_fini
);
1075 MODULE_LICENSE("GPL");
1076 MODULE_ALIAS_XFRM_TYPE(AF_INET
, XFRM_PROTO_ESP
);