esp4.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. __be16 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.output);
  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,
  179. XFRM_SKB_CB(skb)->seq.output);
  180. ESP_SKB_CB(skb)->tmp = tmp;
  181. err = crypto_aead_givencrypt(req);
  182. if (err == -EINPROGRESS)
  183. goto error;
  184. if (err == -EBUSY)
  185. err = NET_XMIT_DROP;
  186. kfree(tmp);
  187. error:
  188. return err;
  189. }
  190. static int esp_input_done2(struct sk_buff *skb, int err)
  191. {
  192. struct iphdr *iph;
  193. struct xfrm_state *x = xfrm_input_state(skb);
  194. struct esp_data *esp = x->data;
  195. struct crypto_aead *aead = esp->aead;
  196. int alen = crypto_aead_authsize(aead);
  197. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  198. int elen = skb->len - hlen;
  199. int ihl;
  200. u8 nexthdr[2];
  201. int padlen;
  202. kfree(ESP_SKB_CB(skb)->tmp);
  203. if (unlikely(err))
  204. goto out;
  205. if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
  206. BUG();
  207. err = -EINVAL;
  208. padlen = nexthdr[0];
  209. if (padlen + 2 + alen >= elen)
  210. goto out;
  211. /* ... check padding bits here. Silly. :-) */
  212. iph = ip_hdr(skb);
  213. ihl = iph->ihl * 4;
  214. if (x->encap) {
  215. struct xfrm_encap_tmpl *encap = x->encap;
  216. struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
  217. /*
  218. * 1) if the NAT-T peer's IP or port changed then
  219. * advertize the change to the keying daemon.
  220. * This is an inbound SA, so just compare
  221. * SRC ports.
  222. */
  223. if (iph->saddr != x->props.saddr.a4 ||
  224. uh->source != encap->encap_sport) {
  225. xfrm_address_t ipaddr;
  226. ipaddr.a4 = iph->saddr;
  227. km_new_mapping(x, &ipaddr, uh->source);
  228. /* XXX: perhaps add an extra
  229. * policy check here, to see
  230. * if we should allow or
  231. * reject a packet from a
  232. * different source
  233. * address/port.
  234. */
  235. }
  236. /*
  237. * 2) ignore UDP/TCP checksums in case
  238. * of NAT-T in Transport Mode, or
  239. * perform other post-processing fixes
  240. * as per draft-ietf-ipsec-udp-encaps-06,
  241. * section 3.1.2
  242. */
  243. if (x->props.mode == XFRM_MODE_TRANSPORT)
  244. skb->ip_summed = CHECKSUM_UNNECESSARY;
  245. }
  246. pskb_trim(skb, skb->len - alen - padlen - 2);
  247. __skb_pull(skb, hlen);
  248. skb_set_transport_header(skb, -ihl);
  249. err = nexthdr[1];
  250. /* RFC4303: Drop dummy packets without any error */
  251. if (err == IPPROTO_NONE)
  252. err = -EINVAL;
  253. out:
  254. return err;
  255. }
  256. static void esp_input_done(struct crypto_async_request *base, int err)
  257. {
  258. struct sk_buff *skb = base->data;
  259. xfrm_input_resume(skb, esp_input_done2(skb, err));
  260. }
  261. /*
  262. * Note: detecting truncated vs. non-truncated authentication data is very
  263. * expensive, so we only support truncated data, which is the recommended
  264. * and common case.
  265. */
  266. static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
  267. {
  268. struct ip_esp_hdr *esph;
  269. struct esp_data *esp = x->data;
  270. struct crypto_aead *aead = esp->aead;
  271. struct aead_request *req;
  272. struct sk_buff *trailer;
  273. int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
  274. int nfrags;
  275. void *tmp;
  276. u8 *iv;
  277. struct scatterlist *sg;
  278. struct scatterlist *asg;
  279. int err = -EINVAL;
  280. if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
  281. goto out;
  282. if (elen <= 0)
  283. goto out;
  284. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  285. goto out;
  286. nfrags = err;
  287. err = -ENOMEM;
  288. tmp = esp_alloc_tmp(aead, nfrags + 1);
  289. if (!tmp)
  290. goto out;
  291. ESP_SKB_CB(skb)->tmp = tmp;
  292. iv = esp_tmp_iv(aead, tmp);
  293. req = esp_tmp_req(aead, iv);
  294. asg = esp_req_sg(aead, req);
  295. sg = asg + 1;
  296. skb->ip_summed = CHECKSUM_NONE;
  297. esph = (struct ip_esp_hdr *)skb->data;
  298. /* Get ivec. This can be wrong, check against another impls. */
  299. iv = esph->enc_data;
  300. sg_init_table(sg, nfrags);
  301. skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
  302. sg_init_one(asg, esph, sizeof(*esph));
  303. aead_request_set_callback(req, 0, esp_input_done, skb);
  304. aead_request_set_crypt(req, sg, sg, elen, iv);
  305. aead_request_set_assoc(req, asg, sizeof(*esph));
  306. err = crypto_aead_decrypt(req);
  307. if (err == -EINPROGRESS)
  308. goto out;
  309. err = esp_input_done2(skb, err);
  310. out:
  311. return err;
  312. }
  313. static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
  314. {
  315. struct esp_data *esp = x->data;
  316. u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
  317. u32 align = max_t(u32, blksize, esp->padlen);
  318. u32 rem;
  319. mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
  320. rem = mtu & (align - 1);
  321. mtu &= ~(align - 1);
  322. switch (x->props.mode) {
  323. case XFRM_MODE_TUNNEL:
  324. break;
  325. default:
  326. case XFRM_MODE_TRANSPORT:
  327. /* The worst case */
  328. mtu -= blksize - 4;
  329. mtu += min_t(u32, blksize - 4, rem);
  330. break;
  331. case XFRM_MODE_BEET:
  332. /* The worst case. */
  333. mtu += min_t(u32, IPV4_BEET_PHMAXLEN, rem);
  334. break;
  335. }
  336. return mtu - 2;
  337. }
  338. static void esp4_err(struct sk_buff *skb, u32 info)
  339. {
  340. struct iphdr *iph = (struct iphdr*)skb->data;
  341. struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
  342. struct xfrm_state *x;
  343. if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
  344. icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  345. return;
  346. x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
  347. if (!x)
  348. return;
  349. NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
  350. ntohl(esph->spi), ntohl(iph->daddr));
  351. xfrm_state_put(x);
  352. }
  353. static void esp_destroy(struct xfrm_state *x)
  354. {
  355. struct esp_data *esp = x->data;
  356. if (!esp)
  357. return;
  358. crypto_free_aead(esp->aead);
  359. kfree(esp);
  360. }
  361. static int esp_init_aead(struct xfrm_state *x)
  362. {
  363. struct esp_data *esp = x->data;
  364. struct crypto_aead *aead;
  365. int err;
  366. aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
  367. err = PTR_ERR(aead);
  368. if (IS_ERR(aead))
  369. goto error;
  370. esp->aead = aead;
  371. err = crypto_aead_setkey(aead, x->aead->alg_key,
  372. (x->aead->alg_key_len + 7) / 8);
  373. if (err)
  374. goto error;
  375. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  376. if (err)
  377. goto error;
  378. error:
  379. return err;
  380. }
  381. static int esp_init_authenc(struct xfrm_state *x)
  382. {
  383. struct esp_data *esp = x->data;
  384. struct crypto_aead *aead;
  385. struct crypto_authenc_key_param *param;
  386. struct rtattr *rta;
  387. char *key;
  388. char *p;
  389. char authenc_name[CRYPTO_MAX_ALG_NAME];
  390. unsigned int keylen;
  391. int err;
  392. err = -EINVAL;
  393. if (x->ealg == NULL)
  394. goto error;
  395. err = -ENAMETOOLONG;
  396. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
  397. x->aalg ? x->aalg->alg_name : "digest_null",
  398. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  399. goto error;
  400. aead = crypto_alloc_aead(authenc_name, 0, 0);
  401. err = PTR_ERR(aead);
  402. if (IS_ERR(aead))
  403. goto error;
  404. esp->aead = aead;
  405. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  406. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  407. err = -ENOMEM;
  408. key = kmalloc(keylen, GFP_KERNEL);
  409. if (!key)
  410. goto error;
  411. p = key;
  412. rta = (void *)p;
  413. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  414. rta->rta_len = RTA_LENGTH(sizeof(*param));
  415. param = RTA_DATA(rta);
  416. p += RTA_SPACE(sizeof(*param));
  417. if (x->aalg) {
  418. struct xfrm_algo_desc *aalg_desc;
  419. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  420. p += (x->aalg->alg_key_len + 7) / 8;
  421. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  422. BUG_ON(!aalg_desc);
  423. err = -EINVAL;
  424. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  425. crypto_aead_authsize(aead)) {
  426. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  427. x->aalg->alg_name,
  428. crypto_aead_authsize(aead),
  429. aalg_desc->uinfo.auth.icv_fullbits/8);
  430. goto free_key;
  431. }
  432. err = crypto_aead_setauthsize(
  433. aead, aalg_desc->uinfo.auth.icv_truncbits / 8);
  434. if (err)
  435. goto free_key;
  436. }
  437. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  438. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  439. err = crypto_aead_setkey(aead, key, keylen);
  440. free_key:
  441. kfree(key);
  442. error:
  443. return err;
  444. }
  445. static int esp_init_state(struct xfrm_state *x)
  446. {
  447. struct esp_data *esp;
  448. struct crypto_aead *aead;
  449. u32 align;
  450. int err;
  451. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  452. if (esp == NULL)
  453. return -ENOMEM;
  454. x->data = esp;
  455. if (x->aead)
  456. err = esp_init_aead(x);
  457. else
  458. err = esp_init_authenc(x);
  459. if (err)
  460. goto error;
  461. aead = esp->aead;
  462. esp->padlen = 0;
  463. x->props.header_len = sizeof(struct ip_esp_hdr) +
  464. crypto_aead_ivsize(aead);
  465. if (x->props.mode == XFRM_MODE_TUNNEL)
  466. x->props.header_len += sizeof(struct iphdr);
  467. else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
  468. x->props.header_len += IPV4_BEET_PHMAXLEN;
  469. if (x->encap) {
  470. struct xfrm_encap_tmpl *encap = x->encap;
  471. switch (encap->encap_type) {
  472. default:
  473. goto error;
  474. case UDP_ENCAP_ESPINUDP:
  475. x->props.header_len += sizeof(struct udphdr);
  476. break;
  477. case UDP_ENCAP_ESPINUDP_NON_IKE:
  478. x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
  479. break;
  480. }
  481. }
  482. align = ALIGN(crypto_aead_blocksize(aead), 4);
  483. if (esp->padlen)
  484. align = max_t(u32, align, esp->padlen);
  485. x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
  486. error:
  487. return err;
  488. }
  489. static const struct xfrm_type esp_type =
  490. {
  491. .description = "ESP4",
  492. .owner = THIS_MODULE,
  493. .proto = IPPROTO_ESP,
  494. .flags = XFRM_TYPE_REPLAY_PROT,
  495. .init_state = esp_init_state,
  496. .destructor = esp_destroy,
  497. .get_mtu = esp4_get_mtu,
  498. .input = esp_input,
  499. .output = esp_output
  500. };
  501. static struct net_protocol esp4_protocol = {
  502. .handler = xfrm4_rcv,
  503. .err_handler = esp4_err,
  504. .no_policy = 1,
  505. };
  506. static int __init esp4_init(void)
  507. {
  508. if (xfrm_register_type(&esp_type, AF_INET) < 0) {
  509. printk(KERN_INFO "ip esp init: can't add xfrm type\n");
  510. return -EAGAIN;
  511. }
  512. if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
  513. printk(KERN_INFO "ip esp init: can't add protocol\n");
  514. xfrm_unregister_type(&esp_type, AF_INET);
  515. return -EAGAIN;
  516. }
  517. return 0;
  518. }
  519. static void __exit esp4_fini(void)
  520. {
  521. if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
  522. printk(KERN_INFO "ip esp close: can't remove protocol\n");
  523. if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
  524. printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
  525. }
  526. module_init(esp4_init);
  527. module_exit(esp4_fini);
  528. MODULE_LICENSE("GPL");
  529. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);