mlme.c 66 KB

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