work.c 29 KB

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