mlme.c 129 KB

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