• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/utils.h>
39 #include <net/seg6.h>
40 #include <net/genetlink.h>
41 #include <net/seg6_hmac.h>
42 #include <linux/random.h>
43 
44 static DEFINE_PER_CPU(char [SEG6_HMAC_RING_SIZE], hmac_ring);
45 
seg6_hmac_cmpfn(struct rhashtable_compare_arg * arg,const void * obj)46 static 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 
seg6_hinfo_release(struct seg6_hmac_info * hinfo)53 static inline void seg6_hinfo_release(struct seg6_hmac_info *hinfo)
54 {
55 	kfree_rcu(hinfo, rcu);
56 }
57 
seg6_free_hi(void * ptr,void * arg)58 static 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 
66 static 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 
74 static 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 
seg6_get_tlv_hmac(struct ipv6_sr_hdr * srh)85 static 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 
__hmac_get_algo(u8 alg_id)104 static 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 
__do_hmac(struct seg6_hmac_info * hinfo,const char * text,u8 psize,u8 * output,int outlen)119 static 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 
157 failed:
158 	return ret;
159 }
160 
seg6_hmac_compute(struct seg6_hmac_info * hinfo,struct ipv6_sr_hdr * hdr,struct in6_addr * saddr,u8 * output)161 int 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 }
230 EXPORT_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  */
seg6_hmac_validate_skb(struct sk_buff * skb)237 bool 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 	int require_hmac;
246 
247 	idev = __in6_dev_get(skb->dev);
248 
249 	srh = (struct ipv6_sr_hdr *)skb_transport_header(skb);
250 
251 	tlv = seg6_get_tlv_hmac(srh);
252 
253 	require_hmac = READ_ONCE(idev->cnf.seg6_require_hmac);
254 	/* mandatory check but no tlv */
255 	if (require_hmac > 0 && !tlv)
256 		return false;
257 
258 	/* no check */
259 	if (require_hmac < 0)
260 		return true;
261 
262 	/* check only if present */
263 	if (require_hmac == 0 && !tlv)
264 		return true;
265 
266 	/* now, seg6_require_hmac >= 0 && tlv */
267 
268 	hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
269 	if (!hinfo)
270 		return false;
271 
272 	if (seg6_hmac_compute(hinfo, srh, &ipv6_hdr(skb)->saddr, hmac_output))
273 		return false;
274 
275 	if (crypto_memneq(hmac_output, tlv->hmac, SEG6_HMAC_FIELD_LEN))
276 		return false;
277 
278 	return true;
279 }
280 EXPORT_SYMBOL(seg6_hmac_validate_skb);
281 
282 /* called with rcu_read_lock() */
seg6_hmac_info_lookup(struct net * net,u32 key)283 struct seg6_hmac_info *seg6_hmac_info_lookup(struct net *net, u32 key)
284 {
285 	struct seg6_pernet_data *sdata = seg6_pernet(net);
286 	struct seg6_hmac_info *hinfo;
287 
288 	hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
289 
290 	return hinfo;
291 }
292 EXPORT_SYMBOL(seg6_hmac_info_lookup);
293 
seg6_hmac_info_add(struct net * net,u32 key,struct seg6_hmac_info * hinfo)294 int seg6_hmac_info_add(struct net *net, u32 key, struct seg6_hmac_info *hinfo)
295 {
296 	struct seg6_pernet_data *sdata = seg6_pernet(net);
297 	int err;
298 
299 	if (!__hmac_get_algo(hinfo->alg_id))
300 		return -EINVAL;
301 
302 	err = rhashtable_lookup_insert_fast(&sdata->hmac_infos, &hinfo->node,
303 					    rht_params);
304 
305 	return err;
306 }
307 EXPORT_SYMBOL(seg6_hmac_info_add);
308 
seg6_hmac_info_del(struct net * net,u32 key)309 int seg6_hmac_info_del(struct net *net, u32 key)
310 {
311 	struct seg6_pernet_data *sdata = seg6_pernet(net);
312 	struct seg6_hmac_info *hinfo;
313 	int err = -ENOENT;
314 
315 	hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params);
316 	if (!hinfo)
317 		goto out;
318 
319 	err = rhashtable_remove_fast(&sdata->hmac_infos, &hinfo->node,
320 				     rht_params);
321 	if (err)
322 		goto out;
323 
324 	seg6_hinfo_release(hinfo);
325 
326 out:
327 	return err;
328 }
329 EXPORT_SYMBOL(seg6_hmac_info_del);
330 
seg6_push_hmac(struct net * net,struct in6_addr * saddr,struct ipv6_sr_hdr * srh)331 int seg6_push_hmac(struct net *net, struct in6_addr *saddr,
332 		   struct ipv6_sr_hdr *srh)
333 {
334 	struct seg6_hmac_info *hinfo;
335 	struct sr6_tlv_hmac *tlv;
336 	int err = -ENOENT;
337 
338 	tlv = seg6_get_tlv_hmac(srh);
339 	if (!tlv)
340 		return -EINVAL;
341 
342 	rcu_read_lock();
343 
344 	hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid));
345 	if (!hinfo)
346 		goto out;
347 
348 	memset(tlv->hmac, 0, SEG6_HMAC_FIELD_LEN);
349 	err = seg6_hmac_compute(hinfo, srh, saddr, tlv->hmac);
350 
351 out:
352 	rcu_read_unlock();
353 	return err;
354 }
355 EXPORT_SYMBOL(seg6_push_hmac);
356 
seg6_hmac_init_algo(void)357 static int seg6_hmac_init_algo(void)
358 {
359 	struct seg6_hmac_algo *algo;
360 	struct crypto_shash *tfm;
361 	struct shash_desc *shash;
362 	int i, alg_count, cpu;
363 	int ret = -ENOMEM;
364 
365 	alg_count = ARRAY_SIZE(hmac_algos);
366 
367 	for (i = 0; i < alg_count; i++) {
368 		struct crypto_shash **p_tfm;
369 		int shsize;
370 
371 		algo = &hmac_algos[i];
372 		algo->tfms = alloc_percpu(struct crypto_shash *);
373 		if (!algo->tfms)
374 			goto error_out;
375 
376 		for_each_possible_cpu(cpu) {
377 			tfm = crypto_alloc_shash(algo->name, 0, 0);
378 			if (IS_ERR(tfm)) {
379 				ret = PTR_ERR(tfm);
380 				goto error_out;
381 			}
382 			p_tfm = per_cpu_ptr(algo->tfms, cpu);
383 			*p_tfm = tfm;
384 		}
385 
386 		p_tfm = raw_cpu_ptr(algo->tfms);
387 		tfm = *p_tfm;
388 
389 		shsize = sizeof(*shash) + crypto_shash_descsize(tfm);
390 
391 		algo->shashs = alloc_percpu(struct shash_desc *);
392 		if (!algo->shashs)
393 			goto error_out;
394 
395 		for_each_possible_cpu(cpu) {
396 			shash = kzalloc_node(shsize, GFP_KERNEL,
397 					     cpu_to_node(cpu));
398 			if (!shash)
399 				goto error_out;
400 			*per_cpu_ptr(algo->shashs, cpu) = shash;
401 		}
402 	}
403 
404 	return 0;
405 
406 error_out:
407 	seg6_hmac_exit();
408 	return ret;
409 }
410 
seg6_hmac_init(void)411 int __init seg6_hmac_init(void)
412 {
413 	return seg6_hmac_init_algo();
414 }
415 
seg6_hmac_net_init(struct net * net)416 int __net_init seg6_hmac_net_init(struct net *net)
417 {
418 	struct seg6_pernet_data *sdata = seg6_pernet(net);
419 
420 	return rhashtable_init(&sdata->hmac_infos, &rht_params);
421 }
422 
seg6_hmac_exit(void)423 void seg6_hmac_exit(void)
424 {
425 	struct seg6_hmac_algo *algo = NULL;
426 	struct crypto_shash *tfm;
427 	struct shash_desc *shash;
428 	int i, alg_count, cpu;
429 
430 	alg_count = ARRAY_SIZE(hmac_algos);
431 	for (i = 0; i < alg_count; i++) {
432 		algo = &hmac_algos[i];
433 
434 		if (algo->shashs) {
435 			for_each_possible_cpu(cpu) {
436 				shash = *per_cpu_ptr(algo->shashs, cpu);
437 				kfree(shash);
438 			}
439 			free_percpu(algo->shashs);
440 		}
441 
442 		if (algo->tfms) {
443 			for_each_possible_cpu(cpu) {
444 				tfm = *per_cpu_ptr(algo->tfms, cpu);
445 				crypto_free_shash(tfm);
446 			}
447 			free_percpu(algo->tfms);
448 		}
449 	}
450 }
451 EXPORT_SYMBOL(seg6_hmac_exit);
452 
seg6_hmac_net_exit(struct net * net)453 void __net_exit seg6_hmac_net_exit(struct net *net)
454 {
455 	struct seg6_pernet_data *sdata = seg6_pernet(net);
456 
457 	rhashtable_free_and_destroy(&sdata->hmac_infos, seg6_free_hi, NULL);
458 }
459 EXPORT_SYMBOL(seg6_hmac_net_exit);
460