mlme.c 53 KB

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