Linux Audio

Check our new training course

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