mlme.c 98 KB

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