nl-phy.c 7.8 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. * Written by:
  16. * Sergey Lapin <slapin@ossfans.org>
  17. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  18. * Maxim Osipov <maxim.osipov@siemens.com>
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/if_arp.h>
  23. #include <net/netlink.h>
  24. #include <net/genetlink.h>
  25. #include <net/cfg802154.h>
  26. #include <net/af_ieee802154.h>
  27. #include <net/ieee802154_netdev.h>
  28. #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
  29. #include <linux/nl802154.h>
  30. #include "ieee802154.h"
  31. #include "rdev-ops.h"
  32. #include "core.h"
  33. static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
  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. return -EMSGSIZE;
  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. if (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. goto nla_put_failure;
  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. goto nla_put_failure;
  59. mutex_unlock(&phy->pib_lock);
  60. kfree(buf);
  61. return genlmsg_end(msg, hdr);
  62. nla_put_failure:
  63. mutex_unlock(&phy->pib_lock);
  64. genlmsg_cancel(msg, hdr);
  65. out:
  66. kfree(buf);
  67. return -EMSGSIZE;
  68. }
  69. int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
  70. {
  71. /* Request for interface name, index, type, IEEE address,
  72. * PAN Id, short address
  73. */
  74. struct sk_buff *msg;
  75. struct wpan_phy *phy;
  76. const char *name;
  77. int rc = -ENOBUFS;
  78. pr_debug("%s\n", __func__);
  79. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  80. return -EINVAL;
  81. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  82. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  83. return -EINVAL; /* phy name should be null-terminated */
  84. phy = wpan_phy_find(name);
  85. if (!phy)
  86. return -ENODEV;
  87. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  88. if (!msg)
  89. goto out_dev;
  90. rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
  91. 0, phy);
  92. if (rc < 0)
  93. goto out_free;
  94. wpan_phy_put(phy);
  95. return genlmsg_reply(msg, info);
  96. out_free:
  97. nlmsg_free(msg);
  98. out_dev:
  99. wpan_phy_put(phy);
  100. return rc;
  101. }
  102. struct dump_phy_data {
  103. struct sk_buff *skb;
  104. struct netlink_callback *cb;
  105. int idx, s_idx;
  106. };
  107. static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
  108. {
  109. int rc;
  110. struct dump_phy_data *data = _data;
  111. pr_debug("%s\n", __func__);
  112. if (data->idx++ < data->s_idx)
  113. return 0;
  114. rc = ieee802154_nl_fill_phy(data->skb,
  115. NETLINK_CB(data->cb->skb).portid,
  116. data->cb->nlh->nlmsg_seq,
  117. NLM_F_MULTI,
  118. phy);
  119. if (rc < 0) {
  120. data->idx--;
  121. return rc;
  122. }
  123. return 0;
  124. }
  125. int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
  126. {
  127. struct dump_phy_data data = {
  128. .cb = cb,
  129. .skb = skb,
  130. .s_idx = cb->args[0],
  131. .idx = 0,
  132. };
  133. pr_debug("%s\n", __func__);
  134. wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
  135. cb->args[0] = data.idx;
  136. return skb->len;
  137. }
  138. int ieee802154_add_iface(struct sk_buff *skb, 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. int type = __IEEE802154_DEV_INVALID;
  147. pr_debug("%s\n", __func__);
  148. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  149. return -EINVAL;
  150. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  151. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  152. return -EINVAL; /* phy name should be null-terminated */
  153. if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
  154. devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  155. if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
  156. != '\0')
  157. return -EINVAL; /* phy name should be null-terminated */
  158. } else {
  159. devname = "wpan%d";
  160. }
  161. if (strlen(devname) >= IFNAMSIZ)
  162. return -ENAMETOOLONG;
  163. phy = wpan_phy_find(name);
  164. if (!phy)
  165. return -ENODEV;
  166. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
  167. if (!msg)
  168. goto out_dev;
  169. if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
  170. nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
  171. IEEE802154_ADDR_LEN) {
  172. rc = -EINVAL;
  173. goto nla_put_failure;
  174. }
  175. if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
  176. type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
  177. if (type >= __IEEE802154_DEV_MAX) {
  178. rc = -EINVAL;
  179. goto nla_put_failure;
  180. }
  181. }
  182. dev = rdev_add_virtual_intf_deprecated(wpan_phy_to_rdev(phy), devname,
  183. type);
  184. if (IS_ERR(dev)) {
  185. rc = PTR_ERR(dev);
  186. goto nla_put_failure;
  187. }
  188. if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
  189. struct sockaddr addr;
  190. addr.sa_family = ARPHRD_IEEE802154;
  191. nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
  192. IEEE802154_ADDR_LEN);
  193. /* strangely enough, some callbacks (inetdev_event) from
  194. * dev_set_mac_address require RTNL_LOCK
  195. */
  196. rtnl_lock();
  197. rc = dev_set_mac_address(dev, &addr);
  198. rtnl_unlock();
  199. if (rc)
  200. goto dev_unregister;
  201. }
  202. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  203. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
  204. goto nla_put_failure;
  205. dev_put(dev);
  206. wpan_phy_put(phy);
  207. return ieee802154_nl_reply(msg, info);
  208. dev_unregister:
  209. rtnl_lock(); /* del_iface must be called with RTNL lock */
  210. rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
  211. dev_put(dev);
  212. rtnl_unlock();
  213. nla_put_failure:
  214. nlmsg_free(msg);
  215. out_dev:
  216. wpan_phy_put(phy);
  217. return rc;
  218. }
  219. int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
  220. {
  221. struct sk_buff *msg;
  222. struct wpan_phy *phy;
  223. const char *name;
  224. int rc;
  225. struct net_device *dev;
  226. pr_debug("%s\n", __func__);
  227. if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
  228. return -EINVAL;
  229. name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  230. if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
  231. return -EINVAL; /* name should be null-terminated */
  232. dev = dev_get_by_name(genl_info_net(info), name);
  233. if (!dev)
  234. return -ENODEV;
  235. phy = ieee802154_mlme_ops(dev)->get_phy(dev);
  236. BUG_ON(!phy);
  237. rc = -EINVAL;
  238. /* phy name is optional, but should be checked if it's given */
  239. if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
  240. struct wpan_phy *phy2;
  241. const char *pname =
  242. nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  243. if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
  244. != '\0')
  245. /* name should be null-terminated */
  246. goto out_dev;
  247. phy2 = wpan_phy_find(pname);
  248. if (!phy2)
  249. goto out_dev;
  250. if (phy != phy2) {
  251. wpan_phy_put(phy2);
  252. goto out_dev;
  253. }
  254. }
  255. rc = -ENOBUFS;
  256. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
  257. if (!msg)
  258. goto out_dev;
  259. rtnl_lock();
  260. rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
  261. /* We don't have device anymore */
  262. dev_put(dev);
  263. dev = NULL;
  264. rtnl_unlock();
  265. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  266. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
  267. goto nla_put_failure;
  268. wpan_phy_put(phy);
  269. return ieee802154_nl_reply(msg, info);
  270. nla_put_failure:
  271. nlmsg_free(msg);
  272. out_dev:
  273. wpan_phy_put(phy);
  274. if (dev)
  275. dev_put(dev);
  276. return rc;
  277. }