[NETFILTER]: nf_conntrack: move conntrack protocol sysctls to individual modules
[linux-2.6.22.y-op.git] / net / netlink / genetlink.c
blobcc874f0fcbdbc26abf59d245b9c7f87f65023759
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 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 (family->id == GENL_ID_GENERATE) {
226 u16 newid = genl_generate_id();
228 if (!newid) {
229 err = -ENOMEM;
230 goto errout_locked;
233 family->id = newid;
236 if (family->maxattr) {
237 family->attrbuf = kmalloc((family->maxattr+1) *
238 sizeof(struct nlattr *), GFP_KERNEL);
239 if (family->attrbuf == NULL) {
240 err = -ENOMEM;
241 goto errout_locked;
243 } else
244 family->attrbuf = NULL;
246 list_add_tail(&family->family_list, genl_family_chain(family->id));
247 genl_unlock();
249 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
251 return 0;
253 errout_locked:
254 genl_unlock();
255 errout:
256 return err;
260 * genl_unregister_family - unregister generic netlink family
261 * @family: generic netlink family
263 * Unregisters the specified family.
265 * Returns 0 on success or a negative error code.
267 int genl_unregister_family(struct genl_family *family)
269 struct genl_family *rc;
271 genl_lock();
273 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
274 if (family->id != rc->id || strcmp(rc->name, family->name))
275 continue;
277 list_del(&rc->family_list);
278 INIT_LIST_HEAD(&family->ops_list);
279 genl_unlock();
281 kfree(family->attrbuf);
282 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
283 return 0;
286 genl_unlock();
288 return -ENOENT;
291 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
292 int *errp)
294 struct genl_ops *ops;
295 struct genl_family *family;
296 struct genl_info info;
297 struct genlmsghdr *hdr = nlmsg_data(nlh);
298 int hdrlen, err = -EINVAL;
300 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
301 goto ignore;
303 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
304 goto ignore;
306 family = genl_family_find_byid(nlh->nlmsg_type);
307 if (family == NULL) {
308 err = -ENOENT;
309 goto errout;
312 hdrlen = GENL_HDRLEN + family->hdrsize;
313 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
314 goto errout;
316 ops = genl_get_cmd(hdr->cmd, family);
317 if (ops == NULL) {
318 err = -EOPNOTSUPP;
319 goto errout;
322 if ((ops->flags & GENL_ADMIN_PERM) && security_netlink_recv(skb, CAP_NET_ADMIN)) {
323 err = -EPERM;
324 goto errout;
327 if (nlh->nlmsg_flags & NLM_F_DUMP) {
328 if (ops->dumpit == NULL) {
329 err = -EOPNOTSUPP;
330 goto errout;
333 *errp = err = netlink_dump_start(genl_sock, skb, nlh,
334 ops->dumpit, NULL);
335 if (err == 0)
336 skb_pull(skb, min(NLMSG_ALIGN(nlh->nlmsg_len),
337 skb->len));
338 return -1;
341 if (ops->doit == NULL) {
342 err = -EOPNOTSUPP;
343 goto errout;
346 if (family->attrbuf) {
347 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
348 ops->policy);
349 if (err < 0)
350 goto errout;
353 info.snd_seq = nlh->nlmsg_seq;
354 info.snd_pid = NETLINK_CB(skb).pid;
355 info.nlhdr = nlh;
356 info.genlhdr = nlmsg_data(nlh);
357 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
358 info.attrs = family->attrbuf;
360 *errp = err = ops->doit(skb, &info);
361 return err;
363 ignore:
364 return 0;
366 errout:
367 *errp = err;
368 return -1;
371 static void genl_rcv(struct sock *sk, int len)
373 unsigned int qlen = 0;
375 do {
376 if (genl_trylock())
377 return;
378 netlink_run_queue(sk, &qlen, genl_rcv_msg);
379 genl_unlock();
380 } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
383 /**************************************************************************
384 * Controller
385 **************************************************************************/
387 static struct genl_family genl_ctrl = {
388 .id = GENL_ID_CTRL,
389 .name = "nlctrl",
390 .version = 0x1,
391 .maxattr = CTRL_ATTR_MAX,
394 static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
395 u32 flags, struct sk_buff *skb, u8 cmd)
397 void *hdr;
399 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
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);
405 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
406 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
407 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
409 if (!list_empty(&family->ops_list)) {
410 struct nlattr *nla_ops;
411 struct genl_ops *ops;
412 int idx = 1;
414 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
415 if (nla_ops == NULL)
416 goto nla_put_failure;
418 list_for_each_entry(ops, &family->ops_list, ops_list) {
419 struct nlattr *nest;
421 nest = nla_nest_start(skb, idx++);
422 if (nest == NULL)
423 goto nla_put_failure;
425 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
426 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
428 if (ops->policy)
429 NLA_PUT_FLAG(skb, CTRL_ATTR_OP_POLICY);
431 if (ops->doit)
432 NLA_PUT_FLAG(skb, CTRL_ATTR_OP_DOIT);
434 if (ops->dumpit)
435 NLA_PUT_FLAG(skb, CTRL_ATTR_OP_DUMPIT);
437 nla_nest_end(skb, nest);
440 nla_nest_end(skb, nla_ops);
443 return genlmsg_end(skb, hdr);
445 nla_put_failure:
446 return genlmsg_cancel(skb, hdr);
449 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
452 int i, n = 0;
453 struct genl_family *rt;
454 int chains_to_skip = cb->args[0];
455 int fams_to_skip = cb->args[1];
457 if (chains_to_skip != 0)
458 genl_lock();
460 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
461 if (i < chains_to_skip)
462 continue;
463 n = 0;
464 list_for_each_entry(rt, genl_family_chain(i), family_list) {
465 if (++n < fams_to_skip)
466 continue;
467 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
468 cb->nlh->nlmsg_seq, NLM_F_MULTI,
469 skb, CTRL_CMD_NEWFAMILY) < 0)
470 goto errout;
473 fams_to_skip = 0;
476 errout:
477 if (chains_to_skip != 0)
478 genl_unlock();
480 cb->args[0] = i;
481 cb->args[1] = n;
483 return skb->len;
486 static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
487 int seq, u8 cmd)
489 struct sk_buff *skb;
490 int err;
492 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
493 if (skb == NULL)
494 return ERR_PTR(-ENOBUFS);
496 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
497 if (err < 0) {
498 nlmsg_free(skb);
499 return ERR_PTR(err);
502 return skb;
505 static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = {
506 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
507 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
508 .len = GENL_NAMSIZ - 1 },
511 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
513 struct sk_buff *msg;
514 struct genl_family *res = NULL;
515 int err = -EINVAL;
517 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
518 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
519 res = genl_family_find_byid(id);
522 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
523 char *name;
525 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
526 res = genl_family_find_byname(name);
529 if (res == NULL) {
530 err = -ENOENT;
531 goto errout;
534 msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
535 CTRL_CMD_NEWFAMILY);
536 if (IS_ERR(msg)) {
537 err = PTR_ERR(msg);
538 goto errout;
541 err = genlmsg_reply(msg, info);
542 errout:
543 return err;
546 static int genl_ctrl_event(int event, void *data)
548 struct sk_buff *msg;
550 if (genl_sock == NULL)
551 return 0;
553 switch (event) {
554 case CTRL_CMD_NEWFAMILY:
555 case CTRL_CMD_DELFAMILY:
556 msg = ctrl_build_msg(data, 0, 0, event);
557 if (IS_ERR(msg))
558 return PTR_ERR(msg);
560 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
561 break;
564 return 0;
567 static struct genl_ops genl_ctrl_ops = {
568 .cmd = CTRL_CMD_GETFAMILY,
569 .doit = ctrl_getfamily,
570 .dumpit = ctrl_dumpfamily,
571 .policy = ctrl_policy,
574 static int __init genl_init(void)
576 int i, err;
578 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
579 INIT_LIST_HEAD(&family_ht[i]);
581 err = genl_register_family(&genl_ctrl);
582 if (err < 0)
583 goto errout;
585 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
586 if (err < 0)
587 goto errout_register;
589 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
590 genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
591 genl_rcv, THIS_MODULE);
592 if (genl_sock == NULL)
593 panic("GENL: Cannot initialize generic netlink\n");
595 return 0;
597 errout_register:
598 genl_unregister_family(&genl_ctrl);
599 errout:
600 panic("GENL: Cannot register controller: %d\n", err);
603 subsys_initcall(genl_init);
605 EXPORT_SYMBOL(genl_sock);
606 EXPORT_SYMBOL(genl_register_ops);
607 EXPORT_SYMBOL(genl_unregister_ops);
608 EXPORT_SYMBOL(genl_register_family);
609 EXPORT_SYMBOL(genl_unregister_family);