blob: 73d3f0c84ceba896da5be7d46753a85ecb0bfea9 [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');
Masatake YAMATOb9f3bf12013-04-01 14:50:40 -0400145 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700146
147 genl_lock();
148
149 /* special-case our own group */
150 if (grp == &notify_grp)
151 id = GENL_ID_CTRL;
152 else
153 id = find_first_zero_bit(mc_groups,
154 mc_groups_longs * BITS_PER_LONG);
155
156
157 if (id >= mc_groups_longs * BITS_PER_LONG) {
158 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
159
160 if (mc_groups == &mc_group_start) {
161 new_groups = kzalloc(nlen, GFP_KERNEL);
162 if (!new_groups) {
163 err = -ENOMEM;
164 goto out;
165 }
166 mc_groups = new_groups;
167 *mc_groups = mc_group_start;
168 } else {
169 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
170 if (!new_groups) {
171 err = -ENOMEM;
172 goto out;
173 }
174 mc_groups = new_groups;
175 mc_groups[mc_groups_longs] = 0;
176 }
177 mc_groups_longs++;
178 }
179
Johannes Berg134e6372009-07-10 09:51:34 +0000180 if (family->netnsok) {
181 struct net *net;
182
Johannes Bergd136f1b2009-09-12 03:03:15 +0000183 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000184 rcu_read_lock();
185 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000186 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000187 mc_groups_longs * BITS_PER_LONG);
188 if (err) {
189 /*
190 * No need to roll back, can only fail if
191 * memory allocation fails and then the
192 * number of _possible_ groups has been
193 * increased on some sockets which is ok.
194 */
195 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000196 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000197 goto out;
198 }
199 }
200 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000201 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000202 } else {
203 err = netlink_change_ngroups(init_net.genl_sock,
204 mc_groups_longs * BITS_PER_LONG);
205 if (err)
206 goto out;
207 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700208
209 grp->id = id;
210 set_bit(id, mc_groups);
211 list_add_tail(&grp->list, &family->mcast_groups);
212 grp->family = family;
213
214 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
215 out:
216 genl_unlock();
Thomas Graf79d310d2007-07-24 15:34:53 -0700217 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700218}
219EXPORT_SYMBOL(genl_register_mc_group);
220
Thomas Graf79dc43862007-07-24 15:32:46 -0700221static void __genl_unregister_mc_group(struct genl_family *family,
222 struct genl_multicast_group *grp)
223{
Johannes Berg134e6372009-07-10 09:51:34 +0000224 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700225 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000226
Johannes Bergb8273572009-09-24 15:44:05 -0700227 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000228 rcu_read_lock();
229 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700230 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000231 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700232 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000233
Thomas Graf79dc43862007-07-24 15:32:46 -0700234 clear_bit(grp->id, mc_groups);
235 list_del(&grp->list);
236 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
237 grp->id = 0;
238 grp->family = NULL;
239}
240
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700241/**
242 * genl_unregister_mc_group - unregister a multicast group
243 *
244 * Unregisters the specified multicast group and notifies userspace
245 * about it. All current listeners on the group are removed.
246 *
247 * Note: It is not necessary to unregister all multicast groups before
248 * unregistering the family, unregistering the family will cause
249 * all assigned multicast groups to be unregistered automatically.
250 *
251 * @family: Generic netlink family the group belongs to.
252 * @grp: The group to unregister, must have been registered successfully
253 * previously.
254 */
255void genl_unregister_mc_group(struct genl_family *family,
256 struct genl_multicast_group *grp)
257{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700258 genl_lock();
Thomas Graf79dc43862007-07-24 15:32:46 -0700259 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700260 genl_unlock();
261}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800262EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700263
264static void genl_unregister_mc_groups(struct genl_family *family)
265{
266 struct genl_multicast_group *grp, *tmp;
267
268 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700269 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700270}
271
Thomas Graf482a8522005-11-10 02:25:56 +0100272/**
273 * genl_register_ops - register generic netlink operations
274 * @family: generic netlink family
275 * @ops: operations to be registered
276 *
277 * Registers the specified operations and assigns them to the specified
278 * family. Either a doit or dumpit callback must be specified or the
279 * operation will fail. Only one operation structure per command
280 * identifier may be registered.
281 *
282 * See include/net/genetlink.h for more documenation on the operations
283 * structure.
284 *
285 * Returns 0 on success or a negative error code.
286 */
287int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
288{
289 int err = -EINVAL;
290
291 if (ops->dumpit == NULL && ops->doit == NULL)
292 goto errout;
293
294 if (genl_get_cmd(ops->cmd, family)) {
295 err = -EEXIST;
296 goto errout;
297 }
298
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800299 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800300 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800301 if (ops->doit)
302 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800303 if (ops->policy)
304 ops->flags |= GENL_CMD_CAP_HASPOL;
305
Thomas Graf482a8522005-11-10 02:25:56 +0100306 genl_lock();
307 list_add_tail(&ops->ops_list, &family->ops_list);
308 genl_unlock();
309
310 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
311 err = 0;
312errout:
313 return err;
314}
Changli Gao416c2f92010-07-25 20:46:01 +0000315EXPORT_SYMBOL(genl_register_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100316
317/**
318 * genl_unregister_ops - unregister generic netlink operations
319 * @family: generic netlink family
320 * @ops: operations to be unregistered
321 *
322 * Unregisters the specified operations and unassigns them from the
323 * specified family. The operation blocks until the current message
324 * processing has finished and doesn't start again until the
325 * unregister process has finished.
326 *
327 * Note: It is not necessary to unregister all operations before
328 * unregistering the family, unregistering the family will cause
329 * all assigned operations to be unregistered automatically.
330 *
331 * Returns 0 on success or a negative error code.
332 */
333int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
334{
335 struct genl_ops *rc;
336
337 genl_lock();
338 list_for_each_entry(rc, &family->ops_list, ops_list) {
339 if (rc == ops) {
340 list_del(&ops->ops_list);
341 genl_unlock();
342 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
343 return 0;
344 }
345 }
346 genl_unlock();
347
348 return -ENOENT;
349}
Changli Gao416c2f92010-07-25 20:46:01 +0000350EXPORT_SYMBOL(genl_unregister_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100351
352/**
353 * genl_register_family - register a generic netlink family
354 * @family: generic netlink family
355 *
356 * Registers the specified family after validating it first. Only one
357 * family may be registered with the same family name or identifier.
358 * The family id may equal GENL_ID_GENERATE causing an unique id to
359 * be automatically generated and assigned.
360 *
361 * Return 0 on success or a negative error code.
362 */
363int genl_register_family(struct genl_family *family)
364{
365 int err = -EINVAL;
366
367 if (family->id && family->id < GENL_MIN_ID)
368 goto errout;
369
370 if (family->id > GENL_MAX_ID)
371 goto errout;
372
373 INIT_LIST_HEAD(&family->ops_list);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700374 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100375
376 genl_lock();
377
378 if (genl_family_find_byname(family->name)) {
379 err = -EEXIST;
380 goto errout_locked;
381 }
382
Thomas Graf482a8522005-11-10 02:25:56 +0100383 if (family->id == GENL_ID_GENERATE) {
384 u16 newid = genl_generate_id();
385
386 if (!newid) {
387 err = -ENOMEM;
388 goto errout_locked;
389 }
390
391 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000392 } else if (genl_family_find_byid(family->id)) {
393 err = -EEXIST;
394 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100395 }
396
397 if (family->maxattr) {
398 family->attrbuf = kmalloc((family->maxattr+1) *
399 sizeof(struct nlattr *), GFP_KERNEL);
400 if (family->attrbuf == NULL) {
401 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800402 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100403 }
404 } else
405 family->attrbuf = NULL;
406
407 list_add_tail(&family->family_list, genl_family_chain(family->id));
408 genl_unlock();
409
410 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
411
412 return 0;
413
414errout_locked:
415 genl_unlock();
416errout:
417 return err;
418}
Changli Gao416c2f92010-07-25 20:46:01 +0000419EXPORT_SYMBOL(genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100420
421/**
Michał Mirosława7b11d72009-05-21 10:34:04 +0000422 * genl_register_family_with_ops - register a generic netlink family
423 * @family: generic netlink family
424 * @ops: operations to be registered
425 * @n_ops: number of elements to register
426 *
427 * Registers the specified family and operations from the specified table.
428 * Only one family may be registered with the same family name or identifier.
429 *
430 * The family id may equal GENL_ID_GENERATE causing an unique id to
431 * be automatically generated and assigned.
432 *
433 * Either a doit or dumpit callback must be specified for every registered
434 * operation or the function will fail. Only one operation structure per
435 * command identifier may be registered.
436 *
437 * See include/net/genetlink.h for more documenation on the operations
438 * structure.
439 *
440 * This is equivalent to calling genl_register_family() followed by
441 * genl_register_ops() for every operation entry in the table taking
442 * care to unregister the family on error path.
443 *
444 * Return 0 on success or a negative error code.
445 */
446int genl_register_family_with_ops(struct genl_family *family,
447 struct genl_ops *ops, size_t n_ops)
448{
449 int err, i;
450
451 err = genl_register_family(family);
452 if (err)
453 return err;
454
455 for (i = 0; i < n_ops; ++i, ++ops) {
456 err = genl_register_ops(family, ops);
457 if (err)
458 goto err_out;
459 }
460 return 0;
461err_out:
462 genl_unregister_family(family);
463 return err;
464}
465EXPORT_SYMBOL(genl_register_family_with_ops);
466
467/**
Thomas Graf482a8522005-11-10 02:25:56 +0100468 * genl_unregister_family - unregister generic netlink family
469 * @family: generic netlink family
470 *
471 * Unregisters the specified family.
472 *
473 * Returns 0 on success or a negative error code.
474 */
475int genl_unregister_family(struct genl_family *family)
476{
477 struct genl_family *rc;
478
479 genl_lock();
480
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800481 genl_unregister_mc_groups(family);
482
Thomas Graf482a8522005-11-10 02:25:56 +0100483 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
484 if (family->id != rc->id || strcmp(rc->name, family->name))
485 continue;
486
487 list_del(&rc->family_list);
488 INIT_LIST_HEAD(&family->ops_list);
489 genl_unlock();
490
Thomas Graf482a8522005-11-10 02:25:56 +0100491 kfree(family->attrbuf);
492 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
493 return 0;
494 }
495
496 genl_unlock();
497
498 return -ENOENT;
499}
Changli Gao416c2f92010-07-25 20:46:01 +0000500EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100501
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500502/**
503 * genlmsg_put - Add generic netlink header to netlink message
504 * @skb: socket buffer holding the message
505 * @pid: netlink pid the message is addressed to
506 * @seq: sequence number (usually the one of the sender)
507 * @family: generic netlink family
508 * @flags netlink message flags
509 * @cmd: generic netlink command
510 *
511 * Returns pointer to user specific header
512 */
513void *genlmsg_put(struct sk_buff *skb, u32 pid, u32 seq,
514 struct genl_family *family, int flags, u8 cmd)
515{
516 struct nlmsghdr *nlh;
517 struct genlmsghdr *hdr;
518
519 nlh = nlmsg_put(skb, pid, seq, family->id, GENL_HDRLEN +
520 family->hdrsize, flags);
521 if (nlh == NULL)
522 return NULL;
523
524 hdr = nlmsg_data(nlh);
525 hdr->cmd = cmd;
526 hdr->version = family->version;
527 hdr->reserved = 0;
528
529 return (char *) hdr + GENL_HDRLEN;
530}
531EXPORT_SYMBOL(genlmsg_put);
532
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700533static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100534{
535 struct genl_ops *ops;
536 struct genl_family *family;
Johannes Berg134e6372009-07-10 09:51:34 +0000537 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100538 struct genl_info info;
539 struct genlmsghdr *hdr = nlmsg_data(nlh);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700540 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100541
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900542 family = genl_family_find_byid(nlh->nlmsg_type);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700543 if (family == NULL)
544 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100545
Johannes Berg134e6372009-07-10 09:51:34 +0000546 /* this family doesn't exist in this netns */
547 if (!family->netnsok && !net_eq(net, &init_net))
548 return -ENOENT;
549
Thomas Graf482a8522005-11-10 02:25:56 +0100550 hdrlen = GENL_HDRLEN + family->hdrsize;
551 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700552 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100553
554 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700555 if (ops == NULL)
556 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100557
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700558 if ((ops->flags & GENL_ADMIN_PERM) &&
Eric Parisfd778462012-01-03 12:25:16 -0500559 !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700560 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100561
David S. Millerb8f3ab42011-01-18 12:40:38 -0800562 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700563 if (ops->dumpit == NULL)
564 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100565
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700566 genl_unlock();
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000567 {
568 struct netlink_dump_control c = {
569 .dump = ops->dumpit,
570 .done = ops->done,
571 };
572 err = netlink_dump_start(net->genl_sock, skb, nlh, &c);
573 }
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700574 genl_lock();
575 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100576 }
577
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700578 if (ops->doit == NULL)
579 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100580
581 if (family->attrbuf) {
582 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
583 ops->policy);
584 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700585 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100586 }
587
588 info.snd_seq = nlh->nlmsg_seq;
589 info.snd_pid = NETLINK_CB(skb).pid;
590 info.nlhdr = nlh;
591 info.genlhdr = nlmsg_data(nlh);
592 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
593 info.attrs = family->attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000594 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200595 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100596
Johannes Bergff4c92d2010-10-04 21:14:03 +0200597 if (family->pre_doit) {
598 err = family->pre_doit(ops, skb, &info);
599 if (err)
600 return err;
601 }
602
603 err = ops->doit(skb, &info);
604
605 if (family->post_doit)
606 family->post_doit(ops, skb, &info);
607
608 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100609}
610
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700611static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100612{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700613 genl_lock();
614 netlink_rcv_skb(skb, &genl_rcv_msg);
615 genl_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100616}
617
618/**************************************************************************
619 * Controller
620 **************************************************************************/
621
Thomas Graf17c157c2006-11-14 19:46:02 -0800622static struct genl_family genl_ctrl = {
623 .id = GENL_ID_CTRL,
624 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800625 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800626 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000627 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800628};
629
Thomas Graf482a8522005-11-10 02:25:56 +0100630static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
631 u32 flags, struct sk_buff *skb, u8 cmd)
632{
633 void *hdr;
634
Thomas Graf17c157c2006-11-14 19:46:02 -0800635 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100636 if (hdr == NULL)
637 return -1;
638
639 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
640 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
Thomas Grafeb328112006-09-18 00:01:59 -0700641 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
642 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
643 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
644
Thomas Grafe94ef682006-11-23 11:44:37 -0800645 if (!list_empty(&family->ops_list)) {
646 struct nlattr *nla_ops;
647 struct genl_ops *ops;
648 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700649
Thomas Grafe94ef682006-11-23 11:44:37 -0800650 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
651 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700652 goto nla_put_failure;
653
Thomas Grafe94ef682006-11-23 11:44:37 -0800654 list_for_each_entry(ops, &family->ops_list, ops_list) {
655 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700656
Thomas Grafe94ef682006-11-23 11:44:37 -0800657 nest = nla_nest_start(skb, idx++);
658 if (nest == NULL)
659 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700660
Thomas Grafe94ef682006-11-23 11:44:37 -0800661 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
662 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
Thomas Grafeb328112006-09-18 00:01:59 -0700663
Thomas Grafe94ef682006-11-23 11:44:37 -0800664 nla_nest_end(skb, nest);
665 }
666
667 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700668 }
669
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700670 if (!list_empty(&family->mcast_groups)) {
671 struct genl_multicast_group *grp;
672 struct nlattr *nla_grps;
673 int idx = 1;
674
675 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
676 if (nla_grps == NULL)
677 goto nla_put_failure;
678
679 list_for_each_entry(grp, &family->mcast_groups, list) {
680 struct nlattr *nest;
681
682 nest = nla_nest_start(skb, idx++);
683 if (nest == NULL)
684 goto nla_put_failure;
685
686 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
687 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
688 grp->name);
689
690 nla_nest_end(skb, nest);
691 }
692 nla_nest_end(skb, nla_grps);
693 }
694
695 return genlmsg_end(skb, hdr);
696
697nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700698 genlmsg_cancel(skb, hdr);
699 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700700}
701
702static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
703 u32 seq, u32 flags, struct sk_buff *skb,
704 u8 cmd)
705{
706 void *hdr;
707 struct nlattr *nla_grps;
708 struct nlattr *nest;
709
710 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
711 if (hdr == NULL)
712 return -1;
713
714 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
715 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
716
717 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
718 if (nla_grps == NULL)
719 goto nla_put_failure;
720
721 nest = nla_nest_start(skb, 1);
722 if (nest == NULL)
723 goto nla_put_failure;
724
725 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
726 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
727 grp->name);
728
729 nla_nest_end(skb, nest);
730 nla_nest_end(skb, nla_grps);
731
Thomas Graf482a8522005-11-10 02:25:56 +0100732 return genlmsg_end(skb, hdr);
733
734nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700735 genlmsg_cancel(skb, hdr);
736 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100737}
738
739static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
740{
741
742 int i, n = 0;
743 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000744 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100745 int chains_to_skip = cb->args[0];
746 int fams_to_skip = cb->args[1];
747
Samir Bellabese1d5a012010-01-07 22:10:56 +0000748 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100749 n = 0;
750 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000751 if (!rt->netnsok && !net_eq(net, &init_net))
752 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100753 if (++n < fams_to_skip)
754 continue;
755 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
756 cb->nlh->nlmsg_seq, NLM_F_MULTI,
757 skb, CTRL_CMD_NEWFAMILY) < 0)
758 goto errout;
759 }
760
761 fams_to_skip = 0;
762 }
763
764errout:
765 cb->args[0] = i;
766 cb->args[1] = n;
767
768 return skb->len;
769}
770
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700771static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
772 u32 pid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100773{
774 struct sk_buff *skb;
775 int err;
776
Thomas Graf339bf982006-11-10 14:10:15 -0800777 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100778 if (skb == NULL)
779 return ERR_PTR(-ENOBUFS);
780
781 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
782 if (err < 0) {
783 nlmsg_free(skb);
784 return ERR_PTR(err);
785 }
786
787 return skb;
788}
789
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700790static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
791 u32 pid, int seq, u8 cmd)
792{
793 struct sk_buff *skb;
794 int err;
795
796 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
797 if (skb == NULL)
798 return ERR_PTR(-ENOBUFS);
799
800 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
801 if (err < 0) {
802 nlmsg_free(skb);
803 return ERR_PTR(err);
804 }
805
806 return skb;
807}
808
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700809static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100810 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700811 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
812 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100813};
814
815static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
816{
817 struct sk_buff *msg;
818 struct genl_family *res = NULL;
819 int err = -EINVAL;
820
821 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
822 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
823 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000824 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100825 }
826
827 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700828 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100829
Thomas Graf5176f912006-08-26 20:13:18 -0700830 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100831 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500832#ifdef CONFIG_MODULES
833 if (res == NULL) {
834 genl_unlock();
835 request_module("net-pf-%d-proto-%d-type-%s",
836 PF_NETLINK, NETLINK_GENERIC, name);
837 genl_lock();
838 res = genl_family_find_byname(name);
839 }
840#endif
Johannes Berg134e6372009-07-10 09:51:34 +0000841 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100842 }
843
Johannes Berg134e6372009-07-10 09:51:34 +0000844 if (res == NULL)
845 return err;
846
847 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
848 /* family doesn't exist here */
849 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100850 }
851
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700852 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
853 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000854 if (IS_ERR(msg))
855 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100856
Johannes Berg134e6372009-07-10 09:51:34 +0000857 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100858}
859
860static int genl_ctrl_event(int event, void *data)
861{
862 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000863 struct genl_family *family;
864 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100865
Johannes Berg134e6372009-07-10 09:51:34 +0000866 /* genl is still initialising */
867 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100868 return 0;
869
870 switch (event) {
871 case CTRL_CMD_NEWFAMILY:
872 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000873 family = data;
874 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700875 break;
876 case CTRL_CMD_NEWMCAST_GRP:
877 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000878 grp = data;
879 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700880 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100881 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000882 default:
883 return -EINVAL;
884 }
885
886 if (IS_ERR(msg))
887 return PTR_ERR(msg);
888
889 if (!family->netnsok) {
890 genlmsg_multicast_netns(&init_net, msg, 0,
891 GENL_ID_CTRL, GFP_KERNEL);
892 } else {
893 rcu_read_lock();
894 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
895 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100896 }
897
898 return 0;
899}
900
901static struct genl_ops genl_ctrl_ops = {
902 .cmd = CTRL_CMD_GETFAMILY,
903 .doit = ctrl_getfamily,
904 .dumpit = ctrl_dumpfamily,
905 .policy = ctrl_policy,
906};
907
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700908static struct genl_multicast_group notify_grp = {
909 .name = "notify",
910};
911
Johannes Berg134e6372009-07-10 09:51:34 +0000912static int __net_init genl_pernet_init(struct net *net)
913{
914 /* we'll bump the group number right afterwards */
915 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
916 genl_rcv, &genl_mutex,
917 THIS_MODULE);
918
919 if (!net->genl_sock && net_eq(net, &init_net))
920 panic("GENL: Cannot initialize generic netlink\n");
921
922 if (!net->genl_sock)
923 return -ENOMEM;
924
925 return 0;
926}
927
928static void __net_exit genl_pernet_exit(struct net *net)
929{
930 netlink_kernel_release(net->genl_sock);
931 net->genl_sock = NULL;
932}
933
934static struct pernet_operations genl_pernet_ops = {
935 .init = genl_pernet_init,
936 .exit = genl_pernet_exit,
937};
938
Thomas Graf482a8522005-11-10 02:25:56 +0100939static int __init genl_init(void)
940{
941 int i, err;
942
943 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
944 INIT_LIST_HEAD(&family_ht[i]);
945
Changli Gao652c6712010-07-25 23:21:05 +0000946 err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
Thomas Graf482a8522005-11-10 02:25:56 +0100947 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000948 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100949
950 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700951
Johannes Berg134e6372009-07-10 09:51:34 +0000952 err = register_pernet_subsys(&genl_pernet_ops);
953 if (err)
954 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100955
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700956 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
957 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000958 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700959
Thomas Graf482a8522005-11-10 02:25:56 +0100960 return 0;
961
Johannes Berg134e6372009-07-10 09:51:34 +0000962problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100963 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100964}
965
966subsys_initcall(genl_init);
967
Johannes Berg134e6372009-07-10 09:51:34 +0000968static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
969 gfp_t flags)
970{
971 struct sk_buff *tmp;
972 struct net *net, *prev = NULL;
973 int err;
974
975 for_each_net_rcu(net) {
976 if (prev) {
977 tmp = skb_clone(skb, flags);
978 if (!tmp) {
979 err = -ENOMEM;
980 goto error;
981 }
982 err = nlmsg_multicast(prev->genl_sock, tmp,
983 pid, group, flags);
984 if (err)
985 goto error;
986 }
987
988 prev = net;
989 }
990
991 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
992 error:
993 kfree_skb(skb);
994 return err;
995}
996
997int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
998 gfp_t flags)
999{
1000 return genlmsg_mcast(skb, pid, group, flags);
1001}
1002EXPORT_SYMBOL(genlmsg_multicast_allns);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001003
1004void genl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
1005 struct nlmsghdr *nlh, gfp_t flags)
1006{
1007 struct sock *sk = net->genl_sock;
1008 int report = 0;
1009
1010 if (nlh)
1011 report = nlmsg_report(nlh);
1012
1013 nlmsg_notify(sk, skb, pid, group, report, flags);
1014}
1015EXPORT_SYMBOL(genl_notify);