nl-phy.c 7.4 KB

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