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 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 59 static inline void seg6_hinfo_release(struct seg6_hmac_info *hinfo) 60 { 61 kfree_rcu(hinfo, rcu); 62 } 63 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 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 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 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 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 */ 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() */ 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 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 if (!__hmac_get_algo(hinfo->alg_id)) 308 return -EINVAL; 309 310 err = rhashtable_lookup_insert_fast(&sdata->hmac_infos, &hinfo->node, 311 rht_params); 312 313 return err; 314 } 315 EXPORT_SYMBOL(seg6_hmac_info_add); 316 317 int seg6_hmac_info_del(struct net *net, u32 key) 318 { 319 struct seg6_pernet_data *sdata = seg6_pernet(net); 320 struct seg6_hmac_info *hinfo; 321 int err = -ENOENT; 322 323 hinfo = rhashtable_lookup_fast(&sdata->hmac_infos, &key, rht_params); 324 if (!hinfo) 325 goto out; 326 327 err = rhashtable_remove_fast(&sdata->hmac_infos, &hinfo->node, 328 rht_params); 329 if (err) 330 goto out; 331 332 seg6_hinfo_release(hinfo); 333 334 out: 335 return err; 336 } 337 EXPORT_SYMBOL(seg6_hmac_info_del); 338 339 int seg6_push_hmac(struct net *net, struct in6_addr *saddr, 340 struct ipv6_sr_hdr *srh) 341 { 342 struct seg6_hmac_info *hinfo; 343 struct sr6_tlv_hmac *tlv; 344 int err = -ENOENT; 345 346 tlv = seg6_get_tlv_hmac(srh); 347 if (!tlv) 348 return -EINVAL; 349 350 rcu_read_lock(); 351 352 hinfo = seg6_hmac_info_lookup(net, be32_to_cpu(tlv->hmackeyid)); 353 if (!hinfo) 354 goto out; 355 356 memset(tlv->hmac, 0, SEG6_HMAC_FIELD_LEN); 357 err = seg6_hmac_compute(hinfo, srh, saddr, tlv->hmac); 358 359 out: 360 rcu_read_unlock(); 361 return err; 362 } 363 EXPORT_SYMBOL(seg6_push_hmac); 364 365 static int seg6_hmac_init_algo(void) 366 { 367 struct seg6_hmac_algo *algo; 368 struct crypto_shash *tfm; 369 struct shash_desc *shash; 370 int i, alg_count, cpu; 371 int ret = -ENOMEM; 372 373 alg_count = ARRAY_SIZE(hmac_algos); 374 375 for (i = 0; i < alg_count; i++) { 376 struct crypto_shash **p_tfm; 377 int shsize; 378 379 algo = &hmac_algos[i]; 380 algo->tfms = alloc_percpu(struct crypto_shash *); 381 if (!algo->tfms) 382 goto error_out; 383 384 for_each_possible_cpu(cpu) { 385 tfm = crypto_alloc_shash(algo->name, 0, 0); 386 if (IS_ERR(tfm)) { 387 ret = PTR_ERR(tfm); 388 goto error_out; 389 } 390 p_tfm = per_cpu_ptr(algo->tfms, cpu); 391 *p_tfm = tfm; 392 } 393 394 p_tfm = raw_cpu_ptr(algo->tfms); 395 tfm = *p_tfm; 396 397 shsize = sizeof(*shash) + crypto_shash_descsize(tfm); 398 399 algo->shashs = alloc_percpu(struct shash_desc *); 400 if (!algo->shashs) 401 goto error_out; 402 403 for_each_possible_cpu(cpu) { 404 shash = kzalloc_node(shsize, GFP_KERNEL, 405 cpu_to_node(cpu)); 406 if (!shash) 407 goto error_out; 408 *per_cpu_ptr(algo->shashs, cpu) = shash; 409 } 410 } 411 412 return 0; 413 414 error_out: 415 seg6_hmac_exit(); 416 return ret; 417 } 418 419 int __init seg6_hmac_init(void) 420 { 421 return seg6_hmac_init_algo(); 422 } 423 424 int __net_init seg6_hmac_net_init(struct net *net) 425 { 426 struct seg6_pernet_data *sdata = seg6_pernet(net); 427 428 return rhashtable_init(&sdata->hmac_infos, &rht_params); 429 } 430 431 void seg6_hmac_exit(void) 432 { 433 struct seg6_hmac_algo *algo = NULL; 434 struct crypto_shash *tfm; 435 struct shash_desc *shash; 436 int i, alg_count, cpu; 437 438 alg_count = ARRAY_SIZE(hmac_algos); 439 for (i = 0; i < alg_count; i++) { 440 algo = &hmac_algos[i]; 441 442 if (algo->shashs) { 443 for_each_possible_cpu(cpu) { 444 shash = *per_cpu_ptr(algo->shashs, cpu); 445 kfree(shash); 446 } 447 free_percpu(algo->shashs); 448 } 449 450 if (algo->tfms) { 451 for_each_possible_cpu(cpu) { 452 tfm = *per_cpu_ptr(algo->tfms, cpu); 453 crypto_free_shash(tfm); 454 } 455 free_percpu(algo->tfms); 456 } 457 } 458 } 459 EXPORT_SYMBOL(seg6_hmac_exit); 460 461 void __net_exit seg6_hmac_net_exit(struct net *net) 462 { 463 struct seg6_pernet_data *sdata = seg6_pernet(net); 464 465 rhashtable_free_and_destroy(&sdata->hmac_infos, seg6_free_hi, NULL); 466 } 467 EXPORT_SYMBOL(seg6_hmac_net_exit); 468