2 * NETLINK Generic Netlink Family
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/types.h>
12 #include <linux/socket.h>
13 #include <linux/string.h>
14 #include <linux/skbuff.h>
15 #include <linux/mutex.h>
17 #include <net/genetlink.h>
19 struct sock
*genl_sock
= NULL
;
21 static DEFINE_MUTEX(genl_mutex
); /* serialization of message processing */
23 static void genl_lock(void)
25 mutex_lock(&genl_mutex
);
28 static int genl_trylock(void)
30 return !mutex_trylock(&genl_mutex
);
33 static void genl_unlock(void)
35 mutex_unlock(&genl_mutex
);
37 if (genl_sock
&& genl_sock
->sk_receive_queue
.qlen
)
38 genl_sock
->sk_data_ready(genl_sock
, 0);
41 #define GENL_FAM_TAB_SIZE 16
42 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
44 static struct list_head family_ht
[GENL_FAM_TAB_SIZE
];
46 static int genl_ctrl_event(int event
, void *data
);
48 static inline unsigned int genl_family_hash(unsigned int id
)
50 return id
& GENL_FAM_TAB_MASK
;
53 static inline struct list_head
*genl_family_chain(unsigned int id
)
55 return &family_ht
[genl_family_hash(id
)];
58 static struct genl_family
*genl_family_find_byid(unsigned int id
)
60 struct genl_family
*f
;
62 list_for_each_entry(f
, genl_family_chain(id
), family_list
)
69 static struct genl_family
*genl_family_find_byname(char *name
)
71 struct genl_family
*f
;
74 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
75 list_for_each_entry(f
, genl_family_chain(i
), family_list
)
76 if (strcmp(f
->name
, name
) == 0)
82 static struct genl_ops
*genl_get_cmd(u8 cmd
, struct genl_family
*family
)
86 list_for_each_entry(ops
, &family
->ops_list
, ops_list
)
93 /* Of course we are going to have problems once we hit
94 * 2^16 alive types, but that can only happen by year 2K
96 static inline u16
genl_generate_id(void)
98 static u16 id_gen_idx
;
103 id_gen_idx
= GENL_MIN_ID
;
105 if (++id_gen_idx
> GENL_MAX_ID
) {
114 } while (genl_family_find_byid(id_gen_idx
));
120 * genl_register_ops - register generic netlink operations
121 * @family: generic netlink family
122 * @ops: operations to be registered
124 * Registers the specified operations and assigns them to the specified
125 * family. Either a doit or dumpit callback must be specified or the
126 * operation will fail. Only one operation structure per command
127 * identifier may be registered.
129 * See include/net/genetlink.h for more documenation on the operations
132 * Returns 0 on success or a negative error code.
134 int genl_register_ops(struct genl_family
*family
, struct genl_ops
*ops
)
138 if (ops
->dumpit
== NULL
&& ops
->doit
== NULL
)
141 if (genl_get_cmd(ops
->cmd
, family
)) {
147 ops
->flags
|= GENL_CMD_CAP_DUMP
;
149 ops
->flags
|= GENL_CMD_CAP_DO
;
151 ops
->flags
|= GENL_CMD_CAP_HASPOL
;
154 list_add_tail(&ops
->ops_list
, &family
->ops_list
);
157 genl_ctrl_event(CTRL_CMD_NEWOPS
, ops
);
164 * genl_unregister_ops - unregister generic netlink operations
165 * @family: generic netlink family
166 * @ops: operations to be unregistered
168 * Unregisters the specified operations and unassigns them from the
169 * specified family. The operation blocks until the current message
170 * processing has finished and doesn't start again until the
171 * unregister process has finished.
173 * Note: It is not necessary to unregister all operations before
174 * unregistering the family, unregistering the family will cause
175 * all assigned operations to be unregistered automatically.
177 * Returns 0 on success or a negative error code.
179 int genl_unregister_ops(struct genl_family
*family
, struct genl_ops
*ops
)
184 list_for_each_entry(rc
, &family
->ops_list
, ops_list
) {
186 list_del(&ops
->ops_list
);
188 genl_ctrl_event(CTRL_CMD_DELOPS
, ops
);
198 * genl_register_family - register a generic netlink family
199 * @family: generic netlink family
201 * Registers the specified family after validating it first. Only one
202 * family may be registered with the same family name or identifier.
203 * The family id may equal GENL_ID_GENERATE causing an unique id to
204 * be automatically generated and assigned.
206 * Return 0 on success or a negative error code.
208 int genl_register_family(struct genl_family
*family
)
212 if (family
->id
&& family
->id
< GENL_MIN_ID
)
215 if (family
->id
> GENL_MAX_ID
)
218 INIT_LIST_HEAD(&family
->ops_list
);
222 if (genl_family_find_byname(family
->name
)) {
227 if (genl_family_find_byid(family
->id
)) {
232 if (family
->id
== GENL_ID_GENERATE
) {
233 u16 newid
= genl_generate_id();
243 if (family
->maxattr
) {
244 family
->attrbuf
= kmalloc((family
->maxattr
+1) *
245 sizeof(struct nlattr
*), GFP_KERNEL
);
246 if (family
->attrbuf
== NULL
) {
251 family
->attrbuf
= NULL
;
253 list_add_tail(&family
->family_list
, genl_family_chain(family
->id
));
256 genl_ctrl_event(CTRL_CMD_NEWFAMILY
, family
);
267 * genl_unregister_family - unregister generic netlink family
268 * @family: generic netlink family
270 * Unregisters the specified family.
272 * Returns 0 on success or a negative error code.
274 int genl_unregister_family(struct genl_family
*family
)
276 struct genl_family
*rc
;
280 list_for_each_entry(rc
, genl_family_chain(family
->id
), family_list
) {
281 if (family
->id
!= rc
->id
|| strcmp(rc
->name
, family
->name
))
284 list_del(&rc
->family_list
);
285 INIT_LIST_HEAD(&family
->ops_list
);
288 kfree(family
->attrbuf
);
289 genl_ctrl_event(CTRL_CMD_DELFAMILY
, family
);
298 static int genl_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
301 struct genl_ops
*ops
;
302 struct genl_family
*family
;
303 struct genl_info info
;
304 struct genlmsghdr
*hdr
= nlmsg_data(nlh
);
305 int hdrlen
, err
= -EINVAL
;
307 if (!(nlh
->nlmsg_flags
& NLM_F_REQUEST
))
310 if (nlh
->nlmsg_type
< NLMSG_MIN_TYPE
)
313 family
= genl_family_find_byid(nlh
->nlmsg_type
);
314 if (family
== NULL
) {
319 hdrlen
= GENL_HDRLEN
+ family
->hdrsize
;
320 if (nlh
->nlmsg_len
< nlmsg_msg_size(hdrlen
))
323 ops
= genl_get_cmd(hdr
->cmd
, family
);
329 if ((ops
->flags
& GENL_ADMIN_PERM
) && security_netlink_recv(skb
, CAP_NET_ADMIN
)) {
334 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
335 if (ops
->dumpit
== NULL
) {
340 *errp
= err
= netlink_dump_start(genl_sock
, skb
, nlh
,
341 ops
->dumpit
, ops
->done
);
343 skb_pull(skb
, min(NLMSG_ALIGN(nlh
->nlmsg_len
),
348 if (ops
->doit
== NULL
) {
353 if (family
->attrbuf
) {
354 err
= nlmsg_parse(nlh
, hdrlen
, family
->attrbuf
, family
->maxattr
,
360 info
.snd_seq
= nlh
->nlmsg_seq
;
361 info
.snd_pid
= NETLINK_CB(skb
).pid
;
363 info
.genlhdr
= nlmsg_data(nlh
);
364 info
.userhdr
= nlmsg_data(nlh
) + GENL_HDRLEN
;
365 info
.attrs
= family
->attrbuf
;
367 *errp
= err
= ops
->doit(skb
, &info
);
378 static void genl_rcv(struct sock
*sk
, int len
)
380 unsigned int qlen
= 0;
385 netlink_run_queue(sk
, &qlen
, genl_rcv_msg
);
387 } while (qlen
&& genl_sock
&& genl_sock
->sk_receive_queue
.qlen
);
390 /**************************************************************************
392 **************************************************************************/
394 static struct genl_family genl_ctrl
= {
398 .maxattr
= CTRL_ATTR_MAX
,
401 static int ctrl_fill_info(struct genl_family
*family
, u32 pid
, u32 seq
,
402 u32 flags
, struct sk_buff
*skb
, u8 cmd
)
406 hdr
= genlmsg_put(skb
, pid
, seq
, &genl_ctrl
, flags
, cmd
);
410 NLA_PUT_STRING(skb
, CTRL_ATTR_FAMILY_NAME
, family
->name
);
411 NLA_PUT_U16(skb
, CTRL_ATTR_FAMILY_ID
, family
->id
);
412 NLA_PUT_U32(skb
, CTRL_ATTR_VERSION
, family
->version
);
413 NLA_PUT_U32(skb
, CTRL_ATTR_HDRSIZE
, family
->hdrsize
);
414 NLA_PUT_U32(skb
, CTRL_ATTR_MAXATTR
, family
->maxattr
);
416 if (!list_empty(&family
->ops_list
)) {
417 struct nlattr
*nla_ops
;
418 struct genl_ops
*ops
;
421 nla_ops
= nla_nest_start(skb
, CTRL_ATTR_OPS
);
423 goto nla_put_failure
;
425 list_for_each_entry(ops
, &family
->ops_list
, ops_list
) {
428 nest
= nla_nest_start(skb
, idx
++);
430 goto nla_put_failure
;
432 NLA_PUT_U32(skb
, CTRL_ATTR_OP_ID
, ops
->cmd
);
433 NLA_PUT_U32(skb
, CTRL_ATTR_OP_FLAGS
, ops
->flags
);
435 nla_nest_end(skb
, nest
);
438 nla_nest_end(skb
, nla_ops
);
441 return genlmsg_end(skb
, hdr
);
444 return genlmsg_cancel(skb
, hdr
);
447 static int ctrl_dumpfamily(struct sk_buff
*skb
, struct netlink_callback
*cb
)
451 struct genl_family
*rt
;
452 int chains_to_skip
= cb
->args
[0];
453 int fams_to_skip
= cb
->args
[1];
455 if (chains_to_skip
!= 0)
458 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++) {
459 if (i
< chains_to_skip
)
462 list_for_each_entry(rt
, genl_family_chain(i
), family_list
) {
463 if (++n
< fams_to_skip
)
465 if (ctrl_fill_info(rt
, NETLINK_CB(cb
->skb
).pid
,
466 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
467 skb
, CTRL_CMD_NEWFAMILY
) < 0)
475 if (chains_to_skip
!= 0)
484 static struct sk_buff
*ctrl_build_msg(struct genl_family
*family
, u32 pid
,
490 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
492 return ERR_PTR(-ENOBUFS
);
494 err
= ctrl_fill_info(family
, pid
, seq
, 0, skb
, cmd
);
503 static struct nla_policy ctrl_policy
[CTRL_ATTR_MAX
+1] __read_mostly
= {
504 [CTRL_ATTR_FAMILY_ID
] = { .type
= NLA_U16
},
505 [CTRL_ATTR_FAMILY_NAME
] = { .type
= NLA_NUL_STRING
,
506 .len
= GENL_NAMSIZ
- 1 },
509 static int ctrl_getfamily(struct sk_buff
*skb
, struct genl_info
*info
)
512 struct genl_family
*res
= NULL
;
515 if (info
->attrs
[CTRL_ATTR_FAMILY_ID
]) {
516 u16 id
= nla_get_u16(info
->attrs
[CTRL_ATTR_FAMILY_ID
]);
517 res
= genl_family_find_byid(id
);
520 if (info
->attrs
[CTRL_ATTR_FAMILY_NAME
]) {
523 name
= nla_data(info
->attrs
[CTRL_ATTR_FAMILY_NAME
]);
524 res
= genl_family_find_byname(name
);
532 msg
= ctrl_build_msg(res
, info
->snd_pid
, info
->snd_seq
,
539 err
= genlmsg_reply(msg
, info
);
544 static int genl_ctrl_event(int event
, void *data
)
548 if (genl_sock
== NULL
)
552 case CTRL_CMD_NEWFAMILY
:
553 case CTRL_CMD_DELFAMILY
:
554 msg
= ctrl_build_msg(data
, 0, 0, event
);
558 genlmsg_multicast(msg
, 0, GENL_ID_CTRL
, GFP_KERNEL
);
565 static struct genl_ops genl_ctrl_ops
= {
566 .cmd
= CTRL_CMD_GETFAMILY
,
567 .doit
= ctrl_getfamily
,
568 .dumpit
= ctrl_dumpfamily
,
569 .policy
= ctrl_policy
,
572 static int __init
genl_init(void)
576 for (i
= 0; i
< GENL_FAM_TAB_SIZE
; i
++)
577 INIT_LIST_HEAD(&family_ht
[i
]);
579 err
= genl_register_family(&genl_ctrl
);
583 err
= genl_register_ops(&genl_ctrl
, &genl_ctrl_ops
);
585 goto errout_register
;
587 netlink_set_nonroot(NETLINK_GENERIC
, NL_NONROOT_RECV
);
588 genl_sock
= netlink_kernel_create(NETLINK_GENERIC
, GENL_MAX_ID
,
589 genl_rcv
, THIS_MODULE
);
590 if (genl_sock
== NULL
)
591 panic("GENL: Cannot initialize generic netlink\n");
596 genl_unregister_family(&genl_ctrl
);
598 panic("GENL: Cannot register controller: %d\n", err
);
601 subsys_initcall(genl_init
);
603 EXPORT_SYMBOL(genl_sock
);
604 EXPORT_SYMBOL(genl_register_ops
);
605 EXPORT_SYMBOL(genl_unregister_ops
);
606 EXPORT_SYMBOL(genl_register_family
);
607 EXPORT_SYMBOL(genl_unregister_family
);