ctrl_iface.c 46 KB

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