mlme.c 115 KB

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