work.c 30 KB

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