mlme.c 126 KB

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