work.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. * mac80211 work implementation
  3. *
  4. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  5. * Copyright 2004, Instant802 Networks, Inc.
  6. * Copyright 2005, Devicescape Software, Inc.
  7. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  8. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  9. * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/crc32.h>
  21. #include <linux/slab.h>
  22. #include <net/mac80211.h>
  23. #include <asm/unaligned.h>
  24. #include "ieee80211_i.h"
  25. #include "rate.h"
  26. #include "driver-ops.h"
  27. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  28. #define IEEE80211_AUTH_MAX_TRIES 3
  29. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  30. #define IEEE80211_ASSOC_MAX_TRIES 3
  31. enum work_action {
  32. WORK_ACT_MISMATCH,
  33. WORK_ACT_NONE,
  34. WORK_ACT_TIMEOUT,
  35. WORK_ACT_DONE,
  36. };
  37. /* utils */
  38. static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
  39. {
  40. lockdep_assert_held(&local->mtx);
  41. }
  42. /*
  43. * We can have multiple work items (and connection probing)
  44. * scheduling this timer, but we need to take care to only
  45. * reschedule it when it should fire _earlier_ than it was
  46. * asked for before, or if it's not pending right now. This
  47. * function ensures that. Note that it then is required to
  48. * run this function for all timeouts after the first one
  49. * has happened -- the work that runs from this timer will
  50. * do that.
  51. */
  52. static void run_again(struct ieee80211_local *local,
  53. unsigned long timeout)
  54. {
  55. ASSERT_WORK_MTX(local);
  56. if (!timer_pending(&local->work_timer) ||
  57. time_before(timeout, local->work_timer.expires))
  58. mod_timer(&local->work_timer, timeout);
  59. }
  60. void free_work(struct ieee80211_work *wk)
  61. {
  62. kfree_rcu(wk, rcu_head);
  63. }
  64. static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
  65. struct ieee80211_supported_band *sband,
  66. u32 *rates)
  67. {
  68. int i, j, count;
  69. *rates = 0;
  70. count = 0;
  71. for (i = 0; i < supp_rates_len; i++) {
  72. int rate = (supp_rates[i] & 0x7F) * 5;
  73. for (j = 0; j < sband->n_bitrates; j++)
  74. if (sband->bitrates[j].bitrate == rate) {
  75. *rates |= BIT(j);
  76. count++;
  77. break;
  78. }
  79. }
  80. return count;
  81. }
  82. /* frame sending functions */
  83. static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
  84. struct sk_buff *skb, const u8 *ht_info_ie,
  85. struct ieee80211_supported_band *sband,
  86. struct ieee80211_channel *channel,
  87. enum ieee80211_smps_mode smps)
  88. {
  89. struct ieee80211_ht_info *ht_info;
  90. u8 *pos;
  91. u32 flags = channel->flags;
  92. u16 cap;
  93. struct ieee80211_sta_ht_cap ht_cap;
  94. BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
  95. if (!sband->ht_cap.ht_supported)
  96. return;
  97. if (!ht_info_ie)
  98. return;
  99. if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
  100. return;
  101. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  102. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  103. ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
  104. /* determine capability flags */
  105. cap = ht_cap.cap;
  106. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  107. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  108. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  109. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  110. cap &= ~IEEE80211_HT_CAP_SGI_40;
  111. }
  112. break;
  113. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  114. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  115. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  116. cap &= ~IEEE80211_HT_CAP_SGI_40;
  117. }
  118. break;
  119. }
  120. /* set SM PS mode properly */
  121. cap &= ~IEEE80211_HT_CAP_SM_PS;
  122. switch (smps) {
  123. case IEEE80211_SMPS_AUTOMATIC:
  124. case IEEE80211_SMPS_NUM_MODES:
  125. WARN_ON(1);
  126. case IEEE80211_SMPS_OFF:
  127. cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
  128. IEEE80211_HT_CAP_SM_PS_SHIFT;
  129. break;
  130. case IEEE80211_SMPS_STATIC:
  131. cap |= WLAN_HT_CAP_SM_PS_STATIC <<
  132. IEEE80211_HT_CAP_SM_PS_SHIFT;
  133. break;
  134. case IEEE80211_SMPS_DYNAMIC:
  135. cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
  136. IEEE80211_HT_CAP_SM_PS_SHIFT;
  137. break;
  138. }
  139. /* reserve and fill IE */
  140. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  141. ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
  142. }
  143. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
  144. struct ieee80211_work *wk)
  145. {
  146. struct ieee80211_local *local = sdata->local;
  147. struct sk_buff *skb;
  148. struct ieee80211_mgmt *mgmt;
  149. u8 *pos, qos_info;
  150. size_t offset = 0, noffset;
  151. int i, count, rates_len, supp_rates_len;
  152. u16 capab;
  153. struct ieee80211_supported_band *sband;
  154. u32 rates = 0;
  155. sband = local->hw.wiphy->bands[wk->chan->band];
  156. if (wk->assoc.supp_rates_len) {
  157. /*
  158. * Get all rates supported by the device and the AP as
  159. * some APs don't like getting a superset of their rates
  160. * in the association request (e.g. D-Link DAP 1353 in
  161. * b-only mode)...
  162. */
  163. rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
  164. wk->assoc.supp_rates_len,
  165. sband, &rates);
  166. } else {
  167. /*
  168. * In case AP not provide any supported rates information
  169. * before association, we send information element(s) with
  170. * all rates that we support.
  171. */
  172. rates = ~0;
  173. rates_len = sband->n_bitrates;
  174. }
  175. skb = alloc_skb(local->hw.extra_tx_headroom +
  176. sizeof(*mgmt) + /* bit too much but doesn't matter */
  177. 2 + wk->assoc.ssid_len + /* SSID */
  178. 4 + rates_len + /* (extended) rates */
  179. 4 + /* power capability */
  180. 2 + 2 * sband->n_channels + /* supported channels */
  181. 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
  182. wk->ie_len + /* extra IEs */
  183. 9, /* WMM */
  184. GFP_KERNEL);
  185. if (!skb)
  186. return;
  187. skb_reserve(skb, local->hw.extra_tx_headroom);
  188. capab = WLAN_CAPABILITY_ESS;
  189. if (sband->band == IEEE80211_BAND_2GHZ) {
  190. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  191. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  192. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  193. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  194. }
  195. if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
  196. capab |= WLAN_CAPABILITY_PRIVACY;
  197. if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  198. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  199. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  200. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  201. memset(mgmt, 0, 24);
  202. memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
  203. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  204. memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
  205. if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
  206. skb_put(skb, 10);
  207. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  208. IEEE80211_STYPE_REASSOC_REQ);
  209. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  210. mgmt->u.reassoc_req.listen_interval =
  211. cpu_to_le16(local->hw.conf.listen_interval);
  212. memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
  213. ETH_ALEN);
  214. } else {
  215. skb_put(skb, 4);
  216. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  217. IEEE80211_STYPE_ASSOC_REQ);
  218. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  219. mgmt->u.assoc_req.listen_interval =
  220. cpu_to_le16(local->hw.conf.listen_interval);
  221. }
  222. /* SSID */
  223. pos = skb_put(skb, 2 + wk->assoc.ssid_len);
  224. *pos++ = WLAN_EID_SSID;
  225. *pos++ = wk->assoc.ssid_len;
  226. memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
  227. /* add all rates which were marked to be used above */
  228. supp_rates_len = rates_len;
  229. if (supp_rates_len > 8)
  230. supp_rates_len = 8;
  231. pos = skb_put(skb, supp_rates_len + 2);
  232. *pos++ = WLAN_EID_SUPP_RATES;
  233. *pos++ = supp_rates_len;
  234. count = 0;
  235. for (i = 0; i < sband->n_bitrates; i++) {
  236. if (BIT(i) & rates) {
  237. int rate = sband->bitrates[i].bitrate;
  238. *pos++ = (u8) (rate / 5);
  239. if (++count == 8)
  240. break;
  241. }
  242. }
  243. if (rates_len > count) {
  244. pos = skb_put(skb, rates_len - count + 2);
  245. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  246. *pos++ = rates_len - count;
  247. for (i++; i < sband->n_bitrates; i++) {
  248. if (BIT(i) & rates) {
  249. int rate = sband->bitrates[i].bitrate;
  250. *pos++ = (u8) (rate / 5);
  251. }
  252. }
  253. }
  254. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  255. /* 1. power capabilities */
  256. pos = skb_put(skb, 4);
  257. *pos++ = WLAN_EID_PWR_CAPABILITY;
  258. *pos++ = 2;
  259. *pos++ = 0; /* min tx power */
  260. *pos++ = wk->chan->max_power; /* max tx power */
  261. /* 2. supported channels */
  262. /* TODO: get this in reg domain format */
  263. pos = skb_put(skb, 2 * sband->n_channels + 2);
  264. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  265. *pos++ = 2 * sband->n_channels;
  266. for (i = 0; i < sband->n_channels; i++) {
  267. *pos++ = ieee80211_frequency_to_channel(
  268. sband->channels[i].center_freq);
  269. *pos++ = 1; /* one channel in the subband*/
  270. }
  271. }
  272. /* if present, add any custom IEs that go before HT */
  273. if (wk->ie_len && wk->ie) {
  274. static const u8 before_ht[] = {
  275. WLAN_EID_SSID,
  276. WLAN_EID_SUPP_RATES,
  277. WLAN_EID_EXT_SUPP_RATES,
  278. WLAN_EID_PWR_CAPABILITY,
  279. WLAN_EID_SUPPORTED_CHANNELS,
  280. WLAN_EID_RSN,
  281. WLAN_EID_QOS_CAPA,
  282. WLAN_EID_RRM_ENABLED_CAPABILITIES,
  283. WLAN_EID_MOBILITY_DOMAIN,
  284. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  285. };
  286. noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
  287. before_ht, ARRAY_SIZE(before_ht),
  288. offset);
  289. pos = skb_put(skb, noffset - offset);
  290. memcpy(pos, wk->ie + offset, noffset - offset);
  291. offset = noffset;
  292. }
  293. if (wk->assoc.use_11n && wk->assoc.wmm_used &&
  294. local->hw.queues >= 4)
  295. ieee80211_add_ht_ie(sdata, skb, wk->assoc.ht_information_ie,
  296. sband, wk->chan, wk->assoc.smps);
  297. /* if present, add any custom non-vendor IEs that go after HT */
  298. if (wk->ie_len && wk->ie) {
  299. noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
  300. offset);
  301. pos = skb_put(skb, noffset - offset);
  302. memcpy(pos, wk->ie + offset, noffset - offset);
  303. offset = noffset;
  304. }
  305. if (wk->assoc.wmm_used && local->hw.queues >= 4) {
  306. if (wk->assoc.uapsd_used) {
  307. qos_info = local->uapsd_queues;
  308. qos_info |= (local->uapsd_max_sp_len <<
  309. IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
  310. } else {
  311. qos_info = 0;
  312. }
  313. pos = skb_put(skb, 9);
  314. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  315. *pos++ = 7; /* len */
  316. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  317. *pos++ = 0x50;
  318. *pos++ = 0xf2;
  319. *pos++ = 2; /* WME */
  320. *pos++ = 0; /* WME info */
  321. *pos++ = 1; /* WME ver */
  322. *pos++ = qos_info;
  323. }
  324. /* add any remaining custom (i.e. vendor specific here) IEs */
  325. if (wk->ie_len && wk->ie) {
  326. noffset = wk->ie_len;
  327. pos = skb_put(skb, noffset - offset);
  328. memcpy(pos, wk->ie + offset, noffset - offset);
  329. }
  330. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  331. ieee80211_tx_skb(sdata, skb);
  332. }
  333. static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
  334. struct ieee80211_work *wk)
  335. {
  336. struct cfg80211_bss *cbss;
  337. u16 capa_val = WLAN_CAPABILITY_ESS;
  338. if (wk->probe_auth.privacy)
  339. capa_val |= WLAN_CAPABILITY_PRIVACY;
  340. cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
  341. wk->probe_auth.ssid, wk->probe_auth.ssid_len,
  342. WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
  343. capa_val);
  344. if (!cbss)
  345. return;
  346. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  347. cfg80211_put_bss(cbss);
  348. }
  349. static enum work_action __must_check
  350. ieee80211_direct_probe(struct ieee80211_work *wk)
  351. {
  352. struct ieee80211_sub_if_data *sdata = wk->sdata;
  353. struct ieee80211_local *local = sdata->local;
  354. if (!wk->probe_auth.synced) {
  355. int ret = drv_tx_sync(local, sdata, wk->filter_ta,
  356. IEEE80211_TX_SYNC_AUTH);
  357. if (ret)
  358. return WORK_ACT_TIMEOUT;
  359. }
  360. wk->probe_auth.synced = true;
  361. wk->probe_auth.tries++;
  362. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  363. printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
  364. sdata->name, wk->filter_ta);
  365. /*
  366. * Most likely AP is not in the range so remove the
  367. * bss struct for that AP.
  368. */
  369. ieee80211_remove_auth_bss(local, wk);
  370. return WORK_ACT_TIMEOUT;
  371. }
  372. printk(KERN_DEBUG "%s: direct probe to %pM (try %d/%i)\n",
  373. sdata->name, wk->filter_ta, wk->probe_auth.tries,
  374. IEEE80211_AUTH_MAX_TRIES);
  375. /*
  376. * Direct probe is sent to broadcast address as some APs
  377. * will not answer to direct packet in unassociated state.
  378. */
  379. ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
  380. wk->probe_auth.ssid_len, NULL, 0,
  381. (u32) -1, true, false);
  382. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  383. run_again(local, wk->timeout);
  384. return WORK_ACT_NONE;
  385. }
  386. static enum work_action __must_check
  387. ieee80211_authenticate(struct ieee80211_work *wk)
  388. {
  389. struct ieee80211_sub_if_data *sdata = wk->sdata;
  390. struct ieee80211_local *local = sdata->local;
  391. if (!wk->probe_auth.synced) {
  392. int ret = drv_tx_sync(local, sdata, wk->filter_ta,
  393. IEEE80211_TX_SYNC_AUTH);
  394. if (ret)
  395. return WORK_ACT_TIMEOUT;
  396. }
  397. wk->probe_auth.synced = true;
  398. wk->probe_auth.tries++;
  399. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  400. printk(KERN_DEBUG "%s: authentication with %pM"
  401. " timed out\n", sdata->name, wk->filter_ta);
  402. /*
  403. * Most likely AP is not in the range so remove the
  404. * bss struct for that AP.
  405. */
  406. ieee80211_remove_auth_bss(local, wk);
  407. return WORK_ACT_TIMEOUT;
  408. }
  409. printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
  410. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  411. ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
  412. wk->ie_len, wk->filter_ta, NULL, 0, 0);
  413. wk->probe_auth.transaction = 2;
  414. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  415. run_again(local, wk->timeout);
  416. return WORK_ACT_NONE;
  417. }
  418. static enum work_action __must_check
  419. ieee80211_associate(struct ieee80211_work *wk)
  420. {
  421. struct ieee80211_sub_if_data *sdata = wk->sdata;
  422. struct ieee80211_local *local = sdata->local;
  423. if (!wk->assoc.synced) {
  424. int ret = drv_tx_sync(local, sdata, wk->filter_ta,
  425. IEEE80211_TX_SYNC_ASSOC);
  426. if (ret)
  427. return WORK_ACT_TIMEOUT;
  428. }
  429. wk->assoc.synced = true;
  430. wk->assoc.tries++;
  431. if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
  432. printk(KERN_DEBUG "%s: association with %pM"
  433. " timed out\n",
  434. sdata->name, wk->filter_ta);
  435. /*
  436. * Most likely AP is not in the range so remove the
  437. * bss struct for that AP.
  438. */
  439. if (wk->assoc.bss)
  440. cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
  441. return WORK_ACT_TIMEOUT;
  442. }
  443. printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
  444. sdata->name, wk->filter_ta, wk->assoc.tries);
  445. ieee80211_send_assoc(sdata, wk);
  446. wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  447. run_again(local, wk->timeout);
  448. return WORK_ACT_NONE;
  449. }
  450. static enum work_action __must_check
  451. ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
  452. {
  453. /*
  454. * First time we run, do nothing -- the generic code will
  455. * have switched to the right channel etc.
  456. */
  457. if (!wk->started) {
  458. wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
  459. cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
  460. wk->chan, wk->chan_type,
  461. wk->remain.duration, GFP_KERNEL);
  462. return WORK_ACT_NONE;
  463. }
  464. return WORK_ACT_TIMEOUT;
  465. }
  466. static enum work_action __must_check
  467. ieee80211_offchannel_tx(struct ieee80211_work *wk)
  468. {
  469. if (!wk->started) {
  470. wk->timeout = jiffies + msecs_to_jiffies(wk->offchan_tx.wait);
  471. /*
  472. * After this, offchan_tx.frame remains but now is no
  473. * longer a valid pointer -- we still need it as the
  474. * cookie for canceling this work/status matching.
  475. */
  476. ieee80211_tx_skb(wk->sdata, wk->offchan_tx.frame);
  477. return WORK_ACT_NONE;
  478. }
  479. return WORK_ACT_TIMEOUT;
  480. }
  481. static enum work_action __must_check
  482. ieee80211_assoc_beacon_wait(struct ieee80211_work *wk)
  483. {
  484. if (wk->started)
  485. return WORK_ACT_TIMEOUT;
  486. /*
  487. * Wait up to one beacon interval ...
  488. * should this be more if we miss one?
  489. */
  490. printk(KERN_DEBUG "%s: waiting for beacon from %pM\n",
  491. wk->sdata->name, wk->filter_ta);
  492. wk->timeout = TU_TO_EXP_TIME(wk->assoc.bss->beacon_interval);
  493. return WORK_ACT_NONE;
  494. }
  495. static void ieee80211_auth_challenge(struct ieee80211_work *wk,
  496. struct ieee80211_mgmt *mgmt,
  497. size_t len)
  498. {
  499. struct ieee80211_sub_if_data *sdata = wk->sdata;
  500. u8 *pos;
  501. struct ieee802_11_elems elems;
  502. pos = mgmt->u.auth.variable;
  503. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  504. if (!elems.challenge)
  505. return;
  506. ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
  507. elems.challenge - 2, elems.challenge_len + 2,
  508. wk->filter_ta, wk->probe_auth.key,
  509. wk->probe_auth.key_len, wk->probe_auth.key_idx);
  510. wk->probe_auth.transaction = 4;
  511. }
  512. static enum work_action __must_check
  513. ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
  514. struct ieee80211_mgmt *mgmt, size_t len)
  515. {
  516. u16 auth_alg, auth_transaction, status_code;
  517. if (wk->type != IEEE80211_WORK_AUTH)
  518. return WORK_ACT_MISMATCH;
  519. if (len < 24 + 6)
  520. return WORK_ACT_NONE;
  521. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  522. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  523. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  524. if (auth_alg != wk->probe_auth.algorithm ||
  525. auth_transaction != wk->probe_auth.transaction)
  526. return WORK_ACT_NONE;
  527. if (status_code != WLAN_STATUS_SUCCESS) {
  528. printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
  529. wk->sdata->name, mgmt->sa, status_code);
  530. return WORK_ACT_DONE;
  531. }
  532. switch (wk->probe_auth.algorithm) {
  533. case WLAN_AUTH_OPEN:
  534. case WLAN_AUTH_LEAP:
  535. case WLAN_AUTH_FT:
  536. break;
  537. case WLAN_AUTH_SHARED_KEY:
  538. if (wk->probe_auth.transaction != 4) {
  539. ieee80211_auth_challenge(wk, mgmt, len);
  540. /* need another frame */
  541. return WORK_ACT_NONE;
  542. }
  543. break;
  544. default:
  545. WARN_ON(1);
  546. return WORK_ACT_NONE;
  547. }
  548. printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
  549. return WORK_ACT_DONE;
  550. }
  551. static enum work_action __must_check
  552. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
  553. struct ieee80211_mgmt *mgmt, size_t len,
  554. bool reassoc)
  555. {
  556. struct ieee80211_sub_if_data *sdata = wk->sdata;
  557. struct ieee80211_local *local = sdata->local;
  558. u16 capab_info, status_code, aid;
  559. struct ieee802_11_elems elems;
  560. u8 *pos;
  561. if (wk->type != IEEE80211_WORK_ASSOC)
  562. return WORK_ACT_MISMATCH;
  563. /*
  564. * AssocResp and ReassocResp have identical structure, so process both
  565. * of them in this function.
  566. */
  567. if (len < 24 + 6)
  568. return WORK_ACT_NONE;
  569. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  570. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  571. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  572. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  573. "status=%d aid=%d)\n",
  574. sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
  575. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  576. pos = mgmt->u.assoc_resp.variable;
  577. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  578. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  579. elems.timeout_int && elems.timeout_int_len == 5 &&
  580. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  581. u32 tu, ms;
  582. tu = get_unaligned_le32(elems.timeout_int + 1);
  583. ms = tu * 1024 / 1000;
  584. printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
  585. "comeback duration %u TU (%u ms)\n",
  586. sdata->name, mgmt->sa, tu, ms);
  587. wk->timeout = jiffies + msecs_to_jiffies(ms);
  588. if (ms > IEEE80211_ASSOC_TIMEOUT)
  589. run_again(local, wk->timeout);
  590. return WORK_ACT_NONE;
  591. }
  592. if (status_code != WLAN_STATUS_SUCCESS)
  593. printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
  594. sdata->name, mgmt->sa, status_code);
  595. else
  596. printk(KERN_DEBUG "%s: associated\n", sdata->name);
  597. return WORK_ACT_DONE;
  598. }
  599. static enum work_action __must_check
  600. ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
  601. struct ieee80211_mgmt *mgmt, size_t len,
  602. struct ieee80211_rx_status *rx_status)
  603. {
  604. struct ieee80211_sub_if_data *sdata = wk->sdata;
  605. struct ieee80211_local *local = sdata->local;
  606. size_t baselen;
  607. ASSERT_WORK_MTX(local);
  608. if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
  609. return WORK_ACT_MISMATCH;
  610. if (len < 24 + 12)
  611. return WORK_ACT_NONE;
  612. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  613. if (baselen > len)
  614. return WORK_ACT_NONE;
  615. printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
  616. return WORK_ACT_DONE;
  617. }
  618. static enum work_action __must_check
  619. ieee80211_rx_mgmt_beacon(struct ieee80211_work *wk,
  620. struct ieee80211_mgmt *mgmt, size_t len)
  621. {
  622. struct ieee80211_sub_if_data *sdata = wk->sdata;
  623. struct ieee80211_local *local = sdata->local;
  624. ASSERT_WORK_MTX(local);
  625. if (wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
  626. return WORK_ACT_MISMATCH;
  627. if (len < 24 + 12)
  628. return WORK_ACT_NONE;
  629. printk(KERN_DEBUG "%s: beacon received\n", sdata->name);
  630. return WORK_ACT_DONE;
  631. }
  632. static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
  633. struct sk_buff *skb)
  634. {
  635. struct ieee80211_rx_status *rx_status;
  636. struct ieee80211_mgmt *mgmt;
  637. struct ieee80211_work *wk;
  638. enum work_action rma = WORK_ACT_NONE;
  639. u16 fc;
  640. rx_status = (struct ieee80211_rx_status *) skb->cb;
  641. mgmt = (struct ieee80211_mgmt *) skb->data;
  642. fc = le16_to_cpu(mgmt->frame_control);
  643. mutex_lock(&local->mtx);
  644. list_for_each_entry(wk, &local->work_list, list) {
  645. const u8 *bssid = NULL;
  646. switch (wk->type) {
  647. case IEEE80211_WORK_DIRECT_PROBE:
  648. case IEEE80211_WORK_AUTH:
  649. case IEEE80211_WORK_ASSOC:
  650. case IEEE80211_WORK_ASSOC_BEACON_WAIT:
  651. bssid = wk->filter_ta;
  652. break;
  653. default:
  654. continue;
  655. }
  656. /*
  657. * Before queuing, we already verified mgmt->sa,
  658. * so this is needed just for matching.
  659. */
  660. if (compare_ether_addr(bssid, mgmt->bssid))
  661. continue;
  662. switch (fc & IEEE80211_FCTL_STYPE) {
  663. case IEEE80211_STYPE_BEACON:
  664. rma = ieee80211_rx_mgmt_beacon(wk, mgmt, skb->len);
  665. break;
  666. case IEEE80211_STYPE_PROBE_RESP:
  667. rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
  668. rx_status);
  669. break;
  670. case IEEE80211_STYPE_AUTH:
  671. rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
  672. break;
  673. case IEEE80211_STYPE_ASSOC_RESP:
  674. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  675. skb->len, false);
  676. break;
  677. case IEEE80211_STYPE_REASSOC_RESP:
  678. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  679. skb->len, true);
  680. break;
  681. default:
  682. WARN_ON(1);
  683. rma = WORK_ACT_NONE;
  684. }
  685. /*
  686. * We've either received an unexpected frame, or we have
  687. * multiple work items and need to match the frame to the
  688. * right one.
  689. */
  690. if (rma == WORK_ACT_MISMATCH)
  691. continue;
  692. /*
  693. * We've processed this frame for that work, so it can't
  694. * belong to another work struct.
  695. * NB: this is also required for correctness for 'rma'!
  696. */
  697. break;
  698. }
  699. switch (rma) {
  700. case WORK_ACT_MISMATCH:
  701. /* ignore this unmatched frame */
  702. break;
  703. case WORK_ACT_NONE:
  704. break;
  705. case WORK_ACT_DONE:
  706. list_del_rcu(&wk->list);
  707. break;
  708. default:
  709. WARN(1, "unexpected: %d", rma);
  710. }
  711. mutex_unlock(&local->mtx);
  712. if (rma != WORK_ACT_DONE)
  713. goto out;
  714. switch (wk->done(wk, skb)) {
  715. case WORK_DONE_DESTROY:
  716. free_work(wk);
  717. break;
  718. case WORK_DONE_REQUEUE:
  719. synchronize_rcu();
  720. wk->started = false; /* restart */
  721. mutex_lock(&local->mtx);
  722. list_add_tail(&wk->list, &local->work_list);
  723. mutex_unlock(&local->mtx);
  724. }
  725. out:
  726. kfree_skb(skb);
  727. }
  728. static bool ieee80211_work_ct_coexists(enum nl80211_channel_type wk_ct,
  729. enum nl80211_channel_type oper_ct)
  730. {
  731. switch (wk_ct) {
  732. case NL80211_CHAN_NO_HT:
  733. return true;
  734. case NL80211_CHAN_HT20:
  735. if (oper_ct != NL80211_CHAN_NO_HT)
  736. return true;
  737. return false;
  738. case NL80211_CHAN_HT40MINUS:
  739. case NL80211_CHAN_HT40PLUS:
  740. return (wk_ct == oper_ct);
  741. }
  742. WARN_ON(1); /* shouldn't get here */
  743. return false;
  744. }
  745. static enum nl80211_channel_type
  746. ieee80211_calc_ct(enum nl80211_channel_type wk_ct,
  747. enum nl80211_channel_type oper_ct)
  748. {
  749. switch (wk_ct) {
  750. case NL80211_CHAN_NO_HT:
  751. return oper_ct;
  752. case NL80211_CHAN_HT20:
  753. if (oper_ct != NL80211_CHAN_NO_HT)
  754. return oper_ct;
  755. return wk_ct;
  756. case NL80211_CHAN_HT40MINUS:
  757. case NL80211_CHAN_HT40PLUS:
  758. return wk_ct;
  759. }
  760. WARN_ON(1); /* shouldn't get here */
  761. return wk_ct;
  762. }
  763. static void ieee80211_work_timer(unsigned long data)
  764. {
  765. struct ieee80211_local *local = (void *) data;
  766. if (local->quiescing)
  767. return;
  768. ieee80211_queue_work(&local->hw, &local->work_work);
  769. }
  770. static void ieee80211_work_work(struct work_struct *work)
  771. {
  772. struct ieee80211_local *local =
  773. container_of(work, struct ieee80211_local, work_work);
  774. struct sk_buff *skb;
  775. struct ieee80211_work *wk, *tmp;
  776. LIST_HEAD(free_work);
  777. enum work_action rma;
  778. bool remain_off_channel = false;
  779. if (local->scanning)
  780. return;
  781. /*
  782. * ieee80211_queue_work() should have picked up most cases,
  783. * here we'll pick the rest.
  784. */
  785. if (WARN(local->suspended, "work scheduled while going to suspend\n"))
  786. return;
  787. /* first process frames to avoid timing out while a frame is pending */
  788. while ((skb = skb_dequeue(&local->work_skb_queue)))
  789. ieee80211_work_rx_queued_mgmt(local, skb);
  790. mutex_lock(&local->mtx);
  791. ieee80211_recalc_idle(local);
  792. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  793. bool started = wk->started;
  794. /* mark work as started if it's on the current off-channel */
  795. if (!started && local->tmp_channel &&
  796. wk->chan == local->tmp_channel &&
  797. wk->chan_type == local->tmp_channel_type) {
  798. started = true;
  799. wk->timeout = jiffies;
  800. }
  801. if (!started && !local->tmp_channel) {
  802. bool on_oper_chan, on_oper_chan2;
  803. enum nl80211_channel_type wk_ct;
  804. on_oper_chan = ieee80211_cfg_on_oper_channel(local);
  805. /* Work with existing channel type if possible. */
  806. wk_ct = wk->chan_type;
  807. if (wk->chan == local->hw.conf.channel)
  808. wk_ct = ieee80211_calc_ct(wk->chan_type,
  809. local->hw.conf.channel_type);
  810. local->tmp_channel = wk->chan;
  811. local->tmp_channel_type = wk_ct;
  812. /*
  813. * Leave the station vifs in awake mode if they
  814. * happen to be on the same channel as
  815. * the requested channel.
  816. */
  817. on_oper_chan2 = ieee80211_cfg_on_oper_channel(local);
  818. if (on_oper_chan != on_oper_chan2) {
  819. if (on_oper_chan2) {
  820. /* going off oper channel, PS too */
  821. ieee80211_offchannel_stop_vifs(local,
  822. true);
  823. ieee80211_hw_config(local, 0);
  824. } else {
  825. /* going on channel, but leave PS
  826. * off-channel. */
  827. ieee80211_hw_config(local, 0);
  828. ieee80211_offchannel_return(local,
  829. true,
  830. false);
  831. }
  832. }
  833. started = true;
  834. wk->timeout = jiffies;
  835. }
  836. /* don't try to work with items that aren't started */
  837. if (!started)
  838. continue;
  839. if (time_is_after_jiffies(wk->timeout)) {
  840. /*
  841. * This work item isn't supposed to be worked on
  842. * right now, but take care to adjust the timer
  843. * properly.
  844. */
  845. run_again(local, wk->timeout);
  846. continue;
  847. }
  848. switch (wk->type) {
  849. default:
  850. WARN_ON(1);
  851. /* nothing */
  852. rma = WORK_ACT_NONE;
  853. break;
  854. case IEEE80211_WORK_ABORT:
  855. rma = WORK_ACT_TIMEOUT;
  856. break;
  857. case IEEE80211_WORK_DIRECT_PROBE:
  858. rma = ieee80211_direct_probe(wk);
  859. break;
  860. case IEEE80211_WORK_AUTH:
  861. rma = ieee80211_authenticate(wk);
  862. break;
  863. case IEEE80211_WORK_ASSOC:
  864. rma = ieee80211_associate(wk);
  865. break;
  866. case IEEE80211_WORK_REMAIN_ON_CHANNEL:
  867. rma = ieee80211_remain_on_channel_timeout(wk);
  868. break;
  869. case IEEE80211_WORK_OFFCHANNEL_TX:
  870. rma = ieee80211_offchannel_tx(wk);
  871. break;
  872. case IEEE80211_WORK_ASSOC_BEACON_WAIT:
  873. rma = ieee80211_assoc_beacon_wait(wk);
  874. break;
  875. }
  876. wk->started = started;
  877. switch (rma) {
  878. case WORK_ACT_NONE:
  879. /* might have changed the timeout */
  880. run_again(local, wk->timeout);
  881. break;
  882. case WORK_ACT_TIMEOUT:
  883. list_del_rcu(&wk->list);
  884. synchronize_rcu();
  885. list_add(&wk->list, &free_work);
  886. break;
  887. default:
  888. WARN(1, "unexpected: %d", rma);
  889. }
  890. }
  891. list_for_each_entry(wk, &local->work_list, list) {
  892. if (!wk->started)
  893. continue;
  894. if (wk->chan != local->tmp_channel)
  895. continue;
  896. if (!ieee80211_work_ct_coexists(wk->chan_type,
  897. local->tmp_channel_type))
  898. continue;
  899. remain_off_channel = true;
  900. }
  901. if (!remain_off_channel && local->tmp_channel) {
  902. local->tmp_channel = NULL;
  903. /* If tmp_channel wasn't operating channel, then
  904. * we need to go back on-channel.
  905. * NOTE: If we can ever be here while scannning,
  906. * or if the hw_config() channel config logic changes,
  907. * then we may need to do a more thorough check to see if
  908. * we still need to do a hardware config. Currently,
  909. * we cannot be here while scanning, however.
  910. */
  911. if (!ieee80211_cfg_on_oper_channel(local))
  912. ieee80211_hw_config(local, 0);
  913. /* At the least, we need to disable offchannel_ps,
  914. * so just go ahead and run the entire offchannel
  915. * return logic here. We *could* skip enabling
  916. * beaconing if we were already on-oper-channel
  917. * as a future optimization.
  918. */
  919. ieee80211_offchannel_return(local, true, true);
  920. /* give connection some time to breathe */
  921. run_again(local, jiffies + HZ/2);
  922. }
  923. if (list_empty(&local->work_list) && local->scan_req &&
  924. !local->scanning)
  925. ieee80211_queue_delayed_work(&local->hw,
  926. &local->scan_work,
  927. round_jiffies_relative(0));
  928. ieee80211_recalc_idle(local);
  929. mutex_unlock(&local->mtx);
  930. list_for_each_entry_safe(wk, tmp, &free_work, list) {
  931. wk->done(wk, NULL);
  932. list_del(&wk->list);
  933. kfree(wk);
  934. }
  935. }
  936. void ieee80211_add_work(struct ieee80211_work *wk)
  937. {
  938. struct ieee80211_local *local;
  939. if (WARN_ON(!wk->chan))
  940. return;
  941. if (WARN_ON(!wk->sdata))
  942. return;
  943. if (WARN_ON(!wk->done))
  944. return;
  945. if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
  946. return;
  947. wk->started = false;
  948. local = wk->sdata->local;
  949. mutex_lock(&local->mtx);
  950. list_add_tail(&wk->list, &local->work_list);
  951. mutex_unlock(&local->mtx);
  952. ieee80211_queue_work(&local->hw, &local->work_work);
  953. }
  954. void ieee80211_work_init(struct ieee80211_local *local)
  955. {
  956. INIT_LIST_HEAD(&local->work_list);
  957. setup_timer(&local->work_timer, ieee80211_work_timer,
  958. (unsigned long)local);
  959. INIT_WORK(&local->work_work, ieee80211_work_work);
  960. skb_queue_head_init(&local->work_skb_queue);
  961. }
  962. void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
  963. {
  964. struct ieee80211_local *local = sdata->local;
  965. struct ieee80211_work *wk;
  966. bool cleanup = false;
  967. mutex_lock(&local->mtx);
  968. list_for_each_entry(wk, &local->work_list, list) {
  969. if (wk->sdata != sdata)
  970. continue;
  971. cleanup = true;
  972. wk->type = IEEE80211_WORK_ABORT;
  973. wk->started = true;
  974. wk->timeout = jiffies;
  975. }
  976. mutex_unlock(&local->mtx);
  977. /* run cleanups etc. */
  978. if (cleanup)
  979. ieee80211_work_work(&local->work_work);
  980. mutex_lock(&local->mtx);
  981. list_for_each_entry(wk, &local->work_list, list) {
  982. if (wk->sdata != sdata)
  983. continue;
  984. WARN_ON(1);
  985. break;
  986. }
  987. mutex_unlock(&local->mtx);
  988. }
  989. ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  990. struct sk_buff *skb)
  991. {
  992. struct ieee80211_local *local = sdata->local;
  993. struct ieee80211_mgmt *mgmt;
  994. struct ieee80211_work *wk;
  995. u16 fc;
  996. if (skb->len < 24)
  997. return RX_DROP_MONITOR;
  998. mgmt = (struct ieee80211_mgmt *) skb->data;
  999. fc = le16_to_cpu(mgmt->frame_control);
  1000. list_for_each_entry_rcu(wk, &local->work_list, list) {
  1001. if (sdata != wk->sdata)
  1002. continue;
  1003. if (compare_ether_addr(wk->filter_ta, mgmt->sa))
  1004. continue;
  1005. if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
  1006. continue;
  1007. switch (fc & IEEE80211_FCTL_STYPE) {
  1008. case IEEE80211_STYPE_AUTH:
  1009. case IEEE80211_STYPE_PROBE_RESP:
  1010. case IEEE80211_STYPE_ASSOC_RESP:
  1011. case IEEE80211_STYPE_REASSOC_RESP:
  1012. case IEEE80211_STYPE_BEACON:
  1013. skb_queue_tail(&local->work_skb_queue, skb);
  1014. ieee80211_queue_work(&local->hw, &local->work_work);
  1015. return RX_QUEUED;
  1016. }
  1017. }
  1018. return RX_CONTINUE;
  1019. }
  1020. static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
  1021. struct sk_buff *skb)
  1022. {
  1023. /*
  1024. * We are done serving the remain-on-channel command.
  1025. */
  1026. cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
  1027. wk->chan, wk->chan_type,
  1028. GFP_KERNEL);
  1029. return WORK_DONE_DESTROY;
  1030. }
  1031. int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  1032. struct ieee80211_channel *chan,
  1033. enum nl80211_channel_type channel_type,
  1034. unsigned int duration, u64 *cookie)
  1035. {
  1036. struct ieee80211_work *wk;
  1037. wk = kzalloc(sizeof(*wk), GFP_KERNEL);
  1038. if (!wk)
  1039. return -ENOMEM;
  1040. wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
  1041. wk->chan = chan;
  1042. wk->chan_type = channel_type;
  1043. wk->sdata = sdata;
  1044. wk->done = ieee80211_remain_done;
  1045. wk->remain.duration = duration;
  1046. *cookie = (unsigned long) wk;
  1047. ieee80211_add_work(wk);
  1048. return 0;
  1049. }
  1050. int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  1051. u64 cookie)
  1052. {
  1053. struct ieee80211_local *local = sdata->local;
  1054. struct ieee80211_work *wk, *tmp;
  1055. bool found = false;
  1056. mutex_lock(&local->mtx);
  1057. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  1058. if ((unsigned long) wk == cookie) {
  1059. wk->timeout = jiffies;
  1060. found = true;
  1061. break;
  1062. }
  1063. }
  1064. mutex_unlock(&local->mtx);
  1065. if (!found)
  1066. return -ENOENT;
  1067. ieee80211_queue_work(&local->hw, &local->work_work);
  1068. return 0;
  1069. }