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 void genl_lock(void)
27 mutex_lock(&genl_mutex
);
30 static void genl_unlock(void)
32 mutex_unlock(&genl_mutex
);
34 if (genl_sock
&& genl_sock
->sk_receive_queue
.qlen
)
35 genl_sock
->sk_data_ready(genl_sock
, 0);
38 #define GENL_FAM_TAB_SIZE 16
39 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
41 static struct list_head family_ht
[GENL_FAM_TAB_SIZE
];
43 * Bitmap of multicast groups that are currently in use.
45 * To avoid an allocation at boot of just one unsigned long,
46 * declare it global instead.
47 * Bit 0 is marked as already used since group 0 is invalid.
49 static unsigned long mc_group_start
= 0x1;
50 static unsigned long *mc_groups
= &mc_group_start
;
51 static unsigned long mc_groups_longs
= 1;
53 static int genl_ctrl_event(int event
, void *data
);
55 static inline unsigned int genl_family_hash(unsigned int id
)
57 return id
& GENL_FAM_TAB_MASK
;
60 static inline struct list_head
*genl_family_chain(unsigned int id
)
62 return &family_ht
[genl_family_hash(id
)];
65 static struct genl_family
*genl_family_find_byid(unsigned int id
)
67 struct genl_family
*f
;
69 list_for_each_entry(f
, genl_family_chain(id
), family_list
)
76 static struct genl_family
*genl_family_find_byname(char *name
)
78 struct genl_family
*f
;
81 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
82 list_for_each_entry(f
, genl_family_chain(i
), family_list
)
83 if (strcmp(f
->name
, name
) == 0)
89 static struct genl_ops
*genl_get_cmd(u8 cmd
, struct genl_family
*family
)
93 list_for_each_entry(ops
, &family
->ops_list
, ops_list
)
100 /* Of course we are going to have problems once we hit
101 * 2^16 alive types, but that can only happen by year 2K
103 static inline u16
genl_generate_id(void)
105 static u16 id_gen_idx
;
110 id_gen_idx
= GENL_MIN_ID
;
112 if (++id_gen_idx
> GENL_MAX_ID
) {
121 } while (genl_family_find_byid(id_gen_idx
));
126 static struct genl_multicast_group notify_grp
;
129 * genl_register_mc_group - register a multicast group
131 * Registers the specified multicast group and notifies userspace
132 * about the new group.
134 * Returns 0 on success or a negative error code.
136 * @family: The generic netlink family the group shall be registered for.
137 * @grp: The group to register, must have a name.
139 int genl_register_mc_group(struct genl_family
*family
,
140 struct genl_multicast_group
*grp
)
143 unsigned long *new_groups
;
146 BUG_ON(grp
->name
[0] == '\0');
150 /* special-case our own group */
151 if (grp
== ¬ify_grp
)
154 id
= find_first_zero_bit(mc_groups
,
155 mc_groups_longs
* BITS_PER_LONG
);
158 if (id
>= mc_groups_longs
* BITS_PER_LONG
) {
159 size_t nlen
= (mc_groups_longs
+ 1) * sizeof(unsigned long);
161 if (mc_groups
== &mc_group_start
) {
162 new_groups
= kzalloc(nlen
, GFP_KERNEL
);
167 mc_groups
= new_groups
;
168 *mc_groups
= mc_group_start
;
170 new_groups
= krealloc(mc_groups
, nlen
, GFP_KERNEL
);
175 mc_groups
= new_groups
;
176 mc_groups
[mc_groups_longs
] = 0;
181 err
= netlink_change_ngroups(genl_sock
,
182 mc_groups_longs
* BITS_PER_LONG
);
187 set_bit(id
, mc_groups
);
188 list_add_tail(&grp
->list
, &family
->mcast_groups
);
189 grp
->family
= family
;
191 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP
, grp
);
196 EXPORT_SYMBOL(genl_register_mc_group
);
198 static void __genl_unregister_mc_group(struct genl_family
*family
,
199 struct genl_multicast_group
*grp
)
201 BUG_ON(grp
->family
!= family
);
202 netlink_clear_multicast_users(genl_sock
, grp
->id
);
203 clear_bit(grp
->id
, mc_groups
);
204 list_del(&grp
->list
);
205 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP
, grp
);
211 * genl_unregister_mc_group - unregister a multicast group
213 * Unregisters the specified multicast group and notifies userspace
214 * about it. All current listeners on the group are removed.
216 * Note: It is not necessary to unregister all multicast groups before
217 * unregistering the family, unregistering the family will cause
218 * all assigned multicast groups to be unregistered automatically.
220 * @family: Generic netlink family the group belongs to.
221 * @grp: The group to unregister, must have been registered successfully
224 void genl_unregister_mc_group(struct genl_family
*family
,
225 struct genl_multicast_group
*grp
)
228 __genl_unregister_mc_group(family
, grp
);
231 EXPORT_SYMBOL(genl_unregister_mc_group
);
233 static void genl_unregister_mc_groups(struct genl_family
*family
)
235 struct genl_multicast_group
*grp
, *tmp
;
238 list_for_each_entry_safe(grp
, tmp
, &family
->mcast_groups
, list
)
239 __genl_unregister_mc_group(family
, grp
);
244 * genl_register_ops - register generic netlink operations
245 * @family: generic netlink family
246 * @ops: operations to be registered
248 * Registers the specified operations and assigns them to the specified
249 * family. Either a doit or dumpit callback must be specified or the
250 * operation will fail. Only one operation structure per command
251 * identifier may be registered.
253 * See include/net/genetlink.h for more documenation on the operations
256 * Returns 0 on success or a negative error code.
258 int genl_register_ops(struct genl_family
*family
, struct genl_ops
*ops
)
262 if (ops
->dumpit
== NULL
&& ops
->doit
== NULL
)
265 if (genl_get_cmd(ops
->cmd
, family
)) {
271 ops
->flags
|= GENL_CMD_CAP_DUMP
;
273 ops
->flags
|= GENL_CMD_CAP_DO
;
275 ops
->flags
|= GENL_CMD_CAP_HASPOL
;
278 list_add_tail(&ops
->ops_list
, &family
->ops_list
);
281 genl_ctrl_event(CTRL_CMD_NEWOPS
, ops
);
288 * genl_unregister_ops - unregister generic netlink operations
289 * @family: generic netlink family
290 * @ops: operations to be unregistered
292 * Unregisters the specified operations and unassigns them from the
293 * specified family. The operation blocks until the current message
294 * processing has finished and doesn't start again until the
295 * unregister process has finished.
297 * Note: It is not necessary to unregister all operations before
298 * unregistering the family, unregistering the family will cause
299 * all assigned operations to be unregistered automatically.
301 * Returns 0 on success or a negative error code.
303 int genl_unregister_ops(struct genl_family
*family
, struct genl_ops
*ops
)
308 list_for_each_entry(rc
, &family
->ops_list
, ops_list
) {
310 list_del(&ops
->ops_list
);
312 genl_ctrl_event(CTRL_CMD_DELOPS
, ops
);
322 * genl_register_family - register a generic netlink family
323 * @family: generic netlink family
325 * Registers the specified family after validating it first. Only one
326 * family may be registered with the same family name or identifier.
327 * The family id may equal GENL_ID_GENERATE causing an unique id to
328 * be automatically generated and assigned.
330 * Return 0 on success or a negative error code.
332 int genl_register_family(struct genl_family
*family
)
336 if (family
->id
&& family
->id
< GENL_MIN_ID
)
339 if (family
->id
> GENL_MAX_ID
)
342 INIT_LIST_HEAD(&family
->ops_list
);
343 INIT_LIST_HEAD(&family
->mcast_groups
);
347 if (genl_family_find_byname(family
->name
)) {
352 if (genl_family_find_byid(family
->id
)) {
357 if (family
->id
== GENL_ID_GENERATE
) {
358 u16 newid
= genl_generate_id();
368 if (family
->maxattr
) {
369 family
->attrbuf
= kmalloc((family
->maxattr
+1) *
370 sizeof(struct nlattr
*), GFP_KERNEL
);
371 if (family
->attrbuf
== NULL
) {
376 family
->attrbuf
= NULL
;
378 list_add_tail(&family
->family_list
, genl_family_chain(family
->id
));
381 genl_ctrl_event(CTRL_CMD_NEWFAMILY
, family
);
392 * genl_unregister_family - unregister generic netlink family
393 * @family: generic netlink family
395 * Unregisters the specified family.
397 * Returns 0 on success or a negative error code.
399 int genl_unregister_family(struct genl_family
*family
)
401 struct genl_family
*rc
;
403 genl_unregister_mc_groups(family
);
407 list_for_each_entry(rc
, genl_family_chain(family
->id
), family_list
) {
408 if (family
->id
!= rc
->id
|| strcmp(rc
->name
, family
->name
))
411 list_del(&rc
->family_list
);
412 INIT_LIST_HEAD(&family
->ops_list
);
415 kfree(family
->attrbuf
);
416 genl_ctrl_event(CTRL_CMD_DELFAMILY
, family
);
425 static int genl_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
427 struct genl_ops
*ops
;
428 struct genl_family
*family
;
429 struct genl_info info
;
430 struct genlmsghdr
*hdr
= nlmsg_data(nlh
);
433 family
= genl_family_find_byid(nlh
->nlmsg_type
);
437 hdrlen
= GENL_HDRLEN
+ family
->hdrsize
;
438 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
441 ops
= genl_get_cmd(hdr
->cmd
, family
);
445 if ((ops
->flags
& GENL_ADMIN_PERM
) &&
446 security_netlink_recv(skb
, CAP_NET_ADMIN
))
449 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
450 if (ops
->dumpit
== NULL
)
453 return netlink_dump_start(genl_sock
, skb
, nlh
,
454 ops
->dumpit
, ops
->done
);
457 if (ops
->doit
== NULL
)
460 if (family
->attrbuf
) {
461 err
= nlmsg_parse(nlh
, hdrlen
, family
->attrbuf
, family
->maxattr
,
467 info
.snd_seq
= nlh
->nlmsg_seq
;
468 info
.snd_pid
= NETLINK_CB(skb
).pid
;
470 info
.genlhdr
= nlmsg_data(nlh
);
471 info
.userhdr
= nlmsg_data(nlh
) + GENL_HDRLEN
;
472 info
.attrs
= family
->attrbuf
;
474 return ops
->doit(skb
, &info
);
477 static void genl_rcv(struct sk_buff
*skb
)
480 netlink_rcv_skb(skb
, &genl_rcv_msg
);
484 /**************************************************************************
486 **************************************************************************/
488 static struct genl_family genl_ctrl
= {
492 .maxattr
= CTRL_ATTR_MAX
,
495 static int ctrl_fill_info(struct genl_family
*family
, u32 pid
, u32 seq
,
496 u32 flags
, struct sk_buff
*skb
, u8 cmd
)
500 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
504 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, family
->name
);
505 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, family
->id
);
506 NLA_PUT_U32(skb
, CTRL_ATTR_VERSION
, family
->version
);
507 NLA_PUT_U32(skb
, CTRL_ATTR_HDRSIZE
, family
->hdrsize
);
508 NLA_PUT_U32(skb
, CTRL_ATTR_MAXATTR
, family
->maxattr
);
510 if (!list_empty(&family
->ops_list
)) {
511 struct nlattr
*nla_ops
;
512 struct genl_ops
*ops
;
515 nla_ops
= nla_nest_start(skb
, CTRL_ATTR_OPS
);
517 goto nla_put_failure
;
519 list_for_each_entry(ops
, &family
->ops_list
, ops_list
) {
522 nest
= nla_nest_start(skb
, idx
++);
524 goto nla_put_failure
;
526 NLA_PUT_U32(skb
, CTRL_ATTR_OP_ID
, ops
->cmd
);
527 NLA_PUT_U32(skb
, CTRL_ATTR_OP_FLAGS
, ops
->flags
);
529 nla_nest_end(skb
, nest
);
532 nla_nest_end(skb
, nla_ops
);
535 if (!list_empty(&family
->mcast_groups
)) {
536 struct genl_multicast_group
*grp
;
537 struct nlattr
*nla_grps
;
540 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
541 if (nla_grps
== NULL
)
542 goto nla_put_failure
;
544 list_for_each_entry(grp
, &family
->mcast_groups
, list
) {
547 nest
= nla_nest_start(skb
, idx
++);
549 goto nla_put_failure
;
551 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
552 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
555 nla_nest_end(skb
, nest
);
557 nla_nest_end(skb
, nla_grps
);
560 return genlmsg_end(skb
, hdr
);
563 return genlmsg_cancel(skb
, hdr
);
566 static int ctrl_fill_mcgrp_info(struct genl_multicast_group
*grp
, u32 pid
,
567 u32 seq
, u32 flags
, struct sk_buff
*skb
,
571 struct nlattr
*nla_grps
;
574 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
578 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, grp
->family
->name
);
579 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, grp
->family
->id
);
581 nla_grps
= nla_nest_start(skb
, CTRL_ATTR_MCAST_GROUPS
);
582 if (nla_grps
== NULL
)
583 goto nla_put_failure
;
585 nest
= nla_nest_start(skb
, 1);
587 goto nla_put_failure
;
589 NLA_PUT_U32(skb
, CTRL_ATTR_MCAST_GRP_ID
, grp
->id
);
590 NLA_PUT_STRING(skb
, CTRL_ATTR_MCAST_GRP_NAME
,
593 nla_nest_end(skb
, nest
);
594 nla_nest_end(skb
, nla_grps
);
596 return genlmsg_end(skb
, hdr
);
599 return genlmsg_cancel(skb
, hdr
);
602 static int ctrl_dumpfamily(struct sk_buff
*skb
, struct netlink_callback
*cb
)
606 struct genl_family
*rt
;
607 int chains_to_skip
= cb
->args
[0];
608 int fams_to_skip
= cb
->args
[1];
610 if (chains_to_skip
!= 0)
613 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++) {
614 if (i
< chains_to_skip
)
617 list_for_each_entry(rt
, genl_family_chain(i
), family_list
) {
618 if (++n
< fams_to_skip
)
620 if (ctrl_fill_info(rt
, NETLINK_CB(cb
->skb
).pid
,
621 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
622 skb
, CTRL_CMD_NEWFAMILY
) < 0)
630 if (chains_to_skip
!= 0)
639 static struct sk_buff
*ctrl_build_family_msg(struct genl_family
*family
,
640 u32 pid
, int seq
, u8 cmd
)
645 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
647 return ERR_PTR(-ENOBUFS
);
649 err
= ctrl_fill_info(family
, pid
, seq
, 0, skb
, cmd
);
658 static struct sk_buff
*ctrl_build_mcgrp_msg(struct genl_multicast_group
*grp
,
659 u32 pid
, int seq
, u8 cmd
)
664 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
666 return ERR_PTR(-ENOBUFS
);
668 err
= ctrl_fill_mcgrp_info(grp
, pid
, seq
, 0, skb
, cmd
);
677 static const struct nla_policy ctrl_policy
[CTRL_ATTR_MAX
+1] = {
678 [CTRL_ATTR_FAMILY_ID
] = { .type
= NLA_U16
},
679 [CTRL_ATTR_FAMILY_NAME
] = { .type
= NLA_NUL_STRING
,
680 .len
= GENL_NAMSIZ
- 1 },
683 static int ctrl_getfamily(struct sk_buff
*skb
, struct genl_info
*info
)
686 struct genl_family
*res
= NULL
;
689 if (info
->attrs
[CTRL_ATTR_FAMILY_ID
]) {
690 u16 id
= nla_get_u16(info
->attrs
[CTRL_ATTR_FAMILY_ID
]);
691 res
= genl_family_find_byid(id
);
694 if (info
->attrs
[CTRL_ATTR_FAMILY_NAME
]) {
697 name
= nla_data(info
->attrs
[CTRL_ATTR_FAMILY_NAME
]);
698 res
= genl_family_find_byname(name
);
706 msg
= ctrl_build_family_msg(res
, info
->snd_pid
, info
->snd_seq
,
713 err
= genlmsg_reply(msg
, info
);
718 static int genl_ctrl_event(int event
, void *data
)
722 if (genl_sock
== NULL
)
726 case CTRL_CMD_NEWFAMILY
:
727 case CTRL_CMD_DELFAMILY
:
728 msg
= ctrl_build_family_msg(data
, 0, 0, event
);
732 genlmsg_multicast(msg
, 0, GENL_ID_CTRL
, GFP_KERNEL
);
734 case CTRL_CMD_NEWMCAST_GRP
:
735 case CTRL_CMD_DELMCAST_GRP
:
736 msg
= ctrl_build_mcgrp_msg(data
, 0, 0, event
);
740 genlmsg_multicast(msg
, 0, GENL_ID_CTRL
, GFP_KERNEL
);
747 static struct genl_ops genl_ctrl_ops
= {
748 .cmd
= CTRL_CMD_GETFAMILY
,
749 .doit
= ctrl_getfamily
,
750 .dumpit
= ctrl_dumpfamily
,
751 .policy
= ctrl_policy
,
754 static struct genl_multicast_group notify_grp
= {
758 static int __init
genl_init(void)
762 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
763 INIT_LIST_HEAD(&family_ht
[i
]);
765 err
= genl_register_family(&genl_ctrl
);
769 err
= genl_register_ops(&genl_ctrl
, &genl_ctrl_ops
);
771 goto errout_register
;
773 netlink_set_nonroot(NETLINK_GENERIC
, NL_NONROOT_RECV
);
775 /* we'll bump the group number right afterwards */
776 genl_sock
= netlink_kernel_create(NETLINK_GENERIC
, 0, genl_rcv
,
778 if (genl_sock
== NULL
)
779 panic("GENL: Cannot initialize generic netlink\n");
781 err
= genl_register_mc_group(&genl_ctrl
, ¬ify_grp
);
783 goto errout_register
;
788 genl_unregister_family(&genl_ctrl
);
790 panic("GENL: Cannot register controller: %d\n", err
);
793 subsys_initcall(genl_init
);
795 EXPORT_SYMBOL(genl_sock
);
796 EXPORT_SYMBOL(genl_register_ops
);
797 EXPORT_SYMBOL(genl_unregister_ops
);
798 EXPORT_SYMBOL(genl_register_family
);
799 EXPORT_SYMBOL(genl_unregister_family
);