ctrl_iface.c 47 KB

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