mlme.c 80 KB

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