ah6.c 18 KB

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