mlme.c 157 KB

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