mlme.c 114 KB

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