mlme.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551
  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. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  1081. }
  1082. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  1083. u16 capab, bool erp_valid, u8 erp)
  1084. {
  1085. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1086. u32 changed = 0;
  1087. bool use_protection;
  1088. bool use_short_preamble;
  1089. bool use_short_slot;
  1090. if (erp_valid) {
  1091. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  1092. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  1093. } else {
  1094. use_protection = false;
  1095. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  1096. }
  1097. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1098. if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
  1099. use_short_slot = true;
  1100. if (use_protection != bss_conf->use_cts_prot) {
  1101. bss_conf->use_cts_prot = use_protection;
  1102. changed |= BSS_CHANGED_ERP_CTS_PROT;
  1103. }
  1104. if (use_short_preamble != bss_conf->use_short_preamble) {
  1105. bss_conf->use_short_preamble = use_short_preamble;
  1106. changed |= BSS_CHANGED_ERP_PREAMBLE;
  1107. }
  1108. if (use_short_slot != bss_conf->use_short_slot) {
  1109. bss_conf->use_short_slot = use_short_slot;
  1110. changed |= BSS_CHANGED_ERP_SLOT;
  1111. }
  1112. return changed;
  1113. }
  1114. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  1115. struct cfg80211_bss *cbss,
  1116. u32 bss_info_changed)
  1117. {
  1118. struct ieee80211_bss *bss = (void *)cbss->priv;
  1119. struct ieee80211_local *local = sdata->local;
  1120. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1121. bss_info_changed |= BSS_CHANGED_ASSOC;
  1122. /* set timing information */
  1123. bss_conf->beacon_int = cbss->beacon_interval;
  1124. bss_conf->timestamp = cbss->tsf;
  1125. bss_info_changed |= BSS_CHANGED_BEACON_INT;
  1126. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  1127. cbss->capability, bss->has_erp_value, bss->erp_value);
  1128. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  1129. IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
  1130. sdata->u.mgd.associated = cbss;
  1131. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  1132. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  1133. /* just to be sure */
  1134. sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1135. IEEE80211_STA_BEACON_POLL);
  1136. ieee80211_led_assoc(local, 1);
  1137. if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
  1138. bss_conf->dtim_period = bss->dtim_period;
  1139. else
  1140. bss_conf->dtim_period = 0;
  1141. bss_conf->assoc = 1;
  1142. /*
  1143. * For now just always ask the driver to update the basic rateset
  1144. * when we have associated, we aren't checking whether it actually
  1145. * changed or not.
  1146. */
  1147. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  1148. /* And the BSSID changed - we're associated now */
  1149. bss_info_changed |= BSS_CHANGED_BSSID;
  1150. /* Tell the driver to monitor connection quality (if supported) */
  1151. if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
  1152. bss_conf->cqm_rssi_thold)
  1153. bss_info_changed |= BSS_CHANGED_CQM;
  1154. /* Enable ARP filtering */
  1155. if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
  1156. bss_conf->arp_filter_enabled = sdata->arp_filter_state;
  1157. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  1158. }
  1159. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  1160. mutex_lock(&local->iflist_mtx);
  1161. ieee80211_recalc_ps(local, -1);
  1162. ieee80211_recalc_smps(local);
  1163. mutex_unlock(&local->iflist_mtx);
  1164. netif_tx_start_all_queues(sdata->dev);
  1165. netif_carrier_on(sdata->dev);
  1166. }
  1167. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  1168. bool remove_sta, bool tx)
  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, config_changed = 0;
  1174. u8 bssid[ETH_ALEN];
  1175. ASSERT_MGD_MTX(ifmgd);
  1176. if (WARN_ON(!ifmgd->associated))
  1177. return;
  1178. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  1179. ifmgd->associated = NULL;
  1180. memset(ifmgd->bssid, 0, ETH_ALEN);
  1181. /*
  1182. * we need to commit the associated = NULL change because the
  1183. * scan code uses that to determine whether this iface should
  1184. * go to/wake up from powersave or not -- and could otherwise
  1185. * wake the queues erroneously.
  1186. */
  1187. smp_mb();
  1188. /*
  1189. * Thus, we can only afterwards stop the queues -- to account
  1190. * for the case where another CPU is finishing a scan at this
  1191. * time -- we don't want the scan code to enable queues.
  1192. */
  1193. netif_tx_stop_all_queues(sdata->dev);
  1194. netif_carrier_off(sdata->dev);
  1195. mutex_lock(&local->sta_mtx);
  1196. sta = sta_info_get(sdata, bssid);
  1197. if (sta) {
  1198. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1199. ieee80211_sta_tear_down_BA_sessions(sta, tx);
  1200. }
  1201. mutex_unlock(&local->sta_mtx);
  1202. changed |= ieee80211_reset_erp_info(sdata);
  1203. ieee80211_led_assoc(local, 0);
  1204. changed |= BSS_CHANGED_ASSOC;
  1205. sdata->vif.bss_conf.assoc = false;
  1206. ieee80211_set_wmm_default(sdata);
  1207. /* channel(_type) changes are handled by ieee80211_hw_config */
  1208. WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
  1209. /* on the next assoc, re-program HT parameters */
  1210. sdata->ht_opmode_valid = false;
  1211. memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
  1212. memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
  1213. local->power_constr_level = 0;
  1214. del_timer_sync(&local->dynamic_ps_timer);
  1215. cancel_work_sync(&local->dynamic_ps_enable_work);
  1216. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1217. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1218. config_changed |= IEEE80211_CONF_CHANGE_PS;
  1219. }
  1220. local->ps_sdata = NULL;
  1221. ieee80211_hw_config(local, config_changed);
  1222. /* Disable ARP filtering */
  1223. if (sdata->vif.bss_conf.arp_filter_enabled) {
  1224. sdata->vif.bss_conf.arp_filter_enabled = false;
  1225. changed |= BSS_CHANGED_ARP_FILTER;
  1226. }
  1227. /* The BSSID (not really interesting) and HT changed */
  1228. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  1229. ieee80211_bss_info_change_notify(sdata, changed);
  1230. /* remove AP and TDLS peers */
  1231. if (remove_sta)
  1232. sta_info_flush(local, sdata);
  1233. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  1234. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  1235. del_timer_sync(&sdata->u.mgd.timer);
  1236. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  1237. }
  1238. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  1239. struct ieee80211_hdr *hdr)
  1240. {
  1241. /*
  1242. * We can postpone the mgd.timer whenever receiving unicast frames
  1243. * from AP because we know that the connection is working both ways
  1244. * at that time. But multicast frames (and hence also beacons) must
  1245. * be ignored here, because we need to trigger the timer during
  1246. * data idle periods for sending the periodic probe request to the
  1247. * AP we're connected to.
  1248. */
  1249. if (is_multicast_ether_addr(hdr->addr1))
  1250. return;
  1251. ieee80211_sta_reset_conn_monitor(sdata);
  1252. }
  1253. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  1254. {
  1255. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1256. if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1257. IEEE80211_STA_CONNECTION_POLL)))
  1258. return;
  1259. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1260. IEEE80211_STA_BEACON_POLL);
  1261. mutex_lock(&sdata->local->iflist_mtx);
  1262. ieee80211_recalc_ps(sdata->local, -1);
  1263. mutex_unlock(&sdata->local->iflist_mtx);
  1264. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  1265. return;
  1266. /*
  1267. * We've received a probe response, but are not sure whether
  1268. * we have or will be receiving any beacons or data, so let's
  1269. * schedule the timers again, just in case.
  1270. */
  1271. ieee80211_sta_reset_beacon_monitor(sdata);
  1272. mod_timer(&ifmgd->conn_mon_timer,
  1273. round_jiffies_up(jiffies +
  1274. IEEE80211_CONNECTION_IDLE_TIME));
  1275. }
  1276. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  1277. struct ieee80211_hdr *hdr, bool ack)
  1278. {
  1279. if (!ieee80211_is_data(hdr->frame_control))
  1280. return;
  1281. if (ack)
  1282. ieee80211_sta_reset_conn_monitor(sdata);
  1283. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  1284. sdata->u.mgd.probe_send_count > 0) {
  1285. if (ack)
  1286. sdata->u.mgd.probe_send_count = 0;
  1287. else
  1288. sdata->u.mgd.nullfunc_failed = true;
  1289. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1290. }
  1291. }
  1292. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  1293. {
  1294. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1295. const u8 *ssid;
  1296. u8 *dst = ifmgd->associated->bssid;
  1297. u8 unicast_limit = max(1, max_probe_tries - 3);
  1298. /*
  1299. * Try sending broadcast probe requests for the last three
  1300. * probe requests after the first ones failed since some
  1301. * buggy APs only support broadcast probe requests.
  1302. */
  1303. if (ifmgd->probe_send_count >= unicast_limit)
  1304. dst = NULL;
  1305. /*
  1306. * When the hardware reports an accurate Tx ACK status, it's
  1307. * better to send a nullfunc frame instead of a probe request,
  1308. * as it will kick us off the AP quickly if we aren't associated
  1309. * anymore. The timeout will be reset if the frame is ACKed by
  1310. * the AP.
  1311. */
  1312. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  1313. ifmgd->nullfunc_failed = false;
  1314. ieee80211_send_nullfunc(sdata->local, sdata, 0);
  1315. } else {
  1316. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1317. ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0,
  1318. (u32) -1, true, false);
  1319. }
  1320. ifmgd->probe_send_count++;
  1321. ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
  1322. run_again(ifmgd, ifmgd->probe_timeout);
  1323. }
  1324. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  1325. bool beacon)
  1326. {
  1327. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1328. bool already = false;
  1329. if (!ieee80211_sdata_running(sdata))
  1330. return;
  1331. if (sdata->local->scanning)
  1332. return;
  1333. if (sdata->local->tmp_channel)
  1334. return;
  1335. mutex_lock(&ifmgd->mtx);
  1336. if (!ifmgd->associated)
  1337. goto out;
  1338. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1339. if (beacon && net_ratelimit())
  1340. printk(KERN_DEBUG "%s: detected beacon loss from AP "
  1341. "- sending probe request\n", sdata->name);
  1342. #endif
  1343. /*
  1344. * The driver/our work has already reported this event or the
  1345. * connection monitoring has kicked in and we have already sent
  1346. * a probe request. Or maybe the AP died and the driver keeps
  1347. * reporting until we disassociate...
  1348. *
  1349. * In either case we have to ignore the current call to this
  1350. * function (except for setting the correct probe reason bit)
  1351. * because otherwise we would reset the timer every time and
  1352. * never check whether we received a probe response!
  1353. */
  1354. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1355. IEEE80211_STA_CONNECTION_POLL))
  1356. already = true;
  1357. if (beacon)
  1358. ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
  1359. else
  1360. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  1361. if (already)
  1362. goto out;
  1363. mutex_lock(&sdata->local->iflist_mtx);
  1364. ieee80211_recalc_ps(sdata->local, -1);
  1365. mutex_unlock(&sdata->local->iflist_mtx);
  1366. ifmgd->probe_send_count = 0;
  1367. ieee80211_mgd_probe_ap_send(sdata);
  1368. out:
  1369. mutex_unlock(&ifmgd->mtx);
  1370. }
  1371. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  1372. struct ieee80211_vif *vif)
  1373. {
  1374. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1375. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1376. struct sk_buff *skb;
  1377. const u8 *ssid;
  1378. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1379. return NULL;
  1380. ASSERT_MGD_MTX(ifmgd);
  1381. if (!ifmgd->associated)
  1382. return NULL;
  1383. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1384. skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
  1385. (u32) -1, ssid + 2, ssid[1],
  1386. NULL, 0, true);
  1387. return skb;
  1388. }
  1389. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  1390. static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
  1391. {
  1392. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1393. struct ieee80211_local *local = sdata->local;
  1394. u8 bssid[ETH_ALEN];
  1395. u8 frame_buf[DEAUTH_DISASSOC_LEN];
  1396. mutex_lock(&ifmgd->mtx);
  1397. if (!ifmgd->associated) {
  1398. mutex_unlock(&ifmgd->mtx);
  1399. return;
  1400. }
  1401. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  1402. printk(KERN_DEBUG "%s: Connection to AP %pM lost.\n",
  1403. sdata->name, bssid);
  1404. ieee80211_set_disassoc(sdata, true, true);
  1405. mutex_unlock(&ifmgd->mtx);
  1406. /*
  1407. * must be outside lock due to cfg80211,
  1408. * but that's not a problem.
  1409. */
  1410. ieee80211_send_deauth_disassoc(sdata, bssid,
  1411. IEEE80211_STYPE_DEAUTH,
  1412. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1413. false, frame_buf);
  1414. cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
  1415. mutex_lock(&local->mtx);
  1416. ieee80211_recalc_idle(local);
  1417. mutex_unlock(&local->mtx);
  1418. }
  1419. void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  1420. {
  1421. struct ieee80211_sub_if_data *sdata =
  1422. container_of(work, struct ieee80211_sub_if_data,
  1423. u.mgd.beacon_connection_loss_work);
  1424. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1425. struct sta_info *sta;
  1426. if (ifmgd->associated) {
  1427. rcu_read_lock();
  1428. sta = sta_info_get(sdata, ifmgd->bssid);
  1429. if (sta)
  1430. sta->beacon_loss_count++;
  1431. rcu_read_unlock();
  1432. }
  1433. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  1434. __ieee80211_connection_loss(sdata);
  1435. else
  1436. ieee80211_mgd_probe_ap(sdata, true);
  1437. }
  1438. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  1439. {
  1440. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1441. struct ieee80211_hw *hw = &sdata->local->hw;
  1442. trace_api_beacon_loss(sdata);
  1443. WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
  1444. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1445. }
  1446. EXPORT_SYMBOL(ieee80211_beacon_loss);
  1447. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  1448. {
  1449. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1450. struct ieee80211_hw *hw = &sdata->local->hw;
  1451. trace_api_connection_loss(sdata);
  1452. WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
  1453. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1454. }
  1455. EXPORT_SYMBOL(ieee80211_connection_loss);
  1456. static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
  1457. bool assoc)
  1458. {
  1459. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1460. lockdep_assert_held(&sdata->u.mgd.mtx);
  1461. if (auth_data->synced)
  1462. drv_finish_tx_sync(sdata->local, sdata,
  1463. auth_data->bss->bssid,
  1464. IEEE80211_TX_SYNC_AUTH);
  1465. if (!assoc) {
  1466. sta_info_destroy_addr(sdata, auth_data->bss->bssid);
  1467. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1468. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1469. }
  1470. cfg80211_put_bss(auth_data->bss);
  1471. kfree(auth_data);
  1472. sdata->u.mgd.auth_data = NULL;
  1473. }
  1474. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  1475. struct ieee80211_mgmt *mgmt, size_t len)
  1476. {
  1477. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1478. u8 *pos;
  1479. struct ieee802_11_elems elems;
  1480. pos = mgmt->u.auth.variable;
  1481. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1482. if (!elems.challenge)
  1483. return;
  1484. auth_data->expected_transaction = 4;
  1485. ieee80211_send_auth(sdata, 3, auth_data->algorithm,
  1486. elems.challenge - 2, elems.challenge_len + 2,
  1487. auth_data->bss->bssid, auth_data->bss->bssid,
  1488. auth_data->key, auth_data->key_len,
  1489. auth_data->key_idx);
  1490. }
  1491. static enum rx_mgmt_action __must_check
  1492. ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  1493. struct ieee80211_mgmt *mgmt, size_t len)
  1494. {
  1495. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1496. u8 bssid[ETH_ALEN];
  1497. u16 auth_alg, auth_transaction, status_code;
  1498. struct sta_info *sta;
  1499. lockdep_assert_held(&ifmgd->mtx);
  1500. if (len < 24 + 6)
  1501. return RX_MGMT_NONE;
  1502. if (!ifmgd->auth_data || ifmgd->auth_data->done)
  1503. return RX_MGMT_NONE;
  1504. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  1505. if (memcmp(bssid, mgmt->bssid, ETH_ALEN))
  1506. return RX_MGMT_NONE;
  1507. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  1508. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  1509. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  1510. if (auth_alg != ifmgd->auth_data->algorithm ||
  1511. auth_transaction != ifmgd->auth_data->expected_transaction)
  1512. return RX_MGMT_NONE;
  1513. if (status_code != WLAN_STATUS_SUCCESS) {
  1514. printk(KERN_DEBUG "%s: %pM denied authentication (status %d)\n",
  1515. sdata->name, mgmt->sa, status_code);
  1516. goto out;
  1517. }
  1518. switch (ifmgd->auth_data->algorithm) {
  1519. case WLAN_AUTH_OPEN:
  1520. case WLAN_AUTH_LEAP:
  1521. case WLAN_AUTH_FT:
  1522. break;
  1523. case WLAN_AUTH_SHARED_KEY:
  1524. if (ifmgd->auth_data->expected_transaction != 4) {
  1525. ieee80211_auth_challenge(sdata, mgmt, len);
  1526. /* need another frame */
  1527. return RX_MGMT_NONE;
  1528. }
  1529. break;
  1530. default:
  1531. WARN_ONCE(1, "invalid auth alg %d",
  1532. ifmgd->auth_data->algorithm);
  1533. return RX_MGMT_NONE;
  1534. }
  1535. printk(KERN_DEBUG "%s: authenticated\n", sdata->name);
  1536. out:
  1537. if (ifmgd->auth_data->synced)
  1538. drv_finish_tx_sync(sdata->local, sdata, bssid,
  1539. IEEE80211_TX_SYNC_AUTH);
  1540. ifmgd->auth_data->synced = false;
  1541. ifmgd->auth_data->done = true;
  1542. ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
  1543. run_again(ifmgd, ifmgd->auth_data->timeout);
  1544. /* move station state to auth */
  1545. mutex_lock(&sdata->local->sta_mtx);
  1546. sta = sta_info_get(sdata, bssid);
  1547. if (!sta) {
  1548. WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
  1549. goto out_err;
  1550. }
  1551. if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
  1552. printk(KERN_DEBUG "%s: failed moving %pM to auth\n",
  1553. sdata->name, bssid);
  1554. goto out_err;
  1555. }
  1556. mutex_unlock(&sdata->local->sta_mtx);
  1557. return RX_MGMT_CFG80211_RX_AUTH;
  1558. out_err:
  1559. mutex_unlock(&sdata->local->sta_mtx);
  1560. /* ignore frame -- wait for timeout */
  1561. return RX_MGMT_NONE;
  1562. }
  1563. static enum rx_mgmt_action __must_check
  1564. ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1565. struct ieee80211_mgmt *mgmt, size_t len)
  1566. {
  1567. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1568. const u8 *bssid = NULL;
  1569. u16 reason_code;
  1570. lockdep_assert_held(&ifmgd->mtx);
  1571. if (len < 24 + 2)
  1572. return RX_MGMT_NONE;
  1573. if (!ifmgd->associated ||
  1574. memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN))
  1575. return RX_MGMT_NONE;
  1576. bssid = ifmgd->associated->bssid;
  1577. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1578. printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
  1579. sdata->name, bssid, reason_code);
  1580. ieee80211_set_disassoc(sdata, true, false);
  1581. mutex_lock(&sdata->local->mtx);
  1582. ieee80211_recalc_idle(sdata->local);
  1583. mutex_unlock(&sdata->local->mtx);
  1584. return RX_MGMT_CFG80211_DEAUTH;
  1585. }
  1586. static enum rx_mgmt_action __must_check
  1587. ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1588. struct ieee80211_mgmt *mgmt, size_t len)
  1589. {
  1590. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1591. u16 reason_code;
  1592. lockdep_assert_held(&ifmgd->mtx);
  1593. if (len < 24 + 2)
  1594. return RX_MGMT_NONE;
  1595. if (!ifmgd->associated ||
  1596. memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN))
  1597. return RX_MGMT_NONE;
  1598. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1599. printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
  1600. sdata->name, mgmt->sa, reason_code);
  1601. ieee80211_set_disassoc(sdata, true, false);
  1602. mutex_lock(&sdata->local->mtx);
  1603. ieee80211_recalc_idle(sdata->local);
  1604. mutex_unlock(&sdata->local->mtx);
  1605. return RX_MGMT_CFG80211_DISASSOC;
  1606. }
  1607. static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  1608. u8 *supp_rates, unsigned int supp_rates_len,
  1609. u32 *rates, u32 *basic_rates,
  1610. bool *have_higher_than_11mbit,
  1611. int *min_rate, int *min_rate_index)
  1612. {
  1613. int i, j;
  1614. for (i = 0; i < supp_rates_len; i++) {
  1615. int rate = (supp_rates[i] & 0x7f) * 5;
  1616. bool is_basic = !!(supp_rates[i] & 0x80);
  1617. if (rate > 110)
  1618. *have_higher_than_11mbit = true;
  1619. /*
  1620. * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
  1621. * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
  1622. *
  1623. * Note: Even through the membership selector and the basic
  1624. * rate flag share the same bit, they are not exactly
  1625. * the same.
  1626. */
  1627. if (!!(supp_rates[i] & 0x80) &&
  1628. (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1629. continue;
  1630. for (j = 0; j < sband->n_bitrates; j++) {
  1631. if (sband->bitrates[j].bitrate == rate) {
  1632. *rates |= BIT(j);
  1633. if (is_basic)
  1634. *basic_rates |= BIT(j);
  1635. if (rate < *min_rate) {
  1636. *min_rate = rate;
  1637. *min_rate_index = j;
  1638. }
  1639. break;
  1640. }
  1641. }
  1642. }
  1643. }
  1644. static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  1645. bool assoc)
  1646. {
  1647. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  1648. lockdep_assert_held(&sdata->u.mgd.mtx);
  1649. if (assoc_data->synced)
  1650. drv_finish_tx_sync(sdata->local, sdata,
  1651. assoc_data->bss->bssid,
  1652. IEEE80211_TX_SYNC_ASSOC);
  1653. if (!assoc) {
  1654. sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
  1655. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1656. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1657. }
  1658. kfree(assoc_data);
  1659. sdata->u.mgd.assoc_data = NULL;
  1660. }
  1661. static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
  1662. struct cfg80211_bss *cbss,
  1663. struct ieee80211_mgmt *mgmt, size_t len)
  1664. {
  1665. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1666. struct ieee80211_local *local = sdata->local;
  1667. struct ieee80211_supported_band *sband;
  1668. struct sta_info *sta;
  1669. u8 *pos;
  1670. u32 rates, basic_rates;
  1671. u16 capab_info, aid;
  1672. struct ieee802_11_elems elems;
  1673. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1674. u32 changed = 0;
  1675. int err;
  1676. bool have_higher_than_11mbit = false;
  1677. u16 ap_ht_cap_flags;
  1678. int min_rate = INT_MAX, min_rate_index = -1;
  1679. /* AssocResp and ReassocResp have identical structure */
  1680. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1681. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1682. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1683. printk(KERN_DEBUG
  1684. "%s: invalid AID value 0x%x; bits 15:14 not set\n",
  1685. sdata->name, aid);
  1686. aid &= ~(BIT(15) | BIT(14));
  1687. ifmgd->broken_ap = false;
  1688. if (aid == 0 || aid > IEEE80211_MAX_AID) {
  1689. printk(KERN_DEBUG
  1690. "%s: invalid AID value %d (out of range), turn off PS\n",
  1691. sdata->name, aid);
  1692. aid = 0;
  1693. ifmgd->broken_ap = true;
  1694. }
  1695. pos = mgmt->u.assoc_resp.variable;
  1696. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1697. if (!elems.supp_rates) {
  1698. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  1699. sdata->name);
  1700. return false;
  1701. }
  1702. ifmgd->aid = aid;
  1703. mutex_lock(&sdata->local->sta_mtx);
  1704. /*
  1705. * station info was already allocated and inserted before
  1706. * the association and should be available to us
  1707. */
  1708. sta = sta_info_get(sdata, cbss->bssid);
  1709. if (WARN_ON(!sta)) {
  1710. mutex_unlock(&sdata->local->sta_mtx);
  1711. return false;
  1712. }
  1713. rates = 0;
  1714. basic_rates = 0;
  1715. sband = local->hw.wiphy->bands[local->oper_channel->band];
  1716. ieee80211_get_rates(sband, elems.supp_rates, elems.supp_rates_len,
  1717. &rates, &basic_rates, &have_higher_than_11mbit,
  1718. &min_rate, &min_rate_index);
  1719. ieee80211_get_rates(sband, elems.ext_supp_rates,
  1720. elems.ext_supp_rates_len, &rates, &basic_rates,
  1721. &have_higher_than_11mbit,
  1722. &min_rate, &min_rate_index);
  1723. /*
  1724. * some buggy APs don't advertise basic_rates. use the lowest
  1725. * supported rate instead.
  1726. */
  1727. if (unlikely(!basic_rates) && min_rate_index >= 0) {
  1728. printk(KERN_DEBUG "%s: No basic rates in AssocResp. "
  1729. "Using min supported rate instead.\n", sdata->name);
  1730. basic_rates = BIT(min_rate_index);
  1731. }
  1732. sta->sta.supp_rates[local->oper_channel->band] = rates;
  1733. sdata->vif.bss_conf.basic_rates = basic_rates;
  1734. /* cf. IEEE 802.11 9.2.12 */
  1735. if (local->oper_channel->band == IEEE80211_BAND_2GHZ &&
  1736. have_higher_than_11mbit)
  1737. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  1738. else
  1739. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  1740. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1741. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  1742. elems.ht_cap_elem, &sta->sta.ht_cap);
  1743. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1744. rate_control_rate_init(sta);
  1745. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  1746. set_sta_flag(sta, WLAN_STA_MFP);
  1747. if (elems.wmm_param)
  1748. set_sta_flag(sta, WLAN_STA_WME);
  1749. err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
  1750. if (!err)
  1751. err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  1752. if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  1753. err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  1754. if (err) {
  1755. printk(KERN_DEBUG
  1756. "%s: failed to move station %pM to desired state\n",
  1757. sdata->name, sta->sta.addr);
  1758. WARN_ON(__sta_info_destroy(sta));
  1759. mutex_unlock(&sdata->local->sta_mtx);
  1760. return false;
  1761. }
  1762. mutex_unlock(&sdata->local->sta_mtx);
  1763. /*
  1764. * Always handle WMM once after association regardless
  1765. * of the first value the AP uses. Setting -1 here has
  1766. * that effect because the AP values is an unsigned
  1767. * 4-bit value.
  1768. */
  1769. ifmgd->wmm_last_param_set = -1;
  1770. if (elems.wmm_param)
  1771. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  1772. elems.wmm_param_len);
  1773. else
  1774. ieee80211_set_wmm_default(sdata);
  1775. if (elems.ht_info_elem && elems.wmm_param &&
  1776. (sdata->local->hw.queues >= 4) &&
  1777. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1778. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1779. cbss->bssid, ap_ht_cap_flags,
  1780. false);
  1781. /* set AID and assoc capability,
  1782. * ieee80211_set_associated() will tell the driver */
  1783. bss_conf->aid = aid;
  1784. bss_conf->assoc_capability = capab_info;
  1785. ieee80211_set_associated(sdata, cbss, changed);
  1786. /*
  1787. * If we're using 4-addr mode, let the AP know that we're
  1788. * doing so, so that it can create the STA VLAN on its side
  1789. */
  1790. if (ifmgd->use_4addr)
  1791. ieee80211_send_4addr_nullfunc(local, sdata);
  1792. /*
  1793. * Start timer to probe the connection to the AP now.
  1794. * Also start the timer that will detect beacon loss.
  1795. */
  1796. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  1797. ieee80211_sta_reset_beacon_monitor(sdata);
  1798. return true;
  1799. }
  1800. static enum rx_mgmt_action __must_check
  1801. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  1802. struct ieee80211_mgmt *mgmt, size_t len,
  1803. struct cfg80211_bss **bss)
  1804. {
  1805. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1806. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  1807. u16 capab_info, status_code, aid;
  1808. struct ieee802_11_elems elems;
  1809. u8 *pos;
  1810. bool reassoc;
  1811. lockdep_assert_held(&ifmgd->mtx);
  1812. if (!assoc_data)
  1813. return RX_MGMT_NONE;
  1814. if (memcmp(assoc_data->bss->bssid, mgmt->bssid, ETH_ALEN))
  1815. return RX_MGMT_NONE;
  1816. /*
  1817. * AssocResp and ReassocResp have identical structure, so process both
  1818. * of them in this function.
  1819. */
  1820. if (len < 24 + 6)
  1821. return RX_MGMT_NONE;
  1822. reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
  1823. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1824. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1825. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1826. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  1827. "status=%d aid=%d)\n",
  1828. sdata->name, reassoc ? "Rea" : "A", mgmt->sa,
  1829. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1830. pos = mgmt->u.assoc_resp.variable;
  1831. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1832. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  1833. elems.timeout_int && elems.timeout_int_len == 5 &&
  1834. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  1835. u32 tu, ms;
  1836. tu = get_unaligned_le32(elems.timeout_int + 1);
  1837. ms = tu * 1024 / 1000;
  1838. printk(KERN_DEBUG "%s: %pM rejected association temporarily; "
  1839. "comeback duration %u TU (%u ms)\n",
  1840. sdata->name, mgmt->sa, tu, ms);
  1841. assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
  1842. if (ms > IEEE80211_ASSOC_TIMEOUT)
  1843. run_again(ifmgd, assoc_data->timeout);
  1844. return RX_MGMT_NONE;
  1845. }
  1846. *bss = assoc_data->bss;
  1847. if (status_code != WLAN_STATUS_SUCCESS) {
  1848. printk(KERN_DEBUG "%s: %pM denied association (code=%d)\n",
  1849. sdata->name, mgmt->sa, status_code);
  1850. ieee80211_destroy_assoc_data(sdata, false);
  1851. } else {
  1852. printk(KERN_DEBUG "%s: associated\n", sdata->name);
  1853. /* tell driver about sync done first */
  1854. if (assoc_data->synced) {
  1855. drv_finish_tx_sync(sdata->local, sdata,
  1856. assoc_data->bss->bssid,
  1857. IEEE80211_TX_SYNC_ASSOC);
  1858. assoc_data->synced = false;
  1859. }
  1860. if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
  1861. /* oops -- internal error -- send timeout for now */
  1862. ieee80211_destroy_assoc_data(sdata, true);
  1863. sta_info_destroy_addr(sdata, mgmt->bssid);
  1864. cfg80211_put_bss(*bss);
  1865. return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
  1866. }
  1867. /*
  1868. * destroy assoc_data afterwards, as otherwise an idle
  1869. * recalc after assoc_data is NULL but before associated
  1870. * is set can cause the interface to go idle
  1871. */
  1872. ieee80211_destroy_assoc_data(sdata, true);
  1873. }
  1874. return RX_MGMT_CFG80211_RX_ASSOC;
  1875. }
  1876. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1877. struct ieee80211_mgmt *mgmt,
  1878. size_t len,
  1879. struct ieee80211_rx_status *rx_status,
  1880. struct ieee802_11_elems *elems,
  1881. bool beacon)
  1882. {
  1883. struct ieee80211_local *local = sdata->local;
  1884. int freq;
  1885. struct ieee80211_bss *bss;
  1886. struct ieee80211_channel *channel;
  1887. bool need_ps = false;
  1888. if (sdata->u.mgd.associated &&
  1889. memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
  1890. ETH_ALEN) == 0) {
  1891. bss = (void *)sdata->u.mgd.associated->priv;
  1892. /* not previously set so we may need to recalc */
  1893. need_ps = !bss->dtim_period;
  1894. }
  1895. if (elems->ds_params && elems->ds_params_len == 1)
  1896. freq = ieee80211_channel_to_frequency(elems->ds_params[0],
  1897. rx_status->band);
  1898. else
  1899. freq = rx_status->freq;
  1900. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1901. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1902. return;
  1903. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1904. channel, beacon);
  1905. if (bss)
  1906. ieee80211_rx_bss_put(local, bss);
  1907. if (!sdata->u.mgd.associated)
  1908. return;
  1909. if (need_ps) {
  1910. mutex_lock(&local->iflist_mtx);
  1911. ieee80211_recalc_ps(local, -1);
  1912. mutex_unlock(&local->iflist_mtx);
  1913. }
  1914. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  1915. (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
  1916. ETH_ALEN) == 0)) {
  1917. struct ieee80211_channel_sw_ie *sw_elem =
  1918. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  1919. ieee80211_sta_process_chanswitch(sdata, sw_elem,
  1920. bss, rx_status->mactime);
  1921. }
  1922. }
  1923. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1924. struct sk_buff *skb)
  1925. {
  1926. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1927. struct ieee80211_if_managed *ifmgd;
  1928. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  1929. size_t baselen, len = skb->len;
  1930. struct ieee802_11_elems elems;
  1931. ifmgd = &sdata->u.mgd;
  1932. ASSERT_MGD_MTX(ifmgd);
  1933. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  1934. return; /* ignore ProbeResp to foreign address */
  1935. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1936. if (baselen > len)
  1937. return;
  1938. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1939. &elems);
  1940. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1941. if (ifmgd->associated &&
  1942. memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0)
  1943. ieee80211_reset_ap_probe(sdata);
  1944. if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
  1945. memcmp(mgmt->bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN) == 0) {
  1946. /* got probe response, continue with auth */
  1947. printk(KERN_DEBUG "%s: direct probe responded\n", sdata->name);
  1948. ifmgd->auth_data->tries = 0;
  1949. ifmgd->auth_data->timeout = jiffies;
  1950. run_again(ifmgd, ifmgd->auth_data->timeout);
  1951. }
  1952. }
  1953. /*
  1954. * This is the canonical list of information elements we care about,
  1955. * the filter code also gives us all changes to the Microsoft OUI
  1956. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  1957. *
  1958. * We implement beacon filtering in software since that means we can
  1959. * avoid processing the frame here and in cfg80211, and userspace
  1960. * will not be able to tell whether the hardware supports it or not.
  1961. *
  1962. * XXX: This list needs to be dynamic -- userspace needs to be able to
  1963. * add items it requires. It also needs to be able to tell us to
  1964. * look out for other vendor IEs.
  1965. */
  1966. static const u64 care_about_ies =
  1967. (1ULL << WLAN_EID_COUNTRY) |
  1968. (1ULL << WLAN_EID_ERP_INFO) |
  1969. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  1970. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  1971. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1972. (1ULL << WLAN_EID_HT_INFORMATION);
  1973. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1974. struct ieee80211_mgmt *mgmt,
  1975. size_t len,
  1976. struct ieee80211_rx_status *rx_status)
  1977. {
  1978. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1979. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1980. size_t baselen;
  1981. struct ieee802_11_elems elems;
  1982. struct ieee80211_local *local = sdata->local;
  1983. u32 changed = 0;
  1984. bool erp_valid, directed_tim = false;
  1985. u8 erp_value = 0;
  1986. u32 ncrc;
  1987. u8 *bssid;
  1988. lockdep_assert_held(&ifmgd->mtx);
  1989. /* Process beacon from the current BSS */
  1990. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1991. if (baselen > len)
  1992. return;
  1993. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1994. return;
  1995. if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
  1996. memcmp(mgmt->bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN) == 0) {
  1997. ieee802_11_parse_elems(mgmt->u.beacon.variable,
  1998. len - baselen, &elems);
  1999. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  2000. false);
  2001. ifmgd->assoc_data->have_beacon = true;
  2002. ifmgd->assoc_data->sent_assoc = false;
  2003. /* continue assoc process */
  2004. ifmgd->assoc_data->timeout = jiffies;
  2005. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2006. return;
  2007. }
  2008. if (!ifmgd->associated ||
  2009. memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN))
  2010. return;
  2011. bssid = ifmgd->associated->bssid;
  2012. /* Track average RSSI from the Beacon frames of the current AP */
  2013. ifmgd->last_beacon_signal = rx_status->signal;
  2014. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  2015. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  2016. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  2017. ifmgd->last_cqm_event_signal = 0;
  2018. ifmgd->count_beacon_signal = 1;
  2019. ifmgd->last_ave_beacon_signal = 0;
  2020. } else {
  2021. ifmgd->ave_beacon_signal =
  2022. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  2023. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  2024. ifmgd->ave_beacon_signal) / 16;
  2025. ifmgd->count_beacon_signal++;
  2026. }
  2027. if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
  2028. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  2029. int sig = ifmgd->ave_beacon_signal;
  2030. int last_sig = ifmgd->last_ave_beacon_signal;
  2031. /*
  2032. * if signal crosses either of the boundaries, invoke callback
  2033. * with appropriate parameters
  2034. */
  2035. if (sig > ifmgd->rssi_max_thold &&
  2036. (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
  2037. ifmgd->last_ave_beacon_signal = sig;
  2038. drv_rssi_callback(local, RSSI_EVENT_HIGH);
  2039. } else if (sig < ifmgd->rssi_min_thold &&
  2040. (last_sig >= ifmgd->rssi_max_thold ||
  2041. last_sig == 0)) {
  2042. ifmgd->last_ave_beacon_signal = sig;
  2043. drv_rssi_callback(local, RSSI_EVENT_LOW);
  2044. }
  2045. }
  2046. if (bss_conf->cqm_rssi_thold &&
  2047. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  2048. !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
  2049. int sig = ifmgd->ave_beacon_signal / 16;
  2050. int last_event = ifmgd->last_cqm_event_signal;
  2051. int thold = bss_conf->cqm_rssi_thold;
  2052. int hyst = bss_conf->cqm_rssi_hyst;
  2053. if (sig < thold &&
  2054. (last_event == 0 || sig < last_event - hyst)) {
  2055. ifmgd->last_cqm_event_signal = sig;
  2056. ieee80211_cqm_rssi_notify(
  2057. &sdata->vif,
  2058. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  2059. GFP_KERNEL);
  2060. } else if (sig > thold &&
  2061. (last_event == 0 || sig > last_event + hyst)) {
  2062. ifmgd->last_cqm_event_signal = sig;
  2063. ieee80211_cqm_rssi_notify(
  2064. &sdata->vif,
  2065. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  2066. GFP_KERNEL);
  2067. }
  2068. }
  2069. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  2070. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2071. if (net_ratelimit()) {
  2072. printk(KERN_DEBUG "%s: cancelling probereq poll due "
  2073. "to a received beacon\n", sdata->name);
  2074. }
  2075. #endif
  2076. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  2077. mutex_lock(&local->iflist_mtx);
  2078. ieee80211_recalc_ps(local, -1);
  2079. mutex_unlock(&local->iflist_mtx);
  2080. }
  2081. /*
  2082. * Push the beacon loss detection into the future since
  2083. * we are processing a beacon from the AP just now.
  2084. */
  2085. ieee80211_sta_reset_beacon_monitor(sdata);
  2086. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  2087. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  2088. len - baselen, &elems,
  2089. care_about_ies, ncrc);
  2090. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  2091. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  2092. ifmgd->aid);
  2093. if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
  2094. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  2095. true);
  2096. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2097. elems.wmm_param_len);
  2098. }
  2099. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  2100. if (directed_tim) {
  2101. if (local->hw.conf.dynamic_ps_timeout > 0) {
  2102. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2103. ieee80211_hw_config(local,
  2104. IEEE80211_CONF_CHANGE_PS);
  2105. ieee80211_send_nullfunc(local, sdata, 0);
  2106. } else {
  2107. local->pspolling = true;
  2108. /*
  2109. * Here is assumed that the driver will be
  2110. * able to send ps-poll frame and receive a
  2111. * response even though power save mode is
  2112. * enabled, but some drivers might require
  2113. * to disable power save here. This needs
  2114. * to be investigated.
  2115. */
  2116. ieee80211_send_pspoll(local, sdata);
  2117. }
  2118. }
  2119. }
  2120. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  2121. return;
  2122. ifmgd->beacon_crc = ncrc;
  2123. ifmgd->beacon_crc_valid = true;
  2124. if (elems.erp_info && elems.erp_info_len >= 1) {
  2125. erp_valid = true;
  2126. erp_value = elems.erp_info[0];
  2127. } else {
  2128. erp_valid = false;
  2129. }
  2130. changed |= ieee80211_handle_bss_capability(sdata,
  2131. le16_to_cpu(mgmt->u.beacon.capab_info),
  2132. erp_valid, erp_value);
  2133. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  2134. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
  2135. struct sta_info *sta;
  2136. struct ieee80211_supported_band *sband;
  2137. u16 ap_ht_cap_flags;
  2138. rcu_read_lock();
  2139. sta = sta_info_get(sdata, bssid);
  2140. if (WARN_ON(!sta)) {
  2141. rcu_read_unlock();
  2142. return;
  2143. }
  2144. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  2145. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  2146. elems.ht_cap_elem, &sta->sta.ht_cap);
  2147. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  2148. rcu_read_unlock();
  2149. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  2150. bssid, ap_ht_cap_flags, true);
  2151. }
  2152. /* Note: country IE parsing is done for us by cfg80211 */
  2153. if (elems.country_elem) {
  2154. /* TODO: IBSS also needs this */
  2155. if (elems.pwr_constr_elem)
  2156. ieee80211_handle_pwr_constr(sdata,
  2157. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  2158. elems.pwr_constr_elem,
  2159. elems.pwr_constr_elem_len);
  2160. }
  2161. ieee80211_bss_info_change_notify(sdata, changed);
  2162. }
  2163. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  2164. struct sk_buff *skb)
  2165. {
  2166. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2167. struct ieee80211_rx_status *rx_status;
  2168. struct ieee80211_mgmt *mgmt;
  2169. struct cfg80211_bss *bss = NULL;
  2170. enum rx_mgmt_action rma = RX_MGMT_NONE;
  2171. u16 fc;
  2172. rx_status = (struct ieee80211_rx_status *) skb->cb;
  2173. mgmt = (struct ieee80211_mgmt *) skb->data;
  2174. fc = le16_to_cpu(mgmt->frame_control);
  2175. mutex_lock(&ifmgd->mtx);
  2176. switch (fc & IEEE80211_FCTL_STYPE) {
  2177. case IEEE80211_STYPE_BEACON:
  2178. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  2179. break;
  2180. case IEEE80211_STYPE_PROBE_RESP:
  2181. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  2182. break;
  2183. case IEEE80211_STYPE_AUTH:
  2184. rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  2185. break;
  2186. case IEEE80211_STYPE_DEAUTH:
  2187. rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  2188. break;
  2189. case IEEE80211_STYPE_DISASSOC:
  2190. rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  2191. break;
  2192. case IEEE80211_STYPE_ASSOC_RESP:
  2193. case IEEE80211_STYPE_REASSOC_RESP:
  2194. rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
  2195. break;
  2196. case IEEE80211_STYPE_ACTION:
  2197. switch (mgmt->u.action.category) {
  2198. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2199. ieee80211_sta_process_chanswitch(sdata,
  2200. &mgmt->u.action.u.chan_switch.sw_elem,
  2201. (void *)ifmgd->associated->priv,
  2202. rx_status->mactime);
  2203. break;
  2204. }
  2205. }
  2206. mutex_unlock(&ifmgd->mtx);
  2207. switch (rma) {
  2208. case RX_MGMT_NONE:
  2209. /* no action */
  2210. break;
  2211. case RX_MGMT_CFG80211_DEAUTH:
  2212. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  2213. break;
  2214. case RX_MGMT_CFG80211_DISASSOC:
  2215. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  2216. break;
  2217. case RX_MGMT_CFG80211_RX_AUTH:
  2218. cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
  2219. break;
  2220. case RX_MGMT_CFG80211_RX_ASSOC:
  2221. cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
  2222. break;
  2223. case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
  2224. cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
  2225. break;
  2226. default:
  2227. WARN(1, "unexpected: %d", rma);
  2228. }
  2229. }
  2230. static void ieee80211_sta_timer(unsigned long data)
  2231. {
  2232. struct ieee80211_sub_if_data *sdata =
  2233. (struct ieee80211_sub_if_data *) data;
  2234. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2235. struct ieee80211_local *local = sdata->local;
  2236. if (local->quiescing) {
  2237. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  2238. return;
  2239. }
  2240. ieee80211_queue_work(&local->hw, &sdata->work);
  2241. }
  2242. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  2243. u8 *bssid, u8 reason)
  2244. {
  2245. struct ieee80211_local *local = sdata->local;
  2246. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2247. u8 frame_buf[DEAUTH_DISASSOC_LEN];
  2248. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  2249. IEEE80211_STA_BEACON_POLL);
  2250. ieee80211_set_disassoc(sdata, true, true);
  2251. mutex_unlock(&ifmgd->mtx);
  2252. /*
  2253. * must be outside lock due to cfg80211,
  2254. * but that's not a problem.
  2255. */
  2256. ieee80211_send_deauth_disassoc(sdata, bssid,
  2257. IEEE80211_STYPE_DEAUTH,
  2258. reason, false, frame_buf);
  2259. cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
  2260. mutex_lock(&local->mtx);
  2261. ieee80211_recalc_idle(local);
  2262. mutex_unlock(&local->mtx);
  2263. mutex_lock(&ifmgd->mtx);
  2264. }
  2265. static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
  2266. {
  2267. struct ieee80211_local *local = sdata->local;
  2268. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2269. struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
  2270. lockdep_assert_held(&ifmgd->mtx);
  2271. if (WARN_ON_ONCE(!auth_data))
  2272. return -EINVAL;
  2273. if (!auth_data->synced) {
  2274. int ret = drv_tx_sync(local, sdata, auth_data->bss->bssid,
  2275. IEEE80211_TX_SYNC_AUTH);
  2276. if (ret)
  2277. return ret;
  2278. }
  2279. auth_data->synced = true;
  2280. auth_data->tries++;
  2281. if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
  2282. printk(KERN_DEBUG "%s: authentication with %pM timed out\n",
  2283. sdata->name, auth_data->bss->bssid);
  2284. /*
  2285. * Most likely AP is not in the range so remove the
  2286. * bss struct for that AP.
  2287. */
  2288. cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
  2289. return -ETIMEDOUT;
  2290. }
  2291. if (auth_data->bss->proberesp_ies) {
  2292. printk(KERN_DEBUG "%s: send auth to %pM (try %d/%d)\n",
  2293. sdata->name, auth_data->bss->bssid, auth_data->tries,
  2294. IEEE80211_AUTH_MAX_TRIES);
  2295. auth_data->expected_transaction = 2;
  2296. ieee80211_send_auth(sdata, 1, auth_data->algorithm,
  2297. auth_data->ie, auth_data->ie_len,
  2298. auth_data->bss->bssid,
  2299. auth_data->bss->bssid, NULL, 0, 0);
  2300. } else {
  2301. const u8 *ssidie;
  2302. printk(KERN_DEBUG "%s: direct probe to %pM (try %d/%i)\n",
  2303. sdata->name, auth_data->bss->bssid, auth_data->tries,
  2304. IEEE80211_AUTH_MAX_TRIES);
  2305. ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
  2306. if (!ssidie)
  2307. return -EINVAL;
  2308. /*
  2309. * Direct probe is sent to broadcast address as some APs
  2310. * will not answer to direct packet in unassociated state.
  2311. */
  2312. ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
  2313. NULL, 0, (u32) -1, true, false);
  2314. }
  2315. auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  2316. run_again(ifmgd, auth_data->timeout);
  2317. return 0;
  2318. }
  2319. static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
  2320. {
  2321. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2322. struct ieee80211_local *local = sdata->local;
  2323. lockdep_assert_held(&sdata->u.mgd.mtx);
  2324. if (!assoc_data->synced) {
  2325. int ret = drv_tx_sync(local, sdata, assoc_data->bss->bssid,
  2326. IEEE80211_TX_SYNC_ASSOC);
  2327. if (ret)
  2328. return ret;
  2329. }
  2330. assoc_data->synced = true;
  2331. assoc_data->tries++;
  2332. if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
  2333. printk(KERN_DEBUG "%s: association with %pM timed out\n",
  2334. sdata->name, assoc_data->bss->bssid);
  2335. /*
  2336. * Most likely AP is not in the range so remove the
  2337. * bss struct for that AP.
  2338. */
  2339. cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
  2340. return -ETIMEDOUT;
  2341. }
  2342. printk(KERN_DEBUG "%s: associate with %pM (try %d/%d)\n",
  2343. sdata->name, assoc_data->bss->bssid, assoc_data->tries,
  2344. IEEE80211_ASSOC_MAX_TRIES);
  2345. ieee80211_send_assoc(sdata);
  2346. assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  2347. run_again(&sdata->u.mgd, assoc_data->timeout);
  2348. return 0;
  2349. }
  2350. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  2351. {
  2352. struct ieee80211_local *local = sdata->local;
  2353. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2354. mutex_lock(&ifmgd->mtx);
  2355. if (ifmgd->auth_data &&
  2356. time_after(jiffies, ifmgd->auth_data->timeout)) {
  2357. if (ifmgd->auth_data->done) {
  2358. /*
  2359. * ok ... we waited for assoc but userspace didn't,
  2360. * so let's just kill the auth data
  2361. */
  2362. ieee80211_destroy_auth_data(sdata, false);
  2363. } else if (ieee80211_probe_auth(sdata)) {
  2364. u8 bssid[ETH_ALEN];
  2365. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  2366. ieee80211_destroy_auth_data(sdata, false);
  2367. mutex_unlock(&ifmgd->mtx);
  2368. cfg80211_send_auth_timeout(sdata->dev, bssid);
  2369. mutex_lock(&ifmgd->mtx);
  2370. }
  2371. } else if (ifmgd->auth_data)
  2372. run_again(ifmgd, ifmgd->auth_data->timeout);
  2373. if (ifmgd->assoc_data &&
  2374. time_after(jiffies, ifmgd->assoc_data->timeout)) {
  2375. if (!ifmgd->assoc_data->have_beacon ||
  2376. ieee80211_do_assoc(sdata)) {
  2377. u8 bssid[ETH_ALEN];
  2378. memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
  2379. ieee80211_destroy_assoc_data(sdata, false);
  2380. mutex_unlock(&ifmgd->mtx);
  2381. cfg80211_send_assoc_timeout(sdata->dev, bssid);
  2382. mutex_lock(&ifmgd->mtx);
  2383. }
  2384. } else if (ifmgd->assoc_data)
  2385. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2386. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  2387. IEEE80211_STA_CONNECTION_POLL) &&
  2388. ifmgd->associated) {
  2389. u8 bssid[ETH_ALEN];
  2390. int max_tries;
  2391. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  2392. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2393. max_tries = max_nullfunc_tries;
  2394. else
  2395. max_tries = max_probe_tries;
  2396. /* ACK received for nullfunc probing frame */
  2397. if (!ifmgd->probe_send_count)
  2398. ieee80211_reset_ap_probe(sdata);
  2399. else if (ifmgd->nullfunc_failed) {
  2400. if (ifmgd->probe_send_count < max_tries) {
  2401. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2402. wiphy_debug(local->hw.wiphy,
  2403. "%s: No ack for nullfunc frame to"
  2404. " AP %pM, try %d/%i\n",
  2405. sdata->name, bssid,
  2406. ifmgd->probe_send_count, max_tries);
  2407. #endif
  2408. ieee80211_mgd_probe_ap_send(sdata);
  2409. } else {
  2410. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2411. wiphy_debug(local->hw.wiphy,
  2412. "%s: No ack for nullfunc frame to"
  2413. " AP %pM, disconnecting.\n",
  2414. sdata->name, bssid);
  2415. #endif
  2416. ieee80211_sta_connection_lost(sdata, bssid,
  2417. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2418. }
  2419. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  2420. run_again(ifmgd, ifmgd->probe_timeout);
  2421. else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  2422. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2423. wiphy_debug(local->hw.wiphy,
  2424. "%s: Failed to send nullfunc to AP %pM"
  2425. " after %dms, disconnecting.\n",
  2426. sdata->name,
  2427. bssid, probe_wait_ms);
  2428. #endif
  2429. ieee80211_sta_connection_lost(sdata, bssid,
  2430. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2431. } else if (ifmgd->probe_send_count < max_tries) {
  2432. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2433. wiphy_debug(local->hw.wiphy,
  2434. "%s: No probe response from AP %pM"
  2435. " after %dms, try %d/%i\n",
  2436. sdata->name,
  2437. bssid, probe_wait_ms,
  2438. ifmgd->probe_send_count, max_tries);
  2439. #endif
  2440. ieee80211_mgd_probe_ap_send(sdata);
  2441. } else {
  2442. /*
  2443. * We actually lost the connection ... or did we?
  2444. * Let's make sure!
  2445. */
  2446. wiphy_debug(local->hw.wiphy,
  2447. "%s: No probe response from AP %pM"
  2448. " after %dms, disconnecting.\n",
  2449. sdata->name,
  2450. bssid, probe_wait_ms);
  2451. ieee80211_sta_connection_lost(sdata, bssid,
  2452. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2453. }
  2454. }
  2455. mutex_unlock(&ifmgd->mtx);
  2456. mutex_lock(&local->mtx);
  2457. ieee80211_recalc_idle(local);
  2458. mutex_unlock(&local->mtx);
  2459. }
  2460. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  2461. {
  2462. struct ieee80211_sub_if_data *sdata =
  2463. (struct ieee80211_sub_if_data *) data;
  2464. struct ieee80211_local *local = sdata->local;
  2465. if (local->quiescing)
  2466. return;
  2467. ieee80211_queue_work(&sdata->local->hw,
  2468. &sdata->u.mgd.beacon_connection_loss_work);
  2469. }
  2470. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  2471. {
  2472. struct ieee80211_sub_if_data *sdata =
  2473. (struct ieee80211_sub_if_data *) data;
  2474. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2475. struct ieee80211_local *local = sdata->local;
  2476. if (local->quiescing)
  2477. return;
  2478. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  2479. }
  2480. static void ieee80211_sta_monitor_work(struct work_struct *work)
  2481. {
  2482. struct ieee80211_sub_if_data *sdata =
  2483. container_of(work, struct ieee80211_sub_if_data,
  2484. u.mgd.monitor_work);
  2485. ieee80211_mgd_probe_ap(sdata, false);
  2486. }
  2487. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  2488. {
  2489. u32 flags;
  2490. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  2491. sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
  2492. IEEE80211_STA_CONNECTION_POLL);
  2493. /* let's probe the connection once */
  2494. flags = sdata->local->hw.flags;
  2495. if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
  2496. ieee80211_queue_work(&sdata->local->hw,
  2497. &sdata->u.mgd.monitor_work);
  2498. /* and do all the other regular work too */
  2499. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  2500. }
  2501. }
  2502. #ifdef CONFIG_PM
  2503. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  2504. {
  2505. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2506. /*
  2507. * we need to use atomic bitops for the running bits
  2508. * only because both timers might fire at the same
  2509. * time -- the code here is properly synchronised.
  2510. */
  2511. cancel_work_sync(&ifmgd->request_smps_work);
  2512. cancel_work_sync(&ifmgd->monitor_work);
  2513. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  2514. if (del_timer_sync(&ifmgd->timer))
  2515. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  2516. cancel_work_sync(&ifmgd->chswitch_work);
  2517. if (del_timer_sync(&ifmgd->chswitch_timer))
  2518. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  2519. /* these will just be re-established on connection */
  2520. del_timer_sync(&ifmgd->conn_mon_timer);
  2521. del_timer_sync(&ifmgd->bcn_mon_timer);
  2522. }
  2523. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  2524. {
  2525. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2526. if (!ifmgd->associated)
  2527. return;
  2528. if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
  2529. sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
  2530. mutex_lock(&ifmgd->mtx);
  2531. if (ifmgd->associated) {
  2532. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  2533. wiphy_debug(sdata->local->hw.wiphy,
  2534. "%s: driver requested disconnect after resume.\n",
  2535. sdata->name);
  2536. #endif
  2537. ieee80211_sta_connection_lost(sdata,
  2538. ifmgd->associated->bssid,
  2539. WLAN_REASON_UNSPECIFIED);
  2540. mutex_unlock(&ifmgd->mtx);
  2541. return;
  2542. }
  2543. mutex_unlock(&ifmgd->mtx);
  2544. }
  2545. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  2546. add_timer(&ifmgd->timer);
  2547. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  2548. add_timer(&ifmgd->chswitch_timer);
  2549. ieee80211_sta_reset_beacon_monitor(sdata);
  2550. ieee80211_restart_sta_timer(sdata);
  2551. }
  2552. #endif
  2553. /* interface setup */
  2554. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  2555. {
  2556. struct ieee80211_if_managed *ifmgd;
  2557. ifmgd = &sdata->u.mgd;
  2558. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  2559. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  2560. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  2561. ieee80211_beacon_connection_loss_work);
  2562. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
  2563. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  2564. (unsigned long) sdata);
  2565. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  2566. (unsigned long) sdata);
  2567. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  2568. (unsigned long) sdata);
  2569. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  2570. (unsigned long) sdata);
  2571. ifmgd->flags = 0;
  2572. ifmgd->powersave = sdata->wdev.ps;
  2573. mutex_init(&ifmgd->mtx);
  2574. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  2575. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  2576. else
  2577. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  2578. }
  2579. /* scan finished notification */
  2580. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  2581. {
  2582. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  2583. /* Restart STA timers */
  2584. rcu_read_lock();
  2585. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  2586. ieee80211_restart_sta_timer(sdata);
  2587. rcu_read_unlock();
  2588. }
  2589. int ieee80211_max_network_latency(struct notifier_block *nb,
  2590. unsigned long data, void *dummy)
  2591. {
  2592. s32 latency_usec = (s32) data;
  2593. struct ieee80211_local *local =
  2594. container_of(nb, struct ieee80211_local,
  2595. network_latency_notifier);
  2596. mutex_lock(&local->iflist_mtx);
  2597. ieee80211_recalc_ps(local, latency_usec);
  2598. mutex_unlock(&local->iflist_mtx);
  2599. return 0;
  2600. }
  2601. /* config hooks */
  2602. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  2603. struct cfg80211_auth_request *req)
  2604. {
  2605. struct ieee80211_local *local = sdata->local;
  2606. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2607. struct ieee80211_mgd_auth_data *auth_data;
  2608. struct sta_info *sta;
  2609. u16 auth_alg;
  2610. int err;
  2611. /* prepare auth data structure */
  2612. switch (req->auth_type) {
  2613. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  2614. auth_alg = WLAN_AUTH_OPEN;
  2615. break;
  2616. case NL80211_AUTHTYPE_SHARED_KEY:
  2617. if (IS_ERR(local->wep_tx_tfm))
  2618. return -EOPNOTSUPP;
  2619. auth_alg = WLAN_AUTH_SHARED_KEY;
  2620. break;
  2621. case NL80211_AUTHTYPE_FT:
  2622. auth_alg = WLAN_AUTH_FT;
  2623. break;
  2624. case NL80211_AUTHTYPE_NETWORK_EAP:
  2625. auth_alg = WLAN_AUTH_LEAP;
  2626. break;
  2627. default:
  2628. return -EOPNOTSUPP;
  2629. }
  2630. auth_data = kzalloc(sizeof(*auth_data) + req->ie_len, GFP_KERNEL);
  2631. if (!auth_data)
  2632. return -ENOMEM;
  2633. auth_data->bss = req->bss;
  2634. if (req->ie && req->ie_len) {
  2635. memcpy(auth_data->ie, req->ie, req->ie_len);
  2636. auth_data->ie_len = req->ie_len;
  2637. }
  2638. if (req->key && req->key_len) {
  2639. auth_data->key_len = req->key_len;
  2640. auth_data->key_idx = req->key_idx;
  2641. memcpy(auth_data->key, req->key, req->key_len);
  2642. }
  2643. auth_data->algorithm = auth_alg;
  2644. /* try to authenticate/probe */
  2645. mutex_lock(&ifmgd->mtx);
  2646. if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
  2647. ifmgd->assoc_data) {
  2648. err = -EBUSY;
  2649. goto err_free;
  2650. }
  2651. if (ifmgd->auth_data)
  2652. ieee80211_destroy_auth_data(sdata, false);
  2653. /* prep auth_data so we don't go into idle on disassoc */
  2654. ifmgd->auth_data = auth_data;
  2655. if (ifmgd->associated)
  2656. ieee80211_set_disassoc(sdata, true, false);
  2657. printk(KERN_DEBUG "%s: authenticate with %pM\n",
  2658. sdata->name, req->bss->bssid);
  2659. mutex_lock(&local->mtx);
  2660. ieee80211_recalc_idle(sdata->local);
  2661. mutex_unlock(&local->mtx);
  2662. /* switch to the right channel */
  2663. local->oper_channel = req->bss->channel;
  2664. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  2665. /* set BSSID */
  2666. memcpy(ifmgd->bssid, req->bss->bssid, ETH_ALEN);
  2667. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2668. /* add station entry */
  2669. sta = sta_info_alloc(sdata, req->bss->bssid, GFP_KERNEL);
  2670. if (!sta) {
  2671. err = -ENOMEM;
  2672. goto err_clear;
  2673. }
  2674. err = sta_info_insert(sta);
  2675. if (err) {
  2676. printk(KERN_DEBUG
  2677. "%s: failed to insert STA entry for the AP %pM (error %d)\n",
  2678. sdata->name, req->bss->bssid, err);
  2679. goto err_clear;
  2680. }
  2681. err = ieee80211_probe_auth(sdata);
  2682. if (err) {
  2683. if (auth_data->synced)
  2684. drv_finish_tx_sync(local, sdata, req->bss->bssid,
  2685. IEEE80211_TX_SYNC_AUTH);
  2686. sta_info_destroy_addr(sdata, req->bss->bssid);
  2687. goto err_clear;
  2688. }
  2689. /* hold our own reference */
  2690. cfg80211_ref_bss(auth_data->bss);
  2691. err = 0;
  2692. goto out_unlock;
  2693. err_clear:
  2694. ifmgd->auth_data = NULL;
  2695. err_free:
  2696. kfree(auth_data);
  2697. out_unlock:
  2698. mutex_unlock(&ifmgd->mtx);
  2699. return err;
  2700. }
  2701. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  2702. struct cfg80211_assoc_request *req)
  2703. {
  2704. struct ieee80211_local *local = sdata->local;
  2705. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2706. struct ieee80211_bss *bss = (void *)req->bss->priv;
  2707. struct ieee80211_mgd_assoc_data *assoc_data;
  2708. struct sta_info *sta;
  2709. const u8 *ssidie;
  2710. int i, err;
  2711. ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  2712. if (!ssidie)
  2713. return -EINVAL;
  2714. assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
  2715. if (!assoc_data)
  2716. return -ENOMEM;
  2717. mutex_lock(&ifmgd->mtx);
  2718. if (ifmgd->associated)
  2719. ieee80211_set_disassoc(sdata, true, false);
  2720. if (ifmgd->auth_data && !ifmgd->auth_data->done) {
  2721. err = -EBUSY;
  2722. goto err_free;
  2723. }
  2724. if (ifmgd->assoc_data) {
  2725. err = -EBUSY;
  2726. goto err_free;
  2727. }
  2728. if (ifmgd->auth_data) {
  2729. bool match;
  2730. /* keep sta info, bssid if matching */
  2731. match = memcmp(ifmgd->bssid, req->bss->bssid, ETH_ALEN) == 0;
  2732. ieee80211_destroy_auth_data(sdata, match);
  2733. }
  2734. /* prepare assoc data */
  2735. ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
  2736. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  2737. ifmgd->beacon_crc_valid = false;
  2738. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
  2739. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  2740. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  2741. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
  2742. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2743. if (req->flags & ASSOC_REQ_DISABLE_HT)
  2744. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2745. memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
  2746. memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
  2747. sizeof(ifmgd->ht_capa_mask));
  2748. if (req->ie && req->ie_len) {
  2749. memcpy(assoc_data->ie, req->ie, req->ie_len);
  2750. assoc_data->ie_len = req->ie_len;
  2751. }
  2752. assoc_data->bss = req->bss;
  2753. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  2754. if (ifmgd->powersave)
  2755. ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
  2756. else
  2757. ifmgd->ap_smps = IEEE80211_SMPS_OFF;
  2758. } else
  2759. ifmgd->ap_smps = ifmgd->req_smps;
  2760. /*
  2761. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  2762. * We still associate in non-HT mode (11a/b/g) if any one of these
  2763. * ciphers is configured as pairwise.
  2764. * We can set this to true for non-11n hardware, that'll be checked
  2765. * separately along with the peer capabilities.
  2766. */
  2767. assoc_data->capability = req->bss->capability;
  2768. assoc_data->wmm_used = bss->wmm_used;
  2769. assoc_data->supp_rates = bss->supp_rates;
  2770. assoc_data->supp_rates_len = bss->supp_rates_len;
  2771. assoc_data->ht_information_ie =
  2772. ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
  2773. if (bss->wmm_used && bss->uapsd_supported &&
  2774. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
  2775. assoc_data->uapsd_used = true;
  2776. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  2777. } else {
  2778. assoc_data->uapsd_used = false;
  2779. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  2780. }
  2781. memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
  2782. assoc_data->ssid_len = ssidie[1];
  2783. if (req->prev_bssid)
  2784. memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
  2785. if (req->use_mfp) {
  2786. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  2787. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  2788. } else {
  2789. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  2790. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  2791. }
  2792. if (req->crypto.control_port)
  2793. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  2794. else
  2795. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  2796. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  2797. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  2798. /* kick off associate process */
  2799. ifmgd->assoc_data = assoc_data;
  2800. mutex_lock(&local->mtx);
  2801. ieee80211_recalc_idle(sdata->local);
  2802. mutex_unlock(&local->mtx);
  2803. /* switch to the right channel */
  2804. local->oper_channel = req->bss->channel;
  2805. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  2806. rcu_read_lock();
  2807. sta = sta_info_get(sdata, req->bss->bssid);
  2808. rcu_read_unlock();
  2809. if (!sta) {
  2810. /* set BSSID */
  2811. memcpy(ifmgd->bssid, req->bss->bssid, ETH_ALEN);
  2812. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2813. sta = sta_info_alloc(sdata, req->bss->bssid, GFP_KERNEL);
  2814. if (!sta) {
  2815. err = -ENOMEM;
  2816. goto err_clear;
  2817. }
  2818. sta_info_pre_move_state(sta, IEEE80211_STA_AUTH);
  2819. err = sta_info_insert(sta);
  2820. sta = NULL;
  2821. if (err) {
  2822. printk(KERN_DEBUG
  2823. "%s: failed to insert STA entry for the AP (error %d)\n",
  2824. sdata->name, err);
  2825. goto err_clear;
  2826. }
  2827. } else
  2828. WARN_ON_ONCE(memcmp(ifmgd->bssid, req->bss->bssid, ETH_ALEN));
  2829. if (!bss->dtim_period &&
  2830. sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
  2831. /*
  2832. * Wait up to one beacon interval ...
  2833. * should this be more if we miss one?
  2834. */
  2835. printk(KERN_DEBUG "%s: waiting for beacon from %pM\n",
  2836. sdata->name, ifmgd->bssid);
  2837. assoc_data->timeout = jiffies +
  2838. TU_TO_EXP_TIME(req->bss->beacon_interval);
  2839. } else {
  2840. assoc_data->have_beacon = true;
  2841. assoc_data->sent_assoc = false;
  2842. assoc_data->timeout = jiffies;
  2843. }
  2844. run_again(ifmgd, assoc_data->timeout);
  2845. err = 0;
  2846. goto out;
  2847. err_clear:
  2848. ifmgd->assoc_data = NULL;
  2849. err_free:
  2850. kfree(assoc_data);
  2851. out:
  2852. mutex_unlock(&ifmgd->mtx);
  2853. return err;
  2854. }
  2855. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  2856. struct cfg80211_deauth_request *req)
  2857. {
  2858. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2859. bool assoc_bss = false;
  2860. u8 frame_buf[DEAUTH_DISASSOC_LEN];
  2861. mutex_lock(&ifmgd->mtx);
  2862. if (ifmgd->associated &&
  2863. memcmp(ifmgd->associated->bssid, req->bssid, ETH_ALEN) == 0) {
  2864. ieee80211_set_disassoc(sdata, false, true);
  2865. assoc_bss = true;
  2866. } else if (ifmgd->auth_data) {
  2867. ieee80211_destroy_auth_data(sdata, false);
  2868. mutex_unlock(&ifmgd->mtx);
  2869. return 0;
  2870. }
  2871. mutex_unlock(&ifmgd->mtx);
  2872. printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
  2873. sdata->name, req->bssid, req->reason_code);
  2874. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  2875. IEEE80211_STYPE_DEAUTH,
  2876. req->reason_code, true, frame_buf);
  2877. __cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
  2878. if (assoc_bss)
  2879. sta_info_flush(sdata->local, sdata);
  2880. mutex_lock(&sdata->local->mtx);
  2881. ieee80211_recalc_idle(sdata->local);
  2882. mutex_unlock(&sdata->local->mtx);
  2883. return 0;
  2884. }
  2885. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  2886. struct cfg80211_disassoc_request *req)
  2887. {
  2888. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2889. u8 bssid[ETH_ALEN];
  2890. u8 frame_buf[DEAUTH_DISASSOC_LEN];
  2891. mutex_lock(&ifmgd->mtx);
  2892. /*
  2893. * cfg80211 should catch this ... but it's racy since
  2894. * we can receive a disassoc frame, process it, hand it
  2895. * to cfg80211 while that's in a locked section already
  2896. * trying to tell us that the user wants to disconnect.
  2897. */
  2898. if (ifmgd->associated != req->bss) {
  2899. mutex_unlock(&ifmgd->mtx);
  2900. return -ENOLINK;
  2901. }
  2902. printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
  2903. sdata->name, req->bss->bssid, req->reason_code);
  2904. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  2905. ieee80211_set_disassoc(sdata, false, true);
  2906. mutex_unlock(&ifmgd->mtx);
  2907. ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
  2908. IEEE80211_STYPE_DISASSOC,
  2909. req->reason_code,
  2910. !req->local_state_change, frame_buf);
  2911. __cfg80211_send_disassoc(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
  2912. sta_info_flush(sdata->local, sdata);
  2913. mutex_lock(&sdata->local->mtx);
  2914. ieee80211_recalc_idle(sdata->local);
  2915. mutex_unlock(&sdata->local->mtx);
  2916. return 0;
  2917. }
  2918. void ieee80211_mgd_teardown(struct ieee80211_sub_if_data *sdata)
  2919. {
  2920. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2921. mutex_lock(&ifmgd->mtx);
  2922. if (ifmgd->assoc_data)
  2923. ieee80211_destroy_assoc_data(sdata, false);
  2924. if (ifmgd->auth_data)
  2925. ieee80211_destroy_auth_data(sdata, false);
  2926. del_timer_sync(&ifmgd->timer);
  2927. mutex_unlock(&ifmgd->mtx);
  2928. }
  2929. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  2930. enum nl80211_cqm_rssi_threshold_event rssi_event,
  2931. gfp_t gfp)
  2932. {
  2933. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2934. trace_api_cqm_rssi_notify(sdata, rssi_event);
  2935. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  2936. }
  2937. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);
  2938. unsigned char ieee80211_get_operstate(struct ieee80211_vif *vif)
  2939. {
  2940. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2941. return sdata->dev->operstate;
  2942. }
  2943. EXPORT_SYMBOL(ieee80211_get_operstate);