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