nl-mac.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 portid,
  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 = -EOPNOTSUPP;
  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 (!ieee802154_mlme_ops(dev)->assoc_req)
  276. goto out;
  277. if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) {
  278. addr.addr_type = IEEE802154_ADDR_LONG;
  279. nla_memcpy(addr.hwaddr,
  280. info->attrs[IEEE802154_ATTR_COORD_HW_ADDR],
  281. IEEE802154_ADDR_LEN);
  282. } else {
  283. addr.addr_type = IEEE802154_ADDR_SHORT;
  284. addr.short_addr = nla_get_u16(
  285. info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
  286. }
  287. addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
  288. if (info->attrs[IEEE802154_ATTR_PAGE])
  289. page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
  290. else
  291. page = 0;
  292. ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr,
  293. nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]),
  294. page,
  295. nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY]));
  296. out:
  297. dev_put(dev);
  298. return ret;
  299. }
  300. static int ieee802154_associate_resp(struct sk_buff *skb,
  301. struct genl_info *info)
  302. {
  303. struct net_device *dev;
  304. struct ieee802154_addr addr;
  305. int ret = -EOPNOTSUPP;
  306. if (!info->attrs[IEEE802154_ATTR_STATUS] ||
  307. !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] ||
  308. !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR])
  309. return -EINVAL;
  310. dev = ieee802154_nl_get_dev(info);
  311. if (!dev)
  312. return -ENODEV;
  313. if (!ieee802154_mlme_ops(dev)->assoc_resp)
  314. goto out;
  315. addr.addr_type = IEEE802154_ADDR_LONG;
  316. nla_memcpy(addr.hwaddr, info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
  317. IEEE802154_ADDR_LEN);
  318. addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  319. ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr,
  320. nla_get_u16(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]),
  321. nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS]));
  322. out:
  323. dev_put(dev);
  324. return ret;
  325. }
  326. static int ieee802154_disassociate_req(struct sk_buff *skb,
  327. struct genl_info *info)
  328. {
  329. struct net_device *dev;
  330. struct ieee802154_addr addr;
  331. int ret = -EOPNOTSUPP;
  332. if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] &&
  333. !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) ||
  334. !info->attrs[IEEE802154_ATTR_REASON])
  335. return -EINVAL;
  336. dev = ieee802154_nl_get_dev(info);
  337. if (!dev)
  338. return -ENODEV;
  339. if (!ieee802154_mlme_ops(dev)->disassoc_req)
  340. goto out;
  341. if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) {
  342. addr.addr_type = IEEE802154_ADDR_LONG;
  343. nla_memcpy(addr.hwaddr,
  344. info->attrs[IEEE802154_ATTR_DEST_HW_ADDR],
  345. IEEE802154_ADDR_LEN);
  346. } else {
  347. addr.addr_type = IEEE802154_ADDR_SHORT;
  348. addr.short_addr = nla_get_u16(
  349. info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]);
  350. }
  351. addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  352. ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr,
  353. nla_get_u8(info->attrs[IEEE802154_ATTR_REASON]));
  354. out:
  355. dev_put(dev);
  356. return ret;
  357. }
  358. /*
  359. * PANid, channel, beacon_order = 15, superframe_order = 15,
  360. * PAN_coordinator, battery_life_extension = 0,
  361. * coord_realignment = 0, security_enable = 0
  362. */
  363. static int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
  364. {
  365. struct net_device *dev;
  366. struct ieee802154_addr addr;
  367. u8 channel, bcn_ord, sf_ord;
  368. u8 page;
  369. int pan_coord, blx, coord_realign;
  370. int ret = -EOPNOTSUPP;
  371. if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] ||
  372. !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] ||
  373. !info->attrs[IEEE802154_ATTR_CHANNEL] ||
  374. !info->attrs[IEEE802154_ATTR_BCN_ORD] ||
  375. !info->attrs[IEEE802154_ATTR_SF_ORD] ||
  376. !info->attrs[IEEE802154_ATTR_PAN_COORD] ||
  377. !info->attrs[IEEE802154_ATTR_BAT_EXT] ||
  378. !info->attrs[IEEE802154_ATTR_COORD_REALIGN]
  379. )
  380. return -EINVAL;
  381. dev = ieee802154_nl_get_dev(info);
  382. if (!dev)
  383. return -ENODEV;
  384. if (!ieee802154_mlme_ops(dev)->start_req)
  385. goto out;
  386. addr.addr_type = IEEE802154_ADDR_SHORT;
  387. addr.short_addr = nla_get_u16(
  388. info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]);
  389. addr.pan_id = nla_get_u16(info->attrs[IEEE802154_ATTR_COORD_PAN_ID]);
  390. channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]);
  391. bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]);
  392. sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]);
  393. pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]);
  394. blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]);
  395. coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]);
  396. if (info->attrs[IEEE802154_ATTR_PAGE])
  397. page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
  398. else
  399. page = 0;
  400. if (addr.short_addr == IEEE802154_ADDR_BROADCAST) {
  401. ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS);
  402. dev_put(dev);
  403. return -EINVAL;
  404. }
  405. ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page,
  406. bcn_ord, sf_ord, pan_coord, blx, coord_realign);
  407. out:
  408. dev_put(dev);
  409. return ret;
  410. }
  411. static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
  412. {
  413. struct net_device *dev;
  414. int ret = -EOPNOTSUPP;
  415. u8 type;
  416. u32 channels;
  417. u8 duration;
  418. u8 page;
  419. if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] ||
  420. !info->attrs[IEEE802154_ATTR_CHANNELS] ||
  421. !info->attrs[IEEE802154_ATTR_DURATION])
  422. return -EINVAL;
  423. dev = ieee802154_nl_get_dev(info);
  424. if (!dev)
  425. return -ENODEV;
  426. if (!ieee802154_mlme_ops(dev)->scan_req)
  427. goto out;
  428. type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]);
  429. channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]);
  430. duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]);
  431. if (info->attrs[IEEE802154_ATTR_PAGE])
  432. page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]);
  433. else
  434. page = 0;
  435. ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels, page,
  436. duration);
  437. out:
  438. dev_put(dev);
  439. return ret;
  440. }
  441. static int ieee802154_list_iface(struct sk_buff *skb,
  442. struct genl_info *info)
  443. {
  444. /* Request for interface name, index, type, IEEE address,
  445. PAN Id, short address */
  446. struct sk_buff *msg;
  447. struct net_device *dev = NULL;
  448. int rc = -ENOBUFS;
  449. pr_debug("%s\n", __func__);
  450. dev = ieee802154_nl_get_dev(info);
  451. if (!dev)
  452. return -ENODEV;
  453. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  454. if (!msg)
  455. goto out_dev;
  456. rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq,
  457. 0, dev);
  458. if (rc < 0)
  459. goto out_free;
  460. dev_put(dev);
  461. return genlmsg_reply(msg, info);
  462. out_free:
  463. nlmsg_free(msg);
  464. out_dev:
  465. dev_put(dev);
  466. return rc;
  467. }
  468. static int ieee802154_dump_iface(struct sk_buff *skb,
  469. struct netlink_callback *cb)
  470. {
  471. struct net *net = sock_net(skb->sk);
  472. struct net_device *dev;
  473. int idx;
  474. int s_idx = cb->args[0];
  475. pr_debug("%s\n", __func__);
  476. idx = 0;
  477. for_each_netdev(net, dev) {
  478. if (idx < s_idx || (dev->type != ARPHRD_IEEE802154))
  479. goto cont;
  480. if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid,
  481. cb->nlh->nlmsg_seq, NLM_F_MULTI, dev) < 0)
  482. break;
  483. cont:
  484. idx++;
  485. }
  486. cb->args[0] = idx;
  487. return skb->len;
  488. }
  489. static struct genl_ops ieee802154_coordinator_ops[] = {
  490. IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
  491. IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
  492. IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
  493. IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
  494. IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
  495. IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
  496. ieee802154_dump_iface),
  497. };
  498. /*
  499. * No need to unregister as family unregistration will do it.
  500. */
  501. int nl802154_mac_register(void)
  502. {
  503. int i;
  504. int rc;
  505. rc = genl_register_mc_group(&nl802154_family,
  506. &ieee802154_coord_mcgrp);
  507. if (rc)
  508. return rc;
  509. rc = genl_register_mc_group(&nl802154_family,
  510. &ieee802154_beacon_mcgrp);
  511. if (rc)
  512. return rc;
  513. for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
  514. rc = genl_register_ops(&nl802154_family,
  515. &ieee802154_coordinator_ops[i]);
  516. if (rc)
  517. return rc;
  518. }
  519. return 0;
  520. }