mlme.c 116 KB

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