ctrl_iface.c 73 KB

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