blob: a28fda7420d9d2699cb0c0f7a6707ecd9bd0503c [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>
Johannes Berg2dbba6f2007-07-18 15:47:52 -07006 * Johannes Berg <johannes@sipsolutions.net>
Thomas Graf482a8522005-11-10 02:25:56 +01007 */
8
Thomas Graf482a8522005-11-10 02:25:56 +01009#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>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080016#include <linux/mutex.h>
Johannes Berg2dbba6f2007-07-18 15:47:52 -070017#include <linux/bitmap.h>
Thomas Graf482a8522005-11-10 02:25:56 +010018#include <net/sock.h>
19#include <net/genetlink.h>
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
James Chapmanf408e0c2010-04-02 06:19:05 +000023void genl_lock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010024{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080025 mutex_lock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010026}
James Chapmanf408e0c2010-04-02 06:19:05 +000027EXPORT_SYMBOL(genl_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010028
James Chapmanf408e0c2010-04-02 06:19:05 +000029void genl_unlock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010030{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080031 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010032}
James Chapmanf408e0c2010-04-02 06:19:05 +000033EXPORT_SYMBOL(genl_unlock);
Thomas Graf482a8522005-11-10 02:25:56 +010034
35#define GENL_FAM_TAB_SIZE 16
36#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
37
38static struct list_head family_ht[GENL_FAM_TAB_SIZE];
Johannes Berg2dbba6f2007-07-18 15:47:52 -070039/*
40 * Bitmap of multicast groups that are currently in use.
41 *
42 * To avoid an allocation at boot of just one unsigned long,
43 * declare it global instead.
44 * Bit 0 is marked as already used since group 0 is invalid.
45 */
46static unsigned long mc_group_start = 0x1;
47static unsigned long *mc_groups = &mc_group_start;
48static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010049
50static int genl_ctrl_event(int event, void *data);
51
52static inline unsigned int genl_family_hash(unsigned int id)
53{
54 return id & GENL_FAM_TAB_MASK;
55}
56
57static inline struct list_head *genl_family_chain(unsigned int id)
58{
59 return &family_ht[genl_family_hash(id)];
60}
61
62static struct genl_family *genl_family_find_byid(unsigned int id)
63{
64 struct genl_family *f;
65
66 list_for_each_entry(f, genl_family_chain(id), family_list)
67 if (f->id == id)
68 return f;
69
70 return NULL;
71}
72
73static struct genl_family *genl_family_find_byname(char *name)
74{
75 struct genl_family *f;
76 int i;
77
78 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
79 list_for_each_entry(f, genl_family_chain(i), family_list)
80 if (strcmp(f->name, name) == 0)
81 return f;
82
83 return NULL;
84}
85
86static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
87{
88 struct genl_ops *ops;
89
90 list_for_each_entry(ops, &family->ops_list, ops_list)
91 if (ops->cmd == cmd)
92 return ops;
93
94 return NULL;
95}
96
97/* Of course we are going to have problems once we hit
98 * 2^16 alive types, but that can only happen by year 2K
99*/
100static inline u16 genl_generate_id(void)
101{
Krishna Kumar988ade62009-10-14 19:55:07 +0000102 static u16 id_gen_idx = GENL_MIN_ID;
103 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100104
Krishna Kumar988ade62009-10-14 19:55:07 +0000105 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
106 if (!genl_family_find_byid(id_gen_idx))
107 return id_gen_idx;
108 if (++id_gen_idx > GENL_MAX_ID)
Thomas Graf482a8522005-11-10 02:25:56 +0100109 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000110 }
Thomas Graf482a8522005-11-10 02:25:56 +0100111
Krishna Kumar988ade62009-10-14 19:55:07 +0000112 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100113}
114
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700115static struct genl_multicast_group notify_grp;
116
117/**
118 * genl_register_mc_group - register a multicast group
119 *
120 * Registers the specified multicast group and notifies userspace
121 * about the new group.
122 *
123 * Returns 0 on success or a negative error code.
124 *
125 * @family: The generic netlink family the group shall be registered for.
126 * @grp: The group to register, must have a name.
127 */
128int genl_register_mc_group(struct genl_family *family,
129 struct genl_multicast_group *grp)
130{
131 int id;
132 unsigned long *new_groups;
Brian Haleyb1f57192009-09-04 20:36:52 -0700133 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700134
135 BUG_ON(grp->name[0] == '\0');
136
137 genl_lock();
138
139 /* special-case our own group */
140 if (grp == &notify_grp)
141 id = GENL_ID_CTRL;
142 else
143 id = find_first_zero_bit(mc_groups,
144 mc_groups_longs * BITS_PER_LONG);
145
146
147 if (id >= mc_groups_longs * BITS_PER_LONG) {
148 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
149
150 if (mc_groups == &mc_group_start) {
151 new_groups = kzalloc(nlen, GFP_KERNEL);
152 if (!new_groups) {
153 err = -ENOMEM;
154 goto out;
155 }
156 mc_groups = new_groups;
157 *mc_groups = mc_group_start;
158 } else {
159 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
160 if (!new_groups) {
161 err = -ENOMEM;
162 goto out;
163 }
164 mc_groups = new_groups;
165 mc_groups[mc_groups_longs] = 0;
166 }
167 mc_groups_longs++;
168 }
169
Johannes Berg134e6372009-07-10 09:51:34 +0000170 if (family->netnsok) {
171 struct net *net;
172
Johannes Bergd136f1b2009-09-12 03:03:15 +0000173 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000174 rcu_read_lock();
175 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000176 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000177 mc_groups_longs * BITS_PER_LONG);
178 if (err) {
179 /*
180 * No need to roll back, can only fail if
181 * memory allocation fails and then the
182 * number of _possible_ groups has been
183 * increased on some sockets which is ok.
184 */
185 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000186 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000187 goto out;
188 }
189 }
190 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000191 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000192 } else {
193 err = netlink_change_ngroups(init_net.genl_sock,
194 mc_groups_longs * BITS_PER_LONG);
195 if (err)
196 goto out;
197 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700198
199 grp->id = id;
200 set_bit(id, mc_groups);
201 list_add_tail(&grp->list, &family->mcast_groups);
202 grp->family = family;
203
204 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
205 out:
206 genl_unlock();
Thomas Graf79d310d2007-07-24 15:34:53 -0700207 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700208}
209EXPORT_SYMBOL(genl_register_mc_group);
210
Thomas Graf79dc43862007-07-24 15:32:46 -0700211static void __genl_unregister_mc_group(struct genl_family *family,
212 struct genl_multicast_group *grp)
213{
Johannes Berg134e6372009-07-10 09:51:34 +0000214 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700215 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000216
Johannes Bergb8273572009-09-24 15:44:05 -0700217 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000218 rcu_read_lock();
219 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700220 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000221 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700222 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000223
Thomas Graf79dc43862007-07-24 15:32:46 -0700224 clear_bit(grp->id, mc_groups);
225 list_del(&grp->list);
226 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
227 grp->id = 0;
228 grp->family = NULL;
229}
230
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700231/**
232 * genl_unregister_mc_group - unregister a multicast group
233 *
234 * Unregisters the specified multicast group and notifies userspace
235 * about it. All current listeners on the group are removed.
236 *
237 * Note: It is not necessary to unregister all multicast groups before
238 * unregistering the family, unregistering the family will cause
239 * all assigned multicast groups to be unregistered automatically.
240 *
241 * @family: Generic netlink family the group belongs to.
242 * @grp: The group to unregister, must have been registered successfully
243 * previously.
244 */
245void genl_unregister_mc_group(struct genl_family *family,
246 struct genl_multicast_group *grp)
247{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700248 genl_lock();
Thomas Graf79dc43862007-07-24 15:32:46 -0700249 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700250 genl_unlock();
251}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800252EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700253
254static void genl_unregister_mc_groups(struct genl_family *family)
255{
256 struct genl_multicast_group *grp, *tmp;
257
258 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700259 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700260}
261
Thomas Graf482a8522005-11-10 02:25:56 +0100262/**
263 * genl_register_ops - register generic netlink operations
264 * @family: generic netlink family
265 * @ops: operations to be registered
266 *
267 * Registers the specified operations and assigns them to the specified
268 * family. Either a doit or dumpit callback must be specified or the
269 * operation will fail. Only one operation structure per command
270 * identifier may be registered.
271 *
272 * See include/net/genetlink.h for more documenation on the operations
273 * structure.
274 *
275 * Returns 0 on success or a negative error code.
276 */
277int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
278{
279 int err = -EINVAL;
280
281 if (ops->dumpit == NULL && ops->doit == NULL)
282 goto errout;
283
284 if (genl_get_cmd(ops->cmd, family)) {
285 err = -EEXIST;
286 goto errout;
287 }
288
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800289 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800290 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800291 if (ops->doit)
292 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800293 if (ops->policy)
294 ops->flags |= GENL_CMD_CAP_HASPOL;
295
Thomas Graf482a8522005-11-10 02:25:56 +0100296 genl_lock();
297 list_add_tail(&ops->ops_list, &family->ops_list);
298 genl_unlock();
299
300 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
301 err = 0;
302errout:
303 return err;
304}
305
306/**
307 * genl_unregister_ops - unregister generic netlink operations
308 * @family: generic netlink family
309 * @ops: operations to be unregistered
310 *
311 * Unregisters the specified operations and unassigns them from the
312 * specified family. The operation blocks until the current message
313 * processing has finished and doesn't start again until the
314 * unregister process has finished.
315 *
316 * Note: It is not necessary to unregister all operations before
317 * unregistering the family, unregistering the family will cause
318 * all assigned operations to be unregistered automatically.
319 *
320 * Returns 0 on success or a negative error code.
321 */
322int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
323{
324 struct genl_ops *rc;
325
326 genl_lock();
327 list_for_each_entry(rc, &family->ops_list, ops_list) {
328 if (rc == ops) {
329 list_del(&ops->ops_list);
330 genl_unlock();
331 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
332 return 0;
333 }
334 }
335 genl_unlock();
336
337 return -ENOENT;
338}
339
340/**
341 * genl_register_family - register a generic netlink family
342 * @family: generic netlink family
343 *
344 * Registers the specified family after validating it first. Only one
345 * family may be registered with the same family name or identifier.
346 * The family id may equal GENL_ID_GENERATE causing an unique id to
347 * be automatically generated and assigned.
348 *
349 * Return 0 on success or a negative error code.
350 */
351int genl_register_family(struct genl_family *family)
352{
353 int err = -EINVAL;
354
355 if (family->id && family->id < GENL_MIN_ID)
356 goto errout;
357
358 if (family->id > GENL_MAX_ID)
359 goto errout;
360
361 INIT_LIST_HEAD(&family->ops_list);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700362 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100363
364 genl_lock();
365
366 if (genl_family_find_byname(family->name)) {
367 err = -EEXIST;
368 goto errout_locked;
369 }
370
Thomas Graf482a8522005-11-10 02:25:56 +0100371 if (family->id == GENL_ID_GENERATE) {
372 u16 newid = genl_generate_id();
373
374 if (!newid) {
375 err = -ENOMEM;
376 goto errout_locked;
377 }
378
379 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000380 } else if (genl_family_find_byid(family->id)) {
381 err = -EEXIST;
382 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100383 }
384
385 if (family->maxattr) {
386 family->attrbuf = kmalloc((family->maxattr+1) *
387 sizeof(struct nlattr *), GFP_KERNEL);
388 if (family->attrbuf == NULL) {
389 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800390 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100391 }
392 } else
393 family->attrbuf = NULL;
394
395 list_add_tail(&family->family_list, genl_family_chain(family->id));
396 genl_unlock();
397
398 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
399
400 return 0;
401
402errout_locked:
403 genl_unlock();
404errout:
405 return err;
406}
407
408/**
Michał Mirosława7b11d72009-05-21 10:34:04 +0000409 * genl_register_family_with_ops - register a generic netlink family
410 * @family: generic netlink family
411 * @ops: operations to be registered
412 * @n_ops: number of elements to register
413 *
414 * Registers the specified family and operations from the specified table.
415 * Only one family may be registered with the same family name or identifier.
416 *
417 * The family id may equal GENL_ID_GENERATE causing an unique id to
418 * be automatically generated and assigned.
419 *
420 * Either a doit or dumpit callback must be specified for every registered
421 * operation or the function will fail. Only one operation structure per
422 * command identifier may be registered.
423 *
424 * See include/net/genetlink.h for more documenation on the operations
425 * structure.
426 *
427 * This is equivalent to calling genl_register_family() followed by
428 * genl_register_ops() for every operation entry in the table taking
429 * care to unregister the family on error path.
430 *
431 * Return 0 on success or a negative error code.
432 */
433int genl_register_family_with_ops(struct genl_family *family,
434 struct genl_ops *ops, size_t n_ops)
435{
436 int err, i;
437
438 err = genl_register_family(family);
439 if (err)
440 return err;
441
442 for (i = 0; i < n_ops; ++i, ++ops) {
443 err = genl_register_ops(family, ops);
444 if (err)
445 goto err_out;
446 }
447 return 0;
448err_out:
449 genl_unregister_family(family);
450 return err;
451}
452EXPORT_SYMBOL(genl_register_family_with_ops);
453
454/**
Thomas Graf482a8522005-11-10 02:25:56 +0100455 * genl_unregister_family - unregister generic netlink family
456 * @family: generic netlink family
457 *
458 * Unregisters the specified family.
459 *
460 * Returns 0 on success or a negative error code.
461 */
462int genl_unregister_family(struct genl_family *family)
463{
464 struct genl_family *rc;
465
466 genl_lock();
467
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800468 genl_unregister_mc_groups(family);
469
Thomas Graf482a8522005-11-10 02:25:56 +0100470 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
471 if (family->id != rc->id || strcmp(rc->name, family->name))
472 continue;
473
474 list_del(&rc->family_list);
475 INIT_LIST_HEAD(&family->ops_list);
476 genl_unlock();
477
Thomas Graf482a8522005-11-10 02:25:56 +0100478 kfree(family->attrbuf);
479 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
480 return 0;
481 }
482
483 genl_unlock();
484
485 return -ENOENT;
486}
487
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700488static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100489{
490 struct genl_ops *ops;
491 struct genl_family *family;
Johannes Berg134e6372009-07-10 09:51:34 +0000492 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100493 struct genl_info info;
494 struct genlmsghdr *hdr = nlmsg_data(nlh);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700495 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100496
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900497 family = genl_family_find_byid(nlh->nlmsg_type);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700498 if (family == NULL)
499 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100500
Johannes Berg134e6372009-07-10 09:51:34 +0000501 /* this family doesn't exist in this netns */
502 if (!family->netnsok && !net_eq(net, &init_net))
503 return -ENOENT;
504
Thomas Graf482a8522005-11-10 02:25:56 +0100505 hdrlen = GENL_HDRLEN + family->hdrsize;
506 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700507 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100508
509 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700510 if (ops == NULL)
511 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100512
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700513 if ((ops->flags & GENL_ADMIN_PERM) &&
514 security_netlink_recv(skb, CAP_NET_ADMIN))
515 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100516
517 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700518 if (ops->dumpit == NULL)
519 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100520
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700521 genl_unlock();
Johannes Berg134e6372009-07-10 09:51:34 +0000522 err = netlink_dump_start(net->genl_sock, skb, nlh,
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700523 ops->dumpit, ops->done);
524 genl_lock();
525 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100526 }
527
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700528 if (ops->doit == NULL)
529 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100530
531 if (family->attrbuf) {
532 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
533 ops->policy);
534 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700535 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100536 }
537
538 info.snd_seq = nlh->nlmsg_seq;
539 info.snd_pid = NETLINK_CB(skb).pid;
540 info.nlhdr = nlh;
541 info.genlhdr = nlmsg_data(nlh);
542 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
543 info.attrs = family->attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000544 genl_info_net_set(&info, net);
Thomas Graf482a8522005-11-10 02:25:56 +0100545
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700546 return ops->doit(skb, &info);
Thomas Graf482a8522005-11-10 02:25:56 +0100547}
548
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700549static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100550{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700551 genl_lock();
552 netlink_rcv_skb(skb, &genl_rcv_msg);
553 genl_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100554}
555
556/**************************************************************************
557 * Controller
558 **************************************************************************/
559
Thomas Graf17c157c2006-11-14 19:46:02 -0800560static struct genl_family genl_ctrl = {
561 .id = GENL_ID_CTRL,
562 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800563 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800564 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000565 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800566};
567
Thomas Graf482a8522005-11-10 02:25:56 +0100568static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
569 u32 flags, struct sk_buff *skb, u8 cmd)
570{
571 void *hdr;
572
Thomas Graf17c157c2006-11-14 19:46:02 -0800573 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100574 if (hdr == NULL)
575 return -1;
576
577 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
578 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
Thomas Grafeb328112006-09-18 00:01:59 -0700579 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
580 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
581 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
582
Thomas Grafe94ef682006-11-23 11:44:37 -0800583 if (!list_empty(&family->ops_list)) {
584 struct nlattr *nla_ops;
585 struct genl_ops *ops;
586 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700587
Thomas Grafe94ef682006-11-23 11:44:37 -0800588 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
589 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700590 goto nla_put_failure;
591
Thomas Grafe94ef682006-11-23 11:44:37 -0800592 list_for_each_entry(ops, &family->ops_list, ops_list) {
593 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700594
Thomas Grafe94ef682006-11-23 11:44:37 -0800595 nest = nla_nest_start(skb, idx++);
596 if (nest == NULL)
597 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700598
Thomas Grafe94ef682006-11-23 11:44:37 -0800599 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
600 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
Thomas Grafeb328112006-09-18 00:01:59 -0700601
Thomas Grafe94ef682006-11-23 11:44:37 -0800602 nla_nest_end(skb, nest);
603 }
604
605 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700606 }
607
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700608 if (!list_empty(&family->mcast_groups)) {
609 struct genl_multicast_group *grp;
610 struct nlattr *nla_grps;
611 int idx = 1;
612
613 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
614 if (nla_grps == NULL)
615 goto nla_put_failure;
616
617 list_for_each_entry(grp, &family->mcast_groups, list) {
618 struct nlattr *nest;
619
620 nest = nla_nest_start(skb, idx++);
621 if (nest == NULL)
622 goto nla_put_failure;
623
624 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
625 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
626 grp->name);
627
628 nla_nest_end(skb, nest);
629 }
630 nla_nest_end(skb, nla_grps);
631 }
632
633 return genlmsg_end(skb, hdr);
634
635nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700636 genlmsg_cancel(skb, hdr);
637 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700638}
639
640static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
641 u32 seq, u32 flags, struct sk_buff *skb,
642 u8 cmd)
643{
644 void *hdr;
645 struct nlattr *nla_grps;
646 struct nlattr *nest;
647
648 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
649 if (hdr == NULL)
650 return -1;
651
652 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
653 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
654
655 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
656 if (nla_grps == NULL)
657 goto nla_put_failure;
658
659 nest = nla_nest_start(skb, 1);
660 if (nest == NULL)
661 goto nla_put_failure;
662
663 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
664 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
665 grp->name);
666
667 nla_nest_end(skb, nest);
668 nla_nest_end(skb, nla_grps);
669
Thomas Graf482a8522005-11-10 02:25:56 +0100670 return genlmsg_end(skb, hdr);
671
672nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700673 genlmsg_cancel(skb, hdr);
674 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100675}
676
677static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
678{
679
680 int i, n = 0;
681 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000682 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100683 int chains_to_skip = cb->args[0];
684 int fams_to_skip = cb->args[1];
685
Samir Bellabese1d5a012010-01-07 22:10:56 +0000686 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100687 n = 0;
688 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000689 if (!rt->netnsok && !net_eq(net, &init_net))
690 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100691 if (++n < fams_to_skip)
692 continue;
693 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
694 cb->nlh->nlmsg_seq, NLM_F_MULTI,
695 skb, CTRL_CMD_NEWFAMILY) < 0)
696 goto errout;
697 }
698
699 fams_to_skip = 0;
700 }
701
702errout:
703 cb->args[0] = i;
704 cb->args[1] = n;
705
706 return skb->len;
707}
708
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700709static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
710 u32 pid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100711{
712 struct sk_buff *skb;
713 int err;
714
Thomas Graf339bf982006-11-10 14:10:15 -0800715 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100716 if (skb == NULL)
717 return ERR_PTR(-ENOBUFS);
718
719 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
720 if (err < 0) {
721 nlmsg_free(skb);
722 return ERR_PTR(err);
723 }
724
725 return skb;
726}
727
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700728static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
729 u32 pid, int seq, u8 cmd)
730{
731 struct sk_buff *skb;
732 int err;
733
734 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
735 if (skb == NULL)
736 return ERR_PTR(-ENOBUFS);
737
738 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
739 if (err < 0) {
740 nlmsg_free(skb);
741 return ERR_PTR(err);
742 }
743
744 return skb;
745}
746
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700747static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100748 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700749 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
750 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100751};
752
753static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
754{
755 struct sk_buff *msg;
756 struct genl_family *res = NULL;
757 int err = -EINVAL;
758
759 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
760 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
761 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000762 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100763 }
764
765 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700766 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100767
Thomas Graf5176f912006-08-26 20:13:18 -0700768 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100769 res = genl_family_find_byname(name);
Johannes Berg134e6372009-07-10 09:51:34 +0000770 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100771 }
772
Johannes Berg134e6372009-07-10 09:51:34 +0000773 if (res == NULL)
774 return err;
775
776 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
777 /* family doesn't exist here */
778 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100779 }
780
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700781 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
782 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000783 if (IS_ERR(msg))
784 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100785
Johannes Berg134e6372009-07-10 09:51:34 +0000786 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100787}
788
789static int genl_ctrl_event(int event, void *data)
790{
791 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000792 struct genl_family *family;
793 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100794
Johannes Berg134e6372009-07-10 09:51:34 +0000795 /* genl is still initialising */
796 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100797 return 0;
798
799 switch (event) {
800 case CTRL_CMD_NEWFAMILY:
801 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000802 family = data;
803 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700804 break;
805 case CTRL_CMD_NEWMCAST_GRP:
806 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000807 grp = data;
808 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700809 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100810 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000811 default:
812 return -EINVAL;
813 }
814
815 if (IS_ERR(msg))
816 return PTR_ERR(msg);
817
818 if (!family->netnsok) {
819 genlmsg_multicast_netns(&init_net, msg, 0,
820 GENL_ID_CTRL, GFP_KERNEL);
821 } else {
822 rcu_read_lock();
823 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
824 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100825 }
826
827 return 0;
828}
829
830static struct genl_ops genl_ctrl_ops = {
831 .cmd = CTRL_CMD_GETFAMILY,
832 .doit = ctrl_getfamily,
833 .dumpit = ctrl_dumpfamily,
834 .policy = ctrl_policy,
835};
836
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700837static struct genl_multicast_group notify_grp = {
838 .name = "notify",
839};
840
Johannes Berg134e6372009-07-10 09:51:34 +0000841static int __net_init genl_pernet_init(struct net *net)
842{
843 /* we'll bump the group number right afterwards */
844 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
845 genl_rcv, &genl_mutex,
846 THIS_MODULE);
847
848 if (!net->genl_sock && net_eq(net, &init_net))
849 panic("GENL: Cannot initialize generic netlink\n");
850
851 if (!net->genl_sock)
852 return -ENOMEM;
853
854 return 0;
855}
856
857static void __net_exit genl_pernet_exit(struct net *net)
858{
859 netlink_kernel_release(net->genl_sock);
860 net->genl_sock = NULL;
861}
862
863static struct pernet_operations genl_pernet_ops = {
864 .init = genl_pernet_init,
865 .exit = genl_pernet_exit,
866};
867
Thomas Graf482a8522005-11-10 02:25:56 +0100868static int __init genl_init(void)
869{
870 int i, err;
871
872 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
873 INIT_LIST_HEAD(&family_ht[i]);
874
875 err = genl_register_family(&genl_ctrl);
876 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000877 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100878
879 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
880 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000881 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100882
883 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700884
Johannes Berg134e6372009-07-10 09:51:34 +0000885 err = register_pernet_subsys(&genl_pernet_ops);
886 if (err)
887 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100888
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700889 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
890 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000891 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700892
Thomas Graf482a8522005-11-10 02:25:56 +0100893 return 0;
894
Johannes Berg134e6372009-07-10 09:51:34 +0000895problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100896 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100897}
898
899subsys_initcall(genl_init);
900
Thomas Graf482a8522005-11-10 02:25:56 +0100901EXPORT_SYMBOL(genl_register_ops);
902EXPORT_SYMBOL(genl_unregister_ops);
903EXPORT_SYMBOL(genl_register_family);
904EXPORT_SYMBOL(genl_unregister_family);
Johannes Berg134e6372009-07-10 09:51:34 +0000905
906static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
907 gfp_t flags)
908{
909 struct sk_buff *tmp;
910 struct net *net, *prev = NULL;
911 int err;
912
913 for_each_net_rcu(net) {
914 if (prev) {
915 tmp = skb_clone(skb, flags);
916 if (!tmp) {
917 err = -ENOMEM;
918 goto error;
919 }
920 err = nlmsg_multicast(prev->genl_sock, tmp,
921 pid, group, flags);
922 if (err)
923 goto error;
924 }
925
926 prev = net;
927 }
928
929 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
930 error:
931 kfree_skb(skb);
932 return err;
933}
934
935int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
936 gfp_t flags)
937{
938 return genlmsg_mcast(skb, pid, group, flags);
939}
940EXPORT_SYMBOL(genlmsg_multicast_allns);