esp4.c 12 KB

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