1 #include <linux/module.h> 2 #include <net/ip.h> 3 #include <net/xfrm.h> 4 #include <net/esp.h> 5 #include <asm/scatterlist.h> 6 #include <linux/crypto.h> 7 #include <linux/kernel.h> 8 #include <linux/pfkeyv2.h> 9 #include <linux/random.h> 10 #include <net/icmp.h> 11 #include <net/protocol.h> 12 #include <net/udp.h> 13 14 static int esp_output(struct xfrm_state *x, struct sk_buff *skb) 15 { 16 int err; 17 struct iphdr *top_iph; 18 struct ip_esp_hdr *esph; 19 struct crypto_tfm *tfm; 20 struct esp_data *esp; 21 struct sk_buff *trailer; 22 int blksize; 23 int clen; 24 int alen; 25 int nfrags; 26 27 /* Strip IP+ESP header. */ 28 __skb_pull(skb, skb->h.raw - skb->data); 29 /* Now skb is pure payload to encrypt */ 30 31 err = -ENOMEM; 32 33 /* Round to block size */ 34 clen = skb->len; 35 36 esp = x->data; 37 alen = esp->auth.icv_trunc_len; 38 tfm = esp->conf.tfm; 39 blksize = ALIGN(crypto_tfm_alg_blocksize(tfm), 4); 40 clen = ALIGN(clen + 2, blksize); 41 if (esp->conf.padlen) 42 clen = ALIGN(clen, esp->conf.padlen); 43 44 if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0) 45 goto error; 46 47 /* Fill padding... */ 48 do { 49 int i; 50 for (i=0; i<clen-skb->len - 2; i++) 51 *(u8*)(trailer->tail + i) = i+1; 52 } while (0); 53 *(u8*)(trailer->tail + clen-skb->len - 2) = (clen - skb->len)-2; 54 pskb_put(skb, trailer, clen - skb->len); 55 56 __skb_push(skb, skb->data - skb->nh.raw); 57 top_iph = skb->nh.iph; 58 esph = (struct ip_esp_hdr *)(skb->nh.raw + top_iph->ihl*4); 59 top_iph->tot_len = htons(skb->len + alen); 60 *(u8*)(trailer->tail - 1) = top_iph->protocol; 61 62 /* this is non-NULL only with UDP Encapsulation */ 63 if (x->encap) { 64 struct xfrm_encap_tmpl *encap = x->encap; 65 struct udphdr *uh; 66 u32 *udpdata32; 67 68 uh = (struct udphdr *)esph; 69 uh->source = encap->encap_sport; 70 uh->dest = encap->encap_dport; 71 uh->len = htons(skb->len + alen - top_iph->ihl*4); 72 uh->check = 0; 73 74 switch (encap->encap_type) { 75 default: 76 case UDP_ENCAP_ESPINUDP: 77 esph = (struct ip_esp_hdr *)(uh + 1); 78 break; 79 case UDP_ENCAP_ESPINUDP_NON_IKE: 80 udpdata32 = (u32 *)(uh + 1); 81 udpdata32[0] = udpdata32[1] = 0; 82 esph = (struct ip_esp_hdr *)(udpdata32 + 2); 83 break; 84 } 85 86 top_iph->protocol = IPPROTO_UDP; 87 } else 88 top_iph->protocol = IPPROTO_ESP; 89 90 esph->spi = x->id.spi; 91 esph->seq_no = htonl(++x->replay.oseq); 92 xfrm_aevent_doreplay(x); 93 94 if (esp->conf.ivlen) 95 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm)); 96 97 do { 98 struct scatterlist *sg = &esp->sgbuf[0]; 99 100 if (unlikely(nfrags > ESP_NUM_FAST_SG)) { 101 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC); 102 if (!sg) 103 goto error; 104 } 105 skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen); 106 crypto_cipher_encrypt(tfm, sg, sg, clen); 107 if (unlikely(sg != &esp->sgbuf[0])) 108 kfree(sg); 109 } while (0); 110 111 if (esp->conf.ivlen) { 112 memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm)); 113 crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm)); 114 } 115 116 if (esp->auth.icv_full_len) { 117 esp->auth.icv(esp, skb, (u8*)esph-skb->data, 118 sizeof(struct ip_esp_hdr) + esp->conf.ivlen+clen, trailer->tail); 119 pskb_put(skb, trailer, alen); 120 } 121 122 ip_send_check(top_iph); 123 124 err = 0; 125 126 error: 127 return err; 128 } 129 130 /* 131 * Note: detecting truncated vs. non-truncated authentication data is very 132 * expensive, so we only support truncated data, which is the recommended 133 * and common case. 134 */ 135 static int esp_input(struct xfrm_state *x, struct sk_buff *skb) 136 { 137 struct iphdr *iph; 138 struct ip_esp_hdr *esph; 139 struct esp_data *esp = x->data; 140 struct sk_buff *trailer; 141 int blksize = ALIGN(crypto_tfm_alg_blocksize(esp->conf.tfm), 4); 142 int alen = esp->auth.icv_trunc_len; 143 int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen; 144 int nfrags; 145 int ihl; 146 u8 nexthdr[2]; 147 struct scatterlist *sg; 148 int padlen; 149 150 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr))) 151 goto out; 152 153 if (elen <= 0 || (elen & (blksize-1))) 154 goto out; 155 156 /* If integrity check is required, do this. */ 157 if (esp->auth.icv_full_len) { 158 u8 sum[esp->auth.icv_full_len]; 159 u8 sum1[alen]; 160 161 esp->auth.icv(esp, skb, 0, skb->len-alen, sum); 162 163 if (skb_copy_bits(skb, skb->len-alen, sum1, alen)) 164 BUG(); 165 166 if (unlikely(memcmp(sum, sum1, alen))) { 167 x->stats.integrity_failed++; 168 goto out; 169 } 170 } 171 172 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) 173 goto out; 174 175 skb->ip_summed = CHECKSUM_NONE; 176 177 esph = (struct ip_esp_hdr*)skb->data; 178 179 /* Get ivec. This can be wrong, check against another impls. */ 180 if (esp->conf.ivlen) 181 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm)); 182 183 sg = &esp->sgbuf[0]; 184 185 if (unlikely(nfrags > ESP_NUM_FAST_SG)) { 186 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC); 187 if (!sg) 188 goto out; 189 } 190 skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen); 191 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen); 192 if (unlikely(sg != &esp->sgbuf[0])) 193 kfree(sg); 194 195 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2)) 196 BUG(); 197 198 padlen = nexthdr[0]; 199 if (padlen+2 >= elen) 200 goto out; 201 202 /* ... check padding bits here. Silly. :-) */ 203 204 iph = skb->nh.iph; 205 ihl = iph->ihl * 4; 206 207 if (x->encap) { 208 struct xfrm_encap_tmpl *encap = x->encap; 209 struct udphdr *uh = (void *)(skb->nh.raw + ihl); 210 211 /* 212 * 1) if the NAT-T peer's IP or port changed then 213 * advertize the change to the keying daemon. 214 * This is an inbound SA, so just compare 215 * SRC ports. 216 */ 217 if (iph->saddr != x->props.saddr.a4 || 218 uh->source != encap->encap_sport) { 219 xfrm_address_t ipaddr; 220 221 ipaddr.a4 = iph->saddr; 222 km_new_mapping(x, &ipaddr, uh->source); 223 224 /* XXX: perhaps add an extra 225 * policy check here, to see 226 * if we should allow or 227 * reject a packet from a 228 * different source 229 * address/port. 230 */ 231 } 232 233 /* 234 * 2) ignore UDP/TCP checksums in case 235 * of NAT-T in Transport Mode, or 236 * perform other post-processing fixes 237 * as per draft-ietf-ipsec-udp-encaps-06, 238 * section 3.1.2 239 */ 240 if (!x->props.mode) 241 skb->ip_summed = CHECKSUM_UNNECESSARY; 242 } 243 244 iph->protocol = nexthdr[1]; 245 pskb_trim(skb, skb->len - alen - padlen - 2); 246 skb->h.raw = __skb_pull(skb, sizeof(*esph) + esp->conf.ivlen) - ihl; 247 248 return 0; 249 250 out: 251 return -EINVAL; 252 } 253 254 static u32 esp4_get_max_size(struct xfrm_state *x, int mtu) 255 { 256 struct esp_data *esp = x->data; 257 u32 blksize = ALIGN(crypto_tfm_alg_blocksize(esp->conf.tfm), 4); 258 259 if (x->props.mode) { 260 mtu = ALIGN(mtu + 2, blksize); 261 } else { 262 /* The worst case. */ 263 mtu = ALIGN(mtu + 2, 4) + blksize - 4; 264 } 265 if (esp->conf.padlen) 266 mtu = ALIGN(mtu, esp->conf.padlen); 267 268 return mtu + x->props.header_len + esp->auth.icv_trunc_len; 269 } 270 271 static void esp4_err(struct sk_buff *skb, u32 info) 272 { 273 struct iphdr *iph = (struct iphdr*)skb->data; 274 struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2)); 275 struct xfrm_state *x; 276 277 if (skb->h.icmph->type != ICMP_DEST_UNREACH || 278 skb->h.icmph->code != ICMP_FRAG_NEEDED) 279 return; 280 281 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET); 282 if (!x) 283 return; 284 NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n", 285 ntohl(esph->spi), ntohl(iph->daddr)); 286 xfrm_state_put(x); 287 } 288 289 static void esp_destroy(struct xfrm_state *x) 290 { 291 struct esp_data *esp = x->data; 292 293 if (!esp) 294 return; 295 296 crypto_free_tfm(esp->conf.tfm); 297 esp->conf.tfm = NULL; 298 kfree(esp->conf.ivec); 299 esp->conf.ivec = NULL; 300 crypto_free_tfm(esp->auth.tfm); 301 esp->auth.tfm = NULL; 302 kfree(esp->auth.work_icv); 303 esp->auth.work_icv = NULL; 304 kfree(esp); 305 } 306 307 static int esp_init_state(struct xfrm_state *x) 308 { 309 struct esp_data *esp = NULL; 310 311 /* null auth and encryption can have zero length keys */ 312 if (x->aalg) { 313 if (x->aalg->alg_key_len > 512) 314 goto error; 315 } 316 if (x->ealg == NULL) 317 goto error; 318 319 esp = kzalloc(sizeof(*esp), GFP_KERNEL); 320 if (esp == NULL) 321 return -ENOMEM; 322 323 if (x->aalg) { 324 struct xfrm_algo_desc *aalg_desc; 325 326 esp->auth.key = x->aalg->alg_key; 327 esp->auth.key_len = (x->aalg->alg_key_len+7)/8; 328 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0); 329 if (esp->auth.tfm == NULL) 330 goto error; 331 esp->auth.icv = esp_hmac_digest; 332 333 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0); 334 BUG_ON(!aalg_desc); 335 336 if (aalg_desc->uinfo.auth.icv_fullbits/8 != 337 crypto_tfm_alg_digestsize(esp->auth.tfm)) { 338 NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n", 339 x->aalg->alg_name, 340 crypto_tfm_alg_digestsize(esp->auth.tfm), 341 aalg_desc->uinfo.auth.icv_fullbits/8); 342 goto error; 343 } 344 345 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8; 346 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8; 347 348 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL); 349 if (!esp->auth.work_icv) 350 goto error; 351 } 352 esp->conf.key = x->ealg->alg_key; 353 esp->conf.key_len = (x->ealg->alg_key_len+7)/8; 354 if (x->props.ealgo == SADB_EALG_NULL) 355 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB); 356 else 357 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC); 358 if (esp->conf.tfm == NULL) 359 goto error; 360 esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm); 361 esp->conf.padlen = 0; 362 if (esp->conf.ivlen) { 363 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL); 364 if (unlikely(esp->conf.ivec == NULL)) 365 goto error; 366 get_random_bytes(esp->conf.ivec, esp->conf.ivlen); 367 } 368 if (crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len)) 369 goto error; 370 x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen; 371 if (x->props.mode) 372 x->props.header_len += sizeof(struct iphdr); 373 if (x->encap) { 374 struct xfrm_encap_tmpl *encap = x->encap; 375 376 switch (encap->encap_type) { 377 default: 378 goto error; 379 case UDP_ENCAP_ESPINUDP: 380 x->props.header_len += sizeof(struct udphdr); 381 break; 382 case UDP_ENCAP_ESPINUDP_NON_IKE: 383 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32); 384 break; 385 } 386 } 387 x->data = esp; 388 x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len; 389 return 0; 390 391 error: 392 x->data = esp; 393 esp_destroy(x); 394 x->data = NULL; 395 return -EINVAL; 396 } 397 398 static struct xfrm_type esp_type = 399 { 400 .description = "ESP4", 401 .owner = THIS_MODULE, 402 .proto = IPPROTO_ESP, 403 .init_state = esp_init_state, 404 .destructor = esp_destroy, 405 .get_max_size = esp4_get_max_size, 406 .input = esp_input, 407 .output = esp_output 408 }; 409 410 static struct net_protocol esp4_protocol = { 411 .handler = xfrm4_rcv, 412 .err_handler = esp4_err, 413 .no_policy = 1, 414 }; 415 416 static int __init esp4_init(void) 417 { 418 if (xfrm_register_type(&esp_type, AF_INET) < 0) { 419 printk(KERN_INFO "ip esp init: can't add xfrm type\n"); 420 return -EAGAIN; 421 } 422 if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) { 423 printk(KERN_INFO "ip esp init: can't add protocol\n"); 424 xfrm_unregister_type(&esp_type, AF_INET); 425 return -EAGAIN; 426 } 427 return 0; 428 } 429 430 static void __exit esp4_fini(void) 431 { 432 if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0) 433 printk(KERN_INFO "ip esp close: can't remove protocol\n"); 434 if (xfrm_unregister_type(&esp_type, AF_INET) < 0) 435 printk(KERN_INFO "ip esp close: can't remove xfrm type\n"); 436 } 437 438 module_init(esp4_init); 439 module_exit(esp4_fini); 440 MODULE_LICENSE("GPL"); 441