drm: detypedef the hashtab and more of sman
[linux-2.6/mini2440.git] / net / netlink / genetlink.c
blobb9ab62f938d07111f2b7489ad9f5ca9d31dbb746
1 /*
2 * NETLINK Generic Netlink Family
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
6 */
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>
16 #include <net/sock.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)
63 if (f->id == id)
64 return f;
66 return NULL;
69 static struct genl_family *genl_family_find_byname(char *name)
71 struct genl_family *f;
72 int i;
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)
77 return f;
79 return NULL;
82 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
84 struct genl_ops *ops;
86 list_for_each_entry(ops, &family->ops_list, ops_list)
87 if (ops->cmd == cmd)
88 return ops;
90 return NULL;
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;
99 int overflowed = 0;
101 do {
102 if (id_gen_idx == 0)
103 id_gen_idx = GENL_MIN_ID;
105 if (++id_gen_idx > GENL_MAX_ID) {
106 if (!overflowed) {
107 overflowed = 1;
108 id_gen_idx = 0;
109 continue;
110 } else
111 return 0;
114 } while (genl_family_find_byid(id_gen_idx));
116 return 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
130 * structure.
132 * Returns 0 on success or a negative error code.
134 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
136 int err = -EINVAL;
138 if (ops->dumpit == NULL && ops->doit == NULL)
139 goto errout;
141 if (genl_get_cmd(ops->cmd, family)) {
142 err = -EEXIST;
143 goto errout;
146 if (ops->dumpit)
147 ops->flags |= GENL_CMD_CAP_DUMP;
148 if (ops->doit)
149 ops->flags |= GENL_CMD_CAP_DO;
150 if (ops->policy)
151 ops->flags |= GENL_CMD_CAP_HASPOL;
153 genl_lock();
154 list_add_tail(&ops->ops_list, &family->ops_list);
155 genl_unlock();
157 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
158 err = 0;
159 errout:
160 return err;
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)
181 struct genl_ops *rc;
183 genl_lock();
184 list_for_each_entry(rc, &family->ops_list, ops_list) {
185 if (rc == ops) {
186 list_del(&ops->ops_list);
187 genl_unlock();
188 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
189 return 0;
192 genl_unlock();
194 return -ENOENT;
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)
210 int err = -EINVAL;
212 if (family->id && family->id < GENL_MIN_ID)
213 goto errout;
215 if (family->id > GENL_MAX_ID)
216 goto errout;
218 INIT_LIST_HEAD(&family->ops_list);
220 genl_lock();
222 if (genl_family_find_byname(family->name)) {
223 err = -EEXIST;
224 goto errout_locked;
227 if (genl_family_find_byid(family->id)) {
228 err = -EEXIST;
229 goto errout_locked;
232 if (family->id == GENL_ID_GENERATE) {
233 u16 newid = genl_generate_id();
235 if (!newid) {
236 err = -ENOMEM;
237 goto errout_locked;
240 family->id = newid;
243 if (family->maxattr) {
244 family->attrbuf = kmalloc((family->maxattr+1) *
245 sizeof(struct nlattr *), GFP_KERNEL);
246 if (family->attrbuf == NULL) {
247 err = -ENOMEM;
248 goto errout_locked;
250 } else
251 family->attrbuf = NULL;
253 list_add_tail(&family->family_list, genl_family_chain(family->id));
254 genl_unlock();
256 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
258 return 0;
260 errout_locked:
261 genl_unlock();
262 errout:
263 return err;
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;
278 genl_lock();
280 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
281 if (family->id != rc->id || strcmp(rc->name, family->name))
282 continue;
284 list_del(&rc->family_list);
285 INIT_LIST_HEAD(&family->ops_list);
286 genl_unlock();
288 kfree(family->attrbuf);
289 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
290 return 0;
293 genl_unlock();
295 return -ENOENT;
298 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
300 struct genl_ops *ops;
301 struct genl_family *family;
302 struct genl_info info;
303 struct genlmsghdr *hdr = nlmsg_data(nlh);
304 int hdrlen, err;
306 family = genl_family_find_byid(nlh->nlmsg_type);
307 if (family == NULL)
308 return -ENOENT;
310 hdrlen = GENL_HDRLEN + family->hdrsize;
311 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
312 return -EINVAL;
314 ops = genl_get_cmd(hdr->cmd, family);
315 if (ops == NULL)
316 return -EOPNOTSUPP;
318 if ((ops->flags & GENL_ADMIN_PERM) &&
319 security_netlink_recv(skb, CAP_NET_ADMIN))
320 return -EPERM;
322 if (nlh->nlmsg_flags & NLM_F_DUMP) {
323 if (ops->dumpit == NULL)
324 return -EOPNOTSUPP;
326 return netlink_dump_start(genl_sock, skb, nlh,
327 ops->dumpit, ops->done);
330 if (ops->doit == NULL)
331 return -EOPNOTSUPP;
333 if (family->attrbuf) {
334 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
335 ops->policy);
336 if (err < 0)
337 return err;
340 info.snd_seq = nlh->nlmsg_seq;
341 info.snd_pid = NETLINK_CB(skb).pid;
342 info.nlhdr = nlh;
343 info.genlhdr = nlmsg_data(nlh);
344 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
345 info.attrs = family->attrbuf;
347 return ops->doit(skb, &info);
350 static void genl_rcv(struct sock *sk, int len)
352 unsigned int qlen = 0;
354 do {
355 if (genl_trylock())
356 return;
357 netlink_run_queue(sk, &qlen, genl_rcv_msg);
358 genl_unlock();
359 } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
362 /**************************************************************************
363 * Controller
364 **************************************************************************/
366 static struct genl_family genl_ctrl = {
367 .id = GENL_ID_CTRL,
368 .name = "nlctrl",
369 .version = 0x2,
370 .maxattr = CTRL_ATTR_MAX,
373 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
374 u32 flags, struct sk_buff *skb, u8 cmd)
376 void *hdr;
378 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
379 if (hdr == NULL)
380 return -1;
382 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
383 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
384 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
385 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
386 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
388 if (!list_empty(&family->ops_list)) {
389 struct nlattr *nla_ops;
390 struct genl_ops *ops;
391 int idx = 1;
393 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
394 if (nla_ops == NULL)
395 goto nla_put_failure;
397 list_for_each_entry(ops, &family->ops_list, ops_list) {
398 struct nlattr *nest;
400 nest = nla_nest_start(skb, idx++);
401 if (nest == NULL)
402 goto nla_put_failure;
404 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
405 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
407 nla_nest_end(skb, nest);
410 nla_nest_end(skb, nla_ops);
413 return genlmsg_end(skb, hdr);
415 nla_put_failure:
416 return genlmsg_cancel(skb, hdr);
419 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
422 int i, n = 0;
423 struct genl_family *rt;
424 int chains_to_skip = cb->args[0];
425 int fams_to_skip = cb->args[1];
427 if (chains_to_skip != 0)
428 genl_lock();
430 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
431 if (i < chains_to_skip)
432 continue;
433 n = 0;
434 list_for_each_entry(rt, genl_family_chain(i), family_list) {
435 if (++n < fams_to_skip)
436 continue;
437 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
438 cb->nlh->nlmsg_seq, NLM_F_MULTI,
439 skb, CTRL_CMD_NEWFAMILY) < 0)
440 goto errout;
443 fams_to_skip = 0;
446 errout:
447 if (chains_to_skip != 0)
448 genl_unlock();
450 cb->args[0] = i;
451 cb->args[1] = n;
453 return skb->len;
456 static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
457 int seq, u8 cmd)
459 struct sk_buff *skb;
460 int err;
462 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
463 if (skb == NULL)
464 return ERR_PTR(-ENOBUFS);
466 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
467 if (err < 0) {
468 nlmsg_free(skb);
469 return ERR_PTR(err);
472 return skb;
475 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
476 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
477 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
478 .len = GENL_NAMSIZ - 1 },
481 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
483 struct sk_buff *msg;
484 struct genl_family *res = NULL;
485 int err = -EINVAL;
487 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
488 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
489 res = genl_family_find_byid(id);
492 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
493 char *name;
495 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
496 res = genl_family_find_byname(name);
499 if (res == NULL) {
500 err = -ENOENT;
501 goto errout;
504 msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
505 CTRL_CMD_NEWFAMILY);
506 if (IS_ERR(msg)) {
507 err = PTR_ERR(msg);
508 goto errout;
511 err = genlmsg_reply(msg, info);
512 errout:
513 return err;
516 static int genl_ctrl_event(int event, void *data)
518 struct sk_buff *msg;
520 if (genl_sock == NULL)
521 return 0;
523 switch (event) {
524 case CTRL_CMD_NEWFAMILY:
525 case CTRL_CMD_DELFAMILY:
526 msg = ctrl_build_msg(data, 0, 0, event);
527 if (IS_ERR(msg))
528 return PTR_ERR(msg);
530 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
531 break;
534 return 0;
537 static struct genl_ops genl_ctrl_ops = {
538 .cmd = CTRL_CMD_GETFAMILY,
539 .doit = ctrl_getfamily,
540 .dumpit = ctrl_dumpfamily,
541 .policy = ctrl_policy,
544 static int __init genl_init(void)
546 int i, err;
548 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
549 INIT_LIST_HEAD(&family_ht[i]);
551 err = genl_register_family(&genl_ctrl);
552 if (err < 0)
553 goto errout;
555 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
556 if (err < 0)
557 goto errout_register;
559 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
560 genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
561 genl_rcv, NULL, THIS_MODULE);
562 if (genl_sock == NULL)
563 panic("GENL: Cannot initialize generic netlink\n");
565 return 0;
567 errout_register:
568 genl_unregister_family(&genl_ctrl);
569 errout:
570 panic("GENL: Cannot register controller: %d\n", err);
573 subsys_initcall(genl_init);
575 EXPORT_SYMBOL(genl_sock);
576 EXPORT_SYMBOL(genl_register_ops);
577 EXPORT_SYMBOL(genl_unregister_ops);
578 EXPORT_SYMBOL(genl_register_family);
579 EXPORT_SYMBOL(genl_unregister_family);