blob: a1154717219ef261e0d6a08640346cd83a5c444b [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
Pravin B Shelar86b13092011-11-10 19:14:51 -080036#ifdef CONFIG_PROVE_LOCKING
37int lockdep_genl_is_held(void)
38{
39 return lockdep_is_held(&genl_mutex);
40}
41EXPORT_SYMBOL(lockdep_genl_is_held);
42#endif
43
Thomas Graf482a8522005-11-10 02:25:56 +010044#define GENL_FAM_TAB_SIZE 16
45#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
46
47static struct list_head family_ht[GENL_FAM_TAB_SIZE];
Johannes Berg2dbba6f2007-07-18 15:47:52 -070048/*
49 * Bitmap of multicast groups that are currently in use.
50 *
51 * To avoid an allocation at boot of just one unsigned long,
52 * declare it global instead.
53 * Bit 0 is marked as already used since group 0 is invalid.
54 */
55static unsigned long mc_group_start = 0x1;
56static unsigned long *mc_groups = &mc_group_start;
57static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010058
59static int genl_ctrl_event(int event, void *data);
60
61static inline unsigned int genl_family_hash(unsigned int id)
62{
63 return id & GENL_FAM_TAB_MASK;
64}
65
66static inline struct list_head *genl_family_chain(unsigned int id)
67{
68 return &family_ht[genl_family_hash(id)];
69}
70
71static struct genl_family *genl_family_find_byid(unsigned int id)
72{
73 struct genl_family *f;
74
75 list_for_each_entry(f, genl_family_chain(id), family_list)
76 if (f->id == id)
77 return f;
78
79 return NULL;
80}
81
82static struct genl_family *genl_family_find_byname(char *name)
83{
84 struct genl_family *f;
85 int i;
86
87 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
88 list_for_each_entry(f, genl_family_chain(i), family_list)
89 if (strcmp(f->name, name) == 0)
90 return f;
91
92 return NULL;
93}
94
95static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
96{
97 struct genl_ops *ops;
98
99 list_for_each_entry(ops, &family->ops_list, ops_list)
100 if (ops->cmd == cmd)
101 return ops;
102
103 return NULL;
104}
105
106/* Of course we are going to have problems once we hit
107 * 2^16 alive types, but that can only happen by year 2K
108*/
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000109static u16 genl_generate_id(void)
Thomas Graf482a8522005-11-10 02:25:56 +0100110{
Krishna Kumar988ade62009-10-14 19:55:07 +0000111 static u16 id_gen_idx = GENL_MIN_ID;
112 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100113
Krishna Kumar988ade62009-10-14 19:55:07 +0000114 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
115 if (!genl_family_find_byid(id_gen_idx))
116 return id_gen_idx;
117 if (++id_gen_idx > GENL_MAX_ID)
Thomas Graf482a8522005-11-10 02:25:56 +0100118 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000119 }
Thomas Graf482a8522005-11-10 02:25:56 +0100120
Krishna Kumar988ade62009-10-14 19:55:07 +0000121 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100122}
123
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700124static struct genl_multicast_group notify_grp;
125
126/**
127 * genl_register_mc_group - register a multicast group
128 *
129 * Registers the specified multicast group and notifies userspace
130 * about the new group.
131 *
132 * Returns 0 on success or a negative error code.
133 *
134 * @family: The generic netlink family the group shall be registered for.
135 * @grp: The group to register, must have a name.
136 */
137int genl_register_mc_group(struct genl_family *family,
138 struct genl_multicast_group *grp)
139{
140 int id;
141 unsigned long *new_groups;
Brian Haleyb1f57192009-09-04 20:36:52 -0700142 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700143
144 BUG_ON(grp->name[0] == '\0');
145
146 genl_lock();
147
148 /* special-case our own group */
149 if (grp == &notify_grp)
150 id = GENL_ID_CTRL;
151 else
152 id = find_first_zero_bit(mc_groups,
153 mc_groups_longs * BITS_PER_LONG);
154
155
156 if (id >= mc_groups_longs * BITS_PER_LONG) {
157 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
158
159 if (mc_groups == &mc_group_start) {
160 new_groups = kzalloc(nlen, GFP_KERNEL);
161 if (!new_groups) {
162 err = -ENOMEM;
163 goto out;
164 }
165 mc_groups = new_groups;
166 *mc_groups = mc_group_start;
167 } else {
168 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
169 if (!new_groups) {
170 err = -ENOMEM;
171 goto out;
172 }
173 mc_groups = new_groups;
174 mc_groups[mc_groups_longs] = 0;
175 }
176 mc_groups_longs++;
177 }
178
Johannes Berg134e6372009-07-10 09:51:34 +0000179 if (family->netnsok) {
180 struct net *net;
181
Johannes Bergd136f1b2009-09-12 03:03:15 +0000182 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000183 rcu_read_lock();
184 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000185 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000186 mc_groups_longs * BITS_PER_LONG);
187 if (err) {
188 /*
189 * No need to roll back, can only fail if
190 * memory allocation fails and then the
191 * number of _possible_ groups has been
192 * increased on some sockets which is ok.
193 */
194 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000195 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000196 goto out;
197 }
198 }
199 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000200 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000201 } else {
202 err = netlink_change_ngroups(init_net.genl_sock,
203 mc_groups_longs * BITS_PER_LONG);
204 if (err)
205 goto out;
206 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700207
208 grp->id = id;
209 set_bit(id, mc_groups);
210 list_add_tail(&grp->list, &family->mcast_groups);
211 grp->family = family;
212
213 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
214 out:
215 genl_unlock();
Thomas Graf79d310d2007-07-24 15:34:53 -0700216 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700217}
218EXPORT_SYMBOL(genl_register_mc_group);
219
Thomas Graf79dc43862007-07-24 15:32:46 -0700220static void __genl_unregister_mc_group(struct genl_family *family,
221 struct genl_multicast_group *grp)
222{
Johannes Berg134e6372009-07-10 09:51:34 +0000223 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700224 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000225
Johannes Bergb8273572009-09-24 15:44:05 -0700226 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000227 rcu_read_lock();
228 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700229 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000230 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700231 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000232
Thomas Graf79dc43862007-07-24 15:32:46 -0700233 clear_bit(grp->id, mc_groups);
234 list_del(&grp->list);
235 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
236 grp->id = 0;
237 grp->family = NULL;
238}
239
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700240/**
241 * genl_unregister_mc_group - unregister a multicast group
242 *
243 * Unregisters the specified multicast group and notifies userspace
244 * about it. All current listeners on the group are removed.
245 *
246 * Note: It is not necessary to unregister all multicast groups before
247 * unregistering the family, unregistering the family will cause
248 * all assigned multicast groups to be unregistered automatically.
249 *
250 * @family: Generic netlink family the group belongs to.
251 * @grp: The group to unregister, must have been registered successfully
252 * previously.
253 */
254void genl_unregister_mc_group(struct genl_family *family,
255 struct genl_multicast_group *grp)
256{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700257 genl_lock();
Thomas Graf79dc43862007-07-24 15:32:46 -0700258 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700259 genl_unlock();
260}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800261EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700262
263static void genl_unregister_mc_groups(struct genl_family *family)
264{
265 struct genl_multicast_group *grp, *tmp;
266
267 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700268 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700269}
270
Thomas Graf482a8522005-11-10 02:25:56 +0100271/**
272 * genl_register_ops - register generic netlink operations
273 * @family: generic netlink family
274 * @ops: operations to be registered
275 *
276 * Registers the specified operations and assigns them to the specified
277 * family. Either a doit or dumpit callback must be specified or the
278 * operation will fail. Only one operation structure per command
279 * identifier may be registered.
280 *
281 * See include/net/genetlink.h for more documenation on the operations
282 * structure.
283 *
284 * Returns 0 on success or a negative error code.
285 */
286int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
287{
288 int err = -EINVAL;
289
290 if (ops->dumpit == NULL && ops->doit == NULL)
291 goto errout;
292
293 if (genl_get_cmd(ops->cmd, family)) {
294 err = -EEXIST;
295 goto errout;
296 }
297
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800298 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800299 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800300 if (ops->doit)
301 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800302 if (ops->policy)
303 ops->flags |= GENL_CMD_CAP_HASPOL;
304
Thomas Graf482a8522005-11-10 02:25:56 +0100305 genl_lock();
306 list_add_tail(&ops->ops_list, &family->ops_list);
307 genl_unlock();
308
309 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
310 err = 0;
311errout:
312 return err;
313}
Changli Gao416c2f92010-07-25 20:46:01 +0000314EXPORT_SYMBOL(genl_register_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100315
316/**
317 * genl_unregister_ops - unregister generic netlink operations
318 * @family: generic netlink family
319 * @ops: operations to be unregistered
320 *
321 * Unregisters the specified operations and unassigns them from the
322 * specified family. The operation blocks until the current message
323 * processing has finished and doesn't start again until the
324 * unregister process has finished.
325 *
326 * Note: It is not necessary to unregister all operations before
327 * unregistering the family, unregistering the family will cause
328 * all assigned operations to be unregistered automatically.
329 *
330 * Returns 0 on success or a negative error code.
331 */
332int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
333{
334 struct genl_ops *rc;
335
336 genl_lock();
337 list_for_each_entry(rc, &family->ops_list, ops_list) {
338 if (rc == ops) {
339 list_del(&ops->ops_list);
340 genl_unlock();
341 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
342 return 0;
343 }
344 }
345 genl_unlock();
346
347 return -ENOENT;
348}
Changli Gao416c2f92010-07-25 20:46:01 +0000349EXPORT_SYMBOL(genl_unregister_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100350
351/**
352 * genl_register_family - register a generic netlink family
353 * @family: generic netlink family
354 *
355 * Registers the specified family after validating it first. Only one
356 * family may be registered with the same family name or identifier.
357 * The family id may equal GENL_ID_GENERATE causing an unique id to
358 * be automatically generated and assigned.
359 *
360 * Return 0 on success or a negative error code.
361 */
362int genl_register_family(struct genl_family *family)
363{
364 int err = -EINVAL;
365
366 if (family->id && family->id < GENL_MIN_ID)
367 goto errout;
368
369 if (family->id > GENL_MAX_ID)
370 goto errout;
371
372 INIT_LIST_HEAD(&family->ops_list);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700373 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100374
375 genl_lock();
376
377 if (genl_family_find_byname(family->name)) {
378 err = -EEXIST;
379 goto errout_locked;
380 }
381
Thomas Graf482a8522005-11-10 02:25:56 +0100382 if (family->id == GENL_ID_GENERATE) {
383 u16 newid = genl_generate_id();
384
385 if (!newid) {
386 err = -ENOMEM;
387 goto errout_locked;
388 }
389
390 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000391 } else if (genl_family_find_byid(family->id)) {
392 err = -EEXIST;
393 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100394 }
395
396 if (family->maxattr) {
397 family->attrbuf = kmalloc((family->maxattr+1) *
398 sizeof(struct nlattr *), GFP_KERNEL);
399 if (family->attrbuf == NULL) {
400 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800401 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100402 }
403 } else
404 family->attrbuf = NULL;
405
406 list_add_tail(&family->family_list, genl_family_chain(family->id));
407 genl_unlock();
408
409 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
410
411 return 0;
412
413errout_locked:
414 genl_unlock();
415errout:
416 return err;
417}
Changli Gao416c2f92010-07-25 20:46:01 +0000418EXPORT_SYMBOL(genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100419
420/**
Michał Mirosława7b11d72009-05-21 10:34:04 +0000421 * genl_register_family_with_ops - register a generic netlink family
422 * @family: generic netlink family
423 * @ops: operations to be registered
424 * @n_ops: number of elements to register
425 *
426 * Registers the specified family and operations from the specified table.
427 * Only one family may be registered with the same family name or identifier.
428 *
429 * The family id may equal GENL_ID_GENERATE causing an unique id to
430 * be automatically generated and assigned.
431 *
432 * Either a doit or dumpit callback must be specified for every registered
433 * operation or the function will fail. Only one operation structure per
434 * command identifier may be registered.
435 *
436 * See include/net/genetlink.h for more documenation on the operations
437 * structure.
438 *
439 * This is equivalent to calling genl_register_family() followed by
440 * genl_register_ops() for every operation entry in the table taking
441 * care to unregister the family on error path.
442 *
443 * Return 0 on success or a negative error code.
444 */
445int genl_register_family_with_ops(struct genl_family *family,
446 struct genl_ops *ops, size_t n_ops)
447{
448 int err, i;
449
450 err = genl_register_family(family);
451 if (err)
452 return err;
453
454 for (i = 0; i < n_ops; ++i, ++ops) {
455 err = genl_register_ops(family, ops);
456 if (err)
457 goto err_out;
458 }
459 return 0;
460err_out:
461 genl_unregister_family(family);
462 return err;
463}
464EXPORT_SYMBOL(genl_register_family_with_ops);
465
466/**
Thomas Graf482a8522005-11-10 02:25:56 +0100467 * genl_unregister_family - unregister generic netlink family
468 * @family: generic netlink family
469 *
470 * Unregisters the specified family.
471 *
472 * Returns 0 on success or a negative error code.
473 */
474int genl_unregister_family(struct genl_family *family)
475{
476 struct genl_family *rc;
477
478 genl_lock();
479
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800480 genl_unregister_mc_groups(family);
481
Thomas Graf482a8522005-11-10 02:25:56 +0100482 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
483 if (family->id != rc->id || strcmp(rc->name, family->name))
484 continue;
485
486 list_del(&rc->family_list);
487 INIT_LIST_HEAD(&family->ops_list);
488 genl_unlock();
489
Thomas Graf482a8522005-11-10 02:25:56 +0100490 kfree(family->attrbuf);
491 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
492 return 0;
493 }
494
495 genl_unlock();
496
497 return -ENOENT;
498}
Changli Gao416c2f92010-07-25 20:46:01 +0000499EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100500
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500501/**
502 * genlmsg_put - Add generic netlink header to netlink message
503 * @skb: socket buffer holding the message
504 * @pid: netlink pid the message is addressed to
505 * @seq: sequence number (usually the one of the sender)
506 * @family: generic netlink family
507 * @flags netlink message flags
508 * @cmd: generic netlink command
509 *
510 * Returns pointer to user specific header
511 */
512void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
513 struct genl_family *family, int flags, u8 cmd)
514{
515 struct nlmsghdr *nlh;
516 struct genlmsghdr *hdr;
517
518 nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
519 family->hdrsize, flags);
520 if (nlh == NULL)
521 return NULL;
522
523 hdr = nlmsg_data(nlh);
524 hdr->cmd = cmd;
525 hdr->version = family->version;
526 hdr->reserved = 0;
527
528 return (char *) hdr + GENL_HDRLEN;
529}
530EXPORT_SYMBOL(genlmsg_put);
531
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700532static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100533{
534 struct genl_ops *ops;
535 struct genl_family *family;
Johannes Berg134e6372009-07-10 09:51:34 +0000536 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100537 struct genl_info info;
538 struct genlmsghdr *hdr = nlmsg_data(nlh);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700539 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100540
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900541 family = genl_family_find_byid(nlh->nlmsg_type);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700542 if (family == NULL)
543 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100544
Johannes Berg134e6372009-07-10 09:51:34 +0000545 /* this family doesn't exist in this netns */
546 if (!family->netnsok && !net_eq(net, &init_net))
547 return -ENOENT;
548
Thomas Graf482a8522005-11-10 02:25:56 +0100549 hdrlen = GENL_HDRLEN + family->hdrsize;
550 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700551 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100552
553 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700554 if (ops == NULL)
555 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100556
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700557 if ((ops->flags & GENL_ADMIN_PERM) &&
Eric Parisfd778462012-01-03 12:25:16 -0500558 !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700559 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100560
David S. Millerb8f3ab42011-01-18 12:40:38 -0800561 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700562 if (ops->dumpit == NULL)
563 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100564
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700565 genl_unlock();
Johannes Berg134e6372009-07-10 09:51:34 +0000566 err = netlink_dump_start(net->genl_sock, skb, nlh,
Greg Rosec7ac8672011-06-10 01:27:09 +0000567 ops->dumpit, ops->done, 0);
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700568 genl_lock();
569 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100570 }
571
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700572 if (ops->doit == NULL)
573 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100574
575 if (family->attrbuf) {
576 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
577 ops->policy);
578 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700579 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100580 }
581
582 info.snd_seq = nlh->nlmsg_seq;
583 info.snd_pid = NETLINK_CB(skb).pid;
584 info.nlhdr = nlh;
585 info.genlhdr = nlmsg_data(nlh);
586 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
587 info.attrs = family->attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000588 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200589 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100590
Johannes Bergff4c92d2010-10-04 21:14:03 +0200591 if (family->pre_doit) {
592 err = family->pre_doit(ops, skb, &info);
593 if (err)
594 return err;
595 }
596
597 err = ops->doit(skb, &info);
598
599 if (family->post_doit)
600 family->post_doit(ops, skb, &info);
601
602 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100603}
604
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700605static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100606{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700607 genl_lock();
608 netlink_rcv_skb(skb, &genl_rcv_msg);
609 genl_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100610}
611
612/**************************************************************************
613 * Controller
614 **************************************************************************/
615
Thomas Graf17c157c2006-11-14 19:46:02 -0800616static struct genl_family genl_ctrl = {
617 .id = GENL_ID_CTRL,
618 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800619 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800620 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000621 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800622};
623
Thomas Graf482a8522005-11-10 02:25:56 +0100624static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
625 u32 flags, struct sk_buff *skb, u8 cmd)
626{
627 void *hdr;
628
Thomas Graf17c157c2006-11-14 19:46:02 -0800629 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100630 if (hdr == NULL)
631 return -1;
632
633 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
634 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
Thomas Grafeb328112006-09-18 00:01:59 -0700635 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
636 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
637 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
638
Thomas Grafe94ef682006-11-23 11:44:37 -0800639 if (!list_empty(&family->ops_list)) {
640 struct nlattr *nla_ops;
641 struct genl_ops *ops;
642 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700643
Thomas Grafe94ef682006-11-23 11:44:37 -0800644 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
645 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700646 goto nla_put_failure;
647
Thomas Grafe94ef682006-11-23 11:44:37 -0800648 list_for_each_entry(ops, &family->ops_list, ops_list) {
649 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700650
Thomas Grafe94ef682006-11-23 11:44:37 -0800651 nest = nla_nest_start(skb, idx++);
652 if (nest == NULL)
653 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700654
Thomas Grafe94ef682006-11-23 11:44:37 -0800655 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
656 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
Thomas Grafeb328112006-09-18 00:01:59 -0700657
Thomas Grafe94ef682006-11-23 11:44:37 -0800658 nla_nest_end(skb, nest);
659 }
660
661 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700662 }
663
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700664 if (!list_empty(&family->mcast_groups)) {
665 struct genl_multicast_group *grp;
666 struct nlattr *nla_grps;
667 int idx = 1;
668
669 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
670 if (nla_grps == NULL)
671 goto nla_put_failure;
672
673 list_for_each_entry(grp, &family->mcast_groups, list) {
674 struct nlattr *nest;
675
676 nest = nla_nest_start(skb, idx++);
677 if (nest == NULL)
678 goto nla_put_failure;
679
680 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
681 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
682 grp->name);
683
684 nla_nest_end(skb, nest);
685 }
686 nla_nest_end(skb, nla_grps);
687 }
688
689 return genlmsg_end(skb, hdr);
690
691nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700692 genlmsg_cancel(skb, hdr);
693 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700694}
695
696static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
697 u32 seq, u32 flags, struct sk_buff *skb,
698 u8 cmd)
699{
700 void *hdr;
701 struct nlattr *nla_grps;
702 struct nlattr *nest;
703
704 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
705 if (hdr == NULL)
706 return -1;
707
708 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
709 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
710
711 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
712 if (nla_grps == NULL)
713 goto nla_put_failure;
714
715 nest = nla_nest_start(skb, 1);
716 if (nest == NULL)
717 goto nla_put_failure;
718
719 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
720 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
721 grp->name);
722
723 nla_nest_end(skb, nest);
724 nla_nest_end(skb, nla_grps);
725
Thomas Graf482a8522005-11-10 02:25:56 +0100726 return genlmsg_end(skb, hdr);
727
728nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700729 genlmsg_cancel(skb, hdr);
730 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100731}
732
733static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
734{
735
736 int i, n = 0;
737 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000738 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100739 int chains_to_skip = cb->args[0];
740 int fams_to_skip = cb->args[1];
741
Samir Bellabese1d5a012010-01-07 22:10:56 +0000742 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100743 n = 0;
744 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000745 if (!rt->netnsok && !net_eq(net, &init_net))
746 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100747 if (++n < fams_to_skip)
748 continue;
749 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
750 cb->nlh->nlmsg_seq, NLM_F_MULTI,
751 skb, CTRL_CMD_NEWFAMILY) < 0)
752 goto errout;
753 }
754
755 fams_to_skip = 0;
756 }
757
758errout:
759 cb->args[0] = i;
760 cb->args[1] = n;
761
762 return skb->len;
763}
764
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700765static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
766 u32 pid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100767{
768 struct sk_buff *skb;
769 int err;
770
Thomas Graf339bf982006-11-10 14:10:15 -0800771 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100772 if (skb == NULL)
773 return ERR_PTR(-ENOBUFS);
774
775 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
776 if (err < 0) {
777 nlmsg_free(skb);
778 return ERR_PTR(err);
779 }
780
781 return skb;
782}
783
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700784static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
785 u32 pid, int seq, u8 cmd)
786{
787 struct sk_buff *skb;
788 int err;
789
790 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
791 if (skb == NULL)
792 return ERR_PTR(-ENOBUFS);
793
794 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
795 if (err < 0) {
796 nlmsg_free(skb);
797 return ERR_PTR(err);
798 }
799
800 return skb;
801}
802
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700803static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100804 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700805 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
806 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100807};
808
809static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
810{
811 struct sk_buff *msg;
812 struct genl_family *res = NULL;
813 int err = -EINVAL;
814
815 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
816 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
817 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000818 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100819 }
820
821 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700822 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100823
Thomas Graf5176f912006-08-26 20:13:18 -0700824 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100825 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500826#ifdef CONFIG_MODULES
827 if (res == NULL) {
828 genl_unlock();
829 request_module("net-pf-%d-proto-%d-type-%s",
830 PF_NETLINK, NETLINK_GENERIC, name);
831 genl_lock();
832 res = genl_family_find_byname(name);
833 }
834#endif
Johannes Berg134e6372009-07-10 09:51:34 +0000835 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100836 }
837
Johannes Berg134e6372009-07-10 09:51:34 +0000838 if (res == NULL)
839 return err;
840
841 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
842 /* family doesn't exist here */
843 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100844 }
845
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700846 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
847 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000848 if (IS_ERR(msg))
849 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100850
Johannes Berg134e6372009-07-10 09:51:34 +0000851 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100852}
853
854static int genl_ctrl_event(int event, void *data)
855{
856 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000857 struct genl_family *family;
858 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100859
Johannes Berg134e6372009-07-10 09:51:34 +0000860 /* genl is still initialising */
861 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100862 return 0;
863
864 switch (event) {
865 case CTRL_CMD_NEWFAMILY:
866 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000867 family = data;
868 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700869 break;
870 case CTRL_CMD_NEWMCAST_GRP:
871 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000872 grp = data;
873 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700874 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100875 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000876 default:
877 return -EINVAL;
878 }
879
880 if (IS_ERR(msg))
881 return PTR_ERR(msg);
882
883 if (!family->netnsok) {
884 genlmsg_multicast_netns(&init_net, msg, 0,
885 GENL_ID_CTRL, GFP_KERNEL);
886 } else {
887 rcu_read_lock();
888 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
889 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100890 }
891
892 return 0;
893}
894
895static struct genl_ops genl_ctrl_ops = {
896 .cmd = CTRL_CMD_GETFAMILY,
897 .doit = ctrl_getfamily,
898 .dumpit = ctrl_dumpfamily,
899 .policy = ctrl_policy,
900};
901
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700902static struct genl_multicast_group notify_grp = {
903 .name = "notify",
904};
905
Johannes Berg134e6372009-07-10 09:51:34 +0000906static int __net_init genl_pernet_init(struct net *net)
907{
908 /* we'll bump the group number right afterwards */
909 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
910 genl_rcv, &genl_mutex,
911 THIS_MODULE);
912
913 if (!net->genl_sock && net_eq(net, &init_net))
914 panic("GENL: Cannot initialize generic netlink\n");
915
916 if (!net->genl_sock)
917 return -ENOMEM;
918
919 return 0;
920}
921
922static void __net_exit genl_pernet_exit(struct net *net)
923{
924 netlink_kernel_release(net->genl_sock);
925 net->genl_sock = NULL;
926}
927
928static struct pernet_operations genl_pernet_ops = {
929 .init = genl_pernet_init,
930 .exit = genl_pernet_exit,
931};
932
Thomas Graf482a8522005-11-10 02:25:56 +0100933static int __init genl_init(void)
934{
935 int i, err;
936
937 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
938 INIT_LIST_HEAD(&family_ht[i]);
939
Changli Gao652c6712010-07-25 23:21:05 +0000940 err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
Thomas Graf482a8522005-11-10 02:25:56 +0100941 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000942 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100943
944 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700945
Johannes Berg134e6372009-07-10 09:51:34 +0000946 err = register_pernet_subsys(&genl_pernet_ops);
947 if (err)
948 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100949
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700950 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
951 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000952 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700953
Thomas Graf482a8522005-11-10 02:25:56 +0100954 return 0;
955
Johannes Berg134e6372009-07-10 09:51:34 +0000956problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100957 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100958}
959
960subsys_initcall(genl_init);
961
Johannes Berg134e6372009-07-10 09:51:34 +0000962static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
963 gfp_t flags)
964{
965 struct sk_buff *tmp;
966 struct net *net, *prev = NULL;
967 int err;
968
969 for_each_net_rcu(net) {
970 if (prev) {
971 tmp = skb_clone(skb, flags);
972 if (!tmp) {
973 err = -ENOMEM;
974 goto error;
975 }
976 err = nlmsg_multicast(prev->genl_sock, tmp,
977 pid, group, flags);
978 if (err)
979 goto error;
980 }
981
982 prev = net;
983 }
984
985 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
986 error:
987 kfree_skb(skb);
988 return err;
989}
990
991int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
992 gfp_t flags)
993{
994 return genlmsg_mcast(skb, pid, group, flags);
995}
996EXPORT_SYMBOL(genlmsg_multicast_allns);
Pravin B Shelar263ba612011-11-10 19:14:37 -0800997
998void genl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
999 struct nlmsghdr *nlh, gfp_t flags)
1000{
1001 struct sock *sk = net->genl_sock;
1002 int report = 0;
1003
1004 if (nlh)
1005 report = nlmsg_report(nlh);
1006
1007 nlmsg_notify(sk, skb, pid, group, report, flags);
1008}
1009EXPORT_SYMBOL(genl_notify);