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> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/socket.h> |
| 14 | #include <linux/string.h> |
| 15 | #include <linux/skbuff.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 17 | #include <linux/bitmap.h> |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 18 | #include <net/sock.h> |
| 19 | #include <net/genetlink.h> |
| 20 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 21 | static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */ |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 22 | |
Denis V. Lunev | 3b71535 | 2007-10-10 21:13:32 -0700 | [diff] [blame] | 23 | static inline void genl_lock(void) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 24 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 25 | mutex_lock(&genl_mutex); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 26 | } |
| 27 | |
Denis V. Lunev | 3b71535 | 2007-10-10 21:13:32 -0700 | [diff] [blame] | 28 | static inline void genl_unlock(void) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 29 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 30 | mutex_unlock(&genl_mutex); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | #define GENL_FAM_TAB_SIZE 16 |
| 34 | #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1) |
| 35 | |
| 36 | static struct list_head family_ht[GENL_FAM_TAB_SIZE]; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 37 | /* |
| 38 | * Bitmap of multicast groups that are currently in use. |
| 39 | * |
| 40 | * To avoid an allocation at boot of just one unsigned long, |
| 41 | * declare it global instead. |
| 42 | * Bit 0 is marked as already used since group 0 is invalid. |
| 43 | */ |
| 44 | static unsigned long mc_group_start = 0x1; |
| 45 | static unsigned long *mc_groups = &mc_group_start; |
| 46 | static unsigned long mc_groups_longs = 1; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 47 | |
| 48 | static int genl_ctrl_event(int event, void *data); |
| 49 | |
| 50 | static inline unsigned int genl_family_hash(unsigned int id) |
| 51 | { |
| 52 | return id & GENL_FAM_TAB_MASK; |
| 53 | } |
| 54 | |
| 55 | static inline struct list_head *genl_family_chain(unsigned int id) |
| 56 | { |
| 57 | return &family_ht[genl_family_hash(id)]; |
| 58 | } |
| 59 | |
| 60 | static struct genl_family *genl_family_find_byid(unsigned int id) |
| 61 | { |
| 62 | struct genl_family *f; |
| 63 | |
| 64 | list_for_each_entry(f, genl_family_chain(id), family_list) |
| 65 | if (f->id == id) |
| 66 | return f; |
| 67 | |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | static struct genl_family *genl_family_find_byname(char *name) |
| 72 | { |
| 73 | struct genl_family *f; |
| 74 | int i; |
| 75 | |
| 76 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) |
| 77 | list_for_each_entry(f, genl_family_chain(i), family_list) |
| 78 | if (strcmp(f->name, name) == 0) |
| 79 | return f; |
| 80 | |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family) |
| 85 | { |
| 86 | struct genl_ops *ops; |
| 87 | |
| 88 | list_for_each_entry(ops, &family->ops_list, ops_list) |
| 89 | if (ops->cmd == cmd) |
| 90 | return ops; |
| 91 | |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | /* Of course we are going to have problems once we hit |
| 96 | * 2^16 alive types, but that can only happen by year 2K |
| 97 | */ |
| 98 | static inline u16 genl_generate_id(void) |
| 99 | { |
| 100 | static u16 id_gen_idx; |
| 101 | int overflowed = 0; |
| 102 | |
| 103 | do { |
| 104 | if (id_gen_idx == 0) |
| 105 | id_gen_idx = GENL_MIN_ID; |
| 106 | |
| 107 | if (++id_gen_idx > GENL_MAX_ID) { |
| 108 | if (!overflowed) { |
| 109 | overflowed = 1; |
| 110 | id_gen_idx = 0; |
| 111 | continue; |
| 112 | } else |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | } while (genl_family_find_byid(id_gen_idx)); |
| 117 | |
| 118 | return id_gen_idx; |
| 119 | } |
| 120 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 121 | static struct genl_multicast_group notify_grp; |
| 122 | |
| 123 | /** |
| 124 | * genl_register_mc_group - register a multicast group |
| 125 | * |
| 126 | * Registers the specified multicast group and notifies userspace |
| 127 | * about the new group. |
| 128 | * |
| 129 | * Returns 0 on success or a negative error code. |
| 130 | * |
| 131 | * @family: The generic netlink family the group shall be registered for. |
| 132 | * @grp: The group to register, must have a name. |
| 133 | */ |
| 134 | int genl_register_mc_group(struct genl_family *family, |
| 135 | struct genl_multicast_group *grp) |
| 136 | { |
| 137 | int id; |
| 138 | unsigned long *new_groups; |
| 139 | int err; |
| 140 | |
| 141 | BUG_ON(grp->name[0] == '\0'); |
| 142 | |
| 143 | genl_lock(); |
| 144 | |
| 145 | /* special-case our own group */ |
| 146 | if (grp == ¬ify_grp) |
| 147 | id = GENL_ID_CTRL; |
| 148 | else |
| 149 | id = find_first_zero_bit(mc_groups, |
| 150 | mc_groups_longs * BITS_PER_LONG); |
| 151 | |
| 152 | |
| 153 | if (id >= mc_groups_longs * BITS_PER_LONG) { |
| 154 | size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long); |
| 155 | |
| 156 | if (mc_groups == &mc_group_start) { |
| 157 | new_groups = kzalloc(nlen, GFP_KERNEL); |
| 158 | if (!new_groups) { |
| 159 | err = -ENOMEM; |
| 160 | goto out; |
| 161 | } |
| 162 | mc_groups = new_groups; |
| 163 | *mc_groups = mc_group_start; |
| 164 | } else { |
| 165 | new_groups = krealloc(mc_groups, nlen, GFP_KERNEL); |
| 166 | if (!new_groups) { |
| 167 | err = -ENOMEM; |
| 168 | goto out; |
| 169 | } |
| 170 | mc_groups = new_groups; |
| 171 | mc_groups[mc_groups_longs] = 0; |
| 172 | } |
| 173 | mc_groups_longs++; |
| 174 | } |
| 175 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 176 | if (family->netnsok) { |
| 177 | struct net *net; |
| 178 | |
| 179 | rcu_read_lock(); |
| 180 | for_each_net_rcu(net) { |
| 181 | err = netlink_change_ngroups(net->genl_sock, |
| 182 | mc_groups_longs * BITS_PER_LONG); |
| 183 | if (err) { |
| 184 | /* |
| 185 | * No need to roll back, can only fail if |
| 186 | * memory allocation fails and then the |
| 187 | * number of _possible_ groups has been |
| 188 | * increased on some sockets which is ok. |
| 189 | */ |
| 190 | rcu_read_unlock(); |
| 191 | goto out; |
| 192 | } |
| 193 | } |
| 194 | rcu_read_unlock(); |
| 195 | } else { |
| 196 | err = netlink_change_ngroups(init_net.genl_sock, |
| 197 | mc_groups_longs * BITS_PER_LONG); |
| 198 | if (err) |
| 199 | goto out; |
| 200 | } |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 201 | |
| 202 | grp->id = id; |
| 203 | set_bit(id, mc_groups); |
| 204 | list_add_tail(&grp->list, &family->mcast_groups); |
| 205 | grp->family = family; |
| 206 | |
| 207 | genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp); |
| 208 | out: |
| 209 | genl_unlock(); |
Thomas Graf | 79d310d | 2007-07-24 15:34:53 -0700 | [diff] [blame] | 210 | return err; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 211 | } |
| 212 | EXPORT_SYMBOL(genl_register_mc_group); |
| 213 | |
Thomas Graf | 79dc4386 | 2007-07-24 15:32:46 -0700 | [diff] [blame] | 214 | static void __genl_unregister_mc_group(struct genl_family *family, |
| 215 | struct genl_multicast_group *grp) |
| 216 | { |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 217 | struct net *net; |
Thomas Graf | 79dc4386 | 2007-07-24 15:32:46 -0700 | [diff] [blame] | 218 | BUG_ON(grp->family != family); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 219 | |
| 220 | rcu_read_lock(); |
| 221 | for_each_net_rcu(net) |
| 222 | netlink_clear_multicast_users(net->genl_sock, grp->id); |
| 223 | rcu_read_unlock(); |
| 224 | |
Thomas Graf | 79dc4386 | 2007-07-24 15:32:46 -0700 | [diff] [blame] | 225 | clear_bit(grp->id, mc_groups); |
| 226 | list_del(&grp->list); |
| 227 | genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp); |
| 228 | grp->id = 0; |
| 229 | grp->family = NULL; |
| 230 | } |
| 231 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 232 | /** |
| 233 | * genl_unregister_mc_group - unregister a multicast group |
| 234 | * |
| 235 | * Unregisters the specified multicast group and notifies userspace |
| 236 | * about it. All current listeners on the group are removed. |
| 237 | * |
| 238 | * Note: It is not necessary to unregister all multicast groups before |
| 239 | * unregistering the family, unregistering the family will cause |
| 240 | * all assigned multicast groups to be unregistered automatically. |
| 241 | * |
| 242 | * @family: Generic netlink family the group belongs to. |
| 243 | * @grp: The group to unregister, must have been registered successfully |
| 244 | * previously. |
| 245 | */ |
| 246 | void genl_unregister_mc_group(struct genl_family *family, |
| 247 | struct genl_multicast_group *grp) |
| 248 | { |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 249 | genl_lock(); |
Thomas Graf | 79dc4386 | 2007-07-24 15:32:46 -0700 | [diff] [blame] | 250 | __genl_unregister_mc_group(family, grp); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 251 | genl_unlock(); |
| 252 | } |
Inaky Perez-Gonzalez | 3efb40c | 2008-12-20 16:57:37 -0800 | [diff] [blame] | 253 | EXPORT_SYMBOL(genl_unregister_mc_group); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 254 | |
| 255 | static void genl_unregister_mc_groups(struct genl_family *family) |
| 256 | { |
| 257 | struct genl_multicast_group *grp, *tmp; |
| 258 | |
| 259 | list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list) |
Thomas Graf | 79dc4386 | 2007-07-24 15:32:46 -0700 | [diff] [blame] | 260 | __genl_unregister_mc_group(family, grp); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 263 | /** |
| 264 | * genl_register_ops - register generic netlink operations |
| 265 | * @family: generic netlink family |
| 266 | * @ops: operations to be registered |
| 267 | * |
| 268 | * Registers the specified operations and assigns them to the specified |
| 269 | * family. Either a doit or dumpit callback must be specified or the |
| 270 | * operation will fail. Only one operation structure per command |
| 271 | * identifier may be registered. |
| 272 | * |
| 273 | * See include/net/genetlink.h for more documenation on the operations |
| 274 | * structure. |
| 275 | * |
| 276 | * Returns 0 on success or a negative error code. |
| 277 | */ |
| 278 | int genl_register_ops(struct genl_family *family, struct genl_ops *ops) |
| 279 | { |
| 280 | int err = -EINVAL; |
| 281 | |
| 282 | if (ops->dumpit == NULL && ops->doit == NULL) |
| 283 | goto errout; |
| 284 | |
| 285 | if (genl_get_cmd(ops->cmd, family)) { |
| 286 | err = -EEXIST; |
| 287 | goto errout; |
| 288 | } |
| 289 | |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 290 | if (ops->dumpit) |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 291 | ops->flags |= GENL_CMD_CAP_DUMP; |
Jamal Hadi Salim | 48d4ed7 | 2006-12-06 20:06:25 -0800 | [diff] [blame] | 292 | if (ops->doit) |
| 293 | ops->flags |= GENL_CMD_CAP_DO; |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 294 | if (ops->policy) |
| 295 | ops->flags |= GENL_CMD_CAP_HASPOL; |
| 296 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 297 | genl_lock(); |
| 298 | list_add_tail(&ops->ops_list, &family->ops_list); |
| 299 | genl_unlock(); |
| 300 | |
| 301 | genl_ctrl_event(CTRL_CMD_NEWOPS, ops); |
| 302 | err = 0; |
| 303 | errout: |
| 304 | return err; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * genl_unregister_ops - unregister generic netlink operations |
| 309 | * @family: generic netlink family |
| 310 | * @ops: operations to be unregistered |
| 311 | * |
| 312 | * Unregisters the specified operations and unassigns them from the |
| 313 | * specified family. The operation blocks until the current message |
| 314 | * processing has finished and doesn't start again until the |
| 315 | * unregister process has finished. |
| 316 | * |
| 317 | * Note: It is not necessary to unregister all operations before |
| 318 | * unregistering the family, unregistering the family will cause |
| 319 | * all assigned operations to be unregistered automatically. |
| 320 | * |
| 321 | * Returns 0 on success or a negative error code. |
| 322 | */ |
| 323 | int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops) |
| 324 | { |
| 325 | struct genl_ops *rc; |
| 326 | |
| 327 | genl_lock(); |
| 328 | list_for_each_entry(rc, &family->ops_list, ops_list) { |
| 329 | if (rc == ops) { |
| 330 | list_del(&ops->ops_list); |
| 331 | genl_unlock(); |
| 332 | genl_ctrl_event(CTRL_CMD_DELOPS, ops); |
| 333 | return 0; |
| 334 | } |
| 335 | } |
| 336 | genl_unlock(); |
| 337 | |
| 338 | return -ENOENT; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * genl_register_family - register a generic netlink family |
| 343 | * @family: generic netlink family |
| 344 | * |
| 345 | * Registers the specified family after validating it first. Only one |
| 346 | * family may be registered with the same family name or identifier. |
| 347 | * The family id may equal GENL_ID_GENERATE causing an unique id to |
| 348 | * be automatically generated and assigned. |
| 349 | * |
| 350 | * Return 0 on success or a negative error code. |
| 351 | */ |
| 352 | int genl_register_family(struct genl_family *family) |
| 353 | { |
| 354 | int err = -EINVAL; |
| 355 | |
| 356 | if (family->id && family->id < GENL_MIN_ID) |
| 357 | goto errout; |
| 358 | |
| 359 | if (family->id > GENL_MAX_ID) |
| 360 | goto errout; |
| 361 | |
| 362 | INIT_LIST_HEAD(&family->ops_list); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 363 | INIT_LIST_HEAD(&family->mcast_groups); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 364 | |
| 365 | genl_lock(); |
| 366 | |
| 367 | if (genl_family_find_byname(family->name)) { |
| 368 | err = -EEXIST; |
| 369 | goto errout_locked; |
| 370 | } |
| 371 | |
| 372 | if (genl_family_find_byid(family->id)) { |
| 373 | err = -EEXIST; |
| 374 | goto errout_locked; |
| 375 | } |
| 376 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 377 | if (family->id == GENL_ID_GENERATE) { |
| 378 | u16 newid = genl_generate_id(); |
| 379 | |
| 380 | if (!newid) { |
| 381 | err = -ENOMEM; |
| 382 | goto errout_locked; |
| 383 | } |
| 384 | |
| 385 | family->id = newid; |
| 386 | } |
| 387 | |
| 388 | if (family->maxattr) { |
| 389 | family->attrbuf = kmalloc((family->maxattr+1) * |
| 390 | sizeof(struct nlattr *), GFP_KERNEL); |
| 391 | if (family->attrbuf == NULL) { |
| 392 | err = -ENOMEM; |
Jamal Hadi Salim | e200bd8 | 2006-02-13 15:51:24 -0800 | [diff] [blame] | 393 | goto errout_locked; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 394 | } |
| 395 | } else |
| 396 | family->attrbuf = NULL; |
| 397 | |
| 398 | list_add_tail(&family->family_list, genl_family_chain(family->id)); |
| 399 | genl_unlock(); |
| 400 | |
| 401 | genl_ctrl_event(CTRL_CMD_NEWFAMILY, family); |
| 402 | |
| 403 | return 0; |
| 404 | |
| 405 | errout_locked: |
| 406 | genl_unlock(); |
| 407 | errout: |
| 408 | return err; |
| 409 | } |
| 410 | |
| 411 | /** |
Michał Mirosław | a7b11d7 | 2009-05-21 10:34:04 +0000 | [diff] [blame] | 412 | * genl_register_family_with_ops - register a generic netlink family |
| 413 | * @family: generic netlink family |
| 414 | * @ops: operations to be registered |
| 415 | * @n_ops: number of elements to register |
| 416 | * |
| 417 | * Registers the specified family and operations from the specified table. |
| 418 | * Only one family may be registered with the same family name or identifier. |
| 419 | * |
| 420 | * The family id may equal GENL_ID_GENERATE causing an unique id to |
| 421 | * be automatically generated and assigned. |
| 422 | * |
| 423 | * Either a doit or dumpit callback must be specified for every registered |
| 424 | * operation or the function will fail. Only one operation structure per |
| 425 | * command identifier may be registered. |
| 426 | * |
| 427 | * See include/net/genetlink.h for more documenation on the operations |
| 428 | * structure. |
| 429 | * |
| 430 | * This is equivalent to calling genl_register_family() followed by |
| 431 | * genl_register_ops() for every operation entry in the table taking |
| 432 | * care to unregister the family on error path. |
| 433 | * |
| 434 | * Return 0 on success or a negative error code. |
| 435 | */ |
| 436 | int genl_register_family_with_ops(struct genl_family *family, |
| 437 | struct genl_ops *ops, size_t n_ops) |
| 438 | { |
| 439 | int err, i; |
| 440 | |
| 441 | err = genl_register_family(family); |
| 442 | if (err) |
| 443 | return err; |
| 444 | |
| 445 | for (i = 0; i < n_ops; ++i, ++ops) { |
| 446 | err = genl_register_ops(family, ops); |
| 447 | if (err) |
| 448 | goto err_out; |
| 449 | } |
| 450 | return 0; |
| 451 | err_out: |
| 452 | genl_unregister_family(family); |
| 453 | return err; |
| 454 | } |
| 455 | EXPORT_SYMBOL(genl_register_family_with_ops); |
| 456 | |
| 457 | /** |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 458 | * genl_unregister_family - unregister generic netlink family |
| 459 | * @family: generic netlink family |
| 460 | * |
| 461 | * Unregisters the specified family. |
| 462 | * |
| 463 | * Returns 0 on success or a negative error code. |
| 464 | */ |
| 465 | int genl_unregister_family(struct genl_family *family) |
| 466 | { |
| 467 | struct genl_family *rc; |
| 468 | |
| 469 | genl_lock(); |
| 470 | |
Pavel Emelyanov | 910d6c3 | 2008-02-12 22:16:33 -0800 | [diff] [blame] | 471 | genl_unregister_mc_groups(family); |
| 472 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 473 | list_for_each_entry(rc, genl_family_chain(family->id), family_list) { |
| 474 | if (family->id != rc->id || strcmp(rc->name, family->name)) |
| 475 | continue; |
| 476 | |
| 477 | list_del(&rc->family_list); |
| 478 | INIT_LIST_HEAD(&family->ops_list); |
| 479 | genl_unlock(); |
| 480 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 481 | kfree(family->attrbuf); |
| 482 | genl_ctrl_event(CTRL_CMD_DELFAMILY, family); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | genl_unlock(); |
| 487 | |
| 488 | return -ENOENT; |
| 489 | } |
| 490 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 491 | static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 492 | { |
| 493 | struct genl_ops *ops; |
| 494 | struct genl_family *family; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 495 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 496 | struct genl_info info; |
| 497 | struct genlmsghdr *hdr = nlmsg_data(nlh); |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 498 | int hdrlen, err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 499 | |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 500 | family = genl_family_find_byid(nlh->nlmsg_type); |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 501 | if (family == NULL) |
| 502 | return -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 503 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 504 | /* this family doesn't exist in this netns */ |
| 505 | if (!family->netnsok && !net_eq(net, &init_net)) |
| 506 | return -ENOENT; |
| 507 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 508 | hdrlen = GENL_HDRLEN + family->hdrsize; |
| 509 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 510 | return -EINVAL; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 511 | |
| 512 | ops = genl_get_cmd(hdr->cmd, family); |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 513 | if (ops == NULL) |
| 514 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 515 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 516 | if ((ops->flags & GENL_ADMIN_PERM) && |
| 517 | security_netlink_recv(skb, CAP_NET_ADMIN)) |
| 518 | return -EPERM; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 519 | |
| 520 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 521 | if (ops->dumpit == NULL) |
| 522 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 523 | |
Patrick McHardy | 6d1a3fb | 2008-06-18 02:07:07 -0700 | [diff] [blame] | 524 | genl_unlock(); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 525 | err = netlink_dump_start(net->genl_sock, skb, nlh, |
Patrick McHardy | 6d1a3fb | 2008-06-18 02:07:07 -0700 | [diff] [blame] | 526 | ops->dumpit, ops->done); |
| 527 | genl_lock(); |
| 528 | return err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 529 | } |
| 530 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 531 | if (ops->doit == NULL) |
| 532 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 533 | |
| 534 | if (family->attrbuf) { |
| 535 | err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr, |
| 536 | ops->policy); |
| 537 | if (err < 0) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 538 | return err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | info.snd_seq = nlh->nlmsg_seq; |
| 542 | info.snd_pid = NETLINK_CB(skb).pid; |
| 543 | info.nlhdr = nlh; |
| 544 | info.genlhdr = nlmsg_data(nlh); |
| 545 | info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN; |
| 546 | info.attrs = family->attrbuf; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 547 | genl_info_net_set(&info, net); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 548 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 549 | return ops->doit(skb, &info); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 550 | } |
| 551 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 552 | static void genl_rcv(struct sk_buff *skb) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 553 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 554 | genl_lock(); |
| 555 | netlink_rcv_skb(skb, &genl_rcv_msg); |
| 556 | genl_unlock(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /************************************************************************** |
| 560 | * Controller |
| 561 | **************************************************************************/ |
| 562 | |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 563 | static struct genl_family genl_ctrl = { |
| 564 | .id = GENL_ID_CTRL, |
| 565 | .name = "nlctrl", |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 566 | .version = 0x2, |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 567 | .maxattr = CTRL_ATTR_MAX, |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 568 | .netnsok = true, |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 569 | }; |
| 570 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 571 | static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq, |
| 572 | u32 flags, struct sk_buff *skb, u8 cmd) |
| 573 | { |
| 574 | void *hdr; |
| 575 | |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 576 | hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 577 | if (hdr == NULL) |
| 578 | return -1; |
| 579 | |
| 580 | NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name); |
| 581 | NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id); |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 582 | NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version); |
| 583 | NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize); |
| 584 | NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr); |
| 585 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 586 | if (!list_empty(&family->ops_list)) { |
| 587 | struct nlattr *nla_ops; |
| 588 | struct genl_ops *ops; |
| 589 | int idx = 1; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 590 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 591 | nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS); |
| 592 | if (nla_ops == NULL) |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 593 | goto nla_put_failure; |
| 594 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 595 | list_for_each_entry(ops, &family->ops_list, ops_list) { |
| 596 | struct nlattr *nest; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 597 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 598 | nest = nla_nest_start(skb, idx++); |
| 599 | if (nest == NULL) |
| 600 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 601 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 602 | NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd); |
| 603 | NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags); |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 604 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 605 | nla_nest_end(skb, nest); |
| 606 | } |
| 607 | |
| 608 | nla_nest_end(skb, nla_ops); |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 611 | if (!list_empty(&family->mcast_groups)) { |
| 612 | struct genl_multicast_group *grp; |
| 613 | struct nlattr *nla_grps; |
| 614 | int idx = 1; |
| 615 | |
| 616 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); |
| 617 | if (nla_grps == NULL) |
| 618 | goto nla_put_failure; |
| 619 | |
| 620 | list_for_each_entry(grp, &family->mcast_groups, list) { |
| 621 | struct nlattr *nest; |
| 622 | |
| 623 | nest = nla_nest_start(skb, idx++); |
| 624 | if (nest == NULL) |
| 625 | goto nla_put_failure; |
| 626 | |
| 627 | NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id); |
| 628 | NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME, |
| 629 | grp->name); |
| 630 | |
| 631 | nla_nest_end(skb, nest); |
| 632 | } |
| 633 | nla_nest_end(skb, nla_grps); |
| 634 | } |
| 635 | |
| 636 | return genlmsg_end(skb, hdr); |
| 637 | |
| 638 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 639 | genlmsg_cancel(skb, hdr); |
| 640 | return -EMSGSIZE; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 pid, |
| 644 | u32 seq, u32 flags, struct sk_buff *skb, |
| 645 | u8 cmd) |
| 646 | { |
| 647 | void *hdr; |
| 648 | struct nlattr *nla_grps; |
| 649 | struct nlattr *nest; |
| 650 | |
| 651 | hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd); |
| 652 | if (hdr == NULL) |
| 653 | return -1; |
| 654 | |
| 655 | NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name); |
| 656 | NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id); |
| 657 | |
| 658 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); |
| 659 | if (nla_grps == NULL) |
| 660 | goto nla_put_failure; |
| 661 | |
| 662 | nest = nla_nest_start(skb, 1); |
| 663 | if (nest == NULL) |
| 664 | goto nla_put_failure; |
| 665 | |
| 666 | NLA_PUT_U32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id); |
| 667 | NLA_PUT_STRING(skb, CTRL_ATTR_MCAST_GRP_NAME, |
| 668 | grp->name); |
| 669 | |
| 670 | nla_nest_end(skb, nest); |
| 671 | nla_nest_end(skb, nla_grps); |
| 672 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 673 | return genlmsg_end(skb, hdr); |
| 674 | |
| 675 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 676 | genlmsg_cancel(skb, hdr); |
| 677 | return -EMSGSIZE; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) |
| 681 | { |
| 682 | |
| 683 | int i, n = 0; |
| 684 | struct genl_family *rt; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 685 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 686 | int chains_to_skip = cb->args[0]; |
| 687 | int fams_to_skip = cb->args[1]; |
| 688 | |
| 689 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) { |
| 690 | if (i < chains_to_skip) |
| 691 | continue; |
| 692 | n = 0; |
| 693 | list_for_each_entry(rt, genl_family_chain(i), family_list) { |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 694 | if (!rt->netnsok && !net_eq(net, &init_net)) |
| 695 | continue; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 696 | if (++n < fams_to_skip) |
| 697 | continue; |
| 698 | if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid, |
| 699 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 700 | skb, CTRL_CMD_NEWFAMILY) < 0) |
| 701 | goto errout; |
| 702 | } |
| 703 | |
| 704 | fams_to_skip = 0; |
| 705 | } |
| 706 | |
| 707 | errout: |
| 708 | cb->args[0] = i; |
| 709 | cb->args[1] = n; |
| 710 | |
| 711 | return skb->len; |
| 712 | } |
| 713 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 714 | static struct sk_buff *ctrl_build_family_msg(struct genl_family *family, |
| 715 | u32 pid, int seq, u8 cmd) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 716 | { |
| 717 | struct sk_buff *skb; |
| 718 | int err; |
| 719 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 720 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 721 | if (skb == NULL) |
| 722 | return ERR_PTR(-ENOBUFS); |
| 723 | |
| 724 | err = ctrl_fill_info(family, pid, seq, 0, skb, cmd); |
| 725 | if (err < 0) { |
| 726 | nlmsg_free(skb); |
| 727 | return ERR_PTR(err); |
| 728 | } |
| 729 | |
| 730 | return skb; |
| 731 | } |
| 732 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 733 | static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp, |
| 734 | u32 pid, int seq, u8 cmd) |
| 735 | { |
| 736 | struct sk_buff *skb; |
| 737 | int err; |
| 738 | |
| 739 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 740 | if (skb == NULL) |
| 741 | return ERR_PTR(-ENOBUFS); |
| 742 | |
| 743 | err = ctrl_fill_mcgrp_info(grp, pid, seq, 0, skb, cmd); |
| 744 | if (err < 0) { |
| 745 | nlmsg_free(skb); |
| 746 | return ERR_PTR(err); |
| 747 | } |
| 748 | |
| 749 | return skb; |
| 750 | } |
| 751 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 752 | static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = { |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 753 | [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 754 | [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING, |
| 755 | .len = GENL_NAMSIZ - 1 }, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 756 | }; |
| 757 | |
| 758 | static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info) |
| 759 | { |
| 760 | struct sk_buff *msg; |
| 761 | struct genl_family *res = NULL; |
| 762 | int err = -EINVAL; |
| 763 | |
| 764 | if (info->attrs[CTRL_ATTR_FAMILY_ID]) { |
| 765 | u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]); |
| 766 | res = genl_family_find_byid(id); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 767 | err = -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | if (info->attrs[CTRL_ATTR_FAMILY_NAME]) { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 771 | char *name; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 772 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 773 | name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 774 | res = genl_family_find_byname(name); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 775 | err = -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 778 | if (res == NULL) |
| 779 | return err; |
| 780 | |
| 781 | if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) { |
| 782 | /* family doesn't exist here */ |
| 783 | return -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 784 | } |
| 785 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 786 | msg = ctrl_build_family_msg(res, info->snd_pid, info->snd_seq, |
| 787 | CTRL_CMD_NEWFAMILY); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 788 | if (IS_ERR(msg)) |
| 789 | return PTR_ERR(msg); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 790 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 791 | return genlmsg_reply(msg, info); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | static int genl_ctrl_event(int event, void *data) |
| 795 | { |
| 796 | struct sk_buff *msg; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 797 | struct genl_family *family; |
| 798 | struct genl_multicast_group *grp; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 799 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 800 | /* genl is still initialising */ |
| 801 | if (!init_net.genl_sock) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 802 | return 0; |
| 803 | |
| 804 | switch (event) { |
| 805 | case CTRL_CMD_NEWFAMILY: |
| 806 | case CTRL_CMD_DELFAMILY: |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 807 | family = data; |
| 808 | msg = ctrl_build_family_msg(family, 0, 0, event); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 809 | break; |
| 810 | case CTRL_CMD_NEWMCAST_GRP: |
| 811 | case CTRL_CMD_DELMCAST_GRP: |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 812 | grp = data; |
| 813 | family = grp->family; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 814 | msg = ctrl_build_mcgrp_msg(data, 0, 0, event); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 815 | break; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 816 | default: |
| 817 | return -EINVAL; |
| 818 | } |
| 819 | |
| 820 | if (IS_ERR(msg)) |
| 821 | return PTR_ERR(msg); |
| 822 | |
| 823 | if (!family->netnsok) { |
| 824 | genlmsg_multicast_netns(&init_net, msg, 0, |
| 825 | GENL_ID_CTRL, GFP_KERNEL); |
| 826 | } else { |
| 827 | rcu_read_lock(); |
| 828 | genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC); |
| 829 | rcu_read_unlock(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | static struct genl_ops genl_ctrl_ops = { |
| 836 | .cmd = CTRL_CMD_GETFAMILY, |
| 837 | .doit = ctrl_getfamily, |
| 838 | .dumpit = ctrl_dumpfamily, |
| 839 | .policy = ctrl_policy, |
| 840 | }; |
| 841 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 842 | static struct genl_multicast_group notify_grp = { |
| 843 | .name = "notify", |
| 844 | }; |
| 845 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 846 | static int __net_init genl_pernet_init(struct net *net) |
| 847 | { |
| 848 | /* we'll bump the group number right afterwards */ |
| 849 | net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0, |
| 850 | genl_rcv, &genl_mutex, |
| 851 | THIS_MODULE); |
| 852 | |
| 853 | if (!net->genl_sock && net_eq(net, &init_net)) |
| 854 | panic("GENL: Cannot initialize generic netlink\n"); |
| 855 | |
| 856 | if (!net->genl_sock) |
| 857 | return -ENOMEM; |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | static void __net_exit genl_pernet_exit(struct net *net) |
| 863 | { |
| 864 | netlink_kernel_release(net->genl_sock); |
| 865 | net->genl_sock = NULL; |
| 866 | } |
| 867 | |
| 868 | static struct pernet_operations genl_pernet_ops = { |
| 869 | .init = genl_pernet_init, |
| 870 | .exit = genl_pernet_exit, |
| 871 | }; |
| 872 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 873 | static int __init genl_init(void) |
| 874 | { |
| 875 | int i, err; |
| 876 | |
| 877 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) |
| 878 | INIT_LIST_HEAD(&family_ht[i]); |
| 879 | |
| 880 | err = genl_register_family(&genl_ctrl); |
| 881 | if (err < 0) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 882 | goto problem; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 883 | |
| 884 | err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops); |
| 885 | if (err < 0) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 886 | goto problem; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 887 | |
| 888 | netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 889 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 890 | err = register_pernet_subsys(&genl_pernet_ops); |
| 891 | if (err) |
| 892 | goto problem; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 893 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 894 | err = genl_register_mc_group(&genl_ctrl, ¬ify_grp); |
| 895 | if (err < 0) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 896 | goto problem; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 897 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 898 | return 0; |
| 899 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 900 | problem: |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 901 | panic("GENL: Cannot register controller: %d\n", err); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | subsys_initcall(genl_init); |
| 905 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 906 | EXPORT_SYMBOL(genl_register_ops); |
| 907 | EXPORT_SYMBOL(genl_unregister_ops); |
| 908 | EXPORT_SYMBOL(genl_register_family); |
| 909 | EXPORT_SYMBOL(genl_unregister_family); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame^] | 910 | |
| 911 | static int genlmsg_mcast(struct sk_buff *skb, u32 pid, unsigned long group, |
| 912 | gfp_t flags) |
| 913 | { |
| 914 | struct sk_buff *tmp; |
| 915 | struct net *net, *prev = NULL; |
| 916 | int err; |
| 917 | |
| 918 | for_each_net_rcu(net) { |
| 919 | if (prev) { |
| 920 | tmp = skb_clone(skb, flags); |
| 921 | if (!tmp) { |
| 922 | err = -ENOMEM; |
| 923 | goto error; |
| 924 | } |
| 925 | err = nlmsg_multicast(prev->genl_sock, tmp, |
| 926 | pid, group, flags); |
| 927 | if (err) |
| 928 | goto error; |
| 929 | } |
| 930 | |
| 931 | prev = net; |
| 932 | } |
| 933 | |
| 934 | return nlmsg_multicast(prev->genl_sock, skb, pid, group, flags); |
| 935 | error: |
| 936 | kfree_skb(skb); |
| 937 | return err; |
| 938 | } |
| 939 | |
| 940 | int genlmsg_multicast_allns(struct sk_buff *skb, u32 pid, unsigned int group, |
| 941 | gfp_t flags) |
| 942 | { |
| 943 | return genlmsg_mcast(skb, pid, group, flags); |
| 944 | } |
| 945 | EXPORT_SYMBOL(genlmsg_multicast_allns); |