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