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 | |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 109 | static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 110 | { |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 111 | int i; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 112 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 113 | for (i = 0; i < family->n_ops; i++) |
| 114 | if (family->ops[i].cmd == cmd) |
| 115 | return &family->ops[i]; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 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 | |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame^] | 286 | static int genl_validate_ops(struct genl_family *family) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 287 | { |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame^] | 288 | const struct genl_ops *ops = family->ops; |
| 289 | unsigned int n_ops = family->n_ops; |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 290 | int i, j; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 291 | |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame^] | 292 | if (WARN_ON(n_ops && !ops)) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | if (!n_ops) |
| 296 | return 0; |
| 297 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 298 | for (i = 0; i < n_ops; i++) { |
| 299 | if (ops[i].dumpit == NULL && ops[i].doit == NULL) |
| 300 | return -EINVAL; |
| 301 | for (j = i + 1; j < n_ops; j++) |
| 302 | if (ops[i].cmd == ops[j].cmd) |
| 303 | return -EINVAL; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 306 | /* family is not registered yet, so no locking needed */ |
| 307 | family->ops = ops; |
| 308 | family->n_ops = n_ops; |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 309 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 310 | return 0; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 311 | } |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 312 | |
| 313 | /** |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 314 | * __genl_register_family - register a generic netlink family |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 315 | * @family: generic netlink family |
| 316 | * |
| 317 | * Registers the specified family after validating it first. Only one |
| 318 | * family may be registered with the same family name or identifier. |
| 319 | * The family id may equal GENL_ID_GENERATE causing an unique id to |
| 320 | * be automatically generated and assigned. |
| 321 | * |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame^] | 322 | * The family's ops array must already be assigned, you can use the |
| 323 | * genl_register_family_with_ops() helper function. |
| 324 | * |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 325 | * Return 0 on success or a negative error code. |
| 326 | */ |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 327 | int __genl_register_family(struct genl_family *family) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 328 | { |
| 329 | int err = -EINVAL; |
| 330 | |
| 331 | if (family->id && family->id < GENL_MIN_ID) |
| 332 | goto errout; |
| 333 | |
| 334 | if (family->id > GENL_MAX_ID) |
| 335 | goto errout; |
| 336 | |
Johannes Berg | 568508a | 2013-11-15 14:19:08 +0100 | [diff] [blame^] | 337 | err = genl_validate_ops(family); |
| 338 | if (err) |
| 339 | return err; |
| 340 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 341 | INIT_LIST_HEAD(&family->mcast_groups); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 342 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 343 | genl_lock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 344 | |
| 345 | if (genl_family_find_byname(family->name)) { |
| 346 | err = -EEXIST; |
| 347 | goto errout_locked; |
| 348 | } |
| 349 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 350 | if (family->id == GENL_ID_GENERATE) { |
| 351 | u16 newid = genl_generate_id(); |
| 352 | |
| 353 | if (!newid) { |
| 354 | err = -ENOMEM; |
| 355 | goto errout_locked; |
| 356 | } |
| 357 | |
| 358 | family->id = newid; |
Krishna Kumar | 93860b0 | 2009-10-14 19:54:53 +0000 | [diff] [blame] | 359 | } else if (genl_family_find_byid(family->id)) { |
| 360 | err = -EEXIST; |
| 361 | goto errout_locked; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 364 | if (family->maxattr && !family->parallel_ops) { |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 365 | family->attrbuf = kmalloc((family->maxattr+1) * |
| 366 | sizeof(struct nlattr *), GFP_KERNEL); |
| 367 | if (family->attrbuf == NULL) { |
| 368 | err = -ENOMEM; |
Jamal Hadi Salim | e200bd8 | 2006-02-13 15:51:24 -0800 | [diff] [blame] | 369 | goto errout_locked; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 370 | } |
| 371 | } else |
| 372 | family->attrbuf = NULL; |
| 373 | |
| 374 | list_add_tail(&family->family_list, genl_family_chain(family->id)); |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 375 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 376 | |
| 377 | genl_ctrl_event(CTRL_CMD_NEWFAMILY, family); |
| 378 | |
| 379 | return 0; |
| 380 | |
| 381 | errout_locked: |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 382 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 383 | errout: |
| 384 | return err; |
| 385 | } |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 386 | EXPORT_SYMBOL(__genl_register_family); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 387 | |
| 388 | /** |
| 389 | * genl_unregister_family - unregister generic netlink family |
| 390 | * @family: generic netlink family |
| 391 | * |
| 392 | * Unregisters the specified family. |
| 393 | * |
| 394 | * Returns 0 on success or a negative error code. |
| 395 | */ |
| 396 | int genl_unregister_family(struct genl_family *family) |
| 397 | { |
| 398 | struct genl_family *rc; |
| 399 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 400 | genl_lock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 401 | |
Pavel Emelyanov | 910d6c3 | 2008-02-12 22:16:33 -0800 | [diff] [blame] | 402 | genl_unregister_mc_groups(family); |
| 403 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 404 | list_for_each_entry(rc, genl_family_chain(family->id), family_list) { |
| 405 | if (family->id != rc->id || strcmp(rc->name, family->name)) |
| 406 | continue; |
| 407 | |
| 408 | list_del(&rc->family_list); |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 409 | family->n_ops = 0; |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 410 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 411 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 412 | kfree(family->attrbuf); |
| 413 | genl_ctrl_event(CTRL_CMD_DELFAMILY, family); |
| 414 | return 0; |
| 415 | } |
| 416 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 417 | genl_unlock_all(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 418 | |
| 419 | return -ENOENT; |
| 420 | } |
Changli Gao | 416c2f9 | 2010-07-25 20:46:01 +0000 | [diff] [blame] | 421 | EXPORT_SYMBOL(genl_unregister_family); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 422 | |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 423 | /** |
| 424 | * genlmsg_put - Add generic netlink header to netlink message |
| 425 | * @skb: socket buffer holding the message |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 426 | * @portid: netlink portid the message is addressed to |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 427 | * @seq: sequence number (usually the one of the sender) |
| 428 | * @family: generic netlink family |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 429 | * @flags: netlink message flags |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 430 | * @cmd: generic netlink command |
| 431 | * |
| 432 | * Returns pointer to user specific header |
| 433 | */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 434 | void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 435 | struct genl_family *family, int flags, u8 cmd) |
| 436 | { |
| 437 | struct nlmsghdr *nlh; |
| 438 | struct genlmsghdr *hdr; |
| 439 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 440 | nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN + |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 441 | family->hdrsize, flags); |
| 442 | if (nlh == NULL) |
| 443 | return NULL; |
| 444 | |
| 445 | hdr = nlmsg_data(nlh); |
| 446 | hdr->cmd = cmd; |
| 447 | hdr->version = family->version; |
| 448 | hdr->reserved = 0; |
| 449 | |
| 450 | return (char *) hdr + GENL_HDRLEN; |
| 451 | } |
| 452 | EXPORT_SYMBOL(genlmsg_put); |
| 453 | |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 454 | static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb) |
| 455 | { |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 456 | /* our ops are always const - netlink API doesn't propagate that */ |
| 457 | const struct genl_ops *ops = cb->data; |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 458 | int rc; |
| 459 | |
| 460 | genl_lock(); |
| 461 | rc = ops->dumpit(skb, cb); |
| 462 | genl_unlock(); |
| 463 | return rc; |
| 464 | } |
| 465 | |
| 466 | static int genl_lock_done(struct netlink_callback *cb) |
| 467 | { |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 468 | /* our ops are always const - netlink API doesn't propagate that */ |
| 469 | const struct genl_ops *ops = cb->data; |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 470 | int rc = 0; |
| 471 | |
| 472 | if (ops->done) { |
| 473 | genl_lock(); |
| 474 | rc = ops->done(cb); |
| 475 | genl_unlock(); |
| 476 | } |
| 477 | return rc; |
| 478 | } |
| 479 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 480 | static int genl_family_rcv_msg(struct genl_family *family, |
| 481 | struct sk_buff *skb, |
| 482 | struct nlmsghdr *nlh) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 483 | { |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 484 | const struct genl_ops *ops; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 485 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 486 | struct genl_info info; |
| 487 | struct genlmsghdr *hdr = nlmsg_data(nlh); |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 488 | struct nlattr **attrbuf; |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 489 | int hdrlen, err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 490 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 491 | /* this family doesn't exist in this netns */ |
| 492 | if (!family->netnsok && !net_eq(net, &init_net)) |
| 493 | return -ENOENT; |
| 494 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 495 | hdrlen = GENL_HDRLEN + family->hdrsize; |
| 496 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 497 | return -EINVAL; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 498 | |
| 499 | ops = genl_get_cmd(hdr->cmd, family); |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 500 | if (ops == NULL) |
| 501 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 502 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 503 | if ((ops->flags & GENL_ADMIN_PERM) && |
Eric Paris | fd77846 | 2012-01-03 12:25:16 -0500 | [diff] [blame] | 504 | !capable(CAP_NET_ADMIN)) |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 505 | return -EPERM; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 506 | |
Pablo Neira | e1ee367 | 2013-07-29 12:30:04 +0200 | [diff] [blame] | 507 | if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) { |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 508 | int rc; |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 509 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 510 | if (ops->dumpit == NULL) |
| 511 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 512 | |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 513 | if (!family->parallel_ops) { |
| 514 | struct netlink_dump_control c = { |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 515 | .module = family->module, |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 516 | /* we have const, but the netlink API doesn't */ |
| 517 | .data = (void *)ops, |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 518 | .dump = genl_lock_dumpit, |
| 519 | .done = genl_lock_done, |
| 520 | }; |
| 521 | |
| 522 | genl_unlock(); |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 523 | rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c); |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 524 | genl_lock(); |
| 525 | |
| 526 | } else { |
| 527 | struct netlink_dump_control c = { |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 528 | .module = family->module, |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 529 | .dump = ops->dumpit, |
| 530 | .done = ops->done, |
| 531 | }; |
| 532 | |
Pravin B Shelar | 33c6b1f | 2013-08-23 12:45:04 -0700 | [diff] [blame] | 533 | rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c); |
Pravin B Shelar | 9b96309 | 2013-08-23 12:44:55 -0700 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | return rc; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 539 | if (ops->doit == NULL) |
| 540 | return -EOPNOTSUPP; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 541 | |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 542 | if (family->maxattr && family->parallel_ops) { |
| 543 | attrbuf = kmalloc((family->maxattr+1) * |
| 544 | sizeof(struct nlattr *), GFP_KERNEL); |
| 545 | if (attrbuf == NULL) |
| 546 | return -ENOMEM; |
| 547 | } else |
| 548 | attrbuf = family->attrbuf; |
| 549 | |
| 550 | if (attrbuf) { |
| 551 | err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 552 | ops->policy); |
| 553 | if (err < 0) |
Wei Yongjun | 50754d21 | 2013-04-26 15:34:16 +0000 | [diff] [blame] | 554 | goto out; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | info.snd_seq = nlh->nlmsg_seq; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 558 | info.snd_portid = NETLINK_CB(skb).portid; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 559 | info.nlhdr = nlh; |
| 560 | info.genlhdr = nlmsg_data(nlh); |
| 561 | info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN; |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 562 | info.attrs = attrbuf; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 563 | genl_info_net_set(&info, net); |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 564 | memset(&info.user_ptr, 0, sizeof(info.user_ptr)); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 565 | |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 566 | if (family->pre_doit) { |
| 567 | err = family->pre_doit(ops, skb, &info); |
| 568 | if (err) |
Wei Yongjun | 50754d21 | 2013-04-26 15:34:16 +0000 | [diff] [blame] | 569 | goto out; |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | err = ops->doit(skb, &info); |
| 573 | |
| 574 | if (family->post_doit) |
| 575 | family->post_doit(ops, skb, &info); |
| 576 | |
Wei Yongjun | 50754d21 | 2013-04-26 15:34:16 +0000 | [diff] [blame] | 577 | out: |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 578 | if (family->parallel_ops) |
| 579 | kfree(attrbuf); |
| 580 | |
| 581 | return err; |
| 582 | } |
| 583 | |
| 584 | static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
| 585 | { |
| 586 | struct genl_family *family; |
| 587 | int err; |
| 588 | |
| 589 | family = genl_family_find_byid(nlh->nlmsg_type); |
| 590 | if (family == NULL) |
| 591 | return -ENOENT; |
| 592 | |
| 593 | if (!family->parallel_ops) |
| 594 | genl_lock(); |
| 595 | |
| 596 | err = genl_family_rcv_msg(family, skb, nlh); |
| 597 | |
| 598 | if (!family->parallel_ops) |
| 599 | genl_unlock(); |
| 600 | |
Johannes Berg | ff4c92d | 2010-10-04 21:14:03 +0200 | [diff] [blame] | 601 | return err; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 602 | } |
| 603 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 604 | static void genl_rcv(struct sk_buff *skb) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 605 | { |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 606 | down_read(&cb_lock); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 607 | netlink_rcv_skb(skb, &genl_rcv_msg); |
Pravin B Shelar | def3117 | 2013-04-23 07:48:30 +0000 | [diff] [blame] | 608 | up_read(&cb_lock); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /************************************************************************** |
| 612 | * Controller |
| 613 | **************************************************************************/ |
| 614 | |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 615 | static struct genl_family genl_ctrl = { |
| 616 | .id = GENL_ID_CTRL, |
| 617 | .name = "nlctrl", |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 618 | .version = 0x2, |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 619 | .maxattr = CTRL_ATTR_MAX, |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 620 | .netnsok = true, |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 621 | }; |
| 622 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 623 | 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] | 624 | u32 flags, struct sk_buff *skb, u8 cmd) |
| 625 | { |
| 626 | void *hdr; |
| 627 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 628 | hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 629 | if (hdr == NULL) |
| 630 | return -1; |
| 631 | |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 632 | if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) || |
| 633 | nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) || |
| 634 | nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) || |
| 635 | nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) || |
| 636 | nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr)) |
| 637 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 638 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 639 | if (family->n_ops) { |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 640 | struct nlattr *nla_ops; |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 641 | int i; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 642 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 643 | nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS); |
| 644 | if (nla_ops == NULL) |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 645 | goto nla_put_failure; |
| 646 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 647 | for (i = 0; i < family->n_ops; i++) { |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 648 | struct nlattr *nest; |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 649 | const struct genl_ops *ops = &family->ops[i]; |
| 650 | u32 flags = ops->flags; |
| 651 | |
| 652 | if (ops->dumpit) |
| 653 | flags |= GENL_CMD_CAP_DUMP; |
| 654 | if (ops->doit) |
| 655 | flags |= GENL_CMD_CAP_DO; |
| 656 | if (ops->policy) |
| 657 | flags |= GENL_CMD_CAP_HASPOL; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 658 | |
Johannes Berg | d91824c | 2013-11-14 17:14:44 +0100 | [diff] [blame] | 659 | nest = nla_nest_start(skb, i + 1); |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 660 | if (nest == NULL) |
| 661 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 662 | |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 663 | if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) || |
Johannes Berg | f84f771 | 2013-11-14 17:14:45 +0100 | [diff] [blame] | 664 | nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, flags)) |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 665 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 666 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 667 | nla_nest_end(skb, nest); |
| 668 | } |
| 669 | |
| 670 | nla_nest_end(skb, nla_ops); |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 673 | if (!list_empty(&family->mcast_groups)) { |
| 674 | struct genl_multicast_group *grp; |
| 675 | struct nlattr *nla_grps; |
| 676 | int idx = 1; |
| 677 | |
| 678 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); |
| 679 | if (nla_grps == NULL) |
| 680 | goto nla_put_failure; |
| 681 | |
| 682 | list_for_each_entry(grp, &family->mcast_groups, list) { |
| 683 | struct nlattr *nest; |
| 684 | |
| 685 | nest = nla_nest_start(skb, idx++); |
| 686 | if (nest == NULL) |
| 687 | goto nla_put_failure; |
| 688 | |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 689 | if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) || |
| 690 | nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME, |
| 691 | grp->name)) |
| 692 | goto nla_put_failure; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 693 | |
| 694 | nla_nest_end(skb, nest); |
| 695 | } |
| 696 | nla_nest_end(skb, nla_grps); |
| 697 | } |
| 698 | |
| 699 | return genlmsg_end(skb, hdr); |
| 700 | |
| 701 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 702 | genlmsg_cancel(skb, hdr); |
| 703 | return -EMSGSIZE; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 706 | 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] | 707 | u32 seq, u32 flags, struct sk_buff *skb, |
| 708 | u8 cmd) |
| 709 | { |
| 710 | void *hdr; |
| 711 | struct nlattr *nla_grps; |
| 712 | struct nlattr *nest; |
| 713 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 714 | hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 715 | if (hdr == NULL) |
| 716 | return -1; |
| 717 | |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 718 | if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) || |
| 719 | nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id)) |
| 720 | goto nla_put_failure; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 721 | |
| 722 | nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); |
| 723 | if (nla_grps == NULL) |
| 724 | goto nla_put_failure; |
| 725 | |
| 726 | nest = nla_nest_start(skb, 1); |
| 727 | if (nest == NULL) |
| 728 | goto nla_put_failure; |
| 729 | |
David S. Miller | 444653f | 2012-03-29 23:25:11 -0400 | [diff] [blame] | 730 | if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) || |
| 731 | nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME, |
| 732 | grp->name)) |
| 733 | goto nla_put_failure; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 734 | |
| 735 | nla_nest_end(skb, nest); |
| 736 | nla_nest_end(skb, nla_grps); |
| 737 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 738 | return genlmsg_end(skb, hdr); |
| 739 | |
| 740 | nla_put_failure: |
Thomas Graf | bc3ed28 | 2008-06-03 16:36:54 -0700 | [diff] [blame] | 741 | genlmsg_cancel(skb, hdr); |
| 742 | return -EMSGSIZE; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) |
| 746 | { |
| 747 | |
| 748 | int i, n = 0; |
| 749 | struct genl_family *rt; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 750 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 751 | int chains_to_skip = cb->args[0]; |
| 752 | int fams_to_skip = cb->args[1]; |
| 753 | |
Samir Bellabes | e1d5a01 | 2010-01-07 22:10:56 +0000 | [diff] [blame] | 754 | for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) { |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 755 | n = 0; |
| 756 | list_for_each_entry(rt, genl_family_chain(i), family_list) { |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 757 | if (!rt->netnsok && !net_eq(net, &init_net)) |
| 758 | continue; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 759 | if (++n < fams_to_skip) |
| 760 | continue; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 761 | if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 762 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 763 | skb, CTRL_CMD_NEWFAMILY) < 0) |
| 764 | goto errout; |
| 765 | } |
| 766 | |
| 767 | fams_to_skip = 0; |
| 768 | } |
| 769 | |
| 770 | errout: |
| 771 | cb->args[0] = i; |
| 772 | cb->args[1] = n; |
| 773 | |
| 774 | return skb->len; |
| 775 | } |
| 776 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 777 | 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] | 778 | u32 portid, int seq, u8 cmd) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 779 | { |
| 780 | struct sk_buff *skb; |
| 781 | int err; |
| 782 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 783 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 784 | if (skb == NULL) |
| 785 | return ERR_PTR(-ENOBUFS); |
| 786 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 787 | err = ctrl_fill_info(family, portid, seq, 0, skb, cmd); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 788 | if (err < 0) { |
| 789 | nlmsg_free(skb); |
| 790 | return ERR_PTR(err); |
| 791 | } |
| 792 | |
| 793 | return skb; |
| 794 | } |
| 795 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 796 | 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] | 797 | u32 portid, int seq, u8 cmd) |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 798 | { |
| 799 | struct sk_buff *skb; |
| 800 | int err; |
| 801 | |
| 802 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 803 | if (skb == NULL) |
| 804 | return ERR_PTR(-ENOBUFS); |
| 805 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 806 | err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 807 | if (err < 0) { |
| 808 | nlmsg_free(skb); |
| 809 | return ERR_PTR(err); |
| 810 | } |
| 811 | |
| 812 | return skb; |
| 813 | } |
| 814 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 815 | static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = { |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 816 | [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 817 | [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING, |
| 818 | .len = GENL_NAMSIZ - 1 }, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 819 | }; |
| 820 | |
| 821 | static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info) |
| 822 | { |
| 823 | struct sk_buff *msg; |
| 824 | struct genl_family *res = NULL; |
| 825 | int err = -EINVAL; |
| 826 | |
| 827 | if (info->attrs[CTRL_ATTR_FAMILY_ID]) { |
| 828 | u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]); |
| 829 | res = genl_family_find_byid(id); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 830 | err = -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | if (info->attrs[CTRL_ATTR_FAMILY_NAME]) { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 834 | char *name; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 835 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 836 | name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 837 | res = genl_family_find_byname(name); |
Stephen Hemminger | fa84309 | 2011-12-28 13:48:55 -0500 | [diff] [blame] | 838 | #ifdef CONFIG_MODULES |
| 839 | if (res == NULL) { |
| 840 | genl_unlock(); |
Stanislaw Gruszka | c74f2b2 | 2013-07-26 11:00:10 +0200 | [diff] [blame] | 841 | up_read(&cb_lock); |
Neil Horman | e9412c3 | 2012-05-29 09:30:41 +0000 | [diff] [blame] | 842 | request_module("net-pf-%d-proto-%d-family-%s", |
Stephen Hemminger | fa84309 | 2011-12-28 13:48:55 -0500 | [diff] [blame] | 843 | PF_NETLINK, NETLINK_GENERIC, name); |
Stanislaw Gruszka | c74f2b2 | 2013-07-26 11:00:10 +0200 | [diff] [blame] | 844 | down_read(&cb_lock); |
Stephen Hemminger | fa84309 | 2011-12-28 13:48:55 -0500 | [diff] [blame] | 845 | genl_lock(); |
| 846 | res = genl_family_find_byname(name); |
| 847 | } |
| 848 | #endif |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 849 | err = -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 850 | } |
| 851 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 852 | if (res == NULL) |
| 853 | return err; |
| 854 | |
| 855 | if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) { |
| 856 | /* family doesn't exist here */ |
| 857 | return -ENOENT; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 860 | msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq, |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 861 | CTRL_CMD_NEWFAMILY); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 862 | if (IS_ERR(msg)) |
| 863 | return PTR_ERR(msg); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 864 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 865 | return genlmsg_reply(msg, info); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static int genl_ctrl_event(int event, void *data) |
| 869 | { |
| 870 | struct sk_buff *msg; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 871 | struct genl_family *family; |
| 872 | struct genl_multicast_group *grp; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 873 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 874 | /* genl is still initialising */ |
| 875 | if (!init_net.genl_sock) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 876 | return 0; |
| 877 | |
| 878 | switch (event) { |
| 879 | case CTRL_CMD_NEWFAMILY: |
| 880 | case CTRL_CMD_DELFAMILY: |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 881 | family = data; |
| 882 | msg = ctrl_build_family_msg(family, 0, 0, event); |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 883 | break; |
| 884 | case CTRL_CMD_NEWMCAST_GRP: |
| 885 | case CTRL_CMD_DELMCAST_GRP: |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 886 | grp = data; |
| 887 | family = grp->family; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 888 | msg = ctrl_build_mcgrp_msg(data, 0, 0, event); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 889 | break; |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 890 | default: |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | |
| 894 | if (IS_ERR(msg)) |
| 895 | return PTR_ERR(msg); |
| 896 | |
| 897 | if (!family->netnsok) { |
| 898 | genlmsg_multicast_netns(&init_net, msg, 0, |
| 899 | GENL_ID_CTRL, GFP_KERNEL); |
| 900 | } else { |
| 901 | rcu_read_lock(); |
| 902 | genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC); |
| 903 | rcu_read_unlock(); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | static struct genl_ops genl_ctrl_ops = { |
| 910 | .cmd = CTRL_CMD_GETFAMILY, |
| 911 | .doit = ctrl_getfamily, |
| 912 | .dumpit = ctrl_dumpfamily, |
| 913 | .policy = ctrl_policy, |
| 914 | }; |
| 915 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 916 | static struct genl_multicast_group notify_grp = { |
| 917 | .name = "notify", |
| 918 | }; |
| 919 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 920 | static int __net_init genl_pernet_init(struct net *net) |
| 921 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 922 | struct netlink_kernel_cfg cfg = { |
| 923 | .input = genl_rcv, |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 924 | .flags = NL_CFG_F_NONROOT_RECV, |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 925 | }; |
| 926 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 927 | /* we'll bump the group number right afterwards */ |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 928 | net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 929 | |
| 930 | if (!net->genl_sock && net_eq(net, &init_net)) |
| 931 | panic("GENL: Cannot initialize generic netlink\n"); |
| 932 | |
| 933 | if (!net->genl_sock) |
| 934 | return -ENOMEM; |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | static void __net_exit genl_pernet_exit(struct net *net) |
| 940 | { |
| 941 | netlink_kernel_release(net->genl_sock); |
| 942 | net->genl_sock = NULL; |
| 943 | } |
| 944 | |
| 945 | static struct pernet_operations genl_pernet_ops = { |
| 946 | .init = genl_pernet_init, |
| 947 | .exit = genl_pernet_exit, |
| 948 | }; |
| 949 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 950 | static int __init genl_init(void) |
| 951 | { |
| 952 | int i, err; |
| 953 | |
| 954 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) |
| 955 | INIT_LIST_HEAD(&family_ht[i]); |
| 956 | |
Changli Gao | 652c671 | 2010-07-25 23:21:05 +0000 | [diff] [blame] | 957 | err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 958 | if (err < 0) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 959 | goto problem; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 960 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 961 | err = register_pernet_subsys(&genl_pernet_ops); |
| 962 | if (err) |
| 963 | goto problem; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 964 | |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 965 | err = genl_register_mc_group(&genl_ctrl, ¬ify_grp); |
| 966 | if (err < 0) |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 967 | goto problem; |
Johannes Berg | 2dbba6f | 2007-07-18 15:47:52 -0700 | [diff] [blame] | 968 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 969 | return 0; |
| 970 | |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 971 | problem: |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 972 | panic("GENL: Cannot register controller: %d\n", err); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | subsys_initcall(genl_init); |
| 976 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 977 | 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] | 978 | gfp_t flags) |
| 979 | { |
| 980 | struct sk_buff *tmp; |
| 981 | struct net *net, *prev = NULL; |
| 982 | int err; |
| 983 | |
| 984 | for_each_net_rcu(net) { |
| 985 | if (prev) { |
| 986 | tmp = skb_clone(skb, flags); |
| 987 | if (!tmp) { |
| 988 | err = -ENOMEM; |
| 989 | goto error; |
| 990 | } |
| 991 | err = nlmsg_multicast(prev->genl_sock, tmp, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 992 | portid, group, flags); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 993 | if (err) |
| 994 | goto error; |
| 995 | } |
| 996 | |
| 997 | prev = net; |
| 998 | } |
| 999 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1000 | return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1001 | error: |
| 1002 | kfree_skb(skb); |
| 1003 | return err; |
| 1004 | } |
| 1005 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1006 | 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] | 1007 | gfp_t flags) |
| 1008 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1009 | return genlmsg_mcast(skb, portid, group, flags); |
Johannes Berg | 134e637 | 2009-07-10 09:51:34 +0000 | [diff] [blame] | 1010 | } |
| 1011 | EXPORT_SYMBOL(genlmsg_multicast_allns); |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1012 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1013 | 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] | 1014 | struct nlmsghdr *nlh, gfp_t flags) |
| 1015 | { |
| 1016 | struct sock *sk = net->genl_sock; |
| 1017 | int report = 0; |
| 1018 | |
| 1019 | if (nlh) |
| 1020 | report = nlmsg_report(nlh); |
| 1021 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1022 | nlmsg_notify(sk, skb, portid, group, report, flags); |
Pravin B Shelar | 263ba61 | 2011-11-10 19:14:37 -0800 | [diff] [blame] | 1023 | } |
| 1024 | EXPORT_SYMBOL(genl_notify); |