ctrl_iface.c 45 KB

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