mlme.c 112 KB

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