esp6.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Authors
  19. *
  20. * Mitsuru KANDA @USAGI : IPv6 Support
  21. * Kazunori MIYAZAWA @USAGI :
  22. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  23. *
  24. * This file is derived from net/ipv4/esp.c
  25. */
  26. #define pr_fmt(fmt) "IPv6: " fmt
  27. #include <crypto/aead.h>
  28. #include <crypto/authenc.h>
  29. #include <linux/err.h>
  30. #include <linux/module.h>
  31. #include <net/ip.h>
  32. #include <net/xfrm.h>
  33. #include <net/esp.h>
  34. #include <linux/scatterlist.h>
  35. #include <linux/kernel.h>
  36. #include <linux/pfkeyv2.h>
  37. #include <linux/random.h>
  38. #include <linux/slab.h>
  39. #include <linux/spinlock.h>
  40. #include <net/ip6_route.h>
  41. #include <net/icmp.h>
  42. #include <net/ipv6.h>
  43. #include <net/protocol.h>
  44. #include <linux/icmpv6.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_givcrypt_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_givcrypt_request *esp_tmp_givreq(
  86. struct crypto_aead *aead, u8 *iv)
  87. {
  88. struct aead_givcrypt_request *req;
  89. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  90. crypto_tfm_ctx_alignment());
  91. aead_givcrypt_set_tfm(req, aead);
  92. return req;
  93. }
  94. static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
  95. {
  96. struct aead_request *req;
  97. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  98. crypto_tfm_ctx_alignment());
  99. aead_request_set_tfm(req, aead);
  100. return req;
  101. }
  102. static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
  103. struct aead_request *req)
  104. {
  105. return (void *)ALIGN((unsigned long)(req + 1) +
  106. crypto_aead_reqsize(aead),
  107. __alignof__(struct scatterlist));
  108. }
  109. static inline struct scatterlist *esp_givreq_sg(
  110. struct crypto_aead *aead, struct aead_givcrypt_request *req)
  111. {
  112. return (void *)ALIGN((unsigned long)(req + 1) +
  113. crypto_aead_reqsize(aead),
  114. __alignof__(struct scatterlist));
  115. }
  116. static void esp_output_done(struct crypto_async_request *base, int err)
  117. {
  118. struct sk_buff *skb = base->data;
  119. kfree(ESP_SKB_CB(skb)->tmp);
  120. xfrm_output_resume(skb, err);
  121. }
  122. static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
  123. {
  124. int err;
  125. struct ip_esp_hdr *esph;
  126. struct crypto_aead *aead;
  127. struct aead_givcrypt_request *req;
  128. struct scatterlist *sg;
  129. struct scatterlist *asg;
  130. struct sk_buff *trailer;
  131. void *tmp;
  132. int blksize;
  133. int clen;
  134. int alen;
  135. int plen;
  136. int tfclen;
  137. int nfrags;
  138. int assoclen;
  139. int sglists;
  140. int seqhilen;
  141. u8 *iv;
  142. u8 *tail;
  143. __be32 *seqhi;
  144. struct esp_data *esp = x->data;
  145. /* skb is pure payload to encrypt */
  146. aead = esp->aead;
  147. alen = crypto_aead_authsize(aead);
  148. tfclen = 0;
  149. if (x->tfcpad) {
  150. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  151. u32 padto;
  152. padto = min(x->tfcpad, esp6_get_mtu(x, dst->child_mtu_cached));
  153. if (skb->len < padto)
  154. tfclen = padto - skb->len;
  155. }
  156. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  157. clen = ALIGN(skb->len + 2 + tfclen, blksize);
  158. if (esp->padlen)
  159. clen = ALIGN(clen, esp->padlen);
  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. sglists = 1;
  167. seqhilen = 0;
  168. if (x->props.flags & XFRM_STATE_ESN) {
  169. sglists += 2;
  170. seqhilen += sizeof(__be32);
  171. assoclen += seqhilen;
  172. }
  173. tmp = esp_alloc_tmp(aead, nfrags + sglists, seqhilen);
  174. if (!tmp) {
  175. err = -ENOMEM;
  176. goto error;
  177. }
  178. seqhi = esp_tmp_seqhi(tmp);
  179. iv = esp_tmp_iv(aead, tmp, seqhilen);
  180. req = esp_tmp_givreq(aead, iv);
  181. asg = esp_givreq_sg(aead, req);
  182. sg = asg + sglists;
  183. /* Fill padding... */
  184. tail = skb_tail_pointer(trailer);
  185. if (tfclen) {
  186. memset(tail, 0, tfclen);
  187. tail += tfclen;
  188. }
  189. do {
  190. int i;
  191. for (i = 0; i < plen - 2; i++)
  192. tail[i] = i + 1;
  193. } while (0);
  194. tail[plen - 2] = plen - 2;
  195. tail[plen - 1] = *skb_mac_header(skb);
  196. pskb_put(skb, trailer, clen - skb->len + alen);
  197. skb_push(skb, -skb_network_offset(skb));
  198. esph = ip_esp_hdr(skb);
  199. *skb_mac_header(skb) = IPPROTO_ESP;
  200. esph->spi = x->id.spi;
  201. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  202. sg_init_table(sg, nfrags);
  203. skb_to_sgvec(skb, sg,
  204. esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
  205. clen + alen);
  206. if ((x->props.flags & XFRM_STATE_ESN)) {
  207. sg_init_table(asg, 3);
  208. sg_set_buf(asg, &esph->spi, sizeof(__be32));
  209. *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
  210. sg_set_buf(asg + 1, seqhi, seqhilen);
  211. sg_set_buf(asg + 2, &esph->seq_no, sizeof(__be32));
  212. } else
  213. sg_init_one(asg, esph, sizeof(*esph));
  214. aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
  215. aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
  216. aead_givcrypt_set_assoc(req, asg, assoclen);
  217. aead_givcrypt_set_giv(req, esph->enc_data,
  218. XFRM_SKB_CB(skb)->seq.output.low);
  219. ESP_SKB_CB(skb)->tmp = tmp;
  220. err = crypto_aead_givencrypt(req);
  221. if (err == -EINPROGRESS)
  222. goto error;
  223. if (err == -EBUSY)
  224. err = NET_XMIT_DROP;
  225. kfree(tmp);
  226. error:
  227. return err;
  228. }
  229. static int esp_input_done2(struct sk_buff *skb, int err)
  230. {
  231. struct xfrm_state *x = xfrm_input_state(skb);
  232. struct esp_data *esp = x->data;
  233. struct crypto_aead *aead = esp->aead;
  234. int alen = crypto_aead_authsize(aead);
  235. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  236. int elen = skb->len - hlen;
  237. int hdr_len = skb_network_header_len(skb);
  238. int padlen;
  239. u8 nexthdr[2];
  240. kfree(ESP_SKB_CB(skb)->tmp);
  241. if (unlikely(err))
  242. goto out;
  243. if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
  244. BUG();
  245. err = -EINVAL;
  246. padlen = nexthdr[0];
  247. if (padlen + 2 + alen >= elen) {
  248. LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage "
  249. "padlen=%d, elen=%d\n", padlen + 2, elen - alen);
  250. goto out;
  251. }
  252. /* ... check padding bits here. Silly. :-) */
  253. pskb_trim(skb, skb->len - alen - padlen - 2);
  254. __skb_pull(skb, hlen);
  255. if (x->props.mode == XFRM_MODE_TUNNEL)
  256. skb_reset_transport_header(skb);
  257. else
  258. skb_set_transport_header(skb, -hdr_len);
  259. err = nexthdr[1];
  260. /* RFC4303: Drop dummy packets without any error */
  261. if (err == IPPROTO_NONE)
  262. err = -EINVAL;
  263. out:
  264. return err;
  265. }
  266. static void esp_input_done(struct crypto_async_request *base, int err)
  267. {
  268. struct sk_buff *skb = base->data;
  269. xfrm_input_resume(skb, esp_input_done2(skb, err));
  270. }
  271. static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
  272. {
  273. struct ip_esp_hdr *esph;
  274. struct esp_data *esp = x->data;
  275. struct crypto_aead *aead = esp->aead;
  276. struct aead_request *req;
  277. struct sk_buff *trailer;
  278. int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
  279. int nfrags;
  280. int assoclen;
  281. int sglists;
  282. int seqhilen;
  283. int ret = 0;
  284. void *tmp;
  285. __be32 *seqhi;
  286. u8 *iv;
  287. struct scatterlist *sg;
  288. struct scatterlist *asg;
  289. if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead))) {
  290. ret = -EINVAL;
  291. goto out;
  292. }
  293. if (elen <= 0) {
  294. ret = -EINVAL;
  295. goto out;
  296. }
  297. if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
  298. ret = -EINVAL;
  299. goto out;
  300. }
  301. ret = -ENOMEM;
  302. assoclen = sizeof(*esph);
  303. sglists = 1;
  304. seqhilen = 0;
  305. if (x->props.flags & XFRM_STATE_ESN) {
  306. sglists += 2;
  307. seqhilen += sizeof(__be32);
  308. assoclen += seqhilen;
  309. }
  310. tmp = esp_alloc_tmp(aead, nfrags + sglists, seqhilen);
  311. if (!tmp)
  312. goto out;
  313. ESP_SKB_CB(skb)->tmp = tmp;
  314. seqhi = esp_tmp_seqhi(tmp);
  315. iv = esp_tmp_iv(aead, tmp, seqhilen);
  316. req = esp_tmp_req(aead, iv);
  317. asg = esp_req_sg(aead, req);
  318. sg = asg + sglists;
  319. skb->ip_summed = CHECKSUM_NONE;
  320. esph = (struct ip_esp_hdr *)skb->data;
  321. /* Get ivec. This can be wrong, check against another impls. */
  322. iv = esph->enc_data;
  323. sg_init_table(sg, nfrags);
  324. skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
  325. if ((x->props.flags & XFRM_STATE_ESN)) {
  326. sg_init_table(asg, 3);
  327. sg_set_buf(asg, &esph->spi, sizeof(__be32));
  328. *seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
  329. sg_set_buf(asg + 1, seqhi, seqhilen);
  330. sg_set_buf(asg + 2, &esph->seq_no, sizeof(__be32));
  331. } else
  332. sg_init_one(asg, esph, sizeof(*esph));
  333. aead_request_set_callback(req, 0, esp_input_done, skb);
  334. aead_request_set_crypt(req, sg, sg, elen, iv);
  335. aead_request_set_assoc(req, asg, assoclen);
  336. ret = crypto_aead_decrypt(req);
  337. if (ret == -EINPROGRESS)
  338. goto out;
  339. ret = esp_input_done2(skb, ret);
  340. out:
  341. return ret;
  342. }
  343. static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
  344. {
  345. struct esp_data *esp = x->data;
  346. u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
  347. u32 align = max_t(u32, blksize, esp->padlen);
  348. unsigned int net_adj;
  349. if (x->props.mode != XFRM_MODE_TUNNEL)
  350. net_adj = sizeof(struct ipv6hdr);
  351. else
  352. net_adj = 0;
  353. return ((mtu - x->props.header_len - crypto_aead_authsize(esp->aead) -
  354. net_adj) & ~(align - 1)) + (net_adj - 2);
  355. }
  356. static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  357. u8 type, u8 code, int offset, __be32 info)
  358. {
  359. struct net *net = dev_net(skb->dev);
  360. const struct ipv6hdr *iph = (const struct ipv6hdr *)skb->data;
  361. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
  362. struct xfrm_state *x;
  363. if (type != ICMPV6_DEST_UNREACH &&
  364. type != ICMPV6_PKT_TOOBIG &&
  365. type != NDISC_REDIRECT)
  366. return;
  367. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  368. esph->spi, IPPROTO_ESP, AF_INET6);
  369. if (!x)
  370. return;
  371. if (type == NDISC_REDIRECT)
  372. ip6_redirect(skb, net, 0, 0);
  373. else
  374. ip6_update_pmtu(skb, net, info, 0, 0);
  375. xfrm_state_put(x);
  376. }
  377. static void esp6_destroy(struct xfrm_state *x)
  378. {
  379. struct esp_data *esp = x->data;
  380. if (!esp)
  381. return;
  382. crypto_free_aead(esp->aead);
  383. kfree(esp);
  384. }
  385. static int esp_init_aead(struct xfrm_state *x)
  386. {
  387. struct esp_data *esp = x->data;
  388. struct crypto_aead *aead;
  389. int err;
  390. aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
  391. err = PTR_ERR(aead);
  392. if (IS_ERR(aead))
  393. goto error;
  394. esp->aead = aead;
  395. err = crypto_aead_setkey(aead, x->aead->alg_key,
  396. (x->aead->alg_key_len + 7) / 8);
  397. if (err)
  398. goto error;
  399. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  400. if (err)
  401. goto error;
  402. error:
  403. return err;
  404. }
  405. static int esp_init_authenc(struct xfrm_state *x)
  406. {
  407. struct esp_data *esp = x->data;
  408. struct crypto_aead *aead;
  409. struct crypto_authenc_key_param *param;
  410. struct rtattr *rta;
  411. char *key;
  412. char *p;
  413. char authenc_name[CRYPTO_MAX_ALG_NAME];
  414. unsigned int keylen;
  415. int err;
  416. err = -EINVAL;
  417. if (x->ealg == NULL)
  418. goto error;
  419. err = -ENAMETOOLONG;
  420. if ((x->props.flags & XFRM_STATE_ESN)) {
  421. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  422. "authencesn(%s,%s)",
  423. x->aalg ? x->aalg->alg_name : "digest_null",
  424. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  425. goto error;
  426. } else {
  427. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  428. "authenc(%s,%s)",
  429. x->aalg ? x->aalg->alg_name : "digest_null",
  430. x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
  431. goto error;
  432. }
  433. aead = crypto_alloc_aead(authenc_name, 0, 0);
  434. err = PTR_ERR(aead);
  435. if (IS_ERR(aead))
  436. goto error;
  437. esp->aead = aead;
  438. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  439. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  440. err = -ENOMEM;
  441. key = kmalloc(keylen, GFP_KERNEL);
  442. if (!key)
  443. goto error;
  444. p = key;
  445. rta = (void *)p;
  446. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  447. rta->rta_len = RTA_LENGTH(sizeof(*param));
  448. param = RTA_DATA(rta);
  449. p += RTA_SPACE(sizeof(*param));
  450. if (x->aalg) {
  451. struct xfrm_algo_desc *aalg_desc;
  452. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  453. p += (x->aalg->alg_key_len + 7) / 8;
  454. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  455. BUG_ON(!aalg_desc);
  456. err = -EINVAL;
  457. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  458. crypto_aead_authsize(aead)) {
  459. NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
  460. x->aalg->alg_name,
  461. crypto_aead_authsize(aead),
  462. aalg_desc->uinfo.auth.icv_fullbits/8);
  463. goto free_key;
  464. }
  465. err = crypto_aead_setauthsize(
  466. aead, x->aalg->alg_trunc_len / 8);
  467. if (err)
  468. goto free_key;
  469. }
  470. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  471. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  472. err = crypto_aead_setkey(aead, key, keylen);
  473. free_key:
  474. kfree(key);
  475. error:
  476. return err;
  477. }
  478. static int esp6_init_state(struct xfrm_state *x)
  479. {
  480. struct esp_data *esp;
  481. struct crypto_aead *aead;
  482. u32 align;
  483. int err;
  484. if (x->encap)
  485. return -EINVAL;
  486. esp = kzalloc(sizeof(*esp), GFP_KERNEL);
  487. if (esp == NULL)
  488. return -ENOMEM;
  489. x->data = esp;
  490. if (x->aead)
  491. err = esp_init_aead(x);
  492. else
  493. err = esp_init_authenc(x);
  494. if (err)
  495. goto error;
  496. aead = esp->aead;
  497. esp->padlen = 0;
  498. x->props.header_len = sizeof(struct ip_esp_hdr) +
  499. crypto_aead_ivsize(aead);
  500. switch (x->props.mode) {
  501. case XFRM_MODE_BEET:
  502. if (x->sel.family != AF_INET6)
  503. x->props.header_len += IPV4_BEET_PHMAXLEN +
  504. (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
  505. break;
  506. case XFRM_MODE_TRANSPORT:
  507. break;
  508. case XFRM_MODE_TUNNEL:
  509. x->props.header_len += sizeof(struct ipv6hdr);
  510. break;
  511. default:
  512. goto error;
  513. }
  514. align = ALIGN(crypto_aead_blocksize(aead), 4);
  515. if (esp->padlen)
  516. align = max_t(u32, align, esp->padlen);
  517. x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
  518. error:
  519. return err;
  520. }
  521. static const struct xfrm_type esp6_type =
  522. {
  523. .description = "ESP6",
  524. .owner = THIS_MODULE,
  525. .proto = IPPROTO_ESP,
  526. .flags = XFRM_TYPE_REPLAY_PROT,
  527. .init_state = esp6_init_state,
  528. .destructor = esp6_destroy,
  529. .get_mtu = esp6_get_mtu,
  530. .input = esp6_input,
  531. .output = esp6_output,
  532. .hdr_offset = xfrm6_find_1stfragopt,
  533. };
  534. static const struct inet6_protocol esp6_protocol = {
  535. .handler = xfrm6_rcv,
  536. .err_handler = esp6_err,
  537. .flags = INET6_PROTO_NOPOLICY,
  538. };
  539. static int __init esp6_init(void)
  540. {
  541. if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
  542. pr_info("%s: can't add xfrm type\n", __func__);
  543. return -EAGAIN;
  544. }
  545. if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
  546. pr_info("%s: can't add protocol\n", __func__);
  547. xfrm_unregister_type(&esp6_type, AF_INET6);
  548. return -EAGAIN;
  549. }
  550. return 0;
  551. }
  552. static void __exit esp6_fini(void)
  553. {
  554. if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
  555. pr_info("%s: can't remove protocol\n", __func__);
  556. if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
  557. pr_info("%s: can't remove xfrm type\n", __func__);
  558. }
  559. module_init(esp6_init);
  560. module_exit(esp6_fini);
  561. MODULE_LICENSE("GPL");
  562. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);