ctrl_iface.c 72 KB

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