esp6.c 16 KB

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