mlme.c 56 KB

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