esp4.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 net *net = dev_net(skb->dev);
  341. struct iphdr *iph = (struct iphdr *)skb->data;
  342. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
  343. struct xfrm_state *x;
  344. if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
  345. icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  346. return;
  347. x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
  348. if (!x)
  349. return;
  350. NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
  351. ntohl(esph->spi), ntohl(iph->daddr));
  352. xfrm_state_put(x);
  353. }
  354. static void esp_destroy(struct xfrm_state *x)
  355. {
  356. struct esp_data *esp = x->data;
  357. if (!esp)
  358. return;
  359. crypto_free_aead(esp->aead);
  360. kfree(esp);
  361. }
  362. static int esp_init_aead(struct xfrm_state *x)
  363. {
  364. struct esp_data *esp = x->data;
  365. struct crypto_aead *aead;
  366. int err;
  367. aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
  368. err = PTR_ERR(aead);
  369. if (IS_ERR(aead))
  370. goto error;
  371. esp->aead = aead;
  372. err = crypto_aead_setkey(aead, x->aead->alg_key,
  373. (x->aead->alg_key_len + 7) / 8);
  374. if (err)
  375. goto error;
  376. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  377. if (err)
  378. goto error;
  379. error:
  380. return err;
  381. }
  382. static int esp_init_authenc(struct xfrm_state *x)
  383. {
  384. struct esp_data *esp = x->data;
  385. struct crypto_aead *aead;
  386. struct crypto_authenc_key_param *param;
  387. struct rtattr *rta;
  388. char *key;
  389. char *p;
  390. char authenc_name[CRYPTO_MAX_ALG_NAME];
  391. unsigned int keylen;
  392. int err;
  393. err = -EINVAL;
  394. if (x->ealg == NULL)
  395. goto error;
  396. err = -ENAMETOOLONG;
  397. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
  398. x->aalg ? x->aalg->alg_name : "digest_null",
  399. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  400. goto error;
  401. aead = crypto_alloc_aead(authenc_name, 0, 0);
  402. err = PTR_ERR(aead);
  403. if (IS_ERR(aead))
  404. goto error;
  405. esp->aead = aead;
  406. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  407. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  408. err = -ENOMEM;
  409. key = kmalloc(keylen, GFP_KERNEL);
  410. if (!key)
  411. goto error;
  412. p = key;
  413. rta = (void *)p;
  414. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  415. rta->rta_len = RTA_LENGTH(sizeof(*param));
  416. param = RTA_DATA(rta);
  417. p += RTA_SPACE(sizeof(*param));
  418. if (x->aalg) {
  419. struct xfrm_algo_desc *aalg_desc;
  420. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  421. p += (x->aalg->alg_key_len + 7) / 8;
  422. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  423. BUG_ON(!aalg_desc);
  424. err = -EINVAL;
  425. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  426. crypto_aead_authsize(aead)) {
  427. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  428. x->aalg->alg_name,
  429. crypto_aead_authsize(aead),
  430. aalg_desc->uinfo.auth.icv_fullbits/8);
  431. goto free_key;
  432. }
  433. err = crypto_aead_setauthsize(
  434. aead, aalg_desc->uinfo.auth.icv_truncbits / 8);
  435. if (err)
  436. goto free_key;
  437. }
  438. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  439. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  440. err = crypto_aead_setkey(aead, key, keylen);
  441. free_key:
  442. kfree(key);
  443. error:
  444. return err;
  445. }
  446. static int esp_init_state(struct xfrm_state *x)
  447. {
  448. struct esp_data *esp;
  449. struct crypto_aead *aead;
  450. u32 align;
  451. int err;
  452. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  453. if (esp == NULL)
  454. return -ENOMEM;
  455. x->data = esp;
  456. if (x->aead)
  457. err = esp_init_aead(x);
  458. else
  459. err = esp_init_authenc(x);
  460. if (err)
  461. goto error;
  462. aead = esp->aead;
  463. esp->padlen = 0;
  464. x->props.header_len = sizeof(struct ip_esp_hdr) +
  465. crypto_aead_ivsize(aead);
  466. if (x->props.mode == XFRM_MODE_TUNNEL)
  467. x->props.header_len += sizeof(struct iphdr);
  468. else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
  469. x->props.header_len += IPV4_BEET_PHMAXLEN;
  470. if (x->encap) {
  471. struct xfrm_encap_tmpl *encap = x->encap;
  472. switch (encap->encap_type) {
  473. default:
  474. goto error;
  475. case UDP_ENCAP_ESPINUDP:
  476. x->props.header_len += sizeof(struct udphdr);
  477. break;
  478. case UDP_ENCAP_ESPINUDP_NON_IKE:
  479. x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
  480. break;
  481. }
  482. }
  483. align = ALIGN(crypto_aead_blocksize(aead), 4);
  484. if (esp->padlen)
  485. align = max_t(u32, align, esp->padlen);
  486. x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
  487. error:
  488. return err;
  489. }
  490. static const struct xfrm_type esp_type =
  491. {
  492. .description = "ESP4",
  493. .owner = THIS_MODULE,
  494. .proto = IPPROTO_ESP,
  495. .flags = XFRM_TYPE_REPLAY_PROT,
  496. .init_state = esp_init_state,
  497. .destructor = esp_destroy,
  498. .get_mtu = esp4_get_mtu,
  499. .input = esp_input,
  500. .output = esp_output
  501. };
  502. static struct net_protocol esp4_protocol = {
  503. .handler = xfrm4_rcv,
  504. .err_handler = esp4_err,
  505. .no_policy = 1,
  506. .netns_ok = 1,
  507. };
  508. static int __init esp4_init(void)
  509. {
  510. if (xfrm_register_type(&esp_type, AF_INET) < 0) {
  511. printk(KERN_INFO "ip esp init: can't add xfrm type\n");
  512. return -EAGAIN;
  513. }
  514. if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
  515. printk(KERN_INFO "ip esp init: can't add protocol\n");
  516. xfrm_unregister_type(&esp_type, AF_INET);
  517. return -EAGAIN;
  518. }
  519. return 0;
  520. }
  521. static void __exit esp4_fini(void)
  522. {
  523. if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
  524. printk(KERN_INFO "ip esp close: can't remove protocol\n");
  525. if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
  526. printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
  527. }
  528. module_init(esp4_init);
  529. module_exit(esp4_fini);
  530. MODULE_LICENSE("GPL");
  531. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);