netlink.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Netlink inteface for IEEE 802.15.4 stack
  3. *
  4. * Copyright 2007, 2008 Siemens AG
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Written by:
  20. * Sergey Lapin <slapin@ossfans.org>
  21. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  22. * Maxim Osipov <maxim.osipov@siemens.com>
  23. */
  24. #include <linux/kernel.h>
  25. #include <net/genetlink.h>
  26. #include <linux/nl802154.h>
  27. #include "ieee802154.h"
  28. static unsigned int ieee802154_seq_num;
  29. static DEFINE_SPINLOCK(ieee802154_seq_lock);
  30. struct genl_family nl802154_family = {
  31. .id = GENL_ID_GENERATE,
  32. .hdrsize = 0,
  33. .name = IEEE802154_NL_NAME,
  34. .version = 1,
  35. .maxattr = IEEE802154_ATTR_MAX,
  36. };
  37. /* Requests to userspace */
  38. struct sk_buff *ieee802154_nl_create(int flags, u8 req)
  39. {
  40. void *hdr;
  41. struct sk_buff *msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  42. unsigned long f;
  43. if (!msg)
  44. return NULL;
  45. spin_lock_irqsave(&ieee802154_seq_lock, f);
  46. hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
  47. &nl802154_family, flags, req);
  48. spin_unlock_irqrestore(&ieee802154_seq_lock, f);
  49. if (!hdr) {
  50. nlmsg_free(msg);
  51. return NULL;
  52. }
  53. return msg;
  54. }
  55. int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
  56. {
  57. /* XXX: nlh is right at the start of msg */
  58. void *hdr = genlmsg_data(NLMSG_DATA(msg->data));
  59. if (genlmsg_end(msg, hdr) < 0)
  60. goto out;
  61. return genlmsg_multicast(msg, 0, group, GFP_ATOMIC);
  62. out:
  63. nlmsg_free(msg);
  64. return -ENOBUFS;
  65. }
  66. struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
  67. int flags, u8 req)
  68. {
  69. void *hdr;
  70. struct sk_buff *msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
  71. if (!msg)
  72. return NULL;
  73. hdr = genlmsg_put_reply(msg, info,
  74. &nl802154_family, flags, req);
  75. if (!hdr) {
  76. nlmsg_free(msg);
  77. return NULL;
  78. }
  79. return msg;
  80. }
  81. int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
  82. {
  83. /* XXX: nlh is right at the start of msg */
  84. void *hdr = genlmsg_data(NLMSG_DATA(msg->data));
  85. if (genlmsg_end(msg, hdr) < 0)
  86. goto out;
  87. return genlmsg_reply(msg, info);
  88. out:
  89. nlmsg_free(msg);
  90. return -ENOBUFS;
  91. }
  92. int __init ieee802154_nl_init(void)
  93. {
  94. int rc;
  95. rc = genl_register_family(&nl802154_family);
  96. if (rc)
  97. goto fail;
  98. rc = nl802154_mac_register();
  99. if (rc)
  100. goto fail;
  101. rc = nl802154_phy_register();
  102. if (rc)
  103. goto fail;
  104. return 0;
  105. fail:
  106. genl_unregister_family(&nl802154_family);
  107. return rc;
  108. }
  109. void __exit ieee802154_nl_exit(void)
  110. {
  111. genl_unregister_family(&nl802154_family);
  112. }