ctrl_iface.c 73 KB

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