mlme.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  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/rtnetlink.h>
  19. #include <linux/pm_qos_params.h>
  20. #include <linux/crc32.h>
  21. #include <linux/slab.h>
  22. #include <net/mac80211.h>
  23. #include <asm/unaligned.h>
  24. #include "ieee80211_i.h"
  25. #include "driver-ops.h"
  26. #include "rate.h"
  27. #include "led.h"
  28. static int max_nullfunc_tries = 2;
  29. module_param(max_nullfunc_tries, int, 0644);
  30. MODULE_PARM_DESC(max_nullfunc_tries,
  31. "Maximum nullfunc tx tries before disconnecting (reason 4).");
  32. static int max_probe_tries = 5;
  33. module_param(max_probe_tries, int, 0644);
  34. MODULE_PARM_DESC(max_probe_tries,
  35. "Maximum probe tries before disconnecting (reason 4).");
  36. /*
  37. * Beacon loss timeout is calculated as N frames times the
  38. * advertised beacon interval. This may need to be somewhat
  39. * higher than what hardware might detect to account for
  40. * delays in the host processing frames. But since we also
  41. * probe on beacon miss before declaring the connection lost
  42. * default to what we want.
  43. */
  44. #define IEEE80211_BEACON_LOSS_COUNT 7
  45. /*
  46. * Time the connection can be idle before we probe
  47. * it to see if we can still talk to the AP.
  48. */
  49. #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
  50. /*
  51. * Time we wait for a probe response after sending
  52. * a probe request because of beacon loss or for
  53. * checking the connection still works.
  54. */
  55. static int probe_wait_ms = 500;
  56. module_param(probe_wait_ms, int, 0644);
  57. MODULE_PARM_DESC(probe_wait_ms,
  58. "Maximum time(ms) to wait for probe response"
  59. " before disconnecting (reason 4).");
  60. /*
  61. * Weight given to the latest Beacon frame when calculating average signal
  62. * strength for Beacon frames received in the current BSS. This must be
  63. * between 1 and 15.
  64. */
  65. #define IEEE80211_SIGNAL_AVE_WEIGHT 3
  66. /*
  67. * How many Beacon frames need to have been used in average signal strength
  68. * before starting to indicate signal change events.
  69. */
  70. #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
  71. #define TMR_RUNNING_TIMER 0
  72. #define TMR_RUNNING_CHANSW 1
  73. /*
  74. * All cfg80211 functions have to be called outside a locked
  75. * section so that they can acquire a lock themselves... This
  76. * is much simpler than queuing up things in cfg80211, but we
  77. * do need some indirection for that here.
  78. */
  79. enum rx_mgmt_action {
  80. /* no action required */
  81. RX_MGMT_NONE,
  82. /* caller must call cfg80211_send_rx_auth() */
  83. RX_MGMT_CFG80211_AUTH,
  84. /* caller must call cfg80211_send_rx_assoc() */
  85. RX_MGMT_CFG80211_ASSOC,
  86. /* caller must call cfg80211_send_deauth() */
  87. RX_MGMT_CFG80211_DEAUTH,
  88. /* caller must call cfg80211_send_disassoc() */
  89. RX_MGMT_CFG80211_DISASSOC,
  90. /* caller must tell cfg80211 about internal error */
  91. RX_MGMT_CFG80211_ASSOC_ERROR,
  92. };
  93. /* utils */
  94. static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
  95. {
  96. lockdep_assert_held(&ifmgd->mtx);
  97. }
  98. /*
  99. * We can have multiple work items (and connection probing)
  100. * scheduling this timer, but we need to take care to only
  101. * reschedule it when it should fire _earlier_ than it was
  102. * asked for before, or if it's not pending right now. This
  103. * function ensures that. Note that it then is required to
  104. * run this function for all timeouts after the first one
  105. * has happened -- the work that runs from this timer will
  106. * do that.
  107. */
  108. static void run_again(struct ieee80211_if_managed *ifmgd,
  109. unsigned long timeout)
  110. {
  111. ASSERT_MGD_MTX(ifmgd);
  112. if (!timer_pending(&ifmgd->timer) ||
  113. time_before(timeout, ifmgd->timer.expires))
  114. mod_timer(&ifmgd->timer, timeout);
  115. }
  116. void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
  117. {
  118. if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
  119. return;
  120. mod_timer(&sdata->u.mgd.bcn_mon_timer,
  121. round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
  122. }
  123. void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
  124. {
  125. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  126. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  127. return;
  128. mod_timer(&sdata->u.mgd.conn_mon_timer,
  129. round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  130. ifmgd->probe_send_count = 0;
  131. }
  132. static int ecw2cw(int ecw)
  133. {
  134. return (1 << ecw) - 1;
  135. }
  136. /*
  137. * ieee80211_enable_ht should be called only after the operating band
  138. * has been determined as ht configuration depends on the hw's
  139. * HT abilities for a specific band.
  140. */
  141. static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
  142. struct ieee80211_ht_info *hti,
  143. const u8 *bssid, u16 ap_ht_cap_flags)
  144. {
  145. struct ieee80211_local *local = sdata->local;
  146. struct ieee80211_supported_band *sband;
  147. struct sta_info *sta;
  148. u32 changed = 0;
  149. int hti_cfreq;
  150. u16 ht_opmode;
  151. bool enable_ht = true;
  152. enum nl80211_channel_type prev_chantype;
  153. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  154. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  155. prev_chantype = sdata->vif.bss_conf.channel_type;
  156. /* HT is not supported */
  157. if (!sband->ht_cap.ht_supported)
  158. enable_ht = false;
  159. if (enable_ht) {
  160. hti_cfreq = ieee80211_channel_to_frequency(hti->control_chan,
  161. sband->band);
  162. /* check that channel matches the right operating channel */
  163. if (local->hw.conf.channel->center_freq != hti_cfreq) {
  164. /* Some APs mess this up, evidently.
  165. * Netgear WNDR3700 sometimes reports 4 higher than
  166. * the actual channel, for instance.
  167. */
  168. printk(KERN_DEBUG
  169. "%s: Wrong control channel in association"
  170. " response: configured center-freq: %d"
  171. " hti-cfreq: %d hti->control_chan: %d"
  172. " band: %d. Disabling HT.\n",
  173. sdata->name,
  174. local->hw.conf.channel->center_freq,
  175. hti_cfreq, hti->control_chan,
  176. sband->band);
  177. enable_ht = false;
  178. }
  179. }
  180. if (enable_ht) {
  181. channel_type = NL80211_CHAN_HT20;
  182. if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
  183. (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
  184. (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
  185. switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  186. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  187. if (!(local->hw.conf.channel->flags &
  188. IEEE80211_CHAN_NO_HT40PLUS))
  189. channel_type = NL80211_CHAN_HT40PLUS;
  190. break;
  191. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  192. if (!(local->hw.conf.channel->flags &
  193. IEEE80211_CHAN_NO_HT40MINUS))
  194. channel_type = NL80211_CHAN_HT40MINUS;
  195. break;
  196. }
  197. }
  198. }
  199. if (local->tmp_channel)
  200. local->tmp_channel_type = channel_type;
  201. if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
  202. /* can only fail due to HT40+/- mismatch */
  203. channel_type = NL80211_CHAN_HT20;
  204. WARN_ON(!ieee80211_set_channel_type(local, sdata, channel_type));
  205. }
  206. /* channel_type change automatically detected */
  207. ieee80211_hw_config(local, 0);
  208. if (prev_chantype != channel_type) {
  209. rcu_read_lock();
  210. sta = sta_info_get(sdata, bssid);
  211. if (sta)
  212. rate_control_rate_update(local, sband, sta,
  213. IEEE80211_RC_HT_CHANGED,
  214. channel_type);
  215. rcu_read_unlock();
  216. }
  217. ht_opmode = le16_to_cpu(hti->operation_mode);
  218. /* if bss configuration changed store the new one */
  219. if (sdata->ht_opmode_valid != enable_ht ||
  220. sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
  221. prev_chantype != channel_type) {
  222. changed |= BSS_CHANGED_HT;
  223. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  224. sdata->ht_opmode_valid = enable_ht;
  225. }
  226. return changed;
  227. }
  228. /* frame sending functions */
  229. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  230. const u8 *bssid, u16 stype, u16 reason,
  231. void *cookie, bool send_frame)
  232. {
  233. struct ieee80211_local *local = sdata->local;
  234. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  235. struct sk_buff *skb;
  236. struct ieee80211_mgmt *mgmt;
  237. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
  238. if (!skb) {
  239. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  240. "deauth/disassoc frame\n", sdata->name);
  241. return;
  242. }
  243. skb_reserve(skb, local->hw.extra_tx_headroom);
  244. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  245. memset(mgmt, 0, 24);
  246. memcpy(mgmt->da, bssid, ETH_ALEN);
  247. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  248. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  249. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  250. skb_put(skb, 2);
  251. /* u.deauth.reason_code == u.disassoc.reason_code */
  252. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  253. if (stype == IEEE80211_STYPE_DEAUTH)
  254. if (cookie)
  255. __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  256. else
  257. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  258. else
  259. if (cookie)
  260. __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  261. else
  262. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  263. if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
  264. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  265. if (send_frame)
  266. ieee80211_tx_skb(sdata, skb);
  267. else
  268. kfree_skb(skb);
  269. }
  270. void ieee80211_send_pspoll(struct ieee80211_local *local,
  271. struct ieee80211_sub_if_data *sdata)
  272. {
  273. struct ieee80211_pspoll *pspoll;
  274. struct sk_buff *skb;
  275. skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
  276. if (!skb)
  277. return;
  278. pspoll = (struct ieee80211_pspoll *) skb->data;
  279. pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  280. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  281. ieee80211_tx_skb(sdata, skb);
  282. }
  283. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  284. struct ieee80211_sub_if_data *sdata,
  285. int powersave)
  286. {
  287. struct sk_buff *skb;
  288. struct ieee80211_hdr_3addr *nullfunc;
  289. skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
  290. if (!skb)
  291. return;
  292. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  293. if (powersave)
  294. nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  295. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  296. ieee80211_tx_skb(sdata, skb);
  297. }
  298. static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
  299. struct ieee80211_sub_if_data *sdata)
  300. {
  301. struct sk_buff *skb;
  302. struct ieee80211_hdr *nullfunc;
  303. __le16 fc;
  304. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  305. return;
  306. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
  307. if (!skb) {
  308. printk(KERN_DEBUG "%s: failed to allocate buffer for 4addr "
  309. "nullfunc frame\n", sdata->name);
  310. return;
  311. }
  312. skb_reserve(skb, local->hw.extra_tx_headroom);
  313. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
  314. memset(nullfunc, 0, 30);
  315. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  316. IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  317. nullfunc->frame_control = fc;
  318. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  319. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  320. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  321. memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
  322. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  323. ieee80211_tx_skb(sdata, skb);
  324. }
  325. /* spectrum management related things */
  326. static void ieee80211_chswitch_work(struct work_struct *work)
  327. {
  328. struct ieee80211_sub_if_data *sdata =
  329. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  330. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  331. if (!ieee80211_sdata_running(sdata))
  332. return;
  333. mutex_lock(&ifmgd->mtx);
  334. if (!ifmgd->associated)
  335. goto out;
  336. sdata->local->oper_channel = sdata->local->csa_channel;
  337. if (!sdata->local->ops->channel_switch) {
  338. /* call "hw_config" only if doing sw channel switch */
  339. ieee80211_hw_config(sdata->local,
  340. IEEE80211_CONF_CHANGE_CHANNEL);
  341. }
  342. /* XXX: shouldn't really modify cfg80211-owned data! */
  343. ifmgd->associated->channel = sdata->local->oper_channel;
  344. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  345. IEEE80211_QUEUE_STOP_REASON_CSA);
  346. out:
  347. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  348. mutex_unlock(&ifmgd->mtx);
  349. }
  350. void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
  351. {
  352. struct ieee80211_sub_if_data *sdata;
  353. struct ieee80211_if_managed *ifmgd;
  354. sdata = vif_to_sdata(vif);
  355. ifmgd = &sdata->u.mgd;
  356. trace_api_chswitch_done(sdata, success);
  357. if (!success) {
  358. /*
  359. * If the channel switch was not successful, stay
  360. * around on the old channel. We currently lack
  361. * good handling of this situation, possibly we
  362. * should just drop the association.
  363. */
  364. sdata->local->csa_channel = sdata->local->oper_channel;
  365. }
  366. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  367. }
  368. EXPORT_SYMBOL(ieee80211_chswitch_done);
  369. static void ieee80211_chswitch_timer(unsigned long data)
  370. {
  371. struct ieee80211_sub_if_data *sdata =
  372. (struct ieee80211_sub_if_data *) data;
  373. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  374. if (sdata->local->quiescing) {
  375. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  376. return;
  377. }
  378. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  379. }
  380. void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  381. struct ieee80211_channel_sw_ie *sw_elem,
  382. struct ieee80211_bss *bss,
  383. u64 timestamp)
  384. {
  385. struct cfg80211_bss *cbss =
  386. container_of((void *)bss, struct cfg80211_bss, priv);
  387. struct ieee80211_channel *new_ch;
  388. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  389. int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
  390. cbss->channel->band);
  391. ASSERT_MGD_MTX(ifmgd);
  392. if (!ifmgd->associated)
  393. return;
  394. if (sdata->local->scanning)
  395. return;
  396. /* Disregard subsequent beacons if we are already running a timer
  397. processing a CSA */
  398. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  399. return;
  400. new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  401. if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
  402. return;
  403. sdata->local->csa_channel = new_ch;
  404. if (sdata->local->ops->channel_switch) {
  405. /* use driver's channel switch callback */
  406. struct ieee80211_channel_switch ch_switch;
  407. memset(&ch_switch, 0, sizeof(ch_switch));
  408. ch_switch.timestamp = timestamp;
  409. if (sw_elem->mode) {
  410. ch_switch.block_tx = true;
  411. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  412. IEEE80211_QUEUE_STOP_REASON_CSA);
  413. }
  414. ch_switch.channel = new_ch;
  415. ch_switch.count = sw_elem->count;
  416. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  417. drv_channel_switch(sdata->local, &ch_switch);
  418. return;
  419. }
  420. /* channel switch handled in software */
  421. if (sw_elem->count <= 1) {
  422. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  423. } else {
  424. if (sw_elem->mode)
  425. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  426. IEEE80211_QUEUE_STOP_REASON_CSA);
  427. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  428. mod_timer(&ifmgd->chswitch_timer,
  429. jiffies +
  430. msecs_to_jiffies(sw_elem->count *
  431. cbss->beacon_interval));
  432. }
  433. }
  434. static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  435. u16 capab_info, u8 *pwr_constr_elem,
  436. u8 pwr_constr_elem_len)
  437. {
  438. struct ieee80211_conf *conf = &sdata->local->hw.conf;
  439. if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
  440. return;
  441. /* Power constraint IE length should be 1 octet */
  442. if (pwr_constr_elem_len != 1)
  443. return;
  444. if ((*pwr_constr_elem <= conf->channel->max_power) &&
  445. (*pwr_constr_elem != sdata->local->power_constr_level)) {
  446. sdata->local->power_constr_level = *pwr_constr_elem;
  447. ieee80211_hw_config(sdata->local, 0);
  448. }
  449. }
  450. void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
  451. {
  452. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  453. struct ieee80211_local *local = sdata->local;
  454. struct ieee80211_conf *conf = &local->hw.conf;
  455. WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
  456. !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
  457. (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
  458. local->disable_dynamic_ps = false;
  459. conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
  460. }
  461. EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
  462. void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
  463. {
  464. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  465. struct ieee80211_local *local = sdata->local;
  466. struct ieee80211_conf *conf = &local->hw.conf;
  467. WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
  468. !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
  469. (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
  470. local->disable_dynamic_ps = true;
  471. conf->dynamic_ps_timeout = 0;
  472. del_timer_sync(&local->dynamic_ps_timer);
  473. ieee80211_queue_work(&local->hw,
  474. &local->dynamic_ps_enable_work);
  475. }
  476. EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
  477. /* powersave */
  478. static void ieee80211_enable_ps(struct ieee80211_local *local,
  479. struct ieee80211_sub_if_data *sdata)
  480. {
  481. struct ieee80211_conf *conf = &local->hw.conf;
  482. /*
  483. * If we are scanning right now then the parameters will
  484. * take effect when scan finishes.
  485. */
  486. if (local->scanning)
  487. return;
  488. if (conf->dynamic_ps_timeout > 0 &&
  489. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  490. mod_timer(&local->dynamic_ps_timer, jiffies +
  491. msecs_to_jiffies(conf->dynamic_ps_timeout));
  492. } else {
  493. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  494. ieee80211_send_nullfunc(local, sdata, 1);
  495. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  496. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
  497. return;
  498. conf->flags |= IEEE80211_CONF_PS;
  499. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  500. }
  501. }
  502. static void ieee80211_change_ps(struct ieee80211_local *local)
  503. {
  504. struct ieee80211_conf *conf = &local->hw.conf;
  505. if (local->ps_sdata) {
  506. ieee80211_enable_ps(local, local->ps_sdata);
  507. } else if (conf->flags & IEEE80211_CONF_PS) {
  508. conf->flags &= ~IEEE80211_CONF_PS;
  509. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  510. del_timer_sync(&local->dynamic_ps_timer);
  511. cancel_work_sync(&local->dynamic_ps_enable_work);
  512. }
  513. }
  514. /* need to hold RTNL or interface lock */
  515. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  516. {
  517. struct ieee80211_sub_if_data *sdata, *found = NULL;
  518. int count = 0;
  519. int timeout;
  520. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  521. local->ps_sdata = NULL;
  522. return;
  523. }
  524. if (!list_empty(&local->work_list)) {
  525. local->ps_sdata = NULL;
  526. goto change;
  527. }
  528. list_for_each_entry(sdata, &local->interfaces, list) {
  529. if (!ieee80211_sdata_running(sdata))
  530. continue;
  531. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  532. /* If an AP vif is found, then disable PS
  533. * by setting the count to zero thereby setting
  534. * ps_sdata to NULL.
  535. */
  536. count = 0;
  537. break;
  538. }
  539. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  540. continue;
  541. found = sdata;
  542. count++;
  543. }
  544. if (count == 1 && found->u.mgd.powersave &&
  545. found->u.mgd.associated &&
  546. found->u.mgd.associated->beacon_ies &&
  547. !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
  548. IEEE80211_STA_CONNECTION_POLL))) {
  549. struct ieee80211_conf *conf = &local->hw.conf;
  550. s32 beaconint_us;
  551. if (latency < 0)
  552. latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
  553. beaconint_us = ieee80211_tu_to_usec(
  554. found->vif.bss_conf.beacon_int);
  555. timeout = local->dynamic_ps_forced_timeout;
  556. if (timeout < 0) {
  557. /*
  558. * Go to full PSM if the user configures a very low
  559. * latency requirement.
  560. * The 2000 second value is there for compatibility
  561. * until the PM_QOS_NETWORK_LATENCY is configured
  562. * with real values.
  563. */
  564. if (latency > (1900 * USEC_PER_MSEC) &&
  565. latency != (2000 * USEC_PER_SEC))
  566. timeout = 0;
  567. else
  568. timeout = 100;
  569. }
  570. local->dynamic_ps_user_timeout = timeout;
  571. if (!local->disable_dynamic_ps)
  572. conf->dynamic_ps_timeout =
  573. local->dynamic_ps_user_timeout;
  574. if (beaconint_us > latency) {
  575. local->ps_sdata = NULL;
  576. } else {
  577. struct ieee80211_bss *bss;
  578. int maxslp = 1;
  579. u8 dtimper;
  580. bss = (void *)found->u.mgd.associated->priv;
  581. dtimper = bss->dtim_period;
  582. /* If the TIM IE is invalid, pretend the value is 1 */
  583. if (!dtimper)
  584. dtimper = 1;
  585. else if (dtimper > 1)
  586. maxslp = min_t(int, dtimper,
  587. latency / beaconint_us);
  588. local->hw.conf.max_sleep_period = maxslp;
  589. local->hw.conf.ps_dtim_period = dtimper;
  590. local->ps_sdata = found;
  591. }
  592. } else {
  593. local->ps_sdata = NULL;
  594. }
  595. change:
  596. ieee80211_change_ps(local);
  597. }
  598. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  599. {
  600. struct ieee80211_local *local =
  601. container_of(work, struct ieee80211_local,
  602. dynamic_ps_disable_work);
  603. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  604. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  605. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  606. }
  607. ieee80211_wake_queues_by_reason(&local->hw,
  608. IEEE80211_QUEUE_STOP_REASON_PS);
  609. }
  610. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  611. {
  612. struct ieee80211_local *local =
  613. container_of(work, struct ieee80211_local,
  614. dynamic_ps_enable_work);
  615. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  616. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  617. /* can only happen when PS was just disabled anyway */
  618. if (!sdata)
  619. return;
  620. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  621. return;
  622. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  623. (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)))
  624. ieee80211_send_nullfunc(local, sdata, 1);
  625. if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
  626. (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
  627. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  628. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  629. local->hw.conf.flags |= IEEE80211_CONF_PS;
  630. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  631. }
  632. }
  633. void ieee80211_dynamic_ps_timer(unsigned long data)
  634. {
  635. struct ieee80211_local *local = (void *) data;
  636. if (local->quiescing || local->suspended)
  637. return;
  638. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  639. }
  640. /* MLME */
  641. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  642. struct ieee80211_sub_if_data *sdata,
  643. u8 *wmm_param, size_t wmm_param_len)
  644. {
  645. struct ieee80211_tx_queue_params params;
  646. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  647. size_t left;
  648. int count;
  649. u8 *pos, uapsd_queues = 0;
  650. if (!local->ops->conf_tx)
  651. return;
  652. if (local->hw.queues < 4)
  653. return;
  654. if (!wmm_param)
  655. return;
  656. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  657. return;
  658. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  659. uapsd_queues = local->uapsd_queues;
  660. count = wmm_param[6] & 0x0f;
  661. if (count == ifmgd->wmm_last_param_set)
  662. return;
  663. ifmgd->wmm_last_param_set = count;
  664. pos = wmm_param + 8;
  665. left = wmm_param_len - 8;
  666. memset(&params, 0, sizeof(params));
  667. local->wmm_acm = 0;
  668. for (; left >= 4; left -= 4, pos += 4) {
  669. int aci = (pos[0] >> 5) & 0x03;
  670. int acm = (pos[0] >> 4) & 0x01;
  671. bool uapsd = false;
  672. int queue;
  673. switch (aci) {
  674. case 1: /* AC_BK */
  675. queue = 3;
  676. if (acm)
  677. local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  678. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  679. uapsd = true;
  680. break;
  681. case 2: /* AC_VI */
  682. queue = 1;
  683. if (acm)
  684. local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  685. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  686. uapsd = true;
  687. break;
  688. case 3: /* AC_VO */
  689. queue = 0;
  690. if (acm)
  691. local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  692. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  693. uapsd = true;
  694. break;
  695. case 0: /* AC_BE */
  696. default:
  697. queue = 2;
  698. if (acm)
  699. local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  700. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  701. uapsd = true;
  702. break;
  703. }
  704. params.aifs = pos[0] & 0x0f;
  705. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  706. params.cw_min = ecw2cw(pos[1] & 0x0f);
  707. params.txop = get_unaligned_le16(pos + 2);
  708. params.uapsd = uapsd;
  709. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  710. wiphy_debug(local->hw.wiphy,
  711. "WMM queue=%d aci=%d acm=%d aifs=%d "
  712. "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
  713. queue, aci, acm,
  714. params.aifs, params.cw_min, params.cw_max,
  715. params.txop, params.uapsd);
  716. #endif
  717. if (drv_conf_tx(local, queue, &params))
  718. wiphy_debug(local->hw.wiphy,
  719. "failed to set TX queue parameters for queue %d\n",
  720. queue);
  721. }
  722. /* enable WMM or activate new settings */
  723. sdata->vif.bss_conf.qos = true;
  724. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  725. }
  726. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  727. u16 capab, bool erp_valid, u8 erp)
  728. {
  729. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  730. u32 changed = 0;
  731. bool use_protection;
  732. bool use_short_preamble;
  733. bool use_short_slot;
  734. if (erp_valid) {
  735. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  736. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  737. } else {
  738. use_protection = false;
  739. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  740. }
  741. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  742. if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
  743. use_short_slot = true;
  744. if (use_protection != bss_conf->use_cts_prot) {
  745. bss_conf->use_cts_prot = use_protection;
  746. changed |= BSS_CHANGED_ERP_CTS_PROT;
  747. }
  748. if (use_short_preamble != bss_conf->use_short_preamble) {
  749. bss_conf->use_short_preamble = use_short_preamble;
  750. changed |= BSS_CHANGED_ERP_PREAMBLE;
  751. }
  752. if (use_short_slot != bss_conf->use_short_slot) {
  753. bss_conf->use_short_slot = use_short_slot;
  754. changed |= BSS_CHANGED_ERP_SLOT;
  755. }
  756. return changed;
  757. }
  758. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  759. struct cfg80211_bss *cbss,
  760. u32 bss_info_changed)
  761. {
  762. struct ieee80211_bss *bss = (void *)cbss->priv;
  763. struct ieee80211_local *local = sdata->local;
  764. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  765. bss_info_changed |= BSS_CHANGED_ASSOC;
  766. /* set timing information */
  767. bss_conf->beacon_int = cbss->beacon_interval;
  768. bss_conf->timestamp = cbss->tsf;
  769. bss_info_changed |= BSS_CHANGED_BEACON_INT;
  770. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  771. cbss->capability, bss->has_erp_value, bss->erp_value);
  772. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  773. IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
  774. sdata->u.mgd.associated = cbss;
  775. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  776. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  777. /* just to be sure */
  778. sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  779. IEEE80211_STA_BEACON_POLL);
  780. ieee80211_led_assoc(local, 1);
  781. if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
  782. bss_conf->dtim_period = bss->dtim_period;
  783. else
  784. bss_conf->dtim_period = 0;
  785. bss_conf->assoc = 1;
  786. /*
  787. * For now just always ask the driver to update the basic rateset
  788. * when we have associated, we aren't checking whether it actually
  789. * changed or not.
  790. */
  791. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  792. /* And the BSSID changed - we're associated now */
  793. bss_info_changed |= BSS_CHANGED_BSSID;
  794. /* Tell the driver to monitor connection quality (if supported) */
  795. if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
  796. bss_conf->cqm_rssi_thold)
  797. bss_info_changed |= BSS_CHANGED_CQM;
  798. /* Enable ARP filtering */
  799. if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
  800. bss_conf->arp_filter_enabled = sdata->arp_filter_state;
  801. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  802. }
  803. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  804. mutex_lock(&local->iflist_mtx);
  805. ieee80211_recalc_ps(local, -1);
  806. ieee80211_recalc_smps(local);
  807. mutex_unlock(&local->iflist_mtx);
  808. netif_tx_start_all_queues(sdata->dev);
  809. netif_carrier_on(sdata->dev);
  810. }
  811. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  812. bool remove_sta, bool tx)
  813. {
  814. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  815. struct ieee80211_local *local = sdata->local;
  816. struct sta_info *sta;
  817. u32 changed = 0, config_changed = 0;
  818. u8 bssid[ETH_ALEN];
  819. ASSERT_MGD_MTX(ifmgd);
  820. if (WARN_ON(!ifmgd->associated))
  821. return;
  822. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  823. ifmgd->associated = NULL;
  824. memset(ifmgd->bssid, 0, ETH_ALEN);
  825. /*
  826. * we need to commit the associated = NULL change because the
  827. * scan code uses that to determine whether this iface should
  828. * go to/wake up from powersave or not -- and could otherwise
  829. * wake the queues erroneously.
  830. */
  831. smp_mb();
  832. /*
  833. * Thus, we can only afterwards stop the queues -- to account
  834. * for the case where another CPU is finishing a scan at this
  835. * time -- we don't want the scan code to enable queues.
  836. */
  837. netif_tx_stop_all_queues(sdata->dev);
  838. netif_carrier_off(sdata->dev);
  839. mutex_lock(&local->sta_mtx);
  840. sta = sta_info_get(sdata, bssid);
  841. if (sta) {
  842. set_sta_flags(sta, WLAN_STA_BLOCK_BA);
  843. ieee80211_sta_tear_down_BA_sessions(sta, tx);
  844. }
  845. mutex_unlock(&local->sta_mtx);
  846. changed |= ieee80211_reset_erp_info(sdata);
  847. ieee80211_led_assoc(local, 0);
  848. changed |= BSS_CHANGED_ASSOC;
  849. sdata->vif.bss_conf.assoc = false;
  850. ieee80211_set_wmm_default(sdata);
  851. /* channel(_type) changes are handled by ieee80211_hw_config */
  852. WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
  853. /* on the next assoc, re-program HT parameters */
  854. sdata->ht_opmode_valid = false;
  855. local->power_constr_level = 0;
  856. del_timer_sync(&local->dynamic_ps_timer);
  857. cancel_work_sync(&local->dynamic_ps_enable_work);
  858. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  859. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  860. config_changed |= IEEE80211_CONF_CHANGE_PS;
  861. }
  862. ieee80211_hw_config(local, config_changed);
  863. /* Disable ARP filtering */
  864. if (sdata->vif.bss_conf.arp_filter_enabled) {
  865. sdata->vif.bss_conf.arp_filter_enabled = false;
  866. changed |= BSS_CHANGED_ARP_FILTER;
  867. }
  868. /* The BSSID (not really interesting) and HT changed */
  869. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  870. ieee80211_bss_info_change_notify(sdata, changed);
  871. if (remove_sta)
  872. sta_info_destroy_addr(sdata, bssid);
  873. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  874. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  875. del_timer_sync(&sdata->u.mgd.timer);
  876. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  877. }
  878. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  879. struct ieee80211_hdr *hdr)
  880. {
  881. /*
  882. * We can postpone the mgd.timer whenever receiving unicast frames
  883. * from AP because we know that the connection is working both ways
  884. * at that time. But multicast frames (and hence also beacons) must
  885. * be ignored here, because we need to trigger the timer during
  886. * data idle periods for sending the periodic probe request to the
  887. * AP we're connected to.
  888. */
  889. if (is_multicast_ether_addr(hdr->addr1))
  890. return;
  891. ieee80211_sta_reset_conn_monitor(sdata);
  892. }
  893. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  894. {
  895. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  896. if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  897. IEEE80211_STA_CONNECTION_POLL)))
  898. return;
  899. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  900. IEEE80211_STA_BEACON_POLL);
  901. mutex_lock(&sdata->local->iflist_mtx);
  902. ieee80211_recalc_ps(sdata->local, -1);
  903. mutex_unlock(&sdata->local->iflist_mtx);
  904. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  905. return;
  906. /*
  907. * We've received a probe response, but are not sure whether
  908. * we have or will be receiving any beacons or data, so let's
  909. * schedule the timers again, just in case.
  910. */
  911. ieee80211_sta_reset_beacon_monitor(sdata);
  912. mod_timer(&ifmgd->conn_mon_timer,
  913. round_jiffies_up(jiffies +
  914. IEEE80211_CONNECTION_IDLE_TIME));
  915. }
  916. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  917. struct ieee80211_hdr *hdr, bool ack)
  918. {
  919. if (!ieee80211_is_data(hdr->frame_control))
  920. return;
  921. if (ack)
  922. ieee80211_sta_reset_conn_monitor(sdata);
  923. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  924. sdata->u.mgd.probe_send_count > 0) {
  925. if (ack)
  926. sdata->u.mgd.probe_send_count = 0;
  927. else
  928. sdata->u.mgd.nullfunc_failed = true;
  929. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  930. }
  931. }
  932. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  933. {
  934. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  935. const u8 *ssid;
  936. u8 *dst = ifmgd->associated->bssid;
  937. u8 unicast_limit = max(1, max_probe_tries - 3);
  938. /*
  939. * Try sending broadcast probe requests for the last three
  940. * probe requests after the first ones failed since some
  941. * buggy APs only support broadcast probe requests.
  942. */
  943. if (ifmgd->probe_send_count >= unicast_limit)
  944. dst = NULL;
  945. /*
  946. * When the hardware reports an accurate Tx ACK status, it's
  947. * better to send a nullfunc frame instead of a probe request,
  948. * as it will kick us off the AP quickly if we aren't associated
  949. * anymore. The timeout will be reset if the frame is ACKed by
  950. * the AP.
  951. */
  952. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  953. ifmgd->nullfunc_failed = false;
  954. ieee80211_send_nullfunc(sdata->local, sdata, 0);
  955. } else {
  956. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  957. ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0);
  958. }
  959. ifmgd->probe_send_count++;
  960. ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
  961. run_again(ifmgd, ifmgd->probe_timeout);
  962. }
  963. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  964. bool beacon)
  965. {
  966. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  967. bool already = false;
  968. if (!ieee80211_sdata_running(sdata))
  969. return;
  970. if (sdata->local->scanning)
  971. return;
  972. if (sdata->local->tmp_channel)
  973. return;
  974. mutex_lock(&ifmgd->mtx);
  975. if (!ifmgd->associated)
  976. goto out;
  977. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  978. if (beacon && net_ratelimit())
  979. printk(KERN_DEBUG "%s: detected beacon loss from AP "
  980. "- sending probe request\n", sdata->name);
  981. #endif
  982. /*
  983. * The driver/our work has already reported this event or the
  984. * connection monitoring has kicked in and we have already sent
  985. * a probe request. Or maybe the AP died and the driver keeps
  986. * reporting until we disassociate...
  987. *
  988. * In either case we have to ignore the current call to this
  989. * function (except for setting the correct probe reason bit)
  990. * because otherwise we would reset the timer every time and
  991. * never check whether we received a probe response!
  992. */
  993. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  994. IEEE80211_STA_CONNECTION_POLL))
  995. already = true;
  996. if (beacon)
  997. ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
  998. else
  999. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  1000. if (already)
  1001. goto out;
  1002. mutex_lock(&sdata->local->iflist_mtx);
  1003. ieee80211_recalc_ps(sdata->local, -1);
  1004. mutex_unlock(&sdata->local->iflist_mtx);
  1005. ifmgd->probe_send_count = 0;
  1006. ieee80211_mgd_probe_ap_send(sdata);
  1007. out:
  1008. mutex_unlock(&ifmgd->mtx);
  1009. }
  1010. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  1011. struct ieee80211_vif *vif)
  1012. {
  1013. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1014. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1015. struct sk_buff *skb;
  1016. const u8 *ssid;
  1017. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1018. return NULL;
  1019. ASSERT_MGD_MTX(ifmgd);
  1020. if (!ifmgd->associated)
  1021. return NULL;
  1022. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1023. skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
  1024. ssid + 2, ssid[1], NULL, 0);
  1025. return skb;
  1026. }
  1027. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  1028. static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
  1029. {
  1030. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1031. struct ieee80211_local *local = sdata->local;
  1032. u8 bssid[ETH_ALEN];
  1033. mutex_lock(&ifmgd->mtx);
  1034. if (!ifmgd->associated) {
  1035. mutex_unlock(&ifmgd->mtx);
  1036. return;
  1037. }
  1038. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  1039. printk(KERN_DEBUG "%s: Connection to AP %pM lost.\n",
  1040. sdata->name, bssid);
  1041. ieee80211_set_disassoc(sdata, true, true);
  1042. mutex_unlock(&ifmgd->mtx);
  1043. mutex_lock(&local->mtx);
  1044. ieee80211_recalc_idle(local);
  1045. mutex_unlock(&local->mtx);
  1046. /*
  1047. * must be outside lock due to cfg80211,
  1048. * but that's not a problem.
  1049. */
  1050. ieee80211_send_deauth_disassoc(sdata, bssid,
  1051. IEEE80211_STYPE_DEAUTH,
  1052. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1053. NULL, true);
  1054. }
  1055. void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  1056. {
  1057. struct ieee80211_sub_if_data *sdata =
  1058. container_of(work, struct ieee80211_sub_if_data,
  1059. u.mgd.beacon_connection_loss_work);
  1060. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  1061. __ieee80211_connection_loss(sdata);
  1062. else
  1063. ieee80211_mgd_probe_ap(sdata, true);
  1064. }
  1065. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  1066. {
  1067. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1068. struct ieee80211_hw *hw = &sdata->local->hw;
  1069. trace_api_beacon_loss(sdata);
  1070. WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
  1071. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1072. }
  1073. EXPORT_SYMBOL(ieee80211_beacon_loss);
  1074. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  1075. {
  1076. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1077. struct ieee80211_hw *hw = &sdata->local->hw;
  1078. trace_api_connection_loss(sdata);
  1079. WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
  1080. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1081. }
  1082. EXPORT_SYMBOL(ieee80211_connection_loss);
  1083. static enum rx_mgmt_action __must_check
  1084. ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1085. struct ieee80211_mgmt *mgmt, size_t len)
  1086. {
  1087. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1088. const u8 *bssid = NULL;
  1089. u16 reason_code;
  1090. if (len < 24 + 2)
  1091. return RX_MGMT_NONE;
  1092. ASSERT_MGD_MTX(ifmgd);
  1093. bssid = ifmgd->associated->bssid;
  1094. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1095. printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
  1096. sdata->name, bssid, reason_code);
  1097. ieee80211_set_disassoc(sdata, true, false);
  1098. mutex_lock(&sdata->local->mtx);
  1099. ieee80211_recalc_idle(sdata->local);
  1100. mutex_unlock(&sdata->local->mtx);
  1101. return RX_MGMT_CFG80211_DEAUTH;
  1102. }
  1103. static enum rx_mgmt_action __must_check
  1104. ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1105. struct ieee80211_mgmt *mgmt, size_t len)
  1106. {
  1107. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1108. u16 reason_code;
  1109. if (len < 24 + 2)
  1110. return RX_MGMT_NONE;
  1111. ASSERT_MGD_MTX(ifmgd);
  1112. if (WARN_ON(!ifmgd->associated))
  1113. return RX_MGMT_NONE;
  1114. if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
  1115. return RX_MGMT_NONE;
  1116. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1117. printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
  1118. sdata->name, mgmt->sa, reason_code);
  1119. ieee80211_set_disassoc(sdata, true, false);
  1120. mutex_lock(&sdata->local->mtx);
  1121. ieee80211_recalc_idle(sdata->local);
  1122. mutex_unlock(&sdata->local->mtx);
  1123. return RX_MGMT_CFG80211_DISASSOC;
  1124. }
  1125. static bool ieee80211_assoc_success(struct ieee80211_work *wk,
  1126. struct ieee80211_mgmt *mgmt, size_t len)
  1127. {
  1128. struct ieee80211_sub_if_data *sdata = wk->sdata;
  1129. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1130. struct ieee80211_local *local = sdata->local;
  1131. struct ieee80211_supported_band *sband;
  1132. struct sta_info *sta;
  1133. struct cfg80211_bss *cbss = wk->assoc.bss;
  1134. u8 *pos;
  1135. u32 rates, basic_rates;
  1136. u16 capab_info, aid;
  1137. struct ieee802_11_elems elems;
  1138. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1139. u32 changed = 0;
  1140. int i, j, err;
  1141. bool have_higher_than_11mbit = false;
  1142. u16 ap_ht_cap_flags;
  1143. /* AssocResp and ReassocResp have identical structure */
  1144. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1145. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1146. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1147. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  1148. "set\n", sdata->name, aid);
  1149. aid &= ~(BIT(15) | BIT(14));
  1150. pos = mgmt->u.assoc_resp.variable;
  1151. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1152. if (!elems.supp_rates) {
  1153. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  1154. sdata->name);
  1155. return false;
  1156. }
  1157. ifmgd->aid = aid;
  1158. sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  1159. if (!sta) {
  1160. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  1161. " the AP\n", sdata->name);
  1162. return false;
  1163. }
  1164. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
  1165. WLAN_STA_ASSOC_AP);
  1166. if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  1167. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  1168. rates = 0;
  1169. basic_rates = 0;
  1170. sband = local->hw.wiphy->bands[wk->chan->band];
  1171. for (i = 0; i < elems.supp_rates_len; i++) {
  1172. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  1173. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1174. if (rate > 110)
  1175. have_higher_than_11mbit = true;
  1176. for (j = 0; j < sband->n_bitrates; j++) {
  1177. if (sband->bitrates[j].bitrate == rate) {
  1178. rates |= BIT(j);
  1179. if (is_basic)
  1180. basic_rates |= BIT(j);
  1181. break;
  1182. }
  1183. }
  1184. }
  1185. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  1186. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  1187. bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
  1188. if (rate > 110)
  1189. have_higher_than_11mbit = true;
  1190. for (j = 0; j < sband->n_bitrates; j++) {
  1191. if (sband->bitrates[j].bitrate == rate) {
  1192. rates |= BIT(j);
  1193. if (is_basic)
  1194. basic_rates |= BIT(j);
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. sta->sta.supp_rates[wk->chan->band] = rates;
  1200. sdata->vif.bss_conf.basic_rates = basic_rates;
  1201. /* cf. IEEE 802.11 9.2.12 */
  1202. if (wk->chan->band == IEEE80211_BAND_2GHZ &&
  1203. have_higher_than_11mbit)
  1204. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  1205. else
  1206. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  1207. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1208. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1209. elems.ht_cap_elem, &sta->sta.ht_cap);
  1210. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1211. rate_control_rate_init(sta);
  1212. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  1213. set_sta_flags(sta, WLAN_STA_MFP);
  1214. if (elems.wmm_param)
  1215. set_sta_flags(sta, WLAN_STA_WME);
  1216. err = sta_info_insert(sta);
  1217. sta = NULL;
  1218. if (err) {
  1219. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  1220. " the AP (error %d)\n", sdata->name, err);
  1221. return false;
  1222. }
  1223. /*
  1224. * Always handle WMM once after association regardless
  1225. * of the first value the AP uses. Setting -1 here has
  1226. * that effect because the AP values is an unsigned
  1227. * 4-bit value.
  1228. */
  1229. ifmgd->wmm_last_param_set = -1;
  1230. if (elems.wmm_param)
  1231. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  1232. elems.wmm_param_len);
  1233. else
  1234. ieee80211_set_wmm_default(sdata);
  1235. local->oper_channel = wk->chan;
  1236. if (elems.ht_info_elem && elems.wmm_param &&
  1237. (sdata->local->hw.queues >= 4) &&
  1238. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1239. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1240. cbss->bssid, ap_ht_cap_flags);
  1241. /* set AID and assoc capability,
  1242. * ieee80211_set_associated() will tell the driver */
  1243. bss_conf->aid = aid;
  1244. bss_conf->assoc_capability = capab_info;
  1245. ieee80211_set_associated(sdata, cbss, changed);
  1246. /*
  1247. * If we're using 4-addr mode, let the AP know that we're
  1248. * doing so, so that it can create the STA VLAN on its side
  1249. */
  1250. if (ifmgd->use_4addr)
  1251. ieee80211_send_4addr_nullfunc(local, sdata);
  1252. /*
  1253. * Start timer to probe the connection to the AP now.
  1254. * Also start the timer that will detect beacon loss.
  1255. */
  1256. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  1257. ieee80211_sta_reset_beacon_monitor(sdata);
  1258. return true;
  1259. }
  1260. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1261. struct ieee80211_mgmt *mgmt,
  1262. size_t len,
  1263. struct ieee80211_rx_status *rx_status,
  1264. struct ieee802_11_elems *elems,
  1265. bool beacon)
  1266. {
  1267. struct ieee80211_local *local = sdata->local;
  1268. int freq;
  1269. struct ieee80211_bss *bss;
  1270. struct ieee80211_channel *channel;
  1271. bool need_ps = false;
  1272. if (sdata->u.mgd.associated) {
  1273. bss = (void *)sdata->u.mgd.associated->priv;
  1274. /* not previously set so we may need to recalc */
  1275. need_ps = !bss->dtim_period;
  1276. }
  1277. if (elems->ds_params && elems->ds_params_len == 1)
  1278. freq = ieee80211_channel_to_frequency(elems->ds_params[0],
  1279. rx_status->band);
  1280. else
  1281. freq = rx_status->freq;
  1282. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1283. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1284. return;
  1285. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1286. channel, beacon);
  1287. if (bss)
  1288. ieee80211_rx_bss_put(local, bss);
  1289. if (!sdata->u.mgd.associated)
  1290. return;
  1291. if (need_ps) {
  1292. mutex_lock(&local->iflist_mtx);
  1293. ieee80211_recalc_ps(local, -1);
  1294. mutex_unlock(&local->iflist_mtx);
  1295. }
  1296. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  1297. (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
  1298. ETH_ALEN) == 0)) {
  1299. struct ieee80211_channel_sw_ie *sw_elem =
  1300. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  1301. ieee80211_sta_process_chanswitch(sdata, sw_elem,
  1302. bss, rx_status->mactime);
  1303. }
  1304. }
  1305. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1306. struct sk_buff *skb)
  1307. {
  1308. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1309. struct ieee80211_if_managed *ifmgd;
  1310. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  1311. size_t baselen, len = skb->len;
  1312. struct ieee802_11_elems elems;
  1313. ifmgd = &sdata->u.mgd;
  1314. ASSERT_MGD_MTX(ifmgd);
  1315. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  1316. return; /* ignore ProbeResp to foreign address */
  1317. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1318. if (baselen > len)
  1319. return;
  1320. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1321. &elems);
  1322. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1323. if (ifmgd->associated &&
  1324. memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0)
  1325. ieee80211_reset_ap_probe(sdata);
  1326. }
  1327. /*
  1328. * This is the canonical list of information elements we care about,
  1329. * the filter code also gives us all changes to the Microsoft OUI
  1330. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  1331. *
  1332. * We implement beacon filtering in software since that means we can
  1333. * avoid processing the frame here and in cfg80211, and userspace
  1334. * will not be able to tell whether the hardware supports it or not.
  1335. *
  1336. * XXX: This list needs to be dynamic -- userspace needs to be able to
  1337. * add items it requires. It also needs to be able to tell us to
  1338. * look out for other vendor IEs.
  1339. */
  1340. static const u64 care_about_ies =
  1341. (1ULL << WLAN_EID_COUNTRY) |
  1342. (1ULL << WLAN_EID_ERP_INFO) |
  1343. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  1344. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  1345. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1346. (1ULL << WLAN_EID_HT_INFORMATION);
  1347. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1348. struct ieee80211_mgmt *mgmt,
  1349. size_t len,
  1350. struct ieee80211_rx_status *rx_status)
  1351. {
  1352. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1353. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1354. size_t baselen;
  1355. struct ieee802_11_elems elems;
  1356. struct ieee80211_local *local = sdata->local;
  1357. u32 changed = 0;
  1358. bool erp_valid, directed_tim = false;
  1359. u8 erp_value = 0;
  1360. u32 ncrc;
  1361. u8 *bssid;
  1362. ASSERT_MGD_MTX(ifmgd);
  1363. /* Process beacon from the current BSS */
  1364. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1365. if (baselen > len)
  1366. return;
  1367. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1368. return;
  1369. /*
  1370. * We might have received a number of frames, among them a
  1371. * disassoc frame and a beacon...
  1372. */
  1373. if (!ifmgd->associated)
  1374. return;
  1375. bssid = ifmgd->associated->bssid;
  1376. /*
  1377. * And in theory even frames from a different AP we were just
  1378. * associated to a split-second ago!
  1379. */
  1380. if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
  1381. return;
  1382. /* Track average RSSI from the Beacon frames of the current AP */
  1383. ifmgd->last_beacon_signal = rx_status->signal;
  1384. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  1385. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  1386. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  1387. ifmgd->last_cqm_event_signal = 0;
  1388. ifmgd->count_beacon_signal = 1;
  1389. } else {
  1390. ifmgd->ave_beacon_signal =
  1391. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  1392. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  1393. ifmgd->ave_beacon_signal) / 16;
  1394. ifmgd->count_beacon_signal++;
  1395. }
  1396. if (bss_conf->cqm_rssi_thold &&
  1397. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  1398. !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
  1399. int sig = ifmgd->ave_beacon_signal / 16;
  1400. int last_event = ifmgd->last_cqm_event_signal;
  1401. int thold = bss_conf->cqm_rssi_thold;
  1402. int hyst = bss_conf->cqm_rssi_hyst;
  1403. if (sig < thold &&
  1404. (last_event == 0 || sig < last_event - hyst)) {
  1405. ifmgd->last_cqm_event_signal = sig;
  1406. ieee80211_cqm_rssi_notify(
  1407. &sdata->vif,
  1408. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  1409. GFP_KERNEL);
  1410. } else if (sig > thold &&
  1411. (last_event == 0 || sig > last_event + hyst)) {
  1412. ifmgd->last_cqm_event_signal = sig;
  1413. ieee80211_cqm_rssi_notify(
  1414. &sdata->vif,
  1415. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  1416. GFP_KERNEL);
  1417. }
  1418. }
  1419. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  1420. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1421. if (net_ratelimit()) {
  1422. printk(KERN_DEBUG "%s: cancelling probereq poll due "
  1423. "to a received beacon\n", sdata->name);
  1424. }
  1425. #endif
  1426. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  1427. mutex_lock(&local->iflist_mtx);
  1428. ieee80211_recalc_ps(local, -1);
  1429. mutex_unlock(&local->iflist_mtx);
  1430. }
  1431. /*
  1432. * Push the beacon loss detection into the future since
  1433. * we are processing a beacon from the AP just now.
  1434. */
  1435. ieee80211_sta_reset_beacon_monitor(sdata);
  1436. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  1437. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  1438. len - baselen, &elems,
  1439. care_about_ies, ncrc);
  1440. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1441. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  1442. ifmgd->aid);
  1443. if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
  1444. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  1445. true);
  1446. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  1447. elems.wmm_param_len);
  1448. }
  1449. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  1450. if (directed_tim) {
  1451. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1452. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1453. ieee80211_hw_config(local,
  1454. IEEE80211_CONF_CHANGE_PS);
  1455. ieee80211_send_nullfunc(local, sdata, 0);
  1456. } else {
  1457. local->pspolling = true;
  1458. /*
  1459. * Here is assumed that the driver will be
  1460. * able to send ps-poll frame and receive a
  1461. * response even though power save mode is
  1462. * enabled, but some drivers might require
  1463. * to disable power save here. This needs
  1464. * to be investigated.
  1465. */
  1466. ieee80211_send_pspoll(local, sdata);
  1467. }
  1468. }
  1469. }
  1470. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  1471. return;
  1472. ifmgd->beacon_crc = ncrc;
  1473. ifmgd->beacon_crc_valid = true;
  1474. if (elems.erp_info && elems.erp_info_len >= 1) {
  1475. erp_valid = true;
  1476. erp_value = elems.erp_info[0];
  1477. } else {
  1478. erp_valid = false;
  1479. }
  1480. changed |= ieee80211_handle_bss_capability(sdata,
  1481. le16_to_cpu(mgmt->u.beacon.capab_info),
  1482. erp_valid, erp_value);
  1483. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  1484. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
  1485. struct sta_info *sta;
  1486. struct ieee80211_supported_band *sband;
  1487. u16 ap_ht_cap_flags;
  1488. rcu_read_lock();
  1489. sta = sta_info_get(sdata, bssid);
  1490. if (WARN_ON(!sta)) {
  1491. rcu_read_unlock();
  1492. return;
  1493. }
  1494. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1495. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1496. elems.ht_cap_elem, &sta->sta.ht_cap);
  1497. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1498. rcu_read_unlock();
  1499. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1500. bssid, ap_ht_cap_flags);
  1501. }
  1502. /* Note: country IE parsing is done for us by cfg80211 */
  1503. if (elems.country_elem) {
  1504. /* TODO: IBSS also needs this */
  1505. if (elems.pwr_constr_elem)
  1506. ieee80211_handle_pwr_constr(sdata,
  1507. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1508. elems.pwr_constr_elem,
  1509. elems.pwr_constr_elem_len);
  1510. }
  1511. ieee80211_bss_info_change_notify(sdata, changed);
  1512. }
  1513. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1514. struct sk_buff *skb)
  1515. {
  1516. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1517. struct ieee80211_rx_status *rx_status;
  1518. struct ieee80211_mgmt *mgmt;
  1519. enum rx_mgmt_action rma = RX_MGMT_NONE;
  1520. u16 fc;
  1521. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1522. mgmt = (struct ieee80211_mgmt *) skb->data;
  1523. fc = le16_to_cpu(mgmt->frame_control);
  1524. mutex_lock(&ifmgd->mtx);
  1525. if (ifmgd->associated &&
  1526. memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
  1527. switch (fc & IEEE80211_FCTL_STYPE) {
  1528. case IEEE80211_STYPE_BEACON:
  1529. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  1530. rx_status);
  1531. break;
  1532. case IEEE80211_STYPE_PROBE_RESP:
  1533. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  1534. break;
  1535. case IEEE80211_STYPE_DEAUTH:
  1536. rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  1537. break;
  1538. case IEEE80211_STYPE_DISASSOC:
  1539. rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  1540. break;
  1541. case IEEE80211_STYPE_ACTION:
  1542. switch (mgmt->u.action.category) {
  1543. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1544. ieee80211_sta_process_chanswitch(sdata,
  1545. &mgmt->u.action.u.chan_switch.sw_elem,
  1546. (void *)ifmgd->associated->priv,
  1547. rx_status->mactime);
  1548. break;
  1549. }
  1550. }
  1551. mutex_unlock(&ifmgd->mtx);
  1552. switch (rma) {
  1553. case RX_MGMT_NONE:
  1554. /* no action */
  1555. break;
  1556. case RX_MGMT_CFG80211_DEAUTH:
  1557. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  1558. break;
  1559. case RX_MGMT_CFG80211_DISASSOC:
  1560. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  1561. break;
  1562. default:
  1563. WARN(1, "unexpected: %d", rma);
  1564. }
  1565. return;
  1566. }
  1567. mutex_unlock(&ifmgd->mtx);
  1568. if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
  1569. (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH) {
  1570. struct ieee80211_local *local = sdata->local;
  1571. struct ieee80211_work *wk;
  1572. mutex_lock(&local->mtx);
  1573. list_for_each_entry(wk, &local->work_list, list) {
  1574. if (wk->sdata != sdata)
  1575. continue;
  1576. if (wk->type != IEEE80211_WORK_ASSOC &&
  1577. wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
  1578. continue;
  1579. if (memcmp(mgmt->bssid, wk->filter_ta, ETH_ALEN))
  1580. continue;
  1581. if (memcmp(mgmt->sa, wk->filter_ta, ETH_ALEN))
  1582. continue;
  1583. /*
  1584. * Printing the message only here means we can't
  1585. * spuriously print it, but it also means that it
  1586. * won't be printed when the frame comes in before
  1587. * we even tried to associate or in similar cases.
  1588. *
  1589. * Ultimately, I suspect cfg80211 should print the
  1590. * messages instead.
  1591. */
  1592. printk(KERN_DEBUG
  1593. "%s: deauthenticated from %pM (Reason: %u)\n",
  1594. sdata->name, mgmt->bssid,
  1595. le16_to_cpu(mgmt->u.deauth.reason_code));
  1596. list_del_rcu(&wk->list);
  1597. free_work(wk);
  1598. break;
  1599. }
  1600. mutex_unlock(&local->mtx);
  1601. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  1602. }
  1603. }
  1604. static void ieee80211_sta_timer(unsigned long data)
  1605. {
  1606. struct ieee80211_sub_if_data *sdata =
  1607. (struct ieee80211_sub_if_data *) data;
  1608. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1609. struct ieee80211_local *local = sdata->local;
  1610. if (local->quiescing) {
  1611. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1612. return;
  1613. }
  1614. ieee80211_queue_work(&local->hw, &sdata->work);
  1615. }
  1616. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  1617. u8 *bssid)
  1618. {
  1619. struct ieee80211_local *local = sdata->local;
  1620. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1621. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1622. IEEE80211_STA_BEACON_POLL);
  1623. ieee80211_set_disassoc(sdata, true, true);
  1624. mutex_unlock(&ifmgd->mtx);
  1625. mutex_lock(&local->mtx);
  1626. ieee80211_recalc_idle(local);
  1627. mutex_unlock(&local->mtx);
  1628. /*
  1629. * must be outside lock due to cfg80211,
  1630. * but that's not a problem.
  1631. */
  1632. ieee80211_send_deauth_disassoc(sdata, bssid,
  1633. IEEE80211_STYPE_DEAUTH,
  1634. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1635. NULL, true);
  1636. mutex_lock(&ifmgd->mtx);
  1637. }
  1638. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  1639. {
  1640. struct ieee80211_local *local = sdata->local;
  1641. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1642. /* then process the rest of the work */
  1643. mutex_lock(&ifmgd->mtx);
  1644. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1645. IEEE80211_STA_CONNECTION_POLL) &&
  1646. ifmgd->associated) {
  1647. u8 bssid[ETH_ALEN];
  1648. int max_tries;
  1649. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  1650. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1651. max_tries = max_nullfunc_tries;
  1652. else
  1653. max_tries = max_probe_tries;
  1654. /* ACK received for nullfunc probing frame */
  1655. if (!ifmgd->probe_send_count)
  1656. ieee80211_reset_ap_probe(sdata);
  1657. else if (ifmgd->nullfunc_failed) {
  1658. if (ifmgd->probe_send_count < max_tries) {
  1659. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1660. wiphy_debug(local->hw.wiphy,
  1661. "%s: No ack for nullfunc frame to"
  1662. " AP %pM, try %d/%i\n",
  1663. sdata->name, bssid,
  1664. ifmgd->probe_send_count, max_tries);
  1665. #endif
  1666. ieee80211_mgd_probe_ap_send(sdata);
  1667. } else {
  1668. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1669. wiphy_debug(local->hw.wiphy,
  1670. "%s: No ack for nullfunc frame to"
  1671. " AP %pM, disconnecting.\n",
  1672. sdata->name, bssid);
  1673. #endif
  1674. ieee80211_sta_connection_lost(sdata, bssid);
  1675. }
  1676. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  1677. run_again(ifmgd, ifmgd->probe_timeout);
  1678. else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  1679. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1680. wiphy_debug(local->hw.wiphy,
  1681. "%s: Failed to send nullfunc to AP %pM"
  1682. " after %dms, disconnecting.\n",
  1683. sdata->name,
  1684. bssid, probe_wait_ms);
  1685. #endif
  1686. ieee80211_sta_connection_lost(sdata, bssid);
  1687. } else if (ifmgd->probe_send_count < max_tries) {
  1688. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1689. wiphy_debug(local->hw.wiphy,
  1690. "%s: No probe response from AP %pM"
  1691. " after %dms, try %d/%i\n",
  1692. sdata->name,
  1693. bssid, probe_wait_ms,
  1694. ifmgd->probe_send_count, max_tries);
  1695. #endif
  1696. ieee80211_mgd_probe_ap_send(sdata);
  1697. } else {
  1698. /*
  1699. * We actually lost the connection ... or did we?
  1700. * Let's make sure!
  1701. */
  1702. wiphy_debug(local->hw.wiphy,
  1703. "%s: No probe response from AP %pM"
  1704. " after %dms, disconnecting.\n",
  1705. sdata->name,
  1706. bssid, probe_wait_ms);
  1707. ieee80211_sta_connection_lost(sdata, bssid);
  1708. }
  1709. }
  1710. mutex_unlock(&ifmgd->mtx);
  1711. }
  1712. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  1713. {
  1714. struct ieee80211_sub_if_data *sdata =
  1715. (struct ieee80211_sub_if_data *) data;
  1716. struct ieee80211_local *local = sdata->local;
  1717. if (local->quiescing)
  1718. return;
  1719. ieee80211_queue_work(&sdata->local->hw,
  1720. &sdata->u.mgd.beacon_connection_loss_work);
  1721. }
  1722. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  1723. {
  1724. struct ieee80211_sub_if_data *sdata =
  1725. (struct ieee80211_sub_if_data *) data;
  1726. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1727. struct ieee80211_local *local = sdata->local;
  1728. if (local->quiescing)
  1729. return;
  1730. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  1731. }
  1732. static void ieee80211_sta_monitor_work(struct work_struct *work)
  1733. {
  1734. struct ieee80211_sub_if_data *sdata =
  1735. container_of(work, struct ieee80211_sub_if_data,
  1736. u.mgd.monitor_work);
  1737. ieee80211_mgd_probe_ap(sdata, false);
  1738. }
  1739. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  1740. {
  1741. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1742. sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
  1743. IEEE80211_STA_CONNECTION_POLL);
  1744. /* let's probe the connection once */
  1745. ieee80211_queue_work(&sdata->local->hw,
  1746. &sdata->u.mgd.monitor_work);
  1747. /* and do all the other regular work too */
  1748. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1749. }
  1750. }
  1751. #ifdef CONFIG_PM
  1752. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  1753. {
  1754. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1755. /*
  1756. * we need to use atomic bitops for the running bits
  1757. * only because both timers might fire at the same
  1758. * time -- the code here is properly synchronised.
  1759. */
  1760. cancel_work_sync(&ifmgd->request_smps_work);
  1761. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  1762. if (del_timer_sync(&ifmgd->timer))
  1763. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1764. cancel_work_sync(&ifmgd->chswitch_work);
  1765. if (del_timer_sync(&ifmgd->chswitch_timer))
  1766. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  1767. cancel_work_sync(&ifmgd->monitor_work);
  1768. /* these will just be re-established on connection */
  1769. del_timer_sync(&ifmgd->conn_mon_timer);
  1770. del_timer_sync(&ifmgd->bcn_mon_timer);
  1771. }
  1772. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  1773. {
  1774. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1775. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  1776. add_timer(&ifmgd->timer);
  1777. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  1778. add_timer(&ifmgd->chswitch_timer);
  1779. ieee80211_sta_reset_beacon_monitor(sdata);
  1780. ieee80211_restart_sta_timer(sdata);
  1781. }
  1782. #endif
  1783. /* interface setup */
  1784. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1785. {
  1786. struct ieee80211_if_managed *ifmgd;
  1787. ifmgd = &sdata->u.mgd;
  1788. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  1789. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  1790. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  1791. ieee80211_beacon_connection_loss_work);
  1792. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
  1793. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  1794. (unsigned long) sdata);
  1795. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  1796. (unsigned long) sdata);
  1797. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  1798. (unsigned long) sdata);
  1799. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  1800. (unsigned long) sdata);
  1801. ifmgd->flags = 0;
  1802. mutex_init(&ifmgd->mtx);
  1803. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  1804. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  1805. else
  1806. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  1807. }
  1808. /* scan finished notification */
  1809. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  1810. {
  1811. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  1812. /* Restart STA timers */
  1813. rcu_read_lock();
  1814. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  1815. ieee80211_restart_sta_timer(sdata);
  1816. rcu_read_unlock();
  1817. }
  1818. int ieee80211_max_network_latency(struct notifier_block *nb,
  1819. unsigned long data, void *dummy)
  1820. {
  1821. s32 latency_usec = (s32) data;
  1822. struct ieee80211_local *local =
  1823. container_of(nb, struct ieee80211_local,
  1824. network_latency_notifier);
  1825. mutex_lock(&local->iflist_mtx);
  1826. ieee80211_recalc_ps(local, latency_usec);
  1827. mutex_unlock(&local->iflist_mtx);
  1828. return 0;
  1829. }
  1830. /* config hooks */
  1831. static enum work_done_result
  1832. ieee80211_probe_auth_done(struct ieee80211_work *wk,
  1833. struct sk_buff *skb)
  1834. {
  1835. if (!skb) {
  1836. cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
  1837. return WORK_DONE_DESTROY;
  1838. }
  1839. if (wk->type == IEEE80211_WORK_AUTH) {
  1840. cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
  1841. return WORK_DONE_DESTROY;
  1842. }
  1843. mutex_lock(&wk->sdata->u.mgd.mtx);
  1844. ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
  1845. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1846. wk->type = IEEE80211_WORK_AUTH;
  1847. wk->probe_auth.tries = 0;
  1848. return WORK_DONE_REQUEUE;
  1849. }
  1850. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  1851. struct cfg80211_auth_request *req)
  1852. {
  1853. const u8 *ssid;
  1854. struct ieee80211_work *wk;
  1855. u16 auth_alg;
  1856. if (req->local_state_change)
  1857. return 0; /* no need to update mac80211 state */
  1858. switch (req->auth_type) {
  1859. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  1860. auth_alg = WLAN_AUTH_OPEN;
  1861. break;
  1862. case NL80211_AUTHTYPE_SHARED_KEY:
  1863. if (IS_ERR(sdata->local->wep_tx_tfm))
  1864. return -EOPNOTSUPP;
  1865. auth_alg = WLAN_AUTH_SHARED_KEY;
  1866. break;
  1867. case NL80211_AUTHTYPE_FT:
  1868. auth_alg = WLAN_AUTH_FT;
  1869. break;
  1870. case NL80211_AUTHTYPE_NETWORK_EAP:
  1871. auth_alg = WLAN_AUTH_LEAP;
  1872. break;
  1873. default:
  1874. return -EOPNOTSUPP;
  1875. }
  1876. wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
  1877. if (!wk)
  1878. return -ENOMEM;
  1879. memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
  1880. if (req->ie && req->ie_len) {
  1881. memcpy(wk->ie, req->ie, req->ie_len);
  1882. wk->ie_len = req->ie_len;
  1883. }
  1884. if (req->key && req->key_len) {
  1885. wk->probe_auth.key_len = req->key_len;
  1886. wk->probe_auth.key_idx = req->key_idx;
  1887. memcpy(wk->probe_auth.key, req->key, req->key_len);
  1888. }
  1889. ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  1890. memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
  1891. wk->probe_auth.ssid_len = ssid[1];
  1892. wk->probe_auth.algorithm = auth_alg;
  1893. wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
  1894. /* if we already have a probe, don't probe again */
  1895. if (req->bss->proberesp_ies)
  1896. wk->type = IEEE80211_WORK_AUTH;
  1897. else
  1898. wk->type = IEEE80211_WORK_DIRECT_PROBE;
  1899. wk->chan = req->bss->channel;
  1900. wk->sdata = sdata;
  1901. wk->done = ieee80211_probe_auth_done;
  1902. ieee80211_add_work(wk);
  1903. return 0;
  1904. }
  1905. static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
  1906. struct sk_buff *skb)
  1907. {
  1908. struct ieee80211_mgmt *mgmt;
  1909. struct ieee80211_rx_status *rx_status;
  1910. struct ieee802_11_elems elems;
  1911. u16 status;
  1912. if (!skb) {
  1913. cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
  1914. return WORK_DONE_DESTROY;
  1915. }
  1916. if (wk->type == IEEE80211_WORK_ASSOC_BEACON_WAIT) {
  1917. mutex_lock(&wk->sdata->u.mgd.mtx);
  1918. rx_status = (void *) skb->cb;
  1919. ieee802_11_parse_elems(skb->data + 24 + 12, skb->len - 24 - 12, &elems);
  1920. ieee80211_rx_bss_info(wk->sdata, (void *)skb->data, skb->len, rx_status,
  1921. &elems, true);
  1922. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1923. wk->type = IEEE80211_WORK_ASSOC;
  1924. /* not really done yet */
  1925. return WORK_DONE_REQUEUE;
  1926. }
  1927. mgmt = (void *)skb->data;
  1928. status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1929. if (status == WLAN_STATUS_SUCCESS) {
  1930. mutex_lock(&wk->sdata->u.mgd.mtx);
  1931. if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
  1932. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1933. /* oops -- internal error -- send timeout for now */
  1934. cfg80211_send_assoc_timeout(wk->sdata->dev,
  1935. wk->filter_ta);
  1936. return WORK_DONE_DESTROY;
  1937. }
  1938. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1939. }
  1940. cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
  1941. return WORK_DONE_DESTROY;
  1942. }
  1943. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  1944. struct cfg80211_assoc_request *req)
  1945. {
  1946. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1947. struct ieee80211_bss *bss = (void *)req->bss->priv;
  1948. struct ieee80211_work *wk;
  1949. const u8 *ssid;
  1950. int i;
  1951. mutex_lock(&ifmgd->mtx);
  1952. if (ifmgd->associated) {
  1953. if (!req->prev_bssid ||
  1954. memcmp(req->prev_bssid, ifmgd->associated->bssid,
  1955. ETH_ALEN)) {
  1956. /*
  1957. * We are already associated and the request was not a
  1958. * reassociation request from the current BSS, so
  1959. * reject it.
  1960. */
  1961. mutex_unlock(&ifmgd->mtx);
  1962. return -EALREADY;
  1963. }
  1964. /* Trying to reassociate - clear previous association state */
  1965. ieee80211_set_disassoc(sdata, true, false);
  1966. }
  1967. mutex_unlock(&ifmgd->mtx);
  1968. wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
  1969. if (!wk)
  1970. return -ENOMEM;
  1971. ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
  1972. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  1973. ifmgd->beacon_crc_valid = false;
  1974. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
  1975. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  1976. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  1977. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
  1978. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  1979. if (req->ie && req->ie_len) {
  1980. memcpy(wk->ie, req->ie, req->ie_len);
  1981. wk->ie_len = req->ie_len;
  1982. } else
  1983. wk->ie_len = 0;
  1984. wk->assoc.bss = req->bss;
  1985. memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
  1986. /* new association always uses requested smps mode */
  1987. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  1988. if (ifmgd->powersave)
  1989. ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
  1990. else
  1991. ifmgd->ap_smps = IEEE80211_SMPS_OFF;
  1992. } else
  1993. ifmgd->ap_smps = ifmgd->req_smps;
  1994. wk->assoc.smps = ifmgd->ap_smps;
  1995. /*
  1996. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  1997. * We still associate in non-HT mode (11a/b/g) if any one of these
  1998. * ciphers is configured as pairwise.
  1999. * We can set this to true for non-11n hardware, that'll be checked
  2000. * separately along with the peer capabilities.
  2001. */
  2002. wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
  2003. wk->assoc.capability = req->bss->capability;
  2004. wk->assoc.wmm_used = bss->wmm_used;
  2005. wk->assoc.supp_rates = bss->supp_rates;
  2006. wk->assoc.supp_rates_len = bss->supp_rates_len;
  2007. wk->assoc.ht_information_ie =
  2008. ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
  2009. if (bss->wmm_used && bss->uapsd_supported &&
  2010. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
  2011. wk->assoc.uapsd_used = true;
  2012. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  2013. } else {
  2014. wk->assoc.uapsd_used = false;
  2015. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  2016. }
  2017. ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  2018. memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
  2019. wk->assoc.ssid_len = ssid[1];
  2020. if (req->prev_bssid)
  2021. memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
  2022. wk->chan = req->bss->channel;
  2023. wk->sdata = sdata;
  2024. wk->done = ieee80211_assoc_done;
  2025. if (!bss->dtim_period &&
  2026. sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
  2027. wk->type = IEEE80211_WORK_ASSOC_BEACON_WAIT;
  2028. else
  2029. wk->type = IEEE80211_WORK_ASSOC;
  2030. if (req->use_mfp) {
  2031. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  2032. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  2033. } else {
  2034. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  2035. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  2036. }
  2037. if (req->crypto.control_port)
  2038. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  2039. else
  2040. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  2041. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  2042. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  2043. ieee80211_add_work(wk);
  2044. return 0;
  2045. }
  2046. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  2047. struct cfg80211_deauth_request *req,
  2048. void *cookie)
  2049. {
  2050. struct ieee80211_local *local = sdata->local;
  2051. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2052. struct ieee80211_work *wk;
  2053. u8 bssid[ETH_ALEN];
  2054. bool assoc_bss = false;
  2055. mutex_lock(&ifmgd->mtx);
  2056. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  2057. if (ifmgd->associated == req->bss) {
  2058. ieee80211_set_disassoc(sdata, false, true);
  2059. mutex_unlock(&ifmgd->mtx);
  2060. assoc_bss = true;
  2061. } else {
  2062. bool not_auth_yet = false;
  2063. mutex_unlock(&ifmgd->mtx);
  2064. mutex_lock(&local->mtx);
  2065. list_for_each_entry(wk, &local->work_list, list) {
  2066. if (wk->sdata != sdata)
  2067. continue;
  2068. if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
  2069. wk->type != IEEE80211_WORK_AUTH &&
  2070. wk->type != IEEE80211_WORK_ASSOC &&
  2071. wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
  2072. continue;
  2073. if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
  2074. continue;
  2075. not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
  2076. list_del_rcu(&wk->list);
  2077. free_work(wk);
  2078. break;
  2079. }
  2080. mutex_unlock(&local->mtx);
  2081. /*
  2082. * If somebody requests authentication and we haven't
  2083. * sent out an auth frame yet there's no need to send
  2084. * out a deauth frame either. If the state was PROBE,
  2085. * then this is the case. If it's AUTH we have sent a
  2086. * frame, and if it's IDLE we have completed the auth
  2087. * process already.
  2088. */
  2089. if (not_auth_yet) {
  2090. __cfg80211_auth_canceled(sdata->dev, bssid);
  2091. return 0;
  2092. }
  2093. }
  2094. printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
  2095. sdata->name, bssid, req->reason_code);
  2096. ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
  2097. req->reason_code, cookie,
  2098. !req->local_state_change);
  2099. if (assoc_bss)
  2100. sta_info_destroy_addr(sdata, bssid);
  2101. mutex_lock(&sdata->local->mtx);
  2102. ieee80211_recalc_idle(sdata->local);
  2103. mutex_unlock(&sdata->local->mtx);
  2104. return 0;
  2105. }
  2106. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  2107. struct cfg80211_disassoc_request *req,
  2108. void *cookie)
  2109. {
  2110. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2111. u8 bssid[ETH_ALEN];
  2112. mutex_lock(&ifmgd->mtx);
  2113. /*
  2114. * cfg80211 should catch this ... but it's racy since
  2115. * we can receive a disassoc frame, process it, hand it
  2116. * to cfg80211 while that's in a locked section already
  2117. * trying to tell us that the user wants to disconnect.
  2118. */
  2119. if (ifmgd->associated != req->bss) {
  2120. mutex_unlock(&ifmgd->mtx);
  2121. return -ENOLINK;
  2122. }
  2123. printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
  2124. sdata->name, req->bss->bssid, req->reason_code);
  2125. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  2126. ieee80211_set_disassoc(sdata, false, true);
  2127. mutex_unlock(&ifmgd->mtx);
  2128. ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
  2129. IEEE80211_STYPE_DISASSOC, req->reason_code,
  2130. cookie, !req->local_state_change);
  2131. sta_info_destroy_addr(sdata, bssid);
  2132. mutex_lock(&sdata->local->mtx);
  2133. ieee80211_recalc_idle(sdata->local);
  2134. mutex_unlock(&sdata->local->mtx);
  2135. return 0;
  2136. }
  2137. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  2138. enum nl80211_cqm_rssi_threshold_event rssi_event,
  2139. gfp_t gfp)
  2140. {
  2141. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2142. trace_api_cqm_rssi_notify(sdata, rssi_event);
  2143. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  2144. }
  2145. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);