mlme.c 98 KB

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