mlme.c 132 KB

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