work.c 30 KB

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