nl-phy.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 pid,
  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. static int ieee802154_list_phy(struct sk_buff *skb,
  72. struct genl_info *info)
  73. {
  74. /* Request for interface name, index, type, IEEE address,
  75. PAN Id, short address */
  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_GOODSIZE, GFP_KERNEL);
  90. if (!msg)
  91. goto out_dev;
  92. rc = ieee802154_nl_fill_phy(msg, info->snd_pid, 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).pid,
  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. static int ieee802154_dump_phy(struct sk_buff *skb,
  128. struct netlink_callback *cb)
  129. {
  130. struct dump_phy_data data = {
  131. .cb = cb,
  132. .skb = skb,
  133. .s_idx = cb->args[0],
  134. .idx = 0,
  135. };
  136. pr_debug("%s\n", __func__);
  137. wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
  138. cb->args[0] = data.idx;
  139. return skb->len;
  140. }
  141. static int ieee802154_add_iface(struct sk_buff *skb,
  142. struct genl_info *info)
  143. {
  144. struct sk_buff *msg;
  145. struct wpan_phy *phy;
  146. const char *name;
  147. const char *devname;
  148. int rc = -ENOBUFS;
  149. struct net_device *dev;
  150. pr_debug("%s\n", __func__);
  151. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  152. return -EINVAL;
  153. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  154. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  155. return -EINVAL; /* phy name should be null-terminated */
  156. if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
  157. devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  158. if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
  159. != '\0')
  160. return -EINVAL; /* phy name should be null-terminated */
  161. } else {
  162. devname = "wpan%d";
  163. }
  164. if (strlen(devname) >= IFNAMSIZ)
  165. return -ENAMETOOLONG;
  166. phy = wpan_phy_find(name);
  167. if (!phy)
  168. return -ENODEV;
  169. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
  170. if (!msg)
  171. goto out_dev;
  172. if (!phy->add_iface) {
  173. rc = -EINVAL;
  174. goto nla_put_failure;
  175. }
  176. if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
  177. nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
  178. IEEE802154_ADDR_LEN) {
  179. rc = -EINVAL;
  180. goto nla_put_failure;
  181. }
  182. dev = phy->add_iface(phy, devname);
  183. if (IS_ERR(dev)) {
  184. rc = PTR_ERR(dev);
  185. goto nla_put_failure;
  186. }
  187. if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
  188. struct sockaddr addr;
  189. addr.sa_family = ARPHRD_IEEE802154;
  190. nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
  191. IEEE802154_ADDR_LEN);
  192. /*
  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. phy->del_iface(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. static int ieee802154_del_iface(struct sk_buff *skb,
  220. struct genl_info *info)
  221. {
  222. struct sk_buff *msg;
  223. struct wpan_phy *phy;
  224. const char *name;
  225. int rc;
  226. struct net_device *dev;
  227. pr_debug("%s\n", __func__);
  228. if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
  229. return -EINVAL;
  230. name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  231. if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
  232. return -EINVAL; /* name should be null-terminated */
  233. dev = dev_get_by_name(genl_info_net(info), name);
  234. if (!dev)
  235. return -ENODEV;
  236. phy = ieee802154_mlme_ops(dev)->get_phy(dev);
  237. BUG_ON(!phy);
  238. rc = -EINVAL;
  239. /* phy name is optional, but should be checked if it's given */
  240. if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
  241. struct wpan_phy *phy2;
  242. const char *pname =
  243. nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  244. if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
  245. != '\0')
  246. /* name should be null-terminated */
  247. goto out_dev;
  248. phy2 = wpan_phy_find(pname);
  249. if (!phy2)
  250. goto out_dev;
  251. if (phy != phy2) {
  252. wpan_phy_put(phy2);
  253. goto out_dev;
  254. }
  255. }
  256. rc = -ENOBUFS;
  257. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
  258. if (!msg)
  259. goto out_dev;
  260. if (!phy->del_iface) {
  261. rc = -EINVAL;
  262. goto nla_put_failure;
  263. }
  264. rtnl_lock();
  265. phy->del_iface(phy, dev);
  266. /* We don't have device anymore */
  267. dev_put(dev);
  268. dev = NULL;
  269. rtnl_unlock();
  270. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  271. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
  272. goto nla_put_failure;
  273. wpan_phy_put(phy);
  274. return ieee802154_nl_reply(msg, info);
  275. nla_put_failure:
  276. nlmsg_free(msg);
  277. out_dev:
  278. wpan_phy_put(phy);
  279. if (dev)
  280. dev_put(dev);
  281. return rc;
  282. }
  283. static struct genl_ops ieee802154_phy_ops[] = {
  284. IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
  285. ieee802154_dump_phy),
  286. IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
  287. IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
  288. };
  289. /*
  290. * No need to unregister as family unregistration will do it.
  291. */
  292. int nl802154_phy_register(void)
  293. {
  294. int i;
  295. int rc;
  296. for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
  297. rc = genl_register_ops(&nl802154_family,
  298. &ieee802154_phy_ops[i]);
  299. if (rc)
  300. return rc;
  301. }
  302. return 0;
  303. }