esp6.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * Copyright (C)2002 USAGI/WIDE Project
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Authors
  18. *
  19. * Mitsuru KANDA @USAGI : IPv6 Support
  20. * Kazunori MIYAZAWA @USAGI :
  21. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  22. *
  23. * This file is derived from net/ipv4/esp.c
  24. */
  25. #define pr_fmt(fmt) "IPv6: " fmt
  26. #include <crypto/aead.h>
  27. #include <crypto/authenc.h>
  28. #include <linux/err.h>
  29. #include <linux/module.h>
  30. #include <net/ip.h>
  31. #include <net/xfrm.h>
  32. #include <net/esp.h>
  33. #include <linux/scatterlist.h>
  34. #include <linux/kernel.h>
  35. #include <linux/pfkeyv2.h>
  36. #include <linux/random.h>
  37. #include <linux/slab.h>
  38. #include <linux/spinlock.h>
  39. #include <net/ip6_route.h>
  40. #include <net/icmp.h>
  41. #include <net/ipv6.h>
  42. #include <net/protocol.h>
  43. #include <linux/icmpv6.h>
  44. #include <linux/highmem.h>
  45. struct esp_skb_cb {
  46. struct xfrm_skb_cb xfrm;
  47. void *tmp;
  48. };
  49. #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
  50. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu);
  51. /*
  52. * Allocate an AEAD request structure with extra space for SG and IV.
  53. *
  54. * For alignment considerations the upper 32 bits of the sequence number are
  55. * placed at the front, if present. Followed by the IV, the request and finally
  56. * the SG list.
  57. *
  58. * TODO: Use spare space in skb for this where possible.
  59. */
  60. static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int seqihlen)
  61. {
  62. unsigned int len;
  63. len = seqihlen;
  64. len += crypto_aead_ivsize(aead);
  65. if (len) {
  66. len += crypto_aead_alignmask(aead) &
  67. ~(crypto_tfm_ctx_alignment() - 1);
  68. len = ALIGN(len, crypto_tfm_ctx_alignment());
  69. }
  70. len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
  71. len = ALIGN(len, __alignof__(struct scatterlist));
  72. len += sizeof(struct scatterlist) * nfrags;
  73. return kmalloc(len, GFP_ATOMIC);
  74. }
  75. static inline __be32 *esp_tmp_seqhi(void *tmp)
  76. {
  77. return PTR_ALIGN((__be32 *)tmp, __alignof__(__be32));
  78. }
  79. static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int seqhilen)
  80. {
  81. return crypto_aead_ivsize(aead) ?
  82. PTR_ALIGN((u8 *)tmp + seqhilen,
  83. crypto_aead_alignmask(aead) + 1) : tmp + seqhilen;
  84. }
  85. static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
  86. {
  87. struct aead_request *req;
  88. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  89. crypto_tfm_ctx_alignment());
  90. aead_request_set_tfm(req, aead);
  91. return req;
  92. }
  93. static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
  94. struct aead_request *req)
  95. {
  96. return (void *)ALIGN((unsigned long)(req + 1) +
  97. crypto_aead_reqsize(aead),
  98. __alignof__(struct scatterlist));
  99. }
  100. static void esp_ssg_unref(struct xfrm_state *x, void *tmp)
  101. {
  102. struct crypto_aead *aead = x->data;
  103. int seqhilen = 0;
  104. u8 *iv;
  105. struct aead_request *req;
  106. struct scatterlist *sg;
  107. if (x->props.flags & XFRM_STATE_ESN)
  108. seqhilen += sizeof(__be32);
  109. iv = esp_tmp_iv(aead, tmp, seqhilen);
  110. req = esp_tmp_req(aead, iv);
  111. /* Unref skb_frag_pages in the src scatterlist if necessary.
  112. * Skip the first sg which comes from skb->data.
  113. */
  114. if (req->src != req->dst)
  115. for (sg = sg_next(req->src); sg; sg = sg_next(sg))
  116. put_page(sg_page(sg));
  117. }
  118. static void esp_output_done(struct crypto_async_request *base, int err)
  119. {
  120. struct sk_buff *skb = base->data;
  121. void *tmp;
  122. struct dst_entry *dst = skb_dst(skb);
  123. struct xfrm_state *x = dst->xfrm;
  124. tmp = ESP_SKB_CB(skb)->tmp;
  125. esp_ssg_unref(x, tmp);
  126. kfree(tmp);
  127. xfrm_output_resume(skb, err);
  128. }
  129. /* Move ESP header back into place. */
  130. static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
  131. {
  132. struct ip_esp_hdr *esph = (void *)(skb->data + offset);
  133. void *tmp = ESP_SKB_CB(skb)->tmp;
  134. __be32 *seqhi = esp_tmp_seqhi(tmp);
  135. esph->seq_no = esph->spi;
  136. esph->spi = *seqhi;
  137. }
  138. static void esp_output_restore_header(struct sk_buff *skb)
  139. {
  140. esp_restore_header(skb, skb_transport_offset(skb) - sizeof(__be32));
  141. }
  142. static struct ip_esp_hdr *esp_output_set_esn(struct sk_buff *skb,
  143. struct xfrm_state *x,
  144. struct ip_esp_hdr *esph,
  145. __be32 *seqhi)
  146. {
  147. /* For ESN we move the header forward by 4 bytes to
  148. * accomodate the high bits. We will move it back after
  149. * encryption.
  150. */
  151. if ((x->props.flags & XFRM_STATE_ESN)) {
  152. struct xfrm_offload *xo = xfrm_offload(skb);
  153. esph = (void *)(skb_transport_header(skb) - sizeof(__be32));
  154. *seqhi = esph->spi;
  155. if (xo)
  156. esph->seq_no = htonl(xo->seq.hi);
  157. else
  158. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
  159. }
  160. esph->spi = x->id.spi;
  161. return esph;
  162. }
  163. static void esp_output_done_esn(struct crypto_async_request *base, int err)
  164. {
  165. struct sk_buff *skb = base->data;
  166. esp_output_restore_header(skb);
  167. esp_output_done(base, err);
  168. }
  169. static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
  170. {
  171. /* Fill padding... */
  172. if (tfclen) {
  173. memset(tail, 0, tfclen);
  174. tail += tfclen;
  175. }
  176. do {
  177. int i;
  178. for (i = 0; i < plen - 2; i++)
  179. tail[i] = i + 1;
  180. } while (0);
  181. tail[plen - 2] = plen - 2;
  182. tail[plen - 1] = proto;
  183. }
  184. int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  185. {
  186. u8 *tail;
  187. u8 *vaddr;
  188. int nfrags;
  189. struct page *page;
  190. struct sk_buff *trailer;
  191. int tailen = esp->tailen;
  192. if (!skb_cloned(skb)) {
  193. if (tailen <= skb_tailroom(skb)) {
  194. nfrags = 1;
  195. trailer = skb;
  196. tail = skb_tail_pointer(trailer);
  197. goto skip_cow;
  198. } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
  199. && !skb_has_frag_list(skb)) {
  200. int allocsize;
  201. struct sock *sk = skb->sk;
  202. struct page_frag *pfrag = &x->xfrag;
  203. esp->inplace = false;
  204. allocsize = ALIGN(tailen, L1_CACHE_BYTES);
  205. spin_lock_bh(&x->lock);
  206. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  207. spin_unlock_bh(&x->lock);
  208. goto cow;
  209. }
  210. page = pfrag->page;
  211. get_page(page);
  212. vaddr = kmap_atomic(page);
  213. tail = vaddr + pfrag->offset;
  214. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  215. kunmap_atomic(vaddr);
  216. nfrags = skb_shinfo(skb)->nr_frags;
  217. __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
  218. tailen);
  219. skb_shinfo(skb)->nr_frags = ++nfrags;
  220. pfrag->offset = pfrag->offset + allocsize;
  221. spin_unlock_bh(&x->lock);
  222. nfrags++;
  223. skb->len += tailen;
  224. skb->data_len += tailen;
  225. skb->truesize += tailen;
  226. if (sk)
  227. refcount_add(tailen, &sk->sk_wmem_alloc);
  228. goto out;
  229. }
  230. }
  231. cow:
  232. nfrags = skb_cow_data(skb, tailen, &trailer);
  233. if (nfrags < 0)
  234. goto out;
  235. tail = skb_tail_pointer(trailer);
  236. skip_cow:
  237. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  238. pskb_put(skb, trailer, tailen);
  239. out:
  240. return nfrags;
  241. }
  242. EXPORT_SYMBOL_GPL(esp6_output_head);
  243. int esp6_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  244. {
  245. u8 *iv;
  246. int alen;
  247. void *tmp;
  248. int ivlen;
  249. int assoclen;
  250. int seqhilen;
  251. __be32 *seqhi;
  252. struct page *page;
  253. struct ip_esp_hdr *esph;
  254. struct aead_request *req;
  255. struct crypto_aead *aead;
  256. struct scatterlist *sg, *dsg;
  257. int err = -ENOMEM;
  258. assoclen = sizeof(struct ip_esp_hdr);
  259. seqhilen = 0;
  260. if (x->props.flags & XFRM_STATE_ESN) {
  261. seqhilen += sizeof(__be32);
  262. assoclen += sizeof(__be32);
  263. }
  264. aead = x->data;
  265. alen = crypto_aead_authsize(aead);
  266. ivlen = crypto_aead_ivsize(aead);
  267. tmp = esp_alloc_tmp(aead, esp->nfrags + 2, seqhilen);
  268. if (!tmp)
  269. goto error;
  270. seqhi = esp_tmp_seqhi(tmp);
  271. iv = esp_tmp_iv(aead, tmp, seqhilen);
  272. req = esp_tmp_req(aead, iv);
  273. sg = esp_req_sg(aead, req);
  274. if (esp->inplace)
  275. dsg = sg;
  276. else
  277. dsg = &sg[esp->nfrags];
  278. esph = esp_output_set_esn(skb, x, ip_esp_hdr(skb), seqhi);
  279. sg_init_table(sg, esp->nfrags);
  280. err = skb_to_sgvec(skb, sg,
  281. (unsigned char *)esph - skb->data,
  282. assoclen + ivlen + esp->clen + alen);
  283. if (unlikely(err < 0))
  284. goto error_free;
  285. if (!esp->inplace) {
  286. int allocsize;
  287. struct page_frag *pfrag = &x->xfrag;
  288. allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
  289. spin_lock_bh(&x->lock);
  290. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  291. spin_unlock_bh(&x->lock);
  292. goto error_free;
  293. }
  294. skb_shinfo(skb)->nr_frags = 1;
  295. page = pfrag->page;
  296. get_page(page);
  297. /* replace page frags in skb with new page */
  298. __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
  299. pfrag->offset = pfrag->offset + allocsize;
  300. spin_unlock_bh(&x->lock);
  301. sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
  302. err = skb_to_sgvec(skb, dsg,
  303. (unsigned char *)esph - skb->data,
  304. assoclen + ivlen + esp->clen + alen);
  305. if (unlikely(err < 0))
  306. goto error_free;
  307. }
  308. if ((x->props.flags & XFRM_STATE_ESN))
  309. aead_request_set_callback(req, 0, esp_output_done_esn, skb);
  310. else
  311. aead_request_set_callback(req, 0, esp_output_done, skb);
  312. aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
  313. aead_request_set_ad(req, assoclen);
  314. memset(iv, 0, ivlen);
  315. memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
  316. min(ivlen, 8));
  317. ESP_SKB_CB(skb)->tmp = tmp;
  318. err = crypto_aead_encrypt(req);
  319. switch (err) {
  320. case -EINPROGRESS:
  321. goto error;
  322. case -ENOSPC:
  323. err = NET_XMIT_DROP;
  324. break;
  325. case 0:
  326. if ((x->props.flags & XFRM_STATE_ESN))
  327. esp_output_restore_header(skb);
  328. }
  329. if (sg != dsg)
  330. esp_ssg_unref(x, tmp);
  331. error_free:
  332. kfree(tmp);
  333. error:
  334. return err;
  335. }
  336. EXPORT_SYMBOL_GPL(esp6_output_tail);
  337. static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
  338. {
  339. int alen;
  340. int blksize;
  341. struct ip_esp_hdr *esph;
  342. struct crypto_aead *aead;
  343. struct esp_info esp;
  344. esp.inplace = true;
  345. esp.proto = *skb_mac_header(skb);
  346. *skb_mac_header(skb) = IPPROTO_ESP;
  347. /* skb is pure payload to encrypt */
  348. aead = x->data;
  349. alen = crypto_aead_authsize(aead);
  350. esp.tfclen = 0;
  351. if (x->tfcpad) {
  352. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  353. u32 padto;
  354. padto = min(x->tfcpad, esp6_get_mtu(x, dst->child_mtu_cached));
  355. if (skb->len < padto)
  356. esp.tfclen = padto - skb->len;
  357. }
  358. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  359. esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
  360. esp.plen = esp.clen - skb->len - esp.tfclen;
  361. esp.tailen = esp.tfclen + esp.plen + alen;
  362. esp.nfrags = esp6_output_head(x, skb, &esp);
  363. if (esp.nfrags < 0)
  364. return esp.nfrags;
  365. esph = ip_esp_hdr(skb);
  366. esph->spi = x->id.spi;
  367. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  368. esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
  369. ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
  370. skb_push(skb, -skb_network_offset(skb));
  371. return esp6_output_tail(x, skb, &esp);
  372. }
  373. static inline int esp_remove_trailer(struct sk_buff *skb)
  374. {
  375. struct xfrm_state *x = xfrm_input_state(skb);
  376. struct xfrm_offload *xo = xfrm_offload(skb);
  377. struct crypto_aead *aead = x->data;
  378. int alen, hlen, elen;
  379. int padlen, trimlen;
  380. __wsum csumdiff;
  381. u8 nexthdr[2];
  382. int ret;
  383. alen = crypto_aead_authsize(aead);
  384. hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  385. elen = skb->len - hlen;
  386. if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
  387. ret = xo->proto;
  388. goto out;
  389. }
  390. ret = skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2);
  391. BUG_ON(ret);
  392. ret = -EINVAL;
  393. padlen = nexthdr[0];
  394. if (padlen + 2 + alen >= elen) {
  395. net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
  396. padlen + 2, elen - alen);
  397. goto out;
  398. }
  399. trimlen = alen + padlen + 2;
  400. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  401. csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
  402. skb->csum = csum_block_sub(skb->csum, csumdiff,
  403. skb->len - trimlen);
  404. }
  405. pskb_trim(skb, skb->len - trimlen);
  406. ret = nexthdr[1];
  407. out:
  408. return ret;
  409. }
  410. int esp6_input_done2(struct sk_buff *skb, int err)
  411. {
  412. struct xfrm_state *x = xfrm_input_state(skb);
  413. struct xfrm_offload *xo = xfrm_offload(skb);
  414. struct crypto_aead *aead = x->data;
  415. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  416. int hdr_len = skb_network_header_len(skb);
  417. if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
  418. kfree(ESP_SKB_CB(skb)->tmp);
  419. if (unlikely(err))
  420. goto out;
  421. err = esp_remove_trailer(skb);
  422. if (unlikely(err < 0))
  423. goto out;
  424. skb_postpull_rcsum(skb, skb_network_header(skb),
  425. skb_network_header_len(skb));
  426. skb_pull_rcsum(skb, hlen);
  427. if (x->props.mode == XFRM_MODE_TUNNEL)
  428. skb_reset_transport_header(skb);
  429. else
  430. skb_set_transport_header(skb, -hdr_len);
  431. /* RFC4303: Drop dummy packets without any error */
  432. if (err == IPPROTO_NONE)
  433. err = -EINVAL;
  434. out:
  435. return err;
  436. }
  437. EXPORT_SYMBOL_GPL(esp6_input_done2);
  438. static void esp_input_done(struct crypto_async_request *base, int err)
  439. {
  440. struct sk_buff *skb = base->data;
  441. xfrm_input_resume(skb, esp6_input_done2(skb, err));
  442. }
  443. static void esp_input_restore_header(struct sk_buff *skb)
  444. {
  445. esp_restore_header(skb, 0);
  446. __skb_pull(skb, 4);
  447. }
  448. static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
  449. {
  450. struct xfrm_state *x = xfrm_input_state(skb);
  451. /* For ESN we move the header forward by 4 bytes to
  452. * accomodate the high bits. We will move it back after
  453. * decryption.
  454. */
  455. if ((x->props.flags & XFRM_STATE_ESN)) {
  456. struct ip_esp_hdr *esph = skb_push(skb, 4);
  457. *seqhi = esph->spi;
  458. esph->spi = esph->seq_no;
  459. esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
  460. }
  461. }
  462. static void esp_input_done_esn(struct crypto_async_request *base, int err)
  463. {
  464. struct sk_buff *skb = base->data;
  465. esp_input_restore_header(skb);
  466. esp_input_done(base, err);
  467. }
  468. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  469. {
  470. struct ip_esp_hdr *esph;
  471. struct crypto_aead *aead = x->data;
  472. struct aead_request *req;
  473. struct sk_buff *trailer;
  474. int ivlen = crypto_aead_ivsize(aead);
  475. int elen = skb->len - sizeof(*esph) - ivlen;
  476. int nfrags;
  477. int assoclen;
  478. int seqhilen;
  479. int ret = 0;
  480. void *tmp;
  481. __be32 *seqhi;
  482. u8 *iv;
  483. struct scatterlist *sg;
  484. if (!pskb_may_pull(skb, sizeof(*esph) + ivlen)) {
  485. ret = -EINVAL;
  486. goto out;
  487. }
  488. if (elen <= 0) {
  489. ret = -EINVAL;
  490. goto out;
  491. }
  492. assoclen = sizeof(*esph);
  493. seqhilen = 0;
  494. if (x->props.flags & XFRM_STATE_ESN) {
  495. seqhilen += sizeof(__be32);
  496. assoclen += seqhilen;
  497. }
  498. if (!skb_cloned(skb)) {
  499. if (!skb_is_nonlinear(skb)) {
  500. nfrags = 1;
  501. goto skip_cow;
  502. } else if (!skb_has_frag_list(skb)) {
  503. nfrags = skb_shinfo(skb)->nr_frags;
  504. nfrags++;
  505. goto skip_cow;
  506. }
  507. }
  508. nfrags = skb_cow_data(skb, 0, &trailer);
  509. if (nfrags < 0) {
  510. ret = -EINVAL;
  511. goto out;
  512. }
  513. skip_cow:
  514. ret = -ENOMEM;
  515. tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
  516. if (!tmp)
  517. goto out;
  518. ESP_SKB_CB(skb)->tmp = tmp;
  519. seqhi = esp_tmp_seqhi(tmp);
  520. iv = esp_tmp_iv(aead, tmp, seqhilen);
  521. req = esp_tmp_req(aead, iv);
  522. sg = esp_req_sg(aead, req);
  523. esp_input_set_header(skb, seqhi);
  524. sg_init_table(sg, nfrags);
  525. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  526. if (unlikely(ret < 0))
  527. goto out;
  528. skb->ip_summed = CHECKSUM_NONE;
  529. if ((x->props.flags & XFRM_STATE_ESN))
  530. aead_request_set_callback(req, 0, esp_input_done_esn, skb);
  531. else
  532. aead_request_set_callback(req, 0, esp_input_done, skb);
  533. aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
  534. aead_request_set_ad(req, assoclen);
  535. ret = crypto_aead_decrypt(req);
  536. if (ret == -EINPROGRESS)
  537. goto out;
  538. if ((x->props.flags & XFRM_STATE_ESN))
  539. esp_input_restore_header(skb);
  540. ret = esp6_input_done2(skb, ret);
  541. out:
  542. return ret;
  543. }
  544. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
  545. {
  546. struct crypto_aead *aead = x->data;
  547. u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  548. unsigned int net_adj;
  549. if (x->props.mode != XFRM_MODE_TUNNEL)
  550. net_adj = sizeof(struct ipv6hdr);
  551. else
  552. net_adj = 0;
  553. return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
  554. net_adj) & ~(blksize - 1)) + net_adj - 2;
  555. }
  556. static int esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  557. u8 type, u8 code, int offset, __be32 info)
  558. {
  559. struct net *net = dev_net(skb->dev);
  560. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  561. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  562. struct xfrm_state *x;
  563. if (type != ICMPV6_PKT_TOOBIG &&
  564. type != NDISC_REDIRECT)
  565. return 0;
  566. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  567. esph->spi, IPPROTO_ESP, AF_INET6);
  568. if (!x)
  569. return 0;
  570. if (type == NDISC_REDIRECT)
  571. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  572. sock_net_uid(net, NULL));
  573. else
  574. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  575. xfrm_state_put(x);
  576. return 0;
  577. }
  578. static void esp6_destroy(struct xfrm_state *x)
  579. {
  580. struct crypto_aead *aead = x->data;
  581. if (!aead)
  582. return;
  583. crypto_free_aead(aead);
  584. }
  585. static int esp_init_aead(struct xfrm_state *x)
  586. {
  587. char aead_name[CRYPTO_MAX_ALG_NAME];
  588. struct crypto_aead *aead;
  589. int err;
  590. u32 mask = 0;
  591. err = -ENAMETOOLONG;
  592. if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  593. x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
  594. goto error;
  595. if (x->xso.offload_handle)
  596. mask |= CRYPTO_ALG_ASYNC;
  597. aead = crypto_alloc_aead(aead_name, 0, mask);
  598. err = PTR_ERR(aead);
  599. if (IS_ERR(aead))
  600. goto error;
  601. x->data = aead;
  602. err = crypto_aead_setkey(aead, x->aead->alg_key,
  603. (x->aead->alg_key_len + 7) / 8);
  604. if (err)
  605. goto error;
  606. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  607. if (err)
  608. goto error;
  609. error:
  610. return err;
  611. }
  612. static int esp_init_authenc(struct xfrm_state *x)
  613. {
  614. struct crypto_aead *aead;
  615. struct crypto_authenc_key_param *param;
  616. struct rtattr *rta;
  617. char *key;
  618. char *p;
  619. char authenc_name[CRYPTO_MAX_ALG_NAME];
  620. unsigned int keylen;
  621. int err;
  622. u32 mask = 0;
  623. err = -EINVAL;
  624. if (!x->ealg)
  625. goto error;
  626. err = -ENAMETOOLONG;
  627. if ((x->props.flags & XFRM_STATE_ESN)) {
  628. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  629. "%s%sauthencesn(%s,%s)%s",
  630. x->geniv ?: "", x->geniv ? "(" : "",
  631. x->aalg ? x->aalg->alg_name : "digest_null",
  632. x->ealg->alg_name,
  633. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  634. goto error;
  635. } else {
  636. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  637. "%s%sauthenc(%s,%s)%s",
  638. x->geniv ?: "", x->geniv ? "(" : "",
  639. x->aalg ? x->aalg->alg_name : "digest_null",
  640. x->ealg->alg_name,
  641. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  642. goto error;
  643. }
  644. if (x->xso.offload_handle)
  645. mask |= CRYPTO_ALG_ASYNC;
  646. aead = crypto_alloc_aead(authenc_name, 0, mask);
  647. err = PTR_ERR(aead);
  648. if (IS_ERR(aead))
  649. goto error;
  650. x->data = aead;
  651. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  652. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  653. err = -ENOMEM;
  654. key = kmalloc(keylen, GFP_KERNEL);
  655. if (!key)
  656. goto error;
  657. p = key;
  658. rta = (void *)p;
  659. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  660. rta->rta_len = RTA_LENGTH(sizeof(*param));
  661. param = RTA_DATA(rta);
  662. p += RTA_SPACE(sizeof(*param));
  663. if (x->aalg) {
  664. struct xfrm_algo_desc *aalg_desc;
  665. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  666. p += (x->aalg->alg_key_len + 7) / 8;
  667. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  668. BUG_ON(!aalg_desc);
  669. err = -EINVAL;
  670. if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
  671. crypto_aead_authsize(aead)) {
  672. pr_info("ESP: %s digestsize %u != %hu\n",
  673. x->aalg->alg_name,
  674. crypto_aead_authsize(aead),
  675. aalg_desc->uinfo.auth.icv_fullbits / 8);
  676. goto free_key;
  677. }
  678. err = crypto_aead_setauthsize(
  679. aead, x->aalg->alg_trunc_len / 8);
  680. if (err)
  681. goto free_key;
  682. }
  683. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  684. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  685. err = crypto_aead_setkey(aead, key, keylen);
  686. free_key:
  687. kfree(key);
  688. error:
  689. return err;
  690. }
  691. static int esp6_init_state(struct xfrm_state *x)
  692. {
  693. struct crypto_aead *aead;
  694. u32 align;
  695. int err;
  696. if (x->encap)
  697. return -EINVAL;
  698. x->data = NULL;
  699. if (x->aead)
  700. err = esp_init_aead(x);
  701. else
  702. err = esp_init_authenc(x);
  703. if (err)
  704. goto error;
  705. aead = x->data;
  706. x->props.header_len = sizeof(struct ip_esp_hdr) +
  707. crypto_aead_ivsize(aead);
  708. switch (x->props.mode) {
  709. case XFRM_MODE_BEET:
  710. if (x->sel.family != AF_INET6)
  711. x->props.header_len += IPV4_BEET_PHMAXLEN +
  712. (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
  713. break;
  714. case XFRM_MODE_TRANSPORT:
  715. break;
  716. case XFRM_MODE_TUNNEL:
  717. x->props.header_len += sizeof(struct ipv6hdr);
  718. break;
  719. default:
  720. goto error;
  721. }
  722. align = ALIGN(crypto_aead_blocksize(aead), 4);
  723. x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
  724. error:
  725. return err;
  726. }
  727. static int esp6_rcv_cb(struct sk_buff *skb, int err)
  728. {
  729. return 0;
  730. }
  731. static const struct xfrm_type esp6_type = {
  732. .description = "ESP6",
  733. .owner = THIS_MODULE,
  734. .proto = IPPROTO_ESP,
  735. .flags = XFRM_TYPE_REPLAY_PROT,
  736. .init_state = esp6_init_state,
  737. .destructor = esp6_destroy,
  738. .get_mtu = esp6_get_mtu,
  739. .input = esp6_input,
  740. .output = esp6_output,
  741. .hdr_offset = xfrm6_find_1stfragopt,
  742. };
  743. static struct xfrm6_protocol esp6_protocol = {
  744. .handler = xfrm6_rcv,
  745. .cb_handler = esp6_rcv_cb,
  746. .err_handler = esp6_err,
  747. .priority = 0,
  748. };
  749. static int __init esp6_init(void)
  750. {
  751. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  752. pr_info("%s: can't add xfrm type\n", __func__);
  753. return -EAGAIN;
  754. }
  755. if (xfrm6_protocol_register(&esp6_protocol, IPPROTO_ESP) < 0) {
  756. pr_info("%s: can't add protocol\n", __func__);
  757. xfrm_unregister_type(&esp6_type, AF_INET6);
  758. return -EAGAIN;
  759. }
  760. return 0;
  761. }
  762. static void __exit esp6_fini(void)
  763. {
  764. if (xfrm6_protocol_deregister(&esp6_protocol, IPPROTO_ESP) < 0)
  765. pr_info("%s: can't remove protocol\n", __func__);
  766. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  767. pr_info("%s: can't remove xfrm type\n", __func__);
  768. }
  769. module_init(esp6_init);
  770. module_exit(esp6_fini);
  771. MODULE_LICENSE("GPL");
  772. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);