ah6.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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/ah.c.
  25. */
  26. #include <crypto/hash.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <net/ip.h>
  30. #include <net/ah.h>
  31. #include <linux/crypto.h>
  32. #include <linux/pfkeyv2.h>
  33. #include <linux/string.h>
  34. #include <linux/scatterlist.h>
  35. #include <net/icmp.h>
  36. #include <net/ipv6.h>
  37. #include <net/protocol.h>
  38. #include <net/xfrm.h>
  39. #define IPV6HDR_BASELEN 8
  40. struct tmp_ext {
  41. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  42. struct in6_addr saddr;
  43. #endif
  44. struct in6_addr daddr;
  45. char hdrs[0];
  46. };
  47. struct ah_skb_cb {
  48. struct xfrm_skb_cb xfrm;
  49. void *tmp;
  50. };
  51. #define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
  52. static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
  53. unsigned int size)
  54. {
  55. unsigned int len;
  56. len = size + crypto_ahash_digestsize(ahash) +
  57. (crypto_ahash_alignmask(ahash) &
  58. ~(crypto_tfm_ctx_alignment() - 1));
  59. len = ALIGN(len, crypto_tfm_ctx_alignment());
  60. len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
  61. len = ALIGN(len, __alignof__(struct scatterlist));
  62. len += sizeof(struct scatterlist) * nfrags;
  63. return kmalloc(len, GFP_ATOMIC);
  64. }
  65. static inline struct tmp_ext *ah_tmp_ext(void *base)
  66. {
  67. return base + IPV6HDR_BASELEN;
  68. }
  69. static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset)
  70. {
  71. return tmp + offset;
  72. }
  73. static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
  74. unsigned int offset)
  75. {
  76. return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
  77. }
  78. static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
  79. u8 *icv)
  80. {
  81. struct ahash_request *req;
  82. req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
  83. crypto_tfm_ctx_alignment());
  84. ahash_request_set_tfm(req, ahash);
  85. return req;
  86. }
  87. static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
  88. struct ahash_request *req)
  89. {
  90. return (void *)ALIGN((unsigned long)(req + 1) +
  91. crypto_ahash_reqsize(ahash),
  92. __alignof__(struct scatterlist));
  93. }
  94. static int zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
  95. {
  96. u8 *opt = (u8 *)opthdr;
  97. int len = ipv6_optlen(opthdr);
  98. int off = 0;
  99. int optlen = 0;
  100. off += 2;
  101. len -= 2;
  102. while (len > 0) {
  103. switch (opt[off]) {
  104. case IPV6_TLV_PAD0:
  105. optlen = 1;
  106. break;
  107. default:
  108. if (len < 2)
  109. goto bad;
  110. optlen = opt[off+1]+2;
  111. if (len < optlen)
  112. goto bad;
  113. if (opt[off] & 0x20)
  114. memset(&opt[off+2], 0, opt[off+1]);
  115. break;
  116. }
  117. off += optlen;
  118. len -= optlen;
  119. }
  120. if (len == 0)
  121. return 1;
  122. bad:
  123. return 0;
  124. }
  125. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  126. /**
  127. * ipv6_rearrange_destopt - rearrange IPv6 destination options header
  128. * @iph: IPv6 header
  129. * @destopt: destionation options header
  130. */
  131. static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
  132. {
  133. u8 *opt = (u8 *)destopt;
  134. int len = ipv6_optlen(destopt);
  135. int off = 0;
  136. int optlen = 0;
  137. off += 2;
  138. len -= 2;
  139. while (len > 0) {
  140. switch (opt[off]) {
  141. case IPV6_TLV_PAD0:
  142. optlen = 1;
  143. break;
  144. default:
  145. if (len < 2)
  146. goto bad;
  147. optlen = opt[off+1]+2;
  148. if (len < optlen)
  149. goto bad;
  150. /* Rearrange the source address in @iph and the
  151. * addresses in home address option for final source.
  152. * See 11.3.2 of RFC 3775 for details.
  153. */
  154. if (opt[off] == IPV6_TLV_HAO) {
  155. struct in6_addr final_addr;
  156. struct ipv6_destopt_hao *hao;
  157. hao = (struct ipv6_destopt_hao *)&opt[off];
  158. if (hao->length != sizeof(hao->addr)) {
  159. if (net_ratelimit())
  160. printk(KERN_WARNING "destopt hao: invalid header length: %u\n", hao->length);
  161. goto bad;
  162. }
  163. ipv6_addr_copy(&final_addr, &hao->addr);
  164. ipv6_addr_copy(&hao->addr, &iph->saddr);
  165. ipv6_addr_copy(&iph->saddr, &final_addr);
  166. }
  167. break;
  168. }
  169. off += optlen;
  170. len -= optlen;
  171. }
  172. /* Note: ok if len == 0 */
  173. bad:
  174. return;
  175. }
  176. #else
  177. static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
  178. #endif
  179. /**
  180. * ipv6_rearrange_rthdr - rearrange IPv6 routing header
  181. * @iph: IPv6 header
  182. * @rthdr: routing header
  183. *
  184. * Rearrange the destination address in @iph and the addresses in @rthdr
  185. * so that they appear in the order they will at the final destination.
  186. * See Appendix A2 of RFC 2402 for details.
  187. */
  188. static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
  189. {
  190. int segments, segments_left;
  191. struct in6_addr *addrs;
  192. struct in6_addr final_addr;
  193. segments_left = rthdr->segments_left;
  194. if (segments_left == 0)
  195. return;
  196. rthdr->segments_left = 0;
  197. /* The value of rthdr->hdrlen has been verified either by the system
  198. * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
  199. * packets. So we can assume that it is even and that segments is
  200. * greater than or equal to segments_left.
  201. *
  202. * For the same reason we can assume that this option is of type 0.
  203. */
  204. segments = rthdr->hdrlen >> 1;
  205. addrs = ((struct rt0_hdr *)rthdr)->addr;
  206. ipv6_addr_copy(&final_addr, addrs + segments - 1);
  207. addrs += segments - segments_left;
  208. memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
  209. ipv6_addr_copy(addrs, &iph->daddr);
  210. ipv6_addr_copy(&iph->daddr, &final_addr);
  211. }
  212. static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
  213. {
  214. union {
  215. struct ipv6hdr *iph;
  216. struct ipv6_opt_hdr *opth;
  217. struct ipv6_rt_hdr *rth;
  218. char *raw;
  219. } exthdr = { .iph = iph };
  220. char *end = exthdr.raw + len;
  221. int nexthdr = iph->nexthdr;
  222. exthdr.iph++;
  223. while (exthdr.raw < end) {
  224. switch (nexthdr) {
  225. case NEXTHDR_DEST:
  226. if (dir == XFRM_POLICY_OUT)
  227. ipv6_rearrange_destopt(iph, exthdr.opth);
  228. case NEXTHDR_HOP:
  229. if (!zero_out_mutable_opts(exthdr.opth)) {
  230. LIMIT_NETDEBUG(
  231. KERN_WARNING "overrun %sopts\n",
  232. nexthdr == NEXTHDR_HOP ?
  233. "hop" : "dest");
  234. return -EINVAL;
  235. }
  236. break;
  237. case NEXTHDR_ROUTING:
  238. ipv6_rearrange_rthdr(iph, exthdr.rth);
  239. break;
  240. default :
  241. return 0;
  242. }
  243. nexthdr = exthdr.opth->nexthdr;
  244. exthdr.raw += ipv6_optlen(exthdr.opth);
  245. }
  246. return 0;
  247. }
  248. static void ah6_output_done(struct crypto_async_request *base, int err)
  249. {
  250. int extlen;
  251. u8 *iph_base;
  252. u8 *icv;
  253. struct sk_buff *skb = base->data;
  254. struct xfrm_state *x = skb_dst(skb)->xfrm;
  255. struct ah_data *ahp = x->data;
  256. struct ipv6hdr *top_iph = ipv6_hdr(skb);
  257. struct ip_auth_hdr *ah = ip_auth_hdr(skb);
  258. struct tmp_ext *iph_ext;
  259. extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
  260. if (extlen)
  261. extlen += sizeof(*iph_ext);
  262. iph_base = AH_SKB_CB(skb)->tmp;
  263. iph_ext = ah_tmp_ext(iph_base);
  264. icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen);
  265. memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
  266. memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
  267. if (extlen) {
  268. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  269. memcpy(&top_iph->saddr, iph_ext, extlen);
  270. #else
  271. memcpy(&top_iph->daddr, iph_ext, extlen);
  272. #endif
  273. }
  274. err = ah->nexthdr;
  275. kfree(AH_SKB_CB(skb)->tmp);
  276. xfrm_output_resume(skb, err);
  277. }
  278. static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
  279. {
  280. int err;
  281. int nfrags;
  282. int extlen;
  283. u8 *iph_base;
  284. u8 *icv;
  285. u8 nexthdr;
  286. struct sk_buff *trailer;
  287. struct crypto_ahash *ahash;
  288. struct ahash_request *req;
  289. struct scatterlist *sg;
  290. struct ipv6hdr *top_iph;
  291. struct ip_auth_hdr *ah;
  292. struct ah_data *ahp;
  293. struct tmp_ext *iph_ext;
  294. ahp = x->data;
  295. ahash = ahp->ahash;
  296. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  297. goto out;
  298. nfrags = err;
  299. skb_push(skb, -skb_network_offset(skb));
  300. extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
  301. if (extlen)
  302. extlen += sizeof(*iph_ext);
  303. err = -ENOMEM;
  304. iph_base = ah_alloc_tmp(ahash, nfrags, IPV6HDR_BASELEN + extlen);
  305. if (!iph_base)
  306. goto out;
  307. iph_ext = ah_tmp_ext(iph_base);
  308. icv = ah_tmp_icv(ahash, iph_ext, extlen);
  309. req = ah_tmp_req(ahash, icv);
  310. sg = ah_req_sg(ahash, req);
  311. ah = ip_auth_hdr(skb);
  312. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  313. top_iph = ipv6_hdr(skb);
  314. top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
  315. nexthdr = *skb_mac_header(skb);
  316. *skb_mac_header(skb) = IPPROTO_AH;
  317. /* When there are no extension headers, we only need to save the first
  318. * 8 bytes of the base IP header.
  319. */
  320. memcpy(iph_base, top_iph, IPV6HDR_BASELEN);
  321. if (extlen) {
  322. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  323. memcpy(iph_ext, &top_iph->saddr, extlen);
  324. #else
  325. memcpy(iph_ext, &top_iph->daddr, extlen);
  326. #endif
  327. err = ipv6_clear_mutable_options(top_iph,
  328. extlen - sizeof(*iph_ext) +
  329. sizeof(*top_iph),
  330. XFRM_POLICY_OUT);
  331. if (err)
  332. goto out_free;
  333. }
  334. ah->nexthdr = nexthdr;
  335. top_iph->priority = 0;
  336. top_iph->flow_lbl[0] = 0;
  337. top_iph->flow_lbl[1] = 0;
  338. top_iph->flow_lbl[2] = 0;
  339. top_iph->hop_limit = 0;
  340. ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
  341. ah->reserved = 0;
  342. ah->spi = x->id.spi;
  343. ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  344. sg_init_table(sg, nfrags);
  345. skb_to_sgvec(skb, sg, 0, skb->len);
  346. ahash_request_set_crypt(req, sg, icv, skb->len);
  347. ahash_request_set_callback(req, 0, ah6_output_done, skb);
  348. AH_SKB_CB(skb)->tmp = iph_base;
  349. err = crypto_ahash_digest(req);
  350. if (err) {
  351. if (err == -EINPROGRESS)
  352. goto out;
  353. if (err == -EBUSY)
  354. err = NET_XMIT_DROP;
  355. goto out_free;
  356. }
  357. memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
  358. memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
  359. if (extlen) {
  360. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  361. memcpy(&top_iph->saddr, iph_ext, extlen);
  362. #else
  363. memcpy(&top_iph->daddr, iph_ext, extlen);
  364. #endif
  365. }
  366. out_free:
  367. kfree(iph_base);
  368. out:
  369. return err;
  370. }
  371. static void ah6_input_done(struct crypto_async_request *base, int err)
  372. {
  373. u8 *auth_data;
  374. u8 *icv;
  375. u8 *work_iph;
  376. struct sk_buff *skb = base->data;
  377. struct xfrm_state *x = xfrm_input_state(skb);
  378. struct ah_data *ahp = x->data;
  379. struct ip_auth_hdr *ah = ip_auth_hdr(skb);
  380. int hdr_len = skb_network_header_len(skb);
  381. int ah_hlen = (ah->hdrlen + 2) << 2;
  382. work_iph = AH_SKB_CB(skb)->tmp;
  383. auth_data = ah_tmp_auth(work_iph, hdr_len);
  384. icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
  385. err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
  386. if (err)
  387. goto out;
  388. skb->network_header += ah_hlen;
  389. memcpy(skb_network_header(skb), work_iph, hdr_len);
  390. __skb_pull(skb, ah_hlen + hdr_len);
  391. skb_set_transport_header(skb, -hdr_len);
  392. err = ah->nexthdr;
  393. out:
  394. kfree(AH_SKB_CB(skb)->tmp);
  395. xfrm_input_resume(skb, err);
  396. }
  397. static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
  398. {
  399. /*
  400. * Before process AH
  401. * [IPv6][Ext1][Ext2][AH][Dest][Payload]
  402. * |<-------------->| hdr_len
  403. *
  404. * To erase AH:
  405. * Keeping copy of cleared headers. After AH processing,
  406. * Moving the pointer of skb->network_header by using skb_pull as long
  407. * as AH header length. Then copy back the copy as long as hdr_len
  408. * If destination header following AH exists, copy it into after [Ext2].
  409. *
  410. * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
  411. * There is offset of AH before IPv6 header after the process.
  412. */
  413. u8 *auth_data;
  414. u8 *icv;
  415. u8 *work_iph;
  416. struct sk_buff *trailer;
  417. struct crypto_ahash *ahash;
  418. struct ahash_request *req;
  419. struct scatterlist *sg;
  420. struct ip_auth_hdr *ah;
  421. struct ipv6hdr *ip6h;
  422. struct ah_data *ahp;
  423. u16 hdr_len;
  424. u16 ah_hlen;
  425. int nexthdr;
  426. int nfrags;
  427. int err = -ENOMEM;
  428. if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
  429. goto out;
  430. /* We are going to _remove_ AH header to keep sockets happy,
  431. * so... Later this can change. */
  432. if (skb_cloned(skb) &&
  433. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  434. goto out;
  435. skb->ip_summed = CHECKSUM_NONE;
  436. hdr_len = skb_network_header_len(skb);
  437. ah = (struct ip_auth_hdr *)skb->data;
  438. ahp = x->data;
  439. ahash = ahp->ahash;
  440. nexthdr = ah->nexthdr;
  441. ah_hlen = (ah->hdrlen + 2) << 2;
  442. if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
  443. ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
  444. goto out;
  445. if (!pskb_may_pull(skb, ah_hlen))
  446. goto out;
  447. if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
  448. goto out;
  449. nfrags = err;
  450. ah = (struct ip_auth_hdr *)skb->data;
  451. ip6h = ipv6_hdr(skb);
  452. skb_push(skb, hdr_len);
  453. work_iph = ah_alloc_tmp(ahash, nfrags, hdr_len + ahp->icv_trunc_len);
  454. if (!work_iph)
  455. goto out;
  456. auth_data = ah_tmp_auth(work_iph, hdr_len);
  457. icv = ah_tmp_icv(ahash, auth_data, ahp->icv_trunc_len);
  458. req = ah_tmp_req(ahash, icv);
  459. sg = ah_req_sg(ahash, req);
  460. memcpy(work_iph, ip6h, hdr_len);
  461. memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
  462. memset(ah->auth_data, 0, ahp->icv_trunc_len);
  463. if (ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN))
  464. goto out_free;
  465. ip6h->priority = 0;
  466. ip6h->flow_lbl[0] = 0;
  467. ip6h->flow_lbl[1] = 0;
  468. ip6h->flow_lbl[2] = 0;
  469. ip6h->hop_limit = 0;
  470. sg_init_table(sg, nfrags);
  471. skb_to_sgvec(skb, sg, 0, skb->len);
  472. ahash_request_set_crypt(req, sg, icv, skb->len);
  473. ahash_request_set_callback(req, 0, ah6_input_done, skb);
  474. AH_SKB_CB(skb)->tmp = work_iph;
  475. err = crypto_ahash_digest(req);
  476. if (err) {
  477. if (err == -EINPROGRESS)
  478. goto out;
  479. if (err == -EBUSY)
  480. err = NET_XMIT_DROP;
  481. goto out_free;
  482. }
  483. err = memcmp(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG: 0;
  484. if (err)
  485. goto out_free;
  486. skb->network_header += ah_hlen;
  487. memcpy(skb_network_header(skb), work_iph, hdr_len);
  488. skb->transport_header = skb->network_header;
  489. __skb_pull(skb, ah_hlen + hdr_len);
  490. err = nexthdr;
  491. out_free:
  492. kfree(work_iph);
  493. out:
  494. return err;
  495. }
  496. static void ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  497. u8 type, u8 code, int offset, __be32 info)
  498. {
  499. struct net *net = dev_net(skb->dev);
  500. struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
  501. struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+offset);
  502. struct xfrm_state *x;
  503. if (type != ICMPV6_DEST_UNREACH &&
  504. type != ICMPV6_PKT_TOOBIG)
  505. return;
  506. x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
  507. if (!x)
  508. return;
  509. NETDEBUG(KERN_DEBUG "pmtu discovery on SA AH/%08x/%pI6\n",
  510. ntohl(ah->spi), &iph->daddr);
  511. xfrm_state_put(x);
  512. }
  513. static int ah6_init_state(struct xfrm_state *x)
  514. {
  515. struct ah_data *ahp = NULL;
  516. struct xfrm_algo_desc *aalg_desc;
  517. struct crypto_ahash *ahash;
  518. if (!x->aalg)
  519. goto error;
  520. if (x->encap)
  521. goto error;
  522. ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
  523. if (ahp == NULL)
  524. return -ENOMEM;
  525. ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
  526. if (IS_ERR(ahash))
  527. goto error;
  528. ahp->ahash = ahash;
  529. if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
  530. (x->aalg->alg_key_len + 7) / 8))
  531. goto error;
  532. /*
  533. * Lookup the algorithm description maintained by xfrm_algo,
  534. * verify crypto transform properties, and store information
  535. * we need for AH processing. This lookup cannot fail here
  536. * after a successful crypto_alloc_hash().
  537. */
  538. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  539. BUG_ON(!aalg_desc);
  540. if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
  541. crypto_ahash_digestsize(ahash)) {
  542. printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
  543. x->aalg->alg_name, crypto_ahash_digestsize(ahash),
  544. aalg_desc->uinfo.auth.icv_fullbits/8);
  545. goto error;
  546. }
  547. ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
  548. ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
  549. BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
  550. x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
  551. ahp->icv_trunc_len);
  552. switch (x->props.mode) {
  553. case XFRM_MODE_BEET:
  554. case XFRM_MODE_TRANSPORT:
  555. break;
  556. case XFRM_MODE_TUNNEL:
  557. x->props.header_len += sizeof(struct ipv6hdr);
  558. break;
  559. default:
  560. goto error;
  561. }
  562. x->data = ahp;
  563. return 0;
  564. error:
  565. if (ahp) {
  566. crypto_free_ahash(ahp->ahash);
  567. kfree(ahp);
  568. }
  569. return -EINVAL;
  570. }
  571. static void ah6_destroy(struct xfrm_state *x)
  572. {
  573. struct ah_data *ahp = x->data;
  574. if (!ahp)
  575. return;
  576. crypto_free_ahash(ahp->ahash);
  577. kfree(ahp);
  578. }
  579. static const struct xfrm_type ah6_type =
  580. {
  581. .description = "AH6",
  582. .owner = THIS_MODULE,
  583. .proto = IPPROTO_AH,
  584. .flags = XFRM_TYPE_REPLAY_PROT,
  585. .init_state = ah6_init_state,
  586. .destructor = ah6_destroy,
  587. .input = ah6_input,
  588. .output = ah6_output,
  589. .hdr_offset = xfrm6_find_1stfragopt,
  590. };
  591. static const struct inet6_protocol ah6_protocol = {
  592. .handler = xfrm6_rcv,
  593. .err_handler = ah6_err,
  594. .flags = INET6_PROTO_NOPOLICY,
  595. };
  596. static int __init ah6_init(void)
  597. {
  598. if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
  599. printk(KERN_INFO "ipv6 ah init: can't add xfrm type\n");
  600. return -EAGAIN;
  601. }
  602. if (inet6_add_protocol(&ah6_protocol, IPPROTO_AH) < 0) {
  603. printk(KERN_INFO "ipv6 ah init: can't add protocol\n");
  604. xfrm_unregister_type(&ah6_type, AF_INET6);
  605. return -EAGAIN;
  606. }
  607. return 0;
  608. }
  609. static void __exit ah6_fini(void)
  610. {
  611. if (inet6_del_protocol(&ah6_protocol, IPPROTO_AH) < 0)
  612. printk(KERN_INFO "ipv6 ah close: can't remove protocol\n");
  613. if (xfrm_unregister_type(&ah6_type, AF_INET6) < 0)
  614. printk(KERN_INFO "ipv6 ah close: can't remove xfrm type\n");
  615. }
  616. module_init(ah6_init);
  617. module_exit(ah6_fini);
  618. MODULE_LICENSE("GPL");
  619. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);