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