esp6.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. struct xfrm_offload *xo = xfrm_offload(skb);
  122. void *tmp;
  123. struct xfrm_state *x;
  124. if (xo && (xo->flags & XFRM_DEV_RESUME))
  125. x = skb->sp->xvec[skb->sp->len - 1];
  126. else
  127. x = skb_dst(skb)->xfrm;
  128. tmp = ESP_SKB_CB(skb)->tmp;
  129. esp_ssg_unref(x, tmp);
  130. kfree(tmp);
  131. if (xo && (xo->flags & XFRM_DEV_RESUME)) {
  132. if (err) {
  133. XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  134. kfree_skb(skb);
  135. return;
  136. }
  137. skb_push(skb, skb->data - skb_mac_header(skb));
  138. secpath_reset(skb);
  139. xfrm_dev_resume(skb);
  140. } else {
  141. xfrm_output_resume(skb, err);
  142. }
  143. }
  144. /* Move ESP header back into place. */
  145. static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
  146. {
  147. struct ip_esp_hdr *esph = (void *)(skb->data + offset);
  148. void *tmp = ESP_SKB_CB(skb)->tmp;
  149. __be32 *seqhi = esp_tmp_seqhi(tmp);
  150. esph->seq_no = esph->spi;
  151. esph->spi = *seqhi;
  152. }
  153. static void esp_output_restore_header(struct sk_buff *skb)
  154. {
  155. esp_restore_header(skb, skb_transport_offset(skb) - sizeof(__be32));
  156. }
  157. static struct ip_esp_hdr *esp_output_set_esn(struct sk_buff *skb,
  158. struct xfrm_state *x,
  159. struct ip_esp_hdr *esph,
  160. __be32 *seqhi)
  161. {
  162. /* For ESN we move the header forward by 4 bytes to
  163. * accomodate the high bits. We will move it back after
  164. * encryption.
  165. */
  166. if ((x->props.flags & XFRM_STATE_ESN)) {
  167. struct xfrm_offload *xo = xfrm_offload(skb);
  168. esph = (void *)(skb_transport_header(skb) - sizeof(__be32));
  169. *seqhi = esph->spi;
  170. if (xo)
  171. esph->seq_no = htonl(xo->seq.hi);
  172. else
  173. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
  174. }
  175. esph->spi = x->id.spi;
  176. return esph;
  177. }
  178. static void esp_output_done_esn(struct crypto_async_request *base, int err)
  179. {
  180. struct sk_buff *skb = base->data;
  181. esp_output_restore_header(skb);
  182. esp_output_done(base, err);
  183. }
  184. static void esp_output_fill_trailer(u8 *tail, int tfclen, int plen, __u8 proto)
  185. {
  186. /* Fill padding... */
  187. if (tfclen) {
  188. memset(tail, 0, tfclen);
  189. tail += tfclen;
  190. }
  191. do {
  192. int i;
  193. for (i = 0; i < plen - 2; i++)
  194. tail[i] = i + 1;
  195. } while (0);
  196. tail[plen - 2] = plen - 2;
  197. tail[plen - 1] = proto;
  198. }
  199. int esp6_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  200. {
  201. u8 *tail;
  202. int nfrags;
  203. struct page *page;
  204. struct sk_buff *trailer;
  205. int tailen = esp->tailen;
  206. if (!skb_cloned(skb)) {
  207. if (tailen <= skb_tailroom(skb)) {
  208. nfrags = 1;
  209. trailer = skb;
  210. tail = skb_tail_pointer(trailer);
  211. goto skip_cow;
  212. } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
  213. && !skb_has_frag_list(skb)) {
  214. int allocsize;
  215. struct sock *sk = skb->sk;
  216. struct page_frag *pfrag = &x->xfrag;
  217. esp->inplace = false;
  218. allocsize = ALIGN(tailen, L1_CACHE_BYTES);
  219. spin_lock_bh(&x->lock);
  220. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  221. spin_unlock_bh(&x->lock);
  222. goto cow;
  223. }
  224. page = pfrag->page;
  225. get_page(page);
  226. tail = page_address(page) + pfrag->offset;
  227. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  228. nfrags = skb_shinfo(skb)->nr_frags;
  229. __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
  230. tailen);
  231. skb_shinfo(skb)->nr_frags = ++nfrags;
  232. pfrag->offset = pfrag->offset + allocsize;
  233. spin_unlock_bh(&x->lock);
  234. nfrags++;
  235. skb->len += tailen;
  236. skb->data_len += tailen;
  237. skb->truesize += tailen;
  238. if (sk && sk_fullsock(sk))
  239. refcount_add(tailen, &sk->sk_wmem_alloc);
  240. goto out;
  241. }
  242. }
  243. cow:
  244. nfrags = skb_cow_data(skb, tailen, &trailer);
  245. if (nfrags < 0)
  246. goto out;
  247. tail = skb_tail_pointer(trailer);
  248. skip_cow:
  249. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  250. pskb_put(skb, trailer, tailen);
  251. out:
  252. return nfrags;
  253. }
  254. EXPORT_SYMBOL_GPL(esp6_output_head);
  255. int esp6_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  256. {
  257. u8 *iv;
  258. int alen;
  259. void *tmp;
  260. int ivlen;
  261. int assoclen;
  262. int seqhilen;
  263. __be32 *seqhi;
  264. struct page *page;
  265. struct ip_esp_hdr *esph;
  266. struct aead_request *req;
  267. struct crypto_aead *aead;
  268. struct scatterlist *sg, *dsg;
  269. int err = -ENOMEM;
  270. assoclen = sizeof(struct ip_esp_hdr);
  271. seqhilen = 0;
  272. if (x->props.flags & XFRM_STATE_ESN) {
  273. seqhilen += sizeof(__be32);
  274. assoclen += sizeof(__be32);
  275. }
  276. aead = x->data;
  277. alen = crypto_aead_authsize(aead);
  278. ivlen = crypto_aead_ivsize(aead);
  279. tmp = esp_alloc_tmp(aead, esp->nfrags + 2, seqhilen);
  280. if (!tmp)
  281. goto error;
  282. seqhi = esp_tmp_seqhi(tmp);
  283. iv = esp_tmp_iv(aead, tmp, seqhilen);
  284. req = esp_tmp_req(aead, iv);
  285. sg = esp_req_sg(aead, req);
  286. if (esp->inplace)
  287. dsg = sg;
  288. else
  289. dsg = &sg[esp->nfrags];
  290. esph = esp_output_set_esn(skb, x, ip_esp_hdr(skb), seqhi);
  291. sg_init_table(sg, esp->nfrags);
  292. err = skb_to_sgvec(skb, sg,
  293. (unsigned char *)esph - skb->data,
  294. assoclen + ivlen + esp->clen + alen);
  295. if (unlikely(err < 0))
  296. goto error_free;
  297. if (!esp->inplace) {
  298. int allocsize;
  299. struct page_frag *pfrag = &x->xfrag;
  300. allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
  301. spin_lock_bh(&x->lock);
  302. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  303. spin_unlock_bh(&x->lock);
  304. goto error_free;
  305. }
  306. skb_shinfo(skb)->nr_frags = 1;
  307. page = pfrag->page;
  308. get_page(page);
  309. /* replace page frags in skb with new page */
  310. __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
  311. pfrag->offset = pfrag->offset + allocsize;
  312. spin_unlock_bh(&x->lock);
  313. sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
  314. err = skb_to_sgvec(skb, dsg,
  315. (unsigned char *)esph - skb->data,
  316. assoclen + ivlen + esp->clen + alen);
  317. if (unlikely(err < 0))
  318. goto error_free;
  319. }
  320. if ((x->props.flags & XFRM_STATE_ESN))
  321. aead_request_set_callback(req, 0, esp_output_done_esn, skb);
  322. else
  323. aead_request_set_callback(req, 0, esp_output_done, skb);
  324. aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
  325. aead_request_set_ad(req, assoclen);
  326. memset(iv, 0, ivlen);
  327. memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
  328. min(ivlen, 8));
  329. ESP_SKB_CB(skb)->tmp = tmp;
  330. err = crypto_aead_encrypt(req);
  331. switch (err) {
  332. case -EINPROGRESS:
  333. goto error;
  334. case -ENOSPC:
  335. err = NET_XMIT_DROP;
  336. break;
  337. case 0:
  338. if ((x->props.flags & XFRM_STATE_ESN))
  339. esp_output_restore_header(skb);
  340. }
  341. if (sg != dsg)
  342. esp_ssg_unref(x, tmp);
  343. error_free:
  344. kfree(tmp);
  345. error:
  346. return err;
  347. }
  348. EXPORT_SYMBOL_GPL(esp6_output_tail);
  349. static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
  350. {
  351. int alen;
  352. int blksize;
  353. struct ip_esp_hdr *esph;
  354. struct crypto_aead *aead;
  355. struct esp_info esp;
  356. esp.inplace = true;
  357. esp.proto = *skb_mac_header(skb);
  358. *skb_mac_header(skb) = IPPROTO_ESP;
  359. /* skb is pure payload to encrypt */
  360. aead = x->data;
  361. alen = crypto_aead_authsize(aead);
  362. esp.tfclen = 0;
  363. if (x->tfcpad) {
  364. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  365. u32 padto;
  366. padto = min(x->tfcpad, esp6_get_mtu(x, dst->child_mtu_cached));
  367. if (skb->len < padto)
  368. esp.tfclen = padto - skb->len;
  369. }
  370. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  371. esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
  372. esp.plen = esp.clen - skb->len - esp.tfclen;
  373. esp.tailen = esp.tfclen + esp.plen + alen;
  374. esp.nfrags = esp6_output_head(x, skb, &esp);
  375. if (esp.nfrags < 0)
  376. return esp.nfrags;
  377. esph = ip_esp_hdr(skb);
  378. esph->spi = x->id.spi;
  379. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  380. esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
  381. ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
  382. skb_push(skb, -skb_network_offset(skb));
  383. return esp6_output_tail(x, skb, &esp);
  384. }
  385. static inline int esp_remove_trailer(struct sk_buff *skb)
  386. {
  387. struct xfrm_state *x = xfrm_input_state(skb);
  388. struct xfrm_offload *xo = xfrm_offload(skb);
  389. struct crypto_aead *aead = x->data;
  390. int alen, hlen, elen;
  391. int padlen, trimlen;
  392. __wsum csumdiff;
  393. u8 nexthdr[2];
  394. int ret;
  395. alen = crypto_aead_authsize(aead);
  396. hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  397. elen = skb->len - hlen;
  398. if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
  399. ret = xo->proto;
  400. goto out;
  401. }
  402. ret = skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2);
  403. BUG_ON(ret);
  404. ret = -EINVAL;
  405. padlen = nexthdr[0];
  406. if (padlen + 2 + alen >= elen) {
  407. net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
  408. padlen + 2, elen - alen);
  409. goto out;
  410. }
  411. trimlen = alen + padlen + 2;
  412. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  413. csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
  414. skb->csum = csum_block_sub(skb->csum, csumdiff,
  415. skb->len - trimlen);
  416. }
  417. pskb_trim(skb, skb->len - trimlen);
  418. ret = nexthdr[1];
  419. out:
  420. return ret;
  421. }
  422. int esp6_input_done2(struct sk_buff *skb, int err)
  423. {
  424. struct xfrm_state *x = xfrm_input_state(skb);
  425. struct xfrm_offload *xo = xfrm_offload(skb);
  426. struct crypto_aead *aead = x->data;
  427. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  428. int hdr_len = skb_network_header_len(skb);
  429. if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
  430. kfree(ESP_SKB_CB(skb)->tmp);
  431. if (unlikely(err))
  432. goto out;
  433. err = esp_remove_trailer(skb);
  434. if (unlikely(err < 0))
  435. goto out;
  436. skb_postpull_rcsum(skb, skb_network_header(skb),
  437. skb_network_header_len(skb));
  438. skb_pull_rcsum(skb, hlen);
  439. if (x->props.mode == XFRM_MODE_TUNNEL)
  440. skb_reset_transport_header(skb);
  441. else
  442. skb_set_transport_header(skb, -hdr_len);
  443. /* RFC4303: Drop dummy packets without any error */
  444. if (err == IPPROTO_NONE)
  445. err = -EINVAL;
  446. out:
  447. return err;
  448. }
  449. EXPORT_SYMBOL_GPL(esp6_input_done2);
  450. static void esp_input_done(struct crypto_async_request *base, int err)
  451. {
  452. struct sk_buff *skb = base->data;
  453. xfrm_input_resume(skb, esp6_input_done2(skb, err));
  454. }
  455. static void esp_input_restore_header(struct sk_buff *skb)
  456. {
  457. esp_restore_header(skb, 0);
  458. __skb_pull(skb, 4);
  459. }
  460. static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
  461. {
  462. struct xfrm_state *x = xfrm_input_state(skb);
  463. /* For ESN we move the header forward by 4 bytes to
  464. * accomodate the high bits. We will move it back after
  465. * decryption.
  466. */
  467. if ((x->props.flags & XFRM_STATE_ESN)) {
  468. struct ip_esp_hdr *esph = skb_push(skb, 4);
  469. *seqhi = esph->spi;
  470. esph->spi = esph->seq_no;
  471. esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
  472. }
  473. }
  474. static void esp_input_done_esn(struct crypto_async_request *base, int err)
  475. {
  476. struct sk_buff *skb = base->data;
  477. esp_input_restore_header(skb);
  478. esp_input_done(base, err);
  479. }
  480. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  481. {
  482. struct ip_esp_hdr *esph;
  483. struct crypto_aead *aead = x->data;
  484. struct aead_request *req;
  485. struct sk_buff *trailer;
  486. int ivlen = crypto_aead_ivsize(aead);
  487. int elen = skb->len - sizeof(*esph) - ivlen;
  488. int nfrags;
  489. int assoclen;
  490. int seqhilen;
  491. int ret = 0;
  492. void *tmp;
  493. __be32 *seqhi;
  494. u8 *iv;
  495. struct scatterlist *sg;
  496. if (!pskb_may_pull(skb, sizeof(*esph) + ivlen)) {
  497. ret = -EINVAL;
  498. goto out;
  499. }
  500. if (elen <= 0) {
  501. ret = -EINVAL;
  502. goto out;
  503. }
  504. assoclen = sizeof(*esph);
  505. seqhilen = 0;
  506. if (x->props.flags & XFRM_STATE_ESN) {
  507. seqhilen += sizeof(__be32);
  508. assoclen += seqhilen;
  509. }
  510. if (!skb_cloned(skb)) {
  511. if (!skb_is_nonlinear(skb)) {
  512. nfrags = 1;
  513. goto skip_cow;
  514. } else if (!skb_has_frag_list(skb)) {
  515. nfrags = skb_shinfo(skb)->nr_frags;
  516. nfrags++;
  517. goto skip_cow;
  518. }
  519. }
  520. nfrags = skb_cow_data(skb, 0, &trailer);
  521. if (nfrags < 0) {
  522. ret = -EINVAL;
  523. goto out;
  524. }
  525. skip_cow:
  526. ret = -ENOMEM;
  527. tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
  528. if (!tmp)
  529. goto out;
  530. ESP_SKB_CB(skb)->tmp = tmp;
  531. seqhi = esp_tmp_seqhi(tmp);
  532. iv = esp_tmp_iv(aead, tmp, seqhilen);
  533. req = esp_tmp_req(aead, iv);
  534. sg = esp_req_sg(aead, req);
  535. esp_input_set_header(skb, seqhi);
  536. sg_init_table(sg, nfrags);
  537. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  538. if (unlikely(ret < 0)) {
  539. kfree(tmp);
  540. goto out;
  541. }
  542. skb->ip_summed = CHECKSUM_NONE;
  543. if ((x->props.flags & XFRM_STATE_ESN))
  544. aead_request_set_callback(req, 0, esp_input_done_esn, skb);
  545. else
  546. aead_request_set_callback(req, 0, esp_input_done, skb);
  547. aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
  548. aead_request_set_ad(req, assoclen);
  549. ret = crypto_aead_decrypt(req);
  550. if (ret == -EINPROGRESS)
  551. goto out;
  552. if ((x->props.flags & XFRM_STATE_ESN))
  553. esp_input_restore_header(skb);
  554. ret = esp6_input_done2(skb, ret);
  555. out:
  556. return ret;
  557. }
  558. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
  559. {
  560. struct crypto_aead *aead = x->data;
  561. u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  562. unsigned int net_adj;
  563. if (x->props.mode != XFRM_MODE_TUNNEL)
  564. net_adj = sizeof(struct ipv6hdr);
  565. else
  566. net_adj = 0;
  567. return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
  568. net_adj) & ~(blksize - 1)) + net_adj - 2;
  569. }
  570. static int esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  571. u8 type, u8 code, int offset, __be32 info)
  572. {
  573. struct net *net = dev_net(skb->dev);
  574. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  575. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  576. struct xfrm_state *x;
  577. if (type != ICMPV6_PKT_TOOBIG &&
  578. type != NDISC_REDIRECT)
  579. return 0;
  580. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  581. esph->spi, IPPROTO_ESP, AF_INET6);
  582. if (!x)
  583. return 0;
  584. if (type == NDISC_REDIRECT)
  585. ip6_redirect(skb, net, skb->dev->ifindex, 0,
  586. sock_net_uid(net, NULL));
  587. else
  588. ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
  589. xfrm_state_put(x);
  590. return 0;
  591. }
  592. static void esp6_destroy(struct xfrm_state *x)
  593. {
  594. struct crypto_aead *aead = x->data;
  595. if (!aead)
  596. return;
  597. crypto_free_aead(aead);
  598. }
  599. static int esp_init_aead(struct xfrm_state *x)
  600. {
  601. char aead_name[CRYPTO_MAX_ALG_NAME];
  602. struct crypto_aead *aead;
  603. int err;
  604. err = -ENAMETOOLONG;
  605. if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  606. x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
  607. goto error;
  608. aead = crypto_alloc_aead(aead_name, 0, 0);
  609. err = PTR_ERR(aead);
  610. if (IS_ERR(aead))
  611. goto error;
  612. x->data = aead;
  613. err = crypto_aead_setkey(aead, x->aead->alg_key,
  614. (x->aead->alg_key_len + 7) / 8);
  615. if (err)
  616. goto error;
  617. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  618. if (err)
  619. goto error;
  620. error:
  621. return err;
  622. }
  623. static int esp_init_authenc(struct xfrm_state *x)
  624. {
  625. struct crypto_aead *aead;
  626. struct crypto_authenc_key_param *param;
  627. struct rtattr *rta;
  628. char *key;
  629. char *p;
  630. char authenc_name[CRYPTO_MAX_ALG_NAME];
  631. unsigned int keylen;
  632. int err;
  633. err = -EINVAL;
  634. if (!x->ealg)
  635. goto error;
  636. err = -ENAMETOOLONG;
  637. if ((x->props.flags & XFRM_STATE_ESN)) {
  638. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  639. "%s%sauthencesn(%s,%s)%s",
  640. x->geniv ?: "", x->geniv ? "(" : "",
  641. x->aalg ? x->aalg->alg_name : "digest_null",
  642. x->ealg->alg_name,
  643. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  644. goto error;
  645. } else {
  646. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  647. "%s%sauthenc(%s,%s)%s",
  648. x->geniv ?: "", x->geniv ? "(" : "",
  649. x->aalg ? x->aalg->alg_name : "digest_null",
  650. x->ealg->alg_name,
  651. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  652. goto error;
  653. }
  654. aead = crypto_alloc_aead(authenc_name, 0, 0);
  655. err = PTR_ERR(aead);
  656. if (IS_ERR(aead))
  657. goto error;
  658. x->data = aead;
  659. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  660. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  661. err = -ENOMEM;
  662. key = kmalloc(keylen, GFP_KERNEL);
  663. if (!key)
  664. goto error;
  665. p = key;
  666. rta = (void *)p;
  667. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  668. rta->rta_len = RTA_LENGTH(sizeof(*param));
  669. param = RTA_DATA(rta);
  670. p += RTA_SPACE(sizeof(*param));
  671. if (x->aalg) {
  672. struct xfrm_algo_desc *aalg_desc;
  673. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  674. p += (x->aalg->alg_key_len + 7) / 8;
  675. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  676. BUG_ON(!aalg_desc);
  677. err = -EINVAL;
  678. if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
  679. crypto_aead_authsize(aead)) {
  680. pr_info("ESP: %s digestsize %u != %hu\n",
  681. x->aalg->alg_name,
  682. crypto_aead_authsize(aead),
  683. aalg_desc->uinfo.auth.icv_fullbits / 8);
  684. goto free_key;
  685. }
  686. err = crypto_aead_setauthsize(
  687. aead, x->aalg->alg_trunc_len / 8);
  688. if (err)
  689. goto free_key;
  690. }
  691. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  692. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  693. err = crypto_aead_setkey(aead, key, keylen);
  694. free_key:
  695. kfree(key);
  696. error:
  697. return err;
  698. }
  699. static int esp6_init_state(struct xfrm_state *x)
  700. {
  701. struct crypto_aead *aead;
  702. u32 align;
  703. int err;
  704. if (x->encap)
  705. return -EINVAL;
  706. x->data = NULL;
  707. if (x->aead)
  708. err = esp_init_aead(x);
  709. else
  710. err = esp_init_authenc(x);
  711. if (err)
  712. goto error;
  713. aead = x->data;
  714. x->props.header_len = sizeof(struct ip_esp_hdr) +
  715. crypto_aead_ivsize(aead);
  716. switch (x->props.mode) {
  717. case XFRM_MODE_BEET:
  718. if (x->sel.family != AF_INET6)
  719. x->props.header_len += IPV4_BEET_PHMAXLEN +
  720. (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
  721. break;
  722. default:
  723. case XFRM_MODE_TRANSPORT:
  724. break;
  725. case XFRM_MODE_TUNNEL:
  726. x->props.header_len += sizeof(struct ipv6hdr);
  727. break;
  728. }
  729. align = ALIGN(crypto_aead_blocksize(aead), 4);
  730. x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
  731. error:
  732. return err;
  733. }
  734. static int esp6_rcv_cb(struct sk_buff *skb, int err)
  735. {
  736. return 0;
  737. }
  738. static const struct xfrm_type esp6_type = {
  739. .description = "ESP6",
  740. .owner = THIS_MODULE,
  741. .proto = IPPROTO_ESP,
  742. .flags = XFRM_TYPE_REPLAY_PROT,
  743. .init_state = esp6_init_state,
  744. .destructor = esp6_destroy,
  745. .get_mtu = esp6_get_mtu,
  746. .input = esp6_input,
  747. .output = esp6_output,
  748. .hdr_offset = xfrm6_find_1stfragopt,
  749. };
  750. static struct xfrm6_protocol esp6_protocol = {
  751. .handler = xfrm6_rcv,
  752. .cb_handler = esp6_rcv_cb,
  753. .err_handler = esp6_err,
  754. .priority = 0,
  755. };
  756. static int __init esp6_init(void)
  757. {
  758. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  759. pr_info("%s: can't add xfrm type\n", __func__);
  760. return -EAGAIN;
  761. }
  762. if (xfrm6_protocol_register(&esp6_protocol, IPPROTO_ESP) < 0) {
  763. pr_info("%s: can't add protocol\n", __func__);
  764. xfrm_unregister_type(&esp6_type, AF_INET6);
  765. return -EAGAIN;
  766. }
  767. return 0;
  768. }
  769. static void __exit esp6_fini(void)
  770. {
  771. if (xfrm6_protocol_deregister(&esp6_protocol, IPPROTO_ESP) < 0)
  772. pr_info("%s: can't remove protocol\n", __func__);
  773. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  774. pr_info("%s: can't remove xfrm type\n", __func__);
  775. }
  776. module_init(esp6_init);
  777. module_exit(esp6_fini);
  778. MODULE_LICENSE("GPL");
  779. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);