ctrl_iface.c 47 KB

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