ctrl_iface.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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_OOB
  209. static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
  210. {
  211. char *path, *method, *name;
  212. path = os_strchr(txt, ' ');
  213. if (path == NULL)
  214. return -1;
  215. *path++ = '\0';
  216. method = os_strchr(path, ' ');
  217. if (method == NULL)
  218. return -1;
  219. *method++ = '\0';
  220. name = os_strchr(method, ' ');
  221. if (name != NULL)
  222. *name++ = '\0';
  223. return hostapd_wps_start_oob(hapd, txt, path, method, name);
  224. }
  225. #endif /* CONFIG_WPS_OOB */
  226. #ifdef CONFIG_WPS_NFC
  227. static int hostapd_ctrl_iface_wps_nfc_tag_read(struct hostapd_data *hapd,
  228. char *pos)
  229. {
  230. size_t len;
  231. struct wpabuf *buf;
  232. int ret;
  233. len = os_strlen(pos);
  234. if (len & 0x01)
  235. return -1;
  236. len /= 2;
  237. buf = wpabuf_alloc(len);
  238. if (buf == NULL)
  239. return -1;
  240. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  241. wpabuf_free(buf);
  242. return -1;
  243. }
  244. ret = hostapd_wps_nfc_tag_read(hapd, buf);
  245. wpabuf_free(buf);
  246. return ret;
  247. }
  248. static int hostapd_ctrl_iface_wps_nfc_config_token(struct hostapd_data *hapd,
  249. char *cmd, char *reply,
  250. size_t max_len)
  251. {
  252. int ndef;
  253. struct wpabuf *buf;
  254. int res;
  255. if (os_strcmp(cmd, "WPS") == 0)
  256. ndef = 0;
  257. else if (os_strcmp(cmd, "NDEF") == 0)
  258. ndef = 1;
  259. else
  260. return -1;
  261. buf = hostapd_wps_nfc_config_token(hapd, ndef);
  262. if (buf == NULL)
  263. return -1;
  264. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  265. wpabuf_len(buf));
  266. reply[res++] = '\n';
  267. reply[res] = '\0';
  268. wpabuf_free(buf);
  269. return res;
  270. }
  271. static int hostapd_ctrl_iface_wps_nfc_token_gen(struct hostapd_data *hapd,
  272. char *reply, size_t max_len,
  273. int ndef)
  274. {
  275. struct wpabuf *buf;
  276. int res;
  277. buf = hostapd_wps_nfc_token_gen(hapd, ndef);
  278. if (buf == NULL)
  279. return -1;
  280. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  281. wpabuf_len(buf));
  282. reply[res++] = '\n';
  283. reply[res] = '\0';
  284. wpabuf_free(buf);
  285. return res;
  286. }
  287. static int hostapd_ctrl_iface_wps_nfc_token(struct hostapd_data *hapd,
  288. char *cmd, char *reply,
  289. size_t max_len)
  290. {
  291. if (os_strcmp(cmd, "WPS") == 0)
  292. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  293. max_len, 0);
  294. if (os_strcmp(cmd, "NDEF") == 0)
  295. return hostapd_ctrl_iface_wps_nfc_token_gen(hapd, reply,
  296. max_len, 1);
  297. if (os_strcmp(cmd, "enable") == 0)
  298. return hostapd_wps_nfc_token_enable(hapd);
  299. if (os_strcmp(cmd, "disable") == 0) {
  300. hostapd_wps_nfc_token_disable(hapd);
  301. return 0;
  302. }
  303. return -1;
  304. }
  305. #endif /* CONFIG_WPS_NFC */
  306. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  307. char *buf, size_t buflen)
  308. {
  309. int timeout = 300;
  310. char *pos;
  311. const char *pin_txt;
  312. pos = os_strchr(txt, ' ');
  313. if (pos)
  314. *pos++ = '\0';
  315. if (os_strcmp(txt, "disable") == 0) {
  316. hostapd_wps_ap_pin_disable(hapd);
  317. return os_snprintf(buf, buflen, "OK\n");
  318. }
  319. if (os_strcmp(txt, "random") == 0) {
  320. if (pos)
  321. timeout = atoi(pos);
  322. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  323. if (pin_txt == NULL)
  324. return -1;
  325. return os_snprintf(buf, buflen, "%s", pin_txt);
  326. }
  327. if (os_strcmp(txt, "get") == 0) {
  328. pin_txt = hostapd_wps_ap_pin_get(hapd);
  329. if (pin_txt == NULL)
  330. return -1;
  331. return os_snprintf(buf, buflen, "%s", pin_txt);
  332. }
  333. if (os_strcmp(txt, "set") == 0) {
  334. char *pin;
  335. if (pos == NULL)
  336. return -1;
  337. pin = pos;
  338. pos = os_strchr(pos, ' ');
  339. if (pos) {
  340. *pos++ = '\0';
  341. timeout = atoi(pos);
  342. }
  343. if (os_strlen(pin) > buflen)
  344. return -1;
  345. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  346. return -1;
  347. return os_snprintf(buf, buflen, "%s", pin);
  348. }
  349. return -1;
  350. }
  351. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  352. {
  353. char *pos;
  354. char *ssid, *auth, *encr = NULL, *key = NULL;
  355. ssid = txt;
  356. pos = os_strchr(txt, ' ');
  357. if (!pos)
  358. return -1;
  359. *pos++ = '\0';
  360. auth = pos;
  361. pos = os_strchr(pos, ' ');
  362. if (pos) {
  363. *pos++ = '\0';
  364. encr = pos;
  365. pos = os_strchr(pos, ' ');
  366. if (pos) {
  367. *pos++ = '\0';
  368. key = pos;
  369. }
  370. }
  371. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  372. }
  373. #endif /* CONFIG_WPS */
  374. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  375. const char *cmd)
  376. {
  377. u8 addr[ETH_ALEN];
  378. const char *url;
  379. u8 buf[1000], *pos;
  380. struct ieee80211_mgmt *mgmt;
  381. size_t url_len;
  382. if (hwaddr_aton(cmd, addr))
  383. return -1;
  384. url = cmd + 17;
  385. if (*url != ' ')
  386. return -1;
  387. url++;
  388. url_len = os_strlen(url);
  389. if (url_len > 255)
  390. return -1;
  391. os_memset(buf, 0, sizeof(buf));
  392. mgmt = (struct ieee80211_mgmt *) buf;
  393. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  394. WLAN_FC_STYPE_ACTION);
  395. os_memcpy(mgmt->da, addr, ETH_ALEN);
  396. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  397. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  398. mgmt->u.action.category = WLAN_ACTION_WNM;
  399. mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
  400. mgmt->u.action.u.bss_tm_req.dialog_token = 1;
  401. mgmt->u.action.u.bss_tm_req.req_mode =
  402. WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
  403. mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
  404. mgmt->u.action.u.bss_tm_req.validity_interval = 0;
  405. pos = mgmt->u.action.u.bss_tm_req.variable;
  406. /* Session Information URL */
  407. *pos++ = url_len;
  408. os_memcpy(pos, url, url_len);
  409. pos += url_len;
  410. if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
  411. wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
  412. "Management Request frame");
  413. return -1;
  414. }
  415. return 0;
  416. }
  417. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  418. char *buf, size_t buflen)
  419. {
  420. int ret;
  421. char *pos, *end;
  422. pos = buf;
  423. end = buf + buflen;
  424. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  425. "ssid=%s\n",
  426. MAC2STR(hapd->own_addr),
  427. wpa_ssid_txt(hapd->conf->ssid.ssid,
  428. hapd->conf->ssid.ssid_len));
  429. if (ret < 0 || ret >= end - pos)
  430. return pos - buf;
  431. pos += ret;
  432. #ifdef CONFIG_WPS
  433. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  434. hapd->conf->wps_state == 0 ? "disabled" :
  435. (hapd->conf->wps_state == 1 ? "not configured" :
  436. "configured"));
  437. if (ret < 0 || ret >= end - pos)
  438. return pos - buf;
  439. pos += ret;
  440. if (hapd->conf->wps_state && hapd->conf->wpa &&
  441. hapd->conf->ssid.wpa_passphrase) {
  442. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  443. hapd->conf->ssid.wpa_passphrase);
  444. if (ret < 0 || ret >= end - pos)
  445. return pos - buf;
  446. pos += ret;
  447. }
  448. if (hapd->conf->wps_state && hapd->conf->wpa &&
  449. hapd->conf->ssid.wpa_psk &&
  450. hapd->conf->ssid.wpa_psk->group) {
  451. char hex[PMK_LEN * 2 + 1];
  452. wpa_snprintf_hex(hex, sizeof(hex),
  453. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  454. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  455. if (ret < 0 || ret >= end - pos)
  456. return pos - buf;
  457. pos += ret;
  458. }
  459. #endif /* CONFIG_WPS */
  460. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  461. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  462. if (ret < 0 || ret >= end - pos)
  463. return pos - buf;
  464. pos += ret;
  465. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  466. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  467. if (ret < 0 || ret >= end - pos)
  468. return pos - buf;
  469. pos += ret;
  470. }
  471. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  472. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  473. if (ret < 0 || ret >= end - pos)
  474. return pos - buf;
  475. pos += ret;
  476. }
  477. #ifdef CONFIG_IEEE80211R
  478. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  479. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  480. if (ret < 0 || ret >= end - pos)
  481. return pos - buf;
  482. pos += ret;
  483. }
  484. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  485. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  486. if (ret < 0 || ret >= end - pos)
  487. return pos - buf;
  488. pos += ret;
  489. }
  490. #endif /* CONFIG_IEEE80211R */
  491. #ifdef CONFIG_IEEE80211W
  492. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  493. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  494. if (ret < 0 || ret >= end - pos)
  495. return pos - buf;
  496. pos += ret;
  497. }
  498. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  499. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  500. if (ret < 0 || ret >= end - pos)
  501. return pos - buf;
  502. pos += ret;
  503. }
  504. #endif /* CONFIG_IEEE80211W */
  505. ret = os_snprintf(pos, end - pos, "\n");
  506. if (ret < 0 || ret >= end - pos)
  507. return pos - buf;
  508. pos += ret;
  509. }
  510. if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
  511. ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n");
  512. if (ret < 0 || ret >= end - pos)
  513. return pos - buf;
  514. pos += ret;
  515. } else if (hapd->conf->wpa &&
  516. hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
  517. ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
  518. if (ret < 0 || ret >= end - pos)
  519. return pos - buf;
  520. pos += ret;
  521. }
  522. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  523. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  524. if (ret < 0 || ret >= end - pos)
  525. return pos - buf;
  526. pos += ret;
  527. if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
  528. ret = os_snprintf(pos, end - pos, "CCMP ");
  529. if (ret < 0 || ret >= end - pos)
  530. return pos - buf;
  531. pos += ret;
  532. }
  533. if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
  534. ret = os_snprintf(pos, end - pos, "TKIP ");
  535. if (ret < 0 || ret >= end - pos)
  536. return pos - buf;
  537. pos += ret;
  538. }
  539. ret = os_snprintf(pos, end - pos, "\n");
  540. if (ret < 0 || ret >= end - pos)
  541. return pos - buf;
  542. pos += ret;
  543. }
  544. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  545. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  546. if (ret < 0 || ret >= end - pos)
  547. return pos - buf;
  548. pos += ret;
  549. if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
  550. ret = os_snprintf(pos, end - pos, "CCMP ");
  551. if (ret < 0 || ret >= end - pos)
  552. return pos - buf;
  553. pos += ret;
  554. }
  555. if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
  556. ret = os_snprintf(pos, end - pos, "TKIP ");
  557. if (ret < 0 || ret >= end - pos)
  558. return pos - buf;
  559. pos += ret;
  560. }
  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. #ifdef CONFIG_WPS_OOB
  763. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  764. if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
  765. reply_len = -1;
  766. #endif /* CONFIG_WPS_OOB */
  767. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  768. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  769. reply, reply_size);
  770. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  771. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  772. reply_len = -1;
  773. #ifdef CONFIG_WPS_NFC
  774. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  775. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  776. reply_len = -1;
  777. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  778. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  779. hapd, buf + 21, reply, reply_size);
  780. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  781. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  782. hapd, buf + 14, reply, reply_size);
  783. #endif /* CONFIG_WPS_NFC */
  784. #endif /* CONFIG_WPS */
  785. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  786. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  787. reply_len = -1;
  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, 0,
  863. hapd->conf->ctrl_interface_gid) < 0) {
  864. perror("chown[ctrl_interface]");
  865. return -1;
  866. }
  867. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  868. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  869. goto fail;
  870. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  871. if (s < 0) {
  872. perror("socket(PF_UNIX)");
  873. goto fail;
  874. }
  875. os_memset(&addr, 0, sizeof(addr));
  876. #ifdef __FreeBSD__
  877. addr.sun_len = sizeof(addr);
  878. #endif /* __FreeBSD__ */
  879. addr.sun_family = AF_UNIX;
  880. fname = hostapd_ctrl_iface_path(hapd);
  881. if (fname == NULL)
  882. goto fail;
  883. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  884. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  885. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  886. strerror(errno));
  887. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  888. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  889. " allow connections - assuming it was left"
  890. "over from forced program termination");
  891. if (unlink(fname) < 0) {
  892. perror("unlink[ctrl_iface]");
  893. wpa_printf(MSG_ERROR, "Could not unlink "
  894. "existing ctrl_iface socket '%s'",
  895. fname);
  896. goto fail;
  897. }
  898. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  899. 0) {
  900. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  901. goto fail;
  902. }
  903. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  904. "ctrl_iface socket '%s'", fname);
  905. } else {
  906. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  907. "be in use - cannot override it");
  908. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  909. "not used anymore", fname);
  910. os_free(fname);
  911. fname = NULL;
  912. goto fail;
  913. }
  914. }
  915. if (hapd->conf->ctrl_interface_gid_set &&
  916. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  917. perror("chown[ctrl_interface/ifname]");
  918. goto fail;
  919. }
  920. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  921. perror("chmod[ctrl_interface/ifname]");
  922. goto fail;
  923. }
  924. os_free(fname);
  925. hapd->ctrl_sock = s;
  926. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  927. NULL);
  928. hapd->msg_ctx = hapd;
  929. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  930. return 0;
  931. fail:
  932. if (s >= 0)
  933. close(s);
  934. if (fname) {
  935. unlink(fname);
  936. os_free(fname);
  937. }
  938. return -1;
  939. }
  940. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  941. {
  942. struct wpa_ctrl_dst *dst, *prev;
  943. if (hapd->ctrl_sock > -1) {
  944. char *fname;
  945. eloop_unregister_read_sock(hapd->ctrl_sock);
  946. close(hapd->ctrl_sock);
  947. hapd->ctrl_sock = -1;
  948. fname = hostapd_ctrl_iface_path(hapd);
  949. if (fname)
  950. unlink(fname);
  951. os_free(fname);
  952. if (hapd->conf->ctrl_interface &&
  953. rmdir(hapd->conf->ctrl_interface) < 0) {
  954. if (errno == ENOTEMPTY) {
  955. wpa_printf(MSG_DEBUG, "Control interface "
  956. "directory not empty - leaving it "
  957. "behind");
  958. } else {
  959. perror("rmdir[ctrl_interface]");
  960. }
  961. }
  962. }
  963. dst = hapd->ctrl_dst;
  964. while (dst) {
  965. prev = dst;
  966. dst = dst->next;
  967. os_free(prev);
  968. }
  969. }
  970. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  971. char *buf)
  972. {
  973. if (hostapd_add_iface(interfaces, buf) < 0) {
  974. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  975. return -1;
  976. }
  977. return 0;
  978. }
  979. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  980. char *buf)
  981. {
  982. if (hostapd_remove_iface(interfaces, buf) < 0) {
  983. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  984. return -1;
  985. }
  986. return 0;
  987. }
  988. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  989. void *sock_ctx)
  990. {
  991. void *interfaces = eloop_ctx;
  992. char buf[256];
  993. int res;
  994. struct sockaddr_un from;
  995. socklen_t fromlen = sizeof(from);
  996. char reply[24];
  997. int reply_len;
  998. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  999. (struct sockaddr *) &from, &fromlen);
  1000. if (res < 0) {
  1001. perror("recvfrom(ctrl_iface)");
  1002. return;
  1003. }
  1004. buf[res] = '\0';
  1005. os_memcpy(reply, "OK\n", 3);
  1006. reply_len = 3;
  1007. if (os_strcmp(buf, "PING") == 0) {
  1008. os_memcpy(reply, "PONG\n", 5);
  1009. reply_len = 5;
  1010. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1011. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1012. reply_len = -1;
  1013. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1014. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1015. reply_len = -1;
  1016. } else {
  1017. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1018. "ignored");
  1019. reply_len = -1;
  1020. }
  1021. if (reply_len < 0) {
  1022. os_memcpy(reply, "FAIL\n", 5);
  1023. reply_len = 5;
  1024. }
  1025. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1026. }
  1027. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1028. {
  1029. char *buf;
  1030. size_t len;
  1031. if (interface->global_iface_path == NULL)
  1032. return NULL;
  1033. len = os_strlen(interface->global_iface_path) +
  1034. os_strlen(interface->global_iface_name) + 2;
  1035. buf = os_malloc(len);
  1036. if (buf == NULL)
  1037. return NULL;
  1038. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1039. interface->global_iface_name);
  1040. buf[len - 1] = '\0';
  1041. return buf;
  1042. }
  1043. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1044. {
  1045. struct sockaddr_un addr;
  1046. int s = -1;
  1047. char *fname = NULL;
  1048. if (interface->global_iface_path == NULL) {
  1049. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1050. return 0;
  1051. }
  1052. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1053. if (errno == EEXIST) {
  1054. wpa_printf(MSG_DEBUG, "Using existing control "
  1055. "interface directory.");
  1056. } else {
  1057. perror("mkdir[ctrl_interface]");
  1058. goto fail;
  1059. }
  1060. }
  1061. if (os_strlen(interface->global_iface_path) + 1 +
  1062. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1063. goto fail;
  1064. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1065. if (s < 0) {
  1066. perror("socket(PF_UNIX)");
  1067. goto fail;
  1068. }
  1069. os_memset(&addr, 0, sizeof(addr));
  1070. #ifdef __FreeBSD__
  1071. addr.sun_len = sizeof(addr);
  1072. #endif /* __FreeBSD__ */
  1073. addr.sun_family = AF_UNIX;
  1074. fname = hostapd_global_ctrl_iface_path(interface);
  1075. if (fname == NULL)
  1076. goto fail;
  1077. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1078. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1079. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1080. strerror(errno));
  1081. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1082. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1083. " allow connections - assuming it was left"
  1084. "over from forced program termination");
  1085. if (unlink(fname) < 0) {
  1086. perror("unlink[ctrl_iface]");
  1087. wpa_printf(MSG_ERROR, "Could not unlink "
  1088. "existing ctrl_iface socket '%s'",
  1089. fname);
  1090. goto fail;
  1091. }
  1092. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1093. 0) {
  1094. perror("bind(PF_UNIX)");
  1095. goto fail;
  1096. }
  1097. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1098. "ctrl_iface socket '%s'", fname);
  1099. } else {
  1100. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1101. "be in use - cannot override it");
  1102. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1103. "not used anymore", fname);
  1104. os_free(fname);
  1105. fname = NULL;
  1106. goto fail;
  1107. }
  1108. }
  1109. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1110. perror("chmod[ctrl_interface/ifname]");
  1111. goto fail;
  1112. }
  1113. os_free(fname);
  1114. interface->global_ctrl_sock = s;
  1115. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1116. interface, NULL);
  1117. return 0;
  1118. fail:
  1119. if (s >= 0)
  1120. close(s);
  1121. if (fname) {
  1122. unlink(fname);
  1123. os_free(fname);
  1124. }
  1125. return -1;
  1126. }
  1127. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1128. {
  1129. char *fname = NULL;
  1130. if (interfaces->global_ctrl_sock > -1) {
  1131. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1132. close(interfaces->global_ctrl_sock);
  1133. interfaces->global_ctrl_sock = -1;
  1134. fname = hostapd_global_ctrl_iface_path(interfaces);
  1135. if (fname) {
  1136. unlink(fname);
  1137. os_free(fname);
  1138. }
  1139. if (interfaces->global_iface_path &&
  1140. rmdir(interfaces->global_iface_path) < 0) {
  1141. if (errno == ENOTEMPTY) {
  1142. wpa_printf(MSG_DEBUG, "Control interface "
  1143. "directory not empty - leaving it "
  1144. "behind");
  1145. } else {
  1146. perror("rmdir[ctrl_interface]");
  1147. }
  1148. }
  1149. os_free(interfaces->global_iface_path);
  1150. interfaces->global_iface_path = NULL;
  1151. }
  1152. }
  1153. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1154. const char *buf, size_t len)
  1155. {
  1156. struct wpa_ctrl_dst *dst, *next;
  1157. struct msghdr msg;
  1158. int idx;
  1159. struct iovec io[2];
  1160. char levelstr[10];
  1161. dst = hapd->ctrl_dst;
  1162. if (hapd->ctrl_sock < 0 || dst == NULL)
  1163. return;
  1164. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1165. io[0].iov_base = levelstr;
  1166. io[0].iov_len = os_strlen(levelstr);
  1167. io[1].iov_base = (char *) buf;
  1168. io[1].iov_len = len;
  1169. os_memset(&msg, 0, sizeof(msg));
  1170. msg.msg_iov = io;
  1171. msg.msg_iovlen = 2;
  1172. idx = 0;
  1173. while (dst) {
  1174. next = dst->next;
  1175. if (level >= dst->debug_level) {
  1176. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1177. (u8 *) dst->addr.sun_path, dst->addrlen -
  1178. offsetof(struct sockaddr_un, sun_path));
  1179. msg.msg_name = &dst->addr;
  1180. msg.msg_namelen = dst->addrlen;
  1181. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1182. int _errno = errno;
  1183. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1184. "%d - %s",
  1185. idx, errno, strerror(errno));
  1186. dst->errors++;
  1187. if (dst->errors > 10 || _errno == ENOENT) {
  1188. hostapd_ctrl_iface_detach(
  1189. hapd, &dst->addr,
  1190. dst->addrlen);
  1191. }
  1192. } else
  1193. dst->errors = 0;
  1194. }
  1195. idx++;
  1196. dst = next;
  1197. }
  1198. }
  1199. #endif /* CONFIG_NATIVE_WINDOWS */