mlme.c 155 KB

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