ctrl_iface.c 45 KB

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