mlme.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194
  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. run_again(ifmgd, ifmgd->auth_data->timeout);
  1664. if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
  1665. ifmgd->auth_data->expected_transaction != 2) {
  1666. /*
  1667. * Report auth frame to user space for processing since another
  1668. * round of Authentication frames is still needed.
  1669. */
  1670. return RX_MGMT_CFG80211_RX_AUTH;
  1671. }
  1672. /* move station state to auth */
  1673. mutex_lock(&sdata->local->sta_mtx);
  1674. sta = sta_info_get(sdata, bssid);
  1675. if (!sta) {
  1676. WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
  1677. goto out_err;
  1678. }
  1679. if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
  1680. sdata_info(sdata, "failed moving %pM to auth\n", bssid);
  1681. goto out_err;
  1682. }
  1683. mutex_unlock(&sdata->local->sta_mtx);
  1684. return RX_MGMT_CFG80211_RX_AUTH;
  1685. out_err:
  1686. mutex_unlock(&sdata->local->sta_mtx);
  1687. /* ignore frame -- wait for timeout */
  1688. return RX_MGMT_NONE;
  1689. }
  1690. static enum rx_mgmt_action __must_check
  1691. ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1692. struct ieee80211_mgmt *mgmt, size_t len)
  1693. {
  1694. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1695. const u8 *bssid = NULL;
  1696. u16 reason_code;
  1697. lockdep_assert_held(&ifmgd->mtx);
  1698. if (len < 24 + 2)
  1699. return RX_MGMT_NONE;
  1700. if (!ifmgd->associated ||
  1701. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1702. return RX_MGMT_NONE;
  1703. bssid = ifmgd->associated->bssid;
  1704. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1705. sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
  1706. bssid, reason_code);
  1707. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1708. return RX_MGMT_CFG80211_DEAUTH;
  1709. }
  1710. static enum rx_mgmt_action __must_check
  1711. ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1712. struct ieee80211_mgmt *mgmt, size_t len)
  1713. {
  1714. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1715. u16 reason_code;
  1716. lockdep_assert_held(&ifmgd->mtx);
  1717. if (len < 24 + 2)
  1718. return RX_MGMT_NONE;
  1719. if (!ifmgd->associated ||
  1720. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1721. return RX_MGMT_NONE;
  1722. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1723. sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
  1724. mgmt->sa, reason_code);
  1725. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1726. return RX_MGMT_CFG80211_DISASSOC;
  1727. }
  1728. static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  1729. u8 *supp_rates, unsigned int supp_rates_len,
  1730. u32 *rates, u32 *basic_rates,
  1731. bool *have_higher_than_11mbit,
  1732. int *min_rate, int *min_rate_index)
  1733. {
  1734. int i, j;
  1735. for (i = 0; i < supp_rates_len; i++) {
  1736. int rate = (supp_rates[i] & 0x7f) * 5;
  1737. bool is_basic = !!(supp_rates[i] & 0x80);
  1738. if (rate > 110)
  1739. *have_higher_than_11mbit = true;
  1740. /*
  1741. * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
  1742. * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
  1743. *
  1744. * Note: Even through the membership selector and the basic
  1745. * rate flag share the same bit, they are not exactly
  1746. * the same.
  1747. */
  1748. if (!!(supp_rates[i] & 0x80) &&
  1749. (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1750. continue;
  1751. for (j = 0; j < sband->n_bitrates; j++) {
  1752. if (sband->bitrates[j].bitrate == rate) {
  1753. *rates |= BIT(j);
  1754. if (is_basic)
  1755. *basic_rates |= BIT(j);
  1756. if (rate < *min_rate) {
  1757. *min_rate = rate;
  1758. *min_rate_index = j;
  1759. }
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. }
  1765. static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  1766. bool assoc)
  1767. {
  1768. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  1769. lockdep_assert_held(&sdata->u.mgd.mtx);
  1770. if (!assoc) {
  1771. sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
  1772. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1773. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1774. sdata->u.mgd.flags = 0;
  1775. ieee80211_vif_release_channel(sdata);
  1776. }
  1777. kfree(assoc_data);
  1778. sdata->u.mgd.assoc_data = NULL;
  1779. }
  1780. static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
  1781. struct cfg80211_bss *cbss,
  1782. struct ieee80211_mgmt *mgmt, size_t len)
  1783. {
  1784. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1785. struct ieee80211_local *local = sdata->local;
  1786. struct ieee80211_supported_band *sband;
  1787. struct sta_info *sta;
  1788. u8 *pos;
  1789. u16 capab_info, aid;
  1790. struct ieee802_11_elems elems;
  1791. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1792. u32 changed = 0;
  1793. int err;
  1794. /* AssocResp and ReassocResp have identical structure */
  1795. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1796. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1797. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1798. sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
  1799. aid);
  1800. aid &= ~(BIT(15) | BIT(14));
  1801. ifmgd->broken_ap = false;
  1802. if (aid == 0 || aid > IEEE80211_MAX_AID) {
  1803. sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
  1804. aid);
  1805. aid = 0;
  1806. ifmgd->broken_ap = true;
  1807. }
  1808. pos = mgmt->u.assoc_resp.variable;
  1809. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1810. if (!elems.supp_rates) {
  1811. sdata_info(sdata, "no SuppRates element in AssocResp\n");
  1812. return false;
  1813. }
  1814. ifmgd->aid = aid;
  1815. mutex_lock(&sdata->local->sta_mtx);
  1816. /*
  1817. * station info was already allocated and inserted before
  1818. * the association and should be available to us
  1819. */
  1820. sta = sta_info_get(sdata, cbss->bssid);
  1821. if (WARN_ON(!sta)) {
  1822. mutex_unlock(&sdata->local->sta_mtx);
  1823. return false;
  1824. }
  1825. sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
  1826. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  1827. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  1828. elems.ht_cap_elem, &sta->sta.ht_cap);
  1829. sta->supports_40mhz =
  1830. sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  1831. if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  1832. ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
  1833. elems.vht_cap_elem,
  1834. &sta->sta.vht_cap);
  1835. rate_control_rate_init(sta);
  1836. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  1837. set_sta_flag(sta, WLAN_STA_MFP);
  1838. if (elems.wmm_param)
  1839. set_sta_flag(sta, WLAN_STA_WME);
  1840. err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  1841. if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  1842. err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  1843. if (err) {
  1844. sdata_info(sdata,
  1845. "failed to move station %pM to desired state\n",
  1846. sta->sta.addr);
  1847. WARN_ON(__sta_info_destroy(sta));
  1848. mutex_unlock(&sdata->local->sta_mtx);
  1849. return false;
  1850. }
  1851. mutex_unlock(&sdata->local->sta_mtx);
  1852. /*
  1853. * Always handle WMM once after association regardless
  1854. * of the first value the AP uses. Setting -1 here has
  1855. * that effect because the AP values is an unsigned
  1856. * 4-bit value.
  1857. */
  1858. ifmgd->wmm_last_param_set = -1;
  1859. if (elems.wmm_param)
  1860. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  1861. elems.wmm_param_len);
  1862. else
  1863. ieee80211_set_wmm_default(sdata, false);
  1864. changed |= BSS_CHANGED_QOS;
  1865. if (elems.ht_operation && elems.wmm_param &&
  1866. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  1867. changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
  1868. cbss->bssid, false);
  1869. /* set AID and assoc capability,
  1870. * ieee80211_set_associated() will tell the driver */
  1871. bss_conf->aid = aid;
  1872. bss_conf->assoc_capability = capab_info;
  1873. ieee80211_set_associated(sdata, cbss, changed);
  1874. /*
  1875. * If we're using 4-addr mode, let the AP know that we're
  1876. * doing so, so that it can create the STA VLAN on its side
  1877. */
  1878. if (ifmgd->use_4addr)
  1879. ieee80211_send_4addr_nullfunc(local, sdata);
  1880. /*
  1881. * Start timer to probe the connection to the AP now.
  1882. * Also start the timer that will detect beacon loss.
  1883. */
  1884. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  1885. ieee80211_sta_reset_beacon_monitor(sdata);
  1886. return true;
  1887. }
  1888. static enum rx_mgmt_action __must_check
  1889. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  1890. struct ieee80211_mgmt *mgmt, size_t len,
  1891. struct cfg80211_bss **bss)
  1892. {
  1893. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1894. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  1895. u16 capab_info, status_code, aid;
  1896. struct ieee802_11_elems elems;
  1897. u8 *pos;
  1898. bool reassoc;
  1899. lockdep_assert_held(&ifmgd->mtx);
  1900. if (!assoc_data)
  1901. return RX_MGMT_NONE;
  1902. if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
  1903. return RX_MGMT_NONE;
  1904. /*
  1905. * AssocResp and ReassocResp have identical structure, so process both
  1906. * of them in this function.
  1907. */
  1908. if (len < 24 + 6)
  1909. return RX_MGMT_NONE;
  1910. reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
  1911. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1912. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1913. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1914. sdata_info(sdata,
  1915. "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
  1916. reassoc ? "Rea" : "A", mgmt->sa,
  1917. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1918. pos = mgmt->u.assoc_resp.variable;
  1919. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1920. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  1921. elems.timeout_int && elems.timeout_int_len == 5 &&
  1922. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  1923. u32 tu, ms;
  1924. tu = get_unaligned_le32(elems.timeout_int + 1);
  1925. ms = tu * 1024 / 1000;
  1926. sdata_info(sdata,
  1927. "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
  1928. mgmt->sa, tu, ms);
  1929. assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
  1930. if (ms > IEEE80211_ASSOC_TIMEOUT)
  1931. run_again(ifmgd, assoc_data->timeout);
  1932. return RX_MGMT_NONE;
  1933. }
  1934. *bss = assoc_data->bss;
  1935. if (status_code != WLAN_STATUS_SUCCESS) {
  1936. sdata_info(sdata, "%pM denied association (code=%d)\n",
  1937. mgmt->sa, status_code);
  1938. ieee80211_destroy_assoc_data(sdata, false);
  1939. } else {
  1940. if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
  1941. /* oops -- internal error -- send timeout for now */
  1942. ieee80211_destroy_assoc_data(sdata, false);
  1943. cfg80211_put_bss(sdata->local->hw.wiphy, *bss);
  1944. return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
  1945. }
  1946. sdata_info(sdata, "associated\n");
  1947. /*
  1948. * destroy assoc_data afterwards, as otherwise an idle
  1949. * recalc after assoc_data is NULL but before associated
  1950. * is set can cause the interface to go idle
  1951. */
  1952. ieee80211_destroy_assoc_data(sdata, true);
  1953. }
  1954. return RX_MGMT_CFG80211_RX_ASSOC;
  1955. }
  1956. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1957. struct ieee80211_mgmt *mgmt, size_t len,
  1958. struct ieee80211_rx_status *rx_status,
  1959. struct ieee802_11_elems *elems)
  1960. {
  1961. struct ieee80211_local *local = sdata->local;
  1962. int freq;
  1963. struct ieee80211_bss *bss;
  1964. struct ieee80211_channel *channel;
  1965. bool need_ps = false;
  1966. if ((sdata->u.mgd.associated &&
  1967. ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) ||
  1968. (sdata->u.mgd.assoc_data &&
  1969. ether_addr_equal(mgmt->bssid,
  1970. sdata->u.mgd.assoc_data->bss->bssid))) {
  1971. /* not previously set so we may need to recalc */
  1972. need_ps = sdata->u.mgd.associated && !sdata->u.mgd.dtim_period;
  1973. if (elems->tim && !elems->parse_error) {
  1974. struct ieee80211_tim_ie *tim_ie = elems->tim;
  1975. sdata->u.mgd.dtim_period = tim_ie->dtim_period;
  1976. }
  1977. }
  1978. if (elems->ds_params && elems->ds_params_len == 1)
  1979. freq = ieee80211_channel_to_frequency(elems->ds_params[0],
  1980. rx_status->band);
  1981. else
  1982. freq = rx_status->freq;
  1983. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1984. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1985. return;
  1986. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1987. channel);
  1988. if (bss)
  1989. ieee80211_rx_bss_put(local, bss);
  1990. if (!sdata->u.mgd.associated)
  1991. return;
  1992. if (need_ps) {
  1993. mutex_lock(&local->iflist_mtx);
  1994. ieee80211_recalc_ps(local, -1);
  1995. mutex_unlock(&local->iflist_mtx);
  1996. }
  1997. if (elems->ch_switch_ie &&
  1998. memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
  1999. ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
  2000. bss, rx_status->mactime);
  2001. }
  2002. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  2003. struct sk_buff *skb)
  2004. {
  2005. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  2006. struct ieee80211_if_managed *ifmgd;
  2007. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  2008. size_t baselen, len = skb->len;
  2009. struct ieee802_11_elems elems;
  2010. ifmgd = &sdata->u.mgd;
  2011. ASSERT_MGD_MTX(ifmgd);
  2012. if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
  2013. return; /* ignore ProbeResp to foreign address */
  2014. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  2015. if (baselen > len)
  2016. return;
  2017. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  2018. &elems);
  2019. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2020. if (ifmgd->associated &&
  2021. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2022. ieee80211_reset_ap_probe(sdata);
  2023. if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
  2024. ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
  2025. /* got probe response, continue with auth */
  2026. sdata_info(sdata, "direct probe responded\n");
  2027. ifmgd->auth_data->tries = 0;
  2028. ifmgd->auth_data->timeout = jiffies;
  2029. run_again(ifmgd, ifmgd->auth_data->timeout);
  2030. }
  2031. }
  2032. /*
  2033. * This is the canonical list of information elements we care about,
  2034. * the filter code also gives us all changes to the Microsoft OUI
  2035. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  2036. *
  2037. * We implement beacon filtering in software since that means we can
  2038. * avoid processing the frame here and in cfg80211, and userspace
  2039. * will not be able to tell whether the hardware supports it or not.
  2040. *
  2041. * XXX: This list needs to be dynamic -- userspace needs to be able to
  2042. * add items it requires. It also needs to be able to tell us to
  2043. * look out for other vendor IEs.
  2044. */
  2045. static const u64 care_about_ies =
  2046. (1ULL << WLAN_EID_COUNTRY) |
  2047. (1ULL << WLAN_EID_ERP_INFO) |
  2048. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  2049. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  2050. (1ULL << WLAN_EID_HT_CAPABILITY) |
  2051. (1ULL << WLAN_EID_HT_OPERATION);
  2052. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  2053. struct ieee80211_mgmt *mgmt,
  2054. size_t len,
  2055. struct ieee80211_rx_status *rx_status)
  2056. {
  2057. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2058. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2059. size_t baselen;
  2060. struct ieee802_11_elems elems;
  2061. struct ieee80211_local *local = sdata->local;
  2062. struct ieee80211_chanctx_conf *chanctx_conf;
  2063. struct ieee80211_channel *chan;
  2064. u32 changed = 0;
  2065. bool erp_valid;
  2066. u8 erp_value = 0;
  2067. u32 ncrc;
  2068. u8 *bssid;
  2069. lockdep_assert_held(&ifmgd->mtx);
  2070. /* Process beacon from the current BSS */
  2071. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  2072. if (baselen > len)
  2073. return;
  2074. rcu_read_lock();
  2075. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2076. if (!chanctx_conf) {
  2077. rcu_read_unlock();
  2078. return;
  2079. }
  2080. if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
  2081. rcu_read_unlock();
  2082. return;
  2083. }
  2084. chan = chanctx_conf->def.chan;
  2085. rcu_read_unlock();
  2086. if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
  2087. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  2088. ieee802_11_parse_elems(mgmt->u.beacon.variable,
  2089. len - baselen, &elems);
  2090. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2091. ifmgd->assoc_data->have_beacon = true;
  2092. ifmgd->assoc_data->need_beacon = false;
  2093. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  2094. sdata->vif.bss_conf.sync_tsf =
  2095. le64_to_cpu(mgmt->u.beacon.timestamp);
  2096. sdata->vif.bss_conf.sync_device_ts =
  2097. rx_status->device_timestamp;
  2098. if (elems.tim)
  2099. sdata->vif.bss_conf.sync_dtim_count =
  2100. elems.tim->dtim_count;
  2101. else
  2102. sdata->vif.bss_conf.sync_dtim_count = 0;
  2103. }
  2104. /* continue assoc process */
  2105. ifmgd->assoc_data->timeout = jiffies;
  2106. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2107. return;
  2108. }
  2109. if (!ifmgd->associated ||
  2110. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2111. return;
  2112. bssid = ifmgd->associated->bssid;
  2113. /* Track average RSSI from the Beacon frames of the current AP */
  2114. ifmgd->last_beacon_signal = rx_status->signal;
  2115. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  2116. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  2117. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  2118. ifmgd->last_cqm_event_signal = 0;
  2119. ifmgd->count_beacon_signal = 1;
  2120. ifmgd->last_ave_beacon_signal = 0;
  2121. } else {
  2122. ifmgd->ave_beacon_signal =
  2123. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  2124. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  2125. ifmgd->ave_beacon_signal) / 16;
  2126. ifmgd->count_beacon_signal++;
  2127. }
  2128. if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
  2129. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  2130. int sig = ifmgd->ave_beacon_signal;
  2131. int last_sig = ifmgd->last_ave_beacon_signal;
  2132. /*
  2133. * if signal crosses either of the boundaries, invoke callback
  2134. * with appropriate parameters
  2135. */
  2136. if (sig > ifmgd->rssi_max_thold &&
  2137. (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
  2138. ifmgd->last_ave_beacon_signal = sig;
  2139. drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
  2140. } else if (sig < ifmgd->rssi_min_thold &&
  2141. (last_sig >= ifmgd->rssi_max_thold ||
  2142. last_sig == 0)) {
  2143. ifmgd->last_ave_beacon_signal = sig;
  2144. drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
  2145. }
  2146. }
  2147. if (bss_conf->cqm_rssi_thold &&
  2148. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  2149. !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
  2150. int sig = ifmgd->ave_beacon_signal / 16;
  2151. int last_event = ifmgd->last_cqm_event_signal;
  2152. int thold = bss_conf->cqm_rssi_thold;
  2153. int hyst = bss_conf->cqm_rssi_hyst;
  2154. if (sig < thold &&
  2155. (last_event == 0 || sig < last_event - hyst)) {
  2156. ifmgd->last_cqm_event_signal = sig;
  2157. ieee80211_cqm_rssi_notify(
  2158. &sdata->vif,
  2159. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  2160. GFP_KERNEL);
  2161. } else if (sig > thold &&
  2162. (last_event == 0 || sig > last_event + hyst)) {
  2163. ifmgd->last_cqm_event_signal = sig;
  2164. ieee80211_cqm_rssi_notify(
  2165. &sdata->vif,
  2166. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  2167. GFP_KERNEL);
  2168. }
  2169. }
  2170. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  2171. mlme_dbg_ratelimited(sdata,
  2172. "cancelling AP probe due to a received beacon\n");
  2173. mutex_lock(&local->mtx);
  2174. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  2175. ieee80211_run_deferred_scan(local);
  2176. mutex_unlock(&local->mtx);
  2177. mutex_lock(&local->iflist_mtx);
  2178. ieee80211_recalc_ps(local, -1);
  2179. mutex_unlock(&local->iflist_mtx);
  2180. }
  2181. /*
  2182. * Push the beacon loss detection into the future since
  2183. * we are processing a beacon from the AP just now.
  2184. */
  2185. ieee80211_sta_reset_beacon_monitor(sdata);
  2186. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  2187. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  2188. len - baselen, &elems,
  2189. care_about_ies, ncrc);
  2190. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  2191. bool directed_tim = ieee80211_check_tim(elems.tim,
  2192. elems.tim_len,
  2193. ifmgd->aid);
  2194. if (directed_tim) {
  2195. if (local->hw.conf.dynamic_ps_timeout > 0) {
  2196. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2197. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2198. ieee80211_hw_config(local,
  2199. IEEE80211_CONF_CHANGE_PS);
  2200. }
  2201. ieee80211_send_nullfunc(local, sdata, 0);
  2202. } else if (!local->pspolling && sdata->u.mgd.powersave) {
  2203. local->pspolling = true;
  2204. /*
  2205. * Here is assumed that the driver will be
  2206. * able to send ps-poll frame and receive a
  2207. * response even though power save mode is
  2208. * enabled, but some drivers might require
  2209. * to disable power save here. This needs
  2210. * to be investigated.
  2211. */
  2212. ieee80211_send_pspoll(local, sdata);
  2213. }
  2214. }
  2215. }
  2216. if (sdata->vif.p2p) {
  2217. u8 noa[2];
  2218. int ret;
  2219. ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
  2220. len - baselen,
  2221. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  2222. noa, sizeof(noa));
  2223. if (ret >= 2 && sdata->u.mgd.p2p_noa_index != noa[0]) {
  2224. bss_conf->p2p_oppps = noa[1] & 0x80;
  2225. bss_conf->p2p_ctwindow = noa[1] & 0x7f;
  2226. changed |= BSS_CHANGED_P2P_PS;
  2227. sdata->u.mgd.p2p_noa_index = noa[0];
  2228. /*
  2229. * make sure we update all information, the CRC
  2230. * mechanism doesn't look at P2P attributes.
  2231. */
  2232. ifmgd->beacon_crc_valid = false;
  2233. }
  2234. }
  2235. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  2236. return;
  2237. ifmgd->beacon_crc = ncrc;
  2238. ifmgd->beacon_crc_valid = true;
  2239. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2240. if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2241. elems.wmm_param_len))
  2242. changed |= BSS_CHANGED_QOS;
  2243. /*
  2244. * If we haven't had a beacon before, tell the driver about the
  2245. * DTIM period (and beacon timing if desired) now.
  2246. */
  2247. if (!bss_conf->dtim_period) {
  2248. /* a few bogus AP send dtim_period = 0 or no TIM IE */
  2249. if (elems.tim)
  2250. bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
  2251. else
  2252. bss_conf->dtim_period = 1;
  2253. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  2254. sdata->vif.bss_conf.sync_tsf =
  2255. le64_to_cpu(mgmt->u.beacon.timestamp);
  2256. sdata->vif.bss_conf.sync_device_ts =
  2257. rx_status->device_timestamp;
  2258. if (elems.tim)
  2259. sdata->vif.bss_conf.sync_dtim_count =
  2260. elems.tim->dtim_count;
  2261. else
  2262. sdata->vif.bss_conf.sync_dtim_count = 0;
  2263. }
  2264. changed |= BSS_CHANGED_DTIM_PERIOD;
  2265. }
  2266. if (elems.erp_info && elems.erp_info_len >= 1) {
  2267. erp_valid = true;
  2268. erp_value = elems.erp_info[0];
  2269. } else {
  2270. erp_valid = false;
  2271. }
  2272. changed |= ieee80211_handle_bss_capability(sdata,
  2273. le16_to_cpu(mgmt->u.beacon.capab_info),
  2274. erp_valid, erp_value);
  2275. if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
  2276. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  2277. changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
  2278. bssid, true);
  2279. if (elems.country_elem && elems.pwr_constr_elem &&
  2280. mgmt->u.probe_resp.capab_info &
  2281. cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
  2282. changed |= ieee80211_handle_pwr_constr(sdata, chan,
  2283. elems.country_elem,
  2284. elems.country_elem_len,
  2285. elems.pwr_constr_elem);
  2286. ieee80211_bss_info_change_notify(sdata, changed);
  2287. }
  2288. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  2289. struct sk_buff *skb)
  2290. {
  2291. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2292. struct ieee80211_rx_status *rx_status;
  2293. struct ieee80211_mgmt *mgmt;
  2294. struct cfg80211_bss *bss = NULL;
  2295. enum rx_mgmt_action rma = RX_MGMT_NONE;
  2296. u16 fc;
  2297. rx_status = (struct ieee80211_rx_status *) skb->cb;
  2298. mgmt = (struct ieee80211_mgmt *) skb->data;
  2299. fc = le16_to_cpu(mgmt->frame_control);
  2300. mutex_lock(&ifmgd->mtx);
  2301. switch (fc & IEEE80211_FCTL_STYPE) {
  2302. case IEEE80211_STYPE_BEACON:
  2303. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  2304. break;
  2305. case IEEE80211_STYPE_PROBE_RESP:
  2306. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  2307. break;
  2308. case IEEE80211_STYPE_AUTH:
  2309. rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  2310. break;
  2311. case IEEE80211_STYPE_DEAUTH:
  2312. rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  2313. break;
  2314. case IEEE80211_STYPE_DISASSOC:
  2315. rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  2316. break;
  2317. case IEEE80211_STYPE_ASSOC_RESP:
  2318. case IEEE80211_STYPE_REASSOC_RESP:
  2319. rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
  2320. break;
  2321. case IEEE80211_STYPE_ACTION:
  2322. switch (mgmt->u.action.category) {
  2323. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2324. ieee80211_sta_process_chanswitch(sdata,
  2325. &mgmt->u.action.u.chan_switch.sw_elem,
  2326. (void *)ifmgd->associated->priv,
  2327. rx_status->mactime);
  2328. break;
  2329. }
  2330. }
  2331. mutex_unlock(&ifmgd->mtx);
  2332. switch (rma) {
  2333. case RX_MGMT_NONE:
  2334. /* no action */
  2335. break;
  2336. case RX_MGMT_CFG80211_DEAUTH:
  2337. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  2338. break;
  2339. case RX_MGMT_CFG80211_DISASSOC:
  2340. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  2341. break;
  2342. case RX_MGMT_CFG80211_RX_AUTH:
  2343. cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
  2344. break;
  2345. case RX_MGMT_CFG80211_RX_ASSOC:
  2346. cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
  2347. break;
  2348. case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
  2349. cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
  2350. break;
  2351. default:
  2352. WARN(1, "unexpected: %d", rma);
  2353. }
  2354. }
  2355. static void ieee80211_sta_timer(unsigned long data)
  2356. {
  2357. struct ieee80211_sub_if_data *sdata =
  2358. (struct ieee80211_sub_if_data *) data;
  2359. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2360. struct ieee80211_local *local = sdata->local;
  2361. if (local->quiescing) {
  2362. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  2363. return;
  2364. }
  2365. ieee80211_queue_work(&local->hw, &sdata->work);
  2366. }
  2367. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  2368. u8 *bssid, u8 reason, bool tx)
  2369. {
  2370. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2371. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2372. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
  2373. tx, frame_buf);
  2374. mutex_unlock(&ifmgd->mtx);
  2375. /*
  2376. * must be outside lock due to cfg80211,
  2377. * but that's not a problem.
  2378. */
  2379. cfg80211_send_deauth(sdata->dev, frame_buf, IEEE80211_DEAUTH_FRAME_LEN);
  2380. mutex_lock(&ifmgd->mtx);
  2381. }
  2382. static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
  2383. {
  2384. struct ieee80211_local *local = sdata->local;
  2385. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2386. struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
  2387. u32 tx_flags = 0;
  2388. lockdep_assert_held(&ifmgd->mtx);
  2389. if (WARN_ON_ONCE(!auth_data))
  2390. return -EINVAL;
  2391. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2392. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  2393. IEEE80211_TX_INTFL_MLME_CONN_TX;
  2394. auth_data->tries++;
  2395. if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
  2396. sdata_info(sdata, "authentication with %pM timed out\n",
  2397. auth_data->bss->bssid);
  2398. /*
  2399. * Most likely AP is not in the range so remove the
  2400. * bss struct for that AP.
  2401. */
  2402. cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
  2403. return -ETIMEDOUT;
  2404. }
  2405. drv_mgd_prepare_tx(local, sdata);
  2406. if (auth_data->bss->proberesp_ies) {
  2407. u16 trans = 1;
  2408. u16 status = 0;
  2409. sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
  2410. auth_data->bss->bssid, auth_data->tries,
  2411. IEEE80211_AUTH_MAX_TRIES);
  2412. auth_data->expected_transaction = 2;
  2413. if (auth_data->algorithm == WLAN_AUTH_SAE) {
  2414. trans = auth_data->sae_trans;
  2415. status = auth_data->sae_status;
  2416. auth_data->expected_transaction = trans;
  2417. }
  2418. ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
  2419. auth_data->data, auth_data->data_len,
  2420. auth_data->bss->bssid,
  2421. auth_data->bss->bssid, NULL, 0, 0,
  2422. tx_flags);
  2423. } else {
  2424. const u8 *ssidie;
  2425. sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
  2426. auth_data->bss->bssid, auth_data->tries,
  2427. IEEE80211_AUTH_MAX_TRIES);
  2428. rcu_read_lock();
  2429. ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
  2430. if (!ssidie) {
  2431. rcu_read_unlock();
  2432. return -EINVAL;
  2433. }
  2434. /*
  2435. * Direct probe is sent to broadcast address as some APs
  2436. * will not answer to direct packet in unassociated state.
  2437. */
  2438. ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
  2439. NULL, 0, (u32) -1, true, tx_flags,
  2440. auth_data->bss->channel, false);
  2441. rcu_read_unlock();
  2442. }
  2443. if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
  2444. auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  2445. run_again(ifmgd, auth_data->timeout);
  2446. }
  2447. return 0;
  2448. }
  2449. static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
  2450. {
  2451. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2452. struct ieee80211_local *local = sdata->local;
  2453. lockdep_assert_held(&sdata->u.mgd.mtx);
  2454. assoc_data->tries++;
  2455. if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
  2456. sdata_info(sdata, "association with %pM timed out\n",
  2457. assoc_data->bss->bssid);
  2458. /*
  2459. * Most likely AP is not in the range so remove the
  2460. * bss struct for that AP.
  2461. */
  2462. cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
  2463. return -ETIMEDOUT;
  2464. }
  2465. sdata_info(sdata, "associate with %pM (try %d/%d)\n",
  2466. assoc_data->bss->bssid, assoc_data->tries,
  2467. IEEE80211_ASSOC_MAX_TRIES);
  2468. ieee80211_send_assoc(sdata);
  2469. if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
  2470. assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  2471. run_again(&sdata->u.mgd, assoc_data->timeout);
  2472. }
  2473. return 0;
  2474. }
  2475. void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
  2476. __le16 fc, bool acked)
  2477. {
  2478. struct ieee80211_local *local = sdata->local;
  2479. sdata->u.mgd.status_fc = fc;
  2480. sdata->u.mgd.status_acked = acked;
  2481. sdata->u.mgd.status_received = true;
  2482. ieee80211_queue_work(&local->hw, &sdata->work);
  2483. }
  2484. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  2485. {
  2486. struct ieee80211_local *local = sdata->local;
  2487. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2488. mutex_lock(&ifmgd->mtx);
  2489. if (ifmgd->status_received) {
  2490. __le16 fc = ifmgd->status_fc;
  2491. bool status_acked = ifmgd->status_acked;
  2492. ifmgd->status_received = false;
  2493. if (ifmgd->auth_data &&
  2494. (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
  2495. if (status_acked) {
  2496. ifmgd->auth_data->timeout =
  2497. jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
  2498. run_again(ifmgd, ifmgd->auth_data->timeout);
  2499. } else {
  2500. ifmgd->auth_data->timeout = jiffies - 1;
  2501. }
  2502. } else if (ifmgd->assoc_data &&
  2503. (ieee80211_is_assoc_req(fc) ||
  2504. ieee80211_is_reassoc_req(fc))) {
  2505. if (status_acked) {
  2506. ifmgd->assoc_data->timeout =
  2507. jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
  2508. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2509. } else {
  2510. ifmgd->assoc_data->timeout = jiffies - 1;
  2511. }
  2512. }
  2513. }
  2514. if (ifmgd->auth_data &&
  2515. time_after(jiffies, ifmgd->auth_data->timeout)) {
  2516. if (ifmgd->auth_data->done) {
  2517. /*
  2518. * ok ... we waited for assoc but userspace didn't,
  2519. * so let's just kill the auth data
  2520. */
  2521. ieee80211_destroy_auth_data(sdata, false);
  2522. } else if (ieee80211_probe_auth(sdata)) {
  2523. u8 bssid[ETH_ALEN];
  2524. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  2525. ieee80211_destroy_auth_data(sdata, false);
  2526. mutex_unlock(&ifmgd->mtx);
  2527. cfg80211_send_auth_timeout(sdata->dev, bssid);
  2528. mutex_lock(&ifmgd->mtx);
  2529. }
  2530. } else if (ifmgd->auth_data)
  2531. run_again(ifmgd, ifmgd->auth_data->timeout);
  2532. if (ifmgd->assoc_data &&
  2533. time_after(jiffies, ifmgd->assoc_data->timeout)) {
  2534. if ((ifmgd->assoc_data->need_beacon &&
  2535. !ifmgd->assoc_data->have_beacon) ||
  2536. ieee80211_do_assoc(sdata)) {
  2537. u8 bssid[ETH_ALEN];
  2538. memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
  2539. ieee80211_destroy_assoc_data(sdata, false);
  2540. mutex_unlock(&ifmgd->mtx);
  2541. cfg80211_send_assoc_timeout(sdata->dev, bssid);
  2542. mutex_lock(&ifmgd->mtx);
  2543. }
  2544. } else if (ifmgd->assoc_data)
  2545. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2546. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  2547. IEEE80211_STA_CONNECTION_POLL) &&
  2548. ifmgd->associated) {
  2549. u8 bssid[ETH_ALEN];
  2550. int max_tries;
  2551. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  2552. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2553. max_tries = max_nullfunc_tries;
  2554. else
  2555. max_tries = max_probe_tries;
  2556. /* ACK received for nullfunc probing frame */
  2557. if (!ifmgd->probe_send_count)
  2558. ieee80211_reset_ap_probe(sdata);
  2559. else if (ifmgd->nullfunc_failed) {
  2560. if (ifmgd->probe_send_count < max_tries) {
  2561. mlme_dbg(sdata,
  2562. "No ack for nullfunc frame to AP %pM, try %d/%i\n",
  2563. bssid, ifmgd->probe_send_count,
  2564. max_tries);
  2565. ieee80211_mgd_probe_ap_send(sdata);
  2566. } else {
  2567. mlme_dbg(sdata,
  2568. "No ack for nullfunc frame to AP %pM, disconnecting.\n",
  2569. bssid);
  2570. ieee80211_sta_connection_lost(sdata, bssid,
  2571. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  2572. false);
  2573. }
  2574. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  2575. run_again(ifmgd, ifmgd->probe_timeout);
  2576. else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  2577. mlme_dbg(sdata,
  2578. "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
  2579. bssid, probe_wait_ms);
  2580. ieee80211_sta_connection_lost(sdata, bssid,
  2581. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  2582. } else if (ifmgd->probe_send_count < max_tries) {
  2583. mlme_dbg(sdata,
  2584. "No probe response from AP %pM after %dms, try %d/%i\n",
  2585. bssid, probe_wait_ms,
  2586. ifmgd->probe_send_count, max_tries);
  2587. ieee80211_mgd_probe_ap_send(sdata);
  2588. } else {
  2589. /*
  2590. * We actually lost the connection ... or did we?
  2591. * Let's make sure!
  2592. */
  2593. wiphy_debug(local->hw.wiphy,
  2594. "%s: No probe response from AP %pM"
  2595. " after %dms, disconnecting.\n",
  2596. sdata->name,
  2597. bssid, probe_wait_ms);
  2598. ieee80211_sta_connection_lost(sdata, bssid,
  2599. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  2600. }
  2601. }
  2602. mutex_unlock(&ifmgd->mtx);
  2603. }
  2604. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  2605. {
  2606. struct ieee80211_sub_if_data *sdata =
  2607. (struct ieee80211_sub_if_data *) data;
  2608. struct ieee80211_local *local = sdata->local;
  2609. if (local->quiescing)
  2610. return;
  2611. sdata->u.mgd.connection_loss = false;
  2612. ieee80211_queue_work(&sdata->local->hw,
  2613. &sdata->u.mgd.beacon_connection_loss_work);
  2614. }
  2615. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  2616. {
  2617. struct ieee80211_sub_if_data *sdata =
  2618. (struct ieee80211_sub_if_data *) data;
  2619. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2620. struct ieee80211_local *local = sdata->local;
  2621. if (local->quiescing)
  2622. return;
  2623. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  2624. }
  2625. static void ieee80211_sta_monitor_work(struct work_struct *work)
  2626. {
  2627. struct ieee80211_sub_if_data *sdata =
  2628. container_of(work, struct ieee80211_sub_if_data,
  2629. u.mgd.monitor_work);
  2630. ieee80211_mgd_probe_ap(sdata, false);
  2631. }
  2632. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  2633. {
  2634. u32 flags;
  2635. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  2636. __ieee80211_stop_poll(sdata);
  2637. /* let's probe the connection once */
  2638. flags = sdata->local->hw.flags;
  2639. if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
  2640. ieee80211_queue_work(&sdata->local->hw,
  2641. &sdata->u.mgd.monitor_work);
  2642. /* and do all the other regular work too */
  2643. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  2644. }
  2645. }
  2646. #ifdef CONFIG_PM
  2647. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  2648. {
  2649. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2650. /*
  2651. * we need to use atomic bitops for the running bits
  2652. * only because both timers might fire at the same
  2653. * time -- the code here is properly synchronised.
  2654. */
  2655. cancel_work_sync(&ifmgd->request_smps_work);
  2656. cancel_work_sync(&ifmgd->monitor_work);
  2657. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  2658. cancel_work_sync(&ifmgd->csa_connection_drop_work);
  2659. if (del_timer_sync(&ifmgd->timer))
  2660. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  2661. cancel_work_sync(&ifmgd->chswitch_work);
  2662. if (del_timer_sync(&ifmgd->chswitch_timer))
  2663. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  2664. /* these will just be re-established on connection */
  2665. del_timer_sync(&ifmgd->conn_mon_timer);
  2666. del_timer_sync(&ifmgd->bcn_mon_timer);
  2667. }
  2668. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  2669. {
  2670. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2671. mutex_lock(&ifmgd->mtx);
  2672. if (!ifmgd->associated) {
  2673. mutex_unlock(&ifmgd->mtx);
  2674. return;
  2675. }
  2676. if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
  2677. sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
  2678. mlme_dbg(sdata, "driver requested disconnect after resume\n");
  2679. ieee80211_sta_connection_lost(sdata,
  2680. ifmgd->associated->bssid,
  2681. WLAN_REASON_UNSPECIFIED,
  2682. true);
  2683. mutex_unlock(&ifmgd->mtx);
  2684. return;
  2685. }
  2686. mutex_unlock(&ifmgd->mtx);
  2687. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  2688. add_timer(&ifmgd->timer);
  2689. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  2690. add_timer(&ifmgd->chswitch_timer);
  2691. ieee80211_sta_reset_beacon_monitor(sdata);
  2692. mutex_lock(&sdata->local->mtx);
  2693. ieee80211_restart_sta_timer(sdata);
  2694. mutex_unlock(&sdata->local->mtx);
  2695. }
  2696. #endif
  2697. /* interface setup */
  2698. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  2699. {
  2700. struct ieee80211_if_managed *ifmgd;
  2701. ifmgd = &sdata->u.mgd;
  2702. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  2703. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  2704. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  2705. ieee80211_beacon_connection_loss_work);
  2706. INIT_WORK(&ifmgd->csa_connection_drop_work,
  2707. ieee80211_csa_connection_drop_work);
  2708. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
  2709. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  2710. (unsigned long) sdata);
  2711. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  2712. (unsigned long) sdata);
  2713. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  2714. (unsigned long) sdata);
  2715. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  2716. (unsigned long) sdata);
  2717. ifmgd->flags = 0;
  2718. ifmgd->powersave = sdata->wdev.ps;
  2719. ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
  2720. ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
  2721. mutex_init(&ifmgd->mtx);
  2722. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  2723. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  2724. else
  2725. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  2726. }
  2727. /* scan finished notification */
  2728. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  2729. {
  2730. struct ieee80211_sub_if_data *sdata;
  2731. /* Restart STA timers */
  2732. rcu_read_lock();
  2733. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  2734. ieee80211_restart_sta_timer(sdata);
  2735. rcu_read_unlock();
  2736. }
  2737. int ieee80211_max_network_latency(struct notifier_block *nb,
  2738. unsigned long data, void *dummy)
  2739. {
  2740. s32 latency_usec = (s32) data;
  2741. struct ieee80211_local *local =
  2742. container_of(nb, struct ieee80211_local,
  2743. network_latency_notifier);
  2744. mutex_lock(&local->iflist_mtx);
  2745. ieee80211_recalc_ps(local, latency_usec);
  2746. mutex_unlock(&local->iflist_mtx);
  2747. return 0;
  2748. }
  2749. static u32 chandef_downgrade(struct cfg80211_chan_def *c)
  2750. {
  2751. u32 ret;
  2752. int tmp;
  2753. switch (c->width) {
  2754. case NL80211_CHAN_WIDTH_20:
  2755. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2756. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2757. break;
  2758. case NL80211_CHAN_WIDTH_40:
  2759. c->width = NL80211_CHAN_WIDTH_20;
  2760. c->center_freq1 = c->chan->center_freq;
  2761. ret = IEEE80211_STA_DISABLE_40MHZ |
  2762. IEEE80211_STA_DISABLE_VHT;
  2763. break;
  2764. case NL80211_CHAN_WIDTH_80:
  2765. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  2766. /* n_P40 */
  2767. tmp /= 2;
  2768. /* freq_P40 */
  2769. c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
  2770. c->width = NL80211_CHAN_WIDTH_40;
  2771. ret = IEEE80211_STA_DISABLE_VHT;
  2772. break;
  2773. case NL80211_CHAN_WIDTH_80P80:
  2774. c->center_freq2 = 0;
  2775. c->width = NL80211_CHAN_WIDTH_80;
  2776. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2777. IEEE80211_STA_DISABLE_160MHZ;
  2778. break;
  2779. case NL80211_CHAN_WIDTH_160:
  2780. /* n_P20 */
  2781. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  2782. /* n_P80 */
  2783. tmp /= 4;
  2784. c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
  2785. c->width = NL80211_CHAN_WIDTH_80;
  2786. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2787. IEEE80211_STA_DISABLE_160MHZ;
  2788. break;
  2789. default:
  2790. case NL80211_CHAN_WIDTH_20_NOHT:
  2791. WARN_ON_ONCE(1);
  2792. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2793. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2794. break;
  2795. }
  2796. WARN_ON_ONCE(!cfg80211_chandef_valid(c));
  2797. return ret;
  2798. }
  2799. static u32
  2800. ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
  2801. struct ieee80211_supported_band *sband,
  2802. struct ieee80211_channel *channel,
  2803. const struct ieee80211_ht_operation *ht_oper,
  2804. const struct ieee80211_vht_operation *vht_oper,
  2805. struct cfg80211_chan_def *chandef)
  2806. {
  2807. struct cfg80211_chan_def vht_chandef;
  2808. u32 ht_cfreq, ret;
  2809. chandef->chan = channel;
  2810. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  2811. chandef->center_freq1 = channel->center_freq;
  2812. chandef->center_freq2 = 0;
  2813. if (!ht_oper || !sband->ht_cap.ht_supported) {
  2814. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2815. goto out;
  2816. }
  2817. chandef->width = NL80211_CHAN_WIDTH_20;
  2818. ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
  2819. channel->band);
  2820. /* check that channel matches the right operating channel */
  2821. if (channel->center_freq != ht_cfreq) {
  2822. /*
  2823. * It's possible that some APs are confused here;
  2824. * Netgear WNDR3700 sometimes reports 4 higher than
  2825. * the actual channel in association responses, but
  2826. * since we look at probe response/beacon data here
  2827. * it should be OK.
  2828. */
  2829. sdata_info(sdata,
  2830. "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
  2831. channel->center_freq, ht_cfreq,
  2832. ht_oper->primary_chan, channel->band);
  2833. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2834. goto out;
  2835. }
  2836. /* check 40 MHz support, if we have it */
  2837. if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  2838. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  2839. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  2840. chandef->width = NL80211_CHAN_WIDTH_40;
  2841. chandef->center_freq1 += 10;
  2842. break;
  2843. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  2844. chandef->width = NL80211_CHAN_WIDTH_40;
  2845. chandef->center_freq1 -= 10;
  2846. break;
  2847. }
  2848. } else {
  2849. /* 40 MHz (and 80 MHz) must be supported for VHT */
  2850. ret = IEEE80211_STA_DISABLE_VHT;
  2851. goto out;
  2852. }
  2853. if (!vht_oper || !sband->vht_cap.vht_supported) {
  2854. ret = IEEE80211_STA_DISABLE_VHT;
  2855. goto out;
  2856. }
  2857. vht_chandef.chan = channel;
  2858. vht_chandef.center_freq1 =
  2859. ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
  2860. channel->band);
  2861. vht_chandef.center_freq2 = 0;
  2862. if (vht_oper->center_freq_seg2_idx)
  2863. vht_chandef.center_freq2 =
  2864. ieee80211_channel_to_frequency(
  2865. vht_oper->center_freq_seg2_idx,
  2866. channel->band);
  2867. switch (vht_oper->chan_width) {
  2868. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  2869. vht_chandef.width = chandef->width;
  2870. break;
  2871. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  2872. vht_chandef.width = NL80211_CHAN_WIDTH_80;
  2873. break;
  2874. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  2875. vht_chandef.width = NL80211_CHAN_WIDTH_160;
  2876. break;
  2877. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  2878. vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
  2879. break;
  2880. default:
  2881. sdata_info(sdata,
  2882. "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
  2883. vht_oper->chan_width);
  2884. ret = IEEE80211_STA_DISABLE_VHT;
  2885. goto out;
  2886. }
  2887. if (!cfg80211_chandef_valid(&vht_chandef)) {
  2888. sdata_info(sdata,
  2889. "AP VHT information is invalid, disable VHT\n");
  2890. ret = IEEE80211_STA_DISABLE_VHT;
  2891. goto out;
  2892. }
  2893. if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
  2894. ret = 0;
  2895. goto out;
  2896. }
  2897. if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
  2898. sdata_info(sdata,
  2899. "AP VHT information doesn't match HT, disable VHT\n");
  2900. ret = IEEE80211_STA_DISABLE_VHT;
  2901. goto out;
  2902. }
  2903. *chandef = vht_chandef;
  2904. ret = 0;
  2905. while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  2906. IEEE80211_CHAN_DISABLED)) {
  2907. if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
  2908. ret = IEEE80211_STA_DISABLE_HT |
  2909. IEEE80211_STA_DISABLE_VHT;
  2910. goto out;
  2911. }
  2912. ret = chandef_downgrade(chandef);
  2913. }
  2914. if (chandef->width != vht_chandef.width)
  2915. sdata_info(sdata,
  2916. "local regulatory prevented using AP HT/VHT configuration, downgraded\n");
  2917. out:
  2918. WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
  2919. return ret;
  2920. }
  2921. static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
  2922. struct cfg80211_bss *cbss)
  2923. {
  2924. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2925. const u8 *ht_cap_ie, *vht_cap_ie;
  2926. const struct ieee80211_ht_cap *ht_cap;
  2927. const struct ieee80211_vht_cap *vht_cap;
  2928. u8 chains = 1;
  2929. if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
  2930. return chains;
  2931. ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  2932. if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
  2933. ht_cap = (void *)(ht_cap_ie + 2);
  2934. chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
  2935. /*
  2936. * TODO: use "Tx Maximum Number Spatial Streams Supported" and
  2937. * "Tx Unequal Modulation Supported" fields.
  2938. */
  2939. }
  2940. if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
  2941. return chains;
  2942. vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  2943. if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
  2944. u8 nss;
  2945. u16 tx_mcs_map;
  2946. vht_cap = (void *)(vht_cap_ie + 2);
  2947. tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
  2948. for (nss = 8; nss > 0; nss--) {
  2949. if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
  2950. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  2951. break;
  2952. }
  2953. /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
  2954. chains = max(chains, nss);
  2955. }
  2956. return chains;
  2957. }
  2958. static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
  2959. struct cfg80211_bss *cbss)
  2960. {
  2961. struct ieee80211_local *local = sdata->local;
  2962. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2963. const struct ieee80211_ht_operation *ht_oper = NULL;
  2964. const struct ieee80211_vht_operation *vht_oper = NULL;
  2965. struct ieee80211_supported_band *sband;
  2966. struct cfg80211_chan_def chandef;
  2967. int ret;
  2968. sband = local->hw.wiphy->bands[cbss->channel->band];
  2969. ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
  2970. IEEE80211_STA_DISABLE_80P80MHZ |
  2971. IEEE80211_STA_DISABLE_160MHZ);
  2972. rcu_read_lock();
  2973. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  2974. sband->ht_cap.ht_supported) {
  2975. const u8 *ht_oper_ie;
  2976. ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
  2977. if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
  2978. ht_oper = (void *)(ht_oper_ie + 2);
  2979. }
  2980. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  2981. sband->vht_cap.vht_supported) {
  2982. const u8 *vht_oper_ie;
  2983. vht_oper_ie = ieee80211_bss_get_ie(cbss,
  2984. WLAN_EID_VHT_OPERATION);
  2985. if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
  2986. vht_oper = (void *)(vht_oper_ie + 2);
  2987. if (vht_oper && !ht_oper) {
  2988. vht_oper = NULL;
  2989. sdata_info(sdata,
  2990. "AP advertised VHT without HT, disabling both\n");
  2991. sdata->flags |= IEEE80211_STA_DISABLE_HT;
  2992. sdata->flags |= IEEE80211_STA_DISABLE_VHT;
  2993. }
  2994. }
  2995. ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
  2996. cbss->channel,
  2997. ht_oper, vht_oper,
  2998. &chandef);
  2999. sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
  3000. local->rx_chains);
  3001. rcu_read_unlock();
  3002. /* will change later if needed */
  3003. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3004. /*
  3005. * If this fails (possibly due to channel context sharing
  3006. * on incompatible channels, e.g. 80+80 and 160 sharing the
  3007. * same control channel) try to use a smaller bandwidth.
  3008. */
  3009. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3010. IEEE80211_CHANCTX_SHARED);
  3011. while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
  3012. ifmgd->flags |= chandef_downgrade(&chandef);
  3013. return ret;
  3014. }
  3015. static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
  3016. struct cfg80211_bss *cbss, bool assoc)
  3017. {
  3018. struct ieee80211_local *local = sdata->local;
  3019. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3020. struct ieee80211_bss *bss = (void *)cbss->priv;
  3021. struct sta_info *new_sta = NULL;
  3022. bool have_sta = false;
  3023. int err;
  3024. if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
  3025. return -EINVAL;
  3026. if (assoc) {
  3027. rcu_read_lock();
  3028. have_sta = sta_info_get(sdata, cbss->bssid);
  3029. rcu_read_unlock();
  3030. }
  3031. if (!have_sta) {
  3032. new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  3033. if (!new_sta)
  3034. return -ENOMEM;
  3035. }
  3036. if (new_sta) {
  3037. u32 rates = 0, basic_rates = 0;
  3038. bool have_higher_than_11mbit;
  3039. int min_rate = INT_MAX, min_rate_index = -1;
  3040. struct ieee80211_supported_band *sband;
  3041. const struct cfg80211_bss_ies *ies;
  3042. sband = local->hw.wiphy->bands[cbss->channel->band];
  3043. err = ieee80211_prep_channel(sdata, cbss);
  3044. if (err) {
  3045. sta_info_free(local, new_sta);
  3046. return err;
  3047. }
  3048. ieee80211_get_rates(sband, bss->supp_rates,
  3049. bss->supp_rates_len,
  3050. &rates, &basic_rates,
  3051. &have_higher_than_11mbit,
  3052. &min_rate, &min_rate_index);
  3053. /*
  3054. * This used to be a workaround for basic rates missing
  3055. * in the association response frame. Now that we no
  3056. * longer use the basic rates from there, it probably
  3057. * doesn't happen any more, but keep the workaround so
  3058. * in case some *other* APs are buggy in different ways
  3059. * we can connect -- with a warning.
  3060. */
  3061. if (!basic_rates && min_rate_index >= 0) {
  3062. sdata_info(sdata,
  3063. "No basic rates, using min rate instead\n");
  3064. basic_rates = BIT(min_rate_index);
  3065. }
  3066. new_sta->sta.supp_rates[cbss->channel->band] = rates;
  3067. sdata->vif.bss_conf.basic_rates = basic_rates;
  3068. /* cf. IEEE 802.11 9.2.12 */
  3069. if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
  3070. have_higher_than_11mbit)
  3071. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  3072. else
  3073. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  3074. memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
  3075. /* set timing information */
  3076. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  3077. rcu_read_lock();
  3078. ies = rcu_dereference(cbss->beacon_ies);
  3079. if (ies) {
  3080. const u8 *tim_ie;
  3081. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3082. sdata->vif.bss_conf.sync_device_ts =
  3083. bss->device_ts_beacon;
  3084. tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3085. ies->data, ies->len);
  3086. if (tim_ie && tim_ie[1] >= 2)
  3087. sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
  3088. else
  3089. sdata->vif.bss_conf.sync_dtim_count = 0;
  3090. } else if (!(local->hw.flags &
  3091. IEEE80211_HW_TIMING_BEACON_ONLY)) {
  3092. ies = rcu_dereference(cbss->proberesp_ies);
  3093. /* must be non-NULL since beacon IEs were NULL */
  3094. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3095. sdata->vif.bss_conf.sync_device_ts =
  3096. bss->device_ts_presp;
  3097. sdata->vif.bss_conf.sync_dtim_count = 0;
  3098. } else {
  3099. sdata->vif.bss_conf.sync_tsf = 0;
  3100. sdata->vif.bss_conf.sync_device_ts = 0;
  3101. sdata->vif.bss_conf.sync_dtim_count = 0;
  3102. }
  3103. rcu_read_unlock();
  3104. /* tell driver about BSSID, basic rates and timing */
  3105. ieee80211_bss_info_change_notify(sdata,
  3106. BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
  3107. BSS_CHANGED_BEACON_INT);
  3108. if (assoc)
  3109. sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
  3110. err = sta_info_insert(new_sta);
  3111. new_sta = NULL;
  3112. if (err) {
  3113. sdata_info(sdata,
  3114. "failed to insert STA entry for the AP (error %d)\n",
  3115. err);
  3116. return err;
  3117. }
  3118. } else
  3119. WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
  3120. return 0;
  3121. }
  3122. /* config hooks */
  3123. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  3124. struct cfg80211_auth_request *req)
  3125. {
  3126. struct ieee80211_local *local = sdata->local;
  3127. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3128. struct ieee80211_mgd_auth_data *auth_data;
  3129. u16 auth_alg;
  3130. int err;
  3131. /* prepare auth data structure */
  3132. switch (req->auth_type) {
  3133. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  3134. auth_alg = WLAN_AUTH_OPEN;
  3135. break;
  3136. case NL80211_AUTHTYPE_SHARED_KEY:
  3137. if (IS_ERR(local->wep_tx_tfm))
  3138. return -EOPNOTSUPP;
  3139. auth_alg = WLAN_AUTH_SHARED_KEY;
  3140. break;
  3141. case NL80211_AUTHTYPE_FT:
  3142. auth_alg = WLAN_AUTH_FT;
  3143. break;
  3144. case NL80211_AUTHTYPE_NETWORK_EAP:
  3145. auth_alg = WLAN_AUTH_LEAP;
  3146. break;
  3147. case NL80211_AUTHTYPE_SAE:
  3148. auth_alg = WLAN_AUTH_SAE;
  3149. break;
  3150. default:
  3151. return -EOPNOTSUPP;
  3152. }
  3153. auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
  3154. req->ie_len, GFP_KERNEL);
  3155. if (!auth_data)
  3156. return -ENOMEM;
  3157. auth_data->bss = req->bss;
  3158. if (req->sae_data_len >= 4) {
  3159. __le16 *pos = (__le16 *) req->sae_data;
  3160. auth_data->sae_trans = le16_to_cpu(pos[0]);
  3161. auth_data->sae_status = le16_to_cpu(pos[1]);
  3162. memcpy(auth_data->data, req->sae_data + 4,
  3163. req->sae_data_len - 4);
  3164. auth_data->data_len += req->sae_data_len - 4;
  3165. }
  3166. if (req->ie && req->ie_len) {
  3167. memcpy(&auth_data->data[auth_data->data_len],
  3168. req->ie, req->ie_len);
  3169. auth_data->data_len += req->ie_len;
  3170. }
  3171. if (req->key && req->key_len) {
  3172. auth_data->key_len = req->key_len;
  3173. auth_data->key_idx = req->key_idx;
  3174. memcpy(auth_data->key, req->key, req->key_len);
  3175. }
  3176. auth_data->algorithm = auth_alg;
  3177. /* try to authenticate/probe */
  3178. mutex_lock(&ifmgd->mtx);
  3179. if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
  3180. ifmgd->assoc_data) {
  3181. err = -EBUSY;
  3182. goto err_free;
  3183. }
  3184. if (ifmgd->auth_data)
  3185. ieee80211_destroy_auth_data(sdata, false);
  3186. /* prep auth_data so we don't go into idle on disassoc */
  3187. ifmgd->auth_data = auth_data;
  3188. if (ifmgd->associated)
  3189. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  3190. sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
  3191. err = ieee80211_prep_connection(sdata, req->bss, false);
  3192. if (err)
  3193. goto err_clear;
  3194. err = ieee80211_probe_auth(sdata);
  3195. if (err) {
  3196. sta_info_destroy_addr(sdata, req->bss->bssid);
  3197. goto err_clear;
  3198. }
  3199. /* hold our own reference */
  3200. cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
  3201. err = 0;
  3202. goto out_unlock;
  3203. err_clear:
  3204. memset(ifmgd->bssid, 0, ETH_ALEN);
  3205. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3206. ifmgd->auth_data = NULL;
  3207. err_free:
  3208. kfree(auth_data);
  3209. out_unlock:
  3210. mutex_unlock(&ifmgd->mtx);
  3211. return err;
  3212. }
  3213. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  3214. struct cfg80211_assoc_request *req)
  3215. {
  3216. struct ieee80211_local *local = sdata->local;
  3217. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3218. struct ieee80211_bss *bss = (void *)req->bss->priv;
  3219. struct ieee80211_mgd_assoc_data *assoc_data;
  3220. const struct cfg80211_bss_ies *beacon_ies;
  3221. struct ieee80211_supported_band *sband;
  3222. const u8 *ssidie, *ht_ie, *vht_ie;
  3223. int i, err;
  3224. assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
  3225. if (!assoc_data)
  3226. return -ENOMEM;
  3227. rcu_read_lock();
  3228. ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  3229. if (!ssidie) {
  3230. rcu_read_unlock();
  3231. kfree(assoc_data);
  3232. return -EINVAL;
  3233. }
  3234. memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
  3235. assoc_data->ssid_len = ssidie[1];
  3236. rcu_read_unlock();
  3237. mutex_lock(&ifmgd->mtx);
  3238. if (ifmgd->associated)
  3239. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  3240. if (ifmgd->auth_data && !ifmgd->auth_data->done) {
  3241. err = -EBUSY;
  3242. goto err_free;
  3243. }
  3244. if (ifmgd->assoc_data) {
  3245. err = -EBUSY;
  3246. goto err_free;
  3247. }
  3248. if (ifmgd->auth_data) {
  3249. bool match;
  3250. /* keep sta info, bssid if matching */
  3251. match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
  3252. ieee80211_destroy_auth_data(sdata, match);
  3253. }
  3254. /* prepare assoc data */
  3255. ifmgd->beacon_crc_valid = false;
  3256. /*
  3257. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  3258. * We still associate in non-HT mode (11a/b/g) if any one of these
  3259. * ciphers is configured as pairwise.
  3260. * We can set this to true for non-11n hardware, that'll be checked
  3261. * separately along with the peer capabilities.
  3262. */
  3263. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
  3264. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  3265. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  3266. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
  3267. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3268. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3269. netdev_info(sdata->dev,
  3270. "disabling HT/VHT due to WEP/TKIP use\n");
  3271. }
  3272. }
  3273. if (req->flags & ASSOC_REQ_DISABLE_HT) {
  3274. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3275. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3276. }
  3277. /* Also disable HT if we don't support it or the AP doesn't use WMM */
  3278. sband = local->hw.wiphy->bands[req->bss->channel->band];
  3279. if (!sband->ht_cap.ht_supported ||
  3280. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
  3281. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3282. if (!bss->wmm_used)
  3283. netdev_info(sdata->dev,
  3284. "disabling HT as WMM/QoS is not supported by the AP\n");
  3285. }
  3286. /* disable VHT if we don't support it or the AP doesn't use WMM */
  3287. if (!sband->vht_cap.vht_supported ||
  3288. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
  3289. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3290. if (!bss->wmm_used)
  3291. netdev_info(sdata->dev,
  3292. "disabling VHT as WMM/QoS is not supported by the AP\n");
  3293. }
  3294. memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
  3295. memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
  3296. sizeof(ifmgd->ht_capa_mask));
  3297. if (req->ie && req->ie_len) {
  3298. memcpy(assoc_data->ie, req->ie, req->ie_len);
  3299. assoc_data->ie_len = req->ie_len;
  3300. }
  3301. assoc_data->bss = req->bss;
  3302. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  3303. if (ifmgd->powersave)
  3304. sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
  3305. else
  3306. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3307. } else
  3308. sdata->smps_mode = ifmgd->req_smps;
  3309. assoc_data->capability = req->bss->capability;
  3310. assoc_data->wmm = bss->wmm_used &&
  3311. (local->hw.queues >= IEEE80211_NUM_ACS);
  3312. assoc_data->supp_rates = bss->supp_rates;
  3313. assoc_data->supp_rates_len = bss->supp_rates_len;
  3314. rcu_read_lock();
  3315. ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
  3316. if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
  3317. assoc_data->ap_ht_param =
  3318. ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
  3319. else
  3320. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3321. vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
  3322. if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
  3323. memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
  3324. sizeof(struct ieee80211_vht_cap));
  3325. else
  3326. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3327. rcu_read_unlock();
  3328. if (bss->wmm_used && bss->uapsd_supported &&
  3329. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
  3330. assoc_data->uapsd = true;
  3331. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  3332. } else {
  3333. assoc_data->uapsd = false;
  3334. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  3335. }
  3336. if (req->prev_bssid)
  3337. memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
  3338. if (req->use_mfp) {
  3339. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  3340. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  3341. } else {
  3342. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  3343. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  3344. }
  3345. if (req->crypto.control_port)
  3346. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  3347. else
  3348. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  3349. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  3350. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  3351. /* kick off associate process */
  3352. ifmgd->assoc_data = assoc_data;
  3353. ifmgd->dtim_period = 0;
  3354. err = ieee80211_prep_connection(sdata, req->bss, true);
  3355. if (err)
  3356. goto err_clear;
  3357. rcu_read_lock();
  3358. beacon_ies = rcu_dereference(req->bss->beacon_ies);
  3359. if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
  3360. !beacon_ies) {
  3361. /*
  3362. * Wait up to one beacon interval ...
  3363. * should this be more if we miss one?
  3364. */
  3365. sdata_info(sdata, "waiting for beacon from %pM\n",
  3366. ifmgd->bssid);
  3367. assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
  3368. assoc_data->need_beacon = true;
  3369. } else if (beacon_ies) {
  3370. const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3371. beacon_ies->data,
  3372. beacon_ies->len);
  3373. u8 dtim_count = 0;
  3374. if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
  3375. const struct ieee80211_tim_ie *tim;
  3376. tim = (void *)(tim_ie + 2);
  3377. ifmgd->dtim_period = tim->dtim_period;
  3378. dtim_count = tim->dtim_count;
  3379. }
  3380. assoc_data->have_beacon = true;
  3381. assoc_data->timeout = jiffies;
  3382. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  3383. sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
  3384. sdata->vif.bss_conf.sync_device_ts =
  3385. bss->device_ts_beacon;
  3386. sdata->vif.bss_conf.sync_dtim_count = dtim_count;
  3387. }
  3388. } else {
  3389. assoc_data->timeout = jiffies;
  3390. }
  3391. rcu_read_unlock();
  3392. run_again(ifmgd, assoc_data->timeout);
  3393. if (bss->corrupt_data) {
  3394. char *corrupt_type = "data";
  3395. if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
  3396. if (bss->corrupt_data &
  3397. IEEE80211_BSS_CORRUPT_PROBE_RESP)
  3398. corrupt_type = "beacon and probe response";
  3399. else
  3400. corrupt_type = "beacon";
  3401. } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
  3402. corrupt_type = "probe response";
  3403. sdata_info(sdata, "associating with AP with corrupt %s\n",
  3404. corrupt_type);
  3405. }
  3406. err = 0;
  3407. goto out;
  3408. err_clear:
  3409. memset(ifmgd->bssid, 0, ETH_ALEN);
  3410. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3411. ifmgd->assoc_data = NULL;
  3412. err_free:
  3413. kfree(assoc_data);
  3414. out:
  3415. mutex_unlock(&ifmgd->mtx);
  3416. return err;
  3417. }
  3418. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  3419. struct cfg80211_deauth_request *req)
  3420. {
  3421. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3422. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3423. bool tx = !req->local_state_change;
  3424. bool sent_frame = false;
  3425. mutex_lock(&ifmgd->mtx);
  3426. sdata_info(sdata,
  3427. "deauthenticating from %pM by local choice (reason=%d)\n",
  3428. req->bssid, req->reason_code);
  3429. if (ifmgd->auth_data) {
  3430. drv_mgd_prepare_tx(sdata->local, sdata);
  3431. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  3432. IEEE80211_STYPE_DEAUTH,
  3433. req->reason_code, tx,
  3434. frame_buf);
  3435. ieee80211_destroy_auth_data(sdata, false);
  3436. mutex_unlock(&ifmgd->mtx);
  3437. sent_frame = tx;
  3438. goto out;
  3439. }
  3440. if (ifmgd->associated &&
  3441. ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
  3442. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3443. req->reason_code, tx, frame_buf);
  3444. sent_frame = tx;
  3445. }
  3446. mutex_unlock(&ifmgd->mtx);
  3447. out:
  3448. if (sent_frame)
  3449. __cfg80211_send_deauth(sdata->dev, frame_buf,
  3450. IEEE80211_DEAUTH_FRAME_LEN);
  3451. return 0;
  3452. }
  3453. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  3454. struct cfg80211_disassoc_request *req)
  3455. {
  3456. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3457. u8 bssid[ETH_ALEN];
  3458. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3459. mutex_lock(&ifmgd->mtx);
  3460. /*
  3461. * cfg80211 should catch this ... but it's racy since
  3462. * we can receive a disassoc frame, process it, hand it
  3463. * to cfg80211 while that's in a locked section already
  3464. * trying to tell us that the user wants to disconnect.
  3465. */
  3466. if (ifmgd->associated != req->bss) {
  3467. mutex_unlock(&ifmgd->mtx);
  3468. return -ENOLINK;
  3469. }
  3470. sdata_info(sdata,
  3471. "disassociating from %pM by local choice (reason=%d)\n",
  3472. req->bss->bssid, req->reason_code);
  3473. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  3474. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
  3475. req->reason_code, !req->local_state_change,
  3476. frame_buf);
  3477. mutex_unlock(&ifmgd->mtx);
  3478. __cfg80211_send_disassoc(sdata->dev, frame_buf,
  3479. IEEE80211_DEAUTH_FRAME_LEN);
  3480. return 0;
  3481. }
  3482. void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
  3483. {
  3484. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3485. mutex_lock(&ifmgd->mtx);
  3486. if (ifmgd->assoc_data)
  3487. ieee80211_destroy_assoc_data(sdata, false);
  3488. if (ifmgd->auth_data)
  3489. ieee80211_destroy_auth_data(sdata, false);
  3490. del_timer_sync(&ifmgd->timer);
  3491. mutex_unlock(&ifmgd->mtx);
  3492. }
  3493. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  3494. enum nl80211_cqm_rssi_threshold_event rssi_event,
  3495. gfp_t gfp)
  3496. {
  3497. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  3498. trace_api_cqm_rssi_notify(sdata, rssi_event);
  3499. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  3500. }
  3501. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);