netlink.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. 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. genlmsg_end(msg, hdr);
  57. return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
  58. }
  59. struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
  60. int flags, u8 req)
  61. {
  62. void *hdr;
  63. struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  64. if (!msg)
  65. return NULL;
  66. hdr = genlmsg_put_reply(msg, info,
  67. &nl802154_family, flags, req);
  68. if (!hdr) {
  69. nlmsg_free(msg);
  70. return NULL;
  71. }
  72. return msg;
  73. }
  74. int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
  75. {
  76. struct nlmsghdr *nlh = nlmsg_hdr(msg);
  77. void *hdr = genlmsg_data(nlmsg_data(nlh));
  78. genlmsg_end(msg, hdr);
  79. return genlmsg_reply(msg, info);
  80. }
  81. static const struct genl_ops ieee8021154_ops[] = {
  82. /* see nl-phy.c */
  83. IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
  84. ieee802154_dump_phy),
  85. IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
  86. IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
  87. /* see nl-mac.c */
  88. IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
  89. IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
  90. IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
  91. IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
  92. IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
  93. IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
  94. ieee802154_dump_iface),
  95. IEEE802154_OP(IEEE802154_SET_MACPARAMS, ieee802154_set_macparams),
  96. IEEE802154_OP(IEEE802154_LLSEC_GETPARAMS, ieee802154_llsec_getparams),
  97. IEEE802154_OP(IEEE802154_LLSEC_SETPARAMS, ieee802154_llsec_setparams),
  98. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_KEY, NULL,
  99. ieee802154_llsec_dump_keys),
  100. IEEE802154_OP(IEEE802154_LLSEC_ADD_KEY, ieee802154_llsec_add_key),
  101. IEEE802154_OP(IEEE802154_LLSEC_DEL_KEY, ieee802154_llsec_del_key),
  102. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEV, NULL,
  103. ieee802154_llsec_dump_devs),
  104. IEEE802154_OP(IEEE802154_LLSEC_ADD_DEV, ieee802154_llsec_add_dev),
  105. IEEE802154_OP(IEEE802154_LLSEC_DEL_DEV, ieee802154_llsec_del_dev),
  106. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEVKEY, NULL,
  107. ieee802154_llsec_dump_devkeys),
  108. IEEE802154_OP(IEEE802154_LLSEC_ADD_DEVKEY, ieee802154_llsec_add_devkey),
  109. IEEE802154_OP(IEEE802154_LLSEC_DEL_DEVKEY, ieee802154_llsec_del_devkey),
  110. IEEE802154_DUMP(IEEE802154_LLSEC_LIST_SECLEVEL, NULL,
  111. ieee802154_llsec_dump_seclevels),
  112. IEEE802154_OP(IEEE802154_LLSEC_ADD_SECLEVEL,
  113. ieee802154_llsec_add_seclevel),
  114. IEEE802154_OP(IEEE802154_LLSEC_DEL_SECLEVEL,
  115. ieee802154_llsec_del_seclevel),
  116. };
  117. static const struct genl_multicast_group ieee802154_mcgrps[] = {
  118. [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, },
  119. [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, },
  120. };
  121. int __init ieee802154_nl_init(void)
  122. {
  123. return genl_register_family_with_ops_groups(&nl802154_family,
  124. ieee8021154_ops,
  125. ieee802154_mcgrps);
  126. }
  127. void ieee802154_nl_exit(void)
  128. {
  129. genl_unregister_family(&nl802154_family);
  130. }