mlme.c 113 KB

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