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> |
| 6 | */ |
| 7 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/socket.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/skbuff.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 16 | #include <net/sock.h> |
| 17 | #include <net/genetlink.h> |
| 18 | |
| 19 | struct sock *genl_sock = NULL; |
| 20 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 21 | static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */ |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 22 | |
| 23 | static void genl_lock(void) |
| 24 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 25 | mutex_lock(&genl_mutex); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static int genl_trylock(void) |
| 29 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 30 | return !mutex_trylock(&genl_mutex); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void genl_unlock(void) |
| 34 | { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 35 | mutex_unlock(&genl_mutex); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 36 | |
| 37 | if (genl_sock && genl_sock->sk_receive_queue.qlen) |
| 38 | genl_sock->sk_data_ready(genl_sock, 0); |
| 39 | } |
| 40 | |
| 41 | #define GENL_FAM_TAB_SIZE 16 |
| 42 | #define GENL_FAM_TAB_MASK (GENL_FAM_TAB_SIZE - 1) |
| 43 | |
| 44 | static struct list_head family_ht[GENL_FAM_TAB_SIZE]; |
| 45 | |
| 46 | static int genl_ctrl_event(int event, void *data); |
| 47 | |
| 48 | static inline unsigned int genl_family_hash(unsigned int id) |
| 49 | { |
| 50 | return id & GENL_FAM_TAB_MASK; |
| 51 | } |
| 52 | |
| 53 | static inline struct list_head *genl_family_chain(unsigned int id) |
| 54 | { |
| 55 | return &family_ht[genl_family_hash(id)]; |
| 56 | } |
| 57 | |
| 58 | static struct genl_family *genl_family_find_byid(unsigned int id) |
| 59 | { |
| 60 | struct genl_family *f; |
| 61 | |
| 62 | list_for_each_entry(f, genl_family_chain(id), family_list) |
| 63 | if (f->id == id) |
| 64 | return f; |
| 65 | |
| 66 | return NULL; |
| 67 | } |
| 68 | |
| 69 | static struct genl_family *genl_family_find_byname(char *name) |
| 70 | { |
| 71 | struct genl_family *f; |
| 72 | int i; |
| 73 | |
| 74 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) |
| 75 | list_for_each_entry(f, genl_family_chain(i), family_list) |
| 76 | if (strcmp(f->name, name) == 0) |
| 77 | return f; |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family) |
| 83 | { |
| 84 | struct genl_ops *ops; |
| 85 | |
| 86 | list_for_each_entry(ops, &family->ops_list, ops_list) |
| 87 | if (ops->cmd == cmd) |
| 88 | return ops; |
| 89 | |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | /* Of course we are going to have problems once we hit |
| 94 | * 2^16 alive types, but that can only happen by year 2K |
| 95 | */ |
| 96 | static inline u16 genl_generate_id(void) |
| 97 | { |
| 98 | static u16 id_gen_idx; |
| 99 | int overflowed = 0; |
| 100 | |
| 101 | do { |
| 102 | if (id_gen_idx == 0) |
| 103 | id_gen_idx = GENL_MIN_ID; |
| 104 | |
| 105 | if (++id_gen_idx > GENL_MAX_ID) { |
| 106 | if (!overflowed) { |
| 107 | overflowed = 1; |
| 108 | id_gen_idx = 0; |
| 109 | continue; |
| 110 | } else |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | } while (genl_family_find_byid(id_gen_idx)); |
| 115 | |
| 116 | return id_gen_idx; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * genl_register_ops - register generic netlink operations |
| 121 | * @family: generic netlink family |
| 122 | * @ops: operations to be registered |
| 123 | * |
| 124 | * Registers the specified operations and assigns them to the specified |
| 125 | * family. Either a doit or dumpit callback must be specified or the |
| 126 | * operation will fail. Only one operation structure per command |
| 127 | * identifier may be registered. |
| 128 | * |
| 129 | * See include/net/genetlink.h for more documenation on the operations |
| 130 | * structure. |
| 131 | * |
| 132 | * Returns 0 on success or a negative error code. |
| 133 | */ |
| 134 | int genl_register_ops(struct genl_family *family, struct genl_ops *ops) |
| 135 | { |
| 136 | int err = -EINVAL; |
| 137 | |
| 138 | if (ops->dumpit == NULL && ops->doit == NULL) |
| 139 | goto errout; |
| 140 | |
| 141 | if (genl_get_cmd(ops->cmd, family)) { |
| 142 | err = -EEXIST; |
| 143 | goto errout; |
| 144 | } |
| 145 | |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 146 | if (ops->dumpit) |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 147 | ops->flags |= GENL_CMD_CAP_DUMP; |
Jamal Hadi Salim | 48d4ed7 | 2006-12-06 20:06:25 -0800 | [diff] [blame] | 148 | if (ops->doit) |
| 149 | ops->flags |= GENL_CMD_CAP_DO; |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 150 | if (ops->policy) |
| 151 | ops->flags |= GENL_CMD_CAP_HASPOL; |
| 152 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 153 | genl_lock(); |
| 154 | list_add_tail(&ops->ops_list, &family->ops_list); |
| 155 | genl_unlock(); |
| 156 | |
| 157 | genl_ctrl_event(CTRL_CMD_NEWOPS, ops); |
| 158 | err = 0; |
| 159 | errout: |
| 160 | return err; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * genl_unregister_ops - unregister generic netlink operations |
| 165 | * @family: generic netlink family |
| 166 | * @ops: operations to be unregistered |
| 167 | * |
| 168 | * Unregisters the specified operations and unassigns them from the |
| 169 | * specified family. The operation blocks until the current message |
| 170 | * processing has finished and doesn't start again until the |
| 171 | * unregister process has finished. |
| 172 | * |
| 173 | * Note: It is not necessary to unregister all operations before |
| 174 | * unregistering the family, unregistering the family will cause |
| 175 | * all assigned operations to be unregistered automatically. |
| 176 | * |
| 177 | * Returns 0 on success or a negative error code. |
| 178 | */ |
| 179 | int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops) |
| 180 | { |
| 181 | struct genl_ops *rc; |
| 182 | |
| 183 | genl_lock(); |
| 184 | list_for_each_entry(rc, &family->ops_list, ops_list) { |
| 185 | if (rc == ops) { |
| 186 | list_del(&ops->ops_list); |
| 187 | genl_unlock(); |
| 188 | genl_ctrl_event(CTRL_CMD_DELOPS, ops); |
| 189 | return 0; |
| 190 | } |
| 191 | } |
| 192 | genl_unlock(); |
| 193 | |
| 194 | return -ENOENT; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * genl_register_family - register a generic netlink family |
| 199 | * @family: generic netlink family |
| 200 | * |
| 201 | * Registers the specified family after validating it first. Only one |
| 202 | * family may be registered with the same family name or identifier. |
| 203 | * The family id may equal GENL_ID_GENERATE causing an unique id to |
| 204 | * be automatically generated and assigned. |
| 205 | * |
| 206 | * Return 0 on success or a negative error code. |
| 207 | */ |
| 208 | int genl_register_family(struct genl_family *family) |
| 209 | { |
| 210 | int err = -EINVAL; |
| 211 | |
| 212 | if (family->id && family->id < GENL_MIN_ID) |
| 213 | goto errout; |
| 214 | |
| 215 | if (family->id > GENL_MAX_ID) |
| 216 | goto errout; |
| 217 | |
| 218 | INIT_LIST_HEAD(&family->ops_list); |
| 219 | |
| 220 | genl_lock(); |
| 221 | |
| 222 | if (genl_family_find_byname(family->name)) { |
| 223 | err = -EEXIST; |
| 224 | goto errout_locked; |
| 225 | } |
| 226 | |
| 227 | if (genl_family_find_byid(family->id)) { |
| 228 | err = -EEXIST; |
| 229 | goto errout_locked; |
| 230 | } |
| 231 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 232 | if (family->id == GENL_ID_GENERATE) { |
| 233 | u16 newid = genl_generate_id(); |
| 234 | |
| 235 | if (!newid) { |
| 236 | err = -ENOMEM; |
| 237 | goto errout_locked; |
| 238 | } |
| 239 | |
| 240 | family->id = newid; |
| 241 | } |
| 242 | |
| 243 | if (family->maxattr) { |
| 244 | family->attrbuf = kmalloc((family->maxattr+1) * |
| 245 | sizeof(struct nlattr *), GFP_KERNEL); |
| 246 | if (family->attrbuf == NULL) { |
| 247 | err = -ENOMEM; |
Jamal Hadi Salim | e200bd8 | 2006-02-13 15:51:24 -0800 | [diff] [blame] | 248 | goto errout_locked; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 249 | } |
| 250 | } else |
| 251 | family->attrbuf = NULL; |
| 252 | |
| 253 | list_add_tail(&family->family_list, genl_family_chain(family->id)); |
| 254 | genl_unlock(); |
| 255 | |
| 256 | genl_ctrl_event(CTRL_CMD_NEWFAMILY, family); |
| 257 | |
| 258 | return 0; |
| 259 | |
| 260 | errout_locked: |
| 261 | genl_unlock(); |
| 262 | errout: |
| 263 | return err; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * genl_unregister_family - unregister generic netlink family |
| 268 | * @family: generic netlink family |
| 269 | * |
| 270 | * Unregisters the specified family. |
| 271 | * |
| 272 | * Returns 0 on success or a negative error code. |
| 273 | */ |
| 274 | int genl_unregister_family(struct genl_family *family) |
| 275 | { |
| 276 | struct genl_family *rc; |
| 277 | |
| 278 | genl_lock(); |
| 279 | |
| 280 | list_for_each_entry(rc, genl_family_chain(family->id), family_list) { |
| 281 | if (family->id != rc->id || strcmp(rc->name, family->name)) |
| 282 | continue; |
| 283 | |
| 284 | list_del(&rc->family_list); |
| 285 | INIT_LIST_HEAD(&family->ops_list); |
| 286 | genl_unlock(); |
| 287 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 288 | kfree(family->attrbuf); |
| 289 | genl_ctrl_event(CTRL_CMD_DELFAMILY, family); |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | genl_unlock(); |
| 294 | |
| 295 | return -ENOENT; |
| 296 | } |
| 297 | |
Jamal Hadi Salim | e200bd8 | 2006-02-13 15:51:24 -0800 | [diff] [blame] | 298 | static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 299 | int *errp) |
| 300 | { |
| 301 | struct genl_ops *ops; |
| 302 | struct genl_family *family; |
| 303 | struct genl_info info; |
| 304 | struct genlmsghdr *hdr = nlmsg_data(nlh); |
| 305 | int hdrlen, err = -EINVAL; |
| 306 | |
| 307 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) |
| 308 | goto ignore; |
| 309 | |
| 310 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) |
| 311 | goto ignore; |
| 312 | |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 313 | family = genl_family_find_byid(nlh->nlmsg_type); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 314 | if (family == NULL) { |
| 315 | err = -ENOENT; |
| 316 | goto errout; |
| 317 | } |
| 318 | |
| 319 | hdrlen = GENL_HDRLEN + family->hdrsize; |
| 320 | if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen)) |
| 321 | goto errout; |
| 322 | |
| 323 | ops = genl_get_cmd(hdr->cmd, family); |
| 324 | if (ops == NULL) { |
| 325 | err = -EOPNOTSUPP; |
| 326 | goto errout; |
| 327 | } |
| 328 | |
Darrel Goeddel | c7bdb54 | 2006-06-27 13:26:11 -0700 | [diff] [blame] | 329 | if ((ops->flags & GENL_ADMIN_PERM) && security_netlink_recv(skb, CAP_NET_ADMIN)) { |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 330 | err = -EPERM; |
| 331 | goto errout; |
| 332 | } |
| 333 | |
| 334 | if (nlh->nlmsg_flags & NLM_F_DUMP) { |
| 335 | if (ops->dumpit == NULL) { |
| 336 | err = -EOPNOTSUPP; |
| 337 | goto errout; |
| 338 | } |
| 339 | |
| 340 | *errp = err = netlink_dump_start(genl_sock, skb, nlh, |
Jamal Hadi Salim | a4d1366 | 2006-12-01 20:07:42 -0800 | [diff] [blame] | 341 | ops->dumpit, ops->done); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 342 | if (err == 0) |
| 343 | skb_pull(skb, min(NLMSG_ALIGN(nlh->nlmsg_len), |
| 344 | skb->len)); |
| 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | if (ops->doit == NULL) { |
| 349 | err = -EOPNOTSUPP; |
| 350 | goto errout; |
| 351 | } |
| 352 | |
| 353 | if (family->attrbuf) { |
| 354 | err = nlmsg_parse(nlh, hdrlen, family->attrbuf, family->maxattr, |
| 355 | ops->policy); |
| 356 | if (err < 0) |
| 357 | goto errout; |
| 358 | } |
| 359 | |
| 360 | info.snd_seq = nlh->nlmsg_seq; |
| 361 | info.snd_pid = NETLINK_CB(skb).pid; |
| 362 | info.nlhdr = nlh; |
| 363 | info.genlhdr = nlmsg_data(nlh); |
| 364 | info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN; |
| 365 | info.attrs = family->attrbuf; |
| 366 | |
| 367 | *errp = err = ops->doit(skb, &info); |
| 368 | return err; |
| 369 | |
| 370 | ignore: |
| 371 | return 0; |
| 372 | |
| 373 | errout: |
| 374 | *errp = err; |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | static void genl_rcv(struct sock *sk, int len) |
| 379 | { |
| 380 | unsigned int qlen = 0; |
| 381 | |
| 382 | do { |
| 383 | if (genl_trylock()) |
| 384 | return; |
Jamal Hadi Salim | e200bd8 | 2006-02-13 15:51:24 -0800 | [diff] [blame] | 385 | netlink_run_queue(sk, &qlen, genl_rcv_msg); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 386 | genl_unlock(); |
| 387 | } while (qlen && genl_sock && genl_sock->sk_receive_queue.qlen); |
| 388 | } |
| 389 | |
| 390 | /************************************************************************** |
| 391 | * Controller |
| 392 | **************************************************************************/ |
| 393 | |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 394 | static struct genl_family genl_ctrl = { |
| 395 | .id = GENL_ID_CTRL, |
| 396 | .name = "nlctrl", |
Jamal Hadi Salim | 334c29a | 2006-12-04 19:31:51 -0800 | [diff] [blame] | 397 | .version = 0x2, |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 398 | .maxattr = CTRL_ATTR_MAX, |
| 399 | }; |
| 400 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 401 | static int ctrl_fill_info(struct genl_family *family, u32 pid, u32 seq, |
| 402 | u32 flags, struct sk_buff *skb, u8 cmd) |
| 403 | { |
| 404 | void *hdr; |
| 405 | |
Thomas Graf | 17c157c | 2006-11-14 19:46:02 -0800 | [diff] [blame] | 406 | hdr = genlmsg_put(skb, pid, seq, &genl_ctrl, flags, cmd); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 407 | if (hdr == NULL) |
| 408 | return -1; |
| 409 | |
| 410 | NLA_PUT_STRING(skb, CTRL_ATTR_FAMILY_NAME, family->name); |
| 411 | NLA_PUT_U16(skb, CTRL_ATTR_FAMILY_ID, family->id); |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 412 | NLA_PUT_U32(skb, CTRL_ATTR_VERSION, family->version); |
| 413 | NLA_PUT_U32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize); |
| 414 | NLA_PUT_U32(skb, CTRL_ATTR_MAXATTR, family->maxattr); |
| 415 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 416 | if (!list_empty(&family->ops_list)) { |
| 417 | struct nlattr *nla_ops; |
| 418 | struct genl_ops *ops; |
| 419 | int idx = 1; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 420 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 421 | nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS); |
| 422 | if (nla_ops == NULL) |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 423 | goto nla_put_failure; |
| 424 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 425 | list_for_each_entry(ops, &family->ops_list, ops_list) { |
| 426 | struct nlattr *nest; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 427 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 428 | nest = nla_nest_start(skb, idx++); |
| 429 | if (nest == NULL) |
| 430 | goto nla_put_failure; |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 431 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 432 | NLA_PUT_U32(skb, CTRL_ATTR_OP_ID, ops->cmd); |
| 433 | NLA_PUT_U32(skb, CTRL_ATTR_OP_FLAGS, ops->flags); |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 434 | |
Thomas Graf | e94ef68 | 2006-11-23 11:44:37 -0800 | [diff] [blame] | 435 | nla_nest_end(skb, nest); |
| 436 | } |
| 437 | |
| 438 | nla_nest_end(skb, nla_ops); |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 441 | return genlmsg_end(skb, hdr); |
| 442 | |
| 443 | nla_put_failure: |
| 444 | return genlmsg_cancel(skb, hdr); |
| 445 | } |
| 446 | |
| 447 | static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb) |
| 448 | { |
| 449 | |
| 450 | int i, n = 0; |
| 451 | struct genl_family *rt; |
| 452 | int chains_to_skip = cb->args[0]; |
| 453 | int fams_to_skip = cb->args[1]; |
| 454 | |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 455 | if (chains_to_skip != 0) |
| 456 | genl_lock(); |
| 457 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 458 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) { |
| 459 | if (i < chains_to_skip) |
| 460 | continue; |
| 461 | n = 0; |
| 462 | list_for_each_entry(rt, genl_family_chain(i), family_list) { |
| 463 | if (++n < fams_to_skip) |
| 464 | continue; |
| 465 | if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).pid, |
| 466 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 467 | skb, CTRL_CMD_NEWFAMILY) < 0) |
| 468 | goto errout; |
| 469 | } |
| 470 | |
| 471 | fams_to_skip = 0; |
| 472 | } |
| 473 | |
| 474 | errout: |
Thomas Graf | eb32811 | 2006-09-18 00:01:59 -0700 | [diff] [blame] | 475 | if (chains_to_skip != 0) |
| 476 | genl_unlock(); |
| 477 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 478 | cb->args[0] = i; |
| 479 | cb->args[1] = n; |
| 480 | |
| 481 | return skb->len; |
| 482 | } |
| 483 | |
| 484 | static struct sk_buff *ctrl_build_msg(struct genl_family *family, u32 pid, |
Per Liden | b461d2f2 | 2006-01-03 14:13:29 -0800 | [diff] [blame] | 485 | int seq, u8 cmd) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 486 | { |
| 487 | struct sk_buff *skb; |
| 488 | int err; |
| 489 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 490 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 491 | if (skb == NULL) |
| 492 | return ERR_PTR(-ENOBUFS); |
| 493 | |
| 494 | err = ctrl_fill_info(family, pid, seq, 0, skb, cmd); |
| 495 | if (err < 0) { |
| 496 | nlmsg_free(skb); |
| 497 | return ERR_PTR(err); |
| 498 | } |
| 499 | |
| 500 | return skb; |
| 501 | } |
| 502 | |
| 503 | static struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] __read_mostly = { |
| 504 | [CTRL_ATTR_FAMILY_ID] = { .type = NLA_U16 }, |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 505 | [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING, |
| 506 | .len = GENL_NAMSIZ - 1 }, |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info) |
| 510 | { |
| 511 | struct sk_buff *msg; |
| 512 | struct genl_family *res = NULL; |
| 513 | int err = -EINVAL; |
| 514 | |
| 515 | if (info->attrs[CTRL_ATTR_FAMILY_ID]) { |
| 516 | u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]); |
| 517 | res = genl_family_find_byid(id); |
| 518 | } |
| 519 | |
| 520 | if (info->attrs[CTRL_ATTR_FAMILY_NAME]) { |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 521 | char *name; |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 522 | |
Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 523 | name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 524 | res = genl_family_find_byname(name); |
| 525 | } |
| 526 | |
| 527 | if (res == NULL) { |
| 528 | err = -ENOENT; |
| 529 | goto errout; |
| 530 | } |
| 531 | |
| 532 | msg = ctrl_build_msg(res, info->snd_pid, info->snd_seq, |
| 533 | CTRL_CMD_NEWFAMILY); |
| 534 | if (IS_ERR(msg)) { |
| 535 | err = PTR_ERR(msg); |
| 536 | goto errout; |
| 537 | } |
| 538 | |
Thomas Graf | 81878d2 | 2006-11-14 19:45:27 -0800 | [diff] [blame] | 539 | err = genlmsg_reply(msg, info); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 540 | errout: |
| 541 | return err; |
| 542 | } |
| 543 | |
| 544 | static int genl_ctrl_event(int event, void *data) |
| 545 | { |
| 546 | struct sk_buff *msg; |
| 547 | |
| 548 | if (genl_sock == NULL) |
| 549 | return 0; |
| 550 | |
| 551 | switch (event) { |
| 552 | case CTRL_CMD_NEWFAMILY: |
| 553 | case CTRL_CMD_DELFAMILY: |
| 554 | msg = ctrl_build_msg(data, 0, 0, event); |
| 555 | if (IS_ERR(msg)) |
| 556 | return PTR_ERR(msg); |
| 557 | |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 558 | genlmsg_multicast(msg, 0, GENL_ID_CTRL, GFP_KERNEL); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | static struct genl_ops genl_ctrl_ops = { |
| 566 | .cmd = CTRL_CMD_GETFAMILY, |
| 567 | .doit = ctrl_getfamily, |
| 568 | .dumpit = ctrl_dumpfamily, |
| 569 | .policy = ctrl_policy, |
| 570 | }; |
| 571 | |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 572 | static int __init genl_init(void) |
| 573 | { |
| 574 | int i, err; |
| 575 | |
| 576 | for (i = 0; i < GENL_FAM_TAB_SIZE; i++) |
| 577 | INIT_LIST_HEAD(&family_ht[i]); |
| 578 | |
| 579 | err = genl_register_family(&genl_ctrl); |
| 580 | if (err < 0) |
| 581 | goto errout; |
| 582 | |
| 583 | err = genl_register_ops(&genl_ctrl, &genl_ctrl_ops); |
| 584 | if (err < 0) |
| 585 | goto errout_register; |
| 586 | |
| 587 | netlink_set_nonroot(NETLINK_GENERIC, NL_NONROOT_RECV); |
| 588 | genl_sock = netlink_kernel_create(NETLINK_GENERIC, GENL_MAX_ID, |
| 589 | genl_rcv, THIS_MODULE); |
Jamal Hadi Salim | e200bd8 | 2006-02-13 15:51:24 -0800 | [diff] [blame] | 590 | if (genl_sock == NULL) |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 591 | panic("GENL: Cannot initialize generic netlink\n"); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 592 | |
| 593 | return 0; |
| 594 | |
| 595 | errout_register: |
| 596 | genl_unregister_family(&genl_ctrl); |
| 597 | errout: |
| 598 | panic("GENL: Cannot register controller: %d\n", err); |
Thomas Graf | 482a852 | 2005-11-10 02:25:56 +0100 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | subsys_initcall(genl_init); |
| 602 | |
| 603 | EXPORT_SYMBOL(genl_sock); |
| 604 | EXPORT_SYMBOL(genl_register_ops); |
| 605 | EXPORT_SYMBOL(genl_unregister_ops); |
| 606 | EXPORT_SYMBOL(genl_register_family); |
| 607 | EXPORT_SYMBOL(genl_unregister_family); |