work.c 28 KB

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