mlme.c 98 KB

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