nl-mac.c 15 KB

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