2 * NETLINK Generic Netlink Family
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
6 * Johannes Berg <johannes@sipsolutions.net>
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>
17 #include <linux/bitmap.h>
19 #include <net/genetlink.h>
21 struct sock
*genl_sock
= NULL
;
23 static DEFINE_MUTEX(genl_mutex
); /* serialization of message processing */
25 static void genl_lock(void)
27 mutex_lock(&genl_mutex
);
30 static int genl_trylock(void)
32 return !mutex_trylock(&genl_mutex
);
35 static void genl_unlock(void)
37 mutex_unlock(&genl_mutex
);
39 if (genl_sock
&& genl_sock
->sk_receive_queue
.qlen
)
40 genl_sock
->sk_data_ready(genl_sock
, 0);
43 #define GENL_FAM_TAB_SIZE 16
44 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
46 static struct list_head family_ht
[GENL_FAM_TAB_SIZE
];
48 * Bitmap of multicast groups that are currently in use.
50 * To avoid an allocation at boot of just one unsigned long,
51 * declare it global instead.
52 * Bit 0 is marked as already used since group 0 is invalid.
54 static unsigned long mc_group_start
= 0x1;
55 static unsigned long *mc_groups
= &mc_group_start
;
56 static unsigned long mc_groups_longs
= 1;
58 static int genl_ctrl_event(int event
, void *data
);
60 static inline unsigned int genl_family_hash(unsigned int id
)
62 return id
& GENL_FAM_TAB_MASK
;
65 static inline struct list_head
*genl_family_chain(unsigned int id
)
67 return &family_ht
[genl_family_hash(id
)];
70 static struct genl_family
*genl_family_find_byid(unsigned int id
)
72 struct genl_family
*f
;
74 list_for_each_entry(f
, genl_family_chain(id
), family_list
)
81 static struct genl_family
*genl_family_find_byname(char *name
)
83 struct genl_family
*f
;
86 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
87 list_for_each_entry(f
, genl_family_chain(i
), family_list
)
88 if (strcmp(f
->name
, name
) == 0)
94 static struct genl_ops
*genl_get_cmd(u8 cmd
, struct genl_family
*family
)
98 list_for_each_entry(ops
, &family
->ops_list
, ops_list
)
105 /* Of course we are going to have problems once we hit
106 * 2^16 alive types, but that can only happen by year 2K
108 static inline u16
genl_generate_id(void)
110 static u16 id_gen_idx
;
115 id_gen_idx
= GENL_MIN_ID
;
117 if (++id_gen_idx
> GENL_MAX_ID
) {
126 } while (genl_family_find_byid(id_gen_idx
));
131 static struct genl_multicast_group notify_grp
;
134 * genl_register_mc_group - register a multicast group
136 * Registers the specified multicast group and notifies userspace
137 * about the new group.
139 * Returns 0 on success or a negative error code.
141 * @family: The generic netlink family the group shall be registered for.
142 * @grp: The group to register, must have a name.
144 int genl_register_mc_group(struct genl_family
*family
,
145 struct genl_multicast_group
*grp
)
148 unsigned long *new_groups
;
151 BUG_ON(grp
->name
[0] == '\0');
155 /* special-case our own group */
156 if (grp
== ¬ify_grp
)
159 id
= find_first_zero_bit(mc_groups
,
160 mc_groups_longs
* BITS_PER_LONG
);
163 if (id
>= mc_groups_longs
* BITS_PER_LONG
) {
164 size_t nlen
= (mc_groups_longs
+ 1) * sizeof(unsigned long);
166 if (mc_groups
== &mc_group_start
) {
167 new_groups
= kzalloc(nlen
, GFP_KERNEL
);
172 mc_groups
= new_groups
;
173 *mc_groups
= mc_group_start
;
175 new_groups
= krealloc(mc_groups
, nlen
, GFP_KERNEL
);
180 mc_groups
= new_groups
;
181 mc_groups
[mc_groups_longs
] = 0;
186 err
= netlink_change_ngroups(genl_sock
,
187 sizeof(unsigned long) * NETLINK_GENERIC
);
192 set_bit(id
, mc_groups
);
193 list_add_tail(&grp
->list
, &family
->mcast_groups
);
194 grp
->family
= family
;
196 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP
, grp
);
201 EXPORT_SYMBOL(genl_register_mc_group
);
204 * genl_unregister_mc_group - unregister a multicast group
206 * Unregisters the specified multicast group and notifies userspace
207 * about it. All current listeners on the group are removed.
209 * Note: It is not necessary to unregister all multicast groups before
210 * unregistering the family, unregistering the family will cause
211 * all assigned multicast groups to be unregistered automatically.
213 * @family: Generic netlink family the group belongs to.
214 * @grp: The group to unregister, must have been registered successfully
217 void genl_unregister_mc_group(struct genl_family
*family
,
218 struct genl_multicast_group
*grp
)
220 BUG_ON(grp
->family
!= family
);
222 netlink_clear_multicast_users(genl_sock
, grp
->id
);
223 clear_bit(grp
->id
, mc_groups
);
224 list_del(&grp
->list
);
225 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP
, grp
);
231 static void genl_unregister_mc_groups(struct genl_family
*family
)
233 struct genl_multicast_group
*grp
, *tmp
;
235 list_for_each_entry_safe(grp
, tmp
, &family
->mcast_groups
, list
)
236 genl_unregister_mc_group(family
, grp
);
240 * genl_register_ops - register generic netlink operations
241 * @family: generic netlink family
242 * @ops: operations to be registered
244 * Registers the specified operations and assigns them to the specified
245 * family. Either a doit or dumpit callback must be specified or the
246 * operation will fail. Only one operation structure per command
247 * identifier may be registered.
249 * See include/net/genetlink.h for more documenation on the operations
252 * Returns 0 on success or a negative error code.
254 int genl_register_ops(struct genl_family
*family
, struct genl_ops
*ops
)
258 if (ops
->dumpit
== NULL
&& ops
->doit
== NULL
)
261 if (genl_get_cmd(ops
->cmd
, family
)) {
267 ops
->flags
|= GENL_CMD_CAP_DUMP
;
269 ops
->flags
|= GENL_CMD_CAP_DO
;
271 ops
->flags
|= GENL_CMD_CAP_HASPOL
;
274 list_add_tail(&ops
->ops_list
, &family
->ops_list
);
277 genl_ctrl_event(CTRL_CMD_NEWOPS
, ops
);
284 * genl_unregister_ops - unregister generic netlink operations
285 * @family: generic netlink family
286 * @ops: operations to be unregistered
288 * Unregisters the specified operations and unassigns them from the
289 * specified family. The operation blocks until the current message
290 * processing has finished and doesn't start again until the
291 * unregister process has finished.
293 * Note: It is not necessary to unregister all operations before
294 * unregistering the family, unregistering the family will cause
295 * all assigned operations to be unregistered automatically.
297 * Returns 0 on success or a negative error code.
299 int genl_unregister_ops(struct genl_family
*family
, struct genl_ops
*ops
)
304 list_for_each_entry(rc
, &family
->ops_list
, ops_list
) {
306 list_del(&ops
->ops_list
);
308 genl_ctrl_event(CTRL_CMD_DELOPS
, ops
);
318 * genl_register_family - register a generic netlink family
319 * @family: generic netlink family
321 * Registers the specified family after validating it first. Only one
322 * family may be registered with the same family name or identifier.
323 * The family id may equal GENL_ID_GENERATE causing an unique id to
324 * be automatically generated and assigned.
326 * Return 0 on success or a negative error code.
328 int genl_register_family(struct genl_family
*family
)
332 if (family
->id
&& family
->id
< GENL_MIN_ID
)
335 if (family
->id
> GENL_MAX_ID
)
338 INIT_LIST_HEAD(&family
->ops_list
);
339 INIT_LIST_HEAD(&family
->mcast_groups
);
343 if (genl_family_find_byname(family
->name
)) {
348 if (genl_family_find_byid(family
->id
)) {
353 if (family
->id
== GENL_ID_GENERATE
) {
354 u16 newid
= genl_generate_id();
364 if (family
->maxattr
) {
365 family
->attrbuf
= kmalloc((family
->maxattr
+1) *
366 sizeof(struct nlattr
*), GFP_KERNEL
);
367 if (family
->attrbuf
== NULL
) {
372 family
->attrbuf
= NULL
;
374 list_add_tail(&family
->family_list
, genl_family_chain(family
->id
));
377 genl_ctrl_event(CTRL_CMD_NEWFAMILY
, family
);
388 * genl_unregister_family - unregister generic netlink family
389 * @family: generic netlink family
391 * Unregisters the specified family.
393 * Returns 0 on success or a negative error code.
395 int genl_unregister_family(struct genl_family
*family
)
397 struct genl_family
*rc
;
399 genl_unregister_mc_groups(family
);
403 list_for_each_entry(rc
, genl_family_chain(family
->id
), family_list
) {
404 if (family
->id
!= rc
->id
|| strcmp(rc
->name
, family
->name
))
407 list_del(&rc
->family_list
);
408 INIT_LIST_HEAD(&family
->ops_list
);
411 kfree(family
->attrbuf
);
412 genl_ctrl_event(CTRL_CMD_DELFAMILY
, family
);
421 static int genl_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
423 struct genl_ops
*ops
;
424 struct genl_family
*family
;
425 struct genl_info info
;
426 struct genlmsghdr
*hdr
= nlmsg_data(nlh
);
429 family
= genl_family_find_byid(nlh
->nlmsg_type
);
433 hdrlen
= GENL_HDRLEN
+ family
->hdrsize
;
434 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
437 ops
= genl_get_cmd(hdr
->cmd
, family
);
441 if ((ops
->flags
& GENL_ADMIN_PERM
) &&
442 security_netlink_recv(skb
, CAP_NET_ADMIN
))
445 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
446 if (ops
->dumpit
== NULL
)
449 return netlink_dump_start(genl_sock
, skb
, nlh
,
450 ops
->dumpit
, ops
->done
);
453 if (ops
->doit
== NULL
)
456 if (family
->attrbuf
) {
457 err
= nlmsg_parse(nlh
, hdrlen
, family
->attrbuf
, family
->maxattr
,
463 info
.snd_seq
= nlh
->nlmsg_seq
;
464 info
.snd_pid
= NETLINK_CB(skb
).pid
;
466 info
.genlhdr
= nlmsg_data(nlh
);
467 info
.userhdr
= nlmsg_data(nlh
) + GENL_HDRLEN
;
468 info
.attrs
= family
->attrbuf
;
470 return ops
->doit(skb
, &info
);
473 static void genl_rcv(struct sock
*sk
, int len
)
475 unsigned int qlen
= 0;
480 netlink_run_queue(sk
, &qlen
, genl_rcv_msg
);
482 } while (qlen
&& genl_sock
&& genl_sock
->sk_receive_queue
.qlen
);
485 /**************************************************************************
487 **************************************************************************/
489 static struct genl_family genl_ctrl
= {
493 .maxattr
= CTRL_ATTR_MAX
,
496 static int ctrl_fill_info(struct genl_family
*family
, u32 pid
, u32 seq
,
497 u32 flags
, struct sk_buff
*skb
, u8 cmd
)
501 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
505 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, family
->name
);
506 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, family
->id
);
507 NLA_PUT_U32(skb
, CTRL_ATTR_VERSION
, family
->version
);
508 NLA_PUT_U32(skb
, CTRL_ATTR_HDRSIZE
, family
->hdrsize
);
509 NLA_PUT_U32(skb
, CTRL_ATTR_MAXATTR
, family
->maxattr
);
511 if (!list_empty(&family
->ops_list
)) {
512 struct nlattr
*nla_ops
;
513 struct genl_ops
*ops
;
516 nla_ops
= nla_nest_start(skb
, CTRL_ATTR_OPS
);
518 goto nla_put_failure
;
520 list_for_each_entry(ops
, &family
->ops_list
, ops_list
) {
523 nest
= nla_nest_start(skb
, idx
++);
525 goto nla_put_failure
;
527 NLA_PUT_U32(skb
, CTRL_ATTR_OP_ID
, ops
->cmd
);
528 NLA_PUT_U32(skb
, CTRL_ATTR_OP_FLAGS
, ops
->flags
);
530 nla_nest_end(skb
, nest
);
533 nla_nest_end(skb
, nla_ops
);
536 if (!list_empty(&family
->mcast_groups
)) {
537 struct genl_multicast_group
*grp
;
538 struct nlattr
*nla_grps
;
541 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
542 if (nla_grps
== NULL
)
543 goto nla_put_failure
;
545 list_for_each_entry(grp
, &family
->mcast_groups
, list
) {
548 nest
= nla_nest_start(skb
, idx
++);
550 goto nla_put_failure
;
552 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
553 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
556 nla_nest_end(skb
, nest
);
558 nla_nest_end(skb
, nla_grps
);
561 return genlmsg_end(skb
, hdr
);
564 return genlmsg_cancel(skb
, hdr
);
567 static int ctrl_fill_mcgrp_info(struct genl_multicast_group
*grp
, u32 pid
,
568 u32 seq
, u32 flags
, struct sk_buff
*skb
,
572 struct nlattr
*nla_grps
;
575 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
579 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, grp
->family
->name
);
580 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, grp
->family
->id
);
582 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
583 if (nla_grps
== NULL
)
584 goto nla_put_failure
;
586 nest
= nla_nest_start(skb
, 1);
588 goto nla_put_failure
;
590 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
591 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
594 nla_nest_end(skb
, nest
);
595 nla_nest_end(skb
, nla_grps
);
597 return genlmsg_end(skb
, hdr
);
600 return genlmsg_cancel(skb
, hdr
);
603 static int ctrl_dumpfamily(struct sk_buff
*skb
, struct netlink_callback
*cb
)
607 struct genl_family
*rt
;
608 int chains_to_skip
= cb
->args
[0];
609 int fams_to_skip
= cb
->args
[1];
611 if (chains_to_skip
!= 0)
614 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++) {
615 if (i
< chains_to_skip
)
618 list_for_each_entry(rt
, genl_family_chain(i
), family_list
) {
619 if (++n
< fams_to_skip
)
621 if (ctrl_fill_info(rt
, NETLINK_CB(cb
->skb
).pid
,
622 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
623 skb
, CTRL_CMD_NEWFAMILY
) < 0)
631 if (chains_to_skip
!= 0)
640 static struct sk_buff
*ctrl_build_family_msg(struct genl_family
*family
,
641 u32 pid
, int seq
, u8 cmd
)
646 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
648 return ERR_PTR(-ENOBUFS
);
650 err
= ctrl_fill_info(family
, pid
, seq
, 0, skb
, cmd
);
659 static struct sk_buff
*ctrl_build_mcgrp_msg(struct genl_multicast_group
*grp
,
660 u32 pid
, int seq
, u8 cmd
)
665 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
667 return ERR_PTR(-ENOBUFS
);
669 err
= ctrl_fill_mcgrp_info(grp
, pid
, seq
, 0, skb
, cmd
);
678 static const struct nla_policy ctrl_policy
[CTRL_ATTR_MAX
+1] = {
679 [CTRL_ATTR_FAMILY_ID
] = { .type
= NLA_U16
},
680 [CTRL_ATTR_FAMILY_NAME
] = { .type
= NLA_NUL_STRING
,
681 .len
= GENL_NAMSIZ
- 1 },
684 static int ctrl_getfamily(struct sk_buff
*skb
, struct genl_info
*info
)
687 struct genl_family
*res
= NULL
;
690 if (info
->attrs
[CTRL_ATTR_FAMILY_ID
]) {
691 u16 id
= nla_get_u16(info
->attrs
[CTRL_ATTR_FAMILY_ID
]);
692 res
= genl_family_find_byid(id
);
695 if (info
->attrs
[CTRL_ATTR_FAMILY_NAME
]) {
698 name
= nla_data(info
->attrs
[CTRL_ATTR_FAMILY_NAME
]);
699 res
= genl_family_find_byname(name
);
707 msg
= ctrl_build_family_msg(res
, info
->snd_pid
, info
->snd_seq
,
714 err
= genlmsg_reply(msg
, info
);
719 static int genl_ctrl_event(int event
, void *data
)
723 if (genl_sock
== NULL
)
727 case CTRL_CMD_NEWFAMILY
:
728 case CTRL_CMD_DELFAMILY
:
729 msg
= ctrl_build_family_msg(data
, 0, 0, event
);
733 genlmsg_multicast(msg
, 0, GENL_ID_CTRL
, GFP_KERNEL
);
735 case CTRL_CMD_NEWMCAST_GRP
:
736 case CTRL_CMD_DELMCAST_GRP
:
737 msg
= ctrl_build_mcgrp_msg(data
, 0, 0, event
);
741 genlmsg_multicast(msg
, 0, GENL_ID_CTRL
, GFP_KERNEL
);
748 static struct genl_ops genl_ctrl_ops
= {
749 .cmd
= CTRL_CMD_GETFAMILY
,
750 .doit
= ctrl_getfamily
,
751 .dumpit
= ctrl_dumpfamily
,
752 .policy
= ctrl_policy
,
755 static struct genl_multicast_group notify_grp
= {
759 static int __init
genl_init(void)
763 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
764 INIT_LIST_HEAD(&family_ht
[i
]);
766 err
= genl_register_family(&genl_ctrl
);
770 err
= genl_register_ops(&genl_ctrl
, &genl_ctrl_ops
);
772 goto errout_register
;
774 netlink_set_nonroot(NETLINK_GENERIC
, NL_NONROOT_RECV
);
776 /* we'll bump the group number right afterwards */
777 genl_sock
= netlink_kernel_create(NETLINK_GENERIC
, 0, genl_rcv
,
779 if (genl_sock
== NULL
)
780 panic("GENL: Cannot initialize generic netlink\n");
782 err
= genl_register_mc_group(&genl_ctrl
, ¬ify_grp
);
784 goto errout_register
;
789 genl_unregister_family(&genl_ctrl
);
791 panic("GENL: Cannot register controller: %d\n", err
);
794 subsys_initcall(genl_init
);
796 EXPORT_SYMBOL(genl_sock
);
797 EXPORT_SYMBOL(genl_register_ops
);
798 EXPORT_SYMBOL(genl_unregister_ops
);
799 EXPORT_SYMBOL(genl_register_family
);
800 EXPORT_SYMBOL(genl_unregister_family
);