mlme.c 66 KB

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