nl-phy.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/netlink.h>
  26. #include <net/genetlink.h>
  27. #include <net/wpan-phy.h>
  28. #include <linux/nl802154.h>
  29. #include "ieee802154.h"
  30. static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 pid,
  31. u32 seq, int flags, struct wpan_phy *phy)
  32. {
  33. void *hdr;
  34. int i, pages = 0;
  35. uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
  36. pr_debug("%s\n", __func__);
  37. if (!buf)
  38. goto out;
  39. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
  40. IEEE802154_LIST_PHY);
  41. if (!hdr)
  42. goto out;
  43. mutex_lock(&phy->pib_lock);
  44. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  45. NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, phy->current_page);
  46. NLA_PUT_U8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel);
  47. for (i = 0; i < 32; i++) {
  48. if (phy->channels_supported[i])
  49. buf[pages++] = phy->channels_supported[i] | (i << 27);
  50. }
  51. if (pages)
  52. NLA_PUT(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
  53. pages * sizeof(uint32_t), buf);
  54. mutex_unlock(&phy->pib_lock);
  55. return genlmsg_end(msg, hdr);
  56. nla_put_failure:
  57. mutex_unlock(&phy->pib_lock);
  58. genlmsg_cancel(msg, hdr);
  59. out:
  60. kfree(buf);
  61. return -EMSGSIZE;
  62. }
  63. static int ieee802154_list_phy(struct sk_buff *skb,
  64. struct genl_info *info)
  65. {
  66. /* Request for interface name, index, type, IEEE address,
  67. PAN Id, short address */
  68. struct sk_buff *msg;
  69. struct wpan_phy *phy;
  70. const char *name;
  71. int rc = -ENOBUFS;
  72. pr_debug("%s\n", __func__);
  73. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  74. return -EINVAL;
  75. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  76. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  77. return -EINVAL; /* phy name should be null-terminated */
  78. phy = wpan_phy_find(name);
  79. if (!phy)
  80. return -ENODEV;
  81. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  82. if (!msg)
  83. goto out_dev;
  84. rc = ieee802154_nl_fill_phy(msg, info->snd_pid, info->snd_seq,
  85. 0, phy);
  86. if (rc < 0)
  87. goto out_free;
  88. wpan_phy_put(phy);
  89. return genlmsg_reply(msg, info);
  90. out_free:
  91. nlmsg_free(msg);
  92. out_dev:
  93. wpan_phy_put(phy);
  94. return rc;
  95. }
  96. struct dump_phy_data {
  97. struct sk_buff *skb;
  98. struct netlink_callback *cb;
  99. int idx, s_idx;
  100. };
  101. static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
  102. {
  103. int rc;
  104. struct dump_phy_data *data = _data;
  105. pr_debug("%s\n", __func__);
  106. if (data->idx++ < data->s_idx)
  107. return 0;
  108. rc = ieee802154_nl_fill_phy(data->skb,
  109. NETLINK_CB(data->cb->skb).pid,
  110. data->cb->nlh->nlmsg_seq,
  111. NLM_F_MULTI,
  112. phy);
  113. if (rc < 0) {
  114. data->idx--;
  115. return rc;
  116. }
  117. return 0;
  118. }
  119. static int ieee802154_dump_phy(struct sk_buff *skb,
  120. struct netlink_callback *cb)
  121. {
  122. struct dump_phy_data data = {
  123. .cb = cb,
  124. .skb = skb,
  125. .s_idx = cb->args[0],
  126. .idx = 0,
  127. };
  128. pr_debug("%s\n", __func__);
  129. wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
  130. cb->args[0] = data.idx;
  131. return skb->len;
  132. }
  133. static struct genl_ops ieee802154_phy_ops[] = {
  134. IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
  135. ieee802154_dump_phy),
  136. };
  137. /*
  138. * No need to unregister as family unregistration will do it.
  139. */
  140. int nl802154_phy_register(void)
  141. {
  142. int i;
  143. int rc;
  144. for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
  145. rc = genl_register_ops(&nl802154_family,
  146. &ieee802154_phy_ops[i]);
  147. if (rc)
  148. return rc;
  149. }
  150. return 0;
  151. }