work.c 28 KB

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