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