ctrl_iface.c 183 KB

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