mlme.c 66 KB

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