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
)
252 struct sk_buff
*trailer
;
253 int tailen
= esp
->tailen
;
255 /* this is non-NULL only with UDP Encapsulation */
257 esp_output_udp_encap(x
, skb
, esp
);
259 if (!skb_cloned(skb
)) {
260 if (tailen
<= skb_availroom(skb
)) {
263 tail
= skb_tail_pointer(trailer
);
266 } else if ((skb_shinfo(skb
)->nr_frags
< MAX_SKB_FRAGS
)
267 && !skb_has_frag_list(skb
)) {
269 struct sock
*sk
= skb
->sk
;
270 struct page_frag
*pfrag
= &x
->xfrag
;
272 esp
->inplace
= false;
274 allocsize
= ALIGN(tailen
, L1_CACHE_BYTES
);
276 spin_lock_bh(&x
->lock
);
278 if (unlikely(!skb_page_frag_refill(allocsize
, pfrag
, GFP_ATOMIC
))) {
279 spin_unlock_bh(&x
->lock
);
286 vaddr
= kmap_atomic(page
);
288 tail
= vaddr
+ pfrag
->offset
;
290 esp_output_fill_trailer(tail
, esp
->tfclen
, esp
->plen
, esp
->proto
);
292 kunmap_atomic(vaddr
);
294 spin_unlock_bh(&x
->lock
);
296 nfrags
= skb_shinfo(skb
)->nr_frags
;
298 __skb_fill_page_desc(skb
, nfrags
, page
, pfrag
->offset
,
300 skb_shinfo(skb
)->nr_frags
= ++nfrags
;
302 pfrag
->offset
= pfrag
->offset
+ allocsize
;
306 skb
->data_len
+= tailen
;
307 skb
->truesize
+= tailen
;
309 atomic_add(tailen
, &sk
->sk_wmem_alloc
);
316 nfrags
= skb_cow_data(skb
, tailen
, &trailer
);
319 tail
= skb_tail_pointer(trailer
);
320 esp
->esph
= ip_esp_hdr(skb
);
323 esp_output_fill_trailer(tail
, esp
->tfclen
, esp
->plen
, esp
->proto
);
324 pskb_put(skb
, trailer
, tailen
);
329 EXPORT_SYMBOL_GPL(esp_output_head
);
331 int esp_output_tail(struct xfrm_state
*x
, struct sk_buff
*skb
, struct esp_info
*esp
)
340 struct ip_esp_hdr
*esph
;
341 struct crypto_aead
*aead
;
342 struct aead_request
*req
;
343 struct scatterlist
*sg
, *dsg
;
344 struct esp_output_extra
*extra
;
347 assoclen
= sizeof(struct ip_esp_hdr
);
350 if (x
->props
.flags
& XFRM_STATE_ESN
) {
351 extralen
+= sizeof(*extra
);
352 assoclen
+= sizeof(__be32
);
356 alen
= crypto_aead_authsize(aead
);
357 ivlen
= crypto_aead_ivsize(aead
);
359 tmp
= esp_alloc_tmp(aead
, esp
->nfrags
+ 2, extralen
);
363 extra
= esp_tmp_extra(tmp
);
364 iv
= esp_tmp_iv(aead
, tmp
, extralen
);
365 req
= esp_tmp_req(aead
, iv
);
366 sg
= esp_req_sg(aead
, req
);
371 dsg
= &sg
[esp
->nfrags
];
373 esph
= esp_output_set_extra(skb
, x
, esp
->esph
, extra
);
376 sg_init_table(sg
, esp
->nfrags
);
377 skb_to_sgvec(skb
, sg
,
378 (unsigned char *)esph
- skb
->data
,
379 assoclen
+ ivlen
+ esp
->clen
+ alen
);
383 struct page_frag
*pfrag
= &x
->xfrag
;
385 allocsize
= ALIGN(skb
->data_len
, L1_CACHE_BYTES
);
387 spin_lock_bh(&x
->lock
);
388 if (unlikely(!skb_page_frag_refill(allocsize
, pfrag
, GFP_ATOMIC
))) {
389 spin_unlock_bh(&x
->lock
);
393 skb_shinfo(skb
)->nr_frags
= 1;
397 /* replace page frags in skb with new page */
398 __skb_fill_page_desc(skb
, 0, page
, pfrag
->offset
, skb
->data_len
);
399 pfrag
->offset
= pfrag
->offset
+ allocsize
;
400 spin_unlock_bh(&x
->lock
);
402 sg_init_table(dsg
, skb_shinfo(skb
)->nr_frags
+ 1);
403 skb_to_sgvec(skb
, dsg
,
404 (unsigned char *)esph
- skb
->data
,
405 assoclen
+ ivlen
+ esp
->clen
+ alen
);
408 if ((x
->props
.flags
& XFRM_STATE_ESN
))
409 aead_request_set_callback(req
, 0, esp_output_done_esn
, skb
);
411 aead_request_set_callback(req
, 0, esp_output_done
, skb
);
413 aead_request_set_crypt(req
, sg
, dsg
, ivlen
+ esp
->clen
, iv
);
414 aead_request_set_ad(req
, assoclen
);
416 memset(iv
, 0, ivlen
);
417 memcpy(iv
+ ivlen
- min(ivlen
, 8), (u8
*)&esp
->seqno
+ 8 - min(ivlen
, 8),
420 ESP_SKB_CB(skb
)->tmp
= tmp
;
421 err
= crypto_aead_encrypt(req
);
432 if ((x
->props
.flags
& XFRM_STATE_ESN
))
433 esp_output_restore_header(skb
);
437 esp_ssg_unref(x
, tmp
);
443 EXPORT_SYMBOL_GPL(esp_output_tail
);
445 static int esp_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
449 struct ip_esp_hdr
*esph
;
450 struct crypto_aead
*aead
;
455 esp
.proto
= *skb_mac_header(skb
);
456 *skb_mac_header(skb
) = IPPROTO_ESP
;
458 /* skb is pure payload to encrypt */
461 alen
= crypto_aead_authsize(aead
);
465 struct xfrm_dst
*dst
= (struct xfrm_dst
*)skb_dst(skb
);
468 padto
= min(x
->tfcpad
, esp4_get_mtu(x
, dst
->child_mtu_cached
));
469 if (skb
->len
< padto
)
470 esp
.tfclen
= padto
- skb
->len
;
472 blksize
= ALIGN(crypto_aead_blocksize(aead
), 4);
473 esp
.clen
= ALIGN(skb
->len
+ 2 + esp
.tfclen
, blksize
);
474 esp
.plen
= esp
.clen
- skb
->len
- esp
.tfclen
;
475 esp
.tailen
= esp
.tfclen
+ esp
.plen
+ alen
;
477 esp
.esph
= ip_esp_hdr(skb
);
479 esp
.nfrags
= esp_output_head(x
, skb
, &esp
);
484 esph
->spi
= x
->id
.spi
;
486 esph
->seq_no
= htonl(XFRM_SKB_CB(skb
)->seq
.output
.low
);
487 esp
.seqno
= cpu_to_be64(XFRM_SKB_CB(skb
)->seq
.output
.low
+
488 ((u64
)XFRM_SKB_CB(skb
)->seq
.output
.hi
<< 32));
490 skb_push(skb
, -skb_network_offset(skb
));
492 return esp_output_tail(x
, skb
, &esp
);
495 int esp_input_done2(struct sk_buff
*skb
, int err
)
497 const struct iphdr
*iph
;
498 struct xfrm_state
*x
= xfrm_input_state(skb
);
499 struct xfrm_offload
*xo
= xfrm_offload(skb
);
500 struct crypto_aead
*aead
= x
->data
;
501 int alen
= crypto_aead_authsize(aead
);
502 int hlen
= sizeof(struct ip_esp_hdr
) + crypto_aead_ivsize(aead
);
503 int elen
= skb
->len
- hlen
;
508 if (!xo
|| (xo
&& !(xo
->flags
& CRYPTO_DONE
)))
509 kfree(ESP_SKB_CB(skb
)->tmp
);
514 if (skb_copy_bits(skb
, skb
->len
-alen
-2, nexthdr
, 2))
519 if (padlen
+ 2 + alen
>= elen
)
522 /* ... check padding bits here. Silly. :-) */
528 struct xfrm_encap_tmpl
*encap
= x
->encap
;
529 struct udphdr
*uh
= (void *)(skb_network_header(skb
) + ihl
);
532 * 1) if the NAT-T peer's IP or port changed then
533 * advertize the change to the keying daemon.
534 * This is an inbound SA, so just compare
537 if (iph
->saddr
!= x
->props
.saddr
.a4
||
538 uh
->source
!= encap
->encap_sport
) {
539 xfrm_address_t ipaddr
;
541 ipaddr
.a4
= iph
->saddr
;
542 km_new_mapping(x
, &ipaddr
, uh
->source
);
544 /* XXX: perhaps add an extra
545 * policy check here, to see
546 * if we should allow or
547 * reject a packet from a
554 * 2) ignore UDP/TCP checksums in case
555 * of NAT-T in Transport Mode, or
556 * perform other post-processing fixes
557 * as per draft-ietf-ipsec-udp-encaps-06,
560 if (x
->props
.mode
== XFRM_MODE_TRANSPORT
)
561 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
564 pskb_trim(skb
, skb
->len
- alen
- padlen
- 2);
565 __skb_pull(skb
, hlen
);
566 if (x
->props
.mode
== XFRM_MODE_TUNNEL
)
567 skb_reset_transport_header(skb
);
569 skb_set_transport_header(skb
, -ihl
);
573 /* RFC4303: Drop dummy packets without any error */
574 if (err
== IPPROTO_NONE
)
580 EXPORT_SYMBOL_GPL(esp_input_done2
);
582 static void esp_input_done(struct crypto_async_request
*base
, int err
)
584 struct sk_buff
*skb
= base
->data
;
586 xfrm_input_resume(skb
, esp_input_done2(skb
, err
));
589 static void esp_input_restore_header(struct sk_buff
*skb
)
591 esp_restore_header(skb
, 0);
595 static void esp_input_set_header(struct sk_buff
*skb
, __be32
*seqhi
)
597 struct xfrm_state
*x
= xfrm_input_state(skb
);
598 struct ip_esp_hdr
*esph
= (struct ip_esp_hdr
*)skb
->data
;
600 /* For ESN we move the header forward by 4 bytes to
601 * accomodate the high bits. We will move it back after
604 if ((x
->props
.flags
& XFRM_STATE_ESN
)) {
605 esph
= (void *)skb_push(skb
, 4);
607 esph
->spi
= esph
->seq_no
;
608 esph
->seq_no
= XFRM_SKB_CB(skb
)->seq
.input
.hi
;
612 static void esp_input_done_esn(struct crypto_async_request
*base
, int err
)
614 struct sk_buff
*skb
= base
->data
;
616 esp_input_restore_header(skb
);
617 esp_input_done(base
, err
);
621 * Note: detecting truncated vs. non-truncated authentication data is very
622 * expensive, so we only support truncated data, which is the recommended
625 static int esp_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
627 struct ip_esp_hdr
*esph
;
628 struct crypto_aead
*aead
= x
->data
;
629 struct aead_request
*req
;
630 struct sk_buff
*trailer
;
631 int ivlen
= crypto_aead_ivsize(aead
);
632 int elen
= skb
->len
- sizeof(*esph
) - ivlen
;
639 struct scatterlist
*sg
;
642 if (!pskb_may_pull(skb
, sizeof(*esph
) + ivlen
))
648 assoclen
= sizeof(*esph
);
651 if (x
->props
.flags
& XFRM_STATE_ESN
) {
652 seqhilen
+= sizeof(__be32
);
653 assoclen
+= seqhilen
;
656 if (!skb_cloned(skb
)) {
657 if (!skb_is_nonlinear(skb
)) {
661 } else if (!skb_has_frag_list(skb
)) {
662 nfrags
= skb_shinfo(skb
)->nr_frags
;
669 err
= skb_cow_data(skb
, 0, &trailer
);
677 tmp
= esp_alloc_tmp(aead
, nfrags
, seqhilen
);
681 ESP_SKB_CB(skb
)->tmp
= tmp
;
682 seqhi
= esp_tmp_extra(tmp
);
683 iv
= esp_tmp_iv(aead
, tmp
, seqhilen
);
684 req
= esp_tmp_req(aead
, iv
);
685 sg
= esp_req_sg(aead
, req
);
687 esp_input_set_header(skb
, seqhi
);
689 sg_init_table(sg
, nfrags
);
690 skb_to_sgvec(skb
, sg
, 0, skb
->len
);
692 skb
->ip_summed
= CHECKSUM_NONE
;
694 if ((x
->props
.flags
& XFRM_STATE_ESN
))
695 aead_request_set_callback(req
, 0, esp_input_done_esn
, skb
);
697 aead_request_set_callback(req
, 0, esp_input_done
, skb
);
699 aead_request_set_crypt(req
, sg
, sg
, elen
+ ivlen
, iv
);
700 aead_request_set_ad(req
, assoclen
);
702 err
= crypto_aead_decrypt(req
);
703 if (err
== -EINPROGRESS
)
706 if ((x
->props
.flags
& XFRM_STATE_ESN
))
707 esp_input_restore_header(skb
);
709 err
= esp_input_done2(skb
, err
);
715 static u32
esp4_get_mtu(struct xfrm_state
*x
, int mtu
)
717 struct crypto_aead
*aead
= x
->data
;
718 u32 blksize
= ALIGN(crypto_aead_blocksize(aead
), 4);
719 unsigned int net_adj
;
721 switch (x
->props
.mode
) {
722 case XFRM_MODE_TRANSPORT
:
724 net_adj
= sizeof(struct iphdr
);
726 case XFRM_MODE_TUNNEL
:
733 return ((mtu
- x
->props
.header_len
- crypto_aead_authsize(aead
) -
734 net_adj
) & ~(blksize
- 1)) + net_adj
- 2;
737 static int esp4_err(struct sk_buff
*skb
, u32 info
)
739 struct net
*net
= dev_net(skb
->dev
);
740 const struct iphdr
*iph
= (const struct iphdr
*)skb
->data
;
741 struct ip_esp_hdr
*esph
= (struct ip_esp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
742 struct xfrm_state
*x
;
744 switch (icmp_hdr(skb
)->type
) {
745 case ICMP_DEST_UNREACH
:
746 if (icmp_hdr(skb
)->code
!= ICMP_FRAG_NEEDED
)
754 x
= xfrm_state_lookup(net
, skb
->mark
, (const xfrm_address_t
*)&iph
->daddr
,
755 esph
->spi
, IPPROTO_ESP
, AF_INET
);
759 if (icmp_hdr(skb
)->type
== ICMP_DEST_UNREACH
)
760 ipv4_update_pmtu(skb
, net
, info
, 0, 0, IPPROTO_ESP
, 0);
762 ipv4_redirect(skb
, net
, 0, 0, IPPROTO_ESP
, 0);
768 static void esp_destroy(struct xfrm_state
*x
)
770 struct crypto_aead
*aead
= x
->data
;
775 crypto_free_aead(aead
);
778 static int esp_init_aead(struct xfrm_state
*x
)
780 char aead_name
[CRYPTO_MAX_ALG_NAME
];
781 struct crypto_aead
*aead
;
786 if (snprintf(aead_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)",
787 x
->geniv
, x
->aead
->alg_name
) >= CRYPTO_MAX_ALG_NAME
)
790 if (x
->xso
.offload_handle
)
791 mask
|= CRYPTO_ALG_ASYNC
;
793 aead
= crypto_alloc_aead(aead_name
, 0, mask
);
800 err
= crypto_aead_setkey(aead
, x
->aead
->alg_key
,
801 (x
->aead
->alg_key_len
+ 7) / 8);
805 err
= crypto_aead_setauthsize(aead
, x
->aead
->alg_icv_len
/ 8);
813 static int esp_init_authenc(struct xfrm_state
*x
)
815 struct crypto_aead
*aead
;
816 struct crypto_authenc_key_param
*param
;
820 char authenc_name
[CRYPTO_MAX_ALG_NAME
];
831 if ((x
->props
.flags
& XFRM_STATE_ESN
)) {
832 if (snprintf(authenc_name
, CRYPTO_MAX_ALG_NAME
,
833 "%s%sauthencesn(%s,%s)%s",
834 x
->geniv
?: "", x
->geniv
? "(" : "",
835 x
->aalg
? x
->aalg
->alg_name
: "digest_null",
837 x
->geniv
? ")" : "") >= CRYPTO_MAX_ALG_NAME
)
840 if (snprintf(authenc_name
, CRYPTO_MAX_ALG_NAME
,
841 "%s%sauthenc(%s,%s)%s",
842 x
->geniv
?: "", x
->geniv
? "(" : "",
843 x
->aalg
? x
->aalg
->alg_name
: "digest_null",
845 x
->geniv
? ")" : "") >= CRYPTO_MAX_ALG_NAME
)
849 if (x
->xso
.offload_handle
)
850 mask
|= CRYPTO_ALG_ASYNC
;
852 aead
= crypto_alloc_aead(authenc_name
, 0, mask
);
859 keylen
= (x
->aalg
? (x
->aalg
->alg_key_len
+ 7) / 8 : 0) +
860 (x
->ealg
->alg_key_len
+ 7) / 8 + RTA_SPACE(sizeof(*param
));
862 key
= kmalloc(keylen
, GFP_KERNEL
);
868 rta
->rta_type
= CRYPTO_AUTHENC_KEYA_PARAM
;
869 rta
->rta_len
= RTA_LENGTH(sizeof(*param
));
870 param
= RTA_DATA(rta
);
871 p
+= RTA_SPACE(sizeof(*param
));
874 struct xfrm_algo_desc
*aalg_desc
;
876 memcpy(p
, x
->aalg
->alg_key
, (x
->aalg
->alg_key_len
+ 7) / 8);
877 p
+= (x
->aalg
->alg_key_len
+ 7) / 8;
879 aalg_desc
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
883 if (aalg_desc
->uinfo
.auth
.icv_fullbits
/ 8 !=
884 crypto_aead_authsize(aead
)) {
885 pr_info("ESP: %s digestsize %u != %hu\n",
887 crypto_aead_authsize(aead
),
888 aalg_desc
->uinfo
.auth
.icv_fullbits
/ 8);
892 err
= crypto_aead_setauthsize(
893 aead
, x
->aalg
->alg_trunc_len
/ 8);
898 param
->enckeylen
= cpu_to_be32((x
->ealg
->alg_key_len
+ 7) / 8);
899 memcpy(p
, x
->ealg
->alg_key
, (x
->ealg
->alg_key_len
+ 7) / 8);
901 err
= crypto_aead_setkey(aead
, key
, keylen
);
910 static int esp_init_state(struct xfrm_state
*x
)
912 struct crypto_aead
*aead
;
919 err
= esp_init_aead(x
);
921 err
= esp_init_authenc(x
);
928 x
->props
.header_len
= sizeof(struct ip_esp_hdr
) +
929 crypto_aead_ivsize(aead
);
930 if (x
->props
.mode
== XFRM_MODE_TUNNEL
)
931 x
->props
.header_len
+= sizeof(struct iphdr
);
932 else if (x
->props
.mode
== XFRM_MODE_BEET
&& x
->sel
.family
!= AF_INET6
)
933 x
->props
.header_len
+= IPV4_BEET_PHMAXLEN
;
935 struct xfrm_encap_tmpl
*encap
= x
->encap
;
937 switch (encap
->encap_type
) {
940 case UDP_ENCAP_ESPINUDP
:
941 x
->props
.header_len
+= sizeof(struct udphdr
);
943 case UDP_ENCAP_ESPINUDP_NON_IKE
:
944 x
->props
.header_len
+= sizeof(struct udphdr
) + 2 * sizeof(u32
);
949 align
= ALIGN(crypto_aead_blocksize(aead
), 4);
950 x
->props
.trailer_len
= align
+ 1 + crypto_aead_authsize(aead
);
956 static int esp4_rcv_cb(struct sk_buff
*skb
, int err
)
961 static const struct xfrm_type esp_type
=
963 .description
= "ESP4",
964 .owner
= THIS_MODULE
,
965 .proto
= IPPROTO_ESP
,
966 .flags
= XFRM_TYPE_REPLAY_PROT
,
967 .init_state
= esp_init_state
,
968 .destructor
= esp_destroy
,
969 .get_mtu
= esp4_get_mtu
,
971 .output
= esp_output
,
974 static struct xfrm4_protocol esp4_protocol
= {
975 .handler
= xfrm4_rcv
,
976 .input_handler
= xfrm_input
,
977 .cb_handler
= esp4_rcv_cb
,
978 .err_handler
= esp4_err
,
982 static int __init
esp4_init(void)
984 if (xfrm_register_type(&esp_type
, AF_INET
) < 0) {
985 pr_info("%s: can't add xfrm type\n", __func__
);
988 if (xfrm4_protocol_register(&esp4_protocol
, IPPROTO_ESP
) < 0) {
989 pr_info("%s: can't add protocol\n", __func__
);
990 xfrm_unregister_type(&esp_type
, AF_INET
);
996 static void __exit
esp4_fini(void)
998 if (xfrm4_protocol_deregister(&esp4_protocol
, IPPROTO_ESP
) < 0)
999 pr_info("%s: can't remove protocol\n", __func__
);
1000 if (xfrm_unregister_type(&esp_type
, AF_INET
) < 0)
1001 pr_info("%s: can't remove xfrm type\n", __func__
);
1004 module_init(esp4_init
);
1005 module_exit(esp4_fini
);
1006 MODULE_LICENSE("GPL");
1007 MODULE_ALIAS_XFRM_TYPE(AF_INET
, XFRM_PROTO_ESP
);