mlme.c 120 KB

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