scan.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IEEE 802.15.4 scanning management
  4. *
  5. * Copyright (C) 2021 Qorvo US, Inc
  6. * Authors:
  7. * - David Girault <david.girault@qorvo.com>
  8. * - Miquel Raynal <miquel.raynal@bootlin.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/rtnetlink.h>
  12. #include <net/mac802154.h>
  13. #include "ieee802154_i.h"
  14. #include "driver-ops.h"
  15. #include "../ieee802154/nl802154.h"
  16. #define IEEE802154_BEACON_MHR_SZ 13
  17. #define IEEE802154_BEACON_PL_SZ 4
  18. #define IEEE802154_MAC_CMD_MHR_SZ 23
  19. #define IEEE802154_MAC_CMD_PL_SZ 1
  20. #define IEEE802154_BEACON_SKB_SZ (IEEE802154_BEACON_MHR_SZ + \
  21. IEEE802154_BEACON_PL_SZ)
  22. #define IEEE802154_MAC_CMD_SKB_SZ (IEEE802154_MAC_CMD_MHR_SZ + \
  23. IEEE802154_MAC_CMD_PL_SZ)
  24. /* mac802154_scan_cleanup_locked() must be called upon scan completion or abort.
  25. * - Completions are asynchronous, not locked by the rtnl and decided by the
  26. * scan worker.
  27. * - Aborts are decided by userspace, and locked by the rtnl.
  28. *
  29. * Concurrent modifications to the PHY, the interfaces or the hardware is in
  30. * general prevented by the rtnl. So in most cases we don't need additional
  31. * protection.
  32. *
  33. * However, the scan worker get's triggered without anybody noticing and thus we
  34. * must ensure the presence of the devices as well as data consistency:
  35. * - The sub-interface and device driver module get both their reference
  36. * counters incremented whenever we start a scan, so they cannot disappear
  37. * during operation.
  38. * - Data consistency is achieved by the use of rcu protected pointers.
  39. */
  40. static int mac802154_scan_cleanup_locked(struct ieee802154_local *local,
  41. struct ieee802154_sub_if_data *sdata,
  42. bool aborted)
  43. {
  44. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  45. struct wpan_phy *wpan_phy = local->phy;
  46. struct cfg802154_scan_request *request;
  47. u8 arg;
  48. /* Prevent any further use of the scan request */
  49. clear_bit(IEEE802154_IS_SCANNING, &local->ongoing);
  50. cancel_delayed_work(&local->scan_work);
  51. request = rcu_replace_pointer(local->scan_req, NULL, 1);
  52. if (!request)
  53. return 0;
  54. kvfree_rcu_mightsleep(request);
  55. /* Advertize first, while we know the devices cannot be removed */
  56. if (aborted)
  57. arg = NL802154_SCAN_DONE_REASON_ABORTED;
  58. else
  59. arg = NL802154_SCAN_DONE_REASON_FINISHED;
  60. nl802154_scan_done(wpan_phy, wpan_dev, arg);
  61. /* Cleanup software stack */
  62. ieee802154_mlme_op_post(local);
  63. /* Set the hardware back in its original state */
  64. drv_set_channel(local, wpan_phy->current_page,
  65. wpan_phy->current_channel);
  66. ieee802154_configure_durations(wpan_phy, wpan_phy->current_page,
  67. wpan_phy->current_channel);
  68. drv_stop(local);
  69. synchronize_net();
  70. sdata->required_filtering = sdata->iface_default_filtering;
  71. drv_start(local, sdata->required_filtering, &local->addr_filt);
  72. return 0;
  73. }
  74. int mac802154_abort_scan_locked(struct ieee802154_local *local,
  75. struct ieee802154_sub_if_data *sdata)
  76. {
  77. ASSERT_RTNL();
  78. if (!mac802154_is_scanning(local))
  79. return -ESRCH;
  80. return mac802154_scan_cleanup_locked(local, sdata, true);
  81. }
  82. static unsigned int mac802154_scan_get_channel_time(u8 duration_order,
  83. u8 symbol_duration)
  84. {
  85. u64 base_super_frame_duration = (u64)symbol_duration *
  86. IEEE802154_SUPERFRAME_PERIOD * IEEE802154_SLOT_PERIOD;
  87. return usecs_to_jiffies(base_super_frame_duration *
  88. (BIT(duration_order) + 1));
  89. }
  90. static void mac802154_flush_queued_beacons(struct ieee802154_local *local)
  91. {
  92. struct cfg802154_mac_pkt *mac_pkt, *tmp;
  93. list_for_each_entry_safe(mac_pkt, tmp, &local->rx_beacon_list, node) {
  94. list_del(&mac_pkt->node);
  95. kfree_skb(mac_pkt->skb);
  96. kfree(mac_pkt);
  97. }
  98. }
  99. static void
  100. mac802154_scan_get_next_channel(struct ieee802154_local *local,
  101. struct cfg802154_scan_request *scan_req,
  102. u8 *channel)
  103. {
  104. (*channel)++;
  105. *channel = find_next_bit((const unsigned long *)&scan_req->channels,
  106. IEEE802154_MAX_CHANNEL + 1,
  107. *channel);
  108. }
  109. static int mac802154_scan_find_next_chan(struct ieee802154_local *local,
  110. struct cfg802154_scan_request *scan_req,
  111. u8 page, u8 *channel)
  112. {
  113. mac802154_scan_get_next_channel(local, scan_req, channel);
  114. if (*channel > IEEE802154_MAX_CHANNEL)
  115. return -EINVAL;
  116. return 0;
  117. }
  118. static int mac802154_scan_prepare_beacon_req(struct ieee802154_local *local)
  119. {
  120. memset(&local->scan_beacon_req, 0, sizeof(local->scan_beacon_req));
  121. local->scan_beacon_req.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD;
  122. local->scan_beacon_req.mhr.fc.dest_addr_mode = IEEE802154_SHORT_ADDRESSING;
  123. local->scan_beacon_req.mhr.fc.version = IEEE802154_2003_STD;
  124. local->scan_beacon_req.mhr.fc.source_addr_mode = IEEE802154_NO_ADDRESSING;
  125. local->scan_beacon_req.mhr.dest.mode = IEEE802154_ADDR_SHORT;
  126. local->scan_beacon_req.mhr.dest.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
  127. local->scan_beacon_req.mhr.dest.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
  128. local->scan_beacon_req.mac_pl.cmd_id = IEEE802154_CMD_BEACON_REQ;
  129. return 0;
  130. }
  131. static int mac802154_transmit_beacon_req(struct ieee802154_local *local,
  132. struct ieee802154_sub_if_data *sdata)
  133. {
  134. struct sk_buff *skb;
  135. int ret;
  136. skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ, GFP_KERNEL);
  137. if (!skb)
  138. return -ENOBUFS;
  139. skb->dev = sdata->dev;
  140. ret = ieee802154_mac_cmd_push(skb, &local->scan_beacon_req, NULL, 0);
  141. if (ret) {
  142. kfree_skb(skb);
  143. return ret;
  144. }
  145. return ieee802154_mlme_tx(local, sdata, skb);
  146. }
  147. void mac802154_scan_worker(struct work_struct *work)
  148. {
  149. struct ieee802154_local *local =
  150. container_of(work, struct ieee802154_local, scan_work.work);
  151. struct cfg802154_scan_request *scan_req;
  152. enum nl802154_scan_types scan_req_type;
  153. struct ieee802154_sub_if_data *sdata;
  154. unsigned int scan_duration = 0;
  155. struct wpan_phy *wpan_phy;
  156. u8 scan_req_duration;
  157. u8 page, channel;
  158. int ret;
  159. /* Ensure the device receiver is turned off when changing channels
  160. * because there is no atomic way to change the channel and know on
  161. * which one a beacon might have been received.
  162. */
  163. drv_stop(local);
  164. synchronize_net();
  165. mac802154_flush_queued_beacons(local);
  166. rcu_read_lock();
  167. scan_req = rcu_dereference(local->scan_req);
  168. if (unlikely(!scan_req)) {
  169. rcu_read_unlock();
  170. return;
  171. }
  172. sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(scan_req->wpan_dev);
  173. /* Wait an arbitrary amount of time in case we cannot use the device */
  174. if (local->suspended || !ieee802154_sdata_running(sdata)) {
  175. rcu_read_unlock();
  176. queue_delayed_work(local->mac_wq, &local->scan_work,
  177. msecs_to_jiffies(1000));
  178. return;
  179. }
  180. wpan_phy = scan_req->wpan_phy;
  181. scan_req_type = scan_req->type;
  182. scan_req_duration = scan_req->duration;
  183. /* Look for the next valid chan */
  184. page = local->scan_page;
  185. channel = local->scan_channel;
  186. do {
  187. ret = mac802154_scan_find_next_chan(local, scan_req, page, &channel);
  188. if (ret) {
  189. rcu_read_unlock();
  190. goto end_scan;
  191. }
  192. } while (!ieee802154_chan_is_valid(scan_req->wpan_phy, page, channel));
  193. rcu_read_unlock();
  194. /* Bypass the stack on purpose when changing the channel */
  195. rtnl_lock();
  196. ret = drv_set_channel(local, page, channel);
  197. rtnl_unlock();
  198. if (ret) {
  199. dev_err(&sdata->dev->dev,
  200. "Channel change failure during scan, aborting (%d)\n", ret);
  201. goto end_scan;
  202. }
  203. local->scan_page = page;
  204. local->scan_channel = channel;
  205. rtnl_lock();
  206. ret = drv_start(local, IEEE802154_FILTERING_3_SCAN, &local->addr_filt);
  207. rtnl_unlock();
  208. if (ret) {
  209. dev_err(&sdata->dev->dev,
  210. "Restarting failure after channel change, aborting (%d)\n", ret);
  211. goto end_scan;
  212. }
  213. if (scan_req_type == NL802154_SCAN_ACTIVE) {
  214. ret = mac802154_transmit_beacon_req(local, sdata);
  215. if (ret)
  216. dev_err(&sdata->dev->dev,
  217. "Error when transmitting beacon request (%d)\n", ret);
  218. }
  219. ieee802154_configure_durations(wpan_phy, page, channel);
  220. scan_duration = mac802154_scan_get_channel_time(scan_req_duration,
  221. wpan_phy->symbol_duration);
  222. dev_dbg(&sdata->dev->dev,
  223. "Scan page %u channel %u for %ums\n",
  224. page, channel, jiffies_to_msecs(scan_duration));
  225. queue_delayed_work(local->mac_wq, &local->scan_work, scan_duration);
  226. return;
  227. end_scan:
  228. rtnl_lock();
  229. mac802154_scan_cleanup_locked(local, sdata, false);
  230. rtnl_unlock();
  231. }
  232. int mac802154_trigger_scan_locked(struct ieee802154_sub_if_data *sdata,
  233. struct cfg802154_scan_request *request)
  234. {
  235. struct ieee802154_local *local = sdata->local;
  236. ASSERT_RTNL();
  237. if (mac802154_is_scanning(local))
  238. return -EBUSY;
  239. if (request->type != NL802154_SCAN_PASSIVE &&
  240. request->type != NL802154_SCAN_ACTIVE)
  241. return -EOPNOTSUPP;
  242. /* Store scanning parameters */
  243. rcu_assign_pointer(local->scan_req, request);
  244. /* Software scanning requires to set promiscuous mode, so we need to
  245. * pause the Tx queue during the entire operation.
  246. */
  247. ieee802154_mlme_op_pre(local);
  248. sdata->required_filtering = IEEE802154_FILTERING_3_SCAN;
  249. local->scan_page = request->page;
  250. local->scan_channel = -1;
  251. set_bit(IEEE802154_IS_SCANNING, &local->ongoing);
  252. if (request->type == NL802154_SCAN_ACTIVE)
  253. mac802154_scan_prepare_beacon_req(local);
  254. nl802154_scan_started(request->wpan_phy, request->wpan_dev);
  255. queue_delayed_work(local->mac_wq, &local->scan_work, 0);
  256. return 0;
  257. }
  258. int mac802154_process_beacon(struct ieee802154_local *local,
  259. struct sk_buff *skb,
  260. u8 page, u8 channel)
  261. {
  262. struct ieee802154_beacon_hdr *bh = (void *)skb->data;
  263. struct ieee802154_addr *src = &mac_cb(skb)->source;
  264. struct cfg802154_scan_request *scan_req;
  265. struct ieee802154_coord_desc desc;
  266. if (skb->len != sizeof(*bh))
  267. return -EINVAL;
  268. if (unlikely(src->mode == IEEE802154_ADDR_NONE))
  269. return -EINVAL;
  270. dev_dbg(&skb->dev->dev,
  271. "BEACON received on page %u channel %u\n",
  272. page, channel);
  273. memcpy(&desc.addr, src, sizeof(desc.addr));
  274. desc.page = page;
  275. desc.channel = channel;
  276. desc.link_quality = mac_cb(skb)->lqi;
  277. desc.superframe_spec = get_unaligned_le16(skb->data);
  278. desc.gts_permit = bh->gts_permit;
  279. trace_802154_scan_event(&desc);
  280. rcu_read_lock();
  281. scan_req = rcu_dereference(local->scan_req);
  282. if (likely(scan_req))
  283. nl802154_scan_event(scan_req->wpan_phy, scan_req->wpan_dev, &desc);
  284. rcu_read_unlock();
  285. return 0;
  286. }
  287. static int mac802154_transmit_beacon(struct ieee802154_local *local,
  288. struct wpan_dev *wpan_dev)
  289. {
  290. struct cfg802154_beacon_request *beacon_req;
  291. struct ieee802154_sub_if_data *sdata;
  292. struct sk_buff *skb;
  293. int ret;
  294. /* Update the sequence number */
  295. local->beacon.mhr.seq = atomic_inc_return(&wpan_dev->bsn) & 0xFF;
  296. skb = alloc_skb(IEEE802154_BEACON_SKB_SZ, GFP_KERNEL);
  297. if (!skb)
  298. return -ENOBUFS;
  299. rcu_read_lock();
  300. beacon_req = rcu_dereference(local->beacon_req);
  301. if (unlikely(!beacon_req)) {
  302. rcu_read_unlock();
  303. kfree_skb(skb);
  304. return -EINVAL;
  305. }
  306. sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev);
  307. skb->dev = sdata->dev;
  308. rcu_read_unlock();
  309. ret = ieee802154_beacon_push(skb, &local->beacon);
  310. if (ret) {
  311. kfree_skb(skb);
  312. return ret;
  313. }
  314. /* Using the MLME transmission helper for sending beacons is a bit
  315. * overkill because we do not really care about the final outcome.
  316. *
  317. * Even though, going through the whole net stack with a regular
  318. * dev_queue_xmit() is not relevant either because we want beacons to be
  319. * sent "now" rather than go through the whole net stack scheduling
  320. * (qdisc & co).
  321. *
  322. * Finally, using ieee802154_subif_start_xmit() would only be an option
  323. * if we had a generic transmit helper which would acquire the
  324. * HARD_TX_LOCK() to prevent buffer handling conflicts with regular
  325. * packets.
  326. *
  327. * So for now we keep it simple and send beacons with our MLME helper,
  328. * even if it stops the ieee802154 queue entirely during these
  329. * transmissions, wich anyway does not have a huge impact on the
  330. * performances given the current design of the stack.
  331. */
  332. return ieee802154_mlme_tx(local, sdata, skb);
  333. }
  334. void mac802154_beacon_worker(struct work_struct *work)
  335. {
  336. struct ieee802154_local *local =
  337. container_of(work, struct ieee802154_local, beacon_work.work);
  338. struct cfg802154_beacon_request *beacon_req;
  339. struct ieee802154_sub_if_data *sdata;
  340. struct wpan_dev *wpan_dev;
  341. u8 interval;
  342. int ret;
  343. rcu_read_lock();
  344. beacon_req = rcu_dereference(local->beacon_req);
  345. if (unlikely(!beacon_req)) {
  346. rcu_read_unlock();
  347. return;
  348. }
  349. sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev);
  350. /* Wait an arbitrary amount of time in case we cannot use the device */
  351. if (local->suspended || !ieee802154_sdata_running(sdata)) {
  352. rcu_read_unlock();
  353. queue_delayed_work(local->mac_wq, &local->beacon_work,
  354. msecs_to_jiffies(1000));
  355. return;
  356. }
  357. wpan_dev = beacon_req->wpan_dev;
  358. interval = beacon_req->interval;
  359. rcu_read_unlock();
  360. dev_dbg(&sdata->dev->dev, "Sending beacon\n");
  361. ret = mac802154_transmit_beacon(local, wpan_dev);
  362. if (ret)
  363. dev_err(&sdata->dev->dev,
  364. "Beacon could not be transmitted (%d)\n", ret);
  365. if (interval < IEEE802154_ACTIVE_SCAN_DURATION)
  366. queue_delayed_work(local->mac_wq, &local->beacon_work,
  367. local->beacon_interval);
  368. }
  369. int mac802154_stop_beacons_locked(struct ieee802154_local *local,
  370. struct ieee802154_sub_if_data *sdata)
  371. {
  372. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  373. struct cfg802154_beacon_request *request;
  374. ASSERT_RTNL();
  375. if (!mac802154_is_beaconing(local))
  376. return -ESRCH;
  377. clear_bit(IEEE802154_IS_BEACONING, &local->ongoing);
  378. cancel_delayed_work(&local->beacon_work);
  379. request = rcu_replace_pointer(local->beacon_req, NULL, 1);
  380. if (!request)
  381. return 0;
  382. kvfree_rcu_mightsleep(request);
  383. nl802154_beaconing_done(wpan_dev);
  384. return 0;
  385. }
  386. int mac802154_send_beacons_locked(struct ieee802154_sub_if_data *sdata,
  387. struct cfg802154_beacon_request *request)
  388. {
  389. struct ieee802154_local *local = sdata->local;
  390. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  391. ASSERT_RTNL();
  392. if (mac802154_is_beaconing(local))
  393. mac802154_stop_beacons_locked(local, sdata);
  394. /* Store beaconing parameters */
  395. rcu_assign_pointer(local->beacon_req, request);
  396. set_bit(IEEE802154_IS_BEACONING, &local->ongoing);
  397. memset(&local->beacon, 0, sizeof(local->beacon));
  398. local->beacon.mhr.fc.type = IEEE802154_FC_TYPE_BEACON;
  399. local->beacon.mhr.fc.security_enabled = 0;
  400. local->beacon.mhr.fc.frame_pending = 0;
  401. local->beacon.mhr.fc.ack_request = 0;
  402. local->beacon.mhr.fc.intra_pan = 0;
  403. local->beacon.mhr.fc.dest_addr_mode = IEEE802154_NO_ADDRESSING;
  404. local->beacon.mhr.fc.version = IEEE802154_2003_STD;
  405. local->beacon.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING;
  406. atomic_set(&request->wpan_dev->bsn, -1);
  407. local->beacon.mhr.source.mode = IEEE802154_ADDR_LONG;
  408. local->beacon.mhr.source.pan_id = request->wpan_dev->pan_id;
  409. local->beacon.mhr.source.extended_addr = request->wpan_dev->extended_addr;
  410. local->beacon.mac_pl.beacon_order = request->interval;
  411. if (request->interval <= IEEE802154_MAX_SCAN_DURATION)
  412. local->beacon.mac_pl.superframe_order = request->interval;
  413. local->beacon.mac_pl.final_cap_slot = 0xf;
  414. local->beacon.mac_pl.battery_life_ext = 0;
  415. local->beacon.mac_pl.pan_coordinator = !wpan_dev->parent;
  416. local->beacon.mac_pl.assoc_permit = 1;
  417. if (request->interval == IEEE802154_ACTIVE_SCAN_DURATION)
  418. return 0;
  419. /* Start the beacon work */
  420. local->beacon_interval =
  421. mac802154_scan_get_channel_time(request->interval,
  422. request->wpan_phy->symbol_duration);
  423. queue_delayed_work(local->mac_wq, &local->beacon_work, 0);
  424. return 0;
  425. }
  426. int mac802154_perform_association(struct ieee802154_sub_if_data *sdata,
  427. struct ieee802154_pan_device *coord,
  428. __le16 *short_addr)
  429. {
  430. u64 ceaddr = swab64((__force u64)coord->extended_addr);
  431. struct ieee802154_association_req_frame frame = {};
  432. struct ieee802154_local *local = sdata->local;
  433. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  434. struct sk_buff *skb;
  435. int ret;
  436. frame.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD;
  437. frame.mhr.fc.security_enabled = 0;
  438. frame.mhr.fc.frame_pending = 0;
  439. frame.mhr.fc.ack_request = 1; /* We always expect an ack here */
  440. frame.mhr.fc.intra_pan = 0;
  441. frame.mhr.fc.dest_addr_mode = (coord->mode == IEEE802154_ADDR_LONG) ?
  442. IEEE802154_EXTENDED_ADDRESSING : IEEE802154_SHORT_ADDRESSING;
  443. frame.mhr.fc.version = IEEE802154_2003_STD;
  444. frame.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING;
  445. frame.mhr.source.mode = IEEE802154_ADDR_LONG;
  446. frame.mhr.source.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
  447. frame.mhr.source.extended_addr = wpan_dev->extended_addr;
  448. frame.mhr.dest.mode = coord->mode;
  449. frame.mhr.dest.pan_id = coord->pan_id;
  450. if (coord->mode == IEEE802154_ADDR_LONG)
  451. frame.mhr.dest.extended_addr = coord->extended_addr;
  452. else
  453. frame.mhr.dest.short_addr = coord->short_addr;
  454. frame.mhr.seq = atomic_inc_return(&wpan_dev->dsn) & 0xFF;
  455. frame.mac_pl.cmd_id = IEEE802154_CMD_ASSOCIATION_REQ;
  456. frame.assoc_req_pl.device_type = 1;
  457. frame.assoc_req_pl.power_source = 1;
  458. frame.assoc_req_pl.rx_on_when_idle = 1;
  459. frame.assoc_req_pl.alloc_addr = 1;
  460. skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ + sizeof(frame.assoc_req_pl),
  461. GFP_KERNEL);
  462. if (!skb)
  463. return -ENOBUFS;
  464. skb->dev = sdata->dev;
  465. ret = ieee802154_mac_cmd_push(skb, &frame, &frame.assoc_req_pl,
  466. sizeof(frame.assoc_req_pl));
  467. if (ret) {
  468. kfree_skb(skb);
  469. return ret;
  470. }
  471. local->assoc_dev = coord;
  472. reinit_completion(&local->assoc_done);
  473. set_bit(IEEE802154_IS_ASSOCIATING, &local->ongoing);
  474. ret = ieee802154_mlme_tx_one_locked(local, sdata, skb);
  475. if (ret) {
  476. if (ret > 0)
  477. ret = (ret == IEEE802154_NO_ACK) ? -EREMOTEIO : -EIO;
  478. dev_warn(&sdata->dev->dev,
  479. "No ASSOC REQ ACK received from %8phC\n", &ceaddr);
  480. goto clear_assoc;
  481. }
  482. ret = wait_for_completion_killable_timeout(&local->assoc_done, 10 * HZ);
  483. if (ret <= 0) {
  484. dev_warn(&sdata->dev->dev,
  485. "No ASSOC RESP received from %8phC\n", &ceaddr);
  486. ret = -ETIMEDOUT;
  487. goto clear_assoc;
  488. }
  489. if (local->assoc_status != IEEE802154_ASSOCIATION_SUCCESSFUL) {
  490. if (local->assoc_status == IEEE802154_PAN_AT_CAPACITY)
  491. ret = -ERANGE;
  492. else
  493. ret = -EPERM;
  494. dev_warn(&sdata->dev->dev,
  495. "Negative ASSOC RESP received from %8phC: %s\n", &ceaddr,
  496. local->assoc_status == IEEE802154_PAN_AT_CAPACITY ?
  497. "PAN at capacity" : "access denied");
  498. }
  499. ret = 0;
  500. *short_addr = local->assoc_addr;
  501. clear_assoc:
  502. clear_bit(IEEE802154_IS_ASSOCIATING, &local->ongoing);
  503. local->assoc_dev = NULL;
  504. return ret;
  505. }
  506. int mac802154_process_association_resp(struct ieee802154_sub_if_data *sdata,
  507. struct sk_buff *skb)
  508. {
  509. struct ieee802154_addr *src = &mac_cb(skb)->source;
  510. struct ieee802154_addr *dest = &mac_cb(skb)->dest;
  511. u64 deaddr = swab64((__force u64)dest->extended_addr);
  512. struct ieee802154_local *local = sdata->local;
  513. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  514. struct ieee802154_assoc_resp_pl resp_pl = {};
  515. if (skb->len != sizeof(resp_pl))
  516. return -EINVAL;
  517. if (unlikely(src->mode != IEEE802154_EXTENDED_ADDRESSING ||
  518. dest->mode != IEEE802154_EXTENDED_ADDRESSING))
  519. return -EINVAL;
  520. if (unlikely(dest->extended_addr != wpan_dev->extended_addr ||
  521. src->extended_addr != local->assoc_dev->extended_addr))
  522. return -ENODEV;
  523. memcpy(&resp_pl, skb->data, sizeof(resp_pl));
  524. local->assoc_addr = resp_pl.short_addr;
  525. local->assoc_status = resp_pl.status;
  526. dev_dbg(&skb->dev->dev,
  527. "ASSOC RESP 0x%x received from %8phC, getting short address %04x\n",
  528. local->assoc_status, &deaddr, local->assoc_addr);
  529. complete(&local->assoc_done);
  530. return 0;
  531. }
  532. int mac802154_send_disassociation_notif(struct ieee802154_sub_if_data *sdata,
  533. struct ieee802154_pan_device *target,
  534. u8 reason)
  535. {
  536. struct ieee802154_disassociation_notif_frame frame = {};
  537. u64 teaddr = swab64((__force u64)target->extended_addr);
  538. struct ieee802154_local *local = sdata->local;
  539. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  540. struct sk_buff *skb;
  541. int ret;
  542. frame.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD;
  543. frame.mhr.fc.security_enabled = 0;
  544. frame.mhr.fc.frame_pending = 0;
  545. frame.mhr.fc.ack_request = 1;
  546. frame.mhr.fc.intra_pan = 1;
  547. frame.mhr.fc.dest_addr_mode = (target->mode == IEEE802154_ADDR_LONG) ?
  548. IEEE802154_EXTENDED_ADDRESSING : IEEE802154_SHORT_ADDRESSING;
  549. frame.mhr.fc.version = IEEE802154_2003_STD;
  550. frame.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING;
  551. frame.mhr.source.mode = IEEE802154_ADDR_LONG;
  552. frame.mhr.source.pan_id = wpan_dev->pan_id;
  553. frame.mhr.source.extended_addr = wpan_dev->extended_addr;
  554. frame.mhr.dest.mode = target->mode;
  555. frame.mhr.dest.pan_id = wpan_dev->pan_id;
  556. if (target->mode == IEEE802154_ADDR_LONG)
  557. frame.mhr.dest.extended_addr = target->extended_addr;
  558. else
  559. frame.mhr.dest.short_addr = target->short_addr;
  560. frame.mhr.seq = atomic_inc_return(&wpan_dev->dsn) & 0xFF;
  561. frame.mac_pl.cmd_id = IEEE802154_CMD_DISASSOCIATION_NOTIFY;
  562. frame.disassoc_pl = reason;
  563. skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ + sizeof(frame.disassoc_pl),
  564. GFP_KERNEL);
  565. if (!skb)
  566. return -ENOBUFS;
  567. skb->dev = sdata->dev;
  568. ret = ieee802154_mac_cmd_push(skb, &frame, &frame.disassoc_pl,
  569. sizeof(frame.disassoc_pl));
  570. if (ret) {
  571. kfree_skb(skb);
  572. return ret;
  573. }
  574. ret = ieee802154_mlme_tx_one_locked(local, sdata, skb);
  575. if (ret) {
  576. dev_warn(&sdata->dev->dev,
  577. "No DISASSOC ACK received from %8phC\n", &teaddr);
  578. if (ret > 0)
  579. ret = (ret == IEEE802154_NO_ACK) ? -EREMOTEIO : -EIO;
  580. return ret;
  581. }
  582. dev_dbg(&sdata->dev->dev, "DISASSOC ACK received from %8phC\n", &teaddr);
  583. return 0;
  584. }
  585. static int
  586. mac802154_send_association_resp_locked(struct ieee802154_sub_if_data *sdata,
  587. struct ieee802154_pan_device *target,
  588. struct ieee802154_assoc_resp_pl *assoc_resp_pl)
  589. {
  590. u64 teaddr = swab64((__force u64)target->extended_addr);
  591. struct ieee802154_association_resp_frame frame = {};
  592. struct ieee802154_local *local = sdata->local;
  593. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  594. struct sk_buff *skb;
  595. int ret;
  596. frame.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD;
  597. frame.mhr.fc.security_enabled = 0;
  598. frame.mhr.fc.frame_pending = 0;
  599. frame.mhr.fc.ack_request = 1; /* We always expect an ack here */
  600. frame.mhr.fc.intra_pan = 1;
  601. frame.mhr.fc.dest_addr_mode = IEEE802154_EXTENDED_ADDRESSING;
  602. frame.mhr.fc.version = IEEE802154_2003_STD;
  603. frame.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING;
  604. frame.mhr.source.mode = IEEE802154_ADDR_LONG;
  605. frame.mhr.source.extended_addr = wpan_dev->extended_addr;
  606. frame.mhr.dest.mode = IEEE802154_ADDR_LONG;
  607. frame.mhr.dest.pan_id = wpan_dev->pan_id;
  608. frame.mhr.dest.extended_addr = target->extended_addr;
  609. frame.mhr.seq = atomic_inc_return(&wpan_dev->dsn) & 0xFF;
  610. frame.mac_pl.cmd_id = IEEE802154_CMD_ASSOCIATION_RESP;
  611. skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ + sizeof(*assoc_resp_pl),
  612. GFP_KERNEL);
  613. if (!skb)
  614. return -ENOBUFS;
  615. skb->dev = sdata->dev;
  616. ret = ieee802154_mac_cmd_push(skb, &frame, assoc_resp_pl,
  617. sizeof(*assoc_resp_pl));
  618. if (ret) {
  619. kfree_skb(skb);
  620. return ret;
  621. }
  622. ret = ieee802154_mlme_tx_locked(local, sdata, skb);
  623. if (ret) {
  624. dev_warn(&sdata->dev->dev,
  625. "No ASSOC RESP ACK received from %8phC\n", &teaddr);
  626. if (ret > 0)
  627. ret = (ret == IEEE802154_NO_ACK) ? -EREMOTEIO : -EIO;
  628. return ret;
  629. }
  630. return 0;
  631. }
  632. int mac802154_process_association_req(struct ieee802154_sub_if_data *sdata,
  633. struct sk_buff *skb)
  634. {
  635. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  636. struct ieee802154_addr *src = &mac_cb(skb)->source;
  637. struct ieee802154_addr *dest = &mac_cb(skb)->dest;
  638. struct ieee802154_assoc_resp_pl assoc_resp_pl = {};
  639. struct ieee802154_assoc_req_pl assoc_req_pl;
  640. struct ieee802154_pan_device *child, *exchild;
  641. struct ieee802154_addr tmp = {};
  642. u64 ceaddr;
  643. int ret;
  644. if (skb->len != sizeof(assoc_req_pl))
  645. return -EINVAL;
  646. if (unlikely(src->mode != IEEE802154_EXTENDED_ADDRESSING))
  647. return -EINVAL;
  648. if (unlikely(dest->pan_id != wpan_dev->pan_id))
  649. return -ENODEV;
  650. if (dest->mode == IEEE802154_EXTENDED_ADDRESSING &&
  651. unlikely(dest->extended_addr != wpan_dev->extended_addr))
  652. return -ENODEV;
  653. else if (dest->mode == IEEE802154_SHORT_ADDRESSING &&
  654. unlikely(dest->short_addr != wpan_dev->short_addr))
  655. return -ENODEV;
  656. if (wpan_dev->parent) {
  657. dev_dbg(&sdata->dev->dev,
  658. "Ignoring ASSOC REQ, not the PAN coordinator\n");
  659. return -ENODEV;
  660. }
  661. mutex_lock(&wpan_dev->association_lock);
  662. memcpy(&assoc_req_pl, skb->data, sizeof(assoc_req_pl));
  663. if (assoc_req_pl.assoc_type) {
  664. dev_err(&skb->dev->dev, "Fast associations not supported yet\n");
  665. ret = -EOPNOTSUPP;
  666. goto unlock;
  667. }
  668. child = kzalloc(sizeof(*child), GFP_KERNEL);
  669. if (!child) {
  670. ret = -ENOMEM;
  671. goto unlock;
  672. }
  673. child->extended_addr = src->extended_addr;
  674. child->mode = IEEE802154_EXTENDED_ADDRESSING;
  675. ceaddr = swab64((__force u64)child->extended_addr);
  676. if (wpan_dev->nchildren >= wpan_dev->max_associations) {
  677. if (!wpan_dev->max_associations)
  678. assoc_resp_pl.status = IEEE802154_PAN_ACCESS_DENIED;
  679. else
  680. assoc_resp_pl.status = IEEE802154_PAN_AT_CAPACITY;
  681. assoc_resp_pl.short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST);
  682. dev_dbg(&sdata->dev->dev,
  683. "Refusing ASSOC REQ from child %8phC, %s\n", &ceaddr,
  684. assoc_resp_pl.status == IEEE802154_PAN_ACCESS_DENIED ?
  685. "access denied" : "too many children");
  686. } else {
  687. assoc_resp_pl.status = IEEE802154_ASSOCIATION_SUCCESSFUL;
  688. if (assoc_req_pl.alloc_addr) {
  689. assoc_resp_pl.short_addr = cfg802154_get_free_short_addr(wpan_dev);
  690. child->mode = IEEE802154_SHORT_ADDRESSING;
  691. } else {
  692. assoc_resp_pl.short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC);
  693. }
  694. child->short_addr = assoc_resp_pl.short_addr;
  695. dev_dbg(&sdata->dev->dev,
  696. "Accepting ASSOC REQ from child %8phC, providing short address 0x%04x\n",
  697. &ceaddr, le16_to_cpu(child->short_addr));
  698. }
  699. ret = mac802154_send_association_resp_locked(sdata, child, &assoc_resp_pl);
  700. if (ret || assoc_resp_pl.status != IEEE802154_ASSOCIATION_SUCCESSFUL) {
  701. kfree(child);
  702. goto unlock;
  703. }
  704. dev_dbg(&sdata->dev->dev,
  705. "Successful association with new child %8phC\n", &ceaddr);
  706. /* Ensure this child is not already associated (might happen due to
  707. * retransmissions), in this case drop the ex structure.
  708. */
  709. tmp.mode = child->mode;
  710. tmp.extended_addr = child->extended_addr;
  711. exchild = cfg802154_device_is_child(wpan_dev, &tmp);
  712. if (exchild) {
  713. dev_dbg(&sdata->dev->dev,
  714. "Child %8phC was already known\n", &ceaddr);
  715. list_del(&exchild->node);
  716. }
  717. list_add(&child->node, &wpan_dev->children);
  718. wpan_dev->nchildren++;
  719. unlock:
  720. mutex_unlock(&wpan_dev->association_lock);
  721. return ret;
  722. }
  723. int mac802154_process_disassociation_notif(struct ieee802154_sub_if_data *sdata,
  724. struct sk_buff *skb)
  725. {
  726. struct ieee802154_addr *src = &mac_cb(skb)->source;
  727. struct ieee802154_addr *dest = &mac_cb(skb)->dest;
  728. struct wpan_dev *wpan_dev = &sdata->wpan_dev;
  729. struct ieee802154_pan_device *child;
  730. struct ieee802154_addr target;
  731. bool parent;
  732. u64 teaddr;
  733. if (skb->len != sizeof(u8))
  734. return -EINVAL;
  735. if (unlikely(src->mode != IEEE802154_EXTENDED_ADDRESSING))
  736. return -EINVAL;
  737. if (dest->mode == IEEE802154_EXTENDED_ADDRESSING &&
  738. unlikely(dest->extended_addr != wpan_dev->extended_addr))
  739. return -ENODEV;
  740. else if (dest->mode == IEEE802154_SHORT_ADDRESSING &&
  741. unlikely(dest->short_addr != wpan_dev->short_addr))
  742. return -ENODEV;
  743. if (dest->pan_id != wpan_dev->pan_id)
  744. return -ENODEV;
  745. target.mode = IEEE802154_EXTENDED_ADDRESSING;
  746. target.extended_addr = src->extended_addr;
  747. teaddr = swab64((__force u64)target.extended_addr);
  748. dev_dbg(&skb->dev->dev, "Processing DISASSOC NOTIF from %8phC\n", &teaddr);
  749. mutex_lock(&wpan_dev->association_lock);
  750. parent = cfg802154_device_is_parent(wpan_dev, &target);
  751. if (!parent)
  752. child = cfg802154_device_is_child(wpan_dev, &target);
  753. if (!parent && !child) {
  754. mutex_unlock(&wpan_dev->association_lock);
  755. return -EINVAL;
  756. }
  757. if (parent) {
  758. kfree(wpan_dev->parent);
  759. wpan_dev->parent = NULL;
  760. } else {
  761. list_del(&child->node);
  762. kfree(child);
  763. wpan_dev->nchildren--;
  764. }
  765. mutex_unlock(&wpan_dev->association_lock);
  766. return 0;
  767. }