mlme.c 56 KB

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