esp4.c 12 KB

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