ctrl_iface.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251
  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 "notify.h"
  33. #include "bss.h"
  34. #include "scan.h"
  35. #include "ctrl_iface.h"
  36. extern struct wpa_driver_ops *wpa_drivers[];
  37. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  38. char *buf, int len);
  39. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  40. char *buf, int len);
  41. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  42. char *cmd)
  43. {
  44. char *value;
  45. int ret = 0;
  46. value = os_strchr(cmd, ' ');
  47. if (value == NULL)
  48. return -1;
  49. *value++ = '\0';
  50. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  51. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  52. eapol_sm_configure(wpa_s->eapol,
  53. atoi(value), -1, -1, -1);
  54. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  55. eapol_sm_configure(wpa_s->eapol,
  56. -1, atoi(value), -1, -1);
  57. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  58. eapol_sm_configure(wpa_s->eapol,
  59. -1, -1, atoi(value), -1);
  60. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  61. eapol_sm_configure(wpa_s->eapol,
  62. -1, -1, -1, atoi(value));
  63. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  64. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  65. atoi(value)))
  66. ret = -1;
  67. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  68. 0) {
  69. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  70. atoi(value)))
  71. ret = -1;
  72. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  73. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  74. ret = -1;
  75. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  76. wpa_s->wps_fragment_size = atoi(value);
  77. } else {
  78. value[-1] = '=';
  79. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  80. if (ret == 0)
  81. wpa_supplicant_update_config(wpa_s);
  82. }
  83. return ret;
  84. }
  85. #ifdef IEEE8021X_EAPOL
  86. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  87. char *addr)
  88. {
  89. u8 bssid[ETH_ALEN];
  90. struct wpa_ssid *ssid = wpa_s->current_ssid;
  91. if (hwaddr_aton(addr, bssid)) {
  92. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  93. "'%s'", addr);
  94. return -1;
  95. }
  96. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  97. rsn_preauth_deinit(wpa_s->wpa);
  98. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  99. return -1;
  100. return 0;
  101. }
  102. #endif /* IEEE8021X_EAPOL */
  103. #ifdef CONFIG_PEERKEY
  104. /* MLME-STKSTART.request(peer) */
  105. static int wpa_supplicant_ctrl_iface_stkstart(
  106. struct wpa_supplicant *wpa_s, char *addr)
  107. {
  108. u8 peer[ETH_ALEN];
  109. if (hwaddr_aton(addr, peer)) {
  110. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  111. "address '%s'", addr);
  112. return -1;
  113. }
  114. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  115. MAC2STR(peer));
  116. return wpa_sm_stkstart(wpa_s->wpa, peer);
  117. }
  118. #endif /* CONFIG_PEERKEY */
  119. #ifdef CONFIG_IEEE80211R
  120. static int wpa_supplicant_ctrl_iface_ft_ds(
  121. struct wpa_supplicant *wpa_s, char *addr)
  122. {
  123. u8 target_ap[ETH_ALEN];
  124. struct wpa_bss *bss;
  125. const u8 *mdie;
  126. if (hwaddr_aton(addr, target_ap)) {
  127. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  128. "address '%s'", addr);
  129. return -1;
  130. }
  131. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  132. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  133. if (bss)
  134. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  135. else
  136. mdie = NULL;
  137. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  138. }
  139. #endif /* CONFIG_IEEE80211R */
  140. #ifdef CONFIG_WPS
  141. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  142. char *cmd)
  143. {
  144. u8 bssid[ETH_ALEN], *_bssid = bssid;
  145. if (cmd == NULL || os_strcmp(cmd, "any") == 0)
  146. _bssid = NULL;
  147. else if (hwaddr_aton(cmd, bssid)) {
  148. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  149. cmd);
  150. return -1;
  151. }
  152. #ifdef CONFIG_AP
  153. if (wpa_s->ap_iface)
  154. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid);
  155. #endif /* CONFIG_AP */
  156. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  157. }
  158. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  159. char *cmd, char *buf,
  160. size_t buflen)
  161. {
  162. u8 bssid[ETH_ALEN], *_bssid = bssid;
  163. char *pin;
  164. int ret;
  165. pin = os_strchr(cmd, ' ');
  166. if (pin)
  167. *pin++ = '\0';
  168. if (os_strcmp(cmd, "any") == 0)
  169. _bssid = NULL;
  170. else if (hwaddr_aton(cmd, bssid)) {
  171. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  172. cmd);
  173. return -1;
  174. }
  175. #ifdef CONFIG_AP
  176. if (wpa_s->ap_iface)
  177. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  178. buf, buflen);
  179. #endif /* CONFIG_AP */
  180. if (pin) {
  181. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0);
  182. if (ret < 0)
  183. return -1;
  184. ret = os_snprintf(buf, buflen, "%s", pin);
  185. if (ret < 0 || (size_t) ret >= buflen)
  186. return -1;
  187. return ret;
  188. }
  189. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0);
  190. if (ret < 0)
  191. return -1;
  192. /* Return the generated PIN */
  193. ret = os_snprintf(buf, buflen, "%08d", ret);
  194. if (ret < 0 || (size_t) ret >= buflen)
  195. return -1;
  196. return ret;
  197. }
  198. #ifdef CONFIG_WPS_OOB
  199. static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
  200. char *cmd)
  201. {
  202. char *path, *method, *name;
  203. path = os_strchr(cmd, ' ');
  204. if (path == NULL)
  205. return -1;
  206. *path++ = '\0';
  207. method = os_strchr(path, ' ');
  208. if (method == NULL)
  209. return -1;
  210. *method++ = '\0';
  211. name = os_strchr(method, ' ');
  212. if (name != NULL)
  213. *name++ = '\0';
  214. return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
  215. }
  216. #endif /* CONFIG_WPS_OOB */
  217. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  218. char *cmd)
  219. {
  220. u8 bssid[ETH_ALEN], *_bssid = bssid;
  221. char *pin;
  222. char *new_ssid;
  223. char *new_auth;
  224. char *new_encr;
  225. char *new_key;
  226. struct wps_new_ap_settings ap;
  227. pin = os_strchr(cmd, ' ');
  228. if (pin == NULL)
  229. return -1;
  230. *pin++ = '\0';
  231. if (os_strcmp(cmd, "any") == 0)
  232. _bssid = NULL;
  233. else if (hwaddr_aton(cmd, bssid)) {
  234. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  235. cmd);
  236. return -1;
  237. }
  238. new_ssid = os_strchr(pin, ' ');
  239. if (new_ssid == NULL)
  240. return wpas_wps_start_reg(wpa_s, _bssid, pin, NULL);
  241. *new_ssid++ = '\0';
  242. new_auth = os_strchr(new_ssid, ' ');
  243. if (new_auth == NULL)
  244. return -1;
  245. *new_auth++ = '\0';
  246. new_encr = os_strchr(new_auth, ' ');
  247. if (new_encr == NULL)
  248. return -1;
  249. *new_encr++ = '\0';
  250. new_key = os_strchr(new_encr, ' ');
  251. if (new_key == NULL)
  252. return -1;
  253. *new_key++ = '\0';
  254. os_memset(&ap, 0, sizeof(ap));
  255. ap.ssid_hex = new_ssid;
  256. ap.auth = new_auth;
  257. ap.encr = new_encr;
  258. ap.key_hex = new_key;
  259. return wpas_wps_start_reg(wpa_s, _bssid, pin, &ap);
  260. }
  261. #ifdef CONFIG_WPS_ER
  262. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  263. char *cmd)
  264. {
  265. char *uuid = cmd, *pin, *pos;
  266. u8 addr_buf[ETH_ALEN], *addr = NULL;
  267. pin = os_strchr(uuid, ' ');
  268. if (pin == NULL)
  269. return -1;
  270. *pin++ = '\0';
  271. pos = os_strchr(pin, ' ');
  272. if (pos) {
  273. *pos++ = '\0';
  274. if (hwaddr_aton(pos, addr_buf) == 0)
  275. addr = addr_buf;
  276. }
  277. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  278. }
  279. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  280. char *cmd)
  281. {
  282. char *uuid = cmd, *pin;
  283. pin = os_strchr(uuid, ' ');
  284. if (pin == NULL)
  285. return -1;
  286. *pin++ = '\0';
  287. return wpas_wps_er_learn(wpa_s, uuid, pin);
  288. }
  289. static int wpa_supplicant_ctrl_iface_wps_er_config(
  290. struct wpa_supplicant *wpa_s, char *cmd)
  291. {
  292. char *pin;
  293. char *new_ssid;
  294. char *new_auth;
  295. char *new_encr;
  296. char *new_key;
  297. struct wps_new_ap_settings ap;
  298. pin = os_strchr(cmd, ' ');
  299. if (pin == NULL)
  300. return -1;
  301. *pin++ = '\0';
  302. new_ssid = os_strchr(pin, ' ');
  303. if (new_ssid == NULL)
  304. return -1;
  305. *new_ssid++ = '\0';
  306. new_auth = os_strchr(new_ssid, ' ');
  307. if (new_auth == NULL)
  308. return -1;
  309. *new_auth++ = '\0';
  310. new_encr = os_strchr(new_auth, ' ');
  311. if (new_encr == NULL)
  312. return -1;
  313. *new_encr++ = '\0';
  314. new_key = os_strchr(new_encr, ' ');
  315. if (new_key == NULL)
  316. return -1;
  317. *new_key++ = '\0';
  318. os_memset(&ap, 0, sizeof(ap));
  319. ap.ssid_hex = new_ssid;
  320. ap.auth = new_auth;
  321. ap.encr = new_encr;
  322. ap.key_hex = new_key;
  323. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  324. }
  325. #endif /* CONFIG_WPS_ER */
  326. #endif /* CONFIG_WPS */
  327. #ifdef CONFIG_IBSS_RSN
  328. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  329. struct wpa_supplicant *wpa_s, char *addr)
  330. {
  331. u8 peer[ETH_ALEN];
  332. if (hwaddr_aton(addr, peer)) {
  333. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  334. "address '%s'", addr);
  335. return -1;
  336. }
  337. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  338. MAC2STR(peer));
  339. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  340. }
  341. #endif /* CONFIG_IBSS_RSN */
  342. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  343. char *rsp)
  344. {
  345. #ifdef IEEE8021X_EAPOL
  346. char *pos, *id_pos;
  347. int id;
  348. struct wpa_ssid *ssid;
  349. struct eap_peer_config *eap;
  350. pos = os_strchr(rsp, '-');
  351. if (pos == NULL)
  352. return -1;
  353. *pos++ = '\0';
  354. id_pos = pos;
  355. pos = os_strchr(pos, ':');
  356. if (pos == NULL)
  357. return -1;
  358. *pos++ = '\0';
  359. id = atoi(id_pos);
  360. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  361. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  362. (u8 *) pos, os_strlen(pos));
  363. ssid = wpa_config_get_network(wpa_s->conf, id);
  364. if (ssid == NULL) {
  365. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  366. "to update", id);
  367. return -1;
  368. }
  369. eap = &ssid->eap;
  370. if (os_strcmp(rsp, "IDENTITY") == 0) {
  371. os_free(eap->identity);
  372. eap->identity = (u8 *) os_strdup(pos);
  373. eap->identity_len = os_strlen(pos);
  374. eap->pending_req_identity = 0;
  375. if (ssid == wpa_s->current_ssid)
  376. wpa_s->reassociate = 1;
  377. } else if (os_strcmp(rsp, "PASSWORD") == 0) {
  378. os_free(eap->password);
  379. eap->password = (u8 *) os_strdup(pos);
  380. eap->password_len = os_strlen(pos);
  381. eap->pending_req_password = 0;
  382. if (ssid == wpa_s->current_ssid)
  383. wpa_s->reassociate = 1;
  384. } else if (os_strcmp(rsp, "NEW_PASSWORD") == 0) {
  385. os_free(eap->new_password);
  386. eap->new_password = (u8 *) os_strdup(pos);
  387. eap->new_password_len = os_strlen(pos);
  388. eap->pending_req_new_password = 0;
  389. if (ssid == wpa_s->current_ssid)
  390. wpa_s->reassociate = 1;
  391. } else if (os_strcmp(rsp, "PIN") == 0) {
  392. os_free(eap->pin);
  393. eap->pin = os_strdup(pos);
  394. eap->pending_req_pin = 0;
  395. if (ssid == wpa_s->current_ssid)
  396. wpa_s->reassociate = 1;
  397. } else if (os_strcmp(rsp, "OTP") == 0) {
  398. os_free(eap->otp);
  399. eap->otp = (u8 *) os_strdup(pos);
  400. eap->otp_len = os_strlen(pos);
  401. os_free(eap->pending_req_otp);
  402. eap->pending_req_otp = NULL;
  403. eap->pending_req_otp_len = 0;
  404. } else if (os_strcmp(rsp, "PASSPHRASE") == 0) {
  405. os_free(eap->private_key_passwd);
  406. eap->private_key_passwd = (u8 *) os_strdup(pos);
  407. eap->pending_req_passphrase = 0;
  408. if (ssid == wpa_s->current_ssid)
  409. wpa_s->reassociate = 1;
  410. } else {
  411. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", rsp);
  412. return -1;
  413. }
  414. return 0;
  415. #else /* IEEE8021X_EAPOL */
  416. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  417. return -1;
  418. #endif /* IEEE8021X_EAPOL */
  419. }
  420. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  421. const char *params,
  422. char *buf, size_t buflen)
  423. {
  424. char *pos, *end, tmp[30];
  425. int res, verbose, ret;
  426. verbose = os_strcmp(params, "-VERBOSE") == 0;
  427. pos = buf;
  428. end = buf + buflen;
  429. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  430. struct wpa_ssid *ssid = wpa_s->current_ssid;
  431. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  432. MAC2STR(wpa_s->bssid));
  433. if (ret < 0 || ret >= end - pos)
  434. return pos - buf;
  435. pos += ret;
  436. if (ssid) {
  437. u8 *_ssid = ssid->ssid;
  438. size_t ssid_len = ssid->ssid_len;
  439. u8 ssid_buf[MAX_SSID_LEN];
  440. if (ssid_len == 0) {
  441. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  442. if (_res < 0)
  443. ssid_len = 0;
  444. else
  445. ssid_len = _res;
  446. _ssid = ssid_buf;
  447. }
  448. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  449. wpa_ssid_txt(_ssid, ssid_len),
  450. ssid->id);
  451. if (ret < 0 || ret >= end - pos)
  452. return pos - buf;
  453. pos += ret;
  454. if (ssid->id_str) {
  455. ret = os_snprintf(pos, end - pos,
  456. "id_str=%s\n",
  457. ssid->id_str);
  458. if (ret < 0 || ret >= end - pos)
  459. return pos - buf;
  460. pos += ret;
  461. }
  462. switch (ssid->mode) {
  463. case WPAS_MODE_INFRA:
  464. ret = os_snprintf(pos, end - pos,
  465. "mode=station\n");
  466. break;
  467. case WPAS_MODE_IBSS:
  468. ret = os_snprintf(pos, end - pos,
  469. "mode=IBSS\n");
  470. break;
  471. case WPAS_MODE_AP:
  472. ret = os_snprintf(pos, end - pos,
  473. "mode=AP\n");
  474. break;
  475. case WPAS_MODE_P2P_GO:
  476. ret = os_snprintf(pos, end - pos,
  477. "mode=P2P GO\n");
  478. break;
  479. case WPAS_MODE_P2P_GROUP_FORMATION:
  480. ret = os_snprintf(pos, end - pos,
  481. "mode=P2P GO - group "
  482. "formation\n");
  483. break;
  484. default:
  485. ret = 0;
  486. break;
  487. }
  488. if (ret < 0 || ret >= end - pos)
  489. return pos - buf;
  490. pos += ret;
  491. }
  492. #ifdef CONFIG_AP
  493. if (wpa_s->ap_iface) {
  494. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  495. end - pos,
  496. verbose);
  497. } else
  498. #endif /* CONFIG_AP */
  499. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  500. }
  501. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  502. wpa_supplicant_state_txt(wpa_s->wpa_state));
  503. if (ret < 0 || ret >= end - pos)
  504. return pos - buf;
  505. pos += ret;
  506. if (wpa_s->l2 &&
  507. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  508. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  509. if (ret < 0 || ret >= end - pos)
  510. return pos - buf;
  511. pos += ret;
  512. }
  513. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  514. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  515. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  516. verbose);
  517. if (res >= 0)
  518. pos += res;
  519. }
  520. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  521. if (res >= 0)
  522. pos += res;
  523. return pos - buf;
  524. }
  525. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  526. char *cmd)
  527. {
  528. char *pos;
  529. int id;
  530. struct wpa_ssid *ssid;
  531. u8 bssid[ETH_ALEN];
  532. /* cmd: "<network id> <BSSID>" */
  533. pos = os_strchr(cmd, ' ');
  534. if (pos == NULL)
  535. return -1;
  536. *pos++ = '\0';
  537. id = atoi(cmd);
  538. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  539. if (hwaddr_aton(pos, bssid)) {
  540. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  541. return -1;
  542. }
  543. ssid = wpa_config_get_network(wpa_s->conf, id);
  544. if (ssid == NULL) {
  545. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  546. "to update", id);
  547. return -1;
  548. }
  549. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  550. ssid->bssid_set = !is_zero_ether_addr(bssid);
  551. return 0;
  552. }
  553. static int wpa_supplicant_ctrl_iface_list_networks(
  554. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  555. {
  556. char *pos, *end;
  557. struct wpa_ssid *ssid;
  558. int ret;
  559. pos = buf;
  560. end = buf + buflen;
  561. ret = os_snprintf(pos, end - pos,
  562. "network id / ssid / bssid / flags\n");
  563. if (ret < 0 || ret >= end - pos)
  564. return pos - buf;
  565. pos += ret;
  566. ssid = wpa_s->conf->ssid;
  567. while (ssid) {
  568. ret = os_snprintf(pos, end - pos, "%d\t%s",
  569. ssid->id,
  570. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  571. if (ret < 0 || ret >= end - pos)
  572. return pos - buf;
  573. pos += ret;
  574. if (ssid->bssid_set) {
  575. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  576. MAC2STR(ssid->bssid));
  577. } else {
  578. ret = os_snprintf(pos, end - pos, "\tany");
  579. }
  580. if (ret < 0 || ret >= end - pos)
  581. return pos - buf;
  582. pos += ret;
  583. ret = os_snprintf(pos, end - pos, "\t%s%s%s",
  584. ssid == wpa_s->current_ssid ?
  585. "[CURRENT]" : "",
  586. ssid->disabled ? "[DISABLED]" : "",
  587. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  588. "");
  589. if (ret < 0 || ret >= end - pos)
  590. return pos - buf;
  591. pos += ret;
  592. ret = os_snprintf(pos, end - pos, "\n");
  593. if (ret < 0 || ret >= end - pos)
  594. return pos - buf;
  595. pos += ret;
  596. ssid = ssid->next;
  597. }
  598. return pos - buf;
  599. }
  600. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  601. {
  602. int first = 1, ret;
  603. ret = os_snprintf(pos, end - pos, "-");
  604. if (ret < 0 || ret >= end - pos)
  605. return pos;
  606. pos += ret;
  607. if (cipher & WPA_CIPHER_NONE) {
  608. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  609. if (ret < 0 || ret >= end - pos)
  610. return pos;
  611. pos += ret;
  612. first = 0;
  613. }
  614. if (cipher & WPA_CIPHER_WEP40) {
  615. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  616. if (ret < 0 || ret >= end - pos)
  617. return pos;
  618. pos += ret;
  619. first = 0;
  620. }
  621. if (cipher & WPA_CIPHER_WEP104) {
  622. ret = os_snprintf(pos, end - pos, "%sWEP104",
  623. first ? "" : "+");
  624. if (ret < 0 || ret >= end - pos)
  625. return pos;
  626. pos += ret;
  627. first = 0;
  628. }
  629. if (cipher & WPA_CIPHER_TKIP) {
  630. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  631. if (ret < 0 || ret >= end - pos)
  632. return pos;
  633. pos += ret;
  634. first = 0;
  635. }
  636. if (cipher & WPA_CIPHER_CCMP) {
  637. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  638. if (ret < 0 || ret >= end - pos)
  639. return pos;
  640. pos += ret;
  641. first = 0;
  642. }
  643. return pos;
  644. }
  645. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  646. const u8 *ie, size_t ie_len)
  647. {
  648. struct wpa_ie_data data;
  649. int first, ret;
  650. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  651. if (ret < 0 || ret >= end - pos)
  652. return pos;
  653. pos += ret;
  654. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  655. ret = os_snprintf(pos, end - pos, "?]");
  656. if (ret < 0 || ret >= end - pos)
  657. return pos;
  658. pos += ret;
  659. return pos;
  660. }
  661. first = 1;
  662. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  663. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  664. if (ret < 0 || ret >= end - pos)
  665. return pos;
  666. pos += ret;
  667. first = 0;
  668. }
  669. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  670. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  671. if (ret < 0 || ret >= end - pos)
  672. return pos;
  673. pos += ret;
  674. first = 0;
  675. }
  676. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  677. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  678. if (ret < 0 || ret >= end - pos)
  679. return pos;
  680. pos += ret;
  681. first = 0;
  682. }
  683. #ifdef CONFIG_IEEE80211R
  684. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  685. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  686. first ? "" : "+");
  687. if (ret < 0 || ret >= end - pos)
  688. return pos;
  689. pos += ret;
  690. first = 0;
  691. }
  692. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  693. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  694. first ? "" : "+");
  695. if (ret < 0 || ret >= end - pos)
  696. return pos;
  697. pos += ret;
  698. first = 0;
  699. }
  700. #endif /* CONFIG_IEEE80211R */
  701. #ifdef CONFIG_IEEE80211W
  702. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  703. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  704. first ? "" : "+");
  705. if (ret < 0 || ret >= end - pos)
  706. return pos;
  707. pos += ret;
  708. first = 0;
  709. }
  710. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  711. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  712. first ? "" : "+");
  713. if (ret < 0 || ret >= end - pos)
  714. return pos;
  715. pos += ret;
  716. first = 0;
  717. }
  718. #endif /* CONFIG_IEEE80211W */
  719. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  720. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  721. ret = os_snprintf(pos, end - pos, "-preauth");
  722. if (ret < 0 || ret >= end - pos)
  723. return pos;
  724. pos += ret;
  725. }
  726. ret = os_snprintf(pos, end - pos, "]");
  727. if (ret < 0 || ret >= end - pos)
  728. return pos;
  729. pos += ret;
  730. return pos;
  731. }
  732. #ifdef CONFIG_WPS
  733. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  734. char *pos, char *end,
  735. struct wpabuf *wps_ie)
  736. {
  737. int ret;
  738. const char *txt;
  739. if (wps_ie == NULL)
  740. return pos;
  741. if (wps_is_selected_pbc_registrar(wps_ie))
  742. txt = "[WPS-PBC]";
  743. #ifdef CONFIG_WPS2
  744. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  745. txt = "[WPS-AUTH]";
  746. #endif /* CONFIG_WPS2 */
  747. else if (wps_is_selected_pin_registrar(wps_ie))
  748. txt = "[WPS-PIN]";
  749. else
  750. txt = "[WPS]";
  751. ret = os_snprintf(pos, end - pos, "%s", txt);
  752. if (ret >= 0 && ret < end - pos)
  753. pos += ret;
  754. wpabuf_free(wps_ie);
  755. return pos;
  756. }
  757. #endif /* CONFIG_WPS */
  758. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  759. char *pos, char *end,
  760. const struct wpa_bss *bss)
  761. {
  762. #ifdef CONFIG_WPS
  763. struct wpabuf *wps_ie;
  764. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  765. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  766. #else /* CONFIG_WPS */
  767. return pos;
  768. #endif /* CONFIG_WPS */
  769. }
  770. /* Format one result on one text line into a buffer. */
  771. static int wpa_supplicant_ctrl_iface_scan_result(
  772. struct wpa_supplicant *wpa_s,
  773. const struct wpa_bss *bss, char *buf, size_t buflen)
  774. {
  775. char *pos, *end;
  776. int ret;
  777. const u8 *ie, *ie2;
  778. pos = buf;
  779. end = buf + buflen;
  780. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  781. MAC2STR(bss->bssid), bss->freq, bss->level);
  782. if (ret < 0 || ret >= end - pos)
  783. return pos - buf;
  784. pos += ret;
  785. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  786. if (ie)
  787. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  788. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  789. if (ie2)
  790. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  791. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  792. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  793. ret = os_snprintf(pos, end - pos, "[WEP]");
  794. if (ret < 0 || ret >= end - pos)
  795. return pos - buf;
  796. pos += ret;
  797. }
  798. if (bss->caps & IEEE80211_CAP_IBSS) {
  799. ret = os_snprintf(pos, end - pos, "[IBSS]");
  800. if (ret < 0 || ret >= end - pos)
  801. return pos - buf;
  802. pos += ret;
  803. }
  804. if (bss->caps & IEEE80211_CAP_ESS) {
  805. ret = os_snprintf(pos, end - pos, "[ESS]");
  806. if (ret < 0 || ret >= end - pos)
  807. return pos - buf;
  808. pos += ret;
  809. }
  810. ret = os_snprintf(pos, end - pos, "\t%s",
  811. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  812. if (ret < 0 || ret >= end - pos)
  813. return pos - buf;
  814. pos += ret;
  815. ret = os_snprintf(pos, end - pos, "\n");
  816. if (ret < 0 || ret >= end - pos)
  817. return pos - buf;
  818. pos += ret;
  819. return pos - buf;
  820. }
  821. static int wpa_supplicant_ctrl_iface_scan_results(
  822. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  823. {
  824. char *pos, *end;
  825. struct wpa_bss *bss;
  826. int ret;
  827. pos = buf;
  828. end = buf + buflen;
  829. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  830. "flags / ssid\n");
  831. if (ret < 0 || ret >= end - pos)
  832. return pos - buf;
  833. pos += ret;
  834. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  835. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  836. end - pos);
  837. if (ret < 0 || ret >= end - pos)
  838. return pos - buf;
  839. pos += ret;
  840. }
  841. return pos - buf;
  842. }
  843. static int wpa_supplicant_ctrl_iface_select_network(
  844. struct wpa_supplicant *wpa_s, char *cmd)
  845. {
  846. int id;
  847. struct wpa_ssid *ssid;
  848. /* cmd: "<network id>" or "any" */
  849. if (os_strcmp(cmd, "any") == 0) {
  850. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  851. ssid = NULL;
  852. } else {
  853. id = atoi(cmd);
  854. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  855. ssid = wpa_config_get_network(wpa_s->conf, id);
  856. if (ssid == NULL) {
  857. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  858. "network id=%d", id);
  859. return -1;
  860. }
  861. if (ssid->disabled == 2) {
  862. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  863. "SELECT_NETWORK with persistent P2P group");
  864. return -1;
  865. }
  866. }
  867. wpa_supplicant_select_network(wpa_s, ssid);
  868. return 0;
  869. }
  870. static int wpa_supplicant_ctrl_iface_enable_network(
  871. struct wpa_supplicant *wpa_s, char *cmd)
  872. {
  873. int id;
  874. struct wpa_ssid *ssid;
  875. /* cmd: "<network id>" or "all" */
  876. if (os_strcmp(cmd, "all") == 0) {
  877. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  878. ssid = NULL;
  879. } else {
  880. id = atoi(cmd);
  881. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  882. ssid = wpa_config_get_network(wpa_s->conf, id);
  883. if (ssid == NULL) {
  884. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  885. "network id=%d", id);
  886. return -1;
  887. }
  888. if (ssid->disabled == 2) {
  889. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  890. "ENABLE_NETWORK with persistent P2P group");
  891. return -1;
  892. }
  893. }
  894. wpa_supplicant_enable_network(wpa_s, ssid);
  895. return 0;
  896. }
  897. static int wpa_supplicant_ctrl_iface_disable_network(
  898. struct wpa_supplicant *wpa_s, char *cmd)
  899. {
  900. int id;
  901. struct wpa_ssid *ssid;
  902. /* cmd: "<network id>" or "all" */
  903. if (os_strcmp(cmd, "all") == 0) {
  904. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  905. ssid = NULL;
  906. } else {
  907. id = atoi(cmd);
  908. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  909. ssid = wpa_config_get_network(wpa_s->conf, id);
  910. if (ssid == NULL) {
  911. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  912. "network id=%d", id);
  913. return -1;
  914. }
  915. if (ssid->disabled == 2) {
  916. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  917. "DISABLE_NETWORK with persistent P2P "
  918. "group");
  919. return -1;
  920. }
  921. }
  922. wpa_supplicant_disable_network(wpa_s, ssid);
  923. return 0;
  924. }
  925. static int wpa_supplicant_ctrl_iface_add_network(
  926. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  927. {
  928. struct wpa_ssid *ssid;
  929. int ret;
  930. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  931. ssid = wpa_config_add_network(wpa_s->conf);
  932. if (ssid == NULL)
  933. return -1;
  934. wpas_notify_network_added(wpa_s, ssid);
  935. ssid->disabled = 1;
  936. wpa_config_set_network_defaults(ssid);
  937. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  938. if (ret < 0 || (size_t) ret >= buflen)
  939. return -1;
  940. return ret;
  941. }
  942. static int wpa_supplicant_ctrl_iface_remove_network(
  943. struct wpa_supplicant *wpa_s, char *cmd)
  944. {
  945. int id;
  946. struct wpa_ssid *ssid;
  947. /* cmd: "<network id>" or "all" */
  948. if (os_strcmp(cmd, "all") == 0) {
  949. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  950. ssid = wpa_s->conf->ssid;
  951. while (ssid) {
  952. struct wpa_ssid *remove_ssid = ssid;
  953. id = ssid->id;
  954. ssid = ssid->next;
  955. wpas_notify_network_removed(wpa_s, remove_ssid);
  956. wpa_config_remove_network(wpa_s->conf, id);
  957. }
  958. if (wpa_s->current_ssid) {
  959. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  960. wpa_supplicant_disassociate(wpa_s,
  961. WLAN_REASON_DEAUTH_LEAVING);
  962. }
  963. return 0;
  964. }
  965. id = atoi(cmd);
  966. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  967. ssid = wpa_config_get_network(wpa_s->conf, id);
  968. if (ssid == NULL ||
  969. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  970. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  971. "id=%d", id);
  972. return -1;
  973. }
  974. if (ssid == wpa_s->current_ssid) {
  975. /*
  976. * Invalidate the EAP session cache if the current network is
  977. * removed.
  978. */
  979. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  980. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  981. }
  982. return 0;
  983. }
  984. static int wpa_supplicant_ctrl_iface_set_network(
  985. struct wpa_supplicant *wpa_s, char *cmd)
  986. {
  987. int id;
  988. struct wpa_ssid *ssid;
  989. char *name, *value;
  990. /* cmd: "<network id> <variable name> <value>" */
  991. name = os_strchr(cmd, ' ');
  992. if (name == NULL)
  993. return -1;
  994. *name++ = '\0';
  995. value = os_strchr(name, ' ');
  996. if (value == NULL)
  997. return -1;
  998. *value++ = '\0';
  999. id = atoi(cmd);
  1000. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1001. id, name);
  1002. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1003. (u8 *) value, os_strlen(value));
  1004. ssid = wpa_config_get_network(wpa_s->conf, id);
  1005. if (ssid == NULL) {
  1006. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1007. "id=%d", id);
  1008. return -1;
  1009. }
  1010. if (wpa_config_set(ssid, name, value, 0) < 0) {
  1011. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  1012. "variable '%s'", name);
  1013. return -1;
  1014. }
  1015. if (wpa_s->current_ssid == ssid) {
  1016. /*
  1017. * Invalidate the EAP session cache if anything in the current
  1018. * configuration changes.
  1019. */
  1020. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1021. }
  1022. if ((os_strcmp(name, "psk") == 0 &&
  1023. value[0] == '"' && ssid->ssid_len) ||
  1024. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  1025. wpa_config_update_psk(ssid);
  1026. else if (os_strcmp(name, "priority") == 0)
  1027. wpa_config_update_prio_list(wpa_s->conf);
  1028. return 0;
  1029. }
  1030. static int wpa_supplicant_ctrl_iface_get_network(
  1031. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1032. {
  1033. int id;
  1034. size_t res;
  1035. struct wpa_ssid *ssid;
  1036. char *name, *value;
  1037. /* cmd: "<network id> <variable name>" */
  1038. name = os_strchr(cmd, ' ');
  1039. if (name == NULL || buflen == 0)
  1040. return -1;
  1041. *name++ = '\0';
  1042. id = atoi(cmd);
  1043. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1044. id, name);
  1045. ssid = wpa_config_get_network(wpa_s->conf, id);
  1046. if (ssid == NULL) {
  1047. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1048. "id=%d", id);
  1049. return -1;
  1050. }
  1051. value = wpa_config_get_no_key(ssid, name);
  1052. if (value == NULL) {
  1053. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1054. "variable '%s'", name);
  1055. return -1;
  1056. }
  1057. res = os_strlcpy(buf, value, buflen);
  1058. if (res >= buflen) {
  1059. os_free(value);
  1060. return -1;
  1061. }
  1062. os_free(value);
  1063. return res;
  1064. }
  1065. #ifndef CONFIG_NO_CONFIG_WRITE
  1066. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  1067. {
  1068. int ret;
  1069. if (!wpa_s->conf->update_config) {
  1070. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  1071. "to update configuration (update_config=0)");
  1072. return -1;
  1073. }
  1074. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1075. if (ret) {
  1076. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  1077. "update configuration");
  1078. } else {
  1079. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  1080. " updated");
  1081. }
  1082. return ret;
  1083. }
  1084. #endif /* CONFIG_NO_CONFIG_WRITE */
  1085. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1086. struct wpa_driver_capa *capa,
  1087. char *buf, size_t buflen)
  1088. {
  1089. int ret, first = 1;
  1090. char *pos, *end;
  1091. size_t len;
  1092. pos = buf;
  1093. end = pos + buflen;
  1094. if (res < 0) {
  1095. if (strict)
  1096. return 0;
  1097. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1098. if (len >= buflen)
  1099. return -1;
  1100. return len;
  1101. }
  1102. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1103. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1104. if (ret < 0 || ret >= end - pos)
  1105. return pos - buf;
  1106. pos += ret;
  1107. first = 0;
  1108. }
  1109. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1110. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1111. if (ret < 0 || ret >= end - pos)
  1112. return pos - buf;
  1113. pos += ret;
  1114. first = 0;
  1115. }
  1116. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1117. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1118. if (ret < 0 || ret >= end - pos)
  1119. return pos - buf;
  1120. pos += ret;
  1121. first = 0;
  1122. }
  1123. return pos - buf;
  1124. }
  1125. static int ctrl_iface_get_capability_group(int res, char *strict,
  1126. struct wpa_driver_capa *capa,
  1127. char *buf, size_t buflen)
  1128. {
  1129. int ret, first = 1;
  1130. char *pos, *end;
  1131. size_t len;
  1132. pos = buf;
  1133. end = pos + buflen;
  1134. if (res < 0) {
  1135. if (strict)
  1136. return 0;
  1137. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1138. if (len >= buflen)
  1139. return -1;
  1140. return len;
  1141. }
  1142. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1143. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1144. if (ret < 0 || ret >= end - pos)
  1145. return pos - buf;
  1146. pos += ret;
  1147. first = 0;
  1148. }
  1149. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1150. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1151. if (ret < 0 || ret >= end - pos)
  1152. return pos - buf;
  1153. pos += ret;
  1154. first = 0;
  1155. }
  1156. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1157. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1158. first ? "" : " ");
  1159. if (ret < 0 || ret >= end - pos)
  1160. return pos - buf;
  1161. pos += ret;
  1162. first = 0;
  1163. }
  1164. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1165. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1166. if (ret < 0 || ret >= end - pos)
  1167. return pos - buf;
  1168. pos += ret;
  1169. first = 0;
  1170. }
  1171. return pos - buf;
  1172. }
  1173. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1174. struct wpa_driver_capa *capa,
  1175. char *buf, size_t buflen)
  1176. {
  1177. int ret;
  1178. char *pos, *end;
  1179. size_t len;
  1180. pos = buf;
  1181. end = pos + buflen;
  1182. if (res < 0) {
  1183. if (strict)
  1184. return 0;
  1185. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1186. "NONE", buflen);
  1187. if (len >= buflen)
  1188. return -1;
  1189. return len;
  1190. }
  1191. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1192. if (ret < 0 || ret >= end - pos)
  1193. return pos - buf;
  1194. pos += ret;
  1195. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1196. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1197. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1198. if (ret < 0 || ret >= end - pos)
  1199. return pos - buf;
  1200. pos += ret;
  1201. }
  1202. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1203. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1204. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1205. if (ret < 0 || ret >= end - pos)
  1206. return pos - buf;
  1207. pos += ret;
  1208. }
  1209. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1210. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1211. if (ret < 0 || ret >= end - pos)
  1212. return pos - buf;
  1213. pos += ret;
  1214. }
  1215. return pos - buf;
  1216. }
  1217. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1218. struct wpa_driver_capa *capa,
  1219. char *buf, size_t buflen)
  1220. {
  1221. int ret, first = 1;
  1222. char *pos, *end;
  1223. size_t len;
  1224. pos = buf;
  1225. end = pos + buflen;
  1226. if (res < 0) {
  1227. if (strict)
  1228. return 0;
  1229. len = os_strlcpy(buf, "RSN WPA", buflen);
  1230. if (len >= buflen)
  1231. return -1;
  1232. return len;
  1233. }
  1234. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1235. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1236. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1237. if (ret < 0 || ret >= end - pos)
  1238. return pos - buf;
  1239. pos += ret;
  1240. first = 0;
  1241. }
  1242. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1243. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1244. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1245. if (ret < 0 || ret >= end - pos)
  1246. return pos - buf;
  1247. pos += ret;
  1248. first = 0;
  1249. }
  1250. return pos - buf;
  1251. }
  1252. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  1253. struct wpa_driver_capa *capa,
  1254. char *buf, size_t buflen)
  1255. {
  1256. int ret, first = 1;
  1257. char *pos, *end;
  1258. size_t len;
  1259. pos = buf;
  1260. end = pos + buflen;
  1261. if (res < 0) {
  1262. if (strict)
  1263. return 0;
  1264. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  1265. if (len >= buflen)
  1266. return -1;
  1267. return len;
  1268. }
  1269. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1270. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1271. if (ret < 0 || ret >= end - pos)
  1272. return pos - buf;
  1273. pos += ret;
  1274. first = 0;
  1275. }
  1276. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1277. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1278. first ? "" : " ");
  1279. if (ret < 0 || ret >= end - pos)
  1280. return pos - buf;
  1281. pos += ret;
  1282. first = 0;
  1283. }
  1284. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  1285. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  1286. if (ret < 0 || ret >= end - pos)
  1287. return pos - buf;
  1288. pos += ret;
  1289. first = 0;
  1290. }
  1291. return pos - buf;
  1292. }
  1293. static int wpa_supplicant_ctrl_iface_get_capability(
  1294. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  1295. size_t buflen)
  1296. {
  1297. struct wpa_driver_capa capa;
  1298. int res;
  1299. char *strict;
  1300. char field[30];
  1301. size_t len;
  1302. /* Determine whether or not strict checking was requested */
  1303. len = os_strlcpy(field, _field, sizeof(field));
  1304. if (len >= sizeof(field))
  1305. return -1;
  1306. strict = os_strchr(field, ' ');
  1307. if (strict != NULL) {
  1308. *strict++ = '\0';
  1309. if (os_strcmp(strict, "strict") != 0)
  1310. return -1;
  1311. }
  1312. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1313. field, strict ? strict : "");
  1314. if (os_strcmp(field, "eap") == 0) {
  1315. return eap_get_names(buf, buflen);
  1316. }
  1317. res = wpa_drv_get_capa(wpa_s, &capa);
  1318. if (os_strcmp(field, "pairwise") == 0)
  1319. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1320. buf, buflen);
  1321. if (os_strcmp(field, "group") == 0)
  1322. return ctrl_iface_get_capability_group(res, strict, &capa,
  1323. buf, buflen);
  1324. if (os_strcmp(field, "key_mgmt") == 0)
  1325. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1326. buf, buflen);
  1327. if (os_strcmp(field, "proto") == 0)
  1328. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1329. buf, buflen);
  1330. if (os_strcmp(field, "auth_alg") == 0)
  1331. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1332. buf, buflen);
  1333. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  1334. field);
  1335. return -1;
  1336. }
  1337. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  1338. const char *cmd, char *buf,
  1339. size_t buflen)
  1340. {
  1341. u8 bssid[ETH_ALEN];
  1342. size_t i;
  1343. struct wpa_bss *bss;
  1344. int ret;
  1345. char *pos, *end;
  1346. const u8 *ie, *ie2;
  1347. if (os_strcmp(cmd, "FIRST") == 0)
  1348. bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
  1349. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  1350. i = atoi(cmd + 3);
  1351. bss = wpa_bss_get_id(wpa_s, i);
  1352. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  1353. i = atoi(cmd + 5);
  1354. bss = wpa_bss_get_id(wpa_s, i);
  1355. if (bss) {
  1356. struct dl_list *next = bss->list_id.next;
  1357. if (next == &wpa_s->bss_id)
  1358. bss = NULL;
  1359. else
  1360. bss = dl_list_entry(next, struct wpa_bss,
  1361. list_id);
  1362. }
  1363. } else if (hwaddr_aton(cmd, bssid) == 0)
  1364. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1365. else {
  1366. struct wpa_bss *tmp;
  1367. i = atoi(cmd);
  1368. bss = NULL;
  1369. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  1370. {
  1371. if (i-- == 0) {
  1372. bss = tmp;
  1373. break;
  1374. }
  1375. }
  1376. }
  1377. if (bss == NULL)
  1378. return 0;
  1379. pos = buf;
  1380. end = buf + buflen;
  1381. ret = os_snprintf(pos, end - pos,
  1382. "id=%u\n"
  1383. "bssid=" MACSTR "\n"
  1384. "freq=%d\n"
  1385. "beacon_int=%d\n"
  1386. "capabilities=0x%04x\n"
  1387. "qual=%d\n"
  1388. "noise=%d\n"
  1389. "level=%d\n"
  1390. "tsf=%016llu\n"
  1391. "ie=",
  1392. bss->id,
  1393. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1394. bss->caps, bss->qual, bss->noise, bss->level,
  1395. (unsigned long long) bss->tsf);
  1396. if (ret < 0 || ret >= end - pos)
  1397. return pos - buf;
  1398. pos += ret;
  1399. ie = (const u8 *) (bss + 1);
  1400. for (i = 0; i < bss->ie_len; i++) {
  1401. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  1402. if (ret < 0 || ret >= end - pos)
  1403. return pos - buf;
  1404. pos += ret;
  1405. }
  1406. ret = os_snprintf(pos, end - pos, "\n");
  1407. if (ret < 0 || ret >= end - pos)
  1408. return pos - buf;
  1409. pos += ret;
  1410. ret = os_snprintf(pos, end - pos, "flags=");
  1411. if (ret < 0 || ret >= end - pos)
  1412. return pos - buf;
  1413. pos += ret;
  1414. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1415. if (ie)
  1416. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1417. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1418. if (ie2)
  1419. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1420. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1421. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1422. ret = os_snprintf(pos, end - pos, "[WEP]");
  1423. if (ret < 0 || ret >= end - pos)
  1424. return pos - buf;
  1425. pos += ret;
  1426. }
  1427. if (bss->caps & IEEE80211_CAP_IBSS) {
  1428. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1429. if (ret < 0 || ret >= end - pos)
  1430. return pos - buf;
  1431. pos += ret;
  1432. }
  1433. if (bss->caps & IEEE80211_CAP_ESS) {
  1434. ret = os_snprintf(pos, end - pos, "[ESS]");
  1435. if (ret < 0 || ret >= end - pos)
  1436. return pos - buf;
  1437. pos += ret;
  1438. }
  1439. ret = os_snprintf(pos, end - pos, "\n");
  1440. if (ret < 0 || ret >= end - pos)
  1441. return pos - buf;
  1442. pos += ret;
  1443. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1444. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1445. if (ret < 0 || ret >= end - pos)
  1446. return pos - buf;
  1447. pos += ret;
  1448. #ifdef CONFIG_WPS
  1449. ie = (const u8 *) (bss + 1);
  1450. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  1451. if (ret < 0 || ret >= end - pos)
  1452. return pos - buf;
  1453. pos += ret;
  1454. #endif /* CONFIG_WPS */
  1455. return pos - buf;
  1456. }
  1457. static int wpa_supplicant_ctrl_iface_ap_scan(
  1458. struct wpa_supplicant *wpa_s, char *cmd)
  1459. {
  1460. int ap_scan = atoi(cmd);
  1461. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  1462. }
  1463. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  1464. {
  1465. u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
  1466. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  1467. /* MLME-DELETEKEYS.request */
  1468. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
  1469. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
  1470. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
  1471. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
  1472. #ifdef CONFIG_IEEE80211W
  1473. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
  1474. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
  1475. #endif /* CONFIG_IEEE80211W */
  1476. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  1477. 0);
  1478. /* MLME-SETPROTECTION.request(None) */
  1479. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  1480. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  1481. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1482. wpa_sm_drop_sa(wpa_s->wpa);
  1483. }
  1484. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  1485. char *addr)
  1486. {
  1487. u8 bssid[ETH_ALEN];
  1488. struct wpa_bss *bss;
  1489. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1490. if (hwaddr_aton(addr, bssid)) {
  1491. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  1492. "address '%s'", addr);
  1493. return -1;
  1494. }
  1495. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  1496. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1497. if (!bss) {
  1498. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  1499. "from BSS table");
  1500. return -1;
  1501. }
  1502. /*
  1503. * TODO: Find best network configuration block from configuration to
  1504. * allow roaming to other networks
  1505. */
  1506. if (!ssid) {
  1507. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  1508. "configuration known for the target AP");
  1509. return -1;
  1510. }
  1511. wpa_s->reassociate = 1;
  1512. wpa_supplicant_connect(wpa_s, bss, ssid);
  1513. return 0;
  1514. }
  1515. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  1516. char *buf, size_t *resp_len)
  1517. {
  1518. char *reply;
  1519. const int reply_size = 2048;
  1520. int ctrl_rsp = 0;
  1521. int reply_len;
  1522. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  1523. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1524. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  1525. (const u8 *) buf, os_strlen(buf));
  1526. } else {
  1527. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
  1528. (const u8 *) buf, os_strlen(buf));
  1529. }
  1530. reply = os_malloc(reply_size);
  1531. if (reply == NULL) {
  1532. *resp_len = 1;
  1533. return NULL;
  1534. }
  1535. os_memcpy(reply, "OK\n", 3);
  1536. reply_len = 3;
  1537. if (os_strcmp(buf, "PING") == 0) {
  1538. os_memcpy(reply, "PONG\n", 5);
  1539. reply_len = 5;
  1540. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  1541. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  1542. } else if (os_strcmp(buf, "MIB") == 0) {
  1543. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  1544. if (reply_len >= 0) {
  1545. int res;
  1546. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  1547. reply_size - reply_len);
  1548. if (res < 0)
  1549. reply_len = -1;
  1550. else
  1551. reply_len += res;
  1552. }
  1553. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  1554. reply_len = wpa_supplicant_ctrl_iface_status(
  1555. wpa_s, buf + 6, reply, reply_size);
  1556. } else if (os_strcmp(buf, "PMKSA") == 0) {
  1557. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  1558. reply_size);
  1559. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1560. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  1561. reply_len = -1;
  1562. } else if (os_strcmp(buf, "LOGON") == 0) {
  1563. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1564. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  1565. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1566. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  1567. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1568. reply_len = -1;
  1569. else {
  1570. wpa_s->disconnected = 0;
  1571. wpa_s->reassociate = 1;
  1572. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1573. }
  1574. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  1575. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1576. reply_len = -1;
  1577. else if (wpa_s->disconnected) {
  1578. wpa_s->disconnected = 0;
  1579. wpa_s->reassociate = 1;
  1580. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1581. }
  1582. #ifdef IEEE8021X_EAPOL
  1583. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  1584. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  1585. reply_len = -1;
  1586. #endif /* IEEE8021X_EAPOL */
  1587. #ifdef CONFIG_PEERKEY
  1588. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  1589. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  1590. reply_len = -1;
  1591. #endif /* CONFIG_PEERKEY */
  1592. #ifdef CONFIG_IEEE80211R
  1593. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  1594. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  1595. reply_len = -1;
  1596. #endif /* CONFIG_IEEE80211R */
  1597. #ifdef CONFIG_WPS
  1598. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1599. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
  1600. reply_len = -1;
  1601. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  1602. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
  1603. reply_len = -1;
  1604. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1605. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  1606. reply,
  1607. reply_size);
  1608. #ifdef CONFIG_WPS_OOB
  1609. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  1610. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  1611. reply_len = -1;
  1612. #endif /* CONFIG_WPS_OOB */
  1613. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  1614. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  1615. reply_len = -1;
  1616. #ifdef CONFIG_WPS_ER
  1617. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  1618. if (wpas_wps_er_start(wpa_s, NULL))
  1619. reply_len = -1;
  1620. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  1621. if (wpas_wps_er_start(wpa_s, buf + 13))
  1622. reply_len = -1;
  1623. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  1624. if (wpas_wps_er_stop(wpa_s))
  1625. reply_len = -1;
  1626. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  1627. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  1628. reply_len = -1;
  1629. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  1630. if (wpas_wps_er_pbc(wpa_s, buf + 11))
  1631. reply_len = -1;
  1632. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  1633. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  1634. reply_len = -1;
  1635. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  1636. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  1637. reply_len = -1;
  1638. #endif /* CONFIG_WPS_ER */
  1639. #endif /* CONFIG_WPS */
  1640. #ifdef CONFIG_IBSS_RSN
  1641. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  1642. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  1643. reply_len = -1;
  1644. #endif /* CONFIG_IBSS_RSN */
  1645. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  1646. {
  1647. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  1648. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  1649. reply_len = -1;
  1650. else
  1651. ctrl_rsp = 1;
  1652. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  1653. if (wpa_supplicant_reload_configuration(wpa_s))
  1654. reply_len = -1;
  1655. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1656. wpa_supplicant_terminate_proc(wpa_s->global);
  1657. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  1658. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  1659. reply_len = -1;
  1660. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  1661. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  1662. wpa_s, reply, reply_size);
  1663. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  1664. wpa_s->reassociate = 0;
  1665. wpa_s->disconnected = 1;
  1666. wpa_supplicant_deauthenticate(wpa_s,
  1667. WLAN_REASON_DEAUTH_LEAVING);
  1668. } else if (os_strcmp(buf, "SCAN") == 0) {
  1669. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1670. reply_len = -1;
  1671. else {
  1672. wpa_s->scan_req = 2;
  1673. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1674. }
  1675. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  1676. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  1677. wpa_s, reply, reply_size);
  1678. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  1679. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  1680. reply_len = -1;
  1681. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  1682. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  1683. reply_len = -1;
  1684. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  1685. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  1686. reply_len = -1;
  1687. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  1688. reply_len = wpa_supplicant_ctrl_iface_add_network(
  1689. wpa_s, reply, reply_size);
  1690. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  1691. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  1692. reply_len = -1;
  1693. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1694. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  1695. reply_len = -1;
  1696. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  1697. reply_len = wpa_supplicant_ctrl_iface_get_network(
  1698. wpa_s, buf + 12, reply, reply_size);
  1699. #ifndef CONFIG_NO_CONFIG_WRITE
  1700. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  1701. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  1702. reply_len = -1;
  1703. #endif /* CONFIG_NO_CONFIG_WRITE */
  1704. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  1705. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  1706. wpa_s, buf + 15, reply, reply_size);
  1707. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  1708. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  1709. reply_len = -1;
  1710. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1711. reply_len = wpa_supplicant_global_iface_list(
  1712. wpa_s->global, reply, reply_size);
  1713. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1714. reply_len = wpa_supplicant_global_iface_interfaces(
  1715. wpa_s->global, reply, reply_size);
  1716. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  1717. reply_len = wpa_supplicant_ctrl_iface_bss(
  1718. wpa_s, buf + 4, reply, reply_size);
  1719. #ifdef CONFIG_AP
  1720. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1721. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  1722. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1723. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  1724. reply_size);
  1725. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1726. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  1727. reply_size);
  1728. #endif /* CONFIG_AP */
  1729. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  1730. wpas_notify_suspend(wpa_s->global);
  1731. } else if (os_strcmp(buf, "RESUME") == 0) {
  1732. wpas_notify_resume(wpa_s->global);
  1733. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  1734. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  1735. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  1736. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  1737. reply_len = -1;
  1738. } else {
  1739. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1740. reply_len = 16;
  1741. }
  1742. if (reply_len < 0) {
  1743. os_memcpy(reply, "FAIL\n", 5);
  1744. reply_len = 5;
  1745. }
  1746. if (ctrl_rsp)
  1747. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1748. *resp_len = reply_len;
  1749. return reply;
  1750. }
  1751. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  1752. char *cmd)
  1753. {
  1754. struct wpa_interface iface;
  1755. char *pos;
  1756. /*
  1757. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  1758. * TAB<bridge_ifname>
  1759. */
  1760. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  1761. os_memset(&iface, 0, sizeof(iface));
  1762. do {
  1763. iface.ifname = pos = cmd;
  1764. pos = os_strchr(pos, '\t');
  1765. if (pos)
  1766. *pos++ = '\0';
  1767. if (iface.ifname[0] == '\0')
  1768. return -1;
  1769. if (pos == NULL)
  1770. break;
  1771. iface.confname = pos;
  1772. pos = os_strchr(pos, '\t');
  1773. if (pos)
  1774. *pos++ = '\0';
  1775. if (iface.confname[0] == '\0')
  1776. iface.confname = NULL;
  1777. if (pos == NULL)
  1778. break;
  1779. iface.driver = pos;
  1780. pos = os_strchr(pos, '\t');
  1781. if (pos)
  1782. *pos++ = '\0';
  1783. if (iface.driver[0] == '\0')
  1784. iface.driver = NULL;
  1785. if (pos == NULL)
  1786. break;
  1787. iface.ctrl_interface = pos;
  1788. pos = os_strchr(pos, '\t');
  1789. if (pos)
  1790. *pos++ = '\0';
  1791. if (iface.ctrl_interface[0] == '\0')
  1792. iface.ctrl_interface = NULL;
  1793. if (pos == NULL)
  1794. break;
  1795. iface.driver_param = pos;
  1796. pos = os_strchr(pos, '\t');
  1797. if (pos)
  1798. *pos++ = '\0';
  1799. if (iface.driver_param[0] == '\0')
  1800. iface.driver_param = NULL;
  1801. if (pos == NULL)
  1802. break;
  1803. iface.bridge_ifname = pos;
  1804. pos = os_strchr(pos, '\t');
  1805. if (pos)
  1806. *pos++ = '\0';
  1807. if (iface.bridge_ifname[0] == '\0')
  1808. iface.bridge_ifname = NULL;
  1809. if (pos == NULL)
  1810. break;
  1811. } while (0);
  1812. if (wpa_supplicant_get_iface(global, iface.ifname))
  1813. return -1;
  1814. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  1815. }
  1816. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  1817. char *cmd)
  1818. {
  1819. struct wpa_supplicant *wpa_s;
  1820. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  1821. wpa_s = wpa_supplicant_get_iface(global, cmd);
  1822. if (wpa_s == NULL)
  1823. return -1;
  1824. return wpa_supplicant_remove_iface(global, wpa_s);
  1825. }
  1826. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  1827. {
  1828. struct wpa_interface_info *prev;
  1829. while (iface) {
  1830. prev = iface;
  1831. iface = iface->next;
  1832. os_free(prev->ifname);
  1833. os_free(prev->desc);
  1834. os_free(prev);
  1835. }
  1836. }
  1837. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  1838. char *buf, int len)
  1839. {
  1840. int i, res;
  1841. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  1842. char *pos, *end;
  1843. for (i = 0; wpa_drivers[i]; i++) {
  1844. struct wpa_driver_ops *drv = wpa_drivers[i];
  1845. if (drv->get_interfaces == NULL)
  1846. continue;
  1847. tmp = drv->get_interfaces(global->drv_priv[i]);
  1848. if (tmp == NULL)
  1849. continue;
  1850. if (last == NULL)
  1851. iface = last = tmp;
  1852. else
  1853. last->next = tmp;
  1854. while (last->next)
  1855. last = last->next;
  1856. }
  1857. pos = buf;
  1858. end = buf + len;
  1859. for (tmp = iface; tmp; tmp = tmp->next) {
  1860. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  1861. tmp->drv_name, tmp->ifname,
  1862. tmp->desc ? tmp->desc : "");
  1863. if (res < 0 || res >= end - pos) {
  1864. *pos = '\0';
  1865. break;
  1866. }
  1867. pos += res;
  1868. }
  1869. wpa_free_iface_info(iface);
  1870. return pos - buf;
  1871. }
  1872. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  1873. char *buf, int len)
  1874. {
  1875. int res;
  1876. char *pos, *end;
  1877. struct wpa_supplicant *wpa_s;
  1878. wpa_s = global->ifaces;
  1879. pos = buf;
  1880. end = buf + len;
  1881. while (wpa_s) {
  1882. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  1883. if (res < 0 || res >= end - pos) {
  1884. *pos = '\0';
  1885. break;
  1886. }
  1887. pos += res;
  1888. wpa_s = wpa_s->next;
  1889. }
  1890. return pos - buf;
  1891. }
  1892. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  1893. char *buf, size_t *resp_len)
  1894. {
  1895. char *reply;
  1896. const int reply_size = 2048;
  1897. int reply_len;
  1898. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  1899. (const u8 *) buf, os_strlen(buf));
  1900. reply = os_malloc(reply_size);
  1901. if (reply == NULL) {
  1902. *resp_len = 1;
  1903. return NULL;
  1904. }
  1905. os_memcpy(reply, "OK\n", 3);
  1906. reply_len = 3;
  1907. if (os_strcmp(buf, "PING") == 0) {
  1908. os_memcpy(reply, "PONG\n", 5);
  1909. reply_len = 5;
  1910. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  1911. if (wpa_supplicant_global_iface_add(global, buf + 14))
  1912. reply_len = -1;
  1913. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  1914. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  1915. reply_len = -1;
  1916. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1917. reply_len = wpa_supplicant_global_iface_list(
  1918. global, reply, reply_size);
  1919. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1920. reply_len = wpa_supplicant_global_iface_interfaces(
  1921. global, reply, reply_size);
  1922. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1923. wpa_supplicant_terminate_proc(global);
  1924. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  1925. wpas_notify_suspend(global);
  1926. } else if (os_strcmp(buf, "RESUME") == 0) {
  1927. wpas_notify_resume(global);
  1928. } else {
  1929. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1930. reply_len = 16;
  1931. }
  1932. if (reply_len < 0) {
  1933. os_memcpy(reply, "FAIL\n", 5);
  1934. reply_len = 5;
  1935. }
  1936. *resp_len = reply_len;
  1937. return reply;
  1938. }