mlme.c 68 KB

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