blob: de019f9fbfe13bb819a5002766d2d161931fc7e2 [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>
11#include <net/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020012#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <net/udp.h>
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
16{
17 int err;
18 struct iphdr *top_iph;
19 struct ip_esp_hdr *esph;
Herbert Xu6b7326c2006-07-30 15:41:01 +100020 struct crypto_blkcipher *tfm;
21 struct blkcipher_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 struct esp_data *esp;
23 struct sk_buff *trailer;
24 int blksize;
25 int clen;
26 int alen;
27 int nfrags;
28
29 /* Strip IP+ESP header. */
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -070030 __skb_pull(skb, skb_transport_offset(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 /* Now skb is pure payload to encrypt */
32
33 err = -ENOMEM;
34
35 /* Round to block size */
36 clen = skb->len;
37
38 esp = x->data;
39 alen = esp->auth.icv_trunc_len;
40 tfm = esp->conf.tfm;
Herbert Xu6b7326c2006-07-30 15:41:01 +100041 desc.tfm = tfm;
42 desc.flags = 0;
43 blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
Herbert Xua02a6422005-10-10 21:11:08 -070044 clen = ALIGN(clen + 2, blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (esp->conf.padlen)
Herbert Xua02a6422005-10-10 21:11:08 -070046 clen = ALIGN(clen, esp->conf.padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0)
49 goto error;
50
51 /* Fill padding... */
52 do {
53 int i;
54 for (i=0; i<clen-skb->len - 2; i++)
55 *(u8*)(trailer->tail + i) = i+1;
56 } while (0);
57 *(u8*)(trailer->tail + clen-skb->len - 2) = (clen - skb->len)-2;
58 pskb_put(skb, trailer, clen - skb->len);
59
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070060 __skb_push(skb, skb->data - skb_network_header(skb));
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070061 top_iph = ip_hdr(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070062 esph = (struct ip_esp_hdr *)(skb_network_header(skb) +
63 top_iph->ihl * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 top_iph->tot_len = htons(skb->len + alen);
65 *(u8*)(trailer->tail - 1) = top_iph->protocol;
66
67 /* this is non-NULL only with UDP Encapsulation */
68 if (x->encap) {
69 struct xfrm_encap_tmpl *encap = x->encap;
70 struct udphdr *uh;
Al Virod5a0a1e2006-11-08 00:23:14 -080071 __be32 *udpdata32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 uh = (struct udphdr *)esph;
74 uh->source = encap->encap_sport;
75 uh->dest = encap->encap_dport;
76 uh->len = htons(skb->len + alen - top_iph->ihl*4);
77 uh->check = 0;
78
79 switch (encap->encap_type) {
80 default:
81 case UDP_ENCAP_ESPINUDP:
82 esph = (struct ip_esp_hdr *)(uh + 1);
83 break;
84 case UDP_ENCAP_ESPINUDP_NON_IKE:
Al Virod5a0a1e2006-11-08 00:23:14 -080085 udpdata32 = (__be32 *)(uh + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 udpdata32[0] = udpdata32[1] = 0;
87 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
88 break;
89 }
90
91 top_iph->protocol = IPPROTO_UDP;
92 } else
93 top_iph->protocol = IPPROTO_ESP;
94
95 esph->spi = x->id.spi;
96 esph->seq_no = htonl(++x->replay.oseq);
Jamal Hadi Salim9500e8a2006-03-20 19:15:29 -080097 xfrm_aevent_doreplay(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
David S. Millere4bec822006-09-22 15:17:35 -070099 if (esp->conf.ivlen) {
100 if (unlikely(!esp->conf.ivinitted)) {
101 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
102 esp->conf.ivinitted = 1;
103 }
Herbert Xu6b7326c2006-07-30 15:41:01 +1000104 crypto_blkcipher_set_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
David S. Millere4bec822006-09-22 15:17:35 -0700105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 do {
108 struct scatterlist *sg = &esp->sgbuf[0];
109
110 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
111 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
112 if (!sg)
113 goto error;
114 }
115 skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
Herbert Xu6b7326c2006-07-30 15:41:01 +1000116 err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (unlikely(sg != &esp->sgbuf[0]))
118 kfree(sg);
119 } while (0);
120
Herbert Xu6b7326c2006-07-30 15:41:01 +1000121 if (unlikely(err))
122 goto error;
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (esp->conf.ivlen) {
Herbert Xu6b7326c2006-07-30 15:41:01 +1000125 memcpy(esph->enc_data, esp->conf.ivec, esp->conf.ivlen);
126 crypto_blkcipher_get_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
129 if (esp->auth.icv_full_len) {
Herbert Xu07d4ee52006-08-20 14:24:50 +1000130 err = esp_mac_digest(esp, skb, (u8 *)esph - skb->data,
131 sizeof(*esph) + esp->conf.ivlen + clen);
132 memcpy(pskb_put(skb, trailer, alen), esp->auth.work_icv, alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
135 ip_send_check(top_iph);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137error:
138 return err;
139}
140
141/*
142 * Note: detecting truncated vs. non-truncated authentication data is very
143 * expensive, so we only support truncated data, which is the recommended
144 * and common case.
145 */
Herbert Xue6956332006-04-01 00:52:46 -0800146static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct iphdr *iph;
149 struct ip_esp_hdr *esph;
150 struct esp_data *esp = x->data;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000151 struct crypto_blkcipher *tfm = esp->conf.tfm;
152 struct blkcipher_desc desc = { .tfm = tfm };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct sk_buff *trailer;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000154 int blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 int alen = esp->auth.icv_trunc_len;
156 int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
157 int nfrags;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700158 int ihl;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800159 u8 nexthdr[2];
160 struct scatterlist *sg;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800161 int padlen;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000162 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
165 goto out;
166
167 if (elen <= 0 || (elen & (blksize-1)))
168 goto out;
169
170 /* If integrity check is required, do this. */
171 if (esp->auth.icv_full_len) {
Herbert Xu07d4ee52006-08-20 14:24:50 +1000172 u8 sum[alen];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Herbert Xu07d4ee52006-08-20 14:24:50 +1000174 err = esp_mac_digest(esp, skb, 0, skb->len - alen);
175 if (err)
176 goto out;
177
178 if (skb_copy_bits(skb, skb->len - alen, sum, alen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 BUG();
180
Herbert Xu07d4ee52006-08-20 14:24:50 +1000181 if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 x->stats.integrity_failed++;
183 goto out;
184 }
185 }
186
187 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
188 goto out;
189
190 skb->ip_summed = CHECKSUM_NONE;
191
192 esph = (struct ip_esp_hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /* Get ivec. This can be wrong, check against another impls. */
195 if (esp->conf.ivlen)
Herbert Xu6b7326c2006-07-30 15:41:01 +1000196 crypto_blkcipher_set_iv(tfm, esph->enc_data, esp->conf.ivlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800198 sg = &esp->sgbuf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800200 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
201 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
202 if (!sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800205 skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
Herbert Xu6b7326c2006-07-30 15:41:01 +1000206 err = crypto_blkcipher_decrypt(&desc, sg, sg, elen);
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800207 if (unlikely(sg != &esp->sgbuf[0]))
208 kfree(sg);
Herbert Xu6b7326c2006-07-30 15:41:01 +1000209 if (unlikely(err))
210 return err;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800211
212 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
213 BUG();
214
215 padlen = nexthdr[0];
216 if (padlen+2 >= elen)
217 goto out;
218
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900219 /* ... check padding bits here. Silly. :-) */
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800220
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700221 iph = ip_hdr(skb);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700222 ihl = iph->ihl * 4;
223
Herbert Xu752c1f42006-02-27 13:00:40 -0800224 if (x->encap) {
225 struct xfrm_encap_tmpl *encap = x->encap;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700226 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
Herbert Xu752c1f42006-02-27 13:00:40 -0800227
228 /*
229 * 1) if the NAT-T peer's IP or port changed then
230 * advertize the change to the keying daemon.
231 * This is an inbound SA, so just compare
232 * SRC ports.
233 */
234 if (iph->saddr != x->props.saddr.a4 ||
235 uh->source != encap->encap_sport) {
236 xfrm_address_t ipaddr;
237
238 ipaddr.a4 = iph->saddr;
239 km_new_mapping(x, &ipaddr, uh->source);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900240
Herbert Xu752c1f42006-02-27 13:00:40 -0800241 /* XXX: perhaps add an extra
242 * policy check here, to see
243 * if we should allow or
244 * reject a packet from a
245 * different source
246 * address/port.
247 */
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800248 }
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900249
Herbert Xu752c1f42006-02-27 13:00:40 -0800250 /*
251 * 2) ignore UDP/TCP checksums in case
252 * of NAT-T in Transport Mode, or
253 * perform other post-processing fixes
254 * as per draft-ietf-ipsec-udp-encaps-06,
255 * section 3.1.2
256 */
Diego Beltrami0a694522006-10-03 23:47:05 -0700257 if (x->props.mode == XFRM_MODE_TRANSPORT ||
258 x->props.mode == XFRM_MODE_BEET)
Herbert Xu752c1f42006-02-27 13:00:40 -0800259 skb->ip_summed = CHECKSUM_UNNECESSARY;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800260 }
261
262 iph->protocol = nexthdr[1];
263 pskb_trim(skb, skb->len - alen - padlen - 2);
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -0300264 __skb_pull(skb, sizeof(*esph) + esp->conf.ivlen);
265 skb_set_transport_header(skb, -ihl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 return 0;
268
269out:
270 return -EINVAL;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
274{
275 struct esp_data *esp = x->data;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000276 u32 blksize = ALIGN(crypto_blkcipher_blocksize(esp->conf.tfm), 4);
Diego Beltrami0a694522006-10-03 23:47:05 -0700277 int enclen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Diego Beltrami0a694522006-10-03 23:47:05 -0700279 switch (x->props.mode) {
280 case XFRM_MODE_TUNNEL:
281 mtu = ALIGN(mtu +2, blksize);
282 break;
283 default:
284 case XFRM_MODE_TRANSPORT:
285 /* The worst case */
Herbert Xud4875b02005-10-10 21:11:34 -0700286 mtu = ALIGN(mtu + 2, 4) + blksize - 4;
Diego Beltrami0a694522006-10-03 23:47:05 -0700287 break;
288 case XFRM_MODE_BEET:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900289 /* The worst case. */
Diego Beltrami0a694522006-10-03 23:47:05 -0700290 enclen = IPV4_BEET_PHMAXLEN;
291 mtu = ALIGN(mtu + enclen + 2, blksize);
292 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Diego Beltrami0a694522006-10-03 23:47:05 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (esp->conf.padlen)
Herbert Xua02a6422005-10-10 21:11:08 -0700296 mtu = ALIGN(mtu, esp->conf.padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Diego Beltrami0a694522006-10-03 23:47:05 -0700298 return mtu + x->props.header_len + esp->auth.icv_trunc_len - enclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static void esp4_err(struct sk_buff *skb, u32 info)
302{
303 struct iphdr *iph = (struct iphdr*)skb->data;
304 struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
305 struct xfrm_state *x;
306
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300307 if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
308 icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return;
310
311 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
312 if (!x)
313 return;
Patrick McHardy64ce2072005-08-09 20:50:53 -0700314 NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
315 ntohl(esph->spi), ntohl(iph->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 xfrm_state_put(x);
317}
318
319static void esp_destroy(struct xfrm_state *x)
320{
321 struct esp_data *esp = x->data;
322
323 if (!esp)
324 return;
325
Herbert Xu6b7326c2006-07-30 15:41:01 +1000326 crypto_free_blkcipher(esp->conf.tfm);
Jesper Juhl573dbd92005-09-01 17:44:29 -0700327 esp->conf.tfm = NULL;
328 kfree(esp->conf.ivec);
329 esp->conf.ivec = NULL;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000330 crypto_free_hash(esp->auth.tfm);
Jesper Juhl573dbd92005-09-01 17:44:29 -0700331 esp->auth.tfm = NULL;
332 kfree(esp->auth.work_icv);
333 esp->auth.work_icv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 kfree(esp);
335}
336
Herbert Xu72cb6962005-06-20 13:18:08 -0700337static int esp_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 struct esp_data *esp = NULL;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000340 struct crypto_blkcipher *tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* null auth and encryption can have zero length keys */
343 if (x->aalg) {
344 if (x->aalg->alg_key_len > 512)
345 goto error;
346 }
347 if (x->ealg == NULL)
348 goto error;
349
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700350 esp = kzalloc(sizeof(*esp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (esp == NULL)
352 return -ENOMEM;
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (x->aalg) {
355 struct xfrm_algo_desc *aalg_desc;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000356 struct crypto_hash *hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 esp->auth.key = x->aalg->alg_key;
359 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000360 hash = crypto_alloc_hash(x->aalg->alg_name, 0,
361 CRYPTO_ALG_ASYNC);
362 if (IS_ERR(hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 goto error;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000364
365 esp->auth.tfm = hash;
366 if (crypto_hash_setkey(hash, esp->auth.key, esp->auth.key_len))
367 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
370 BUG_ON(!aalg_desc);
371
372 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
Herbert Xu07d4ee52006-08-20 14:24:50 +1000373 crypto_hash_digestsize(hash)) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700374 NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
375 x->aalg->alg_name,
Herbert Xu07d4ee52006-08-20 14:24:50 +1000376 crypto_hash_digestsize(hash),
Patrick McHardy64ce2072005-08-09 20:50:53 -0700377 aalg_desc->uinfo.auth.icv_fullbits/8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goto error;
379 }
380
381 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
382 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
383
384 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
385 if (!esp->auth.work_icv)
386 goto error;
387 }
388 esp->conf.key = x->ealg->alg_key;
389 esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000390 tfm = crypto_alloc_blkcipher(x->ealg->alg_name, 0, CRYPTO_ALG_ASYNC);
391 if (IS_ERR(tfm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto error;
Herbert Xu6b7326c2006-07-30 15:41:01 +1000393 esp->conf.tfm = tfm;
394 esp->conf.ivlen = crypto_blkcipher_ivsize(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 esp->conf.padlen = 0;
396 if (esp->conf.ivlen) {
397 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
398 if (unlikely(esp->conf.ivec == NULL))
399 goto error;
David S. Millere4bec822006-09-22 15:17:35 -0700400 esp->conf.ivinitted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Herbert Xu6b7326c2006-07-30 15:41:01 +1000402 if (crypto_blkcipher_setkey(tfm, esp->conf.key, esp->conf.key_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto error;
404 x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
Masahide NAKAMURA7e49e6d2006-09-22 15:05:15 -0700405 if (x->props.mode == XFRM_MODE_TUNNEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 x->props.header_len += sizeof(struct iphdr);
407 if (x->encap) {
408 struct xfrm_encap_tmpl *encap = x->encap;
409
410 switch (encap->encap_type) {
411 default:
412 goto error;
413 case UDP_ENCAP_ESPINUDP:
414 x->props.header_len += sizeof(struct udphdr);
415 break;
416 case UDP_ENCAP_ESPINUDP_NON_IKE:
417 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
418 break;
419 }
420 }
421 x->data = esp;
422 x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
423 return 0;
424
425error:
426 x->data = esp;
427 esp_destroy(x);
428 x->data = NULL;
429 return -EINVAL;
430}
431
432static struct xfrm_type esp_type =
433{
434 .description = "ESP4",
435 .owner = THIS_MODULE,
436 .proto = IPPROTO_ESP,
437 .init_state = esp_init_state,
438 .destructor = esp_destroy,
439 .get_max_size = esp4_get_max_size,
440 .input = esp_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 .output = esp_output
442};
443
444static struct net_protocol esp4_protocol = {
445 .handler = xfrm4_rcv,
446 .err_handler = esp4_err,
447 .no_policy = 1,
448};
449
450static int __init esp4_init(void)
451{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
453 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
454 return -EAGAIN;
455 }
456 if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
457 printk(KERN_INFO "ip esp init: can't add protocol\n");
458 xfrm_unregister_type(&esp_type, AF_INET);
459 return -EAGAIN;
460 }
461 return 0;
462}
463
464static void __exit esp4_fini(void)
465{
466 if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
467 printk(KERN_INFO "ip esp close: can't remove protocol\n");
468 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
469 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
470}
471
472module_init(esp4_init);
473module_exit(esp4_fini);
474MODULE_LICENSE("GPL");