mlme.c 100 KB

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