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