mlme.c 100 KB

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