mlme.c 119 KB

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