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 inline void genl_lock(void)
27 mutex_lock(&genl_mutex
);
30 static inline void genl_unlock(void)
32 mutex_unlock(&genl_mutex
);
35 #define GENL_FAM_TAB_SIZE 16
36 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
38 static struct list_head family_ht
[GENL_FAM_TAB_SIZE
];
40 * Bitmap of multicast groups that are currently in use.
42 * To avoid an allocation at boot of just one unsigned long,
43 * declare it global instead.
44 * Bit 0 is marked as already used since group 0 is invalid.
46 static unsigned long mc_group_start
= 0x1;
47 static unsigned long *mc_groups
= &mc_group_start
;
48 static unsigned long mc_groups_longs
= 1;
50 static int genl_ctrl_event(int event
, void *data
);
52 static inline unsigned int genl_family_hash(unsigned int id
)
54 return id
& GENL_FAM_TAB_MASK
;
57 static inline struct list_head
*genl_family_chain(unsigned int id
)
59 return &family_ht
[genl_family_hash(id
)];
62 static struct genl_family
*genl_family_find_byid(unsigned int id
)
64 struct genl_family
*f
;
66 list_for_each_entry(f
, genl_family_chain(id
), family_list
)
73 static struct genl_family
*genl_family_find_byname(char *name
)
75 struct genl_family
*f
;
78 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
79 list_for_each_entry(f
, genl_family_chain(i
), family_list
)
80 if (strcmp(f
->name
, name
) == 0)
86 static struct genl_ops
*genl_get_cmd(u8 cmd
, struct genl_family
*family
)
90 list_for_each_entry(ops
, &family
->ops_list
, ops_list
)
97 /* Of course we are going to have problems once we hit
98 * 2^16 alive types, but that can only happen by year 2K
100 static inline u16
genl_generate_id(void)
102 static u16 id_gen_idx
;
107 id_gen_idx
= GENL_MIN_ID
;
109 if (++id_gen_idx
> GENL_MAX_ID
) {
118 } while (genl_family_find_byid(id_gen_idx
));
123 static struct genl_multicast_group notify_grp
;
126 * genl_register_mc_group - register a multicast group
128 * Registers the specified multicast group and notifies userspace
129 * about the new group.
131 * Returns 0 on success or a negative error code.
133 * @family: The generic netlink family the group shall be registered for.
134 * @grp: The group to register, must have a name.
136 int genl_register_mc_group(struct genl_family
*family
,
137 struct genl_multicast_group
*grp
)
140 unsigned long *new_groups
;
143 BUG_ON(grp
->name
[0] == '\0');
147 /* special-case our own group */
148 if (grp
== ¬ify_grp
)
151 id
= find_first_zero_bit(mc_groups
,
152 mc_groups_longs
* BITS_PER_LONG
);
155 if (id
>= mc_groups_longs
* BITS_PER_LONG
) {
156 size_t nlen
= (mc_groups_longs
+ 1) * sizeof(unsigned long);
158 if (mc_groups
== &mc_group_start
) {
159 new_groups
= kzalloc(nlen
, GFP_KERNEL
);
164 mc_groups
= new_groups
;
165 *mc_groups
= mc_group_start
;
167 new_groups
= krealloc(mc_groups
, nlen
, GFP_KERNEL
);
172 mc_groups
= new_groups
;
173 mc_groups
[mc_groups_longs
] = 0;
178 err
= netlink_change_ngroups(genl_sock
,
179 mc_groups_longs
* BITS_PER_LONG
);
184 set_bit(id
, mc_groups
);
185 list_add_tail(&grp
->list
, &family
->mcast_groups
);
186 grp
->family
= family
;
188 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP
, grp
);
193 EXPORT_SYMBOL(genl_register_mc_group
);
195 static void __genl_unregister_mc_group(struct genl_family
*family
,
196 struct genl_multicast_group
*grp
)
198 BUG_ON(grp
->family
!= family
);
199 netlink_clear_multicast_users(genl_sock
, grp
->id
);
200 clear_bit(grp
->id
, mc_groups
);
201 list_del(&grp
->list
);
202 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP
, grp
);
208 * genl_unregister_mc_group - unregister a multicast group
210 * Unregisters the specified multicast group and notifies userspace
211 * about it. All current listeners on the group are removed.
213 * Note: It is not necessary to unregister all multicast groups before
214 * unregistering the family, unregistering the family will cause
215 * all assigned multicast groups to be unregistered automatically.
217 * @family: Generic netlink family the group belongs to.
218 * @grp: The group to unregister, must have been registered successfully
221 void genl_unregister_mc_group(struct genl_family
*family
,
222 struct genl_multicast_group
*grp
)
225 __genl_unregister_mc_group(family
, grp
);
228 EXPORT_SYMBOL(genl_unregister_mc_group
);
230 static void genl_unregister_mc_groups(struct genl_family
*family
)
232 struct genl_multicast_group
*grp
, *tmp
;
234 list_for_each_entry_safe(grp
, tmp
, &family
->mcast_groups
, list
)
235 __genl_unregister_mc_group(family
, grp
);
239 * genl_register_ops - register generic netlink operations
240 * @family: generic netlink family
241 * @ops: operations to be registered
243 * Registers the specified operations and assigns them to the specified
244 * family. Either a doit or dumpit callback must be specified or the
245 * operation will fail. Only one operation structure per command
246 * identifier may be registered.
248 * See include/net/genetlink.h for more documenation on the operations
251 * Returns 0 on success or a negative error code.
253 int genl_register_ops(struct genl_family
*family
, struct genl_ops
*ops
)
257 if (ops
->dumpit
== NULL
&& ops
->doit
== NULL
)
260 if (genl_get_cmd(ops
->cmd
, family
)) {
266 ops
->flags
|= GENL_CMD_CAP_DUMP
;
268 ops
->flags
|= GENL_CMD_CAP_DO
;
270 ops
->flags
|= GENL_CMD_CAP_HASPOL
;
273 list_add_tail(&ops
->ops_list
, &family
->ops_list
);
276 genl_ctrl_event(CTRL_CMD_NEWOPS
, ops
);
283 * genl_unregister_ops - unregister generic netlink operations
284 * @family: generic netlink family
285 * @ops: operations to be unregistered
287 * Unregisters the specified operations and unassigns them from the
288 * specified family. The operation blocks until the current message
289 * processing has finished and doesn't start again until the
290 * unregister process has finished.
292 * Note: It is not necessary to unregister all operations before
293 * unregistering the family, unregistering the family will cause
294 * all assigned operations to be unregistered automatically.
296 * Returns 0 on success or a negative error code.
298 int genl_unregister_ops(struct genl_family
*family
, struct genl_ops
*ops
)
303 list_for_each_entry(rc
, &family
->ops_list
, ops_list
) {
305 list_del(&ops
->ops_list
);
307 genl_ctrl_event(CTRL_CMD_DELOPS
, ops
);
317 * genl_register_family - register a generic netlink family
318 * @family: generic netlink family
320 * Registers the specified family after validating it first. Only one
321 * family may be registered with the same family name or identifier.
322 * The family id may equal GENL_ID_GENERATE causing an unique id to
323 * be automatically generated and assigned.
325 * Return 0 on success or a negative error code.
327 int genl_register_family(struct genl_family
*family
)
331 if (family
->id
&& family
->id
< GENL_MIN_ID
)
334 if (family
->id
> GENL_MAX_ID
)
337 INIT_LIST_HEAD(&family
->ops_list
);
338 INIT_LIST_HEAD(&family
->mcast_groups
);
342 if (genl_family_find_byname(family
->name
)) {
347 if (genl_family_find_byid(family
->id
)) {
352 if (family
->id
== GENL_ID_GENERATE
) {
353 u16 newid
= genl_generate_id();
363 if (family
->maxattr
) {
364 family
->attrbuf
= kmalloc((family
->maxattr
+1) *
365 sizeof(struct nlattr
*), GFP_KERNEL
);
366 if (family
->attrbuf
== NULL
) {
371 family
->attrbuf
= NULL
;
373 list_add_tail(&family
->family_list
, genl_family_chain(family
->id
));
376 genl_ctrl_event(CTRL_CMD_NEWFAMILY
, family
);
387 * genl_register_family_with_ops - register a generic netlink family
388 * @family: generic netlink family
389 * @ops: operations to be registered
390 * @n_ops: number of elements to register
392 * Registers the specified family and operations from the specified table.
393 * Only one family may be registered with the same family name or identifier.
395 * The family id may equal GENL_ID_GENERATE causing an unique id to
396 * be automatically generated and assigned.
398 * Either a doit or dumpit callback must be specified for every registered
399 * operation or the function will fail. Only one operation structure per
400 * command identifier may be registered.
402 * See include/net/genetlink.h for more documenation on the operations
405 * This is equivalent to calling genl_register_family() followed by
406 * genl_register_ops() for every operation entry in the table taking
407 * care to unregister the family on error path.
409 * Return 0 on success or a negative error code.
411 int genl_register_family_with_ops(struct genl_family
*family
,
412 struct genl_ops
*ops
, size_t n_ops
)
416 err
= genl_register_family(family
);
420 for (i
= 0; i
< n_ops
; ++i
, ++ops
) {
421 err
= genl_register_ops(family
, ops
);
427 genl_unregister_family(family
);
430 EXPORT_SYMBOL(genl_register_family_with_ops
);
433 * genl_unregister_family - unregister generic netlink family
434 * @family: generic netlink family
436 * Unregisters the specified family.
438 * Returns 0 on success or a negative error code.
440 int genl_unregister_family(struct genl_family
*family
)
442 struct genl_family
*rc
;
446 genl_unregister_mc_groups(family
);
448 list_for_each_entry(rc
, genl_family_chain(family
->id
), family_list
) {
449 if (family
->id
!= rc
->id
|| strcmp(rc
->name
, family
->name
))
452 list_del(&rc
->family_list
);
453 INIT_LIST_HEAD(&family
->ops_list
);
456 kfree(family
->attrbuf
);
457 genl_ctrl_event(CTRL_CMD_DELFAMILY
, family
);
466 static int genl_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
468 struct genl_ops
*ops
;
469 struct genl_family
*family
;
470 struct genl_info info
;
471 struct genlmsghdr
*hdr
= nlmsg_data(nlh
);
474 family
= genl_family_find_byid(nlh
->nlmsg_type
);
478 hdrlen
= GENL_HDRLEN
+ family
->hdrsize
;
479 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
482 ops
= genl_get_cmd(hdr
->cmd
, family
);
486 if ((ops
->flags
& GENL_ADMIN_PERM
) &&
487 security_netlink_recv(skb
, CAP_NET_ADMIN
))
490 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
491 if (ops
->dumpit
== NULL
)
495 err
= netlink_dump_start(genl_sock
, skb
, nlh
,
496 ops
->dumpit
, ops
->done
);
501 if (ops
->doit
== NULL
)
504 if (family
->attrbuf
) {
505 err
= nlmsg_parse(nlh
, hdrlen
, family
->attrbuf
, family
->maxattr
,
511 info
.snd_seq
= nlh
->nlmsg_seq
;
512 info
.snd_pid
= NETLINK_CB(skb
).pid
;
514 info
.genlhdr
= nlmsg_data(nlh
);
515 info
.userhdr
= nlmsg_data(nlh
) + GENL_HDRLEN
;
516 info
.attrs
= family
->attrbuf
;
518 return ops
->doit(skb
, &info
);
521 static void genl_rcv(struct sk_buff
*skb
)
524 netlink_rcv_skb(skb
, &genl_rcv_msg
);
528 /**************************************************************************
530 **************************************************************************/
532 static struct genl_family genl_ctrl
= {
536 .maxattr
= CTRL_ATTR_MAX
,
539 static int ctrl_fill_info(struct genl_family
*family
, u32 pid
, u32 seq
,
540 u32 flags
, struct sk_buff
*skb
, u8 cmd
)
544 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
548 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, family
->name
);
549 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, family
->id
);
550 NLA_PUT_U32(skb
, CTRL_ATTR_VERSION
, family
->version
);
551 NLA_PUT_U32(skb
, CTRL_ATTR_HDRSIZE
, family
->hdrsize
);
552 NLA_PUT_U32(skb
, CTRL_ATTR_MAXATTR
, family
->maxattr
);
554 if (!list_empty(&family
->ops_list
)) {
555 struct nlattr
*nla_ops
;
556 struct genl_ops
*ops
;
559 nla_ops
= nla_nest_start(skb
, CTRL_ATTR_OPS
);
561 goto nla_put_failure
;
563 list_for_each_entry(ops
, &family
->ops_list
, ops_list
) {
566 nest
= nla_nest_start(skb
, idx
++);
568 goto nla_put_failure
;
570 NLA_PUT_U32(skb
, CTRL_ATTR_OP_ID
, ops
->cmd
);
571 NLA_PUT_U32(skb
, CTRL_ATTR_OP_FLAGS
, ops
->flags
);
573 nla_nest_end(skb
, nest
);
576 nla_nest_end(skb
, nla_ops
);
579 if (!list_empty(&family
->mcast_groups
)) {
580 struct genl_multicast_group
*grp
;
581 struct nlattr
*nla_grps
;
584 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
585 if (nla_grps
== NULL
)
586 goto nla_put_failure
;
588 list_for_each_entry(grp
, &family
->mcast_groups
, list
) {
591 nest
= nla_nest_start(skb
, idx
++);
593 goto nla_put_failure
;
595 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
596 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
599 nla_nest_end(skb
, nest
);
601 nla_nest_end(skb
, nla_grps
);
604 return genlmsg_end(skb
, hdr
);
607 genlmsg_cancel(skb
, hdr
);
611 static int ctrl_fill_mcgrp_info(struct genl_multicast_group
*grp
, u32 pid
,
612 u32 seq
, u32 flags
, struct sk_buff
*skb
,
616 struct nlattr
*nla_grps
;
619 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
623 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, grp
->family
->name
);
624 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, grp
->family
->id
);
626 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
627 if (nla_grps
== NULL
)
628 goto nla_put_failure
;
630 nest
= nla_nest_start(skb
, 1);
632 goto nla_put_failure
;
634 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
635 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
638 nla_nest_end(skb
, nest
);
639 nla_nest_end(skb
, nla_grps
);
641 return genlmsg_end(skb
, hdr
);
644 genlmsg_cancel(skb
, hdr
);
648 static int ctrl_dumpfamily(struct sk_buff
*skb
, struct netlink_callback
*cb
)
652 struct genl_family
*rt
;
653 int chains_to_skip
= cb
->args
[0];
654 int fams_to_skip
= cb
->args
[1];
656 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++) {
657 if (i
< chains_to_skip
)
660 list_for_each_entry(rt
, genl_family_chain(i
), family_list
) {
661 if (++n
< fams_to_skip
)
663 if (ctrl_fill_info(rt
, NETLINK_CB(cb
->skb
).pid
,
664 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
665 skb
, CTRL_CMD_NEWFAMILY
) < 0)
679 static struct sk_buff
*ctrl_build_family_msg(struct genl_family
*family
,
680 u32 pid
, int seq
, u8 cmd
)
685 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
687 return ERR_PTR(-ENOBUFS
);
689 err
= ctrl_fill_info(family
, pid
, seq
, 0, skb
, cmd
);
698 static struct sk_buff
*ctrl_build_mcgrp_msg(struct genl_multicast_group
*grp
,
699 u32 pid
, int seq
, u8 cmd
)
704 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
706 return ERR_PTR(-ENOBUFS
);
708 err
= ctrl_fill_mcgrp_info(grp
, pid
, seq
, 0, skb
, cmd
);
717 static const struct nla_policy ctrl_policy
[CTRL_ATTR_MAX
+1] = {
718 [CTRL_ATTR_FAMILY_ID
] = { .type
= NLA_U16
},
719 [CTRL_ATTR_FAMILY_NAME
] = { .type
= NLA_NUL_STRING
,
720 .len
= GENL_NAMSIZ
- 1 },
723 static int ctrl_getfamily(struct sk_buff
*skb
, struct genl_info
*info
)
726 struct genl_family
*res
= NULL
;
729 if (info
->attrs
[CTRL_ATTR_FAMILY_ID
]) {
730 u16 id
= nla_get_u16(info
->attrs
[CTRL_ATTR_FAMILY_ID
]);
731 res
= genl_family_find_byid(id
);
734 if (info
->attrs
[CTRL_ATTR_FAMILY_NAME
]) {
737 name
= nla_data(info
->attrs
[CTRL_ATTR_FAMILY_NAME
]);
738 res
= genl_family_find_byname(name
);
746 msg
= ctrl_build_family_msg(res
, info
->snd_pid
, info
->snd_seq
,
753 err
= genlmsg_reply(msg
, info
);
758 static int genl_ctrl_event(int event
, void *data
)
762 if (genl_sock
== NULL
)
766 case CTRL_CMD_NEWFAMILY
:
767 case CTRL_CMD_DELFAMILY
:
768 msg
= ctrl_build_family_msg(data
, 0, 0, event
);
772 genlmsg_multicast(msg
, 0, GENL_ID_CTRL
, GFP_KERNEL
);
774 case CTRL_CMD_NEWMCAST_GRP
:
775 case CTRL_CMD_DELMCAST_GRP
:
776 msg
= ctrl_build_mcgrp_msg(data
, 0, 0, event
);
780 genlmsg_multicast(msg
, 0, GENL_ID_CTRL
, GFP_KERNEL
);
787 static struct genl_ops genl_ctrl_ops
= {
788 .cmd
= CTRL_CMD_GETFAMILY
,
789 .doit
= ctrl_getfamily
,
790 .dumpit
= ctrl_dumpfamily
,
791 .policy
= ctrl_policy
,
794 static struct genl_multicast_group notify_grp
= {
798 static int __init
genl_init(void)
802 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
803 INIT_LIST_HEAD(&family_ht
[i
]);
805 err
= genl_register_family(&genl_ctrl
);
809 err
= genl_register_ops(&genl_ctrl
, &genl_ctrl_ops
);
811 goto errout_register
;
813 netlink_set_nonroot(NETLINK_GENERIC
, NL_NONROOT_RECV
);
815 /* we'll bump the group number right afterwards */
816 genl_sock
= netlink_kernel_create(&init_net
, NETLINK_GENERIC
, 0,
817 genl_rcv
, &genl_mutex
, THIS_MODULE
);
818 if (genl_sock
== NULL
)
819 panic("GENL: Cannot initialize generic netlink\n");
821 err
= genl_register_mc_group(&genl_ctrl
, ¬ify_grp
);
823 goto errout_register
;
828 genl_unregister_family(&genl_ctrl
);
830 panic("GENL: Cannot register controller: %d\n", err
);
833 subsys_initcall(genl_init
);
835 EXPORT_SYMBOL(genl_sock
);
836 EXPORT_SYMBOL(genl_register_ops
);
837 EXPORT_SYMBOL(genl_unregister_ops
);
838 EXPORT_SYMBOL(genl_register_family
);
839 EXPORT_SYMBOL(genl_unregister_family
);