2  * NETLINK      Generic Netlink Family
 
   4  *              Authors:        Jamal Hadi Salim
 
   5  *                              Thomas Graf <tgraf@suug.ch>
 
   8 #include <linux/config.h>
 
   9 #include <linux/module.h>
 
  10 #include <linux/kernel.h>
 
  11 #include <linux/errno.h>
 
  12 #include <linux/types.h>
 
  13 #include <linux/socket.h>
 
  14 #include <linux/string.h>
 
  15 #include <linux/skbuff.h>
 
  16 #include <linux/mutex.h>
 
  18 #include <net/genetlink.h>
 
  20 struct sock *genl_sock = NULL;
 
  22 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
 
  24 static void genl_lock(void)
 
  26         mutex_lock(&genl_mutex);
 
  29 static int genl_trylock(void)
 
  31         return !mutex_trylock(&genl_mutex);
 
  34 static void genl_unlock(void)
 
  36         mutex_unlock(&genl_mutex);
 
  38         if (genl_sock && genl_sock->sk_receive_queue.qlen)
 
  39                 genl_sock->sk_data_ready(genl_sock, 0);
 
  42 #define GENL_FAM_TAB_SIZE       16
 
  43 #define GENL_FAM_TAB_MASK       (GENL_FAM_TAB_SIZE - 1)
 
  45 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
 
  47 static int genl_ctrl_event(int event, void *data);
 
  49 static inline unsigned int genl_family_hash(unsigned int id)
 
  51         return id & GENL_FAM_TAB_MASK;
 
  54 static inline struct list_head *genl_family_chain(unsigned int id)
 
  56         return &family_ht[genl_family_hash(id)];
 
  59 static struct genl_family *genl_family_find_byid(unsigned int id)
 
  61         struct genl_family *f;
 
  63         list_for_each_entry(f, genl_family_chain(id), family_list)
 
  70 static struct genl_family *genl_family_find_byname(char *name)
 
  72         struct genl_family *f;
 
  75         for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
 
  76                 list_for_each_entry(f, genl_family_chain(i), family_list)
 
  77                         if (strcmp(f->name, name) == 0)
 
  83 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
 
  87         list_for_each_entry(ops, &family->ops_list, ops_list)
 
  94 /* Of course we are going to have problems once we hit
 
  95  * 2^16 alive types, but that can only happen by year 2K
 
  97 static inline u16 genl_generate_id(void)
 
  99         static u16 id_gen_idx;
 
 104                         id_gen_idx = GENL_MIN_ID;
 
 106                 if (++id_gen_idx > GENL_MAX_ID) {
 
 115         } while (genl_family_find_byid(id_gen_idx));
 
 121  * genl_register_ops - register generic netlink operations
 
 122  * @family: generic netlink family
 
 123  * @ops: operations to be registered
 
 125  * Registers the specified operations and assigns them to the specified
 
 126  * family. Either a doit or dumpit callback must be specified or the
 
 127  * operation will fail. Only one operation structure per command
 
 128  * identifier may be registered.
 
 130  * See include/net/genetlink.h for more documenation on the operations
 
 133  * Returns 0 on success or a negative error code.
 
 135 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
 
 139         if (ops->dumpit == NULL && ops->doit == NULL)
 
 142         if (genl_get_cmd(ops->cmd, family)) {
 
 148         list_add_tail(&ops->ops_list, &family->ops_list);
 
 151         genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
 
 158  * genl_unregister_ops - unregister generic netlink operations
 
 159  * @family: generic netlink family
 
 160  * @ops: operations to be unregistered
 
 162  * Unregisters the specified operations and unassigns them from the
 
 163  * specified family. The operation blocks until the current message
 
 164  * processing has finished and doesn't start again until the
 
 165  * unregister process has finished.
 
 167  * Note: It is not necessary to unregister all operations before
 
 168  *       unregistering the family, unregistering the family will cause
 
 169  *       all assigned operations to be unregistered automatically.
 
 171  * Returns 0 on success or a negative error code.
 
 173 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
 
 178         list_for_each_entry(rc, &family->ops_list, ops_list) {
 
 180                         list_del(&ops->ops_list);
 
 182                         genl_ctrl_event(CTRL_CMD_DELOPS, ops);
 
 192  * genl_register_family - register a generic netlink family
 
 193  * @family: generic netlink family
 
 195  * Registers the specified family after validating it first. Only one
 
 196  * family may be registered with the same family name or identifier.
 
 197  * The family id may equal GENL_ID_GENERATE causing an unique id to
 
 198  * be automatically generated and assigned.
 
 200  * Return 0 on success or a negative error code.
 
 202 int genl_register_family(struct genl_family *family)
 
 206         if (family->id && family->id < GENL_MIN_ID)
 
 209         if (family->id > GENL_MAX_ID)
 
 212         INIT_LIST_HEAD(&family->ops_list);
 
 216         if (genl_family_find_byname(family->name)) {
 
 221         if (genl_family_find_byid(family->id)) {
 
 226         if (family->id == GENL_ID_GENERATE) {
 
 227                 u16 newid = genl_generate_id();
 
 237         if (family->maxattr) {
 
 238                 family->attrbuf = kmalloc((family->maxattr+1) *
 
 239                                         sizeof(struct nlattr *), GFP_KERNEL);
 
 240                 if (family->attrbuf == NULL) {
 
 245                 family->attrbuf = NULL;
 
 247         list_add_tail(&family->family_list, genl_family_chain(family->id));
 
 250         genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
 
 261  * genl_unregister_family - unregister generic netlink family
 
 262  * @family: generic netlink family
 
 264  * Unregisters the specified family.
 
 266  * Returns 0 on success or a negative error code.
 
 268 int genl_unregister_family(struct genl_family *family)
 
 270         struct genl_family *rc;
 
 274         list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
 
 275                 if (family->id != rc->id || strcmp(rc->name, family->name))
 
 278                 list_del(&rc->family_list);
 
 279                 INIT_LIST_HEAD(&family->ops_list);
 
 282                 kfree(family->attrbuf);
 
 283                 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
 
 292 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 295         struct genl_ops *ops;
 
 296         struct genl_family *family;
 
 297         struct genl_info info;
 
 298         struct genlmsghdr *hdr = nlmsg_data(nlh);
 
 299         int hdrlen, err = -EINVAL;
 
 301         if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
 
 304         if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
 
 307         family = genl_family_find_byid(nlh->nlmsg_type);
 
 308         if (family == NULL) {
 
 313         hdrlen = GENL_HDRLEN + family->hdrsize;
 
 314         if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
 
 317         ops = genl_get_cmd(hdr->cmd, family);
 
 323         if ((ops->flags & GENL_ADMIN_PERM) && security_netlink_recv(skb)) {
 
 328         if (nlh->nlmsg_flags & NLM_F_DUMP) {
 
 329                 if (ops->dumpit == NULL) {
 
 334                 *errp = err = netlink_dump_start(genl_sock, skb, nlh,
 
 337                         skb_pull(skb, min(NLMSG_ALIGN(nlh->nlmsg_len),
 
 342         if (ops->doit == NULL) {
 
 347         if (family->attrbuf) {
 
 348                 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
 
 354         info.snd_seq = nlh->nlmsg_seq;
 
 355         info.snd_pid = NETLINK_CB(skb).pid;
 
 357         info.genlhdr = nlmsg_data(nlh);
 
 358         info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
 
 359         info.attrs = family->attrbuf;
 
 361         *errp = err = ops->doit(skb, &info);
 
 372 static void genl_rcv(struct sock *sk, int len)
 
 374         unsigned int qlen = 0;
 
 379                 netlink_run_queue(sk, &qlen, genl_rcv_msg);
 
 381         } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
 
 384 /**************************************************************************
 
 386  **************************************************************************/
 
 388 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
 
 389                           u32 flags, struct sk_buff *skb, u8 cmd)
 
 393         hdr = genlmsg_put(skb, pid, seq, GENL_ID_CTRL, 0, flags, cmd,
 
 398         NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
 
 399         NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
 
 401         return genlmsg_end(skb, hdr);
 
 404         return genlmsg_cancel(skb, hdr);
 
 407 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
 
 411         struct genl_family *rt;
 
 412         int chains_to_skip = cb->args[0];
 
 413         int fams_to_skip = cb->args[1];
 
 415         for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
 
 416                 if (i < chains_to_skip)
 
 419                 list_for_each_entry(rt, genl_family_chain(i), family_list) {
 
 420                         if (++n < fams_to_skip)
 
 422                         if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
 
 423                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
 
 424                                            skb, CTRL_CMD_NEWFAMILY) < 0)
 
 438 static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
 
 444         skb = nlmsg_new(NLMSG_GOODSIZE);
 
 446                 return ERR_PTR(-ENOBUFS);
 
 448         err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
 
 457 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = {
 
 458         [CTRL_ATTR_FAMILY_ID]   = { .type = NLA_U16 },
 
 459         [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING },
 
 462 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
 
 465         struct genl_family *res = NULL;
 
 468         if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
 
 469                 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
 
 470                 res = genl_family_find_byid(id);
 
 473         if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
 
 474                 char name[GENL_NAMSIZ];
 
 476                 if (nla_strlcpy(name, info->attrs[CTRL_ATTR_FAMILY_NAME],
 
 477                                 GENL_NAMSIZ) >= GENL_NAMSIZ)
 
 480                 res = genl_family_find_byname(name);
 
 488         msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
 
 495         err = genlmsg_unicast(msg, info->snd_pid);
 
 500 static int genl_ctrl_event(int event, void *data)
 
 504         if (genl_sock == NULL)
 
 508         case CTRL_CMD_NEWFAMILY:
 
 509         case CTRL_CMD_DELFAMILY:
 
 510                 msg = ctrl_build_msg(data, 0, 0, event);
 
 514                 genlmsg_multicast(msg, 0, GENL_ID_CTRL);
 
 521 static struct genl_ops genl_ctrl_ops = {
 
 522         .cmd            = CTRL_CMD_GETFAMILY,
 
 523         .doit           = ctrl_getfamily,
 
 524         .dumpit         = ctrl_dumpfamily,
 
 525         .policy         = ctrl_policy,
 
 528 static struct genl_family genl_ctrl = {
 
 532         .maxattr = CTRL_ATTR_MAX,
 
 535 static int __init genl_init(void)
 
 539         for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
 
 540                 INIT_LIST_HEAD(&family_ht[i]);
 
 542         err = genl_register_family(&genl_ctrl);
 
 546         err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
 
 548                 goto errout_register;
 
 550         netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
 
 551         genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
 
 552                                           genl_rcv, THIS_MODULE);
 
 553         if (genl_sock == NULL)
 
 554                 panic("GENL: Cannot initialize generic netlink\n");
 
 559         genl_unregister_family(&genl_ctrl);
 
 561         panic("GENL: Cannot register controller: %d\n", err);
 
 564 subsys_initcall(genl_init);
 
 566 EXPORT_SYMBOL(genl_sock);
 
 567 EXPORT_SYMBOL(genl_register_ops);
 
 568 EXPORT_SYMBOL(genl_unregister_ops);
 
 569 EXPORT_SYMBOL(genl_register_family);
 
 570 EXPORT_SYMBOL(genl_unregister_family);