mlme.c 99 KB

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