esp6.c 20 KB

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