blob: ddfdb7d2e02bc0ddb9d5ea16efca211941053989 [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{
100 static u16 id_gen_idx;
101 int overflowed = 0;
102
103 do {
104 if (id_gen_idx == 0)
105 id_gen_idx = GENL_MIN_ID;
106
107 if (++id_gen_idx > GENL_MAX_ID) {
108 if (!overflowed) {
109 overflowed = 1;
110 id_gen_idx = 0;
111 continue;
112 } else
113 return 0;
114 }
115
116 } while (genl_family_find_byid(id_gen_idx));
117
118 return id_gen_idx;
119}
120
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700121static struct genl_multicast_group notify_grp;
122
123/**
124 * genl_register_mc_group - register a multicast group
125 *
126 * Registers the specified multicast group and notifies userspace
127 * about the new group.
128 *
129 * Returns 0 on success or a negative error code.
130 *
131 * @family: The generic netlink family the group shall be registered for.
132 * @grp: The group to register, must have a name.
133 */
134int genl_register_mc_group(struct genl_family *family,
135 struct genl_multicast_group *grp)
136{
137 int id;
138 unsigned long *new_groups;
Brian Haleyb1f57192009-09-04 20:36:52 -0700139 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700140
141 BUG_ON(grp->name[0] == '\0');
142
143 genl_lock();
144
145 /* special-case our own group */
146 if (grp == &notify_grp)
147 id = GENL_ID_CTRL;
148 else
149 id = find_first_zero_bit(mc_groups,
150 mc_groups_longs * BITS_PER_LONG);
151
152
153 if (id >= mc_groups_longs * BITS_PER_LONG) {
154 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
155
156 if (mc_groups == &mc_group_start) {
157 new_groups = kzalloc(nlen, GFP_KERNEL);
158 if (!new_groups) {
159 err = -ENOMEM;
160 goto out;
161 }
162 mc_groups = new_groups;
163 *mc_groups = mc_group_start;
164 } else {
165 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
166 if (!new_groups) {
167 err = -ENOMEM;
168 goto out;
169 }
170 mc_groups = new_groups;
171 mc_groups[mc_groups_longs] = 0;
172 }
173 mc_groups_longs++;
174 }
175
Johannes Berg134e6372009-07-10 09:51:34 +0000176 if (family->netnsok) {
177 struct net *net;
178
Johannes Bergd136f1b2009-09-12 03:03:15 +0000179 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000180 rcu_read_lock();
181 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000182 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000183 mc_groups_longs * BITS_PER_LONG);
184 if (err) {
185 /*
186 * No need to roll back, can only fail if
187 * memory allocation fails and then the
188 * number of _possible_ groups has been
189 * increased on some sockets which is ok.
190 */
191 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000192 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000193 goto out;
194 }
195 }
196 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000197 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000198 } else {
199 err = netlink_change_ngroups(init_net.genl_sock,
200 mc_groups_longs * BITS_PER_LONG);
201 if (err)
202 goto out;
203 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700204
205 grp->id = id;
206 set_bit(id, mc_groups);
207 list_add_tail(&grp->list, &family->mcast_groups);
208 grp->family = family;
209
210 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
211 out:
212 genl_unlock();
Thomas Graf79d310d2007-07-24 15:34:53 -0700213 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700214}
215EXPORT_SYMBOL(genl_register_mc_group);
216
Thomas Graf79dc43862007-07-24 15:32:46 -0700217static void __genl_unregister_mc_group(struct genl_family *family,
218 struct genl_multicast_group *grp)
219{
Johannes Berg134e6372009-07-10 09:51:34 +0000220 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700221 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000222
Johannes Bergb8273572009-09-24 15:44:05 -0700223 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000224 rcu_read_lock();
225 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700226 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000227 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700228 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000229
Thomas Graf79dc43862007-07-24 15:32:46 -0700230 clear_bit(grp->id, mc_groups);
231 list_del(&grp->list);
232 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
233 grp->id = 0;
234 grp->family = NULL;
235}
236
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700237/**
238 * genl_unregister_mc_group - unregister a multicast group
239 *
240 * Unregisters the specified multicast group and notifies userspace
241 * about it. All current listeners on the group are removed.
242 *
243 * Note: It is not necessary to unregister all multicast groups before
244 * unregistering the family, unregistering the family will cause
245 * all assigned multicast groups to be unregistered automatically.
246 *
247 * @family: Generic netlink family the group belongs to.
248 * @grp: The group to unregister, must have been registered successfully
249 * previously.
250 */
251void genl_unregister_mc_group(struct genl_family *family,
252 struct genl_multicast_group *grp)
253{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700254 genl_lock();
Thomas Graf79dc43862007-07-24 15:32:46 -0700255 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700256 genl_unlock();
257}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800258EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700259
260static void genl_unregister_mc_groups(struct genl_family *family)
261{
262 struct genl_multicast_group *grp, *tmp;
263
264 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700265 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700266}
267
Thomas Graf482a8522005-11-10 02:25:56 +0100268/**
269 * genl_register_ops - register generic netlink operations
270 * @family: generic netlink family
271 * @ops: operations to be registered
272 *
273 * Registers the specified operations and assigns them to the specified
274 * family. Either a doit or dumpit callback must be specified or the
275 * operation will fail. Only one operation structure per command
276 * identifier may be registered.
277 *
278 * See include/net/genetlink.h for more documenation on the operations
279 * structure.
280 *
281 * Returns 0 on success or a negative error code.
282 */
283int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
284{
285 int err = -EINVAL;
286
287 if (ops->dumpit == NULL && ops->doit == NULL)
288 goto errout;
289
290 if (genl_get_cmd(ops->cmd, family)) {
291 err = -EEXIST;
292 goto errout;
293 }
294
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800295 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800296 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800297 if (ops->doit)
298 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800299 if (ops->policy)
300 ops->flags |= GENL_CMD_CAP_HASPOL;
301
Thomas Graf482a8522005-11-10 02:25:56 +0100302 genl_lock();
303 list_add_tail(&ops->ops_list, &family->ops_list);
304 genl_unlock();
305
306 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
307 err = 0;
308errout:
309 return err;
310}
311
312/**
313 * genl_unregister_ops - unregister generic netlink operations
314 * @family: generic netlink family
315 * @ops: operations to be unregistered
316 *
317 * Unregisters the specified operations and unassigns them from the
318 * specified family. The operation blocks until the current message
319 * processing has finished and doesn't start again until the
320 * unregister process has finished.
321 *
322 * Note: It is not necessary to unregister all operations before
323 * unregistering the family, unregistering the family will cause
324 * all assigned operations to be unregistered automatically.
325 *
326 * Returns 0 on success or a negative error code.
327 */
328int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
329{
330 struct genl_ops *rc;
331
332 genl_lock();
333 list_for_each_entry(rc, &family->ops_list, ops_list) {
334 if (rc == ops) {
335 list_del(&ops->ops_list);
336 genl_unlock();
337 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
338 return 0;
339 }
340 }
341 genl_unlock();
342
343 return -ENOENT;
344}
345
346/**
347 * genl_register_family - register a generic netlink family
348 * @family: generic netlink family
349 *
350 * Registers the specified family after validating it first. Only one
351 * family may be registered with the same family name or identifier.
352 * The family id may equal GENL_ID_GENERATE causing an unique id to
353 * be automatically generated and assigned.
354 *
355 * Return 0 on success or a negative error code.
356 */
357int genl_register_family(struct genl_family *family)
358{
359 int err = -EINVAL;
360
361 if (family->id && family->id < GENL_MIN_ID)
362 goto errout;
363
364 if (family->id > GENL_MAX_ID)
365 goto errout;
366
367 INIT_LIST_HEAD(&family->ops_list);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700368 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100369
370 genl_lock();
371
372 if (genl_family_find_byname(family->name)) {
373 err = -EEXIST;
374 goto errout_locked;
375 }
376
Thomas Graf482a8522005-11-10 02:25:56 +0100377 if (family->id == GENL_ID_GENERATE) {
378 u16 newid = genl_generate_id();
379
380 if (!newid) {
381 err = -ENOMEM;
382 goto errout_locked;
383 }
384
385 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000386 } else if (genl_family_find_byid(family->id)) {
387 err = -EEXIST;
388 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100389 }
390
391 if (family->maxattr) {
392 family->attrbuf = kmalloc((family->maxattr+1) *
393 sizeof(struct nlattr *), GFP_KERNEL);
394 if (family->attrbuf == NULL) {
395 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800396 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100397 }
398 } else
399 family->attrbuf = NULL;
400
401 list_add_tail(&family->family_list, genl_family_chain(family->id));
402 genl_unlock();
403
404 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
405
406 return 0;
407
408errout_locked:
409 genl_unlock();
410errout:
411 return err;
412}
413
414/**
Michał Mirosława7b11d72009-05-21 10:34:04 +0000415 * genl_register_family_with_ops - register a generic netlink family
416 * @family: generic netlink family
417 * @ops: operations to be registered
418 * @n_ops: number of elements to register
419 *
420 * Registers the specified family and operations from the specified table.
421 * Only one family may be registered with the same family name or identifier.
422 *
423 * The family id may equal GENL_ID_GENERATE causing an unique id to
424 * be automatically generated and assigned.
425 *
426 * Either a doit or dumpit callback must be specified for every registered
427 * operation or the function will fail. Only one operation structure per
428 * command identifier may be registered.
429 *
430 * See include/net/genetlink.h for more documenation on the operations
431 * structure.
432 *
433 * This is equivalent to calling genl_register_family() followed by
434 * genl_register_ops() for every operation entry in the table taking
435 * care to unregister the family on error path.
436 *
437 * Return 0 on success or a negative error code.
438 */
439int genl_register_family_with_ops(struct genl_family *family,
440 struct genl_ops *ops, size_t n_ops)
441{
442 int err, i;
443
444 err = genl_register_family(family);
445 if (err)
446 return err;
447
448 for (i = 0; i < n_ops; ++i, ++ops) {
449 err = genl_register_ops(family, ops);
450 if (err)
451 goto err_out;
452 }
453 return 0;
454err_out:
455 genl_unregister_family(family);
456 return err;
457}
458EXPORT_SYMBOL(genl_register_family_with_ops);
459
460/**
Thomas Graf482a8522005-11-10 02:25:56 +0100461 * genl_unregister_family - unregister generic netlink family
462 * @family: generic netlink family
463 *
464 * Unregisters the specified family.
465 *
466 * Returns 0 on success or a negative error code.
467 */
468int genl_unregister_family(struct genl_family *family)
469{
470 struct genl_family *rc;
471
472 genl_lock();
473
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800474 genl_unregister_mc_groups(family);
475
Thomas Graf482a8522005-11-10 02:25:56 +0100476 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
477 if (family->id != rc->id || strcmp(rc->name, family->name))
478 continue;
479
480 list_del(&rc->family_list);
481 INIT_LIST_HEAD(&family->ops_list);
482 genl_unlock();
483
Thomas Graf482a8522005-11-10 02:25:56 +0100484 kfree(family->attrbuf);
485 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
486 return 0;
487 }
488
489 genl_unlock();
490
491 return -ENOENT;
492}
493
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700494static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100495{
496 struct genl_ops *ops;
497 struct genl_family *family;
Johannes Berg134e6372009-07-10 09:51:34 +0000498 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100499 struct genl_info info;
500 struct genlmsghdr *hdr = nlmsg_data(nlh);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700501 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100502
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900503 family = genl_family_find_byid(nlh->nlmsg_type);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700504 if (family == NULL)
505 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100506
Johannes Berg134e6372009-07-10 09:51:34 +0000507 /* this family doesn't exist in this netns */
508 if (!family->netnsok && !net_eq(net, &init_net))
509 return -ENOENT;
510
Thomas Graf482a8522005-11-10 02:25:56 +0100511 hdrlen = GENL_HDRLEN + family->hdrsize;
512 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700513 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100514
515 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700516 if (ops == NULL)
517 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100518
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700519 if ((ops->flags & GENL_ADMIN_PERM) &&
520 security_netlink_recv(skb, CAP_NET_ADMIN))
521 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100522
523 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700524 if (ops->dumpit == NULL)
525 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100526
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700527 genl_unlock();
Johannes Berg134e6372009-07-10 09:51:34 +0000528 err = netlink_dump_start(net->genl_sock, skb, nlh,
Patrick McHardy6d1a3fb2008-06-18 02:07:07 -0700529 ops->dumpit, ops->done);
530 genl_lock();
531 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100532 }
533
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700534 if (ops->doit == NULL)
535 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100536
537 if (family->attrbuf) {
538 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
539 ops->policy);
540 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700541 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100542 }
543
544 info.snd_seq = nlh->nlmsg_seq;
545 info.snd_pid = NETLINK_CB(skb).pid;
546 info.nlhdr = nlh;
547 info.genlhdr = nlmsg_data(nlh);
548 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
549 info.attrs = family->attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000550 genl_info_net_set(&info, net);
Thomas Graf482a8522005-11-10 02:25:56 +0100551
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700552 return ops->doit(skb, &info);
Thomas Graf482a8522005-11-10 02:25:56 +0100553}
554
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700555static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100556{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700557 genl_lock();
558 netlink_rcv_skb(skb, &genl_rcv_msg);
559 genl_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100560}
561
562/**************************************************************************
563 * Controller
564 **************************************************************************/
565
Thomas Graf17c157c2006-11-14 19:46:02 -0800566static struct genl_family genl_ctrl = {
567 .id = GENL_ID_CTRL,
568 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800569 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800570 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000571 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800572};
573
Thomas Graf482a8522005-11-10 02:25:56 +0100574static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
575 u32 flags, struct sk_buff *skb, u8 cmd)
576{
577 void *hdr;
578
Thomas Graf17c157c2006-11-14 19:46:02 -0800579 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100580 if (hdr == NULL)
581 return -1;
582
583 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
584 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
Thomas Grafeb328112006-09-18 00:01:59 -0700585 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
586 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
587 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
588
Thomas Grafe94ef682006-11-23 11:44:37 -0800589 if (!list_empty(&family->ops_list)) {
590 struct nlattr *nla_ops;
591 struct genl_ops *ops;
592 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700593
Thomas Grafe94ef682006-11-23 11:44:37 -0800594 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
595 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700596 goto nla_put_failure;
597
Thomas Grafe94ef682006-11-23 11:44:37 -0800598 list_for_each_entry(ops, &family->ops_list, ops_list) {
599 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700600
Thomas Grafe94ef682006-11-23 11:44:37 -0800601 nest = nla_nest_start(skb, idx++);
602 if (nest == NULL)
603 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700604
Thomas Grafe94ef682006-11-23 11:44:37 -0800605 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
606 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
Thomas Grafeb328112006-09-18 00:01:59 -0700607
Thomas Grafe94ef682006-11-23 11:44:37 -0800608 nla_nest_end(skb, nest);
609 }
610
611 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700612 }
613
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700614 if (!list_empty(&family->mcast_groups)) {
615 struct genl_multicast_group *grp;
616 struct nlattr *nla_grps;
617 int idx = 1;
618
619 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
620 if (nla_grps == NULL)
621 goto nla_put_failure;
622
623 list_for_each_entry(grp, &family->mcast_groups, list) {
624 struct nlattr *nest;
625
626 nest = nla_nest_start(skb, idx++);
627 if (nest == NULL)
628 goto nla_put_failure;
629
630 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
631 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
632 grp->name);
633
634 nla_nest_end(skb, nest);
635 }
636 nla_nest_end(skb, nla_grps);
637 }
638
639 return genlmsg_end(skb, hdr);
640
641nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700642 genlmsg_cancel(skb, hdr);
643 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700644}
645
646static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
647 u32 seq, u32 flags, struct sk_buff *skb,
648 u8 cmd)
649{
650 void *hdr;
651 struct nlattr *nla_grps;
652 struct nlattr *nest;
653
654 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
655 if (hdr == NULL)
656 return -1;
657
658 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
659 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
660
661 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
662 if (nla_grps == NULL)
663 goto nla_put_failure;
664
665 nest = nla_nest_start(skb, 1);
666 if (nest == NULL)
667 goto nla_put_failure;
668
669 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
670 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
671 grp->name);
672
673 nla_nest_end(skb, nest);
674 nla_nest_end(skb, nla_grps);
675
Thomas Graf482a8522005-11-10 02:25:56 +0100676 return genlmsg_end(skb, hdr);
677
678nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700679 genlmsg_cancel(skb, hdr);
680 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100681}
682
683static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
684{
685
686 int i, n = 0;
687 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000688 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100689 int chains_to_skip = cb->args[0];
690 int fams_to_skip = cb->args[1];
691
692 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
693 if (i < chains_to_skip)
694 continue;
695 n = 0;
696 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000697 if (!rt->netnsok && !net_eq(net, &init_net))
698 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100699 if (++n < fams_to_skip)
700 continue;
701 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
702 cb->nlh->nlmsg_seq, NLM_F_MULTI,
703 skb, CTRL_CMD_NEWFAMILY) < 0)
704 goto errout;
705 }
706
707 fams_to_skip = 0;
708 }
709
710errout:
711 cb->args[0] = i;
712 cb->args[1] = n;
713
714 return skb->len;
715}
716
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700717static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
718 u32 pid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100719{
720 struct sk_buff *skb;
721 int err;
722
Thomas Graf339bf982006-11-10 14:10:15 -0800723 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100724 if (skb == NULL)
725 return ERR_PTR(-ENOBUFS);
726
727 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
728 if (err < 0) {
729 nlmsg_free(skb);
730 return ERR_PTR(err);
731 }
732
733 return skb;
734}
735
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700736static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
737 u32 pid, int seq, u8 cmd)
738{
739 struct sk_buff *skb;
740 int err;
741
742 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
743 if (skb == NULL)
744 return ERR_PTR(-ENOBUFS);
745
746 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
747 if (err < 0) {
748 nlmsg_free(skb);
749 return ERR_PTR(err);
750 }
751
752 return skb;
753}
754
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700755static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100756 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700757 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
758 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100759};
760
761static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
762{
763 struct sk_buff *msg;
764 struct genl_family *res = NULL;
765 int err = -EINVAL;
766
767 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
768 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
769 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000770 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100771 }
772
773 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700774 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100775
Thomas Graf5176f912006-08-26 20:13:18 -0700776 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100777 res = genl_family_find_byname(name);
Johannes Berg134e6372009-07-10 09:51:34 +0000778 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100779 }
780
Johannes Berg134e6372009-07-10 09:51:34 +0000781 if (res == NULL)
782 return err;
783
784 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
785 /* family doesn't exist here */
786 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100787 }
788
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700789 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
790 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000791 if (IS_ERR(msg))
792 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100793
Johannes Berg134e6372009-07-10 09:51:34 +0000794 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100795}
796
797static int genl_ctrl_event(int event, void *data)
798{
799 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000800 struct genl_family *family;
801 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100802
Johannes Berg134e6372009-07-10 09:51:34 +0000803 /* genl is still initialising */
804 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100805 return 0;
806
807 switch (event) {
808 case CTRL_CMD_NEWFAMILY:
809 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000810 family = data;
811 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700812 break;
813 case CTRL_CMD_NEWMCAST_GRP:
814 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000815 grp = data;
816 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700817 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100818 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000819 default:
820 return -EINVAL;
821 }
822
823 if (IS_ERR(msg))
824 return PTR_ERR(msg);
825
826 if (!family->netnsok) {
827 genlmsg_multicast_netns(&init_net, msg, 0,
828 GENL_ID_CTRL, GFP_KERNEL);
829 } else {
830 rcu_read_lock();
831 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
832 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100833 }
834
835 return 0;
836}
837
838static struct genl_ops genl_ctrl_ops = {
839 .cmd = CTRL_CMD_GETFAMILY,
840 .doit = ctrl_getfamily,
841 .dumpit = ctrl_dumpfamily,
842 .policy = ctrl_policy,
843};
844
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700845static struct genl_multicast_group notify_grp = {
846 .name = "notify",
847};
848
Johannes Berg134e6372009-07-10 09:51:34 +0000849static int __net_init genl_pernet_init(struct net *net)
850{
851 /* we'll bump the group number right afterwards */
852 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
853 genl_rcv, &genl_mutex,
854 THIS_MODULE);
855
856 if (!net->genl_sock && net_eq(net, &init_net))
857 panic("GENL: Cannot initialize generic netlink\n");
858
859 if (!net->genl_sock)
860 return -ENOMEM;
861
862 return 0;
863}
864
865static void __net_exit genl_pernet_exit(struct net *net)
866{
867 netlink_kernel_release(net->genl_sock);
868 net->genl_sock = NULL;
869}
870
871static struct pernet_operations genl_pernet_ops = {
872 .init = genl_pernet_init,
873 .exit = genl_pernet_exit,
874};
875
Thomas Graf482a8522005-11-10 02:25:56 +0100876static int __init genl_init(void)
877{
878 int i, err;
879
880 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
881 INIT_LIST_HEAD(&family_ht[i]);
882
883 err = genl_register_family(&genl_ctrl);
884 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000885 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100886
887 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
888 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000889 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100890
891 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700892
Johannes Berg134e6372009-07-10 09:51:34 +0000893 err = register_pernet_subsys(&genl_pernet_ops);
894 if (err)
895 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100896
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700897 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
898 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000899 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700900
Thomas Graf482a8522005-11-10 02:25:56 +0100901 return 0;
902
Johannes Berg134e6372009-07-10 09:51:34 +0000903problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100904 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100905}
906
907subsys_initcall(genl_init);
908
Thomas Graf482a8522005-11-10 02:25:56 +0100909EXPORT_SYMBOL(genl_register_ops);
910EXPORT_SYMBOL(genl_unregister_ops);
911EXPORT_SYMBOL(genl_register_family);
912EXPORT_SYMBOL(genl_unregister_family);
Johannes Berg134e6372009-07-10 09:51:34 +0000913
914static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
915 gfp_t flags)
916{
917 struct sk_buff *tmp;
918 struct net *net, *prev = NULL;
919 int err;
920
921 for_each_net_rcu(net) {
922 if (prev) {
923 tmp = skb_clone(skb, flags);
924 if (!tmp) {
925 err = -ENOMEM;
926 goto error;
927 }
928 err = nlmsg_multicast(prev->genl_sock, tmp,
929 pid, group, flags);
930 if (err)
931 goto error;
932 }
933
934 prev = net;
935 }
936
937 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
938 error:
939 kfree_skb(skb);
940 return err;
941}
942
943int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
944 gfp_t flags)
945{
946 return genlmsg_mcast(skb, pid, group, flags);
947}
948EXPORT_SYMBOL(genlmsg_multicast_allns);