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