ctrl_iface.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #ifndef CONFIG_NATIVE_WINDOWS
  10. #include <sys/un.h>
  11. #include <sys/stat.h>
  12. #include <stddef.h>
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "common/version.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "drivers/driver.h"
  18. #include "radius/radius_client.h"
  19. #include "ap/hostapd.h"
  20. #include "ap/ap_config.h"
  21. #include "ap/ieee802_1x.h"
  22. #include "ap/wpa_auth.h"
  23. #include "ap/ieee802_11.h"
  24. #include "ap/sta_info.h"
  25. #include "ap/wps_hostapd.h"
  26. #include "ap/ctrl_iface_ap.h"
  27. #include "ap/ap_drv_ops.h"
  28. #include "wps/wps_defs.h"
  29. #include "wps/wps.h"
  30. #include "config_file.h"
  31. #include "ctrl_iface.h"
  32. struct wpa_ctrl_dst {
  33. struct wpa_ctrl_dst *next;
  34. struct sockaddr_un addr;
  35. socklen_t addrlen;
  36. int debug_level;
  37. int errors;
  38. };
  39. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  40. const char *buf, size_t len);
  41. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  42. struct sockaddr_un *from,
  43. socklen_t fromlen)
  44. {
  45. struct wpa_ctrl_dst *dst;
  46. dst = os_zalloc(sizeof(*dst));
  47. if (dst == NULL)
  48. return -1;
  49. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  50. dst->addrlen = fromlen;
  51. dst->debug_level = MSG_INFO;
  52. dst->next = hapd->ctrl_dst;
  53. hapd->ctrl_dst = dst;
  54. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  55. (u8 *) from->sun_path,
  56. fromlen - offsetof(struct sockaddr_un, sun_path));
  57. return 0;
  58. }
  59. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  60. struct sockaddr_un *from,
  61. socklen_t fromlen)
  62. {
  63. struct wpa_ctrl_dst *dst, *prev = NULL;
  64. dst = hapd->ctrl_dst;
  65. while (dst) {
  66. if (fromlen == dst->addrlen &&
  67. os_memcmp(from->sun_path, dst->addr.sun_path,
  68. fromlen - offsetof(struct sockaddr_un, sun_path))
  69. == 0) {
  70. if (prev == NULL)
  71. hapd->ctrl_dst = dst->next;
  72. else
  73. prev->next = dst->next;
  74. os_free(dst);
  75. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  76. (u8 *) from->sun_path,
  77. fromlen -
  78. offsetof(struct sockaddr_un, sun_path));
  79. return 0;
  80. }
  81. prev = dst;
  82. dst = dst->next;
  83. }
  84. return -1;
  85. }
  86. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  87. struct sockaddr_un *from,
  88. socklen_t fromlen,
  89. char *level)
  90. {
  91. struct wpa_ctrl_dst *dst;
  92. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  93. dst = hapd->ctrl_dst;
  94. while (dst) {
  95. if (fromlen == dst->addrlen &&
  96. os_memcmp(from->sun_path, dst->addr.sun_path,
  97. fromlen - offsetof(struct sockaddr_un, sun_path))
  98. == 0) {
  99. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  100. "level", (u8 *) from->sun_path, fromlen -
  101. offsetof(struct sockaddr_un, sun_path));
  102. dst->debug_level = atoi(level);
  103. return 0;
  104. }
  105. dst = dst->next;
  106. }
  107. return -1;
  108. }
  109. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  110. const char *txtaddr)
  111. {
  112. u8 addr[ETH_ALEN];
  113. struct sta_info *sta;
  114. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  115. if (hwaddr_aton(txtaddr, addr))
  116. return -1;
  117. sta = ap_get_sta(hapd, addr);
  118. if (sta)
  119. return 0;
  120. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  121. "notification", MAC2STR(addr));
  122. sta = ap_sta_add(hapd, addr);
  123. if (sta == NULL)
  124. return -1;
  125. hostapd_new_assoc_sta(hapd, sta, 0);
  126. return 0;
  127. }
  128. #ifdef CONFIG_IEEE80211W
  129. #ifdef NEED_AP_MLME
  130. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  131. const char *txtaddr)
  132. {
  133. u8 addr[ETH_ALEN];
  134. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  135. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  136. if (hwaddr_aton(txtaddr, addr) ||
  137. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  138. return -1;
  139. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  140. return 0;
  141. }
  142. #endif /* NEED_AP_MLME */
  143. #endif /* CONFIG_IEEE80211W */
  144. #ifdef CONFIG_WPS
  145. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  146. {
  147. char *pin = os_strchr(txt, ' ');
  148. char *timeout_txt;
  149. int timeout;
  150. u8 addr_buf[ETH_ALEN], *addr = NULL;
  151. char *pos;
  152. if (pin == NULL)
  153. return -1;
  154. *pin++ = '\0';
  155. timeout_txt = os_strchr(pin, ' ');
  156. if (timeout_txt) {
  157. *timeout_txt++ = '\0';
  158. timeout = atoi(timeout_txt);
  159. pos = os_strchr(timeout_txt, ' ');
  160. if (pos) {
  161. *pos++ = '\0';
  162. if (hwaddr_aton(pos, addr_buf) == 0)
  163. addr = addr_buf;
  164. }
  165. } else
  166. timeout = 0;
  167. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  168. }
  169. static int hostapd_ctrl_iface_wps_check_pin(
  170. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  171. {
  172. char pin[9];
  173. size_t len;
  174. char *pos;
  175. int ret;
  176. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  177. (u8 *) cmd, os_strlen(cmd));
  178. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  179. if (*pos < '0' || *pos > '9')
  180. continue;
  181. pin[len++] = *pos;
  182. if (len == 9) {
  183. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  184. return -1;
  185. }
  186. }
  187. if (len != 4 && len != 8) {
  188. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  189. return -1;
  190. }
  191. pin[len] = '\0';
  192. if (len == 8) {
  193. unsigned int pin_val;
  194. pin_val = atoi(pin);
  195. if (!wps_pin_valid(pin_val)) {
  196. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  197. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  198. if (ret < 0 || (size_t) ret >= buflen)
  199. return -1;
  200. return ret;
  201. }
  202. }
  203. ret = os_snprintf(buf, buflen, "%s", pin);
  204. if (ret < 0 || (size_t) ret >= buflen)
  205. return -1;
  206. return ret;
  207. }
  208. #ifdef CONFIG_WPS_NFC
  209. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  210. char *pos)
  211. {
  212. size_t len;
  213. struct wpabuf *buf;
  214. int ret;
  215. len = os_strlen(pos);
  216. if (len & 0x01)
  217. return -1;
  218. len /= 2;
  219. buf = wpabuf_alloc(len);
  220. if (buf == NULL)
  221. return -1;
  222. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  223. wpabuf_free(buf);
  224. return -1;
  225. }
  226. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  227. wpabuf_free(buf);
  228. return ret;
  229. }
  230. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  231. char *cmd, char *reply,
  232. size_t max_len)
  233. {
  234. int ndef;
  235. struct wpabuf *buf;
  236. int res;
  237. if (os_strcmp(cmd, "WPS") == 0)
  238. ndef = 0;
  239. else if (os_strcmp(cmd, "NDEF") == 0)
  240. ndef = 1;
  241. else
  242. return -1;
  243. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  244. if (buf == NULL)
  245. return -1;
  246. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  247. wpabuf_len(buf));
  248. reply[res++] = '\n';
  249. reply[res] = '\0';
  250. wpabuf_free(buf);
  251. return res;
  252. }
  253. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  254. char *reply, size_t max_len,
  255. int ndef)
  256. {
  257. struct wpabuf *buf;
  258. int res;
  259. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  260. if (buf == NULL)
  261. return -1;
  262. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  263. wpabuf_len(buf));
  264. reply[res++] = '\n';
  265. reply[res] = '\0';
  266. wpabuf_free(buf);
  267. return res;
  268. }
  269. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  270. char *cmd, char *reply,
  271. size_t max_len)
  272. {
  273. if (os_strcmp(cmd, "WPS") == 0)
  274. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  275. max_len, 0);
  276. if (os_strcmp(cmd, "NDEF") == 0)
  277. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  278. max_len, 1);
  279. if (os_strcmp(cmd, "enable") == 0)
  280. return hostapd_wps_nfc_token_enable(hapd);
  281. if (os_strcmp(cmd, "disable") == 0) {
  282. hostapd_wps_nfc_token_disable(hapd);
  283. return 0;
  284. }
  285. return -1;
  286. }
  287. #endif /* CONFIG_WPS_NFC */
  288. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  289. char *buf, size_t buflen)
  290. {
  291. int timeout = 300;
  292. char *pos;
  293. const char *pin_txt;
  294. pos = os_strchr(txt, ' ');
  295. if (pos)
  296. *pos++ = '\0';
  297. if (os_strcmp(txt, "disable") == 0) {
  298. hostapd_wps_ap_pin_disable(hapd);
  299. return os_snprintf(buf, buflen, "OK\n");
  300. }
  301. if (os_strcmp(txt, "random") == 0) {
  302. if (pos)
  303. timeout = atoi(pos);
  304. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  305. if (pin_txt == NULL)
  306. return -1;
  307. return os_snprintf(buf, buflen, "%s", pin_txt);
  308. }
  309. if (os_strcmp(txt, "get") == 0) {
  310. pin_txt = hostapd_wps_ap_pin_get(hapd);
  311. if (pin_txt == NULL)
  312. return -1;
  313. return os_snprintf(buf, buflen, "%s", pin_txt);
  314. }
  315. if (os_strcmp(txt, "set") == 0) {
  316. char *pin;
  317. if (pos == NULL)
  318. return -1;
  319. pin = pos;
  320. pos = os_strchr(pos, ' ');
  321. if (pos) {
  322. *pos++ = '\0';
  323. timeout = atoi(pos);
  324. }
  325. if (os_strlen(pin) > buflen)
  326. return -1;
  327. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  328. return -1;
  329. return os_snprintf(buf, buflen, "%s", pin);
  330. }
  331. return -1;
  332. }
  333. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  334. {
  335. char *pos;
  336. char *ssid, *auth, *encr = NULL, *key = NULL;
  337. ssid = txt;
  338. pos = os_strchr(txt, ' ');
  339. if (!pos)
  340. return -1;
  341. *pos++ = '\0';
  342. auth = pos;
  343. pos = os_strchr(pos, ' ');
  344. if (pos) {
  345. *pos++ = '\0';
  346. encr = pos;
  347. pos = os_strchr(pos, ' ');
  348. if (pos) {
  349. *pos++ = '\0';
  350. key = pos;
  351. }
  352. }
  353. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  354. }
  355. #endif /* CONFIG_WPS */
  356. #ifdef CONFIG_WNM
  357. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  358. const char *cmd)
  359. {
  360. u8 addr[ETH_ALEN];
  361. u8 buf[1000], *pos;
  362. struct ieee80211_mgmt *mgmt;
  363. int disassoc_timer;
  364. if (hwaddr_aton(cmd, addr))
  365. return -1;
  366. if (cmd[17] != ' ')
  367. return -1;
  368. disassoc_timer = atoi(cmd + 17);
  369. os_memset(buf, 0, sizeof(buf));
  370. mgmt = (struct ieee80211_mgmt *) buf;
  371. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  372. WLAN_FC_STYPE_ACTION);
  373. os_memcpy(mgmt->da, addr, ETH_ALEN);
  374. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  375. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  376. mgmt->u.action.category = WLAN_ACTION_WNM;
  377. mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
  378. mgmt->u.action.u.bss_tm_req.dialog_token = 1;
  379. mgmt->u.action.u.bss_tm_req.req_mode =
  380. WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
  381. mgmt->u.action.u.bss_tm_req.disassoc_timer =
  382. host_to_le16(disassoc_timer);
  383. mgmt->u.action.u.bss_tm_req.validity_interval = 0;
  384. pos = mgmt->u.action.u.bss_tm_req.variable;
  385. if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
  386. wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
  387. "Management Request frame");
  388. return -1;
  389. }
  390. return 0;
  391. }
  392. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  393. const char *cmd)
  394. {
  395. u8 addr[ETH_ALEN];
  396. const char *url;
  397. u8 buf[1000], *pos;
  398. struct ieee80211_mgmt *mgmt;
  399. size_t url_len;
  400. if (hwaddr_aton(cmd, addr))
  401. return -1;
  402. url = cmd + 17;
  403. if (*url != ' ')
  404. return -1;
  405. url++;
  406. url_len = os_strlen(url);
  407. if (url_len > 255)
  408. return -1;
  409. os_memset(buf, 0, sizeof(buf));
  410. mgmt = (struct ieee80211_mgmt *) buf;
  411. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  412. WLAN_FC_STYPE_ACTION);
  413. os_memcpy(mgmt->da, addr, ETH_ALEN);
  414. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  415. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  416. mgmt->u.action.category = WLAN_ACTION_WNM;
  417. mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
  418. mgmt->u.action.u.bss_tm_req.dialog_token = 1;
  419. mgmt->u.action.u.bss_tm_req.req_mode =
  420. WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
  421. mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
  422. mgmt->u.action.u.bss_tm_req.validity_interval = 0;
  423. pos = mgmt->u.action.u.bss_tm_req.variable;
  424. /* Session Information URL */
  425. *pos++ = url_len;
  426. os_memcpy(pos, url, url_len);
  427. pos += url_len;
  428. if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
  429. wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
  430. "Management Request frame");
  431. return -1;
  432. }
  433. return 0;
  434. }
  435. #endif /* CONFIG_WNM */
  436. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  437. char *buf, size_t buflen)
  438. {
  439. int ret;
  440. char *pos, *end;
  441. pos = buf;
  442. end = buf + buflen;
  443. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  444. "ssid=%s\n",
  445. MAC2STR(hapd->own_addr),
  446. wpa_ssid_txt(hapd->conf->ssid.ssid,
  447. hapd->conf->ssid.ssid_len));
  448. if (ret < 0 || ret >= end - pos)
  449. return pos - buf;
  450. pos += ret;
  451. #ifdef CONFIG_WPS
  452. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  453. hapd->conf->wps_state == 0 ? "disabled" :
  454. (hapd->conf->wps_state == 1 ? "not configured" :
  455. "configured"));
  456. if (ret < 0 || ret >= end - pos)
  457. return pos - buf;
  458. pos += ret;
  459. if (hapd->conf->wps_state && hapd->conf->wpa &&
  460. hapd->conf->ssid.wpa_passphrase) {
  461. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  462. hapd->conf->ssid.wpa_passphrase);
  463. if (ret < 0 || ret >= end - pos)
  464. return pos - buf;
  465. pos += ret;
  466. }
  467. if (hapd->conf->wps_state && hapd->conf->wpa &&
  468. hapd->conf->ssid.wpa_psk &&
  469. hapd->conf->ssid.wpa_psk->group) {
  470. char hex[PMK_LEN * 2 + 1];
  471. wpa_snprintf_hex(hex, sizeof(hex),
  472. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  473. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  474. if (ret < 0 || ret >= end - pos)
  475. return pos - buf;
  476. pos += ret;
  477. }
  478. #endif /* CONFIG_WPS */
  479. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  480. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  481. if (ret < 0 || ret >= end - pos)
  482. return pos - buf;
  483. pos += ret;
  484. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  485. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  486. if (ret < 0 || ret >= end - pos)
  487. return pos - buf;
  488. pos += ret;
  489. }
  490. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  491. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  492. if (ret < 0 || ret >= end - pos)
  493. return pos - buf;
  494. pos += ret;
  495. }
  496. #ifdef CONFIG_IEEE80211R
  497. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  498. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  499. if (ret < 0 || ret >= end - pos)
  500. return pos - buf;
  501. pos += ret;
  502. }
  503. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  504. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  505. if (ret < 0 || ret >= end - pos)
  506. return pos - buf;
  507. pos += ret;
  508. }
  509. #endif /* CONFIG_IEEE80211R */
  510. #ifdef CONFIG_IEEE80211W
  511. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  512. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  513. if (ret < 0 || ret >= end - pos)
  514. return pos - buf;
  515. pos += ret;
  516. }
  517. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  518. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  519. if (ret < 0 || ret >= end - pos)
  520. return pos - buf;
  521. pos += ret;
  522. }
  523. #endif /* CONFIG_IEEE80211W */
  524. ret = os_snprintf(pos, end - pos, "\n");
  525. if (ret < 0 || ret >= end - pos)
  526. return pos - buf;
  527. pos += ret;
  528. }
  529. if (hapd->conf->wpa) {
  530. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  531. wpa_cipher_txt(hapd->conf->wpa_group));
  532. if (ret < 0 || ret >= end - pos)
  533. return pos - buf;
  534. pos += ret;
  535. }
  536. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  537. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  538. if (ret < 0 || ret >= end - pos)
  539. return pos - buf;
  540. pos += ret;
  541. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  542. " ");
  543. if (ret < 0)
  544. return pos - buf;
  545. pos += ret;
  546. ret = os_snprintf(pos, end - pos, "\n");
  547. if (ret < 0 || ret >= end - pos)
  548. return pos - buf;
  549. pos += ret;
  550. }
  551. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  552. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  553. if (ret < 0 || ret >= end - pos)
  554. return pos - buf;
  555. pos += ret;
  556. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  557. " ");
  558. if (ret < 0)
  559. return pos - buf;
  560. pos += ret;
  561. ret = os_snprintf(pos, end - pos, "\n");
  562. if (ret < 0 || ret >= end - pos)
  563. return pos - buf;
  564. pos += ret;
  565. }
  566. return pos - buf;
  567. }
  568. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  569. {
  570. char *value;
  571. int ret = 0;
  572. value = os_strchr(cmd, ' ');
  573. if (value == NULL)
  574. return -1;
  575. *value++ = '\0';
  576. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  577. if (0) {
  578. #ifdef CONFIG_WPS_TESTING
  579. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  580. long int val;
  581. val = strtol(value, NULL, 0);
  582. if (val < 0 || val > 0xff) {
  583. ret = -1;
  584. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  585. "wps_version_number %ld", val);
  586. } else {
  587. wps_version_number = val;
  588. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  589. "version %u.%u",
  590. (wps_version_number & 0xf0) >> 4,
  591. wps_version_number & 0x0f);
  592. hostapd_wps_update_ie(hapd);
  593. }
  594. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  595. wps_testing_dummy_cred = atoi(value);
  596. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  597. wps_testing_dummy_cred);
  598. #endif /* CONFIG_WPS_TESTING */
  599. #ifdef CONFIG_INTERWORKING
  600. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  601. int val = atoi(value);
  602. if (val <= 0)
  603. ret = -1;
  604. else
  605. hapd->gas_frag_limit = val;
  606. #endif /* CONFIG_INTERWORKING */
  607. } else {
  608. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  609. }
  610. return ret;
  611. }
  612. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  613. char *buf, size_t buflen)
  614. {
  615. int res;
  616. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  617. if (os_strcmp(cmd, "version") == 0) {
  618. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  619. if (res < 0 || (unsigned int) res >= buflen)
  620. return -1;
  621. return res;
  622. }
  623. return -1;
  624. }
  625. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  626. {
  627. if (hostapd_enable_iface(iface) < 0) {
  628. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  629. return -1;
  630. }
  631. return 0;
  632. }
  633. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  634. {
  635. if (hostapd_reload_iface(iface) < 0) {
  636. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  637. return -1;
  638. }
  639. return 0;
  640. }
  641. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  642. {
  643. if (hostapd_disable_iface(iface) < 0) {
  644. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  645. return -1;
  646. }
  647. return 0;
  648. }
  649. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  650. void *sock_ctx)
  651. {
  652. struct hostapd_data *hapd = eloop_ctx;
  653. char buf[256];
  654. int res;
  655. struct sockaddr_un from;
  656. socklen_t fromlen = sizeof(from);
  657. char *reply;
  658. const int reply_size = 4096;
  659. int reply_len;
  660. int level = MSG_DEBUG;
  661. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  662. (struct sockaddr *) &from, &fromlen);
  663. if (res < 0) {
  664. perror("recvfrom(ctrl_iface)");
  665. return;
  666. }
  667. buf[res] = '\0';
  668. if (os_strcmp(buf, "PING") == 0)
  669. level = MSG_EXCESSIVE;
  670. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  671. reply = os_malloc(reply_size);
  672. if (reply == NULL) {
  673. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  674. fromlen);
  675. return;
  676. }
  677. os_memcpy(reply, "OK\n", 3);
  678. reply_len = 3;
  679. if (os_strcmp(buf, "PING") == 0) {
  680. os_memcpy(reply, "PONG\n", 5);
  681. reply_len = 5;
  682. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  683. if (wpa_debug_reopen_file() < 0)
  684. reply_len = -1;
  685. } else if (os_strcmp(buf, "MIB") == 0) {
  686. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  687. if (reply_len >= 0) {
  688. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  689. reply_size - reply_len);
  690. if (res < 0)
  691. reply_len = -1;
  692. else
  693. reply_len += res;
  694. }
  695. if (reply_len >= 0) {
  696. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  697. reply_size - reply_len);
  698. if (res < 0)
  699. reply_len = -1;
  700. else
  701. reply_len += res;
  702. }
  703. #ifndef CONFIG_NO_RADIUS
  704. if (reply_len >= 0) {
  705. res = radius_client_get_mib(hapd->radius,
  706. reply + reply_len,
  707. reply_size - reply_len);
  708. if (res < 0)
  709. reply_len = -1;
  710. else
  711. reply_len += res;
  712. }
  713. #endif /* CONFIG_NO_RADIUS */
  714. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  715. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  716. reply_size);
  717. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  718. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  719. reply_size);
  720. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  721. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  722. reply_size);
  723. } else if (os_strcmp(buf, "ATTACH") == 0) {
  724. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  725. reply_len = -1;
  726. } else if (os_strcmp(buf, "DETACH") == 0) {
  727. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  728. reply_len = -1;
  729. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  730. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  731. buf + 6))
  732. reply_len = -1;
  733. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  734. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  735. reply_len = -1;
  736. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  737. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  738. reply_len = -1;
  739. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  740. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  741. reply_len = -1;
  742. #ifdef CONFIG_IEEE80211W
  743. #ifdef NEED_AP_MLME
  744. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  745. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  746. reply_len = -1;
  747. #endif /* NEED_AP_MLME */
  748. #endif /* CONFIG_IEEE80211W */
  749. #ifdef CONFIG_WPS
  750. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  751. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  752. reply_len = -1;
  753. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  754. reply_len = hostapd_ctrl_iface_wps_check_pin(
  755. hapd, buf + 14, reply, reply_size);
  756. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  757. if (hostapd_wps_button_pushed(hapd, NULL))
  758. reply_len = -1;
  759. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  760. if (hostapd_wps_cancel(hapd))
  761. reply_len = -1;
  762. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  763. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  764. reply, reply_size);
  765. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  766. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  767. reply_len = -1;
  768. #ifdef CONFIG_WPS_NFC
  769. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  770. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  771. reply_len = -1;
  772. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  773. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  774. hapd, buf + 21, reply, reply_size);
  775. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  776. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  777. hapd, buf + 14, reply, reply_size);
  778. #endif /* CONFIG_WPS_NFC */
  779. #endif /* CONFIG_WPS */
  780. #ifdef CONFIG_WNM
  781. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  782. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  783. reply_len = -1;
  784. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  785. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  786. reply_len = -1;
  787. #endif /* CONFIG_WNM */
  788. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  789. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  790. reply_size);
  791. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  792. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  793. reply_len = -1;
  794. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  795. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  796. reply_size);
  797. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  798. if (hostapd_ctrl_iface_enable(hapd->iface))
  799. reply_len = -1;
  800. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  801. if (hostapd_ctrl_iface_reload(hapd->iface))
  802. reply_len = -1;
  803. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  804. if (hostapd_ctrl_iface_disable(hapd->iface))
  805. reply_len = -1;
  806. } else {
  807. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  808. reply_len = 16;
  809. }
  810. if (reply_len < 0) {
  811. os_memcpy(reply, "FAIL\n", 5);
  812. reply_len = 5;
  813. }
  814. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  815. os_free(reply);
  816. }
  817. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  818. {
  819. char *buf;
  820. size_t len;
  821. if (hapd->conf->ctrl_interface == NULL)
  822. return NULL;
  823. len = os_strlen(hapd->conf->ctrl_interface) +
  824. os_strlen(hapd->conf->iface) + 2;
  825. buf = os_malloc(len);
  826. if (buf == NULL)
  827. return NULL;
  828. os_snprintf(buf, len, "%s/%s",
  829. hapd->conf->ctrl_interface, hapd->conf->iface);
  830. buf[len - 1] = '\0';
  831. return buf;
  832. }
  833. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  834. const char *txt, size_t len)
  835. {
  836. struct hostapd_data *hapd = ctx;
  837. if (hapd == NULL)
  838. return;
  839. hostapd_ctrl_iface_send(hapd, level, txt, len);
  840. }
  841. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  842. {
  843. struct sockaddr_un addr;
  844. int s = -1;
  845. char *fname = NULL;
  846. if (hapd->ctrl_sock > -1) {
  847. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  848. return 0;
  849. }
  850. if (hapd->conf->ctrl_interface == NULL)
  851. return 0;
  852. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  853. if (errno == EEXIST) {
  854. wpa_printf(MSG_DEBUG, "Using existing control "
  855. "interface directory.");
  856. } else {
  857. perror("mkdir[ctrl_interface]");
  858. goto fail;
  859. }
  860. }
  861. if (hapd->conf->ctrl_interface_gid_set &&
  862. chown(hapd->conf->ctrl_interface, -1,
  863. hapd->conf->ctrl_interface_gid) < 0) {
  864. perror("chown[ctrl_interface]");
  865. return -1;
  866. }
  867. #ifdef ANDROID
  868. /*
  869. * Android is using umask 0077 which would leave the control interface
  870. * directory without group access. This breaks things since Wi-Fi
  871. * framework assumes that this directory can be accessed by other
  872. * applications in the wifi group. Fix this by adding group access even
  873. * if umask value would prevent this.
  874. */
  875. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  876. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  877. strerror(errno));
  878. /* Try to continue anyway */
  879. }
  880. #endif /* ANDROID */
  881. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  882. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  883. goto fail;
  884. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  885. if (s < 0) {
  886. perror("socket(PF_UNIX)");
  887. goto fail;
  888. }
  889. os_memset(&addr, 0, sizeof(addr));
  890. #ifdef __FreeBSD__
  891. addr.sun_len = sizeof(addr);
  892. #endif /* __FreeBSD__ */
  893. addr.sun_family = AF_UNIX;
  894. fname = hostapd_ctrl_iface_path(hapd);
  895. if (fname == NULL)
  896. goto fail;
  897. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  898. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  899. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  900. strerror(errno));
  901. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  902. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  903. " allow connections - assuming it was left"
  904. "over from forced program termination");
  905. if (unlink(fname) < 0) {
  906. perror("unlink[ctrl_iface]");
  907. wpa_printf(MSG_ERROR, "Could not unlink "
  908. "existing ctrl_iface socket '%s'",
  909. fname);
  910. goto fail;
  911. }
  912. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  913. 0) {
  914. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  915. goto fail;
  916. }
  917. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  918. "ctrl_iface socket '%s'", fname);
  919. } else {
  920. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  921. "be in use - cannot override it");
  922. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  923. "not used anymore", fname);
  924. os_free(fname);
  925. fname = NULL;
  926. goto fail;
  927. }
  928. }
  929. if (hapd->conf->ctrl_interface_gid_set &&
  930. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  931. perror("chown[ctrl_interface/ifname]");
  932. goto fail;
  933. }
  934. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  935. perror("chmod[ctrl_interface/ifname]");
  936. goto fail;
  937. }
  938. os_free(fname);
  939. hapd->ctrl_sock = s;
  940. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  941. NULL);
  942. hapd->msg_ctx = hapd;
  943. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  944. return 0;
  945. fail:
  946. if (s >= 0)
  947. close(s);
  948. if (fname) {
  949. unlink(fname);
  950. os_free(fname);
  951. }
  952. return -1;
  953. }
  954. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  955. {
  956. struct wpa_ctrl_dst *dst, *prev;
  957. if (hapd->ctrl_sock > -1) {
  958. char *fname;
  959. eloop_unregister_read_sock(hapd->ctrl_sock);
  960. close(hapd->ctrl_sock);
  961. hapd->ctrl_sock = -1;
  962. fname = hostapd_ctrl_iface_path(hapd);
  963. if (fname)
  964. unlink(fname);
  965. os_free(fname);
  966. if (hapd->conf->ctrl_interface &&
  967. rmdir(hapd->conf->ctrl_interface) < 0) {
  968. if (errno == ENOTEMPTY) {
  969. wpa_printf(MSG_DEBUG, "Control interface "
  970. "directory not empty - leaving it "
  971. "behind");
  972. } else {
  973. perror("rmdir[ctrl_interface]");
  974. }
  975. }
  976. }
  977. dst = hapd->ctrl_dst;
  978. while (dst) {
  979. prev = dst;
  980. dst = dst->next;
  981. os_free(prev);
  982. }
  983. }
  984. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  985. char *buf)
  986. {
  987. if (hostapd_add_iface(interfaces, buf) < 0) {
  988. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  989. return -1;
  990. }
  991. return 0;
  992. }
  993. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  994. char *buf)
  995. {
  996. if (hostapd_remove_iface(interfaces, buf) < 0) {
  997. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  998. return -1;
  999. }
  1000. return 0;
  1001. }
  1002. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1003. void *sock_ctx)
  1004. {
  1005. void *interfaces = eloop_ctx;
  1006. char buf[256];
  1007. int res;
  1008. struct sockaddr_un from;
  1009. socklen_t fromlen = sizeof(from);
  1010. char reply[24];
  1011. int reply_len;
  1012. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1013. (struct sockaddr *) &from, &fromlen);
  1014. if (res < 0) {
  1015. perror("recvfrom(ctrl_iface)");
  1016. return;
  1017. }
  1018. buf[res] = '\0';
  1019. os_memcpy(reply, "OK\n", 3);
  1020. reply_len = 3;
  1021. if (os_strcmp(buf, "PING") == 0) {
  1022. os_memcpy(reply, "PONG\n", 5);
  1023. reply_len = 5;
  1024. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1025. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1026. reply_len = -1;
  1027. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1028. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1029. reply_len = -1;
  1030. } else {
  1031. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1032. "ignored");
  1033. reply_len = -1;
  1034. }
  1035. if (reply_len < 0) {
  1036. os_memcpy(reply, "FAIL\n", 5);
  1037. reply_len = 5;
  1038. }
  1039. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1040. }
  1041. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1042. {
  1043. char *buf;
  1044. size_t len;
  1045. if (interface->global_iface_path == NULL)
  1046. return NULL;
  1047. len = os_strlen(interface->global_iface_path) +
  1048. os_strlen(interface->global_iface_name) + 2;
  1049. buf = os_malloc(len);
  1050. if (buf == NULL)
  1051. return NULL;
  1052. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1053. interface->global_iface_name);
  1054. buf[len - 1] = '\0';
  1055. return buf;
  1056. }
  1057. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1058. {
  1059. struct sockaddr_un addr;
  1060. int s = -1;
  1061. char *fname = NULL;
  1062. if (interface->global_iface_path == NULL) {
  1063. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1064. return 0;
  1065. }
  1066. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1067. if (errno == EEXIST) {
  1068. wpa_printf(MSG_DEBUG, "Using existing control "
  1069. "interface directory.");
  1070. } else {
  1071. perror("mkdir[ctrl_interface]");
  1072. goto fail;
  1073. }
  1074. }
  1075. if (os_strlen(interface->global_iface_path) + 1 +
  1076. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1077. goto fail;
  1078. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1079. if (s < 0) {
  1080. perror("socket(PF_UNIX)");
  1081. goto fail;
  1082. }
  1083. os_memset(&addr, 0, sizeof(addr));
  1084. #ifdef __FreeBSD__
  1085. addr.sun_len = sizeof(addr);
  1086. #endif /* __FreeBSD__ */
  1087. addr.sun_family = AF_UNIX;
  1088. fname = hostapd_global_ctrl_iface_path(interface);
  1089. if (fname == NULL)
  1090. goto fail;
  1091. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1092. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1093. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1094. strerror(errno));
  1095. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1096. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1097. " allow connections - assuming it was left"
  1098. "over from forced program termination");
  1099. if (unlink(fname) < 0) {
  1100. perror("unlink[ctrl_iface]");
  1101. wpa_printf(MSG_ERROR, "Could not unlink "
  1102. "existing ctrl_iface socket '%s'",
  1103. fname);
  1104. goto fail;
  1105. }
  1106. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1107. 0) {
  1108. perror("bind(PF_UNIX)");
  1109. goto fail;
  1110. }
  1111. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1112. "ctrl_iface socket '%s'", fname);
  1113. } else {
  1114. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1115. "be in use - cannot override it");
  1116. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1117. "not used anymore", fname);
  1118. os_free(fname);
  1119. fname = NULL;
  1120. goto fail;
  1121. }
  1122. }
  1123. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1124. perror("chmod[ctrl_interface/ifname]");
  1125. goto fail;
  1126. }
  1127. os_free(fname);
  1128. interface->global_ctrl_sock = s;
  1129. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1130. interface, NULL);
  1131. return 0;
  1132. fail:
  1133. if (s >= 0)
  1134. close(s);
  1135. if (fname) {
  1136. unlink(fname);
  1137. os_free(fname);
  1138. }
  1139. return -1;
  1140. }
  1141. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1142. {
  1143. char *fname = NULL;
  1144. if (interfaces->global_ctrl_sock > -1) {
  1145. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1146. close(interfaces->global_ctrl_sock);
  1147. interfaces->global_ctrl_sock = -1;
  1148. fname = hostapd_global_ctrl_iface_path(interfaces);
  1149. if (fname) {
  1150. unlink(fname);
  1151. os_free(fname);
  1152. }
  1153. if (interfaces->global_iface_path &&
  1154. rmdir(interfaces->global_iface_path) < 0) {
  1155. if (errno == ENOTEMPTY) {
  1156. wpa_printf(MSG_DEBUG, "Control interface "
  1157. "directory not empty - leaving it "
  1158. "behind");
  1159. } else {
  1160. perror("rmdir[ctrl_interface]");
  1161. }
  1162. }
  1163. os_free(interfaces->global_iface_path);
  1164. interfaces->global_iface_path = NULL;
  1165. }
  1166. }
  1167. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1168. const char *buf, size_t len)
  1169. {
  1170. struct wpa_ctrl_dst *dst, *next;
  1171. struct msghdr msg;
  1172. int idx;
  1173. struct iovec io[2];
  1174. char levelstr[10];
  1175. dst = hapd->ctrl_dst;
  1176. if (hapd->ctrl_sock < 0 || dst == NULL)
  1177. return;
  1178. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1179. io[0].iov_base = levelstr;
  1180. io[0].iov_len = os_strlen(levelstr);
  1181. io[1].iov_base = (char *) buf;
  1182. io[1].iov_len = len;
  1183. os_memset(&msg, 0, sizeof(msg));
  1184. msg.msg_iov = io;
  1185. msg.msg_iovlen = 2;
  1186. idx = 0;
  1187. while (dst) {
  1188. next = dst->next;
  1189. if (level >= dst->debug_level) {
  1190. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1191. (u8 *) dst->addr.sun_path, dst->addrlen -
  1192. offsetof(struct sockaddr_un, sun_path));
  1193. msg.msg_name = &dst->addr;
  1194. msg.msg_namelen = dst->addrlen;
  1195. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1196. int _errno = errno;
  1197. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1198. "%d - %s",
  1199. idx, errno, strerror(errno));
  1200. dst->errors++;
  1201. if (dst->errors > 10 || _errno == ENOENT) {
  1202. hostapd_ctrl_iface_detach(
  1203. hapd, &dst->addr,
  1204. dst->addrlen);
  1205. }
  1206. } else
  1207. dst->errors = 0;
  1208. }
  1209. idx++;
  1210. dst = next;
  1211. }
  1212. }
  1213. #endif /* CONFIG_NATIVE_WINDOWS */