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