esp6.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. struct esp_skb_cb {
  45. struct xfrm_skb_cb xfrm;
  46. void *tmp;
  47. };
  48. #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
  49. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu);
  50. /*
  51. * Allocate an AEAD request structure with extra space for SG and IV.
  52. *
  53. * For alignment considerations the upper 32 bits of the sequence number are
  54. * placed at the front, if present. Followed by the IV, the request and finally
  55. * the SG list.
  56. *
  57. * TODO: Use spare space in skb for this where possible.
  58. */
  59. static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int seqihlen)
  60. {
  61. unsigned int len;
  62. len = seqihlen;
  63. len += crypto_aead_ivsize(aead);
  64. if (len) {
  65. len += crypto_aead_alignmask(aead) &
  66. ~(crypto_tfm_ctx_alignment() - 1);
  67. len = ALIGN(len, crypto_tfm_ctx_alignment());
  68. }
  69. len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
  70. len = ALIGN(len, __alignof__(struct scatterlist));
  71. len += sizeof(struct scatterlist) * nfrags;
  72. return kmalloc(len, GFP_ATOMIC);
  73. }
  74. static inline __be32 *esp_tmp_seqhi(void *tmp)
  75. {
  76. return PTR_ALIGN((__be32 *)tmp, __alignof__(__be32));
  77. }
  78. static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int seqhilen)
  79. {
  80. return crypto_aead_ivsize(aead) ?
  81. PTR_ALIGN((u8 *)tmp + seqhilen,
  82. crypto_aead_alignmask(aead) + 1) : tmp + seqhilen;
  83. }
  84. static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
  85. {
  86. struct aead_request *req;
  87. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  88. crypto_tfm_ctx_alignment());
  89. aead_request_set_tfm(req, aead);
  90. return req;
  91. }
  92. static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
  93. struct aead_request *req)
  94. {
  95. return (void *)ALIGN((unsigned long)(req + 1) +
  96. crypto_aead_reqsize(aead),
  97. __alignof__(struct scatterlist));
  98. }
  99. static void esp_output_done(struct crypto_async_request *base, int err)
  100. {
  101. struct sk_buff *skb = base->data;
  102. kfree(ESP_SKB_CB(skb)->tmp);
  103. xfrm_output_resume(skb, err);
  104. }
  105. /* Move ESP header back into place. */
  106. static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
  107. {
  108. struct ip_esp_hdr *esph = (void *)(skb->data + offset);
  109. void *tmp = ESP_SKB_CB(skb)->tmp;
  110. __be32 *seqhi = esp_tmp_seqhi(tmp);
  111. esph->seq_no = esph->spi;
  112. esph->spi = *seqhi;
  113. }
  114. static void esp_output_restore_header(struct sk_buff *skb)
  115. {
  116. esp_restore_header(skb, skb_transport_offset(skb) - sizeof(__be32));
  117. }
  118. static void esp_output_done_esn(struct crypto_async_request *base, int err)
  119. {
  120. struct sk_buff *skb = base->data;
  121. esp_output_restore_header(skb);
  122. esp_output_done(base, err);
  123. }
  124. static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
  125. {
  126. int err;
  127. struct ip_esp_hdr *esph;
  128. struct crypto_aead *aead;
  129. struct aead_request *req;
  130. struct scatterlist *sg;
  131. struct sk_buff *trailer;
  132. void *tmp;
  133. int blksize;
  134. int clen;
  135. int alen;
  136. int plen;
  137. int ivlen;
  138. int tfclen;
  139. int nfrags;
  140. int assoclen;
  141. int seqhilen;
  142. u8 *iv;
  143. u8 *tail;
  144. __be32 *seqhi;
  145. __be64 seqno;
  146. /* skb is pure payload to encrypt */
  147. aead = x->data;
  148. alen = crypto_aead_authsize(aead);
  149. ivlen = crypto_aead_ivsize(aead);
  150. tfclen = 0;
  151. if (x->tfcpad) {
  152. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  153. u32 padto;
  154. padto = min(x->tfcpad, esp6_get_mtu(x, dst->child_mtu_cached));
  155. if (skb->len < padto)
  156. tfclen = padto - skb->len;
  157. }
  158. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  159. clen = ALIGN(skb->len + 2 + tfclen, blksize);
  160. plen = clen - skb->len - tfclen;
  161. err = skb_cow_data(skb, tfclen + plen + alen, &trailer);
  162. if (err < 0)
  163. goto error;
  164. nfrags = err;
  165. assoclen = sizeof(*esph);
  166. seqhilen = 0;
  167. if (x->props.flags & XFRM_STATE_ESN) {
  168. seqhilen += sizeof(__be32);
  169. assoclen += seqhilen;
  170. }
  171. tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
  172. if (!tmp) {
  173. err = -ENOMEM;
  174. goto error;
  175. }
  176. seqhi = esp_tmp_seqhi(tmp);
  177. iv = esp_tmp_iv(aead, tmp, seqhilen);
  178. req = esp_tmp_req(aead, iv);
  179. sg = esp_req_sg(aead, req);
  180. /* Fill padding... */
  181. tail = skb_tail_pointer(trailer);
  182. if (tfclen) {
  183. memset(tail, 0, tfclen);
  184. tail += tfclen;
  185. }
  186. do {
  187. int i;
  188. for (i = 0; i < plen - 2; i++)
  189. tail[i] = i + 1;
  190. } while (0);
  191. tail[plen - 2] = plen - 2;
  192. tail[plen - 1] = *skb_mac_header(skb);
  193. pskb_put(skb, trailer, clen - skb->len + alen);
  194. skb_push(skb, -skb_network_offset(skb));
  195. esph = ip_esp_hdr(skb);
  196. *skb_mac_header(skb) = IPPROTO_ESP;
  197. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  198. aead_request_set_callback(req, 0, esp_output_done, skb);
  199. /* For ESN we move the header forward by 4 bytes to
  200. * accomodate the high bits. We will move it back after
  201. * encryption.
  202. */
  203. if ((x->props.flags & XFRM_STATE_ESN)) {
  204. esph = (void *)(skb_transport_header(skb) - sizeof(__be32));
  205. *seqhi = esph->spi;
  206. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
  207. aead_request_set_callback(req, 0, esp_output_done_esn, skb);
  208. }
  209. esph->spi = x->id.spi;
  210. sg_init_table(sg, nfrags);
  211. err = skb_to_sgvec(skb, sg,
  212. (unsigned char *)esph - skb->data,
  213. assoclen + ivlen + clen + alen);
  214. if (unlikely(err < 0))
  215. goto error;
  216. aead_request_set_crypt(req, sg, sg, ivlen + clen, iv);
  217. aead_request_set_ad(req, assoclen);
  218. seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
  219. ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
  220. memset(iv, 0, ivlen);
  221. memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&seqno + 8 - min(ivlen, 8),
  222. min(ivlen, 8));
  223. ESP_SKB_CB(skb)->tmp = tmp;
  224. err = crypto_aead_encrypt(req);
  225. switch (err) {
  226. case -EINPROGRESS:
  227. goto error;
  228. case -EBUSY:
  229. err = NET_XMIT_DROP;
  230. break;
  231. case 0:
  232. if ((x->props.flags & XFRM_STATE_ESN))
  233. esp_output_restore_header(skb);
  234. }
  235. kfree(tmp);
  236. error:
  237. return err;
  238. }
  239. static int esp_input_done2(struct sk_buff *skb, int err)
  240. {
  241. struct xfrm_state *x = xfrm_input_state(skb);
  242. struct crypto_aead *aead = x->data;
  243. int alen = crypto_aead_authsize(aead);
  244. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  245. int elen = skb->len - hlen;
  246. int hdr_len = skb_network_header_len(skb);
  247. int padlen;
  248. u8 nexthdr[2];
  249. kfree(ESP_SKB_CB(skb)->tmp);
  250. if (unlikely(err))
  251. goto out;
  252. if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
  253. BUG();
  254. err = -EINVAL;
  255. padlen = nexthdr[0];
  256. if (padlen + 2 + alen >= elen) {
  257. net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
  258. padlen + 2, elen - alen);
  259. goto out;
  260. }
  261. /* ... check padding bits here. Silly. :-) */
  262. pskb_trim(skb, skb->len - alen - padlen - 2);
  263. __skb_pull(skb, hlen);
  264. if (x->props.mode == XFRM_MODE_TUNNEL)
  265. skb_reset_transport_header(skb);
  266. else
  267. skb_set_transport_header(skb, -hdr_len);
  268. err = nexthdr[1];
  269. /* RFC4303: Drop dummy packets without any error */
  270. if (err == IPPROTO_NONE)
  271. err = -EINVAL;
  272. out:
  273. return err;
  274. }
  275. static void esp_input_done(struct crypto_async_request *base, int err)
  276. {
  277. struct sk_buff *skb = base->data;
  278. xfrm_input_resume(skb, esp_input_done2(skb, err));
  279. }
  280. static void esp_input_restore_header(struct sk_buff *skb)
  281. {
  282. esp_restore_header(skb, 0);
  283. __skb_pull(skb, 4);
  284. }
  285. static void esp_input_done_esn(struct crypto_async_request *base, int err)
  286. {
  287. struct sk_buff *skb = base->data;
  288. esp_input_restore_header(skb);
  289. esp_input_done(base, err);
  290. }
  291. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  292. {
  293. struct ip_esp_hdr *esph;
  294. struct crypto_aead *aead = x->data;
  295. struct aead_request *req;
  296. struct sk_buff *trailer;
  297. int ivlen = crypto_aead_ivsize(aead);
  298. int elen = skb->len - sizeof(*esph) - ivlen;
  299. int nfrags;
  300. int assoclen;
  301. int seqhilen;
  302. int ret = 0;
  303. void *tmp;
  304. __be32 *seqhi;
  305. u8 *iv;
  306. struct scatterlist *sg;
  307. if (!pskb_may_pull(skb, sizeof(*esph) + ivlen)) {
  308. ret = -EINVAL;
  309. goto out;
  310. }
  311. if (elen <= 0) {
  312. ret = -EINVAL;
  313. goto out;
  314. }
  315. nfrags = skb_cow_data(skb, 0, &trailer);
  316. if (nfrags < 0) {
  317. ret = -EINVAL;
  318. goto out;
  319. }
  320. ret = -ENOMEM;
  321. assoclen = sizeof(*esph);
  322. seqhilen = 0;
  323. if (x->props.flags & XFRM_STATE_ESN) {
  324. seqhilen += sizeof(__be32);
  325. assoclen += seqhilen;
  326. }
  327. tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
  328. if (!tmp)
  329. goto out;
  330. ESP_SKB_CB(skb)->tmp = tmp;
  331. seqhi = esp_tmp_seqhi(tmp);
  332. iv = esp_tmp_iv(aead, tmp, seqhilen);
  333. req = esp_tmp_req(aead, iv);
  334. sg = esp_req_sg(aead, req);
  335. skb->ip_summed = CHECKSUM_NONE;
  336. esph = (struct ip_esp_hdr *)skb->data;
  337. aead_request_set_callback(req, 0, esp_input_done, skb);
  338. /* For ESN we move the header forward by 4 bytes to
  339. * accomodate the high bits. We will move it back after
  340. * decryption.
  341. */
  342. if ((x->props.flags & XFRM_STATE_ESN)) {
  343. esph = (void *)skb_push(skb, 4);
  344. *seqhi = esph->spi;
  345. esph->spi = esph->seq_no;
  346. esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
  347. aead_request_set_callback(req, 0, esp_input_done_esn, skb);
  348. }
  349. sg_init_table(sg, nfrags);
  350. ret = skb_to_sgvec(skb, sg, 0, skb->len);
  351. if (unlikely(ret < 0))
  352. goto out;
  353. aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
  354. aead_request_set_ad(req, assoclen);
  355. ret = crypto_aead_decrypt(req);
  356. if (ret == -EINPROGRESS)
  357. goto out;
  358. if ((x->props.flags & XFRM_STATE_ESN))
  359. esp_input_restore_header(skb);
  360. ret = esp_input_done2(skb, ret);
  361. out:
  362. return ret;
  363. }
  364. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
  365. {
  366. struct crypto_aead *aead = x->data;
  367. u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  368. unsigned int net_adj;
  369. if (x->props.mode != XFRM_MODE_TUNNEL)
  370. net_adj = sizeof(struct ipv6hdr);
  371. else
  372. net_adj = 0;
  373. return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
  374. net_adj) & ~(blksize - 1)) + net_adj - 2;
  375. }
  376. static int esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  377. u8 type, u8 code, int offset, __be32 info)
  378. {
  379. struct net *net = dev_net(skb->dev);
  380. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  381. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  382. struct xfrm_state *x;
  383. if (type != ICMPV6_PKT_TOOBIG &&
  384. type != NDISC_REDIRECT)
  385. return 0;
  386. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  387. esph->spi, IPPROTO_ESP, AF_INET6);
  388. if (!x)
  389. return 0;
  390. if (type == NDISC_REDIRECT)
  391. ip6_redirect(skb, net, skb->dev->ifindex, 0);
  392. else
  393. ip6_update_pmtu(skb, net, info, 0, 0);
  394. xfrm_state_put(x);
  395. return 0;
  396. }
  397. static void esp6_destroy(struct xfrm_state *x)
  398. {
  399. struct crypto_aead *aead = x->data;
  400. if (!aead)
  401. return;
  402. crypto_free_aead(aead);
  403. }
  404. static int esp_init_aead(struct xfrm_state *x)
  405. {
  406. char aead_name[CRYPTO_MAX_ALG_NAME];
  407. struct crypto_aead *aead;
  408. int err;
  409. err = -ENAMETOOLONG;
  410. if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  411. x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
  412. goto error;
  413. aead = crypto_alloc_aead(aead_name, 0, 0);
  414. err = PTR_ERR(aead);
  415. if (IS_ERR(aead))
  416. goto error;
  417. x->data = aead;
  418. err = crypto_aead_setkey(aead, x->aead->alg_key,
  419. (x->aead->alg_key_len + 7) / 8);
  420. if (err)
  421. goto error;
  422. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  423. if (err)
  424. goto error;
  425. error:
  426. return err;
  427. }
  428. static int esp_init_authenc(struct xfrm_state *x)
  429. {
  430. struct crypto_aead *aead;
  431. struct crypto_authenc_key_param *param;
  432. struct rtattr *rta;
  433. char *key;
  434. char *p;
  435. char authenc_name[CRYPTO_MAX_ALG_NAME];
  436. unsigned int keylen;
  437. int err;
  438. err = -EINVAL;
  439. if (!x->ealg)
  440. goto error;
  441. err = -ENAMETOOLONG;
  442. if ((x->props.flags & XFRM_STATE_ESN)) {
  443. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  444. "%s%sauthencesn(%s,%s)%s",
  445. x->geniv ?: "", x->geniv ? "(" : "",
  446. x->aalg ? x->aalg->alg_name : "digest_null",
  447. x->ealg->alg_name,
  448. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  449. goto error;
  450. } else {
  451. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  452. "%s%sauthenc(%s,%s)%s",
  453. x->geniv ?: "", x->geniv ? "(" : "",
  454. x->aalg ? x->aalg->alg_name : "digest_null",
  455. x->ealg->alg_name,
  456. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  457. goto error;
  458. }
  459. aead = crypto_alloc_aead(authenc_name, 0, 0);
  460. err = PTR_ERR(aead);
  461. if (IS_ERR(aead))
  462. goto error;
  463. x->data = aead;
  464. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  465. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  466. err = -ENOMEM;
  467. key = kmalloc(keylen, GFP_KERNEL);
  468. if (!key)
  469. goto error;
  470. p = key;
  471. rta = (void *)p;
  472. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  473. rta->rta_len = RTA_LENGTH(sizeof(*param));
  474. param = RTA_DATA(rta);
  475. p += RTA_SPACE(sizeof(*param));
  476. if (x->aalg) {
  477. struct xfrm_algo_desc *aalg_desc;
  478. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  479. p += (x->aalg->alg_key_len + 7) / 8;
  480. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  481. BUG_ON(!aalg_desc);
  482. err = -EINVAL;
  483. if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
  484. crypto_aead_authsize(aead)) {
  485. pr_info("ESP: %s digestsize %u != %hu\n",
  486. x->aalg->alg_name,
  487. crypto_aead_authsize(aead),
  488. aalg_desc->uinfo.auth.icv_fullbits / 8);
  489. goto free_key;
  490. }
  491. err = crypto_aead_setauthsize(
  492. aead, x->aalg->alg_trunc_len / 8);
  493. if (err)
  494. goto free_key;
  495. }
  496. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  497. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  498. err = crypto_aead_setkey(aead, key, keylen);
  499. free_key:
  500. kfree(key);
  501. error:
  502. return err;
  503. }
  504. static int esp6_init_state(struct xfrm_state *x)
  505. {
  506. struct crypto_aead *aead;
  507. u32 align;
  508. int err;
  509. if (x->encap)
  510. return -EINVAL;
  511. x->data = NULL;
  512. if (x->aead)
  513. err = esp_init_aead(x);
  514. else
  515. err = esp_init_authenc(x);
  516. if (err)
  517. goto error;
  518. aead = x->data;
  519. x->props.header_len = sizeof(struct ip_esp_hdr) +
  520. crypto_aead_ivsize(aead);
  521. switch (x->props.mode) {
  522. case XFRM_MODE_BEET:
  523. if (x->sel.family != AF_INET6)
  524. x->props.header_len += IPV4_BEET_PHMAXLEN +
  525. (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
  526. break;
  527. case XFRM_MODE_TRANSPORT:
  528. break;
  529. case XFRM_MODE_TUNNEL:
  530. x->props.header_len += sizeof(struct ipv6hdr);
  531. break;
  532. default:
  533. goto error;
  534. }
  535. align = ALIGN(crypto_aead_blocksize(aead), 4);
  536. x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
  537. error:
  538. return err;
  539. }
  540. static int esp6_rcv_cb(struct sk_buff *skb, int err)
  541. {
  542. return 0;
  543. }
  544. static const struct xfrm_type esp6_type = {
  545. .description = "ESP6",
  546. .owner = THIS_MODULE,
  547. .proto = IPPROTO_ESP,
  548. .flags = XFRM_TYPE_REPLAY_PROT,
  549. .init_state = esp6_init_state,
  550. .destructor = esp6_destroy,
  551. .get_mtu = esp6_get_mtu,
  552. .input = esp6_input,
  553. .output = esp6_output,
  554. .hdr_offset = xfrm6_find_1stfragopt,
  555. };
  556. static struct xfrm6_protocol esp6_protocol = {
  557. .handler = xfrm6_rcv,
  558. .cb_handler = esp6_rcv_cb,
  559. .err_handler = esp6_err,
  560. .priority = 0,
  561. };
  562. static int __init esp6_init(void)
  563. {
  564. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  565. pr_info("%s: can't add xfrm type\n", __func__);
  566. return -EAGAIN;
  567. }
  568. if (xfrm6_protocol_register(&esp6_protocol, IPPROTO_ESP) < 0) {
  569. pr_info("%s: can't add protocol\n", __func__);
  570. xfrm_unregister_type(&esp6_type, AF_INET6);
  571. return -EAGAIN;
  572. }
  573. return 0;
  574. }
  575. static void __exit esp6_fini(void)
  576. {
  577. if (xfrm6_protocol_deregister(&esp6_protocol, IPPROTO_ESP) < 0)
  578. pr_info("%s: can't remove protocol\n", __func__);
  579. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  580. pr_info("%s: can't remove xfrm type\n", __func__);
  581. }
  582. module_init(esp6_init);
  583. module_exit(esp6_fini);
  584. MODULE_LICENSE("GPL");
  585. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);