work.c 28 KB

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