blob: f5aa23c3e886d88b852ff0aeeb8ef55ae2bb3182 [file] [log] [blame]
Thomas Graf482a8522005-11-10 02:25:56 +01001/*
2 * NETLINK Generic Netlink Family
3 *
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
Johannes Berg2dbba6f2007-07-18 15:47:52 -07006 * Johannes Berg <johannes@sipsolutions.net>
Thomas Graf482a8522005-11-10 02:25:56 +01007 */
8
Thomas Graf482a8522005-11-10 02:25:56 +01009#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/types.h>
13#include <linux/socket.h>
14#include <linux/string.h>
15#include <linux/skbuff.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080016#include <linux/mutex.h>
Johannes Berg2dbba6f2007-07-18 15:47:52 -070017#include <linux/bitmap.h>
Thomas Graf482a8522005-11-10 02:25:56 +010018#include <net/sock.h>
19#include <net/genetlink.h>
20
21struct sock *genl_sock = NULL;
22
Ingo Molnar14cc3e22006-03-26 01:37:14 -080023static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
Thomas Graf482a8522005-11-10 02:25:56 +010024
Denis V. Lunev3b715352007-10-10 21:13:32 -070025static inline void genl_lock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010026{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080027 mutex_lock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010028}
29
Denis V. Lunev3b715352007-10-10 21:13:32 -070030static inline void genl_unlock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010031{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080032 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010033}
34
35#define GENL_FAM_TAB_SIZE 16
36#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
37
38static struct list_head family_ht[GENL_FAM_TAB_SIZE];
Johannes Berg2dbba6f2007-07-18 15:47:52 -070039/*
40 * Bitmap of multicast groups that are currently in use.
41 *
42 * To avoid an allocation at boot of just one unsigned long,
43 * declare it global instead.
44 * Bit 0 is marked as already used since group 0 is invalid.
45 */
46static unsigned long mc_group_start = 0x1;
47static unsigned long *mc_groups = &mc_group_start;
48static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010049
50static int genl_ctrl_event(int event, void *data);
51
52static inline unsigned int genl_family_hash(unsigned int id)
53{
54 return id & GENL_FAM_TAB_MASK;
55}
56
57static inline struct list_head *genl_family_chain(unsigned int id)
58{
59 return &family_ht[genl_family_hash(id)];
60}
61
62static struct genl_family *genl_family_find_byid(unsigned int id)
63{
64 struct genl_family *f;
65
66 list_for_each_entry(f, genl_family_chain(id), family_list)
67 if (f->id == id)
68 return f;
69
70 return NULL;
71}
72
73static struct genl_family *genl_family_find_byname(char *name)
74{
75 struct genl_family *f;
76 int i;
77
78 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
79 list_for_each_entry(f, genl_family_chain(i), family_list)
80 if (strcmp(f->name, name) == 0)
81 return f;
82
83 return NULL;
84}
85
86static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
87{
88 struct genl_ops *ops;
89
90 list_for_each_entry(ops, &family->ops_list, ops_list)
91 if (ops->cmd == cmd)
92 return ops;
93
94 return NULL;
95}
96
97/* Of course we are going to have problems once we hit
98 * 2^16 alive types, but that can only happen by year 2K
99*/
100static inline u16 genl_generate_id(void)
101{
102 static u16 id_gen_idx;
103 int overflowed = 0;
104
105 do {
106 if (id_gen_idx == 0)
107 id_gen_idx = GENL_MIN_ID;
108
109 if (++id_gen_idx > GENL_MAX_ID) {
110 if (!overflowed) {
111 overflowed = 1;
112 id_gen_idx = 0;
113 continue;
114 } else
115 return 0;
116 }
117
118 } while (genl_family_find_byid(id_gen_idx));
119
120 return id_gen_idx;
121}
122
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700123static struct genl_multicast_group notify_grp;
124
125/**
126 * genl_register_mc_group - register a multicast group
127 *
128 * Registers the specified multicast group and notifies userspace
129 * about the new group.
130 *
131 * Returns 0 on success or a negative error code.
132 *
133 * @family: The generic netlink family the group shall be registered for.
134 * @grp: The group to register, must have a name.
135 */
136int genl_register_mc_group(struct genl_family *family,
137 struct genl_multicast_group *grp)
138{
139 int id;
140 unsigned long *new_groups;
141 int err;
142
143 BUG_ON(grp->name[0] == '\0');
144
145 genl_lock();
146
147 /* special-case our own group */
148 if (grp == &notify_grp)
149 id = GENL_ID_CTRL;
150 else
151 id = find_first_zero_bit(mc_groups,
152 mc_groups_longs * BITS_PER_LONG);
153
154
155 if (id >= mc_groups_longs * BITS_PER_LONG) {
156 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
157
158 if (mc_groups == &mc_group_start) {
159 new_groups = kzalloc(nlen, GFP_KERNEL);
160 if (!new_groups) {
161 err = -ENOMEM;
162 goto out;
163 }
164 mc_groups = new_groups;
165 *mc_groups = mc_group_start;
166 } else {
167 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
168 if (!new_groups) {
169 err = -ENOMEM;
170 goto out;
171 }
172 mc_groups = new_groups;
173 mc_groups[mc_groups_longs] = 0;
174 }
175 mc_groups_longs++;
176 }
177
178 err = netlink_change_ngroups(genl_sock,
Thomas Graf2c04ddb2007-07-24 15:33:51 -0700179 mc_groups_longs * BITS_PER_LONG);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700180 if (err)
181 goto out;
182
183 grp->id = id;
184 set_bit(id, mc_groups);
185 list_add_tail(&grp->list, &family->mcast_groups);
186 grp->family = family;
187
188 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
189 out:
190 genl_unlock();
Thomas Graf79d310d2007-07-24 15:34:53 -0700191 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700192}
193EXPORT_SYMBOL(genl_register_mc_group);
194
Thomas Graf79dc43862007-07-24 15:32:46 -0700195static void __genl_unregister_mc_group(struct genl_family *family,
196 struct genl_multicast_group *grp)
197{
198 BUG_ON(grp->family != family);
199 netlink_clear_multicast_users(genl_sock, grp->id);
200 clear_bit(grp->id, mc_groups);
201 list_del(&grp->list);
202 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
203 grp->id = 0;
204 grp->family = NULL;
205}
206
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700207/**
208 * genl_unregister_mc_group - unregister a multicast group
209 *
210 * Unregisters the specified multicast group and notifies userspace
211 * about it. All current listeners on the group are removed.
212 *
213 * Note: It is not necessary to unregister all multicast groups before
214 * unregistering the family, unregistering the family will cause
215 * all assigned multicast groups to be unregistered automatically.
216 *
217 * @family: Generic netlink family the group belongs to.
218 * @grp: The group to unregister, must have been registered successfully
219 * previously.
220 */
221void genl_unregister_mc_group(struct genl_family *family,
222 struct genl_multicast_group *grp)
223{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700224 genl_lock();
Thomas Graf79dc43862007-07-24 15:32:46 -0700225 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700226 genl_unlock();
227}
228
229static void genl_unregister_mc_groups(struct genl_family *family)
230{
231 struct genl_multicast_group *grp, *tmp;
232
233 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700234 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700235}
236
Thomas Graf482a8522005-11-10 02:25:56 +0100237/**
238 * genl_register_ops - register generic netlink operations
239 * @family: generic netlink family
240 * @ops: operations to be registered
241 *
242 * Registers the specified operations and assigns them to the specified
243 * family. Either a doit or dumpit callback must be specified or the
244 * operation will fail. Only one operation structure per command
245 * identifier may be registered.
246 *
247 * See include/net/genetlink.h for more documenation on the operations
248 * structure.
249 *
250 * Returns 0 on success or a negative error code.
251 */
252int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
253{
254 int err = -EINVAL;
255
256 if (ops->dumpit == NULL && ops->doit == NULL)
257 goto errout;
258
259 if (genl_get_cmd(ops->cmd, family)) {
260 err = -EEXIST;
261 goto errout;
262 }
263
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800264 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800265 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800266 if (ops->doit)
267 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800268 if (ops->policy)
269 ops->flags |= GENL_CMD_CAP_HASPOL;
270
Thomas Graf482a8522005-11-10 02:25:56 +0100271 genl_lock();
272 list_add_tail(&ops->ops_list, &family->ops_list);
273 genl_unlock();
274
275 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
276 err = 0;
277errout:
278 return err;
279}
280
281/**
282 * genl_unregister_ops - unregister generic netlink operations
283 * @family: generic netlink family
284 * @ops: operations to be unregistered
285 *
286 * Unregisters the specified operations and unassigns them from the
287 * specified family. The operation blocks until the current message
288 * processing has finished and doesn't start again until the
289 * unregister process has finished.
290 *
291 * Note: It is not necessary to unregister all operations before
292 * unregistering the family, unregistering the family will cause
293 * all assigned operations to be unregistered automatically.
294 *
295 * Returns 0 on success or a negative error code.
296 */
297int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
298{
299 struct genl_ops *rc;
300
301 genl_lock();
302 list_for_each_entry(rc, &family->ops_list, ops_list) {
303 if (rc == ops) {
304 list_del(&ops->ops_list);
305 genl_unlock();
306 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
307 return 0;
308 }
309 }
310 genl_unlock();
311
312 return -ENOENT;
313}
314
315/**
316 * genl_register_family - register a generic netlink family
317 * @family: generic netlink family
318 *
319 * Registers the specified family after validating it first. Only one
320 * family may be registered with the same family name or identifier.
321 * The family id may equal GENL_ID_GENERATE causing an unique id to
322 * be automatically generated and assigned.
323 *
324 * Return 0 on success or a negative error code.
325 */
326int genl_register_family(struct genl_family *family)
327{
328 int err = -EINVAL;
329
330 if (family->id && family->id < GENL_MIN_ID)
331 goto errout;
332
333 if (family->id > GENL_MAX_ID)
334 goto errout;
335
336 INIT_LIST_HEAD(&family->ops_list);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700337 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100338
339 genl_lock();
340
341 if (genl_family_find_byname(family->name)) {
342 err = -EEXIST;
343 goto errout_locked;
344 }
345
346 if (genl_family_find_byid(family->id)) {
347 err = -EEXIST;
348 goto errout_locked;
349 }
350
Thomas Graf482a8522005-11-10 02:25:56 +0100351 if (family->id == GENL_ID_GENERATE) {
352 u16 newid = genl_generate_id();
353
354 if (!newid) {
355 err = -ENOMEM;
356 goto errout_locked;
357 }
358
359 family->id = newid;
360 }
361
362 if (family->maxattr) {
363 family->attrbuf = kmalloc((family->maxattr+1) *
364 sizeof(struct nlattr *), GFP_KERNEL);
365 if (family->attrbuf == NULL) {
366 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800367 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100368 }
369 } else
370 family->attrbuf = NULL;
371
372 list_add_tail(&family->family_list, genl_family_chain(family->id));
373 genl_unlock();
374
375 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
376
377 return 0;
378
379errout_locked:
380 genl_unlock();
381errout:
382 return err;
383}
384
385/**
386 * genl_unregister_family - unregister generic netlink family
387 * @family: generic netlink family
388 *
389 * Unregisters the specified family.
390 *
391 * Returns 0 on success or a negative error code.
392 */
393int genl_unregister_family(struct genl_family *family)
394{
395 struct genl_family *rc;
396
397 genl_lock();
398
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800399 genl_unregister_mc_groups(family);
400
Thomas Graf482a8522005-11-10 02:25:56 +0100401 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
402 if (family->id != rc->id || strcmp(rc->name, family->name))
403 continue;
404
405 list_del(&rc->family_list);
406 INIT_LIST_HEAD(&family->ops_list);
407 genl_unlock();
408
Thomas Graf482a8522005-11-10 02:25:56 +0100409 kfree(family->attrbuf);
410 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
411 return 0;
412 }
413
414 genl_unlock();
415
416 return -ENOENT;
417}
418
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700419static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100420{
421 struct genl_ops *ops;
422 struct genl_family *family;
423 struct genl_info info;
424 struct genlmsghdr *hdr = nlmsg_data(nlh);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700425 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100426
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900427 family = genl_family_find_byid(nlh->nlmsg_type);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700428 if (family == NULL)
429 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100430
431 hdrlen = GENL_HDRLEN + family->hdrsize;
432 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700433 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100434
435 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700436 if (ops == NULL)
437 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100438
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700439 if ((ops->flags & GENL_ADMIN_PERM) &&
440 security_netlink_recv(skb, CAP_NET_ADMIN))
441 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100442
443 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700444 if (ops->dumpit == NULL)
445 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100446
Thomas Grafc702e802007-03-22 23:30:55 -0700447 return netlink_dump_start(genl_sock, skb, nlh,
448 ops->dumpit, ops->done);
Thomas Graf482a8522005-11-10 02:25:56 +0100449 }
450
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700451 if (ops->doit == NULL)
452 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100453
454 if (family->attrbuf) {
455 err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr,
456 ops->policy);
457 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700458 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100459 }
460
461 info.snd_seq = nlh->nlmsg_seq;
462 info.snd_pid = NETLINK_CB(skb).pid;
463 info.nlhdr = nlh;
464 info.genlhdr = nlmsg_data(nlh);
465 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
466 info.attrs = family->attrbuf;
467
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700468 return ops->doit(skb, &info);
Thomas Graf482a8522005-11-10 02:25:56 +0100469}
470
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700471static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100472{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700473 genl_lock();
474 netlink_rcv_skb(skb, &genl_rcv_msg);
475 genl_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100476}
477
478/**************************************************************************
479 * Controller
480 **************************************************************************/
481
Thomas Graf17c157c2006-11-14 19:46:02 -0800482static struct genl_family genl_ctrl = {
483 .id = GENL_ID_CTRL,
484 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800485 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800486 .maxattr = CTRL_ATTR_MAX,
487};
488
Thomas Graf482a8522005-11-10 02:25:56 +0100489static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq,
490 u32 flags, struct sk_buff *skb, u8 cmd)
491{
492 void *hdr;
493
Thomas Graf17c157c2006-11-14 19:46:02 -0800494 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100495 if (hdr == NULL)
496 return -1;
497
498 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name);
499 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id);
Thomas Grafeb328112006-09-18 00:01:59 -0700500 NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version);
501 NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize);
502 NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr);
503
Thomas Grafe94ef682006-11-23 11:44:37 -0800504 if (!list_empty(&family->ops_list)) {
505 struct nlattr *nla_ops;
506 struct genl_ops *ops;
507 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700508
Thomas Grafe94ef682006-11-23 11:44:37 -0800509 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
510 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700511 goto nla_put_failure;
512
Thomas Grafe94ef682006-11-23 11:44:37 -0800513 list_for_each_entry(ops, &family->ops_list, ops_list) {
514 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700515
Thomas Grafe94ef682006-11-23 11:44:37 -0800516 nest = nla_nest_start(skb, idx++);
517 if (nest == NULL)
518 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700519
Thomas Grafe94ef682006-11-23 11:44:37 -0800520 NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd);
521 NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags);
Thomas Grafeb328112006-09-18 00:01:59 -0700522
Thomas Grafe94ef682006-11-23 11:44:37 -0800523 nla_nest_end(skb, nest);
524 }
525
526 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700527 }
528
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700529 if (!list_empty(&family->mcast_groups)) {
530 struct genl_multicast_group *grp;
531 struct nlattr *nla_grps;
532 int idx = 1;
533
534 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
535 if (nla_grps == NULL)
536 goto nla_put_failure;
537
538 list_for_each_entry(grp, &family->mcast_groups, list) {
539 struct nlattr *nest;
540
541 nest = nla_nest_start(skb, idx++);
542 if (nest == NULL)
543 goto nla_put_failure;
544
545 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
546 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
547 grp->name);
548
549 nla_nest_end(skb, nest);
550 }
551 nla_nest_end(skb, nla_grps);
552 }
553
554 return genlmsg_end(skb, hdr);
555
556nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700557 genlmsg_cancel(skb, hdr);
558 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700559}
560
561static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid,
562 u32 seq, u32 flags, struct sk_buff *skb,
563 u8 cmd)
564{
565 void *hdr;
566 struct nlattr *nla_grps;
567 struct nlattr *nest;
568
569 hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd);
570 if (hdr == NULL)
571 return -1;
572
573 NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name);
574 NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id);
575
576 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
577 if (nla_grps == NULL)
578 goto nla_put_failure;
579
580 nest = nla_nest_start(skb, 1);
581 if (nest == NULL)
582 goto nla_put_failure;
583
584 NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id);
585 NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME,
586 grp->name);
587
588 nla_nest_end(skb, nest);
589 nla_nest_end(skb, nla_grps);
590
Thomas Graf482a8522005-11-10 02:25:56 +0100591 return genlmsg_end(skb, hdr);
592
593nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700594 genlmsg_cancel(skb, hdr);
595 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100596}
597
598static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
599{
600
601 int i, n = 0;
602 struct genl_family *rt;
603 int chains_to_skip = cb->args[0];
604 int fams_to_skip = cb->args[1];
605
Thomas Grafeb328112006-09-18 00:01:59 -0700606 if (chains_to_skip != 0)
607 genl_lock();
608
Thomas Graf482a8522005-11-10 02:25:56 +0100609 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) {
610 if (i < chains_to_skip)
611 continue;
612 n = 0;
613 list_for_each_entry(rt, genl_family_chain(i), family_list) {
614 if (++n < fams_to_skip)
615 continue;
616 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid,
617 cb->nlh->nlmsg_seq, NLM_F_MULTI,
618 skb, CTRL_CMD_NEWFAMILY) < 0)
619 goto errout;
620 }
621
622 fams_to_skip = 0;
623 }
624
625errout:
Thomas Grafeb328112006-09-18 00:01:59 -0700626 if (chains_to_skip != 0)
627 genl_unlock();
628
Thomas Graf482a8522005-11-10 02:25:56 +0100629 cb->args[0] = i;
630 cb->args[1] = n;
631
632 return skb->len;
633}
634
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700635static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
636 u32 pid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100637{
638 struct sk_buff *skb;
639 int err;
640
Thomas Graf339bf982006-11-10 14:10:15 -0800641 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100642 if (skb == NULL)
643 return ERR_PTR(-ENOBUFS);
644
645 err = ctrl_fill_info(family, pid, seq, 0, skb, cmd);
646 if (err < 0) {
647 nlmsg_free(skb);
648 return ERR_PTR(err);
649 }
650
651 return skb;
652}
653
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700654static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
655 u32 pid, int seq, u8 cmd)
656{
657 struct sk_buff *skb;
658 int err;
659
660 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
661 if (skb == NULL)
662 return ERR_PTR(-ENOBUFS);
663
664 err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd);
665 if (err < 0) {
666 nlmsg_free(skb);
667 return ERR_PTR(err);
668 }
669
670 return skb;
671}
672
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700673static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100674 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700675 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
676 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100677};
678
679static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
680{
681 struct sk_buff *msg;
682 struct genl_family *res = NULL;
683 int err = -EINVAL;
684
685 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
686 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
687 res = genl_family_find_byid(id);
688 }
689
690 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700691 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100692
Thomas Graf5176f912006-08-26 20:13:18 -0700693 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100694 res = genl_family_find_byname(name);
695 }
696
697 if (res == NULL) {
698 err = -ENOENT;
699 goto errout;
700 }
701
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700702 msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq,
703 CTRL_CMD_NEWFAMILY);
Thomas Graf482a8522005-11-10 02:25:56 +0100704 if (IS_ERR(msg)) {
705 err = PTR_ERR(msg);
706 goto errout;
707 }
708
Thomas Graf81878d22006-11-14 19:45:27 -0800709 err = genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100710errout:
711 return err;
712}
713
714static int genl_ctrl_event(int event, void *data)
715{
716 struct sk_buff *msg;
717
718 if (genl_sock == NULL)
719 return 0;
720
721 switch (event) {
722 case CTRL_CMD_NEWFAMILY:
723 case CTRL_CMD_DELFAMILY:
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700724 msg = ctrl_build_family_msg(data, 0, 0, event);
725 if (IS_ERR(msg))
726 return PTR_ERR(msg);
727
728 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
729 break;
730 case CTRL_CMD_NEWMCAST_GRP:
731 case CTRL_CMD_DELMCAST_GRP:
732 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100733 if (IS_ERR(msg))
734 return PTR_ERR(msg);
735
Thomas Grafd387f6a2006-08-15 00:31:06 -0700736 genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100737 break;
738 }
739
740 return 0;
741}
742
743static struct genl_ops genl_ctrl_ops = {
744 .cmd = CTRL_CMD_GETFAMILY,
745 .doit = ctrl_getfamily,
746 .dumpit = ctrl_dumpfamily,
747 .policy = ctrl_policy,
748};
749
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700750static struct genl_multicast_group notify_grp = {
751 .name = "notify",
752};
753
Thomas Graf482a8522005-11-10 02:25:56 +0100754static int __init genl_init(void)
755{
756 int i, err;
757
758 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
759 INIT_LIST_HEAD(&family_ht[i]);
760
761 err = genl_register_family(&genl_ctrl);
762 if (err < 0)
763 goto errout;
764
765 err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops);
766 if (err < 0)
767 goto errout_register;
768
769 netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700770
771 /* we'll bump the group number right afterwards */
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200772 genl_sock = netlink_kernel_create(&init_net, NETLINK_GENERIC, 0,
773 genl_rcv, NULL, THIS_MODULE);
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800774 if (genl_sock == NULL)
Thomas Graf482a8522005-11-10 02:25:56 +0100775 panic("GENL: Cannot initialize generic netlink\n");
Thomas Graf482a8522005-11-10 02:25:56 +0100776
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700777 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
778 if (err < 0)
779 goto errout_register;
780
Thomas Graf482a8522005-11-10 02:25:56 +0100781 return 0;
782
783errout_register:
784 genl_unregister_family(&genl_ctrl);
785errout:
786 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100787}
788
789subsys_initcall(genl_init);
790
791EXPORT_SYMBOL(genl_sock);
792EXPORT_SYMBOL(genl_register_ops);
793EXPORT_SYMBOL(genl_unregister_ops);
794EXPORT_SYMBOL(genl_register_family);
795EXPORT_SYMBOL(genl_unregister_family);