ctrl_iface.c 46 KB

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