work.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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. return WORK_ACT_TIMEOUT;
  375. }
  376. printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
  377. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  378. /*
  379. * Direct probe is sent to broadcast address as some APs
  380. * will not answer to direct packet in unassociated state.
  381. */
  382. ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
  383. wk->probe_auth.ssid_len, NULL, 0);
  384. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  385. run_again(local, wk->timeout);
  386. return WORK_ACT_NONE;
  387. }
  388. static enum work_action __must_check
  389. ieee80211_authenticate(struct ieee80211_work *wk)
  390. {
  391. struct ieee80211_sub_if_data *sdata = wk->sdata;
  392. struct ieee80211_local *local = sdata->local;
  393. wk->probe_auth.tries++;
  394. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  395. printk(KERN_DEBUG "%s: authentication with %pM"
  396. " timed out\n", sdata->name, wk->filter_ta);
  397. /*
  398. * Most likely AP is not in the range so remove the
  399. * bss struct for that AP.
  400. */
  401. ieee80211_remove_auth_bss(local, wk);
  402. return WORK_ACT_TIMEOUT;
  403. }
  404. printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
  405. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  406. ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
  407. wk->ie_len, wk->filter_ta, NULL, 0, 0);
  408. wk->probe_auth.transaction = 2;
  409. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  410. run_again(local, wk->timeout);
  411. return WORK_ACT_NONE;
  412. }
  413. static enum work_action __must_check
  414. ieee80211_associate(struct ieee80211_work *wk)
  415. {
  416. struct ieee80211_sub_if_data *sdata = wk->sdata;
  417. struct ieee80211_local *local = sdata->local;
  418. wk->assoc.tries++;
  419. if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
  420. printk(KERN_DEBUG "%s: association with %pM"
  421. " timed out\n",
  422. sdata->name, wk->filter_ta);
  423. /*
  424. * Most likely AP is not in the range so remove the
  425. * bss struct for that AP.
  426. */
  427. if (wk->assoc.bss)
  428. cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
  429. return WORK_ACT_TIMEOUT;
  430. }
  431. printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
  432. sdata->name, wk->filter_ta, wk->assoc.tries);
  433. ieee80211_send_assoc(sdata, wk);
  434. wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  435. run_again(local, wk->timeout);
  436. return WORK_ACT_NONE;
  437. }
  438. static enum work_action __must_check
  439. ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
  440. {
  441. /*
  442. * First time we run, do nothing -- the generic code will
  443. * have switched to the right channel etc.
  444. */
  445. if (!wk->remain.started) {
  446. wk->remain.started = true;
  447. wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
  448. cfg80211_ready_on_channel(wk->sdata->dev, (u64)wk, wk->chan,
  449. wk->chan_type, wk->remain.duration,
  450. GFP_KERNEL);
  451. return WORK_ACT_NONE;
  452. }
  453. return WORK_ACT_TIMEOUT;
  454. }
  455. static void ieee80211_auth_challenge(struct ieee80211_work *wk,
  456. struct ieee80211_mgmt *mgmt,
  457. size_t len)
  458. {
  459. struct ieee80211_sub_if_data *sdata = wk->sdata;
  460. u8 *pos;
  461. struct ieee802_11_elems elems;
  462. pos = mgmt->u.auth.variable;
  463. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  464. if (!elems.challenge)
  465. return;
  466. ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
  467. elems.challenge - 2, elems.challenge_len + 2,
  468. wk->filter_ta, wk->probe_auth.key,
  469. wk->probe_auth.key_len, wk->probe_auth.key_idx);
  470. wk->probe_auth.transaction = 4;
  471. }
  472. static enum work_action __must_check
  473. ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
  474. struct ieee80211_mgmt *mgmt, size_t len)
  475. {
  476. u16 auth_alg, auth_transaction, status_code;
  477. if (wk->type != IEEE80211_WORK_AUTH)
  478. return WORK_ACT_NONE;
  479. if (len < 24 + 6)
  480. return WORK_ACT_NONE;
  481. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  482. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  483. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  484. if (auth_alg != wk->probe_auth.algorithm ||
  485. auth_transaction != wk->probe_auth.transaction)
  486. return WORK_ACT_NONE;
  487. if (status_code != WLAN_STATUS_SUCCESS) {
  488. printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
  489. wk->sdata->name, mgmt->sa, status_code);
  490. return WORK_ACT_DONE;
  491. }
  492. switch (wk->probe_auth.algorithm) {
  493. case WLAN_AUTH_OPEN:
  494. case WLAN_AUTH_LEAP:
  495. case WLAN_AUTH_FT:
  496. break;
  497. case WLAN_AUTH_SHARED_KEY:
  498. if (wk->probe_auth.transaction != 4) {
  499. ieee80211_auth_challenge(wk, mgmt, len);
  500. /* need another frame */
  501. return WORK_ACT_NONE;
  502. }
  503. break;
  504. default:
  505. WARN_ON(1);
  506. return WORK_ACT_NONE;
  507. }
  508. printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
  509. return WORK_ACT_DONE;
  510. }
  511. static enum work_action __must_check
  512. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
  513. struct ieee80211_mgmt *mgmt, size_t len,
  514. bool reassoc)
  515. {
  516. struct ieee80211_sub_if_data *sdata = wk->sdata;
  517. struct ieee80211_local *local = sdata->local;
  518. u16 capab_info, status_code, aid;
  519. struct ieee802_11_elems elems;
  520. u8 *pos;
  521. /*
  522. * AssocResp and ReassocResp have identical structure, so process both
  523. * of them in this function.
  524. */
  525. if (len < 24 + 6)
  526. return WORK_ACT_NONE;
  527. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  528. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  529. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  530. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  531. "status=%d aid=%d)\n",
  532. sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
  533. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  534. pos = mgmt->u.assoc_resp.variable;
  535. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  536. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  537. elems.timeout_int && elems.timeout_int_len == 5 &&
  538. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  539. u32 tu, ms;
  540. tu = get_unaligned_le32(elems.timeout_int + 1);
  541. ms = tu * 1024 / 1000;
  542. printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
  543. "comeback duration %u TU (%u ms)\n",
  544. sdata->name, mgmt->sa, tu, ms);
  545. wk->timeout = jiffies + msecs_to_jiffies(ms);
  546. if (ms > IEEE80211_ASSOC_TIMEOUT)
  547. run_again(local, wk->timeout);
  548. return WORK_ACT_NONE;
  549. }
  550. if (status_code != WLAN_STATUS_SUCCESS)
  551. printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
  552. sdata->name, mgmt->sa, status_code);
  553. else
  554. printk(KERN_DEBUG "%s: associated\n", sdata->name);
  555. return WORK_ACT_DONE;
  556. }
  557. static enum work_action __must_check
  558. ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
  559. struct ieee80211_mgmt *mgmt, size_t len,
  560. struct ieee80211_rx_status *rx_status)
  561. {
  562. struct ieee80211_sub_if_data *sdata = wk->sdata;
  563. struct ieee80211_local *local = sdata->local;
  564. size_t baselen;
  565. ASSERT_WORK_MTX(local);
  566. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  567. if (baselen > len)
  568. return WORK_ACT_NONE;
  569. printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
  570. return WORK_ACT_DONE;
  571. }
  572. static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
  573. struct sk_buff *skb)
  574. {
  575. struct ieee80211_rx_status *rx_status;
  576. struct ieee80211_mgmt *mgmt;
  577. struct ieee80211_work *wk;
  578. enum work_action rma = WORK_ACT_NONE;
  579. u16 fc;
  580. rx_status = (struct ieee80211_rx_status *) skb->cb;
  581. mgmt = (struct ieee80211_mgmt *) skb->data;
  582. fc = le16_to_cpu(mgmt->frame_control);
  583. mutex_lock(&local->work_mtx);
  584. list_for_each_entry(wk, &local->work_list, list) {
  585. const u8 *bssid = NULL;
  586. switch (wk->type) {
  587. case IEEE80211_WORK_DIRECT_PROBE:
  588. case IEEE80211_WORK_AUTH:
  589. case IEEE80211_WORK_ASSOC:
  590. bssid = wk->filter_ta;
  591. break;
  592. default:
  593. continue;
  594. }
  595. /*
  596. * Before queuing, we already verified mgmt->sa,
  597. * so this is needed just for matching.
  598. */
  599. if (compare_ether_addr(bssid, mgmt->bssid))
  600. continue;
  601. switch (fc & IEEE80211_FCTL_STYPE) {
  602. case IEEE80211_STYPE_PROBE_RESP:
  603. rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
  604. rx_status);
  605. break;
  606. case IEEE80211_STYPE_AUTH:
  607. rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
  608. break;
  609. case IEEE80211_STYPE_ASSOC_RESP:
  610. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  611. skb->len, false);
  612. break;
  613. case IEEE80211_STYPE_REASSOC_RESP:
  614. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  615. skb->len, true);
  616. break;
  617. default:
  618. WARN_ON(1);
  619. }
  620. /*
  621. * We've processed this frame for that work, so it can't
  622. * belong to another work struct.
  623. * NB: this is also required for correctness for 'rma'!
  624. */
  625. break;
  626. }
  627. switch (rma) {
  628. case WORK_ACT_NONE:
  629. break;
  630. case WORK_ACT_DONE:
  631. list_del_rcu(&wk->list);
  632. break;
  633. default:
  634. WARN(1, "unexpected: %d", rma);
  635. }
  636. mutex_unlock(&local->work_mtx);
  637. if (rma != WORK_ACT_DONE)
  638. goto out;
  639. switch (wk->done(wk, skb)) {
  640. case WORK_DONE_DESTROY:
  641. free_work(wk);
  642. break;
  643. case WORK_DONE_REQUEUE:
  644. synchronize_rcu();
  645. wk->started = false; /* restart */
  646. mutex_lock(&local->work_mtx);
  647. list_add_tail(&wk->list, &local->work_list);
  648. mutex_unlock(&local->work_mtx);
  649. }
  650. out:
  651. kfree_skb(skb);
  652. }
  653. static void ieee80211_work_timer(unsigned long data)
  654. {
  655. struct ieee80211_local *local = (void *) data;
  656. if (local->quiescing)
  657. return;
  658. ieee80211_queue_work(&local->hw, &local->work_work);
  659. }
  660. static void ieee80211_work_work(struct work_struct *work)
  661. {
  662. struct ieee80211_local *local =
  663. container_of(work, struct ieee80211_local, work_work);
  664. struct sk_buff *skb;
  665. struct ieee80211_work *wk, *tmp;
  666. LIST_HEAD(free_work);
  667. enum work_action rma;
  668. bool remain_off_channel = false;
  669. if (local->scanning)
  670. return;
  671. /*
  672. * ieee80211_queue_work() should have picked up most cases,
  673. * here we'll pick the the rest.
  674. */
  675. if (WARN(local->suspended, "work scheduled while going to suspend\n"))
  676. return;
  677. /* first process frames to avoid timing out while a frame is pending */
  678. while ((skb = skb_dequeue(&local->work_skb_queue)))
  679. ieee80211_work_rx_queued_mgmt(local, skb);
  680. ieee80211_recalc_idle(local);
  681. mutex_lock(&local->work_mtx);
  682. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  683. /* mark work as started if it's on the current off-channel */
  684. if (!wk->started && local->tmp_channel &&
  685. wk->chan == local->tmp_channel &&
  686. wk->chan_type == local->tmp_channel_type) {
  687. wk->started = true;
  688. }
  689. if (!wk->started && !local->tmp_channel) {
  690. /*
  691. * TODO: could optimize this by leaving the
  692. * station vifs in awake mode if they
  693. * happen to be on the same channel as
  694. * the requested channel
  695. */
  696. ieee80211_offchannel_stop_beaconing(local);
  697. ieee80211_offchannel_stop_station(local);
  698. local->tmp_channel = wk->chan;
  699. local->tmp_channel_type = wk->chan_type;
  700. ieee80211_hw_config(local, 0);
  701. wk->started = true;
  702. wk->timeout = jiffies;
  703. }
  704. /* don't try to work with items that aren't started */
  705. if (!wk->started)
  706. continue;
  707. if (time_is_after_jiffies(wk->timeout)) {
  708. /*
  709. * This work item isn't supposed to be worked on
  710. * right now, but take care to adjust the timer
  711. * properly.
  712. */
  713. run_again(local, wk->timeout);
  714. continue;
  715. }
  716. switch (wk->type) {
  717. default:
  718. WARN_ON(1);
  719. /* nothing */
  720. rma = WORK_ACT_NONE;
  721. break;
  722. case IEEE80211_WORK_ABORT:
  723. rma = WORK_ACT_TIMEOUT;
  724. case IEEE80211_WORK_DIRECT_PROBE:
  725. rma = ieee80211_direct_probe(wk);
  726. break;
  727. case IEEE80211_WORK_AUTH:
  728. rma = ieee80211_authenticate(wk);
  729. break;
  730. case IEEE80211_WORK_ASSOC:
  731. rma = ieee80211_associate(wk);
  732. break;
  733. case IEEE80211_WORK_REMAIN_ON_CHANNEL:
  734. rma = ieee80211_remain_on_channel_timeout(wk);
  735. break;
  736. }
  737. switch (rma) {
  738. case WORK_ACT_NONE:
  739. /* might have changed the timeout */
  740. run_again(local, wk->timeout);
  741. break;
  742. case WORK_ACT_TIMEOUT:
  743. list_del_rcu(&wk->list);
  744. synchronize_rcu();
  745. list_add(&wk->list, &free_work);
  746. break;
  747. default:
  748. WARN(1, "unexpected: %d", rma);
  749. }
  750. }
  751. list_for_each_entry(wk, &local->work_list, list) {
  752. if (!wk->started)
  753. continue;
  754. if (wk->chan != local->tmp_channel)
  755. continue;
  756. if (wk->chan_type != local->tmp_channel_type)
  757. continue;
  758. remain_off_channel = true;
  759. }
  760. if (!remain_off_channel && local->tmp_channel) {
  761. local->tmp_channel = NULL;
  762. ieee80211_hw_config(local, 0);
  763. ieee80211_offchannel_return(local, true);
  764. /* give connection some time to breathe */
  765. run_again(local, jiffies + HZ/2);
  766. }
  767. if (list_empty(&local->work_list) && local->scan_req)
  768. ieee80211_queue_delayed_work(&local->hw,
  769. &local->scan_work,
  770. round_jiffies_relative(0));
  771. mutex_unlock(&local->work_mtx);
  772. ieee80211_recalc_idle(local);
  773. list_for_each_entry_safe(wk, tmp, &free_work, list) {
  774. wk->done(wk, NULL);
  775. list_del(&wk->list);
  776. kfree(wk);
  777. }
  778. }
  779. void ieee80211_add_work(struct ieee80211_work *wk)
  780. {
  781. struct ieee80211_local *local;
  782. if (WARN_ON(!wk->chan))
  783. return;
  784. if (WARN_ON(!wk->sdata))
  785. return;
  786. if (WARN_ON(!wk->done))
  787. return;
  788. wk->started = false;
  789. local = wk->sdata->local;
  790. mutex_lock(&local->work_mtx);
  791. list_add_tail(&wk->list, &local->work_list);
  792. mutex_unlock(&local->work_mtx);
  793. ieee80211_queue_work(&local->hw, &local->work_work);
  794. }
  795. void ieee80211_work_init(struct ieee80211_local *local)
  796. {
  797. mutex_init(&local->work_mtx);
  798. INIT_LIST_HEAD(&local->work_list);
  799. setup_timer(&local->work_timer, ieee80211_work_timer,
  800. (unsigned long)local);
  801. INIT_WORK(&local->work_work, ieee80211_work_work);
  802. skb_queue_head_init(&local->work_skb_queue);
  803. }
  804. void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
  805. {
  806. struct ieee80211_local *local = sdata->local;
  807. struct ieee80211_work *wk;
  808. mutex_lock(&local->work_mtx);
  809. list_for_each_entry(wk, &local->work_list, list) {
  810. if (wk->sdata != sdata)
  811. continue;
  812. wk->type = IEEE80211_WORK_ABORT;
  813. wk->started = true;
  814. wk->timeout = jiffies;
  815. }
  816. mutex_unlock(&local->work_mtx);
  817. /* run cleanups etc. */
  818. ieee80211_work_work(&local->work_work);
  819. mutex_lock(&local->work_mtx);
  820. list_for_each_entry(wk, &local->work_list, list) {
  821. if (wk->sdata != sdata)
  822. continue;
  823. WARN_ON(1);
  824. break;
  825. }
  826. mutex_unlock(&local->work_mtx);
  827. }
  828. ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  829. struct sk_buff *skb)
  830. {
  831. struct ieee80211_local *local = sdata->local;
  832. struct ieee80211_mgmt *mgmt;
  833. struct ieee80211_work *wk;
  834. u16 fc;
  835. if (skb->len < 24)
  836. return RX_DROP_MONITOR;
  837. mgmt = (struct ieee80211_mgmt *) skb->data;
  838. fc = le16_to_cpu(mgmt->frame_control);
  839. list_for_each_entry_rcu(wk, &local->work_list, list) {
  840. if (sdata != wk->sdata)
  841. continue;
  842. if (compare_ether_addr(wk->filter_ta, mgmt->sa))
  843. continue;
  844. if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
  845. continue;
  846. switch (fc & IEEE80211_FCTL_STYPE) {
  847. case IEEE80211_STYPE_AUTH:
  848. case IEEE80211_STYPE_PROBE_RESP:
  849. case IEEE80211_STYPE_ASSOC_RESP:
  850. case IEEE80211_STYPE_REASSOC_RESP:
  851. case IEEE80211_STYPE_DEAUTH:
  852. case IEEE80211_STYPE_DISASSOC:
  853. skb_queue_tail(&local->work_skb_queue, skb);
  854. ieee80211_queue_work(&local->hw, &local->work_work);
  855. return RX_QUEUED;
  856. }
  857. }
  858. return RX_CONTINUE;
  859. }
  860. static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
  861. struct sk_buff *skb)
  862. {
  863. /*
  864. * We are done serving the remain-on-channel command.
  865. */
  866. cfg80211_remain_on_channel_expired(wk->sdata->dev, (u64)wk,
  867. wk->chan, wk->chan_type,
  868. GFP_KERNEL);
  869. return WORK_DONE_DESTROY;
  870. }
  871. int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  872. struct ieee80211_channel *chan,
  873. enum nl80211_channel_type channel_type,
  874. unsigned int duration, u64 *cookie)
  875. {
  876. struct ieee80211_work *wk;
  877. wk = kzalloc(sizeof(*wk), GFP_KERNEL);
  878. if (!wk)
  879. return -ENOMEM;
  880. wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
  881. wk->chan = chan;
  882. wk->chan_type = channel_type;
  883. wk->sdata = sdata;
  884. wk->done = ieee80211_remain_done;
  885. wk->remain.duration = duration;
  886. *cookie = (u64)wk;
  887. ieee80211_add_work(wk);
  888. return 0;
  889. }
  890. int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  891. u64 cookie)
  892. {
  893. struct ieee80211_local *local = sdata->local;
  894. struct ieee80211_work *wk, *tmp;
  895. bool found = false;
  896. mutex_lock(&local->work_mtx);
  897. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  898. if ((u64)wk == cookie) {
  899. wk->timeout = jiffies;
  900. found = true;
  901. break;
  902. }
  903. }
  904. mutex_unlock(&local->work_mtx);
  905. if (!found)
  906. return -ENOENT;
  907. ieee80211_queue_work(&local->hw, &local->work_work);
  908. return 0;
  909. }