您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

wpa_cli.c 79KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297
  1. /*
  2. * WPA Supplicant - command line interface for wpa_supplicant daemon
  3. * Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #ifdef CONFIG_CTRL_IFACE
  16. #ifdef CONFIG_CTRL_IFACE_UNIX
  17. #include <dirent.h>
  18. #endif /* CONFIG_CTRL_IFACE_UNIX */
  19. #include "common/wpa_ctrl.h"
  20. #include "utils/common.h"
  21. #include "utils/eloop.h"
  22. #include "utils/edit.h"
  23. #include "common/version.h"
  24. #ifdef ANDROID
  25. #include <cutils/properties.h>
  26. #endif /* ANDROID */
  27. static const char *wpa_cli_version =
  28. "wpa_cli v" VERSION_STR "\n"
  29. "Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi> and contributors";
  30. static const char *wpa_cli_license =
  31. "This program is free software. You can distribute it and/or modify it\n"
  32. "under the terms of the GNU General Public License version 2.\n"
  33. "\n"
  34. "Alternatively, this software may be distributed under the terms of the\n"
  35. "BSD license. See README and COPYING for more details.\n";
  36. static const char *wpa_cli_full_license =
  37. "This program is free software; you can redistribute it and/or modify\n"
  38. "it under the terms of the GNU General Public License version 2 as\n"
  39. "published by the Free Software Foundation.\n"
  40. "\n"
  41. "This program is distributed in the hope that it will be useful,\n"
  42. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  43. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  44. "GNU General Public License for more details.\n"
  45. "\n"
  46. "You should have received a copy of the GNU General Public License\n"
  47. "along with this program; if not, write to the Free Software\n"
  48. "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
  49. "\n"
  50. "Alternatively, this software may be distributed under the terms of the\n"
  51. "BSD license.\n"
  52. "\n"
  53. "Redistribution and use in source and binary forms, with or without\n"
  54. "modification, are permitted provided that the following conditions are\n"
  55. "met:\n"
  56. "\n"
  57. "1. Redistributions of source code must retain the above copyright\n"
  58. " notice, this list of conditions and the following disclaimer.\n"
  59. "\n"
  60. "2. Redistributions in binary form must reproduce the above copyright\n"
  61. " notice, this list of conditions and the following disclaimer in the\n"
  62. " documentation and/or other materials provided with the distribution.\n"
  63. "\n"
  64. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  65. " names of its contributors may be used to endorse or promote products\n"
  66. " derived from this software without specific prior written permission.\n"
  67. "\n"
  68. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  69. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  70. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  71. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
  72. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  73. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  74. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  75. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  76. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  77. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  78. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  79. "\n";
  80. static struct wpa_ctrl *ctrl_conn;
  81. static struct wpa_ctrl *mon_conn;
  82. static int wpa_cli_quit = 0;
  83. static int wpa_cli_attached = 0;
  84. static int wpa_cli_connected = 0;
  85. static int wpa_cli_last_id = 0;
  86. #ifndef CONFIG_CTRL_IFACE_DIR
  87. #define CONFIG_CTRL_IFACE_DIR "/var/run/wpa_supplicant"
  88. #endif /* CONFIG_CTRL_IFACE_DIR */
  89. static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
  90. static char *ctrl_ifname = NULL;
  91. static const char *pid_file = NULL;
  92. static const char *action_file = NULL;
  93. static int ping_interval = 5;
  94. static int interactive = 0;
  95. static void print_help(void);
  96. static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx);
  97. static void usage(void)
  98. {
  99. printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvB] "
  100. "[-a<action file>] \\\n"
  101. " [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] "
  102. "[command..]\n"
  103. " -h = help (show this usage text)\n"
  104. " -v = shown version information\n"
  105. " -a = run in daemon mode executing the action file based on "
  106. "events from\n"
  107. " wpa_supplicant\n"
  108. " -B = run a daemon in the background\n"
  109. " default path: " CONFIG_CTRL_IFACE_DIR "\n"
  110. " default interface: first interface found in socket path\n");
  111. print_help();
  112. }
  113. static int str_starts(const char *src, const char *match)
  114. {
  115. return os_strncmp(src, match, os_strlen(match)) == 0;
  116. }
  117. static int wpa_cli_show_event(const char *event)
  118. {
  119. const char *start;
  120. start = os_strchr(event, '>');
  121. if (start == NULL)
  122. return 1;
  123. start++;
  124. /*
  125. * Skip BSS added/removed events since they can be relatively frequent
  126. * and are likely of not much use for an interactive user.
  127. */
  128. if (str_starts(start, WPA_EVENT_BSS_ADDED) ||
  129. str_starts(start, WPA_EVENT_BSS_REMOVED))
  130. return 0;
  131. return 1;
  132. }
  133. static int wpa_cli_open_connection(const char *ifname, int attach)
  134. {
  135. #if defined(CONFIG_CTRL_IFACE_UDP) || defined(CONFIG_CTRL_IFACE_NAMED_PIPE)
  136. ctrl_conn = wpa_ctrl_open(ifname);
  137. if (ctrl_conn == NULL)
  138. return -1;
  139. if (attach && interactive)
  140. mon_conn = wpa_ctrl_open(ifname);
  141. else
  142. mon_conn = NULL;
  143. #else /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
  144. char *cfile = NULL;
  145. int flen, res;
  146. if (ifname == NULL)
  147. return -1;
  148. #ifdef ANDROID
  149. if (access(ctrl_iface_dir, F_OK) < 0) {
  150. cfile = os_strdup(ifname);
  151. if (cfile == NULL)
  152. return -1;
  153. }
  154. #endif /* ANDROID */
  155. if (cfile == NULL) {
  156. flen = os_strlen(ctrl_iface_dir) + os_strlen(ifname) + 2;
  157. cfile = os_malloc(flen);
  158. if (cfile == NULL)
  159. return -1;
  160. res = os_snprintf(cfile, flen, "%s/%s", ctrl_iface_dir,
  161. ifname);
  162. if (res < 0 || res >= flen) {
  163. os_free(cfile);
  164. return -1;
  165. }
  166. }
  167. ctrl_conn = wpa_ctrl_open(cfile);
  168. if (ctrl_conn == NULL) {
  169. os_free(cfile);
  170. return -1;
  171. }
  172. if (attach && interactive)
  173. mon_conn = wpa_ctrl_open(cfile);
  174. else
  175. mon_conn = NULL;
  176. os_free(cfile);
  177. #endif /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
  178. if (mon_conn) {
  179. if (wpa_ctrl_attach(mon_conn) == 0) {
  180. wpa_cli_attached = 1;
  181. if (interactive)
  182. eloop_register_read_sock(
  183. wpa_ctrl_get_fd(mon_conn),
  184. wpa_cli_mon_receive, NULL, NULL);
  185. } else {
  186. printf("Warning: Failed to attach to "
  187. "wpa_supplicant.\n");
  188. return -1;
  189. }
  190. }
  191. return 0;
  192. }
  193. static void wpa_cli_close_connection(void)
  194. {
  195. if (ctrl_conn == NULL)
  196. return;
  197. if (wpa_cli_attached) {
  198. wpa_ctrl_detach(interactive ? mon_conn : ctrl_conn);
  199. wpa_cli_attached = 0;
  200. }
  201. wpa_ctrl_close(ctrl_conn);
  202. ctrl_conn = NULL;
  203. if (mon_conn) {
  204. eloop_unregister_read_sock(wpa_ctrl_get_fd(mon_conn));
  205. wpa_ctrl_close(mon_conn);
  206. mon_conn = NULL;
  207. }
  208. }
  209. static void wpa_cli_msg_cb(char *msg, size_t len)
  210. {
  211. printf("%s\n", msg);
  212. }
  213. static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
  214. {
  215. //Modified by Kurt; enlarge command string for scan_result
  216. char buf[4096];
  217. size_t len;
  218. int ret;
  219. if (ctrl_conn == NULL) {
  220. printf("Not connected to wpa_supplicant - command dropped.\n");
  221. return -1;
  222. }
  223. len = sizeof(buf) - 1;
  224. ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
  225. wpa_cli_msg_cb);
  226. if (ret == -2) {
  227. printf("'%s' command timed out.\n", cmd);
  228. return -2;
  229. } else if (ret < 0) {
  230. printf("'%s' command failed.\n", cmd);
  231. return -1;
  232. }
  233. if (print) {
  234. buf[len] = '\0';
  235. printf("%s", buf);
  236. if (interactive && len > 0 && buf[len - 1] != '\n')
  237. printf("\n");
  238. }
  239. return 0;
  240. }
  241. static int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
  242. {
  243. return _wpa_ctrl_command(ctrl, cmd, 1);
  244. }
  245. static int wpa_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
  246. {
  247. int verbose = argc > 0 && os_strcmp(argv[0], "verbose") == 0;
  248. return wpa_ctrl_command(ctrl, verbose ? "STATUS-VERBOSE" : "STATUS");
  249. }
  250. static int wpa_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
  251. {
  252. return wpa_ctrl_command(ctrl, "PING");
  253. }
  254. static int wpa_cli_cmd_relog(struct wpa_ctrl *ctrl, int argc, char *argv[])
  255. {
  256. return wpa_ctrl_command(ctrl, "RELOG");
  257. }
  258. static int wpa_cli_cmd_note(struct wpa_ctrl *ctrl, int argc, char *argv[])
  259. {
  260. char cmd[256];
  261. int ret;
  262. if (argc == 0)
  263. return -1;
  264. ret = os_snprintf(cmd, sizeof(cmd), "NOTE %s", argv[0]);
  265. if (ret < 0 || (size_t) ret >= sizeof(cmd))
  266. return -1;
  267. return wpa_ctrl_command(ctrl, cmd);
  268. }
  269. static int wpa_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
  270. {
  271. return wpa_ctrl_command(ctrl, "MIB");
  272. }
  273. static int wpa_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
  274. {
  275. return wpa_ctrl_command(ctrl, "PMKSA");
  276. }
  277. static int wpa_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
  278. {
  279. print_help();
  280. return 0;
  281. }
  282. static int wpa_cli_cmd_license(struct wpa_ctrl *ctrl, int argc, char *argv[])
  283. {
  284. printf("%s\n\n%s\n", wpa_cli_version, wpa_cli_full_license);
  285. return 0;
  286. }
  287. static int wpa_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
  288. {
  289. wpa_cli_quit = 1;
  290. if (interactive)
  291. eloop_terminate();
  292. return 0;
  293. }
  294. static void wpa_cli_show_variables(void)
  295. {
  296. printf("set variables:\n"
  297. " EAPOL::heldPeriod (EAPOL state machine held period, "
  298. "in seconds)\n"
  299. " EAPOL::authPeriod (EAPOL state machine authentication "
  300. "period, in seconds)\n"
  301. " EAPOL::startPeriod (EAPOL state machine start period, in "
  302. "seconds)\n"
  303. " EAPOL::maxStart (EAPOL state machine maximum start "
  304. "attempts)\n");
  305. printf(" dot11RSNAConfigPMKLifetime (WPA/WPA2 PMK lifetime in "
  306. "seconds)\n"
  307. " dot11RSNAConfigPMKReauthThreshold (WPA/WPA2 reauthentication"
  308. " threshold\n\tpercentage)\n"
  309. " dot11RSNAConfigSATimeout (WPA/WPA2 timeout for completing "
  310. "security\n\tassociation in seconds)\n");
  311. }
  312. static int wpa_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
  313. {
  314. char cmd[256];
  315. int res;
  316. if (argc == 0) {
  317. wpa_cli_show_variables();
  318. return 0;
  319. }
  320. if (argc != 2) {
  321. printf("Invalid SET command: needs two arguments (variable "
  322. "name and value)\n");
  323. return -1;
  324. }
  325. res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
  326. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  327. printf("Too long SET command.\n");
  328. return -1;
  329. }
  330. return wpa_ctrl_command(ctrl, cmd);
  331. }
  332. static int wpa_cli_cmd_get(struct wpa_ctrl *ctrl, int argc, char *argv[])
  333. {
  334. char cmd[256];
  335. int res;
  336. if (argc != 1) {
  337. printf("Invalid GET command: need one argument (variable "
  338. "name)\n");
  339. return -1;
  340. }
  341. res = os_snprintf(cmd, sizeof(cmd), "GET %s", argv[0]);
  342. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  343. printf("Too long GET command.\n");
  344. return -1;
  345. }
  346. return wpa_ctrl_command(ctrl, cmd);
  347. }
  348. static int wpa_cli_cmd_logoff(struct wpa_ctrl *ctrl, int argc, char *argv[])
  349. {
  350. return wpa_ctrl_command(ctrl, "LOGOFF");
  351. }
  352. static int wpa_cli_cmd_logon(struct wpa_ctrl *ctrl, int argc, char *argv[])
  353. {
  354. return wpa_ctrl_command(ctrl, "LOGON");
  355. }
  356. static int wpa_cli_cmd_reassociate(struct wpa_ctrl *ctrl, int argc,
  357. char *argv[])
  358. {
  359. return wpa_ctrl_command(ctrl, "REASSOCIATE");
  360. }
  361. static int wpa_cli_cmd_preauthenticate(struct wpa_ctrl *ctrl, int argc,
  362. char *argv[])
  363. {
  364. char cmd[256];
  365. int res;
  366. if (argc != 1) {
  367. printf("Invalid PREAUTH command: needs one argument "
  368. "(BSSID)\n");
  369. return -1;
  370. }
  371. res = os_snprintf(cmd, sizeof(cmd), "PREAUTH %s", argv[0]);
  372. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  373. printf("Too long PREAUTH command.\n");
  374. return -1;
  375. }
  376. return wpa_ctrl_command(ctrl, cmd);
  377. }
  378. static int wpa_cli_cmd_ap_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
  379. {
  380. char cmd[256];
  381. int res;
  382. if (argc != 1) {
  383. printf("Invalid AP_SCAN command: needs one argument (ap_scan "
  384. "value)\n");
  385. return -1;
  386. }
  387. res = os_snprintf(cmd, sizeof(cmd), "AP_SCAN %s", argv[0]);
  388. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  389. printf("Too long AP_SCAN command.\n");
  390. return -1;
  391. }
  392. return wpa_ctrl_command(ctrl, cmd);
  393. }
  394. static int wpa_cli_cmd_scan_interval(struct wpa_ctrl *ctrl, int argc,
  395. char *argv[])
  396. {
  397. char cmd[256];
  398. int res;
  399. if (argc != 1) {
  400. printf("Invalid SCAN_INTERVAL command: needs one argument "
  401. "scan_interval value)\n");
  402. return -1;
  403. }
  404. res = os_snprintf(cmd, sizeof(cmd), "SCAN_INTERVAL %s", argv[0]);
  405. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  406. printf("Too long SCAN_INTERVAL command.\n");
  407. return -1;
  408. }
  409. return wpa_ctrl_command(ctrl, cmd);
  410. }
  411. static int wpa_cli_cmd_bss_expire_age(struct wpa_ctrl *ctrl, int argc,
  412. char *argv[])
  413. {
  414. char cmd[256];
  415. int res;
  416. if (argc != 1) {
  417. printf("Invalid BSS_EXPIRE_AGE command: needs one argument "
  418. "(bss_expire_age value)\n");
  419. return -1;
  420. }
  421. res = os_snprintf(cmd, sizeof(cmd), "BSS_EXPIRE_AGE %s", argv[0]);
  422. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  423. printf("Too long BSS_EXPIRE_AGE command.\n");
  424. return -1;
  425. }
  426. return wpa_ctrl_command(ctrl, cmd);
  427. }
  428. static int wpa_cli_cmd_bss_expire_count(struct wpa_ctrl *ctrl, int argc,
  429. char *argv[])
  430. {
  431. char cmd[256];
  432. int res;
  433. if (argc != 1) {
  434. printf("Invalid BSS_EXPIRE_COUNT command: needs one argument "
  435. "(bss_expire_count value)\n");
  436. return -1;
  437. }
  438. res = os_snprintf(cmd, sizeof(cmd), "BSS_EXPIRE_COUNT %s", argv[0]);
  439. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  440. printf("Too long BSS_EXPIRE_COUNT command.\n");
  441. return -1;
  442. }
  443. return wpa_ctrl_command(ctrl, cmd);
  444. }
  445. static int wpa_cli_cmd_stkstart(struct wpa_ctrl *ctrl, int argc,
  446. char *argv[])
  447. {
  448. char cmd[256];
  449. int res;
  450. if (argc != 1) {
  451. printf("Invalid STKSTART command: needs one argument "
  452. "(Peer STA MAC address)\n");
  453. return -1;
  454. }
  455. res = os_snprintf(cmd, sizeof(cmd), "STKSTART %s", argv[0]);
  456. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  457. printf("Too long STKSTART command.\n");
  458. return -1;
  459. }
  460. return wpa_ctrl_command(ctrl, cmd);
  461. }
  462. static int wpa_cli_cmd_ft_ds(struct wpa_ctrl *ctrl, int argc, char *argv[])
  463. {
  464. char cmd[256];
  465. int res;
  466. if (argc != 1) {
  467. printf("Invalid FT_DS command: needs one argument "
  468. "(Target AP MAC address)\n");
  469. return -1;
  470. }
  471. res = os_snprintf(cmd, sizeof(cmd), "FT_DS %s", argv[0]);
  472. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  473. printf("Too long FT_DS command.\n");
  474. return -1;
  475. }
  476. return wpa_ctrl_command(ctrl, cmd);
  477. }
  478. static int wpa_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc, char *argv[])
  479. {
  480. char cmd[256];
  481. int res;
  482. if (argc == 0) {
  483. /* Any BSSID */
  484. return wpa_ctrl_command(ctrl, "WPS_PBC");
  485. }
  486. /* Specific BSSID */
  487. res = os_snprintf(cmd, sizeof(cmd), "WPS_PBC %s", argv[0]);
  488. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  489. printf("Too long WPS_PBC command.\n");
  490. return -1;
  491. }
  492. return wpa_ctrl_command(ctrl, cmd);
  493. }
  494. static int wpa_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
  495. {
  496. char cmd[256];
  497. int res;
  498. if (argc == 0) {
  499. printf("Invalid WPS_PIN command: need one or two arguments:\n"
  500. "- BSSID: use 'any' to select any\n"
  501. "- PIN: optional, used only with devices that have no "
  502. "display\n");
  503. return -1;
  504. }
  505. if (argc == 1) {
  506. /* Use dynamically generated PIN (returned as reply) */
  507. res = os_snprintf(cmd, sizeof(cmd), "WPS_PIN %s", argv[0]);
  508. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  509. printf("Too long WPS_PIN command.\n");
  510. return -1;
  511. }
  512. return wpa_ctrl_command(ctrl, cmd);
  513. }
  514. /* Use hardcoded PIN from a label */
  515. res = os_snprintf(cmd, sizeof(cmd), "WPS_PIN %s %s", argv[0], argv[1]);
  516. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  517. printf("Too long WPS_PIN command.\n");
  518. return -1;
  519. }
  520. return wpa_ctrl_command(ctrl, cmd);
  521. }
  522. static int wpa_cli_cmd_wps_check_pin(struct wpa_ctrl *ctrl, int argc,
  523. char *argv[])
  524. {
  525. char cmd[256];
  526. int res;
  527. if (argc != 1 && argc != 2) {
  528. printf("Invalid WPS_CHECK_PIN command: needs one argument:\n"
  529. "- PIN to be verified\n");
  530. return -1;
  531. }
  532. if (argc == 2)
  533. res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s %s",
  534. argv[0], argv[1]);
  535. else
  536. res = os_snprintf(cmd, sizeof(cmd), "WPS_CHECK_PIN %s",
  537. argv[0]);
  538. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  539. printf("Too long WPS_CHECK_PIN command.\n");
  540. return -1;
  541. }
  542. return wpa_ctrl_command(ctrl, cmd);
  543. }
  544. static int wpa_cli_cmd_wps_cancel(struct wpa_ctrl *ctrl, int argc,
  545. char *argv[])
  546. {
  547. return wpa_ctrl_command(ctrl, "WPS_CANCEL");
  548. }
  549. #ifdef CONFIG_WPS_OOB
  550. static int wpa_cli_cmd_wps_oob(struct wpa_ctrl *ctrl, int argc, char *argv[])
  551. {
  552. char cmd[256];
  553. int res;
  554. if (argc != 3 && argc != 4) {
  555. printf("Invalid WPS_OOB command: need three or four "
  556. "arguments:\n"
  557. "- DEV_TYPE: use 'ufd' or 'nfc'\n"
  558. "- PATH: path of OOB device like '/mnt'\n"
  559. "- METHOD: OOB method 'pin-e' or 'pin-r', "
  560. "'cred'\n"
  561. "- DEV_NAME: (only for NFC) device name like "
  562. "'pn531'\n");
  563. return -1;
  564. }
  565. if (argc == 3)
  566. res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s",
  567. argv[0], argv[1], argv[2]);
  568. else
  569. res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s %s",
  570. argv[0], argv[1], argv[2], argv[3]);
  571. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  572. printf("Too long WPS_OOB command.\n");
  573. return -1;
  574. }
  575. return wpa_ctrl_command(ctrl, cmd);
  576. }
  577. #endif /* CONFIG_WPS_OOB */
  578. static int wpa_cli_cmd_wps_reg(struct wpa_ctrl *ctrl, int argc, char *argv[])
  579. {
  580. char cmd[256];
  581. int res;
  582. if (argc == 2)
  583. res = os_snprintf(cmd, sizeof(cmd), "WPS_REG %s %s",
  584. argv[0], argv[1]);
  585. else if (argc == 5 || argc == 6) {
  586. char ssid_hex[2 * 32 + 1];
  587. char key_hex[2 * 64 + 1];
  588. int i;
  589. ssid_hex[0] = '\0';
  590. for (i = 0; i < 32; i++) {
  591. if (argv[2][i] == '\0')
  592. break;
  593. os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
  594. }
  595. key_hex[0] = '\0';
  596. if (argc == 6) {
  597. for (i = 0; i < 64; i++) {
  598. if (argv[5][i] == '\0')
  599. break;
  600. os_snprintf(&key_hex[i * 2], 3, "%02x",
  601. argv[5][i]);
  602. }
  603. }
  604. res = os_snprintf(cmd, sizeof(cmd),
  605. "WPS_REG %s %s %s %s %s %s",
  606. argv[0], argv[1], ssid_hex, argv[3], argv[4],
  607. key_hex);
  608. } else {
  609. printf("Invalid WPS_REG command: need two arguments:\n"
  610. "- BSSID of the target AP\n"
  611. "- AP PIN\n");
  612. printf("Alternatively, six arguments can be used to "
  613. "reconfigure the AP:\n"
  614. "- BSSID of the target AP\n"
  615. "- AP PIN\n"
  616. "- new SSID\n"
  617. "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
  618. "- new encr (NONE, WEP, TKIP, CCMP)\n"
  619. "- new key\n");
  620. return -1;
  621. }
  622. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  623. printf("Too long WPS_REG command.\n");
  624. return -1;
  625. }
  626. return wpa_ctrl_command(ctrl, cmd);
  627. }
  628. static int wpa_cli_cmd_wps_ap_pin(struct wpa_ctrl *ctrl, int argc,
  629. char *argv[])
  630. {
  631. char cmd[256];
  632. int res;
  633. if (argc < 1) {
  634. printf("Invalid WPS_AP_PIN command: needs at least one "
  635. "argument\n");
  636. return -1;
  637. }
  638. if (argc > 2)
  639. res = os_snprintf(cmd, sizeof(cmd), "WPS_AP_PIN %s %s %s",
  640. argv[0], argv[1], argv[2]);
  641. else if (argc > 1)
  642. res = os_snprintf(cmd, sizeof(cmd), "WPS_AP_PIN %s %s",
  643. argv[0], argv[1]);
  644. else
  645. res = os_snprintf(cmd, sizeof(cmd), "WPS_AP_PIN %s",
  646. argv[0]);
  647. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  648. printf("Too long WPS_AP_PIN command.\n");
  649. return -1;
  650. }
  651. return wpa_ctrl_command(ctrl, cmd);
  652. }
  653. static int wpa_cli_cmd_wps_er_start(struct wpa_ctrl *ctrl, int argc,
  654. char *argv[])
  655. {
  656. char cmd[100];
  657. if (argc > 0) {
  658. os_snprintf(cmd, sizeof(cmd), "WPS_ER_START %s", argv[0]);
  659. return wpa_ctrl_command(ctrl, cmd);
  660. }
  661. return wpa_ctrl_command(ctrl, "WPS_ER_START");
  662. }
  663. static int wpa_cli_cmd_wps_er_stop(struct wpa_ctrl *ctrl, int argc,
  664. char *argv[])
  665. {
  666. return wpa_ctrl_command(ctrl, "WPS_ER_STOP");
  667. }
  668. static int wpa_cli_cmd_wps_er_pin(struct wpa_ctrl *ctrl, int argc,
  669. char *argv[])
  670. {
  671. char cmd[256];
  672. int res;
  673. if (argc < 2) {
  674. printf("Invalid WPS_ER_PIN command: need at least two "
  675. "arguments:\n"
  676. "- UUID: use 'any' to select any\n"
  677. "- PIN: Enrollee PIN\n"
  678. "optional: - Enrollee MAC address\n");
  679. return -1;
  680. }
  681. if (argc > 2)
  682. res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_PIN %s %s %s",
  683. argv[0], argv[1], argv[2]);
  684. else
  685. res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_PIN %s %s",
  686. argv[0], argv[1]);
  687. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  688. printf("Too long WPS_ER_PIN command.\n");
  689. return -1;
  690. }
  691. return wpa_ctrl_command(ctrl, cmd);
  692. }
  693. static int wpa_cli_cmd_wps_er_pbc(struct wpa_ctrl *ctrl, int argc,
  694. char *argv[])
  695. {
  696. char cmd[256];
  697. int res;
  698. if (argc != 1) {
  699. printf("Invalid WPS_ER_PBC command: need one argument:\n"
  700. "- UUID: Specify the Enrollee\n");
  701. return -1;
  702. }
  703. res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_PBC %s",
  704. argv[0]);
  705. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  706. printf("Too long WPS_ER_PBC command.\n");
  707. return -1;
  708. }
  709. return wpa_ctrl_command(ctrl, cmd);
  710. }
  711. static int wpa_cli_cmd_wps_er_learn(struct wpa_ctrl *ctrl, int argc,
  712. char *argv[])
  713. {
  714. char cmd[256];
  715. int res;
  716. if (argc != 2) {
  717. printf("Invalid WPS_ER_LEARN command: need two arguments:\n"
  718. "- UUID: specify which AP to use\n"
  719. "- PIN: AP PIN\n");
  720. return -1;
  721. }
  722. res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_LEARN %s %s",
  723. argv[0], argv[1]);
  724. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  725. printf("Too long WPS_ER_LEARN command.\n");
  726. return -1;
  727. }
  728. return wpa_ctrl_command(ctrl, cmd);
  729. }
  730. static int wpa_cli_cmd_wps_er_set_config(struct wpa_ctrl *ctrl, int argc,
  731. char *argv[])
  732. {
  733. char cmd[256];
  734. int res;
  735. if (argc != 2) {
  736. printf("Invalid WPS_ER_SET_CONFIG command: need two "
  737. "arguments:\n"
  738. "- UUID: specify which AP to use\n"
  739. "- Network configuration id\n");
  740. return -1;
  741. }
  742. res = os_snprintf(cmd, sizeof(cmd), "WPS_ER_SET_CONFIG %s %s",
  743. argv[0], argv[1]);
  744. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  745. printf("Too long WPS_ER_SET_CONFIG command.\n");
  746. return -1;
  747. }
  748. return wpa_ctrl_command(ctrl, cmd);
  749. }
  750. static int wpa_cli_cmd_wps_er_config(struct wpa_ctrl *ctrl, int argc,
  751. char *argv[])
  752. {
  753. char cmd[256];
  754. int res;
  755. if (argc == 5 || argc == 6) {
  756. char ssid_hex[2 * 32 + 1];
  757. char key_hex[2 * 64 + 1];
  758. int i;
  759. ssid_hex[0] = '\0';
  760. for (i = 0; i < 32; i++) {
  761. if (argv[2][i] == '\0')
  762. break;
  763. os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
  764. }
  765. key_hex[0] = '\0';
  766. if (argc == 6) {
  767. for (i = 0; i < 64; i++) {
  768. if (argv[5][i] == '\0')
  769. break;
  770. os_snprintf(&key_hex[i * 2], 3, "%02x",
  771. argv[5][i]);
  772. }
  773. }
  774. res = os_snprintf(cmd, sizeof(cmd),
  775. "WPS_ER_CONFIG %s %s %s %s %s %s",
  776. argv[0], argv[1], ssid_hex, argv[3], argv[4],
  777. key_hex);
  778. } else {
  779. printf("Invalid WPS_ER_CONFIG command: need six arguments:\n"
  780. "- AP UUID\n"
  781. "- AP PIN\n"
  782. "- new SSID\n"
  783. "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
  784. "- new encr (NONE, WEP, TKIP, CCMP)\n"
  785. "- new key\n");
  786. return -1;
  787. }
  788. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  789. printf("Too long WPS_ER_CONFIG command.\n");
  790. return -1;
  791. }
  792. return wpa_ctrl_command(ctrl, cmd);
  793. }
  794. static int wpa_cli_cmd_ibss_rsn(struct wpa_ctrl *ctrl, int argc, char *argv[])
  795. {
  796. char cmd[256];
  797. int res;
  798. if (argc != 1) {
  799. printf("Invalid IBSS_RSN command: needs one argument "
  800. "(Peer STA MAC address)\n");
  801. return -1;
  802. }
  803. res = os_snprintf(cmd, sizeof(cmd), "IBSS_RSN %s", argv[0]);
  804. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  805. printf("Too long IBSS_RSN command.\n");
  806. return -1;
  807. }
  808. return wpa_ctrl_command(ctrl, cmd);
  809. }
  810. static int wpa_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
  811. {
  812. char cmd[256];
  813. int res;
  814. if (argc != 1) {
  815. printf("Invalid LEVEL command: needs one argument (debug "
  816. "level)\n");
  817. return -1;
  818. }
  819. res = os_snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
  820. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  821. printf("Too long LEVEL command.\n");
  822. return -1;
  823. }
  824. return wpa_ctrl_command(ctrl, cmd);
  825. }
  826. static int wpa_cli_cmd_identity(struct wpa_ctrl *ctrl, int argc, char *argv[])
  827. {
  828. char cmd[256], *pos, *end;
  829. int i, ret;
  830. if (argc < 2) {
  831. printf("Invalid IDENTITY command: needs two arguments "
  832. "(network id and identity)\n");
  833. return -1;
  834. }
  835. end = cmd + sizeof(cmd);
  836. pos = cmd;
  837. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s",
  838. argv[0], argv[1]);
  839. if (ret < 0 || ret >= end - pos) {
  840. printf("Too long IDENTITY command.\n");
  841. return -1;
  842. }
  843. pos += ret;
  844. for (i = 2; i < argc; i++) {
  845. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  846. if (ret < 0 || ret >= end - pos) {
  847. printf("Too long IDENTITY command.\n");
  848. return -1;
  849. }
  850. pos += ret;
  851. }
  852. return wpa_ctrl_command(ctrl, cmd);
  853. }
  854. static int wpa_cli_cmd_password(struct wpa_ctrl *ctrl, int argc, char *argv[])
  855. {
  856. char cmd[256], *pos, *end;
  857. int i, ret;
  858. if (argc < 2) {
  859. printf("Invalid PASSWORD command: needs two arguments "
  860. "(network id and password)\n");
  861. return -1;
  862. }
  863. end = cmd + sizeof(cmd);
  864. pos = cmd;
  865. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s",
  866. argv[0], argv[1]);
  867. if (ret < 0 || ret >= end - pos) {
  868. printf("Too long PASSWORD command.\n");
  869. return -1;
  870. }
  871. pos += ret;
  872. for (i = 2; i < argc; i++) {
  873. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  874. if (ret < 0 || ret >= end - pos) {
  875. printf("Too long PASSWORD command.\n");
  876. return -1;
  877. }
  878. pos += ret;
  879. }
  880. return wpa_ctrl_command(ctrl, cmd);
  881. }
  882. static int wpa_cli_cmd_new_password(struct wpa_ctrl *ctrl, int argc,
  883. char *argv[])
  884. {
  885. char cmd[256], *pos, *end;
  886. int i, ret;
  887. if (argc < 2) {
  888. printf("Invalid NEW_PASSWORD command: needs two arguments "
  889. "(network id and password)\n");
  890. return -1;
  891. }
  892. end = cmd + sizeof(cmd);
  893. pos = cmd;
  894. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s",
  895. argv[0], argv[1]);
  896. if (ret < 0 || ret >= end - pos) {
  897. printf("Too long NEW_PASSWORD command.\n");
  898. return -1;
  899. }
  900. pos += ret;
  901. for (i = 2; i < argc; i++) {
  902. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  903. if (ret < 0 || ret >= end - pos) {
  904. printf("Too long NEW_PASSWORD command.\n");
  905. return -1;
  906. }
  907. pos += ret;
  908. }
  909. return wpa_ctrl_command(ctrl, cmd);
  910. }
  911. static int wpa_cli_cmd_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
  912. {
  913. char cmd[256], *pos, *end;
  914. int i, ret;
  915. if (argc < 2) {
  916. printf("Invalid PIN command: needs two arguments "
  917. "(network id and pin)\n");
  918. return -1;
  919. }
  920. end = cmd + sizeof(cmd);
  921. pos = cmd;
  922. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s",
  923. argv[0], argv[1]);
  924. if (ret < 0 || ret >= end - pos) {
  925. printf("Too long PIN command.\n");
  926. return -1;
  927. }
  928. pos += ret;
  929. for (i = 2; i < argc; i++) {
  930. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  931. if (ret < 0 || ret >= end - pos) {
  932. printf("Too long PIN command.\n");
  933. return -1;
  934. }
  935. pos += ret;
  936. }
  937. return wpa_ctrl_command(ctrl, cmd);
  938. }
  939. static int wpa_cli_cmd_otp(struct wpa_ctrl *ctrl, int argc, char *argv[])
  940. {
  941. char cmd[256], *pos, *end;
  942. int i, ret;
  943. if (argc < 2) {
  944. printf("Invalid OTP command: needs two arguments (network "
  945. "id and password)\n");
  946. return -1;
  947. }
  948. end = cmd + sizeof(cmd);
  949. pos = cmd;
  950. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s",
  951. argv[0], argv[1]);
  952. if (ret < 0 || ret >= end - pos) {
  953. printf("Too long OTP command.\n");
  954. return -1;
  955. }
  956. pos += ret;
  957. for (i = 2; i < argc; i++) {
  958. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  959. if (ret < 0 || ret >= end - pos) {
  960. printf("Too long OTP command.\n");
  961. return -1;
  962. }
  963. pos += ret;
  964. }
  965. return wpa_ctrl_command(ctrl, cmd);
  966. }
  967. static int wpa_cli_cmd_passphrase(struct wpa_ctrl *ctrl, int argc,
  968. char *argv[])
  969. {
  970. char cmd[256], *pos, *end;
  971. int i, ret;
  972. if (argc < 2) {
  973. printf("Invalid PASSPHRASE command: needs two arguments "
  974. "(network id and passphrase)\n");
  975. return -1;
  976. }
  977. end = cmd + sizeof(cmd);
  978. pos = cmd;
  979. ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s",
  980. argv[0], argv[1]);
  981. if (ret < 0 || ret >= end - pos) {
  982. printf("Too long PASSPHRASE command.\n");
  983. return -1;
  984. }
  985. pos += ret;
  986. for (i = 2; i < argc; i++) {
  987. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  988. if (ret < 0 || ret >= end - pos) {
  989. printf("Too long PASSPHRASE command.\n");
  990. return -1;
  991. }
  992. pos += ret;
  993. }
  994. return wpa_ctrl_command(ctrl, cmd);
  995. }
  996. static int wpa_cli_cmd_bssid(struct wpa_ctrl *ctrl, int argc, char *argv[])
  997. {
  998. char cmd[256], *pos, *end;
  999. int i, ret;
  1000. if (argc < 2) {
  1001. printf("Invalid BSSID command: needs two arguments (network "
  1002. "id and BSSID)\n");
  1003. return -1;
  1004. }
  1005. end = cmd + sizeof(cmd);
  1006. pos = cmd;
  1007. ret = os_snprintf(pos, end - pos, "BSSID");
  1008. if (ret < 0 || ret >= end - pos) {
  1009. printf("Too long BSSID command.\n");
  1010. return -1;
  1011. }
  1012. pos += ret;
  1013. for (i = 0; i < argc; i++) {
  1014. ret = os_snprintf(pos, end - pos, " %s", argv[i]);
  1015. if (ret < 0 || ret >= end - pos) {
  1016. printf("Too long BSSID command.\n");
  1017. return -1;
  1018. }
  1019. pos += ret;
  1020. }
  1021. return wpa_ctrl_command(ctrl, cmd);
  1022. }
  1023. static int wpa_cli_cmd_list_networks(struct wpa_ctrl *ctrl, int argc,
  1024. char *argv[])
  1025. {
  1026. return wpa_ctrl_command(ctrl, "LIST_NETWORKS");
  1027. }
  1028. static int wpa_cli_cmd_select_network(struct wpa_ctrl *ctrl, int argc,
  1029. char *argv[])
  1030. {
  1031. char cmd[32];
  1032. int res;
  1033. if (argc < 1) {
  1034. printf("Invalid SELECT_NETWORK command: needs one argument "
  1035. "(network id)\n");
  1036. return -1;
  1037. }
  1038. res = os_snprintf(cmd, sizeof(cmd), "SELECT_NETWORK %s", argv[0]);
  1039. if (res < 0 || (size_t) res >= sizeof(cmd))
  1040. return -1;
  1041. cmd[sizeof(cmd) - 1] = '\0';
  1042. return wpa_ctrl_command(ctrl, cmd);
  1043. }
  1044. static int wpa_cli_cmd_enable_network(struct wpa_ctrl *ctrl, int argc,
  1045. char *argv[])
  1046. {
  1047. char cmd[32];
  1048. int res;
  1049. if (argc < 1) {
  1050. printf("Invalid ENABLE_NETWORK command: needs one argument "
  1051. "(network id)\n");
  1052. return -1;
  1053. }
  1054. res = os_snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %s", argv[0]);
  1055. if (res < 0 || (size_t) res >= sizeof(cmd))
  1056. return -1;
  1057. cmd[sizeof(cmd) - 1] = '\0';
  1058. return wpa_ctrl_command(ctrl, cmd);
  1059. }
  1060. static int wpa_cli_cmd_disable_network(struct wpa_ctrl *ctrl, int argc,
  1061. char *argv[])
  1062. {
  1063. char cmd[32];
  1064. int res;
  1065. if (argc < 1) {
  1066. printf("Invalid DISABLE_NETWORK command: needs one argument "
  1067. "(network id)\n");
  1068. return -1;
  1069. }
  1070. res = os_snprintf(cmd, sizeof(cmd), "DISABLE_NETWORK %s", argv[0]);
  1071. if (res < 0 || (size_t) res >= sizeof(cmd))
  1072. return -1;
  1073. cmd[sizeof(cmd) - 1] = '\0';
  1074. return wpa_ctrl_command(ctrl, cmd);
  1075. }
  1076. static int wpa_cli_cmd_add_network(struct wpa_ctrl *ctrl, int argc,
  1077. char *argv[])
  1078. {
  1079. return wpa_ctrl_command(ctrl, "ADD_NETWORK");
  1080. }
  1081. static int wpa_cli_cmd_remove_network(struct wpa_ctrl *ctrl, int argc,
  1082. char *argv[])
  1083. {
  1084. char cmd[32];
  1085. int res;
  1086. if (argc < 1) {
  1087. printf("Invalid REMOVE_NETWORK command: needs one argument "
  1088. "(network id)\n");
  1089. return -1;
  1090. }
  1091. res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %s", argv[0]);
  1092. if (res < 0 || (size_t) res >= sizeof(cmd))
  1093. return -1;
  1094. cmd[sizeof(cmd) - 1] = '\0';
  1095. return wpa_ctrl_command(ctrl, cmd);
  1096. }
  1097. static void wpa_cli_show_network_variables(void)
  1098. {
  1099. printf("set_network variables:\n"
  1100. " ssid (network name, SSID)\n"
  1101. " psk (WPA passphrase or pre-shared key)\n"
  1102. " key_mgmt (key management protocol)\n"
  1103. " identity (EAP identity)\n"
  1104. " password (EAP password)\n"
  1105. " ...\n"
  1106. "\n"
  1107. "Note: Values are entered in the same format as the "
  1108. "configuration file is using,\n"
  1109. "i.e., strings values need to be inside double quotation "
  1110. "marks.\n"
  1111. "For example: set_network 1 ssid \"network name\"\n"
  1112. "\n"
  1113. "Please see wpa_supplicant.conf documentation for full list "
  1114. "of\navailable variables.\n");
  1115. }
  1116. static int wpa_cli_cmd_set_network(struct wpa_ctrl *ctrl, int argc,
  1117. char *argv[])
  1118. {
  1119. char cmd[256];
  1120. int res;
  1121. if (argc == 0) {
  1122. wpa_cli_show_network_variables();
  1123. return 0;
  1124. }
  1125. if (argc != 3) {
  1126. printf("Invalid SET_NETWORK command: needs three arguments\n"
  1127. "(network id, variable name, and value)\n");
  1128. return -1;
  1129. }
  1130. res = os_snprintf(cmd, sizeof(cmd), "SET_NETWORK %s %s %s",
  1131. argv[0], argv[1], argv[2]);
  1132. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  1133. printf("Too long SET_NETWORK command.\n");
  1134. return -1;
  1135. }
  1136. return wpa_ctrl_command(ctrl, cmd);
  1137. }
  1138. static int wpa_cli_cmd_get_network(struct wpa_ctrl *ctrl, int argc,
  1139. char *argv[])
  1140. {
  1141. char cmd[256];
  1142. int res;
  1143. if (argc == 0) {
  1144. wpa_cli_show_network_variables();
  1145. return 0;
  1146. }
  1147. if (argc != 2) {
  1148. printf("Invalid GET_NETWORK command: needs two arguments\n"
  1149. "(network id and variable name)\n");
  1150. return -1;
  1151. }
  1152. res = os_snprintf(cmd, sizeof(cmd), "GET_NETWORK %s %s",
  1153. argv[0], argv[1]);
  1154. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  1155. printf("Too long GET_NETWORK command.\n");
  1156. return -1;
  1157. }
  1158. return wpa_ctrl_command(ctrl, cmd);
  1159. }
  1160. static int wpa_cli_cmd_disconnect(struct wpa_ctrl *ctrl, int argc,
  1161. char *argv[])
  1162. {
  1163. return wpa_ctrl_command(ctrl, "DISCONNECT");
  1164. }
  1165. static int wpa_cli_cmd_reconnect(struct wpa_ctrl *ctrl, int argc,
  1166. char *argv[])
  1167. {
  1168. return wpa_ctrl_command(ctrl, "RECONNECT");
  1169. }
  1170. static int wpa_cli_cmd_save_config(struct wpa_ctrl *ctrl, int argc,
  1171. char *argv[])
  1172. {
  1173. return wpa_ctrl_command(ctrl, "SAVE_CONFIG");
  1174. }
  1175. static int wpa_cli_cmd_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1176. {
  1177. return wpa_ctrl_command(ctrl, "SCAN");
  1178. }
  1179. static int wpa_cli_cmd_scan_results(struct wpa_ctrl *ctrl, int argc,
  1180. char *argv[])
  1181. {
  1182. return wpa_ctrl_command(ctrl, "SCAN_RESULTS");
  1183. }
  1184. static int wpa_cli_cmd_bss(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1185. {
  1186. char cmd[64];
  1187. int res;
  1188. if (argc != 1) {
  1189. printf("Invalid BSS command: need one argument (index or "
  1190. "BSSID)\n");
  1191. return -1;
  1192. }
  1193. res = os_snprintf(cmd, sizeof(cmd), "BSS %s", argv[0]);
  1194. if (res < 0 || (size_t) res >= sizeof(cmd))
  1195. return -1;
  1196. cmd[sizeof(cmd) - 1] = '\0';
  1197. return wpa_ctrl_command(ctrl, cmd);
  1198. }
  1199. static int wpa_cli_cmd_get_capability(struct wpa_ctrl *ctrl, int argc,
  1200. char *argv[])
  1201. {
  1202. char cmd[64];
  1203. int res;
  1204. if (argc < 1 || argc > 2) {
  1205. printf("Invalid GET_CAPABILITY command: need either one or "
  1206. "two arguments\n");
  1207. return -1;
  1208. }
  1209. if ((argc == 2) && os_strcmp(argv[1], "strict") != 0) {
  1210. printf("Invalid GET_CAPABILITY command: second argument, "
  1211. "if any, must be 'strict'\n");
  1212. return -1;
  1213. }
  1214. res = os_snprintf(cmd, sizeof(cmd), "GET_CAPABILITY %s%s", argv[0],
  1215. (argc == 2) ? " strict" : "");
  1216. if (res < 0 || (size_t) res >= sizeof(cmd))
  1217. return -1;
  1218. cmd[sizeof(cmd) - 1] = '\0';
  1219. return wpa_ctrl_command(ctrl, cmd);
  1220. }
  1221. static int wpa_cli_list_interfaces(struct wpa_ctrl *ctrl)
  1222. {
  1223. printf("Available interfaces:\n");
  1224. return wpa_ctrl_command(ctrl, "INTERFACES");
  1225. }
  1226. static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1227. {
  1228. if (argc < 1) {
  1229. wpa_cli_list_interfaces(ctrl);
  1230. return 0;
  1231. }
  1232. wpa_cli_close_connection();
  1233. os_free(ctrl_ifname);
  1234. ctrl_ifname = os_strdup(argv[0]);
  1235. if (wpa_cli_open_connection(ctrl_ifname, 1)) {
  1236. printf("Connected to interface '%s.\n", ctrl_ifname);
  1237. } else {
  1238. printf("Could not connect to interface '%s' - re-trying\n",
  1239. ctrl_ifname);
  1240. }
  1241. return 0;
  1242. }
  1243. static int wpa_cli_cmd_reconfigure(struct wpa_ctrl *ctrl, int argc,
  1244. char *argv[])
  1245. {
  1246. return wpa_ctrl_command(ctrl, "RECONFIGURE");
  1247. }
  1248. static int wpa_cli_cmd_terminate(struct wpa_ctrl *ctrl, int argc,
  1249. char *argv[])
  1250. {
  1251. return wpa_ctrl_command(ctrl, "TERMINATE");
  1252. }
  1253. static int wpa_cli_cmd_interface_add(struct wpa_ctrl *ctrl, int argc,
  1254. char *argv[])
  1255. {
  1256. char cmd[256];
  1257. int res;
  1258. if (argc < 1) {
  1259. printf("Invalid INTERFACE_ADD command: needs at least one "
  1260. "argument (interface name)\n"
  1261. "All arguments: ifname confname driver ctrl_interface "
  1262. "driver_param bridge_name\n");
  1263. return -1;
  1264. }
  1265. /*
  1266. * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
  1267. * <driver_param>TAB<bridge_name>
  1268. */
  1269. res = os_snprintf(cmd, sizeof(cmd),
  1270. "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
  1271. argv[0],
  1272. argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "",
  1273. argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "",
  1274. argc > 5 ? argv[5] : "");
  1275. if (res < 0 || (size_t) res >= sizeof(cmd))
  1276. return -1;
  1277. cmd[sizeof(cmd) - 1] = '\0';
  1278. return wpa_ctrl_command(ctrl, cmd);
  1279. }
  1280. static int wpa_cli_cmd_interface_remove(struct wpa_ctrl *ctrl, int argc,
  1281. char *argv[])
  1282. {
  1283. char cmd[128];
  1284. int res;
  1285. if (argc != 1) {
  1286. printf("Invalid INTERFACE_REMOVE command: needs one argument "
  1287. "(interface name)\n");
  1288. return -1;
  1289. }
  1290. res = os_snprintf(cmd, sizeof(cmd), "INTERFACE_REMOVE %s", argv[0]);
  1291. if (res < 0 || (size_t) res >= sizeof(cmd))
  1292. return -1;
  1293. cmd[sizeof(cmd) - 1] = '\0';
  1294. return wpa_ctrl_command(ctrl, cmd);
  1295. }
  1296. static int wpa_cli_cmd_interface_list(struct wpa_ctrl *ctrl, int argc,
  1297. char *argv[])
  1298. {
  1299. return wpa_ctrl_command(ctrl, "INTERFACE_LIST");
  1300. }
  1301. #ifdef CONFIG_AP
  1302. static int wpa_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1303. {
  1304. char buf[64];
  1305. if (argc != 1) {
  1306. printf("Invalid 'sta' command - exactly one argument, STA "
  1307. "address, is required.\n");
  1308. return -1;
  1309. }
  1310. os_snprintf(buf, sizeof(buf), "STA %s", argv[0]);
  1311. return wpa_ctrl_command(ctrl, buf);
  1312. }
  1313. static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
  1314. char *addr, size_t addr_len)
  1315. {
  1316. char buf[4096], *pos;
  1317. size_t len;
  1318. int ret;
  1319. if (ctrl_conn == NULL) {
  1320. printf("Not connected to hostapd - command dropped.\n");
  1321. return -1;
  1322. }
  1323. len = sizeof(buf) - 1;
  1324. ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
  1325. wpa_cli_msg_cb);
  1326. if (ret == -2) {
  1327. printf("'%s' command timed out.\n", cmd);
  1328. return -2;
  1329. } else if (ret < 0) {
  1330. printf("'%s' command failed.\n", cmd);
  1331. return -1;
  1332. }
  1333. buf[len] = '\0';
  1334. if (memcmp(buf, "FAIL", 4) == 0)
  1335. return -1;
  1336. printf("%s", buf);
  1337. pos = buf;
  1338. while (*pos != '\0' && *pos != '\n')
  1339. pos++;
  1340. *pos = '\0';
  1341. os_strlcpy(addr, buf, addr_len);
  1342. return 0;
  1343. }
  1344. static int wpa_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1345. {
  1346. char addr[32], cmd[64];
  1347. if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
  1348. return 0;
  1349. do {
  1350. os_snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
  1351. } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
  1352. return -1;
  1353. }
  1354. #endif /* CONFIG_AP */
  1355. static int wpa_cli_cmd_suspend(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1356. {
  1357. return wpa_ctrl_command(ctrl, "SUSPEND");
  1358. }
  1359. static int wpa_cli_cmd_resume(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1360. {
  1361. return wpa_ctrl_command(ctrl, "RESUME");
  1362. }
  1363. static int wpa_cli_cmd_drop_sa(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1364. {
  1365. return wpa_ctrl_command(ctrl, "DROP_SA");
  1366. }
  1367. static int wpa_cli_cmd_roam(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1368. {
  1369. char cmd[128];
  1370. int res;
  1371. if (argc != 1) {
  1372. printf("Invalid ROAM command: needs one argument "
  1373. "(target AP's BSSID)\n");
  1374. return -1;
  1375. }
  1376. res = os_snprintf(cmd, sizeof(cmd), "ROAM %s", argv[0]);
  1377. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  1378. printf("Too long ROAM command.\n");
  1379. return -1;
  1380. }
  1381. return wpa_ctrl_command(ctrl, cmd);
  1382. }
  1383. #ifdef CONFIG_P2P
  1384. static int wpa_cli_cmd_p2p_find(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1385. {
  1386. char cmd[128];
  1387. int res;
  1388. if (argc == 0)
  1389. return wpa_ctrl_command(ctrl, "P2P_FIND");
  1390. if (argc > 1)
  1391. res = os_snprintf(cmd, sizeof(cmd), "P2P_FIND %s %s",
  1392. argv[0], argv[1]);
  1393. else
  1394. res = os_snprintf(cmd, sizeof(cmd), "P2P_FIND %s", argv[0]);
  1395. if (res < 0 || (size_t) res >= sizeof(cmd))
  1396. return -1;
  1397. cmd[sizeof(cmd) - 1] = '\0';
  1398. return wpa_ctrl_command(ctrl, cmd);
  1399. }
  1400. static int wpa_cli_cmd_p2p_stop_find(struct wpa_ctrl *ctrl, int argc,
  1401. char *argv[])
  1402. {
  1403. return wpa_ctrl_command(ctrl, "P2P_STOP_FIND");
  1404. }
  1405. static int wpa_cli_cmd_p2p_connect(struct wpa_ctrl *ctrl, int argc,
  1406. char *argv[])
  1407. {
  1408. char cmd[128];
  1409. int res;
  1410. if (argc < 2) {
  1411. printf("Invalid P2P_CONNECT command: needs at least two "
  1412. "arguments (address and pbc/PIN)\n");
  1413. return -1;
  1414. }
  1415. if (argc > 4)
  1416. res = os_snprintf(cmd, sizeof(cmd),
  1417. "P2P_CONNECT %s %s %s %s %s",
  1418. argv[0], argv[1], argv[2], argv[3],
  1419. argv[4]);
  1420. else if (argc > 3)
  1421. res = os_snprintf(cmd, sizeof(cmd), "P2P_CONNECT %s %s %s %s",
  1422. argv[0], argv[1], argv[2], argv[3]);
  1423. else if (argc > 2)
  1424. res = os_snprintf(cmd, sizeof(cmd), "P2P_CONNECT %s %s %s",
  1425. argv[0], argv[1], argv[2]);
  1426. else
  1427. res = os_snprintf(cmd, sizeof(cmd), "P2P_CONNECT %s %s",
  1428. argv[0], argv[1]);
  1429. if (res < 0 || (size_t) res >= sizeof(cmd))
  1430. return -1;
  1431. cmd[sizeof(cmd) - 1] = '\0';
  1432. return wpa_ctrl_command(ctrl, cmd);
  1433. }
  1434. static int wpa_cli_cmd_p2p_listen(struct wpa_ctrl *ctrl, int argc,
  1435. char *argv[])
  1436. {
  1437. char cmd[128];
  1438. int res;
  1439. if (argc == 0)
  1440. return wpa_ctrl_command(ctrl, "P2P_LISTEN");
  1441. res = os_snprintf(cmd, sizeof(cmd), "P2P_LISTEN %s", argv[0]);
  1442. if (res < 0 || (size_t) res >= sizeof(cmd))
  1443. return -1;
  1444. cmd[sizeof(cmd) - 1] = '\0';
  1445. return wpa_ctrl_command(ctrl, cmd);
  1446. }
  1447. static int wpa_cli_cmd_p2p_group_remove(struct wpa_ctrl *ctrl, int argc,
  1448. char *argv[])
  1449. {
  1450. char cmd[128];
  1451. int res;
  1452. if (argc != 1) {
  1453. printf("Invalid P2P_GROUP_REMOVE command: needs one argument "
  1454. "(interface name)\n");
  1455. return -1;
  1456. }
  1457. res = os_snprintf(cmd, sizeof(cmd), "P2P_GROUP_REMOVE %s", argv[0]);
  1458. if (res < 0 || (size_t) res >= sizeof(cmd))
  1459. return -1;
  1460. cmd[sizeof(cmd) - 1] = '\0';
  1461. return wpa_ctrl_command(ctrl, cmd);
  1462. }
  1463. static int wpa_cli_cmd_p2p_group_add(struct wpa_ctrl *ctrl, int argc,
  1464. char *argv[])
  1465. {
  1466. char cmd[128];
  1467. int res;
  1468. if (argc == 0)
  1469. return wpa_ctrl_command(ctrl, "P2P_GROUP_ADD");
  1470. res = os_snprintf(cmd, sizeof(cmd), "P2P_GROUP_ADD %s", argv[0]);
  1471. if (res < 0 || (size_t) res >= sizeof(cmd))
  1472. return -1;
  1473. cmd[sizeof(cmd) - 1] = '\0';
  1474. return wpa_ctrl_command(ctrl, cmd);
  1475. }
  1476. static int wpa_cli_cmd_p2p_prov_disc(struct wpa_ctrl *ctrl, int argc,
  1477. char *argv[])
  1478. {
  1479. char cmd[128];
  1480. int res;
  1481. if (argc != 2) {
  1482. printf("Invalid P2P_PROV_DISC command: needs two arguments "
  1483. "(address and config method\n"
  1484. "(display, keypad, or pbc)\n");
  1485. return -1;
  1486. }
  1487. res = os_snprintf(cmd, sizeof(cmd), "P2P_PROV_DISC %s %s",
  1488. argv[0], argv[1]);
  1489. if (res < 0 || (size_t) res >= sizeof(cmd))
  1490. return -1;
  1491. cmd[sizeof(cmd) - 1] = '\0';
  1492. return wpa_ctrl_command(ctrl, cmd);
  1493. }
  1494. static int wpa_cli_cmd_p2p_get_passphrase(struct wpa_ctrl *ctrl, int argc,
  1495. char *argv[])
  1496. {
  1497. return wpa_ctrl_command(ctrl, "P2P_GET_PASSPHRASE");
  1498. }
  1499. static int wpa_cli_cmd_p2p_serv_disc_req(struct wpa_ctrl *ctrl, int argc,
  1500. char *argv[])
  1501. {
  1502. char cmd[4096];
  1503. int res;
  1504. if (argc != 2 && argc != 4) {
  1505. printf("Invalid P2P_SERV_DISC_REQ command: needs two "
  1506. "arguments (address and TLVs) or four arguments "
  1507. "(address, \"upnp\", version, search target "
  1508. "(SSDP ST:)\n");
  1509. return -1;
  1510. }
  1511. if (argc == 4)
  1512. res = os_snprintf(cmd, sizeof(cmd),
  1513. "P2P_SERV_DISC_REQ %s %s %s %s",
  1514. argv[0], argv[1], argv[2], argv[3]);
  1515. else
  1516. res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_REQ %s %s",
  1517. argv[0], argv[1]);
  1518. if (res < 0 || (size_t) res >= sizeof(cmd))
  1519. return -1;
  1520. cmd[sizeof(cmd) - 1] = '\0';
  1521. return wpa_ctrl_command(ctrl, cmd);
  1522. }
  1523. static int wpa_cli_cmd_p2p_serv_disc_cancel_req(struct wpa_ctrl *ctrl,
  1524. int argc, char *argv[])
  1525. {
  1526. char cmd[128];
  1527. int res;
  1528. if (argc != 1) {
  1529. printf("Invalid P2P_SERV_DISC_CANCEL_REQ command: needs one "
  1530. "argument (pending request identifier)\n");
  1531. return -1;
  1532. }
  1533. res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_CANCEL_REQ %s",
  1534. argv[0]);
  1535. if (res < 0 || (size_t) res >= sizeof(cmd))
  1536. return -1;
  1537. cmd[sizeof(cmd) - 1] = '\0';
  1538. return wpa_ctrl_command(ctrl, cmd);
  1539. }
  1540. static int wpa_cli_cmd_p2p_serv_disc_resp(struct wpa_ctrl *ctrl, int argc,
  1541. char *argv[])
  1542. {
  1543. char cmd[4096];
  1544. int res;
  1545. if (argc != 4) {
  1546. printf("Invalid P2P_SERV_DISC_RESP command: needs four "
  1547. "arguments (freq, address, dialog token, and TLVs)\n");
  1548. return -1;
  1549. }
  1550. res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_RESP %s %s %s %s",
  1551. argv[0], argv[1], argv[2], argv[3]);
  1552. if (res < 0 || (size_t) res >= sizeof(cmd))
  1553. return -1;
  1554. cmd[sizeof(cmd) - 1] = '\0';
  1555. return wpa_ctrl_command(ctrl, cmd);
  1556. }
  1557. static int wpa_cli_cmd_p2p_service_update(struct wpa_ctrl *ctrl, int argc,
  1558. char *argv[])
  1559. {
  1560. return wpa_ctrl_command(ctrl, "P2P_SERVICE_UPDATE");
  1561. }
  1562. static int wpa_cli_cmd_p2p_serv_disc_external(struct wpa_ctrl *ctrl,
  1563. int argc, char *argv[])
  1564. {
  1565. char cmd[128];
  1566. int res;
  1567. if (argc != 1) {
  1568. printf("Invalid P2P_SERV_DISC_EXTERNAL command: needs one "
  1569. "argument (external processing: 0/1)\n");
  1570. return -1;
  1571. }
  1572. res = os_snprintf(cmd, sizeof(cmd), "P2P_SERV_DISC_EXTERNAL %s",
  1573. argv[0]);
  1574. if (res < 0 || (size_t) res >= sizeof(cmd))
  1575. return -1;
  1576. cmd[sizeof(cmd) - 1] = '\0';
  1577. return wpa_ctrl_command(ctrl, cmd);
  1578. }
  1579. static int wpa_cli_cmd_p2p_service_flush(struct wpa_ctrl *ctrl, int argc,
  1580. char *argv[])
  1581. {
  1582. return wpa_ctrl_command(ctrl, "P2P_SERVICE_FLUSH");
  1583. }
  1584. static int wpa_cli_cmd_p2p_service_add(struct wpa_ctrl *ctrl, int argc,
  1585. char *argv[])
  1586. {
  1587. char cmd[4096];
  1588. int res;
  1589. if (argc != 3 && argc != 4) {
  1590. printf("Invalid P2P_SERVICE_ADD command: needs three or four "
  1591. "arguments\n");
  1592. return -1;
  1593. }
  1594. if (argc == 4)
  1595. res = os_snprintf(cmd, sizeof(cmd),
  1596. "P2P_SERVICE_ADD %s %s %s %s",
  1597. argv[0], argv[1], argv[2], argv[3]);
  1598. else
  1599. res = os_snprintf(cmd, sizeof(cmd),
  1600. "P2P_SERVICE_ADD %s %s %s",
  1601. argv[0], argv[1], argv[2]);
  1602. if (res < 0 || (size_t) res >= sizeof(cmd))
  1603. return -1;
  1604. cmd[sizeof(cmd) - 1] = '\0';
  1605. return wpa_ctrl_command(ctrl, cmd);
  1606. }
  1607. static int wpa_cli_cmd_p2p_service_del(struct wpa_ctrl *ctrl, int argc,
  1608. char *argv[])
  1609. {
  1610. char cmd[4096];
  1611. int res;
  1612. if (argc != 2 && argc != 3) {
  1613. printf("Invalid P2P_SERVICE_DEL command: needs two or three "
  1614. "arguments\n");
  1615. return -1;
  1616. }
  1617. if (argc == 3)
  1618. res = os_snprintf(cmd, sizeof(cmd),
  1619. "P2P_SERVICE_DEL %s %s %s",
  1620. argv[0], argv[1], argv[2]);
  1621. else
  1622. res = os_snprintf(cmd, sizeof(cmd),
  1623. "P2P_SERVICE_DEL %s %s",
  1624. argv[0], argv[1]);
  1625. if (res < 0 || (size_t) res >= sizeof(cmd))
  1626. return -1;
  1627. cmd[sizeof(cmd) - 1] = '\0';
  1628. return wpa_ctrl_command(ctrl, cmd);
  1629. }
  1630. static int wpa_cli_cmd_p2p_reject(struct wpa_ctrl *ctrl,
  1631. int argc, char *argv[])
  1632. {
  1633. char cmd[128];
  1634. int res;
  1635. if (argc != 1) {
  1636. printf("Invalid P2P_REJECT command: needs one argument "
  1637. "(peer address)\n");
  1638. return -1;
  1639. }
  1640. res = os_snprintf(cmd, sizeof(cmd), "P2P_REJECT %s", argv[0]);
  1641. if (res < 0 || (size_t) res >= sizeof(cmd))
  1642. return -1;
  1643. cmd[sizeof(cmd) - 1] = '\0';
  1644. return wpa_ctrl_command(ctrl, cmd);
  1645. }
  1646. static int wpa_cli_cmd_p2p_invite(struct wpa_ctrl *ctrl,
  1647. int argc, char *argv[])
  1648. {
  1649. char cmd[128];
  1650. int res;
  1651. if (argc < 1) {
  1652. printf("Invalid P2P_INVITE command: needs at least one "
  1653. "argument\n");
  1654. return -1;
  1655. }
  1656. if (argc > 2)
  1657. res = os_snprintf(cmd, sizeof(cmd), "P2P_INVITE %s %s %s",
  1658. argv[0], argv[1], argv[2]);
  1659. else if (argc > 1)
  1660. res = os_snprintf(cmd, sizeof(cmd), "P2P_INVITE %s %s",
  1661. argv[0], argv[1]);
  1662. else
  1663. res = os_snprintf(cmd, sizeof(cmd), "P2P_INVITE %s", argv[0]);
  1664. if (res < 0 || (size_t) res >= sizeof(cmd))
  1665. return -1;
  1666. cmd[sizeof(cmd) - 1] = '\0';
  1667. return wpa_ctrl_command(ctrl, cmd);
  1668. }
  1669. static int wpa_cli_cmd_p2p_peer(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1670. {
  1671. char buf[64];
  1672. if (argc != 1) {
  1673. printf("Invalid 'p2p_peer' command - exactly one argument, "
  1674. "P2P peer device address, is required.\n");
  1675. return -1;
  1676. }
  1677. os_snprintf(buf, sizeof(buf), "P2P_PEER %s", argv[0]);
  1678. return wpa_ctrl_command(ctrl, buf);
  1679. }
  1680. static int wpa_ctrl_command_p2p_peer(struct wpa_ctrl *ctrl, char *cmd,
  1681. char *addr, size_t addr_len,
  1682. int discovered)
  1683. {
  1684. char buf[4096], *pos;
  1685. size_t len;
  1686. int ret;
  1687. if (ctrl_conn == NULL)
  1688. return -1;
  1689. len = sizeof(buf) - 1;
  1690. ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
  1691. wpa_cli_msg_cb);
  1692. if (ret == -2) {
  1693. printf("'%s' command timed out.\n", cmd);
  1694. return -2;
  1695. } else if (ret < 0) {
  1696. printf("'%s' command failed.\n", cmd);
  1697. return -1;
  1698. }
  1699. buf[len] = '\0';
  1700. if (memcmp(buf, "FAIL", 4) == 0)
  1701. return -1;
  1702. pos = buf;
  1703. while (*pos != '\0' && *pos != '\n')
  1704. pos++;
  1705. *pos++ = '\0';
  1706. os_strlcpy(addr, buf, addr_len);
  1707. if (!discovered || os_strstr(pos, "[PROBE_REQ_ONLY]") == NULL)
  1708. printf("%s\n", addr);
  1709. return 0;
  1710. }
  1711. static int wpa_cli_cmd_p2p_peers(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1712. {
  1713. char addr[32], cmd[64];
  1714. int discovered;
  1715. discovered = argc > 0 && os_strcmp(argv[0], "discovered") == 0;
  1716. if (wpa_ctrl_command_p2p_peer(ctrl, "P2P_PEER FIRST",
  1717. addr, sizeof(addr), discovered))
  1718. return 0;
  1719. do {
  1720. os_snprintf(cmd, sizeof(cmd), "P2P_PEER NEXT-%s", addr);
  1721. } while (wpa_ctrl_command_p2p_peer(ctrl, cmd, addr, sizeof(addr),
  1722. discovered) == 0);
  1723. return -1;
  1724. }
  1725. static int wpa_cli_cmd_p2p_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1726. {
  1727. char cmd[100];
  1728. int res;
  1729. if (argc != 2) {
  1730. printf("Invalid P2P_SET command: needs two arguments (field, "
  1731. "value)\n");
  1732. return -1;
  1733. }
  1734. res = os_snprintf(cmd, sizeof(cmd), "P2P_SET %s %s", argv[0], argv[1]);
  1735. if (res < 0 || (size_t) res >= sizeof(cmd))
  1736. return -1;
  1737. cmd[sizeof(cmd) - 1] = '\0';
  1738. return wpa_ctrl_command(ctrl, cmd);
  1739. }
  1740. static int wpa_cli_cmd_p2p_flush(struct wpa_ctrl *ctrl, int argc, char *argv[])
  1741. {
  1742. return wpa_ctrl_command(ctrl, "P2P_FLUSH");
  1743. }
  1744. static int wpa_cli_cmd_p2p_cancel(struct wpa_ctrl *ctrl, int argc,
  1745. char *argv[])
  1746. {
  1747. return wpa_ctrl_command(ctrl, "P2P_CANCEL");
  1748. }
  1749. static int wpa_cli_cmd_p2p_unauthorize(struct wpa_ctrl *ctrl, int argc,
  1750. char *argv[])
  1751. {
  1752. char cmd[100];
  1753. int res;
  1754. if (argc != 1) {
  1755. printf("Invalid P2P_UNAUTHORIZE command: needs one argument "
  1756. "(peer address)\n");
  1757. return -1;
  1758. }
  1759. res = os_snprintf(cmd, sizeof(cmd), "P2P_UNAUTHORIZE %s", argv[0]);
  1760. if (res < 0 || (size_t) res >= sizeof(cmd))
  1761. return -1;
  1762. cmd[sizeof(cmd) - 1] = '\0';
  1763. return wpa_ctrl_command(ctrl, cmd);
  1764. }
  1765. static int wpa_cli_cmd_p2p_presence_req(struct wpa_ctrl *ctrl, int argc,
  1766. char *argv[])
  1767. {
  1768. char cmd[100];
  1769. int res;
  1770. if (argc != 0 && argc != 2 && argc != 4) {
  1771. printf("Invalid P2P_PRESENCE_REQ command: needs two arguments "
  1772. "(preferred duration, interval; in microsecods).\n"
  1773. "Optional second pair can be used to provide "
  1774. "acceptable values.\n");
  1775. return -1;
  1776. }
  1777. if (argc == 4)
  1778. res = os_snprintf(cmd, sizeof(cmd),
  1779. "P2P_PRESENCE_REQ %s %s %s %s",
  1780. argv[0], argv[1], argv[2], argv[3]);
  1781. else if (argc == 2)
  1782. res = os_snprintf(cmd, sizeof(cmd), "P2P_PRESENCE_REQ %s %s",
  1783. argv[0], argv[1]);
  1784. else
  1785. res = os_snprintf(cmd, sizeof(cmd), "P2P_PRESENCE_REQ");
  1786. if (res < 0 || (size_t) res >= sizeof(cmd))
  1787. return -1;
  1788. cmd[sizeof(cmd) - 1] = '\0';
  1789. return wpa_ctrl_command(ctrl, cmd);
  1790. }
  1791. static int wpa_cli_cmd_p2p_ext_listen(struct wpa_ctrl *ctrl, int argc,
  1792. char *argv[])
  1793. {
  1794. char cmd[100];
  1795. int res;
  1796. if (argc != 0 && argc != 2) {
  1797. printf("Invalid P2P_EXT_LISTEN command: needs two arguments "
  1798. "(availability period, availability interval; in "
  1799. "millisecods).\n"
  1800. "Extended Listen Timing can be cancelled with this "
  1801. "command when used without parameters.\n");
  1802. return -1;
  1803. }
  1804. if (argc == 2)
  1805. res = os_snprintf(cmd, sizeof(cmd), "P2P_EXT_LISTEN %s %s",
  1806. argv[0], argv[1]);
  1807. else
  1808. res = os_snprintf(cmd, sizeof(cmd), "P2P_EXT_LISTEN");
  1809. if (res < 0 || (size_t) res >= sizeof(cmd))
  1810. return -1;
  1811. cmd[sizeof(cmd) - 1] = '\0';
  1812. return wpa_ctrl_command(ctrl, cmd);
  1813. }
  1814. #endif /* CONFIG_P2P */
  1815. static int wpa_cli_cmd_sta_autoconnect(struct wpa_ctrl *ctrl, int argc,
  1816. char *argv[])
  1817. {
  1818. char cmd[256];
  1819. int res;
  1820. if (argc != 1) {
  1821. printf("Invalid STA_AUTOCONNECT command: needs one argument "
  1822. "(0/1 = disable/enable automatic reconnection)\n");
  1823. return -1;
  1824. }
  1825. res = os_snprintf(cmd, sizeof(cmd), "STA_AUTOCONNECT %s", argv[0]);
  1826. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  1827. printf("Too long STA_AUTOCONNECT command.\n");
  1828. return -1;
  1829. }
  1830. return wpa_ctrl_command(ctrl, cmd);
  1831. }
  1832. static int wpa_cli_cmd_tdls_discover(struct wpa_ctrl *ctrl, int argc,
  1833. char *argv[])
  1834. {
  1835. char cmd[256];
  1836. int res;
  1837. if (argc != 1) {
  1838. printf("Invalid TDLS_DISCOVER command: needs one argument "
  1839. "(Peer STA MAC address)\n");
  1840. return -1;
  1841. }
  1842. res = os_snprintf(cmd, sizeof(cmd), "TDLS_DISCOVER %s", argv[0]);
  1843. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  1844. printf("Too long TDLS_DISCOVER command.\n");
  1845. return -1;
  1846. }
  1847. return wpa_ctrl_command(ctrl, cmd);
  1848. }
  1849. static int wpa_cli_cmd_tdls_setup(struct wpa_ctrl *ctrl, int argc,
  1850. char *argv[])
  1851. {
  1852. char cmd[256];
  1853. int res;
  1854. if (argc != 1) {
  1855. printf("Invalid TDLS_SETUP command: needs one argument "
  1856. "(Peer STA MAC address)\n");
  1857. return -1;
  1858. }
  1859. res = os_snprintf(cmd, sizeof(cmd), "TDLS_SETUP %s", argv[0]);
  1860. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  1861. printf("Too long TDLS_SETUP command.\n");
  1862. return -1;
  1863. }
  1864. return wpa_ctrl_command(ctrl, cmd);
  1865. }
  1866. static int wpa_cli_cmd_tdls_teardown(struct wpa_ctrl *ctrl, int argc,
  1867. char *argv[])
  1868. {
  1869. char cmd[256];
  1870. int res;
  1871. if (argc != 1) {
  1872. printf("Invalid TDLS_TEARDOWN command: needs one argument "
  1873. "(Peer STA MAC address)\n");
  1874. return -1;
  1875. }
  1876. res = os_snprintf(cmd, sizeof(cmd), "TDLS_TEARDOWN %s", argv[0]);
  1877. if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
  1878. printf("Too long TDLS_TEARDOWN command.\n");
  1879. return -1;
  1880. }
  1881. return wpa_ctrl_command(ctrl, cmd);
  1882. }
  1883. static int wpa_cli_cmd_signal_poll(struct wpa_ctrl *ctrl, int argc,
  1884. char *argv[])
  1885. {
  1886. return wpa_ctrl_command(ctrl, "SIGNAL_POLL");
  1887. }
  1888. enum wpa_cli_cmd_flags {
  1889. cli_cmd_flag_none = 0x00,
  1890. cli_cmd_flag_sensitive = 0x01
  1891. };
  1892. struct wpa_cli_cmd {
  1893. const char *cmd;
  1894. int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
  1895. enum wpa_cli_cmd_flags flags;
  1896. const char *usage;
  1897. };
  1898. static struct wpa_cli_cmd wpa_cli_commands[] = {
  1899. { "status", wpa_cli_cmd_status,
  1900. cli_cmd_flag_none,
  1901. "[verbose] = get current WPA/EAPOL/EAP status" },
  1902. { "ping", wpa_cli_cmd_ping,
  1903. cli_cmd_flag_none,
  1904. "= pings wpa_supplicant" },
  1905. { "relog", wpa_cli_cmd_relog,
  1906. cli_cmd_flag_none,
  1907. "= re-open log-file (allow rolling logs)" },
  1908. { "note", wpa_cli_cmd_note,
  1909. cli_cmd_flag_none,
  1910. "<text> = add a note to wpa_supplicant debug log" },
  1911. { "mib", wpa_cli_cmd_mib,
  1912. cli_cmd_flag_none,
  1913. "= get MIB variables (dot1x, dot11)" },
  1914. { "help", wpa_cli_cmd_help,
  1915. cli_cmd_flag_none,
  1916. "= show this usage help" },
  1917. { "interface", wpa_cli_cmd_interface,
  1918. cli_cmd_flag_none,
  1919. "[ifname] = show interfaces/select interface" },
  1920. { "level", wpa_cli_cmd_level,
  1921. cli_cmd_flag_none,
  1922. "<debug level> = change debug level" },
  1923. { "license", wpa_cli_cmd_license,
  1924. cli_cmd_flag_none,
  1925. "= show full wpa_cli license" },
  1926. { "quit", wpa_cli_cmd_quit,
  1927. cli_cmd_flag_none,
  1928. "= exit wpa_cli" },
  1929. { "set", wpa_cli_cmd_set,
  1930. cli_cmd_flag_none,
  1931. "= set variables (shows list of variables when run without "
  1932. "arguments)" },
  1933. { "get", wpa_cli_cmd_get,
  1934. cli_cmd_flag_none,
  1935. "<name> = get information" },
  1936. { "logon", wpa_cli_cmd_logon,
  1937. cli_cmd_flag_none,
  1938. "= IEEE 802.1X EAPOL state machine logon" },
  1939. { "logoff", wpa_cli_cmd_logoff,
  1940. cli_cmd_flag_none,
  1941. "= IEEE 802.1X EAPOL state machine logoff" },
  1942. { "pmksa", wpa_cli_cmd_pmksa,
  1943. cli_cmd_flag_none,
  1944. "= show PMKSA cache" },
  1945. { "reassociate", wpa_cli_cmd_reassociate,
  1946. cli_cmd_flag_none,
  1947. "= force reassociation" },
  1948. { "preauthenticate", wpa_cli_cmd_preauthenticate,
  1949. cli_cmd_flag_none,
  1950. "<BSSID> = force preauthentication" },
  1951. { "identity", wpa_cli_cmd_identity,
  1952. cli_cmd_flag_none,
  1953. "<network id> <identity> = configure identity for an SSID" },
  1954. { "password", wpa_cli_cmd_password,
  1955. cli_cmd_flag_sensitive,
  1956. "<network id> <password> = configure password for an SSID" },
  1957. { "new_password", wpa_cli_cmd_new_password,
  1958. cli_cmd_flag_sensitive,
  1959. "<network id> <password> = change password for an SSID" },
  1960. { "pin", wpa_cli_cmd_pin,
  1961. cli_cmd_flag_sensitive,
  1962. "<network id> <pin> = configure pin for an SSID" },
  1963. { "otp", wpa_cli_cmd_otp,
  1964. cli_cmd_flag_sensitive,
  1965. "<network id> <password> = configure one-time-password for an SSID"
  1966. },
  1967. { "passphrase", wpa_cli_cmd_passphrase,
  1968. cli_cmd_flag_sensitive,
  1969. "<network id> <passphrase> = configure private key passphrase\n"
  1970. " for an SSID" },
  1971. { "bssid", wpa_cli_cmd_bssid,
  1972. cli_cmd_flag_none,
  1973. "<network id> <BSSID> = set preferred BSSID for an SSID" },
  1974. { "list_networks", wpa_cli_cmd_list_networks,
  1975. cli_cmd_flag_none,
  1976. "= list configured networks" },
  1977. { "select_network", wpa_cli_cmd_select_network,
  1978. cli_cmd_flag_none,
  1979. "<network id> = select a network (disable others)" },
  1980. { "enable_network", wpa_cli_cmd_enable_network,
  1981. cli_cmd_flag_none,
  1982. "<network id> = enable a network" },
  1983. { "disable_network", wpa_cli_cmd_disable_network,
  1984. cli_cmd_flag_none,
  1985. "<network id> = disable a network" },
  1986. { "add_network", wpa_cli_cmd_add_network,
  1987. cli_cmd_flag_none,
  1988. "= add a network" },
  1989. { "remove_network", wpa_cli_cmd_remove_network,
  1990. cli_cmd_flag_none,
  1991. "<network id> = remove a network" },
  1992. { "set_network", wpa_cli_cmd_set_network,
  1993. cli_cmd_flag_sensitive,
  1994. "<network id> <variable> <value> = set network variables (shows\n"
  1995. " list of variables when run without arguments)" },
  1996. { "get_network", wpa_cli_cmd_get_network,
  1997. cli_cmd_flag_none,
  1998. "<network id> <variable> = get network variables" },
  1999. { "save_config", wpa_cli_cmd_save_config,
  2000. cli_cmd_flag_none,
  2001. "= save the current configuration" },
  2002. { "disconnect", wpa_cli_cmd_disconnect,
  2003. cli_cmd_flag_none,
  2004. "= disconnect and wait for reassociate/reconnect command before\n"
  2005. " connecting" },
  2006. { "reconnect", wpa_cli_cmd_reconnect,
  2007. cli_cmd_flag_none,
  2008. "= like reassociate, but only takes effect if already disconnected"
  2009. },
  2010. { "scan", wpa_cli_cmd_scan,
  2011. cli_cmd_flag_none,
  2012. "= request new BSS scan" },
  2013. { "scan_results", wpa_cli_cmd_scan_results,
  2014. cli_cmd_flag_none,
  2015. "= get latest scan results" },
  2016. { "bss", wpa_cli_cmd_bss,
  2017. cli_cmd_flag_none,
  2018. "<<idx> | <bssid>> = get detailed scan result info" },
  2019. { "get_capability", wpa_cli_cmd_get_capability,
  2020. cli_cmd_flag_none,
  2021. "<eap/pairwise/group/key_mgmt/proto/auth_alg> = get capabilies" },
  2022. { "reconfigure", wpa_cli_cmd_reconfigure,
  2023. cli_cmd_flag_none,
  2024. "= force wpa_supplicant to re-read its configuration file" },
  2025. { "terminate", wpa_cli_cmd_terminate,
  2026. cli_cmd_flag_none,
  2027. "= terminate wpa_supplicant" },
  2028. { "interface_add", wpa_cli_cmd_interface_add,
  2029. cli_cmd_flag_none,
  2030. "<ifname> <confname> <driver> <ctrl_interface> <driver_param>\n"
  2031. " <bridge_name> = adds new interface, all parameters but <ifname>\n"
  2032. " are optional" },
  2033. { "interface_remove", wpa_cli_cmd_interface_remove,
  2034. cli_cmd_flag_none,
  2035. "<ifname> = removes the interface" },
  2036. { "interface_list", wpa_cli_cmd_interface_list,
  2037. cli_cmd_flag_none,
  2038. "= list available interfaces" },
  2039. { "ap_scan", wpa_cli_cmd_ap_scan,
  2040. cli_cmd_flag_none,
  2041. "<value> = set ap_scan parameter" },
  2042. { "scan_interval", wpa_cli_cmd_scan_interval,
  2043. cli_cmd_flag_none,
  2044. "<value> = set scan_interval parameter (in seconds)" },
  2045. { "bss_expire_age", wpa_cli_cmd_bss_expire_age,
  2046. cli_cmd_flag_none,
  2047. "<value> = set BSS expiration age parameter" },
  2048. { "bss_expire_count", wpa_cli_cmd_bss_expire_count,
  2049. cli_cmd_flag_none,
  2050. "<value> = set BSS expiration scan count parameter" },
  2051. { "stkstart", wpa_cli_cmd_stkstart,
  2052. cli_cmd_flag_none,
  2053. "<addr> = request STK negotiation with <addr>" },
  2054. { "ft_ds", wpa_cli_cmd_ft_ds,
  2055. cli_cmd_flag_none,
  2056. "<addr> = request over-the-DS FT with <addr>" },
  2057. { "wps_pbc", wpa_cli_cmd_wps_pbc,
  2058. cli_cmd_flag_none,
  2059. "[BSSID] = start Wi-Fi Protected Setup: Push Button Configuration" },
  2060. { "wps_pin", wpa_cli_cmd_wps_pin,
  2061. cli_cmd_flag_sensitive,
  2062. "<BSSID> [PIN] = start WPS PIN method (returns PIN, if not "
  2063. "hardcoded)" },
  2064. { "wps_check_pin", wpa_cli_cmd_wps_check_pin,
  2065. cli_cmd_flag_sensitive,
  2066. "<PIN> = verify PIN checksum" },
  2067. { "wps_cancel", wpa_cli_cmd_wps_cancel, cli_cmd_flag_none,
  2068. "Cancels the pending WPS operation" },
  2069. #ifdef CONFIG_WPS_OOB
  2070. { "wps_oob", wpa_cli_cmd_wps_oob,
  2071. cli_cmd_flag_sensitive,
  2072. "<DEV_TYPE> <PATH> <METHOD> [DEV_NAME] = start WPS OOB" },
  2073. #endif /* CONFIG_WPS_OOB */
  2074. { "wps_reg", wpa_cli_cmd_wps_reg,
  2075. cli_cmd_flag_sensitive,
  2076. "<BSSID> <AP PIN> = start WPS Registrar to configure an AP" },
  2077. { "wps_ap_pin", wpa_cli_cmd_wps_ap_pin,
  2078. cli_cmd_flag_sensitive,
  2079. "[params..] = enable/disable AP PIN" },
  2080. { "wps_er_start", wpa_cli_cmd_wps_er_start,
  2081. cli_cmd_flag_none,
  2082. "[IP address] = start Wi-Fi Protected Setup External Registrar" },
  2083. { "wps_er_stop", wpa_cli_cmd_wps_er_stop,
  2084. cli_cmd_flag_none,
  2085. "= stop Wi-Fi Protected Setup External Registrar" },
  2086. { "wps_er_pin", wpa_cli_cmd_wps_er_pin,
  2087. cli_cmd_flag_sensitive,
  2088. "<UUID> <PIN> = add an Enrollee PIN to External Registrar" },
  2089. { "wps_er_pbc", wpa_cli_cmd_wps_er_pbc,
  2090. cli_cmd_flag_none,
  2091. "<UUID> = accept an Enrollee PBC using External Registrar" },
  2092. { "wps_er_learn", wpa_cli_cmd_wps_er_learn,
  2093. cli_cmd_flag_sensitive,
  2094. "<UUID> <PIN> = learn AP configuration" },
  2095. { "wps_er_set_config", wpa_cli_cmd_wps_er_set_config,
  2096. cli_cmd_flag_none,
  2097. "<UUID> <network id> = set AP configuration for enrolling" },
  2098. { "wps_er_config", wpa_cli_cmd_wps_er_config,
  2099. cli_cmd_flag_sensitive,
  2100. "<UUID> <PIN> <SSID> <auth> <encr> <key> = configure AP" },
  2101. { "ibss_rsn", wpa_cli_cmd_ibss_rsn,
  2102. cli_cmd_flag_none,
  2103. "<addr> = request RSN authentication with <addr> in IBSS" },
  2104. #ifdef CONFIG_AP
  2105. { "sta", wpa_cli_cmd_sta,
  2106. cli_cmd_flag_none,
  2107. "<addr> = get information about an associated station (AP)" },
  2108. { "all_sta", wpa_cli_cmd_all_sta,
  2109. cli_cmd_flag_none,
  2110. "= get information about all associated stations (AP)" },
  2111. #endif /* CONFIG_AP */
  2112. { "suspend", wpa_cli_cmd_suspend, cli_cmd_flag_none,
  2113. "= notification of suspend/hibernate" },
  2114. { "resume", wpa_cli_cmd_resume, cli_cmd_flag_none,
  2115. "= notification of resume/thaw" },
  2116. { "drop_sa", wpa_cli_cmd_drop_sa, cli_cmd_flag_none,
  2117. "= drop SA without deauth/disassoc (test command)" },
  2118. { "roam", wpa_cli_cmd_roam,
  2119. cli_cmd_flag_none,
  2120. "<addr> = roam to the specified BSS" },
  2121. #ifdef CONFIG_P2P
  2122. { "p2p_find", wpa_cli_cmd_p2p_find, cli_cmd_flag_none,
  2123. "[timeout] [type=*] = find P2P Devices for up-to timeout seconds" },
  2124. { "p2p_stop_find", wpa_cli_cmd_p2p_stop_find, cli_cmd_flag_none,
  2125. "= stop P2P Devices search" },
  2126. { "p2p_connect", wpa_cli_cmd_p2p_connect, cli_cmd_flag_none,
  2127. "<addr> <\"pbc\"|PIN> = connect to a P2P Devices" },
  2128. { "p2p_listen", wpa_cli_cmd_p2p_listen, cli_cmd_flag_none,
  2129. "[timeout] = listen for P2P Devices for up-to timeout seconds" },
  2130. { "p2p_group_remove", wpa_cli_cmd_p2p_group_remove, cli_cmd_flag_none,
  2131. "<ifname> = remove P2P group interface (terminate group if GO)" },
  2132. { "p2p_group_add", wpa_cli_cmd_p2p_group_add, cli_cmd_flag_none,
  2133. "= add a new P2P group (local end as GO)" },
  2134. { "p2p_prov_disc", wpa_cli_cmd_p2p_prov_disc, cli_cmd_flag_none,
  2135. "<addr> <method> = request provisioning discovery" },
  2136. { "p2p_get_passphrase", wpa_cli_cmd_p2p_get_passphrase,
  2137. cli_cmd_flag_none,
  2138. "= get the passphrase for a group (GO only)" },
  2139. { "p2p_serv_disc_req", wpa_cli_cmd_p2p_serv_disc_req,
  2140. cli_cmd_flag_none,
  2141. "<addr> <TLVs> = schedule service discovery request" },
  2142. { "p2p_serv_disc_cancel_req", wpa_cli_cmd_p2p_serv_disc_cancel_req,
  2143. cli_cmd_flag_none,
  2144. "<id> = cancel pending service discovery request" },
  2145. { "p2p_serv_disc_resp", wpa_cli_cmd_p2p_serv_disc_resp,
  2146. cli_cmd_flag_none,
  2147. "<freq> <addr> <dialog token> <TLVs> = service discovery response" },
  2148. { "p2p_service_update", wpa_cli_cmd_p2p_service_update,
  2149. cli_cmd_flag_none,
  2150. "= indicate change in local services" },
  2151. { "p2p_serv_disc_external", wpa_cli_cmd_p2p_serv_disc_external,
  2152. cli_cmd_flag_none,
  2153. "<external> = set external processing of service discovery" },
  2154. { "p2p_service_flush", wpa_cli_cmd_p2p_service_flush,
  2155. cli_cmd_flag_none,
  2156. "= remove all stored service entries" },
  2157. { "p2p_service_add", wpa_cli_cmd_p2p_service_add,
  2158. cli_cmd_flag_none,
  2159. "<bonjour|upnp> <query|version> <response|service> = add a local "
  2160. "service" },
  2161. { "p2p_service_del", wpa_cli_cmd_p2p_service_del,
  2162. cli_cmd_flag_none,
  2163. "<bonjour|upnp> <query|version> [|service] = remove a local "
  2164. "service" },
  2165. { "p2p_reject", wpa_cli_cmd_p2p_reject,
  2166. cli_cmd_flag_none,
  2167. "<addr> = reject connection attempts from a specific peer" },
  2168. { "p2p_invite", wpa_cli_cmd_p2p_invite,
  2169. cli_cmd_flag_none,
  2170. "<cmd> [peer=addr] = invite peer" },
  2171. { "p2p_peers", wpa_cli_cmd_p2p_peers, cli_cmd_flag_none,
  2172. "[discovered] = list known (optionally, only fully discovered) P2P "
  2173. "peers" },
  2174. { "p2p_peer", wpa_cli_cmd_p2p_peer, cli_cmd_flag_none,
  2175. "<address> = show information about known P2P peer" },
  2176. { "p2p_set", wpa_cli_cmd_p2p_set, cli_cmd_flag_none,
  2177. "<field> <value> = set a P2P parameter" },
  2178. { "p2p_flush", wpa_cli_cmd_p2p_flush, cli_cmd_flag_none,
  2179. "= flush P2P state" },
  2180. { "p2p_cancel", wpa_cli_cmd_p2p_cancel, cli_cmd_flag_none,
  2181. "= cancel P2P group formation" },
  2182. { "p2p_unauthorize", wpa_cli_cmd_p2p_unauthorize, cli_cmd_flag_none,
  2183. "<address> = unauthorize a peer" },
  2184. { "p2p_presence_req", wpa_cli_cmd_p2p_presence_req, cli_cmd_flag_none,
  2185. "[<duration> <interval>] [<duration> <interval>] = request GO "
  2186. "presence" },
  2187. { "p2p_ext_listen", wpa_cli_cmd_p2p_ext_listen, cli_cmd_flag_none,
  2188. "[<period> <interval>] = set extended listen timing" },
  2189. #endif /* CONFIG_P2P */
  2190. { "sta_autoconnect", wpa_cli_cmd_sta_autoconnect, cli_cmd_flag_none,
  2191. "<0/1> = disable/enable automatic reconnection" },
  2192. { "tdls_discover", wpa_cli_cmd_tdls_discover,
  2193. cli_cmd_flag_none,
  2194. "<addr> = request TDLS discovery with <addr>" },
  2195. { "tdls_setup", wpa_cli_cmd_tdls_setup,
  2196. cli_cmd_flag_none,
  2197. "<addr> = request TDLS setup with <addr>" },
  2198. { "tdls_teardown", wpa_cli_cmd_tdls_teardown,
  2199. cli_cmd_flag_none,
  2200. "<addr> = tear down TDLS with <addr>" },
  2201. { "signal_poll", wpa_cli_cmd_signal_poll,
  2202. cli_cmd_flag_none,
  2203. "= get signal parameters" },
  2204. { NULL, NULL, cli_cmd_flag_none, NULL }
  2205. };
  2206. /*
  2207. * Prints command usage, lines are padded with the specified string.
  2208. */
  2209. static void print_cmd_help(struct wpa_cli_cmd *cmd, const char *pad)
  2210. {
  2211. char c;
  2212. size_t n;
  2213. printf("%s%s ", pad, cmd->cmd);
  2214. for (n = 0; (c = cmd->usage[n]); n++) {
  2215. printf("%c", c);
  2216. if (c == '\n')
  2217. printf("%s", pad);
  2218. }
  2219. printf("\n");
  2220. }
  2221. static void print_help(void)
  2222. {
  2223. int n;
  2224. printf("commands:\n");
  2225. for (n = 0; wpa_cli_commands[n].cmd; n++)
  2226. print_cmd_help(&wpa_cli_commands[n], " ");
  2227. }
  2228. static int wpa_cli_edit_filter_history_cb(void *ctx, const char *cmd)
  2229. {
  2230. const char *c, *delim;
  2231. int n;
  2232. size_t len;
  2233. delim = os_strchr(cmd, ' ');
  2234. if (delim)
  2235. len = delim - cmd;
  2236. else
  2237. len = os_strlen(cmd);
  2238. for (n = 0; (c = wpa_cli_commands[n].cmd); n++) {
  2239. if (os_strncasecmp(cmd, c, len) == 0 && len == os_strlen(c))
  2240. return (wpa_cli_commands[n].flags &
  2241. cli_cmd_flag_sensitive);
  2242. }
  2243. return 0;
  2244. }
  2245. static char ** wpa_list_cmd_list(void)
  2246. {
  2247. char **res;
  2248. int i, count;
  2249. count = sizeof(wpa_cli_commands) / sizeof(wpa_cli_commands[0]);
  2250. res = os_zalloc(count * sizeof(char *));
  2251. if (res == NULL)
  2252. return NULL;
  2253. for (i = 0; wpa_cli_commands[i].cmd; i++) {
  2254. res[i] = os_strdup(wpa_cli_commands[i].cmd);
  2255. if (res[i] == NULL)
  2256. break;
  2257. }
  2258. return res;
  2259. }
  2260. static char ** wpa_cli_cmd_completion(const char *cmd, const char *str,
  2261. int pos)
  2262. {
  2263. int i;
  2264. for (i = 0; wpa_cli_commands[i].cmd; i++) {
  2265. if (os_strcasecmp(wpa_cli_commands[i].cmd, cmd) == 0) {
  2266. edit_clear_line();
  2267. printf("\r%s\n", wpa_cli_commands[i].usage);
  2268. edit_redraw();
  2269. break;
  2270. }
  2271. }
  2272. return NULL;
  2273. }
  2274. static char ** wpa_cli_edit_completion_cb(void *ctx, const char *str, int pos)
  2275. {
  2276. char **res;
  2277. const char *end;
  2278. char *cmd;
  2279. end = os_strchr(str, ' ');
  2280. if (end == NULL || str + pos < end)
  2281. return wpa_list_cmd_list();
  2282. cmd = os_malloc(pos + 1);
  2283. if (cmd == NULL)
  2284. return NULL;
  2285. os_memcpy(cmd, str, pos);
  2286. cmd[end - str] = '\0';
  2287. res = wpa_cli_cmd_completion(cmd, str, pos);
  2288. os_free(cmd);
  2289. return res;
  2290. }
  2291. static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
  2292. {
  2293. struct wpa_cli_cmd *cmd, *match = NULL;
  2294. int count;
  2295. int ret = 0;
  2296. count = 0;
  2297. cmd = wpa_cli_commands;
  2298. while (cmd->cmd) {
  2299. if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
  2300. {
  2301. match = cmd;
  2302. if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
  2303. /* we have an exact match */
  2304. count = 1;
  2305. break;
  2306. }
  2307. count++;
  2308. }
  2309. cmd++;
  2310. }
  2311. if (count > 1) {
  2312. printf("Ambiguous command '%s'; possible commands:", argv[0]);
  2313. cmd = wpa_cli_commands;
  2314. while (cmd->cmd) {
  2315. if (os_strncasecmp(cmd->cmd, argv[0],
  2316. os_strlen(argv[0])) == 0) {
  2317. printf(" %s", cmd->cmd);
  2318. }
  2319. cmd++;
  2320. }
  2321. printf("\n");
  2322. ret = 1;
  2323. } else if (count == 0) {
  2324. printf("Unknown command '%s'\n", argv[0]);
  2325. ret = 1;
  2326. } else {
  2327. ret = match->handler(ctrl, argc - 1, &argv[1]);
  2328. }
  2329. return ret;
  2330. }
  2331. static int str_match(const char *a, const char *b)
  2332. {
  2333. return os_strncmp(a, b, os_strlen(b)) == 0;
  2334. }
  2335. static int wpa_cli_exec(const char *program, const char *arg1,
  2336. const char *arg2)
  2337. {
  2338. char *cmd;
  2339. size_t len;
  2340. int res;
  2341. int ret = 0;
  2342. len = os_strlen(program) + os_strlen(arg1) + os_strlen(arg2) + 3;
  2343. cmd = os_malloc(len);
  2344. if (cmd == NULL)
  2345. return -1;
  2346. res = os_snprintf(cmd, len, "%s %s %s", program, arg1, arg2);
  2347. if (res < 0 || (size_t) res >= len) {
  2348. os_free(cmd);
  2349. return -1;
  2350. }
  2351. cmd[len - 1] = '\0';
  2352. #ifndef _WIN32_WCE
  2353. if (system(cmd) < 0)
  2354. ret = -1;
  2355. #endif /* _WIN32_WCE */
  2356. os_free(cmd);
  2357. return ret;
  2358. }
  2359. static void wpa_cli_action_process(const char *msg)
  2360. {
  2361. const char *pos;
  2362. char *copy = NULL, *id, *pos2;
  2363. pos = msg;
  2364. if (*pos == '<') {
  2365. /* skip priority */
  2366. pos = os_strchr(pos, '>');
  2367. if (pos)
  2368. pos++;
  2369. else
  2370. pos = msg;
  2371. }
  2372. if (str_match(pos, WPA_EVENT_CONNECTED)) {
  2373. int new_id = -1;
  2374. os_unsetenv("WPA_ID");
  2375. os_unsetenv("WPA_ID_STR");
  2376. os_unsetenv("WPA_CTRL_DIR");
  2377. pos = os_strstr(pos, "[id=");
  2378. if (pos)
  2379. copy = os_strdup(pos + 4);
  2380. if (copy) {
  2381. pos2 = id = copy;
  2382. while (*pos2 && *pos2 != ' ')
  2383. pos2++;
  2384. *pos2++ = '\0';
  2385. new_id = atoi(id);
  2386. os_setenv("WPA_ID", id, 1);
  2387. while (*pos2 && *pos2 != '=')
  2388. pos2++;
  2389. if (*pos2 == '=')
  2390. pos2++;
  2391. id = pos2;
  2392. while (*pos2 && *pos2 != ']')
  2393. pos2++;
  2394. *pos2 = '\0';
  2395. os_setenv("WPA_ID_STR", id, 1);
  2396. os_free(copy);
  2397. }
  2398. os_setenv("WPA_CTRL_DIR", ctrl_iface_dir, 1);
  2399. if (!wpa_cli_connected || new_id != wpa_cli_last_id) {
  2400. wpa_cli_connected = 1;
  2401. wpa_cli_last_id = new_id;
  2402. wpa_cli_exec(action_file, ctrl_ifname, "CONNECTED");
  2403. }
  2404. } else if (str_match(pos, WPA_EVENT_DISCONNECTED)) {
  2405. if (wpa_cli_connected) {
  2406. wpa_cli_connected = 0;
  2407. wpa_cli_exec(action_file, ctrl_ifname, "DISCONNECTED");
  2408. }
  2409. } else if (str_match(pos, P2P_EVENT_GROUP_STARTED)) {
  2410. wpa_cli_exec(action_file, ctrl_ifname, pos);
  2411. } else if (str_match(pos, P2P_EVENT_GROUP_REMOVED)) {
  2412. wpa_cli_exec(action_file, ctrl_ifname, pos);
  2413. } else if (str_match(pos, P2P_EVENT_CROSS_CONNECT_ENABLE)) {
  2414. wpa_cli_exec(action_file, ctrl_ifname, pos);
  2415. } else if (str_match(pos, P2P_EVENT_CROSS_CONNECT_DISABLE)) {
  2416. wpa_cli_exec(action_file, ctrl_ifname, pos);
  2417. } else if (str_match(pos, WPS_EVENT_SUCCESS)) {
  2418. wpa_cli_exec(action_file, ctrl_ifname, pos);
  2419. } else if (str_match(pos, WPS_EVENT_FAIL)) {
  2420. wpa_cli_exec(action_file, ctrl_ifname, pos);
  2421. } else if (str_match(pos, WPA_EVENT_TERMINATING)) {
  2422. printf("wpa_supplicant is terminating - stop monitoring\n");
  2423. wpa_cli_quit = 1;
  2424. }
  2425. }
  2426. #ifndef CONFIG_ANSI_C_EXTRA
  2427. static void wpa_cli_action_cb(char *msg, size_t len)
  2428. {
  2429. wpa_cli_action_process(msg);
  2430. }
  2431. #endif /* CONFIG_ANSI_C_EXTRA */
  2432. static void wpa_cli_reconnect(void)
  2433. {
  2434. wpa_cli_close_connection();
  2435. wpa_cli_open_connection(ctrl_ifname, 1);
  2436. }
  2437. static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor)
  2438. {
  2439. if (ctrl_conn == NULL) {
  2440. wpa_cli_reconnect();
  2441. return;
  2442. }
  2443. while (wpa_ctrl_pending(ctrl) > 0) {
  2444. char buf[256];
  2445. size_t len = sizeof(buf) - 1;
  2446. if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
  2447. buf[len] = '\0';
  2448. if (action_monitor)
  2449. wpa_cli_action_process(buf);
  2450. else {
  2451. if (wpa_cli_show_event(buf)) {
  2452. edit_clear_line();
  2453. printf("\r%s\n", buf);
  2454. edit_redraw();
  2455. }
  2456. }
  2457. } else {
  2458. printf("Could not read pending message.\n");
  2459. break;
  2460. }
  2461. }
  2462. if (wpa_ctrl_pending(ctrl) < 0) {
  2463. printf("Connection to wpa_supplicant lost - trying to "
  2464. "reconnect\n");
  2465. wpa_cli_reconnect();
  2466. }
  2467. }
  2468. #define max_args 10
  2469. static int tokenize_cmd(char *cmd, char *argv[])
  2470. {
  2471. char *pos;
  2472. int argc = 0;
  2473. pos = cmd;
  2474. for (;;) {
  2475. while (*pos == ' ')
  2476. pos++;
  2477. if (*pos == '\0')
  2478. break;
  2479. argv[argc] = pos;
  2480. argc++;
  2481. if (argc == max_args)
  2482. break;
  2483. if (*pos == '"') {
  2484. char *pos2 = os_strrchr(pos, '"');
  2485. if (pos2)
  2486. pos = pos2 + 1;
  2487. }
  2488. while (*pos != '\0' && *pos != ' ')
  2489. pos++;
  2490. if (*pos == ' ')
  2491. *pos++ = '\0';
  2492. }
  2493. return argc;
  2494. }
  2495. static void wpa_cli_ping(void *eloop_ctx, void *timeout_ctx)
  2496. {
  2497. if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
  2498. printf("Connection to wpa_supplicant lost - trying to "
  2499. "reconnect\n");
  2500. wpa_cli_close_connection();
  2501. }
  2502. if (!ctrl_conn)
  2503. wpa_cli_reconnect();
  2504. eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
  2505. }
  2506. static void wpa_cli_eloop_terminate(int sig, void *signal_ctx)
  2507. {
  2508. eloop_terminate();
  2509. }
  2510. static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx)
  2511. {
  2512. wpa_cli_recv_pending(mon_conn, 0);
  2513. }
  2514. static void wpa_cli_edit_cmd_cb(void *ctx, char *cmd)
  2515. {
  2516. char *argv[max_args];
  2517. int argc;
  2518. argc = tokenize_cmd(cmd, argv);
  2519. if (argc)
  2520. wpa_request(ctrl_conn, argc, argv);
  2521. }
  2522. static void wpa_cli_edit_eof_cb(void *ctx)
  2523. {
  2524. eloop_terminate();
  2525. }
  2526. static void wpa_cli_interactive(void)
  2527. {
  2528. char *home, *hfile = NULL;
  2529. printf("\nInteractive mode\n\n");
  2530. home = getenv("HOME");
  2531. if (home) {
  2532. const char *fname = ".wpa_cli_history";
  2533. int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
  2534. hfile = os_malloc(hfile_len);
  2535. if (hfile)
  2536. os_snprintf(hfile, hfile_len, "%s/%s", home, fname);
  2537. }
  2538. eloop_register_signal_terminate(wpa_cli_eloop_terminate, NULL);
  2539. edit_init(wpa_cli_edit_cmd_cb, wpa_cli_edit_eof_cb,
  2540. wpa_cli_edit_completion_cb, NULL, hfile);
  2541. eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
  2542. eloop_run();
  2543. edit_deinit(hfile, wpa_cli_edit_filter_history_cb);
  2544. os_free(hfile);
  2545. eloop_cancel_timeout(wpa_cli_ping, NULL, NULL);
  2546. wpa_cli_close_connection();
  2547. }
  2548. static void wpa_cli_action(struct wpa_ctrl *ctrl)
  2549. {
  2550. #ifdef CONFIG_ANSI_C_EXTRA
  2551. /* TODO: ANSI C version(?) */
  2552. printf("Action processing not supported in ANSI C build.\n");
  2553. #else /* CONFIG_ANSI_C_EXTRA */
  2554. fd_set rfds;
  2555. int fd, res;
  2556. struct timeval tv;
  2557. char buf[256]; /* note: large enough to fit in unsolicited messages */
  2558. size_t len;
  2559. fd = wpa_ctrl_get_fd(ctrl);
  2560. while (!wpa_cli_quit) {
  2561. FD_ZERO(&rfds);
  2562. FD_SET(fd, &rfds);
  2563. tv.tv_sec = ping_interval;
  2564. tv.tv_usec = 0;
  2565. res = select(fd + 1, &rfds, NULL, NULL, &tv);
  2566. if (res < 0 && errno != EINTR) {
  2567. perror("select");
  2568. break;
  2569. }
  2570. if (FD_ISSET(fd, &rfds))
  2571. wpa_cli_recv_pending(ctrl, 1);
  2572. else {
  2573. /* verify that connection is still working */
  2574. len = sizeof(buf) - 1;
  2575. if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
  2576. wpa_cli_action_cb) < 0 ||
  2577. len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
  2578. printf("wpa_supplicant did not reply to PING "
  2579. "command - exiting\n");
  2580. break;
  2581. }
  2582. }
  2583. }
  2584. #endif /* CONFIG_ANSI_C_EXTRA */
  2585. }
  2586. static void wpa_cli_cleanup(void)
  2587. {
  2588. wpa_cli_close_connection();
  2589. if (pid_file)
  2590. os_daemonize_terminate(pid_file);
  2591. os_program_deinit();
  2592. }
  2593. static void wpa_cli_terminate(int sig)
  2594. {
  2595. wpa_cli_cleanup();
  2596. exit(0);
  2597. }
  2598. static char * wpa_cli_get_default_ifname(void)
  2599. {
  2600. char *ifname = NULL;
  2601. #ifdef CONFIG_CTRL_IFACE_UNIX
  2602. struct dirent *dent;
  2603. DIR *dir = opendir(ctrl_iface_dir);
  2604. if (!dir) {
  2605. #ifdef ANDROID
  2606. char ifprop[PROPERTY_VALUE_MAX];
  2607. if (property_get("wifi.interface", ifprop, NULL) != 0) {
  2608. ifname = os_strdup(ifprop);
  2609. printf("Using interface '%s'\n", ifname);
  2610. return ifname;
  2611. }
  2612. #endif /* ANDROID */
  2613. return NULL;
  2614. }
  2615. while ((dent = readdir(dir))) {
  2616. #ifdef _DIRENT_HAVE_D_TYPE
  2617. /*
  2618. * Skip the file if it is not a socket. Also accept
  2619. * DT_UNKNOWN (0) in case the C library or underlying
  2620. * file system does not support d_type.
  2621. */
  2622. if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
  2623. continue;
  2624. #endif /* _DIRENT_HAVE_D_TYPE */
  2625. if (os_strcmp(dent->d_name, ".") == 0 ||
  2626. os_strcmp(dent->d_name, "..") == 0)
  2627. continue;
  2628. printf("Selected interface '%s'\n", dent->d_name);
  2629. ifname = os_strdup(dent->d_name);
  2630. break;
  2631. }
  2632. closedir(dir);
  2633. #endif /* CONFIG_CTRL_IFACE_UNIX */
  2634. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  2635. char buf[2048], *pos;
  2636. size_t len;
  2637. struct wpa_ctrl *ctrl;
  2638. int ret;
  2639. ctrl = wpa_ctrl_open(NULL);
  2640. if (ctrl == NULL)
  2641. return NULL;
  2642. len = sizeof(buf) - 1;
  2643. ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
  2644. if (ret >= 0) {
  2645. buf[len] = '\0';
  2646. pos = os_strchr(buf, '\n');
  2647. if (pos)
  2648. *pos = '\0';
  2649. ifname = os_strdup(buf);
  2650. }
  2651. wpa_ctrl_close(ctrl);
  2652. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  2653. return ifname;
  2654. }
  2655. int main(int argc, char *argv[])
  2656. {
  2657. int warning_displayed = 0;
  2658. int c;
  2659. int daemonize = 0;
  2660. int ret = 0;
  2661. const char *global = NULL;
  2662. if (os_program_init())
  2663. return -1;
  2664. for (;;) {
  2665. c = getopt(argc, argv, "a:Bg:G:hi:p:P:v");
  2666. if (c < 0)
  2667. break;
  2668. switch (c) {
  2669. case 'a':
  2670. action_file = optarg;
  2671. break;
  2672. case 'B':
  2673. daemonize = 1;
  2674. break;
  2675. case 'g':
  2676. global = optarg;
  2677. break;
  2678. case 'G':
  2679. ping_interval = atoi(optarg);
  2680. break;
  2681. case 'h':
  2682. usage();
  2683. return 0;
  2684. case 'v':
  2685. printf("%s\n", wpa_cli_version);
  2686. return 0;
  2687. case 'i':
  2688. os_free(ctrl_ifname);
  2689. ctrl_ifname = os_strdup(optarg);
  2690. break;
  2691. case 'p':
  2692. ctrl_iface_dir = optarg;
  2693. break;
  2694. case 'P':
  2695. pid_file = optarg;
  2696. break;
  2697. default:
  2698. usage();
  2699. return -1;
  2700. }
  2701. }
  2702. interactive = (argc == optind) && (action_file == NULL);
  2703. if (interactive)
  2704. printf("%s\n\n%s\n\n", wpa_cli_version, wpa_cli_license);
  2705. if (eloop_init())
  2706. return -1;
  2707. if (global) {
  2708. #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
  2709. ctrl_conn = wpa_ctrl_open(NULL);
  2710. #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  2711. ctrl_conn = wpa_ctrl_open(global);
  2712. #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
  2713. if (ctrl_conn == NULL) {
  2714. perror("Failed to connect to wpa_supplicant - "
  2715. "wpa_ctrl_open");
  2716. return -1;
  2717. }
  2718. }
  2719. #ifndef _WIN32_WCE
  2720. signal(SIGINT, wpa_cli_terminate);
  2721. signal(SIGTERM, wpa_cli_terminate);
  2722. #endif /* _WIN32_WCE */
  2723. if (ctrl_ifname == NULL)
  2724. ctrl_ifname = wpa_cli_get_default_ifname();
  2725. if (interactive) {
  2726. for (; !global;) {
  2727. if (wpa_cli_open_connection(ctrl_ifname, 1) == 0) {
  2728. if (warning_displayed)
  2729. printf("Connection established.\n");
  2730. break;
  2731. }
  2732. if (!warning_displayed) {
  2733. printf("Could not connect to wpa_supplicant - "
  2734. "re-trying\n");
  2735. warning_displayed = 1;
  2736. }
  2737. os_sleep(1, 0);
  2738. continue;
  2739. }
  2740. } else {
  2741. if (!global &&
  2742. wpa_cli_open_connection(ctrl_ifname, 0) < 0) {
  2743. perror("Failed to connect to wpa_supplicant - "
  2744. "wpa_ctrl_open");
  2745. return -1;
  2746. }
  2747. if (action_file) {
  2748. if (wpa_ctrl_attach(ctrl_conn) == 0) {
  2749. wpa_cli_attached = 1;
  2750. } else {
  2751. printf("Warning: Failed to attach to "
  2752. "wpa_supplicant.\n");
  2753. return -1;
  2754. }
  2755. }
  2756. }
  2757. if (daemonize && os_daemonize(pid_file))
  2758. return -1;
  2759. if (interactive)
  2760. wpa_cli_interactive();
  2761. else if (action_file)
  2762. wpa_cli_action(ctrl_conn);
  2763. else
  2764. ret = wpa_request(ctrl_conn, argc - optind, &argv[optind]);
  2765. os_free(ctrl_ifname);
  2766. eloop_destroy();
  2767. wpa_cli_cleanup();
  2768. return ret;
  2769. }
  2770. #else /* CONFIG_CTRL_IFACE */
  2771. int main(int argc, char *argv[])
  2772. {
  2773. printf("CONFIG_CTRL_IFACE not defined - wpa_cli disabled\n");
  2774. return -1;
  2775. }
  2776. #endif /* CONFIG_CTRL_IFACE */