work.c 28 KB

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