ctrl_iface.c 84 KB

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