mlme.c 69 KB

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