ah6.c 17 KB

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