nl-phy.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. struct sk_buff *msg;
  76. struct wpan_phy *phy;
  77. const char *name;
  78. int rc = -ENOBUFS;
  79. pr_debug("%s\n", __func__);
  80. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  81. return -EINVAL;
  82. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  83. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  84. return -EINVAL; /* phy name should be null-terminated */
  85. phy = wpan_phy_find(name);
  86. if (!phy)
  87. return -ENODEV;
  88. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  89. if (!msg)
  90. goto out_dev;
  91. rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
  92. 0, phy);
  93. if (rc < 0)
  94. goto out_free;
  95. wpan_phy_put(phy);
  96. return genlmsg_reply(msg, info);
  97. out_free:
  98. nlmsg_free(msg);
  99. out_dev:
  100. wpan_phy_put(phy);
  101. return rc;
  102. }
  103. struct dump_phy_data {
  104. struct sk_buff *skb;
  105. struct netlink_callback *cb;
  106. int idx, s_idx;
  107. };
  108. static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
  109. {
  110. int rc;
  111. struct dump_phy_data *data = _data;
  112. pr_debug("%s\n", __func__);
  113. if (data->idx++ < data->s_idx)
  114. return 0;
  115. rc = ieee802154_nl_fill_phy(data->skb,
  116. NETLINK_CB(data->cb->skb).portid,
  117. data->cb->nlh->nlmsg_seq,
  118. NLM_F_MULTI,
  119. phy);
  120. if (rc < 0) {
  121. data->idx--;
  122. return rc;
  123. }
  124. return 0;
  125. }
  126. int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
  127. {
  128. struct dump_phy_data data = {
  129. .cb = cb,
  130. .skb = skb,
  131. .s_idx = cb->args[0],
  132. .idx = 0,
  133. };
  134. pr_debug("%s\n", __func__);
  135. wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
  136. cb->args[0] = data.idx;
  137. return skb->len;
  138. }
  139. int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
  140. {
  141. struct sk_buff *msg;
  142. struct wpan_phy *phy;
  143. const char *name;
  144. const char *devname;
  145. int rc = -ENOBUFS;
  146. struct net_device *dev;
  147. int type = __IEEE802154_DEV_INVALID;
  148. pr_debug("%s\n", __func__);
  149. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  150. return -EINVAL;
  151. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  152. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  153. return -EINVAL; /* phy name should be null-terminated */
  154. if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
  155. devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  156. if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
  157. != '\0')
  158. return -EINVAL; /* phy name should be null-terminated */
  159. } else {
  160. devname = "wpan%d";
  161. }
  162. if (strlen(devname) >= IFNAMSIZ)
  163. return -ENAMETOOLONG;
  164. phy = wpan_phy_find(name);
  165. if (!phy)
  166. return -ENODEV;
  167. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
  168. if (!msg)
  169. goto out_dev;
  170. if (!phy->add_iface) {
  171. rc = -EINVAL;
  172. goto nla_put_failure;
  173. }
  174. if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
  175. nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
  176. IEEE802154_ADDR_LEN) {
  177. rc = -EINVAL;
  178. goto nla_put_failure;
  179. }
  180. if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
  181. type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
  182. if (type >= __IEEE802154_DEV_MAX)
  183. return -EINVAL;
  184. }
  185. dev = phy->add_iface(phy, devname, type);
  186. if (IS_ERR(dev)) {
  187. rc = PTR_ERR(dev);
  188. goto nla_put_failure;
  189. }
  190. if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
  191. struct sockaddr addr;
  192. addr.sa_family = ARPHRD_IEEE802154;
  193. nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
  194. IEEE802154_ADDR_LEN);
  195. /*
  196. * strangely enough, some callbacks (inetdev_event) from
  197. * dev_set_mac_address require RTNL_LOCK
  198. */
  199. rtnl_lock();
  200. rc = dev_set_mac_address(dev, &addr);
  201. rtnl_unlock();
  202. if (rc)
  203. goto dev_unregister;
  204. }
  205. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  206. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
  207. goto nla_put_failure;
  208. dev_put(dev);
  209. wpan_phy_put(phy);
  210. return ieee802154_nl_reply(msg, info);
  211. dev_unregister:
  212. rtnl_lock(); /* del_iface must be called with RTNL lock */
  213. phy->del_iface(phy, dev);
  214. dev_put(dev);
  215. rtnl_unlock();
  216. nla_put_failure:
  217. nlmsg_free(msg);
  218. out_dev:
  219. wpan_phy_put(phy);
  220. return rc;
  221. }
  222. int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
  223. {
  224. struct sk_buff *msg;
  225. struct wpan_phy *phy;
  226. const char *name;
  227. int rc;
  228. struct net_device *dev;
  229. pr_debug("%s\n", __func__);
  230. if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
  231. return -EINVAL;
  232. name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  233. if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
  234. return -EINVAL; /* name should be null-terminated */
  235. dev = dev_get_by_name(genl_info_net(info), name);
  236. if (!dev)
  237. return -ENODEV;
  238. phy = ieee802154_mlme_ops(dev)->get_phy(dev);
  239. BUG_ON(!phy);
  240. rc = -EINVAL;
  241. /* phy name is optional, but should be checked if it's given */
  242. if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
  243. struct wpan_phy *phy2;
  244. const char *pname =
  245. nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  246. if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
  247. != '\0')
  248. /* name should be null-terminated */
  249. goto out_dev;
  250. phy2 = wpan_phy_find(pname);
  251. if (!phy2)
  252. goto out_dev;
  253. if (phy != phy2) {
  254. wpan_phy_put(phy2);
  255. goto out_dev;
  256. }
  257. }
  258. rc = -ENOBUFS;
  259. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
  260. if (!msg)
  261. goto out_dev;
  262. if (!phy->del_iface) {
  263. rc = -EINVAL;
  264. goto nla_put_failure;
  265. }
  266. rtnl_lock();
  267. phy->del_iface(phy, dev);
  268. /* We don't have device anymore */
  269. dev_put(dev);
  270. dev = NULL;
  271. rtnl_unlock();
  272. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  273. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
  274. goto nla_put_failure;
  275. wpan_phy_put(phy);
  276. return ieee802154_nl_reply(msg, info);
  277. nla_put_failure:
  278. nlmsg_free(msg);
  279. out_dev:
  280. wpan_phy_put(phy);
  281. if (dev)
  282. dev_put(dev);
  283. return rc;
  284. }