mlme.c 121 KB

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