mlme.c 72 KB

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