nl-phy.c 8.0 KB

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