work.c 30 KB

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