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