netlink.c 4.3 KB

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