xref: /openbmc/linux/net/ipv6/seg6_hmac.c (revision 4bb1eb3c)
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 
44 static DEFINE_PER_CPU(char [SEG6_HMAC_RING_SIZE], hmac_ring);
45 
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 
53 static inline void seg6_hinfo_release(struct seg6_hmac_info *hinfo)
54 {
55 	kfree_rcu(hinfo, rcu);
56 }
57 
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 
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 
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 
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 
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  */
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 
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 }
278 EXPORT_SYMBOL(seg6_hmac_validate_skb);
279 
280 /* called with rcu_read_lock() */
281 struct 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 }
290 EXPORT_SYMBOL(seg6_hmac_info_lookup);
291 
292 int 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 }
302 EXPORT_SYMBOL(seg6_hmac_info_add);
303 
304 int 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 
321 out:
322 	return err;
323 }
324 EXPORT_SYMBOL(seg6_hmac_info_del);
325 
326 int 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 
346 out:
347 	rcu_read_unlock();
348 	return err;
349 }
350 EXPORT_SYMBOL(seg6_push_hmac);
351 
352 static 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 
399 int __init seg6_hmac_init(void)
400 {
401 	return seg6_hmac_init_algo();
402 }
403 EXPORT_SYMBOL(seg6_hmac_init);
404 
405 int __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 }
413 EXPORT_SYMBOL(seg6_hmac_net_init);
414 
415 void 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 }
436 EXPORT_SYMBOL(seg6_hmac_exit);
437 
438 void __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 }
444 EXPORT_SYMBOL(seg6_hmac_net_exit);
445