blob: eb916c44884f18dc8edb42928703f868fdd3dd31 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Thomas Graf482a8522005-11-10 02:25:56 +01002/*
3 * NETLINK Generic Netlink Family
4 *
5 * Authors: Jamal Hadi Salim
6 * Thomas Graf <tgraf@suug.ch>
Johannes Berg2dbba6f2007-07-18 15:47:52 -07007 * Johannes Berg <johannes@sipsolutions.net>
Thomas Graf482a8522005-11-10 02:25:56 +01008 */
9
Thomas Graf482a8522005-11-10 02:25:56 +010010#include <linux/module.h>
11#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Thomas Graf482a8522005-11-10 02:25:56 +010013#include <linux/errno.h>
14#include <linux/types.h>
15#include <linux/socket.h>
16#include <linux/string.h>
17#include <linux/skbuff.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080018#include <linux/mutex.h>
Johannes Berg2dbba6f2007-07-18 15:47:52 -070019#include <linux/bitmap.h>
Pravin B Shelardef31172013-04-23 07:48:30 +000020#include <linux/rwsem.h>
Johannes Berg2ae0f172016-10-24 14:40:04 +020021#include <linux/idr.h>
Thomas Graf482a8522005-11-10 02:25:56 +010022#include <net/sock.h>
23#include <net/genetlink.h>
24
Ingo Molnar14cc3e22006-03-26 01:37:14 -080025static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
Pravin B Shelardef31172013-04-23 07:48:30 +000026static DECLARE_RWSEM(cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010027
Johannes Bergee1c24422015-01-16 11:37:14 +010028atomic_t genl_sk_destructing_cnt = ATOMIC_INIT(0);
29DECLARE_WAIT_QUEUE_HEAD(genl_sk_destructing_waitq);
30
James Chapmanf408e0c2010-04-02 06:19:05 +000031void genl_lock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010032{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080033 mutex_lock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010034}
James Chapmanf408e0c2010-04-02 06:19:05 +000035EXPORT_SYMBOL(genl_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010036
James Chapmanf408e0c2010-04-02 06:19:05 +000037void genl_unlock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010038{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080039 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010040}
James Chapmanf408e0c2010-04-02 06:19:05 +000041EXPORT_SYMBOL(genl_unlock);
Thomas Graf482a8522005-11-10 02:25:56 +010042
WANG Cong320f5ea2012-07-24 13:44:01 +080043#ifdef CONFIG_LOCKDEP
Yaowei Bai61d03532015-10-08 21:28:54 +080044bool lockdep_genl_is_held(void)
Pravin B Shelar86b13092011-11-10 19:14:51 -080045{
46 return lockdep_is_held(&genl_mutex);
47}
48EXPORT_SYMBOL(lockdep_genl_is_held);
49#endif
50
Pravin B Shelardef31172013-04-23 07:48:30 +000051static void genl_lock_all(void)
52{
53 down_write(&cb_lock);
54 genl_lock();
55}
56
57static void genl_unlock_all(void)
58{
59 genl_unlock();
60 up_write(&cb_lock);
61}
62
Johannes Berg2ae0f172016-10-24 14:40:04 +020063static DEFINE_IDR(genl_fam_idr);
Thomas Graf482a8522005-11-10 02:25:56 +010064
Johannes Berg2dbba6f2007-07-18 15:47:52 -070065/*
66 * Bitmap of multicast groups that are currently in use.
67 *
68 * To avoid an allocation at boot of just one unsigned long,
69 * declare it global instead.
70 * Bit 0 is marked as already used since group 0 is invalid.
Johannes Berge5dcecb2013-11-19 15:19:32 +010071 * Bit 1 is marked as already used since the drop-monitor code
72 * abuses the API and thinks it can statically use group 1.
73 * That group will typically conflict with other groups that
74 * any proper users use.
Johannes Berg2a94fe42013-11-19 15:19:39 +010075 * Bit 16 is marked as used since it's used for generic netlink
76 * and the code no longer marks pre-reserved IDs as used.
Johannes Berg2ecf7532013-11-19 15:19:33 +010077 * Bit 17 is marked as already used since the VFS quota code
78 * also abused this API and relied on family == group ID, we
79 * cater to that by giving it a static family and group ID.
Johannes Berg5e53e682013-11-24 21:09:26 +010080 * Bit 18 is marked as already used since the PMCRAID driver
81 * did the same thing as the VFS quota code (maybe copied?)
Johannes Berg2dbba6f2007-07-18 15:47:52 -070082 */
Johannes Berg2a94fe42013-11-19 15:19:39 +010083static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) |
Johannes Berg5e53e682013-11-24 21:09:26 +010084 BIT(GENL_ID_VFS_DQUOT) |
85 BIT(GENL_ID_PMCRAID);
Johannes Berg2dbba6f2007-07-18 15:47:52 -070086static unsigned long *mc_groups = &mc_group_start;
87static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010088
Johannes Berg2ae0f172016-10-24 14:40:04 +020089static int genl_ctrl_event(int event, const struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010090 const struct genl_multicast_group *grp,
91 int grp_id);
Thomas Graf482a8522005-11-10 02:25:56 +010092
Johannes Berg2ae0f172016-10-24 14:40:04 +020093static const struct genl_family *genl_family_find_byid(unsigned int id)
Thomas Graf482a8522005-11-10 02:25:56 +010094{
Johannes Berg2ae0f172016-10-24 14:40:04 +020095 return idr_find(&genl_fam_idr, id);
Thomas Graf482a8522005-11-10 02:25:56 +010096}
97
Johannes Berg2ae0f172016-10-24 14:40:04 +020098static const struct genl_family *genl_family_find_byname(char *name)
Thomas Graf482a8522005-11-10 02:25:56 +010099{
Johannes Berg2ae0f172016-10-24 14:40:04 +0200100 const struct genl_family *family;
101 unsigned int id;
Thomas Graf482a8522005-11-10 02:25:56 +0100102
Johannes Berg2ae0f172016-10-24 14:40:04 +0200103 idr_for_each_entry(&genl_fam_idr, family, id)
104 if (strcmp(family->name, name) == 0)
105 return family;
Thomas Graf482a8522005-11-10 02:25:56 +0100106
107 return NULL;
108}
109
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700110static int genl_get_cmd_cnt(const struct genl_family *family)
111{
112 return family->n_ops + family->n_small_ops;
113}
114
115static void genl_op_from_full(const struct genl_family *family,
116 unsigned int i, struct genl_ops *op)
117{
118 *op = family->ops[i];
Jakub Kicinski48526a02020-10-02 14:49:57 -0700119
120 if (!op->maxattr)
121 op->maxattr = family->maxattr;
122 if (!op->policy)
123 op->policy = family->policy;
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700124}
125
126static int genl_get_cmd_full(u8 cmd, const struct genl_family *family,
127 struct genl_ops *op)
Thomas Graf482a8522005-11-10 02:25:56 +0100128{
Johannes Bergd91824c2013-11-14 17:14:44 +0100129 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100130
Johannes Bergd91824c2013-11-14 17:14:44 +0100131 for (i = 0; i < family->n_ops; i++)
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700132 if (family->ops[i].cmd == cmd) {
133 genl_op_from_full(family, i, op);
134 return 0;
135 }
Thomas Graf482a8522005-11-10 02:25:56 +0100136
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700137 return -ENOENT;
138}
139
140static void genl_op_from_small(const struct genl_family *family,
141 unsigned int i, struct genl_ops *op)
142{
143 memset(op, 0, sizeof(*op));
144 op->doit = family->small_ops[i].doit;
145 op->dumpit = family->small_ops[i].dumpit;
146 op->cmd = family->small_ops[i].cmd;
147 op->internal_flags = family->small_ops[i].internal_flags;
148 op->flags = family->small_ops[i].flags;
149 op->validate = family->small_ops[i].validate;
Jakub Kicinski48526a02020-10-02 14:49:57 -0700150
151 op->maxattr = family->maxattr;
152 op->policy = family->policy;
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700153}
154
155static int genl_get_cmd_small(u8 cmd, const struct genl_family *family,
156 struct genl_ops *op)
157{
158 int i;
159
160 for (i = 0; i < family->n_small_ops; i++)
161 if (family->small_ops[i].cmd == cmd) {
162 genl_op_from_small(family, i, op);
163 return 0;
164 }
165
166 return -ENOENT;
167}
168
169static int genl_get_cmd(u8 cmd, const struct genl_family *family,
170 struct genl_ops *op)
171{
172 if (!genl_get_cmd_full(cmd, family, op))
173 return 0;
174 return genl_get_cmd_small(cmd, family, op);
175}
176
177static void genl_get_cmd_by_index(unsigned int i,
178 const struct genl_family *family,
179 struct genl_ops *op)
180{
181 if (i < family->n_ops)
182 genl_op_from_full(family, i, op);
183 else if (i < family->n_ops + family->n_small_ops)
184 genl_op_from_small(family, i - family->n_ops, op);
185 else
186 WARN_ON_ONCE(1);
Thomas Graf482a8522005-11-10 02:25:56 +0100187}
188
Johannes Berg2a94fe42013-11-19 15:19:39 +0100189static int genl_allocate_reserve_groups(int n_groups, int *first_id)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700190{
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700191 unsigned long *new_groups;
Johannes Berg2a94fe42013-11-19 15:19:39 +0100192 int start = 0;
193 int i;
194 int id;
195 bool fits;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700196
Johannes Berg2a94fe42013-11-19 15:19:39 +0100197 do {
198 if (start == 0)
199 id = find_first_zero_bit(mc_groups,
200 mc_groups_longs *
201 BITS_PER_LONG);
202 else
203 id = find_next_zero_bit(mc_groups,
204 mc_groups_longs * BITS_PER_LONG,
205 start);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700206
Johannes Berg2a94fe42013-11-19 15:19:39 +0100207 fits = true;
208 for (i = id;
209 i < min_t(int, id + n_groups,
210 mc_groups_longs * BITS_PER_LONG);
211 i++) {
212 if (test_bit(i, mc_groups)) {
213 start = i;
214 fits = false;
215 break;
216 }
217 }
218
David S. Millerb8e429a2016-01-13 10:28:06 -0500219 if (id + n_groups > mc_groups_longs * BITS_PER_LONG) {
Johannes Berg2a94fe42013-11-19 15:19:39 +0100220 unsigned long new_longs = mc_groups_longs +
221 BITS_TO_LONGS(n_groups);
222 size_t nlen = new_longs * sizeof(unsigned long);
223
224 if (mc_groups == &mc_group_start) {
225 new_groups = kzalloc(nlen, GFP_KERNEL);
226 if (!new_groups)
227 return -ENOMEM;
228 mc_groups = new_groups;
229 *mc_groups = mc_group_start;
230 } else {
231 new_groups = krealloc(mc_groups, nlen,
232 GFP_KERNEL);
233 if (!new_groups)
234 return -ENOMEM;
235 mc_groups = new_groups;
236 for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
237 mc_groups[mc_groups_longs + i] = 0;
238 }
239 mc_groups_longs = new_longs;
240 }
241 } while (!fits);
242
243 for (i = id; i < id + n_groups; i++)
244 set_bit(i, mc_groups);
245 *first_id = id;
246 return 0;
247}
248
249static struct genl_family genl_ctrl;
250
251static int genl_validate_assign_mc_groups(struct genl_family *family)
252{
253 int first_id;
254 int n_groups = family->n_mcgrps;
Geert Uytterhoeven0f0e2152013-11-23 13:01:50 +0100255 int err = 0, i;
Johannes Berg2a94fe42013-11-19 15:19:39 +0100256 bool groups_allocated = false;
257
258 if (!n_groups)
259 return 0;
260
261 for (i = 0; i < n_groups; i++) {
262 const struct genl_multicast_group *grp = &family->mcgrps[i];
263
264 if (WARN_ON(grp->name[0] == '\0'))
265 return -EINVAL;
266 if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL))
267 return -EINVAL;
268 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700269
Johannes Berge5dcecb2013-11-19 15:19:32 +0100270 /* special-case our own group and hacks */
Johannes Berg2a94fe42013-11-19 15:19:39 +0100271 if (family == &genl_ctrl) {
272 first_id = GENL_ID_CTRL;
273 BUG_ON(n_groups != 1);
274 } else if (strcmp(family->name, "NET_DM") == 0) {
275 first_id = 1;
276 BUG_ON(n_groups != 1);
Johannes Berg5e53e682013-11-24 21:09:26 +0100277 } else if (family->id == GENL_ID_VFS_DQUOT) {
Johannes Berg2a94fe42013-11-19 15:19:39 +0100278 first_id = GENL_ID_VFS_DQUOT;
279 BUG_ON(n_groups != 1);
Johannes Berg5e53e682013-11-24 21:09:26 +0100280 } else if (family->id == GENL_ID_PMCRAID) {
281 first_id = GENL_ID_PMCRAID;
282 BUG_ON(n_groups != 1);
Johannes Berg2a94fe42013-11-19 15:19:39 +0100283 } else {
284 groups_allocated = true;
285 err = genl_allocate_reserve_groups(n_groups, &first_id);
286 if (err)
287 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700288 }
289
Johannes Berg2a94fe42013-11-19 15:19:39 +0100290 family->mcgrp_offset = first_id;
291
Randy Dunlap85405912020-08-22 16:40:15 -0700292 /* if still initializing, can't and don't need to realloc bitmaps */
Johannes Berg2a94fe42013-11-19 15:19:39 +0100293 if (!init_net.genl_sock)
294 return 0;
295
Johannes Berg134e6372009-07-10 09:51:34 +0000296 if (family->netnsok) {
297 struct net *net;
298
Johannes Bergd136f1b2009-09-12 03:03:15 +0000299 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000300 rcu_read_lock();
301 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000302 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000303 mc_groups_longs * BITS_PER_LONG);
304 if (err) {
305 /*
306 * No need to roll back, can only fail if
307 * memory allocation fails and then the
308 * number of _possible_ groups has been
309 * increased on some sockets which is ok.
310 */
Johannes Berg2a94fe42013-11-19 15:19:39 +0100311 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000312 }
313 }
314 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000315 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000316 } else {
317 err = netlink_change_ngroups(init_net.genl_sock,
318 mc_groups_longs * BITS_PER_LONG);
Johannes Berg134e6372009-07-10 09:51:34 +0000319 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700320
Johannes Berg2a94fe42013-11-19 15:19:39 +0100321 if (groups_allocated && err) {
322 for (i = 0; i < family->n_mcgrps; i++)
323 clear_bit(family->mcgrp_offset + i, mc_groups);
324 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700325
Thomas Graf79d310d2007-07-24 15:34:53 -0700326 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700327}
Thomas Graf79dc43862007-07-24 15:32:46 -0700328
Johannes Berg2ae0f172016-10-24 14:40:04 +0200329static void genl_unregister_mc_groups(const struct genl_family *family)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700330{
Johannes Berg2a94fe42013-11-19 15:19:39 +0100331 struct net *net;
332 int i;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700333
Johannes Berg2a94fe42013-11-19 15:19:39 +0100334 netlink_table_grab();
335 rcu_read_lock();
336 for_each_net_rcu(net) {
337 for (i = 0; i < family->n_mcgrps; i++)
338 __netlink_clear_multicast_users(
339 net->genl_sock, family->mcgrp_offset + i);
340 }
341 rcu_read_unlock();
342 netlink_table_ungrab();
343
344 for (i = 0; i < family->n_mcgrps; i++) {
345 int grp_id = family->mcgrp_offset + i;
346
347 if (grp_id != 1)
348 clear_bit(grp_id, mc_groups);
349 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family,
350 &family->mcgrps[i], grp_id);
351 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700352}
353
Denis ChengRq2f91abd2014-06-02 01:18:01 -0700354static int genl_validate_ops(const struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100355{
Johannes Bergd91824c2013-11-14 17:14:44 +0100356 int i, j;
Thomas Graf482a8522005-11-10 02:25:56 +0100357
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700358 if (WARN_ON(family->n_ops && !family->ops) ||
359 WARN_ON(family->n_small_ops && !family->small_ops))
Johannes Berg568508a2013-11-15 14:19:08 +0100360 return -EINVAL;
361
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700362 for (i = 0; i < genl_get_cmd_cnt(family); i++) {
363 struct genl_ops op;
Johannes Berg568508a2013-11-15 14:19:08 +0100364
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700365 genl_get_cmd_by_index(i, family, &op);
366 if (op.dumpit == NULL && op.doit == NULL)
Johannes Bergd91824c2013-11-14 17:14:44 +0100367 return -EINVAL;
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700368 for (j = i + 1; j < genl_get_cmd_cnt(family); j++) {
369 struct genl_ops op2;
370
371 genl_get_cmd_by_index(j, family, &op2);
372 if (op.cmd == op2.cmd)
Johannes Bergd91824c2013-11-14 17:14:44 +0100373 return -EINVAL;
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700374 }
Thomas Graf482a8522005-11-10 02:25:56 +0100375 }
376
Johannes Bergd91824c2013-11-14 17:14:44 +0100377 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100378}
Thomas Graf482a8522005-11-10 02:25:56 +0100379
380/**
Johannes Berg489111e2016-10-24 14:40:03 +0200381 * genl_register_family - register a generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +0100382 * @family: generic netlink family
383 *
384 * Registers the specified family after validating it first. Only one
385 * family may be registered with the same family name or identifier.
Thomas Graf482a8522005-11-10 02:25:56 +0100386 *
Johannes Berg489111e2016-10-24 14:40:03 +0200387 * The family's ops, multicast groups and module pointer must already
388 * be assigned.
Johannes Berg568508a2013-11-15 14:19:08 +0100389 *
Thomas Graf482a8522005-11-10 02:25:56 +0100390 * Return 0 on success or a negative error code.
391 */
Johannes Berg489111e2016-10-24 14:40:03 +0200392int genl_register_family(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100393{
Johannes Berga07ea4d2016-10-24 14:40:02 +0200394 int err, i;
Johannes Berg2ae0f172016-10-24 14:40:04 +0200395 int start = GENL_START_ALLOC, end = GENL_MAX_ID;
Thomas Graf482a8522005-11-10 02:25:56 +0100396
Johannes Berg568508a2013-11-15 14:19:08 +0100397 err = genl_validate_ops(family);
398 if (err)
399 return err;
400
Pravin B Shelardef31172013-04-23 07:48:30 +0000401 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100402
403 if (genl_family_find_byname(family->name)) {
404 err = -EEXIST;
405 goto errout_locked;
406 }
407
Johannes Berg2ae0f172016-10-24 14:40:04 +0200408 /*
409 * Sadly, a few cases need to be special-cased
410 * due to them having previously abused the API
411 * and having used their family ID also as their
412 * multicast group ID, so we use reserved IDs
413 * for both to be sure we can do that mapping.
414 */
Johannes Berga07ea4d2016-10-24 14:40:02 +0200415 if (family == &genl_ctrl) {
Johannes Berg2ae0f172016-10-24 14:40:04 +0200416 /* and this needs to be special for initial family lookups */
417 start = end = GENL_ID_CTRL;
418 } else if (strcmp(family->name, "pmcraid") == 0) {
419 start = end = GENL_ID_PMCRAID;
420 } else if (strcmp(family->name, "VFS_DQUOT") == 0) {
421 start = end = GENL_ID_VFS_DQUOT;
Thomas Graf482a8522005-11-10 02:25:56 +0100422 }
423
Marcel Holtmann4e43df32019-04-24 22:18:53 +0200424 family->id = idr_alloc_cyclic(&genl_fam_idr, family,
425 start, end + 1, GFP_KERNEL);
Wei Yongjun22ca9042016-11-01 14:45:52 +0000426 if (family->id < 0) {
427 err = family->id;
Cong Wangbf64ff42020-06-27 00:12:24 -0700428 goto errout_locked;
Wei Yongjun22ca9042016-11-01 14:45:52 +0000429 }
Johannes Berg2a94fe42013-11-19 15:19:39 +0100430
Johannes Berg2ae0f172016-10-24 14:40:04 +0200431 err = genl_validate_assign_mc_groups(family);
432 if (err)
433 goto errout_remove;
434
Pravin B Shelardef31172013-04-23 07:48:30 +0000435 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100436
Johannes Berg2a94fe42013-11-19 15:19:39 +0100437 /* send all events */
438 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
439 for (i = 0; i < family->n_mcgrps; i++)
440 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
441 &family->mcgrps[i], family->mcgrp_offset + i);
Thomas Graf482a8522005-11-10 02:25:56 +0100442
443 return 0;
444
Johannes Berg2ae0f172016-10-24 14:40:04 +0200445errout_remove:
446 idr_remove(&genl_fam_idr, family->id);
Thomas Graf482a8522005-11-10 02:25:56 +0100447errout_locked:
Pravin B Shelardef31172013-04-23 07:48:30 +0000448 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100449 return err;
450}
Johannes Berg489111e2016-10-24 14:40:03 +0200451EXPORT_SYMBOL(genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100452
453/**
454 * genl_unregister_family - unregister generic netlink family
455 * @family: generic netlink family
456 *
457 * Unregisters the specified family.
458 *
459 * Returns 0 on success or a negative error code.
460 */
Johannes Berg2ae0f172016-10-24 14:40:04 +0200461int genl_unregister_family(const struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100462{
Pravin B Shelardef31172013-04-23 07:48:30 +0000463 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100464
pravin shelar0e82c762016-10-28 16:01:41 -0700465 if (!genl_family_find_byid(family->id)) {
Johannes Berg2ae0f172016-10-24 14:40:04 +0200466 genl_unlock_all();
467 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100468 }
469
Johannes Berg2ae0f172016-10-24 14:40:04 +0200470 genl_unregister_mc_groups(family);
Thomas Graf482a8522005-11-10 02:25:56 +0100471
Johannes Berg2ae0f172016-10-24 14:40:04 +0200472 idr_remove(&genl_fam_idr, family->id);
473
474 up_write(&cb_lock);
475 wait_event(genl_sk_destructing_waitq,
476 atomic_read(&genl_sk_destructing_cnt) == 0);
477 genl_unlock();
478
Johannes Berg2ae0f172016-10-24 14:40:04 +0200479 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
480
481 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100482}
Changli Gao416c2f92010-07-25 20:46:01 +0000483EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100484
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500485/**
486 * genlmsg_put - Add generic netlink header to netlink message
487 * @skb: socket buffer holding the message
Eric W. Biederman15e47302012-09-07 20:12:54 +0000488 * @portid: netlink portid the message is addressed to
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500489 * @seq: sequence number (usually the one of the sender)
490 * @family: generic netlink family
Ben Hutchings2c530402012-07-10 10:55:09 +0000491 * @flags: netlink message flags
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500492 * @cmd: generic netlink command
493 *
494 * Returns pointer to user specific header
495 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000496void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg2ae0f172016-10-24 14:40:04 +0200497 const struct genl_family *family, int flags, u8 cmd)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500498{
499 struct nlmsghdr *nlh;
500 struct genlmsghdr *hdr;
501
Eric W. Biederman15e47302012-09-07 20:12:54 +0000502 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500503 family->hdrsize, flags);
504 if (nlh == NULL)
505 return NULL;
506
507 hdr = nlmsg_data(nlh);
508 hdr->cmd = cmd;
509 hdr->version = family->version;
510 hdr->reserved = 0;
511
512 return (char *) hdr + GENL_HDRLEN;
513}
514EXPORT_SYMBOL(genlmsg_put);
515
Jiri Pirko1927f412019-10-05 20:04:34 +0200516static struct genl_dumpit_info *genl_dumpit_info_alloc(void)
517{
518 return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL);
519}
520
521static void genl_dumpit_info_free(const struct genl_dumpit_info *info)
522{
523 kfree(info);
524}
525
Jiri Pirkoc10e6cf2019-10-05 20:04:35 +0200526static struct nlattr **
527genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
528 struct nlmsghdr *nlh,
529 struct netlink_ext_ack *extack,
530 const struct genl_ops *ops,
531 int hdrlen,
Cong Wangb65ce382020-06-12 00:16:55 -0700532 enum genl_validate_flags no_strict_flag)
Jiri Pirkoc10e6cf2019-10-05 20:04:35 +0200533{
534 enum netlink_validation validate = ops->validate & no_strict_flag ?
535 NL_VALIDATE_LIBERAL :
536 NL_VALIDATE_STRICT;
537 struct nlattr **attrbuf;
538 int err;
539
Jakub Kicinski48526a02020-10-02 14:49:57 -0700540 if (!ops->maxattr)
Michal Kubecekcb0ce182019-10-11 09:40:09 +0200541 return NULL;
542
Jakub Kicinski48526a02020-10-02 14:49:57 -0700543 attrbuf = kmalloc_array(ops->maxattr + 1,
Cong Wangbf64ff42020-06-27 00:12:24 -0700544 sizeof(struct nlattr *), GFP_KERNEL);
545 if (!attrbuf)
546 return ERR_PTR(-ENOMEM);
Jiri Pirkoc10e6cf2019-10-05 20:04:35 +0200547
Jakub Kicinski48526a02020-10-02 14:49:57 -0700548 err = __nlmsg_parse(nlh, hdrlen, attrbuf, ops->maxattr, ops->policy,
549 validate, extack);
Paolo Abeni39f3b412020-02-21 19:42:13 +0100550 if (err) {
Cong Wangbf64ff42020-06-27 00:12:24 -0700551 kfree(attrbuf);
Jiri Pirkoc10e6cf2019-10-05 20:04:35 +0200552 return ERR_PTR(err);
553 }
554 return attrbuf;
555}
556
Cong Wangbf64ff42020-06-27 00:12:24 -0700557static void genl_family_rcv_msg_attrs_free(struct nlattr **attrbuf)
Jiri Pirkoc10e6cf2019-10-05 20:04:35 +0200558{
Cong Wangbf64ff42020-06-27 00:12:24 -0700559 kfree(attrbuf);
Jiri Pirkoc10e6cf2019-10-05 20:04:35 +0200560}
561
Cong Wangc36f0552020-06-02 21:49:10 -0700562struct genl_start_context {
563 const struct genl_family *family;
564 struct nlmsghdr *nlh;
565 struct netlink_ext_ack *extack;
566 const struct genl_ops *ops;
567 int hdrlen;
568};
569
570static int genl_start(struct netlink_callback *cb)
Tom Herbertfc9e50f2015-12-15 15:41:37 -0800571{
Cong Wangc36f0552020-06-02 21:49:10 -0700572 struct genl_start_context *ctx = cb->data;
573 const struct genl_ops *ops = ctx->ops;
574 struct genl_dumpit_info *info;
575 struct nlattr **attrs = NULL;
Tom Herbertfc9e50f2015-12-15 15:41:37 -0800576 int rc = 0;
577
Cong Wangc36f0552020-06-02 21:49:10 -0700578 if (ops->validate & GENL_DONT_VALIDATE_DUMP)
579 goto no_attrs;
580
581 if (ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen))
582 return -EINVAL;
583
584 attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
585 ops, ctx->hdrlen,
Cong Wangb65ce382020-06-12 00:16:55 -0700586 GENL_DONT_VALIDATE_DUMP_STRICT);
Cong Wangc36f0552020-06-02 21:49:10 -0700587 if (IS_ERR(attrs))
588 return PTR_ERR(attrs);
589
590no_attrs:
591 info = genl_dumpit_info_alloc();
592 if (!info) {
Cong Wangbf64ff42020-06-27 00:12:24 -0700593 genl_family_rcv_msg_attrs_free(attrs);
Cong Wangc36f0552020-06-02 21:49:10 -0700594 return -ENOMEM;
595 }
596 info->family = ctx->family;
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700597 info->op = *ops;
Cong Wangc36f0552020-06-02 21:49:10 -0700598 info->attrs = attrs;
599
600 cb->data = info;
Tom Herbertfc9e50f2015-12-15 15:41:37 -0800601 if (ops->start) {
Cong Wangc36f0552020-06-02 21:49:10 -0700602 if (!ctx->family->parallel_ops)
603 genl_lock();
Tom Herbertfc9e50f2015-12-15 15:41:37 -0800604 rc = ops->start(cb);
Cong Wangc36f0552020-06-02 21:49:10 -0700605 if (!ctx->family->parallel_ops)
606 genl_unlock();
607 }
608
609 if (rc) {
Cong Wangbf64ff42020-06-27 00:12:24 -0700610 genl_family_rcv_msg_attrs_free(info->attrs);
Cong Wangc36f0552020-06-02 21:49:10 -0700611 genl_dumpit_info_free(info);
612 cb->data = NULL;
Tom Herbertfc9e50f2015-12-15 15:41:37 -0800613 }
614 return rc;
615}
616
Pravin B Shelar9b963092013-08-23 12:44:55 -0700617static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
618{
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700619 const struct genl_ops *ops = &genl_dumpit_info(cb)->op;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700620 int rc;
621
622 genl_lock();
623 rc = ops->dumpit(skb, cb);
624 genl_unlock();
625 return rc;
626}
627
628static int genl_lock_done(struct netlink_callback *cb)
629{
Jiri Pirko1927f412019-10-05 20:04:34 +0200630 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700631 const struct genl_ops *ops = &info->op;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700632 int rc = 0;
633
634 if (ops->done) {
635 genl_lock();
636 rc = ops->done(cb);
637 genl_unlock();
638 }
Cong Wangbf64ff42020-06-27 00:12:24 -0700639 genl_family_rcv_msg_attrs_free(info->attrs);
Jiri Pirko1927f412019-10-05 20:04:34 +0200640 genl_dumpit_info_free(info);
641 return rc;
642}
643
644static int genl_parallel_done(struct netlink_callback *cb)
645{
646 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700647 const struct genl_ops *ops = &info->op;
Jiri Pirko1927f412019-10-05 20:04:34 +0200648 int rc = 0;
649
650 if (ops->done)
651 rc = ops->done(cb);
Cong Wangbf64ff42020-06-27 00:12:24 -0700652 genl_family_rcv_msg_attrs_free(info->attrs);
Jiri Pirko1927f412019-10-05 20:04:34 +0200653 genl_dumpit_info_free(info);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700654 return rc;
655}
656
Jiri Pirkobe064de2019-10-05 20:04:33 +0200657static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
658 struct sk_buff *skb,
659 struct nlmsghdr *nlh,
660 struct netlink_ext_ack *extack,
661 const struct genl_ops *ops,
662 int hdrlen, struct net *net)
Thomas Graf482a8522005-11-10 02:25:56 +0100663{
Cong Wangc36f0552020-06-02 21:49:10 -0700664 struct genl_start_context ctx;
Jiri Pirkobe064de2019-10-05 20:04:33 +0200665 int err;
Thomas Graf482a8522005-11-10 02:25:56 +0100666
Jiri Pirkobe064de2019-10-05 20:04:33 +0200667 if (!ops->dumpit)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700668 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100669
Cong Wangc36f0552020-06-02 21:49:10 -0700670 ctx.family = family;
671 ctx.nlh = nlh;
672 ctx.extack = extack;
673 ctx.ops = ops;
674 ctx.hdrlen = hdrlen;
Jiri Pirko1927f412019-10-05 20:04:34 +0200675
Jiri Pirkobe064de2019-10-05 20:04:33 +0200676 if (!family->parallel_ops) {
677 struct netlink_dump_control c = {
678 .module = family->module,
Cong Wangc36f0552020-06-02 21:49:10 -0700679 .data = &ctx,
680 .start = genl_start,
Jiri Pirkobe064de2019-10-05 20:04:33 +0200681 .dump = genl_lock_dumpit,
682 .done = genl_lock_done,
683 };
684
685 genl_unlock();
686 err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
687 genl_lock();
Jiri Pirkobe064de2019-10-05 20:04:33 +0200688 } else {
689 struct netlink_dump_control c = {
690 .module = family->module,
Cong Wangc36f0552020-06-02 21:49:10 -0700691 .data = &ctx,
692 .start = genl_start,
Jiri Pirkobe064de2019-10-05 20:04:33 +0200693 .dump = ops->dumpit,
Jiri Pirko1927f412019-10-05 20:04:34 +0200694 .done = genl_parallel_done,
Jiri Pirkobe064de2019-10-05 20:04:33 +0200695 };
696
697 err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
698 }
699
700 return err;
701}
702
703static int genl_family_rcv_msg_doit(const struct genl_family *family,
704 struct sk_buff *skb,
705 struct nlmsghdr *nlh,
706 struct netlink_ext_ack *extack,
707 const struct genl_ops *ops,
708 int hdrlen, struct net *net)
709{
710 struct nlattr **attrbuf;
711 struct genl_info info;
712 int err;
713
714 if (!ops->doit)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700715 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100716
Jiri Pirkoc10e6cf2019-10-05 20:04:35 +0200717 attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
718 ops, hdrlen,
Cong Wangb65ce382020-06-12 00:16:55 -0700719 GENL_DONT_VALIDATE_STRICT);
Jiri Pirkoc10e6cf2019-10-05 20:04:35 +0200720 if (IS_ERR(attrbuf))
721 return PTR_ERR(attrbuf);
Thomas Graf482a8522005-11-10 02:25:56 +0100722
723 info.snd_seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000724 info.snd_portid = NETLINK_CB(skb).portid;
Thomas Graf482a8522005-11-10 02:25:56 +0100725 info.nlhdr = nlh;
726 info.genlhdr = nlmsg_data(nlh);
727 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
Pravin B Shelardef31172013-04-23 07:48:30 +0000728 info.attrs = attrbuf;
Johannes Berg7ab606d2017-04-12 14:34:05 +0200729 info.extack = extack;
Johannes Berg134e6372009-07-10 09:51:34 +0000730 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200731 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100732
Johannes Bergff4c92d2010-10-04 21:14:03 +0200733 if (family->pre_doit) {
734 err = family->pre_doit(ops, skb, &info);
735 if (err)
Wei Yongjun50754d212013-04-26 15:34:16 +0000736 goto out;
Johannes Bergff4c92d2010-10-04 21:14:03 +0200737 }
738
739 err = ops->doit(skb, &info);
740
741 if (family->post_doit)
742 family->post_doit(ops, skb, &info);
743
Wei Yongjun50754d212013-04-26 15:34:16 +0000744out:
Cong Wangbf64ff42020-06-27 00:12:24 -0700745 genl_family_rcv_msg_attrs_free(attrbuf);
Pravin B Shelardef31172013-04-23 07:48:30 +0000746
747 return err;
748}
749
Jiri Pirkobe064de2019-10-05 20:04:33 +0200750static int genl_family_rcv_msg(const struct genl_family *family,
751 struct sk_buff *skb,
752 struct nlmsghdr *nlh,
753 struct netlink_ext_ack *extack)
754{
Jiri Pirkobe064de2019-10-05 20:04:33 +0200755 struct net *net = sock_net(skb->sk);
756 struct genlmsghdr *hdr = nlmsg_data(nlh);
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700757 struct genl_ops op;
Jiri Pirkobe064de2019-10-05 20:04:33 +0200758 int hdrlen;
759
760 /* this family doesn't exist in this netns */
761 if (!family->netnsok && !net_eq(net, &init_net))
762 return -ENOENT;
763
764 hdrlen = GENL_HDRLEN + family->hdrsize;
765 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
766 return -EINVAL;
767
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700768 if (genl_get_cmd(hdr->cmd, family, &op))
Jiri Pirkobe064de2019-10-05 20:04:33 +0200769 return -EOPNOTSUPP;
770
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700771 if ((op.flags & GENL_ADMIN_PERM) &&
Jiri Pirkobe064de2019-10-05 20:04:33 +0200772 !netlink_capable(skb, CAP_NET_ADMIN))
773 return -EPERM;
774
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700775 if ((op.flags & GENL_UNS_ADMIN_PERM) &&
Jiri Pirkobe064de2019-10-05 20:04:33 +0200776 !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
777 return -EPERM;
778
779 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP)
780 return genl_family_rcv_msg_dumpit(family, skb, nlh, extack,
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700781 &op, hdrlen, net);
Jiri Pirkobe064de2019-10-05 20:04:33 +0200782 else
783 return genl_family_rcv_msg_doit(family, skb, nlh, extack,
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700784 &op, hdrlen, net);
Jiri Pirkobe064de2019-10-05 20:04:33 +0200785}
786
Johannes Berg2d4bc932017-04-12 14:34:04 +0200787static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
788 struct netlink_ext_ack *extack)
Pravin B Shelardef31172013-04-23 07:48:30 +0000789{
Johannes Berg2ae0f172016-10-24 14:40:04 +0200790 const struct genl_family *family;
Pravin B Shelardef31172013-04-23 07:48:30 +0000791 int err;
792
793 family = genl_family_find_byid(nlh->nlmsg_type);
794 if (family == NULL)
795 return -ENOENT;
796
797 if (!family->parallel_ops)
798 genl_lock();
799
Johannes Berg7ab606d2017-04-12 14:34:05 +0200800 err = genl_family_rcv_msg(family, skb, nlh, extack);
Pravin B Shelardef31172013-04-23 07:48:30 +0000801
802 if (!family->parallel_ops)
803 genl_unlock();
804
Johannes Bergff4c92d2010-10-04 21:14:03 +0200805 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100806}
807
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700808static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100809{
Pravin B Shelardef31172013-04-23 07:48:30 +0000810 down_read(&cb_lock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700811 netlink_rcv_skb(skb, &genl_rcv_msg);
Pravin B Shelardef31172013-04-23 07:48:30 +0000812 up_read(&cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +0100813}
814
815/**************************************************************************
816 * Controller
817 **************************************************************************/
818
Johannes Berg489111e2016-10-24 14:40:03 +0200819static struct genl_family genl_ctrl;
Thomas Graf17c157c2006-11-14 19:46:02 -0800820
Johannes Berg2ae0f172016-10-24 14:40:04 +0200821static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
Thomas Graf482a8522005-11-10 02:25:56 +0100822 u32 flags, struct sk_buff *skb, u8 cmd)
823{
824 void *hdr;
825
Eric W. Biederman15e47302012-09-07 20:12:54 +0000826 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100827 if (hdr == NULL)
828 return -1;
829
David S. Miller444653f2012-03-29 23:25:11 -0400830 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
831 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
832 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
833 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
834 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
835 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700836
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700837 if (genl_get_cmd_cnt(family)) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800838 struct nlattr *nla_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100839 int i;
Thomas Grafeb328112006-09-18 00:01:59 -0700840
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200841 nla_ops = nla_nest_start_noflag(skb, CTRL_ATTR_OPS);
Thomas Grafe94ef682006-11-23 11:44:37 -0800842 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700843 goto nla_put_failure;
844
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700845 for (i = 0; i < genl_get_cmd_cnt(family); i++) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800846 struct nlattr *nest;
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700847 struct genl_ops op;
848 u32 op_flags;
Johannes Bergf84f7712013-11-14 17:14:45 +0100849
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700850 genl_get_cmd_by_index(i, family, &op);
851 op_flags = op.flags;
852 if (op.dumpit)
Johannes Berg029b2342013-11-18 20:54:58 +0100853 op_flags |= GENL_CMD_CAP_DUMP;
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700854 if (op.doit)
Johannes Berg029b2342013-11-18 20:54:58 +0100855 op_flags |= GENL_CMD_CAP_DO;
Jakub Kicinski48526a02020-10-02 14:49:57 -0700856 if (op.policy)
Johannes Berg029b2342013-11-18 20:54:58 +0100857 op_flags |= GENL_CMD_CAP_HASPOL;
Thomas Grafeb328112006-09-18 00:01:59 -0700858
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200859 nest = nla_nest_start_noflag(skb, i + 1);
Thomas Grafe94ef682006-11-23 11:44:37 -0800860 if (nest == NULL)
861 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700862
Jakub Kicinski0b588af2020-10-02 14:49:53 -0700863 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, op.cmd) ||
Johannes Berg029b2342013-11-18 20:54:58 +0100864 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
David S. Miller444653f2012-03-29 23:25:11 -0400865 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700866
Thomas Grafe94ef682006-11-23 11:44:37 -0800867 nla_nest_end(skb, nest);
868 }
869
870 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700871 }
872
Johannes Berg2a94fe42013-11-19 15:19:39 +0100873 if (family->n_mcgrps) {
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700874 struct nlattr *nla_grps;
Johannes Berg2a94fe42013-11-19 15:19:39 +0100875 int i;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700876
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200877 nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700878 if (nla_grps == NULL)
879 goto nla_put_failure;
880
Johannes Berg2a94fe42013-11-19 15:19:39 +0100881 for (i = 0; i < family->n_mcgrps; i++) {
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700882 struct nlattr *nest;
Johannes Berg2a94fe42013-11-19 15:19:39 +0100883 const struct genl_multicast_group *grp;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700884
Johannes Berg2a94fe42013-11-19 15:19:39 +0100885 grp = &family->mcgrps[i];
886
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200887 nest = nla_nest_start_noflag(skb, i + 1);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700888 if (nest == NULL)
889 goto nla_put_failure;
890
Johannes Berg2a94fe42013-11-19 15:19:39 +0100891 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
892 family->mcgrp_offset + i) ||
David S. Miller444653f2012-03-29 23:25:11 -0400893 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
894 grp->name))
895 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700896
897 nla_nest_end(skb, nest);
898 }
899 nla_nest_end(skb, nla_grps);
900 }
901
Johannes Berg053c0952015-01-16 22:09:00 +0100902 genlmsg_end(skb, hdr);
903 return 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700904
905nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700906 genlmsg_cancel(skb, hdr);
907 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700908}
909
Johannes Berg2ae0f172016-10-24 14:40:04 +0200910static int ctrl_fill_mcgrp_info(const struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +0100911 const struct genl_multicast_group *grp,
912 int grp_id, u32 portid, u32 seq, u32 flags,
913 struct sk_buff *skb, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700914{
915 void *hdr;
916 struct nlattr *nla_grps;
917 struct nlattr *nest;
918
Eric W. Biederman15e47302012-09-07 20:12:54 +0000919 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700920 if (hdr == NULL)
921 return -1;
922
Johannes Bergc2ebb902013-11-19 15:19:36 +0100923 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
924 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
David S. Miller444653f2012-03-29 23:25:11 -0400925 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700926
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200927 nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700928 if (nla_grps == NULL)
929 goto nla_put_failure;
930
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200931 nest = nla_nest_start_noflag(skb, 1);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700932 if (nest == NULL)
933 goto nla_put_failure;
934
Johannes Berg2a94fe42013-11-19 15:19:39 +0100935 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
David S. Miller444653f2012-03-29 23:25:11 -0400936 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
937 grp->name))
938 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700939
940 nla_nest_end(skb, nest);
941 nla_nest_end(skb, nla_grps);
942
Johannes Berg053c0952015-01-16 22:09:00 +0100943 genlmsg_end(skb, hdr);
944 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100945
946nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700947 genlmsg_cancel(skb, hdr);
948 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100949}
950
951static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
952{
Johannes Berg2ae0f172016-10-24 14:40:04 +0200953 int n = 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100954 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000955 struct net *net = sock_net(skb->sk);
Johannes Berg2ae0f172016-10-24 14:40:04 +0200956 int fams_to_skip = cb->args[0];
957 unsigned int id;
Thomas Graf482a8522005-11-10 02:25:56 +0100958
Johannes Berg2ae0f172016-10-24 14:40:04 +0200959 idr_for_each_entry(&genl_fam_idr, rt, id) {
960 if (!rt->netnsok && !net_eq(net, &init_net))
961 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100962
Johannes Berg2ae0f172016-10-24 14:40:04 +0200963 if (n++ < fams_to_skip)
964 continue;
965
966 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
967 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Stanislaw Gruszka1d2a6a52017-03-22 16:08:33 +0100968 skb, CTRL_CMD_NEWFAMILY) < 0) {
969 n--;
Johannes Berg2ae0f172016-10-24 14:40:04 +0200970 break;
Stanislaw Gruszka1d2a6a52017-03-22 16:08:33 +0100971 }
Thomas Graf482a8522005-11-10 02:25:56 +0100972 }
973
Johannes Berg2ae0f172016-10-24 14:40:04 +0200974 cb->args[0] = n;
Thomas Graf482a8522005-11-10 02:25:56 +0100975 return skb->len;
976}
977
Johannes Berg2ae0f172016-10-24 14:40:04 +0200978static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000979 u32 portid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100980{
981 struct sk_buff *skb;
982 int err;
983
Thomas Graf339bf982006-11-10 14:10:15 -0800984 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100985 if (skb == NULL)
986 return ERR_PTR(-ENOBUFS);
987
Eric W. Biederman15e47302012-09-07 20:12:54 +0000988 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100989 if (err < 0) {
990 nlmsg_free(skb);
991 return ERR_PTR(err);
992 }
993
994 return skb;
995}
996
Johannes Berg2a94fe42013-11-19 15:19:39 +0100997static struct sk_buff *
Johannes Berg2ae0f172016-10-24 14:40:04 +0200998ctrl_build_mcgrp_msg(const struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +0100999 const struct genl_multicast_group *grp,
1000 int grp_id, u32 portid, int seq, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -07001001{
1002 struct sk_buff *skb;
1003 int err;
1004
1005 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1006 if (skb == NULL)
1007 return ERR_PTR(-ENOBUFS);
1008
Johannes Berg2a94fe42013-11-19 15:19:39 +01001009 err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
1010 seq, 0, skb, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -07001011 if (err < 0) {
1012 nlmsg_free(skb);
1013 return ERR_PTR(err);
1014 }
1015
1016 return skb;
1017}
1018
Jakub Kicinskia4bb4f52020-10-02 14:50:00 -07001019static const struct nla_policy ctrl_policy_family[] = {
Thomas Graf482a8522005-11-10 02:25:56 +01001020 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -07001021 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
1022 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +01001023};
1024
1025static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
1026{
1027 struct sk_buff *msg;
Johannes Berg2ae0f172016-10-24 14:40:04 +02001028 const struct genl_family *res = NULL;
Thomas Graf482a8522005-11-10 02:25:56 +01001029 int err = -EINVAL;
1030
1031 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
1032 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
1033 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +00001034 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +01001035 }
1036
1037 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -07001038 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +01001039
Thomas Graf5176f912006-08-26 20:13:18 -07001040 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +01001041 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -05001042#ifdef CONFIG_MODULES
1043 if (res == NULL) {
1044 genl_unlock();
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +02001045 up_read(&cb_lock);
Neil Hormane9412c32012-05-29 09:30:41 +00001046 request_module("net-pf-%d-proto-%d-family-%s",
Stephen Hemmingerfa843092011-12-28 13:48:55 -05001047 PF_NETLINK, NETLINK_GENERIC, name);
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +02001048 down_read(&cb_lock);
Stephen Hemmingerfa843092011-12-28 13:48:55 -05001049 genl_lock();
1050 res = genl_family_find_byname(name);
1051 }
1052#endif
Johannes Berg134e6372009-07-10 09:51:34 +00001053 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +01001054 }
1055
Johannes Berg134e6372009-07-10 09:51:34 +00001056 if (res == NULL)
1057 return err;
1058
1059 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
1060 /* family doesn't exist here */
1061 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +01001062 }
1063
Eric W. Biederman15e47302012-09-07 20:12:54 +00001064 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
Johannes Berg2dbba6f2007-07-18 15:47:52 -07001065 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +00001066 if (IS_ERR(msg))
1067 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +01001068
Johannes Berg134e6372009-07-10 09:51:34 +00001069 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +01001070}
1071
Johannes Berg2ae0f172016-10-24 14:40:04 +02001072static int genl_ctrl_event(int event, const struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001073 const struct genl_multicast_group *grp,
1074 int grp_id)
Thomas Graf482a8522005-11-10 02:25:56 +01001075{
1076 struct sk_buff *msg;
1077
Johannes Berg134e6372009-07-10 09:51:34 +00001078 /* genl is still initialising */
1079 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +01001080 return 0;
1081
1082 switch (event) {
1083 case CTRL_CMD_NEWFAMILY:
1084 case CTRL_CMD_DELFAMILY:
Johannes Bergc2ebb902013-11-19 15:19:36 +01001085 WARN_ON(grp);
Johannes Berg134e6372009-07-10 09:51:34 +00001086 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -07001087 break;
1088 case CTRL_CMD_NEWMCAST_GRP:
1089 case CTRL_CMD_DELMCAST_GRP:
Johannes Bergc2ebb902013-11-19 15:19:36 +01001090 BUG_ON(!grp);
Johannes Berg2a94fe42013-11-19 15:19:39 +01001091 msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +01001092 break;
Johannes Berg134e6372009-07-10 09:51:34 +00001093 default:
1094 return -EINVAL;
1095 }
1096
1097 if (IS_ERR(msg))
1098 return PTR_ERR(msg);
1099
1100 if (!family->netnsok) {
Johannes Berg68eb5502013-11-19 15:19:38 +01001101 genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001102 0, GFP_KERNEL);
Johannes Berg134e6372009-07-10 09:51:34 +00001103 } else {
1104 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +01001105 genlmsg_multicast_allns(&genl_ctrl, msg, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001106 0, GFP_ATOMIC);
Johannes Berg134e6372009-07-10 09:51:34 +00001107 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +01001108 }
1109
1110 return 0;
1111}
1112
Jakub Kicinskiadc84842020-10-02 14:49:55 -07001113struct ctrl_dump_policy_ctx {
1114 struct netlink_policy_dump_state *state;
Johannes Berg50a896cf2020-10-03 10:44:45 +02001115 const struct genl_family *rt;
1116 unsigned int opidx;
Jakub Kicinskiadc84842020-10-02 14:49:55 -07001117 u16 fam_id;
Johannes Berg50a896cf2020-10-03 10:44:45 +02001118 u8 policies:1;
Jakub Kicinskiadc84842020-10-02 14:49:55 -07001119};
1120
Jakub Kicinskia4bb4f52020-10-02 14:50:00 -07001121static const struct nla_policy ctrl_policy_policy[] = {
1122 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
1123 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
1124 .len = GENL_NAMSIZ - 1 },
1125};
1126
Jakub Kicinski78ade612020-10-02 14:49:56 -07001127static int ctrl_dumppolicy_start(struct netlink_callback *cb)
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001128{
Jakub Kicinski8e1ed282020-10-02 14:49:59 -07001129 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
Jakub Kicinskiadc84842020-10-02 14:49:55 -07001130 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
Jakub Kicinski8e1ed282020-10-02 14:49:59 -07001131 struct nlattr **tb = info->attrs;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001132 const struct genl_family *rt;
Johannes Berg50a896cf2020-10-03 10:44:45 +02001133 struct genl_ops op;
1134 int err, i;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001135
Jakub Kicinskiadc84842020-10-02 14:49:55 -07001136 BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
1137
Jakub Kicinski78ade612020-10-02 14:49:56 -07001138 if (!tb[CTRL_ATTR_FAMILY_ID] && !tb[CTRL_ATTR_FAMILY_NAME])
1139 return -EINVAL;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001140
Jakub Kicinski78ade612020-10-02 14:49:56 -07001141 if (tb[CTRL_ATTR_FAMILY_ID]) {
1142 ctx->fam_id = nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
1143 } else {
1144 rt = genl_family_find_byname(
1145 nla_data(tb[CTRL_ATTR_FAMILY_NAME]));
1146 if (!rt)
1147 return -ENOENT;
1148 ctx->fam_id = rt->id;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001149 }
1150
Jakub Kicinskiadc84842020-10-02 14:49:55 -07001151 rt = genl_family_find_byid(ctx->fam_id);
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001152 if (!rt)
1153 return -ENOENT;
1154
Johannes Berg50a896cf2020-10-03 10:44:45 +02001155 ctx->rt = rt;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001156
Johannes Berg50a896cf2020-10-03 10:44:45 +02001157 for (i = 0; i < genl_get_cmd_cnt(rt); i++) {
1158 genl_get_cmd_by_index(i, rt, &op);
1159
1160 if (op.policy) {
1161 err = netlink_policy_dump_add_policy(&ctx->state,
1162 op.policy,
1163 op.maxattr);
1164 if (err)
1165 return err;
1166 }
1167 }
1168
1169 if (!ctx->state)
1170 return -ENODATA;
1171 return 0;
Jakub Kicinski78ade612020-10-02 14:49:56 -07001172}
1173
Johannes Bergaa85ee52020-10-03 10:44:44 +02001174static void *ctrl_dumppolicy_prep(struct sk_buff *skb,
1175 struct netlink_callback *cb)
1176{
1177 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1178 void *hdr;
1179
1180 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1181 cb->nlh->nlmsg_seq, &genl_ctrl,
1182 NLM_F_MULTI, CTRL_CMD_GETPOLICY);
1183 if (!hdr)
1184 return NULL;
1185
1186 if (nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, ctx->fam_id))
1187 return NULL;
1188
1189 return hdr;
1190}
1191
Johannes Berg50a896cf2020-10-03 10:44:45 +02001192static int ctrl_dumppolicy_put_op(struct sk_buff *skb,
1193 struct netlink_callback *cb,
1194 struct genl_ops *op)
1195{
1196 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1197 struct nlattr *nest_pol, *nest_op;
1198 void *hdr;
1199 int idx;
1200
1201 /* skip if we have nothing to show */
1202 if (!op->policy)
1203 return 0;
1204 if (!op->doit &&
1205 (!op->dumpit || op->validate & GENL_DONT_VALIDATE_DUMP))
1206 return 0;
1207
1208 hdr = ctrl_dumppolicy_prep(skb, cb);
1209 if (!hdr)
1210 return -ENOBUFS;
1211
1212 nest_pol = nla_nest_start(skb, CTRL_ATTR_OP_POLICY);
1213 if (!nest_pol)
1214 goto err;
1215
1216 nest_op = nla_nest_start(skb, op->cmd);
1217 if (!nest_op)
1218 goto err;
1219
1220 /* for now both do/dump are always the same */
1221 idx = netlink_policy_dump_get_policy_idx(ctx->state,
1222 op->policy,
1223 op->maxattr);
1224
1225 if (op->doit && nla_put_u32(skb, CTRL_ATTR_POLICY_DO, idx))
1226 goto err;
1227
1228 if (op->dumpit && !(op->validate & GENL_DONT_VALIDATE_DUMP) &&
1229 nla_put_u32(skb, CTRL_ATTR_POLICY_DUMP, idx))
1230 goto err;
1231
1232 nla_nest_end(skb, nest_op);
1233 nla_nest_end(skb, nest_pol);
1234 genlmsg_end(skb, hdr);
1235
1236 return 0;
1237err:
1238 genlmsg_cancel(skb, hdr);
1239 return -ENOBUFS;
1240}
1241
Jakub Kicinski78ade612020-10-02 14:49:56 -07001242static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb)
1243{
1244 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
Johannes Berg50a896cf2020-10-03 10:44:45 +02001245 void *hdr;
1246
1247 if (!ctx->policies) {
1248 while (ctx->opidx < genl_get_cmd_cnt(ctx->rt)) {
1249 struct genl_ops op;
1250
1251 genl_get_cmd_by_index(ctx->opidx, ctx->rt, &op);
1252
1253 if (ctrl_dumppolicy_put_op(skb, cb, &op))
1254 return skb->len;
1255
1256 ctx->opidx++;
1257 }
1258
1259 /* completed with the per-op policy index list */
1260 ctx->policies = true;
1261 }
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001262
Jakub Kicinskiadc84842020-10-02 14:49:55 -07001263 while (netlink_policy_dump_loop(ctx->state)) {
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001264 struct nlattr *nest;
1265
Johannes Bergaa85ee52020-10-03 10:44:44 +02001266 hdr = ctrl_dumppolicy_prep(skb, cb);
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001267 if (!hdr)
1268 goto nla_put_failure;
1269
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001270 nest = nla_nest_start(skb, CTRL_ATTR_POLICY);
1271 if (!nest)
1272 goto nla_put_failure;
1273
Jakub Kicinskiadc84842020-10-02 14:49:55 -07001274 if (netlink_policy_dump_write(skb, ctx->state))
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001275 goto nla_put_failure;
1276
1277 nla_nest_end(skb, nest);
1278
1279 genlmsg_end(skb, hdr);
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001280 }
1281
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001282 return skb->len;
Johannes Berg50a896cf2020-10-03 10:44:45 +02001283
1284nla_put_failure:
1285 genlmsg_cancel(skb, hdr);
1286 return skb->len;
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001287}
1288
Johannes Berg949ca6b2020-10-02 09:46:04 +02001289static int ctrl_dumppolicy_done(struct netlink_callback *cb)
1290{
Jakub Kicinskiadc84842020-10-02 14:49:55 -07001291 struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1292
1293 netlink_policy_dump_free(ctx->state);
Johannes Berg949ca6b2020-10-02 09:46:04 +02001294 return 0;
1295}
1296
stephen hemminger12d8de62016-08-31 15:22:00 -07001297static const struct genl_ops genl_ctrl_ops[] = {
Johannes Bergc53ed742013-11-19 15:19:31 +01001298 {
1299 .cmd = CTRL_CMD_GETFAMILY,
Johannes Bergef6243a2019-04-26 14:07:31 +02001300 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jakub Kicinskia4bb4f52020-10-02 14:50:00 -07001301 .policy = ctrl_policy_family,
1302 .maxattr = ARRAY_SIZE(ctrl_policy_family) - 1,
Johannes Bergc53ed742013-11-19 15:19:31 +01001303 .doit = ctrl_getfamily,
1304 .dumpit = ctrl_dumpfamily,
Johannes Bergc53ed742013-11-19 15:19:31 +01001305 },
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001306 {
1307 .cmd = CTRL_CMD_GETPOLICY,
Jakub Kicinskia4bb4f52020-10-02 14:50:00 -07001308 .policy = ctrl_policy_policy,
1309 .maxattr = ARRAY_SIZE(ctrl_policy_policy) - 1,
Jakub Kicinski78ade612020-10-02 14:49:56 -07001310 .start = ctrl_dumppolicy_start,
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001311 .dumpit = ctrl_dumppolicy,
Johannes Berg949ca6b2020-10-02 09:46:04 +02001312 .done = ctrl_dumppolicy_done,
Johannes Bergd07dcf9a2020-04-30 22:13:12 +02001313 },
Thomas Graf482a8522005-11-10 02:25:56 +01001314};
1315
stephen hemminger12d8de62016-08-31 15:22:00 -07001316static const struct genl_multicast_group genl_ctrl_groups[] = {
Johannes Berg2a94fe42013-11-19 15:19:39 +01001317 { .name = "notify", },
Johannes Berg2dbba6f2007-07-18 15:47:52 -07001318};
1319
Johannes Berg56989f62016-10-24 14:40:05 +02001320static struct genl_family genl_ctrl __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001321 .module = THIS_MODULE,
1322 .ops = genl_ctrl_ops,
1323 .n_ops = ARRAY_SIZE(genl_ctrl_ops),
1324 .mcgrps = genl_ctrl_groups,
1325 .n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
1326 .id = GENL_ID_CTRL,
1327 .name = "nlctrl",
1328 .version = 0x2,
Johannes Berg489111e2016-10-24 14:40:03 +02001329 .netnsok = true,
1330};
1331
Johannes Berg134e6372009-07-10 09:51:34 +00001332static int __net_init genl_pernet_init(struct net *net)
1333{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001334 struct netlink_kernel_cfg cfg = {
1335 .input = genl_rcv,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001336 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001337 };
1338
Johannes Berg134e6372009-07-10 09:51:34 +00001339 /* we'll bump the group number right afterwards */
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001340 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
Johannes Berg134e6372009-07-10 09:51:34 +00001341
1342 if (!net->genl_sock && net_eq(net, &init_net))
1343 panic("GENL: Cannot initialize generic netlink\n");
1344
1345 if (!net->genl_sock)
1346 return -ENOMEM;
1347
1348 return 0;
1349}
1350
1351static void __net_exit genl_pernet_exit(struct net *net)
1352{
1353 netlink_kernel_release(net->genl_sock);
1354 net->genl_sock = NULL;
1355}
1356
1357static struct pernet_operations genl_pernet_ops = {
1358 .init = genl_pernet_init,
1359 .exit = genl_pernet_exit,
1360};
1361
Thomas Graf482a8522005-11-10 02:25:56 +01001362static int __init genl_init(void)
1363{
Johannes Berg2ae0f172016-10-24 14:40:04 +02001364 int err;
Thomas Graf482a8522005-11-10 02:25:56 +01001365
Johannes Berg489111e2016-10-24 14:40:03 +02001366 err = genl_register_family(&genl_ctrl);
Thomas Graf482a8522005-11-10 02:25:56 +01001367 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +00001368 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +01001369
Johannes Berg134e6372009-07-10 09:51:34 +00001370 err = register_pernet_subsys(&genl_pernet_ops);
1371 if (err)
1372 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +01001373
1374 return 0;
1375
Johannes Berg134e6372009-07-10 09:51:34 +00001376problem:
Thomas Graf482a8522005-11-10 02:25:56 +01001377 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +01001378}
1379
Daniel Lezcanoc62e7ac2020-07-15 09:41:18 +02001380core_initcall(genl_init);
Thomas Graf482a8522005-11-10 02:25:56 +01001381
Eric W. Biederman15e47302012-09-07 20:12:54 +00001382static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
Johannes Berg134e6372009-07-10 09:51:34 +00001383 gfp_t flags)
1384{
1385 struct sk_buff *tmp;
1386 struct net *net, *prev = NULL;
Nicolas Dichtelcb9f7a92018-02-06 14:48:32 +01001387 bool delivered = false;
Johannes Berg134e6372009-07-10 09:51:34 +00001388 int err;
1389
1390 for_each_net_rcu(net) {
1391 if (prev) {
1392 tmp = skb_clone(skb, flags);
1393 if (!tmp) {
1394 err = -ENOMEM;
1395 goto error;
1396 }
1397 err = nlmsg_multicast(prev->genl_sock, tmp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001398 portid, group, flags);
Nicolas Dichtelcb9f7a92018-02-06 14:48:32 +01001399 if (!err)
1400 delivered = true;
1401 else if (err != -ESRCH)
Johannes Berg134e6372009-07-10 09:51:34 +00001402 goto error;
1403 }
1404
1405 prev = net;
1406 }
1407
Nicolas Dichtelcb9f7a92018-02-06 14:48:32 +01001408 err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1409 if (!err)
1410 delivered = true;
1411 else if (err != -ESRCH)
Nicolas Dichtel02a23852018-03-14 21:10:23 +01001412 return err;
Nicolas Dichtelcb9f7a92018-02-06 14:48:32 +01001413 return delivered ? 0 : -ESRCH;
Johannes Berg134e6372009-07-10 09:51:34 +00001414 error:
1415 kfree_skb(skb);
1416 return err;
1417}
1418
Johannes Berg2ae0f172016-10-24 14:40:04 +02001419int genlmsg_multicast_allns(const struct genl_family *family,
1420 struct sk_buff *skb, u32 portid,
1421 unsigned int group, gfp_t flags)
Johannes Berg134e6372009-07-10 09:51:34 +00001422{
Johannes Berg220815a2013-11-21 18:17:04 +01001423 if (WARN_ON_ONCE(group >= family->n_mcgrps))
Johannes Berg2a94fe42013-11-19 15:19:39 +01001424 return -EINVAL;
1425 group = family->mcgrp_offset + group;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001426 return genlmsg_mcast(skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001427}
1428EXPORT_SYMBOL(genlmsg_multicast_allns);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001429
Johannes Berg2ae0f172016-10-24 14:40:04 +02001430void genl_notify(const struct genl_family *family, struct sk_buff *skb,
Jiri Benc92c14d92015-09-22 18:56:43 +02001431 struct genl_info *info, u32 group, gfp_t flags)
Pravin B Shelar263ba612011-11-10 19:14:37 -08001432{
Jiri Benc92c14d92015-09-22 18:56:43 +02001433 struct net *net = genl_info_net(info);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001434 struct sock *sk = net->genl_sock;
1435 int report = 0;
1436
Jiri Benc92c14d92015-09-22 18:56:43 +02001437 if (info->nlhdr)
1438 report = nlmsg_report(info->nlhdr);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001439
Johannes Berg220815a2013-11-21 18:17:04 +01001440 if (WARN_ON_ONCE(group >= family->n_mcgrps))
Johannes Berg2a94fe42013-11-19 15:19:39 +01001441 return;
1442 group = family->mcgrp_offset + group;
Jiri Benc92c14d92015-09-22 18:56:43 +02001443 nlmsg_notify(sk, skb, info->snd_portid, group, report, flags);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001444}
1445EXPORT_SYMBOL(genl_notify);