mlme.c 66 KB

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