blob: 36e3a86cacf6c00b0dee3a49438101a74ee470ec [file] [log] [blame]
Thomas Graf482a8522005-11-10 02:25:56 +01001/*
2 * NETLINK Generic Netlink Family
3 *
4 * Authors: Jamal Hadi Salim
5 * Thomas Graf <tgraf@suug.ch>
Johannes Berg2dbba6f2007-07-18 15:47:52 -07006 * Johannes Berg <johannes@sipsolutions.net>
Thomas Graf482a8522005-11-10 02:25:56 +01007 */
8
Thomas Graf482a8522005-11-10 02:25:56 +01009#include <linux/module.h>
10#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Thomas Graf482a8522005-11-10 02:25:56 +010012#include <linux/errno.h>
13#include <linux/types.h>
14#include <linux/socket.h>
15#include <linux/string.h>
16#include <linux/skbuff.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080017#include <linux/mutex.h>
Johannes Berg2dbba6f2007-07-18 15:47:52 -070018#include <linux/bitmap.h>
Pravin B Shelardef31172013-04-23 07:48:30 +000019#include <linux/rwsem.h>
Thomas Graf482a8522005-11-10 02:25:56 +010020#include <net/sock.h>
21#include <net/genetlink.h>
22
Ingo Molnar14cc3e22006-03-26 01:37:14 -080023static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
Pravin B Shelardef31172013-04-23 07:48:30 +000024static DECLARE_RWSEM(cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010025
James Chapmanf408e0c2010-04-02 06:19:05 +000026void genl_lock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010027{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080028 mutex_lock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010029}
James Chapmanf408e0c2010-04-02 06:19:05 +000030EXPORT_SYMBOL(genl_lock);
Thomas Graf482a8522005-11-10 02:25:56 +010031
James Chapmanf408e0c2010-04-02 06:19:05 +000032void genl_unlock(void)
Thomas Graf482a8522005-11-10 02:25:56 +010033{
Ingo Molnar14cc3e22006-03-26 01:37:14 -080034 mutex_unlock(&genl_mutex);
Thomas Graf482a8522005-11-10 02:25:56 +010035}
James Chapmanf408e0c2010-04-02 06:19:05 +000036EXPORT_SYMBOL(genl_unlock);
Thomas Graf482a8522005-11-10 02:25:56 +010037
WANG Cong320f5ea2012-07-24 13:44:01 +080038#ifdef CONFIG_LOCKDEP
Pravin B Shelar86b13092011-11-10 19:14:51 -080039int lockdep_genl_is_held(void)
40{
41 return lockdep_is_held(&genl_mutex);
42}
43EXPORT_SYMBOL(lockdep_genl_is_held);
44#endif
45
Pravin B Shelardef31172013-04-23 07:48:30 +000046static void genl_lock_all(void)
47{
48 down_write(&cb_lock);
49 genl_lock();
50}
51
52static void genl_unlock_all(void)
53{
54 genl_unlock();
55 up_write(&cb_lock);
56}
57
Thomas Graf482a8522005-11-10 02:25:56 +010058#define GENL_FAM_TAB_SIZE 16
59#define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1)
60
61static struct list_head family_ht[GENL_FAM_TAB_SIZE];
Johannes Berg2dbba6f2007-07-18 15:47:52 -070062/*
63 * Bitmap of multicast groups that are currently in use.
64 *
65 * To avoid an allocation at boot of just one unsigned long,
66 * declare it global instead.
67 * Bit 0 is marked as already used since group 0 is invalid.
Johannes Berge5dcecb2013-11-19 15:19:32 +010068 * Bit 1 is marked as already used since the drop-monitor code
69 * abuses the API and thinks it can statically use group 1.
70 * That group will typically conflict with other groups that
71 * any proper users use.
Johannes Berg2ecf7532013-11-19 15:19:33 +010072 * Bit 17 is marked as already used since the VFS quota code
73 * also abused this API and relied on family == group ID, we
74 * cater to that by giving it a static family and group ID.
Johannes Berg2dbba6f2007-07-18 15:47:52 -070075 */
Johannes Berg2ecf7532013-11-19 15:19:33 +010076static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_VFS_DQUOT);
Johannes Berg2dbba6f2007-07-18 15:47:52 -070077static unsigned long *mc_groups = &mc_group_start;
78static unsigned long mc_groups_longs = 1;
Thomas Graf482a8522005-11-10 02:25:56 +010079
Johannes Bergc2ebb902013-11-19 15:19:36 +010080static int genl_ctrl_event(int event, struct genl_family *family,
81 struct genl_multicast_group *grp);
Thomas Graf482a8522005-11-10 02:25:56 +010082
83static inline unsigned int genl_family_hash(unsigned int id)
84{
85 return id & GENL_FAM_TAB_MASK;
86}
87
88static inline struct list_head *genl_family_chain(unsigned int id)
89{
90 return &family_ht[genl_family_hash(id)];
91}
92
93static struct genl_family *genl_family_find_byid(unsigned int id)
94{
95 struct genl_family *f;
96
97 list_for_each_entry(f, genl_family_chain(id), family_list)
98 if (f->id == id)
99 return f;
100
101 return NULL;
102}
103
104static struct genl_family *genl_family_find_byname(char *name)
105{
106 struct genl_family *f;
107 int i;
108
109 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
110 list_for_each_entry(f, genl_family_chain(i), family_list)
111 if (strcmp(f->name, name) == 0)
112 return f;
113
114 return NULL;
115}
116
Johannes Bergf84f7712013-11-14 17:14:45 +0100117static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100118{
Johannes Bergd91824c2013-11-14 17:14:44 +0100119 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100120
Johannes Bergd91824c2013-11-14 17:14:44 +0100121 for (i = 0; i < family->n_ops; i++)
122 if (family->ops[i].cmd == cmd)
123 return &family->ops[i];
Thomas Graf482a8522005-11-10 02:25:56 +0100124
125 return NULL;
126}
127
128/* Of course we are going to have problems once we hit
129 * 2^16 alive types, but that can only happen by year 2K
130*/
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000131static u16 genl_generate_id(void)
Thomas Graf482a8522005-11-10 02:25:56 +0100132{
Krishna Kumar988ade62009-10-14 19:55:07 +0000133 static u16 id_gen_idx = GENL_MIN_ID;
134 int i;
Thomas Graf482a8522005-11-10 02:25:56 +0100135
Krishna Kumar988ade62009-10-14 19:55:07 +0000136 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
Johannes Berg2ecf7532013-11-19 15:19:33 +0100137 if (id_gen_idx != GENL_ID_VFS_DQUOT &&
138 !genl_family_find_byid(id_gen_idx))
Krishna Kumar988ade62009-10-14 19:55:07 +0000139 return id_gen_idx;
140 if (++id_gen_idx > GENL_MAX_ID)
Thomas Graf482a8522005-11-10 02:25:56 +0100141 id_gen_idx = GENL_MIN_ID;
Krishna Kumar988ade62009-10-14 19:55:07 +0000142 }
Thomas Graf482a8522005-11-10 02:25:56 +0100143
Krishna Kumar988ade62009-10-14 19:55:07 +0000144 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100145}
146
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700147static struct genl_multicast_group notify_grp;
148
149/**
150 * genl_register_mc_group - register a multicast group
151 *
152 * Registers the specified multicast group and notifies userspace
153 * about the new group.
154 *
155 * Returns 0 on success or a negative error code.
156 *
157 * @family: The generic netlink family the group shall be registered for.
158 * @grp: The group to register, must have a name.
159 */
160int genl_register_mc_group(struct genl_family *family,
161 struct genl_multicast_group *grp)
162{
163 int id;
164 unsigned long *new_groups;
Brian Haleyb1f57192009-09-04 20:36:52 -0700165 int err = 0;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700166
167 BUG_ON(grp->name[0] == '\0');
Masatake YAMATOf1e79e22013-03-19 01:47:27 +0000168 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700169
Pravin B Shelardef31172013-04-23 07:48:30 +0000170 genl_lock_all();
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700171
Johannes Berge5dcecb2013-11-19 15:19:32 +0100172 /* special-case our own group and hacks */
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700173 if (grp == &notify_grp)
174 id = GENL_ID_CTRL;
Johannes Berge5dcecb2013-11-19 15:19:32 +0100175 else if (strcmp(family->name, "NET_DM") == 0)
176 id = 1;
Johannes Berg2ecf7532013-11-19 15:19:33 +0100177 else if (strcmp(family->name, "VFS_DQUOT") == 0)
178 id = GENL_ID_VFS_DQUOT;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700179 else
180 id = find_first_zero_bit(mc_groups,
181 mc_groups_longs * BITS_PER_LONG);
182
183
184 if (id >= mc_groups_longs * BITS_PER_LONG) {
185 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
186
187 if (mc_groups == &mc_group_start) {
188 new_groups = kzalloc(nlen, GFP_KERNEL);
189 if (!new_groups) {
190 err = -ENOMEM;
191 goto out;
192 }
193 mc_groups = new_groups;
194 *mc_groups = mc_group_start;
195 } else {
196 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
197 if (!new_groups) {
198 err = -ENOMEM;
199 goto out;
200 }
201 mc_groups = new_groups;
202 mc_groups[mc_groups_longs] = 0;
203 }
204 mc_groups_longs++;
205 }
206
Johannes Berg134e6372009-07-10 09:51:34 +0000207 if (family->netnsok) {
208 struct net *net;
209
Johannes Bergd136f1b2009-09-12 03:03:15 +0000210 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000211 rcu_read_lock();
212 for_each_net_rcu(net) {
Johannes Bergd136f1b2009-09-12 03:03:15 +0000213 err = __netlink_change_ngroups(net->genl_sock,
Johannes Berg134e6372009-07-10 09:51:34 +0000214 mc_groups_longs * BITS_PER_LONG);
215 if (err) {
216 /*
217 * No need to roll back, can only fail if
218 * memory allocation fails and then the
219 * number of _possible_ groups has been
220 * increased on some sockets which is ok.
221 */
222 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000223 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000224 goto out;
225 }
226 }
227 rcu_read_unlock();
Johannes Bergd136f1b2009-09-12 03:03:15 +0000228 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000229 } else {
230 err = netlink_change_ngroups(init_net.genl_sock,
231 mc_groups_longs * BITS_PER_LONG);
232 if (err)
233 goto out;
234 }
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700235
236 grp->id = id;
237 set_bit(id, mc_groups);
238 list_add_tail(&grp->list, &family->mcast_groups);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700239
Johannes Bergc2ebb902013-11-19 15:19:36 +0100240 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700241 out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000242 genl_unlock_all();
Thomas Graf79d310d2007-07-24 15:34:53 -0700243 return err;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700244}
245EXPORT_SYMBOL(genl_register_mc_group);
246
Thomas Graf79dc43862007-07-24 15:32:46 -0700247static void __genl_unregister_mc_group(struct genl_family *family,
248 struct genl_multicast_group *grp)
249{
Johannes Berg134e6372009-07-10 09:51:34 +0000250 struct net *net;
Johannes Berg134e6372009-07-10 09:51:34 +0000251
Johannes Bergb8273572009-09-24 15:44:05 -0700252 netlink_table_grab();
Johannes Berg134e6372009-07-10 09:51:34 +0000253 rcu_read_lock();
254 for_each_net_rcu(net)
Johannes Bergb8273572009-09-24 15:44:05 -0700255 __netlink_clear_multicast_users(net->genl_sock, grp->id);
Johannes Berg134e6372009-07-10 09:51:34 +0000256 rcu_read_unlock();
Johannes Bergb8273572009-09-24 15:44:05 -0700257 netlink_table_ungrab();
Johannes Berg134e6372009-07-10 09:51:34 +0000258
Johannes Berge5dcecb2013-11-19 15:19:32 +0100259 if (grp->id != 1)
260 clear_bit(grp->id, mc_groups);
Thomas Graf79dc43862007-07-24 15:32:46 -0700261 list_del(&grp->list);
Johannes Bergc2ebb902013-11-19 15:19:36 +0100262 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family, grp);
Thomas Graf79dc43862007-07-24 15:32:46 -0700263 grp->id = 0;
Thomas Graf79dc43862007-07-24 15:32:46 -0700264}
265
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700266static void genl_unregister_mc_groups(struct genl_family *family)
267{
268 struct genl_multicast_group *grp, *tmp;
269
270 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
Thomas Graf79dc43862007-07-24 15:32:46 -0700271 __genl_unregister_mc_group(family, grp);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700272}
273
Johannes Berg568508a2013-11-15 14:19:08 +0100274static int genl_validate_ops(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100275{
Johannes Berg568508a2013-11-15 14:19:08 +0100276 const struct genl_ops *ops = family->ops;
277 unsigned int n_ops = family->n_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100278 int i, j;
Thomas Graf482a8522005-11-10 02:25:56 +0100279
Johannes Berg568508a2013-11-15 14:19:08 +0100280 if (WARN_ON(n_ops && !ops))
281 return -EINVAL;
282
283 if (!n_ops)
284 return 0;
285
Johannes Bergd91824c2013-11-14 17:14:44 +0100286 for (i = 0; i < n_ops; i++) {
287 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
288 return -EINVAL;
289 for (j = i + 1; j < n_ops; j++)
290 if (ops[i].cmd == ops[j].cmd)
291 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100292 }
293
Johannes Bergd91824c2013-11-14 17:14:44 +0100294 /* family is not registered yet, so no locking needed */
295 family->ops = ops;
296 family->n_ops = n_ops;
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800297
Johannes Bergd91824c2013-11-14 17:14:44 +0100298 return 0;
Thomas Graf482a8522005-11-10 02:25:56 +0100299}
Thomas Graf482a8522005-11-10 02:25:56 +0100300
301/**
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700302 * __genl_register_family - register a generic netlink family
Thomas Graf482a8522005-11-10 02:25:56 +0100303 * @family: generic netlink family
304 *
305 * Registers the specified family after validating it first. Only one
306 * family may be registered with the same family name or identifier.
307 * The family id may equal GENL_ID_GENERATE causing an unique id to
308 * be automatically generated and assigned.
309 *
Johannes Berg568508a2013-11-15 14:19:08 +0100310 * The family's ops array must already be assigned, you can use the
311 * genl_register_family_with_ops() helper function.
312 *
Thomas Graf482a8522005-11-10 02:25:56 +0100313 * Return 0 on success or a negative error code.
314 */
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700315int __genl_register_family(struct genl_family *family)
Thomas Graf482a8522005-11-10 02:25:56 +0100316{
317 int err = -EINVAL;
318
319 if (family->id && family->id < GENL_MIN_ID)
320 goto errout;
321
322 if (family->id > GENL_MAX_ID)
323 goto errout;
324
Johannes Berg568508a2013-11-15 14:19:08 +0100325 err = genl_validate_ops(family);
326 if (err)
327 return err;
328
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700329 INIT_LIST_HEAD(&family->mcast_groups);
Thomas Graf482a8522005-11-10 02:25:56 +0100330
Pravin B Shelardef31172013-04-23 07:48:30 +0000331 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100332
333 if (genl_family_find_byname(family->name)) {
334 err = -EEXIST;
335 goto errout_locked;
336 }
337
Thomas Graf482a8522005-11-10 02:25:56 +0100338 if (family->id == GENL_ID_GENERATE) {
339 u16 newid = genl_generate_id();
340
341 if (!newid) {
342 err = -ENOMEM;
343 goto errout_locked;
344 }
345
346 family->id = newid;
Krishna Kumar93860b02009-10-14 19:54:53 +0000347 } else if (genl_family_find_byid(family->id)) {
348 err = -EEXIST;
349 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100350 }
351
Pravin B Shelardef31172013-04-23 07:48:30 +0000352 if (family->maxattr && !family->parallel_ops) {
Thomas Graf482a8522005-11-10 02:25:56 +0100353 family->attrbuf = kmalloc((family->maxattr+1) *
354 sizeof(struct nlattr *), GFP_KERNEL);
355 if (family->attrbuf == NULL) {
356 err = -ENOMEM;
Jamal Hadi Salime200bd82006-02-13 15:51:24 -0800357 goto errout_locked;
Thomas Graf482a8522005-11-10 02:25:56 +0100358 }
359 } else
360 family->attrbuf = NULL;
361
362 list_add_tail(&family->family_list, genl_family_chain(family->id));
Pravin B Shelardef31172013-04-23 07:48:30 +0000363 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100364
Johannes Bergc2ebb902013-11-19 15:19:36 +0100365 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL);
Thomas Graf482a8522005-11-10 02:25:56 +0100366
367 return 0;
368
369errout_locked:
Pravin B Shelardef31172013-04-23 07:48:30 +0000370 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100371errout:
372 return err;
373}
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700374EXPORT_SYMBOL(__genl_register_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100375
376/**
377 * genl_unregister_family - unregister generic netlink family
378 * @family: generic netlink family
379 *
380 * Unregisters the specified family.
381 *
382 * Returns 0 on success or a negative error code.
383 */
384int genl_unregister_family(struct genl_family *family)
385{
386 struct genl_family *rc;
387
Pravin B Shelardef31172013-04-23 07:48:30 +0000388 genl_lock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100389
Pavel Emelyanov910d6c32008-02-12 22:16:33 -0800390 genl_unregister_mc_groups(family);
391
Thomas Graf482a8522005-11-10 02:25:56 +0100392 list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
393 if (family->id != rc->id || strcmp(rc->name, family->name))
394 continue;
395
396 list_del(&rc->family_list);
Johannes Bergd91824c2013-11-14 17:14:44 +0100397 family->n_ops = 0;
Pravin B Shelardef31172013-04-23 07:48:30 +0000398 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100399
Thomas Graf482a8522005-11-10 02:25:56 +0100400 kfree(family->attrbuf);
Johannes Bergc2ebb902013-11-19 15:19:36 +0100401 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL);
Thomas Graf482a8522005-11-10 02:25:56 +0100402 return 0;
403 }
404
Pravin B Shelardef31172013-04-23 07:48:30 +0000405 genl_unlock_all();
Thomas Graf482a8522005-11-10 02:25:56 +0100406
407 return -ENOENT;
408}
Changli Gao416c2f92010-07-25 20:46:01 +0000409EXPORT_SYMBOL(genl_unregister_family);
Thomas Graf482a8522005-11-10 02:25:56 +0100410
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500411/**
412 * genlmsg_put - Add generic netlink header to netlink message
413 * @skb: socket buffer holding the message
Eric W. Biederman15e47302012-09-07 20:12:54 +0000414 * @portid: netlink portid the message is addressed to
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500415 * @seq: sequence number (usually the one of the sender)
416 * @family: generic netlink family
Ben Hutchings2c530402012-07-10 10:55:09 +0000417 * @flags: netlink message flags
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500418 * @cmd: generic netlink command
419 *
420 * Returns pointer to user specific header
421 */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000422void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500423 struct genl_family *family, int flags, u8 cmd)
424{
425 struct nlmsghdr *nlh;
426 struct genlmsghdr *hdr;
427
Eric W. Biederman15e47302012-09-07 20:12:54 +0000428 nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
Denys Vlasenkoa46621a2012-01-30 15:22:06 -0500429 family->hdrsize, flags);
430 if (nlh == NULL)
431 return NULL;
432
433 hdr = nlmsg_data(nlh);
434 hdr->cmd = cmd;
435 hdr->version = family->version;
436 hdr->reserved = 0;
437
438 return (char *) hdr + GENL_HDRLEN;
439}
440EXPORT_SYMBOL(genlmsg_put);
441
Pravin B Shelar9b963092013-08-23 12:44:55 -0700442static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
443{
Johannes Bergf84f7712013-11-14 17:14:45 +0100444 /* our ops are always const - netlink API doesn't propagate that */
445 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700446 int rc;
447
448 genl_lock();
449 rc = ops->dumpit(skb, cb);
450 genl_unlock();
451 return rc;
452}
453
454static int genl_lock_done(struct netlink_callback *cb)
455{
Johannes Bergf84f7712013-11-14 17:14:45 +0100456 /* our ops are always const - netlink API doesn't propagate that */
457 const struct genl_ops *ops = cb->data;
Pravin B Shelar9b963092013-08-23 12:44:55 -0700458 int rc = 0;
459
460 if (ops->done) {
461 genl_lock();
462 rc = ops->done(cb);
463 genl_unlock();
464 }
465 return rc;
466}
467
Pravin B Shelardef31172013-04-23 07:48:30 +0000468static int genl_family_rcv_msg(struct genl_family *family,
469 struct sk_buff *skb,
470 struct nlmsghdr *nlh)
Thomas Graf482a8522005-11-10 02:25:56 +0100471{
Johannes Bergf84f7712013-11-14 17:14:45 +0100472 const struct genl_ops *ops;
Johannes Berg134e6372009-07-10 09:51:34 +0000473 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100474 struct genl_info info;
475 struct genlmsghdr *hdr = nlmsg_data(nlh);
Pravin B Shelardef31172013-04-23 07:48:30 +0000476 struct nlattr **attrbuf;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700477 int hdrlen, err;
Thomas Graf482a8522005-11-10 02:25:56 +0100478
Johannes Berg134e6372009-07-10 09:51:34 +0000479 /* this family doesn't exist in this netns */
480 if (!family->netnsok && !net_eq(net, &init_net))
481 return -ENOENT;
482
Thomas Graf482a8522005-11-10 02:25:56 +0100483 hdrlen = GENL_HDRLEN + family->hdrsize;
484 if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700485 return -EINVAL;
Thomas Graf482a8522005-11-10 02:25:56 +0100486
487 ops = genl_get_cmd(hdr->cmd, family);
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700488 if (ops == NULL)
489 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100490
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700491 if ((ops->flags & GENL_ADMIN_PERM) &&
Eric Parisfd778462012-01-03 12:25:16 -0500492 !capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700493 return -EPERM;
Thomas Graf482a8522005-11-10 02:25:56 +0100494
Pablo Neirae1ee3672013-07-29 12:30:04 +0200495 if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
Pravin B Shelar9b963092013-08-23 12:44:55 -0700496 int rc;
Pravin B Shelardef31172013-04-23 07:48:30 +0000497
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700498 if (ops->dumpit == NULL)
499 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100500
Pravin B Shelar9b963092013-08-23 12:44:55 -0700501 if (!family->parallel_ops) {
502 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700503 .module = family->module,
Johannes Bergf84f7712013-11-14 17:14:45 +0100504 /* we have const, but the netlink API doesn't */
505 .data = (void *)ops,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700506 .dump = genl_lock_dumpit,
507 .done = genl_lock_done,
508 };
509
510 genl_unlock();
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700511 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700512 genl_lock();
513
514 } else {
515 struct netlink_dump_control c = {
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700516 .module = family->module,
Pravin B Shelar9b963092013-08-23 12:44:55 -0700517 .dump = ops->dumpit,
518 .done = ops->done,
519 };
520
Pravin B Shelar33c6b1f2013-08-23 12:45:04 -0700521 rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
Pravin B Shelar9b963092013-08-23 12:44:55 -0700522 }
523
524 return rc;
Thomas Graf482a8522005-11-10 02:25:56 +0100525 }
526
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700527 if (ops->doit == NULL)
528 return -EOPNOTSUPP;
Thomas Graf482a8522005-11-10 02:25:56 +0100529
Pravin B Shelardef31172013-04-23 07:48:30 +0000530 if (family->maxattr && family->parallel_ops) {
531 attrbuf = kmalloc((family->maxattr+1) *
532 sizeof(struct nlattr *), GFP_KERNEL);
533 if (attrbuf == NULL)
534 return -ENOMEM;
535 } else
536 attrbuf = family->attrbuf;
537
538 if (attrbuf) {
539 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
Thomas Graf482a8522005-11-10 02:25:56 +0100540 ops->policy);
541 if (err < 0)
Wei Yongjun50754d212013-04-26 15:34:16 +0000542 goto out;
Thomas Graf482a8522005-11-10 02:25:56 +0100543 }
544
545 info.snd_seq = nlh->nlmsg_seq;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000546 info.snd_portid = NETLINK_CB(skb).portid;
Thomas Graf482a8522005-11-10 02:25:56 +0100547 info.nlhdr = nlh;
548 info.genlhdr = nlmsg_data(nlh);
549 info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
Pravin B Shelardef31172013-04-23 07:48:30 +0000550 info.attrs = attrbuf;
Johannes Berg134e6372009-07-10 09:51:34 +0000551 genl_info_net_set(&info, net);
Johannes Bergff4c92d2010-10-04 21:14:03 +0200552 memset(&info.user_ptr, 0, sizeof(info.user_ptr));
Thomas Graf482a8522005-11-10 02:25:56 +0100553
Johannes Bergff4c92d2010-10-04 21:14:03 +0200554 if (family->pre_doit) {
555 err = family->pre_doit(ops, skb, &info);
556 if (err)
Wei Yongjun50754d212013-04-26 15:34:16 +0000557 goto out;
Johannes Bergff4c92d2010-10-04 21:14:03 +0200558 }
559
560 err = ops->doit(skb, &info);
561
562 if (family->post_doit)
563 family->post_doit(ops, skb, &info);
564
Wei Yongjun50754d212013-04-26 15:34:16 +0000565out:
Pravin B Shelardef31172013-04-23 07:48:30 +0000566 if (family->parallel_ops)
567 kfree(attrbuf);
568
569 return err;
570}
571
572static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
573{
574 struct genl_family *family;
575 int err;
576
577 family = genl_family_find_byid(nlh->nlmsg_type);
578 if (family == NULL)
579 return -ENOENT;
580
581 if (!family->parallel_ops)
582 genl_lock();
583
584 err = genl_family_rcv_msg(family, skb, nlh);
585
586 if (!family->parallel_ops)
587 genl_unlock();
588
Johannes Bergff4c92d2010-10-04 21:14:03 +0200589 return err;
Thomas Graf482a8522005-11-10 02:25:56 +0100590}
591
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700592static void genl_rcv(struct sk_buff *skb)
Thomas Graf482a8522005-11-10 02:25:56 +0100593{
Pravin B Shelardef31172013-04-23 07:48:30 +0000594 down_read(&cb_lock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700595 netlink_rcv_skb(skb, &genl_rcv_msg);
Pravin B Shelardef31172013-04-23 07:48:30 +0000596 up_read(&cb_lock);
Thomas Graf482a8522005-11-10 02:25:56 +0100597}
598
599/**************************************************************************
600 * Controller
601 **************************************************************************/
602
Thomas Graf17c157c2006-11-14 19:46:02 -0800603static struct genl_family genl_ctrl = {
604 .id = GENL_ID_CTRL,
605 .name = "nlctrl",
Jamal Hadi Salim334c29a2006-12-04 19:31:51 -0800606 .version = 0x2,
Thomas Graf17c157c2006-11-14 19:46:02 -0800607 .maxattr = CTRL_ATTR_MAX,
Johannes Berg134e6372009-07-10 09:51:34 +0000608 .netnsok = true,
Thomas Graf17c157c2006-11-14 19:46:02 -0800609};
610
Eric W. Biederman15e47302012-09-07 20:12:54 +0000611static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
Thomas Graf482a8522005-11-10 02:25:56 +0100612 u32 flags, struct sk_buff *skb, u8 cmd)
613{
614 void *hdr;
615
Eric W. Biederman15e47302012-09-07 20:12:54 +0000616 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100617 if (hdr == NULL)
618 return -1;
619
David S. Miller444653f2012-03-29 23:25:11 -0400620 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
621 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
622 nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
623 nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
624 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
625 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700626
Johannes Bergd91824c2013-11-14 17:14:44 +0100627 if (family->n_ops) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800628 struct nlattr *nla_ops;
Johannes Bergd91824c2013-11-14 17:14:44 +0100629 int i;
Thomas Grafeb328112006-09-18 00:01:59 -0700630
Thomas Grafe94ef682006-11-23 11:44:37 -0800631 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
632 if (nla_ops == NULL)
Thomas Grafeb328112006-09-18 00:01:59 -0700633 goto nla_put_failure;
634
Johannes Bergd91824c2013-11-14 17:14:44 +0100635 for (i = 0; i < family->n_ops; i++) {
Thomas Grafe94ef682006-11-23 11:44:37 -0800636 struct nlattr *nest;
Johannes Bergf84f7712013-11-14 17:14:45 +0100637 const struct genl_ops *ops = &family->ops[i];
Johannes Berg029b2342013-11-18 20:54:58 +0100638 u32 op_flags = ops->flags;
Johannes Bergf84f7712013-11-14 17:14:45 +0100639
640 if (ops->dumpit)
Johannes Berg029b2342013-11-18 20:54:58 +0100641 op_flags |= GENL_CMD_CAP_DUMP;
Johannes Bergf84f7712013-11-14 17:14:45 +0100642 if (ops->doit)
Johannes Berg029b2342013-11-18 20:54:58 +0100643 op_flags |= GENL_CMD_CAP_DO;
Johannes Bergf84f7712013-11-14 17:14:45 +0100644 if (ops->policy)
Johannes Berg029b2342013-11-18 20:54:58 +0100645 op_flags |= GENL_CMD_CAP_HASPOL;
Thomas Grafeb328112006-09-18 00:01:59 -0700646
Johannes Bergd91824c2013-11-14 17:14:44 +0100647 nest = nla_nest_start(skb, i + 1);
Thomas Grafe94ef682006-11-23 11:44:37 -0800648 if (nest == NULL)
649 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700650
David S. Miller444653f2012-03-29 23:25:11 -0400651 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
Johannes Berg029b2342013-11-18 20:54:58 +0100652 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
David S. Miller444653f2012-03-29 23:25:11 -0400653 goto nla_put_failure;
Thomas Grafeb328112006-09-18 00:01:59 -0700654
Thomas Grafe94ef682006-11-23 11:44:37 -0800655 nla_nest_end(skb, nest);
656 }
657
658 nla_nest_end(skb, nla_ops);
Thomas Grafeb328112006-09-18 00:01:59 -0700659 }
660
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700661 if (!list_empty(&family->mcast_groups)) {
662 struct genl_multicast_group *grp;
663 struct nlattr *nla_grps;
664 int idx = 1;
665
666 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
667 if (nla_grps == NULL)
668 goto nla_put_failure;
669
670 list_for_each_entry(grp, &family->mcast_groups, list) {
671 struct nlattr *nest;
672
673 nest = nla_nest_start(skb, idx++);
674 if (nest == NULL)
675 goto nla_put_failure;
676
David S. Miller444653f2012-03-29 23:25:11 -0400677 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
678 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
679 grp->name))
680 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700681
682 nla_nest_end(skb, nest);
683 }
684 nla_nest_end(skb, nla_grps);
685 }
686
687 return genlmsg_end(skb, hdr);
688
689nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700690 genlmsg_cancel(skb, hdr);
691 return -EMSGSIZE;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700692}
693
Johannes Bergc2ebb902013-11-19 15:19:36 +0100694static int ctrl_fill_mcgrp_info(struct genl_family *family,
695 struct genl_multicast_group *grp, u32 portid,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700696 u32 seq, u32 flags, struct sk_buff *skb,
697 u8 cmd)
698{
699 void *hdr;
700 struct nlattr *nla_grps;
701 struct nlattr *nest;
702
Eric W. Biederman15e47302012-09-07 20:12:54 +0000703 hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700704 if (hdr == NULL)
705 return -1;
706
Johannes Bergc2ebb902013-11-19 15:19:36 +0100707 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
708 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
David S. Miller444653f2012-03-29 23:25:11 -0400709 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700710
711 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
712 if (nla_grps == NULL)
713 goto nla_put_failure;
714
715 nest = nla_nest_start(skb, 1);
716 if (nest == NULL)
717 goto nla_put_failure;
718
David S. Miller444653f2012-03-29 23:25:11 -0400719 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
720 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
721 grp->name))
722 goto nla_put_failure;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700723
724 nla_nest_end(skb, nest);
725 nla_nest_end(skb, nla_grps);
726
Thomas Graf482a8522005-11-10 02:25:56 +0100727 return genlmsg_end(skb, hdr);
728
729nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700730 genlmsg_cancel(skb, hdr);
731 return -EMSGSIZE;
Thomas Graf482a8522005-11-10 02:25:56 +0100732}
733
734static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
735{
736
737 int i, n = 0;
738 struct genl_family *rt;
Johannes Berg134e6372009-07-10 09:51:34 +0000739 struct net *net = sock_net(skb->sk);
Thomas Graf482a8522005-11-10 02:25:56 +0100740 int chains_to_skip = cb->args[0];
741 int fams_to_skip = cb->args[1];
742
Samir Bellabese1d5a012010-01-07 22:10:56 +0000743 for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
Thomas Graf482a8522005-11-10 02:25:56 +0100744 n = 0;
745 list_for_each_entry(rt, genl_family_chain(i), family_list) {
Johannes Berg134e6372009-07-10 09:51:34 +0000746 if (!rt->netnsok && !net_eq(net, &init_net))
747 continue;
Thomas Graf482a8522005-11-10 02:25:56 +0100748 if (++n < fams_to_skip)
749 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000750 if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
Thomas Graf482a8522005-11-10 02:25:56 +0100751 cb->nlh->nlmsg_seq, NLM_F_MULTI,
752 skb, CTRL_CMD_NEWFAMILY) < 0)
753 goto errout;
754 }
755
756 fams_to_skip = 0;
757 }
758
759errout:
760 cb->args[0] = i;
761 cb->args[1] = n;
762
763 return skb->len;
764}
765
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700766static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000767 u32 portid, int seq, u8 cmd)
Thomas Graf482a8522005-11-10 02:25:56 +0100768{
769 struct sk_buff *skb;
770 int err;
771
Thomas Graf339bf982006-11-10 14:10:15 -0800772 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Thomas Graf482a8522005-11-10 02:25:56 +0100773 if (skb == NULL)
774 return ERR_PTR(-ENOBUFS);
775
Eric W. Biederman15e47302012-09-07 20:12:54 +0000776 err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
Thomas Graf482a8522005-11-10 02:25:56 +0100777 if (err < 0) {
778 nlmsg_free(skb);
779 return ERR_PTR(err);
780 }
781
782 return skb;
783}
784
Johannes Bergc2ebb902013-11-19 15:19:36 +0100785static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_family *family,
786 struct genl_multicast_group *grp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000787 u32 portid, int seq, u8 cmd)
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700788{
789 struct sk_buff *skb;
790 int err;
791
792 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
793 if (skb == NULL)
794 return ERR_PTR(-ENOBUFS);
795
Johannes Bergc2ebb902013-11-19 15:19:36 +0100796 err = ctrl_fill_mcgrp_info(family, grp, portid, seq, 0, skb, cmd);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700797 if (err < 0) {
798 nlmsg_free(skb);
799 return ERR_PTR(err);
800 }
801
802 return skb;
803}
804
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700805static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
Thomas Graf482a8522005-11-10 02:25:56 +0100806 [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 },
Thomas Graf5176f912006-08-26 20:13:18 -0700807 [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
808 .len = GENL_NAMSIZ - 1 },
Thomas Graf482a8522005-11-10 02:25:56 +0100809};
810
811static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
812{
813 struct sk_buff *msg;
814 struct genl_family *res = NULL;
815 int err = -EINVAL;
816
817 if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
818 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
819 res = genl_family_find_byid(id);
Johannes Berg134e6372009-07-10 09:51:34 +0000820 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100821 }
822
823 if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
Thomas Graf5176f912006-08-26 20:13:18 -0700824 char *name;
Thomas Graf482a8522005-11-10 02:25:56 +0100825
Thomas Graf5176f912006-08-26 20:13:18 -0700826 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
Thomas Graf482a8522005-11-10 02:25:56 +0100827 res = genl_family_find_byname(name);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500828#ifdef CONFIG_MODULES
829 if (res == NULL) {
830 genl_unlock();
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200831 up_read(&cb_lock);
Neil Hormane9412c32012-05-29 09:30:41 +0000832 request_module("net-pf-%d-proto-%d-family-%s",
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500833 PF_NETLINK, NETLINK_GENERIC, name);
Stanislaw Gruszkac74f2b22013-07-26 11:00:10 +0200834 down_read(&cb_lock);
Stephen Hemmingerfa843092011-12-28 13:48:55 -0500835 genl_lock();
836 res = genl_family_find_byname(name);
837 }
838#endif
Johannes Berg134e6372009-07-10 09:51:34 +0000839 err = -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100840 }
841
Johannes Berg134e6372009-07-10 09:51:34 +0000842 if (res == NULL)
843 return err;
844
845 if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
846 /* family doesn't exist here */
847 return -ENOENT;
Thomas Graf482a8522005-11-10 02:25:56 +0100848 }
849
Eric W. Biederman15e47302012-09-07 20:12:54 +0000850 msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700851 CTRL_CMD_NEWFAMILY);
Johannes Berg134e6372009-07-10 09:51:34 +0000852 if (IS_ERR(msg))
853 return PTR_ERR(msg);
Thomas Graf482a8522005-11-10 02:25:56 +0100854
Johannes Berg134e6372009-07-10 09:51:34 +0000855 return genlmsg_reply(msg, info);
Thomas Graf482a8522005-11-10 02:25:56 +0100856}
857
Johannes Bergc2ebb902013-11-19 15:19:36 +0100858static int genl_ctrl_event(int event, struct genl_family *family,
859 struct genl_multicast_group *grp)
Thomas Graf482a8522005-11-10 02:25:56 +0100860{
861 struct sk_buff *msg;
862
Johannes Berg134e6372009-07-10 09:51:34 +0000863 /* genl is still initialising */
864 if (!init_net.genl_sock)
Thomas Graf482a8522005-11-10 02:25:56 +0100865 return 0;
866
867 switch (event) {
868 case CTRL_CMD_NEWFAMILY:
869 case CTRL_CMD_DELFAMILY:
Johannes Bergc2ebb902013-11-19 15:19:36 +0100870 WARN_ON(grp);
Johannes Berg134e6372009-07-10 09:51:34 +0000871 msg = ctrl_build_family_msg(family, 0, 0, event);
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700872 break;
873 case CTRL_CMD_NEWMCAST_GRP:
874 case CTRL_CMD_DELMCAST_GRP:
Johannes Bergc2ebb902013-11-19 15:19:36 +0100875 BUG_ON(!grp);
876 msg = ctrl_build_mcgrp_msg(family, grp, 0, 0, event);
Thomas Graf482a8522005-11-10 02:25:56 +0100877 break;
Johannes Berg134e6372009-07-10 09:51:34 +0000878 default:
879 return -EINVAL;
880 }
881
882 if (IS_ERR(msg))
883 return PTR_ERR(msg);
884
885 if (!family->netnsok) {
Johannes Berg68eb5502013-11-19 15:19:38 +0100886 genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
Johannes Berg134e6372009-07-10 09:51:34 +0000887 GENL_ID_CTRL, GFP_KERNEL);
888 } else {
889 rcu_read_lock();
Johannes Berg68eb5502013-11-19 15:19:38 +0100890 genlmsg_multicast_allns(&genl_ctrl, msg, 0,
891 GENL_ID_CTRL, GFP_ATOMIC);
Johannes Berg134e6372009-07-10 09:51:34 +0000892 rcu_read_unlock();
Thomas Graf482a8522005-11-10 02:25:56 +0100893 }
894
895 return 0;
896}
897
Johannes Bergc53ed742013-11-19 15:19:31 +0100898static struct genl_ops genl_ctrl_ops[] = {
899 {
900 .cmd = CTRL_CMD_GETFAMILY,
901 .doit = ctrl_getfamily,
902 .dumpit = ctrl_dumpfamily,
903 .policy = ctrl_policy,
904 },
Thomas Graf482a8522005-11-10 02:25:56 +0100905};
906
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700907static struct genl_multicast_group notify_grp = {
908 .name = "notify",
909};
910
Johannes Berg134e6372009-07-10 09:51:34 +0000911static int __net_init genl_pernet_init(struct net *net)
912{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000913 struct netlink_kernel_cfg cfg = {
914 .input = genl_rcv,
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000915 .flags = NL_CFG_F_NONROOT_RECV,
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000916 };
917
Johannes Berg134e6372009-07-10 09:51:34 +0000918 /* we'll bump the group number right afterwards */
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000919 net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
Johannes Berg134e6372009-07-10 09:51:34 +0000920
921 if (!net->genl_sock && net_eq(net, &init_net))
922 panic("GENL: Cannot initialize generic netlink\n");
923
924 if (!net->genl_sock)
925 return -ENOMEM;
926
927 return 0;
928}
929
930static void __net_exit genl_pernet_exit(struct net *net)
931{
932 netlink_kernel_release(net->genl_sock);
933 net->genl_sock = NULL;
934}
935
936static struct pernet_operations genl_pernet_ops = {
937 .init = genl_pernet_init,
938 .exit = genl_pernet_exit,
939};
940
Thomas Graf482a8522005-11-10 02:25:56 +0100941static int __init genl_init(void)
942{
943 int i, err;
944
945 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
946 INIT_LIST_HEAD(&family_ht[i]);
947
Johannes Bergc53ed742013-11-19 15:19:31 +0100948 err = genl_register_family_with_ops(&genl_ctrl, genl_ctrl_ops);
Thomas Graf482a8522005-11-10 02:25:56 +0100949 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000950 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100951
Johannes Berg134e6372009-07-10 09:51:34 +0000952 err = register_pernet_subsys(&genl_pernet_ops);
953 if (err)
954 goto problem;
Thomas Graf482a8522005-11-10 02:25:56 +0100955
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700956 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
957 if (err < 0)
Johannes Berg134e6372009-07-10 09:51:34 +0000958 goto problem;
Johannes Berg2dbba6f2007-07-18 15:47:52 -0700959
Thomas Graf482a8522005-11-10 02:25:56 +0100960 return 0;
961
Johannes Berg134e6372009-07-10 09:51:34 +0000962problem:
Thomas Graf482a8522005-11-10 02:25:56 +0100963 panic("GENL: Cannot register controller: %d\n", err);
Thomas Graf482a8522005-11-10 02:25:56 +0100964}
965
966subsys_initcall(genl_init);
967
Eric W. Biederman15e47302012-09-07 20:12:54 +0000968static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
Johannes Berg134e6372009-07-10 09:51:34 +0000969 gfp_t flags)
970{
971 struct sk_buff *tmp;
972 struct net *net, *prev = NULL;
973 int err;
974
975 for_each_net_rcu(net) {
976 if (prev) {
977 tmp = skb_clone(skb, flags);
978 if (!tmp) {
979 err = -ENOMEM;
980 goto error;
981 }
982 err = nlmsg_multicast(prev->genl_sock, tmp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000983 portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +0000984 if (err)
985 goto error;
986 }
987
988 prev = net;
989 }
990
Eric W. Biederman15e47302012-09-07 20:12:54 +0000991 return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +0000992 error:
993 kfree_skb(skb);
994 return err;
995}
996
Johannes Berg68eb5502013-11-19 15:19:38 +0100997int genlmsg_multicast_allns(struct genl_family *family, struct sk_buff *skb,
998 u32 portid, unsigned int group, gfp_t flags)
Johannes Berg134e6372009-07-10 09:51:34 +0000999{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001000 return genlmsg_mcast(skb, portid, group, flags);
Johannes Berg134e6372009-07-10 09:51:34 +00001001}
1002EXPORT_SYMBOL(genlmsg_multicast_allns);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001003
Johannes Berg68eb5502013-11-19 15:19:38 +01001004void genl_notify(struct genl_family *family,
1005 struct sk_buff *skb, struct net *net, u32 portid, u32 group,
Pravin B Shelar263ba612011-11-10 19:14:37 -08001006 struct nlmsghdr *nlh, gfp_t flags)
1007{
1008 struct sock *sk = net->genl_sock;
1009 int report = 0;
1010
1011 if (nlh)
1012 report = nlmsg_report(nlh);
1013
Eric W. Biederman15e47302012-09-07 20:12:54 +00001014 nlmsg_notify(sk, skb, portid, group, report, flags);
Pravin B Shelar263ba612011-11-10 19:14:37 -08001015}
1016EXPORT_SYMBOL(genl_notify);