esp6.c 22 KB

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