blob: 452910dae89fbfca056c6f79ce7ac8e3c8427b59 [file] [log] [blame]
Herbert Xu6b7326c2006-07-30 15:41:01 +10001#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/module.h>
3#include <net/ip.h>
4#include <net/xfrm.h>
5#include <net/esp.h>
6#include <asm/scatterlist.h>
7#include <linux/crypto.h>
Herbert Xua02a6422005-10-10 21:11:08 -07008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/pfkeyv2.h>
10#include <linux/random.h>
Herbert Xub7c65382007-10-09 13:33:35 -070011#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <net/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020013#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <net/udp.h>
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
17{
18 int err;
19 struct iphdr *top_iph;
20 struct ip_esp_hdr *esph;
Herbert Xu6b7326c2006-07-30 15:41:01 +100021 struct crypto_blkcipher *tfm;
22 struct blkcipher_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 struct esp_data *esp;
24 struct sk_buff *trailer;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070025 u8 *tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 int blksize;
27 int clen;
28 int alen;
29 int nfrags;
30
31 /* Strip IP+ESP header. */
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -070032 __skb_pull(skb, skb_transport_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 /* Now skb is pure payload to encrypt */
34
35 err = -ENOMEM;
36
37 /* Round to block size */
38 clen = skb->len;
39
40 esp = x->data;
41 alen = esp->auth.icv_trunc_len;
42 tfm = esp->conf.tfm;
Herbert Xu6b7326c2006-07-30 15:41:01 +100043 desc.tfm = tfm;
44 desc.flags = 0;
45 blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
Herbert Xua02a6422005-10-10 21:11:08 -070046 clen = ALIGN(clen + 2, blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (esp->conf.padlen)
Herbert Xua02a6422005-10-10 21:11:08 -070048 clen = ALIGN(clen, esp->conf.padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0)
51 goto error;
52
53 /* Fill padding... */
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070054 tail = skb_tail_pointer(trailer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 do {
56 int i;
57 for (i=0; i<clen-skb->len - 2; i++)
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070058 tail[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 } while (0);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070060 tail[clen - skb->len - 2] = (clen - skb->len) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 pskb_put(skb, trailer, clen - skb->len);
62
Herbert Xu007f0212007-10-09 13:25:59 -070063 __skb_push(skb, -skb_network_offset(skb));
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070064 top_iph = ip_hdr(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070065 esph = (struct ip_esp_hdr *)(skb_network_header(skb) +
66 top_iph->ihl * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 top_iph->tot_len = htons(skb->len + alen);
Patrick McHardy55792252007-04-09 11:46:17 -070068 *(skb_tail_pointer(trailer) - 1) = top_iph->protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Herbert Xub7c65382007-10-09 13:33:35 -070070 spin_lock_bh(&x->lock);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* this is non-NULL only with UDP Encapsulation */
73 if (x->encap) {
74 struct xfrm_encap_tmpl *encap = x->encap;
75 struct udphdr *uh;
Al Virod5a0a1e2006-11-08 00:23:14 -080076 __be32 *udpdata32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 uh = (struct udphdr *)esph;
79 uh->source = encap->encap_sport;
80 uh->dest = encap->encap_dport;
81 uh->len = htons(skb->len + alen - top_iph->ihl*4);
82 uh->check = 0;
83
84 switch (encap->encap_type) {
85 default:
86 case UDP_ENCAP_ESPINUDP:
87 esph = (struct ip_esp_hdr *)(uh + 1);
88 break;
89 case UDP_ENCAP_ESPINUDP_NON_IKE:
Al Virod5a0a1e2006-11-08 00:23:14 -080090 udpdata32 = (__be32 *)(uh + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 udpdata32[0] = udpdata32[1] = 0;
92 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
93 break;
94 }
95
96 top_iph->protocol = IPPROTO_UDP;
97 } else
98 top_iph->protocol = IPPROTO_ESP;
99
100 esph->spi = x->id.spi;
Herbert Xu436a0a42007-10-08 17:25:53 -0700101 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
David S. Millere4bec822006-09-22 15:17:35 -0700103 if (esp->conf.ivlen) {
104 if (unlikely(!esp->conf.ivinitted)) {
105 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
106 esp->conf.ivinitted = 1;
107 }
Herbert Xu6b7326c2006-07-30 15:41:01 +1000108 crypto_blkcipher_set_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
David S. Millere4bec822006-09-22 15:17:35 -0700109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 do {
112 struct scatterlist *sg = &esp->sgbuf[0];
113
114 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
115 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
116 if (!sg)
Herbert Xub7c65382007-10-09 13:33:35 -0700117 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
Herbert Xu6b7326c2006-07-30 15:41:01 +1000120 err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (unlikely(sg != &esp->sgbuf[0]))
122 kfree(sg);
123 } while (0);
124
Herbert Xu6b7326c2006-07-30 15:41:01 +1000125 if (unlikely(err))
Herbert Xub7c65382007-10-09 13:33:35 -0700126 goto unlock;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (esp->conf.ivlen) {
Herbert Xu6b7326c2006-07-30 15:41:01 +1000129 memcpy(esph->enc_data, esp->conf.ivec, esp->conf.ivlen);
130 crypto_blkcipher_get_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132
133 if (esp->auth.icv_full_len) {
Herbert Xu07d4ee52006-08-20 14:24:50 +1000134 err = esp_mac_digest(esp, skb, (u8 *)esph - skb->data,
135 sizeof(*esph) + esp->conf.ivlen + clen);
136 memcpy(pskb_put(skb, trailer, alen), esp->auth.work_icv, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
Herbert Xub7c65382007-10-09 13:33:35 -0700139unlock:
140 spin_unlock_bh(&x->lock);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 ip_send_check(top_iph);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144error:
145 return err;
146}
147
148/*
149 * Note: detecting truncated vs. non-truncated authentication data is very
150 * expensive, so we only support truncated data, which is the recommended
151 * and common case.
152 */
Herbert Xue6956332006-04-01 00:52:46 -0800153static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct iphdr *iph;
156 struct ip_esp_hdr *esph;
157 struct esp_data *esp = x->data;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000158 struct crypto_blkcipher *tfm = esp->conf.tfm;
159 struct blkcipher_desc desc = { .tfm = tfm };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct sk_buff *trailer;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000161 int blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int alen = esp->auth.icv_trunc_len;
163 int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
164 int nfrags;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700165 int ihl;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800166 u8 nexthdr[2];
167 struct scatterlist *sg;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800168 int padlen;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000169 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
172 goto out;
173
174 if (elen <= 0 || (elen & (blksize-1)))
175 goto out;
176
177 /* If integrity check is required, do this. */
178 if (esp->auth.icv_full_len) {
Herbert Xu07d4ee52006-08-20 14:24:50 +1000179 u8 sum[alen];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Herbert Xu07d4ee52006-08-20 14:24:50 +1000181 err = esp_mac_digest(esp, skb, 0, skb->len - alen);
182 if (err)
183 goto out;
184
185 if (skb_copy_bits(skb, skb->len - alen, sum, alen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 BUG();
187
Herbert Xu07d4ee52006-08-20 14:24:50 +1000188 if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 x->stats.integrity_failed++;
190 goto out;
191 }
192 }
193
194 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
195 goto out;
196
197 skb->ip_summed = CHECKSUM_NONE;
198
199 esph = (struct ip_esp_hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* Get ivec. This can be wrong, check against another impls. */
202 if (esp->conf.ivlen)
Herbert Xu6b7326c2006-07-30 15:41:01 +1000203 crypto_blkcipher_set_iv(tfm, esph->enc_data, esp->conf.ivlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800205 sg = &esp->sgbuf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800207 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
208 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
209 if (!sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800212 skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
Herbert Xu6b7326c2006-07-30 15:41:01 +1000213 err = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800214 if (unlikely(sg != &esp->sgbuf[0]))
215 kfree(sg);
Herbert Xu6b7326c2006-07-30 15:41:01 +1000216 if (unlikely(err))
217 return err;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800218
219 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
220 BUG();
221
222 padlen = nexthdr[0];
223 if (padlen+2 >= elen)
224 goto out;
225
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900226 /* ... check padding bits here. Silly. :-) */
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800227
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700228 iph = ip_hdr(skb);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700229 ihl = iph->ihl * 4;
230
Herbert Xu752c1f42006-02-27 13:00:40 -0800231 if (x->encap) {
232 struct xfrm_encap_tmpl *encap = x->encap;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700233 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
Herbert Xu752c1f42006-02-27 13:00:40 -0800234
235 /*
236 * 1) if the NAT-T peer's IP or port changed then
237 * advertize the change to the keying daemon.
238 * This is an inbound SA, so just compare
239 * SRC ports.
240 */
241 if (iph->saddr != x->props.saddr.a4 ||
242 uh->source != encap->encap_sport) {
243 xfrm_address_t ipaddr;
244
245 ipaddr.a4 = iph->saddr;
246 km_new_mapping(x, &ipaddr, uh->source);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900247
Herbert Xu752c1f42006-02-27 13:00:40 -0800248 /* XXX: perhaps add an extra
249 * policy check here, to see
250 * if we should allow or
251 * reject a packet from a
252 * different source
253 * address/port.
254 */
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800255 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900256
Herbert Xu752c1f42006-02-27 13:00:40 -0800257 /*
258 * 2) ignore UDP/TCP checksums in case
259 * of NAT-T in Transport Mode, or
260 * perform other post-processing fixes
261 * as per draft-ietf-ipsec-udp-encaps-06,
262 * section 3.1.2
263 */
Diego Beltrami0a694522006-10-03 23:47:05 -0700264 if (x->props.mode == XFRM_MODE_TRANSPORT ||
265 x->props.mode == XFRM_MODE_BEET)
Herbert Xu752c1f42006-02-27 13:00:40 -0800266 skb->ip_summed = CHECKSUM_UNNECESSARY;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800267 }
268
269 iph->protocol = nexthdr[1];
270 pskb_trim(skb, skb->len - alen - padlen - 2);
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -0300271 __skb_pull(skb, sizeof(*esph) + esp->conf.ivlen);
272 skb_set_transport_header(skb, -ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 return 0;
275
276out:
277 return -EINVAL;
278}
279
Patrick McHardyc5c25232007-04-09 11:47:18 -0700280static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct esp_data *esp = x->data;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000283 u32 blksize = ALIGN(crypto_blkcipher_blocksize(esp->conf.tfm), 4);
Patrick McHardyc5c25232007-04-09 11:47:18 -0700284 u32 align = max_t(u32, blksize, esp->conf.padlen);
285 u32 rem;
286
287 mtu -= x->props.header_len + esp->auth.icv_trunc_len;
288 rem = mtu & (align - 1);
289 mtu &= ~(align - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Diego Beltrami0a694522006-10-03 23:47:05 -0700291 switch (x->props.mode) {
292 case XFRM_MODE_TUNNEL:
Diego Beltrami0a694522006-10-03 23:47:05 -0700293 break;
294 default:
295 case XFRM_MODE_TRANSPORT:
296 /* The worst case */
Patrick McHardyc5c25232007-04-09 11:47:18 -0700297 mtu -= blksize - 4;
298 mtu += min_t(u32, blksize - 4, rem);
Diego Beltrami0a694522006-10-03 23:47:05 -0700299 break;
300 case XFRM_MODE_BEET:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900301 /* The worst case. */
Patrick McHardyc5c25232007-04-09 11:47:18 -0700302 mtu += min_t(u32, IPV4_BEET_PHMAXLEN, rem);
Diego Beltrami0a694522006-10-03 23:47:05 -0700303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Diego Beltrami0a694522006-10-03 23:47:05 -0700305
Patrick McHardyc5c25232007-04-09 11:47:18 -0700306 return mtu - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309static void esp4_err(struct sk_buff *skb, u32 info)
310{
311 struct iphdr *iph = (struct iphdr*)skb->data;
312 struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
313 struct xfrm_state *x;
314
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300315 if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
316 icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return;
318
319 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
320 if (!x)
321 return;
Patrick McHardy64ce2072005-08-09 20:50:53 -0700322 NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
323 ntohl(esph->spi), ntohl(iph->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 xfrm_state_put(x);
325}
326
327static void esp_destroy(struct xfrm_state *x)
328{
329 struct esp_data *esp = x->data;
330
331 if (!esp)
332 return;
333
Herbert Xu6b7326c2006-07-30 15:41:01 +1000334 crypto_free_blkcipher(esp->conf.tfm);
Jesper Juhl573dbd92005-09-01 17:44:29 -0700335 esp->conf.tfm = NULL;
336 kfree(esp->conf.ivec);
337 esp->conf.ivec = NULL;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000338 crypto_free_hash(esp->auth.tfm);
Jesper Juhl573dbd92005-09-01 17:44:29 -0700339 esp->auth.tfm = NULL;
340 kfree(esp->auth.work_icv);
341 esp->auth.work_icv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 kfree(esp);
343}
344
Herbert Xu72cb6962005-06-20 13:18:08 -0700345static int esp_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 struct esp_data *esp = NULL;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000348 struct crypto_blkcipher *tfm;
Patrick McHardyc5c25232007-04-09 11:47:18 -0700349 u32 align;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (x->ealg == NULL)
352 goto error;
353
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700354 esp = kzalloc(sizeof(*esp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (esp == NULL)
356 return -ENOMEM;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (x->aalg) {
359 struct xfrm_algo_desc *aalg_desc;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000360 struct crypto_hash *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Herbert Xu07d4ee52006-08-20 14:24:50 +1000362 hash = crypto_alloc_hash(x->aalg->alg_name, 0,
363 CRYPTO_ALG_ASYNC);
364 if (IS_ERR(hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto error;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000366
367 esp->auth.tfm = hash;
Herbert Xu4b7137f2007-10-08 17:13:44 -0700368 if (crypto_hash_setkey(hash, x->aalg->alg_key,
369 (x->aalg->alg_key_len + 7) / 8))
Herbert Xu07d4ee52006-08-20 14:24:50 +1000370 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
373 BUG_ON(!aalg_desc);
374
375 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
Herbert Xu07d4ee52006-08-20 14:24:50 +1000376 crypto_hash_digestsize(hash)) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700377 NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
378 x->aalg->alg_name,
Herbert Xu07d4ee52006-08-20 14:24:50 +1000379 crypto_hash_digestsize(hash),
Patrick McHardy64ce2072005-08-09 20:50:53 -0700380 aalg_desc->uinfo.auth.icv_fullbits/8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto error;
382 }
383
384 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
385 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
386
387 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
388 if (!esp->auth.work_icv)
389 goto error;
390 }
Herbert Xu4b7137f2007-10-08 17:13:44 -0700391
Herbert Xu6b7326c2006-07-30 15:41:01 +1000392 tfm = crypto_alloc_blkcipher(x->ealg->alg_name, 0, CRYPTO_ALG_ASYNC);
393 if (IS_ERR(tfm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto error;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000395 esp->conf.tfm = tfm;
396 esp->conf.ivlen = crypto_blkcipher_ivsize(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 esp->conf.padlen = 0;
398 if (esp->conf.ivlen) {
399 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
400 if (unlikely(esp->conf.ivec == NULL))
401 goto error;
David S. Millere4bec822006-09-22 15:17:35 -0700402 esp->conf.ivinitted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Herbert Xu4b7137f2007-10-08 17:13:44 -0700404 if (crypto_blkcipher_setkey(tfm, x->ealg->alg_key,
405 (x->ealg->alg_key_len + 7) / 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto error;
407 x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700408 if (x->props.mode == XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 x->props.header_len += sizeof(struct iphdr);
Patrick McHardyac758e32007-04-09 11:47:58 -0700410 else if (x->props.mode == XFRM_MODE_BEET)
411 x->props.header_len += IPV4_BEET_PHMAXLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (x->encap) {
413 struct xfrm_encap_tmpl *encap = x->encap;
414
415 switch (encap->encap_type) {
416 default:
417 goto error;
418 case UDP_ENCAP_ESPINUDP:
419 x->props.header_len += sizeof(struct udphdr);
420 break;
421 case UDP_ENCAP_ESPINUDP_NON_IKE:
422 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
423 break;
424 }
425 }
426 x->data = esp;
Patrick McHardyc5c25232007-04-09 11:47:18 -0700427 align = ALIGN(crypto_blkcipher_blocksize(esp->conf.tfm), 4);
428 if (esp->conf.padlen)
429 align = max_t(u32, align, esp->conf.padlen);
430 x->props.trailer_len = align + 1 + esp->auth.icv_trunc_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432
433error:
434 x->data = esp;
435 esp_destroy(x);
436 x->data = NULL;
437 return -EINVAL;
438}
439
440static struct xfrm_type esp_type =
441{
442 .description = "ESP4",
443 .owner = THIS_MODULE,
444 .proto = IPPROTO_ESP,
Herbert Xu436a0a42007-10-08 17:25:53 -0700445 .flags = XFRM_TYPE_REPLAY_PROT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 .init_state = esp_init_state,
447 .destructor = esp_destroy,
Patrick McHardyc5c25232007-04-09 11:47:18 -0700448 .get_mtu = esp4_get_mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 .input = esp_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 .output = esp_output
451};
452
453static struct net_protocol esp4_protocol = {
454 .handler = xfrm4_rcv,
455 .err_handler = esp4_err,
456 .no_policy = 1,
457};
458
459static int __init esp4_init(void)
460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
462 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
463 return -EAGAIN;
464 }
465 if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
466 printk(KERN_INFO "ip esp init: can't add protocol\n");
467 xfrm_unregister_type(&esp_type, AF_INET);
468 return -EAGAIN;
469 }
470 return 0;
471}
472
473static void __exit esp4_fini(void)
474{
475 if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
476 printk(KERN_INFO "ip esp close: can't remove protocol\n");
477 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
478 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
479}
480
481module_init(esp4_init);
482module_exit(esp4_fini);
483MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700484MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);