mlme.c 104 KB

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