mlme.c 74 KB

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