blob: bee91a7527a58ae002b5464471f14b93f875e511 [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.
Johannes Berge5dcecb2013-11-19 15:19:32 +010068 * Bit 1 is marked as already used since the drop-monitor code
69 * abuses the API and thinks it can statically use group 1.
70 * That group will typically conflict with other groups that
71 * any proper users use.
Johannes Berg2ecf7532013-11-19 15:19:33 +010072 * Bit 17 is marked as already used since the VFS quota code
73 * also abused this API and relied on family == group ID, we
74 * cater to that by giving it a static family and group ID.
Johannes Berg2dbba6f2007-07-18 15:47:52 -070075 */
Johannes Berg2ecf7532013-11-19 15:19:33 +010076static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_VFS_DQUOT);
Johannes Berg2dbba6f2007-07-18 15:47:52 -070077static unsigned long *mc_groups = &mc_group_start;
78static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010079
80static int genl_ctrl_event(int event, void *data);
81
82static inline unsigned int genl_family_hash(unsigned int id)
83{
84 return id & GENL_FAM_TAB_MASK;
85}
86
87static inline struct list_head *genl_family_chain(unsigned int id)
88{
89 return &family_ht[genl_family_hash(id)];
90}
91
92static struct genl_family *genl_family_find_byid(unsigned int id)
93{
94 struct genl_family *f;
95
96 list_for_each_entry(f, genl_family_chain(id), family_list)
97 if (f->id == id)
98 return f;
99
100 return NULL;
101}
102
103static struct genl_family *genl_family_find_byname(char *name)
104{
105 struct genl_family *f;
106 int i;
107
108 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
109 list_for_each_entry(f, genl_family_chain(i), family_list)
110 if (strcmp(f->name, name) == 0)
111 return f;
112
113 return NULL;
114}
115
Johannes Bergf84f7712013-11-14 17:14:45 +0100116static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100117{
Johannes Bergd91824c2013-11-14 17:14:44 +0100118 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100119
Johannes Bergd91824c2013-11-14 17:14:44 +0100120 for (i = 0; i < family->n_ops; i++)
121 if (family->ops[i].cmd == cmd)
122 return &family->ops[i];
Thomas Graf482a8522005-11-10 02:25:56 +0100123
124 return NULL;
125}
126
127/* Of course we are going to have problems once we hit
128 * 2^16 alive types, but that can only happen by year 2K
129*/
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000130static u16 genl_generate_id(void)
Thomas Graf482a8522005-11-10 02:25:56 +0100131{
Krishna Kumar988ade62009-10-14 19:55:07 +0000132 static u16 id_gen_idx = GENL_MIN_ID;
133 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100134
Krishna Kumar988ade62009-10-14 19:55:07 +0000135 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
Johannes Berg2ecf7532013-11-19 15:19:33 +0100136 if (id_gen_idx != GENL_ID_VFS_DQUOT &&
137 !genl_family_find_byid(id_gen_idx))
Krishna Kumar988ade62009-10-14 19:55:07 +0000138 return id_gen_idx;
139 if (++id_gen_idx > GENL_MAX_ID)
Thomas Graf482a8522005-11-10 02:25:56 +0100140 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000141 }
Thomas Graf482a8522005-11-10 02:25:56 +0100142
Krishna Kumar988ade62009-10-14 19:55:07 +0000143 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100144}
145
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700146static struct genl_multicast_group notify_grp;
147
148/**
149 * genl_register_mc_group - register a multicast group
150 *
151 * Registers the specified multicast group and notifies userspace
152 * about the new group.
153 *
154 * Returns 0 on success or a negative error code.
155 *
156 * @family: The generic netlink family the group shall be registered for.
157 * @grp: The group to register, must have a name.
158 */
159int genl_register_mc_group(struct genl_family *family,
160 struct genl_multicast_group *grp)
161{
162 int id;
163 unsigned long *new_groups;
Brian Haleyb1f57192009-09-04 20:36:52 -0700164 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700165
166 BUG_ON(grp->name[0] == '\0');
Masatake YAMATOf1e79e22013-03-19 01:47:27 +0000167 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700168
Pravin B Shelardef31172013-04-23 07:48:30 +0000169 genl_lock_all();
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700170
Johannes Berge5dcecb2013-11-19 15:19:32 +0100171 /* special-case our own group and hacks */
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700172 if (grp == &notify_grp)
173 id = GENL_ID_CTRL;
Johannes Berge5dcecb2013-11-19 15:19:32 +0100174 else if (strcmp(family->name, "NET_DM") == 0)
175 id = 1;
Johannes Berg2ecf7532013-11-19 15:19:33 +0100176 else if (strcmp(family->name, "VFS_DQUOT") == 0)
177 id = GENL_ID_VFS_DQUOT;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700178 else
179 id = find_first_zero_bit(mc_groups,
180 mc_groups_longs * BITS_PER_LONG);
181
182
183 if (id >= mc_groups_longs * BITS_PER_LONG) {
184 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
185
186 if (mc_groups == &mc_group_start) {
187 new_groups = kzalloc(nlen, GFP_KERNEL);
188 if (!new_groups) {
189 err = -ENOMEM;
190 goto out;
191 }
192 mc_groups = new_groups;
193 *mc_groups = mc_group_start;
194 } else {
195 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
196 if (!new_groups) {
197 err = -ENOMEM;
198 goto out;
199 }
200 mc_groups = new_groups;
201 mc_groups[mc_groups_longs] = 0;
202 }
203 mc_groups_longs++;
204 }
205
Johannes Berg134e6372009-07-10 09:51:34 +0000206 if (family->netnsok) {
207 struct net *net;
208
Johannes Bergd136f1b2009-09-12 03:03:15 +0000209 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000210 rcu_read_lock();
211 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000212 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000213 mc_groups_longs * BITS_PER_LONG);
214 if (err) {
215 /*
216 * No need to roll back, can only fail if
217 * memory allocation fails and then the
218 * number of _possible_ groups has been
219 * increased on some sockets which is ok.
220 */
221 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000222 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000223 goto out;
224 }
225 }
226 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000227 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000228 } else {
229 err = netlink_change_ngroups(init_net.genl_sock,
230 mc_groups_longs * BITS_PER_LONG);
231 if (err)
232 goto out;
233 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700234
235 grp->id = id;
236 set_bit(id, mc_groups);
237 list_add_tail(&grp->list, &family->mcast_groups);
238 grp->family = family;
239
240 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
241 out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000242 genl_unlock_all();
Thomas Graf79d310d2007-07-24 15:34:53 -0700243 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700244}
245EXPORT_SYMBOL(genl_register_mc_group);
246
Thomas Graf79dc43862007-07-24 15:32:46 -0700247static void __genl_unregister_mc_group(struct genl_family *family,
248 struct genl_multicast_group *grp)
249{
Johannes Berg134e6372009-07-10 09:51:34 +0000250 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700251 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000252
Johannes Bergb8273572009-09-24 15:44:05 -0700253 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000254 rcu_read_lock();
255 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700256 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000257 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700258 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000259
Johannes Berge5dcecb2013-11-19 15:19:32 +0100260 if (grp->id != 1)
261 clear_bit(grp->id, mc_groups);
Thomas Graf79dc43862007-07-24 15:32:46 -0700262 list_del(&grp->list);
263 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
264 grp->id = 0;
265 grp->family = NULL;
266}
267
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700268/**
269 * genl_unregister_mc_group - unregister a multicast group
270 *
271 * Unregisters the specified multicast group and notifies userspace
272 * about it. All current listeners on the group are removed.
273 *
274 * Note: It is not necessary to unregister all multicast groups before
275 * unregistering the family, unregistering the family will cause
276 * all assigned multicast groups to be unregistered automatically.
277 *
278 * @family: Generic netlink family the group belongs to.
279 * @grp: The group to unregister, must have been registered successfully
280 * previously.
281 */
282void genl_unregister_mc_group(struct genl_family *family,
283 struct genl_multicast_group *grp)
284{
Pravin B Shelardef31172013-04-23 07:48:30 +0000285 genl_lock_all();
Thomas Graf79dc43862007-07-24 15:32:46 -0700286 __genl_unregister_mc_group(family, grp);
Pravin B Shelardef31172013-04-23 07:48:30 +0000287 genl_unlock_all();
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700288}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800289EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700290
291static void genl_unregister_mc_groups(struct genl_family *family)
292{
293 struct genl_multicast_group *grp, *tmp;
294
295 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700296 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700297}
298
Johannes Berg568508a2013-11-15 14:19:08 +0100299static int genl_validate_ops(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100300{
Johannes Berg568508a2013-11-15 14:19:08 +0100301 const struct genl_ops *ops = family->ops;
302 unsigned int n_ops = family->n_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100303 int i, j;
Thomas Graf482a8522005-11-10 02:25:56 +0100304
Johannes Berg568508a2013-11-15 14:19:08 +0100305 if (WARN_ON(n_ops && !ops))
306 return -EINVAL;
307
308 if (!n_ops)
309 return 0;
310
Johannes Bergd91824c2013-11-14 17:14:44 +0100311 for (i = 0; i < n_ops; i++) {
312 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
313 return -EINVAL;
314 for (j = i + 1; j < n_ops; j++)
315 if (ops[i].cmd == ops[j].cmd)
316 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100317 }
318
Johannes Bergd91824c2013-11-14 17:14:44 +0100319 /* family is not registered yet, so no locking needed */
320 family->ops = ops;
321 family->n_ops = n_ops;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800322
Johannes Bergd91824c2013-11-14 17:14:44 +0100323 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100324}
Thomas Graf482a8522005-11-10 02:25:56 +0100325
326/**
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700327 * __genl_register_family - register a generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +0100328 * @family: generic netlink family
329 *
330 * Registers the specified family after validating it first. Only one
331 * family may be registered with the same family name or identifier.
332 * The family id may equal GENL_ID_GENERATE causing an unique id to
333 * be automatically generated and assigned.
334 *
Johannes Berg568508a2013-11-15 14:19:08 +0100335 * The family's ops array must already be assigned, you can use the
336 * genl_register_family_with_ops() helper function.
337 *
Thomas Graf482a8522005-11-10 02:25:56 +0100338 * Return 0 on success or a negative error code.
339 */
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700340int __genl_register_family(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100341{
342 int err = -EINVAL;
343
344 if (family->id && family->id < GENL_MIN_ID)
345 goto errout;
346
347 if (family->id > GENL_MAX_ID)
348 goto errout;
349
Johannes Berg568508a2013-11-15 14:19:08 +0100350 err = genl_validate_ops(family);
351 if (err)
352 return err;
353
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700354 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100355
Pravin B Shelardef31172013-04-23 07:48:30 +0000356 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100357
358 if (genl_family_find_byname(family->name)) {
359 err = -EEXIST;
360 goto errout_locked;
361 }
362
Thomas Graf482a8522005-11-10 02:25:56 +0100363 if (family->id == GENL_ID_GENERATE) {
364 u16 newid = genl_generate_id();
365
366 if (!newid) {
367 err = -ENOMEM;
368 goto errout_locked;
369 }
370
371 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000372 } else if (genl_family_find_byid(family->id)) {
373 err = -EEXIST;
374 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100375 }
376
Pravin B Shelardef31172013-04-23 07:48:30 +0000377 if (family->maxattr && !family->parallel_ops) {
Thomas Graf482a8522005-11-10 02:25:56 +0100378 family->attrbuf = kmalloc((family->maxattr+1) *
379 sizeof(struct nlattr *), GFP_KERNEL);
380 if (family->attrbuf == NULL) {
381 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800382 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100383 }
384 } else
385 family->attrbuf = NULL;
386
387 list_add_tail(&family->family_list, genl_family_chain(family->id));
Pravin B Shelardef31172013-04-23 07:48:30 +0000388 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100389
390 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
391
392 return 0;
393
394errout_locked:
Pravin B Shelardef31172013-04-23 07:48:30 +0000395 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100396errout:
397 return err;
398}
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700399EXPORT_SYMBOL(__genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100400
401/**
402 * genl_unregister_family - unregister generic netlink family
403 * @family: generic netlink family
404 *
405 * Unregisters the specified family.
406 *
407 * Returns 0 on success or a negative error code.
408 */
409int genl_unregister_family(struct genl_family *family)
410{
411 struct genl_family *rc;
412
Pravin B Shelardef31172013-04-23 07:48:30 +0000413 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100414
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800415 genl_unregister_mc_groups(family);
416
Thomas Graf482a8522005-11-10 02:25:56 +0100417 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
418 if (family->id != rc->id || strcmp(rc->name, family->name))
419 continue;
420
421 list_del(&rc->family_list);
Johannes Bergd91824c2013-11-14 17:14:44 +0100422 family->n_ops = 0;
Pravin B Shelardef31172013-04-23 07:48:30 +0000423 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100424
Thomas Graf482a8522005-11-10 02:25:56 +0100425 kfree(family->attrbuf);
426 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
427 return 0;
428 }
429
Pravin B Shelardef31172013-04-23 07:48:30 +0000430 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100431
432 return -ENOENT;
433}
Changli Gao416c2f92010-07-25 20:46:01 +0000434EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100435
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500436/**
437 * genlmsg_put - Add generic netlink header to netlink message
438 * @skb: socket buffer holding the message
Eric W. Biederman15e47302012-09-07 20:12:54 +0000439 * @portid: netlink portid the message is addressed to
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500440 * @seq: sequence number (usually the one of the sender)
441 * @family: generic netlink family
Ben Hutchings2c530402012-07-10 10:55:09 +0000442 * @flags: netlink message flags
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500443 * @cmd: generic netlink command
444 *
445 * Returns pointer to user specific header
446 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000447void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500448 struct genl_family *family, int flags, u8 cmd)
449{
450 struct nlmsghdr *nlh;
451 struct genlmsghdr *hdr;
452
Eric W. Biederman15e47302012-09-07 20:12:54 +0000453 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500454 family->hdrsize, flags);
455 if (nlh == NULL)
456 return NULL;
457
458 hdr = nlmsg_data(nlh);
459 hdr->cmd = cmd;
460 hdr->version = family->version;
461 hdr->reserved = 0;
462
463 return (char *) hdr + GENL_HDRLEN;
464}
465EXPORT_SYMBOL(genlmsg_put);
466
Pravin B Shelar9b963092013-08-23 12:44:55 -0700467static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
468{
Johannes Bergf84f7712013-11-14 17:14:45 +0100469 /* our ops are always const - netlink API doesn't propagate that */
470 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700471 int rc;
472
473 genl_lock();
474 rc = ops->dumpit(skb, cb);
475 genl_unlock();
476 return rc;
477}
478
479static int genl_lock_done(struct netlink_callback *cb)
480{
Johannes Bergf84f7712013-11-14 17:14:45 +0100481 /* our ops are always const - netlink API doesn't propagate that */
482 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700483 int rc = 0;
484
485 if (ops->done) {
486 genl_lock();
487 rc = ops->done(cb);
488 genl_unlock();
489 }
490 return rc;
491}
492
Pravin B Shelardef31172013-04-23 07:48:30 +0000493static int genl_family_rcv_msg(struct genl_family *family,
494 struct sk_buff *skb,
495 struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100496{
Johannes Bergf84f7712013-11-14 17:14:45 +0100497 const struct genl_ops *ops;
Johannes Berg134e6372009-07-10 09:51:34 +0000498 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100499 struct genl_info info;
500 struct genlmsghdr *hdr = nlmsg_data(nlh);
Pravin B Shelardef31172013-04-23 07:48:30 +0000501 struct nlattr **attrbuf;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700502 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100503
Johannes Berg134e6372009-07-10 09:51:34 +0000504 /* this family doesn't exist in this netns */
505 if (!family->netnsok && !net_eq(net, &init_net))
506 return -ENOENT;
507
Thomas Graf482a8522005-11-10 02:25:56 +0100508 hdrlen = GENL_HDRLEN + family->hdrsize;
509 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700510 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100511
512 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700513 if (ops == NULL)
514 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100515
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700516 if ((ops->flags & GENL_ADMIN_PERM) &&
Eric Parisfd778462012-01-03 12:25:16 -0500517 !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700518 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100519
Pablo Neirae1ee3672013-07-29 12:30:04 +0200520 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
Pravin B Shelar9b963092013-08-23 12:44:55 -0700521 int rc;
Pravin B Shelardef31172013-04-23 07:48:30 +0000522
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700523 if (ops->dumpit == NULL)
524 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100525
Pravin B Shelar9b963092013-08-23 12:44:55 -0700526 if (!family->parallel_ops) {
527 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700528 .module = family->module,
Johannes Bergf84f7712013-11-14 17:14:45 +0100529 /* we have const, but the netlink API doesn't */
530 .data = (void *)ops,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700531 .dump = genl_lock_dumpit,
532 .done = genl_lock_done,
533 };
534
535 genl_unlock();
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700536 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700537 genl_lock();
538
539 } else {
540 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700541 .module = family->module,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700542 .dump = ops->dumpit,
543 .done = ops->done,
544 };
545
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700546 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700547 }
548
549 return rc;
Thomas Graf482a8522005-11-10 02:25:56 +0100550 }
551
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700552 if (ops->doit == NULL)
553 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100554
Pravin B Shelardef31172013-04-23 07:48:30 +0000555 if (family->maxattr && family->parallel_ops) {
556 attrbuf = kmalloc((family->maxattr+1) *
557 sizeof(struct nlattr *), GFP_KERNEL);
558 if (attrbuf == NULL)
559 return -ENOMEM;
560 } else
561 attrbuf = family->attrbuf;
562
563 if (attrbuf) {
564 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
Thomas Graf482a8522005-11-10 02:25:56 +0100565 ops->policy);
566 if (err < 0)
Wei Yongjun50754d22013-04-26 15:34:16 +0000567 goto out;
Thomas Graf482a8522005-11-10 02:25:56 +0100568 }
569
570 info.snd_seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000571 info.snd_portid = NETLINK_CB(skb).portid;
Thomas Graf482a8522005-11-10 02:25:56 +0100572 info.nlhdr = nlh;
573 info.genlhdr = nlmsg_data(nlh);
574 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
Pravin B Shelardef31172013-04-23 07:48:30 +0000575 info.attrs = attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000576 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200577 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100578
Johannes Bergff4c92d2010-10-04 21:14:03 +0200579 if (family->pre_doit) {
580 err = family->pre_doit(ops, skb, &info);
581 if (err)
Wei Yongjun50754d22013-04-26 15:34:16 +0000582 goto out;
Johannes Bergff4c92d2010-10-04 21:14:03 +0200583 }
584
585 err = ops->doit(skb, &info);
586
587 if (family->post_doit)
588 family->post_doit(ops, skb, &info);
589
Wei Yongjun50754d22013-04-26 15:34:16 +0000590out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000591 if (family->parallel_ops)
592 kfree(attrbuf);
593
594 return err;
595}
596
597static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
598{
599 struct genl_family *family;
600 int err;
601
602 family = genl_family_find_byid(nlh->nlmsg_type);
603 if (family == NULL)
604 return -ENOENT;
605
606 if (!family->parallel_ops)
607 genl_lock();
608
609 err = genl_family_rcv_msg(family, skb, nlh);
610
611 if (!family->parallel_ops)
612 genl_unlock();
613
Johannes Bergff4c92d2010-10-04 21:14:03 +0200614 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100615}
616
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700617static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100618{
Pravin B Shelardef31172013-04-23 07:48:30 +0000619 down_read(&cb_lock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700620 netlink_rcv_skb(skb, &genl_rcv_msg);
Pravin B Shelardef31172013-04-23 07:48:30 +0000621 up_read(&cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +0100622}
623
624/**************************************************************************
625 * Controller
626 **************************************************************************/
627
Thomas Graf17c157c2006-11-14 19:46:02 -0800628static struct genl_family genl_ctrl = {
629 .id = GENL_ID_CTRL,
630 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800631 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800632 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000633 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800634};
635
Eric W. Biederman15e47302012-09-07 20:12:54 +0000636static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
Thomas Graf482a8522005-11-10 02:25:56 +0100637 u32 flags, struct sk_buff *skb, u8 cmd)
638{
639 void *hdr;
640
Eric W. Biederman15e47302012-09-07 20:12:54 +0000641 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100642 if (hdr == NULL)
643 return -1;
644
David S. Miller444653f2012-03-29 23:25:11 -0400645 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
646 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
647 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
648 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
649 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
650 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700651
Johannes Bergd91824c2013-11-14 17:14:44 +0100652 if (family->n_ops) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800653 struct nlattr *nla_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100654 int i;
Thomas Grafeb328112006-09-18 00:01:59 -0700655
Thomas Grafe94ef682006-11-23 11:44:37 -0800656 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
657 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700658 goto nla_put_failure;
659
Johannes Bergd91824c2013-11-14 17:14:44 +0100660 for (i = 0; i < family->n_ops; i++) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800661 struct nlattr *nest;
Johannes Bergf84f7712013-11-14 17:14:45 +0100662 const struct genl_ops *ops = &family->ops[i];
Johannes Berg029b2342013-11-18 20:54:58 +0100663 u32 op_flags = ops->flags;
Johannes Bergf84f7712013-11-14 17:14:45 +0100664
665 if (ops->dumpit)
Johannes Berg029b2342013-11-18 20:54:58 +0100666 op_flags |= GENL_CMD_CAP_DUMP;
Johannes Bergf84f7712013-11-14 17:14:45 +0100667 if (ops->doit)
Johannes Berg029b2342013-11-18 20:54:58 +0100668 op_flags |= GENL_CMD_CAP_DO;
Johannes Bergf84f7712013-11-14 17:14:45 +0100669 if (ops->policy)
Johannes Berg029b2342013-11-18 20:54:58 +0100670 op_flags |= GENL_CMD_CAP_HASPOL;
Thomas Grafeb328112006-09-18 00:01:59 -0700671
Johannes Bergd91824c2013-11-14 17:14:44 +0100672 nest = nla_nest_start(skb, i + 1);
Thomas Grafe94ef682006-11-23 11:44:37 -0800673 if (nest == NULL)
674 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700675
David S. Miller444653f2012-03-29 23:25:11 -0400676 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
Johannes Berg029b2342013-11-18 20:54:58 +0100677 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
David S. Miller444653f2012-03-29 23:25:11 -0400678 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700679
Thomas Grafe94ef682006-11-23 11:44:37 -0800680 nla_nest_end(skb, nest);
681 }
682
683 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700684 }
685
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700686 if (!list_empty(&family->mcast_groups)) {
687 struct genl_multicast_group *grp;
688 struct nlattr *nla_grps;
689 int idx = 1;
690
691 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
692 if (nla_grps == NULL)
693 goto nla_put_failure;
694
695 list_for_each_entry(grp, &family->mcast_groups, list) {
696 struct nlattr *nest;
697
698 nest = nla_nest_start(skb, idx++);
699 if (nest == NULL)
700 goto nla_put_failure;
701
David S. Miller444653f2012-03-29 23:25:11 -0400702 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
703 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
704 grp->name))
705 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700706
707 nla_nest_end(skb, nest);
708 }
709 nla_nest_end(skb, nla_grps);
710 }
711
712 return genlmsg_end(skb, hdr);
713
714nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700715 genlmsg_cancel(skb, hdr);
716 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700717}
718
Eric W. Biederman15e47302012-09-07 20:12:54 +0000719static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700720 u32 seq, u32 flags, struct sk_buff *skb,
721 u8 cmd)
722{
723 void *hdr;
724 struct nlattr *nla_grps;
725 struct nlattr *nest;
726
Eric W. Biederman15e47302012-09-07 20:12:54 +0000727 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700728 if (hdr == NULL)
729 return -1;
730
David S. Miller444653f2012-03-29 23:25:11 -0400731 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
732 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
733 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700734
735 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
736 if (nla_grps == NULL)
737 goto nla_put_failure;
738
739 nest = nla_nest_start(skb, 1);
740 if (nest == NULL)
741 goto nla_put_failure;
742
David S. Miller444653f2012-03-29 23:25:11 -0400743 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
744 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
745 grp->name))
746 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700747
748 nla_nest_end(skb, nest);
749 nla_nest_end(skb, nla_grps);
750
Thomas Graf482a8522005-11-10 02:25:56 +0100751 return genlmsg_end(skb, hdr);
752
753nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700754 genlmsg_cancel(skb, hdr);
755 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100756}
757
758static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
759{
760
761 int i, n = 0;
762 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000763 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100764 int chains_to_skip = cb->args[0];
765 int fams_to_skip = cb->args[1];
766
Samir Bellabese1d5a012010-01-07 22:10:56 +0000767 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100768 n = 0;
769 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000770 if (!rt->netnsok && !net_eq(net, &init_net))
771 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100772 if (++n < fams_to_skip)
773 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000774 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
Thomas Graf482a8522005-11-10 02:25:56 +0100775 cb->nlh->nlmsg_seq, NLM_F_MULTI,
776 skb, CTRL_CMD_NEWFAMILY) < 0)
777 goto errout;
778 }
779
780 fams_to_skip = 0;
781 }
782
783errout:
784 cb->args[0] = i;
785 cb->args[1] = n;
786
787 return skb->len;
788}
789
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700790static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000791 u32 portid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100792{
793 struct sk_buff *skb;
794 int err;
795
Thomas Graf339bf982006-11-10 14:10:15 -0800796 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100797 if (skb == NULL)
798 return ERR_PTR(-ENOBUFS);
799
Eric W. Biederman15e47302012-09-07 20:12:54 +0000800 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100801 if (err < 0) {
802 nlmsg_free(skb);
803 return ERR_PTR(err);
804 }
805
806 return skb;
807}
808
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700809static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000810 u32 portid, int seq, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700811{
812 struct sk_buff *skb;
813 int err;
814
815 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
816 if (skb == NULL)
817 return ERR_PTR(-ENOBUFS);
818
Eric W. Biederman15e47302012-09-07 20:12:54 +0000819 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700820 if (err < 0) {
821 nlmsg_free(skb);
822 return ERR_PTR(err);
823 }
824
825 return skb;
826}
827
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700828static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100829 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700830 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
831 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100832};
833
834static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
835{
836 struct sk_buff *msg;
837 struct genl_family *res = NULL;
838 int err = -EINVAL;
839
840 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
841 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
842 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000843 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100844 }
845
846 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700847 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100848
Thomas Graf5176f912006-08-26 20:13:18 -0700849 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100850 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500851#ifdef CONFIG_MODULES
852 if (res == NULL) {
853 genl_unlock();
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200854 up_read(&cb_lock);
Neil Hormane9412c32012-05-29 09:30:41 +0000855 request_module("net-pf-%d-proto-%d-family-%s",
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500856 PF_NETLINK, NETLINK_GENERIC, name);
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200857 down_read(&cb_lock);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500858 genl_lock();
859 res = genl_family_find_byname(name);
860 }
861#endif
Johannes Berg134e6372009-07-10 09:51:34 +0000862 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100863 }
864
Johannes Berg134e6372009-07-10 09:51:34 +0000865 if (res == NULL)
866 return err;
867
868 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
869 /* family doesn't exist here */
870 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100871 }
872
Eric W. Biederman15e47302012-09-07 20:12:54 +0000873 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700874 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000875 if (IS_ERR(msg))
876 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100877
Johannes Berg134e6372009-07-10 09:51:34 +0000878 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100879}
880
881static int genl_ctrl_event(int event, void *data)
882{
883 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000884 struct genl_family *family;
885 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100886
Johannes Berg134e6372009-07-10 09:51:34 +0000887 /* genl is still initialising */
888 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100889 return 0;
890
891 switch (event) {
892 case CTRL_CMD_NEWFAMILY:
893 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000894 family = data;
895 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700896 break;
897 case CTRL_CMD_NEWMCAST_GRP:
898 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000899 grp = data;
900 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700901 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100902 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000903 default:
904 return -EINVAL;
905 }
906
907 if (IS_ERR(msg))
908 return PTR_ERR(msg);
909
910 if (!family->netnsok) {
911 genlmsg_multicast_netns(&init_net, msg, 0,
912 GENL_ID_CTRL, GFP_KERNEL);
913 } else {
914 rcu_read_lock();
915 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
916 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100917 }
918
919 return 0;
920}
921
Johannes Bergc53ed742013-11-19 15:19:31 +0100922static struct genl_ops genl_ctrl_ops[] = {
923 {
924 .cmd = CTRL_CMD_GETFAMILY,
925 .doit = ctrl_getfamily,
926 .dumpit = ctrl_dumpfamily,
927 .policy = ctrl_policy,
928 },
Thomas Graf482a8522005-11-10 02:25:56 +0100929};
930
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700931static struct genl_multicast_group notify_grp = {
932 .name = "notify",
933};
934
Johannes Berg134e6372009-07-10 09:51:34 +0000935static int __net_init genl_pernet_init(struct net *net)
936{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000937 struct netlink_kernel_cfg cfg = {
938 .input = genl_rcv,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000939 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000940 };
941
Johannes Berg134e6372009-07-10 09:51:34 +0000942 /* we'll bump the group number right afterwards */
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000943 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
Johannes Berg134e6372009-07-10 09:51:34 +0000944
945 if (!net->genl_sock && net_eq(net, &init_net))
946 panic("GENL: Cannot initialize generic netlink\n");
947
948 if (!net->genl_sock)
949 return -ENOMEM;
950
951 return 0;
952}
953
954static void __net_exit genl_pernet_exit(struct net *net)
955{
956 netlink_kernel_release(net->genl_sock);
957 net->genl_sock = NULL;
958}
959
960static struct pernet_operations genl_pernet_ops = {
961 .init = genl_pernet_init,
962 .exit = genl_pernet_exit,
963};
964
Thomas Graf482a8522005-11-10 02:25:56 +0100965static int __init genl_init(void)
966{
967 int i, err;
968
969 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
970 INIT_LIST_HEAD(&family_ht[i]);
971
Johannes Bergc53ed742013-11-19 15:19:31 +0100972 err = genl_register_family_with_ops(&genl_ctrl, genl_ctrl_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100973 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000974 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100975
Johannes Berg134e6372009-07-10 09:51:34 +0000976 err = register_pernet_subsys(&genl_pernet_ops);
977 if (err)
978 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100979
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700980 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
981 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000982 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700983
Thomas Graf482a8522005-11-10 02:25:56 +0100984 return 0;
985
Johannes Berg134e6372009-07-10 09:51:34 +0000986problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100987 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100988}
989
990subsys_initcall(genl_init);
991
Eric W. Biederman15e47302012-09-07 20:12:54 +0000992static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
Johannes Berg134e6372009-07-10 09:51:34 +0000993 gfp_t flags)
994{
995 struct sk_buff *tmp;
996 struct net *net, *prev = NULL;
997 int err;
998
999 for_each_net_rcu(net) {
1000 if (prev) {
1001 tmp = skb_clone(skb, flags);
1002 if (!tmp) {
1003 err = -ENOMEM;
1004 goto error;
1005 }
1006 err = nlmsg_multicast(prev->genl_sock, tmp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001007 portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001008 if (err)
1009 goto error;
1010 }
1011
1012 prev = net;
1013 }
1014
Eric W. Biederman15e47302012-09-07 20:12:54 +00001015 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001016 error:
1017 kfree_skb(skb);
1018 return err;
1019}
1020
Eric W. Biederman15e47302012-09-07 20:12:54 +00001021int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
Johannes Berg134e6372009-07-10 09:51:34 +00001022 gfp_t flags)
1023{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001024 return genlmsg_mcast(skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001025}
1026EXPORT_SYMBOL(genlmsg_multicast_allns);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001027
Eric W. Biederman15e47302012-09-07 20:12:54 +00001028void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
Pravin B Shelar263ba612011-11-10 19:14:37 -08001029 struct nlmsghdr *nlh, gfp_t flags)
1030{
1031 struct sock *sk = net->genl_sock;
1032 int report = 0;
1033
1034 if (nlh)
1035 report = nlmsg_report(nlh);
1036
Eric W. Biederman15e47302012-09-07 20:12:54 +00001037 nlmsg_notify(sk, skb, portid, group, report, flags);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001038}
1039EXPORT_SYMBOL(genl_notify);