mlme.c 98 KB

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