mlme.c 116 KB

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