ctrl_iface.c 47 KB

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