mlme.c 120 KB

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