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
) {
179 netlink_table_grab();
181 for_each_net_rcu(net
) {
182 err
= __netlink_change_ngroups(net
->genl_sock
,
183 mc_groups_longs
* BITS_PER_LONG
);
186 * No need to roll back, can only fail if
187 * memory allocation fails and then the
188 * number of _possible_ groups has been
189 * increased on some sockets which is ok.
192 netlink_table_ungrab();
197 netlink_table_ungrab();
199 err
= netlink_change_ngroups(init_net
.genl_sock
,
200 mc_groups_longs
* BITS_PER_LONG
);
206 set_bit(id
, mc_groups
);
207 list_add_tail(&grp
->list
, &family
->mcast_groups
);
208 grp
->family
= family
;
210 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP
, grp
);
215 EXPORT_SYMBOL(genl_register_mc_group
);
217 static void __genl_unregister_mc_group(struct genl_family
*family
,
218 struct genl_multicast_group
*grp
)
221 BUG_ON(grp
->family
!= family
);
223 netlink_table_grab();
225 for_each_net_rcu(net
)
226 __netlink_clear_multicast_users(net
->genl_sock
, grp
->id
);
228 netlink_table_ungrab();
230 clear_bit(grp
->id
, mc_groups
);
231 list_del(&grp
->list
);
232 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP
, grp
);
238 * genl_unregister_mc_group - unregister a multicast group
240 * Unregisters the specified multicast group and notifies userspace
241 * about it. All current listeners on the group are removed.
243 * Note: It is not necessary to unregister all multicast groups before
244 * unregistering the family, unregistering the family will cause
245 * all assigned multicast groups to be unregistered automatically.
247 * @family: Generic netlink family the group belongs to.
248 * @grp: The group to unregister, must have been registered successfully
251 void genl_unregister_mc_group(struct genl_family
*family
,
252 struct genl_multicast_group
*grp
)
255 __genl_unregister_mc_group(family
, grp
);
258 EXPORT_SYMBOL(genl_unregister_mc_group
);
260 static void genl_unregister_mc_groups(struct genl_family
*family
)
262 struct genl_multicast_group
*grp
, *tmp
;
264 list_for_each_entry_safe(grp
, tmp
, &family
->mcast_groups
, list
)
265 __genl_unregister_mc_group(family
, grp
);
269 * genl_register_ops - register generic netlink operations
270 * @family: generic netlink family
271 * @ops: operations to be registered
273 * Registers the specified operations and assigns them to the specified
274 * family. Either a doit or dumpit callback must be specified or the
275 * operation will fail. Only one operation structure per command
276 * identifier may be registered.
278 * See include/net/genetlink.h for more documenation on the operations
281 * Returns 0 on success or a negative error code.
283 int genl_register_ops(struct genl_family
*family
, struct genl_ops
*ops
)
287 if (ops
->dumpit
== NULL
&& ops
->doit
== NULL
)
290 if (genl_get_cmd(ops
->cmd
, family
)) {
296 ops
->flags
|= GENL_CMD_CAP_DUMP
;
298 ops
->flags
|= GENL_CMD_CAP_DO
;
300 ops
->flags
|= GENL_CMD_CAP_HASPOL
;
303 list_add_tail(&ops
->ops_list
, &family
->ops_list
);
306 genl_ctrl_event(CTRL_CMD_NEWOPS
, ops
);
313 * genl_unregister_ops - unregister generic netlink operations
314 * @family: generic netlink family
315 * @ops: operations to be unregistered
317 * Unregisters the specified operations and unassigns them from the
318 * specified family. The operation blocks until the current message
319 * processing has finished and doesn't start again until the
320 * unregister process has finished.
322 * Note: It is not necessary to unregister all operations before
323 * unregistering the family, unregistering the family will cause
324 * all assigned operations to be unregistered automatically.
326 * Returns 0 on success or a negative error code.
328 int genl_unregister_ops(struct genl_family
*family
, struct genl_ops
*ops
)
333 list_for_each_entry(rc
, &family
->ops_list
, ops_list
) {
335 list_del(&ops
->ops_list
);
337 genl_ctrl_event(CTRL_CMD_DELOPS
, ops
);
347 * genl_register_family - register a generic netlink family
348 * @family: generic netlink family
350 * Registers the specified family after validating it first. Only one
351 * family may be registered with the same family name or identifier.
352 * The family id may equal GENL_ID_GENERATE causing an unique id to
353 * be automatically generated and assigned.
355 * Return 0 on success or a negative error code.
357 int genl_register_family(struct genl_family
*family
)
361 if (family
->id
&& family
->id
< GENL_MIN_ID
)
364 if (family
->id
> GENL_MAX_ID
)
367 INIT_LIST_HEAD(&family
->ops_list
);
368 INIT_LIST_HEAD(&family
->mcast_groups
);
372 if (genl_family_find_byname(family
->name
)) {
377 if (genl_family_find_byid(family
->id
)) {
382 if (family
->id
== GENL_ID_GENERATE
) {
383 u16 newid
= genl_generate_id();
393 if (family
->maxattr
) {
394 family
->attrbuf
= kmalloc((family
->maxattr
+1) *
395 sizeof(struct nlattr
*), GFP_KERNEL
);
396 if (family
->attrbuf
== NULL
) {
401 family
->attrbuf
= NULL
;
403 list_add_tail(&family
->family_list
, genl_family_chain(family
->id
));
406 genl_ctrl_event(CTRL_CMD_NEWFAMILY
, family
);
417 * genl_register_family_with_ops - register a generic netlink family
418 * @family: generic netlink family
419 * @ops: operations to be registered
420 * @n_ops: number of elements to register
422 * Registers the specified family and operations from the specified table.
423 * Only one family may be registered with the same family name or identifier.
425 * The family id may equal GENL_ID_GENERATE causing an unique id to
426 * be automatically generated and assigned.
428 * Either a doit or dumpit callback must be specified for every registered
429 * operation or the function will fail. Only one operation structure per
430 * command identifier may be registered.
432 * See include/net/genetlink.h for more documenation on the operations
435 * This is equivalent to calling genl_register_family() followed by
436 * genl_register_ops() for every operation entry in the table taking
437 * care to unregister the family on error path.
439 * Return 0 on success or a negative error code.
441 int genl_register_family_with_ops(struct genl_family
*family
,
442 struct genl_ops
*ops
, size_t n_ops
)
446 err
= genl_register_family(family
);
450 for (i
= 0; i
< n_ops
; ++i
, ++ops
) {
451 err
= genl_register_ops(family
, ops
);
457 genl_unregister_family(family
);
460 EXPORT_SYMBOL(genl_register_family_with_ops
);
463 * genl_unregister_family - unregister generic netlink family
464 * @family: generic netlink family
466 * Unregisters the specified family.
468 * Returns 0 on success or a negative error code.
470 int genl_unregister_family(struct genl_family
*family
)
472 struct genl_family
*rc
;
476 genl_unregister_mc_groups(family
);
478 list_for_each_entry(rc
, genl_family_chain(family
->id
), family_list
) {
479 if (family
->id
!= rc
->id
|| strcmp(rc
->name
, family
->name
))
482 list_del(&rc
->family_list
);
483 INIT_LIST_HEAD(&family
->ops_list
);
486 kfree(family
->attrbuf
);
487 genl_ctrl_event(CTRL_CMD_DELFAMILY
, family
);
496 static int genl_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
498 struct genl_ops
*ops
;
499 struct genl_family
*family
;
500 struct net
*net
= sock_net(skb
->sk
);
501 struct genl_info info
;
502 struct genlmsghdr
*hdr
= nlmsg_data(nlh
);
505 family
= genl_family_find_byid(nlh
->nlmsg_type
);
509 /* this family doesn't exist in this netns */
510 if (!family
->netnsok
&& !net_eq(net
, &init_net
))
513 hdrlen
= GENL_HDRLEN
+ family
->hdrsize
;
514 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
517 ops
= genl_get_cmd(hdr
->cmd
, family
);
521 if ((ops
->flags
& GENL_ADMIN_PERM
) &&
522 security_netlink_recv(skb
, CAP_NET_ADMIN
))
525 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
526 if (ops
->dumpit
== NULL
)
530 err
= netlink_dump_start(net
->genl_sock
, skb
, nlh
,
531 ops
->dumpit
, ops
->done
);
536 if (ops
->doit
== NULL
)
539 if (family
->attrbuf
) {
540 err
= nlmsg_parse(nlh
, hdrlen
, family
->attrbuf
, family
->maxattr
,
546 info
.snd_seq
= nlh
->nlmsg_seq
;
547 info
.snd_pid
= NETLINK_CB(skb
).pid
;
549 info
.genlhdr
= nlmsg_data(nlh
);
550 info
.userhdr
= nlmsg_data(nlh
) + GENL_HDRLEN
;
551 info
.attrs
= family
->attrbuf
;
552 genl_info_net_set(&info
, net
);
554 return ops
->doit(skb
, &info
);
557 static void genl_rcv(struct sk_buff
*skb
)
560 netlink_rcv_skb(skb
, &genl_rcv_msg
);
564 /**************************************************************************
566 **************************************************************************/
568 static struct genl_family genl_ctrl
= {
572 .maxattr
= CTRL_ATTR_MAX
,
576 static int ctrl_fill_info(struct genl_family
*family
, u32 pid
, u32 seq
,
577 u32 flags
, struct sk_buff
*skb
, u8 cmd
)
581 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
585 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, family
->name
);
586 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, family
->id
);
587 NLA_PUT_U32(skb
, CTRL_ATTR_VERSION
, family
->version
);
588 NLA_PUT_U32(skb
, CTRL_ATTR_HDRSIZE
, family
->hdrsize
);
589 NLA_PUT_U32(skb
, CTRL_ATTR_MAXATTR
, family
->maxattr
);
591 if (!list_empty(&family
->ops_list
)) {
592 struct nlattr
*nla_ops
;
593 struct genl_ops
*ops
;
596 nla_ops
= nla_nest_start(skb
, CTRL_ATTR_OPS
);
598 goto nla_put_failure
;
600 list_for_each_entry(ops
, &family
->ops_list
, ops_list
) {
603 nest
= nla_nest_start(skb
, idx
++);
605 goto nla_put_failure
;
607 NLA_PUT_U32(skb
, CTRL_ATTR_OP_ID
, ops
->cmd
);
608 NLA_PUT_U32(skb
, CTRL_ATTR_OP_FLAGS
, ops
->flags
);
610 nla_nest_end(skb
, nest
);
613 nla_nest_end(skb
, nla_ops
);
616 if (!list_empty(&family
->mcast_groups
)) {
617 struct genl_multicast_group
*grp
;
618 struct nlattr
*nla_grps
;
621 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
622 if (nla_grps
== NULL
)
623 goto nla_put_failure
;
625 list_for_each_entry(grp
, &family
->mcast_groups
, list
) {
628 nest
= nla_nest_start(skb
, idx
++);
630 goto nla_put_failure
;
632 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
633 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
636 nla_nest_end(skb
, nest
);
638 nla_nest_end(skb
, nla_grps
);
641 return genlmsg_end(skb
, hdr
);
644 genlmsg_cancel(skb
, hdr
);
648 static int ctrl_fill_mcgrp_info(struct genl_multicast_group
*grp
, u32 pid
,
649 u32 seq
, u32 flags
, struct sk_buff
*skb
,
653 struct nlattr
*nla_grps
;
656 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
660 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, grp
->family
->name
);
661 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, grp
->family
->id
);
663 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
664 if (nla_grps
== NULL
)
665 goto nla_put_failure
;
667 nest
= nla_nest_start(skb
, 1);
669 goto nla_put_failure
;
671 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
672 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
675 nla_nest_end(skb
, nest
);
676 nla_nest_end(skb
, nla_grps
);
678 return genlmsg_end(skb
, hdr
);
681 genlmsg_cancel(skb
, hdr
);
685 static int ctrl_dumpfamily(struct sk_buff
*skb
, struct netlink_callback
*cb
)
689 struct genl_family
*rt
;
690 struct net
*net
= sock_net(skb
->sk
);
691 int chains_to_skip
= cb
->args
[0];
692 int fams_to_skip
= cb
->args
[1];
694 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++) {
695 if (i
< chains_to_skip
)
698 list_for_each_entry(rt
, genl_family_chain(i
), family_list
) {
699 if (!rt
->netnsok
&& !net_eq(net
, &init_net
))
701 if (++n
< fams_to_skip
)
703 if (ctrl_fill_info(rt
, NETLINK_CB(cb
->skb
).pid
,
704 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
705 skb
, CTRL_CMD_NEWFAMILY
) < 0)
719 static struct sk_buff
*ctrl_build_family_msg(struct genl_family
*family
,
720 u32 pid
, int seq
, u8 cmd
)
725 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
727 return ERR_PTR(-ENOBUFS
);
729 err
= ctrl_fill_info(family
, pid
, seq
, 0, skb
, cmd
);
738 static struct sk_buff
*ctrl_build_mcgrp_msg(struct genl_multicast_group
*grp
,
739 u32 pid
, int seq
, u8 cmd
)
744 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
746 return ERR_PTR(-ENOBUFS
);
748 err
= ctrl_fill_mcgrp_info(grp
, pid
, seq
, 0, skb
, cmd
);
757 static const struct nla_policy ctrl_policy
[CTRL_ATTR_MAX
+1] = {
758 [CTRL_ATTR_FAMILY_ID
] = { .type
= NLA_U16
},
759 [CTRL_ATTR_FAMILY_NAME
] = { .type
= NLA_NUL_STRING
,
760 .len
= GENL_NAMSIZ
- 1 },
763 static int ctrl_getfamily(struct sk_buff
*skb
, struct genl_info
*info
)
766 struct genl_family
*res
= NULL
;
769 if (info
->attrs
[CTRL_ATTR_FAMILY_ID
]) {
770 u16 id
= nla_get_u16(info
->attrs
[CTRL_ATTR_FAMILY_ID
]);
771 res
= genl_family_find_byid(id
);
775 if (info
->attrs
[CTRL_ATTR_FAMILY_NAME
]) {
778 name
= nla_data(info
->attrs
[CTRL_ATTR_FAMILY_NAME
]);
779 res
= genl_family_find_byname(name
);
786 if (!res
->netnsok
&& !net_eq(genl_info_net(info
), &init_net
)) {
787 /* family doesn't exist here */
791 msg
= ctrl_build_family_msg(res
, info
->snd_pid
, info
->snd_seq
,
796 return genlmsg_reply(msg
, info
);
799 static int genl_ctrl_event(int event
, void *data
)
802 struct genl_family
*family
;
803 struct genl_multicast_group
*grp
;
805 /* genl is still initialising */
806 if (!init_net
.genl_sock
)
810 case CTRL_CMD_NEWFAMILY
:
811 case CTRL_CMD_DELFAMILY
:
813 msg
= ctrl_build_family_msg(family
, 0, 0, event
);
815 case CTRL_CMD_NEWMCAST_GRP
:
816 case CTRL_CMD_DELMCAST_GRP
:
818 family
= grp
->family
;
819 msg
= ctrl_build_mcgrp_msg(data
, 0, 0, event
);
828 if (!family
->netnsok
) {
829 genlmsg_multicast_netns(&init_net
, msg
, 0,
830 GENL_ID_CTRL
, GFP_KERNEL
);
833 genlmsg_multicast_allns(msg
, 0, GENL_ID_CTRL
, GFP_ATOMIC
);
840 static struct genl_ops genl_ctrl_ops
= {
841 .cmd
= CTRL_CMD_GETFAMILY
,
842 .doit
= ctrl_getfamily
,
843 .dumpit
= ctrl_dumpfamily
,
844 .policy
= ctrl_policy
,
847 static struct genl_multicast_group notify_grp
= {
851 static int __net_init
genl_pernet_init(struct net
*net
)
853 /* we'll bump the group number right afterwards */
854 net
->genl_sock
= netlink_kernel_create(net
, NETLINK_GENERIC
, 0,
855 genl_rcv
, &genl_mutex
,
858 if (!net
->genl_sock
&& net_eq(net
, &init_net
))
859 panic("GENL: Cannot initialize generic netlink\n");
867 static void __net_exit
genl_pernet_exit(struct net
*net
)
869 netlink_kernel_release(net
->genl_sock
);
870 net
->genl_sock
= NULL
;
873 static struct pernet_operations genl_pernet_ops
= {
874 .init
= genl_pernet_init
,
875 .exit
= genl_pernet_exit
,
878 static int __init
genl_init(void)
882 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
883 INIT_LIST_HEAD(&family_ht
[i
]);
885 err
= genl_register_family(&genl_ctrl
);
889 err
= genl_register_ops(&genl_ctrl
, &genl_ctrl_ops
);
893 netlink_set_nonroot(NETLINK_GENERIC
, NL_NONROOT_RECV
);
895 err
= register_pernet_subsys(&genl_pernet_ops
);
899 err
= genl_register_mc_group(&genl_ctrl
, ¬ify_grp
);
906 panic("GENL: Cannot register controller: %d\n", err
);
909 subsys_initcall(genl_init
);
911 EXPORT_SYMBOL(genl_register_ops
);
912 EXPORT_SYMBOL(genl_unregister_ops
);
913 EXPORT_SYMBOL(genl_register_family
);
914 EXPORT_SYMBOL(genl_unregister_family
);
916 static int genlmsg_mcast(struct sk_buff
*skb
, u32 pid
, unsigned long group
,
920 struct net
*net
, *prev
= NULL
;
923 for_each_net_rcu(net
) {
925 tmp
= skb_clone(skb
, flags
);
930 err
= nlmsg_multicast(prev
->genl_sock
, tmp
,
939 return nlmsg_multicast(prev
->genl_sock
, skb
, pid
, group
, flags
);
945 int genlmsg_multicast_allns(struct sk_buff
*skb
, u32 pid
, unsigned int group
,
948 return genlmsg_mcast(skb
, pid
, group
, flags
);
950 EXPORT_SYMBOL(genlmsg_multicast_allns
);