xref: /openbmc/linux/net/ipv6/seg6_hmac.c (revision fa79581ea66ca43d56ef065346ac5be767fcb418)
1bf355b8dSDavid Lebrun /*
2bf355b8dSDavid Lebrun  *  SR-IPv6 implementation -- HMAC functions
3bf355b8dSDavid Lebrun  *
4bf355b8dSDavid Lebrun  *  Author:
5bf355b8dSDavid Lebrun  *  David Lebrun <david.lebrun@uclouvain.be>
6bf355b8dSDavid Lebrun  *
7bf355b8dSDavid Lebrun  *
8bf355b8dSDavid Lebrun  *  This program is free software; you can redistribute it and/or
9bf355b8dSDavid Lebrun  *      modify it under the terms of the GNU General Public License
10bf355b8dSDavid Lebrun  *      as published by the Free Software Foundation; either version
11bf355b8dSDavid Lebrun  *      2 of the License, or (at your option) any later version.
12bf355b8dSDavid Lebrun  */
13bf355b8dSDavid Lebrun 
14bf355b8dSDavid Lebrun #include <linux/errno.h>
15bf355b8dSDavid Lebrun #include <linux/types.h>
16bf355b8dSDavid Lebrun #include <linux/socket.h>
17bf355b8dSDavid Lebrun #include <linux/sockios.h>
18bf355b8dSDavid Lebrun #include <linux/net.h>
19bf355b8dSDavid Lebrun #include <linux/netdevice.h>
20bf355b8dSDavid Lebrun #include <linux/in6.h>
21bf355b8dSDavid Lebrun #include <linux/icmpv6.h>
22bf355b8dSDavid Lebrun #include <linux/mroute6.h>
23bf355b8dSDavid Lebrun #include <linux/slab.h>
24bf355b8dSDavid Lebrun 
25bf355b8dSDavid Lebrun #include <linux/netfilter.h>
26bf355b8dSDavid Lebrun #include <linux/netfilter_ipv6.h>
27bf355b8dSDavid Lebrun 
28bf355b8dSDavid Lebrun #include <net/sock.h>
29bf355b8dSDavid Lebrun #include <net/snmp.h>
30bf355b8dSDavid Lebrun 
31bf355b8dSDavid Lebrun #include <net/ipv6.h>
32bf355b8dSDavid Lebrun #include <net/protocol.h>
33bf355b8dSDavid Lebrun #include <net/transp_v6.h>
34bf355b8dSDavid Lebrun #include <net/rawv6.h>
35bf355b8dSDavid Lebrun #include <net/ndisc.h>
36bf355b8dSDavid Lebrun #include <net/ip6_route.h>
37bf355b8dSDavid Lebrun #include <net/addrconf.h>
38bf355b8dSDavid Lebrun #include <net/xfrm.h>
39bf355b8dSDavid Lebrun 
40bf355b8dSDavid Lebrun #include <linux/cryptohash.h>
41bf355b8dSDavid Lebrun #include <crypto/hash.h>
42bf355b8dSDavid Lebrun #include <crypto/sha.h>
43bf355b8dSDavid Lebrun #include <net/seg6.h>
44bf355b8dSDavid Lebrun #include <net/genetlink.h>
45bf355b8dSDavid Lebrun #include <net/seg6_hmac.h>
46bf355b8dSDavid Lebrun #include <linux/random.h>
47bf355b8dSDavid Lebrun 
48bf355b8dSDavid Lebrun static char * __percpu *hmac_ring;
49bf355b8dSDavid Lebrun 
50bf355b8dSDavid Lebrun static int seg6_hmac_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
51bf355b8dSDavid Lebrun {
52bf355b8dSDavid Lebrun 	const struct seg6_hmac_info *hinfo = obj;
53bf355b8dSDavid Lebrun 
54bf355b8dSDavid Lebrun 	return (hinfo->hmackeyid != *(__u32 *)arg->key);
55bf355b8dSDavid Lebrun }
56bf355b8dSDavid Lebrun 
57bf355b8dSDavid Lebrun static inline void seg6_hinfo_release(struct seg6_hmac_info *hinfo)
58bf355b8dSDavid Lebrun {
59bf355b8dSDavid Lebrun 	kfree_rcu(hinfo, rcu);
60bf355b8dSDavid Lebrun }
61bf355b8dSDavid Lebrun 
62bf355b8dSDavid Lebrun static void seg6_free_hi(void *ptr, void *arg)
63bf355b8dSDavid Lebrun {
64bf355b8dSDavid Lebrun 	struct seg6_hmac_info *hinfo = (struct seg6_hmac_info *)ptr;
65bf355b8dSDavid Lebrun 
66bf355b8dSDavid Lebrun 	if (hinfo)
67bf355b8dSDavid Lebrun 		seg6_hinfo_release(hinfo);
68bf355b8dSDavid Lebrun }
69bf355b8dSDavid Lebrun 
70bf355b8dSDavid Lebrun static const struct rhashtable_params rht_params = {
71bf355b8dSDavid Lebrun 	.head_offset		= offsetof(struct seg6_hmac_info, node),
72bf355b8dSDavid Lebrun 	.key_offset		= offsetof(struct seg6_hmac_info, hmackeyid),
73bf355b8dSDavid Lebrun 	.key_len		= sizeof(u32),
74bf355b8dSDavid Lebrun 	.automatic_shrinking	= true,
75bf355b8dSDavid Lebrun 	.obj_cmpfn		= seg6_hmac_cmpfn,
76bf355b8dSDavid Lebrun };
77bf355b8dSDavid Lebrun 
78bf355b8dSDavid Lebrun static struct seg6_hmac_algo hmac_algos[] = {
79bf355b8dSDavid Lebrun 	{
80bf355b8dSDavid Lebrun 		.alg_id = SEG6_HMAC_ALGO_SHA1,
81bf355b8dSDavid Lebrun 		.name = "hmac(sha1)",
82bf355b8dSDavid Lebrun 	},
83bf355b8dSDavid Lebrun 	{
84bf355b8dSDavid Lebrun 		.alg_id = SEG6_HMAC_ALGO_SHA256,
85bf355b8dSDavid Lebrun 		.name = "hmac(sha256)",
86bf355b8dSDavid Lebrun 	},
87bf355b8dSDavid Lebrun };
88bf355b8dSDavid Lebrun 
89bf355b8dSDavid Lebrun static struct sr6_tlv_hmac *seg6_get_tlv_hmac(struct ipv6_sr_hdr *srh)
90bf355b8dSDavid Lebrun {
91bf355b8dSDavid Lebrun 	struct sr6_tlv_hmac *tlv;
92bf355b8dSDavid Lebrun 
93bf355b8dSDavid Lebrun 	if (srh->hdrlen < (srh->first_segment + 1) * 2 + 5)
94bf355b8dSDavid Lebrun 		return NULL;
95bf355b8dSDavid Lebrun 
96bf355b8dSDavid Lebrun 	if (!sr_has_hmac(srh))
97bf355b8dSDavid Lebrun 		return NULL;
98bf355b8dSDavid Lebrun 
99bf355b8dSDavid Lebrun 	tlv = (struct sr6_tlv_hmac *)
100bf355b8dSDavid Lebrun 	      ((char *)srh + ((srh->hdrlen + 1) << 3) - 40);
101bf355b8dSDavid Lebrun 
102bf355b8dSDavid Lebrun 	if (tlv->tlvhdr.type != SR6_TLV_HMAC || tlv->tlvhdr.len != 38)
103bf355b8dSDavid Lebrun 		return NULL;
104bf355b8dSDavid Lebrun 
105bf355b8dSDavid Lebrun 	return tlv;
106bf355b8dSDavid Lebrun }
107bf355b8dSDavid Lebrun 
108bf355b8dSDavid Lebrun static struct seg6_hmac_algo *__hmac_get_algo(u8 alg_id)
109bf355b8dSDavid Lebrun {
110bf355b8dSDavid Lebrun 	struct seg6_hmac_algo *algo;
111bf355b8dSDavid Lebrun 	int i, alg_count;
112bf355b8dSDavid Lebrun 
113bf355b8dSDavid Lebrun 	alg_count = sizeof(hmac_algos) / sizeof(struct seg6_hmac_algo);
114bf355b8dSDavid Lebrun 	for (i = 0; i < alg_count; i++) {
115bf355b8dSDavid Lebrun 		algo = &hmac_algos[i];
116bf355b8dSDavid Lebrun 		if (algo->alg_id == alg_id)
117bf355b8dSDavid Lebrun 			return algo;
118bf355b8dSDavid Lebrun 	}
119bf355b8dSDavid Lebrun 
120bf355b8dSDavid Lebrun 	return NULL;
121bf355b8dSDavid Lebrun }
122bf355b8dSDavid Lebrun 
123bf355b8dSDavid Lebrun static int __do_hmac(struct seg6_hmac_info *hinfo, const char *text, u8 psize,
124bf355b8dSDavid Lebrun 		     u8 *output, int outlen)
125bf355b8dSDavid Lebrun {
126bf355b8dSDavid Lebrun 	struct seg6_hmac_algo *algo;
127bf355b8dSDavid Lebrun 	struct crypto_shash *tfm;
128bf355b8dSDavid Lebrun 	struct shash_desc *shash;
129bf355b8dSDavid Lebrun 	int ret, dgsize;
130bf355b8dSDavid Lebrun 
131bf355b8dSDavid Lebrun 	algo = __hmac_get_algo(hinfo->alg_id);
132bf355b8dSDavid Lebrun 	if (!algo)
133bf355b8dSDavid Lebrun 		return -ENOENT;
134bf355b8dSDavid Lebrun 
135bf355b8dSDavid Lebrun 	tfm = *this_cpu_ptr(algo->tfms);
136bf355b8dSDavid Lebrun 
137bf355b8dSDavid Lebrun 	dgsize = crypto_shash_digestsize(tfm);
138bf355b8dSDavid Lebrun 	if (dgsize > outlen) {
139bf355b8dSDavid Lebrun 		pr_debug("sr-ipv6: __do_hmac: digest size too big (%d / %d)\n",
140bf355b8dSDavid Lebrun 			 dgsize, outlen);
141bf355b8dSDavid Lebrun 		return -ENOMEM;
142bf355b8dSDavid Lebrun 	}
143bf355b8dSDavid Lebrun 
144bf355b8dSDavid Lebrun 	ret = crypto_shash_setkey(tfm, hinfo->secret, hinfo->slen);
145bf355b8dSDavid Lebrun 	if (ret < 0) {
146bf355b8dSDavid Lebrun 		pr_debug("sr-ipv6: crypto_shash_setkey failed: err %d\n", ret);
147bf355b8dSDavid Lebrun 		goto failed;
148bf355b8dSDavid Lebrun 	}
149bf355b8dSDavid Lebrun 
150bf355b8dSDavid Lebrun 	shash = *this_cpu_ptr(algo->shashs);
151bf355b8dSDavid Lebrun 	shash->tfm = tfm;
152bf355b8dSDavid Lebrun 
153bf355b8dSDavid Lebrun 	ret = crypto_shash_digest(shash, text, psize, output);
154bf355b8dSDavid Lebrun 	if (ret < 0) {
155bf355b8dSDavid Lebrun 		pr_debug("sr-ipv6: crypto_shash_digest failed: err %d\n", ret);
156bf355b8dSDavid Lebrun 		goto failed;
157bf355b8dSDavid Lebrun 	}
158bf355b8dSDavid Lebrun 
159bf355b8dSDavid Lebrun 	return dgsize;
160bf355b8dSDavid Lebrun 
161bf355b8dSDavid Lebrun failed:
162bf355b8dSDavid Lebrun 	return ret;
163bf355b8dSDavid Lebrun }
164bf355b8dSDavid Lebrun 
165bf355b8dSDavid Lebrun int seg6_hmac_compute(struct seg6_hmac_info *hinfo, struct ipv6_sr_hdr *hdr,
166bf355b8dSDavid Lebrun 		      struct in6_addr *saddr, u8 *output)
167bf355b8dSDavid Lebrun {
168bf355b8dSDavid Lebrun 	__be32 hmackeyid = cpu_to_be32(hinfo->hmackeyid);
169bf355b8dSDavid Lebrun 	u8 tmp_out[SEG6_HMAC_MAX_DIGESTSIZE];
170bf355b8dSDavid Lebrun 	int plen, i, dgsize, wrsize;
171bf355b8dSDavid Lebrun 	char *ring, *off;
172bf355b8dSDavid Lebrun 
173bf355b8dSDavid Lebrun 	/* a 160-byte buffer for digest output allows to store highest known
174bf355b8dSDavid Lebrun 	 * hash function (RadioGatun) with up to 1216 bits
175bf355b8dSDavid Lebrun 	 */
176bf355b8dSDavid Lebrun 
177bf355b8dSDavid Lebrun 	/* saddr(16) + first_seg(1) + cleanup(1) + keyid(4) + seglist(16n) */
178bf355b8dSDavid Lebrun 	plen = 16 + 1 + 1 + 4 + (hdr->first_segment + 1) * 16;
179bf355b8dSDavid Lebrun 
180bf355b8dSDavid Lebrun 	/* this limit allows for 14 segments */
181bf355b8dSDavid Lebrun 	if (plen >= SEG6_HMAC_RING_SIZE)
182bf355b8dSDavid Lebrun 		return -EMSGSIZE;
183bf355b8dSDavid Lebrun 
184bf355b8dSDavid Lebrun 	/* Let's build the HMAC text on the ring buffer. The text is composed
185bf355b8dSDavid Lebrun 	 * as follows, in order:
186bf355b8dSDavid Lebrun 	 *
187bf355b8dSDavid Lebrun 	 * 1. Source IPv6 address (128 bits)
188bf355b8dSDavid Lebrun 	 * 2. first_segment value (8 bits)
189bf355b8dSDavid Lebrun 	 * 3. cleanup flag (8 bits: highest bit is cleanup value, others are 0)
190bf355b8dSDavid Lebrun 	 * 4. HMAC Key ID (32 bits)
191bf355b8dSDavid Lebrun 	 * 5. All segments in the segments list (n * 128 bits)
192bf355b8dSDavid Lebrun 	 */
193bf355b8dSDavid Lebrun 
194bf355b8dSDavid Lebrun 	local_bh_disable();
195bf355b8dSDavid Lebrun 	ring = *this_cpu_ptr(hmac_ring);
196bf355b8dSDavid Lebrun 	off = ring;
197bf355b8dSDavid Lebrun 
198bf355b8dSDavid Lebrun 	/* source address */
199bf355b8dSDavid Lebrun 	memcpy(off, saddr, 16);
200bf355b8dSDavid Lebrun 	off += 16;
201bf355b8dSDavid Lebrun 
202bf355b8dSDavid Lebrun 	/* first_segment value */
203bf355b8dSDavid Lebrun 	*off++ = hdr->first_segment;
204bf355b8dSDavid Lebrun 
205bf355b8dSDavid Lebrun 	/* cleanup flag */
206bf355b8dSDavid Lebrun 	*off++ = !!(sr_has_cleanup(hdr)) << 7;
207bf355b8dSDavid Lebrun 
208bf355b8dSDavid Lebrun 	/* HMAC Key ID */
209bf355b8dSDavid Lebrun 	memcpy(off, &hmackeyid, 4);
210bf355b8dSDavid Lebrun 	off += 4;
211bf355b8dSDavid Lebrun 
212bf355b8dSDavid Lebrun 	/* all segments in the list */
213bf355b8dSDavid Lebrun 	for (i = 0; i < hdr->first_segment + 1; i++) {
214bf355b8dSDavid Lebrun 		memcpy(off, hdr->segments + i, 16);
215bf355b8dSDavid Lebrun 		off += 16;
216bf355b8dSDavid Lebrun 	}
217bf355b8dSDavid Lebrun 
218bf355b8dSDavid Lebrun 	dgsize = __do_hmac(hinfo, ring, plen, tmp_out,
219bf355b8dSDavid Lebrun 			   SEG6_HMAC_MAX_DIGESTSIZE);
220bf355b8dSDavid Lebrun 	local_bh_enable();
221bf355b8dSDavid Lebrun 
222bf355b8dSDavid Lebrun 	if (dgsize < 0)
223bf355b8dSDavid Lebrun 		return dgsize;
224bf355b8dSDavid Lebrun 
225bf355b8dSDavid Lebrun 	wrsize = SEG6_HMAC_FIELD_LEN;
226bf355b8dSDavid Lebrun 	if (wrsize > dgsize)
227bf355b8dSDavid Lebrun 		wrsize = dgsize;
228bf355b8dSDavid Lebrun 
229bf355b8dSDavid Lebrun 	memset(output, 0, SEG6_HMAC_FIELD_LEN);
230bf355b8dSDavid Lebrun 	memcpy(output, tmp_out, wrsize);
231bf355b8dSDavid Lebrun 
232bf355b8dSDavid Lebrun 	return 0;
233bf355b8dSDavid Lebrun }
234bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_hmac_compute);
235bf355b8dSDavid Lebrun 
236bf355b8dSDavid Lebrun /* checks if an incoming SR-enabled packet's HMAC status matches
237bf355b8dSDavid Lebrun  * the incoming policy.
238bf355b8dSDavid Lebrun  *
239bf355b8dSDavid Lebrun  * called with rcu_read_lock()
240bf355b8dSDavid Lebrun  */
241bf355b8dSDavid Lebrun bool seg6_hmac_validate_skb(struct sk_buff *skb)
242bf355b8dSDavid Lebrun {
243bf355b8dSDavid Lebrun 	u8 hmac_output[SEG6_HMAC_FIELD_LEN];
244bf355b8dSDavid Lebrun 	struct net *net = dev_net(skb->dev);
245bf355b8dSDavid Lebrun 	struct seg6_hmac_info *hinfo;
246bf355b8dSDavid Lebrun 	struct sr6_tlv_hmac *tlv;
247bf355b8dSDavid Lebrun 	struct ipv6_sr_hdr *srh;
248bf355b8dSDavid Lebrun 	struct inet6_dev *idev;
249bf355b8dSDavid Lebrun 
250bf355b8dSDavid Lebrun 	idev = __in6_dev_get(skb->dev);
251bf355b8dSDavid Lebrun 
252bf355b8dSDavid Lebrun 	srh = (struct ipv6_sr_hdr *)skb_transport_header(skb);
253bf355b8dSDavid Lebrun 
254bf355b8dSDavid Lebrun 	tlv = seg6_get_tlv_hmac(srh);
255bf355b8dSDavid Lebrun 
256bf355b8dSDavid Lebrun 	/* mandatory check but no tlv */
257bf355b8dSDavid Lebrun 	if (idev->cnf.seg6_require_hmac > 0 && !tlv)
258bf355b8dSDavid Lebrun 		return false;
259bf355b8dSDavid Lebrun 
260bf355b8dSDavid Lebrun 	/* no check */
261bf355b8dSDavid Lebrun 	if (idev->cnf.seg6_require_hmac < 0)
262bf355b8dSDavid Lebrun 		return true;
263bf355b8dSDavid Lebrun 
264bf355b8dSDavid Lebrun 	/* check only if present */
265bf355b8dSDavid Lebrun 	if (idev->cnf.seg6_require_hmac == 0 && !tlv)
266bf355b8dSDavid Lebrun 		return true;
267bf355b8dSDavid Lebrun 
268bf355b8dSDavid Lebrun 	/* now, seg6_require_hmac >= 0 && tlv */
269bf355b8dSDavid Lebrun 
270bf355b8dSDavid Lebrun 	hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
271bf355b8dSDavid Lebrun 	if (!hinfo)
272bf355b8dSDavid Lebrun 		return false;
273bf355b8dSDavid Lebrun 
274bf355b8dSDavid Lebrun 	if (seg6_hmac_compute(hinfo, srh, &ipv6_hdr(skb)->saddr, hmac_output))
275bf355b8dSDavid Lebrun 		return false;
276bf355b8dSDavid Lebrun 
277bf355b8dSDavid Lebrun 	if (memcmp(hmac_output, tlv->hmac, SEG6_HMAC_FIELD_LEN) != 0)
278bf355b8dSDavid Lebrun 		return false;
279bf355b8dSDavid Lebrun 
280bf355b8dSDavid Lebrun 	return true;
281bf355b8dSDavid Lebrun }
282bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_hmac_validate_skb);
283bf355b8dSDavid Lebrun 
284bf355b8dSDavid Lebrun /* called with rcu_read_lock() */
285bf355b8dSDavid Lebrun struct seg6_hmac_info *seg6_hmac_info_lookup(struct net *net, u32 key)
286bf355b8dSDavid Lebrun {
287bf355b8dSDavid Lebrun 	struct seg6_pernet_data *sdata = seg6_pernet(net);
288bf355b8dSDavid Lebrun 	struct seg6_hmac_info *hinfo;
289bf355b8dSDavid Lebrun 
290bf355b8dSDavid Lebrun 	hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
291bf355b8dSDavid Lebrun 
292bf355b8dSDavid Lebrun 	return hinfo;
293bf355b8dSDavid Lebrun }
294bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_hmac_info_lookup);
295bf355b8dSDavid Lebrun 
296bf355b8dSDavid Lebrun int seg6_hmac_info_add(struct net *net, u32 key, struct seg6_hmac_info *hinfo)
297bf355b8dSDavid Lebrun {
298bf355b8dSDavid Lebrun 	struct seg6_pernet_data *sdata = seg6_pernet(net);
299bf355b8dSDavid Lebrun 	int err;
300bf355b8dSDavid Lebrun 
301bf355b8dSDavid Lebrun 	err = rhashtable_lookup_insert_fast(&sdata->hmac_infos, &hinfo->node,
302bf355b8dSDavid Lebrun 					    rht_params);
303bf355b8dSDavid Lebrun 
304bf355b8dSDavid Lebrun 	return err;
305bf355b8dSDavid Lebrun }
306bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_hmac_info_add);
307bf355b8dSDavid Lebrun 
308bf355b8dSDavid Lebrun int seg6_hmac_info_del(struct net *net, u32 key)
309bf355b8dSDavid Lebrun {
310bf355b8dSDavid Lebrun 	struct seg6_pernet_data *sdata = seg6_pernet(net);
311bf355b8dSDavid Lebrun 	struct seg6_hmac_info *hinfo;
312bf355b8dSDavid Lebrun 	int err = -ENOENT;
313bf355b8dSDavid Lebrun 
314bf355b8dSDavid Lebrun 	hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
315bf355b8dSDavid Lebrun 	if (!hinfo)
316bf355b8dSDavid Lebrun 		goto out;
317bf355b8dSDavid Lebrun 
318bf355b8dSDavid Lebrun 	err = rhashtable_remove_fast(&sdata->hmac_infos, &hinfo->node,
319bf355b8dSDavid Lebrun 				     rht_params);
320bf355b8dSDavid Lebrun 	if (err)
321bf355b8dSDavid Lebrun 		goto out;
322bf355b8dSDavid Lebrun 
323bf355b8dSDavid Lebrun 	seg6_hinfo_release(hinfo);
324bf355b8dSDavid Lebrun 
325bf355b8dSDavid Lebrun out:
326bf355b8dSDavid Lebrun 	return err;
327bf355b8dSDavid Lebrun }
328bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_hmac_info_del);
329bf355b8dSDavid Lebrun 
330bf355b8dSDavid Lebrun int seg6_push_hmac(struct net *net, struct in6_addr *saddr,
331bf355b8dSDavid Lebrun 		   struct ipv6_sr_hdr *srh)
332bf355b8dSDavid Lebrun {
333bf355b8dSDavid Lebrun 	struct seg6_hmac_info *hinfo;
334bf355b8dSDavid Lebrun 	struct sr6_tlv_hmac *tlv;
335bf355b8dSDavid Lebrun 	int err = -ENOENT;
336bf355b8dSDavid Lebrun 
337bf355b8dSDavid Lebrun 	tlv = seg6_get_tlv_hmac(srh);
338bf355b8dSDavid Lebrun 	if (!tlv)
339bf355b8dSDavid Lebrun 		return -EINVAL;
340bf355b8dSDavid Lebrun 
341bf355b8dSDavid Lebrun 	rcu_read_lock();
342bf355b8dSDavid Lebrun 
343bf355b8dSDavid Lebrun 	hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
344bf355b8dSDavid Lebrun 	if (!hinfo)
345bf355b8dSDavid Lebrun 		goto out;
346bf355b8dSDavid Lebrun 
347bf355b8dSDavid Lebrun 	memset(tlv->hmac, 0, SEG6_HMAC_FIELD_LEN);
348bf355b8dSDavid Lebrun 	err = seg6_hmac_compute(hinfo, srh, saddr, tlv->hmac);
349bf355b8dSDavid Lebrun 
350bf355b8dSDavid Lebrun out:
351bf355b8dSDavid Lebrun 	rcu_read_unlock();
352bf355b8dSDavid Lebrun 	return err;
353bf355b8dSDavid Lebrun }
354bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_push_hmac);
355bf355b8dSDavid Lebrun 
356bf355b8dSDavid Lebrun static int seg6_hmac_init_ring(void)
357bf355b8dSDavid Lebrun {
358bf355b8dSDavid Lebrun 	int i;
359bf355b8dSDavid Lebrun 
360bf355b8dSDavid Lebrun 	hmac_ring = alloc_percpu(char *);
361bf355b8dSDavid Lebrun 
362bf355b8dSDavid Lebrun 	if (!hmac_ring)
363bf355b8dSDavid Lebrun 		return -ENOMEM;
364bf355b8dSDavid Lebrun 
365bf355b8dSDavid Lebrun 	for_each_possible_cpu(i) {
366bf355b8dSDavid Lebrun 		char *ring = kzalloc(SEG6_HMAC_RING_SIZE, GFP_KERNEL);
367bf355b8dSDavid Lebrun 
368bf355b8dSDavid Lebrun 		if (!ring)
369bf355b8dSDavid Lebrun 			return -ENOMEM;
370bf355b8dSDavid Lebrun 
371bf355b8dSDavid Lebrun 		*per_cpu_ptr(hmac_ring, i) = ring;
372bf355b8dSDavid Lebrun 	}
373bf355b8dSDavid Lebrun 
374bf355b8dSDavid Lebrun 	return 0;
375bf355b8dSDavid Lebrun }
376bf355b8dSDavid Lebrun 
377bf355b8dSDavid Lebrun static int seg6_hmac_init_algo(void)
378bf355b8dSDavid Lebrun {
379bf355b8dSDavid Lebrun 	struct seg6_hmac_algo *algo;
380bf355b8dSDavid Lebrun 	struct crypto_shash *tfm;
381bf355b8dSDavid Lebrun 	struct shash_desc *shash;
382bf355b8dSDavid Lebrun 	int i, alg_count, cpu;
383bf355b8dSDavid Lebrun 
384bf355b8dSDavid Lebrun 	alg_count = sizeof(hmac_algos) / sizeof(struct seg6_hmac_algo);
385bf355b8dSDavid Lebrun 
386bf355b8dSDavid Lebrun 	for (i = 0; i < alg_count; i++) {
387bf355b8dSDavid Lebrun 		struct crypto_shash **p_tfm;
388bf355b8dSDavid Lebrun 		int shsize;
389bf355b8dSDavid Lebrun 
390bf355b8dSDavid Lebrun 		algo = &hmac_algos[i];
391bf355b8dSDavid Lebrun 		algo->tfms = alloc_percpu(struct crypto_shash *);
392bf355b8dSDavid Lebrun 		if (!algo->tfms)
393bf355b8dSDavid Lebrun 			return -ENOMEM;
394bf355b8dSDavid Lebrun 
395bf355b8dSDavid Lebrun 		for_each_possible_cpu(cpu) {
396bf355b8dSDavid Lebrun 			tfm = crypto_alloc_shash(algo->name, 0, GFP_KERNEL);
397bf355b8dSDavid Lebrun 			if (IS_ERR(tfm))
398bf355b8dSDavid Lebrun 				return PTR_ERR(tfm);
399bf355b8dSDavid Lebrun 			p_tfm = per_cpu_ptr(algo->tfms, cpu);
400bf355b8dSDavid Lebrun 			*p_tfm = tfm;
401bf355b8dSDavid Lebrun 		}
402bf355b8dSDavid Lebrun 
403*fa79581eSDavid Lebrun 		p_tfm = raw_cpu_ptr(algo->tfms);
404bf355b8dSDavid Lebrun 		tfm = *p_tfm;
405bf355b8dSDavid Lebrun 
406bf355b8dSDavid Lebrun 		shsize = sizeof(*shash) + crypto_shash_descsize(tfm);
407bf355b8dSDavid Lebrun 
408bf355b8dSDavid Lebrun 		algo->shashs = alloc_percpu(struct shash_desc *);
409bf355b8dSDavid Lebrun 		if (!algo->shashs)
410bf355b8dSDavid Lebrun 			return -ENOMEM;
411bf355b8dSDavid Lebrun 
412bf355b8dSDavid Lebrun 		for_each_possible_cpu(cpu) {
413bf355b8dSDavid Lebrun 			shash = kzalloc(shsize, GFP_KERNEL);
414bf355b8dSDavid Lebrun 			if (!shash)
415bf355b8dSDavid Lebrun 				return -ENOMEM;
416bf355b8dSDavid Lebrun 			*per_cpu_ptr(algo->shashs, cpu) = shash;
417bf355b8dSDavid Lebrun 		}
418bf355b8dSDavid Lebrun 	}
419bf355b8dSDavid Lebrun 
420bf355b8dSDavid Lebrun 	return 0;
421bf355b8dSDavid Lebrun }
422bf355b8dSDavid Lebrun 
423bf355b8dSDavid Lebrun int __init seg6_hmac_init(void)
424bf355b8dSDavid Lebrun {
425bf355b8dSDavid Lebrun 	int ret;
426bf355b8dSDavid Lebrun 
427bf355b8dSDavid Lebrun 	ret = seg6_hmac_init_ring();
428bf355b8dSDavid Lebrun 	if (ret < 0)
429bf355b8dSDavid Lebrun 		goto out;
430bf355b8dSDavid Lebrun 
431bf355b8dSDavid Lebrun 	ret = seg6_hmac_init_algo();
432bf355b8dSDavid Lebrun 
433bf355b8dSDavid Lebrun out:
434bf355b8dSDavid Lebrun 	return ret;
435bf355b8dSDavid Lebrun }
436bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_hmac_init);
437bf355b8dSDavid Lebrun 
438bf355b8dSDavid Lebrun int __net_init seg6_hmac_net_init(struct net *net)
439bf355b8dSDavid Lebrun {
440bf355b8dSDavid Lebrun 	struct seg6_pernet_data *sdata = seg6_pernet(net);
441bf355b8dSDavid Lebrun 
442bf355b8dSDavid Lebrun 	rhashtable_init(&sdata->hmac_infos, &rht_params);
443bf355b8dSDavid Lebrun 
444bf355b8dSDavid Lebrun 	return 0;
445bf355b8dSDavid Lebrun }
446bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_hmac_net_init);
447bf355b8dSDavid Lebrun 
448bf355b8dSDavid Lebrun void seg6_hmac_exit(void)
449bf355b8dSDavid Lebrun {
450bf355b8dSDavid Lebrun 	struct seg6_hmac_algo *algo = NULL;
451bf355b8dSDavid Lebrun 	int i, alg_count, cpu;
452bf355b8dSDavid Lebrun 
453bf355b8dSDavid Lebrun 	for_each_possible_cpu(i) {
454bf355b8dSDavid Lebrun 		char *ring = *per_cpu_ptr(hmac_ring, i);
455bf355b8dSDavid Lebrun 
456bf355b8dSDavid Lebrun 		kfree(ring);
457bf355b8dSDavid Lebrun 	}
458bf355b8dSDavid Lebrun 	free_percpu(hmac_ring);
459bf355b8dSDavid Lebrun 
460bf355b8dSDavid Lebrun 	alg_count = sizeof(hmac_algos) / sizeof(struct seg6_hmac_algo);
461bf355b8dSDavid Lebrun 	for (i = 0; i < alg_count; i++) {
462bf355b8dSDavid Lebrun 		algo = &hmac_algos[i];
463bf355b8dSDavid Lebrun 		for_each_possible_cpu(cpu) {
464bf355b8dSDavid Lebrun 			struct crypto_shash *tfm;
465bf355b8dSDavid Lebrun 			struct shash_desc *shash;
466bf355b8dSDavid Lebrun 
467bf355b8dSDavid Lebrun 			shash = *per_cpu_ptr(algo->shashs, cpu);
468bf355b8dSDavid Lebrun 			kfree(shash);
469bf355b8dSDavid Lebrun 			tfm = *per_cpu_ptr(algo->tfms, cpu);
470bf355b8dSDavid Lebrun 			crypto_free_shash(tfm);
471bf355b8dSDavid Lebrun 		}
472bf355b8dSDavid Lebrun 		free_percpu(algo->tfms);
473bf355b8dSDavid Lebrun 		free_percpu(algo->shashs);
474bf355b8dSDavid Lebrun 	}
475bf355b8dSDavid Lebrun }
476bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_hmac_exit);
477bf355b8dSDavid Lebrun 
478bf355b8dSDavid Lebrun void __net_exit seg6_hmac_net_exit(struct net *net)
479bf355b8dSDavid Lebrun {
480bf355b8dSDavid Lebrun 	struct seg6_pernet_data *sdata = seg6_pernet(net);
481bf355b8dSDavid Lebrun 
482bf355b8dSDavid Lebrun 	rhashtable_free_and_destroy(&sdata->hmac_infos, seg6_free_hi, NULL);
483bf355b8dSDavid Lebrun }
484bf355b8dSDavid Lebrun EXPORT_SYMBOL(seg6_hmac_net_exit);
485