nl-phy.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Netlink interface for IEEE 802.15.4 stack
  4. *
  5. * Copyright 2007, 2008 Siemens AG
  6. *
  7. * Written by:
  8. * Sergey Lapin <[email protected]>
  9. * Dmitry Eremin-Solenikov <[email protected]>
  10. * Maxim Osipov <[email protected]>
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/if_arp.h>
  15. #include <net/netlink.h>
  16. #include <net/genetlink.h>
  17. #include <net/cfg802154.h>
  18. #include <net/af_ieee802154.h>
  19. #include <net/ieee802154_netdev.h>
  20. #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
  21. #include <linux/nl802154.h>
  22. #include "ieee802154.h"
  23. #include "rdev-ops.h"
  24. #include "core.h"
  25. static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
  26. u32 seq, int flags, struct wpan_phy *phy)
  27. {
  28. void *hdr;
  29. int i, pages = 0;
  30. u32 *buf = kcalloc(IEEE802154_MAX_PAGE + 1, sizeof(u32), GFP_KERNEL);
  31. pr_debug("%s\n", __func__);
  32. if (!buf)
  33. return -EMSGSIZE;
  34. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
  35. IEEE802154_LIST_PHY);
  36. if (!hdr)
  37. goto out;
  38. rtnl_lock();
  39. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  40. nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) ||
  41. nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel))
  42. goto nla_put_failure;
  43. for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
  44. if (phy->supported.channels[i])
  45. buf[pages++] = phy->supported.channels[i] | (i << 27);
  46. }
  47. if (pages &&
  48. nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
  49. pages * sizeof(uint32_t), buf))
  50. goto nla_put_failure;
  51. rtnl_unlock();
  52. kfree(buf);
  53. genlmsg_end(msg, hdr);
  54. return 0;
  55. nla_put_failure:
  56. rtnl_unlock();
  57. genlmsg_cancel(msg, hdr);
  58. out:
  59. kfree(buf);
  60. return -EMSGSIZE;
  61. }
  62. int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
  63. {
  64. /* Request for interface name, index, type, IEEE address,
  65. * PAN Id, short address
  66. */
  67. struct sk_buff *msg;
  68. struct wpan_phy *phy;
  69. const char *name;
  70. int rc = -ENOBUFS;
  71. pr_debug("%s\n", __func__);
  72. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  73. return -EINVAL;
  74. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  75. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  76. return -EINVAL; /* phy name should be null-terminated */
  77. phy = wpan_phy_find(name);
  78. if (!phy)
  79. return -ENODEV;
  80. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  81. if (!msg)
  82. goto out_dev;
  83. rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
  84. 0, phy);
  85. if (rc < 0)
  86. goto out_free;
  87. wpan_phy_put(phy);
  88. return genlmsg_reply(msg, info);
  89. out_free:
  90. nlmsg_free(msg);
  91. out_dev:
  92. wpan_phy_put(phy);
  93. return rc;
  94. }
  95. struct dump_phy_data {
  96. struct sk_buff *skb;
  97. struct netlink_callback *cb;
  98. int idx, s_idx;
  99. };
  100. static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
  101. {
  102. int rc;
  103. struct dump_phy_data *data = _data;
  104. pr_debug("%s\n", __func__);
  105. if (data->idx++ < data->s_idx)
  106. return 0;
  107. rc = ieee802154_nl_fill_phy(data->skb,
  108. NETLINK_CB(data->cb->skb).portid,
  109. data->cb->nlh->nlmsg_seq,
  110. NLM_F_MULTI,
  111. phy);
  112. if (rc < 0) {
  113. data->idx--;
  114. return rc;
  115. }
  116. return 0;
  117. }
  118. int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
  119. {
  120. struct dump_phy_data data = {
  121. .cb = cb,
  122. .skb = skb,
  123. .s_idx = cb->args[0],
  124. .idx = 0,
  125. };
  126. pr_debug("%s\n", __func__);
  127. wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
  128. cb->args[0] = data.idx;
  129. return skb->len;
  130. }
  131. int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
  132. {
  133. struct sk_buff *msg;
  134. struct wpan_phy *phy;
  135. const char *name;
  136. const char *devname;
  137. int rc = -ENOBUFS;
  138. struct net_device *dev;
  139. int type = __IEEE802154_DEV_INVALID;
  140. unsigned char name_assign_type;
  141. pr_debug("%s\n", __func__);
  142. if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
  143. return -EINVAL;
  144. name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  145. if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
  146. return -EINVAL; /* phy name should be null-terminated */
  147. if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
  148. devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  149. if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
  150. != '\0')
  151. return -EINVAL; /* phy name should be null-terminated */
  152. name_assign_type = NET_NAME_USER;
  153. } else {
  154. devname = "wpan%d";
  155. name_assign_type = NET_NAME_ENUM;
  156. }
  157. if (strlen(devname) >= IFNAMSIZ)
  158. return -ENAMETOOLONG;
  159. phy = wpan_phy_find(name);
  160. if (!phy)
  161. return -ENODEV;
  162. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
  163. if (!msg)
  164. goto out_dev;
  165. if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
  166. nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
  167. IEEE802154_ADDR_LEN) {
  168. rc = -EINVAL;
  169. goto nla_put_failure;
  170. }
  171. if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
  172. type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
  173. if (type >= __IEEE802154_DEV_MAX) {
  174. rc = -EINVAL;
  175. goto nla_put_failure;
  176. }
  177. }
  178. dev = rdev_add_virtual_intf_deprecated(wpan_phy_to_rdev(phy), devname,
  179. name_assign_type, type);
  180. if (IS_ERR(dev)) {
  181. rc = PTR_ERR(dev);
  182. goto nla_put_failure;
  183. }
  184. dev_hold(dev);
  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. /* strangely enough, some callbacks (inetdev_event) from
  191. * dev_set_mac_address require RTNL_LOCK
  192. */
  193. rtnl_lock();
  194. rc = dev_set_mac_address(dev, &addr, NULL);
  195. rtnl_unlock();
  196. if (rc)
  197. goto dev_unregister;
  198. }
  199. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  200. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name)) {
  201. rc = -EMSGSIZE;
  202. goto nla_put_failure;
  203. }
  204. dev_put(dev);
  205. wpan_phy_put(phy);
  206. return ieee802154_nl_reply(msg, info);
  207. dev_unregister:
  208. rtnl_lock(); /* del_iface must be called with RTNL lock */
  209. rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
  210. dev_put(dev);
  211. rtnl_unlock();
  212. nla_put_failure:
  213. nlmsg_free(msg);
  214. out_dev:
  215. wpan_phy_put(phy);
  216. return rc;
  217. }
  218. int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
  219. {
  220. struct sk_buff *msg;
  221. struct wpan_phy *phy;
  222. const char *name;
  223. int rc;
  224. struct net_device *dev;
  225. pr_debug("%s\n", __func__);
  226. if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
  227. return -EINVAL;
  228. name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
  229. if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
  230. return -EINVAL; /* name should be null-terminated */
  231. rc = -ENODEV;
  232. dev = dev_get_by_name(genl_info_net(info), name);
  233. if (!dev)
  234. return rc;
  235. if (dev->type != ARPHRD_IEEE802154)
  236. goto out;
  237. phy = dev->ieee802154_ptr->wpan_phy;
  238. BUG_ON(!phy);
  239. get_device(&phy->dev);
  240. rc = -EINVAL;
  241. /* phy name is optional, but should be checked if it's given */
  242. if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
  243. struct wpan_phy *phy2;
  244. const char *pname =
  245. nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
  246. if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
  247. != '\0')
  248. /* name should be null-terminated */
  249. goto out_dev;
  250. phy2 = wpan_phy_find(pname);
  251. if (!phy2)
  252. goto out_dev;
  253. if (phy != phy2) {
  254. wpan_phy_put(phy2);
  255. goto out_dev;
  256. }
  257. }
  258. rc = -ENOBUFS;
  259. msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
  260. if (!msg)
  261. goto out_dev;
  262. rtnl_lock();
  263. rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
  264. /* We don't have device anymore */
  265. dev_put(dev);
  266. dev = NULL;
  267. rtnl_unlock();
  268. if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
  269. nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
  270. goto nla_put_failure;
  271. wpan_phy_put(phy);
  272. return ieee802154_nl_reply(msg, info);
  273. nla_put_failure:
  274. nlmsg_free(msg);
  275. out_dev:
  276. wpan_phy_put(phy);
  277. out:
  278. dev_put(dev);
  279. return rc;
  280. }