ctrl_iface.c 238 KB

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