nl-phy.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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_DEFAULT_SIZE, 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. int type = __IEEE802154_DEV_INVALID;
  151. pr_debug("%s\n", __func__);
  152. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  153. return -EINVAL;
  154. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  155. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  156. return -EINVAL; /* phy name should be null-terminated */
  157. if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
  158. devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  159. if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
  160. != '\0')
  161. return -EINVAL; /* phy name should be null-terminated */
  162. } else {
  163. devname = "wpan%d";
  164. }
  165. if (strlen(devname) >= IFNAMSIZ)
  166. return -ENAMETOOLONG;
  167. phy = wpan_phy_find(name);
  168. if (!phy)
  169. return -ENODEV;
  170. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
  171. if (!msg)
  172. goto out_dev;
  173. if (!phy->add_iface) {
  174. rc = -EINVAL;
  175. goto nla_put_failure;
  176. }
  177. if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
  178. nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
  179. IEEE802154_ADDR_LEN) {
  180. rc = -EINVAL;
  181. goto nla_put_failure;
  182. }
  183. if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
  184. type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
  185. if (type >= __IEEE802154_DEV_MAX)
  186. return -EINVAL;
  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. /*
  199. * strangely enough, some callbacks (inetdev_event) from
  200. * dev_set_mac_address require RTNL_LOCK
  201. */
  202. rtnl_lock();
  203. rc = dev_set_mac_address(dev, &addr);
  204. rtnl_unlock();
  205. if (rc)
  206. goto dev_unregister;
  207. }
  208. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  209. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
  210. goto nla_put_failure;
  211. dev_put(dev);
  212. wpan_phy_put(phy);
  213. return ieee802154_nl_reply(msg, info);
  214. dev_unregister:
  215. rtnl_lock(); /* del_iface must be called with RTNL lock */
  216. phy->del_iface(phy, dev);
  217. dev_put(dev);
  218. rtnl_unlock();
  219. nla_put_failure:
  220. nlmsg_free(msg);
  221. out_dev:
  222. wpan_phy_put(phy);
  223. return rc;
  224. }
  225. static int ieee802154_del_iface(struct sk_buff *skb,
  226. struct genl_info *info)
  227. {
  228. struct sk_buff *msg;
  229. struct wpan_phy *phy;
  230. const char *name;
  231. int rc;
  232. struct net_device *dev;
  233. pr_debug("%s\n", __func__);
  234. if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
  235. return -EINVAL;
  236. name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  237. if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
  238. return -EINVAL; /* name should be null-terminated */
  239. dev = dev_get_by_name(genl_info_net(info), name);
  240. if (!dev)
  241. return -ENODEV;
  242. phy = ieee802154_mlme_ops(dev)->get_phy(dev);
  243. BUG_ON(!phy);
  244. rc = -EINVAL;
  245. /* phy name is optional, but should be checked if it's given */
  246. if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
  247. struct wpan_phy *phy2;
  248. const char *pname =
  249. nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  250. if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
  251. != '\0')
  252. /* name should be null-terminated */
  253. goto out_dev;
  254. phy2 = wpan_phy_find(pname);
  255. if (!phy2)
  256. goto out_dev;
  257. if (phy != phy2) {
  258. wpan_phy_put(phy2);
  259. goto out_dev;
  260. }
  261. }
  262. rc = -ENOBUFS;
  263. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
  264. if (!msg)
  265. goto out_dev;
  266. if (!phy->del_iface) {
  267. rc = -EINVAL;
  268. goto nla_put_failure;
  269. }
  270. rtnl_lock();
  271. phy->del_iface(phy, dev);
  272. /* We don't have device anymore */
  273. dev_put(dev);
  274. dev = NULL;
  275. rtnl_unlock();
  276. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  277. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
  278. goto nla_put_failure;
  279. wpan_phy_put(phy);
  280. return ieee802154_nl_reply(msg, info);
  281. nla_put_failure:
  282. nlmsg_free(msg);
  283. out_dev:
  284. wpan_phy_put(phy);
  285. if (dev)
  286. dev_put(dev);
  287. return rc;
  288. }
  289. static struct genl_ops ieee802154_phy_ops[] = {
  290. IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
  291. ieee802154_dump_phy),
  292. IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
  293. IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
  294. };
  295. /*
  296. * No need to unregister as family unregistration will do it.
  297. */
  298. int nl802154_phy_register(void)
  299. {
  300. int i;
  301. int rc;
  302. for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
  303. rc = genl_register_ops(&nl802154_family,
  304. &ieee802154_phy_ops[i]);
  305. if (rc)
  306. return rc;
  307. }
  308. return 0;
  309. }