mlme.c 98 KB

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