ctrl_iface.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351
  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. u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
  1641. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  1642. /* MLME-DELETEKEYS.request */
  1643. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
  1644. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
  1645. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
  1646. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
  1647. #ifdef CONFIG_IEEE80211W
  1648. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
  1649. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
  1650. #endif /* CONFIG_IEEE80211W */
  1651. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  1652. 0);
  1653. /* MLME-SETPROTECTION.request(None) */
  1654. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  1655. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  1656. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1657. wpa_sm_drop_sa(wpa_s->wpa);
  1658. }
  1659. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  1660. char *addr)
  1661. {
  1662. u8 bssid[ETH_ALEN];
  1663. struct wpa_bss *bss;
  1664. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1665. if (hwaddr_aton(addr, bssid)) {
  1666. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  1667. "address '%s'", addr);
  1668. return -1;
  1669. }
  1670. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  1671. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1672. if (!bss) {
  1673. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  1674. "from BSS table");
  1675. return -1;
  1676. }
  1677. /*
  1678. * TODO: Find best network configuration block from configuration to
  1679. * allow roaming to other networks
  1680. */
  1681. if (!ssid) {
  1682. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  1683. "configuration known for the target AP");
  1684. return -1;
  1685. }
  1686. wpa_s->reassociate = 1;
  1687. wpa_supplicant_connect(wpa_s, bss, ssid);
  1688. return 0;
  1689. }
  1690. #ifdef CONFIG_P2P
  1691. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  1692. {
  1693. unsigned int timeout = atoi(cmd);
  1694. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  1695. if (os_strstr(cmd, "type=social"))
  1696. type = P2P_FIND_ONLY_SOCIAL;
  1697. else if (os_strstr(cmd, "type=progressive"))
  1698. type = P2P_FIND_PROGRESSIVE;
  1699. wpas_p2p_find(wpa_s, timeout, type);
  1700. return 0;
  1701. }
  1702. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  1703. char *buf, size_t buflen)
  1704. {
  1705. u8 addr[ETH_ALEN];
  1706. char *pos, *pos2;
  1707. char *pin = NULL;
  1708. enum p2p_wps_method wps_method;
  1709. int new_pin;
  1710. int ret;
  1711. int persistent_group;
  1712. int join;
  1713. int auth;
  1714. int go_intent = -1;
  1715. int freq = 0;
  1716. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad] [persistent]
  1717. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] */
  1718. if (hwaddr_aton(cmd, addr))
  1719. return -1;
  1720. pos = cmd + 17;
  1721. if (*pos != ' ')
  1722. return -1;
  1723. pos++;
  1724. persistent_group = os_strstr(pos, " persistent") != NULL;
  1725. join = os_strstr(pos, " join") != NULL;
  1726. auth = os_strstr(pos, " auth") != NULL;
  1727. pos2 = os_strstr(pos, " go_intent=");
  1728. if (pos2) {
  1729. pos2 += 11;
  1730. go_intent = atoi(pos2);
  1731. if (go_intent < 0 || go_intent > 15)
  1732. return -1;
  1733. }
  1734. pos2 = os_strstr(pos, " freq=");
  1735. if (pos2) {
  1736. pos2 += 6;
  1737. freq = atoi(pos2);
  1738. if (freq <= 0)
  1739. return -1;
  1740. }
  1741. if (os_strncmp(pos, "pin", 3) == 0) {
  1742. /* Request random PIN (to be displayed) and enable the PIN */
  1743. wps_method = WPS_PIN_DISPLAY;
  1744. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  1745. wps_method = WPS_PBC;
  1746. } else {
  1747. pin = pos;
  1748. pos = os_strchr(pin, ' ');
  1749. wps_method = WPS_PIN_KEYPAD;
  1750. if (pos) {
  1751. *pos++ = '\0';
  1752. if (os_strncmp(pos, "label", 5) == 0)
  1753. wps_method = WPS_PIN_LABEL;
  1754. else if (os_strncmp(pos, "display", 7) == 0)
  1755. wps_method = WPS_PIN_DISPLAY;
  1756. }
  1757. }
  1758. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  1759. persistent_group, join, auth, go_intent,
  1760. freq);
  1761. if (new_pin == -2) {
  1762. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  1763. return 25;
  1764. }
  1765. if (new_pin == -3) {
  1766. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  1767. return 25;
  1768. }
  1769. if (new_pin < 0)
  1770. return -1;
  1771. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  1772. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  1773. if (ret < 0 || (size_t) ret >= buflen)
  1774. return -1;
  1775. return ret;
  1776. }
  1777. os_memcpy(buf, "OK\n", 3);
  1778. return 3;
  1779. }
  1780. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  1781. {
  1782. unsigned int timeout = atoi(cmd);
  1783. return wpas_p2p_listen(wpa_s, timeout);
  1784. }
  1785. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  1786. {
  1787. u8 addr[ETH_ALEN];
  1788. char *pos;
  1789. /* <addr> <config method> */
  1790. if (hwaddr_aton(cmd, addr))
  1791. return -1;
  1792. pos = cmd + 17;
  1793. if (*pos != ' ')
  1794. return -1;
  1795. pos++;
  1796. return wpas_p2p_prov_disc(wpa_s, addr, pos);
  1797. }
  1798. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  1799. size_t buflen)
  1800. {
  1801. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1802. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  1803. ssid->passphrase == NULL)
  1804. return -1;
  1805. os_strlcpy(buf, ssid->passphrase, buflen);
  1806. return os_strlen(buf);
  1807. }
  1808. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  1809. char *buf, size_t buflen)
  1810. {
  1811. u64 ref;
  1812. int res;
  1813. u8 dst_buf[ETH_ALEN], *dst;
  1814. struct wpabuf *tlvs;
  1815. char *pos;
  1816. size_t len;
  1817. if (hwaddr_aton(cmd, dst_buf))
  1818. return -1;
  1819. dst = dst_buf;
  1820. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  1821. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  1822. dst = NULL;
  1823. pos = cmd + 17;
  1824. if (*pos != ' ')
  1825. return -1;
  1826. pos++;
  1827. if (os_strncmp(pos, "upnp ", 5) == 0) {
  1828. u8 version;
  1829. pos += 5;
  1830. if (hexstr2bin(pos, &version, 1) < 0)
  1831. return -1;
  1832. pos += 2;
  1833. if (*pos != ' ')
  1834. return -1;
  1835. pos++;
  1836. ref = (unsigned long) wpas_p2p_sd_request_upnp(wpa_s, dst,
  1837. version, pos);
  1838. } else {
  1839. len = os_strlen(pos);
  1840. if (len & 1)
  1841. return -1;
  1842. len /= 2;
  1843. tlvs = wpabuf_alloc(len);
  1844. if (tlvs == NULL)
  1845. return -1;
  1846. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  1847. wpabuf_free(tlvs);
  1848. return -1;
  1849. }
  1850. ref = (unsigned long) wpas_p2p_sd_request(wpa_s, dst, tlvs);
  1851. wpabuf_free(tlvs);
  1852. }
  1853. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  1854. if (res < 0 || (unsigned) res >= buflen)
  1855. return -1;
  1856. return res;
  1857. }
  1858. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  1859. char *cmd)
  1860. {
  1861. long long unsigned val;
  1862. u64 req;
  1863. if (sscanf(cmd, "%llx", &val) != 1)
  1864. return -1;
  1865. req = val;
  1866. return wpas_p2p_sd_cancel_request(wpa_s, (void *) (unsigned long) req);
  1867. }
  1868. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  1869. {
  1870. int freq;
  1871. u8 dst[ETH_ALEN];
  1872. u8 dialog_token;
  1873. struct wpabuf *resp_tlvs;
  1874. char *pos, *pos2;
  1875. size_t len;
  1876. pos = os_strchr(cmd, ' ');
  1877. if (pos == NULL)
  1878. return -1;
  1879. *pos++ = '\0';
  1880. freq = atoi(cmd);
  1881. if (freq == 0)
  1882. return -1;
  1883. if (hwaddr_aton(pos, dst))
  1884. return -1;
  1885. pos += 17;
  1886. if (*pos != ' ')
  1887. return -1;
  1888. pos++;
  1889. pos2 = os_strchr(pos, ' ');
  1890. if (pos2 == NULL)
  1891. return -1;
  1892. *pos2++ = '\0';
  1893. dialog_token = atoi(pos);
  1894. len = os_strlen(pos2);
  1895. if (len & 1)
  1896. return -1;
  1897. len /= 2;
  1898. resp_tlvs = wpabuf_alloc(len);
  1899. if (resp_tlvs == NULL)
  1900. return -1;
  1901. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  1902. wpabuf_free(resp_tlvs);
  1903. return -1;
  1904. }
  1905. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  1906. wpabuf_free(resp_tlvs);
  1907. return 0;
  1908. }
  1909. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  1910. char *cmd)
  1911. {
  1912. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  1913. return 0;
  1914. }
  1915. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  1916. char *cmd)
  1917. {
  1918. char *pos;
  1919. size_t len;
  1920. struct wpabuf *query, *resp;
  1921. pos = os_strchr(cmd, ' ');
  1922. if (pos == NULL)
  1923. return -1;
  1924. *pos++ = '\0';
  1925. len = os_strlen(cmd);
  1926. if (len & 1)
  1927. return -1;
  1928. len /= 2;
  1929. query = wpabuf_alloc(len);
  1930. if (query == NULL)
  1931. return -1;
  1932. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  1933. wpabuf_free(query);
  1934. return -1;
  1935. }
  1936. len = os_strlen(pos);
  1937. if (len & 1) {
  1938. wpabuf_free(query);
  1939. return -1;
  1940. }
  1941. len /= 2;
  1942. resp = wpabuf_alloc(len);
  1943. if (resp == NULL) {
  1944. wpabuf_free(query);
  1945. return -1;
  1946. }
  1947. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  1948. wpabuf_free(query);
  1949. wpabuf_free(resp);
  1950. return -1;
  1951. }
  1952. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  1953. wpabuf_free(query);
  1954. wpabuf_free(resp);
  1955. return -1;
  1956. }
  1957. return 0;
  1958. }
  1959. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  1960. {
  1961. char *pos;
  1962. u8 version;
  1963. pos = os_strchr(cmd, ' ');
  1964. if (pos == NULL)
  1965. return -1;
  1966. *pos++ = '\0';
  1967. if (hexstr2bin(cmd, &version, 1) < 0)
  1968. return -1;
  1969. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  1970. }
  1971. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  1972. {
  1973. char *pos;
  1974. pos = os_strchr(cmd, ' ');
  1975. if (pos == NULL)
  1976. return -1;
  1977. *pos++ = '\0';
  1978. if (os_strcmp(cmd, "bonjour") == 0)
  1979. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  1980. if (os_strcmp(cmd, "upnp") == 0)
  1981. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  1982. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  1983. return -1;
  1984. }
  1985. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  1986. char *cmd)
  1987. {
  1988. size_t len;
  1989. struct wpabuf *query;
  1990. int ret;
  1991. len = os_strlen(cmd);
  1992. if (len & 1)
  1993. return -1;
  1994. len /= 2;
  1995. query = wpabuf_alloc(len);
  1996. if (query == NULL)
  1997. return -1;
  1998. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  1999. wpabuf_free(query);
  2000. return -1;
  2001. }
  2002. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  2003. wpabuf_free(query);
  2004. return ret;
  2005. }
  2006. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  2007. {
  2008. char *pos;
  2009. u8 version;
  2010. pos = os_strchr(cmd, ' ');
  2011. if (pos == NULL)
  2012. return -1;
  2013. *pos++ = '\0';
  2014. if (hexstr2bin(cmd, &version, 1) < 0)
  2015. return -1;
  2016. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  2017. }
  2018. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  2019. {
  2020. char *pos;
  2021. pos = os_strchr(cmd, ' ');
  2022. if (pos == NULL)
  2023. return -1;
  2024. *pos++ = '\0';
  2025. if (os_strcmp(cmd, "bonjour") == 0)
  2026. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  2027. if (os_strcmp(cmd, "upnp") == 0)
  2028. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  2029. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  2030. return -1;
  2031. }
  2032. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  2033. {
  2034. u8 addr[ETH_ALEN];
  2035. /* <addr> */
  2036. if (hwaddr_aton(cmd, addr))
  2037. return -1;
  2038. return wpas_p2p_reject(wpa_s, addr);
  2039. }
  2040. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  2041. {
  2042. char *pos;
  2043. int id;
  2044. struct wpa_ssid *ssid;
  2045. u8 peer[ETH_ALEN];
  2046. id = atoi(cmd);
  2047. pos = os_strstr(cmd, " peer=");
  2048. if (pos) {
  2049. pos += 6;
  2050. if (hwaddr_aton(pos, peer))
  2051. return -1;
  2052. }
  2053. ssid = wpa_config_get_network(wpa_s->conf, id);
  2054. if (ssid == NULL || ssid->disabled != 2) {
  2055. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  2056. "for persistent P2P group",
  2057. id);
  2058. return -1;
  2059. }
  2060. return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
  2061. }
  2062. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  2063. {
  2064. char *pos;
  2065. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  2066. pos = os_strstr(cmd, " peer=");
  2067. if (!pos)
  2068. return -1;
  2069. *pos = '\0';
  2070. pos += 6;
  2071. if (hwaddr_aton(pos, peer)) {
  2072. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  2073. return -1;
  2074. }
  2075. pos = os_strstr(pos, " go_dev_addr=");
  2076. if (pos) {
  2077. pos += 13;
  2078. if (hwaddr_aton(pos, go_dev_addr)) {
  2079. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  2080. pos);
  2081. return -1;
  2082. }
  2083. go_dev = go_dev_addr;
  2084. }
  2085. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  2086. }
  2087. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  2088. {
  2089. if (os_strncmp(cmd, "persistent=", 11) == 0)
  2090. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  2091. if (os_strncmp(cmd, "group=", 6) == 0)
  2092. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  2093. return -1;
  2094. }
  2095. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  2096. char *cmd, int freq)
  2097. {
  2098. int id;
  2099. struct wpa_ssid *ssid;
  2100. id = atoi(cmd);
  2101. ssid = wpa_config_get_network(wpa_s->conf, id);
  2102. if (ssid == NULL || ssid->disabled != 2) {
  2103. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  2104. "for persistent P2P group",
  2105. id);
  2106. return -1;
  2107. }
  2108. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq);
  2109. }
  2110. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  2111. {
  2112. int freq = 0;
  2113. char *pos;
  2114. pos = os_strstr(cmd, "freq=");
  2115. if (pos)
  2116. freq = atoi(pos + 5);
  2117. if (os_strncmp(cmd, "persistent=", 11) == 0)
  2118. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq);
  2119. if (os_strcmp(cmd, "persistent") == 0 ||
  2120. os_strncmp(cmd, "persistent ", 11) == 0)
  2121. return wpas_p2p_group_add(wpa_s, 1, freq);
  2122. if (os_strncmp(cmd, "freq=", 5) == 0)
  2123. return wpas_p2p_group_add(wpa_s, 0, freq);
  2124. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  2125. cmd);
  2126. return -1;
  2127. }
  2128. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  2129. char *buf, size_t buflen)
  2130. {
  2131. u8 addr[ETH_ALEN], *addr_ptr;
  2132. int next;
  2133. if (!wpa_s->global->p2p)
  2134. return -1;
  2135. if (os_strcmp(cmd, "FIRST") == 0) {
  2136. addr_ptr = NULL;
  2137. next = 0;
  2138. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2139. if (hwaddr_aton(cmd + 5, addr) < 0)
  2140. return -1;
  2141. addr_ptr = addr;
  2142. next = 1;
  2143. } else {
  2144. if (hwaddr_aton(cmd, addr) < 0)
  2145. return -1;
  2146. addr_ptr = addr;
  2147. next = 0;
  2148. }
  2149. return p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next,
  2150. buf, buflen);
  2151. }
  2152. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  2153. {
  2154. char *param;
  2155. if (wpa_s->global->p2p == NULL)
  2156. return -1;
  2157. param = os_strchr(cmd, ' ');
  2158. if (param == NULL)
  2159. return -1;
  2160. *param++ = '\0';
  2161. if (os_strcmp(cmd, "discoverability") == 0) {
  2162. p2p_set_client_discoverability(wpa_s->global->p2p,
  2163. atoi(param));
  2164. return 0;
  2165. }
  2166. if (os_strcmp(cmd, "managed") == 0) {
  2167. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  2168. return 0;
  2169. }
  2170. if (os_strcmp(cmd, "listen_channel") == 0) {
  2171. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  2172. atoi(param));
  2173. }
  2174. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  2175. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  2176. os_strlen(param));
  2177. }
  2178. if (os_strcmp(cmd, "noa") == 0) {
  2179. char *pos;
  2180. int count, start, duration;
  2181. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  2182. count = atoi(param);
  2183. pos = os_strchr(param, ',');
  2184. if (pos == NULL)
  2185. return -1;
  2186. pos++;
  2187. start = atoi(pos);
  2188. pos = os_strchr(pos, ',');
  2189. if (pos == NULL)
  2190. return -1;
  2191. pos++;
  2192. duration = atoi(pos);
  2193. if (count < 0 || count > 255 || start < 0 || duration < 0)
  2194. return -1;
  2195. if (count == 0 && duration > 0)
  2196. return -1;
  2197. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  2198. "start=%d duration=%d", count, start, duration);
  2199. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  2200. }
  2201. if (os_strcmp(cmd, "ps") == 0)
  2202. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  2203. if (os_strcmp(cmd, "oppps") == 0)
  2204. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  2205. if (os_strcmp(cmd, "ctwindow") == 0)
  2206. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  2207. if (os_strcmp(cmd, "disabled") == 0) {
  2208. wpa_s->global->p2p_disabled = atoi(param);
  2209. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  2210. wpa_s->global->p2p_disabled ?
  2211. "disabled" : "enabled");
  2212. if (wpa_s->global->p2p_disabled) {
  2213. wpas_p2p_stop_find(wpa_s);
  2214. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2215. p2p_flush(wpa_s->global->p2p);
  2216. }
  2217. return 0;
  2218. }
  2219. if (os_strcmp(cmd, "force_long_sd") == 0) {
  2220. wpa_s->force_long_sd = atoi(param);
  2221. return 0;
  2222. }
  2223. if (os_strcmp(cmd, "peer_filter") == 0) {
  2224. u8 addr[ETH_ALEN];
  2225. if (hwaddr_aton(param, addr))
  2226. return -1;
  2227. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  2228. return 0;
  2229. }
  2230. if (os_strcmp(cmd, "cross_connect") == 0)
  2231. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  2232. if (os_strcmp(cmd, "go_apsd") == 0) {
  2233. if (os_strcmp(param, "disable") == 0)
  2234. wpa_s->set_ap_uapsd = 0;
  2235. else {
  2236. wpa_s->set_ap_uapsd = 1;
  2237. wpa_s->ap_uapsd = atoi(param);
  2238. }
  2239. return 0;
  2240. }
  2241. if (os_strcmp(cmd, "client_apsd") == 0) {
  2242. if (os_strcmp(param, "disable") == 0)
  2243. wpa_s->set_sta_uapsd = 0;
  2244. else {
  2245. int be, bk, vi, vo;
  2246. char *pos;
  2247. /* format: BE,BK,VI,VO;max SP Length */
  2248. be = atoi(param);
  2249. pos = os_strchr(param, ',');
  2250. if (pos == NULL)
  2251. return -1;
  2252. pos++;
  2253. bk = atoi(pos);
  2254. pos = os_strchr(pos, ',');
  2255. if (pos == NULL)
  2256. return -1;
  2257. pos++;
  2258. vi = atoi(pos);
  2259. pos = os_strchr(pos, ',');
  2260. if (pos == NULL)
  2261. return -1;
  2262. pos++;
  2263. vo = atoi(pos);
  2264. /* ignore max SP Length for now */
  2265. wpa_s->set_sta_uapsd = 1;
  2266. wpa_s->sta_uapsd = 0;
  2267. if (be)
  2268. wpa_s->sta_uapsd |= BIT(0);
  2269. if (bk)
  2270. wpa_s->sta_uapsd |= BIT(1);
  2271. if (vi)
  2272. wpa_s->sta_uapsd |= BIT(2);
  2273. if (vo)
  2274. wpa_s->sta_uapsd |= BIT(3);
  2275. }
  2276. return 0;
  2277. }
  2278. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  2279. cmd);
  2280. return -1;
  2281. }
  2282. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  2283. {
  2284. char *pos, *pos2;
  2285. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  2286. if (cmd[0]) {
  2287. pos = os_strchr(cmd, ' ');
  2288. if (pos == NULL)
  2289. return -1;
  2290. *pos++ = '\0';
  2291. dur1 = atoi(cmd);
  2292. pos2 = os_strchr(pos, ' ');
  2293. if (pos2)
  2294. *pos2++ = '\0';
  2295. int1 = atoi(pos);
  2296. } else
  2297. pos2 = NULL;
  2298. if (pos2) {
  2299. pos = os_strchr(pos2, ' ');
  2300. if (pos == NULL)
  2301. return -1;
  2302. *pos++ = '\0';
  2303. dur2 = atoi(pos2);
  2304. int2 = atoi(pos);
  2305. }
  2306. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  2307. }
  2308. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  2309. {
  2310. char *pos;
  2311. unsigned int period = 0, interval = 0;
  2312. if (cmd[0]) {
  2313. pos = os_strchr(cmd, ' ');
  2314. if (pos == NULL)
  2315. return -1;
  2316. *pos++ = '\0';
  2317. period = atoi(cmd);
  2318. interval = atoi(pos);
  2319. }
  2320. return wpas_p2p_ext_listen(wpa_s, period, interval);
  2321. }
  2322. #endif /* CONFIG_P2P */
  2323. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  2324. struct wpa_supplicant *wpa_s, char *cmd)
  2325. {
  2326. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  2327. return 0;
  2328. }
  2329. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  2330. char *buf, size_t *resp_len)
  2331. {
  2332. char *reply;
  2333. const int reply_size = 4096;
  2334. int ctrl_rsp = 0;
  2335. int reply_len;
  2336. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  2337. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  2338. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  2339. (const u8 *) buf, os_strlen(buf));
  2340. } else {
  2341. int level = MSG_DEBUG;
  2342. if (os_strcmp(buf, "PING") == 0)
  2343. level = MSG_EXCESSIVE;
  2344. wpa_hexdump_ascii(level, "RX ctrl_iface",
  2345. (const u8 *) buf, os_strlen(buf));
  2346. }
  2347. reply = os_malloc(reply_size);
  2348. if (reply == NULL) {
  2349. *resp_len = 1;
  2350. return NULL;
  2351. }
  2352. os_memcpy(reply, "OK\n", 3);
  2353. reply_len = 3;
  2354. if (os_strcmp(buf, "PING") == 0) {
  2355. os_memcpy(reply, "PONG\n", 5);
  2356. reply_len = 5;
  2357. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  2358. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  2359. } else if (os_strcmp(buf, "MIB") == 0) {
  2360. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  2361. if (reply_len >= 0) {
  2362. int res;
  2363. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  2364. reply_size - reply_len);
  2365. if (res < 0)
  2366. reply_len = -1;
  2367. else
  2368. reply_len += res;
  2369. }
  2370. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  2371. reply_len = wpa_supplicant_ctrl_iface_status(
  2372. wpa_s, buf + 6, reply, reply_size);
  2373. } else if (os_strcmp(buf, "PMKSA") == 0) {
  2374. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  2375. reply_size);
  2376. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  2377. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  2378. reply_len = -1;
  2379. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  2380. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  2381. reply, reply_size);
  2382. } else if (os_strcmp(buf, "LOGON") == 0) {
  2383. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  2384. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  2385. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  2386. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  2387. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2388. reply_len = -1;
  2389. else {
  2390. wpa_s->disconnected = 0;
  2391. wpa_s->reassociate = 1;
  2392. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2393. }
  2394. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  2395. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2396. reply_len = -1;
  2397. else if (wpa_s->disconnected) {
  2398. wpa_s->disconnected = 0;
  2399. wpa_s->reassociate = 1;
  2400. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2401. }
  2402. #ifdef IEEE8021X_EAPOL
  2403. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  2404. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  2405. reply_len = -1;
  2406. #endif /* IEEE8021X_EAPOL */
  2407. #ifdef CONFIG_PEERKEY
  2408. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  2409. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  2410. reply_len = -1;
  2411. #endif /* CONFIG_PEERKEY */
  2412. #ifdef CONFIG_IEEE80211R
  2413. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  2414. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  2415. reply_len = -1;
  2416. #endif /* CONFIG_IEEE80211R */
  2417. #ifdef CONFIG_WPS
  2418. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  2419. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
  2420. reply_len = -1;
  2421. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  2422. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
  2423. reply_len = -1;
  2424. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  2425. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  2426. reply,
  2427. reply_size);
  2428. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  2429. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  2430. wpa_s, buf + 14, reply, reply_size);
  2431. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  2432. if (wpas_wps_cancel(wpa_s))
  2433. reply_len = -1;
  2434. #ifdef CONFIG_WPS_OOB
  2435. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  2436. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  2437. reply_len = -1;
  2438. #endif /* CONFIG_WPS_OOB */
  2439. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  2440. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  2441. reply_len = -1;
  2442. #ifdef CONFIG_AP
  2443. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  2444. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  2445. wpa_s, buf + 11, reply, reply_size);
  2446. #endif /* CONFIG_AP */
  2447. #ifdef CONFIG_WPS_ER
  2448. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  2449. if (wpas_wps_er_start(wpa_s, NULL))
  2450. reply_len = -1;
  2451. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  2452. if (wpas_wps_er_start(wpa_s, buf + 13))
  2453. reply_len = -1;
  2454. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  2455. if (wpas_wps_er_stop(wpa_s))
  2456. reply_len = -1;
  2457. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  2458. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  2459. reply_len = -1;
  2460. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  2461. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  2462. if (ret == -2) {
  2463. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  2464. reply_len = 17;
  2465. } else if (ret == -3) {
  2466. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  2467. reply_len = 18;
  2468. } else if (ret == -4) {
  2469. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  2470. reply_len = 20;
  2471. } else if (ret)
  2472. reply_len = -1;
  2473. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  2474. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  2475. reply_len = -1;
  2476. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  2477. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  2478. buf + 18))
  2479. reply_len = -1;
  2480. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  2481. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  2482. reply_len = -1;
  2483. #endif /* CONFIG_WPS_ER */
  2484. #endif /* CONFIG_WPS */
  2485. #ifdef CONFIG_IBSS_RSN
  2486. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  2487. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  2488. reply_len = -1;
  2489. #endif /* CONFIG_IBSS_RSN */
  2490. #ifdef CONFIG_P2P
  2491. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  2492. if (p2p_ctrl_find(wpa_s, buf + 9))
  2493. reply_len = -1;
  2494. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  2495. if (p2p_ctrl_find(wpa_s, ""))
  2496. reply_len = -1;
  2497. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  2498. wpas_p2p_stop_find(wpa_s);
  2499. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  2500. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  2501. reply_size);
  2502. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  2503. if (p2p_ctrl_listen(wpa_s, buf + 11))
  2504. reply_len = -1;
  2505. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  2506. if (p2p_ctrl_listen(wpa_s, ""))
  2507. reply_len = -1;
  2508. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  2509. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  2510. reply_len = -1;
  2511. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  2512. if (wpas_p2p_group_add(wpa_s, 0, 0))
  2513. reply_len = -1;
  2514. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  2515. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  2516. reply_len = -1;
  2517. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  2518. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  2519. reply_len = -1;
  2520. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  2521. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  2522. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  2523. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  2524. reply_size);
  2525. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  2526. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  2527. reply_len = -1;
  2528. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  2529. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  2530. reply_len = -1;
  2531. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  2532. wpas_p2p_sd_service_update(wpa_s);
  2533. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  2534. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  2535. reply_len = -1;
  2536. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  2537. wpas_p2p_service_flush(wpa_s);
  2538. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  2539. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  2540. reply_len = -1;
  2541. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  2542. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  2543. reply_len = -1;
  2544. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  2545. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  2546. reply_len = -1;
  2547. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  2548. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  2549. reply_len = -1;
  2550. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  2551. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  2552. reply_size);
  2553. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  2554. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  2555. reply_len = -1;
  2556. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  2557. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  2558. wpa_s->force_long_sd = 0;
  2559. p2p_flush(wpa_s->global->p2p);
  2560. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  2561. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  2562. reply_len = -1;
  2563. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  2564. if (wpas_p2p_cancel(wpa_s))
  2565. reply_len = -1;
  2566. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  2567. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  2568. reply_len = -1;
  2569. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  2570. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  2571. reply_len = -1;
  2572. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  2573. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  2574. reply_len = -1;
  2575. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  2576. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  2577. reply_len = -1;
  2578. #endif /* CONFIG_P2P */
  2579. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  2580. {
  2581. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  2582. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  2583. reply_len = -1;
  2584. else
  2585. ctrl_rsp = 1;
  2586. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  2587. if (wpa_supplicant_reload_configuration(wpa_s))
  2588. reply_len = -1;
  2589. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2590. wpa_supplicant_terminate_proc(wpa_s->global);
  2591. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  2592. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  2593. reply_len = -1;
  2594. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  2595. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  2596. wpa_s, reply, reply_size);
  2597. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  2598. wpa_s->reassociate = 0;
  2599. wpa_s->disconnected = 1;
  2600. wpa_supplicant_deauthenticate(wpa_s,
  2601. WLAN_REASON_DEAUTH_LEAVING);
  2602. } else if (os_strcmp(buf, "SCAN") == 0) {
  2603. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  2604. reply_len = -1;
  2605. else {
  2606. wpa_s->scan_req = 2;
  2607. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2608. }
  2609. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  2610. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  2611. wpa_s, reply, reply_size);
  2612. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  2613. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  2614. reply_len = -1;
  2615. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  2616. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  2617. reply_len = -1;
  2618. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  2619. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  2620. reply_len = -1;
  2621. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  2622. reply_len = wpa_supplicant_ctrl_iface_add_network(
  2623. wpa_s, reply, reply_size);
  2624. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  2625. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  2626. reply_len = -1;
  2627. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  2628. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  2629. reply_len = -1;
  2630. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  2631. reply_len = wpa_supplicant_ctrl_iface_get_network(
  2632. wpa_s, buf + 12, reply, reply_size);
  2633. #ifndef CONFIG_NO_CONFIG_WRITE
  2634. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  2635. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  2636. reply_len = -1;
  2637. #endif /* CONFIG_NO_CONFIG_WRITE */
  2638. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  2639. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  2640. wpa_s, buf + 15, reply, reply_size);
  2641. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  2642. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  2643. reply_len = -1;
  2644. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  2645. reply_len = wpa_supplicant_global_iface_list(
  2646. wpa_s->global, reply, reply_size);
  2647. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  2648. reply_len = wpa_supplicant_global_iface_interfaces(
  2649. wpa_s->global, reply, reply_size);
  2650. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  2651. reply_len = wpa_supplicant_ctrl_iface_bss(
  2652. wpa_s, buf + 4, reply, reply_size);
  2653. #ifdef CONFIG_AP
  2654. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  2655. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  2656. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  2657. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  2658. reply_size);
  2659. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  2660. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  2661. reply_size);
  2662. #endif /* CONFIG_AP */
  2663. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  2664. wpas_notify_suspend(wpa_s->global);
  2665. } else if (os_strcmp(buf, "RESUME") == 0) {
  2666. wpas_notify_resume(wpa_s->global);
  2667. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  2668. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  2669. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  2670. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  2671. reply_len = -1;
  2672. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  2673. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  2674. reply_len = -1;
  2675. } else {
  2676. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2677. reply_len = 16;
  2678. }
  2679. if (reply_len < 0) {
  2680. os_memcpy(reply, "FAIL\n", 5);
  2681. reply_len = 5;
  2682. }
  2683. if (ctrl_rsp)
  2684. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  2685. *resp_len = reply_len;
  2686. return reply;
  2687. }
  2688. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  2689. char *cmd)
  2690. {
  2691. struct wpa_interface iface;
  2692. char *pos;
  2693. /*
  2694. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  2695. * TAB<bridge_ifname>
  2696. */
  2697. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  2698. os_memset(&iface, 0, sizeof(iface));
  2699. do {
  2700. iface.ifname = pos = cmd;
  2701. pos = os_strchr(pos, '\t');
  2702. if (pos)
  2703. *pos++ = '\0';
  2704. if (iface.ifname[0] == '\0')
  2705. return -1;
  2706. if (pos == NULL)
  2707. break;
  2708. iface.confname = pos;
  2709. pos = os_strchr(pos, '\t');
  2710. if (pos)
  2711. *pos++ = '\0';
  2712. if (iface.confname[0] == '\0')
  2713. iface.confname = NULL;
  2714. if (pos == NULL)
  2715. break;
  2716. iface.driver = pos;
  2717. pos = os_strchr(pos, '\t');
  2718. if (pos)
  2719. *pos++ = '\0';
  2720. if (iface.driver[0] == '\0')
  2721. iface.driver = NULL;
  2722. if (pos == NULL)
  2723. break;
  2724. iface.ctrl_interface = pos;
  2725. pos = os_strchr(pos, '\t');
  2726. if (pos)
  2727. *pos++ = '\0';
  2728. if (iface.ctrl_interface[0] == '\0')
  2729. iface.ctrl_interface = NULL;
  2730. if (pos == NULL)
  2731. break;
  2732. iface.driver_param = pos;
  2733. pos = os_strchr(pos, '\t');
  2734. if (pos)
  2735. *pos++ = '\0';
  2736. if (iface.driver_param[0] == '\0')
  2737. iface.driver_param = NULL;
  2738. if (pos == NULL)
  2739. break;
  2740. iface.bridge_ifname = pos;
  2741. pos = os_strchr(pos, '\t');
  2742. if (pos)
  2743. *pos++ = '\0';
  2744. if (iface.bridge_ifname[0] == '\0')
  2745. iface.bridge_ifname = NULL;
  2746. if (pos == NULL)
  2747. break;
  2748. } while (0);
  2749. if (wpa_supplicant_get_iface(global, iface.ifname))
  2750. return -1;
  2751. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  2752. }
  2753. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  2754. char *cmd)
  2755. {
  2756. struct wpa_supplicant *wpa_s;
  2757. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  2758. wpa_s = wpa_supplicant_get_iface(global, cmd);
  2759. if (wpa_s == NULL)
  2760. return -1;
  2761. return wpa_supplicant_remove_iface(global, wpa_s);
  2762. }
  2763. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  2764. {
  2765. struct wpa_interface_info *prev;
  2766. while (iface) {
  2767. prev = iface;
  2768. iface = iface->next;
  2769. os_free(prev->ifname);
  2770. os_free(prev->desc);
  2771. os_free(prev);
  2772. }
  2773. }
  2774. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  2775. char *buf, int len)
  2776. {
  2777. int i, res;
  2778. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  2779. char *pos, *end;
  2780. for (i = 0; wpa_drivers[i]; i++) {
  2781. struct wpa_driver_ops *drv = wpa_drivers[i];
  2782. if (drv->get_interfaces == NULL)
  2783. continue;
  2784. tmp = drv->get_interfaces(global->drv_priv[i]);
  2785. if (tmp == NULL)
  2786. continue;
  2787. if (last == NULL)
  2788. iface = last = tmp;
  2789. else
  2790. last->next = tmp;
  2791. while (last->next)
  2792. last = last->next;
  2793. }
  2794. pos = buf;
  2795. end = buf + len;
  2796. for (tmp = iface; tmp; tmp = tmp->next) {
  2797. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  2798. tmp->drv_name, tmp->ifname,
  2799. tmp->desc ? tmp->desc : "");
  2800. if (res < 0 || res >= end - pos) {
  2801. *pos = '\0';
  2802. break;
  2803. }
  2804. pos += res;
  2805. }
  2806. wpa_free_iface_info(iface);
  2807. return pos - buf;
  2808. }
  2809. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  2810. char *buf, int len)
  2811. {
  2812. int res;
  2813. char *pos, *end;
  2814. struct wpa_supplicant *wpa_s;
  2815. wpa_s = global->ifaces;
  2816. pos = buf;
  2817. end = buf + len;
  2818. while (wpa_s) {
  2819. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  2820. if (res < 0 || res >= end - pos) {
  2821. *pos = '\0';
  2822. break;
  2823. }
  2824. pos += res;
  2825. wpa_s = wpa_s->next;
  2826. }
  2827. return pos - buf;
  2828. }
  2829. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  2830. char *buf, size_t *resp_len)
  2831. {
  2832. char *reply;
  2833. const int reply_size = 2048;
  2834. int reply_len;
  2835. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  2836. (const u8 *) buf, os_strlen(buf));
  2837. reply = os_malloc(reply_size);
  2838. if (reply == NULL) {
  2839. *resp_len = 1;
  2840. return NULL;
  2841. }
  2842. os_memcpy(reply, "OK\n", 3);
  2843. reply_len = 3;
  2844. if (os_strcmp(buf, "PING") == 0) {
  2845. os_memcpy(reply, "PONG\n", 5);
  2846. reply_len = 5;
  2847. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  2848. if (wpa_supplicant_global_iface_add(global, buf + 14))
  2849. reply_len = -1;
  2850. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  2851. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  2852. reply_len = -1;
  2853. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  2854. reply_len = wpa_supplicant_global_iface_list(
  2855. global, reply, reply_size);
  2856. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  2857. reply_len = wpa_supplicant_global_iface_interfaces(
  2858. global, reply, reply_size);
  2859. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  2860. wpa_supplicant_terminate_proc(global);
  2861. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  2862. wpas_notify_suspend(global);
  2863. } else if (os_strcmp(buf, "RESUME") == 0) {
  2864. wpas_notify_resume(global);
  2865. } else {
  2866. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  2867. reply_len = 16;
  2868. }
  2869. if (reply_len < 0) {
  2870. os_memcpy(reply, "FAIL\n", 5);
  2871. reply_len = 5;
  2872. }
  2873. *resp_len = reply_len;
  2874. return reply;
  2875. }