ctrl_iface.c 111 KB

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