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