ctrl_iface.c 204 KB

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