work.c 30 KB

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