blob: d8273b057763bfc5cdab87e588bf4a9fbc2765d4 [file] [log] [blame]
Thomas Graf482a8522005-11-10 02:25:56 +01001/*
2 * NETLINK Generic Netlink Family
3 *
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
Johannes Berg2dbba6f2007-07-18 15:47:52 -07006 * Johannes Berg <johannes@sipsolutions.net>
Thomas Graf482a8522005-11-10 02:25:56 +01007 */
8
Thomas Graf482a8522005-11-10 02:25:56 +01009#include <linux/module.h>
10#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Thomas Graf482a8522005-11-10 02:25:56 +010012#include <linux/errno.h>
13#include <linux/types.h>
14#include <linux/socket.h>
15#include <linux/string.h>
16#include <linux/skbuff.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080017#include <linux/mutex.h>
Johannes Berg2dbba6f2007-07-18 15:47:52 -070018#include <linux/bitmap.h>
Pravin B Shelardef31172013-04-23 07:48:30 +000019#include <linux/rwsem.h>
Thomas Graf482a8522005-11-10 02:25:56 +010020#include <net/sock.h>
21#include <net/genetlink.h>
22
Ingo Molnar14cc3e22006-03-26 01:37:14 -080023static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
Pravin B Shelardef31172013-04-23 07:48:30 +000024static DECLARE_RWSEM(cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010025
James Chapmanf408e0c2010-04-02 06:19:05 +000026void genl_lock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010027{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080028 mutex_lock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010029}
James Chapmanf408e0c2010-04-02 06:19:05 +000030EXPORT_SYMBOL(genl_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010031
James Chapmanf408e0c2010-04-02 06:19:05 +000032void genl_unlock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010033{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080034 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010035}
James Chapmanf408e0c2010-04-02 06:19:05 +000036EXPORT_SYMBOL(genl_unlock);
Thomas Graf482a8522005-11-10 02:25:56 +010037
WANG Cong320f5ea2012-07-24 13:44:01 +080038#ifdef CONFIG_LOCKDEP
Pravin B Shelar86b13092011-11-10 19:14:51 -080039int lockdep_genl_is_held(void)
40{
41 return lockdep_is_held(&genl_mutex);
42}
43EXPORT_SYMBOL(lockdep_genl_is_held);
44#endif
45
Pravin B Shelardef31172013-04-23 07:48:30 +000046static void genl_lock_all(void)
47{
48 down_write(&cb_lock);
49 genl_lock();
50}
51
52static void genl_unlock_all(void)
53{
54 genl_unlock();
55 up_write(&cb_lock);
56}
57
Thomas Graf482a8522005-11-10 02:25:56 +010058#define GENL_FAM_TAB_SIZE 16
59#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
60
61static struct list_head family_ht[GENL_FAM_TAB_SIZE];
Johannes Berg2dbba6f2007-07-18 15:47:52 -070062/*
63 * Bitmap of multicast groups that are currently in use.
64 *
65 * To avoid an allocation at boot of just one unsigned long,
66 * declare it global instead.
67 * Bit 0 is marked as already used since group 0 is invalid.
68 */
69static unsigned long mc_group_start = 0x1;
70static unsigned long *mc_groups = &mc_group_start;
71static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010072
73static int genl_ctrl_event(int event, void *data);
74
75static inline unsigned int genl_family_hash(unsigned int id)
76{
77 return id & GENL_FAM_TAB_MASK;
78}
79
80static inline struct list_head *genl_family_chain(unsigned int id)
81{
82 return &family_ht[genl_family_hash(id)];
83}
84
85static struct genl_family *genl_family_find_byid(unsigned int id)
86{
87 struct genl_family *f;
88
89 list_for_each_entry(f, genl_family_chain(id), family_list)
90 if (f->id == id)
91 return f;
92
93 return NULL;
94}
95
96static struct genl_family *genl_family_find_byname(char *name)
97{
98 struct genl_family *f;
99 int i;
100
101 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
102 list_for_each_entry(f, genl_family_chain(i), family_list)
103 if (strcmp(f->name, name) == 0)
104 return f;
105
106 return NULL;
107}
108
109static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
110{
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 Bergd91824c2013-11-14 17:14:44 +0100286static int genl_validate_add_ops(struct genl_family *family, struct genl_ops *ops,
287 unsigned int n_ops)
Thomas Graf482a8522005-11-10 02:25:56 +0100288{
Johannes Bergd91824c2013-11-14 17:14:44 +0100289 int i, j;
Thomas Graf482a8522005-11-10 02:25:56 +0100290
Johannes Bergd91824c2013-11-14 17:14:44 +0100291 for (i = 0; i < n_ops; i++) {
292 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
293 return -EINVAL;
294 for (j = i + 1; j < n_ops; j++)
295 if (ops[i].cmd == ops[j].cmd)
296 return -EINVAL;
297 if (ops[i].dumpit)
298 ops[i].flags |= GENL_CMD_CAP_DUMP;
299 if (ops[i].doit)
300 ops[i].flags |= GENL_CMD_CAP_DO;
301 if (ops[i].policy)
302 ops[i].flags |= GENL_CMD_CAP_HASPOL;
Thomas Graf482a8522005-11-10 02:25:56 +0100303 }
304
Johannes Bergd91824c2013-11-14 17:14:44 +0100305 /* family is not registered yet, so no locking needed */
306 family->ops = ops;
307 family->n_ops = n_ops;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800308
Johannes Bergd91824c2013-11-14 17:14:44 +0100309 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100310}
Thomas Graf482a8522005-11-10 02:25:56 +0100311
312/**
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700313 * __genl_register_family - register a generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +0100314 * @family: generic netlink family
315 *
316 * Registers the specified family after validating it first. Only one
317 * family may be registered with the same family name or identifier.
318 * The family id may equal GENL_ID_GENERATE causing an unique id to
319 * be automatically generated and assigned.
320 *
321 * Return 0 on success or a negative error code.
322 */
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700323int __genl_register_family(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100324{
325 int err = -EINVAL;
326
327 if (family->id && family->id < GENL_MIN_ID)
328 goto errout;
329
330 if (family->id > GENL_MAX_ID)
331 goto errout;
332
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700333 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100334
Pravin B Shelardef31172013-04-23 07:48:30 +0000335 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100336
337 if (genl_family_find_byname(family->name)) {
338 err = -EEXIST;
339 goto errout_locked;
340 }
341
Thomas Graf482a8522005-11-10 02:25:56 +0100342 if (family->id == GENL_ID_GENERATE) {
343 u16 newid = genl_generate_id();
344
345 if (!newid) {
346 err = -ENOMEM;
347 goto errout_locked;
348 }
349
350 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000351 } else if (genl_family_find_byid(family->id)) {
352 err = -EEXIST;
353 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100354 }
355
Pravin B Shelardef31172013-04-23 07:48:30 +0000356 if (family->maxattr && !family->parallel_ops) {
Thomas Graf482a8522005-11-10 02:25:56 +0100357 family->attrbuf = kmalloc((family->maxattr+1) *
358 sizeof(struct nlattr *), GFP_KERNEL);
359 if (family->attrbuf == NULL) {
360 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800361 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100362 }
363 } else
364 family->attrbuf = NULL;
365
366 list_add_tail(&family->family_list, genl_family_chain(family->id));
Pravin B Shelardef31172013-04-23 07:48:30 +0000367 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100368
369 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
370
371 return 0;
372
373errout_locked:
Pravin B Shelardef31172013-04-23 07:48:30 +0000374 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100375errout:
376 return err;
377}
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700378EXPORT_SYMBOL(__genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100379
380/**
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700381 * __genl_register_family_with_ops - register a generic netlink family
Michał Mirosława7b11d72009-05-21 10:34:04 +0000382 * @family: generic netlink family
383 * @ops: operations to be registered
384 * @n_ops: number of elements to register
385 *
386 * Registers the specified family and operations from the specified table.
387 * Only one family may be registered with the same family name or identifier.
388 *
389 * The family id may equal GENL_ID_GENERATE causing an unique id to
390 * be automatically generated and assigned.
391 *
392 * Either a doit or dumpit callback must be specified for every registered
393 * operation or the function will fail. Only one operation structure per
394 * command identifier may be registered.
395 *
396 * See include/net/genetlink.h for more documenation on the operations
397 * structure.
398 *
Michał Mirosława7b11d72009-05-21 10:34:04 +0000399 * Return 0 on success or a negative error code.
400 */
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700401int __genl_register_family_with_ops(struct genl_family *family,
Michał Mirosława7b11d72009-05-21 10:34:04 +0000402 struct genl_ops *ops, size_t n_ops)
403{
Johannes Bergd91824c2013-11-14 17:14:44 +0100404 int err;
Michał Mirosława7b11d72009-05-21 10:34:04 +0000405
Johannes Bergd91824c2013-11-14 17:14:44 +0100406 err = genl_validate_add_ops(family, ops, n_ops);
Michał Mirosława7b11d72009-05-21 10:34:04 +0000407 if (err)
408 return err;
409
Johannes Bergd91824c2013-11-14 17:14:44 +0100410 return __genl_register_family(family);
Michał Mirosława7b11d72009-05-21 10:34:04 +0000411}
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700412EXPORT_SYMBOL(__genl_register_family_with_ops);
Michał Mirosława7b11d72009-05-21 10:34:04 +0000413
414/**
Thomas Graf482a8522005-11-10 02:25:56 +0100415 * genl_unregister_family - unregister generic netlink family
416 * @family: generic netlink family
417 *
418 * Unregisters the specified family.
419 *
420 * Returns 0 on success or a negative error code.
421 */
422int genl_unregister_family(struct genl_family *family)
423{
424 struct genl_family *rc;
425
Pravin B Shelardef31172013-04-23 07:48:30 +0000426 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100427
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800428 genl_unregister_mc_groups(family);
429
Thomas Graf482a8522005-11-10 02:25:56 +0100430 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
431 if (family->id != rc->id || strcmp(rc->name, family->name))
432 continue;
433
434 list_del(&rc->family_list);
Johannes Bergd91824c2013-11-14 17:14:44 +0100435 family->n_ops = 0;
Pravin B Shelardef31172013-04-23 07:48:30 +0000436 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100437
Thomas Graf482a8522005-11-10 02:25:56 +0100438 kfree(family->attrbuf);
439 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
440 return 0;
441 }
442
Pravin B Shelardef31172013-04-23 07:48:30 +0000443 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100444
445 return -ENOENT;
446}
Changli Gao416c2f92010-07-25 20:46:01 +0000447EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100448
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500449/**
450 * genlmsg_put - Add generic netlink header to netlink message
451 * @skb: socket buffer holding the message
Eric W. Biederman15e47302012-09-07 20:12:54 +0000452 * @portid: netlink portid the message is addressed to
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500453 * @seq: sequence number (usually the one of the sender)
454 * @family: generic netlink family
Ben Hutchings2c530402012-07-10 10:55:09 +0000455 * @flags: netlink message flags
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500456 * @cmd: generic netlink command
457 *
458 * Returns pointer to user specific header
459 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000460void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500461 struct genl_family *family, int flags, u8 cmd)
462{
463 struct nlmsghdr *nlh;
464 struct genlmsghdr *hdr;
465
Eric W. Biederman15e47302012-09-07 20:12:54 +0000466 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500467 family->hdrsize, flags);
468 if (nlh == NULL)
469 return NULL;
470
471 hdr = nlmsg_data(nlh);
472 hdr->cmd = cmd;
473 hdr->version = family->version;
474 hdr->reserved = 0;
475
476 return (char *) hdr + GENL_HDRLEN;
477}
478EXPORT_SYMBOL(genlmsg_put);
479
Pravin B Shelar9b963092013-08-23 12:44:55 -0700480static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
481{
482 struct genl_ops *ops = cb->data;
483 int rc;
484
485 genl_lock();
486 rc = ops->dumpit(skb, cb);
487 genl_unlock();
488 return rc;
489}
490
491static int genl_lock_done(struct netlink_callback *cb)
492{
493 struct genl_ops *ops = cb->data;
494 int rc = 0;
495
496 if (ops->done) {
497 genl_lock();
498 rc = ops->done(cb);
499 genl_unlock();
500 }
501 return rc;
502}
503
Pravin B Shelardef31172013-04-23 07:48:30 +0000504static int genl_family_rcv_msg(struct genl_family *family,
505 struct sk_buff *skb,
506 struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100507{
508 struct genl_ops *ops;
Johannes Berg134e6372009-07-10 09:51:34 +0000509 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100510 struct genl_info info;
511 struct genlmsghdr *hdr = nlmsg_data(nlh);
Pravin B Shelardef31172013-04-23 07:48:30 +0000512 struct nlattr **attrbuf;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700513 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100514
Johannes Berg134e6372009-07-10 09:51:34 +0000515 /* this family doesn't exist in this netns */
516 if (!family->netnsok && !net_eq(net, &init_net))
517 return -ENOENT;
518
Thomas Graf482a8522005-11-10 02:25:56 +0100519 hdrlen = GENL_HDRLEN + family->hdrsize;
520 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700521 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100522
523 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700524 if (ops == NULL)
525 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100526
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700527 if ((ops->flags & GENL_ADMIN_PERM) &&
Eric Parisfd778462012-01-03 12:25:16 -0500528 !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700529 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100530
Pablo Neirae1ee3672013-07-29 12:30:04 +0200531 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
Pravin B Shelar9b963092013-08-23 12:44:55 -0700532 int rc;
Pravin B Shelardef31172013-04-23 07:48:30 +0000533
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700534 if (ops->dumpit == NULL)
535 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100536
Pravin B Shelar9b963092013-08-23 12:44:55 -0700537 if (!family->parallel_ops) {
538 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700539 .module = family->module,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700540 .data = ops,
541 .dump = genl_lock_dumpit,
542 .done = genl_lock_done,
543 };
544
545 genl_unlock();
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 genl_lock();
548
549 } else {
550 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700551 .module = family->module,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700552 .dump = ops->dumpit,
553 .done = ops->done,
554 };
555
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700556 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700557 }
558
559 return rc;
Thomas Graf482a8522005-11-10 02:25:56 +0100560 }
561
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700562 if (ops->doit == NULL)
563 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100564
Pravin B Shelardef31172013-04-23 07:48:30 +0000565 if (family->maxattr && family->parallel_ops) {
566 attrbuf = kmalloc((family->maxattr+1) *
567 sizeof(struct nlattr *), GFP_KERNEL);
568 if (attrbuf == NULL)
569 return -ENOMEM;
570 } else
571 attrbuf = family->attrbuf;
572
573 if (attrbuf) {
574 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
Thomas Graf482a8522005-11-10 02:25:56 +0100575 ops->policy);
576 if (err < 0)
Wei Yongjun50754d212013-04-26 15:34:16 +0000577 goto out;
Thomas Graf482a8522005-11-10 02:25:56 +0100578 }
579
580 info.snd_seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000581 info.snd_portid = NETLINK_CB(skb).portid;
Thomas Graf482a8522005-11-10 02:25:56 +0100582 info.nlhdr = nlh;
583 info.genlhdr = nlmsg_data(nlh);
584 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
Pravin B Shelardef31172013-04-23 07:48:30 +0000585 info.attrs = attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000586 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200587 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100588
Johannes Bergff4c92d2010-10-04 21:14:03 +0200589 if (family->pre_doit) {
590 err = family->pre_doit(ops, skb, &info);
591 if (err)
Wei Yongjun50754d212013-04-26 15:34:16 +0000592 goto out;
Johannes Bergff4c92d2010-10-04 21:14:03 +0200593 }
594
595 err = ops->doit(skb, &info);
596
597 if (family->post_doit)
598 family->post_doit(ops, skb, &info);
599
Wei Yongjun50754d212013-04-26 15:34:16 +0000600out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000601 if (family->parallel_ops)
602 kfree(attrbuf);
603
604 return err;
605}
606
607static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
608{
609 struct genl_family *family;
610 int err;
611
612 family = genl_family_find_byid(nlh->nlmsg_type);
613 if (family == NULL)
614 return -ENOENT;
615
616 if (!family->parallel_ops)
617 genl_lock();
618
619 err = genl_family_rcv_msg(family, skb, nlh);
620
621 if (!family->parallel_ops)
622 genl_unlock();
623
Johannes Bergff4c92d2010-10-04 21:14:03 +0200624 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100625}
626
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700627static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100628{
Pravin B Shelardef31172013-04-23 07:48:30 +0000629 down_read(&cb_lock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700630 netlink_rcv_skb(skb, &genl_rcv_msg);
Pravin B Shelardef31172013-04-23 07:48:30 +0000631 up_read(&cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +0100632}
633
634/**************************************************************************
635 * Controller
636 **************************************************************************/
637
Thomas Graf17c157c2006-11-14 19:46:02 -0800638static struct genl_family genl_ctrl = {
639 .id = GENL_ID_CTRL,
640 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800641 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800642 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000643 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800644};
645
Eric W. Biederman15e47302012-09-07 20:12:54 +0000646static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
Thomas Graf482a8522005-11-10 02:25:56 +0100647 u32 flags, struct sk_buff *skb, u8 cmd)
648{
649 void *hdr;
650
Eric W. Biederman15e47302012-09-07 20:12:54 +0000651 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100652 if (hdr == NULL)
653 return -1;
654
David S. Miller444653f2012-03-29 23:25:11 -0400655 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
656 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
657 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
658 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
659 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
660 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700661
Johannes Bergd91824c2013-11-14 17:14:44 +0100662 if (family->n_ops) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800663 struct nlattr *nla_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100664 int i;
Thomas Grafeb328112006-09-18 00:01:59 -0700665
Thomas Grafe94ef682006-11-23 11:44:37 -0800666 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
667 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700668 goto nla_put_failure;
669
Johannes Bergd91824c2013-11-14 17:14:44 +0100670 for (i = 0; i < family->n_ops; i++) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800671 struct nlattr *nest;
Johannes Bergd91824c2013-11-14 17:14:44 +0100672 struct genl_ops *ops = &family->ops[i];
Thomas Grafeb328112006-09-18 00:01:59 -0700673
Johannes Bergd91824c2013-11-14 17:14:44 +0100674 nest = nla_nest_start(skb, i + 1);
Thomas Grafe94ef682006-11-23 11:44:37 -0800675 if (nest == NULL)
676 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700677
David S. Miller444653f2012-03-29 23:25:11 -0400678 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
679 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, ops->flags))
680 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700681
Thomas Grafe94ef682006-11-23 11:44:37 -0800682 nla_nest_end(skb, nest);
683 }
684
685 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700686 }
687
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700688 if (!list_empty(&family->mcast_groups)) {
689 struct genl_multicast_group *grp;
690 struct nlattr *nla_grps;
691 int idx = 1;
692
693 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
694 if (nla_grps == NULL)
695 goto nla_put_failure;
696
697 list_for_each_entry(grp, &family->mcast_groups, list) {
698 struct nlattr *nest;
699
700 nest = nla_nest_start(skb, idx++);
701 if (nest == NULL)
702 goto nla_put_failure;
703
David S. Miller444653f2012-03-29 23:25:11 -0400704 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
705 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
706 grp->name))
707 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700708
709 nla_nest_end(skb, nest);
710 }
711 nla_nest_end(skb, nla_grps);
712 }
713
714 return genlmsg_end(skb, hdr);
715
716nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700717 genlmsg_cancel(skb, hdr);
718 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700719}
720
Eric W. Biederman15e47302012-09-07 20:12:54 +0000721static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700722 u32 seq, u32 flags, struct sk_buff *skb,
723 u8 cmd)
724{
725 void *hdr;
726 struct nlattr *nla_grps;
727 struct nlattr *nest;
728
Eric W. Biederman15e47302012-09-07 20:12:54 +0000729 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700730 if (hdr == NULL)
731 return -1;
732
David S. Miller444653f2012-03-29 23:25:11 -0400733 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
734 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
735 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700736
737 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
738 if (nla_grps == NULL)
739 goto nla_put_failure;
740
741 nest = nla_nest_start(skb, 1);
742 if (nest == NULL)
743 goto nla_put_failure;
744
David S. Miller444653f2012-03-29 23:25:11 -0400745 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
746 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
747 grp->name))
748 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700749
750 nla_nest_end(skb, nest);
751 nla_nest_end(skb, nla_grps);
752
Thomas Graf482a8522005-11-10 02:25:56 +0100753 return genlmsg_end(skb, hdr);
754
755nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700756 genlmsg_cancel(skb, hdr);
757 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100758}
759
760static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
761{
762
763 int i, n = 0;
764 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000765 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100766 int chains_to_skip = cb->args[0];
767 int fams_to_skip = cb->args[1];
768
Samir Bellabese1d5a012010-01-07 22:10:56 +0000769 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100770 n = 0;
771 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000772 if (!rt->netnsok && !net_eq(net, &init_net))
773 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100774 if (++n < fams_to_skip)
775 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000776 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
Thomas Graf482a8522005-11-10 02:25:56 +0100777 cb->nlh->nlmsg_seq, NLM_F_MULTI,
778 skb, CTRL_CMD_NEWFAMILY) < 0)
779 goto errout;
780 }
781
782 fams_to_skip = 0;
783 }
784
785errout:
786 cb->args[0] = i;
787 cb->args[1] = n;
788
789 return skb->len;
790}
791
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700792static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000793 u32 portid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100794{
795 struct sk_buff *skb;
796 int err;
797
Thomas Graf339bf982006-11-10 14:10:15 -0800798 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100799 if (skb == NULL)
800 return ERR_PTR(-ENOBUFS);
801
Eric W. Biederman15e47302012-09-07 20:12:54 +0000802 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100803 if (err < 0) {
804 nlmsg_free(skb);
805 return ERR_PTR(err);
806 }
807
808 return skb;
809}
810
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700811static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000812 u32 portid, int seq, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700813{
814 struct sk_buff *skb;
815 int err;
816
817 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
818 if (skb == NULL)
819 return ERR_PTR(-ENOBUFS);
820
Eric W. Biederman15e47302012-09-07 20:12:54 +0000821 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700822 if (err < 0) {
823 nlmsg_free(skb);
824 return ERR_PTR(err);
825 }
826
827 return skb;
828}
829
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700830static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100831 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700832 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
833 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100834};
835
836static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
837{
838 struct sk_buff *msg;
839 struct genl_family *res = NULL;
840 int err = -EINVAL;
841
842 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
843 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
844 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000845 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100846 }
847
848 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700849 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100850
Thomas Graf5176f912006-08-26 20:13:18 -0700851 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100852 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500853#ifdef CONFIG_MODULES
854 if (res == NULL) {
855 genl_unlock();
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200856 up_read(&cb_lock);
Neil Hormane9412c32012-05-29 09:30:41 +0000857 request_module("net-pf-%d-proto-%d-family-%s",
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500858 PF_NETLINK, NETLINK_GENERIC, name);
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200859 down_read(&cb_lock);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500860 genl_lock();
861 res = genl_family_find_byname(name);
862 }
863#endif
Johannes Berg134e6372009-07-10 09:51:34 +0000864 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100865 }
866
Johannes Berg134e6372009-07-10 09:51:34 +0000867 if (res == NULL)
868 return err;
869
870 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
871 /* family doesn't exist here */
872 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100873 }
874
Eric W. Biederman15e47302012-09-07 20:12:54 +0000875 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700876 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000877 if (IS_ERR(msg))
878 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100879
Johannes Berg134e6372009-07-10 09:51:34 +0000880 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100881}
882
883static int genl_ctrl_event(int event, void *data)
884{
885 struct sk_buff *msg;
Johannes Berg134e6372009-07-10 09:51:34 +0000886 struct genl_family *family;
887 struct genl_multicast_group *grp;
Thomas Graf482a8522005-11-10 02:25:56 +0100888
Johannes Berg134e6372009-07-10 09:51:34 +0000889 /* genl is still initialising */
890 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100891 return 0;
892
893 switch (event) {
894 case CTRL_CMD_NEWFAMILY:
895 case CTRL_CMD_DELFAMILY:
Johannes Berg134e6372009-07-10 09:51:34 +0000896 family = data;
897 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700898 break;
899 case CTRL_CMD_NEWMCAST_GRP:
900 case CTRL_CMD_DELMCAST_GRP:
Johannes Berg134e6372009-07-10 09:51:34 +0000901 grp = data;
902 family = grp->family;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700903 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100904 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000905 default:
906 return -EINVAL;
907 }
908
909 if (IS_ERR(msg))
910 return PTR_ERR(msg);
911
912 if (!family->netnsok) {
913 genlmsg_multicast_netns(&init_net, msg, 0,
914 GENL_ID_CTRL, GFP_KERNEL);
915 } else {
916 rcu_read_lock();
917 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
918 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100919 }
920
921 return 0;
922}
923
924static struct genl_ops genl_ctrl_ops = {
925 .cmd = CTRL_CMD_GETFAMILY,
926 .doit = ctrl_getfamily,
927 .dumpit = ctrl_dumpfamily,
928 .policy = ctrl_policy,
929};
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
Changli Gao652c6712010-07-25 23:21:05 +0000972 err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
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);