mlme.c 73 KB

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