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/slab.h>
12 #include <linux/errno.h>
13 #include <linux/types.h>
14 #include <linux/socket.h>
15 #include <linux/string.h>
16 #include <linux/skbuff.h>
17 #include <linux/mutex.h>
18 #include <linux/bitmap.h>
20 #include <net/genetlink.h>
22 static DEFINE_MUTEX(genl_mutex
); /* serialization of message processing */
24 static inline void genl_lock(void)
26 mutex_lock(&genl_mutex
);
29 static inline void genl_unlock(void)
31 mutex_unlock(&genl_mutex
);
34 #define GENL_FAM_TAB_SIZE 16
35 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
37 static struct list_head family_ht
[GENL_FAM_TAB_SIZE
];
39 * Bitmap of multicast groups that are currently in use.
41 * To avoid an allocation at boot of just one unsigned long,
42 * declare it global instead.
43 * Bit 0 is marked as already used since group 0 is invalid.
45 static unsigned long mc_group_start
= 0x1;
46 static unsigned long *mc_groups
= &mc_group_start
;
47 static unsigned long mc_groups_longs
= 1;
49 static int genl_ctrl_event(int event
, void *data
);
51 static inline unsigned int genl_family_hash(unsigned int id
)
53 return id
& GENL_FAM_TAB_MASK
;
56 static inline struct list_head
*genl_family_chain(unsigned int id
)
58 return &family_ht
[genl_family_hash(id
)];
61 static struct genl_family
*genl_family_find_byid(unsigned int id
)
63 struct genl_family
*f
;
65 list_for_each_entry(f
, genl_family_chain(id
), family_list
)
72 static struct genl_family
*genl_family_find_byname(char *name
)
74 struct genl_family
*f
;
77 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
78 list_for_each_entry(f
, genl_family_chain(i
), family_list
)
79 if (strcmp(f
->name
, name
) == 0)
85 static struct genl_ops
*genl_get_cmd(u8 cmd
, struct genl_family
*family
)
89 list_for_each_entry(ops
, &family
->ops_list
, ops_list
)
96 /* Of course we are going to have problems once we hit
97 * 2^16 alive types, but that can only happen by year 2K
99 static inline u16
genl_generate_id(void)
101 static u16 id_gen_idx
= GENL_MIN_ID
;
104 for (i
= 0; i
<= GENL_MAX_ID
- GENL_MIN_ID
; i
++) {
105 if (!genl_family_find_byid(id_gen_idx
))
107 if (++id_gen_idx
> GENL_MAX_ID
)
108 id_gen_idx
= GENL_MIN_ID
;
114 static struct genl_multicast_group notify_grp
;
117 * genl_register_mc_group - register a multicast group
119 * Registers the specified multicast group and notifies userspace
120 * about the new group.
122 * Returns 0 on success or a negative error code.
124 * @family: The generic netlink family the group shall be registered for.
125 * @grp: The group to register, must have a name.
127 int genl_register_mc_group(struct genl_family
*family
,
128 struct genl_multicast_group
*grp
)
131 unsigned long *new_groups
;
134 BUG_ON(grp
->name
[0] == '\0');
138 /* special-case our own group */
139 if (grp
== ¬ify_grp
)
142 id
= find_first_zero_bit(mc_groups
,
143 mc_groups_longs
* BITS_PER_LONG
);
146 if (id
>= mc_groups_longs
* BITS_PER_LONG
) {
147 size_t nlen
= (mc_groups_longs
+ 1) * sizeof(unsigned long);
149 if (mc_groups
== &mc_group_start
) {
150 new_groups
= kzalloc(nlen
, GFP_KERNEL
);
155 mc_groups
= new_groups
;
156 *mc_groups
= mc_group_start
;
158 new_groups
= krealloc(mc_groups
, nlen
, GFP_KERNEL
);
163 mc_groups
= new_groups
;
164 mc_groups
[mc_groups_longs
] = 0;
169 if (family
->netnsok
) {
172 netlink_table_grab();
174 for_each_net_rcu(net
) {
175 err
= __netlink_change_ngroups(net
->genl_sock
,
176 mc_groups_longs
* BITS_PER_LONG
);
179 * No need to roll back, can only fail if
180 * memory allocation fails and then the
181 * number of _possible_ groups has been
182 * increased on some sockets which is ok.
185 netlink_table_ungrab();
190 netlink_table_ungrab();
192 err
= netlink_change_ngroups(init_net
.genl_sock
,
193 mc_groups_longs
* BITS_PER_LONG
);
199 set_bit(id
, mc_groups
);
200 list_add_tail(&grp
->list
, &family
->mcast_groups
);
201 grp
->family
= family
;
203 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP
, grp
);
208 EXPORT_SYMBOL(genl_register_mc_group
);
210 static void __genl_unregister_mc_group(struct genl_family
*family
,
211 struct genl_multicast_group
*grp
)
214 BUG_ON(grp
->family
!= family
);
216 netlink_table_grab();
218 for_each_net_rcu(net
)
219 __netlink_clear_multicast_users(net
->genl_sock
, grp
->id
);
221 netlink_table_ungrab();
223 clear_bit(grp
->id
, mc_groups
);
224 list_del(&grp
->list
);
225 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP
, grp
);
231 * genl_unregister_mc_group - unregister a multicast group
233 * Unregisters the specified multicast group and notifies userspace
234 * about it. All current listeners on the group are removed.
236 * Note: It is not necessary to unregister all multicast groups before
237 * unregistering the family, unregistering the family will cause
238 * all assigned multicast groups to be unregistered automatically.
240 * @family: Generic netlink family the group belongs to.
241 * @grp: The group to unregister, must have been registered successfully
244 void genl_unregister_mc_group(struct genl_family
*family
,
245 struct genl_multicast_group
*grp
)
248 __genl_unregister_mc_group(family
, grp
);
251 EXPORT_SYMBOL(genl_unregister_mc_group
);
253 static void genl_unregister_mc_groups(struct genl_family
*family
)
255 struct genl_multicast_group
*grp
, *tmp
;
257 list_for_each_entry_safe(grp
, tmp
, &family
->mcast_groups
, list
)
258 __genl_unregister_mc_group(family
, grp
);
262 * genl_register_ops - register generic netlink operations
263 * @family: generic netlink family
264 * @ops: operations to be registered
266 * Registers the specified operations and assigns them to the specified
267 * family. Either a doit or dumpit callback must be specified or the
268 * operation will fail. Only one operation structure per command
269 * identifier may be registered.
271 * See include/net/genetlink.h for more documenation on the operations
274 * Returns 0 on success or a negative error code.
276 int genl_register_ops(struct genl_family
*family
, struct genl_ops
*ops
)
280 if (ops
->dumpit
== NULL
&& ops
->doit
== NULL
)
283 if (genl_get_cmd(ops
->cmd
, family
)) {
289 ops
->flags
|= GENL_CMD_CAP_DUMP
;
291 ops
->flags
|= GENL_CMD_CAP_DO
;
293 ops
->flags
|= GENL_CMD_CAP_HASPOL
;
296 list_add_tail(&ops
->ops_list
, &family
->ops_list
);
299 genl_ctrl_event(CTRL_CMD_NEWOPS
, ops
);
306 * genl_unregister_ops - unregister generic netlink operations
307 * @family: generic netlink family
308 * @ops: operations to be unregistered
310 * Unregisters the specified operations and unassigns them from the
311 * specified family. The operation blocks until the current message
312 * processing has finished and doesn't start again until the
313 * unregister process has finished.
315 * Note: It is not necessary to unregister all operations before
316 * unregistering the family, unregistering the family will cause
317 * all assigned operations to be unregistered automatically.
319 * Returns 0 on success or a negative error code.
321 int genl_unregister_ops(struct genl_family
*family
, struct genl_ops
*ops
)
326 list_for_each_entry(rc
, &family
->ops_list
, ops_list
) {
328 list_del(&ops
->ops_list
);
330 genl_ctrl_event(CTRL_CMD_DELOPS
, ops
);
340 * genl_register_family - register a generic netlink family
341 * @family: generic netlink family
343 * Registers the specified family after validating it first. Only one
344 * family may be registered with the same family name or identifier.
345 * The family id may equal GENL_ID_GENERATE causing an unique id to
346 * be automatically generated and assigned.
348 * Return 0 on success or a negative error code.
350 int genl_register_family(struct genl_family
*family
)
354 if (family
->id
&& family
->id
< GENL_MIN_ID
)
357 if (family
->id
> GENL_MAX_ID
)
360 INIT_LIST_HEAD(&family
->ops_list
);
361 INIT_LIST_HEAD(&family
->mcast_groups
);
365 if (genl_family_find_byname(family
->name
)) {
370 if (family
->id
== GENL_ID_GENERATE
) {
371 u16 newid
= genl_generate_id();
379 } else if (genl_family_find_byid(family
->id
)) {
384 if (family
->maxattr
) {
385 family
->attrbuf
= kmalloc((family
->maxattr
+1) *
386 sizeof(struct nlattr
*), GFP_KERNEL
);
387 if (family
->attrbuf
== NULL
) {
392 family
->attrbuf
= NULL
;
394 list_add_tail(&family
->family_list
, genl_family_chain(family
->id
));
397 genl_ctrl_event(CTRL_CMD_NEWFAMILY
, family
);
408 * genl_register_family_with_ops - register a generic netlink family
409 * @family: generic netlink family
410 * @ops: operations to be registered
411 * @n_ops: number of elements to register
413 * Registers the specified family and operations from the specified table.
414 * Only one family may be registered with the same family name or identifier.
416 * The family id may equal GENL_ID_GENERATE causing an unique id to
417 * be automatically generated and assigned.
419 * Either a doit or dumpit callback must be specified for every registered
420 * operation or the function will fail. Only one operation structure per
421 * command identifier may be registered.
423 * See include/net/genetlink.h for more documenation on the operations
426 * This is equivalent to calling genl_register_family() followed by
427 * genl_register_ops() for every operation entry in the table taking
428 * care to unregister the family on error path.
430 * Return 0 on success or a negative error code.
432 int genl_register_family_with_ops(struct genl_family
*family
,
433 struct genl_ops
*ops
, size_t n_ops
)
437 err
= genl_register_family(family
);
441 for (i
= 0; i
< n_ops
; ++i
, ++ops
) {
442 err
= genl_register_ops(family
, ops
);
448 genl_unregister_family(family
);
451 EXPORT_SYMBOL(genl_register_family_with_ops
);
454 * genl_unregister_family - unregister generic netlink family
455 * @family: generic netlink family
457 * Unregisters the specified family.
459 * Returns 0 on success or a negative error code.
461 int genl_unregister_family(struct genl_family
*family
)
463 struct genl_family
*rc
;
467 genl_unregister_mc_groups(family
);
469 list_for_each_entry(rc
, genl_family_chain(family
->id
), family_list
) {
470 if (family
->id
!= rc
->id
|| strcmp(rc
->name
, family
->name
))
473 list_del(&rc
->family_list
);
474 INIT_LIST_HEAD(&family
->ops_list
);
477 kfree(family
->attrbuf
);
478 genl_ctrl_event(CTRL_CMD_DELFAMILY
, family
);
487 static int genl_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
489 struct genl_ops
*ops
;
490 struct genl_family
*family
;
491 struct net
*net
= sock_net(skb
->sk
);
492 struct genl_info info
;
493 struct genlmsghdr
*hdr
= nlmsg_data(nlh
);
496 family
= genl_family_find_byid(nlh
->nlmsg_type
);
500 /* this family doesn't exist in this netns */
501 if (!family
->netnsok
&& !net_eq(net
, &init_net
))
504 hdrlen
= GENL_HDRLEN
+ family
->hdrsize
;
505 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
508 ops
= genl_get_cmd(hdr
->cmd
, family
);
512 if ((ops
->flags
& GENL_ADMIN_PERM
) &&
513 security_netlink_recv(skb
, CAP_NET_ADMIN
))
516 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
517 if (ops
->dumpit
== NULL
)
521 err
= netlink_dump_start(net
->genl_sock
, skb
, nlh
,
522 ops
->dumpit
, ops
->done
);
527 if (ops
->doit
== NULL
)
530 if (family
->attrbuf
) {
531 err
= nlmsg_parse(nlh
, hdrlen
, family
->attrbuf
, family
->maxattr
,
537 info
.snd_seq
= nlh
->nlmsg_seq
;
538 info
.snd_pid
= NETLINK_CB(skb
).pid
;
540 info
.genlhdr
= nlmsg_data(nlh
);
541 info
.userhdr
= nlmsg_data(nlh
) + GENL_HDRLEN
;
542 info
.attrs
= family
->attrbuf
;
543 genl_info_net_set(&info
, net
);
545 return ops
->doit(skb
, &info
);
548 static void genl_rcv(struct sk_buff
*skb
)
551 netlink_rcv_skb(skb
, &genl_rcv_msg
);
555 /**************************************************************************
557 **************************************************************************/
559 static struct genl_family genl_ctrl
= {
563 .maxattr
= CTRL_ATTR_MAX
,
567 static int ctrl_fill_info(struct genl_family
*family
, u32 pid
, u32 seq
,
568 u32 flags
, struct sk_buff
*skb
, u8 cmd
)
572 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
576 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, family
->name
);
577 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, family
->id
);
578 NLA_PUT_U32(skb
, CTRL_ATTR_VERSION
, family
->version
);
579 NLA_PUT_U32(skb
, CTRL_ATTR_HDRSIZE
, family
->hdrsize
);
580 NLA_PUT_U32(skb
, CTRL_ATTR_MAXATTR
, family
->maxattr
);
582 if (!list_empty(&family
->ops_list
)) {
583 struct nlattr
*nla_ops
;
584 struct genl_ops
*ops
;
587 nla_ops
= nla_nest_start(skb
, CTRL_ATTR_OPS
);
589 goto nla_put_failure
;
591 list_for_each_entry(ops
, &family
->ops_list
, ops_list
) {
594 nest
= nla_nest_start(skb
, idx
++);
596 goto nla_put_failure
;
598 NLA_PUT_U32(skb
, CTRL_ATTR_OP_ID
, ops
->cmd
);
599 NLA_PUT_U32(skb
, CTRL_ATTR_OP_FLAGS
, ops
->flags
);
601 nla_nest_end(skb
, nest
);
604 nla_nest_end(skb
, nla_ops
);
607 if (!list_empty(&family
->mcast_groups
)) {
608 struct genl_multicast_group
*grp
;
609 struct nlattr
*nla_grps
;
612 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
613 if (nla_grps
== NULL
)
614 goto nla_put_failure
;
616 list_for_each_entry(grp
, &family
->mcast_groups
, list
) {
619 nest
= nla_nest_start(skb
, idx
++);
621 goto nla_put_failure
;
623 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
624 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
627 nla_nest_end(skb
, nest
);
629 nla_nest_end(skb
, nla_grps
);
632 return genlmsg_end(skb
, hdr
);
635 genlmsg_cancel(skb
, hdr
);
639 static int ctrl_fill_mcgrp_info(struct genl_multicast_group
*grp
, u32 pid
,
640 u32 seq
, u32 flags
, struct sk_buff
*skb
,
644 struct nlattr
*nla_grps
;
647 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
651 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, grp
->family
->name
);
652 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, grp
->family
->id
);
654 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
655 if (nla_grps
== NULL
)
656 goto nla_put_failure
;
658 nest
= nla_nest_start(skb
, 1);
660 goto nla_put_failure
;
662 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
663 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
666 nla_nest_end(skb
, nest
);
667 nla_nest_end(skb
, nla_grps
);
669 return genlmsg_end(skb
, hdr
);
672 genlmsg_cancel(skb
, hdr
);
676 static int ctrl_dumpfamily(struct sk_buff
*skb
, struct netlink_callback
*cb
)
680 struct genl_family
*rt
;
681 struct net
*net
= sock_net(skb
->sk
);
682 int chains_to_skip
= cb
->args
[0];
683 int fams_to_skip
= cb
->args
[1];
685 for (i
= chains_to_skip
; i
< GENL_FAM_TAB_SIZE
; i
++) {
687 list_for_each_entry(rt
, genl_family_chain(i
), family_list
) {
688 if (!rt
->netnsok
&& !net_eq(net
, &init_net
))
690 if (++n
< fams_to_skip
)
692 if (ctrl_fill_info(rt
, NETLINK_CB(cb
->skb
).pid
,
693 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
694 skb
, CTRL_CMD_NEWFAMILY
) < 0)
708 static struct sk_buff
*ctrl_build_family_msg(struct genl_family
*family
,
709 u32 pid
, int seq
, u8 cmd
)
714 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
716 return ERR_PTR(-ENOBUFS
);
718 err
= ctrl_fill_info(family
, pid
, seq
, 0, skb
, cmd
);
727 static struct sk_buff
*ctrl_build_mcgrp_msg(struct genl_multicast_group
*grp
,
728 u32 pid
, int seq
, u8 cmd
)
733 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
735 return ERR_PTR(-ENOBUFS
);
737 err
= ctrl_fill_mcgrp_info(grp
, pid
, seq
, 0, skb
, cmd
);
746 static const struct nla_policy ctrl_policy
[CTRL_ATTR_MAX
+1] = {
747 [CTRL_ATTR_FAMILY_ID
] = { .type
= NLA_U16
},
748 [CTRL_ATTR_FAMILY_NAME
] = { .type
= NLA_NUL_STRING
,
749 .len
= GENL_NAMSIZ
- 1 },
752 static int ctrl_getfamily(struct sk_buff
*skb
, struct genl_info
*info
)
755 struct genl_family
*res
= NULL
;
758 if (info
->attrs
[CTRL_ATTR_FAMILY_ID
]) {
759 u16 id
= nla_get_u16(info
->attrs
[CTRL_ATTR_FAMILY_ID
]);
760 res
= genl_family_find_byid(id
);
764 if (info
->attrs
[CTRL_ATTR_FAMILY_NAME
]) {
767 name
= nla_data(info
->attrs
[CTRL_ATTR_FAMILY_NAME
]);
768 res
= genl_family_find_byname(name
);
775 if (!res
->netnsok
&& !net_eq(genl_info_net(info
), &init_net
)) {
776 /* family doesn't exist here */
780 msg
= ctrl_build_family_msg(res
, info
->snd_pid
, info
->snd_seq
,
785 return genlmsg_reply(msg
, info
);
788 static int genl_ctrl_event(int event
, void *data
)
791 struct genl_family
*family
;
792 struct genl_multicast_group
*grp
;
794 /* genl is still initialising */
795 if (!init_net
.genl_sock
)
799 case CTRL_CMD_NEWFAMILY
:
800 case CTRL_CMD_DELFAMILY
:
802 msg
= ctrl_build_family_msg(family
, 0, 0, event
);
804 case CTRL_CMD_NEWMCAST_GRP
:
805 case CTRL_CMD_DELMCAST_GRP
:
807 family
= grp
->family
;
808 msg
= ctrl_build_mcgrp_msg(data
, 0, 0, event
);
817 if (!family
->netnsok
) {
818 genlmsg_multicast_netns(&init_net
, msg
, 0,
819 GENL_ID_CTRL
, GFP_KERNEL
);
822 genlmsg_multicast_allns(msg
, 0, GENL_ID_CTRL
, GFP_ATOMIC
);
829 static struct genl_ops genl_ctrl_ops
= {
830 .cmd
= CTRL_CMD_GETFAMILY
,
831 .doit
= ctrl_getfamily
,
832 .dumpit
= ctrl_dumpfamily
,
833 .policy
= ctrl_policy
,
836 static struct genl_multicast_group notify_grp
= {
840 static int __net_init
genl_pernet_init(struct net
*net
)
842 /* we'll bump the group number right afterwards */
843 net
->genl_sock
= netlink_kernel_create(net
, NETLINK_GENERIC
, 0,
844 genl_rcv
, &genl_mutex
,
847 if (!net
->genl_sock
&& net_eq(net
, &init_net
))
848 panic("GENL: Cannot initialize generic netlink\n");
856 static void __net_exit
genl_pernet_exit(struct net
*net
)
858 netlink_kernel_release(net
->genl_sock
);
859 net
->genl_sock
= NULL
;
862 static struct pernet_operations genl_pernet_ops
= {
863 .init
= genl_pernet_init
,
864 .exit
= genl_pernet_exit
,
867 static int __init
genl_init(void)
871 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
872 INIT_LIST_HEAD(&family_ht
[i
]);
874 err
= genl_register_family(&genl_ctrl
);
878 err
= genl_register_ops(&genl_ctrl
, &genl_ctrl_ops
);
882 netlink_set_nonroot(NETLINK_GENERIC
, NL_NONROOT_RECV
);
884 err
= register_pernet_subsys(&genl_pernet_ops
);
888 err
= genl_register_mc_group(&genl_ctrl
, ¬ify_grp
);
895 panic("GENL: Cannot register controller: %d\n", err
);
898 subsys_initcall(genl_init
);
900 EXPORT_SYMBOL(genl_register_ops
);
901 EXPORT_SYMBOL(genl_unregister_ops
);
902 EXPORT_SYMBOL(genl_register_family
);
903 EXPORT_SYMBOL(genl_unregister_family
);
905 static int genlmsg_mcast(struct sk_buff
*skb
, u32 pid
, unsigned long group
,
909 struct net
*net
, *prev
= NULL
;
912 for_each_net_rcu(net
) {
914 tmp
= skb_clone(skb
, flags
);
919 err
= nlmsg_multicast(prev
->genl_sock
, tmp
,
928 return nlmsg_multicast(prev
->genl_sock
, skb
, pid
, group
, flags
);
934 int genlmsg_multicast_allns(struct sk_buff
*skb
, u32 pid
, unsigned int group
,
937 return genlmsg_mcast(skb
, pid
, group
, flags
);
939 EXPORT_SYMBOL(genlmsg_multicast_allns
);