mlme.c 114 KB

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