esp4.c 12 KB

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