xref: /openbmc/linux/net/ipv6/seg6_hmac.c (revision f66501dc)
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 <linux/cryptohash.h>
38 #include <crypto/hash.h>
39 #include <crypto/sha.h>
40 #include <net/seg6.h>
41 #include <net/genetlink.h>
42 #include <net/seg6_hmac.h>
43 #include <linux/random.h>
44 
45 static DEFINE_PER_CPU(char [SEG6_HMAC_RING_SIZE], hmac_ring);
46 
47 static int seg6_hmac_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
48 {
49 	const struct seg6_hmac_info *hinfo = obj;
50 
51 	return (hinfo->hmackeyid != *(__u32 *)arg->key);
52 }
53 
54 static inline void seg6_hinfo_release(struct seg6_hmac_info *hinfo)
55 {
56 	kfree_rcu(hinfo, rcu);
57 }
58 
59 static void seg6_free_hi(void *ptr, void *arg)
60 {
61 	struct seg6_hmac_info *hinfo = (struct seg6_hmac_info *)ptr;
62 
63 	if (hinfo)
64 		seg6_hinfo_release(hinfo);
65 }
66 
67 static const struct rhashtable_params rht_params = {
68 	.head_offset		= offsetof(struct seg6_hmac_info, node),
69 	.key_offset		= offsetof(struct seg6_hmac_info, hmackeyid),
70 	.key_len		= sizeof(u32),
71 	.automatic_shrinking	= true,
72 	.obj_cmpfn		= seg6_hmac_cmpfn,
73 };
74 
75 static struct seg6_hmac_algo hmac_algos[] = {
76 	{
77 		.alg_id = SEG6_HMAC_ALGO_SHA1,
78 		.name = "hmac(sha1)",
79 	},
80 	{
81 		.alg_id = SEG6_HMAC_ALGO_SHA256,
82 		.name = "hmac(sha256)",
83 	},
84 };
85 
86 static struct sr6_tlv_hmac *seg6_get_tlv_hmac(struct ipv6_sr_hdr *srh)
87 {
88 	struct sr6_tlv_hmac *tlv;
89 
90 	if (srh->hdrlen < (srh->first_segment + 1) * 2 + 5)
91 		return NULL;
92 
93 	if (!sr_has_hmac(srh))
94 		return NULL;
95 
96 	tlv = (struct sr6_tlv_hmac *)
97 	      ((char *)srh + ((srh->hdrlen + 1) << 3) - 40);
98 
99 	if (tlv->tlvhdr.type != SR6_TLV_HMAC || tlv->tlvhdr.len != 38)
100 		return NULL;
101 
102 	return tlv;
103 }
104 
105 static struct seg6_hmac_algo *__hmac_get_algo(u8 alg_id)
106 {
107 	struct seg6_hmac_algo *algo;
108 	int i, alg_count;
109 
110 	alg_count = ARRAY_SIZE(hmac_algos);
111 	for (i = 0; i < alg_count; i++) {
112 		algo = &hmac_algos[i];
113 		if (algo->alg_id == alg_id)
114 			return algo;
115 	}
116 
117 	return NULL;
118 }
119 
120 static int __do_hmac(struct seg6_hmac_info *hinfo, const char *text, u8 psize,
121 		     u8 *output, int outlen)
122 {
123 	struct seg6_hmac_algo *algo;
124 	struct crypto_shash *tfm;
125 	struct shash_desc *shash;
126 	int ret, dgsize;
127 
128 	algo = __hmac_get_algo(hinfo->alg_id);
129 	if (!algo)
130 		return -ENOENT;
131 
132 	tfm = *this_cpu_ptr(algo->tfms);
133 
134 	dgsize = crypto_shash_digestsize(tfm);
135 	if (dgsize > outlen) {
136 		pr_debug("sr-ipv6: __do_hmac: digest size too big (%d / %d)\n",
137 			 dgsize, outlen);
138 		return -ENOMEM;
139 	}
140 
141 	ret = crypto_shash_setkey(tfm, hinfo->secret, hinfo->slen);
142 	if (ret < 0) {
143 		pr_debug("sr-ipv6: crypto_shash_setkey failed: err %d\n", ret);
144 		goto failed;
145 	}
146 
147 	shash = *this_cpu_ptr(algo->shashs);
148 	shash->tfm = tfm;
149 
150 	ret = crypto_shash_digest(shash, text, psize, output);
151 	if (ret < 0) {
152 		pr_debug("sr-ipv6: crypto_shash_digest failed: err %d\n", ret);
153 		goto failed;
154 	}
155 
156 	return dgsize;
157 
158 failed:
159 	return ret;
160 }
161 
162 int seg6_hmac_compute(struct seg6_hmac_info *hinfo, struct ipv6_sr_hdr *hdr,
163 		      struct in6_addr *saddr, u8 *output)
164 {
165 	__be32 hmackeyid = cpu_to_be32(hinfo->hmackeyid);
166 	u8 tmp_out[SEG6_HMAC_MAX_DIGESTSIZE];
167 	int plen, i, dgsize, wrsize;
168 	char *ring, *off;
169 
170 	/* a 160-byte buffer for digest output allows to store highest known
171 	 * hash function (RadioGatun) with up to 1216 bits
172 	 */
173 
174 	/* saddr(16) + first_seg(1) + flags(1) + keyid(4) + seglist(16n) */
175 	plen = 16 + 1 + 1 + 4 + (hdr->first_segment + 1) * 16;
176 
177 	/* this limit allows for 14 segments */
178 	if (plen >= SEG6_HMAC_RING_SIZE)
179 		return -EMSGSIZE;
180 
181 	/* Let's build the HMAC text on the ring buffer. The text is composed
182 	 * as follows, in order:
183 	 *
184 	 * 1. Source IPv6 address (128 bits)
185 	 * 2. first_segment value (8 bits)
186 	 * 3. Flags (8 bits)
187 	 * 4. HMAC Key ID (32 bits)
188 	 * 5. All segments in the segments list (n * 128 bits)
189 	 */
190 
191 	local_bh_disable();
192 	ring = this_cpu_ptr(hmac_ring);
193 	off = ring;
194 
195 	/* source address */
196 	memcpy(off, saddr, 16);
197 	off += 16;
198 
199 	/* first_segment value */
200 	*off++ = hdr->first_segment;
201 
202 	/* flags */
203 	*off++ = hdr->flags;
204 
205 	/* HMAC Key ID */
206 	memcpy(off, &hmackeyid, 4);
207 	off += 4;
208 
209 	/* all segments in the list */
210 	for (i = 0; i < hdr->first_segment + 1; i++) {
211 		memcpy(off, hdr->segments + i, 16);
212 		off += 16;
213 	}
214 
215 	dgsize = __do_hmac(hinfo, ring, plen, tmp_out,
216 			   SEG6_HMAC_MAX_DIGESTSIZE);
217 	local_bh_enable();
218 
219 	if (dgsize < 0)
220 		return dgsize;
221 
222 	wrsize = SEG6_HMAC_FIELD_LEN;
223 	if (wrsize > dgsize)
224 		wrsize = dgsize;
225 
226 	memset(output, 0, SEG6_HMAC_FIELD_LEN);
227 	memcpy(output, tmp_out, wrsize);
228 
229 	return 0;
230 }
231 EXPORT_SYMBOL(seg6_hmac_compute);
232 
233 /* checks if an incoming SR-enabled packet's HMAC status matches
234  * the incoming policy.
235  *
236  * called with rcu_read_lock()
237  */
238 bool seg6_hmac_validate_skb(struct sk_buff *skb)
239 {
240 	u8 hmac_output[SEG6_HMAC_FIELD_LEN];
241 	struct net *net = dev_net(skb->dev);
242 	struct seg6_hmac_info *hinfo;
243 	struct sr6_tlv_hmac *tlv;
244 	struct ipv6_sr_hdr *srh;
245 	struct inet6_dev *idev;
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 	/* mandatory check but no tlv */
254 	if (idev->cnf.seg6_require_hmac > 0 && !tlv)
255 		return false;
256 
257 	/* no check */
258 	if (idev->cnf.seg6_require_hmac < 0)
259 		return true;
260 
261 	/* check only if present */
262 	if (idev->cnf.seg6_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 }
279 EXPORT_SYMBOL(seg6_hmac_validate_skb);
280 
281 /* called with rcu_read_lock() */
282 struct 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 }
291 EXPORT_SYMBOL(seg6_hmac_info_lookup);
292 
293 int 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 }
303 EXPORT_SYMBOL(seg6_hmac_info_add);
304 
305 int 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 
322 out:
323 	return err;
324 }
325 EXPORT_SYMBOL(seg6_hmac_info_del);
326 
327 int 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 
347 out:
348 	rcu_read_unlock();
349 	return err;
350 }
351 EXPORT_SYMBOL(seg6_push_hmac);
352 
353 static 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 
360 	alg_count = ARRAY_SIZE(hmac_algos);
361 
362 	for (i = 0; i < alg_count; i++) {
363 		struct crypto_shash **p_tfm;
364 		int shsize;
365 
366 		algo = &hmac_algos[i];
367 		algo->tfms = alloc_percpu(struct crypto_shash *);
368 		if (!algo->tfms)
369 			return -ENOMEM;
370 
371 		for_each_possible_cpu(cpu) {
372 			tfm = crypto_alloc_shash(algo->name, 0, 0);
373 			if (IS_ERR(tfm))
374 				return PTR_ERR(tfm);
375 			p_tfm = per_cpu_ptr(algo->tfms, cpu);
376 			*p_tfm = tfm;
377 		}
378 
379 		p_tfm = raw_cpu_ptr(algo->tfms);
380 		tfm = *p_tfm;
381 
382 		shsize = sizeof(*shash) + crypto_shash_descsize(tfm);
383 
384 		algo->shashs = alloc_percpu(struct shash_desc *);
385 		if (!algo->shashs)
386 			return -ENOMEM;
387 
388 		for_each_possible_cpu(cpu) {
389 			shash = kzalloc_node(shsize, GFP_KERNEL,
390 					     cpu_to_node(cpu));
391 			if (!shash)
392 				return -ENOMEM;
393 			*per_cpu_ptr(algo->shashs, cpu) = shash;
394 		}
395 	}
396 
397 	return 0;
398 }
399 
400 int __init seg6_hmac_init(void)
401 {
402 	return seg6_hmac_init_algo();
403 }
404 EXPORT_SYMBOL(seg6_hmac_init);
405 
406 int __net_init seg6_hmac_net_init(struct net *net)
407 {
408 	struct seg6_pernet_data *sdata = seg6_pernet(net);
409 
410 	rhashtable_init(&sdata->hmac_infos, &rht_params);
411 
412 	return 0;
413 }
414 EXPORT_SYMBOL(seg6_hmac_net_init);
415 
416 void seg6_hmac_exit(void)
417 {
418 	struct seg6_hmac_algo *algo = NULL;
419 	int i, alg_count, cpu;
420 
421 	alg_count = ARRAY_SIZE(hmac_algos);
422 	for (i = 0; i < alg_count; i++) {
423 		algo = &hmac_algos[i];
424 		for_each_possible_cpu(cpu) {
425 			struct crypto_shash *tfm;
426 			struct shash_desc *shash;
427 
428 			shash = *per_cpu_ptr(algo->shashs, cpu);
429 			kfree(shash);
430 			tfm = *per_cpu_ptr(algo->tfms, cpu);
431 			crypto_free_shash(tfm);
432 		}
433 		free_percpu(algo->tfms);
434 		free_percpu(algo->shashs);
435 	}
436 }
437 EXPORT_SYMBOL(seg6_hmac_exit);
438 
439 void __net_exit seg6_hmac_net_exit(struct net *net)
440 {
441 	struct seg6_pernet_data *sdata = seg6_pernet(net);
442 
443 	rhashtable_free_and_destroy(&sdata->hmac_infos, seg6_free_hi, NULL);
444 }
445 EXPORT_SYMBOL(seg6_hmac_net_exit);
446