ctrl_iface.c 219 KB

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