Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * NETLINK Generic Netlink Family |
| 3 | * |
| 4 | * Authors: Jamal Hadi Salim |
| 5 | * Thomas Graf <tgraf@suug.ch> |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 6 | * Johannes Berg <johannes@sipsolutions.net> |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 12 | #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 Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 18 | #include <linux/bitmap.h> |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 19 | #include <linux/rwsem.h> |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 20 | #include <net/sock.h> |
| 21 | #include <net/genetlink.h> |
| 22 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 23 | static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */ |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 24 | static DECLARE_RWSEM(cb_lock); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 25 | |
James Chapman | f408e0c | 2010-04-02 06:19:05 +0000 | [diff] [blame] | 26 | void genl_lock(void) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 27 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 28 | mutex_lock(&genl_mutex); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 29 | } |
James Chapman | f408e0c | 2010-04-02 06:19:05 +0000 | [diff] [blame] | 30 | EXPORT_SYMBOL(genl_lock); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 31 | |
James Chapman | f408e0c | 2010-04-02 06:19:05 +0000 | [diff] [blame] | 32 | void genl_unlock(void) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 33 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 34 | mutex_unlock(&genl_mutex); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 35 | } |
James Chapman | f408e0c | 2010-04-02 06:19:05 +0000 | [diff] [blame] | 36 | EXPORT_SYMBOL(genl_unlock); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 37 | |
WANG Cong | 320f5ea | 2012-07-24 13:44:01 +0800 | [diff] [blame] | 38 | #ifdef CONFIG_LOCKDEP |
Pravin B Shelar | 86b1309 | 2011-11-10 19:14:51 -0800 | [diff] [blame] | 39 | int lockdep_genl_is_held(void) |
| 40 | { |
| 41 | return lockdep_is_held(&genl_mutex); |
| 42 | } |
| 43 | EXPORT_SYMBOL(lockdep_genl_is_held); |
| 44 | #endif |
| 45 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 46 | static void genl_lock_all(void) |
| 47 | { |
| 48 | down_write(&cb_lock); |
| 49 | genl_lock(); |
| 50 | } |
| 51 | |
| 52 | static void genl_unlock_all(void) |
| 53 | { |
| 54 | genl_unlock(); |
| 55 | up_write(&cb_lock); |
| 56 | } |
| 57 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 58 | #define GENL_FAM_TAB_SIZE 16 |
| 59 | #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1) |
| 60 | |
| 61 | static struct list_head family_ht[GENL_FAM_TAB_SIZE]; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 62 | /* |
| 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 Berg | e5dcecb | 2013-11-19 15:19:32 +0100 | [diff] [blame] | 68 | * 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 Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 72 | * Bit 16 is marked as used since it's used for generic netlink |
| 73 | * and the code no longer marks pre-reserved IDs as used. |
Johannes Berg | 2ecf753 | 2013-11-19 15:19:33 +0100 | [diff] [blame] | 74 | * Bit 17 is marked as already used since the VFS quota code |
| 75 | * also abused this API and relied on family == group ID, we |
| 76 | * cater to that by giving it a static family and group ID. |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 77 | */ |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 78 | static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) | |
| 79 | BIT(GENL_ID_VFS_DQUOT); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 80 | static unsigned long *mc_groups = &mc_group_start; |
| 81 | static unsigned long mc_groups_longs = 1; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 82 | |
Johannes Berg | c2ebb90 | 2013-11-19 15:19:36 +0100 | [diff] [blame] | 83 | static int genl_ctrl_event(int event, struct genl_family *family, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 84 | const struct genl_multicast_group *grp, |
| 85 | int grp_id); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 86 | |
| 87 | static inline unsigned int genl_family_hash(unsigned int id) |
| 88 | { |
| 89 | return id & GENL_FAM_TAB_MASK; |
| 90 | } |
| 91 | |
| 92 | static inline struct list_head *genl_family_chain(unsigned int id) |
| 93 | { |
| 94 | return &family_ht[genl_family_hash(id)]; |
| 95 | } |
| 96 | |
| 97 | static struct genl_family *genl_family_find_byid(unsigned int id) |
| 98 | { |
| 99 | struct genl_family *f; |
| 100 | |
| 101 | list_for_each_entry(f, genl_family_chain(id), family_list) |
| 102 | if (f->id == id) |
| 103 | return f; |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | static struct genl_family *genl_family_find_byname(char *name) |
| 109 | { |
| 110 | struct genl_family *f; |
| 111 | int i; |
| 112 | |
| 113 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) |
| 114 | list_for_each_entry(f, genl_family_chain(i), family_list) |
| 115 | if (strcmp(f->name, name) == 0) |
| 116 | return f; |
| 117 | |
| 118 | return NULL; |
| 119 | } |
| 120 | |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 121 | static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family) |
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 | int i; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 124 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 125 | for (i = 0; i < family->n_ops; i++) |
| 126 | if (family->ops[i].cmd == cmd) |
| 127 | return &family->ops[i]; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 128 | |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | /* Of course we are going to have problems once we hit |
| 133 | * 2^16 alive types, but that can only happen by year 2K |
| 134 | */ |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 135 | static u16 genl_generate_id(void) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 136 | { |
Krishna Kumar | 988ade6 | 2009-10-14 19:55:07 +0000 | [diff] [blame] | 137 | static u16 id_gen_idx = GENL_MIN_ID; |
| 138 | int i; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 139 | |
Krishna Kumar | 988ade6 | 2009-10-14 19:55:07 +0000 | [diff] [blame] | 140 | for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) { |
Johannes Berg | 2ecf753 | 2013-11-19 15:19:33 +0100 | [diff] [blame] | 141 | if (id_gen_idx != GENL_ID_VFS_DQUOT && |
| 142 | !genl_family_find_byid(id_gen_idx)) |
Krishna Kumar | 988ade6 | 2009-10-14 19:55:07 +0000 | [diff] [blame] | 143 | return id_gen_idx; |
| 144 | if (++id_gen_idx > GENL_MAX_ID) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 145 | id_gen_idx = GENL_MIN_ID; |
Krishna Kumar | 988ade6 | 2009-10-14 19:55:07 +0000 | [diff] [blame] | 146 | } |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 147 | |
Krishna Kumar | 988ade6 | 2009-10-14 19:55:07 +0000 | [diff] [blame] | 148 | return 0; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 151 | static int genl_allocate_reserve_groups(int n_groups, int *first_id) |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 152 | { |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 153 | unsigned long *new_groups; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 154 | int start = 0; |
| 155 | int i; |
| 156 | int id; |
| 157 | bool fits; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 158 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 159 | do { |
| 160 | if (start == 0) |
| 161 | id = find_first_zero_bit(mc_groups, |
| 162 | mc_groups_longs * |
| 163 | BITS_PER_LONG); |
| 164 | else |
| 165 | id = find_next_zero_bit(mc_groups, |
| 166 | mc_groups_longs * BITS_PER_LONG, |
| 167 | start); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 168 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 169 | fits = true; |
| 170 | for (i = id; |
| 171 | i < min_t(int, id + n_groups, |
| 172 | mc_groups_longs * BITS_PER_LONG); |
| 173 | i++) { |
| 174 | if (test_bit(i, mc_groups)) { |
| 175 | start = i; |
| 176 | fits = false; |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (id >= mc_groups_longs * BITS_PER_LONG) { |
| 182 | unsigned long new_longs = mc_groups_longs + |
| 183 | BITS_TO_LONGS(n_groups); |
| 184 | size_t nlen = new_longs * sizeof(unsigned long); |
| 185 | |
| 186 | if (mc_groups == &mc_group_start) { |
| 187 | new_groups = kzalloc(nlen, GFP_KERNEL); |
| 188 | if (!new_groups) |
| 189 | return -ENOMEM; |
| 190 | mc_groups = new_groups; |
| 191 | *mc_groups = mc_group_start; |
| 192 | } else { |
| 193 | new_groups = krealloc(mc_groups, nlen, |
| 194 | GFP_KERNEL); |
| 195 | if (!new_groups) |
| 196 | return -ENOMEM; |
| 197 | mc_groups = new_groups; |
| 198 | for (i = 0; i < BITS_TO_LONGS(n_groups); i++) |
| 199 | mc_groups[mc_groups_longs + i] = 0; |
| 200 | } |
| 201 | mc_groups_longs = new_longs; |
| 202 | } |
| 203 | } while (!fits); |
| 204 | |
| 205 | for (i = id; i < id + n_groups; i++) |
| 206 | set_bit(i, mc_groups); |
| 207 | *first_id = id; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static struct genl_family genl_ctrl; |
| 212 | |
| 213 | static int genl_validate_assign_mc_groups(struct genl_family *family) |
| 214 | { |
| 215 | int first_id; |
| 216 | int n_groups = family->n_mcgrps; |
| 217 | int err, i; |
| 218 | bool groups_allocated = false; |
| 219 | |
| 220 | if (!n_groups) |
| 221 | return 0; |
| 222 | |
| 223 | for (i = 0; i < n_groups; i++) { |
| 224 | const struct genl_multicast_group *grp = &family->mcgrps[i]; |
| 225 | |
| 226 | if (WARN_ON(grp->name[0] == '\0')) |
| 227 | return -EINVAL; |
| 228 | if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL)) |
| 229 | return -EINVAL; |
| 230 | } |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 231 | |
Johannes Berg | e5dcecb | 2013-11-19 15:19:32 +0100 | [diff] [blame] | 232 | /* special-case our own group and hacks */ |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 233 | if (family == &genl_ctrl) { |
| 234 | first_id = GENL_ID_CTRL; |
| 235 | BUG_ON(n_groups != 1); |
| 236 | } else if (strcmp(family->name, "NET_DM") == 0) { |
| 237 | first_id = 1; |
| 238 | BUG_ON(n_groups != 1); |
| 239 | } else if (strcmp(family->name, "VFS_DQUOT") == 0) { |
| 240 | first_id = GENL_ID_VFS_DQUOT; |
| 241 | BUG_ON(n_groups != 1); |
| 242 | } else { |
| 243 | groups_allocated = true; |
| 244 | err = genl_allocate_reserve_groups(n_groups, &first_id); |
| 245 | if (err) |
| 246 | return err; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 249 | family->mcgrp_offset = first_id; |
| 250 | |
| 251 | /* if still initializing, can't and don't need to to realloc bitmaps */ |
| 252 | if (!init_net.genl_sock) |
| 253 | return 0; |
| 254 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 255 | if (family->netnsok) { |
| 256 | struct net *net; |
| 257 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 258 | netlink_table_grab(); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 259 | rcu_read_lock(); |
| 260 | for_each_net_rcu(net) { |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 261 | err = __netlink_change_ngroups(net->genl_sock, |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 262 | mc_groups_longs * BITS_PER_LONG); |
| 263 | if (err) { |
| 264 | /* |
| 265 | * No need to roll back, can only fail if |
| 266 | * memory allocation fails and then the |
| 267 | * number of _possible_ groups has been |
| 268 | * increased on some sockets which is ok. |
| 269 | */ |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 270 | break; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | rcu_read_unlock(); |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 274 | netlink_table_ungrab(); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 275 | } else { |
| 276 | err = netlink_change_ngroups(init_net.genl_sock, |
| 277 | mc_groups_longs * BITS_PER_LONG); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 278 | } |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 279 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 280 | if (groups_allocated && err) { |
| 281 | for (i = 0; i < family->n_mcgrps; i++) |
| 282 | clear_bit(family->mcgrp_offset + i, mc_groups); |
| 283 | } |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 284 | |
Thomas Graf | 79d310d | 2007-07-24 15:34:53 -0700 | [diff] [blame] | 285 | return err; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 286 | } |
Thomas Graf | 79dc4386 | 2007-07-24 15:32:46 -0700 | [diff] [blame] | 287 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 288 | static void genl_unregister_mc_groups(struct genl_family *family) |
| 289 | { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 290 | struct net *net; |
| 291 | int i; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 292 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 293 | netlink_table_grab(); |
| 294 | rcu_read_lock(); |
| 295 | for_each_net_rcu(net) { |
| 296 | for (i = 0; i < family->n_mcgrps; i++) |
| 297 | __netlink_clear_multicast_users( |
| 298 | net->genl_sock, family->mcgrp_offset + i); |
| 299 | } |
| 300 | rcu_read_unlock(); |
| 301 | netlink_table_ungrab(); |
| 302 | |
| 303 | for (i = 0; i < family->n_mcgrps; i++) { |
| 304 | int grp_id = family->mcgrp_offset + i; |
| 305 | |
| 306 | if (grp_id != 1) |
| 307 | clear_bit(grp_id, mc_groups); |
| 308 | genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family, |
| 309 | &family->mcgrps[i], grp_id); |
| 310 | } |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame] | 313 | static int genl_validate_ops(struct genl_family *family) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 314 | { |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame] | 315 | const struct genl_ops *ops = family->ops; |
| 316 | unsigned int n_ops = family->n_ops; |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 317 | int i, j; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 318 | |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame] | 319 | if (WARN_ON(n_ops && !ops)) |
| 320 | return -EINVAL; |
| 321 | |
| 322 | if (!n_ops) |
| 323 | return 0; |
| 324 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 325 | for (i = 0; i < n_ops; i++) { |
| 326 | if (ops[i].dumpit == NULL && ops[i].doit == NULL) |
| 327 | return -EINVAL; |
| 328 | for (j = i + 1; j < n_ops; j++) |
| 329 | if (ops[i].cmd == ops[j].cmd) |
| 330 | return -EINVAL; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 333 | /* family is not registered yet, so no locking needed */ |
| 334 | family->ops = ops; |
| 335 | family->n_ops = n_ops; |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 336 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 337 | return 0; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 338 | } |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 339 | |
| 340 | /** |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 341 | * __genl_register_family - register a generic netlink family |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 342 | * @family: generic netlink family |
| 343 | * |
| 344 | * Registers the specified family after validating it first. Only one |
| 345 | * family may be registered with the same family name or identifier. |
| 346 | * The family id may equal GENL_ID_GENERATE causing an unique id to |
| 347 | * be automatically generated and assigned. |
| 348 | * |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame] | 349 | * The family's ops array must already be assigned, you can use the |
| 350 | * genl_register_family_with_ops() helper function. |
| 351 | * |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 352 | * Return 0 on success or a negative error code. |
| 353 | */ |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 354 | int __genl_register_family(struct genl_family *family) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 355 | { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 356 | int err = -EINVAL, i; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 357 | |
| 358 | if (family->id && family->id < GENL_MIN_ID) |
| 359 | goto errout; |
| 360 | |
| 361 | if (family->id > GENL_MAX_ID) |
| 362 | goto errout; |
| 363 | |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame] | 364 | err = genl_validate_ops(family); |
| 365 | if (err) |
| 366 | return err; |
| 367 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 368 | genl_lock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 369 | |
| 370 | if (genl_family_find_byname(family->name)) { |
| 371 | err = -EEXIST; |
| 372 | goto errout_locked; |
| 373 | } |
| 374 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 375 | if (family->id == GENL_ID_GENERATE) { |
| 376 | u16 newid = genl_generate_id(); |
| 377 | |
| 378 | if (!newid) { |
| 379 | err = -ENOMEM; |
| 380 | goto errout_locked; |
| 381 | } |
| 382 | |
| 383 | family->id = newid; |
Krishna Kumar | 93860b0 | 2009-10-14 19:54:53 +0000 | [diff] [blame] | 384 | } else if (genl_family_find_byid(family->id)) { |
| 385 | err = -EEXIST; |
| 386 | goto errout_locked; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 387 | } |
| 388 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 389 | if (family->maxattr && !family->parallel_ops) { |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 390 | family->attrbuf = kmalloc((family->maxattr+1) * |
| 391 | sizeof(struct nlattr *), GFP_KERNEL); |
| 392 | if (family->attrbuf == NULL) { |
| 393 | err = -ENOMEM; |
Jamal Hadi Salim | e200bd8 | 2006-02-13 15:51:24 -0800 | [diff] [blame] | 394 | goto errout_locked; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 395 | } |
| 396 | } else |
| 397 | family->attrbuf = NULL; |
| 398 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 399 | err = genl_validate_assign_mc_groups(family); |
| 400 | if (err) |
| 401 | goto errout_locked; |
| 402 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 403 | list_add_tail(&family->family_list, genl_family_chain(family->id)); |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 404 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 405 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 406 | /* send all events */ |
| 407 | genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0); |
| 408 | for (i = 0; i < family->n_mcgrps; i++) |
| 409 | genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family, |
| 410 | &family->mcgrps[i], family->mcgrp_offset + i); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 411 | |
| 412 | return 0; |
| 413 | |
| 414 | errout_locked: |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 415 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 416 | errout: |
| 417 | return err; |
| 418 | } |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 419 | EXPORT_SYMBOL(__genl_register_family); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 420 | |
| 421 | /** |
| 422 | * genl_unregister_family - unregister generic netlink family |
| 423 | * @family: generic netlink family |
| 424 | * |
| 425 | * Unregisters the specified family. |
| 426 | * |
| 427 | * Returns 0 on success or a negative error code. |
| 428 | */ |
| 429 | int genl_unregister_family(struct genl_family *family) |
| 430 | { |
| 431 | struct genl_family *rc; |
| 432 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 433 | genl_lock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 434 | |
Pavel Emelyanov | 910d6c3 | 2008-02-12 22:16:33 -0800 | [diff] [blame] | 435 | genl_unregister_mc_groups(family); |
| 436 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 437 | list_for_each_entry(rc, genl_family_chain(family->id), family_list) { |
| 438 | if (family->id != rc->id || strcmp(rc->name, family->name)) |
| 439 | continue; |
| 440 | |
| 441 | list_del(&rc->family_list); |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 442 | family->n_ops = 0; |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 443 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 444 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 445 | kfree(family->attrbuf); |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 446 | genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 450 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 451 | |
| 452 | return -ENOENT; |
| 453 | } |
Changli Gao | 416c2f9 | 2010-07-25 20:46:01 +0000 | [diff] [blame] | 454 | EXPORT_SYMBOL(genl_unregister_family); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 455 | |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 456 | /** |
| 457 | * genlmsg_put - Add generic netlink header to netlink message |
| 458 | * @skb: socket buffer holding the message |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 459 | * @portid: netlink portid the message is addressed to |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 460 | * @seq: sequence number (usually the one of the sender) |
| 461 | * @family: generic netlink family |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 462 | * @flags: netlink message flags |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 463 | * @cmd: generic netlink command |
| 464 | * |
| 465 | * Returns pointer to user specific header |
| 466 | */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 467 | void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 468 | struct genl_family *family, int flags, u8 cmd) |
| 469 | { |
| 470 | struct nlmsghdr *nlh; |
| 471 | struct genlmsghdr *hdr; |
| 472 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 473 | nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN + |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 474 | family->hdrsize, flags); |
| 475 | if (nlh == NULL) |
| 476 | return NULL; |
| 477 | |
| 478 | hdr = nlmsg_data(nlh); |
| 479 | hdr->cmd = cmd; |
| 480 | hdr->version = family->version; |
| 481 | hdr->reserved = 0; |
| 482 | |
| 483 | return (char *) hdr + GENL_HDRLEN; |
| 484 | } |
| 485 | EXPORT_SYMBOL(genlmsg_put); |
| 486 | |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 487 | static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb) |
| 488 | { |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 489 | /* our ops are always const - netlink API doesn't propagate that */ |
| 490 | const struct genl_ops *ops = cb->data; |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 491 | int rc; |
| 492 | |
| 493 | genl_lock(); |
| 494 | rc = ops->dumpit(skb, cb); |
| 495 | genl_unlock(); |
| 496 | return rc; |
| 497 | } |
| 498 | |
| 499 | static int genl_lock_done(struct netlink_callback *cb) |
| 500 | { |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 501 | /* our ops are always const - netlink API doesn't propagate that */ |
| 502 | const struct genl_ops *ops = cb->data; |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 503 | int rc = 0; |
| 504 | |
| 505 | if (ops->done) { |
| 506 | genl_lock(); |
| 507 | rc = ops->done(cb); |
| 508 | genl_unlock(); |
| 509 | } |
| 510 | return rc; |
| 511 | } |
| 512 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 513 | static int genl_family_rcv_msg(struct genl_family *family, |
| 514 | struct sk_buff *skb, |
| 515 | struct nlmsghdr *nlh) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 516 | { |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 517 | const struct genl_ops *ops; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 518 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 519 | struct genl_info info; |
| 520 | struct genlmsghdr *hdr = nlmsg_data(nlh); |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 521 | struct nlattr **attrbuf; |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 522 | int hdrlen, err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 523 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 524 | /* this family doesn't exist in this netns */ |
| 525 | if (!family->netnsok && !net_eq(net, &init_net)) |
| 526 | return -ENOENT; |
| 527 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 528 | hdrlen = GENL_HDRLEN + family->hdrsize; |
| 529 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 530 | return -EINVAL; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 531 | |
| 532 | ops = genl_get_cmd(hdr->cmd, family); |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 533 | if (ops == NULL) |
| 534 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 535 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 536 | if ((ops->flags & GENL_ADMIN_PERM) && |
Eric Paris | fd77846 | 2012-01-03 12:25:16 -0500 | [diff] [blame] | 537 | !capable(CAP_NET_ADMIN)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 538 | return -EPERM; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 539 | |
Pablo Neira | e1ee367 | 2013-07-29 12:30:04 +0200 | [diff] [blame] | 540 | if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) { |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 541 | int rc; |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 542 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 543 | if (ops->dumpit == NULL) |
| 544 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 545 | |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 546 | if (!family->parallel_ops) { |
| 547 | struct netlink_dump_control c = { |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 548 | .module = family->module, |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 549 | /* we have const, but the netlink API doesn't */ |
| 550 | .data = (void *)ops, |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 551 | .dump = genl_lock_dumpit, |
| 552 | .done = genl_lock_done, |
| 553 | }; |
| 554 | |
| 555 | genl_unlock(); |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 556 | rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c); |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 557 | genl_lock(); |
| 558 | |
| 559 | } else { |
| 560 | struct netlink_dump_control c = { |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 561 | .module = family->module, |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 562 | .dump = ops->dumpit, |
| 563 | .done = ops->done, |
| 564 | }; |
| 565 | |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 566 | rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c); |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | return rc; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 572 | if (ops->doit == NULL) |
| 573 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 574 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 575 | if (family->maxattr && family->parallel_ops) { |
| 576 | attrbuf = kmalloc((family->maxattr+1) * |
| 577 | sizeof(struct nlattr *), GFP_KERNEL); |
| 578 | if (attrbuf == NULL) |
| 579 | return -ENOMEM; |
| 580 | } else |
| 581 | attrbuf = family->attrbuf; |
| 582 | |
| 583 | if (attrbuf) { |
| 584 | err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 585 | ops->policy); |
| 586 | if (err < 0) |
Wei Yongjun | 50754d21 | 2013-04-26 15:34:16 +0000 | [diff] [blame] | 587 | goto out; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | info.snd_seq = nlh->nlmsg_seq; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 591 | info.snd_portid = NETLINK_CB(skb).portid; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 592 | info.nlhdr = nlh; |
| 593 | info.genlhdr = nlmsg_data(nlh); |
| 594 | info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN; |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 595 | info.attrs = attrbuf; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 596 | genl_info_net_set(&info, net); |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 597 | memset(&info.user_ptr, 0, sizeof(info.user_ptr)); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 598 | |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 599 | if (family->pre_doit) { |
| 600 | err = family->pre_doit(ops, skb, &info); |
| 601 | if (err) |
Wei Yongjun | 50754d21 | 2013-04-26 15:34:16 +0000 | [diff] [blame] | 602 | goto out; |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | err = ops->doit(skb, &info); |
| 606 | |
| 607 | if (family->post_doit) |
| 608 | family->post_doit(ops, skb, &info); |
| 609 | |
Wei Yongjun | 50754d21 | 2013-04-26 15:34:16 +0000 | [diff] [blame] | 610 | out: |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 611 | if (family->parallel_ops) |
| 612 | kfree(attrbuf); |
| 613 | |
| 614 | return err; |
| 615 | } |
| 616 | |
| 617 | static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 618 | { |
| 619 | struct genl_family *family; |
| 620 | int err; |
| 621 | |
| 622 | family = genl_family_find_byid(nlh->nlmsg_type); |
| 623 | if (family == NULL) |
| 624 | return -ENOENT; |
| 625 | |
| 626 | if (!family->parallel_ops) |
| 627 | genl_lock(); |
| 628 | |
| 629 | err = genl_family_rcv_msg(family, skb, nlh); |
| 630 | |
| 631 | if (!family->parallel_ops) |
| 632 | genl_unlock(); |
| 633 | |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 634 | return err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 637 | static void genl_rcv(struct sk_buff *skb) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 638 | { |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 639 | down_read(&cb_lock); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 640 | netlink_rcv_skb(skb, &genl_rcv_msg); |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 641 | up_read(&cb_lock); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /************************************************************************** |
| 645 | * Controller |
| 646 | **************************************************************************/ |
| 647 | |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 648 | static struct genl_family genl_ctrl = { |
| 649 | .id = GENL_ID_CTRL, |
| 650 | .name = "nlctrl", |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 651 | .version = 0x2, |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 652 | .maxattr = CTRL_ATTR_MAX, |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 653 | .netnsok = true, |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 654 | }; |
| 655 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 656 | static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 657 | u32 flags, struct sk_buff *skb, u8 cmd) |
| 658 | { |
| 659 | void *hdr; |
| 660 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 661 | hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 662 | if (hdr == NULL) |
| 663 | return -1; |
| 664 | |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 665 | if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) || |
| 666 | nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) || |
| 667 | nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) || |
| 668 | nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) || |
| 669 | nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr)) |
| 670 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 671 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 672 | if (family->n_ops) { |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 673 | struct nlattr *nla_ops; |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 674 | int i; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 675 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 676 | nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS); |
| 677 | if (nla_ops == NULL) |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 678 | goto nla_put_failure; |
| 679 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 680 | for (i = 0; i < family->n_ops; i++) { |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 681 | struct nlattr *nest; |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 682 | const struct genl_ops *ops = &family->ops[i]; |
Johannes Berg | 029b234 | 2013-11-18 20:54:58 +0100 | [diff] [blame] | 683 | u32 op_flags = ops->flags; |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 684 | |
| 685 | if (ops->dumpit) |
Johannes Berg | 029b234 | 2013-11-18 20:54:58 +0100 | [diff] [blame] | 686 | op_flags |= GENL_CMD_CAP_DUMP; |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 687 | if (ops->doit) |
Johannes Berg | 029b234 | 2013-11-18 20:54:58 +0100 | [diff] [blame] | 688 | op_flags |= GENL_CMD_CAP_DO; |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 689 | if (ops->policy) |
Johannes Berg | 029b234 | 2013-11-18 20:54:58 +0100 | [diff] [blame] | 690 | op_flags |= GENL_CMD_CAP_HASPOL; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 691 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 692 | nest = nla_nest_start(skb, i + 1); |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 693 | if (nest == NULL) |
| 694 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 695 | |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 696 | if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) || |
Johannes Berg | 029b234 | 2013-11-18 20:54:58 +0100 | [diff] [blame] | 697 | nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags)) |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 698 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 699 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 700 | nla_nest_end(skb, nest); |
| 701 | } |
| 702 | |
| 703 | nla_nest_end(skb, nla_ops); |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 706 | if (family->n_mcgrps) { |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 707 | struct nlattr *nla_grps; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 708 | int i; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 709 | |
| 710 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); |
| 711 | if (nla_grps == NULL) |
| 712 | goto nla_put_failure; |
| 713 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 714 | for (i = 0; i < family->n_mcgrps; i++) { |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 715 | struct nlattr *nest; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 716 | const struct genl_multicast_group *grp; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 717 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 718 | grp = &family->mcgrps[i]; |
| 719 | |
| 720 | nest = nla_nest_start(skb, i + 1); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 721 | if (nest == NULL) |
| 722 | goto nla_put_failure; |
| 723 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 724 | if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, |
| 725 | family->mcgrp_offset + i) || |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 726 | nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME, |
| 727 | grp->name)) |
| 728 | goto nla_put_failure; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 729 | |
| 730 | nla_nest_end(skb, nest); |
| 731 | } |
| 732 | nla_nest_end(skb, nla_grps); |
| 733 | } |
| 734 | |
| 735 | return genlmsg_end(skb, hdr); |
| 736 | |
| 737 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 738 | genlmsg_cancel(skb, hdr); |
| 739 | return -EMSGSIZE; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Johannes Berg | c2ebb90 | 2013-11-19 15:19:36 +0100 | [diff] [blame] | 742 | static int ctrl_fill_mcgrp_info(struct genl_family *family, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 743 | const struct genl_multicast_group *grp, |
| 744 | int grp_id, u32 portid, u32 seq, u32 flags, |
| 745 | struct sk_buff *skb, u8 cmd) |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 746 | { |
| 747 | void *hdr; |
| 748 | struct nlattr *nla_grps; |
| 749 | struct nlattr *nest; |
| 750 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 751 | hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 752 | if (hdr == NULL) |
| 753 | return -1; |
| 754 | |
Johannes Berg | c2ebb90 | 2013-11-19 15:19:36 +0100 | [diff] [blame] | 755 | if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) || |
| 756 | nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id)) |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 757 | goto nla_put_failure; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 758 | |
| 759 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); |
| 760 | if (nla_grps == NULL) |
| 761 | goto nla_put_failure; |
| 762 | |
| 763 | nest = nla_nest_start(skb, 1); |
| 764 | if (nest == NULL) |
| 765 | goto nla_put_failure; |
| 766 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 767 | 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] | 768 | nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME, |
| 769 | grp->name)) |
| 770 | goto nla_put_failure; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 771 | |
| 772 | nla_nest_end(skb, nest); |
| 773 | nla_nest_end(skb, nla_grps); |
| 774 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 775 | return genlmsg_end(skb, hdr); |
| 776 | |
| 777 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 778 | genlmsg_cancel(skb, hdr); |
| 779 | return -EMSGSIZE; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) |
| 783 | { |
| 784 | |
| 785 | int i, n = 0; |
| 786 | struct genl_family *rt; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 787 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 788 | int chains_to_skip = cb->args[0]; |
| 789 | int fams_to_skip = cb->args[1]; |
| 790 | |
Samir Bellabes | e1d5a01 | 2010-01-07 22:10:56 +0000 | [diff] [blame] | 791 | for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) { |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 792 | n = 0; |
| 793 | list_for_each_entry(rt, genl_family_chain(i), family_list) { |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 794 | if (!rt->netnsok && !net_eq(net, &init_net)) |
| 795 | continue; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 796 | if (++n < fams_to_skip) |
| 797 | continue; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 798 | if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 799 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 800 | skb, CTRL_CMD_NEWFAMILY) < 0) |
| 801 | goto errout; |
| 802 | } |
| 803 | |
| 804 | fams_to_skip = 0; |
| 805 | } |
| 806 | |
| 807 | errout: |
| 808 | cb->args[0] = i; |
| 809 | cb->args[1] = n; |
| 810 | |
| 811 | return skb->len; |
| 812 | } |
| 813 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 814 | static struct sk_buff *ctrl_build_family_msg(struct genl_family *family, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 815 | u32 portid, int seq, u8 cmd) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 816 | { |
| 817 | struct sk_buff *skb; |
| 818 | int err; |
| 819 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 820 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 821 | if (skb == NULL) |
| 822 | return ERR_PTR(-ENOBUFS); |
| 823 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 824 | err = ctrl_fill_info(family, portid, seq, 0, skb, cmd); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 825 | if (err < 0) { |
| 826 | nlmsg_free(skb); |
| 827 | return ERR_PTR(err); |
| 828 | } |
| 829 | |
| 830 | return skb; |
| 831 | } |
| 832 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 833 | static struct sk_buff * |
| 834 | ctrl_build_mcgrp_msg(struct genl_family *family, |
| 835 | const struct genl_multicast_group *grp, |
| 836 | int grp_id, u32 portid, int seq, u8 cmd) |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 837 | { |
| 838 | struct sk_buff *skb; |
| 839 | int err; |
| 840 | |
| 841 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 842 | if (skb == NULL) |
| 843 | return ERR_PTR(-ENOBUFS); |
| 844 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 845 | err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid, |
| 846 | seq, 0, skb, cmd); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 847 | if (err < 0) { |
| 848 | nlmsg_free(skb); |
| 849 | return ERR_PTR(err); |
| 850 | } |
| 851 | |
| 852 | return skb; |
| 853 | } |
| 854 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 855 | static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = { |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 856 | [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 857 | [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING, |
| 858 | .len = GENL_NAMSIZ - 1 }, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 859 | }; |
| 860 | |
| 861 | static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info) |
| 862 | { |
| 863 | struct sk_buff *msg; |
| 864 | struct genl_family *res = NULL; |
| 865 | int err = -EINVAL; |
| 866 | |
| 867 | if (info->attrs[CTRL_ATTR_FAMILY_ID]) { |
| 868 | u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]); |
| 869 | res = genl_family_find_byid(id); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 870 | err = -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | if (info->attrs[CTRL_ATTR_FAMILY_NAME]) { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 874 | char *name; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 875 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 876 | name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 877 | res = genl_family_find_byname(name); |
Stephen Hemminger | fa84309 | 2011-12-28 13:48:55 -0500 | [diff] [blame] | 878 | #ifdef CONFIG_MODULES |
| 879 | if (res == NULL) { |
| 880 | genl_unlock(); |
Stanislaw Gruszka | c74f2b2 | 2013-07-26 11:00:10 +0200 | [diff] [blame] | 881 | up_read(&cb_lock); |
Neil Horman | e9412c3 | 2012-05-29 09:30:41 +0000 | [diff] [blame] | 882 | request_module("net-pf-%d-proto-%d-family-%s", |
Stephen Hemminger | fa84309 | 2011-12-28 13:48:55 -0500 | [diff] [blame] | 883 | PF_NETLINK, NETLINK_GENERIC, name); |
Stanislaw Gruszka | c74f2b2 | 2013-07-26 11:00:10 +0200 | [diff] [blame] | 884 | down_read(&cb_lock); |
Stephen Hemminger | fa84309 | 2011-12-28 13:48:55 -0500 | [diff] [blame] | 885 | genl_lock(); |
| 886 | res = genl_family_find_byname(name); |
| 887 | } |
| 888 | #endif |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 889 | err = -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 890 | } |
| 891 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 892 | if (res == NULL) |
| 893 | return err; |
| 894 | |
| 895 | if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) { |
| 896 | /* family doesn't exist here */ |
| 897 | return -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 898 | } |
| 899 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 900 | msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq, |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 901 | CTRL_CMD_NEWFAMILY); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 902 | if (IS_ERR(msg)) |
| 903 | return PTR_ERR(msg); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 904 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 905 | return genlmsg_reply(msg, info); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 906 | } |
| 907 | |
Johannes Berg | c2ebb90 | 2013-11-19 15:19:36 +0100 | [diff] [blame] | 908 | static int genl_ctrl_event(int event, struct genl_family *family, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 909 | const struct genl_multicast_group *grp, |
| 910 | int grp_id) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 911 | { |
| 912 | struct sk_buff *msg; |
| 913 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 914 | /* genl is still initialising */ |
| 915 | if (!init_net.genl_sock) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 916 | return 0; |
| 917 | |
| 918 | switch (event) { |
| 919 | case CTRL_CMD_NEWFAMILY: |
| 920 | case CTRL_CMD_DELFAMILY: |
Johannes Berg | c2ebb90 | 2013-11-19 15:19:36 +0100 | [diff] [blame] | 921 | WARN_ON(grp); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 922 | msg = ctrl_build_family_msg(family, 0, 0, event); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 923 | break; |
| 924 | case CTRL_CMD_NEWMCAST_GRP: |
| 925 | case CTRL_CMD_DELMCAST_GRP: |
Johannes Berg | c2ebb90 | 2013-11-19 15:19:36 +0100 | [diff] [blame] | 926 | BUG_ON(!grp); |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 927 | msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 928 | break; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 929 | default: |
| 930 | return -EINVAL; |
| 931 | } |
| 932 | |
| 933 | if (IS_ERR(msg)) |
| 934 | return PTR_ERR(msg); |
| 935 | |
| 936 | if (!family->netnsok) { |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 937 | genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 938 | 0, GFP_KERNEL); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 939 | } else { |
| 940 | rcu_read_lock(); |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 941 | genlmsg_multicast_allns(&genl_ctrl, msg, 0, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 942 | 0, GFP_ATOMIC); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 943 | rcu_read_unlock(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | return 0; |
| 947 | } |
| 948 | |
Johannes Berg | c53ed74 | 2013-11-19 15:19:31 +0100 | [diff] [blame] | 949 | static struct genl_ops genl_ctrl_ops[] = { |
| 950 | { |
| 951 | .cmd = CTRL_CMD_GETFAMILY, |
| 952 | .doit = ctrl_getfamily, |
| 953 | .dumpit = ctrl_dumpfamily, |
| 954 | .policy = ctrl_policy, |
| 955 | }, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 956 | }; |
| 957 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 958 | static struct genl_multicast_group genl_ctrl_groups[] = { |
| 959 | { .name = "notify", }, |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 960 | }; |
| 961 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 962 | static int __net_init genl_pernet_init(struct net *net) |
| 963 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 964 | struct netlink_kernel_cfg cfg = { |
| 965 | .input = genl_rcv, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 966 | .flags = NL_CFG_F_NONROOT_RECV, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 967 | }; |
| 968 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 969 | /* we'll bump the group number right afterwards */ |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 970 | net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 971 | |
| 972 | if (!net->genl_sock && net_eq(net, &init_net)) |
| 973 | panic("GENL: Cannot initialize generic netlink\n"); |
| 974 | |
| 975 | if (!net->genl_sock) |
| 976 | return -ENOMEM; |
| 977 | |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | static void __net_exit genl_pernet_exit(struct net *net) |
| 982 | { |
| 983 | netlink_kernel_release(net->genl_sock); |
| 984 | net->genl_sock = NULL; |
| 985 | } |
| 986 | |
| 987 | static struct pernet_operations genl_pernet_ops = { |
| 988 | .init = genl_pernet_init, |
| 989 | .exit = genl_pernet_exit, |
| 990 | }; |
| 991 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 992 | static int __init genl_init(void) |
| 993 | { |
| 994 | int i, err; |
| 995 | |
| 996 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) |
| 997 | INIT_LIST_HEAD(&family_ht[i]); |
| 998 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 999 | err = genl_register_family_with_ops_groups(&genl_ctrl, genl_ctrl_ops, |
| 1000 | genl_ctrl_groups); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1001 | if (err < 0) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1002 | goto problem; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1003 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1004 | err = register_pernet_subsys(&genl_pernet_ops); |
| 1005 | if (err) |
| 1006 | goto problem; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1007 | |
| 1008 | return 0; |
| 1009 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1010 | problem: |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1011 | panic("GENL: Cannot register controller: %d\n", err); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | subsys_initcall(genl_init); |
| 1015 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1016 | 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] | 1017 | gfp_t flags) |
| 1018 | { |
| 1019 | struct sk_buff *tmp; |
| 1020 | struct net *net, *prev = NULL; |
| 1021 | int err; |
| 1022 | |
| 1023 | for_each_net_rcu(net) { |
| 1024 | if (prev) { |
| 1025 | tmp = skb_clone(skb, flags); |
| 1026 | if (!tmp) { |
| 1027 | err = -ENOMEM; |
| 1028 | goto error; |
| 1029 | } |
| 1030 | err = nlmsg_multicast(prev->genl_sock, tmp, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1031 | portid, group, flags); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1032 | if (err) |
| 1033 | goto error; |
| 1034 | } |
| 1035 | |
| 1036 | prev = net; |
| 1037 | } |
| 1038 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1039 | return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1040 | error: |
| 1041 | kfree_skb(skb); |
| 1042 | return err; |
| 1043 | } |
| 1044 | |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 1045 | int genlmsg_multicast_allns(struct genl_family *family, struct sk_buff *skb, |
| 1046 | u32 portid, unsigned int group, gfp_t flags) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1047 | { |
Johannes Berg | 220815a | 2013-11-21 18:17:04 +0100 | [diff] [blame] | 1048 | if (WARN_ON_ONCE(group >= family->n_mcgrps)) |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1049 | return -EINVAL; |
| 1050 | group = family->mcgrp_offset + group; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1051 | return genlmsg_mcast(skb, portid, group, flags); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1052 | } |
| 1053 | EXPORT_SYMBOL(genlmsg_multicast_allns); |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1054 | |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 1055 | void genl_notify(struct genl_family *family, |
| 1056 | struct sk_buff *skb, struct net *net, u32 portid, u32 group, |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1057 | struct nlmsghdr *nlh, gfp_t flags) |
| 1058 | { |
| 1059 | struct sock *sk = net->genl_sock; |
| 1060 | int report = 0; |
| 1061 | |
| 1062 | if (nlh) |
| 1063 | report = nlmsg_report(nlh); |
| 1064 | |
Johannes Berg | 220815a | 2013-11-21 18:17:04 +0100 | [diff] [blame] | 1065 | if (WARN_ON_ONCE(group >= family->n_mcgrps)) |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1066 | return; |
| 1067 | group = family->mcgrp_offset + group; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1068 | nlmsg_notify(sk, skb, portid, group, report, flags); |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1069 | } |
| 1070 | EXPORT_SYMBOL(genl_notify); |