ctrl_iface.c 78 KB

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