work.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * mac80211 work implementation
  3. *
  4. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  5. * Copyright 2004, Instant802 Networks, Inc.
  6. * Copyright 2005, Devicescape Software, Inc.
  7. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  8. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  9. * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/crc32.h>
  21. #include <linux/slab.h>
  22. #include <net/mac80211.h>
  23. #include <asm/unaligned.h>
  24. #include "ieee80211_i.h"
  25. #include "rate.h"
  26. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  27. #define IEEE80211_AUTH_MAX_TRIES 3
  28. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  29. #define IEEE80211_ASSOC_MAX_TRIES 3
  30. #define IEEE80211_MAX_PROBE_TRIES 5
  31. enum work_action {
  32. WORK_ACT_NONE,
  33. WORK_ACT_TIMEOUT,
  34. WORK_ACT_DONE,
  35. };
  36. /* utils */
  37. static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
  38. {
  39. WARN_ON(!mutex_is_locked(&local->work_mtx));
  40. }
  41. /*
  42. * We can have multiple work items (and connection probing)
  43. * scheduling this timer, but we need to take care to only
  44. * reschedule it when it should fire _earlier_ than it was
  45. * asked for before, or if it's not pending right now. This
  46. * function ensures that. Note that it then is required to
  47. * run this function for all timeouts after the first one
  48. * has happened -- the work that runs from this timer will
  49. * do that.
  50. */
  51. static void run_again(struct ieee80211_local *local,
  52. unsigned long timeout)
  53. {
  54. ASSERT_WORK_MTX(local);
  55. if (!timer_pending(&local->work_timer) ||
  56. time_before(timeout, local->work_timer.expires))
  57. mod_timer(&local->work_timer, timeout);
  58. }
  59. static void work_free_rcu(struct rcu_head *head)
  60. {
  61. struct ieee80211_work *wk =
  62. container_of(head, struct ieee80211_work, rcu_head);
  63. kfree(wk);
  64. }
  65. void free_work(struct ieee80211_work *wk)
  66. {
  67. call_rcu(&wk->rcu_head, work_free_rcu);
  68. }
  69. static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
  70. struct ieee80211_supported_band *sband,
  71. u32 *rates)
  72. {
  73. int i, j, count;
  74. *rates = 0;
  75. count = 0;
  76. for (i = 0; i < supp_rates_len; i++) {
  77. int rate = (supp_rates[i] & 0x7F) * 5;
  78. for (j = 0; j < sband->n_bitrates; j++)
  79. if (sband->bitrates[j].bitrate == rate) {
  80. *rates |= BIT(j);
  81. count++;
  82. break;
  83. }
  84. }
  85. return count;
  86. }
  87. /* frame sending functions */
  88. static void ieee80211_add_ht_ie(struct sk_buff *skb, const u8 *ht_info_ie,
  89. struct ieee80211_supported_band *sband,
  90. struct ieee80211_channel *channel,
  91. enum ieee80211_smps_mode smps)
  92. {
  93. struct ieee80211_ht_info *ht_info;
  94. u8 *pos;
  95. u32 flags = channel->flags;
  96. u16 cap = sband->ht_cap.cap;
  97. __le16 tmp;
  98. if (!sband->ht_cap.ht_supported)
  99. return;
  100. if (!ht_info_ie)
  101. return;
  102. if (ht_info_ie[1] < sizeof(struct ieee80211_ht_info))
  103. return;
  104. ht_info = (struct ieee80211_ht_info *)(ht_info_ie + 2);
  105. /* determine capability flags */
  106. if (ieee80211_disable_40mhz_24ghz &&
  107. sband->band == IEEE80211_BAND_2GHZ) {
  108. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  109. cap &= ~IEEE80211_HT_CAP_SGI_40;
  110. }
  111. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  112. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  113. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  114. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  115. cap &= ~IEEE80211_HT_CAP_SGI_40;
  116. }
  117. break;
  118. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  119. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  120. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  121. cap &= ~IEEE80211_HT_CAP_SGI_40;
  122. }
  123. break;
  124. }
  125. /* set SM PS mode properly */
  126. cap &= ~IEEE80211_HT_CAP_SM_PS;
  127. switch (smps) {
  128. case IEEE80211_SMPS_AUTOMATIC:
  129. case IEEE80211_SMPS_NUM_MODES:
  130. WARN_ON(1);
  131. case IEEE80211_SMPS_OFF:
  132. cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
  133. IEEE80211_HT_CAP_SM_PS_SHIFT;
  134. break;
  135. case IEEE80211_SMPS_STATIC:
  136. cap |= WLAN_HT_CAP_SM_PS_STATIC <<
  137. IEEE80211_HT_CAP_SM_PS_SHIFT;
  138. break;
  139. case IEEE80211_SMPS_DYNAMIC:
  140. cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
  141. IEEE80211_HT_CAP_SM_PS_SHIFT;
  142. break;
  143. }
  144. /* reserve and fill IE */
  145. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  146. *pos++ = WLAN_EID_HT_CAPABILITY;
  147. *pos++ = sizeof(struct ieee80211_ht_cap);
  148. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  149. /* capability flags */
  150. tmp = cpu_to_le16(cap);
  151. memcpy(pos, &tmp, sizeof(u16));
  152. pos += sizeof(u16);
  153. /* AMPDU parameters */
  154. *pos++ = sband->ht_cap.ampdu_factor |
  155. (sband->ht_cap.ampdu_density <<
  156. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  157. /* MCS set */
  158. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  159. pos += sizeof(sband->ht_cap.mcs);
  160. /* extended capabilities */
  161. pos += sizeof(__le16);
  162. /* BF capabilities */
  163. pos += sizeof(__le32);
  164. /* antenna selection */
  165. pos += sizeof(u8);
  166. }
  167. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata,
  168. struct ieee80211_work *wk)
  169. {
  170. struct ieee80211_local *local = sdata->local;
  171. struct sk_buff *skb;
  172. struct ieee80211_mgmt *mgmt;
  173. u8 *pos, qos_info;
  174. const u8 *ies;
  175. size_t offset = 0, noffset;
  176. int i, len, count, rates_len, supp_rates_len;
  177. u16 capab;
  178. struct ieee80211_supported_band *sband;
  179. u32 rates = 0;
  180. sband = local->hw.wiphy->bands[wk->chan->band];
  181. /*
  182. * Get all rates supported by the device and the AP as
  183. * some APs don't like getting a superset of their rates
  184. * in the association request (e.g. D-Link DAP 1353 in
  185. * b-only mode)...
  186. */
  187. rates_len = ieee80211_compatible_rates(wk->assoc.supp_rates,
  188. wk->assoc.supp_rates_len,
  189. sband, &rates);
  190. skb = alloc_skb(local->hw.extra_tx_headroom +
  191. sizeof(*mgmt) + /* bit too much but doesn't matter */
  192. 2 + wk->assoc.ssid_len + /* SSID */
  193. 4 + rates_len + /* (extended) rates */
  194. 4 + /* power capability */
  195. 2 + 2 * sband->n_channels + /* supported channels */
  196. 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
  197. wk->ie_len + /* extra IEs */
  198. 9, /* WMM */
  199. GFP_KERNEL);
  200. if (!skb) {
  201. printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
  202. "frame\n", sdata->name);
  203. return;
  204. }
  205. skb_reserve(skb, local->hw.extra_tx_headroom);
  206. capab = WLAN_CAPABILITY_ESS;
  207. if (sband->band == IEEE80211_BAND_2GHZ) {
  208. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  209. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  210. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  211. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  212. }
  213. if (wk->assoc.capability & WLAN_CAPABILITY_PRIVACY)
  214. capab |= WLAN_CAPABILITY_PRIVACY;
  215. if ((wk->assoc.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  216. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  217. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  218. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  219. memset(mgmt, 0, 24);
  220. memcpy(mgmt->da, wk->filter_ta, ETH_ALEN);
  221. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  222. memcpy(mgmt->bssid, wk->filter_ta, ETH_ALEN);
  223. if (!is_zero_ether_addr(wk->assoc.prev_bssid)) {
  224. skb_put(skb, 10);
  225. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  226. IEEE80211_STYPE_REASSOC_REQ);
  227. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  228. mgmt->u.reassoc_req.listen_interval =
  229. cpu_to_le16(local->hw.conf.listen_interval);
  230. memcpy(mgmt->u.reassoc_req.current_ap, wk->assoc.prev_bssid,
  231. ETH_ALEN);
  232. } else {
  233. skb_put(skb, 4);
  234. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  235. IEEE80211_STYPE_ASSOC_REQ);
  236. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  237. mgmt->u.assoc_req.listen_interval =
  238. cpu_to_le16(local->hw.conf.listen_interval);
  239. }
  240. /* SSID */
  241. ies = pos = skb_put(skb, 2 + wk->assoc.ssid_len);
  242. *pos++ = WLAN_EID_SSID;
  243. *pos++ = wk->assoc.ssid_len;
  244. memcpy(pos, wk->assoc.ssid, wk->assoc.ssid_len);
  245. /* add all rates which were marked to be used above */
  246. supp_rates_len = rates_len;
  247. if (supp_rates_len > 8)
  248. supp_rates_len = 8;
  249. len = sband->n_bitrates;
  250. pos = skb_put(skb, supp_rates_len + 2);
  251. *pos++ = WLAN_EID_SUPP_RATES;
  252. *pos++ = supp_rates_len;
  253. count = 0;
  254. for (i = 0; i < sband->n_bitrates; i++) {
  255. if (BIT(i) & rates) {
  256. int rate = sband->bitrates[i].bitrate;
  257. *pos++ = (u8) (rate / 5);
  258. if (++count == 8)
  259. break;
  260. }
  261. }
  262. if (rates_len > count) {
  263. pos = skb_put(skb, rates_len - count + 2);
  264. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  265. *pos++ = rates_len - count;
  266. for (i++; i < sband->n_bitrates; i++) {
  267. if (BIT(i) & rates) {
  268. int rate = sband->bitrates[i].bitrate;
  269. *pos++ = (u8) (rate / 5);
  270. }
  271. }
  272. }
  273. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  274. /* 1. power capabilities */
  275. pos = skb_put(skb, 4);
  276. *pos++ = WLAN_EID_PWR_CAPABILITY;
  277. *pos++ = 2;
  278. *pos++ = 0; /* min tx power */
  279. *pos++ = wk->chan->max_power; /* max tx power */
  280. /* 2. supported channels */
  281. /* TODO: get this in reg domain format */
  282. pos = skb_put(skb, 2 * sband->n_channels + 2);
  283. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  284. *pos++ = 2 * sband->n_channels;
  285. for (i = 0; i < sband->n_channels; i++) {
  286. *pos++ = ieee80211_frequency_to_channel(
  287. sband->channels[i].center_freq);
  288. *pos++ = 1; /* one channel in the subband*/
  289. }
  290. }
  291. /* if present, add any custom IEs that go before HT */
  292. if (wk->ie_len && wk->ie) {
  293. static const u8 before_ht[] = {
  294. WLAN_EID_SSID,
  295. WLAN_EID_SUPP_RATES,
  296. WLAN_EID_EXT_SUPP_RATES,
  297. WLAN_EID_PWR_CAPABILITY,
  298. WLAN_EID_SUPPORTED_CHANNELS,
  299. WLAN_EID_RSN,
  300. WLAN_EID_QOS_CAPA,
  301. WLAN_EID_RRM_ENABLED_CAPABILITIES,
  302. WLAN_EID_MOBILITY_DOMAIN,
  303. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  304. };
  305. noffset = ieee80211_ie_split(wk->ie, wk->ie_len,
  306. before_ht, ARRAY_SIZE(before_ht),
  307. offset);
  308. pos = skb_put(skb, noffset - offset);
  309. memcpy(pos, wk->ie + offset, noffset - offset);
  310. offset = noffset;
  311. }
  312. if (wk->assoc.use_11n && wk->assoc.wmm_used &&
  313. local->hw.queues >= 4)
  314. ieee80211_add_ht_ie(skb, wk->assoc.ht_information_ie,
  315. sband, wk->chan, wk->assoc.smps);
  316. /* if present, add any custom non-vendor IEs that go after HT */
  317. if (wk->ie_len && wk->ie) {
  318. noffset = ieee80211_ie_split_vendor(wk->ie, wk->ie_len,
  319. offset);
  320. pos = skb_put(skb, noffset - offset);
  321. memcpy(pos, wk->ie + offset, noffset - offset);
  322. offset = noffset;
  323. }
  324. if (wk->assoc.wmm_used && local->hw.queues >= 4) {
  325. if (wk->assoc.uapsd_used) {
  326. qos_info = local->uapsd_queues;
  327. qos_info |= (local->uapsd_max_sp_len <<
  328. IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
  329. } else {
  330. qos_info = 0;
  331. }
  332. pos = skb_put(skb, 9);
  333. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  334. *pos++ = 7; /* len */
  335. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  336. *pos++ = 0x50;
  337. *pos++ = 0xf2;
  338. *pos++ = 2; /* WME */
  339. *pos++ = 0; /* WME info */
  340. *pos++ = 1; /* WME ver */
  341. *pos++ = qos_info;
  342. }
  343. /* add any remaining custom (i.e. vendor specific here) IEs */
  344. if (wk->ie_len && wk->ie) {
  345. noffset = wk->ie_len;
  346. pos = skb_put(skb, noffset - offset);
  347. memcpy(pos, wk->ie + offset, noffset - offset);
  348. }
  349. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  350. ieee80211_tx_skb(sdata, skb);
  351. }
  352. static void ieee80211_remove_auth_bss(struct ieee80211_local *local,
  353. struct ieee80211_work *wk)
  354. {
  355. struct cfg80211_bss *cbss;
  356. u16 capa_val = WLAN_CAPABILITY_ESS;
  357. if (wk->probe_auth.privacy)
  358. capa_val |= WLAN_CAPABILITY_PRIVACY;
  359. cbss = cfg80211_get_bss(local->hw.wiphy, wk->chan, wk->filter_ta,
  360. wk->probe_auth.ssid, wk->probe_auth.ssid_len,
  361. WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
  362. capa_val);
  363. if (!cbss)
  364. return;
  365. cfg80211_unlink_bss(local->hw.wiphy, cbss);
  366. cfg80211_put_bss(cbss);
  367. }
  368. static enum work_action __must_check
  369. ieee80211_direct_probe(struct ieee80211_work *wk)
  370. {
  371. struct ieee80211_sub_if_data *sdata = wk->sdata;
  372. struct ieee80211_local *local = sdata->local;
  373. wk->probe_auth.tries++;
  374. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  375. printk(KERN_DEBUG "%s: direct probe to %pM timed out\n",
  376. sdata->name, wk->filter_ta);
  377. /*
  378. * Most likely AP is not in the range so remove the
  379. * bss struct for that AP.
  380. */
  381. ieee80211_remove_auth_bss(local, wk);
  382. return WORK_ACT_TIMEOUT;
  383. }
  384. printk(KERN_DEBUG "%s: direct probe to %pM (try %d)\n",
  385. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  386. /*
  387. * Direct probe is sent to broadcast address as some APs
  388. * will not answer to direct packet in unassociated state.
  389. */
  390. ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
  391. wk->probe_auth.ssid_len, NULL, 0);
  392. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  393. run_again(local, wk->timeout);
  394. return WORK_ACT_NONE;
  395. }
  396. static enum work_action __must_check
  397. ieee80211_authenticate(struct ieee80211_work *wk)
  398. {
  399. struct ieee80211_sub_if_data *sdata = wk->sdata;
  400. struct ieee80211_local *local = sdata->local;
  401. wk->probe_auth.tries++;
  402. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  403. printk(KERN_DEBUG "%s: authentication with %pM"
  404. " timed out\n", sdata->name, wk->filter_ta);
  405. /*
  406. * Most likely AP is not in the range so remove the
  407. * bss struct for that AP.
  408. */
  409. ieee80211_remove_auth_bss(local, wk);
  410. return WORK_ACT_TIMEOUT;
  411. }
  412. printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
  413. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  414. ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
  415. wk->ie_len, wk->filter_ta, NULL, 0, 0);
  416. wk->probe_auth.transaction = 2;
  417. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  418. run_again(local, wk->timeout);
  419. return WORK_ACT_NONE;
  420. }
  421. static enum work_action __must_check
  422. ieee80211_associate(struct ieee80211_work *wk)
  423. {
  424. struct ieee80211_sub_if_data *sdata = wk->sdata;
  425. struct ieee80211_local *local = sdata->local;
  426. wk->assoc.tries++;
  427. if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
  428. printk(KERN_DEBUG "%s: association with %pM"
  429. " timed out\n",
  430. sdata->name, wk->filter_ta);
  431. /*
  432. * Most likely AP is not in the range so remove the
  433. * bss struct for that AP.
  434. */
  435. if (wk->assoc.bss)
  436. cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
  437. return WORK_ACT_TIMEOUT;
  438. }
  439. printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
  440. sdata->name, wk->filter_ta, wk->assoc.tries);
  441. ieee80211_send_assoc(sdata, wk);
  442. wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  443. run_again(local, wk->timeout);
  444. return WORK_ACT_NONE;
  445. }
  446. static enum work_action __must_check
  447. ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
  448. {
  449. /*
  450. * First time we run, do nothing -- the generic code will
  451. * have switched to the right channel etc.
  452. */
  453. if (!wk->started) {
  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. bool started = wk->started;
  691. /* mark work as started if it's on the current off-channel */
  692. if (!started && local->tmp_channel &&
  693. wk->chan == local->tmp_channel &&
  694. wk->chan_type == local->tmp_channel_type) {
  695. started = true;
  696. wk->timeout = jiffies;
  697. }
  698. if (!started && !local->tmp_channel) {
  699. /*
  700. * TODO: could optimize this by leaving the
  701. * station vifs in awake mode if they
  702. * happen to be on the same channel as
  703. * the requested channel
  704. */
  705. ieee80211_offchannel_stop_beaconing(local);
  706. ieee80211_offchannel_stop_station(local);
  707. local->tmp_channel = wk->chan;
  708. local->tmp_channel_type = wk->chan_type;
  709. ieee80211_hw_config(local, 0);
  710. started = true;
  711. wk->timeout = jiffies;
  712. }
  713. /* don't try to work with items that aren't started */
  714. if (!started)
  715. continue;
  716. if (time_is_after_jiffies(wk->timeout)) {
  717. /*
  718. * This work item isn't supposed to be worked on
  719. * right now, but take care to adjust the timer
  720. * properly.
  721. */
  722. run_again(local, wk->timeout);
  723. continue;
  724. }
  725. switch (wk->type) {
  726. default:
  727. WARN_ON(1);
  728. /* nothing */
  729. rma = WORK_ACT_NONE;
  730. break;
  731. case IEEE80211_WORK_ABORT:
  732. rma = WORK_ACT_TIMEOUT;
  733. break;
  734. case IEEE80211_WORK_DIRECT_PROBE:
  735. rma = ieee80211_direct_probe(wk);
  736. break;
  737. case IEEE80211_WORK_AUTH:
  738. rma = ieee80211_authenticate(wk);
  739. break;
  740. case IEEE80211_WORK_ASSOC:
  741. rma = ieee80211_associate(wk);
  742. break;
  743. case IEEE80211_WORK_REMAIN_ON_CHANNEL:
  744. rma = ieee80211_remain_on_channel_timeout(wk);
  745. break;
  746. }
  747. wk->started = started;
  748. switch (rma) {
  749. case WORK_ACT_NONE:
  750. /* might have changed the timeout */
  751. run_again(local, wk->timeout);
  752. break;
  753. case WORK_ACT_TIMEOUT:
  754. list_del_rcu(&wk->list);
  755. synchronize_rcu();
  756. list_add(&wk->list, &free_work);
  757. break;
  758. default:
  759. WARN(1, "unexpected: %d", rma);
  760. }
  761. }
  762. list_for_each_entry(wk, &local->work_list, list) {
  763. if (!wk->started)
  764. continue;
  765. if (wk->chan != local->tmp_channel)
  766. continue;
  767. if (wk->chan_type != local->tmp_channel_type)
  768. continue;
  769. remain_off_channel = true;
  770. }
  771. if (!remain_off_channel && local->tmp_channel) {
  772. local->tmp_channel = NULL;
  773. ieee80211_hw_config(local, 0);
  774. ieee80211_offchannel_return(local, true);
  775. /* give connection some time to breathe */
  776. run_again(local, jiffies + HZ/2);
  777. }
  778. if (list_empty(&local->work_list) && local->scan_req)
  779. ieee80211_queue_delayed_work(&local->hw,
  780. &local->scan_work,
  781. round_jiffies_relative(0));
  782. mutex_unlock(&local->work_mtx);
  783. ieee80211_recalc_idle(local);
  784. list_for_each_entry_safe(wk, tmp, &free_work, list) {
  785. wk->done(wk, NULL);
  786. list_del(&wk->list);
  787. kfree(wk);
  788. }
  789. }
  790. void ieee80211_add_work(struct ieee80211_work *wk)
  791. {
  792. struct ieee80211_local *local;
  793. if (WARN_ON(!wk->chan))
  794. return;
  795. if (WARN_ON(!wk->sdata))
  796. return;
  797. if (WARN_ON(!wk->done))
  798. return;
  799. if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
  800. return;
  801. wk->started = false;
  802. local = wk->sdata->local;
  803. mutex_lock(&local->work_mtx);
  804. list_add_tail(&wk->list, &local->work_list);
  805. mutex_unlock(&local->work_mtx);
  806. ieee80211_queue_work(&local->hw, &local->work_work);
  807. }
  808. void ieee80211_work_init(struct ieee80211_local *local)
  809. {
  810. mutex_init(&local->work_mtx);
  811. INIT_LIST_HEAD(&local->work_list);
  812. setup_timer(&local->work_timer, ieee80211_work_timer,
  813. (unsigned long)local);
  814. INIT_WORK(&local->work_work, ieee80211_work_work);
  815. skb_queue_head_init(&local->work_skb_queue);
  816. }
  817. void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
  818. {
  819. struct ieee80211_local *local = sdata->local;
  820. struct ieee80211_work *wk;
  821. mutex_lock(&local->work_mtx);
  822. list_for_each_entry(wk, &local->work_list, list) {
  823. if (wk->sdata != sdata)
  824. continue;
  825. wk->type = IEEE80211_WORK_ABORT;
  826. wk->started = true;
  827. wk->timeout = jiffies;
  828. }
  829. mutex_unlock(&local->work_mtx);
  830. /* run cleanups etc. */
  831. ieee80211_work_work(&local->work_work);
  832. mutex_lock(&local->work_mtx);
  833. list_for_each_entry(wk, &local->work_list, list) {
  834. if (wk->sdata != sdata)
  835. continue;
  836. WARN_ON(1);
  837. break;
  838. }
  839. mutex_unlock(&local->work_mtx);
  840. }
  841. ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  842. struct sk_buff *skb)
  843. {
  844. struct ieee80211_local *local = sdata->local;
  845. struct ieee80211_mgmt *mgmt;
  846. struct ieee80211_work *wk;
  847. u16 fc;
  848. if (skb->len < 24)
  849. return RX_DROP_MONITOR;
  850. mgmt = (struct ieee80211_mgmt *) skb->data;
  851. fc = le16_to_cpu(mgmt->frame_control);
  852. list_for_each_entry_rcu(wk, &local->work_list, list) {
  853. if (sdata != wk->sdata)
  854. continue;
  855. if (compare_ether_addr(wk->filter_ta, mgmt->sa))
  856. continue;
  857. if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
  858. continue;
  859. switch (fc & IEEE80211_FCTL_STYPE) {
  860. case IEEE80211_STYPE_AUTH:
  861. case IEEE80211_STYPE_PROBE_RESP:
  862. case IEEE80211_STYPE_ASSOC_RESP:
  863. case IEEE80211_STYPE_REASSOC_RESP:
  864. skb_queue_tail(&local->work_skb_queue, skb);
  865. ieee80211_queue_work(&local->hw, &local->work_work);
  866. return RX_QUEUED;
  867. }
  868. }
  869. return RX_CONTINUE;
  870. }
  871. static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
  872. struct sk_buff *skb)
  873. {
  874. /*
  875. * We are done serving the remain-on-channel command.
  876. */
  877. cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
  878. wk->chan, wk->chan_type,
  879. GFP_KERNEL);
  880. return WORK_DONE_DESTROY;
  881. }
  882. int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  883. struct ieee80211_channel *chan,
  884. enum nl80211_channel_type channel_type,
  885. unsigned int duration, u64 *cookie)
  886. {
  887. struct ieee80211_work *wk;
  888. wk = kzalloc(sizeof(*wk), GFP_KERNEL);
  889. if (!wk)
  890. return -ENOMEM;
  891. wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
  892. wk->chan = chan;
  893. wk->chan_type = channel_type;
  894. wk->sdata = sdata;
  895. wk->done = ieee80211_remain_done;
  896. wk->remain.duration = duration;
  897. *cookie = (unsigned long) wk;
  898. ieee80211_add_work(wk);
  899. return 0;
  900. }
  901. int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  902. u64 cookie)
  903. {
  904. struct ieee80211_local *local = sdata->local;
  905. struct ieee80211_work *wk, *tmp;
  906. bool found = false;
  907. mutex_lock(&local->work_mtx);
  908. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  909. if ((unsigned long) wk == cookie) {
  910. wk->timeout = jiffies;
  911. found = true;
  912. break;
  913. }
  914. }
  915. mutex_unlock(&local->work_mtx);
  916. if (!found)
  917. return -ENOENT;
  918. ieee80211_queue_work(&local->hw, &local->work_work);
  919. return 0;
  920. }