[PATCH] m68knommu: make enable_irq() macro statement
[firewire-audio.git] / net / netlink / genetlink.c
blobf329b72578f568440d8e0ef331210eb6408a9bb8
1 /*
2 * NETLINK Generic Netlink Family
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
6 */
8 #include <linux/config.h>
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 <net/sock.h>
18 #include <net/genetlink.h>
20 struct sock *genl_sock = NULL;
22 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
24 static void genl_lock(void)
26 mutex_lock(&genl_mutex);
29 static int genl_trylock(void)
31 return !mutex_trylock(&genl_mutex);
34 static void genl_unlock(void)
36 mutex_unlock(&genl_mutex);
38 if (genl_sock && genl_sock->sk_receive_queue.qlen)
39 genl_sock->sk_data_ready(genl_sock, 0);
42 #define GENL_FAM_TAB_SIZE 16
43 #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
45 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
47 static int genl_ctrl_event(int event, void *data);
49 static inline unsigned int genl_family_hash(unsigned int id)
51 return id & GENL_FAM_TAB_MASK;
54 static inline struct list_head *genl_family_chain(unsigned int id)
56 return &family_ht[genl_family_hash(id)];
59 static struct genl_family *genl_family_find_byid(unsigned int id)
61 struct genl_family *f;
63 list_for_each_entry(f, genl_family_chain(id), family_list)
64 if (f->id == id)
65 return f;
67 return NULL;
70 static struct genl_family *genl_family_find_byname(char *name)
72 struct genl_family *f;
73 int i;
75 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
76 list_for_each_entry(f, genl_family_chain(i), family_list)
77 if (strcmp(f->name, name) == 0)
78 return f;
80 return NULL;
83 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
85 struct genl_ops *ops;
87 list_for_each_entry(ops, &family->ops_list, ops_list)
88 if (ops->cmd == cmd)
89 return ops;
91 return NULL;
94 /* Of course we are going to have problems once we hit
95 * 2^16 alive types, but that can only happen by year 2K
97 static inline u16 genl_generate_id(void)
99 static u16 id_gen_idx;
100 int overflowed = 0;
102 do {
103 if (id_gen_idx == 0)
104 id_gen_idx = GENL_MIN_ID;
106 if (++id_gen_idx > GENL_MAX_ID) {
107 if (!overflowed) {
108 overflowed = 1;
109 id_gen_idx = 0;
110 continue;
111 } else
112 return 0;
115 } while (genl_family_find_byid(id_gen_idx));
117 return id_gen_idx;
121 * genl_register_ops - register generic netlink operations
122 * @family: generic netlink family
123 * @ops: operations to be registered
125 * Registers the specified operations and assigns them to the specified
126 * family. Either a doit or dumpit callback must be specified or the
127 * operation will fail. Only one operation structure per command
128 * identifier may be registered.
130 * See include/net/genetlink.h for more documenation on the operations
131 * structure.
133 * Returns 0 on success or a negative error code.
135 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
137 int err = -EINVAL;
139 if (ops->dumpit == NULL && ops->doit == NULL)
140 goto errout;
142 if (genl_get_cmd(ops->cmd, family)) {
143 err = -EEXIST;
144 goto errout;
147 genl_lock();
148 list_add_tail(&ops->ops_list, &family->ops_list);
149 genl_unlock();
151 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
152 err = 0;
153 errout:
154 return err;
158 * genl_unregister_ops - unregister generic netlink operations
159 * @family: generic netlink family
160 * @ops: operations to be unregistered
162 * Unregisters the specified operations and unassigns them from the
163 * specified family. The operation blocks until the current message
164 * processing has finished and doesn't start again until the
165 * unregister process has finished.
167 * Note: It is not necessary to unregister all operations before
168 * unregistering the family, unregistering the family will cause
169 * all assigned operations to be unregistered automatically.
171 * Returns 0 on success or a negative error code.
173 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
175 struct genl_ops *rc;
177 genl_lock();
178 list_for_each_entry(rc, &family->ops_list, ops_list) {
179 if (rc == ops) {
180 list_del(&ops->ops_list);
181 genl_unlock();
182 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
183 return 0;
186 genl_unlock();
188 return -ENOENT;
192 * genl_register_family - register a generic netlink family
193 * @family: generic netlink family
195 * Registers the specified family after validating it first. Only one
196 * family may be registered with the same family name or identifier.
197 * The family id may equal GENL_ID_GENERATE causing an unique id to
198 * be automatically generated and assigned.
200 * Return 0 on success or a negative error code.
202 int genl_register_family(struct genl_family *family)
204 int err = -EINVAL;
206 if (family->id && family->id < GENL_MIN_ID)
207 goto errout;
209 if (family->id > GENL_MAX_ID)
210 goto errout;
212 INIT_LIST_HEAD(&family->ops_list);
214 genl_lock();
216 if (genl_family_find_byname(family->name)) {
217 err = -EEXIST;
218 goto errout_locked;
221 if (genl_family_find_byid(family->id)) {
222 err = -EEXIST;
223 goto errout_locked;
226 if (family->id == GENL_ID_GENERATE) {
227 u16 newid = genl_generate_id();
229 if (!newid) {
230 err = -ENOMEM;
231 goto errout_locked;
234 family->id = newid;
237 if (family->maxattr) {
238 family->attrbuf = kmalloc((family->maxattr+1) *
239 sizeof(struct nlattr *), GFP_KERNEL);
240 if (family->attrbuf == NULL) {
241 err = -ENOMEM;
242 goto errout_locked;
244 } else
245 family->attrbuf = NULL;
247 list_add_tail(&family->family_list, genl_family_chain(family->id));
248 genl_unlock();
250 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
252 return 0;
254 errout_locked:
255 genl_unlock();
256 errout:
257 return err;
261 * genl_unregister_family - unregister generic netlink family
262 * @family: generic netlink family
264 * Unregisters the specified family.
266 * Returns 0 on success or a negative error code.
268 int genl_unregister_family(struct genl_family *family)
270 struct genl_family *rc;
272 genl_lock();
274 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
275 if (family->id != rc->id || strcmp(rc->name, family->name))
276 continue;
278 list_del(&rc->family_list);
279 INIT_LIST_HEAD(&family->ops_list);
280 genl_unlock();
282 kfree(family->attrbuf);
283 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
284 return 0;
287 genl_unlock();
289 return -ENOENT;
292 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
293 int *errp)
295 struct genl_ops *ops;
296 struct genl_family *family;
297 struct genl_info info;
298 struct genlmsghdr *hdr = nlmsg_data(nlh);
299 int hdrlen, err = -EINVAL;
301 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
302 goto ignore;
304 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
305 goto ignore;
307 family = genl_family_find_byid(nlh->nlmsg_type);
308 if (family == NULL) {
309 err = -ENOENT;
310 goto errout;
313 hdrlen = GENL_HDRLEN + family->hdrsize;
314 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
315 goto errout;
317 ops = genl_get_cmd(hdr->cmd, family);
318 if (ops == NULL) {
319 err = -EOPNOTSUPP;
320 goto errout;
323 if ((ops->flags & GENL_ADMIN_PERM) && security_netlink_recv(skb)) {
324 err = -EPERM;
325 goto errout;
328 if (nlh->nlmsg_flags & NLM_F_DUMP) {
329 if (ops->dumpit == NULL) {
330 err = -EOPNOTSUPP;
331 goto errout;
334 *errp = err = netlink_dump_start(genl_sock, skb, nlh,
335 ops->dumpit, NULL);
336 if (err == 0)
337 skb_pull(skb, min(NLMSG_ALIGN(nlh->nlmsg_len),
338 skb->len));
339 return -1;
342 if (ops->doit == NULL) {
343 err = -EOPNOTSUPP;
344 goto errout;
347 if (family->attrbuf) {
348 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
349 ops->policy);
350 if (err < 0)
351 goto errout;
354 info.snd_seq = nlh->nlmsg_seq;
355 info.snd_pid = NETLINK_CB(skb).pid;
356 info.nlhdr = nlh;
357 info.genlhdr = nlmsg_data(nlh);
358 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
359 info.attrs = family->attrbuf;
361 *errp = err = ops->doit(skb, &info);
362 return err;
364 ignore:
365 return 0;
367 errout:
368 *errp = err;
369 return -1;
372 static void genl_rcv(struct sock *sk, int len)
374 unsigned int qlen = 0;
376 do {
377 if (genl_trylock())
378 return;
379 netlink_run_queue(sk, &qlen, genl_rcv_msg);
380 genl_unlock();
381 } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
384 /**************************************************************************
385 * Controller
386 **************************************************************************/
388 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
389 u32 flags, struct sk_buff *skb, u8 cmd)
391 void *hdr;
393 hdr = genlmsg_put(skb, pid, seq, GENL_ID_CTRL, 0, flags, cmd,
394 family->version);
395 if (hdr == NULL)
396 return -1;
398 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
399 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
401 return genlmsg_end(skb, hdr);
403 nla_put_failure:
404 return genlmsg_cancel(skb, hdr);
407 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
410 int i, n = 0;
411 struct genl_family *rt;
412 int chains_to_skip = cb->args[0];
413 int fams_to_skip = cb->args[1];
415 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
416 if (i < chains_to_skip)
417 continue;
418 n = 0;
419 list_for_each_entry(rt, genl_family_chain(i), family_list) {
420 if (++n < fams_to_skip)
421 continue;
422 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
423 cb->nlh->nlmsg_seq, NLM_F_MULTI,
424 skb, CTRL_CMD_NEWFAMILY) < 0)
425 goto errout;
428 fams_to_skip = 0;
431 errout:
432 cb->args[0] = i;
433 cb->args[1] = n;
435 return skb->len;
438 static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
439 int seq, u8 cmd)
441 struct sk_buff *skb;
442 int err;
444 skb = nlmsg_new(NLMSG_GOODSIZE);
445 if (skb == NULL)
446 return ERR_PTR(-ENOBUFS);
448 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
449 if (err < 0) {
450 nlmsg_free(skb);
451 return ERR_PTR(err);
454 return skb;
457 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = {
458 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
459 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING },
462 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
464 struct sk_buff *msg;
465 struct genl_family *res = NULL;
466 int err = -EINVAL;
468 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
469 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
470 res = genl_family_find_byid(id);
473 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
474 char name[GENL_NAMSIZ];
476 if (nla_strlcpy(name, info->attrs[CTRL_ATTR_FAMILY_NAME],
477 GENL_NAMSIZ) >= GENL_NAMSIZ)
478 goto errout;
480 res = genl_family_find_byname(name);
483 if (res == NULL) {
484 err = -ENOENT;
485 goto errout;
488 msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
489 CTRL_CMD_NEWFAMILY);
490 if (IS_ERR(msg)) {
491 err = PTR_ERR(msg);
492 goto errout;
495 err = genlmsg_unicast(msg, info->snd_pid);
496 errout:
497 return err;
500 static int genl_ctrl_event(int event, void *data)
502 struct sk_buff *msg;
504 if (genl_sock == NULL)
505 return 0;
507 switch (event) {
508 case CTRL_CMD_NEWFAMILY:
509 case CTRL_CMD_DELFAMILY:
510 msg = ctrl_build_msg(data, 0, 0, event);
511 if (IS_ERR(msg))
512 return PTR_ERR(msg);
514 genlmsg_multicast(msg, 0, GENL_ID_CTRL);
515 break;
518 return 0;
521 static struct genl_ops genl_ctrl_ops = {
522 .cmd = CTRL_CMD_GETFAMILY,
523 .doit = ctrl_getfamily,
524 .dumpit = ctrl_dumpfamily,
525 .policy = ctrl_policy,
528 static struct genl_family genl_ctrl = {
529 .id = GENL_ID_CTRL,
530 .name = "nlctrl",
531 .version = 0x1,
532 .maxattr = CTRL_ATTR_MAX,
535 static int __init genl_init(void)
537 int i, err;
539 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
540 INIT_LIST_HEAD(&family_ht[i]);
542 err = genl_register_family(&genl_ctrl);
543 if (err < 0)
544 goto errout;
546 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
547 if (err < 0)
548 goto errout_register;
550 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
551 genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
552 genl_rcv, THIS_MODULE);
553 if (genl_sock == NULL)
554 panic("GENL: Cannot initialize generic netlink\n");
556 return 0;
558 errout_register:
559 genl_unregister_family(&genl_ctrl);
560 errout:
561 panic("GENL: Cannot register controller: %d\n", err);
564 subsys_initcall(genl_init);
566 EXPORT_SYMBOL(genl_sock);
567 EXPORT_SYMBOL(genl_register_ops);
568 EXPORT_SYMBOL(genl_unregister_ops);
569 EXPORT_SYMBOL(genl_register_family);
570 EXPORT_SYMBOL(genl_unregister_family);