esp4.c 11 KB

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