mlme.c 97 KB

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