blob: 85bf42e2a9438318a476d429d02d6e4b6c402692 [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 Berg2a94fe42013-11-19 15:19:39 +010072 * Bit 16 is marked as used since it's used for generic netlink
73 * and the code no longer marks pre-reserved IDs as used.
Johannes Berg2ecf7532013-11-19 15:19:33 +010074 * Bit 17 is marked as already used since the VFS quota code
75 * also abused this API and relied on family == group ID, we
76 * cater to that by giving it a static family and group ID.
Johannes Berg2dbba6f2007-07-18 15:47:52 -070077 */
Johannes Berg2a94fe42013-11-19 15:19:39 +010078static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) |
79 BIT(GENL_ID_VFS_DQUOT);
Johannes Berg2dbba6f2007-07-18 15:47:52 -070080static unsigned long *mc_groups = &mc_group_start;
81static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010082
Johannes Bergc2ebb902013-11-19 15:19:36 +010083static int genl_ctrl_event(int event, struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010084 const struct genl_multicast_group *grp,
85 int grp_id);
Thomas Graf482a8522005-11-10 02:25:56 +010086
87static inline unsigned int genl_family_hash(unsigned int id)
88{
89 return id & GENL_FAM_TAB_MASK;
90}
91
92static inline struct list_head *genl_family_chain(unsigned int id)
93{
94 return &family_ht[genl_family_hash(id)];
95}
96
97static struct genl_family *genl_family_find_byid(unsigned int id)
98{
99 struct genl_family *f;
100
101 list_for_each_entry(f, genl_family_chain(id), family_list)
102 if (f->id == id)
103 return f;
104
105 return NULL;
106}
107
108static struct genl_family *genl_family_find_byname(char *name)
109{
110 struct genl_family *f;
111 int i;
112
113 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
114 list_for_each_entry(f, genl_family_chain(i), family_list)
115 if (strcmp(f->name, name) == 0)
116 return f;
117
118 return NULL;
119}
120
Johannes Bergf84f7712013-11-14 17:14:45 +0100121static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100122{
Johannes Bergd91824c2013-11-14 17:14:44 +0100123 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100124
Johannes Bergd91824c2013-11-14 17:14:44 +0100125 for (i = 0; i < family->n_ops; i++)
126 if (family->ops[i].cmd == cmd)
127 return &family->ops[i];
Thomas Graf482a8522005-11-10 02:25:56 +0100128
129 return NULL;
130}
131
132/* Of course we are going to have problems once we hit
133 * 2^16 alive types, but that can only happen by year 2K
134*/
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000135static u16 genl_generate_id(void)
Thomas Graf482a8522005-11-10 02:25:56 +0100136{
Krishna Kumar988ade62009-10-14 19:55:07 +0000137 static u16 id_gen_idx = GENL_MIN_ID;
138 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100139
Krishna Kumar988ade62009-10-14 19:55:07 +0000140 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
Johannes Berg2ecf7532013-11-19 15:19:33 +0100141 if (id_gen_idx != GENL_ID_VFS_DQUOT &&
142 !genl_family_find_byid(id_gen_idx))
Krishna Kumar988ade62009-10-14 19:55:07 +0000143 return id_gen_idx;
144 if (++id_gen_idx > GENL_MAX_ID)
Thomas Graf482a8522005-11-10 02:25:56 +0100145 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000146 }
Thomas Graf482a8522005-11-10 02:25:56 +0100147
Krishna Kumar988ade62009-10-14 19:55:07 +0000148 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100149}
150
Johannes Berg2a94fe42013-11-19 15:19:39 +0100151static int genl_allocate_reserve_groups(int n_groups, int *first_id)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700152{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700153 unsigned long *new_groups;
Johannes Berg2a94fe42013-11-19 15:19:39 +0100154 int start = 0;
155 int i;
156 int id;
157 bool fits;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700158
Johannes Berg2a94fe42013-11-19 15:19:39 +0100159 do {
160 if (start == 0)
161 id = find_first_zero_bit(mc_groups,
162 mc_groups_longs *
163 BITS_PER_LONG);
164 else
165 id = find_next_zero_bit(mc_groups,
166 mc_groups_longs * BITS_PER_LONG,
167 start);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700168
Johannes Berg2a94fe42013-11-19 15:19:39 +0100169 fits = true;
170 for (i = id;
171 i < min_t(int, id + n_groups,
172 mc_groups_longs * BITS_PER_LONG);
173 i++) {
174 if (test_bit(i, mc_groups)) {
175 start = i;
176 fits = false;
177 break;
178 }
179 }
180
181 if (id >= mc_groups_longs * BITS_PER_LONG) {
182 unsigned long new_longs = mc_groups_longs +
183 BITS_TO_LONGS(n_groups);
184 size_t nlen = new_longs * sizeof(unsigned long);
185
186 if (mc_groups == &mc_group_start) {
187 new_groups = kzalloc(nlen, GFP_KERNEL);
188 if (!new_groups)
189 return -ENOMEM;
190 mc_groups = new_groups;
191 *mc_groups = mc_group_start;
192 } else {
193 new_groups = krealloc(mc_groups, nlen,
194 GFP_KERNEL);
195 if (!new_groups)
196 return -ENOMEM;
197 mc_groups = new_groups;
198 for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
199 mc_groups[mc_groups_longs + i] = 0;
200 }
201 mc_groups_longs = new_longs;
202 }
203 } while (!fits);
204
205 for (i = id; i < id + n_groups; i++)
206 set_bit(i, mc_groups);
207 *first_id = id;
208 return 0;
209}
210
211static struct genl_family genl_ctrl;
212
213static int genl_validate_assign_mc_groups(struct genl_family *family)
214{
215 int first_id;
216 int n_groups = family->n_mcgrps;
217 int err, i;
218 bool groups_allocated = false;
219
220 if (!n_groups)
221 return 0;
222
223 for (i = 0; i < n_groups; i++) {
224 const struct genl_multicast_group *grp = &family->mcgrps[i];
225
226 if (WARN_ON(grp->name[0] == '\0'))
227 return -EINVAL;
228 if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL))
229 return -EINVAL;
230 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700231
Johannes Berge5dcecb2013-11-19 15:19:32 +0100232 /* special-case our own group and hacks */
Johannes Berg2a94fe42013-11-19 15:19:39 +0100233 if (family == &genl_ctrl) {
234 first_id = GENL_ID_CTRL;
235 BUG_ON(n_groups != 1);
236 } else if (strcmp(family->name, "NET_DM") == 0) {
237 first_id = 1;
238 BUG_ON(n_groups != 1);
239 } else if (strcmp(family->name, "VFS_DQUOT") == 0) {
240 first_id = GENL_ID_VFS_DQUOT;
241 BUG_ON(n_groups != 1);
242 } else {
243 groups_allocated = true;
244 err = genl_allocate_reserve_groups(n_groups, &first_id);
245 if (err)
246 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700247 }
248
Johannes Berg2a94fe42013-11-19 15:19:39 +0100249 family->mcgrp_offset = first_id;
250
251 /* if still initializing, can't and don't need to to realloc bitmaps */
252 if (!init_net.genl_sock)
253 return 0;
254
Johannes Berg134e6372009-07-10 09:51:34 +0000255 if (family->netnsok) {
256 struct net *net;
257
Johannes Bergd136f1b2009-09-12 03:03:15 +0000258 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000259 rcu_read_lock();
260 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000261 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000262 mc_groups_longs * BITS_PER_LONG);
263 if (err) {
264 /*
265 * No need to roll back, can only fail if
266 * memory allocation fails and then the
267 * number of _possible_ groups has been
268 * increased on some sockets which is ok.
269 */
Johannes Berg2a94fe42013-11-19 15:19:39 +0100270 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000271 }
272 }
273 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000274 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000275 } else {
276 err = netlink_change_ngroups(init_net.genl_sock,
277 mc_groups_longs * BITS_PER_LONG);
Johannes Berg134e6372009-07-10 09:51:34 +0000278 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700279
Johannes Berg2a94fe42013-11-19 15:19:39 +0100280 if (groups_allocated && err) {
281 for (i = 0; i < family->n_mcgrps; i++)
282 clear_bit(family->mcgrp_offset + i, mc_groups);
283 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700284
Thomas Graf79d310d2007-07-24 15:34:53 -0700285 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700286}
Thomas Graf79dc43862007-07-24 15:32:46 -0700287
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700288static void genl_unregister_mc_groups(struct genl_family *family)
289{
Johannes Berg2a94fe42013-11-19 15:19:39 +0100290 struct net *net;
291 int i;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700292
Johannes Berg2a94fe42013-11-19 15:19:39 +0100293 netlink_table_grab();
294 rcu_read_lock();
295 for_each_net_rcu(net) {
296 for (i = 0; i < family->n_mcgrps; i++)
297 __netlink_clear_multicast_users(
298 net->genl_sock, family->mcgrp_offset + i);
299 }
300 rcu_read_unlock();
301 netlink_table_ungrab();
302
303 for (i = 0; i < family->n_mcgrps; i++) {
304 int grp_id = family->mcgrp_offset + i;
305
306 if (grp_id != 1)
307 clear_bit(grp_id, mc_groups);
308 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family,
309 &family->mcgrps[i], grp_id);
310 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700311}
312
Johannes Berg568508a2013-11-15 14:19:08 +0100313static int genl_validate_ops(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100314{
Johannes Berg568508a2013-11-15 14:19:08 +0100315 const struct genl_ops *ops = family->ops;
316 unsigned int n_ops = family->n_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100317 int i, j;
Thomas Graf482a8522005-11-10 02:25:56 +0100318
Johannes Berg568508a2013-11-15 14:19:08 +0100319 if (WARN_ON(n_ops && !ops))
320 return -EINVAL;
321
322 if (!n_ops)
323 return 0;
324
Johannes Bergd91824c2013-11-14 17:14:44 +0100325 for (i = 0; i < n_ops; i++) {
326 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
327 return -EINVAL;
328 for (j = i + 1; j < n_ops; j++)
329 if (ops[i].cmd == ops[j].cmd)
330 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100331 }
332
Johannes Bergd91824c2013-11-14 17:14:44 +0100333 /* family is not registered yet, so no locking needed */
334 family->ops = ops;
335 family->n_ops = n_ops;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800336
Johannes Bergd91824c2013-11-14 17:14:44 +0100337 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100338}
Thomas Graf482a8522005-11-10 02:25:56 +0100339
340/**
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700341 * __genl_register_family - register a generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +0100342 * @family: generic netlink family
343 *
344 * Registers the specified family after validating it first. Only one
345 * family may be registered with the same family name or identifier.
346 * The family id may equal GENL_ID_GENERATE causing an unique id to
347 * be automatically generated and assigned.
348 *
Johannes Berg568508a2013-11-15 14:19:08 +0100349 * The family's ops array must already be assigned, you can use the
350 * genl_register_family_with_ops() helper function.
351 *
Thomas Graf482a8522005-11-10 02:25:56 +0100352 * Return 0 on success or a negative error code.
353 */
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700354int __genl_register_family(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100355{
Johannes Berg2a94fe42013-11-19 15:19:39 +0100356 int err = -EINVAL, i;
Thomas Graf482a8522005-11-10 02:25:56 +0100357
358 if (family->id && family->id < GENL_MIN_ID)
359 goto errout;
360
361 if (family->id > GENL_MAX_ID)
362 goto errout;
363
Johannes Berg568508a2013-11-15 14:19:08 +0100364 err = genl_validate_ops(family);
365 if (err)
366 return err;
367
Pravin B Shelardef31172013-04-23 07:48:30 +0000368 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100369
370 if (genl_family_find_byname(family->name)) {
371 err = -EEXIST;
372 goto errout_locked;
373 }
374
Thomas Graf482a8522005-11-10 02:25:56 +0100375 if (family->id == GENL_ID_GENERATE) {
376 u16 newid = genl_generate_id();
377
378 if (!newid) {
379 err = -ENOMEM;
380 goto errout_locked;
381 }
382
383 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000384 } else if (genl_family_find_byid(family->id)) {
385 err = -EEXIST;
386 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100387 }
388
Pravin B Shelardef31172013-04-23 07:48:30 +0000389 if (family->maxattr && !family->parallel_ops) {
Thomas Graf482a8522005-11-10 02:25:56 +0100390 family->attrbuf = kmalloc((family->maxattr+1) *
391 sizeof(struct nlattr *), GFP_KERNEL);
392 if (family->attrbuf == NULL) {
393 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800394 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100395 }
396 } else
397 family->attrbuf = NULL;
398
Johannes Berg2a94fe42013-11-19 15:19:39 +0100399 err = genl_validate_assign_mc_groups(family);
400 if (err)
401 goto errout_locked;
402
Thomas Graf482a8522005-11-10 02:25:56 +0100403 list_add_tail(&family->family_list, genl_family_chain(family->id));
Pravin B Shelardef31172013-04-23 07:48:30 +0000404 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100405
Johannes Berg2a94fe42013-11-19 15:19:39 +0100406 /* send all events */
407 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
408 for (i = 0; i < family->n_mcgrps; i++)
409 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
410 &family->mcgrps[i], family->mcgrp_offset + i);
Thomas Graf482a8522005-11-10 02:25:56 +0100411
412 return 0;
413
414errout_locked:
Pravin B Shelardef31172013-04-23 07:48:30 +0000415 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100416errout:
417 return err;
418}
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700419EXPORT_SYMBOL(__genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100420
421/**
422 * genl_unregister_family - unregister generic netlink family
423 * @family: generic netlink family
424 *
425 * Unregisters the specified family.
426 *
427 * Returns 0 on success or a negative error code.
428 */
429int genl_unregister_family(struct genl_family *family)
430{
431 struct genl_family *rc;
432
Pravin B Shelardef31172013-04-23 07:48:30 +0000433 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100434
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800435 genl_unregister_mc_groups(family);
436
Thomas Graf482a8522005-11-10 02:25:56 +0100437 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
438 if (family->id != rc->id || strcmp(rc->name, family->name))
439 continue;
440
441 list_del(&rc->family_list);
Johannes Bergd91824c2013-11-14 17:14:44 +0100442 family->n_ops = 0;
Pravin B Shelardef31172013-04-23 07:48:30 +0000443 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100444
Thomas Graf482a8522005-11-10 02:25:56 +0100445 kfree(family->attrbuf);
Johannes Berg2a94fe42013-11-19 15:19:39 +0100446 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
Thomas Graf482a8522005-11-10 02:25:56 +0100447 return 0;
448 }
449
Pravin B Shelardef31172013-04-23 07:48:30 +0000450 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100451
452 return -ENOENT;
453}
Changli Gao416c2f92010-07-25 20:46:01 +0000454EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100455
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500456/**
Thomas Grafbb9b18f2013-11-30 13:21:30 +0100457 * genlmsg_new_unicast - Allocate generic netlink message for unicast
458 * @payload: size of the message payload
459 * @info: information on destination
460 * @flags: the type of memory to allocate
461 *
462 * Allocates a new sk_buff large enough to cover the specified payload
463 * plus required Netlink headers. Will check receiving socket for
464 * memory mapped i/o capability and use it if enabled. Will fall back
465 * to non-mapped skb if message size exceeds the frame size of the ring.
466 */
467struct sk_buff *genlmsg_new_unicast(size_t payload, struct genl_info *info,
468 gfp_t flags)
469{
470 size_t len = nlmsg_total_size(genlmsg_total_size(payload));
471
472 return netlink_alloc_skb(info->dst_sk, len, info->snd_portid, flags);
473}
474EXPORT_SYMBOL_GPL(genlmsg_new_unicast);
475
476/**
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500477 * genlmsg_put - Add generic netlink header to netlink message
478 * @skb: socket buffer holding the message
Eric W. Biederman15e47302012-09-07 20:12:54 +0000479 * @portid: netlink portid the message is addressed to
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500480 * @seq: sequence number (usually the one of the sender)
481 * @family: generic netlink family
Ben Hutchings2c530402012-07-10 10:55:09 +0000482 * @flags: netlink message flags
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500483 * @cmd: generic netlink command
484 *
485 * Returns pointer to user specific header
486 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000487void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500488 struct genl_family *family, int flags, u8 cmd)
489{
490 struct nlmsghdr *nlh;
491 struct genlmsghdr *hdr;
492
Eric W. Biederman15e47302012-09-07 20:12:54 +0000493 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500494 family->hdrsize, flags);
495 if (nlh == NULL)
496 return NULL;
497
498 hdr = nlmsg_data(nlh);
499 hdr->cmd = cmd;
500 hdr->version = family->version;
501 hdr->reserved = 0;
502
503 return (char *) hdr + GENL_HDRLEN;
504}
505EXPORT_SYMBOL(genlmsg_put);
506
Pravin B Shelar9b963092013-08-23 12:44:55 -0700507static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
508{
Johannes Bergf84f7712013-11-14 17:14:45 +0100509 /* our ops are always const - netlink API doesn't propagate that */
510 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700511 int rc;
512
513 genl_lock();
514 rc = ops->dumpit(skb, cb);
515 genl_unlock();
516 return rc;
517}
518
519static int genl_lock_done(struct netlink_callback *cb)
520{
Johannes Bergf84f7712013-11-14 17:14:45 +0100521 /* our ops are always const - netlink API doesn't propagate that */
522 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700523 int rc = 0;
524
525 if (ops->done) {
526 genl_lock();
527 rc = ops->done(cb);
528 genl_unlock();
529 }
530 return rc;
531}
532
Pravin B Shelardef31172013-04-23 07:48:30 +0000533static int genl_family_rcv_msg(struct genl_family *family,
534 struct sk_buff *skb,
535 struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100536{
Johannes Bergf84f7712013-11-14 17:14:45 +0100537 const struct genl_ops *ops;
Johannes Berg134e6372009-07-10 09:51:34 +0000538 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100539 struct genl_info info;
540 struct genlmsghdr *hdr = nlmsg_data(nlh);
Pravin B Shelardef31172013-04-23 07:48:30 +0000541 struct nlattr **attrbuf;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700542 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100543
Johannes Berg134e6372009-07-10 09:51:34 +0000544 /* this family doesn't exist in this netns */
545 if (!family->netnsok && !net_eq(net, &init_net))
546 return -ENOENT;
547
Thomas Graf482a8522005-11-10 02:25:56 +0100548 hdrlen = GENL_HDRLEN + family->hdrsize;
549 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700550 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100551
552 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700553 if (ops == NULL)
554 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100555
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700556 if ((ops->flags & GENL_ADMIN_PERM) &&
Eric Parisfd778462012-01-03 12:25:16 -0500557 !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700558 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100559
Pablo Neirae1ee3672013-07-29 12:30:04 +0200560 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
Pravin B Shelar9b963092013-08-23 12:44:55 -0700561 int rc;
Pravin B Shelardef31172013-04-23 07:48:30 +0000562
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700563 if (ops->dumpit == NULL)
564 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100565
Pravin B Shelar9b963092013-08-23 12:44:55 -0700566 if (!family->parallel_ops) {
567 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700568 .module = family->module,
Johannes Bergf84f7712013-11-14 17:14:45 +0100569 /* we have const, but the netlink API doesn't */
570 .data = (void *)ops,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700571 .dump = genl_lock_dumpit,
572 .done = genl_lock_done,
573 };
574
575 genl_unlock();
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700576 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700577 genl_lock();
578
579 } else {
580 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700581 .module = family->module,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700582 .dump = ops->dumpit,
583 .done = ops->done,
584 };
585
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700586 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700587 }
588
589 return rc;
Thomas Graf482a8522005-11-10 02:25:56 +0100590 }
591
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700592 if (ops->doit == NULL)
593 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100594
Pravin B Shelardef31172013-04-23 07:48:30 +0000595 if (family->maxattr && family->parallel_ops) {
596 attrbuf = kmalloc((family->maxattr+1) *
597 sizeof(struct nlattr *), GFP_KERNEL);
598 if (attrbuf == NULL)
599 return -ENOMEM;
600 } else
601 attrbuf = family->attrbuf;
602
603 if (attrbuf) {
604 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
Thomas Graf482a8522005-11-10 02:25:56 +0100605 ops->policy);
606 if (err < 0)
Wei Yongjun50754d212013-04-26 15:34:16 +0000607 goto out;
Thomas Graf482a8522005-11-10 02:25:56 +0100608 }
609
610 info.snd_seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000611 info.snd_portid = NETLINK_CB(skb).portid;
Thomas Graf482a8522005-11-10 02:25:56 +0100612 info.nlhdr = nlh;
613 info.genlhdr = nlmsg_data(nlh);
614 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
Pravin B Shelardef31172013-04-23 07:48:30 +0000615 info.attrs = attrbuf;
Thomas Grafbb9b18f2013-11-30 13:21:30 +0100616 info.dst_sk = skb->sk;
Johannes Berg134e6372009-07-10 09:51:34 +0000617 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200618 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100619
Johannes Bergff4c92d2010-10-04 21:14:03 +0200620 if (family->pre_doit) {
621 err = family->pre_doit(ops, skb, &info);
622 if (err)
Wei Yongjun50754d212013-04-26 15:34:16 +0000623 goto out;
Johannes Bergff4c92d2010-10-04 21:14:03 +0200624 }
625
626 err = ops->doit(skb, &info);
627
628 if (family->post_doit)
629 family->post_doit(ops, skb, &info);
630
Wei Yongjun50754d212013-04-26 15:34:16 +0000631out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000632 if (family->parallel_ops)
633 kfree(attrbuf);
634
635 return err;
636}
637
638static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
639{
640 struct genl_family *family;
641 int err;
642
643 family = genl_family_find_byid(nlh->nlmsg_type);
644 if (family == NULL)
645 return -ENOENT;
646
647 if (!family->parallel_ops)
648 genl_lock();
649
650 err = genl_family_rcv_msg(family, skb, nlh);
651
652 if (!family->parallel_ops)
653 genl_unlock();
654
Johannes Bergff4c92d2010-10-04 21:14:03 +0200655 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100656}
657
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700658static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100659{
Pravin B Shelardef31172013-04-23 07:48:30 +0000660 down_read(&cb_lock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700661 netlink_rcv_skb(skb, &genl_rcv_msg);
Pravin B Shelardef31172013-04-23 07:48:30 +0000662 up_read(&cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +0100663}
664
665/**************************************************************************
666 * Controller
667 **************************************************************************/
668
Thomas Graf17c157c2006-11-14 19:46:02 -0800669static struct genl_family genl_ctrl = {
670 .id = GENL_ID_CTRL,
671 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800672 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800673 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000674 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800675};
676
Eric W. Biederman15e47302012-09-07 20:12:54 +0000677static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
Thomas Graf482a8522005-11-10 02:25:56 +0100678 u32 flags, struct sk_buff *skb, u8 cmd)
679{
680 void *hdr;
681
Eric W. Biederman15e47302012-09-07 20:12:54 +0000682 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100683 if (hdr == NULL)
684 return -1;
685
David S. Miller444653f2012-03-29 23:25:11 -0400686 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
687 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
688 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
689 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
690 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
691 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700692
Johannes Bergd91824c2013-11-14 17:14:44 +0100693 if (family->n_ops) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800694 struct nlattr *nla_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100695 int i;
Thomas Grafeb328112006-09-18 00:01:59 -0700696
Thomas Grafe94ef682006-11-23 11:44:37 -0800697 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
698 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700699 goto nla_put_failure;
700
Johannes Bergd91824c2013-11-14 17:14:44 +0100701 for (i = 0; i < family->n_ops; i++) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800702 struct nlattr *nest;
Johannes Bergf84f7712013-11-14 17:14:45 +0100703 const struct genl_ops *ops = &family->ops[i];
Johannes Berg029b2342013-11-18 20:54:58 +0100704 u32 op_flags = ops->flags;
Johannes Bergf84f7712013-11-14 17:14:45 +0100705
706 if (ops->dumpit)
Johannes Berg029b2342013-11-18 20:54:58 +0100707 op_flags |= GENL_CMD_CAP_DUMP;
Johannes Bergf84f7712013-11-14 17:14:45 +0100708 if (ops->doit)
Johannes Berg029b2342013-11-18 20:54:58 +0100709 op_flags |= GENL_CMD_CAP_DO;
Johannes Bergf84f7712013-11-14 17:14:45 +0100710 if (ops->policy)
Johannes Berg029b2342013-11-18 20:54:58 +0100711 op_flags |= GENL_CMD_CAP_HASPOL;
Thomas Grafeb328112006-09-18 00:01:59 -0700712
Johannes Bergd91824c2013-11-14 17:14:44 +0100713 nest = nla_nest_start(skb, i + 1);
Thomas Grafe94ef682006-11-23 11:44:37 -0800714 if (nest == NULL)
715 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700716
David S. Miller444653f2012-03-29 23:25:11 -0400717 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
Johannes Berg029b2342013-11-18 20:54:58 +0100718 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
David S. Miller444653f2012-03-29 23:25:11 -0400719 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700720
Thomas Grafe94ef682006-11-23 11:44:37 -0800721 nla_nest_end(skb, nest);
722 }
723
724 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700725 }
726
Johannes Berg2a94fe42013-11-19 15:19:39 +0100727 if (family->n_mcgrps) {
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700728 struct nlattr *nla_grps;
Johannes Berg2a94fe42013-11-19 15:19:39 +0100729 int i;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700730
731 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
732 if (nla_grps == NULL)
733 goto nla_put_failure;
734
Johannes Berg2a94fe42013-11-19 15:19:39 +0100735 for (i = 0; i < family->n_mcgrps; i++) {
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700736 struct nlattr *nest;
Johannes Berg2a94fe42013-11-19 15:19:39 +0100737 const struct genl_multicast_group *grp;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700738
Johannes Berg2a94fe42013-11-19 15:19:39 +0100739 grp = &family->mcgrps[i];
740
741 nest = nla_nest_start(skb, i + 1);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700742 if (nest == NULL)
743 goto nla_put_failure;
744
Johannes Berg2a94fe42013-11-19 15:19:39 +0100745 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
746 family->mcgrp_offset + i) ||
David S. Miller444653f2012-03-29 23:25:11 -0400747 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
748 grp->name))
749 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700750
751 nla_nest_end(skb, nest);
752 }
753 nla_nest_end(skb, nla_grps);
754 }
755
756 return genlmsg_end(skb, hdr);
757
758nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700759 genlmsg_cancel(skb, hdr);
760 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700761}
762
Johannes Bergc2ebb902013-11-19 15:19:36 +0100763static int ctrl_fill_mcgrp_info(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +0100764 const struct genl_multicast_group *grp,
765 int grp_id, u32 portid, u32 seq, u32 flags,
766 struct sk_buff *skb, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700767{
768 void *hdr;
769 struct nlattr *nla_grps;
770 struct nlattr *nest;
771
Eric W. Biederman15e47302012-09-07 20:12:54 +0000772 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700773 if (hdr == NULL)
774 return -1;
775
Johannes Bergc2ebb902013-11-19 15:19:36 +0100776 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
777 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
David S. Miller444653f2012-03-29 23:25:11 -0400778 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700779
780 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
781 if (nla_grps == NULL)
782 goto nla_put_failure;
783
784 nest = nla_nest_start(skb, 1);
785 if (nest == NULL)
786 goto nla_put_failure;
787
Johannes Berg2a94fe42013-11-19 15:19:39 +0100788 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
David S. Miller444653f2012-03-29 23:25:11 -0400789 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
790 grp->name))
791 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700792
793 nla_nest_end(skb, nest);
794 nla_nest_end(skb, nla_grps);
795
Thomas Graf482a8522005-11-10 02:25:56 +0100796 return genlmsg_end(skb, hdr);
797
798nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700799 genlmsg_cancel(skb, hdr);
800 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100801}
802
803static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
804{
805
806 int i, n = 0;
807 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000808 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100809 int chains_to_skip = cb->args[0];
810 int fams_to_skip = cb->args[1];
811
Samir Bellabese1d5a012010-01-07 22:10:56 +0000812 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100813 n = 0;
814 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000815 if (!rt->netnsok && !net_eq(net, &init_net))
816 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100817 if (++n < fams_to_skip)
818 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000819 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
Thomas Graf482a8522005-11-10 02:25:56 +0100820 cb->nlh->nlmsg_seq, NLM_F_MULTI,
821 skb, CTRL_CMD_NEWFAMILY) < 0)
822 goto errout;
823 }
824
825 fams_to_skip = 0;
826 }
827
828errout:
829 cb->args[0] = i;
830 cb->args[1] = n;
831
832 return skb->len;
833}
834
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700835static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000836 u32 portid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100837{
838 struct sk_buff *skb;
839 int err;
840
Thomas Graf339bf982006-11-10 14:10:15 -0800841 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100842 if (skb == NULL)
843 return ERR_PTR(-ENOBUFS);
844
Eric W. Biederman15e47302012-09-07 20:12:54 +0000845 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100846 if (err < 0) {
847 nlmsg_free(skb);
848 return ERR_PTR(err);
849 }
850
851 return skb;
852}
853
Johannes Berg2a94fe42013-11-19 15:19:39 +0100854static struct sk_buff *
855ctrl_build_mcgrp_msg(struct genl_family *family,
856 const struct genl_multicast_group *grp,
857 int grp_id, u32 portid, int seq, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700858{
859 struct sk_buff *skb;
860 int err;
861
862 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
863 if (skb == NULL)
864 return ERR_PTR(-ENOBUFS);
865
Johannes Berg2a94fe42013-11-19 15:19:39 +0100866 err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
867 seq, 0, skb, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700868 if (err < 0) {
869 nlmsg_free(skb);
870 return ERR_PTR(err);
871 }
872
873 return skb;
874}
875
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700876static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100877 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700878 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
879 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100880};
881
882static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
883{
884 struct sk_buff *msg;
885 struct genl_family *res = NULL;
886 int err = -EINVAL;
887
888 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
889 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
890 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000891 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100892 }
893
894 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700895 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100896
Thomas Graf5176f912006-08-26 20:13:18 -0700897 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100898 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500899#ifdef CONFIG_MODULES
900 if (res == NULL) {
901 genl_unlock();
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200902 up_read(&cb_lock);
Neil Hormane9412c32012-05-29 09:30:41 +0000903 request_module("net-pf-%d-proto-%d-family-%s",
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500904 PF_NETLINK, NETLINK_GENERIC, name);
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200905 down_read(&cb_lock);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500906 genl_lock();
907 res = genl_family_find_byname(name);
908 }
909#endif
Johannes Berg134e6372009-07-10 09:51:34 +0000910 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100911 }
912
Johannes Berg134e6372009-07-10 09:51:34 +0000913 if (res == NULL)
914 return err;
915
916 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
917 /* family doesn't exist here */
918 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100919 }
920
Eric W. Biederman15e47302012-09-07 20:12:54 +0000921 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700922 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000923 if (IS_ERR(msg))
924 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100925
Johannes Berg134e6372009-07-10 09:51:34 +0000926 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100927}
928
Johannes Bergc2ebb902013-11-19 15:19:36 +0100929static int genl_ctrl_event(int event, struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +0100930 const struct genl_multicast_group *grp,
931 int grp_id)
Thomas Graf482a8522005-11-10 02:25:56 +0100932{
933 struct sk_buff *msg;
934
Johannes Berg134e6372009-07-10 09:51:34 +0000935 /* genl is still initialising */
936 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100937 return 0;
938
939 switch (event) {
940 case CTRL_CMD_NEWFAMILY:
941 case CTRL_CMD_DELFAMILY:
Johannes Bergc2ebb902013-11-19 15:19:36 +0100942 WARN_ON(grp);
Johannes Berg134e6372009-07-10 09:51:34 +0000943 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700944 break;
945 case CTRL_CMD_NEWMCAST_GRP:
946 case CTRL_CMD_DELMCAST_GRP:
Johannes Bergc2ebb902013-11-19 15:19:36 +0100947 BUG_ON(!grp);
Johannes Berg2a94fe42013-11-19 15:19:39 +0100948 msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100949 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000950 default:
951 return -EINVAL;
952 }
953
954 if (IS_ERR(msg))
955 return PTR_ERR(msg);
956
957 if (!family->netnsok) {
Johannes Berg68eb5502013-11-19 15:19:38 +0100958 genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +0100959 0, GFP_KERNEL);
Johannes Berg134e6372009-07-10 09:51:34 +0000960 } else {
961 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +0100962 genlmsg_multicast_allns(&genl_ctrl, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +0100963 0, GFP_ATOMIC);
Johannes Berg134e6372009-07-10 09:51:34 +0000964 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100965 }
966
967 return 0;
968}
969
Johannes Bergc53ed742013-11-19 15:19:31 +0100970static struct genl_ops genl_ctrl_ops[] = {
971 {
972 .cmd = CTRL_CMD_GETFAMILY,
973 .doit = ctrl_getfamily,
974 .dumpit = ctrl_dumpfamily,
975 .policy = ctrl_policy,
976 },
Thomas Graf482a8522005-11-10 02:25:56 +0100977};
978
Johannes Berg2a94fe42013-11-19 15:19:39 +0100979static struct genl_multicast_group genl_ctrl_groups[] = {
980 { .name = "notify", },
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700981};
982
Johannes Berg134e6372009-07-10 09:51:34 +0000983static int __net_init genl_pernet_init(struct net *net)
984{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000985 struct netlink_kernel_cfg cfg = {
986 .input = genl_rcv,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000987 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000988 };
989
Johannes Berg134e6372009-07-10 09:51:34 +0000990 /* we'll bump the group number right afterwards */
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000991 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
Johannes Berg134e6372009-07-10 09:51:34 +0000992
993 if (!net->genl_sock && net_eq(net, &init_net))
994 panic("GENL: Cannot initialize generic netlink\n");
995
996 if (!net->genl_sock)
997 return -ENOMEM;
998
999 return 0;
1000}
1001
1002static void __net_exit genl_pernet_exit(struct net *net)
1003{
1004 netlink_kernel_release(net->genl_sock);
1005 net->genl_sock = NULL;
1006}
1007
1008static struct pernet_operations genl_pernet_ops = {
1009 .init = genl_pernet_init,
1010 .exit = genl_pernet_exit,
1011};
1012
Thomas Graf482a8522005-11-10 02:25:56 +01001013static int __init genl_init(void)
1014{
1015 int i, err;
1016
1017 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
1018 INIT_LIST_HEAD(&family_ht[i]);
1019
Johannes Berg2a94fe42013-11-19 15:19:39 +01001020 err = genl_register_family_with_ops_groups(&genl_ctrl, genl_ctrl_ops,
1021 genl_ctrl_groups);
Thomas Graf482a8522005-11-10 02:25:56 +01001022 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +00001023 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +01001024
Johannes Berg134e6372009-07-10 09:51:34 +00001025 err = register_pernet_subsys(&genl_pernet_ops);
1026 if (err)
1027 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +01001028
1029 return 0;
1030
Johannes Berg134e6372009-07-10 09:51:34 +00001031problem:
Thomas Graf482a8522005-11-10 02:25:56 +01001032 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +01001033}
1034
1035subsys_initcall(genl_init);
1036
Eric W. Biederman15e47302012-09-07 20:12:54 +00001037static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
Johannes Berg134e6372009-07-10 09:51:34 +00001038 gfp_t flags)
1039{
1040 struct sk_buff *tmp;
1041 struct net *net, *prev = NULL;
1042 int err;
1043
1044 for_each_net_rcu(net) {
1045 if (prev) {
1046 tmp = skb_clone(skb, flags);
1047 if (!tmp) {
1048 err = -ENOMEM;
1049 goto error;
1050 }
1051 err = nlmsg_multicast(prev->genl_sock, tmp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001052 portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001053 if (err)
1054 goto error;
1055 }
1056
1057 prev = net;
1058 }
1059
Eric W. Biederman15e47302012-09-07 20:12:54 +00001060 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001061 error:
1062 kfree_skb(skb);
1063 return err;
1064}
1065
Johannes Berg68eb5502013-11-19 15:19:38 +01001066int genlmsg_multicast_allns(struct genl_family *family, struct sk_buff *skb,
1067 u32 portid, unsigned int group, gfp_t flags)
Johannes Berg134e6372009-07-10 09:51:34 +00001068{
Johannes Berg220815a2013-11-21 18:17:04 +01001069 if (WARN_ON_ONCE(group >= family->n_mcgrps))
Johannes Berg2a94fe42013-11-19 15:19:39 +01001070 return -EINVAL;
1071 group = family->mcgrp_offset + group;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001072 return genlmsg_mcast(skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001073}
1074EXPORT_SYMBOL(genlmsg_multicast_allns);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001075
Johannes Berg68eb5502013-11-19 15:19:38 +01001076void genl_notify(struct genl_family *family,
1077 struct sk_buff *skb, struct net *net, u32 portid, u32 group,
Pravin B Shelar263ba612011-11-10 19:14:37 -08001078 struct nlmsghdr *nlh, gfp_t flags)
1079{
1080 struct sock *sk = net->genl_sock;
1081 int report = 0;
1082
1083 if (nlh)
1084 report = nlmsg_report(nlh);
1085
Johannes Berg220815a2013-11-21 18:17:04 +01001086 if (WARN_ON_ONCE(group >= family->n_mcgrps))
Johannes Berg2a94fe42013-11-19 15:19:39 +01001087 return;
1088 group = family->mcgrp_offset + group;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001089 nlmsg_notify(sk, skb, portid, group, report, flags);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001090}
1091EXPORT_SYMBOL(genl_notify);