ctrl_iface.c 49 KB

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