ctrl_iface.c 45 KB

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