ctrl_iface.c 80 KB

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