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