mlme.c 65 KB

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