esp4.c 11 KB

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