work.c 28 KB

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