work.c 28 KB

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