blob: 06438fa2b1e5d1bfcef02b274941e0553a533bd2 [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
Denis V. Lunev3b715352007-10-10 21:13:32 -070024static inline void 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}
28
Denis V. Lunev3b715352007-10-10 21:13:32 -070029static inline void genl_unlock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010030{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080031 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010032}
33
34#define GENL_FAM_TAB_SIZE 16
35#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
36
37static struct list_head family_ht[GENL_FAM_TAB_SIZE];
Johannes Berg2dbba6f2007-07-18 15:47:52 -070038/*
39 * Bitmap of multicast groups that are currently in use.
40 *
41 * To avoid an allocation at boot of just one unsigned long,
42 * declare it global instead.
43 * Bit 0 is marked as already used since group 0 is invalid.
44 */
45static unsigned long mc_group_start = 0x1;
46static unsigned long *mc_groups = &mc_group_start;
47static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010048
49static int genl_ctrl_event(int event, void *data);
50
51static inline unsigned int genl_family_hash(unsigned int id)
52{
53 return id & GENL_FAM_TAB_MASK;
54}
55
56static inline struct list_head *genl_family_chain(unsigned int id)
57{
58 return &family_ht[genl_family_hash(id)];
59}
60
61static struct genl_family *genl_family_find_byid(unsigned int id)
62{
63 struct genl_family *f;
64
65 list_for_each_entry(f, genl_family_chain(id), family_list)
66 if (f->id == id)
67 return f;
68
69 return NULL;
70}
71
72static struct genl_family *genl_family_find_byname(char *name)
73{
74 struct genl_family *f;
75 int i;
76
77 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
78 list_for_each_entry(f, genl_family_chain(i), family_list)
79 if (strcmp(f->name, name) == 0)
80 return f;
81
82 return NULL;
83}
84
85static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
86{
87 struct genl_ops *ops;
88
89 list_for_each_entry(ops, &family->ops_list, ops_list)
90 if (ops->cmd == cmd)
91 return ops;
92
93 return NULL;
94}
95
96/* Of course we are going to have problems once we hit
97 * 2^16 alive types, but that can only happen by year 2K
98*/
99static inline u16 genl_generate_id(void)
100{
Krishna Kumar988ade62009-10-14 19:55:07 +0000101 static u16 id_gen_idx = GENL_MIN_ID;
102 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100103
Krishna Kumar988ade62009-10-14 19:55:07 +0000104 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
105 if (!genl_family_find_byid(id_gen_idx))
106 return id_gen_idx;
107 if (++id_gen_idx > GENL_MAX_ID)
Thomas Graf482a8522005-11-10 02:25:56 +0100108 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000109 }
Thomas Graf482a8522005-11-10 02:25:56 +0100110
Krishna Kumar988ade62009-10-14 19:55:07 +0000111 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100112}
113
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700114static struct genl_multicast_group notify_grp;
115
116/**
117 * genl_register_mc_group - register a multicast group
118 *
119 * Registers the specified multicast group and notifies userspace
120 * about the new group.
121 *
122 * Returns 0 on success or a negative error code.
123 *
124 * @family: The generic netlink family the group shall be registered for.
125 * @grp: The group to register, must have a name.
126 */
127int genl_register_mc_group(struct genl_family *family,
128 struct genl_multicast_group *grp)
129{
130 int id;
131 unsigned long *new_groups;
Brian Haleyb1f57192009-09-04 20:36:52 -0700132 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700133
134 BUG_ON(grp->name[0] == '\0');
135
136 genl_lock();
137
138 /* special-case our own group */
139 if (grp == &notify_grp)
140 id = GENL_ID_CTRL;
141 else
142 id = find_first_zero_bit(mc_groups,
143 mc_groups_longs * BITS_PER_LONG);
144
145
146 if (id >= mc_groups_longs * BITS_PER_LONG) {
147 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
148
149 if (mc_groups == &mc_group_start) {
150 new_groups = kzalloc(nlen, GFP_KERNEL);
151 if (!new_groups) {
152 err = -ENOMEM;
153 goto out;
154 }
155 mc_groups = new_groups;
156 *mc_groups = mc_group_start;
157 } else {
158 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
159 if (!new_groups) {
160 err = -ENOMEM;
161 goto out;
162 }
163 mc_groups = new_groups;
164 mc_groups[mc_groups_longs] = 0;
165 }
166 mc_groups_longs++;
167 }
168
Johannes Berg134e6372009-07-10 09:51:34 +0000169 if (family->netnsok) {
170 struct net *net;
171
Johannes Bergd136f1b2009-09-12 03:03:15 +0000172 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000173 rcu_read_lock();
174 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000175 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000176 mc_groups_longs * BITS_PER_LONG);
177 if (err) {
178 /*
179 * No need to roll back, can only fail if
180 * memory allocation fails and then the
181 * number of _possible_ groups has been
182 * increased on some sockets which is ok.
183 */
184 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000185 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000186 goto out;
187 }
188 }
189 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000190 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000191 } else {
192 err = netlink_change_ngroups(init_net.genl_sock,
193 mc_groups_longs * BITS_PER_LONG);
194 if (err)
195 goto out;
196 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700197
198 grp->id = id;
199 set_bit(id, mc_groups);
200 list_add_tail(&grp->list, &family->mcast_groups);
201 grp->family = family;
202
203 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
204 out:
205 genl_unlock();
Thomas Graf79d310d2007-07-24 15:34:53 -0700206 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700207}
208EXPORT_SYMBOL(genl_register_mc_group);
209
Thomas Graf79dc43862007-07-24 15:32:46 -0700210static void __genl_unregister_mc_group(struct genl_family *family,
211 struct genl_multicast_group *grp)
212{
Johannes Berg134e6372009-07-10 09:51:34 +0000213 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700214 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000215
Johannes Bergb8273572009-09-24 15:44:05 -0700216 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000217 rcu_read_lock();
218 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700219 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000220 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700221 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000222
Thomas Graf79dc43862007-07-24 15:32:46 -0700223 clear_bit(grp->id, mc_groups);
224 list_del(&grp->list);
225 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
226 grp->id = 0;
227 grp->family = NULL;
228}
229
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700230/**
231 * genl_unregister_mc_group - unregister a multicast group
232 *
233 * Unregisters the specified multicast group and notifies userspace
234 * about it. All current listeners on the group are removed.
235 *
236 * Note: It is not necessary to unregister all multicast groups before
237 * unregistering the family, unregistering the family will cause
238 * all assigned multicast groups to be unregistered automatically.
239 *
240 * @family: Generic netlink family the group belongs to.
241 * @grp: The group to unregister, must have been registered successfully
242 * previously.
243 */
244void genl_unregister_mc_group(struct genl_family *family,
245 struct genl_multicast_group *grp)
246{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700247 genl_lock();
Thomas Graf79dc43862007-07-24 15:32:46 -0700248 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700249 genl_unlock();
250}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800251EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700252
253static void genl_unregister_mc_groups(struct genl_family *family)
254{
255 struct genl_multicast_group *grp, *tmp;
256
257 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700258 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700259}
260
Thomas Graf482a8522005-11-10 02:25:56 +0100261/**
262 * genl_register_ops - register generic netlink operations
263 * @family: generic netlink family
264 * @ops: operations to be registered
265 *
266 * Registers the specified operations and assigns them to the specified
267 * family. Either a doit or dumpit callback must be specified or the
268 * operation will fail. Only one operation structure per command
269 * identifier may be registered.
270 *
271 * See include/net/genetlink.h for more documenation on the operations
272 * structure.
273 *
274 * Returns 0 on success or a negative error code.
275 */
276int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
277{
278 int err = -EINVAL;
279
280 if (ops->dumpit == NULL && ops->doit == NULL)
281 goto errout;
282
283 if (genl_get_cmd(ops->cmd, family)) {
284 err = -EEXIST;
285 goto errout;
286 }
287
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800288 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800289 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800290 if (ops->doit)
291 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800292 if (ops->policy)
293 ops->flags |= GENL_CMD_CAP_HASPOL;
294
Thomas Graf482a8522005-11-10 02:25:56 +0100295 genl_lock();
296 list_add_tail(&ops->ops_list, &family->ops_list);
297 genl_unlock();
298
299 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
300 err = 0;
301errout:
302 return err;
303}
304
305/**
306 * genl_unregister_ops - unregister generic netlink operations
307 * @family: generic netlink family
308 * @ops: operations to be unregistered
309 *
310 * Unregisters the specified operations and unassigns them from the
311 * specified family. The operation blocks until the current message
312 * processing has finished and doesn't start again until the
313 * unregister process has finished.
314 *
315 * Note: It is not necessary to unregister all operations before
316 * unregistering the family, unregistering the family will cause
317 * all assigned operations to be unregistered automatically.
318 *
319 * Returns 0 on success or a negative error code.
320 */
321int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
322{
323 struct genl_ops *rc;
324
325 genl_lock();
326 list_for_each_entry(rc, &family->ops_list, ops_list) {
327 if (rc == ops) {
328 list_del(&ops->ops_list);
329 genl_unlock();
330 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
331 return 0;
332 }
333 }
334 genl_unlock();
335
336 return -ENOENT;
337}
338
339/**
340 * genl_register_family - register a generic netlink family
341 * @family: generic netlink family
342 *
343 * Registers the specified family after validating it first. Only one
344 * family may be registered with the same family name or identifier.
345 * The family id may equal GENL_ID_GENERATE causing an unique id to
346 * be automatically generated and assigned.
347 *
348 * Return 0 on success or a negative error code.
349 */
350int genl_register_family(struct genl_family *family)
351{
352 int err = -EINVAL;
353
354 if (family->id && family->id < GENL_MIN_ID)
355 goto errout;
356
357 if (family->id > GENL_MAX_ID)
358 goto errout;
359
360 INIT_LIST_HEAD(&family->ops_list);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700361 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100362
363 genl_lock();
364
365 if (genl_family_find_byname(family->name)) {
366 err = -EEXIST;
367 goto errout_locked;
368 }
369
Thomas Graf482a8522005-11-10 02:25:56 +0100370 if (family->id == GENL_ID_GENERATE) {
371 u16 newid = genl_generate_id();
372
373 if (!newid) {
374 err = -ENOMEM;
375 goto errout_locked;
376 }
377
378 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000379 } else if (genl_family_find_byid(family->id)) {
380 err = -EEXIST;
381 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100382 }
383
384 if (family->maxattr) {
385 family->attrbuf = kmalloc((family->maxattr+1) *
386 sizeof(struct nlattr *), GFP_KERNEL);
387 if (family->attrbuf == NULL) {
388 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800389 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100390 }
391 } else
392 family->attrbuf = NULL;
393
394 list_add_tail(&family->family_list, genl_family_chain(family->id));
395 genl_unlock();
396
397 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
398
399 return 0;
400
401errout_locked:
402 genl_unlock();
403errout:
404 return err;
405}
406
407/**
Michał Mirosława7b11d72009-05-21 10:34:04 +0000408 * genl_register_family_with_ops - register a generic netlink family
409 * @family: generic netlink family
410 * @ops: operations to be registered
411 * @n_ops: number of elements to register
412 *
413 * Registers the specified family and operations from the specified table.
414 * Only one family may be registered with the same family name or identifier.
415 *
416 * The family id may equal GENL_ID_GENERATE causing an unique id to
417 * be automatically generated and assigned.
418 *
419 * Either a doit or dumpit callback must be specified for every registered
420 * operation or the function will fail. Only one operation structure per
421 * command identifier may be registered.
422 *
423 * See include/net/genetlink.h for more documenation on the operations
424 * structure.
425 *
426 * This is equivalent to calling genl_register_family() followed by
427 * genl_register_ops() for every operation entry in the table taking
428 * care to unregister the family on error path.
429 *
430 * Return 0 on success or a negative error code.
431 */
432int genl_register_family_with_ops(struct genl_family *family,
433 struct genl_ops *ops, size_t n_ops)
434{
435 int err, i;
436
437 err = genl_register_family(family);
438 if (err)
439 return err;
440
441 for (i = 0; i < n_ops; ++i, ++ops) {
442 err = genl_register_ops(family, ops);
443 if (err)
444 goto err_out;
445 }
446 return 0;
447err_out:
448 genl_unregister_family(family);
449 return err;
450}
451EXPORT_SYMBOL(genl_register_family_with_ops);
452
453/**
Thomas Graf482a8522005-11-10 02:25:56 +0100454 * genl_unregister_family - unregister generic netlink family
455 * @family: generic netlink family
456 *
457 * Unregisters the specified family.
458 *
459 * Returns 0 on success or a negative error code.
460 */
461int genl_unregister_family(struct genl_family *family)
462{
463 struct genl_family *rc;
464
465 genl_lock();
466
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800467 genl_unregister_mc_groups(family);
468
Thomas Graf482a8522005-11-10 02:25:56 +0100469 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
470 if (family->id != rc->id || strcmp(rc->name, family->name))
471 continue;
472
473 list_del(&rc->family_list);
474 INIT_LIST_HEAD(&family->ops_list);
475 genl_unlock();
476
Thomas Graf482a8522005-11-10 02:25:56 +0100477 kfree(family->attrbuf);
478 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
479 return 0;
480 }
481
482 genl_unlock();
483
484 return -ENOENT;
485}
486
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700487static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100488{
489 struct genl_ops *ops;
490 struct genl_family *family;
Johannes Berg134e6372009-07-10 09:51:34 +0000491 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100492 struct genl_info info;
493 struct genlmsghdr *hdr = nlmsg_data(nlh);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700494 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100495
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900496 family = genl_family_find_byid(nlh->nlmsg_type);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700497 if (family == NULL)
498 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100499
Johannes Berg134e6372009-07-10 09:51:34 +0000500 /* this family doesn't exist in this netns */
501 if (!family->netnsok && !net_eq(net, &init_net))
502 return -ENOENT;
503
Thomas Graf482a8522005-11-10 02:25:56 +0100504 hdrlen = GENL_HDRLEN + family->hdrsize;
505 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700506 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100507
508 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700509 if (ops == NULL)
510 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100511
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700512 if ((ops->flags & GENL_ADMIN_PERM) &&
513 security_netlink_recv(skb, CAP_NET_ADMIN))
514 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100515
516 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700517 if (ops->dumpit == NULL)
518 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100519
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700520 genl_unlock();
Johannes Berg134e6372009-07-10 09:51:34 +0000521 err = netlink_dump_start(net->genl_sock, skb, nlh,
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700522 ops->dumpit, ops->done);
523 genl_lock();
524 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100525 }
526
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700527 if (ops->doit == NULL)
528 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100529
530 if (family->attrbuf) {
531 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
532 ops->policy);
533 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700534 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100535 }
536
537 info.snd_seq = nlh->nlmsg_seq;
538 info.snd_pid = NETLINK_CB(skb).pid;
539 info.nlhdr = nlh;
540 info.genlhdr = nlmsg_data(nlh);
541 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
542 info.attrs = family->attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000543 genl_info_net_set(&info, net);
Thomas Graf482a8522005-11-10 02:25:56 +0100544
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700545 return ops->doit(skb, &info);
Thomas Graf482a8522005-11-10 02:25:56 +0100546}
547
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700548static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100549{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700550 genl_lock();
551 netlink_rcv_skb(skb, &genl_rcv_msg);
552 genl_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100553}
554
555/**************************************************************************
556 * Controller
557 **************************************************************************/
558
Thomas Graf17c157c2006-11-14 19:46:02 -0800559static struct genl_family genl_ctrl = {
560 .id = GENL_ID_CTRL,
561 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800562 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800563 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000564 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800565};
566
Thomas Graf482a8522005-11-10 02:25:56 +0100567static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
568 u32 flags, struct sk_buff *skb, u8 cmd)
569{
570 void *hdr;
571
Thomas Graf17c157c2006-11-14 19:46:02 -0800572 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100573 if (hdr == NULL)
574 return -1;
575
576 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
577 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
Thomas Grafeb328112006-09-18 00:01:59 -0700578 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
579 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
580 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
581
Thomas Grafe94ef682006-11-23 11:44:37 -0800582 if (!list_empty(&family->ops_list)) {
583 struct nlattr *nla_ops;
584 struct genl_ops *ops;
585 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700586
Thomas Grafe94ef682006-11-23 11:44:37 -0800587 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
588 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700589 goto nla_put_failure;
590
Thomas Grafe94ef682006-11-23 11:44:37 -0800591 list_for_each_entry(ops, &family->ops_list, ops_list) {
592 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700593
Thomas Grafe94ef682006-11-23 11:44:37 -0800594 nest = nla_nest_start(skb, idx++);
595 if (nest == NULL)
596 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700597
Thomas Grafe94ef682006-11-23 11:44:37 -0800598 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
599 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
Thomas Grafeb328112006-09-18 00:01:59 -0700600
Thomas Grafe94ef682006-11-23 11:44:37 -0800601 nla_nest_end(skb, nest);
602 }
603
604 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700605 }
606
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700607 if (!list_empty(&family->mcast_groups)) {
608 struct genl_multicast_group *grp;
609 struct nlattr *nla_grps;
610 int idx = 1;
611
612 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
613 if (nla_grps == NULL)
614 goto nla_put_failure;
615
616 list_for_each_entry(grp, &family->mcast_groups, list) {
617 struct nlattr *nest;
618
619 nest = nla_nest_start(skb, idx++);
620 if (nest == NULL)
621 goto nla_put_failure;
622
623 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
624 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
625 grp->name);
626
627 nla_nest_end(skb, nest);
628 }
629 nla_nest_end(skb, nla_grps);
630 }
631
632 return genlmsg_end(skb, hdr);
633
634nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700635 genlmsg_cancel(skb, hdr);
636 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700637}
638
639static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
640 u32 seq, u32 flags, struct sk_buff *skb,
641 u8 cmd)
642{
643 void *hdr;
644 struct nlattr *nla_grps;
645 struct nlattr *nest;
646
647 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
648 if (hdr == NULL)
649 return -1;
650
651 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
652 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
653
654 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
655 if (nla_grps == NULL)
656 goto nla_put_failure;
657
658 nest = nla_nest_start(skb, 1);
659 if (nest == NULL)
660 goto nla_put_failure;
661
662 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
663 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
664 grp->name);
665
666 nla_nest_end(skb, nest);
667 nla_nest_end(skb, nla_grps);
668
Thomas Graf482a8522005-11-10 02:25:56 +0100669 return genlmsg_end(skb, hdr);
670
671nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700672 genlmsg_cancel(skb, hdr);
673 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100674}
675
676static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
677{
678
679 int i, n = 0;
680 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000681 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100682 int chains_to_skip = cb->args[0];
683 int fams_to_skip = cb->args[1];
684
Samir Bellabese1d5a012010-01-07 22:10:56 +0000685 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100686 n = 0;
687 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000688 if (!rt->netnsok && !net_eq(net, &init_net))
689 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100690 if (++n < fams_to_skip)
691 continue;
692 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
693 cb->nlh->nlmsg_seq, NLM_F_MULTI,
694 skb, CTRL_CMD_NEWFAMILY) < 0)
695 goto errout;
696 }
697
698 fams_to_skip = 0;
699 }
700
701errout:
702 cb->args[0] = i;
703 cb->args[1] = n;
704
705 return skb->len;
706}
707
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700708static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
709 u32 pid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100710{
711 struct sk_buff *skb;
712 int err;
713
Thomas Graf339bf982006-11-10 14:10:15 -0800714 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100715 if (skb == NULL)
716 return ERR_PTR(-ENOBUFS);
717
718 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
719 if (err < 0) {
720 nlmsg_free(skb);
721 return ERR_PTR(err);
722 }
723
724 return skb;
725}
726
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700727static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
728 u32 pid, int seq, u8 cmd)
729{
730 struct sk_buff *skb;
731 int err;
732
733 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
734 if (skb == NULL)
735 return ERR_PTR(-ENOBUFS);
736
737 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
738 if (err < 0) {
739 nlmsg_free(skb);
740 return ERR_PTR(err);
741 }
742
743 return skb;
744}
745
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700746static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100747 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700748 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
749 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100750};
751
752static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
753{
754 struct sk_buff *msg;
755 struct genl_family *res = NULL;
756 int err = -EINVAL;
757
758 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
759 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
760 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000761 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100762 }
763
764 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700765 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100766
Thomas Graf5176f912006-08-26 20:13:18 -0700767 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100768 res = genl_family_find_byname(name);
Johannes Berg134e6372009-07-10 09:51:34 +0000769 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100770 }
771
Johannes Berg134e6372009-07-10 09:51:34 +0000772 if (res == NULL)
773 return err;
774
775 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
776 /* family doesn't exist here */
777 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100778 }
779
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700780 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
781 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000782 if (IS_ERR(msg))
783 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100784
Johannes Berg134e6372009-07-10 09:51:34 +0000785 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100786}
787
788static int genl_ctrl_event(int event, void *data)
789{
790 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000791 struct genl_family *family;
792 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100793
Johannes Berg134e6372009-07-10 09:51:34 +0000794 /* genl is still initialising */
795 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100796 return 0;
797
798 switch (event) {
799 case CTRL_CMD_NEWFAMILY:
800 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000801 family = data;
802 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700803 break;
804 case CTRL_CMD_NEWMCAST_GRP:
805 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000806 grp = data;
807 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700808 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100809 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000810 default:
811 return -EINVAL;
812 }
813
814 if (IS_ERR(msg))
815 return PTR_ERR(msg);
816
817 if (!family->netnsok) {
818 genlmsg_multicast_netns(&init_net, msg, 0,
819 GENL_ID_CTRL, GFP_KERNEL);
820 } else {
821 rcu_read_lock();
822 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
823 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100824 }
825
826 return 0;
827}
828
829static struct genl_ops genl_ctrl_ops = {
830 .cmd = CTRL_CMD_GETFAMILY,
831 .doit = ctrl_getfamily,
832 .dumpit = ctrl_dumpfamily,
833 .policy = ctrl_policy,
834};
835
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700836static struct genl_multicast_group notify_grp = {
837 .name = "notify",
838};
839
Johannes Berg134e6372009-07-10 09:51:34 +0000840static int __net_init genl_pernet_init(struct net *net)
841{
842 /* we'll bump the group number right afterwards */
843 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
844 genl_rcv, &genl_mutex,
845 THIS_MODULE);
846
847 if (!net->genl_sock && net_eq(net, &init_net))
848 panic("GENL: Cannot initialize generic netlink\n");
849
850 if (!net->genl_sock)
851 return -ENOMEM;
852
853 return 0;
854}
855
856static void __net_exit genl_pernet_exit(struct net *net)
857{
858 netlink_kernel_release(net->genl_sock);
859 net->genl_sock = NULL;
860}
861
862static struct pernet_operations genl_pernet_ops = {
863 .init = genl_pernet_init,
864 .exit = genl_pernet_exit,
865};
866
Thomas Graf482a8522005-11-10 02:25:56 +0100867static int __init genl_init(void)
868{
869 int i, err;
870
871 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
872 INIT_LIST_HEAD(&family_ht[i]);
873
874 err = genl_register_family(&genl_ctrl);
875 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000876 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100877
878 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
879 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000880 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100881
882 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700883
Johannes Berg134e6372009-07-10 09:51:34 +0000884 err = register_pernet_subsys(&genl_pernet_ops);
885 if (err)
886 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100887
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700888 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
889 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000890 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700891
Thomas Graf482a8522005-11-10 02:25:56 +0100892 return 0;
893
Johannes Berg134e6372009-07-10 09:51:34 +0000894problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100895 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100896}
897
898subsys_initcall(genl_init);
899
Thomas Graf482a8522005-11-10 02:25:56 +0100900EXPORT_SYMBOL(genl_register_ops);
901EXPORT_SYMBOL(genl_unregister_ops);
902EXPORT_SYMBOL(genl_register_family);
903EXPORT_SYMBOL(genl_unregister_family);
Johannes Berg134e6372009-07-10 09:51:34 +0000904
905static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
906 gfp_t flags)
907{
908 struct sk_buff *tmp;
909 struct net *net, *prev = NULL;
910 int err;
911
912 for_each_net_rcu(net) {
913 if (prev) {
914 tmp = skb_clone(skb, flags);
915 if (!tmp) {
916 err = -ENOMEM;
917 goto error;
918 }
919 err = nlmsg_multicast(prev->genl_sock, tmp,
920 pid, group, flags);
921 if (err)
922 goto error;
923 }
924
925 prev = net;
926 }
927
928 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
929 error:
930 kfree_skb(skb);
931 return err;
932}
933
934int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
935 gfp_t flags)
936{
937 return genlmsg_mcast(skb, pid, group, flags);
938}
939EXPORT_SYMBOL(genlmsg_multicast_allns);