nl-phy.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. 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. for (i = 0; i < 32; i++) {
  53. if (phy->channels_supported[i])
  54. buf[pages++] = phy->channels_supported[i] | (i << 27);
  55. }
  56. if (pages)
  57. NLA_PUT(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
  58. pages * sizeof(uint32_t), buf);
  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. static int ieee802154_list_phy(struct sk_buff *skb,
  70. struct genl_info *info)
  71. {
  72. /* Request for interface name, index, type, IEEE address,
  73. PAN Id, short address */
  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_GOODSIZE, GFP_KERNEL);
  88. if (!msg)
  89. goto out_dev;
  90. rc = ieee802154_nl_fill_phy(msg, info->snd_pid, 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).pid,
  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. static int ieee802154_dump_phy(struct sk_buff *skb,
  126. 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. static int ieee802154_add_iface(struct sk_buff *skb,
  140. 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. 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. dev = phy->add_iface(phy, devname);
  181. if (IS_ERR(dev)) {
  182. rc = PTR_ERR(dev);
  183. goto nla_put_failure;
  184. }
  185. if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
  186. struct sockaddr addr;
  187. addr.sa_family = ARPHRD_IEEE802154;
  188. nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
  189. IEEE802154_ADDR_LEN);
  190. /*
  191. * strangely enough, some callbacks (inetdev_event) from
  192. * dev_set_mac_address require RTNL_LOCK
  193. */
  194. rtnl_lock();
  195. rc = dev_set_mac_address(dev, &addr);
  196. rtnl_unlock();
  197. if (rc)
  198. goto dev_unregister;
  199. }
  200. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  201. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  202. dev_put(dev);
  203. wpan_phy_put(phy);
  204. return ieee802154_nl_reply(msg, info);
  205. dev_unregister:
  206. rtnl_lock(); /* del_iface must be called with RTNL lock */
  207. phy->del_iface(phy, dev);
  208. dev_put(dev);
  209. rtnl_unlock();
  210. nla_put_failure:
  211. nlmsg_free(msg);
  212. out_dev:
  213. wpan_phy_put(phy);
  214. return rc;
  215. }
  216. static int ieee802154_del_iface(struct sk_buff *skb,
  217. struct genl_info *info)
  218. {
  219. struct sk_buff *msg;
  220. struct wpan_phy *phy;
  221. const char *name;
  222. int rc;
  223. struct net_device *dev;
  224. pr_debug("%s\n", __func__);
  225. if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
  226. return -EINVAL;
  227. name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  228. if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
  229. return -EINVAL; /* name should be null-terminated */
  230. dev = dev_get_by_name(genl_info_net(info), name);
  231. if (!dev)
  232. return -ENODEV;
  233. phy = ieee802154_mlme_ops(dev)->get_phy(dev);
  234. BUG_ON(!phy);
  235. rc = -EINVAL;
  236. /* phy name is optional, but should be checked if it's given */
  237. if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
  238. struct wpan_phy *phy2;
  239. const char *pname =
  240. nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  241. if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
  242. != '\0')
  243. /* name should be null-terminated */
  244. goto out_dev;
  245. phy2 = wpan_phy_find(pname);
  246. if (!phy2)
  247. goto out_dev;
  248. if (phy != phy2) {
  249. wpan_phy_put(phy2);
  250. goto out_dev;
  251. }
  252. }
  253. rc = -ENOBUFS;
  254. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
  255. if (!msg)
  256. goto out_dev;
  257. if (!phy->del_iface) {
  258. rc = -EINVAL;
  259. goto nla_put_failure;
  260. }
  261. rtnl_lock();
  262. phy->del_iface(phy, dev);
  263. /* We don't have device anymore */
  264. dev_put(dev);
  265. dev = NULL;
  266. rtnl_unlock();
  267. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  268. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, name);
  269. wpan_phy_put(phy);
  270. return ieee802154_nl_reply(msg, info);
  271. nla_put_failure:
  272. nlmsg_free(msg);
  273. out_dev:
  274. wpan_phy_put(phy);
  275. if (dev)
  276. dev_put(dev);
  277. return rc;
  278. }
  279. static struct genl_ops ieee802154_phy_ops[] = {
  280. IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
  281. ieee802154_dump_phy),
  282. IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
  283. IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
  284. };
  285. /*
  286. * No need to unregister as family unregistration will do it.
  287. */
  288. int nl802154_phy_register(void)
  289. {
  290. int i;
  291. int rc;
  292. for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
  293. rc = genl_register_ops(&nl802154_family,
  294. &ieee802154_phy_ops[i]);
  295. if (rc)
  296. return rc;
  297. }
  298. return 0;
  299. }