ctrl_iface.c 46 KB

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