esp6.c 16 KB

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