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