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