mlme.c 68 KB

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