PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/mac80211/compat_wl12xx/net/mac80211/work.c

https://github.com/MiniCMX/android_hardware_ti_wlan
C | 1206 lines | 890 code | 200 blank | 116 comment | 145 complexity | d332645fef580f22649b0bb9f9f640c0 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  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 5
  29. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  30. #define IEEE80211_ASSOC_MAX_TRIES 5
  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. if (status_code == WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA) {
  531. u32 ms = 200;
  532. printk(KERN_DEBUG "%s: %pM rejected auth; "
  533. "trying again in %u ms\n",
  534. wk->sdata->name, mgmt->sa, ms);
  535. wk->timeout = jiffies + msecs_to_jiffies(ms);
  536. return WORK_ACT_NONE;
  537. }
  538. return WORK_ACT_DONE;
  539. }
  540. switch (wk->probe_auth.algorithm) {
  541. case WLAN_AUTH_OPEN:
  542. case WLAN_AUTH_LEAP:
  543. case WLAN_AUTH_FT:
  544. break;
  545. case WLAN_AUTH_SHARED_KEY:
  546. if (wk->probe_auth.transaction != 4) {
  547. ieee80211_auth_challenge(wk, mgmt, len);
  548. /* need another frame */
  549. return WORK_ACT_NONE;
  550. }
  551. break;
  552. default:
  553. WARN_ON(1);
  554. return WORK_ACT_NONE;
  555. }
  556. printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
  557. return WORK_ACT_DONE;
  558. }
  559. static enum work_action __must_check
  560. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
  561. struct ieee80211_mgmt *mgmt, size_t len,
  562. bool reassoc)
  563. {
  564. struct ieee80211_sub_if_data *sdata = wk->sdata;
  565. struct ieee80211_local *local = sdata->local;
  566. u16 capab_info, status_code, aid;
  567. struct ieee802_11_elems elems;
  568. u8 *pos;
  569. if (wk->type != IEEE80211_WORK_ASSOC)
  570. return WORK_ACT_MISMATCH;
  571. /*
  572. * AssocResp and ReassocResp have identical structure, so process both
  573. * of them in this function.
  574. */
  575. if (len < 24 + 6)
  576. return WORK_ACT_NONE;
  577. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  578. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  579. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  580. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  581. "status=%d aid=%d)\n",
  582. sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
  583. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  584. pos = mgmt->u.assoc_resp.variable;
  585. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  586. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  587. elems.timeout_int && elems.timeout_int_len == 5 &&
  588. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  589. u32 tu, ms;
  590. tu = get_unaligned_le32(elems.timeout_int + 1);
  591. ms = tu * 1024 / 1000;
  592. printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
  593. "comeback duration %u TU (%u ms)\n",
  594. sdata->name, mgmt->sa, tu, ms);
  595. wk->timeout = jiffies + msecs_to_jiffies(ms);
  596. if (ms > IEEE80211_ASSOC_TIMEOUT)
  597. run_again(local, wk->timeout);
  598. return WORK_ACT_NONE;
  599. }
  600. if (status_code == WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA) {
  601. u32 ms = 200;
  602. printk(KERN_DEBUG "%s: %pM rejected association; "
  603. "trying again in %u ms\n",
  604. sdata->name, mgmt->sa, ms);
  605. wk->timeout = jiffies + msecs_to_jiffies(ms);
  606. return WORK_ACT_NONE;
  607. }
  608. if (status_code != WLAN_STATUS_SUCCESS)
  609. printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
  610. sdata->name, mgmt->sa, status_code);
  611. else
  612. printk(KERN_DEBUG "%s: associated\n", sdata->name);
  613. return WORK_ACT_DONE;
  614. }
  615. static enum work_action __must_check
  616. ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
  617. struct ieee80211_mgmt *mgmt, size_t len,
  618. struct ieee80211_rx_status *rx_status)
  619. {
  620. struct ieee80211_sub_if_data *sdata = wk->sdata;
  621. struct ieee80211_local *local = sdata->local;
  622. size_t baselen;
  623. ASSERT_WORK_MTX(local);
  624. if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
  625. return WORK_ACT_MISMATCH;
  626. if (len < 24 + 12)
  627. return WORK_ACT_NONE;
  628. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  629. if (baselen > len)
  630. return WORK_ACT_NONE;
  631. printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
  632. return WORK_ACT_DONE;
  633. }
  634. static enum work_action __must_check
  635. ieee80211_rx_mgmt_beacon(struct ieee80211_work *wk,
  636. struct ieee80211_mgmt *mgmt, size_t len)
  637. {
  638. struct ieee80211_sub_if_data *sdata = wk->sdata;
  639. struct ieee80211_local *local = sdata->local;
  640. ASSERT_WORK_MTX(local);
  641. if (wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
  642. return WORK_ACT_MISMATCH;
  643. if (len < 24 + 12)
  644. return WORK_ACT_NONE;
  645. printk(KERN_DEBUG "%s: beacon received\n", sdata->name);
  646. return WORK_ACT_DONE;
  647. }
  648. static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
  649. struct sk_buff *skb)
  650. {
  651. struct ieee80211_rx_status *rx_status;
  652. struct ieee80211_mgmt *mgmt;
  653. struct ieee80211_work *wk;
  654. enum work_action rma = WORK_ACT_NONE;
  655. u16 fc;
  656. rx_status = (struct ieee80211_rx_status *) skb->cb;
  657. mgmt = (struct ieee80211_mgmt *) skb->data;
  658. fc = le16_to_cpu(mgmt->frame_control);
  659. mutex_lock(&local->mtx);
  660. list_for_each_entry(wk, &local->work_list, list) {
  661. const u8 *bssid = NULL;
  662. switch (wk->type) {
  663. case IEEE80211_WORK_DIRECT_PROBE:
  664. case IEEE80211_WORK_AUTH:
  665. case IEEE80211_WORK_ASSOC:
  666. case IEEE80211_WORK_ASSOC_BEACON_WAIT:
  667. bssid = wk->filter_ta;
  668. break;
  669. default:
  670. continue;
  671. }
  672. /*
  673. * Before queuing, we already verified mgmt->sa,
  674. * so this is needed just for matching.
  675. */
  676. if (compare_ether_addr(bssid, mgmt->bssid))
  677. continue;
  678. switch (fc & IEEE80211_FCTL_STYPE) {
  679. case IEEE80211_STYPE_BEACON:
  680. rma = ieee80211_rx_mgmt_beacon(wk, mgmt, skb->len);
  681. break;
  682. case IEEE80211_STYPE_PROBE_RESP:
  683. rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
  684. rx_status);
  685. break;
  686. case IEEE80211_STYPE_AUTH:
  687. rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
  688. break;
  689. case IEEE80211_STYPE_ASSOC_RESP:
  690. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  691. skb->len, false);
  692. break;
  693. case IEEE80211_STYPE_REASSOC_RESP:
  694. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  695. skb->len, true);
  696. break;
  697. default:
  698. WARN_ON(1);
  699. rma = WORK_ACT_NONE;
  700. }
  701. /*
  702. * We've either received an unexpected frame, or we have
  703. * multiple work items and need to match the frame to the
  704. * right one.
  705. */
  706. if (rma == WORK_ACT_MISMATCH)
  707. continue;
  708. /*
  709. * We've processed this frame for that work, so it can't
  710. * belong to another work struct.
  711. * NB: this is also required for correctness for 'rma'!
  712. */
  713. break;
  714. }
  715. switch (rma) {
  716. case WORK_ACT_MISMATCH:
  717. /* ignore this unmatched frame */
  718. break;
  719. case WORK_ACT_NONE:
  720. break;
  721. case WORK_ACT_DONE:
  722. list_del_rcu(&wk->list);
  723. break;
  724. default:
  725. WARN(1, "unexpected: %d", rma);
  726. }
  727. mutex_unlock(&local->mtx);
  728. if (rma != WORK_ACT_DONE)
  729. goto out;
  730. switch (wk->done(wk, skb)) {
  731. case WORK_DONE_DESTROY:
  732. free_work(wk);
  733. break;
  734. case WORK_DONE_REQUEUE:
  735. synchronize_rcu();
  736. wk->started = false; /* restart */
  737. mutex_lock(&local->mtx);
  738. list_add_tail(&wk->list, &local->work_list);
  739. mutex_unlock(&local->mtx);
  740. }
  741. out:
  742. kfree_skb(skb);
  743. }
  744. static void ieee80211_work_timer(unsigned long data)
  745. {
  746. struct ieee80211_local *local = (void *) data;
  747. if (local->quiescing)
  748. return;
  749. ieee80211_queue_work(&local->hw, &local->work_work);
  750. }
  751. static void ieee80211_work_work(struct work_struct *work)
  752. {
  753. struct ieee80211_local *local =
  754. container_of(work, struct ieee80211_local, work_work);
  755. struct sk_buff *skb;
  756. struct ieee80211_work *wk, *tmp;
  757. LIST_HEAD(free_work);
  758. enum work_action rma;
  759. bool remain_off_channel = false;
  760. if (local->scanning)
  761. return;
  762. /*
  763. * ieee80211_queue_work() should have picked up most cases,
  764. * here we'll pick the rest.
  765. */
  766. if (WARN(local->suspended, "work scheduled while going to suspend\n"))
  767. return;
  768. /* first process frames to avoid timing out while a frame is pending */
  769. while ((skb = skb_dequeue(&local->work_skb_queue)))
  770. ieee80211_work_rx_queued_mgmt(local, skb);
  771. mutex_lock(&local->mtx);
  772. ieee80211_recalc_idle(local);
  773. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  774. bool started = wk->started;
  775. /* mark work as started if it's on the current off-channel */
  776. if (!started && local->tmp_channel &&
  777. wk->chan == local->tmp_channel &&
  778. wk->chan_type == local->tmp_channel_type) {
  779. started = true;
  780. wk->timeout = jiffies;
  781. }
  782. if (!started && !local->tmp_channel) {
  783. ieee80211_offchannel_stop_vifs(local, true);
  784. local->tmp_channel = wk->chan;
  785. local->tmp_channel_type = wk->chan_type;
  786. ieee80211_hw_config(local, 0);
  787. started = true;
  788. wk->timeout = jiffies;
  789. }
  790. /* don't try to work with items that aren't started */
  791. if (!started)
  792. continue;
  793. if (time_is_after_jiffies(wk->timeout)) {
  794. /*
  795. * This work item isn't supposed to be worked on
  796. * right now, but take care to adjust the timer
  797. * properly.
  798. */
  799. run_again(local, wk->timeout);
  800. continue;
  801. }
  802. switch (wk->type) {
  803. default:
  804. WARN_ON(1);
  805. /* nothing */
  806. rma = WORK_ACT_NONE;
  807. break;
  808. case IEEE80211_WORK_ABORT:
  809. rma = WORK_ACT_TIMEOUT;
  810. break;
  811. case IEEE80211_WORK_DIRECT_PROBE:
  812. rma = ieee80211_direct_probe(wk);
  813. break;
  814. case IEEE80211_WORK_AUTH:
  815. rma = ieee80211_authenticate(wk);
  816. break;
  817. case IEEE80211_WORK_ASSOC:
  818. rma = ieee80211_associate(wk);
  819. break;
  820. case IEEE80211_WORK_REMAIN_ON_CHANNEL:
  821. rma = ieee80211_remain_on_channel_timeout(wk);
  822. break;
  823. case IEEE80211_WORK_OFFCHANNEL_TX:
  824. rma = ieee80211_offchannel_tx(wk);
  825. break;
  826. case IEEE80211_WORK_ASSOC_BEACON_WAIT:
  827. rma = ieee80211_assoc_beacon_wait(wk);
  828. break;
  829. }
  830. wk->started = started;
  831. switch (rma) {
  832. case WORK_ACT_NONE:
  833. /* might have changed the timeout */
  834. run_again(local, wk->timeout);
  835. break;
  836. case WORK_ACT_TIMEOUT:
  837. list_del_rcu(&wk->list);
  838. synchronize_rcu();
  839. list_add(&wk->list, &free_work);
  840. break;
  841. default:
  842. WARN(1, "unexpected: %d", rma);
  843. }
  844. }
  845. list_for_each_entry(wk, &local->work_list, list) {
  846. if (!wk->started)
  847. continue;
  848. if (wk->chan != local->tmp_channel ||
  849. wk->chan_type != local->tmp_channel_type)
  850. continue;
  851. remain_off_channel = true;
  852. }
  853. if (!remain_off_channel && local->tmp_channel) {
  854. local->tmp_channel = NULL;
  855. ieee80211_hw_config(local, 0);
  856. ieee80211_offchannel_return(local, true);
  857. /* give connection some time to breathe */
  858. run_again(local, jiffies + HZ/2);
  859. }
  860. if (list_empty(&local->work_list) && local->scan_req &&
  861. !local->scanning)
  862. ieee80211_queue_delayed_work(&local->hw,
  863. &local->scan_work,
  864. round_jiffies_relative(0));
  865. ieee80211_recalc_idle(local);
  866. mutex_unlock(&local->mtx);
  867. list_for_each_entry_safe(wk, tmp, &free_work, list) {
  868. wk->done(wk, NULL);
  869. list_del(&wk->list);
  870. kfree(wk);
  871. }
  872. }
  873. void ieee80211_add_work(struct ieee80211_work *wk)
  874. {
  875. struct ieee80211_local *local;
  876. if (WARN_ON(!wk->chan))
  877. return;
  878. if (WARN_ON(!wk->sdata))
  879. return;
  880. if (WARN_ON(!wk->done))
  881. return;
  882. if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
  883. return;
  884. wk->started = false;
  885. local = wk->sdata->local;
  886. mutex_lock(&local->mtx);
  887. list_add_tail(&wk->list, &local->work_list);
  888. mutex_unlock(&local->mtx);
  889. ieee80211_queue_work(&local->hw, &local->work_work);
  890. }
  891. void ieee80211_work_init(struct ieee80211_local *local)
  892. {
  893. INIT_LIST_HEAD(&local->work_list);
  894. setup_timer(&local->work_timer, ieee80211_work_timer,
  895. (unsigned long)local);
  896. INIT_WORK(&local->work_work, ieee80211_work_work);
  897. skb_queue_head_init(&local->work_skb_queue);
  898. }
  899. void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
  900. {
  901. struct ieee80211_local *local = sdata->local;
  902. struct ieee80211_work *wk;
  903. bool cleanup = false;
  904. mutex_lock(&local->mtx);
  905. list_for_each_entry(wk, &local->work_list, list) {
  906. if (wk->sdata != sdata)
  907. continue;
  908. cleanup = true;
  909. wk->type = IEEE80211_WORK_ABORT;
  910. wk->started = true;
  911. wk->timeout = jiffies;
  912. }
  913. mutex_unlock(&local->mtx);
  914. /* run cleanups etc. */
  915. if (cleanup)
  916. ieee80211_work_work(&local->work_work);
  917. mutex_lock(&local->mtx);
  918. list_for_each_entry(wk, &local->work_list, list) {
  919. if (wk->sdata != sdata)
  920. continue;
  921. WARN_ON(1);
  922. break;
  923. }
  924. mutex_unlock(&local->mtx);
  925. }
  926. ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  927. struct sk_buff *skb)
  928. {
  929. struct ieee80211_local *local = sdata->local;
  930. struct ieee80211_mgmt *mgmt;
  931. struct ieee80211_work *wk;
  932. u16 fc;
  933. if (skb->len < 24)
  934. return RX_DROP_MONITOR;
  935. mgmt = (struct ieee80211_mgmt *) skb->data;
  936. fc = le16_to_cpu(mgmt->frame_control);
  937. list_for_each_entry_rcu(wk, &local->work_list, list) {
  938. if (sdata != wk->sdata)
  939. continue;
  940. if (compare_ether_addr(wk->filter_ta, mgmt->sa))
  941. continue;
  942. if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
  943. continue;
  944. switch (fc & IEEE80211_FCTL_STYPE) {
  945. case IEEE80211_STYPE_AUTH:
  946. case IEEE80211_STYPE_PROBE_RESP:
  947. case IEEE80211_STYPE_ASSOC_RESP:
  948. case IEEE80211_STYPE_REASSOC_RESP:
  949. case IEEE80211_STYPE_BEACON:
  950. skb_queue_tail(&local->work_skb_queue, skb);
  951. ieee80211_queue_work(&local->hw, &local->work_work);
  952. return RX_QUEUED;
  953. }
  954. }
  955. return RX_CONTINUE;
  956. }
  957. static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
  958. struct sk_buff *skb)
  959. {
  960. /*
  961. * We are done serving the remain-on-channel command.
  962. */
  963. cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
  964. wk->chan, wk->chan_type,
  965. GFP_KERNEL);
  966. return WORK_DONE_DESTROY;
  967. }
  968. int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  969. struct ieee80211_channel *chan,
  970. enum nl80211_channel_type channel_type,
  971. unsigned int duration, u64 *cookie)
  972. {
  973. struct ieee80211_work *wk;
  974. wk = kzalloc(sizeof(*wk), GFP_KERNEL);
  975. if (!wk)
  976. return -ENOMEM;
  977. wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
  978. wk->chan = chan;
  979. wk->chan_type = channel_type;
  980. wk->sdata = sdata;
  981. wk->done = ieee80211_remain_done;
  982. wk->remain.duration = duration;
  983. *cookie = (unsigned long) wk;
  984. ieee80211_add_work(wk);
  985. return 0;
  986. }
  987. int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  988. u64 cookie)
  989. {
  990. struct ieee80211_local *local = sdata->local;
  991. struct ieee80211_work *wk, *tmp;
  992. bool found = false;
  993. mutex_lock(&local->mtx);
  994. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  995. if ((unsigned long) wk == cookie) {
  996. wk->timeout = jiffies;
  997. found = true;
  998. break;
  999. }
  1000. }
  1001. mutex_unlock(&local->mtx);
  1002. if (!found)
  1003. return -ENOENT;
  1004. ieee80211_queue_work(&local->hw, &local->work_work);
  1005. return 0;
  1006. }