Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ctrl_iface.c 86KB

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