mlme.c 99 KB

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