nl-mac.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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/if_arp.h>
  26. #include <linux/netdevice.h>
  27. #include <net/netlink.h>
  28. #include <net/genetlink.h>
  29. #include <net/sock.h>
  30. #include <linux/nl802154.h>
  31. #include <net/af_ieee802154.h>
  32. #include <net/nl802154.h>
  33. #include <net/ieee802154.h>
  34. #include <net/ieee802154_netdev.h>
  35. #include <net/wpan-phy.h>
  36. #include "ieee802154.h"
  37. static struct genl_multicast_group ieee802154_coord_mcgrp = {
  38. .name = IEEE802154_MCAST_COORD_NAME,
  39. };
  40. static struct genl_multicast_group ieee802154_beacon_mcgrp = {
  41. .name = IEEE802154_MCAST_BEACON_NAME,
  42. };
  43. int ieee802154_nl_assoc_indic(struct net_device *dev,
  44. struct ieee802154_addr *addr, u8 cap)
  45. {
  46. struct sk_buff *msg;
  47. pr_debug("%s\n", __func__);
  48. if (addr->addr_type != IEEE802154_ADDR_LONG) {
  49. pr_err("%s: received non-long source address!\n", __func__);
  50. return -EINVAL;
  51. }
  52. msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC);
  53. if (!msg)
  54. return -ENOBUFS;
  55. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  56. NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
  57. NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  58. dev->dev_addr);
  59. NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
  60. addr->hwaddr);
  61. NLA_PUT_U8(msg, IEEE802154_ATTR_CAPABILITY, cap);
  62. return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
  63. nla_put_failure:
  64. nlmsg_free(msg);
  65. return -ENOBUFS;
  66. }
  67. EXPORT_SYMBOL(ieee802154_nl_assoc_indic);
  68. int ieee802154_nl_assoc_confirm(struct net_device *dev, u16 short_addr,
  69. u8 status)
  70. {
  71. struct sk_buff *msg;
  72. pr_debug("%s\n", __func__);
  73. msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF);
  74. if (!msg)
  75. return -ENOBUFS;
  76. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  77. NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
  78. NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  79. dev->dev_addr);
  80. NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr);
  81. NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
  82. return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
  83. nla_put_failure:
  84. nlmsg_free(msg);
  85. return -ENOBUFS;
  86. }
  87. EXPORT_SYMBOL(ieee802154_nl_assoc_confirm);
  88. int ieee802154_nl_disassoc_indic(struct net_device *dev,
  89. struct ieee802154_addr *addr, u8 reason)
  90. {
  91. struct sk_buff *msg;
  92. pr_debug("%s\n", __func__);
  93. msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC);
  94. if (!msg)
  95. return -ENOBUFS;
  96. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  97. NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
  98. NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  99. dev->dev_addr);
  100. if (addr->addr_type == IEEE802154_ADDR_LONG)
  101. NLA_PUT(msg, IEEE802154_ATTR_SRC_HW_ADDR, IEEE802154_ADDR_LEN,
  102. addr->hwaddr);
  103. else
  104. NLA_PUT_U16(msg, IEEE802154_ATTR_SRC_SHORT_ADDR,
  105. addr->short_addr);
  106. NLA_PUT_U8(msg, IEEE802154_ATTR_REASON, reason);
  107. return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
  108. nla_put_failure:
  109. nlmsg_free(msg);
  110. return -ENOBUFS;
  111. }
  112. EXPORT_SYMBOL(ieee802154_nl_disassoc_indic);
  113. int ieee802154_nl_disassoc_confirm(struct net_device *dev, u8 status)
  114. {
  115. struct sk_buff *msg;
  116. pr_debug("%s\n", __func__);
  117. msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF);
  118. if (!msg)
  119. return -ENOBUFS;
  120. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  121. NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
  122. NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  123. dev->dev_addr);
  124. NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
  125. return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
  126. nla_put_failure:
  127. nlmsg_free(msg);
  128. return -ENOBUFS;
  129. }
  130. EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm);
  131. int ieee802154_nl_beacon_indic(struct net_device *dev,
  132. u16 panid, u16 coord_addr)
  133. {
  134. struct sk_buff *msg;
  135. pr_debug("%s\n", __func__);
  136. msg = ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC);
  137. if (!msg)
  138. return -ENOBUFS;
  139. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  140. NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
  141. NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  142. dev->dev_addr);
  143. NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, coord_addr);
  144. NLA_PUT_U16(msg, IEEE802154_ATTR_COORD_PAN_ID, panid);
  145. return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
  146. nla_put_failure:
  147. nlmsg_free(msg);
  148. return -ENOBUFS;
  149. }
  150. EXPORT_SYMBOL(ieee802154_nl_beacon_indic);
  151. int ieee802154_nl_scan_confirm(struct net_device *dev,
  152. u8 status, u8 scan_type, u32 unscanned, u8 page,
  153. u8 *edl/* , struct list_head *pan_desc_list */)
  154. {
  155. struct sk_buff *msg;
  156. pr_debug("%s\n", __func__);
  157. msg = ieee802154_nl_create(0, IEEE802154_SCAN_CONF);
  158. if (!msg)
  159. return -ENOBUFS;
  160. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  161. NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
  162. NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  163. dev->dev_addr);
  164. NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
  165. NLA_PUT_U8(msg, IEEE802154_ATTR_SCAN_TYPE, scan_type);
  166. NLA_PUT_U32(msg, IEEE802154_ATTR_CHANNELS, unscanned);
  167. NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, page);
  168. if (edl)
  169. NLA_PUT(msg, IEEE802154_ATTR_ED_LIST, 27, edl);
  170. return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
  171. nla_put_failure:
  172. nlmsg_free(msg);
  173. return -ENOBUFS;
  174. }
  175. EXPORT_SYMBOL(ieee802154_nl_scan_confirm);
  176. int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
  177. {
  178. struct sk_buff *msg;
  179. pr_debug("%s\n", __func__);
  180. msg = ieee802154_nl_create(0, IEEE802154_START_CONF);
  181. if (!msg)
  182. return -ENOBUFS;
  183. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  184. NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
  185. NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  186. dev->dev_addr);
  187. NLA_PUT_U8(msg, IEEE802154_ATTR_STATUS, status);
  188. return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id);
  189. nla_put_failure:
  190. nlmsg_free(msg);
  191. return -ENOBUFS;
  192. }
  193. EXPORT_SYMBOL(ieee802154_nl_start_confirm);
  194. static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 pid,
  195. u32 seq, int flags, struct net_device *dev)
  196. {
  197. void *hdr;
  198. struct wpan_phy *phy;
  199. pr_debug("%s\n", __func__);
  200. hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
  201. IEEE802154_LIST_IFACE);
  202. if (!hdr)
  203. goto out;
  204. phy = ieee802154_mlme_ops(dev)->get_phy(dev);
  205. BUG_ON(!phy);
  206. NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name);
  207. NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy));
  208. NLA_PUT_U32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex);
  209. NLA_PUT(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN,
  210. dev->dev_addr);
  211. NLA_PUT_U16(msg, IEEE802154_ATTR_SHORT_ADDR,
  212. ieee802154_mlme_ops(dev)->get_short_addr(dev));
  213. NLA_PUT_U16(msg, IEEE802154_ATTR_PAN_ID,
  214. ieee802154_mlme_ops(dev)->get_pan_id(dev));
  215. wpan_phy_put(phy);
  216. return genlmsg_end(msg, hdr);
  217. nla_put_failure:
  218. wpan_phy_put(phy);
  219. genlmsg_cancel(msg, hdr);
  220. out:
  221. return -EMSGSIZE;
  222. }
  223. /* Requests from userspace */
  224. static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
  225. {
  226. struct net_device *dev;
  227. if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
  228. char name[IFNAMSIZ + 1];
  229. nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME],
  230. sizeof(name));
  231. dev = dev_get_by_name(&init_net, name);
  232. } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX])
  233. dev = dev_get_by_index(&init_net,
  234. nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX]));
  235. else
  236. return NULL;
  237. if (!dev)
  238. return NULL;
  239. if (dev->type != ARPHRD_IEEE802154) {
  240. dev_put(dev);
  241. return NULL;
  242. }
  243. return dev;
  244. }
  245. static int ieee802154_associate_req(struct sk_buff *skb,
  246. struct genl_info *info)
  247. {
  248. struct net_device *dev;
  249. struct ieee802154_addr addr;
  250. u8 page;
  251. int ret = -EINVAL;
  252. if (!info->attrs[IEEE802154_ATTR_CHANNEL] ||
  253. !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
  254. (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] &&
  255. !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) ||
  256. !info->attrs[IEEE802154_ATTR_CAPABILITY])
  257. return -EINVAL;
  258. dev = ieee802154_nl_get_dev(info);
  259. if (!dev)
  260. return -ENODEV;
  261. if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
  262. addr.addr_type = IEEE802154_ADDR_LONG;
  263. nla_memcpy(addr.hwaddr,
  264. info->attrs[IEEE802154_ATTR_COORD_HW_ADDR],
  265. IEEE802154_ADDR_LEN);
  266. } else {
  267. addr.addr_type = IEEE802154_ADDR_SHORT;
  268. addr.short_addr = nla_get_u16(
  269. info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
  270. }
  271. addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
  272. if (info->attrs[IEEE802154_ATTR_PAGE])
  273. page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
  274. else
  275. page = 0;
  276. ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
  277. nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
  278. page,
  279. nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
  280. dev_put(dev);
  281. return ret;
  282. }
  283. static int ieee802154_associate_resp(struct sk_buff *skb,
  284. struct genl_info *info)
  285. {
  286. struct net_device *dev;
  287. struct ieee802154_addr addr;
  288. int ret = -EINVAL;
  289. if (!info->attrs[IEEE802154_ATTR_STATUS] ||
  290. !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
  291. !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
  292. return -EINVAL;
  293. dev = ieee802154_nl_get_dev(info);
  294. if (!dev)
  295. return -ENODEV;
  296. addr.addr_type = IEEE802154_ADDR_LONG;
  297. nla_memcpy(addr.hwaddr, info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
  298. IEEE802154_ADDR_LEN);
  299. addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  300. ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
  301. nla_get_u16(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
  302. nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
  303. dev_put(dev);
  304. return ret;
  305. }
  306. static int ieee802154_disassociate_req(struct sk_buff *skb,
  307. struct genl_info *info)
  308. {
  309. struct net_device *dev;
  310. struct ieee802154_addr addr;
  311. int ret = -EINVAL;
  312. if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
  313. !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
  314. !info->attrs[IEEE802154_ATTR_REASON])
  315. return -EINVAL;
  316. dev = ieee802154_nl_get_dev(info);
  317. if (!dev)
  318. return -ENODEV;
  319. if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
  320. addr.addr_type = IEEE802154_ADDR_LONG;
  321. nla_memcpy(addr.hwaddr,
  322. info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
  323. IEEE802154_ADDR_LEN);
  324. } else {
  325. addr.addr_type = IEEE802154_ADDR_SHORT;
  326. addr.short_addr = nla_get_u16(
  327. info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
  328. }
  329. addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  330. ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
  331. nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
  332. dev_put(dev);
  333. return ret;
  334. }
  335. /*
  336. * PANid, channel, beacon_order = 15, superframe_order = 15,
  337. * PAN_coordinator, battery_life_extension = 0,
  338. * coord_realignment = 0, security_enable = 0
  339. */
  340. static int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
  341. {
  342. struct net_device *dev;
  343. struct ieee802154_addr addr;
  344. u8 channel, bcn_ord, sf_ord;
  345. u8 page;
  346. int pan_coord, blx, coord_realign;
  347. int ret;
  348. if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
  349. !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
  350. !info->attrs[IEEE802154_ATTR_CHANNEL] ||
  351. !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
  352. !info->attrs[IEEE802154_ATTR_SF_ORD] ||
  353. !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
  354. !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
  355. !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
  356. )
  357. return -EINVAL;
  358. dev = ieee802154_nl_get_dev(info);
  359. if (!dev)
  360. return -ENODEV;
  361. addr.addr_type = IEEE802154_ADDR_SHORT;
  362. addr.short_addr = nla_get_u16(
  363. info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
  364. addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
  365. channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
  366. bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
  367. sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
  368. pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
  369. blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
  370. coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
  371. if (info->attrs[IEEE802154_ATTR_PAGE])
  372. page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
  373. else
  374. page = 0;
  375. if (addr.short_addr == IEEE802154_ADDR_BROADCAST) {
  376. ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
  377. dev_put(dev);
  378. return -EINVAL;
  379. }
  380. ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
  381. bcn_ord, sf_ord, pan_coord, blx, coord_realign);
  382. dev_put(dev);
  383. return ret;
  384. }
  385. static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
  386. {
  387. struct net_device *dev;
  388. int ret;
  389. u8 type;
  390. u32 channels;
  391. u8 duration;
  392. u8 page;
  393. if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
  394. !info->attrs[IEEE802154_ATTR_CHANNELS] ||
  395. !info->attrs[IEEE802154_ATTR_DURATION])
  396. return -EINVAL;
  397. dev = ieee802154_nl_get_dev(info);
  398. if (!dev)
  399. return -ENODEV;
  400. type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
  401. channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
  402. duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
  403. if (info->attrs[IEEE802154_ATTR_PAGE])
  404. page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
  405. else
  406. page = 0;
  407. ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels, page,
  408. duration);
  409. dev_put(dev);
  410. return ret;
  411. }
  412. static int ieee802154_list_iface(struct sk_buff *skb,
  413. struct genl_info *info)
  414. {
  415. /* Request for interface name, index, type, IEEE address,
  416. PAN Id, short address */
  417. struct sk_buff *msg;
  418. struct net_device *dev = NULL;
  419. int rc = -ENOBUFS;
  420. pr_debug("%s\n", __func__);
  421. dev = ieee802154_nl_get_dev(info);
  422. if (!dev)
  423. return -ENODEV;
  424. msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  425. if (!msg)
  426. goto out_dev;
  427. rc = ieee802154_nl_fill_iface(msg, info->snd_pid, info->snd_seq,
  428. 0, dev);
  429. if (rc < 0)
  430. goto out_free;
  431. dev_put(dev);
  432. return genlmsg_reply(msg, info);
  433. out_free:
  434. nlmsg_free(msg);
  435. out_dev:
  436. dev_put(dev);
  437. return rc;
  438. }
  439. static int ieee802154_dump_iface(struct sk_buff *skb,
  440. struct netlink_callback *cb)
  441. {
  442. struct net *net = sock_net(skb->sk);
  443. struct net_device *dev;
  444. int idx;
  445. int s_idx = cb->args[0];
  446. pr_debug("%s\n", __func__);
  447. idx = 0;
  448. for_each_netdev(net, dev) {
  449. if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
  450. goto cont;
  451. if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).pid,
  452. cb->nlh->nlmsg_seq, NLM_F_MULTI, dev) < 0)
  453. break;
  454. cont:
  455. idx++;
  456. }
  457. cb->args[0] = idx;
  458. return skb->len;
  459. }
  460. static struct genl_ops ieee802154_coordinator_ops[] = {
  461. IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
  462. IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
  463. IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
  464. IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
  465. IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
  466. IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
  467. ieee802154_dump_iface),
  468. };
  469. /*
  470. * No need to unregister as family unregistration will do it.
  471. */
  472. int nl802154_mac_register(void)
  473. {
  474. int i;
  475. int rc;
  476. rc = genl_register_mc_group(&nl802154_family,
  477. &ieee802154_coord_mcgrp);
  478. if (rc)
  479. return rc;
  480. rc = genl_register_mc_group(&nl802154_family,
  481. &ieee802154_beacon_mcgrp);
  482. if (rc)
  483. return rc;
  484. for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
  485. rc = genl_register_ops(&nl802154_family,
  486. &ieee802154_coordinator_ops[i]);
  487. if (rc)
  488. return rc;
  489. }
  490. return 0;
  491. }