esp4.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. #include <crypto/aead.h>
  2. #include <crypto/authenc.h>
  3. #include <linux/err.h>
  4. #include <linux/module.h>
  5. #include <net/ip.h>
  6. #include <net/xfrm.h>
  7. #include <net/esp.h>
  8. #include <linux/scatterlist.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pfkeyv2.h>
  11. #include <linux/rtnetlink.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/in6.h>
  15. #include <net/icmp.h>
  16. #include <net/protocol.h>
  17. #include <net/udp.h>
  18. struct esp_skb_cb {
  19. struct xfrm_skb_cb xfrm;
  20. void *tmp;
  21. };
  22. #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
  23. /*
  24. * Allocate an AEAD request structure with extra space for SG and IV.
  25. *
  26. * For alignment considerations the IV is placed at the front, followed
  27. * by the request and finally the SG list.
  28. *
  29. * TODO: Use spare space in skb for this where possible.
  30. */
  31. static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags)
  32. {
  33. unsigned int len;
  34. len = crypto_aead_ivsize(aead);
  35. if (len) {
  36. len += crypto_aead_alignmask(aead) &
  37. ~(crypto_tfm_ctx_alignment() - 1);
  38. len = ALIGN(len, crypto_tfm_ctx_alignment());
  39. }
  40. len += sizeof(struct aead_givcrypt_request) + crypto_aead_reqsize(aead);
  41. len = ALIGN(len, __alignof__(struct scatterlist));
  42. len += sizeof(struct scatterlist) * nfrags;
  43. return kmalloc(len, GFP_ATOMIC);
  44. }
  45. static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp)
  46. {
  47. return crypto_aead_ivsize(aead) ?
  48. PTR_ALIGN((u8 *)tmp, crypto_aead_alignmask(aead) + 1) : tmp;
  49. }
  50. static inline struct aead_givcrypt_request *esp_tmp_givreq(
  51. struct crypto_aead *aead, u8 *iv)
  52. {
  53. struct aead_givcrypt_request *req;
  54. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  55. crypto_tfm_ctx_alignment());
  56. aead_givcrypt_set_tfm(req, aead);
  57. return req;
  58. }
  59. static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
  60. {
  61. struct aead_request *req;
  62. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  63. crypto_tfm_ctx_alignment());
  64. aead_request_set_tfm(req, aead);
  65. return req;
  66. }
  67. static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
  68. struct aead_request *req)
  69. {
  70. return (void *)ALIGN((unsigned long)(req + 1) +
  71. crypto_aead_reqsize(aead),
  72. __alignof__(struct scatterlist));
  73. }
  74. static inline struct scatterlist *esp_givreq_sg(
  75. struct crypto_aead *aead, struct aead_givcrypt_request *req)
  76. {
  77. return (void *)ALIGN((unsigned long)(req + 1) +
  78. crypto_aead_reqsize(aead),
  79. __alignof__(struct scatterlist));
  80. }
  81. static void esp_output_done(struct crypto_async_request *base, int err)
  82. {
  83. struct sk_buff *skb = base->data;
  84. kfree(ESP_SKB_CB(skb)->tmp);
  85. xfrm_output_resume(skb, err);
  86. }
  87. static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
  88. {
  89. int err;
  90. struct ip_esp_hdr *esph;
  91. struct crypto_aead *aead;
  92. struct aead_givcrypt_request *req;
  93. struct scatterlist *sg;
  94. struct scatterlist *asg;
  95. struct esp_data *esp;
  96. struct sk_buff *trailer;
  97. void *tmp;
  98. u8 *iv;
  99. u8 *tail;
  100. int blksize;
  101. int clen;
  102. int alen;
  103. int nfrags;
  104. /* skb is pure payload to encrypt */
  105. err = -ENOMEM;
  106. /* Round to block size */
  107. clen = skb->len;
  108. esp = x->data;
  109. aead = esp->aead;
  110. alen = crypto_aead_authsize(aead);
  111. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  112. clen = ALIGN(clen + 2, blksize);
  113. if (esp->padlen)
  114. clen = ALIGN(clen, esp->padlen);
  115. if ((err = skb_cow_data(skb, clen - skb->len + alen, &trailer)) < 0)
  116. goto error;
  117. nfrags = err;
  118. tmp = esp_alloc_tmp(aead, nfrags + 1);
  119. if (!tmp)
  120. goto error;
  121. iv = esp_tmp_iv(aead, tmp);
  122. req = esp_tmp_givreq(aead, iv);
  123. asg = esp_givreq_sg(aead, req);
  124. sg = asg + 1;
  125. /* Fill padding... */
  126. tail = skb_tail_pointer(trailer);
  127. do {
  128. int i;
  129. for (i=0; i<clen-skb->len - 2; i++)
  130. tail[i] = i + 1;
  131. } while (0);
  132. tail[clen - skb->len - 2] = (clen - skb->len) - 2;
  133. tail[clen - skb->len - 1] = *skb_mac_header(skb);
  134. pskb_put(skb, trailer, clen - skb->len + alen);
  135. skb_push(skb, -skb_network_offset(skb));
  136. esph = ip_esp_hdr(skb);
  137. *skb_mac_header(skb) = IPPROTO_ESP;
  138. /* this is non-NULL only with UDP Encapsulation */
  139. if (x->encap) {
  140. struct xfrm_encap_tmpl *encap = x->encap;
  141. struct udphdr *uh;
  142. __be32 *udpdata32;
  143. unsigned int sport, dport;
  144. int encap_type;
  145. spin_lock_bh(&x->lock);
  146. sport = encap->encap_sport;
  147. dport = encap->encap_dport;
  148. encap_type = encap->encap_type;
  149. spin_unlock_bh(&x->lock);
  150. uh = (struct udphdr *)esph;
  151. uh->source = sport;
  152. uh->dest = dport;
  153. uh->len = htons(skb->len - skb_transport_offset(skb));
  154. uh->check = 0;
  155. switch (encap_type) {
  156. default:
  157. case UDP_ENCAP_ESPINUDP:
  158. esph = (struct ip_esp_hdr *)(uh + 1);
  159. break;
  160. case UDP_ENCAP_ESPINUDP_NON_IKE:
  161. udpdata32 = (__be32 *)(uh + 1);
  162. udpdata32[0] = udpdata32[1] = 0;
  163. esph = (struct ip_esp_hdr *)(udpdata32 + 2);
  164. break;
  165. }
  166. *skb_mac_header(skb) = IPPROTO_UDP;
  167. }
  168. esph->spi = x->id.spi;
  169. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
  170. sg_init_table(sg, nfrags);
  171. skb_to_sgvec(skb, sg,
  172. esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
  173. clen + alen);
  174. sg_init_one(asg, esph, sizeof(*esph));
  175. aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
  176. aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
  177. aead_givcrypt_set_assoc(req, asg, sizeof(*esph));
  178. aead_givcrypt_set_giv(req, esph->enc_data, XFRM_SKB_CB(skb)->seq);
  179. ESP_SKB_CB(skb)->tmp = tmp;
  180. err = crypto_aead_givencrypt(req);
  181. if (err == -EINPROGRESS)
  182. goto error;
  183. if (err == -EBUSY)
  184. err = NET_XMIT_DROP;
  185. kfree(tmp);
  186. error:
  187. return err;
  188. }
  189. static int esp_input_done2(struct sk_buff *skb, int err)
  190. {
  191. struct iphdr *iph;
  192. struct xfrm_state *x = xfrm_input_state(skb);
  193. struct esp_data *esp = x->data;
  194. struct crypto_aead *aead = esp->aead;
  195. int alen = crypto_aead_authsize(aead);
  196. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  197. int elen = skb->len - hlen;
  198. int ihl;
  199. u8 nexthdr[2];
  200. int padlen;
  201. kfree(ESP_SKB_CB(skb)->tmp);
  202. if (unlikely(err))
  203. goto out;
  204. if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
  205. BUG();
  206. err = -EINVAL;
  207. padlen = nexthdr[0];
  208. if (padlen + 2 + alen >= elen)
  209. goto out;
  210. /* ... check padding bits here. Silly. :-) */
  211. iph = ip_hdr(skb);
  212. ihl = iph->ihl * 4;
  213. if (x->encap) {
  214. struct xfrm_encap_tmpl *encap = x->encap;
  215. struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
  216. /*
  217. * 1) if the NAT-T peer's IP or port changed then
  218. * advertize the change to the keying daemon.
  219. * This is an inbound SA, so just compare
  220. * SRC ports.
  221. */
  222. if (iph->saddr != x->props.saddr.a4 ||
  223. uh->source != encap->encap_sport) {
  224. xfrm_address_t ipaddr;
  225. ipaddr.a4 = iph->saddr;
  226. km_new_mapping(x, &ipaddr, uh->source);
  227. /* XXX: perhaps add an extra
  228. * policy check here, to see
  229. * if we should allow or
  230. * reject a packet from a
  231. * different source
  232. * address/port.
  233. */
  234. }
  235. /*
  236. * 2) ignore UDP/TCP checksums in case
  237. * of NAT-T in Transport Mode, or
  238. * perform other post-processing fixes
  239. * as per draft-ietf-ipsec-udp-encaps-06,
  240. * section 3.1.2
  241. */
  242. if (x->props.mode == XFRM_MODE_TRANSPORT)
  243. skb->ip_summed = CHECKSUM_UNNECESSARY;
  244. }
  245. pskb_trim(skb, skb->len - alen - padlen - 2);
  246. __skb_pull(skb, hlen);
  247. skb_set_transport_header(skb, -ihl);
  248. err = nexthdr[1];
  249. /* RFC4303: Drop dummy packets without any error */
  250. if (err == IPPROTO_NONE)
  251. err = -EINVAL;
  252. out:
  253. return err;
  254. }
  255. static void esp_input_done(struct crypto_async_request *base, int err)
  256. {
  257. struct sk_buff *skb = base->data;
  258. xfrm_input_resume(skb, esp_input_done2(skb, err));
  259. }
  260. /*
  261. * Note: detecting truncated vs. non-truncated authentication data is very
  262. * expensive, so we only support truncated data, which is the recommended
  263. * and common case.
  264. */
  265. static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
  266. {
  267. struct ip_esp_hdr *esph;
  268. struct esp_data *esp = x->data;
  269. struct crypto_aead *aead = esp->aead;
  270. struct aead_request *req;
  271. struct sk_buff *trailer;
  272. int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
  273. int nfrags;
  274. void *tmp;
  275. u8 *iv;
  276. struct scatterlist *sg;
  277. struct scatterlist *asg;
  278. int err = -EINVAL;
  279. if (!pskb_may_pull(skb, sizeof(*esph)))
  280. goto out;
  281. if (elen <= 0)
  282. goto out;
  283. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  284. goto out;
  285. nfrags = err;
  286. err = -ENOMEM;
  287. tmp = esp_alloc_tmp(aead, nfrags + 1);
  288. if (!tmp)
  289. goto out;
  290. ESP_SKB_CB(skb)->tmp = tmp;
  291. iv = esp_tmp_iv(aead, tmp);
  292. req = esp_tmp_req(aead, iv);
  293. asg = esp_req_sg(aead, req);
  294. sg = asg + 1;
  295. skb->ip_summed = CHECKSUM_NONE;
  296. esph = (struct ip_esp_hdr *)skb->data;
  297. /* Get ivec. This can be wrong, check against another impls. */
  298. iv = esph->enc_data;
  299. sg_init_table(sg, nfrags);
  300. skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
  301. sg_init_one(asg, esph, sizeof(*esph));
  302. aead_request_set_callback(req, 0, esp_input_done, skb);
  303. aead_request_set_crypt(req, sg, sg, elen, iv);
  304. aead_request_set_assoc(req, asg, sizeof(*esph));
  305. err = crypto_aead_decrypt(req);
  306. if (err == -EINPROGRESS)
  307. goto out;
  308. err = esp_input_done2(skb, err);
  309. out:
  310. return err;
  311. }
  312. static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
  313. {
  314. struct esp_data *esp = x->data;
  315. u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
  316. u32 align = max_t(u32, blksize, esp->padlen);
  317. u32 rem;
  318. mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
  319. rem = mtu & (align - 1);
  320. mtu &= ~(align - 1);
  321. switch (x->props.mode) {
  322. case XFRM_MODE_TUNNEL:
  323. break;
  324. default:
  325. case XFRM_MODE_TRANSPORT:
  326. /* The worst case */
  327. mtu -= blksize - 4;
  328. mtu += min_t(u32, blksize - 4, rem);
  329. break;
  330. case XFRM_MODE_BEET:
  331. /* The worst case. */
  332. mtu += min_t(u32, IPV4_BEET_PHMAXLEN, rem);
  333. break;
  334. }
  335. return mtu - 2;
  336. }
  337. static void esp4_err(struct sk_buff *skb, u32 info)
  338. {
  339. struct iphdr *iph = (struct iphdr*)skb->data;
  340. struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
  341. struct xfrm_state *x;
  342. if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
  343. icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  344. return;
  345. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
  346. if (!x)
  347. return;
  348. NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
  349. ntohl(esph->spi), ntohl(iph->daddr));
  350. xfrm_state_put(x);
  351. }
  352. static void esp_destroy(struct xfrm_state *x)
  353. {
  354. struct esp_data *esp = x->data;
  355. if (!esp)
  356. return;
  357. crypto_free_aead(esp->aead);
  358. kfree(esp);
  359. }
  360. static int esp_init_aead(struct xfrm_state *x)
  361. {
  362. struct esp_data *esp = x->data;
  363. struct crypto_aead *aead;
  364. int err;
  365. aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
  366. err = PTR_ERR(aead);
  367. if (IS_ERR(aead))
  368. goto error;
  369. esp->aead = aead;
  370. err = crypto_aead_setkey(aead, x->aead->alg_key,
  371. (x->aead->alg_key_len + 7) / 8);
  372. if (err)
  373. goto error;
  374. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  375. if (err)
  376. goto error;
  377. error:
  378. return err;
  379. }
  380. static int esp_init_authenc(struct xfrm_state *x)
  381. {
  382. struct esp_data *esp = x->data;
  383. struct crypto_aead *aead;
  384. struct crypto_authenc_key_param *param;
  385. struct rtattr *rta;
  386. char *key;
  387. char *p;
  388. char authenc_name[CRYPTO_MAX_ALG_NAME];
  389. unsigned int keylen;
  390. int err;
  391. err = -EINVAL;
  392. if (x->ealg == NULL)
  393. goto error;
  394. err = -ENAMETOOLONG;
  395. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
  396. x->aalg ? x->aalg->alg_name : "digest_null",
  397. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  398. goto error;
  399. aead = crypto_alloc_aead(authenc_name, 0, 0);
  400. err = PTR_ERR(aead);
  401. if (IS_ERR(aead))
  402. goto error;
  403. esp->aead = aead;
  404. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  405. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  406. err = -ENOMEM;
  407. key = kmalloc(keylen, GFP_KERNEL);
  408. if (!key)
  409. goto error;
  410. p = key;
  411. rta = (void *)p;
  412. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  413. rta->rta_len = RTA_LENGTH(sizeof(*param));
  414. param = RTA_DATA(rta);
  415. p += RTA_SPACE(sizeof(*param));
  416. if (x->aalg) {
  417. struct xfrm_algo_desc *aalg_desc;
  418. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  419. p += (x->aalg->alg_key_len + 7) / 8;
  420. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  421. BUG_ON(!aalg_desc);
  422. err = -EINVAL;
  423. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  424. crypto_aead_authsize(aead)) {
  425. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  426. x->aalg->alg_name,
  427. crypto_aead_authsize(aead),
  428. aalg_desc->uinfo.auth.icv_fullbits/8);
  429. goto free_key;
  430. }
  431. err = crypto_aead_setauthsize(
  432. aead, aalg_desc->uinfo.auth.icv_truncbits / 8);
  433. if (err)
  434. goto free_key;
  435. }
  436. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  437. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  438. err = crypto_aead_setkey(aead, key, keylen);
  439. free_key:
  440. kfree(key);
  441. error:
  442. return err;
  443. }
  444. static int esp_init_state(struct xfrm_state *x)
  445. {
  446. struct esp_data *esp;
  447. struct crypto_aead *aead;
  448. u32 align;
  449. int err;
  450. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  451. if (esp == NULL)
  452. return -ENOMEM;
  453. x->data = esp;
  454. if (x->aead)
  455. err = esp_init_aead(x);
  456. else
  457. err = esp_init_authenc(x);
  458. if (err)
  459. goto error;
  460. aead = esp->aead;
  461. esp->padlen = 0;
  462. x->props.header_len = sizeof(struct ip_esp_hdr) +
  463. crypto_aead_ivsize(aead);
  464. if (x->props.mode == XFRM_MODE_TUNNEL)
  465. x->props.header_len += sizeof(struct iphdr);
  466. else if (x->props.mode == XFRM_MODE_BEET)
  467. x->props.header_len += IPV4_BEET_PHMAXLEN;
  468. if (x->encap) {
  469. struct xfrm_encap_tmpl *encap = x->encap;
  470. switch (encap->encap_type) {
  471. default:
  472. goto error;
  473. case UDP_ENCAP_ESPINUDP:
  474. x->props.header_len += sizeof(struct udphdr);
  475. break;
  476. case UDP_ENCAP_ESPINUDP_NON_IKE:
  477. x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
  478. break;
  479. }
  480. }
  481. align = ALIGN(crypto_aead_blocksize(aead), 4);
  482. if (esp->padlen)
  483. align = max_t(u32, align, esp->padlen);
  484. x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
  485. error:
  486. return err;
  487. }
  488. static const struct xfrm_type esp_type =
  489. {
  490. .description = "ESP4",
  491. .owner = THIS_MODULE,
  492. .proto = IPPROTO_ESP,
  493. .flags = XFRM_TYPE_REPLAY_PROT,
  494. .init_state = esp_init_state,
  495. .destructor = esp_destroy,
  496. .get_mtu = esp4_get_mtu,
  497. .input = esp_input,
  498. .output = esp_output
  499. };
  500. static struct net_protocol esp4_protocol = {
  501. .handler = xfrm4_rcv,
  502. .err_handler = esp4_err,
  503. .no_policy = 1,
  504. };
  505. static int __init esp4_init(void)
  506. {
  507. if (xfrm_register_type(&esp_type, AF_INET) < 0) {
  508. printk(KERN_INFO "ip esp init: can't add xfrm type\n");
  509. return -EAGAIN;
  510. }
  511. if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
  512. printk(KERN_INFO "ip esp init: can't add protocol\n");
  513. xfrm_unregister_type(&esp_type, AF_INET);
  514. return -EAGAIN;
  515. }
  516. return 0;
  517. }
  518. static void __exit esp4_fini(void)
  519. {
  520. if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
  521. printk(KERN_INFO "ip esp close: can't remove protocol\n");
  522. if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
  523. printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
  524. }
  525. module_init(esp4_init);
  526. module_exit(esp4_fini);
  527. MODULE_LICENSE("GPL");
  528. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);