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