ah6.c 18 KB

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