ctrl_iface.c 239 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #ifdef CONFIG_TESTING_OPTIONS
  10. #include <net/ethernet.h>
  11. #include <netinet/ip.h>
  12. #endif /* CONFIG_TESTING_OPTIONS */
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "utils/uuid.h"
  16. #include "utils/module_tests.h"
  17. #include "common/version.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/ieee802_11_common.h"
  20. #include "common/wpa_ctrl.h"
  21. #include "crypto/tls.h"
  22. #include "ap/hostapd.h"
  23. #include "eap_peer/eap.h"
  24. #include "eapol_supp/eapol_supp_sm.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "rsn_supp/preauth.h"
  27. #include "rsn_supp/pmksa_cache.h"
  28. #include "l2_packet/l2_packet.h"
  29. #include "wps/wps.h"
  30. #include "fst/fst.h"
  31. #include "fst/fst_ctrl_iface.h"
  32. #include "config.h"
  33. #include "wpa_supplicant_i.h"
  34. #include "driver_i.h"
  35. #include "wps_supplicant.h"
  36. #include "ibss_rsn.h"
  37. #include "ap.h"
  38. #include "p2p_supplicant.h"
  39. #include "p2p/p2p.h"
  40. #include "hs20_supplicant.h"
  41. #include "wifi_display.h"
  42. #include "notify.h"
  43. #include "bss.h"
  44. #include "scan.h"
  45. #include "ctrl_iface.h"
  46. #include "interworking.h"
  47. #include "blacklist.h"
  48. #include "autoscan.h"
  49. #include "wnm_sta.h"
  50. #include "offchannel.h"
  51. #include "drivers/driver.h"
  52. #include "mesh.h"
  53. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  54. char *buf, int len);
  55. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  56. const char *input,
  57. char *buf, int len);
  58. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
  59. char *val);
  60. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  61. {
  62. char *pos;
  63. u8 addr[ETH_ALEN], *filter = NULL, *n;
  64. size_t count = 0;
  65. pos = val;
  66. while (pos) {
  67. if (*pos == '\0')
  68. break;
  69. if (hwaddr_aton(pos, addr)) {
  70. os_free(filter);
  71. return -1;
  72. }
  73. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  74. if (n == NULL) {
  75. os_free(filter);
  76. return -1;
  77. }
  78. filter = n;
  79. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  80. count++;
  81. pos = os_strchr(pos, ' ');
  82. if (pos)
  83. pos++;
  84. }
  85. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  86. os_free(wpa_s->bssid_filter);
  87. wpa_s->bssid_filter = filter;
  88. wpa_s->bssid_filter_count = count;
  89. return 0;
  90. }
  91. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  92. {
  93. char *pos;
  94. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  95. struct wpa_ssid_value *ssid = NULL, *ns;
  96. size_t count = 0, ssid_count = 0;
  97. struct wpa_ssid *c;
  98. /*
  99. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  100. * SSID_SPEC ::= ssid <SSID_HEX>
  101. * BSSID_SPEC ::= bssid <BSSID_HEX>
  102. */
  103. pos = val;
  104. while (pos) {
  105. if (*pos == '\0')
  106. break;
  107. if (os_strncmp(pos, "bssid ", 6) == 0) {
  108. int res;
  109. pos += 6;
  110. res = hwaddr_aton2(pos, addr);
  111. if (res < 0) {
  112. os_free(ssid);
  113. os_free(bssid);
  114. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  115. "BSSID value '%s'", pos);
  116. return -1;
  117. }
  118. pos += res;
  119. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  120. if (n == NULL) {
  121. os_free(ssid);
  122. os_free(bssid);
  123. return -1;
  124. }
  125. bssid = n;
  126. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  127. count++;
  128. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  129. char *end;
  130. pos += 5;
  131. end = pos;
  132. while (*end) {
  133. if (*end == '\0' || *end == ' ')
  134. break;
  135. end++;
  136. }
  137. ns = os_realloc_array(ssid, ssid_count + 1,
  138. sizeof(struct wpa_ssid_value));
  139. if (ns == NULL) {
  140. os_free(ssid);
  141. os_free(bssid);
  142. return -1;
  143. }
  144. ssid = ns;
  145. if ((end - pos) & 0x01 ||
  146. end - pos > 2 * SSID_MAX_LEN ||
  147. hexstr2bin(pos, ssid[ssid_count].ssid,
  148. (end - pos) / 2) < 0) {
  149. os_free(ssid);
  150. os_free(bssid);
  151. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  152. "SSID value '%s'", pos);
  153. return -1;
  154. }
  155. ssid[ssid_count].ssid_len = (end - pos) / 2;
  156. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  157. ssid[ssid_count].ssid,
  158. ssid[ssid_count].ssid_len);
  159. ssid_count++;
  160. pos = end;
  161. } else {
  162. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  163. "'%s'", pos);
  164. os_free(ssid);
  165. os_free(bssid);
  166. return -1;
  167. }
  168. pos = os_strchr(pos, ' ');
  169. if (pos)
  170. pos++;
  171. }
  172. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  173. os_free(wpa_s->disallow_aps_bssid);
  174. wpa_s->disallow_aps_bssid = bssid;
  175. wpa_s->disallow_aps_bssid_count = count;
  176. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  177. os_free(wpa_s->disallow_aps_ssid);
  178. wpa_s->disallow_aps_ssid = ssid;
  179. wpa_s->disallow_aps_ssid_count = ssid_count;
  180. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  181. return 0;
  182. c = wpa_s->current_ssid;
  183. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  184. return 0;
  185. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  186. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  187. return 0;
  188. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  189. "because current AP was marked disallowed");
  190. #ifdef CONFIG_SME
  191. wpa_s->sme.prev_bssid_set = 0;
  192. #endif /* CONFIG_SME */
  193. wpa_s->reassociate = 1;
  194. wpa_s->own_disconnect_req = 1;
  195. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  196. wpa_supplicant_req_scan(wpa_s, 0, 0);
  197. return 0;
  198. }
  199. #ifndef CONFIG_NO_CONFIG_BLOBS
  200. static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
  201. {
  202. char *name = pos;
  203. struct wpa_config_blob *blob;
  204. size_t len;
  205. pos = os_strchr(pos, ' ');
  206. if (pos == NULL)
  207. return -1;
  208. *pos++ = '\0';
  209. len = os_strlen(pos);
  210. if (len & 1)
  211. return -1;
  212. wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
  213. blob = os_zalloc(sizeof(*blob));
  214. if (blob == NULL)
  215. return -1;
  216. blob->name = os_strdup(name);
  217. blob->data = os_malloc(len / 2);
  218. if (blob->name == NULL || blob->data == NULL) {
  219. wpa_config_free_blob(blob);
  220. return -1;
  221. }
  222. if (hexstr2bin(pos, blob->data, len / 2) < 0) {
  223. wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
  224. wpa_config_free_blob(blob);
  225. return -1;
  226. }
  227. blob->len = len / 2;
  228. wpa_config_set_blob(wpa_s->conf, blob);
  229. return 0;
  230. }
  231. #endif /* CONFIG_NO_CONFIG_BLOBS */
  232. static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
  233. {
  234. char *params;
  235. char *pos;
  236. int *freqs = NULL;
  237. int ret;
  238. if (atoi(cmd)) {
  239. params = os_strchr(cmd, ' ');
  240. os_free(wpa_s->manual_sched_scan_freqs);
  241. if (params) {
  242. params++;
  243. pos = os_strstr(params, "freq=");
  244. if (pos)
  245. freqs = freq_range_to_channel_list(wpa_s,
  246. pos + 5);
  247. }
  248. wpa_s->manual_sched_scan_freqs = freqs;
  249. ret = wpas_start_pno(wpa_s);
  250. } else {
  251. ret = wpas_stop_pno(wpa_s);
  252. }
  253. return ret;
  254. }
  255. static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
  256. {
  257. union wpa_event_data event;
  258. if (os_strcmp(band, "AUTO") == 0)
  259. wpa_s->setband = WPA_SETBAND_AUTO;
  260. else if (os_strcmp(band, "5G") == 0)
  261. wpa_s->setband = WPA_SETBAND_5G;
  262. else if (os_strcmp(band, "2G") == 0)
  263. wpa_s->setband = WPA_SETBAND_2G;
  264. else
  265. return -1;
  266. if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
  267. os_memset(&event, 0, sizeof(event));
  268. event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
  269. event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
  270. wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
  271. }
  272. return 0;
  273. }
  274. static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
  275. const char *cmd)
  276. {
  277. struct wpabuf *lci;
  278. if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
  279. wpabuf_free(wpa_s->lci);
  280. wpa_s->lci = NULL;
  281. return 0;
  282. }
  283. lci = wpabuf_parse_bin(cmd);
  284. if (!lci)
  285. return -1;
  286. if (os_get_reltime(&wpa_s->lci_time)) {
  287. wpabuf_free(lci);
  288. return -1;
  289. }
  290. wpabuf_free(wpa_s->lci);
  291. wpa_s->lci = lci;
  292. return 0;
  293. }
  294. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  295. char *cmd)
  296. {
  297. char *value;
  298. int ret = 0;
  299. value = os_strchr(cmd, ' ');
  300. if (value == NULL)
  301. return -1;
  302. *value++ = '\0';
  303. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  304. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  305. eapol_sm_configure(wpa_s->eapol,
  306. atoi(value), -1, -1, -1);
  307. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  308. eapol_sm_configure(wpa_s->eapol,
  309. -1, atoi(value), -1, -1);
  310. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  311. eapol_sm_configure(wpa_s->eapol,
  312. -1, -1, atoi(value), -1);
  313. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  314. eapol_sm_configure(wpa_s->eapol,
  315. -1, -1, -1, atoi(value));
  316. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  317. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  318. atoi(value)))
  319. ret = -1;
  320. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  321. 0) {
  322. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  323. atoi(value)))
  324. ret = -1;
  325. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  326. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  327. ret = -1;
  328. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  329. wpa_s->wps_fragment_size = atoi(value);
  330. #ifdef CONFIG_WPS_TESTING
  331. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  332. long int val;
  333. val = strtol(value, NULL, 0);
  334. if (val < 0 || val > 0xff) {
  335. ret = -1;
  336. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  337. "wps_version_number %ld", val);
  338. } else {
  339. wps_version_number = val;
  340. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  341. "version %u.%u",
  342. (wps_version_number & 0xf0) >> 4,
  343. wps_version_number & 0x0f);
  344. }
  345. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  346. wps_testing_dummy_cred = atoi(value);
  347. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  348. wps_testing_dummy_cred);
  349. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  350. wps_corrupt_pkhash = atoi(value);
  351. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  352. wps_corrupt_pkhash);
  353. } else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
  354. if (value[0] == '\0') {
  355. wps_force_auth_types_in_use = 0;
  356. } else {
  357. wps_force_auth_types = strtol(value, NULL, 0);
  358. wps_force_auth_types_in_use = 1;
  359. }
  360. } else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
  361. if (value[0] == '\0') {
  362. wps_force_encr_types_in_use = 0;
  363. } else {
  364. wps_force_encr_types = strtol(value, NULL, 0);
  365. wps_force_encr_types_in_use = 1;
  366. }
  367. #endif /* CONFIG_WPS_TESTING */
  368. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  369. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  370. ret = -1;
  371. #ifdef CONFIG_TDLS
  372. #ifdef CONFIG_TDLS_TESTING
  373. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  374. tdls_testing = strtol(value, NULL, 0);
  375. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  376. #endif /* CONFIG_TDLS_TESTING */
  377. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  378. int disabled = atoi(value);
  379. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  380. if (disabled) {
  381. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  382. ret = -1;
  383. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  384. ret = -1;
  385. wpa_tdls_enable(wpa_s->wpa, !disabled);
  386. #endif /* CONFIG_TDLS */
  387. } else if (os_strcasecmp(cmd, "pno") == 0) {
  388. ret = wpas_ctrl_pno(wpa_s, value);
  389. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  390. int disabled = atoi(value);
  391. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  392. ret = -1;
  393. else if (disabled)
  394. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  395. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  396. if (os_strcmp(value, "disable") == 0)
  397. wpa_s->set_sta_uapsd = 0;
  398. else {
  399. int be, bk, vi, vo;
  400. char *pos;
  401. /* format: BE,BK,VI,VO;max SP Length */
  402. be = atoi(value);
  403. pos = os_strchr(value, ',');
  404. if (pos == NULL)
  405. return -1;
  406. pos++;
  407. bk = atoi(pos);
  408. pos = os_strchr(pos, ',');
  409. if (pos == NULL)
  410. return -1;
  411. pos++;
  412. vi = atoi(pos);
  413. pos = os_strchr(pos, ',');
  414. if (pos == NULL)
  415. return -1;
  416. pos++;
  417. vo = atoi(pos);
  418. /* ignore max SP Length for now */
  419. wpa_s->set_sta_uapsd = 1;
  420. wpa_s->sta_uapsd = 0;
  421. if (be)
  422. wpa_s->sta_uapsd |= BIT(0);
  423. if (bk)
  424. wpa_s->sta_uapsd |= BIT(1);
  425. if (vi)
  426. wpa_s->sta_uapsd |= BIT(2);
  427. if (vo)
  428. wpa_s->sta_uapsd |= BIT(3);
  429. }
  430. } else if (os_strcasecmp(cmd, "ps") == 0) {
  431. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  432. #ifdef CONFIG_WIFI_DISPLAY
  433. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  434. int enabled = !!atoi(value);
  435. if (enabled && !wpa_s->global->p2p)
  436. ret = -1;
  437. else
  438. wifi_display_enable(wpa_s->global, enabled);
  439. #endif /* CONFIG_WIFI_DISPLAY */
  440. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  441. ret = set_bssid_filter(wpa_s, value);
  442. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  443. ret = set_disallow_aps(wpa_s, value);
  444. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  445. wpa_s->no_keep_alive = !!atoi(value);
  446. #ifdef CONFIG_TESTING_OPTIONS
  447. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  448. wpa_s->ext_mgmt_frame_handling = !!atoi(value);
  449. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  450. wpa_s->ext_eapol_frame_io = !!atoi(value);
  451. #ifdef CONFIG_AP
  452. if (wpa_s->ap_iface) {
  453. wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
  454. wpa_s->ext_eapol_frame_io;
  455. }
  456. #endif /* CONFIG_AP */
  457. } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
  458. wpa_s->extra_roc_dur = atoi(value);
  459. } else if (os_strcasecmp(cmd, "test_failure") == 0) {
  460. wpa_s->test_failure = atoi(value);
  461. } else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
  462. wpa_s->p2p_go_csa_on_inv = !!atoi(value);
  463. } else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
  464. wpa_s->ignore_auth_resp = !!atoi(value);
  465. } else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
  466. wpa_s->ignore_assoc_disallow = !!atoi(value);
  467. #endif /* CONFIG_TESTING_OPTIONS */
  468. #ifndef CONFIG_NO_CONFIG_BLOBS
  469. } else if (os_strcmp(cmd, "blob") == 0) {
  470. ret = wpas_ctrl_set_blob(wpa_s, value);
  471. #endif /* CONFIG_NO_CONFIG_BLOBS */
  472. } else if (os_strcasecmp(cmd, "setband") == 0) {
  473. ret = wpas_ctrl_set_band(wpa_s, value);
  474. #ifdef CONFIG_MBO
  475. } else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
  476. ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
  477. } else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
  478. wpas_mbo_update_cell_capa(wpa_s, atoi(value));
  479. #endif /* CONFIG_MBO */
  480. } else if (os_strcasecmp(cmd, "lci") == 0) {
  481. ret = wpas_ctrl_iface_set_lci(wpa_s, value);
  482. } else {
  483. value[-1] = '=';
  484. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  485. if (ret == 0)
  486. wpa_supplicant_update_config(wpa_s);
  487. }
  488. return ret;
  489. }
  490. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  491. char *cmd, char *buf, size_t buflen)
  492. {
  493. int res = -1;
  494. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  495. if (os_strcmp(cmd, "version") == 0) {
  496. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  497. } else if (os_strcasecmp(cmd, "country") == 0) {
  498. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  499. res = os_snprintf(buf, buflen, "%c%c",
  500. wpa_s->conf->country[0],
  501. wpa_s->conf->country[1]);
  502. #ifdef CONFIG_WIFI_DISPLAY
  503. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  504. int enabled;
  505. if (wpa_s->global->p2p == NULL ||
  506. wpa_s->global->p2p_disabled)
  507. enabled = 0;
  508. else
  509. enabled = wpa_s->global->wifi_display;
  510. res = os_snprintf(buf, buflen, "%d", enabled);
  511. #endif /* CONFIG_WIFI_DISPLAY */
  512. #ifdef CONFIG_TESTING_GET_GTK
  513. } else if (os_strcmp(cmd, "gtk") == 0) {
  514. if (wpa_s->last_gtk_len == 0)
  515. return -1;
  516. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  517. wpa_s->last_gtk_len);
  518. return res;
  519. #endif /* CONFIG_TESTING_GET_GTK */
  520. } else if (os_strcmp(cmd, "tls_library") == 0) {
  521. res = tls_get_library_version(buf, buflen);
  522. } else {
  523. res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
  524. }
  525. if (os_snprintf_error(buflen, res))
  526. return -1;
  527. return res;
  528. }
  529. #ifdef IEEE8021X_EAPOL
  530. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  531. char *addr)
  532. {
  533. u8 bssid[ETH_ALEN];
  534. struct wpa_ssid *ssid = wpa_s->current_ssid;
  535. if (hwaddr_aton(addr, bssid)) {
  536. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  537. "'%s'", addr);
  538. return -1;
  539. }
  540. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  541. rsn_preauth_deinit(wpa_s->wpa);
  542. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  543. return -1;
  544. return 0;
  545. }
  546. #endif /* IEEE8021X_EAPOL */
  547. #ifdef CONFIG_PEERKEY
  548. /* MLME-STKSTART.request(peer) */
  549. static int wpa_supplicant_ctrl_iface_stkstart(
  550. struct wpa_supplicant *wpa_s, char *addr)
  551. {
  552. u8 peer[ETH_ALEN];
  553. if (hwaddr_aton(addr, peer)) {
  554. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  555. "address '%s'", addr);
  556. return -1;
  557. }
  558. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  559. MAC2STR(peer));
  560. return wpa_sm_stkstart(wpa_s->wpa, peer);
  561. }
  562. #endif /* CONFIG_PEERKEY */
  563. #ifdef CONFIG_TDLS
  564. static int wpa_supplicant_ctrl_iface_tdls_discover(
  565. struct wpa_supplicant *wpa_s, char *addr)
  566. {
  567. u8 peer[ETH_ALEN];
  568. int ret;
  569. if (hwaddr_aton(addr, peer)) {
  570. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  571. "address '%s'", addr);
  572. return -1;
  573. }
  574. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  575. MAC2STR(peer));
  576. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  577. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  578. else
  579. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  580. return ret;
  581. }
  582. static int wpa_supplicant_ctrl_iface_tdls_setup(
  583. struct wpa_supplicant *wpa_s, char *addr)
  584. {
  585. u8 peer[ETH_ALEN];
  586. int ret;
  587. if (hwaddr_aton(addr, peer)) {
  588. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  589. "address '%s'", addr);
  590. return -1;
  591. }
  592. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  593. MAC2STR(peer));
  594. if ((wpa_s->conf->tdls_external_control) &&
  595. wpa_tdls_is_external_setup(wpa_s->wpa))
  596. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  597. wpa_tdls_remove(wpa_s->wpa, peer);
  598. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  599. ret = wpa_tdls_start(wpa_s->wpa, peer);
  600. else
  601. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  602. return ret;
  603. }
  604. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  605. struct wpa_supplicant *wpa_s, char *addr)
  606. {
  607. u8 peer[ETH_ALEN];
  608. int ret;
  609. if (os_strcmp(addr, "*") == 0) {
  610. /* remove everyone */
  611. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
  612. wpa_tdls_teardown_peers(wpa_s->wpa);
  613. return 0;
  614. }
  615. if (hwaddr_aton(addr, peer)) {
  616. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  617. "address '%s'", addr);
  618. return -1;
  619. }
  620. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  621. MAC2STR(peer));
  622. if ((wpa_s->conf->tdls_external_control) &&
  623. wpa_tdls_is_external_setup(wpa_s->wpa))
  624. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  625. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  626. ret = wpa_tdls_teardown_link(
  627. wpa_s->wpa, peer,
  628. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  629. else
  630. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  631. return ret;
  632. }
  633. static int ctrl_iface_get_capability_tdls(
  634. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  635. {
  636. int ret;
  637. ret = os_snprintf(buf, buflen, "%s\n",
  638. wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
  639. (wpa_s->drv_flags &
  640. WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
  641. "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
  642. if (os_snprintf_error(buflen, ret))
  643. return -1;
  644. return ret;
  645. }
  646. static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
  647. struct wpa_supplicant *wpa_s, char *cmd)
  648. {
  649. u8 peer[ETH_ALEN];
  650. struct hostapd_freq_params freq_params;
  651. u8 oper_class;
  652. char *pos, *end;
  653. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  654. wpa_printf(MSG_INFO,
  655. "tdls_chanswitch: Only supported with external setup");
  656. return -1;
  657. }
  658. os_memset(&freq_params, 0, sizeof(freq_params));
  659. pos = os_strchr(cmd, ' ');
  660. if (pos == NULL)
  661. return -1;
  662. *pos++ = '\0';
  663. oper_class = strtol(pos, &end, 10);
  664. if (pos == end) {
  665. wpa_printf(MSG_INFO,
  666. "tdls_chanswitch: Invalid op class provided");
  667. return -1;
  668. }
  669. pos = end;
  670. freq_params.freq = atoi(pos);
  671. if (freq_params.freq == 0) {
  672. wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
  673. return -1;
  674. }
  675. #define SET_FREQ_SETTING(str) \
  676. do { \
  677. const char *pos2 = os_strstr(pos, " " #str "="); \
  678. if (pos2) { \
  679. pos2 += sizeof(" " #str "=") - 1; \
  680. freq_params.str = atoi(pos2); \
  681. } \
  682. } while (0)
  683. SET_FREQ_SETTING(center_freq1);
  684. SET_FREQ_SETTING(center_freq2);
  685. SET_FREQ_SETTING(bandwidth);
  686. SET_FREQ_SETTING(sec_channel_offset);
  687. #undef SET_FREQ_SETTING
  688. freq_params.ht_enabled = !!os_strstr(pos, " ht");
  689. freq_params.vht_enabled = !!os_strstr(pos, " vht");
  690. if (hwaddr_aton(cmd, peer)) {
  691. wpa_printf(MSG_DEBUG,
  692. "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
  693. cmd);
  694. return -1;
  695. }
  696. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
  697. " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
  698. MAC2STR(peer), oper_class, freq_params.freq,
  699. freq_params.center_freq1, freq_params.center_freq2,
  700. freq_params.bandwidth, freq_params.sec_channel_offset,
  701. freq_params.ht_enabled ? " HT" : "",
  702. freq_params.vht_enabled ? " VHT" : "");
  703. return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
  704. &freq_params);
  705. }
  706. static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
  707. struct wpa_supplicant *wpa_s, char *cmd)
  708. {
  709. u8 peer[ETH_ALEN];
  710. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  711. wpa_printf(MSG_INFO,
  712. "tdls_chanswitch: Only supported with external setup");
  713. return -1;
  714. }
  715. if (hwaddr_aton(cmd, peer)) {
  716. wpa_printf(MSG_DEBUG,
  717. "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
  718. cmd);
  719. return -1;
  720. }
  721. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
  722. MAC2STR(peer));
  723. return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
  724. }
  725. static int wpa_supplicant_ctrl_iface_tdls_link_status(
  726. struct wpa_supplicant *wpa_s, const char *addr,
  727. char *buf, size_t buflen)
  728. {
  729. u8 peer[ETH_ALEN];
  730. const char *tdls_status;
  731. int ret;
  732. if (hwaddr_aton(addr, peer)) {
  733. wpa_printf(MSG_DEBUG,
  734. "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
  735. addr);
  736. return -1;
  737. }
  738. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
  739. MAC2STR(peer));
  740. tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
  741. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
  742. ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
  743. if (os_snprintf_error(buflen, ret))
  744. return -1;
  745. return ret;
  746. }
  747. #endif /* CONFIG_TDLS */
  748. static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
  749. {
  750. char *token, *context = NULL;
  751. struct wmm_ac_ts_setup_params params = {
  752. .tsid = 0xff,
  753. .direction = 0xff,
  754. };
  755. while ((token = str_token(cmd, " ", &context))) {
  756. if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
  757. sscanf(token, "up=%i", &params.user_priority) == 1 ||
  758. sscanf(token, "nominal_msdu_size=%i",
  759. &params.nominal_msdu_size) == 1 ||
  760. sscanf(token, "mean_data_rate=%i",
  761. &params.mean_data_rate) == 1 ||
  762. sscanf(token, "min_phy_rate=%i",
  763. &params.minimum_phy_rate) == 1 ||
  764. sscanf(token, "sba=%i",
  765. &params.surplus_bandwidth_allowance) == 1)
  766. continue;
  767. if (os_strcasecmp(token, "downlink") == 0) {
  768. params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
  769. } else if (os_strcasecmp(token, "uplink") == 0) {
  770. params.direction = WMM_TSPEC_DIRECTION_UPLINK;
  771. } else if (os_strcasecmp(token, "bidi") == 0) {
  772. params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
  773. } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
  774. params.fixed_nominal_msdu = 1;
  775. } else {
  776. wpa_printf(MSG_DEBUG,
  777. "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
  778. token);
  779. return -1;
  780. }
  781. }
  782. return wpas_wmm_ac_addts(wpa_s, &params);
  783. }
  784. static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
  785. {
  786. u8 tsid = atoi(cmd);
  787. return wpas_wmm_ac_delts(wpa_s, tsid);
  788. }
  789. #ifdef CONFIG_IEEE80211R
  790. static int wpa_supplicant_ctrl_iface_ft_ds(
  791. struct wpa_supplicant *wpa_s, char *addr)
  792. {
  793. u8 target_ap[ETH_ALEN];
  794. struct wpa_bss *bss;
  795. const u8 *mdie;
  796. if (hwaddr_aton(addr, target_ap)) {
  797. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  798. "address '%s'", addr);
  799. return -1;
  800. }
  801. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  802. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  803. if (bss)
  804. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  805. else
  806. mdie = NULL;
  807. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  808. }
  809. #endif /* CONFIG_IEEE80211R */
  810. #ifdef CONFIG_WPS
  811. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  812. char *cmd)
  813. {
  814. u8 bssid[ETH_ALEN], *_bssid = bssid;
  815. #ifdef CONFIG_P2P
  816. u8 p2p_dev_addr[ETH_ALEN];
  817. #endif /* CONFIG_P2P */
  818. #ifdef CONFIG_AP
  819. u8 *_p2p_dev_addr = NULL;
  820. #endif /* CONFIG_AP */
  821. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  822. _bssid = NULL;
  823. #ifdef CONFIG_P2P
  824. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  825. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  826. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  827. "P2P Device Address '%s'",
  828. cmd + 13);
  829. return -1;
  830. }
  831. _p2p_dev_addr = p2p_dev_addr;
  832. #endif /* CONFIG_P2P */
  833. } else if (hwaddr_aton(cmd, bssid)) {
  834. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  835. cmd);
  836. return -1;
  837. }
  838. #ifdef CONFIG_AP
  839. if (wpa_s->ap_iface)
  840. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  841. #endif /* CONFIG_AP */
  842. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  843. }
  844. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  845. char *cmd, char *buf,
  846. size_t buflen)
  847. {
  848. u8 bssid[ETH_ALEN], *_bssid = bssid;
  849. char *pin;
  850. int ret;
  851. pin = os_strchr(cmd, ' ');
  852. if (pin)
  853. *pin++ = '\0';
  854. if (os_strcmp(cmd, "any") == 0)
  855. _bssid = NULL;
  856. else if (os_strcmp(cmd, "get") == 0) {
  857. if (wps_generate_pin((unsigned int *) &ret) < 0)
  858. return -1;
  859. goto done;
  860. } else if (hwaddr_aton(cmd, bssid)) {
  861. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  862. cmd);
  863. return -1;
  864. }
  865. #ifdef CONFIG_AP
  866. if (wpa_s->ap_iface) {
  867. int timeout = 0;
  868. char *pos;
  869. if (pin) {
  870. pos = os_strchr(pin, ' ');
  871. if (pos) {
  872. *pos++ = '\0';
  873. timeout = atoi(pos);
  874. }
  875. }
  876. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  877. buf, buflen, timeout);
  878. }
  879. #endif /* CONFIG_AP */
  880. if (pin) {
  881. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  882. DEV_PW_DEFAULT);
  883. if (ret < 0)
  884. return -1;
  885. ret = os_snprintf(buf, buflen, "%s", pin);
  886. if (os_snprintf_error(buflen, ret))
  887. return -1;
  888. return ret;
  889. }
  890. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  891. if (ret < 0)
  892. return -1;
  893. done:
  894. /* Return the generated PIN */
  895. ret = os_snprintf(buf, buflen, "%08d", ret);
  896. if (os_snprintf_error(buflen, ret))
  897. return -1;
  898. return ret;
  899. }
  900. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  901. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  902. {
  903. char pin[9];
  904. size_t len;
  905. char *pos;
  906. int ret;
  907. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  908. (u8 *) cmd, os_strlen(cmd));
  909. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  910. if (*pos < '0' || *pos > '9')
  911. continue;
  912. pin[len++] = *pos;
  913. if (len == 9) {
  914. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  915. return -1;
  916. }
  917. }
  918. if (len != 4 && len != 8) {
  919. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  920. return -1;
  921. }
  922. pin[len] = '\0';
  923. if (len == 8) {
  924. unsigned int pin_val;
  925. pin_val = atoi(pin);
  926. if (!wps_pin_valid(pin_val)) {
  927. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  928. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  929. if (os_snprintf_error(buflen, ret))
  930. return -1;
  931. return ret;
  932. }
  933. }
  934. ret = os_snprintf(buf, buflen, "%s", pin);
  935. if (os_snprintf_error(buflen, ret))
  936. return -1;
  937. return ret;
  938. }
  939. #ifdef CONFIG_WPS_NFC
  940. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  941. char *cmd)
  942. {
  943. u8 bssid[ETH_ALEN], *_bssid = bssid;
  944. if (cmd == NULL || cmd[0] == '\0')
  945. _bssid = NULL;
  946. else if (hwaddr_aton(cmd, bssid))
  947. return -1;
  948. return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
  949. 0, 0);
  950. }
  951. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  952. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  953. {
  954. int ndef;
  955. struct wpabuf *buf;
  956. int res;
  957. char *pos;
  958. pos = os_strchr(cmd, ' ');
  959. if (pos)
  960. *pos++ = '\0';
  961. if (os_strcmp(cmd, "WPS") == 0)
  962. ndef = 0;
  963. else if (os_strcmp(cmd, "NDEF") == 0)
  964. ndef = 1;
  965. else
  966. return -1;
  967. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  968. if (buf == NULL)
  969. return -1;
  970. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  971. wpabuf_len(buf));
  972. reply[res++] = '\n';
  973. reply[res] = '\0';
  974. wpabuf_free(buf);
  975. return res;
  976. }
  977. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  978. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  979. {
  980. int ndef;
  981. struct wpabuf *buf;
  982. int res;
  983. if (os_strcmp(cmd, "WPS") == 0)
  984. ndef = 0;
  985. else if (os_strcmp(cmd, "NDEF") == 0)
  986. ndef = 1;
  987. else
  988. return -1;
  989. buf = wpas_wps_nfc_token(wpa_s, ndef);
  990. if (buf == NULL)
  991. return -1;
  992. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  993. wpabuf_len(buf));
  994. reply[res++] = '\n';
  995. reply[res] = '\0';
  996. wpabuf_free(buf);
  997. return res;
  998. }
  999. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  1000. struct wpa_supplicant *wpa_s, char *pos)
  1001. {
  1002. size_t len;
  1003. struct wpabuf *buf;
  1004. int ret;
  1005. char *freq;
  1006. int forced_freq = 0;
  1007. freq = strstr(pos, " freq=");
  1008. if (freq) {
  1009. *freq = '\0';
  1010. freq += 6;
  1011. forced_freq = atoi(freq);
  1012. }
  1013. len = os_strlen(pos);
  1014. if (len & 0x01)
  1015. return -1;
  1016. len /= 2;
  1017. buf = wpabuf_alloc(len);
  1018. if (buf == NULL)
  1019. return -1;
  1020. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  1021. wpabuf_free(buf);
  1022. return -1;
  1023. }
  1024. ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
  1025. wpabuf_free(buf);
  1026. return ret;
  1027. }
  1028. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  1029. char *reply, size_t max_len,
  1030. int ndef)
  1031. {
  1032. struct wpabuf *buf;
  1033. int res;
  1034. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  1035. if (buf == NULL)
  1036. return -1;
  1037. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1038. wpabuf_len(buf));
  1039. reply[res++] = '\n';
  1040. reply[res] = '\0';
  1041. wpabuf_free(buf);
  1042. return res;
  1043. }
  1044. #ifdef CONFIG_P2P
  1045. static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
  1046. char *reply, size_t max_len,
  1047. int ndef)
  1048. {
  1049. struct wpabuf *buf;
  1050. int res;
  1051. buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
  1052. if (buf == NULL) {
  1053. wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
  1054. return -1;
  1055. }
  1056. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1057. wpabuf_len(buf));
  1058. reply[res++] = '\n';
  1059. reply[res] = '\0';
  1060. wpabuf_free(buf);
  1061. return res;
  1062. }
  1063. #endif /* CONFIG_P2P */
  1064. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  1065. char *cmd, char *reply,
  1066. size_t max_len)
  1067. {
  1068. char *pos;
  1069. int ndef;
  1070. pos = os_strchr(cmd, ' ');
  1071. if (pos == NULL)
  1072. return -1;
  1073. *pos++ = '\0';
  1074. if (os_strcmp(cmd, "WPS") == 0)
  1075. ndef = 0;
  1076. else if (os_strcmp(cmd, "NDEF") == 0)
  1077. ndef = 1;
  1078. else
  1079. return -1;
  1080. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1081. if (!ndef)
  1082. return -1;
  1083. return wpas_ctrl_nfc_get_handover_req_wps(
  1084. wpa_s, reply, max_len, ndef);
  1085. }
  1086. #ifdef CONFIG_P2P
  1087. if (os_strcmp(pos, "P2P-CR") == 0) {
  1088. return wpas_ctrl_nfc_get_handover_req_p2p(
  1089. wpa_s, reply, max_len, ndef);
  1090. }
  1091. #endif /* CONFIG_P2P */
  1092. return -1;
  1093. }
  1094. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  1095. char *reply, size_t max_len,
  1096. int ndef, int cr, char *uuid)
  1097. {
  1098. struct wpabuf *buf;
  1099. int res;
  1100. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  1101. if (buf == NULL)
  1102. return -1;
  1103. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1104. wpabuf_len(buf));
  1105. reply[res++] = '\n';
  1106. reply[res] = '\0';
  1107. wpabuf_free(buf);
  1108. return res;
  1109. }
  1110. #ifdef CONFIG_P2P
  1111. static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
  1112. char *reply, size_t max_len,
  1113. int ndef, int tag)
  1114. {
  1115. struct wpabuf *buf;
  1116. int res;
  1117. buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
  1118. if (buf == NULL)
  1119. return -1;
  1120. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1121. wpabuf_len(buf));
  1122. reply[res++] = '\n';
  1123. reply[res] = '\0';
  1124. wpabuf_free(buf);
  1125. return res;
  1126. }
  1127. #endif /* CONFIG_P2P */
  1128. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  1129. char *cmd, char *reply,
  1130. size_t max_len)
  1131. {
  1132. char *pos, *pos2;
  1133. int ndef;
  1134. pos = os_strchr(cmd, ' ');
  1135. if (pos == NULL)
  1136. return -1;
  1137. *pos++ = '\0';
  1138. if (os_strcmp(cmd, "WPS") == 0)
  1139. ndef = 0;
  1140. else if (os_strcmp(cmd, "NDEF") == 0)
  1141. ndef = 1;
  1142. else
  1143. return -1;
  1144. pos2 = os_strchr(pos, ' ');
  1145. if (pos2)
  1146. *pos2++ = '\0';
  1147. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1148. if (!ndef)
  1149. return -1;
  1150. return wpas_ctrl_nfc_get_handover_sel_wps(
  1151. wpa_s, reply, max_len, ndef,
  1152. os_strcmp(pos, "WPS-CR") == 0, pos2);
  1153. }
  1154. #ifdef CONFIG_P2P
  1155. if (os_strcmp(pos, "P2P-CR") == 0) {
  1156. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1157. wpa_s, reply, max_len, ndef, 0);
  1158. }
  1159. if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
  1160. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1161. wpa_s, reply, max_len, ndef, 1);
  1162. }
  1163. #endif /* CONFIG_P2P */
  1164. return -1;
  1165. }
  1166. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  1167. char *cmd)
  1168. {
  1169. size_t len;
  1170. struct wpabuf *req, *sel;
  1171. int ret;
  1172. char *pos, *role, *type, *pos2;
  1173. #ifdef CONFIG_P2P
  1174. char *freq;
  1175. int forced_freq = 0;
  1176. freq = strstr(cmd, " freq=");
  1177. if (freq) {
  1178. *freq = '\0';
  1179. freq += 6;
  1180. forced_freq = atoi(freq);
  1181. }
  1182. #endif /* CONFIG_P2P */
  1183. role = cmd;
  1184. pos = os_strchr(role, ' ');
  1185. if (pos == NULL) {
  1186. wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
  1187. return -1;
  1188. }
  1189. *pos++ = '\0';
  1190. type = pos;
  1191. pos = os_strchr(type, ' ');
  1192. if (pos == NULL) {
  1193. wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
  1194. return -1;
  1195. }
  1196. *pos++ = '\0';
  1197. pos2 = os_strchr(pos, ' ');
  1198. if (pos2 == NULL) {
  1199. wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
  1200. return -1;
  1201. }
  1202. *pos2++ = '\0';
  1203. len = os_strlen(pos);
  1204. if (len & 0x01) {
  1205. wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
  1206. return -1;
  1207. }
  1208. len /= 2;
  1209. req = wpabuf_alloc(len);
  1210. if (req == NULL) {
  1211. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
  1212. return -1;
  1213. }
  1214. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  1215. wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
  1216. wpabuf_free(req);
  1217. return -1;
  1218. }
  1219. len = os_strlen(pos2);
  1220. if (len & 0x01) {
  1221. wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
  1222. wpabuf_free(req);
  1223. return -1;
  1224. }
  1225. len /= 2;
  1226. sel = wpabuf_alloc(len);
  1227. if (sel == NULL) {
  1228. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
  1229. wpabuf_free(req);
  1230. return -1;
  1231. }
  1232. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  1233. wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
  1234. wpabuf_free(req);
  1235. wpabuf_free(sel);
  1236. return -1;
  1237. }
  1238. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
  1239. role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
  1240. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  1241. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  1242. #ifdef CONFIG_AP
  1243. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
  1244. {
  1245. ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
  1246. if (ret < 0)
  1247. ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
  1248. #endif /* CONFIG_AP */
  1249. #ifdef CONFIG_P2P
  1250. } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
  1251. {
  1252. ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
  1253. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
  1254. {
  1255. ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
  1256. forced_freq);
  1257. #endif /* CONFIG_P2P */
  1258. } else {
  1259. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  1260. "reported: role=%s type=%s", role, type);
  1261. ret = -1;
  1262. }
  1263. wpabuf_free(req);
  1264. wpabuf_free(sel);
  1265. if (ret)
  1266. wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
  1267. return ret;
  1268. }
  1269. #endif /* CONFIG_WPS_NFC */
  1270. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  1271. char *cmd)
  1272. {
  1273. u8 bssid[ETH_ALEN];
  1274. char *pin;
  1275. char *new_ssid;
  1276. char *new_auth;
  1277. char *new_encr;
  1278. char *new_key;
  1279. struct wps_new_ap_settings ap;
  1280. pin = os_strchr(cmd, ' ');
  1281. if (pin == NULL)
  1282. return -1;
  1283. *pin++ = '\0';
  1284. if (hwaddr_aton(cmd, bssid)) {
  1285. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  1286. cmd);
  1287. return -1;
  1288. }
  1289. new_ssid = os_strchr(pin, ' ');
  1290. if (new_ssid == NULL)
  1291. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1292. *new_ssid++ = '\0';
  1293. new_auth = os_strchr(new_ssid, ' ');
  1294. if (new_auth == NULL)
  1295. return -1;
  1296. *new_auth++ = '\0';
  1297. new_encr = os_strchr(new_auth, ' ');
  1298. if (new_encr == NULL)
  1299. return -1;
  1300. *new_encr++ = '\0';
  1301. new_key = os_strchr(new_encr, ' ');
  1302. if (new_key == NULL)
  1303. return -1;
  1304. *new_key++ = '\0';
  1305. os_memset(&ap, 0, sizeof(ap));
  1306. ap.ssid_hex = new_ssid;
  1307. ap.auth = new_auth;
  1308. ap.encr = new_encr;
  1309. ap.key_hex = new_key;
  1310. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  1311. }
  1312. #ifdef CONFIG_AP
  1313. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  1314. char *cmd, char *buf,
  1315. size_t buflen)
  1316. {
  1317. int timeout = 300;
  1318. char *pos;
  1319. const char *pin_txt;
  1320. if (!wpa_s->ap_iface)
  1321. return -1;
  1322. pos = os_strchr(cmd, ' ');
  1323. if (pos)
  1324. *pos++ = '\0';
  1325. if (os_strcmp(cmd, "disable") == 0) {
  1326. wpas_wps_ap_pin_disable(wpa_s);
  1327. return os_snprintf(buf, buflen, "OK\n");
  1328. }
  1329. if (os_strcmp(cmd, "random") == 0) {
  1330. if (pos)
  1331. timeout = atoi(pos);
  1332. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1333. if (pin_txt == NULL)
  1334. return -1;
  1335. return os_snprintf(buf, buflen, "%s", pin_txt);
  1336. }
  1337. if (os_strcmp(cmd, "get") == 0) {
  1338. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1339. if (pin_txt == NULL)
  1340. return -1;
  1341. return os_snprintf(buf, buflen, "%s", pin_txt);
  1342. }
  1343. if (os_strcmp(cmd, "set") == 0) {
  1344. char *pin;
  1345. if (pos == NULL)
  1346. return -1;
  1347. pin = pos;
  1348. pos = os_strchr(pos, ' ');
  1349. if (pos) {
  1350. *pos++ = '\0';
  1351. timeout = atoi(pos);
  1352. }
  1353. if (os_strlen(pin) > buflen)
  1354. return -1;
  1355. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1356. return -1;
  1357. return os_snprintf(buf, buflen, "%s", pin);
  1358. }
  1359. return -1;
  1360. }
  1361. #endif /* CONFIG_AP */
  1362. #ifdef CONFIG_WPS_ER
  1363. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1364. char *cmd)
  1365. {
  1366. char *uuid = cmd, *pin, *pos;
  1367. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1368. pin = os_strchr(uuid, ' ');
  1369. if (pin == NULL)
  1370. return -1;
  1371. *pin++ = '\0';
  1372. pos = os_strchr(pin, ' ');
  1373. if (pos) {
  1374. *pos++ = '\0';
  1375. if (hwaddr_aton(pos, addr_buf) == 0)
  1376. addr = addr_buf;
  1377. }
  1378. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1379. }
  1380. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1381. char *cmd)
  1382. {
  1383. char *uuid = cmd, *pin;
  1384. pin = os_strchr(uuid, ' ');
  1385. if (pin == NULL)
  1386. return -1;
  1387. *pin++ = '\0';
  1388. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1389. }
  1390. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1391. struct wpa_supplicant *wpa_s, char *cmd)
  1392. {
  1393. char *uuid = cmd, *id;
  1394. id = os_strchr(uuid, ' ');
  1395. if (id == NULL)
  1396. return -1;
  1397. *id++ = '\0';
  1398. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1399. }
  1400. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1401. struct wpa_supplicant *wpa_s, char *cmd)
  1402. {
  1403. char *pin;
  1404. char *new_ssid;
  1405. char *new_auth;
  1406. char *new_encr;
  1407. char *new_key;
  1408. struct wps_new_ap_settings ap;
  1409. pin = os_strchr(cmd, ' ');
  1410. if (pin == NULL)
  1411. return -1;
  1412. *pin++ = '\0';
  1413. new_ssid = os_strchr(pin, ' ');
  1414. if (new_ssid == NULL)
  1415. return -1;
  1416. *new_ssid++ = '\0';
  1417. new_auth = os_strchr(new_ssid, ' ');
  1418. if (new_auth == NULL)
  1419. return -1;
  1420. *new_auth++ = '\0';
  1421. new_encr = os_strchr(new_auth, ' ');
  1422. if (new_encr == NULL)
  1423. return -1;
  1424. *new_encr++ = '\0';
  1425. new_key = os_strchr(new_encr, ' ');
  1426. if (new_key == NULL)
  1427. return -1;
  1428. *new_key++ = '\0';
  1429. os_memset(&ap, 0, sizeof(ap));
  1430. ap.ssid_hex = new_ssid;
  1431. ap.auth = new_auth;
  1432. ap.encr = new_encr;
  1433. ap.key_hex = new_key;
  1434. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1435. }
  1436. #ifdef CONFIG_WPS_NFC
  1437. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1438. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1439. {
  1440. int ndef;
  1441. struct wpabuf *buf;
  1442. int res;
  1443. char *uuid;
  1444. uuid = os_strchr(cmd, ' ');
  1445. if (uuid == NULL)
  1446. return -1;
  1447. *uuid++ = '\0';
  1448. if (os_strcmp(cmd, "WPS") == 0)
  1449. ndef = 0;
  1450. else if (os_strcmp(cmd, "NDEF") == 0)
  1451. ndef = 1;
  1452. else
  1453. return -1;
  1454. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1455. if (buf == NULL)
  1456. return -1;
  1457. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1458. wpabuf_len(buf));
  1459. reply[res++] = '\n';
  1460. reply[res] = '\0';
  1461. wpabuf_free(buf);
  1462. return res;
  1463. }
  1464. #endif /* CONFIG_WPS_NFC */
  1465. #endif /* CONFIG_WPS_ER */
  1466. #endif /* CONFIG_WPS */
  1467. #ifdef CONFIG_IBSS_RSN
  1468. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1469. struct wpa_supplicant *wpa_s, char *addr)
  1470. {
  1471. u8 peer[ETH_ALEN];
  1472. if (hwaddr_aton(addr, peer)) {
  1473. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1474. "address '%s'", addr);
  1475. return -1;
  1476. }
  1477. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1478. MAC2STR(peer));
  1479. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1480. }
  1481. #endif /* CONFIG_IBSS_RSN */
  1482. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1483. char *rsp)
  1484. {
  1485. #ifdef IEEE8021X_EAPOL
  1486. char *pos, *id_pos;
  1487. int id;
  1488. struct wpa_ssid *ssid;
  1489. pos = os_strchr(rsp, '-');
  1490. if (pos == NULL)
  1491. return -1;
  1492. *pos++ = '\0';
  1493. id_pos = pos;
  1494. pos = os_strchr(pos, ':');
  1495. if (pos == NULL)
  1496. return -1;
  1497. *pos++ = '\0';
  1498. id = atoi(id_pos);
  1499. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1500. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1501. (u8 *) pos, os_strlen(pos));
  1502. ssid = wpa_config_get_network(wpa_s->conf, id);
  1503. if (ssid == NULL) {
  1504. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1505. "to update", id);
  1506. return -1;
  1507. }
  1508. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1509. pos);
  1510. #else /* IEEE8021X_EAPOL */
  1511. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1512. return -1;
  1513. #endif /* IEEE8021X_EAPOL */
  1514. }
  1515. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1516. const char *params,
  1517. char *buf, size_t buflen)
  1518. {
  1519. char *pos, *end, tmp[30];
  1520. int res, verbose, wps, ret;
  1521. #ifdef CONFIG_HS20
  1522. const u8 *hs20;
  1523. #endif /* CONFIG_HS20 */
  1524. const u8 *sess_id;
  1525. size_t sess_id_len;
  1526. if (os_strcmp(params, "-DRIVER") == 0)
  1527. return wpa_drv_status(wpa_s, buf, buflen);
  1528. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1529. wps = os_strcmp(params, "-WPS") == 0;
  1530. pos = buf;
  1531. end = buf + buflen;
  1532. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1533. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1534. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1535. MAC2STR(wpa_s->bssid));
  1536. if (os_snprintf_error(end - pos, ret))
  1537. return pos - buf;
  1538. pos += ret;
  1539. ret = os_snprintf(pos, end - pos, "freq=%u\n",
  1540. wpa_s->assoc_freq);
  1541. if (os_snprintf_error(end - pos, ret))
  1542. return pos - buf;
  1543. pos += ret;
  1544. if (ssid) {
  1545. u8 *_ssid = ssid->ssid;
  1546. size_t ssid_len = ssid->ssid_len;
  1547. u8 ssid_buf[SSID_MAX_LEN];
  1548. if (ssid_len == 0) {
  1549. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1550. if (_res < 0)
  1551. ssid_len = 0;
  1552. else
  1553. ssid_len = _res;
  1554. _ssid = ssid_buf;
  1555. }
  1556. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1557. wpa_ssid_txt(_ssid, ssid_len),
  1558. ssid->id);
  1559. if (os_snprintf_error(end - pos, ret))
  1560. return pos - buf;
  1561. pos += ret;
  1562. if (wps && ssid->passphrase &&
  1563. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1564. (ssid->mode == WPAS_MODE_AP ||
  1565. ssid->mode == WPAS_MODE_P2P_GO)) {
  1566. ret = os_snprintf(pos, end - pos,
  1567. "passphrase=%s\n",
  1568. ssid->passphrase);
  1569. if (os_snprintf_error(end - pos, ret))
  1570. return pos - buf;
  1571. pos += ret;
  1572. }
  1573. if (ssid->id_str) {
  1574. ret = os_snprintf(pos, end - pos,
  1575. "id_str=%s\n",
  1576. ssid->id_str);
  1577. if (os_snprintf_error(end - pos, ret))
  1578. return pos - buf;
  1579. pos += ret;
  1580. }
  1581. switch (ssid->mode) {
  1582. case WPAS_MODE_INFRA:
  1583. ret = os_snprintf(pos, end - pos,
  1584. "mode=station\n");
  1585. break;
  1586. case WPAS_MODE_IBSS:
  1587. ret = os_snprintf(pos, end - pos,
  1588. "mode=IBSS\n");
  1589. break;
  1590. case WPAS_MODE_AP:
  1591. ret = os_snprintf(pos, end - pos,
  1592. "mode=AP\n");
  1593. break;
  1594. case WPAS_MODE_P2P_GO:
  1595. ret = os_snprintf(pos, end - pos,
  1596. "mode=P2P GO\n");
  1597. break;
  1598. case WPAS_MODE_P2P_GROUP_FORMATION:
  1599. ret = os_snprintf(pos, end - pos,
  1600. "mode=P2P GO - group "
  1601. "formation\n");
  1602. break;
  1603. case WPAS_MODE_MESH:
  1604. ret = os_snprintf(pos, end - pos,
  1605. "mode=mesh\n");
  1606. break;
  1607. default:
  1608. ret = 0;
  1609. break;
  1610. }
  1611. if (os_snprintf_error(end - pos, ret))
  1612. return pos - buf;
  1613. pos += ret;
  1614. }
  1615. #ifdef CONFIG_AP
  1616. if (wpa_s->ap_iface) {
  1617. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1618. end - pos,
  1619. verbose);
  1620. } else
  1621. #endif /* CONFIG_AP */
  1622. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1623. }
  1624. #ifdef CONFIG_SAE
  1625. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1626. #ifdef CONFIG_AP
  1627. !wpa_s->ap_iface &&
  1628. #endif /* CONFIG_AP */
  1629. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1630. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1631. wpa_s->sme.sae.group);
  1632. if (os_snprintf_error(end - pos, ret))
  1633. return pos - buf;
  1634. pos += ret;
  1635. }
  1636. #endif /* CONFIG_SAE */
  1637. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1638. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1639. if (os_snprintf_error(end - pos, ret))
  1640. return pos - buf;
  1641. pos += ret;
  1642. if (wpa_s->l2 &&
  1643. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1644. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1645. if (os_snprintf_error(end - pos, ret))
  1646. return pos - buf;
  1647. pos += ret;
  1648. }
  1649. #ifdef CONFIG_P2P
  1650. if (wpa_s->global->p2p) {
  1651. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1652. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1653. if (os_snprintf_error(end - pos, ret))
  1654. return pos - buf;
  1655. pos += ret;
  1656. }
  1657. #endif /* CONFIG_P2P */
  1658. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1659. MAC2STR(wpa_s->own_addr));
  1660. if (os_snprintf_error(end - pos, ret))
  1661. return pos - buf;
  1662. pos += ret;
  1663. #ifdef CONFIG_HS20
  1664. if (wpa_s->current_bss &&
  1665. (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1666. HS20_IE_VENDOR_TYPE)) &&
  1667. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1668. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1669. int release = 1;
  1670. if (hs20[1] >= 5) {
  1671. u8 rel_num = (hs20[6] & 0xf0) >> 4;
  1672. release = rel_num + 1;
  1673. }
  1674. ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
  1675. if (os_snprintf_error(end - pos, ret))
  1676. return pos - buf;
  1677. pos += ret;
  1678. }
  1679. if (wpa_s->current_ssid) {
  1680. struct wpa_cred *cred;
  1681. char *type;
  1682. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1683. size_t i;
  1684. if (wpa_s->current_ssid->parent_cred != cred)
  1685. continue;
  1686. if (cred->provisioning_sp) {
  1687. ret = os_snprintf(pos, end - pos,
  1688. "provisioning_sp=%s\n",
  1689. cred->provisioning_sp);
  1690. if (os_snprintf_error(end - pos, ret))
  1691. return pos - buf;
  1692. pos += ret;
  1693. }
  1694. if (!cred->domain)
  1695. goto no_domain;
  1696. i = 0;
  1697. if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
  1698. struct wpabuf *names =
  1699. wpa_s->current_bss->anqp->domain_name;
  1700. for (i = 0; names && i < cred->num_domain; i++)
  1701. {
  1702. if (domain_name_list_contains(
  1703. names, cred->domain[i], 1))
  1704. break;
  1705. }
  1706. if (i == cred->num_domain)
  1707. i = 0; /* show first entry by default */
  1708. }
  1709. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1710. cred->domain[i]);
  1711. if (os_snprintf_error(end - pos, ret))
  1712. return pos - buf;
  1713. pos += ret;
  1714. no_domain:
  1715. if (wpa_s->current_bss == NULL ||
  1716. wpa_s->current_bss->anqp == NULL)
  1717. res = -1;
  1718. else
  1719. res = interworking_home_sp_cred(
  1720. wpa_s, cred,
  1721. wpa_s->current_bss->anqp->domain_name);
  1722. if (res > 0)
  1723. type = "home";
  1724. else if (res == 0)
  1725. type = "roaming";
  1726. else
  1727. type = "unknown";
  1728. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1729. if (os_snprintf_error(end - pos, ret))
  1730. return pos - buf;
  1731. pos += ret;
  1732. break;
  1733. }
  1734. }
  1735. #endif /* CONFIG_HS20 */
  1736. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1737. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1738. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1739. verbose);
  1740. if (res >= 0)
  1741. pos += res;
  1742. }
  1743. sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
  1744. if (sess_id) {
  1745. char *start = pos;
  1746. ret = os_snprintf(pos, end - pos, "eap_session_id=");
  1747. if (os_snprintf_error(end - pos, ret))
  1748. return start - buf;
  1749. pos += ret;
  1750. ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
  1751. if (ret <= 0)
  1752. return start - buf;
  1753. pos += ret;
  1754. ret = os_snprintf(pos, end - pos, "\n");
  1755. if (os_snprintf_error(end - pos, ret))
  1756. return start - buf;
  1757. pos += ret;
  1758. }
  1759. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1760. if (res >= 0)
  1761. pos += res;
  1762. #ifdef CONFIG_WPS
  1763. {
  1764. char uuid_str[100];
  1765. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1766. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1767. if (os_snprintf_error(end - pos, ret))
  1768. return pos - buf;
  1769. pos += ret;
  1770. }
  1771. #endif /* CONFIG_WPS */
  1772. #ifdef ANDROID
  1773. /*
  1774. * Allow using the STATUS command with default behavior, say for debug,
  1775. * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
  1776. * events with STATUS-NO_EVENTS.
  1777. */
  1778. if (os_strcmp(params, "-NO_EVENTS")) {
  1779. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1780. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1781. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1782. wpa_s->wpa_state,
  1783. MAC2STR(wpa_s->bssid),
  1784. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1785. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1786. wpa_s->current_ssid->ssid_len) : "");
  1787. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1788. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1789. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1790. "- connection to " MACSTR
  1791. " completed %s [id=%d id_str=%s]",
  1792. MAC2STR(wpa_s->bssid), "(auth)",
  1793. ssid ? ssid->id : -1,
  1794. ssid && ssid->id_str ? ssid->id_str : "");
  1795. }
  1796. }
  1797. #endif /* ANDROID */
  1798. return pos - buf;
  1799. }
  1800. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1801. char *cmd)
  1802. {
  1803. char *pos;
  1804. int id;
  1805. struct wpa_ssid *ssid;
  1806. u8 bssid[ETH_ALEN];
  1807. /* cmd: "<network id> <BSSID>" */
  1808. pos = os_strchr(cmd, ' ');
  1809. if (pos == NULL)
  1810. return -1;
  1811. *pos++ = '\0';
  1812. id = atoi(cmd);
  1813. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1814. if (hwaddr_aton(pos, bssid)) {
  1815. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1816. return -1;
  1817. }
  1818. ssid = wpa_config_get_network(wpa_s->conf, id);
  1819. if (ssid == NULL) {
  1820. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1821. "to update", id);
  1822. return -1;
  1823. }
  1824. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1825. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1826. return 0;
  1827. }
  1828. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1829. char *cmd, char *buf,
  1830. size_t buflen)
  1831. {
  1832. u8 bssid[ETH_ALEN];
  1833. struct wpa_blacklist *e;
  1834. char *pos, *end;
  1835. int ret;
  1836. /* cmd: "BLACKLIST [<BSSID>]" */
  1837. if (*cmd == '\0') {
  1838. pos = buf;
  1839. end = buf + buflen;
  1840. e = wpa_s->blacklist;
  1841. while (e) {
  1842. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1843. MAC2STR(e->bssid));
  1844. if (os_snprintf_error(end - pos, ret))
  1845. return pos - buf;
  1846. pos += ret;
  1847. e = e->next;
  1848. }
  1849. return pos - buf;
  1850. }
  1851. cmd++;
  1852. if (os_strncmp(cmd, "clear", 5) == 0) {
  1853. wpa_blacklist_clear(wpa_s);
  1854. os_memcpy(buf, "OK\n", 3);
  1855. return 3;
  1856. }
  1857. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1858. if (hwaddr_aton(cmd, bssid)) {
  1859. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1860. return -1;
  1861. }
  1862. /*
  1863. * Add the BSSID twice, so its count will be 2, causing it to be
  1864. * skipped when processing scan results.
  1865. */
  1866. ret = wpa_blacklist_add(wpa_s, bssid);
  1867. if (ret < 0)
  1868. return -1;
  1869. ret = wpa_blacklist_add(wpa_s, bssid);
  1870. if (ret < 0)
  1871. return -1;
  1872. os_memcpy(buf, "OK\n", 3);
  1873. return 3;
  1874. }
  1875. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1876. char *cmd, char *buf,
  1877. size_t buflen)
  1878. {
  1879. char *pos, *end, *stamp;
  1880. int ret;
  1881. /* cmd: "LOG_LEVEL [<level>]" */
  1882. if (*cmd == '\0') {
  1883. pos = buf;
  1884. end = buf + buflen;
  1885. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1886. "Timestamp: %d\n",
  1887. debug_level_str(wpa_debug_level),
  1888. wpa_debug_timestamp);
  1889. if (os_snprintf_error(end - pos, ret))
  1890. ret = 0;
  1891. return ret;
  1892. }
  1893. while (*cmd == ' ')
  1894. cmd++;
  1895. stamp = os_strchr(cmd, ' ');
  1896. if (stamp) {
  1897. *stamp++ = '\0';
  1898. while (*stamp == ' ') {
  1899. stamp++;
  1900. }
  1901. }
  1902. if (os_strlen(cmd)) {
  1903. int level = str_to_debug_level(cmd);
  1904. if (level < 0)
  1905. return -1;
  1906. wpa_debug_level = level;
  1907. }
  1908. if (stamp && os_strlen(stamp))
  1909. wpa_debug_timestamp = atoi(stamp);
  1910. os_memcpy(buf, "OK\n", 3);
  1911. return 3;
  1912. }
  1913. static int wpa_supplicant_ctrl_iface_list_networks(
  1914. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1915. {
  1916. char *pos, *end, *prev;
  1917. struct wpa_ssid *ssid;
  1918. int ret;
  1919. pos = buf;
  1920. end = buf + buflen;
  1921. ret = os_snprintf(pos, end - pos,
  1922. "network id / ssid / bssid / flags\n");
  1923. if (os_snprintf_error(end - pos, ret))
  1924. return pos - buf;
  1925. pos += ret;
  1926. ssid = wpa_s->conf->ssid;
  1927. /* skip over ssids until we find next one */
  1928. if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
  1929. int last_id = atoi(cmd + 8);
  1930. if (last_id != -1) {
  1931. while (ssid != NULL && ssid->id <= last_id) {
  1932. ssid = ssid->next;
  1933. }
  1934. }
  1935. }
  1936. while (ssid) {
  1937. prev = pos;
  1938. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1939. ssid->id,
  1940. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1941. if (os_snprintf_error(end - pos, ret))
  1942. return prev - buf;
  1943. pos += ret;
  1944. if (ssid->bssid_set) {
  1945. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1946. MAC2STR(ssid->bssid));
  1947. } else {
  1948. ret = os_snprintf(pos, end - pos, "\tany");
  1949. }
  1950. if (os_snprintf_error(end - pos, ret))
  1951. return prev - buf;
  1952. pos += ret;
  1953. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1954. ssid == wpa_s->current_ssid ?
  1955. "[CURRENT]" : "",
  1956. ssid->disabled ? "[DISABLED]" : "",
  1957. ssid->disabled_until.sec ?
  1958. "[TEMP-DISABLED]" : "",
  1959. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1960. "");
  1961. if (os_snprintf_error(end - pos, ret))
  1962. return prev - buf;
  1963. pos += ret;
  1964. ret = os_snprintf(pos, end - pos, "\n");
  1965. if (os_snprintf_error(end - pos, ret))
  1966. return prev - buf;
  1967. pos += ret;
  1968. ssid = ssid->next;
  1969. }
  1970. return pos - buf;
  1971. }
  1972. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1973. {
  1974. int ret;
  1975. ret = os_snprintf(pos, end - pos, "-");
  1976. if (os_snprintf_error(end - pos, ret))
  1977. return pos;
  1978. pos += ret;
  1979. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1980. if (ret < 0)
  1981. return pos;
  1982. pos += ret;
  1983. return pos;
  1984. }
  1985. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1986. const u8 *ie, size_t ie_len)
  1987. {
  1988. struct wpa_ie_data data;
  1989. char *start;
  1990. int ret;
  1991. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1992. if (os_snprintf_error(end - pos, ret))
  1993. return pos;
  1994. pos += ret;
  1995. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1996. ret = os_snprintf(pos, end - pos, "?]");
  1997. if (os_snprintf_error(end - pos, ret))
  1998. return pos;
  1999. pos += ret;
  2000. return pos;
  2001. }
  2002. start = pos;
  2003. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  2004. ret = os_snprintf(pos, end - pos, "%sEAP",
  2005. pos == start ? "" : "+");
  2006. if (os_snprintf_error(end - pos, ret))
  2007. return pos;
  2008. pos += ret;
  2009. }
  2010. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  2011. ret = os_snprintf(pos, end - pos, "%sPSK",
  2012. pos == start ? "" : "+");
  2013. if (os_snprintf_error(end - pos, ret))
  2014. return pos;
  2015. pos += ret;
  2016. }
  2017. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  2018. ret = os_snprintf(pos, end - pos, "%sNone",
  2019. pos == start ? "" : "+");
  2020. if (os_snprintf_error(end - pos, ret))
  2021. return pos;
  2022. pos += ret;
  2023. }
  2024. if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
  2025. ret = os_snprintf(pos, end - pos, "%sSAE",
  2026. pos == start ? "" : "+");
  2027. if (os_snprintf_error(end - pos, ret))
  2028. return pos;
  2029. pos += ret;
  2030. }
  2031. #ifdef CONFIG_IEEE80211R
  2032. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  2033. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  2034. pos == start ? "" : "+");
  2035. if (os_snprintf_error(end - pos, ret))
  2036. return pos;
  2037. pos += ret;
  2038. }
  2039. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  2040. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  2041. pos == start ? "" : "+");
  2042. if (os_snprintf_error(end - pos, ret))
  2043. return pos;
  2044. pos += ret;
  2045. }
  2046. if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  2047. ret = os_snprintf(pos, end - pos, "%sFT/SAE",
  2048. pos == start ? "" : "+");
  2049. if (os_snprintf_error(end - pos, ret))
  2050. return pos;
  2051. pos += ret;
  2052. }
  2053. #endif /* CONFIG_IEEE80211R */
  2054. #ifdef CONFIG_IEEE80211W
  2055. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  2056. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  2057. pos == start ? "" : "+");
  2058. if (os_snprintf_error(end - pos, ret))
  2059. return pos;
  2060. pos += ret;
  2061. }
  2062. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  2063. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  2064. pos == start ? "" : "+");
  2065. if (os_snprintf_error(end - pos, ret))
  2066. return pos;
  2067. pos += ret;
  2068. }
  2069. #endif /* CONFIG_IEEE80211W */
  2070. #ifdef CONFIG_SUITEB
  2071. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  2072. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
  2073. pos == start ? "" : "+");
  2074. if (os_snprintf_error(end - pos, ret))
  2075. return pos;
  2076. pos += ret;
  2077. }
  2078. #endif /* CONFIG_SUITEB */
  2079. #ifdef CONFIG_SUITEB192
  2080. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  2081. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
  2082. pos == start ? "" : "+");
  2083. if (os_snprintf_error(end - pos, ret))
  2084. return pos;
  2085. pos += ret;
  2086. }
  2087. #endif /* CONFIG_SUITEB192 */
  2088. if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
  2089. ret = os_snprintf(pos, end - pos, "%sOSEN",
  2090. pos == start ? "" : "+");
  2091. if (os_snprintf_error(end - pos, ret))
  2092. return pos;
  2093. pos += ret;
  2094. }
  2095. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  2096. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  2097. ret = os_snprintf(pos, end - pos, "-preauth");
  2098. if (os_snprintf_error(end - pos, ret))
  2099. return pos;
  2100. pos += ret;
  2101. }
  2102. ret = os_snprintf(pos, end - pos, "]");
  2103. if (os_snprintf_error(end - pos, ret))
  2104. return pos;
  2105. pos += ret;
  2106. return pos;
  2107. }
  2108. #ifdef CONFIG_WPS
  2109. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  2110. char *pos, char *end,
  2111. struct wpabuf *wps_ie)
  2112. {
  2113. int ret;
  2114. const char *txt;
  2115. if (wps_ie == NULL)
  2116. return pos;
  2117. if (wps_is_selected_pbc_registrar(wps_ie))
  2118. txt = "[WPS-PBC]";
  2119. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  2120. txt = "[WPS-AUTH]";
  2121. else if (wps_is_selected_pin_registrar(wps_ie))
  2122. txt = "[WPS-PIN]";
  2123. else
  2124. txt = "[WPS]";
  2125. ret = os_snprintf(pos, end - pos, "%s", txt);
  2126. if (!os_snprintf_error(end - pos, ret))
  2127. pos += ret;
  2128. wpabuf_free(wps_ie);
  2129. return pos;
  2130. }
  2131. #endif /* CONFIG_WPS */
  2132. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  2133. char *pos, char *end,
  2134. const struct wpa_bss *bss)
  2135. {
  2136. #ifdef CONFIG_WPS
  2137. struct wpabuf *wps_ie;
  2138. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  2139. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  2140. #else /* CONFIG_WPS */
  2141. return pos;
  2142. #endif /* CONFIG_WPS */
  2143. }
  2144. /* Format one result on one text line into a buffer. */
  2145. static int wpa_supplicant_ctrl_iface_scan_result(
  2146. struct wpa_supplicant *wpa_s,
  2147. const struct wpa_bss *bss, char *buf, size_t buflen)
  2148. {
  2149. char *pos, *end;
  2150. int ret;
  2151. const u8 *ie, *ie2, *osen_ie, *p2p, *mesh;
  2152. mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
  2153. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  2154. if (!p2p)
  2155. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  2156. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  2157. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  2158. 0)
  2159. return 0; /* Do not show P2P listen discovery results here */
  2160. pos = buf;
  2161. end = buf + buflen;
  2162. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  2163. MAC2STR(bss->bssid), bss->freq, bss->level);
  2164. if (os_snprintf_error(end - pos, ret))
  2165. return -1;
  2166. pos += ret;
  2167. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2168. if (ie)
  2169. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  2170. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2171. if (ie2) {
  2172. pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
  2173. ie2, 2 + ie2[1]);
  2174. }
  2175. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  2176. if (osen_ie)
  2177. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  2178. osen_ie, 2 + osen_ie[1]);
  2179. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2180. if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
  2181. ret = os_snprintf(pos, end - pos, "[WEP]");
  2182. if (os_snprintf_error(end - pos, ret))
  2183. return -1;
  2184. pos += ret;
  2185. }
  2186. if (mesh) {
  2187. ret = os_snprintf(pos, end - pos, "[MESH]");
  2188. if (os_snprintf_error(end - pos, ret))
  2189. return -1;
  2190. pos += ret;
  2191. }
  2192. if (bss_is_dmg(bss)) {
  2193. const char *s;
  2194. ret = os_snprintf(pos, end - pos, "[DMG]");
  2195. if (os_snprintf_error(end - pos, ret))
  2196. return -1;
  2197. pos += ret;
  2198. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  2199. case IEEE80211_CAP_DMG_IBSS:
  2200. s = "[IBSS]";
  2201. break;
  2202. case IEEE80211_CAP_DMG_AP:
  2203. s = "[ESS]";
  2204. break;
  2205. case IEEE80211_CAP_DMG_PBSS:
  2206. s = "[PBSS]";
  2207. break;
  2208. default:
  2209. s = "";
  2210. break;
  2211. }
  2212. ret = os_snprintf(pos, end - pos, "%s", s);
  2213. if (os_snprintf_error(end - pos, ret))
  2214. return -1;
  2215. pos += ret;
  2216. } else {
  2217. if (bss->caps & IEEE80211_CAP_IBSS) {
  2218. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2219. if (os_snprintf_error(end - pos, ret))
  2220. return -1;
  2221. pos += ret;
  2222. }
  2223. if (bss->caps & IEEE80211_CAP_ESS) {
  2224. ret = os_snprintf(pos, end - pos, "[ESS]");
  2225. if (os_snprintf_error(end - pos, ret))
  2226. return -1;
  2227. pos += ret;
  2228. }
  2229. }
  2230. if (p2p) {
  2231. ret = os_snprintf(pos, end - pos, "[P2P]");
  2232. if (os_snprintf_error(end - pos, ret))
  2233. return -1;
  2234. pos += ret;
  2235. }
  2236. #ifdef CONFIG_HS20
  2237. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  2238. ret = os_snprintf(pos, end - pos, "[HS20]");
  2239. if (os_snprintf_error(end - pos, ret))
  2240. return -1;
  2241. pos += ret;
  2242. }
  2243. #endif /* CONFIG_HS20 */
  2244. #ifdef CONFIG_FST
  2245. if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
  2246. ret = os_snprintf(pos, end - pos, "[FST]");
  2247. if (os_snprintf_error(end - pos, ret))
  2248. return -1;
  2249. pos += ret;
  2250. }
  2251. #endif /* CONFIG_FST */
  2252. ret = os_snprintf(pos, end - pos, "\t%s",
  2253. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2254. if (os_snprintf_error(end - pos, ret))
  2255. return -1;
  2256. pos += ret;
  2257. ret = os_snprintf(pos, end - pos, "\n");
  2258. if (os_snprintf_error(end - pos, ret))
  2259. return -1;
  2260. pos += ret;
  2261. return pos - buf;
  2262. }
  2263. static int wpa_supplicant_ctrl_iface_scan_results(
  2264. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2265. {
  2266. char *pos, *end;
  2267. struct wpa_bss *bss;
  2268. int ret;
  2269. pos = buf;
  2270. end = buf + buflen;
  2271. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  2272. "flags / ssid\n");
  2273. if (os_snprintf_error(end - pos, ret))
  2274. return pos - buf;
  2275. pos += ret;
  2276. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2277. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  2278. end - pos);
  2279. if (ret < 0 || ret >= end - pos)
  2280. return pos - buf;
  2281. pos += ret;
  2282. }
  2283. return pos - buf;
  2284. }
  2285. #ifdef CONFIG_MESH
  2286. static int wpa_supplicant_ctrl_iface_mesh_interface_add(
  2287. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  2288. {
  2289. char *pos, ifname[IFNAMSIZ + 1];
  2290. ifname[0] = '\0';
  2291. pos = os_strstr(cmd, "ifname=");
  2292. if (pos) {
  2293. pos += 7;
  2294. os_strlcpy(ifname, pos, sizeof(ifname));
  2295. }
  2296. if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
  2297. return -1;
  2298. os_strlcpy(reply, ifname, max_len);
  2299. return os_strlen(ifname);
  2300. }
  2301. static int wpa_supplicant_ctrl_iface_mesh_group_add(
  2302. struct wpa_supplicant *wpa_s, char *cmd)
  2303. {
  2304. int id;
  2305. struct wpa_ssid *ssid;
  2306. id = atoi(cmd);
  2307. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
  2308. ssid = wpa_config_get_network(wpa_s->conf, id);
  2309. if (ssid == NULL) {
  2310. wpa_printf(MSG_DEBUG,
  2311. "CTRL_IFACE: Could not find network id=%d", id);
  2312. return -1;
  2313. }
  2314. if (ssid->mode != WPAS_MODE_MESH) {
  2315. wpa_printf(MSG_DEBUG,
  2316. "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
  2317. return -1;
  2318. }
  2319. if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
  2320. ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
  2321. wpa_printf(MSG_ERROR,
  2322. "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
  2323. return -1;
  2324. }
  2325. /*
  2326. * TODO: If necessary write our own group_add function,
  2327. * for now we can reuse select_network
  2328. */
  2329. wpa_supplicant_select_network(wpa_s, ssid);
  2330. return 0;
  2331. }
  2332. static int wpa_supplicant_ctrl_iface_mesh_group_remove(
  2333. struct wpa_supplicant *wpa_s, char *cmd)
  2334. {
  2335. struct wpa_supplicant *orig;
  2336. struct wpa_global *global;
  2337. int found = 0;
  2338. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
  2339. global = wpa_s->global;
  2340. orig = wpa_s;
  2341. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2342. if (os_strcmp(wpa_s->ifname, cmd) == 0) {
  2343. found = 1;
  2344. break;
  2345. }
  2346. }
  2347. if (!found) {
  2348. wpa_printf(MSG_ERROR,
  2349. "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
  2350. cmd);
  2351. return -1;
  2352. }
  2353. if (wpa_s->mesh_if_created && wpa_s == orig) {
  2354. wpa_printf(MSG_ERROR,
  2355. "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
  2356. return -1;
  2357. }
  2358. wpa_s->reassociate = 0;
  2359. wpa_s->disconnected = 1;
  2360. wpa_supplicant_cancel_sched_scan(wpa_s);
  2361. wpa_supplicant_cancel_scan(wpa_s);
  2362. /*
  2363. * TODO: If necessary write our own group_remove function,
  2364. * for now we can reuse deauthenticate
  2365. */
  2366. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2367. if (wpa_s->mesh_if_created)
  2368. wpa_supplicant_remove_iface(global, wpa_s, 0);
  2369. return 0;
  2370. }
  2371. static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
  2372. struct wpa_supplicant *wpa_s, char *cmd)
  2373. {
  2374. u8 addr[ETH_ALEN];
  2375. if (hwaddr_aton(cmd, addr) < 0)
  2376. return -1;
  2377. return wpas_mesh_peer_remove(wpa_s, addr);
  2378. }
  2379. static int wpa_supplicant_ctrl_iface_mesh_peer_add(
  2380. struct wpa_supplicant *wpa_s, char *cmd)
  2381. {
  2382. u8 addr[ETH_ALEN];
  2383. int duration;
  2384. char *pos;
  2385. pos = os_strstr(cmd, " duration=");
  2386. if (pos) {
  2387. *pos = '\0';
  2388. duration = atoi(pos + 10);
  2389. } else {
  2390. duration = -1;
  2391. }
  2392. if (hwaddr_aton(cmd, addr))
  2393. return -1;
  2394. return wpas_mesh_peer_add(wpa_s, addr, duration);
  2395. }
  2396. #endif /* CONFIG_MESH */
  2397. static int wpa_supplicant_ctrl_iface_select_network(
  2398. struct wpa_supplicant *wpa_s, char *cmd)
  2399. {
  2400. int id;
  2401. struct wpa_ssid *ssid;
  2402. char *pos;
  2403. /* cmd: "<network id>" or "any" */
  2404. if (os_strncmp(cmd, "any", 3) == 0) {
  2405. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  2406. ssid = NULL;
  2407. } else {
  2408. id = atoi(cmd);
  2409. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  2410. ssid = wpa_config_get_network(wpa_s->conf, id);
  2411. if (ssid == NULL) {
  2412. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2413. "network id=%d", id);
  2414. return -1;
  2415. }
  2416. if (ssid->disabled == 2) {
  2417. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2418. "SELECT_NETWORK with persistent P2P group");
  2419. return -1;
  2420. }
  2421. }
  2422. pos = os_strstr(cmd, " freq=");
  2423. if (pos) {
  2424. int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
  2425. if (freqs) {
  2426. wpa_s->scan_req = MANUAL_SCAN_REQ;
  2427. os_free(wpa_s->manual_scan_freqs);
  2428. wpa_s->manual_scan_freqs = freqs;
  2429. }
  2430. }
  2431. wpa_s->scan_min_time.sec = 0;
  2432. wpa_s->scan_min_time.usec = 0;
  2433. wpa_supplicant_select_network(wpa_s, ssid);
  2434. return 0;
  2435. }
  2436. static int wpa_supplicant_ctrl_iface_enable_network(
  2437. struct wpa_supplicant *wpa_s, char *cmd)
  2438. {
  2439. int id;
  2440. struct wpa_ssid *ssid;
  2441. /* cmd: "<network id>" or "all" */
  2442. if (os_strcmp(cmd, "all") == 0) {
  2443. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2444. ssid = NULL;
  2445. } else {
  2446. id = atoi(cmd);
  2447. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2448. ssid = wpa_config_get_network(wpa_s->conf, id);
  2449. if (ssid == NULL) {
  2450. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2451. "network id=%d", id);
  2452. return -1;
  2453. }
  2454. if (ssid->disabled == 2) {
  2455. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2456. "ENABLE_NETWORK with persistent P2P group");
  2457. return -1;
  2458. }
  2459. if (os_strstr(cmd, " no-connect")) {
  2460. ssid->disabled = 0;
  2461. return 0;
  2462. }
  2463. }
  2464. wpa_s->scan_min_time.sec = 0;
  2465. wpa_s->scan_min_time.usec = 0;
  2466. wpa_supplicant_enable_network(wpa_s, ssid);
  2467. return 0;
  2468. }
  2469. static int wpa_supplicant_ctrl_iface_disable_network(
  2470. struct wpa_supplicant *wpa_s, char *cmd)
  2471. {
  2472. int id;
  2473. struct wpa_ssid *ssid;
  2474. /* cmd: "<network id>" or "all" */
  2475. if (os_strcmp(cmd, "all") == 0) {
  2476. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2477. ssid = NULL;
  2478. } else {
  2479. id = atoi(cmd);
  2480. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2481. ssid = wpa_config_get_network(wpa_s->conf, id);
  2482. if (ssid == NULL) {
  2483. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2484. "network id=%d", id);
  2485. return -1;
  2486. }
  2487. if (ssid->disabled == 2) {
  2488. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2489. "DISABLE_NETWORK with persistent P2P "
  2490. "group");
  2491. return -1;
  2492. }
  2493. }
  2494. wpa_supplicant_disable_network(wpa_s, ssid);
  2495. return 0;
  2496. }
  2497. static int wpa_supplicant_ctrl_iface_add_network(
  2498. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2499. {
  2500. struct wpa_ssid *ssid;
  2501. int ret;
  2502. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2503. ssid = wpa_supplicant_add_network(wpa_s);
  2504. if (ssid == NULL)
  2505. return -1;
  2506. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2507. if (os_snprintf_error(buflen, ret))
  2508. return -1;
  2509. return ret;
  2510. }
  2511. static int wpa_supplicant_ctrl_iface_remove_network(
  2512. struct wpa_supplicant *wpa_s, char *cmd)
  2513. {
  2514. int id;
  2515. struct wpa_ssid *ssid;
  2516. int result;
  2517. /* cmd: "<network id>" or "all" */
  2518. if (os_strcmp(cmd, "all") == 0) {
  2519. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2520. if (wpa_s->sched_scanning)
  2521. wpa_supplicant_cancel_sched_scan(wpa_s);
  2522. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2523. if (wpa_s->current_ssid) {
  2524. #ifdef CONFIG_SME
  2525. wpa_s->sme.prev_bssid_set = 0;
  2526. #endif /* CONFIG_SME */
  2527. wpa_sm_set_config(wpa_s->wpa, NULL);
  2528. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2529. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2530. wpa_s->own_disconnect_req = 1;
  2531. wpa_supplicant_deauthenticate(
  2532. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2533. }
  2534. ssid = wpa_s->conf->ssid;
  2535. while (ssid) {
  2536. struct wpa_ssid *remove_ssid = ssid;
  2537. id = ssid->id;
  2538. ssid = ssid->next;
  2539. if (wpa_s->last_ssid == remove_ssid)
  2540. wpa_s->last_ssid = NULL;
  2541. wpas_notify_network_removed(wpa_s, remove_ssid);
  2542. wpa_config_remove_network(wpa_s->conf, id);
  2543. }
  2544. return 0;
  2545. }
  2546. id = atoi(cmd);
  2547. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2548. result = wpa_supplicant_remove_network(wpa_s, id);
  2549. if (result == -1) {
  2550. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2551. "id=%d", id);
  2552. return -1;
  2553. }
  2554. if (result == -2) {
  2555. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2556. "network id=%d", id);
  2557. return -1;
  2558. }
  2559. return 0;
  2560. }
  2561. static int wpa_supplicant_ctrl_iface_update_network(
  2562. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2563. char *name, char *value)
  2564. {
  2565. int ret;
  2566. ret = wpa_config_set(ssid, name, value, 0);
  2567. if (ret < 0) {
  2568. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2569. "variable '%s'", name);
  2570. return -1;
  2571. }
  2572. if (ret == 1)
  2573. return 0; /* No change to the previously configured value */
  2574. if (os_strcmp(name, "bssid") != 0 &&
  2575. os_strcmp(name, "priority") != 0) {
  2576. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2577. if (wpa_s->current_ssid == ssid ||
  2578. wpa_s->current_ssid == NULL) {
  2579. /*
  2580. * Invalidate the EAP session cache if anything in the
  2581. * current or previously used configuration changes.
  2582. */
  2583. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2584. }
  2585. }
  2586. if ((os_strcmp(name, "psk") == 0 &&
  2587. value[0] == '"' && ssid->ssid_len) ||
  2588. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2589. wpa_config_update_psk(ssid);
  2590. else if (os_strcmp(name, "priority") == 0)
  2591. wpa_config_update_prio_list(wpa_s->conf);
  2592. return 0;
  2593. }
  2594. static int wpa_supplicant_ctrl_iface_set_network(
  2595. struct wpa_supplicant *wpa_s, char *cmd)
  2596. {
  2597. int id, ret, prev_bssid_set, prev_disabled;
  2598. struct wpa_ssid *ssid;
  2599. char *name, *value;
  2600. u8 prev_bssid[ETH_ALEN];
  2601. /* cmd: "<network id> <variable name> <value>" */
  2602. name = os_strchr(cmd, ' ');
  2603. if (name == NULL)
  2604. return -1;
  2605. *name++ = '\0';
  2606. value = os_strchr(name, ' ');
  2607. if (value == NULL)
  2608. return -1;
  2609. *value++ = '\0';
  2610. id = atoi(cmd);
  2611. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2612. id, name);
  2613. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2614. (u8 *) value, os_strlen(value));
  2615. ssid = wpa_config_get_network(wpa_s->conf, id);
  2616. if (ssid == NULL) {
  2617. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2618. "id=%d", id);
  2619. return -1;
  2620. }
  2621. prev_bssid_set = ssid->bssid_set;
  2622. prev_disabled = ssid->disabled;
  2623. os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
  2624. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
  2625. value);
  2626. if (ret == 0 &&
  2627. (ssid->bssid_set != prev_bssid_set ||
  2628. os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
  2629. wpas_notify_network_bssid_set_changed(wpa_s, ssid);
  2630. if (prev_disabled != ssid->disabled &&
  2631. (prev_disabled == 2 || ssid->disabled == 2))
  2632. wpas_notify_network_type_changed(wpa_s, ssid);
  2633. return ret;
  2634. }
  2635. static int wpa_supplicant_ctrl_iface_get_network(
  2636. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2637. {
  2638. int id;
  2639. size_t res;
  2640. struct wpa_ssid *ssid;
  2641. char *name, *value;
  2642. /* cmd: "<network id> <variable name>" */
  2643. name = os_strchr(cmd, ' ');
  2644. if (name == NULL || buflen == 0)
  2645. return -1;
  2646. *name++ = '\0';
  2647. id = atoi(cmd);
  2648. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2649. id, name);
  2650. ssid = wpa_config_get_network(wpa_s->conf, id);
  2651. if (ssid == NULL) {
  2652. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
  2653. "id=%d", id);
  2654. return -1;
  2655. }
  2656. value = wpa_config_get_no_key(ssid, name);
  2657. if (value == NULL) {
  2658. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
  2659. "variable '%s'", name);
  2660. return -1;
  2661. }
  2662. res = os_strlcpy(buf, value, buflen);
  2663. if (res >= buflen) {
  2664. os_free(value);
  2665. return -1;
  2666. }
  2667. os_free(value);
  2668. return res;
  2669. }
  2670. static int wpa_supplicant_ctrl_iface_dup_network(
  2671. struct wpa_supplicant *wpa_s, char *cmd,
  2672. struct wpa_supplicant *dst_wpa_s)
  2673. {
  2674. struct wpa_ssid *ssid_s, *ssid_d;
  2675. char *name, *id, *value;
  2676. int id_s, id_d, ret;
  2677. /* cmd: "<src network id> <dst network id> <variable name>" */
  2678. id = os_strchr(cmd, ' ');
  2679. if (id == NULL)
  2680. return -1;
  2681. *id++ = '\0';
  2682. name = os_strchr(id, ' ');
  2683. if (name == NULL)
  2684. return -1;
  2685. *name++ = '\0';
  2686. id_s = atoi(cmd);
  2687. id_d = atoi(id);
  2688. wpa_printf(MSG_DEBUG,
  2689. "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
  2690. wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
  2691. ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
  2692. if (ssid_s == NULL) {
  2693. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2694. "network id=%d", id_s);
  2695. return -1;
  2696. }
  2697. ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
  2698. if (ssid_d == NULL) {
  2699. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2700. "network id=%d", id_d);
  2701. return -1;
  2702. }
  2703. value = wpa_config_get(ssid_s, name);
  2704. if (value == NULL) {
  2705. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2706. "variable '%s'", name);
  2707. return -1;
  2708. }
  2709. ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
  2710. value);
  2711. os_free(value);
  2712. return ret;
  2713. }
  2714. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2715. char *buf, size_t buflen)
  2716. {
  2717. char *pos, *end;
  2718. struct wpa_cred *cred;
  2719. int ret;
  2720. pos = buf;
  2721. end = buf + buflen;
  2722. ret = os_snprintf(pos, end - pos,
  2723. "cred id / realm / username / domain / imsi\n");
  2724. if (os_snprintf_error(end - pos, ret))
  2725. return pos - buf;
  2726. pos += ret;
  2727. cred = wpa_s->conf->cred;
  2728. while (cred) {
  2729. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2730. cred->id, cred->realm ? cred->realm : "",
  2731. cred->username ? cred->username : "",
  2732. cred->domain ? cred->domain[0] : "",
  2733. cred->imsi ? cred->imsi : "");
  2734. if (os_snprintf_error(end - pos, ret))
  2735. return pos - buf;
  2736. pos += ret;
  2737. cred = cred->next;
  2738. }
  2739. return pos - buf;
  2740. }
  2741. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2742. char *buf, size_t buflen)
  2743. {
  2744. struct wpa_cred *cred;
  2745. int ret;
  2746. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2747. cred = wpa_config_add_cred(wpa_s->conf);
  2748. if (cred == NULL)
  2749. return -1;
  2750. wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
  2751. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2752. if (os_snprintf_error(buflen, ret))
  2753. return -1;
  2754. return ret;
  2755. }
  2756. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2757. struct wpa_cred *cred)
  2758. {
  2759. struct wpa_ssid *ssid;
  2760. char str[20];
  2761. int id;
  2762. if (cred == NULL) {
  2763. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2764. return -1;
  2765. }
  2766. id = cred->id;
  2767. if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  2768. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2769. return -1;
  2770. }
  2771. wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
  2772. /* Remove any network entry created based on the removed credential */
  2773. ssid = wpa_s->conf->ssid;
  2774. while (ssid) {
  2775. if (ssid->parent_cred == cred) {
  2776. int res;
  2777. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2778. "used the removed credential", ssid->id);
  2779. res = os_snprintf(str, sizeof(str), "%d", ssid->id);
  2780. if (os_snprintf_error(sizeof(str), res))
  2781. str[sizeof(str) - 1] = '\0';
  2782. ssid = ssid->next;
  2783. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2784. } else
  2785. ssid = ssid->next;
  2786. }
  2787. return 0;
  2788. }
  2789. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2790. char *cmd)
  2791. {
  2792. int id;
  2793. struct wpa_cred *cred, *prev;
  2794. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2795. * "provisioning_sp=<FQDN> */
  2796. if (os_strcmp(cmd, "all") == 0) {
  2797. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2798. cred = wpa_s->conf->cred;
  2799. while (cred) {
  2800. prev = cred;
  2801. cred = cred->next;
  2802. wpas_ctrl_remove_cred(wpa_s, prev);
  2803. }
  2804. return 0;
  2805. }
  2806. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2807. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2808. cmd + 8);
  2809. cred = wpa_s->conf->cred;
  2810. while (cred) {
  2811. prev = cred;
  2812. cred = cred->next;
  2813. if (prev->domain) {
  2814. size_t i;
  2815. for (i = 0; i < prev->num_domain; i++) {
  2816. if (os_strcmp(prev->domain[i], cmd + 8)
  2817. != 0)
  2818. continue;
  2819. wpas_ctrl_remove_cred(wpa_s, prev);
  2820. break;
  2821. }
  2822. }
  2823. }
  2824. return 0;
  2825. }
  2826. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  2827. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  2828. cmd + 16);
  2829. cred = wpa_s->conf->cred;
  2830. while (cred) {
  2831. prev = cred;
  2832. cred = cred->next;
  2833. if (prev->provisioning_sp &&
  2834. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  2835. wpas_ctrl_remove_cred(wpa_s, prev);
  2836. }
  2837. return 0;
  2838. }
  2839. id = atoi(cmd);
  2840. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2841. cred = wpa_config_get_cred(wpa_s->conf, id);
  2842. return wpas_ctrl_remove_cred(wpa_s, cred);
  2843. }
  2844. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2845. char *cmd)
  2846. {
  2847. int id;
  2848. struct wpa_cred *cred;
  2849. char *name, *value;
  2850. /* cmd: "<cred id> <variable name> <value>" */
  2851. name = os_strchr(cmd, ' ');
  2852. if (name == NULL)
  2853. return -1;
  2854. *name++ = '\0';
  2855. value = os_strchr(name, ' ');
  2856. if (value == NULL)
  2857. return -1;
  2858. *value++ = '\0';
  2859. id = atoi(cmd);
  2860. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2861. id, name);
  2862. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2863. (u8 *) value, os_strlen(value));
  2864. cred = wpa_config_get_cred(wpa_s->conf, id);
  2865. if (cred == NULL) {
  2866. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2867. id);
  2868. return -1;
  2869. }
  2870. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2871. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2872. "variable '%s'", name);
  2873. return -1;
  2874. }
  2875. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  2876. return 0;
  2877. }
  2878. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  2879. char *cmd, char *buf,
  2880. size_t buflen)
  2881. {
  2882. int id;
  2883. size_t res;
  2884. struct wpa_cred *cred;
  2885. char *name, *value;
  2886. /* cmd: "<cred id> <variable name>" */
  2887. name = os_strchr(cmd, ' ');
  2888. if (name == NULL)
  2889. return -1;
  2890. *name++ = '\0';
  2891. id = atoi(cmd);
  2892. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  2893. id, name);
  2894. cred = wpa_config_get_cred(wpa_s->conf, id);
  2895. if (cred == NULL) {
  2896. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2897. id);
  2898. return -1;
  2899. }
  2900. value = wpa_config_get_cred_no_key(cred, name);
  2901. if (value == NULL) {
  2902. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  2903. name);
  2904. return -1;
  2905. }
  2906. res = os_strlcpy(buf, value, buflen);
  2907. if (res >= buflen) {
  2908. os_free(value);
  2909. return -1;
  2910. }
  2911. os_free(value);
  2912. return res;
  2913. }
  2914. #ifndef CONFIG_NO_CONFIG_WRITE
  2915. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2916. {
  2917. int ret;
  2918. if (!wpa_s->conf->update_config) {
  2919. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2920. "to update configuration (update_config=0)");
  2921. return -1;
  2922. }
  2923. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2924. if (ret) {
  2925. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2926. "update configuration");
  2927. } else {
  2928. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2929. " updated");
  2930. }
  2931. return ret;
  2932. }
  2933. #endif /* CONFIG_NO_CONFIG_WRITE */
  2934. struct cipher_info {
  2935. unsigned int capa;
  2936. const char *name;
  2937. int group_only;
  2938. };
  2939. static const struct cipher_info ciphers[] = {
  2940. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2941. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2942. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2943. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2944. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2945. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2946. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2947. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2948. };
  2949. static const struct cipher_info ciphers_group_mgmt[] = {
  2950. { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
  2951. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
  2952. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
  2953. { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
  2954. };
  2955. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2956. struct wpa_driver_capa *capa,
  2957. char *buf, size_t buflen)
  2958. {
  2959. int ret;
  2960. char *pos, *end;
  2961. size_t len;
  2962. unsigned int i;
  2963. pos = buf;
  2964. end = pos + buflen;
  2965. if (res < 0) {
  2966. if (strict)
  2967. return 0;
  2968. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2969. if (len >= buflen)
  2970. return -1;
  2971. return len;
  2972. }
  2973. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2974. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2975. ret = os_snprintf(pos, end - pos, "%s%s",
  2976. pos == buf ? "" : " ",
  2977. ciphers[i].name);
  2978. if (os_snprintf_error(end - pos, ret))
  2979. return pos - buf;
  2980. pos += ret;
  2981. }
  2982. }
  2983. return pos - buf;
  2984. }
  2985. static int ctrl_iface_get_capability_group(int res, char *strict,
  2986. struct wpa_driver_capa *capa,
  2987. char *buf, size_t buflen)
  2988. {
  2989. int ret;
  2990. char *pos, *end;
  2991. size_t len;
  2992. unsigned int i;
  2993. pos = buf;
  2994. end = pos + buflen;
  2995. if (res < 0) {
  2996. if (strict)
  2997. return 0;
  2998. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2999. if (len >= buflen)
  3000. return -1;
  3001. return len;
  3002. }
  3003. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  3004. if (capa->enc & ciphers[i].capa) {
  3005. ret = os_snprintf(pos, end - pos, "%s%s",
  3006. pos == buf ? "" : " ",
  3007. ciphers[i].name);
  3008. if (os_snprintf_error(end - pos, ret))
  3009. return pos - buf;
  3010. pos += ret;
  3011. }
  3012. }
  3013. return pos - buf;
  3014. }
  3015. static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
  3016. struct wpa_driver_capa *capa,
  3017. char *buf, size_t buflen)
  3018. {
  3019. int ret;
  3020. char *pos, *end;
  3021. unsigned int i;
  3022. pos = buf;
  3023. end = pos + buflen;
  3024. if (res < 0)
  3025. return 0;
  3026. for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
  3027. if (capa->enc & ciphers_group_mgmt[i].capa) {
  3028. ret = os_snprintf(pos, end - pos, "%s%s",
  3029. pos == buf ? "" : " ",
  3030. ciphers_group_mgmt[i].name);
  3031. if (os_snprintf_error(end - pos, ret))
  3032. return pos - buf;
  3033. pos += ret;
  3034. }
  3035. }
  3036. return pos - buf;
  3037. }
  3038. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  3039. struct wpa_driver_capa *capa,
  3040. char *buf, size_t buflen)
  3041. {
  3042. int ret;
  3043. char *pos, *end;
  3044. size_t len;
  3045. pos = buf;
  3046. end = pos + buflen;
  3047. if (res < 0) {
  3048. if (strict)
  3049. return 0;
  3050. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  3051. "NONE", buflen);
  3052. if (len >= buflen)
  3053. return -1;
  3054. return len;
  3055. }
  3056. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  3057. if (os_snprintf_error(end - pos, ret))
  3058. return pos - buf;
  3059. pos += ret;
  3060. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3061. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  3062. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  3063. if (os_snprintf_error(end - pos, ret))
  3064. return pos - buf;
  3065. pos += ret;
  3066. }
  3067. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  3068. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3069. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  3070. if (os_snprintf_error(end - pos, ret))
  3071. return pos - buf;
  3072. pos += ret;
  3073. }
  3074. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  3075. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  3076. if (os_snprintf_error(end - pos, ret))
  3077. return pos - buf;
  3078. pos += ret;
  3079. }
  3080. #ifdef CONFIG_SUITEB
  3081. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
  3082. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
  3083. if (os_snprintf_error(end - pos, ret))
  3084. return pos - buf;
  3085. pos += ret;
  3086. }
  3087. #endif /* CONFIG_SUITEB */
  3088. #ifdef CONFIG_SUITEB192
  3089. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
  3090. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
  3091. if (os_snprintf_error(end - pos, ret))
  3092. return pos - buf;
  3093. pos += ret;
  3094. }
  3095. #endif /* CONFIG_SUITEB192 */
  3096. return pos - buf;
  3097. }
  3098. static int ctrl_iface_get_capability_proto(int res, char *strict,
  3099. struct wpa_driver_capa *capa,
  3100. char *buf, size_t buflen)
  3101. {
  3102. int ret;
  3103. char *pos, *end;
  3104. size_t len;
  3105. pos = buf;
  3106. end = pos + buflen;
  3107. if (res < 0) {
  3108. if (strict)
  3109. return 0;
  3110. len = os_strlcpy(buf, "RSN WPA", buflen);
  3111. if (len >= buflen)
  3112. return -1;
  3113. return len;
  3114. }
  3115. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  3116. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3117. ret = os_snprintf(pos, end - pos, "%sRSN",
  3118. pos == buf ? "" : " ");
  3119. if (os_snprintf_error(end - pos, ret))
  3120. return pos - buf;
  3121. pos += ret;
  3122. }
  3123. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3124. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  3125. ret = os_snprintf(pos, end - pos, "%sWPA",
  3126. pos == buf ? "" : " ");
  3127. if (os_snprintf_error(end - pos, ret))
  3128. return pos - buf;
  3129. pos += ret;
  3130. }
  3131. return pos - buf;
  3132. }
  3133. static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
  3134. int res, char *strict,
  3135. struct wpa_driver_capa *capa,
  3136. char *buf, size_t buflen)
  3137. {
  3138. int ret;
  3139. char *pos, *end;
  3140. size_t len;
  3141. pos = buf;
  3142. end = pos + buflen;
  3143. if (res < 0) {
  3144. if (strict)
  3145. return 0;
  3146. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  3147. if (len >= buflen)
  3148. return -1;
  3149. return len;
  3150. }
  3151. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  3152. ret = os_snprintf(pos, end - pos, "%sOPEN",
  3153. pos == buf ? "" : " ");
  3154. if (os_snprintf_error(end - pos, ret))
  3155. return pos - buf;
  3156. pos += ret;
  3157. }
  3158. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  3159. ret = os_snprintf(pos, end - pos, "%sSHARED",
  3160. pos == buf ? "" : " ");
  3161. if (os_snprintf_error(end - pos, ret))
  3162. return pos - buf;
  3163. pos += ret;
  3164. }
  3165. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  3166. ret = os_snprintf(pos, end - pos, "%sLEAP",
  3167. pos == buf ? "" : " ");
  3168. if (os_snprintf_error(end - pos, ret))
  3169. return pos - buf;
  3170. pos += ret;
  3171. }
  3172. #ifdef CONFIG_SAE
  3173. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
  3174. ret = os_snprintf(pos, end - pos, "%sSAE",
  3175. pos == buf ? "" : " ");
  3176. if (os_snprintf_error(end - pos, ret))
  3177. return pos - buf;
  3178. pos += ret;
  3179. }
  3180. #endif /* CONFIG_SAE */
  3181. return pos - buf;
  3182. }
  3183. static int ctrl_iface_get_capability_modes(int res, char *strict,
  3184. struct wpa_driver_capa *capa,
  3185. char *buf, size_t buflen)
  3186. {
  3187. int ret;
  3188. char *pos, *end;
  3189. size_t len;
  3190. pos = buf;
  3191. end = pos + buflen;
  3192. if (res < 0) {
  3193. if (strict)
  3194. return 0;
  3195. len = os_strlcpy(buf, "IBSS AP", buflen);
  3196. if (len >= buflen)
  3197. return -1;
  3198. return len;
  3199. }
  3200. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  3201. ret = os_snprintf(pos, end - pos, "%sIBSS",
  3202. pos == buf ? "" : " ");
  3203. if (os_snprintf_error(end - pos, ret))
  3204. return pos - buf;
  3205. pos += ret;
  3206. }
  3207. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  3208. ret = os_snprintf(pos, end - pos, "%sAP",
  3209. pos == buf ? "" : " ");
  3210. if (os_snprintf_error(end - pos, ret))
  3211. return pos - buf;
  3212. pos += ret;
  3213. }
  3214. #ifdef CONFIG_MESH
  3215. if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
  3216. ret = os_snprintf(pos, end - pos, "%sMESH",
  3217. pos == buf ? "" : " ");
  3218. if (os_snprintf_error(end - pos, ret))
  3219. return pos - buf;
  3220. pos += ret;
  3221. }
  3222. #endif /* CONFIG_MESH */
  3223. return pos - buf;
  3224. }
  3225. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  3226. char *buf, size_t buflen)
  3227. {
  3228. struct hostapd_channel_data *chnl;
  3229. int ret, i, j;
  3230. char *pos, *end, *hmode;
  3231. pos = buf;
  3232. end = pos + buflen;
  3233. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3234. switch (wpa_s->hw.modes[j].mode) {
  3235. case HOSTAPD_MODE_IEEE80211B:
  3236. hmode = "B";
  3237. break;
  3238. case HOSTAPD_MODE_IEEE80211G:
  3239. hmode = "G";
  3240. break;
  3241. case HOSTAPD_MODE_IEEE80211A:
  3242. hmode = "A";
  3243. break;
  3244. case HOSTAPD_MODE_IEEE80211AD:
  3245. hmode = "AD";
  3246. break;
  3247. default:
  3248. continue;
  3249. }
  3250. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  3251. if (os_snprintf_error(end - pos, ret))
  3252. return pos - buf;
  3253. pos += ret;
  3254. chnl = wpa_s->hw.modes[j].channels;
  3255. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3256. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3257. continue;
  3258. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  3259. if (os_snprintf_error(end - pos, ret))
  3260. return pos - buf;
  3261. pos += ret;
  3262. }
  3263. ret = os_snprintf(pos, end - pos, "\n");
  3264. if (os_snprintf_error(end - pos, ret))
  3265. return pos - buf;
  3266. pos += ret;
  3267. }
  3268. return pos - buf;
  3269. }
  3270. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  3271. char *buf, size_t buflen)
  3272. {
  3273. struct hostapd_channel_data *chnl;
  3274. int ret, i, j;
  3275. char *pos, *end, *hmode;
  3276. pos = buf;
  3277. end = pos + buflen;
  3278. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3279. switch (wpa_s->hw.modes[j].mode) {
  3280. case HOSTAPD_MODE_IEEE80211B:
  3281. hmode = "B";
  3282. break;
  3283. case HOSTAPD_MODE_IEEE80211G:
  3284. hmode = "G";
  3285. break;
  3286. case HOSTAPD_MODE_IEEE80211A:
  3287. hmode = "A";
  3288. break;
  3289. case HOSTAPD_MODE_IEEE80211AD:
  3290. hmode = "AD";
  3291. break;
  3292. default:
  3293. continue;
  3294. }
  3295. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  3296. hmode);
  3297. if (os_snprintf_error(end - pos, ret))
  3298. return pos - buf;
  3299. pos += ret;
  3300. chnl = wpa_s->hw.modes[j].channels;
  3301. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3302. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3303. continue;
  3304. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  3305. chnl[i].chan, chnl[i].freq,
  3306. chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
  3307. " (NO_IR)" : "",
  3308. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  3309. " (DFS)" : "");
  3310. if (os_snprintf_error(end - pos, ret))
  3311. return pos - buf;
  3312. pos += ret;
  3313. }
  3314. ret = os_snprintf(pos, end - pos, "\n");
  3315. if (os_snprintf_error(end - pos, ret))
  3316. return pos - buf;
  3317. pos += ret;
  3318. }
  3319. return pos - buf;
  3320. }
  3321. static int wpa_supplicant_ctrl_iface_get_capability(
  3322. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  3323. size_t buflen)
  3324. {
  3325. struct wpa_driver_capa capa;
  3326. int res;
  3327. char *strict;
  3328. char field[30];
  3329. size_t len;
  3330. /* Determine whether or not strict checking was requested */
  3331. len = os_strlcpy(field, _field, sizeof(field));
  3332. if (len >= sizeof(field))
  3333. return -1;
  3334. strict = os_strchr(field, ' ');
  3335. if (strict != NULL) {
  3336. *strict++ = '\0';
  3337. if (os_strcmp(strict, "strict") != 0)
  3338. return -1;
  3339. }
  3340. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  3341. field, strict ? strict : "");
  3342. if (os_strcmp(field, "eap") == 0) {
  3343. return eap_get_names(buf, buflen);
  3344. }
  3345. res = wpa_drv_get_capa(wpa_s, &capa);
  3346. if (os_strcmp(field, "pairwise") == 0)
  3347. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  3348. buf, buflen);
  3349. if (os_strcmp(field, "group") == 0)
  3350. return ctrl_iface_get_capability_group(res, strict, &capa,
  3351. buf, buflen);
  3352. if (os_strcmp(field, "group_mgmt") == 0)
  3353. return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
  3354. buf, buflen);
  3355. if (os_strcmp(field, "key_mgmt") == 0)
  3356. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  3357. buf, buflen);
  3358. if (os_strcmp(field, "proto") == 0)
  3359. return ctrl_iface_get_capability_proto(res, strict, &capa,
  3360. buf, buflen);
  3361. if (os_strcmp(field, "auth_alg") == 0)
  3362. return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
  3363. &capa, buf, buflen);
  3364. if (os_strcmp(field, "modes") == 0)
  3365. return ctrl_iface_get_capability_modes(res, strict, &capa,
  3366. buf, buflen);
  3367. if (os_strcmp(field, "channels") == 0)
  3368. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  3369. if (os_strcmp(field, "freq") == 0)
  3370. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  3371. #ifdef CONFIG_TDLS
  3372. if (os_strcmp(field, "tdls") == 0)
  3373. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  3374. #endif /* CONFIG_TDLS */
  3375. #ifdef CONFIG_ERP
  3376. if (os_strcmp(field, "erp") == 0) {
  3377. res = os_snprintf(buf, buflen, "ERP");
  3378. if (os_snprintf_error(buflen, res))
  3379. return -1;
  3380. return res;
  3381. }
  3382. #endif /* CONFIG_EPR */
  3383. #ifdef CONFIG_FIPS
  3384. if (os_strcmp(field, "fips") == 0) {
  3385. res = os_snprintf(buf, buflen, "FIPS");
  3386. if (os_snprintf_error(buflen, res))
  3387. return -1;
  3388. return res;
  3389. }
  3390. #endif /* CONFIG_FIPS */
  3391. #ifdef CONFIG_ACS
  3392. if (os_strcmp(field, "acs") == 0) {
  3393. res = os_snprintf(buf, buflen, "ACS");
  3394. if (os_snprintf_error(buflen, res))
  3395. return -1;
  3396. return res;
  3397. }
  3398. #endif /* CONFIG_ACS */
  3399. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  3400. field);
  3401. return -1;
  3402. }
  3403. #ifdef CONFIG_INTERWORKING
  3404. static char * anqp_add_hex(char *pos, char *end, const char *title,
  3405. struct wpabuf *data)
  3406. {
  3407. char *start = pos;
  3408. size_t i;
  3409. int ret;
  3410. const u8 *d;
  3411. if (data == NULL)
  3412. return start;
  3413. ret = os_snprintf(pos, end - pos, "%s=", title);
  3414. if (os_snprintf_error(end - pos, ret))
  3415. return start;
  3416. pos += ret;
  3417. d = wpabuf_head_u8(data);
  3418. for (i = 0; i < wpabuf_len(data); i++) {
  3419. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  3420. if (os_snprintf_error(end - pos, ret))
  3421. return start;
  3422. pos += ret;
  3423. }
  3424. ret = os_snprintf(pos, end - pos, "\n");
  3425. if (os_snprintf_error(end - pos, ret))
  3426. return start;
  3427. pos += ret;
  3428. return pos;
  3429. }
  3430. #endif /* CONFIG_INTERWORKING */
  3431. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  3432. unsigned long mask, char *buf, size_t buflen)
  3433. {
  3434. size_t i;
  3435. int ret;
  3436. char *pos, *end;
  3437. const u8 *ie, *ie2, *osen_ie;
  3438. pos = buf;
  3439. end = buf + buflen;
  3440. if (mask & WPA_BSS_MASK_ID) {
  3441. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  3442. if (os_snprintf_error(end - pos, ret))
  3443. return 0;
  3444. pos += ret;
  3445. }
  3446. if (mask & WPA_BSS_MASK_BSSID) {
  3447. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  3448. MAC2STR(bss->bssid));
  3449. if (os_snprintf_error(end - pos, ret))
  3450. return 0;
  3451. pos += ret;
  3452. }
  3453. if (mask & WPA_BSS_MASK_FREQ) {
  3454. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  3455. if (os_snprintf_error(end - pos, ret))
  3456. return 0;
  3457. pos += ret;
  3458. }
  3459. if (mask & WPA_BSS_MASK_BEACON_INT) {
  3460. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  3461. bss->beacon_int);
  3462. if (os_snprintf_error(end - pos, ret))
  3463. return 0;
  3464. pos += ret;
  3465. }
  3466. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  3467. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  3468. bss->caps);
  3469. if (os_snprintf_error(end - pos, ret))
  3470. return 0;
  3471. pos += ret;
  3472. }
  3473. if (mask & WPA_BSS_MASK_QUAL) {
  3474. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  3475. if (os_snprintf_error(end - pos, ret))
  3476. return 0;
  3477. pos += ret;
  3478. }
  3479. if (mask & WPA_BSS_MASK_NOISE) {
  3480. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  3481. if (os_snprintf_error(end - pos, ret))
  3482. return 0;
  3483. pos += ret;
  3484. }
  3485. if (mask & WPA_BSS_MASK_LEVEL) {
  3486. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  3487. if (os_snprintf_error(end - pos, ret))
  3488. return 0;
  3489. pos += ret;
  3490. }
  3491. if (mask & WPA_BSS_MASK_TSF) {
  3492. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  3493. (unsigned long long) bss->tsf);
  3494. if (os_snprintf_error(end - pos, ret))
  3495. return 0;
  3496. pos += ret;
  3497. }
  3498. if (mask & WPA_BSS_MASK_AGE) {
  3499. struct os_reltime now;
  3500. os_get_reltime(&now);
  3501. ret = os_snprintf(pos, end - pos, "age=%d\n",
  3502. (int) (now.sec - bss->last_update.sec));
  3503. if (os_snprintf_error(end - pos, ret))
  3504. return 0;
  3505. pos += ret;
  3506. }
  3507. if (mask & WPA_BSS_MASK_IE) {
  3508. ret = os_snprintf(pos, end - pos, "ie=");
  3509. if (os_snprintf_error(end - pos, ret))
  3510. return 0;
  3511. pos += ret;
  3512. ie = (const u8 *) (bss + 1);
  3513. for (i = 0; i < bss->ie_len; i++) {
  3514. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  3515. if (os_snprintf_error(end - pos, ret))
  3516. return 0;
  3517. pos += ret;
  3518. }
  3519. ret = os_snprintf(pos, end - pos, "\n");
  3520. if (os_snprintf_error(end - pos, ret))
  3521. return 0;
  3522. pos += ret;
  3523. }
  3524. if (mask & WPA_BSS_MASK_FLAGS) {
  3525. ret = os_snprintf(pos, end - pos, "flags=");
  3526. if (os_snprintf_error(end - pos, ret))
  3527. return 0;
  3528. pos += ret;
  3529. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3530. if (ie)
  3531. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3532. 2 + ie[1]);
  3533. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3534. if (ie2)
  3535. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  3536. 2 + ie2[1]);
  3537. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  3538. if (osen_ie)
  3539. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  3540. osen_ie, 2 + osen_ie[1]);
  3541. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3542. if (!ie && !ie2 && !osen_ie &&
  3543. (bss->caps & IEEE80211_CAP_PRIVACY)) {
  3544. ret = os_snprintf(pos, end - pos, "[WEP]");
  3545. if (os_snprintf_error(end - pos, ret))
  3546. return 0;
  3547. pos += ret;
  3548. }
  3549. if (bss_is_dmg(bss)) {
  3550. const char *s;
  3551. ret = os_snprintf(pos, end - pos, "[DMG]");
  3552. if (os_snprintf_error(end - pos, ret))
  3553. return 0;
  3554. pos += ret;
  3555. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3556. case IEEE80211_CAP_DMG_IBSS:
  3557. s = "[IBSS]";
  3558. break;
  3559. case IEEE80211_CAP_DMG_AP:
  3560. s = "[ESS]";
  3561. break;
  3562. case IEEE80211_CAP_DMG_PBSS:
  3563. s = "[PBSS]";
  3564. break;
  3565. default:
  3566. s = "";
  3567. break;
  3568. }
  3569. ret = os_snprintf(pos, end - pos, "%s", s);
  3570. if (os_snprintf_error(end - pos, ret))
  3571. return 0;
  3572. pos += ret;
  3573. } else {
  3574. if (bss->caps & IEEE80211_CAP_IBSS) {
  3575. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3576. if (os_snprintf_error(end - pos, ret))
  3577. return 0;
  3578. pos += ret;
  3579. }
  3580. if (bss->caps & IEEE80211_CAP_ESS) {
  3581. ret = os_snprintf(pos, end - pos, "[ESS]");
  3582. if (os_snprintf_error(end - pos, ret))
  3583. return 0;
  3584. pos += ret;
  3585. }
  3586. }
  3587. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  3588. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  3589. ret = os_snprintf(pos, end - pos, "[P2P]");
  3590. if (os_snprintf_error(end - pos, ret))
  3591. return 0;
  3592. pos += ret;
  3593. }
  3594. #ifdef CONFIG_HS20
  3595. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  3596. ret = os_snprintf(pos, end - pos, "[HS20]");
  3597. if (os_snprintf_error(end - pos, ret))
  3598. return 0;
  3599. pos += ret;
  3600. }
  3601. #endif /* CONFIG_HS20 */
  3602. ret = os_snprintf(pos, end - pos, "\n");
  3603. if (os_snprintf_error(end - pos, ret))
  3604. return 0;
  3605. pos += ret;
  3606. }
  3607. if (mask & WPA_BSS_MASK_SSID) {
  3608. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  3609. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3610. if (os_snprintf_error(end - pos, ret))
  3611. return 0;
  3612. pos += ret;
  3613. }
  3614. #ifdef CONFIG_WPS
  3615. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  3616. ie = (const u8 *) (bss + 1);
  3617. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  3618. if (ret >= end - pos)
  3619. return 0;
  3620. if (ret > 0)
  3621. pos += ret;
  3622. }
  3623. #endif /* CONFIG_WPS */
  3624. #ifdef CONFIG_P2P
  3625. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  3626. ie = (const u8 *) (bss + 1);
  3627. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  3628. if (ret >= end - pos)
  3629. return 0;
  3630. if (ret > 0)
  3631. pos += ret;
  3632. }
  3633. #endif /* CONFIG_P2P */
  3634. #ifdef CONFIG_WIFI_DISPLAY
  3635. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  3636. struct wpabuf *wfd;
  3637. ie = (const u8 *) (bss + 1);
  3638. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  3639. WFD_IE_VENDOR_TYPE);
  3640. if (wfd) {
  3641. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  3642. if (os_snprintf_error(end - pos, ret)) {
  3643. wpabuf_free(wfd);
  3644. return 0;
  3645. }
  3646. pos += ret;
  3647. pos += wpa_snprintf_hex(pos, end - pos,
  3648. wpabuf_head(wfd),
  3649. wpabuf_len(wfd));
  3650. wpabuf_free(wfd);
  3651. ret = os_snprintf(pos, end - pos, "\n");
  3652. if (os_snprintf_error(end - pos, ret))
  3653. return 0;
  3654. pos += ret;
  3655. }
  3656. }
  3657. #endif /* CONFIG_WIFI_DISPLAY */
  3658. #ifdef CONFIG_INTERWORKING
  3659. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3660. struct wpa_bss_anqp *anqp = bss->anqp;
  3661. struct wpa_bss_anqp_elem *elem;
  3662. pos = anqp_add_hex(pos, end, "anqp_capability_list",
  3663. anqp->capability_list);
  3664. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3665. anqp->venue_name);
  3666. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3667. anqp->network_auth_type);
  3668. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3669. anqp->roaming_consortium);
  3670. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3671. anqp->ip_addr_type_availability);
  3672. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3673. anqp->nai_realm);
  3674. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3675. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3676. anqp->domain_name);
  3677. #ifdef CONFIG_HS20
  3678. pos = anqp_add_hex(pos, end, "hs20_capability_list",
  3679. anqp->hs20_capability_list);
  3680. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3681. anqp->hs20_operator_friendly_name);
  3682. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3683. anqp->hs20_wan_metrics);
  3684. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3685. anqp->hs20_connection_capability);
  3686. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3687. anqp->hs20_operating_class);
  3688. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3689. anqp->hs20_osu_providers_list);
  3690. #endif /* CONFIG_HS20 */
  3691. dl_list_for_each(elem, &anqp->anqp_elems,
  3692. struct wpa_bss_anqp_elem, list) {
  3693. char title[20];
  3694. os_snprintf(title, sizeof(title), "anqp[%u]",
  3695. elem->infoid);
  3696. pos = anqp_add_hex(pos, end, title, elem->payload);
  3697. }
  3698. }
  3699. #endif /* CONFIG_INTERWORKING */
  3700. #ifdef CONFIG_MESH
  3701. if (mask & WPA_BSS_MASK_MESH_SCAN) {
  3702. ie = (const u8 *) (bss + 1);
  3703. ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
  3704. if (ret >= end - pos)
  3705. return 0;
  3706. if (ret > 0)
  3707. pos += ret;
  3708. }
  3709. #endif /* CONFIG_MESH */
  3710. if (mask & WPA_BSS_MASK_SNR) {
  3711. ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
  3712. if (os_snprintf_error(end - pos, ret))
  3713. return 0;
  3714. pos += ret;
  3715. }
  3716. if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
  3717. ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
  3718. bss->est_throughput);
  3719. if (os_snprintf_error(end - pos, ret))
  3720. return 0;
  3721. pos += ret;
  3722. }
  3723. #ifdef CONFIG_FST
  3724. if (mask & WPA_BSS_MASK_FST) {
  3725. ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
  3726. if (ret < 0 || ret >= end - pos)
  3727. return 0;
  3728. pos += ret;
  3729. }
  3730. #endif /* CONFIG_FST */
  3731. if (mask & WPA_BSS_MASK_DELIM) {
  3732. ret = os_snprintf(pos, end - pos, "====\n");
  3733. if (os_snprintf_error(end - pos, ret))
  3734. return 0;
  3735. pos += ret;
  3736. }
  3737. return pos - buf;
  3738. }
  3739. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  3740. const char *cmd, char *buf,
  3741. size_t buflen)
  3742. {
  3743. u8 bssid[ETH_ALEN];
  3744. size_t i;
  3745. struct wpa_bss *bss;
  3746. struct wpa_bss *bsslast = NULL;
  3747. struct dl_list *next;
  3748. int ret = 0;
  3749. int len;
  3750. char *ctmp, *end = buf + buflen;
  3751. unsigned long mask = WPA_BSS_MASK_ALL;
  3752. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  3753. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  3754. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  3755. list_id);
  3756. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  3757. list_id);
  3758. } else { /* N1-N2 */
  3759. unsigned int id1, id2;
  3760. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  3761. wpa_printf(MSG_INFO, "Wrong BSS range "
  3762. "format");
  3763. return 0;
  3764. }
  3765. if (*(cmd + 6) == '-')
  3766. id1 = 0;
  3767. else
  3768. id1 = atoi(cmd + 6);
  3769. ctmp++;
  3770. if (*ctmp >= '0' && *ctmp <= '9')
  3771. id2 = atoi(ctmp);
  3772. else
  3773. id2 = (unsigned int) -1;
  3774. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  3775. if (id2 == (unsigned int) -1)
  3776. bsslast = dl_list_last(&wpa_s->bss_id,
  3777. struct wpa_bss,
  3778. list_id);
  3779. else {
  3780. bsslast = wpa_bss_get_id(wpa_s, id2);
  3781. if (bsslast == NULL && bss && id2 > id1) {
  3782. struct wpa_bss *tmp = bss;
  3783. for (;;) {
  3784. next = tmp->list_id.next;
  3785. if (next == &wpa_s->bss_id)
  3786. break;
  3787. tmp = dl_list_entry(
  3788. next, struct wpa_bss,
  3789. list_id);
  3790. if (tmp->id > id2)
  3791. break;
  3792. bsslast = tmp;
  3793. }
  3794. }
  3795. }
  3796. }
  3797. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  3798. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  3799. else if (os_strncmp(cmd, "LAST", 4) == 0)
  3800. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  3801. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  3802. i = atoi(cmd + 3);
  3803. bss = wpa_bss_get_id(wpa_s, i);
  3804. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3805. i = atoi(cmd + 5);
  3806. bss = wpa_bss_get_id(wpa_s, i);
  3807. if (bss) {
  3808. next = bss->list_id.next;
  3809. if (next == &wpa_s->bss_id)
  3810. bss = NULL;
  3811. else
  3812. bss = dl_list_entry(next, struct wpa_bss,
  3813. list_id);
  3814. }
  3815. #ifdef CONFIG_P2P
  3816. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  3817. if (hwaddr_aton(cmd + 13, bssid) == 0)
  3818. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  3819. else
  3820. bss = NULL;
  3821. #endif /* CONFIG_P2P */
  3822. } else if (hwaddr_aton(cmd, bssid) == 0)
  3823. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3824. else {
  3825. struct wpa_bss *tmp;
  3826. i = atoi(cmd);
  3827. bss = NULL;
  3828. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  3829. {
  3830. if (i-- == 0) {
  3831. bss = tmp;
  3832. break;
  3833. }
  3834. }
  3835. }
  3836. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  3837. mask = strtoul(ctmp + 5, NULL, 0x10);
  3838. if (mask == 0)
  3839. mask = WPA_BSS_MASK_ALL;
  3840. }
  3841. if (bss == NULL)
  3842. return 0;
  3843. if (bsslast == NULL)
  3844. bsslast = bss;
  3845. do {
  3846. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3847. ret += len;
  3848. buf += len;
  3849. buflen -= len;
  3850. if (bss == bsslast) {
  3851. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3852. (bss == dl_list_last(&wpa_s->bss_id,
  3853. struct wpa_bss, list_id))) {
  3854. int res;
  3855. res = os_snprintf(buf - 5, end - buf + 5,
  3856. "####\n");
  3857. if (os_snprintf_error(end - buf + 5, res)) {
  3858. wpa_printf(MSG_DEBUG,
  3859. "Could not add end delim");
  3860. }
  3861. }
  3862. break;
  3863. }
  3864. next = bss->list_id.next;
  3865. if (next == &wpa_s->bss_id)
  3866. break;
  3867. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3868. } while (bss && len);
  3869. return ret;
  3870. }
  3871. static int wpa_supplicant_ctrl_iface_ap_scan(
  3872. struct wpa_supplicant *wpa_s, char *cmd)
  3873. {
  3874. int ap_scan = atoi(cmd);
  3875. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3876. }
  3877. static int wpa_supplicant_ctrl_iface_scan_interval(
  3878. struct wpa_supplicant *wpa_s, char *cmd)
  3879. {
  3880. int scan_int = atoi(cmd);
  3881. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3882. }
  3883. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3884. struct wpa_supplicant *wpa_s, char *cmd)
  3885. {
  3886. int expire_age = atoi(cmd);
  3887. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3888. }
  3889. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3890. struct wpa_supplicant *wpa_s, char *cmd)
  3891. {
  3892. int expire_count = atoi(cmd);
  3893. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3894. }
  3895. static void wpa_supplicant_ctrl_iface_bss_flush(
  3896. struct wpa_supplicant *wpa_s, char *cmd)
  3897. {
  3898. int flush_age = atoi(cmd);
  3899. if (flush_age == 0)
  3900. wpa_bss_flush(wpa_s);
  3901. else
  3902. wpa_bss_flush_by_age(wpa_s, flush_age);
  3903. }
  3904. #ifdef CONFIG_TESTING_OPTIONS
  3905. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3906. {
  3907. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3908. /* MLME-DELETEKEYS.request */
  3909. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3910. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3911. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3912. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3913. #ifdef CONFIG_IEEE80211W
  3914. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3915. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3916. #endif /* CONFIG_IEEE80211W */
  3917. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3918. 0);
  3919. /* MLME-SETPROTECTION.request(None) */
  3920. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3921. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3922. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3923. wpa_sm_drop_sa(wpa_s->wpa);
  3924. }
  3925. #endif /* CONFIG_TESTING_OPTIONS */
  3926. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3927. char *addr)
  3928. {
  3929. #ifdef CONFIG_NO_SCAN_PROCESSING
  3930. return -1;
  3931. #else /* CONFIG_NO_SCAN_PROCESSING */
  3932. u8 bssid[ETH_ALEN];
  3933. struct wpa_bss *bss;
  3934. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3935. if (hwaddr_aton(addr, bssid)) {
  3936. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3937. "address '%s'", addr);
  3938. return -1;
  3939. }
  3940. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3941. if (!ssid) {
  3942. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3943. "configuration known for the target AP");
  3944. return -1;
  3945. }
  3946. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3947. if (!bss) {
  3948. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3949. "from BSS table");
  3950. return -1;
  3951. }
  3952. /*
  3953. * TODO: Find best network configuration block from configuration to
  3954. * allow roaming to other networks
  3955. */
  3956. wpa_s->reassociate = 1;
  3957. wpa_supplicant_connect(wpa_s, bss, ssid);
  3958. return 0;
  3959. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3960. }
  3961. #ifdef CONFIG_P2P
  3962. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3963. {
  3964. unsigned int timeout = atoi(cmd);
  3965. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3966. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3967. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  3968. char *pos;
  3969. unsigned int search_delay;
  3970. const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
  3971. u8 seek_count = 0;
  3972. int freq = 0;
  3973. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3974. wpa_dbg(wpa_s, MSG_INFO,
  3975. "Reject P2P_FIND since interface is disabled");
  3976. return -1;
  3977. }
  3978. if (os_strstr(cmd, "type=social"))
  3979. type = P2P_FIND_ONLY_SOCIAL;
  3980. else if (os_strstr(cmd, "type=progressive"))
  3981. type = P2P_FIND_PROGRESSIVE;
  3982. pos = os_strstr(cmd, "dev_id=");
  3983. if (pos) {
  3984. pos += 7;
  3985. if (hwaddr_aton(pos, dev_id))
  3986. return -1;
  3987. _dev_id = dev_id;
  3988. }
  3989. pos = os_strstr(cmd, "dev_type=");
  3990. if (pos) {
  3991. pos += 9;
  3992. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  3993. return -1;
  3994. _dev_type = dev_type;
  3995. }
  3996. pos = os_strstr(cmd, "delay=");
  3997. if (pos) {
  3998. pos += 6;
  3999. search_delay = atoi(pos);
  4000. } else
  4001. search_delay = wpas_p2p_search_delay(wpa_s);
  4002. pos = os_strstr(cmd, "freq=");
  4003. if (pos) {
  4004. pos += 5;
  4005. freq = atoi(pos);
  4006. if (freq <= 0)
  4007. return -1;
  4008. }
  4009. /* Must be searched for last, because it adds nul termination */
  4010. pos = os_strstr(cmd, " seek=");
  4011. if (pos)
  4012. pos += 6;
  4013. while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
  4014. char *term;
  4015. _seek[seek_count++] = pos;
  4016. seek = _seek;
  4017. term = os_strchr(pos, ' ');
  4018. if (!term)
  4019. break;
  4020. *term = '\0';
  4021. pos = os_strstr(term + 1, "seek=");
  4022. if (pos)
  4023. pos += 5;
  4024. }
  4025. if (seek_count > P2P_MAX_QUERY_HASH) {
  4026. seek[0] = NULL;
  4027. seek_count = 1;
  4028. }
  4029. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  4030. _dev_id, search_delay, seek_count, seek, freq);
  4031. }
  4032. static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
  4033. {
  4034. const char *last = NULL;
  4035. const char *token;
  4036. long int token_len;
  4037. unsigned int i;
  4038. /* Expected predefined CPT names delimited by ':' */
  4039. for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
  4040. if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
  4041. wpa_printf(MSG_ERROR,
  4042. "P2PS: CPT name list is too long, expected up to %d names",
  4043. P2PS_FEATURE_CAPAB_CPT_MAX);
  4044. cpt[0] = 0;
  4045. return -1;
  4046. }
  4047. token_len = last - token;
  4048. if (token_len == 3 &&
  4049. os_memcmp(token, "UDP", token_len) == 0) {
  4050. cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4051. } else if (token_len == 3 &&
  4052. os_memcmp(token, "MAC", token_len) == 0) {
  4053. cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
  4054. } else {
  4055. wpa_printf(MSG_ERROR,
  4056. "P2PS: Unsupported CPT name '%s'", token);
  4057. cpt[0] = 0;
  4058. return -1;
  4059. }
  4060. if (isblank((unsigned char) *last)) {
  4061. i++;
  4062. break;
  4063. }
  4064. }
  4065. cpt[i] = 0;
  4066. return 0;
  4067. }
  4068. static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
  4069. {
  4070. struct p2ps_provision *p2ps_prov;
  4071. char *pos;
  4072. size_t info_len = 0;
  4073. char *info = NULL;
  4074. u8 role = P2PS_SETUP_NONE;
  4075. long long unsigned val;
  4076. int i;
  4077. pos = os_strstr(cmd, "info=");
  4078. if (pos) {
  4079. pos += 5;
  4080. info_len = os_strlen(pos);
  4081. if (info_len) {
  4082. info = os_malloc(info_len + 1);
  4083. if (info) {
  4084. info_len = utf8_unescape(pos, info_len,
  4085. info, info_len + 1);
  4086. } else
  4087. info_len = 0;
  4088. }
  4089. }
  4090. p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
  4091. if (p2ps_prov == NULL) {
  4092. os_free(info);
  4093. return NULL;
  4094. }
  4095. if (info) {
  4096. os_memcpy(p2ps_prov->info, info, info_len);
  4097. p2ps_prov->info[info_len] = '\0';
  4098. os_free(info);
  4099. }
  4100. pos = os_strstr(cmd, "status=");
  4101. if (pos)
  4102. p2ps_prov->status = atoi(pos + 7);
  4103. else
  4104. p2ps_prov->status = -1;
  4105. pos = os_strstr(cmd, "adv_id=");
  4106. if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
  4107. goto invalid_args;
  4108. p2ps_prov->adv_id = val;
  4109. pos = os_strstr(cmd, "method=");
  4110. if (pos)
  4111. p2ps_prov->method = strtol(pos + 7, NULL, 16);
  4112. else
  4113. p2ps_prov->method = 0;
  4114. pos = os_strstr(cmd, "session=");
  4115. if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
  4116. goto invalid_args;
  4117. p2ps_prov->session_id = val;
  4118. pos = os_strstr(cmd, "adv_mac=");
  4119. if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
  4120. goto invalid_args;
  4121. pos = os_strstr(cmd, "session_mac=");
  4122. if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
  4123. goto invalid_args;
  4124. pos = os_strstr(cmd, "cpt=");
  4125. if (pos) {
  4126. if (p2ps_ctrl_parse_cpt_priority(pos + 4,
  4127. p2ps_prov->cpt_priority))
  4128. goto invalid_args;
  4129. } else {
  4130. p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4131. }
  4132. for (i = 0; p2ps_prov->cpt_priority[i]; i++)
  4133. p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
  4134. /* force conncap with tstCap (no sanity checks) */
  4135. pos = os_strstr(cmd, "tstCap=");
  4136. if (pos) {
  4137. role = strtol(pos + 7, NULL, 16);
  4138. } else {
  4139. pos = os_strstr(cmd, "role=");
  4140. if (pos) {
  4141. role = strtol(pos + 5, NULL, 16);
  4142. if (role != P2PS_SETUP_CLIENT &&
  4143. role != P2PS_SETUP_GROUP_OWNER)
  4144. role = P2PS_SETUP_NONE;
  4145. }
  4146. }
  4147. p2ps_prov->role = role;
  4148. return p2ps_prov;
  4149. invalid_args:
  4150. os_free(p2ps_prov);
  4151. return NULL;
  4152. }
  4153. static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4154. {
  4155. u8 addr[ETH_ALEN];
  4156. struct p2ps_provision *p2ps_prov;
  4157. char *pos;
  4158. /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
  4159. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4160. if (hwaddr_aton(cmd, addr))
  4161. return -1;
  4162. pos = cmd + 17;
  4163. if (*pos != ' ')
  4164. return -1;
  4165. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4166. if (!p2ps_prov)
  4167. return -1;
  4168. if (p2ps_prov->status < 0) {
  4169. os_free(p2ps_prov);
  4170. return -1;
  4171. }
  4172. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4173. p2ps_prov);
  4174. }
  4175. static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
  4176. {
  4177. u8 addr[ETH_ALEN];
  4178. struct p2ps_provision *p2ps_prov;
  4179. char *pos;
  4180. /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
  4181. * session=<ses_id> mac=<ses_mac> [info=<infodata>]
  4182. */
  4183. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4184. if (hwaddr_aton(cmd, addr))
  4185. return -1;
  4186. pos = cmd + 17;
  4187. if (*pos != ' ')
  4188. return -1;
  4189. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4190. if (!p2ps_prov)
  4191. return -1;
  4192. p2ps_prov->pd_seeker = 1;
  4193. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4194. p2ps_prov);
  4195. }
  4196. static int parse_freq(int chwidth, int freq2)
  4197. {
  4198. if (freq2 < 0)
  4199. return -1;
  4200. if (freq2)
  4201. return VHT_CHANWIDTH_80P80MHZ;
  4202. switch (chwidth) {
  4203. case 0:
  4204. case 20:
  4205. case 40:
  4206. return VHT_CHANWIDTH_USE_HT;
  4207. case 80:
  4208. return VHT_CHANWIDTH_80MHZ;
  4209. case 160:
  4210. return VHT_CHANWIDTH_160MHZ;
  4211. default:
  4212. wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
  4213. chwidth);
  4214. return -1;
  4215. }
  4216. }
  4217. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  4218. char *buf, size_t buflen)
  4219. {
  4220. u8 addr[ETH_ALEN];
  4221. char *pos, *pos2;
  4222. char *pin = NULL;
  4223. enum p2p_wps_method wps_method;
  4224. int new_pin;
  4225. int ret;
  4226. int persistent_group, persistent_id = -1;
  4227. int join;
  4228. int auth;
  4229. int automatic;
  4230. int go_intent = -1;
  4231. int freq = 0;
  4232. int pd;
  4233. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  4234. u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
  4235. size_t group_ssid_len = 0;
  4236. if (!wpa_s->global->p2p_init_wpa_s)
  4237. return -1;
  4238. if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
  4239. wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
  4240. wpa_s->global->p2p_init_wpa_s->ifname);
  4241. wpa_s = wpa_s->global->p2p_init_wpa_s;
  4242. }
  4243. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
  4244. * [persistent|persistent=<network id>]
  4245. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  4246. * [ht40] [vht] [auto] [ssid=<hexdump>] */
  4247. if (hwaddr_aton(cmd, addr))
  4248. return -1;
  4249. pos = cmd + 17;
  4250. if (*pos != ' ')
  4251. return -1;
  4252. pos++;
  4253. persistent_group = os_strstr(pos, " persistent") != NULL;
  4254. pos2 = os_strstr(pos, " persistent=");
  4255. if (pos2) {
  4256. struct wpa_ssid *ssid;
  4257. persistent_id = atoi(pos2 + 12);
  4258. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4259. if (ssid == NULL || ssid->disabled != 2 ||
  4260. ssid->mode != WPAS_MODE_P2P_GO) {
  4261. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  4262. "SSID id=%d for persistent P2P group (GO)",
  4263. persistent_id);
  4264. return -1;
  4265. }
  4266. }
  4267. join = os_strstr(pos, " join") != NULL;
  4268. auth = os_strstr(pos, " auth") != NULL;
  4269. automatic = os_strstr(pos, " auto") != NULL;
  4270. pd = os_strstr(pos, " provdisc") != NULL;
  4271. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4272. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4273. vht;
  4274. pos2 = os_strstr(pos, " go_intent=");
  4275. if (pos2) {
  4276. pos2 += 11;
  4277. go_intent = atoi(pos2);
  4278. if (go_intent < 0 || go_intent > 15)
  4279. return -1;
  4280. }
  4281. pos2 = os_strstr(pos, " freq=");
  4282. if (pos2) {
  4283. pos2 += 6;
  4284. freq = atoi(pos2);
  4285. if (freq <= 0)
  4286. return -1;
  4287. }
  4288. pos2 = os_strstr(pos, " freq2=");
  4289. if (pos2)
  4290. freq2 = atoi(pos2 + 7);
  4291. pos2 = os_strstr(pos, " max_oper_chwidth=");
  4292. if (pos2)
  4293. chwidth = atoi(pos2 + 18);
  4294. max_oper_chwidth = parse_freq(chwidth, freq2);
  4295. if (max_oper_chwidth < 0)
  4296. return -1;
  4297. pos2 = os_strstr(pos, " ssid=");
  4298. if (pos2) {
  4299. char *end;
  4300. pos2 += 6;
  4301. end = os_strchr(pos2, ' ');
  4302. if (!end)
  4303. group_ssid_len = os_strlen(pos2) / 2;
  4304. else
  4305. group_ssid_len = (end - pos2) / 2;
  4306. if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
  4307. hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
  4308. return -1;
  4309. group_ssid = _group_ssid;
  4310. }
  4311. if (os_strncmp(pos, "pin", 3) == 0) {
  4312. /* Request random PIN (to be displayed) and enable the PIN */
  4313. wps_method = WPS_PIN_DISPLAY;
  4314. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  4315. wps_method = WPS_PBC;
  4316. } else if (os_strstr(pos, "p2ps") != NULL) {
  4317. wps_method = WPS_P2PS;
  4318. } else {
  4319. pin = pos;
  4320. pos = os_strchr(pin, ' ');
  4321. wps_method = WPS_PIN_KEYPAD;
  4322. if (pos) {
  4323. *pos++ = '\0';
  4324. if (os_strncmp(pos, "display", 7) == 0)
  4325. wps_method = WPS_PIN_DISPLAY;
  4326. }
  4327. if (!wps_pin_str_valid(pin)) {
  4328. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  4329. return 17;
  4330. }
  4331. }
  4332. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  4333. persistent_group, automatic, join,
  4334. auth, go_intent, freq, freq2, persistent_id,
  4335. pd, ht40, vht, max_oper_chwidth,
  4336. group_ssid, group_ssid_len);
  4337. if (new_pin == -2) {
  4338. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  4339. return 25;
  4340. }
  4341. if (new_pin == -3) {
  4342. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  4343. return 25;
  4344. }
  4345. if (new_pin < 0)
  4346. return -1;
  4347. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  4348. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  4349. if (os_snprintf_error(buflen, ret))
  4350. return -1;
  4351. return ret;
  4352. }
  4353. os_memcpy(buf, "OK\n", 3);
  4354. return 3;
  4355. }
  4356. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4357. {
  4358. unsigned int timeout = atoi(cmd);
  4359. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4360. wpa_dbg(wpa_s, MSG_INFO,
  4361. "Reject P2P_LISTEN since interface is disabled");
  4362. return -1;
  4363. }
  4364. return wpas_p2p_listen(wpa_s, timeout);
  4365. }
  4366. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  4367. {
  4368. u8 addr[ETH_ALEN];
  4369. char *pos;
  4370. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  4371. /* <addr> <config method> [join|auto] */
  4372. if (hwaddr_aton(cmd, addr))
  4373. return -1;
  4374. pos = cmd + 17;
  4375. if (*pos != ' ')
  4376. return -1;
  4377. pos++;
  4378. if (os_strstr(pos, " join") != NULL)
  4379. use = WPAS_P2P_PD_FOR_JOIN;
  4380. else if (os_strstr(pos, " auto") != NULL)
  4381. use = WPAS_P2P_PD_AUTO;
  4382. return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
  4383. }
  4384. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  4385. size_t buflen)
  4386. {
  4387. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4388. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  4389. ssid->passphrase == NULL)
  4390. return -1;
  4391. os_strlcpy(buf, ssid->passphrase, buflen);
  4392. return os_strlen(buf);
  4393. }
  4394. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  4395. char *buf, size_t buflen)
  4396. {
  4397. u64 ref;
  4398. int res;
  4399. u8 dst_buf[ETH_ALEN], *dst;
  4400. struct wpabuf *tlvs;
  4401. char *pos;
  4402. size_t len;
  4403. if (hwaddr_aton(cmd, dst_buf))
  4404. return -1;
  4405. dst = dst_buf;
  4406. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  4407. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  4408. dst = NULL;
  4409. pos = cmd + 17;
  4410. if (*pos != ' ')
  4411. return -1;
  4412. pos++;
  4413. if (os_strncmp(pos, "upnp ", 5) == 0) {
  4414. u8 version;
  4415. pos += 5;
  4416. if (hexstr2bin(pos, &version, 1) < 0)
  4417. return -1;
  4418. pos += 2;
  4419. if (*pos != ' ')
  4420. return -1;
  4421. pos++;
  4422. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  4423. #ifdef CONFIG_WIFI_DISPLAY
  4424. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  4425. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  4426. #endif /* CONFIG_WIFI_DISPLAY */
  4427. } else if (os_strncmp(pos, "asp ", 4) == 0) {
  4428. char *svc_str;
  4429. char *svc_info = NULL;
  4430. u32 id;
  4431. pos += 4;
  4432. if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
  4433. return -1;
  4434. pos = os_strchr(pos, ' ');
  4435. if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
  4436. return -1;
  4437. svc_str = pos + 1;
  4438. pos = os_strchr(svc_str, ' ');
  4439. if (pos)
  4440. *pos++ = '\0';
  4441. /* All remaining data is the svc_info string */
  4442. if (pos && pos[0] && pos[0] != ' ') {
  4443. len = os_strlen(pos);
  4444. /* Unescape in place */
  4445. len = utf8_unescape(pos, len, pos, len);
  4446. if (len > 0xff)
  4447. return -1;
  4448. svc_info = pos;
  4449. }
  4450. ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
  4451. svc_str, svc_info);
  4452. } else {
  4453. len = os_strlen(pos);
  4454. if (len & 1)
  4455. return -1;
  4456. len /= 2;
  4457. tlvs = wpabuf_alloc(len);
  4458. if (tlvs == NULL)
  4459. return -1;
  4460. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  4461. wpabuf_free(tlvs);
  4462. return -1;
  4463. }
  4464. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  4465. wpabuf_free(tlvs);
  4466. }
  4467. if (ref == 0)
  4468. return -1;
  4469. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  4470. if (os_snprintf_error(buflen, res))
  4471. return -1;
  4472. return res;
  4473. }
  4474. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  4475. char *cmd)
  4476. {
  4477. long long unsigned val;
  4478. u64 req;
  4479. if (sscanf(cmd, "%llx", &val) != 1)
  4480. return -1;
  4481. req = val;
  4482. return wpas_p2p_sd_cancel_request(wpa_s, req);
  4483. }
  4484. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4485. {
  4486. int freq;
  4487. u8 dst[ETH_ALEN];
  4488. u8 dialog_token;
  4489. struct wpabuf *resp_tlvs;
  4490. char *pos, *pos2;
  4491. size_t len;
  4492. pos = os_strchr(cmd, ' ');
  4493. if (pos == NULL)
  4494. return -1;
  4495. *pos++ = '\0';
  4496. freq = atoi(cmd);
  4497. if (freq == 0)
  4498. return -1;
  4499. if (hwaddr_aton(pos, dst))
  4500. return -1;
  4501. pos += 17;
  4502. if (*pos != ' ')
  4503. return -1;
  4504. pos++;
  4505. pos2 = os_strchr(pos, ' ');
  4506. if (pos2 == NULL)
  4507. return -1;
  4508. *pos2++ = '\0';
  4509. dialog_token = atoi(pos);
  4510. len = os_strlen(pos2);
  4511. if (len & 1)
  4512. return -1;
  4513. len /= 2;
  4514. resp_tlvs = wpabuf_alloc(len);
  4515. if (resp_tlvs == NULL)
  4516. return -1;
  4517. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  4518. wpabuf_free(resp_tlvs);
  4519. return -1;
  4520. }
  4521. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  4522. wpabuf_free(resp_tlvs);
  4523. return 0;
  4524. }
  4525. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  4526. char *cmd)
  4527. {
  4528. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  4529. return -1;
  4530. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  4531. return 0;
  4532. }
  4533. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  4534. char *cmd)
  4535. {
  4536. char *pos;
  4537. size_t len;
  4538. struct wpabuf *query, *resp;
  4539. pos = os_strchr(cmd, ' ');
  4540. if (pos == NULL)
  4541. return -1;
  4542. *pos++ = '\0';
  4543. len = os_strlen(cmd);
  4544. if (len & 1)
  4545. return -1;
  4546. len /= 2;
  4547. query = wpabuf_alloc(len);
  4548. if (query == NULL)
  4549. return -1;
  4550. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  4551. wpabuf_free(query);
  4552. return -1;
  4553. }
  4554. len = os_strlen(pos);
  4555. if (len & 1) {
  4556. wpabuf_free(query);
  4557. return -1;
  4558. }
  4559. len /= 2;
  4560. resp = wpabuf_alloc(len);
  4561. if (resp == NULL) {
  4562. wpabuf_free(query);
  4563. return -1;
  4564. }
  4565. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  4566. wpabuf_free(query);
  4567. wpabuf_free(resp);
  4568. return -1;
  4569. }
  4570. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  4571. wpabuf_free(query);
  4572. wpabuf_free(resp);
  4573. return -1;
  4574. }
  4575. return 0;
  4576. }
  4577. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  4578. {
  4579. char *pos;
  4580. u8 version;
  4581. pos = os_strchr(cmd, ' ');
  4582. if (pos == NULL)
  4583. return -1;
  4584. *pos++ = '\0';
  4585. if (hexstr2bin(cmd, &version, 1) < 0)
  4586. return -1;
  4587. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  4588. }
  4589. static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
  4590. u8 replace, char *cmd)
  4591. {
  4592. char *pos;
  4593. char *adv_str;
  4594. u32 auto_accept, adv_id, svc_state, config_methods;
  4595. char *svc_info = NULL;
  4596. char *cpt_prio_str;
  4597. u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
  4598. pos = os_strchr(cmd, ' ');
  4599. if (pos == NULL)
  4600. return -1;
  4601. *pos++ = '\0';
  4602. /* Auto-Accept value is mandatory, and must be one of the
  4603. * single values (0, 1, 2, 4) */
  4604. auto_accept = atoi(cmd);
  4605. switch (auto_accept) {
  4606. case P2PS_SETUP_NONE: /* No auto-accept */
  4607. case P2PS_SETUP_NEW:
  4608. case P2PS_SETUP_CLIENT:
  4609. case P2PS_SETUP_GROUP_OWNER:
  4610. break;
  4611. default:
  4612. return -1;
  4613. }
  4614. /* Advertisement ID is mandatory */
  4615. cmd = pos;
  4616. pos = os_strchr(cmd, ' ');
  4617. if (pos == NULL)
  4618. return -1;
  4619. *pos++ = '\0';
  4620. /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
  4621. if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
  4622. return -1;
  4623. /* Only allow replacements if exist, and adds if not */
  4624. if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
  4625. if (!replace)
  4626. return -1;
  4627. } else {
  4628. if (replace)
  4629. return -1;
  4630. }
  4631. /* svc_state between 0 - 0xff is mandatory */
  4632. if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
  4633. return -1;
  4634. pos = os_strchr(pos, ' ');
  4635. if (pos == NULL)
  4636. return -1;
  4637. /* config_methods is mandatory */
  4638. pos++;
  4639. if (sscanf(pos, "%x", &config_methods) != 1)
  4640. return -1;
  4641. if (!(config_methods &
  4642. (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
  4643. return -1;
  4644. pos = os_strchr(pos, ' ');
  4645. if (pos == NULL)
  4646. return -1;
  4647. pos++;
  4648. adv_str = pos;
  4649. /* Advertisement string is mandatory */
  4650. if (!pos[0] || pos[0] == ' ')
  4651. return -1;
  4652. /* Terminate svc string */
  4653. pos = os_strchr(pos, ' ');
  4654. if (pos != NULL)
  4655. *pos++ = '\0';
  4656. cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
  4657. if (cpt_prio_str) {
  4658. pos = os_strchr(pos, ' ');
  4659. if (pos != NULL)
  4660. *pos++ = '\0';
  4661. if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
  4662. return -1;
  4663. } else {
  4664. cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4665. cpt_prio[1] = 0;
  4666. }
  4667. /* Service and Response Information are optional */
  4668. if (pos && pos[0]) {
  4669. size_t len;
  4670. /* Note the bare ' included, which cannot exist legally
  4671. * in unescaped string. */
  4672. svc_info = os_strstr(pos, "svc_info='");
  4673. if (svc_info) {
  4674. svc_info += 9;
  4675. len = os_strlen(svc_info);
  4676. utf8_unescape(svc_info, len, svc_info, len);
  4677. }
  4678. }
  4679. return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
  4680. (u8) svc_state, (u16) config_methods,
  4681. svc_info, cpt_prio);
  4682. }
  4683. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  4684. {
  4685. char *pos;
  4686. pos = os_strchr(cmd, ' ');
  4687. if (pos == NULL)
  4688. return -1;
  4689. *pos++ = '\0';
  4690. if (os_strcmp(cmd, "bonjour") == 0)
  4691. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  4692. if (os_strcmp(cmd, "upnp") == 0)
  4693. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  4694. if (os_strcmp(cmd, "asp") == 0)
  4695. return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
  4696. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  4697. return -1;
  4698. }
  4699. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  4700. char *cmd)
  4701. {
  4702. size_t len;
  4703. struct wpabuf *query;
  4704. int ret;
  4705. len = os_strlen(cmd);
  4706. if (len & 1)
  4707. return -1;
  4708. len /= 2;
  4709. query = wpabuf_alloc(len);
  4710. if (query == NULL)
  4711. return -1;
  4712. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  4713. wpabuf_free(query);
  4714. return -1;
  4715. }
  4716. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  4717. wpabuf_free(query);
  4718. return ret;
  4719. }
  4720. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  4721. {
  4722. char *pos;
  4723. u8 version;
  4724. pos = os_strchr(cmd, ' ');
  4725. if (pos == NULL)
  4726. return -1;
  4727. *pos++ = '\0';
  4728. if (hexstr2bin(cmd, &version, 1) < 0)
  4729. return -1;
  4730. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  4731. }
  4732. static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
  4733. {
  4734. u32 adv_id;
  4735. if (os_strcmp(cmd, "all") == 0) {
  4736. wpas_p2p_service_flush_asp(wpa_s);
  4737. return 0;
  4738. }
  4739. if (sscanf(cmd, "%x", &adv_id) != 1)
  4740. return -1;
  4741. return wpas_p2p_service_del_asp(wpa_s, adv_id);
  4742. }
  4743. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  4744. {
  4745. char *pos;
  4746. pos = os_strchr(cmd, ' ');
  4747. if (pos == NULL)
  4748. return -1;
  4749. *pos++ = '\0';
  4750. if (os_strcmp(cmd, "bonjour") == 0)
  4751. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  4752. if (os_strcmp(cmd, "upnp") == 0)
  4753. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  4754. if (os_strcmp(cmd, "asp") == 0)
  4755. return p2p_ctrl_service_del_asp(wpa_s, pos);
  4756. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  4757. return -1;
  4758. }
  4759. static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
  4760. {
  4761. char *pos;
  4762. pos = os_strchr(cmd, ' ');
  4763. if (pos == NULL)
  4764. return -1;
  4765. *pos++ = '\0';
  4766. if (os_strcmp(cmd, "asp") == 0)
  4767. return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
  4768. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  4769. return -1;
  4770. }
  4771. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  4772. {
  4773. u8 addr[ETH_ALEN];
  4774. /* <addr> */
  4775. if (hwaddr_aton(cmd, addr))
  4776. return -1;
  4777. return wpas_p2p_reject(wpa_s, addr);
  4778. }
  4779. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  4780. {
  4781. char *pos;
  4782. int id;
  4783. struct wpa_ssid *ssid;
  4784. u8 *_peer = NULL, peer[ETH_ALEN];
  4785. int freq = 0, pref_freq = 0;
  4786. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  4787. id = atoi(cmd);
  4788. pos = os_strstr(cmd, " peer=");
  4789. if (pos) {
  4790. pos += 6;
  4791. if (hwaddr_aton(pos, peer))
  4792. return -1;
  4793. _peer = peer;
  4794. }
  4795. ssid = wpa_config_get_network(wpa_s->conf, id);
  4796. if (ssid == NULL || ssid->disabled != 2) {
  4797. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  4798. "for persistent P2P group",
  4799. id);
  4800. return -1;
  4801. }
  4802. pos = os_strstr(cmd, " freq=");
  4803. if (pos) {
  4804. pos += 6;
  4805. freq = atoi(pos);
  4806. if (freq <= 0)
  4807. return -1;
  4808. }
  4809. pos = os_strstr(cmd, " pref=");
  4810. if (pos) {
  4811. pos += 6;
  4812. pref_freq = atoi(pos);
  4813. if (pref_freq <= 0)
  4814. return -1;
  4815. }
  4816. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4817. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4818. vht;
  4819. pos = os_strstr(cmd, "freq2=");
  4820. if (pos)
  4821. freq2 = atoi(pos + 6);
  4822. pos = os_strstr(cmd, " max_oper_chwidth=");
  4823. if (pos)
  4824. chwidth = atoi(pos + 18);
  4825. max_oper_chwidth = parse_freq(chwidth, freq2);
  4826. if (max_oper_chwidth < 0)
  4827. return -1;
  4828. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
  4829. max_oper_chwidth, pref_freq);
  4830. }
  4831. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  4832. {
  4833. char *pos;
  4834. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  4835. pos = os_strstr(cmd, " peer=");
  4836. if (!pos)
  4837. return -1;
  4838. *pos = '\0';
  4839. pos += 6;
  4840. if (hwaddr_aton(pos, peer)) {
  4841. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  4842. return -1;
  4843. }
  4844. pos = os_strstr(pos, " go_dev_addr=");
  4845. if (pos) {
  4846. pos += 13;
  4847. if (hwaddr_aton(pos, go_dev_addr)) {
  4848. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  4849. pos);
  4850. return -1;
  4851. }
  4852. go_dev = go_dev_addr;
  4853. }
  4854. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  4855. }
  4856. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  4857. {
  4858. if (os_strncmp(cmd, "persistent=", 11) == 0)
  4859. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  4860. if (os_strncmp(cmd, "group=", 6) == 0)
  4861. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  4862. return -1;
  4863. }
  4864. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  4865. int id, int freq, int vht_center_freq2,
  4866. int ht40, int vht, int vht_chwidth)
  4867. {
  4868. struct wpa_ssid *ssid;
  4869. ssid = wpa_config_get_network(wpa_s->conf, id);
  4870. if (ssid == NULL || ssid->disabled != 2) {
  4871. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  4872. "for persistent P2P group",
  4873. id);
  4874. return -1;
  4875. }
  4876. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
  4877. vht_center_freq2, 0, ht40, vht,
  4878. vht_chwidth, NULL, 0, 0);
  4879. }
  4880. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  4881. {
  4882. int freq = 0, persistent = 0, group_id = -1;
  4883. int vht = wpa_s->conf->p2p_go_vht;
  4884. int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
  4885. int max_oper_chwidth, chwidth = 0, freq2 = 0;
  4886. char *token, *context = NULL;
  4887. while ((token = str_token(cmd, " ", &context))) {
  4888. if (sscanf(token, "freq=%d", &freq) == 1 ||
  4889. sscanf(token, "freq2=%d", &freq2) == 1 ||
  4890. sscanf(token, "persistent=%d", &group_id) == 1 ||
  4891. sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
  4892. continue;
  4893. } else if (os_strcmp(token, "ht40") == 0) {
  4894. ht40 = 1;
  4895. } else if (os_strcmp(token, "vht") == 0) {
  4896. vht = 1;
  4897. ht40 = 1;
  4898. } else if (os_strcmp(token, "persistent") == 0) {
  4899. persistent = 1;
  4900. } else {
  4901. wpa_printf(MSG_DEBUG,
  4902. "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
  4903. token);
  4904. return -1;
  4905. }
  4906. }
  4907. max_oper_chwidth = parse_freq(chwidth, freq2);
  4908. if (max_oper_chwidth < 0)
  4909. return -1;
  4910. if (group_id >= 0)
  4911. return p2p_ctrl_group_add_persistent(wpa_s, group_id,
  4912. freq, freq2, ht40, vht,
  4913. max_oper_chwidth);
  4914. return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
  4915. max_oper_chwidth);
  4916. }
  4917. static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
  4918. char *buf, size_t buflen)
  4919. {
  4920. u8 dev_addr[ETH_ALEN];
  4921. struct wpa_ssid *ssid;
  4922. int res;
  4923. const u8 *iaddr;
  4924. ssid = wpa_s->current_ssid;
  4925. if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
  4926. hwaddr_aton(cmd, dev_addr))
  4927. return -1;
  4928. iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
  4929. if (!iaddr)
  4930. return -1;
  4931. res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
  4932. if (os_snprintf_error(buflen, res))
  4933. return -1;
  4934. return res;
  4935. }
  4936. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  4937. char *buf, size_t buflen)
  4938. {
  4939. u8 addr[ETH_ALEN], *addr_ptr;
  4940. int next, res;
  4941. const struct p2p_peer_info *info;
  4942. char *pos, *end;
  4943. char devtype[WPS_DEV_TYPE_BUFSIZE];
  4944. struct wpa_ssid *ssid;
  4945. size_t i;
  4946. if (!wpa_s->global->p2p)
  4947. return -1;
  4948. if (os_strcmp(cmd, "FIRST") == 0) {
  4949. addr_ptr = NULL;
  4950. next = 0;
  4951. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  4952. if (hwaddr_aton(cmd + 5, addr) < 0)
  4953. return -1;
  4954. addr_ptr = addr;
  4955. next = 1;
  4956. } else {
  4957. if (hwaddr_aton(cmd, addr) < 0)
  4958. return -1;
  4959. addr_ptr = addr;
  4960. next = 0;
  4961. }
  4962. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  4963. if (info == NULL)
  4964. return -1;
  4965. pos = buf;
  4966. end = buf + buflen;
  4967. res = os_snprintf(pos, end - pos, MACSTR "\n"
  4968. "pri_dev_type=%s\n"
  4969. "device_name=%s\n"
  4970. "manufacturer=%s\n"
  4971. "model_name=%s\n"
  4972. "model_number=%s\n"
  4973. "serial_number=%s\n"
  4974. "config_methods=0x%x\n"
  4975. "dev_capab=0x%x\n"
  4976. "group_capab=0x%x\n"
  4977. "level=%d\n",
  4978. MAC2STR(info->p2p_device_addr),
  4979. wps_dev_type_bin2str(info->pri_dev_type,
  4980. devtype, sizeof(devtype)),
  4981. info->device_name,
  4982. info->manufacturer,
  4983. info->model_name,
  4984. info->model_number,
  4985. info->serial_number,
  4986. info->config_methods,
  4987. info->dev_capab,
  4988. info->group_capab,
  4989. info->level);
  4990. if (os_snprintf_error(end - pos, res))
  4991. return pos - buf;
  4992. pos += res;
  4993. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  4994. {
  4995. const u8 *t;
  4996. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  4997. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  4998. wps_dev_type_bin2str(t, devtype,
  4999. sizeof(devtype)));
  5000. if (os_snprintf_error(end - pos, res))
  5001. return pos - buf;
  5002. pos += res;
  5003. }
  5004. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  5005. if (ssid) {
  5006. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  5007. if (os_snprintf_error(end - pos, res))
  5008. return pos - buf;
  5009. pos += res;
  5010. }
  5011. res = p2p_get_peer_info_txt(info, pos, end - pos);
  5012. if (res < 0)
  5013. return pos - buf;
  5014. pos += res;
  5015. if (info->vendor_elems) {
  5016. res = os_snprintf(pos, end - pos, "vendor_elems=");
  5017. if (os_snprintf_error(end - pos, res))
  5018. return pos - buf;
  5019. pos += res;
  5020. pos += wpa_snprintf_hex(pos, end - pos,
  5021. wpabuf_head(info->vendor_elems),
  5022. wpabuf_len(info->vendor_elems));
  5023. res = os_snprintf(pos, end - pos, "\n");
  5024. if (os_snprintf_error(end - pos, res))
  5025. return pos - buf;
  5026. pos += res;
  5027. }
  5028. return pos - buf;
  5029. }
  5030. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  5031. const char *param)
  5032. {
  5033. unsigned int i;
  5034. if (wpa_s->global->p2p == NULL)
  5035. return -1;
  5036. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  5037. return -1;
  5038. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  5039. struct wpa_freq_range *freq;
  5040. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  5041. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  5042. freq->min, freq->max);
  5043. }
  5044. wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
  5045. return 0;
  5046. }
  5047. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  5048. {
  5049. char *param;
  5050. if (wpa_s->global->p2p == NULL)
  5051. return -1;
  5052. param = os_strchr(cmd, ' ');
  5053. if (param == NULL)
  5054. return -1;
  5055. *param++ = '\0';
  5056. if (os_strcmp(cmd, "discoverability") == 0) {
  5057. p2p_set_client_discoverability(wpa_s->global->p2p,
  5058. atoi(param));
  5059. return 0;
  5060. }
  5061. if (os_strcmp(cmd, "managed") == 0) {
  5062. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  5063. return 0;
  5064. }
  5065. if (os_strcmp(cmd, "listen_channel") == 0) {
  5066. char *pos;
  5067. u8 channel, op_class;
  5068. channel = atoi(param);
  5069. pos = os_strchr(param, ' ');
  5070. op_class = pos ? atoi(pos) : 81;
  5071. return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
  5072. channel, 1);
  5073. }
  5074. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  5075. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  5076. os_strlen(param));
  5077. }
  5078. if (os_strcmp(cmd, "noa") == 0) {
  5079. char *pos;
  5080. int count, start, duration;
  5081. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  5082. count = atoi(param);
  5083. pos = os_strchr(param, ',');
  5084. if (pos == NULL)
  5085. return -1;
  5086. pos++;
  5087. start = atoi(pos);
  5088. pos = os_strchr(pos, ',');
  5089. if (pos == NULL)
  5090. return -1;
  5091. pos++;
  5092. duration = atoi(pos);
  5093. if (count < 0 || count > 255 || start < 0 || duration < 0)
  5094. return -1;
  5095. if (count == 0 && duration > 0)
  5096. return -1;
  5097. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  5098. "start=%d duration=%d", count, start, duration);
  5099. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  5100. }
  5101. if (os_strcmp(cmd, "ps") == 0)
  5102. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  5103. if (os_strcmp(cmd, "oppps") == 0)
  5104. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  5105. if (os_strcmp(cmd, "ctwindow") == 0)
  5106. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  5107. if (os_strcmp(cmd, "disabled") == 0) {
  5108. wpa_s->global->p2p_disabled = atoi(param);
  5109. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  5110. wpa_s->global->p2p_disabled ?
  5111. "disabled" : "enabled");
  5112. if (wpa_s->global->p2p_disabled) {
  5113. wpas_p2p_stop_find(wpa_s);
  5114. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5115. p2p_flush(wpa_s->global->p2p);
  5116. }
  5117. return 0;
  5118. }
  5119. if (os_strcmp(cmd, "conc_pref") == 0) {
  5120. if (os_strcmp(param, "sta") == 0)
  5121. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  5122. else if (os_strcmp(param, "p2p") == 0)
  5123. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  5124. else {
  5125. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  5126. return -1;
  5127. }
  5128. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  5129. "%s", param);
  5130. return 0;
  5131. }
  5132. if (os_strcmp(cmd, "force_long_sd") == 0) {
  5133. wpa_s->force_long_sd = atoi(param);
  5134. return 0;
  5135. }
  5136. if (os_strcmp(cmd, "peer_filter") == 0) {
  5137. u8 addr[ETH_ALEN];
  5138. if (hwaddr_aton(param, addr))
  5139. return -1;
  5140. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  5141. return 0;
  5142. }
  5143. if (os_strcmp(cmd, "cross_connect") == 0)
  5144. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  5145. if (os_strcmp(cmd, "go_apsd") == 0) {
  5146. if (os_strcmp(param, "disable") == 0)
  5147. wpa_s->set_ap_uapsd = 0;
  5148. else {
  5149. wpa_s->set_ap_uapsd = 1;
  5150. wpa_s->ap_uapsd = atoi(param);
  5151. }
  5152. return 0;
  5153. }
  5154. if (os_strcmp(cmd, "client_apsd") == 0) {
  5155. if (os_strcmp(param, "disable") == 0)
  5156. wpa_s->set_sta_uapsd = 0;
  5157. else {
  5158. int be, bk, vi, vo;
  5159. char *pos;
  5160. /* format: BE,BK,VI,VO;max SP Length */
  5161. be = atoi(param);
  5162. pos = os_strchr(param, ',');
  5163. if (pos == NULL)
  5164. return -1;
  5165. pos++;
  5166. bk = atoi(pos);
  5167. pos = os_strchr(pos, ',');
  5168. if (pos == NULL)
  5169. return -1;
  5170. pos++;
  5171. vi = atoi(pos);
  5172. pos = os_strchr(pos, ',');
  5173. if (pos == NULL)
  5174. return -1;
  5175. pos++;
  5176. vo = atoi(pos);
  5177. /* ignore max SP Length for now */
  5178. wpa_s->set_sta_uapsd = 1;
  5179. wpa_s->sta_uapsd = 0;
  5180. if (be)
  5181. wpa_s->sta_uapsd |= BIT(0);
  5182. if (bk)
  5183. wpa_s->sta_uapsd |= BIT(1);
  5184. if (vi)
  5185. wpa_s->sta_uapsd |= BIT(2);
  5186. if (vo)
  5187. wpa_s->sta_uapsd |= BIT(3);
  5188. }
  5189. return 0;
  5190. }
  5191. if (os_strcmp(cmd, "disallow_freq") == 0)
  5192. return p2p_ctrl_disallow_freq(wpa_s, param);
  5193. if (os_strcmp(cmd, "disc_int") == 0) {
  5194. int min_disc_int, max_disc_int, max_disc_tu;
  5195. char *pos;
  5196. pos = param;
  5197. min_disc_int = atoi(pos);
  5198. pos = os_strchr(pos, ' ');
  5199. if (pos == NULL)
  5200. return -1;
  5201. *pos++ = '\0';
  5202. max_disc_int = atoi(pos);
  5203. pos = os_strchr(pos, ' ');
  5204. if (pos == NULL)
  5205. return -1;
  5206. *pos++ = '\0';
  5207. max_disc_tu = atoi(pos);
  5208. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  5209. max_disc_int, max_disc_tu);
  5210. }
  5211. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  5212. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  5213. return 0;
  5214. }
  5215. #ifdef CONFIG_WPS_NFC
  5216. if (os_strcmp(cmd, "nfc_tag") == 0)
  5217. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  5218. #endif /* CONFIG_WPS_NFC */
  5219. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  5220. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  5221. return 0;
  5222. }
  5223. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  5224. cmd);
  5225. return -1;
  5226. }
  5227. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  5228. {
  5229. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5230. wpa_s->force_long_sd = 0;
  5231. wpas_p2p_stop_find(wpa_s);
  5232. wpa_s->parent->p2ps_method_config_any = 0;
  5233. if (wpa_s->global->p2p)
  5234. p2p_flush(wpa_s->global->p2p);
  5235. }
  5236. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  5237. {
  5238. char *pos, *pos2;
  5239. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  5240. if (cmd[0]) {
  5241. pos = os_strchr(cmd, ' ');
  5242. if (pos == NULL)
  5243. return -1;
  5244. *pos++ = '\0';
  5245. dur1 = atoi(cmd);
  5246. pos2 = os_strchr(pos, ' ');
  5247. if (pos2)
  5248. *pos2++ = '\0';
  5249. int1 = atoi(pos);
  5250. } else
  5251. pos2 = NULL;
  5252. if (pos2) {
  5253. pos = os_strchr(pos2, ' ');
  5254. if (pos == NULL)
  5255. return -1;
  5256. *pos++ = '\0';
  5257. dur2 = atoi(pos2);
  5258. int2 = atoi(pos);
  5259. }
  5260. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  5261. }
  5262. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  5263. {
  5264. char *pos;
  5265. unsigned int period = 0, interval = 0;
  5266. if (cmd[0]) {
  5267. pos = os_strchr(cmd, ' ');
  5268. if (pos == NULL)
  5269. return -1;
  5270. *pos++ = '\0';
  5271. period = atoi(cmd);
  5272. interval = atoi(pos);
  5273. }
  5274. return wpas_p2p_ext_listen(wpa_s, period, interval);
  5275. }
  5276. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  5277. {
  5278. const char *pos;
  5279. u8 peer[ETH_ALEN];
  5280. int iface_addr = 0;
  5281. pos = cmd;
  5282. if (os_strncmp(pos, "iface=", 6) == 0) {
  5283. iface_addr = 1;
  5284. pos += 6;
  5285. }
  5286. if (hwaddr_aton(pos, peer))
  5287. return -1;
  5288. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  5289. return 0;
  5290. }
  5291. static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
  5292. {
  5293. int freq = 0, period = 0, interval = 0, count = 0;
  5294. if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
  5295. {
  5296. wpa_printf(MSG_DEBUG,
  5297. "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
  5298. return -1;
  5299. }
  5300. return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
  5301. }
  5302. #endif /* CONFIG_P2P */
  5303. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  5304. {
  5305. struct wpa_freq_range_list ranges;
  5306. int *freqs = NULL;
  5307. struct hostapd_hw_modes *mode;
  5308. u16 i;
  5309. if (wpa_s->hw.modes == NULL)
  5310. return NULL;
  5311. os_memset(&ranges, 0, sizeof(ranges));
  5312. if (freq_range_list_parse(&ranges, val) < 0)
  5313. return NULL;
  5314. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  5315. int j;
  5316. mode = &wpa_s->hw.modes[i];
  5317. for (j = 0; j < mode->num_channels; j++) {
  5318. unsigned int freq;
  5319. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  5320. continue;
  5321. freq = mode->channels[j].freq;
  5322. if (!freq_range_list_includes(&ranges, freq))
  5323. continue;
  5324. int_array_add_unique(&freqs, freq);
  5325. }
  5326. }
  5327. os_free(ranges.range);
  5328. return freqs;
  5329. }
  5330. #ifdef CONFIG_INTERWORKING
  5331. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  5332. {
  5333. int auto_sel = 0;
  5334. int *freqs = NULL;
  5335. if (param) {
  5336. char *pos;
  5337. auto_sel = os_strstr(param, "auto") != NULL;
  5338. pos = os_strstr(param, "freq=");
  5339. if (pos) {
  5340. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  5341. if (freqs == NULL)
  5342. return -1;
  5343. }
  5344. }
  5345. return interworking_select(wpa_s, auto_sel, freqs);
  5346. }
  5347. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
  5348. int only_add)
  5349. {
  5350. u8 bssid[ETH_ALEN];
  5351. struct wpa_bss *bss;
  5352. if (hwaddr_aton(dst, bssid)) {
  5353. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  5354. return -1;
  5355. }
  5356. bss = wpa_bss_get_bssid(wpa_s, bssid);
  5357. if (bss == NULL) {
  5358. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  5359. MAC2STR(bssid));
  5360. return -1;
  5361. }
  5362. if (bss->ssid_len == 0) {
  5363. int found = 0;
  5364. wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
  5365. " does not have SSID information", MAC2STR(bssid));
  5366. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
  5367. list) {
  5368. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  5369. bss->ssid_len > 0) {
  5370. found = 1;
  5371. break;
  5372. }
  5373. }
  5374. if (!found)
  5375. return -1;
  5376. wpa_printf(MSG_DEBUG,
  5377. "Found another matching BSS entry with SSID");
  5378. }
  5379. return interworking_connect(wpa_s, bss, only_add);
  5380. }
  5381. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  5382. {
  5383. u8 dst_addr[ETH_ALEN];
  5384. int used;
  5385. char *pos;
  5386. #define MAX_ANQP_INFO_ID 100
  5387. u16 id[MAX_ANQP_INFO_ID];
  5388. size_t num_id = 0;
  5389. u32 subtypes = 0;
  5390. used = hwaddr_aton2(dst, dst_addr);
  5391. if (used < 0)
  5392. return -1;
  5393. pos = dst + used;
  5394. if (*pos == ' ')
  5395. pos++;
  5396. while (num_id < MAX_ANQP_INFO_ID) {
  5397. if (os_strncmp(pos, "hs20:", 5) == 0) {
  5398. #ifdef CONFIG_HS20
  5399. int num = atoi(pos + 5);
  5400. if (num <= 0 || num > 31)
  5401. return -1;
  5402. subtypes |= BIT(num);
  5403. #else /* CONFIG_HS20 */
  5404. return -1;
  5405. #endif /* CONFIG_HS20 */
  5406. } else {
  5407. id[num_id] = atoi(pos);
  5408. if (id[num_id])
  5409. num_id++;
  5410. }
  5411. pos = os_strchr(pos + 1, ',');
  5412. if (pos == NULL)
  5413. break;
  5414. pos++;
  5415. }
  5416. if (num_id == 0)
  5417. return -1;
  5418. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
  5419. }
  5420. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  5421. {
  5422. u8 dst_addr[ETH_ALEN];
  5423. struct wpabuf *advproto, *query = NULL;
  5424. int used, ret = -1;
  5425. char *pos, *end;
  5426. size_t len;
  5427. used = hwaddr_aton2(cmd, dst_addr);
  5428. if (used < 0)
  5429. return -1;
  5430. pos = cmd + used;
  5431. while (*pos == ' ')
  5432. pos++;
  5433. /* Advertisement Protocol ID */
  5434. end = os_strchr(pos, ' ');
  5435. if (end)
  5436. len = end - pos;
  5437. else
  5438. len = os_strlen(pos);
  5439. if (len & 0x01)
  5440. return -1;
  5441. len /= 2;
  5442. if (len == 0)
  5443. return -1;
  5444. advproto = wpabuf_alloc(len);
  5445. if (advproto == NULL)
  5446. return -1;
  5447. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  5448. goto fail;
  5449. if (end) {
  5450. /* Optional Query Request */
  5451. pos = end + 1;
  5452. while (*pos == ' ')
  5453. pos++;
  5454. len = os_strlen(pos);
  5455. if (len) {
  5456. if (len & 0x01)
  5457. goto fail;
  5458. len /= 2;
  5459. if (len == 0)
  5460. goto fail;
  5461. query = wpabuf_alloc(len);
  5462. if (query == NULL)
  5463. goto fail;
  5464. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  5465. goto fail;
  5466. }
  5467. }
  5468. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  5469. fail:
  5470. wpabuf_free(advproto);
  5471. wpabuf_free(query);
  5472. return ret;
  5473. }
  5474. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  5475. size_t buflen)
  5476. {
  5477. u8 addr[ETH_ALEN];
  5478. int dialog_token;
  5479. int used;
  5480. char *pos;
  5481. size_t resp_len, start, requested_len;
  5482. struct wpabuf *resp;
  5483. int ret;
  5484. used = hwaddr_aton2(cmd, addr);
  5485. if (used < 0)
  5486. return -1;
  5487. pos = cmd + used;
  5488. while (*pos == ' ')
  5489. pos++;
  5490. dialog_token = atoi(pos);
  5491. if (wpa_s->last_gas_resp &&
  5492. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  5493. dialog_token == wpa_s->last_gas_dialog_token)
  5494. resp = wpa_s->last_gas_resp;
  5495. else if (wpa_s->prev_gas_resp &&
  5496. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  5497. dialog_token == wpa_s->prev_gas_dialog_token)
  5498. resp = wpa_s->prev_gas_resp;
  5499. else
  5500. return -1;
  5501. resp_len = wpabuf_len(resp);
  5502. start = 0;
  5503. requested_len = resp_len;
  5504. pos = os_strchr(pos, ' ');
  5505. if (pos) {
  5506. start = atoi(pos);
  5507. if (start > resp_len)
  5508. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  5509. pos = os_strchr(pos, ',');
  5510. if (pos == NULL)
  5511. return -1;
  5512. pos++;
  5513. requested_len = atoi(pos);
  5514. if (start + requested_len > resp_len)
  5515. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  5516. }
  5517. if (requested_len * 2 + 1 > buflen)
  5518. return os_snprintf(buf, buflen, "FAIL-Too long response");
  5519. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  5520. requested_len);
  5521. if (start + requested_len == resp_len) {
  5522. /*
  5523. * Free memory by dropping the response after it has been
  5524. * fetched.
  5525. */
  5526. if (resp == wpa_s->prev_gas_resp) {
  5527. wpabuf_free(wpa_s->prev_gas_resp);
  5528. wpa_s->prev_gas_resp = NULL;
  5529. } else {
  5530. wpabuf_free(wpa_s->last_gas_resp);
  5531. wpa_s->last_gas_resp = NULL;
  5532. }
  5533. }
  5534. return ret;
  5535. }
  5536. #endif /* CONFIG_INTERWORKING */
  5537. #ifdef CONFIG_HS20
  5538. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  5539. {
  5540. u8 dst_addr[ETH_ALEN];
  5541. int used;
  5542. char *pos;
  5543. u32 subtypes = 0;
  5544. used = hwaddr_aton2(dst, dst_addr);
  5545. if (used < 0)
  5546. return -1;
  5547. pos = dst + used;
  5548. if (*pos == ' ')
  5549. pos++;
  5550. for (;;) {
  5551. int num = atoi(pos);
  5552. if (num <= 0 || num > 31)
  5553. return -1;
  5554. subtypes |= BIT(num);
  5555. pos = os_strchr(pos + 1, ',');
  5556. if (pos == NULL)
  5557. break;
  5558. pos++;
  5559. }
  5560. if (subtypes == 0)
  5561. return -1;
  5562. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
  5563. }
  5564. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  5565. const u8 *addr, const char *realm)
  5566. {
  5567. u8 *buf;
  5568. size_t rlen, len;
  5569. int ret;
  5570. rlen = os_strlen(realm);
  5571. len = 3 + rlen;
  5572. buf = os_malloc(len);
  5573. if (buf == NULL)
  5574. return -1;
  5575. buf[0] = 1; /* NAI Home Realm Count */
  5576. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  5577. buf[2] = rlen;
  5578. os_memcpy(buf + 3, realm, rlen);
  5579. ret = hs20_anqp_send_req(wpa_s, addr,
  5580. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  5581. buf, len, 0);
  5582. os_free(buf);
  5583. return ret;
  5584. }
  5585. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  5586. char *dst)
  5587. {
  5588. struct wpa_cred *cred = wpa_s->conf->cred;
  5589. u8 dst_addr[ETH_ALEN];
  5590. int used;
  5591. u8 *buf;
  5592. size_t len;
  5593. int ret;
  5594. used = hwaddr_aton2(dst, dst_addr);
  5595. if (used < 0)
  5596. return -1;
  5597. while (dst[used] == ' ')
  5598. used++;
  5599. if (os_strncmp(dst + used, "realm=", 6) == 0)
  5600. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  5601. dst + used + 6);
  5602. len = os_strlen(dst + used);
  5603. if (len == 0 && cred && cred->realm)
  5604. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  5605. if (len & 1)
  5606. return -1;
  5607. len /= 2;
  5608. buf = os_malloc(len);
  5609. if (buf == NULL)
  5610. return -1;
  5611. if (hexstr2bin(dst + used, buf, len) < 0) {
  5612. os_free(buf);
  5613. return -1;
  5614. }
  5615. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  5616. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  5617. buf, len, 0);
  5618. os_free(buf);
  5619. return ret;
  5620. }
  5621. static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
  5622. int buflen)
  5623. {
  5624. u8 dst_addr[ETH_ALEN];
  5625. int used;
  5626. char *ctx = NULL, *icon, *poffset, *psize;
  5627. used = hwaddr_aton2(cmd, dst_addr);
  5628. if (used < 0)
  5629. return -1;
  5630. cmd += used;
  5631. icon = str_token(cmd, " ", &ctx);
  5632. poffset = str_token(cmd, " ", &ctx);
  5633. psize = str_token(cmd, " ", &ctx);
  5634. if (!icon || !poffset || !psize)
  5635. return -1;
  5636. wpa_s->fetch_osu_icon_in_progress = 0;
  5637. return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
  5638. reply, buflen);
  5639. }
  5640. static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
  5641. {
  5642. u8 dst_addr[ETH_ALEN];
  5643. int used;
  5644. char *icon;
  5645. if (!cmd[0])
  5646. return hs20_del_icon(wpa_s, NULL, NULL);
  5647. used = hwaddr_aton2(cmd, dst_addr);
  5648. if (used < 0)
  5649. return -1;
  5650. while (cmd[used] == ' ')
  5651. used++;
  5652. icon = cmd[used] ? &cmd[used] : NULL;
  5653. return hs20_del_icon(wpa_s, dst_addr, icon);
  5654. }
  5655. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
  5656. {
  5657. u8 dst_addr[ETH_ALEN];
  5658. int used;
  5659. char *icon;
  5660. used = hwaddr_aton2(cmd, dst_addr);
  5661. if (used < 0)
  5662. return -1;
  5663. while (cmd[used] == ' ')
  5664. used++;
  5665. icon = &cmd[used];
  5666. wpa_s->fetch_osu_icon_in_progress = 0;
  5667. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  5668. (u8 *) icon, os_strlen(icon), inmem);
  5669. }
  5670. #endif /* CONFIG_HS20 */
  5671. #ifdef CONFIG_AUTOSCAN
  5672. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  5673. char *cmd)
  5674. {
  5675. enum wpa_states state = wpa_s->wpa_state;
  5676. char *new_params = NULL;
  5677. if (os_strlen(cmd) > 0) {
  5678. new_params = os_strdup(cmd);
  5679. if (new_params == NULL)
  5680. return -1;
  5681. }
  5682. os_free(wpa_s->conf->autoscan);
  5683. wpa_s->conf->autoscan = new_params;
  5684. if (wpa_s->conf->autoscan == NULL)
  5685. autoscan_deinit(wpa_s);
  5686. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  5687. autoscan_init(wpa_s, 1);
  5688. else if (state == WPA_SCANNING)
  5689. wpa_supplicant_reinit_autoscan(wpa_s);
  5690. return 0;
  5691. }
  5692. #endif /* CONFIG_AUTOSCAN */
  5693. #ifdef CONFIG_WNM
  5694. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  5695. {
  5696. int enter;
  5697. int intval = 0;
  5698. char *pos;
  5699. int ret;
  5700. struct wpabuf *tfs_req = NULL;
  5701. if (os_strncmp(cmd, "enter", 5) == 0)
  5702. enter = 1;
  5703. else if (os_strncmp(cmd, "exit", 4) == 0)
  5704. enter = 0;
  5705. else
  5706. return -1;
  5707. pos = os_strstr(cmd, " interval=");
  5708. if (pos)
  5709. intval = atoi(pos + 10);
  5710. pos = os_strstr(cmd, " tfs_req=");
  5711. if (pos) {
  5712. char *end;
  5713. size_t len;
  5714. pos += 9;
  5715. end = os_strchr(pos, ' ');
  5716. if (end)
  5717. len = end - pos;
  5718. else
  5719. len = os_strlen(pos);
  5720. if (len & 1)
  5721. return -1;
  5722. len /= 2;
  5723. tfs_req = wpabuf_alloc(len);
  5724. if (tfs_req == NULL)
  5725. return -1;
  5726. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  5727. wpabuf_free(tfs_req);
  5728. return -1;
  5729. }
  5730. }
  5731. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  5732. WNM_SLEEP_MODE_EXIT, intval,
  5733. tfs_req);
  5734. wpabuf_free(tfs_req);
  5735. return ret;
  5736. }
  5737. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  5738. {
  5739. int query_reason, list = 0;
  5740. query_reason = atoi(cmd);
  5741. cmd = os_strchr(cmd, ' ');
  5742. if (cmd) {
  5743. cmd++;
  5744. if (os_strncmp(cmd, "list", 4) == 0) {
  5745. list = 1;
  5746. } else {
  5747. wpa_printf(MSG_DEBUG, "WNM Query: Invalid option %s",
  5748. cmd);
  5749. return -1;
  5750. }
  5751. }
  5752. wpa_printf(MSG_DEBUG,
  5753. "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
  5754. query_reason, list ? " candidate list" : "");
  5755. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason, list);
  5756. }
  5757. #endif /* CONFIG_WNM */
  5758. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  5759. size_t buflen)
  5760. {
  5761. struct wpa_signal_info si;
  5762. int ret;
  5763. char *pos, *end;
  5764. ret = wpa_drv_signal_poll(wpa_s, &si);
  5765. if (ret)
  5766. return -1;
  5767. pos = buf;
  5768. end = buf + buflen;
  5769. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  5770. "NOISE=%d\nFREQUENCY=%u\n",
  5771. si.current_signal, si.current_txrate / 1000,
  5772. si.current_noise, si.frequency);
  5773. if (os_snprintf_error(end - pos, ret))
  5774. return -1;
  5775. pos += ret;
  5776. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  5777. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  5778. channel_width_to_string(si.chanwidth));
  5779. if (os_snprintf_error(end - pos, ret))
  5780. return -1;
  5781. pos += ret;
  5782. }
  5783. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  5784. ret = os_snprintf(pos, end - pos,
  5785. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  5786. si.center_frq1, si.center_frq2);
  5787. if (os_snprintf_error(end - pos, ret))
  5788. return -1;
  5789. pos += ret;
  5790. }
  5791. if (si.avg_signal) {
  5792. ret = os_snprintf(pos, end - pos,
  5793. "AVG_RSSI=%d\n", si.avg_signal);
  5794. if (os_snprintf_error(end - pos, ret))
  5795. return -1;
  5796. pos += ret;
  5797. }
  5798. if (si.avg_beacon_signal) {
  5799. ret = os_snprintf(pos, end - pos,
  5800. "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
  5801. if (os_snprintf_error(end - pos, ret))
  5802. return -1;
  5803. pos += ret;
  5804. }
  5805. return pos - buf;
  5806. }
  5807. static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
  5808. const char *cmd)
  5809. {
  5810. const char *pos;
  5811. int threshold = 0;
  5812. int hysteresis = 0;
  5813. if (wpa_s->bgscan && wpa_s->bgscan_priv) {
  5814. wpa_printf(MSG_DEBUG,
  5815. "Reject SIGNAL_MONITOR command - bgscan is active");
  5816. return -1;
  5817. }
  5818. pos = os_strstr(cmd, "THRESHOLD=");
  5819. if (pos)
  5820. threshold = atoi(pos + 10);
  5821. pos = os_strstr(cmd, "HYSTERESIS=");
  5822. if (pos)
  5823. hysteresis = atoi(pos + 11);
  5824. return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
  5825. }
  5826. static int wpas_ctrl_iface_get_pref_freq_list(
  5827. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  5828. {
  5829. unsigned int freq_list[100], num = 100, i;
  5830. int ret;
  5831. enum wpa_driver_if_type iface_type;
  5832. char *pos, *end;
  5833. pos = buf;
  5834. end = buf + buflen;
  5835. /* buf: "<interface_type>" */
  5836. if (os_strcmp(cmd, "STATION") == 0)
  5837. iface_type = WPA_IF_STATION;
  5838. else if (os_strcmp(cmd, "AP") == 0)
  5839. iface_type = WPA_IF_AP_BSS;
  5840. else if (os_strcmp(cmd, "P2P_GO") == 0)
  5841. iface_type = WPA_IF_P2P_GO;
  5842. else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
  5843. iface_type = WPA_IF_P2P_CLIENT;
  5844. else if (os_strcmp(cmd, "IBSS") == 0)
  5845. iface_type = WPA_IF_IBSS;
  5846. else if (os_strcmp(cmd, "TDLS") == 0)
  5847. iface_type = WPA_IF_TDLS;
  5848. else
  5849. return -1;
  5850. wpa_printf(MSG_DEBUG,
  5851. "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
  5852. iface_type, buf);
  5853. ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
  5854. if (ret)
  5855. return -1;
  5856. for (i = 0; i < num; i++) {
  5857. ret = os_snprintf(pos, end - pos, "%s%u",
  5858. i > 0 ? "," : "", freq_list[i]);
  5859. if (os_snprintf_error(end - pos, ret))
  5860. return -1;
  5861. pos += ret;
  5862. }
  5863. return pos - buf;
  5864. }
  5865. static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
  5866. char *buf, size_t buflen)
  5867. {
  5868. int ret, i;
  5869. char *pos, *end;
  5870. ret = os_snprintf(buf, buflen, "%016llX:\n",
  5871. (long long unsigned) wpa_s->drv_flags);
  5872. if (os_snprintf_error(buflen, ret))
  5873. return -1;
  5874. pos = buf + ret;
  5875. end = buf + buflen;
  5876. for (i = 0; i < 64; i++) {
  5877. if (wpa_s->drv_flags & (1LLU << i)) {
  5878. ret = os_snprintf(pos, end - pos, "%s\n",
  5879. driver_flag_to_string(1LLU << i));
  5880. if (os_snprintf_error(end - pos, ret))
  5881. return -1;
  5882. pos += ret;
  5883. }
  5884. }
  5885. return pos - buf;
  5886. }
  5887. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  5888. size_t buflen)
  5889. {
  5890. struct hostap_sta_driver_data sta;
  5891. int ret;
  5892. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  5893. if (ret)
  5894. return -1;
  5895. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  5896. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  5897. if (os_snprintf_error(buflen, ret))
  5898. return -1;
  5899. return ret;
  5900. }
  5901. #ifdef ANDROID
  5902. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  5903. char *buf, size_t buflen)
  5904. {
  5905. int ret;
  5906. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  5907. if (ret == 0) {
  5908. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  5909. struct p2p_data *p2p = wpa_s->global->p2p;
  5910. if (p2p) {
  5911. char country[3];
  5912. country[0] = cmd[8];
  5913. country[1] = cmd[9];
  5914. country[2] = 0x04;
  5915. p2p_set_country(p2p, country);
  5916. }
  5917. }
  5918. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  5919. if (os_snprintf_error(buflen, ret))
  5920. ret = -1;
  5921. }
  5922. return ret;
  5923. }
  5924. #endif /* ANDROID */
  5925. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  5926. char *buf, size_t buflen)
  5927. {
  5928. int ret;
  5929. char *pos;
  5930. u8 *data = NULL;
  5931. unsigned int vendor_id, subcmd;
  5932. struct wpabuf *reply;
  5933. size_t data_len = 0;
  5934. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  5935. vendor_id = strtoul(cmd, &pos, 16);
  5936. if (!isblank((unsigned char) *pos))
  5937. return -EINVAL;
  5938. subcmd = strtoul(pos, &pos, 10);
  5939. if (*pos != '\0') {
  5940. if (!isblank((unsigned char) *pos++))
  5941. return -EINVAL;
  5942. data_len = os_strlen(pos);
  5943. }
  5944. if (data_len) {
  5945. data_len /= 2;
  5946. data = os_malloc(data_len);
  5947. if (!data)
  5948. return -1;
  5949. if (hexstr2bin(pos, data, data_len)) {
  5950. wpa_printf(MSG_DEBUG,
  5951. "Vendor command: wrong parameter format");
  5952. os_free(data);
  5953. return -EINVAL;
  5954. }
  5955. }
  5956. reply = wpabuf_alloc((buflen - 1) / 2);
  5957. if (!reply) {
  5958. os_free(data);
  5959. return -1;
  5960. }
  5961. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  5962. reply);
  5963. if (ret == 0)
  5964. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  5965. wpabuf_len(reply));
  5966. wpabuf_free(reply);
  5967. os_free(data);
  5968. return ret;
  5969. }
  5970. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  5971. {
  5972. #ifdef CONFIG_P2P
  5973. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
  5974. wpa_s->global->p2p_init_wpa_s : wpa_s;
  5975. #endif /* CONFIG_P2P */
  5976. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  5977. wpas_abort_ongoing_scan(wpa_s);
  5978. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  5979. /*
  5980. * Avoid possible auto connect re-connection on getting
  5981. * disconnected due to state flush.
  5982. */
  5983. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  5984. }
  5985. #ifdef CONFIG_P2P
  5986. wpas_p2p_group_remove(p2p_wpa_s, "*");
  5987. wpas_p2p_cancel(p2p_wpa_s);
  5988. p2p_ctrl_flush(p2p_wpa_s);
  5989. wpas_p2p_service_flush(p2p_wpa_s);
  5990. p2p_wpa_s->global->p2p_disabled = 0;
  5991. p2p_wpa_s->global->p2p_per_sta_psk = 0;
  5992. p2p_wpa_s->conf->num_sec_device_types = 0;
  5993. p2p_wpa_s->p2p_disable_ip_addr_req = 0;
  5994. os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
  5995. p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
  5996. p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
  5997. p2p_wpa_s->global->pending_p2ps_group = 0;
  5998. p2p_wpa_s->global->pending_p2ps_group_freq = 0;
  5999. #endif /* CONFIG_P2P */
  6000. #ifdef CONFIG_WPS_TESTING
  6001. wps_version_number = 0x20;
  6002. wps_testing_dummy_cred = 0;
  6003. wps_corrupt_pkhash = 0;
  6004. wps_force_auth_types_in_use = 0;
  6005. wps_force_encr_types_in_use = 0;
  6006. #endif /* CONFIG_WPS_TESTING */
  6007. #ifdef CONFIG_WPS
  6008. wpa_s->wps_fragment_size = 0;
  6009. wpas_wps_cancel(wpa_s);
  6010. wps_registrar_flush(wpa_s->wps->registrar);
  6011. #endif /* CONFIG_WPS */
  6012. wpa_s->after_wps = 0;
  6013. wpa_s->known_wps_freq = 0;
  6014. #ifdef CONFIG_TDLS
  6015. #ifdef CONFIG_TDLS_TESTING
  6016. tdls_testing = 0;
  6017. #endif /* CONFIG_TDLS_TESTING */
  6018. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  6019. wpa_tdls_enable(wpa_s->wpa, 1);
  6020. #endif /* CONFIG_TDLS */
  6021. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  6022. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  6023. wpa_s->no_keep_alive = 0;
  6024. wpa_s->own_disconnect_req = 0;
  6025. os_free(wpa_s->disallow_aps_bssid);
  6026. wpa_s->disallow_aps_bssid = NULL;
  6027. wpa_s->disallow_aps_bssid_count = 0;
  6028. os_free(wpa_s->disallow_aps_ssid);
  6029. wpa_s->disallow_aps_ssid = NULL;
  6030. wpa_s->disallow_aps_ssid_count = 0;
  6031. wpa_s->set_sta_uapsd = 0;
  6032. wpa_s->sta_uapsd = 0;
  6033. wpa_drv_radio_disable(wpa_s, 0);
  6034. wpa_blacklist_clear(wpa_s);
  6035. wpa_s->extra_blacklist_count = 0;
  6036. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  6037. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  6038. wpa_config_flush_blobs(wpa_s->conf);
  6039. wpa_s->conf->auto_interworking = 0;
  6040. wpa_s->conf->okc = 0;
  6041. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  6042. rsn_preauth_deinit(wpa_s->wpa);
  6043. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  6044. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  6045. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  6046. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  6047. radio_remove_works(wpa_s, NULL, 1);
  6048. wpa_s->ext_work_in_progress = 0;
  6049. wpa_s->next_ssid = NULL;
  6050. #ifdef CONFIG_INTERWORKING
  6051. #ifdef CONFIG_HS20
  6052. hs20_cancel_fetch_osu(wpa_s);
  6053. hs20_del_icon(wpa_s, NULL, NULL);
  6054. #endif /* CONFIG_HS20 */
  6055. #endif /* CONFIG_INTERWORKING */
  6056. wpa_s->ext_mgmt_frame_handling = 0;
  6057. wpa_s->ext_eapol_frame_io = 0;
  6058. #ifdef CONFIG_TESTING_OPTIONS
  6059. wpa_s->extra_roc_dur = 0;
  6060. wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
  6061. wpa_s->p2p_go_csa_on_inv = 0;
  6062. wpa_s->ignore_auth_resp = 0;
  6063. wpa_s->ignore_assoc_disallow = 0;
  6064. wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
  6065. #endif /* CONFIG_TESTING_OPTIONS */
  6066. wpa_s->disconnected = 0;
  6067. os_free(wpa_s->next_scan_freqs);
  6068. wpa_s->next_scan_freqs = NULL;
  6069. wpa_bss_flush(wpa_s);
  6070. if (!dl_list_empty(&wpa_s->bss)) {
  6071. wpa_printf(MSG_DEBUG,
  6072. "BSS table not empty after flush: %u entries, current_bss=%p bssid="
  6073. MACSTR " pending_bssid=" MACSTR,
  6074. dl_list_len(&wpa_s->bss), wpa_s->current_bss,
  6075. MAC2STR(wpa_s->bssid),
  6076. MAC2STR(wpa_s->pending_bssid));
  6077. }
  6078. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  6079. wpa_s->wnmsleep_used = 0;
  6080. #ifdef CONFIG_SME
  6081. wpa_s->sme.last_unprot_disconnect.sec = 0;
  6082. #endif /* CONFIG_SME */
  6083. }
  6084. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  6085. char *buf, size_t buflen)
  6086. {
  6087. struct wpa_radio_work *work;
  6088. char *pos, *end;
  6089. struct os_reltime now, diff;
  6090. pos = buf;
  6091. end = buf + buflen;
  6092. os_get_reltime(&now);
  6093. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  6094. {
  6095. int ret;
  6096. os_reltime_sub(&now, &work->time, &diff);
  6097. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  6098. work->type, work->wpa_s->ifname, work->freq,
  6099. work->started, diff.sec, diff.usec);
  6100. if (os_snprintf_error(end - pos, ret))
  6101. break;
  6102. pos += ret;
  6103. }
  6104. return pos - buf;
  6105. }
  6106. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  6107. {
  6108. struct wpa_radio_work *work = eloop_ctx;
  6109. struct wpa_external_work *ework = work->ctx;
  6110. wpa_dbg(work->wpa_s, MSG_DEBUG,
  6111. "Timing out external radio work %u (%s)",
  6112. ework->id, work->type);
  6113. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  6114. work->wpa_s->ext_work_in_progress = 0;
  6115. radio_work_done(work);
  6116. os_free(ework);
  6117. }
  6118. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  6119. {
  6120. struct wpa_external_work *ework = work->ctx;
  6121. if (deinit) {
  6122. if (work->started)
  6123. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6124. work, NULL);
  6125. /*
  6126. * work->type points to a buffer in ework, so need to replace
  6127. * that here with a fixed string to avoid use of freed memory
  6128. * in debug prints.
  6129. */
  6130. work->type = "freed-ext-work";
  6131. work->ctx = NULL;
  6132. os_free(ework);
  6133. return;
  6134. }
  6135. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  6136. ework->id, ework->type);
  6137. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  6138. work->wpa_s->ext_work_in_progress = 1;
  6139. if (!ework->timeout)
  6140. ework->timeout = 10;
  6141. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  6142. work, NULL);
  6143. }
  6144. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  6145. char *buf, size_t buflen)
  6146. {
  6147. struct wpa_external_work *ework;
  6148. char *pos, *pos2;
  6149. size_t type_len;
  6150. int ret;
  6151. unsigned int freq = 0;
  6152. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  6153. ework = os_zalloc(sizeof(*ework));
  6154. if (ework == NULL)
  6155. return -1;
  6156. pos = os_strchr(cmd, ' ');
  6157. if (pos) {
  6158. type_len = pos - cmd;
  6159. pos++;
  6160. pos2 = os_strstr(pos, "freq=");
  6161. if (pos2)
  6162. freq = atoi(pos2 + 5);
  6163. pos2 = os_strstr(pos, "timeout=");
  6164. if (pos2)
  6165. ework->timeout = atoi(pos2 + 8);
  6166. } else {
  6167. type_len = os_strlen(cmd);
  6168. }
  6169. if (4 + type_len >= sizeof(ework->type))
  6170. type_len = sizeof(ework->type) - 4 - 1;
  6171. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  6172. os_memcpy(ework->type + 4, cmd, type_len);
  6173. ework->type[4 + type_len] = '\0';
  6174. wpa_s->ext_work_id++;
  6175. if (wpa_s->ext_work_id == 0)
  6176. wpa_s->ext_work_id++;
  6177. ework->id = wpa_s->ext_work_id;
  6178. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  6179. ework) < 0) {
  6180. os_free(ework);
  6181. return -1;
  6182. }
  6183. ret = os_snprintf(buf, buflen, "%u", ework->id);
  6184. if (os_snprintf_error(buflen, ret))
  6185. return -1;
  6186. return ret;
  6187. }
  6188. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  6189. {
  6190. struct wpa_radio_work *work;
  6191. unsigned int id = atoi(cmd);
  6192. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  6193. {
  6194. struct wpa_external_work *ework;
  6195. if (os_strncmp(work->type, "ext:", 4) != 0)
  6196. continue;
  6197. ework = work->ctx;
  6198. if (id && ework->id != id)
  6199. continue;
  6200. wpa_dbg(wpa_s, MSG_DEBUG,
  6201. "Completed external radio work %u (%s)",
  6202. ework->id, ework->type);
  6203. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  6204. wpa_s->ext_work_in_progress = 0;
  6205. radio_work_done(work);
  6206. os_free(ework);
  6207. return 3; /* "OK\n" */
  6208. }
  6209. return -1;
  6210. }
  6211. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  6212. char *buf, size_t buflen)
  6213. {
  6214. if (os_strcmp(cmd, "show") == 0)
  6215. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  6216. if (os_strncmp(cmd, "add ", 4) == 0)
  6217. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  6218. if (os_strncmp(cmd, "done ", 5) == 0)
  6219. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  6220. return -1;
  6221. }
  6222. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  6223. {
  6224. struct wpa_radio_work *work, *tmp;
  6225. if (!wpa_s || !wpa_s->radio)
  6226. return;
  6227. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  6228. struct wpa_radio_work, list) {
  6229. struct wpa_external_work *ework;
  6230. if (os_strncmp(work->type, "ext:", 4) != 0)
  6231. continue;
  6232. ework = work->ctx;
  6233. wpa_dbg(wpa_s, MSG_DEBUG,
  6234. "Flushing%s external radio work %u (%s)",
  6235. work->started ? " started" : "", ework->id,
  6236. ework->type);
  6237. if (work->started)
  6238. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6239. work, NULL);
  6240. radio_work_done(work);
  6241. os_free(ework);
  6242. }
  6243. }
  6244. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  6245. {
  6246. struct wpa_supplicant *wpa_s = eloop_ctx;
  6247. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  6248. }
  6249. static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
  6250. unsigned int *scan_id_count, int scan_id[])
  6251. {
  6252. const char *pos = value;
  6253. while (pos) {
  6254. if (*pos == ' ' || *pos == '\0')
  6255. break;
  6256. if (*scan_id_count == MAX_SCAN_ID)
  6257. return -1;
  6258. scan_id[(*scan_id_count)++] = atoi(pos);
  6259. pos = os_strchr(pos, ',');
  6260. if (pos)
  6261. pos++;
  6262. }
  6263. return 0;
  6264. }
  6265. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  6266. char *reply, int reply_size, int *reply_len)
  6267. {
  6268. char *pos;
  6269. unsigned int manual_scan_passive = 0;
  6270. unsigned int manual_scan_use_id = 0;
  6271. unsigned int manual_scan_only_new = 0;
  6272. unsigned int scan_only = 0;
  6273. unsigned int scan_id_count = 0;
  6274. int scan_id[MAX_SCAN_ID];
  6275. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  6276. struct wpa_scan_results *scan_res);
  6277. int *manual_scan_freqs = NULL;
  6278. struct wpa_ssid_value *ssid = NULL, *ns;
  6279. unsigned int ssid_count = 0;
  6280. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  6281. *reply_len = -1;
  6282. return;
  6283. }
  6284. if (radio_work_pending(wpa_s, "scan")) {
  6285. wpa_printf(MSG_DEBUG,
  6286. "Pending scan scheduled - reject new request");
  6287. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6288. return;
  6289. }
  6290. #ifdef CONFIG_INTERWORKING
  6291. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
  6292. wpa_printf(MSG_DEBUG,
  6293. "Interworking select in progress - reject new scan");
  6294. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6295. return;
  6296. }
  6297. #endif /* CONFIG_INTERWORKING */
  6298. if (params) {
  6299. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  6300. scan_only = 1;
  6301. pos = os_strstr(params, "freq=");
  6302. if (pos) {
  6303. manual_scan_freqs = freq_range_to_channel_list(wpa_s,
  6304. pos + 5);
  6305. if (manual_scan_freqs == NULL) {
  6306. *reply_len = -1;
  6307. goto done;
  6308. }
  6309. }
  6310. pos = os_strstr(params, "passive=");
  6311. if (pos)
  6312. manual_scan_passive = !!atoi(pos + 8);
  6313. pos = os_strstr(params, "use_id=");
  6314. if (pos)
  6315. manual_scan_use_id = atoi(pos + 7);
  6316. pos = os_strstr(params, "only_new=1");
  6317. if (pos)
  6318. manual_scan_only_new = 1;
  6319. pos = os_strstr(params, "scan_id=");
  6320. if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
  6321. scan_id) < 0) {
  6322. *reply_len = -1;
  6323. goto done;
  6324. }
  6325. pos = params;
  6326. while (pos && *pos != '\0') {
  6327. if (os_strncmp(pos, "ssid ", 5) == 0) {
  6328. char *end;
  6329. pos += 5;
  6330. end = pos;
  6331. while (*end) {
  6332. if (*end == '\0' || *end == ' ')
  6333. break;
  6334. end++;
  6335. }
  6336. ns = os_realloc_array(
  6337. ssid, ssid_count + 1,
  6338. sizeof(struct wpa_ssid_value));
  6339. if (ns == NULL) {
  6340. *reply_len = -1;
  6341. goto done;
  6342. }
  6343. ssid = ns;
  6344. if ((end - pos) & 0x01 ||
  6345. end - pos > 2 * SSID_MAX_LEN ||
  6346. hexstr2bin(pos, ssid[ssid_count].ssid,
  6347. (end - pos) / 2) < 0) {
  6348. wpa_printf(MSG_DEBUG,
  6349. "Invalid SSID value '%s'",
  6350. pos);
  6351. *reply_len = -1;
  6352. goto done;
  6353. }
  6354. ssid[ssid_count].ssid_len = (end - pos) / 2;
  6355. wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
  6356. ssid[ssid_count].ssid,
  6357. ssid[ssid_count].ssid_len);
  6358. ssid_count++;
  6359. pos = end;
  6360. }
  6361. pos = os_strchr(pos, ' ');
  6362. if (pos)
  6363. pos++;
  6364. }
  6365. }
  6366. wpa_s->num_ssids_from_scan_req = ssid_count;
  6367. os_free(wpa_s->ssids_from_scan_req);
  6368. if (ssid_count) {
  6369. wpa_s->ssids_from_scan_req = ssid;
  6370. ssid = NULL;
  6371. } else {
  6372. wpa_s->ssids_from_scan_req = NULL;
  6373. }
  6374. if (scan_only)
  6375. scan_res_handler = scan_only_handler;
  6376. else if (wpa_s->scan_res_handler == scan_only_handler)
  6377. scan_res_handler = NULL;
  6378. else
  6379. scan_res_handler = wpa_s->scan_res_handler;
  6380. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  6381. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  6382. (wpa_s->wpa_state == WPA_COMPLETED))) {
  6383. wpa_s->manual_scan_passive = manual_scan_passive;
  6384. wpa_s->manual_scan_use_id = manual_scan_use_id;
  6385. wpa_s->manual_scan_only_new = manual_scan_only_new;
  6386. wpa_s->scan_id_count = scan_id_count;
  6387. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  6388. wpa_s->scan_res_handler = scan_res_handler;
  6389. os_free(wpa_s->manual_scan_freqs);
  6390. wpa_s->manual_scan_freqs = manual_scan_freqs;
  6391. manual_scan_freqs = NULL;
  6392. wpa_s->normal_scans = 0;
  6393. wpa_s->scan_req = MANUAL_SCAN_REQ;
  6394. wpa_s->after_wps = 0;
  6395. wpa_s->known_wps_freq = 0;
  6396. wpa_supplicant_req_scan(wpa_s, 0, 0);
  6397. if (wpa_s->manual_scan_use_id) {
  6398. wpa_s->manual_scan_id++;
  6399. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  6400. wpa_s->manual_scan_id);
  6401. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  6402. wpa_s->manual_scan_id);
  6403. }
  6404. } else if (wpa_s->sched_scanning) {
  6405. wpa_s->manual_scan_passive = manual_scan_passive;
  6406. wpa_s->manual_scan_use_id = manual_scan_use_id;
  6407. wpa_s->manual_scan_only_new = manual_scan_only_new;
  6408. wpa_s->scan_id_count = scan_id_count;
  6409. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  6410. wpa_s->scan_res_handler = scan_res_handler;
  6411. os_free(wpa_s->manual_scan_freqs);
  6412. wpa_s->manual_scan_freqs = manual_scan_freqs;
  6413. manual_scan_freqs = NULL;
  6414. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  6415. wpa_supplicant_cancel_sched_scan(wpa_s);
  6416. wpa_s->scan_req = MANUAL_SCAN_REQ;
  6417. wpa_supplicant_req_scan(wpa_s, 0, 0);
  6418. if (wpa_s->manual_scan_use_id) {
  6419. wpa_s->manual_scan_id++;
  6420. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  6421. wpa_s->manual_scan_id);
  6422. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  6423. wpa_s->manual_scan_id);
  6424. }
  6425. } else {
  6426. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  6427. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6428. }
  6429. done:
  6430. os_free(manual_scan_freqs);
  6431. os_free(ssid);
  6432. }
  6433. #ifdef CONFIG_TESTING_OPTIONS
  6434. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  6435. unsigned int freq, const u8 *dst,
  6436. const u8 *src, const u8 *bssid,
  6437. const u8 *data, size_t data_len,
  6438. enum offchannel_send_action_result
  6439. result)
  6440. {
  6441. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  6442. " src=" MACSTR " bssid=" MACSTR " result=%s",
  6443. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  6444. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  6445. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  6446. "NO_ACK" : "FAILED"));
  6447. }
  6448. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  6449. {
  6450. char *pos, *param;
  6451. size_t len;
  6452. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  6453. int res, used;
  6454. int freq = 0, no_cck = 0, wait_time = 0;
  6455. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  6456. * <action=Action frame payload> */
  6457. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  6458. pos = cmd;
  6459. used = hwaddr_aton2(pos, da);
  6460. if (used < 0)
  6461. return -1;
  6462. pos += used;
  6463. while (*pos == ' ')
  6464. pos++;
  6465. used = hwaddr_aton2(pos, bssid);
  6466. if (used < 0)
  6467. return -1;
  6468. pos += used;
  6469. param = os_strstr(pos, " freq=");
  6470. if (param) {
  6471. param += 6;
  6472. freq = atoi(param);
  6473. }
  6474. param = os_strstr(pos, " no_cck=");
  6475. if (param) {
  6476. param += 8;
  6477. no_cck = atoi(param);
  6478. }
  6479. param = os_strstr(pos, " wait_time=");
  6480. if (param) {
  6481. param += 11;
  6482. wait_time = atoi(param);
  6483. }
  6484. param = os_strstr(pos, " action=");
  6485. if (param == NULL)
  6486. return -1;
  6487. param += 8;
  6488. len = os_strlen(param);
  6489. if (len & 1)
  6490. return -1;
  6491. len /= 2;
  6492. buf = os_malloc(len);
  6493. if (buf == NULL)
  6494. return -1;
  6495. if (hexstr2bin(param, buf, len) < 0) {
  6496. os_free(buf);
  6497. return -1;
  6498. }
  6499. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  6500. buf, len, wait_time,
  6501. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  6502. os_free(buf);
  6503. return res;
  6504. }
  6505. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  6506. {
  6507. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  6508. offchannel_send_action_done(wpa_s);
  6509. }
  6510. static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
  6511. char *cmd)
  6512. {
  6513. char *pos, *param;
  6514. size_t len;
  6515. u8 *buf;
  6516. int freq = 0, datarate = 0, ssi_signal = 0;
  6517. union wpa_event_data event;
  6518. if (!wpa_s->ext_mgmt_frame_handling)
  6519. return -1;
  6520. /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
  6521. wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
  6522. pos = cmd;
  6523. param = os_strstr(pos, "freq=");
  6524. if (param) {
  6525. param += 5;
  6526. freq = atoi(param);
  6527. }
  6528. param = os_strstr(pos, " datarate=");
  6529. if (param) {
  6530. param += 10;
  6531. datarate = atoi(param);
  6532. }
  6533. param = os_strstr(pos, " ssi_signal=");
  6534. if (param) {
  6535. param += 12;
  6536. ssi_signal = atoi(param);
  6537. }
  6538. param = os_strstr(pos, " frame=");
  6539. if (param == NULL)
  6540. return -1;
  6541. param += 7;
  6542. len = os_strlen(param);
  6543. if (len & 1)
  6544. return -1;
  6545. len /= 2;
  6546. buf = os_malloc(len);
  6547. if (buf == NULL)
  6548. return -1;
  6549. if (hexstr2bin(param, buf, len) < 0) {
  6550. os_free(buf);
  6551. return -1;
  6552. }
  6553. os_memset(&event, 0, sizeof(event));
  6554. event.rx_mgmt.freq = freq;
  6555. event.rx_mgmt.frame = buf;
  6556. event.rx_mgmt.frame_len = len;
  6557. event.rx_mgmt.ssi_signal = ssi_signal;
  6558. event.rx_mgmt.datarate = datarate;
  6559. wpa_s->ext_mgmt_frame_handling = 0;
  6560. wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
  6561. wpa_s->ext_mgmt_frame_handling = 1;
  6562. os_free(buf);
  6563. return 0;
  6564. }
  6565. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  6566. {
  6567. char *pos, *param;
  6568. union wpa_event_data event;
  6569. enum wpa_event_type ev;
  6570. /* <event name> [parameters..] */
  6571. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  6572. pos = cmd;
  6573. param = os_strchr(pos, ' ');
  6574. if (param)
  6575. *param++ = '\0';
  6576. os_memset(&event, 0, sizeof(event));
  6577. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  6578. ev = EVENT_INTERFACE_ENABLED;
  6579. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  6580. ev = EVENT_INTERFACE_DISABLED;
  6581. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  6582. ev = EVENT_AVOID_FREQUENCIES;
  6583. if (param == NULL)
  6584. param = "";
  6585. if (freq_range_list_parse(&event.freq_range, param) < 0)
  6586. return -1;
  6587. wpa_supplicant_event(wpa_s, ev, &event);
  6588. os_free(event.freq_range.range);
  6589. return 0;
  6590. } else {
  6591. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  6592. cmd);
  6593. return -1;
  6594. }
  6595. wpa_supplicant_event(wpa_s, ev, &event);
  6596. return 0;
  6597. }
  6598. static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
  6599. {
  6600. char *pos;
  6601. u8 src[ETH_ALEN], *buf;
  6602. int used;
  6603. size_t len;
  6604. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  6605. pos = cmd;
  6606. used = hwaddr_aton2(pos, src);
  6607. if (used < 0)
  6608. return -1;
  6609. pos += used;
  6610. while (*pos == ' ')
  6611. pos++;
  6612. len = os_strlen(pos);
  6613. if (len & 1)
  6614. return -1;
  6615. len /= 2;
  6616. buf = os_malloc(len);
  6617. if (buf == NULL)
  6618. return -1;
  6619. if (hexstr2bin(pos, buf, len) < 0) {
  6620. os_free(buf);
  6621. return -1;
  6622. }
  6623. wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
  6624. os_free(buf);
  6625. return 0;
  6626. }
  6627. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  6628. {
  6629. size_t i;
  6630. u32 sum = 0;
  6631. const u16 *pos = buf;
  6632. for (i = 0; i < len / 2; i++)
  6633. sum += *pos++;
  6634. while (sum >> 16)
  6635. sum = (sum & 0xffff) + (sum >> 16);
  6636. return sum ^ 0xffff;
  6637. }
  6638. #define HWSIM_PACKETLEN 1500
  6639. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  6640. static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  6641. size_t len)
  6642. {
  6643. struct wpa_supplicant *wpa_s = ctx;
  6644. const struct ether_header *eth;
  6645. struct iphdr ip;
  6646. const u8 *pos;
  6647. unsigned int i;
  6648. if (len != HWSIM_PACKETLEN)
  6649. return;
  6650. eth = (const struct ether_header *) buf;
  6651. os_memcpy(&ip, eth + 1, sizeof(ip));
  6652. pos = &buf[sizeof(*eth) + sizeof(ip)];
  6653. if (ip.ihl != 5 || ip.version != 4 ||
  6654. ntohs(ip.tot_len) != HWSIM_IP_LEN)
  6655. return;
  6656. for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
  6657. if (*pos != (u8) i)
  6658. return;
  6659. pos++;
  6660. }
  6661. wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  6662. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  6663. }
  6664. static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
  6665. char *cmd)
  6666. {
  6667. int enabled = atoi(cmd);
  6668. char *pos;
  6669. const char *ifname;
  6670. if (!enabled) {
  6671. if (wpa_s->l2_test) {
  6672. l2_packet_deinit(wpa_s->l2_test);
  6673. wpa_s->l2_test = NULL;
  6674. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
  6675. }
  6676. return 0;
  6677. }
  6678. if (wpa_s->l2_test)
  6679. return 0;
  6680. pos = os_strstr(cmd, " ifname=");
  6681. if (pos)
  6682. ifname = pos + 8;
  6683. else
  6684. ifname = wpa_s->ifname;
  6685. wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
  6686. ETHERTYPE_IP, wpas_data_test_rx,
  6687. wpa_s, 1);
  6688. if (wpa_s->l2_test == NULL)
  6689. return -1;
  6690. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
  6691. return 0;
  6692. }
  6693. static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
  6694. {
  6695. u8 dst[ETH_ALEN], src[ETH_ALEN];
  6696. char *pos;
  6697. int used;
  6698. long int val;
  6699. u8 tos;
  6700. u8 buf[2 + HWSIM_PACKETLEN];
  6701. struct ether_header *eth;
  6702. struct iphdr *ip;
  6703. u8 *dpos;
  6704. unsigned int i;
  6705. if (wpa_s->l2_test == NULL)
  6706. return -1;
  6707. /* format: <dst> <src> <tos> */
  6708. pos = cmd;
  6709. used = hwaddr_aton2(pos, dst);
  6710. if (used < 0)
  6711. return -1;
  6712. pos += used;
  6713. while (*pos == ' ')
  6714. pos++;
  6715. used = hwaddr_aton2(pos, src);
  6716. if (used < 0)
  6717. return -1;
  6718. pos += used;
  6719. val = strtol(pos, NULL, 0);
  6720. if (val < 0 || val > 0xff)
  6721. return -1;
  6722. tos = val;
  6723. eth = (struct ether_header *) &buf[2];
  6724. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  6725. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  6726. eth->ether_type = htons(ETHERTYPE_IP);
  6727. ip = (struct iphdr *) (eth + 1);
  6728. os_memset(ip, 0, sizeof(*ip));
  6729. ip->ihl = 5;
  6730. ip->version = 4;
  6731. ip->ttl = 64;
  6732. ip->tos = tos;
  6733. ip->tot_len = htons(HWSIM_IP_LEN);
  6734. ip->protocol = 1;
  6735. ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
  6736. ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
  6737. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  6738. dpos = (u8 *) (ip + 1);
  6739. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  6740. *dpos++ = i;
  6741. if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
  6742. HWSIM_PACKETLEN) < 0)
  6743. return -1;
  6744. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
  6745. " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  6746. return 0;
  6747. }
  6748. static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
  6749. char *cmd)
  6750. {
  6751. u8 *buf;
  6752. struct ether_header *eth;
  6753. struct l2_packet_data *l2 = NULL;
  6754. size_t len;
  6755. u16 ethertype;
  6756. int res = -1;
  6757. len = os_strlen(cmd);
  6758. if (len & 1 || len < ETH_HLEN * 2)
  6759. return -1;
  6760. len /= 2;
  6761. buf = os_malloc(len);
  6762. if (buf == NULL)
  6763. return -1;
  6764. if (hexstr2bin(cmd, buf, len) < 0)
  6765. goto done;
  6766. eth = (struct ether_header *) buf;
  6767. ethertype = ntohs(eth->ether_type);
  6768. l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
  6769. wpas_data_test_rx, wpa_s, 1);
  6770. if (l2 == NULL)
  6771. goto done;
  6772. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  6773. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
  6774. done:
  6775. if (l2)
  6776. l2_packet_deinit(l2);
  6777. os_free(buf);
  6778. return res < 0 ? -1 : 0;
  6779. }
  6780. static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
  6781. {
  6782. #ifdef WPA_TRACE_BFD
  6783. char *pos;
  6784. wpa_trace_fail_after = atoi(cmd);
  6785. pos = os_strchr(cmd, ':');
  6786. if (pos) {
  6787. pos++;
  6788. os_strlcpy(wpa_trace_fail_func, pos,
  6789. sizeof(wpa_trace_fail_func));
  6790. } else {
  6791. wpa_trace_fail_after = 0;
  6792. }
  6793. return 0;
  6794. #else /* WPA_TRACE_BFD */
  6795. return -1;
  6796. #endif /* WPA_TRACE_BFD */
  6797. }
  6798. static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
  6799. char *buf, size_t buflen)
  6800. {
  6801. #ifdef WPA_TRACE_BFD
  6802. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
  6803. wpa_trace_fail_func);
  6804. #else /* WPA_TRACE_BFD */
  6805. return -1;
  6806. #endif /* WPA_TRACE_BFD */
  6807. }
  6808. static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
  6809. {
  6810. #ifdef WPA_TRACE_BFD
  6811. char *pos;
  6812. wpa_trace_test_fail_after = atoi(cmd);
  6813. pos = os_strchr(cmd, ':');
  6814. if (pos) {
  6815. pos++;
  6816. os_strlcpy(wpa_trace_test_fail_func, pos,
  6817. sizeof(wpa_trace_test_fail_func));
  6818. } else {
  6819. wpa_trace_test_fail_after = 0;
  6820. }
  6821. return 0;
  6822. #else /* WPA_TRACE_BFD */
  6823. return -1;
  6824. #endif /* WPA_TRACE_BFD */
  6825. }
  6826. static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
  6827. char *buf, size_t buflen)
  6828. {
  6829. #ifdef WPA_TRACE_BFD
  6830. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
  6831. wpa_trace_test_fail_func);
  6832. #else /* WPA_TRACE_BFD */
  6833. return -1;
  6834. #endif /* WPA_TRACE_BFD */
  6835. }
  6836. static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
  6837. {
  6838. struct wpa_supplicant *wpa_s = eloop_ctx;
  6839. int i, count = (intptr_t) timeout_ctx;
  6840. wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
  6841. count);
  6842. for (i = 0; i < count; i++) {
  6843. wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
  6844. i + 1, count);
  6845. }
  6846. }
  6847. static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
  6848. {
  6849. int count;
  6850. count = atoi(cmd);
  6851. if (count <= 0)
  6852. return -1;
  6853. return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
  6854. (void *) (intptr_t) count);
  6855. }
  6856. static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
  6857. const char *cmd)
  6858. {
  6859. struct wpabuf *buf;
  6860. size_t len;
  6861. len = os_strlen(cmd);
  6862. if (len & 1)
  6863. return -1;
  6864. len /= 2;
  6865. if (len == 0) {
  6866. buf = NULL;
  6867. } else {
  6868. buf = wpabuf_alloc(len);
  6869. if (buf == NULL)
  6870. return -1;
  6871. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  6872. wpabuf_free(buf);
  6873. return -1;
  6874. }
  6875. }
  6876. wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
  6877. return 0;
  6878. }
  6879. #endif /* CONFIG_TESTING_OPTIONS */
  6880. static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
  6881. {
  6882. char *pos = cmd;
  6883. int frame;
  6884. size_t len;
  6885. struct wpabuf *buf;
  6886. struct ieee802_11_elems elems;
  6887. frame = atoi(pos);
  6888. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  6889. return -1;
  6890. wpa_s = wpas_vendor_elem(wpa_s, frame);
  6891. pos = os_strchr(pos, ' ');
  6892. if (pos == NULL)
  6893. return -1;
  6894. pos++;
  6895. len = os_strlen(pos);
  6896. if (len == 0)
  6897. return 0;
  6898. if (len & 1)
  6899. return -1;
  6900. len /= 2;
  6901. buf = wpabuf_alloc(len);
  6902. if (buf == NULL)
  6903. return -1;
  6904. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  6905. wpabuf_free(buf);
  6906. return -1;
  6907. }
  6908. if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
  6909. ParseFailed) {
  6910. wpabuf_free(buf);
  6911. return -1;
  6912. }
  6913. if (wpa_s->vendor_elem[frame] == NULL) {
  6914. wpa_s->vendor_elem[frame] = buf;
  6915. wpas_vendor_elem_update(wpa_s);
  6916. return 0;
  6917. }
  6918. if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
  6919. wpabuf_free(buf);
  6920. return -1;
  6921. }
  6922. wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
  6923. wpabuf_free(buf);
  6924. wpas_vendor_elem_update(wpa_s);
  6925. return 0;
  6926. }
  6927. static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
  6928. char *buf, size_t buflen)
  6929. {
  6930. int frame = atoi(cmd);
  6931. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  6932. return -1;
  6933. wpa_s = wpas_vendor_elem(wpa_s, frame);
  6934. if (wpa_s->vendor_elem[frame] == NULL)
  6935. return 0;
  6936. return wpa_snprintf_hex(buf, buflen,
  6937. wpabuf_head_u8(wpa_s->vendor_elem[frame]),
  6938. wpabuf_len(wpa_s->vendor_elem[frame]));
  6939. }
  6940. static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
  6941. {
  6942. char *pos = cmd;
  6943. int frame;
  6944. size_t len;
  6945. u8 *buf;
  6946. struct ieee802_11_elems elems;
  6947. int res;
  6948. frame = atoi(pos);
  6949. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  6950. return -1;
  6951. wpa_s = wpas_vendor_elem(wpa_s, frame);
  6952. pos = os_strchr(pos, ' ');
  6953. if (pos == NULL)
  6954. return -1;
  6955. pos++;
  6956. if (*pos == '*') {
  6957. wpabuf_free(wpa_s->vendor_elem[frame]);
  6958. wpa_s->vendor_elem[frame] = NULL;
  6959. wpas_vendor_elem_update(wpa_s);
  6960. return 0;
  6961. }
  6962. if (wpa_s->vendor_elem[frame] == NULL)
  6963. return -1;
  6964. len = os_strlen(pos);
  6965. if (len == 0)
  6966. return 0;
  6967. if (len & 1)
  6968. return -1;
  6969. len /= 2;
  6970. buf = os_malloc(len);
  6971. if (buf == NULL)
  6972. return -1;
  6973. if (hexstr2bin(pos, buf, len) < 0) {
  6974. os_free(buf);
  6975. return -1;
  6976. }
  6977. if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
  6978. os_free(buf);
  6979. return -1;
  6980. }
  6981. res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
  6982. os_free(buf);
  6983. return res;
  6984. }
  6985. static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
  6986. {
  6987. struct wpa_supplicant *wpa_s = ctx;
  6988. size_t len;
  6989. const u8 *data;
  6990. /*
  6991. * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
  6992. * BSSID[6]
  6993. * BSSID Information[4]
  6994. * Operating Class[1]
  6995. * Channel Number[1]
  6996. * PHY Type[1]
  6997. * Optional Subelements[variable]
  6998. */
  6999. #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
  7000. if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
  7001. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
  7002. goto out;
  7003. }
  7004. data = wpabuf_head_u8(neighbor_rep);
  7005. len = wpabuf_len(neighbor_rep);
  7006. while (len >= 2 + NR_IE_MIN_LEN) {
  7007. const u8 *nr;
  7008. char lci[256 * 2 + 1];
  7009. char civic[256 * 2 + 1];
  7010. u8 nr_len = data[1];
  7011. const u8 *pos = data, *end;
  7012. if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
  7013. nr_len < NR_IE_MIN_LEN) {
  7014. wpa_printf(MSG_DEBUG,
  7015. "CTRL: Invalid Neighbor Report element: id=%u len=%u",
  7016. data[0], nr_len);
  7017. goto out;
  7018. }
  7019. if (2U + nr_len > len) {
  7020. wpa_printf(MSG_DEBUG,
  7021. "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
  7022. data[0], len, nr_len);
  7023. goto out;
  7024. }
  7025. pos += 2;
  7026. end = pos + nr_len;
  7027. nr = pos;
  7028. pos += NR_IE_MIN_LEN;
  7029. lci[0] = '\0';
  7030. civic[0] = '\0';
  7031. while (end - pos > 2) {
  7032. u8 s_id, s_len;
  7033. s_id = *pos++;
  7034. s_len = *pos++;
  7035. if (s_len > end - pos)
  7036. goto out;
  7037. if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
  7038. /* Measurement Token[1] */
  7039. /* Measurement Report Mode[1] */
  7040. /* Measurement Type[1] */
  7041. /* Measurement Report[variable] */
  7042. switch (pos[2]) {
  7043. case MEASURE_TYPE_LCI:
  7044. if (lci[0])
  7045. break;
  7046. wpa_snprintf_hex(lci, sizeof(lci),
  7047. pos, s_len);
  7048. break;
  7049. case MEASURE_TYPE_LOCATION_CIVIC:
  7050. if (civic[0])
  7051. break;
  7052. wpa_snprintf_hex(civic, sizeof(civic),
  7053. pos, s_len);
  7054. break;
  7055. }
  7056. }
  7057. pos += s_len;
  7058. }
  7059. wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
  7060. "bssid=" MACSTR
  7061. " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
  7062. MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
  7063. nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
  7064. nr[ETH_ALEN + 6],
  7065. lci[0] ? " lci=" : "", lci,
  7066. civic[0] ? " civic=" : "", civic);
  7067. data = end;
  7068. len -= 2 + nr_len;
  7069. }
  7070. out:
  7071. wpabuf_free(neighbor_rep);
  7072. }
  7073. static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
  7074. char *cmd)
  7075. {
  7076. struct wpa_ssid_value ssid, *ssid_p = NULL;
  7077. int ret, lci = 0, civic = 0;
  7078. char *ssid_s;
  7079. ssid_s = os_strstr(cmd, "ssid=");
  7080. if (ssid_s) {
  7081. if (ssid_parse(ssid_s + 5, &ssid)) {
  7082. wpa_printf(MSG_ERROR,
  7083. "CTRL: Send Neighbor Report: bad SSID");
  7084. return -1;
  7085. }
  7086. ssid_p = &ssid;
  7087. /*
  7088. * Move cmd after the SSID text that may include "lci" or
  7089. * "civic".
  7090. */
  7091. cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
  7092. if (cmd)
  7093. cmd++;
  7094. }
  7095. if (cmd && os_strstr(cmd, "lci"))
  7096. lci = 1;
  7097. if (cmd && os_strstr(cmd, "civic"))
  7098. civic = 1;
  7099. ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
  7100. wpas_ctrl_neighbor_rep_cb,
  7101. wpa_s);
  7102. return ret;
  7103. }
  7104. static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
  7105. {
  7106. eapol_sm_erp_flush(wpa_s->eapol);
  7107. return 0;
  7108. }
  7109. static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
  7110. char *cmd)
  7111. {
  7112. char *token, *context = NULL;
  7113. unsigned int enable = ~0, type = 0;
  7114. u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
  7115. u8 *addr = NULL, *mask = NULL;
  7116. while ((token = str_token(cmd, " ", &context))) {
  7117. if (os_strcasecmp(token, "scan") == 0) {
  7118. type |= MAC_ADDR_RAND_SCAN;
  7119. } else if (os_strcasecmp(token, "sched") == 0) {
  7120. type |= MAC_ADDR_RAND_SCHED_SCAN;
  7121. } else if (os_strcasecmp(token, "pno") == 0) {
  7122. type |= MAC_ADDR_RAND_PNO;
  7123. } else if (os_strcasecmp(token, "all") == 0) {
  7124. type = wpa_s->mac_addr_rand_supported;
  7125. } else if (os_strncasecmp(token, "enable=", 7) == 0) {
  7126. enable = atoi(token + 7);
  7127. } else if (os_strncasecmp(token, "addr=", 5) == 0) {
  7128. addr = _addr;
  7129. if (hwaddr_aton(token + 5, addr)) {
  7130. wpa_printf(MSG_INFO,
  7131. "CTRL: Invalid MAC address: %s",
  7132. token);
  7133. return -1;
  7134. }
  7135. } else if (os_strncasecmp(token, "mask=", 5) == 0) {
  7136. mask = _mask;
  7137. if (hwaddr_aton(token + 5, mask)) {
  7138. wpa_printf(MSG_INFO,
  7139. "CTRL: Invalid MAC address mask: %s",
  7140. token);
  7141. return -1;
  7142. }
  7143. } else {
  7144. wpa_printf(MSG_INFO,
  7145. "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
  7146. token);
  7147. return -1;
  7148. }
  7149. }
  7150. if (!type) {
  7151. wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
  7152. return -1;
  7153. }
  7154. if ((wpa_s->mac_addr_rand_supported & type) != type) {
  7155. wpa_printf(MSG_INFO,
  7156. "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
  7157. type, wpa_s->mac_addr_rand_supported);
  7158. return -1;
  7159. }
  7160. if (enable > 1) {
  7161. wpa_printf(MSG_INFO,
  7162. "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
  7163. return -1;
  7164. }
  7165. if (!enable) {
  7166. wpas_mac_addr_rand_scan_clear(wpa_s, type);
  7167. if (wpa_s->pno) {
  7168. if (type & MAC_ADDR_RAND_PNO) {
  7169. wpas_stop_pno(wpa_s);
  7170. wpas_start_pno(wpa_s);
  7171. }
  7172. } else if (wpa_s->sched_scanning &&
  7173. (type & MAC_ADDR_RAND_SCHED_SCAN)) {
  7174. /* simulate timeout to restart the sched scan */
  7175. wpa_s->sched_scan_timed_out = 1;
  7176. wpa_s->prev_sched_ssid = NULL;
  7177. wpa_supplicant_cancel_sched_scan(wpa_s);
  7178. }
  7179. return 0;
  7180. }
  7181. if ((addr && !mask) || (!addr && mask)) {
  7182. wpa_printf(MSG_INFO,
  7183. "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
  7184. return -1;
  7185. }
  7186. if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
  7187. wpa_printf(MSG_INFO,
  7188. "CTRL: MAC_RAND_SCAN cannot allow multicast address");
  7189. return -1;
  7190. }
  7191. if (type & MAC_ADDR_RAND_SCAN) {
  7192. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
  7193. addr, mask);
  7194. }
  7195. if (type & MAC_ADDR_RAND_SCHED_SCAN) {
  7196. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
  7197. addr, mask);
  7198. if (wpa_s->sched_scanning && !wpa_s->pno) {
  7199. /* simulate timeout to restart the sched scan */
  7200. wpa_s->sched_scan_timed_out = 1;
  7201. wpa_s->prev_sched_ssid = NULL;
  7202. wpa_supplicant_cancel_sched_scan(wpa_s);
  7203. }
  7204. }
  7205. if (type & MAC_ADDR_RAND_PNO) {
  7206. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
  7207. addr, mask);
  7208. if (wpa_s->pno) {
  7209. wpas_stop_pno(wpa_s);
  7210. wpas_start_pno(wpa_s);
  7211. }
  7212. }
  7213. return 0;
  7214. }
  7215. static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
  7216. char *buf, size_t buflen)
  7217. {
  7218. size_t reply_len;
  7219. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
  7220. #ifdef CONFIG_AP
  7221. reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
  7222. buflen - reply_len);
  7223. #endif /* CONFIG_AP */
  7224. return reply_len;
  7225. }
  7226. static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
  7227. {
  7228. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  7229. #ifdef CONFIG_AP
  7230. wpas_ap_pmksa_cache_flush(wpa_s);
  7231. #endif /* CONFIG_AP */
  7232. }
  7233. static int wpas_ctrl_cmd_debug_level(const char *cmd)
  7234. {
  7235. if (os_strcmp(cmd, "PING") == 0 ||
  7236. os_strncmp(cmd, "BSS ", 4) == 0 ||
  7237. os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
  7238. os_strncmp(cmd, "STATUS", 6) == 0 ||
  7239. os_strncmp(cmd, "STA ", 4) == 0 ||
  7240. os_strncmp(cmd, "STA-", 4) == 0)
  7241. return MSG_EXCESSIVE;
  7242. return MSG_DEBUG;
  7243. }
  7244. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  7245. char *buf, size_t *resp_len)
  7246. {
  7247. char *reply;
  7248. const int reply_size = 4096;
  7249. int reply_len;
  7250. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  7251. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  7252. if (wpa_debug_show_keys)
  7253. wpa_dbg(wpa_s, MSG_DEBUG,
  7254. "Control interface command '%s'", buf);
  7255. else
  7256. wpa_dbg(wpa_s, MSG_DEBUG,
  7257. "Control interface command '%s [REMOVED]'",
  7258. os_strncmp(buf, WPA_CTRL_RSP,
  7259. os_strlen(WPA_CTRL_RSP)) == 0 ?
  7260. WPA_CTRL_RSP : "SET_NETWORK");
  7261. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  7262. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  7263. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  7264. (const u8 *) buf, os_strlen(buf));
  7265. } else {
  7266. int level = wpas_ctrl_cmd_debug_level(buf);
  7267. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  7268. }
  7269. reply = os_malloc(reply_size);
  7270. if (reply == NULL) {
  7271. *resp_len = 1;
  7272. return NULL;
  7273. }
  7274. os_memcpy(reply, "OK\n", 3);
  7275. reply_len = 3;
  7276. if (os_strcmp(buf, "PING") == 0) {
  7277. os_memcpy(reply, "PONG\n", 5);
  7278. reply_len = 5;
  7279. } else if (os_strcmp(buf, "IFNAME") == 0) {
  7280. reply_len = os_strlen(wpa_s->ifname);
  7281. os_memcpy(reply, wpa_s->ifname, reply_len);
  7282. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  7283. if (wpa_debug_reopen_file() < 0)
  7284. reply_len = -1;
  7285. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  7286. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  7287. } else if (os_strcmp(buf, "MIB") == 0) {
  7288. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  7289. if (reply_len >= 0) {
  7290. reply_len += eapol_sm_get_mib(wpa_s->eapol,
  7291. reply + reply_len,
  7292. reply_size - reply_len);
  7293. }
  7294. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  7295. reply_len = wpa_supplicant_ctrl_iface_status(
  7296. wpa_s, buf + 6, reply, reply_size);
  7297. } else if (os_strcmp(buf, "PMKSA") == 0) {
  7298. reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
  7299. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  7300. wpas_ctrl_iface_pmksa_flush(wpa_s);
  7301. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  7302. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  7303. reply_len = -1;
  7304. } else if (os_strncmp(buf, "DUMP", 4) == 0) {
  7305. reply_len = wpa_config_dump_values(wpa_s->conf,
  7306. reply, reply_size);
  7307. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  7308. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  7309. reply, reply_size);
  7310. } else if (os_strcmp(buf, "LOGON") == 0) {
  7311. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  7312. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  7313. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  7314. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  7315. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  7316. reply_len = -1;
  7317. else
  7318. wpas_request_connection(wpa_s);
  7319. } else if (os_strcmp(buf, "REATTACH") == 0) {
  7320. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  7321. !wpa_s->current_ssid)
  7322. reply_len = -1;
  7323. else {
  7324. wpa_s->reattach = 1;
  7325. wpas_request_connection(wpa_s);
  7326. }
  7327. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  7328. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  7329. reply_len = -1;
  7330. else if (wpa_s->disconnected)
  7331. wpas_request_connection(wpa_s);
  7332. #ifdef IEEE8021X_EAPOL
  7333. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  7334. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  7335. reply_len = -1;
  7336. #endif /* IEEE8021X_EAPOL */
  7337. #ifdef CONFIG_PEERKEY
  7338. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  7339. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  7340. reply_len = -1;
  7341. #endif /* CONFIG_PEERKEY */
  7342. #ifdef CONFIG_IEEE80211R
  7343. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  7344. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  7345. reply_len = -1;
  7346. #endif /* CONFIG_IEEE80211R */
  7347. #ifdef CONFIG_WPS
  7348. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  7349. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  7350. if (res == -2) {
  7351. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  7352. reply_len = 17;
  7353. } else if (res)
  7354. reply_len = -1;
  7355. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  7356. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  7357. if (res == -2) {
  7358. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  7359. reply_len = 17;
  7360. } else if (res)
  7361. reply_len = -1;
  7362. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  7363. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  7364. reply,
  7365. reply_size);
  7366. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  7367. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  7368. wpa_s, buf + 14, reply, reply_size);
  7369. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  7370. if (wpas_wps_cancel(wpa_s))
  7371. reply_len = -1;
  7372. #ifdef CONFIG_WPS_NFC
  7373. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  7374. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  7375. reply_len = -1;
  7376. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  7377. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  7378. reply_len = -1;
  7379. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  7380. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  7381. wpa_s, buf + 21, reply, reply_size);
  7382. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  7383. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  7384. wpa_s, buf + 14, reply, reply_size);
  7385. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  7386. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  7387. buf + 17))
  7388. reply_len = -1;
  7389. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  7390. reply_len = wpas_ctrl_nfc_get_handover_req(
  7391. wpa_s, buf + 21, reply, reply_size);
  7392. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  7393. reply_len = wpas_ctrl_nfc_get_handover_sel(
  7394. wpa_s, buf + 21, reply, reply_size);
  7395. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  7396. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  7397. reply_len = -1;
  7398. #endif /* CONFIG_WPS_NFC */
  7399. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  7400. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  7401. reply_len = -1;
  7402. #ifdef CONFIG_AP
  7403. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  7404. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  7405. wpa_s, buf + 11, reply, reply_size);
  7406. #endif /* CONFIG_AP */
  7407. #ifdef CONFIG_WPS_ER
  7408. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  7409. if (wpas_wps_er_start(wpa_s, NULL))
  7410. reply_len = -1;
  7411. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  7412. if (wpas_wps_er_start(wpa_s, buf + 13))
  7413. reply_len = -1;
  7414. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  7415. wpas_wps_er_stop(wpa_s);
  7416. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  7417. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  7418. reply_len = -1;
  7419. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  7420. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  7421. if (ret == -2) {
  7422. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  7423. reply_len = 17;
  7424. } else if (ret == -3) {
  7425. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  7426. reply_len = 18;
  7427. } else if (ret == -4) {
  7428. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  7429. reply_len = 20;
  7430. } else if (ret)
  7431. reply_len = -1;
  7432. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  7433. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  7434. reply_len = -1;
  7435. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  7436. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  7437. buf + 18))
  7438. reply_len = -1;
  7439. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  7440. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  7441. reply_len = -1;
  7442. #ifdef CONFIG_WPS_NFC
  7443. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  7444. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  7445. wpa_s, buf + 24, reply, reply_size);
  7446. #endif /* CONFIG_WPS_NFC */
  7447. #endif /* CONFIG_WPS_ER */
  7448. #endif /* CONFIG_WPS */
  7449. #ifdef CONFIG_IBSS_RSN
  7450. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  7451. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  7452. reply_len = -1;
  7453. #endif /* CONFIG_IBSS_RSN */
  7454. #ifdef CONFIG_MESH
  7455. } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
  7456. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  7457. wpa_s, buf + 19, reply, reply_size);
  7458. } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
  7459. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  7460. wpa_s, "", reply, reply_size);
  7461. } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
  7462. if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
  7463. reply_len = -1;
  7464. } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
  7465. if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
  7466. buf + 18))
  7467. reply_len = -1;
  7468. } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
  7469. if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
  7470. reply_len = -1;
  7471. } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
  7472. if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
  7473. reply_len = -1;
  7474. #endif /* CONFIG_MESH */
  7475. #ifdef CONFIG_P2P
  7476. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  7477. if (p2p_ctrl_find(wpa_s, buf + 8))
  7478. reply_len = -1;
  7479. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  7480. if (p2p_ctrl_find(wpa_s, ""))
  7481. reply_len = -1;
  7482. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  7483. wpas_p2p_stop_find(wpa_s);
  7484. } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
  7485. if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
  7486. reply_len = -1;
  7487. } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
  7488. if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
  7489. reply_len = -1;
  7490. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  7491. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  7492. reply_size);
  7493. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  7494. if (p2p_ctrl_listen(wpa_s, buf + 11))
  7495. reply_len = -1;
  7496. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  7497. if (p2p_ctrl_listen(wpa_s, ""))
  7498. reply_len = -1;
  7499. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  7500. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  7501. reply_len = -1;
  7502. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  7503. if (p2p_ctrl_group_add(wpa_s, ""))
  7504. reply_len = -1;
  7505. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  7506. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  7507. reply_len = -1;
  7508. } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
  7509. reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
  7510. reply_size);
  7511. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  7512. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  7513. reply_len = -1;
  7514. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  7515. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  7516. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  7517. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  7518. reply_size);
  7519. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  7520. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  7521. reply_len = -1;
  7522. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  7523. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  7524. reply_len = -1;
  7525. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  7526. wpas_p2p_sd_service_update(wpa_s);
  7527. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  7528. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  7529. reply_len = -1;
  7530. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  7531. wpas_p2p_service_flush(wpa_s);
  7532. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  7533. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  7534. reply_len = -1;
  7535. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  7536. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  7537. reply_len = -1;
  7538. } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
  7539. if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
  7540. reply_len = -1;
  7541. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  7542. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  7543. reply_len = -1;
  7544. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  7545. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  7546. reply_len = -1;
  7547. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  7548. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  7549. reply_size);
  7550. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  7551. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  7552. reply_len = -1;
  7553. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  7554. p2p_ctrl_flush(wpa_s);
  7555. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  7556. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  7557. reply_len = -1;
  7558. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  7559. if (wpas_p2p_cancel(wpa_s))
  7560. reply_len = -1;
  7561. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  7562. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  7563. reply_len = -1;
  7564. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  7565. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  7566. reply_len = -1;
  7567. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  7568. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  7569. reply_len = -1;
  7570. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  7571. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  7572. reply_len = -1;
  7573. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  7574. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  7575. reply_len = -1;
  7576. } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
  7577. if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
  7578. reply_len = -1;
  7579. } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
  7580. if (wpas_p2p_lo_stop(wpa_s))
  7581. reply_len = -1;
  7582. #endif /* CONFIG_P2P */
  7583. #ifdef CONFIG_WIFI_DISPLAY
  7584. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  7585. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  7586. reply_len = -1;
  7587. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  7588. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  7589. reply, reply_size);
  7590. #endif /* CONFIG_WIFI_DISPLAY */
  7591. #ifdef CONFIG_INTERWORKING
  7592. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  7593. if (interworking_fetch_anqp(wpa_s) < 0)
  7594. reply_len = -1;
  7595. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  7596. interworking_stop_fetch_anqp(wpa_s);
  7597. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  7598. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  7599. reply_len = -1;
  7600. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  7601. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  7602. reply_len = -1;
  7603. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  7604. if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
  7605. reply_len = -1;
  7606. } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
  7607. int id;
  7608. id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
  7609. if (id < 0)
  7610. reply_len = -1;
  7611. else {
  7612. reply_len = os_snprintf(reply, reply_size, "%d\n", id);
  7613. if (os_snprintf_error(reply_size, reply_len))
  7614. reply_len = -1;
  7615. }
  7616. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  7617. if (get_anqp(wpa_s, buf + 9) < 0)
  7618. reply_len = -1;
  7619. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  7620. if (gas_request(wpa_s, buf + 12) < 0)
  7621. reply_len = -1;
  7622. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  7623. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  7624. reply_size);
  7625. #endif /* CONFIG_INTERWORKING */
  7626. #ifdef CONFIG_HS20
  7627. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  7628. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  7629. reply_len = -1;
  7630. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  7631. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  7632. reply_len = -1;
  7633. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  7634. if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
  7635. reply_len = -1;
  7636. } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
  7637. if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
  7638. reply_len = -1;
  7639. } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
  7640. reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
  7641. } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
  7642. if (del_hs20_icon(wpa_s, buf + 14) < 0)
  7643. reply_len = -1;
  7644. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  7645. if (hs20_fetch_osu(wpa_s, 0) < 0)
  7646. reply_len = -1;
  7647. } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
  7648. if (hs20_fetch_osu(wpa_s, 1) < 0)
  7649. reply_len = -1;
  7650. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  7651. hs20_cancel_fetch_osu(wpa_s);
  7652. #endif /* CONFIG_HS20 */
  7653. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  7654. {
  7655. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  7656. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  7657. reply_len = -1;
  7658. else {
  7659. /*
  7660. * Notify response from timeout to allow the control
  7661. * interface response to be sent first.
  7662. */
  7663. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  7664. wpa_s, NULL);
  7665. }
  7666. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  7667. if (wpa_supplicant_reload_configuration(wpa_s))
  7668. reply_len = -1;
  7669. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  7670. wpa_supplicant_terminate_proc(wpa_s->global);
  7671. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  7672. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  7673. reply_len = -1;
  7674. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  7675. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  7676. wpa_s, buf + 9, reply, reply_size);
  7677. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  7678. reply_len = wpa_supplicant_ctrl_iface_log_level(
  7679. wpa_s, buf + 9, reply, reply_size);
  7680. } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
  7681. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  7682. wpa_s, buf + 14, reply, reply_size);
  7683. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  7684. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  7685. wpa_s, NULL, reply, reply_size);
  7686. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  7687. wpas_request_disconnection(wpa_s);
  7688. } else if (os_strcmp(buf, "SCAN") == 0) {
  7689. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  7690. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  7691. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  7692. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  7693. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  7694. wpa_s, reply, reply_size);
  7695. } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
  7696. if (wpas_abort_ongoing_scan(wpa_s) < 0)
  7697. reply_len = -1;
  7698. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  7699. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  7700. reply_len = -1;
  7701. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  7702. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  7703. reply_len = -1;
  7704. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  7705. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  7706. reply_len = -1;
  7707. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  7708. reply_len = wpa_supplicant_ctrl_iface_add_network(
  7709. wpa_s, reply, reply_size);
  7710. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  7711. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  7712. reply_len = -1;
  7713. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  7714. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  7715. reply_len = -1;
  7716. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  7717. reply_len = wpa_supplicant_ctrl_iface_get_network(
  7718. wpa_s, buf + 12, reply, reply_size);
  7719. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  7720. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
  7721. wpa_s))
  7722. reply_len = -1;
  7723. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  7724. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  7725. wpa_s, reply, reply_size);
  7726. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  7727. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  7728. wpa_s, reply, reply_size);
  7729. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  7730. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  7731. reply_len = -1;
  7732. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  7733. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  7734. reply_len = -1;
  7735. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  7736. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  7737. reply,
  7738. reply_size);
  7739. #ifndef CONFIG_NO_CONFIG_WRITE
  7740. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  7741. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  7742. reply_len = -1;
  7743. #endif /* CONFIG_NO_CONFIG_WRITE */
  7744. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  7745. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  7746. wpa_s, buf + 15, reply, reply_size);
  7747. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  7748. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  7749. reply_len = -1;
  7750. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  7751. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  7752. reply_len = -1;
  7753. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  7754. reply_len = wpa_supplicant_global_iface_list(
  7755. wpa_s->global, reply, reply_size);
  7756. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  7757. reply_len = wpa_supplicant_global_iface_interfaces(
  7758. wpa_s->global, buf + 10, reply, reply_size);
  7759. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  7760. reply_len = wpa_supplicant_ctrl_iface_bss(
  7761. wpa_s, buf + 4, reply, reply_size);
  7762. #ifdef CONFIG_AP
  7763. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  7764. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  7765. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  7766. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  7767. reply_size);
  7768. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  7769. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  7770. reply_size);
  7771. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  7772. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  7773. reply_len = -1;
  7774. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  7775. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  7776. reply_len = -1;
  7777. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  7778. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  7779. reply_len = -1;
  7780. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  7781. if (wpas_ap_stop_ap(wpa_s))
  7782. reply_len = -1;
  7783. #endif /* CONFIG_AP */
  7784. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  7785. wpas_notify_suspend(wpa_s->global);
  7786. } else if (os_strcmp(buf, "RESUME") == 0) {
  7787. wpas_notify_resume(wpa_s->global);
  7788. #ifdef CONFIG_TESTING_OPTIONS
  7789. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  7790. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  7791. #endif /* CONFIG_TESTING_OPTIONS */
  7792. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  7793. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  7794. reply_len = -1;
  7795. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  7796. wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
  7797. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  7798. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  7799. reply_len = -1;
  7800. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  7801. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  7802. buf + 17))
  7803. reply_len = -1;
  7804. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  7805. wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
  7806. #ifdef CONFIG_TDLS
  7807. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  7808. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  7809. reply_len = -1;
  7810. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  7811. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  7812. reply_len = -1;
  7813. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  7814. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  7815. reply_len = -1;
  7816. } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
  7817. if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
  7818. buf + 17))
  7819. reply_len = -1;
  7820. } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
  7821. if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
  7822. buf + 24))
  7823. reply_len = -1;
  7824. } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
  7825. reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
  7826. wpa_s, buf + 17, reply, reply_size);
  7827. #endif /* CONFIG_TDLS */
  7828. } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
  7829. reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
  7830. } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
  7831. if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
  7832. reply_len = -1;
  7833. } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
  7834. if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
  7835. reply_len = -1;
  7836. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  7837. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  7838. reply_size);
  7839. } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
  7840. if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
  7841. reply_len = -1;
  7842. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  7843. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  7844. reply_size);
  7845. #ifdef CONFIG_AUTOSCAN
  7846. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  7847. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  7848. reply_len = -1;
  7849. #endif /* CONFIG_AUTOSCAN */
  7850. } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
  7851. reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
  7852. reply_size);
  7853. #ifdef ANDROID
  7854. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  7855. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  7856. reply_size);
  7857. #endif /* ANDROID */
  7858. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  7859. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  7860. reply_size);
  7861. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  7862. pmksa_cache_clear_current(wpa_s->wpa);
  7863. eapol_sm_request_reauth(wpa_s->eapol);
  7864. #ifdef CONFIG_WNM
  7865. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  7866. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  7867. reply_len = -1;
  7868. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
  7869. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
  7870. reply_len = -1;
  7871. #endif /* CONFIG_WNM */
  7872. } else if (os_strcmp(buf, "FLUSH") == 0) {
  7873. wpa_supplicant_ctrl_iface_flush(wpa_s);
  7874. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  7875. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  7876. reply_size);
  7877. #ifdef CONFIG_TESTING_OPTIONS
  7878. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  7879. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  7880. reply_len = -1;
  7881. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  7882. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  7883. } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
  7884. if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
  7885. reply_len = -1;
  7886. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  7887. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  7888. reply_len = -1;
  7889. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  7890. if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
  7891. reply_len = -1;
  7892. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  7893. if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
  7894. reply_len = -1;
  7895. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  7896. if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
  7897. reply_len = -1;
  7898. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  7899. if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
  7900. reply_len = -1;
  7901. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  7902. if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
  7903. reply_len = -1;
  7904. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  7905. reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
  7906. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  7907. if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
  7908. reply_len = -1;
  7909. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  7910. reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
  7911. } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
  7912. if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
  7913. reply_len = -1;
  7914. } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
  7915. if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
  7916. reply_len = -1;
  7917. #endif /* CONFIG_TESTING_OPTIONS */
  7918. } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
  7919. if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
  7920. reply_len = -1;
  7921. } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
  7922. reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
  7923. reply_size);
  7924. } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
  7925. if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
  7926. reply_len = -1;
  7927. } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
  7928. if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
  7929. reply_len = -1;
  7930. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  7931. wpas_ctrl_iface_erp_flush(wpa_s);
  7932. } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
  7933. if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
  7934. reply_len = -1;
  7935. } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
  7936. reply_len = wpas_ctrl_iface_get_pref_freq_list(
  7937. wpa_s, buf + 19, reply, reply_size);
  7938. } else {
  7939. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  7940. reply_len = 16;
  7941. }
  7942. if (reply_len < 0) {
  7943. os_memcpy(reply, "FAIL\n", 5);
  7944. reply_len = 5;
  7945. }
  7946. *resp_len = reply_len;
  7947. return reply;
  7948. }
  7949. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  7950. char *cmd)
  7951. {
  7952. struct wpa_interface iface;
  7953. char *pos, *extra;
  7954. struct wpa_supplicant *wpa_s;
  7955. unsigned int create_iface = 0;
  7956. u8 mac_addr[ETH_ALEN];
  7957. enum wpa_driver_if_type type = WPA_IF_STATION;
  7958. /*
  7959. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  7960. * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
  7961. */
  7962. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  7963. os_memset(&iface, 0, sizeof(iface));
  7964. do {
  7965. iface.ifname = pos = cmd;
  7966. pos = os_strchr(pos, '\t');
  7967. if (pos)
  7968. *pos++ = '\0';
  7969. if (iface.ifname[0] == '\0')
  7970. return -1;
  7971. if (pos == NULL)
  7972. break;
  7973. iface.confname = pos;
  7974. pos = os_strchr(pos, '\t');
  7975. if (pos)
  7976. *pos++ = '\0';
  7977. if (iface.confname[0] == '\0')
  7978. iface.confname = NULL;
  7979. if (pos == NULL)
  7980. break;
  7981. iface.driver = pos;
  7982. pos = os_strchr(pos, '\t');
  7983. if (pos)
  7984. *pos++ = '\0';
  7985. if (iface.driver[0] == '\0')
  7986. iface.driver = NULL;
  7987. if (pos == NULL)
  7988. break;
  7989. iface.ctrl_interface = pos;
  7990. pos = os_strchr(pos, '\t');
  7991. if (pos)
  7992. *pos++ = '\0';
  7993. if (iface.ctrl_interface[0] == '\0')
  7994. iface.ctrl_interface = NULL;
  7995. if (pos == NULL)
  7996. break;
  7997. iface.driver_param = pos;
  7998. pos = os_strchr(pos, '\t');
  7999. if (pos)
  8000. *pos++ = '\0';
  8001. if (iface.driver_param[0] == '\0')
  8002. iface.driver_param = NULL;
  8003. if (pos == NULL)
  8004. break;
  8005. iface.bridge_ifname = pos;
  8006. pos = os_strchr(pos, '\t');
  8007. if (pos)
  8008. *pos++ = '\0';
  8009. if (iface.bridge_ifname[0] == '\0')
  8010. iface.bridge_ifname = NULL;
  8011. if (pos == NULL)
  8012. break;
  8013. extra = pos;
  8014. pos = os_strchr(pos, '\t');
  8015. if (pos)
  8016. *pos++ = '\0';
  8017. if (!extra[0])
  8018. break;
  8019. if (os_strcmp(extra, "create") == 0) {
  8020. create_iface = 1;
  8021. if (!pos)
  8022. break;
  8023. if (os_strcmp(pos, "sta") == 0) {
  8024. type = WPA_IF_STATION;
  8025. } else if (os_strcmp(pos, "ap") == 0) {
  8026. type = WPA_IF_AP_BSS;
  8027. } else {
  8028. wpa_printf(MSG_DEBUG,
  8029. "INTERFACE_ADD unsupported interface type: '%s'",
  8030. pos);
  8031. return -1;
  8032. }
  8033. } else {
  8034. wpa_printf(MSG_DEBUG,
  8035. "INTERFACE_ADD unsupported extra parameter: '%s'",
  8036. extra);
  8037. return -1;
  8038. }
  8039. } while (0);
  8040. if (create_iface) {
  8041. wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
  8042. iface.ifname);
  8043. if (!global->ifaces)
  8044. return -1;
  8045. if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
  8046. NULL, NULL, NULL, mac_addr, NULL) < 0) {
  8047. wpa_printf(MSG_ERROR,
  8048. "CTRL_IFACE interface creation failed");
  8049. return -1;
  8050. }
  8051. wpa_printf(MSG_DEBUG,
  8052. "CTRL_IFACE interface '%s' created with MAC addr: "
  8053. MACSTR, iface.ifname, MAC2STR(mac_addr));
  8054. }
  8055. if (wpa_supplicant_get_iface(global, iface.ifname))
  8056. goto fail;
  8057. wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
  8058. if (!wpa_s)
  8059. goto fail;
  8060. wpa_s->added_vif = create_iface;
  8061. return 0;
  8062. fail:
  8063. if (create_iface)
  8064. wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
  8065. return -1;
  8066. }
  8067. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  8068. char *cmd)
  8069. {
  8070. struct wpa_supplicant *wpa_s;
  8071. int ret;
  8072. unsigned int delete_iface;
  8073. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  8074. wpa_s = wpa_supplicant_get_iface(global, cmd);
  8075. if (wpa_s == NULL)
  8076. return -1;
  8077. delete_iface = wpa_s->added_vif;
  8078. ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
  8079. if (!ret && delete_iface) {
  8080. wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
  8081. cmd);
  8082. ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
  8083. }
  8084. return ret;
  8085. }
  8086. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  8087. {
  8088. struct wpa_interface_info *prev;
  8089. while (iface) {
  8090. prev = iface;
  8091. iface = iface->next;
  8092. os_free(prev->ifname);
  8093. os_free(prev->desc);
  8094. os_free(prev);
  8095. }
  8096. }
  8097. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  8098. char *buf, int len)
  8099. {
  8100. int i, res;
  8101. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  8102. char *pos, *end;
  8103. for (i = 0; wpa_drivers[i]; i++) {
  8104. const struct wpa_driver_ops *drv = wpa_drivers[i];
  8105. if (drv->get_interfaces == NULL)
  8106. continue;
  8107. tmp = drv->get_interfaces(global->drv_priv[i]);
  8108. if (tmp == NULL)
  8109. continue;
  8110. if (last == NULL)
  8111. iface = last = tmp;
  8112. else
  8113. last->next = tmp;
  8114. while (last->next)
  8115. last = last->next;
  8116. }
  8117. pos = buf;
  8118. end = buf + len;
  8119. for (tmp = iface; tmp; tmp = tmp->next) {
  8120. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  8121. tmp->drv_name, tmp->ifname,
  8122. tmp->desc ? tmp->desc : "");
  8123. if (os_snprintf_error(end - pos, res)) {
  8124. *pos = '\0';
  8125. break;
  8126. }
  8127. pos += res;
  8128. }
  8129. wpa_free_iface_info(iface);
  8130. return pos - buf;
  8131. }
  8132. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  8133. const char *input,
  8134. char *buf, int len)
  8135. {
  8136. int res;
  8137. char *pos, *end;
  8138. struct wpa_supplicant *wpa_s;
  8139. int show_ctrl = 0;
  8140. if (input)
  8141. show_ctrl = !!os_strstr(input, "ctrl");
  8142. wpa_s = global->ifaces;
  8143. pos = buf;
  8144. end = buf + len;
  8145. while (wpa_s) {
  8146. if (show_ctrl)
  8147. res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
  8148. wpa_s->ifname,
  8149. wpa_s->conf->ctrl_interface ?
  8150. wpa_s->conf->ctrl_interface : "N/A");
  8151. else
  8152. res = os_snprintf(pos, end - pos, "%s\n",
  8153. wpa_s->ifname);
  8154. if (os_snprintf_error(end - pos, res)) {
  8155. *pos = '\0';
  8156. break;
  8157. }
  8158. pos += res;
  8159. wpa_s = wpa_s->next;
  8160. }
  8161. return pos - buf;
  8162. }
  8163. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  8164. const char *ifname,
  8165. char *cmd, size_t *resp_len)
  8166. {
  8167. struct wpa_supplicant *wpa_s;
  8168. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  8169. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  8170. break;
  8171. }
  8172. if (wpa_s == NULL) {
  8173. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  8174. if (resp)
  8175. *resp_len = os_strlen(resp);
  8176. else
  8177. *resp_len = 1;
  8178. return resp;
  8179. }
  8180. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  8181. }
  8182. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  8183. char *buf, size_t *resp_len)
  8184. {
  8185. #ifdef CONFIG_P2P
  8186. static const char * cmd[] = {
  8187. "LIST_NETWORKS",
  8188. "P2P_FIND",
  8189. "P2P_STOP_FIND",
  8190. "P2P_LISTEN",
  8191. "P2P_GROUP_ADD",
  8192. "P2P_GET_PASSPHRASE",
  8193. "P2P_SERVICE_UPDATE",
  8194. "P2P_SERVICE_FLUSH",
  8195. "P2P_FLUSH",
  8196. "P2P_CANCEL",
  8197. "P2P_PRESENCE_REQ",
  8198. "P2P_EXT_LISTEN",
  8199. NULL
  8200. };
  8201. static const char * prefix[] = {
  8202. #ifdef ANDROID
  8203. "DRIVER ",
  8204. #endif /* ANDROID */
  8205. "GET_NETWORK ",
  8206. "REMOVE_NETWORK ",
  8207. "P2P_FIND ",
  8208. "P2P_CONNECT ",
  8209. "P2P_LISTEN ",
  8210. "P2P_GROUP_REMOVE ",
  8211. "P2P_GROUP_ADD ",
  8212. "P2P_GROUP_MEMBER ",
  8213. "P2P_PROV_DISC ",
  8214. "P2P_SERV_DISC_REQ ",
  8215. "P2P_SERV_DISC_CANCEL_REQ ",
  8216. "P2P_SERV_DISC_RESP ",
  8217. "P2P_SERV_DISC_EXTERNAL ",
  8218. "P2P_SERVICE_ADD ",
  8219. "P2P_SERVICE_DEL ",
  8220. "P2P_SERVICE_REP ",
  8221. "P2P_REJECT ",
  8222. "P2P_INVITE ",
  8223. "P2P_PEER ",
  8224. "P2P_SET ",
  8225. "P2P_UNAUTHORIZE ",
  8226. "P2P_PRESENCE_REQ ",
  8227. "P2P_EXT_LISTEN ",
  8228. "P2P_REMOVE_CLIENT ",
  8229. "WPS_NFC_TOKEN ",
  8230. "WPS_NFC_TAG_READ ",
  8231. "NFC_GET_HANDOVER_SEL ",
  8232. "NFC_GET_HANDOVER_REQ ",
  8233. "NFC_REPORT_HANDOVER ",
  8234. "P2P_ASP_PROVISION ",
  8235. "P2P_ASP_PROVISION_RESP ",
  8236. NULL
  8237. };
  8238. int found = 0;
  8239. int i;
  8240. if (global->p2p_init_wpa_s == NULL)
  8241. return NULL;
  8242. for (i = 0; !found && cmd[i]; i++) {
  8243. if (os_strcmp(buf, cmd[i]) == 0)
  8244. found = 1;
  8245. }
  8246. for (i = 0; !found && prefix[i]; i++) {
  8247. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  8248. found = 1;
  8249. }
  8250. if (found)
  8251. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  8252. buf, resp_len);
  8253. #endif /* CONFIG_P2P */
  8254. return NULL;
  8255. }
  8256. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  8257. char *buf, size_t *resp_len)
  8258. {
  8259. #ifdef CONFIG_WIFI_DISPLAY
  8260. if (global->p2p_init_wpa_s == NULL)
  8261. return NULL;
  8262. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  8263. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  8264. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  8265. buf, resp_len);
  8266. #endif /* CONFIG_WIFI_DISPLAY */
  8267. return NULL;
  8268. }
  8269. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  8270. char *buf, size_t *resp_len)
  8271. {
  8272. char *ret;
  8273. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  8274. if (ret)
  8275. return ret;
  8276. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  8277. if (ret)
  8278. return ret;
  8279. return NULL;
  8280. }
  8281. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  8282. {
  8283. char *value;
  8284. value = os_strchr(cmd, ' ');
  8285. if (value == NULL)
  8286. return -1;
  8287. *value++ = '\0';
  8288. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  8289. #ifdef CONFIG_WIFI_DISPLAY
  8290. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  8291. wifi_display_enable(global, !!atoi(value));
  8292. return 0;
  8293. }
  8294. #endif /* CONFIG_WIFI_DISPLAY */
  8295. /* Restore cmd to its original value to allow redirection */
  8296. value[-1] = ' ';
  8297. return -1;
  8298. }
  8299. static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
  8300. char *cmd)
  8301. {
  8302. struct wpa_supplicant *wpa_s[2]; /* src, dst */
  8303. char *p;
  8304. unsigned int i;
  8305. /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
  8306. * <variable name> */
  8307. for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
  8308. p = os_strchr(cmd, ' ');
  8309. if (p == NULL)
  8310. return -1;
  8311. *p = '\0';
  8312. wpa_s[i] = global->ifaces;
  8313. for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
  8314. if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
  8315. break;
  8316. }
  8317. if (!wpa_s[i]) {
  8318. wpa_printf(MSG_DEBUG,
  8319. "CTRL_IFACE: Could not find iface=%s", cmd);
  8320. return -1;
  8321. }
  8322. cmd = p + 1;
  8323. }
  8324. return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
  8325. }
  8326. #ifndef CONFIG_NO_CONFIG_WRITE
  8327. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  8328. {
  8329. int ret = 0, saved = 0;
  8330. struct wpa_supplicant *wpa_s;
  8331. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  8332. if (!wpa_s->conf->update_config) {
  8333. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  8334. continue;
  8335. }
  8336. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  8337. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  8338. ret = 1;
  8339. } else {
  8340. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  8341. saved++;
  8342. }
  8343. }
  8344. if (!saved && !ret) {
  8345. wpa_dbg(wpa_s, MSG_DEBUG,
  8346. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  8347. ret = 1;
  8348. }
  8349. return ret;
  8350. }
  8351. #endif /* CONFIG_NO_CONFIG_WRITE */
  8352. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  8353. char *buf, size_t buflen)
  8354. {
  8355. char *pos, *end;
  8356. int ret;
  8357. struct wpa_supplicant *wpa_s;
  8358. pos = buf;
  8359. end = buf + buflen;
  8360. #ifdef CONFIG_P2P
  8361. if (global->p2p && !global->p2p_disabled) {
  8362. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  8363. "\n"
  8364. "p2p_state=%s\n",
  8365. MAC2STR(global->p2p_dev_addr),
  8366. p2p_get_state_txt(global->p2p));
  8367. if (os_snprintf_error(end - pos, ret))
  8368. return pos - buf;
  8369. pos += ret;
  8370. } else if (global->p2p) {
  8371. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  8372. if (os_snprintf_error(end - pos, ret))
  8373. return pos - buf;
  8374. pos += ret;
  8375. }
  8376. #endif /* CONFIG_P2P */
  8377. #ifdef CONFIG_WIFI_DISPLAY
  8378. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  8379. !!global->wifi_display);
  8380. if (os_snprintf_error(end - pos, ret))
  8381. return pos - buf;
  8382. pos += ret;
  8383. #endif /* CONFIG_WIFI_DISPLAY */
  8384. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  8385. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  8386. "address=" MACSTR "\n",
  8387. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  8388. if (os_snprintf_error(end - pos, ret))
  8389. return pos - buf;
  8390. pos += ret;
  8391. }
  8392. return pos - buf;
  8393. }
  8394. #ifdef CONFIG_FST
  8395. static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
  8396. char *cmd, char *buf,
  8397. size_t reply_size)
  8398. {
  8399. char ifname[IFNAMSIZ + 1];
  8400. struct fst_iface_cfg cfg;
  8401. struct wpa_supplicant *wpa_s;
  8402. struct fst_wpa_obj iface_obj;
  8403. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  8404. wpa_s = wpa_supplicant_get_iface(global, ifname);
  8405. if (wpa_s) {
  8406. if (wpa_s->fst) {
  8407. wpa_printf(MSG_INFO, "FST: Already attached");
  8408. return -1;
  8409. }
  8410. fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
  8411. wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
  8412. &iface_obj, &cfg);
  8413. if (wpa_s->fst)
  8414. return os_snprintf(buf, reply_size, "OK\n");
  8415. }
  8416. }
  8417. return -1;
  8418. }
  8419. static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
  8420. char *cmd, char *buf,
  8421. size_t reply_size)
  8422. {
  8423. char ifname[IFNAMSIZ + 1];
  8424. struct wpa_supplicant *wpa_s;
  8425. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  8426. wpa_s = wpa_supplicant_get_iface(global, ifname);
  8427. if (wpa_s) {
  8428. if (!fst_iface_detach(ifname)) {
  8429. wpa_s->fst = NULL;
  8430. return os_snprintf(buf, reply_size, "OK\n");
  8431. }
  8432. }
  8433. }
  8434. return -1;
  8435. }
  8436. #endif /* CONFIG_FST */
  8437. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  8438. char *buf, size_t *resp_len)
  8439. {
  8440. char *reply;
  8441. const int reply_size = 2048;
  8442. int reply_len;
  8443. int level = MSG_DEBUG;
  8444. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  8445. char *pos = os_strchr(buf + 7, ' ');
  8446. if (pos) {
  8447. *pos++ = '\0';
  8448. return wpas_global_ctrl_iface_ifname(global,
  8449. buf + 7, pos,
  8450. resp_len);
  8451. }
  8452. }
  8453. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  8454. if (reply)
  8455. return reply;
  8456. if (os_strcmp(buf, "PING") == 0)
  8457. level = MSG_EXCESSIVE;
  8458. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  8459. (const u8 *) buf, os_strlen(buf));
  8460. reply = os_malloc(reply_size);
  8461. if (reply == NULL) {
  8462. *resp_len = 1;
  8463. return NULL;
  8464. }
  8465. os_memcpy(reply, "OK\n", 3);
  8466. reply_len = 3;
  8467. if (os_strcmp(buf, "PING") == 0) {
  8468. os_memcpy(reply, "PONG\n", 5);
  8469. reply_len = 5;
  8470. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  8471. if (wpa_supplicant_global_iface_add(global, buf + 14))
  8472. reply_len = -1;
  8473. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  8474. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  8475. reply_len = -1;
  8476. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  8477. reply_len = wpa_supplicant_global_iface_list(
  8478. global, reply, reply_size);
  8479. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  8480. reply_len = wpa_supplicant_global_iface_interfaces(
  8481. global, buf + 10, reply, reply_size);
  8482. #ifdef CONFIG_FST
  8483. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  8484. reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
  8485. reply,
  8486. reply_size);
  8487. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  8488. reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
  8489. reply,
  8490. reply_size);
  8491. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  8492. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  8493. #endif /* CONFIG_FST */
  8494. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  8495. wpa_supplicant_terminate_proc(global);
  8496. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  8497. wpas_notify_suspend(global);
  8498. } else if (os_strcmp(buf, "RESUME") == 0) {
  8499. wpas_notify_resume(global);
  8500. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  8501. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  8502. #ifdef CONFIG_P2P
  8503. if (global->p2p_init_wpa_s) {
  8504. os_free(reply);
  8505. /* Check if P2P redirection would work for this
  8506. * command. */
  8507. return wpa_supplicant_ctrl_iface_process(
  8508. global->p2p_init_wpa_s,
  8509. buf, resp_len);
  8510. }
  8511. #endif /* CONFIG_P2P */
  8512. reply_len = -1;
  8513. }
  8514. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  8515. if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
  8516. reply_len = -1;
  8517. #ifndef CONFIG_NO_CONFIG_WRITE
  8518. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  8519. if (wpas_global_ctrl_iface_save_config(global))
  8520. reply_len = -1;
  8521. #endif /* CONFIG_NO_CONFIG_WRITE */
  8522. } else if (os_strcmp(buf, "STATUS") == 0) {
  8523. reply_len = wpas_global_ctrl_iface_status(global, reply,
  8524. reply_size);
  8525. #ifdef CONFIG_MODULE_TESTS
  8526. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  8527. if (wpas_module_tests() < 0)
  8528. reply_len = -1;
  8529. #endif /* CONFIG_MODULE_TESTS */
  8530. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  8531. if (wpa_debug_reopen_file() < 0)
  8532. reply_len = -1;
  8533. } else {
  8534. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  8535. reply_len = 16;
  8536. }
  8537. if (reply_len < 0) {
  8538. os_memcpy(reply, "FAIL\n", 5);
  8539. reply_len = 5;
  8540. }
  8541. *resp_len = reply_len;
  8542. return reply;
  8543. }