work.c 30 KB

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