esp6.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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_availroom(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. spin_unlock_bh(&x->lock);
  217. nfrags = skb_shinfo(skb)->nr_frags;
  218. __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
  219. tailen);
  220. skb_shinfo(skb)->nr_frags = ++nfrags;
  221. pfrag->offset = pfrag->offset + allocsize;
  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;
  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;
  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;
  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 -EBUSY:
  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. kfree(tmp);
  332. error:
  333. return err;
  334. }
  335. EXPORT_SYMBOL_GPL(esp6_output_tail);
  336. static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
  337. {
  338. int alen;
  339. int blksize;
  340. struct ip_esp_hdr *esph;
  341. struct crypto_aead *aead;
  342. struct esp_info esp;
  343. esp.inplace = true;
  344. esp.proto = *skb_mac_header(skb);
  345. *skb_mac_header(skb) = IPPROTO_ESP;
  346. /* skb is pure payload to encrypt */
  347. aead = x->data;
  348. alen = crypto_aead_authsize(aead);
  349. esp.tfclen = 0;
  350. if (x->tfcpad) {
  351. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  352. u32 padto;
  353. padto = min(x->tfcpad, esp6_get_mtu(x, dst->child_mtu_cached));
  354. if (skb->len < padto)
  355. esp.tfclen = padto - skb->len;
  356. }
  357. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  358. esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
  359. esp.plen = esp.clen - skb->len - esp.tfclen;
  360. esp.tailen = esp.tfclen + esp.plen + alen;
  361. esp.nfrags = esp6_output_head(x, skb, &esp);
  362. if (esp.nfrags < 0)
  363. return esp.nfrags;
  364. esph = ip_esp_hdr(skb);
  365. esph->spi = x->id.spi;
  366. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  367. esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
  368. ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
  369. skb_push(skb, -skb_network_offset(skb));
  370. return esp6_output_tail(x, skb, &esp);
  371. }
  372. int esp6_input_done2(struct sk_buff *skb, int err)
  373. {
  374. struct xfrm_state *x = xfrm_input_state(skb);
  375. struct xfrm_offload *xo = xfrm_offload(skb);
  376. struct crypto_aead *aead = x->data;
  377. int alen = crypto_aead_authsize(aead);
  378. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  379. int elen = skb->len - hlen;
  380. int hdr_len = skb_network_header_len(skb);
  381. int padlen;
  382. u8 nexthdr[2];
  383. if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
  384. kfree(ESP_SKB_CB(skb)->tmp);
  385. if (unlikely(err))
  386. goto out;
  387. if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
  388. BUG();
  389. err = -EINVAL;
  390. padlen = nexthdr[0];
  391. if (padlen + 2 + alen >= elen) {
  392. net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
  393. padlen + 2, elen - alen);
  394. goto out;
  395. }
  396. /* ... check padding bits here. Silly. :-) */
  397. pskb_trim(skb, skb->len - alen - padlen - 2);
  398. __skb_pull(skb, hlen);
  399. if (x->props.mode == XFRM_MODE_TUNNEL)
  400. skb_reset_transport_header(skb);
  401. else
  402. skb_set_transport_header(skb, -hdr_len);
  403. err = nexthdr[1];
  404. /* RFC4303: Drop dummy packets without any error */
  405. if (err == IPPROTO_NONE)
  406. err = -EINVAL;
  407. out:
  408. return err;
  409. }
  410. EXPORT_SYMBOL_GPL(esp6_input_done2);
  411. static void esp_input_done(struct crypto_async_request *base, int err)
  412. {
  413. struct sk_buff *skb = base->data;
  414. xfrm_input_resume(skb, esp6_input_done2(skb, err));
  415. }
  416. static void esp_input_restore_header(struct sk_buff *skb)
  417. {
  418. esp_restore_header(skb, 0);
  419. __skb_pull(skb, 4);
  420. }
  421. static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
  422. {
  423. struct xfrm_state *x = xfrm_input_state(skb);
  424. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)skb->data;
  425. /* For ESN we move the header forward by 4 bytes to
  426. * accomodate the high bits. We will move it back after
  427. * decryption.
  428. */
  429. if ((x->props.flags & XFRM_STATE_ESN)) {
  430. esph = skb_push(skb, 4);
  431. *seqhi = esph->spi;
  432. esph->spi = esph->seq_no;
  433. esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
  434. }
  435. }
  436. static void esp_input_done_esn(struct crypto_async_request *base, int err)
  437. {
  438. struct sk_buff *skb = base->data;
  439. esp_input_restore_header(skb);
  440. esp_input_done(base, err);
  441. }
  442. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  443. {
  444. struct ip_esp_hdr *esph;
  445. struct crypto_aead *aead = x->data;
  446. struct aead_request *req;
  447. struct sk_buff *trailer;
  448. int ivlen = crypto_aead_ivsize(aead);
  449. int elen = skb->len - sizeof(*esph) - ivlen;
  450. int nfrags;
  451. int assoclen;
  452. int seqhilen;
  453. int ret = 0;
  454. void *tmp;
  455. __be32 *seqhi;
  456. u8 *iv;
  457. struct scatterlist *sg;
  458. if (!pskb_may_pull(skb, sizeof(*esph) + ivlen)) {
  459. ret = -EINVAL;
  460. goto out;
  461. }
  462. if (elen <= 0) {
  463. ret = -EINVAL;
  464. goto out;
  465. }
  466. assoclen = sizeof(*esph);
  467. seqhilen = 0;
  468. if (x->props.flags & XFRM_STATE_ESN) {
  469. seqhilen += sizeof(__be32);
  470. assoclen += seqhilen;
  471. }
  472. if (!skb_cloned(skb)) {
  473. if (!skb_is_nonlinear(skb)) {
  474. nfrags = 1;
  475. goto skip_cow;
  476. } else if (!skb_has_frag_list(skb)) {
  477. nfrags = skb_shinfo(skb)->nr_frags;
  478. nfrags++;
  479. goto skip_cow;
  480. }
  481. }
  482. nfrags = skb_cow_data(skb, 0, &trailer);
  483. if (nfrags < 0) {
  484. ret = -EINVAL;
  485. goto out;
  486. }
  487. skip_cow:
  488. ret = -ENOMEM;
  489. tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
  490. if (!tmp)
  491. goto out;
  492. ESP_SKB_CB(skb)->tmp = tmp;
  493. seqhi = esp_tmp_seqhi(tmp);
  494. iv = esp_tmp_iv(aead, tmp, seqhilen);
  495. req = esp_tmp_req(aead, iv);
  496. sg = esp_req_sg(aead, req);
  497. esp_input_set_header(skb, seqhi);
  498. sg_init_table(sg, nfrags);
  499. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  500. if (unlikely(ret < 0))
  501. goto out;
  502. skb->ip_summed = CHECKSUM_NONE;
  503. if ((x->props.flags & XFRM_STATE_ESN))
  504. aead_request_set_callback(req, 0, esp_input_done_esn, skb);
  505. else
  506. aead_request_set_callback(req, 0, esp_input_done, skb);
  507. aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
  508. aead_request_set_ad(req, assoclen);
  509. ret = crypto_aead_decrypt(req);
  510. if (ret == -EINPROGRESS)
  511. goto out;
  512. if ((x->props.flags & XFRM_STATE_ESN))
  513. esp_input_restore_header(skb);
  514. ret = esp6_input_done2(skb, ret);
  515. out:
  516. return ret;
  517. }
  518. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
  519. {
  520. struct crypto_aead *aead = x->data;
  521. u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  522. unsigned int net_adj;
  523. if (x->props.mode != XFRM_MODE_TUNNEL)
  524. net_adj = sizeof(struct ipv6hdr);
  525. else
  526. net_adj = 0;
  527. return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
  528. net_adj) & ~(blksize - 1)) + net_adj - 2;
  529. }
  530. static int esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  531. u8 type, u8 code, int offset, __be32 info)
  532. {
  533. struct net *net = dev_net(skb->dev);
  534. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  535. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  536. struct xfrm_state *x;
  537. if (type != ICMPV6_PKT_TOOBIG &&
  538. type != NDISC_REDIRECT)
  539. return 0;
  540. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  541. esph->spi, IPPROTO_ESP, AF_INET6);
  542. if (!x)
  543. return 0;
  544. if (type == NDISC_REDIRECT)
  545. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  546. sock_net_uid(net, NULL));
  547. else
  548. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  549. xfrm_state_put(x);
  550. return 0;
  551. }
  552. static void esp6_destroy(struct xfrm_state *x)
  553. {
  554. struct crypto_aead *aead = x->data;
  555. if (!aead)
  556. return;
  557. crypto_free_aead(aead);
  558. }
  559. static int esp_init_aead(struct xfrm_state *x)
  560. {
  561. char aead_name[CRYPTO_MAX_ALG_NAME];
  562. struct crypto_aead *aead;
  563. int err;
  564. u32 mask = 0;
  565. err = -ENAMETOOLONG;
  566. if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  567. x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
  568. goto error;
  569. if (x->xso.offload_handle)
  570. mask |= CRYPTO_ALG_ASYNC;
  571. aead = crypto_alloc_aead(aead_name, 0, mask);
  572. err = PTR_ERR(aead);
  573. if (IS_ERR(aead))
  574. goto error;
  575. x->data = aead;
  576. err = crypto_aead_setkey(aead, x->aead->alg_key,
  577. (x->aead->alg_key_len + 7) / 8);
  578. if (err)
  579. goto error;
  580. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  581. if (err)
  582. goto error;
  583. error:
  584. return err;
  585. }
  586. static int esp_init_authenc(struct xfrm_state *x)
  587. {
  588. struct crypto_aead *aead;
  589. struct crypto_authenc_key_param *param;
  590. struct rtattr *rta;
  591. char *key;
  592. char *p;
  593. char authenc_name[CRYPTO_MAX_ALG_NAME];
  594. unsigned int keylen;
  595. int err;
  596. u32 mask = 0;
  597. err = -EINVAL;
  598. if (!x->ealg)
  599. goto error;
  600. err = -ENAMETOOLONG;
  601. if ((x->props.flags & XFRM_STATE_ESN)) {
  602. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  603. "%s%sauthencesn(%s,%s)%s",
  604. x->geniv ?: "", x->geniv ? "(" : "",
  605. x->aalg ? x->aalg->alg_name : "digest_null",
  606. x->ealg->alg_name,
  607. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  608. goto error;
  609. } else {
  610. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  611. "%s%sauthenc(%s,%s)%s",
  612. x->geniv ?: "", x->geniv ? "(" : "",
  613. x->aalg ? x->aalg->alg_name : "digest_null",
  614. x->ealg->alg_name,
  615. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  616. goto error;
  617. }
  618. if (x->xso.offload_handle)
  619. mask |= CRYPTO_ALG_ASYNC;
  620. aead = crypto_alloc_aead(authenc_name, 0, mask);
  621. err = PTR_ERR(aead);
  622. if (IS_ERR(aead))
  623. goto error;
  624. x->data = aead;
  625. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  626. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  627. err = -ENOMEM;
  628. key = kmalloc(keylen, GFP_KERNEL);
  629. if (!key)
  630. goto error;
  631. p = key;
  632. rta = (void *)p;
  633. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  634. rta->rta_len = RTA_LENGTH(sizeof(*param));
  635. param = RTA_DATA(rta);
  636. p += RTA_SPACE(sizeof(*param));
  637. if (x->aalg) {
  638. struct xfrm_algo_desc *aalg_desc;
  639. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  640. p += (x->aalg->alg_key_len + 7) / 8;
  641. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  642. BUG_ON(!aalg_desc);
  643. err = -EINVAL;
  644. if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
  645. crypto_aead_authsize(aead)) {
  646. pr_info("ESP: %s digestsize %u != %hu\n",
  647. x->aalg->alg_name,
  648. crypto_aead_authsize(aead),
  649. aalg_desc->uinfo.auth.icv_fullbits / 8);
  650. goto free_key;
  651. }
  652. err = crypto_aead_setauthsize(
  653. aead, x->aalg->alg_trunc_len / 8);
  654. if (err)
  655. goto free_key;
  656. }
  657. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  658. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  659. err = crypto_aead_setkey(aead, key, keylen);
  660. free_key:
  661. kfree(key);
  662. error:
  663. return err;
  664. }
  665. static int esp6_init_state(struct xfrm_state *x)
  666. {
  667. struct crypto_aead *aead;
  668. u32 align;
  669. int err;
  670. if (x->encap)
  671. return -EINVAL;
  672. x->data = NULL;
  673. if (x->aead)
  674. err = esp_init_aead(x);
  675. else
  676. err = esp_init_authenc(x);
  677. if (err)
  678. goto error;
  679. aead = x->data;
  680. x->props.header_len = sizeof(struct ip_esp_hdr) +
  681. crypto_aead_ivsize(aead);
  682. switch (x->props.mode) {
  683. case XFRM_MODE_BEET:
  684. if (x->sel.family != AF_INET6)
  685. x->props.header_len += IPV4_BEET_PHMAXLEN +
  686. (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
  687. break;
  688. case XFRM_MODE_TRANSPORT:
  689. break;
  690. case XFRM_MODE_TUNNEL:
  691. x->props.header_len += sizeof(struct ipv6hdr);
  692. break;
  693. default:
  694. goto error;
  695. }
  696. align = ALIGN(crypto_aead_blocksize(aead), 4);
  697. x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
  698. error:
  699. return err;
  700. }
  701. static int esp6_rcv_cb(struct sk_buff *skb, int err)
  702. {
  703. return 0;
  704. }
  705. static const struct xfrm_type esp6_type = {
  706. .description = "ESP6",
  707. .owner = THIS_MODULE,
  708. .proto = IPPROTO_ESP,
  709. .flags = XFRM_TYPE_REPLAY_PROT,
  710. .init_state = esp6_init_state,
  711. .destructor = esp6_destroy,
  712. .get_mtu = esp6_get_mtu,
  713. .input = esp6_input,
  714. .output = esp6_output,
  715. .hdr_offset = xfrm6_find_1stfragopt,
  716. };
  717. static struct xfrm6_protocol esp6_protocol = {
  718. .handler = xfrm6_rcv,
  719. .cb_handler = esp6_rcv_cb,
  720. .err_handler = esp6_err,
  721. .priority = 0,
  722. };
  723. static int __init esp6_init(void)
  724. {
  725. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  726. pr_info("%s: can't add xfrm type\n", __func__);
  727. return -EAGAIN;
  728. }
  729. if (xfrm6_protocol_register(&esp6_protocol, IPPROTO_ESP) < 0) {
  730. pr_info("%s: can't add protocol\n", __func__);
  731. xfrm_unregister_type(&esp6_type, AF_INET6);
  732. return -EAGAIN;
  733. }
  734. return 0;
  735. }
  736. static void __exit esp6_fini(void)
  737. {
  738. if (xfrm6_protocol_deregister(&esp6_protocol, IPPROTO_ESP) < 0)
  739. pr_info("%s: can't remove protocol\n", __func__);
  740. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  741. pr_info("%s: can't remove xfrm type\n", __func__);
  742. }
  743. module_init(esp6_init);
  744. module_exit(esp6_fini);
  745. MODULE_LICENSE("GPL");
  746. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);