[SCSI] lpfc_scsi.c: make lpfc_get_scsi_buf() static
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netlink / genetlink.c
blob3b1378498d50b12647f2ea433a341b16502e1ac9
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 <net/sock.h>
17 #include <net/genetlink.h>
19 struct sock *genl_sock = NULL;
21 static DECLARE_MUTEX(genl_sem); /* serialization of message processing */
23 static void genl_lock(void)
25 down(&genl_sem);
28 static int genl_trylock(void)
30 return down_trylock(&genl_sem);
33 static void genl_unlock(void)
35 up(&genl_sem);
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 genl_lock();
147 list_add_tail(&ops->ops_list, &family->ops_list);
148 genl_unlock();
150 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
151 err = 0;
152 errout:
153 return err;
157 * genl_unregister_ops - unregister generic netlink operations
158 * @family: generic netlink family
159 * @ops: operations to be unregistered
161 * Unregisters the specified operations and unassigns them from the
162 * specified family. The operation blocks until the current message
163 * processing has finished and doesn't start again until the
164 * unregister process has finished.
166 * Note: It is not necessary to unregister all operations before
167 * unregistering the family, unregistering the family will cause
168 * all assigned operations to be unregistered automatically.
170 * Returns 0 on success or a negative error code.
172 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
174 struct genl_ops *rc;
176 genl_lock();
177 list_for_each_entry(rc, &family->ops_list, ops_list) {
178 if (rc == ops) {
179 list_del(&ops->ops_list);
180 genl_unlock();
181 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
182 return 0;
185 genl_unlock();
187 return -ENOENT;
191 * genl_register_family - register a generic netlink family
192 * @family: generic netlink family
194 * Registers the specified family after validating it first. Only one
195 * family may be registered with the same family name or identifier.
196 * The family id may equal GENL_ID_GENERATE causing an unique id to
197 * be automatically generated and assigned.
199 * Return 0 on success or a negative error code.
201 int genl_register_family(struct genl_family *family)
203 int err = -EINVAL;
205 if (family->id && family->id < GENL_MIN_ID)
206 goto errout;
208 if (family->id > GENL_MAX_ID)
209 goto errout;
211 INIT_LIST_HEAD(&family->ops_list);
213 genl_lock();
215 if (genl_family_find_byname(family->name)) {
216 err = -EEXIST;
217 goto errout_locked;
220 if (genl_family_find_byid(family->id)) {
221 err = -EEXIST;
222 goto errout_locked;
225 if (!try_module_get(family->owner)) {
226 err = -EBUSY;
227 goto errout_locked;
230 if (family->id == GENL_ID_GENERATE) {
231 u16 newid = genl_generate_id();
233 if (!newid) {
234 err = -ENOMEM;
235 goto errout_locked;
238 family->id = newid;
241 if (family->maxattr) {
242 family->attrbuf = kmalloc((family->maxattr+1) *
243 sizeof(struct nlattr *), GFP_KERNEL);
244 if (family->attrbuf == NULL) {
245 err = -ENOMEM;
246 goto errout;
248 } else
249 family->attrbuf = NULL;
251 list_add_tail(&family->family_list, genl_family_chain(family->id));
252 genl_unlock();
254 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
256 return 0;
258 errout_locked:
259 genl_unlock();
260 errout:
261 return err;
265 * genl_unregister_family - unregister generic netlink family
266 * @family: generic netlink family
268 * Unregisters the specified family.
270 * Returns 0 on success or a negative error code.
272 int genl_unregister_family(struct genl_family *family)
274 struct genl_family *rc;
276 genl_lock();
278 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
279 if (family->id != rc->id || strcmp(rc->name, family->name))
280 continue;
282 list_del(&rc->family_list);
283 INIT_LIST_HEAD(&family->ops_list);
284 genl_unlock();
286 module_put(family->owner);
287 kfree(family->attrbuf);
288 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
289 return 0;
292 genl_unlock();
294 return -ENOENT;
297 static inline int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
298 int *errp)
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 = -EINVAL;
306 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
307 goto ignore;
309 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
310 goto ignore;
312 family = genl_family_find_byid(nlh->nlmsg_type);
313 if (family == NULL) {
314 err = -ENOENT;
315 goto errout;
318 hdrlen = GENL_HDRLEN + family->hdrsize;
319 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
320 goto errout;
322 ops = genl_get_cmd(hdr->cmd, family);
323 if (ops == NULL) {
324 err = -EOPNOTSUPP;
325 goto errout;
328 if ((ops->flags & GENL_ADMIN_PERM) && security_netlink_recv(skb)) {
329 err = -EPERM;
330 goto errout;
333 if (nlh->nlmsg_flags & NLM_F_DUMP) {
334 if (ops->dumpit == NULL) {
335 err = -EOPNOTSUPP;
336 goto errout;
339 *errp = err = netlink_dump_start(genl_sock, skb, nlh,
340 ops->dumpit, NULL);
341 if (err == 0)
342 skb_pull(skb, min(NLMSG_ALIGN(nlh->nlmsg_len),
343 skb->len));
344 return -1;
347 if (ops->doit == NULL) {
348 err = -EOPNOTSUPP;
349 goto errout;
352 if (family->attrbuf) {
353 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
354 ops->policy);
355 if (err < 0)
356 goto errout;
359 info.snd_seq = nlh->nlmsg_seq;
360 info.snd_pid = NETLINK_CB(skb).pid;
361 info.nlhdr = nlh;
362 info.genlhdr = nlmsg_data(nlh);
363 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
364 info.attrs = family->attrbuf;
366 *errp = err = ops->doit(skb, &info);
367 return err;
369 ignore:
370 return 0;
372 errout:
373 *errp = err;
374 return -1;
377 static void genl_rcv(struct sock *sk, int len)
379 unsigned int qlen = 0;
381 do {
382 if (genl_trylock())
383 return;
384 netlink_run_queue(sk, &qlen, &genl_rcv_msg);
385 genl_unlock();
386 } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
389 /**************************************************************************
390 * Controller
391 **************************************************************************/
393 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
394 u32 flags, struct sk_buff *skb, u8 cmd)
396 void *hdr;
398 hdr = genlmsg_put(skb, pid, seq, GENL_ID_CTRL, 0, flags, cmd,
399 family->version);
400 if (hdr == NULL)
401 return -1;
403 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
404 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
406 return genlmsg_end(skb, hdr);
408 nla_put_failure:
409 return genlmsg_cancel(skb, hdr);
412 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
415 int i, n = 0;
416 struct genl_family *rt;
417 int chains_to_skip = cb->args[0];
418 int fams_to_skip = cb->args[1];
420 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
421 if (i < chains_to_skip)
422 continue;
423 n = 0;
424 list_for_each_entry(rt, genl_family_chain(i), family_list) {
425 if (++n < fams_to_skip)
426 continue;
427 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
428 cb->nlh->nlmsg_seq, NLM_F_MULTI,
429 skb, CTRL_CMD_NEWFAMILY) < 0)
430 goto errout;
433 fams_to_skip = 0;
436 errout:
437 cb->args[0] = i;
438 cb->args[1] = n;
440 return skb->len;
443 static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
444 int seq, u8 cmd)
446 struct sk_buff *skb;
447 int err;
449 skb = nlmsg_new(NLMSG_GOODSIZE);
450 if (skb == NULL)
451 return ERR_PTR(-ENOBUFS);
453 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
454 if (err < 0) {
455 nlmsg_free(skb);
456 return ERR_PTR(err);
459 return skb;
462 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = {
463 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
464 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_STRING },
467 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
469 struct sk_buff *msg;
470 struct genl_family *res = NULL;
471 int err = -EINVAL;
473 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
474 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
475 res = genl_family_find_byid(id);
478 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
479 char name[GENL_NAMSIZ];
481 if (nla_strlcpy(name, info->attrs[CTRL_ATTR_FAMILY_NAME],
482 GENL_NAMSIZ) >= GENL_NAMSIZ)
483 goto errout;
485 res = genl_family_find_byname(name);
488 if (res == NULL) {
489 err = -ENOENT;
490 goto errout;
493 msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
494 CTRL_CMD_NEWFAMILY);
495 if (IS_ERR(msg)) {
496 err = PTR_ERR(msg);
497 goto errout;
500 err = genlmsg_unicast(msg, info->snd_pid);
501 errout:
502 return err;
505 static int genl_ctrl_event(int event, void *data)
507 struct sk_buff *msg;
509 if (genl_sock == NULL)
510 return 0;
512 switch (event) {
513 case CTRL_CMD_NEWFAMILY:
514 case CTRL_CMD_DELFAMILY:
515 msg = ctrl_build_msg(data, 0, 0, event);
516 if (IS_ERR(msg))
517 return PTR_ERR(msg);
519 genlmsg_multicast(msg, 0, GENL_ID_CTRL);
520 break;
523 return 0;
526 static struct genl_ops genl_ctrl_ops = {
527 .cmd = CTRL_CMD_GETFAMILY,
528 .doit = ctrl_getfamily,
529 .dumpit = ctrl_dumpfamily,
530 .policy = ctrl_policy,
533 static struct genl_family genl_ctrl = {
534 .id = GENL_ID_CTRL,
535 .name = "nlctrl",
536 .version = 0x1,
537 .maxattr = CTRL_ATTR_MAX,
538 .owner = THIS_MODULE,
541 static int __init genl_init(void)
543 int i, err;
545 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
546 INIT_LIST_HEAD(&family_ht[i]);
548 err = genl_register_family(&genl_ctrl);
549 if (err < 0)
550 goto errout;
552 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
553 if (err < 0)
554 goto errout_register;
556 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
557 genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
558 genl_rcv, THIS_MODULE);
559 if (genl_sock == NULL) {
560 panic("GENL: Cannot initialize generic netlink\n");
561 return -ENOMEM;
564 return 0;
566 errout_register:
567 genl_unregister_family(&genl_ctrl);
568 errout:
569 panic("GENL: Cannot register controller: %d\n", err);
570 return 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);