mlme.c 66 KB

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