nl-mac.c 16 KB

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