work.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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;
  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. pos = skb_put(skb, 9);
  325. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  326. *pos++ = 7; /* len */
  327. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  328. *pos++ = 0x50;
  329. *pos++ = 0xf2;
  330. *pos++ = 2; /* WME */
  331. *pos++ = 0; /* WME info */
  332. *pos++ = 1; /* WME ver */
  333. *pos++ = 0;
  334. }
  335. /* add any remaining custom (i.e. vendor specific here) IEs */
  336. if (wk->ie_len && wk->ie) {
  337. noffset = wk->ie_len;
  338. pos = skb_put(skb, noffset - offset);
  339. memcpy(pos, wk->ie + offset, noffset - offset);
  340. }
  341. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  342. ieee80211_tx_skb(sdata, skb);
  343. }
  344. static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
  345. struct ieee80211_work *wk)
  346. {
  347. struct cfg80211_bss *cbss;
  348. u16 capa_val = WLAN_CAPABILITY_ESS;
  349. if (wk->probe_auth.privacy)
  350. capa_val |= WLAN_CAPABILITY_PRIVACY;
  351. cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
  352. wk->probe_auth.ssid, wk->probe_auth.ssid_len,
  353. WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
  354. capa_val);
  355. if (!cbss)
  356. return;
  357. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  358. cfg80211_put_bss(cbss);
  359. }
  360. static enum work_action __must_check
  361. ieee80211_direct_probe(struct ieee80211_work *wk)
  362. {
  363. struct ieee80211_sub_if_data *sdata = wk->sdata;
  364. struct ieee80211_local *local = sdata->local;
  365. wk->probe_auth.tries++;
  366. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  367. printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
  368. sdata->name, wk->filter_ta);
  369. /*
  370. * Most likely AP is not in the range so remove the
  371. * bss struct for that AP.
  372. */
  373. ieee80211_remove_auth_bss(local, wk);
  374. /*
  375. * We might have a pending scan which had no chance to run yet
  376. * due to work needing to be done. Hence, queue the STAs work
  377. * again for that.
  378. */
  379. ieee80211_queue_work(&local->hw, &local->work_work);
  380. return WORK_ACT_TIMEOUT;
  381. }
  382. printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
  383. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  384. /*
  385. * Direct probe is sent to broadcast address as some APs
  386. * will not answer to direct packet in unassociated state.
  387. */
  388. ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
  389. wk->probe_auth.ssid_len, NULL, 0);
  390. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  391. run_again(local, wk->timeout);
  392. return WORK_ACT_NONE;
  393. }
  394. static enum work_action __must_check
  395. ieee80211_authenticate(struct ieee80211_work *wk)
  396. {
  397. struct ieee80211_sub_if_data *sdata = wk->sdata;
  398. struct ieee80211_local *local = sdata->local;
  399. wk->probe_auth.tries++;
  400. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  401. printk(KERN_DEBUG "%s: authentication with %pM"
  402. " timed out\n", sdata->name, wk->filter_ta);
  403. /*
  404. * Most likely AP is not in the range so remove the
  405. * bss struct for that AP.
  406. */
  407. ieee80211_remove_auth_bss(local, wk);
  408. /*
  409. * We might have a pending scan which had no chance to run yet
  410. * due to work needing to be done. Hence, queue the STAs work
  411. * again for that.
  412. */
  413. ieee80211_queue_work(&local->hw, &local->work_work);
  414. return WORK_ACT_TIMEOUT;
  415. }
  416. printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
  417. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  418. ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
  419. wk->ie_len, wk->filter_ta, NULL, 0, 0);
  420. wk->probe_auth.transaction = 2;
  421. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  422. run_again(local, wk->timeout);
  423. return WORK_ACT_NONE;
  424. }
  425. static enum work_action __must_check
  426. ieee80211_associate(struct ieee80211_work *wk)
  427. {
  428. struct ieee80211_sub_if_data *sdata = wk->sdata;
  429. struct ieee80211_local *local = sdata->local;
  430. wk->assoc.tries++;
  431. if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
  432. printk(KERN_DEBUG "%s: association with %pM"
  433. " timed out\n",
  434. sdata->name, wk->filter_ta);
  435. /*
  436. * Most likely AP is not in the range so remove the
  437. * bss struct for that AP.
  438. */
  439. if (wk->assoc.bss)
  440. cfg80211_unlink_bss(local->hw.wiphy,
  441. &wk->assoc.bss->cbss);
  442. /*
  443. * We might have a pending scan which had no chance to run yet
  444. * due to work needing to be done. Hence, queue the STAs work
  445. * again for that.
  446. */
  447. ieee80211_queue_work(&local->hw, &local->work_work);
  448. return WORK_ACT_TIMEOUT;
  449. }
  450. printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
  451. sdata->name, wk->filter_ta, wk->assoc.tries);
  452. ieee80211_send_assoc(sdata, wk);
  453. wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  454. run_again(local, wk->timeout);
  455. return WORK_ACT_NONE;
  456. }
  457. static void ieee80211_auth_challenge(struct ieee80211_work *wk,
  458. struct ieee80211_mgmt *mgmt,
  459. size_t len)
  460. {
  461. struct ieee80211_sub_if_data *sdata = wk->sdata;
  462. u8 *pos;
  463. struct ieee802_11_elems elems;
  464. pos = mgmt->u.auth.variable;
  465. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  466. if (!elems.challenge)
  467. return;
  468. ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
  469. elems.challenge - 2, elems.challenge_len + 2,
  470. wk->filter_ta, wk->probe_auth.key,
  471. wk->probe_auth.key_len, wk->probe_auth.key_idx);
  472. wk->probe_auth.transaction = 4;
  473. }
  474. static enum work_action __must_check
  475. ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
  476. struct ieee80211_mgmt *mgmt, size_t len)
  477. {
  478. u16 auth_alg, auth_transaction, status_code;
  479. if (wk->type != IEEE80211_WORK_AUTH)
  480. return WORK_ACT_NONE;
  481. if (len < 24 + 6)
  482. return WORK_ACT_NONE;
  483. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  484. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  485. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  486. if (auth_alg != wk->probe_auth.algorithm ||
  487. auth_transaction != wk->probe_auth.transaction)
  488. return WORK_ACT_NONE;
  489. if (status_code != WLAN_STATUS_SUCCESS) {
  490. printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
  491. wk->sdata->name, mgmt->sa, status_code);
  492. return WORK_ACT_DONE;
  493. }
  494. switch (wk->probe_auth.algorithm) {
  495. case WLAN_AUTH_OPEN:
  496. case WLAN_AUTH_LEAP:
  497. case WLAN_AUTH_FT:
  498. break;
  499. case WLAN_AUTH_SHARED_KEY:
  500. if (wk->probe_auth.transaction != 4) {
  501. ieee80211_auth_challenge(wk, mgmt, len);
  502. /* need another frame */
  503. return WORK_ACT_NONE;
  504. }
  505. break;
  506. default:
  507. WARN_ON(1);
  508. return WORK_ACT_NONE;
  509. }
  510. printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
  511. return WORK_ACT_DONE;
  512. }
  513. static enum work_action __must_check
  514. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
  515. struct ieee80211_mgmt *mgmt, size_t len,
  516. bool reassoc)
  517. {
  518. struct ieee80211_sub_if_data *sdata = wk->sdata;
  519. struct ieee80211_local *local = sdata->local;
  520. u16 capab_info, status_code, aid;
  521. struct ieee802_11_elems elems;
  522. u8 *pos;
  523. /*
  524. * AssocResp and ReassocResp have identical structure, so process both
  525. * of them in this function.
  526. */
  527. if (len < 24 + 6)
  528. return WORK_ACT_NONE;
  529. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  530. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  531. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  532. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  533. "status=%d aid=%d)\n",
  534. sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
  535. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  536. pos = mgmt->u.assoc_resp.variable;
  537. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  538. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  539. elems.timeout_int && elems.timeout_int_len == 5 &&
  540. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  541. u32 tu, ms;
  542. tu = get_unaligned_le32(elems.timeout_int + 1);
  543. ms = tu * 1024 / 1000;
  544. printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
  545. "comeback duration %u TU (%u ms)\n",
  546. sdata->name, mgmt->sa, tu, ms);
  547. wk->timeout = jiffies + msecs_to_jiffies(ms);
  548. if (ms > IEEE80211_ASSOC_TIMEOUT)
  549. run_again(local, wk->timeout);
  550. return WORK_ACT_NONE;
  551. }
  552. if (status_code != WLAN_STATUS_SUCCESS)
  553. printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
  554. sdata->name, mgmt->sa, status_code);
  555. else
  556. printk(KERN_DEBUG "%s: associated\n", sdata->name);
  557. return WORK_ACT_DONE;
  558. }
  559. static enum work_action __must_check
  560. ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
  561. struct ieee80211_mgmt *mgmt, size_t len,
  562. struct ieee80211_rx_status *rx_status)
  563. {
  564. struct ieee80211_sub_if_data *sdata = wk->sdata;
  565. struct ieee80211_local *local = sdata->local;
  566. size_t baselen;
  567. ASSERT_WORK_MTX(local);
  568. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  569. if (baselen > len)
  570. return WORK_ACT_NONE;
  571. printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
  572. return WORK_ACT_DONE;
  573. }
  574. static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
  575. struct sk_buff *skb)
  576. {
  577. struct ieee80211_rx_status *rx_status;
  578. struct ieee80211_mgmt *mgmt;
  579. struct ieee80211_work *wk;
  580. enum work_action rma = WORK_ACT_NONE;
  581. u16 fc;
  582. rx_status = (struct ieee80211_rx_status *) skb->cb;
  583. mgmt = (struct ieee80211_mgmt *) skb->data;
  584. fc = le16_to_cpu(mgmt->frame_control);
  585. mutex_lock(&local->work_mtx);
  586. list_for_each_entry(wk, &local->work_list, list) {
  587. const u8 *bssid = NULL;
  588. switch (wk->type) {
  589. case IEEE80211_WORK_DIRECT_PROBE:
  590. case IEEE80211_WORK_AUTH:
  591. case IEEE80211_WORK_ASSOC:
  592. bssid = wk->filter_ta;
  593. break;
  594. default:
  595. continue;
  596. }
  597. /*
  598. * Before queuing, we already verified mgmt->sa,
  599. * so this is needed just for matching.
  600. */
  601. if (compare_ether_addr(bssid, mgmt->bssid))
  602. continue;
  603. switch (fc & IEEE80211_FCTL_STYPE) {
  604. case IEEE80211_STYPE_PROBE_RESP:
  605. rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
  606. rx_status);
  607. break;
  608. case IEEE80211_STYPE_AUTH:
  609. rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
  610. break;
  611. case IEEE80211_STYPE_ASSOC_RESP:
  612. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  613. skb->len, false);
  614. break;
  615. case IEEE80211_STYPE_REASSOC_RESP:
  616. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  617. skb->len, true);
  618. break;
  619. default:
  620. WARN_ON(1);
  621. }
  622. /*
  623. * We've processed this frame for that work, so it can't
  624. * belong to another work struct.
  625. * NB: this is also required for correctness for 'rma'!
  626. */
  627. break;
  628. }
  629. switch (rma) {
  630. case WORK_ACT_NONE:
  631. break;
  632. case WORK_ACT_DONE:
  633. list_del_rcu(&wk->list);
  634. break;
  635. default:
  636. WARN(1, "unexpected: %d", rma);
  637. }
  638. mutex_unlock(&local->work_mtx);
  639. if (rma != WORK_ACT_DONE)
  640. goto out;
  641. switch (wk->done(wk, skb)) {
  642. case WORK_DONE_DESTROY:
  643. free_work(wk);
  644. break;
  645. case WORK_DONE_REQUEUE:
  646. synchronize_rcu();
  647. wk->timeout = jiffies; /* run again directly */
  648. mutex_lock(&local->work_mtx);
  649. list_add_tail(&wk->list, &local->work_list);
  650. mutex_unlock(&local->work_mtx);
  651. }
  652. out:
  653. kfree_skb(skb);
  654. }
  655. static void ieee80211_work_timer(unsigned long data)
  656. {
  657. struct ieee80211_local *local = (void *) data;
  658. if (local->quiescing)
  659. return;
  660. ieee80211_queue_work(&local->hw, &local->work_work);
  661. }
  662. static void ieee80211_work_work(struct work_struct *work)
  663. {
  664. struct ieee80211_local *local =
  665. container_of(work, struct ieee80211_local, work_work);
  666. struct sk_buff *skb;
  667. struct ieee80211_work *wk, *tmp;
  668. LIST_HEAD(free_work);
  669. enum work_action rma;
  670. if (local->scanning)
  671. return;
  672. /*
  673. * ieee80211_queue_work() should have picked up most cases,
  674. * here we'll pick the the rest.
  675. */
  676. if (WARN(local->suspended, "work scheduled while going to suspend\n"))
  677. return;
  678. /* first process frames to avoid timing out while a frame is pending */
  679. while ((skb = skb_dequeue(&local->work_skb_queue)))
  680. ieee80211_work_rx_queued_mgmt(local, skb);
  681. ieee80211_recalc_idle(local);
  682. mutex_lock(&local->work_mtx);
  683. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  684. if (time_is_after_jiffies(wk->timeout)) {
  685. /*
  686. * This work item isn't supposed to be worked on
  687. * right now, but take care to adjust the timer
  688. * properly.
  689. */
  690. run_again(local, wk->timeout);
  691. continue;
  692. }
  693. switch (wk->type) {
  694. default:
  695. WARN_ON(1);
  696. /* nothing */
  697. rma = WORK_ACT_NONE;
  698. break;
  699. case IEEE80211_WORK_DIRECT_PROBE:
  700. rma = ieee80211_direct_probe(wk);
  701. break;
  702. case IEEE80211_WORK_AUTH:
  703. rma = ieee80211_authenticate(wk);
  704. break;
  705. case IEEE80211_WORK_ASSOC:
  706. rma = ieee80211_associate(wk);
  707. break;
  708. }
  709. switch (rma) {
  710. case WORK_ACT_NONE:
  711. /* no action required */
  712. break;
  713. case WORK_ACT_TIMEOUT:
  714. list_del_rcu(&wk->list);
  715. synchronize_rcu();
  716. list_add(&wk->list, &free_work);
  717. break;
  718. default:
  719. WARN(1, "unexpected: %d", rma);
  720. }
  721. }
  722. if (list_empty(&local->work_list) && local->scan_req)
  723. ieee80211_queue_delayed_work(&local->hw,
  724. &local->scan_work,
  725. round_jiffies_relative(0));
  726. mutex_unlock(&local->work_mtx);
  727. list_for_each_entry_safe(wk, tmp, &free_work, list) {
  728. wk->done(wk, NULL);
  729. list_del(&wk->list);
  730. kfree(wk);
  731. }
  732. }
  733. void ieee80211_add_work(struct ieee80211_work *wk)
  734. {
  735. struct ieee80211_local *local;
  736. if (WARN_ON(!wk->chan))
  737. return;
  738. if (WARN_ON(!wk->sdata))
  739. return;
  740. if (WARN_ON(!wk->done))
  741. return;
  742. wk->timeout = jiffies;
  743. local = wk->sdata->local;
  744. mutex_lock(&local->work_mtx);
  745. list_add_tail(&wk->list, &local->work_list);
  746. mutex_unlock(&local->work_mtx);
  747. ieee80211_queue_work(&local->hw, &local->work_work);
  748. }
  749. void ieee80211_work_init(struct ieee80211_local *local)
  750. {
  751. mutex_init(&local->work_mtx);
  752. INIT_LIST_HEAD(&local->work_list);
  753. setup_timer(&local->work_timer, ieee80211_work_timer,
  754. (unsigned long)local);
  755. INIT_WORK(&local->work_work, ieee80211_work_work);
  756. skb_queue_head_init(&local->work_skb_queue);
  757. }
  758. void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
  759. {
  760. struct ieee80211_local *local = sdata->local;
  761. struct ieee80211_work *wk, *tmp;
  762. mutex_lock(&local->work_mtx);
  763. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  764. if (wk->sdata != sdata)
  765. continue;
  766. list_del(&wk->list);
  767. free_work(wk);
  768. }
  769. mutex_unlock(&local->work_mtx);
  770. }
  771. ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  772. struct sk_buff *skb)
  773. {
  774. struct ieee80211_local *local = sdata->local;
  775. struct ieee80211_mgmt *mgmt;
  776. struct ieee80211_work *wk;
  777. u16 fc;
  778. if (skb->len < 24)
  779. return RX_DROP_MONITOR;
  780. mgmt = (struct ieee80211_mgmt *) skb->data;
  781. fc = le16_to_cpu(mgmt->frame_control);
  782. list_for_each_entry_rcu(wk, &local->work_list, list) {
  783. if (sdata != wk->sdata)
  784. continue;
  785. if (compare_ether_addr(wk->filter_ta, mgmt->sa))
  786. continue;
  787. if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
  788. continue;
  789. switch (fc & IEEE80211_FCTL_STYPE) {
  790. case IEEE80211_STYPE_AUTH:
  791. case IEEE80211_STYPE_PROBE_RESP:
  792. case IEEE80211_STYPE_ASSOC_RESP:
  793. case IEEE80211_STYPE_REASSOC_RESP:
  794. case IEEE80211_STYPE_DEAUTH:
  795. case IEEE80211_STYPE_DISASSOC:
  796. skb_queue_tail(&local->work_skb_queue, skb);
  797. ieee80211_queue_work(&local->hw, &local->work_work);
  798. return RX_QUEUED;
  799. }
  800. }
  801. return RX_CONTINUE;
  802. }