Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 1 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #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 Xu | a02a642 | 2005-10-10 21:11:08 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/pfkeyv2.h> |
| 10 | #include <linux/random.h> |
| 11 | #include <net/icmp.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 12 | #include <net/protocol.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <net/udp.h> |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | static 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 Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 20 | struct crypto_blkcipher *tfm; |
| 21 | struct blkcipher_desc desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | 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. */ |
| 30 | __skb_pull(skb, skb->h.raw - skb->data); |
| 31 | /* 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 Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 41 | desc.tfm = tfm; |
| 42 | desc.flags = 0; |
| 43 | blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4); |
Herbert Xu | a02a642 | 2005-10-10 21:11:08 -0700 | [diff] [blame] | 44 | clen = ALIGN(clen + 2, blksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | if (esp->conf.padlen) |
Herbert Xu | a02a642 | 2005-10-10 21:11:08 -0700 | [diff] [blame] | 46 | clen = ALIGN(clen, esp->conf.padlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 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 Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 60 | __skb_push(skb, skb->data - skb_network_header(skb)); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 61 | top_iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 62 | esph = (struct ip_esp_hdr *)(skb_network_header(skb) + |
| 63 | top_iph->ihl * 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | 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 Viro | d5a0a1e | 2006-11-08 00:23:14 -0800 | [diff] [blame] | 71 | __be32 *udpdata32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 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 Viro | d5a0a1e | 2006-11-08 00:23:14 -0800 | [diff] [blame] | 85 | udpdata32 = (__be32 *)(uh + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | 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 Salim | 9500e8a | 2006-03-20 19:15:29 -0800 | [diff] [blame] | 97 | xfrm_aevent_doreplay(x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
David S. Miller | e4bec82 | 2006-09-22 15:17:35 -0700 | [diff] [blame] | 99 | 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 Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 104 | crypto_blkcipher_set_iv(tfm, esp->conf.ivec, esp->conf.ivlen); |
David S. Miller | e4bec82 | 2006-09-22 15:17:35 -0700 | [diff] [blame] | 105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 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 Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 116 | err = crypto_blkcipher_encrypt(&desc, sg, sg, clen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | if (unlikely(sg != &esp->sgbuf[0])) |
| 118 | kfree(sg); |
| 119 | } while (0); |
| 120 | |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 121 | if (unlikely(err)) |
| 122 | goto error; |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | if (esp->conf.ivlen) { |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 125 | memcpy(esph->enc_data, esp->conf.ivec, esp->conf.ivlen); |
| 126 | crypto_blkcipher_get_iv(tfm, esp->conf.ivec, esp->conf.ivlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (esp->auth.icv_full_len) { |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 130 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | ip_send_check(top_iph); |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | error: |
| 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 Xu | e695633 | 2006-04-01 00:52:46 -0800 | [diff] [blame] | 146 | static int esp_input(struct xfrm_state *x, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | struct iphdr *iph; |
| 149 | struct ip_esp_hdr *esph; |
| 150 | struct esp_data *esp = x->data; |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 151 | struct crypto_blkcipher *tfm = esp->conf.tfm; |
| 152 | struct blkcipher_desc desc = { .tfm = tfm }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | struct sk_buff *trailer; |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 154 | int blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | 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 Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 158 | int ihl; |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 159 | u8 nexthdr[2]; |
| 160 | struct scatterlist *sg; |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 161 | int padlen; |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 162 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 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 Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 172 | u8 sum[alen]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 174 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | BUG(); |
| 180 | |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 181 | if (unlikely(memcmp(esp->auth.work_icv, sum, alen))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | /* Get ivec. This can be wrong, check against another impls. */ |
| 195 | if (esp->conf.ivlen) |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 196 | crypto_blkcipher_set_iv(tfm, esph->enc_data, esp->conf.ivlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 198 | sg = &esp->sgbuf[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 200 | if (unlikely(nfrags > ESP_NUM_FAST_SG)) { |
| 201 | sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC); |
| 202 | if (!sg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 205 | skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen); |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 206 | err = crypto_blkcipher_decrypt(&desc, sg, sg, elen); |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 207 | if (unlikely(sg != &esp->sgbuf[0])) |
| 208 | kfree(sg); |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 209 | if (unlikely(err)) |
| 210 | return err; |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 211 | |
| 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 Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 219 | /* ... check padding bits here. Silly. :-) */ |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 220 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 221 | iph = ip_hdr(skb); |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 222 | ihl = iph->ihl * 4; |
| 223 | |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 224 | if (x->encap) { |
| 225 | struct xfrm_encap_tmpl *encap = x->encap; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 226 | struct udphdr *uh = (void *)(skb_network_header(skb) + ihl); |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 227 | |
| 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 Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 240 | |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 241 | /* 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 Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 248 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 249 | |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 250 | /* |
| 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 Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 257 | if (x->props.mode == XFRM_MODE_TRANSPORT || |
| 258 | x->props.mode == XFRM_MODE_BEET) |
Herbert Xu | 752c1f4 | 2006-02-27 13:00:40 -0800 | [diff] [blame] | 259 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Herbert Xu | 4bf05ec | 2006-02-27 13:00:01 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | iph->protocol = nexthdr[1]; |
| 263 | pskb_trim(skb, skb->len - alen - padlen - 2); |
Herbert Xu | 31a4ab9 | 2006-05-27 23:06:13 -0700 | [diff] [blame] | 264 | skb->h.raw = __skb_pull(skb, sizeof(*esph) + esp->conf.ivlen) - ihl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
| 266 | return 0; |
| 267 | |
| 268 | out: |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | static u32 esp4_get_max_size(struct xfrm_state *x, int mtu) |
| 273 | { |
| 274 | struct esp_data *esp = x->data; |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 275 | u32 blksize = ALIGN(crypto_blkcipher_blocksize(esp->conf.tfm), 4); |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 276 | int enclen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 278 | switch (x->props.mode) { |
| 279 | case XFRM_MODE_TUNNEL: |
| 280 | mtu = ALIGN(mtu +2, blksize); |
| 281 | break; |
| 282 | default: |
| 283 | case XFRM_MODE_TRANSPORT: |
| 284 | /* The worst case */ |
Herbert Xu | d4875b0 | 2005-10-10 21:11:34 -0700 | [diff] [blame] | 285 | mtu = ALIGN(mtu + 2, 4) + blksize - 4; |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 286 | break; |
| 287 | case XFRM_MODE_BEET: |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 288 | /* The worst case. */ |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 289 | enclen = IPV4_BEET_PHMAXLEN; |
| 290 | mtu = ALIGN(mtu + enclen + 2, blksize); |
| 291 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | if (esp->conf.padlen) |
Herbert Xu | a02a642 | 2005-10-10 21:11:08 -0700 | [diff] [blame] | 295 | mtu = ALIGN(mtu, esp->conf.padlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Diego Beltrami | 0a69452 | 2006-10-03 23:47:05 -0700 | [diff] [blame] | 297 | return mtu + x->props.header_len + esp->auth.icv_trunc_len - enclen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static void esp4_err(struct sk_buff *skb, u32 info) |
| 301 | { |
| 302 | struct iphdr *iph = (struct iphdr*)skb->data; |
| 303 | struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2)); |
| 304 | struct xfrm_state *x; |
| 305 | |
| 306 | if (skb->h.icmph->type != ICMP_DEST_UNREACH || |
| 307 | skb->h.icmph->code != ICMP_FRAG_NEEDED) |
| 308 | return; |
| 309 | |
| 310 | x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET); |
| 311 | if (!x) |
| 312 | return; |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 313 | NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n", |
| 314 | ntohl(esph->spi), ntohl(iph->daddr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | xfrm_state_put(x); |
| 316 | } |
| 317 | |
| 318 | static void esp_destroy(struct xfrm_state *x) |
| 319 | { |
| 320 | struct esp_data *esp = x->data; |
| 321 | |
| 322 | if (!esp) |
| 323 | return; |
| 324 | |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 325 | crypto_free_blkcipher(esp->conf.tfm); |
Jesper Juhl | 573dbd9 | 2005-09-01 17:44:29 -0700 | [diff] [blame] | 326 | esp->conf.tfm = NULL; |
| 327 | kfree(esp->conf.ivec); |
| 328 | esp->conf.ivec = NULL; |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 329 | crypto_free_hash(esp->auth.tfm); |
Jesper Juhl | 573dbd9 | 2005-09-01 17:44:29 -0700 | [diff] [blame] | 330 | esp->auth.tfm = NULL; |
| 331 | kfree(esp->auth.work_icv); |
| 332 | esp->auth.work_icv = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | kfree(esp); |
| 334 | } |
| 335 | |
Herbert Xu | 72cb696 | 2005-06-20 13:18:08 -0700 | [diff] [blame] | 336 | static int esp_init_state(struct xfrm_state *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
| 338 | struct esp_data *esp = NULL; |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 339 | struct crypto_blkcipher *tfm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
| 341 | /* null auth and encryption can have zero length keys */ |
| 342 | if (x->aalg) { |
| 343 | if (x->aalg->alg_key_len > 512) |
| 344 | goto error; |
| 345 | } |
| 346 | if (x->ealg == NULL) |
| 347 | goto error; |
| 348 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 349 | esp = kzalloc(sizeof(*esp), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (esp == NULL) |
| 351 | return -ENOMEM; |
| 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | if (x->aalg) { |
| 354 | struct xfrm_algo_desc *aalg_desc; |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 355 | struct crypto_hash *hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
| 357 | esp->auth.key = x->aalg->alg_key; |
| 358 | esp->auth.key_len = (x->aalg->alg_key_len+7)/8; |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 359 | hash = crypto_alloc_hash(x->aalg->alg_name, 0, |
| 360 | CRYPTO_ALG_ASYNC); |
| 361 | if (IS_ERR(hash)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | goto error; |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 363 | |
| 364 | esp->auth.tfm = hash; |
| 365 | if (crypto_hash_setkey(hash, esp->auth.key, esp->auth.key_len)) |
| 366 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); |
| 369 | BUG_ON(!aalg_desc); |
| 370 | |
| 371 | if (aalg_desc->uinfo.auth.icv_fullbits/8 != |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 372 | crypto_hash_digestsize(hash)) { |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 373 | NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n", |
| 374 | x->aalg->alg_name, |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 375 | crypto_hash_digestsize(hash), |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 376 | aalg_desc->uinfo.auth.icv_fullbits/8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | goto error; |
| 378 | } |
| 379 | |
| 380 | esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; |
| 381 | esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8; |
| 382 | |
| 383 | esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL); |
| 384 | if (!esp->auth.work_icv) |
| 385 | goto error; |
| 386 | } |
| 387 | esp->conf.key = x->ealg->alg_key; |
| 388 | esp->conf.key_len = (x->ealg->alg_key_len+7)/8; |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 389 | tfm = crypto_alloc_blkcipher(x->ealg->alg_name, 0, CRYPTO_ALG_ASYNC); |
| 390 | if (IS_ERR(tfm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | goto error; |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 392 | esp->conf.tfm = tfm; |
| 393 | esp->conf.ivlen = crypto_blkcipher_ivsize(tfm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | esp->conf.padlen = 0; |
| 395 | if (esp->conf.ivlen) { |
| 396 | esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL); |
| 397 | if (unlikely(esp->conf.ivec == NULL)) |
| 398 | goto error; |
David S. Miller | e4bec82 | 2006-09-22 15:17:35 -0700 | [diff] [blame] | 399 | esp->conf.ivinitted = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 401 | if (crypto_blkcipher_setkey(tfm, esp->conf.key, esp->conf.key_len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | goto error; |
| 403 | x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen; |
Masahide NAKAMURA | 7e49e6d | 2006-09-22 15:05:15 -0700 | [diff] [blame] | 404 | if (x->props.mode == XFRM_MODE_TUNNEL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | x->props.header_len += sizeof(struct iphdr); |
| 406 | if (x->encap) { |
| 407 | struct xfrm_encap_tmpl *encap = x->encap; |
| 408 | |
| 409 | switch (encap->encap_type) { |
| 410 | default: |
| 411 | goto error; |
| 412 | case UDP_ENCAP_ESPINUDP: |
| 413 | x->props.header_len += sizeof(struct udphdr); |
| 414 | break; |
| 415 | case UDP_ENCAP_ESPINUDP_NON_IKE: |
| 416 | x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32); |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | x->data = esp; |
| 421 | x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len; |
| 422 | return 0; |
| 423 | |
| 424 | error: |
| 425 | x->data = esp; |
| 426 | esp_destroy(x); |
| 427 | x->data = NULL; |
| 428 | return -EINVAL; |
| 429 | } |
| 430 | |
| 431 | static struct xfrm_type esp_type = |
| 432 | { |
| 433 | .description = "ESP4", |
| 434 | .owner = THIS_MODULE, |
| 435 | .proto = IPPROTO_ESP, |
| 436 | .init_state = esp_init_state, |
| 437 | .destructor = esp_destroy, |
| 438 | .get_max_size = esp4_get_max_size, |
| 439 | .input = esp_input, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | .output = esp_output |
| 441 | }; |
| 442 | |
| 443 | static struct net_protocol esp4_protocol = { |
| 444 | .handler = xfrm4_rcv, |
| 445 | .err_handler = esp4_err, |
| 446 | .no_policy = 1, |
| 447 | }; |
| 448 | |
| 449 | static int __init esp4_init(void) |
| 450 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | if (xfrm_register_type(&esp_type, AF_INET) < 0) { |
| 452 | printk(KERN_INFO "ip esp init: can't add xfrm type\n"); |
| 453 | return -EAGAIN; |
| 454 | } |
| 455 | if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) { |
| 456 | printk(KERN_INFO "ip esp init: can't add protocol\n"); |
| 457 | xfrm_unregister_type(&esp_type, AF_INET); |
| 458 | return -EAGAIN; |
| 459 | } |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static void __exit esp4_fini(void) |
| 464 | { |
| 465 | if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0) |
| 466 | printk(KERN_INFO "ip esp close: can't remove protocol\n"); |
| 467 | if (xfrm_unregister_type(&esp_type, AF_INET) < 0) |
| 468 | printk(KERN_INFO "ip esp close: can't remove xfrm type\n"); |
| 469 | } |
| 470 | |
| 471 | module_init(esp4_init); |
| 472 | module_exit(esp4_fini); |
| 473 | MODULE_LICENSE("GPL"); |