1915d7e5eSDavid Lebrun /* 2915d7e5eSDavid Lebrun * SR-IPv6 implementation 3915d7e5eSDavid Lebrun * 4915d7e5eSDavid Lebrun * Author: 5915d7e5eSDavid Lebrun * David Lebrun <david.lebrun@uclouvain.be> 6915d7e5eSDavid Lebrun * 7915d7e5eSDavid Lebrun * 8915d7e5eSDavid Lebrun * This program is free software; you can redistribute it and/or 9915d7e5eSDavid Lebrun * modify it under the terms of the GNU General Public License 10915d7e5eSDavid Lebrun * as published by the Free Software Foundation; either version 11915d7e5eSDavid Lebrun * 2 of the License, or (at your option) any later version. 12915d7e5eSDavid Lebrun */ 13915d7e5eSDavid Lebrun 14915d7e5eSDavid Lebrun #include <linux/errno.h> 15915d7e5eSDavid Lebrun #include <linux/types.h> 16915d7e5eSDavid Lebrun #include <linux/socket.h> 17915d7e5eSDavid Lebrun #include <linux/net.h> 18915d7e5eSDavid Lebrun #include <linux/in6.h> 19915d7e5eSDavid Lebrun #include <linux/slab.h> 20915d7e5eSDavid Lebrun 21915d7e5eSDavid Lebrun #include <net/ipv6.h> 22915d7e5eSDavid Lebrun #include <net/protocol.h> 23915d7e5eSDavid Lebrun 24915d7e5eSDavid Lebrun #include <net/seg6.h> 25915d7e5eSDavid Lebrun #include <net/genetlink.h> 26915d7e5eSDavid Lebrun #include <linux/seg6.h> 27915d7e5eSDavid Lebrun #include <linux/seg6_genl.h> 28*4f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 29*4f4853dcSDavid Lebrun #include <net/seg6_hmac.h> 30*4f4853dcSDavid Lebrun #endif 31915d7e5eSDavid Lebrun 326c8702c6SDavid Lebrun bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len) 336c8702c6SDavid Lebrun { 346c8702c6SDavid Lebrun int trailing; 356c8702c6SDavid Lebrun unsigned int tlv_offset; 366c8702c6SDavid Lebrun 376c8702c6SDavid Lebrun if (srh->type != IPV6_SRCRT_TYPE_4) 386c8702c6SDavid Lebrun return false; 396c8702c6SDavid Lebrun 406c8702c6SDavid Lebrun if (((srh->hdrlen + 1) << 3) != len) 416c8702c6SDavid Lebrun return false; 426c8702c6SDavid Lebrun 436c8702c6SDavid Lebrun if (srh->segments_left != srh->first_segment) 446c8702c6SDavid Lebrun return false; 456c8702c6SDavid Lebrun 466c8702c6SDavid Lebrun tlv_offset = sizeof(*srh) + ((srh->first_segment + 1) << 4); 476c8702c6SDavid Lebrun 486c8702c6SDavid Lebrun trailing = len - tlv_offset; 496c8702c6SDavid Lebrun if (trailing < 0) 506c8702c6SDavid Lebrun return false; 516c8702c6SDavid Lebrun 526c8702c6SDavid Lebrun while (trailing) { 536c8702c6SDavid Lebrun struct sr6_tlv *tlv; 546c8702c6SDavid Lebrun unsigned int tlv_len; 556c8702c6SDavid Lebrun 566c8702c6SDavid Lebrun tlv = (struct sr6_tlv *)((unsigned char *)srh + tlv_offset); 576c8702c6SDavid Lebrun tlv_len = sizeof(*tlv) + tlv->len; 586c8702c6SDavid Lebrun 596c8702c6SDavid Lebrun trailing -= tlv_len; 606c8702c6SDavid Lebrun if (trailing < 0) 616c8702c6SDavid Lebrun return false; 626c8702c6SDavid Lebrun 636c8702c6SDavid Lebrun tlv_offset += tlv_len; 646c8702c6SDavid Lebrun } 656c8702c6SDavid Lebrun 666c8702c6SDavid Lebrun return true; 676c8702c6SDavid Lebrun } 686c8702c6SDavid Lebrun 69915d7e5eSDavid Lebrun static struct genl_family seg6_genl_family; 70915d7e5eSDavid Lebrun 71915d7e5eSDavid Lebrun static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX + 1] = { 72915d7e5eSDavid Lebrun [SEG6_ATTR_DST] = { .type = NLA_BINARY, 73915d7e5eSDavid Lebrun .len = sizeof(struct in6_addr) }, 74915d7e5eSDavid Lebrun [SEG6_ATTR_DSTLEN] = { .type = NLA_S32, }, 75915d7e5eSDavid Lebrun [SEG6_ATTR_HMACKEYID] = { .type = NLA_U32, }, 76915d7e5eSDavid Lebrun [SEG6_ATTR_SECRET] = { .type = NLA_BINARY, }, 77915d7e5eSDavid Lebrun [SEG6_ATTR_SECRETLEN] = { .type = NLA_U8, }, 78915d7e5eSDavid Lebrun [SEG6_ATTR_ALGID] = { .type = NLA_U8, }, 79915d7e5eSDavid Lebrun [SEG6_ATTR_HMACINFO] = { .type = NLA_NESTED, }, 80915d7e5eSDavid Lebrun }; 81915d7e5eSDavid Lebrun 82*4f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 83*4f4853dcSDavid Lebrun 84*4f4853dcSDavid Lebrun static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info) 85*4f4853dcSDavid Lebrun { 86*4f4853dcSDavid Lebrun struct net *net = genl_info_net(info); 87*4f4853dcSDavid Lebrun struct seg6_pernet_data *sdata; 88*4f4853dcSDavid Lebrun struct seg6_hmac_info *hinfo; 89*4f4853dcSDavid Lebrun u32 hmackeyid; 90*4f4853dcSDavid Lebrun char *secret; 91*4f4853dcSDavid Lebrun int err = 0; 92*4f4853dcSDavid Lebrun u8 algid; 93*4f4853dcSDavid Lebrun u8 slen; 94*4f4853dcSDavid Lebrun 95*4f4853dcSDavid Lebrun sdata = seg6_pernet(net); 96*4f4853dcSDavid Lebrun 97*4f4853dcSDavid Lebrun if (!info->attrs[SEG6_ATTR_HMACKEYID] || 98*4f4853dcSDavid Lebrun !info->attrs[SEG6_ATTR_SECRETLEN] || 99*4f4853dcSDavid Lebrun !info->attrs[SEG6_ATTR_ALGID]) 100*4f4853dcSDavid Lebrun return -EINVAL; 101*4f4853dcSDavid Lebrun 102*4f4853dcSDavid Lebrun hmackeyid = nla_get_u32(info->attrs[SEG6_ATTR_HMACKEYID]); 103*4f4853dcSDavid Lebrun slen = nla_get_u8(info->attrs[SEG6_ATTR_SECRETLEN]); 104*4f4853dcSDavid Lebrun algid = nla_get_u8(info->attrs[SEG6_ATTR_ALGID]); 105*4f4853dcSDavid Lebrun 106*4f4853dcSDavid Lebrun if (hmackeyid == 0) 107*4f4853dcSDavid Lebrun return -EINVAL; 108*4f4853dcSDavid Lebrun 109*4f4853dcSDavid Lebrun if (slen > SEG6_HMAC_SECRET_LEN) 110*4f4853dcSDavid Lebrun return -EINVAL; 111*4f4853dcSDavid Lebrun 112*4f4853dcSDavid Lebrun mutex_lock(&sdata->lock); 113*4f4853dcSDavid Lebrun hinfo = seg6_hmac_info_lookup(net, hmackeyid); 114*4f4853dcSDavid Lebrun 115*4f4853dcSDavid Lebrun if (!slen) { 116*4f4853dcSDavid Lebrun if (!hinfo) 117*4f4853dcSDavid Lebrun err = -ENOENT; 118*4f4853dcSDavid Lebrun 119*4f4853dcSDavid Lebrun err = seg6_hmac_info_del(net, hmackeyid); 120*4f4853dcSDavid Lebrun 121*4f4853dcSDavid Lebrun goto out_unlock; 122*4f4853dcSDavid Lebrun } 123*4f4853dcSDavid Lebrun 124*4f4853dcSDavid Lebrun if (!info->attrs[SEG6_ATTR_SECRET]) { 125*4f4853dcSDavid Lebrun err = -EINVAL; 126*4f4853dcSDavid Lebrun goto out_unlock; 127*4f4853dcSDavid Lebrun } 128*4f4853dcSDavid Lebrun 129*4f4853dcSDavid Lebrun if (hinfo) { 130*4f4853dcSDavid Lebrun err = seg6_hmac_info_del(net, hmackeyid); 131*4f4853dcSDavid Lebrun if (err) 132*4f4853dcSDavid Lebrun goto out_unlock; 133*4f4853dcSDavid Lebrun } 134*4f4853dcSDavid Lebrun 135*4f4853dcSDavid Lebrun secret = (char *)nla_data(info->attrs[SEG6_ATTR_SECRET]); 136*4f4853dcSDavid Lebrun 137*4f4853dcSDavid Lebrun hinfo = kzalloc(sizeof(*hinfo), GFP_KERNEL); 138*4f4853dcSDavid Lebrun if (!hinfo) { 139*4f4853dcSDavid Lebrun err = -ENOMEM; 140*4f4853dcSDavid Lebrun goto out_unlock; 141*4f4853dcSDavid Lebrun } 142*4f4853dcSDavid Lebrun 143*4f4853dcSDavid Lebrun memcpy(hinfo->secret, secret, slen); 144*4f4853dcSDavid Lebrun hinfo->slen = slen; 145*4f4853dcSDavid Lebrun hinfo->alg_id = algid; 146*4f4853dcSDavid Lebrun hinfo->hmackeyid = hmackeyid; 147*4f4853dcSDavid Lebrun 148*4f4853dcSDavid Lebrun err = seg6_hmac_info_add(net, hmackeyid, hinfo); 149*4f4853dcSDavid Lebrun if (err) 150*4f4853dcSDavid Lebrun kfree(hinfo); 151*4f4853dcSDavid Lebrun 152*4f4853dcSDavid Lebrun out_unlock: 153*4f4853dcSDavid Lebrun mutex_unlock(&sdata->lock); 154*4f4853dcSDavid Lebrun return err; 155*4f4853dcSDavid Lebrun } 156*4f4853dcSDavid Lebrun 157*4f4853dcSDavid Lebrun #else 158*4f4853dcSDavid Lebrun 159915d7e5eSDavid Lebrun static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info) 160915d7e5eSDavid Lebrun { 161915d7e5eSDavid Lebrun return -ENOTSUPP; 162915d7e5eSDavid Lebrun } 163915d7e5eSDavid Lebrun 164*4f4853dcSDavid Lebrun #endif 165*4f4853dcSDavid Lebrun 166915d7e5eSDavid Lebrun static int seg6_genl_set_tunsrc(struct sk_buff *skb, struct genl_info *info) 167915d7e5eSDavid Lebrun { 168915d7e5eSDavid Lebrun struct net *net = genl_info_net(info); 169915d7e5eSDavid Lebrun struct in6_addr *val, *t_old, *t_new; 170915d7e5eSDavid Lebrun struct seg6_pernet_data *sdata; 171915d7e5eSDavid Lebrun 172915d7e5eSDavid Lebrun sdata = seg6_pernet(net); 173915d7e5eSDavid Lebrun 174915d7e5eSDavid Lebrun if (!info->attrs[SEG6_ATTR_DST]) 175915d7e5eSDavid Lebrun return -EINVAL; 176915d7e5eSDavid Lebrun 177915d7e5eSDavid Lebrun val = nla_data(info->attrs[SEG6_ATTR_DST]); 178915d7e5eSDavid Lebrun t_new = kmemdup(val, sizeof(*val), GFP_KERNEL); 179915d7e5eSDavid Lebrun 180915d7e5eSDavid Lebrun mutex_lock(&sdata->lock); 181915d7e5eSDavid Lebrun 182915d7e5eSDavid Lebrun t_old = sdata->tun_src; 183915d7e5eSDavid Lebrun rcu_assign_pointer(sdata->tun_src, t_new); 184915d7e5eSDavid Lebrun 185915d7e5eSDavid Lebrun mutex_unlock(&sdata->lock); 186915d7e5eSDavid Lebrun 187915d7e5eSDavid Lebrun synchronize_net(); 188915d7e5eSDavid Lebrun kfree(t_old); 189915d7e5eSDavid Lebrun 190915d7e5eSDavid Lebrun return 0; 191915d7e5eSDavid Lebrun } 192915d7e5eSDavid Lebrun 193915d7e5eSDavid Lebrun static int seg6_genl_get_tunsrc(struct sk_buff *skb, struct genl_info *info) 194915d7e5eSDavid Lebrun { 195915d7e5eSDavid Lebrun struct net *net = genl_info_net(info); 196915d7e5eSDavid Lebrun struct in6_addr *tun_src; 197915d7e5eSDavid Lebrun struct sk_buff *msg; 198915d7e5eSDavid Lebrun void *hdr; 199915d7e5eSDavid Lebrun 200915d7e5eSDavid Lebrun msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 201915d7e5eSDavid Lebrun if (!msg) 202915d7e5eSDavid Lebrun return -ENOMEM; 203915d7e5eSDavid Lebrun 204915d7e5eSDavid Lebrun hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, 205915d7e5eSDavid Lebrun &seg6_genl_family, 0, SEG6_CMD_GET_TUNSRC); 206915d7e5eSDavid Lebrun if (!hdr) 207915d7e5eSDavid Lebrun goto free_msg; 208915d7e5eSDavid Lebrun 209915d7e5eSDavid Lebrun rcu_read_lock(); 210915d7e5eSDavid Lebrun tun_src = rcu_dereference(seg6_pernet(net)->tun_src); 211915d7e5eSDavid Lebrun 212915d7e5eSDavid Lebrun if (nla_put(msg, SEG6_ATTR_DST, sizeof(struct in6_addr), tun_src)) 213915d7e5eSDavid Lebrun goto nla_put_failure; 214915d7e5eSDavid Lebrun 215915d7e5eSDavid Lebrun rcu_read_unlock(); 216915d7e5eSDavid Lebrun 217915d7e5eSDavid Lebrun genlmsg_end(msg, hdr); 218915d7e5eSDavid Lebrun genlmsg_reply(msg, info); 219915d7e5eSDavid Lebrun 220915d7e5eSDavid Lebrun return 0; 221915d7e5eSDavid Lebrun 222915d7e5eSDavid Lebrun nla_put_failure: 223915d7e5eSDavid Lebrun rcu_read_unlock(); 224915d7e5eSDavid Lebrun genlmsg_cancel(msg, hdr); 225915d7e5eSDavid Lebrun free_msg: 226915d7e5eSDavid Lebrun nlmsg_free(msg); 227915d7e5eSDavid Lebrun return -ENOMEM; 228915d7e5eSDavid Lebrun } 229915d7e5eSDavid Lebrun 230*4f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 231*4f4853dcSDavid Lebrun 232*4f4853dcSDavid Lebrun static int __seg6_hmac_fill_info(struct seg6_hmac_info *hinfo, 233*4f4853dcSDavid Lebrun struct sk_buff *msg) 234*4f4853dcSDavid Lebrun { 235*4f4853dcSDavid Lebrun if (nla_put_u32(msg, SEG6_ATTR_HMACKEYID, hinfo->hmackeyid) || 236*4f4853dcSDavid Lebrun nla_put_u8(msg, SEG6_ATTR_SECRETLEN, hinfo->slen) || 237*4f4853dcSDavid Lebrun nla_put(msg, SEG6_ATTR_SECRET, hinfo->slen, hinfo->secret) || 238*4f4853dcSDavid Lebrun nla_put_u8(msg, SEG6_ATTR_ALGID, hinfo->alg_id)) 239*4f4853dcSDavid Lebrun return -1; 240*4f4853dcSDavid Lebrun 241*4f4853dcSDavid Lebrun return 0; 242*4f4853dcSDavid Lebrun } 243*4f4853dcSDavid Lebrun 244*4f4853dcSDavid Lebrun static int __seg6_genl_dumphmac_element(struct seg6_hmac_info *hinfo, 245*4f4853dcSDavid Lebrun u32 portid, u32 seq, u32 flags, 246*4f4853dcSDavid Lebrun struct sk_buff *skb, u8 cmd) 247*4f4853dcSDavid Lebrun { 248*4f4853dcSDavid Lebrun void *hdr; 249*4f4853dcSDavid Lebrun 250*4f4853dcSDavid Lebrun hdr = genlmsg_put(skb, portid, seq, &seg6_genl_family, flags, cmd); 251*4f4853dcSDavid Lebrun if (!hdr) 252*4f4853dcSDavid Lebrun return -ENOMEM; 253*4f4853dcSDavid Lebrun 254*4f4853dcSDavid Lebrun if (__seg6_hmac_fill_info(hinfo, skb) < 0) 255*4f4853dcSDavid Lebrun goto nla_put_failure; 256*4f4853dcSDavid Lebrun 257*4f4853dcSDavid Lebrun genlmsg_end(skb, hdr); 258*4f4853dcSDavid Lebrun return 0; 259*4f4853dcSDavid Lebrun 260*4f4853dcSDavid Lebrun nla_put_failure: 261*4f4853dcSDavid Lebrun genlmsg_cancel(skb, hdr); 262*4f4853dcSDavid Lebrun return -EMSGSIZE; 263*4f4853dcSDavid Lebrun } 264*4f4853dcSDavid Lebrun 265*4f4853dcSDavid Lebrun static int seg6_genl_dumphmac_start(struct netlink_callback *cb) 266*4f4853dcSDavid Lebrun { 267*4f4853dcSDavid Lebrun struct net *net = sock_net(cb->skb->sk); 268*4f4853dcSDavid Lebrun struct seg6_pernet_data *sdata; 269*4f4853dcSDavid Lebrun struct rhashtable_iter *iter; 270*4f4853dcSDavid Lebrun 271*4f4853dcSDavid Lebrun sdata = seg6_pernet(net); 272*4f4853dcSDavid Lebrun iter = (struct rhashtable_iter *)cb->args[0]; 273*4f4853dcSDavid Lebrun 274*4f4853dcSDavid Lebrun if (!iter) { 275*4f4853dcSDavid Lebrun iter = kmalloc(sizeof(*iter), GFP_KERNEL); 276*4f4853dcSDavid Lebrun if (!iter) 277*4f4853dcSDavid Lebrun return -ENOMEM; 278*4f4853dcSDavid Lebrun 279*4f4853dcSDavid Lebrun cb->args[0] = (long)iter; 280*4f4853dcSDavid Lebrun } 281*4f4853dcSDavid Lebrun 282*4f4853dcSDavid Lebrun rhashtable_walk_enter(&sdata->hmac_infos, iter); 283*4f4853dcSDavid Lebrun 284*4f4853dcSDavid Lebrun return 0; 285*4f4853dcSDavid Lebrun } 286*4f4853dcSDavid Lebrun 287*4f4853dcSDavid Lebrun static int seg6_genl_dumphmac_done(struct netlink_callback *cb) 288*4f4853dcSDavid Lebrun { 289*4f4853dcSDavid Lebrun struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0]; 290*4f4853dcSDavid Lebrun 291*4f4853dcSDavid Lebrun rhashtable_walk_exit(iter); 292*4f4853dcSDavid Lebrun 293*4f4853dcSDavid Lebrun kfree(iter); 294*4f4853dcSDavid Lebrun 295*4f4853dcSDavid Lebrun return 0; 296*4f4853dcSDavid Lebrun } 297*4f4853dcSDavid Lebrun 298*4f4853dcSDavid Lebrun static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb) 299*4f4853dcSDavid Lebrun { 300*4f4853dcSDavid Lebrun struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0]; 301*4f4853dcSDavid Lebrun struct net *net = sock_net(skb->sk); 302*4f4853dcSDavid Lebrun struct seg6_pernet_data *sdata; 303*4f4853dcSDavid Lebrun struct seg6_hmac_info *hinfo; 304*4f4853dcSDavid Lebrun int ret; 305*4f4853dcSDavid Lebrun 306*4f4853dcSDavid Lebrun sdata = seg6_pernet(net); 307*4f4853dcSDavid Lebrun 308*4f4853dcSDavid Lebrun ret = rhashtable_walk_start(iter); 309*4f4853dcSDavid Lebrun if (ret && ret != -EAGAIN) 310*4f4853dcSDavid Lebrun goto done; 311*4f4853dcSDavid Lebrun 312*4f4853dcSDavid Lebrun for (;;) { 313*4f4853dcSDavid Lebrun hinfo = rhashtable_walk_next(iter); 314*4f4853dcSDavid Lebrun 315*4f4853dcSDavid Lebrun if (IS_ERR(hinfo)) { 316*4f4853dcSDavid Lebrun if (PTR_ERR(hinfo) == -EAGAIN) 317*4f4853dcSDavid Lebrun continue; 318*4f4853dcSDavid Lebrun ret = PTR_ERR(hinfo); 319*4f4853dcSDavid Lebrun goto done; 320*4f4853dcSDavid Lebrun } else if (!hinfo) { 321*4f4853dcSDavid Lebrun break; 322*4f4853dcSDavid Lebrun } 323*4f4853dcSDavid Lebrun 324*4f4853dcSDavid Lebrun ret = __seg6_genl_dumphmac_element(hinfo, 325*4f4853dcSDavid Lebrun NETLINK_CB(cb->skb).portid, 326*4f4853dcSDavid Lebrun cb->nlh->nlmsg_seq, 327*4f4853dcSDavid Lebrun NLM_F_MULTI, 328*4f4853dcSDavid Lebrun skb, SEG6_CMD_DUMPHMAC); 329*4f4853dcSDavid Lebrun if (ret) 330*4f4853dcSDavid Lebrun goto done; 331*4f4853dcSDavid Lebrun } 332*4f4853dcSDavid Lebrun 333*4f4853dcSDavid Lebrun ret = skb->len; 334*4f4853dcSDavid Lebrun 335*4f4853dcSDavid Lebrun done: 336*4f4853dcSDavid Lebrun rhashtable_walk_stop(iter); 337*4f4853dcSDavid Lebrun return ret; 338*4f4853dcSDavid Lebrun } 339*4f4853dcSDavid Lebrun 340*4f4853dcSDavid Lebrun #else 341*4f4853dcSDavid Lebrun 342*4f4853dcSDavid Lebrun static int seg6_genl_dumphmac_start(struct netlink_callback *cb) 343*4f4853dcSDavid Lebrun { 344*4f4853dcSDavid Lebrun return 0; 345*4f4853dcSDavid Lebrun } 346*4f4853dcSDavid Lebrun 347*4f4853dcSDavid Lebrun static int seg6_genl_dumphmac_done(struct netlink_callback *cb) 348*4f4853dcSDavid Lebrun { 349*4f4853dcSDavid Lebrun return 0; 350*4f4853dcSDavid Lebrun } 351*4f4853dcSDavid Lebrun 352915d7e5eSDavid Lebrun static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb) 353915d7e5eSDavid Lebrun { 354915d7e5eSDavid Lebrun return -ENOTSUPP; 355915d7e5eSDavid Lebrun } 356915d7e5eSDavid Lebrun 357*4f4853dcSDavid Lebrun #endif 358*4f4853dcSDavid Lebrun 359915d7e5eSDavid Lebrun static int __net_init seg6_net_init(struct net *net) 360915d7e5eSDavid Lebrun { 361915d7e5eSDavid Lebrun struct seg6_pernet_data *sdata; 362915d7e5eSDavid Lebrun 363915d7e5eSDavid Lebrun sdata = kzalloc(sizeof(*sdata), GFP_KERNEL); 364915d7e5eSDavid Lebrun if (!sdata) 365915d7e5eSDavid Lebrun return -ENOMEM; 366915d7e5eSDavid Lebrun 367915d7e5eSDavid Lebrun mutex_init(&sdata->lock); 368915d7e5eSDavid Lebrun 369915d7e5eSDavid Lebrun sdata->tun_src = kzalloc(sizeof(*sdata->tun_src), GFP_KERNEL); 370915d7e5eSDavid Lebrun if (!sdata->tun_src) { 371915d7e5eSDavid Lebrun kfree(sdata); 372915d7e5eSDavid Lebrun return -ENOMEM; 373915d7e5eSDavid Lebrun } 374915d7e5eSDavid Lebrun 375915d7e5eSDavid Lebrun net->ipv6.seg6_data = sdata; 376915d7e5eSDavid Lebrun 377*4f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 378*4f4853dcSDavid Lebrun seg6_hmac_net_init(net); 379*4f4853dcSDavid Lebrun #endif 380*4f4853dcSDavid Lebrun 381915d7e5eSDavid Lebrun return 0; 382915d7e5eSDavid Lebrun } 383915d7e5eSDavid Lebrun 384915d7e5eSDavid Lebrun static void __net_exit seg6_net_exit(struct net *net) 385915d7e5eSDavid Lebrun { 386915d7e5eSDavid Lebrun struct seg6_pernet_data *sdata = seg6_pernet(net); 387915d7e5eSDavid Lebrun 388*4f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 389*4f4853dcSDavid Lebrun seg6_hmac_net_exit(net); 390*4f4853dcSDavid Lebrun #endif 391*4f4853dcSDavid Lebrun 392915d7e5eSDavid Lebrun kfree(sdata->tun_src); 393915d7e5eSDavid Lebrun kfree(sdata); 394915d7e5eSDavid Lebrun } 395915d7e5eSDavid Lebrun 396915d7e5eSDavid Lebrun static struct pernet_operations ip6_segments_ops = { 397915d7e5eSDavid Lebrun .init = seg6_net_init, 398915d7e5eSDavid Lebrun .exit = seg6_net_exit, 399915d7e5eSDavid Lebrun }; 400915d7e5eSDavid Lebrun 401915d7e5eSDavid Lebrun static const struct genl_ops seg6_genl_ops[] = { 402915d7e5eSDavid Lebrun { 403915d7e5eSDavid Lebrun .cmd = SEG6_CMD_SETHMAC, 404915d7e5eSDavid Lebrun .doit = seg6_genl_sethmac, 405915d7e5eSDavid Lebrun .policy = seg6_genl_policy, 406915d7e5eSDavid Lebrun .flags = GENL_ADMIN_PERM, 407915d7e5eSDavid Lebrun }, 408915d7e5eSDavid Lebrun { 409915d7e5eSDavid Lebrun .cmd = SEG6_CMD_DUMPHMAC, 410*4f4853dcSDavid Lebrun .start = seg6_genl_dumphmac_start, 411915d7e5eSDavid Lebrun .dumpit = seg6_genl_dumphmac, 412*4f4853dcSDavid Lebrun .done = seg6_genl_dumphmac_done, 413915d7e5eSDavid Lebrun .policy = seg6_genl_policy, 414915d7e5eSDavid Lebrun .flags = GENL_ADMIN_PERM, 415915d7e5eSDavid Lebrun }, 416915d7e5eSDavid Lebrun { 417915d7e5eSDavid Lebrun .cmd = SEG6_CMD_SET_TUNSRC, 418915d7e5eSDavid Lebrun .doit = seg6_genl_set_tunsrc, 419915d7e5eSDavid Lebrun .policy = seg6_genl_policy, 420915d7e5eSDavid Lebrun .flags = GENL_ADMIN_PERM, 421915d7e5eSDavid Lebrun }, 422915d7e5eSDavid Lebrun { 423915d7e5eSDavid Lebrun .cmd = SEG6_CMD_GET_TUNSRC, 424915d7e5eSDavid Lebrun .doit = seg6_genl_get_tunsrc, 425915d7e5eSDavid Lebrun .policy = seg6_genl_policy, 426915d7e5eSDavid Lebrun .flags = GENL_ADMIN_PERM, 427915d7e5eSDavid Lebrun }, 428915d7e5eSDavid Lebrun }; 429915d7e5eSDavid Lebrun 430915d7e5eSDavid Lebrun static struct genl_family seg6_genl_family __ro_after_init = { 431915d7e5eSDavid Lebrun .hdrsize = 0, 432915d7e5eSDavid Lebrun .name = SEG6_GENL_NAME, 433915d7e5eSDavid Lebrun .version = SEG6_GENL_VERSION, 434915d7e5eSDavid Lebrun .maxattr = SEG6_ATTR_MAX, 435915d7e5eSDavid Lebrun .netnsok = true, 436915d7e5eSDavid Lebrun .parallel_ops = true, 437915d7e5eSDavid Lebrun .ops = seg6_genl_ops, 438915d7e5eSDavid Lebrun .n_ops = ARRAY_SIZE(seg6_genl_ops), 439915d7e5eSDavid Lebrun .module = THIS_MODULE, 440915d7e5eSDavid Lebrun }; 441915d7e5eSDavid Lebrun 442915d7e5eSDavid Lebrun int __init seg6_init(void) 443915d7e5eSDavid Lebrun { 444915d7e5eSDavid Lebrun int err = -ENOMEM; 445915d7e5eSDavid Lebrun 446915d7e5eSDavid Lebrun err = genl_register_family(&seg6_genl_family); 447915d7e5eSDavid Lebrun if (err) 448915d7e5eSDavid Lebrun goto out; 449915d7e5eSDavid Lebrun 450915d7e5eSDavid Lebrun err = register_pernet_subsys(&ip6_segments_ops); 451915d7e5eSDavid Lebrun if (err) 452915d7e5eSDavid Lebrun goto out_unregister_genl; 453915d7e5eSDavid Lebrun 4546c8702c6SDavid Lebrun err = seg6_iptunnel_init(); 4556c8702c6SDavid Lebrun if (err) 4566c8702c6SDavid Lebrun goto out_unregister_pernet; 4576c8702c6SDavid Lebrun 458*4f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 459*4f4853dcSDavid Lebrun err = seg6_hmac_init(); 460*4f4853dcSDavid Lebrun if (err) 461*4f4853dcSDavid Lebrun goto out_unregister_iptun; 462*4f4853dcSDavid Lebrun #endif 463*4f4853dcSDavid Lebrun 464915d7e5eSDavid Lebrun pr_info("Segment Routing with IPv6\n"); 465915d7e5eSDavid Lebrun 466915d7e5eSDavid Lebrun out: 467915d7e5eSDavid Lebrun return err; 468*4f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 469*4f4853dcSDavid Lebrun out_unregister_iptun: 470*4f4853dcSDavid Lebrun seg6_iptunnel_exit(); 471*4f4853dcSDavid Lebrun #endif 4726c8702c6SDavid Lebrun out_unregister_pernet: 4736c8702c6SDavid Lebrun unregister_pernet_subsys(&ip6_segments_ops); 474915d7e5eSDavid Lebrun out_unregister_genl: 475915d7e5eSDavid Lebrun genl_unregister_family(&seg6_genl_family); 476915d7e5eSDavid Lebrun goto out; 477915d7e5eSDavid Lebrun } 478915d7e5eSDavid Lebrun 479915d7e5eSDavid Lebrun void seg6_exit(void) 480915d7e5eSDavid Lebrun { 481*4f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC 482*4f4853dcSDavid Lebrun seg6_hmac_exit(); 483*4f4853dcSDavid Lebrun #endif 4846c8702c6SDavid Lebrun seg6_iptunnel_exit(); 485915d7e5eSDavid Lebrun unregister_pernet_subsys(&ip6_segments_ops); 486915d7e5eSDavid Lebrun genl_unregister_family(&seg6_genl_family); 487915d7e5eSDavid Lebrun } 488