blob: a7c62d3d05a153f615e4aad95310f5a5fed41d89 [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
Johannes Bergf84f7712013-11-14 17:14:45 +0100109static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100110{
Johannes Bergd91824c2013-11-14 17:14:44 +0100111 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100112
Johannes Bergd91824c2013-11-14 17:14:44 +0100113 for (i = 0; i < family->n_ops; i++)
114 if (family->ops[i].cmd == cmd)
115 return &family->ops[i];
Thomas Graf482a8522005-11-10 02:25:56 +0100116
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
Johannes Bergf84f7712013-11-14 17:14:45 +0100286static int genl_validate_add_ops(struct genl_family *family,
287 const struct genl_ops *ops,
Johannes Bergd91824c2013-11-14 17:14:44 +0100288 unsigned int n_ops)
Thomas Graf482a8522005-11-10 02:25:56 +0100289{
Johannes Bergd91824c2013-11-14 17:14:44 +0100290 int i, j;
Thomas Graf482a8522005-11-10 02:25:56 +0100291
Johannes Bergd91824c2013-11-14 17:14:44 +0100292 for (i = 0; i < n_ops; i++) {
293 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
294 return -EINVAL;
295 for (j = i + 1; j < n_ops; j++)
296 if (ops[i].cmd == ops[j].cmd)
297 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100298 }
299
Johannes Bergd91824c2013-11-14 17:14:44 +0100300 /* family is not registered yet, so no locking needed */
301 family->ops = ops;
302 family->n_ops = n_ops;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800303
Johannes Bergd91824c2013-11-14 17:14:44 +0100304 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100305}
Thomas Graf482a8522005-11-10 02:25:56 +0100306
307/**
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700308 * __genl_register_family - register a generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +0100309 * @family: generic netlink family
310 *
311 * Registers the specified family after validating it first. Only one
312 * family may be registered with the same family name or identifier.
313 * The family id may equal GENL_ID_GENERATE causing an unique id to
314 * be automatically generated and assigned.
315 *
316 * Return 0 on success or a negative error code.
317 */
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700318int __genl_register_family(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100319{
320 int err = -EINVAL;
321
322 if (family->id && family->id < GENL_MIN_ID)
323 goto errout;
324
325 if (family->id > GENL_MAX_ID)
326 goto errout;
327
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700328 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100329
Pravin B Shelardef31172013-04-23 07:48:30 +0000330 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100331
332 if (genl_family_find_byname(family->name)) {
333 err = -EEXIST;
334 goto errout_locked;
335 }
336
Thomas Graf482a8522005-11-10 02:25:56 +0100337 if (family->id == GENL_ID_GENERATE) {
338 u16 newid = genl_generate_id();
339
340 if (!newid) {
341 err = -ENOMEM;
342 goto errout_locked;
343 }
344
345 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000346 } else if (genl_family_find_byid(family->id)) {
347 err = -EEXIST;
348 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100349 }
350
Pravin B Shelardef31172013-04-23 07:48:30 +0000351 if (family->maxattr && !family->parallel_ops) {
Thomas Graf482a8522005-11-10 02:25:56 +0100352 family->attrbuf = kmalloc((family->maxattr+1) *
353 sizeof(struct nlattr *), GFP_KERNEL);
354 if (family->attrbuf == NULL) {
355 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800356 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100357 }
358 } else
359 family->attrbuf = NULL;
360
361 list_add_tail(&family->family_list, genl_family_chain(family->id));
Pravin B Shelardef31172013-04-23 07:48:30 +0000362 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100363
364 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
365
366 return 0;
367
368errout_locked:
Pravin B Shelardef31172013-04-23 07:48:30 +0000369 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100370errout:
371 return err;
372}
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700373EXPORT_SYMBOL(__genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100374
375/**
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700376 * __genl_register_family_with_ops - register a generic netlink family
Michał Mirosława7b11d72009-05-21 10:34:04 +0000377 * @family: generic netlink family
378 * @ops: operations to be registered
379 * @n_ops: number of elements to register
380 *
381 * Registers the specified family and operations from the specified table.
382 * Only one family may be registered with the same family name or identifier.
383 *
384 * The family id may equal GENL_ID_GENERATE causing an unique id to
385 * be automatically generated and assigned.
386 *
387 * Either a doit or dumpit callback must be specified for every registered
388 * operation or the function will fail. Only one operation structure per
389 * command identifier may be registered.
390 *
391 * See include/net/genetlink.h for more documenation on the operations
392 * structure.
393 *
Michał Mirosława7b11d72009-05-21 10:34:04 +0000394 * Return 0 on success or a negative error code.
395 */
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700396int __genl_register_family_with_ops(struct genl_family *family,
Johannes Bergf84f7712013-11-14 17:14:45 +0100397 const struct genl_ops *ops, size_t n_ops)
Michał Mirosława7b11d72009-05-21 10:34:04 +0000398{
Johannes Bergd91824c2013-11-14 17:14:44 +0100399 int err;
Michał Mirosława7b11d72009-05-21 10:34:04 +0000400
Johannes Bergd91824c2013-11-14 17:14:44 +0100401 err = genl_validate_add_ops(family, ops, n_ops);
Michał Mirosława7b11d72009-05-21 10:34:04 +0000402 if (err)
403 return err;
404
Johannes Bergd91824c2013-11-14 17:14:44 +0100405 return __genl_register_family(family);
Michał Mirosława7b11d72009-05-21 10:34:04 +0000406}
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700407EXPORT_SYMBOL(__genl_register_family_with_ops);
Michał Mirosława7b11d72009-05-21 10:34:04 +0000408
409/**
Thomas Graf482a8522005-11-10 02:25:56 +0100410 * genl_unregister_family - unregister generic netlink family
411 * @family: generic netlink family
412 *
413 * Unregisters the specified family.
414 *
415 * Returns 0 on success or a negative error code.
416 */
417int genl_unregister_family(struct genl_family *family)
418{
419 struct genl_family *rc;
420
Pravin B Shelardef31172013-04-23 07:48:30 +0000421 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100422
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800423 genl_unregister_mc_groups(family);
424
Thomas Graf482a8522005-11-10 02:25:56 +0100425 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
426 if (family->id != rc->id || strcmp(rc->name, family->name))
427 continue;
428
429 list_del(&rc->family_list);
Johannes Bergd91824c2013-11-14 17:14:44 +0100430 family->n_ops = 0;
Pravin B Shelardef31172013-04-23 07:48:30 +0000431 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100432
Thomas Graf482a8522005-11-10 02:25:56 +0100433 kfree(family->attrbuf);
434 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
435 return 0;
436 }
437
Pravin B Shelardef31172013-04-23 07:48:30 +0000438 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100439
440 return -ENOENT;
441}
Changli Gao416c2f92010-07-25 20:46:01 +0000442EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100443
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500444/**
445 * genlmsg_put - Add generic netlink header to netlink message
446 * @skb: socket buffer holding the message
Eric W. Biederman15e47302012-09-07 20:12:54 +0000447 * @portid: netlink portid the message is addressed to
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500448 * @seq: sequence number (usually the one of the sender)
449 * @family: generic netlink family
Ben Hutchings2c530402012-07-10 10:55:09 +0000450 * @flags: netlink message flags
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500451 * @cmd: generic netlink command
452 *
453 * Returns pointer to user specific header
454 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000455void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500456 struct genl_family *family, int flags, u8 cmd)
457{
458 struct nlmsghdr *nlh;
459 struct genlmsghdr *hdr;
460
Eric W. Biederman15e47302012-09-07 20:12:54 +0000461 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500462 family->hdrsize, flags);
463 if (nlh == NULL)
464 return NULL;
465
466 hdr = nlmsg_data(nlh);
467 hdr->cmd = cmd;
468 hdr->version = family->version;
469 hdr->reserved = 0;
470
471 return (char *) hdr + GENL_HDRLEN;
472}
473EXPORT_SYMBOL(genlmsg_put);
474
Pravin B Shelar9b963092013-08-23 12:44:55 -0700475static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
476{
Johannes Bergf84f7712013-11-14 17:14:45 +0100477 /* our ops are always const - netlink API doesn't propagate that */
478 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700479 int rc;
480
481 genl_lock();
482 rc = ops->dumpit(skb, cb);
483 genl_unlock();
484 return rc;
485}
486
487static int genl_lock_done(struct netlink_callback *cb)
488{
Johannes Bergf84f7712013-11-14 17:14:45 +0100489 /* our ops are always const - netlink API doesn't propagate that */
490 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700491 int rc = 0;
492
493 if (ops->done) {
494 genl_lock();
495 rc = ops->done(cb);
496 genl_unlock();
497 }
498 return rc;
499}
500
Pravin B Shelardef31172013-04-23 07:48:30 +0000501static int genl_family_rcv_msg(struct genl_family *family,
502 struct sk_buff *skb,
503 struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100504{
Johannes Bergf84f7712013-11-14 17:14:45 +0100505 const struct genl_ops *ops;
Johannes Berg134e6372009-07-10 09:51:34 +0000506 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100507 struct genl_info info;
508 struct genlmsghdr *hdr = nlmsg_data(nlh);
Pravin B Shelardef31172013-04-23 07:48:30 +0000509 struct nlattr **attrbuf;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700510 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100511
Johannes Berg134e6372009-07-10 09:51:34 +0000512 /* this family doesn't exist in this netns */
513 if (!family->netnsok && !net_eq(net, &init_net))
514 return -ENOENT;
515
Thomas Graf482a8522005-11-10 02:25:56 +0100516 hdrlen = GENL_HDRLEN + family->hdrsize;
517 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700518 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100519
520 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700521 if (ops == NULL)
522 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100523
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700524 if ((ops->flags & GENL_ADMIN_PERM) &&
Eric Parisfd778462012-01-03 12:25:16 -0500525 !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700526 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100527
Pablo Neirae1ee3672013-07-29 12:30:04 +0200528 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
Pravin B Shelar9b963092013-08-23 12:44:55 -0700529 int rc;
Pravin B Shelardef31172013-04-23 07:48:30 +0000530
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700531 if (ops->dumpit == NULL)
532 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100533
Pravin B Shelar9b963092013-08-23 12:44:55 -0700534 if (!family->parallel_ops) {
535 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700536 .module = family->module,
Johannes Bergf84f7712013-11-14 17:14:45 +0100537 /* we have const, but the netlink API doesn't */
538 .data = (void *)ops,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700539 .dump = genl_lock_dumpit,
540 .done = genl_lock_done,
541 };
542
543 genl_unlock();
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700544 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700545 genl_lock();
546
547 } else {
548 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700549 .module = family->module,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700550 .dump = ops->dumpit,
551 .done = ops->done,
552 };
553
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700554 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700555 }
556
557 return rc;
Thomas Graf482a8522005-11-10 02:25:56 +0100558 }
559
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700560 if (ops->doit == NULL)
561 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100562
Pravin B Shelardef31172013-04-23 07:48:30 +0000563 if (family->maxattr && family->parallel_ops) {
564 attrbuf = kmalloc((family->maxattr+1) *
565 sizeof(struct nlattr *), GFP_KERNEL);
566 if (attrbuf == NULL)
567 return -ENOMEM;
568 } else
569 attrbuf = family->attrbuf;
570
571 if (attrbuf) {
572 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
Thomas Graf482a8522005-11-10 02:25:56 +0100573 ops->policy);
574 if (err < 0)
Wei Yongjun50754d22013-04-26 15:34:16 +0000575 goto out;
Thomas Graf482a8522005-11-10 02:25:56 +0100576 }
577
578 info.snd_seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000579 info.snd_portid = NETLINK_CB(skb).portid;
Thomas Graf482a8522005-11-10 02:25:56 +0100580 info.nlhdr = nlh;
581 info.genlhdr = nlmsg_data(nlh);
582 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
Pravin B Shelardef31172013-04-23 07:48:30 +0000583 info.attrs = attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000584 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200585 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100586
Johannes Bergff4c92d2010-10-04 21:14:03 +0200587 if (family->pre_doit) {
588 err = family->pre_doit(ops, skb, &info);
589 if (err)
Wei Yongjun50754d22013-04-26 15:34:16 +0000590 goto out;
Johannes Bergff4c92d2010-10-04 21:14:03 +0200591 }
592
593 err = ops->doit(skb, &info);
594
595 if (family->post_doit)
596 family->post_doit(ops, skb, &info);
597
Wei Yongjun50754d22013-04-26 15:34:16 +0000598out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000599 if (family->parallel_ops)
600 kfree(attrbuf);
601
602 return err;
603}
604
605static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
606{
607 struct genl_family *family;
608 int err;
609
610 family = genl_family_find_byid(nlh->nlmsg_type);
611 if (family == NULL)
612 return -ENOENT;
613
614 if (!family->parallel_ops)
615 genl_lock();
616
617 err = genl_family_rcv_msg(family, skb, nlh);
618
619 if (!family->parallel_ops)
620 genl_unlock();
621
Johannes Bergff4c92d2010-10-04 21:14:03 +0200622 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100623}
624
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700625static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100626{
Pravin B Shelardef31172013-04-23 07:48:30 +0000627 down_read(&cb_lock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700628 netlink_rcv_skb(skb, &genl_rcv_msg);
Pravin B Shelardef31172013-04-23 07:48:30 +0000629 up_read(&cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +0100630}
631
632/**************************************************************************
633 * Controller
634 **************************************************************************/
635
Thomas Graf17c157c2006-11-14 19:46:02 -0800636static struct genl_family genl_ctrl = {
637 .id = GENL_ID_CTRL,
638 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800639 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800640 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000641 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800642};
643
Eric W. Biederman15e47302012-09-07 20:12:54 +0000644static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
Thomas Graf482a8522005-11-10 02:25:56 +0100645 u32 flags, struct sk_buff *skb, u8 cmd)
646{
647 void *hdr;
648
Eric W. Biederman15e47302012-09-07 20:12:54 +0000649 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100650 if (hdr == NULL)
651 return -1;
652
David S. Miller444653f2012-03-29 23:25:11 -0400653 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
654 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
655 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
656 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
657 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
658 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700659
Johannes Bergd91824c2013-11-14 17:14:44 +0100660 if (family->n_ops) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800661 struct nlattr *nla_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100662 int i;
Thomas Grafeb328112006-09-18 00:01:59 -0700663
Thomas Grafe94ef682006-11-23 11:44:37 -0800664 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
665 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700666 goto nla_put_failure;
667
Johannes Bergd91824c2013-11-14 17:14:44 +0100668 for (i = 0; i < family->n_ops; i++) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800669 struct nlattr *nest;
Johannes Bergf84f7712013-11-14 17:14:45 +0100670 const struct genl_ops *ops = &family->ops[i];
671 u32 flags = ops->flags;
672
673 if (ops->dumpit)
674 flags |= GENL_CMD_CAP_DUMP;
675 if (ops->doit)
676 flags |= GENL_CMD_CAP_DO;
677 if (ops->policy)
678 flags |= GENL_CMD_CAP_HASPOL;
Thomas Grafeb328112006-09-18 00:01:59 -0700679
Johannes Bergd91824c2013-11-14 17:14:44 +0100680 nest = nla_nest_start(skb, i + 1);
Thomas Grafe94ef682006-11-23 11:44:37 -0800681 if (nest == NULL)
682 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700683
David S. Miller444653f2012-03-29 23:25:11 -0400684 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
Johannes Bergf84f7712013-11-14 17:14:45 +0100685 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, flags))
David S. Miller444653f2012-03-29 23:25:11 -0400686 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700687
Thomas Grafe94ef682006-11-23 11:44:37 -0800688 nla_nest_end(skb, nest);
689 }
690
691 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700692 }
693
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700694 if (!list_empty(&family->mcast_groups)) {
695 struct genl_multicast_group *grp;
696 struct nlattr *nla_grps;
697 int idx = 1;
698
699 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
700 if (nla_grps == NULL)
701 goto nla_put_failure;
702
703 list_for_each_entry(grp, &family->mcast_groups, list) {
704 struct nlattr *nest;
705
706 nest = nla_nest_start(skb, idx++);
707 if (nest == NULL)
708 goto nla_put_failure;
709
David S. Miller444653f2012-03-29 23:25:11 -0400710 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
711 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
712 grp->name))
713 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700714
715 nla_nest_end(skb, nest);
716 }
717 nla_nest_end(skb, nla_grps);
718 }
719
720 return genlmsg_end(skb, hdr);
721
722nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700723 genlmsg_cancel(skb, hdr);
724 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700725}
726
Eric W. Biederman15e47302012-09-07 20:12:54 +0000727static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700728 u32 seq, u32 flags, struct sk_buff *skb,
729 u8 cmd)
730{
731 void *hdr;
732 struct nlattr *nla_grps;
733 struct nlattr *nest;
734
Eric W. Biederman15e47302012-09-07 20:12:54 +0000735 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700736 if (hdr == NULL)
737 return -1;
738
David S. Miller444653f2012-03-29 23:25:11 -0400739 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
740 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
741 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700742
743 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
744 if (nla_grps == NULL)
745 goto nla_put_failure;
746
747 nest = nla_nest_start(skb, 1);
748 if (nest == NULL)
749 goto nla_put_failure;
750
David S. Miller444653f2012-03-29 23:25:11 -0400751 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
752 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
753 grp->name))
754 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700755
756 nla_nest_end(skb, nest);
757 nla_nest_end(skb, nla_grps);
758
Thomas Graf482a8522005-11-10 02:25:56 +0100759 return genlmsg_end(skb, hdr);
760
761nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700762 genlmsg_cancel(skb, hdr);
763 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100764}
765
766static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
767{
768
769 int i, n = 0;
770 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000771 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100772 int chains_to_skip = cb->args[0];
773 int fams_to_skip = cb->args[1];
774
Samir Bellabese1d5a012010-01-07 22:10:56 +0000775 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100776 n = 0;
777 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000778 if (!rt->netnsok && !net_eq(net, &init_net))
779 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100780 if (++n < fams_to_skip)
781 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000782 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
Thomas Graf482a8522005-11-10 02:25:56 +0100783 cb->nlh->nlmsg_seq, NLM_F_MULTI,
784 skb, CTRL_CMD_NEWFAMILY) < 0)
785 goto errout;
786 }
787
788 fams_to_skip = 0;
789 }
790
791errout:
792 cb->args[0] = i;
793 cb->args[1] = n;
794
795 return skb->len;
796}
797
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700798static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000799 u32 portid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100800{
801 struct sk_buff *skb;
802 int err;
803
Thomas Graf339bf982006-11-10 14:10:15 -0800804 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100805 if (skb == NULL)
806 return ERR_PTR(-ENOBUFS);
807
Eric W. Biederman15e47302012-09-07 20:12:54 +0000808 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100809 if (err < 0) {
810 nlmsg_free(skb);
811 return ERR_PTR(err);
812 }
813
814 return skb;
815}
816
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700817static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000818 u32 portid, int seq, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700819{
820 struct sk_buff *skb;
821 int err;
822
823 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
824 if (skb == NULL)
825 return ERR_PTR(-ENOBUFS);
826
Eric W. Biederman15e47302012-09-07 20:12:54 +0000827 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700828 if (err < 0) {
829 nlmsg_free(skb);
830 return ERR_PTR(err);
831 }
832
833 return skb;
834}
835
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700836static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100837 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700838 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
839 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100840};
841
842static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
843{
844 struct sk_buff *msg;
845 struct genl_family *res = NULL;
846 int err = -EINVAL;
847
848 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
849 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
850 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000851 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100852 }
853
854 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700855 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100856
Thomas Graf5176f912006-08-26 20:13:18 -0700857 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100858 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500859#ifdef CONFIG_MODULES
860 if (res == NULL) {
861 genl_unlock();
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200862 up_read(&cb_lock);
Neil Hormane9412c32012-05-29 09:30:41 +0000863 request_module("net-pf-%d-proto-%d-family-%s",
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500864 PF_NETLINK, NETLINK_GENERIC, name);
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200865 down_read(&cb_lock);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500866 genl_lock();
867 res = genl_family_find_byname(name);
868 }
869#endif
Johannes Berg134e6372009-07-10 09:51:34 +0000870 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100871 }
872
Johannes Berg134e6372009-07-10 09:51:34 +0000873 if (res == NULL)
874 return err;
875
876 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
877 /* family doesn't exist here */
878 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100879 }
880
Eric W. Biederman15e47302012-09-07 20:12:54 +0000881 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700882 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000883 if (IS_ERR(msg))
884 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100885
Johannes Berg134e6372009-07-10 09:51:34 +0000886 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100887}
888
889static int genl_ctrl_event(int event, void *data)
890{
891 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000892 struct genl_family *family;
893 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100894
Johannes Berg134e6372009-07-10 09:51:34 +0000895 /* genl is still initialising */
896 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100897 return 0;
898
899 switch (event) {
900 case CTRL_CMD_NEWFAMILY:
901 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000902 family = data;
903 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700904 break;
905 case CTRL_CMD_NEWMCAST_GRP:
906 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000907 grp = data;
908 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700909 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100910 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000911 default:
912 return -EINVAL;
913 }
914
915 if (IS_ERR(msg))
916 return PTR_ERR(msg);
917
918 if (!family->netnsok) {
919 genlmsg_multicast_netns(&init_net, msg, 0,
920 GENL_ID_CTRL, GFP_KERNEL);
921 } else {
922 rcu_read_lock();
923 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
924 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100925 }
926
927 return 0;
928}
929
930static struct genl_ops genl_ctrl_ops = {
931 .cmd = CTRL_CMD_GETFAMILY,
932 .doit = ctrl_getfamily,
933 .dumpit = ctrl_dumpfamily,
934 .policy = ctrl_policy,
935};
936
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700937static struct genl_multicast_group notify_grp = {
938 .name = "notify",
939};
940
Johannes Berg134e6372009-07-10 09:51:34 +0000941static int __net_init genl_pernet_init(struct net *net)
942{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000943 struct netlink_kernel_cfg cfg = {
944 .input = genl_rcv,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000945 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000946 };
947
Johannes Berg134e6372009-07-10 09:51:34 +0000948 /* we'll bump the group number right afterwards */
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000949 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
Johannes Berg134e6372009-07-10 09:51:34 +0000950
951 if (!net->genl_sock && net_eq(net, &init_net))
952 panic("GENL: Cannot initialize generic netlink\n");
953
954 if (!net->genl_sock)
955 return -ENOMEM;
956
957 return 0;
958}
959
960static void __net_exit genl_pernet_exit(struct net *net)
961{
962 netlink_kernel_release(net->genl_sock);
963 net->genl_sock = NULL;
964}
965
966static struct pernet_operations genl_pernet_ops = {
967 .init = genl_pernet_init,
968 .exit = genl_pernet_exit,
969};
970
Thomas Graf482a8522005-11-10 02:25:56 +0100971static int __init genl_init(void)
972{
973 int i, err;
974
975 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
976 INIT_LIST_HEAD(&family_ht[i]);
977
Changli Gao652c6712010-07-25 23:21:05 +0000978 err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
Thomas Graf482a8522005-11-10 02:25:56 +0100979 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000980 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100981
Johannes Berg134e6372009-07-10 09:51:34 +0000982 err = register_pernet_subsys(&genl_pernet_ops);
983 if (err)
984 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100985
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700986 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
987 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000988 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700989
Thomas Graf482a8522005-11-10 02:25:56 +0100990 return 0;
991
Johannes Berg134e6372009-07-10 09:51:34 +0000992problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100993 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100994}
995
996subsys_initcall(genl_init);
997
Eric W. Biederman15e47302012-09-07 20:12:54 +0000998static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
Johannes Berg134e6372009-07-10 09:51:34 +0000999 gfp_t flags)
1000{
1001 struct sk_buff *tmp;
1002 struct net *net, *prev = NULL;
1003 int err;
1004
1005 for_each_net_rcu(net) {
1006 if (prev) {
1007 tmp = skb_clone(skb, flags);
1008 if (!tmp) {
1009 err = -ENOMEM;
1010 goto error;
1011 }
1012 err = nlmsg_multicast(prev->genl_sock, tmp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001013 portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001014 if (err)
1015 goto error;
1016 }
1017
1018 prev = net;
1019 }
1020
Eric W. Biederman15e47302012-09-07 20:12:54 +00001021 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001022 error:
1023 kfree_skb(skb);
1024 return err;
1025}
1026
Eric W. Biederman15e47302012-09-07 20:12:54 +00001027int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
Johannes Berg134e6372009-07-10 09:51:34 +00001028 gfp_t flags)
1029{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001030 return genlmsg_mcast(skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001031}
1032EXPORT_SYMBOL(genlmsg_multicast_allns);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001033
Eric W. Biederman15e47302012-09-07 20:12:54 +00001034void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
Pravin B Shelar263ba612011-11-10 19:14:37 -08001035 struct nlmsghdr *nlh, gfp_t flags)
1036{
1037 struct sock *sk = net->genl_sock;
1038 int report = 0;
1039
1040 if (nlh)
1041 report = nlmsg_report(nlh);
1042
Eric W. Biederman15e47302012-09-07 20:12:54 +00001043 nlmsg_notify(sk, skb, portid, group, report, flags);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001044}
1045EXPORT_SYMBOL(genl_notify);