ctrl_iface.c 268 KB

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