ctrl_iface.c 123 KB

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