Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 2 | /* |
| 3 | * NETLINK Generic Netlink Family |
| 4 | * |
| 5 | * Authors: Jamal Hadi Salim |
| 6 | * Thomas Graf <tgraf@suug.ch> |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 7 | * Johannes Berg <johannes@sipsolutions.net> |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 13 | #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 Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 19 | #include <linux/bitmap.h> |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 20 | #include <linux/rwsem.h> |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 21 | #include <linux/idr.h> |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 22 | #include <net/sock.h> |
| 23 | #include <net/genetlink.h> |
| 24 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 25 | static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */ |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 26 | static DECLARE_RWSEM(cb_lock); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 27 | |
Johannes Berg | ee1c2442 | 2015-01-16 11:37:14 +0100 | [diff] [blame] | 28 | atomic_t genl_sk_destructing_cnt = ATOMIC_INIT(0); |
| 29 | DECLARE_WAIT_QUEUE_HEAD(genl_sk_destructing_waitq); |
| 30 | |
James Chapman | f408e0c | 2010-04-02 06:19:05 +0000 | [diff] [blame] | 31 | void genl_lock(void) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 32 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 33 | mutex_lock(&genl_mutex); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 34 | } |
James Chapman | f408e0c | 2010-04-02 06:19:05 +0000 | [diff] [blame] | 35 | EXPORT_SYMBOL(genl_lock); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 36 | |
James Chapman | f408e0c | 2010-04-02 06:19:05 +0000 | [diff] [blame] | 37 | void genl_unlock(void) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 38 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 39 | mutex_unlock(&genl_mutex); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 40 | } |
James Chapman | f408e0c | 2010-04-02 06:19:05 +0000 | [diff] [blame] | 41 | EXPORT_SYMBOL(genl_unlock); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 42 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 43 | static void genl_lock_all(void) |
| 44 | { |
| 45 | down_write(&cb_lock); |
| 46 | genl_lock(); |
| 47 | } |
| 48 | |
| 49 | static void genl_unlock_all(void) |
| 50 | { |
| 51 | genl_unlock(); |
| 52 | up_write(&cb_lock); |
| 53 | } |
| 54 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 55 | static DEFINE_IDR(genl_fam_idr); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 56 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Bitmap of multicast groups that are currently in use. |
| 59 | * |
| 60 | * To avoid an allocation at boot of just one unsigned long, |
| 61 | * declare it global instead. |
| 62 | * Bit 0 is marked as already used since group 0 is invalid. |
Johannes Berg | e5dcecb | 2013-11-19 15:19:32 +0100 | [diff] [blame] | 63 | * Bit 1 is marked as already used since the drop-monitor code |
| 64 | * abuses the API and thinks it can statically use group 1. |
| 65 | * That group will typically conflict with other groups that |
| 66 | * any proper users use. |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 67 | * Bit 16 is marked as used since it's used for generic netlink |
| 68 | * and the code no longer marks pre-reserved IDs as used. |
Johannes Berg | 2ecf753 | 2013-11-19 15:19:33 +0100 | [diff] [blame] | 69 | * Bit 17 is marked as already used since the VFS quota code |
| 70 | * also abused this API and relied on family == group ID, we |
| 71 | * cater to that by giving it a static family and group ID. |
Johannes Berg | 5e53e68 | 2013-11-24 21:09:26 +0100 | [diff] [blame] | 72 | * Bit 18 is marked as already used since the PMCRAID driver |
| 73 | * did the same thing as the VFS quota code (maybe copied?) |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 74 | */ |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 75 | static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) | |
Johannes Berg | 5e53e68 | 2013-11-24 21:09:26 +0100 | [diff] [blame] | 76 | BIT(GENL_ID_VFS_DQUOT) | |
| 77 | BIT(GENL_ID_PMCRAID); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 78 | static unsigned long *mc_groups = &mc_group_start; |
| 79 | static unsigned long mc_groups_longs = 1; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 80 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 81 | static int genl_ctrl_event(int event, const struct genl_family *family, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 82 | const struct genl_multicast_group *grp, |
| 83 | int grp_id); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 84 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 85 | static const struct genl_family *genl_family_find_byid(unsigned int id) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 86 | { |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 87 | return idr_find(&genl_fam_idr, id); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 90 | static const struct genl_family *genl_family_find_byname(char *name) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 91 | { |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 92 | const struct genl_family *family; |
| 93 | unsigned int id; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 94 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 95 | idr_for_each_entry(&genl_fam_idr, family, id) |
| 96 | if (strcmp(family->name, name) == 0) |
| 97 | return family; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 98 | |
| 99 | return NULL; |
| 100 | } |
| 101 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 102 | static int genl_get_cmd_cnt(const struct genl_family *family) |
| 103 | { |
| 104 | return family->n_ops + family->n_small_ops; |
| 105 | } |
| 106 | |
| 107 | static void genl_op_from_full(const struct genl_family *family, |
| 108 | unsigned int i, struct genl_ops *op) |
| 109 | { |
| 110 | *op = family->ops[i]; |
Jakub Kicinski | 48526a0 | 2020-10-02 14:49:57 -0700 | [diff] [blame] | 111 | |
| 112 | if (!op->maxattr) |
| 113 | op->maxattr = family->maxattr; |
| 114 | if (!op->policy) |
| 115 | op->policy = family->policy; |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jakub Kicinski | e992a6e | 2020-10-03 10:44:46 +0200 | [diff] [blame] | 118 | static int genl_get_cmd_full(u32 cmd, const struct genl_family *family, |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 119 | struct genl_ops *op) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 120 | { |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 121 | int i; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 122 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 123 | for (i = 0; i < family->n_ops; i++) |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 124 | if (family->ops[i].cmd == cmd) { |
| 125 | genl_op_from_full(family, i, op); |
| 126 | return 0; |
| 127 | } |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 128 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 129 | return -ENOENT; |
| 130 | } |
| 131 | |
| 132 | static void genl_op_from_small(const struct genl_family *family, |
| 133 | unsigned int i, struct genl_ops *op) |
| 134 | { |
| 135 | memset(op, 0, sizeof(*op)); |
| 136 | op->doit = family->small_ops[i].doit; |
| 137 | op->dumpit = family->small_ops[i].dumpit; |
| 138 | op->cmd = family->small_ops[i].cmd; |
| 139 | op->internal_flags = family->small_ops[i].internal_flags; |
| 140 | op->flags = family->small_ops[i].flags; |
| 141 | op->validate = family->small_ops[i].validate; |
Jakub Kicinski | 48526a0 | 2020-10-02 14:49:57 -0700 | [diff] [blame] | 142 | |
| 143 | op->maxattr = family->maxattr; |
| 144 | op->policy = family->policy; |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jakub Kicinski | e992a6e | 2020-10-03 10:44:46 +0200 | [diff] [blame] | 147 | static int genl_get_cmd_small(u32 cmd, const struct genl_family *family, |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 148 | struct genl_ops *op) |
| 149 | { |
| 150 | int i; |
| 151 | |
| 152 | for (i = 0; i < family->n_small_ops; i++) |
| 153 | if (family->small_ops[i].cmd == cmd) { |
| 154 | genl_op_from_small(family, i, op); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | return -ENOENT; |
| 159 | } |
| 160 | |
Jakub Kicinski | e992a6e | 2020-10-03 10:44:46 +0200 | [diff] [blame] | 161 | static int genl_get_cmd(u32 cmd, const struct genl_family *family, |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 162 | struct genl_ops *op) |
| 163 | { |
| 164 | if (!genl_get_cmd_full(cmd, family, op)) |
| 165 | return 0; |
| 166 | return genl_get_cmd_small(cmd, family, op); |
| 167 | } |
| 168 | |
| 169 | static void genl_get_cmd_by_index(unsigned int i, |
| 170 | const struct genl_family *family, |
| 171 | struct genl_ops *op) |
| 172 | { |
| 173 | if (i < family->n_ops) |
| 174 | genl_op_from_full(family, i, op); |
| 175 | else if (i < family->n_ops + family->n_small_ops) |
| 176 | genl_op_from_small(family, i - family->n_ops, op); |
| 177 | else |
| 178 | WARN_ON_ONCE(1); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 181 | static int genl_allocate_reserve_groups(int n_groups, int *first_id) |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 182 | { |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 183 | unsigned long *new_groups; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 184 | int start = 0; |
| 185 | int i; |
| 186 | int id; |
| 187 | bool fits; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 188 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 189 | do { |
| 190 | if (start == 0) |
| 191 | id = find_first_zero_bit(mc_groups, |
| 192 | mc_groups_longs * |
| 193 | BITS_PER_LONG); |
| 194 | else |
| 195 | id = find_next_zero_bit(mc_groups, |
| 196 | mc_groups_longs * BITS_PER_LONG, |
| 197 | start); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 198 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 199 | fits = true; |
| 200 | for (i = id; |
| 201 | i < min_t(int, id + n_groups, |
| 202 | mc_groups_longs * BITS_PER_LONG); |
| 203 | i++) { |
| 204 | if (test_bit(i, mc_groups)) { |
| 205 | start = i; |
| 206 | fits = false; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
David S. Miller | b8e429a | 2016-01-13 10:28:06 -0500 | [diff] [blame] | 211 | if (id + n_groups > mc_groups_longs * BITS_PER_LONG) { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 212 | unsigned long new_longs = mc_groups_longs + |
| 213 | BITS_TO_LONGS(n_groups); |
| 214 | size_t nlen = new_longs * sizeof(unsigned long); |
| 215 | |
| 216 | if (mc_groups == &mc_group_start) { |
| 217 | new_groups = kzalloc(nlen, GFP_KERNEL); |
| 218 | if (!new_groups) |
| 219 | return -ENOMEM; |
| 220 | mc_groups = new_groups; |
| 221 | *mc_groups = mc_group_start; |
| 222 | } else { |
| 223 | new_groups = krealloc(mc_groups, nlen, |
| 224 | GFP_KERNEL); |
| 225 | if (!new_groups) |
| 226 | return -ENOMEM; |
| 227 | mc_groups = new_groups; |
| 228 | for (i = 0; i < BITS_TO_LONGS(n_groups); i++) |
| 229 | mc_groups[mc_groups_longs + i] = 0; |
| 230 | } |
| 231 | mc_groups_longs = new_longs; |
| 232 | } |
| 233 | } while (!fits); |
| 234 | |
| 235 | for (i = id; i < id + n_groups; i++) |
| 236 | set_bit(i, mc_groups); |
| 237 | *first_id = id; |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static struct genl_family genl_ctrl; |
| 242 | |
| 243 | static int genl_validate_assign_mc_groups(struct genl_family *family) |
| 244 | { |
| 245 | int first_id; |
| 246 | int n_groups = family->n_mcgrps; |
Geert Uytterhoeven | 0f0e215 | 2013-11-23 13:01:50 +0100 | [diff] [blame] | 247 | int err = 0, i; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 248 | bool groups_allocated = false; |
| 249 | |
| 250 | if (!n_groups) |
| 251 | return 0; |
| 252 | |
| 253 | for (i = 0; i < n_groups; i++) { |
| 254 | const struct genl_multicast_group *grp = &family->mcgrps[i]; |
| 255 | |
| 256 | if (WARN_ON(grp->name[0] == '\0')) |
| 257 | return -EINVAL; |
| 258 | if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL)) |
| 259 | return -EINVAL; |
| 260 | } |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 261 | |
Johannes Berg | e5dcecb | 2013-11-19 15:19:32 +0100 | [diff] [blame] | 262 | /* special-case our own group and hacks */ |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 263 | if (family == &genl_ctrl) { |
| 264 | first_id = GENL_ID_CTRL; |
| 265 | BUG_ON(n_groups != 1); |
| 266 | } else if (strcmp(family->name, "NET_DM") == 0) { |
| 267 | first_id = 1; |
| 268 | BUG_ON(n_groups != 1); |
Johannes Berg | 5e53e68 | 2013-11-24 21:09:26 +0100 | [diff] [blame] | 269 | } else if (family->id == GENL_ID_VFS_DQUOT) { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 270 | first_id = GENL_ID_VFS_DQUOT; |
| 271 | BUG_ON(n_groups != 1); |
Johannes Berg | 5e53e68 | 2013-11-24 21:09:26 +0100 | [diff] [blame] | 272 | } else if (family->id == GENL_ID_PMCRAID) { |
| 273 | first_id = GENL_ID_PMCRAID; |
| 274 | BUG_ON(n_groups != 1); |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 275 | } else { |
| 276 | groups_allocated = true; |
| 277 | err = genl_allocate_reserve_groups(n_groups, &first_id); |
| 278 | if (err) |
| 279 | return err; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 282 | family->mcgrp_offset = first_id; |
| 283 | |
Randy Dunlap | 8540591 | 2020-08-22 16:40:15 -0700 | [diff] [blame] | 284 | /* if still initializing, can't and don't need to realloc bitmaps */ |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 285 | if (!init_net.genl_sock) |
| 286 | return 0; |
| 287 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 288 | if (family->netnsok) { |
| 289 | struct net *net; |
| 290 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 291 | netlink_table_grab(); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 292 | rcu_read_lock(); |
| 293 | for_each_net_rcu(net) { |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 294 | err = __netlink_change_ngroups(net->genl_sock, |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 295 | mc_groups_longs * BITS_PER_LONG); |
| 296 | if (err) { |
| 297 | /* |
| 298 | * No need to roll back, can only fail if |
| 299 | * memory allocation fails and then the |
| 300 | * number of _possible_ groups has been |
| 301 | * increased on some sockets which is ok. |
| 302 | */ |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 303 | break; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | rcu_read_unlock(); |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 307 | netlink_table_ungrab(); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 308 | } else { |
| 309 | err = netlink_change_ngroups(init_net.genl_sock, |
| 310 | mc_groups_longs * BITS_PER_LONG); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 311 | } |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 312 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 313 | if (groups_allocated && err) { |
| 314 | for (i = 0; i < family->n_mcgrps; i++) |
| 315 | clear_bit(family->mcgrp_offset + i, mc_groups); |
| 316 | } |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 317 | |
Thomas Graf | 79d310d | 2007-07-24 15:34:53 -0700 | [diff] [blame] | 318 | return err; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 319 | } |
Thomas Graf | 79dc4386 | 2007-07-24 15:32:46 -0700 | [diff] [blame] | 320 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 321 | static void genl_unregister_mc_groups(const struct genl_family *family) |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 322 | { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 323 | struct net *net; |
| 324 | int i; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 325 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 326 | netlink_table_grab(); |
| 327 | rcu_read_lock(); |
| 328 | for_each_net_rcu(net) { |
| 329 | for (i = 0; i < family->n_mcgrps; i++) |
| 330 | __netlink_clear_multicast_users( |
| 331 | net->genl_sock, family->mcgrp_offset + i); |
| 332 | } |
| 333 | rcu_read_unlock(); |
| 334 | netlink_table_ungrab(); |
| 335 | |
| 336 | for (i = 0; i < family->n_mcgrps; i++) { |
| 337 | int grp_id = family->mcgrp_offset + i; |
| 338 | |
| 339 | if (grp_id != 1) |
| 340 | clear_bit(grp_id, mc_groups); |
| 341 | genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family, |
| 342 | &family->mcgrps[i], grp_id); |
| 343 | } |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Denis ChengRq | 2f91abd | 2014-06-02 01:18:01 -0700 | [diff] [blame] | 346 | static int genl_validate_ops(const struct genl_family *family) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 347 | { |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 348 | int i, j; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 349 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 350 | if (WARN_ON(family->n_ops && !family->ops) || |
| 351 | WARN_ON(family->n_small_ops && !family->small_ops)) |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame] | 352 | return -EINVAL; |
| 353 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 354 | for (i = 0; i < genl_get_cmd_cnt(family); i++) { |
| 355 | struct genl_ops op; |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame] | 356 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 357 | genl_get_cmd_by_index(i, family, &op); |
| 358 | if (op.dumpit == NULL && op.doit == NULL) |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 359 | return -EINVAL; |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 360 | for (j = i + 1; j < genl_get_cmd_cnt(family); j++) { |
| 361 | struct genl_ops op2; |
| 362 | |
| 363 | genl_get_cmd_by_index(j, family, &op2); |
| 364 | if (op.cmd == op2.cmd) |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 365 | return -EINVAL; |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 366 | } |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 369 | return 0; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 370 | } |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 371 | |
| 372 | /** |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 373 | * genl_register_family - register a generic netlink family |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 374 | * @family: generic netlink family |
| 375 | * |
| 376 | * Registers the specified family after validating it first. Only one |
| 377 | * family may be registered with the same family name or identifier. |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 378 | * |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 379 | * The family's ops, multicast groups and module pointer must already |
| 380 | * be assigned. |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame] | 381 | * |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 382 | * Return 0 on success or a negative error code. |
| 383 | */ |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 384 | int genl_register_family(struct genl_family *family) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 385 | { |
Johannes Berg | a07ea4d | 2016-10-24 14:40:02 +0200 | [diff] [blame] | 386 | int err, i; |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 387 | int start = GENL_START_ALLOC, end = GENL_MAX_ID; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 388 | |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame] | 389 | err = genl_validate_ops(family); |
| 390 | if (err) |
| 391 | return err; |
| 392 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 393 | genl_lock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 394 | |
| 395 | if (genl_family_find_byname(family->name)) { |
| 396 | err = -EEXIST; |
| 397 | goto errout_locked; |
| 398 | } |
| 399 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 400 | /* |
| 401 | * Sadly, a few cases need to be special-cased |
| 402 | * due to them having previously abused the API |
| 403 | * and having used their family ID also as their |
| 404 | * multicast group ID, so we use reserved IDs |
| 405 | * for both to be sure we can do that mapping. |
| 406 | */ |
Johannes Berg | a07ea4d | 2016-10-24 14:40:02 +0200 | [diff] [blame] | 407 | if (family == &genl_ctrl) { |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 408 | /* and this needs to be special for initial family lookups */ |
| 409 | start = end = GENL_ID_CTRL; |
| 410 | } else if (strcmp(family->name, "pmcraid") == 0) { |
| 411 | start = end = GENL_ID_PMCRAID; |
| 412 | } else if (strcmp(family->name, "VFS_DQUOT") == 0) { |
| 413 | start = end = GENL_ID_VFS_DQUOT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Marcel Holtmann | 4e43df3 | 2019-04-24 22:18:53 +0200 | [diff] [blame] | 416 | family->id = idr_alloc_cyclic(&genl_fam_idr, family, |
| 417 | start, end + 1, GFP_KERNEL); |
Wei Yongjun | 22ca904 | 2016-11-01 14:45:52 +0000 | [diff] [blame] | 418 | if (family->id < 0) { |
| 419 | err = family->id; |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 420 | goto errout_locked; |
Wei Yongjun | 22ca904 | 2016-11-01 14:45:52 +0000 | [diff] [blame] | 421 | } |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 422 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 423 | err = genl_validate_assign_mc_groups(family); |
| 424 | if (err) |
| 425 | goto errout_remove; |
| 426 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 427 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 428 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 429 | /* send all events */ |
| 430 | genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0); |
| 431 | for (i = 0; i < family->n_mcgrps; i++) |
| 432 | genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family, |
| 433 | &family->mcgrps[i], family->mcgrp_offset + i); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 434 | |
| 435 | return 0; |
| 436 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 437 | errout_remove: |
| 438 | idr_remove(&genl_fam_idr, family->id); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 439 | errout_locked: |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 440 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 441 | return err; |
| 442 | } |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 443 | EXPORT_SYMBOL(genl_register_family); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 444 | |
| 445 | /** |
| 446 | * genl_unregister_family - unregister generic netlink family |
| 447 | * @family: generic netlink family |
| 448 | * |
| 449 | * Unregisters the specified family. |
| 450 | * |
| 451 | * Returns 0 on success or a negative error code. |
| 452 | */ |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 453 | int genl_unregister_family(const struct genl_family *family) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 454 | { |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 455 | genl_lock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 456 | |
pravin shelar | 0e82c76 | 2016-10-28 16:01:41 -0700 | [diff] [blame] | 457 | if (!genl_family_find_byid(family->id)) { |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 458 | genl_unlock_all(); |
| 459 | return -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 460 | } |
| 461 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 462 | genl_unregister_mc_groups(family); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 463 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 464 | idr_remove(&genl_fam_idr, family->id); |
| 465 | |
| 466 | up_write(&cb_lock); |
| 467 | wait_event(genl_sk_destructing_waitq, |
| 468 | atomic_read(&genl_sk_destructing_cnt) == 0); |
| 469 | genl_unlock(); |
| 470 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 471 | genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0); |
| 472 | |
| 473 | return 0; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 474 | } |
Changli Gao | 416c2f9 | 2010-07-25 20:46:01 +0000 | [diff] [blame] | 475 | EXPORT_SYMBOL(genl_unregister_family); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 476 | |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 477 | /** |
| 478 | * genlmsg_put - Add generic netlink header to netlink message |
| 479 | * @skb: socket buffer holding the message |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 480 | * @portid: netlink portid the message is addressed to |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 481 | * @seq: sequence number (usually the one of the sender) |
| 482 | * @family: generic netlink family |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 483 | * @flags: netlink message flags |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 484 | * @cmd: generic netlink command |
| 485 | * |
| 486 | * Returns pointer to user specific header |
| 487 | */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 488 | void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 489 | const struct genl_family *family, int flags, u8 cmd) |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 490 | { |
| 491 | struct nlmsghdr *nlh; |
| 492 | struct genlmsghdr *hdr; |
| 493 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 494 | nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN + |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 495 | family->hdrsize, flags); |
| 496 | if (nlh == NULL) |
| 497 | return NULL; |
| 498 | |
| 499 | hdr = nlmsg_data(nlh); |
| 500 | hdr->cmd = cmd; |
| 501 | hdr->version = family->version; |
| 502 | hdr->reserved = 0; |
| 503 | |
| 504 | return (char *) hdr + GENL_HDRLEN; |
| 505 | } |
| 506 | EXPORT_SYMBOL(genlmsg_put); |
| 507 | |
Jiri Pirko | 1927f41 | 2019-10-05 20:04:34 +0200 | [diff] [blame] | 508 | static struct genl_dumpit_info *genl_dumpit_info_alloc(void) |
| 509 | { |
| 510 | return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL); |
| 511 | } |
| 512 | |
| 513 | static void genl_dumpit_info_free(const struct genl_dumpit_info *info) |
| 514 | { |
| 515 | kfree(info); |
| 516 | } |
| 517 | |
Jiri Pirko | c10e6cf | 2019-10-05 20:04:35 +0200 | [diff] [blame] | 518 | static struct nlattr ** |
| 519 | genl_family_rcv_msg_attrs_parse(const struct genl_family *family, |
| 520 | struct nlmsghdr *nlh, |
| 521 | struct netlink_ext_ack *extack, |
| 522 | const struct genl_ops *ops, |
| 523 | int hdrlen, |
Cong Wang | b65ce38 | 2020-06-12 00:16:55 -0700 | [diff] [blame] | 524 | enum genl_validate_flags no_strict_flag) |
Jiri Pirko | c10e6cf | 2019-10-05 20:04:35 +0200 | [diff] [blame] | 525 | { |
| 526 | enum netlink_validation validate = ops->validate & no_strict_flag ? |
| 527 | NL_VALIDATE_LIBERAL : |
| 528 | NL_VALIDATE_STRICT; |
| 529 | struct nlattr **attrbuf; |
| 530 | int err; |
| 531 | |
Jakub Kicinski | 48526a0 | 2020-10-02 14:49:57 -0700 | [diff] [blame] | 532 | if (!ops->maxattr) |
Michal Kubecek | cb0ce18 | 2019-10-11 09:40:09 +0200 | [diff] [blame] | 533 | return NULL; |
| 534 | |
Jakub Kicinski | 48526a0 | 2020-10-02 14:49:57 -0700 | [diff] [blame] | 535 | attrbuf = kmalloc_array(ops->maxattr + 1, |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 536 | sizeof(struct nlattr *), GFP_KERNEL); |
| 537 | if (!attrbuf) |
| 538 | return ERR_PTR(-ENOMEM); |
Jiri Pirko | c10e6cf | 2019-10-05 20:04:35 +0200 | [diff] [blame] | 539 | |
Jakub Kicinski | 48526a0 | 2020-10-02 14:49:57 -0700 | [diff] [blame] | 540 | err = __nlmsg_parse(nlh, hdrlen, attrbuf, ops->maxattr, ops->policy, |
| 541 | validate, extack); |
Paolo Abeni | 39f3b41 | 2020-02-21 19:42:13 +0100 | [diff] [blame] | 542 | if (err) { |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 543 | kfree(attrbuf); |
Jiri Pirko | c10e6cf | 2019-10-05 20:04:35 +0200 | [diff] [blame] | 544 | return ERR_PTR(err); |
| 545 | } |
| 546 | return attrbuf; |
| 547 | } |
| 548 | |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 549 | static void genl_family_rcv_msg_attrs_free(struct nlattr **attrbuf) |
Jiri Pirko | c10e6cf | 2019-10-05 20:04:35 +0200 | [diff] [blame] | 550 | { |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 551 | kfree(attrbuf); |
Jiri Pirko | c10e6cf | 2019-10-05 20:04:35 +0200 | [diff] [blame] | 552 | } |
| 553 | |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 554 | struct genl_start_context { |
| 555 | const struct genl_family *family; |
| 556 | struct nlmsghdr *nlh; |
| 557 | struct netlink_ext_ack *extack; |
| 558 | const struct genl_ops *ops; |
| 559 | int hdrlen; |
| 560 | }; |
| 561 | |
| 562 | static int genl_start(struct netlink_callback *cb) |
Tom Herbert | fc9e50f | 2015-12-15 15:41:37 -0800 | [diff] [blame] | 563 | { |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 564 | struct genl_start_context *ctx = cb->data; |
| 565 | const struct genl_ops *ops = ctx->ops; |
| 566 | struct genl_dumpit_info *info; |
| 567 | struct nlattr **attrs = NULL; |
Tom Herbert | fc9e50f | 2015-12-15 15:41:37 -0800 | [diff] [blame] | 568 | int rc = 0; |
| 569 | |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 570 | if (ops->validate & GENL_DONT_VALIDATE_DUMP) |
| 571 | goto no_attrs; |
| 572 | |
| 573 | if (ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen)) |
| 574 | return -EINVAL; |
| 575 | |
| 576 | attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack, |
| 577 | ops, ctx->hdrlen, |
Cong Wang | b65ce38 | 2020-06-12 00:16:55 -0700 | [diff] [blame] | 578 | GENL_DONT_VALIDATE_DUMP_STRICT); |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 579 | if (IS_ERR(attrs)) |
| 580 | return PTR_ERR(attrs); |
| 581 | |
| 582 | no_attrs: |
| 583 | info = genl_dumpit_info_alloc(); |
| 584 | if (!info) { |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 585 | genl_family_rcv_msg_attrs_free(attrs); |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 586 | return -ENOMEM; |
| 587 | } |
| 588 | info->family = ctx->family; |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 589 | info->op = *ops; |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 590 | info->attrs = attrs; |
| 591 | |
| 592 | cb->data = info; |
Tom Herbert | fc9e50f | 2015-12-15 15:41:37 -0800 | [diff] [blame] | 593 | if (ops->start) { |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 594 | if (!ctx->family->parallel_ops) |
| 595 | genl_lock(); |
Tom Herbert | fc9e50f | 2015-12-15 15:41:37 -0800 | [diff] [blame] | 596 | rc = ops->start(cb); |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 597 | if (!ctx->family->parallel_ops) |
| 598 | genl_unlock(); |
| 599 | } |
| 600 | |
| 601 | if (rc) { |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 602 | genl_family_rcv_msg_attrs_free(info->attrs); |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 603 | genl_dumpit_info_free(info); |
| 604 | cb->data = NULL; |
Tom Herbert | fc9e50f | 2015-12-15 15:41:37 -0800 | [diff] [blame] | 605 | } |
| 606 | return rc; |
| 607 | } |
| 608 | |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 609 | static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb) |
| 610 | { |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 611 | const struct genl_ops *ops = &genl_dumpit_info(cb)->op; |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 612 | int rc; |
| 613 | |
| 614 | genl_lock(); |
| 615 | rc = ops->dumpit(skb, cb); |
| 616 | genl_unlock(); |
| 617 | return rc; |
| 618 | } |
| 619 | |
| 620 | static int genl_lock_done(struct netlink_callback *cb) |
| 621 | { |
Jiri Pirko | 1927f41 | 2019-10-05 20:04:34 +0200 | [diff] [blame] | 622 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 623 | const struct genl_ops *ops = &info->op; |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 624 | int rc = 0; |
| 625 | |
| 626 | if (ops->done) { |
| 627 | genl_lock(); |
| 628 | rc = ops->done(cb); |
| 629 | genl_unlock(); |
| 630 | } |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 631 | genl_family_rcv_msg_attrs_free(info->attrs); |
Jiri Pirko | 1927f41 | 2019-10-05 20:04:34 +0200 | [diff] [blame] | 632 | genl_dumpit_info_free(info); |
| 633 | return rc; |
| 634 | } |
| 635 | |
| 636 | static int genl_parallel_done(struct netlink_callback *cb) |
| 637 | { |
| 638 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 639 | const struct genl_ops *ops = &info->op; |
Jiri Pirko | 1927f41 | 2019-10-05 20:04:34 +0200 | [diff] [blame] | 640 | int rc = 0; |
| 641 | |
| 642 | if (ops->done) |
| 643 | rc = ops->done(cb); |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 644 | genl_family_rcv_msg_attrs_free(info->attrs); |
Jiri Pirko | 1927f41 | 2019-10-05 20:04:34 +0200 | [diff] [blame] | 645 | genl_dumpit_info_free(info); |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 646 | return rc; |
| 647 | } |
| 648 | |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 649 | static int genl_family_rcv_msg_dumpit(const struct genl_family *family, |
| 650 | struct sk_buff *skb, |
| 651 | struct nlmsghdr *nlh, |
| 652 | struct netlink_ext_ack *extack, |
| 653 | const struct genl_ops *ops, |
| 654 | int hdrlen, struct net *net) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 655 | { |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 656 | struct genl_start_context ctx; |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 657 | int err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 658 | |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 659 | if (!ops->dumpit) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 660 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 661 | |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 662 | ctx.family = family; |
| 663 | ctx.nlh = nlh; |
| 664 | ctx.extack = extack; |
| 665 | ctx.ops = ops; |
| 666 | ctx.hdrlen = hdrlen; |
Jiri Pirko | 1927f41 | 2019-10-05 20:04:34 +0200 | [diff] [blame] | 667 | |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 668 | if (!family->parallel_ops) { |
| 669 | struct netlink_dump_control c = { |
| 670 | .module = family->module, |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 671 | .data = &ctx, |
| 672 | .start = genl_start, |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 673 | .dump = genl_lock_dumpit, |
| 674 | .done = genl_lock_done, |
| 675 | }; |
| 676 | |
| 677 | genl_unlock(); |
| 678 | err = __netlink_dump_start(net->genl_sock, skb, nlh, &c); |
| 679 | genl_lock(); |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 680 | } else { |
| 681 | struct netlink_dump_control c = { |
| 682 | .module = family->module, |
Cong Wang | c36f055 | 2020-06-02 21:49:10 -0700 | [diff] [blame] | 683 | .data = &ctx, |
| 684 | .start = genl_start, |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 685 | .dump = ops->dumpit, |
Jiri Pirko | 1927f41 | 2019-10-05 20:04:34 +0200 | [diff] [blame] | 686 | .done = genl_parallel_done, |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | err = __netlink_dump_start(net->genl_sock, skb, nlh, &c); |
| 690 | } |
| 691 | |
| 692 | return err; |
| 693 | } |
| 694 | |
| 695 | static int genl_family_rcv_msg_doit(const struct genl_family *family, |
| 696 | struct sk_buff *skb, |
| 697 | struct nlmsghdr *nlh, |
| 698 | struct netlink_ext_ack *extack, |
| 699 | const struct genl_ops *ops, |
| 700 | int hdrlen, struct net *net) |
| 701 | { |
| 702 | struct nlattr **attrbuf; |
| 703 | struct genl_info info; |
| 704 | int err; |
| 705 | |
| 706 | if (!ops->doit) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 707 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 708 | |
Jiri Pirko | c10e6cf | 2019-10-05 20:04:35 +0200 | [diff] [blame] | 709 | attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack, |
| 710 | ops, hdrlen, |
Cong Wang | b65ce38 | 2020-06-12 00:16:55 -0700 | [diff] [blame] | 711 | GENL_DONT_VALIDATE_STRICT); |
Jiri Pirko | c10e6cf | 2019-10-05 20:04:35 +0200 | [diff] [blame] | 712 | if (IS_ERR(attrbuf)) |
| 713 | return PTR_ERR(attrbuf); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 714 | |
| 715 | info.snd_seq = nlh->nlmsg_seq; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 716 | info.snd_portid = NETLINK_CB(skb).portid; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 717 | info.nlhdr = nlh; |
| 718 | info.genlhdr = nlmsg_data(nlh); |
| 719 | info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN; |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 720 | info.attrs = attrbuf; |
Johannes Berg | 7ab606d | 2017-04-12 14:34:05 +0200 | [diff] [blame] | 721 | info.extack = extack; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 722 | genl_info_net_set(&info, net); |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 723 | memset(&info.user_ptr, 0, sizeof(info.user_ptr)); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 724 | |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 725 | if (family->pre_doit) { |
| 726 | err = family->pre_doit(ops, skb, &info); |
| 727 | if (err) |
Wei Yongjun | 50754d21 | 2013-04-26 15:34:16 +0000 | [diff] [blame] | 728 | goto out; |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | err = ops->doit(skb, &info); |
| 732 | |
| 733 | if (family->post_doit) |
| 734 | family->post_doit(ops, skb, &info); |
| 735 | |
Wei Yongjun | 50754d21 | 2013-04-26 15:34:16 +0000 | [diff] [blame] | 736 | out: |
Cong Wang | bf64ff4 | 2020-06-27 00:12:24 -0700 | [diff] [blame] | 737 | genl_family_rcv_msg_attrs_free(attrbuf); |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 738 | |
| 739 | return err; |
| 740 | } |
| 741 | |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 742 | static int genl_family_rcv_msg(const struct genl_family *family, |
| 743 | struct sk_buff *skb, |
| 744 | struct nlmsghdr *nlh, |
| 745 | struct netlink_ext_ack *extack) |
| 746 | { |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 747 | struct net *net = sock_net(skb->sk); |
| 748 | struct genlmsghdr *hdr = nlmsg_data(nlh); |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 749 | struct genl_ops op; |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 750 | int hdrlen; |
| 751 | |
| 752 | /* this family doesn't exist in this netns */ |
| 753 | if (!family->netnsok && !net_eq(net, &init_net)) |
| 754 | return -ENOENT; |
| 755 | |
| 756 | hdrlen = GENL_HDRLEN + family->hdrsize; |
| 757 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
| 758 | return -EINVAL; |
| 759 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 760 | if (genl_get_cmd(hdr->cmd, family, &op)) |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 761 | return -EOPNOTSUPP; |
| 762 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 763 | if ((op.flags & GENL_ADMIN_PERM) && |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 764 | !netlink_capable(skb, CAP_NET_ADMIN)) |
| 765 | return -EPERM; |
| 766 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 767 | if ((op.flags & GENL_UNS_ADMIN_PERM) && |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 768 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 769 | return -EPERM; |
| 770 | |
| 771 | if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) |
| 772 | return genl_family_rcv_msg_dumpit(family, skb, nlh, extack, |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 773 | &op, hdrlen, net); |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 774 | else |
| 775 | return genl_family_rcv_msg_doit(family, skb, nlh, extack, |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 776 | &op, hdrlen, net); |
Jiri Pirko | be064de | 2019-10-05 20:04:33 +0200 | [diff] [blame] | 777 | } |
| 778 | |
Johannes Berg | 2d4bc93 | 2017-04-12 14:34:04 +0200 | [diff] [blame] | 779 | static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 780 | struct netlink_ext_ack *extack) |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 781 | { |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 782 | const struct genl_family *family; |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 783 | int err; |
| 784 | |
| 785 | family = genl_family_find_byid(nlh->nlmsg_type); |
| 786 | if (family == NULL) |
| 787 | return -ENOENT; |
| 788 | |
| 789 | if (!family->parallel_ops) |
| 790 | genl_lock(); |
| 791 | |
Johannes Berg | 7ab606d | 2017-04-12 14:34:05 +0200 | [diff] [blame] | 792 | err = genl_family_rcv_msg(family, skb, nlh, extack); |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 793 | |
| 794 | if (!family->parallel_ops) |
| 795 | genl_unlock(); |
| 796 | |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 797 | return err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 800 | static void genl_rcv(struct sk_buff *skb) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 801 | { |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 802 | down_read(&cb_lock); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 803 | netlink_rcv_skb(skb, &genl_rcv_msg); |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 804 | up_read(&cb_lock); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /************************************************************************** |
| 808 | * Controller |
| 809 | **************************************************************************/ |
| 810 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 811 | static struct genl_family genl_ctrl; |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 812 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 813 | static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 814 | u32 flags, struct sk_buff *skb, u8 cmd) |
| 815 | { |
| 816 | void *hdr; |
| 817 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 818 | hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 819 | if (hdr == NULL) |
| 820 | return -1; |
| 821 | |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 822 | if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) || |
| 823 | nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) || |
| 824 | nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) || |
| 825 | nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) || |
| 826 | nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr)) |
| 827 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 828 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 829 | if (genl_get_cmd_cnt(family)) { |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 830 | struct nlattr *nla_ops; |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 831 | int i; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 832 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 833 | nla_ops = nla_nest_start_noflag(skb, CTRL_ATTR_OPS); |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 834 | if (nla_ops == NULL) |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 835 | goto nla_put_failure; |
| 836 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 837 | for (i = 0; i < genl_get_cmd_cnt(family); i++) { |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 838 | struct nlattr *nest; |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 839 | struct genl_ops op; |
| 840 | u32 op_flags; |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 841 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 842 | genl_get_cmd_by_index(i, family, &op); |
| 843 | op_flags = op.flags; |
| 844 | if (op.dumpit) |
Johannes Berg | 029b234 | 2013-11-18 20:54:58 +0100 | [diff] [blame] | 845 | op_flags |= GENL_CMD_CAP_DUMP; |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 846 | if (op.doit) |
Johannes Berg | 029b234 | 2013-11-18 20:54:58 +0100 | [diff] [blame] | 847 | op_flags |= GENL_CMD_CAP_DO; |
Jakub Kicinski | 48526a0 | 2020-10-02 14:49:57 -0700 | [diff] [blame] | 848 | if (op.policy) |
Johannes Berg | 029b234 | 2013-11-18 20:54:58 +0100 | [diff] [blame] | 849 | op_flags |= GENL_CMD_CAP_HASPOL; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 850 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 851 | nest = nla_nest_start_noflag(skb, i + 1); |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 852 | if (nest == NULL) |
| 853 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 854 | |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 855 | if (nla_put_u32(skb, CTRL_ATTR_OP_ID, op.cmd) || |
Johannes Berg | 029b234 | 2013-11-18 20:54:58 +0100 | [diff] [blame] | 856 | nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags)) |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 857 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 858 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 859 | nla_nest_end(skb, nest); |
| 860 | } |
| 861 | |
| 862 | nla_nest_end(skb, nla_ops); |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 865 | if (family->n_mcgrps) { |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 866 | struct nlattr *nla_grps; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 867 | int i; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 868 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 869 | nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 870 | if (nla_grps == NULL) |
| 871 | goto nla_put_failure; |
| 872 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 873 | for (i = 0; i < family->n_mcgrps; i++) { |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 874 | struct nlattr *nest; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 875 | const struct genl_multicast_group *grp; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 876 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 877 | grp = &family->mcgrps[i]; |
| 878 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 879 | nest = nla_nest_start_noflag(skb, i + 1); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 880 | if (nest == NULL) |
| 881 | goto nla_put_failure; |
| 882 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 883 | if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, |
| 884 | family->mcgrp_offset + i) || |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 885 | nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME, |
| 886 | grp->name)) |
| 887 | goto nla_put_failure; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 888 | |
| 889 | nla_nest_end(skb, nest); |
| 890 | } |
| 891 | nla_nest_end(skb, nla_grps); |
| 892 | } |
| 893 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 894 | genlmsg_end(skb, hdr); |
| 895 | return 0; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 896 | |
| 897 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 898 | genlmsg_cancel(skb, hdr); |
| 899 | return -EMSGSIZE; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 902 | static int ctrl_fill_mcgrp_info(const struct genl_family *family, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 903 | const struct genl_multicast_group *grp, |
| 904 | int grp_id, u32 portid, u32 seq, u32 flags, |
| 905 | struct sk_buff *skb, u8 cmd) |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 906 | { |
| 907 | void *hdr; |
| 908 | struct nlattr *nla_grps; |
| 909 | struct nlattr *nest; |
| 910 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 911 | hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 912 | if (hdr == NULL) |
| 913 | return -1; |
| 914 | |
Johannes Berg | c2ebb90 | 2013-11-19 15:19:36 +0100 | [diff] [blame] | 915 | if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) || |
| 916 | nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id)) |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 917 | goto nla_put_failure; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 918 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 919 | nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 920 | if (nla_grps == NULL) |
| 921 | goto nla_put_failure; |
| 922 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 923 | nest = nla_nest_start_noflag(skb, 1); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 924 | if (nest == NULL) |
| 925 | goto nla_put_failure; |
| 926 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 927 | if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) || |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 928 | nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME, |
| 929 | grp->name)) |
| 930 | goto nla_put_failure; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 931 | |
| 932 | nla_nest_end(skb, nest); |
| 933 | nla_nest_end(skb, nla_grps); |
| 934 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 935 | genlmsg_end(skb, hdr); |
| 936 | return 0; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 937 | |
| 938 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 939 | genlmsg_cancel(skb, hdr); |
| 940 | return -EMSGSIZE; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) |
| 944 | { |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 945 | int n = 0; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 946 | struct genl_family *rt; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 947 | struct net *net = sock_net(skb->sk); |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 948 | int fams_to_skip = cb->args[0]; |
| 949 | unsigned int id; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 950 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 951 | idr_for_each_entry(&genl_fam_idr, rt, id) { |
| 952 | if (!rt->netnsok && !net_eq(net, &init_net)) |
| 953 | continue; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 954 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 955 | if (n++ < fams_to_skip) |
| 956 | continue; |
| 957 | |
| 958 | if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid, |
| 959 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
Stanislaw Gruszka | 1d2a6a5 | 2017-03-22 16:08:33 +0100 | [diff] [blame] | 960 | skb, CTRL_CMD_NEWFAMILY) < 0) { |
| 961 | n--; |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 962 | break; |
Stanislaw Gruszka | 1d2a6a5 | 2017-03-22 16:08:33 +0100 | [diff] [blame] | 963 | } |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 964 | } |
| 965 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 966 | cb->args[0] = n; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 967 | return skb->len; |
| 968 | } |
| 969 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 970 | static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 971 | u32 portid, int seq, u8 cmd) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 972 | { |
| 973 | struct sk_buff *skb; |
| 974 | int err; |
| 975 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 976 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 977 | if (skb == NULL) |
| 978 | return ERR_PTR(-ENOBUFS); |
| 979 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 980 | err = ctrl_fill_info(family, portid, seq, 0, skb, cmd); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 981 | if (err < 0) { |
| 982 | nlmsg_free(skb); |
| 983 | return ERR_PTR(err); |
| 984 | } |
| 985 | |
| 986 | return skb; |
| 987 | } |
| 988 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 989 | static struct sk_buff * |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 990 | ctrl_build_mcgrp_msg(const struct genl_family *family, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 991 | const struct genl_multicast_group *grp, |
| 992 | int grp_id, u32 portid, int seq, u8 cmd) |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 993 | { |
| 994 | struct sk_buff *skb; |
| 995 | int err; |
| 996 | |
| 997 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 998 | if (skb == NULL) |
| 999 | return ERR_PTR(-ENOBUFS); |
| 1000 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1001 | err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid, |
| 1002 | seq, 0, skb, cmd); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 1003 | if (err < 0) { |
| 1004 | nlmsg_free(skb); |
| 1005 | return ERR_PTR(err); |
| 1006 | } |
| 1007 | |
| 1008 | return skb; |
| 1009 | } |
| 1010 | |
Jakub Kicinski | a4bb4f5 | 2020-10-02 14:50:00 -0700 | [diff] [blame] | 1011 | static const struct nla_policy ctrl_policy_family[] = { |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1012 | [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 1013 | [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING, |
| 1014 | .len = GENL_NAMSIZ - 1 }, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1015 | }; |
| 1016 | |
| 1017 | static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info) |
| 1018 | { |
| 1019 | struct sk_buff *msg; |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 1020 | const struct genl_family *res = NULL; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1021 | int err = -EINVAL; |
| 1022 | |
| 1023 | if (info->attrs[CTRL_ATTR_FAMILY_ID]) { |
| 1024 | u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]); |
| 1025 | res = genl_family_find_byid(id); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1026 | err = -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | if (info->attrs[CTRL_ATTR_FAMILY_NAME]) { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 1030 | char *name; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1031 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 1032 | name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1033 | res = genl_family_find_byname(name); |
Stephen Hemminger | fa84309 | 2011-12-28 13:48:55 -0500 | [diff] [blame] | 1034 | #ifdef CONFIG_MODULES |
| 1035 | if (res == NULL) { |
| 1036 | genl_unlock(); |
Stanislaw Gruszka | c74f2b2 | 2013-07-26 11:00:10 +0200 | [diff] [blame] | 1037 | up_read(&cb_lock); |
Neil Horman | e9412c3 | 2012-05-29 09:30:41 +0000 | [diff] [blame] | 1038 | request_module("net-pf-%d-proto-%d-family-%s", |
Stephen Hemminger | fa84309 | 2011-12-28 13:48:55 -0500 | [diff] [blame] | 1039 | PF_NETLINK, NETLINK_GENERIC, name); |
Stanislaw Gruszka | c74f2b2 | 2013-07-26 11:00:10 +0200 | [diff] [blame] | 1040 | down_read(&cb_lock); |
Stephen Hemminger | fa84309 | 2011-12-28 13:48:55 -0500 | [diff] [blame] | 1041 | genl_lock(); |
| 1042 | res = genl_family_find_byname(name); |
| 1043 | } |
| 1044 | #endif |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1045 | err = -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1046 | } |
| 1047 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1048 | if (res == NULL) |
| 1049 | return err; |
| 1050 | |
| 1051 | if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) { |
| 1052 | /* family doesn't exist here */ |
| 1053 | return -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1056 | msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq, |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 1057 | CTRL_CMD_NEWFAMILY); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1058 | if (IS_ERR(msg)) |
| 1059 | return PTR_ERR(msg); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1060 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1061 | return genlmsg_reply(msg, info); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1062 | } |
| 1063 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 1064 | static int genl_ctrl_event(int event, const struct genl_family *family, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1065 | const struct genl_multicast_group *grp, |
| 1066 | int grp_id) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1067 | { |
| 1068 | struct sk_buff *msg; |
| 1069 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1070 | /* genl is still initialising */ |
| 1071 | if (!init_net.genl_sock) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1072 | return 0; |
| 1073 | |
| 1074 | switch (event) { |
| 1075 | case CTRL_CMD_NEWFAMILY: |
| 1076 | case CTRL_CMD_DELFAMILY: |
Johannes Berg | c2ebb90 | 2013-11-19 15:19:36 +0100 | [diff] [blame] | 1077 | WARN_ON(grp); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1078 | msg = ctrl_build_family_msg(family, 0, 0, event); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 1079 | break; |
| 1080 | case CTRL_CMD_NEWMCAST_GRP: |
| 1081 | case CTRL_CMD_DELMCAST_GRP: |
Johannes Berg | c2ebb90 | 2013-11-19 15:19:36 +0100 | [diff] [blame] | 1082 | BUG_ON(!grp); |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1083 | msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1084 | break; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1085 | default: |
| 1086 | return -EINVAL; |
| 1087 | } |
| 1088 | |
| 1089 | if (IS_ERR(msg)) |
| 1090 | return PTR_ERR(msg); |
| 1091 | |
| 1092 | if (!family->netnsok) { |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 1093 | genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1094 | 0, GFP_KERNEL); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1095 | } else { |
| 1096 | rcu_read_lock(); |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 1097 | genlmsg_multicast_allns(&genl_ctrl, msg, 0, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1098 | 0, GFP_ATOMIC); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1099 | rcu_read_unlock(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
Jakub Kicinski | adc8484 | 2020-10-02 14:49:55 -0700 | [diff] [blame] | 1105 | struct ctrl_dump_policy_ctx { |
| 1106 | struct netlink_policy_dump_state *state; |
Johannes Berg | 50a896cf | 2020-10-03 10:44:45 +0200 | [diff] [blame] | 1107 | const struct genl_family *rt; |
| 1108 | unsigned int opidx; |
Jakub Kicinski | e992a6e | 2020-10-03 10:44:46 +0200 | [diff] [blame] | 1109 | u32 op; |
Jakub Kicinski | adc8484 | 2020-10-02 14:49:55 -0700 | [diff] [blame] | 1110 | u16 fam_id; |
Jakub Kicinski | e992a6e | 2020-10-03 10:44:46 +0200 | [diff] [blame] | 1111 | u8 policies:1, |
| 1112 | single_op:1; |
Jakub Kicinski | adc8484 | 2020-10-02 14:49:55 -0700 | [diff] [blame] | 1113 | }; |
| 1114 | |
Jakub Kicinski | a4bb4f5 | 2020-10-02 14:50:00 -0700 | [diff] [blame] | 1115 | static const struct nla_policy ctrl_policy_policy[] = { |
| 1116 | [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, |
| 1117 | [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING, |
| 1118 | .len = GENL_NAMSIZ - 1 }, |
Jakub Kicinski | e992a6e | 2020-10-03 10:44:46 +0200 | [diff] [blame] | 1119 | [CTRL_ATTR_OP] = { .type = NLA_U32 }, |
Jakub Kicinski | a4bb4f5 | 2020-10-02 14:50:00 -0700 | [diff] [blame] | 1120 | }; |
| 1121 | |
Jakub Kicinski | 78ade61 | 2020-10-02 14:49:56 -0700 | [diff] [blame] | 1122 | static int ctrl_dumppolicy_start(struct netlink_callback *cb) |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1123 | { |
Jakub Kicinski | 8e1ed28 | 2020-10-02 14:49:59 -0700 | [diff] [blame] | 1124 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
Jakub Kicinski | adc8484 | 2020-10-02 14:49:55 -0700 | [diff] [blame] | 1125 | struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; |
Jakub Kicinski | 8e1ed28 | 2020-10-02 14:49:59 -0700 | [diff] [blame] | 1126 | struct nlattr **tb = info->attrs; |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1127 | const struct genl_family *rt; |
Johannes Berg | 50a896cf | 2020-10-03 10:44:45 +0200 | [diff] [blame] | 1128 | struct genl_ops op; |
| 1129 | int err, i; |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1130 | |
Jakub Kicinski | adc8484 | 2020-10-02 14:49:55 -0700 | [diff] [blame] | 1131 | BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx)); |
| 1132 | |
Jakub Kicinski | 78ade61 | 2020-10-02 14:49:56 -0700 | [diff] [blame] | 1133 | if (!tb[CTRL_ATTR_FAMILY_ID] && !tb[CTRL_ATTR_FAMILY_NAME]) |
| 1134 | return -EINVAL; |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1135 | |
Jakub Kicinski | 78ade61 | 2020-10-02 14:49:56 -0700 | [diff] [blame] | 1136 | if (tb[CTRL_ATTR_FAMILY_ID]) { |
| 1137 | ctx->fam_id = nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]); |
| 1138 | } else { |
| 1139 | rt = genl_family_find_byname( |
| 1140 | nla_data(tb[CTRL_ATTR_FAMILY_NAME])); |
| 1141 | if (!rt) |
| 1142 | return -ENOENT; |
| 1143 | ctx->fam_id = rt->id; |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Jakub Kicinski | adc8484 | 2020-10-02 14:49:55 -0700 | [diff] [blame] | 1146 | rt = genl_family_find_byid(ctx->fam_id); |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1147 | if (!rt) |
| 1148 | return -ENOENT; |
| 1149 | |
Johannes Berg | 50a896cf | 2020-10-03 10:44:45 +0200 | [diff] [blame] | 1150 | ctx->rt = rt; |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1151 | |
Jakub Kicinski | e992a6e | 2020-10-03 10:44:46 +0200 | [diff] [blame] | 1152 | if (tb[CTRL_ATTR_OP]) { |
| 1153 | ctx->single_op = true; |
| 1154 | ctx->op = nla_get_u32(tb[CTRL_ATTR_OP]); |
| 1155 | |
| 1156 | err = genl_get_cmd(ctx->op, rt, &op); |
| 1157 | if (err) { |
| 1158 | NL_SET_BAD_ATTR(cb->extack, tb[CTRL_ATTR_OP]); |
| 1159 | return err; |
| 1160 | } |
| 1161 | |
| 1162 | if (!op.policy) |
| 1163 | return -ENODATA; |
| 1164 | |
| 1165 | return netlink_policy_dump_add_policy(&ctx->state, op.policy, |
| 1166 | op.maxattr); |
| 1167 | } |
| 1168 | |
Johannes Berg | 50a896cf | 2020-10-03 10:44:45 +0200 | [diff] [blame] | 1169 | for (i = 0; i < genl_get_cmd_cnt(rt); i++) { |
| 1170 | genl_get_cmd_by_index(i, rt, &op); |
| 1171 | |
| 1172 | if (op.policy) { |
| 1173 | err = netlink_policy_dump_add_policy(&ctx->state, |
| 1174 | op.policy, |
| 1175 | op.maxattr); |
| 1176 | if (err) |
| 1177 | return err; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | if (!ctx->state) |
| 1182 | return -ENODATA; |
| 1183 | return 0; |
Jakub Kicinski | 78ade61 | 2020-10-02 14:49:56 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Johannes Berg | aa85ee5 | 2020-10-03 10:44:44 +0200 | [diff] [blame] | 1186 | static void *ctrl_dumppolicy_prep(struct sk_buff *skb, |
| 1187 | struct netlink_callback *cb) |
| 1188 | { |
| 1189 | struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; |
| 1190 | void *hdr; |
| 1191 | |
| 1192 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, |
| 1193 | cb->nlh->nlmsg_seq, &genl_ctrl, |
| 1194 | NLM_F_MULTI, CTRL_CMD_GETPOLICY); |
| 1195 | if (!hdr) |
| 1196 | return NULL; |
| 1197 | |
| 1198 | if (nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, ctx->fam_id)) |
| 1199 | return NULL; |
| 1200 | |
| 1201 | return hdr; |
| 1202 | } |
| 1203 | |
Johannes Berg | 50a896cf | 2020-10-03 10:44:45 +0200 | [diff] [blame] | 1204 | static int ctrl_dumppolicy_put_op(struct sk_buff *skb, |
| 1205 | struct netlink_callback *cb, |
| 1206 | struct genl_ops *op) |
| 1207 | { |
| 1208 | struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; |
| 1209 | struct nlattr *nest_pol, *nest_op; |
| 1210 | void *hdr; |
| 1211 | int idx; |
| 1212 | |
| 1213 | /* skip if we have nothing to show */ |
| 1214 | if (!op->policy) |
| 1215 | return 0; |
| 1216 | if (!op->doit && |
| 1217 | (!op->dumpit || op->validate & GENL_DONT_VALIDATE_DUMP)) |
| 1218 | return 0; |
| 1219 | |
| 1220 | hdr = ctrl_dumppolicy_prep(skb, cb); |
| 1221 | if (!hdr) |
| 1222 | return -ENOBUFS; |
| 1223 | |
| 1224 | nest_pol = nla_nest_start(skb, CTRL_ATTR_OP_POLICY); |
| 1225 | if (!nest_pol) |
| 1226 | goto err; |
| 1227 | |
| 1228 | nest_op = nla_nest_start(skb, op->cmd); |
| 1229 | if (!nest_op) |
| 1230 | goto err; |
| 1231 | |
| 1232 | /* for now both do/dump are always the same */ |
| 1233 | idx = netlink_policy_dump_get_policy_idx(ctx->state, |
| 1234 | op->policy, |
| 1235 | op->maxattr); |
| 1236 | |
| 1237 | if (op->doit && nla_put_u32(skb, CTRL_ATTR_POLICY_DO, idx)) |
| 1238 | goto err; |
| 1239 | |
| 1240 | if (op->dumpit && !(op->validate & GENL_DONT_VALIDATE_DUMP) && |
| 1241 | nla_put_u32(skb, CTRL_ATTR_POLICY_DUMP, idx)) |
| 1242 | goto err; |
| 1243 | |
| 1244 | nla_nest_end(skb, nest_op); |
| 1245 | nla_nest_end(skb, nest_pol); |
| 1246 | genlmsg_end(skb, hdr); |
| 1247 | |
| 1248 | return 0; |
| 1249 | err: |
| 1250 | genlmsg_cancel(skb, hdr); |
| 1251 | return -ENOBUFS; |
| 1252 | } |
| 1253 | |
Jakub Kicinski | 78ade61 | 2020-10-02 14:49:56 -0700 | [diff] [blame] | 1254 | static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb) |
| 1255 | { |
| 1256 | struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; |
Johannes Berg | 50a896cf | 2020-10-03 10:44:45 +0200 | [diff] [blame] | 1257 | void *hdr; |
| 1258 | |
| 1259 | if (!ctx->policies) { |
| 1260 | while (ctx->opidx < genl_get_cmd_cnt(ctx->rt)) { |
| 1261 | struct genl_ops op; |
| 1262 | |
Jakub Kicinski | e992a6e | 2020-10-03 10:44:46 +0200 | [diff] [blame] | 1263 | if (ctx->single_op) { |
| 1264 | int err; |
| 1265 | |
| 1266 | err = genl_get_cmd(ctx->op, ctx->rt, &op); |
| 1267 | if (WARN_ON(err)) |
| 1268 | return skb->len; |
| 1269 | |
| 1270 | /* break out of the loop after this one */ |
| 1271 | ctx->opidx = genl_get_cmd_cnt(ctx->rt); |
| 1272 | } else { |
| 1273 | genl_get_cmd_by_index(ctx->opidx, ctx->rt, &op); |
| 1274 | } |
Johannes Berg | 50a896cf | 2020-10-03 10:44:45 +0200 | [diff] [blame] | 1275 | |
| 1276 | if (ctrl_dumppolicy_put_op(skb, cb, &op)) |
| 1277 | return skb->len; |
| 1278 | |
| 1279 | ctx->opidx++; |
| 1280 | } |
| 1281 | |
| 1282 | /* completed with the per-op policy index list */ |
| 1283 | ctx->policies = true; |
| 1284 | } |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1285 | |
Jakub Kicinski | adc8484 | 2020-10-02 14:49:55 -0700 | [diff] [blame] | 1286 | while (netlink_policy_dump_loop(ctx->state)) { |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1287 | struct nlattr *nest; |
| 1288 | |
Johannes Berg | aa85ee5 | 2020-10-03 10:44:44 +0200 | [diff] [blame] | 1289 | hdr = ctrl_dumppolicy_prep(skb, cb); |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1290 | if (!hdr) |
| 1291 | goto nla_put_failure; |
| 1292 | |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1293 | nest = nla_nest_start(skb, CTRL_ATTR_POLICY); |
| 1294 | if (!nest) |
| 1295 | goto nla_put_failure; |
| 1296 | |
Jakub Kicinski | adc8484 | 2020-10-02 14:49:55 -0700 | [diff] [blame] | 1297 | if (netlink_policy_dump_write(skb, ctx->state)) |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1298 | goto nla_put_failure; |
| 1299 | |
| 1300 | nla_nest_end(skb, nest); |
| 1301 | |
| 1302 | genlmsg_end(skb, hdr); |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1303 | } |
| 1304 | |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1305 | return skb->len; |
Johannes Berg | 50a896cf | 2020-10-03 10:44:45 +0200 | [diff] [blame] | 1306 | |
| 1307 | nla_put_failure: |
| 1308 | genlmsg_cancel(skb, hdr); |
| 1309 | return skb->len; |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1310 | } |
| 1311 | |
Johannes Berg | 949ca6b | 2020-10-02 09:46:04 +0200 | [diff] [blame] | 1312 | static int ctrl_dumppolicy_done(struct netlink_callback *cb) |
| 1313 | { |
Jakub Kicinski | adc8484 | 2020-10-02 14:49:55 -0700 | [diff] [blame] | 1314 | struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx; |
| 1315 | |
| 1316 | netlink_policy_dump_free(ctx->state); |
Johannes Berg | 949ca6b | 2020-10-02 09:46:04 +0200 | [diff] [blame] | 1317 | return 0; |
| 1318 | } |
| 1319 | |
stephen hemminger | 12d8de6 | 2016-08-31 15:22:00 -0700 | [diff] [blame] | 1320 | static const struct genl_ops genl_ctrl_ops[] = { |
Johannes Berg | c53ed74 | 2013-11-19 15:19:31 +0100 | [diff] [blame] | 1321 | { |
| 1322 | .cmd = CTRL_CMD_GETFAMILY, |
Johannes Berg | ef6243a | 2019-04-26 14:07:31 +0200 | [diff] [blame] | 1323 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
Jakub Kicinski | a4bb4f5 | 2020-10-02 14:50:00 -0700 | [diff] [blame] | 1324 | .policy = ctrl_policy_family, |
| 1325 | .maxattr = ARRAY_SIZE(ctrl_policy_family) - 1, |
Johannes Berg | c53ed74 | 2013-11-19 15:19:31 +0100 | [diff] [blame] | 1326 | .doit = ctrl_getfamily, |
| 1327 | .dumpit = ctrl_dumpfamily, |
Johannes Berg | c53ed74 | 2013-11-19 15:19:31 +0100 | [diff] [blame] | 1328 | }, |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1329 | { |
| 1330 | .cmd = CTRL_CMD_GETPOLICY, |
Jakub Kicinski | a4bb4f5 | 2020-10-02 14:50:00 -0700 | [diff] [blame] | 1331 | .policy = ctrl_policy_policy, |
| 1332 | .maxattr = ARRAY_SIZE(ctrl_policy_policy) - 1, |
Jakub Kicinski | 78ade61 | 2020-10-02 14:49:56 -0700 | [diff] [blame] | 1333 | .start = ctrl_dumppolicy_start, |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1334 | .dumpit = ctrl_dumppolicy, |
Johannes Berg | 949ca6b | 2020-10-02 09:46:04 +0200 | [diff] [blame] | 1335 | .done = ctrl_dumppolicy_done, |
Johannes Berg | d07dcf9a | 2020-04-30 22:13:12 +0200 | [diff] [blame] | 1336 | }, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1337 | }; |
| 1338 | |
stephen hemminger | 12d8de6 | 2016-08-31 15:22:00 -0700 | [diff] [blame] | 1339 | static const struct genl_multicast_group genl_ctrl_groups[] = { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1340 | { .name = "notify", }, |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 1341 | }; |
| 1342 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1343 | static struct genl_family genl_ctrl __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1344 | .module = THIS_MODULE, |
| 1345 | .ops = genl_ctrl_ops, |
| 1346 | .n_ops = ARRAY_SIZE(genl_ctrl_ops), |
| 1347 | .mcgrps = genl_ctrl_groups, |
| 1348 | .n_mcgrps = ARRAY_SIZE(genl_ctrl_groups), |
| 1349 | .id = GENL_ID_CTRL, |
| 1350 | .name = "nlctrl", |
| 1351 | .version = 0x2, |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1352 | .netnsok = true, |
| 1353 | }; |
| 1354 | |
Florian Westphal | 4d54cc3 | 2021-02-12 15:59:59 -0800 | [diff] [blame] | 1355 | static int genl_bind(struct net *net, int group) |
| 1356 | { |
| 1357 | const struct genl_family *family; |
| 1358 | unsigned int id; |
| 1359 | int ret = 0; |
| 1360 | |
| 1361 | genl_lock_all(); |
| 1362 | |
| 1363 | idr_for_each_entry(&genl_fam_idr, family, id) { |
| 1364 | const struct genl_multicast_group *grp; |
| 1365 | int i; |
| 1366 | |
| 1367 | if (family->n_mcgrps == 0) |
| 1368 | continue; |
| 1369 | |
| 1370 | i = group - family->mcgrp_offset; |
| 1371 | if (i < 0 || i >= family->n_mcgrps) |
| 1372 | continue; |
| 1373 | |
| 1374 | grp = &family->mcgrps[i]; |
| 1375 | if ((grp->flags & GENL_UNS_ADMIN_PERM) && |
| 1376 | !ns_capable(net->user_ns, CAP_NET_ADMIN)) |
| 1377 | ret = -EPERM; |
| 1378 | |
| 1379 | break; |
| 1380 | } |
| 1381 | |
| 1382 | genl_unlock_all(); |
| 1383 | return ret; |
| 1384 | } |
| 1385 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1386 | static int __net_init genl_pernet_init(struct net *net) |
| 1387 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 1388 | struct netlink_kernel_cfg cfg = { |
| 1389 | .input = genl_rcv, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 1390 | .flags = NL_CFG_F_NONROOT_RECV, |
Florian Westphal | 4d54cc3 | 2021-02-12 15:59:59 -0800 | [diff] [blame] | 1391 | .bind = genl_bind, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 1392 | }; |
| 1393 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1394 | /* we'll bump the group number right afterwards */ |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 1395 | net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (!net->genl_sock && net_eq(net, &init_net)) |
| 1398 | panic("GENL: Cannot initialize generic netlink\n"); |
| 1399 | |
| 1400 | if (!net->genl_sock) |
| 1401 | return -ENOMEM; |
| 1402 | |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | static void __net_exit genl_pernet_exit(struct net *net) |
| 1407 | { |
| 1408 | netlink_kernel_release(net->genl_sock); |
| 1409 | net->genl_sock = NULL; |
| 1410 | } |
| 1411 | |
| 1412 | static struct pernet_operations genl_pernet_ops = { |
| 1413 | .init = genl_pernet_init, |
| 1414 | .exit = genl_pernet_exit, |
| 1415 | }; |
| 1416 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1417 | static int __init genl_init(void) |
| 1418 | { |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 1419 | int err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1420 | |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1421 | err = genl_register_family(&genl_ctrl); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1422 | if (err < 0) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1423 | goto problem; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1424 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1425 | err = register_pernet_subsys(&genl_pernet_ops); |
| 1426 | if (err) |
| 1427 | goto problem; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1428 | |
| 1429 | return 0; |
| 1430 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1431 | problem: |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1432 | panic("GENL: Cannot register controller: %d\n", err); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
Daniel Lezcano | c62e7ac | 2020-07-15 09:41:18 +0200 | [diff] [blame] | 1435 | core_initcall(genl_init); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1436 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1437 | static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group, |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1438 | gfp_t flags) |
| 1439 | { |
| 1440 | struct sk_buff *tmp; |
| 1441 | struct net *net, *prev = NULL; |
Nicolas Dichtel | cb9f7a9 | 2018-02-06 14:48:32 +0100 | [diff] [blame] | 1442 | bool delivered = false; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1443 | int err; |
| 1444 | |
| 1445 | for_each_net_rcu(net) { |
| 1446 | if (prev) { |
| 1447 | tmp = skb_clone(skb, flags); |
| 1448 | if (!tmp) { |
| 1449 | err = -ENOMEM; |
| 1450 | goto error; |
| 1451 | } |
| 1452 | err = nlmsg_multicast(prev->genl_sock, tmp, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1453 | portid, group, flags); |
Nicolas Dichtel | cb9f7a9 | 2018-02-06 14:48:32 +0100 | [diff] [blame] | 1454 | if (!err) |
| 1455 | delivered = true; |
| 1456 | else if (err != -ESRCH) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1457 | goto error; |
| 1458 | } |
| 1459 | |
| 1460 | prev = net; |
| 1461 | } |
| 1462 | |
Nicolas Dichtel | cb9f7a9 | 2018-02-06 14:48:32 +0100 | [diff] [blame] | 1463 | err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags); |
| 1464 | if (!err) |
| 1465 | delivered = true; |
| 1466 | else if (err != -ESRCH) |
Nicolas Dichtel | 02a2385 | 2018-03-14 21:10:23 +0100 | [diff] [blame] | 1467 | return err; |
Nicolas Dichtel | cb9f7a9 | 2018-02-06 14:48:32 +0100 | [diff] [blame] | 1468 | return delivered ? 0 : -ESRCH; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1469 | error: |
| 1470 | kfree_skb(skb); |
| 1471 | return err; |
| 1472 | } |
| 1473 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 1474 | int genlmsg_multicast_allns(const struct genl_family *family, |
| 1475 | struct sk_buff *skb, u32 portid, |
| 1476 | unsigned int group, gfp_t flags) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1477 | { |
Johannes Berg | 220815a | 2013-11-21 18:17:04 +0100 | [diff] [blame] | 1478 | if (WARN_ON_ONCE(group >= family->n_mcgrps)) |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1479 | return -EINVAL; |
Yajun Deng | f9b282b | 2021-07-27 11:41:41 +0800 | [diff] [blame] | 1480 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1481 | group = family->mcgrp_offset + group; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1482 | return genlmsg_mcast(skb, portid, group, flags); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1483 | } |
| 1484 | EXPORT_SYMBOL(genlmsg_multicast_allns); |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1485 | |
Johannes Berg | 2ae0f17 | 2016-10-24 14:40:04 +0200 | [diff] [blame] | 1486 | void genl_notify(const struct genl_family *family, struct sk_buff *skb, |
Jiri Benc | 92c14d9 | 2015-09-22 18:56:43 +0200 | [diff] [blame] | 1487 | struct genl_info *info, u32 group, gfp_t flags) |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1488 | { |
Jiri Benc | 92c14d9 | 2015-09-22 18:56:43 +0200 | [diff] [blame] | 1489 | struct net *net = genl_info_net(info); |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1490 | struct sock *sk = net->genl_sock; |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1491 | |
Johannes Berg | 220815a | 2013-11-21 18:17:04 +0100 | [diff] [blame] | 1492 | if (WARN_ON_ONCE(group >= family->n_mcgrps)) |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1493 | return; |
Yajun Deng | f9b282b | 2021-07-27 11:41:41 +0800 | [diff] [blame] | 1494 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1495 | group = family->mcgrp_offset + group; |
Yajun Deng | f9b282b | 2021-07-27 11:41:41 +0800 | [diff] [blame] | 1496 | nlmsg_notify(sk, skb, info->snd_portid, group, |
| 1497 | nlmsg_report(info->nlhdr), flags); |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1498 | } |
| 1499 | EXPORT_SYMBOL(genl_notify); |