mlme.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600
  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/moduleparam.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/pm_qos.h>
  21. #include <linux/crc32.h>
  22. #include <linux/slab.h>
  23. #include <linux/export.h>
  24. #include <net/mac80211.h>
  25. #include <asm/unaligned.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "led.h"
  30. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  31. #define IEEE80211_AUTH_MAX_TRIES 3
  32. #define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
  33. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  34. #define IEEE80211_ASSOC_MAX_TRIES 3
  35. static int max_nullfunc_tries = 2;
  36. module_param(max_nullfunc_tries, int, 0644);
  37. MODULE_PARM_DESC(max_nullfunc_tries,
  38. "Maximum nullfunc tx tries before disconnecting (reason 4).");
  39. static int max_probe_tries = 5;
  40. module_param(max_probe_tries, int, 0644);
  41. MODULE_PARM_DESC(max_probe_tries,
  42. "Maximum probe tries before disconnecting (reason 4).");
  43. /*
  44. * Beacon loss timeout is calculated as N frames times the
  45. * advertised beacon interval. This may need to be somewhat
  46. * higher than what hardware might detect to account for
  47. * delays in the host processing frames. But since we also
  48. * probe on beacon miss before declaring the connection lost
  49. * default to what we want.
  50. */
  51. #define IEEE80211_BEACON_LOSS_COUNT 7
  52. /*
  53. * Time the connection can be idle before we probe
  54. * it to see if we can still talk to the AP.
  55. */
  56. #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
  57. /*
  58. * Time we wait for a probe response after sending
  59. * a probe request because of beacon loss or for
  60. * checking the connection still works.
  61. */
  62. static int probe_wait_ms = 500;
  63. module_param(probe_wait_ms, int, 0644);
  64. MODULE_PARM_DESC(probe_wait_ms,
  65. "Maximum time(ms) to wait for probe response"
  66. " before disconnecting (reason 4).");
  67. /*
  68. * Weight given to the latest Beacon frame when calculating average signal
  69. * strength for Beacon frames received in the current BSS. This must be
  70. * between 1 and 15.
  71. */
  72. #define IEEE80211_SIGNAL_AVE_WEIGHT 3
  73. /*
  74. * How many Beacon frames need to have been used in average signal strength
  75. * before starting to indicate signal change events.
  76. */
  77. #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
  78. #define TMR_RUNNING_TIMER 0
  79. #define TMR_RUNNING_CHANSW 1
  80. /*
  81. * All cfg80211 functions have to be called outside a locked
  82. * section so that they can acquire a lock themselves... This
  83. * is much simpler than queuing up things in cfg80211, but we
  84. * do need some indirection for that here.
  85. */
  86. enum rx_mgmt_action {
  87. /* no action required */
  88. RX_MGMT_NONE,
  89. /* caller must call cfg80211_send_deauth() */
  90. RX_MGMT_CFG80211_DEAUTH,
  91. /* caller must call cfg80211_send_disassoc() */
  92. RX_MGMT_CFG80211_DISASSOC,
  93. /* caller must call cfg80211_send_rx_auth() */
  94. RX_MGMT_CFG80211_RX_AUTH,
  95. /* caller must call cfg80211_send_rx_assoc() */
  96. RX_MGMT_CFG80211_RX_ASSOC,
  97. /* caller must call cfg80211_send_assoc_timeout() */
  98. RX_MGMT_CFG80211_ASSOC_TIMEOUT,
  99. };
  100. /* utils */
  101. static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
  102. {
  103. lockdep_assert_held(&ifmgd->mtx);
  104. }
  105. /*
  106. * We can have multiple work items (and connection probing)
  107. * scheduling this timer, but we need to take care to only
  108. * reschedule it when it should fire _earlier_ than it was
  109. * asked for before, or if it's not pending right now. This
  110. * function ensures that. Note that it then is required to
  111. * run this function for all timeouts after the first one
  112. * has happened -- the work that runs from this timer will
  113. * do that.
  114. */
  115. static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
  116. {
  117. ASSERT_MGD_MTX(ifmgd);
  118. if (!timer_pending(&ifmgd->timer) ||
  119. time_before(timeout, ifmgd->timer.expires))
  120. mod_timer(&ifmgd->timer, timeout);
  121. }
  122. void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
  123. {
  124. if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
  125. return;
  126. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  127. return;
  128. mod_timer(&sdata->u.mgd.bcn_mon_timer,
  129. round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
  130. }
  131. void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
  132. {
  133. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  134. if (unlikely(!sdata->u.mgd.associated))
  135. return;
  136. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  137. return;
  138. mod_timer(&sdata->u.mgd.conn_mon_timer,
  139. round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  140. ifmgd->probe_send_count = 0;
  141. }
  142. static int ecw2cw(int ecw)
  143. {
  144. return (1 << ecw) - 1;
  145. }
  146. static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
  147. struct ieee80211_ht_operation *ht_oper,
  148. const u8 *bssid, bool reconfig)
  149. {
  150. struct ieee80211_local *local = sdata->local;
  151. struct ieee80211_supported_band *sband;
  152. struct sta_info *sta;
  153. u32 changed = 0;
  154. u16 ht_opmode;
  155. bool disable_40 = false;
  156. sband = local->hw.wiphy->bands[local->oper_channel->band];
  157. switch (sdata->vif.bss_conf.channel_type) {
  158. case NL80211_CHAN_HT40PLUS:
  159. if (local->oper_channel->flags & IEEE80211_CHAN_NO_HT40PLUS)
  160. disable_40 = true;
  161. break;
  162. case NL80211_CHAN_HT40MINUS:
  163. if (local->oper_channel->flags & IEEE80211_CHAN_NO_HT40MINUS)
  164. disable_40 = true;
  165. break;
  166. default:
  167. break;
  168. }
  169. /* This can change during the lifetime of the BSS */
  170. if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
  171. disable_40 = true;
  172. mutex_lock(&local->sta_mtx);
  173. sta = sta_info_get(sdata, bssid);
  174. WARN_ON_ONCE(!sta);
  175. if (sta && !sta->supports_40mhz)
  176. disable_40 = true;
  177. if (sta && (!reconfig ||
  178. (disable_40 != !(sta->sta.ht_cap.cap &
  179. IEEE80211_HT_CAP_SUP_WIDTH_20_40)))) {
  180. if (disable_40)
  181. sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  182. else
  183. sta->sta.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  184. rate_control_rate_update(local, sband, sta,
  185. IEEE80211_RC_BW_CHANGED);
  186. }
  187. mutex_unlock(&local->sta_mtx);
  188. ht_opmode = le16_to_cpu(ht_oper->operation_mode);
  189. /* if bss configuration changed store the new one */
  190. if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
  191. changed |= BSS_CHANGED_HT;
  192. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  193. }
  194. return changed;
  195. }
  196. /* frame sending functions */
  197. static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
  198. struct ieee80211_supported_band *sband,
  199. u32 *rates)
  200. {
  201. int i, j, count;
  202. *rates = 0;
  203. count = 0;
  204. for (i = 0; i < supp_rates_len; i++) {
  205. int rate = (supp_rates[i] & 0x7F) * 5;
  206. for (j = 0; j < sband->n_bitrates; j++)
  207. if (sband->bitrates[j].bitrate == rate) {
  208. *rates |= BIT(j);
  209. count++;
  210. break;
  211. }
  212. }
  213. return count;
  214. }
  215. static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
  216. struct sk_buff *skb, u8 ap_ht_param,
  217. struct ieee80211_supported_band *sband,
  218. struct ieee80211_channel *channel,
  219. enum ieee80211_smps_mode smps)
  220. {
  221. u8 *pos;
  222. u32 flags = channel->flags;
  223. u16 cap;
  224. struct ieee80211_sta_ht_cap ht_cap;
  225. BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
  226. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  227. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  228. /* determine capability flags */
  229. cap = ht_cap.cap;
  230. switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  231. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  232. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  233. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  234. cap &= ~IEEE80211_HT_CAP_SGI_40;
  235. }
  236. break;
  237. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  238. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  239. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  240. cap &= ~IEEE80211_HT_CAP_SGI_40;
  241. }
  242. break;
  243. }
  244. /*
  245. * If 40 MHz was disabled associate as though we weren't
  246. * capable of 40 MHz -- some broken APs will never fall
  247. * back to trying to transmit in 20 MHz.
  248. */
  249. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
  250. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  251. cap &= ~IEEE80211_HT_CAP_SGI_40;
  252. }
  253. /* set SM PS mode properly */
  254. cap &= ~IEEE80211_HT_CAP_SM_PS;
  255. switch (smps) {
  256. case IEEE80211_SMPS_AUTOMATIC:
  257. case IEEE80211_SMPS_NUM_MODES:
  258. WARN_ON(1);
  259. case IEEE80211_SMPS_OFF:
  260. cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
  261. IEEE80211_HT_CAP_SM_PS_SHIFT;
  262. break;
  263. case IEEE80211_SMPS_STATIC:
  264. cap |= WLAN_HT_CAP_SM_PS_STATIC <<
  265. IEEE80211_HT_CAP_SM_PS_SHIFT;
  266. break;
  267. case IEEE80211_SMPS_DYNAMIC:
  268. cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
  269. IEEE80211_HT_CAP_SM_PS_SHIFT;
  270. break;
  271. }
  272. /* reserve and fill IE */
  273. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  274. ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
  275. }
  276. static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
  277. struct sk_buff *skb,
  278. struct ieee80211_supported_band *sband)
  279. {
  280. u8 *pos;
  281. u32 cap;
  282. struct ieee80211_sta_vht_cap vht_cap;
  283. BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
  284. memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
  285. /* determine capability flags */
  286. cap = vht_cap.cap;
  287. /* reserve and fill IE */
  288. pos = skb_put(skb, sizeof(struct ieee80211_vht_capabilities) + 2);
  289. ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
  290. }
  291. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
  292. {
  293. struct ieee80211_local *local = sdata->local;
  294. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  295. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  296. struct sk_buff *skb;
  297. struct ieee80211_mgmt *mgmt;
  298. u8 *pos, qos_info;
  299. size_t offset = 0, noffset;
  300. int i, count, rates_len, supp_rates_len;
  301. u16 capab;
  302. struct ieee80211_supported_band *sband;
  303. u32 rates = 0;
  304. lockdep_assert_held(&ifmgd->mtx);
  305. sband = local->hw.wiphy->bands[local->oper_channel->band];
  306. if (assoc_data->supp_rates_len) {
  307. /*
  308. * Get all rates supported by the device and the AP as
  309. * some APs don't like getting a superset of their rates
  310. * in the association request (e.g. D-Link DAP 1353 in
  311. * b-only mode)...
  312. */
  313. rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
  314. assoc_data->supp_rates_len,
  315. sband, &rates);
  316. } else {
  317. /*
  318. * In case AP not provide any supported rates information
  319. * before association, we send information element(s) with
  320. * all rates that we support.
  321. */
  322. rates = ~0;
  323. rates_len = sband->n_bitrates;
  324. }
  325. skb = alloc_skb(local->hw.extra_tx_headroom +
  326. sizeof(*mgmt) + /* bit too much but doesn't matter */
  327. 2 + assoc_data->ssid_len + /* SSID */
  328. 4 + rates_len + /* (extended) rates */
  329. 4 + /* power capability */
  330. 2 + 2 * sband->n_channels + /* supported channels */
  331. 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
  332. 2 + sizeof(struct ieee80211_vht_capabilities) + /* VHT */
  333. assoc_data->ie_len + /* extra IEs */
  334. 9, /* WMM */
  335. GFP_KERNEL);
  336. if (!skb)
  337. return;
  338. skb_reserve(skb, local->hw.extra_tx_headroom);
  339. capab = WLAN_CAPABILITY_ESS;
  340. if (sband->band == IEEE80211_BAND_2GHZ) {
  341. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  342. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  343. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  344. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  345. }
  346. if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
  347. capab |= WLAN_CAPABILITY_PRIVACY;
  348. if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  349. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  350. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  351. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  352. memset(mgmt, 0, 24);
  353. memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
  354. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  355. memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
  356. if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
  357. skb_put(skb, 10);
  358. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  359. IEEE80211_STYPE_REASSOC_REQ);
  360. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  361. mgmt->u.reassoc_req.listen_interval =
  362. cpu_to_le16(local->hw.conf.listen_interval);
  363. memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
  364. ETH_ALEN);
  365. } else {
  366. skb_put(skb, 4);
  367. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  368. IEEE80211_STYPE_ASSOC_REQ);
  369. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  370. mgmt->u.assoc_req.listen_interval =
  371. cpu_to_le16(local->hw.conf.listen_interval);
  372. }
  373. /* SSID */
  374. pos = skb_put(skb, 2 + assoc_data->ssid_len);
  375. *pos++ = WLAN_EID_SSID;
  376. *pos++ = assoc_data->ssid_len;
  377. memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
  378. /* add all rates which were marked to be used above */
  379. supp_rates_len = rates_len;
  380. if (supp_rates_len > 8)
  381. supp_rates_len = 8;
  382. pos = skb_put(skb, supp_rates_len + 2);
  383. *pos++ = WLAN_EID_SUPP_RATES;
  384. *pos++ = supp_rates_len;
  385. count = 0;
  386. for (i = 0; i < sband->n_bitrates; i++) {
  387. if (BIT(i) & rates) {
  388. int rate = sband->bitrates[i].bitrate;
  389. *pos++ = (u8) (rate / 5);
  390. if (++count == 8)
  391. break;
  392. }
  393. }
  394. if (rates_len > count) {
  395. pos = skb_put(skb, rates_len - count + 2);
  396. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  397. *pos++ = rates_len - count;
  398. for (i++; i < sband->n_bitrates; i++) {
  399. if (BIT(i) & rates) {
  400. int rate = sband->bitrates[i].bitrate;
  401. *pos++ = (u8) (rate / 5);
  402. }
  403. }
  404. }
  405. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  406. /* 1. power capabilities */
  407. pos = skb_put(skb, 4);
  408. *pos++ = WLAN_EID_PWR_CAPABILITY;
  409. *pos++ = 2;
  410. *pos++ = 0; /* min tx power */
  411. *pos++ = local->oper_channel->max_power; /* max tx power */
  412. /* 2. supported channels */
  413. /* TODO: get this in reg domain format */
  414. pos = skb_put(skb, 2 * sband->n_channels + 2);
  415. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  416. *pos++ = 2 * sband->n_channels;
  417. for (i = 0; i < sband->n_channels; i++) {
  418. *pos++ = ieee80211_frequency_to_channel(
  419. sband->channels[i].center_freq);
  420. *pos++ = 1; /* one channel in the subband*/
  421. }
  422. }
  423. /* if present, add any custom IEs that go before HT */
  424. if (assoc_data->ie_len && assoc_data->ie) {
  425. static const u8 before_ht[] = {
  426. WLAN_EID_SSID,
  427. WLAN_EID_SUPP_RATES,
  428. WLAN_EID_EXT_SUPP_RATES,
  429. WLAN_EID_PWR_CAPABILITY,
  430. WLAN_EID_SUPPORTED_CHANNELS,
  431. WLAN_EID_RSN,
  432. WLAN_EID_QOS_CAPA,
  433. WLAN_EID_RRM_ENABLED_CAPABILITIES,
  434. WLAN_EID_MOBILITY_DOMAIN,
  435. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  436. };
  437. noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
  438. before_ht, ARRAY_SIZE(before_ht),
  439. offset);
  440. pos = skb_put(skb, noffset - offset);
  441. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  442. offset = noffset;
  443. }
  444. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  445. ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
  446. sband, local->oper_channel, ifmgd->ap_smps);
  447. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  448. ieee80211_add_vht_ie(sdata, skb, sband);
  449. /* if present, add any custom non-vendor IEs that go after HT */
  450. if (assoc_data->ie_len && assoc_data->ie) {
  451. noffset = ieee80211_ie_split_vendor(assoc_data->ie,
  452. assoc_data->ie_len,
  453. offset);
  454. pos = skb_put(skb, noffset - offset);
  455. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  456. offset = noffset;
  457. }
  458. if (assoc_data->wmm) {
  459. if (assoc_data->uapsd) {
  460. qos_info = ifmgd->uapsd_queues;
  461. qos_info |= (ifmgd->uapsd_max_sp_len <<
  462. IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
  463. } else {
  464. qos_info = 0;
  465. }
  466. pos = skb_put(skb, 9);
  467. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  468. *pos++ = 7; /* len */
  469. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  470. *pos++ = 0x50;
  471. *pos++ = 0xf2;
  472. *pos++ = 2; /* WME */
  473. *pos++ = 0; /* WME info */
  474. *pos++ = 1; /* WME ver */
  475. *pos++ = qos_info;
  476. }
  477. /* add any remaining custom (i.e. vendor specific here) IEs */
  478. if (assoc_data->ie_len && assoc_data->ie) {
  479. noffset = assoc_data->ie_len;
  480. pos = skb_put(skb, noffset - offset);
  481. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  482. }
  483. drv_mgd_prepare_tx(local, sdata);
  484. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  485. ieee80211_tx_skb(sdata, skb);
  486. }
  487. void ieee80211_send_pspoll(struct ieee80211_local *local,
  488. struct ieee80211_sub_if_data *sdata)
  489. {
  490. struct ieee80211_pspoll *pspoll;
  491. struct sk_buff *skb;
  492. skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
  493. if (!skb)
  494. return;
  495. pspoll = (struct ieee80211_pspoll *) skb->data;
  496. pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  497. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  498. ieee80211_tx_skb(sdata, skb);
  499. }
  500. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  501. struct ieee80211_sub_if_data *sdata,
  502. int powersave)
  503. {
  504. struct sk_buff *skb;
  505. struct ieee80211_hdr_3addr *nullfunc;
  506. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  507. skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
  508. if (!skb)
  509. return;
  510. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  511. if (powersave)
  512. nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  513. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  514. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  515. IEEE80211_STA_CONNECTION_POLL))
  516. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
  517. ieee80211_tx_skb(sdata, skb);
  518. }
  519. static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
  520. struct ieee80211_sub_if_data *sdata)
  521. {
  522. struct sk_buff *skb;
  523. struct ieee80211_hdr *nullfunc;
  524. __le16 fc;
  525. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  526. return;
  527. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
  528. if (!skb)
  529. return;
  530. skb_reserve(skb, local->hw.extra_tx_headroom);
  531. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
  532. memset(nullfunc, 0, 30);
  533. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  534. IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  535. nullfunc->frame_control = fc;
  536. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  537. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  538. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  539. memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
  540. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  541. ieee80211_tx_skb(sdata, skb);
  542. }
  543. /* spectrum management related things */
  544. static void ieee80211_chswitch_work(struct work_struct *work)
  545. {
  546. struct ieee80211_sub_if_data *sdata =
  547. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  548. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  549. if (!ieee80211_sdata_running(sdata))
  550. return;
  551. mutex_lock(&ifmgd->mtx);
  552. if (!ifmgd->associated)
  553. goto out;
  554. sdata->local->oper_channel = sdata->local->csa_channel;
  555. if (!sdata->local->ops->channel_switch) {
  556. /* call "hw_config" only if doing sw channel switch */
  557. ieee80211_hw_config(sdata->local,
  558. IEEE80211_CONF_CHANGE_CHANNEL);
  559. } else {
  560. /* update the device channel directly */
  561. sdata->local->hw.conf.channel = sdata->local->oper_channel;
  562. }
  563. /* XXX: shouldn't really modify cfg80211-owned data! */
  564. ifmgd->associated->channel = sdata->local->oper_channel;
  565. /* XXX: wait for a beacon first? */
  566. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  567. IEEE80211_QUEUE_STOP_REASON_CSA);
  568. out:
  569. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  570. mutex_unlock(&ifmgd->mtx);
  571. }
  572. void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
  573. {
  574. struct ieee80211_sub_if_data *sdata;
  575. struct ieee80211_if_managed *ifmgd;
  576. sdata = vif_to_sdata(vif);
  577. ifmgd = &sdata->u.mgd;
  578. trace_api_chswitch_done(sdata, success);
  579. if (!success) {
  580. sdata_info(sdata,
  581. "driver channel switch failed, disconnecting\n");
  582. ieee80211_queue_work(&sdata->local->hw,
  583. &ifmgd->csa_connection_drop_work);
  584. } else {
  585. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  586. }
  587. }
  588. EXPORT_SYMBOL(ieee80211_chswitch_done);
  589. static void ieee80211_chswitch_timer(unsigned long data)
  590. {
  591. struct ieee80211_sub_if_data *sdata =
  592. (struct ieee80211_sub_if_data *) data;
  593. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  594. if (sdata->local->quiescing) {
  595. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  596. return;
  597. }
  598. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  599. }
  600. void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  601. struct ieee80211_channel_sw_ie *sw_elem,
  602. struct ieee80211_bss *bss,
  603. u64 timestamp)
  604. {
  605. struct cfg80211_bss *cbss =
  606. container_of((void *)bss, struct cfg80211_bss, priv);
  607. struct ieee80211_channel *new_ch;
  608. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  609. int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
  610. cbss->channel->band);
  611. ASSERT_MGD_MTX(ifmgd);
  612. if (!ifmgd->associated)
  613. return;
  614. if (sdata->local->scanning)
  615. return;
  616. /* Disregard subsequent beacons if we are already running a timer
  617. processing a CSA */
  618. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  619. return;
  620. new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  621. if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
  622. sdata_info(sdata,
  623. "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
  624. ifmgd->associated->bssid, new_freq);
  625. ieee80211_queue_work(&sdata->local->hw,
  626. &ifmgd->csa_connection_drop_work);
  627. return;
  628. }
  629. sdata->local->csa_channel = new_ch;
  630. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  631. if (sw_elem->mode)
  632. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  633. IEEE80211_QUEUE_STOP_REASON_CSA);
  634. if (sdata->local->ops->channel_switch) {
  635. /* use driver's channel switch callback */
  636. struct ieee80211_channel_switch ch_switch = {
  637. .timestamp = timestamp,
  638. .block_tx = sw_elem->mode,
  639. .channel = new_ch,
  640. .count = sw_elem->count,
  641. };
  642. drv_channel_switch(sdata->local, &ch_switch);
  643. return;
  644. }
  645. /* channel switch handled in software */
  646. if (sw_elem->count <= 1)
  647. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  648. else
  649. mod_timer(&ifmgd->chswitch_timer,
  650. TU_TO_EXP_TIME(sw_elem->count *
  651. cbss->beacon_interval));
  652. }
  653. static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  654. u16 capab_info, u8 *pwr_constr_elem)
  655. {
  656. struct ieee80211_conf *conf = &sdata->local->hw.conf;
  657. if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
  658. return;
  659. if ((*pwr_constr_elem <= conf->channel->max_reg_power) &&
  660. (*pwr_constr_elem != sdata->local->power_constr_level)) {
  661. sdata->local->power_constr_level = *pwr_constr_elem;
  662. ieee80211_hw_config(sdata->local, 0);
  663. }
  664. }
  665. void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
  666. {
  667. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  668. struct ieee80211_local *local = sdata->local;
  669. struct ieee80211_conf *conf = &local->hw.conf;
  670. WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
  671. !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
  672. (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
  673. local->disable_dynamic_ps = false;
  674. conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
  675. }
  676. EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
  677. void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
  678. {
  679. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  680. struct ieee80211_local *local = sdata->local;
  681. struct ieee80211_conf *conf = &local->hw.conf;
  682. WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
  683. !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
  684. (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
  685. local->disable_dynamic_ps = true;
  686. conf->dynamic_ps_timeout = 0;
  687. del_timer_sync(&local->dynamic_ps_timer);
  688. ieee80211_queue_work(&local->hw,
  689. &local->dynamic_ps_enable_work);
  690. }
  691. EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
  692. /* powersave */
  693. static void ieee80211_enable_ps(struct ieee80211_local *local,
  694. struct ieee80211_sub_if_data *sdata)
  695. {
  696. struct ieee80211_conf *conf = &local->hw.conf;
  697. /*
  698. * If we are scanning right now then the parameters will
  699. * take effect when scan finishes.
  700. */
  701. if (local->scanning)
  702. return;
  703. if (conf->dynamic_ps_timeout > 0 &&
  704. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  705. mod_timer(&local->dynamic_ps_timer, jiffies +
  706. msecs_to_jiffies(conf->dynamic_ps_timeout));
  707. } else {
  708. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  709. ieee80211_send_nullfunc(local, sdata, 1);
  710. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  711. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
  712. return;
  713. conf->flags |= IEEE80211_CONF_PS;
  714. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  715. }
  716. }
  717. static void ieee80211_change_ps(struct ieee80211_local *local)
  718. {
  719. struct ieee80211_conf *conf = &local->hw.conf;
  720. if (local->ps_sdata) {
  721. ieee80211_enable_ps(local, local->ps_sdata);
  722. } else if (conf->flags & IEEE80211_CONF_PS) {
  723. conf->flags &= ~IEEE80211_CONF_PS;
  724. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  725. del_timer_sync(&local->dynamic_ps_timer);
  726. cancel_work_sync(&local->dynamic_ps_enable_work);
  727. }
  728. }
  729. static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
  730. {
  731. struct ieee80211_if_managed *mgd = &sdata->u.mgd;
  732. struct sta_info *sta = NULL;
  733. bool authorized = false;
  734. if (!mgd->powersave)
  735. return false;
  736. if (mgd->broken_ap)
  737. return false;
  738. if (!mgd->associated)
  739. return false;
  740. if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
  741. IEEE80211_STA_CONNECTION_POLL))
  742. return false;
  743. rcu_read_lock();
  744. sta = sta_info_get(sdata, mgd->bssid);
  745. if (sta)
  746. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  747. rcu_read_unlock();
  748. return authorized;
  749. }
  750. /* need to hold RTNL or interface lock */
  751. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  752. {
  753. struct ieee80211_sub_if_data *sdata, *found = NULL;
  754. int count = 0;
  755. int timeout;
  756. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  757. local->ps_sdata = NULL;
  758. return;
  759. }
  760. list_for_each_entry(sdata, &local->interfaces, list) {
  761. if (!ieee80211_sdata_running(sdata))
  762. continue;
  763. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  764. /* If an AP vif is found, then disable PS
  765. * by setting the count to zero thereby setting
  766. * ps_sdata to NULL.
  767. */
  768. count = 0;
  769. break;
  770. }
  771. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  772. continue;
  773. found = sdata;
  774. count++;
  775. }
  776. if (count == 1 && ieee80211_powersave_allowed(found)) {
  777. struct ieee80211_conf *conf = &local->hw.conf;
  778. s32 beaconint_us;
  779. if (latency < 0)
  780. latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
  781. beaconint_us = ieee80211_tu_to_usec(
  782. found->vif.bss_conf.beacon_int);
  783. timeout = local->dynamic_ps_forced_timeout;
  784. if (timeout < 0) {
  785. /*
  786. * Go to full PSM if the user configures a very low
  787. * latency requirement.
  788. * The 2000 second value is there for compatibility
  789. * until the PM_QOS_NETWORK_LATENCY is configured
  790. * with real values.
  791. */
  792. if (latency > (1900 * USEC_PER_MSEC) &&
  793. latency != (2000 * USEC_PER_SEC))
  794. timeout = 0;
  795. else
  796. timeout = 100;
  797. }
  798. local->dynamic_ps_user_timeout = timeout;
  799. if (!local->disable_dynamic_ps)
  800. conf->dynamic_ps_timeout =
  801. local->dynamic_ps_user_timeout;
  802. if (beaconint_us > latency) {
  803. local->ps_sdata = NULL;
  804. } else {
  805. struct ieee80211_bss *bss;
  806. int maxslp = 1;
  807. u8 dtimper;
  808. bss = (void *)found->u.mgd.associated->priv;
  809. dtimper = bss->dtim_period;
  810. /* If the TIM IE is invalid, pretend the value is 1 */
  811. if (!dtimper)
  812. dtimper = 1;
  813. else if (dtimper > 1)
  814. maxslp = min_t(int, dtimper,
  815. latency / beaconint_us);
  816. local->hw.conf.max_sleep_period = maxslp;
  817. local->hw.conf.ps_dtim_period = dtimper;
  818. local->ps_sdata = found;
  819. }
  820. } else {
  821. local->ps_sdata = NULL;
  822. }
  823. ieee80211_change_ps(local);
  824. }
  825. void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
  826. {
  827. bool ps_allowed = ieee80211_powersave_allowed(sdata);
  828. if (sdata->vif.bss_conf.ps != ps_allowed) {
  829. sdata->vif.bss_conf.ps = ps_allowed;
  830. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
  831. }
  832. }
  833. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  834. {
  835. struct ieee80211_local *local =
  836. container_of(work, struct ieee80211_local,
  837. dynamic_ps_disable_work);
  838. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  839. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  840. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  841. }
  842. ieee80211_wake_queues_by_reason(&local->hw,
  843. IEEE80211_QUEUE_STOP_REASON_PS);
  844. }
  845. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  846. {
  847. struct ieee80211_local *local =
  848. container_of(work, struct ieee80211_local,
  849. dynamic_ps_enable_work);
  850. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  851. struct ieee80211_if_managed *ifmgd;
  852. unsigned long flags;
  853. int q;
  854. /* can only happen when PS was just disabled anyway */
  855. if (!sdata)
  856. return;
  857. ifmgd = &sdata->u.mgd;
  858. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  859. return;
  860. if (!local->disable_dynamic_ps &&
  861. local->hw.conf.dynamic_ps_timeout > 0) {
  862. /* don't enter PS if TX frames are pending */
  863. if (drv_tx_frames_pending(local)) {
  864. mod_timer(&local->dynamic_ps_timer, jiffies +
  865. msecs_to_jiffies(
  866. local->hw.conf.dynamic_ps_timeout));
  867. return;
  868. }
  869. /*
  870. * transmission can be stopped by others which leads to
  871. * dynamic_ps_timer expiry. Postpone the ps timer if it
  872. * is not the actual idle state.
  873. */
  874. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  875. for (q = 0; q < local->hw.queues; q++) {
  876. if (local->queue_stop_reasons[q]) {
  877. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  878. flags);
  879. mod_timer(&local->dynamic_ps_timer, jiffies +
  880. msecs_to_jiffies(
  881. local->hw.conf.dynamic_ps_timeout));
  882. return;
  883. }
  884. }
  885. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  886. }
  887. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  888. !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  889. netif_tx_stop_all_queues(sdata->dev);
  890. if (drv_tx_frames_pending(local))
  891. mod_timer(&local->dynamic_ps_timer, jiffies +
  892. msecs_to_jiffies(
  893. local->hw.conf.dynamic_ps_timeout));
  894. else {
  895. ieee80211_send_nullfunc(local, sdata, 1);
  896. /* Flush to get the tx status of nullfunc frame */
  897. drv_flush(local, false);
  898. }
  899. }
  900. if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
  901. (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
  902. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  903. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  904. local->hw.conf.flags |= IEEE80211_CONF_PS;
  905. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  906. }
  907. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  908. netif_tx_wake_all_queues(sdata->dev);
  909. }
  910. void ieee80211_dynamic_ps_timer(unsigned long data)
  911. {
  912. struct ieee80211_local *local = (void *) data;
  913. if (local->quiescing || local->suspended)
  914. return;
  915. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  916. }
  917. /* MLME */
  918. static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
  919. struct ieee80211_sub_if_data *sdata,
  920. u8 *wmm_param, size_t wmm_param_len)
  921. {
  922. struct ieee80211_tx_queue_params params;
  923. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  924. size_t left;
  925. int count;
  926. u8 *pos, uapsd_queues = 0;
  927. if (!local->ops->conf_tx)
  928. return false;
  929. if (local->hw.queues < IEEE80211_NUM_ACS)
  930. return false;
  931. if (!wmm_param)
  932. return false;
  933. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  934. return false;
  935. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  936. uapsd_queues = ifmgd->uapsd_queues;
  937. count = wmm_param[6] & 0x0f;
  938. if (count == ifmgd->wmm_last_param_set)
  939. return false;
  940. ifmgd->wmm_last_param_set = count;
  941. pos = wmm_param + 8;
  942. left = wmm_param_len - 8;
  943. memset(&params, 0, sizeof(params));
  944. sdata->wmm_acm = 0;
  945. for (; left >= 4; left -= 4, pos += 4) {
  946. int aci = (pos[0] >> 5) & 0x03;
  947. int acm = (pos[0] >> 4) & 0x01;
  948. bool uapsd = false;
  949. int queue;
  950. switch (aci) {
  951. case 1: /* AC_BK */
  952. queue = 3;
  953. if (acm)
  954. sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  955. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  956. uapsd = true;
  957. break;
  958. case 2: /* AC_VI */
  959. queue = 1;
  960. if (acm)
  961. sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  962. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  963. uapsd = true;
  964. break;
  965. case 3: /* AC_VO */
  966. queue = 0;
  967. if (acm)
  968. sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  969. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  970. uapsd = true;
  971. break;
  972. case 0: /* AC_BE */
  973. default:
  974. queue = 2;
  975. if (acm)
  976. sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  977. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  978. uapsd = true;
  979. break;
  980. }
  981. params.aifs = pos[0] & 0x0f;
  982. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  983. params.cw_min = ecw2cw(pos[1] & 0x0f);
  984. params.txop = get_unaligned_le16(pos + 2);
  985. params.uapsd = uapsd;
  986. mlme_dbg(sdata,
  987. "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
  988. queue, aci, acm,
  989. params.aifs, params.cw_min, params.cw_max,
  990. params.txop, params.uapsd);
  991. sdata->tx_conf[queue] = params;
  992. if (drv_conf_tx(local, sdata, queue, &params))
  993. sdata_err(sdata,
  994. "failed to set TX queue parameters for queue %d\n",
  995. queue);
  996. }
  997. /* enable WMM or activate new settings */
  998. sdata->vif.bss_conf.qos = true;
  999. return true;
  1000. }
  1001. static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1002. {
  1003. lockdep_assert_held(&sdata->local->mtx);
  1004. sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1005. IEEE80211_STA_BEACON_POLL);
  1006. ieee80211_run_deferred_scan(sdata->local);
  1007. }
  1008. static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1009. {
  1010. mutex_lock(&sdata->local->mtx);
  1011. __ieee80211_stop_poll(sdata);
  1012. mutex_unlock(&sdata->local->mtx);
  1013. }
  1014. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  1015. u16 capab, bool erp_valid, u8 erp)
  1016. {
  1017. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1018. u32 changed = 0;
  1019. bool use_protection;
  1020. bool use_short_preamble;
  1021. bool use_short_slot;
  1022. if (erp_valid) {
  1023. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  1024. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  1025. } else {
  1026. use_protection = false;
  1027. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  1028. }
  1029. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1030. if (sdata->local->oper_channel->band == IEEE80211_BAND_5GHZ)
  1031. use_short_slot = true;
  1032. if (use_protection != bss_conf->use_cts_prot) {
  1033. bss_conf->use_cts_prot = use_protection;
  1034. changed |= BSS_CHANGED_ERP_CTS_PROT;
  1035. }
  1036. if (use_short_preamble != bss_conf->use_short_preamble) {
  1037. bss_conf->use_short_preamble = use_short_preamble;
  1038. changed |= BSS_CHANGED_ERP_PREAMBLE;
  1039. }
  1040. if (use_short_slot != bss_conf->use_short_slot) {
  1041. bss_conf->use_short_slot = use_short_slot;
  1042. changed |= BSS_CHANGED_ERP_SLOT;
  1043. }
  1044. return changed;
  1045. }
  1046. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  1047. struct cfg80211_bss *cbss,
  1048. u32 bss_info_changed)
  1049. {
  1050. struct ieee80211_bss *bss = (void *)cbss->priv;
  1051. struct ieee80211_local *local = sdata->local;
  1052. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1053. bss_info_changed |= BSS_CHANGED_ASSOC;
  1054. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  1055. bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
  1056. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  1057. IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
  1058. sdata->u.mgd.associated = cbss;
  1059. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  1060. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  1061. /* just to be sure */
  1062. ieee80211_stop_poll(sdata);
  1063. ieee80211_led_assoc(local, 1);
  1064. if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
  1065. bss_conf->dtim_period = bss->dtim_period;
  1066. else
  1067. bss_conf->dtim_period = 0;
  1068. bss_conf->assoc = 1;
  1069. /* Tell the driver to monitor connection quality (if supported) */
  1070. if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
  1071. bss_conf->cqm_rssi_thold)
  1072. bss_info_changed |= BSS_CHANGED_CQM;
  1073. /* Enable ARP filtering */
  1074. if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
  1075. bss_conf->arp_filter_enabled = sdata->arp_filter_state;
  1076. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  1077. }
  1078. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  1079. mutex_lock(&local->iflist_mtx);
  1080. ieee80211_recalc_ps(local, -1);
  1081. ieee80211_recalc_smps(local);
  1082. mutex_unlock(&local->iflist_mtx);
  1083. ieee80211_recalc_ps_vif(sdata);
  1084. netif_tx_start_all_queues(sdata->dev);
  1085. netif_carrier_on(sdata->dev);
  1086. }
  1087. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  1088. u16 stype, u16 reason, bool tx,
  1089. u8 *frame_buf)
  1090. {
  1091. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1092. struct ieee80211_local *local = sdata->local;
  1093. struct sta_info *sta;
  1094. u32 changed = 0;
  1095. ASSERT_MGD_MTX(ifmgd);
  1096. if (WARN_ON_ONCE(tx && !frame_buf))
  1097. return;
  1098. if (WARN_ON(!ifmgd->associated))
  1099. return;
  1100. ieee80211_stop_poll(sdata);
  1101. ifmgd->associated = NULL;
  1102. /*
  1103. * we need to commit the associated = NULL change because the
  1104. * scan code uses that to determine whether this iface should
  1105. * go to/wake up from powersave or not -- and could otherwise
  1106. * wake the queues erroneously.
  1107. */
  1108. smp_mb();
  1109. /*
  1110. * Thus, we can only afterwards stop the queues -- to account
  1111. * for the case where another CPU is finishing a scan at this
  1112. * time -- we don't want the scan code to enable queues.
  1113. */
  1114. netif_tx_stop_all_queues(sdata->dev);
  1115. netif_carrier_off(sdata->dev);
  1116. mutex_lock(&local->sta_mtx);
  1117. sta = sta_info_get(sdata, ifmgd->bssid);
  1118. if (sta) {
  1119. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1120. ieee80211_sta_tear_down_BA_sessions(sta, tx);
  1121. }
  1122. mutex_unlock(&local->sta_mtx);
  1123. /*
  1124. * if we want to get out of ps before disassoc (why?) we have
  1125. * to do it before sending disassoc, as otherwise the null-packet
  1126. * won't be valid.
  1127. */
  1128. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1129. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1130. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1131. }
  1132. local->ps_sdata = NULL;
  1133. /* disable per-vif ps */
  1134. ieee80211_recalc_ps_vif(sdata);
  1135. /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
  1136. if (tx)
  1137. drv_flush(local, false);
  1138. /* deauthenticate/disassociate now */
  1139. if (tx || frame_buf)
  1140. ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
  1141. reason, tx, frame_buf);
  1142. /* flush out frame */
  1143. if (tx)
  1144. drv_flush(local, false);
  1145. /* clear bssid only after building the needed mgmt frames */
  1146. memset(ifmgd->bssid, 0, ETH_ALEN);
  1147. /* remove AP and TDLS peers */
  1148. sta_info_flush(local, sdata);
  1149. /* finally reset all BSS / config parameters */
  1150. changed |= ieee80211_reset_erp_info(sdata);
  1151. ieee80211_led_assoc(local, 0);
  1152. changed |= BSS_CHANGED_ASSOC;
  1153. sdata->vif.bss_conf.assoc = false;
  1154. /* on the next assoc, re-program HT parameters */
  1155. memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
  1156. memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
  1157. local->power_constr_level = 0;
  1158. del_timer_sync(&local->dynamic_ps_timer);
  1159. cancel_work_sync(&local->dynamic_ps_enable_work);
  1160. /* Disable ARP filtering */
  1161. if (sdata->vif.bss_conf.arp_filter_enabled) {
  1162. sdata->vif.bss_conf.arp_filter_enabled = false;
  1163. changed |= BSS_CHANGED_ARP_FILTER;
  1164. }
  1165. sdata->vif.bss_conf.qos = false;
  1166. changed |= BSS_CHANGED_QOS;
  1167. /* The BSSID (not really interesting) and HT changed */
  1168. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  1169. ieee80211_bss_info_change_notify(sdata, changed);
  1170. /* channel(_type) changes are handled by ieee80211_hw_config */
  1171. WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
  1172. ieee80211_hw_config(local, 0);
  1173. /* disassociated - set to defaults now */
  1174. ieee80211_set_wmm_default(sdata, false);
  1175. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  1176. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  1177. del_timer_sync(&sdata->u.mgd.timer);
  1178. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  1179. sdata->u.mgd.timers_running = 0;
  1180. }
  1181. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  1182. struct ieee80211_hdr *hdr)
  1183. {
  1184. /*
  1185. * We can postpone the mgd.timer whenever receiving unicast frames
  1186. * from AP because we know that the connection is working both ways
  1187. * at that time. But multicast frames (and hence also beacons) must
  1188. * be ignored here, because we need to trigger the timer during
  1189. * data idle periods for sending the periodic probe request to the
  1190. * AP we're connected to.
  1191. */
  1192. if (is_multicast_ether_addr(hdr->addr1))
  1193. return;
  1194. ieee80211_sta_reset_conn_monitor(sdata);
  1195. }
  1196. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  1197. {
  1198. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1199. struct ieee80211_local *local = sdata->local;
  1200. mutex_lock(&local->mtx);
  1201. if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1202. IEEE80211_STA_CONNECTION_POLL))) {
  1203. mutex_unlock(&local->mtx);
  1204. return;
  1205. }
  1206. __ieee80211_stop_poll(sdata);
  1207. mutex_lock(&local->iflist_mtx);
  1208. ieee80211_recalc_ps(local, -1);
  1209. mutex_unlock(&local->iflist_mtx);
  1210. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  1211. goto out;
  1212. /*
  1213. * We've received a probe response, but are not sure whether
  1214. * we have or will be receiving any beacons or data, so let's
  1215. * schedule the timers again, just in case.
  1216. */
  1217. ieee80211_sta_reset_beacon_monitor(sdata);
  1218. mod_timer(&ifmgd->conn_mon_timer,
  1219. round_jiffies_up(jiffies +
  1220. IEEE80211_CONNECTION_IDLE_TIME));
  1221. out:
  1222. mutex_unlock(&local->mtx);
  1223. }
  1224. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  1225. struct ieee80211_hdr *hdr, bool ack)
  1226. {
  1227. if (!ieee80211_is_data(hdr->frame_control))
  1228. return;
  1229. if (ack)
  1230. ieee80211_sta_reset_conn_monitor(sdata);
  1231. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  1232. sdata->u.mgd.probe_send_count > 0) {
  1233. if (ack)
  1234. sdata->u.mgd.probe_send_count = 0;
  1235. else
  1236. sdata->u.mgd.nullfunc_failed = true;
  1237. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1238. }
  1239. }
  1240. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  1241. {
  1242. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1243. const u8 *ssid;
  1244. u8 *dst = ifmgd->associated->bssid;
  1245. u8 unicast_limit = max(1, max_probe_tries - 3);
  1246. /*
  1247. * Try sending broadcast probe requests for the last three
  1248. * probe requests after the first ones failed since some
  1249. * buggy APs only support broadcast probe requests.
  1250. */
  1251. if (ifmgd->probe_send_count >= unicast_limit)
  1252. dst = NULL;
  1253. /*
  1254. * When the hardware reports an accurate Tx ACK status, it's
  1255. * better to send a nullfunc frame instead of a probe request,
  1256. * as it will kick us off the AP quickly if we aren't associated
  1257. * anymore. The timeout will be reset if the frame is ACKed by
  1258. * the AP.
  1259. */
  1260. ifmgd->probe_send_count++;
  1261. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  1262. ifmgd->nullfunc_failed = false;
  1263. ieee80211_send_nullfunc(sdata->local, sdata, 0);
  1264. } else {
  1265. int ssid_len;
  1266. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1267. if (WARN_ON_ONCE(ssid == NULL))
  1268. ssid_len = 0;
  1269. else
  1270. ssid_len = ssid[1];
  1271. ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
  1272. 0, (u32) -1, true, false,
  1273. ifmgd->associated->channel);
  1274. }
  1275. ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
  1276. run_again(ifmgd, ifmgd->probe_timeout);
  1277. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1278. drv_flush(sdata->local, false);
  1279. }
  1280. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  1281. bool beacon)
  1282. {
  1283. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1284. bool already = false;
  1285. if (!ieee80211_sdata_running(sdata))
  1286. return;
  1287. mutex_lock(&ifmgd->mtx);
  1288. if (!ifmgd->associated)
  1289. goto out;
  1290. mutex_lock(&sdata->local->mtx);
  1291. if (sdata->local->tmp_channel || sdata->local->scanning) {
  1292. mutex_unlock(&sdata->local->mtx);
  1293. goto out;
  1294. }
  1295. if (beacon)
  1296. mlme_dbg_ratelimited(sdata,
  1297. "detected beacon loss from AP - sending probe request\n");
  1298. ieee80211_cqm_rssi_notify(&sdata->vif,
  1299. NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
  1300. /*
  1301. * The driver/our work has already reported this event or the
  1302. * connection monitoring has kicked in and we have already sent
  1303. * a probe request. Or maybe the AP died and the driver keeps
  1304. * reporting until we disassociate...
  1305. *
  1306. * In either case we have to ignore the current call to this
  1307. * function (except for setting the correct probe reason bit)
  1308. * because otherwise we would reset the timer every time and
  1309. * never check whether we received a probe response!
  1310. */
  1311. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1312. IEEE80211_STA_CONNECTION_POLL))
  1313. already = true;
  1314. if (beacon)
  1315. ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
  1316. else
  1317. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  1318. mutex_unlock(&sdata->local->mtx);
  1319. if (already)
  1320. goto out;
  1321. mutex_lock(&sdata->local->iflist_mtx);
  1322. ieee80211_recalc_ps(sdata->local, -1);
  1323. mutex_unlock(&sdata->local->iflist_mtx);
  1324. ifmgd->probe_send_count = 0;
  1325. ieee80211_mgd_probe_ap_send(sdata);
  1326. out:
  1327. mutex_unlock(&ifmgd->mtx);
  1328. }
  1329. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  1330. struct ieee80211_vif *vif)
  1331. {
  1332. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1333. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1334. struct cfg80211_bss *cbss;
  1335. struct sk_buff *skb;
  1336. const u8 *ssid;
  1337. int ssid_len;
  1338. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1339. return NULL;
  1340. ASSERT_MGD_MTX(ifmgd);
  1341. if (ifmgd->associated)
  1342. cbss = ifmgd->associated;
  1343. else if (ifmgd->auth_data)
  1344. cbss = ifmgd->auth_data->bss;
  1345. else if (ifmgd->assoc_data)
  1346. cbss = ifmgd->assoc_data->bss;
  1347. else
  1348. return NULL;
  1349. ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
  1350. if (WARN_ON_ONCE(ssid == NULL))
  1351. ssid_len = 0;
  1352. else
  1353. ssid_len = ssid[1];
  1354. skb = ieee80211_build_probe_req(sdata, cbss->bssid,
  1355. (u32) -1,
  1356. sdata->local->oper_channel,
  1357. ssid + 2, ssid_len,
  1358. NULL, 0, true);
  1359. return skb;
  1360. }
  1361. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  1362. static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
  1363. bool transmit_frame)
  1364. {
  1365. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1366. struct ieee80211_local *local = sdata->local;
  1367. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  1368. mutex_lock(&ifmgd->mtx);
  1369. if (!ifmgd->associated) {
  1370. mutex_unlock(&ifmgd->mtx);
  1371. return;
  1372. }
  1373. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  1374. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1375. transmit_frame, frame_buf);
  1376. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  1377. mutex_unlock(&ifmgd->mtx);
  1378. /*
  1379. * must be outside lock due to cfg80211,
  1380. * but that's not a problem.
  1381. */
  1382. cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
  1383. mutex_lock(&local->mtx);
  1384. ieee80211_recalc_idle(local);
  1385. mutex_unlock(&local->mtx);
  1386. }
  1387. static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  1388. {
  1389. struct ieee80211_sub_if_data *sdata =
  1390. container_of(work, struct ieee80211_sub_if_data,
  1391. u.mgd.beacon_connection_loss_work);
  1392. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1393. struct sta_info *sta;
  1394. if (ifmgd->associated) {
  1395. rcu_read_lock();
  1396. sta = sta_info_get(sdata, ifmgd->bssid);
  1397. if (sta)
  1398. sta->beacon_loss_count++;
  1399. rcu_read_unlock();
  1400. }
  1401. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
  1402. sdata_info(sdata, "Connection to AP %pM lost\n",
  1403. ifmgd->bssid);
  1404. __ieee80211_disconnect(sdata, false);
  1405. } else {
  1406. ieee80211_mgd_probe_ap(sdata, true);
  1407. }
  1408. }
  1409. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  1410. {
  1411. struct ieee80211_sub_if_data *sdata =
  1412. container_of(work, struct ieee80211_sub_if_data,
  1413. u.mgd.csa_connection_drop_work);
  1414. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  1415. IEEE80211_QUEUE_STOP_REASON_CSA);
  1416. __ieee80211_disconnect(sdata, true);
  1417. }
  1418. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  1419. {
  1420. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1421. struct ieee80211_hw *hw = &sdata->local->hw;
  1422. trace_api_beacon_loss(sdata);
  1423. WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
  1424. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1425. }
  1426. EXPORT_SYMBOL(ieee80211_beacon_loss);
  1427. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  1428. {
  1429. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1430. struct ieee80211_hw *hw = &sdata->local->hw;
  1431. trace_api_connection_loss(sdata);
  1432. WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
  1433. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1434. }
  1435. EXPORT_SYMBOL(ieee80211_connection_loss);
  1436. static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
  1437. bool assoc)
  1438. {
  1439. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1440. lockdep_assert_held(&sdata->u.mgd.mtx);
  1441. if (!assoc) {
  1442. sta_info_destroy_addr(sdata, auth_data->bss->bssid);
  1443. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1444. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1445. }
  1446. cfg80211_put_bss(auth_data->bss);
  1447. kfree(auth_data);
  1448. sdata->u.mgd.auth_data = NULL;
  1449. }
  1450. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  1451. struct ieee80211_mgmt *mgmt, size_t len)
  1452. {
  1453. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1454. u8 *pos;
  1455. struct ieee802_11_elems elems;
  1456. pos = mgmt->u.auth.variable;
  1457. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1458. if (!elems.challenge)
  1459. return;
  1460. auth_data->expected_transaction = 4;
  1461. drv_mgd_prepare_tx(sdata->local, sdata);
  1462. ieee80211_send_auth(sdata, 3, auth_data->algorithm,
  1463. elems.challenge - 2, elems.challenge_len + 2,
  1464. auth_data->bss->bssid, auth_data->bss->bssid,
  1465. auth_data->key, auth_data->key_len,
  1466. auth_data->key_idx);
  1467. }
  1468. static enum rx_mgmt_action __must_check
  1469. ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  1470. struct ieee80211_mgmt *mgmt, size_t len)
  1471. {
  1472. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1473. u8 bssid[ETH_ALEN];
  1474. u16 auth_alg, auth_transaction, status_code;
  1475. struct sta_info *sta;
  1476. lockdep_assert_held(&ifmgd->mtx);
  1477. if (len < 24 + 6)
  1478. return RX_MGMT_NONE;
  1479. if (!ifmgd->auth_data || ifmgd->auth_data->done)
  1480. return RX_MGMT_NONE;
  1481. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  1482. if (!ether_addr_equal(bssid, mgmt->bssid))
  1483. return RX_MGMT_NONE;
  1484. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  1485. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  1486. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  1487. if (auth_alg != ifmgd->auth_data->algorithm ||
  1488. auth_transaction != ifmgd->auth_data->expected_transaction)
  1489. return RX_MGMT_NONE;
  1490. if (status_code != WLAN_STATUS_SUCCESS) {
  1491. sdata_info(sdata, "%pM denied authentication (status %d)\n",
  1492. mgmt->sa, status_code);
  1493. ieee80211_destroy_auth_data(sdata, false);
  1494. return RX_MGMT_CFG80211_RX_AUTH;
  1495. }
  1496. switch (ifmgd->auth_data->algorithm) {
  1497. case WLAN_AUTH_OPEN:
  1498. case WLAN_AUTH_LEAP:
  1499. case WLAN_AUTH_FT:
  1500. break;
  1501. case WLAN_AUTH_SHARED_KEY:
  1502. if (ifmgd->auth_data->expected_transaction != 4) {
  1503. ieee80211_auth_challenge(sdata, mgmt, len);
  1504. /* need another frame */
  1505. return RX_MGMT_NONE;
  1506. }
  1507. break;
  1508. default:
  1509. WARN_ONCE(1, "invalid auth alg %d",
  1510. ifmgd->auth_data->algorithm);
  1511. return RX_MGMT_NONE;
  1512. }
  1513. sdata_info(sdata, "authenticated\n");
  1514. ifmgd->auth_data->done = true;
  1515. ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
  1516. run_again(ifmgd, ifmgd->auth_data->timeout);
  1517. /* move station state to auth */
  1518. mutex_lock(&sdata->local->sta_mtx);
  1519. sta = sta_info_get(sdata, bssid);
  1520. if (!sta) {
  1521. WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
  1522. goto out_err;
  1523. }
  1524. if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
  1525. sdata_info(sdata, "failed moving %pM to auth\n", bssid);
  1526. goto out_err;
  1527. }
  1528. mutex_unlock(&sdata->local->sta_mtx);
  1529. return RX_MGMT_CFG80211_RX_AUTH;
  1530. out_err:
  1531. mutex_unlock(&sdata->local->sta_mtx);
  1532. /* ignore frame -- wait for timeout */
  1533. return RX_MGMT_NONE;
  1534. }
  1535. static enum rx_mgmt_action __must_check
  1536. ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1537. struct ieee80211_mgmt *mgmt, size_t len)
  1538. {
  1539. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1540. const u8 *bssid = NULL;
  1541. u16 reason_code;
  1542. lockdep_assert_held(&ifmgd->mtx);
  1543. if (len < 24 + 2)
  1544. return RX_MGMT_NONE;
  1545. if (!ifmgd->associated ||
  1546. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1547. return RX_MGMT_NONE;
  1548. bssid = ifmgd->associated->bssid;
  1549. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1550. sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
  1551. bssid, reason_code);
  1552. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1553. mutex_lock(&sdata->local->mtx);
  1554. ieee80211_recalc_idle(sdata->local);
  1555. mutex_unlock(&sdata->local->mtx);
  1556. return RX_MGMT_CFG80211_DEAUTH;
  1557. }
  1558. static enum rx_mgmt_action __must_check
  1559. ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1560. struct ieee80211_mgmt *mgmt, size_t len)
  1561. {
  1562. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1563. u16 reason_code;
  1564. lockdep_assert_held(&ifmgd->mtx);
  1565. if (len < 24 + 2)
  1566. return RX_MGMT_NONE;
  1567. if (!ifmgd->associated ||
  1568. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1569. return RX_MGMT_NONE;
  1570. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1571. sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
  1572. mgmt->sa, reason_code);
  1573. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1574. mutex_lock(&sdata->local->mtx);
  1575. ieee80211_recalc_idle(sdata->local);
  1576. mutex_unlock(&sdata->local->mtx);
  1577. return RX_MGMT_CFG80211_DISASSOC;
  1578. }
  1579. static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  1580. u8 *supp_rates, unsigned int supp_rates_len,
  1581. u32 *rates, u32 *basic_rates,
  1582. bool *have_higher_than_11mbit,
  1583. int *min_rate, int *min_rate_index)
  1584. {
  1585. int i, j;
  1586. for (i = 0; i < supp_rates_len; i++) {
  1587. int rate = (supp_rates[i] & 0x7f) * 5;
  1588. bool is_basic = !!(supp_rates[i] & 0x80);
  1589. if (rate > 110)
  1590. *have_higher_than_11mbit = true;
  1591. /*
  1592. * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
  1593. * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
  1594. *
  1595. * Note: Even through the membership selector and the basic
  1596. * rate flag share the same bit, they are not exactly
  1597. * the same.
  1598. */
  1599. if (!!(supp_rates[i] & 0x80) &&
  1600. (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1601. continue;
  1602. for (j = 0; j < sband->n_bitrates; j++) {
  1603. if (sband->bitrates[j].bitrate == rate) {
  1604. *rates |= BIT(j);
  1605. if (is_basic)
  1606. *basic_rates |= BIT(j);
  1607. if (rate < *min_rate) {
  1608. *min_rate = rate;
  1609. *min_rate_index = j;
  1610. }
  1611. break;
  1612. }
  1613. }
  1614. }
  1615. }
  1616. static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  1617. bool assoc)
  1618. {
  1619. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  1620. lockdep_assert_held(&sdata->u.mgd.mtx);
  1621. if (!assoc) {
  1622. sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
  1623. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1624. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1625. }
  1626. kfree(assoc_data);
  1627. sdata->u.mgd.assoc_data = NULL;
  1628. }
  1629. static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
  1630. struct cfg80211_bss *cbss,
  1631. struct ieee80211_mgmt *mgmt, size_t len)
  1632. {
  1633. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1634. struct ieee80211_local *local = sdata->local;
  1635. struct ieee80211_supported_band *sband;
  1636. struct sta_info *sta;
  1637. u8 *pos;
  1638. u16 capab_info, aid;
  1639. struct ieee802_11_elems elems;
  1640. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1641. u32 changed = 0;
  1642. int err;
  1643. /* AssocResp and ReassocResp have identical structure */
  1644. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1645. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1646. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1647. sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
  1648. aid);
  1649. aid &= ~(BIT(15) | BIT(14));
  1650. ifmgd->broken_ap = false;
  1651. if (aid == 0 || aid > IEEE80211_MAX_AID) {
  1652. sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
  1653. aid);
  1654. aid = 0;
  1655. ifmgd->broken_ap = true;
  1656. }
  1657. pos = mgmt->u.assoc_resp.variable;
  1658. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1659. if (!elems.supp_rates) {
  1660. sdata_info(sdata, "no SuppRates element in AssocResp\n");
  1661. return false;
  1662. }
  1663. ifmgd->aid = aid;
  1664. mutex_lock(&sdata->local->sta_mtx);
  1665. /*
  1666. * station info was already allocated and inserted before
  1667. * the association and should be available to us
  1668. */
  1669. sta = sta_info_get(sdata, cbss->bssid);
  1670. if (WARN_ON(!sta)) {
  1671. mutex_unlock(&sdata->local->sta_mtx);
  1672. return false;
  1673. }
  1674. sband = local->hw.wiphy->bands[local->oper_channel->band];
  1675. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1676. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  1677. elems.ht_cap_elem, &sta->sta.ht_cap);
  1678. sta->supports_40mhz =
  1679. sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  1680. rate_control_rate_init(sta);
  1681. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  1682. set_sta_flag(sta, WLAN_STA_MFP);
  1683. if (elems.wmm_param)
  1684. set_sta_flag(sta, WLAN_STA_WME);
  1685. err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
  1686. if (!err)
  1687. err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  1688. if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  1689. err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  1690. if (err) {
  1691. sdata_info(sdata,
  1692. "failed to move station %pM to desired state\n",
  1693. sta->sta.addr);
  1694. WARN_ON(__sta_info_destroy(sta));
  1695. mutex_unlock(&sdata->local->sta_mtx);
  1696. return false;
  1697. }
  1698. mutex_unlock(&sdata->local->sta_mtx);
  1699. /*
  1700. * Always handle WMM once after association regardless
  1701. * of the first value the AP uses. Setting -1 here has
  1702. * that effect because the AP values is an unsigned
  1703. * 4-bit value.
  1704. */
  1705. ifmgd->wmm_last_param_set = -1;
  1706. if (elems.wmm_param)
  1707. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  1708. elems.wmm_param_len);
  1709. else
  1710. ieee80211_set_wmm_default(sdata, false);
  1711. changed |= BSS_CHANGED_QOS;
  1712. if (elems.ht_operation && elems.wmm_param &&
  1713. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1714. changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
  1715. cbss->bssid, false);
  1716. /* set AID and assoc capability,
  1717. * ieee80211_set_associated() will tell the driver */
  1718. bss_conf->aid = aid;
  1719. bss_conf->assoc_capability = capab_info;
  1720. ieee80211_set_associated(sdata, cbss, changed);
  1721. /*
  1722. * If we're using 4-addr mode, let the AP know that we're
  1723. * doing so, so that it can create the STA VLAN on its side
  1724. */
  1725. if (ifmgd->use_4addr)
  1726. ieee80211_send_4addr_nullfunc(local, sdata);
  1727. /*
  1728. * Start timer to probe the connection to the AP now.
  1729. * Also start the timer that will detect beacon loss.
  1730. */
  1731. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  1732. ieee80211_sta_reset_beacon_monitor(sdata);
  1733. return true;
  1734. }
  1735. static enum rx_mgmt_action __must_check
  1736. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  1737. struct ieee80211_mgmt *mgmt, size_t len,
  1738. struct cfg80211_bss **bss)
  1739. {
  1740. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1741. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  1742. u16 capab_info, status_code, aid;
  1743. struct ieee802_11_elems elems;
  1744. u8 *pos;
  1745. bool reassoc;
  1746. lockdep_assert_held(&ifmgd->mtx);
  1747. if (!assoc_data)
  1748. return RX_MGMT_NONE;
  1749. if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
  1750. return RX_MGMT_NONE;
  1751. /*
  1752. * AssocResp and ReassocResp have identical structure, so process both
  1753. * of them in this function.
  1754. */
  1755. if (len < 24 + 6)
  1756. return RX_MGMT_NONE;
  1757. reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
  1758. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1759. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1760. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1761. sdata_info(sdata,
  1762. "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
  1763. reassoc ? "Rea" : "A", mgmt->sa,
  1764. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1765. pos = mgmt->u.assoc_resp.variable;
  1766. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1767. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  1768. elems.timeout_int && elems.timeout_int_len == 5 &&
  1769. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  1770. u32 tu, ms;
  1771. tu = get_unaligned_le32(elems.timeout_int + 1);
  1772. ms = tu * 1024 / 1000;
  1773. sdata_info(sdata,
  1774. "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
  1775. mgmt->sa, tu, ms);
  1776. assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
  1777. if (ms > IEEE80211_ASSOC_TIMEOUT)
  1778. run_again(ifmgd, assoc_data->timeout);
  1779. return RX_MGMT_NONE;
  1780. }
  1781. *bss = assoc_data->bss;
  1782. if (status_code != WLAN_STATUS_SUCCESS) {
  1783. sdata_info(sdata, "%pM denied association (code=%d)\n",
  1784. mgmt->sa, status_code);
  1785. ieee80211_destroy_assoc_data(sdata, false);
  1786. } else {
  1787. if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
  1788. /* oops -- internal error -- send timeout for now */
  1789. ieee80211_destroy_assoc_data(sdata, false);
  1790. cfg80211_put_bss(*bss);
  1791. return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
  1792. }
  1793. sdata_info(sdata, "associated\n");
  1794. /*
  1795. * destroy assoc_data afterwards, as otherwise an idle
  1796. * recalc after assoc_data is NULL but before associated
  1797. * is set can cause the interface to go idle
  1798. */
  1799. ieee80211_destroy_assoc_data(sdata, true);
  1800. }
  1801. return RX_MGMT_CFG80211_RX_ASSOC;
  1802. }
  1803. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1804. struct ieee80211_mgmt *mgmt,
  1805. size_t len,
  1806. struct ieee80211_rx_status *rx_status,
  1807. struct ieee802_11_elems *elems,
  1808. bool beacon)
  1809. {
  1810. struct ieee80211_local *local = sdata->local;
  1811. int freq;
  1812. struct ieee80211_bss *bss;
  1813. struct ieee80211_channel *channel;
  1814. bool need_ps = false;
  1815. if (sdata->u.mgd.associated &&
  1816. ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) {
  1817. bss = (void *)sdata->u.mgd.associated->priv;
  1818. /* not previously set so we may need to recalc */
  1819. need_ps = !bss->dtim_period;
  1820. }
  1821. if (elems->ds_params && elems->ds_params_len == 1)
  1822. freq = ieee80211_channel_to_frequency(elems->ds_params[0],
  1823. rx_status->band);
  1824. else
  1825. freq = rx_status->freq;
  1826. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1827. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1828. return;
  1829. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1830. channel, beacon);
  1831. if (bss)
  1832. ieee80211_rx_bss_put(local, bss);
  1833. if (!sdata->u.mgd.associated)
  1834. return;
  1835. if (need_ps) {
  1836. mutex_lock(&local->iflist_mtx);
  1837. ieee80211_recalc_ps(local, -1);
  1838. mutex_unlock(&local->iflist_mtx);
  1839. }
  1840. if (elems->ch_switch_ie &&
  1841. memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
  1842. ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
  1843. bss, rx_status->mactime);
  1844. }
  1845. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1846. struct sk_buff *skb)
  1847. {
  1848. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1849. struct ieee80211_if_managed *ifmgd;
  1850. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  1851. size_t baselen, len = skb->len;
  1852. struct ieee802_11_elems elems;
  1853. ifmgd = &sdata->u.mgd;
  1854. ASSERT_MGD_MTX(ifmgd);
  1855. if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
  1856. return; /* ignore ProbeResp to foreign address */
  1857. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1858. if (baselen > len)
  1859. return;
  1860. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1861. &elems);
  1862. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1863. if (ifmgd->associated &&
  1864. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1865. ieee80211_reset_ap_probe(sdata);
  1866. if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
  1867. ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
  1868. /* got probe response, continue with auth */
  1869. sdata_info(sdata, "direct probe responded\n");
  1870. ifmgd->auth_data->tries = 0;
  1871. ifmgd->auth_data->timeout = jiffies;
  1872. run_again(ifmgd, ifmgd->auth_data->timeout);
  1873. }
  1874. }
  1875. /*
  1876. * This is the canonical list of information elements we care about,
  1877. * the filter code also gives us all changes to the Microsoft OUI
  1878. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  1879. *
  1880. * We implement beacon filtering in software since that means we can
  1881. * avoid processing the frame here and in cfg80211, and userspace
  1882. * will not be able to tell whether the hardware supports it or not.
  1883. *
  1884. * XXX: This list needs to be dynamic -- userspace needs to be able to
  1885. * add items it requires. It also needs to be able to tell us to
  1886. * look out for other vendor IEs.
  1887. */
  1888. static const u64 care_about_ies =
  1889. (1ULL << WLAN_EID_COUNTRY) |
  1890. (1ULL << WLAN_EID_ERP_INFO) |
  1891. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  1892. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  1893. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1894. (1ULL << WLAN_EID_HT_OPERATION);
  1895. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1896. struct ieee80211_mgmt *mgmt,
  1897. size_t len,
  1898. struct ieee80211_rx_status *rx_status)
  1899. {
  1900. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1901. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1902. size_t baselen;
  1903. struct ieee802_11_elems elems;
  1904. struct ieee80211_local *local = sdata->local;
  1905. u32 changed = 0;
  1906. bool erp_valid, directed_tim = false;
  1907. u8 erp_value = 0;
  1908. u32 ncrc;
  1909. u8 *bssid;
  1910. lockdep_assert_held(&ifmgd->mtx);
  1911. /* Process beacon from the current BSS */
  1912. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1913. if (baselen > len)
  1914. return;
  1915. if (rx_status->freq != local->oper_channel->center_freq)
  1916. return;
  1917. if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
  1918. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  1919. ieee802_11_parse_elems(mgmt->u.beacon.variable,
  1920. len - baselen, &elems);
  1921. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  1922. false);
  1923. ifmgd->assoc_data->have_beacon = true;
  1924. ifmgd->assoc_data->sent_assoc = false;
  1925. /* continue assoc process */
  1926. ifmgd->assoc_data->timeout = jiffies;
  1927. run_again(ifmgd, ifmgd->assoc_data->timeout);
  1928. return;
  1929. }
  1930. if (!ifmgd->associated ||
  1931. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1932. return;
  1933. bssid = ifmgd->associated->bssid;
  1934. /* Track average RSSI from the Beacon frames of the current AP */
  1935. ifmgd->last_beacon_signal = rx_status->signal;
  1936. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  1937. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  1938. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  1939. ifmgd->last_cqm_event_signal = 0;
  1940. ifmgd->count_beacon_signal = 1;
  1941. ifmgd->last_ave_beacon_signal = 0;
  1942. } else {
  1943. ifmgd->ave_beacon_signal =
  1944. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  1945. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  1946. ifmgd->ave_beacon_signal) / 16;
  1947. ifmgd->count_beacon_signal++;
  1948. }
  1949. if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
  1950. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  1951. int sig = ifmgd->ave_beacon_signal;
  1952. int last_sig = ifmgd->last_ave_beacon_signal;
  1953. /*
  1954. * if signal crosses either of the boundaries, invoke callback
  1955. * with appropriate parameters
  1956. */
  1957. if (sig > ifmgd->rssi_max_thold &&
  1958. (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
  1959. ifmgd->last_ave_beacon_signal = sig;
  1960. drv_rssi_callback(local, RSSI_EVENT_HIGH);
  1961. } else if (sig < ifmgd->rssi_min_thold &&
  1962. (last_sig >= ifmgd->rssi_max_thold ||
  1963. last_sig == 0)) {
  1964. ifmgd->last_ave_beacon_signal = sig;
  1965. drv_rssi_callback(local, RSSI_EVENT_LOW);
  1966. }
  1967. }
  1968. if (bss_conf->cqm_rssi_thold &&
  1969. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  1970. !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
  1971. int sig = ifmgd->ave_beacon_signal / 16;
  1972. int last_event = ifmgd->last_cqm_event_signal;
  1973. int thold = bss_conf->cqm_rssi_thold;
  1974. int hyst = bss_conf->cqm_rssi_hyst;
  1975. if (sig < thold &&
  1976. (last_event == 0 || sig < last_event - hyst)) {
  1977. ifmgd->last_cqm_event_signal = sig;
  1978. ieee80211_cqm_rssi_notify(
  1979. &sdata->vif,
  1980. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  1981. GFP_KERNEL);
  1982. } else if (sig > thold &&
  1983. (last_event == 0 || sig > last_event + hyst)) {
  1984. ifmgd->last_cqm_event_signal = sig;
  1985. ieee80211_cqm_rssi_notify(
  1986. &sdata->vif,
  1987. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  1988. GFP_KERNEL);
  1989. }
  1990. }
  1991. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  1992. mlme_dbg_ratelimited(sdata,
  1993. "cancelling probereq poll due to a received beacon\n");
  1994. mutex_lock(&local->mtx);
  1995. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  1996. ieee80211_run_deferred_scan(local);
  1997. mutex_unlock(&local->mtx);
  1998. mutex_lock(&local->iflist_mtx);
  1999. ieee80211_recalc_ps(local, -1);
  2000. mutex_unlock(&local->iflist_mtx);
  2001. }
  2002. /*
  2003. * Push the beacon loss detection into the future since
  2004. * we are processing a beacon from the AP just now.
  2005. */
  2006. ieee80211_sta_reset_beacon_monitor(sdata);
  2007. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  2008. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  2009. len - baselen, &elems,
  2010. care_about_ies, ncrc);
  2011. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  2012. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  2013. ifmgd->aid);
  2014. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  2015. if (directed_tim) {
  2016. if (local->hw.conf.dynamic_ps_timeout > 0) {
  2017. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2018. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2019. ieee80211_hw_config(local,
  2020. IEEE80211_CONF_CHANGE_PS);
  2021. }
  2022. ieee80211_send_nullfunc(local, sdata, 0);
  2023. } else if (!local->pspolling && sdata->u.mgd.powersave) {
  2024. local->pspolling = true;
  2025. /*
  2026. * Here is assumed that the driver will be
  2027. * able to send ps-poll frame and receive a
  2028. * response even though power save mode is
  2029. * enabled, but some drivers might require
  2030. * to disable power save here. This needs
  2031. * to be investigated.
  2032. */
  2033. ieee80211_send_pspoll(local, sdata);
  2034. }
  2035. }
  2036. }
  2037. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  2038. return;
  2039. ifmgd->beacon_crc = ncrc;
  2040. ifmgd->beacon_crc_valid = true;
  2041. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  2042. true);
  2043. if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2044. elems.wmm_param_len))
  2045. changed |= BSS_CHANGED_QOS;
  2046. if (elems.erp_info && elems.erp_info_len >= 1) {
  2047. erp_valid = true;
  2048. erp_value = elems.erp_info[0];
  2049. } else {
  2050. erp_valid = false;
  2051. }
  2052. changed |= ieee80211_handle_bss_capability(sdata,
  2053. le16_to_cpu(mgmt->u.beacon.capab_info),
  2054. erp_valid, erp_value);
  2055. if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
  2056. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
  2057. struct ieee80211_supported_band *sband;
  2058. sband = local->hw.wiphy->bands[local->oper_channel->band];
  2059. changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
  2060. bssid, true);
  2061. }
  2062. /* Note: country IE parsing is done for us by cfg80211 */
  2063. if (elems.country_elem) {
  2064. /* TODO: IBSS also needs this */
  2065. if (elems.pwr_constr_elem)
  2066. ieee80211_handle_pwr_constr(sdata,
  2067. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  2068. elems.pwr_constr_elem);
  2069. }
  2070. ieee80211_bss_info_change_notify(sdata, changed);
  2071. }
  2072. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  2073. struct sk_buff *skb)
  2074. {
  2075. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2076. struct ieee80211_rx_status *rx_status;
  2077. struct ieee80211_mgmt *mgmt;
  2078. struct cfg80211_bss *bss = NULL;
  2079. enum rx_mgmt_action rma = RX_MGMT_NONE;
  2080. u16 fc;
  2081. rx_status = (struct ieee80211_rx_status *) skb->cb;
  2082. mgmt = (struct ieee80211_mgmt *) skb->data;
  2083. fc = le16_to_cpu(mgmt->frame_control);
  2084. mutex_lock(&ifmgd->mtx);
  2085. switch (fc & IEEE80211_FCTL_STYPE) {
  2086. case IEEE80211_STYPE_BEACON:
  2087. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  2088. break;
  2089. case IEEE80211_STYPE_PROBE_RESP:
  2090. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  2091. break;
  2092. case IEEE80211_STYPE_AUTH:
  2093. rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  2094. break;
  2095. case IEEE80211_STYPE_DEAUTH:
  2096. rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  2097. break;
  2098. case IEEE80211_STYPE_DISASSOC:
  2099. rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  2100. break;
  2101. case IEEE80211_STYPE_ASSOC_RESP:
  2102. case IEEE80211_STYPE_REASSOC_RESP:
  2103. rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
  2104. break;
  2105. case IEEE80211_STYPE_ACTION:
  2106. switch (mgmt->u.action.category) {
  2107. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2108. ieee80211_sta_process_chanswitch(sdata,
  2109. &mgmt->u.action.u.chan_switch.sw_elem,
  2110. (void *)ifmgd->associated->priv,
  2111. rx_status->mactime);
  2112. break;
  2113. }
  2114. }
  2115. mutex_unlock(&ifmgd->mtx);
  2116. switch (rma) {
  2117. case RX_MGMT_NONE:
  2118. /* no action */
  2119. break;
  2120. case RX_MGMT_CFG80211_DEAUTH:
  2121. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  2122. break;
  2123. case RX_MGMT_CFG80211_DISASSOC:
  2124. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  2125. break;
  2126. case RX_MGMT_CFG80211_RX_AUTH:
  2127. cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
  2128. break;
  2129. case RX_MGMT_CFG80211_RX_ASSOC:
  2130. cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
  2131. break;
  2132. case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
  2133. cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
  2134. break;
  2135. default:
  2136. WARN(1, "unexpected: %d", rma);
  2137. }
  2138. }
  2139. static void ieee80211_sta_timer(unsigned long data)
  2140. {
  2141. struct ieee80211_sub_if_data *sdata =
  2142. (struct ieee80211_sub_if_data *) data;
  2143. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2144. struct ieee80211_local *local = sdata->local;
  2145. if (local->quiescing) {
  2146. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  2147. return;
  2148. }
  2149. ieee80211_queue_work(&local->hw, &sdata->work);
  2150. }
  2151. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  2152. u8 *bssid, u8 reason)
  2153. {
  2154. struct ieee80211_local *local = sdata->local;
  2155. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2156. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2157. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
  2158. false, frame_buf);
  2159. mutex_unlock(&ifmgd->mtx);
  2160. /*
  2161. * must be outside lock due to cfg80211,
  2162. * but that's not a problem.
  2163. */
  2164. cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
  2165. mutex_lock(&local->mtx);
  2166. ieee80211_recalc_idle(local);
  2167. mutex_unlock(&local->mtx);
  2168. mutex_lock(&ifmgd->mtx);
  2169. }
  2170. static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
  2171. {
  2172. struct ieee80211_local *local = sdata->local;
  2173. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2174. struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
  2175. lockdep_assert_held(&ifmgd->mtx);
  2176. if (WARN_ON_ONCE(!auth_data))
  2177. return -EINVAL;
  2178. auth_data->tries++;
  2179. if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
  2180. sdata_info(sdata, "authentication with %pM timed out\n",
  2181. auth_data->bss->bssid);
  2182. /*
  2183. * Most likely AP is not in the range so remove the
  2184. * bss struct for that AP.
  2185. */
  2186. cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
  2187. return -ETIMEDOUT;
  2188. }
  2189. drv_mgd_prepare_tx(local, sdata);
  2190. if (auth_data->bss->proberesp_ies) {
  2191. sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
  2192. auth_data->bss->bssid, auth_data->tries,
  2193. IEEE80211_AUTH_MAX_TRIES);
  2194. auth_data->expected_transaction = 2;
  2195. ieee80211_send_auth(sdata, 1, auth_data->algorithm,
  2196. auth_data->ie, auth_data->ie_len,
  2197. auth_data->bss->bssid,
  2198. auth_data->bss->bssid, NULL, 0, 0);
  2199. } else {
  2200. const u8 *ssidie;
  2201. sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
  2202. auth_data->bss->bssid, auth_data->tries,
  2203. IEEE80211_AUTH_MAX_TRIES);
  2204. ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
  2205. if (!ssidie)
  2206. return -EINVAL;
  2207. /*
  2208. * Direct probe is sent to broadcast address as some APs
  2209. * will not answer to direct packet in unassociated state.
  2210. */
  2211. ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
  2212. NULL, 0, (u32) -1, true, false,
  2213. auth_data->bss->channel);
  2214. }
  2215. auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  2216. run_again(ifmgd, auth_data->timeout);
  2217. return 0;
  2218. }
  2219. static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
  2220. {
  2221. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2222. struct ieee80211_local *local = sdata->local;
  2223. lockdep_assert_held(&sdata->u.mgd.mtx);
  2224. assoc_data->tries++;
  2225. if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
  2226. sdata_info(sdata, "association with %pM timed out\n",
  2227. assoc_data->bss->bssid);
  2228. /*
  2229. * Most likely AP is not in the range so remove the
  2230. * bss struct for that AP.
  2231. */
  2232. cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
  2233. return -ETIMEDOUT;
  2234. }
  2235. sdata_info(sdata, "associate with %pM (try %d/%d)\n",
  2236. assoc_data->bss->bssid, assoc_data->tries,
  2237. IEEE80211_ASSOC_MAX_TRIES);
  2238. ieee80211_send_assoc(sdata);
  2239. assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  2240. run_again(&sdata->u.mgd, assoc_data->timeout);
  2241. return 0;
  2242. }
  2243. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  2244. {
  2245. struct ieee80211_local *local = sdata->local;
  2246. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2247. mutex_lock(&ifmgd->mtx);
  2248. if (ifmgd->auth_data &&
  2249. time_after(jiffies, ifmgd->auth_data->timeout)) {
  2250. if (ifmgd->auth_data->done) {
  2251. /*
  2252. * ok ... we waited for assoc but userspace didn't,
  2253. * so let's just kill the auth data
  2254. */
  2255. ieee80211_destroy_auth_data(sdata, false);
  2256. } else if (ieee80211_probe_auth(sdata)) {
  2257. u8 bssid[ETH_ALEN];
  2258. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  2259. ieee80211_destroy_auth_data(sdata, false);
  2260. mutex_unlock(&ifmgd->mtx);
  2261. cfg80211_send_auth_timeout(sdata->dev, bssid);
  2262. mutex_lock(&ifmgd->mtx);
  2263. }
  2264. } else if (ifmgd->auth_data)
  2265. run_again(ifmgd, ifmgd->auth_data->timeout);
  2266. if (ifmgd->assoc_data &&
  2267. time_after(jiffies, ifmgd->assoc_data->timeout)) {
  2268. if (!ifmgd->assoc_data->have_beacon ||
  2269. ieee80211_do_assoc(sdata)) {
  2270. u8 bssid[ETH_ALEN];
  2271. memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
  2272. ieee80211_destroy_assoc_data(sdata, false);
  2273. mutex_unlock(&ifmgd->mtx);
  2274. cfg80211_send_assoc_timeout(sdata->dev, bssid);
  2275. mutex_lock(&ifmgd->mtx);
  2276. }
  2277. } else if (ifmgd->assoc_data)
  2278. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2279. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  2280. IEEE80211_STA_CONNECTION_POLL) &&
  2281. ifmgd->associated) {
  2282. u8 bssid[ETH_ALEN];
  2283. int max_tries;
  2284. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  2285. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2286. max_tries = max_nullfunc_tries;
  2287. else
  2288. max_tries = max_probe_tries;
  2289. /* ACK received for nullfunc probing frame */
  2290. if (!ifmgd->probe_send_count)
  2291. ieee80211_reset_ap_probe(sdata);
  2292. else if (ifmgd->nullfunc_failed) {
  2293. if (ifmgd->probe_send_count < max_tries) {
  2294. mlme_dbg(sdata,
  2295. "No ack for nullfunc frame to AP %pM, try %d/%i\n",
  2296. bssid, ifmgd->probe_send_count,
  2297. max_tries);
  2298. ieee80211_mgd_probe_ap_send(sdata);
  2299. } else {
  2300. mlme_dbg(sdata,
  2301. "No ack for nullfunc frame to AP %pM, disconnecting.\n",
  2302. bssid);
  2303. ieee80211_sta_connection_lost(sdata, bssid,
  2304. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2305. }
  2306. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  2307. run_again(ifmgd, ifmgd->probe_timeout);
  2308. else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  2309. mlme_dbg(sdata,
  2310. "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
  2311. bssid, probe_wait_ms);
  2312. ieee80211_sta_connection_lost(sdata, bssid,
  2313. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2314. } else if (ifmgd->probe_send_count < max_tries) {
  2315. mlme_dbg(sdata,
  2316. "No probe response from AP %pM after %dms, try %d/%i\n",
  2317. bssid, probe_wait_ms,
  2318. ifmgd->probe_send_count, max_tries);
  2319. ieee80211_mgd_probe_ap_send(sdata);
  2320. } else {
  2321. /*
  2322. * We actually lost the connection ... or did we?
  2323. * Let's make sure!
  2324. */
  2325. wiphy_debug(local->hw.wiphy,
  2326. "%s: No probe response from AP %pM"
  2327. " after %dms, disconnecting.\n",
  2328. sdata->name,
  2329. bssid, probe_wait_ms);
  2330. ieee80211_sta_connection_lost(sdata, bssid,
  2331. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2332. }
  2333. }
  2334. mutex_unlock(&ifmgd->mtx);
  2335. mutex_lock(&local->mtx);
  2336. ieee80211_recalc_idle(local);
  2337. mutex_unlock(&local->mtx);
  2338. }
  2339. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  2340. {
  2341. struct ieee80211_sub_if_data *sdata =
  2342. (struct ieee80211_sub_if_data *) data;
  2343. struct ieee80211_local *local = sdata->local;
  2344. if (local->quiescing)
  2345. return;
  2346. ieee80211_queue_work(&sdata->local->hw,
  2347. &sdata->u.mgd.beacon_connection_loss_work);
  2348. }
  2349. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  2350. {
  2351. struct ieee80211_sub_if_data *sdata =
  2352. (struct ieee80211_sub_if_data *) data;
  2353. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2354. struct ieee80211_local *local = sdata->local;
  2355. if (local->quiescing)
  2356. return;
  2357. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  2358. }
  2359. static void ieee80211_sta_monitor_work(struct work_struct *work)
  2360. {
  2361. struct ieee80211_sub_if_data *sdata =
  2362. container_of(work, struct ieee80211_sub_if_data,
  2363. u.mgd.monitor_work);
  2364. ieee80211_mgd_probe_ap(sdata, false);
  2365. }
  2366. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  2367. {
  2368. u32 flags;
  2369. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  2370. __ieee80211_stop_poll(sdata);
  2371. /* let's probe the connection once */
  2372. flags = sdata->local->hw.flags;
  2373. if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
  2374. ieee80211_queue_work(&sdata->local->hw,
  2375. &sdata->u.mgd.monitor_work);
  2376. /* and do all the other regular work too */
  2377. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  2378. }
  2379. }
  2380. #ifdef CONFIG_PM
  2381. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  2382. {
  2383. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2384. /*
  2385. * we need to use atomic bitops for the running bits
  2386. * only because both timers might fire at the same
  2387. * time -- the code here is properly synchronised.
  2388. */
  2389. cancel_work_sync(&ifmgd->request_smps_work);
  2390. cancel_work_sync(&ifmgd->monitor_work);
  2391. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  2392. cancel_work_sync(&ifmgd->csa_connection_drop_work);
  2393. if (del_timer_sync(&ifmgd->timer))
  2394. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  2395. cancel_work_sync(&ifmgd->chswitch_work);
  2396. if (del_timer_sync(&ifmgd->chswitch_timer))
  2397. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  2398. /* these will just be re-established on connection */
  2399. del_timer_sync(&ifmgd->conn_mon_timer);
  2400. del_timer_sync(&ifmgd->bcn_mon_timer);
  2401. }
  2402. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  2403. {
  2404. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2405. if (!ifmgd->associated)
  2406. return;
  2407. if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
  2408. sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
  2409. mutex_lock(&ifmgd->mtx);
  2410. if (ifmgd->associated) {
  2411. mlme_dbg(sdata,
  2412. "driver requested disconnect after resume\n");
  2413. ieee80211_sta_connection_lost(sdata,
  2414. ifmgd->associated->bssid,
  2415. WLAN_REASON_UNSPECIFIED);
  2416. mutex_unlock(&ifmgd->mtx);
  2417. return;
  2418. }
  2419. mutex_unlock(&ifmgd->mtx);
  2420. }
  2421. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  2422. add_timer(&ifmgd->timer);
  2423. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  2424. add_timer(&ifmgd->chswitch_timer);
  2425. ieee80211_sta_reset_beacon_monitor(sdata);
  2426. mutex_lock(&sdata->local->mtx);
  2427. ieee80211_restart_sta_timer(sdata);
  2428. mutex_unlock(&sdata->local->mtx);
  2429. }
  2430. #endif
  2431. /* interface setup */
  2432. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  2433. {
  2434. struct ieee80211_if_managed *ifmgd;
  2435. ifmgd = &sdata->u.mgd;
  2436. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  2437. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  2438. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  2439. ieee80211_beacon_connection_loss_work);
  2440. INIT_WORK(&ifmgd->csa_connection_drop_work,
  2441. ieee80211_csa_connection_drop_work);
  2442. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
  2443. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  2444. (unsigned long) sdata);
  2445. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  2446. (unsigned long) sdata);
  2447. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  2448. (unsigned long) sdata);
  2449. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  2450. (unsigned long) sdata);
  2451. ifmgd->flags = 0;
  2452. ifmgd->powersave = sdata->wdev.ps;
  2453. ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
  2454. ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
  2455. mutex_init(&ifmgd->mtx);
  2456. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  2457. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  2458. else
  2459. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  2460. }
  2461. /* scan finished notification */
  2462. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  2463. {
  2464. struct ieee80211_sub_if_data *sdata;
  2465. /* Restart STA timers */
  2466. rcu_read_lock();
  2467. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  2468. ieee80211_restart_sta_timer(sdata);
  2469. rcu_read_unlock();
  2470. }
  2471. int ieee80211_max_network_latency(struct notifier_block *nb,
  2472. unsigned long data, void *dummy)
  2473. {
  2474. s32 latency_usec = (s32) data;
  2475. struct ieee80211_local *local =
  2476. container_of(nb, struct ieee80211_local,
  2477. network_latency_notifier);
  2478. mutex_lock(&local->iflist_mtx);
  2479. ieee80211_recalc_ps(local, latency_usec);
  2480. mutex_unlock(&local->iflist_mtx);
  2481. return 0;
  2482. }
  2483. static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
  2484. struct cfg80211_bss *cbss)
  2485. {
  2486. struct ieee80211_local *local = sdata->local;
  2487. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2488. int ht_cfreq;
  2489. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  2490. const u8 *ht_oper_ie;
  2491. const struct ieee80211_ht_operation *ht_oper = NULL;
  2492. struct ieee80211_supported_band *sband;
  2493. sband = local->hw.wiphy->bands[cbss->channel->band];
  2494. ifmgd->flags &= ~IEEE80211_STA_DISABLE_40MHZ;
  2495. if (sband->ht_cap.ht_supported) {
  2496. ht_oper_ie = cfg80211_find_ie(WLAN_EID_HT_OPERATION,
  2497. cbss->information_elements,
  2498. cbss->len_information_elements);
  2499. if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
  2500. ht_oper = (void *)(ht_oper_ie + 2);
  2501. }
  2502. if (ht_oper) {
  2503. ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
  2504. cbss->channel->band);
  2505. /* check that channel matches the right operating channel */
  2506. if (cbss->channel->center_freq != ht_cfreq) {
  2507. /*
  2508. * It's possible that some APs are confused here;
  2509. * Netgear WNDR3700 sometimes reports 4 higher than
  2510. * the actual channel in association responses, but
  2511. * since we look at probe response/beacon data here
  2512. * it should be OK.
  2513. */
  2514. sdata_info(sdata,
  2515. "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
  2516. cbss->channel->center_freq,
  2517. ht_cfreq, ht_oper->primary_chan,
  2518. cbss->channel->band);
  2519. ht_oper = NULL;
  2520. }
  2521. }
  2522. if (ht_oper) {
  2523. channel_type = NL80211_CHAN_HT20;
  2524. if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  2525. switch (ht_oper->ht_param &
  2526. IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  2527. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  2528. channel_type = NL80211_CHAN_HT40PLUS;
  2529. break;
  2530. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  2531. channel_type = NL80211_CHAN_HT40MINUS;
  2532. break;
  2533. }
  2534. }
  2535. }
  2536. if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
  2537. /* can only fail due to HT40+/- mismatch */
  2538. channel_type = NL80211_CHAN_HT20;
  2539. sdata_info(sdata,
  2540. "disabling 40 MHz due to multi-vif mismatch\n");
  2541. ifmgd->flags |= IEEE80211_STA_DISABLE_40MHZ;
  2542. WARN_ON(!ieee80211_set_channel_type(local, sdata,
  2543. channel_type));
  2544. }
  2545. local->oper_channel = cbss->channel;
  2546. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  2547. return 0;
  2548. }
  2549. static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
  2550. struct cfg80211_bss *cbss, bool assoc)
  2551. {
  2552. struct ieee80211_local *local = sdata->local;
  2553. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2554. struct ieee80211_bss *bss = (void *)cbss->priv;
  2555. struct sta_info *new_sta = NULL;
  2556. bool have_sta = false;
  2557. int err;
  2558. if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
  2559. return -EINVAL;
  2560. if (assoc) {
  2561. rcu_read_lock();
  2562. have_sta = sta_info_get(sdata, cbss->bssid);
  2563. rcu_read_unlock();
  2564. }
  2565. if (!have_sta) {
  2566. new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  2567. if (!new_sta)
  2568. return -ENOMEM;
  2569. }
  2570. mutex_lock(&local->mtx);
  2571. ieee80211_recalc_idle(sdata->local);
  2572. mutex_unlock(&local->mtx);
  2573. if (new_sta) {
  2574. u32 rates = 0, basic_rates = 0;
  2575. bool have_higher_than_11mbit;
  2576. int min_rate = INT_MAX, min_rate_index = -1;
  2577. struct ieee80211_supported_band *sband;
  2578. sband = local->hw.wiphy->bands[cbss->channel->band];
  2579. err = ieee80211_prep_channel(sdata, cbss);
  2580. if (err) {
  2581. sta_info_free(local, new_sta);
  2582. return err;
  2583. }
  2584. ieee80211_get_rates(sband, bss->supp_rates,
  2585. bss->supp_rates_len,
  2586. &rates, &basic_rates,
  2587. &have_higher_than_11mbit,
  2588. &min_rate, &min_rate_index);
  2589. /*
  2590. * This used to be a workaround for basic rates missing
  2591. * in the association response frame. Now that we no
  2592. * longer use the basic rates from there, it probably
  2593. * doesn't happen any more, but keep the workaround so
  2594. * in case some *other* APs are buggy in different ways
  2595. * we can connect -- with a warning.
  2596. */
  2597. if (!basic_rates && min_rate_index >= 0) {
  2598. sdata_info(sdata,
  2599. "No basic rates, using min rate instead\n");
  2600. basic_rates = BIT(min_rate_index);
  2601. }
  2602. new_sta->sta.supp_rates[cbss->channel->band] = rates;
  2603. sdata->vif.bss_conf.basic_rates = basic_rates;
  2604. /* cf. IEEE 802.11 9.2.12 */
  2605. if (local->oper_channel->band == IEEE80211_BAND_2GHZ &&
  2606. have_higher_than_11mbit)
  2607. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  2608. else
  2609. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  2610. memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
  2611. /* set timing information */
  2612. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  2613. sdata->vif.bss_conf.sync_tsf = cbss->tsf;
  2614. sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
  2615. /* tell driver about BSSID, basic rates and timing */
  2616. ieee80211_bss_info_change_notify(sdata,
  2617. BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
  2618. BSS_CHANGED_BEACON_INT);
  2619. if (assoc)
  2620. sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
  2621. err = sta_info_insert(new_sta);
  2622. new_sta = NULL;
  2623. if (err) {
  2624. sdata_info(sdata,
  2625. "failed to insert STA entry for the AP (error %d)\n",
  2626. err);
  2627. return err;
  2628. }
  2629. } else
  2630. WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
  2631. return 0;
  2632. }
  2633. /* config hooks */
  2634. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  2635. struct cfg80211_auth_request *req)
  2636. {
  2637. struct ieee80211_local *local = sdata->local;
  2638. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2639. struct ieee80211_mgd_auth_data *auth_data;
  2640. u16 auth_alg;
  2641. int err;
  2642. /* prepare auth data structure */
  2643. switch (req->auth_type) {
  2644. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  2645. auth_alg = WLAN_AUTH_OPEN;
  2646. break;
  2647. case NL80211_AUTHTYPE_SHARED_KEY:
  2648. if (IS_ERR(local->wep_tx_tfm))
  2649. return -EOPNOTSUPP;
  2650. auth_alg = WLAN_AUTH_SHARED_KEY;
  2651. break;
  2652. case NL80211_AUTHTYPE_FT:
  2653. auth_alg = WLAN_AUTH_FT;
  2654. break;
  2655. case NL80211_AUTHTYPE_NETWORK_EAP:
  2656. auth_alg = WLAN_AUTH_LEAP;
  2657. break;
  2658. default:
  2659. return -EOPNOTSUPP;
  2660. }
  2661. auth_data = kzalloc(sizeof(*auth_data) + req->ie_len, GFP_KERNEL);
  2662. if (!auth_data)
  2663. return -ENOMEM;
  2664. auth_data->bss = req->bss;
  2665. if (req->ie && req->ie_len) {
  2666. memcpy(auth_data->ie, req->ie, req->ie_len);
  2667. auth_data->ie_len = req->ie_len;
  2668. }
  2669. if (req->key && req->key_len) {
  2670. auth_data->key_len = req->key_len;
  2671. auth_data->key_idx = req->key_idx;
  2672. memcpy(auth_data->key, req->key, req->key_len);
  2673. }
  2674. auth_data->algorithm = auth_alg;
  2675. /* try to authenticate/probe */
  2676. mutex_lock(&ifmgd->mtx);
  2677. if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
  2678. ifmgd->assoc_data) {
  2679. err = -EBUSY;
  2680. goto err_free;
  2681. }
  2682. if (ifmgd->auth_data)
  2683. ieee80211_destroy_auth_data(sdata, false);
  2684. /* prep auth_data so we don't go into idle on disassoc */
  2685. ifmgd->auth_data = auth_data;
  2686. if (ifmgd->associated)
  2687. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2688. sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
  2689. err = ieee80211_prep_connection(sdata, req->bss, false);
  2690. if (err)
  2691. goto err_clear;
  2692. err = ieee80211_probe_auth(sdata);
  2693. if (err) {
  2694. sta_info_destroy_addr(sdata, req->bss->bssid);
  2695. goto err_clear;
  2696. }
  2697. /* hold our own reference */
  2698. cfg80211_ref_bss(auth_data->bss);
  2699. err = 0;
  2700. goto out_unlock;
  2701. err_clear:
  2702. memset(ifmgd->bssid, 0, ETH_ALEN);
  2703. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2704. ifmgd->auth_data = NULL;
  2705. err_free:
  2706. kfree(auth_data);
  2707. out_unlock:
  2708. mutex_unlock(&ifmgd->mtx);
  2709. return err;
  2710. }
  2711. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  2712. struct cfg80211_assoc_request *req)
  2713. {
  2714. struct ieee80211_local *local = sdata->local;
  2715. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2716. struct ieee80211_bss *bss = (void *)req->bss->priv;
  2717. struct ieee80211_mgd_assoc_data *assoc_data;
  2718. struct ieee80211_supported_band *sband;
  2719. const u8 *ssidie, *ht_ie;
  2720. int i, err;
  2721. ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  2722. if (!ssidie)
  2723. return -EINVAL;
  2724. assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
  2725. if (!assoc_data)
  2726. return -ENOMEM;
  2727. mutex_lock(&ifmgd->mtx);
  2728. if (ifmgd->associated)
  2729. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2730. if (ifmgd->auth_data && !ifmgd->auth_data->done) {
  2731. err = -EBUSY;
  2732. goto err_free;
  2733. }
  2734. if (ifmgd->assoc_data) {
  2735. err = -EBUSY;
  2736. goto err_free;
  2737. }
  2738. if (ifmgd->auth_data) {
  2739. bool match;
  2740. /* keep sta info, bssid if matching */
  2741. match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
  2742. ieee80211_destroy_auth_data(sdata, match);
  2743. }
  2744. /* prepare assoc data */
  2745. /*
  2746. * keep only the 40 MHz disable bit set as it might have
  2747. * been set during authentication already, all other bits
  2748. * should be reset for a new connection
  2749. */
  2750. ifmgd->flags &= IEEE80211_STA_DISABLE_40MHZ;
  2751. ifmgd->beacon_crc_valid = false;
  2752. /*
  2753. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  2754. * We still associate in non-HT mode (11a/b/g) if any one of these
  2755. * ciphers is configured as pairwise.
  2756. * We can set this to true for non-11n hardware, that'll be checked
  2757. * separately along with the peer capabilities.
  2758. */
  2759. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
  2760. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  2761. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  2762. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
  2763. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2764. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  2765. netdev_info(sdata->dev,
  2766. "disabling HT/VHT due to WEP/TKIP use\n");
  2767. }
  2768. }
  2769. if (req->flags & ASSOC_REQ_DISABLE_HT) {
  2770. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2771. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  2772. }
  2773. /* Also disable HT if we don't support it or the AP doesn't use WMM */
  2774. sband = local->hw.wiphy->bands[req->bss->channel->band];
  2775. if (!sband->ht_cap.ht_supported ||
  2776. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
  2777. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2778. if (!bss->wmm_used)
  2779. netdev_info(sdata->dev,
  2780. "disabling HT as WMM/QoS is not supported by the AP\n");
  2781. }
  2782. /* disable VHT if we don't support it or the AP doesn't use WMM */
  2783. if (!sband->vht_cap.vht_supported ||
  2784. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
  2785. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  2786. if (!bss->wmm_used)
  2787. netdev_info(sdata->dev,
  2788. "disabling VHT as WMM/QoS is not supported by the AP\n");
  2789. }
  2790. memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
  2791. memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
  2792. sizeof(ifmgd->ht_capa_mask));
  2793. if (req->ie && req->ie_len) {
  2794. memcpy(assoc_data->ie, req->ie, req->ie_len);
  2795. assoc_data->ie_len = req->ie_len;
  2796. }
  2797. assoc_data->bss = req->bss;
  2798. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  2799. if (ifmgd->powersave)
  2800. ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
  2801. else
  2802. ifmgd->ap_smps = IEEE80211_SMPS_OFF;
  2803. } else
  2804. ifmgd->ap_smps = ifmgd->req_smps;
  2805. assoc_data->capability = req->bss->capability;
  2806. assoc_data->wmm = bss->wmm_used &&
  2807. (local->hw.queues >= IEEE80211_NUM_ACS);
  2808. assoc_data->supp_rates = bss->supp_rates;
  2809. assoc_data->supp_rates_len = bss->supp_rates_len;
  2810. ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
  2811. if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
  2812. assoc_data->ap_ht_param =
  2813. ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
  2814. else
  2815. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2816. if (bss->wmm_used && bss->uapsd_supported &&
  2817. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
  2818. assoc_data->uapsd = true;
  2819. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  2820. } else {
  2821. assoc_data->uapsd = false;
  2822. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  2823. }
  2824. memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
  2825. assoc_data->ssid_len = ssidie[1];
  2826. if (req->prev_bssid)
  2827. memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
  2828. if (req->use_mfp) {
  2829. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  2830. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  2831. } else {
  2832. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  2833. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  2834. }
  2835. if (req->crypto.control_port)
  2836. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  2837. else
  2838. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  2839. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  2840. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  2841. /* kick off associate process */
  2842. ifmgd->assoc_data = assoc_data;
  2843. err = ieee80211_prep_connection(sdata, req->bss, true);
  2844. if (err)
  2845. goto err_clear;
  2846. if (!bss->dtim_period &&
  2847. sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
  2848. /*
  2849. * Wait up to one beacon interval ...
  2850. * should this be more if we miss one?
  2851. */
  2852. sdata_info(sdata, "waiting for beacon from %pM\n",
  2853. ifmgd->bssid);
  2854. assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
  2855. } else {
  2856. assoc_data->have_beacon = true;
  2857. assoc_data->sent_assoc = false;
  2858. assoc_data->timeout = jiffies;
  2859. }
  2860. run_again(ifmgd, assoc_data->timeout);
  2861. if (bss->corrupt_data) {
  2862. char *corrupt_type = "data";
  2863. if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
  2864. if (bss->corrupt_data &
  2865. IEEE80211_BSS_CORRUPT_PROBE_RESP)
  2866. corrupt_type = "beacon and probe response";
  2867. else
  2868. corrupt_type = "beacon";
  2869. } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
  2870. corrupt_type = "probe response";
  2871. sdata_info(sdata, "associating with AP with corrupt %s\n",
  2872. corrupt_type);
  2873. }
  2874. err = 0;
  2875. goto out;
  2876. err_clear:
  2877. memset(ifmgd->bssid, 0, ETH_ALEN);
  2878. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2879. ifmgd->assoc_data = NULL;
  2880. err_free:
  2881. kfree(assoc_data);
  2882. out:
  2883. mutex_unlock(&ifmgd->mtx);
  2884. return err;
  2885. }
  2886. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  2887. struct cfg80211_deauth_request *req)
  2888. {
  2889. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2890. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2891. mutex_lock(&ifmgd->mtx);
  2892. if (ifmgd->auth_data) {
  2893. ieee80211_destroy_auth_data(sdata, false);
  2894. mutex_unlock(&ifmgd->mtx);
  2895. return 0;
  2896. }
  2897. sdata_info(sdata,
  2898. "deauthenticating from %pM by local choice (reason=%d)\n",
  2899. req->bssid, req->reason_code);
  2900. if (ifmgd->associated &&
  2901. ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
  2902. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  2903. req->reason_code, true, frame_buf);
  2904. } else {
  2905. drv_mgd_prepare_tx(sdata->local, sdata);
  2906. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  2907. IEEE80211_STYPE_DEAUTH,
  2908. req->reason_code, true,
  2909. frame_buf);
  2910. }
  2911. mutex_unlock(&ifmgd->mtx);
  2912. __cfg80211_send_deauth(sdata->dev, frame_buf,
  2913. IEEE80211_DEAUTH_FRAME_LEN);
  2914. mutex_lock(&sdata->local->mtx);
  2915. ieee80211_recalc_idle(sdata->local);
  2916. mutex_unlock(&sdata->local->mtx);
  2917. return 0;
  2918. }
  2919. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  2920. struct cfg80211_disassoc_request *req)
  2921. {
  2922. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2923. u8 bssid[ETH_ALEN];
  2924. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2925. mutex_lock(&ifmgd->mtx);
  2926. /*
  2927. * cfg80211 should catch this ... but it's racy since
  2928. * we can receive a disassoc frame, process it, hand it
  2929. * to cfg80211 while that's in a locked section already
  2930. * trying to tell us that the user wants to disconnect.
  2931. */
  2932. if (ifmgd->associated != req->bss) {
  2933. mutex_unlock(&ifmgd->mtx);
  2934. return -ENOLINK;
  2935. }
  2936. sdata_info(sdata,
  2937. "disassociating from %pM by local choice (reason=%d)\n",
  2938. req->bss->bssid, req->reason_code);
  2939. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  2940. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
  2941. req->reason_code, !req->local_state_change,
  2942. frame_buf);
  2943. mutex_unlock(&ifmgd->mtx);
  2944. __cfg80211_send_disassoc(sdata->dev, frame_buf,
  2945. IEEE80211_DEAUTH_FRAME_LEN);
  2946. mutex_lock(&sdata->local->mtx);
  2947. ieee80211_recalc_idle(sdata->local);
  2948. mutex_unlock(&sdata->local->mtx);
  2949. return 0;
  2950. }
  2951. void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
  2952. {
  2953. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2954. mutex_lock(&ifmgd->mtx);
  2955. if (ifmgd->assoc_data)
  2956. ieee80211_destroy_assoc_data(sdata, false);
  2957. if (ifmgd->auth_data)
  2958. ieee80211_destroy_auth_data(sdata, false);
  2959. del_timer_sync(&ifmgd->timer);
  2960. mutex_unlock(&ifmgd->mtx);
  2961. }
  2962. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  2963. enum nl80211_cqm_rssi_threshold_event rssi_event,
  2964. gfp_t gfp)
  2965. {
  2966. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2967. trace_api_cqm_rssi_notify(sdata, rssi_event);
  2968. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  2969. }
  2970. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);