mlme.c 64 KB

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