work.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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_send_assoc(struct ieee80211_sub_if_data *sdata,
  88. struct ieee80211_work *wk)
  89. {
  90. struct ieee80211_local *local = sdata->local;
  91. struct sk_buff *skb;
  92. struct ieee80211_mgmt *mgmt;
  93. u8 *pos;
  94. const u8 *ies, *ht_ie;
  95. int i, len, count, rates_len, supp_rates_len;
  96. u16 capab;
  97. struct ieee80211_supported_band *sband;
  98. u32 rates = 0;
  99. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  100. sizeof(*mgmt) + 200 + wk->ie_len +
  101. wk->assoc.ssid_len);
  102. if (!skb) {
  103. printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
  104. "frame\n", sdata->name);
  105. return;
  106. }
  107. skb_reserve(skb, local->hw.extra_tx_headroom);
  108. sband = local->hw.wiphy->bands[wk->chan->band];
  109. capab = WLAN_CAPABILITY_ESS;
  110. if (sband->band == IEEE80211_BAND_2GHZ) {
  111. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  112. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  113. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  114. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  115. }
  116. if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
  117. capab |= WLAN_CAPABILITY_PRIVACY;
  118. /*
  119. * Get all rates supported by the device and the AP as
  120. * some APs don't like getting a superset of their rates
  121. * in the association request (e.g. D-Link DAP 1353 in
  122. * b-only mode)...
  123. */
  124. rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
  125. wk->assoc.supp_rates_len,
  126. sband, &rates);
  127. if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  128. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  129. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  130. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  131. memset(mgmt, 0, 24);
  132. memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
  133. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  134. memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
  135. if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
  136. skb_put(skb, 10);
  137. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  138. IEEE80211_STYPE_REASSOC_REQ);
  139. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  140. mgmt->u.reassoc_req.listen_interval =
  141. cpu_to_le16(local->hw.conf.listen_interval);
  142. memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
  143. ETH_ALEN);
  144. } else {
  145. skb_put(skb, 4);
  146. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  147. IEEE80211_STYPE_ASSOC_REQ);
  148. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  149. mgmt->u.assoc_req.listen_interval =
  150. cpu_to_le16(local->hw.conf.listen_interval);
  151. }
  152. /* SSID */
  153. ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
  154. *pos++ = WLAN_EID_SSID;
  155. *pos++ = wk->assoc.ssid_len;
  156. memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
  157. /* add all rates which were marked to be used above */
  158. supp_rates_len = rates_len;
  159. if (supp_rates_len > 8)
  160. supp_rates_len = 8;
  161. len = sband->n_bitrates;
  162. pos = skb_put(skb, supp_rates_len + 2);
  163. *pos++ = WLAN_EID_SUPP_RATES;
  164. *pos++ = supp_rates_len;
  165. count = 0;
  166. for (i = 0; i < sband->n_bitrates; i++) {
  167. if (BIT(i) & rates) {
  168. int rate = sband->bitrates[i].bitrate;
  169. *pos++ = (u8) (rate / 5);
  170. if (++count == 8)
  171. break;
  172. }
  173. }
  174. if (rates_len > count) {
  175. pos = skb_put(skb, rates_len - count + 2);
  176. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  177. *pos++ = rates_len - count;
  178. for (i++; i < sband->n_bitrates; i++) {
  179. if (BIT(i) & rates) {
  180. int rate = sband->bitrates[i].bitrate;
  181. *pos++ = (u8) (rate / 5);
  182. }
  183. }
  184. }
  185. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  186. /* 1. power capabilities */
  187. pos = skb_put(skb, 4);
  188. *pos++ = WLAN_EID_PWR_CAPABILITY;
  189. *pos++ = 2;
  190. *pos++ = 0; /* min tx power */
  191. *pos++ = local->hw.conf.channel->max_power; /* max tx power */
  192. /* 2. supported channels */
  193. /* TODO: get this in reg domain format */
  194. pos = skb_put(skb, 2 * sband->n_channels + 2);
  195. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  196. *pos++ = 2 * sband->n_channels;
  197. for (i = 0; i < sband->n_channels; i++) {
  198. *pos++ = ieee80211_frequency_to_channel(
  199. sband->channels[i].center_freq);
  200. *pos++ = 1; /* one channel in the subband*/
  201. }
  202. }
  203. if (wk->ie_len && wk->ie) {
  204. pos = skb_put(skb, wk->ie_len);
  205. memcpy(pos, wk->ie, wk->ie_len);
  206. }
  207. if (wk->assoc.wmm_used && local->hw.queues >= 4) {
  208. pos = skb_put(skb, 9);
  209. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  210. *pos++ = 7; /* len */
  211. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  212. *pos++ = 0x50;
  213. *pos++ = 0xf2;
  214. *pos++ = 2; /* WME */
  215. *pos++ = 0; /* WME info */
  216. *pos++ = 1; /* WME ver */
  217. *pos++ = 0;
  218. }
  219. /* wmm support is a must to HT */
  220. /*
  221. * IEEE802.11n does not allow TKIP/WEP as pairwise
  222. * ciphers in HT mode. We still associate in non-ht
  223. * mode (11a/b/g) if any one of these ciphers is
  224. * configured as pairwise.
  225. */
  226. if (wk->assoc.use_11n && wk->assoc.wmm_used &&
  227. (local->hw.queues >= 4) &&
  228. sband->ht_cap.ht_supported &&
  229. (ht_ie = wk->assoc.ht_information_ie) &&
  230. ht_ie[1] >= sizeof(struct ieee80211_ht_info)) {
  231. struct ieee80211_ht_info *ht_info =
  232. (struct ieee80211_ht_info *)(ht_ie + 2);
  233. u16 cap = sband->ht_cap.cap;
  234. __le16 tmp;
  235. u32 flags = local->hw.conf.channel->flags;
  236. /* determine capability flags */
  237. if (ieee80211_disable_40mhz_24ghz &&
  238. sband->band == IEEE80211_BAND_2GHZ) {
  239. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  240. cap &= ~IEEE80211_HT_CAP_SGI_40;
  241. }
  242. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  243. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  244. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  245. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  246. cap &= ~IEEE80211_HT_CAP_SGI_40;
  247. }
  248. break;
  249. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  250. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  251. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  252. cap &= ~IEEE80211_HT_CAP_SGI_40;
  253. }
  254. break;
  255. }
  256. /* set SM PS mode properly */
  257. cap &= ~IEEE80211_HT_CAP_SM_PS;
  258. switch (wk->assoc.smps) {
  259. case IEEE80211_SMPS_AUTOMATIC:
  260. case IEEE80211_SMPS_NUM_MODES:
  261. WARN_ON(1);
  262. case IEEE80211_SMPS_OFF:
  263. cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
  264. IEEE80211_HT_CAP_SM_PS_SHIFT;
  265. break;
  266. case IEEE80211_SMPS_STATIC:
  267. cap |= WLAN_HT_CAP_SM_PS_STATIC <<
  268. IEEE80211_HT_CAP_SM_PS_SHIFT;
  269. break;
  270. case IEEE80211_SMPS_DYNAMIC:
  271. cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
  272. IEEE80211_HT_CAP_SM_PS_SHIFT;
  273. break;
  274. }
  275. /* reserve and fill IE */
  276. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  277. *pos++ = WLAN_EID_HT_CAPABILITY;
  278. *pos++ = sizeof(struct ieee80211_ht_cap);
  279. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  280. /* capability flags */
  281. tmp = cpu_to_le16(cap);
  282. memcpy(pos, &tmp, sizeof(u16));
  283. pos += sizeof(u16);
  284. /* AMPDU parameters */
  285. *pos++ = sband->ht_cap.ampdu_factor |
  286. (sband->ht_cap.ampdu_density <<
  287. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  288. /* MCS set */
  289. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  290. pos += sizeof(sband->ht_cap.mcs);
  291. /* extended capabilities */
  292. pos += sizeof(__le16);
  293. /* BF capabilities */
  294. pos += sizeof(__le32);
  295. /* antenna selection */
  296. pos += sizeof(u8);
  297. }
  298. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  299. ieee80211_tx_skb(sdata, skb);
  300. }
  301. static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
  302. struct ieee80211_work *wk)
  303. {
  304. struct cfg80211_bss *cbss;
  305. u16 capa_val = WLAN_CAPABILITY_ESS;
  306. if (wk->probe_auth.privacy)
  307. capa_val |= WLAN_CAPABILITY_PRIVACY;
  308. cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
  309. wk->probe_auth.ssid, wk->probe_auth.ssid_len,
  310. WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
  311. capa_val);
  312. if (!cbss)
  313. return;
  314. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  315. cfg80211_put_bss(cbss);
  316. }
  317. static enum work_action __must_check
  318. ieee80211_direct_probe(struct ieee80211_work *wk)
  319. {
  320. struct ieee80211_sub_if_data *sdata = wk->sdata;
  321. struct ieee80211_local *local = sdata->local;
  322. wk->probe_auth.tries++;
  323. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  324. printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
  325. sdata->name, wk->filter_ta);
  326. /*
  327. * Most likely AP is not in the range so remove the
  328. * bss struct for that AP.
  329. */
  330. ieee80211_remove_auth_bss(local, wk);
  331. /*
  332. * We might have a pending scan which had no chance to run yet
  333. * due to work needing to be done. Hence, queue the STAs work
  334. * again for that.
  335. */
  336. ieee80211_queue_work(&local->hw, &local->work_work);
  337. return WORK_ACT_TIMEOUT;
  338. }
  339. printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
  340. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  341. /*
  342. * Direct probe is sent to broadcast address as some APs
  343. * will not answer to direct packet in unassociated state.
  344. */
  345. ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
  346. wk->probe_auth.ssid_len, NULL, 0);
  347. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  348. run_again(local, wk->timeout);
  349. return WORK_ACT_NONE;
  350. }
  351. static enum work_action __must_check
  352. ieee80211_authenticate(struct ieee80211_work *wk)
  353. {
  354. struct ieee80211_sub_if_data *sdata = wk->sdata;
  355. struct ieee80211_local *local = sdata->local;
  356. wk->probe_auth.tries++;
  357. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  358. printk(KERN_DEBUG "%s: authentication with %pM"
  359. " timed out\n", sdata->name, wk->filter_ta);
  360. /*
  361. * Most likely AP is not in the range so remove the
  362. * bss struct for that AP.
  363. */
  364. ieee80211_remove_auth_bss(local, wk);
  365. /*
  366. * We might have a pending scan which had no chance to run yet
  367. * due to work needing to be done. Hence, queue the STAs work
  368. * again for that.
  369. */
  370. ieee80211_queue_work(&local->hw, &local->work_work);
  371. return WORK_ACT_TIMEOUT;
  372. }
  373. printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
  374. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  375. ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
  376. wk->ie_len, wk->filter_ta, NULL, 0, 0);
  377. wk->probe_auth.transaction = 2;
  378. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  379. run_again(local, wk->timeout);
  380. return WORK_ACT_NONE;
  381. }
  382. static enum work_action __must_check
  383. ieee80211_associate(struct ieee80211_work *wk)
  384. {
  385. struct ieee80211_sub_if_data *sdata = wk->sdata;
  386. struct ieee80211_local *local = sdata->local;
  387. wk->assoc.tries++;
  388. if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
  389. printk(KERN_DEBUG "%s: association with %pM"
  390. " timed out\n",
  391. sdata->name, wk->filter_ta);
  392. /*
  393. * Most likely AP is not in the range so remove the
  394. * bss struct for that AP.
  395. */
  396. if (wk->assoc.bss)
  397. cfg80211_unlink_bss(local->hw.wiphy,
  398. &wk->assoc.bss->cbss);
  399. /*
  400. * We might have a pending scan which had no chance to run yet
  401. * due to work needing to be done. Hence, queue the STAs work
  402. * again for that.
  403. */
  404. ieee80211_queue_work(&local->hw, &local->work_work);
  405. return WORK_ACT_TIMEOUT;
  406. }
  407. printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
  408. sdata->name, wk->filter_ta, wk->assoc.tries);
  409. ieee80211_send_assoc(sdata, wk);
  410. wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  411. run_again(local, wk->timeout);
  412. return WORK_ACT_NONE;
  413. }
  414. static void ieee80211_auth_challenge(struct ieee80211_work *wk,
  415. struct ieee80211_mgmt *mgmt,
  416. size_t len)
  417. {
  418. struct ieee80211_sub_if_data *sdata = wk->sdata;
  419. u8 *pos;
  420. struct ieee802_11_elems elems;
  421. pos = mgmt->u.auth.variable;
  422. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  423. if (!elems.challenge)
  424. return;
  425. ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
  426. elems.challenge - 2, elems.challenge_len + 2,
  427. wk->filter_ta, wk->probe_auth.key,
  428. wk->probe_auth.key_len, wk->probe_auth.key_idx);
  429. wk->probe_auth.transaction = 4;
  430. }
  431. static enum work_action __must_check
  432. ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
  433. struct ieee80211_mgmt *mgmt, size_t len)
  434. {
  435. u16 auth_alg, auth_transaction, status_code;
  436. if (wk->type != IEEE80211_WORK_AUTH)
  437. return WORK_ACT_NONE;
  438. if (len < 24 + 6)
  439. return WORK_ACT_NONE;
  440. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  441. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  442. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  443. if (auth_alg != wk->probe_auth.algorithm ||
  444. auth_transaction != wk->probe_auth.transaction)
  445. return WORK_ACT_NONE;
  446. if (status_code != WLAN_STATUS_SUCCESS) {
  447. printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
  448. wk->sdata->name, mgmt->sa, status_code);
  449. return WORK_ACT_DONE;
  450. }
  451. switch (wk->probe_auth.algorithm) {
  452. case WLAN_AUTH_OPEN:
  453. case WLAN_AUTH_LEAP:
  454. case WLAN_AUTH_FT:
  455. break;
  456. case WLAN_AUTH_SHARED_KEY:
  457. if (wk->probe_auth.transaction != 4) {
  458. ieee80211_auth_challenge(wk, mgmt, len);
  459. /* need another frame */
  460. return WORK_ACT_NONE;
  461. }
  462. break;
  463. default:
  464. WARN_ON(1);
  465. return WORK_ACT_NONE;
  466. }
  467. printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
  468. return WORK_ACT_DONE;
  469. }
  470. static enum work_action __must_check
  471. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
  472. struct ieee80211_mgmt *mgmt, size_t len,
  473. bool reassoc)
  474. {
  475. struct ieee80211_sub_if_data *sdata = wk->sdata;
  476. struct ieee80211_local *local = sdata->local;
  477. u16 capab_info, status_code, aid;
  478. struct ieee802_11_elems elems;
  479. u8 *pos;
  480. /*
  481. * AssocResp and ReassocResp have identical structure, so process both
  482. * of them in this function.
  483. */
  484. if (len < 24 + 6)
  485. return WORK_ACT_NONE;
  486. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  487. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  488. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  489. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  490. "status=%d aid=%d)\n",
  491. sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
  492. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  493. pos = mgmt->u.assoc_resp.variable;
  494. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  495. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  496. elems.timeout_int && elems.timeout_int_len == 5 &&
  497. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  498. u32 tu, ms;
  499. tu = get_unaligned_le32(elems.timeout_int + 1);
  500. ms = tu * 1024 / 1000;
  501. printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
  502. "comeback duration %u TU (%u ms)\n",
  503. sdata->name, mgmt->sa, tu, ms);
  504. wk->timeout = jiffies + msecs_to_jiffies(ms);
  505. if (ms > IEEE80211_ASSOC_TIMEOUT)
  506. run_again(local, wk->timeout);
  507. return WORK_ACT_NONE;
  508. }
  509. if (status_code != WLAN_STATUS_SUCCESS)
  510. printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
  511. sdata->name, mgmt->sa, status_code);
  512. else
  513. printk(KERN_DEBUG "%s: associated\n", sdata->name);
  514. return WORK_ACT_DONE;
  515. }
  516. static enum work_action __must_check
  517. ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
  518. struct ieee80211_mgmt *mgmt, size_t len,
  519. struct ieee80211_rx_status *rx_status)
  520. {
  521. struct ieee80211_sub_if_data *sdata = wk->sdata;
  522. struct ieee80211_local *local = sdata->local;
  523. size_t baselen;
  524. ASSERT_WORK_MTX(local);
  525. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  526. if (baselen > len)
  527. return WORK_ACT_NONE;
  528. printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
  529. return WORK_ACT_DONE;
  530. }
  531. static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
  532. struct sk_buff *skb)
  533. {
  534. struct ieee80211_rx_status *rx_status;
  535. struct ieee80211_mgmt *mgmt;
  536. struct ieee80211_work *wk;
  537. enum work_action rma = WORK_ACT_NONE;
  538. u16 fc;
  539. rx_status = (struct ieee80211_rx_status *) skb->cb;
  540. mgmt = (struct ieee80211_mgmt *) skb->data;
  541. fc = le16_to_cpu(mgmt->frame_control);
  542. mutex_lock(&local->work_mtx);
  543. list_for_each_entry(wk, &local->work_list, list) {
  544. const u8 *bssid = NULL;
  545. switch (wk->type) {
  546. case IEEE80211_WORK_DIRECT_PROBE:
  547. case IEEE80211_WORK_AUTH:
  548. case IEEE80211_WORK_ASSOC:
  549. bssid = wk->filter_ta;
  550. break;
  551. default:
  552. continue;
  553. }
  554. /*
  555. * Before queuing, we already verified mgmt->sa,
  556. * so this is needed just for matching.
  557. */
  558. if (compare_ether_addr(bssid, mgmt->bssid))
  559. continue;
  560. switch (fc & IEEE80211_FCTL_STYPE) {
  561. case IEEE80211_STYPE_PROBE_RESP:
  562. rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
  563. rx_status);
  564. break;
  565. case IEEE80211_STYPE_AUTH:
  566. rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
  567. break;
  568. case IEEE80211_STYPE_ASSOC_RESP:
  569. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  570. skb->len, false);
  571. break;
  572. case IEEE80211_STYPE_REASSOC_RESP:
  573. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  574. skb->len, true);
  575. break;
  576. default:
  577. WARN_ON(1);
  578. }
  579. /*
  580. * We've processed this frame for that work, so it can't
  581. * belong to another work struct.
  582. * NB: this is also required for correctness for 'rma'!
  583. */
  584. break;
  585. }
  586. switch (rma) {
  587. case WORK_ACT_NONE:
  588. break;
  589. case WORK_ACT_DONE:
  590. list_del_rcu(&wk->list);
  591. break;
  592. default:
  593. WARN(1, "unexpected: %d", rma);
  594. }
  595. mutex_unlock(&local->work_mtx);
  596. if (rma != WORK_ACT_DONE)
  597. goto out;
  598. switch (wk->done(wk, skb)) {
  599. case WORK_DONE_DESTROY:
  600. free_work(wk);
  601. break;
  602. case WORK_DONE_REQUEUE:
  603. synchronize_rcu();
  604. wk->timeout = jiffies; /* run again directly */
  605. mutex_lock(&local->work_mtx);
  606. list_add_tail(&wk->list, &local->work_list);
  607. mutex_unlock(&local->work_mtx);
  608. }
  609. out:
  610. kfree_skb(skb);
  611. }
  612. static void ieee80211_work_timer(unsigned long data)
  613. {
  614. struct ieee80211_local *local = (void *) data;
  615. if (local->quiescing)
  616. return;
  617. ieee80211_queue_work(&local->hw, &local->work_work);
  618. }
  619. static void ieee80211_work_work(struct work_struct *work)
  620. {
  621. struct ieee80211_local *local =
  622. container_of(work, struct ieee80211_local, work_work);
  623. struct sk_buff *skb;
  624. struct ieee80211_work *wk, *tmp;
  625. LIST_HEAD(free_work);
  626. enum work_action rma;
  627. if (local->scanning)
  628. return;
  629. /*
  630. * ieee80211_queue_work() should have picked up most cases,
  631. * here we'll pick the the rest.
  632. */
  633. if (WARN(local->suspended, "work scheduled while going to suspend\n"))
  634. return;
  635. /* first process frames to avoid timing out while a frame is pending */
  636. while ((skb = skb_dequeue(&local->work_skb_queue)))
  637. ieee80211_work_rx_queued_mgmt(local, skb);
  638. ieee80211_recalc_idle(local);
  639. mutex_lock(&local->work_mtx);
  640. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  641. if (time_is_after_jiffies(wk->timeout)) {
  642. /*
  643. * This work item isn't supposed to be worked on
  644. * right now, but take care to adjust the timer
  645. * properly.
  646. */
  647. run_again(local, wk->timeout);
  648. continue;
  649. }
  650. switch (wk->type) {
  651. default:
  652. WARN_ON(1);
  653. /* nothing */
  654. rma = WORK_ACT_NONE;
  655. break;
  656. case IEEE80211_WORK_DIRECT_PROBE:
  657. rma = ieee80211_direct_probe(wk);
  658. break;
  659. case IEEE80211_WORK_AUTH:
  660. rma = ieee80211_authenticate(wk);
  661. break;
  662. case IEEE80211_WORK_ASSOC:
  663. rma = ieee80211_associate(wk);
  664. break;
  665. }
  666. switch (rma) {
  667. case WORK_ACT_NONE:
  668. /* no action required */
  669. break;
  670. case WORK_ACT_TIMEOUT:
  671. list_del_rcu(&wk->list);
  672. synchronize_rcu();
  673. list_add(&wk->list, &free_work);
  674. break;
  675. default:
  676. WARN(1, "unexpected: %d", rma);
  677. }
  678. }
  679. if (list_empty(&local->work_list) && local->scan_req)
  680. ieee80211_queue_delayed_work(&local->hw,
  681. &local->scan_work,
  682. round_jiffies_relative(0));
  683. mutex_unlock(&local->work_mtx);
  684. list_for_each_entry_safe(wk, tmp, &free_work, list) {
  685. wk->done(wk, NULL);
  686. list_del(&wk->list);
  687. kfree(wk);
  688. }
  689. }
  690. void ieee80211_add_work(struct ieee80211_work *wk)
  691. {
  692. struct ieee80211_local *local;
  693. if (WARN_ON(!wk->chan))
  694. return;
  695. if (WARN_ON(!wk->sdata))
  696. return;
  697. if (WARN_ON(!wk->done))
  698. return;
  699. wk->timeout = jiffies;
  700. local = wk->sdata->local;
  701. mutex_lock(&local->work_mtx);
  702. list_add_tail(&wk->list, &local->work_list);
  703. mutex_unlock(&local->work_mtx);
  704. ieee80211_queue_work(&local->hw, &local->work_work);
  705. }
  706. void ieee80211_work_init(struct ieee80211_local *local)
  707. {
  708. mutex_init(&local->work_mtx);
  709. INIT_LIST_HEAD(&local->work_list);
  710. setup_timer(&local->work_timer, ieee80211_work_timer,
  711. (unsigned long)local);
  712. INIT_WORK(&local->work_work, ieee80211_work_work);
  713. skb_queue_head_init(&local->work_skb_queue);
  714. }
  715. void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
  716. {
  717. struct ieee80211_local *local = sdata->local;
  718. struct ieee80211_work *wk, *tmp;
  719. mutex_lock(&local->work_mtx);
  720. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  721. if (wk->sdata != sdata)
  722. continue;
  723. list_del(&wk->list);
  724. free_work(wk);
  725. }
  726. mutex_unlock(&local->work_mtx);
  727. }
  728. ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  729. struct sk_buff *skb)
  730. {
  731. struct ieee80211_local *local = sdata->local;
  732. struct ieee80211_mgmt *mgmt;
  733. struct ieee80211_work *wk;
  734. u16 fc;
  735. if (skb->len < 24)
  736. return RX_DROP_MONITOR;
  737. mgmt = (struct ieee80211_mgmt *) skb->data;
  738. fc = le16_to_cpu(mgmt->frame_control);
  739. list_for_each_entry_rcu(wk, &local->work_list, list) {
  740. if (sdata != wk->sdata)
  741. continue;
  742. if (compare_ether_addr(wk->filter_ta, mgmt->sa))
  743. continue;
  744. if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
  745. continue;
  746. switch (fc & IEEE80211_FCTL_STYPE) {
  747. case IEEE80211_STYPE_AUTH:
  748. case IEEE80211_STYPE_PROBE_RESP:
  749. case IEEE80211_STYPE_ASSOC_RESP:
  750. case IEEE80211_STYPE_REASSOC_RESP:
  751. case IEEE80211_STYPE_DEAUTH:
  752. case IEEE80211_STYPE_DISASSOC:
  753. skb_queue_tail(&local->work_skb_queue, skb);
  754. ieee80211_queue_work(&local->hw, &local->work_work);
  755. return RX_QUEUED;
  756. }
  757. }
  758. return RX_CONTINUE;
  759. }