blob: aa4308afcc7f07168d9317235e795218d878acdd [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Thomas Graf482a8522005-11-10 02:25:56 +010012#include <linux/errno.h>
13#include <linux/types.h>
14#include <linux/socket.h>
15#include <linux/string.h>
16#include <linux/skbuff.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080017#include <linux/mutex.h>
Johannes Berg2dbba6f2007-07-18 15:47:52 -070018#include <linux/bitmap.h>
Thomas Graf482a8522005-11-10 02:25:56 +010019#include <net/sock.h>
20#include <net/genetlink.h>
21
Ingo Molnar14cc3e22006-03-26 01:37:14 -080022static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
Thomas Graf482a8522005-11-10 02:25:56 +010023
James Chapmanf408e0c2010-04-02 06:19:05 +000024void genl_lock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010025{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080026 mutex_lock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010027}
James Chapmanf408e0c2010-04-02 06:19:05 +000028EXPORT_SYMBOL(genl_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010029
James Chapmanf408e0c2010-04-02 06:19:05 +000030void genl_unlock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010031{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080032 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010033}
James Chapmanf408e0c2010-04-02 06:19:05 +000034EXPORT_SYMBOL(genl_unlock);
Thomas Graf482a8522005-11-10 02:25:56 +010035
36#define GENL_FAM_TAB_SIZE 16
37#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
38
39static struct list_head family_ht[GENL_FAM_TAB_SIZE];
Johannes Berg2dbba6f2007-07-18 15:47:52 -070040/*
41 * Bitmap of multicast groups that are currently in use.
42 *
43 * To avoid an allocation at boot of just one unsigned long,
44 * declare it global instead.
45 * Bit 0 is marked as already used since group 0 is invalid.
46 */
47static unsigned long mc_group_start = 0x1;
48static unsigned long *mc_groups = &mc_group_start;
49static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010050
51static int genl_ctrl_event(int event, void *data);
52
53static inline unsigned int genl_family_hash(unsigned int id)
54{
55 return id & GENL_FAM_TAB_MASK;
56}
57
58static inline struct list_head *genl_family_chain(unsigned int id)
59{
60 return &family_ht[genl_family_hash(id)];
61}
62
63static struct genl_family *genl_family_find_byid(unsigned int id)
64{
65 struct genl_family *f;
66
67 list_for_each_entry(f, genl_family_chain(id), family_list)
68 if (f->id == id)
69 return f;
70
71 return NULL;
72}
73
74static struct genl_family *genl_family_find_byname(char *name)
75{
76 struct genl_family *f;
77 int i;
78
79 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
80 list_for_each_entry(f, genl_family_chain(i), family_list)
81 if (strcmp(f->name, name) == 0)
82 return f;
83
84 return NULL;
85}
86
87static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
88{
89 struct genl_ops *ops;
90
91 list_for_each_entry(ops, &family->ops_list, ops_list)
92 if (ops->cmd == cmd)
93 return ops;
94
95 return NULL;
96}
97
98/* Of course we are going to have problems once we hit
99 * 2^16 alive types, but that can only happen by year 2K
100*/
101static inline u16 genl_generate_id(void)
102{
Krishna Kumar988ade62009-10-14 19:55:07 +0000103 static u16 id_gen_idx = GENL_MIN_ID;
104 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100105
Krishna Kumar988ade62009-10-14 19:55:07 +0000106 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
107 if (!genl_family_find_byid(id_gen_idx))
108 return id_gen_idx;
109 if (++id_gen_idx > GENL_MAX_ID)
Thomas Graf482a8522005-11-10 02:25:56 +0100110 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000111 }
Thomas Graf482a8522005-11-10 02:25:56 +0100112
Krishna Kumar988ade62009-10-14 19:55:07 +0000113 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100114}
115
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700116static struct genl_multicast_group notify_grp;
117
118/**
119 * genl_register_mc_group - register a multicast group
120 *
121 * Registers the specified multicast group and notifies userspace
122 * about the new group.
123 *
124 * Returns 0 on success or a negative error code.
125 *
126 * @family: The generic netlink family the group shall be registered for.
127 * @grp: The group to register, must have a name.
128 */
129int genl_register_mc_group(struct genl_family *family,
130 struct genl_multicast_group *grp)
131{
132 int id;
133 unsigned long *new_groups;
Brian Haleyb1f57192009-09-04 20:36:52 -0700134 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700135
136 BUG_ON(grp->name[0] == '\0');
137
138 genl_lock();
139
140 /* special-case our own group */
141 if (grp == &notify_grp)
142 id = GENL_ID_CTRL;
143 else
144 id = find_first_zero_bit(mc_groups,
145 mc_groups_longs * BITS_PER_LONG);
146
147
148 if (id >= mc_groups_longs * BITS_PER_LONG) {
149 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
150
151 if (mc_groups == &mc_group_start) {
152 new_groups = kzalloc(nlen, GFP_KERNEL);
153 if (!new_groups) {
154 err = -ENOMEM;
155 goto out;
156 }
157 mc_groups = new_groups;
158 *mc_groups = mc_group_start;
159 } else {
160 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
161 if (!new_groups) {
162 err = -ENOMEM;
163 goto out;
164 }
165 mc_groups = new_groups;
166 mc_groups[mc_groups_longs] = 0;
167 }
168 mc_groups_longs++;
169 }
170
Johannes Berg134e6372009-07-10 09:51:34 +0000171 if (family->netnsok) {
172 struct net *net;
173
Johannes Bergd136f1b2009-09-12 03:03:15 +0000174 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000175 rcu_read_lock();
176 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000177 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000178 mc_groups_longs * BITS_PER_LONG);
179 if (err) {
180 /*
181 * No need to roll back, can only fail if
182 * memory allocation fails and then the
183 * number of _possible_ groups has been
184 * increased on some sockets which is ok.
185 */
186 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000187 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000188 goto out;
189 }
190 }
191 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000192 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000193 } else {
194 err = netlink_change_ngroups(init_net.genl_sock,
195 mc_groups_longs * BITS_PER_LONG);
196 if (err)
197 goto out;
198 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700199
200 grp->id = id;
201 set_bit(id, mc_groups);
202 list_add_tail(&grp->list, &family->mcast_groups);
203 grp->family = family;
204
205 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
206 out:
207 genl_unlock();
Thomas Graf79d310d2007-07-24 15:34:53 -0700208 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700209}
210EXPORT_SYMBOL(genl_register_mc_group);
211
Thomas Graf79dc43862007-07-24 15:32:46 -0700212static void __genl_unregister_mc_group(struct genl_family *family,
213 struct genl_multicast_group *grp)
214{
Johannes Berg134e6372009-07-10 09:51:34 +0000215 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700216 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000217
Johannes Bergb8273572009-09-24 15:44:05 -0700218 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000219 rcu_read_lock();
220 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700221 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000222 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700223 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000224
Thomas Graf79dc43862007-07-24 15:32:46 -0700225 clear_bit(grp->id, mc_groups);
226 list_del(&grp->list);
227 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
228 grp->id = 0;
229 grp->family = NULL;
230}
231
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700232/**
233 * genl_unregister_mc_group - unregister a multicast group
234 *
235 * Unregisters the specified multicast group and notifies userspace
236 * about it. All current listeners on the group are removed.
237 *
238 * Note: It is not necessary to unregister all multicast groups before
239 * unregistering the family, unregistering the family will cause
240 * all assigned multicast groups to be unregistered automatically.
241 *
242 * @family: Generic netlink family the group belongs to.
243 * @grp: The group to unregister, must have been registered successfully
244 * previously.
245 */
246void genl_unregister_mc_group(struct genl_family *family,
247 struct genl_multicast_group *grp)
248{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700249 genl_lock();
Thomas Graf79dc43862007-07-24 15:32:46 -0700250 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700251 genl_unlock();
252}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800253EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700254
255static void genl_unregister_mc_groups(struct genl_family *family)
256{
257 struct genl_multicast_group *grp, *tmp;
258
259 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700260 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700261}
262
Thomas Graf482a8522005-11-10 02:25:56 +0100263/**
264 * genl_register_ops - register generic netlink operations
265 * @family: generic netlink family
266 * @ops: operations to be registered
267 *
268 * Registers the specified operations and assigns them to the specified
269 * family. Either a doit or dumpit callback must be specified or the
270 * operation will fail. Only one operation structure per command
271 * identifier may be registered.
272 *
273 * See include/net/genetlink.h for more documenation on the operations
274 * structure.
275 *
276 * Returns 0 on success or a negative error code.
277 */
278int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
279{
280 int err = -EINVAL;
281
282 if (ops->dumpit == NULL && ops->doit == NULL)
283 goto errout;
284
285 if (genl_get_cmd(ops->cmd, family)) {
286 err = -EEXIST;
287 goto errout;
288 }
289
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800290 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800291 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800292 if (ops->doit)
293 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800294 if (ops->policy)
295 ops->flags |= GENL_CMD_CAP_HASPOL;
296
Thomas Graf482a8522005-11-10 02:25:56 +0100297 genl_lock();
298 list_add_tail(&ops->ops_list, &family->ops_list);
299 genl_unlock();
300
301 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
302 err = 0;
303errout:
304 return err;
305}
306
307/**
308 * genl_unregister_ops - unregister generic netlink operations
309 * @family: generic netlink family
310 * @ops: operations to be unregistered
311 *
312 * Unregisters the specified operations and unassigns them from the
313 * specified family. The operation blocks until the current message
314 * processing has finished and doesn't start again until the
315 * unregister process has finished.
316 *
317 * Note: It is not necessary to unregister all operations before
318 * unregistering the family, unregistering the family will cause
319 * all assigned operations to be unregistered automatically.
320 *
321 * Returns 0 on success or a negative error code.
322 */
323int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
324{
325 struct genl_ops *rc;
326
327 genl_lock();
328 list_for_each_entry(rc, &family->ops_list, ops_list) {
329 if (rc == ops) {
330 list_del(&ops->ops_list);
331 genl_unlock();
332 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
333 return 0;
334 }
335 }
336 genl_unlock();
337
338 return -ENOENT;
339}
340
341/**
342 * genl_register_family - register a generic netlink family
343 * @family: generic netlink family
344 *
345 * Registers the specified family after validating it first. Only one
346 * family may be registered with the same family name or identifier.
347 * The family id may equal GENL_ID_GENERATE causing an unique id to
348 * be automatically generated and assigned.
349 *
350 * Return 0 on success or a negative error code.
351 */
352int genl_register_family(struct genl_family *family)
353{
354 int err = -EINVAL;
355
356 if (family->id && family->id < GENL_MIN_ID)
357 goto errout;
358
359 if (family->id > GENL_MAX_ID)
360 goto errout;
361
362 INIT_LIST_HEAD(&family->ops_list);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700363 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100364
365 genl_lock();
366
367 if (genl_family_find_byname(family->name)) {
368 err = -EEXIST;
369 goto errout_locked;
370 }
371
Thomas Graf482a8522005-11-10 02:25:56 +0100372 if (family->id == GENL_ID_GENERATE) {
373 u16 newid = genl_generate_id();
374
375 if (!newid) {
376 err = -ENOMEM;
377 goto errout_locked;
378 }
379
380 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000381 } else if (genl_family_find_byid(family->id)) {
382 err = -EEXIST;
383 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100384 }
385
386 if (family->maxattr) {
387 family->attrbuf = kmalloc((family->maxattr+1) *
388 sizeof(struct nlattr *), GFP_KERNEL);
389 if (family->attrbuf == NULL) {
390 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800391 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100392 }
393 } else
394 family->attrbuf = NULL;
395
396 list_add_tail(&family->family_list, genl_family_chain(family->id));
397 genl_unlock();
398
399 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
400
401 return 0;
402
403errout_locked:
404 genl_unlock();
405errout:
406 return err;
407}
408
409/**
Michał Mirosława7b11d72009-05-21 10:34:04 +0000410 * genl_register_family_with_ops - register a generic netlink family
411 * @family: generic netlink family
412 * @ops: operations to be registered
413 * @n_ops: number of elements to register
414 *
415 * Registers the specified family and operations from the specified table.
416 * Only one family may be registered with the same family name or identifier.
417 *
418 * The family id may equal GENL_ID_GENERATE causing an unique id to
419 * be automatically generated and assigned.
420 *
421 * Either a doit or dumpit callback must be specified for every registered
422 * operation or the function will fail. Only one operation structure per
423 * command identifier may be registered.
424 *
425 * See include/net/genetlink.h for more documenation on the operations
426 * structure.
427 *
428 * This is equivalent to calling genl_register_family() followed by
429 * genl_register_ops() for every operation entry in the table taking
430 * care to unregister the family on error path.
431 *
432 * Return 0 on success or a negative error code.
433 */
434int genl_register_family_with_ops(struct genl_family *family,
435 struct genl_ops *ops, size_t n_ops)
436{
437 int err, i;
438
439 err = genl_register_family(family);
440 if (err)
441 return err;
442
443 for (i = 0; i < n_ops; ++i, ++ops) {
444 err = genl_register_ops(family, ops);
445 if (err)
446 goto err_out;
447 }
448 return 0;
449err_out:
450 genl_unregister_family(family);
451 return err;
452}
453EXPORT_SYMBOL(genl_register_family_with_ops);
454
455/**
Thomas Graf482a8522005-11-10 02:25:56 +0100456 * genl_unregister_family - unregister generic netlink family
457 * @family: generic netlink family
458 *
459 * Unregisters the specified family.
460 *
461 * Returns 0 on success or a negative error code.
462 */
463int genl_unregister_family(struct genl_family *family)
464{
465 struct genl_family *rc;
466
467 genl_lock();
468
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800469 genl_unregister_mc_groups(family);
470
Thomas Graf482a8522005-11-10 02:25:56 +0100471 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
472 if (family->id != rc->id || strcmp(rc->name, family->name))
473 continue;
474
475 list_del(&rc->family_list);
476 INIT_LIST_HEAD(&family->ops_list);
477 genl_unlock();
478
Thomas Graf482a8522005-11-10 02:25:56 +0100479 kfree(family->attrbuf);
480 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
481 return 0;
482 }
483
484 genl_unlock();
485
486 return -ENOENT;
487}
488
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700489static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100490{
491 struct genl_ops *ops;
492 struct genl_family *family;
Johannes Berg134e6372009-07-10 09:51:34 +0000493 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100494 struct genl_info info;
495 struct genlmsghdr *hdr = nlmsg_data(nlh);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700496 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100497
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900498 family = genl_family_find_byid(nlh->nlmsg_type);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700499 if (family == NULL)
500 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100501
Johannes Berg134e6372009-07-10 09:51:34 +0000502 /* this family doesn't exist in this netns */
503 if (!family->netnsok && !net_eq(net, &init_net))
504 return -ENOENT;
505
Thomas Graf482a8522005-11-10 02:25:56 +0100506 hdrlen = GENL_HDRLEN + family->hdrsize;
507 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700508 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100509
510 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700511 if (ops == NULL)
512 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100513
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700514 if ((ops->flags & GENL_ADMIN_PERM) &&
515 security_netlink_recv(skb, CAP_NET_ADMIN))
516 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100517
518 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700519 if (ops->dumpit == NULL)
520 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100521
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700522 genl_unlock();
Johannes Berg134e6372009-07-10 09:51:34 +0000523 err = netlink_dump_start(net->genl_sock, skb, nlh,
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700524 ops->dumpit, ops->done);
525 genl_lock();
526 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100527 }
528
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700529 if (ops->doit == NULL)
530 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100531
532 if (family->attrbuf) {
533 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
534 ops->policy);
535 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700536 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100537 }
538
539 info.snd_seq = nlh->nlmsg_seq;
540 info.snd_pid = NETLINK_CB(skb).pid;
541 info.nlhdr = nlh;
542 info.genlhdr = nlmsg_data(nlh);
543 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
544 info.attrs = family->attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000545 genl_info_net_set(&info, net);
Thomas Graf482a8522005-11-10 02:25:56 +0100546
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700547 return ops->doit(skb, &info);
Thomas Graf482a8522005-11-10 02:25:56 +0100548}
549
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700550static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100551{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700552 genl_lock();
553 netlink_rcv_skb(skb, &genl_rcv_msg);
554 genl_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100555}
556
557/**************************************************************************
558 * Controller
559 **************************************************************************/
560
Thomas Graf17c157c2006-11-14 19:46:02 -0800561static struct genl_family genl_ctrl = {
562 .id = GENL_ID_CTRL,
563 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800564 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800565 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000566 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800567};
568
Thomas Graf482a8522005-11-10 02:25:56 +0100569static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
570 u32 flags, struct sk_buff *skb, u8 cmd)
571{
572 void *hdr;
573
Thomas Graf17c157c2006-11-14 19:46:02 -0800574 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100575 if (hdr == NULL)
576 return -1;
577
578 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
579 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
Thomas Grafeb328112006-09-18 00:01:59 -0700580 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
581 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
582 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
583
Thomas Grafe94ef682006-11-23 11:44:37 -0800584 if (!list_empty(&family->ops_list)) {
585 struct nlattr *nla_ops;
586 struct genl_ops *ops;
587 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700588
Thomas Grafe94ef682006-11-23 11:44:37 -0800589 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
590 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700591 goto nla_put_failure;
592
Thomas Grafe94ef682006-11-23 11:44:37 -0800593 list_for_each_entry(ops, &family->ops_list, ops_list) {
594 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700595
Thomas Grafe94ef682006-11-23 11:44:37 -0800596 nest = nla_nest_start(skb, idx++);
597 if (nest == NULL)
598 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700599
Thomas Grafe94ef682006-11-23 11:44:37 -0800600 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
601 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
Thomas Grafeb328112006-09-18 00:01:59 -0700602
Thomas Grafe94ef682006-11-23 11:44:37 -0800603 nla_nest_end(skb, nest);
604 }
605
606 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700607 }
608
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700609 if (!list_empty(&family->mcast_groups)) {
610 struct genl_multicast_group *grp;
611 struct nlattr *nla_grps;
612 int idx = 1;
613
614 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
615 if (nla_grps == NULL)
616 goto nla_put_failure;
617
618 list_for_each_entry(grp, &family->mcast_groups, list) {
619 struct nlattr *nest;
620
621 nest = nla_nest_start(skb, idx++);
622 if (nest == NULL)
623 goto nla_put_failure;
624
625 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
626 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
627 grp->name);
628
629 nla_nest_end(skb, nest);
630 }
631 nla_nest_end(skb, nla_grps);
632 }
633
634 return genlmsg_end(skb, hdr);
635
636nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700637 genlmsg_cancel(skb, hdr);
638 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700639}
640
641static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
642 u32 seq, u32 flags, struct sk_buff *skb,
643 u8 cmd)
644{
645 void *hdr;
646 struct nlattr *nla_grps;
647 struct nlattr *nest;
648
649 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
650 if (hdr == NULL)
651 return -1;
652
653 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
654 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
655
656 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
657 if (nla_grps == NULL)
658 goto nla_put_failure;
659
660 nest = nla_nest_start(skb, 1);
661 if (nest == NULL)
662 goto nla_put_failure;
663
664 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
665 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
666 grp->name);
667
668 nla_nest_end(skb, nest);
669 nla_nest_end(skb, nla_grps);
670
Thomas Graf482a8522005-11-10 02:25:56 +0100671 return genlmsg_end(skb, hdr);
672
673nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700674 genlmsg_cancel(skb, hdr);
675 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100676}
677
678static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
679{
680
681 int i, n = 0;
682 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000683 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100684 int chains_to_skip = cb->args[0];
685 int fams_to_skip = cb->args[1];
686
Samir Bellabese1d5a012010-01-07 22:10:56 +0000687 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100688 n = 0;
689 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000690 if (!rt->netnsok && !net_eq(net, &init_net))
691 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100692 if (++n < fams_to_skip)
693 continue;
694 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
695 cb->nlh->nlmsg_seq, NLM_F_MULTI,
696 skb, CTRL_CMD_NEWFAMILY) < 0)
697 goto errout;
698 }
699
700 fams_to_skip = 0;
701 }
702
703errout:
704 cb->args[0] = i;
705 cb->args[1] = n;
706
707 return skb->len;
708}
709
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700710static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
711 u32 pid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100712{
713 struct sk_buff *skb;
714 int err;
715
Thomas Graf339bf982006-11-10 14:10:15 -0800716 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100717 if (skb == NULL)
718 return ERR_PTR(-ENOBUFS);
719
720 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
721 if (err < 0) {
722 nlmsg_free(skb);
723 return ERR_PTR(err);
724 }
725
726 return skb;
727}
728
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700729static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
730 u32 pid, int seq, u8 cmd)
731{
732 struct sk_buff *skb;
733 int err;
734
735 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
736 if (skb == NULL)
737 return ERR_PTR(-ENOBUFS);
738
739 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
740 if (err < 0) {
741 nlmsg_free(skb);
742 return ERR_PTR(err);
743 }
744
745 return skb;
746}
747
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700748static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100749 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700750 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
751 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100752};
753
754static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
755{
756 struct sk_buff *msg;
757 struct genl_family *res = NULL;
758 int err = -EINVAL;
759
760 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
761 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
762 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000763 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100764 }
765
766 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700767 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100768
Thomas Graf5176f912006-08-26 20:13:18 -0700769 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100770 res = genl_family_find_byname(name);
Johannes Berg134e6372009-07-10 09:51:34 +0000771 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100772 }
773
Johannes Berg134e6372009-07-10 09:51:34 +0000774 if (res == NULL)
775 return err;
776
777 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
778 /* family doesn't exist here */
779 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100780 }
781
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700782 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
783 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000784 if (IS_ERR(msg))
785 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100786
Johannes Berg134e6372009-07-10 09:51:34 +0000787 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100788}
789
790static int genl_ctrl_event(int event, void *data)
791{
792 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000793 struct genl_family *family;
794 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100795
Johannes Berg134e6372009-07-10 09:51:34 +0000796 /* genl is still initialising */
797 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100798 return 0;
799
800 switch (event) {
801 case CTRL_CMD_NEWFAMILY:
802 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000803 family = data;
804 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700805 break;
806 case CTRL_CMD_NEWMCAST_GRP:
807 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000808 grp = data;
809 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700810 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100811 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000812 default:
813 return -EINVAL;
814 }
815
816 if (IS_ERR(msg))
817 return PTR_ERR(msg);
818
819 if (!family->netnsok) {
820 genlmsg_multicast_netns(&init_net, msg, 0,
821 GENL_ID_CTRL, GFP_KERNEL);
822 } else {
823 rcu_read_lock();
824 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
825 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100826 }
827
828 return 0;
829}
830
831static struct genl_ops genl_ctrl_ops = {
832 .cmd = CTRL_CMD_GETFAMILY,
833 .doit = ctrl_getfamily,
834 .dumpit = ctrl_dumpfamily,
835 .policy = ctrl_policy,
836};
837
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700838static struct genl_multicast_group notify_grp = {
839 .name = "notify",
840};
841
Johannes Berg134e6372009-07-10 09:51:34 +0000842static int __net_init genl_pernet_init(struct net *net)
843{
844 /* we'll bump the group number right afterwards */
845 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
846 genl_rcv, &genl_mutex,
847 THIS_MODULE);
848
849 if (!net->genl_sock && net_eq(net, &init_net))
850 panic("GENL: Cannot initialize generic netlink\n");
851
852 if (!net->genl_sock)
853 return -ENOMEM;
854
855 return 0;
856}
857
858static void __net_exit genl_pernet_exit(struct net *net)
859{
860 netlink_kernel_release(net->genl_sock);
861 net->genl_sock = NULL;
862}
863
864static struct pernet_operations genl_pernet_ops = {
865 .init = genl_pernet_init,
866 .exit = genl_pernet_exit,
867};
868
Thomas Graf482a8522005-11-10 02:25:56 +0100869static int __init genl_init(void)
870{
871 int i, err;
872
873 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
874 INIT_LIST_HEAD(&family_ht[i]);
875
876 err = genl_register_family(&genl_ctrl);
877 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000878 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100879
880 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
881 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000882 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100883
884 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700885
Johannes Berg134e6372009-07-10 09:51:34 +0000886 err = register_pernet_subsys(&genl_pernet_ops);
887 if (err)
888 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100889
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700890 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
891 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000892 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700893
Thomas Graf482a8522005-11-10 02:25:56 +0100894 return 0;
895
Johannes Berg134e6372009-07-10 09:51:34 +0000896problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100897 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100898}
899
900subsys_initcall(genl_init);
901
Thomas Graf482a8522005-11-10 02:25:56 +0100902EXPORT_SYMBOL(genl_register_ops);
903EXPORT_SYMBOL(genl_unregister_ops);
904EXPORT_SYMBOL(genl_register_family);
905EXPORT_SYMBOL(genl_unregister_family);
Johannes Berg134e6372009-07-10 09:51:34 +0000906
907static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
908 gfp_t flags)
909{
910 struct sk_buff *tmp;
911 struct net *net, *prev = NULL;
912 int err;
913
914 for_each_net_rcu(net) {
915 if (prev) {
916 tmp = skb_clone(skb, flags);
917 if (!tmp) {
918 err = -ENOMEM;
919 goto error;
920 }
921 err = nlmsg_multicast(prev->genl_sock, tmp,
922 pid, group, flags);
923 if (err)
924 goto error;
925 }
926
927 prev = net;
928 }
929
930 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
931 error:
932 kfree_skb(skb);
933 return err;
934}
935
936int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
937 gfp_t flags)
938{
939 return genlmsg_mcast(skb, pid, group, flags);
940}
941EXPORT_SYMBOL(genlmsg_multicast_allns);