netlink.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * 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. struct genl_family nl802154_family = {
  28. .id = GENL_ID_GENERATE,
  29. .hdrsize = 0,
  30. .name = IEEE802154_NL_NAME,
  31. .version = 1,
  32. .maxattr = IEEE802154_ATTR_MAX,
  33. };
  34. /* Requests to userspace */
  35. struct sk_buff *ieee802154_nl_create(int flags, u8 req)
  36. {
  37. void *hdr;
  38. struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  39. unsigned long f;
  40. if (!msg)
  41. return NULL;
  42. spin_lock_irqsave(&ieee802154_seq_lock, f);
  43. hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
  44. &nl802154_family, flags, req);
  45. spin_unlock_irqrestore(&ieee802154_seq_lock, f);
  46. if (!hdr) {
  47. nlmsg_free(msg);
  48. return NULL;
  49. }
  50. return msg;
  51. }
  52. int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
  53. {
  54. struct nlmsghdr *nlh = nlmsg_hdr(msg);
  55. void *hdr = genlmsg_data(nlmsg_data(nlh));
  56. if (genlmsg_end(msg, hdr) < 0)
  57. goto out;
  58. return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
  59. out:
  60. nlmsg_free(msg);
  61. return -ENOBUFS;
  62. }
  63. struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
  64. int flags, u8 req)
  65. {
  66. void *hdr;
  67. struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  68. if (!msg)
  69. return NULL;
  70. hdr = genlmsg_put_reply(msg, info,
  71. &nl802154_family, flags, req);
  72. if (!hdr) {
  73. nlmsg_free(msg);
  74. return NULL;
  75. }
  76. return msg;
  77. }
  78. int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
  79. {
  80. struct nlmsghdr *nlh = nlmsg_hdr(msg);
  81. void *hdr = genlmsg_data(nlmsg_data(nlh));
  82. if (genlmsg_end(msg, hdr) < 0)
  83. goto out;
  84. return genlmsg_reply(msg, info);
  85. out:
  86. nlmsg_free(msg);
  87. return -ENOBUFS;
  88. }
  89. static const struct genl_ops ieee8021154_ops[] = {
  90. /* see nl-phy.c */
  91. IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
  92. ieee802154_dump_phy),
  93. IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
  94. IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
  95. /* see nl-mac.c */
  96. IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
  97. IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
  98. IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
  99. IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
  100. IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
  101. IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
  102. ieee802154_dump_iface),
  103. IEEE802154_OP(IEEE802154_SET_MACPARAMS, ieee802154_set_macparams),
  104. IEEE802154_OP(IEEE802154_LLSEC_GETPARAMS, ieee802154_llsec_getparams),
  105. IEEE802154_OP(IEEE802154_LLSEC_SETPARAMS, ieee802154_llsec_setparams),
  106. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_KEY, NULL,
  107. ieee802154_llsec_dump_keys),
  108. IEEE802154_OP(IEEE802154_LLSEC_ADD_KEY, ieee802154_llsec_add_key),
  109. IEEE802154_OP(IEEE802154_LLSEC_DEL_KEY, ieee802154_llsec_del_key),
  110. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEV, NULL,
  111. ieee802154_llsec_dump_devs),
  112. IEEE802154_OP(IEEE802154_LLSEC_ADD_DEV, ieee802154_llsec_add_dev),
  113. IEEE802154_OP(IEEE802154_LLSEC_DEL_DEV, ieee802154_llsec_del_dev),
  114. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEVKEY, NULL,
  115. ieee802154_llsec_dump_devkeys),
  116. IEEE802154_OP(IEEE802154_LLSEC_ADD_DEVKEY, ieee802154_llsec_add_devkey),
  117. IEEE802154_OP(IEEE802154_LLSEC_DEL_DEVKEY, ieee802154_llsec_del_devkey),
  118. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_SECLEVEL, NULL,
  119. ieee802154_llsec_dump_seclevels),
  120. IEEE802154_OP(IEEE802154_LLSEC_ADD_SECLEVEL,
  121. ieee802154_llsec_add_seclevel),
  122. IEEE802154_OP(IEEE802154_LLSEC_DEL_SECLEVEL,
  123. ieee802154_llsec_del_seclevel),
  124. };
  125. static const struct genl_multicast_group ieee802154_mcgrps[] = {
  126. [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, },
  127. [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, },
  128. };
  129. int __init ieee802154_nl_init(void)
  130. {
  131. return genl_register_family_with_ops_groups(&nl802154_family,
  132. ieee8021154_ops,
  133. ieee802154_mcgrps);
  134. }
  135. void __exit ieee802154_nl_exit(void)
  136. {
  137. genl_unregister_family(&nl802154_family);
  138. }