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