ctrl_iface.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2012, 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. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "common/version.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/ieee802_11_common.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "eap_peer/eap.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "rsn_supp/wpa.h"
  18. #include "rsn_supp/preauth.h"
  19. #include "rsn_supp/pmksa_cache.h"
  20. #include "l2_packet/l2_packet.h"
  21. #include "wps/wps.h"
  22. #include "config.h"
  23. #include "wpa_supplicant_i.h"
  24. #include "driver_i.h"
  25. #include "wps_supplicant.h"
  26. #include "ibss_rsn.h"
  27. #include "ap.h"
  28. #include "p2p_supplicant.h"
  29. #include "p2p/p2p.h"
  30. #include "hs20_supplicant.h"
  31. #include "wifi_display.h"
  32. #include "notify.h"
  33. #include "bss.h"
  34. #include "scan.h"
  35. #include "ctrl_iface.h"
  36. #include "interworking.h"
  37. #include "blacklist.h"
  38. #include "autoscan.h"
  39. #include "wnm_sta.h"
  40. extern struct wpa_driver_ops *wpa_drivers[];
  41. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  42. char *buf, int len);
  43. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  44. char *buf, int len);
  45. static int pno_start(struct wpa_supplicant *wpa_s)
  46. {
  47. int ret;
  48. size_t i, num_ssid;
  49. struct wpa_ssid *ssid;
  50. struct wpa_driver_scan_params params;
  51. if (wpa_s->pno)
  52. return 0;
  53. if (wpa_s->wpa_state == WPA_SCANNING) {
  54. wpa_supplicant_cancel_sched_scan(wpa_s);
  55. wpa_supplicant_cancel_scan(wpa_s);
  56. }
  57. os_memset(&params, 0, sizeof(params));
  58. num_ssid = 0;
  59. ssid = wpa_s->conf->ssid;
  60. while (ssid) {
  61. if (!wpas_network_disabled(wpa_s, ssid))
  62. num_ssid++;
  63. ssid = ssid->next;
  64. }
  65. if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
  66. wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
  67. "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
  68. num_ssid = WPAS_MAX_SCAN_SSIDS;
  69. }
  70. if (num_ssid == 0) {
  71. wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
  72. return -1;
  73. }
  74. params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
  75. num_ssid);
  76. if (params.filter_ssids == NULL)
  77. return -1;
  78. i = 0;
  79. ssid = wpa_s->conf->ssid;
  80. while (ssid) {
  81. if (!wpas_network_disabled(wpa_s, ssid)) {
  82. params.ssids[i].ssid = ssid->ssid;
  83. params.ssids[i].ssid_len = ssid->ssid_len;
  84. params.num_ssids++;
  85. os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
  86. ssid->ssid_len);
  87. params.filter_ssids[i].ssid_len = ssid->ssid_len;
  88. params.num_filter_ssids++;
  89. i++;
  90. if (i == num_ssid)
  91. break;
  92. }
  93. ssid = ssid->next;
  94. }
  95. if (wpa_s->conf->filter_rssi)
  96. params.filter_rssi = wpa_s->conf->filter_rssi;
  97. ret = wpa_drv_sched_scan(wpa_s, &params, 10 * 1000);
  98. os_free(params.filter_ssids);
  99. if (ret == 0)
  100. wpa_s->pno = 1;
  101. return ret;
  102. }
  103. static int pno_stop(struct wpa_supplicant *wpa_s)
  104. {
  105. int ret = 0;
  106. if (wpa_s->pno) {
  107. wpa_s->pno = 0;
  108. ret = wpa_drv_stop_sched_scan(wpa_s);
  109. }
  110. if (wpa_s->wpa_state == WPA_SCANNING)
  111. wpa_supplicant_req_scan(wpa_s, 0, 0);
  112. return ret;
  113. }
  114. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  115. {
  116. char *pos;
  117. u8 addr[ETH_ALEN], *filter = NULL, *n;
  118. size_t count = 0;
  119. pos = val;
  120. while (pos) {
  121. if (*pos == '\0')
  122. break;
  123. if (hwaddr_aton(pos, addr)) {
  124. os_free(filter);
  125. return -1;
  126. }
  127. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  128. if (n == NULL) {
  129. os_free(filter);
  130. return -1;
  131. }
  132. filter = n;
  133. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  134. count++;
  135. pos = os_strchr(pos, ' ');
  136. if (pos)
  137. pos++;
  138. }
  139. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  140. os_free(wpa_s->bssid_filter);
  141. wpa_s->bssid_filter = filter;
  142. wpa_s->bssid_filter_count = count;
  143. return 0;
  144. }
  145. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  146. {
  147. char *pos;
  148. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  149. struct wpa_ssid_value *ssid = NULL, *ns;
  150. size_t count = 0, ssid_count = 0;
  151. struct wpa_ssid *c;
  152. /*
  153. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | “”
  154. * SSID_SPEC ::= ssid <SSID_HEX>
  155. * BSSID_SPEC ::= bssid <BSSID_HEX>
  156. */
  157. pos = val;
  158. while (pos) {
  159. if (*pos == '\0')
  160. break;
  161. if (os_strncmp(pos, "bssid ", 6) == 0) {
  162. int res;
  163. pos += 6;
  164. res = hwaddr_aton2(pos, addr);
  165. if (res < 0) {
  166. os_free(ssid);
  167. os_free(bssid);
  168. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  169. "BSSID value '%s'", pos);
  170. return -1;
  171. }
  172. pos += res;
  173. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  174. if (n == NULL) {
  175. os_free(ssid);
  176. os_free(bssid);
  177. return -1;
  178. }
  179. bssid = n;
  180. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  181. count++;
  182. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  183. char *end;
  184. pos += 5;
  185. end = pos;
  186. while (*end) {
  187. if (*end == '\0' || *end == ' ')
  188. break;
  189. end++;
  190. }
  191. ns = os_realloc_array(ssid, ssid_count + 1,
  192. sizeof(struct wpa_ssid_value));
  193. if (ns == NULL) {
  194. os_free(ssid);
  195. os_free(bssid);
  196. return -1;
  197. }
  198. ssid = ns;
  199. if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
  200. hexstr2bin(pos, ssid[ssid_count].ssid,
  201. (end - pos) / 2) < 0) {
  202. os_free(ssid);
  203. os_free(bssid);
  204. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  205. "SSID value '%s'", pos);
  206. return -1;
  207. }
  208. ssid[ssid_count].ssid_len = (end - pos) / 2;
  209. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  210. ssid[ssid_count].ssid,
  211. ssid[ssid_count].ssid_len);
  212. ssid_count++;
  213. pos = end;
  214. } else {
  215. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  216. "'%s'", pos);
  217. os_free(ssid);
  218. os_free(bssid);
  219. return -1;
  220. }
  221. pos = os_strchr(pos, ' ');
  222. if (pos)
  223. pos++;
  224. }
  225. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  226. os_free(wpa_s->disallow_aps_bssid);
  227. wpa_s->disallow_aps_bssid = bssid;
  228. wpa_s->disallow_aps_bssid_count = count;
  229. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  230. os_free(wpa_s->disallow_aps_ssid);
  231. wpa_s->disallow_aps_ssid = ssid;
  232. wpa_s->disallow_aps_ssid_count = ssid_count;
  233. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  234. return 0;
  235. c = wpa_s->current_ssid;
  236. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  237. return 0;
  238. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  239. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  240. return 0;
  241. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  242. "because current AP was marked disallowed");
  243. #ifdef CONFIG_SME
  244. wpa_s->sme.prev_bssid_set = 0;
  245. #endif /* CONFIG_SME */
  246. wpa_s->reassociate = 1;
  247. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  248. wpa_supplicant_req_scan(wpa_s, 0, 0);
  249. return 0;
  250. }
  251. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  252. char *cmd)
  253. {
  254. char *value;
  255. int ret = 0;
  256. value = os_strchr(cmd, ' ');
  257. if (value == NULL)
  258. return -1;
  259. *value++ = '\0';
  260. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  261. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  262. eapol_sm_configure(wpa_s->eapol,
  263. atoi(value), -1, -1, -1);
  264. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  265. eapol_sm_configure(wpa_s->eapol,
  266. -1, atoi(value), -1, -1);
  267. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  268. eapol_sm_configure(wpa_s->eapol,
  269. -1, -1, atoi(value), -1);
  270. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  271. eapol_sm_configure(wpa_s->eapol,
  272. -1, -1, -1, atoi(value));
  273. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  274. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  275. atoi(value)))
  276. ret = -1;
  277. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  278. 0) {
  279. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  280. atoi(value)))
  281. ret = -1;
  282. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  283. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  284. ret = -1;
  285. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  286. wpa_s->wps_fragment_size = atoi(value);
  287. #ifdef CONFIG_WPS_TESTING
  288. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  289. long int val;
  290. val = strtol(value, NULL, 0);
  291. if (val < 0 || val > 0xff) {
  292. ret = -1;
  293. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  294. "wps_version_number %ld", val);
  295. } else {
  296. wps_version_number = val;
  297. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  298. "version %u.%u",
  299. (wps_version_number & 0xf0) >> 4,
  300. wps_version_number & 0x0f);
  301. }
  302. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  303. wps_testing_dummy_cred = atoi(value);
  304. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  305. wps_testing_dummy_cred);
  306. #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_TESTING
  311. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  312. extern unsigned int tdls_testing;
  313. tdls_testing = strtol(value, NULL, 0);
  314. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  315. #endif /* CONFIG_TDLS_TESTING */
  316. #ifdef CONFIG_TDLS
  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. if (atoi(value))
  329. ret = pno_start(wpa_s);
  330. else
  331. ret = pno_stop(wpa_s);
  332. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  333. int disabled = atoi(value);
  334. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  335. ret = -1;
  336. else if (disabled)
  337. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  338. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  339. if (os_strcmp(value, "disable") == 0)
  340. wpa_s->set_sta_uapsd = 0;
  341. else {
  342. int be, bk, vi, vo;
  343. char *pos;
  344. /* format: BE,BK,VI,VO;max SP Length */
  345. be = atoi(value);
  346. pos = os_strchr(value, ',');
  347. if (pos == NULL)
  348. return -1;
  349. pos++;
  350. bk = atoi(pos);
  351. pos = os_strchr(pos, ',');
  352. if (pos == NULL)
  353. return -1;
  354. pos++;
  355. vi = atoi(pos);
  356. pos = os_strchr(pos, ',');
  357. if (pos == NULL)
  358. return -1;
  359. pos++;
  360. vo = atoi(pos);
  361. /* ignore max SP Length for now */
  362. wpa_s->set_sta_uapsd = 1;
  363. wpa_s->sta_uapsd = 0;
  364. if (be)
  365. wpa_s->sta_uapsd |= BIT(0);
  366. if (bk)
  367. wpa_s->sta_uapsd |= BIT(1);
  368. if (vi)
  369. wpa_s->sta_uapsd |= BIT(2);
  370. if (vo)
  371. wpa_s->sta_uapsd |= BIT(3);
  372. }
  373. } else if (os_strcasecmp(cmd, "ps") == 0) {
  374. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  375. #ifdef CONFIG_WIFI_DISPLAY
  376. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  377. wifi_display_enable(wpa_s->global, !!atoi(value));
  378. #endif /* CONFIG_WIFI_DISPLAY */
  379. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  380. ret = set_bssid_filter(wpa_s, value);
  381. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  382. ret = set_disallow_aps(wpa_s, value);
  383. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  384. wpa_s->no_keep_alive = !!atoi(value);
  385. } else {
  386. value[-1] = '=';
  387. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  388. if (ret == 0)
  389. wpa_supplicant_update_config(wpa_s);
  390. }
  391. return ret;
  392. }
  393. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  394. char *cmd, char *buf, size_t buflen)
  395. {
  396. int res = -1;
  397. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  398. if (os_strcmp(cmd, "version") == 0) {
  399. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  400. } else if (os_strcasecmp(cmd, "country") == 0) {
  401. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  402. res = os_snprintf(buf, buflen, "%c%c",
  403. wpa_s->conf->country[0],
  404. wpa_s->conf->country[1]);
  405. #ifdef CONFIG_WIFI_DISPLAY
  406. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  407. res = os_snprintf(buf, buflen, "%d",
  408. wpa_s->global->wifi_display);
  409. if (res < 0 || (unsigned int) res >= buflen)
  410. return -1;
  411. return res;
  412. #endif /* CONFIG_WIFI_DISPLAY */
  413. }
  414. if (res < 0 || (unsigned int) res >= buflen)
  415. return -1;
  416. return res;
  417. }
  418. #ifdef IEEE8021X_EAPOL
  419. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  420. char *addr)
  421. {
  422. u8 bssid[ETH_ALEN];
  423. struct wpa_ssid *ssid = wpa_s->current_ssid;
  424. if (hwaddr_aton(addr, bssid)) {
  425. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  426. "'%s'", addr);
  427. return -1;
  428. }
  429. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  430. rsn_preauth_deinit(wpa_s->wpa);
  431. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  432. return -1;
  433. return 0;
  434. }
  435. #endif /* IEEE8021X_EAPOL */
  436. #ifdef CONFIG_PEERKEY
  437. /* MLME-STKSTART.request(peer) */
  438. static int wpa_supplicant_ctrl_iface_stkstart(
  439. struct wpa_supplicant *wpa_s, char *addr)
  440. {
  441. u8 peer[ETH_ALEN];
  442. if (hwaddr_aton(addr, peer)) {
  443. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  444. "address '%s'", addr);
  445. return -1;
  446. }
  447. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  448. MAC2STR(peer));
  449. return wpa_sm_stkstart(wpa_s->wpa, peer);
  450. }
  451. #endif /* CONFIG_PEERKEY */
  452. #ifdef CONFIG_TDLS
  453. static int wpa_supplicant_ctrl_iface_tdls_discover(
  454. struct wpa_supplicant *wpa_s, char *addr)
  455. {
  456. u8 peer[ETH_ALEN];
  457. int ret;
  458. if (hwaddr_aton(addr, peer)) {
  459. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  460. "address '%s'", addr);
  461. return -1;
  462. }
  463. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  464. MAC2STR(peer));
  465. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  466. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  467. else
  468. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  469. return ret;
  470. }
  471. static int wpa_supplicant_ctrl_iface_tdls_setup(
  472. struct wpa_supplicant *wpa_s, char *addr)
  473. {
  474. u8 peer[ETH_ALEN];
  475. int ret;
  476. if (hwaddr_aton(addr, peer)) {
  477. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  478. "address '%s'", addr);
  479. return -1;
  480. }
  481. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  482. MAC2STR(peer));
  483. wpa_tdls_remove(wpa_s->wpa, peer);
  484. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  485. ret = wpa_tdls_start(wpa_s->wpa, peer);
  486. else
  487. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  488. return ret;
  489. }
  490. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  491. struct wpa_supplicant *wpa_s, char *addr)
  492. {
  493. u8 peer[ETH_ALEN];
  494. if (hwaddr_aton(addr, peer)) {
  495. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  496. "address '%s'", addr);
  497. return -1;
  498. }
  499. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  500. MAC2STR(peer));
  501. return wpa_tdls_teardown_link(wpa_s->wpa, peer,
  502. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  503. }
  504. #endif /* CONFIG_TDLS */
  505. #ifdef CONFIG_IEEE80211R
  506. static int wpa_supplicant_ctrl_iface_ft_ds(
  507. struct wpa_supplicant *wpa_s, char *addr)
  508. {
  509. u8 target_ap[ETH_ALEN];
  510. struct wpa_bss *bss;
  511. const u8 *mdie;
  512. if (hwaddr_aton(addr, target_ap)) {
  513. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  514. "address '%s'", addr);
  515. return -1;
  516. }
  517. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  518. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  519. if (bss)
  520. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  521. else
  522. mdie = NULL;
  523. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  524. }
  525. #endif /* CONFIG_IEEE80211R */
  526. #ifdef CONFIG_WPS
  527. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  528. char *cmd)
  529. {
  530. u8 bssid[ETH_ALEN], *_bssid = bssid;
  531. #ifdef CONFIG_P2P
  532. u8 p2p_dev_addr[ETH_ALEN];
  533. #endif /* CONFIG_P2P */
  534. #ifdef CONFIG_AP
  535. u8 *_p2p_dev_addr = NULL;
  536. #endif /* CONFIG_AP */
  537. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  538. _bssid = NULL;
  539. #ifdef CONFIG_P2P
  540. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  541. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  542. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  543. "P2P Device Address '%s'",
  544. cmd + 13);
  545. return -1;
  546. }
  547. _p2p_dev_addr = p2p_dev_addr;
  548. #endif /* CONFIG_P2P */
  549. } else if (hwaddr_aton(cmd, bssid)) {
  550. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  551. cmd);
  552. return -1;
  553. }
  554. #ifdef CONFIG_AP
  555. if (wpa_s->ap_iface)
  556. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  557. #endif /* CONFIG_AP */
  558. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  559. }
  560. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  561. char *cmd, char *buf,
  562. size_t buflen)
  563. {
  564. u8 bssid[ETH_ALEN], *_bssid = bssid;
  565. char *pin;
  566. int ret;
  567. pin = os_strchr(cmd, ' ');
  568. if (pin)
  569. *pin++ = '\0';
  570. if (os_strcmp(cmd, "any") == 0)
  571. _bssid = NULL;
  572. else if (os_strcmp(cmd, "get") == 0) {
  573. ret = wps_generate_pin();
  574. goto done;
  575. } else if (hwaddr_aton(cmd, bssid)) {
  576. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  577. cmd);
  578. return -1;
  579. }
  580. #ifdef CONFIG_AP
  581. if (wpa_s->ap_iface) {
  582. int timeout = 0;
  583. char *pos;
  584. if (pin) {
  585. pos = os_strchr(pin, ' ');
  586. if (pos) {
  587. *pos++ = '\0';
  588. timeout = atoi(pos);
  589. }
  590. }
  591. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  592. buf, buflen, timeout);
  593. }
  594. #endif /* CONFIG_AP */
  595. if (pin) {
  596. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  597. DEV_PW_DEFAULT);
  598. if (ret < 0)
  599. return -1;
  600. ret = os_snprintf(buf, buflen, "%s", pin);
  601. if (ret < 0 || (size_t) ret >= buflen)
  602. return -1;
  603. return ret;
  604. }
  605. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  606. if (ret < 0)
  607. return -1;
  608. done:
  609. /* Return the generated PIN */
  610. ret = os_snprintf(buf, buflen, "%08d", ret);
  611. if (ret < 0 || (size_t) ret >= buflen)
  612. return -1;
  613. return ret;
  614. }
  615. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  616. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  617. {
  618. char pin[9];
  619. size_t len;
  620. char *pos;
  621. int ret;
  622. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  623. (u8 *) cmd, os_strlen(cmd));
  624. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  625. if (*pos < '0' || *pos > '9')
  626. continue;
  627. pin[len++] = *pos;
  628. if (len == 9) {
  629. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  630. return -1;
  631. }
  632. }
  633. if (len != 4 && len != 8) {
  634. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  635. return -1;
  636. }
  637. pin[len] = '\0';
  638. if (len == 8) {
  639. unsigned int pin_val;
  640. pin_val = atoi(pin);
  641. if (!wps_pin_valid(pin_val)) {
  642. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  643. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  644. if (ret < 0 || (size_t) ret >= buflen)
  645. return -1;
  646. return ret;
  647. }
  648. }
  649. ret = os_snprintf(buf, buflen, "%s", pin);
  650. if (ret < 0 || (size_t) ret >= buflen)
  651. return -1;
  652. return ret;
  653. }
  654. #ifdef CONFIG_WPS_NFC
  655. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  656. char *cmd)
  657. {
  658. u8 bssid[ETH_ALEN], *_bssid = bssid;
  659. if (cmd == NULL || cmd[0] == '\0')
  660. _bssid = NULL;
  661. else if (hwaddr_aton(cmd, bssid))
  662. return -1;
  663. return wpas_wps_start_nfc(wpa_s, _bssid);
  664. }
  665. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  666. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  667. {
  668. int ndef;
  669. struct wpabuf *buf;
  670. int res;
  671. if (os_strcmp(cmd, "WPS") == 0)
  672. ndef = 0;
  673. else if (os_strcmp(cmd, "NDEF") == 0)
  674. ndef = 1;
  675. else
  676. return -1;
  677. buf = wpas_wps_nfc_token(wpa_s, ndef);
  678. if (buf == NULL)
  679. return -1;
  680. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  681. wpabuf_len(buf));
  682. reply[res++] = '\n';
  683. reply[res] = '\0';
  684. wpabuf_free(buf);
  685. return res;
  686. }
  687. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  688. struct wpa_supplicant *wpa_s, char *pos)
  689. {
  690. size_t len;
  691. struct wpabuf *buf;
  692. int ret;
  693. len = os_strlen(pos);
  694. if (len & 0x01)
  695. return -1;
  696. len /= 2;
  697. buf = wpabuf_alloc(len);
  698. if (buf == NULL)
  699. return -1;
  700. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  701. wpabuf_free(buf);
  702. return -1;
  703. }
  704. ret = wpas_wps_nfc_tag_read(wpa_s, buf);
  705. wpabuf_free(buf);
  706. return ret;
  707. }
  708. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  709. char *reply, size_t max_len,
  710. int cr)
  711. {
  712. struct wpabuf *buf;
  713. int res;
  714. buf = wpas_wps_nfc_handover_req(wpa_s, cr);
  715. if (buf == NULL)
  716. return -1;
  717. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  718. wpabuf_len(buf));
  719. reply[res++] = '\n';
  720. reply[res] = '\0';
  721. wpabuf_free(buf);
  722. return res;
  723. }
  724. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  725. char *cmd, char *reply,
  726. size_t max_len)
  727. {
  728. char *pos;
  729. pos = os_strchr(cmd, ' ');
  730. if (pos == NULL)
  731. return -1;
  732. *pos++ = '\0';
  733. if (os_strcmp(cmd, "NDEF") != 0)
  734. return -1;
  735. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  736. return wpas_ctrl_nfc_get_handover_req_wps(
  737. wpa_s, reply, max_len, os_strcmp(pos, "WPS-CR") == 0);
  738. }
  739. return -1;
  740. }
  741. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  742. char *reply, size_t max_len)
  743. {
  744. struct wpabuf *buf;
  745. int res;
  746. buf = wpas_wps_nfc_handover_sel(wpa_s);
  747. if (buf == NULL)
  748. return -1;
  749. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  750. wpabuf_len(buf));
  751. reply[res++] = '\n';
  752. reply[res] = '\0';
  753. wpabuf_free(buf);
  754. return res;
  755. }
  756. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  757. char *cmd, char *reply,
  758. size_t max_len)
  759. {
  760. char *pos;
  761. pos = os_strchr(cmd, ' ');
  762. if (pos == NULL)
  763. return -1;
  764. *pos++ = '\0';
  765. if (os_strcmp(cmd, "NDEF") != 0)
  766. return -1;
  767. if (os_strcmp(pos, "WPS") == 0) {
  768. return wpas_ctrl_nfc_get_handover_sel_wps(wpa_s, reply,
  769. max_len);
  770. }
  771. return -1;
  772. }
  773. static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
  774. char *cmd, char *reply,
  775. size_t max_len)
  776. {
  777. size_t len;
  778. struct wpabuf *buf;
  779. int ret;
  780. len = os_strlen(cmd);
  781. if (len & 0x01)
  782. return -1;
  783. len /= 2;
  784. buf = wpabuf_alloc(len);
  785. if (buf == NULL)
  786. return -1;
  787. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  788. wpabuf_free(buf);
  789. return -1;
  790. }
  791. ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
  792. wpabuf_free(buf);
  793. return ret;
  794. }
  795. static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
  796. char *cmd)
  797. {
  798. size_t len;
  799. struct wpabuf *buf;
  800. int ret;
  801. len = os_strlen(cmd);
  802. if (len & 0x01)
  803. return -1;
  804. len /= 2;
  805. buf = wpabuf_alloc(len);
  806. if (buf == NULL)
  807. return -1;
  808. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  809. wpabuf_free(buf);
  810. return -1;
  811. }
  812. ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
  813. wpabuf_free(buf);
  814. return ret;
  815. }
  816. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  817. char *cmd)
  818. {
  819. size_t len;
  820. struct wpabuf *req, *sel;
  821. int ret;
  822. char *pos, *role, *type, *pos2;
  823. role = cmd;
  824. pos = os_strchr(role, ' ');
  825. if (pos == NULL)
  826. return -1;
  827. *pos++ = '\0';
  828. type = pos;
  829. pos = os_strchr(type, ' ');
  830. if (pos == NULL)
  831. return -1;
  832. *pos++ = '\0';
  833. pos2 = os_strchr(pos, ' ');
  834. if (pos2 == NULL)
  835. return -1;
  836. *pos2++ = '\0';
  837. len = os_strlen(pos);
  838. if (len & 0x01)
  839. return -1;
  840. len /= 2;
  841. req = wpabuf_alloc(len);
  842. if (req == NULL)
  843. return -1;
  844. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  845. wpabuf_free(req);
  846. return -1;
  847. }
  848. len = os_strlen(pos2);
  849. if (len & 0x01) {
  850. wpabuf_free(req);
  851. return -1;
  852. }
  853. len /= 2;
  854. sel = wpabuf_alloc(len);
  855. if (sel == NULL) {
  856. wpabuf_free(req);
  857. return -1;
  858. }
  859. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  860. wpabuf_free(req);
  861. wpabuf_free(sel);
  862. return -1;
  863. }
  864. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  865. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  866. } else {
  867. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  868. "reported: role=%s type=%s", role, type);
  869. ret = -1;
  870. }
  871. wpabuf_free(req);
  872. wpabuf_free(sel);
  873. return ret;
  874. }
  875. #endif /* CONFIG_WPS_NFC */
  876. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  877. char *cmd)
  878. {
  879. u8 bssid[ETH_ALEN];
  880. char *pin;
  881. char *new_ssid;
  882. char *new_auth;
  883. char *new_encr;
  884. char *new_key;
  885. struct wps_new_ap_settings ap;
  886. pin = os_strchr(cmd, ' ');
  887. if (pin == NULL)
  888. return -1;
  889. *pin++ = '\0';
  890. if (hwaddr_aton(cmd, bssid)) {
  891. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  892. cmd);
  893. return -1;
  894. }
  895. new_ssid = os_strchr(pin, ' ');
  896. if (new_ssid == NULL)
  897. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  898. *new_ssid++ = '\0';
  899. new_auth = os_strchr(new_ssid, ' ');
  900. if (new_auth == NULL)
  901. return -1;
  902. *new_auth++ = '\0';
  903. new_encr = os_strchr(new_auth, ' ');
  904. if (new_encr == NULL)
  905. return -1;
  906. *new_encr++ = '\0';
  907. new_key = os_strchr(new_encr, ' ');
  908. if (new_key == NULL)
  909. return -1;
  910. *new_key++ = '\0';
  911. os_memset(&ap, 0, sizeof(ap));
  912. ap.ssid_hex = new_ssid;
  913. ap.auth = new_auth;
  914. ap.encr = new_encr;
  915. ap.key_hex = new_key;
  916. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  917. }
  918. #ifdef CONFIG_AP
  919. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  920. char *cmd, char *buf,
  921. size_t buflen)
  922. {
  923. int timeout = 300;
  924. char *pos;
  925. const char *pin_txt;
  926. if (!wpa_s->ap_iface)
  927. return -1;
  928. pos = os_strchr(cmd, ' ');
  929. if (pos)
  930. *pos++ = '\0';
  931. if (os_strcmp(cmd, "disable") == 0) {
  932. wpas_wps_ap_pin_disable(wpa_s);
  933. return os_snprintf(buf, buflen, "OK\n");
  934. }
  935. if (os_strcmp(cmd, "random") == 0) {
  936. if (pos)
  937. timeout = atoi(pos);
  938. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  939. if (pin_txt == NULL)
  940. return -1;
  941. return os_snprintf(buf, buflen, "%s", pin_txt);
  942. }
  943. if (os_strcmp(cmd, "get") == 0) {
  944. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  945. if (pin_txt == NULL)
  946. return -1;
  947. return os_snprintf(buf, buflen, "%s", pin_txt);
  948. }
  949. if (os_strcmp(cmd, "set") == 0) {
  950. char *pin;
  951. if (pos == NULL)
  952. return -1;
  953. pin = pos;
  954. pos = os_strchr(pos, ' ');
  955. if (pos) {
  956. *pos++ = '\0';
  957. timeout = atoi(pos);
  958. }
  959. if (os_strlen(pin) > buflen)
  960. return -1;
  961. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  962. return -1;
  963. return os_snprintf(buf, buflen, "%s", pin);
  964. }
  965. return -1;
  966. }
  967. #endif /* CONFIG_AP */
  968. #ifdef CONFIG_WPS_ER
  969. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  970. char *cmd)
  971. {
  972. char *uuid = cmd, *pin, *pos;
  973. u8 addr_buf[ETH_ALEN], *addr = NULL;
  974. pin = os_strchr(uuid, ' ');
  975. if (pin == NULL)
  976. return -1;
  977. *pin++ = '\0';
  978. pos = os_strchr(pin, ' ');
  979. if (pos) {
  980. *pos++ = '\0';
  981. if (hwaddr_aton(pos, addr_buf) == 0)
  982. addr = addr_buf;
  983. }
  984. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  985. }
  986. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  987. char *cmd)
  988. {
  989. char *uuid = cmd, *pin;
  990. pin = os_strchr(uuid, ' ');
  991. if (pin == NULL)
  992. return -1;
  993. *pin++ = '\0';
  994. return wpas_wps_er_learn(wpa_s, uuid, pin);
  995. }
  996. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  997. struct wpa_supplicant *wpa_s, char *cmd)
  998. {
  999. char *uuid = cmd, *id;
  1000. id = os_strchr(uuid, ' ');
  1001. if (id == NULL)
  1002. return -1;
  1003. *id++ = '\0';
  1004. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1005. }
  1006. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1007. struct wpa_supplicant *wpa_s, char *cmd)
  1008. {
  1009. char *pin;
  1010. char *new_ssid;
  1011. char *new_auth;
  1012. char *new_encr;
  1013. char *new_key;
  1014. struct wps_new_ap_settings ap;
  1015. pin = os_strchr(cmd, ' ');
  1016. if (pin == NULL)
  1017. return -1;
  1018. *pin++ = '\0';
  1019. new_ssid = os_strchr(pin, ' ');
  1020. if (new_ssid == NULL)
  1021. return -1;
  1022. *new_ssid++ = '\0';
  1023. new_auth = os_strchr(new_ssid, ' ');
  1024. if (new_auth == NULL)
  1025. return -1;
  1026. *new_auth++ = '\0';
  1027. new_encr = os_strchr(new_auth, ' ');
  1028. if (new_encr == NULL)
  1029. return -1;
  1030. *new_encr++ = '\0';
  1031. new_key = os_strchr(new_encr, ' ');
  1032. if (new_key == NULL)
  1033. return -1;
  1034. *new_key++ = '\0';
  1035. os_memset(&ap, 0, sizeof(ap));
  1036. ap.ssid_hex = new_ssid;
  1037. ap.auth = new_auth;
  1038. ap.encr = new_encr;
  1039. ap.key_hex = new_key;
  1040. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1041. }
  1042. #ifdef CONFIG_WPS_NFC
  1043. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1044. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1045. {
  1046. int ndef;
  1047. struct wpabuf *buf;
  1048. int res;
  1049. char *uuid;
  1050. uuid = os_strchr(cmd, ' ');
  1051. if (uuid == NULL)
  1052. return -1;
  1053. *uuid++ = '\0';
  1054. if (os_strcmp(cmd, "WPS") == 0)
  1055. ndef = 0;
  1056. else if (os_strcmp(cmd, "NDEF") == 0)
  1057. ndef = 1;
  1058. else
  1059. return -1;
  1060. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1061. if (buf == NULL)
  1062. return -1;
  1063. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1064. wpabuf_len(buf));
  1065. reply[res++] = '\n';
  1066. reply[res] = '\0';
  1067. wpabuf_free(buf);
  1068. return res;
  1069. }
  1070. #endif /* CONFIG_WPS_NFC */
  1071. #endif /* CONFIG_WPS_ER */
  1072. #endif /* CONFIG_WPS */
  1073. #ifdef CONFIG_IBSS_RSN
  1074. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1075. struct wpa_supplicant *wpa_s, char *addr)
  1076. {
  1077. u8 peer[ETH_ALEN];
  1078. if (hwaddr_aton(addr, peer)) {
  1079. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1080. "address '%s'", addr);
  1081. return -1;
  1082. }
  1083. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1084. MAC2STR(peer));
  1085. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1086. }
  1087. #endif /* CONFIG_IBSS_RSN */
  1088. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1089. char *rsp)
  1090. {
  1091. #ifdef IEEE8021X_EAPOL
  1092. char *pos, *id_pos;
  1093. int id;
  1094. struct wpa_ssid *ssid;
  1095. pos = os_strchr(rsp, '-');
  1096. if (pos == NULL)
  1097. return -1;
  1098. *pos++ = '\0';
  1099. id_pos = pos;
  1100. pos = os_strchr(pos, ':');
  1101. if (pos == NULL)
  1102. return -1;
  1103. *pos++ = '\0';
  1104. id = atoi(id_pos);
  1105. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1106. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1107. (u8 *) pos, os_strlen(pos));
  1108. ssid = wpa_config_get_network(wpa_s->conf, id);
  1109. if (ssid == NULL) {
  1110. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1111. "to update", id);
  1112. return -1;
  1113. }
  1114. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1115. pos);
  1116. #else /* IEEE8021X_EAPOL */
  1117. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1118. return -1;
  1119. #endif /* IEEE8021X_EAPOL */
  1120. }
  1121. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1122. const char *params,
  1123. char *buf, size_t buflen)
  1124. {
  1125. char *pos, *end, tmp[30];
  1126. int res, verbose, wps, ret;
  1127. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1128. wps = os_strcmp(params, "-WPS") == 0;
  1129. pos = buf;
  1130. end = buf + buflen;
  1131. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1132. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1133. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1134. MAC2STR(wpa_s->bssid));
  1135. if (ret < 0 || ret >= end - pos)
  1136. return pos - buf;
  1137. pos += ret;
  1138. if (ssid) {
  1139. u8 *_ssid = ssid->ssid;
  1140. size_t ssid_len = ssid->ssid_len;
  1141. u8 ssid_buf[MAX_SSID_LEN];
  1142. if (ssid_len == 0) {
  1143. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1144. if (_res < 0)
  1145. ssid_len = 0;
  1146. else
  1147. ssid_len = _res;
  1148. _ssid = ssid_buf;
  1149. }
  1150. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1151. wpa_ssid_txt(_ssid, ssid_len),
  1152. ssid->id);
  1153. if (ret < 0 || ret >= end - pos)
  1154. return pos - buf;
  1155. pos += ret;
  1156. if (wps && ssid->passphrase &&
  1157. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1158. (ssid->mode == WPAS_MODE_AP ||
  1159. ssid->mode == WPAS_MODE_P2P_GO)) {
  1160. ret = os_snprintf(pos, end - pos,
  1161. "passphrase=%s\n",
  1162. ssid->passphrase);
  1163. if (ret < 0 || ret >= end - pos)
  1164. return pos - buf;
  1165. pos += ret;
  1166. }
  1167. if (ssid->id_str) {
  1168. ret = os_snprintf(pos, end - pos,
  1169. "id_str=%s\n",
  1170. ssid->id_str);
  1171. if (ret < 0 || ret >= end - pos)
  1172. return pos - buf;
  1173. pos += ret;
  1174. }
  1175. switch (ssid->mode) {
  1176. case WPAS_MODE_INFRA:
  1177. ret = os_snprintf(pos, end - pos,
  1178. "mode=station\n");
  1179. break;
  1180. case WPAS_MODE_IBSS:
  1181. ret = os_snprintf(pos, end - pos,
  1182. "mode=IBSS\n");
  1183. break;
  1184. case WPAS_MODE_AP:
  1185. ret = os_snprintf(pos, end - pos,
  1186. "mode=AP\n");
  1187. break;
  1188. case WPAS_MODE_P2P_GO:
  1189. ret = os_snprintf(pos, end - pos,
  1190. "mode=P2P GO\n");
  1191. break;
  1192. case WPAS_MODE_P2P_GROUP_FORMATION:
  1193. ret = os_snprintf(pos, end - pos,
  1194. "mode=P2P GO - group "
  1195. "formation\n");
  1196. break;
  1197. default:
  1198. ret = 0;
  1199. break;
  1200. }
  1201. if (ret < 0 || ret >= end - pos)
  1202. return pos - buf;
  1203. pos += ret;
  1204. }
  1205. #ifdef CONFIG_AP
  1206. if (wpa_s->ap_iface) {
  1207. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1208. end - pos,
  1209. verbose);
  1210. } else
  1211. #endif /* CONFIG_AP */
  1212. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1213. }
  1214. #ifdef CONFIG_SAE
  1215. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1216. wpa_s->sme.sae.state == SAE_ACCEPTED && !wpa_s->ap_iface) {
  1217. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1218. wpa_s->sme.sae.group);
  1219. if (ret < 0 || ret >= end - pos)
  1220. return pos - buf;
  1221. pos += ret;
  1222. }
  1223. #endif /* CONFIG_SAE */
  1224. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1225. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1226. if (ret < 0 || ret >= end - pos)
  1227. return pos - buf;
  1228. pos += ret;
  1229. if (wpa_s->l2 &&
  1230. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1231. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1232. if (ret < 0 || ret >= end - pos)
  1233. return pos - buf;
  1234. pos += ret;
  1235. }
  1236. #ifdef CONFIG_P2P
  1237. if (wpa_s->global->p2p) {
  1238. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1239. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1240. if (ret < 0 || ret >= end - pos)
  1241. return pos - buf;
  1242. pos += ret;
  1243. }
  1244. #endif /* CONFIG_P2P */
  1245. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1246. MAC2STR(wpa_s->own_addr));
  1247. if (ret < 0 || ret >= end - pos)
  1248. return pos - buf;
  1249. pos += ret;
  1250. #ifdef CONFIG_HS20
  1251. if (wpa_s->current_bss &&
  1252. wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
  1253. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1254. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1255. ret = os_snprintf(pos, end - pos, "hs20=1\n");
  1256. if (ret < 0 || ret >= end - pos)
  1257. return pos - buf;
  1258. pos += ret;
  1259. }
  1260. if (wpa_s->current_ssid) {
  1261. struct wpa_cred *cred;
  1262. char *type;
  1263. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1264. if (wpa_s->current_ssid->parent_cred != cred)
  1265. continue;
  1266. if (!cred->domain)
  1267. continue;
  1268. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1269. cred->domain);
  1270. if (ret < 0 || ret >= end - pos)
  1271. return pos - buf;
  1272. pos += ret;
  1273. if (wpa_s->current_bss == NULL ||
  1274. wpa_s->current_bss->anqp == NULL)
  1275. res = -1;
  1276. else
  1277. res = interworking_home_sp_cred(
  1278. wpa_s, cred,
  1279. wpa_s->current_bss->anqp->domain_name);
  1280. if (res > 0)
  1281. type = "home";
  1282. else if (res == 0)
  1283. type = "roaming";
  1284. else
  1285. type = "unknown";
  1286. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1287. if (ret < 0 || ret >= end - pos)
  1288. return pos - buf;
  1289. pos += ret;
  1290. break;
  1291. }
  1292. }
  1293. #endif /* CONFIG_HS20 */
  1294. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1295. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1296. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1297. verbose);
  1298. if (res >= 0)
  1299. pos += res;
  1300. }
  1301. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1302. if (res >= 0)
  1303. pos += res;
  1304. return pos - buf;
  1305. }
  1306. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1307. char *cmd)
  1308. {
  1309. char *pos;
  1310. int id;
  1311. struct wpa_ssid *ssid;
  1312. u8 bssid[ETH_ALEN];
  1313. /* cmd: "<network id> <BSSID>" */
  1314. pos = os_strchr(cmd, ' ');
  1315. if (pos == NULL)
  1316. return -1;
  1317. *pos++ = '\0';
  1318. id = atoi(cmd);
  1319. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1320. if (hwaddr_aton(pos, bssid)) {
  1321. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1322. return -1;
  1323. }
  1324. ssid = wpa_config_get_network(wpa_s->conf, id);
  1325. if (ssid == NULL) {
  1326. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1327. "to update", id);
  1328. return -1;
  1329. }
  1330. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1331. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1332. return 0;
  1333. }
  1334. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1335. char *cmd, char *buf,
  1336. size_t buflen)
  1337. {
  1338. u8 bssid[ETH_ALEN];
  1339. struct wpa_blacklist *e;
  1340. char *pos, *end;
  1341. int ret;
  1342. /* cmd: "BLACKLIST [<BSSID>]" */
  1343. if (*cmd == '\0') {
  1344. pos = buf;
  1345. end = buf + buflen;
  1346. e = wpa_s->blacklist;
  1347. while (e) {
  1348. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1349. MAC2STR(e->bssid));
  1350. if (ret < 0 || ret >= end - pos)
  1351. return pos - buf;
  1352. pos += ret;
  1353. e = e->next;
  1354. }
  1355. return pos - buf;
  1356. }
  1357. cmd++;
  1358. if (os_strncmp(cmd, "clear", 5) == 0) {
  1359. wpa_blacklist_clear(wpa_s);
  1360. os_memcpy(buf, "OK\n", 3);
  1361. return 3;
  1362. }
  1363. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1364. if (hwaddr_aton(cmd, bssid)) {
  1365. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1366. return -1;
  1367. }
  1368. /*
  1369. * Add the BSSID twice, so its count will be 2, causing it to be
  1370. * skipped when processing scan results.
  1371. */
  1372. ret = wpa_blacklist_add(wpa_s, bssid);
  1373. if (ret != 0)
  1374. return -1;
  1375. ret = wpa_blacklist_add(wpa_s, bssid);
  1376. if (ret != 0)
  1377. return -1;
  1378. os_memcpy(buf, "OK\n", 3);
  1379. return 3;
  1380. }
  1381. extern int wpa_debug_level;
  1382. extern int wpa_debug_timestamp;
  1383. static const char * debug_level_str(int level)
  1384. {
  1385. switch (level) {
  1386. case MSG_EXCESSIVE:
  1387. return "EXCESSIVE";
  1388. case MSG_MSGDUMP:
  1389. return "MSGDUMP";
  1390. case MSG_DEBUG:
  1391. return "DEBUG";
  1392. case MSG_INFO:
  1393. return "INFO";
  1394. case MSG_WARNING:
  1395. return "WARNING";
  1396. case MSG_ERROR:
  1397. return "ERROR";
  1398. default:
  1399. return "?";
  1400. }
  1401. }
  1402. static int str_to_debug_level(const char *s)
  1403. {
  1404. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1405. return MSG_EXCESSIVE;
  1406. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1407. return MSG_MSGDUMP;
  1408. if (os_strcasecmp(s, "DEBUG") == 0)
  1409. return MSG_DEBUG;
  1410. if (os_strcasecmp(s, "INFO") == 0)
  1411. return MSG_INFO;
  1412. if (os_strcasecmp(s, "WARNING") == 0)
  1413. return MSG_WARNING;
  1414. if (os_strcasecmp(s, "ERROR") == 0)
  1415. return MSG_ERROR;
  1416. return -1;
  1417. }
  1418. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1419. char *cmd, char *buf,
  1420. size_t buflen)
  1421. {
  1422. char *pos, *end, *stamp;
  1423. int ret;
  1424. if (cmd == NULL) {
  1425. return -1;
  1426. }
  1427. /* cmd: "LOG_LEVEL [<level>]" */
  1428. if (*cmd == '\0') {
  1429. pos = buf;
  1430. end = buf + buflen;
  1431. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1432. "Timestamp: %d\n",
  1433. debug_level_str(wpa_debug_level),
  1434. wpa_debug_timestamp);
  1435. if (ret < 0 || ret >= end - pos)
  1436. ret = 0;
  1437. return ret;
  1438. }
  1439. while (*cmd == ' ')
  1440. cmd++;
  1441. stamp = os_strchr(cmd, ' ');
  1442. if (stamp) {
  1443. *stamp++ = '\0';
  1444. while (*stamp == ' ') {
  1445. stamp++;
  1446. }
  1447. }
  1448. if (cmd && os_strlen(cmd)) {
  1449. int level = str_to_debug_level(cmd);
  1450. if (level < 0)
  1451. return -1;
  1452. wpa_debug_level = level;
  1453. }
  1454. if (stamp && os_strlen(stamp))
  1455. wpa_debug_timestamp = atoi(stamp);
  1456. os_memcpy(buf, "OK\n", 3);
  1457. return 3;
  1458. }
  1459. static int wpa_supplicant_ctrl_iface_list_networks(
  1460. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1461. {
  1462. char *pos, *end;
  1463. struct wpa_ssid *ssid;
  1464. int ret;
  1465. pos = buf;
  1466. end = buf + buflen;
  1467. ret = os_snprintf(pos, end - pos,
  1468. "network id / ssid / bssid / flags\n");
  1469. if (ret < 0 || ret >= end - pos)
  1470. return pos - buf;
  1471. pos += ret;
  1472. ssid = wpa_s->conf->ssid;
  1473. while (ssid) {
  1474. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1475. ssid->id,
  1476. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1477. if (ret < 0 || ret >= end - pos)
  1478. return pos - buf;
  1479. pos += ret;
  1480. if (ssid->bssid_set) {
  1481. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1482. MAC2STR(ssid->bssid));
  1483. } else {
  1484. ret = os_snprintf(pos, end - pos, "\tany");
  1485. }
  1486. if (ret < 0 || ret >= end - pos)
  1487. return pos - buf;
  1488. pos += ret;
  1489. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1490. ssid == wpa_s->current_ssid ?
  1491. "[CURRENT]" : "",
  1492. ssid->disabled ? "[DISABLED]" : "",
  1493. ssid->disabled_until.sec ?
  1494. "[TEMP-DISABLED]" : "",
  1495. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1496. "");
  1497. if (ret < 0 || ret >= end - pos)
  1498. return pos - buf;
  1499. pos += ret;
  1500. ret = os_snprintf(pos, end - pos, "\n");
  1501. if (ret < 0 || ret >= end - pos)
  1502. return pos - buf;
  1503. pos += ret;
  1504. ssid = ssid->next;
  1505. }
  1506. return pos - buf;
  1507. }
  1508. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1509. {
  1510. int ret;
  1511. ret = os_snprintf(pos, end - pos, "-");
  1512. if (ret < 0 || ret >= end - pos)
  1513. return pos;
  1514. pos += ret;
  1515. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1516. if (ret < 0)
  1517. return pos;
  1518. pos += ret;
  1519. return pos;
  1520. }
  1521. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1522. const u8 *ie, size_t ie_len)
  1523. {
  1524. struct wpa_ie_data data;
  1525. int first, ret;
  1526. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1527. if (ret < 0 || ret >= end - pos)
  1528. return pos;
  1529. pos += ret;
  1530. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1531. ret = os_snprintf(pos, end - pos, "?]");
  1532. if (ret < 0 || ret >= end - pos)
  1533. return pos;
  1534. pos += ret;
  1535. return pos;
  1536. }
  1537. first = 1;
  1538. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1539. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  1540. if (ret < 0 || ret >= end - pos)
  1541. return pos;
  1542. pos += ret;
  1543. first = 0;
  1544. }
  1545. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1546. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  1547. if (ret < 0 || ret >= end - pos)
  1548. return pos;
  1549. pos += ret;
  1550. first = 0;
  1551. }
  1552. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1553. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  1554. if (ret < 0 || ret >= end - pos)
  1555. return pos;
  1556. pos += ret;
  1557. first = 0;
  1558. }
  1559. #ifdef CONFIG_IEEE80211R
  1560. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1561. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1562. first ? "" : "+");
  1563. if (ret < 0 || ret >= end - pos)
  1564. return pos;
  1565. pos += ret;
  1566. first = 0;
  1567. }
  1568. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1569. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1570. first ? "" : "+");
  1571. if (ret < 0 || ret >= end - pos)
  1572. return pos;
  1573. pos += ret;
  1574. first = 0;
  1575. }
  1576. #endif /* CONFIG_IEEE80211R */
  1577. #ifdef CONFIG_IEEE80211W
  1578. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1579. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1580. first ? "" : "+");
  1581. if (ret < 0 || ret >= end - pos)
  1582. return pos;
  1583. pos += ret;
  1584. first = 0;
  1585. }
  1586. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1587. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1588. first ? "" : "+");
  1589. if (ret < 0 || ret >= end - pos)
  1590. return pos;
  1591. pos += ret;
  1592. first = 0;
  1593. }
  1594. #endif /* CONFIG_IEEE80211W */
  1595. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1596. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1597. ret = os_snprintf(pos, end - pos, "-preauth");
  1598. if (ret < 0 || ret >= end - pos)
  1599. return pos;
  1600. pos += ret;
  1601. }
  1602. ret = os_snprintf(pos, end - pos, "]");
  1603. if (ret < 0 || ret >= end - pos)
  1604. return pos;
  1605. pos += ret;
  1606. return pos;
  1607. }
  1608. #ifdef CONFIG_WPS
  1609. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1610. char *pos, char *end,
  1611. struct wpabuf *wps_ie)
  1612. {
  1613. int ret;
  1614. const char *txt;
  1615. if (wps_ie == NULL)
  1616. return pos;
  1617. if (wps_is_selected_pbc_registrar(wps_ie))
  1618. txt = "[WPS-PBC]";
  1619. #ifdef CONFIG_WPS2
  1620. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1621. txt = "[WPS-AUTH]";
  1622. #endif /* CONFIG_WPS2 */
  1623. else if (wps_is_selected_pin_registrar(wps_ie))
  1624. txt = "[WPS-PIN]";
  1625. else
  1626. txt = "[WPS]";
  1627. ret = os_snprintf(pos, end - pos, "%s", txt);
  1628. if (ret >= 0 && ret < end - pos)
  1629. pos += ret;
  1630. wpabuf_free(wps_ie);
  1631. return pos;
  1632. }
  1633. #endif /* CONFIG_WPS */
  1634. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1635. char *pos, char *end,
  1636. const struct wpa_bss *bss)
  1637. {
  1638. #ifdef CONFIG_WPS
  1639. struct wpabuf *wps_ie;
  1640. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1641. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1642. #else /* CONFIG_WPS */
  1643. return pos;
  1644. #endif /* CONFIG_WPS */
  1645. }
  1646. /* Format one result on one text line into a buffer. */
  1647. static int wpa_supplicant_ctrl_iface_scan_result(
  1648. struct wpa_supplicant *wpa_s,
  1649. const struct wpa_bss *bss, char *buf, size_t buflen)
  1650. {
  1651. char *pos, *end;
  1652. int ret;
  1653. const u8 *ie, *ie2, *p2p;
  1654. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1655. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1656. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1657. 0)
  1658. return 0; /* Do not show P2P listen discovery results here */
  1659. pos = buf;
  1660. end = buf + buflen;
  1661. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1662. MAC2STR(bss->bssid), bss->freq, bss->level);
  1663. if (ret < 0 || ret >= end - pos)
  1664. return -1;
  1665. pos += ret;
  1666. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1667. if (ie)
  1668. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1669. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1670. if (ie2)
  1671. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1672. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1673. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1674. ret = os_snprintf(pos, end - pos, "[WEP]");
  1675. if (ret < 0 || ret >= end - pos)
  1676. return -1;
  1677. pos += ret;
  1678. }
  1679. if (bss->caps & IEEE80211_CAP_IBSS) {
  1680. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1681. if (ret < 0 || ret >= end - pos)
  1682. return -1;
  1683. pos += ret;
  1684. }
  1685. if (bss->caps & IEEE80211_CAP_ESS) {
  1686. ret = os_snprintf(pos, end - pos, "[ESS]");
  1687. if (ret < 0 || ret >= end - pos)
  1688. return -1;
  1689. pos += ret;
  1690. }
  1691. if (p2p) {
  1692. ret = os_snprintf(pos, end - pos, "[P2P]");
  1693. if (ret < 0 || ret >= end - pos)
  1694. return -1;
  1695. pos += ret;
  1696. }
  1697. #ifdef CONFIG_HS20
  1698. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1699. ret = os_snprintf(pos, end - pos, "[HS20]");
  1700. if (ret < 0 || ret >= end - pos)
  1701. return -1;
  1702. pos += ret;
  1703. }
  1704. #endif /* CONFIG_HS20 */
  1705. ret = os_snprintf(pos, end - pos, "\t%s",
  1706. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1707. if (ret < 0 || ret >= end - pos)
  1708. return -1;
  1709. pos += ret;
  1710. ret = os_snprintf(pos, end - pos, "\n");
  1711. if (ret < 0 || ret >= end - pos)
  1712. return -1;
  1713. pos += ret;
  1714. return pos - buf;
  1715. }
  1716. static int wpa_supplicant_ctrl_iface_scan_results(
  1717. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1718. {
  1719. char *pos, *end;
  1720. struct wpa_bss *bss;
  1721. int ret;
  1722. pos = buf;
  1723. end = buf + buflen;
  1724. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1725. "flags / ssid\n");
  1726. if (ret < 0 || ret >= end - pos)
  1727. return pos - buf;
  1728. pos += ret;
  1729. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1730. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1731. end - pos);
  1732. if (ret < 0 || ret >= end - pos)
  1733. return pos - buf;
  1734. pos += ret;
  1735. }
  1736. return pos - buf;
  1737. }
  1738. static int wpa_supplicant_ctrl_iface_select_network(
  1739. struct wpa_supplicant *wpa_s, char *cmd)
  1740. {
  1741. int id;
  1742. struct wpa_ssid *ssid;
  1743. /* cmd: "<network id>" or "any" */
  1744. if (os_strcmp(cmd, "any") == 0) {
  1745. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1746. ssid = NULL;
  1747. } else {
  1748. id = atoi(cmd);
  1749. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1750. ssid = wpa_config_get_network(wpa_s->conf, id);
  1751. if (ssid == NULL) {
  1752. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1753. "network id=%d", id);
  1754. return -1;
  1755. }
  1756. if (ssid->disabled == 2) {
  1757. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1758. "SELECT_NETWORK with persistent P2P group");
  1759. return -1;
  1760. }
  1761. }
  1762. wpa_supplicant_select_network(wpa_s, ssid);
  1763. return 0;
  1764. }
  1765. static int wpa_supplicant_ctrl_iface_enable_network(
  1766. struct wpa_supplicant *wpa_s, char *cmd)
  1767. {
  1768. int id;
  1769. struct wpa_ssid *ssid;
  1770. /* cmd: "<network id>" or "all" */
  1771. if (os_strcmp(cmd, "all") == 0) {
  1772. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  1773. ssid = NULL;
  1774. } else {
  1775. id = atoi(cmd);
  1776. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  1777. ssid = wpa_config_get_network(wpa_s->conf, id);
  1778. if (ssid == NULL) {
  1779. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1780. "network id=%d", id);
  1781. return -1;
  1782. }
  1783. if (ssid->disabled == 2) {
  1784. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1785. "ENABLE_NETWORK with persistent P2P group");
  1786. return -1;
  1787. }
  1788. if (os_strstr(cmd, " no-connect")) {
  1789. ssid->disabled = 0;
  1790. return 0;
  1791. }
  1792. }
  1793. wpa_supplicant_enable_network(wpa_s, ssid);
  1794. return 0;
  1795. }
  1796. static int wpa_supplicant_ctrl_iface_disable_network(
  1797. struct wpa_supplicant *wpa_s, char *cmd)
  1798. {
  1799. int id;
  1800. struct wpa_ssid *ssid;
  1801. /* cmd: "<network id>" or "all" */
  1802. if (os_strcmp(cmd, "all") == 0) {
  1803. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1804. ssid = NULL;
  1805. } else {
  1806. id = atoi(cmd);
  1807. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1808. ssid = wpa_config_get_network(wpa_s->conf, id);
  1809. if (ssid == NULL) {
  1810. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1811. "network id=%d", id);
  1812. return -1;
  1813. }
  1814. if (ssid->disabled == 2) {
  1815. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1816. "DISABLE_NETWORK with persistent P2P "
  1817. "group");
  1818. return -1;
  1819. }
  1820. }
  1821. wpa_supplicant_disable_network(wpa_s, ssid);
  1822. return 0;
  1823. }
  1824. static int wpa_supplicant_ctrl_iface_add_network(
  1825. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1826. {
  1827. struct wpa_ssid *ssid;
  1828. int ret;
  1829. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1830. ssid = wpa_config_add_network(wpa_s->conf);
  1831. if (ssid == NULL)
  1832. return -1;
  1833. wpas_notify_network_added(wpa_s, ssid);
  1834. ssid->disabled = 1;
  1835. wpa_config_set_network_defaults(ssid);
  1836. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1837. if (ret < 0 || (size_t) ret >= buflen)
  1838. return -1;
  1839. return ret;
  1840. }
  1841. static int wpa_supplicant_ctrl_iface_remove_network(
  1842. struct wpa_supplicant *wpa_s, char *cmd)
  1843. {
  1844. int id;
  1845. struct wpa_ssid *ssid;
  1846. /* cmd: "<network id>" or "all" */
  1847. if (os_strcmp(cmd, "all") == 0) {
  1848. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1849. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1850. if (wpa_s->current_ssid) {
  1851. #ifdef CONFIG_SME
  1852. wpa_s->sme.prev_bssid_set = 0;
  1853. #endif /* CONFIG_SME */
  1854. wpa_sm_set_config(wpa_s->wpa, NULL);
  1855. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1856. wpa_supplicant_deauthenticate(
  1857. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1858. }
  1859. ssid = wpa_s->conf->ssid;
  1860. while (ssid) {
  1861. struct wpa_ssid *remove_ssid = ssid;
  1862. id = ssid->id;
  1863. ssid = ssid->next;
  1864. wpas_notify_network_removed(wpa_s, remove_ssid);
  1865. wpa_config_remove_network(wpa_s->conf, id);
  1866. }
  1867. return 0;
  1868. }
  1869. id = atoi(cmd);
  1870. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1871. ssid = wpa_config_get_network(wpa_s->conf, id);
  1872. if (ssid)
  1873. wpas_notify_network_removed(wpa_s, ssid);
  1874. if (ssid == NULL) {
  1875. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1876. "id=%d", id);
  1877. return -1;
  1878. }
  1879. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  1880. #ifdef CONFIG_SME
  1881. wpa_s->sme.prev_bssid_set = 0;
  1882. #endif /* CONFIG_SME */
  1883. /*
  1884. * Invalidate the EAP session cache if the current or
  1885. * previously used network is removed.
  1886. */
  1887. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1888. }
  1889. if (ssid == wpa_s->current_ssid) {
  1890. wpa_sm_set_config(wpa_s->wpa, NULL);
  1891. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1892. wpa_supplicant_deauthenticate(wpa_s,
  1893. WLAN_REASON_DEAUTH_LEAVING);
  1894. }
  1895. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1896. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  1897. "network id=%d", id);
  1898. return -1;
  1899. }
  1900. return 0;
  1901. }
  1902. static int wpa_supplicant_ctrl_iface_set_network(
  1903. struct wpa_supplicant *wpa_s, char *cmd)
  1904. {
  1905. int id;
  1906. struct wpa_ssid *ssid;
  1907. char *name, *value;
  1908. /* cmd: "<network id> <variable name> <value>" */
  1909. name = os_strchr(cmd, ' ');
  1910. if (name == NULL)
  1911. return -1;
  1912. *name++ = '\0';
  1913. value = os_strchr(name, ' ');
  1914. if (value == NULL)
  1915. return -1;
  1916. *value++ = '\0';
  1917. id = atoi(cmd);
  1918. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1919. id, name);
  1920. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1921. (u8 *) value, os_strlen(value));
  1922. ssid = wpa_config_get_network(wpa_s->conf, id);
  1923. if (ssid == NULL) {
  1924. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1925. "id=%d", id);
  1926. return -1;
  1927. }
  1928. if (wpa_config_set(ssid, name, value, 0) < 0) {
  1929. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  1930. "variable '%s'", name);
  1931. return -1;
  1932. }
  1933. if (os_strcmp(name, "bssid") != 0 &&
  1934. os_strcmp(name, "priority") != 0)
  1935. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  1936. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  1937. /*
  1938. * Invalidate the EAP session cache if anything in the current
  1939. * or previously used configuration changes.
  1940. */
  1941. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1942. }
  1943. if ((os_strcmp(name, "psk") == 0 &&
  1944. value[0] == '"' && ssid->ssid_len) ||
  1945. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  1946. wpa_config_update_psk(ssid);
  1947. else if (os_strcmp(name, "priority") == 0)
  1948. wpa_config_update_prio_list(wpa_s->conf);
  1949. return 0;
  1950. }
  1951. static int wpa_supplicant_ctrl_iface_get_network(
  1952. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1953. {
  1954. int id;
  1955. size_t res;
  1956. struct wpa_ssid *ssid;
  1957. char *name, *value;
  1958. /* cmd: "<network id> <variable name>" */
  1959. name = os_strchr(cmd, ' ');
  1960. if (name == NULL || buflen == 0)
  1961. return -1;
  1962. *name++ = '\0';
  1963. id = atoi(cmd);
  1964. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1965. id, name);
  1966. ssid = wpa_config_get_network(wpa_s->conf, id);
  1967. if (ssid == NULL) {
  1968. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1969. "id=%d", id);
  1970. return -1;
  1971. }
  1972. value = wpa_config_get_no_key(ssid, name);
  1973. if (value == NULL) {
  1974. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1975. "variable '%s'", name);
  1976. return -1;
  1977. }
  1978. res = os_strlcpy(buf, value, buflen);
  1979. if (res >= buflen) {
  1980. os_free(value);
  1981. return -1;
  1982. }
  1983. os_free(value);
  1984. return res;
  1985. }
  1986. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  1987. char *buf, size_t buflen)
  1988. {
  1989. char *pos, *end;
  1990. struct wpa_cred *cred;
  1991. int ret;
  1992. pos = buf;
  1993. end = buf + buflen;
  1994. ret = os_snprintf(pos, end - pos,
  1995. "cred id / realm / username / domain / imsi\n");
  1996. if (ret < 0 || ret >= end - pos)
  1997. return pos - buf;
  1998. pos += ret;
  1999. cred = wpa_s->conf->cred;
  2000. while (cred) {
  2001. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2002. cred->id, cred->realm ? cred->realm : "",
  2003. cred->username ? cred->username : "",
  2004. cred->domain ? cred->domain : "",
  2005. cred->imsi ? cred->imsi : "");
  2006. if (ret < 0 || ret >= end - pos)
  2007. return pos - buf;
  2008. pos += ret;
  2009. cred = cred->next;
  2010. }
  2011. return pos - buf;
  2012. }
  2013. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2014. char *buf, size_t buflen)
  2015. {
  2016. struct wpa_cred *cred;
  2017. int ret;
  2018. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2019. cred = wpa_config_add_cred(wpa_s->conf);
  2020. if (cred == NULL)
  2021. return -1;
  2022. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2023. if (ret < 0 || (size_t) ret >= buflen)
  2024. return -1;
  2025. return ret;
  2026. }
  2027. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2028. struct wpa_cred *cred)
  2029. {
  2030. struct wpa_ssid *ssid;
  2031. char str[20];
  2032. if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
  2033. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2034. return -1;
  2035. }
  2036. /* Remove any network entry created based on the removed credential */
  2037. ssid = wpa_s->conf->ssid;
  2038. while (ssid) {
  2039. if (ssid->parent_cred == cred) {
  2040. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2041. "used the removed credential", ssid->id);
  2042. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2043. ssid = ssid->next;
  2044. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2045. } else
  2046. ssid = ssid->next;
  2047. }
  2048. return 0;
  2049. }
  2050. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2051. char *cmd)
  2052. {
  2053. int id;
  2054. struct wpa_cred *cred, *prev;
  2055. /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
  2056. if (os_strcmp(cmd, "all") == 0) {
  2057. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2058. cred = wpa_s->conf->cred;
  2059. while (cred) {
  2060. prev = cred;
  2061. cred = cred->next;
  2062. wpas_ctrl_remove_cred(wpa_s, prev);
  2063. }
  2064. return 0;
  2065. }
  2066. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2067. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2068. cmd + 8);
  2069. cred = wpa_s->conf->cred;
  2070. while (cred) {
  2071. prev = cred;
  2072. cred = cred->next;
  2073. if (prev->domain &&
  2074. os_strcmp(prev->domain, cmd + 8) == 0)
  2075. wpas_ctrl_remove_cred(wpa_s, prev);
  2076. }
  2077. return 0;
  2078. }
  2079. id = atoi(cmd);
  2080. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2081. cred = wpa_config_get_cred(wpa_s->conf, id);
  2082. return wpas_ctrl_remove_cred(wpa_s, cred);
  2083. }
  2084. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2085. char *cmd)
  2086. {
  2087. int id;
  2088. struct wpa_cred *cred;
  2089. char *name, *value;
  2090. /* cmd: "<cred id> <variable name> <value>" */
  2091. name = os_strchr(cmd, ' ');
  2092. if (name == NULL)
  2093. return -1;
  2094. *name++ = '\0';
  2095. value = os_strchr(name, ' ');
  2096. if (value == NULL)
  2097. return -1;
  2098. *value++ = '\0';
  2099. id = atoi(cmd);
  2100. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2101. id, name);
  2102. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2103. (u8 *) value, os_strlen(value));
  2104. cred = wpa_config_get_cred(wpa_s->conf, id);
  2105. if (cred == NULL) {
  2106. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2107. id);
  2108. return -1;
  2109. }
  2110. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2111. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2112. "variable '%s'", name);
  2113. return -1;
  2114. }
  2115. return 0;
  2116. }
  2117. #ifndef CONFIG_NO_CONFIG_WRITE
  2118. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2119. {
  2120. int ret;
  2121. if (!wpa_s->conf->update_config) {
  2122. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2123. "to update configuration (update_config=0)");
  2124. return -1;
  2125. }
  2126. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2127. if (ret) {
  2128. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2129. "update configuration");
  2130. } else {
  2131. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2132. " updated");
  2133. }
  2134. return ret;
  2135. }
  2136. #endif /* CONFIG_NO_CONFIG_WRITE */
  2137. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2138. struct wpa_driver_capa *capa,
  2139. char *buf, size_t buflen)
  2140. {
  2141. int ret, first = 1;
  2142. char *pos, *end;
  2143. size_t len;
  2144. pos = buf;
  2145. end = pos + buflen;
  2146. if (res < 0) {
  2147. if (strict)
  2148. return 0;
  2149. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2150. if (len >= buflen)
  2151. return -1;
  2152. return len;
  2153. }
  2154. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2155. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2156. if (ret < 0 || ret >= end - pos)
  2157. return pos - buf;
  2158. pos += ret;
  2159. first = 0;
  2160. }
  2161. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2162. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2163. if (ret < 0 || ret >= end - pos)
  2164. return pos - buf;
  2165. pos += ret;
  2166. first = 0;
  2167. }
  2168. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2169. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2170. if (ret < 0 || ret >= end - pos)
  2171. return pos - buf;
  2172. pos += ret;
  2173. first = 0;
  2174. }
  2175. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2176. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  2177. if (ret < 0 || ret >= end - pos)
  2178. return pos - buf;
  2179. pos += ret;
  2180. first = 0;
  2181. }
  2182. return pos - buf;
  2183. }
  2184. static int ctrl_iface_get_capability_group(int res, char *strict,
  2185. struct wpa_driver_capa *capa,
  2186. char *buf, size_t buflen)
  2187. {
  2188. int ret, first = 1;
  2189. char *pos, *end;
  2190. size_t len;
  2191. pos = buf;
  2192. end = pos + buflen;
  2193. if (res < 0) {
  2194. if (strict)
  2195. return 0;
  2196. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2197. if (len >= buflen)
  2198. return -1;
  2199. return len;
  2200. }
  2201. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2202. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2203. if (ret < 0 || ret >= end - pos)
  2204. return pos - buf;
  2205. pos += ret;
  2206. first = 0;
  2207. }
  2208. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2209. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2210. if (ret < 0 || ret >= end - pos)
  2211. return pos - buf;
  2212. pos += ret;
  2213. first = 0;
  2214. }
  2215. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2216. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2217. if (ret < 0 || ret >= end - pos)
  2218. return pos - buf;
  2219. pos += ret;
  2220. first = 0;
  2221. }
  2222. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  2223. ret = os_snprintf(pos, end - pos, "%sWEP104",
  2224. first ? "" : " ");
  2225. if (ret < 0 || ret >= end - pos)
  2226. return pos - buf;
  2227. pos += ret;
  2228. first = 0;
  2229. }
  2230. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  2231. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  2232. if (ret < 0 || ret >= end - pos)
  2233. return pos - buf;
  2234. pos += ret;
  2235. first = 0;
  2236. }
  2237. return pos - buf;
  2238. }
  2239. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2240. struct wpa_driver_capa *capa,
  2241. char *buf, size_t buflen)
  2242. {
  2243. int ret;
  2244. char *pos, *end;
  2245. size_t len;
  2246. pos = buf;
  2247. end = pos + buflen;
  2248. if (res < 0) {
  2249. if (strict)
  2250. return 0;
  2251. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2252. "NONE", buflen);
  2253. if (len >= buflen)
  2254. return -1;
  2255. return len;
  2256. }
  2257. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2258. if (ret < 0 || ret >= end - pos)
  2259. return pos - buf;
  2260. pos += ret;
  2261. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2262. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2263. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2264. if (ret < 0 || ret >= end - pos)
  2265. return pos - buf;
  2266. pos += ret;
  2267. }
  2268. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2269. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2270. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2271. if (ret < 0 || ret >= end - pos)
  2272. return pos - buf;
  2273. pos += ret;
  2274. }
  2275. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2276. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2277. if (ret < 0 || ret >= end - pos)
  2278. return pos - buf;
  2279. pos += ret;
  2280. }
  2281. return pos - buf;
  2282. }
  2283. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2284. struct wpa_driver_capa *capa,
  2285. char *buf, size_t buflen)
  2286. {
  2287. int ret, first = 1;
  2288. char *pos, *end;
  2289. size_t len;
  2290. pos = buf;
  2291. end = pos + buflen;
  2292. if (res < 0) {
  2293. if (strict)
  2294. return 0;
  2295. len = os_strlcpy(buf, "RSN WPA", buflen);
  2296. if (len >= buflen)
  2297. return -1;
  2298. return len;
  2299. }
  2300. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2301. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2302. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  2303. if (ret < 0 || ret >= end - pos)
  2304. return pos - buf;
  2305. pos += ret;
  2306. first = 0;
  2307. }
  2308. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2309. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2310. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  2311. if (ret < 0 || ret >= end - pos)
  2312. return pos - buf;
  2313. pos += ret;
  2314. first = 0;
  2315. }
  2316. return pos - buf;
  2317. }
  2318. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2319. struct wpa_driver_capa *capa,
  2320. char *buf, size_t buflen)
  2321. {
  2322. int ret, first = 1;
  2323. char *pos, *end;
  2324. size_t len;
  2325. pos = buf;
  2326. end = pos + buflen;
  2327. if (res < 0) {
  2328. if (strict)
  2329. return 0;
  2330. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2331. if (len >= buflen)
  2332. return -1;
  2333. return len;
  2334. }
  2335. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2336. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  2337. if (ret < 0 || ret >= end - pos)
  2338. return pos - buf;
  2339. pos += ret;
  2340. first = 0;
  2341. }
  2342. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2343. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2344. first ? "" : " ");
  2345. if (ret < 0 || ret >= end - pos)
  2346. return pos - buf;
  2347. pos += ret;
  2348. first = 0;
  2349. }
  2350. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2351. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  2352. if (ret < 0 || ret >= end - pos)
  2353. return pos - buf;
  2354. pos += ret;
  2355. first = 0;
  2356. }
  2357. return pos - buf;
  2358. }
  2359. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2360. char *buf, size_t buflen)
  2361. {
  2362. struct hostapd_channel_data *chnl;
  2363. int ret, i, j;
  2364. char *pos, *end, *hmode;
  2365. pos = buf;
  2366. end = pos + buflen;
  2367. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2368. switch (wpa_s->hw.modes[j].mode) {
  2369. case HOSTAPD_MODE_IEEE80211B:
  2370. hmode = "B";
  2371. break;
  2372. case HOSTAPD_MODE_IEEE80211G:
  2373. hmode = "G";
  2374. break;
  2375. case HOSTAPD_MODE_IEEE80211A:
  2376. hmode = "A";
  2377. break;
  2378. case HOSTAPD_MODE_IEEE80211AD:
  2379. hmode = "AD";
  2380. break;
  2381. default:
  2382. continue;
  2383. }
  2384. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2385. if (ret < 0 || ret >= end - pos)
  2386. return pos - buf;
  2387. pos += ret;
  2388. chnl = wpa_s->hw.modes[j].channels;
  2389. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2390. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2391. continue;
  2392. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2393. if (ret < 0 || ret >= end - pos)
  2394. return pos - buf;
  2395. pos += ret;
  2396. }
  2397. ret = os_snprintf(pos, end - pos, "\n");
  2398. if (ret < 0 || ret >= end - pos)
  2399. return pos - buf;
  2400. pos += ret;
  2401. }
  2402. return pos - buf;
  2403. }
  2404. static int wpa_supplicant_ctrl_iface_get_capability(
  2405. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2406. size_t buflen)
  2407. {
  2408. struct wpa_driver_capa capa;
  2409. int res;
  2410. char *strict;
  2411. char field[30];
  2412. size_t len;
  2413. /* Determine whether or not strict checking was requested */
  2414. len = os_strlcpy(field, _field, sizeof(field));
  2415. if (len >= sizeof(field))
  2416. return -1;
  2417. strict = os_strchr(field, ' ');
  2418. if (strict != NULL) {
  2419. *strict++ = '\0';
  2420. if (os_strcmp(strict, "strict") != 0)
  2421. return -1;
  2422. }
  2423. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2424. field, strict ? strict : "");
  2425. if (os_strcmp(field, "eap") == 0) {
  2426. return eap_get_names(buf, buflen);
  2427. }
  2428. res = wpa_drv_get_capa(wpa_s, &capa);
  2429. if (os_strcmp(field, "pairwise") == 0)
  2430. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2431. buf, buflen);
  2432. if (os_strcmp(field, "group") == 0)
  2433. return ctrl_iface_get_capability_group(res, strict, &capa,
  2434. buf, buflen);
  2435. if (os_strcmp(field, "key_mgmt") == 0)
  2436. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2437. buf, buflen);
  2438. if (os_strcmp(field, "proto") == 0)
  2439. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2440. buf, buflen);
  2441. if (os_strcmp(field, "auth_alg") == 0)
  2442. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2443. buf, buflen);
  2444. if (os_strcmp(field, "channels") == 0)
  2445. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2446. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2447. field);
  2448. return -1;
  2449. }
  2450. #ifdef CONFIG_INTERWORKING
  2451. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2452. struct wpabuf *data)
  2453. {
  2454. char *start = pos;
  2455. size_t i;
  2456. int ret;
  2457. const u8 *d;
  2458. if (data == NULL)
  2459. return start;
  2460. ret = os_snprintf(pos, end - pos, "%s=", title);
  2461. if (ret < 0 || ret >= end - pos)
  2462. return start;
  2463. pos += ret;
  2464. d = wpabuf_head_u8(data);
  2465. for (i = 0; i < wpabuf_len(data); i++) {
  2466. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2467. if (ret < 0 || ret >= end - pos)
  2468. return start;
  2469. pos += ret;
  2470. }
  2471. ret = os_snprintf(pos, end - pos, "\n");
  2472. if (ret < 0 || ret >= end - pos)
  2473. return start;
  2474. pos += ret;
  2475. return pos;
  2476. }
  2477. #endif /* CONFIG_INTERWORKING */
  2478. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2479. unsigned long mask, char *buf, size_t buflen)
  2480. {
  2481. size_t i;
  2482. int ret;
  2483. char *pos, *end;
  2484. const u8 *ie, *ie2;
  2485. pos = buf;
  2486. end = buf + buflen;
  2487. if (mask & WPA_BSS_MASK_ID) {
  2488. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2489. if (ret < 0 || ret >= end - pos)
  2490. return 0;
  2491. pos += ret;
  2492. }
  2493. if (mask & WPA_BSS_MASK_BSSID) {
  2494. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2495. MAC2STR(bss->bssid));
  2496. if (ret < 0 || ret >= end - pos)
  2497. return 0;
  2498. pos += ret;
  2499. }
  2500. if (mask & WPA_BSS_MASK_FREQ) {
  2501. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2502. if (ret < 0 || ret >= end - pos)
  2503. return 0;
  2504. pos += ret;
  2505. }
  2506. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2507. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2508. bss->beacon_int);
  2509. if (ret < 0 || ret >= end - pos)
  2510. return 0;
  2511. pos += ret;
  2512. }
  2513. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2514. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2515. bss->caps);
  2516. if (ret < 0 || ret >= end - pos)
  2517. return 0;
  2518. pos += ret;
  2519. }
  2520. if (mask & WPA_BSS_MASK_QUAL) {
  2521. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2522. if (ret < 0 || ret >= end - pos)
  2523. return 0;
  2524. pos += ret;
  2525. }
  2526. if (mask & WPA_BSS_MASK_NOISE) {
  2527. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2528. if (ret < 0 || ret >= end - pos)
  2529. return 0;
  2530. pos += ret;
  2531. }
  2532. if (mask & WPA_BSS_MASK_LEVEL) {
  2533. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2534. if (ret < 0 || ret >= end - pos)
  2535. return 0;
  2536. pos += ret;
  2537. }
  2538. if (mask & WPA_BSS_MASK_TSF) {
  2539. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2540. (unsigned long long) bss->tsf);
  2541. if (ret < 0 || ret >= end - pos)
  2542. return 0;
  2543. pos += ret;
  2544. }
  2545. if (mask & WPA_BSS_MASK_AGE) {
  2546. struct os_time now;
  2547. os_get_time(&now);
  2548. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2549. (int) (now.sec - bss->last_update.sec));
  2550. if (ret < 0 || ret >= end - pos)
  2551. return 0;
  2552. pos += ret;
  2553. }
  2554. if (mask & WPA_BSS_MASK_IE) {
  2555. ret = os_snprintf(pos, end - pos, "ie=");
  2556. if (ret < 0 || ret >= end - pos)
  2557. return 0;
  2558. pos += ret;
  2559. ie = (const u8 *) (bss + 1);
  2560. for (i = 0; i < bss->ie_len; i++) {
  2561. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2562. if (ret < 0 || ret >= end - pos)
  2563. return 0;
  2564. pos += ret;
  2565. }
  2566. ret = os_snprintf(pos, end - pos, "\n");
  2567. if (ret < 0 || ret >= end - pos)
  2568. return 0;
  2569. pos += ret;
  2570. }
  2571. if (mask & WPA_BSS_MASK_FLAGS) {
  2572. ret = os_snprintf(pos, end - pos, "flags=");
  2573. if (ret < 0 || ret >= end - pos)
  2574. return 0;
  2575. pos += ret;
  2576. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2577. if (ie)
  2578. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2579. 2 + ie[1]);
  2580. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2581. if (ie2)
  2582. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2583. 2 + ie2[1]);
  2584. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2585. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2586. ret = os_snprintf(pos, end - pos, "[WEP]");
  2587. if (ret < 0 || ret >= end - pos)
  2588. return 0;
  2589. pos += ret;
  2590. }
  2591. if (bss->caps & IEEE80211_CAP_IBSS) {
  2592. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2593. if (ret < 0 || ret >= end - pos)
  2594. return 0;
  2595. pos += ret;
  2596. }
  2597. if (bss->caps & IEEE80211_CAP_ESS) {
  2598. ret = os_snprintf(pos, end - pos, "[ESS]");
  2599. if (ret < 0 || ret >= end - pos)
  2600. return 0;
  2601. pos += ret;
  2602. }
  2603. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
  2604. ret = os_snprintf(pos, end - pos, "[P2P]");
  2605. if (ret < 0 || ret >= end - pos)
  2606. return 0;
  2607. pos += ret;
  2608. }
  2609. #ifdef CONFIG_HS20
  2610. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2611. ret = os_snprintf(pos, end - pos, "[HS20]");
  2612. if (ret < 0 || ret >= end - pos)
  2613. return -1;
  2614. pos += ret;
  2615. }
  2616. #endif /* CONFIG_HS20 */
  2617. ret = os_snprintf(pos, end - pos, "\n");
  2618. if (ret < 0 || ret >= end - pos)
  2619. return 0;
  2620. pos += ret;
  2621. }
  2622. if (mask & WPA_BSS_MASK_SSID) {
  2623. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2624. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2625. if (ret < 0 || ret >= end - pos)
  2626. return 0;
  2627. pos += ret;
  2628. }
  2629. #ifdef CONFIG_WPS
  2630. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2631. ie = (const u8 *) (bss + 1);
  2632. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2633. if (ret < 0 || ret >= end - pos)
  2634. return 0;
  2635. pos += ret;
  2636. }
  2637. #endif /* CONFIG_WPS */
  2638. #ifdef CONFIG_P2P
  2639. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2640. ie = (const u8 *) (bss + 1);
  2641. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2642. if (ret < 0 || ret >= end - pos)
  2643. return 0;
  2644. pos += ret;
  2645. }
  2646. #endif /* CONFIG_P2P */
  2647. #ifdef CONFIG_WIFI_DISPLAY
  2648. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  2649. struct wpabuf *wfd;
  2650. ie = (const u8 *) (bss + 1);
  2651. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  2652. WFD_IE_VENDOR_TYPE);
  2653. if (wfd) {
  2654. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  2655. if (ret < 0 || ret >= end - pos)
  2656. return pos - buf;
  2657. pos += ret;
  2658. pos += wpa_snprintf_hex(pos, end - pos,
  2659. wpabuf_head(wfd),
  2660. wpabuf_len(wfd));
  2661. wpabuf_free(wfd);
  2662. ret = os_snprintf(pos, end - pos, "\n");
  2663. if (ret < 0 || ret >= end - pos)
  2664. return pos - buf;
  2665. pos += ret;
  2666. }
  2667. }
  2668. #endif /* CONFIG_WIFI_DISPLAY */
  2669. #ifdef CONFIG_INTERWORKING
  2670. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  2671. struct wpa_bss_anqp *anqp = bss->anqp;
  2672. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  2673. anqp->venue_name);
  2674. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2675. anqp->network_auth_type);
  2676. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2677. anqp->roaming_consortium);
  2678. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2679. anqp->ip_addr_type_availability);
  2680. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  2681. anqp->nai_realm);
  2682. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  2683. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2684. anqp->domain_name);
  2685. #ifdef CONFIG_HS20
  2686. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  2687. anqp->hs20_operator_friendly_name);
  2688. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  2689. anqp->hs20_wan_metrics);
  2690. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  2691. anqp->hs20_connection_capability);
  2692. #endif /* CONFIG_HS20 */
  2693. }
  2694. #endif /* CONFIG_INTERWORKING */
  2695. return pos - buf;
  2696. }
  2697. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2698. const char *cmd, char *buf,
  2699. size_t buflen)
  2700. {
  2701. u8 bssid[ETH_ALEN];
  2702. size_t i;
  2703. struct wpa_bss *bss;
  2704. struct wpa_bss *bsslast = NULL;
  2705. struct dl_list *next;
  2706. int ret = 0;
  2707. int len;
  2708. char *ctmp;
  2709. unsigned long mask = WPA_BSS_MASK_ALL;
  2710. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  2711. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  2712. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  2713. list_id);
  2714. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  2715. list_id);
  2716. } else { /* N1-N2 */
  2717. unsigned int id1, id2;
  2718. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  2719. wpa_printf(MSG_INFO, "Wrong BSS range "
  2720. "format");
  2721. return 0;
  2722. }
  2723. if (*(cmd + 6) == '-')
  2724. id1 = 0;
  2725. else
  2726. id1 = atoi(cmd + 6);
  2727. ctmp++;
  2728. if (*ctmp >= '0' && *ctmp <= '9')
  2729. id2 = atoi(ctmp);
  2730. else
  2731. id2 = (unsigned int) -1;
  2732. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  2733. if (id2 == (unsigned int) -1)
  2734. bsslast = dl_list_last(&wpa_s->bss_id,
  2735. struct wpa_bss,
  2736. list_id);
  2737. else {
  2738. bsslast = wpa_bss_get_id(wpa_s, id2);
  2739. if (bsslast == NULL && bss && id2 > id1) {
  2740. struct wpa_bss *tmp = bss;
  2741. for (;;) {
  2742. next = tmp->list_id.next;
  2743. if (next == &wpa_s->bss_id)
  2744. break;
  2745. tmp = dl_list_entry(
  2746. next, struct wpa_bss,
  2747. list_id);
  2748. if (tmp->id > id2)
  2749. break;
  2750. bsslast = tmp;
  2751. }
  2752. }
  2753. }
  2754. }
  2755. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  2756. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  2757. else if (os_strncmp(cmd, "LAST", 4) == 0)
  2758. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  2759. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2760. i = atoi(cmd + 3);
  2761. bss = wpa_bss_get_id(wpa_s, i);
  2762. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2763. i = atoi(cmd + 5);
  2764. bss = wpa_bss_get_id(wpa_s, i);
  2765. if (bss) {
  2766. next = bss->list_id.next;
  2767. if (next == &wpa_s->bss_id)
  2768. bss = NULL;
  2769. else
  2770. bss = dl_list_entry(next, struct wpa_bss,
  2771. list_id);
  2772. }
  2773. #ifdef CONFIG_P2P
  2774. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2775. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2776. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2777. else
  2778. bss = NULL;
  2779. #endif /* CONFIG_P2P */
  2780. } else if (hwaddr_aton(cmd, bssid) == 0)
  2781. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2782. else {
  2783. struct wpa_bss *tmp;
  2784. i = atoi(cmd);
  2785. bss = NULL;
  2786. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2787. {
  2788. if (i-- == 0) {
  2789. bss = tmp;
  2790. break;
  2791. }
  2792. }
  2793. }
  2794. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  2795. mask = strtoul(ctmp + 5, NULL, 0x10);
  2796. if (mask == 0)
  2797. mask = WPA_BSS_MASK_ALL;
  2798. }
  2799. if (bss == NULL)
  2800. return 0;
  2801. if (bsslast == NULL)
  2802. bsslast = bss;
  2803. do {
  2804. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  2805. ret += len;
  2806. buf += len;
  2807. buflen -= len;
  2808. if (bss == bsslast)
  2809. break;
  2810. next = bss->list_id.next;
  2811. if (next == &wpa_s->bss_id)
  2812. break;
  2813. bss = dl_list_entry(next, struct wpa_bss, list_id);
  2814. } while (bss && len);
  2815. return ret;
  2816. }
  2817. static int wpa_supplicant_ctrl_iface_ap_scan(
  2818. struct wpa_supplicant *wpa_s, char *cmd)
  2819. {
  2820. int ap_scan = atoi(cmd);
  2821. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  2822. }
  2823. static int wpa_supplicant_ctrl_iface_scan_interval(
  2824. struct wpa_supplicant *wpa_s, char *cmd)
  2825. {
  2826. int scan_int = atoi(cmd);
  2827. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  2828. }
  2829. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  2830. struct wpa_supplicant *wpa_s, char *cmd)
  2831. {
  2832. int expire_age = atoi(cmd);
  2833. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  2834. }
  2835. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  2836. struct wpa_supplicant *wpa_s, char *cmd)
  2837. {
  2838. int expire_count = atoi(cmd);
  2839. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  2840. }
  2841. static int wpa_supplicant_ctrl_iface_bss_flush(
  2842. struct wpa_supplicant *wpa_s, char *cmd)
  2843. {
  2844. int flush_age = atoi(cmd);
  2845. if (flush_age == 0)
  2846. wpa_bss_flush(wpa_s);
  2847. else
  2848. wpa_bss_flush_by_age(wpa_s, flush_age);
  2849. return 0;
  2850. }
  2851. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  2852. {
  2853. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  2854. /* MLME-DELETEKEYS.request */
  2855. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  2856. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  2857. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  2858. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  2859. #ifdef CONFIG_IEEE80211W
  2860. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  2861. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  2862. #endif /* CONFIG_IEEE80211W */
  2863. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  2864. 0);
  2865. /* MLME-SETPROTECTION.request(None) */
  2866. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  2867. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  2868. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  2869. wpa_sm_drop_sa(wpa_s->wpa);
  2870. }
  2871. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  2872. char *addr)
  2873. {
  2874. #ifdef CONFIG_NO_SCAN_PROCESSING
  2875. return -1;
  2876. #else /* CONFIG_NO_SCAN_PROCESSING */
  2877. u8 bssid[ETH_ALEN];
  2878. struct wpa_bss *bss;
  2879. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2880. if (hwaddr_aton(addr, bssid)) {
  2881. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  2882. "address '%s'", addr);
  2883. return -1;
  2884. }
  2885. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  2886. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2887. if (!bss) {
  2888. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  2889. "from BSS table");
  2890. return -1;
  2891. }
  2892. /*
  2893. * TODO: Find best network configuration block from configuration to
  2894. * allow roaming to other networks
  2895. */
  2896. if (!ssid) {
  2897. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  2898. "configuration known for the target AP");
  2899. return -1;
  2900. }
  2901. wpa_s->reassociate = 1;
  2902. wpa_supplicant_connect(wpa_s, bss, ssid);
  2903. return 0;
  2904. #endif /* CONFIG_NO_SCAN_PROCESSING */
  2905. }
  2906. #ifdef CONFIG_P2P
  2907. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  2908. {
  2909. unsigned int timeout = atoi(cmd);
  2910. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  2911. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  2912. char *pos;
  2913. unsigned int search_delay;
  2914. if (os_strstr(cmd, "type=social"))
  2915. type = P2P_FIND_ONLY_SOCIAL;
  2916. else if (os_strstr(cmd, "type=progressive"))
  2917. type = P2P_FIND_PROGRESSIVE;
  2918. pos = os_strstr(cmd, "dev_id=");
  2919. if (pos) {
  2920. pos += 7;
  2921. if (hwaddr_aton(pos, dev_id))
  2922. return -1;
  2923. _dev_id = dev_id;
  2924. }
  2925. pos = os_strstr(cmd, "delay=");
  2926. if (pos) {
  2927. pos += 6;
  2928. search_delay = atoi(pos);
  2929. } else
  2930. search_delay = wpas_p2p_search_delay(wpa_s);
  2931. return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
  2932. search_delay);
  2933. }
  2934. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  2935. char *buf, size_t buflen)
  2936. {
  2937. u8 addr[ETH_ALEN];
  2938. char *pos, *pos2;
  2939. char *pin = NULL;
  2940. enum p2p_wps_method wps_method;
  2941. int new_pin;
  2942. int ret;
  2943. int persistent_group, persistent_id = -1;
  2944. int join;
  2945. int auth;
  2946. int automatic;
  2947. int go_intent = -1;
  2948. int freq = 0;
  2949. int pd;
  2950. int ht40;
  2951. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  2952. * [persistent|persistent=<network id>]
  2953. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  2954. * [ht40] */
  2955. if (hwaddr_aton(cmd, addr))
  2956. return -1;
  2957. pos = cmd + 17;
  2958. if (*pos != ' ')
  2959. return -1;
  2960. pos++;
  2961. persistent_group = os_strstr(pos, " persistent") != NULL;
  2962. pos2 = os_strstr(pos, " persistent=");
  2963. if (pos2) {
  2964. struct wpa_ssid *ssid;
  2965. persistent_id = atoi(pos2 + 12);
  2966. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  2967. if (ssid == NULL || ssid->disabled != 2 ||
  2968. ssid->mode != WPAS_MODE_P2P_GO) {
  2969. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2970. "SSID id=%d for persistent P2P group (GO)",
  2971. persistent_id);
  2972. return -1;
  2973. }
  2974. }
  2975. join = os_strstr(pos, " join") != NULL;
  2976. auth = os_strstr(pos, " auth") != NULL;
  2977. automatic = os_strstr(pos, " auto") != NULL;
  2978. pd = os_strstr(pos, " provdisc") != NULL;
  2979. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  2980. pos2 = os_strstr(pos, " go_intent=");
  2981. if (pos2) {
  2982. pos2 += 11;
  2983. go_intent = atoi(pos2);
  2984. if (go_intent < 0 || go_intent > 15)
  2985. return -1;
  2986. }
  2987. pos2 = os_strstr(pos, " freq=");
  2988. if (pos2) {
  2989. pos2 += 6;
  2990. freq = atoi(pos2);
  2991. if (freq <= 0)
  2992. return -1;
  2993. }
  2994. if (os_strncmp(pos, "pin", 3) == 0) {
  2995. /* Request random PIN (to be displayed) and enable the PIN */
  2996. wps_method = WPS_PIN_DISPLAY;
  2997. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  2998. wps_method = WPS_PBC;
  2999. } else {
  3000. pin = pos;
  3001. pos = os_strchr(pin, ' ');
  3002. wps_method = WPS_PIN_KEYPAD;
  3003. if (pos) {
  3004. *pos++ = '\0';
  3005. if (os_strncmp(pos, "display", 7) == 0)
  3006. wps_method = WPS_PIN_DISPLAY;
  3007. }
  3008. if (!wps_pin_str_valid(pin)) {
  3009. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3010. return 17;
  3011. }
  3012. }
  3013. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3014. persistent_group, automatic, join,
  3015. auth, go_intent, freq, persistent_id, pd,
  3016. ht40);
  3017. if (new_pin == -2) {
  3018. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3019. return 25;
  3020. }
  3021. if (new_pin == -3) {
  3022. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3023. return 25;
  3024. }
  3025. if (new_pin < 0)
  3026. return -1;
  3027. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3028. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3029. if (ret < 0 || (size_t) ret >= buflen)
  3030. return -1;
  3031. return ret;
  3032. }
  3033. os_memcpy(buf, "OK\n", 3);
  3034. return 3;
  3035. }
  3036. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3037. {
  3038. unsigned int timeout = atoi(cmd);
  3039. return wpas_p2p_listen(wpa_s, timeout);
  3040. }
  3041. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3042. {
  3043. u8 addr[ETH_ALEN];
  3044. char *pos;
  3045. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3046. /* <addr> <config method> [join|auto] */
  3047. if (hwaddr_aton(cmd, addr))
  3048. return -1;
  3049. pos = cmd + 17;
  3050. if (*pos != ' ')
  3051. return -1;
  3052. pos++;
  3053. if (os_strstr(pos, " join") != NULL)
  3054. use = WPAS_P2P_PD_FOR_JOIN;
  3055. else if (os_strstr(pos, " auto") != NULL)
  3056. use = WPAS_P2P_PD_AUTO;
  3057. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3058. }
  3059. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3060. size_t buflen)
  3061. {
  3062. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3063. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3064. ssid->passphrase == NULL)
  3065. return -1;
  3066. os_strlcpy(buf, ssid->passphrase, buflen);
  3067. return os_strlen(buf);
  3068. }
  3069. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3070. char *buf, size_t buflen)
  3071. {
  3072. u64 ref;
  3073. int res;
  3074. u8 dst_buf[ETH_ALEN], *dst;
  3075. struct wpabuf *tlvs;
  3076. char *pos;
  3077. size_t len;
  3078. if (hwaddr_aton(cmd, dst_buf))
  3079. return -1;
  3080. dst = dst_buf;
  3081. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3082. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3083. dst = NULL;
  3084. pos = cmd + 17;
  3085. if (*pos != ' ')
  3086. return -1;
  3087. pos++;
  3088. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3089. u8 version;
  3090. pos += 5;
  3091. if (hexstr2bin(pos, &version, 1) < 0)
  3092. return -1;
  3093. pos += 2;
  3094. if (*pos != ' ')
  3095. return -1;
  3096. pos++;
  3097. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3098. #ifdef CONFIG_WIFI_DISPLAY
  3099. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3100. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3101. #endif /* CONFIG_WIFI_DISPLAY */
  3102. } else {
  3103. len = os_strlen(pos);
  3104. if (len & 1)
  3105. return -1;
  3106. len /= 2;
  3107. tlvs = wpabuf_alloc(len);
  3108. if (tlvs == NULL)
  3109. return -1;
  3110. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3111. wpabuf_free(tlvs);
  3112. return -1;
  3113. }
  3114. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3115. wpabuf_free(tlvs);
  3116. }
  3117. if (ref == 0)
  3118. return -1;
  3119. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3120. if (res < 0 || (unsigned) res >= buflen)
  3121. return -1;
  3122. return res;
  3123. }
  3124. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3125. char *cmd)
  3126. {
  3127. long long unsigned val;
  3128. u64 req;
  3129. if (sscanf(cmd, "%llx", &val) != 1)
  3130. return -1;
  3131. req = val;
  3132. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3133. }
  3134. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3135. {
  3136. int freq;
  3137. u8 dst[ETH_ALEN];
  3138. u8 dialog_token;
  3139. struct wpabuf *resp_tlvs;
  3140. char *pos, *pos2;
  3141. size_t len;
  3142. pos = os_strchr(cmd, ' ');
  3143. if (pos == NULL)
  3144. return -1;
  3145. *pos++ = '\0';
  3146. freq = atoi(cmd);
  3147. if (freq == 0)
  3148. return -1;
  3149. if (hwaddr_aton(pos, dst))
  3150. return -1;
  3151. pos += 17;
  3152. if (*pos != ' ')
  3153. return -1;
  3154. pos++;
  3155. pos2 = os_strchr(pos, ' ');
  3156. if (pos2 == NULL)
  3157. return -1;
  3158. *pos2++ = '\0';
  3159. dialog_token = atoi(pos);
  3160. len = os_strlen(pos2);
  3161. if (len & 1)
  3162. return -1;
  3163. len /= 2;
  3164. resp_tlvs = wpabuf_alloc(len);
  3165. if (resp_tlvs == NULL)
  3166. return -1;
  3167. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3168. wpabuf_free(resp_tlvs);
  3169. return -1;
  3170. }
  3171. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3172. wpabuf_free(resp_tlvs);
  3173. return 0;
  3174. }
  3175. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3176. char *cmd)
  3177. {
  3178. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3179. return -1;
  3180. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3181. return 0;
  3182. }
  3183. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3184. char *cmd)
  3185. {
  3186. char *pos;
  3187. size_t len;
  3188. struct wpabuf *query, *resp;
  3189. pos = os_strchr(cmd, ' ');
  3190. if (pos == NULL)
  3191. return -1;
  3192. *pos++ = '\0';
  3193. len = os_strlen(cmd);
  3194. if (len & 1)
  3195. return -1;
  3196. len /= 2;
  3197. query = wpabuf_alloc(len);
  3198. if (query == NULL)
  3199. return -1;
  3200. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3201. wpabuf_free(query);
  3202. return -1;
  3203. }
  3204. len = os_strlen(pos);
  3205. if (len & 1) {
  3206. wpabuf_free(query);
  3207. return -1;
  3208. }
  3209. len /= 2;
  3210. resp = wpabuf_alloc(len);
  3211. if (resp == NULL) {
  3212. wpabuf_free(query);
  3213. return -1;
  3214. }
  3215. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3216. wpabuf_free(query);
  3217. wpabuf_free(resp);
  3218. return -1;
  3219. }
  3220. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3221. wpabuf_free(query);
  3222. wpabuf_free(resp);
  3223. return -1;
  3224. }
  3225. return 0;
  3226. }
  3227. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3228. {
  3229. char *pos;
  3230. u8 version;
  3231. pos = os_strchr(cmd, ' ');
  3232. if (pos == NULL)
  3233. return -1;
  3234. *pos++ = '\0';
  3235. if (hexstr2bin(cmd, &version, 1) < 0)
  3236. return -1;
  3237. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3238. }
  3239. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3240. {
  3241. char *pos;
  3242. pos = os_strchr(cmd, ' ');
  3243. if (pos == NULL)
  3244. return -1;
  3245. *pos++ = '\0';
  3246. if (os_strcmp(cmd, "bonjour") == 0)
  3247. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3248. if (os_strcmp(cmd, "upnp") == 0)
  3249. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3250. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3251. return -1;
  3252. }
  3253. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3254. char *cmd)
  3255. {
  3256. size_t len;
  3257. struct wpabuf *query;
  3258. int ret;
  3259. len = os_strlen(cmd);
  3260. if (len & 1)
  3261. return -1;
  3262. len /= 2;
  3263. query = wpabuf_alloc(len);
  3264. if (query == NULL)
  3265. return -1;
  3266. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3267. wpabuf_free(query);
  3268. return -1;
  3269. }
  3270. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3271. wpabuf_free(query);
  3272. return ret;
  3273. }
  3274. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3275. {
  3276. char *pos;
  3277. u8 version;
  3278. pos = os_strchr(cmd, ' ');
  3279. if (pos == NULL)
  3280. return -1;
  3281. *pos++ = '\0';
  3282. if (hexstr2bin(cmd, &version, 1) < 0)
  3283. return -1;
  3284. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3285. }
  3286. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3287. {
  3288. char *pos;
  3289. pos = os_strchr(cmd, ' ');
  3290. if (pos == NULL)
  3291. return -1;
  3292. *pos++ = '\0';
  3293. if (os_strcmp(cmd, "bonjour") == 0)
  3294. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3295. if (os_strcmp(cmd, "upnp") == 0)
  3296. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3297. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3298. return -1;
  3299. }
  3300. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3301. {
  3302. u8 addr[ETH_ALEN];
  3303. /* <addr> */
  3304. if (hwaddr_aton(cmd, addr))
  3305. return -1;
  3306. return wpas_p2p_reject(wpa_s, addr);
  3307. }
  3308. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3309. {
  3310. char *pos;
  3311. int id;
  3312. struct wpa_ssid *ssid;
  3313. u8 *_peer = NULL, peer[ETH_ALEN];
  3314. int freq = 0;
  3315. int ht40;
  3316. id = atoi(cmd);
  3317. pos = os_strstr(cmd, " peer=");
  3318. if (pos) {
  3319. pos += 6;
  3320. if (hwaddr_aton(pos, peer))
  3321. return -1;
  3322. _peer = peer;
  3323. }
  3324. ssid = wpa_config_get_network(wpa_s->conf, id);
  3325. if (ssid == NULL || ssid->disabled != 2) {
  3326. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3327. "for persistent P2P group",
  3328. id);
  3329. return -1;
  3330. }
  3331. pos = os_strstr(cmd, " freq=");
  3332. if (pos) {
  3333. pos += 6;
  3334. freq = atoi(pos);
  3335. if (freq <= 0)
  3336. return -1;
  3337. }
  3338. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3339. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40);
  3340. }
  3341. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3342. {
  3343. char *pos;
  3344. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3345. pos = os_strstr(cmd, " peer=");
  3346. if (!pos)
  3347. return -1;
  3348. *pos = '\0';
  3349. pos += 6;
  3350. if (hwaddr_aton(pos, peer)) {
  3351. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3352. return -1;
  3353. }
  3354. pos = os_strstr(pos, " go_dev_addr=");
  3355. if (pos) {
  3356. pos += 13;
  3357. if (hwaddr_aton(pos, go_dev_addr)) {
  3358. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3359. pos);
  3360. return -1;
  3361. }
  3362. go_dev = go_dev_addr;
  3363. }
  3364. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3365. }
  3366. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3367. {
  3368. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3369. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3370. if (os_strncmp(cmd, "group=", 6) == 0)
  3371. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3372. return -1;
  3373. }
  3374. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3375. char *cmd, int freq, int ht40)
  3376. {
  3377. int id;
  3378. struct wpa_ssid *ssid;
  3379. id = atoi(cmd);
  3380. ssid = wpa_config_get_network(wpa_s->conf, id);
  3381. if (ssid == NULL || ssid->disabled != 2) {
  3382. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3383. "for persistent P2P group",
  3384. id);
  3385. return -1;
  3386. }
  3387. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40);
  3388. }
  3389. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3390. {
  3391. int freq = 0, ht40;
  3392. char *pos;
  3393. pos = os_strstr(cmd, "freq=");
  3394. if (pos)
  3395. freq = atoi(pos + 5);
  3396. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3397. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3398. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3399. ht40);
  3400. if (os_strcmp(cmd, "persistent") == 0 ||
  3401. os_strncmp(cmd, "persistent ", 11) == 0)
  3402. return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
  3403. if (os_strncmp(cmd, "freq=", 5) == 0)
  3404. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  3405. if (ht40)
  3406. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  3407. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3408. cmd);
  3409. return -1;
  3410. }
  3411. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3412. char *buf, size_t buflen)
  3413. {
  3414. u8 addr[ETH_ALEN], *addr_ptr;
  3415. int next, res;
  3416. const struct p2p_peer_info *info;
  3417. char *pos, *end;
  3418. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3419. struct wpa_ssid *ssid;
  3420. size_t i;
  3421. if (!wpa_s->global->p2p)
  3422. return -1;
  3423. if (os_strcmp(cmd, "FIRST") == 0) {
  3424. addr_ptr = NULL;
  3425. next = 0;
  3426. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3427. if (hwaddr_aton(cmd + 5, addr) < 0)
  3428. return -1;
  3429. addr_ptr = addr;
  3430. next = 1;
  3431. } else {
  3432. if (hwaddr_aton(cmd, addr) < 0)
  3433. return -1;
  3434. addr_ptr = addr;
  3435. next = 0;
  3436. }
  3437. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3438. if (info == NULL)
  3439. return -1;
  3440. pos = buf;
  3441. end = buf + buflen;
  3442. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3443. "pri_dev_type=%s\n"
  3444. "device_name=%s\n"
  3445. "manufacturer=%s\n"
  3446. "model_name=%s\n"
  3447. "model_number=%s\n"
  3448. "serial_number=%s\n"
  3449. "config_methods=0x%x\n"
  3450. "dev_capab=0x%x\n"
  3451. "group_capab=0x%x\n"
  3452. "level=%d\n",
  3453. MAC2STR(info->p2p_device_addr),
  3454. wps_dev_type_bin2str(info->pri_dev_type,
  3455. devtype, sizeof(devtype)),
  3456. info->device_name,
  3457. info->manufacturer,
  3458. info->model_name,
  3459. info->model_number,
  3460. info->serial_number,
  3461. info->config_methods,
  3462. info->dev_capab,
  3463. info->group_capab,
  3464. info->level);
  3465. if (res < 0 || res >= end - pos)
  3466. return pos - buf;
  3467. pos += res;
  3468. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3469. {
  3470. const u8 *t;
  3471. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3472. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3473. wps_dev_type_bin2str(t, devtype,
  3474. sizeof(devtype)));
  3475. if (res < 0 || res >= end - pos)
  3476. return pos - buf;
  3477. pos += res;
  3478. }
  3479. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3480. if (ssid) {
  3481. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3482. if (res < 0 || res >= end - pos)
  3483. return pos - buf;
  3484. pos += res;
  3485. }
  3486. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3487. if (res < 0)
  3488. return pos - buf;
  3489. pos += res;
  3490. return pos - buf;
  3491. }
  3492. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3493. const char *param)
  3494. {
  3495. struct wpa_freq_range *freq = NULL, *n;
  3496. unsigned int count = 0, i;
  3497. const char *pos, *pos2, *pos3;
  3498. if (wpa_s->global->p2p == NULL)
  3499. return -1;
  3500. /*
  3501. * param includes comma separated frequency range.
  3502. * For example: 2412-2432,2462,5000-6000
  3503. */
  3504. pos = param;
  3505. while (pos && pos[0]) {
  3506. n = os_realloc_array(freq, count + 1,
  3507. sizeof(struct wpa_freq_range));
  3508. if (n == NULL) {
  3509. os_free(freq);
  3510. return -1;
  3511. }
  3512. freq = n;
  3513. freq[count].min = atoi(pos);
  3514. pos2 = os_strchr(pos, '-');
  3515. pos3 = os_strchr(pos, ',');
  3516. if (pos2 && (!pos3 || pos2 < pos3)) {
  3517. pos2++;
  3518. freq[count].max = atoi(pos2);
  3519. } else
  3520. freq[count].max = freq[count].min;
  3521. pos = pos3;
  3522. if (pos)
  3523. pos++;
  3524. count++;
  3525. }
  3526. for (i = 0; i < count; i++) {
  3527. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3528. freq[i].min, freq[i].max);
  3529. }
  3530. os_free(wpa_s->global->p2p_disallow_freq);
  3531. wpa_s->global->p2p_disallow_freq = freq;
  3532. wpa_s->global->num_p2p_disallow_freq = count;
  3533. wpas_p2p_update_channel_list(wpa_s);
  3534. return 0;
  3535. }
  3536. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3537. {
  3538. char *param;
  3539. if (wpa_s->global->p2p == NULL)
  3540. return -1;
  3541. param = os_strchr(cmd, ' ');
  3542. if (param == NULL)
  3543. return -1;
  3544. *param++ = '\0';
  3545. if (os_strcmp(cmd, "discoverability") == 0) {
  3546. p2p_set_client_discoverability(wpa_s->global->p2p,
  3547. atoi(param));
  3548. return 0;
  3549. }
  3550. if (os_strcmp(cmd, "managed") == 0) {
  3551. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3552. return 0;
  3553. }
  3554. if (os_strcmp(cmd, "listen_channel") == 0) {
  3555. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3556. atoi(param));
  3557. }
  3558. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3559. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3560. os_strlen(param));
  3561. }
  3562. if (os_strcmp(cmd, "noa") == 0) {
  3563. char *pos;
  3564. int count, start, duration;
  3565. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3566. count = atoi(param);
  3567. pos = os_strchr(param, ',');
  3568. if (pos == NULL)
  3569. return -1;
  3570. pos++;
  3571. start = atoi(pos);
  3572. pos = os_strchr(pos, ',');
  3573. if (pos == NULL)
  3574. return -1;
  3575. pos++;
  3576. duration = atoi(pos);
  3577. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3578. return -1;
  3579. if (count == 0 && duration > 0)
  3580. return -1;
  3581. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3582. "start=%d duration=%d", count, start, duration);
  3583. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3584. }
  3585. if (os_strcmp(cmd, "ps") == 0)
  3586. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3587. if (os_strcmp(cmd, "oppps") == 0)
  3588. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3589. if (os_strcmp(cmd, "ctwindow") == 0)
  3590. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3591. if (os_strcmp(cmd, "disabled") == 0) {
  3592. wpa_s->global->p2p_disabled = atoi(param);
  3593. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3594. wpa_s->global->p2p_disabled ?
  3595. "disabled" : "enabled");
  3596. if (wpa_s->global->p2p_disabled) {
  3597. wpas_p2p_stop_find(wpa_s);
  3598. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3599. p2p_flush(wpa_s->global->p2p);
  3600. }
  3601. return 0;
  3602. }
  3603. if (os_strcmp(cmd, "conc_pref") == 0) {
  3604. if (os_strcmp(param, "sta") == 0)
  3605. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3606. else if (os_strcmp(param, "p2p") == 0)
  3607. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3608. else {
  3609. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3610. return -1;
  3611. }
  3612. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3613. "%s", param);
  3614. return 0;
  3615. }
  3616. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3617. wpa_s->force_long_sd = atoi(param);
  3618. return 0;
  3619. }
  3620. if (os_strcmp(cmd, "peer_filter") == 0) {
  3621. u8 addr[ETH_ALEN];
  3622. if (hwaddr_aton(param, addr))
  3623. return -1;
  3624. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3625. return 0;
  3626. }
  3627. if (os_strcmp(cmd, "cross_connect") == 0)
  3628. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3629. if (os_strcmp(cmd, "go_apsd") == 0) {
  3630. if (os_strcmp(param, "disable") == 0)
  3631. wpa_s->set_ap_uapsd = 0;
  3632. else {
  3633. wpa_s->set_ap_uapsd = 1;
  3634. wpa_s->ap_uapsd = atoi(param);
  3635. }
  3636. return 0;
  3637. }
  3638. if (os_strcmp(cmd, "client_apsd") == 0) {
  3639. if (os_strcmp(param, "disable") == 0)
  3640. wpa_s->set_sta_uapsd = 0;
  3641. else {
  3642. int be, bk, vi, vo;
  3643. char *pos;
  3644. /* format: BE,BK,VI,VO;max SP Length */
  3645. be = atoi(param);
  3646. pos = os_strchr(param, ',');
  3647. if (pos == NULL)
  3648. return -1;
  3649. pos++;
  3650. bk = atoi(pos);
  3651. pos = os_strchr(pos, ',');
  3652. if (pos == NULL)
  3653. return -1;
  3654. pos++;
  3655. vi = atoi(pos);
  3656. pos = os_strchr(pos, ',');
  3657. if (pos == NULL)
  3658. return -1;
  3659. pos++;
  3660. vo = atoi(pos);
  3661. /* ignore max SP Length for now */
  3662. wpa_s->set_sta_uapsd = 1;
  3663. wpa_s->sta_uapsd = 0;
  3664. if (be)
  3665. wpa_s->sta_uapsd |= BIT(0);
  3666. if (bk)
  3667. wpa_s->sta_uapsd |= BIT(1);
  3668. if (vi)
  3669. wpa_s->sta_uapsd |= BIT(2);
  3670. if (vo)
  3671. wpa_s->sta_uapsd |= BIT(3);
  3672. }
  3673. return 0;
  3674. }
  3675. if (os_strcmp(cmd, "disallow_freq") == 0)
  3676. return p2p_ctrl_disallow_freq(wpa_s, param);
  3677. if (os_strcmp(cmd, "disc_int") == 0) {
  3678. int min_disc_int, max_disc_int, max_disc_tu;
  3679. char *pos;
  3680. pos = param;
  3681. min_disc_int = atoi(pos);
  3682. pos = os_strchr(pos, ' ');
  3683. if (pos == NULL)
  3684. return -1;
  3685. *pos++ = '\0';
  3686. max_disc_int = atoi(pos);
  3687. pos = os_strchr(pos, ' ');
  3688. if (pos == NULL)
  3689. return -1;
  3690. *pos++ = '\0';
  3691. max_disc_tu = atoi(pos);
  3692. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  3693. max_disc_int, max_disc_tu);
  3694. }
  3695. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  3696. cmd);
  3697. return -1;
  3698. }
  3699. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  3700. {
  3701. char *pos, *pos2;
  3702. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  3703. if (cmd[0]) {
  3704. pos = os_strchr(cmd, ' ');
  3705. if (pos == NULL)
  3706. return -1;
  3707. *pos++ = '\0';
  3708. dur1 = atoi(cmd);
  3709. pos2 = os_strchr(pos, ' ');
  3710. if (pos2)
  3711. *pos2++ = '\0';
  3712. int1 = atoi(pos);
  3713. } else
  3714. pos2 = NULL;
  3715. if (pos2) {
  3716. pos = os_strchr(pos2, ' ');
  3717. if (pos == NULL)
  3718. return -1;
  3719. *pos++ = '\0';
  3720. dur2 = atoi(pos2);
  3721. int2 = atoi(pos);
  3722. }
  3723. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  3724. }
  3725. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3726. {
  3727. char *pos;
  3728. unsigned int period = 0, interval = 0;
  3729. if (cmd[0]) {
  3730. pos = os_strchr(cmd, ' ');
  3731. if (pos == NULL)
  3732. return -1;
  3733. *pos++ = '\0';
  3734. period = atoi(cmd);
  3735. interval = atoi(pos);
  3736. }
  3737. return wpas_p2p_ext_listen(wpa_s, period, interval);
  3738. }
  3739. #endif /* CONFIG_P2P */
  3740. #ifdef CONFIG_INTERWORKING
  3741. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  3742. {
  3743. u8 bssid[ETH_ALEN];
  3744. struct wpa_bss *bss;
  3745. if (hwaddr_aton(dst, bssid)) {
  3746. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  3747. return -1;
  3748. }
  3749. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3750. if (bss == NULL) {
  3751. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  3752. MAC2STR(bssid));
  3753. return -1;
  3754. }
  3755. return interworking_connect(wpa_s, bss);
  3756. }
  3757. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3758. {
  3759. u8 dst_addr[ETH_ALEN];
  3760. int used;
  3761. char *pos;
  3762. #define MAX_ANQP_INFO_ID 100
  3763. u16 id[MAX_ANQP_INFO_ID];
  3764. size_t num_id = 0;
  3765. used = hwaddr_aton2(dst, dst_addr);
  3766. if (used < 0)
  3767. return -1;
  3768. pos = dst + used;
  3769. while (num_id < MAX_ANQP_INFO_ID) {
  3770. id[num_id] = atoi(pos);
  3771. if (id[num_id])
  3772. num_id++;
  3773. pos = os_strchr(pos + 1, ',');
  3774. if (pos == NULL)
  3775. break;
  3776. pos++;
  3777. }
  3778. if (num_id == 0)
  3779. return -1;
  3780. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  3781. }
  3782. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  3783. {
  3784. u8 dst_addr[ETH_ALEN];
  3785. struct wpabuf *advproto, *query = NULL;
  3786. int used, ret = -1;
  3787. char *pos, *end;
  3788. size_t len;
  3789. used = hwaddr_aton2(cmd, dst_addr);
  3790. if (used < 0)
  3791. return -1;
  3792. pos = cmd + used;
  3793. while (*pos == ' ')
  3794. pos++;
  3795. /* Advertisement Protocol ID */
  3796. end = os_strchr(pos, ' ');
  3797. if (end)
  3798. len = end - pos;
  3799. else
  3800. len = os_strlen(pos);
  3801. if (len & 0x01)
  3802. return -1;
  3803. len /= 2;
  3804. if (len == 0)
  3805. return -1;
  3806. advproto = wpabuf_alloc(len);
  3807. if (advproto == NULL)
  3808. return -1;
  3809. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  3810. goto fail;
  3811. if (end) {
  3812. /* Optional Query Request */
  3813. pos = end + 1;
  3814. while (*pos == ' ')
  3815. pos++;
  3816. len = os_strlen(pos);
  3817. if (len) {
  3818. if (len & 0x01)
  3819. goto fail;
  3820. len /= 2;
  3821. if (len == 0)
  3822. goto fail;
  3823. query = wpabuf_alloc(len);
  3824. if (query == NULL)
  3825. goto fail;
  3826. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  3827. goto fail;
  3828. }
  3829. }
  3830. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  3831. fail:
  3832. wpabuf_free(advproto);
  3833. wpabuf_free(query);
  3834. return ret;
  3835. }
  3836. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  3837. size_t buflen)
  3838. {
  3839. u8 addr[ETH_ALEN];
  3840. int dialog_token;
  3841. int used;
  3842. char *pos;
  3843. size_t resp_len, start, requested_len;
  3844. if (!wpa_s->last_gas_resp)
  3845. return -1;
  3846. used = hwaddr_aton2(cmd, addr);
  3847. if (used < 0)
  3848. return -1;
  3849. pos = cmd + used;
  3850. while (*pos == ' ')
  3851. pos++;
  3852. dialog_token = atoi(pos);
  3853. if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
  3854. dialog_token != wpa_s->last_gas_dialog_token)
  3855. return -1;
  3856. resp_len = wpabuf_len(wpa_s->last_gas_resp);
  3857. start = 0;
  3858. requested_len = resp_len;
  3859. pos = os_strchr(pos, ' ');
  3860. if (pos) {
  3861. start = atoi(pos);
  3862. if (start > resp_len)
  3863. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  3864. pos = os_strchr(pos, ',');
  3865. if (pos == NULL)
  3866. return -1;
  3867. pos++;
  3868. requested_len = atoi(pos);
  3869. if (start + requested_len > resp_len)
  3870. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  3871. }
  3872. if (requested_len * 2 + 1 > buflen)
  3873. return os_snprintf(buf, buflen, "FAIL-Too long response");
  3874. return wpa_snprintf_hex(buf, buflen,
  3875. wpabuf_head_u8(wpa_s->last_gas_resp) + start,
  3876. requested_len);
  3877. }
  3878. #endif /* CONFIG_INTERWORKING */
  3879. #ifdef CONFIG_HS20
  3880. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3881. {
  3882. u8 dst_addr[ETH_ALEN];
  3883. int used;
  3884. char *pos;
  3885. u32 subtypes = 0;
  3886. used = hwaddr_aton2(dst, dst_addr);
  3887. if (used < 0)
  3888. return -1;
  3889. pos = dst + used;
  3890. for (;;) {
  3891. int num = atoi(pos);
  3892. if (num <= 0 || num > 31)
  3893. return -1;
  3894. subtypes |= BIT(num);
  3895. pos = os_strchr(pos + 1, ',');
  3896. if (pos == NULL)
  3897. break;
  3898. pos++;
  3899. }
  3900. if (subtypes == 0)
  3901. return -1;
  3902. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  3903. }
  3904. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  3905. const u8 *addr, const char *realm)
  3906. {
  3907. u8 *buf;
  3908. size_t rlen, len;
  3909. int ret;
  3910. rlen = os_strlen(realm);
  3911. len = 3 + rlen;
  3912. buf = os_malloc(len);
  3913. if (buf == NULL)
  3914. return -1;
  3915. buf[0] = 1; /* NAI Home Realm Count */
  3916. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  3917. buf[2] = rlen;
  3918. os_memcpy(buf + 3, realm, rlen);
  3919. ret = hs20_anqp_send_req(wpa_s, addr,
  3920. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  3921. buf, len);
  3922. os_free(buf);
  3923. return ret;
  3924. }
  3925. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  3926. char *dst)
  3927. {
  3928. struct wpa_cred *cred = wpa_s->conf->cred;
  3929. u8 dst_addr[ETH_ALEN];
  3930. int used;
  3931. u8 *buf;
  3932. size_t len;
  3933. int ret;
  3934. used = hwaddr_aton2(dst, dst_addr);
  3935. if (used < 0)
  3936. return -1;
  3937. while (dst[used] == ' ')
  3938. used++;
  3939. if (os_strncmp(dst + used, "realm=", 6) == 0)
  3940. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  3941. dst + used + 6);
  3942. len = os_strlen(dst + used);
  3943. if (len == 0 && cred && cred->realm)
  3944. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  3945. if (len % 1)
  3946. return -1;
  3947. len /= 2;
  3948. buf = os_malloc(len);
  3949. if (buf == NULL)
  3950. return -1;
  3951. if (hexstr2bin(dst + used, buf, len) < 0) {
  3952. os_free(buf);
  3953. return -1;
  3954. }
  3955. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  3956. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  3957. buf, len);
  3958. os_free(buf);
  3959. return ret;
  3960. }
  3961. #endif /* CONFIG_HS20 */
  3962. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  3963. struct wpa_supplicant *wpa_s, char *cmd)
  3964. {
  3965. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  3966. return 0;
  3967. }
  3968. #ifdef CONFIG_AUTOSCAN
  3969. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  3970. char *cmd)
  3971. {
  3972. enum wpa_states state = wpa_s->wpa_state;
  3973. char *new_params = NULL;
  3974. if (os_strlen(cmd) > 0) {
  3975. new_params = os_strdup(cmd);
  3976. if (new_params == NULL)
  3977. return -1;
  3978. }
  3979. os_free(wpa_s->conf->autoscan);
  3980. wpa_s->conf->autoscan = new_params;
  3981. if (wpa_s->conf->autoscan == NULL)
  3982. autoscan_deinit(wpa_s);
  3983. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  3984. autoscan_init(wpa_s, 1);
  3985. else if (state == WPA_SCANNING)
  3986. wpa_supplicant_reinit_autoscan(wpa_s);
  3987. return 0;
  3988. }
  3989. #endif /* CONFIG_AUTOSCAN */
  3990. #ifdef CONFIG_WNM
  3991. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  3992. {
  3993. int enter;
  3994. int intval = 0;
  3995. char *pos;
  3996. int ret;
  3997. struct wpabuf *tfs_req = NULL;
  3998. if (os_strncmp(cmd, "enter", 5) == 0)
  3999. enter = 1;
  4000. else if (os_strncmp(cmd, "exit", 4) == 0)
  4001. enter = 0;
  4002. else
  4003. return -1;
  4004. pos = os_strstr(cmd, " interval=");
  4005. if (pos)
  4006. intval = atoi(pos + 10);
  4007. pos = os_strstr(cmd, " tfs_req=");
  4008. if (pos) {
  4009. char *end;
  4010. size_t len;
  4011. pos += 9;
  4012. end = os_strchr(pos, ' ');
  4013. if (end)
  4014. len = end - pos;
  4015. else
  4016. len = os_strlen(pos);
  4017. if (len & 1)
  4018. return -1;
  4019. len /= 2;
  4020. tfs_req = wpabuf_alloc(len);
  4021. if (tfs_req == NULL)
  4022. return -1;
  4023. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4024. wpabuf_free(tfs_req);
  4025. return -1;
  4026. }
  4027. }
  4028. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4029. WNM_SLEEP_MODE_EXIT, intval,
  4030. tfs_req);
  4031. wpabuf_free(tfs_req);
  4032. return ret;
  4033. }
  4034. #endif /* CONFIG_WNM */
  4035. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4036. size_t buflen)
  4037. {
  4038. struct wpa_signal_info si;
  4039. int ret;
  4040. ret = wpa_drv_signal_poll(wpa_s, &si);
  4041. if (ret)
  4042. return -1;
  4043. ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
  4044. "NOISE=%d\nFREQUENCY=%u\n",
  4045. si.current_signal, si.current_txrate / 1000,
  4046. si.current_noise, si.frequency);
  4047. if (ret < 0 || (unsigned int) ret > buflen)
  4048. return -1;
  4049. return ret;
  4050. }
  4051. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4052. size_t buflen)
  4053. {
  4054. struct hostap_sta_driver_data sta;
  4055. int ret;
  4056. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4057. if (ret)
  4058. return -1;
  4059. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4060. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4061. if (ret < 0 || (size_t) ret > buflen)
  4062. return -1;
  4063. return ret;
  4064. }
  4065. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  4066. char *buf, size_t *resp_len)
  4067. {
  4068. char *reply;
  4069. const int reply_size = 4096;
  4070. int ctrl_rsp = 0;
  4071. int reply_len;
  4072. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  4073. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  4074. os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  4075. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
  4076. os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
  4077. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  4078. (const u8 *) buf, os_strlen(buf));
  4079. } else {
  4080. int level = MSG_DEBUG;
  4081. if (os_strcmp(buf, "PING") == 0)
  4082. level = MSG_EXCESSIVE;
  4083. wpa_hexdump_ascii(level, "RX ctrl_iface",
  4084. (const u8 *) buf, os_strlen(buf));
  4085. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  4086. }
  4087. reply = os_malloc(reply_size);
  4088. if (reply == NULL) {
  4089. *resp_len = 1;
  4090. return NULL;
  4091. }
  4092. os_memcpy(reply, "OK\n", 3);
  4093. reply_len = 3;
  4094. if (os_strcmp(buf, "PING") == 0) {
  4095. os_memcpy(reply, "PONG\n", 5);
  4096. reply_len = 5;
  4097. } else if (os_strcmp(buf, "IFNAME") == 0) {
  4098. reply_len = os_strlen(wpa_s->ifname);
  4099. os_memcpy(reply, wpa_s->ifname, reply_len);
  4100. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  4101. if (wpa_debug_reopen_file() < 0)
  4102. reply_len = -1;
  4103. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  4104. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  4105. } else if (os_strcmp(buf, "MIB") == 0) {
  4106. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  4107. if (reply_len >= 0) {
  4108. int res;
  4109. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  4110. reply_size - reply_len);
  4111. if (res < 0)
  4112. reply_len = -1;
  4113. else
  4114. reply_len += res;
  4115. }
  4116. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  4117. reply_len = wpa_supplicant_ctrl_iface_status(
  4118. wpa_s, buf + 6, reply, reply_size);
  4119. } else if (os_strcmp(buf, "PMKSA") == 0) {
  4120. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  4121. reply_size);
  4122. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  4123. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  4124. reply_len = -1;
  4125. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  4126. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  4127. reply, reply_size);
  4128. } else if (os_strcmp(buf, "LOGON") == 0) {
  4129. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4130. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  4131. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  4132. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  4133. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4134. reply_len = -1;
  4135. else
  4136. wpas_request_connection(wpa_s);
  4137. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  4138. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4139. reply_len = -1;
  4140. else if (wpa_s->disconnected)
  4141. wpas_request_connection(wpa_s);
  4142. #ifdef IEEE8021X_EAPOL
  4143. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  4144. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  4145. reply_len = -1;
  4146. #endif /* IEEE8021X_EAPOL */
  4147. #ifdef CONFIG_PEERKEY
  4148. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  4149. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  4150. reply_len = -1;
  4151. #endif /* CONFIG_PEERKEY */
  4152. #ifdef CONFIG_IEEE80211R
  4153. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  4154. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  4155. reply_len = -1;
  4156. #endif /* CONFIG_IEEE80211R */
  4157. #ifdef CONFIG_WPS
  4158. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  4159. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  4160. if (res == -2) {
  4161. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4162. reply_len = 17;
  4163. } else if (res)
  4164. reply_len = -1;
  4165. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  4166. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  4167. if (res == -2) {
  4168. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4169. reply_len = 17;
  4170. } else if (res)
  4171. reply_len = -1;
  4172. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  4173. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  4174. reply,
  4175. reply_size);
  4176. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  4177. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  4178. wpa_s, buf + 14, reply, reply_size);
  4179. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  4180. if (wpas_wps_cancel(wpa_s))
  4181. reply_len = -1;
  4182. #ifdef CONFIG_WPS_NFC
  4183. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  4184. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  4185. reply_len = -1;
  4186. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  4187. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  4188. reply_len = -1;
  4189. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  4190. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  4191. wpa_s, buf + 14, reply, reply_size);
  4192. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  4193. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  4194. buf + 17))
  4195. reply_len = -1;
  4196. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  4197. reply_len = wpas_ctrl_nfc_get_handover_req(
  4198. wpa_s, buf + 21, reply, reply_size);
  4199. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  4200. reply_len = wpas_ctrl_nfc_get_handover_sel(
  4201. wpa_s, buf + 21, reply, reply_size);
  4202. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
  4203. reply_len = wpas_ctrl_nfc_rx_handover_req(
  4204. wpa_s, buf + 20, reply, reply_size);
  4205. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
  4206. if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
  4207. reply_len = -1;
  4208. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  4209. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  4210. reply_len = -1;
  4211. #endif /* CONFIG_WPS_NFC */
  4212. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  4213. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  4214. reply_len = -1;
  4215. #ifdef CONFIG_AP
  4216. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  4217. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  4218. wpa_s, buf + 11, reply, reply_size);
  4219. #endif /* CONFIG_AP */
  4220. #ifdef CONFIG_WPS_ER
  4221. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  4222. if (wpas_wps_er_start(wpa_s, NULL))
  4223. reply_len = -1;
  4224. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  4225. if (wpas_wps_er_start(wpa_s, buf + 13))
  4226. reply_len = -1;
  4227. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  4228. if (wpas_wps_er_stop(wpa_s))
  4229. reply_len = -1;
  4230. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  4231. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  4232. reply_len = -1;
  4233. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  4234. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  4235. if (ret == -2) {
  4236. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4237. reply_len = 17;
  4238. } else if (ret == -3) {
  4239. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  4240. reply_len = 18;
  4241. } else if (ret == -4) {
  4242. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  4243. reply_len = 20;
  4244. } else if (ret)
  4245. reply_len = -1;
  4246. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  4247. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  4248. reply_len = -1;
  4249. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  4250. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  4251. buf + 18))
  4252. reply_len = -1;
  4253. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  4254. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  4255. reply_len = -1;
  4256. #ifdef CONFIG_WPS_NFC
  4257. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  4258. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  4259. wpa_s, buf + 24, reply, reply_size);
  4260. #endif /* CONFIG_WPS_NFC */
  4261. #endif /* CONFIG_WPS_ER */
  4262. #endif /* CONFIG_WPS */
  4263. #ifdef CONFIG_IBSS_RSN
  4264. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  4265. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  4266. reply_len = -1;
  4267. #endif /* CONFIG_IBSS_RSN */
  4268. #ifdef CONFIG_P2P
  4269. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  4270. if (p2p_ctrl_find(wpa_s, buf + 9))
  4271. reply_len = -1;
  4272. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  4273. if (p2p_ctrl_find(wpa_s, ""))
  4274. reply_len = -1;
  4275. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  4276. wpas_p2p_stop_find(wpa_s);
  4277. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  4278. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  4279. reply_size);
  4280. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  4281. if (p2p_ctrl_listen(wpa_s, buf + 11))
  4282. reply_len = -1;
  4283. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  4284. if (p2p_ctrl_listen(wpa_s, ""))
  4285. reply_len = -1;
  4286. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  4287. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  4288. reply_len = -1;
  4289. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  4290. if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
  4291. reply_len = -1;
  4292. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  4293. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  4294. reply_len = -1;
  4295. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  4296. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  4297. reply_len = -1;
  4298. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  4299. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  4300. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  4301. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  4302. reply_size);
  4303. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  4304. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  4305. reply_len = -1;
  4306. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  4307. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  4308. reply_len = -1;
  4309. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  4310. wpas_p2p_sd_service_update(wpa_s);
  4311. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  4312. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  4313. reply_len = -1;
  4314. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  4315. wpas_p2p_service_flush(wpa_s);
  4316. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  4317. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  4318. reply_len = -1;
  4319. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  4320. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  4321. reply_len = -1;
  4322. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  4323. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  4324. reply_len = -1;
  4325. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  4326. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  4327. reply_len = -1;
  4328. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  4329. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  4330. reply_size);
  4331. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  4332. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  4333. reply_len = -1;
  4334. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  4335. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4336. wpa_s->force_long_sd = 0;
  4337. if (wpa_s->global->p2p)
  4338. p2p_flush(wpa_s->global->p2p);
  4339. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  4340. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  4341. reply_len = -1;
  4342. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  4343. if (wpas_p2p_cancel(wpa_s))
  4344. reply_len = -1;
  4345. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  4346. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  4347. reply_len = -1;
  4348. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  4349. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  4350. reply_len = -1;
  4351. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  4352. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  4353. reply_len = -1;
  4354. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  4355. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  4356. reply_len = -1;
  4357. #endif /* CONFIG_P2P */
  4358. #ifdef CONFIG_WIFI_DISPLAY
  4359. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  4360. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  4361. reply_len = -1;
  4362. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  4363. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  4364. reply, reply_size);
  4365. #endif /* CONFIG_WIFI_DISPLAY */
  4366. #ifdef CONFIG_INTERWORKING
  4367. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  4368. if (interworking_fetch_anqp(wpa_s) < 0)
  4369. reply_len = -1;
  4370. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  4371. interworking_stop_fetch_anqp(wpa_s);
  4372. } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
  4373. if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
  4374. NULL) < 0)
  4375. reply_len = -1;
  4376. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  4377. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  4378. reply_len = -1;
  4379. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  4380. if (get_anqp(wpa_s, buf + 9) < 0)
  4381. reply_len = -1;
  4382. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  4383. if (gas_request(wpa_s, buf + 12) < 0)
  4384. reply_len = -1;
  4385. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  4386. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  4387. reply_size);
  4388. #endif /* CONFIG_INTERWORKING */
  4389. #ifdef CONFIG_HS20
  4390. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  4391. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  4392. reply_len = -1;
  4393. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  4394. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  4395. reply_len = -1;
  4396. #endif /* CONFIG_HS20 */
  4397. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  4398. {
  4399. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  4400. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  4401. reply_len = -1;
  4402. else
  4403. ctrl_rsp = 1;
  4404. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  4405. if (wpa_supplicant_reload_configuration(wpa_s))
  4406. reply_len = -1;
  4407. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4408. wpa_supplicant_terminate_proc(wpa_s->global);
  4409. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  4410. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  4411. reply_len = -1;
  4412. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  4413. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  4414. wpa_s, buf + 9, reply, reply_size);
  4415. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  4416. reply_len = wpa_supplicant_ctrl_iface_log_level(
  4417. wpa_s, buf + 9, reply, reply_size);
  4418. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  4419. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  4420. wpa_s, reply, reply_size);
  4421. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  4422. #ifdef CONFIG_SME
  4423. wpa_s->sme.prev_bssid_set = 0;
  4424. #endif /* CONFIG_SME */
  4425. wpa_s->reassociate = 0;
  4426. wpa_s->disconnected = 1;
  4427. wpa_supplicant_cancel_sched_scan(wpa_s);
  4428. wpa_supplicant_cancel_scan(wpa_s);
  4429. wpa_supplicant_deauthenticate(wpa_s,
  4430. WLAN_REASON_DEAUTH_LEAVING);
  4431. } else if (os_strcmp(buf, "SCAN") == 0 ||
  4432. os_strncmp(buf, "SCAN ", 5) == 0) {
  4433. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4434. reply_len = -1;
  4435. else {
  4436. if (os_strlen(buf) > 4 &&
  4437. os_strncasecmp(buf + 5, "TYPE=ONLY", 9) == 0)
  4438. wpa_s->scan_res_handler = scan_only_handler;
  4439. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  4440. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  4441. (wpa_s->wpa_state == WPA_COMPLETED))) {
  4442. wpa_s->normal_scans = 0;
  4443. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4444. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4445. } else if (wpa_s->sched_scanning) {
  4446. wpa_printf(MSG_DEBUG, "Stop ongoing "
  4447. "sched_scan to allow requested "
  4448. "full scan to proceed");
  4449. wpa_supplicant_cancel_sched_scan(wpa_s);
  4450. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4451. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4452. } else {
  4453. wpa_printf(MSG_DEBUG, "Ongoing scan action - "
  4454. "reject new request");
  4455. reply_len = os_snprintf(reply, reply_size,
  4456. "FAIL-BUSY\n");
  4457. }
  4458. }
  4459. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  4460. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  4461. wpa_s, reply, reply_size);
  4462. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  4463. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  4464. reply_len = -1;
  4465. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  4466. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  4467. reply_len = -1;
  4468. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  4469. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  4470. reply_len = -1;
  4471. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  4472. reply_len = wpa_supplicant_ctrl_iface_add_network(
  4473. wpa_s, reply, reply_size);
  4474. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  4475. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  4476. reply_len = -1;
  4477. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  4478. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  4479. reply_len = -1;
  4480. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  4481. reply_len = wpa_supplicant_ctrl_iface_get_network(
  4482. wpa_s, buf + 12, reply, reply_size);
  4483. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  4484. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  4485. wpa_s, reply, reply_size);
  4486. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  4487. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  4488. wpa_s, reply, reply_size);
  4489. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  4490. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  4491. reply_len = -1;
  4492. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  4493. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  4494. reply_len = -1;
  4495. #ifndef CONFIG_NO_CONFIG_WRITE
  4496. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  4497. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  4498. reply_len = -1;
  4499. #endif /* CONFIG_NO_CONFIG_WRITE */
  4500. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  4501. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  4502. wpa_s, buf + 15, reply, reply_size);
  4503. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  4504. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  4505. reply_len = -1;
  4506. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  4507. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  4508. reply_len = -1;
  4509. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4510. reply_len = wpa_supplicant_global_iface_list(
  4511. wpa_s->global, reply, reply_size);
  4512. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4513. reply_len = wpa_supplicant_global_iface_interfaces(
  4514. wpa_s->global, reply, reply_size);
  4515. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  4516. reply_len = wpa_supplicant_ctrl_iface_bss(
  4517. wpa_s, buf + 4, reply, reply_size);
  4518. #ifdef CONFIG_AP
  4519. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  4520. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  4521. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  4522. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  4523. reply_size);
  4524. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  4525. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  4526. reply_size);
  4527. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  4528. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  4529. reply_len = -1;
  4530. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  4531. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  4532. reply_len = -1;
  4533. #endif /* CONFIG_AP */
  4534. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4535. wpas_notify_suspend(wpa_s->global);
  4536. } else if (os_strcmp(buf, "RESUME") == 0) {
  4537. wpas_notify_resume(wpa_s->global);
  4538. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  4539. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  4540. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  4541. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  4542. reply_len = -1;
  4543. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  4544. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  4545. reply_len = -1;
  4546. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  4547. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  4548. reply_len = -1;
  4549. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  4550. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  4551. buf + 17))
  4552. reply_len = -1;
  4553. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  4554. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  4555. reply_len = -1;
  4556. #ifdef CONFIG_TDLS
  4557. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  4558. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  4559. reply_len = -1;
  4560. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  4561. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  4562. reply_len = -1;
  4563. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  4564. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  4565. reply_len = -1;
  4566. #endif /* CONFIG_TDLS */
  4567. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  4568. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  4569. reply_size);
  4570. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  4571. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  4572. reply_size);
  4573. #ifdef CONFIG_AUTOSCAN
  4574. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  4575. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  4576. reply_len = -1;
  4577. #endif /* CONFIG_AUTOSCAN */
  4578. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  4579. pmksa_cache_clear_current(wpa_s->wpa);
  4580. eapol_sm_request_reauth(wpa_s->eapol);
  4581. #ifdef CONFIG_WNM
  4582. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  4583. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  4584. reply_len = -1;
  4585. #endif /* CONFIG_WNM */
  4586. } else {
  4587. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4588. reply_len = 16;
  4589. }
  4590. if (reply_len < 0) {
  4591. os_memcpy(reply, "FAIL\n", 5);
  4592. reply_len = 5;
  4593. }
  4594. if (ctrl_rsp)
  4595. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  4596. *resp_len = reply_len;
  4597. return reply;
  4598. }
  4599. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  4600. char *cmd)
  4601. {
  4602. struct wpa_interface iface;
  4603. char *pos;
  4604. /*
  4605. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  4606. * TAB<bridge_ifname>
  4607. */
  4608. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  4609. os_memset(&iface, 0, sizeof(iface));
  4610. do {
  4611. iface.ifname = pos = cmd;
  4612. pos = os_strchr(pos, '\t');
  4613. if (pos)
  4614. *pos++ = '\0';
  4615. if (iface.ifname[0] == '\0')
  4616. return -1;
  4617. if (pos == NULL)
  4618. break;
  4619. iface.confname = pos;
  4620. pos = os_strchr(pos, '\t');
  4621. if (pos)
  4622. *pos++ = '\0';
  4623. if (iface.confname[0] == '\0')
  4624. iface.confname = NULL;
  4625. if (pos == NULL)
  4626. break;
  4627. iface.driver = pos;
  4628. pos = os_strchr(pos, '\t');
  4629. if (pos)
  4630. *pos++ = '\0';
  4631. if (iface.driver[0] == '\0')
  4632. iface.driver = NULL;
  4633. if (pos == NULL)
  4634. break;
  4635. iface.ctrl_interface = pos;
  4636. pos = os_strchr(pos, '\t');
  4637. if (pos)
  4638. *pos++ = '\0';
  4639. if (iface.ctrl_interface[0] == '\0')
  4640. iface.ctrl_interface = NULL;
  4641. if (pos == NULL)
  4642. break;
  4643. iface.driver_param = pos;
  4644. pos = os_strchr(pos, '\t');
  4645. if (pos)
  4646. *pos++ = '\0';
  4647. if (iface.driver_param[0] == '\0')
  4648. iface.driver_param = NULL;
  4649. if (pos == NULL)
  4650. break;
  4651. iface.bridge_ifname = pos;
  4652. pos = os_strchr(pos, '\t');
  4653. if (pos)
  4654. *pos++ = '\0';
  4655. if (iface.bridge_ifname[0] == '\0')
  4656. iface.bridge_ifname = NULL;
  4657. if (pos == NULL)
  4658. break;
  4659. } while (0);
  4660. if (wpa_supplicant_get_iface(global, iface.ifname))
  4661. return -1;
  4662. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  4663. }
  4664. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  4665. char *cmd)
  4666. {
  4667. struct wpa_supplicant *wpa_s;
  4668. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  4669. wpa_s = wpa_supplicant_get_iface(global, cmd);
  4670. if (wpa_s == NULL)
  4671. return -1;
  4672. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  4673. }
  4674. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  4675. {
  4676. struct wpa_interface_info *prev;
  4677. while (iface) {
  4678. prev = iface;
  4679. iface = iface->next;
  4680. os_free(prev->ifname);
  4681. os_free(prev->desc);
  4682. os_free(prev);
  4683. }
  4684. }
  4685. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  4686. char *buf, int len)
  4687. {
  4688. int i, res;
  4689. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  4690. char *pos, *end;
  4691. for (i = 0; wpa_drivers[i]; i++) {
  4692. struct wpa_driver_ops *drv = wpa_drivers[i];
  4693. if (drv->get_interfaces == NULL)
  4694. continue;
  4695. tmp = drv->get_interfaces(global->drv_priv[i]);
  4696. if (tmp == NULL)
  4697. continue;
  4698. if (last == NULL)
  4699. iface = last = tmp;
  4700. else
  4701. last->next = tmp;
  4702. while (last->next)
  4703. last = last->next;
  4704. }
  4705. pos = buf;
  4706. end = buf + len;
  4707. for (tmp = iface; tmp; tmp = tmp->next) {
  4708. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  4709. tmp->drv_name, tmp->ifname,
  4710. tmp->desc ? tmp->desc : "");
  4711. if (res < 0 || res >= end - pos) {
  4712. *pos = '\0';
  4713. break;
  4714. }
  4715. pos += res;
  4716. }
  4717. wpa_free_iface_info(iface);
  4718. return pos - buf;
  4719. }
  4720. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  4721. char *buf, int len)
  4722. {
  4723. int res;
  4724. char *pos, *end;
  4725. struct wpa_supplicant *wpa_s;
  4726. wpa_s = global->ifaces;
  4727. pos = buf;
  4728. end = buf + len;
  4729. while (wpa_s) {
  4730. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  4731. if (res < 0 || res >= end - pos) {
  4732. *pos = '\0';
  4733. break;
  4734. }
  4735. pos += res;
  4736. wpa_s = wpa_s->next;
  4737. }
  4738. return pos - buf;
  4739. }
  4740. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  4741. char *buf, size_t *resp_len)
  4742. {
  4743. char *reply;
  4744. const int reply_size = 2048;
  4745. int reply_len;
  4746. int level = MSG_DEBUG;
  4747. if (os_strcmp(buf, "PING") == 0)
  4748. level = MSG_EXCESSIVE;
  4749. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  4750. (const u8 *) buf, os_strlen(buf));
  4751. reply = os_malloc(reply_size);
  4752. if (reply == NULL) {
  4753. *resp_len = 1;
  4754. return NULL;
  4755. }
  4756. os_memcpy(reply, "OK\n", 3);
  4757. reply_len = 3;
  4758. if (os_strcmp(buf, "PING") == 0) {
  4759. os_memcpy(reply, "PONG\n", 5);
  4760. reply_len = 5;
  4761. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  4762. if (wpa_supplicant_global_iface_add(global, buf + 14))
  4763. reply_len = -1;
  4764. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  4765. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  4766. reply_len = -1;
  4767. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4768. reply_len = wpa_supplicant_global_iface_list(
  4769. global, reply, reply_size);
  4770. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4771. reply_len = wpa_supplicant_global_iface_interfaces(
  4772. global, reply, reply_size);
  4773. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4774. wpa_supplicant_terminate_proc(global);
  4775. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4776. wpas_notify_suspend(global);
  4777. } else if (os_strcmp(buf, "RESUME") == 0) {
  4778. wpas_notify_resume(global);
  4779. } else {
  4780. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4781. reply_len = 16;
  4782. }
  4783. if (reply_len < 0) {
  4784. os_memcpy(reply, "FAIL\n", 5);
  4785. reply_len = 5;
  4786. }
  4787. *resp_len = reply_len;
  4788. return reply;
  4789. }