nl-phy.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 <net/netlink.h>
  27. #include <net/genetlink.h>
  28. #include <net/wpan-phy.h>
  29. #include <net/af_ieee802154.h>
  30. #include <net/ieee802154_netdev.h>
  31. #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
  32. #include <linux/nl802154.h>
  33. #include "ieee802154.h"
  34. static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 pid,
  35. u32 seq, int flags, struct wpan_phy *phy)
  36. {
  37. void *hdr;
  38. int i, pages = 0;
  39. uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL);
  40. pr_debug("%s\n", __func__);
  41. if (!buf)
  42. return -EMSGSIZE;
  43. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
  44. IEEE802154_LIST_PHY);
  45. if (!hdr)
  46. goto out;
  47. mutex_lock(&phy->pib_lock);
  48. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  49. NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, phy->current_page);
  50. NLA_PUT_U8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel);
  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. mutex_unlock(&phy->pib_lock);
  59. kfree(buf);
  60. return genlmsg_end(msg, hdr);
  61. nla_put_failure:
  62. mutex_unlock(&phy->pib_lock);
  63. genlmsg_cancel(msg, hdr);
  64. out:
  65. kfree(buf);
  66. return -EMSGSIZE;
  67. }
  68. static int ieee802154_list_phy(struct sk_buff *skb,
  69. struct genl_info *info)
  70. {
  71. /* Request for interface name, index, type, IEEE address,
  72. PAN Id, short address */
  73. struct sk_buff *msg;
  74. struct wpan_phy *phy;
  75. const char *name;
  76. int rc = -ENOBUFS;
  77. pr_debug("%s\n", __func__);
  78. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  79. return -EINVAL;
  80. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  81. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  82. return -EINVAL; /* phy name should be null-terminated */
  83. phy = wpan_phy_find(name);
  84. if (!phy)
  85. return -ENODEV;
  86. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  87. if (!msg)
  88. goto out_dev;
  89. rc = ieee802154_nl_fill_phy(msg, info->snd_pid, info->snd_seq,
  90. 0, phy);
  91. if (rc < 0)
  92. goto out_free;
  93. wpan_phy_put(phy);
  94. return genlmsg_reply(msg, info);
  95. out_free:
  96. nlmsg_free(msg);
  97. out_dev:
  98. wpan_phy_put(phy);
  99. return rc;
  100. }
  101. struct dump_phy_data {
  102. struct sk_buff *skb;
  103. struct netlink_callback *cb;
  104. int idx, s_idx;
  105. };
  106. static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
  107. {
  108. int rc;
  109. struct dump_phy_data *data = _data;
  110. pr_debug("%s\n", __func__);
  111. if (data->idx++ < data->s_idx)
  112. return 0;
  113. rc = ieee802154_nl_fill_phy(data->skb,
  114. NETLINK_CB(data->cb->skb).pid,
  115. data->cb->nlh->nlmsg_seq,
  116. NLM_F_MULTI,
  117. phy);
  118. if (rc < 0) {
  119. data->idx--;
  120. return rc;
  121. }
  122. return 0;
  123. }
  124. static int ieee802154_dump_phy(struct sk_buff *skb,
  125. 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. static int ieee802154_add_iface(struct sk_buff *skb,
  139. 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. 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 (!phy->add_iface) {
  170. rc = -EINVAL;
  171. goto nla_put_failure;
  172. }
  173. dev = phy->add_iface(phy, devname);
  174. if (IS_ERR(dev)) {
  175. rc = PTR_ERR(dev);
  176. goto nla_put_failure;
  177. }
  178. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  179. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  180. dev_put(dev);
  181. wpan_phy_put(phy);
  182. return ieee802154_nl_reply(msg, info);
  183. nla_put_failure:
  184. nlmsg_free(msg);
  185. out_dev:
  186. wpan_phy_put(phy);
  187. return rc;
  188. }
  189. static int ieee802154_del_iface(struct sk_buff *skb,
  190. struct genl_info *info)
  191. {
  192. struct sk_buff *msg;
  193. struct wpan_phy *phy;
  194. const char *name;
  195. int rc;
  196. struct net_device *dev;
  197. pr_debug("%s\n", __func__);
  198. if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
  199. return -EINVAL;
  200. name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  201. if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
  202. return -EINVAL; /* name should be null-terminated */
  203. dev = dev_get_by_name(genl_info_net(info), name);
  204. if (!dev)
  205. return -ENODEV;
  206. phy = ieee802154_mlme_ops(dev)->get_phy(dev);
  207. BUG_ON(!phy);
  208. rc = -EINVAL;
  209. /* phy name is optional, but should be checked if it's given */
  210. if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
  211. struct wpan_phy *phy2;
  212. const char *pname =
  213. nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  214. if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
  215. != '\0')
  216. /* name should be null-terminated */
  217. goto out_dev;
  218. phy2 = wpan_phy_find(pname);
  219. if (!phy2)
  220. goto out_dev;
  221. if (phy != phy2) {
  222. wpan_phy_put(phy2);
  223. goto out_dev;
  224. }
  225. }
  226. rc = -ENOBUFS;
  227. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
  228. if (!msg)
  229. goto out_dev;
  230. if (!phy->del_iface) {
  231. rc = -EINVAL;
  232. goto nla_put_failure;
  233. }
  234. rtnl_lock();
  235. phy->del_iface(phy, dev);
  236. /* We don't have device anymore */
  237. dev_put(dev);
  238. dev = NULL;
  239. rtnl_unlock();
  240. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  241. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, name);
  242. wpan_phy_put(phy);
  243. return ieee802154_nl_reply(msg, info);
  244. nla_put_failure:
  245. nlmsg_free(msg);
  246. out_dev:
  247. wpan_phy_put(phy);
  248. if (dev)
  249. dev_put(dev);
  250. return rc;
  251. }
  252. static struct genl_ops ieee802154_phy_ops[] = {
  253. IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
  254. ieee802154_dump_phy),
  255. IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
  256. IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
  257. };
  258. /*
  259. * No need to unregister as family unregistration will do it.
  260. */
  261. int nl802154_phy_register(void)
  262. {
  263. int i;
  264. int rc;
  265. for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
  266. rc = genl_register_ops(&nl802154_family,
  267. &ieee802154_phy_ops[i]);
  268. if (rc)
  269. return rc;
  270. }
  271. return 0;
  272. }