mlme.c 103 KB

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