nl-phy.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Netlink interface 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 = kcalloc(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. rtnl_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->supported.channels[i])
  53. buf[pages++] = phy->supported.channels[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. rtnl_unlock();
  60. kfree(buf);
  61. genlmsg_end(msg, hdr);
  62. return 0;
  63. nla_put_failure:
  64. rtnl_unlock();
  65. genlmsg_cancel(msg, hdr);
  66. out:
  67. kfree(buf);
  68. return -EMSGSIZE;
  69. }
  70. int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
  71. {
  72. /* Request for interface name, index, type, IEEE address,
  73. * PAN Id, short address
  74. */
  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. unsigned char name_assign_type;
  149. pr_debug("%s\n", __func__);
  150. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  151. return -EINVAL;
  152. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  153. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  154. return -EINVAL; /* phy name should be null-terminated */
  155. if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
  156. devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  157. if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
  158. != '\0')
  159. return -EINVAL; /* phy name should be null-terminated */
  160. name_assign_type = NET_NAME_USER;
  161. } else {
  162. devname = "wpan%d";
  163. name_assign_type = NET_NAME_ENUM;
  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 (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
  174. nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
  175. IEEE802154_ADDR_LEN) {
  176. rc = -EINVAL;
  177. goto nla_put_failure;
  178. }
  179. if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
  180. type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
  181. if (type >= __IEEE802154_DEV_MAX) {
  182. rc = -EINVAL;
  183. goto nla_put_failure;
  184. }
  185. }
  186. dev = rdev_add_virtual_intf_deprecated(wpan_phy_to_rdev(phy), devname,
  187. name_assign_type, type);
  188. if (IS_ERR(dev)) {
  189. rc = PTR_ERR(dev);
  190. goto nla_put_failure;
  191. }
  192. dev_hold(dev);
  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. /* strangely enough, some callbacks (inetdev_event) from
  199. * dev_set_mac_address require RTNL_LOCK
  200. */
  201. rtnl_lock();
  202. rc = dev_set_mac_address(dev, &addr);
  203. rtnl_unlock();
  204. if (rc)
  205. goto dev_unregister;
  206. }
  207. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  208. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
  209. goto nla_put_failure;
  210. dev_put(dev);
  211. wpan_phy_put(phy);
  212. return ieee802154_nl_reply(msg, info);
  213. dev_unregister:
  214. rtnl_lock(); /* del_iface must be called with RTNL lock */
  215. rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
  216. dev_put(dev);
  217. rtnl_unlock();
  218. nla_put_failure:
  219. nlmsg_free(msg);
  220. out_dev:
  221. wpan_phy_put(phy);
  222. return rc;
  223. }
  224. int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
  225. {
  226. struct sk_buff *msg;
  227. struct wpan_phy *phy;
  228. const char *name;
  229. int rc;
  230. struct net_device *dev;
  231. pr_debug("%s\n", __func__);
  232. if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
  233. return -EINVAL;
  234. name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  235. if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
  236. return -EINVAL; /* name should be null-terminated */
  237. rc = -ENODEV;
  238. dev = dev_get_by_name(genl_info_net(info), name);
  239. if (!dev)
  240. return rc;
  241. if (dev->type != ARPHRD_IEEE802154)
  242. goto out;
  243. phy = dev->ieee802154_ptr->wpan_phy;
  244. BUG_ON(!phy);
  245. get_device(&phy->dev);
  246. rc = -EINVAL;
  247. /* phy name is optional, but should be checked if it's given */
  248. if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
  249. struct wpan_phy *phy2;
  250. const char *pname =
  251. nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  252. if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
  253. != '\0')
  254. /* name should be null-terminated */
  255. goto out_dev;
  256. phy2 = wpan_phy_find(pname);
  257. if (!phy2)
  258. goto out_dev;
  259. if (phy != phy2) {
  260. wpan_phy_put(phy2);
  261. goto out_dev;
  262. }
  263. }
  264. rc = -ENOBUFS;
  265. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
  266. if (!msg)
  267. goto out_dev;
  268. rtnl_lock();
  269. rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
  270. /* We don't have device anymore */
  271. dev_put(dev);
  272. dev = NULL;
  273. rtnl_unlock();
  274. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  275. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
  276. goto nla_put_failure;
  277. wpan_phy_put(phy);
  278. return ieee802154_nl_reply(msg, info);
  279. nla_put_failure:
  280. nlmsg_free(msg);
  281. out_dev:
  282. wpan_phy_put(phy);
  283. out:
  284. if (dev)
  285. dev_put(dev);
  286. return rc;
  287. }