ctrl_iface.c 226 KB

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