ctrl_iface.c 48 KB

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