mlme.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640
  1. /*
  2. * BSS client mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/pm_qos.h>
  21. #include <linux/crc32.h>
  22. #include <linux/slab.h>
  23. #include <linux/export.h>
  24. #include <net/mac80211.h>
  25. #include <asm/unaligned.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "led.h"
  30. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  31. #define IEEE80211_AUTH_MAX_TRIES 3
  32. #define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
  33. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  34. #define IEEE80211_ASSOC_MAX_TRIES 3
  35. static int max_nullfunc_tries = 2;
  36. module_param(max_nullfunc_tries, int, 0644);
  37. MODULE_PARM_DESC(max_nullfunc_tries,
  38. "Maximum nullfunc tx tries before disconnecting (reason 4).");
  39. static int max_probe_tries = 5;
  40. module_param(max_probe_tries, int, 0644);
  41. MODULE_PARM_DESC(max_probe_tries,
  42. "Maximum probe tries before disconnecting (reason 4).");
  43. /*
  44. * Beacon loss timeout is calculated as N frames times the
  45. * advertised beacon interval. This may need to be somewhat
  46. * higher than what hardware might detect to account for
  47. * delays in the host processing frames. But since we also
  48. * probe on beacon miss before declaring the connection lost
  49. * default to what we want.
  50. */
  51. #define IEEE80211_BEACON_LOSS_COUNT 7
  52. /*
  53. * Time the connection can be idle before we probe
  54. * it to see if we can still talk to the AP.
  55. */
  56. #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
  57. /*
  58. * Time we wait for a probe response after sending
  59. * a probe request because of beacon loss or for
  60. * checking the connection still works.
  61. */
  62. static int probe_wait_ms = 500;
  63. module_param(probe_wait_ms, int, 0644);
  64. MODULE_PARM_DESC(probe_wait_ms,
  65. "Maximum time(ms) to wait for probe response"
  66. " before disconnecting (reason 4).");
  67. /*
  68. * Weight given to the latest Beacon frame when calculating average signal
  69. * strength for Beacon frames received in the current BSS. This must be
  70. * between 1 and 15.
  71. */
  72. #define IEEE80211_SIGNAL_AVE_WEIGHT 3
  73. /*
  74. * How many Beacon frames need to have been used in average signal strength
  75. * before starting to indicate signal change events.
  76. */
  77. #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
  78. #define TMR_RUNNING_TIMER 0
  79. #define TMR_RUNNING_CHANSW 1
  80. #define DEAUTH_DISASSOC_LEN (24 /* hdr */ + 2 /* reason */)
  81. /*
  82. * All cfg80211 functions have to be called outside a locked
  83. * section so that they can acquire a lock themselves... This
  84. * is much simpler than queuing up things in cfg80211, but we
  85. * do need some indirection for that here.
  86. */
  87. enum rx_mgmt_action {
  88. /* no action required */
  89. RX_MGMT_NONE,
  90. /* caller must call cfg80211_send_deauth() */
  91. RX_MGMT_CFG80211_DEAUTH,
  92. /* caller must call cfg80211_send_disassoc() */
  93. RX_MGMT_CFG80211_DISASSOC,
  94. /* caller must call cfg80211_send_rx_auth() */
  95. RX_MGMT_CFG80211_RX_AUTH,
  96. /* caller must call cfg80211_send_rx_assoc() */
  97. RX_MGMT_CFG80211_RX_ASSOC,
  98. /* caller must call cfg80211_send_assoc_timeout() */
  99. RX_MGMT_CFG80211_ASSOC_TIMEOUT,
  100. };
  101. /* utils */
  102. static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
  103. {
  104. lockdep_assert_held(&ifmgd->mtx);
  105. }
  106. /*
  107. * We can have multiple work items (and connection probing)
  108. * scheduling this timer, but we need to take care to only
  109. * reschedule it when it should fire _earlier_ than it was
  110. * asked for before, or if it's not pending right now. This
  111. * function ensures that. Note that it then is required to
  112. * run this function for all timeouts after the first one
  113. * has happened -- the work that runs from this timer will
  114. * do that.
  115. */
  116. static void run_again(struct ieee80211_if_managed *ifmgd, unsigned long timeout)
  117. {
  118. ASSERT_MGD_MTX(ifmgd);
  119. if (!timer_pending(&ifmgd->timer) ||
  120. time_before(timeout, ifmgd->timer.expires))
  121. mod_timer(&ifmgd->timer, timeout);
  122. }
  123. void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
  124. {
  125. if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
  126. return;
  127. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  128. return;
  129. mod_timer(&sdata->u.mgd.bcn_mon_timer,
  130. round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
  131. }
  132. void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
  133. {
  134. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  135. if (unlikely(!sdata->u.mgd.associated))
  136. return;
  137. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  138. return;
  139. mod_timer(&sdata->u.mgd.conn_mon_timer,
  140. round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  141. ifmgd->probe_send_count = 0;
  142. }
  143. static int ecw2cw(int ecw)
  144. {
  145. return (1 << ecw) - 1;
  146. }
  147. static u32 ieee80211_config_ht_tx(struct ieee80211_sub_if_data *sdata,
  148. struct ieee80211_ht_operation *ht_oper,
  149. const u8 *bssid, bool reconfig)
  150. {
  151. struct ieee80211_local *local = sdata->local;
  152. struct ieee80211_supported_band *sband;
  153. struct sta_info *sta;
  154. u32 changed = 0;
  155. u16 ht_opmode;
  156. bool disable_40 = false;
  157. sband = local->hw.wiphy->bands[local->oper_channel->band];
  158. switch (sdata->vif.bss_conf.channel_type) {
  159. case NL80211_CHAN_HT40PLUS:
  160. if (local->oper_channel->flags & IEEE80211_CHAN_NO_HT40PLUS)
  161. disable_40 = true;
  162. break;
  163. case NL80211_CHAN_HT40MINUS:
  164. if (local->oper_channel->flags & IEEE80211_CHAN_NO_HT40MINUS)
  165. disable_40 = true;
  166. break;
  167. default:
  168. break;
  169. }
  170. /* This can change during the lifetime of the BSS */
  171. if (!(ht_oper->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY))
  172. disable_40 = true;
  173. mutex_lock(&local->sta_mtx);
  174. sta = sta_info_get(sdata, bssid);
  175. WARN_ON_ONCE(!sta);
  176. if (sta && !sta->supports_40mhz)
  177. disable_40 = true;
  178. if (sta && (!reconfig ||
  179. (disable_40 != !(sta->sta.ht_cap.cap &
  180. IEEE80211_HT_CAP_SUP_WIDTH_20_40)))) {
  181. if (disable_40)
  182. sta->sta.ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  183. else
  184. sta->sta.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  185. rate_control_rate_update(local, sband, sta,
  186. IEEE80211_RC_BW_CHANGED);
  187. }
  188. mutex_unlock(&local->sta_mtx);
  189. ht_opmode = le16_to_cpu(ht_oper->operation_mode);
  190. /* if bss configuration changed store the new one */
  191. if (!reconfig || (sdata->vif.bss_conf.ht_operation_mode != ht_opmode)) {
  192. changed |= BSS_CHANGED_HT;
  193. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  194. }
  195. return changed;
  196. }
  197. /* frame sending functions */
  198. static int ieee80211_compatible_rates(const u8 *supp_rates, int supp_rates_len,
  199. struct ieee80211_supported_band *sband,
  200. u32 *rates)
  201. {
  202. int i, j, count;
  203. *rates = 0;
  204. count = 0;
  205. for (i = 0; i < supp_rates_len; i++) {
  206. int rate = (supp_rates[i] & 0x7F) * 5;
  207. for (j = 0; j < sband->n_bitrates; j++)
  208. if (sband->bitrates[j].bitrate == rate) {
  209. *rates |= BIT(j);
  210. count++;
  211. break;
  212. }
  213. }
  214. return count;
  215. }
  216. static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
  217. struct sk_buff *skb, u8 ap_ht_param,
  218. struct ieee80211_supported_band *sband,
  219. struct ieee80211_channel *channel,
  220. enum ieee80211_smps_mode smps)
  221. {
  222. u8 *pos;
  223. u32 flags = channel->flags;
  224. u16 cap;
  225. struct ieee80211_sta_ht_cap ht_cap;
  226. BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
  227. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  228. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  229. /* determine capability flags */
  230. cap = ht_cap.cap;
  231. switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  232. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  233. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  234. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  235. cap &= ~IEEE80211_HT_CAP_SGI_40;
  236. }
  237. break;
  238. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  239. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  240. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  241. cap &= ~IEEE80211_HT_CAP_SGI_40;
  242. }
  243. break;
  244. }
  245. /*
  246. * If 40 MHz was disabled associate as though we weren't
  247. * capable of 40 MHz -- some broken APs will never fall
  248. * back to trying to transmit in 20 MHz.
  249. */
  250. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
  251. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  252. cap &= ~IEEE80211_HT_CAP_SGI_40;
  253. }
  254. /* set SM PS mode properly */
  255. cap &= ~IEEE80211_HT_CAP_SM_PS;
  256. switch (smps) {
  257. case IEEE80211_SMPS_AUTOMATIC:
  258. case IEEE80211_SMPS_NUM_MODES:
  259. WARN_ON(1);
  260. case IEEE80211_SMPS_OFF:
  261. cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
  262. IEEE80211_HT_CAP_SM_PS_SHIFT;
  263. break;
  264. case IEEE80211_SMPS_STATIC:
  265. cap |= WLAN_HT_CAP_SM_PS_STATIC <<
  266. IEEE80211_HT_CAP_SM_PS_SHIFT;
  267. break;
  268. case IEEE80211_SMPS_DYNAMIC:
  269. cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
  270. IEEE80211_HT_CAP_SM_PS_SHIFT;
  271. break;
  272. }
  273. /* reserve and fill IE */
  274. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  275. ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
  276. }
  277. static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
  278. struct sk_buff *skb,
  279. struct ieee80211_supported_band *sband)
  280. {
  281. u8 *pos;
  282. u32 cap;
  283. struct ieee80211_sta_vht_cap vht_cap;
  284. BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
  285. memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
  286. /* determine capability flags */
  287. cap = vht_cap.cap;
  288. /* reserve and fill IE */
  289. pos = skb_put(skb, sizeof(struct ieee80211_vht_capabilities) + 2);
  290. ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
  291. }
  292. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
  293. {
  294. struct ieee80211_local *local = sdata->local;
  295. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  296. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  297. struct sk_buff *skb;
  298. struct ieee80211_mgmt *mgmt;
  299. u8 *pos, qos_info;
  300. size_t offset = 0, noffset;
  301. int i, count, rates_len, supp_rates_len;
  302. u16 capab;
  303. struct ieee80211_supported_band *sband;
  304. u32 rates = 0;
  305. lockdep_assert_held(&ifmgd->mtx);
  306. sband = local->hw.wiphy->bands[local->oper_channel->band];
  307. if (assoc_data->supp_rates_len) {
  308. /*
  309. * Get all rates supported by the device and the AP as
  310. * some APs don't like getting a superset of their rates
  311. * in the association request (e.g. D-Link DAP 1353 in
  312. * b-only mode)...
  313. */
  314. rates_len = ieee80211_compatible_rates(assoc_data->supp_rates,
  315. assoc_data->supp_rates_len,
  316. sband, &rates);
  317. } else {
  318. /*
  319. * In case AP not provide any supported rates information
  320. * before association, we send information element(s) with
  321. * all rates that we support.
  322. */
  323. rates = ~0;
  324. rates_len = sband->n_bitrates;
  325. }
  326. skb = alloc_skb(local->hw.extra_tx_headroom +
  327. sizeof(*mgmt) + /* bit too much but doesn't matter */
  328. 2 + assoc_data->ssid_len + /* SSID */
  329. 4 + rates_len + /* (extended) rates */
  330. 4 + /* power capability */
  331. 2 + 2 * sband->n_channels + /* supported channels */
  332. 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
  333. 2 + sizeof(struct ieee80211_vht_capabilities) + /* VHT */
  334. assoc_data->ie_len + /* extra IEs */
  335. 9, /* WMM */
  336. GFP_KERNEL);
  337. if (!skb)
  338. return;
  339. skb_reserve(skb, local->hw.extra_tx_headroom);
  340. capab = WLAN_CAPABILITY_ESS;
  341. if (sband->band == IEEE80211_BAND_2GHZ) {
  342. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  343. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  344. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  345. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  346. }
  347. if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
  348. capab |= WLAN_CAPABILITY_PRIVACY;
  349. if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  350. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  351. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  352. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  353. memset(mgmt, 0, 24);
  354. memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
  355. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  356. memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
  357. if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
  358. skb_put(skb, 10);
  359. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  360. IEEE80211_STYPE_REASSOC_REQ);
  361. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  362. mgmt->u.reassoc_req.listen_interval =
  363. cpu_to_le16(local->hw.conf.listen_interval);
  364. memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
  365. ETH_ALEN);
  366. } else {
  367. skb_put(skb, 4);
  368. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  369. IEEE80211_STYPE_ASSOC_REQ);
  370. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  371. mgmt->u.assoc_req.listen_interval =
  372. cpu_to_le16(local->hw.conf.listen_interval);
  373. }
  374. /* SSID */
  375. pos = skb_put(skb, 2 + assoc_data->ssid_len);
  376. *pos++ = WLAN_EID_SSID;
  377. *pos++ = assoc_data->ssid_len;
  378. memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
  379. /* add all rates which were marked to be used above */
  380. supp_rates_len = rates_len;
  381. if (supp_rates_len > 8)
  382. supp_rates_len = 8;
  383. pos = skb_put(skb, supp_rates_len + 2);
  384. *pos++ = WLAN_EID_SUPP_RATES;
  385. *pos++ = supp_rates_len;
  386. count = 0;
  387. for (i = 0; i < sband->n_bitrates; i++) {
  388. if (BIT(i) & rates) {
  389. int rate = sband->bitrates[i].bitrate;
  390. *pos++ = (u8) (rate / 5);
  391. if (++count == 8)
  392. break;
  393. }
  394. }
  395. if (rates_len > count) {
  396. pos = skb_put(skb, rates_len - count + 2);
  397. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  398. *pos++ = rates_len - count;
  399. for (i++; i < sband->n_bitrates; i++) {
  400. if (BIT(i) & rates) {
  401. int rate = sband->bitrates[i].bitrate;
  402. *pos++ = (u8) (rate / 5);
  403. }
  404. }
  405. }
  406. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  407. /* 1. power capabilities */
  408. pos = skb_put(skb, 4);
  409. *pos++ = WLAN_EID_PWR_CAPABILITY;
  410. *pos++ = 2;
  411. *pos++ = 0; /* min tx power */
  412. *pos++ = local->oper_channel->max_power; /* max tx power */
  413. /* 2. supported channels */
  414. /* TODO: get this in reg domain format */
  415. pos = skb_put(skb, 2 * sband->n_channels + 2);
  416. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  417. *pos++ = 2 * sband->n_channels;
  418. for (i = 0; i < sband->n_channels; i++) {
  419. *pos++ = ieee80211_frequency_to_channel(
  420. sband->channels[i].center_freq);
  421. *pos++ = 1; /* one channel in the subband*/
  422. }
  423. }
  424. /* if present, add any custom IEs that go before HT */
  425. if (assoc_data->ie_len && assoc_data->ie) {
  426. static const u8 before_ht[] = {
  427. WLAN_EID_SSID,
  428. WLAN_EID_SUPP_RATES,
  429. WLAN_EID_EXT_SUPP_RATES,
  430. WLAN_EID_PWR_CAPABILITY,
  431. WLAN_EID_SUPPORTED_CHANNELS,
  432. WLAN_EID_RSN,
  433. WLAN_EID_QOS_CAPA,
  434. WLAN_EID_RRM_ENABLED_CAPABILITIES,
  435. WLAN_EID_MOBILITY_DOMAIN,
  436. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  437. };
  438. noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
  439. before_ht, ARRAY_SIZE(before_ht),
  440. offset);
  441. pos = skb_put(skb, noffset - offset);
  442. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  443. offset = noffset;
  444. }
  445. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  446. ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
  447. sband, local->oper_channel, ifmgd->ap_smps);
  448. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  449. ieee80211_add_vht_ie(sdata, skb, sband);
  450. /* if present, add any custom non-vendor IEs that go after HT */
  451. if (assoc_data->ie_len && assoc_data->ie) {
  452. noffset = ieee80211_ie_split_vendor(assoc_data->ie,
  453. assoc_data->ie_len,
  454. offset);
  455. pos = skb_put(skb, noffset - offset);
  456. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  457. offset = noffset;
  458. }
  459. if (assoc_data->wmm) {
  460. if (assoc_data->uapsd) {
  461. qos_info = ifmgd->uapsd_queues;
  462. qos_info |= (ifmgd->uapsd_max_sp_len <<
  463. IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
  464. } else {
  465. qos_info = 0;
  466. }
  467. pos = skb_put(skb, 9);
  468. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  469. *pos++ = 7; /* len */
  470. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  471. *pos++ = 0x50;
  472. *pos++ = 0xf2;
  473. *pos++ = 2; /* WME */
  474. *pos++ = 0; /* WME info */
  475. *pos++ = 1; /* WME ver */
  476. *pos++ = qos_info;
  477. }
  478. /* add any remaining custom (i.e. vendor specific here) IEs */
  479. if (assoc_data->ie_len && assoc_data->ie) {
  480. noffset = assoc_data->ie_len;
  481. pos = skb_put(skb, noffset - offset);
  482. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  483. }
  484. drv_mgd_prepare_tx(local, sdata);
  485. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  486. ieee80211_tx_skb(sdata, skb);
  487. }
  488. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  489. const u8 *bssid, u16 stype,
  490. u16 reason, bool send_frame,
  491. u8 *frame_buf)
  492. {
  493. struct ieee80211_local *local = sdata->local;
  494. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  495. struct sk_buff *skb;
  496. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  497. /* build frame */
  498. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  499. mgmt->duration = 0; /* initialize only */
  500. mgmt->seq_ctrl = 0; /* initialize only */
  501. memcpy(mgmt->da, bssid, ETH_ALEN);
  502. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  503. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  504. /* u.deauth.reason_code == u.disassoc.reason_code */
  505. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  506. if (send_frame) {
  507. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  508. DEAUTH_DISASSOC_LEN);
  509. if (!skb)
  510. return;
  511. skb_reserve(skb, local->hw.extra_tx_headroom);
  512. /* copy in frame */
  513. memcpy(skb_put(skb, DEAUTH_DISASSOC_LEN),
  514. mgmt, DEAUTH_DISASSOC_LEN);
  515. if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
  516. IEEE80211_SKB_CB(skb)->flags |=
  517. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  518. ieee80211_tx_skb(sdata, skb);
  519. }
  520. }
  521. void ieee80211_send_pspoll(struct ieee80211_local *local,
  522. struct ieee80211_sub_if_data *sdata)
  523. {
  524. struct ieee80211_pspoll *pspoll;
  525. struct sk_buff *skb;
  526. skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
  527. if (!skb)
  528. return;
  529. pspoll = (struct ieee80211_pspoll *) skb->data;
  530. pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  531. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  532. ieee80211_tx_skb(sdata, skb);
  533. }
  534. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  535. struct ieee80211_sub_if_data *sdata,
  536. int powersave)
  537. {
  538. struct sk_buff *skb;
  539. struct ieee80211_hdr_3addr *nullfunc;
  540. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  541. skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
  542. if (!skb)
  543. return;
  544. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  545. if (powersave)
  546. nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  547. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  548. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  549. IEEE80211_STA_CONNECTION_POLL))
  550. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
  551. ieee80211_tx_skb(sdata, skb);
  552. }
  553. static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
  554. struct ieee80211_sub_if_data *sdata)
  555. {
  556. struct sk_buff *skb;
  557. struct ieee80211_hdr *nullfunc;
  558. __le16 fc;
  559. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  560. return;
  561. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
  562. if (!skb)
  563. return;
  564. skb_reserve(skb, local->hw.extra_tx_headroom);
  565. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
  566. memset(nullfunc, 0, 30);
  567. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  568. IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  569. nullfunc->frame_control = fc;
  570. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  571. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  572. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  573. memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
  574. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  575. ieee80211_tx_skb(sdata, skb);
  576. }
  577. /* spectrum management related things */
  578. static void ieee80211_chswitch_work(struct work_struct *work)
  579. {
  580. struct ieee80211_sub_if_data *sdata =
  581. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  582. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  583. if (!ieee80211_sdata_running(sdata))
  584. return;
  585. mutex_lock(&ifmgd->mtx);
  586. if (!ifmgd->associated)
  587. goto out;
  588. sdata->local->oper_channel = sdata->local->csa_channel;
  589. if (!sdata->local->ops->channel_switch) {
  590. /* call "hw_config" only if doing sw channel switch */
  591. ieee80211_hw_config(sdata->local,
  592. IEEE80211_CONF_CHANGE_CHANNEL);
  593. } else {
  594. /* update the device channel directly */
  595. sdata->local->hw.conf.channel = sdata->local->oper_channel;
  596. }
  597. /* XXX: shouldn't really modify cfg80211-owned data! */
  598. ifmgd->associated->channel = sdata->local->oper_channel;
  599. /* XXX: wait for a beacon first? */
  600. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  601. IEEE80211_QUEUE_STOP_REASON_CSA);
  602. out:
  603. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  604. mutex_unlock(&ifmgd->mtx);
  605. }
  606. void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
  607. {
  608. struct ieee80211_sub_if_data *sdata;
  609. struct ieee80211_if_managed *ifmgd;
  610. sdata = vif_to_sdata(vif);
  611. ifmgd = &sdata->u.mgd;
  612. trace_api_chswitch_done(sdata, success);
  613. if (!success) {
  614. sdata_info(sdata,
  615. "driver channel switch failed, disconnecting\n");
  616. ieee80211_queue_work(&sdata->local->hw,
  617. &ifmgd->csa_connection_drop_work);
  618. } else {
  619. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  620. }
  621. }
  622. EXPORT_SYMBOL(ieee80211_chswitch_done);
  623. static void ieee80211_chswitch_timer(unsigned long data)
  624. {
  625. struct ieee80211_sub_if_data *sdata =
  626. (struct ieee80211_sub_if_data *) data;
  627. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  628. if (sdata->local->quiescing) {
  629. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  630. return;
  631. }
  632. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  633. }
  634. void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  635. struct ieee80211_channel_sw_ie *sw_elem,
  636. struct ieee80211_bss *bss,
  637. u64 timestamp)
  638. {
  639. struct cfg80211_bss *cbss =
  640. container_of((void *)bss, struct cfg80211_bss, priv);
  641. struct ieee80211_channel *new_ch;
  642. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  643. int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num,
  644. cbss->channel->band);
  645. ASSERT_MGD_MTX(ifmgd);
  646. if (!ifmgd->associated)
  647. return;
  648. if (sdata->local->scanning)
  649. return;
  650. /* Disregard subsequent beacons if we are already running a timer
  651. processing a CSA */
  652. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  653. return;
  654. new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  655. if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED) {
  656. sdata_info(sdata,
  657. "AP %pM switches to unsupported channel (%d MHz), disconnecting\n",
  658. ifmgd->associated->bssid, new_freq);
  659. ieee80211_queue_work(&sdata->local->hw,
  660. &ifmgd->csa_connection_drop_work);
  661. return;
  662. }
  663. sdata->local->csa_channel = new_ch;
  664. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  665. if (sw_elem->mode)
  666. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  667. IEEE80211_QUEUE_STOP_REASON_CSA);
  668. if (sdata->local->ops->channel_switch) {
  669. /* use driver's channel switch callback */
  670. struct ieee80211_channel_switch ch_switch = {
  671. .timestamp = timestamp,
  672. .block_tx = sw_elem->mode,
  673. .channel = new_ch,
  674. .count = sw_elem->count,
  675. };
  676. drv_channel_switch(sdata->local, &ch_switch);
  677. return;
  678. }
  679. /* channel switch handled in software */
  680. if (sw_elem->count <= 1)
  681. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  682. else
  683. mod_timer(&ifmgd->chswitch_timer,
  684. TU_TO_EXP_TIME(sw_elem->count *
  685. cbss->beacon_interval));
  686. }
  687. static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  688. u16 capab_info, u8 *pwr_constr_elem)
  689. {
  690. struct ieee80211_conf *conf = &sdata->local->hw.conf;
  691. if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
  692. return;
  693. if ((*pwr_constr_elem <= conf->channel->max_reg_power) &&
  694. (*pwr_constr_elem != sdata->local->power_constr_level)) {
  695. sdata->local->power_constr_level = *pwr_constr_elem;
  696. ieee80211_hw_config(sdata->local, 0);
  697. }
  698. }
  699. void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
  700. {
  701. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  702. struct ieee80211_local *local = sdata->local;
  703. struct ieee80211_conf *conf = &local->hw.conf;
  704. WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
  705. !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
  706. (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
  707. local->disable_dynamic_ps = false;
  708. conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
  709. }
  710. EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
  711. void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
  712. {
  713. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  714. struct ieee80211_local *local = sdata->local;
  715. struct ieee80211_conf *conf = &local->hw.conf;
  716. WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
  717. !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
  718. (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
  719. local->disable_dynamic_ps = true;
  720. conf->dynamic_ps_timeout = 0;
  721. del_timer_sync(&local->dynamic_ps_timer);
  722. ieee80211_queue_work(&local->hw,
  723. &local->dynamic_ps_enable_work);
  724. }
  725. EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
  726. /* powersave */
  727. static void ieee80211_enable_ps(struct ieee80211_local *local,
  728. struct ieee80211_sub_if_data *sdata)
  729. {
  730. struct ieee80211_conf *conf = &local->hw.conf;
  731. /*
  732. * If we are scanning right now then the parameters will
  733. * take effect when scan finishes.
  734. */
  735. if (local->scanning)
  736. return;
  737. if (conf->dynamic_ps_timeout > 0 &&
  738. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  739. mod_timer(&local->dynamic_ps_timer, jiffies +
  740. msecs_to_jiffies(conf->dynamic_ps_timeout));
  741. } else {
  742. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  743. ieee80211_send_nullfunc(local, sdata, 1);
  744. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  745. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
  746. return;
  747. conf->flags |= IEEE80211_CONF_PS;
  748. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  749. }
  750. }
  751. static void ieee80211_change_ps(struct ieee80211_local *local)
  752. {
  753. struct ieee80211_conf *conf = &local->hw.conf;
  754. if (local->ps_sdata) {
  755. ieee80211_enable_ps(local, local->ps_sdata);
  756. } else if (conf->flags & IEEE80211_CONF_PS) {
  757. conf->flags &= ~IEEE80211_CONF_PS;
  758. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  759. del_timer_sync(&local->dynamic_ps_timer);
  760. cancel_work_sync(&local->dynamic_ps_enable_work);
  761. }
  762. }
  763. static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
  764. {
  765. struct ieee80211_if_managed *mgd = &sdata->u.mgd;
  766. struct sta_info *sta = NULL;
  767. bool authorized = false;
  768. if (!mgd->powersave)
  769. return false;
  770. if (mgd->broken_ap)
  771. return false;
  772. if (!mgd->associated)
  773. return false;
  774. if (mgd->flags & (IEEE80211_STA_BEACON_POLL |
  775. IEEE80211_STA_CONNECTION_POLL))
  776. return false;
  777. rcu_read_lock();
  778. sta = sta_info_get(sdata, mgd->bssid);
  779. if (sta)
  780. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  781. rcu_read_unlock();
  782. return authorized;
  783. }
  784. /* need to hold RTNL or interface lock */
  785. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  786. {
  787. struct ieee80211_sub_if_data *sdata, *found = NULL;
  788. int count = 0;
  789. int timeout;
  790. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  791. local->ps_sdata = NULL;
  792. return;
  793. }
  794. list_for_each_entry(sdata, &local->interfaces, list) {
  795. if (!ieee80211_sdata_running(sdata))
  796. continue;
  797. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  798. /* If an AP vif is found, then disable PS
  799. * by setting the count to zero thereby setting
  800. * ps_sdata to NULL.
  801. */
  802. count = 0;
  803. break;
  804. }
  805. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  806. continue;
  807. found = sdata;
  808. count++;
  809. }
  810. if (count == 1 && ieee80211_powersave_allowed(found)) {
  811. struct ieee80211_conf *conf = &local->hw.conf;
  812. s32 beaconint_us;
  813. if (latency < 0)
  814. latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
  815. beaconint_us = ieee80211_tu_to_usec(
  816. found->vif.bss_conf.beacon_int);
  817. timeout = local->dynamic_ps_forced_timeout;
  818. if (timeout < 0) {
  819. /*
  820. * Go to full PSM if the user configures a very low
  821. * latency requirement.
  822. * The 2000 second value is there for compatibility
  823. * until the PM_QOS_NETWORK_LATENCY is configured
  824. * with real values.
  825. */
  826. if (latency > (1900 * USEC_PER_MSEC) &&
  827. latency != (2000 * USEC_PER_SEC))
  828. timeout = 0;
  829. else
  830. timeout = 100;
  831. }
  832. local->dynamic_ps_user_timeout = timeout;
  833. if (!local->disable_dynamic_ps)
  834. conf->dynamic_ps_timeout =
  835. local->dynamic_ps_user_timeout;
  836. if (beaconint_us > latency) {
  837. local->ps_sdata = NULL;
  838. } else {
  839. struct ieee80211_bss *bss;
  840. int maxslp = 1;
  841. u8 dtimper;
  842. bss = (void *)found->u.mgd.associated->priv;
  843. dtimper = bss->dtim_period;
  844. /* If the TIM IE is invalid, pretend the value is 1 */
  845. if (!dtimper)
  846. dtimper = 1;
  847. else if (dtimper > 1)
  848. maxslp = min_t(int, dtimper,
  849. latency / beaconint_us);
  850. local->hw.conf.max_sleep_period = maxslp;
  851. local->hw.conf.ps_dtim_period = dtimper;
  852. local->ps_sdata = found;
  853. }
  854. } else {
  855. local->ps_sdata = NULL;
  856. }
  857. ieee80211_change_ps(local);
  858. }
  859. void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
  860. {
  861. bool ps_allowed = ieee80211_powersave_allowed(sdata);
  862. if (sdata->vif.bss_conf.ps != ps_allowed) {
  863. sdata->vif.bss_conf.ps = ps_allowed;
  864. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
  865. }
  866. }
  867. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  868. {
  869. struct ieee80211_local *local =
  870. container_of(work, struct ieee80211_local,
  871. dynamic_ps_disable_work);
  872. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  873. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  874. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  875. }
  876. ieee80211_wake_queues_by_reason(&local->hw,
  877. IEEE80211_QUEUE_STOP_REASON_PS);
  878. }
  879. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  880. {
  881. struct ieee80211_local *local =
  882. container_of(work, struct ieee80211_local,
  883. dynamic_ps_enable_work);
  884. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  885. struct ieee80211_if_managed *ifmgd;
  886. unsigned long flags;
  887. int q;
  888. /* can only happen when PS was just disabled anyway */
  889. if (!sdata)
  890. return;
  891. ifmgd = &sdata->u.mgd;
  892. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  893. return;
  894. if (!local->disable_dynamic_ps &&
  895. local->hw.conf.dynamic_ps_timeout > 0) {
  896. /* don't enter PS if TX frames are pending */
  897. if (drv_tx_frames_pending(local)) {
  898. mod_timer(&local->dynamic_ps_timer, jiffies +
  899. msecs_to_jiffies(
  900. local->hw.conf.dynamic_ps_timeout));
  901. return;
  902. }
  903. /*
  904. * transmission can be stopped by others which leads to
  905. * dynamic_ps_timer expiry. Postpone the ps timer if it
  906. * is not the actual idle state.
  907. */
  908. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  909. for (q = 0; q < local->hw.queues; q++) {
  910. if (local->queue_stop_reasons[q]) {
  911. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  912. flags);
  913. mod_timer(&local->dynamic_ps_timer, jiffies +
  914. msecs_to_jiffies(
  915. local->hw.conf.dynamic_ps_timeout));
  916. return;
  917. }
  918. }
  919. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  920. }
  921. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  922. !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  923. netif_tx_stop_all_queues(sdata->dev);
  924. if (drv_tx_frames_pending(local))
  925. mod_timer(&local->dynamic_ps_timer, jiffies +
  926. msecs_to_jiffies(
  927. local->hw.conf.dynamic_ps_timeout));
  928. else {
  929. ieee80211_send_nullfunc(local, sdata, 1);
  930. /* Flush to get the tx status of nullfunc frame */
  931. drv_flush(local, false);
  932. }
  933. }
  934. if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
  935. (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
  936. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  937. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  938. local->hw.conf.flags |= IEEE80211_CONF_PS;
  939. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  940. }
  941. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  942. netif_tx_wake_all_queues(sdata->dev);
  943. }
  944. void ieee80211_dynamic_ps_timer(unsigned long data)
  945. {
  946. struct ieee80211_local *local = (void *) data;
  947. if (local->quiescing || local->suspended)
  948. return;
  949. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  950. }
  951. /* MLME */
  952. static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
  953. struct ieee80211_sub_if_data *sdata,
  954. u8 *wmm_param, size_t wmm_param_len)
  955. {
  956. struct ieee80211_tx_queue_params params;
  957. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  958. size_t left;
  959. int count;
  960. u8 *pos, uapsd_queues = 0;
  961. if (!local->ops->conf_tx)
  962. return false;
  963. if (local->hw.queues < IEEE80211_NUM_ACS)
  964. return false;
  965. if (!wmm_param)
  966. return false;
  967. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  968. return false;
  969. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  970. uapsd_queues = ifmgd->uapsd_queues;
  971. count = wmm_param[6] & 0x0f;
  972. if (count == ifmgd->wmm_last_param_set)
  973. return false;
  974. ifmgd->wmm_last_param_set = count;
  975. pos = wmm_param + 8;
  976. left = wmm_param_len - 8;
  977. memset(&params, 0, sizeof(params));
  978. sdata->wmm_acm = 0;
  979. for (; left >= 4; left -= 4, pos += 4) {
  980. int aci = (pos[0] >> 5) & 0x03;
  981. int acm = (pos[0] >> 4) & 0x01;
  982. bool uapsd = false;
  983. int queue;
  984. switch (aci) {
  985. case 1: /* AC_BK */
  986. queue = 3;
  987. if (acm)
  988. sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  989. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  990. uapsd = true;
  991. break;
  992. case 2: /* AC_VI */
  993. queue = 1;
  994. if (acm)
  995. sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  996. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  997. uapsd = true;
  998. break;
  999. case 3: /* AC_VO */
  1000. queue = 0;
  1001. if (acm)
  1002. sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  1003. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1004. uapsd = true;
  1005. break;
  1006. case 0: /* AC_BE */
  1007. default:
  1008. queue = 2;
  1009. if (acm)
  1010. sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  1011. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1012. uapsd = true;
  1013. break;
  1014. }
  1015. params.aifs = pos[0] & 0x0f;
  1016. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  1017. params.cw_min = ecw2cw(pos[1] & 0x0f);
  1018. params.txop = get_unaligned_le16(pos + 2);
  1019. params.uapsd = uapsd;
  1020. mlme_dbg(sdata,
  1021. "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
  1022. queue, aci, acm,
  1023. params.aifs, params.cw_min, params.cw_max,
  1024. params.txop, params.uapsd);
  1025. sdata->tx_conf[queue] = params;
  1026. if (drv_conf_tx(local, sdata, queue, &params))
  1027. sdata_err(sdata,
  1028. "failed to set TX queue parameters for queue %d\n",
  1029. queue);
  1030. }
  1031. /* enable WMM or activate new settings */
  1032. sdata->vif.bss_conf.qos = true;
  1033. return true;
  1034. }
  1035. static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1036. {
  1037. lockdep_assert_held(&sdata->local->mtx);
  1038. sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1039. IEEE80211_STA_BEACON_POLL);
  1040. ieee80211_run_deferred_scan(sdata->local);
  1041. }
  1042. static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1043. {
  1044. mutex_lock(&sdata->local->mtx);
  1045. __ieee80211_stop_poll(sdata);
  1046. mutex_unlock(&sdata->local->mtx);
  1047. }
  1048. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  1049. u16 capab, bool erp_valid, u8 erp)
  1050. {
  1051. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1052. u32 changed = 0;
  1053. bool use_protection;
  1054. bool use_short_preamble;
  1055. bool use_short_slot;
  1056. if (erp_valid) {
  1057. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  1058. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  1059. } else {
  1060. use_protection = false;
  1061. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  1062. }
  1063. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1064. if (sdata->local->oper_channel->band == IEEE80211_BAND_5GHZ)
  1065. use_short_slot = true;
  1066. if (use_protection != bss_conf->use_cts_prot) {
  1067. bss_conf->use_cts_prot = use_protection;
  1068. changed |= BSS_CHANGED_ERP_CTS_PROT;
  1069. }
  1070. if (use_short_preamble != bss_conf->use_short_preamble) {
  1071. bss_conf->use_short_preamble = use_short_preamble;
  1072. changed |= BSS_CHANGED_ERP_PREAMBLE;
  1073. }
  1074. if (use_short_slot != bss_conf->use_short_slot) {
  1075. bss_conf->use_short_slot = use_short_slot;
  1076. changed |= BSS_CHANGED_ERP_SLOT;
  1077. }
  1078. return changed;
  1079. }
  1080. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  1081. struct cfg80211_bss *cbss,
  1082. u32 bss_info_changed)
  1083. {
  1084. struct ieee80211_bss *bss = (void *)cbss->priv;
  1085. struct ieee80211_local *local = sdata->local;
  1086. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1087. bss_info_changed |= BSS_CHANGED_ASSOC;
  1088. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  1089. bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
  1090. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  1091. IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
  1092. sdata->u.mgd.associated = cbss;
  1093. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  1094. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  1095. /* just to be sure */
  1096. ieee80211_stop_poll(sdata);
  1097. ieee80211_led_assoc(local, 1);
  1098. if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
  1099. bss_conf->dtim_period = bss->dtim_period;
  1100. else
  1101. bss_conf->dtim_period = 0;
  1102. bss_conf->assoc = 1;
  1103. /* Tell the driver to monitor connection quality (if supported) */
  1104. if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
  1105. bss_conf->cqm_rssi_thold)
  1106. bss_info_changed |= BSS_CHANGED_CQM;
  1107. /* Enable ARP filtering */
  1108. if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
  1109. bss_conf->arp_filter_enabled = sdata->arp_filter_state;
  1110. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  1111. }
  1112. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  1113. mutex_lock(&local->iflist_mtx);
  1114. ieee80211_recalc_ps(local, -1);
  1115. ieee80211_recalc_smps(local);
  1116. mutex_unlock(&local->iflist_mtx);
  1117. ieee80211_recalc_ps_vif(sdata);
  1118. netif_tx_start_all_queues(sdata->dev);
  1119. netif_carrier_on(sdata->dev);
  1120. }
  1121. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  1122. u16 stype, u16 reason, bool tx,
  1123. u8 *frame_buf)
  1124. {
  1125. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1126. struct ieee80211_local *local = sdata->local;
  1127. struct sta_info *sta;
  1128. u32 changed = 0;
  1129. ASSERT_MGD_MTX(ifmgd);
  1130. if (WARN_ON_ONCE(tx && !frame_buf))
  1131. return;
  1132. if (WARN_ON(!ifmgd->associated))
  1133. return;
  1134. ieee80211_stop_poll(sdata);
  1135. ifmgd->associated = NULL;
  1136. /*
  1137. * we need to commit the associated = NULL change because the
  1138. * scan code uses that to determine whether this iface should
  1139. * go to/wake up from powersave or not -- and could otherwise
  1140. * wake the queues erroneously.
  1141. */
  1142. smp_mb();
  1143. /*
  1144. * Thus, we can only afterwards stop the queues -- to account
  1145. * for the case where another CPU is finishing a scan at this
  1146. * time -- we don't want the scan code to enable queues.
  1147. */
  1148. netif_tx_stop_all_queues(sdata->dev);
  1149. netif_carrier_off(sdata->dev);
  1150. mutex_lock(&local->sta_mtx);
  1151. sta = sta_info_get(sdata, ifmgd->bssid);
  1152. if (sta) {
  1153. set_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1154. ieee80211_sta_tear_down_BA_sessions(sta, tx);
  1155. }
  1156. mutex_unlock(&local->sta_mtx);
  1157. /*
  1158. * if we want to get out of ps before disassoc (why?) we have
  1159. * to do it before sending disassoc, as otherwise the null-packet
  1160. * won't be valid.
  1161. */
  1162. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1163. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1164. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1165. }
  1166. local->ps_sdata = NULL;
  1167. /* disable per-vif ps */
  1168. ieee80211_recalc_ps_vif(sdata);
  1169. /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
  1170. if (tx)
  1171. drv_flush(local, false);
  1172. /* deauthenticate/disassociate now */
  1173. if (tx || frame_buf)
  1174. ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
  1175. reason, tx, frame_buf);
  1176. /* flush out frame */
  1177. if (tx)
  1178. drv_flush(local, false);
  1179. /* clear bssid only after building the needed mgmt frames */
  1180. memset(ifmgd->bssid, 0, ETH_ALEN);
  1181. /* remove AP and TDLS peers */
  1182. sta_info_flush(local, sdata);
  1183. /* finally reset all BSS / config parameters */
  1184. changed |= ieee80211_reset_erp_info(sdata);
  1185. ieee80211_led_assoc(local, 0);
  1186. changed |= BSS_CHANGED_ASSOC;
  1187. sdata->vif.bss_conf.assoc = false;
  1188. /* on the next assoc, re-program HT parameters */
  1189. memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
  1190. memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
  1191. local->power_constr_level = 0;
  1192. del_timer_sync(&local->dynamic_ps_timer);
  1193. cancel_work_sync(&local->dynamic_ps_enable_work);
  1194. /* Disable ARP filtering */
  1195. if (sdata->vif.bss_conf.arp_filter_enabled) {
  1196. sdata->vif.bss_conf.arp_filter_enabled = false;
  1197. changed |= BSS_CHANGED_ARP_FILTER;
  1198. }
  1199. sdata->vif.bss_conf.qos = false;
  1200. changed |= BSS_CHANGED_QOS;
  1201. /* The BSSID (not really interesting) and HT changed */
  1202. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  1203. ieee80211_bss_info_change_notify(sdata, changed);
  1204. /* channel(_type) changes are handled by ieee80211_hw_config */
  1205. WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
  1206. ieee80211_hw_config(local, 0);
  1207. /* disassociated - set to defaults now */
  1208. ieee80211_set_wmm_default(sdata, false);
  1209. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  1210. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  1211. del_timer_sync(&sdata->u.mgd.timer);
  1212. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  1213. sdata->u.mgd.timers_running = 0;
  1214. }
  1215. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  1216. struct ieee80211_hdr *hdr)
  1217. {
  1218. /*
  1219. * We can postpone the mgd.timer whenever receiving unicast frames
  1220. * from AP because we know that the connection is working both ways
  1221. * at that time. But multicast frames (and hence also beacons) must
  1222. * be ignored here, because we need to trigger the timer during
  1223. * data idle periods for sending the periodic probe request to the
  1224. * AP we're connected to.
  1225. */
  1226. if (is_multicast_ether_addr(hdr->addr1))
  1227. return;
  1228. ieee80211_sta_reset_conn_monitor(sdata);
  1229. }
  1230. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  1231. {
  1232. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1233. struct ieee80211_local *local = sdata->local;
  1234. mutex_lock(&local->mtx);
  1235. if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1236. IEEE80211_STA_CONNECTION_POLL))) {
  1237. mutex_unlock(&local->mtx);
  1238. return;
  1239. }
  1240. __ieee80211_stop_poll(sdata);
  1241. mutex_lock(&local->iflist_mtx);
  1242. ieee80211_recalc_ps(local, -1);
  1243. mutex_unlock(&local->iflist_mtx);
  1244. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  1245. goto out;
  1246. /*
  1247. * We've received a probe response, but are not sure whether
  1248. * we have or will be receiving any beacons or data, so let's
  1249. * schedule the timers again, just in case.
  1250. */
  1251. ieee80211_sta_reset_beacon_monitor(sdata);
  1252. mod_timer(&ifmgd->conn_mon_timer,
  1253. round_jiffies_up(jiffies +
  1254. IEEE80211_CONNECTION_IDLE_TIME));
  1255. out:
  1256. mutex_unlock(&local->mtx);
  1257. }
  1258. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  1259. struct ieee80211_hdr *hdr, bool ack)
  1260. {
  1261. if (!ieee80211_is_data(hdr->frame_control))
  1262. return;
  1263. if (ack)
  1264. ieee80211_sta_reset_conn_monitor(sdata);
  1265. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  1266. sdata->u.mgd.probe_send_count > 0) {
  1267. if (ack)
  1268. sdata->u.mgd.probe_send_count = 0;
  1269. else
  1270. sdata->u.mgd.nullfunc_failed = true;
  1271. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1272. }
  1273. }
  1274. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  1275. {
  1276. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1277. const u8 *ssid;
  1278. u8 *dst = ifmgd->associated->bssid;
  1279. u8 unicast_limit = max(1, max_probe_tries - 3);
  1280. /*
  1281. * Try sending broadcast probe requests for the last three
  1282. * probe requests after the first ones failed since some
  1283. * buggy APs only support broadcast probe requests.
  1284. */
  1285. if (ifmgd->probe_send_count >= unicast_limit)
  1286. dst = NULL;
  1287. /*
  1288. * When the hardware reports an accurate Tx ACK status, it's
  1289. * better to send a nullfunc frame instead of a probe request,
  1290. * as it will kick us off the AP quickly if we aren't associated
  1291. * anymore. The timeout will be reset if the frame is ACKed by
  1292. * the AP.
  1293. */
  1294. ifmgd->probe_send_count++;
  1295. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  1296. ifmgd->nullfunc_failed = false;
  1297. ieee80211_send_nullfunc(sdata->local, sdata, 0);
  1298. } else {
  1299. int ssid_len;
  1300. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1301. if (WARN_ON_ONCE(ssid == NULL))
  1302. ssid_len = 0;
  1303. else
  1304. ssid_len = ssid[1];
  1305. ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
  1306. 0, (u32) -1, true, false,
  1307. ifmgd->associated->channel);
  1308. }
  1309. ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
  1310. run_again(ifmgd, ifmgd->probe_timeout);
  1311. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1312. drv_flush(sdata->local, false);
  1313. }
  1314. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  1315. bool beacon)
  1316. {
  1317. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1318. bool already = false;
  1319. if (!ieee80211_sdata_running(sdata))
  1320. return;
  1321. mutex_lock(&ifmgd->mtx);
  1322. if (!ifmgd->associated)
  1323. goto out;
  1324. mutex_lock(&sdata->local->mtx);
  1325. if (sdata->local->tmp_channel || sdata->local->scanning) {
  1326. mutex_unlock(&sdata->local->mtx);
  1327. goto out;
  1328. }
  1329. if (beacon)
  1330. mlme_dbg_ratelimited(sdata,
  1331. "detected beacon loss from AP - sending probe request\n");
  1332. ieee80211_cqm_rssi_notify(&sdata->vif,
  1333. NL80211_CQM_RSSI_BEACON_LOSS_EVENT, GFP_KERNEL);
  1334. /*
  1335. * The driver/our work has already reported this event or the
  1336. * connection monitoring has kicked in and we have already sent
  1337. * a probe request. Or maybe the AP died and the driver keeps
  1338. * reporting until we disassociate...
  1339. *
  1340. * In either case we have to ignore the current call to this
  1341. * function (except for setting the correct probe reason bit)
  1342. * because otherwise we would reset the timer every time and
  1343. * never check whether we received a probe response!
  1344. */
  1345. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1346. IEEE80211_STA_CONNECTION_POLL))
  1347. already = true;
  1348. if (beacon)
  1349. ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
  1350. else
  1351. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  1352. mutex_unlock(&sdata->local->mtx);
  1353. if (already)
  1354. goto out;
  1355. mutex_lock(&sdata->local->iflist_mtx);
  1356. ieee80211_recalc_ps(sdata->local, -1);
  1357. mutex_unlock(&sdata->local->iflist_mtx);
  1358. ifmgd->probe_send_count = 0;
  1359. ieee80211_mgd_probe_ap_send(sdata);
  1360. out:
  1361. mutex_unlock(&ifmgd->mtx);
  1362. }
  1363. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  1364. struct ieee80211_vif *vif)
  1365. {
  1366. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1367. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1368. struct cfg80211_bss *cbss;
  1369. struct sk_buff *skb;
  1370. const u8 *ssid;
  1371. int ssid_len;
  1372. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1373. return NULL;
  1374. ASSERT_MGD_MTX(ifmgd);
  1375. if (ifmgd->associated)
  1376. cbss = ifmgd->associated;
  1377. else if (ifmgd->auth_data)
  1378. cbss = ifmgd->auth_data->bss;
  1379. else if (ifmgd->assoc_data)
  1380. cbss = ifmgd->assoc_data->bss;
  1381. else
  1382. return NULL;
  1383. ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
  1384. if (WARN_ON_ONCE(ssid == NULL))
  1385. ssid_len = 0;
  1386. else
  1387. ssid_len = ssid[1];
  1388. skb = ieee80211_build_probe_req(sdata, cbss->bssid,
  1389. (u32) -1,
  1390. sdata->local->oper_channel,
  1391. ssid + 2, ssid_len,
  1392. NULL, 0, true);
  1393. return skb;
  1394. }
  1395. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  1396. static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata,
  1397. bool transmit_frame)
  1398. {
  1399. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1400. struct ieee80211_local *local = sdata->local;
  1401. u8 frame_buf[DEAUTH_DISASSOC_LEN];
  1402. mutex_lock(&ifmgd->mtx);
  1403. if (!ifmgd->associated) {
  1404. mutex_unlock(&ifmgd->mtx);
  1405. return;
  1406. }
  1407. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  1408. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1409. transmit_frame, frame_buf);
  1410. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  1411. mutex_unlock(&ifmgd->mtx);
  1412. /*
  1413. * must be outside lock due to cfg80211,
  1414. * but that's not a problem.
  1415. */
  1416. cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
  1417. mutex_lock(&local->mtx);
  1418. ieee80211_recalc_idle(local);
  1419. mutex_unlock(&local->mtx);
  1420. }
  1421. static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  1422. {
  1423. struct ieee80211_sub_if_data *sdata =
  1424. container_of(work, struct ieee80211_sub_if_data,
  1425. u.mgd.beacon_connection_loss_work);
  1426. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1427. struct sta_info *sta;
  1428. if (ifmgd->associated) {
  1429. rcu_read_lock();
  1430. sta = sta_info_get(sdata, ifmgd->bssid);
  1431. if (sta)
  1432. sta->beacon_loss_count++;
  1433. rcu_read_unlock();
  1434. }
  1435. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) {
  1436. sdata_info(sdata, "Connection to AP %pM lost\n",
  1437. ifmgd->bssid);
  1438. __ieee80211_disconnect(sdata, false);
  1439. } else {
  1440. ieee80211_mgd_probe_ap(sdata, true);
  1441. }
  1442. }
  1443. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  1444. {
  1445. struct ieee80211_sub_if_data *sdata =
  1446. container_of(work, struct ieee80211_sub_if_data,
  1447. u.mgd.csa_connection_drop_work);
  1448. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  1449. IEEE80211_QUEUE_STOP_REASON_CSA);
  1450. __ieee80211_disconnect(sdata, true);
  1451. }
  1452. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  1453. {
  1454. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1455. struct ieee80211_hw *hw = &sdata->local->hw;
  1456. trace_api_beacon_loss(sdata);
  1457. WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
  1458. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1459. }
  1460. EXPORT_SYMBOL(ieee80211_beacon_loss);
  1461. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  1462. {
  1463. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1464. struct ieee80211_hw *hw = &sdata->local->hw;
  1465. trace_api_connection_loss(sdata);
  1466. WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
  1467. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1468. }
  1469. EXPORT_SYMBOL(ieee80211_connection_loss);
  1470. static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
  1471. bool assoc)
  1472. {
  1473. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1474. lockdep_assert_held(&sdata->u.mgd.mtx);
  1475. if (!assoc) {
  1476. sta_info_destroy_addr(sdata, auth_data->bss->bssid);
  1477. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1478. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1479. }
  1480. cfg80211_put_bss(auth_data->bss);
  1481. kfree(auth_data);
  1482. sdata->u.mgd.auth_data = NULL;
  1483. }
  1484. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  1485. struct ieee80211_mgmt *mgmt, size_t len)
  1486. {
  1487. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1488. u8 *pos;
  1489. struct ieee802_11_elems elems;
  1490. pos = mgmt->u.auth.variable;
  1491. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1492. if (!elems.challenge)
  1493. return;
  1494. auth_data->expected_transaction = 4;
  1495. drv_mgd_prepare_tx(sdata->local, sdata);
  1496. ieee80211_send_auth(sdata, 3, auth_data->algorithm,
  1497. elems.challenge - 2, elems.challenge_len + 2,
  1498. auth_data->bss->bssid, auth_data->bss->bssid,
  1499. auth_data->key, auth_data->key_len,
  1500. auth_data->key_idx);
  1501. }
  1502. static enum rx_mgmt_action __must_check
  1503. ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  1504. struct ieee80211_mgmt *mgmt, size_t len)
  1505. {
  1506. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1507. u8 bssid[ETH_ALEN];
  1508. u16 auth_alg, auth_transaction, status_code;
  1509. struct sta_info *sta;
  1510. lockdep_assert_held(&ifmgd->mtx);
  1511. if (len < 24 + 6)
  1512. return RX_MGMT_NONE;
  1513. if (!ifmgd->auth_data || ifmgd->auth_data->done)
  1514. return RX_MGMT_NONE;
  1515. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  1516. if (!ether_addr_equal(bssid, mgmt->bssid))
  1517. return RX_MGMT_NONE;
  1518. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  1519. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  1520. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  1521. if (auth_alg != ifmgd->auth_data->algorithm ||
  1522. auth_transaction != ifmgd->auth_data->expected_transaction)
  1523. return RX_MGMT_NONE;
  1524. if (status_code != WLAN_STATUS_SUCCESS) {
  1525. sdata_info(sdata, "%pM denied authentication (status %d)\n",
  1526. mgmt->sa, status_code);
  1527. ieee80211_destroy_auth_data(sdata, false);
  1528. return RX_MGMT_CFG80211_RX_AUTH;
  1529. }
  1530. switch (ifmgd->auth_data->algorithm) {
  1531. case WLAN_AUTH_OPEN:
  1532. case WLAN_AUTH_LEAP:
  1533. case WLAN_AUTH_FT:
  1534. break;
  1535. case WLAN_AUTH_SHARED_KEY:
  1536. if (ifmgd->auth_data->expected_transaction != 4) {
  1537. ieee80211_auth_challenge(sdata, mgmt, len);
  1538. /* need another frame */
  1539. return RX_MGMT_NONE;
  1540. }
  1541. break;
  1542. default:
  1543. WARN_ONCE(1, "invalid auth alg %d",
  1544. ifmgd->auth_data->algorithm);
  1545. return RX_MGMT_NONE;
  1546. }
  1547. sdata_info(sdata, "authenticated\n");
  1548. ifmgd->auth_data->done = true;
  1549. ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
  1550. run_again(ifmgd, ifmgd->auth_data->timeout);
  1551. /* move station state to auth */
  1552. mutex_lock(&sdata->local->sta_mtx);
  1553. sta = sta_info_get(sdata, bssid);
  1554. if (!sta) {
  1555. WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
  1556. goto out_err;
  1557. }
  1558. if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
  1559. sdata_info(sdata, "failed moving %pM to auth\n", bssid);
  1560. goto out_err;
  1561. }
  1562. mutex_unlock(&sdata->local->sta_mtx);
  1563. return RX_MGMT_CFG80211_RX_AUTH;
  1564. out_err:
  1565. mutex_unlock(&sdata->local->sta_mtx);
  1566. /* ignore frame -- wait for timeout */
  1567. return RX_MGMT_NONE;
  1568. }
  1569. static enum rx_mgmt_action __must_check
  1570. ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1571. struct ieee80211_mgmt *mgmt, size_t len)
  1572. {
  1573. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1574. const u8 *bssid = NULL;
  1575. u16 reason_code;
  1576. lockdep_assert_held(&ifmgd->mtx);
  1577. if (len < 24 + 2)
  1578. return RX_MGMT_NONE;
  1579. if (!ifmgd->associated ||
  1580. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1581. return RX_MGMT_NONE;
  1582. bssid = ifmgd->associated->bssid;
  1583. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1584. sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
  1585. bssid, reason_code);
  1586. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1587. mutex_lock(&sdata->local->mtx);
  1588. ieee80211_recalc_idle(sdata->local);
  1589. mutex_unlock(&sdata->local->mtx);
  1590. return RX_MGMT_CFG80211_DEAUTH;
  1591. }
  1592. static enum rx_mgmt_action __must_check
  1593. ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1594. struct ieee80211_mgmt *mgmt, size_t len)
  1595. {
  1596. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1597. u16 reason_code;
  1598. lockdep_assert_held(&ifmgd->mtx);
  1599. if (len < 24 + 2)
  1600. return RX_MGMT_NONE;
  1601. if (!ifmgd->associated ||
  1602. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1603. return RX_MGMT_NONE;
  1604. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1605. sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
  1606. mgmt->sa, reason_code);
  1607. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1608. mutex_lock(&sdata->local->mtx);
  1609. ieee80211_recalc_idle(sdata->local);
  1610. mutex_unlock(&sdata->local->mtx);
  1611. return RX_MGMT_CFG80211_DISASSOC;
  1612. }
  1613. static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  1614. u8 *supp_rates, unsigned int supp_rates_len,
  1615. u32 *rates, u32 *basic_rates,
  1616. bool *have_higher_than_11mbit,
  1617. int *min_rate, int *min_rate_index)
  1618. {
  1619. int i, j;
  1620. for (i = 0; i < supp_rates_len; i++) {
  1621. int rate = (supp_rates[i] & 0x7f) * 5;
  1622. bool is_basic = !!(supp_rates[i] & 0x80);
  1623. if (rate > 110)
  1624. *have_higher_than_11mbit = true;
  1625. /*
  1626. * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
  1627. * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
  1628. *
  1629. * Note: Even through the membership selector and the basic
  1630. * rate flag share the same bit, they are not exactly
  1631. * the same.
  1632. */
  1633. if (!!(supp_rates[i] & 0x80) &&
  1634. (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1635. continue;
  1636. for (j = 0; j < sband->n_bitrates; j++) {
  1637. if (sband->bitrates[j].bitrate == rate) {
  1638. *rates |= BIT(j);
  1639. if (is_basic)
  1640. *basic_rates |= BIT(j);
  1641. if (rate < *min_rate) {
  1642. *min_rate = rate;
  1643. *min_rate_index = j;
  1644. }
  1645. break;
  1646. }
  1647. }
  1648. }
  1649. }
  1650. static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  1651. bool assoc)
  1652. {
  1653. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  1654. lockdep_assert_held(&sdata->u.mgd.mtx);
  1655. if (!assoc) {
  1656. sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
  1657. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1658. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1659. }
  1660. kfree(assoc_data);
  1661. sdata->u.mgd.assoc_data = NULL;
  1662. }
  1663. static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
  1664. struct cfg80211_bss *cbss,
  1665. struct ieee80211_mgmt *mgmt, size_t len)
  1666. {
  1667. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1668. struct ieee80211_local *local = sdata->local;
  1669. struct ieee80211_supported_band *sband;
  1670. struct sta_info *sta;
  1671. u8 *pos;
  1672. u16 capab_info, aid;
  1673. struct ieee802_11_elems elems;
  1674. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1675. u32 changed = 0;
  1676. int err;
  1677. /* AssocResp and ReassocResp have identical structure */
  1678. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1679. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1680. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1681. sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
  1682. aid);
  1683. aid &= ~(BIT(15) | BIT(14));
  1684. ifmgd->broken_ap = false;
  1685. if (aid == 0 || aid > IEEE80211_MAX_AID) {
  1686. sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
  1687. aid);
  1688. aid = 0;
  1689. ifmgd->broken_ap = true;
  1690. }
  1691. pos = mgmt->u.assoc_resp.variable;
  1692. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1693. if (!elems.supp_rates) {
  1694. sdata_info(sdata, "no SuppRates element in AssocResp\n");
  1695. return false;
  1696. }
  1697. ifmgd->aid = aid;
  1698. mutex_lock(&sdata->local->sta_mtx);
  1699. /*
  1700. * station info was already allocated and inserted before
  1701. * the association and should be available to us
  1702. */
  1703. sta = sta_info_get(sdata, cbss->bssid);
  1704. if (WARN_ON(!sta)) {
  1705. mutex_unlock(&sdata->local->sta_mtx);
  1706. return false;
  1707. }
  1708. sband = local->hw.wiphy->bands[local->oper_channel->band];
  1709. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1710. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  1711. elems.ht_cap_elem, &sta->sta.ht_cap);
  1712. sta->supports_40mhz =
  1713. sta->sta.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  1714. rate_control_rate_init(sta);
  1715. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  1716. set_sta_flag(sta, WLAN_STA_MFP);
  1717. if (elems.wmm_param)
  1718. set_sta_flag(sta, WLAN_STA_WME);
  1719. err = sta_info_move_state(sta, IEEE80211_STA_AUTH);
  1720. if (!err)
  1721. err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  1722. if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  1723. err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  1724. if (err) {
  1725. sdata_info(sdata,
  1726. "failed to move station %pM to desired state\n",
  1727. sta->sta.addr);
  1728. WARN_ON(__sta_info_destroy(sta));
  1729. mutex_unlock(&sdata->local->sta_mtx);
  1730. return false;
  1731. }
  1732. mutex_unlock(&sdata->local->sta_mtx);
  1733. /*
  1734. * Always handle WMM once after association regardless
  1735. * of the first value the AP uses. Setting -1 here has
  1736. * that effect because the AP values is an unsigned
  1737. * 4-bit value.
  1738. */
  1739. ifmgd->wmm_last_param_set = -1;
  1740. if (elems.wmm_param)
  1741. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  1742. elems.wmm_param_len);
  1743. else
  1744. ieee80211_set_wmm_default(sdata, false);
  1745. changed |= BSS_CHANGED_QOS;
  1746. if (elems.ht_operation && elems.wmm_param &&
  1747. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1748. changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
  1749. cbss->bssid, false);
  1750. /* set AID and assoc capability,
  1751. * ieee80211_set_associated() will tell the driver */
  1752. bss_conf->aid = aid;
  1753. bss_conf->assoc_capability = capab_info;
  1754. ieee80211_set_associated(sdata, cbss, changed);
  1755. /*
  1756. * If we're using 4-addr mode, let the AP know that we're
  1757. * doing so, so that it can create the STA VLAN on its side
  1758. */
  1759. if (ifmgd->use_4addr)
  1760. ieee80211_send_4addr_nullfunc(local, sdata);
  1761. /*
  1762. * Start timer to probe the connection to the AP now.
  1763. * Also start the timer that will detect beacon loss.
  1764. */
  1765. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  1766. ieee80211_sta_reset_beacon_monitor(sdata);
  1767. return true;
  1768. }
  1769. static enum rx_mgmt_action __must_check
  1770. ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  1771. struct ieee80211_mgmt *mgmt, size_t len,
  1772. struct cfg80211_bss **bss)
  1773. {
  1774. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1775. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  1776. u16 capab_info, status_code, aid;
  1777. struct ieee802_11_elems elems;
  1778. u8 *pos;
  1779. bool reassoc;
  1780. lockdep_assert_held(&ifmgd->mtx);
  1781. if (!assoc_data)
  1782. return RX_MGMT_NONE;
  1783. if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
  1784. return RX_MGMT_NONE;
  1785. /*
  1786. * AssocResp and ReassocResp have identical structure, so process both
  1787. * of them in this function.
  1788. */
  1789. if (len < 24 + 6)
  1790. return RX_MGMT_NONE;
  1791. reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
  1792. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1793. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1794. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1795. sdata_info(sdata,
  1796. "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
  1797. reassoc ? "Rea" : "A", mgmt->sa,
  1798. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1799. pos = mgmt->u.assoc_resp.variable;
  1800. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1801. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  1802. elems.timeout_int && elems.timeout_int_len == 5 &&
  1803. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  1804. u32 tu, ms;
  1805. tu = get_unaligned_le32(elems.timeout_int + 1);
  1806. ms = tu * 1024 / 1000;
  1807. sdata_info(sdata,
  1808. "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
  1809. mgmt->sa, tu, ms);
  1810. assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
  1811. if (ms > IEEE80211_ASSOC_TIMEOUT)
  1812. run_again(ifmgd, assoc_data->timeout);
  1813. return RX_MGMT_NONE;
  1814. }
  1815. *bss = assoc_data->bss;
  1816. if (status_code != WLAN_STATUS_SUCCESS) {
  1817. sdata_info(sdata, "%pM denied association (code=%d)\n",
  1818. mgmt->sa, status_code);
  1819. ieee80211_destroy_assoc_data(sdata, false);
  1820. } else {
  1821. if (!ieee80211_assoc_success(sdata, *bss, mgmt, len)) {
  1822. /* oops -- internal error -- send timeout for now */
  1823. ieee80211_destroy_assoc_data(sdata, false);
  1824. cfg80211_put_bss(*bss);
  1825. return RX_MGMT_CFG80211_ASSOC_TIMEOUT;
  1826. }
  1827. sdata_info(sdata, "associated\n");
  1828. /*
  1829. * destroy assoc_data afterwards, as otherwise an idle
  1830. * recalc after assoc_data is NULL but before associated
  1831. * is set can cause the interface to go idle
  1832. */
  1833. ieee80211_destroy_assoc_data(sdata, true);
  1834. }
  1835. return RX_MGMT_CFG80211_RX_ASSOC;
  1836. }
  1837. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1838. struct ieee80211_mgmt *mgmt,
  1839. size_t len,
  1840. struct ieee80211_rx_status *rx_status,
  1841. struct ieee802_11_elems *elems,
  1842. bool beacon)
  1843. {
  1844. struct ieee80211_local *local = sdata->local;
  1845. int freq;
  1846. struct ieee80211_bss *bss;
  1847. struct ieee80211_channel *channel;
  1848. bool need_ps = false;
  1849. if (sdata->u.mgd.associated &&
  1850. ether_addr_equal(mgmt->bssid, sdata->u.mgd.associated->bssid)) {
  1851. bss = (void *)sdata->u.mgd.associated->priv;
  1852. /* not previously set so we may need to recalc */
  1853. need_ps = !bss->dtim_period;
  1854. }
  1855. if (elems->ds_params && elems->ds_params_len == 1)
  1856. freq = ieee80211_channel_to_frequency(elems->ds_params[0],
  1857. rx_status->band);
  1858. else
  1859. freq = rx_status->freq;
  1860. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1861. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1862. return;
  1863. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1864. channel, beacon);
  1865. if (bss)
  1866. ieee80211_rx_bss_put(local, bss);
  1867. if (!sdata->u.mgd.associated)
  1868. return;
  1869. if (need_ps) {
  1870. mutex_lock(&local->iflist_mtx);
  1871. ieee80211_recalc_ps(local, -1);
  1872. mutex_unlock(&local->iflist_mtx);
  1873. }
  1874. if (elems->ch_switch_ie &&
  1875. memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid, ETH_ALEN) == 0)
  1876. ieee80211_sta_process_chanswitch(sdata, elems->ch_switch_ie,
  1877. bss, rx_status->mactime);
  1878. }
  1879. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1880. struct sk_buff *skb)
  1881. {
  1882. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1883. struct ieee80211_if_managed *ifmgd;
  1884. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  1885. size_t baselen, len = skb->len;
  1886. struct ieee802_11_elems elems;
  1887. ifmgd = &sdata->u.mgd;
  1888. ASSERT_MGD_MTX(ifmgd);
  1889. if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
  1890. return; /* ignore ProbeResp to foreign address */
  1891. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1892. if (baselen > len)
  1893. return;
  1894. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1895. &elems);
  1896. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1897. if (ifmgd->associated &&
  1898. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1899. ieee80211_reset_ap_probe(sdata);
  1900. if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
  1901. ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
  1902. /* got probe response, continue with auth */
  1903. sdata_info(sdata, "direct probe responded\n");
  1904. ifmgd->auth_data->tries = 0;
  1905. ifmgd->auth_data->timeout = jiffies;
  1906. run_again(ifmgd, ifmgd->auth_data->timeout);
  1907. }
  1908. }
  1909. /*
  1910. * This is the canonical list of information elements we care about,
  1911. * the filter code also gives us all changes to the Microsoft OUI
  1912. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  1913. *
  1914. * We implement beacon filtering in software since that means we can
  1915. * avoid processing the frame here and in cfg80211, and userspace
  1916. * will not be able to tell whether the hardware supports it or not.
  1917. *
  1918. * XXX: This list needs to be dynamic -- userspace needs to be able to
  1919. * add items it requires. It also needs to be able to tell us to
  1920. * look out for other vendor IEs.
  1921. */
  1922. static const u64 care_about_ies =
  1923. (1ULL << WLAN_EID_COUNTRY) |
  1924. (1ULL << WLAN_EID_ERP_INFO) |
  1925. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  1926. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  1927. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1928. (1ULL << WLAN_EID_HT_OPERATION);
  1929. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1930. struct ieee80211_mgmt *mgmt,
  1931. size_t len,
  1932. struct ieee80211_rx_status *rx_status)
  1933. {
  1934. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1935. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1936. size_t baselen;
  1937. struct ieee802_11_elems elems;
  1938. struct ieee80211_local *local = sdata->local;
  1939. u32 changed = 0;
  1940. bool erp_valid, directed_tim = false;
  1941. u8 erp_value = 0;
  1942. u32 ncrc;
  1943. u8 *bssid;
  1944. lockdep_assert_held(&ifmgd->mtx);
  1945. /* Process beacon from the current BSS */
  1946. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1947. if (baselen > len)
  1948. return;
  1949. if (rx_status->freq != local->oper_channel->center_freq)
  1950. return;
  1951. if (ifmgd->assoc_data && !ifmgd->assoc_data->have_beacon &&
  1952. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  1953. ieee802_11_parse_elems(mgmt->u.beacon.variable,
  1954. len - baselen, &elems);
  1955. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  1956. false);
  1957. ifmgd->assoc_data->have_beacon = true;
  1958. ifmgd->assoc_data->sent_assoc = false;
  1959. /* continue assoc process */
  1960. ifmgd->assoc_data->timeout = jiffies;
  1961. run_again(ifmgd, ifmgd->assoc_data->timeout);
  1962. return;
  1963. }
  1964. if (!ifmgd->associated ||
  1965. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1966. return;
  1967. bssid = ifmgd->associated->bssid;
  1968. /* Track average RSSI from the Beacon frames of the current AP */
  1969. ifmgd->last_beacon_signal = rx_status->signal;
  1970. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  1971. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  1972. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  1973. ifmgd->last_cqm_event_signal = 0;
  1974. ifmgd->count_beacon_signal = 1;
  1975. ifmgd->last_ave_beacon_signal = 0;
  1976. } else {
  1977. ifmgd->ave_beacon_signal =
  1978. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  1979. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  1980. ifmgd->ave_beacon_signal) / 16;
  1981. ifmgd->count_beacon_signal++;
  1982. }
  1983. if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
  1984. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  1985. int sig = ifmgd->ave_beacon_signal;
  1986. int last_sig = ifmgd->last_ave_beacon_signal;
  1987. /*
  1988. * if signal crosses either of the boundaries, invoke callback
  1989. * with appropriate parameters
  1990. */
  1991. if (sig > ifmgd->rssi_max_thold &&
  1992. (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
  1993. ifmgd->last_ave_beacon_signal = sig;
  1994. drv_rssi_callback(local, RSSI_EVENT_HIGH);
  1995. } else if (sig < ifmgd->rssi_min_thold &&
  1996. (last_sig >= ifmgd->rssi_max_thold ||
  1997. last_sig == 0)) {
  1998. ifmgd->last_ave_beacon_signal = sig;
  1999. drv_rssi_callback(local, RSSI_EVENT_LOW);
  2000. }
  2001. }
  2002. if (bss_conf->cqm_rssi_thold &&
  2003. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  2004. !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
  2005. int sig = ifmgd->ave_beacon_signal / 16;
  2006. int last_event = ifmgd->last_cqm_event_signal;
  2007. int thold = bss_conf->cqm_rssi_thold;
  2008. int hyst = bss_conf->cqm_rssi_hyst;
  2009. if (sig < thold &&
  2010. (last_event == 0 || sig < last_event - hyst)) {
  2011. ifmgd->last_cqm_event_signal = sig;
  2012. ieee80211_cqm_rssi_notify(
  2013. &sdata->vif,
  2014. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  2015. GFP_KERNEL);
  2016. } else if (sig > thold &&
  2017. (last_event == 0 || sig > last_event + hyst)) {
  2018. ifmgd->last_cqm_event_signal = sig;
  2019. ieee80211_cqm_rssi_notify(
  2020. &sdata->vif,
  2021. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  2022. GFP_KERNEL);
  2023. }
  2024. }
  2025. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  2026. mlme_dbg_ratelimited(sdata,
  2027. "cancelling probereq poll due to a received beacon\n");
  2028. mutex_lock(&local->mtx);
  2029. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  2030. ieee80211_run_deferred_scan(local);
  2031. mutex_unlock(&local->mtx);
  2032. mutex_lock(&local->iflist_mtx);
  2033. ieee80211_recalc_ps(local, -1);
  2034. mutex_unlock(&local->iflist_mtx);
  2035. }
  2036. /*
  2037. * Push the beacon loss detection into the future since
  2038. * we are processing a beacon from the AP just now.
  2039. */
  2040. ieee80211_sta_reset_beacon_monitor(sdata);
  2041. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  2042. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  2043. len - baselen, &elems,
  2044. care_about_ies, ncrc);
  2045. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  2046. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  2047. ifmgd->aid);
  2048. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  2049. if (directed_tim) {
  2050. if (local->hw.conf.dynamic_ps_timeout > 0) {
  2051. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2052. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2053. ieee80211_hw_config(local,
  2054. IEEE80211_CONF_CHANGE_PS);
  2055. }
  2056. ieee80211_send_nullfunc(local, sdata, 0);
  2057. } else if (!local->pspolling && sdata->u.mgd.powersave) {
  2058. local->pspolling = true;
  2059. /*
  2060. * Here is assumed that the driver will be
  2061. * able to send ps-poll frame and receive a
  2062. * response even though power save mode is
  2063. * enabled, but some drivers might require
  2064. * to disable power save here. This needs
  2065. * to be investigated.
  2066. */
  2067. ieee80211_send_pspoll(local, sdata);
  2068. }
  2069. }
  2070. }
  2071. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  2072. return;
  2073. ifmgd->beacon_crc = ncrc;
  2074. ifmgd->beacon_crc_valid = true;
  2075. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  2076. true);
  2077. if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2078. elems.wmm_param_len))
  2079. changed |= BSS_CHANGED_QOS;
  2080. if (elems.erp_info && elems.erp_info_len >= 1) {
  2081. erp_valid = true;
  2082. erp_value = elems.erp_info[0];
  2083. } else {
  2084. erp_valid = false;
  2085. }
  2086. changed |= ieee80211_handle_bss_capability(sdata,
  2087. le16_to_cpu(mgmt->u.beacon.capab_info),
  2088. erp_valid, erp_value);
  2089. if (elems.ht_cap_elem && elems.ht_operation && elems.wmm_param &&
  2090. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
  2091. struct ieee80211_supported_band *sband;
  2092. sband = local->hw.wiphy->bands[local->oper_channel->band];
  2093. changed |= ieee80211_config_ht_tx(sdata, elems.ht_operation,
  2094. bssid, true);
  2095. }
  2096. /* Note: country IE parsing is done for us by cfg80211 */
  2097. if (elems.country_elem) {
  2098. /* TODO: IBSS also needs this */
  2099. if (elems.pwr_constr_elem)
  2100. ieee80211_handle_pwr_constr(sdata,
  2101. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  2102. elems.pwr_constr_elem);
  2103. }
  2104. ieee80211_bss_info_change_notify(sdata, changed);
  2105. }
  2106. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  2107. struct sk_buff *skb)
  2108. {
  2109. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2110. struct ieee80211_rx_status *rx_status;
  2111. struct ieee80211_mgmt *mgmt;
  2112. struct cfg80211_bss *bss = NULL;
  2113. enum rx_mgmt_action rma = RX_MGMT_NONE;
  2114. u16 fc;
  2115. rx_status = (struct ieee80211_rx_status *) skb->cb;
  2116. mgmt = (struct ieee80211_mgmt *) skb->data;
  2117. fc = le16_to_cpu(mgmt->frame_control);
  2118. mutex_lock(&ifmgd->mtx);
  2119. switch (fc & IEEE80211_FCTL_STYPE) {
  2120. case IEEE80211_STYPE_BEACON:
  2121. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  2122. break;
  2123. case IEEE80211_STYPE_PROBE_RESP:
  2124. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  2125. break;
  2126. case IEEE80211_STYPE_AUTH:
  2127. rma = ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  2128. break;
  2129. case IEEE80211_STYPE_DEAUTH:
  2130. rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  2131. break;
  2132. case IEEE80211_STYPE_DISASSOC:
  2133. rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  2134. break;
  2135. case IEEE80211_STYPE_ASSOC_RESP:
  2136. case IEEE80211_STYPE_REASSOC_RESP:
  2137. rma = ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, &bss);
  2138. break;
  2139. case IEEE80211_STYPE_ACTION:
  2140. switch (mgmt->u.action.category) {
  2141. case WLAN_CATEGORY_SPECTRUM_MGMT:
  2142. ieee80211_sta_process_chanswitch(sdata,
  2143. &mgmt->u.action.u.chan_switch.sw_elem,
  2144. (void *)ifmgd->associated->priv,
  2145. rx_status->mactime);
  2146. break;
  2147. }
  2148. }
  2149. mutex_unlock(&ifmgd->mtx);
  2150. switch (rma) {
  2151. case RX_MGMT_NONE:
  2152. /* no action */
  2153. break;
  2154. case RX_MGMT_CFG80211_DEAUTH:
  2155. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  2156. break;
  2157. case RX_MGMT_CFG80211_DISASSOC:
  2158. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  2159. break;
  2160. case RX_MGMT_CFG80211_RX_AUTH:
  2161. cfg80211_send_rx_auth(sdata->dev, (u8 *)mgmt, skb->len);
  2162. break;
  2163. case RX_MGMT_CFG80211_RX_ASSOC:
  2164. cfg80211_send_rx_assoc(sdata->dev, bss, (u8 *)mgmt, skb->len);
  2165. break;
  2166. case RX_MGMT_CFG80211_ASSOC_TIMEOUT:
  2167. cfg80211_send_assoc_timeout(sdata->dev, mgmt->bssid);
  2168. break;
  2169. default:
  2170. WARN(1, "unexpected: %d", rma);
  2171. }
  2172. }
  2173. static void ieee80211_sta_timer(unsigned long data)
  2174. {
  2175. struct ieee80211_sub_if_data *sdata =
  2176. (struct ieee80211_sub_if_data *) data;
  2177. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2178. struct ieee80211_local *local = sdata->local;
  2179. if (local->quiescing) {
  2180. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  2181. return;
  2182. }
  2183. ieee80211_queue_work(&local->hw, &sdata->work);
  2184. }
  2185. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  2186. u8 *bssid, u8 reason)
  2187. {
  2188. struct ieee80211_local *local = sdata->local;
  2189. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2190. u8 frame_buf[DEAUTH_DISASSOC_LEN];
  2191. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
  2192. false, frame_buf);
  2193. mutex_unlock(&ifmgd->mtx);
  2194. /*
  2195. * must be outside lock due to cfg80211,
  2196. * but that's not a problem.
  2197. */
  2198. cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
  2199. mutex_lock(&local->mtx);
  2200. ieee80211_recalc_idle(local);
  2201. mutex_unlock(&local->mtx);
  2202. mutex_lock(&ifmgd->mtx);
  2203. }
  2204. static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
  2205. {
  2206. struct ieee80211_local *local = sdata->local;
  2207. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2208. struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
  2209. lockdep_assert_held(&ifmgd->mtx);
  2210. if (WARN_ON_ONCE(!auth_data))
  2211. return -EINVAL;
  2212. auth_data->tries++;
  2213. if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
  2214. sdata_info(sdata, "authentication with %pM timed out\n",
  2215. auth_data->bss->bssid);
  2216. /*
  2217. * Most likely AP is not in the range so remove the
  2218. * bss struct for that AP.
  2219. */
  2220. cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
  2221. return -ETIMEDOUT;
  2222. }
  2223. drv_mgd_prepare_tx(local, sdata);
  2224. if (auth_data->bss->proberesp_ies) {
  2225. sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
  2226. auth_data->bss->bssid, auth_data->tries,
  2227. IEEE80211_AUTH_MAX_TRIES);
  2228. auth_data->expected_transaction = 2;
  2229. ieee80211_send_auth(sdata, 1, auth_data->algorithm,
  2230. auth_data->ie, auth_data->ie_len,
  2231. auth_data->bss->bssid,
  2232. auth_data->bss->bssid, NULL, 0, 0);
  2233. } else {
  2234. const u8 *ssidie;
  2235. sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
  2236. auth_data->bss->bssid, auth_data->tries,
  2237. IEEE80211_AUTH_MAX_TRIES);
  2238. ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
  2239. if (!ssidie)
  2240. return -EINVAL;
  2241. /*
  2242. * Direct probe is sent to broadcast address as some APs
  2243. * will not answer to direct packet in unassociated state.
  2244. */
  2245. ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
  2246. NULL, 0, (u32) -1, true, false,
  2247. auth_data->bss->channel);
  2248. }
  2249. auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  2250. run_again(ifmgd, auth_data->timeout);
  2251. return 0;
  2252. }
  2253. static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
  2254. {
  2255. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2256. struct ieee80211_local *local = sdata->local;
  2257. lockdep_assert_held(&sdata->u.mgd.mtx);
  2258. assoc_data->tries++;
  2259. if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
  2260. sdata_info(sdata, "association with %pM timed out\n",
  2261. assoc_data->bss->bssid);
  2262. /*
  2263. * Most likely AP is not in the range so remove the
  2264. * bss struct for that AP.
  2265. */
  2266. cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
  2267. return -ETIMEDOUT;
  2268. }
  2269. sdata_info(sdata, "associate with %pM (try %d/%d)\n",
  2270. assoc_data->bss->bssid, assoc_data->tries,
  2271. IEEE80211_ASSOC_MAX_TRIES);
  2272. ieee80211_send_assoc(sdata);
  2273. assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  2274. run_again(&sdata->u.mgd, assoc_data->timeout);
  2275. return 0;
  2276. }
  2277. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  2278. {
  2279. struct ieee80211_local *local = sdata->local;
  2280. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2281. mutex_lock(&ifmgd->mtx);
  2282. if (ifmgd->auth_data &&
  2283. time_after(jiffies, ifmgd->auth_data->timeout)) {
  2284. if (ifmgd->auth_data->done) {
  2285. /*
  2286. * ok ... we waited for assoc but userspace didn't,
  2287. * so let's just kill the auth data
  2288. */
  2289. ieee80211_destroy_auth_data(sdata, false);
  2290. } else if (ieee80211_probe_auth(sdata)) {
  2291. u8 bssid[ETH_ALEN];
  2292. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  2293. ieee80211_destroy_auth_data(sdata, false);
  2294. mutex_unlock(&ifmgd->mtx);
  2295. cfg80211_send_auth_timeout(sdata->dev, bssid);
  2296. mutex_lock(&ifmgd->mtx);
  2297. }
  2298. } else if (ifmgd->auth_data)
  2299. run_again(ifmgd, ifmgd->auth_data->timeout);
  2300. if (ifmgd->assoc_data &&
  2301. time_after(jiffies, ifmgd->assoc_data->timeout)) {
  2302. if (!ifmgd->assoc_data->have_beacon ||
  2303. ieee80211_do_assoc(sdata)) {
  2304. u8 bssid[ETH_ALEN];
  2305. memcpy(bssid, ifmgd->assoc_data->bss->bssid, ETH_ALEN);
  2306. ieee80211_destroy_assoc_data(sdata, false);
  2307. mutex_unlock(&ifmgd->mtx);
  2308. cfg80211_send_assoc_timeout(sdata->dev, bssid);
  2309. mutex_lock(&ifmgd->mtx);
  2310. }
  2311. } else if (ifmgd->assoc_data)
  2312. run_again(ifmgd, ifmgd->assoc_data->timeout);
  2313. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  2314. IEEE80211_STA_CONNECTION_POLL) &&
  2315. ifmgd->associated) {
  2316. u8 bssid[ETH_ALEN];
  2317. int max_tries;
  2318. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  2319. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2320. max_tries = max_nullfunc_tries;
  2321. else
  2322. max_tries = max_probe_tries;
  2323. /* ACK received for nullfunc probing frame */
  2324. if (!ifmgd->probe_send_count)
  2325. ieee80211_reset_ap_probe(sdata);
  2326. else if (ifmgd->nullfunc_failed) {
  2327. if (ifmgd->probe_send_count < max_tries) {
  2328. mlme_dbg(sdata,
  2329. "No ack for nullfunc frame to AP %pM, try %d/%i\n",
  2330. bssid, ifmgd->probe_send_count,
  2331. max_tries);
  2332. ieee80211_mgd_probe_ap_send(sdata);
  2333. } else {
  2334. mlme_dbg(sdata,
  2335. "No ack for nullfunc frame to AP %pM, disconnecting.\n",
  2336. bssid);
  2337. ieee80211_sta_connection_lost(sdata, bssid,
  2338. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2339. }
  2340. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  2341. run_again(ifmgd, ifmgd->probe_timeout);
  2342. else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  2343. mlme_dbg(sdata,
  2344. "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
  2345. bssid, probe_wait_ms);
  2346. ieee80211_sta_connection_lost(sdata, bssid,
  2347. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2348. } else if (ifmgd->probe_send_count < max_tries) {
  2349. mlme_dbg(sdata,
  2350. "No probe response from AP %pM after %dms, try %d/%i\n",
  2351. bssid, probe_wait_ms,
  2352. ifmgd->probe_send_count, max_tries);
  2353. ieee80211_mgd_probe_ap_send(sdata);
  2354. } else {
  2355. /*
  2356. * We actually lost the connection ... or did we?
  2357. * Let's make sure!
  2358. */
  2359. wiphy_debug(local->hw.wiphy,
  2360. "%s: No probe response from AP %pM"
  2361. " after %dms, disconnecting.\n",
  2362. sdata->name,
  2363. bssid, probe_wait_ms);
  2364. ieee80211_sta_connection_lost(sdata, bssid,
  2365. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
  2366. }
  2367. }
  2368. mutex_unlock(&ifmgd->mtx);
  2369. mutex_lock(&local->mtx);
  2370. ieee80211_recalc_idle(local);
  2371. mutex_unlock(&local->mtx);
  2372. }
  2373. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  2374. {
  2375. struct ieee80211_sub_if_data *sdata =
  2376. (struct ieee80211_sub_if_data *) data;
  2377. struct ieee80211_local *local = sdata->local;
  2378. if (local->quiescing)
  2379. return;
  2380. ieee80211_queue_work(&sdata->local->hw,
  2381. &sdata->u.mgd.beacon_connection_loss_work);
  2382. }
  2383. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  2384. {
  2385. struct ieee80211_sub_if_data *sdata =
  2386. (struct ieee80211_sub_if_data *) data;
  2387. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2388. struct ieee80211_local *local = sdata->local;
  2389. if (local->quiescing)
  2390. return;
  2391. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  2392. }
  2393. static void ieee80211_sta_monitor_work(struct work_struct *work)
  2394. {
  2395. struct ieee80211_sub_if_data *sdata =
  2396. container_of(work, struct ieee80211_sub_if_data,
  2397. u.mgd.monitor_work);
  2398. ieee80211_mgd_probe_ap(sdata, false);
  2399. }
  2400. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  2401. {
  2402. u32 flags;
  2403. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  2404. __ieee80211_stop_poll(sdata);
  2405. /* let's probe the connection once */
  2406. flags = sdata->local->hw.flags;
  2407. if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
  2408. ieee80211_queue_work(&sdata->local->hw,
  2409. &sdata->u.mgd.monitor_work);
  2410. /* and do all the other regular work too */
  2411. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  2412. }
  2413. }
  2414. #ifdef CONFIG_PM
  2415. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  2416. {
  2417. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2418. /*
  2419. * we need to use atomic bitops for the running bits
  2420. * only because both timers might fire at the same
  2421. * time -- the code here is properly synchronised.
  2422. */
  2423. cancel_work_sync(&ifmgd->request_smps_work);
  2424. cancel_work_sync(&ifmgd->monitor_work);
  2425. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  2426. cancel_work_sync(&ifmgd->csa_connection_drop_work);
  2427. if (del_timer_sync(&ifmgd->timer))
  2428. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  2429. cancel_work_sync(&ifmgd->chswitch_work);
  2430. if (del_timer_sync(&ifmgd->chswitch_timer))
  2431. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  2432. /* these will just be re-established on connection */
  2433. del_timer_sync(&ifmgd->conn_mon_timer);
  2434. del_timer_sync(&ifmgd->bcn_mon_timer);
  2435. }
  2436. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  2437. {
  2438. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2439. if (!ifmgd->associated)
  2440. return;
  2441. if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
  2442. sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
  2443. mutex_lock(&ifmgd->mtx);
  2444. if (ifmgd->associated) {
  2445. mlme_dbg(sdata,
  2446. "driver requested disconnect after resume\n");
  2447. ieee80211_sta_connection_lost(sdata,
  2448. ifmgd->associated->bssid,
  2449. WLAN_REASON_UNSPECIFIED);
  2450. mutex_unlock(&ifmgd->mtx);
  2451. return;
  2452. }
  2453. mutex_unlock(&ifmgd->mtx);
  2454. }
  2455. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  2456. add_timer(&ifmgd->timer);
  2457. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  2458. add_timer(&ifmgd->chswitch_timer);
  2459. ieee80211_sta_reset_beacon_monitor(sdata);
  2460. mutex_lock(&sdata->local->mtx);
  2461. ieee80211_restart_sta_timer(sdata);
  2462. mutex_unlock(&sdata->local->mtx);
  2463. }
  2464. #endif
  2465. /* interface setup */
  2466. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  2467. {
  2468. struct ieee80211_if_managed *ifmgd;
  2469. ifmgd = &sdata->u.mgd;
  2470. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  2471. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  2472. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  2473. ieee80211_beacon_connection_loss_work);
  2474. INIT_WORK(&ifmgd->csa_connection_drop_work,
  2475. ieee80211_csa_connection_drop_work);
  2476. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
  2477. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  2478. (unsigned long) sdata);
  2479. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  2480. (unsigned long) sdata);
  2481. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  2482. (unsigned long) sdata);
  2483. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  2484. (unsigned long) sdata);
  2485. ifmgd->flags = 0;
  2486. ifmgd->powersave = sdata->wdev.ps;
  2487. ifmgd->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
  2488. ifmgd->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
  2489. mutex_init(&ifmgd->mtx);
  2490. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  2491. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  2492. else
  2493. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  2494. }
  2495. /* scan finished notification */
  2496. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  2497. {
  2498. struct ieee80211_sub_if_data *sdata;
  2499. /* Restart STA timers */
  2500. rcu_read_lock();
  2501. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  2502. ieee80211_restart_sta_timer(sdata);
  2503. rcu_read_unlock();
  2504. }
  2505. int ieee80211_max_network_latency(struct notifier_block *nb,
  2506. unsigned long data, void *dummy)
  2507. {
  2508. s32 latency_usec = (s32) data;
  2509. struct ieee80211_local *local =
  2510. container_of(nb, struct ieee80211_local,
  2511. network_latency_notifier);
  2512. mutex_lock(&local->iflist_mtx);
  2513. ieee80211_recalc_ps(local, latency_usec);
  2514. mutex_unlock(&local->iflist_mtx);
  2515. return 0;
  2516. }
  2517. static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
  2518. struct cfg80211_bss *cbss)
  2519. {
  2520. struct ieee80211_local *local = sdata->local;
  2521. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2522. int ht_cfreq;
  2523. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  2524. const u8 *ht_oper_ie;
  2525. const struct ieee80211_ht_operation *ht_oper = NULL;
  2526. struct ieee80211_supported_band *sband;
  2527. sband = local->hw.wiphy->bands[cbss->channel->band];
  2528. ifmgd->flags &= ~IEEE80211_STA_DISABLE_40MHZ;
  2529. if (sband->ht_cap.ht_supported) {
  2530. ht_oper_ie = cfg80211_find_ie(WLAN_EID_HT_OPERATION,
  2531. cbss->information_elements,
  2532. cbss->len_information_elements);
  2533. if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
  2534. ht_oper = (void *)(ht_oper_ie + 2);
  2535. }
  2536. if (ht_oper) {
  2537. ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
  2538. cbss->channel->band);
  2539. /* check that channel matches the right operating channel */
  2540. if (cbss->channel->center_freq != ht_cfreq) {
  2541. /*
  2542. * It's possible that some APs are confused here;
  2543. * Netgear WNDR3700 sometimes reports 4 higher than
  2544. * the actual channel in association responses, but
  2545. * since we look at probe response/beacon data here
  2546. * it should be OK.
  2547. */
  2548. sdata_info(sdata,
  2549. "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
  2550. cbss->channel->center_freq,
  2551. ht_cfreq, ht_oper->primary_chan,
  2552. cbss->channel->band);
  2553. ht_oper = NULL;
  2554. }
  2555. }
  2556. if (ht_oper) {
  2557. channel_type = NL80211_CHAN_HT20;
  2558. if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  2559. switch (ht_oper->ht_param &
  2560. IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  2561. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  2562. channel_type = NL80211_CHAN_HT40PLUS;
  2563. break;
  2564. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  2565. channel_type = NL80211_CHAN_HT40MINUS;
  2566. break;
  2567. }
  2568. }
  2569. }
  2570. if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
  2571. /* can only fail due to HT40+/- mismatch */
  2572. channel_type = NL80211_CHAN_HT20;
  2573. sdata_info(sdata,
  2574. "disabling 40 MHz due to multi-vif mismatch\n");
  2575. ifmgd->flags |= IEEE80211_STA_DISABLE_40MHZ;
  2576. WARN_ON(!ieee80211_set_channel_type(local, sdata,
  2577. channel_type));
  2578. }
  2579. local->oper_channel = cbss->channel;
  2580. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  2581. return 0;
  2582. }
  2583. static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
  2584. struct cfg80211_bss *cbss, bool assoc)
  2585. {
  2586. struct ieee80211_local *local = sdata->local;
  2587. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2588. struct ieee80211_bss *bss = (void *)cbss->priv;
  2589. struct sta_info *new_sta = NULL;
  2590. bool have_sta = false;
  2591. int err;
  2592. if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
  2593. return -EINVAL;
  2594. if (assoc) {
  2595. rcu_read_lock();
  2596. have_sta = sta_info_get(sdata, cbss->bssid);
  2597. rcu_read_unlock();
  2598. }
  2599. if (!have_sta) {
  2600. new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  2601. if (!new_sta)
  2602. return -ENOMEM;
  2603. }
  2604. mutex_lock(&local->mtx);
  2605. ieee80211_recalc_idle(sdata->local);
  2606. mutex_unlock(&local->mtx);
  2607. if (new_sta) {
  2608. u32 rates = 0, basic_rates = 0;
  2609. bool have_higher_than_11mbit;
  2610. int min_rate = INT_MAX, min_rate_index = -1;
  2611. struct ieee80211_supported_band *sband;
  2612. sband = local->hw.wiphy->bands[cbss->channel->band];
  2613. err = ieee80211_prep_channel(sdata, cbss);
  2614. if (err) {
  2615. sta_info_free(local, new_sta);
  2616. return err;
  2617. }
  2618. ieee80211_get_rates(sband, bss->supp_rates,
  2619. bss->supp_rates_len,
  2620. &rates, &basic_rates,
  2621. &have_higher_than_11mbit,
  2622. &min_rate, &min_rate_index);
  2623. /*
  2624. * This used to be a workaround for basic rates missing
  2625. * in the association response frame. Now that we no
  2626. * longer use the basic rates from there, it probably
  2627. * doesn't happen any more, but keep the workaround so
  2628. * in case some *other* APs are buggy in different ways
  2629. * we can connect -- with a warning.
  2630. */
  2631. if (!basic_rates && min_rate_index >= 0) {
  2632. sdata_info(sdata,
  2633. "No basic rates, using min rate instead\n");
  2634. basic_rates = BIT(min_rate_index);
  2635. }
  2636. new_sta->sta.supp_rates[cbss->channel->band] = rates;
  2637. sdata->vif.bss_conf.basic_rates = basic_rates;
  2638. /* cf. IEEE 802.11 9.2.12 */
  2639. if (local->oper_channel->band == IEEE80211_BAND_2GHZ &&
  2640. have_higher_than_11mbit)
  2641. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  2642. else
  2643. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  2644. memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
  2645. /* set timing information */
  2646. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  2647. sdata->vif.bss_conf.sync_tsf = cbss->tsf;
  2648. sdata->vif.bss_conf.sync_device_ts = bss->device_ts;
  2649. /* tell driver about BSSID, basic rates and timing */
  2650. ieee80211_bss_info_change_notify(sdata,
  2651. BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
  2652. BSS_CHANGED_BEACON_INT);
  2653. if (assoc)
  2654. sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
  2655. err = sta_info_insert(new_sta);
  2656. new_sta = NULL;
  2657. if (err) {
  2658. sdata_info(sdata,
  2659. "failed to insert STA entry for the AP (error %d)\n",
  2660. err);
  2661. return err;
  2662. }
  2663. } else
  2664. WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
  2665. return 0;
  2666. }
  2667. /* config hooks */
  2668. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  2669. struct cfg80211_auth_request *req)
  2670. {
  2671. struct ieee80211_local *local = sdata->local;
  2672. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2673. struct ieee80211_mgd_auth_data *auth_data;
  2674. u16 auth_alg;
  2675. int err;
  2676. /* prepare auth data structure */
  2677. switch (req->auth_type) {
  2678. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  2679. auth_alg = WLAN_AUTH_OPEN;
  2680. break;
  2681. case NL80211_AUTHTYPE_SHARED_KEY:
  2682. if (IS_ERR(local->wep_tx_tfm))
  2683. return -EOPNOTSUPP;
  2684. auth_alg = WLAN_AUTH_SHARED_KEY;
  2685. break;
  2686. case NL80211_AUTHTYPE_FT:
  2687. auth_alg = WLAN_AUTH_FT;
  2688. break;
  2689. case NL80211_AUTHTYPE_NETWORK_EAP:
  2690. auth_alg = WLAN_AUTH_LEAP;
  2691. break;
  2692. default:
  2693. return -EOPNOTSUPP;
  2694. }
  2695. auth_data = kzalloc(sizeof(*auth_data) + req->ie_len, GFP_KERNEL);
  2696. if (!auth_data)
  2697. return -ENOMEM;
  2698. auth_data->bss = req->bss;
  2699. if (req->ie && req->ie_len) {
  2700. memcpy(auth_data->ie, req->ie, req->ie_len);
  2701. auth_data->ie_len = req->ie_len;
  2702. }
  2703. if (req->key && req->key_len) {
  2704. auth_data->key_len = req->key_len;
  2705. auth_data->key_idx = req->key_idx;
  2706. memcpy(auth_data->key, req->key, req->key_len);
  2707. }
  2708. auth_data->algorithm = auth_alg;
  2709. /* try to authenticate/probe */
  2710. mutex_lock(&ifmgd->mtx);
  2711. if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
  2712. ifmgd->assoc_data) {
  2713. err = -EBUSY;
  2714. goto err_free;
  2715. }
  2716. if (ifmgd->auth_data)
  2717. ieee80211_destroy_auth_data(sdata, false);
  2718. /* prep auth_data so we don't go into idle on disassoc */
  2719. ifmgd->auth_data = auth_data;
  2720. if (ifmgd->associated)
  2721. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2722. sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
  2723. err = ieee80211_prep_connection(sdata, req->bss, false);
  2724. if (err)
  2725. goto err_clear;
  2726. err = ieee80211_probe_auth(sdata);
  2727. if (err) {
  2728. sta_info_destroy_addr(sdata, req->bss->bssid);
  2729. goto err_clear;
  2730. }
  2731. /* hold our own reference */
  2732. cfg80211_ref_bss(auth_data->bss);
  2733. err = 0;
  2734. goto out_unlock;
  2735. err_clear:
  2736. memset(ifmgd->bssid, 0, ETH_ALEN);
  2737. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2738. ifmgd->auth_data = NULL;
  2739. err_free:
  2740. kfree(auth_data);
  2741. out_unlock:
  2742. mutex_unlock(&ifmgd->mtx);
  2743. return err;
  2744. }
  2745. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  2746. struct cfg80211_assoc_request *req)
  2747. {
  2748. struct ieee80211_local *local = sdata->local;
  2749. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2750. struct ieee80211_bss *bss = (void *)req->bss->priv;
  2751. struct ieee80211_mgd_assoc_data *assoc_data;
  2752. struct ieee80211_supported_band *sband;
  2753. const u8 *ssidie, *ht_ie;
  2754. int i, err;
  2755. ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  2756. if (!ssidie)
  2757. return -EINVAL;
  2758. assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
  2759. if (!assoc_data)
  2760. return -ENOMEM;
  2761. mutex_lock(&ifmgd->mtx);
  2762. if (ifmgd->associated)
  2763. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  2764. if (ifmgd->auth_data && !ifmgd->auth_data->done) {
  2765. err = -EBUSY;
  2766. goto err_free;
  2767. }
  2768. if (ifmgd->assoc_data) {
  2769. err = -EBUSY;
  2770. goto err_free;
  2771. }
  2772. if (ifmgd->auth_data) {
  2773. bool match;
  2774. /* keep sta info, bssid if matching */
  2775. match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
  2776. ieee80211_destroy_auth_data(sdata, match);
  2777. }
  2778. /* prepare assoc data */
  2779. /*
  2780. * keep only the 40 MHz disable bit set as it might have
  2781. * been set during authentication already, all other bits
  2782. * should be reset for a new connection
  2783. */
  2784. ifmgd->flags &= IEEE80211_STA_DISABLE_40MHZ;
  2785. ifmgd->beacon_crc_valid = false;
  2786. /*
  2787. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  2788. * We still associate in non-HT mode (11a/b/g) if any one of these
  2789. * ciphers is configured as pairwise.
  2790. * We can set this to true for non-11n hardware, that'll be checked
  2791. * separately along with the peer capabilities.
  2792. */
  2793. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
  2794. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  2795. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  2796. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
  2797. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2798. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  2799. netdev_info(sdata->dev,
  2800. "disabling HT/VHT due to WEP/TKIP use\n");
  2801. }
  2802. }
  2803. if (req->flags & ASSOC_REQ_DISABLE_HT) {
  2804. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2805. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  2806. }
  2807. /* Also disable HT if we don't support it or the AP doesn't use WMM */
  2808. sband = local->hw.wiphy->bands[req->bss->channel->band];
  2809. if (!sband->ht_cap.ht_supported ||
  2810. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
  2811. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2812. if (!bss->wmm_used)
  2813. netdev_info(sdata->dev,
  2814. "disabling HT as WMM/QoS is not supported by the AP\n");
  2815. }
  2816. /* disable VHT if we don't support it or the AP doesn't use WMM */
  2817. if (!sband->vht_cap.vht_supported ||
  2818. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) {
  2819. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  2820. if (!bss->wmm_used)
  2821. netdev_info(sdata->dev,
  2822. "disabling VHT as WMM/QoS is not supported by the AP\n");
  2823. }
  2824. memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
  2825. memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
  2826. sizeof(ifmgd->ht_capa_mask));
  2827. if (req->ie && req->ie_len) {
  2828. memcpy(assoc_data->ie, req->ie, req->ie_len);
  2829. assoc_data->ie_len = req->ie_len;
  2830. }
  2831. assoc_data->bss = req->bss;
  2832. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  2833. if (ifmgd->powersave)
  2834. ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
  2835. else
  2836. ifmgd->ap_smps = IEEE80211_SMPS_OFF;
  2837. } else
  2838. ifmgd->ap_smps = ifmgd->req_smps;
  2839. assoc_data->capability = req->bss->capability;
  2840. assoc_data->wmm = bss->wmm_used &&
  2841. (local->hw.queues >= IEEE80211_NUM_ACS);
  2842. assoc_data->supp_rates = bss->supp_rates;
  2843. assoc_data->supp_rates_len = bss->supp_rates_len;
  2844. ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
  2845. if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
  2846. assoc_data->ap_ht_param =
  2847. ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
  2848. else
  2849. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  2850. if (bss->wmm_used && bss->uapsd_supported &&
  2851. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
  2852. assoc_data->uapsd = true;
  2853. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  2854. } else {
  2855. assoc_data->uapsd = false;
  2856. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  2857. }
  2858. memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
  2859. assoc_data->ssid_len = ssidie[1];
  2860. if (req->prev_bssid)
  2861. memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
  2862. if (req->use_mfp) {
  2863. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  2864. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  2865. } else {
  2866. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  2867. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  2868. }
  2869. if (req->crypto.control_port)
  2870. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  2871. else
  2872. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  2873. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  2874. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  2875. /* kick off associate process */
  2876. ifmgd->assoc_data = assoc_data;
  2877. err = ieee80211_prep_connection(sdata, req->bss, true);
  2878. if (err)
  2879. goto err_clear;
  2880. if (!bss->dtim_period &&
  2881. sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD) {
  2882. /*
  2883. * Wait up to one beacon interval ...
  2884. * should this be more if we miss one?
  2885. */
  2886. sdata_info(sdata, "waiting for beacon from %pM\n",
  2887. ifmgd->bssid);
  2888. assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
  2889. } else {
  2890. assoc_data->have_beacon = true;
  2891. assoc_data->sent_assoc = false;
  2892. assoc_data->timeout = jiffies;
  2893. }
  2894. run_again(ifmgd, assoc_data->timeout);
  2895. if (bss->corrupt_data) {
  2896. char *corrupt_type = "data";
  2897. if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
  2898. if (bss->corrupt_data &
  2899. IEEE80211_BSS_CORRUPT_PROBE_RESP)
  2900. corrupt_type = "beacon and probe response";
  2901. else
  2902. corrupt_type = "beacon";
  2903. } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
  2904. corrupt_type = "probe response";
  2905. sdata_info(sdata, "associating with AP with corrupt %s\n",
  2906. corrupt_type);
  2907. }
  2908. err = 0;
  2909. goto out;
  2910. err_clear:
  2911. memset(ifmgd->bssid, 0, ETH_ALEN);
  2912. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  2913. ifmgd->assoc_data = NULL;
  2914. err_free:
  2915. kfree(assoc_data);
  2916. out:
  2917. mutex_unlock(&ifmgd->mtx);
  2918. return err;
  2919. }
  2920. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  2921. struct cfg80211_deauth_request *req)
  2922. {
  2923. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2924. u8 frame_buf[DEAUTH_DISASSOC_LEN];
  2925. mutex_lock(&ifmgd->mtx);
  2926. if (ifmgd->auth_data) {
  2927. ieee80211_destroy_auth_data(sdata, false);
  2928. mutex_unlock(&ifmgd->mtx);
  2929. return 0;
  2930. }
  2931. sdata_info(sdata,
  2932. "deauthenticating from %pM by local choice (reason=%d)\n",
  2933. req->bssid, req->reason_code);
  2934. if (ifmgd->associated &&
  2935. ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
  2936. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  2937. req->reason_code, true, frame_buf);
  2938. } else {
  2939. drv_mgd_prepare_tx(sdata->local, sdata);
  2940. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  2941. IEEE80211_STYPE_DEAUTH,
  2942. req->reason_code, true,
  2943. frame_buf);
  2944. }
  2945. mutex_unlock(&ifmgd->mtx);
  2946. __cfg80211_send_deauth(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
  2947. mutex_lock(&sdata->local->mtx);
  2948. ieee80211_recalc_idle(sdata->local);
  2949. mutex_unlock(&sdata->local->mtx);
  2950. return 0;
  2951. }
  2952. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  2953. struct cfg80211_disassoc_request *req)
  2954. {
  2955. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2956. u8 bssid[ETH_ALEN];
  2957. u8 frame_buf[DEAUTH_DISASSOC_LEN];
  2958. mutex_lock(&ifmgd->mtx);
  2959. /*
  2960. * cfg80211 should catch this ... but it's racy since
  2961. * we can receive a disassoc frame, process it, hand it
  2962. * to cfg80211 while that's in a locked section already
  2963. * trying to tell us that the user wants to disconnect.
  2964. */
  2965. if (ifmgd->associated != req->bss) {
  2966. mutex_unlock(&ifmgd->mtx);
  2967. return -ENOLINK;
  2968. }
  2969. sdata_info(sdata,
  2970. "disassociating from %pM by local choice (reason=%d)\n",
  2971. req->bss->bssid, req->reason_code);
  2972. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  2973. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
  2974. req->reason_code, !req->local_state_change,
  2975. frame_buf);
  2976. mutex_unlock(&ifmgd->mtx);
  2977. __cfg80211_send_disassoc(sdata->dev, frame_buf, DEAUTH_DISASSOC_LEN);
  2978. mutex_lock(&sdata->local->mtx);
  2979. ieee80211_recalc_idle(sdata->local);
  2980. mutex_unlock(&sdata->local->mtx);
  2981. return 0;
  2982. }
  2983. void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
  2984. {
  2985. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2986. mutex_lock(&ifmgd->mtx);
  2987. if (ifmgd->assoc_data)
  2988. ieee80211_destroy_assoc_data(sdata, false);
  2989. if (ifmgd->auth_data)
  2990. ieee80211_destroy_auth_data(sdata, false);
  2991. del_timer_sync(&ifmgd->timer);
  2992. mutex_unlock(&ifmgd->mtx);
  2993. }
  2994. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  2995. enum nl80211_cqm_rssi_threshold_event rssi_event,
  2996. gfp_t gfp)
  2997. {
  2998. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2999. trace_api_cqm_rssi_notify(sdata, rssi_event);
  3000. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  3001. }
  3002. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);