blob: 1b897bc92e61c4be37210dd3f382d8d4685677e8 [file] [log] [blame]
Thomas Graf482a8522005-11-10 02:25:56 +01001/*
2 * NETLINK Generic Netlink Family
3 *
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
6 */
7
Thomas Graf482a8522005-11-10 02:25:56 +01008#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>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080015#include <linux/mutex.h>
Thomas Graf482a8522005-11-10 02:25:56 +010016#include <net/sock.h>
17#include <net/genetlink.h>
18
19struct sock *genl_sock = NULL;
20
Ingo Molnar14cc3e22006-03-26 01:37:14 -080021static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
Thomas Graf482a8522005-11-10 02:25:56 +010022
23static void genl_lock(void)
24{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080025 mutex_lock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010026}
27
28static int genl_trylock(void)
29{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080030 return !mutex_trylock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010031}
32
33static void genl_unlock(void)
34{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080035 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010036
37 if (genl_sock && genl_sock->sk_receive_queue.qlen)
38 genl_sock->sk_data_ready(genl_sock, 0);
39}
40
41#define GENL_FAM_TAB_SIZE 16
42#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
43
44static struct list_head family_ht[GENL_FAM_TAB_SIZE];
45
46static int genl_ctrl_event(int event, void *data);
47
48static inline unsigned int genl_family_hash(unsigned int id)
49{
50 return id & GENL_FAM_TAB_MASK;
51}
52
53static inline struct list_head *genl_family_chain(unsigned int id)
54{
55 return &family_ht[genl_family_hash(id)];
56}
57
58static struct genl_family *genl_family_find_byid(unsigned int id)
59{
60 struct genl_family *f;
61
62 list_for_each_entry(f, genl_family_chain(id), family_list)
63 if (f->id == id)
64 return f;
65
66 return NULL;
67}
68
69static struct genl_family *genl_family_find_byname(char *name)
70{
71 struct genl_family *f;
72 int i;
73
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;
78
79 return NULL;
80}
81
82static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
83{
84 struct genl_ops *ops;
85
86 list_for_each_entry(ops, &family->ops_list, ops_list)
87 if (ops->cmd == cmd)
88 return ops;
89
90 return NULL;
91}
92
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
95*/
96static inline u16 genl_generate_id(void)
97{
98 static u16 id_gen_idx;
99 int overflowed = 0;
100
101 do {
102 if (id_gen_idx == 0)
103 id_gen_idx = GENL_MIN_ID;
104
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;
112 }
113
114 } while (genl_family_find_byid(id_gen_idx));
115
116 return id_gen_idx;
117}
118
119/**
120 * genl_register_ops - register generic netlink operations
121 * @family: generic netlink family
122 * @ops: operations to be registered
123 *
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.
128 *
129 * See include/net/genetlink.h for more documenation on the operations
130 * structure.
131 *
132 * Returns 0 on success or a negative error code.
133 */
134int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
135{
136 int err = -EINVAL;
137
138 if (ops->dumpit == NULL && ops->doit == NULL)
139 goto errout;
140
141 if (genl_get_cmd(ops->cmd, family)) {
142 err = -EEXIST;
143 goto errout;
144 }
145
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800146 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800147 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800148 if (ops->doit)
149 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800150 if (ops->policy)
151 ops->flags |= GENL_CMD_CAP_HASPOL;
152
Thomas Graf482a8522005-11-10 02:25:56 +0100153 genl_lock();
154 list_add_tail(&ops->ops_list, &family->ops_list);
155 genl_unlock();
156
157 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
158 err = 0;
159errout:
160 return err;
161}
162
163/**
164 * genl_unregister_ops - unregister generic netlink operations
165 * @family: generic netlink family
166 * @ops: operations to be unregistered
167 *
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.
172 *
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.
176 *
177 * Returns 0 on success or a negative error code.
178 */
179int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
180{
181 struct genl_ops *rc;
182
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;
190 }
191 }
192 genl_unlock();
193
194 return -ENOENT;
195}
196
197/**
198 * genl_register_family - register a generic netlink family
199 * @family: generic netlink family
200 *
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.
205 *
206 * Return 0 on success or a negative error code.
207 */
208int genl_register_family(struct genl_family *family)
209{
210 int err = -EINVAL;
211
212 if (family->id && family->id < GENL_MIN_ID)
213 goto errout;
214
215 if (family->id > GENL_MAX_ID)
216 goto errout;
217
218 INIT_LIST_HEAD(&family->ops_list);
219
220 genl_lock();
221
222 if (genl_family_find_byname(family->name)) {
223 err = -EEXIST;
224 goto errout_locked;
225 }
226
227 if (genl_family_find_byid(family->id)) {
228 err = -EEXIST;
229 goto errout_locked;
230 }
231
Thomas Graf482a8522005-11-10 02:25:56 +0100232 if (family->id == GENL_ID_GENERATE) {
233 u16 newid = genl_generate_id();
234
235 if (!newid) {
236 err = -ENOMEM;
237 goto errout_locked;
238 }
239
240 family->id = newid;
241 }
242
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;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800248 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100249 }
250 } else
251 family->attrbuf = NULL;
252
253 list_add_tail(&family->family_list, genl_family_chain(family->id));
254 genl_unlock();
255
256 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
257
258 return 0;
259
260errout_locked:
261 genl_unlock();
262errout:
263 return err;
264}
265
266/**
267 * genl_unregister_family - unregister generic netlink family
268 * @family: generic netlink family
269 *
270 * Unregisters the specified family.
271 *
272 * Returns 0 on success or a negative error code.
273 */
274int genl_unregister_family(struct genl_family *family)
275{
276 struct genl_family *rc;
277
278 genl_lock();
279
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;
283
284 list_del(&rc->family_list);
285 INIT_LIST_HEAD(&family->ops_list);
286 genl_unlock();
287
Thomas Graf482a8522005-11-10 02:25:56 +0100288 kfree(family->attrbuf);
289 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
290 return 0;
291 }
292
293 genl_unlock();
294
295 return -ENOENT;
296}
297
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700298static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100299{
300 struct genl_ops *ops;
301 struct genl_family *family;
302 struct genl_info info;
303 struct genlmsghdr *hdr = nlmsg_data(nlh);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700304 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100305
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900306 family = genl_family_find_byid(nlh->nlmsg_type);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700307 if (family == NULL)
308 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100309
310 hdrlen = GENL_HDRLEN + family->hdrsize;
311 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700312 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100313
314 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700315 if (ops == NULL)
316 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100317
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700318 if ((ops->flags & GENL_ADMIN_PERM) &&
319 security_netlink_recv(skb, CAP_NET_ADMIN))
320 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100321
322 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700323 if (ops->dumpit == NULL)
324 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100325
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700326 err = netlink_dump_start(genl_sock, skb, nlh,
327 ops->dumpit, ops->done);
Thomas Graf482a8522005-11-10 02:25:56 +0100328 if (err == 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700329 err = -EINTR;
330 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100331 }
332
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700333 if (ops->doit == NULL)
334 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100335
336 if (family->attrbuf) {
337 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
338 ops->policy);
339 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700340 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100341 }
342
343 info.snd_seq = nlh->nlmsg_seq;
344 info.snd_pid = NETLINK_CB(skb).pid;
345 info.nlhdr = nlh;
346 info.genlhdr = nlmsg_data(nlh);
347 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
348 info.attrs = family->attrbuf;
349
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700350 return ops->doit(skb, &info);
Thomas Graf482a8522005-11-10 02:25:56 +0100351}
352
353static void genl_rcv(struct sock *sk, int len)
354{
355 unsigned int qlen = 0;
356
357 do {
358 if (genl_trylock())
359 return;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800360 netlink_run_queue(sk, &qlen, genl_rcv_msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100361 genl_unlock();
362 } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen);
363}
364
365/**************************************************************************
366 * Controller
367 **************************************************************************/
368
Thomas Graf17c157c2006-11-14 19:46:02 -0800369static struct genl_family genl_ctrl = {
370 .id = GENL_ID_CTRL,
371 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800372 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800373 .maxattr = CTRL_ATTR_MAX,
374};
375
Thomas Graf482a8522005-11-10 02:25:56 +0100376static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
377 u32 flags, struct sk_buff *skb, u8 cmd)
378{
379 void *hdr;
380
Thomas Graf17c157c2006-11-14 19:46:02 -0800381 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100382 if (hdr == NULL)
383 return -1;
384
385 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
386 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
Thomas Grafeb328112006-09-18 00:01:59 -0700387 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
388 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
389 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
390
Thomas Grafe94ef682006-11-23 11:44:37 -0800391 if (!list_empty(&family->ops_list)) {
392 struct nlattr *nla_ops;
393 struct genl_ops *ops;
394 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700395
Thomas Grafe94ef682006-11-23 11:44:37 -0800396 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
397 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700398 goto nla_put_failure;
399
Thomas Grafe94ef682006-11-23 11:44:37 -0800400 list_for_each_entry(ops, &family->ops_list, ops_list) {
401 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700402
Thomas Grafe94ef682006-11-23 11:44:37 -0800403 nest = nla_nest_start(skb, idx++);
404 if (nest == NULL)
405 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700406
Thomas Grafe94ef682006-11-23 11:44:37 -0800407 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
408 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
Thomas Grafeb328112006-09-18 00:01:59 -0700409
Thomas Grafe94ef682006-11-23 11:44:37 -0800410 nla_nest_end(skb, nest);
411 }
412
413 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700414 }
415
Thomas Graf482a8522005-11-10 02:25:56 +0100416 return genlmsg_end(skb, hdr);
417
418nla_put_failure:
419 return genlmsg_cancel(skb, hdr);
420}
421
422static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
423{
424
425 int i, n = 0;
426 struct genl_family *rt;
427 int chains_to_skip = cb->args[0];
428 int fams_to_skip = cb->args[1];
429
Thomas Grafeb328112006-09-18 00:01:59 -0700430 if (chains_to_skip != 0)
431 genl_lock();
432
Thomas Graf482a8522005-11-10 02:25:56 +0100433 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
434 if (i < chains_to_skip)
435 continue;
436 n = 0;
437 list_for_each_entry(rt, genl_family_chain(i), family_list) {
438 if (++n < fams_to_skip)
439 continue;
440 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
441 cb->nlh->nlmsg_seq, NLM_F_MULTI,
442 skb, CTRL_CMD_NEWFAMILY) < 0)
443 goto errout;
444 }
445
446 fams_to_skip = 0;
447 }
448
449errout:
Thomas Grafeb328112006-09-18 00:01:59 -0700450 if (chains_to_skip != 0)
451 genl_unlock();
452
Thomas Graf482a8522005-11-10 02:25:56 +0100453 cb->args[0] = i;
454 cb->args[1] = n;
455
456 return skb->len;
457}
458
459static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid,
Per Lidenb461d2f2006-01-03 14:13:29 -0800460 int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100461{
462 struct sk_buff *skb;
463 int err;
464
Thomas Graf339bf982006-11-10 14:10:15 -0800465 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100466 if (skb == NULL)
467 return ERR_PTR(-ENOBUFS);
468
469 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
470 if (err < 0) {
471 nlmsg_free(skb);
472 return ERR_PTR(err);
473 }
474
475 return skb;
476}
477
478static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = {
479 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700480 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
481 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100482};
483
484static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
485{
486 struct sk_buff *msg;
487 struct genl_family *res = NULL;
488 int err = -EINVAL;
489
490 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
491 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
492 res = genl_family_find_byid(id);
493 }
494
495 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700496 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100497
Thomas Graf5176f912006-08-26 20:13:18 -0700498 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100499 res = genl_family_find_byname(name);
500 }
501
502 if (res == NULL) {
503 err = -ENOENT;
504 goto errout;
505 }
506
507 msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq,
508 CTRL_CMD_NEWFAMILY);
509 if (IS_ERR(msg)) {
510 err = PTR_ERR(msg);
511 goto errout;
512 }
513
Thomas Graf81878d22006-11-14 19:45:27 -0800514 err = genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100515errout:
516 return err;
517}
518
519static int genl_ctrl_event(int event, void *data)
520{
521 struct sk_buff *msg;
522
523 if (genl_sock == NULL)
524 return 0;
525
526 switch (event) {
527 case CTRL_CMD_NEWFAMILY:
528 case CTRL_CMD_DELFAMILY:
529 msg = ctrl_build_msg(data, 0, 0, event);
530 if (IS_ERR(msg))
531 return PTR_ERR(msg);
532
Thomas Grafd387f6a2006-08-15 00:31:06 -0700533 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100534 break;
535 }
536
537 return 0;
538}
539
540static struct genl_ops genl_ctrl_ops = {
541 .cmd = CTRL_CMD_GETFAMILY,
542 .doit = ctrl_getfamily,
543 .dumpit = ctrl_dumpfamily,
544 .policy = ctrl_policy,
545};
546
Thomas Graf482a8522005-11-10 02:25:56 +0100547static int __init genl_init(void)
548{
549 int i, err;
550
551 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
552 INIT_LIST_HEAD(&family_ht[i]);
553
554 err = genl_register_family(&genl_ctrl);
555 if (err < 0)
556 goto errout;
557
558 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
559 if (err < 0)
560 goto errout_register;
561
562 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
563 genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID,
564 genl_rcv, THIS_MODULE);
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800565 if (genl_sock == NULL)
Thomas Graf482a8522005-11-10 02:25:56 +0100566 panic("GENL: Cannot initialize generic netlink\n");
Thomas Graf482a8522005-11-10 02:25:56 +0100567
568 return 0;
569
570errout_register:
571 genl_unregister_family(&genl_ctrl);
572errout:
573 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100574}
575
576subsys_initcall(genl_init);
577
578EXPORT_SYMBOL(genl_sock);
579EXPORT_SYMBOL(genl_register_ops);
580EXPORT_SYMBOL(genl_unregister_ops);
581EXPORT_SYMBOL(genl_register_family);
582EXPORT_SYMBOL(genl_unregister_family);