mlme.c 56 KB

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