mlme.c 99 KB

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