ctrl_iface.c 48 KB

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