nl-mac.c 16 KB

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