ah6.c 19 KB

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