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 */
25 mutex_lock(&genl_mutex
);
27 EXPORT_SYMBOL(genl_lock
);
29 void genl_unlock(void)
31 mutex_unlock(&genl_mutex
);
33 EXPORT_SYMBOL(genl_unlock
);
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
= GENL_MIN_ID
;
105 for (i
= 0; i
<= GENL_MAX_ID
- GENL_MIN_ID
; i
++) {
106 if (!genl_family_find_byid(id_gen_idx
))
108 if (++id_gen_idx
> GENL_MAX_ID
)
109 id_gen_idx
= GENL_MIN_ID
;
115 static struct genl_multicast_group notify_grp
;
118 * genl_register_mc_group - register a multicast group
120 * Registers the specified multicast group and notifies userspace
121 * about the new group.
123 * Returns 0 on success or a negative error code.
125 * @family: The generic netlink family the group shall be registered for.
126 * @grp: The group to register, must have a name.
128 int genl_register_mc_group(struct genl_family
*family
,
129 struct genl_multicast_group
*grp
)
132 unsigned long *new_groups
;
135 BUG_ON(grp
->name
[0] == '\0');
139 /* special-case our own group */
140 if (grp
== ¬ify_grp
)
143 id
= find_first_zero_bit(mc_groups
,
144 mc_groups_longs
* BITS_PER_LONG
);
147 if (id
>= mc_groups_longs
* BITS_PER_LONG
) {
148 size_t nlen
= (mc_groups_longs
+ 1) * sizeof(unsigned long);
150 if (mc_groups
== &mc_group_start
) {
151 new_groups
= kzalloc(nlen
, GFP_KERNEL
);
156 mc_groups
= new_groups
;
157 *mc_groups
= mc_group_start
;
159 new_groups
= krealloc(mc_groups
, nlen
, GFP_KERNEL
);
164 mc_groups
= new_groups
;
165 mc_groups
[mc_groups_longs
] = 0;
170 if (family
->netnsok
) {
173 netlink_table_grab();
175 for_each_net_rcu(net
) {
176 err
= __netlink_change_ngroups(net
->genl_sock
,
177 mc_groups_longs
* BITS_PER_LONG
);
180 * No need to roll back, can only fail if
181 * memory allocation fails and then the
182 * number of _possible_ groups has been
183 * increased on some sockets which is ok.
186 netlink_table_ungrab();
191 netlink_table_ungrab();
193 err
= netlink_change_ngroups(init_net
.genl_sock
,
194 mc_groups_longs
* BITS_PER_LONG
);
200 set_bit(id
, mc_groups
);
201 list_add_tail(&grp
->list
, &family
->mcast_groups
);
202 grp
->family
= family
;
204 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP
, grp
);
209 EXPORT_SYMBOL(genl_register_mc_group
);
211 static void __genl_unregister_mc_group(struct genl_family
*family
,
212 struct genl_multicast_group
*grp
)
215 BUG_ON(grp
->family
!= family
);
217 netlink_table_grab();
219 for_each_net_rcu(net
)
220 __netlink_clear_multicast_users(net
->genl_sock
, grp
->id
);
222 netlink_table_ungrab();
224 clear_bit(grp
->id
, mc_groups
);
225 list_del(&grp
->list
);
226 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP
, grp
);
232 * genl_unregister_mc_group - unregister a multicast group
234 * Unregisters the specified multicast group and notifies userspace
235 * about it. All current listeners on the group are removed.
237 * Note: It is not necessary to unregister all multicast groups before
238 * unregistering the family, unregistering the family will cause
239 * all assigned multicast groups to be unregistered automatically.
241 * @family: Generic netlink family the group belongs to.
242 * @grp: The group to unregister, must have been registered successfully
245 void genl_unregister_mc_group(struct genl_family
*family
,
246 struct genl_multicast_group
*grp
)
249 __genl_unregister_mc_group(family
, grp
);
252 EXPORT_SYMBOL(genl_unregister_mc_group
);
254 static void genl_unregister_mc_groups(struct genl_family
*family
)
256 struct genl_multicast_group
*grp
, *tmp
;
258 list_for_each_entry_safe(grp
, tmp
, &family
->mcast_groups
, list
)
259 __genl_unregister_mc_group(family
, grp
);
263 * genl_register_ops - register generic netlink operations
264 * @family: generic netlink family
265 * @ops: operations to be registered
267 * Registers the specified operations and assigns them to the specified
268 * family. Either a doit or dumpit callback must be specified or the
269 * operation will fail. Only one operation structure per command
270 * identifier may be registered.
272 * See include/net/genetlink.h for more documenation on the operations
275 * Returns 0 on success or a negative error code.
277 int genl_register_ops(struct genl_family
*family
, struct genl_ops
*ops
)
281 if (ops
->dumpit
== NULL
&& ops
->doit
== NULL
)
284 if (genl_get_cmd(ops
->cmd
, family
)) {
290 ops
->flags
|= GENL_CMD_CAP_DUMP
;
292 ops
->flags
|= GENL_CMD_CAP_DO
;
294 ops
->flags
|= GENL_CMD_CAP_HASPOL
;
297 list_add_tail(&ops
->ops_list
, &family
->ops_list
);
300 genl_ctrl_event(CTRL_CMD_NEWOPS
, ops
);
307 * genl_unregister_ops - unregister generic netlink operations
308 * @family: generic netlink family
309 * @ops: operations to be unregistered
311 * Unregisters the specified operations and unassigns them from the
312 * specified family. The operation blocks until the current message
313 * processing has finished and doesn't start again until the
314 * unregister process has finished.
316 * Note: It is not necessary to unregister all operations before
317 * unregistering the family, unregistering the family will cause
318 * all assigned operations to be unregistered automatically.
320 * Returns 0 on success or a negative error code.
322 int genl_unregister_ops(struct genl_family
*family
, struct genl_ops
*ops
)
327 list_for_each_entry(rc
, &family
->ops_list
, ops_list
) {
329 list_del(&ops
->ops_list
);
331 genl_ctrl_event(CTRL_CMD_DELOPS
, ops
);
341 * genl_register_family - register a generic netlink family
342 * @family: generic netlink family
344 * Registers the specified family after validating it first. Only one
345 * family may be registered with the same family name or identifier.
346 * The family id may equal GENL_ID_GENERATE causing an unique id to
347 * be automatically generated and assigned.
349 * Return 0 on success or a negative error code.
351 int genl_register_family(struct genl_family
*family
)
355 if (family
->id
&& family
->id
< GENL_MIN_ID
)
358 if (family
->id
> GENL_MAX_ID
)
361 INIT_LIST_HEAD(&family
->ops_list
);
362 INIT_LIST_HEAD(&family
->mcast_groups
);
366 if (genl_family_find_byname(family
->name
)) {
371 if (family
->id
== GENL_ID_GENERATE
) {
372 u16 newid
= genl_generate_id();
380 } else if (genl_family_find_byid(family
->id
)) {
385 if (family
->maxattr
) {
386 family
->attrbuf
= kmalloc((family
->maxattr
+1) *
387 sizeof(struct nlattr
*), GFP_KERNEL
);
388 if (family
->attrbuf
== NULL
) {
393 family
->attrbuf
= NULL
;
395 list_add_tail(&family
->family_list
, genl_family_chain(family
->id
));
398 genl_ctrl_event(CTRL_CMD_NEWFAMILY
, family
);
409 * genl_register_family_with_ops - register a generic netlink family
410 * @family: generic netlink family
411 * @ops: operations to be registered
412 * @n_ops: number of elements to register
414 * Registers the specified family and operations from the specified table.
415 * Only one family may be registered with the same family name or identifier.
417 * The family id may equal GENL_ID_GENERATE causing an unique id to
418 * be automatically generated and assigned.
420 * Either a doit or dumpit callback must be specified for every registered
421 * operation or the function will fail. Only one operation structure per
422 * command identifier may be registered.
424 * See include/net/genetlink.h for more documenation on the operations
427 * This is equivalent to calling genl_register_family() followed by
428 * genl_register_ops() for every operation entry in the table taking
429 * care to unregister the family on error path.
431 * Return 0 on success or a negative error code.
433 int genl_register_family_with_ops(struct genl_family
*family
,
434 struct genl_ops
*ops
, size_t n_ops
)
438 err
= genl_register_family(family
);
442 for (i
= 0; i
< n_ops
; ++i
, ++ops
) {
443 err
= genl_register_ops(family
, ops
);
449 genl_unregister_family(family
);
452 EXPORT_SYMBOL(genl_register_family_with_ops
);
455 * genl_unregister_family - unregister generic netlink family
456 * @family: generic netlink family
458 * Unregisters the specified family.
460 * Returns 0 on success or a negative error code.
462 int genl_unregister_family(struct genl_family
*family
)
464 struct genl_family
*rc
;
468 genl_unregister_mc_groups(family
);
470 list_for_each_entry(rc
, genl_family_chain(family
->id
), family_list
) {
471 if (family
->id
!= rc
->id
|| strcmp(rc
->name
, family
->name
))
474 list_del(&rc
->family_list
);
475 INIT_LIST_HEAD(&family
->ops_list
);
478 kfree(family
->attrbuf
);
479 genl_ctrl_event(CTRL_CMD_DELFAMILY
, family
);
488 static int genl_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
490 struct genl_ops
*ops
;
491 struct genl_family
*family
;
492 struct net
*net
= sock_net(skb
->sk
);
493 struct genl_info info
;
494 struct genlmsghdr
*hdr
= nlmsg_data(nlh
);
497 family
= genl_family_find_byid(nlh
->nlmsg_type
);
501 /* this family doesn't exist in this netns */
502 if (!family
->netnsok
&& !net_eq(net
, &init_net
))
505 hdrlen
= GENL_HDRLEN
+ family
->hdrsize
;
506 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
509 ops
= genl_get_cmd(hdr
->cmd
, family
);
513 if ((ops
->flags
& GENL_ADMIN_PERM
) &&
514 security_netlink_recv(skb
, CAP_NET_ADMIN
))
517 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
518 if (ops
->dumpit
== NULL
)
522 err
= netlink_dump_start(net
->genl_sock
, skb
, nlh
,
523 ops
->dumpit
, ops
->done
);
528 if (ops
->doit
== NULL
)
531 if (family
->attrbuf
) {
532 err
= nlmsg_parse(nlh
, hdrlen
, family
->attrbuf
, family
->maxattr
,
538 info
.snd_seq
= nlh
->nlmsg_seq
;
539 info
.snd_pid
= NETLINK_CB(skb
).pid
;
541 info
.genlhdr
= nlmsg_data(nlh
);
542 info
.userhdr
= nlmsg_data(nlh
) + GENL_HDRLEN
;
543 info
.attrs
= family
->attrbuf
;
544 genl_info_net_set(&info
, net
);
546 return ops
->doit(skb
, &info
);
549 static void genl_rcv(struct sk_buff
*skb
)
552 netlink_rcv_skb(skb
, &genl_rcv_msg
);
556 /**************************************************************************
558 **************************************************************************/
560 static struct genl_family genl_ctrl
= {
564 .maxattr
= CTRL_ATTR_MAX
,
568 static int ctrl_fill_info(struct genl_family
*family
, u32 pid
, u32 seq
,
569 u32 flags
, struct sk_buff
*skb
, u8 cmd
)
573 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
577 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, family
->name
);
578 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, family
->id
);
579 NLA_PUT_U32(skb
, CTRL_ATTR_VERSION
, family
->version
);
580 NLA_PUT_U32(skb
, CTRL_ATTR_HDRSIZE
, family
->hdrsize
);
581 NLA_PUT_U32(skb
, CTRL_ATTR_MAXATTR
, family
->maxattr
);
583 if (!list_empty(&family
->ops_list
)) {
584 struct nlattr
*nla_ops
;
585 struct genl_ops
*ops
;
588 nla_ops
= nla_nest_start(skb
, CTRL_ATTR_OPS
);
590 goto nla_put_failure
;
592 list_for_each_entry(ops
, &family
->ops_list
, ops_list
) {
595 nest
= nla_nest_start(skb
, idx
++);
597 goto nla_put_failure
;
599 NLA_PUT_U32(skb
, CTRL_ATTR_OP_ID
, ops
->cmd
);
600 NLA_PUT_U32(skb
, CTRL_ATTR_OP_FLAGS
, ops
->flags
);
602 nla_nest_end(skb
, nest
);
605 nla_nest_end(skb
, nla_ops
);
608 if (!list_empty(&family
->mcast_groups
)) {
609 struct genl_multicast_group
*grp
;
610 struct nlattr
*nla_grps
;
613 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
614 if (nla_grps
== NULL
)
615 goto nla_put_failure
;
617 list_for_each_entry(grp
, &family
->mcast_groups
, list
) {
620 nest
= nla_nest_start(skb
, idx
++);
622 goto nla_put_failure
;
624 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
625 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
628 nla_nest_end(skb
, nest
);
630 nla_nest_end(skb
, nla_grps
);
633 return genlmsg_end(skb
, hdr
);
636 genlmsg_cancel(skb
, hdr
);
640 static int ctrl_fill_mcgrp_info(struct genl_multicast_group
*grp
, u32 pid
,
641 u32 seq
, u32 flags
, struct sk_buff
*skb
,
645 struct nlattr
*nla_grps
;
648 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
652 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, grp
->family
->name
);
653 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, grp
->family
->id
);
655 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
656 if (nla_grps
== NULL
)
657 goto nla_put_failure
;
659 nest
= nla_nest_start(skb
, 1);
661 goto nla_put_failure
;
663 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
664 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
667 nla_nest_end(skb
, nest
);
668 nla_nest_end(skb
, nla_grps
);
670 return genlmsg_end(skb
, hdr
);
673 genlmsg_cancel(skb
, hdr
);
677 static int ctrl_dumpfamily(struct sk_buff
*skb
, struct netlink_callback
*cb
)
681 struct genl_family
*rt
;
682 struct net
*net
= sock_net(skb
->sk
);
683 int chains_to_skip
= cb
->args
[0];
684 int fams_to_skip
= cb
->args
[1];
686 for (i
= chains_to_skip
; i
< GENL_FAM_TAB_SIZE
; i
++) {
688 list_for_each_entry(rt
, genl_family_chain(i
), family_list
) {
689 if (!rt
->netnsok
&& !net_eq(net
, &init_net
))
691 if (++n
< fams_to_skip
)
693 if (ctrl_fill_info(rt
, NETLINK_CB(cb
->skb
).pid
,
694 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
695 skb
, CTRL_CMD_NEWFAMILY
) < 0)
709 static struct sk_buff
*ctrl_build_family_msg(struct genl_family
*family
,
710 u32 pid
, int seq
, u8 cmd
)
715 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
717 return ERR_PTR(-ENOBUFS
);
719 err
= ctrl_fill_info(family
, pid
, seq
, 0, skb
, cmd
);
728 static struct sk_buff
*ctrl_build_mcgrp_msg(struct genl_multicast_group
*grp
,
729 u32 pid
, int seq
, u8 cmd
)
734 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
736 return ERR_PTR(-ENOBUFS
);
738 err
= ctrl_fill_mcgrp_info(grp
, pid
, seq
, 0, skb
, cmd
);
747 static const struct nla_policy ctrl_policy
[CTRL_ATTR_MAX
+1] = {
748 [CTRL_ATTR_FAMILY_ID
] = { .type
= NLA_U16
},
749 [CTRL_ATTR_FAMILY_NAME
] = { .type
= NLA_NUL_STRING
,
750 .len
= GENL_NAMSIZ
- 1 },
753 static int ctrl_getfamily(struct sk_buff
*skb
, struct genl_info
*info
)
756 struct genl_family
*res
= NULL
;
759 if (info
->attrs
[CTRL_ATTR_FAMILY_ID
]) {
760 u16 id
= nla_get_u16(info
->attrs
[CTRL_ATTR_FAMILY_ID
]);
761 res
= genl_family_find_byid(id
);
765 if (info
->attrs
[CTRL_ATTR_FAMILY_NAME
]) {
768 name
= nla_data(info
->attrs
[CTRL_ATTR_FAMILY_NAME
]);
769 res
= genl_family_find_byname(name
);
776 if (!res
->netnsok
&& !net_eq(genl_info_net(info
), &init_net
)) {
777 /* family doesn't exist here */
781 msg
= ctrl_build_family_msg(res
, info
->snd_pid
, info
->snd_seq
,
786 return genlmsg_reply(msg
, info
);
789 static int genl_ctrl_event(int event
, void *data
)
792 struct genl_family
*family
;
793 struct genl_multicast_group
*grp
;
795 /* genl is still initialising */
796 if (!init_net
.genl_sock
)
800 case CTRL_CMD_NEWFAMILY
:
801 case CTRL_CMD_DELFAMILY
:
803 msg
= ctrl_build_family_msg(family
, 0, 0, event
);
805 case CTRL_CMD_NEWMCAST_GRP
:
806 case CTRL_CMD_DELMCAST_GRP
:
808 family
= grp
->family
;
809 msg
= ctrl_build_mcgrp_msg(data
, 0, 0, event
);
818 if (!family
->netnsok
) {
819 genlmsg_multicast_netns(&init_net
, msg
, 0,
820 GENL_ID_CTRL
, GFP_KERNEL
);
823 genlmsg_multicast_allns(msg
, 0, GENL_ID_CTRL
, GFP_ATOMIC
);
830 static struct genl_ops genl_ctrl_ops
= {
831 .cmd
= CTRL_CMD_GETFAMILY
,
832 .doit
= ctrl_getfamily
,
833 .dumpit
= ctrl_dumpfamily
,
834 .policy
= ctrl_policy
,
837 static struct genl_multicast_group notify_grp
= {
841 static int __net_init
genl_pernet_init(struct net
*net
)
843 /* we'll bump the group number right afterwards */
844 net
->genl_sock
= netlink_kernel_create(net
, NETLINK_GENERIC
, 0,
845 genl_rcv
, &genl_mutex
,
848 if (!net
->genl_sock
&& net_eq(net
, &init_net
))
849 panic("GENL: Cannot initialize generic netlink\n");
857 static void __net_exit
genl_pernet_exit(struct net
*net
)
859 netlink_kernel_release(net
->genl_sock
);
860 net
->genl_sock
= NULL
;
863 static struct pernet_operations genl_pernet_ops
= {
864 .init
= genl_pernet_init
,
865 .exit
= genl_pernet_exit
,
868 static int __init
genl_init(void)
872 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
873 INIT_LIST_HEAD(&family_ht
[i
]);
875 err
= genl_register_family(&genl_ctrl
);
879 err
= genl_register_ops(&genl_ctrl
, &genl_ctrl_ops
);
883 netlink_set_nonroot(NETLINK_GENERIC
, NL_NONROOT_RECV
);
885 err
= register_pernet_subsys(&genl_pernet_ops
);
889 err
= genl_register_mc_group(&genl_ctrl
, ¬ify_grp
);
896 panic("GENL: Cannot register controller: %d\n", err
);
899 subsys_initcall(genl_init
);
901 EXPORT_SYMBOL(genl_register_ops
);
902 EXPORT_SYMBOL(genl_unregister_ops
);
903 EXPORT_SYMBOL(genl_register_family
);
904 EXPORT_SYMBOL(genl_unregister_family
);
906 static int genlmsg_mcast(struct sk_buff
*skb
, u32 pid
, unsigned long group
,
910 struct net
*net
, *prev
= NULL
;
913 for_each_net_rcu(net
) {
915 tmp
= skb_clone(skb
, flags
);
920 err
= nlmsg_multicast(prev
->genl_sock
, tmp
,
929 return nlmsg_multicast(prev
->genl_sock
, skb
, pid
, group
, flags
);
935 int genlmsg_multicast_allns(struct sk_buff
*skb
, u32 pid
, unsigned int group
,
938 return genlmsg_mcast(skb
, pid
, group
, flags
);
940 EXPORT_SYMBOL(genlmsg_multicast_allns
);