mlme.c 72 KB

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