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