netlink.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Netlink interface 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. * Written by:
  16. * Sergey Lapin <slapin@ossfans.org>
  17. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  18. * Maxim Osipov <maxim.osipov@siemens.com>
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/gfp.h>
  22. #include <net/genetlink.h>
  23. #include <linux/nl802154.h>
  24. #include "ieee802154.h"
  25. static unsigned int ieee802154_seq_num;
  26. static DEFINE_SPINLOCK(ieee802154_seq_lock);
  27. /* Requests to userspace */
  28. struct sk_buff *ieee802154_nl_create(int flags, u8 req)
  29. {
  30. void *hdr;
  31. struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  32. unsigned long f;
  33. if (!msg)
  34. return NULL;
  35. spin_lock_irqsave(&ieee802154_seq_lock, f);
  36. hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
  37. &nl802154_family, flags, req);
  38. spin_unlock_irqrestore(&ieee802154_seq_lock, f);
  39. if (!hdr) {
  40. nlmsg_free(msg);
  41. return NULL;
  42. }
  43. return msg;
  44. }
  45. int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
  46. {
  47. struct nlmsghdr *nlh = nlmsg_hdr(msg);
  48. void *hdr = genlmsg_data(nlmsg_data(nlh));
  49. genlmsg_end(msg, hdr);
  50. return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
  51. }
  52. struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
  53. int flags, u8 req)
  54. {
  55. void *hdr;
  56. struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  57. if (!msg)
  58. return NULL;
  59. hdr = genlmsg_put_reply(msg, info,
  60. &nl802154_family, flags, req);
  61. if (!hdr) {
  62. nlmsg_free(msg);
  63. return NULL;
  64. }
  65. return msg;
  66. }
  67. int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
  68. {
  69. struct nlmsghdr *nlh = nlmsg_hdr(msg);
  70. void *hdr = genlmsg_data(nlmsg_data(nlh));
  71. genlmsg_end(msg, hdr);
  72. return genlmsg_reply(msg, info);
  73. }
  74. static const struct genl_ops ieee802154_ops[] = {
  75. /* see nl-phy.c */
  76. IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
  77. ieee802154_dump_phy),
  78. IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
  79. IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
  80. /* see nl-mac.c */
  81. IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
  82. IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
  83. IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
  84. IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
  85. IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
  86. IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
  87. ieee802154_dump_iface),
  88. IEEE802154_OP(IEEE802154_SET_MACPARAMS, ieee802154_set_macparams),
  89. IEEE802154_OP(IEEE802154_LLSEC_GETPARAMS, ieee802154_llsec_getparams),
  90. IEEE802154_OP(IEEE802154_LLSEC_SETPARAMS, ieee802154_llsec_setparams),
  91. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_KEY, NULL,
  92. ieee802154_llsec_dump_keys),
  93. IEEE802154_OP(IEEE802154_LLSEC_ADD_KEY, ieee802154_llsec_add_key),
  94. IEEE802154_OP(IEEE802154_LLSEC_DEL_KEY, ieee802154_llsec_del_key),
  95. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEV, NULL,
  96. ieee802154_llsec_dump_devs),
  97. IEEE802154_OP(IEEE802154_LLSEC_ADD_DEV, ieee802154_llsec_add_dev),
  98. IEEE802154_OP(IEEE802154_LLSEC_DEL_DEV, ieee802154_llsec_del_dev),
  99. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEVKEY, NULL,
  100. ieee802154_llsec_dump_devkeys),
  101. IEEE802154_OP(IEEE802154_LLSEC_ADD_DEVKEY, ieee802154_llsec_add_devkey),
  102. IEEE802154_OP(IEEE802154_LLSEC_DEL_DEVKEY, ieee802154_llsec_del_devkey),
  103. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_SECLEVEL, NULL,
  104. ieee802154_llsec_dump_seclevels),
  105. IEEE802154_OP(IEEE802154_LLSEC_ADD_SECLEVEL,
  106. ieee802154_llsec_add_seclevel),
  107. IEEE802154_OP(IEEE802154_LLSEC_DEL_SECLEVEL,
  108. ieee802154_llsec_del_seclevel),
  109. };
  110. static const struct genl_multicast_group ieee802154_mcgrps[] = {
  111. [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, },
  112. [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, },
  113. };
  114. struct genl_family nl802154_family __ro_after_init = {
  115. .hdrsize = 0,
  116. .name = IEEE802154_NL_NAME,
  117. .version = 1,
  118. .maxattr = IEEE802154_ATTR_MAX,
  119. .module = THIS_MODULE,
  120. .ops = ieee802154_ops,
  121. .n_ops = ARRAY_SIZE(ieee802154_ops),
  122. .mcgrps = ieee802154_mcgrps,
  123. .n_mcgrps = ARRAY_SIZE(ieee802154_mcgrps),
  124. };
  125. int __init ieee802154_nl_init(void)
  126. {
  127. return genl_register_family(&nl802154_family);
  128. }
  129. void ieee802154_nl_exit(void)
  130. {
  131. genl_unregister_family(&nl802154_family);
  132. }