blob: a4b6e148c5dec4ceba8a4aa5ef964353433e2d6a [file] [log] [blame]
Thomas Graf482a8522005-11-10 02:25:56 +01001/*
2 * NETLINK Generic Netlink Family
3 *
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
Johannes Berg2dbba6f2007-07-18 15:47:52 -07006 * Johannes Berg <johannes@sipsolutions.net>
Thomas Graf482a8522005-11-10 02:25:56 +01007 */
8
Thomas Graf482a8522005-11-10 02:25:56 +01009#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/types.h>
13#include <linux/socket.h>
14#include <linux/string.h>
15#include <linux/skbuff.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080016#include <linux/mutex.h>
Johannes Berg2dbba6f2007-07-18 15:47:52 -070017#include <linux/bitmap.h>
Thomas Graf482a8522005-11-10 02:25:56 +010018#include <net/sock.h>
19#include <net/genetlink.h>
20
Ingo Molnar14cc3e22006-03-26 01:37:14 -080021static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
Thomas Graf482a8522005-11-10 02:25:56 +010022
Denis V. Lunev3b715352007-10-10 21:13:32 -070023static inline void genl_lock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010024{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080025 mutex_lock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010026}
27
Denis V. Lunev3b715352007-10-10 21:13:32 -070028static inline void genl_unlock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010029{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080030 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010031}
32
33#define GENL_FAM_TAB_SIZE 16
34#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
35
36static struct list_head family_ht[GENL_FAM_TAB_SIZE];
Johannes Berg2dbba6f2007-07-18 15:47:52 -070037/*
38 * Bitmap of multicast groups that are currently in use.
39 *
40 * To avoid an allocation at boot of just one unsigned long,
41 * declare it global instead.
42 * Bit 0 is marked as already used since group 0 is invalid.
43 */
44static unsigned long mc_group_start = 0x1;
45static unsigned long *mc_groups = &mc_group_start;
46static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010047
48static int genl_ctrl_event(int event, void *data);
49
50static inline unsigned int genl_family_hash(unsigned int id)
51{
52 return id & GENL_FAM_TAB_MASK;
53}
54
55static inline struct list_head *genl_family_chain(unsigned int id)
56{
57 return &family_ht[genl_family_hash(id)];
58}
59
60static struct genl_family *genl_family_find_byid(unsigned int id)
61{
62 struct genl_family *f;
63
64 list_for_each_entry(f, genl_family_chain(id), family_list)
65 if (f->id == id)
66 return f;
67
68 return NULL;
69}
70
71static struct genl_family *genl_family_find_byname(char *name)
72{
73 struct genl_family *f;
74 int i;
75
76 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
77 list_for_each_entry(f, genl_family_chain(i), family_list)
78 if (strcmp(f->name, name) == 0)
79 return f;
80
81 return NULL;
82}
83
84static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
85{
86 struct genl_ops *ops;
87
88 list_for_each_entry(ops, &family->ops_list, ops_list)
89 if (ops->cmd == cmd)
90 return ops;
91
92 return NULL;
93}
94
95/* Of course we are going to have problems once we hit
96 * 2^16 alive types, but that can only happen by year 2K
97*/
98static inline u16 genl_generate_id(void)
99{
Krishna Kumar988ade62009-10-14 19:55:07 +0000100 static u16 id_gen_idx = GENL_MIN_ID;
101 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100102
Krishna Kumar988ade62009-10-14 19:55:07 +0000103 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
104 if (!genl_family_find_byid(id_gen_idx))
105 return id_gen_idx;
106 if (++id_gen_idx > GENL_MAX_ID)
Thomas Graf482a8522005-11-10 02:25:56 +0100107 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000108 }
Thomas Graf482a8522005-11-10 02:25:56 +0100109
Krishna Kumar988ade62009-10-14 19:55:07 +0000110 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100111}
112
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700113static struct genl_multicast_group notify_grp;
114
115/**
116 * genl_register_mc_group - register a multicast group
117 *
118 * Registers the specified multicast group and notifies userspace
119 * about the new group.
120 *
121 * Returns 0 on success or a negative error code.
122 *
123 * @family: The generic netlink family the group shall be registered for.
124 * @grp: The group to register, must have a name.
125 */
126int genl_register_mc_group(struct genl_family *family,
127 struct genl_multicast_group *grp)
128{
129 int id;
130 unsigned long *new_groups;
Brian Haleyb1f57192009-09-04 20:36:52 -0700131 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700132
133 BUG_ON(grp->name[0] == '\0');
134
135 genl_lock();
136
137 /* special-case our own group */
138 if (grp == &notify_grp)
139 id = GENL_ID_CTRL;
140 else
141 id = find_first_zero_bit(mc_groups,
142 mc_groups_longs * BITS_PER_LONG);
143
144
145 if (id >= mc_groups_longs * BITS_PER_LONG) {
146 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
147
148 if (mc_groups == &mc_group_start) {
149 new_groups = kzalloc(nlen, GFP_KERNEL);
150 if (!new_groups) {
151 err = -ENOMEM;
152 goto out;
153 }
154 mc_groups = new_groups;
155 *mc_groups = mc_group_start;
156 } else {
157 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
158 if (!new_groups) {
159 err = -ENOMEM;
160 goto out;
161 }
162 mc_groups = new_groups;
163 mc_groups[mc_groups_longs] = 0;
164 }
165 mc_groups_longs++;
166 }
167
Johannes Berg134e6372009-07-10 09:51:34 +0000168 if (family->netnsok) {
169 struct net *net;
170
Johannes Bergd136f1b2009-09-12 03:03:15 +0000171 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000172 rcu_read_lock();
173 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000174 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000175 mc_groups_longs * BITS_PER_LONG);
176 if (err) {
177 /*
178 * No need to roll back, can only fail if
179 * memory allocation fails and then the
180 * number of _possible_ groups has been
181 * increased on some sockets which is ok.
182 */
183 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000184 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000185 goto out;
186 }
187 }
188 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000189 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000190 } else {
191 err = netlink_change_ngroups(init_net.genl_sock,
192 mc_groups_longs * BITS_PER_LONG);
193 if (err)
194 goto out;
195 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700196
197 grp->id = id;
198 set_bit(id, mc_groups);
199 list_add_tail(&grp->list, &family->mcast_groups);
200 grp->family = family;
201
202 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
203 out:
204 genl_unlock();
Thomas Graf79d310d2007-07-24 15:34:53 -0700205 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700206}
207EXPORT_SYMBOL(genl_register_mc_group);
208
Thomas Graf79dc43862007-07-24 15:32:46 -0700209static void __genl_unregister_mc_group(struct genl_family *family,
210 struct genl_multicast_group *grp)
211{
Johannes Berg134e6372009-07-10 09:51:34 +0000212 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700213 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000214
Johannes Bergb8273572009-09-24 15:44:05 -0700215 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000216 rcu_read_lock();
217 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700218 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000219 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700220 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000221
Thomas Graf79dc43862007-07-24 15:32:46 -0700222 clear_bit(grp->id, mc_groups);
223 list_del(&grp->list);
224 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
225 grp->id = 0;
226 grp->family = NULL;
227}
228
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700229/**
230 * genl_unregister_mc_group - unregister a multicast group
231 *
232 * Unregisters the specified multicast group and notifies userspace
233 * about it. All current listeners on the group are removed.
234 *
235 * Note: It is not necessary to unregister all multicast groups before
236 * unregistering the family, unregistering the family will cause
237 * all assigned multicast groups to be unregistered automatically.
238 *
239 * @family: Generic netlink family the group belongs to.
240 * @grp: The group to unregister, must have been registered successfully
241 * previously.
242 */
243void genl_unregister_mc_group(struct genl_family *family,
244 struct genl_multicast_group *grp)
245{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700246 genl_lock();
Thomas Graf79dc43862007-07-24 15:32:46 -0700247 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700248 genl_unlock();
249}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800250EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700251
252static void genl_unregister_mc_groups(struct genl_family *family)
253{
254 struct genl_multicast_group *grp, *tmp;
255
256 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700257 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700258}
259
Thomas Graf482a8522005-11-10 02:25:56 +0100260/**
261 * genl_register_ops - register generic netlink operations
262 * @family: generic netlink family
263 * @ops: operations to be registered
264 *
265 * Registers the specified operations and assigns them to the specified
266 * family. Either a doit or dumpit callback must be specified or the
267 * operation will fail. Only one operation structure per command
268 * identifier may be registered.
269 *
270 * See include/net/genetlink.h for more documenation on the operations
271 * structure.
272 *
273 * Returns 0 on success or a negative error code.
274 */
275int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
276{
277 int err = -EINVAL;
278
279 if (ops->dumpit == NULL && ops->doit == NULL)
280 goto errout;
281
282 if (genl_get_cmd(ops->cmd, family)) {
283 err = -EEXIST;
284 goto errout;
285 }
286
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800287 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800288 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800289 if (ops->doit)
290 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800291 if (ops->policy)
292 ops->flags |= GENL_CMD_CAP_HASPOL;
293
Thomas Graf482a8522005-11-10 02:25:56 +0100294 genl_lock();
295 list_add_tail(&ops->ops_list, &family->ops_list);
296 genl_unlock();
297
298 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
299 err = 0;
300errout:
301 return err;
302}
303
304/**
305 * genl_unregister_ops - unregister generic netlink operations
306 * @family: generic netlink family
307 * @ops: operations to be unregistered
308 *
309 * Unregisters the specified operations and unassigns them from the
310 * specified family. The operation blocks until the current message
311 * processing has finished and doesn't start again until the
312 * unregister process has finished.
313 *
314 * Note: It is not necessary to unregister all operations before
315 * unregistering the family, unregistering the family will cause
316 * all assigned operations to be unregistered automatically.
317 *
318 * Returns 0 on success or a negative error code.
319 */
320int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
321{
322 struct genl_ops *rc;
323
324 genl_lock();
325 list_for_each_entry(rc, &family->ops_list, ops_list) {
326 if (rc == ops) {
327 list_del(&ops->ops_list);
328 genl_unlock();
329 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
330 return 0;
331 }
332 }
333 genl_unlock();
334
335 return -ENOENT;
336}
337
338/**
339 * genl_register_family - register a generic netlink family
340 * @family: generic netlink family
341 *
342 * Registers the specified family after validating it first. Only one
343 * family may be registered with the same family name or identifier.
344 * The family id may equal GENL_ID_GENERATE causing an unique id to
345 * be automatically generated and assigned.
346 *
347 * Return 0 on success or a negative error code.
348 */
349int genl_register_family(struct genl_family *family)
350{
351 int err = -EINVAL;
352
353 if (family->id && family->id < GENL_MIN_ID)
354 goto errout;
355
356 if (family->id > GENL_MAX_ID)
357 goto errout;
358
359 INIT_LIST_HEAD(&family->ops_list);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700360 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100361
362 genl_lock();
363
364 if (genl_family_find_byname(family->name)) {
365 err = -EEXIST;
366 goto errout_locked;
367 }
368
Thomas Graf482a8522005-11-10 02:25:56 +0100369 if (family->id == GENL_ID_GENERATE) {
370 u16 newid = genl_generate_id();
371
372 if (!newid) {
373 err = -ENOMEM;
374 goto errout_locked;
375 }
376
377 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000378 } else if (genl_family_find_byid(family->id)) {
379 err = -EEXIST;
380 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100381 }
382
383 if (family->maxattr) {
384 family->attrbuf = kmalloc((family->maxattr+1) *
385 sizeof(struct nlattr *), GFP_KERNEL);
386 if (family->attrbuf == NULL) {
387 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800388 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100389 }
390 } else
391 family->attrbuf = NULL;
392
393 list_add_tail(&family->family_list, genl_family_chain(family->id));
394 genl_unlock();
395
396 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
397
398 return 0;
399
400errout_locked:
401 genl_unlock();
402errout:
403 return err;
404}
405
406/**
Michał Mirosława7b11d72009-05-21 10:34:04 +0000407 * genl_register_family_with_ops - register a generic netlink family
408 * @family: generic netlink family
409 * @ops: operations to be registered
410 * @n_ops: number of elements to register
411 *
412 * Registers the specified family and operations from the specified table.
413 * Only one family may be registered with the same family name or identifier.
414 *
415 * The family id may equal GENL_ID_GENERATE causing an unique id to
416 * be automatically generated and assigned.
417 *
418 * Either a doit or dumpit callback must be specified for every registered
419 * operation or the function will fail. Only one operation structure per
420 * command identifier may be registered.
421 *
422 * See include/net/genetlink.h for more documenation on the operations
423 * structure.
424 *
425 * This is equivalent to calling genl_register_family() followed by
426 * genl_register_ops() for every operation entry in the table taking
427 * care to unregister the family on error path.
428 *
429 * Return 0 on success or a negative error code.
430 */
431int genl_register_family_with_ops(struct genl_family *family,
432 struct genl_ops *ops, size_t n_ops)
433{
434 int err, i;
435
436 err = genl_register_family(family);
437 if (err)
438 return err;
439
440 for (i = 0; i < n_ops; ++i, ++ops) {
441 err = genl_register_ops(family, ops);
442 if (err)
443 goto err_out;
444 }
445 return 0;
446err_out:
447 genl_unregister_family(family);
448 return err;
449}
450EXPORT_SYMBOL(genl_register_family_with_ops);
451
452/**
Thomas Graf482a8522005-11-10 02:25:56 +0100453 * genl_unregister_family - unregister generic netlink family
454 * @family: generic netlink family
455 *
456 * Unregisters the specified family.
457 *
458 * Returns 0 on success or a negative error code.
459 */
460int genl_unregister_family(struct genl_family *family)
461{
462 struct genl_family *rc;
463
464 genl_lock();
465
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800466 genl_unregister_mc_groups(family);
467
Thomas Graf482a8522005-11-10 02:25:56 +0100468 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
469 if (family->id != rc->id || strcmp(rc->name, family->name))
470 continue;
471
472 list_del(&rc->family_list);
473 INIT_LIST_HEAD(&family->ops_list);
474 genl_unlock();
475
Thomas Graf482a8522005-11-10 02:25:56 +0100476 kfree(family->attrbuf);
477 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
478 return 0;
479 }
480
481 genl_unlock();
482
483 return -ENOENT;
484}
485
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700486static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100487{
488 struct genl_ops *ops;
489 struct genl_family *family;
Johannes Berg134e6372009-07-10 09:51:34 +0000490 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100491 struct genl_info info;
492 struct genlmsghdr *hdr = nlmsg_data(nlh);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700493 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100494
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900495 family = genl_family_find_byid(nlh->nlmsg_type);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700496 if (family == NULL)
497 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100498
Johannes Berg134e6372009-07-10 09:51:34 +0000499 /* this family doesn't exist in this netns */
500 if (!family->netnsok && !net_eq(net, &init_net))
501 return -ENOENT;
502
Thomas Graf482a8522005-11-10 02:25:56 +0100503 hdrlen = GENL_HDRLEN + family->hdrsize;
504 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700505 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100506
507 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700508 if (ops == NULL)
509 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100510
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700511 if ((ops->flags & GENL_ADMIN_PERM) &&
512 security_netlink_recv(skb, CAP_NET_ADMIN))
513 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100514
515 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700516 if (ops->dumpit == NULL)
517 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100518
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700519 genl_unlock();
Johannes Berg134e6372009-07-10 09:51:34 +0000520 err = netlink_dump_start(net->genl_sock, skb, nlh,
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700521 ops->dumpit, ops->done);
522 genl_lock();
523 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100524 }
525
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700526 if (ops->doit == NULL)
527 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100528
529 if (family->attrbuf) {
530 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
531 ops->policy);
532 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700533 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100534 }
535
536 info.snd_seq = nlh->nlmsg_seq;
537 info.snd_pid = NETLINK_CB(skb).pid;
538 info.nlhdr = nlh;
539 info.genlhdr = nlmsg_data(nlh);
540 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
541 info.attrs = family->attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000542 genl_info_net_set(&info, net);
Thomas Graf482a8522005-11-10 02:25:56 +0100543
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700544 return ops->doit(skb, &info);
Thomas Graf482a8522005-11-10 02:25:56 +0100545}
546
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700547static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100548{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700549 genl_lock();
550 netlink_rcv_skb(skb, &genl_rcv_msg);
551 genl_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100552}
553
554/**************************************************************************
555 * Controller
556 **************************************************************************/
557
Thomas Graf17c157c2006-11-14 19:46:02 -0800558static struct genl_family genl_ctrl = {
559 .id = GENL_ID_CTRL,
560 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800561 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800562 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000563 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800564};
565
Thomas Graf482a8522005-11-10 02:25:56 +0100566static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
567 u32 flags, struct sk_buff *skb, u8 cmd)
568{
569 void *hdr;
570
Thomas Graf17c157c2006-11-14 19:46:02 -0800571 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100572 if (hdr == NULL)
573 return -1;
574
575 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
576 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
Thomas Grafeb328112006-09-18 00:01:59 -0700577 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
578 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
579 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
580
Thomas Grafe94ef682006-11-23 11:44:37 -0800581 if (!list_empty(&family->ops_list)) {
582 struct nlattr *nla_ops;
583 struct genl_ops *ops;
584 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700585
Thomas Grafe94ef682006-11-23 11:44:37 -0800586 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
587 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700588 goto nla_put_failure;
589
Thomas Grafe94ef682006-11-23 11:44:37 -0800590 list_for_each_entry(ops, &family->ops_list, ops_list) {
591 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700592
Thomas Grafe94ef682006-11-23 11:44:37 -0800593 nest = nla_nest_start(skb, idx++);
594 if (nest == NULL)
595 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700596
Thomas Grafe94ef682006-11-23 11:44:37 -0800597 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
598 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
Thomas Grafeb328112006-09-18 00:01:59 -0700599
Thomas Grafe94ef682006-11-23 11:44:37 -0800600 nla_nest_end(skb, nest);
601 }
602
603 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700604 }
605
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700606 if (!list_empty(&family->mcast_groups)) {
607 struct genl_multicast_group *grp;
608 struct nlattr *nla_grps;
609 int idx = 1;
610
611 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
612 if (nla_grps == NULL)
613 goto nla_put_failure;
614
615 list_for_each_entry(grp, &family->mcast_groups, list) {
616 struct nlattr *nest;
617
618 nest = nla_nest_start(skb, idx++);
619 if (nest == NULL)
620 goto nla_put_failure;
621
622 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
623 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
624 grp->name);
625
626 nla_nest_end(skb, nest);
627 }
628 nla_nest_end(skb, nla_grps);
629 }
630
631 return genlmsg_end(skb, hdr);
632
633nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700634 genlmsg_cancel(skb, hdr);
635 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700636}
637
638static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
639 u32 seq, u32 flags, struct sk_buff *skb,
640 u8 cmd)
641{
642 void *hdr;
643 struct nlattr *nla_grps;
644 struct nlattr *nest;
645
646 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
647 if (hdr == NULL)
648 return -1;
649
650 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
651 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
652
653 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
654 if (nla_grps == NULL)
655 goto nla_put_failure;
656
657 nest = nla_nest_start(skb, 1);
658 if (nest == NULL)
659 goto nla_put_failure;
660
661 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
662 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
663 grp->name);
664
665 nla_nest_end(skb, nest);
666 nla_nest_end(skb, nla_grps);
667
Thomas Graf482a8522005-11-10 02:25:56 +0100668 return genlmsg_end(skb, hdr);
669
670nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700671 genlmsg_cancel(skb, hdr);
672 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100673}
674
675static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
676{
677
678 int i, n = 0;
679 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000680 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100681 int chains_to_skip = cb->args[0];
682 int fams_to_skip = cb->args[1];
683
Samir Bellabese1d5a012010-01-07 22:10:56 +0000684 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100685 n = 0;
686 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000687 if (!rt->netnsok && !net_eq(net, &init_net))
688 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100689 if (++n < fams_to_skip)
690 continue;
691 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
692 cb->nlh->nlmsg_seq, NLM_F_MULTI,
693 skb, CTRL_CMD_NEWFAMILY) < 0)
694 goto errout;
695 }
696
697 fams_to_skip = 0;
698 }
699
700errout:
701 cb->args[0] = i;
702 cb->args[1] = n;
703
704 return skb->len;
705}
706
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700707static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
708 u32 pid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100709{
710 struct sk_buff *skb;
711 int err;
712
Thomas Graf339bf982006-11-10 14:10:15 -0800713 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100714 if (skb == NULL)
715 return ERR_PTR(-ENOBUFS);
716
717 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
718 if (err < 0) {
719 nlmsg_free(skb);
720 return ERR_PTR(err);
721 }
722
723 return skb;
724}
725
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700726static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
727 u32 pid, int seq, u8 cmd)
728{
729 struct sk_buff *skb;
730 int err;
731
732 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
733 if (skb == NULL)
734 return ERR_PTR(-ENOBUFS);
735
736 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
737 if (err < 0) {
738 nlmsg_free(skb);
739 return ERR_PTR(err);
740 }
741
742 return skb;
743}
744
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700745static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100746 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700747 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
748 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100749};
750
751static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
752{
753 struct sk_buff *msg;
754 struct genl_family *res = NULL;
755 int err = -EINVAL;
756
757 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
758 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
759 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000760 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100761 }
762
763 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700764 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100765
Thomas Graf5176f912006-08-26 20:13:18 -0700766 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100767 res = genl_family_find_byname(name);
Johannes Berg134e6372009-07-10 09:51:34 +0000768 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100769 }
770
Johannes Berg134e6372009-07-10 09:51:34 +0000771 if (res == NULL)
772 return err;
773
774 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
775 /* family doesn't exist here */
776 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100777 }
778
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700779 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
780 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000781 if (IS_ERR(msg))
782 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100783
Johannes Berg134e6372009-07-10 09:51:34 +0000784 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100785}
786
787static int genl_ctrl_event(int event, void *data)
788{
789 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000790 struct genl_family *family;
791 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100792
Johannes Berg134e6372009-07-10 09:51:34 +0000793 /* genl is still initialising */
794 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100795 return 0;
796
797 switch (event) {
798 case CTRL_CMD_NEWFAMILY:
799 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000800 family = data;
801 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700802 break;
803 case CTRL_CMD_NEWMCAST_GRP:
804 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000805 grp = data;
806 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700807 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100808 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000809 default:
810 return -EINVAL;
811 }
812
813 if (IS_ERR(msg))
814 return PTR_ERR(msg);
815
816 if (!family->netnsok) {
817 genlmsg_multicast_netns(&init_net, msg, 0,
818 GENL_ID_CTRL, GFP_KERNEL);
819 } else {
820 rcu_read_lock();
821 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
822 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100823 }
824
825 return 0;
826}
827
828static struct genl_ops genl_ctrl_ops = {
829 .cmd = CTRL_CMD_GETFAMILY,
830 .doit = ctrl_getfamily,
831 .dumpit = ctrl_dumpfamily,
832 .policy = ctrl_policy,
833};
834
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700835static struct genl_multicast_group notify_grp = {
836 .name = "notify",
837};
838
Johannes Berg134e6372009-07-10 09:51:34 +0000839static int __net_init genl_pernet_init(struct net *net)
840{
841 /* we'll bump the group number right afterwards */
842 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
843 genl_rcv, &genl_mutex,
844 THIS_MODULE);
845
846 if (!net->genl_sock && net_eq(net, &init_net))
847 panic("GENL: Cannot initialize generic netlink\n");
848
849 if (!net->genl_sock)
850 return -ENOMEM;
851
852 return 0;
853}
854
855static void __net_exit genl_pernet_exit(struct net *net)
856{
857 netlink_kernel_release(net->genl_sock);
858 net->genl_sock = NULL;
859}
860
861static struct pernet_operations genl_pernet_ops = {
862 .init = genl_pernet_init,
863 .exit = genl_pernet_exit,
864};
865
Thomas Graf482a8522005-11-10 02:25:56 +0100866static int __init genl_init(void)
867{
868 int i, err;
869
870 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
871 INIT_LIST_HEAD(&family_ht[i]);
872
873 err = genl_register_family(&genl_ctrl);
874 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000875 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100876
877 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
878 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000879 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100880
881 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700882
Johannes Berg134e6372009-07-10 09:51:34 +0000883 err = register_pernet_subsys(&genl_pernet_ops);
884 if (err)
885 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100886
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700887 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
888 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000889 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700890
Thomas Graf482a8522005-11-10 02:25:56 +0100891 return 0;
892
Johannes Berg134e6372009-07-10 09:51:34 +0000893problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100894 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100895}
896
897subsys_initcall(genl_init);
898
Thomas Graf482a8522005-11-10 02:25:56 +0100899EXPORT_SYMBOL(genl_register_ops);
900EXPORT_SYMBOL(genl_unregister_ops);
901EXPORT_SYMBOL(genl_register_family);
902EXPORT_SYMBOL(genl_unregister_family);
Johannes Berg134e6372009-07-10 09:51:34 +0000903
904static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
905 gfp_t flags)
906{
907 struct sk_buff *tmp;
908 struct net *net, *prev = NULL;
909 int err;
910
911 for_each_net_rcu(net) {
912 if (prev) {
913 tmp = skb_clone(skb, flags);
914 if (!tmp) {
915 err = -ENOMEM;
916 goto error;
917 }
918 err = nlmsg_multicast(prev->genl_sock, tmp,
919 pid, group, flags);
920 if (err)
921 goto error;
922 }
923
924 prev = net;
925 }
926
927 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
928 error:
929 kfree_skb(skb);
930 return err;
931}
932
933int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
934 gfp_t flags)
935{
936 return genlmsg_mcast(skb, pid, group, flags);
937}
938EXPORT_SYMBOL(genlmsg_multicast_allns);