ctrl_iface.c 222 KB

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