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