mlme.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276
  1. /*
  2. * BSS client mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/pm_qos_params.h>
  20. #include <linux/crc32.h>
  21. #include <net/mac80211.h>
  22. #include <asm/unaligned.h>
  23. #include "ieee80211_i.h"
  24. #include "driver-ops.h"
  25. #include "rate.h"
  26. #include "led.h"
  27. #define IEEE80211_ASSOC_SCANS_MAX_TRIES 2
  28. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  29. #define IEEE80211_AUTH_MAX_TRIES 3
  30. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  31. #define IEEE80211_ASSOC_MAX_TRIES 3
  32. #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
  33. #define IEEE80211_PROBE_WAIT (HZ / 5)
  34. #define IEEE80211_PROBE_IDLE_TIME (60 * HZ)
  35. #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
  36. #define TMR_RUNNING_TIMER 0
  37. #define TMR_RUNNING_CHANSW 1
  38. /* utils */
  39. static int ecw2cw(int ecw)
  40. {
  41. return (1 << ecw) - 1;
  42. }
  43. static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
  44. struct ieee80211_supported_band *sband,
  45. u32 *rates)
  46. {
  47. int i, j, count;
  48. *rates = 0;
  49. count = 0;
  50. for (i = 0; i < bss->supp_rates_len; i++) {
  51. int rate = (bss->supp_rates[i] & 0x7F) * 5;
  52. for (j = 0; j < sband->n_bitrates; j++)
  53. if (sband->bitrates[j].bitrate == rate) {
  54. *rates |= BIT(j);
  55. count++;
  56. break;
  57. }
  58. }
  59. return count;
  60. }
  61. /*
  62. * ieee80211_enable_ht should be called only after the operating band
  63. * has been determined as ht configuration depends on the hw's
  64. * HT abilities for a specific band.
  65. */
  66. static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
  67. struct ieee80211_ht_info *hti,
  68. u16 ap_ht_cap_flags)
  69. {
  70. struct ieee80211_local *local = sdata->local;
  71. struct ieee80211_supported_band *sband;
  72. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  73. struct sta_info *sta;
  74. u32 changed = 0;
  75. u16 ht_opmode;
  76. bool enable_ht = true, ht_changed;
  77. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  78. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  79. /* HT is not supported */
  80. if (!sband->ht_cap.ht_supported)
  81. enable_ht = false;
  82. /* check that channel matches the right operating channel */
  83. if (local->hw.conf.channel->center_freq !=
  84. ieee80211_channel_to_frequency(hti->control_chan))
  85. enable_ht = false;
  86. if (enable_ht) {
  87. channel_type = NL80211_CHAN_HT20;
  88. if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
  89. (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
  90. (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
  91. switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  92. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  93. if (!(local->hw.conf.channel->flags &
  94. IEEE80211_CHAN_NO_HT40PLUS))
  95. channel_type = NL80211_CHAN_HT40PLUS;
  96. break;
  97. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  98. if (!(local->hw.conf.channel->flags &
  99. IEEE80211_CHAN_NO_HT40MINUS))
  100. channel_type = NL80211_CHAN_HT40MINUS;
  101. break;
  102. }
  103. }
  104. }
  105. ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
  106. channel_type != local->hw.conf.channel_type;
  107. local->oper_channel_type = channel_type;
  108. if (ht_changed) {
  109. /* channel_type change automatically detected */
  110. ieee80211_hw_config(local, 0);
  111. rcu_read_lock();
  112. sta = sta_info_get(local, ifmgd->bssid);
  113. if (sta)
  114. rate_control_rate_update(local, sband, sta,
  115. IEEE80211_RC_HT_CHANGED);
  116. rcu_read_unlock();
  117. }
  118. /* disable HT */
  119. if (!enable_ht)
  120. return 0;
  121. ht_opmode = le16_to_cpu(hti->operation_mode);
  122. /* if bss configuration changed store the new one */
  123. if (!sdata->ht_opmode_valid ||
  124. sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
  125. changed |= BSS_CHANGED_HT;
  126. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  127. sdata->ht_opmode_valid = true;
  128. }
  129. return changed;
  130. }
  131. /* frame sending functions */
  132. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
  133. {
  134. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  135. struct ieee80211_local *local = sdata->local;
  136. struct sk_buff *skb;
  137. struct ieee80211_mgmt *mgmt;
  138. u8 *pos;
  139. const u8 *ies, *ht_ie;
  140. int i, len, count, rates_len, supp_rates_len;
  141. u16 capab;
  142. struct ieee80211_bss *bss;
  143. int wmm = 0;
  144. struct ieee80211_supported_band *sband;
  145. u32 rates = 0;
  146. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  147. sizeof(*mgmt) + 200 + ifmgd->extra_ie_len +
  148. ifmgd->ssid_len);
  149. if (!skb) {
  150. printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
  151. "frame\n", sdata->dev->name);
  152. return;
  153. }
  154. skb_reserve(skb, local->hw.extra_tx_headroom);
  155. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  156. capab = ifmgd->capab;
  157. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
  158. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  159. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  160. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  161. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  162. }
  163. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  164. local->hw.conf.channel->center_freq,
  165. ifmgd->ssid, ifmgd->ssid_len);
  166. if (bss) {
  167. if (bss->cbss.capability & WLAN_CAPABILITY_PRIVACY)
  168. capab |= WLAN_CAPABILITY_PRIVACY;
  169. if (bss->wmm_used)
  170. wmm = 1;
  171. /* get all rates supported by the device and the AP as
  172. * some APs don't like getting a superset of their rates
  173. * in the association request (e.g. D-Link DAP 1353 in
  174. * b-only mode) */
  175. rates_len = ieee80211_compatible_rates(bss, sband, &rates);
  176. if ((bss->cbss.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  177. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  178. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  179. ieee80211_rx_bss_put(local, bss);
  180. } else {
  181. rates = ~0;
  182. rates_len = sband->n_bitrates;
  183. }
  184. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  185. memset(mgmt, 0, 24);
  186. memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
  187. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  188. memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
  189. if (ifmgd->flags & IEEE80211_STA_PREV_BSSID_SET) {
  190. skb_put(skb, 10);
  191. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  192. IEEE80211_STYPE_REASSOC_REQ);
  193. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  194. mgmt->u.reassoc_req.listen_interval =
  195. cpu_to_le16(local->hw.conf.listen_interval);
  196. memcpy(mgmt->u.reassoc_req.current_ap, ifmgd->prev_bssid,
  197. ETH_ALEN);
  198. } else {
  199. skb_put(skb, 4);
  200. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  201. IEEE80211_STYPE_ASSOC_REQ);
  202. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  203. mgmt->u.assoc_req.listen_interval =
  204. cpu_to_le16(local->hw.conf.listen_interval);
  205. }
  206. /* SSID */
  207. ies = pos = skb_put(skb, 2 + ifmgd->ssid_len);
  208. *pos++ = WLAN_EID_SSID;
  209. *pos++ = ifmgd->ssid_len;
  210. memcpy(pos, ifmgd->ssid, ifmgd->ssid_len);
  211. /* add all rates which were marked to be used above */
  212. supp_rates_len = rates_len;
  213. if (supp_rates_len > 8)
  214. supp_rates_len = 8;
  215. len = sband->n_bitrates;
  216. pos = skb_put(skb, supp_rates_len + 2);
  217. *pos++ = WLAN_EID_SUPP_RATES;
  218. *pos++ = supp_rates_len;
  219. count = 0;
  220. for (i = 0; i < sband->n_bitrates; i++) {
  221. if (BIT(i) & rates) {
  222. int rate = sband->bitrates[i].bitrate;
  223. *pos++ = (u8) (rate / 5);
  224. if (++count == 8)
  225. break;
  226. }
  227. }
  228. if (rates_len > count) {
  229. pos = skb_put(skb, rates_len - count + 2);
  230. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  231. *pos++ = rates_len - count;
  232. for (i++; i < sband->n_bitrates; i++) {
  233. if (BIT(i) & rates) {
  234. int rate = sband->bitrates[i].bitrate;
  235. *pos++ = (u8) (rate / 5);
  236. }
  237. }
  238. }
  239. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  240. /* 1. power capabilities */
  241. pos = skb_put(skb, 4);
  242. *pos++ = WLAN_EID_PWR_CAPABILITY;
  243. *pos++ = 2;
  244. *pos++ = 0; /* min tx power */
  245. *pos++ = local->hw.conf.channel->max_power; /* max tx power */
  246. /* 2. supported channels */
  247. /* TODO: get this in reg domain format */
  248. pos = skb_put(skb, 2 * sband->n_channels + 2);
  249. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  250. *pos++ = 2 * sband->n_channels;
  251. for (i = 0; i < sband->n_channels; i++) {
  252. *pos++ = ieee80211_frequency_to_channel(
  253. sband->channels[i].center_freq);
  254. *pos++ = 1; /* one channel in the subband*/
  255. }
  256. }
  257. if (ifmgd->extra_ie) {
  258. pos = skb_put(skb, ifmgd->extra_ie_len);
  259. memcpy(pos, ifmgd->extra_ie, ifmgd->extra_ie_len);
  260. }
  261. if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED)) {
  262. pos = skb_put(skb, 9);
  263. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  264. *pos++ = 7; /* len */
  265. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  266. *pos++ = 0x50;
  267. *pos++ = 0xf2;
  268. *pos++ = 2; /* WME */
  269. *pos++ = 0; /* WME info */
  270. *pos++ = 1; /* WME ver */
  271. *pos++ = 0;
  272. }
  273. /* wmm support is a must to HT */
  274. /*
  275. * IEEE802.11n does not allow TKIP/WEP as pairwise
  276. * ciphers in HT mode. We still associate in non-ht
  277. * mode (11a/b/g) if any one of these ciphers is
  278. * configured as pairwise.
  279. */
  280. if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
  281. sband->ht_cap.ht_supported &&
  282. (ht_ie = ieee80211_bss_get_ie(&bss->cbss, WLAN_EID_HT_INFORMATION)) &&
  283. ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
  284. (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))) {
  285. struct ieee80211_ht_info *ht_info =
  286. (struct ieee80211_ht_info *)(ht_ie + 2);
  287. u16 cap = sband->ht_cap.cap;
  288. __le16 tmp;
  289. u32 flags = local->hw.conf.channel->flags;
  290. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  291. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  292. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  293. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  294. cap &= ~IEEE80211_HT_CAP_SGI_40;
  295. }
  296. break;
  297. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  298. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  299. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  300. cap &= ~IEEE80211_HT_CAP_SGI_40;
  301. }
  302. break;
  303. }
  304. tmp = cpu_to_le16(cap);
  305. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
  306. *pos++ = WLAN_EID_HT_CAPABILITY;
  307. *pos++ = sizeof(struct ieee80211_ht_cap);
  308. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  309. memcpy(pos, &tmp, sizeof(u16));
  310. pos += sizeof(u16);
  311. /* TODO: needs a define here for << 2 */
  312. *pos++ = sband->ht_cap.ampdu_factor |
  313. (sband->ht_cap.ampdu_density << 2);
  314. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  315. }
  316. kfree(ifmgd->assocreq_ies);
  317. ifmgd->assocreq_ies_len = (skb->data + skb->len) - ies;
  318. ifmgd->assocreq_ies = kmalloc(ifmgd->assocreq_ies_len, GFP_KERNEL);
  319. if (ifmgd->assocreq_ies)
  320. memcpy(ifmgd->assocreq_ies, ies, ifmgd->assocreq_ies_len);
  321. ieee80211_tx_skb(sdata, skb, 0);
  322. }
  323. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  324. u16 stype, u16 reason)
  325. {
  326. struct ieee80211_local *local = sdata->local;
  327. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  328. struct sk_buff *skb;
  329. struct ieee80211_mgmt *mgmt;
  330. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
  331. if (!skb) {
  332. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  333. "deauth/disassoc frame\n", sdata->dev->name);
  334. return;
  335. }
  336. skb_reserve(skb, local->hw.extra_tx_headroom);
  337. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  338. memset(mgmt, 0, 24);
  339. memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
  340. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  341. memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
  342. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  343. skb_put(skb, 2);
  344. /* u.deauth.reason_code == u.disassoc.reason_code */
  345. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  346. if (stype == IEEE80211_STYPE_DEAUTH)
  347. cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, skb->len,
  348. GFP_KERNEL);
  349. else
  350. cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, skb->len,
  351. GFP_KERNEL);
  352. ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
  353. }
  354. void ieee80211_send_pspoll(struct ieee80211_local *local,
  355. struct ieee80211_sub_if_data *sdata)
  356. {
  357. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  358. struct ieee80211_pspoll *pspoll;
  359. struct sk_buff *skb;
  360. u16 fc;
  361. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
  362. if (!skb) {
  363. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  364. "pspoll frame\n", sdata->dev->name);
  365. return;
  366. }
  367. skb_reserve(skb, local->hw.extra_tx_headroom);
  368. pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
  369. memset(pspoll, 0, sizeof(*pspoll));
  370. fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
  371. pspoll->frame_control = cpu_to_le16(fc);
  372. pspoll->aid = cpu_to_le16(ifmgd->aid);
  373. /* aid in PS-Poll has its two MSBs each set to 1 */
  374. pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
  375. memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
  376. memcpy(pspoll->ta, sdata->dev->dev_addr, ETH_ALEN);
  377. ieee80211_tx_skb(sdata, skb, 0);
  378. }
  379. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  380. struct ieee80211_sub_if_data *sdata,
  381. int powersave)
  382. {
  383. struct sk_buff *skb;
  384. struct ieee80211_hdr *nullfunc;
  385. __le16 fc;
  386. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  387. return;
  388. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
  389. if (!skb) {
  390. printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
  391. "frame\n", sdata->dev->name);
  392. return;
  393. }
  394. skb_reserve(skb, local->hw.extra_tx_headroom);
  395. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
  396. memset(nullfunc, 0, 24);
  397. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  398. IEEE80211_FCTL_TODS);
  399. if (powersave)
  400. fc |= cpu_to_le16(IEEE80211_FCTL_PM);
  401. nullfunc->frame_control = fc;
  402. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  403. memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
  404. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  405. ieee80211_tx_skb(sdata, skb, 0);
  406. }
  407. /* spectrum management related things */
  408. static void ieee80211_chswitch_work(struct work_struct *work)
  409. {
  410. struct ieee80211_sub_if_data *sdata =
  411. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  412. struct ieee80211_bss *bss;
  413. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  414. if (!netif_running(sdata->dev))
  415. return;
  416. bss = ieee80211_rx_bss_get(sdata->local, ifmgd->bssid,
  417. sdata->local->hw.conf.channel->center_freq,
  418. ifmgd->ssid, ifmgd->ssid_len);
  419. if (!bss)
  420. goto exit;
  421. sdata->local->oper_channel = sdata->local->csa_channel;
  422. /* XXX: shouldn't really modify cfg80211-owned data! */
  423. if (!ieee80211_hw_config(sdata->local, IEEE80211_CONF_CHANGE_CHANNEL))
  424. bss->cbss.channel = sdata->local->oper_channel;
  425. ieee80211_rx_bss_put(sdata->local, bss);
  426. exit:
  427. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  428. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  429. IEEE80211_QUEUE_STOP_REASON_CSA);
  430. }
  431. static void ieee80211_chswitch_timer(unsigned long data)
  432. {
  433. struct ieee80211_sub_if_data *sdata =
  434. (struct ieee80211_sub_if_data *) data;
  435. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  436. if (sdata->local->quiescing) {
  437. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  438. return;
  439. }
  440. queue_work(sdata->local->hw.workqueue, &ifmgd->chswitch_work);
  441. }
  442. void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  443. struct ieee80211_channel_sw_ie *sw_elem,
  444. struct ieee80211_bss *bss)
  445. {
  446. struct ieee80211_channel *new_ch;
  447. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  448. int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
  449. if (ifmgd->state != IEEE80211_STA_MLME_ASSOCIATED)
  450. return;
  451. if (sdata->local->sw_scanning || sdata->local->hw_scanning)
  452. return;
  453. /* Disregard subsequent beacons if we are already running a timer
  454. processing a CSA */
  455. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  456. return;
  457. new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  458. if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
  459. return;
  460. sdata->local->csa_channel = new_ch;
  461. if (sw_elem->count <= 1) {
  462. queue_work(sdata->local->hw.workqueue, &ifmgd->chswitch_work);
  463. } else {
  464. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  465. IEEE80211_QUEUE_STOP_REASON_CSA);
  466. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  467. mod_timer(&ifmgd->chswitch_timer,
  468. jiffies +
  469. msecs_to_jiffies(sw_elem->count *
  470. bss->cbss.beacon_interval));
  471. }
  472. }
  473. static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  474. u16 capab_info, u8 *pwr_constr_elem,
  475. u8 pwr_constr_elem_len)
  476. {
  477. struct ieee80211_conf *conf = &sdata->local->hw.conf;
  478. if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
  479. return;
  480. /* Power constraint IE length should be 1 octet */
  481. if (pwr_constr_elem_len != 1)
  482. return;
  483. if ((*pwr_constr_elem <= conf->channel->max_power) &&
  484. (*pwr_constr_elem != sdata->local->power_constr_level)) {
  485. sdata->local->power_constr_level = *pwr_constr_elem;
  486. ieee80211_hw_config(sdata->local, 0);
  487. }
  488. }
  489. /* powersave */
  490. static void ieee80211_enable_ps(struct ieee80211_local *local,
  491. struct ieee80211_sub_if_data *sdata)
  492. {
  493. struct ieee80211_conf *conf = &local->hw.conf;
  494. /*
  495. * If we are scanning right now then the parameters will
  496. * take effect when scan finishes.
  497. */
  498. if (local->hw_scanning || local->sw_scanning)
  499. return;
  500. if (conf->dynamic_ps_timeout > 0 &&
  501. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  502. mod_timer(&local->dynamic_ps_timer, jiffies +
  503. msecs_to_jiffies(conf->dynamic_ps_timeout));
  504. } else {
  505. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  506. ieee80211_send_nullfunc(local, sdata, 1);
  507. conf->flags |= IEEE80211_CONF_PS;
  508. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  509. }
  510. }
  511. static void ieee80211_change_ps(struct ieee80211_local *local)
  512. {
  513. struct ieee80211_conf *conf = &local->hw.conf;
  514. if (local->ps_sdata) {
  515. ieee80211_enable_ps(local, local->ps_sdata);
  516. } else if (conf->flags & IEEE80211_CONF_PS) {
  517. conf->flags &= ~IEEE80211_CONF_PS;
  518. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  519. del_timer_sync(&local->dynamic_ps_timer);
  520. cancel_work_sync(&local->dynamic_ps_enable_work);
  521. }
  522. }
  523. /* need to hold RTNL or interface lock */
  524. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  525. {
  526. struct ieee80211_sub_if_data *sdata, *found = NULL;
  527. int count = 0;
  528. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  529. local->ps_sdata = NULL;
  530. return;
  531. }
  532. list_for_each_entry(sdata, &local->interfaces, list) {
  533. if (!netif_running(sdata->dev))
  534. continue;
  535. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  536. continue;
  537. found = sdata;
  538. count++;
  539. }
  540. if (count == 1 && found->u.mgd.powersave &&
  541. (found->u.mgd.flags & IEEE80211_STA_ASSOCIATED) &&
  542. !(found->u.mgd.flags & IEEE80211_STA_PROBEREQ_POLL)) {
  543. s32 beaconint_us;
  544. if (latency < 0)
  545. latency = pm_qos_requirement(PM_QOS_NETWORK_LATENCY);
  546. beaconint_us = ieee80211_tu_to_usec(
  547. found->vif.bss_conf.beacon_int);
  548. if (beaconint_us > latency) {
  549. local->ps_sdata = NULL;
  550. } else {
  551. u8 dtimper = found->vif.bss_conf.dtim_period;
  552. int maxslp = 1;
  553. if (dtimper > 1)
  554. maxslp = min_t(int, dtimper,
  555. latency / beaconint_us);
  556. local->hw.conf.max_sleep_period = maxslp;
  557. local->ps_sdata = found;
  558. }
  559. } else {
  560. local->ps_sdata = NULL;
  561. }
  562. ieee80211_change_ps(local);
  563. }
  564. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  565. {
  566. struct ieee80211_local *local =
  567. container_of(work, struct ieee80211_local,
  568. dynamic_ps_disable_work);
  569. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  570. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  571. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  572. }
  573. ieee80211_wake_queues_by_reason(&local->hw,
  574. IEEE80211_QUEUE_STOP_REASON_PS);
  575. }
  576. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  577. {
  578. struct ieee80211_local *local =
  579. container_of(work, struct ieee80211_local,
  580. dynamic_ps_enable_work);
  581. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  582. /* can only happen when PS was just disabled anyway */
  583. if (!sdata)
  584. return;
  585. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  586. return;
  587. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  588. ieee80211_send_nullfunc(local, sdata, 1);
  589. local->hw.conf.flags |= IEEE80211_CONF_PS;
  590. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  591. }
  592. void ieee80211_dynamic_ps_timer(unsigned long data)
  593. {
  594. struct ieee80211_local *local = (void *) data;
  595. if (local->quiescing)
  596. return;
  597. queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
  598. }
  599. /* MLME */
  600. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  601. struct ieee80211_if_managed *ifmgd,
  602. u8 *wmm_param, size_t wmm_param_len)
  603. {
  604. struct ieee80211_tx_queue_params params;
  605. size_t left;
  606. int count;
  607. u8 *pos;
  608. if (!(ifmgd->flags & IEEE80211_STA_WMM_ENABLED))
  609. return;
  610. if (!wmm_param)
  611. return;
  612. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  613. return;
  614. count = wmm_param[6] & 0x0f;
  615. if (count == ifmgd->wmm_last_param_set)
  616. return;
  617. ifmgd->wmm_last_param_set = count;
  618. pos = wmm_param + 8;
  619. left = wmm_param_len - 8;
  620. memset(&params, 0, sizeof(params));
  621. local->wmm_acm = 0;
  622. for (; left >= 4; left -= 4, pos += 4) {
  623. int aci = (pos[0] >> 5) & 0x03;
  624. int acm = (pos[0] >> 4) & 0x01;
  625. int queue;
  626. switch (aci) {
  627. case 1: /* AC_BK */
  628. queue = 3;
  629. if (acm)
  630. local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  631. break;
  632. case 2: /* AC_VI */
  633. queue = 1;
  634. if (acm)
  635. local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  636. break;
  637. case 3: /* AC_VO */
  638. queue = 0;
  639. if (acm)
  640. local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  641. break;
  642. case 0: /* AC_BE */
  643. default:
  644. queue = 2;
  645. if (acm)
  646. local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  647. break;
  648. }
  649. params.aifs = pos[0] & 0x0f;
  650. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  651. params.cw_min = ecw2cw(pos[1] & 0x0f);
  652. params.txop = get_unaligned_le16(pos + 2);
  653. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  654. printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
  655. "cWmin=%d cWmax=%d txop=%d\n",
  656. wiphy_name(local->hw.wiphy), queue, aci, acm,
  657. params.aifs, params.cw_min, params.cw_max, params.txop);
  658. #endif
  659. if (drv_conf_tx(local, queue, &params) && local->ops->conf_tx)
  660. printk(KERN_DEBUG "%s: failed to set TX queue "
  661. "parameters for queue %d\n",
  662. wiphy_name(local->hw.wiphy), queue);
  663. }
  664. }
  665. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  666. u16 capab, bool erp_valid, u8 erp)
  667. {
  668. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  669. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  670. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  671. #endif
  672. u32 changed = 0;
  673. bool use_protection;
  674. bool use_short_preamble;
  675. bool use_short_slot;
  676. if (erp_valid) {
  677. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  678. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  679. } else {
  680. use_protection = false;
  681. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  682. }
  683. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  684. if (use_protection != bss_conf->use_cts_prot) {
  685. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  686. if (net_ratelimit()) {
  687. printk(KERN_DEBUG "%s: CTS protection %s (BSSID=%pM)\n",
  688. sdata->dev->name,
  689. use_protection ? "enabled" : "disabled",
  690. ifmgd->bssid);
  691. }
  692. #endif
  693. bss_conf->use_cts_prot = use_protection;
  694. changed |= BSS_CHANGED_ERP_CTS_PROT;
  695. }
  696. if (use_short_preamble != bss_conf->use_short_preamble) {
  697. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  698. if (net_ratelimit()) {
  699. printk(KERN_DEBUG "%s: switched to %s barker preamble"
  700. " (BSSID=%pM)\n",
  701. sdata->dev->name,
  702. use_short_preamble ? "short" : "long",
  703. ifmgd->bssid);
  704. }
  705. #endif
  706. bss_conf->use_short_preamble = use_short_preamble;
  707. changed |= BSS_CHANGED_ERP_PREAMBLE;
  708. }
  709. if (use_short_slot != bss_conf->use_short_slot) {
  710. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  711. if (net_ratelimit()) {
  712. printk(KERN_DEBUG "%s: switched to %s slot time"
  713. " (BSSID=%pM)\n",
  714. sdata->dev->name,
  715. use_short_slot ? "short" : "long",
  716. ifmgd->bssid);
  717. }
  718. #endif
  719. bss_conf->use_short_slot = use_short_slot;
  720. changed |= BSS_CHANGED_ERP_SLOT;
  721. }
  722. return changed;
  723. }
  724. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  725. u32 bss_info_changed)
  726. {
  727. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  728. struct ieee80211_local *local = sdata->local;
  729. struct ieee80211_conf *conf = &local_to_hw(local)->conf;
  730. struct ieee80211_bss *bss;
  731. bss_info_changed |= BSS_CHANGED_ASSOC;
  732. ifmgd->flags |= IEEE80211_STA_ASSOCIATED;
  733. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  734. conf->channel->center_freq,
  735. ifmgd->ssid, ifmgd->ssid_len);
  736. if (bss) {
  737. /* set timing information */
  738. sdata->vif.bss_conf.beacon_int = bss->cbss.beacon_interval;
  739. sdata->vif.bss_conf.timestamp = bss->cbss.tsf;
  740. sdata->vif.bss_conf.dtim_period = bss->dtim_period;
  741. bss_info_changed |= BSS_CHANGED_BEACON_INT;
  742. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  743. bss->cbss.capability, bss->has_erp_value, bss->erp_value);
  744. ieee80211_rx_bss_put(local, bss);
  745. }
  746. ieee80211_led_assoc(local, 1);
  747. sdata->vif.bss_conf.assoc = 1;
  748. /*
  749. * For now just always ask the driver to update the basic rateset
  750. * when we have associated, we aren't checking whether it actually
  751. * changed or not.
  752. */
  753. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  754. /* And the BSSID changed - we're associated now */
  755. bss_info_changed |= BSS_CHANGED_BSSID;
  756. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  757. /* will be same as sdata */
  758. if (local->ps_sdata) {
  759. mutex_lock(&local->iflist_mtx);
  760. ieee80211_recalc_ps(local, -1);
  761. mutex_unlock(&local->iflist_mtx);
  762. }
  763. netif_tx_start_all_queues(sdata->dev);
  764. netif_carrier_on(sdata->dev);
  765. }
  766. static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata)
  767. {
  768. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  769. struct ieee80211_local *local = sdata->local;
  770. ifmgd->direct_probe_tries++;
  771. if (ifmgd->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
  772. printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
  773. sdata->dev->name, ifmgd->bssid);
  774. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  775. ieee80211_recalc_idle(local);
  776. cfg80211_send_auth_timeout(sdata->dev, ifmgd->bssid,
  777. GFP_KERNEL);
  778. /*
  779. * Most likely AP is not in the range so remove the
  780. * bss information associated to the AP
  781. */
  782. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  783. sdata->local->hw.conf.channel->center_freq,
  784. ifmgd->ssid, ifmgd->ssid_len);
  785. /*
  786. * We might have a pending scan which had no chance to run yet
  787. * due to state == IEEE80211_STA_MLME_DIRECT_PROBE.
  788. * Hence, queue the STAs work again
  789. */
  790. queue_work(local->hw.workqueue, &ifmgd->work);
  791. return;
  792. }
  793. printk(KERN_DEBUG "%s: direct probe to AP %pM try %d\n",
  794. sdata->dev->name, ifmgd->bssid,
  795. ifmgd->direct_probe_tries);
  796. ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  797. /* Direct probe is sent to broadcast address as some APs
  798. * will not answer to direct packet in unassociated state.
  799. */
  800. ieee80211_send_probe_req(sdata, NULL,
  801. ifmgd->ssid, ifmgd->ssid_len, NULL, 0);
  802. mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  803. }
  804. static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata)
  805. {
  806. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  807. struct ieee80211_local *local = sdata->local;
  808. ifmgd->auth_tries++;
  809. if (ifmgd->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
  810. printk(KERN_DEBUG "%s: authentication with AP %pM"
  811. " timed out\n",
  812. sdata->dev->name, ifmgd->bssid);
  813. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  814. ieee80211_recalc_idle(local);
  815. cfg80211_send_auth_timeout(sdata->dev, ifmgd->bssid,
  816. GFP_KERNEL);
  817. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  818. sdata->local->hw.conf.channel->center_freq,
  819. ifmgd->ssid, ifmgd->ssid_len);
  820. /*
  821. * We might have a pending scan which had no chance to run yet
  822. * due to state == IEEE80211_STA_MLME_AUTHENTICATE.
  823. * Hence, queue the STAs work again
  824. */
  825. queue_work(local->hw.workqueue, &ifmgd->work);
  826. return;
  827. }
  828. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  829. printk(KERN_DEBUG "%s: authenticate with AP %pM\n",
  830. sdata->dev->name, ifmgd->bssid);
  831. ieee80211_send_auth(sdata, 1, ifmgd->auth_alg, ifmgd->sme_auth_ie,
  832. ifmgd->sme_auth_ie_len, ifmgd->bssid, 0);
  833. ifmgd->auth_transaction = 2;
  834. mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  835. }
  836. /*
  837. * The disassoc 'reason' argument can be either our own reason
  838. * if self disconnected or a reason code from the AP.
  839. */
  840. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  841. bool deauth, bool self_disconnected,
  842. u16 reason)
  843. {
  844. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  845. struct ieee80211_local *local = sdata->local;
  846. struct ieee80211_conf *conf = &local_to_hw(local)->conf;
  847. struct ieee80211_bss *bss;
  848. struct sta_info *sta;
  849. u32 changed = 0, config_changed = 0;
  850. if (deauth) {
  851. ifmgd->direct_probe_tries = 0;
  852. ifmgd->auth_tries = 0;
  853. }
  854. ifmgd->assoc_scan_tries = 0;
  855. ifmgd->assoc_tries = 0;
  856. netif_tx_stop_all_queues(sdata->dev);
  857. netif_carrier_off(sdata->dev);
  858. rcu_read_lock();
  859. sta = sta_info_get(local, ifmgd->bssid);
  860. if (sta)
  861. ieee80211_sta_tear_down_BA_sessions(sta);
  862. rcu_read_unlock();
  863. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  864. conf->channel->center_freq,
  865. ifmgd->ssid, ifmgd->ssid_len);
  866. if (bss)
  867. ieee80211_rx_bss_put(local, bss);
  868. if (self_disconnected) {
  869. if (deauth)
  870. ieee80211_send_deauth_disassoc(sdata,
  871. IEEE80211_STYPE_DEAUTH, reason);
  872. else
  873. ieee80211_send_deauth_disassoc(sdata,
  874. IEEE80211_STYPE_DISASSOC, reason);
  875. }
  876. ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
  877. changed |= ieee80211_reset_erp_info(sdata);
  878. ieee80211_led_assoc(local, 0);
  879. changed |= BSS_CHANGED_ASSOC;
  880. sdata->vif.bss_conf.assoc = false;
  881. if (self_disconnected || reason == WLAN_REASON_DISASSOC_STA_HAS_LEFT) {
  882. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  883. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  884. sdata->local->hw.conf.channel->center_freq,
  885. ifmgd->ssid, ifmgd->ssid_len);
  886. }
  887. ieee80211_set_wmm_default(sdata);
  888. ieee80211_recalc_idle(local);
  889. /* channel(_type) changes are handled by ieee80211_hw_config */
  890. local->oper_channel_type = NL80211_CHAN_NO_HT;
  891. /* on the next assoc, re-program HT parameters */
  892. sdata->ht_opmode_valid = false;
  893. local->power_constr_level = 0;
  894. del_timer_sync(&local->dynamic_ps_timer);
  895. cancel_work_sync(&local->dynamic_ps_enable_work);
  896. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  897. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  898. config_changed |= IEEE80211_CONF_CHANGE_PS;
  899. }
  900. ieee80211_hw_config(local, config_changed);
  901. /* And the BSSID changed -- not very interesting here */
  902. changed |= BSS_CHANGED_BSSID;
  903. ieee80211_bss_info_change_notify(sdata, changed);
  904. rcu_read_lock();
  905. sta = sta_info_get(local, ifmgd->bssid);
  906. if (!sta) {
  907. rcu_read_unlock();
  908. return;
  909. }
  910. sta_info_unlink(&sta);
  911. rcu_read_unlock();
  912. sta_info_destroy(sta);
  913. }
  914. static void ieee80211_associate(struct ieee80211_sub_if_data *sdata)
  915. {
  916. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  917. struct ieee80211_local *local = sdata->local;
  918. ifmgd->assoc_tries++;
  919. if (ifmgd->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
  920. printk(KERN_DEBUG "%s: association with AP %pM"
  921. " timed out\n",
  922. sdata->dev->name, ifmgd->bssid);
  923. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  924. ieee80211_recalc_idle(local);
  925. cfg80211_send_assoc_timeout(sdata->dev, ifmgd->bssid,
  926. GFP_KERNEL);
  927. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  928. sdata->local->hw.conf.channel->center_freq,
  929. ifmgd->ssid, ifmgd->ssid_len);
  930. /*
  931. * We might have a pending scan which had no chance to run yet
  932. * due to state == IEEE80211_STA_MLME_ASSOCIATE.
  933. * Hence, queue the STAs work again
  934. */
  935. queue_work(local->hw.workqueue, &ifmgd->work);
  936. return;
  937. }
  938. ifmgd->state = IEEE80211_STA_MLME_ASSOCIATE;
  939. printk(KERN_DEBUG "%s: associate with AP %pM\n",
  940. sdata->dev->name, ifmgd->bssid);
  941. ieee80211_send_assoc(sdata);
  942. mod_timer(&ifmgd->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
  943. }
  944. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  945. struct ieee80211_hdr *hdr)
  946. {
  947. /*
  948. * We can postpone the mgd.timer whenever receiving unicast frames
  949. * from AP because we know that the connection is working both ways
  950. * at that time. But multicast frames (and hence also beacons) must
  951. * be ignored here, because we need to trigger the timer during
  952. * data idle periods for sending the periodical probe request to
  953. * the AP.
  954. */
  955. if (!is_multicast_ether_addr(hdr->addr1))
  956. mod_timer(&sdata->u.mgd.timer,
  957. jiffies + IEEE80211_MONITORING_INTERVAL);
  958. }
  959. void ieee80211_beacon_loss_work(struct work_struct *work)
  960. {
  961. struct ieee80211_sub_if_data *sdata =
  962. container_of(work, struct ieee80211_sub_if_data,
  963. u.mgd.beacon_loss_work);
  964. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  965. /*
  966. * The driver has already reported this event and we have
  967. * already sent a probe request. Maybe the AP died and the
  968. * driver keeps reporting until we disassociate... We have
  969. * to ignore that because otherwise we would continually
  970. * reset the timer and never check whether we received a
  971. * probe response!
  972. */
  973. if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL)
  974. return;
  975. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  976. if (net_ratelimit()) {
  977. printk(KERN_DEBUG "%s: driver reports beacon loss from AP %pM "
  978. "- sending probe request\n", sdata->dev->name,
  979. sdata->u.mgd.bssid);
  980. }
  981. #endif
  982. ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
  983. mutex_lock(&sdata->local->iflist_mtx);
  984. ieee80211_recalc_ps(sdata->local, -1);
  985. mutex_unlock(&sdata->local->iflist_mtx);
  986. ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
  987. ifmgd->ssid_len, NULL, 0);
  988. mod_timer(&ifmgd->timer, jiffies + IEEE80211_PROBE_WAIT);
  989. }
  990. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  991. {
  992. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  993. queue_work(sdata->local->hw.workqueue,
  994. &sdata->u.mgd.beacon_loss_work);
  995. }
  996. EXPORT_SYMBOL(ieee80211_beacon_loss);
  997. static void ieee80211_associated(struct ieee80211_sub_if_data *sdata)
  998. {
  999. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1000. struct ieee80211_local *local = sdata->local;
  1001. struct sta_info *sta;
  1002. unsigned long last_rx;
  1003. bool disassoc = false;
  1004. /* TODO: start monitoring current AP signal quality and number of
  1005. * missed beacons. Scan other channels every now and then and search
  1006. * for better APs. */
  1007. /* TODO: remove expired BSSes */
  1008. ifmgd->state = IEEE80211_STA_MLME_ASSOCIATED;
  1009. rcu_read_lock();
  1010. sta = sta_info_get(local, ifmgd->bssid);
  1011. if (!sta) {
  1012. printk(KERN_DEBUG "%s: No STA entry for own AP %pM\n",
  1013. sdata->dev->name, ifmgd->bssid);
  1014. disassoc = true;
  1015. rcu_read_unlock();
  1016. goto out;
  1017. }
  1018. last_rx = sta->last_rx;
  1019. rcu_read_unlock();
  1020. if ((ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) &&
  1021. time_after(jiffies, last_rx + IEEE80211_PROBE_WAIT)) {
  1022. printk(KERN_DEBUG "%s: no probe response from AP %pM "
  1023. "- disassociating\n",
  1024. sdata->dev->name, ifmgd->bssid);
  1025. disassoc = true;
  1026. ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  1027. goto out;
  1028. }
  1029. /*
  1030. * Beacon filtering is only enabled with power save and then the
  1031. * stack should not check for beacon loss.
  1032. */
  1033. if (!((local->hw.flags & IEEE80211_HW_BEACON_FILTER) &&
  1034. (local->hw.conf.flags & IEEE80211_CONF_PS)) &&
  1035. time_after(jiffies,
  1036. ifmgd->last_beacon + IEEE80211_MONITORING_INTERVAL)) {
  1037. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1038. if (net_ratelimit()) {
  1039. printk(KERN_DEBUG "%s: beacon loss from AP %pM "
  1040. "- sending probe request\n",
  1041. sdata->dev->name, ifmgd->bssid);
  1042. }
  1043. #endif
  1044. ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
  1045. mutex_lock(&local->iflist_mtx);
  1046. ieee80211_recalc_ps(local, -1);
  1047. mutex_unlock(&local->iflist_mtx);
  1048. ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
  1049. ifmgd->ssid_len, NULL, 0);
  1050. mod_timer(&ifmgd->timer, jiffies + IEEE80211_PROBE_WAIT);
  1051. goto out;
  1052. }
  1053. if (time_after(jiffies, last_rx + IEEE80211_PROBE_IDLE_TIME)) {
  1054. ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
  1055. mutex_lock(&local->iflist_mtx);
  1056. ieee80211_recalc_ps(local, -1);
  1057. mutex_unlock(&local->iflist_mtx);
  1058. ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
  1059. ifmgd->ssid_len, NULL, 0);
  1060. }
  1061. out:
  1062. if (!disassoc)
  1063. mod_timer(&ifmgd->timer,
  1064. jiffies + IEEE80211_MONITORING_INTERVAL);
  1065. else
  1066. ieee80211_set_disassoc(sdata, true, true,
  1067. WLAN_REASON_PREV_AUTH_NOT_VALID);
  1068. }
  1069. static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata)
  1070. {
  1071. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1072. printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
  1073. ifmgd->flags |= IEEE80211_STA_AUTHENTICATED;
  1074. /* Wait for SME to request association */
  1075. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1076. ieee80211_recalc_idle(sdata->local);
  1077. }
  1078. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  1079. struct ieee80211_mgmt *mgmt,
  1080. size_t len)
  1081. {
  1082. u8 *pos;
  1083. struct ieee802_11_elems elems;
  1084. pos = mgmt->u.auth.variable;
  1085. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1086. if (!elems.challenge)
  1087. return;
  1088. ieee80211_send_auth(sdata, 3, sdata->u.mgd.auth_alg,
  1089. elems.challenge - 2, elems.challenge_len + 2,
  1090. sdata->u.mgd.bssid, 1);
  1091. sdata->u.mgd.auth_transaction = 4;
  1092. }
  1093. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  1094. struct ieee80211_mgmt *mgmt,
  1095. size_t len)
  1096. {
  1097. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1098. u16 auth_alg, auth_transaction, status_code;
  1099. if (ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE)
  1100. return;
  1101. if (len < 24 + 6)
  1102. return;
  1103. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
  1104. return;
  1105. if (memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1106. return;
  1107. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  1108. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  1109. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  1110. if (auth_alg != ifmgd->auth_alg ||
  1111. auth_transaction != ifmgd->auth_transaction)
  1112. return;
  1113. if (status_code != WLAN_STATUS_SUCCESS) {
  1114. cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len,
  1115. GFP_KERNEL);
  1116. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1117. ieee80211_recalc_idle(sdata->local);
  1118. return;
  1119. }
  1120. switch (ifmgd->auth_alg) {
  1121. case WLAN_AUTH_OPEN:
  1122. case WLAN_AUTH_LEAP:
  1123. case WLAN_AUTH_FT:
  1124. ieee80211_auth_completed(sdata);
  1125. cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len,
  1126. GFP_KERNEL);
  1127. break;
  1128. case WLAN_AUTH_SHARED_KEY:
  1129. if (ifmgd->auth_transaction == 4) {
  1130. ieee80211_auth_completed(sdata);
  1131. cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len,
  1132. GFP_KERNEL);
  1133. } else
  1134. ieee80211_auth_challenge(sdata, mgmt, len);
  1135. break;
  1136. }
  1137. }
  1138. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1139. struct ieee80211_mgmt *mgmt,
  1140. size_t len)
  1141. {
  1142. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1143. u16 reason_code;
  1144. if (len < 24 + 2)
  1145. return;
  1146. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
  1147. return;
  1148. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1149. if (ifmgd->flags & IEEE80211_STA_AUTHENTICATED)
  1150. printk(KERN_DEBUG "%s: deauthenticated (Reason: %u)\n",
  1151. sdata->dev->name, reason_code);
  1152. ieee80211_set_disassoc(sdata, true, false, 0);
  1153. ifmgd->flags &= ~IEEE80211_STA_AUTHENTICATED;
  1154. cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, len, GFP_KERNEL);
  1155. }
  1156. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1157. struct ieee80211_mgmt *mgmt,
  1158. size_t len)
  1159. {
  1160. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1161. u16 reason_code;
  1162. if (len < 24 + 2)
  1163. return;
  1164. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
  1165. return;
  1166. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1167. if (ifmgd->flags & IEEE80211_STA_ASSOCIATED)
  1168. printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
  1169. sdata->dev->name, reason_code);
  1170. ieee80211_set_disassoc(sdata, false, false, reason_code);
  1171. cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, len, GFP_KERNEL);
  1172. }
  1173. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  1174. struct ieee80211_mgmt *mgmt,
  1175. size_t len,
  1176. int reassoc)
  1177. {
  1178. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1179. struct ieee80211_local *local = sdata->local;
  1180. struct ieee80211_supported_band *sband;
  1181. struct sta_info *sta;
  1182. u32 rates, basic_rates;
  1183. u16 capab_info, status_code, aid;
  1184. struct ieee802_11_elems elems;
  1185. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1186. u8 *pos;
  1187. u32 changed = 0;
  1188. int i, j;
  1189. bool have_higher_than_11mbit = false, newsta = false;
  1190. u16 ap_ht_cap_flags;
  1191. /* AssocResp and ReassocResp have identical structure, so process both
  1192. * of them in this function. */
  1193. if (ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE)
  1194. return;
  1195. if (len < 24 + 6)
  1196. return;
  1197. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
  1198. return;
  1199. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1200. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1201. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1202. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  1203. "status=%d aid=%d)\n",
  1204. sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
  1205. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1206. pos = mgmt->u.assoc_resp.variable;
  1207. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1208. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  1209. elems.timeout_int && elems.timeout_int_len == 5 &&
  1210. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  1211. u32 tu, ms;
  1212. tu = get_unaligned_le32(elems.timeout_int + 1);
  1213. ms = tu * 1024 / 1000;
  1214. printk(KERN_DEBUG "%s: AP rejected association temporarily; "
  1215. "comeback duration %u TU (%u ms)\n",
  1216. sdata->dev->name, tu, ms);
  1217. if (ms > IEEE80211_ASSOC_TIMEOUT)
  1218. mod_timer(&ifmgd->timer,
  1219. jiffies + msecs_to_jiffies(ms));
  1220. return;
  1221. }
  1222. if (status_code != WLAN_STATUS_SUCCESS) {
  1223. printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
  1224. sdata->dev->name, status_code);
  1225. cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, len,
  1226. GFP_KERNEL);
  1227. /* Wait for SME to decide what to do next */
  1228. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1229. ieee80211_recalc_idle(local);
  1230. return;
  1231. }
  1232. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1233. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  1234. "set\n", sdata->dev->name, aid);
  1235. aid &= ~(BIT(15) | BIT(14));
  1236. if (!elems.supp_rates) {
  1237. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  1238. sdata->dev->name);
  1239. return;
  1240. }
  1241. printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
  1242. ifmgd->aid = aid;
  1243. kfree(ifmgd->assocresp_ies);
  1244. ifmgd->assocresp_ies_len = len - (pos - (u8 *) mgmt);
  1245. ifmgd->assocresp_ies = kmalloc(ifmgd->assocresp_ies_len, GFP_KERNEL);
  1246. if (ifmgd->assocresp_ies)
  1247. memcpy(ifmgd->assocresp_ies, pos, ifmgd->assocresp_ies_len);
  1248. rcu_read_lock();
  1249. /* Add STA entry for the AP */
  1250. sta = sta_info_get(local, ifmgd->bssid);
  1251. if (!sta) {
  1252. newsta = true;
  1253. sta = sta_info_alloc(sdata, ifmgd->bssid, GFP_ATOMIC);
  1254. if (!sta) {
  1255. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  1256. " the AP\n", sdata->dev->name);
  1257. rcu_read_unlock();
  1258. return;
  1259. }
  1260. /* update new sta with its last rx activity */
  1261. sta->last_rx = jiffies;
  1262. }
  1263. /*
  1264. * FIXME: Do we really need to update the sta_info's information here?
  1265. * We already know about the AP (we found it in our list) so it
  1266. * should already be filled with the right info, no?
  1267. * As is stands, all this is racy because typically we assume
  1268. * the information that is filled in here (except flags) doesn't
  1269. * change while a STA structure is alive. As such, it should move
  1270. * to between the sta_info_alloc() and sta_info_insert() above.
  1271. */
  1272. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP);
  1273. if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  1274. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  1275. rates = 0;
  1276. basic_rates = 0;
  1277. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1278. for (i = 0; i < elems.supp_rates_len; i++) {
  1279. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  1280. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1281. if (rate > 110)
  1282. have_higher_than_11mbit = true;
  1283. for (j = 0; j < sband->n_bitrates; j++) {
  1284. if (sband->bitrates[j].bitrate == rate) {
  1285. rates |= BIT(j);
  1286. if (is_basic)
  1287. basic_rates |= BIT(j);
  1288. break;
  1289. }
  1290. }
  1291. }
  1292. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  1293. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  1294. bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
  1295. if (rate > 110)
  1296. have_higher_than_11mbit = true;
  1297. for (j = 0; j < sband->n_bitrates; j++) {
  1298. if (sband->bitrates[j].bitrate == rate) {
  1299. rates |= BIT(j);
  1300. if (is_basic)
  1301. basic_rates |= BIT(j);
  1302. break;
  1303. }
  1304. }
  1305. }
  1306. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  1307. sdata->vif.bss_conf.basic_rates = basic_rates;
  1308. /* cf. IEEE 802.11 9.2.12 */
  1309. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  1310. have_higher_than_11mbit)
  1311. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  1312. else
  1313. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  1314. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1315. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1316. elems.ht_cap_elem, &sta->sta.ht_cap);
  1317. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1318. rate_control_rate_init(sta);
  1319. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  1320. set_sta_flags(sta, WLAN_STA_MFP);
  1321. if (elems.wmm_param)
  1322. set_sta_flags(sta, WLAN_STA_WME);
  1323. if (newsta) {
  1324. int err = sta_info_insert(sta);
  1325. if (err) {
  1326. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  1327. " the AP (error %d)\n", sdata->dev->name, err);
  1328. rcu_read_unlock();
  1329. return;
  1330. }
  1331. }
  1332. rcu_read_unlock();
  1333. if (elems.wmm_param)
  1334. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  1335. elems.wmm_param_len);
  1336. else
  1337. ieee80211_set_wmm_default(sdata);
  1338. if (elems.ht_info_elem && elems.wmm_param &&
  1339. (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
  1340. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1341. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1342. ap_ht_cap_flags);
  1343. /* set AID and assoc capability,
  1344. * ieee80211_set_associated() will tell the driver */
  1345. bss_conf->aid = aid;
  1346. bss_conf->assoc_capability = capab_info;
  1347. ieee80211_set_associated(sdata, changed);
  1348. /*
  1349. * initialise the time of last beacon to be the association time,
  1350. * otherwise beacon loss check will trigger immediately
  1351. */
  1352. ifmgd->last_beacon = jiffies;
  1353. ieee80211_associated(sdata);
  1354. cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, len, GFP_KERNEL);
  1355. }
  1356. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1357. struct ieee80211_mgmt *mgmt,
  1358. size_t len,
  1359. struct ieee80211_rx_status *rx_status,
  1360. struct ieee802_11_elems *elems,
  1361. bool beacon)
  1362. {
  1363. struct ieee80211_local *local = sdata->local;
  1364. int freq;
  1365. struct ieee80211_bss *bss;
  1366. struct ieee80211_channel *channel;
  1367. if (elems->ds_params && elems->ds_params_len == 1)
  1368. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  1369. else
  1370. freq = rx_status->freq;
  1371. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1372. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1373. return;
  1374. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1375. channel, beacon);
  1376. if (!bss)
  1377. return;
  1378. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  1379. (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN) == 0)) {
  1380. struct ieee80211_channel_sw_ie *sw_elem =
  1381. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  1382. ieee80211_sta_process_chanswitch(sdata, sw_elem, bss);
  1383. }
  1384. ieee80211_rx_bss_put(local, bss);
  1385. }
  1386. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1387. struct ieee80211_mgmt *mgmt,
  1388. size_t len,
  1389. struct ieee80211_rx_status *rx_status)
  1390. {
  1391. struct ieee80211_if_managed *ifmgd;
  1392. size_t baselen;
  1393. struct ieee802_11_elems elems;
  1394. ifmgd = &sdata->u.mgd;
  1395. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  1396. return; /* ignore ProbeResp to foreign address */
  1397. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1398. if (baselen > len)
  1399. return;
  1400. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1401. &elems);
  1402. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1403. /* direct probe may be part of the association flow */
  1404. if (ifmgd->state == IEEE80211_STA_MLME_DIRECT_PROBE) {
  1405. printk(KERN_DEBUG "%s direct probe responded\n",
  1406. sdata->dev->name);
  1407. ieee80211_authenticate(sdata);
  1408. }
  1409. if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) {
  1410. ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  1411. mutex_lock(&sdata->local->iflist_mtx);
  1412. ieee80211_recalc_ps(sdata->local, -1);
  1413. mutex_unlock(&sdata->local->iflist_mtx);
  1414. }
  1415. }
  1416. /*
  1417. * This is the canonical list of information elements we care about,
  1418. * the filter code also gives us all changes to the Microsoft OUI
  1419. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  1420. *
  1421. * We implement beacon filtering in software since that means we can
  1422. * avoid processing the frame here and in cfg80211, and userspace
  1423. * will not be able to tell whether the hardware supports it or not.
  1424. *
  1425. * XXX: This list needs to be dynamic -- userspace needs to be able to
  1426. * add items it requires. It also needs to be able to tell us to
  1427. * look out for other vendor IEs.
  1428. */
  1429. static const u64 care_about_ies =
  1430. (1ULL << WLAN_EID_COUNTRY) |
  1431. (1ULL << WLAN_EID_ERP_INFO) |
  1432. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  1433. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  1434. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1435. (1ULL << WLAN_EID_HT_INFORMATION);
  1436. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1437. struct ieee80211_mgmt *mgmt,
  1438. size_t len,
  1439. struct ieee80211_rx_status *rx_status)
  1440. {
  1441. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1442. size_t baselen;
  1443. struct ieee802_11_elems elems;
  1444. struct ieee80211_local *local = sdata->local;
  1445. u32 changed = 0;
  1446. bool erp_valid, directed_tim = false;
  1447. u8 erp_value = 0;
  1448. u32 ncrc;
  1449. /* Process beacon from the current BSS */
  1450. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1451. if (baselen > len)
  1452. return;
  1453. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1454. return;
  1455. if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED) ||
  1456. memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1457. return;
  1458. if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) {
  1459. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1460. if (net_ratelimit()) {
  1461. printk(KERN_DEBUG "%s: cancelling probereq poll due "
  1462. "to a received beacon\n", sdata->dev->name);
  1463. }
  1464. #endif
  1465. ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  1466. mutex_lock(&local->iflist_mtx);
  1467. ieee80211_recalc_ps(local, -1);
  1468. mutex_unlock(&local->iflist_mtx);
  1469. }
  1470. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  1471. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  1472. len - baselen, &elems,
  1473. care_about_ies, ncrc);
  1474. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1475. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  1476. ifmgd->aid);
  1477. if (ncrc != ifmgd->beacon_crc) {
  1478. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  1479. true);
  1480. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  1481. elems.wmm_param_len);
  1482. }
  1483. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  1484. if (directed_tim) {
  1485. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1486. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1487. ieee80211_hw_config(local,
  1488. IEEE80211_CONF_CHANGE_PS);
  1489. ieee80211_send_nullfunc(local, sdata, 0);
  1490. } else {
  1491. local->pspolling = true;
  1492. /*
  1493. * Here is assumed that the driver will be
  1494. * able to send ps-poll frame and receive a
  1495. * response even though power save mode is
  1496. * enabled, but some drivers might require
  1497. * to disable power save here. This needs
  1498. * to be investigated.
  1499. */
  1500. ieee80211_send_pspoll(local, sdata);
  1501. }
  1502. }
  1503. }
  1504. if (ncrc == ifmgd->beacon_crc)
  1505. return;
  1506. ifmgd->beacon_crc = ncrc;
  1507. if (elems.erp_info && elems.erp_info_len >= 1) {
  1508. erp_valid = true;
  1509. erp_value = elems.erp_info[0];
  1510. } else {
  1511. erp_valid = false;
  1512. }
  1513. changed |= ieee80211_handle_bss_capability(sdata,
  1514. le16_to_cpu(mgmt->u.beacon.capab_info),
  1515. erp_valid, erp_value);
  1516. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  1517. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
  1518. struct sta_info *sta;
  1519. struct ieee80211_supported_band *sband;
  1520. u16 ap_ht_cap_flags;
  1521. rcu_read_lock();
  1522. sta = sta_info_get(local, ifmgd->bssid);
  1523. if (!sta) {
  1524. rcu_read_unlock();
  1525. return;
  1526. }
  1527. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1528. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1529. elems.ht_cap_elem, &sta->sta.ht_cap);
  1530. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1531. rcu_read_unlock();
  1532. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1533. ap_ht_cap_flags);
  1534. }
  1535. if (elems.country_elem) {
  1536. /* Note we are only reviewing this on beacons
  1537. * for the BSSID we are associated to */
  1538. regulatory_hint_11d(local->hw.wiphy,
  1539. elems.country_elem, elems.country_elem_len);
  1540. /* TODO: IBSS also needs this */
  1541. if (elems.pwr_constr_elem)
  1542. ieee80211_handle_pwr_constr(sdata,
  1543. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1544. elems.pwr_constr_elem,
  1545. elems.pwr_constr_elem_len);
  1546. }
  1547. ieee80211_bss_info_change_notify(sdata, changed);
  1548. }
  1549. ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  1550. struct sk_buff *skb)
  1551. {
  1552. struct ieee80211_local *local = sdata->local;
  1553. struct ieee80211_mgmt *mgmt;
  1554. u16 fc;
  1555. if (skb->len < 24)
  1556. return RX_DROP_MONITOR;
  1557. mgmt = (struct ieee80211_mgmt *) skb->data;
  1558. fc = le16_to_cpu(mgmt->frame_control);
  1559. switch (fc & IEEE80211_FCTL_STYPE) {
  1560. case IEEE80211_STYPE_PROBE_REQ:
  1561. case IEEE80211_STYPE_PROBE_RESP:
  1562. case IEEE80211_STYPE_BEACON:
  1563. case IEEE80211_STYPE_AUTH:
  1564. case IEEE80211_STYPE_ASSOC_RESP:
  1565. case IEEE80211_STYPE_REASSOC_RESP:
  1566. case IEEE80211_STYPE_DEAUTH:
  1567. case IEEE80211_STYPE_DISASSOC:
  1568. skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
  1569. queue_work(local->hw.workqueue, &sdata->u.mgd.work);
  1570. return RX_QUEUED;
  1571. }
  1572. return RX_DROP_MONITOR;
  1573. }
  1574. static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1575. struct sk_buff *skb)
  1576. {
  1577. struct ieee80211_rx_status *rx_status;
  1578. struct ieee80211_mgmt *mgmt;
  1579. u16 fc;
  1580. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1581. mgmt = (struct ieee80211_mgmt *) skb->data;
  1582. fc = le16_to_cpu(mgmt->frame_control);
  1583. switch (fc & IEEE80211_FCTL_STYPE) {
  1584. case IEEE80211_STYPE_PROBE_RESP:
  1585. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
  1586. rx_status);
  1587. break;
  1588. case IEEE80211_STYPE_BEACON:
  1589. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  1590. rx_status);
  1591. break;
  1592. case IEEE80211_STYPE_AUTH:
  1593. ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  1594. break;
  1595. case IEEE80211_STYPE_ASSOC_RESP:
  1596. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 0);
  1597. break;
  1598. case IEEE80211_STYPE_REASSOC_RESP:
  1599. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 1);
  1600. break;
  1601. case IEEE80211_STYPE_DEAUTH:
  1602. ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  1603. break;
  1604. case IEEE80211_STYPE_DISASSOC:
  1605. ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  1606. break;
  1607. }
  1608. kfree_skb(skb);
  1609. }
  1610. static void ieee80211_sta_timer(unsigned long data)
  1611. {
  1612. struct ieee80211_sub_if_data *sdata =
  1613. (struct ieee80211_sub_if_data *) data;
  1614. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1615. struct ieee80211_local *local = sdata->local;
  1616. if (local->quiescing) {
  1617. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1618. return;
  1619. }
  1620. set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1621. queue_work(local->hw.workqueue, &ifmgd->work);
  1622. }
  1623. static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata)
  1624. {
  1625. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1626. struct ieee80211_local *local = sdata->local;
  1627. /* Reset own TSF to allow time synchronization work. */
  1628. drv_reset_tsf(local);
  1629. ifmgd->wmm_last_param_set = -1; /* allow any WMM update */
  1630. ifmgd->auth_transaction = -1;
  1631. ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
  1632. ifmgd->assoc_scan_tries = 0;
  1633. ifmgd->direct_probe_tries = 0;
  1634. ifmgd->auth_tries = 0;
  1635. ifmgd->assoc_tries = 0;
  1636. netif_tx_stop_all_queues(sdata->dev);
  1637. netif_carrier_off(sdata->dev);
  1638. }
  1639. static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata)
  1640. {
  1641. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1642. struct ieee80211_local *local = sdata->local;
  1643. struct ieee80211_bss *bss;
  1644. u8 *bssid = ifmgd->bssid, *ssid = ifmgd->ssid;
  1645. u8 ssid_len = ifmgd->ssid_len;
  1646. u16 capa_mask = WLAN_CAPABILITY_ESS;
  1647. u16 capa_val = WLAN_CAPABILITY_ESS;
  1648. struct ieee80211_channel *chan = local->oper_channel;
  1649. bss = (void *)cfg80211_get_bss(local->hw.wiphy, chan,
  1650. bssid, ssid, ssid_len,
  1651. capa_mask, capa_val);
  1652. if (bss) {
  1653. local->oper_channel = bss->cbss.channel;
  1654. local->oper_channel_type = NL80211_CHAN_NO_HT;
  1655. ieee80211_hw_config(local, 0);
  1656. ieee80211_sta_def_wmm_params(sdata, bss->supp_rates_len,
  1657. bss->supp_rates);
  1658. if (sdata->u.mgd.mfp == IEEE80211_MFP_REQUIRED)
  1659. sdata->u.mgd.flags |= IEEE80211_STA_MFP_ENABLED;
  1660. else
  1661. sdata->u.mgd.flags &= ~IEEE80211_STA_MFP_ENABLED;
  1662. /* Send out direct probe if no probe resp was received or
  1663. * the one we have is outdated
  1664. */
  1665. if (!bss->last_probe_resp ||
  1666. time_after(jiffies, bss->last_probe_resp
  1667. + IEEE80211_SCAN_RESULT_EXPIRE))
  1668. ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  1669. else
  1670. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  1671. ieee80211_rx_bss_put(local, bss);
  1672. ieee80211_sta_reset_auth(sdata);
  1673. return 0;
  1674. } else {
  1675. if (ifmgd->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
  1676. ifmgd->assoc_scan_tries++;
  1677. ieee80211_request_internal_scan(sdata, ifmgd->ssid,
  1678. ssid_len);
  1679. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  1680. set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
  1681. } else {
  1682. ifmgd->assoc_scan_tries = 0;
  1683. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1684. ieee80211_recalc_idle(local);
  1685. }
  1686. }
  1687. return -1;
  1688. }
  1689. static void ieee80211_sta_work(struct work_struct *work)
  1690. {
  1691. struct ieee80211_sub_if_data *sdata =
  1692. container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
  1693. struct ieee80211_local *local = sdata->local;
  1694. struct ieee80211_if_managed *ifmgd;
  1695. struct sk_buff *skb;
  1696. if (!netif_running(sdata->dev))
  1697. return;
  1698. if (local->sw_scanning || local->hw_scanning)
  1699. return;
  1700. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1701. return;
  1702. /*
  1703. * Nothing should have been stuffed into the workqueue during
  1704. * the suspend->resume cycle. If this WARN is seen then there
  1705. * is a bug with either the driver suspend or something in
  1706. * mac80211 stuffing into the workqueue which we haven't yet
  1707. * cleared during mac80211's suspend cycle.
  1708. */
  1709. if (WARN_ON(local->suspended))
  1710. return;
  1711. ifmgd = &sdata->u.mgd;
  1712. while ((skb = skb_dequeue(&ifmgd->skb_queue)))
  1713. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1714. if (ifmgd->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
  1715. ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  1716. ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE &&
  1717. test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request)) {
  1718. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  1719. round_jiffies_relative(0));
  1720. return;
  1721. }
  1722. if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request)) {
  1723. if (ieee80211_sta_config_auth(sdata))
  1724. return;
  1725. clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1726. } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request))
  1727. return;
  1728. ieee80211_recalc_idle(local);
  1729. switch (ifmgd->state) {
  1730. case IEEE80211_STA_MLME_DISABLED:
  1731. break;
  1732. case IEEE80211_STA_MLME_DIRECT_PROBE:
  1733. ieee80211_direct_probe(sdata);
  1734. break;
  1735. case IEEE80211_STA_MLME_AUTHENTICATE:
  1736. ieee80211_authenticate(sdata);
  1737. break;
  1738. case IEEE80211_STA_MLME_ASSOCIATE:
  1739. ieee80211_associate(sdata);
  1740. break;
  1741. case IEEE80211_STA_MLME_ASSOCIATED:
  1742. ieee80211_associated(sdata);
  1743. break;
  1744. default:
  1745. WARN_ON(1);
  1746. break;
  1747. }
  1748. }
  1749. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  1750. {
  1751. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1752. /*
  1753. * Need to update last_beacon to avoid beacon loss
  1754. * test to trigger.
  1755. */
  1756. sdata->u.mgd.last_beacon = jiffies;
  1757. queue_work(sdata->local->hw.workqueue,
  1758. &sdata->u.mgd.work);
  1759. }
  1760. }
  1761. #ifdef CONFIG_PM
  1762. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  1763. {
  1764. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1765. /*
  1766. * we need to use atomic bitops for the running bits
  1767. * only because both timers might fire at the same
  1768. * time -- the code here is properly synchronised.
  1769. */
  1770. cancel_work_sync(&ifmgd->work);
  1771. cancel_work_sync(&ifmgd->beacon_loss_work);
  1772. if (del_timer_sync(&ifmgd->timer))
  1773. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1774. cancel_work_sync(&ifmgd->chswitch_work);
  1775. if (del_timer_sync(&ifmgd->chswitch_timer))
  1776. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  1777. }
  1778. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  1779. {
  1780. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1781. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  1782. add_timer(&ifmgd->timer);
  1783. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  1784. add_timer(&ifmgd->chswitch_timer);
  1785. }
  1786. #endif
  1787. /* interface setup */
  1788. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1789. {
  1790. struct ieee80211_if_managed *ifmgd;
  1791. u32 hw_flags;
  1792. ifmgd = &sdata->u.mgd;
  1793. INIT_WORK(&ifmgd->work, ieee80211_sta_work);
  1794. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  1795. INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
  1796. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  1797. (unsigned long) sdata);
  1798. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  1799. (unsigned long) sdata);
  1800. skb_queue_head_init(&ifmgd->skb_queue);
  1801. ifmgd->capab = WLAN_CAPABILITY_ESS;
  1802. ifmgd->flags = 0;
  1803. if (sdata->local->hw.queues >= 4)
  1804. ifmgd->flags |= IEEE80211_STA_WMM_ENABLED;
  1805. hw_flags = sdata->local->hw.flags;
  1806. }
  1807. /* configuration hooks */
  1808. void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata)
  1809. {
  1810. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1811. struct ieee80211_local *local = sdata->local;
  1812. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1813. return;
  1814. if (WARN_ON(ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED))
  1815. ieee80211_set_disassoc(sdata, true, true,
  1816. WLAN_REASON_DEAUTH_LEAVING);
  1817. if (WARN_ON(ifmgd->ssid_len == 0)) {
  1818. /*
  1819. * Only allow association to be started if a valid SSID
  1820. * is configured.
  1821. */
  1822. return;
  1823. }
  1824. set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1825. queue_work(local->hw.workqueue, &ifmgd->work);
  1826. }
  1827. int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata,
  1828. const char *ie, size_t len)
  1829. {
  1830. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1831. if (len == 0 && ifmgd->extra_ie_len == 0)
  1832. return -EALREADY;
  1833. if (len == ifmgd->extra_ie_len && ifmgd->extra_ie &&
  1834. memcmp(ifmgd->extra_ie, ie, len) == 0)
  1835. return -EALREADY;
  1836. kfree(ifmgd->extra_ie);
  1837. if (len == 0) {
  1838. ifmgd->extra_ie = NULL;
  1839. ifmgd->extra_ie_len = 0;
  1840. return 0;
  1841. }
  1842. ifmgd->extra_ie = kmalloc(len, GFP_KERNEL);
  1843. if (!ifmgd->extra_ie) {
  1844. ifmgd->extra_ie_len = 0;
  1845. return -ENOMEM;
  1846. }
  1847. memcpy(ifmgd->extra_ie, ie, len);
  1848. ifmgd->extra_ie_len = len;
  1849. return 0;
  1850. }
  1851. int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
  1852. {
  1853. printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
  1854. sdata->dev->name, reason);
  1855. ieee80211_set_disassoc(sdata, true, true, reason);
  1856. return 0;
  1857. }
  1858. int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
  1859. {
  1860. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1861. printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
  1862. sdata->dev->name, reason);
  1863. if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED))
  1864. return -ENOLINK;
  1865. ieee80211_set_disassoc(sdata, false, true, reason);
  1866. return 0;
  1867. }
  1868. /* scan finished notification */
  1869. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  1870. {
  1871. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  1872. /* Restart STA timers */
  1873. rcu_read_lock();
  1874. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  1875. ieee80211_restart_sta_timer(sdata);
  1876. rcu_read_unlock();
  1877. }
  1878. int ieee80211_max_network_latency(struct notifier_block *nb,
  1879. unsigned long data, void *dummy)
  1880. {
  1881. s32 latency_usec = (s32) data;
  1882. struct ieee80211_local *local =
  1883. container_of(nb, struct ieee80211_local,
  1884. network_latency_notifier);
  1885. mutex_lock(&local->iflist_mtx);
  1886. ieee80211_recalc_ps(local, latency_usec);
  1887. mutex_unlock(&local->iflist_mtx);
  1888. return 0;
  1889. }