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 static DEFINE_MUTEX(genl_mutex
); /* serialization of message processing */
23 static inline void genl_lock(void)
25 mutex_lock(&genl_mutex
);
28 static inline void genl_unlock(void)
30 mutex_unlock(&genl_mutex
);
33 #define GENL_FAM_TAB_SIZE 16
34 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
36 static struct list_head family_ht
[GENL_FAM_TAB_SIZE
];
38 * Bitmap of multicast groups that are currently in use.
40 * To avoid an allocation at boot of just one unsigned long,
41 * declare it global instead.
42 * Bit 0 is marked as already used since group 0 is invalid.
44 static unsigned long mc_group_start
= 0x1;
45 static unsigned long *mc_groups
= &mc_group_start
;
46 static unsigned long mc_groups_longs
= 1;
48 static int genl_ctrl_event(int event
, void *data
);
50 static inline unsigned int genl_family_hash(unsigned int id
)
52 return id
& GENL_FAM_TAB_MASK
;
55 static inline struct list_head
*genl_family_chain(unsigned int id
)
57 return &family_ht
[genl_family_hash(id
)];
60 static struct genl_family
*genl_family_find_byid(unsigned int id
)
62 struct genl_family
*f
;
64 list_for_each_entry(f
, genl_family_chain(id
), family_list
)
71 static struct genl_family
*genl_family_find_byname(char *name
)
73 struct genl_family
*f
;
76 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
77 list_for_each_entry(f
, genl_family_chain(i
), family_list
)
78 if (strcmp(f
->name
, name
) == 0)
84 static struct genl_ops
*genl_get_cmd(u8 cmd
, struct genl_family
*family
)
88 list_for_each_entry(ops
, &family
->ops_list
, ops_list
)
95 /* Of course we are going to have problems once we hit
96 * 2^16 alive types, but that can only happen by year 2K
98 static inline u16
genl_generate_id(void)
100 static u16 id_gen_idx
;
105 id_gen_idx
= GENL_MIN_ID
;
107 if (++id_gen_idx
> GENL_MAX_ID
) {
116 } while (genl_family_find_byid(id_gen_idx
));
121 static struct genl_multicast_group notify_grp
;
124 * genl_register_mc_group - register a multicast group
126 * Registers the specified multicast group and notifies userspace
127 * about the new group.
129 * Returns 0 on success or a negative error code.
131 * @family: The generic netlink family the group shall be registered for.
132 * @grp: The group to register, must have a name.
134 int genl_register_mc_group(struct genl_family
*family
,
135 struct genl_multicast_group
*grp
)
138 unsigned long *new_groups
;
141 BUG_ON(grp
->name
[0] == '\0');
145 /* special-case our own group */
146 if (grp
== ¬ify_grp
)
149 id
= find_first_zero_bit(mc_groups
,
150 mc_groups_longs
* BITS_PER_LONG
);
153 if (id
>= mc_groups_longs
* BITS_PER_LONG
) {
154 size_t nlen
= (mc_groups_longs
+ 1) * sizeof(unsigned long);
156 if (mc_groups
== &mc_group_start
) {
157 new_groups
= kzalloc(nlen
, GFP_KERNEL
);
162 mc_groups
= new_groups
;
163 *mc_groups
= mc_group_start
;
165 new_groups
= krealloc(mc_groups
, nlen
, GFP_KERNEL
);
170 mc_groups
= new_groups
;
171 mc_groups
[mc_groups_longs
] = 0;
176 if (family
->netnsok
) {
180 for_each_net_rcu(net
) {
181 err
= netlink_change_ngroups(net
->genl_sock
,
182 mc_groups_longs
* BITS_PER_LONG
);
185 * No need to roll back, can only fail if
186 * memory allocation fails and then the
187 * number of _possible_ groups has been
188 * increased on some sockets which is ok.
196 err
= netlink_change_ngroups(init_net
.genl_sock
,
197 mc_groups_longs
* BITS_PER_LONG
);
203 set_bit(id
, mc_groups
);
204 list_add_tail(&grp
->list
, &family
->mcast_groups
);
205 grp
->family
= family
;
207 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP
, grp
);
212 EXPORT_SYMBOL(genl_register_mc_group
);
214 static void __genl_unregister_mc_group(struct genl_family
*family
,
215 struct genl_multicast_group
*grp
)
218 BUG_ON(grp
->family
!= family
);
221 for_each_net_rcu(net
)
222 netlink_clear_multicast_users(net
->genl_sock
, grp
->id
);
225 clear_bit(grp
->id
, mc_groups
);
226 list_del(&grp
->list
);
227 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP
, grp
);
233 * genl_unregister_mc_group - unregister a multicast group
235 * Unregisters the specified multicast group and notifies userspace
236 * about it. All current listeners on the group are removed.
238 * Note: It is not necessary to unregister all multicast groups before
239 * unregistering the family, unregistering the family will cause
240 * all assigned multicast groups to be unregistered automatically.
242 * @family: Generic netlink family the group belongs to.
243 * @grp: The group to unregister, must have been registered successfully
246 void genl_unregister_mc_group(struct genl_family
*family
,
247 struct genl_multicast_group
*grp
)
250 __genl_unregister_mc_group(family
, grp
);
253 EXPORT_SYMBOL(genl_unregister_mc_group
);
255 static void genl_unregister_mc_groups(struct genl_family
*family
)
257 struct genl_multicast_group
*grp
, *tmp
;
259 list_for_each_entry_safe(grp
, tmp
, &family
->mcast_groups
, list
)
260 __genl_unregister_mc_group(family
, grp
);
264 * genl_register_ops - register generic netlink operations
265 * @family: generic netlink family
266 * @ops: operations to be registered
268 * Registers the specified operations and assigns them to the specified
269 * family. Either a doit or dumpit callback must be specified or the
270 * operation will fail. Only one operation structure per command
271 * identifier may be registered.
273 * See include/net/genetlink.h for more documenation on the operations
276 * Returns 0 on success or a negative error code.
278 int genl_register_ops(struct genl_family
*family
, struct genl_ops
*ops
)
282 if (ops
->dumpit
== NULL
&& ops
->doit
== NULL
)
285 if (genl_get_cmd(ops
->cmd
, family
)) {
291 ops
->flags
|= GENL_CMD_CAP_DUMP
;
293 ops
->flags
|= GENL_CMD_CAP_DO
;
295 ops
->flags
|= GENL_CMD_CAP_HASPOL
;
298 list_add_tail(&ops
->ops_list
, &family
->ops_list
);
301 genl_ctrl_event(CTRL_CMD_NEWOPS
, ops
);
308 * genl_unregister_ops - unregister generic netlink operations
309 * @family: generic netlink family
310 * @ops: operations to be unregistered
312 * Unregisters the specified operations and unassigns them from the
313 * specified family. The operation blocks until the current message
314 * processing has finished and doesn't start again until the
315 * unregister process has finished.
317 * Note: It is not necessary to unregister all operations before
318 * unregistering the family, unregistering the family will cause
319 * all assigned operations to be unregistered automatically.
321 * Returns 0 on success or a negative error code.
323 int genl_unregister_ops(struct genl_family
*family
, struct genl_ops
*ops
)
328 list_for_each_entry(rc
, &family
->ops_list
, ops_list
) {
330 list_del(&ops
->ops_list
);
332 genl_ctrl_event(CTRL_CMD_DELOPS
, ops
);
342 * genl_register_family - register a generic netlink family
343 * @family: generic netlink family
345 * Registers the specified family after validating it first. Only one
346 * family may be registered with the same family name or identifier.
347 * The family id may equal GENL_ID_GENERATE causing an unique id to
348 * be automatically generated and assigned.
350 * Return 0 on success or a negative error code.
352 int genl_register_family(struct genl_family
*family
)
356 if (family
->id
&& family
->id
< GENL_MIN_ID
)
359 if (family
->id
> GENL_MAX_ID
)
362 INIT_LIST_HEAD(&family
->ops_list
);
363 INIT_LIST_HEAD(&family
->mcast_groups
);
367 if (genl_family_find_byname(family
->name
)) {
372 if (genl_family_find_byid(family
->id
)) {
377 if (family
->id
== GENL_ID_GENERATE
) {
378 u16 newid
= genl_generate_id();
388 if (family
->maxattr
) {
389 family
->attrbuf
= kmalloc((family
->maxattr
+1) *
390 sizeof(struct nlattr
*), GFP_KERNEL
);
391 if (family
->attrbuf
== NULL
) {
396 family
->attrbuf
= NULL
;
398 list_add_tail(&family
->family_list
, genl_family_chain(family
->id
));
401 genl_ctrl_event(CTRL_CMD_NEWFAMILY
, family
);
412 * genl_register_family_with_ops - register a generic netlink family
413 * @family: generic netlink family
414 * @ops: operations to be registered
415 * @n_ops: number of elements to register
417 * Registers the specified family and operations from the specified table.
418 * Only one family may be registered with the same family name or identifier.
420 * The family id may equal GENL_ID_GENERATE causing an unique id to
421 * be automatically generated and assigned.
423 * Either a doit or dumpit callback must be specified for every registered
424 * operation or the function will fail. Only one operation structure per
425 * command identifier may be registered.
427 * See include/net/genetlink.h for more documenation on the operations
430 * This is equivalent to calling genl_register_family() followed by
431 * genl_register_ops() for every operation entry in the table taking
432 * care to unregister the family on error path.
434 * Return 0 on success or a negative error code.
436 int genl_register_family_with_ops(struct genl_family
*family
,
437 struct genl_ops
*ops
, size_t n_ops
)
441 err
= genl_register_family(family
);
445 for (i
= 0; i
< n_ops
; ++i
, ++ops
) {
446 err
= genl_register_ops(family
, ops
);
452 genl_unregister_family(family
);
455 EXPORT_SYMBOL(genl_register_family_with_ops
);
458 * genl_unregister_family - unregister generic netlink family
459 * @family: generic netlink family
461 * Unregisters the specified family.
463 * Returns 0 on success or a negative error code.
465 int genl_unregister_family(struct genl_family
*family
)
467 struct genl_family
*rc
;
471 genl_unregister_mc_groups(family
);
473 list_for_each_entry(rc
, genl_family_chain(family
->id
), family_list
) {
474 if (family
->id
!= rc
->id
|| strcmp(rc
->name
, family
->name
))
477 list_del(&rc
->family_list
);
478 INIT_LIST_HEAD(&family
->ops_list
);
481 kfree(family
->attrbuf
);
482 genl_ctrl_event(CTRL_CMD_DELFAMILY
, family
);
491 static int genl_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
493 struct genl_ops
*ops
;
494 struct genl_family
*family
;
495 struct net
*net
= sock_net(skb
->sk
);
496 struct genl_info info
;
497 struct genlmsghdr
*hdr
= nlmsg_data(nlh
);
500 family
= genl_family_find_byid(nlh
->nlmsg_type
);
504 /* this family doesn't exist in this netns */
505 if (!family
->netnsok
&& !net_eq(net
, &init_net
))
508 hdrlen
= GENL_HDRLEN
+ family
->hdrsize
;
509 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
512 ops
= genl_get_cmd(hdr
->cmd
, family
);
516 if ((ops
->flags
& GENL_ADMIN_PERM
) &&
517 security_netlink_recv(skb
, CAP_NET_ADMIN
))
520 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
521 if (ops
->dumpit
== NULL
)
525 err
= netlink_dump_start(net
->genl_sock
, skb
, nlh
,
526 ops
->dumpit
, ops
->done
);
531 if (ops
->doit
== NULL
)
534 if (family
->attrbuf
) {
535 err
= nlmsg_parse(nlh
, hdrlen
, family
->attrbuf
, family
->maxattr
,
541 info
.snd_seq
= nlh
->nlmsg_seq
;
542 info
.snd_pid
= NETLINK_CB(skb
).pid
;
544 info
.genlhdr
= nlmsg_data(nlh
);
545 info
.userhdr
= nlmsg_data(nlh
) + GENL_HDRLEN
;
546 info
.attrs
= family
->attrbuf
;
547 genl_info_net_set(&info
, net
);
549 return ops
->doit(skb
, &info
);
552 static void genl_rcv(struct sk_buff
*skb
)
555 netlink_rcv_skb(skb
, &genl_rcv_msg
);
559 /**************************************************************************
561 **************************************************************************/
563 static struct genl_family genl_ctrl
= {
567 .maxattr
= CTRL_ATTR_MAX
,
571 static int ctrl_fill_info(struct genl_family
*family
, u32 pid
, u32 seq
,
572 u32 flags
, struct sk_buff
*skb
, u8 cmd
)
576 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
580 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, family
->name
);
581 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, family
->id
);
582 NLA_PUT_U32(skb
, CTRL_ATTR_VERSION
, family
->version
);
583 NLA_PUT_U32(skb
, CTRL_ATTR_HDRSIZE
, family
->hdrsize
);
584 NLA_PUT_U32(skb
, CTRL_ATTR_MAXATTR
, family
->maxattr
);
586 if (!list_empty(&family
->ops_list
)) {
587 struct nlattr
*nla_ops
;
588 struct genl_ops
*ops
;
591 nla_ops
= nla_nest_start(skb
, CTRL_ATTR_OPS
);
593 goto nla_put_failure
;
595 list_for_each_entry(ops
, &family
->ops_list
, ops_list
) {
598 nest
= nla_nest_start(skb
, idx
++);
600 goto nla_put_failure
;
602 NLA_PUT_U32(skb
, CTRL_ATTR_OP_ID
, ops
->cmd
);
603 NLA_PUT_U32(skb
, CTRL_ATTR_OP_FLAGS
, ops
->flags
);
605 nla_nest_end(skb
, nest
);
608 nla_nest_end(skb
, nla_ops
);
611 if (!list_empty(&family
->mcast_groups
)) {
612 struct genl_multicast_group
*grp
;
613 struct nlattr
*nla_grps
;
616 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
617 if (nla_grps
== NULL
)
618 goto nla_put_failure
;
620 list_for_each_entry(grp
, &family
->mcast_groups
, list
) {
623 nest
= nla_nest_start(skb
, idx
++);
625 goto nla_put_failure
;
627 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
628 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
631 nla_nest_end(skb
, nest
);
633 nla_nest_end(skb
, nla_grps
);
636 return genlmsg_end(skb
, hdr
);
639 genlmsg_cancel(skb
, hdr
);
643 static int ctrl_fill_mcgrp_info(struct genl_multicast_group
*grp
, u32 pid
,
644 u32 seq
, u32 flags
, struct sk_buff
*skb
,
648 struct nlattr
*nla_grps
;
651 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
655 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, grp
->family
->name
);
656 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, grp
->family
->id
);
658 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
659 if (nla_grps
== NULL
)
660 goto nla_put_failure
;
662 nest
= nla_nest_start(skb
, 1);
664 goto nla_put_failure
;
666 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
667 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
670 nla_nest_end(skb
, nest
);
671 nla_nest_end(skb
, nla_grps
);
673 return genlmsg_end(skb
, hdr
);
676 genlmsg_cancel(skb
, hdr
);
680 static int ctrl_dumpfamily(struct sk_buff
*skb
, struct netlink_callback
*cb
)
684 struct genl_family
*rt
;
685 struct net
*net
= sock_net(skb
->sk
);
686 int chains_to_skip
= cb
->args
[0];
687 int fams_to_skip
= cb
->args
[1];
689 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++) {
690 if (i
< chains_to_skip
)
693 list_for_each_entry(rt
, genl_family_chain(i
), family_list
) {
694 if (!rt
->netnsok
&& !net_eq(net
, &init_net
))
696 if (++n
< fams_to_skip
)
698 if (ctrl_fill_info(rt
, NETLINK_CB(cb
->skb
).pid
,
699 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
700 skb
, CTRL_CMD_NEWFAMILY
) < 0)
714 static struct sk_buff
*ctrl_build_family_msg(struct genl_family
*family
,
715 u32 pid
, int seq
, u8 cmd
)
720 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
722 return ERR_PTR(-ENOBUFS
);
724 err
= ctrl_fill_info(family
, pid
, seq
, 0, skb
, cmd
);
733 static struct sk_buff
*ctrl_build_mcgrp_msg(struct genl_multicast_group
*grp
,
734 u32 pid
, int seq
, u8 cmd
)
739 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
741 return ERR_PTR(-ENOBUFS
);
743 err
= ctrl_fill_mcgrp_info(grp
, pid
, seq
, 0, skb
, cmd
);
752 static const struct nla_policy ctrl_policy
[CTRL_ATTR_MAX
+1] = {
753 [CTRL_ATTR_FAMILY_ID
] = { .type
= NLA_U16
},
754 [CTRL_ATTR_FAMILY_NAME
] = { .type
= NLA_NUL_STRING
,
755 .len
= GENL_NAMSIZ
- 1 },
758 static int ctrl_getfamily(struct sk_buff
*skb
, struct genl_info
*info
)
761 struct genl_family
*res
= NULL
;
764 if (info
->attrs
[CTRL_ATTR_FAMILY_ID
]) {
765 u16 id
= nla_get_u16(info
->attrs
[CTRL_ATTR_FAMILY_ID
]);
766 res
= genl_family_find_byid(id
);
770 if (info
->attrs
[CTRL_ATTR_FAMILY_NAME
]) {
773 name
= nla_data(info
->attrs
[CTRL_ATTR_FAMILY_NAME
]);
774 res
= genl_family_find_byname(name
);
781 if (!res
->netnsok
&& !net_eq(genl_info_net(info
), &init_net
)) {
782 /* family doesn't exist here */
786 msg
= ctrl_build_family_msg(res
, info
->snd_pid
, info
->snd_seq
,
791 return genlmsg_reply(msg
, info
);
794 static int genl_ctrl_event(int event
, void *data
)
797 struct genl_family
*family
;
798 struct genl_multicast_group
*grp
;
800 /* genl is still initialising */
801 if (!init_net
.genl_sock
)
805 case CTRL_CMD_NEWFAMILY
:
806 case CTRL_CMD_DELFAMILY
:
808 msg
= ctrl_build_family_msg(family
, 0, 0, event
);
810 case CTRL_CMD_NEWMCAST_GRP
:
811 case CTRL_CMD_DELMCAST_GRP
:
813 family
= grp
->family
;
814 msg
= ctrl_build_mcgrp_msg(data
, 0, 0, event
);
823 if (!family
->netnsok
) {
824 genlmsg_multicast_netns(&init_net
, msg
, 0,
825 GENL_ID_CTRL
, GFP_KERNEL
);
828 genlmsg_multicast_allns(msg
, 0, GENL_ID_CTRL
, GFP_ATOMIC
);
835 static struct genl_ops genl_ctrl_ops
= {
836 .cmd
= CTRL_CMD_GETFAMILY
,
837 .doit
= ctrl_getfamily
,
838 .dumpit
= ctrl_dumpfamily
,
839 .policy
= ctrl_policy
,
842 static struct genl_multicast_group notify_grp
= {
846 static int __net_init
genl_pernet_init(struct net
*net
)
848 /* we'll bump the group number right afterwards */
849 net
->genl_sock
= netlink_kernel_create(net
, NETLINK_GENERIC
, 0,
850 genl_rcv
, &genl_mutex
,
853 if (!net
->genl_sock
&& net_eq(net
, &init_net
))
854 panic("GENL: Cannot initialize generic netlink\n");
862 static void __net_exit
genl_pernet_exit(struct net
*net
)
864 netlink_kernel_release(net
->genl_sock
);
865 net
->genl_sock
= NULL
;
868 static struct pernet_operations genl_pernet_ops
= {
869 .init
= genl_pernet_init
,
870 .exit
= genl_pernet_exit
,
873 static int __init
genl_init(void)
877 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
878 INIT_LIST_HEAD(&family_ht
[i
]);
880 err
= genl_register_family(&genl_ctrl
);
884 err
= genl_register_ops(&genl_ctrl
, &genl_ctrl_ops
);
888 netlink_set_nonroot(NETLINK_GENERIC
, NL_NONROOT_RECV
);
890 err
= register_pernet_subsys(&genl_pernet_ops
);
894 err
= genl_register_mc_group(&genl_ctrl
, ¬ify_grp
);
901 panic("GENL: Cannot register controller: %d\n", err
);
904 subsys_initcall(genl_init
);
906 EXPORT_SYMBOL(genl_register_ops
);
907 EXPORT_SYMBOL(genl_unregister_ops
);
908 EXPORT_SYMBOL(genl_register_family
);
909 EXPORT_SYMBOL(genl_unregister_family
);
911 static int genlmsg_mcast(struct sk_buff
*skb
, u32 pid
, unsigned long group
,
915 struct net
*net
, *prev
= NULL
;
918 for_each_net_rcu(net
) {
920 tmp
= skb_clone(skb
, flags
);
925 err
= nlmsg_multicast(prev
->genl_sock
, tmp
,
934 return nlmsg_multicast(prev
->genl_sock
, skb
, pid
, group
, flags
);
940 int genlmsg_multicast_allns(struct sk_buff
*skb
, u32 pid
, unsigned int group
,
943 return genlmsg_mcast(skb
, pid
, group
, flags
);
945 EXPORT_SYMBOL(genlmsg_multicast_allns
);