mlme.c 126 KB

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