wpa_cli.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673
  1. /*
  2. * WPA Supplicant - command line interface for wpa_supplicant daemon
  3. * Copyright (c) 2004-2017, 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 "includes.h"
  9. #ifdef CONFIG_CTRL_IFACE
  10. #ifdef CONFIG_CTRL_IFACE_UNIX
  11. #include <dirent.h>
  12. #endif /* CONFIG_CTRL_IFACE_UNIX */
  13. #include "common/cli.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "utils/edit.h"
  18. #include "utils/list.h"
  19. #include "common/version.h"
  20. #include "common/ieee802_11_defs.h"
  21. #ifdef ANDROID
  22. #include <cutils/properties.h>
  23. #endif /* ANDROID */
  24. static const char *const wpa_cli_version =
  25. "wpa_cli v" VERSION_STR "\n"
  26. "Copyright (c) 2004-2017, Jouni Malinen <j@w1.fi> and contributors";
  27. #define VENDOR_ELEM_FRAME_ID \
  28. " 0: Probe Req (P2P), 1: Probe Resp (P2P) , 2: Probe Resp (GO), " \
  29. "3: Beacon (GO), 4: PD Req, 5: PD Resp, 6: GO Neg Req, " \
  30. "7: GO Neg Resp, 8: GO Neg Conf, 9: Inv Req, 10: Inv Resp, " \
  31. "11: Assoc Req (P2P), 12: Assoc Resp (P2P)"
  32. static struct wpa_ctrl *ctrl_conn;
  33. static struct wpa_ctrl *mon_conn;
  34. static int wpa_cli_quit = 0;
  35. static int wpa_cli_attached = 0;
  36. static int wpa_cli_connected = -1;
  37. static int wpa_cli_last_id = 0;
  38. #ifndef CONFIG_CTRL_IFACE_DIR
  39. #define CONFIG_CTRL_IFACE_DIR "/var/run/wpa_supplicant"
  40. #endif /* CONFIG_CTRL_IFACE_DIR */
  41. static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
  42. static const char *client_socket_dir = NULL;
  43. static char *ctrl_ifname = NULL;
  44. static const char *pid_file = NULL;
  45. static const char *action_file = NULL;
  46. static int ping_interval = 5;
  47. static int interactive = 0;
  48. static char *ifname_prefix = NULL;
  49. static DEFINE_DL_LIST(bsses); /* struct cli_txt_entry */
  50. static DEFINE_DL_LIST(p2p_peers); /* struct cli_txt_entry */
  51. static DEFINE_DL_LIST(p2p_groups); /* struct cli_txt_entry */
  52. static DEFINE_DL_LIST(ifnames); /* struct cli_txt_entry */
  53. static DEFINE_DL_LIST(networks); /* struct cli_txt_entry */
  54. static DEFINE_DL_LIST(creds); /* struct cli_txt_entry */
  55. #ifdef CONFIG_AP
  56. static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */
  57. #endif /* CONFIG_AP */
  58. static void print_help(const char *cmd);
  59. static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx);
  60. static void wpa_cli_close_connection(void);
  61. static char * wpa_cli_get_default_ifname(void);
  62. static char ** wpa_list_cmd_list(void);
  63. static void update_creds(struct wpa_ctrl *ctrl);
  64. static void update_networks(struct wpa_ctrl *ctrl);
  65. static void update_stations(struct wpa_ctrl *ctrl);
  66. static void usage(void)
  67. {
  68. printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvB] "
  69. "[-a<action file>] \\\n"
  70. " [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] "
  71. "\\\n"
  72. " [-s<wpa_client_socket_file_path>] "
  73. "[command..]\n"
  74. " -h = help (show this usage text)\n"
  75. " -v = shown version information\n"
  76. " -a = run in daemon mode executing the action file based on "
  77. "events from\n"
  78. " wpa_supplicant\n"
  79. " -B = run a daemon in the background\n"
  80. " default path: " CONFIG_CTRL_IFACE_DIR "\n"
  81. " default interface: first interface found in socket path\n");
  82. print_help(NULL);
  83. }
  84. static int wpa_cli_show_event(const char *event)
  85. {
  86. const char *start;
  87. start = os_strchr(event, '>');
  88. if (start == NULL)
  89. return 1;
  90. start++;
  91. /*
  92. * Skip BSS added/removed events since they can be relatively frequent
  93. * and are likely of not much use for an interactive user.
  94. */
  95. if (str_starts(start, WPA_EVENT_BSS_ADDED) ||
  96. str_starts(start, WPA_EVENT_BSS_REMOVED))
  97. return 0;
  98. return 1;
  99. }
  100. static int wpa_cli_open_connection(const char *ifname, int attach)
  101. {
  102. #if defined(CONFIG_CTRL_IFACE_UDP) || defined(CONFIG_CTRL_IFACE_NAMED_PIPE)
  103. ctrl_conn = wpa_ctrl_open(ifname);
  104. if (ctrl_conn == NULL)
  105. return -1;
  106. if (attach && interactive)
  107. mon_conn = wpa_ctrl_open(ifname);
  108. else
  109. mon_conn = NULL;
  110. #else /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
  111. char *cfile = NULL;
  112. int flen, res;
  113. if (ifname == NULL)
  114. return -1;
  115. #ifdef ANDROID
  116. if (access(ctrl_iface_dir, F_OK) < 0) {
  117. cfile = os_strdup(ifname);
  118. if (cfile == NULL)
  119. return -1;
  120. }
  121. #endif /* ANDROID */
  122. if (client_socket_dir && client_socket_dir[0] &&
  123. access(client_socket_dir, F_OK) < 0) {
  124. perror(client_socket_dir);
  125. os_free(cfile);
  126. return -1;
  127. }
  128. if (cfile == NULL) {
  129. flen = os_strlen(ctrl_iface_dir) + os_strlen(ifname) + 2;
  130. cfile = os_malloc(flen);
  131. if (cfile == NULL)
  132. return -1;
  133. res = os_snprintf(cfile, flen, "%s/%s", ctrl_iface_dir,
  134. ifname);
  135. if (os_snprintf_error(flen, res)) {
  136. os_free(cfile);
  137. return -1;
  138. }
  139. }
  140. ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir);
  141. if (ctrl_conn == NULL) {
  142. os_free(cfile);
  143. return -1;
  144. }
  145. if (attach && interactive)
  146. mon_conn = wpa_ctrl_open2(cfile, client_socket_dir);
  147. else
  148. mon_conn = NULL;
  149. os_free(cfile);
  150. #endif /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
  151. if (mon_conn) {
  152. if (wpa_ctrl_attach(mon_conn) == 0) {
  153. wpa_cli_attached = 1;
  154. if (interactive)
  155. eloop_register_read_sock(
  156. wpa_ctrl_get_fd(mon_conn),
  157. wpa_cli_mon_receive, NULL, NULL);
  158. } else {
  159. printf("Warning: Failed to attach to "
  160. "wpa_supplicant.\n");
  161. wpa_cli_close_connection();
  162. return -1;
  163. }
  164. }
  165. return 0;
  166. }
  167. static void wpa_cli_close_connection(void)
  168. {
  169. if (ctrl_conn == NULL)
  170. return;
  171. if (wpa_cli_attached) {
  172. wpa_ctrl_detach(interactive ? mon_conn : ctrl_conn);
  173. wpa_cli_attached = 0;
  174. }
  175. wpa_ctrl_close(ctrl_conn);
  176. ctrl_conn = NULL;
  177. if (mon_conn) {
  178. eloop_unregister_read_sock(wpa_ctrl_get_fd(mon_conn));
  179. wpa_ctrl_close(mon_conn);
  180. mon_conn = NULL;
  181. }
  182. }
  183. static void wpa_cli_msg_cb(char *msg, size_t len)
  184. {
  185. printf("%s\n", msg);
  186. }
  187. static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd, int print)
  188. {
  189. char buf[4096];
  190. size_t len;
  191. int ret;
  192. if (ctrl_conn == NULL) {
  193. printf("Not connected to wpa_supplicant - command dropped.\n");
  194. return -1;
  195. }
  196. if (ifname_prefix) {
  197. os_snprintf(buf, sizeof(buf), "IFNAME=%s %s",
  198. ifname_prefix, cmd);
  199. buf[sizeof(buf) - 1] = '\0';
  200. cmd = buf;
  201. }
  202. len = sizeof(buf) - 1;
  203. ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
  204. wpa_cli_msg_cb);
  205. if (ret == -2) {
  206. printf("'%s' command timed out.\n", cmd);
  207. return -2;
  208. } else if (ret < 0) {
  209. printf("'%s' command failed.\n", cmd);
  210. return -1;
  211. }
  212. if (print) {
  213. buf[len] = '\0';
  214. printf("%s", buf);
  215. if (interactive && len > 0 && buf[len - 1] != '\n')
  216. printf("\n");
  217. }
  218. return 0;
  219. }
  220. static int wpa_ctrl_command(struct wpa_ctrl *ctrl, const char *cmd)
  221. {
  222. return _wpa_ctrl_command(ctrl, cmd, 1);
  223. }
  224. static int wpa_cli_cmd(struct wpa_ctrl *ctrl, const char *cmd, int min_args,
  225. int argc, char *argv[])
  226. {
  227. char buf[4096];
  228. if (argc < min_args) {
  229. printf("Invalid %s command - at least %d argument%s "
  230. "required.\n", cmd, min_args,
  231. min_args > 1 ? "s are" : " is");
  232. return -1;
  233. }
  234. if (write_cmd(buf, sizeof(buf), cmd, argc, argv) < 0)
  235. return -1;
  236. return wpa_ctrl_command(ctrl, buf);
  237. }
  238. static int wpa_cli_cmd_ifname(struct wpa_ctrl *ctrl, int argc, char *argv[])
  239. {
  240. return wpa_ctrl_command(ctrl, "IFNAME");
  241. }
  242. static int wpa_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
  243. {
  244. if (argc > 0 && os_strcmp(argv[0], "verbose") == 0)
  245. return wpa_ctrl_command(ctrl, "STATUS-VERBOSE");
  246. if (argc > 0 && os_strcmp(argv[0], "wps") == 0)
  247. return wpa_ctrl_command(ctrl, "STATUS-WPS");
  248. if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
  249. return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
  250. #ifdef ANDROID
  251. if (argc > 0 && os_strcmp(argv[0], "no_events") == 0)
  252. return wpa_ctrl_command(ctrl, "STATUS-NO_EVENTS");
  253. #endif /* ANDROID */
  254. return wpa_ctrl_command(ctrl, "STATUS");
  255. }
  256. static int wpa_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
  257. {
  258. return wpa_ctrl_command(ctrl, "PING");
  259. }
  260. static int wpa_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
  261. {
  262. return wpa_ctrl_command(ctrl, "RELOG");
  263. }
  264. static int wpa_cli_cmd_note(struct wpa_ctrl *ctrl, int argc, char *argv[])
  265. {
  266. return wpa_cli_cmd(ctrl, "NOTE", 1, argc, argv);
  267. }
  268. static int wpa_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
  269. {
  270. return wpa_ctrl_command(ctrl, "MIB");
  271. }
  272. static int wpa_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
  273. {
  274. return wpa_ctrl_command(ctrl, "PMKSA");
  275. }
  276. static int wpa_cli_cmd_pmksa_flush(struct wpa_ctrl *ctrl, int argc,
  277. char *argv[])
  278. {
  279. return wpa_ctrl_command(ctrl, "PMKSA_FLUSH");
  280. }
  281. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  282. static int wpa_cli_cmd_pmksa_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
  283. {
  284. return wpa_cli_cmd(ctrl, "PMKSA_GET", 1, argc, argv);
  285. }
  286. static int wpa_cli_cmd_pmksa_add(struct wpa_ctrl *ctrl, int argc, char *argv[])
  287. {
  288. return wpa_cli_cmd(ctrl, "PMKSA_ADD", 8, argc, argv);
  289. }
  290. #ifdef CONFIG_MESH
  291. static int wpa_cli_mesh_cmd_pmksa_get(struct wpa_ctrl *ctrl, int argc,
  292. char *argv[])
  293. {
  294. return wpa_cli_cmd(ctrl, "MESH_PMKSA_GET", 1, argc, argv);
  295. }
  296. static int wpa_cli_mesh_cmd_pmksa_add(struct wpa_ctrl *ctrl, int argc,
  297. char *argv[])
  298. {
  299. return wpa_cli_cmd(ctrl, "MESH_PMKSA_ADD", 4, argc, argv);
  300. }
  301. #endif /* CONFIG_MESH */
  302. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  303. static int wpa_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
  304. {
  305. print_help(argc > 0 ? argv[0] : NULL);
  306. return 0;
  307. }
  308. static char ** wpa_cli_complete_help(const char *str, int pos)
  309. {
  310. int arg = get_cmd_arg_num(str, pos);
  311. char **res = NULL;
  312. switch (arg) {
  313. case 1:
  314. res = wpa_list_cmd_list();
  315. break;
  316. }
  317. return res;
  318. }
  319. static int wpa_cli_cmd_license(struct wpa_ctrl *ctrl, int argc, char *argv[])
  320. {
  321. printf("%s\n\n%s\n", wpa_cli_version, cli_full_license);
  322. return 0;
  323. }
  324. static int wpa_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
  325. {
  326. wpa_cli_quit = 1;
  327. if (interactive)
  328. eloop_terminate();
  329. return 0;
  330. }
  331. static int wpa_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
  332. {
  333. char cmd[256];
  334. int res;
  335. if (argc == 1) {
  336. res = os_snprintf(cmd, sizeof(cmd), "SET %s ", argv[0]);
  337. if (os_snprintf_error(sizeof(cmd), res)) {
  338. printf("Too long SET command.\n");
  339. return -1;
  340. }
  341. return wpa_ctrl_command(ctrl, cmd);
  342. }
  343. return wpa_cli_cmd(ctrl, "SET", 2, argc, argv);
  344. }
  345. static char ** wpa_cli_complete_set(const char *str, int pos)
  346. {
  347. int arg = get_cmd_arg_num(str, pos);
  348. const char *fields[] = {
  349. /* runtime values */
  350. "EAPOL::heldPeriod", "EAPOL::authPeriod", "EAPOL::startPeriod",
  351. "EAPOL::maxStart", "dot11RSNAConfigPMKLifetime",
  352. "dot11RSNAConfigPMKReauthThreshold", "dot11RSNAConfigSATimeout",
  353. "wps_fragment_size", "wps_version_number", "ampdu",
  354. "tdls_testing", "tdls_disabled", "pno", "radio_disabled",
  355. "uapsd", "ps", "wifi_display", "bssid_filter", "disallow_aps",
  356. "no_keep_alive",
  357. /* global configuration parameters */
  358. #ifdef CONFIG_CTRL_IFACE
  359. "ctrl_interface", "no_ctrl_interface", "ctrl_interface_group",
  360. #endif /* CONFIG_CTRL_IFACE */
  361. "eapol_version", "ap_scan", "bgscan",
  362. #ifdef CONFIG_MESH
  363. "user_mpm", "max_peer_links", "mesh_max_inactivity",
  364. "dot11RSNASAERetransPeriod",
  365. #endif /* CONFIG_MESH */
  366. "disable_scan_offload", "fast_reauth", "opensc_engine_path",
  367. "pkcs11_engine_path", "pkcs11_module_path", "openssl_ciphers",
  368. "pcsc_reader", "pcsc_pin", "external_sim", "driver_param",
  369. "dot11RSNAConfigPMKLifetime",
  370. "dot11RSNAConfigPMKReauthThreshold",
  371. "dot11RSNAConfigSATimeout",
  372. #ifndef CONFIG_NO_CONFIG_WRITE
  373. "update_config",
  374. #endif /* CONFIG_NO_CONFIG_WRITE */
  375. "load_dynamic_eap",
  376. #ifdef CONFIG_WPS
  377. "uuid", "device_name", "manufacturer", "model_name",
  378. "model_number", "serial_number", "device_type", "os_version",
  379. "config_methods", "wps_cred_processing", "wps_vendor_ext_m1",
  380. #endif /* CONFIG_WPS */
  381. #ifdef CONFIG_P2P
  382. "sec_device_type",
  383. "p2p_listen_reg_class", "p2p_listen_channel",
  384. "p2p_oper_reg_class", "p2p_oper_channel", "p2p_go_intent",
  385. "p2p_ssid_postfix", "persistent_reconnect", "p2p_intra_bss",
  386. "p2p_group_idle", "p2p_passphrase_len", "p2p_pref_chan",
  387. "p2p_no_go_freq", "p2p_add_cli_chan",
  388. "p2p_optimize_listen_chan", "p2p_go_ht40", "p2p_go_vht",
  389. "p2p_disabled", "p2p_go_ctwindow", "p2p_no_group_iface",
  390. "p2p_ignore_shared_freq", "ip_addr_go", "ip_addr_mask",
  391. "ip_addr_start", "ip_addr_end",
  392. #endif /* CONFIG_P2P */
  393. "country", "bss_max_count", "bss_expiration_age",
  394. "bss_expiration_scan_count", "filter_ssids", "filter_rssi",
  395. "max_num_sta", "disassoc_low_ack", "ap_isolate",
  396. #ifdef CONFIG_HS20
  397. "hs20",
  398. #endif /* CONFIG_HS20 */
  399. "interworking", "hessid", "access_network_type", "pbc_in_m1",
  400. "go_interworking", "go_access_network_type", "go_internet",
  401. "go_venue_group", "go_venue_type",
  402. "autoscan", "wps_nfc_dev_pw_id", "wps_nfc_dh_pubkey",
  403. "wps_nfc_dh_privkey", "wps_nfc_dev_pw", "ext_password_backend",
  404. "p2p_go_max_inactivity", "auto_interworking", "okc", "pmf",
  405. "sae_groups", "dtim_period", "beacon_int",
  406. "ap_vendor_elements", "ignore_old_scan_res", "freq_list",
  407. "scan_cur_freq", "sched_scan_interval",
  408. "tdls_external_control", "osu_dir", "wowlan_triggers",
  409. "p2p_search_delay", "mac_addr", "rand_addr_lifetime",
  410. "preassoc_mac_addr", "key_mgmt_offload", "passive_scan",
  411. "reassoc_same_bss_optim", "wps_priority",
  412. #ifdef CONFIG_TESTING_OPTIONS
  413. "ignore_auth_resp",
  414. #endif /* CONFIG_TESTING_OPTIONS */
  415. "relative_rssi", "relative_band_adjust",
  416. };
  417. int i, num_fields = ARRAY_SIZE(fields);
  418. if (arg == 1) {
  419. char **res = os_calloc(num_fields + 1, sizeof(char *));
  420. if (res == NULL)
  421. return NULL;
  422. for (i = 0; i < num_fields; i++) {
  423. res[i] = os_strdup(fields[i]);
  424. if (res[i] == NULL)
  425. return res;
  426. }
  427. return res;
  428. }
  429. if (arg > 1 && os_strncasecmp(str, "set bssid_filter ", 17) == 0)
  430. return cli_txt_list_array(&bsses);
  431. return NULL;
  432. }
  433. static int wpa_cli_cmd_dump(struct wpa_ctrl *ctrl, int argc, char *argv[])
  434. {
  435. return wpa_ctrl_command(ctrl, "DUMP");
  436. }
  437. static int wpa_cli_cmd_driver_flags(struct wpa_ctrl *ctrl, int argc,
  438. char *argv[])
  439. {
  440. return wpa_ctrl_command(ctrl, "DRIVER_FLAGS");
  441. }
  442. static int wpa_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
  443. {
  444. return wpa_cli_cmd(ctrl, "GET", 1, argc, argv);
  445. }
  446. static char ** wpa_cli_complete_get(const char *str, int pos)
  447. {
  448. int arg = get_cmd_arg_num(str, pos);
  449. const char *fields[] = {
  450. #ifdef CONFIG_CTRL_IFACE
  451. "ctrl_interface", "ctrl_interface_group",
  452. #endif /* CONFIG_CTRL_IFACE */
  453. "eapol_version", "ap_scan",
  454. #ifdef CONFIG_MESH
  455. "user_mpm", "max_peer_links", "mesh_max_inactivity",
  456. #endif /* CONFIG_MESH */
  457. "disable_scan_offload", "fast_reauth", "opensc_engine_path",
  458. "pkcs11_engine_path", "pkcs11_module_path", "openssl_ciphers",
  459. "pcsc_reader", "pcsc_pin", "external_sim", "driver_param",
  460. "dot11RSNAConfigPMKLifetime",
  461. "dot11RSNAConfigPMKReauthThreshold",
  462. "dot11RSNAConfigSATimeout",
  463. #ifndef CONFIG_NO_CONFIG_WRITE
  464. "update_config",
  465. #endif /* CONFIG_NO_CONFIG_WRITE */
  466. #ifdef CONFIG_WPS
  467. "device_name", "manufacturer", "model_name", "model_number",
  468. "serial_number", "config_methods", "wps_cred_processing",
  469. #endif /* CONFIG_WPS */
  470. #ifdef CONFIG_P2P
  471. "p2p_listen_reg_class", "p2p_listen_channel",
  472. "p2p_oper_reg_class", "p2p_oper_channel", "p2p_go_intent",
  473. "p2p_ssid_postfix", "persistent_reconnect", "p2p_intra_bss",
  474. "p2p_group_idle", "p2p_passphrase_len", "p2p_add_cli_chan",
  475. "p2p_optimize_listen_chan", "p2p_go_ht40", "p2p_go_vht",
  476. "p2p_disabled", "p2p_go_ctwindow", "p2p_no_group_iface",
  477. "p2p_ignore_shared_freq", "ip_addr_go", "ip_addr_mask",
  478. "ip_addr_start", "ip_addr_end",
  479. #endif /* CONFIG_P2P */
  480. "bss_max_count", "bss_expiration_age",
  481. "bss_expiration_scan_count", "filter_ssids", "filter_rssi",
  482. "max_num_sta", "disassoc_low_ack", "ap_isolate",
  483. #ifdef CONFIG_HS20
  484. "hs20",
  485. #endif /* CONFIG_HS20 */
  486. "interworking", "access_network_type", "pbc_in_m1", "autoscan",
  487. "go_interworking", "go_access_network_type", "go_internet",
  488. "go_venue_group", "go_venue_type",
  489. "wps_nfc_dev_pw_id", "ext_password_backend",
  490. "p2p_go_max_inactivity", "auto_interworking", "okc", "pmf",
  491. "dtim_period", "beacon_int", "ignore_old_scan_res",
  492. "scan_cur_freq", "sched_scan_interval",
  493. "sched_scan_start_delay",
  494. "tdls_external_control", "osu_dir", "wowlan_triggers",
  495. "p2p_search_delay", "mac_addr", "rand_addr_lifetime",
  496. "preassoc_mac_addr", "key_mgmt_offload", "passive_scan",
  497. "reassoc_same_bss_optim"
  498. };
  499. int i, num_fields = ARRAY_SIZE(fields);
  500. if (arg == 1) {
  501. char **res = os_calloc(num_fields + 1, sizeof(char *));
  502. if (res == NULL)
  503. return NULL;
  504. for (i = 0; i < num_fields; i++) {
  505. res[i] = os_strdup(fields[i]);
  506. if (res[i] == NULL)
  507. return res;
  508. }
  509. return res;
  510. }
  511. return NULL;
  512. }
  513. static int wpa_cli_cmd_logoff(struct wpa_ctrl *ctrl, int argc, char *argv[])
  514. {
  515. return wpa_ctrl_command(ctrl, "LOGOFF");
  516. }
  517. static int wpa_cli_cmd_logon(struct wpa_ctrl *ctrl, int argc, char *argv[])
  518. {
  519. return wpa_ctrl_command(ctrl, "LOGON");
  520. }
  521. static int wpa_cli_cmd_reassociate(struct wpa_ctrl *ctrl, int argc,
  522. char *argv[])
  523. {
  524. return wpa_ctrl_command(ctrl, "REASSOCIATE");
  525. }
  526. static int wpa_cli_cmd_reattach(struct wpa_ctrl *ctrl, int argc, char *argv[])
  527. {
  528. return wpa_ctrl_command(ctrl, "REATTACH");
  529. }
  530. static int wpa_cli_cmd_preauthenticate(struct wpa_ctrl *ctrl, int argc,
  531. char *argv[])
  532. {
  533. return wpa_cli_cmd(ctrl, "PREAUTH", 1, argc, argv);
  534. }
  535. static int wpa_cli_cmd_ap_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
  536. {
  537. return wpa_cli_cmd(ctrl, "AP_SCAN", 1, argc, argv);
  538. }
  539. static int wpa_cli_cmd_scan_interval(struct wpa_ctrl *ctrl, int argc,
  540. char *argv[])
  541. {
  542. return wpa_cli_cmd(ctrl, "SCAN_INTERVAL", 1, argc, argv);
  543. }
  544. static int wpa_cli_cmd_bss_expire_age(struct wpa_ctrl *ctrl, int argc,
  545. char *argv[])
  546. {
  547. return wpa_cli_cmd(ctrl, "BSS_EXPIRE_AGE", 1, argc, argv);
  548. }
  549. static int wpa_cli_cmd_bss_expire_count(struct wpa_ctrl *ctrl, int argc,
  550. char *argv[])
  551. {
  552. return wpa_cli_cmd(ctrl, "BSS_EXPIRE_COUNT", 1, argc, argv);
  553. }
  554. static int wpa_cli_cmd_bss_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
  555. {
  556. char cmd[256];
  557. int res;
  558. if (argc < 1)
  559. res = os_snprintf(cmd, sizeof(cmd), "BSS_FLUSH 0");
  560. else
  561. res = os_snprintf(cmd, sizeof(cmd), "BSS_FLUSH %s", argv[0]);
  562. if (os_snprintf_error(sizeof(cmd), res)) {
  563. printf("Too long BSS_FLUSH command.\n");
  564. return -1;
  565. }
  566. return wpa_ctrl_command(ctrl, cmd);
  567. }
  568. static int wpa_cli_cmd_ft_ds(struct wpa_ctrl *ctrl, int argc, char *argv[])
  569. {
  570. return wpa_cli_cmd(ctrl, "FT_DS", 1, argc, argv);
  571. }
  572. static int wpa_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc, char *argv[])
  573. {
  574. return wpa_cli_cmd(ctrl, "WPS_PBC", 0, argc, argv);
  575. }
  576. static int wpa_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
  577. {
  578. if (argc == 0) {
  579. printf("Invalid WPS_PIN command: need one or two arguments:\n"
  580. "- BSSID: use 'any' to select any\n"
  581. "- PIN: optional, used only with devices that have no "
  582. "display\n");
  583. return -1;
  584. }
  585. return wpa_cli_cmd(ctrl, "WPS_PIN", 1, argc, argv);
  586. }
  587. static int wpa_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
  588. char *argv[])
  589. {
  590. return wpa_cli_cmd(ctrl, "WPS_CHECK_PIN", 1, argc, argv);
  591. }
  592. static int wpa_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
  593. char *argv[])
  594. {
  595. return wpa_ctrl_command(ctrl, "WPS_CANCEL");
  596. }
  597. #ifdef CONFIG_WPS_NFC
  598. static int wpa_cli_cmd_wps_nfc(struct wpa_ctrl *ctrl, int argc, char *argv[])
  599. {
  600. return wpa_cli_cmd(ctrl, "WPS_NFC", 0, argc, argv);
  601. }
  602. static int wpa_cli_cmd_wps_nfc_config_token(struct wpa_ctrl *ctrl, int argc,
  603. char *argv[])
  604. {
  605. return wpa_cli_cmd(ctrl, "WPS_NFC_CONFIG_TOKEN", 1, argc, argv);
  606. }
  607. static int wpa_cli_cmd_wps_nfc_token(struct wpa_ctrl *ctrl, int argc,
  608. char *argv[])
  609. {
  610. return wpa_cli_cmd(ctrl, "WPS_NFC_TOKEN", 1, argc, argv);
  611. }
  612. static int wpa_cli_cmd_wps_nfc_tag_read(struct wpa_ctrl *ctrl, int argc,
  613. char *argv[])
  614. {
  615. int ret;
  616. char *buf;
  617. size_t buflen;
  618. if (argc != 1) {
  619. printf("Invalid 'wps_nfc_tag_read' command - one argument "
  620. "is required.\n");
  621. return -1;
  622. }
  623. buflen = 18 + os_strlen(argv[0]);
  624. buf = os_malloc(buflen);
  625. if (buf == NULL)
  626. return -1;
  627. os_snprintf(buf, buflen, "WPS_NFC_TAG_READ %s", argv[0]);
  628. ret = wpa_ctrl_command(ctrl, buf);
  629. os_free(buf);
  630. return ret;
  631. }
  632. static int wpa_cli_cmd_nfc_get_handover_req(struct wpa_ctrl *ctrl, int argc,
  633. char *argv[])
  634. {
  635. return wpa_cli_cmd(ctrl, "NFC_GET_HANDOVER_REQ", 2, argc, argv);
  636. }
  637. static int wpa_cli_cmd_nfc_get_handover_sel(struct wpa_ctrl *ctrl, int argc,
  638. char *argv[])
  639. {
  640. return wpa_cli_cmd(ctrl, "NFC_GET_HANDOVER_SEL", 2, argc, argv);
  641. }
  642. static int wpa_cli_cmd_nfc_report_handover(struct wpa_ctrl *ctrl, int argc,
  643. char *argv[])
  644. {
  645. return wpa_cli_cmd(ctrl, "NFC_REPORT_HANDOVER", 4, argc, argv);
  646. }
  647. #endif /* CONFIG_WPS_NFC */
  648. static int wpa_cli_cmd_wps_reg(struct wpa_ctrl *ctrl, int argc, char *argv[])
  649. {
  650. char cmd[256];
  651. int res;
  652. if (argc == 2)
  653. res = os_snprintf(cmd, sizeof(cmd), "WPS_REG %s %s",
  654. argv[0], argv[1]);
  655. else if (argc == 5 || argc == 6) {
  656. char ssid_hex[2 * SSID_MAX_LEN + 1];
  657. char key_hex[2 * 64 + 1];
  658. int i;
  659. ssid_hex[0] = '\0';
  660. for (i = 0; i < SSID_MAX_LEN; i++) {
  661. if (argv[2][i] == '\0')
  662. break;
  663. os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
  664. }
  665. key_hex[0] = '\0';
  666. if (argc == 6) {
  667. for (i = 0; i < 64; i++) {
  668. if (argv[5][i] == '\0')
  669. break;
  670. os_snprintf(&key_hex[i * 2], 3, "%02x",
  671. argv[5][i]);
  672. }
  673. }
  674. res = os_snprintf(cmd, sizeof(cmd),
  675. "WPS_REG %s %s %s %s %s %s",
  676. argv[0], argv[1], ssid_hex, argv[3], argv[4],
  677. key_hex);
  678. } else {
  679. printf("Invalid WPS_REG command: need two arguments:\n"
  680. "- BSSID of the target AP\n"
  681. "- AP PIN\n");
  682. printf("Alternatively, six arguments can be used to "
  683. "reconfigure the AP:\n"
  684. "- BSSID of the target AP\n"
  685. "- AP PIN\n"
  686. "- new SSID\n"
  687. "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
  688. "- new encr (NONE, WEP, TKIP, CCMP)\n"
  689. "- new key\n");
  690. return -1;
  691. }
  692. if (os_snprintf_error(sizeof(cmd), res)) {
  693. printf("Too long WPS_REG command.\n");
  694. return -1;
  695. }
  696. return wpa_ctrl_command(ctrl, cmd);
  697. }
  698. static int wpa_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
  699. char *argv[])
  700. {
  701. return wpa_cli_cmd(ctrl, "WPS_AP_PIN", 1, argc, argv);
  702. }
  703. static int wpa_cli_cmd_wps_er_start(struct wpa_ctrl *ctrl, int argc,
  704. char *argv[])
  705. {
  706. return wpa_cli_cmd(ctrl, "WPS_ER_START", 0, argc, argv);
  707. }
  708. static int wpa_cli_cmd_wps_er_stop(struct wpa_ctrl *ctrl, int argc,
  709. char *argv[])
  710. {
  711. return wpa_ctrl_command(ctrl, "WPS_ER_STOP");
  712. }
  713. static int wpa_cli_cmd_wps_er_pin(struct wpa_ctrl *ctrl, int argc,
  714. char *argv[])
  715. {
  716. if (argc < 2) {
  717. printf("Invalid WPS_ER_PIN command: need at least two "
  718. "arguments:\n"
  719. "- UUID: use 'any' to select any\n"
  720. "- PIN: Enrollee PIN\n"
  721. "optional: - Enrollee MAC address\n");
  722. return -1;
  723. }
  724. return wpa_cli_cmd(ctrl, "WPS_ER_PIN", 2, argc, argv);
  725. }
  726. static int wpa_cli_cmd_wps_er_pbc(struct wpa_ctrl *ctrl, int argc,
  727. char *argv[])
  728. {
  729. return wpa_cli_cmd(ctrl, "WPS_ER_PBC", 1, argc, argv);
  730. }
  731. static int wpa_cli_cmd_wps_er_learn(struct wpa_ctrl *ctrl, int argc,
  732. char *argv[])
  733. {
  734. if (argc != 2) {
  735. printf("Invalid WPS_ER_LEARN command: need two arguments:\n"
  736. "- UUID: specify which AP to use\n"
  737. "- PIN: AP PIN\n");
  738. return -1;
  739. }
  740. return wpa_cli_cmd(ctrl, "WPS_ER_LEARN", 2, argc, argv);
  741. }
  742. static int wpa_cli_cmd_wps_er_set_config(struct wpa_ctrl *ctrl, int argc,
  743. char *argv[])
  744. {
  745. if (argc != 2) {
  746. printf("Invalid WPS_ER_SET_CONFIG command: need two "
  747. "arguments:\n"
  748. "- UUID: specify which AP to use\n"
  749. "- Network configuration id\n");
  750. return -1;
  751. }
  752. return wpa_cli_cmd(ctrl, "WPS_ER_SET_CONFIG", 2, argc, argv);
  753. }
  754. static int wpa_cli_cmd_wps_er_config(struct wpa_ctrl *ctrl, int argc,
  755. char *argv[])
  756. {
  757. char cmd[256];
  758. int res;
  759. if (argc == 5 || argc == 6) {
  760. char ssid_hex[2 * SSID_MAX_LEN + 1];
  761. char key_hex[2 * 64 + 1];
  762. int i;
  763. ssid_hex[0] = '\0';
  764. for (i = 0; i < SSID_MAX_LEN; i++) {
  765. if (argv[2][i] == '\0')
  766. break;
  767. os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
  768. }
  769. key_hex[0] = '\0';
  770. if (argc == 6) {
  771. for (i = 0; i < 64; i++) {
  772. if (argv[5][i] == '\0')
  773. break;
  774. os_snprintf(&key_hex[i * 2], 3, "%02x",
  775. argv[5][i]);
  776. }
  777. }
  778. res = os_snprintf(cmd, sizeof(cmd),
  779. "WPS_ER_CONFIG %s %s %s %s %s %s",
  780. argv[0], argv[1], ssid_hex, argv[3], argv[4],
  781. key_hex);
  782. } else {
  783. printf("Invalid WPS_ER_CONFIG command: need six arguments:\n"
  784. "- AP UUID\n"
  785. "- AP PIN\n"
  786. "- new SSID\n"
  787. "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
  788. "- new encr (NONE, WEP, TKIP, CCMP)\n"
  789. "- new key\n");
  790. return -1;
  791. }
  792. if (os_snprintf_error(sizeof(cmd), res)) {
  793. printf("Too long WPS_ER_CONFIG command.\n");
  794. return -1;
  795. }
  796. return wpa_ctrl_command(ctrl, cmd);
  797. }
  798. #ifdef CONFIG_WPS_NFC
  799. static int wpa_cli_cmd_wps_er_nfc_config_token(struct wpa_ctrl *ctrl, int argc,
  800. char *argv[])
  801. {
  802. if (argc != 2) {
  803. printf("Invalid WPS_ER_NFC_CONFIG_TOKEN command: need two "
  804. "arguments:\n"
  805. "- WPS/NDEF: token format\n"
  806. "- UUID: specify which AP to use\n");
  807. return -1;
  808. }
  809. return wpa_cli_cmd(ctrl, "WPS_ER_NFC_CONFIG_TOKEN", 2, argc, argv);
  810. }
  811. #endif /* CONFIG_WPS_NFC */
  812. static int wpa_cli_cmd_ibss_rsn(struct wpa_ctrl *ctrl, int argc, char *argv[])
  813. {
  814. return wpa_cli_cmd(ctrl, "IBSS_RSN", 1, argc, argv);
  815. }
  816. static int wpa_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
  817. {
  818. return wpa_cli_cmd(ctrl, "LEVEL", 1, argc, argv);
  819. }
  820. static int wpa_cli_cmd_identity(struct wpa_ctrl *ctrl, int argc, char *argv[])
  821. {
  822. char cmd[256], *pos, *end;
  823. int i, ret;
  824. if (argc < 2) {
  825. printf("Invalid IDENTITY command: needs two arguments "
  826. "(network id and identity)\n");
  827. return -1;
  828. }
  829. end = cmd + sizeof(cmd);
  830. pos = cmd;
  831. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s",
  832. argv[0], argv[1]);
  833. if (os_snprintf_error(end - pos, ret)) {
  834. printf("Too long IDENTITY command.\n");
  835. return -1;
  836. }
  837. pos += ret;
  838. for (i = 2; i < argc; i++) {
  839. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  840. if (os_snprintf_error(end - pos, ret)) {
  841. printf("Too long IDENTITY command.\n");
  842. return -1;
  843. }
  844. pos += ret;
  845. }
  846. return wpa_ctrl_command(ctrl, cmd);
  847. }
  848. static int wpa_cli_cmd_password(struct wpa_ctrl *ctrl, int argc, char *argv[])
  849. {
  850. char cmd[256], *pos, *end;
  851. int i, ret;
  852. if (argc < 2) {
  853. printf("Invalid PASSWORD command: needs two arguments "
  854. "(network id and password)\n");
  855. return -1;
  856. }
  857. end = cmd + sizeof(cmd);
  858. pos = cmd;
  859. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s",
  860. argv[0], argv[1]);
  861. if (os_snprintf_error(end - pos, ret)) {
  862. printf("Too long PASSWORD command.\n");
  863. return -1;
  864. }
  865. pos += ret;
  866. for (i = 2; i < argc; i++) {
  867. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  868. if (os_snprintf_error(end - pos, ret)) {
  869. printf("Too long PASSWORD command.\n");
  870. return -1;
  871. }
  872. pos += ret;
  873. }
  874. return wpa_ctrl_command(ctrl, cmd);
  875. }
  876. static int wpa_cli_cmd_new_password(struct wpa_ctrl *ctrl, int argc,
  877. char *argv[])
  878. {
  879. char cmd[256], *pos, *end;
  880. int i, ret;
  881. if (argc < 2) {
  882. printf("Invalid NEW_PASSWORD command: needs two arguments "
  883. "(network id and password)\n");
  884. return -1;
  885. }
  886. end = cmd + sizeof(cmd);
  887. pos = cmd;
  888. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s",
  889. argv[0], argv[1]);
  890. if (os_snprintf_error(end - pos, ret)) {
  891. printf("Too long NEW_PASSWORD command.\n");
  892. return -1;
  893. }
  894. pos += ret;
  895. for (i = 2; i < argc; i++) {
  896. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  897. if (os_snprintf_error(end - pos, ret)) {
  898. printf("Too long NEW_PASSWORD command.\n");
  899. return -1;
  900. }
  901. pos += ret;
  902. }
  903. return wpa_ctrl_command(ctrl, cmd);
  904. }
  905. static int wpa_cli_cmd_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
  906. {
  907. char cmd[256], *pos, *end;
  908. int i, ret;
  909. if (argc < 2) {
  910. printf("Invalid PIN command: needs two arguments "
  911. "(network id and pin)\n");
  912. return -1;
  913. }
  914. end = cmd + sizeof(cmd);
  915. pos = cmd;
  916. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s",
  917. argv[0], argv[1]);
  918. if (os_snprintf_error(end - pos, ret)) {
  919. printf("Too long PIN command.\n");
  920. return -1;
  921. }
  922. pos += ret;
  923. for (i = 2; i < argc; i++) {
  924. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  925. if (os_snprintf_error(end - pos, ret)) {
  926. printf("Too long PIN command.\n");
  927. return -1;
  928. }
  929. pos += ret;
  930. }
  931. return wpa_ctrl_command(ctrl, cmd);
  932. }
  933. static int wpa_cli_cmd_otp(struct wpa_ctrl *ctrl, int argc, char *argv[])
  934. {
  935. char cmd[256], *pos, *end;
  936. int i, ret;
  937. if (argc < 2) {
  938. printf("Invalid OTP command: needs two arguments (network "
  939. "id and password)\n");
  940. return -1;
  941. }
  942. end = cmd + sizeof(cmd);
  943. pos = cmd;
  944. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s",
  945. argv[0], argv[1]);
  946. if (os_snprintf_error(end - pos, ret)) {
  947. printf("Too long OTP command.\n");
  948. return -1;
  949. }
  950. pos += ret;
  951. for (i = 2; i < argc; i++) {
  952. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  953. if (os_snprintf_error(end - pos, ret)) {
  954. printf("Too long OTP command.\n");
  955. return -1;
  956. }
  957. pos += ret;
  958. }
  959. return wpa_ctrl_command(ctrl, cmd);
  960. }
  961. static int wpa_cli_cmd_sim(struct wpa_ctrl *ctrl, int argc, char *argv[])
  962. {
  963. char cmd[256], *pos, *end;
  964. int i, ret;
  965. if (argc < 2) {
  966. printf("Invalid SIM command: needs two arguments "
  967. "(network id and SIM operation response)\n");
  968. return -1;
  969. }
  970. end = cmd + sizeof(cmd);
  971. pos = cmd;
  972. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "SIM-%s:%s",
  973. argv[0], argv[1]);
  974. if (os_snprintf_error(end - pos, ret)) {
  975. printf("Too long SIM command.\n");
  976. return -1;
  977. }
  978. pos += ret;
  979. for (i = 2; i < argc; i++) {
  980. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  981. if (os_snprintf_error(end - pos, ret)) {
  982. printf("Too long SIM command.\n");
  983. return -1;
  984. }
  985. pos += ret;
  986. }
  987. return wpa_ctrl_command(ctrl, cmd);
  988. }
  989. static int wpa_cli_cmd_passphrase(struct wpa_ctrl *ctrl, int argc,
  990. char *argv[])
  991. {
  992. char cmd[256], *pos, *end;
  993. int i, ret;
  994. if (argc < 2) {
  995. printf("Invalid PASSPHRASE command: needs two arguments "
  996. "(network id and passphrase)\n");
  997. return -1;
  998. }
  999. end = cmd + sizeof(cmd);
  1000. pos = cmd;
  1001. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s",
  1002. argv[0], argv[1]);
  1003. if (os_snprintf_error(end - pos, ret)) {
  1004. printf("Too long PASSPHRASE command.\n");
  1005. return -1;
  1006. }
  1007. pos += ret;
  1008. for (i = 2; i < argc; i++) {
  1009. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  1010. if (os_snprintf_error(end - pos, ret)) {
  1011. printf("Too long PASSPHRASE command.\n");
  1012. return -1;
  1013. }
  1014. pos += ret;
  1015. }
  1016. return wpa_ctrl_command(ctrl, cmd);
  1017. }
  1018. static int wpa_cli_cmd_bssid(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1019. {
  1020. if (argc < 2) {
  1021. printf("Invalid BSSID command: needs two arguments (network "
  1022. "id and BSSID)\n");
  1023. return -1;
  1024. }
  1025. return wpa_cli_cmd(ctrl, "BSSID", 2, argc, argv);
  1026. }
  1027. static int wpa_cli_cmd_blacklist(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1028. {
  1029. return wpa_cli_cmd(ctrl, "BLACKLIST", 0, argc, argv);
  1030. }
  1031. static int wpa_cli_cmd_log_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1032. {
  1033. return wpa_cli_cmd(ctrl, "LOG_LEVEL", 0, argc, argv);
  1034. }
  1035. static int wpa_cli_cmd_list_networks(struct wpa_ctrl *ctrl, int argc,
  1036. char *argv[])
  1037. {
  1038. return wpa_ctrl_command(ctrl, "LIST_NETWORKS");
  1039. }
  1040. static int wpa_cli_cmd_select_network(struct wpa_ctrl *ctrl, int argc,
  1041. char *argv[])
  1042. {
  1043. return wpa_cli_cmd(ctrl, "SELECT_NETWORK", 1, argc, argv);
  1044. }
  1045. static int wpa_cli_cmd_enable_network(struct wpa_ctrl *ctrl, int argc,
  1046. char *argv[])
  1047. {
  1048. return wpa_cli_cmd(ctrl, "ENABLE_NETWORK", 1, argc, argv);
  1049. }
  1050. static int wpa_cli_cmd_disable_network(struct wpa_ctrl *ctrl, int argc,
  1051. char *argv[])
  1052. {
  1053. return wpa_cli_cmd(ctrl, "DISABLE_NETWORK", 1, argc, argv);
  1054. }
  1055. static int wpa_cli_cmd_add_network(struct wpa_ctrl *ctrl, int argc,
  1056. char *argv[])
  1057. {
  1058. int res = wpa_ctrl_command(ctrl, "ADD_NETWORK");
  1059. if (interactive)
  1060. update_networks(ctrl);
  1061. return res;
  1062. }
  1063. static int wpa_cli_cmd_remove_network(struct wpa_ctrl *ctrl, int argc,
  1064. char *argv[])
  1065. {
  1066. int res = wpa_cli_cmd(ctrl, "REMOVE_NETWORK", 1, argc, argv);
  1067. if (interactive)
  1068. update_networks(ctrl);
  1069. return res;
  1070. }
  1071. static void wpa_cli_show_network_variables(void)
  1072. {
  1073. printf("set_network variables:\n"
  1074. " ssid (network name, SSID)\n"
  1075. " psk (WPA passphrase or pre-shared key)\n"
  1076. " key_mgmt (key management protocol)\n"
  1077. " identity (EAP identity)\n"
  1078. " password (EAP password)\n"
  1079. " ...\n"
  1080. "\n"
  1081. "Note: Values are entered in the same format as the "
  1082. "configuration file is using,\n"
  1083. "i.e., strings values need to be inside double quotation "
  1084. "marks.\n"
  1085. "For example: set_network 1 ssid \"network name\"\n"
  1086. "\n"
  1087. "Please see wpa_supplicant.conf documentation for full list "
  1088. "of\navailable variables.\n");
  1089. }
  1090. static int wpa_cli_cmd_set_network(struct wpa_ctrl *ctrl, int argc,
  1091. char *argv[])
  1092. {
  1093. if (argc == 0) {
  1094. wpa_cli_show_network_variables();
  1095. return 0;
  1096. }
  1097. if (argc < 3) {
  1098. printf("Invalid SET_NETWORK command: needs three arguments\n"
  1099. "(network id, variable name, and value)\n");
  1100. return -1;
  1101. }
  1102. return wpa_cli_cmd(ctrl, "SET_NETWORK", 3, argc, argv);
  1103. }
  1104. static int wpa_cli_cmd_get_network(struct wpa_ctrl *ctrl, int argc,
  1105. char *argv[])
  1106. {
  1107. if (argc == 0) {
  1108. wpa_cli_show_network_variables();
  1109. return 0;
  1110. }
  1111. if (argc != 2) {
  1112. printf("Invalid GET_NETWORK command: needs two arguments\n"
  1113. "(network id and variable name)\n");
  1114. return -1;
  1115. }
  1116. return wpa_cli_cmd(ctrl, "GET_NETWORK", 2, argc, argv);
  1117. }
  1118. static const char *network_fields[] = {
  1119. "ssid", "scan_ssid", "bssid", "bssid_blacklist",
  1120. "bssid_whitelist", "psk", "proto", "key_mgmt",
  1121. "bg_scan_period", "pairwise", "group", "auth_alg", "scan_freq",
  1122. "freq_list", "max_oper_chwidth", "ht40", "vht", "vht_center_freq1",
  1123. "vht_center_freq2", "ht",
  1124. #ifdef IEEE8021X_EAPOL
  1125. "eap", "identity", "anonymous_identity", "password", "ca_cert",
  1126. "ca_path", "client_cert", "private_key", "private_key_passwd",
  1127. "dh_file", "subject_match", "altsubject_match",
  1128. "domain_suffix_match", "domain_match", "ca_cert2", "ca_path2",
  1129. "client_cert2", "private_key2", "private_key2_passwd",
  1130. "dh_file2", "subject_match2", "altsubject_match2",
  1131. "domain_suffix_match2", "domain_match2", "phase1", "phase2",
  1132. "pcsc", "pin", "engine_id", "key_id", "cert_id", "ca_cert_id",
  1133. "pin2", "engine2_id", "key2_id", "cert2_id", "ca_cert2_id",
  1134. "engine", "engine2", "eapol_flags", "sim_num",
  1135. "openssl_ciphers", "erp",
  1136. #endif /* IEEE8021X_EAPOL */
  1137. "wep_key0", "wep_key1", "wep_key2", "wep_key3",
  1138. "wep_tx_keyidx", "priority",
  1139. #ifdef IEEE8021X_EAPOL
  1140. "eap_workaround", "pac_file", "fragment_size", "ocsp",
  1141. #endif /* IEEE8021X_EAPOL */
  1142. #ifdef CONFIG_MESH
  1143. "mode", "no_auto_peer", "mesh_rssi_threshold",
  1144. #else /* CONFIG_MESH */
  1145. "mode",
  1146. #endif /* CONFIG_MESH */
  1147. "proactive_key_caching", "disabled", "id_str",
  1148. #ifdef CONFIG_IEEE80211W
  1149. "ieee80211w",
  1150. #endif /* CONFIG_IEEE80211W */
  1151. "mixed_cell", "frequency", "fixed_freq",
  1152. #ifdef CONFIG_MESH
  1153. "mesh_basic_rates", "dot11MeshMaxRetries",
  1154. "dot11MeshRetryTimeout", "dot11MeshConfirmTimeout",
  1155. "dot11MeshHoldingTimeout",
  1156. #endif /* CONFIG_MESH */
  1157. "wpa_ptk_rekey", "bgscan", "ignore_broadcast_ssid",
  1158. #ifdef CONFIG_P2P
  1159. "go_p2p_dev_addr", "p2p_client_list", "psk_list",
  1160. #endif /* CONFIG_P2P */
  1161. #ifdef CONFIG_HT_OVERRIDES
  1162. "disable_ht", "disable_ht40", "disable_sgi", "disable_ldpc",
  1163. "ht40_intolerant", "disable_max_amsdu", "ampdu_factor",
  1164. "ampdu_density", "ht_mcs",
  1165. #endif /* CONFIG_HT_OVERRIDES */
  1166. #ifdef CONFIG_VHT_OVERRIDES
  1167. "disable_vht", "vht_capa", "vht_capa_mask", "vht_rx_mcs_nss_1",
  1168. "vht_rx_mcs_nss_2", "vht_rx_mcs_nss_3", "vht_rx_mcs_nss_4",
  1169. "vht_rx_mcs_nss_5", "vht_rx_mcs_nss_6", "vht_rx_mcs_nss_7",
  1170. "vht_rx_mcs_nss_8", "vht_tx_mcs_nss_1", "vht_tx_mcs_nss_2",
  1171. "vht_tx_mcs_nss_3", "vht_tx_mcs_nss_4", "vht_tx_mcs_nss_5",
  1172. "vht_tx_mcs_nss_6", "vht_tx_mcs_nss_7", "vht_tx_mcs_nss_8",
  1173. #endif /* CONFIG_VHT_OVERRIDES */
  1174. "ap_max_inactivity", "dtim_period", "beacon_int",
  1175. #ifdef CONFIG_MACSEC
  1176. "macsec_policy",
  1177. "macsec_integ_only",
  1178. "macsec_port",
  1179. "mka_priority",
  1180. #endif /* CONFIG_MACSEC */
  1181. #ifdef CONFIG_HS20
  1182. "update_identifier",
  1183. #endif /* CONFIG_HS20 */
  1184. "mac_addr", "pbss", "wps_disabled"
  1185. };
  1186. static char ** wpa_cli_complete_network(const char *str, int pos)
  1187. {
  1188. int arg = get_cmd_arg_num(str, pos);
  1189. int i, num_fields = ARRAY_SIZE(network_fields);
  1190. char **res = NULL;
  1191. switch (arg) {
  1192. case 1:
  1193. res = cli_txt_list_array(&networks);
  1194. break;
  1195. case 2:
  1196. res = os_calloc(num_fields + 1, sizeof(char *));
  1197. if (res == NULL)
  1198. return NULL;
  1199. for (i = 0; i < num_fields; i++) {
  1200. res[i] = os_strdup(network_fields[i]);
  1201. if (res[i] == NULL)
  1202. break;
  1203. }
  1204. }
  1205. return res;
  1206. }
  1207. static char ** wpa_cli_complete_network_id(const char *str, int pos)
  1208. {
  1209. int arg = get_cmd_arg_num(str, pos);
  1210. if (arg == 1)
  1211. return cli_txt_list_array(&networks);
  1212. return NULL;
  1213. }
  1214. static int wpa_cli_cmd_dup_network(struct wpa_ctrl *ctrl, int argc,
  1215. char *argv[])
  1216. {
  1217. if (argc == 0) {
  1218. wpa_cli_show_network_variables();
  1219. return 0;
  1220. }
  1221. if (argc < 3) {
  1222. printf("Invalid DUP_NETWORK command: needs three arguments\n"
  1223. "(src netid, dest netid, and variable name)\n");
  1224. return -1;
  1225. }
  1226. return wpa_cli_cmd(ctrl, "DUP_NETWORK", 3, argc, argv);
  1227. }
  1228. static char ** wpa_cli_complete_dup_network(const char *str, int pos)
  1229. {
  1230. int arg = get_cmd_arg_num(str, pos);
  1231. int i, num_fields = ARRAY_SIZE(network_fields);
  1232. char **res = NULL;
  1233. switch (arg) {
  1234. case 1:
  1235. case 2:
  1236. res = cli_txt_list_array(&networks);
  1237. break;
  1238. case 3:
  1239. res = os_calloc(num_fields + 1, sizeof(char *));
  1240. if (res == NULL)
  1241. return NULL;
  1242. for (i = 0; i < num_fields; i++) {
  1243. res[i] = os_strdup(network_fields[i]);
  1244. if (res[i] == NULL)
  1245. break;
  1246. }
  1247. }
  1248. return res;
  1249. }
  1250. static int wpa_cli_cmd_list_creds(struct wpa_ctrl *ctrl, int argc,
  1251. char *argv[])
  1252. {
  1253. return wpa_ctrl_command(ctrl, "LIST_CREDS");
  1254. }
  1255. static int wpa_cli_cmd_add_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1256. {
  1257. int res = wpa_ctrl_command(ctrl, "ADD_CRED");
  1258. if (interactive)
  1259. update_creds(ctrl);
  1260. return res;
  1261. }
  1262. static int wpa_cli_cmd_remove_cred(struct wpa_ctrl *ctrl, int argc,
  1263. char *argv[])
  1264. {
  1265. int res = wpa_cli_cmd(ctrl, "REMOVE_CRED", 1, argc, argv);
  1266. if (interactive)
  1267. update_creds(ctrl);
  1268. return res;
  1269. }
  1270. static const char * const cred_fields[] = {
  1271. "temporary", "priority", "sp_priority", "pcsc", "eap",
  1272. "update_identifier", "min_dl_bandwidth_home", "min_ul_bandwidth_home",
  1273. "min_dl_bandwidth_roaming", "min_ul_bandwidth_roaming", "max_bss_load",
  1274. "req_conn_capab", "ocsp", "sim_num", "realm", "username", "password",
  1275. "ca_cert", "client_cert", "private_key", "private_key_passwd", "imsi",
  1276. "milenage", "domain_suffix_match", "domain", "phase1", "phase2",
  1277. "roaming_consortium", "required_roaming_consortium", "excluded_ssid",
  1278. "roaming_partner", "provisioning_sp"
  1279. };
  1280. static char ** wpa_cli_complete_cred(const char *str, int pos)
  1281. {
  1282. int arg = get_cmd_arg_num(str, pos);
  1283. int i, num_fields = ARRAY_SIZE(cred_fields);
  1284. char **res = NULL;
  1285. switch (arg) {
  1286. case 1:
  1287. res = cli_txt_list_array(&creds);
  1288. break;
  1289. case 2:
  1290. res = os_calloc(num_fields + 1, sizeof(char *));
  1291. if (res == NULL)
  1292. return NULL;
  1293. for (i = 0; i < num_fields; i++) {
  1294. res[i] = os_strdup(cred_fields[i]);
  1295. if (res[i] == NULL)
  1296. break;
  1297. }
  1298. }
  1299. return res;
  1300. }
  1301. static int wpa_cli_cmd_set_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1302. {
  1303. if (argc != 3) {
  1304. printf("Invalid SET_CRED command: needs three arguments\n"
  1305. "(cred id, variable name, and value)\n");
  1306. return -1;
  1307. }
  1308. return wpa_cli_cmd(ctrl, "SET_CRED", 3, argc, argv);
  1309. }
  1310. static int wpa_cli_cmd_get_cred(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1311. {
  1312. if (argc != 2) {
  1313. printf("Invalid GET_CRED command: needs two arguments\n"
  1314. "(cred id, variable name)\n");
  1315. return -1;
  1316. }
  1317. return wpa_cli_cmd(ctrl, "GET_CRED", 2, argc, argv);
  1318. }
  1319. static int wpa_cli_cmd_disconnect(struct wpa_ctrl *ctrl, int argc,
  1320. char *argv[])
  1321. {
  1322. return wpa_ctrl_command(ctrl, "DISCONNECT");
  1323. }
  1324. static int wpa_cli_cmd_reconnect(struct wpa_ctrl *ctrl, int argc,
  1325. char *argv[])
  1326. {
  1327. return wpa_ctrl_command(ctrl, "RECONNECT");
  1328. }
  1329. static int wpa_cli_cmd_save_config(struct wpa_ctrl *ctrl, int argc,
  1330. char *argv[])
  1331. {
  1332. return wpa_ctrl_command(ctrl, "SAVE_CONFIG");
  1333. }
  1334. static int wpa_cli_cmd_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1335. {
  1336. return wpa_cli_cmd(ctrl, "SCAN", 0, argc, argv);
  1337. }
  1338. static int wpa_cli_cmd_scan_results(struct wpa_ctrl *ctrl, int argc,
  1339. char *argv[])
  1340. {
  1341. return wpa_ctrl_command(ctrl, "SCAN_RESULTS");
  1342. }
  1343. static int wpa_cli_cmd_abort_scan(struct wpa_ctrl *ctrl, int argc,
  1344. char *argv[])
  1345. {
  1346. return wpa_ctrl_command(ctrl, "ABORT_SCAN");
  1347. }
  1348. static int wpa_cli_cmd_bss(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1349. {
  1350. return wpa_cli_cmd(ctrl, "BSS", 1, argc, argv);
  1351. }
  1352. static char ** wpa_cli_complete_bss(const char *str, int pos)
  1353. {
  1354. int arg = get_cmd_arg_num(str, pos);
  1355. char **res = NULL;
  1356. switch (arg) {
  1357. case 1:
  1358. res = cli_txt_list_array(&bsses);
  1359. break;
  1360. }
  1361. return res;
  1362. }
  1363. static int wpa_cli_cmd_get_capability(struct wpa_ctrl *ctrl, int argc,
  1364. char *argv[])
  1365. {
  1366. if (argc < 1 || argc > 2) {
  1367. printf("Invalid GET_CAPABILITY command: need either one or "
  1368. "two arguments\n");
  1369. return -1;
  1370. }
  1371. if ((argc == 2) && os_strcmp(argv[1], "strict") != 0) {
  1372. printf("Invalid GET_CAPABILITY command: second argument, "
  1373. "if any, must be 'strict'\n");
  1374. return -1;
  1375. }
  1376. return wpa_cli_cmd(ctrl, "GET_CAPABILITY", 1, argc, argv);
  1377. }
  1378. static char ** wpa_cli_complete_get_capability(const char *str, int pos)
  1379. {
  1380. int arg = get_cmd_arg_num(str, pos);
  1381. const char *fields[] = {
  1382. "eap", "pairwise", "group", "group_mgmt", "key_mgmt",
  1383. "proto", "auth_alg", "modes", "channels", "freq",
  1384. #ifdef CONFIG_TDLS
  1385. "tdls",
  1386. #endif /* CONFIG_TDLS */
  1387. #ifdef CONFIG_ERP
  1388. "erp",
  1389. #endif /* CONFIG_ERP */
  1390. #ifdef CONFIG_FIPS
  1391. "fips",
  1392. #endif /* CONFIG_FIPS */
  1393. #ifdef CONFIG_ACS
  1394. "acs",
  1395. #endif /* CONFIG_ACS */
  1396. };
  1397. int i, num_fields = ARRAY_SIZE(fields);
  1398. char **res = NULL;
  1399. if (arg == 1) {
  1400. res = os_calloc(num_fields + 1, sizeof(char *));
  1401. if (res == NULL)
  1402. return NULL;
  1403. for (i = 0; i < num_fields; i++) {
  1404. res[i] = os_strdup(fields[i]);
  1405. if (res[i] == NULL)
  1406. return res;
  1407. }
  1408. }
  1409. if (arg == 2) {
  1410. res = os_calloc(1 + 1, sizeof(char *));
  1411. if (res == NULL)
  1412. return NULL;
  1413. res[0] = os_strdup("strict");
  1414. }
  1415. return res;
  1416. }
  1417. static int wpa_cli_list_interfaces(struct wpa_ctrl *ctrl)
  1418. {
  1419. printf("Available interfaces:\n");
  1420. return wpa_ctrl_command(ctrl, "INTERFACES");
  1421. }
  1422. static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1423. {
  1424. if (argc < 1) {
  1425. wpa_cli_list_interfaces(ctrl);
  1426. return 0;
  1427. }
  1428. wpa_cli_close_connection();
  1429. os_free(ctrl_ifname);
  1430. ctrl_ifname = os_strdup(argv[0]);
  1431. if (!ctrl_ifname) {
  1432. printf("Failed to allocate memory\n");
  1433. return 0;
  1434. }
  1435. if (wpa_cli_open_connection(ctrl_ifname, 1) == 0) {
  1436. printf("Connected to interface '%s.\n", ctrl_ifname);
  1437. } else {
  1438. printf("Could not connect to interface '%s' - re-trying\n",
  1439. ctrl_ifname);
  1440. }
  1441. return 0;
  1442. }
  1443. static int wpa_cli_cmd_reconfigure(struct wpa_ctrl *ctrl, int argc,
  1444. char *argv[])
  1445. {
  1446. return wpa_ctrl_command(ctrl, "RECONFIGURE");
  1447. }
  1448. static int wpa_cli_cmd_terminate(struct wpa_ctrl *ctrl, int argc,
  1449. char *argv[])
  1450. {
  1451. return wpa_ctrl_command(ctrl, "TERMINATE");
  1452. }
  1453. static int wpa_cli_cmd_interface_add(struct wpa_ctrl *ctrl, int argc,
  1454. char *argv[])
  1455. {
  1456. char cmd[256];
  1457. int res;
  1458. if (argc < 1) {
  1459. printf("Invalid INTERFACE_ADD command: needs at least one "
  1460. "argument (interface name)\n"
  1461. "All arguments: ifname confname driver ctrl_interface "
  1462. "driver_param bridge_name [create]\n");
  1463. return -1;
  1464. }
  1465. /*
  1466. * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
  1467. * <driver_param>TAB<bridge_name>[TAB<create>[TAB<type>]]
  1468. */
  1469. res = os_snprintf(cmd, sizeof(cmd),
  1470. "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
  1471. argv[0],
  1472. argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "",
  1473. argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "",
  1474. argc > 5 ? argv[5] : "", argc > 6 ? argv[6] : "",
  1475. argc > 7 ? argv[7] : "");
  1476. if (os_snprintf_error(sizeof(cmd), res))
  1477. return -1;
  1478. cmd[sizeof(cmd) - 1] = '\0';
  1479. return wpa_ctrl_command(ctrl, cmd);
  1480. }
  1481. static int wpa_cli_cmd_interface_remove(struct wpa_ctrl *ctrl, int argc,
  1482. char *argv[])
  1483. {
  1484. return wpa_cli_cmd(ctrl, "INTERFACE_REMOVE", 1, argc, argv);
  1485. }
  1486. static int wpa_cli_cmd_interface_list(struct wpa_ctrl *ctrl, int argc,
  1487. char *argv[])
  1488. {
  1489. return wpa_ctrl_command(ctrl, "INTERFACE_LIST");
  1490. }
  1491. #ifdef CONFIG_AP
  1492. static int wpa_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1493. {
  1494. return wpa_cli_cmd(ctrl, "STA", 1, argc, argv);
  1495. }
  1496. static char ** wpa_cli_complete_sta(const char *str, int pos)
  1497. {
  1498. int arg = get_cmd_arg_num(str, pos);
  1499. char **res = NULL;
  1500. switch (arg) {
  1501. case 1:
  1502. res = cli_txt_list_array(&stations);
  1503. break;
  1504. }
  1505. return res;
  1506. }
  1507. static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, const char *cmd,
  1508. char *addr, size_t addr_len, int print)
  1509. {
  1510. char buf[4096], *pos;
  1511. size_t len;
  1512. int ret;
  1513. if (ctrl_conn == NULL) {
  1514. printf("Not connected to hostapd - command dropped.\n");
  1515. return -1;
  1516. }
  1517. if (ifname_prefix) {
  1518. os_snprintf(buf, sizeof(buf), "IFNAME=%s %s",
  1519. ifname_prefix, cmd);
  1520. buf[sizeof(buf) - 1] = '\0';
  1521. cmd = buf;
  1522. }
  1523. len = sizeof(buf) - 1;
  1524. ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
  1525. wpa_cli_msg_cb);
  1526. if (ret == -2) {
  1527. printf("'%s' command timed out.\n", cmd);
  1528. return -2;
  1529. } else if (ret < 0) {
  1530. printf("'%s' command failed.\n", cmd);
  1531. return -1;
  1532. }
  1533. buf[len] = '\0';
  1534. if (os_memcmp(buf, "FAIL", 4) == 0 ||
  1535. os_memcmp(buf, "UNKNOWN COMMAND", 15) == 0)
  1536. return -1;
  1537. if (print)
  1538. printf("%s", buf);
  1539. pos = buf;
  1540. while (*pos != '\0' && *pos != '\n')
  1541. pos++;
  1542. *pos = '\0';
  1543. os_strlcpy(addr, buf, addr_len);
  1544. return 0;
  1545. }
  1546. static int wpa_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1547. {
  1548. char addr[32], cmd[64];
  1549. if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 1))
  1550. return 0;
  1551. do {
  1552. os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
  1553. } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 1) == 0);
  1554. return -1;
  1555. }
  1556. static int wpa_cli_cmd_list_sta(struct wpa_ctrl *ctrl, int argc,
  1557. char *argv[])
  1558. {
  1559. char addr[32], cmd[64];
  1560. if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
  1561. return 0;
  1562. do {
  1563. if (os_strcmp(addr, "") != 0)
  1564. printf("%s\n", addr);
  1565. os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
  1566. } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
  1567. return 0;
  1568. }
  1569. static int wpa_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
  1570. char *argv[])
  1571. {
  1572. return wpa_cli_cmd(ctrl, "DEAUTHENTICATE", 1, argc, argv);
  1573. }
  1574. static char ** wpa_cli_complete_deauthenticate(const char *str, int pos)
  1575. {
  1576. int arg = get_cmd_arg_num(str, pos);
  1577. char **res = NULL;
  1578. switch (arg) {
  1579. case 1:
  1580. res = cli_txt_list_array(&stations);
  1581. break;
  1582. }
  1583. return res;
  1584. }
  1585. static int wpa_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
  1586. char *argv[])
  1587. {
  1588. return wpa_cli_cmd(ctrl, "DISASSOCIATE", 1, argc, argv);
  1589. }
  1590. static char ** wpa_cli_complete_disassociate(const char *str, int pos)
  1591. {
  1592. int arg = get_cmd_arg_num(str, pos);
  1593. char **res = NULL;
  1594. switch (arg) {
  1595. case 1:
  1596. res = cli_txt_list_array(&stations);
  1597. break;
  1598. }
  1599. return res;
  1600. }
  1601. static int wpa_cli_cmd_chanswitch(struct wpa_ctrl *ctrl, int argc,
  1602. char *argv[])
  1603. {
  1604. return wpa_cli_cmd(ctrl, "CHAN_SWITCH", 2, argc, argv);
  1605. }
  1606. #endif /* CONFIG_AP */
  1607. static int wpa_cli_cmd_suspend(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1608. {
  1609. return wpa_ctrl_command(ctrl, "SUSPEND");
  1610. }
  1611. static int wpa_cli_cmd_resume(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1612. {
  1613. return wpa_ctrl_command(ctrl, "RESUME");
  1614. }
  1615. #ifdef CONFIG_TESTING_OPTIONS
  1616. static int wpa_cli_cmd_drop_sa(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1617. {
  1618. return wpa_ctrl_command(ctrl, "DROP_SA");
  1619. }
  1620. #endif /* CONFIG_TESTING_OPTIONS */
  1621. static int wpa_cli_cmd_roam(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1622. {
  1623. return wpa_cli_cmd(ctrl, "ROAM", 1, argc, argv);
  1624. }
  1625. #ifdef CONFIG_MESH
  1626. static int wpa_cli_cmd_mesh_interface_add(struct wpa_ctrl *ctrl, int argc,
  1627. char *argv[])
  1628. {
  1629. return wpa_cli_cmd(ctrl, "MESH_INTERFACE_ADD", 0, argc, argv);
  1630. }
  1631. static int wpa_cli_cmd_mesh_group_add(struct wpa_ctrl *ctrl, int argc,
  1632. char *argv[])
  1633. {
  1634. return wpa_cli_cmd(ctrl, "MESH_GROUP_ADD", 1, argc, argv);
  1635. }
  1636. static int wpa_cli_cmd_mesh_group_remove(struct wpa_ctrl *ctrl, int argc,
  1637. char *argv[])
  1638. {
  1639. return wpa_cli_cmd(ctrl, "MESH_GROUP_REMOVE", 1, argc, argv);
  1640. }
  1641. static int wpa_cli_cmd_mesh_peer_remove(struct wpa_ctrl *ctrl, int argc,
  1642. char *argv[])
  1643. {
  1644. return wpa_cli_cmd(ctrl, "MESH_PEER_REMOVE", 1, argc, argv);
  1645. }
  1646. static int wpa_cli_cmd_mesh_peer_add(struct wpa_ctrl *ctrl, int argc,
  1647. char *argv[])
  1648. {
  1649. return wpa_cli_cmd(ctrl, "MESH_PEER_ADD", 1, argc, argv);
  1650. }
  1651. #endif /* CONFIG_MESH */
  1652. #ifdef CONFIG_P2P
  1653. static int wpa_cli_cmd_p2p_find(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1654. {
  1655. return wpa_cli_cmd(ctrl, "P2P_FIND", 0, argc, argv);
  1656. }
  1657. static char ** wpa_cli_complete_p2p_find(const char *str, int pos)
  1658. {
  1659. char **res = NULL;
  1660. int arg = get_cmd_arg_num(str, pos);
  1661. res = os_calloc(6, sizeof(char *));
  1662. if (res == NULL)
  1663. return NULL;
  1664. res[0] = os_strdup("type=social");
  1665. if (res[0] == NULL) {
  1666. os_free(res);
  1667. return NULL;
  1668. }
  1669. res[1] = os_strdup("type=progressive");
  1670. if (res[1] == NULL)
  1671. return res;
  1672. res[2] = os_strdup("delay=");
  1673. if (res[2] == NULL)
  1674. return res;
  1675. res[3] = os_strdup("dev_id=");
  1676. if (res[3] == NULL)
  1677. return res;
  1678. if (arg == 1)
  1679. res[4] = os_strdup("[timeout]");
  1680. return res;
  1681. }
  1682. static int wpa_cli_cmd_p2p_stop_find(struct wpa_ctrl *ctrl, int argc,
  1683. char *argv[])
  1684. {
  1685. return wpa_ctrl_command(ctrl, "P2P_STOP_FIND");
  1686. }
  1687. static int wpa_cli_cmd_p2p_asp_provision(struct wpa_ctrl *ctrl, int argc,
  1688. char *argv[])
  1689. {
  1690. return wpa_cli_cmd(ctrl, "P2P_ASP_PROVISION", 3, argc, argv);
  1691. }
  1692. static int wpa_cli_cmd_p2p_asp_provision_resp(struct wpa_ctrl *ctrl, int argc,
  1693. char *argv[])
  1694. {
  1695. return wpa_cli_cmd(ctrl, "P2P_ASP_PROVISION_RESP", 2, argc, argv);
  1696. }
  1697. static int wpa_cli_cmd_p2p_connect(struct wpa_ctrl *ctrl, int argc,
  1698. char *argv[])
  1699. {
  1700. return wpa_cli_cmd(ctrl, "P2P_CONNECT", 2, argc, argv);
  1701. }
  1702. static char ** wpa_cli_complete_p2p_connect(const char *str, int pos)
  1703. {
  1704. int arg = get_cmd_arg_num(str, pos);
  1705. char **res = NULL;
  1706. switch (arg) {
  1707. case 1:
  1708. res = cli_txt_list_array(&p2p_peers);
  1709. break;
  1710. }
  1711. return res;
  1712. }
  1713. static int wpa_cli_cmd_p2p_listen(struct wpa_ctrl *ctrl, int argc,
  1714. char *argv[])
  1715. {
  1716. return wpa_cli_cmd(ctrl, "P2P_LISTEN", 0, argc, argv);
  1717. }
  1718. static int wpa_cli_cmd_p2p_group_remove(struct wpa_ctrl *ctrl, int argc,
  1719. char *argv[])
  1720. {
  1721. return wpa_cli_cmd(ctrl, "P2P_GROUP_REMOVE", 1, argc, argv);
  1722. }
  1723. static char ** wpa_cli_complete_p2p_group_remove(const char *str, int pos)
  1724. {
  1725. int arg = get_cmd_arg_num(str, pos);
  1726. char **res = NULL;
  1727. switch (arg) {
  1728. case 1:
  1729. res = cli_txt_list_array(&p2p_groups);
  1730. break;
  1731. }
  1732. return res;
  1733. }
  1734. static int wpa_cli_cmd_p2p_group_add(struct wpa_ctrl *ctrl, int argc,
  1735. char *argv[])
  1736. {
  1737. return wpa_cli_cmd(ctrl, "P2P_GROUP_ADD", 0, argc, argv);
  1738. }
  1739. static int wpa_cli_cmd_p2p_group_member(struct wpa_ctrl *ctrl, int argc,
  1740. char *argv[])
  1741. {
  1742. return wpa_cli_cmd(ctrl, "P2P_GROUP_MEMBER", 1, argc, argv);
  1743. }
  1744. static int wpa_cli_cmd_p2p_prov_disc(struct wpa_ctrl *ctrl, int argc,
  1745. char *argv[])
  1746. {
  1747. if (argc != 2 && argc != 3) {
  1748. printf("Invalid P2P_PROV_DISC command: needs at least "
  1749. "two arguments, address and config method\n"
  1750. "(display, keypad, or pbc) and an optional join\n");
  1751. return -1;
  1752. }
  1753. return wpa_cli_cmd(ctrl, "P2P_PROV_DISC", 2, argc, argv);
  1754. }
  1755. static int wpa_cli_cmd_p2p_get_passphrase(struct wpa_ctrl *ctrl, int argc,
  1756. char *argv[])
  1757. {
  1758. return wpa_ctrl_command(ctrl, "P2P_GET_PASSPHRASE");
  1759. }
  1760. static int wpa_cli_cmd_p2p_serv_disc_req(struct wpa_ctrl *ctrl, int argc,
  1761. char *argv[])
  1762. {
  1763. char cmd[4096];
  1764. if (argc < 2) {
  1765. printf("Invalid P2P_SERV_DISC_REQ command: needs two "
  1766. "or more arguments (address and TLVs)\n");
  1767. return -1;
  1768. }
  1769. if (write_cmd(cmd, sizeof(cmd), "P2P_SERV_DISC_REQ", argc, argv) < 0)
  1770. return -1;
  1771. return wpa_ctrl_command(ctrl, cmd);
  1772. }
  1773. static int wpa_cli_cmd_p2p_serv_disc_cancel_req(struct wpa_ctrl *ctrl,
  1774. int argc, char *argv[])
  1775. {
  1776. return wpa_cli_cmd(ctrl, "P2P_SERV_DISC_CANCEL_REQ", 1, argc, argv);
  1777. }
  1778. static int wpa_cli_cmd_p2p_serv_disc_resp(struct wpa_ctrl *ctrl, int argc,
  1779. char *argv[])
  1780. {
  1781. char cmd[4096];
  1782. int res;
  1783. if (argc != 4) {
  1784. printf("Invalid P2P_SERV_DISC_RESP command: needs four "
  1785. "arguments (freq, address, dialog token, and TLVs)\n");
  1786. return -1;
  1787. }
  1788. res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_RESP %s %s %s %s",
  1789. argv[0], argv[1], argv[2], argv[3]);
  1790. if (os_snprintf_error(sizeof(cmd), res))
  1791. return -1;
  1792. cmd[sizeof(cmd) - 1] = '\0';
  1793. return wpa_ctrl_command(ctrl, cmd);
  1794. }
  1795. static int wpa_cli_cmd_p2p_service_update(struct wpa_ctrl *ctrl, int argc,
  1796. char *argv[])
  1797. {
  1798. return wpa_ctrl_command(ctrl, "P2P_SERVICE_UPDATE");
  1799. }
  1800. static int wpa_cli_cmd_p2p_serv_disc_external(struct wpa_ctrl *ctrl,
  1801. int argc, char *argv[])
  1802. {
  1803. return wpa_cli_cmd(ctrl, "P2P_SERV_DISC_EXTERNAL", 1, argc, argv);
  1804. }
  1805. static int wpa_cli_cmd_p2p_service_flush(struct wpa_ctrl *ctrl, int argc,
  1806. char *argv[])
  1807. {
  1808. return wpa_ctrl_command(ctrl, "P2P_SERVICE_FLUSH");
  1809. }
  1810. static int wpa_cli_cmd_p2p_service_add(struct wpa_ctrl *ctrl, int argc,
  1811. char *argv[])
  1812. {
  1813. if (argc < 3) {
  1814. printf("Invalid P2P_SERVICE_ADD command: needs 3-6 arguments\n");
  1815. return -1;
  1816. }
  1817. return wpa_cli_cmd(ctrl, "P2P_SERVICE_ADD", 3, argc, argv);
  1818. }
  1819. static int wpa_cli_cmd_p2p_service_rep(struct wpa_ctrl *ctrl, int argc,
  1820. char *argv[])
  1821. {
  1822. if (argc < 5 || argc > 6) {
  1823. printf("Invalid P2P_SERVICE_REP command: needs 5-6 "
  1824. "arguments\n");
  1825. return -1;
  1826. }
  1827. return wpa_cli_cmd(ctrl, "P2P_SERVICE_REP", 5, argc, argv);
  1828. }
  1829. static int wpa_cli_cmd_p2p_service_del(struct wpa_ctrl *ctrl, int argc,
  1830. char *argv[])
  1831. {
  1832. char cmd[4096];
  1833. int res;
  1834. if (argc != 2 && argc != 3) {
  1835. printf("Invalid P2P_SERVICE_DEL command: needs two or three "
  1836. "arguments\n");
  1837. return -1;
  1838. }
  1839. if (argc == 3)
  1840. res = os_snprintf(cmd, sizeof(cmd),
  1841. "P2P_SERVICE_DEL %s %s %s",
  1842. argv[0], argv[1], argv[2]);
  1843. else
  1844. res = os_snprintf(cmd, sizeof(cmd),
  1845. "P2P_SERVICE_DEL %s %s",
  1846. argv[0], argv[1]);
  1847. if (os_snprintf_error(sizeof(cmd), res))
  1848. return -1;
  1849. cmd[sizeof(cmd) - 1] = '\0';
  1850. return wpa_ctrl_command(ctrl, cmd);
  1851. }
  1852. static int wpa_cli_cmd_p2p_reject(struct wpa_ctrl *ctrl,
  1853. int argc, char *argv[])
  1854. {
  1855. return wpa_cli_cmd(ctrl, "P2P_REJECT", 1, argc, argv);
  1856. }
  1857. static int wpa_cli_cmd_p2p_invite(struct wpa_ctrl *ctrl,
  1858. int argc, char *argv[])
  1859. {
  1860. return wpa_cli_cmd(ctrl, "P2P_INVITE", 1, argc, argv);
  1861. }
  1862. static int wpa_cli_cmd_p2p_peer(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1863. {
  1864. return wpa_cli_cmd(ctrl, "P2P_PEER", 1, argc, argv);
  1865. }
  1866. static char ** wpa_cli_complete_p2p_peer(const char *str, int pos)
  1867. {
  1868. int arg = get_cmd_arg_num(str, pos);
  1869. char **res = NULL;
  1870. switch (arg) {
  1871. case 1:
  1872. res = cli_txt_list_array(&p2p_peers);
  1873. break;
  1874. }
  1875. return res;
  1876. }
  1877. static int wpa_ctrl_command_p2p_peer(struct wpa_ctrl *ctrl, const char *cmd,
  1878. char *addr, size_t addr_len,
  1879. int discovered)
  1880. {
  1881. char buf[4096], *pos;
  1882. size_t len;
  1883. int ret;
  1884. if (ctrl_conn == NULL)
  1885. return -1;
  1886. len = sizeof(buf) - 1;
  1887. ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
  1888. wpa_cli_msg_cb);
  1889. if (ret == -2) {
  1890. printf("'%s' command timed out.\n", cmd);
  1891. return -2;
  1892. } else if (ret < 0) {
  1893. printf("'%s' command failed.\n", cmd);
  1894. return -1;
  1895. }
  1896. buf[len] = '\0';
  1897. if (os_memcmp(buf, "FAIL", 4) == 0)
  1898. return -1;
  1899. pos = buf;
  1900. while (*pos != '\0' && *pos != '\n')
  1901. pos++;
  1902. *pos++ = '\0';
  1903. os_strlcpy(addr, buf, addr_len);
  1904. if (!discovered || os_strstr(pos, "[PROBE_REQ_ONLY]") == NULL)
  1905. printf("%s\n", addr);
  1906. return 0;
  1907. }
  1908. static int wpa_cli_cmd_p2p_peers(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1909. {
  1910. char addr[32], cmd[64];
  1911. int discovered;
  1912. discovered = argc > 0 && os_strcmp(argv[0], "discovered") == 0;
  1913. if (wpa_ctrl_command_p2p_peer(ctrl, "P2P_PEER FIRST",
  1914. addr, sizeof(addr), discovered))
  1915. return -1;
  1916. do {
  1917. os_snprintf(cmd, sizeof(cmd), "P2P_PEER NEXT-%s", addr);
  1918. } while (wpa_ctrl_command_p2p_peer(ctrl, cmd, addr, sizeof(addr),
  1919. discovered) == 0);
  1920. return 0;
  1921. }
  1922. static int wpa_cli_cmd_p2p_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1923. {
  1924. return wpa_cli_cmd(ctrl, "P2P_SET", 2, argc, argv);
  1925. }
  1926. static char ** wpa_cli_complete_p2p_set(const char *str, int pos)
  1927. {
  1928. int arg = get_cmd_arg_num(str, pos);
  1929. const char *fields[] = {
  1930. "discoverability",
  1931. "managed",
  1932. "listen_channel",
  1933. "ssid_postfix",
  1934. "noa",
  1935. "ps",
  1936. "oppps",
  1937. "ctwindow",
  1938. "disabled",
  1939. "conc_pref",
  1940. "force_long_sd",
  1941. "peer_filter",
  1942. "cross_connect",
  1943. "go_apsd",
  1944. "client_apsd",
  1945. "disallow_freq",
  1946. "disc_int",
  1947. "per_sta_psk",
  1948. };
  1949. int i, num_fields = ARRAY_SIZE(fields);
  1950. if (arg == 1) {
  1951. char **res = os_calloc(num_fields + 1, sizeof(char *));
  1952. if (res == NULL)
  1953. return NULL;
  1954. for (i = 0; i < num_fields; i++) {
  1955. res[i] = os_strdup(fields[i]);
  1956. if (res[i] == NULL)
  1957. return res;
  1958. }
  1959. return res;
  1960. }
  1961. if (arg == 2 && os_strncasecmp(str, "p2p_set peer_filter ", 20) == 0)
  1962. return cli_txt_list_array(&p2p_peers);
  1963. return NULL;
  1964. }
  1965. static int wpa_cli_cmd_p2p_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1966. {
  1967. return wpa_ctrl_command(ctrl, "P2P_FLUSH");
  1968. }
  1969. static int wpa_cli_cmd_p2p_cancel(struct wpa_ctrl *ctrl, int argc,
  1970. char *argv[])
  1971. {
  1972. return wpa_ctrl_command(ctrl, "P2P_CANCEL");
  1973. }
  1974. static int wpa_cli_cmd_p2p_unauthorize(struct wpa_ctrl *ctrl, int argc,
  1975. char *argv[])
  1976. {
  1977. return wpa_cli_cmd(ctrl, "P2P_UNAUTHORIZE", 1, argc, argv);
  1978. }
  1979. static int wpa_cli_cmd_p2p_presence_req(struct wpa_ctrl *ctrl, int argc,
  1980. char *argv[])
  1981. {
  1982. if (argc != 0 && argc != 2 && argc != 4) {
  1983. printf("Invalid P2P_PRESENCE_REQ command: needs two arguments "
  1984. "(preferred duration, interval; in microsecods).\n"
  1985. "Optional second pair can be used to provide "
  1986. "acceptable values.\n");
  1987. return -1;
  1988. }
  1989. return wpa_cli_cmd(ctrl, "P2P_PRESENCE_REQ", 0, argc, argv);
  1990. }
  1991. static int wpa_cli_cmd_p2p_ext_listen(struct wpa_ctrl *ctrl, int argc,
  1992. char *argv[])
  1993. {
  1994. if (argc != 0 && argc != 2) {
  1995. printf("Invalid P2P_EXT_LISTEN command: needs two arguments "
  1996. "(availability period, availability interval; in "
  1997. "millisecods).\n"
  1998. "Extended Listen Timing can be cancelled with this "
  1999. "command when used without parameters.\n");
  2000. return -1;
  2001. }
  2002. return wpa_cli_cmd(ctrl, "P2P_EXT_LISTEN", 0, argc, argv);
  2003. }
  2004. static int wpa_cli_cmd_p2p_remove_client(struct wpa_ctrl *ctrl, int argc,
  2005. char *argv[])
  2006. {
  2007. return wpa_cli_cmd(ctrl, "P2P_REMOVE_CLIENT", 1, argc, argv);
  2008. }
  2009. #endif /* CONFIG_P2P */
  2010. static int wpa_cli_cmd_vendor_elem_add(struct wpa_ctrl *ctrl, int argc,
  2011. char *argv[])
  2012. {
  2013. return wpa_cli_cmd(ctrl, "VENDOR_ELEM_ADD", 2, argc, argv);
  2014. }
  2015. static int wpa_cli_cmd_vendor_elem_get(struct wpa_ctrl *ctrl, int argc,
  2016. char *argv[])
  2017. {
  2018. return wpa_cli_cmd(ctrl, "VENDOR_ELEM_GET", 1, argc, argv);
  2019. }
  2020. static int wpa_cli_cmd_vendor_elem_remove(struct wpa_ctrl *ctrl, int argc,
  2021. char *argv[])
  2022. {
  2023. return wpa_cli_cmd(ctrl, "VENDOR_ELEM_REMOVE", 2, argc, argv);
  2024. }
  2025. #ifdef CONFIG_WIFI_DISPLAY
  2026. static int wpa_cli_cmd_wfd_subelem_set(struct wpa_ctrl *ctrl, int argc,
  2027. char *argv[])
  2028. {
  2029. char cmd[100];
  2030. int res;
  2031. if (argc != 1 && argc != 2) {
  2032. printf("Invalid WFD_SUBELEM_SET command: needs one or two "
  2033. "arguments (subelem, hexdump)\n");
  2034. return -1;
  2035. }
  2036. res = os_snprintf(cmd, sizeof(cmd), "WFD_SUBELEM_SET %s %s",
  2037. argv[0], argc > 1 ? argv[1] : "");
  2038. if (os_snprintf_error(sizeof(cmd), res))
  2039. return -1;
  2040. cmd[sizeof(cmd) - 1] = '\0';
  2041. return wpa_ctrl_command(ctrl, cmd);
  2042. }
  2043. static int wpa_cli_cmd_wfd_subelem_get(struct wpa_ctrl *ctrl, int argc,
  2044. char *argv[])
  2045. {
  2046. char cmd[100];
  2047. int res;
  2048. if (argc != 1) {
  2049. printf("Invalid WFD_SUBELEM_GET command: needs one "
  2050. "argument (subelem)\n");
  2051. return -1;
  2052. }
  2053. res = os_snprintf(cmd, sizeof(cmd), "WFD_SUBELEM_GET %s",
  2054. argv[0]);
  2055. if (os_snprintf_error(sizeof(cmd), res))
  2056. return -1;
  2057. cmd[sizeof(cmd) - 1] = '\0';
  2058. return wpa_ctrl_command(ctrl, cmd);
  2059. }
  2060. #endif /* CONFIG_WIFI_DISPLAY */
  2061. #ifdef CONFIG_INTERWORKING
  2062. static int wpa_cli_cmd_fetch_anqp(struct wpa_ctrl *ctrl, int argc,
  2063. char *argv[])
  2064. {
  2065. return wpa_ctrl_command(ctrl, "FETCH_ANQP");
  2066. }
  2067. static int wpa_cli_cmd_stop_fetch_anqp(struct wpa_ctrl *ctrl, int argc,
  2068. char *argv[])
  2069. {
  2070. return wpa_ctrl_command(ctrl, "STOP_FETCH_ANQP");
  2071. }
  2072. static int wpa_cli_cmd_interworking_select(struct wpa_ctrl *ctrl, int argc,
  2073. char *argv[])
  2074. {
  2075. return wpa_cli_cmd(ctrl, "INTERWORKING_SELECT", 0, argc, argv);
  2076. }
  2077. static int wpa_cli_cmd_interworking_connect(struct wpa_ctrl *ctrl, int argc,
  2078. char *argv[])
  2079. {
  2080. return wpa_cli_cmd(ctrl, "INTERWORKING_CONNECT", 1, argc, argv);
  2081. }
  2082. static int wpa_cli_cmd_interworking_add_network(struct wpa_ctrl *ctrl, int argc,
  2083. char *argv[])
  2084. {
  2085. return wpa_cli_cmd(ctrl, "INTERWORKING_ADD_NETWORK", 1, argc, argv);
  2086. }
  2087. static int wpa_cli_cmd_anqp_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2088. {
  2089. return wpa_cli_cmd(ctrl, "ANQP_GET", 2, argc, argv);
  2090. }
  2091. static int wpa_cli_cmd_gas_request(struct wpa_ctrl *ctrl, int argc,
  2092. char *argv[])
  2093. {
  2094. return wpa_cli_cmd(ctrl, "GAS_REQUEST", 2, argc, argv);
  2095. }
  2096. static int wpa_cli_cmd_gas_response_get(struct wpa_ctrl *ctrl, int argc,
  2097. char *argv[])
  2098. {
  2099. return wpa_cli_cmd(ctrl, "GAS_RESPONSE_GET", 2, argc, argv);
  2100. }
  2101. #endif /* CONFIG_INTERWORKING */
  2102. #ifdef CONFIG_HS20
  2103. static int wpa_cli_cmd_hs20_anqp_get(struct wpa_ctrl *ctrl, int argc,
  2104. char *argv[])
  2105. {
  2106. return wpa_cli_cmd(ctrl, "HS20_ANQP_GET", 2, argc, argv);
  2107. }
  2108. static int wpa_cli_cmd_get_nai_home_realm_list(struct wpa_ctrl *ctrl, int argc,
  2109. char *argv[])
  2110. {
  2111. char cmd[512];
  2112. if (argc == 0) {
  2113. printf("Command needs one or two arguments (dst mac addr and "
  2114. "optional home realm)\n");
  2115. return -1;
  2116. }
  2117. if (write_cmd(cmd, sizeof(cmd), "HS20_GET_NAI_HOME_REALM_LIST",
  2118. argc, argv) < 0)
  2119. return -1;
  2120. return wpa_ctrl_command(ctrl, cmd);
  2121. }
  2122. static int wpa_cli_cmd_hs20_icon_request(struct wpa_ctrl *ctrl, int argc,
  2123. char *argv[])
  2124. {
  2125. char cmd[512];
  2126. if (argc < 2) {
  2127. printf("Command needs two arguments (dst mac addr and "
  2128. "icon name)\n");
  2129. return -1;
  2130. }
  2131. if (write_cmd(cmd, sizeof(cmd), "HS20_ICON_REQUEST", argc, argv) < 0)
  2132. return -1;
  2133. return wpa_ctrl_command(ctrl, cmd);
  2134. }
  2135. static int wpa_cli_cmd_fetch_osu(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2136. {
  2137. return wpa_ctrl_command(ctrl, "FETCH_OSU");
  2138. }
  2139. static int wpa_cli_cmd_cancel_fetch_osu(struct wpa_ctrl *ctrl, int argc,
  2140. char *argv[])
  2141. {
  2142. return wpa_ctrl_command(ctrl, "CANCEL_FETCH_OSU");
  2143. }
  2144. #endif /* CONFIG_HS20 */
  2145. static int wpa_cli_cmd_sta_autoconnect(struct wpa_ctrl *ctrl, int argc,
  2146. char *argv[])
  2147. {
  2148. return wpa_cli_cmd(ctrl, "STA_AUTOCONNECT", 1, argc, argv);
  2149. }
  2150. static int wpa_cli_cmd_tdls_discover(struct wpa_ctrl *ctrl, int argc,
  2151. char *argv[])
  2152. {
  2153. return wpa_cli_cmd(ctrl, "TDLS_DISCOVER", 1, argc, argv);
  2154. }
  2155. static int wpa_cli_cmd_tdls_setup(struct wpa_ctrl *ctrl, int argc,
  2156. char *argv[])
  2157. {
  2158. return wpa_cli_cmd(ctrl, "TDLS_SETUP", 1, argc, argv);
  2159. }
  2160. static int wpa_cli_cmd_tdls_teardown(struct wpa_ctrl *ctrl, int argc,
  2161. char *argv[])
  2162. {
  2163. return wpa_cli_cmd(ctrl, "TDLS_TEARDOWN", 1, argc, argv);
  2164. }
  2165. static int wpa_cli_cmd_tdls_link_status(struct wpa_ctrl *ctrl, int argc,
  2166. char *argv[])
  2167. {
  2168. return wpa_cli_cmd(ctrl, "TDLS_LINK_STATUS", 1, argc, argv);
  2169. }
  2170. static int wpa_cli_cmd_wmm_ac_addts(struct wpa_ctrl *ctrl, int argc,
  2171. char *argv[])
  2172. {
  2173. return wpa_cli_cmd(ctrl, "WMM_AC_ADDTS", 3, argc, argv);
  2174. }
  2175. static int wpa_cli_cmd_wmm_ac_delts(struct wpa_ctrl *ctrl, int argc,
  2176. char *argv[])
  2177. {
  2178. return wpa_cli_cmd(ctrl, "WMM_AC_DELTS", 1, argc, argv);
  2179. }
  2180. static int wpa_cli_cmd_wmm_ac_status(struct wpa_ctrl *ctrl, int argc,
  2181. char *argv[])
  2182. {
  2183. return wpa_ctrl_command(ctrl, "WMM_AC_STATUS");
  2184. }
  2185. static int wpa_cli_cmd_tdls_chan_switch(struct wpa_ctrl *ctrl, int argc,
  2186. char *argv[])
  2187. {
  2188. return wpa_cli_cmd(ctrl, "TDLS_CHAN_SWITCH", 2, argc, argv);
  2189. }
  2190. static int wpa_cli_cmd_tdls_cancel_chan_switch(struct wpa_ctrl *ctrl, int argc,
  2191. char *argv[])
  2192. {
  2193. return wpa_cli_cmd(ctrl, "TDLS_CANCEL_CHAN_SWITCH", 1, argc, argv);
  2194. }
  2195. static int wpa_cli_cmd_signal_poll(struct wpa_ctrl *ctrl, int argc,
  2196. char *argv[])
  2197. {
  2198. return wpa_ctrl_command(ctrl, "SIGNAL_POLL");
  2199. }
  2200. static int wpa_cli_cmd_signal_monitor(struct wpa_ctrl *ctrl, int argc,
  2201. char *argv[])
  2202. {
  2203. return wpa_cli_cmd(ctrl, "SIGNAL_MONITOR", 0, argc, argv);
  2204. }
  2205. static int wpa_cli_cmd_pktcnt_poll(struct wpa_ctrl *ctrl, int argc,
  2206. char *argv[])
  2207. {
  2208. return wpa_ctrl_command(ctrl, "PKTCNT_POLL");
  2209. }
  2210. static int wpa_cli_cmd_reauthenticate(struct wpa_ctrl *ctrl, int argc,
  2211. char *argv[])
  2212. {
  2213. return wpa_ctrl_command(ctrl, "REAUTHENTICATE");
  2214. }
  2215. #ifdef CONFIG_AUTOSCAN
  2216. static int wpa_cli_cmd_autoscan(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2217. {
  2218. if (argc == 0)
  2219. return wpa_ctrl_command(ctrl, "AUTOSCAN ");
  2220. return wpa_cli_cmd(ctrl, "AUTOSCAN", 0, argc, argv);
  2221. }
  2222. #endif /* CONFIG_AUTOSCAN */
  2223. #ifdef CONFIG_WNM
  2224. static int wpa_cli_cmd_wnm_sleep(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2225. {
  2226. return wpa_cli_cmd(ctrl, "WNM_SLEEP", 0, argc, argv);
  2227. }
  2228. static int wpa_cli_cmd_wnm_bss_query(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2229. {
  2230. return wpa_cli_cmd(ctrl, "WNM_BSS_QUERY", 1, argc, argv);
  2231. }
  2232. #endif /* CONFIG_WNM */
  2233. static int wpa_cli_cmd_raw(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2234. {
  2235. if (argc == 0)
  2236. return -1;
  2237. return wpa_cli_cmd(ctrl, argv[0], 0, argc - 1, &argv[1]);
  2238. }
  2239. #ifdef ANDROID
  2240. static int wpa_cli_cmd_driver(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2241. {
  2242. return wpa_cli_cmd(ctrl, "DRIVER", 1, argc, argv);
  2243. }
  2244. #endif /* ANDROID */
  2245. static int wpa_cli_cmd_vendor(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2246. {
  2247. return wpa_cli_cmd(ctrl, "VENDOR", 1, argc, argv);
  2248. }
  2249. static int wpa_cli_cmd_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2250. {
  2251. return wpa_ctrl_command(ctrl, "FLUSH");
  2252. }
  2253. static int wpa_cli_cmd_radio_work(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2254. {
  2255. return wpa_cli_cmd(ctrl, "RADIO_WORK", 1, argc, argv);
  2256. }
  2257. static int wpa_cli_cmd_neighbor_rep_request(struct wpa_ctrl *ctrl, int argc,
  2258. char *argv[])
  2259. {
  2260. return wpa_cli_cmd(ctrl, "NEIGHBOR_REP_REQUEST", 0, argc, argv);
  2261. }
  2262. static int wpa_cli_cmd_erp_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2263. {
  2264. return wpa_ctrl_command(ctrl, "ERP_FLUSH");
  2265. }
  2266. static int wpa_cli_cmd_mac_rand_scan(struct wpa_ctrl *ctrl, int argc,
  2267. char *argv[])
  2268. {
  2269. return wpa_cli_cmd(ctrl, "MAC_RAND_SCAN", 1, argc, argv);
  2270. }
  2271. static int wpa_cli_cmd_get_pref_freq_list(struct wpa_ctrl *ctrl, int argc,
  2272. char *argv[])
  2273. {
  2274. return wpa_cli_cmd(ctrl, "GET_PREF_FREQ_LIST", 1, argc, argv);
  2275. }
  2276. static int wpa_cli_cmd_p2p_lo_start(struct wpa_ctrl *ctrl, int argc,
  2277. char *argv[])
  2278. {
  2279. return wpa_cli_cmd(ctrl, "P2P_LO_START", 4, argc, argv);
  2280. }
  2281. static int wpa_cli_cmd_p2p_lo_stop(struct wpa_ctrl *ctrl, int argc,
  2282. char *argv[])
  2283. {
  2284. return wpa_cli_cmd(ctrl, "P2P_LO_STOP", 0, argc, argv);
  2285. }
  2286. #ifdef CONFIG_DPP
  2287. static int wpa_cli_cmd_dpp_qr_code(struct wpa_ctrl *ctrl, int argc,
  2288. char *argv[])
  2289. {
  2290. return wpa_cli_cmd(ctrl, "DPP_QR_CODE", 1, argc, argv);
  2291. }
  2292. static int wpa_cli_cmd_dpp_bootstrap_gen(struct wpa_ctrl *ctrl, int argc,
  2293. char *argv[])
  2294. {
  2295. return wpa_cli_cmd(ctrl, "DPP_BOOTSTRAP_GEN", 1, argc, argv);
  2296. }
  2297. static int wpa_cli_cmd_dpp_bootstrap_remove(struct wpa_ctrl *ctrl, int argc,
  2298. char *argv[])
  2299. {
  2300. return wpa_cli_cmd(ctrl, "DPP_BOOTSTRAP_REMOVE", 1, argc, argv);
  2301. }
  2302. static int wpa_cli_cmd_dpp_bootstrap_get_uri(struct wpa_ctrl *ctrl, int argc,
  2303. char *argv[])
  2304. {
  2305. return wpa_cli_cmd(ctrl, "DPP_BOOTSTRAP_GET_URI", 1, argc, argv);
  2306. }
  2307. static int wpa_cli_cmd_dpp_bootstrap_info(struct wpa_ctrl *ctrl, int argc,
  2308. char *argv[])
  2309. {
  2310. return wpa_cli_cmd(ctrl, "DPP_BOOTSTRAP_INFO", 1, argc, argv);
  2311. }
  2312. static int wpa_cli_cmd_dpp_auth_init(struct wpa_ctrl *ctrl, int argc,
  2313. char *argv[])
  2314. {
  2315. return wpa_cli_cmd(ctrl, "DPP_AUTH_INIT", 1, argc, argv);
  2316. }
  2317. static int wpa_cli_cmd_dpp_listen(struct wpa_ctrl *ctrl, int argc,
  2318. char *argv[])
  2319. {
  2320. return wpa_cli_cmd(ctrl, "DPP_LISTEN", 1, argc, argv);
  2321. }
  2322. static int wpa_cli_cmd_dpp_stop_listen(struct wpa_ctrl *ctrl, int argc,
  2323. char *argv[])
  2324. {
  2325. return wpa_ctrl_command(ctrl, "DPP_STOP_LISTEN");
  2326. }
  2327. static int wpa_cli_cmd_dpp_configurator_add(struct wpa_ctrl *ctrl, int argc,
  2328. char *argv[])
  2329. {
  2330. return wpa_cli_cmd(ctrl, "DPP_CONFIGURATOR_ADD", 0, argc, argv);
  2331. }
  2332. static int wpa_cli_cmd_dpp_configurator_remove(struct wpa_ctrl *ctrl, int argc,
  2333. char *argv[])
  2334. {
  2335. return wpa_cli_cmd(ctrl, "DPP_CONFIGURATOR_REMOVE", 1, argc, argv);
  2336. }
  2337. static int wpa_cli_cmd_dpp_configurator_get_key(struct wpa_ctrl *ctrl, int argc,
  2338. char *argv[])
  2339. {
  2340. return wpa_cli_cmd(ctrl, "DPP_CONFIGURATOR_GET_KEY", 1, argc, argv);
  2341. }
  2342. static int wpa_cli_cmd_dpp_pkex_add(struct wpa_ctrl *ctrl, int argc,
  2343. char *argv[])
  2344. {
  2345. return wpa_cli_cmd(ctrl, "DPP_PKEX_ADD", 1, argc, argv);
  2346. }
  2347. static int wpa_cli_cmd_dpp_pkex_remove(struct wpa_ctrl *ctrl, int argc,
  2348. char *argv[])
  2349. {
  2350. return wpa_cli_cmd(ctrl, "DPP_PKEX_REMOVE", 1, argc, argv);
  2351. }
  2352. #endif /* CONFIG_DPP */
  2353. enum wpa_cli_cmd_flags {
  2354. cli_cmd_flag_none = 0x00,
  2355. cli_cmd_flag_sensitive = 0x01
  2356. };
  2357. struct wpa_cli_cmd {
  2358. const char *cmd;
  2359. int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
  2360. char ** (*completion)(const char *str, int pos);
  2361. enum wpa_cli_cmd_flags flags;
  2362. const char *usage;
  2363. };
  2364. static const struct wpa_cli_cmd wpa_cli_commands[] = {
  2365. { "status", wpa_cli_cmd_status, NULL,
  2366. cli_cmd_flag_none,
  2367. "[verbose] = get current WPA/EAPOL/EAP status" },
  2368. { "ifname", wpa_cli_cmd_ifname, NULL,
  2369. cli_cmd_flag_none,
  2370. "= get current interface name" },
  2371. { "ping", wpa_cli_cmd_ping, NULL,
  2372. cli_cmd_flag_none,
  2373. "= pings wpa_supplicant" },
  2374. { "relog", wpa_cli_cmd_relog, NULL,
  2375. cli_cmd_flag_none,
  2376. "= re-open log-file (allow rolling logs)" },
  2377. { "note", wpa_cli_cmd_note, NULL,
  2378. cli_cmd_flag_none,
  2379. "<text> = add a note to wpa_supplicant debug log" },
  2380. { "mib", wpa_cli_cmd_mib, NULL,
  2381. cli_cmd_flag_none,
  2382. "= get MIB variables (dot1x, dot11)" },
  2383. { "help", wpa_cli_cmd_help, wpa_cli_complete_help,
  2384. cli_cmd_flag_none,
  2385. "[command] = show usage help" },
  2386. { "interface", wpa_cli_cmd_interface, NULL,
  2387. cli_cmd_flag_none,
  2388. "[ifname] = show interfaces/select interface" },
  2389. { "level", wpa_cli_cmd_level, NULL,
  2390. cli_cmd_flag_none,
  2391. "<debug level> = change debug level" },
  2392. { "license", wpa_cli_cmd_license, NULL,
  2393. cli_cmd_flag_none,
  2394. "= show full wpa_cli license" },
  2395. { "quit", wpa_cli_cmd_quit, NULL,
  2396. cli_cmd_flag_none,
  2397. "= exit wpa_cli" },
  2398. { "set", wpa_cli_cmd_set, wpa_cli_complete_set,
  2399. cli_cmd_flag_none,
  2400. "= set variables (shows list of variables when run without "
  2401. "arguments)" },
  2402. { "dump", wpa_cli_cmd_dump, NULL,
  2403. cli_cmd_flag_none,
  2404. "= dump config variables" },
  2405. { "get", wpa_cli_cmd_get, wpa_cli_complete_get,
  2406. cli_cmd_flag_none,
  2407. "<name> = get information" },
  2408. { "driver_flags", wpa_cli_cmd_driver_flags, NULL,
  2409. cli_cmd_flag_none,
  2410. "= list driver flags" },
  2411. { "logon", wpa_cli_cmd_logon, NULL,
  2412. cli_cmd_flag_none,
  2413. "= IEEE 802.1X EAPOL state machine logon" },
  2414. { "logoff", wpa_cli_cmd_logoff, NULL,
  2415. cli_cmd_flag_none,
  2416. "= IEEE 802.1X EAPOL state machine logoff" },
  2417. { "pmksa", wpa_cli_cmd_pmksa, NULL,
  2418. cli_cmd_flag_none,
  2419. "= show PMKSA cache" },
  2420. { "pmksa_flush", wpa_cli_cmd_pmksa_flush, NULL,
  2421. cli_cmd_flag_none,
  2422. "= flush PMKSA cache entries" },
  2423. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  2424. { "pmksa_get", wpa_cli_cmd_pmksa_get, NULL,
  2425. cli_cmd_flag_none,
  2426. "<network_id> = fetch all stored PMKSA cache entries" },
  2427. { "pmksa_add", wpa_cli_cmd_pmksa_add, NULL,
  2428. cli_cmd_flag_sensitive,
  2429. "<network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds> <expiration in seconds> <akmp> <opportunistic> = store PMKSA cache entry from external storage" },
  2430. #ifdef CONFIG_MESH
  2431. { "mesh_pmksa_get", wpa_cli_mesh_cmd_pmksa_get, NULL,
  2432. cli_cmd_flag_none,
  2433. "<peer MAC address | any> = fetch all stored mesh PMKSA cache entries" },
  2434. { "mesh_pmksa_add", wpa_cli_mesh_cmd_pmksa_add, NULL,
  2435. cli_cmd_flag_sensitive,
  2436. "<BSSID> <PMKID> <PMK> <expiration in seconds> = store mesh PMKSA cache entry from external storage" },
  2437. #endif /* CONFIG_MESH */
  2438. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  2439. { "reassociate", wpa_cli_cmd_reassociate, NULL,
  2440. cli_cmd_flag_none,
  2441. "= force reassociation" },
  2442. { "reattach", wpa_cli_cmd_reattach, NULL,
  2443. cli_cmd_flag_none,
  2444. "= force reassociation back to the same BSS" },
  2445. { "preauthenticate", wpa_cli_cmd_preauthenticate, wpa_cli_complete_bss,
  2446. cli_cmd_flag_none,
  2447. "<BSSID> = force preauthentication" },
  2448. { "identity", wpa_cli_cmd_identity, wpa_cli_complete_network_id,
  2449. cli_cmd_flag_none,
  2450. "<network id> <identity> = configure identity for an SSID" },
  2451. { "password", wpa_cli_cmd_password, wpa_cli_complete_network_id,
  2452. cli_cmd_flag_sensitive,
  2453. "<network id> <password> = configure password for an SSID" },
  2454. { "new_password", wpa_cli_cmd_new_password,
  2455. wpa_cli_complete_network_id, cli_cmd_flag_sensitive,
  2456. "<network id> <password> = change password for an SSID" },
  2457. { "pin", wpa_cli_cmd_pin, wpa_cli_complete_network_id,
  2458. cli_cmd_flag_sensitive,
  2459. "<network id> <pin> = configure pin for an SSID" },
  2460. { "otp", wpa_cli_cmd_otp, wpa_cli_complete_network_id,
  2461. cli_cmd_flag_sensitive,
  2462. "<network id> <password> = configure one-time-password for an SSID"
  2463. },
  2464. { "passphrase", wpa_cli_cmd_passphrase, wpa_cli_complete_network_id,
  2465. cli_cmd_flag_sensitive,
  2466. "<network id> <passphrase> = configure private key passphrase\n"
  2467. " for an SSID" },
  2468. { "sim", wpa_cli_cmd_sim, wpa_cli_complete_network_id,
  2469. cli_cmd_flag_sensitive,
  2470. "<network id> <pin> = report SIM operation result" },
  2471. { "bssid", wpa_cli_cmd_bssid, wpa_cli_complete_network_id,
  2472. cli_cmd_flag_none,
  2473. "<network id> <BSSID> = set preferred BSSID for an SSID" },
  2474. { "blacklist", wpa_cli_cmd_blacklist, wpa_cli_complete_bss,
  2475. cli_cmd_flag_none,
  2476. "<BSSID> = add a BSSID to the blacklist\n"
  2477. "blacklist clear = clear the blacklist\n"
  2478. "blacklist = display the blacklist" },
  2479. { "log_level", wpa_cli_cmd_log_level, NULL,
  2480. cli_cmd_flag_none,
  2481. "<level> [<timestamp>] = update the log level/timestamp\n"
  2482. "log_level = display the current log level and log options" },
  2483. { "list_networks", wpa_cli_cmd_list_networks, NULL,
  2484. cli_cmd_flag_none,
  2485. "= list configured networks" },
  2486. { "select_network", wpa_cli_cmd_select_network,
  2487. wpa_cli_complete_network_id,
  2488. cli_cmd_flag_none,
  2489. "<network id> = select a network (disable others)" },
  2490. { "enable_network", wpa_cli_cmd_enable_network,
  2491. wpa_cli_complete_network_id,
  2492. cli_cmd_flag_none,
  2493. "<network id> = enable a network" },
  2494. { "disable_network", wpa_cli_cmd_disable_network,
  2495. wpa_cli_complete_network_id,
  2496. cli_cmd_flag_none,
  2497. "<network id> = disable a network" },
  2498. { "add_network", wpa_cli_cmd_add_network, NULL,
  2499. cli_cmd_flag_none,
  2500. "= add a network" },
  2501. { "remove_network", wpa_cli_cmd_remove_network,
  2502. wpa_cli_complete_network_id,
  2503. cli_cmd_flag_none,
  2504. "<network id> = remove a network" },
  2505. { "set_network", wpa_cli_cmd_set_network, wpa_cli_complete_network,
  2506. cli_cmd_flag_sensitive,
  2507. "<network id> <variable> <value> = set network variables (shows\n"
  2508. " list of variables when run without arguments)" },
  2509. { "get_network", wpa_cli_cmd_get_network, wpa_cli_complete_network,
  2510. cli_cmd_flag_none,
  2511. "<network id> <variable> = get network variables" },
  2512. { "dup_network", wpa_cli_cmd_dup_network, wpa_cli_complete_dup_network,
  2513. cli_cmd_flag_none,
  2514. "<src network id> <dst network id> <variable> = duplicate network variables"
  2515. },
  2516. { "list_creds", wpa_cli_cmd_list_creds, NULL,
  2517. cli_cmd_flag_none,
  2518. "= list configured credentials" },
  2519. { "add_cred", wpa_cli_cmd_add_cred, NULL,
  2520. cli_cmd_flag_none,
  2521. "= add a credential" },
  2522. { "remove_cred", wpa_cli_cmd_remove_cred, NULL,
  2523. cli_cmd_flag_none,
  2524. "<cred id> = remove a credential" },
  2525. { "set_cred", wpa_cli_cmd_set_cred, wpa_cli_complete_cred,
  2526. cli_cmd_flag_sensitive,
  2527. "<cred id> <variable> <value> = set credential variables" },
  2528. { "get_cred", wpa_cli_cmd_get_cred, wpa_cli_complete_cred,
  2529. cli_cmd_flag_none,
  2530. "<cred id> <variable> = get credential variables" },
  2531. { "save_config", wpa_cli_cmd_save_config, NULL,
  2532. cli_cmd_flag_none,
  2533. "= save the current configuration" },
  2534. { "disconnect", wpa_cli_cmd_disconnect, NULL,
  2535. cli_cmd_flag_none,
  2536. "= disconnect and wait for reassociate/reconnect command before\n"
  2537. " connecting" },
  2538. { "reconnect", wpa_cli_cmd_reconnect, NULL,
  2539. cli_cmd_flag_none,
  2540. "= like reassociate, but only takes effect if already disconnected"
  2541. },
  2542. { "scan", wpa_cli_cmd_scan, NULL,
  2543. cli_cmd_flag_none,
  2544. "= request new BSS scan" },
  2545. { "scan_results", wpa_cli_cmd_scan_results, NULL,
  2546. cli_cmd_flag_none,
  2547. "= get latest scan results" },
  2548. { "abort_scan", wpa_cli_cmd_abort_scan, NULL,
  2549. cli_cmd_flag_none,
  2550. "= request ongoing scan to be aborted" },
  2551. { "bss", wpa_cli_cmd_bss, wpa_cli_complete_bss,
  2552. cli_cmd_flag_none,
  2553. "<<idx> | <bssid>> = get detailed scan result info" },
  2554. { "get_capability", wpa_cli_cmd_get_capability,
  2555. wpa_cli_complete_get_capability, cli_cmd_flag_none,
  2556. "<eap/pairwise/group/key_mgmt/proto/auth_alg/channels/freq/modes> "
  2557. "= get capabilities" },
  2558. { "reconfigure", wpa_cli_cmd_reconfigure, NULL,
  2559. cli_cmd_flag_none,
  2560. "= force wpa_supplicant to re-read its configuration file" },
  2561. { "terminate", wpa_cli_cmd_terminate, NULL,
  2562. cli_cmd_flag_none,
  2563. "= terminate wpa_supplicant" },
  2564. { "interface_add", wpa_cli_cmd_interface_add, NULL,
  2565. cli_cmd_flag_none,
  2566. "<ifname> <confname> <driver> <ctrl_interface> <driver_param>\n"
  2567. " <bridge_name> <create> <type> = adds new interface, all "
  2568. "parameters but\n"
  2569. " <ifname> are optional. Supported types are station ('sta') and "
  2570. "AP ('ap')" },
  2571. { "interface_remove", wpa_cli_cmd_interface_remove, NULL,
  2572. cli_cmd_flag_none,
  2573. "<ifname> = removes the interface" },
  2574. { "interface_list", wpa_cli_cmd_interface_list, NULL,
  2575. cli_cmd_flag_none,
  2576. "= list available interfaces" },
  2577. { "ap_scan", wpa_cli_cmd_ap_scan, NULL,
  2578. cli_cmd_flag_none,
  2579. "<value> = set ap_scan parameter" },
  2580. { "scan_interval", wpa_cli_cmd_scan_interval, NULL,
  2581. cli_cmd_flag_none,
  2582. "<value> = set scan_interval parameter (in seconds)" },
  2583. { "bss_expire_age", wpa_cli_cmd_bss_expire_age, NULL,
  2584. cli_cmd_flag_none,
  2585. "<value> = set BSS expiration age parameter" },
  2586. { "bss_expire_count", wpa_cli_cmd_bss_expire_count, NULL,
  2587. cli_cmd_flag_none,
  2588. "<value> = set BSS expiration scan count parameter" },
  2589. { "bss_flush", wpa_cli_cmd_bss_flush, NULL,
  2590. cli_cmd_flag_none,
  2591. "<value> = set BSS flush age (0 by default)" },
  2592. { "ft_ds", wpa_cli_cmd_ft_ds, wpa_cli_complete_bss,
  2593. cli_cmd_flag_none,
  2594. "<addr> = request over-the-DS FT with <addr>" },
  2595. { "wps_pbc", wpa_cli_cmd_wps_pbc, wpa_cli_complete_bss,
  2596. cli_cmd_flag_none,
  2597. "[BSSID] = start Wi-Fi Protected Setup: Push Button Configuration" },
  2598. { "wps_pin", wpa_cli_cmd_wps_pin, wpa_cli_complete_bss,
  2599. cli_cmd_flag_sensitive,
  2600. "<BSSID> [PIN] = start WPS PIN method (returns PIN, if not "
  2601. "hardcoded)" },
  2602. { "wps_check_pin", wpa_cli_cmd_wps_check_pin, NULL,
  2603. cli_cmd_flag_sensitive,
  2604. "<PIN> = verify PIN checksum" },
  2605. { "wps_cancel", wpa_cli_cmd_wps_cancel, NULL, cli_cmd_flag_none,
  2606. "Cancels the pending WPS operation" },
  2607. #ifdef CONFIG_WPS_NFC
  2608. { "wps_nfc", wpa_cli_cmd_wps_nfc, wpa_cli_complete_bss,
  2609. cli_cmd_flag_none,
  2610. "[BSSID] = start Wi-Fi Protected Setup: NFC" },
  2611. { "wps_nfc_config_token", wpa_cli_cmd_wps_nfc_config_token, NULL,
  2612. cli_cmd_flag_none,
  2613. "<WPS|NDEF> = build configuration token" },
  2614. { "wps_nfc_token", wpa_cli_cmd_wps_nfc_token, NULL,
  2615. cli_cmd_flag_none,
  2616. "<WPS|NDEF> = create password token" },
  2617. { "wps_nfc_tag_read", wpa_cli_cmd_wps_nfc_tag_read, NULL,
  2618. cli_cmd_flag_sensitive,
  2619. "<hexdump of payload> = report read NFC tag with WPS data" },
  2620. { "nfc_get_handover_req", wpa_cli_cmd_nfc_get_handover_req, NULL,
  2621. cli_cmd_flag_none,
  2622. "<NDEF> <WPS> = create NFC handover request" },
  2623. { "nfc_get_handover_sel", wpa_cli_cmd_nfc_get_handover_sel, NULL,
  2624. cli_cmd_flag_none,
  2625. "<NDEF> <WPS> = create NFC handover select" },
  2626. { "nfc_report_handover", wpa_cli_cmd_nfc_report_handover, NULL,
  2627. cli_cmd_flag_none,
  2628. "<role> <type> <hexdump of req> <hexdump of sel> = report completed "
  2629. "NFC handover" },
  2630. #endif /* CONFIG_WPS_NFC */
  2631. { "wps_reg", wpa_cli_cmd_wps_reg, wpa_cli_complete_bss,
  2632. cli_cmd_flag_sensitive,
  2633. "<BSSID> <AP PIN> = start WPS Registrar to configure an AP" },
  2634. { "wps_ap_pin", wpa_cli_cmd_wps_ap_pin, NULL,
  2635. cli_cmd_flag_sensitive,
  2636. "[params..] = enable/disable AP PIN" },
  2637. { "wps_er_start", wpa_cli_cmd_wps_er_start, NULL,
  2638. cli_cmd_flag_none,
  2639. "[IP address] = start Wi-Fi Protected Setup External Registrar" },
  2640. { "wps_er_stop", wpa_cli_cmd_wps_er_stop, NULL,
  2641. cli_cmd_flag_none,
  2642. "= stop Wi-Fi Protected Setup External Registrar" },
  2643. { "wps_er_pin", wpa_cli_cmd_wps_er_pin, NULL,
  2644. cli_cmd_flag_sensitive,
  2645. "<UUID> <PIN> = add an Enrollee PIN to External Registrar" },
  2646. { "wps_er_pbc", wpa_cli_cmd_wps_er_pbc, NULL,
  2647. cli_cmd_flag_none,
  2648. "<UUID> = accept an Enrollee PBC using External Registrar" },
  2649. { "wps_er_learn", wpa_cli_cmd_wps_er_learn, NULL,
  2650. cli_cmd_flag_sensitive,
  2651. "<UUID> <PIN> = learn AP configuration" },
  2652. { "wps_er_set_config", wpa_cli_cmd_wps_er_set_config, NULL,
  2653. cli_cmd_flag_none,
  2654. "<UUID> <network id> = set AP configuration for enrolling" },
  2655. { "wps_er_config", wpa_cli_cmd_wps_er_config, NULL,
  2656. cli_cmd_flag_sensitive,
  2657. "<UUID> <PIN> <SSID> <auth> <encr> <key> = configure AP" },
  2658. #ifdef CONFIG_WPS_NFC
  2659. { "wps_er_nfc_config_token", wpa_cli_cmd_wps_er_nfc_config_token, NULL,
  2660. cli_cmd_flag_none,
  2661. "<WPS/NDEF> <UUID> = build NFC configuration token" },
  2662. #endif /* CONFIG_WPS_NFC */
  2663. { "ibss_rsn", wpa_cli_cmd_ibss_rsn, NULL,
  2664. cli_cmd_flag_none,
  2665. "<addr> = request RSN authentication with <addr> in IBSS" },
  2666. #ifdef CONFIG_AP
  2667. { "sta", wpa_cli_cmd_sta, wpa_cli_complete_sta,
  2668. cli_cmd_flag_none,
  2669. "<addr> = get information about an associated station (AP)" },
  2670. { "all_sta", wpa_cli_cmd_all_sta, NULL,
  2671. cli_cmd_flag_none,
  2672. "= get information about all associated stations (AP)" },
  2673. { "list_sta", wpa_cli_cmd_list_sta, NULL,
  2674. cli_cmd_flag_none,
  2675. "= list all stations (AP)" },
  2676. { "deauthenticate", wpa_cli_cmd_deauthenticate,
  2677. wpa_cli_complete_deauthenticate, cli_cmd_flag_none,
  2678. "<addr> = deauthenticate a station" },
  2679. { "disassociate", wpa_cli_cmd_disassociate,
  2680. wpa_cli_complete_disassociate, cli_cmd_flag_none,
  2681. "<addr> = disassociate a station" },
  2682. { "chan_switch", wpa_cli_cmd_chanswitch, NULL,
  2683. cli_cmd_flag_none,
  2684. "<cs_count> <freq> [sec_channel_offset=] [center_freq1=]"
  2685. " [center_freq2=] [bandwidth=] [blocktx] [ht|vht]"
  2686. " = CSA parameters" },
  2687. #endif /* CONFIG_AP */
  2688. { "suspend", wpa_cli_cmd_suspend, NULL, cli_cmd_flag_none,
  2689. "= notification of suspend/hibernate" },
  2690. { "resume", wpa_cli_cmd_resume, NULL, cli_cmd_flag_none,
  2691. "= notification of resume/thaw" },
  2692. #ifdef CONFIG_TESTING_OPTIONS
  2693. { "drop_sa", wpa_cli_cmd_drop_sa, NULL, cli_cmd_flag_none,
  2694. "= drop SA without deauth/disassoc (test command)" },
  2695. #endif /* CONFIG_TESTING_OPTIONS */
  2696. { "roam", wpa_cli_cmd_roam, wpa_cli_complete_bss,
  2697. cli_cmd_flag_none,
  2698. "<addr> = roam to the specified BSS" },
  2699. #ifdef CONFIG_MESH
  2700. { "mesh_interface_add", wpa_cli_cmd_mesh_interface_add, NULL,
  2701. cli_cmd_flag_none,
  2702. "[ifname] = Create a new mesh interface" },
  2703. { "mesh_group_add", wpa_cli_cmd_mesh_group_add, NULL,
  2704. cli_cmd_flag_none,
  2705. "<network id> = join a mesh network (disable others)" },
  2706. { "mesh_group_remove", wpa_cli_cmd_mesh_group_remove, NULL,
  2707. cli_cmd_flag_none,
  2708. "<ifname> = Remove mesh group interface" },
  2709. { "mesh_peer_remove", wpa_cli_cmd_mesh_peer_remove, NULL,
  2710. cli_cmd_flag_none,
  2711. "<addr> = Remove a mesh peer" },
  2712. { "mesh_peer_add", wpa_cli_cmd_mesh_peer_add, NULL,
  2713. cli_cmd_flag_none,
  2714. "<addr> [duration=<seconds>] = Add a mesh peer" },
  2715. #endif /* CONFIG_MESH */
  2716. #ifdef CONFIG_P2P
  2717. { "p2p_find", wpa_cli_cmd_p2p_find, wpa_cli_complete_p2p_find,
  2718. cli_cmd_flag_none,
  2719. "[timeout] [type=*] = find P2P Devices for up-to timeout seconds" },
  2720. { "p2p_stop_find", wpa_cli_cmd_p2p_stop_find, NULL, cli_cmd_flag_none,
  2721. "= stop P2P Devices search" },
  2722. { "p2p_asp_provision", wpa_cli_cmd_p2p_asp_provision, NULL,
  2723. cli_cmd_flag_none,
  2724. "<addr> adv_id=<adv_id> conncap=<conncap> [info=<infodata>] = provision with a P2P ASP Device" },
  2725. { "p2p_asp_provision_resp", wpa_cli_cmd_p2p_asp_provision_resp, NULL,
  2726. cli_cmd_flag_none,
  2727. "<addr> adv_id=<adv_id> [role<conncap>] [info=<infodata>] = provision with a P2P ASP Device" },
  2728. { "p2p_connect", wpa_cli_cmd_p2p_connect, wpa_cli_complete_p2p_connect,
  2729. cli_cmd_flag_none,
  2730. "<addr> <\"pbc\"|PIN> [ht40] = connect to a P2P Device" },
  2731. { "p2p_listen", wpa_cli_cmd_p2p_listen, NULL, cli_cmd_flag_none,
  2732. "[timeout] = listen for P2P Devices for up-to timeout seconds" },
  2733. { "p2p_group_remove", wpa_cli_cmd_p2p_group_remove,
  2734. wpa_cli_complete_p2p_group_remove, cli_cmd_flag_none,
  2735. "<ifname> = remove P2P group interface (terminate group if GO)" },
  2736. { "p2p_group_add", wpa_cli_cmd_p2p_group_add, NULL, cli_cmd_flag_none,
  2737. "[ht40] = add a new P2P group (local end as GO)" },
  2738. { "p2p_group_member", wpa_cli_cmd_p2p_group_member, NULL,
  2739. cli_cmd_flag_none,
  2740. "<dev_addr> = Get peer interface address on local GO using peer Device Address" },
  2741. { "p2p_prov_disc", wpa_cli_cmd_p2p_prov_disc,
  2742. wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
  2743. "<addr> <method> = request provisioning discovery" },
  2744. { "p2p_get_passphrase", wpa_cli_cmd_p2p_get_passphrase, NULL,
  2745. cli_cmd_flag_none,
  2746. "= get the passphrase for a group (GO only)" },
  2747. { "p2p_serv_disc_req", wpa_cli_cmd_p2p_serv_disc_req,
  2748. wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
  2749. "<addr> <TLVs> = schedule service discovery request" },
  2750. { "p2p_serv_disc_cancel_req", wpa_cli_cmd_p2p_serv_disc_cancel_req,
  2751. NULL, cli_cmd_flag_none,
  2752. "<id> = cancel pending service discovery request" },
  2753. { "p2p_serv_disc_resp", wpa_cli_cmd_p2p_serv_disc_resp, NULL,
  2754. cli_cmd_flag_none,
  2755. "<freq> <addr> <dialog token> <TLVs> = service discovery response" },
  2756. { "p2p_service_update", wpa_cli_cmd_p2p_service_update, NULL,
  2757. cli_cmd_flag_none,
  2758. "= indicate change in local services" },
  2759. { "p2p_serv_disc_external", wpa_cli_cmd_p2p_serv_disc_external, NULL,
  2760. cli_cmd_flag_none,
  2761. "<external> = set external processing of service discovery" },
  2762. { "p2p_service_flush", wpa_cli_cmd_p2p_service_flush, NULL,
  2763. cli_cmd_flag_none,
  2764. "= remove all stored service entries" },
  2765. { "p2p_service_add", wpa_cli_cmd_p2p_service_add, NULL,
  2766. cli_cmd_flag_none,
  2767. "<bonjour|upnp|asp> <query|version> <response|service> = add a local "
  2768. "service" },
  2769. { "p2p_service_rep", wpa_cli_cmd_p2p_service_rep, NULL,
  2770. cli_cmd_flag_none,
  2771. "asp <auto> <adv_id> <svc_state> <svc_string> [<svc_info>] = replace "
  2772. "local ASP service" },
  2773. { "p2p_service_del", wpa_cli_cmd_p2p_service_del, NULL,
  2774. cli_cmd_flag_none,
  2775. "<bonjour|upnp> <query|version> [|service] = remove a local "
  2776. "service" },
  2777. { "p2p_reject", wpa_cli_cmd_p2p_reject, wpa_cli_complete_p2p_peer,
  2778. cli_cmd_flag_none,
  2779. "<addr> = reject connection attempts from a specific peer" },
  2780. { "p2p_invite", wpa_cli_cmd_p2p_invite, NULL,
  2781. cli_cmd_flag_none,
  2782. "<cmd> [peer=addr] = invite peer" },
  2783. { "p2p_peers", wpa_cli_cmd_p2p_peers, NULL, cli_cmd_flag_none,
  2784. "[discovered] = list known (optionally, only fully discovered) P2P "
  2785. "peers" },
  2786. { "p2p_peer", wpa_cli_cmd_p2p_peer, wpa_cli_complete_p2p_peer,
  2787. cli_cmd_flag_none,
  2788. "<address> = show information about known P2P peer" },
  2789. { "p2p_set", wpa_cli_cmd_p2p_set, wpa_cli_complete_p2p_set,
  2790. cli_cmd_flag_none,
  2791. "<field> <value> = set a P2P parameter" },
  2792. { "p2p_flush", wpa_cli_cmd_p2p_flush, NULL, cli_cmd_flag_none,
  2793. "= flush P2P state" },
  2794. { "p2p_cancel", wpa_cli_cmd_p2p_cancel, NULL, cli_cmd_flag_none,
  2795. "= cancel P2P group formation" },
  2796. { "p2p_unauthorize", wpa_cli_cmd_p2p_unauthorize,
  2797. wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
  2798. "<address> = unauthorize a peer" },
  2799. { "p2p_presence_req", wpa_cli_cmd_p2p_presence_req, NULL,
  2800. cli_cmd_flag_none,
  2801. "[<duration> <interval>] [<duration> <interval>] = request GO "
  2802. "presence" },
  2803. { "p2p_ext_listen", wpa_cli_cmd_p2p_ext_listen, NULL,
  2804. cli_cmd_flag_none,
  2805. "[<period> <interval>] = set extended listen timing" },
  2806. { "p2p_remove_client", wpa_cli_cmd_p2p_remove_client,
  2807. wpa_cli_complete_p2p_peer, cli_cmd_flag_none,
  2808. "<address|iface=address> = remove a peer from all groups" },
  2809. #endif /* CONFIG_P2P */
  2810. { "vendor_elem_add", wpa_cli_cmd_vendor_elem_add, NULL,
  2811. cli_cmd_flag_none,
  2812. "<frame id> <hexdump of elem(s)> = add vendor specific IEs to frame(s)\n"
  2813. VENDOR_ELEM_FRAME_ID },
  2814. { "vendor_elem_get", wpa_cli_cmd_vendor_elem_get, NULL,
  2815. cli_cmd_flag_none,
  2816. "<frame id> = get vendor specific IE(s) to frame(s)\n"
  2817. VENDOR_ELEM_FRAME_ID },
  2818. { "vendor_elem_remove", wpa_cli_cmd_vendor_elem_remove, NULL,
  2819. cli_cmd_flag_none,
  2820. "<frame id> <hexdump of elem(s)> = remove vendor specific IE(s) in frame(s)\n"
  2821. VENDOR_ELEM_FRAME_ID },
  2822. #ifdef CONFIG_WIFI_DISPLAY
  2823. { "wfd_subelem_set", wpa_cli_cmd_wfd_subelem_set, NULL,
  2824. cli_cmd_flag_none,
  2825. "<subelem> [contents] = set Wi-Fi Display subelement" },
  2826. { "wfd_subelem_get", wpa_cli_cmd_wfd_subelem_get, NULL,
  2827. cli_cmd_flag_none,
  2828. "<subelem> = get Wi-Fi Display subelement" },
  2829. #endif /* CONFIG_WIFI_DISPLAY */
  2830. #ifdef CONFIG_INTERWORKING
  2831. { "fetch_anqp", wpa_cli_cmd_fetch_anqp, NULL, cli_cmd_flag_none,
  2832. "= fetch ANQP information for all APs" },
  2833. { "stop_fetch_anqp", wpa_cli_cmd_stop_fetch_anqp, NULL,
  2834. cli_cmd_flag_none,
  2835. "= stop fetch_anqp operation" },
  2836. { "interworking_select", wpa_cli_cmd_interworking_select, NULL,
  2837. cli_cmd_flag_none,
  2838. "[auto] = perform Interworking network selection" },
  2839. { "interworking_connect", wpa_cli_cmd_interworking_connect,
  2840. wpa_cli_complete_bss, cli_cmd_flag_none,
  2841. "<BSSID> = connect using Interworking credentials" },
  2842. { "interworking_add_network", wpa_cli_cmd_interworking_add_network,
  2843. wpa_cli_complete_bss, cli_cmd_flag_none,
  2844. "<BSSID> = connect using Interworking credentials" },
  2845. { "anqp_get", wpa_cli_cmd_anqp_get, wpa_cli_complete_bss,
  2846. cli_cmd_flag_none,
  2847. "<addr> <info id>[,<info id>]... = request ANQP information" },
  2848. { "gas_request", wpa_cli_cmd_gas_request, wpa_cli_complete_bss,
  2849. cli_cmd_flag_none,
  2850. "<addr> <AdvProtoID> [QueryReq] = GAS request" },
  2851. { "gas_response_get", wpa_cli_cmd_gas_response_get,
  2852. wpa_cli_complete_bss, cli_cmd_flag_none,
  2853. "<addr> <dialog token> [start,len] = Fetch last GAS response" },
  2854. #endif /* CONFIG_INTERWORKING */
  2855. #ifdef CONFIG_HS20
  2856. { "hs20_anqp_get", wpa_cli_cmd_hs20_anqp_get, wpa_cli_complete_bss,
  2857. cli_cmd_flag_none,
  2858. "<addr> <subtype>[,<subtype>]... = request HS 2.0 ANQP information"
  2859. },
  2860. { "nai_home_realm_list", wpa_cli_cmd_get_nai_home_realm_list,
  2861. wpa_cli_complete_bss, cli_cmd_flag_none,
  2862. "<addr> <home realm> = get HS20 nai home realm list" },
  2863. { "hs20_icon_request", wpa_cli_cmd_hs20_icon_request,
  2864. wpa_cli_complete_bss, cli_cmd_flag_none,
  2865. "<addr> <icon name> = get Hotspot 2.0 OSU icon" },
  2866. { "fetch_osu", wpa_cli_cmd_fetch_osu, NULL, cli_cmd_flag_none,
  2867. "= fetch OSU provider information from all APs" },
  2868. { "cancel_fetch_osu", wpa_cli_cmd_cancel_fetch_osu, NULL,
  2869. cli_cmd_flag_none,
  2870. "= cancel fetch_osu command" },
  2871. #endif /* CONFIG_HS20 */
  2872. { "sta_autoconnect", wpa_cli_cmd_sta_autoconnect, NULL,
  2873. cli_cmd_flag_none,
  2874. "<0/1> = disable/enable automatic reconnection" },
  2875. { "tdls_discover", wpa_cli_cmd_tdls_discover, NULL,
  2876. cli_cmd_flag_none,
  2877. "<addr> = request TDLS discovery with <addr>" },
  2878. { "tdls_setup", wpa_cli_cmd_tdls_setup, NULL,
  2879. cli_cmd_flag_none,
  2880. "<addr> = request TDLS setup with <addr>" },
  2881. { "tdls_teardown", wpa_cli_cmd_tdls_teardown, NULL,
  2882. cli_cmd_flag_none,
  2883. "<addr> = tear down TDLS with <addr>" },
  2884. { "tdls_link_status", wpa_cli_cmd_tdls_link_status, NULL,
  2885. cli_cmd_flag_none,
  2886. "<addr> = TDLS link status with <addr>" },
  2887. { "wmm_ac_addts", wpa_cli_cmd_wmm_ac_addts, NULL,
  2888. cli_cmd_flag_none,
  2889. "<uplink/downlink/bidi> <tsid=0..7> <up=0..7> [nominal_msdu_size=#] "
  2890. "[mean_data_rate=#] [min_phy_rate=#] [sba=#] [fixed_nominal_msdu] "
  2891. "= add WMM-AC traffic stream" },
  2892. { "wmm_ac_delts", wpa_cli_cmd_wmm_ac_delts, NULL,
  2893. cli_cmd_flag_none,
  2894. "<tsid> = delete WMM-AC traffic stream" },
  2895. { "wmm_ac_status", wpa_cli_cmd_wmm_ac_status, NULL,
  2896. cli_cmd_flag_none,
  2897. "= show status for Wireless Multi-Media Admission-Control" },
  2898. { "tdls_chan_switch", wpa_cli_cmd_tdls_chan_switch, NULL,
  2899. cli_cmd_flag_none,
  2900. "<addr> <oper class> <freq> [sec_channel_offset=] [center_freq1=] "
  2901. "[center_freq2=] [bandwidth=] [ht|vht] = enable channel switching "
  2902. "with TDLS peer" },
  2903. { "tdls_cancel_chan_switch", wpa_cli_cmd_tdls_cancel_chan_switch, NULL,
  2904. cli_cmd_flag_none,
  2905. "<addr> = disable channel switching with TDLS peer <addr>" },
  2906. { "signal_poll", wpa_cli_cmd_signal_poll, NULL,
  2907. cli_cmd_flag_none,
  2908. "= get signal parameters" },
  2909. { "signal_monitor", wpa_cli_cmd_signal_monitor, NULL,
  2910. cli_cmd_flag_none,
  2911. "= set signal monitor parameters" },
  2912. { "pktcnt_poll", wpa_cli_cmd_pktcnt_poll, NULL,
  2913. cli_cmd_flag_none,
  2914. "= get TX/RX packet counters" },
  2915. { "reauthenticate", wpa_cli_cmd_reauthenticate, NULL,
  2916. cli_cmd_flag_none,
  2917. "= trigger IEEE 802.1X/EAPOL reauthentication" },
  2918. #ifdef CONFIG_AUTOSCAN
  2919. { "autoscan", wpa_cli_cmd_autoscan, NULL, cli_cmd_flag_none,
  2920. "[params] = Set or unset (if none) autoscan parameters" },
  2921. #endif /* CONFIG_AUTOSCAN */
  2922. #ifdef CONFIG_WNM
  2923. { "wnm_sleep", wpa_cli_cmd_wnm_sleep, NULL, cli_cmd_flag_none,
  2924. "<enter/exit> [interval=#] = enter/exit WNM-Sleep mode" },
  2925. { "wnm_bss_query", wpa_cli_cmd_wnm_bss_query, NULL, cli_cmd_flag_none,
  2926. "<query reason> [list]"
  2927. " [neighbor=<BSSID>,<BSSID information>,<operating class>,<channel number>,<PHY type>[,<hexdump of optional subelements>]"
  2928. " = Send BSS Transition Management Query" },
  2929. #endif /* CONFIG_WNM */
  2930. { "raw", wpa_cli_cmd_raw, NULL, cli_cmd_flag_sensitive,
  2931. "<params..> = Sent unprocessed command" },
  2932. { "flush", wpa_cli_cmd_flush, NULL, cli_cmd_flag_none,
  2933. "= flush wpa_supplicant state" },
  2934. #ifdef ANDROID
  2935. { "driver", wpa_cli_cmd_driver, NULL, cli_cmd_flag_none,
  2936. "<command> = driver private commands" },
  2937. #endif /* ANDROID */
  2938. { "radio_work", wpa_cli_cmd_radio_work, NULL, cli_cmd_flag_none,
  2939. "= radio_work <show/add/done>" },
  2940. { "vendor", wpa_cli_cmd_vendor, NULL, cli_cmd_flag_none,
  2941. "<vendor id> <command id> [<hex formatted command argument>] = Send vendor command"
  2942. },
  2943. { "neighbor_rep_request",
  2944. wpa_cli_cmd_neighbor_rep_request, NULL, cli_cmd_flag_none,
  2945. "[ssid=<SSID>] [lci] [civic] = Trigger request to AP for neighboring AP report (with optional given SSID in hex or enclosed in double quotes, default: current SSID; with optional LCI and location civic request)"
  2946. },
  2947. { "erp_flush", wpa_cli_cmd_erp_flush, NULL, cli_cmd_flag_none,
  2948. "= flush ERP keys" },
  2949. { "mac_rand_scan",
  2950. wpa_cli_cmd_mac_rand_scan, NULL, cli_cmd_flag_none,
  2951. "<scan|sched|pno|all> enable=<0/1> [addr=mac-address "
  2952. "mask=mac-address-mask] = scan MAC randomization"
  2953. },
  2954. { "get_pref_freq_list", wpa_cli_cmd_get_pref_freq_list, NULL,
  2955. cli_cmd_flag_none,
  2956. "<interface type> = retrieve preferred freq list for the specified interface type" },
  2957. { "p2p_lo_start", wpa_cli_cmd_p2p_lo_start, NULL,
  2958. cli_cmd_flag_none,
  2959. "<freq> <period> <interval> <count> = start P2P listen offload" },
  2960. { "p2p_lo_stop", wpa_cli_cmd_p2p_lo_stop, NULL,
  2961. cli_cmd_flag_none,
  2962. "= stop P2P listen offload" },
  2963. #ifdef CONFIG_DPP
  2964. { "dpp_qr_code", wpa_cli_cmd_dpp_qr_code, NULL, cli_cmd_flag_none,
  2965. "report a scanned DPP URI from a QR Code" },
  2966. { "dpp_bootstrap_gen", wpa_cli_cmd_dpp_bootstrap_gen, NULL,
  2967. cli_cmd_flag_sensitive,
  2968. "type=<qrcode> [chan=..] [mac=..] [info=..] [curve=..] [key=..] = generate DPP bootstrap information" },
  2969. { "dpp_bootstrap_remove", wpa_cli_cmd_dpp_bootstrap_remove, NULL,
  2970. cli_cmd_flag_none,
  2971. "*|<id> = remove DPP bootstrap information" },
  2972. { "dpp_bootstrap_get_uri", wpa_cli_cmd_dpp_bootstrap_get_uri, NULL,
  2973. cli_cmd_flag_none,
  2974. "<id> = get DPP bootstrap URI" },
  2975. { "dpp_bootstrap_info", wpa_cli_cmd_dpp_bootstrap_info, NULL,
  2976. cli_cmd_flag_none,
  2977. "<id> = show DPP bootstrap information" },
  2978. { "dpp_auth_init", wpa_cli_cmd_dpp_auth_init, NULL, cli_cmd_flag_none,
  2979. "peer=<id> [own=<id>] = initiate DPP bootstrapping" },
  2980. { "dpp_listen", wpa_cli_cmd_dpp_listen, NULL, cli_cmd_flag_none,
  2981. "<freq in MHz> = start DPP listen" },
  2982. { "dpp_stop_listen", wpa_cli_cmd_dpp_stop_listen, NULL,
  2983. cli_cmd_flag_none,
  2984. "= stop DPP listen" },
  2985. { "dpp_configurator_add", wpa_cli_cmd_dpp_configurator_add, NULL,
  2986. cli_cmd_flag_sensitive,
  2987. "[curve=..] [key=..] = add DPP configurator" },
  2988. { "dpp_configurator_remove", wpa_cli_cmd_dpp_configurator_remove, NULL,
  2989. cli_cmd_flag_none,
  2990. "*|<id> = remove DPP configurator" },
  2991. { "dpp_configurator_get_key", wpa_cli_cmd_dpp_configurator_get_key,
  2992. NULL, cli_cmd_flag_none,
  2993. "<id> = Get DPP configurator's private key" },
  2994. { "dpp_pkex_add", wpa_cli_cmd_dpp_pkex_add, NULL,
  2995. cli_cmd_flag_sensitive,
  2996. "add PKEX code" },
  2997. { "dpp_pkex_remove", wpa_cli_cmd_dpp_pkex_remove, NULL,
  2998. cli_cmd_flag_none,
  2999. "*|<id> = remove DPP pkex information" },
  3000. #endif /* CONFIG_DPP */
  3001. { NULL, NULL, NULL, cli_cmd_flag_none, NULL }
  3002. };
  3003. /*
  3004. * Prints command usage, lines are padded with the specified string.
  3005. */
  3006. static void print_cmd_help(const struct wpa_cli_cmd *cmd, const char *pad)
  3007. {
  3008. char c;
  3009. size_t n;
  3010. printf("%s%s ", pad, cmd->cmd);
  3011. for (n = 0; (c = cmd->usage[n]); n++) {
  3012. printf("%c", c);
  3013. if (c == '\n')
  3014. printf("%s", pad);
  3015. }
  3016. printf("\n");
  3017. }
  3018. static void print_help(const char *cmd)
  3019. {
  3020. int n;
  3021. printf("commands:\n");
  3022. for (n = 0; wpa_cli_commands[n].cmd; n++) {
  3023. if (cmd == NULL || str_starts(wpa_cli_commands[n].cmd, cmd))
  3024. print_cmd_help(&wpa_cli_commands[n], " ");
  3025. }
  3026. }
  3027. static int wpa_cli_edit_filter_history_cb(void *ctx, const char *cmd)
  3028. {
  3029. const char *c, *delim;
  3030. int n;
  3031. size_t len;
  3032. delim = os_strchr(cmd, ' ');
  3033. if (delim)
  3034. len = delim - cmd;
  3035. else
  3036. len = os_strlen(cmd);
  3037. for (n = 0; (c = wpa_cli_commands[n].cmd); n++) {
  3038. if (os_strncasecmp(cmd, c, len) == 0 && len == os_strlen(c))
  3039. return (wpa_cli_commands[n].flags &
  3040. cli_cmd_flag_sensitive);
  3041. }
  3042. return 0;
  3043. }
  3044. static char ** wpa_list_cmd_list(void)
  3045. {
  3046. char **res;
  3047. int i, count;
  3048. struct cli_txt_entry *e;
  3049. count = ARRAY_SIZE(wpa_cli_commands);
  3050. count += dl_list_len(&p2p_groups);
  3051. count += dl_list_len(&ifnames);
  3052. res = os_calloc(count + 1, sizeof(char *));
  3053. if (res == NULL)
  3054. return NULL;
  3055. for (i = 0; wpa_cli_commands[i].cmd; i++) {
  3056. res[i] = os_strdup(wpa_cli_commands[i].cmd);
  3057. if (res[i] == NULL)
  3058. break;
  3059. }
  3060. dl_list_for_each(e, &p2p_groups, struct cli_txt_entry, list) {
  3061. size_t len = 8 + os_strlen(e->txt);
  3062. res[i] = os_malloc(len);
  3063. if (res[i] == NULL)
  3064. break;
  3065. os_snprintf(res[i], len, "ifname=%s", e->txt);
  3066. i++;
  3067. }
  3068. dl_list_for_each(e, &ifnames, struct cli_txt_entry, list) {
  3069. res[i] = os_strdup(e->txt);
  3070. if (res[i] == NULL)
  3071. break;
  3072. i++;
  3073. }
  3074. return res;
  3075. }
  3076. static char ** wpa_cli_cmd_completion(const char *cmd, const char *str,
  3077. int pos)
  3078. {
  3079. int i;
  3080. for (i = 0; wpa_cli_commands[i].cmd; i++) {
  3081. if (os_strcasecmp(wpa_cli_commands[i].cmd, cmd) == 0) {
  3082. if (wpa_cli_commands[i].completion)
  3083. return wpa_cli_commands[i].completion(str,
  3084. pos);
  3085. edit_clear_line();
  3086. printf("\r%s\n", wpa_cli_commands[i].usage);
  3087. edit_redraw();
  3088. break;
  3089. }
  3090. }
  3091. return NULL;
  3092. }
  3093. static char ** wpa_cli_edit_completion_cb(void *ctx, const char *str, int pos)
  3094. {
  3095. char **res;
  3096. const char *end;
  3097. char *cmd;
  3098. if (pos > 7 && os_strncasecmp(str, "IFNAME=", 7) == 0) {
  3099. end = os_strchr(str, ' ');
  3100. if (end && pos > end - str) {
  3101. pos -= end - str + 1;
  3102. str = end + 1;
  3103. }
  3104. }
  3105. end = os_strchr(str, ' ');
  3106. if (end == NULL || str + pos < end)
  3107. return wpa_list_cmd_list();
  3108. cmd = os_malloc(pos + 1);
  3109. if (cmd == NULL)
  3110. return NULL;
  3111. os_memcpy(cmd, str, pos);
  3112. cmd[end - str] = '\0';
  3113. res = wpa_cli_cmd_completion(cmd, str, pos);
  3114. os_free(cmd);
  3115. return res;
  3116. }
  3117. static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
  3118. {
  3119. const struct wpa_cli_cmd *cmd, *match = NULL;
  3120. int count;
  3121. int ret = 0;
  3122. if (argc > 1 && os_strncasecmp(argv[0], "IFNAME=", 7) == 0) {
  3123. ifname_prefix = argv[0] + 7;
  3124. argv = &argv[1];
  3125. argc--;
  3126. } else
  3127. ifname_prefix = NULL;
  3128. if (argc == 0)
  3129. return -1;
  3130. count = 0;
  3131. cmd = wpa_cli_commands;
  3132. while (cmd->cmd) {
  3133. if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
  3134. {
  3135. match = cmd;
  3136. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  3137. /* we have an exact match */
  3138. count = 1;
  3139. break;
  3140. }
  3141. count++;
  3142. }
  3143. cmd++;
  3144. }
  3145. if (count > 1) {
  3146. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  3147. cmd = wpa_cli_commands;
  3148. while (cmd->cmd) {
  3149. if (os_strncasecmp(cmd->cmd, argv[0],
  3150. os_strlen(argv[0])) == 0) {
  3151. printf(" %s", cmd->cmd);
  3152. }
  3153. cmd++;
  3154. }
  3155. printf("\n");
  3156. ret = 1;
  3157. } else if (count == 0) {
  3158. printf("Unknown command '%s'\n", argv[0]);
  3159. ret = 1;
  3160. } else {
  3161. ret = match->handler(ctrl, argc - 1, &argv[1]);
  3162. }
  3163. return ret;
  3164. }
  3165. static int wpa_cli_exec(const char *program, const char *arg1,
  3166. const char *arg2)
  3167. {
  3168. char *arg;
  3169. size_t len;
  3170. int res;
  3171. /* If no interface is specified, set the global */
  3172. if (!arg1)
  3173. arg1 = "global";
  3174. len = os_strlen(arg1) + os_strlen(arg2) + 2;
  3175. arg = os_malloc(len);
  3176. if (arg == NULL)
  3177. return -1;
  3178. os_snprintf(arg, len, "%s %s", arg1, arg2);
  3179. res = os_exec(program, arg, 1);
  3180. os_free(arg);
  3181. return res;
  3182. }
  3183. static void wpa_cli_action_process(const char *msg)
  3184. {
  3185. const char *pos;
  3186. char *copy = NULL, *id, *pos2;
  3187. const char *ifname = ctrl_ifname;
  3188. char ifname_buf[100];
  3189. if (eloop_terminated())
  3190. return;
  3191. pos = msg;
  3192. if (os_strncmp(pos, "IFNAME=", 7) == 0) {
  3193. const char *end;
  3194. end = os_strchr(pos + 7, ' ');
  3195. if (end && (unsigned int) (end - pos) < sizeof(ifname_buf)) {
  3196. pos += 7;
  3197. os_memcpy(ifname_buf, pos, end - pos);
  3198. ifname_buf[end - pos] = '\0';
  3199. ifname = ifname_buf;
  3200. pos = end + 1;
  3201. }
  3202. }
  3203. if (*pos == '<') {
  3204. const char *prev = pos;
  3205. /* skip priority */
  3206. pos = os_strchr(pos, '>');
  3207. if (pos)
  3208. pos++;
  3209. else
  3210. pos = prev;
  3211. }
  3212. if (str_starts(pos, WPA_EVENT_CONNECTED)) {
  3213. int new_id = -1;
  3214. os_unsetenv("WPA_ID");
  3215. os_unsetenv("WPA_ID_STR");
  3216. os_unsetenv("WPA_CTRL_DIR");
  3217. pos = os_strstr(pos, "[id=");
  3218. if (pos)
  3219. copy = os_strdup(pos + 4);
  3220. if (copy) {
  3221. pos2 = id = copy;
  3222. while (*pos2 && *pos2 != ' ')
  3223. pos2++;
  3224. *pos2++ = '\0';
  3225. new_id = atoi(id);
  3226. os_setenv("WPA_ID", id, 1);
  3227. while (*pos2 && *pos2 != '=')
  3228. pos2++;
  3229. if (*pos2 == '=')
  3230. pos2++;
  3231. id = pos2;
  3232. while (*pos2 && *pos2 != ']')
  3233. pos2++;
  3234. *pos2 = '\0';
  3235. os_setenv("WPA_ID_STR", id, 1);
  3236. os_free(copy);
  3237. }
  3238. os_setenv("WPA_CTRL_DIR", ctrl_iface_dir, 1);
  3239. if (wpa_cli_connected <= 0 || new_id != wpa_cli_last_id) {
  3240. wpa_cli_connected = 1;
  3241. wpa_cli_last_id = new_id;
  3242. wpa_cli_exec(action_file, ifname, "CONNECTED");
  3243. }
  3244. } else if (str_starts(pos, WPA_EVENT_DISCONNECTED)) {
  3245. if (wpa_cli_connected) {
  3246. wpa_cli_connected = 0;
  3247. wpa_cli_exec(action_file, ifname, "DISCONNECTED");
  3248. }
  3249. } else if (str_starts(pos, AP_EVENT_ENABLED)) {
  3250. wpa_cli_exec(action_file, ctrl_ifname, pos);
  3251. } else if (str_starts(pos, AP_EVENT_DISABLED)) {
  3252. wpa_cli_exec(action_file, ctrl_ifname, pos);
  3253. } else if (str_starts(pos, MESH_GROUP_STARTED)) {
  3254. wpa_cli_exec(action_file, ctrl_ifname, pos);
  3255. } else if (str_starts(pos, MESH_GROUP_REMOVED)) {
  3256. wpa_cli_exec(action_file, ctrl_ifname, pos);
  3257. } else if (str_starts(pos, MESH_PEER_CONNECTED)) {
  3258. wpa_cli_exec(action_file, ctrl_ifname, pos);
  3259. } else if (str_starts(pos, MESH_PEER_DISCONNECTED)) {
  3260. wpa_cli_exec(action_file, ctrl_ifname, pos);
  3261. } else if (str_starts(pos, P2P_EVENT_GROUP_STARTED)) {
  3262. wpa_cli_exec(action_file, ifname, pos);
  3263. } else if (str_starts(pos, P2P_EVENT_GROUP_REMOVED)) {
  3264. wpa_cli_exec(action_file, ifname, pos);
  3265. } else if (str_starts(pos, P2P_EVENT_CROSS_CONNECT_ENABLE)) {
  3266. wpa_cli_exec(action_file, ifname, pos);
  3267. } else if (str_starts(pos, P2P_EVENT_CROSS_CONNECT_DISABLE)) {
  3268. wpa_cli_exec(action_file, ifname, pos);
  3269. } else if (str_starts(pos, P2P_EVENT_GO_NEG_FAILURE)) {
  3270. wpa_cli_exec(action_file, ifname, pos);
  3271. } else if (str_starts(pos, WPS_EVENT_SUCCESS)) {
  3272. wpa_cli_exec(action_file, ifname, pos);
  3273. } else if (str_starts(pos, WPS_EVENT_ACTIVE)) {
  3274. wpa_cli_exec(action_file, ifname, pos);
  3275. } else if (str_starts(pos, WPS_EVENT_TIMEOUT)) {
  3276. wpa_cli_exec(action_file, ifname, pos);
  3277. } else if (str_starts(pos, WPS_EVENT_FAIL)) {
  3278. wpa_cli_exec(action_file, ifname, pos);
  3279. } else if (str_starts(pos, AP_STA_CONNECTED)) {
  3280. wpa_cli_exec(action_file, ifname, pos);
  3281. } else if (str_starts(pos, AP_STA_DISCONNECTED)) {
  3282. wpa_cli_exec(action_file, ifname, pos);
  3283. } else if (str_starts(pos, ESS_DISASSOC_IMMINENT)) {
  3284. wpa_cli_exec(action_file, ifname, pos);
  3285. } else if (str_starts(pos, HS20_SUBSCRIPTION_REMEDIATION)) {
  3286. wpa_cli_exec(action_file, ifname, pos);
  3287. } else if (str_starts(pos, HS20_DEAUTH_IMMINENT_NOTICE)) {
  3288. wpa_cli_exec(action_file, ifname, pos);
  3289. } else if (str_starts(pos, WPA_EVENT_TERMINATING)) {
  3290. printf("wpa_supplicant is terminating - stop monitoring\n");
  3291. wpa_cli_quit = 1;
  3292. }
  3293. }
  3294. #ifndef CONFIG_ANSI_C_EXTRA
  3295. static void wpa_cli_action_cb(char *msg, size_t len)
  3296. {
  3297. wpa_cli_action_process(msg);
  3298. }
  3299. #endif /* CONFIG_ANSI_C_EXTRA */
  3300. static void wpa_cli_reconnect(void)
  3301. {
  3302. wpa_cli_close_connection();
  3303. if (wpa_cli_open_connection(ctrl_ifname, 1) < 0)
  3304. return;
  3305. if (interactive) {
  3306. edit_clear_line();
  3307. printf("\rConnection to wpa_supplicant re-established\n");
  3308. edit_redraw();
  3309. update_stations(ctrl_conn);
  3310. }
  3311. }
  3312. static void cli_event(const char *str)
  3313. {
  3314. const char *start, *s;
  3315. start = os_strchr(str, '>');
  3316. if (start == NULL)
  3317. return;
  3318. start++;
  3319. if (str_starts(start, WPA_EVENT_BSS_ADDED)) {
  3320. s = os_strchr(start, ' ');
  3321. if (s == NULL)
  3322. return;
  3323. s = os_strchr(s + 1, ' ');
  3324. if (s == NULL)
  3325. return;
  3326. cli_txt_list_add(&bsses, s + 1);
  3327. return;
  3328. }
  3329. if (str_starts(start, WPA_EVENT_BSS_REMOVED)) {
  3330. s = os_strchr(start, ' ');
  3331. if (s == NULL)
  3332. return;
  3333. s = os_strchr(s + 1, ' ');
  3334. if (s == NULL)
  3335. return;
  3336. cli_txt_list_del_addr(&bsses, s + 1);
  3337. return;
  3338. }
  3339. #ifdef CONFIG_P2P
  3340. if (str_starts(start, P2P_EVENT_DEVICE_FOUND)) {
  3341. s = os_strstr(start, " p2p_dev_addr=");
  3342. if (s == NULL)
  3343. return;
  3344. cli_txt_list_add_addr(&p2p_peers, s + 14);
  3345. return;
  3346. }
  3347. if (str_starts(start, P2P_EVENT_DEVICE_LOST)) {
  3348. s = os_strstr(start, " p2p_dev_addr=");
  3349. if (s == NULL)
  3350. return;
  3351. cli_txt_list_del_addr(&p2p_peers, s + 14);
  3352. return;
  3353. }
  3354. if (str_starts(start, P2P_EVENT_GROUP_STARTED)) {
  3355. s = os_strchr(start, ' ');
  3356. if (s == NULL)
  3357. return;
  3358. cli_txt_list_add_word(&p2p_groups, s + 1, ' ');
  3359. return;
  3360. }
  3361. if (str_starts(start, P2P_EVENT_GROUP_REMOVED)) {
  3362. s = os_strchr(start, ' ');
  3363. if (s == NULL)
  3364. return;
  3365. cli_txt_list_del_word(&p2p_groups, s + 1, ' ');
  3366. return;
  3367. }
  3368. #endif /* CONFIG_P2P */
  3369. }
  3370. static int check_terminating(const char *msg)
  3371. {
  3372. const char *pos = msg;
  3373. if (*pos == '<') {
  3374. /* skip priority */
  3375. pos = os_strchr(pos, '>');
  3376. if (pos)
  3377. pos++;
  3378. else
  3379. pos = msg;
  3380. }
  3381. if (str_starts(pos, WPA_EVENT_TERMINATING) && ctrl_conn) {
  3382. edit_clear_line();
  3383. printf("\rConnection to wpa_supplicant lost - trying to "
  3384. "reconnect\n");
  3385. edit_redraw();
  3386. wpa_cli_attached = 0;
  3387. wpa_cli_close_connection();
  3388. return 1;
  3389. }
  3390. return 0;
  3391. }
  3392. static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor)
  3393. {
  3394. if (ctrl_conn == NULL) {
  3395. wpa_cli_reconnect();
  3396. return;
  3397. }
  3398. while (wpa_ctrl_pending(ctrl) > 0) {
  3399. char buf[4096];
  3400. size_t len = sizeof(buf) - 1;
  3401. if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
  3402. buf[len] = '\0';
  3403. if (action_monitor)
  3404. wpa_cli_action_process(buf);
  3405. else {
  3406. cli_event(buf);
  3407. if (wpa_cli_show_event(buf)) {
  3408. edit_clear_line();
  3409. printf("\r%s\n", buf);
  3410. edit_redraw();
  3411. }
  3412. if (interactive && check_terminating(buf) > 0)
  3413. return;
  3414. }
  3415. } else {
  3416. printf("Could not read pending message.\n");
  3417. break;
  3418. }
  3419. }
  3420. if (wpa_ctrl_pending(ctrl) < 0) {
  3421. printf("Connection to wpa_supplicant lost - trying to "
  3422. "reconnect\n");
  3423. wpa_cli_reconnect();
  3424. }
  3425. }
  3426. static void wpa_cli_ping(void *eloop_ctx, void *timeout_ctx)
  3427. {
  3428. if (ctrl_conn) {
  3429. int res;
  3430. char *prefix = ifname_prefix;
  3431. ifname_prefix = NULL;
  3432. res = _wpa_ctrl_command(ctrl_conn, "PING", 0);
  3433. ifname_prefix = prefix;
  3434. if (res) {
  3435. printf("Connection to wpa_supplicant lost - trying to "
  3436. "reconnect\n");
  3437. wpa_cli_close_connection();
  3438. }
  3439. }
  3440. if (!ctrl_conn)
  3441. wpa_cli_reconnect();
  3442. eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
  3443. }
  3444. static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx)
  3445. {
  3446. wpa_cli_recv_pending(mon_conn, 0);
  3447. }
  3448. static void wpa_cli_edit_cmd_cb(void *ctx, char *cmd)
  3449. {
  3450. char *argv[max_args];
  3451. int argc;
  3452. argc = tokenize_cmd(cmd, argv);
  3453. if (argc)
  3454. wpa_request(ctrl_conn, argc, argv);
  3455. }
  3456. static void wpa_cli_edit_eof_cb(void *ctx)
  3457. {
  3458. eloop_terminate();
  3459. }
  3460. static int warning_displayed = 0;
  3461. static char *hfile = NULL;
  3462. static int edit_started = 0;
  3463. static void start_edit(void)
  3464. {
  3465. char *home;
  3466. char *ps = NULL;
  3467. #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
  3468. ps = wpa_ctrl_get_remote_ifname(ctrl_conn);
  3469. #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
  3470. #ifdef CONFIG_WPA_CLI_HISTORY_DIR
  3471. home = CONFIG_WPA_CLI_HISTORY_DIR;
  3472. #else /* CONFIG_WPA_CLI_HISTORY_DIR */
  3473. home = getenv("HOME");
  3474. #endif /* CONFIG_WPA_CLI_HISTORY_DIR */
  3475. if (home) {
  3476. const char *fname = ".wpa_cli_history";
  3477. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  3478. hfile = os_malloc(hfile_len);
  3479. if (hfile)
  3480. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  3481. }
  3482. if (edit_init(wpa_cli_edit_cmd_cb, wpa_cli_edit_eof_cb,
  3483. wpa_cli_edit_completion_cb, NULL, hfile, ps) < 0) {
  3484. eloop_terminate();
  3485. return;
  3486. }
  3487. edit_started = 1;
  3488. eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
  3489. }
  3490. static void update_bssid_list(struct wpa_ctrl *ctrl)
  3491. {
  3492. char buf[4096];
  3493. size_t len = sizeof(buf);
  3494. int ret;
  3495. const char *cmd = "BSS RANGE=ALL MASK=0x2";
  3496. char *pos, *end;
  3497. if (ctrl == NULL)
  3498. return;
  3499. ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
  3500. if (ret < 0)
  3501. return;
  3502. buf[len] = '\0';
  3503. pos = buf;
  3504. while (pos) {
  3505. pos = os_strstr(pos, "bssid=");
  3506. if (pos == NULL)
  3507. break;
  3508. pos += 6;
  3509. end = os_strchr(pos, '\n');
  3510. if (end == NULL)
  3511. break;
  3512. *end = '\0';
  3513. cli_txt_list_add(&bsses, pos);
  3514. pos = end + 1;
  3515. }
  3516. }
  3517. static void update_ifnames(struct wpa_ctrl *ctrl)
  3518. {
  3519. char buf[4096];
  3520. size_t len = sizeof(buf);
  3521. int ret;
  3522. const char *cmd = "INTERFACES";
  3523. char *pos, *end;
  3524. char txt[200];
  3525. cli_txt_list_flush(&ifnames);
  3526. if (ctrl == NULL)
  3527. return;
  3528. ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
  3529. if (ret < 0)
  3530. return;
  3531. buf[len] = '\0';
  3532. pos = buf;
  3533. while (pos) {
  3534. end = os_strchr(pos, '\n');
  3535. if (end == NULL)
  3536. break;
  3537. *end = '\0';
  3538. ret = os_snprintf(txt, sizeof(txt), "ifname=%s", pos);
  3539. if (!os_snprintf_error(sizeof(txt), ret))
  3540. cli_txt_list_add(&ifnames, txt);
  3541. pos = end + 1;
  3542. }
  3543. }
  3544. static void update_creds(struct wpa_ctrl *ctrl)
  3545. {
  3546. char buf[4096];
  3547. size_t len = sizeof(buf);
  3548. int ret;
  3549. const char *cmd = "LIST_CREDS";
  3550. char *pos, *end;
  3551. int header = 1;
  3552. cli_txt_list_flush(&creds);
  3553. if (ctrl == NULL)
  3554. return;
  3555. ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
  3556. if (ret < 0)
  3557. return;
  3558. buf[len] = '\0';
  3559. pos = buf;
  3560. while (pos) {
  3561. end = os_strchr(pos, '\n');
  3562. if (end == NULL)
  3563. break;
  3564. *end = '\0';
  3565. if (!header)
  3566. cli_txt_list_add_word(&creds, pos, '\t');
  3567. header = 0;
  3568. pos = end + 1;
  3569. }
  3570. }
  3571. static void update_networks(struct wpa_ctrl *ctrl)
  3572. {
  3573. char buf[4096];
  3574. size_t len = sizeof(buf);
  3575. int ret;
  3576. const char *cmd = "LIST_NETWORKS";
  3577. char *pos, *end;
  3578. int header = 1;
  3579. cli_txt_list_flush(&networks);
  3580. if (ctrl == NULL)
  3581. return;
  3582. ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len, NULL);
  3583. if (ret < 0)
  3584. return;
  3585. buf[len] = '\0';
  3586. pos = buf;
  3587. while (pos) {
  3588. end = os_strchr(pos, '\n');
  3589. if (end == NULL)
  3590. break;
  3591. *end = '\0';
  3592. if (!header)
  3593. cli_txt_list_add_word(&networks, pos, '\t');
  3594. header = 0;
  3595. pos = end + 1;
  3596. }
  3597. }
  3598. static void update_stations(struct wpa_ctrl *ctrl)
  3599. {
  3600. #ifdef CONFIG_AP
  3601. char addr[32], cmd[64];
  3602. if (!ctrl || !interactive)
  3603. return;
  3604. cli_txt_list_flush(&stations);
  3605. if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr), 0))
  3606. return;
  3607. do {
  3608. if (os_strcmp(addr, "") != 0)
  3609. cli_txt_list_add(&stations, addr);
  3610. os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
  3611. } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr), 0) == 0);
  3612. #endif /* CONFIG_AP */
  3613. }
  3614. static void try_connection(void *eloop_ctx, void *timeout_ctx)
  3615. {
  3616. if (ctrl_conn)
  3617. goto done;
  3618. if (ctrl_ifname == NULL)
  3619. ctrl_ifname = wpa_cli_get_default_ifname();
  3620. if (wpa_cli_open_connection(ctrl_ifname, 1)) {
  3621. if (!warning_displayed) {
  3622. printf("Could not connect to wpa_supplicant: "
  3623. "%s - re-trying\n",
  3624. ctrl_ifname ? ctrl_ifname : "(nil)");
  3625. warning_displayed = 1;
  3626. }
  3627. eloop_register_timeout(1, 0, try_connection, NULL, NULL);
  3628. return;
  3629. }
  3630. update_bssid_list(ctrl_conn);
  3631. update_creds(ctrl_conn);
  3632. update_networks(ctrl_conn);
  3633. update_stations(ctrl_conn);
  3634. if (warning_displayed)
  3635. printf("Connection established.\n");
  3636. done:
  3637. start_edit();
  3638. }
  3639. static void wpa_cli_interactive(void)
  3640. {
  3641. printf("\nInteractive mode\n\n");
  3642. eloop_register_timeout(0, 0, try_connection, NULL, NULL);
  3643. eloop_run();
  3644. eloop_cancel_timeout(try_connection, NULL, NULL);
  3645. cli_txt_list_flush(&p2p_peers);
  3646. cli_txt_list_flush(&p2p_groups);
  3647. cli_txt_list_flush(&bsses);
  3648. cli_txt_list_flush(&ifnames);
  3649. cli_txt_list_flush(&creds);
  3650. cli_txt_list_flush(&networks);
  3651. if (edit_started)
  3652. edit_deinit(hfile, wpa_cli_edit_filter_history_cb);
  3653. os_free(hfile);
  3654. eloop_cancel_timeout(wpa_cli_ping, NULL, NULL);
  3655. wpa_cli_close_connection();
  3656. }
  3657. static void wpa_cli_action_ping(void *eloop_ctx, void *timeout_ctx)
  3658. {
  3659. struct wpa_ctrl *ctrl = eloop_ctx;
  3660. char buf[256];
  3661. size_t len;
  3662. /* verify that connection is still working */
  3663. len = sizeof(buf) - 1;
  3664. if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
  3665. wpa_cli_action_cb) < 0 ||
  3666. len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
  3667. printf("wpa_supplicant did not reply to PING command - exiting\n");
  3668. eloop_terminate();
  3669. return;
  3670. }
  3671. eloop_register_timeout(ping_interval, 0, wpa_cli_action_ping,
  3672. ctrl, NULL);
  3673. }
  3674. static void wpa_cli_action_receive(int sock, void *eloop_ctx, void *sock_ctx)
  3675. {
  3676. struct wpa_ctrl *ctrl = eloop_ctx;
  3677. wpa_cli_recv_pending(ctrl, 1);
  3678. }
  3679. static void wpa_cli_action(struct wpa_ctrl *ctrl)
  3680. {
  3681. #ifdef CONFIG_ANSI_C_EXTRA
  3682. /* TODO: ANSI C version(?) */
  3683. printf("Action processing not supported in ANSI C build.\n");
  3684. #else /* CONFIG_ANSI_C_EXTRA */
  3685. int fd;
  3686. fd = wpa_ctrl_get_fd(ctrl);
  3687. eloop_register_timeout(ping_interval, 0, wpa_cli_action_ping,
  3688. ctrl, NULL);
  3689. eloop_register_read_sock(fd, wpa_cli_action_receive, ctrl, NULL);
  3690. eloop_run();
  3691. eloop_cancel_timeout(wpa_cli_action_ping, ctrl, NULL);
  3692. eloop_unregister_read_sock(fd);
  3693. #endif /* CONFIG_ANSI_C_EXTRA */
  3694. }
  3695. static void wpa_cli_cleanup(void)
  3696. {
  3697. wpa_cli_close_connection();
  3698. if (pid_file)
  3699. os_daemonize_terminate(pid_file);
  3700. os_program_deinit();
  3701. }
  3702. static void wpa_cli_terminate(int sig, void *ctx)
  3703. {
  3704. eloop_terminate();
  3705. }
  3706. static char * wpa_cli_get_default_ifname(void)
  3707. {
  3708. char *ifname = NULL;
  3709. #ifdef ANDROID
  3710. char ifprop[PROPERTY_VALUE_MAX];
  3711. if (property_get("wifi.interface", ifprop, NULL) != 0) {
  3712. ifname = os_strdup(ifprop);
  3713. printf("Using interface '%s'\n", ifname ? ifname : "N/A");
  3714. }
  3715. #else /* ANDROID */
  3716. #ifdef CONFIG_CTRL_IFACE_UNIX
  3717. struct dirent *dent;
  3718. DIR *dir = opendir(ctrl_iface_dir);
  3719. if (!dir) {
  3720. return NULL;
  3721. }
  3722. while ((dent = readdir(dir))) {
  3723. #ifdef _DIRENT_HAVE_D_TYPE
  3724. /*
  3725. * Skip the file if it is not a socket. Also accept
  3726. * DT_UNKNOWN (0) in case the C library or underlying
  3727. * file system does not support d_type.
  3728. */
  3729. if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
  3730. continue;
  3731. #endif /* _DIRENT_HAVE_D_TYPE */
  3732. if (os_strcmp(dent->d_name, ".") == 0 ||
  3733. os_strcmp(dent->d_name, "..") == 0)
  3734. continue;
  3735. printf("Selected interface '%s'\n", dent->d_name);
  3736. ifname = os_strdup(dent->d_name);
  3737. break;
  3738. }
  3739. closedir(dir);
  3740. #endif /* CONFIG_CTRL_IFACE_UNIX */
  3741. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  3742. char buf[4096], *pos;
  3743. size_t len;
  3744. struct wpa_ctrl *ctrl;
  3745. int ret;
  3746. ctrl = wpa_ctrl_open(NULL);
  3747. if (ctrl == NULL)
  3748. return NULL;
  3749. len = sizeof(buf) - 1;
  3750. ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
  3751. if (ret >= 0) {
  3752. buf[len] = '\0';
  3753. pos = os_strchr(buf, '\n');
  3754. if (pos)
  3755. *pos = '\0';
  3756. ifname = os_strdup(buf);
  3757. }
  3758. wpa_ctrl_close(ctrl);
  3759. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  3760. #endif /* ANDROID */
  3761. return ifname;
  3762. }
  3763. int main(int argc, char *argv[])
  3764. {
  3765. int c;
  3766. int daemonize = 0;
  3767. int ret = 0;
  3768. const char *global = NULL;
  3769. if (os_program_init())
  3770. return -1;
  3771. for (;;) {
  3772. c = getopt(argc, argv, "a:Bg:G:hi:p:P:s:v");
  3773. if (c < 0)
  3774. break;
  3775. switch (c) {
  3776. case 'a':
  3777. action_file = optarg;
  3778. break;
  3779. case 'B':
  3780. daemonize = 1;
  3781. break;
  3782. case 'g':
  3783. global = optarg;
  3784. break;
  3785. case 'G':
  3786. ping_interval = atoi(optarg);
  3787. break;
  3788. case 'h':
  3789. usage();
  3790. return 0;
  3791. case 'v':
  3792. printf("%s\n", wpa_cli_version);
  3793. return 0;
  3794. case 'i':
  3795. os_free(ctrl_ifname);
  3796. ctrl_ifname = os_strdup(optarg);
  3797. break;
  3798. case 'p':
  3799. ctrl_iface_dir = optarg;
  3800. break;
  3801. case 'P':
  3802. pid_file = optarg;
  3803. break;
  3804. case 's':
  3805. client_socket_dir = optarg;
  3806. break;
  3807. default:
  3808. usage();
  3809. return -1;
  3810. }
  3811. }
  3812. interactive = (argc == optind) && (action_file == NULL);
  3813. if (interactive)
  3814. printf("%s\n\n%s\n\n", wpa_cli_version, cli_license);
  3815. if (eloop_init())
  3816. return -1;
  3817. if (global) {
  3818. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  3819. ctrl_conn = wpa_ctrl_open(NULL);
  3820. #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  3821. ctrl_conn = wpa_ctrl_open(global);
  3822. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  3823. if (ctrl_conn == NULL) {
  3824. fprintf(stderr, "Failed to connect to wpa_supplicant "
  3825. "global interface: %s error: %s\n",
  3826. global, strerror(errno));
  3827. return -1;
  3828. }
  3829. if (interactive) {
  3830. update_ifnames(ctrl_conn);
  3831. mon_conn = wpa_ctrl_open(global);
  3832. if (mon_conn) {
  3833. if (wpa_ctrl_attach(mon_conn) == 0) {
  3834. wpa_cli_attached = 1;
  3835. eloop_register_read_sock(
  3836. wpa_ctrl_get_fd(mon_conn),
  3837. wpa_cli_mon_receive,
  3838. NULL, NULL);
  3839. } else {
  3840. printf("Failed to open monitor "
  3841. "connection through global "
  3842. "control interface\n");
  3843. }
  3844. }
  3845. update_stations(ctrl_conn);
  3846. }
  3847. }
  3848. eloop_register_signal_terminate(wpa_cli_terminate, NULL);
  3849. if (ctrl_ifname == NULL)
  3850. ctrl_ifname = wpa_cli_get_default_ifname();
  3851. if (interactive) {
  3852. wpa_cli_interactive();
  3853. } else {
  3854. if (!global &&
  3855. wpa_cli_open_connection(ctrl_ifname, 0) < 0) {
  3856. fprintf(stderr, "Failed to connect to non-global "
  3857. "ctrl_ifname: %s error: %s\n",
  3858. ctrl_ifname ? ctrl_ifname : "(nil)",
  3859. strerror(errno));
  3860. return -1;
  3861. }
  3862. if (action_file) {
  3863. if (wpa_ctrl_attach(ctrl_conn) == 0) {
  3864. wpa_cli_attached = 1;
  3865. } else {
  3866. printf("Warning: Failed to attach to "
  3867. "wpa_supplicant.\n");
  3868. return -1;
  3869. }
  3870. }
  3871. if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
  3872. return -1;
  3873. if (action_file)
  3874. wpa_cli_action(ctrl_conn);
  3875. else
  3876. ret = wpa_request(ctrl_conn, argc - optind,
  3877. &argv[optind]);
  3878. }
  3879. os_free(ctrl_ifname);
  3880. eloop_destroy();
  3881. wpa_cli_cleanup();
  3882. return ret;
  3883. }
  3884. #else /* CONFIG_CTRL_IFACE */
  3885. int main(int argc, char *argv[])
  3886. {
  3887. printf("CONFIG_CTRL_IFACE not defined - wpa_cli disabled\n");
  3888. return -1;
  3889. }
  3890. #endif /* CONFIG_CTRL_IFACE */