mlme.c 129 KB

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