esp4.c 12 KB

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