blob: d88d97617f7ebf93da36c395b4a01e1ed9b64285 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C)2002 USAGI/WIDE Project
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors
6 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Mitsuru KANDA @USAGI : IPv6 Support
Ian Morris67ba4152014-08-24 21:53:10 +01008 * Kazunori MIYAZAWA @USAGI :
9 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090010 *
Ian Morris67ba4152014-08-24 21:53:10 +010011 * This file is derived from net/ipv4/ah.c.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Joe Perchesf3213832012-05-15 14:11:53 +000014#define pr_fmt(fmt) "IPv6: " fmt
15
Sabrina Dubroca67df58a2017-05-03 16:57:57 +020016#include <crypto/algapi.h>
Steffen Klassert8631e9b2009-10-07 22:49:01 +000017#include <crypto/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <net/ip.h>
21#include <net/ah.h>
22#include <linux/crypto.h>
23#include <linux/pfkeyv2.h>
24#include <linux/string.h>
Steffen Klassert8631e9b2009-10-07 22:49:01 +000025#include <linux/scatterlist.h>
David S. Miller81aded22012-06-15 14:54:11 -070026#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/icmp.h>
28#include <net/ipv6.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020029#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Steffen Klassert8631e9b2009-10-07 22:49:01 +000032#define IPV6HDR_BASELEN 8
33
34struct tmp_ext {
Amerigo Wang07a93622012-10-29 16:23:10 +000035#if IS_ENABLED(CONFIG_IPV6_MIP6)
Steffen Klassert8631e9b2009-10-07 22:49:01 +000036 struct in6_addr saddr;
37#endif
38 struct in6_addr daddr;
Gustavo A. R. Silvab0c9a2d2020-02-28 07:36:41 -060039 char hdrs[];
Steffen Klassert8631e9b2009-10-07 22:49:01 +000040};
41
42struct ah_skb_cb {
43 struct xfrm_skb_cb xfrm;
44 void *tmp;
45};
46
47#define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
48
49static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags,
50 unsigned int size)
51{
52 unsigned int len;
53
54 len = size + crypto_ahash_digestsize(ahash) +
55 (crypto_ahash_alignmask(ahash) &
56 ~(crypto_tfm_ctx_alignment() - 1));
57
58 len = ALIGN(len, crypto_tfm_ctx_alignment());
59
60 len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
61 len = ALIGN(len, __alignof__(struct scatterlist));
62
63 len += sizeof(struct scatterlist) * nfrags;
64
65 return kmalloc(len, GFP_ATOMIC);
66}
67
68static inline struct tmp_ext *ah_tmp_ext(void *base)
69{
70 return base + IPV6HDR_BASELEN;
71}
72
73static inline u8 *ah_tmp_auth(u8 *tmp, unsigned int offset)
74{
75 return tmp + offset;
76}
77
78static inline u8 *ah_tmp_icv(struct crypto_ahash *ahash, void *tmp,
79 unsigned int offset)
80{
81 return PTR_ALIGN((u8 *)tmp + offset, crypto_ahash_alignmask(ahash) + 1);
82}
83
84static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
85 u8 *icv)
86{
87 struct ahash_request *req;
88
89 req = (void *)PTR_ALIGN(icv + crypto_ahash_digestsize(ahash),
90 crypto_tfm_ctx_alignment());
91
92 ahash_request_set_tfm(req, ahash);
93
94 return req;
95}
96
97static inline struct scatterlist *ah_req_sg(struct crypto_ahash *ahash,
98 struct ahash_request *req)
99{
100 return (void *)ALIGN((unsigned long)(req + 1) +
101 crypto_ahash_reqsize(ahash),
102 __alignof__(struct scatterlist));
103}
104
Eric Dumazeta50feda2012-05-18 18:57:34 +0000105static bool zero_out_mutable_opts(struct ipv6_opt_hdr *opthdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 u8 *opt = (u8 *)opthdr;
108 int len = ipv6_optlen(opthdr);
109 int off = 0;
110 int optlen = 0;
111
112 off += 2;
113 len -= 2;
114
115 while (len > 0) {
116
117 switch (opt[off]) {
118
Eldad Zack1de5a712012-05-17 06:00:25 +0000119 case IPV6_TLV_PAD1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 optlen = 1;
121 break;
122 default:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900123 if (len < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 goto bad;
125 optlen = opt[off+1]+2;
126 if (len < optlen)
127 goto bad;
128 if (opt[off] & 0x20)
129 memset(&opt[off+2], 0, opt[off+1]);
130 break;
131 }
132
133 off += optlen;
134 len -= optlen;
135 }
136 if (len == 0)
Eric Dumazeta50feda2012-05-18 18:57:34 +0000137 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139bad:
Eric Dumazeta50feda2012-05-18 18:57:34 +0000140 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Amerigo Wang07a93622012-10-29 16:23:10 +0000143#if IS_ENABLED(CONFIG_IPV6_MIP6)
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700144/**
145 * ipv6_rearrange_destopt - rearrange IPv6 destination options header
146 * @iph: IPv6 header
147 * @destopt: destionation options header
148 */
149static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt)
150{
151 u8 *opt = (u8 *)destopt;
152 int len = ipv6_optlen(destopt);
153 int off = 0;
154 int optlen = 0;
155
156 off += 2;
157 len -= 2;
158
159 while (len > 0) {
160
161 switch (opt[off]) {
162
Eldad Zack1de5a712012-05-17 06:00:25 +0000163 case IPV6_TLV_PAD1:
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700164 optlen = 1;
165 break;
166 default:
167 if (len < 2)
168 goto bad;
169 optlen = opt[off+1]+2;
170 if (len < optlen)
171 goto bad;
172
173 /* Rearrange the source address in @iph and the
174 * addresses in home address option for final source.
175 * See 11.3.2 of RFC 3775 for details.
176 */
177 if (opt[off] == IPV6_TLV_HAO) {
178 struct in6_addr final_addr;
179 struct ipv6_destopt_hao *hao;
180
181 hao = (struct ipv6_destopt_hao *)&opt[off];
182 if (hao->length != sizeof(hao->addr)) {
Joe Perchese87cc472012-05-13 21:56:26 +0000183 net_warn_ratelimited("destopt hao: invalid header length: %u\n",
184 hao->length);
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700185 goto bad;
186 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000187 final_addr = hao->addr;
188 hao->addr = iph->saddr;
189 iph->saddr = final_addr;
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700190 }
191 break;
192 }
193
194 off += optlen;
195 len -= optlen;
196 }
YOSHIFUJI Hideakie731c242006-08-24 23:18:12 +0900197 /* Note: ok if len == 0 */
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700198bad:
199 return;
200}
Masahide NAKAMURA136ebf02007-06-26 23:51:41 -0700201#else
202static void ipv6_rearrange_destopt(struct ipv6hdr *iph, struct ipv6_opt_hdr *destopt) {}
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700203#endif
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/**
206 * ipv6_rearrange_rthdr - rearrange IPv6 routing header
207 * @iph: IPv6 header
208 * @rthdr: routing header
209 *
210 * Rearrange the destination address in @iph and the addresses in @rthdr
211 * so that they appear in the order they will at the final destination.
212 * See Appendix A2 of RFC 2402 for details.
213 */
214static void ipv6_rearrange_rthdr(struct ipv6hdr *iph, struct ipv6_rt_hdr *rthdr)
215{
216 int segments, segments_left;
217 struct in6_addr *addrs;
218 struct in6_addr final_addr;
219
220 segments_left = rthdr->segments_left;
221 if (segments_left == 0)
222 return;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900223 rthdr->segments_left = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* The value of rthdr->hdrlen has been verified either by the system
226 * call if it is locally generated, or by ipv6_rthdr_rcv() for incoming
227 * packets. So we can assume that it is even and that segments is
228 * greater than or equal to segments_left.
229 *
230 * For the same reason we can assume that this option is of type 0.
231 */
232 segments = rthdr->hdrlen >> 1;
233
234 addrs = ((struct rt0_hdr *)rthdr)->addr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000235 final_addr = addrs[segments - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 addrs += segments - segments_left;
238 memmove(addrs + 1, addrs, (segments_left - 1) * sizeof(*addrs));
239
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000240 addrs[0] = iph->daddr;
241 iph->daddr = final_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700244static int ipv6_clear_mutable_options(struct ipv6hdr *iph, int len, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 union {
247 struct ipv6hdr *iph;
248 struct ipv6_opt_hdr *opth;
249 struct ipv6_rt_hdr *rth;
250 char *raw;
251 } exthdr = { .iph = iph };
252 char *end = exthdr.raw + len;
253 int nexthdr = iph->nexthdr;
254
255 exthdr.iph++;
256
257 while (exthdr.raw < end) {
258 switch (nexthdr) {
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700259 case NEXTHDR_DEST:
260 if (dir == XFRM_POLICY_OUT)
261 ipv6_rearrange_destopt(iph, exthdr.opth);
Joe Perchesa8eceea2020-03-12 15:50:22 -0700262 fallthrough;
YOSHIFUJI Hideakie731c242006-08-24 23:18:12 +0900263 case NEXTHDR_HOP:
264 if (!zero_out_mutable_opts(exthdr.opth)) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800265 net_dbg_ratelimited("overrun %sopts\n",
266 nexthdr == NEXTHDR_HOP ?
267 "hop" : "dest");
YOSHIFUJI Hideakie731c242006-08-24 23:18:12 +0900268 return -EINVAL;
269 }
270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 case NEXTHDR_ROUTING:
273 ipv6_rearrange_rthdr(iph, exthdr.rth);
274 break;
275
Ian Morris67ba4152014-08-24 21:53:10 +0100276 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278 }
279
280 nexthdr = exthdr.opth->nexthdr;
281 exthdr.raw += ipv6_optlen(exthdr.opth);
282 }
283
284 return 0;
285}
286
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000287static void ah6_output_done(struct crypto_async_request *base, int err)
288{
289 int extlen;
290 u8 *iph_base;
291 u8 *icv;
292 struct sk_buff *skb = base->data;
293 struct xfrm_state *x = skb_dst(skb)->xfrm;
294 struct ah_data *ahp = x->data;
295 struct ipv6hdr *top_iph = ipv6_hdr(skb);
296 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
297 struct tmp_ext *iph_ext;
298
299 extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
300 if (extlen)
301 extlen += sizeof(*iph_ext);
302
303 iph_base = AH_SKB_CB(skb)->tmp;
304 iph_ext = ah_tmp_ext(iph_base);
305 icv = ah_tmp_icv(ahp->ahash, iph_ext, extlen);
306
307 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
308 memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
309
310 if (extlen) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000311#if IS_ENABLED(CONFIG_IPV6_MIP6)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000312 memcpy(&top_iph->saddr, iph_ext, extlen);
313#else
314 memcpy(&top_iph->daddr, iph_ext, extlen);
315#endif
316 }
317
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000318 kfree(AH_SKB_CB(skb)->tmp);
319 xfrm_output_resume(skb, err);
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static int ah6_output(struct xfrm_state *x, struct sk_buff *skb)
323{
324 int err;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000325 int nfrags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int extlen;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000327 u8 *iph_base;
328 u8 *icv;
329 u8 nexthdr;
330 struct sk_buff *trailer;
331 struct crypto_ahash *ahash;
332 struct ahash_request *req;
333 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 struct ipv6hdr *top_iph;
335 struct ip_auth_hdr *ah;
336 struct ah_data *ahp;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000337 struct tmp_ext *iph_ext;
Fan Du26dd70c2014-01-18 09:54:26 +0800338 int seqhi_len = 0;
339 __be32 *seqhi;
340 int sglists = 0;
341 struct scatterlist *seqhisg;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000342
343 ahp = x->data;
344 ahash = ahp->ahash;
345
Ian Morrise5d08d72014-11-23 21:28:43 +0000346 err = skb_cow_data(skb, 0, &trailer);
347 if (err < 0)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000348 goto out;
349 nfrags = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Herbert Xu7b277b12007-10-10 15:44:06 -0700351 skb_push(skb, -skb_network_offset(skb));
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000352 extlen = skb_network_header_len(skb) - sizeof(struct ipv6hdr);
353 if (extlen)
354 extlen += sizeof(*iph_ext);
355
Fan Du26dd70c2014-01-18 09:54:26 +0800356 if (x->props.flags & XFRM_STATE_ESN) {
357 sglists = 1;
358 seqhi_len = sizeof(*seqhi);
359 }
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000360 err = -ENOMEM;
Fan Du26dd70c2014-01-18 09:54:26 +0800361 iph_base = ah_alloc_tmp(ahash, nfrags + sglists, IPV6HDR_BASELEN +
362 extlen + seqhi_len);
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000363 if (!iph_base)
364 goto out;
365
366 iph_ext = ah_tmp_ext(iph_base);
Fan Du26dd70c2014-01-18 09:54:26 +0800367 seqhi = (__be32 *)((char *)iph_ext + extlen);
368 icv = ah_tmp_icv(ahash, seqhi, seqhi_len);
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000369 req = ah_tmp_req(ahash, icv);
370 sg = ah_req_sg(ahash, req);
Fan Du26dd70c2014-01-18 09:54:26 +0800371 seqhisg = sg + nfrags;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000372
373 ah = ip_auth_hdr(skb);
374 memset(ah->auth_data, 0, ahp->icv_trunc_len);
375
Herbert Xu007f0212007-10-09 13:25:59 -0700376 top_iph = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 top_iph->payload_len = htons(skb->len - sizeof(*top_iph));
378
Herbert Xu007f0212007-10-09 13:25:59 -0700379 nexthdr = *skb_mac_header(skb);
380 *skb_mac_header(skb) = IPPROTO_AH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* When there are no extension headers, we only need to save the first
383 * 8 bytes of the base IP header.
384 */
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000385 memcpy(iph_base, top_iph, IPV6HDR_BASELEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (extlen) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000388#if IS_ENABLED(CONFIG_IPV6_MIP6)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000389 memcpy(iph_ext, &top_iph->saddr, extlen);
YOSHIFUJI Hideakie731c242006-08-24 23:18:12 +0900390#else
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000391 memcpy(iph_ext, &top_iph->daddr, extlen);
YOSHIFUJI Hideakie731c242006-08-24 23:18:12 +0900392#endif
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700393 err = ipv6_clear_mutable_options(top_iph,
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000394 extlen - sizeof(*iph_ext) +
Masahide NAKAMURA27637df2006-08-23 19:29:47 -0700395 sizeof(*top_iph),
396 XFRM_POLICY_OUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (err)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000398 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ah->nexthdr = nexthdr;
402
403 top_iph->priority = 0;
404 top_iph->flow_lbl[0] = 0;
405 top_iph->flow_lbl[1] = 0;
406 top_iph->flow_lbl[2] = 0;
407 top_iph->hop_limit = 0;
408
Herbert Xu87bdc482007-10-10 15:45:25 -0700409 ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 ah->reserved = 0;
412 ah->spi = x->id.spi;
Steffen Klassert1ce36442011-03-08 00:06:31 +0000413 ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
Herbert Xub7c65382007-10-09 13:33:35 -0700414
Fan Du26dd70c2014-01-18 09:54:26 +0800415 sg_init_table(sg, nfrags + sglists);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200416 err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
417 if (unlikely(err < 0))
418 goto out_free;
Herbert Xub7c65382007-10-09 13:33:35 -0700419
Fan Du26dd70c2014-01-18 09:54:26 +0800420 if (x->props.flags & XFRM_STATE_ESN) {
421 /* Attach seqhi sg right after packet payload */
422 *seqhi = htonl(XFRM_SKB_CB(skb)->seq.output.hi);
423 sg_set_buf(seqhisg, seqhi, seqhi_len);
424 }
425 ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000426 ahash_request_set_callback(req, 0, ah6_output_done, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000428 AH_SKB_CB(skb)->tmp = iph_base;
429
430 err = crypto_ahash_digest(req);
431 if (err) {
432 if (err == -EINPROGRESS)
433 goto out;
434
Gilad Ben-Yossef068c2e72017-10-18 08:00:35 +0100435 if (err == -ENOSPC)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000436 err = NET_XMIT_DROP;
437 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000440 memcpy(ah->auth_data, icv, ahp->icv_trunc_len);
441 memcpy(top_iph, iph_base, IPV6HDR_BASELEN);
442
443 if (extlen) {
Amerigo Wang07a93622012-10-29 16:23:10 +0000444#if IS_ENABLED(CONFIG_IPV6_MIP6)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000445 memcpy(&top_iph->saddr, iph_ext, extlen);
446#else
447 memcpy(&top_iph->daddr, iph_ext, extlen);
448#endif
449 }
450
451out_free:
452 kfree(iph_base);
453out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return err;
455}
456
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000457static void ah6_input_done(struct crypto_async_request *base, int err)
458{
459 u8 *auth_data;
460 u8 *icv;
461 u8 *work_iph;
462 struct sk_buff *skb = base->data;
463 struct xfrm_state *x = xfrm_input_state(skb);
464 struct ah_data *ahp = x->data;
465 struct ip_auth_hdr *ah = ip_auth_hdr(skb);
466 int hdr_len = skb_network_header_len(skb);
yangxingwu416e8122019-07-10 21:14:10 +0800467 int ah_hlen = ipv6_authlen(ah);
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000468
Gilad Ben-Yossef726282a2017-01-16 13:17:56 +0200469 if (err)
470 goto out;
471
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000472 work_iph = AH_SKB_CB(skb)->tmp;
473 auth_data = ah_tmp_auth(work_iph, hdr_len);
474 icv = ah_tmp_icv(ahp->ahash, auth_data, ahp->icv_trunc_len);
475
Sabrina Dubroca67df58a2017-05-03 16:57:57 +0200476 err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000477 if (err)
478 goto out;
479
Nick Bowlerb7ea81a2011-11-08 12:12:45 +0000480 err = ah->nexthdr;
481
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000482 skb->network_header += ah_hlen;
483 memcpy(skb_network_header(skb), work_iph, hdr_len);
484 __skb_pull(skb, ah_hlen + hdr_len);
Li RongQinga9403f82013-01-08 15:41:12 +0800485 if (x->props.mode == XFRM_MODE_TUNNEL)
486 skb_reset_transport_header(skb);
487 else
488 skb_set_transport_header(skb, -hdr_len);
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000489out:
490 kfree(AH_SKB_CB(skb)->tmp);
491 xfrm_input_resume(skb, err);
492}
493
494
495
Herbert Xue6956332006-04-01 00:52:46 -0800496static int ah6_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 /*
499 * Before process AH
500 * [IPv6][Ext1][Ext2][AH][Dest][Payload]
501 * |<-------------->| hdr_len
502 *
503 * To erase AH:
504 * Keeping copy of cleared headers. After AH processing,
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700505 * Moving the pointer of skb->network_header by using skb_pull as long
506 * as AH header length. Then copy back the copy as long as hdr_len
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * If destination header following AH exists, copy it into after [Ext2].
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900508 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * |<>|[IPv6][Ext1][Ext2][Dest][Payload]
510 * There is offset of AH before IPv6 header after the process.
511 */
512
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000513 u8 *auth_data;
514 u8 *icv;
515 u8 *work_iph;
516 struct sk_buff *trailer;
517 struct crypto_ahash *ahash;
518 struct ahash_request *req;
519 struct scatterlist *sg;
Herbert Xu87bdc482007-10-10 15:45:25 -0700520 struct ip_auth_hdr *ah;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700521 struct ipv6hdr *ip6h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 struct ah_data *ahp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 u16 hdr_len;
524 u16 ah_hlen;
525 int nexthdr;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000526 int nfrags;
527 int err = -ENOMEM;
Fan Du8d6da6f2014-01-18 09:54:27 +0800528 int seqhi_len = 0;
529 __be32 *seqhi;
530 int sglists = 0;
531 struct scatterlist *seqhisg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 if (!pskb_may_pull(skb, sizeof(struct ip_auth_hdr)))
534 goto out;
535
536 /* We are going to _remove_ AH header to keep sockets happy,
537 * so... Later this can change. */
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000538 if (skb_unclone(skb, GFP_ATOMIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto out;
540
Herbert Xu7aa68cb2007-10-17 21:30:07 -0700541 skb->ip_summed = CHECKSUM_NONE;
542
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000543 hdr_len = skb_network_header_len(skb);
Herbert Xu87bdc482007-10-10 15:45:25 -0700544 ah = (struct ip_auth_hdr *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 ahp = x->data;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000546 ahash = ahp->ahash;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 nexthdr = ah->nexthdr;
yangxingwu416e8122019-07-10 21:14:10 +0800549 ah_hlen = ipv6_authlen(ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Herbert Xu87bdc482007-10-10 15:45:25 -0700551 if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
552 ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 if (!pskb_may_pull(skb, ah_hlen))
556 goto out;
557
Ian Morrise5d08d72014-11-23 21:28:43 +0000558 err = skb_cow_data(skb, 0, &trailer);
559 if (err < 0)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000560 goto out;
561 nfrags = err;
562
Dang Hongwu4b0ef1f22011-01-11 07:13:33 +0000563 ah = (struct ip_auth_hdr *)skb->data;
564 ip6h = ipv6_hdr(skb);
565
566 skb_push(skb, hdr_len);
567
Fan Du8d6da6f2014-01-18 09:54:27 +0800568 if (x->props.flags & XFRM_STATE_ESN) {
569 sglists = 1;
570 seqhi_len = sizeof(*seqhi);
571 }
572
573 work_iph = ah_alloc_tmp(ahash, nfrags + sglists, hdr_len +
574 ahp->icv_trunc_len + seqhi_len);
Julia Lawall25105052015-08-23 02:11:21 +0200575 if (!work_iph) {
576 err = -ENOMEM;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000577 goto out;
Julia Lawall25105052015-08-23 02:11:21 +0200578 }
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000579
Fan Du8d6da6f2014-01-18 09:54:27 +0800580 auth_data = ah_tmp_auth((u8 *)work_iph, hdr_len);
581 seqhi = (__be32 *)(auth_data + ahp->icv_trunc_len);
582 icv = ah_tmp_icv(ahash, seqhi, seqhi_len);
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000583 req = ah_tmp_req(ahash, icv);
584 sg = ah_req_sg(ahash, req);
Fan Du8d6da6f2014-01-18 09:54:27 +0800585 seqhisg = sg + nfrags;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000586
587 memcpy(work_iph, ip6h, hdr_len);
588 memcpy(auth_data, ah->auth_data, ahp->icv_trunc_len);
589 memset(ah->auth_data, 0, ahp->icv_trunc_len);
590
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700591 if (ipv6_clear_mutable_options(ip6h, hdr_len, XFRM_POLICY_IN))
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000592 goto out_free;
593
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700594 ip6h->priority = 0;
595 ip6h->flow_lbl[0] = 0;
596 ip6h->flow_lbl[1] = 0;
597 ip6h->flow_lbl[2] = 0;
598 ip6h->hop_limit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Fan Du8d6da6f2014-01-18 09:54:27 +0800600 sg_init_table(sg, nfrags + sglists);
Jason A. Donenfeld3f297702017-06-04 04:16:23 +0200601 err = skb_to_sgvec_nomark(skb, sg, 0, skb->len);
602 if (unlikely(err < 0))
603 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Fan Du8d6da6f2014-01-18 09:54:27 +0800605 if (x->props.flags & XFRM_STATE_ESN) {
606 /* Attach seqhi sg right after packet payload */
607 *seqhi = XFRM_SKB_CB(skb)->seq.input.hi;
608 sg_set_buf(seqhisg, seqhi, seqhi_len);
609 }
610
611 ahash_request_set_crypt(req, sg, icv, skb->len + seqhi_len);
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000612 ahash_request_set_callback(req, 0, ah6_input_done, skb);
613
614 AH_SKB_CB(skb)->tmp = work_iph;
615
616 err = crypto_ahash_digest(req);
617 if (err) {
618 if (err == -EINPROGRESS)
619 goto out;
620
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000621 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800623
Sabrina Dubroca67df58a2017-05-03 16:57:57 +0200624 err = crypto_memneq(icv, auth_data, ahp->icv_trunc_len) ? -EBADMSG : 0;
Herbert Xu0ebea8e2007-11-13 21:45:58 -0800625 if (err)
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000626 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700628 skb->network_header += ah_hlen;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000629 memcpy(skb_network_header(skb), work_iph, hdr_len);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700630 __skb_pull(skb, ah_hlen + hdr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Li RongQinga9403f82013-01-08 15:41:12 +0800632 if (x->props.mode == XFRM_MODE_TUNNEL)
633 skb_reset_transport_header(skb);
634 else
635 skb_set_transport_header(skb, -hdr_len);
636
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000637 err = nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000639out_free:
640 kfree(work_iph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641out:
Herbert Xu07d4ee52006-08-20 14:24:50 +1000642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100645static int ah6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
646 u8 type, u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Alexey Dobriyan4fb236b2008-11-25 17:59:27 -0800648 struct net *net = dev_net(skb->dev);
Ian Morris67ba4152014-08-24 21:53:10 +0100649 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
650 struct ip_auth_hdr *ah = (struct ip_auth_hdr *)(skb->data+offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct xfrm_state *x;
652
Steffen Klassertb3b2b9e2013-09-10 13:43:09 +0200653 if (type != ICMPV6_PKT_TOOBIG &&
David S. Millerec18d9a2012-07-12 00:25:15 -0700654 type != NDISC_REDIRECT)
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Jamal Hadi Salimbd557752010-02-22 16:20:22 -0800657 x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!x)
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
David S. Millerec18d9a2012-07-12 00:25:15 -0700661 if (type == NDISC_REDIRECT)
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900662 ip6_redirect(skb, net, skb->dev->ifindex, 0,
663 sock_net_uid(net, NULL));
David S. Millerec18d9a2012-07-12 00:25:15 -0700664 else
Lorenzo Colittie2d118a2016-11-04 02:23:43 +0900665 ip6_update_pmtu(skb, net, info, 0, 0, sock_net_uid(net, NULL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 xfrm_state_put(x);
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100667
668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Herbert Xu72cb6962005-06-20 13:18:08 -0700671static int ah6_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 struct ah_data *ahp = NULL;
674 struct xfrm_algo_desc *aalg_desc;
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000675 struct crypto_ahash *ahash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 if (!x->aalg)
678 goto error;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (x->encap)
681 goto error;
682
Ingo Oeser0c600ed2006-03-20 23:01:32 -0800683 ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100684 if (!ahp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -ENOMEM;
686
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000687 ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
688 if (IS_ERR(ahash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto error;
Herbert Xu07d4ee52006-08-20 14:24:50 +1000690
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000691 ahp->ahash = ahash;
692 if (crypto_ahash_setkey(ahash, x->aalg->alg_key,
Herbert Xubc31d3b2007-10-08 17:14:34 -0700693 (x->aalg->alg_key_len + 7) / 8))
Herbert Xu07d4ee52006-08-20 14:24:50 +1000694 goto error;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /*
697 * Lookup the algorithm description maintained by xfrm_algo,
698 * verify crypto transform properties, and store information
699 * we need for AH processing. This lookup cannot fail here
Herbert Xu07d4ee52006-08-20 14:24:50 +1000700 * after a successful crypto_alloc_hash().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
702 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
703 BUG_ON(!aalg_desc);
704
705 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000706 crypto_ahash_digestsize(ahash)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000707 pr_info("AH: %s digestsize %u != %hu\n",
708 x->aalg->alg_name, crypto_ahash_digestsize(ahash),
709 aalg_desc->uinfo.auth.icv_fullbits/8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 goto error;
711 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
Martin Willi8f8a0882009-11-25 00:29:53 +0000714 ahp->icv_trunc_len = x->aalg->alg_trunc_len/8;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900715
Herbert Xu87bdc482007-10-10 15:45:25 -0700716 x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
717 ahp->icv_trunc_len);
Herbert Xuca681452007-10-17 21:35:15 -0700718 switch (x->props.mode) {
719 case XFRM_MODE_BEET:
720 case XFRM_MODE_TRANSPORT:
721 break;
722 case XFRM_MODE_TUNNEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 x->props.header_len += sizeof(struct ipv6hdr);
Masahide NAKAMURAea2c47b2007-10-22 02:30:15 -0700724 break;
Herbert Xuca681452007-10-17 21:35:15 -0700725 default:
726 goto error;
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 x->data = ahp;
729
730 return 0;
731
732error:
733 if (ahp) {
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000734 crypto_free_ahash(ahp->ahash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 kfree(ahp);
736 }
737 return -EINVAL;
738}
739
740static void ah6_destroy(struct xfrm_state *x)
741{
742 struct ah_data *ahp = x->data;
743
744 if (!ahp)
745 return;
746
Steffen Klassert8631e9b2009-10-07 22:49:01 +0000747 crypto_free_ahash(ahp->ahash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 kfree(ahp);
749}
750
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100751static int ah6_rcv_cb(struct sk_buff *skb, int err)
752{
753 return 0;
754}
755
Ian Morriscc24bec2014-08-24 21:53:11 +0100756static const struct xfrm_type ah6_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .description = "AH6",
758 .owner = THIS_MODULE,
Ian Morriscc24bec2014-08-24 21:53:11 +0100759 .proto = IPPROTO_AH,
Herbert Xu436a0a42007-10-08 17:25:53 -0700760 .flags = XFRM_TYPE_REPLAY_PROT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 .init_state = ah6_init_state,
762 .destructor = ah6_destroy,
763 .input = ah6_input,
Masahide NAKAMURAaee5adb2006-08-23 17:57:28 -0700764 .output = ah6_output,
765 .hdr_offset = xfrm6_find_1stfragopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766};
767
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100768static struct xfrm6_protocol ah6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 .handler = xfrm6_rcv,
Sabrina Dubroca0146dca2020-04-27 17:59:34 +0200770 .input_handler = xfrm_input,
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100771 .cb_handler = ah6_rcv_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 .err_handler = ah6_err,
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100773 .priority = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774};
775
776static int __init ah6_init(void)
777{
778 if (xfrm_register_type(&ah6_type, AF_INET6) < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +0000779 pr_info("%s: can't add xfrm type\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -EAGAIN;
781 }
782
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100783 if (xfrm6_protocol_register(&ah6_protocol, IPPROTO_AH) < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +0000784 pr_info("%s: can't add protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 xfrm_unregister_type(&ah6_type, AF_INET6);
786 return -EAGAIN;
787 }
788
789 return 0;
790}
791
792static void __exit ah6_fini(void)
793{
Steffen Klasserte924d2d2014-03-14 07:28:07 +0100794 if (xfrm6_protocol_deregister(&ah6_protocol, IPPROTO_AH) < 0)
Joe Perchesf3213832012-05-15 14:11:53 +0000795 pr_info("%s: can't remove protocol\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Florian Westphal4f518e82019-05-03 17:46:19 +0200797 xfrm_unregister_type(&ah6_type, AF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800module_init(ah6_init);
801module_exit(ah6_fini);
802
803MODULE_LICENSE("GPL");
Masahide NAKAMURAd3d6dd32007-06-26 23:57:49 -0700804MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_AH);