mlme.c 71 KB

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