blob: 1076fe16b1228b09581b9a355b13855e761ee1ce [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Thomas Graf482a8522005-11-10 02:25:56 +010012#include <linux/errno.h>
13#include <linux/types.h>
14#include <linux/socket.h>
15#include <linux/string.h>
16#include <linux/skbuff.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080017#include <linux/mutex.h>
Johannes Berg2dbba6f2007-07-18 15:47:52 -070018#include <linux/bitmap.h>
Pravin B Shelardef31172013-04-23 07:48:30 +000019#include <linux/rwsem.h>
Thomas Graf482a8522005-11-10 02:25:56 +010020#include <net/sock.h>
21#include <net/genetlink.h>
22
Ingo Molnar14cc3e22006-03-26 01:37:14 -080023static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
Pravin B Shelardef31172013-04-23 07:48:30 +000024static DECLARE_RWSEM(cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010025
James Chapmanf408e0c2010-04-02 06:19:05 +000026void genl_lock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010027{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080028 mutex_lock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010029}
James Chapmanf408e0c2010-04-02 06:19:05 +000030EXPORT_SYMBOL(genl_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010031
James Chapmanf408e0c2010-04-02 06:19:05 +000032void genl_unlock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010033{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080034 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010035}
James Chapmanf408e0c2010-04-02 06:19:05 +000036EXPORT_SYMBOL(genl_unlock);
Thomas Graf482a8522005-11-10 02:25:56 +010037
WANG Cong320f5ea2012-07-24 13:44:01 +080038#ifdef CONFIG_LOCKDEP
Pravin B Shelar86b13092011-11-10 19:14:51 -080039int lockdep_genl_is_held(void)
40{
41 return lockdep_is_held(&genl_mutex);
42}
43EXPORT_SYMBOL(lockdep_genl_is_held);
44#endif
45
Pravin B Shelardef31172013-04-23 07:48:30 +000046static 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
Thomas Graf482a8522005-11-10 02:25:56 +010058#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];
Johannes Berg2dbba6f2007-07-18 15:47:52 -070062/*
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;
Thomas Graf482a8522005-11-10 02:25:56 +010072
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*/
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000123static u16 genl_generate_id(void)
Thomas Graf482a8522005-11-10 02:25:56 +0100124{
Krishna Kumar988ade62009-10-14 19:55:07 +0000125 static u16 id_gen_idx = GENL_MIN_ID;
126 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100127
Krishna Kumar988ade62009-10-14 19:55:07 +0000128 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)
Thomas Graf482a8522005-11-10 02:25:56 +0100132 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000133 }
Thomas Graf482a8522005-11-10 02:25:56 +0100134
Krishna Kumar988ade62009-10-14 19:55:07 +0000135 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100136}
137
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700138static 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;
Brian Haleyb1f57192009-09-04 20:36:52 -0700156 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700157
158 BUG_ON(grp->name[0] == '\0');
Masatake YAMATOf1e79e22013-03-19 01:47:27 +0000159 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700160
Pravin B Shelardef31172013-04-23 07:48:30 +0000161 genl_lock_all();
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700162
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
Johannes Berg134e6372009-07-10 09:51:34 +0000194 if (family->netnsok) {
195 struct net *net;
196
Johannes Bergd136f1b2009-09-12 03:03:15 +0000197 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000198 rcu_read_lock();
199 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000200 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000201 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();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000210 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000211 goto out;
212 }
213 }
214 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000215 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000216 } else {
217 err = netlink_change_ngroups(init_net.genl_sock,
218 mc_groups_longs * BITS_PER_LONG);
219 if (err)
220 goto out;
221 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700222
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:
Pravin B Shelardef31172013-04-23 07:48:30 +0000230 genl_unlock_all();
Thomas Graf79d310d2007-07-24 15:34:53 -0700231 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700232}
233EXPORT_SYMBOL(genl_register_mc_group);
234
Thomas Graf79dc43862007-07-24 15:32:46 -0700235static void __genl_unregister_mc_group(struct genl_family *family,
236 struct genl_multicast_group *grp)
237{
Johannes Berg134e6372009-07-10 09:51:34 +0000238 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700239 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000240
Johannes Bergb8273572009-09-24 15:44:05 -0700241 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000242 rcu_read_lock();
243 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700244 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000245 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700246 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000247
Thomas Graf79dc43862007-07-24 15:32:46 -0700248 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
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700255/**
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{
Pravin B Shelardef31172013-04-23 07:48:30 +0000272 genl_lock_all();
Thomas Graf79dc43862007-07-24 15:32:46 -0700273 __genl_unregister_mc_group(family, grp);
Pravin B Shelardef31172013-04-23 07:48:30 +0000274 genl_unlock_all();
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700275}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800276EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700277
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)
Thomas Graf79dc43862007-07-24 15:32:46 -0700283 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700284}
285
Thomas Graf482a8522005-11-10 02:25:56 +0100286/**
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
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800313 if (ops->dumpit)
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800314 ops->flags |= GENL_CMD_CAP_DUMP;
Jamal Hadi Salim48d4ed72006-12-06 20:06:25 -0800315 if (ops->doit)
316 ops->flags |= GENL_CMD_CAP_DO;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800317 if (ops->policy)
318 ops->flags |= GENL_CMD_CAP_HASPOL;
319
Pravin B Shelardef31172013-04-23 07:48:30 +0000320 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100321 list_add_tail(&ops->ops_list, &family->ops_list);
Pravin B Shelardef31172013-04-23 07:48:30 +0000322 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100323
324 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
325 err = 0;
326errout:
327 return err;
328}
Changli Gao416c2f92010-07-25 20:46:01 +0000329EXPORT_SYMBOL(genl_register_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100330
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
Pravin B Shelardef31172013-04-23 07:48:30 +0000351 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100352 list_for_each_entry(rc, &family->ops_list, ops_list) {
353 if (rc == ops) {
354 list_del(&ops->ops_list);
Pravin B Shelardef31172013-04-23 07:48:30 +0000355 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100356 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
357 return 0;
358 }
359 }
Pravin B Shelardef31172013-04-23 07:48:30 +0000360 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100361
362 return -ENOENT;
363}
Changli Gao416c2f92010-07-25 20:46:01 +0000364EXPORT_SYMBOL(genl_unregister_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100365
366/**
367 * genl_register_family - register a generic netlink family
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 */
377int genl_register_family(struct genl_family *family)
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);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700388 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100389
Pravin B Shelardef31172013-04-23 07:48:30 +0000390 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100391
392 if (genl_family_find_byname(family->name)) {
393 err = -EEXIST;
394 goto errout_locked;
395 }
396
Thomas Graf482a8522005-11-10 02:25:56 +0100397 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;
Krishna Kumar93860b02009-10-14 19:54:53 +0000406 } else if (genl_family_find_byid(family->id)) {
407 err = -EEXIST;
408 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100409 }
410
Pravin B Shelardef31172013-04-23 07:48:30 +0000411 if (family->maxattr && !family->parallel_ops) {
Thomas Graf482a8522005-11-10 02:25:56 +0100412 family->attrbuf = kmalloc((family->maxattr+1) *
413 sizeof(struct nlattr *), GFP_KERNEL);
414 if (family->attrbuf == NULL) {
415 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800416 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100417 }
418 } else
419 family->attrbuf = NULL;
420
421 list_add_tail(&family->family_list, genl_family_chain(family->id));
Pravin B Shelardef31172013-04-23 07:48:30 +0000422 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100423
424 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
425
426 return 0;
427
428errout_locked:
Pravin B Shelardef31172013-04-23 07:48:30 +0000429 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100430errout:
431 return err;
432}
Changli Gao416c2f92010-07-25 20:46:01 +0000433EXPORT_SYMBOL(genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100434
435/**
Michał Mirosława7b11d72009-05-21 10:34:04 +0000436 * genl_register_family_with_ops - register a generic netlink family
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 */
460int genl_register_family_with_ops(struct genl_family *family,
461 struct genl_ops *ops, size_t n_ops)
462{
463 int err, i;
464
465 err = genl_register_family(family);
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}
479EXPORT_SYMBOL(genl_register_family_with_ops);
480
481/**
Thomas Graf482a8522005-11-10 02:25:56 +0100482 * 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
Pravin B Shelardef31172013-04-23 07:48:30 +0000493 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100494
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800495 genl_unregister_mc_groups(family);
496
Thomas Graf482a8522005-11-10 02:25:56 +0100497 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);
Pravin B Shelardef31172013-04-23 07:48:30 +0000503 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100504
Thomas Graf482a8522005-11-10 02:25:56 +0100505 kfree(family->attrbuf);
506 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
507 return 0;
508 }
509
Pravin B Shelardef31172013-04-23 07:48:30 +0000510 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100511
512 return -ENOENT;
513}
Changli Gao416c2f92010-07-25 20:46:01 +0000514EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100515
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500516/**
517 * genlmsg_put - Add generic netlink header to netlink message
518 * @skb: socket buffer holding the message
Eric W. Biederman15e47302012-09-07 20:12:54 +0000519 * @portid: netlink portid the message is addressed to
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500520 * @seq: sequence number (usually the one of the sender)
521 * @family: generic netlink family
Ben Hutchings2c530402012-07-10 10:55:09 +0000522 * @flags: netlink message flags
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500523 * @cmd: generic netlink command
524 *
525 * Returns pointer to user specific header
526 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000527void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500528 struct genl_family *family, int flags, u8 cmd)
529{
530 struct nlmsghdr *nlh;
531 struct genlmsghdr *hdr;
532
Eric W. Biederman15e47302012-09-07 20:12:54 +0000533 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500534 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
Pravin B Shelardef31172013-04-23 07:48:30 +0000547static int genl_family_rcv_msg(struct genl_family *family,
548 struct sk_buff *skb,
549 struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100550{
551 struct genl_ops *ops;
Johannes Berg134e6372009-07-10 09:51:34 +0000552 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100553 struct genl_info info;
554 struct genlmsghdr *hdr = nlmsg_data(nlh);
Pravin B Shelardef31172013-04-23 07:48:30 +0000555 struct nlattr **attrbuf;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700556 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100557
Johannes Berg134e6372009-07-10 09:51:34 +0000558 /* this family doesn't exist in this netns */
559 if (!family->netnsok && !net_eq(net, &init_net))
560 return -ENOENT;
561
Thomas Graf482a8522005-11-10 02:25:56 +0100562 hdrlen = GENL_HDRLEN + family->hdrsize;
563 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700564 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100565
566 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700567 if (ops == NULL)
568 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100569
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700570 if ((ops->flags & GENL_ADMIN_PERM) &&
Eric Parisfd778462012-01-03 12:25:16 -0500571 !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700572 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100573
David S. Millerb8f3ab42011-01-18 12:40:38 -0800574 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Pravin B Shelardef31172013-04-23 07:48:30 +0000575 struct netlink_dump_control c = {
576 .dump = ops->dumpit,
577 .done = ops->done,
578 };
579
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700580 if (ops->dumpit == NULL)
581 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100582
Pravin B Shelardef31172013-04-23 07:48:30 +0000583 return netlink_dump_start(net->genl_sock, skb, nlh, &c);
Thomas Graf482a8522005-11-10 02:25:56 +0100584 }
585
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700586 if (ops->doit == NULL)
587 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100588
Pravin B Shelardef31172013-04-23 07:48:30 +0000589 if (family->maxattr && family->parallel_ops) {
590 attrbuf = kmalloc((family->maxattr+1) *
591 sizeof(struct nlattr *), GFP_KERNEL);
592 if (attrbuf == NULL)
593 return -ENOMEM;
594 } else
595 attrbuf = family->attrbuf;
596
597 if (attrbuf) {
598 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
Thomas Graf482a8522005-11-10 02:25:56 +0100599 ops->policy);
600 if (err < 0)
Wei Yongjun50754d212013-04-26 15:34:16 +0000601 goto out;
Thomas Graf482a8522005-11-10 02:25:56 +0100602 }
603
604 info.snd_seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000605 info.snd_portid = NETLINK_CB(skb).portid;
Thomas Graf482a8522005-11-10 02:25:56 +0100606 info.nlhdr = nlh;
607 info.genlhdr = nlmsg_data(nlh);
608 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
Pravin B Shelardef31172013-04-23 07:48:30 +0000609 info.attrs = attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000610 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200611 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100612
Johannes Bergff4c92d2010-10-04 21:14:03 +0200613 if (family->pre_doit) {
614 err = family->pre_doit(ops, skb, &info);
615 if (err)
Wei Yongjun50754d212013-04-26 15:34:16 +0000616 goto out;
Johannes Bergff4c92d2010-10-04 21:14:03 +0200617 }
618
619 err = ops->doit(skb, &info);
620
621 if (family->post_doit)
622 family->post_doit(ops, skb, &info);
623
Wei Yongjun50754d212013-04-26 15:34:16 +0000624out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000625 if (family->parallel_ops)
626 kfree(attrbuf);
627
628 return err;
629}
630
631static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
632{
633 struct genl_family *family;
634 int err;
635
636 family = genl_family_find_byid(nlh->nlmsg_type);
637 if (family == NULL)
638 return -ENOENT;
639
640 if (!family->parallel_ops)
641 genl_lock();
642
643 err = genl_family_rcv_msg(family, skb, nlh);
644
645 if (!family->parallel_ops)
646 genl_unlock();
647
Johannes Bergff4c92d2010-10-04 21:14:03 +0200648 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100649}
650
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700651static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100652{
Pravin B Shelardef31172013-04-23 07:48:30 +0000653 down_read(&cb_lock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700654 netlink_rcv_skb(skb, &genl_rcv_msg);
Pravin B Shelardef31172013-04-23 07:48:30 +0000655 up_read(&cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +0100656}
657
658/**************************************************************************
659 * Controller
660 **************************************************************************/
661
Thomas Graf17c157c2006-11-14 19:46:02 -0800662static struct genl_family genl_ctrl = {
663 .id = GENL_ID_CTRL,
664 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800665 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800666 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000667 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800668};
669
Eric W. Biederman15e47302012-09-07 20:12:54 +0000670static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
Thomas Graf482a8522005-11-10 02:25:56 +0100671 u32 flags, struct sk_buff *skb, u8 cmd)
672{
673 void *hdr;
674
Eric W. Biederman15e47302012-09-07 20:12:54 +0000675 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100676 if (hdr == NULL)
677 return -1;
678
David S. Miller444653f2012-03-29 23:25:11 -0400679 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
680 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
681 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
682 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
683 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
684 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700685
Thomas Grafe94ef682006-11-23 11:44:37 -0800686 if (!list_empty(&family->ops_list)) {
687 struct nlattr *nla_ops;
688 struct genl_ops *ops;
689 int idx = 1;
Thomas Grafeb328112006-09-18 00:01:59 -0700690
Thomas Grafe94ef682006-11-23 11:44:37 -0800691 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
692 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700693 goto nla_put_failure;
694
Thomas Grafe94ef682006-11-23 11:44:37 -0800695 list_for_each_entry(ops, &family->ops_list, ops_list) {
696 struct nlattr *nest;
Thomas Grafeb328112006-09-18 00:01:59 -0700697
Thomas Grafe94ef682006-11-23 11:44:37 -0800698 nest = nla_nest_start(skb, idx++);
699 if (nest == NULL)
700 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700701
David S. Miller444653f2012-03-29 23:25:11 -0400702 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
703 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, ops->flags))
704 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700705
Thomas Grafe94ef682006-11-23 11:44:37 -0800706 nla_nest_end(skb, nest);
707 }
708
709 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700710 }
711
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700712 if (!list_empty(&family->mcast_groups)) {
713 struct genl_multicast_group *grp;
714 struct nlattr *nla_grps;
715 int idx = 1;
716
717 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
718 if (nla_grps == NULL)
719 goto nla_put_failure;
720
721 list_for_each_entry(grp, &family->mcast_groups, list) {
722 struct nlattr *nest;
723
724 nest = nla_nest_start(skb, idx++);
725 if (nest == NULL)
726 goto nla_put_failure;
727
David S. Miller444653f2012-03-29 23:25:11 -0400728 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
729 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
730 grp->name))
731 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700732
733 nla_nest_end(skb, nest);
734 }
735 nla_nest_end(skb, nla_grps);
736 }
737
738 return genlmsg_end(skb, hdr);
739
740nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700741 genlmsg_cancel(skb, hdr);
742 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700743}
744
Eric W. Biederman15e47302012-09-07 20:12:54 +0000745static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700746 u32 seq, u32 flags, struct sk_buff *skb,
747 u8 cmd)
748{
749 void *hdr;
750 struct nlattr *nla_grps;
751 struct nlattr *nest;
752
Eric W. Biederman15e47302012-09-07 20:12:54 +0000753 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700754 if (hdr == NULL)
755 return -1;
756
David S. Miller444653f2012-03-29 23:25:11 -0400757 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
758 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
759 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700760
761 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
762 if (nla_grps == NULL)
763 goto nla_put_failure;
764
765 nest = nla_nest_start(skb, 1);
766 if (nest == NULL)
767 goto nla_put_failure;
768
David S. Miller444653f2012-03-29 23:25:11 -0400769 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
770 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
771 grp->name))
772 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700773
774 nla_nest_end(skb, nest);
775 nla_nest_end(skb, nla_grps);
776
Thomas Graf482a8522005-11-10 02:25:56 +0100777 return genlmsg_end(skb, hdr);
778
779nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700780 genlmsg_cancel(skb, hdr);
781 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100782}
783
784static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
785{
786
787 int i, n = 0;
788 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000789 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100790 int chains_to_skip = cb->args[0];
791 int fams_to_skip = cb->args[1];
792
Samir Bellabese1d5a012010-01-07 22:10:56 +0000793 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100794 n = 0;
795 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000796 if (!rt->netnsok && !net_eq(net, &init_net))
797 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100798 if (++n < fams_to_skip)
799 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000800 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
Thomas Graf482a8522005-11-10 02:25:56 +0100801 cb->nlh->nlmsg_seq, NLM_F_MULTI,
802 skb, CTRL_CMD_NEWFAMILY) < 0)
803 goto errout;
804 }
805
806 fams_to_skip = 0;
807 }
808
809errout:
810 cb->args[0] = i;
811 cb->args[1] = n;
812
813 return skb->len;
814}
815
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700816static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000817 u32 portid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100818{
819 struct sk_buff *skb;
820 int err;
821
Thomas Graf339bf982006-11-10 14:10:15 -0800822 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100823 if (skb == NULL)
824 return ERR_PTR(-ENOBUFS);
825
Eric W. Biederman15e47302012-09-07 20:12:54 +0000826 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100827 if (err < 0) {
828 nlmsg_free(skb);
829 return ERR_PTR(err);
830 }
831
832 return skb;
833}
834
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700835static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000836 u32 portid, int seq, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700837{
838 struct sk_buff *skb;
839 int err;
840
841 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
842 if (skb == NULL)
843 return ERR_PTR(-ENOBUFS);
844
Eric W. Biederman15e47302012-09-07 20:12:54 +0000845 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700846 if (err < 0) {
847 nlmsg_free(skb);
848 return ERR_PTR(err);
849 }
850
851 return skb;
852}
853
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700854static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100855 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700856 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
857 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100858};
859
860static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
861{
862 struct sk_buff *msg;
863 struct genl_family *res = NULL;
864 int err = -EINVAL;
865
866 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
867 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
868 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000869 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100870 }
871
872 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700873 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100874
Thomas Graf5176f912006-08-26 20:13:18 -0700875 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100876 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500877#ifdef CONFIG_MODULES
878 if (res == NULL) {
879 genl_unlock();
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200880 up_read(&cb_lock);
Neil Hormane9412c32012-05-29 09:30:41 +0000881 request_module("net-pf-%d-proto-%d-family-%s",
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500882 PF_NETLINK, NETLINK_GENERIC, name);
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200883 down_read(&cb_lock);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500884 genl_lock();
885 res = genl_family_find_byname(name);
886 }
887#endif
Johannes Berg134e6372009-07-10 09:51:34 +0000888 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100889 }
890
Johannes Berg134e6372009-07-10 09:51:34 +0000891 if (res == NULL)
892 return err;
893
894 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
895 /* family doesn't exist here */
896 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100897 }
898
Eric W. Biederman15e47302012-09-07 20:12:54 +0000899 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700900 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000901 if (IS_ERR(msg))
902 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100903
Johannes Berg134e6372009-07-10 09:51:34 +0000904 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100905}
906
907static int genl_ctrl_event(int event, void *data)
908{
909 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000910 struct genl_family *family;
911 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100912
Johannes Berg134e6372009-07-10 09:51:34 +0000913 /* genl is still initialising */
914 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100915 return 0;
916
917 switch (event) {
918 case CTRL_CMD_NEWFAMILY:
919 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000920 family = data;
921 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700922 break;
923 case CTRL_CMD_NEWMCAST_GRP:
924 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000925 grp = data;
926 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700927 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100928 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000929 default:
930 return -EINVAL;
931 }
932
933 if (IS_ERR(msg))
934 return PTR_ERR(msg);
935
936 if (!family->netnsok) {
937 genlmsg_multicast_netns(&init_net, msg, 0,
938 GENL_ID_CTRL, GFP_KERNEL);
939 } else {
940 rcu_read_lock();
941 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
942 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100943 }
944
945 return 0;
946}
947
948static struct genl_ops genl_ctrl_ops = {
949 .cmd = CTRL_CMD_GETFAMILY,
950 .doit = ctrl_getfamily,
951 .dumpit = ctrl_dumpfamily,
952 .policy = ctrl_policy,
953};
954
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700955static struct genl_multicast_group notify_grp = {
956 .name = "notify",
957};
958
Johannes Berg134e6372009-07-10 09:51:34 +0000959static int __net_init genl_pernet_init(struct net *net)
960{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000961 struct netlink_kernel_cfg cfg = {
962 .input = genl_rcv,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000963 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000964 };
965
Johannes Berg134e6372009-07-10 09:51:34 +0000966 /* we'll bump the group number right afterwards */
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000967 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
Johannes Berg134e6372009-07-10 09:51:34 +0000968
969 if (!net->genl_sock && net_eq(net, &init_net))
970 panic("GENL: Cannot initialize generic netlink\n");
971
972 if (!net->genl_sock)
973 return -ENOMEM;
974
975 return 0;
976}
977
978static void __net_exit genl_pernet_exit(struct net *net)
979{
980 netlink_kernel_release(net->genl_sock);
981 net->genl_sock = NULL;
982}
983
984static struct pernet_operations genl_pernet_ops = {
985 .init = genl_pernet_init,
986 .exit = genl_pernet_exit,
987};
988
Thomas Graf482a8522005-11-10 02:25:56 +0100989static int __init genl_init(void)
990{
991 int i, err;
992
993 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
994 INIT_LIST_HEAD(&family_ht[i]);
995
Changli Gao652c6712010-07-25 23:21:05 +0000996 err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
Thomas Graf482a8522005-11-10 02:25:56 +0100997 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000998 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100999
Johannes Berg134e6372009-07-10 09:51:34 +00001000 err = register_pernet_subsys(&genl_pernet_ops);
1001 if (err)
1002 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +01001003
Johannes Berg2dbba6f2007-07-18 15:47:52 -07001004 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
1005 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +00001006 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -07001007
Thomas Graf482a8522005-11-10 02:25:56 +01001008 return 0;
1009
Johannes Berg134e6372009-07-10 09:51:34 +00001010problem:
Thomas Graf482a8522005-11-10 02:25:56 +01001011 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +01001012}
1013
1014subsys_initcall(genl_init);
1015
Eric W. Biederman15e47302012-09-07 20:12:54 +00001016static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
Johannes Berg134e6372009-07-10 09:51:34 +00001017 gfp_t flags)
1018{
1019 struct sk_buff *tmp;
1020 struct net *net, *prev = NULL;
1021 int err;
1022
1023 for_each_net_rcu(net) {
1024 if (prev) {
1025 tmp = skb_clone(skb, flags);
1026 if (!tmp) {
1027 err = -ENOMEM;
1028 goto error;
1029 }
1030 err = nlmsg_multicast(prev->genl_sock, tmp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001031 portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001032 if (err)
1033 goto error;
1034 }
1035
1036 prev = net;
1037 }
1038
Eric W. Biederman15e47302012-09-07 20:12:54 +00001039 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001040 error:
1041 kfree_skb(skb);
1042 return err;
1043}
1044
Eric W. Biederman15e47302012-09-07 20:12:54 +00001045int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
Johannes Berg134e6372009-07-10 09:51:34 +00001046 gfp_t flags)
1047{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001048 return genlmsg_mcast(skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001049}
1050EXPORT_SYMBOL(genlmsg_multicast_allns);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001051
Eric W. Biederman15e47302012-09-07 20:12:54 +00001052void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
Pravin B Shelar263ba612011-11-10 19:14:37 -08001053 struct nlmsghdr *nlh, gfp_t flags)
1054{
1055 struct sock *sk = net->genl_sock;
1056 int report = 0;
1057
1058 if (nlh)
1059 report = nlmsg_report(nlh);
1060
Eric W. Biederman15e47302012-09-07 20:12:54 +00001061 nlmsg_notify(sk, skb, portid, group, report, flags);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001062}
1063EXPORT_SYMBOL(genl_notify);