blob: 353909d46dda86d810bb00248de49bdd1ab0acbd [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 Berg2dbba6f2007-07-18 15:47:52 -070072 */
Johannes Berge5dcecb2013-11-19 15:19:32 +010073static unsigned long mc_group_start = 0x3;
Johannes Berg2dbba6f2007-07-18 15:47:52 -070074static unsigned long *mc_groups = &mc_group_start;
75static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010076
77static int genl_ctrl_event(int event, void *data);
78
79static inline unsigned int genl_family_hash(unsigned int id)
80{
81 return id & GENL_FAM_TAB_MASK;
82}
83
84static inline struct list_head *genl_family_chain(unsigned int id)
85{
86 return &family_ht[genl_family_hash(id)];
87}
88
89static struct genl_family *genl_family_find_byid(unsigned int id)
90{
91 struct genl_family *f;
92
93 list_for_each_entry(f, genl_family_chain(id), family_list)
94 if (f->id == id)
95 return f;
96
97 return NULL;
98}
99
100static struct genl_family *genl_family_find_byname(char *name)
101{
102 struct genl_family *f;
103 int i;
104
105 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
106 list_for_each_entry(f, genl_family_chain(i), family_list)
107 if (strcmp(f->name, name) == 0)
108 return f;
109
110 return NULL;
111}
112
Johannes Bergf84f7712013-11-14 17:14:45 +0100113static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100114{
Johannes Bergd91824c2013-11-14 17:14:44 +0100115 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100116
Johannes Bergd91824c2013-11-14 17:14:44 +0100117 for (i = 0; i < family->n_ops; i++)
118 if (family->ops[i].cmd == cmd)
119 return &family->ops[i];
Thomas Graf482a8522005-11-10 02:25:56 +0100120
121 return NULL;
122}
123
124/* Of course we are going to have problems once we hit
125 * 2^16 alive types, but that can only happen by year 2K
126*/
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000127static u16 genl_generate_id(void)
Thomas Graf482a8522005-11-10 02:25:56 +0100128{
Krishna Kumar988ade62009-10-14 19:55:07 +0000129 static u16 id_gen_idx = GENL_MIN_ID;
130 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100131
Krishna Kumar988ade62009-10-14 19:55:07 +0000132 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
133 if (!genl_family_find_byid(id_gen_idx))
134 return id_gen_idx;
135 if (++id_gen_idx > GENL_MAX_ID)
Thomas Graf482a8522005-11-10 02:25:56 +0100136 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000137 }
Thomas Graf482a8522005-11-10 02:25:56 +0100138
Krishna Kumar988ade62009-10-14 19:55:07 +0000139 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100140}
141
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700142static struct genl_multicast_group notify_grp;
143
144/**
145 * genl_register_mc_group - register a multicast group
146 *
147 * Registers the specified multicast group and notifies userspace
148 * about the new group.
149 *
150 * Returns 0 on success or a negative error code.
151 *
152 * @family: The generic netlink family the group shall be registered for.
153 * @grp: The group to register, must have a name.
154 */
155int genl_register_mc_group(struct genl_family *family,
156 struct genl_multicast_group *grp)
157{
158 int id;
159 unsigned long *new_groups;
Brian Haleyb1f57192009-09-04 20:36:52 -0700160 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700161
162 BUG_ON(grp->name[0] == '\0');
Masatake YAMATOf1e79e22013-03-19 01:47:27 +0000163 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700164
Pravin B Shelardef31172013-04-23 07:48:30 +0000165 genl_lock_all();
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700166
Johannes Berge5dcecb2013-11-19 15:19:32 +0100167 /* special-case our own group and hacks */
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700168 if (grp == &notify_grp)
169 id = GENL_ID_CTRL;
Johannes Berge5dcecb2013-11-19 15:19:32 +0100170 else if (strcmp(family->name, "NET_DM") == 0)
171 id = 1;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700172 else
173 id = find_first_zero_bit(mc_groups,
174 mc_groups_longs * BITS_PER_LONG);
175
176
177 if (id >= mc_groups_longs * BITS_PER_LONG) {
178 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
179
180 if (mc_groups == &mc_group_start) {
181 new_groups = kzalloc(nlen, GFP_KERNEL);
182 if (!new_groups) {
183 err = -ENOMEM;
184 goto out;
185 }
186 mc_groups = new_groups;
187 *mc_groups = mc_group_start;
188 } else {
189 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
190 if (!new_groups) {
191 err = -ENOMEM;
192 goto out;
193 }
194 mc_groups = new_groups;
195 mc_groups[mc_groups_longs] = 0;
196 }
197 mc_groups_longs++;
198 }
199
Johannes Berg134e6372009-07-10 09:51:34 +0000200 if (family->netnsok) {
201 struct net *net;
202
Johannes Bergd136f1b2009-09-12 03:03:15 +0000203 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000204 rcu_read_lock();
205 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000206 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000207 mc_groups_longs * BITS_PER_LONG);
208 if (err) {
209 /*
210 * No need to roll back, can only fail if
211 * memory allocation fails and then the
212 * number of _possible_ groups has been
213 * increased on some sockets which is ok.
214 */
215 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000216 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000217 goto out;
218 }
219 }
220 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000221 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000222 } else {
223 err = netlink_change_ngroups(init_net.genl_sock,
224 mc_groups_longs * BITS_PER_LONG);
225 if (err)
226 goto out;
227 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700228
229 grp->id = id;
230 set_bit(id, mc_groups);
231 list_add_tail(&grp->list, &family->mcast_groups);
232 grp->family = family;
233
234 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
235 out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000236 genl_unlock_all();
Thomas Graf79d310d2007-07-24 15:34:53 -0700237 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700238}
239EXPORT_SYMBOL(genl_register_mc_group);
240
Thomas Graf79dc43862007-07-24 15:32:46 -0700241static void __genl_unregister_mc_group(struct genl_family *family,
242 struct genl_multicast_group *grp)
243{
Johannes Berg134e6372009-07-10 09:51:34 +0000244 struct net *net;
Thomas Graf79dc43862007-07-24 15:32:46 -0700245 BUG_ON(grp->family != family);
Johannes Berg134e6372009-07-10 09:51:34 +0000246
Johannes Bergb8273572009-09-24 15:44:05 -0700247 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000248 rcu_read_lock();
249 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700250 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000251 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700252 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000253
Johannes Berge5dcecb2013-11-19 15:19:32 +0100254 if (grp->id != 1)
255 clear_bit(grp->id, mc_groups);
Thomas Graf79dc43862007-07-24 15:32:46 -0700256 list_del(&grp->list);
257 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
258 grp->id = 0;
259 grp->family = NULL;
260}
261
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700262/**
263 * genl_unregister_mc_group - unregister a multicast group
264 *
265 * Unregisters the specified multicast group and notifies userspace
266 * about it. All current listeners on the group are removed.
267 *
268 * Note: It is not necessary to unregister all multicast groups before
269 * unregistering the family, unregistering the family will cause
270 * all assigned multicast groups to be unregistered automatically.
271 *
272 * @family: Generic netlink family the group belongs to.
273 * @grp: The group to unregister, must have been registered successfully
274 * previously.
275 */
276void genl_unregister_mc_group(struct genl_family *family,
277 struct genl_multicast_group *grp)
278{
Pravin B Shelardef31172013-04-23 07:48:30 +0000279 genl_lock_all();
Thomas Graf79dc43862007-07-24 15:32:46 -0700280 __genl_unregister_mc_group(family, grp);
Pravin B Shelardef31172013-04-23 07:48:30 +0000281 genl_unlock_all();
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700282}
Inaky Perez-Gonzalez3efb40c2008-12-20 16:57:37 -0800283EXPORT_SYMBOL(genl_unregister_mc_group);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700284
285static void genl_unregister_mc_groups(struct genl_family *family)
286{
287 struct genl_multicast_group *grp, *tmp;
288
289 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700290 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700291}
292
Johannes Berg568508a2013-11-15 14:19:08 +0100293static int genl_validate_ops(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100294{
Johannes Berg568508a2013-11-15 14:19:08 +0100295 const struct genl_ops *ops = family->ops;
296 unsigned int n_ops = family->n_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100297 int i, j;
Thomas Graf482a8522005-11-10 02:25:56 +0100298
Johannes Berg568508a2013-11-15 14:19:08 +0100299 if (WARN_ON(n_ops && !ops))
300 return -EINVAL;
301
302 if (!n_ops)
303 return 0;
304
Johannes Bergd91824c2013-11-14 17:14:44 +0100305 for (i = 0; i < n_ops; i++) {
306 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
307 return -EINVAL;
308 for (j = i + 1; j < n_ops; j++)
309 if (ops[i].cmd == ops[j].cmd)
310 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100311 }
312
Johannes Bergd91824c2013-11-14 17:14:44 +0100313 /* family is not registered yet, so no locking needed */
314 family->ops = ops;
315 family->n_ops = n_ops;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800316
Johannes Bergd91824c2013-11-14 17:14:44 +0100317 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100318}
Thomas Graf482a8522005-11-10 02:25:56 +0100319
320/**
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700321 * __genl_register_family - register a generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +0100322 * @family: generic netlink family
323 *
324 * Registers the specified family after validating it first. Only one
325 * family may be registered with the same family name or identifier.
326 * The family id may equal GENL_ID_GENERATE causing an unique id to
327 * be automatically generated and assigned.
328 *
Johannes Berg568508a2013-11-15 14:19:08 +0100329 * The family's ops array must already be assigned, you can use the
330 * genl_register_family_with_ops() helper function.
331 *
Thomas Graf482a8522005-11-10 02:25:56 +0100332 * Return 0 on success or a negative error code.
333 */
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700334int __genl_register_family(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100335{
336 int err = -EINVAL;
337
338 if (family->id && family->id < GENL_MIN_ID)
339 goto errout;
340
341 if (family->id > GENL_MAX_ID)
342 goto errout;
343
Johannes Berg568508a2013-11-15 14:19:08 +0100344 err = genl_validate_ops(family);
345 if (err)
346 return err;
347
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700348 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100349
Pravin B Shelardef31172013-04-23 07:48:30 +0000350 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100351
352 if (genl_family_find_byname(family->name)) {
353 err = -EEXIST;
354 goto errout_locked;
355 }
356
Thomas Graf482a8522005-11-10 02:25:56 +0100357 if (family->id == GENL_ID_GENERATE) {
358 u16 newid = genl_generate_id();
359
360 if (!newid) {
361 err = -ENOMEM;
362 goto errout_locked;
363 }
364
365 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000366 } else if (genl_family_find_byid(family->id)) {
367 err = -EEXIST;
368 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100369 }
370
Pravin B Shelardef31172013-04-23 07:48:30 +0000371 if (family->maxattr && !family->parallel_ops) {
Thomas Graf482a8522005-11-10 02:25:56 +0100372 family->attrbuf = kmalloc((family->maxattr+1) *
373 sizeof(struct nlattr *), GFP_KERNEL);
374 if (family->attrbuf == NULL) {
375 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800376 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100377 }
378 } else
379 family->attrbuf = NULL;
380
381 list_add_tail(&family->family_list, genl_family_chain(family->id));
Pravin B Shelardef31172013-04-23 07:48:30 +0000382 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100383
384 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
385
386 return 0;
387
388errout_locked:
Pravin B Shelardef31172013-04-23 07:48:30 +0000389 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100390errout:
391 return err;
392}
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700393EXPORT_SYMBOL(__genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100394
395/**
396 * genl_unregister_family - unregister generic netlink family
397 * @family: generic netlink family
398 *
399 * Unregisters the specified family.
400 *
401 * Returns 0 on success or a negative error code.
402 */
403int genl_unregister_family(struct genl_family *family)
404{
405 struct genl_family *rc;
406
Pravin B Shelardef31172013-04-23 07:48:30 +0000407 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100408
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800409 genl_unregister_mc_groups(family);
410
Thomas Graf482a8522005-11-10 02:25:56 +0100411 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
412 if (family->id != rc->id || strcmp(rc->name, family->name))
413 continue;
414
415 list_del(&rc->family_list);
Johannes Bergd91824c2013-11-14 17:14:44 +0100416 family->n_ops = 0;
Pravin B Shelardef31172013-04-23 07:48:30 +0000417 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100418
Thomas Graf482a8522005-11-10 02:25:56 +0100419 kfree(family->attrbuf);
420 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
421 return 0;
422 }
423
Pravin B Shelardef31172013-04-23 07:48:30 +0000424 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100425
426 return -ENOENT;
427}
Changli Gao416c2f92010-07-25 20:46:01 +0000428EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100429
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500430/**
431 * genlmsg_put - Add generic netlink header to netlink message
432 * @skb: socket buffer holding the message
Eric W. Biederman15e47302012-09-07 20:12:54 +0000433 * @portid: netlink portid the message is addressed to
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500434 * @seq: sequence number (usually the one of the sender)
435 * @family: generic netlink family
Ben Hutchings2c530402012-07-10 10:55:09 +0000436 * @flags: netlink message flags
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500437 * @cmd: generic netlink command
438 *
439 * Returns pointer to user specific header
440 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000441void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500442 struct genl_family *family, int flags, u8 cmd)
443{
444 struct nlmsghdr *nlh;
445 struct genlmsghdr *hdr;
446
Eric W. Biederman15e47302012-09-07 20:12:54 +0000447 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500448 family->hdrsize, flags);
449 if (nlh == NULL)
450 return NULL;
451
452 hdr = nlmsg_data(nlh);
453 hdr->cmd = cmd;
454 hdr->version = family->version;
455 hdr->reserved = 0;
456
457 return (char *) hdr + GENL_HDRLEN;
458}
459EXPORT_SYMBOL(genlmsg_put);
460
Pravin B Shelar9b963092013-08-23 12:44:55 -0700461static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
462{
Johannes Bergf84f7712013-11-14 17:14:45 +0100463 /* our ops are always const - netlink API doesn't propagate that */
464 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700465 int rc;
466
467 genl_lock();
468 rc = ops->dumpit(skb, cb);
469 genl_unlock();
470 return rc;
471}
472
473static int genl_lock_done(struct netlink_callback *cb)
474{
Johannes Bergf84f7712013-11-14 17:14:45 +0100475 /* our ops are always const - netlink API doesn't propagate that */
476 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700477 int rc = 0;
478
479 if (ops->done) {
480 genl_lock();
481 rc = ops->done(cb);
482 genl_unlock();
483 }
484 return rc;
485}
486
Pravin B Shelardef31172013-04-23 07:48:30 +0000487static int genl_family_rcv_msg(struct genl_family *family,
488 struct sk_buff *skb,
489 struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100490{
Johannes Bergf84f7712013-11-14 17:14:45 +0100491 const struct genl_ops *ops;
Johannes Berg134e6372009-07-10 09:51:34 +0000492 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100493 struct genl_info info;
494 struct genlmsghdr *hdr = nlmsg_data(nlh);
Pravin B Shelardef31172013-04-23 07:48:30 +0000495 struct nlattr **attrbuf;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700496 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100497
Johannes Berg134e6372009-07-10 09:51:34 +0000498 /* this family doesn't exist in this netns */
499 if (!family->netnsok && !net_eq(net, &init_net))
500 return -ENOENT;
501
Thomas Graf482a8522005-11-10 02:25:56 +0100502 hdrlen = GENL_HDRLEN + family->hdrsize;
503 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700504 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100505
506 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700507 if (ops == NULL)
508 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100509
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700510 if ((ops->flags & GENL_ADMIN_PERM) &&
Eric Parisfd778462012-01-03 12:25:16 -0500511 !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700512 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100513
Pablo Neirae1ee3672013-07-29 12:30:04 +0200514 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
Pravin B Shelar9b963092013-08-23 12:44:55 -0700515 int rc;
Pravin B Shelardef31172013-04-23 07:48:30 +0000516
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700517 if (ops->dumpit == NULL)
518 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100519
Pravin B Shelar9b963092013-08-23 12:44:55 -0700520 if (!family->parallel_ops) {
521 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700522 .module = family->module,
Johannes Bergf84f7712013-11-14 17:14:45 +0100523 /* we have const, but the netlink API doesn't */
524 .data = (void *)ops,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700525 .dump = genl_lock_dumpit,
526 .done = genl_lock_done,
527 };
528
529 genl_unlock();
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700530 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700531 genl_lock();
532
533 } else {
534 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700535 .module = family->module,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700536 .dump = ops->dumpit,
537 .done = ops->done,
538 };
539
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700540 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700541 }
542
543 return rc;
Thomas Graf482a8522005-11-10 02:25:56 +0100544 }
545
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700546 if (ops->doit == NULL)
547 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100548
Pravin B Shelardef31172013-04-23 07:48:30 +0000549 if (family->maxattr && family->parallel_ops) {
550 attrbuf = kmalloc((family->maxattr+1) *
551 sizeof(struct nlattr *), GFP_KERNEL);
552 if (attrbuf == NULL)
553 return -ENOMEM;
554 } else
555 attrbuf = family->attrbuf;
556
557 if (attrbuf) {
558 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
Thomas Graf482a8522005-11-10 02:25:56 +0100559 ops->policy);
560 if (err < 0)
Wei Yongjun50754d212013-04-26 15:34:16 +0000561 goto out;
Thomas Graf482a8522005-11-10 02:25:56 +0100562 }
563
564 info.snd_seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000565 info.snd_portid = NETLINK_CB(skb).portid;
Thomas Graf482a8522005-11-10 02:25:56 +0100566 info.nlhdr = nlh;
567 info.genlhdr = nlmsg_data(nlh);
568 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
Pravin B Shelardef31172013-04-23 07:48:30 +0000569 info.attrs = attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000570 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200571 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100572
Johannes Bergff4c92d2010-10-04 21:14:03 +0200573 if (family->pre_doit) {
574 err = family->pre_doit(ops, skb, &info);
575 if (err)
Wei Yongjun50754d212013-04-26 15:34:16 +0000576 goto out;
Johannes Bergff4c92d2010-10-04 21:14:03 +0200577 }
578
579 err = ops->doit(skb, &info);
580
581 if (family->post_doit)
582 family->post_doit(ops, skb, &info);
583
Wei Yongjun50754d212013-04-26 15:34:16 +0000584out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000585 if (family->parallel_ops)
586 kfree(attrbuf);
587
588 return err;
589}
590
591static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
592{
593 struct genl_family *family;
594 int err;
595
596 family = genl_family_find_byid(nlh->nlmsg_type);
597 if (family == NULL)
598 return -ENOENT;
599
600 if (!family->parallel_ops)
601 genl_lock();
602
603 err = genl_family_rcv_msg(family, skb, nlh);
604
605 if (!family->parallel_ops)
606 genl_unlock();
607
Johannes Bergff4c92d2010-10-04 21:14:03 +0200608 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100609}
610
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700611static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100612{
Pravin B Shelardef31172013-04-23 07:48:30 +0000613 down_read(&cb_lock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700614 netlink_rcv_skb(skb, &genl_rcv_msg);
Pravin B Shelardef31172013-04-23 07:48:30 +0000615 up_read(&cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +0100616}
617
618/**************************************************************************
619 * Controller
620 **************************************************************************/
621
Thomas Graf17c157c2006-11-14 19:46:02 -0800622static struct genl_family genl_ctrl = {
623 .id = GENL_ID_CTRL,
624 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800625 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800626 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000627 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800628};
629
Eric W. Biederman15e47302012-09-07 20:12:54 +0000630static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
Thomas Graf482a8522005-11-10 02:25:56 +0100631 u32 flags, struct sk_buff *skb, u8 cmd)
632{
633 void *hdr;
634
Eric W. Biederman15e47302012-09-07 20:12:54 +0000635 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100636 if (hdr == NULL)
637 return -1;
638
David S. Miller444653f2012-03-29 23:25:11 -0400639 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
640 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
641 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
642 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
643 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
644 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700645
Johannes Bergd91824c2013-11-14 17:14:44 +0100646 if (family->n_ops) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800647 struct nlattr *nla_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100648 int i;
Thomas Grafeb328112006-09-18 00:01:59 -0700649
Thomas Grafe94ef682006-11-23 11:44:37 -0800650 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
651 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700652 goto nla_put_failure;
653
Johannes Bergd91824c2013-11-14 17:14:44 +0100654 for (i = 0; i < family->n_ops; i++) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800655 struct nlattr *nest;
Johannes Bergf84f7712013-11-14 17:14:45 +0100656 const struct genl_ops *ops = &family->ops[i];
Johannes Berg029b2342013-11-18 20:54:58 +0100657 u32 op_flags = ops->flags;
Johannes Bergf84f7712013-11-14 17:14:45 +0100658
659 if (ops->dumpit)
Johannes Berg029b2342013-11-18 20:54:58 +0100660 op_flags |= GENL_CMD_CAP_DUMP;
Johannes Bergf84f7712013-11-14 17:14:45 +0100661 if (ops->doit)
Johannes Berg029b2342013-11-18 20:54:58 +0100662 op_flags |= GENL_CMD_CAP_DO;
Johannes Bergf84f7712013-11-14 17:14:45 +0100663 if (ops->policy)
Johannes Berg029b2342013-11-18 20:54:58 +0100664 op_flags |= GENL_CMD_CAP_HASPOL;
Thomas Grafeb328112006-09-18 00:01:59 -0700665
Johannes Bergd91824c2013-11-14 17:14:44 +0100666 nest = nla_nest_start(skb, i + 1);
Thomas Grafe94ef682006-11-23 11:44:37 -0800667 if (nest == NULL)
668 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700669
David S. Miller444653f2012-03-29 23:25:11 -0400670 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
Johannes Berg029b2342013-11-18 20:54:58 +0100671 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
David S. Miller444653f2012-03-29 23:25:11 -0400672 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700673
Thomas Grafe94ef682006-11-23 11:44:37 -0800674 nla_nest_end(skb, nest);
675 }
676
677 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700678 }
679
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700680 if (!list_empty(&family->mcast_groups)) {
681 struct genl_multicast_group *grp;
682 struct nlattr *nla_grps;
683 int idx = 1;
684
685 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
686 if (nla_grps == NULL)
687 goto nla_put_failure;
688
689 list_for_each_entry(grp, &family->mcast_groups, list) {
690 struct nlattr *nest;
691
692 nest = nla_nest_start(skb, idx++);
693 if (nest == NULL)
694 goto nla_put_failure;
695
David S. Miller444653f2012-03-29 23:25:11 -0400696 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
697 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
698 grp->name))
699 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700700
701 nla_nest_end(skb, nest);
702 }
703 nla_nest_end(skb, nla_grps);
704 }
705
706 return genlmsg_end(skb, hdr);
707
708nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700709 genlmsg_cancel(skb, hdr);
710 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700711}
712
Eric W. Biederman15e47302012-09-07 20:12:54 +0000713static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700714 u32 seq, u32 flags, struct sk_buff *skb,
715 u8 cmd)
716{
717 void *hdr;
718 struct nlattr *nla_grps;
719 struct nlattr *nest;
720
Eric W. Biederman15e47302012-09-07 20:12:54 +0000721 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700722 if (hdr == NULL)
723 return -1;
724
David S. Miller444653f2012-03-29 23:25:11 -0400725 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
726 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
727 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700728
729 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
730 if (nla_grps == NULL)
731 goto nla_put_failure;
732
733 nest = nla_nest_start(skb, 1);
734 if (nest == NULL)
735 goto nla_put_failure;
736
David S. Miller444653f2012-03-29 23:25:11 -0400737 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
738 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
739 grp->name))
740 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700741
742 nla_nest_end(skb, nest);
743 nla_nest_end(skb, nla_grps);
744
Thomas Graf482a8522005-11-10 02:25:56 +0100745 return genlmsg_end(skb, hdr);
746
747nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700748 genlmsg_cancel(skb, hdr);
749 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100750}
751
752static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
753{
754
755 int i, n = 0;
756 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000757 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100758 int chains_to_skip = cb->args[0];
759 int fams_to_skip = cb->args[1];
760
Samir Bellabese1d5a012010-01-07 22:10:56 +0000761 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100762 n = 0;
763 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000764 if (!rt->netnsok && !net_eq(net, &init_net))
765 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100766 if (++n < fams_to_skip)
767 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000768 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
Thomas Graf482a8522005-11-10 02:25:56 +0100769 cb->nlh->nlmsg_seq, NLM_F_MULTI,
770 skb, CTRL_CMD_NEWFAMILY) < 0)
771 goto errout;
772 }
773
774 fams_to_skip = 0;
775 }
776
777errout:
778 cb->args[0] = i;
779 cb->args[1] = n;
780
781 return skb->len;
782}
783
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700784static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000785 u32 portid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100786{
787 struct sk_buff *skb;
788 int err;
789
Thomas Graf339bf982006-11-10 14:10:15 -0800790 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100791 if (skb == NULL)
792 return ERR_PTR(-ENOBUFS);
793
Eric W. Biederman15e47302012-09-07 20:12:54 +0000794 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100795 if (err < 0) {
796 nlmsg_free(skb);
797 return ERR_PTR(err);
798 }
799
800 return skb;
801}
802
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700803static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000804 u32 portid, int seq, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700805{
806 struct sk_buff *skb;
807 int err;
808
809 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
810 if (skb == NULL)
811 return ERR_PTR(-ENOBUFS);
812
Eric W. Biederman15e47302012-09-07 20:12:54 +0000813 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700814 if (err < 0) {
815 nlmsg_free(skb);
816 return ERR_PTR(err);
817 }
818
819 return skb;
820}
821
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700822static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100823 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700824 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
825 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100826};
827
828static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
829{
830 struct sk_buff *msg;
831 struct genl_family *res = NULL;
832 int err = -EINVAL;
833
834 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
835 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
836 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000837 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100838 }
839
840 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700841 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100842
Thomas Graf5176f912006-08-26 20:13:18 -0700843 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100844 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500845#ifdef CONFIG_MODULES
846 if (res == NULL) {
847 genl_unlock();
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200848 up_read(&cb_lock);
Neil Hormane9412c32012-05-29 09:30:41 +0000849 request_module("net-pf-%d-proto-%d-family-%s",
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500850 PF_NETLINK, NETLINK_GENERIC, name);
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200851 down_read(&cb_lock);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500852 genl_lock();
853 res = genl_family_find_byname(name);
854 }
855#endif
Johannes Berg134e6372009-07-10 09:51:34 +0000856 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100857 }
858
Johannes Berg134e6372009-07-10 09:51:34 +0000859 if (res == NULL)
860 return err;
861
862 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
863 /* family doesn't exist here */
864 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100865 }
866
Eric W. Biederman15e47302012-09-07 20:12:54 +0000867 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700868 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000869 if (IS_ERR(msg))
870 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100871
Johannes Berg134e6372009-07-10 09:51:34 +0000872 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100873}
874
875static int genl_ctrl_event(int event, void *data)
876{
877 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000878 struct genl_family *family;
879 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100880
Johannes Berg134e6372009-07-10 09:51:34 +0000881 /* genl is still initialising */
882 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100883 return 0;
884
885 switch (event) {
886 case CTRL_CMD_NEWFAMILY:
887 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000888 family = data;
889 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700890 break;
891 case CTRL_CMD_NEWMCAST_GRP:
892 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000893 grp = data;
894 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700895 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100896 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000897 default:
898 return -EINVAL;
899 }
900
901 if (IS_ERR(msg))
902 return PTR_ERR(msg);
903
904 if (!family->netnsok) {
905 genlmsg_multicast_netns(&init_net, msg, 0,
906 GENL_ID_CTRL, GFP_KERNEL);
907 } else {
908 rcu_read_lock();
909 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
910 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100911 }
912
913 return 0;
914}
915
Johannes Bergc53ed742013-11-19 15:19:31 +0100916static struct genl_ops genl_ctrl_ops[] = {
917 {
918 .cmd = CTRL_CMD_GETFAMILY,
919 .doit = ctrl_getfamily,
920 .dumpit = ctrl_dumpfamily,
921 .policy = ctrl_policy,
922 },
Thomas Graf482a8522005-11-10 02:25:56 +0100923};
924
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700925static struct genl_multicast_group notify_grp = {
926 .name = "notify",
927};
928
Johannes Berg134e6372009-07-10 09:51:34 +0000929static int __net_init genl_pernet_init(struct net *net)
930{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000931 struct netlink_kernel_cfg cfg = {
932 .input = genl_rcv,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000933 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000934 };
935
Johannes Berg134e6372009-07-10 09:51:34 +0000936 /* we'll bump the group number right afterwards */
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000937 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
Johannes Berg134e6372009-07-10 09:51:34 +0000938
939 if (!net->genl_sock && net_eq(net, &init_net))
940 panic("GENL: Cannot initialize generic netlink\n");
941
942 if (!net->genl_sock)
943 return -ENOMEM;
944
945 return 0;
946}
947
948static void __net_exit genl_pernet_exit(struct net *net)
949{
950 netlink_kernel_release(net->genl_sock);
951 net->genl_sock = NULL;
952}
953
954static struct pernet_operations genl_pernet_ops = {
955 .init = genl_pernet_init,
956 .exit = genl_pernet_exit,
957};
958
Thomas Graf482a8522005-11-10 02:25:56 +0100959static int __init genl_init(void)
960{
961 int i, err;
962
963 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
964 INIT_LIST_HEAD(&family_ht[i]);
965
Johannes Bergc53ed742013-11-19 15:19:31 +0100966 err = genl_register_family_with_ops(&genl_ctrl, genl_ctrl_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100967 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000968 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100969
Johannes Berg134e6372009-07-10 09:51:34 +0000970 err = register_pernet_subsys(&genl_pernet_ops);
971 if (err)
972 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100973
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700974 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
975 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000976 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700977
Thomas Graf482a8522005-11-10 02:25:56 +0100978 return 0;
979
Johannes Berg134e6372009-07-10 09:51:34 +0000980problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100981 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100982}
983
984subsys_initcall(genl_init);
985
Eric W. Biederman15e47302012-09-07 20:12:54 +0000986static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
Johannes Berg134e6372009-07-10 09:51:34 +0000987 gfp_t flags)
988{
989 struct sk_buff *tmp;
990 struct net *net, *prev = NULL;
991 int err;
992
993 for_each_net_rcu(net) {
994 if (prev) {
995 tmp = skb_clone(skb, flags);
996 if (!tmp) {
997 err = -ENOMEM;
998 goto error;
999 }
1000 err = nlmsg_multicast(prev->genl_sock, tmp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001001 portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001002 if (err)
1003 goto error;
1004 }
1005
1006 prev = net;
1007 }
1008
Eric W. Biederman15e47302012-09-07 20:12:54 +00001009 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001010 error:
1011 kfree_skb(skb);
1012 return err;
1013}
1014
Eric W. Biederman15e47302012-09-07 20:12:54 +00001015int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
Johannes Berg134e6372009-07-10 09:51:34 +00001016 gfp_t flags)
1017{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001018 return genlmsg_mcast(skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001019}
1020EXPORT_SYMBOL(genlmsg_multicast_allns);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001021
Eric W. Biederman15e47302012-09-07 20:12:54 +00001022void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
Pravin B Shelar263ba612011-11-10 19:14:37 -08001023 struct nlmsghdr *nlh, gfp_t flags)
1024{
1025 struct sock *sk = net->genl_sock;
1026 int report = 0;
1027
1028 if (nlh)
1029 report = nlmsg_report(nlh);
1030
Eric W. Biederman15e47302012-09-07 20:12:54 +00001031 nlmsg_notify(sk, skb, portid, group, report, flags);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001032}
1033EXPORT_SYMBOL(genl_notify);