mlme.c 65 KB

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