mlme.c 127 KB

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