Partial revert "Basic kernel memory functionality for the Memory Controller"
[deliverable/linux.git] / net / netlink / genetlink.c
CommitLineData
482a8524
TG
1/*
2 * NETLINK Generic Netlink Family
3 *
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
2dbba6f7 6 * Johannes Berg <johannes@sipsolutions.net>
482a8524
TG
7 */
8
482a8524
TG
9#include <linux/module.h>
10#include <linux/kernel.h>
5a0e3ad6 11#include <linux/slab.h>
482a8524
TG
12#include <linux/errno.h>
13#include <linux/types.h>
14#include <linux/socket.h>
15#include <linux/string.h>
16#include <linux/skbuff.h>
14cc3e2b 17#include <linux/mutex.h>
2dbba6f7 18#include <linux/bitmap.h>
482a8524
TG
19#include <net/sock.h>
20#include <net/genetlink.h>
21
14cc3e2b 22static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
482a8524 23
f408e0ce 24void genl_lock(void)
482a8524 25{
14cc3e2b 26 mutex_lock(&genl_mutex);
482a8524 27}
f408e0ce 28EXPORT_SYMBOL(genl_lock);
482a8524 29
f408e0ce 30void genl_unlock(void)
482a8524 31{
14cc3e2b 32 mutex_unlock(&genl_mutex);
482a8524 33}
f408e0ce 34EXPORT_SYMBOL(genl_unlock);
482a8524 35
86b1309c
PS
36#ifdef CONFIG_PROVE_LOCKING
37int lockdep_genl_is_held(void)
38{
39 return lockdep_is_held(&genl_mutex);
40}
41EXPORT_SYMBOL(lockdep_genl_is_held);
42#endif
43
482a8524
TG
44#define GENL_FAM_TAB_SIZE 16
45#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
46
47static struct list_head family_ht[GENL_FAM_TAB_SIZE];
2dbba6f7
JB
48/*
49 * Bitmap of multicast groups that are currently in use.
50 *
51 * To avoid an allocation at boot of just one unsigned long,
52 * declare it global instead.
53 * Bit 0 is marked as already used since group 0 is invalid.
54 */
55static unsigned long mc_group_start = 0x1;
56static unsigned long *mc_groups = &mc_group_start;
57static unsigned long mc_groups_longs = 1;
482a8524
TG
58
59static int genl_ctrl_event(int event, void *data);
60
61static inline unsigned int genl_family_hash(unsigned int id)
62{
63 return id & GENL_FAM_TAB_MASK;
64}
65
66static inline struct list_head *genl_family_chain(unsigned int id)
67{
68 return &family_ht[genl_family_hash(id)];
69}
70
71static struct genl_family *genl_family_find_byid(unsigned int id)
72{
73 struct genl_family *f;
74
75 list_for_each_entry(f, genl_family_chain(id), family_list)
76 if (f->id == id)
77 return f;
78
79 return NULL;
80}
81
82static struct genl_family *genl_family_find_byname(char *name)
83{
84 struct genl_family *f;
85 int i;
86
87 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
88 list_for_each_entry(f, genl_family_chain(i), family_list)
89 if (strcmp(f->name, name) == 0)
90 return f;
91
92 return NULL;
93}
94
95static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
96{
97 struct genl_ops *ops;
98
99 list_for_each_entry(ops, &family->ops_list, ops_list)
100 if (ops->cmd == cmd)
101 return ops;
102
103 return NULL;
104}
105
106/* Of course we are going to have problems once we hit
107 * 2^16 alive types, but that can only happen by year 2K
108*/
109static inline u16 genl_generate_id(void)
110{
988ade6b
KK
111 static u16 id_gen_idx = GENL_MIN_ID;
112 int i;
482a8524 113
988ade6b
KK
114 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
115 if (!genl_family_find_byid(id_gen_idx))
116 return id_gen_idx;
117 if (++id_gen_idx > GENL_MAX_ID)
482a8524 118 id_gen_idx = GENL_MIN_ID;
988ade6b 119 }
482a8524 120
988ade6b 121 return 0;
482a8524
TG
122}
123
2dbba6f7
JB
124static struct genl_multicast_group notify_grp;
125
126/**
127 * genl_register_mc_group - register a multicast group
128 *
129 * Registers the specified multicast group and notifies userspace
130 * about the new group.
131 *
132 * Returns 0 on success or a negative error code.
133 *
134 * @family: The generic netlink family the group shall be registered for.
135 * @grp: The group to register, must have a name.
136 */
137int genl_register_mc_group(struct genl_family *family,
138 struct genl_multicast_group *grp)
139{
140 int id;
141 unsigned long *new_groups;
b1f57195 142 int err = 0;
2dbba6f7
JB
143
144 BUG_ON(grp->name[0] == '\0');
145
146 genl_lock();
147
148 /* special-case our own group */
149 if (grp == &notify_grp)
150 id = GENL_ID_CTRL;
151 else
152 id = find_first_zero_bit(mc_groups,
153 mc_groups_longs * BITS_PER_LONG);
154
155
156 if (id >= mc_groups_longs * BITS_PER_LONG) {
157 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
158
159 if (mc_groups == &mc_group_start) {
160 new_groups = kzalloc(nlen, GFP_KERNEL);
161 if (!new_groups) {
162 err = -ENOMEM;
163 goto out;
164 }
165 mc_groups = new_groups;
166 *mc_groups = mc_group_start;
167 } else {
168 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
169 if (!new_groups) {
170 err = -ENOMEM;
171 goto out;
172 }
173 mc_groups = new_groups;
174 mc_groups[mc_groups_longs] = 0;
175 }
176 mc_groups_longs++;
177 }
178
134e6375
JB
179 if (family->netnsok) {
180 struct net *net;
181
d136f1bd 182 netlink_table_grab();
134e6375
JB
183 rcu_read_lock();
184 for_each_net_rcu(net) {
d136f1bd 185 err = __netlink_change_ngroups(net->genl_sock,
134e6375
JB
186 mc_groups_longs * BITS_PER_LONG);
187 if (err) {
188 /*
189 * No need to roll back, can only fail if
190 * memory allocation fails and then the
191 * number of _possible_ groups has been
192 * increased on some sockets which is ok.
193 */
194 rcu_read_unlock();
d136f1bd 195 netlink_table_ungrab();
134e6375
JB
196 goto out;
197 }
198 }
199 rcu_read_unlock();
d136f1bd 200 netlink_table_ungrab();
134e6375
JB
201 } else {
202 err = netlink_change_ngroups(init_net.genl_sock,
203 mc_groups_longs * BITS_PER_LONG);
204 if (err)
205 goto out;
206 }
2dbba6f7
JB
207
208 grp->id = id;
209 set_bit(id, mc_groups);
210 list_add_tail(&grp->list, &family->mcast_groups);
211 grp->family = family;
212
213 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
214 out:
215 genl_unlock();
79d310d0 216 return err;
2dbba6f7
JB
217}
218EXPORT_SYMBOL(genl_register_mc_group);
219
79dc4386
TG
220static void __genl_unregister_mc_group(struct genl_family *family,
221 struct genl_multicast_group *grp)
222{
134e6375 223 struct net *net;
79dc4386 224 BUG_ON(grp->family != family);
134e6375 225
b8273570 226 netlink_table_grab();
134e6375
JB
227 rcu_read_lock();
228 for_each_net_rcu(net)
b8273570 229 __netlink_clear_multicast_users(net->genl_sock, grp->id);
134e6375 230 rcu_read_unlock();
b8273570 231 netlink_table_ungrab();
134e6375 232
79dc4386
TG
233 clear_bit(grp->id, mc_groups);
234 list_del(&grp->list);
235 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
236 grp->id = 0;
237 grp->family = NULL;
238}
239
2dbba6f7
JB
240/**
241 * genl_unregister_mc_group - unregister a multicast group
242 *
243 * Unregisters the specified multicast group and notifies userspace
244 * about it. All current listeners on the group are removed.
245 *
246 * Note: It is not necessary to unregister all multicast groups before
247 * unregistering the family, unregistering the family will cause
248 * all assigned multicast groups to be unregistered automatically.
249 *
250 * @family: Generic netlink family the group belongs to.
251 * @grp: The group to unregister, must have been registered successfully
252 * previously.
253 */
254void genl_unregister_mc_group(struct genl_family *family,
255 struct genl_multicast_group *grp)
256{
2dbba6f7 257 genl_lock();
79dc4386 258 __genl_unregister_mc_group(family, grp);
2dbba6f7
JB
259 genl_unlock();
260}
3efb40c2 261EXPORT_SYMBOL(genl_unregister_mc_group);
2dbba6f7
JB
262
263static void genl_unregister_mc_groups(struct genl_family *family)
264{
265 struct genl_multicast_group *grp, *tmp;
266
267 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
79dc4386 268 __genl_unregister_mc_group(family, grp);
2dbba6f7
JB
269}
270
482a8524
TG
271/**
272 * genl_register_ops - register generic netlink operations
273 * @family: generic netlink family
274 * @ops: operations to be registered
275 *
276 * Registers the specified operations and assigns them to the specified
277 * family. Either a doit or dumpit callback must be specified or the
278 * operation will fail. Only one operation structure per command
279 * identifier may be registered.
280 *
281 * See include/net/genetlink.h for more documenation on the operations
282 * structure.
283 *
284 * Returns 0 on success or a negative error code.
285 */
286int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
287{
288 int err = -EINVAL;
289
290 if (ops->dumpit == NULL && ops->doit == NULL)
291 goto errout;
292
293 if (genl_get_cmd(ops->cmd, family)) {
294 err = -EEXIST;
295 goto errout;
296 }
297
334c29a6 298 if (ops->dumpit)
334c29a6 299 ops->flags |= GENL_CMD_CAP_DUMP;
48d4ed7a
JHS
300 if (ops->doit)
301 ops->flags |= GENL_CMD_CAP_DO;
334c29a6
JHS
302 if (ops->policy)
303 ops->flags |= GENL_CMD_CAP_HASPOL;
304
482a8524
TG
305 genl_lock();
306 list_add_tail(&ops->ops_list, &family->ops_list);
307 genl_unlock();
308
309 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
310 err = 0;
311errout:
312 return err;
313}
416c2f9c 314EXPORT_SYMBOL(genl_register_ops);
482a8524
TG
315
316/**
317 * genl_unregister_ops - unregister generic netlink operations
318 * @family: generic netlink family
319 * @ops: operations to be unregistered
320 *
321 * Unregisters the specified operations and unassigns them from the
322 * specified family. The operation blocks until the current message
323 * processing has finished and doesn't start again until the
324 * unregister process has finished.
325 *
326 * Note: It is not necessary to unregister all operations before
327 * unregistering the family, unregistering the family will cause
328 * all assigned operations to be unregistered automatically.
329 *
330 * Returns 0 on success or a negative error code.
331 */
332int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
333{
334 struct genl_ops *rc;
335
336 genl_lock();
337 list_for_each_entry(rc, &family->ops_list, ops_list) {
338 if (rc == ops) {
339 list_del(&ops->ops_list);
340 genl_unlock();
341 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
342 return 0;
343 }
344 }
345 genl_unlock();
346
347 return -ENOENT;
348}
416c2f9c 349EXPORT_SYMBOL(genl_unregister_ops);
482a8524
TG
350
351/**
352 * genl_register_family - register a generic netlink family
353 * @family: generic netlink family
354 *
355 * Registers the specified family after validating it first. Only one
356 * family may be registered with the same family name or identifier.
357 * The family id may equal GENL_ID_GENERATE causing an unique id to
358 * be automatically generated and assigned.
359 *
360 * Return 0 on success or a negative error code.
361 */
362int genl_register_family(struct genl_family *family)
363{
364 int err = -EINVAL;
365
366 if (family->id && family->id < GENL_MIN_ID)
367 goto errout;
368
369 if (family->id > GENL_MAX_ID)
370 goto errout;
371
372 INIT_LIST_HEAD(&family->ops_list);
2dbba6f7 373 INIT_LIST_HEAD(&family->mcast_groups);
482a8524
TG
374
375 genl_lock();
376
377 if (genl_family_find_byname(family->name)) {
378 err = -EEXIST;
379 goto errout_locked;
380 }
381
482a8524
TG
382 if (family->id == GENL_ID_GENERATE) {
383 u16 newid = genl_generate_id();
384
385 if (!newid) {
386 err = -ENOMEM;
387 goto errout_locked;
388 }
389
390 family->id = newid;
93860b08
KK
391 } else if (genl_family_find_byid(family->id)) {
392 err = -EEXIST;
393 goto errout_locked;
482a8524
TG
394 }
395
396 if (family->maxattr) {
397 family->attrbuf = kmalloc((family->maxattr+1) *
398 sizeof(struct nlattr *), GFP_KERNEL);
399 if (family->attrbuf == NULL) {
400 err = -ENOMEM;
e200bd80 401 goto errout_locked;
482a8524
TG
402 }
403 } else
404 family->attrbuf = NULL;
405
406 list_add_tail(&family->family_list, genl_family_chain(family->id));
407 genl_unlock();
408
409 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
410
411 return 0;
412
413errout_locked:
414 genl_unlock();
415errout:
416 return err;
417}
416c2f9c 418EXPORT_SYMBOL(genl_register_family);
482a8524 419
a7b11d73
MM
420/**
421 * genl_register_family_with_ops - register a generic netlink family
422 * @family: generic netlink family
423 * @ops: operations to be registered
424 * @n_ops: number of elements to register
425 *
426 * Registers the specified family and operations from the specified table.
427 * Only one family may be registered with the same family name or identifier.
428 *
429 * The family id may equal GENL_ID_GENERATE causing an unique id to
430 * be automatically generated and assigned.
431 *
432 * Either a doit or dumpit callback must be specified for every registered
433 * operation or the function will fail. Only one operation structure per
434 * command identifier may be registered.
435 *
436 * See include/net/genetlink.h for more documenation on the operations
437 * structure.
438 *
439 * This is equivalent to calling genl_register_family() followed by
440 * genl_register_ops() for every operation entry in the table taking
441 * care to unregister the family on error path.
442 *
443 * Return 0 on success or a negative error code.
444 */
445int genl_register_family_with_ops(struct genl_family *family,
446 struct genl_ops *ops, size_t n_ops)
447{
448 int err, i;
449
450 err = genl_register_family(family);
451 if (err)
452 return err;
453
454 for (i = 0; i < n_ops; ++i, ++ops) {
455 err = genl_register_ops(family, ops);
456 if (err)
457 goto err_out;
458 }
459 return 0;
460err_out:
461 genl_unregister_family(family);
462 return err;
463}
464EXPORT_SYMBOL(genl_register_family_with_ops);
465
482a8524
TG
466/**
467 * genl_unregister_family - unregister generic netlink family
468 * @family: generic netlink family
469 *
470 * Unregisters the specified family.
471 *
472 * Returns 0 on success or a negative error code.
473 */
474int genl_unregister_family(struct genl_family *family)
475{
476 struct genl_family *rc;
477
478 genl_lock();
479
910d6c32
PE
480 genl_unregister_mc_groups(family);
481
482a8524
TG
482 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
483 if (family->id != rc->id || strcmp(rc->name, family->name))
484 continue;
485
486 list_del(&rc->family_list);
487 INIT_LIST_HEAD(&family->ops_list);
488 genl_unlock();
489
482a8524
TG
490 kfree(family->attrbuf);
491 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
492 return 0;
493 }
494
495 genl_unlock();
496
497 return -ENOENT;
498}
416c2f9c 499EXPORT_SYMBOL(genl_unregister_family);
482a8524 500
1d00a4eb 501static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
482a8524
TG
502{
503 struct genl_ops *ops;
504 struct genl_family *family;
134e6375 505 struct net *net = sock_net(skb->sk);
482a8524
TG
506 struct genl_info info;
507 struct genlmsghdr *hdr = nlmsg_data(nlh);
1d00a4eb 508 int hdrlen, err;
482a8524 509
746fac4d 510 family = genl_family_find_byid(nlh->nlmsg_type);
1d00a4eb
TG
511 if (family == NULL)
512 return -ENOENT;
482a8524 513
134e6375
JB
514 /* this family doesn't exist in this netns */
515 if (!family->netnsok && !net_eq(net, &init_net))
516 return -ENOENT;
517
482a8524
TG
518 hdrlen = GENL_HDRLEN + family->hdrsize;
519 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
1d00a4eb 520 return -EINVAL;
482a8524
TG
521
522 ops = genl_get_cmd(hdr->cmd, family);
1d00a4eb
TG
523 if (ops == NULL)
524 return -EOPNOTSUPP;
482a8524 525
1d00a4eb
TG
526 if ((ops->flags & GENL_ADMIN_PERM) &&
527 security_netlink_recv(skb, CAP_NET_ADMIN))
528 return -EPERM;
482a8524 529
b8f3ab42 530 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1d00a4eb
TG
531 if (ops->dumpit == NULL)
532 return -EOPNOTSUPP;
482a8524 533
6d1a3fb5 534 genl_unlock();
134e6375 535 err = netlink_dump_start(net->genl_sock, skb, nlh,
c7ac8679 536 ops->dumpit, ops->done, 0);
6d1a3fb5
PM
537 genl_lock();
538 return err;
482a8524
TG
539 }
540
1d00a4eb
TG
541 if (ops->doit == NULL)
542 return -EOPNOTSUPP;
482a8524
TG
543
544 if (family->attrbuf) {
545 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
546 ops->policy);
547 if (err < 0)
1d00a4eb 548 return err;
482a8524
TG
549 }
550
551 info.snd_seq = nlh->nlmsg_seq;
552 info.snd_pid = NETLINK_CB(skb).pid;
553 info.nlhdr = nlh;
554 info.genlhdr = nlmsg_data(nlh);
555 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
556 info.attrs = family->attrbuf;
134e6375 557 genl_info_net_set(&info, net);
ff4c92d8 558 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
482a8524 559
ff4c92d8
JB
560 if (family->pre_doit) {
561 err = family->pre_doit(ops, skb, &info);
562 if (err)
563 return err;
564 }
565
566 err = ops->doit(skb, &info);
567
568 if (family->post_doit)
569 family->post_doit(ops, skb, &info);
570
571 return err;
482a8524
TG
572}
573
cd40b7d3 574static void genl_rcv(struct sk_buff *skb)
482a8524 575{
cd40b7d3
DL
576 genl_lock();
577 netlink_rcv_skb(skb, &genl_rcv_msg);
578 genl_unlock();
482a8524
TG
579}
580
581/**************************************************************************
582 * Controller
583 **************************************************************************/
584
17c157c8
TG
585static struct genl_family genl_ctrl = {
586 .id = GENL_ID_CTRL,
587 .name = "nlctrl",
334c29a6 588 .version = 0x2,
17c157c8 589 .maxattr = CTRL_ATTR_MAX,
134e6375 590 .netnsok = true,
17c157c8
TG
591};
592
482a8524
TG
593static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
594 u32 flags, struct sk_buff *skb, u8 cmd)
595{
596 void *hdr;
597
17c157c8 598 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
482a8524
TG
599 if (hdr == NULL)
600 return -1;
601
602 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
603 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
eb328111
TG
604 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
605 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
606 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
607
e94ef682
TG
608 if (!list_empty(&family->ops_list)) {
609 struct nlattr *nla_ops;
610 struct genl_ops *ops;
611 int idx = 1;
eb328111 612
e94ef682
TG
613 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
614 if (nla_ops == NULL)
eb328111
TG
615 goto nla_put_failure;
616
e94ef682
TG
617 list_for_each_entry(ops, &family->ops_list, ops_list) {
618 struct nlattr *nest;
eb328111 619
e94ef682
TG
620 nest = nla_nest_start(skb, idx++);
621 if (nest == NULL)
622 goto nla_put_failure;
eb328111 623
e94ef682
TG
624 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
625 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
eb328111 626
e94ef682
TG
627 nla_nest_end(skb, nest);
628 }
629
630 nla_nest_end(skb, nla_ops);
631 }
482a8524 632
2dbba6f7
JB
633 if (!list_empty(&family->mcast_groups)) {
634 struct genl_multicast_group *grp;
635 struct nlattr *nla_grps;
636 int idx = 1;
637
638 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
639 if (nla_grps == NULL)
640 goto nla_put_failure;
641
642 list_for_each_entry(grp, &family->mcast_groups, list) {
643 struct nlattr *nest;
644
645 nest = nla_nest_start(skb, idx++);
646 if (nest == NULL)
647 goto nla_put_failure;
648
649 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
650 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
651 grp->name);
652
653 nla_nest_end(skb, nest);
654 }
655 nla_nest_end(skb, nla_grps);
656 }
657
658 return genlmsg_end(skb, hdr);
659
660nla_put_failure:
bc3ed28c
TG
661 genlmsg_cancel(skb, hdr);
662 return -EMSGSIZE;
2dbba6f7
JB
663}
664
665static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
666 u32 seq, u32 flags, struct sk_buff *skb,
667 u8 cmd)
668{
669 void *hdr;
670 struct nlattr *nla_grps;
671 struct nlattr *nest;
672
673 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
674 if (hdr == NULL)
675 return -1;
676
677 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
678 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
679
680 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
681 if (nla_grps == NULL)
682 goto nla_put_failure;
683
684 nest = nla_nest_start(skb, 1);
685 if (nest == NULL)
686 goto nla_put_failure;
687
688 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
689 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
690 grp->name);
691
692 nla_nest_end(skb, nest);
693 nla_nest_end(skb, nla_grps);
694
482a8524
TG
695 return genlmsg_end(skb, hdr);
696
697nla_put_failure:
bc3ed28c
TG
698 genlmsg_cancel(skb, hdr);
699 return -EMSGSIZE;
482a8524
TG
700}
701
702static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
703{
704
705 int i, n = 0;
706 struct genl_family *rt;
134e6375 707 struct net *net = sock_net(skb->sk);
482a8524
TG
708 int chains_to_skip = cb->args[0];
709 int fams_to_skip = cb->args[1];
710
e1d5a010 711 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
482a8524
TG
712 n = 0;
713 list_for_each_entry(rt, genl_family_chain(i), family_list) {
134e6375
JB
714 if (!rt->netnsok && !net_eq(net, &init_net))
715 continue;
482a8524
TG
716 if (++n < fams_to_skip)
717 continue;
718 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
719 cb->nlh->nlmsg_seq, NLM_F_MULTI,
720 skb, CTRL_CMD_NEWFAMILY) < 0)
721 goto errout;
722 }
723
724 fams_to_skip = 0;
725 }
726
727errout:
728 cb->args[0] = i;
729 cb->args[1] = n;
730
731 return skb->len;
732}
733
2dbba6f7
JB
734static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
735 u32 pid, int seq, u8 cmd)
482a8524
TG
736{
737 struct sk_buff *skb;
738 int err;
739
339bf98f 740 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
482a8524
TG
741 if (skb == NULL)
742 return ERR_PTR(-ENOBUFS);
743
744 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
745 if (err < 0) {
746 nlmsg_free(skb);
747 return ERR_PTR(err);
748 }
749
750 return skb;
751}
752
2dbba6f7
JB
753static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
754 u32 pid, int seq, u8 cmd)
755{
756 struct sk_buff *skb;
757 int err;
758
759 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
760 if (skb == NULL)
761 return ERR_PTR(-ENOBUFS);
762
763 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
764 if (err < 0) {
765 nlmsg_free(skb);
766 return ERR_PTR(err);
767 }
768
769 return skb;
770}
771
ef7c79ed 772static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
482a8524 773 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
5176f91e
TG
774 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
775 .len = GENL_NAMSIZ - 1 },
482a8524
TG
776};
777
778static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
779{
780 struct sk_buff *msg;
781 struct genl_family *res = NULL;
782 int err = -EINVAL;
783
784 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
785 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
786 res = genl_family_find_byid(id);
134e6375 787 err = -ENOENT;
482a8524
TG
788 }
789
790 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
5176f91e 791 char *name;
482a8524 792
5176f91e 793 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
482a8524 794 res = genl_family_find_byname(name);
134e6375 795 err = -ENOENT;
482a8524
TG
796 }
797
134e6375
JB
798 if (res == NULL)
799 return err;
800
801 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
802 /* family doesn't exist here */
803 return -ENOENT;
482a8524
TG
804 }
805
2dbba6f7
JB
806 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
807 CTRL_CMD_NEWFAMILY);
134e6375
JB
808 if (IS_ERR(msg))
809 return PTR_ERR(msg);
482a8524 810
134e6375 811 return genlmsg_reply(msg, info);
482a8524
TG
812}
813
814static int genl_ctrl_event(int event, void *data)
815{
816 struct sk_buff *msg;
134e6375
JB
817 struct genl_family *family;
818 struct genl_multicast_group *grp;
482a8524 819
134e6375
JB
820 /* genl is still initialising */
821 if (!init_net.genl_sock)
482a8524
TG
822 return 0;
823
824 switch (event) {
825 case CTRL_CMD_NEWFAMILY:
826 case CTRL_CMD_DELFAMILY:
134e6375
JB
827 family = data;
828 msg = ctrl_build_family_msg(family, 0, 0, event);
2dbba6f7
JB
829 break;
830 case CTRL_CMD_NEWMCAST_GRP:
831 case CTRL_CMD_DELMCAST_GRP:
134e6375
JB
832 grp = data;
833 family = grp->family;
2dbba6f7 834 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
482a8524 835 break;
134e6375
JB
836 default:
837 return -EINVAL;
838 }
839
840 if (IS_ERR(msg))
841 return PTR_ERR(msg);
842
843 if (!family->netnsok) {
844 genlmsg_multicast_netns(&init_net, msg, 0,
845 GENL_ID_CTRL, GFP_KERNEL);
846 } else {
847 rcu_read_lock();
848 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
849 rcu_read_unlock();
482a8524
TG
850 }
851
852 return 0;
853}
854
855static struct genl_ops genl_ctrl_ops = {
856 .cmd = CTRL_CMD_GETFAMILY,
857 .doit = ctrl_getfamily,
858 .dumpit = ctrl_dumpfamily,
859 .policy = ctrl_policy,
860};
861
2dbba6f7
JB
862static struct genl_multicast_group notify_grp = {
863 .name = "notify",
864};
865
134e6375
JB
866static int __net_init genl_pernet_init(struct net *net)
867{
868 /* we'll bump the group number right afterwards */
869 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0,
870 genl_rcv, &genl_mutex,
871 THIS_MODULE);
872
873 if (!net->genl_sock && net_eq(net, &init_net))
874 panic("GENL: Cannot initialize generic netlink\n");
875
876 if (!net->genl_sock)
877 return -ENOMEM;
878
879 return 0;
880}
881
882static void __net_exit genl_pernet_exit(struct net *net)
883{
884 netlink_kernel_release(net->genl_sock);
885 net->genl_sock = NULL;
886}
887
888static struct pernet_operations genl_pernet_ops = {
889 .init = genl_pernet_init,
890 .exit = genl_pernet_exit,
891};
892
482a8524
TG
893static int __init genl_init(void)
894{
895 int i, err;
896
897 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
898 INIT_LIST_HEAD(&family_ht[i]);
899
652c6717 900 err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
482a8524 901 if (err < 0)
134e6375 902 goto problem;
482a8524
TG
903
904 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
2dbba6f7 905
134e6375
JB
906 err = register_pernet_subsys(&genl_pernet_ops);
907 if (err)
908 goto problem;
482a8524 909
2dbba6f7
JB
910 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
911 if (err < 0)
134e6375 912 goto problem;
2dbba6f7 913
482a8524
TG
914 return 0;
915
134e6375 916problem:
482a8524 917 panic("GENL: Cannot register controller: %d\n", err);
482a8524
TG
918}
919
920subsys_initcall(genl_init);
921
134e6375
JB
922static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group,
923 gfp_t flags)
924{
925 struct sk_buff *tmp;
926 struct net *net, *prev = NULL;
927 int err;
928
929 for_each_net_rcu(net) {
930 if (prev) {
931 tmp = skb_clone(skb, flags);
932 if (!tmp) {
933 err = -ENOMEM;
934 goto error;
935 }
936 err = nlmsg_multicast(prev->genl_sock, tmp,
937 pid, group, flags);
938 if (err)
939 goto error;
940 }
941
942 prev = net;
943 }
944
945 return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags);
946 error:
947 kfree_skb(skb);
948 return err;
949}
950
951int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group,
952 gfp_t flags)
953{
954 return genlmsg_mcast(skb, pid, group, flags);
955}
956EXPORT_SYMBOL(genlmsg_multicast_allns);
263ba61d
PS
957
958void genl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
959 struct nlmsghdr *nlh, gfp_t flags)
960{
961 struct sock *sk = net->genl_sock;
962 int report = 0;
963
964 if (nlh)
965 report = nlmsg_report(nlh);
966
967 nlmsg_notify(sk, skb, pid, group, report, flags);
968}
969EXPORT_SYMBOL(genl_notify);
This page took 0.846392 seconds and 5 git commands to generate.