mlme.c 115 KB

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