ctrl_iface.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2010, 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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "common/wpa_ctrl.h"
  19. #include "eap_peer/eap.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "rsn_supp/wpa.h"
  22. #include "rsn_supp/preauth.h"
  23. #include "rsn_supp/pmksa_cache.h"
  24. #include "l2_packet/l2_packet.h"
  25. #include "wps/wps.h"
  26. #include "config.h"
  27. #include "wpa_supplicant_i.h"
  28. #include "driver_i.h"
  29. #include "wps_supplicant.h"
  30. #include "ibss_rsn.h"
  31. #include "ap.h"
  32. #include "p2p_supplicant.h"
  33. #include "p2p/p2p.h"
  34. #include "notify.h"
  35. #include "bss.h"
  36. #include "scan.h"
  37. #include "ctrl_iface.h"
  38. extern struct wpa_driver_ops *wpa_drivers[];
  39. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  40. char *buf, int len);
  41. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  42. char *buf, int len);
  43. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  44. char *cmd)
  45. {
  46. char *value;
  47. int ret = 0;
  48. value = os_strchr(cmd, ' ');
  49. if (value == NULL)
  50. return -1;
  51. *value++ = '\0';
  52. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  53. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  54. eapol_sm_configure(wpa_s->eapol,
  55. atoi(value), -1, -1, -1);
  56. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  57. eapol_sm_configure(wpa_s->eapol,
  58. -1, atoi(value), -1, -1);
  59. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  60. eapol_sm_configure(wpa_s->eapol,
  61. -1, -1, atoi(value), -1);
  62. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  63. eapol_sm_configure(wpa_s->eapol,
  64. -1, -1, -1, atoi(value));
  65. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  66. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  67. atoi(value)))
  68. ret = -1;
  69. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  70. 0) {
  71. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  72. atoi(value)))
  73. ret = -1;
  74. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  75. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  76. ret = -1;
  77. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  78. wpa_s->wps_fragment_size = atoi(value);
  79. #ifdef CONFIG_WPS_TESTING
  80. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  81. long int val;
  82. val = strtol(value, NULL, 0);
  83. if (val < 0 || val > 0xff) {
  84. ret = -1;
  85. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  86. "wps_version_number %ld", val);
  87. } else {
  88. wps_version_number = val;
  89. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  90. "version %u.%u",
  91. (wps_version_number & 0xf0) >> 4,
  92. wps_version_number & 0x0f);
  93. }
  94. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  95. wps_testing_dummy_cred = atoi(value);
  96. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  97. wps_testing_dummy_cred);
  98. #endif /* CONFIG_WPS_TESTING */
  99. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  100. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  101. ret = -1;
  102. } else {
  103. value[-1] = '=';
  104. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  105. if (ret == 0)
  106. wpa_supplicant_update_config(wpa_s);
  107. }
  108. return ret;
  109. }
  110. #ifdef IEEE8021X_EAPOL
  111. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  112. char *addr)
  113. {
  114. u8 bssid[ETH_ALEN];
  115. struct wpa_ssid *ssid = wpa_s->current_ssid;
  116. if (hwaddr_aton(addr, bssid)) {
  117. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  118. "'%s'", addr);
  119. return -1;
  120. }
  121. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  122. rsn_preauth_deinit(wpa_s->wpa);
  123. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  124. return -1;
  125. return 0;
  126. }
  127. #endif /* IEEE8021X_EAPOL */
  128. #ifdef CONFIG_PEERKEY
  129. /* MLME-STKSTART.request(peer) */
  130. static int wpa_supplicant_ctrl_iface_stkstart(
  131. struct wpa_supplicant *wpa_s, char *addr)
  132. {
  133. u8 peer[ETH_ALEN];
  134. if (hwaddr_aton(addr, peer)) {
  135. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  136. "address '%s'", addr);
  137. return -1;
  138. }
  139. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  140. MAC2STR(peer));
  141. return wpa_sm_stkstart(wpa_s->wpa, peer);
  142. }
  143. #endif /* CONFIG_PEERKEY */
  144. #ifdef CONFIG_IEEE80211R
  145. static int wpa_supplicant_ctrl_iface_ft_ds(
  146. struct wpa_supplicant *wpa_s, char *addr)
  147. {
  148. u8 target_ap[ETH_ALEN];
  149. struct wpa_bss *bss;
  150. const u8 *mdie;
  151. if (hwaddr_aton(addr, target_ap)) {
  152. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  153. "address '%s'", addr);
  154. return -1;
  155. }
  156. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  157. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  158. if (bss)
  159. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  160. else
  161. mdie = NULL;
  162. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  163. }
  164. #endif /* CONFIG_IEEE80211R */
  165. #ifdef CONFIG_WPS
  166. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  167. char *cmd)
  168. {
  169. u8 bssid[ETH_ALEN], *_bssid = bssid;
  170. if (cmd == NULL || os_strcmp(cmd, "any") == 0)
  171. _bssid = NULL;
  172. else if (hwaddr_aton(cmd, bssid)) {
  173. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  174. cmd);
  175. return -1;
  176. }
  177. #ifdef CONFIG_AP
  178. if (wpa_s->ap_iface)
  179. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid);
  180. #endif /* CONFIG_AP */
  181. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  182. }
  183. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  184. char *cmd, char *buf,
  185. size_t buflen)
  186. {
  187. u8 bssid[ETH_ALEN], *_bssid = bssid;
  188. char *pin;
  189. int ret;
  190. pin = os_strchr(cmd, ' ');
  191. if (pin)
  192. *pin++ = '\0';
  193. if (os_strcmp(cmd, "any") == 0)
  194. _bssid = NULL;
  195. else if (hwaddr_aton(cmd, bssid)) {
  196. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  197. cmd);
  198. return -1;
  199. }
  200. #ifdef CONFIG_AP
  201. if (wpa_s->ap_iface)
  202. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  203. buf, buflen);
  204. #endif /* CONFIG_AP */
  205. if (pin) {
  206. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  207. DEV_PW_DEFAULT);
  208. if (ret < 0)
  209. return -1;
  210. ret = os_snprintf(buf, buflen, "%s", pin);
  211. if (ret < 0 || (size_t) ret >= buflen)
  212. return -1;
  213. return ret;
  214. }
  215. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  216. if (ret < 0)
  217. return -1;
  218. /* Return the generated PIN */
  219. ret = os_snprintf(buf, buflen, "%08d", ret);
  220. if (ret < 0 || (size_t) ret >= buflen)
  221. return -1;
  222. return ret;
  223. }
  224. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  225. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  226. {
  227. char pin[9];
  228. size_t len;
  229. char *pos;
  230. int ret;
  231. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  232. (u8 *) cmd, os_strlen(cmd));
  233. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  234. if (*pos < '0' || *pos > '9')
  235. continue;
  236. pin[len++] = *pos;
  237. if (len == 9) {
  238. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  239. return -1;
  240. }
  241. }
  242. if (len != 4 && len != 8) {
  243. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  244. return -1;
  245. }
  246. pin[len] = '\0';
  247. if (len == 8) {
  248. unsigned int pin_val;
  249. pin_val = atoi(pin);
  250. if (!wps_pin_valid(pin_val)) {
  251. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  252. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  253. if (ret < 0 || (size_t) ret >= buflen)
  254. return -1;
  255. return ret;
  256. }
  257. }
  258. ret = os_snprintf(buf, buflen, "%s", pin);
  259. if (ret < 0 || (size_t) ret >= buflen)
  260. return -1;
  261. return ret;
  262. }
  263. #ifdef CONFIG_WPS_OOB
  264. static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
  265. char *cmd)
  266. {
  267. char *path, *method, *name;
  268. path = os_strchr(cmd, ' ');
  269. if (path == NULL)
  270. return -1;
  271. *path++ = '\0';
  272. method = os_strchr(path, ' ');
  273. if (method == NULL)
  274. return -1;
  275. *method++ = '\0';
  276. name = os_strchr(method, ' ');
  277. if (name != NULL)
  278. *name++ = '\0';
  279. return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
  280. }
  281. #endif /* CONFIG_WPS_OOB */
  282. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  283. char *cmd)
  284. {
  285. u8 bssid[ETH_ALEN];
  286. char *pin;
  287. char *new_ssid;
  288. char *new_auth;
  289. char *new_encr;
  290. char *new_key;
  291. struct wps_new_ap_settings ap;
  292. pin = os_strchr(cmd, ' ');
  293. if (pin == NULL)
  294. return -1;
  295. *pin++ = '\0';
  296. if (hwaddr_aton(cmd, bssid)) {
  297. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  298. cmd);
  299. return -1;
  300. }
  301. new_ssid = os_strchr(pin, ' ');
  302. if (new_ssid == NULL)
  303. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  304. *new_ssid++ = '\0';
  305. new_auth = os_strchr(new_ssid, ' ');
  306. if (new_auth == NULL)
  307. return -1;
  308. *new_auth++ = '\0';
  309. new_encr = os_strchr(new_auth, ' ');
  310. if (new_encr == NULL)
  311. return -1;
  312. *new_encr++ = '\0';
  313. new_key = os_strchr(new_encr, ' ');
  314. if (new_key == NULL)
  315. return -1;
  316. *new_key++ = '\0';
  317. os_memset(&ap, 0, sizeof(ap));
  318. ap.ssid_hex = new_ssid;
  319. ap.auth = new_auth;
  320. ap.encr = new_encr;
  321. ap.key_hex = new_key;
  322. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  323. }
  324. #ifdef CONFIG_WPS_ER
  325. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  326. char *cmd)
  327. {
  328. char *uuid = cmd, *pin, *pos;
  329. u8 addr_buf[ETH_ALEN], *addr = NULL;
  330. pin = os_strchr(uuid, ' ');
  331. if (pin == NULL)
  332. return -1;
  333. *pin++ = '\0';
  334. pos = os_strchr(pin, ' ');
  335. if (pos) {
  336. *pos++ = '\0';
  337. if (hwaddr_aton(pos, addr_buf) == 0)
  338. addr = addr_buf;
  339. }
  340. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  341. }
  342. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  343. char *cmd)
  344. {
  345. char *uuid = cmd, *pin;
  346. pin = os_strchr(uuid, ' ');
  347. if (pin == NULL)
  348. return -1;
  349. *pin++ = '\0';
  350. return wpas_wps_er_learn(wpa_s, uuid, pin);
  351. }
  352. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  353. struct wpa_supplicant *wpa_s, char *cmd)
  354. {
  355. char *uuid = cmd, *id;
  356. id = os_strchr(uuid, ' ');
  357. if (id == NULL)
  358. return -1;
  359. *id++ = '\0';
  360. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  361. }
  362. static int wpa_supplicant_ctrl_iface_wps_er_config(
  363. struct wpa_supplicant *wpa_s, char *cmd)
  364. {
  365. char *pin;
  366. char *new_ssid;
  367. char *new_auth;
  368. char *new_encr;
  369. char *new_key;
  370. struct wps_new_ap_settings ap;
  371. pin = os_strchr(cmd, ' ');
  372. if (pin == NULL)
  373. return -1;
  374. *pin++ = '\0';
  375. new_ssid = os_strchr(pin, ' ');
  376. if (new_ssid == NULL)
  377. return -1;
  378. *new_ssid++ = '\0';
  379. new_auth = os_strchr(new_ssid, ' ');
  380. if (new_auth == NULL)
  381. return -1;
  382. *new_auth++ = '\0';
  383. new_encr = os_strchr(new_auth, ' ');
  384. if (new_encr == NULL)
  385. return -1;
  386. *new_encr++ = '\0';
  387. new_key = os_strchr(new_encr, ' ');
  388. if (new_key == NULL)
  389. return -1;
  390. *new_key++ = '\0';
  391. os_memset(&ap, 0, sizeof(ap));
  392. ap.ssid_hex = new_ssid;
  393. ap.auth = new_auth;
  394. ap.encr = new_encr;
  395. ap.key_hex = new_key;
  396. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  397. }
  398. #endif /* CONFIG_WPS_ER */
  399. #endif /* CONFIG_WPS */
  400. #ifdef CONFIG_IBSS_RSN
  401. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  402. struct wpa_supplicant *wpa_s, char *addr)
  403. {
  404. u8 peer[ETH_ALEN];
  405. if (hwaddr_aton(addr, peer)) {
  406. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  407. "address '%s'", addr);
  408. return -1;
  409. }
  410. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  411. MAC2STR(peer));
  412. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  413. }
  414. #endif /* CONFIG_IBSS_RSN */
  415. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  416. char *rsp)
  417. {
  418. #ifdef IEEE8021X_EAPOL
  419. char *pos, *id_pos;
  420. int id;
  421. struct wpa_ssid *ssid;
  422. struct eap_peer_config *eap;
  423. pos = os_strchr(rsp, '-');
  424. if (pos == NULL)
  425. return -1;
  426. *pos++ = '\0';
  427. id_pos = pos;
  428. pos = os_strchr(pos, ':');
  429. if (pos == NULL)
  430. return -1;
  431. *pos++ = '\0';
  432. id = atoi(id_pos);
  433. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  434. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  435. (u8 *) pos, os_strlen(pos));
  436. ssid = wpa_config_get_network(wpa_s->conf, id);
  437. if (ssid == NULL) {
  438. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  439. "to update", id);
  440. return -1;
  441. }
  442. eap = &ssid->eap;
  443. if (os_strcmp(rsp, "IDENTITY") == 0) {
  444. os_free(eap->identity);
  445. eap->identity = (u8 *) os_strdup(pos);
  446. eap->identity_len = os_strlen(pos);
  447. eap->pending_req_identity = 0;
  448. if (ssid == wpa_s->current_ssid)
  449. wpa_s->reassociate = 1;
  450. } else if (os_strcmp(rsp, "PASSWORD") == 0) {
  451. os_free(eap->password);
  452. eap->password = (u8 *) os_strdup(pos);
  453. eap->password_len = os_strlen(pos);
  454. eap->pending_req_password = 0;
  455. if (ssid == wpa_s->current_ssid)
  456. wpa_s->reassociate = 1;
  457. } else if (os_strcmp(rsp, "NEW_PASSWORD") == 0) {
  458. os_free(eap->new_password);
  459. eap->new_password = (u8 *) os_strdup(pos);
  460. eap->new_password_len = os_strlen(pos);
  461. eap->pending_req_new_password = 0;
  462. if (ssid == wpa_s->current_ssid)
  463. wpa_s->reassociate = 1;
  464. } else if (os_strcmp(rsp, "PIN") == 0) {
  465. os_free(eap->pin);
  466. eap->pin = os_strdup(pos);
  467. eap->pending_req_pin = 0;
  468. if (ssid == wpa_s->current_ssid)
  469. wpa_s->reassociate = 1;
  470. } else if (os_strcmp(rsp, "OTP") == 0) {
  471. os_free(eap->otp);
  472. eap->otp = (u8 *) os_strdup(pos);
  473. eap->otp_len = os_strlen(pos);
  474. os_free(eap->pending_req_otp);
  475. eap->pending_req_otp = NULL;
  476. eap->pending_req_otp_len = 0;
  477. } else if (os_strcmp(rsp, "PASSPHRASE") == 0) {
  478. os_free(eap->private_key_passwd);
  479. eap->private_key_passwd = (u8 *) os_strdup(pos);
  480. eap->pending_req_passphrase = 0;
  481. if (ssid == wpa_s->current_ssid)
  482. wpa_s->reassociate = 1;
  483. } else {
  484. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", rsp);
  485. return -1;
  486. }
  487. return 0;
  488. #else /* IEEE8021X_EAPOL */
  489. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  490. return -1;
  491. #endif /* IEEE8021X_EAPOL */
  492. }
  493. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  494. const char *params,
  495. char *buf, size_t buflen)
  496. {
  497. char *pos, *end, tmp[30];
  498. int res, verbose, ret;
  499. verbose = os_strcmp(params, "-VERBOSE") == 0;
  500. pos = buf;
  501. end = buf + buflen;
  502. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  503. struct wpa_ssid *ssid = wpa_s->current_ssid;
  504. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  505. MAC2STR(wpa_s->bssid));
  506. if (ret < 0 || ret >= end - pos)
  507. return pos - buf;
  508. pos += ret;
  509. if (ssid) {
  510. u8 *_ssid = ssid->ssid;
  511. size_t ssid_len = ssid->ssid_len;
  512. u8 ssid_buf[MAX_SSID_LEN];
  513. if (ssid_len == 0) {
  514. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  515. if (_res < 0)
  516. ssid_len = 0;
  517. else
  518. ssid_len = _res;
  519. _ssid = ssid_buf;
  520. }
  521. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  522. wpa_ssid_txt(_ssid, ssid_len),
  523. ssid->id);
  524. if (ret < 0 || ret >= end - pos)
  525. return pos - buf;
  526. pos += ret;
  527. if (ssid->id_str) {
  528. ret = os_snprintf(pos, end - pos,
  529. "id_str=%s\n",
  530. ssid->id_str);
  531. if (ret < 0 || ret >= end - pos)
  532. return pos - buf;
  533. pos += ret;
  534. }
  535. switch (ssid->mode) {
  536. case WPAS_MODE_INFRA:
  537. ret = os_snprintf(pos, end - pos,
  538. "mode=station\n");
  539. break;
  540. case WPAS_MODE_IBSS:
  541. ret = os_snprintf(pos, end - pos,
  542. "mode=IBSS\n");
  543. break;
  544. case WPAS_MODE_AP:
  545. ret = os_snprintf(pos, end - pos,
  546. "mode=AP\n");
  547. break;
  548. case WPAS_MODE_P2P_GO:
  549. ret = os_snprintf(pos, end - pos,
  550. "mode=P2P GO\n");
  551. break;
  552. case WPAS_MODE_P2P_GROUP_FORMATION:
  553. ret = os_snprintf(pos, end - pos,
  554. "mode=P2P GO - group "
  555. "formation\n");
  556. break;
  557. default:
  558. ret = 0;
  559. break;
  560. }
  561. if (ret < 0 || ret >= end - pos)
  562. return pos - buf;
  563. pos += ret;
  564. }
  565. #ifdef CONFIG_AP
  566. if (wpa_s->ap_iface) {
  567. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  568. end - pos,
  569. verbose);
  570. } else
  571. #endif /* CONFIG_AP */
  572. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  573. }
  574. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  575. wpa_supplicant_state_txt(wpa_s->wpa_state));
  576. if (ret < 0 || ret >= end - pos)
  577. return pos - buf;
  578. pos += ret;
  579. if (wpa_s->l2 &&
  580. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  581. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  582. if (ret < 0 || ret >= end - pos)
  583. return pos - buf;
  584. pos += ret;
  585. }
  586. #ifdef CONFIG_P2P
  587. if (wpa_s->global->p2p) {
  588. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  589. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  590. if (ret < 0 || ret >= end - pos)
  591. return pos - buf;
  592. pos += ret;
  593. }
  594. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  595. MAC2STR(wpa_s->own_addr));
  596. if (ret < 0 || ret >= end - pos)
  597. return pos - buf;
  598. pos += ret;
  599. #endif /* CONFIG_P2P */
  600. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  601. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  602. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  603. verbose);
  604. if (res >= 0)
  605. pos += res;
  606. }
  607. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  608. if (res >= 0)
  609. pos += res;
  610. return pos - buf;
  611. }
  612. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  613. char *cmd)
  614. {
  615. char *pos;
  616. int id;
  617. struct wpa_ssid *ssid;
  618. u8 bssid[ETH_ALEN];
  619. /* cmd: "<network id> <BSSID>" */
  620. pos = os_strchr(cmd, ' ');
  621. if (pos == NULL)
  622. return -1;
  623. *pos++ = '\0';
  624. id = atoi(cmd);
  625. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  626. if (hwaddr_aton(pos, bssid)) {
  627. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  628. return -1;
  629. }
  630. ssid = wpa_config_get_network(wpa_s->conf, id);
  631. if (ssid == NULL) {
  632. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  633. "to update", id);
  634. return -1;
  635. }
  636. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  637. ssid->bssid_set = !is_zero_ether_addr(bssid);
  638. return 0;
  639. }
  640. static int wpa_supplicant_ctrl_iface_list_networks(
  641. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  642. {
  643. char *pos, *end;
  644. struct wpa_ssid *ssid;
  645. int ret;
  646. pos = buf;
  647. end = buf + buflen;
  648. ret = os_snprintf(pos, end - pos,
  649. "network id / ssid / bssid / flags\n");
  650. if (ret < 0 || ret >= end - pos)
  651. return pos - buf;
  652. pos += ret;
  653. ssid = wpa_s->conf->ssid;
  654. while (ssid) {
  655. ret = os_snprintf(pos, end - pos, "%d\t%s",
  656. ssid->id,
  657. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  658. if (ret < 0 || ret >= end - pos)
  659. return pos - buf;
  660. pos += ret;
  661. if (ssid->bssid_set) {
  662. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  663. MAC2STR(ssid->bssid));
  664. } else {
  665. ret = os_snprintf(pos, end - pos, "\tany");
  666. }
  667. if (ret < 0 || ret >= end - pos)
  668. return pos - buf;
  669. pos += ret;
  670. ret = os_snprintf(pos, end - pos, "\t%s%s%s",
  671. ssid == wpa_s->current_ssid ?
  672. "[CURRENT]" : "",
  673. ssid->disabled ? "[DISABLED]" : "",
  674. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  675. "");
  676. if (ret < 0 || ret >= end - pos)
  677. return pos - buf;
  678. pos += ret;
  679. ret = os_snprintf(pos, end - pos, "\n");
  680. if (ret < 0 || ret >= end - pos)
  681. return pos - buf;
  682. pos += ret;
  683. ssid = ssid->next;
  684. }
  685. return pos - buf;
  686. }
  687. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  688. {
  689. int first = 1, ret;
  690. ret = os_snprintf(pos, end - pos, "-");
  691. if (ret < 0 || ret >= end - pos)
  692. return pos;
  693. pos += ret;
  694. if (cipher & WPA_CIPHER_NONE) {
  695. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  696. if (ret < 0 || ret >= end - pos)
  697. return pos;
  698. pos += ret;
  699. first = 0;
  700. }
  701. if (cipher & WPA_CIPHER_WEP40) {
  702. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  703. if (ret < 0 || ret >= end - pos)
  704. return pos;
  705. pos += ret;
  706. first = 0;
  707. }
  708. if (cipher & WPA_CIPHER_WEP104) {
  709. ret = os_snprintf(pos, end - pos, "%sWEP104",
  710. first ? "" : "+");
  711. if (ret < 0 || ret >= end - pos)
  712. return pos;
  713. pos += ret;
  714. first = 0;
  715. }
  716. if (cipher & WPA_CIPHER_TKIP) {
  717. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  718. if (ret < 0 || ret >= end - pos)
  719. return pos;
  720. pos += ret;
  721. first = 0;
  722. }
  723. if (cipher & WPA_CIPHER_CCMP) {
  724. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  725. if (ret < 0 || ret >= end - pos)
  726. return pos;
  727. pos += ret;
  728. first = 0;
  729. }
  730. return pos;
  731. }
  732. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  733. const u8 *ie, size_t ie_len)
  734. {
  735. struct wpa_ie_data data;
  736. int first, ret;
  737. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  738. if (ret < 0 || ret >= end - pos)
  739. return pos;
  740. pos += ret;
  741. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  742. ret = os_snprintf(pos, end - pos, "?]");
  743. if (ret < 0 || ret >= end - pos)
  744. return pos;
  745. pos += ret;
  746. return pos;
  747. }
  748. first = 1;
  749. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  750. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  751. if (ret < 0 || ret >= end - pos)
  752. return pos;
  753. pos += ret;
  754. first = 0;
  755. }
  756. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  757. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  758. if (ret < 0 || ret >= end - pos)
  759. return pos;
  760. pos += ret;
  761. first = 0;
  762. }
  763. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  764. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  765. if (ret < 0 || ret >= end - pos)
  766. return pos;
  767. pos += ret;
  768. first = 0;
  769. }
  770. #ifdef CONFIG_IEEE80211R
  771. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  772. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  773. first ? "" : "+");
  774. if (ret < 0 || ret >= end - pos)
  775. return pos;
  776. pos += ret;
  777. first = 0;
  778. }
  779. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  780. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  781. first ? "" : "+");
  782. if (ret < 0 || ret >= end - pos)
  783. return pos;
  784. pos += ret;
  785. first = 0;
  786. }
  787. #endif /* CONFIG_IEEE80211R */
  788. #ifdef CONFIG_IEEE80211W
  789. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  790. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  791. first ? "" : "+");
  792. if (ret < 0 || ret >= end - pos)
  793. return pos;
  794. pos += ret;
  795. first = 0;
  796. }
  797. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  798. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  799. first ? "" : "+");
  800. if (ret < 0 || ret >= end - pos)
  801. return pos;
  802. pos += ret;
  803. first = 0;
  804. }
  805. #endif /* CONFIG_IEEE80211W */
  806. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  807. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  808. ret = os_snprintf(pos, end - pos, "-preauth");
  809. if (ret < 0 || ret >= end - pos)
  810. return pos;
  811. pos += ret;
  812. }
  813. ret = os_snprintf(pos, end - pos, "]");
  814. if (ret < 0 || ret >= end - pos)
  815. return pos;
  816. pos += ret;
  817. return pos;
  818. }
  819. #ifdef CONFIG_WPS
  820. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  821. char *pos, char *end,
  822. struct wpabuf *wps_ie)
  823. {
  824. int ret;
  825. const char *txt;
  826. if (wps_ie == NULL)
  827. return pos;
  828. if (wps_is_selected_pbc_registrar(wps_ie))
  829. txt = "[WPS-PBC]";
  830. #ifdef CONFIG_WPS2
  831. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  832. txt = "[WPS-AUTH]";
  833. #endif /* CONFIG_WPS2 */
  834. else if (wps_is_selected_pin_registrar(wps_ie))
  835. txt = "[WPS-PIN]";
  836. else
  837. txt = "[WPS]";
  838. ret = os_snprintf(pos, end - pos, "%s", txt);
  839. if (ret >= 0 && ret < end - pos)
  840. pos += ret;
  841. wpabuf_free(wps_ie);
  842. return pos;
  843. }
  844. #endif /* CONFIG_WPS */
  845. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  846. char *pos, char *end,
  847. const struct wpa_bss *bss)
  848. {
  849. #ifdef CONFIG_WPS
  850. struct wpabuf *wps_ie;
  851. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  852. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  853. #else /* CONFIG_WPS */
  854. return pos;
  855. #endif /* CONFIG_WPS */
  856. }
  857. /* Format one result on one text line into a buffer. */
  858. static int wpa_supplicant_ctrl_iface_scan_result(
  859. struct wpa_supplicant *wpa_s,
  860. const struct wpa_bss *bss, char *buf, size_t buflen)
  861. {
  862. char *pos, *end;
  863. int ret;
  864. const u8 *ie, *ie2, *p2p;
  865. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  866. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  867. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  868. 0)
  869. return 0; /* Do not show P2P listen discovery results here */
  870. pos = buf;
  871. end = buf + buflen;
  872. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  873. MAC2STR(bss->bssid), bss->freq, bss->level);
  874. if (ret < 0 || ret >= end - pos)
  875. return pos - buf;
  876. pos += ret;
  877. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  878. if (ie)
  879. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  880. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  881. if (ie2)
  882. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  883. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  884. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  885. ret = os_snprintf(pos, end - pos, "[WEP]");
  886. if (ret < 0 || ret >= end - pos)
  887. return pos - buf;
  888. pos += ret;
  889. }
  890. if (bss->caps & IEEE80211_CAP_IBSS) {
  891. ret = os_snprintf(pos, end - pos, "[IBSS]");
  892. if (ret < 0 || ret >= end - pos)
  893. return pos - buf;
  894. pos += ret;
  895. }
  896. if (bss->caps & IEEE80211_CAP_ESS) {
  897. ret = os_snprintf(pos, end - pos, "[ESS]");
  898. if (ret < 0 || ret >= end - pos)
  899. return pos - buf;
  900. pos += ret;
  901. }
  902. if (p2p) {
  903. ret = os_snprintf(pos, end - pos, "[P2P]");
  904. if (ret < 0 || ret >= end - pos)
  905. return pos - buf;
  906. pos += ret;
  907. }
  908. ret = os_snprintf(pos, end - pos, "\t%s",
  909. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  910. if (ret < 0 || ret >= end - pos)
  911. return pos - buf;
  912. pos += ret;
  913. ret = os_snprintf(pos, end - pos, "\n");
  914. if (ret < 0 || ret >= end - pos)
  915. return pos - buf;
  916. pos += ret;
  917. return pos - buf;
  918. }
  919. static int wpa_supplicant_ctrl_iface_scan_results(
  920. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  921. {
  922. char *pos, *end;
  923. struct wpa_bss *bss;
  924. int ret;
  925. pos = buf;
  926. end = buf + buflen;
  927. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  928. "flags / ssid\n");
  929. if (ret < 0 || ret >= end - pos)
  930. return pos - buf;
  931. pos += ret;
  932. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  933. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  934. end - pos);
  935. if (ret < 0 || ret >= end - pos)
  936. return pos - buf;
  937. pos += ret;
  938. }
  939. return pos - buf;
  940. }
  941. static int wpa_supplicant_ctrl_iface_select_network(
  942. struct wpa_supplicant *wpa_s, char *cmd)
  943. {
  944. int id;
  945. struct wpa_ssid *ssid;
  946. /* cmd: "<network id>" or "any" */
  947. if (os_strcmp(cmd, "any") == 0) {
  948. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  949. ssid = NULL;
  950. } else {
  951. id = atoi(cmd);
  952. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  953. ssid = wpa_config_get_network(wpa_s->conf, id);
  954. if (ssid == NULL) {
  955. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  956. "network id=%d", id);
  957. return -1;
  958. }
  959. if (ssid->disabled == 2) {
  960. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  961. "SELECT_NETWORK with persistent P2P group");
  962. return -1;
  963. }
  964. }
  965. wpa_supplicant_select_network(wpa_s, ssid);
  966. return 0;
  967. }
  968. static int wpa_supplicant_ctrl_iface_enable_network(
  969. struct wpa_supplicant *wpa_s, char *cmd)
  970. {
  971. int id;
  972. struct wpa_ssid *ssid;
  973. /* cmd: "<network id>" or "all" */
  974. if (os_strcmp(cmd, "all") == 0) {
  975. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  976. ssid = NULL;
  977. } else {
  978. id = atoi(cmd);
  979. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  980. ssid = wpa_config_get_network(wpa_s->conf, id);
  981. if (ssid == NULL) {
  982. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  983. "network id=%d", id);
  984. return -1;
  985. }
  986. if (ssid->disabled == 2) {
  987. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  988. "ENABLE_NETWORK with persistent P2P group");
  989. return -1;
  990. }
  991. }
  992. wpa_supplicant_enable_network(wpa_s, ssid);
  993. return 0;
  994. }
  995. static int wpa_supplicant_ctrl_iface_disable_network(
  996. struct wpa_supplicant *wpa_s, char *cmd)
  997. {
  998. int id;
  999. struct wpa_ssid *ssid;
  1000. /* cmd: "<network id>" or "all" */
  1001. if (os_strcmp(cmd, "all") == 0) {
  1002. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1003. ssid = NULL;
  1004. } else {
  1005. id = atoi(cmd);
  1006. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1007. ssid = wpa_config_get_network(wpa_s->conf, id);
  1008. if (ssid == NULL) {
  1009. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1010. "network id=%d", id);
  1011. return -1;
  1012. }
  1013. if (ssid->disabled == 2) {
  1014. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1015. "DISABLE_NETWORK with persistent P2P "
  1016. "group");
  1017. return -1;
  1018. }
  1019. }
  1020. wpa_supplicant_disable_network(wpa_s, ssid);
  1021. return 0;
  1022. }
  1023. static int wpa_supplicant_ctrl_iface_add_network(
  1024. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1025. {
  1026. struct wpa_ssid *ssid;
  1027. int ret;
  1028. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1029. ssid = wpa_config_add_network(wpa_s->conf);
  1030. if (ssid == NULL)
  1031. return -1;
  1032. wpas_notify_network_added(wpa_s, ssid);
  1033. ssid->disabled = 1;
  1034. wpa_config_set_network_defaults(ssid);
  1035. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1036. if (ret < 0 || (size_t) ret >= buflen)
  1037. return -1;
  1038. return ret;
  1039. }
  1040. static int wpa_supplicant_ctrl_iface_remove_network(
  1041. struct wpa_supplicant *wpa_s, char *cmd)
  1042. {
  1043. int id;
  1044. struct wpa_ssid *ssid;
  1045. /* cmd: "<network id>" or "all" */
  1046. if (os_strcmp(cmd, "all") == 0) {
  1047. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1048. ssid = wpa_s->conf->ssid;
  1049. while (ssid) {
  1050. struct wpa_ssid *remove_ssid = ssid;
  1051. id = ssid->id;
  1052. ssid = ssid->next;
  1053. wpas_notify_network_removed(wpa_s, remove_ssid);
  1054. wpa_config_remove_network(wpa_s->conf, id);
  1055. }
  1056. if (wpa_s->current_ssid) {
  1057. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1058. wpa_supplicant_disassociate(wpa_s,
  1059. WLAN_REASON_DEAUTH_LEAVING);
  1060. }
  1061. return 0;
  1062. }
  1063. id = atoi(cmd);
  1064. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1065. ssid = wpa_config_get_network(wpa_s->conf, id);
  1066. if (ssid == NULL ||
  1067. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1068. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1069. "id=%d", id);
  1070. return -1;
  1071. }
  1072. if (ssid == wpa_s->current_ssid) {
  1073. /*
  1074. * Invalidate the EAP session cache if the current network is
  1075. * removed.
  1076. */
  1077. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1078. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1079. }
  1080. return 0;
  1081. }
  1082. static int wpa_supplicant_ctrl_iface_set_network(
  1083. struct wpa_supplicant *wpa_s, char *cmd)
  1084. {
  1085. int id;
  1086. struct wpa_ssid *ssid;
  1087. char *name, *value;
  1088. /* cmd: "<network id> <variable name> <value>" */
  1089. name = os_strchr(cmd, ' ');
  1090. if (name == NULL)
  1091. return -1;
  1092. *name++ = '\0';
  1093. value = os_strchr(name, ' ');
  1094. if (value == NULL)
  1095. return -1;
  1096. *value++ = '\0';
  1097. id = atoi(cmd);
  1098. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1099. id, name);
  1100. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1101. (u8 *) value, os_strlen(value));
  1102. ssid = wpa_config_get_network(wpa_s->conf, id);
  1103. if (ssid == NULL) {
  1104. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1105. "id=%d", id);
  1106. return -1;
  1107. }
  1108. if (wpa_config_set(ssid, name, value, 0) < 0) {
  1109. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  1110. "variable '%s'", name);
  1111. return -1;
  1112. }
  1113. if (wpa_s->current_ssid == ssid) {
  1114. /*
  1115. * Invalidate the EAP session cache if anything in the current
  1116. * configuration changes.
  1117. */
  1118. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1119. }
  1120. if ((os_strcmp(name, "psk") == 0 &&
  1121. value[0] == '"' && ssid->ssid_len) ||
  1122. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  1123. wpa_config_update_psk(ssid);
  1124. else if (os_strcmp(name, "priority") == 0)
  1125. wpa_config_update_prio_list(wpa_s->conf);
  1126. return 0;
  1127. }
  1128. static int wpa_supplicant_ctrl_iface_get_network(
  1129. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1130. {
  1131. int id;
  1132. size_t res;
  1133. struct wpa_ssid *ssid;
  1134. char *name, *value;
  1135. /* cmd: "<network id> <variable name>" */
  1136. name = os_strchr(cmd, ' ');
  1137. if (name == NULL || buflen == 0)
  1138. return -1;
  1139. *name++ = '\0';
  1140. id = atoi(cmd);
  1141. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1142. id, name);
  1143. ssid = wpa_config_get_network(wpa_s->conf, id);
  1144. if (ssid == NULL) {
  1145. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1146. "id=%d", id);
  1147. return -1;
  1148. }
  1149. value = wpa_config_get_no_key(ssid, name);
  1150. if (value == NULL) {
  1151. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1152. "variable '%s'", name);
  1153. return -1;
  1154. }
  1155. res = os_strlcpy(buf, value, buflen);
  1156. if (res >= buflen) {
  1157. os_free(value);
  1158. return -1;
  1159. }
  1160. os_free(value);
  1161. return res;
  1162. }
  1163. #ifndef CONFIG_NO_CONFIG_WRITE
  1164. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  1165. {
  1166. int ret;
  1167. if (!wpa_s->conf->update_config) {
  1168. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  1169. "to update configuration (update_config=0)");
  1170. return -1;
  1171. }
  1172. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1173. if (ret) {
  1174. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  1175. "update configuration");
  1176. } else {
  1177. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  1178. " updated");
  1179. }
  1180. return ret;
  1181. }
  1182. #endif /* CONFIG_NO_CONFIG_WRITE */
  1183. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1184. struct wpa_driver_capa *capa,
  1185. char *buf, size_t buflen)
  1186. {
  1187. int ret, first = 1;
  1188. char *pos, *end;
  1189. size_t len;
  1190. pos = buf;
  1191. end = pos + buflen;
  1192. if (res < 0) {
  1193. if (strict)
  1194. return 0;
  1195. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1196. if (len >= buflen)
  1197. return -1;
  1198. return len;
  1199. }
  1200. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1201. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1202. if (ret < 0 || ret >= end - pos)
  1203. return pos - buf;
  1204. pos += ret;
  1205. first = 0;
  1206. }
  1207. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1208. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1209. if (ret < 0 || ret >= end - pos)
  1210. return pos - buf;
  1211. pos += ret;
  1212. first = 0;
  1213. }
  1214. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1215. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1216. if (ret < 0 || ret >= end - pos)
  1217. return pos - buf;
  1218. pos += ret;
  1219. first = 0;
  1220. }
  1221. return pos - buf;
  1222. }
  1223. static int ctrl_iface_get_capability_group(int res, char *strict,
  1224. struct wpa_driver_capa *capa,
  1225. char *buf, size_t buflen)
  1226. {
  1227. int ret, first = 1;
  1228. char *pos, *end;
  1229. size_t len;
  1230. pos = buf;
  1231. end = pos + buflen;
  1232. if (res < 0) {
  1233. if (strict)
  1234. return 0;
  1235. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1236. if (len >= buflen)
  1237. return -1;
  1238. return len;
  1239. }
  1240. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1241. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1242. if (ret < 0 || ret >= end - pos)
  1243. return pos - buf;
  1244. pos += ret;
  1245. first = 0;
  1246. }
  1247. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1248. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1249. if (ret < 0 || ret >= end - pos)
  1250. return pos - buf;
  1251. pos += ret;
  1252. first = 0;
  1253. }
  1254. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1255. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1256. first ? "" : " ");
  1257. if (ret < 0 || ret >= end - pos)
  1258. return pos - buf;
  1259. pos += ret;
  1260. first = 0;
  1261. }
  1262. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1263. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1264. if (ret < 0 || ret >= end - pos)
  1265. return pos - buf;
  1266. pos += ret;
  1267. first = 0;
  1268. }
  1269. return pos - buf;
  1270. }
  1271. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1272. struct wpa_driver_capa *capa,
  1273. char *buf, size_t buflen)
  1274. {
  1275. int ret;
  1276. char *pos, *end;
  1277. size_t len;
  1278. pos = buf;
  1279. end = pos + buflen;
  1280. if (res < 0) {
  1281. if (strict)
  1282. return 0;
  1283. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1284. "NONE", buflen);
  1285. if (len >= buflen)
  1286. return -1;
  1287. return len;
  1288. }
  1289. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1290. if (ret < 0 || ret >= end - pos)
  1291. return pos - buf;
  1292. pos += ret;
  1293. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1294. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1295. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1296. if (ret < 0 || ret >= end - pos)
  1297. return pos - buf;
  1298. pos += ret;
  1299. }
  1300. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1301. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1302. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1303. if (ret < 0 || ret >= end - pos)
  1304. return pos - buf;
  1305. pos += ret;
  1306. }
  1307. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1308. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1309. if (ret < 0 || ret >= end - pos)
  1310. return pos - buf;
  1311. pos += ret;
  1312. }
  1313. return pos - buf;
  1314. }
  1315. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1316. struct wpa_driver_capa *capa,
  1317. char *buf, size_t buflen)
  1318. {
  1319. int ret, first = 1;
  1320. char *pos, *end;
  1321. size_t len;
  1322. pos = buf;
  1323. end = pos + buflen;
  1324. if (res < 0) {
  1325. if (strict)
  1326. return 0;
  1327. len = os_strlcpy(buf, "RSN WPA", buflen);
  1328. if (len >= buflen)
  1329. return -1;
  1330. return len;
  1331. }
  1332. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1333. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1334. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1335. if (ret < 0 || ret >= end - pos)
  1336. return pos - buf;
  1337. pos += ret;
  1338. first = 0;
  1339. }
  1340. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1341. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1342. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1343. if (ret < 0 || ret >= end - pos)
  1344. return pos - buf;
  1345. pos += ret;
  1346. first = 0;
  1347. }
  1348. return pos - buf;
  1349. }
  1350. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  1351. struct wpa_driver_capa *capa,
  1352. char *buf, size_t buflen)
  1353. {
  1354. int ret, first = 1;
  1355. char *pos, *end;
  1356. size_t len;
  1357. pos = buf;
  1358. end = pos + buflen;
  1359. if (res < 0) {
  1360. if (strict)
  1361. return 0;
  1362. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  1363. if (len >= buflen)
  1364. return -1;
  1365. return len;
  1366. }
  1367. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1368. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1369. if (ret < 0 || ret >= end - pos)
  1370. return pos - buf;
  1371. pos += ret;
  1372. first = 0;
  1373. }
  1374. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1375. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1376. first ? "" : " ");
  1377. if (ret < 0 || ret >= end - pos)
  1378. return pos - buf;
  1379. pos += ret;
  1380. first = 0;
  1381. }
  1382. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  1383. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  1384. if (ret < 0 || ret >= end - pos)
  1385. return pos - buf;
  1386. pos += ret;
  1387. first = 0;
  1388. }
  1389. return pos - buf;
  1390. }
  1391. static int wpa_supplicant_ctrl_iface_get_capability(
  1392. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  1393. size_t buflen)
  1394. {
  1395. struct wpa_driver_capa capa;
  1396. int res;
  1397. char *strict;
  1398. char field[30];
  1399. size_t len;
  1400. /* Determine whether or not strict checking was requested */
  1401. len = os_strlcpy(field, _field, sizeof(field));
  1402. if (len >= sizeof(field))
  1403. return -1;
  1404. strict = os_strchr(field, ' ');
  1405. if (strict != NULL) {
  1406. *strict++ = '\0';
  1407. if (os_strcmp(strict, "strict") != 0)
  1408. return -1;
  1409. }
  1410. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1411. field, strict ? strict : "");
  1412. if (os_strcmp(field, "eap") == 0) {
  1413. return eap_get_names(buf, buflen);
  1414. }
  1415. res = wpa_drv_get_capa(wpa_s, &capa);
  1416. if (os_strcmp(field, "pairwise") == 0)
  1417. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1418. buf, buflen);
  1419. if (os_strcmp(field, "group") == 0)
  1420. return ctrl_iface_get_capability_group(res, strict, &capa,
  1421. buf, buflen);
  1422. if (os_strcmp(field, "key_mgmt") == 0)
  1423. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1424. buf, buflen);
  1425. if (os_strcmp(field, "proto") == 0)
  1426. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1427. buf, buflen);
  1428. if (os_strcmp(field, "auth_alg") == 0)
  1429. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1430. buf, buflen);
  1431. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  1432. field);
  1433. return -1;
  1434. }
  1435. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  1436. const char *cmd, char *buf,
  1437. size_t buflen)
  1438. {
  1439. u8 bssid[ETH_ALEN];
  1440. size_t i;
  1441. struct wpa_bss *bss;
  1442. int ret;
  1443. char *pos, *end;
  1444. const u8 *ie, *ie2;
  1445. if (os_strcmp(cmd, "FIRST") == 0)
  1446. bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
  1447. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  1448. i = atoi(cmd + 3);
  1449. bss = wpa_bss_get_id(wpa_s, i);
  1450. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  1451. i = atoi(cmd + 5);
  1452. bss = wpa_bss_get_id(wpa_s, i);
  1453. if (bss) {
  1454. struct dl_list *next = bss->list_id.next;
  1455. if (next == &wpa_s->bss_id)
  1456. bss = NULL;
  1457. else
  1458. bss = dl_list_entry(next, struct wpa_bss,
  1459. list_id);
  1460. }
  1461. } else if (hwaddr_aton(cmd, bssid) == 0)
  1462. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1463. else {
  1464. struct wpa_bss *tmp;
  1465. i = atoi(cmd);
  1466. bss = NULL;
  1467. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  1468. {
  1469. if (i-- == 0) {
  1470. bss = tmp;
  1471. break;
  1472. }
  1473. }
  1474. }
  1475. if (bss == NULL)
  1476. return 0;
  1477. pos = buf;
  1478. end = buf + buflen;
  1479. ret = os_snprintf(pos, end - pos,
  1480. "id=%u\n"
  1481. "bssid=" MACSTR "\n"
  1482. "freq=%d\n"
  1483. "beacon_int=%d\n"
  1484. "capabilities=0x%04x\n"
  1485. "qual=%d\n"
  1486. "noise=%d\n"
  1487. "level=%d\n"
  1488. "tsf=%016llu\n"
  1489. "ie=",
  1490. bss->id,
  1491. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1492. bss->caps, bss->qual, bss->noise, bss->level,
  1493. (unsigned long long) bss->tsf);
  1494. if (ret < 0 || ret >= end - pos)
  1495. return pos - buf;
  1496. pos += ret;
  1497. ie = (const u8 *) (bss + 1);
  1498. for (i = 0; i < bss->ie_len; i++) {
  1499. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  1500. if (ret < 0 || ret >= end - pos)
  1501. return pos - buf;
  1502. pos += ret;
  1503. }
  1504. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
  1505. ret = os_snprintf(pos, end - pos, "[P2P]");
  1506. if (ret < 0 || ret >= end - pos)
  1507. return pos - buf;
  1508. pos += ret;
  1509. }
  1510. ret = os_snprintf(pos, end - pos, "\n");
  1511. if (ret < 0 || ret >= end - pos)
  1512. return pos - buf;
  1513. pos += ret;
  1514. ret = os_snprintf(pos, end - pos, "flags=");
  1515. if (ret < 0 || ret >= end - pos)
  1516. return pos - buf;
  1517. pos += ret;
  1518. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1519. if (ie)
  1520. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1521. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1522. if (ie2)
  1523. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1524. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1525. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1526. ret = os_snprintf(pos, end - pos, "[WEP]");
  1527. if (ret < 0 || ret >= end - pos)
  1528. return pos - buf;
  1529. pos += ret;
  1530. }
  1531. if (bss->caps & IEEE80211_CAP_IBSS) {
  1532. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1533. if (ret < 0 || ret >= end - pos)
  1534. return pos - buf;
  1535. pos += ret;
  1536. }
  1537. if (bss->caps & IEEE80211_CAP_ESS) {
  1538. ret = os_snprintf(pos, end - pos, "[ESS]");
  1539. if (ret < 0 || ret >= end - pos)
  1540. return pos - buf;
  1541. pos += ret;
  1542. }
  1543. ret = os_snprintf(pos, end - pos, "\n");
  1544. if (ret < 0 || ret >= end - pos)
  1545. return pos - buf;
  1546. pos += ret;
  1547. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1548. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1549. if (ret < 0 || ret >= end - pos)
  1550. return pos - buf;
  1551. pos += ret;
  1552. #ifdef CONFIG_WPS
  1553. ie = (const u8 *) (bss + 1);
  1554. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  1555. if (ret < 0 || ret >= end - pos)
  1556. return pos - buf;
  1557. pos += ret;
  1558. #endif /* CONFIG_WPS */
  1559. #ifdef CONFIG_P2P
  1560. ie = (const u8 *) (bss + 1);
  1561. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  1562. if (ret < 0 || ret >= end - pos)
  1563. return pos - buf;
  1564. pos += ret;
  1565. #endif /* CONFIG_P2P */
  1566. return pos - buf;
  1567. }
  1568. static int wpa_supplicant_ctrl_iface_ap_scan(
  1569. struct wpa_supplicant *wpa_s, char *cmd)
  1570. {
  1571. int ap_scan = atoi(cmd);
  1572. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  1573. }
  1574. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  1575. {
  1576. u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
  1577. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  1578. /* MLME-DELETEKEYS.request */
  1579. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
  1580. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
  1581. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
  1582. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
  1583. #ifdef CONFIG_IEEE80211W
  1584. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
  1585. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
  1586. #endif /* CONFIG_IEEE80211W */
  1587. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  1588. 0);
  1589. /* MLME-SETPROTECTION.request(None) */
  1590. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  1591. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  1592. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1593. wpa_sm_drop_sa(wpa_s->wpa);
  1594. }
  1595. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  1596. char *addr)
  1597. {
  1598. u8 bssid[ETH_ALEN];
  1599. struct wpa_bss *bss;
  1600. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1601. if (hwaddr_aton(addr, bssid)) {
  1602. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  1603. "address '%s'", addr);
  1604. return -1;
  1605. }
  1606. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  1607. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1608. if (!bss) {
  1609. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  1610. "from BSS table");
  1611. return -1;
  1612. }
  1613. /*
  1614. * TODO: Find best network configuration block from configuration to
  1615. * allow roaming to other networks
  1616. */
  1617. if (!ssid) {
  1618. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  1619. "configuration known for the target AP");
  1620. return -1;
  1621. }
  1622. wpa_s->reassociate = 1;
  1623. wpa_supplicant_connect(wpa_s, bss, ssid);
  1624. return 0;
  1625. }
  1626. #ifdef CONFIG_P2P
  1627. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  1628. {
  1629. unsigned int timeout = atoi(cmd);
  1630. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  1631. if (os_strstr(cmd, "type=social"))
  1632. type = P2P_FIND_ONLY_SOCIAL;
  1633. else if (os_strstr(cmd, "type=progressive"))
  1634. type = P2P_FIND_PROGRESSIVE;
  1635. wpas_p2p_find(wpa_s, timeout, type);
  1636. return 0;
  1637. }
  1638. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  1639. char *buf, size_t buflen)
  1640. {
  1641. u8 addr[ETH_ALEN];
  1642. char *pos, *pos2;
  1643. char *pin = NULL;
  1644. enum p2p_wps_method wps_method;
  1645. int new_pin;
  1646. int ret;
  1647. int persistent_group;
  1648. int join;
  1649. int auth;
  1650. int go_intent = -1;
  1651. int freq = 0;
  1652. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad] [persistent]
  1653. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] */
  1654. if (hwaddr_aton(cmd, addr))
  1655. return -1;
  1656. pos = cmd + 17;
  1657. if (*pos != ' ')
  1658. return -1;
  1659. pos++;
  1660. persistent_group = os_strstr(pos, " persistent") != NULL;
  1661. join = os_strstr(pos, " join") != NULL;
  1662. auth = os_strstr(pos, " auth") != NULL;
  1663. pos2 = os_strstr(pos, " go_intent=");
  1664. if (pos2) {
  1665. pos2 += 11;
  1666. go_intent = atoi(pos2);
  1667. if (go_intent < 0 || go_intent > 15)
  1668. return -1;
  1669. }
  1670. pos2 = os_strstr(pos, " freq=");
  1671. if (pos2) {
  1672. pos2 += 6;
  1673. freq = atoi(pos2);
  1674. if (freq <= 0)
  1675. return -1;
  1676. }
  1677. if (os_strncmp(pos, "pin", 3) == 0) {
  1678. /* Request random PIN (to be displayed) and enable the PIN */
  1679. wps_method = WPS_PIN_DISPLAY;
  1680. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  1681. wps_method = WPS_PBC;
  1682. } else {
  1683. pin = pos;
  1684. pos = os_strchr(pin, ' ');
  1685. wps_method = WPS_PIN_KEYPAD;
  1686. if (pos) {
  1687. *pos++ = '\0';
  1688. if (os_strncmp(pos, "label", 5) == 0)
  1689. wps_method = WPS_PIN_LABEL;
  1690. else if (os_strncmp(pos, "display", 7) == 0)
  1691. wps_method = WPS_PIN_DISPLAY;
  1692. }
  1693. }
  1694. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  1695. persistent_group, join, auth, go_intent,
  1696. freq);
  1697. if (new_pin == -2) {
  1698. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  1699. return 25;
  1700. }
  1701. if (new_pin == -3) {
  1702. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  1703. return 25;
  1704. }
  1705. if (new_pin < 0)
  1706. return -1;
  1707. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  1708. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  1709. if (ret < 0 || (size_t) ret >= buflen)
  1710. return -1;
  1711. return ret;
  1712. }
  1713. os_memcpy(buf, "OK\n", 3);
  1714. return 3;
  1715. }
  1716. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  1717. {
  1718. unsigned int timeout = atoi(cmd);
  1719. return wpas_p2p_listen(wpa_s, timeout);
  1720. }
  1721. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  1722. {
  1723. u8 addr[ETH_ALEN];
  1724. char *pos;
  1725. /* <addr> <config method> */
  1726. if (hwaddr_aton(cmd, addr))
  1727. return -1;
  1728. pos = cmd + 17;
  1729. if (*pos != ' ')
  1730. return -1;
  1731. pos++;
  1732. return wpas_p2p_prov_disc(wpa_s, addr, pos);
  1733. }
  1734. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  1735. size_t buflen)
  1736. {
  1737. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1738. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  1739. ssid->passphrase == NULL)
  1740. return -1;
  1741. os_strlcpy(buf, ssid->passphrase, buflen);
  1742. return os_strlen(buf);
  1743. }
  1744. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  1745. char *buf, size_t buflen)
  1746. {
  1747. u64 ref;
  1748. int res;
  1749. u8 dst_buf[ETH_ALEN], *dst;
  1750. struct wpabuf *tlvs;
  1751. char *pos;
  1752. size_t len;
  1753. if (hwaddr_aton(cmd, dst_buf))
  1754. return -1;
  1755. dst = dst_buf;
  1756. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  1757. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  1758. dst = NULL;
  1759. pos = cmd + 17;
  1760. if (*pos != ' ')
  1761. return -1;
  1762. pos++;
  1763. if (os_strncmp(pos, "upnp ", 5) == 0) {
  1764. u8 version;
  1765. pos += 5;
  1766. if (hexstr2bin(pos, &version, 1) < 0)
  1767. return -1;
  1768. pos += 2;
  1769. if (*pos != ' ')
  1770. return -1;
  1771. pos++;
  1772. ref = (u64) wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  1773. } else {
  1774. len = os_strlen(pos);
  1775. if (len & 1)
  1776. return -1;
  1777. len /= 2;
  1778. tlvs = wpabuf_alloc(len);
  1779. if (tlvs == NULL)
  1780. return -1;
  1781. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  1782. wpabuf_free(tlvs);
  1783. return -1;
  1784. }
  1785. ref = (u64) wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1786. wpabuf_free(tlvs);
  1787. }
  1788. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  1789. if (res < 0 || (unsigned) res >= buflen)
  1790. return -1;
  1791. return res;
  1792. }
  1793. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  1794. char *cmd)
  1795. {
  1796. long long unsigned val;
  1797. u64 req;
  1798. if (sscanf(cmd, "%llx", &val) != 1)
  1799. return -1;
  1800. req = val;
  1801. return wpas_p2p_sd_cancel_request(wpa_s, (void *) req);
  1802. }
  1803. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  1804. {
  1805. int freq;
  1806. u8 dst_buf[ETH_ALEN], *dst;
  1807. u8 dialog_token;
  1808. struct wpabuf *resp_tlvs;
  1809. char *pos, *pos2;
  1810. size_t len;
  1811. pos = os_strchr(cmd, ' ');
  1812. if (pos == NULL)
  1813. return -1;
  1814. *pos++ = '\0';
  1815. freq = atoi(cmd);
  1816. if (freq == 0)
  1817. return -1;
  1818. if (hwaddr_aton(pos, dst_buf))
  1819. return -1;
  1820. dst = dst_buf;
  1821. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  1822. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  1823. dst = NULL;
  1824. pos += 17;
  1825. if (*pos != ' ')
  1826. return -1;
  1827. pos++;
  1828. pos2 = os_strchr(pos, ' ');
  1829. if (pos2 == NULL)
  1830. return -1;
  1831. *pos2++ = '\0';
  1832. dialog_token = atoi(pos);
  1833. len = os_strlen(pos2);
  1834. if (len & 1)
  1835. return -1;
  1836. len /= 2;
  1837. resp_tlvs = wpabuf_alloc(len);
  1838. if (resp_tlvs == NULL)
  1839. return -1;
  1840. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  1841. wpabuf_free(resp_tlvs);
  1842. return -1;
  1843. }
  1844. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  1845. wpabuf_free(resp_tlvs);
  1846. return 0;
  1847. }
  1848. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  1849. char *cmd)
  1850. {
  1851. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  1852. return 0;
  1853. }
  1854. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1855. char *cmd)
  1856. {
  1857. char *pos;
  1858. size_t len;
  1859. struct wpabuf *query, *resp;
  1860. pos = os_strchr(cmd, ' ');
  1861. if (pos == NULL)
  1862. return -1;
  1863. *pos++ = '\0';
  1864. len = os_strlen(cmd);
  1865. if (len & 1)
  1866. return -1;
  1867. len /= 2;
  1868. query = wpabuf_alloc(len);
  1869. if (query == NULL)
  1870. return -1;
  1871. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  1872. wpabuf_free(query);
  1873. return -1;
  1874. }
  1875. len = os_strlen(pos);
  1876. if (len & 1) {
  1877. wpabuf_free(query);
  1878. return -1;
  1879. }
  1880. len /= 2;
  1881. resp = wpabuf_alloc(len);
  1882. if (resp == NULL) {
  1883. wpabuf_free(query);
  1884. return -1;
  1885. }
  1886. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  1887. wpabuf_free(query);
  1888. wpabuf_free(resp);
  1889. return -1;
  1890. }
  1891. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  1892. wpabuf_free(query);
  1893. wpabuf_free(resp);
  1894. return -1;
  1895. }
  1896. return 0;
  1897. }
  1898. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  1899. {
  1900. char *pos;
  1901. u8 version;
  1902. pos = os_strchr(cmd, ' ');
  1903. if (pos == NULL)
  1904. return -1;
  1905. *pos++ = '\0';
  1906. if (hexstr2bin(cmd, &version, 1) < 0)
  1907. return -1;
  1908. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  1909. }
  1910. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  1911. {
  1912. char *pos;
  1913. pos = os_strchr(cmd, ' ');
  1914. if (pos == NULL)
  1915. return -1;
  1916. *pos++ = '\0';
  1917. if (os_strcmp(cmd, "bonjour") == 0)
  1918. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  1919. if (os_strcmp(cmd, "upnp") == 0)
  1920. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  1921. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  1922. return -1;
  1923. }
  1924. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1925. char *cmd)
  1926. {
  1927. size_t len;
  1928. struct wpabuf *query;
  1929. int ret;
  1930. len = os_strlen(cmd);
  1931. if (len & 1)
  1932. return -1;
  1933. len /= 2;
  1934. query = wpabuf_alloc(len);
  1935. if (query == NULL)
  1936. return -1;
  1937. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  1938. wpabuf_free(query);
  1939. return -1;
  1940. }
  1941. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  1942. wpabuf_free(query);
  1943. return ret;
  1944. }
  1945. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  1946. {
  1947. char *pos;
  1948. u8 version;
  1949. pos = os_strchr(cmd, ' ');
  1950. if (pos == NULL)
  1951. return -1;
  1952. *pos++ = '\0';
  1953. if (hexstr2bin(cmd, &version, 1) < 0)
  1954. return -1;
  1955. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  1956. }
  1957. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  1958. {
  1959. char *pos;
  1960. pos = os_strchr(cmd, ' ');
  1961. if (pos == NULL)
  1962. return -1;
  1963. *pos++ = '\0';
  1964. if (os_strcmp(cmd, "bonjour") == 0)
  1965. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  1966. if (os_strcmp(cmd, "upnp") == 0)
  1967. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  1968. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  1969. return -1;
  1970. }
  1971. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  1972. {
  1973. u8 addr[ETH_ALEN];
  1974. /* <addr> */
  1975. if (hwaddr_aton(cmd, addr))
  1976. return -1;
  1977. return wpas_p2p_reject(wpa_s, addr);
  1978. }
  1979. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  1980. {
  1981. char *pos;
  1982. int id;
  1983. struct wpa_ssid *ssid;
  1984. u8 peer[ETH_ALEN];
  1985. id = atoi(cmd);
  1986. pos = os_strstr(cmd, " peer=");
  1987. if (pos) {
  1988. pos += 6;
  1989. if (hwaddr_aton(pos, peer))
  1990. return -1;
  1991. }
  1992. ssid = wpa_config_get_network(wpa_s->conf, id);
  1993. if (ssid == NULL || ssid->disabled != 2) {
  1994. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1995. "for persistent P2P group",
  1996. id);
  1997. return -1;
  1998. }
  1999. return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
  2000. }
  2001. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  2002. {
  2003. char *pos;
  2004. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  2005. pos = os_strstr(cmd, " peer=");
  2006. if (!pos)
  2007. return -1;
  2008. *pos = '\0';
  2009. pos += 6;
  2010. if (hwaddr_aton(pos, peer)) {
  2011. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  2012. return -1;
  2013. }
  2014. pos = os_strstr(pos, " go_dev_addr=");
  2015. if (pos) {
  2016. pos += 13;
  2017. if (hwaddr_aton(pos, go_dev_addr)) {
  2018. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  2019. pos);
  2020. return -1;
  2021. }
  2022. go_dev = go_dev_addr;
  2023. }
  2024. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  2025. }
  2026. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  2027. {
  2028. if (os_strncmp(cmd, "persistent=", 11) == 0)
  2029. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  2030. if (os_strncmp(cmd, "group=", 6) == 0)
  2031. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  2032. return -1;
  2033. }
  2034. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  2035. char *cmd, int freq)
  2036. {
  2037. int id;
  2038. struct wpa_ssid *ssid;
  2039. id = atoi(cmd);
  2040. ssid = wpa_config_get_network(wpa_s->conf, id);
  2041. if (ssid == NULL || ssid->disabled != 2) {
  2042. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  2043. "for persistent P2P group",
  2044. id);
  2045. return -1;
  2046. }
  2047. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq);
  2048. }
  2049. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  2050. {
  2051. int freq = 0;
  2052. char *pos;
  2053. pos = os_strstr(cmd, "freq=");
  2054. if (pos)
  2055. freq = atoi(pos + 5);
  2056. if (os_strncmp(cmd, "persistent=", 11) == 0)
  2057. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq);
  2058. if (os_strcmp(cmd, "persistent") == 0 ||
  2059. os_strncmp(cmd, "persistent ", 11) == 0)
  2060. return wpas_p2p_group_add(wpa_s, 1, freq);
  2061. if (os_strncmp(cmd, "freq=", 5) == 0)
  2062. return wpas_p2p_group_add(wpa_s, 0, freq);
  2063. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  2064. cmd);
  2065. return -1;
  2066. }
  2067. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  2068. char *buf, size_t buflen)
  2069. {
  2070. u8 addr[ETH_ALEN], *addr_ptr;
  2071. int next;
  2072. if (!wpa_s->global->p2p)
  2073. return -1;
  2074. if (os_strcmp(cmd, "FIRST") == 0) {
  2075. addr_ptr = NULL;
  2076. next = 0;
  2077. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2078. if (hwaddr_aton(cmd + 5, addr) < 0)
  2079. return -1;
  2080. addr_ptr = addr;
  2081. next = 1;
  2082. } else {
  2083. if (hwaddr_aton(cmd, addr) < 0)
  2084. return -1;
  2085. addr_ptr = addr;
  2086. next = 0;
  2087. }
  2088. return p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next,
  2089. buf, buflen);
  2090. }
  2091. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  2092. {
  2093. char *param;
  2094. if (wpa_s->global->p2p == NULL)
  2095. return -1;
  2096. param = os_strchr(cmd, ' ');
  2097. if (param == NULL)
  2098. return -1;
  2099. *param++ = '\0';
  2100. if (os_strcmp(cmd, "discoverability") == 0) {
  2101. p2p_set_client_discoverability(wpa_s->global->p2p,
  2102. atoi(param));
  2103. return 0;
  2104. }
  2105. if (os_strcmp(cmd, "managed") == 0) {
  2106. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  2107. return 0;
  2108. }
  2109. if (os_strcmp(cmd, "listen_channel") == 0) {
  2110. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  2111. atoi(param));
  2112. }
  2113. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  2114. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  2115. os_strlen(param));
  2116. }
  2117. if (os_strcmp(cmd, "noa") == 0) {
  2118. char *pos;
  2119. int count, start, duration;
  2120. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  2121. count = atoi(param);
  2122. pos = os_strchr(param, ',');
  2123. if (pos == NULL)
  2124. return -1;
  2125. pos++;
  2126. start = atoi(pos);
  2127. pos = os_strchr(pos, ',');
  2128. if (pos == NULL)
  2129. return -1;
  2130. pos++;
  2131. duration = atoi(pos);
  2132. if (count < 0 || count > 255 || start < 0 || duration < 0)
  2133. return -1;
  2134. if (count == 0 && duration > 0)
  2135. return -1;
  2136. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  2137. "start=%d duration=%d", count, start, duration);
  2138. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  2139. }
  2140. if (os_strcmp(cmd, "ps") == 0)
  2141. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  2142. if (os_strcmp(cmd, "oppps") == 0)
  2143. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  2144. if (os_strcmp(cmd, "ctwindow") == 0)
  2145. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  2146. if (os_strcmp(cmd, "disabled") == 0) {
  2147. wpa_s->global->p2p_disabled = atoi(param);
  2148. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  2149. wpa_s->global->p2p_disabled ?
  2150. "disabled" : "enabled");
  2151. if (wpa_s->global->p2p_disabled) {
  2152. wpas_p2p_stop_find(wpa_s);
  2153. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2154. p2p_flush(wpa_s->global->p2p);
  2155. }
  2156. return 0;
  2157. }
  2158. if (os_strcmp(cmd, "force_long_sd") == 0) {
  2159. wpa_s->force_long_sd = atoi(param);
  2160. return 0;
  2161. }
  2162. if (os_strcmp(cmd, "peer_filter") == 0) {
  2163. u8 addr[ETH_ALEN];
  2164. if (hwaddr_aton(param, addr))
  2165. return -1;
  2166. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  2167. return 0;
  2168. }
  2169. if (os_strcmp(cmd, "cross_connect") == 0)
  2170. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  2171. if (os_strcmp(cmd, "go_apsd") == 0) {
  2172. if (os_strcmp(param, "disable") == 0)
  2173. wpa_s->set_ap_uapsd = 0;
  2174. else {
  2175. wpa_s->set_ap_uapsd = 1;
  2176. wpa_s->ap_uapsd = atoi(param);
  2177. }
  2178. return 0;
  2179. }
  2180. if (os_strcmp(cmd, "client_apsd") == 0) {
  2181. if (os_strcmp(param, "disable") == 0)
  2182. wpa_s->set_sta_uapsd = 0;
  2183. else {
  2184. int be, bk, vi, vo;
  2185. char *pos;
  2186. /* format: BE,BK,VI,VO;max SP Length */
  2187. be = atoi(param);
  2188. pos = os_strchr(param, ',');
  2189. if (pos == NULL)
  2190. return -1;
  2191. pos++;
  2192. bk = atoi(pos);
  2193. pos = os_strchr(pos, ',');
  2194. if (pos == NULL)
  2195. return -1;
  2196. pos++;
  2197. vi = atoi(pos);
  2198. pos = os_strchr(pos, ',');
  2199. if (pos == NULL)
  2200. return -1;
  2201. pos++;
  2202. vo = atoi(pos);
  2203. /* ignore max SP Length for now */
  2204. wpa_s->set_sta_uapsd = 1;
  2205. wpa_s->sta_uapsd = 0;
  2206. if (be)
  2207. wpa_s->sta_uapsd |= BIT(0);
  2208. if (bk)
  2209. wpa_s->sta_uapsd |= BIT(1);
  2210. if (vi)
  2211. wpa_s->sta_uapsd |= BIT(2);
  2212. if (vo)
  2213. wpa_s->sta_uapsd |= BIT(3);
  2214. }
  2215. return 0;
  2216. }
  2217. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  2218. cmd);
  2219. return -1;
  2220. }
  2221. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  2222. {
  2223. char *pos, *pos2;
  2224. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  2225. if (cmd[0]) {
  2226. pos = os_strchr(cmd, ' ');
  2227. if (pos == NULL)
  2228. return -1;
  2229. *pos++ = '\0';
  2230. dur1 = atoi(cmd);
  2231. pos2 = os_strchr(pos, ' ');
  2232. if (pos2)
  2233. *pos2++ = '\0';
  2234. int1 = atoi(pos);
  2235. } else
  2236. pos2 = NULL;
  2237. if (pos2) {
  2238. pos = os_strchr(pos2, ' ');
  2239. if (pos == NULL)
  2240. return -1;
  2241. *pos++ = '\0';
  2242. dur2 = atoi(pos2);
  2243. int2 = atoi(pos);
  2244. }
  2245. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  2246. }
  2247. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  2248. {
  2249. char *pos;
  2250. unsigned int period = 0, interval = 0;
  2251. if (cmd[0]) {
  2252. pos = os_strchr(cmd, ' ');
  2253. if (pos == NULL)
  2254. return -1;
  2255. *pos++ = '\0';
  2256. period = atoi(cmd);
  2257. interval = atoi(pos);
  2258. }
  2259. return wpas_p2p_ext_listen(wpa_s, period, interval);
  2260. }
  2261. #endif /* CONFIG_P2P */
  2262. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  2263. struct wpa_supplicant *wpa_s, char *cmd)
  2264. {
  2265. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  2266. return 0;
  2267. }
  2268. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  2269. char *buf, size_t *resp_len)
  2270. {
  2271. char *reply;
  2272. const int reply_size = 4096;
  2273. int ctrl_rsp = 0;
  2274. int reply_len;
  2275. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  2276. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  2277. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  2278. (const u8 *) buf, os_strlen(buf));
  2279. } else {
  2280. int level = MSG_DEBUG;
  2281. if (os_strcmp(buf, "PING") == 0)
  2282. level = MSG_EXCESSIVE;
  2283. wpa_hexdump_ascii(level, "RX ctrl_iface",
  2284. (const u8 *) buf, os_strlen(buf));
  2285. }
  2286. reply = os_malloc(reply_size);
  2287. if (reply == NULL) {
  2288. *resp_len = 1;
  2289. return NULL;
  2290. }
  2291. os_memcpy(reply, "OK\n", 3);
  2292. reply_len = 3;
  2293. if (os_strcmp(buf, "PING") == 0) {
  2294. os_memcpy(reply, "PONG\n", 5);
  2295. reply_len = 5;
  2296. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  2297. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  2298. } else if (os_strcmp(buf, "MIB") == 0) {
  2299. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  2300. if (reply_len >= 0) {
  2301. int res;
  2302. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  2303. reply_size - reply_len);
  2304. if (res < 0)
  2305. reply_len = -1;
  2306. else
  2307. reply_len += res;
  2308. }
  2309. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  2310. reply_len = wpa_supplicant_ctrl_iface_status(
  2311. wpa_s, buf + 6, reply, reply_size);
  2312. } else if (os_strcmp(buf, "PMKSA") == 0) {
  2313. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  2314. reply_size);
  2315. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  2316. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  2317. reply_len = -1;
  2318. } else if (os_strcmp(buf, "LOGON") == 0) {
  2319. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  2320. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  2321. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  2322. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  2323. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2324. reply_len = -1;
  2325. else {
  2326. wpa_s->disconnected = 0;
  2327. wpa_s->reassociate = 1;
  2328. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2329. }
  2330. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  2331. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2332. reply_len = -1;
  2333. else if (wpa_s->disconnected) {
  2334. wpa_s->disconnected = 0;
  2335. wpa_s->reassociate = 1;
  2336. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2337. }
  2338. #ifdef IEEE8021X_EAPOL
  2339. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  2340. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  2341. reply_len = -1;
  2342. #endif /* IEEE8021X_EAPOL */
  2343. #ifdef CONFIG_PEERKEY
  2344. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  2345. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  2346. reply_len = -1;
  2347. #endif /* CONFIG_PEERKEY */
  2348. #ifdef CONFIG_IEEE80211R
  2349. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  2350. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  2351. reply_len = -1;
  2352. #endif /* CONFIG_IEEE80211R */
  2353. #ifdef CONFIG_WPS
  2354. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  2355. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
  2356. reply_len = -1;
  2357. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  2358. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
  2359. reply_len = -1;
  2360. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  2361. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  2362. reply,
  2363. reply_size);
  2364. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  2365. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  2366. wpa_s, buf + 14, reply, reply_size);
  2367. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  2368. if (wpas_wps_cancel(wpa_s))
  2369. reply_len = -1;
  2370. #ifdef CONFIG_WPS_OOB
  2371. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  2372. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  2373. reply_len = -1;
  2374. #endif /* CONFIG_WPS_OOB */
  2375. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  2376. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  2377. reply_len = -1;
  2378. #ifdef CONFIG_WPS_ER
  2379. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  2380. if (wpas_wps_er_start(wpa_s, NULL))
  2381. reply_len = -1;
  2382. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  2383. if (wpas_wps_er_start(wpa_s, buf + 13))
  2384. reply_len = -1;
  2385. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  2386. if (wpas_wps_er_stop(wpa_s))
  2387. reply_len = -1;
  2388. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  2389. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  2390. reply_len = -1;
  2391. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  2392. if (wpas_wps_er_pbc(wpa_s, buf + 11))
  2393. reply_len = -1;
  2394. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  2395. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  2396. reply_len = -1;
  2397. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  2398. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  2399. buf + 18))
  2400. reply_len = -1;
  2401. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  2402. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  2403. reply_len = -1;
  2404. #endif /* CONFIG_WPS_ER */
  2405. #endif /* CONFIG_WPS */
  2406. #ifdef CONFIG_IBSS_RSN
  2407. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  2408. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  2409. reply_len = -1;
  2410. #endif /* CONFIG_IBSS_RSN */
  2411. #ifdef CONFIG_P2P
  2412. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  2413. if (p2p_ctrl_find(wpa_s, buf + 9))
  2414. reply_len = -1;
  2415. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  2416. if (p2p_ctrl_find(wpa_s, ""))
  2417. reply_len = -1;
  2418. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  2419. wpas_p2p_stop_find(wpa_s);
  2420. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  2421. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  2422. reply_size);
  2423. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  2424. if (p2p_ctrl_listen(wpa_s, buf + 11))
  2425. reply_len = -1;
  2426. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  2427. if (p2p_ctrl_listen(wpa_s, ""))
  2428. reply_len = -1;
  2429. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  2430. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  2431. reply_len = -1;
  2432. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  2433. if (wpas_p2p_group_add(wpa_s, 0, 0))
  2434. reply_len = -1;
  2435. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  2436. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  2437. reply_len = -1;
  2438. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  2439. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  2440. reply_len = -1;
  2441. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  2442. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  2443. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  2444. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  2445. reply_size);
  2446. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  2447. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  2448. reply_len = -1;
  2449. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  2450. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  2451. reply_len = -1;
  2452. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  2453. wpas_p2p_sd_service_update(wpa_s);
  2454. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  2455. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  2456. reply_len = -1;
  2457. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  2458. wpas_p2p_service_flush(wpa_s);
  2459. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  2460. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  2461. reply_len = -1;
  2462. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  2463. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  2464. reply_len = -1;
  2465. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  2466. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  2467. reply_len = -1;
  2468. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  2469. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  2470. reply_len = -1;
  2471. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  2472. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  2473. reply_size);
  2474. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  2475. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  2476. reply_len = -1;
  2477. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  2478. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2479. wpa_s->force_long_sd = 0;
  2480. p2p_flush(wpa_s->global->p2p);
  2481. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  2482. if (wpas_p2p_cancel(wpa_s))
  2483. reply_len = -1;
  2484. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  2485. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  2486. reply_len = -1;
  2487. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  2488. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  2489. reply_len = -1;
  2490. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  2491. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  2492. reply_len = -1;
  2493. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  2494. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  2495. reply_len = -1;
  2496. #endif /* CONFIG_P2P */
  2497. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  2498. {
  2499. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  2500. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  2501. reply_len = -1;
  2502. else
  2503. ctrl_rsp = 1;
  2504. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  2505. if (wpa_supplicant_reload_configuration(wpa_s))
  2506. reply_len = -1;
  2507. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2508. wpa_supplicant_terminate_proc(wpa_s->global);
  2509. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  2510. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  2511. reply_len = -1;
  2512. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  2513. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  2514. wpa_s, reply, reply_size);
  2515. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  2516. wpa_s->reassociate = 0;
  2517. wpa_s->disconnected = 1;
  2518. wpa_supplicant_deauthenticate(wpa_s,
  2519. WLAN_REASON_DEAUTH_LEAVING);
  2520. } else if (os_strcmp(buf, "SCAN") == 0) {
  2521. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2522. reply_len = -1;
  2523. else {
  2524. wpa_s->scan_req = 2;
  2525. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2526. }
  2527. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  2528. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  2529. wpa_s, reply, reply_size);
  2530. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  2531. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  2532. reply_len = -1;
  2533. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  2534. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  2535. reply_len = -1;
  2536. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  2537. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  2538. reply_len = -1;
  2539. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  2540. reply_len = wpa_supplicant_ctrl_iface_add_network(
  2541. wpa_s, reply, reply_size);
  2542. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  2543. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  2544. reply_len = -1;
  2545. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  2546. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  2547. reply_len = -1;
  2548. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  2549. reply_len = wpa_supplicant_ctrl_iface_get_network(
  2550. wpa_s, buf + 12, reply, reply_size);
  2551. #ifndef CONFIG_NO_CONFIG_WRITE
  2552. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  2553. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  2554. reply_len = -1;
  2555. #endif /* CONFIG_NO_CONFIG_WRITE */
  2556. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  2557. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  2558. wpa_s, buf + 15, reply, reply_size);
  2559. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  2560. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  2561. reply_len = -1;
  2562. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  2563. reply_len = wpa_supplicant_global_iface_list(
  2564. wpa_s->global, reply, reply_size);
  2565. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  2566. reply_len = wpa_supplicant_global_iface_interfaces(
  2567. wpa_s->global, reply, reply_size);
  2568. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  2569. reply_len = wpa_supplicant_ctrl_iface_bss(
  2570. wpa_s, buf + 4, reply, reply_size);
  2571. #ifdef CONFIG_AP
  2572. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  2573. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  2574. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  2575. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  2576. reply_size);
  2577. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  2578. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  2579. reply_size);
  2580. #endif /* CONFIG_AP */
  2581. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  2582. wpas_notify_suspend(wpa_s->global);
  2583. } else if (os_strcmp(buf, "RESUME") == 0) {
  2584. wpas_notify_resume(wpa_s->global);
  2585. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  2586. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  2587. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  2588. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  2589. reply_len = -1;
  2590. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  2591. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  2592. reply_len = -1;
  2593. } else {
  2594. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2595. reply_len = 16;
  2596. }
  2597. if (reply_len < 0) {
  2598. os_memcpy(reply, "FAIL\n", 5);
  2599. reply_len = 5;
  2600. }
  2601. if (ctrl_rsp)
  2602. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  2603. *resp_len = reply_len;
  2604. return reply;
  2605. }
  2606. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  2607. char *cmd)
  2608. {
  2609. struct wpa_interface iface;
  2610. char *pos;
  2611. /*
  2612. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  2613. * TAB<bridge_ifname>
  2614. */
  2615. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  2616. os_memset(&iface, 0, sizeof(iface));
  2617. do {
  2618. iface.ifname = pos = cmd;
  2619. pos = os_strchr(pos, '\t');
  2620. if (pos)
  2621. *pos++ = '\0';
  2622. if (iface.ifname[0] == '\0')
  2623. return -1;
  2624. if (pos == NULL)
  2625. break;
  2626. iface.confname = pos;
  2627. pos = os_strchr(pos, '\t');
  2628. if (pos)
  2629. *pos++ = '\0';
  2630. if (iface.confname[0] == '\0')
  2631. iface.confname = NULL;
  2632. if (pos == NULL)
  2633. break;
  2634. iface.driver = pos;
  2635. pos = os_strchr(pos, '\t');
  2636. if (pos)
  2637. *pos++ = '\0';
  2638. if (iface.driver[0] == '\0')
  2639. iface.driver = NULL;
  2640. if (pos == NULL)
  2641. break;
  2642. iface.ctrl_interface = pos;
  2643. pos = os_strchr(pos, '\t');
  2644. if (pos)
  2645. *pos++ = '\0';
  2646. if (iface.ctrl_interface[0] == '\0')
  2647. iface.ctrl_interface = NULL;
  2648. if (pos == NULL)
  2649. break;
  2650. iface.driver_param = pos;
  2651. pos = os_strchr(pos, '\t');
  2652. if (pos)
  2653. *pos++ = '\0';
  2654. if (iface.driver_param[0] == '\0')
  2655. iface.driver_param = NULL;
  2656. if (pos == NULL)
  2657. break;
  2658. iface.bridge_ifname = pos;
  2659. pos = os_strchr(pos, '\t');
  2660. if (pos)
  2661. *pos++ = '\0';
  2662. if (iface.bridge_ifname[0] == '\0')
  2663. iface.bridge_ifname = NULL;
  2664. if (pos == NULL)
  2665. break;
  2666. } while (0);
  2667. if (wpa_supplicant_get_iface(global, iface.ifname))
  2668. return -1;
  2669. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  2670. }
  2671. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  2672. char *cmd)
  2673. {
  2674. struct wpa_supplicant *wpa_s;
  2675. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  2676. wpa_s = wpa_supplicant_get_iface(global, cmd);
  2677. if (wpa_s == NULL)
  2678. return -1;
  2679. return wpa_supplicant_remove_iface(global, wpa_s);
  2680. }
  2681. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  2682. {
  2683. struct wpa_interface_info *prev;
  2684. while (iface) {
  2685. prev = iface;
  2686. iface = iface->next;
  2687. os_free(prev->ifname);
  2688. os_free(prev->desc);
  2689. os_free(prev);
  2690. }
  2691. }
  2692. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  2693. char *buf, int len)
  2694. {
  2695. int i, res;
  2696. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  2697. char *pos, *end;
  2698. for (i = 0; wpa_drivers[i]; i++) {
  2699. struct wpa_driver_ops *drv = wpa_drivers[i];
  2700. if (drv->get_interfaces == NULL)
  2701. continue;
  2702. tmp = drv->get_interfaces(global->drv_priv[i]);
  2703. if (tmp == NULL)
  2704. continue;
  2705. if (last == NULL)
  2706. iface = last = tmp;
  2707. else
  2708. last->next = tmp;
  2709. while (last->next)
  2710. last = last->next;
  2711. }
  2712. pos = buf;
  2713. end = buf + len;
  2714. for (tmp = iface; tmp; tmp = tmp->next) {
  2715. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  2716. tmp->drv_name, tmp->ifname,
  2717. tmp->desc ? tmp->desc : "");
  2718. if (res < 0 || res >= end - pos) {
  2719. *pos = '\0';
  2720. break;
  2721. }
  2722. pos += res;
  2723. }
  2724. wpa_free_iface_info(iface);
  2725. return pos - buf;
  2726. }
  2727. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  2728. char *buf, int len)
  2729. {
  2730. int res;
  2731. char *pos, *end;
  2732. struct wpa_supplicant *wpa_s;
  2733. wpa_s = global->ifaces;
  2734. pos = buf;
  2735. end = buf + len;
  2736. while (wpa_s) {
  2737. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  2738. if (res < 0 || res >= end - pos) {
  2739. *pos = '\0';
  2740. break;
  2741. }
  2742. pos += res;
  2743. wpa_s = wpa_s->next;
  2744. }
  2745. return pos - buf;
  2746. }
  2747. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  2748. char *buf, size_t *resp_len)
  2749. {
  2750. char *reply;
  2751. const int reply_size = 2048;
  2752. int reply_len;
  2753. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  2754. (const u8 *) buf, os_strlen(buf));
  2755. reply = os_malloc(reply_size);
  2756. if (reply == NULL) {
  2757. *resp_len = 1;
  2758. return NULL;
  2759. }
  2760. os_memcpy(reply, "OK\n", 3);
  2761. reply_len = 3;
  2762. if (os_strcmp(buf, "PING") == 0) {
  2763. os_memcpy(reply, "PONG\n", 5);
  2764. reply_len = 5;
  2765. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  2766. if (wpa_supplicant_global_iface_add(global, buf + 14))
  2767. reply_len = -1;
  2768. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  2769. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  2770. reply_len = -1;
  2771. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  2772. reply_len = wpa_supplicant_global_iface_list(
  2773. global, reply, reply_size);
  2774. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  2775. reply_len = wpa_supplicant_global_iface_interfaces(
  2776. global, reply, reply_size);
  2777. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2778. wpa_supplicant_terminate_proc(global);
  2779. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  2780. wpas_notify_suspend(global);
  2781. } else if (os_strcmp(buf, "RESUME") == 0) {
  2782. wpas_notify_resume(global);
  2783. } else {
  2784. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2785. reply_len = 16;
  2786. }
  2787. if (reply_len < 0) {
  2788. os_memcpy(reply, "FAIL\n", 5);
  2789. reply_len = 5;
  2790. }
  2791. *resp_len = reply_len;
  2792. return reply;
  2793. }