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