mlme.c 98 KB

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