mlme.c 98 KB

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