ctrl_iface.c 83 KB

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