work.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. lockdep_assert_held(&local->mtx);
  41. }
  42. /*
  43. * We can have multiple work items (and connection probing)
  44. * scheduling this timer, but we need to take care to only
  45. * reschedule it when it should fire _earlier_ than it was
  46. * asked for before, or if it's not pending right now. This
  47. * function ensures that. Note that it then is required to
  48. * run this function for all timeouts after the first one
  49. * has happened -- the work that runs from this timer will
  50. * do that.
  51. */
  52. static void run_again(struct ieee80211_local *local,
  53. unsigned long timeout)
  54. {
  55. ASSERT_WORK_MTX(local);
  56. if (!timer_pending(&local->work_timer) ||
  57. time_before(timeout, local->work_timer.expires))
  58. mod_timer(&local->work_timer, timeout);
  59. }
  60. 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/%i)\n",
  396. sdata->name, wk->filter_ta, wk->probe_auth.tries,
  397. IEEE80211_AUTH_MAX_TRIES);
  398. /*
  399. * Direct probe is sent to broadcast address as some APs
  400. * will not answer to direct packet in unassociated state.
  401. */
  402. ieee80211_send_probe_req(sdata, NULL, wk->probe_auth.ssid,
  403. wk->probe_auth.ssid_len, NULL, 0);
  404. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  405. run_again(local, wk->timeout);
  406. return WORK_ACT_NONE;
  407. }
  408. static enum work_action __must_check
  409. ieee80211_authenticate(struct ieee80211_work *wk)
  410. {
  411. struct ieee80211_sub_if_data *sdata = wk->sdata;
  412. struct ieee80211_local *local = sdata->local;
  413. wk->probe_auth.tries++;
  414. if (wk->probe_auth.tries > IEEE80211_AUTH_MAX_TRIES) {
  415. printk(KERN_DEBUG "%s: authentication with %pM"
  416. " timed out\n", sdata->name, wk->filter_ta);
  417. /*
  418. * Most likely AP is not in the range so remove the
  419. * bss struct for that AP.
  420. */
  421. ieee80211_remove_auth_bss(local, wk);
  422. return WORK_ACT_TIMEOUT;
  423. }
  424. printk(KERN_DEBUG "%s: authenticate with %pM (try %d)\n",
  425. sdata->name, wk->filter_ta, wk->probe_auth.tries);
  426. ieee80211_send_auth(sdata, 1, wk->probe_auth.algorithm, wk->ie,
  427. wk->ie_len, wk->filter_ta, NULL, 0, 0);
  428. wk->probe_auth.transaction = 2;
  429. wk->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  430. run_again(local, wk->timeout);
  431. return WORK_ACT_NONE;
  432. }
  433. static enum work_action __must_check
  434. ieee80211_associate(struct ieee80211_work *wk)
  435. {
  436. struct ieee80211_sub_if_data *sdata = wk->sdata;
  437. struct ieee80211_local *local = sdata->local;
  438. wk->assoc.tries++;
  439. if (wk->assoc.tries > IEEE80211_ASSOC_MAX_TRIES) {
  440. printk(KERN_DEBUG "%s: association with %pM"
  441. " timed out\n",
  442. sdata->name, wk->filter_ta);
  443. /*
  444. * Most likely AP is not in the range so remove the
  445. * bss struct for that AP.
  446. */
  447. if (wk->assoc.bss)
  448. cfg80211_unlink_bss(local->hw.wiphy, wk->assoc.bss);
  449. return WORK_ACT_TIMEOUT;
  450. }
  451. printk(KERN_DEBUG "%s: associate with %pM (try %d)\n",
  452. sdata->name, wk->filter_ta, wk->assoc.tries);
  453. ieee80211_send_assoc(sdata, wk);
  454. wk->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  455. run_again(local, wk->timeout);
  456. return WORK_ACT_NONE;
  457. }
  458. static enum work_action __must_check
  459. ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
  460. {
  461. /*
  462. * First time we run, do nothing -- the generic code will
  463. * have switched to the right channel etc.
  464. */
  465. if (!wk->started) {
  466. wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
  467. cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
  468. wk->chan, wk->chan_type,
  469. wk->remain.duration, GFP_KERNEL);
  470. return WORK_ACT_NONE;
  471. }
  472. return WORK_ACT_TIMEOUT;
  473. }
  474. static enum work_action __must_check
  475. ieee80211_offchannel_tx(struct ieee80211_work *wk)
  476. {
  477. if (!wk->started) {
  478. wk->timeout = jiffies + msecs_to_jiffies(wk->offchan_tx.wait);
  479. /*
  480. * After this, offchan_tx.frame remains but now is no
  481. * longer a valid pointer -- we still need it as the
  482. * cookie for canceling this work.
  483. */
  484. ieee80211_tx_skb(wk->sdata, wk->offchan_tx.frame);
  485. return WORK_ACT_NONE;
  486. }
  487. return WORK_ACT_TIMEOUT;
  488. }
  489. static enum work_action __must_check
  490. ieee80211_assoc_beacon_wait(struct ieee80211_work *wk)
  491. {
  492. if (wk->started)
  493. return WORK_ACT_TIMEOUT;
  494. /*
  495. * Wait up to one beacon interval ...
  496. * should this be more if we miss one?
  497. */
  498. printk(KERN_DEBUG "%s: waiting for beacon from %pM\n",
  499. wk->sdata->name, wk->filter_ta);
  500. wk->timeout = TU_TO_EXP_TIME(wk->assoc.bss->beacon_interval);
  501. return WORK_ACT_NONE;
  502. }
  503. static void ieee80211_auth_challenge(struct ieee80211_work *wk,
  504. struct ieee80211_mgmt *mgmt,
  505. size_t len)
  506. {
  507. struct ieee80211_sub_if_data *sdata = wk->sdata;
  508. u8 *pos;
  509. struct ieee802_11_elems elems;
  510. pos = mgmt->u.auth.variable;
  511. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  512. if (!elems.challenge)
  513. return;
  514. ieee80211_send_auth(sdata, 3, wk->probe_auth.algorithm,
  515. elems.challenge - 2, elems.challenge_len + 2,
  516. wk->filter_ta, wk->probe_auth.key,
  517. wk->probe_auth.key_len, wk->probe_auth.key_idx);
  518. wk->probe_auth.transaction = 4;
  519. }
  520. static enum work_action __must_check
  521. ieee80211_rx_mgmt_auth(struct ieee80211_work *wk,
  522. struct ieee80211_mgmt *mgmt, size_t len)
  523. {
  524. u16 auth_alg, auth_transaction, status_code;
  525. if (wk->type != IEEE80211_WORK_AUTH)
  526. return WORK_ACT_MISMATCH;
  527. if (len < 24 + 6)
  528. return WORK_ACT_NONE;
  529. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  530. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  531. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  532. if (auth_alg != wk->probe_auth.algorithm ||
  533. auth_transaction != wk->probe_auth.transaction)
  534. return WORK_ACT_NONE;
  535. if (status_code != WLAN_STATUS_SUCCESS) {
  536. printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
  537. wk->sdata->name, mgmt->sa, status_code);
  538. return WORK_ACT_DONE;
  539. }
  540. switch (wk->probe_auth.algorithm) {
  541. case WLAN_AUTH_OPEN:
  542. case WLAN_AUTH_LEAP:
  543. case WLAN_AUTH_FT:
  544. break;
  545. case WLAN_AUTH_SHARED_KEY:
  546. if (wk->probe_auth.transaction != 4) {
  547. ieee80211_auth_challenge(wk, mgmt, len);
  548. /* need another frame */
  549. return WORK_ACT_NONE;
  550. }
  551. break;
  552. default:
  553. WARN_ON(1);
  554. return WORK_ACT_NONE;
  555. }
  556. printk(KERN_DEBUG "%s: authenticated\n", wk->sdata->name);
  557. return WORK_ACT_DONE;
  558. }
  559. static enum work_action __must_check
  560. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_work *wk,
  561. struct ieee80211_mgmt *mgmt, size_t len,
  562. bool reassoc)
  563. {
  564. struct ieee80211_sub_if_data *sdata = wk->sdata;
  565. struct ieee80211_local *local = sdata->local;
  566. u16 capab_info, status_code, aid;
  567. struct ieee802_11_elems elems;
  568. u8 *pos;
  569. if (wk->type != IEEE80211_WORK_ASSOC)
  570. return WORK_ACT_MISMATCH;
  571. /*
  572. * AssocResp and ReassocResp have identical structure, so process both
  573. * of them in this function.
  574. */
  575. if (len < 24 + 6)
  576. return WORK_ACT_NONE;
  577. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  578. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  579. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  580. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  581. "status=%d aid=%d)\n",
  582. sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
  583. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  584. pos = mgmt->u.assoc_resp.variable;
  585. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  586. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  587. elems.timeout_int && elems.timeout_int_len == 5 &&
  588. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  589. u32 tu, ms;
  590. tu = get_unaligned_le32(elems.timeout_int + 1);
  591. ms = tu * 1024 / 1000;
  592. printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
  593. "comeback duration %u TU (%u ms)\n",
  594. sdata->name, mgmt->sa, tu, ms);
  595. wk->timeout = jiffies + msecs_to_jiffies(ms);
  596. if (ms > IEEE80211_ASSOC_TIMEOUT)
  597. run_again(local, wk->timeout);
  598. return WORK_ACT_NONE;
  599. }
  600. if (status_code != WLAN_STATUS_SUCCESS)
  601. printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
  602. sdata->name, mgmt->sa, status_code);
  603. else
  604. printk(KERN_DEBUG "%s: associated\n", sdata->name);
  605. return WORK_ACT_DONE;
  606. }
  607. static enum work_action __must_check
  608. ieee80211_rx_mgmt_probe_resp(struct ieee80211_work *wk,
  609. struct ieee80211_mgmt *mgmt, size_t len,
  610. struct ieee80211_rx_status *rx_status)
  611. {
  612. struct ieee80211_sub_if_data *sdata = wk->sdata;
  613. struct ieee80211_local *local = sdata->local;
  614. size_t baselen;
  615. ASSERT_WORK_MTX(local);
  616. if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
  617. return WORK_ACT_MISMATCH;
  618. if (len < 24 + 12)
  619. return WORK_ACT_NONE;
  620. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  621. if (baselen > len)
  622. return WORK_ACT_NONE;
  623. printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
  624. return WORK_ACT_DONE;
  625. }
  626. static enum work_action __must_check
  627. ieee80211_rx_mgmt_beacon(struct ieee80211_work *wk,
  628. struct ieee80211_mgmt *mgmt, size_t len)
  629. {
  630. struct ieee80211_sub_if_data *sdata = wk->sdata;
  631. struct ieee80211_local *local = sdata->local;
  632. ASSERT_WORK_MTX(local);
  633. if (wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
  634. return WORK_ACT_MISMATCH;
  635. if (len < 24 + 12)
  636. return WORK_ACT_NONE;
  637. printk(KERN_DEBUG "%s: beacon received\n", sdata->name);
  638. return WORK_ACT_DONE;
  639. }
  640. static void ieee80211_work_rx_queued_mgmt(struct ieee80211_local *local,
  641. struct sk_buff *skb)
  642. {
  643. struct ieee80211_rx_status *rx_status;
  644. struct ieee80211_mgmt *mgmt;
  645. struct ieee80211_work *wk;
  646. enum work_action rma = WORK_ACT_NONE;
  647. u16 fc;
  648. rx_status = (struct ieee80211_rx_status *) skb->cb;
  649. mgmt = (struct ieee80211_mgmt *) skb->data;
  650. fc = le16_to_cpu(mgmt->frame_control);
  651. mutex_lock(&local->mtx);
  652. list_for_each_entry(wk, &local->work_list, list) {
  653. const u8 *bssid = NULL;
  654. switch (wk->type) {
  655. case IEEE80211_WORK_DIRECT_PROBE:
  656. case IEEE80211_WORK_AUTH:
  657. case IEEE80211_WORK_ASSOC:
  658. case IEEE80211_WORK_ASSOC_BEACON_WAIT:
  659. bssid = wk->filter_ta;
  660. break;
  661. default:
  662. continue;
  663. }
  664. /*
  665. * Before queuing, we already verified mgmt->sa,
  666. * so this is needed just for matching.
  667. */
  668. if (compare_ether_addr(bssid, mgmt->bssid))
  669. continue;
  670. switch (fc & IEEE80211_FCTL_STYPE) {
  671. case IEEE80211_STYPE_BEACON:
  672. rma = ieee80211_rx_mgmt_beacon(wk, mgmt, skb->len);
  673. break;
  674. case IEEE80211_STYPE_PROBE_RESP:
  675. rma = ieee80211_rx_mgmt_probe_resp(wk, mgmt, skb->len,
  676. rx_status);
  677. break;
  678. case IEEE80211_STYPE_AUTH:
  679. rma = ieee80211_rx_mgmt_auth(wk, mgmt, skb->len);
  680. break;
  681. case IEEE80211_STYPE_ASSOC_RESP:
  682. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  683. skb->len, false);
  684. break;
  685. case IEEE80211_STYPE_REASSOC_RESP:
  686. rma = ieee80211_rx_mgmt_assoc_resp(wk, mgmt,
  687. skb->len, true);
  688. break;
  689. default:
  690. WARN_ON(1);
  691. rma = WORK_ACT_NONE;
  692. }
  693. /*
  694. * We've either received an unexpected frame, or we have
  695. * multiple work items and need to match the frame to the
  696. * right one.
  697. */
  698. if (rma == WORK_ACT_MISMATCH)
  699. continue;
  700. /*
  701. * We've processed this frame for that work, so it can't
  702. * belong to another work struct.
  703. * NB: this is also required for correctness for 'rma'!
  704. */
  705. break;
  706. }
  707. switch (rma) {
  708. case WORK_ACT_MISMATCH:
  709. /* ignore this unmatched frame */
  710. break;
  711. case WORK_ACT_NONE:
  712. break;
  713. case WORK_ACT_DONE:
  714. list_del_rcu(&wk->list);
  715. break;
  716. default:
  717. WARN(1, "unexpected: %d", rma);
  718. }
  719. mutex_unlock(&local->mtx);
  720. if (rma != WORK_ACT_DONE)
  721. goto out;
  722. switch (wk->done(wk, skb)) {
  723. case WORK_DONE_DESTROY:
  724. free_work(wk);
  725. break;
  726. case WORK_DONE_REQUEUE:
  727. synchronize_rcu();
  728. wk->started = false; /* restart */
  729. mutex_lock(&local->mtx);
  730. list_add_tail(&wk->list, &local->work_list);
  731. mutex_unlock(&local->mtx);
  732. }
  733. out:
  734. kfree_skb(skb);
  735. }
  736. static void ieee80211_work_timer(unsigned long data)
  737. {
  738. struct ieee80211_local *local = (void *) data;
  739. if (local->quiescing)
  740. return;
  741. ieee80211_queue_work(&local->hw, &local->work_work);
  742. }
  743. static void ieee80211_work_work(struct work_struct *work)
  744. {
  745. struct ieee80211_local *local =
  746. container_of(work, struct ieee80211_local, work_work);
  747. struct sk_buff *skb;
  748. struct ieee80211_work *wk, *tmp;
  749. LIST_HEAD(free_work);
  750. enum work_action rma;
  751. bool remain_off_channel = false;
  752. if (local->scanning)
  753. return;
  754. /*
  755. * ieee80211_queue_work() should have picked up most cases,
  756. * here we'll pick the rest.
  757. */
  758. if (WARN(local->suspended, "work scheduled while going to suspend\n"))
  759. return;
  760. /* first process frames to avoid timing out while a frame is pending */
  761. while ((skb = skb_dequeue(&local->work_skb_queue)))
  762. ieee80211_work_rx_queued_mgmt(local, skb);
  763. mutex_lock(&local->mtx);
  764. ieee80211_recalc_idle(local);
  765. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  766. bool started = wk->started;
  767. /* mark work as started if it's on the current off-channel */
  768. if (!started && local->tmp_channel &&
  769. wk->chan == local->tmp_channel &&
  770. wk->chan_type == local->tmp_channel_type) {
  771. started = true;
  772. wk->timeout = jiffies;
  773. }
  774. if (!started && !local->tmp_channel) {
  775. /*
  776. * TODO: could optimize this by leaving the
  777. * station vifs in awake mode if they
  778. * happen to be on the same channel as
  779. * the requested channel
  780. */
  781. ieee80211_offchannel_stop_beaconing(local);
  782. ieee80211_offchannel_stop_station(local);
  783. local->tmp_channel = wk->chan;
  784. local->tmp_channel_type = wk->chan_type;
  785. ieee80211_hw_config(local, 0);
  786. started = true;
  787. wk->timeout = jiffies;
  788. }
  789. /* don't try to work with items that aren't started */
  790. if (!started)
  791. continue;
  792. if (time_is_after_jiffies(wk->timeout)) {
  793. /*
  794. * This work item isn't supposed to be worked on
  795. * right now, but take care to adjust the timer
  796. * properly.
  797. */
  798. run_again(local, wk->timeout);
  799. continue;
  800. }
  801. switch (wk->type) {
  802. default:
  803. WARN_ON(1);
  804. /* nothing */
  805. rma = WORK_ACT_NONE;
  806. break;
  807. case IEEE80211_WORK_ABORT:
  808. rma = WORK_ACT_TIMEOUT;
  809. break;
  810. case IEEE80211_WORK_DIRECT_PROBE:
  811. rma = ieee80211_direct_probe(wk);
  812. break;
  813. case IEEE80211_WORK_AUTH:
  814. rma = ieee80211_authenticate(wk);
  815. break;
  816. case IEEE80211_WORK_ASSOC:
  817. rma = ieee80211_associate(wk);
  818. break;
  819. case IEEE80211_WORK_REMAIN_ON_CHANNEL:
  820. rma = ieee80211_remain_on_channel_timeout(wk);
  821. break;
  822. case IEEE80211_WORK_OFFCHANNEL_TX:
  823. rma = ieee80211_offchannel_tx(wk);
  824. break;
  825. case IEEE80211_WORK_ASSOC_BEACON_WAIT:
  826. rma = ieee80211_assoc_beacon_wait(wk);
  827. break;
  828. }
  829. wk->started = started;
  830. switch (rma) {
  831. case WORK_ACT_NONE:
  832. /* might have changed the timeout */
  833. run_again(local, wk->timeout);
  834. break;
  835. case WORK_ACT_TIMEOUT:
  836. list_del_rcu(&wk->list);
  837. synchronize_rcu();
  838. list_add(&wk->list, &free_work);
  839. break;
  840. default:
  841. WARN(1, "unexpected: %d", rma);
  842. }
  843. }
  844. list_for_each_entry(wk, &local->work_list, list) {
  845. if (!wk->started)
  846. continue;
  847. if (wk->chan != local->tmp_channel)
  848. continue;
  849. if (wk->chan_type != local->tmp_channel_type)
  850. continue;
  851. remain_off_channel = true;
  852. }
  853. if (!remain_off_channel && local->tmp_channel) {
  854. local->tmp_channel = NULL;
  855. ieee80211_hw_config(local, 0);
  856. ieee80211_offchannel_return(local, true);
  857. /* give connection some time to breathe */
  858. run_again(local, jiffies + HZ/2);
  859. }
  860. if (list_empty(&local->work_list) && local->scan_req &&
  861. !local->scanning)
  862. ieee80211_queue_delayed_work(&local->hw,
  863. &local->scan_work,
  864. round_jiffies_relative(0));
  865. ieee80211_recalc_idle(local);
  866. mutex_unlock(&local->mtx);
  867. list_for_each_entry_safe(wk, tmp, &free_work, list) {
  868. wk->done(wk, NULL);
  869. list_del(&wk->list);
  870. kfree(wk);
  871. }
  872. }
  873. void ieee80211_add_work(struct ieee80211_work *wk)
  874. {
  875. struct ieee80211_local *local;
  876. if (WARN_ON(!wk->chan))
  877. return;
  878. if (WARN_ON(!wk->sdata))
  879. return;
  880. if (WARN_ON(!wk->done))
  881. return;
  882. if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
  883. return;
  884. wk->started = false;
  885. local = wk->sdata->local;
  886. mutex_lock(&local->mtx);
  887. list_add_tail(&wk->list, &local->work_list);
  888. mutex_unlock(&local->mtx);
  889. ieee80211_queue_work(&local->hw, &local->work_work);
  890. }
  891. void ieee80211_work_init(struct ieee80211_local *local)
  892. {
  893. INIT_LIST_HEAD(&local->work_list);
  894. setup_timer(&local->work_timer, ieee80211_work_timer,
  895. (unsigned long)local);
  896. INIT_WORK(&local->work_work, ieee80211_work_work);
  897. skb_queue_head_init(&local->work_skb_queue);
  898. }
  899. void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
  900. {
  901. struct ieee80211_local *local = sdata->local;
  902. struct ieee80211_work *wk;
  903. bool cleanup = false;
  904. mutex_lock(&local->mtx);
  905. list_for_each_entry(wk, &local->work_list, list) {
  906. if (wk->sdata != sdata)
  907. continue;
  908. cleanup = true;
  909. wk->type = IEEE80211_WORK_ABORT;
  910. wk->started = true;
  911. wk->timeout = jiffies;
  912. }
  913. mutex_unlock(&local->mtx);
  914. /* run cleanups etc. */
  915. if (cleanup)
  916. ieee80211_work_work(&local->work_work);
  917. mutex_lock(&local->mtx);
  918. list_for_each_entry(wk, &local->work_list, list) {
  919. if (wk->sdata != sdata)
  920. continue;
  921. WARN_ON(1);
  922. break;
  923. }
  924. mutex_unlock(&local->mtx);
  925. }
  926. ieee80211_rx_result ieee80211_work_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  927. struct sk_buff *skb)
  928. {
  929. struct ieee80211_local *local = sdata->local;
  930. struct ieee80211_mgmt *mgmt;
  931. struct ieee80211_work *wk;
  932. u16 fc;
  933. if (skb->len < 24)
  934. return RX_DROP_MONITOR;
  935. mgmt = (struct ieee80211_mgmt *) skb->data;
  936. fc = le16_to_cpu(mgmt->frame_control);
  937. list_for_each_entry_rcu(wk, &local->work_list, list) {
  938. if (sdata != wk->sdata)
  939. continue;
  940. if (compare_ether_addr(wk->filter_ta, mgmt->sa))
  941. continue;
  942. if (compare_ether_addr(wk->filter_ta, mgmt->bssid))
  943. continue;
  944. switch (fc & IEEE80211_FCTL_STYPE) {
  945. case IEEE80211_STYPE_AUTH:
  946. case IEEE80211_STYPE_PROBE_RESP:
  947. case IEEE80211_STYPE_ASSOC_RESP:
  948. case IEEE80211_STYPE_REASSOC_RESP:
  949. case IEEE80211_STYPE_BEACON:
  950. skb_queue_tail(&local->work_skb_queue, skb);
  951. ieee80211_queue_work(&local->hw, &local->work_work);
  952. return RX_QUEUED;
  953. }
  954. }
  955. return RX_CONTINUE;
  956. }
  957. static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
  958. struct sk_buff *skb)
  959. {
  960. /*
  961. * We are done serving the remain-on-channel command.
  962. */
  963. cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
  964. wk->chan, wk->chan_type,
  965. GFP_KERNEL);
  966. return WORK_DONE_DESTROY;
  967. }
  968. int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  969. struct ieee80211_channel *chan,
  970. enum nl80211_channel_type channel_type,
  971. unsigned int duration, u64 *cookie)
  972. {
  973. struct ieee80211_work *wk;
  974. wk = kzalloc(sizeof(*wk), GFP_KERNEL);
  975. if (!wk)
  976. return -ENOMEM;
  977. wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
  978. wk->chan = chan;
  979. wk->chan_type = channel_type;
  980. wk->sdata = sdata;
  981. wk->done = ieee80211_remain_done;
  982. wk->remain.duration = duration;
  983. *cookie = (unsigned long) wk;
  984. ieee80211_add_work(wk);
  985. return 0;
  986. }
  987. int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
  988. u64 cookie)
  989. {
  990. struct ieee80211_local *local = sdata->local;
  991. struct ieee80211_work *wk, *tmp;
  992. bool found = false;
  993. mutex_lock(&local->mtx);
  994. list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
  995. if ((unsigned long) wk == cookie) {
  996. wk->timeout = jiffies;
  997. found = true;
  998. break;
  999. }
  1000. }
  1001. mutex_unlock(&local->mtx);
  1002. if (!found)
  1003. return -ENOENT;
  1004. ieee80211_queue_work(&local->hw, &local->work_work);
  1005. return 0;
  1006. }