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 <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. u8 *tail;
  24. int blksize;
  25. int clen;
  26. int alen;
  27. int nfrags;
  28. /* Strip IP+ESP header. */
  29. __skb_pull(skb, skb_transport_offset(skb));
  30. /* Now skb is pure payload to encrypt */
  31. err = -ENOMEM;
  32. /* Round to block size */
  33. clen = skb->len;
  34. esp = x->data;
  35. alen = esp->auth.icv_trunc_len;
  36. tfm = esp->conf.tfm;
  37. desc.tfm = tfm;
  38. desc.flags = 0;
  39. blksize = ALIGN(crypto_blkcipher_blocksize(tfm), 4);
  40. clen = ALIGN(clen + 2, blksize);
  41. if (esp->conf.padlen)
  42. clen = ALIGN(clen, esp->conf.padlen);
  43. if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0)
  44. goto error;
  45. /* Fill padding... */
  46. tail = skb_tail_pointer(trailer);
  47. do {
  48. int i;
  49. for (i=0; i<clen-skb->len - 2; i++)
  50. tail[i] = i + 1;
  51. } while (0);
  52. tail[clen - skb->len - 2] = (clen - skb->len) - 2;
  53. pskb_put(skb, trailer, clen - skb->len);
  54. __skb_push(skb, skb->data - skb_network_header(skb));
  55. top_iph = ip_hdr(skb);
  56. esph = (struct ip_esp_hdr *)(skb_network_header(skb) +
  57. top_iph->ihl * 4);
  58. top_iph->tot_len = htons(skb->len + alen);
  59. *(skb_tail_pointer(trailer) - 1) = top_iph->protocol;
  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(++x->replay.oseq);
  86. xfrm_aevent_doreplay(x);
  87. if (esp->conf.ivlen) {
  88. if (unlikely(!esp->conf.ivinitted)) {
  89. get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
  90. esp->conf.ivinitted = 1;
  91. }
  92. crypto_blkcipher_set_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
  93. }
  94. do {
  95. struct scatterlist *sg = &esp->sgbuf[0];
  96. if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
  97. sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
  98. if (!sg)
  99. goto error;
  100. }
  101. skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
  102. err = crypto_blkcipher_encrypt(&desc, sg, sg, clen);
  103. if (unlikely(sg != &esp->sgbuf[0]))
  104. kfree(sg);
  105. } while (0);
  106. if (unlikely(err))
  107. goto error;
  108. if (esp->conf.ivlen) {
  109. memcpy(esph->enc_data, esp->conf.ivec, esp->conf.ivlen);
  110. crypto_blkcipher_get_iv(tfm, esp->conf.ivec, esp->conf.ivlen);
  111. }
  112. if (esp->auth.icv_full_len) {
  113. err = esp_mac_digest(esp, skb, (u8 *)esph - skb->data,
  114. sizeof(*esph) + esp->conf.ivlen + clen);
  115. memcpy(pskb_put(skb, trailer, alen), esp->auth.work_icv, alen);
  116. }
  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. x->props.mode == XFRM_MODE_BEET)
  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. /* null auth and encryption can have zero length keys */
  289. if (x->aalg) {
  290. if (x->aalg->alg_key_len > 512)
  291. goto error;
  292. }
  293. if (x->ealg == NULL)
  294. goto error;
  295. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  296. if (esp == NULL)
  297. return -ENOMEM;
  298. if (x->aalg) {
  299. struct xfrm_algo_desc *aalg_desc;
  300. struct crypto_hash *hash;
  301. esp->auth.key = x->aalg->alg_key;
  302. esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
  303. hash = crypto_alloc_hash(x->aalg->alg_name, 0,
  304. CRYPTO_ALG_ASYNC);
  305. if (IS_ERR(hash))
  306. goto error;
  307. esp->auth.tfm = hash;
  308. if (crypto_hash_setkey(hash, esp->auth.key, esp->auth.key_len))
  309. goto error;
  310. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  311. BUG_ON(!aalg_desc);
  312. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  313. crypto_hash_digestsize(hash)) {
  314. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  315. x->aalg->alg_name,
  316. crypto_hash_digestsize(hash),
  317. aalg_desc->uinfo.auth.icv_fullbits/8);
  318. goto error;
  319. }
  320. esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  321. esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
  322. esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
  323. if (!esp->auth.work_icv)
  324. goto error;
  325. }
  326. esp->conf.key = x->ealg->alg_key;
  327. esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
  328. tfm = crypto_alloc_blkcipher(x->ealg->alg_name, 0, CRYPTO_ALG_ASYNC);
  329. if (IS_ERR(tfm))
  330. goto error;
  331. esp->conf.tfm = tfm;
  332. esp->conf.ivlen = crypto_blkcipher_ivsize(tfm);
  333. esp->conf.padlen = 0;
  334. if (esp->conf.ivlen) {
  335. esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
  336. if (unlikely(esp->conf.ivec == NULL))
  337. goto error;
  338. esp->conf.ivinitted = 0;
  339. }
  340. if (crypto_blkcipher_setkey(tfm, esp->conf.key, esp->conf.key_len))
  341. goto error;
  342. x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
  343. if (x->props.mode == XFRM_MODE_TUNNEL)
  344. x->props.header_len += sizeof(struct iphdr);
  345. else if (x->props.mode == XFRM_MODE_BEET)
  346. x->props.header_len += IPV4_BEET_PHMAXLEN;
  347. if (x->encap) {
  348. struct xfrm_encap_tmpl *encap = x->encap;
  349. switch (encap->encap_type) {
  350. default:
  351. goto error;
  352. case UDP_ENCAP_ESPINUDP:
  353. x->props.header_len += sizeof(struct udphdr);
  354. break;
  355. case UDP_ENCAP_ESPINUDP_NON_IKE:
  356. x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
  357. break;
  358. }
  359. }
  360. x->data = esp;
  361. align = ALIGN(crypto_blkcipher_blocksize(esp->conf.tfm), 4);
  362. if (esp->conf.padlen)
  363. align = max_t(u32, align, esp->conf.padlen);
  364. x->props.trailer_len = align + 1 + esp->auth.icv_trunc_len;
  365. return 0;
  366. error:
  367. x->data = esp;
  368. esp_destroy(x);
  369. x->data = NULL;
  370. return -EINVAL;
  371. }
  372. static struct xfrm_type esp_type =
  373. {
  374. .description = "ESP4",
  375. .owner = THIS_MODULE,
  376. .proto = IPPROTO_ESP,
  377. .init_state = esp_init_state,
  378. .destructor = esp_destroy,
  379. .get_mtu = esp4_get_mtu,
  380. .input = esp_input,
  381. .output = esp_output
  382. };
  383. static struct net_protocol esp4_protocol = {
  384. .handler = xfrm4_rcv,
  385. .err_handler = esp4_err,
  386. .no_policy = 1,
  387. };
  388. static int __init esp4_init(void)
  389. {
  390. if (xfrm_register_type(&esp_type, AF_INET) < 0) {
  391. printk(KERN_INFO "ip esp init: can't add xfrm type\n");
  392. return -EAGAIN;
  393. }
  394. if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
  395. printk(KERN_INFO "ip esp init: can't add protocol\n");
  396. xfrm_unregister_type(&esp_type, AF_INET);
  397. return -EAGAIN;
  398. }
  399. return 0;
  400. }
  401. static void __exit esp4_fini(void)
  402. {
  403. if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
  404. printk(KERN_INFO "ip esp close: can't remove protocol\n");
  405. if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
  406. printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
  407. }
  408. module_init(esp4_init);
  409. module_exit(esp4_fini);
  410. MODULE_LICENSE("GPL");
  411. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);