nl-phy.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <net/af_ieee802154.h>
  29. #include <net/ieee802154_netdev.h>
  30. #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
  31. #include <linux/nl802154.h>
  32. #include "ieee802154.h"
  33. static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 pid,
  34. u32 seq, int flags, struct wpan_phy *phy)
  35. {
  36. void *hdr;
  37. int i, pages = 0;
  38. uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
  39. pr_debug("%s\n", __func__);
  40. if (!buf)
  41. goto out;
  42. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
  43. IEEE802154_LIST_PHY);
  44. if (!hdr)
  45. goto out;
  46. mutex_lock(&phy->pib_lock);
  47. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  48. NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, phy->current_page);
  49. NLA_PUT_U8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel);
  50. for (i = 0; i < 32; i++) {
  51. if (phy->channels_supported[i])
  52. buf[pages++] = phy->channels_supported[i] | (i << 27);
  53. }
  54. if (pages)
  55. NLA_PUT(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
  56. pages * sizeof(uint32_t), buf);
  57. mutex_unlock(&phy->pib_lock);
  58. return genlmsg_end(msg, hdr);
  59. nla_put_failure:
  60. mutex_unlock(&phy->pib_lock);
  61. genlmsg_cancel(msg, hdr);
  62. out:
  63. kfree(buf);
  64. return -EMSGSIZE;
  65. }
  66. static int ieee802154_list_phy(struct sk_buff *skb,
  67. struct genl_info *info)
  68. {
  69. /* Request for interface name, index, type, IEEE address,
  70. PAN Id, short address */
  71. struct sk_buff *msg;
  72. struct wpan_phy *phy;
  73. const char *name;
  74. int rc = -ENOBUFS;
  75. pr_debug("%s\n", __func__);
  76. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  77. return -EINVAL;
  78. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  79. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  80. return -EINVAL; /* phy name should be null-terminated */
  81. phy = wpan_phy_find(name);
  82. if (!phy)
  83. return -ENODEV;
  84. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  85. if (!msg)
  86. goto out_dev;
  87. rc = ieee802154_nl_fill_phy(msg, info->snd_pid, info->snd_seq,
  88. 0, phy);
  89. if (rc < 0)
  90. goto out_free;
  91. wpan_phy_put(phy);
  92. return genlmsg_reply(msg, info);
  93. out_free:
  94. nlmsg_free(msg);
  95. out_dev:
  96. wpan_phy_put(phy);
  97. return rc;
  98. }
  99. struct dump_phy_data {
  100. struct sk_buff *skb;
  101. struct netlink_callback *cb;
  102. int idx, s_idx;
  103. };
  104. static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
  105. {
  106. int rc;
  107. struct dump_phy_data *data = _data;
  108. pr_debug("%s\n", __func__);
  109. if (data->idx++ < data->s_idx)
  110. return 0;
  111. rc = ieee802154_nl_fill_phy(data->skb,
  112. NETLINK_CB(data->cb->skb).pid,
  113. data->cb->nlh->nlmsg_seq,
  114. NLM_F_MULTI,
  115. phy);
  116. if (rc < 0) {
  117. data->idx--;
  118. return rc;
  119. }
  120. return 0;
  121. }
  122. static int ieee802154_dump_phy(struct sk_buff *skb,
  123. struct netlink_callback *cb)
  124. {
  125. struct dump_phy_data data = {
  126. .cb = cb,
  127. .skb = skb,
  128. .s_idx = cb->args[0],
  129. .idx = 0,
  130. };
  131. pr_debug("%s\n", __func__);
  132. wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
  133. cb->args[0] = data.idx;
  134. return skb->len;
  135. }
  136. static int ieee802154_add_iface(struct sk_buff *skb,
  137. struct genl_info *info)
  138. {
  139. struct sk_buff *msg;
  140. struct wpan_phy *phy;
  141. const char *name;
  142. const char *devname;
  143. int rc = -ENOBUFS;
  144. struct net_device *dev;
  145. pr_debug("%s\n", __func__);
  146. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  147. return -EINVAL;
  148. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  149. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  150. return -EINVAL; /* phy name should be null-terminated */
  151. if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
  152. devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  153. if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
  154. != '\0')
  155. return -EINVAL; /* phy name should be null-terminated */
  156. } else {
  157. devname = "wpan%d";
  158. }
  159. if (strlen(devname) >= IFNAMSIZ)
  160. return -ENAMETOOLONG;
  161. phy = wpan_phy_find(name);
  162. if (!phy)
  163. return -ENODEV;
  164. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
  165. if (!msg)
  166. goto out_dev;
  167. if (!phy->add_iface) {
  168. rc = -EINVAL;
  169. goto nla_put_failure;
  170. }
  171. dev = phy->add_iface(phy, devname);
  172. if (IS_ERR(dev)) {
  173. rc = PTR_ERR(dev);
  174. goto nla_put_failure;
  175. }
  176. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  177. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  178. dev_put(dev);
  179. wpan_phy_put(phy);
  180. return ieee802154_nl_reply(msg, info);
  181. nla_put_failure:
  182. nlmsg_free(msg);
  183. out_dev:
  184. wpan_phy_put(phy);
  185. return rc;
  186. }
  187. static int ieee802154_del_iface(struct sk_buff *skb,
  188. struct genl_info *info)
  189. {
  190. struct sk_buff *msg;
  191. struct wpan_phy *phy;
  192. const char *name;
  193. int rc;
  194. struct net_device *dev;
  195. pr_debug("%s\n", __func__);
  196. if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
  197. return -EINVAL;
  198. name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  199. if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
  200. return -EINVAL; /* name should be null-terminated */
  201. dev = dev_get_by_name(genl_info_net(info), name);
  202. if (!dev)
  203. return -ENODEV;
  204. phy = ieee802154_mlme_ops(dev)->get_phy(dev);
  205. BUG_ON(!phy);
  206. rc = -EINVAL;
  207. /* phy name is optional, but should be checked if it's given */
  208. if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
  209. struct wpan_phy *phy2;
  210. const char *pname =
  211. nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  212. if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
  213. != '\0')
  214. /* name should be null-terminated */
  215. goto out_dev;
  216. phy2 = wpan_phy_find(pname);
  217. if (!phy2)
  218. goto out_dev;
  219. if (phy != phy2) {
  220. wpan_phy_put(phy2);
  221. goto out_dev;
  222. }
  223. }
  224. rc = -ENOBUFS;
  225. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
  226. if (!msg)
  227. goto out_dev;
  228. if (!phy->del_iface) {
  229. rc = -EINVAL;
  230. goto nla_put_failure;
  231. }
  232. rtnl_lock();
  233. phy->del_iface(phy, dev);
  234. /* We don't have device anymore */
  235. dev_put(dev);
  236. dev = NULL;
  237. rtnl_unlock();
  238. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  239. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, name);
  240. wpan_phy_put(phy);
  241. return ieee802154_nl_reply(msg, info);
  242. nla_put_failure:
  243. nlmsg_free(msg);
  244. out_dev:
  245. wpan_phy_put(phy);
  246. if (dev)
  247. dev_put(dev);
  248. return rc;
  249. }
  250. static struct genl_ops ieee802154_phy_ops[] = {
  251. IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
  252. ieee802154_dump_phy),
  253. IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
  254. IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
  255. };
  256. /*
  257. * No need to unregister as family unregistration will do it.
  258. */
  259. int nl802154_phy_register(void)
  260. {
  261. int i;
  262. int rc;
  263. for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
  264. rc = genl_register_ops(&nl802154_family,
  265. &ieee802154_phy_ops[i]);
  266. if (rc)
  267. return rc;
  268. }
  269. return 0;
  270. }