mlme.c 98 KB

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