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_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_GCMP) {
  517. ret = os_snprintf(pos, end - pos, "group_cipher=GCMP\n");
  518. if (ret < 0 || ret >= end - pos)
  519. return pos - buf;
  520. pos += ret;
  521. } else if (hapd->conf->wpa &&
  522. hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
  523. ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
  524. if (ret < 0 || ret >= end - pos)
  525. return pos - buf;
  526. pos += ret;
  527. }
  528. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  529. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  530. if (ret < 0 || ret >= end - pos)
  531. return pos - buf;
  532. pos += ret;
  533. if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
  534. ret = os_snprintf(pos, end - pos, "CCMP ");
  535. if (ret < 0 || ret >= end - pos)
  536. return pos - buf;
  537. pos += ret;
  538. }
  539. if (hapd->conf->rsn_pairwise & WPA_CIPHER_GCMP) {
  540. ret = os_snprintf(pos, end - pos, "GCMP ");
  541. if (ret < 0 || ret >= end - pos)
  542. return pos - buf;
  543. pos += ret;
  544. }
  545. if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
  546. ret = os_snprintf(pos, end - pos, "TKIP ");
  547. if (ret < 0 || ret >= end - pos)
  548. return pos - buf;
  549. pos += ret;
  550. }
  551. ret = os_snprintf(pos, end - pos, "\n");
  552. if (ret < 0 || ret >= end - pos)
  553. return pos - buf;
  554. pos += ret;
  555. }
  556. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  557. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  558. if (ret < 0 || ret >= end - pos)
  559. return pos - buf;
  560. pos += ret;
  561. if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
  562. ret = os_snprintf(pos, end - pos, "CCMP ");
  563. if (ret < 0 || ret >= end - pos)
  564. return pos - buf;
  565. pos += ret;
  566. }
  567. if (hapd->conf->wpa_pairwise & WPA_CIPHER_GCMP) {
  568. ret = os_snprintf(pos, end - pos, "GCMP ");
  569. if (ret < 0 || ret >= end - pos)
  570. return pos - buf;
  571. pos += ret;
  572. }
  573. if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
  574. ret = os_snprintf(pos, end - pos, "TKIP ");
  575. if (ret < 0 || ret >= end - pos)
  576. return pos - buf;
  577. pos += ret;
  578. }
  579. ret = os_snprintf(pos, end - pos, "\n");
  580. if (ret < 0 || ret >= end - pos)
  581. return pos - buf;
  582. pos += ret;
  583. }
  584. return pos - buf;
  585. }
  586. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  587. {
  588. char *value;
  589. int ret = 0;
  590. value = os_strchr(cmd, ' ');
  591. if (value == NULL)
  592. return -1;
  593. *value++ = '\0';
  594. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  595. if (0) {
  596. #ifdef CONFIG_WPS_TESTING
  597. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  598. long int val;
  599. val = strtol(value, NULL, 0);
  600. if (val < 0 || val > 0xff) {
  601. ret = -1;
  602. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  603. "wps_version_number %ld", val);
  604. } else {
  605. wps_version_number = val;
  606. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  607. "version %u.%u",
  608. (wps_version_number & 0xf0) >> 4,
  609. wps_version_number & 0x0f);
  610. hostapd_wps_update_ie(hapd);
  611. }
  612. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  613. wps_testing_dummy_cred = atoi(value);
  614. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  615. wps_testing_dummy_cred);
  616. #endif /* CONFIG_WPS_TESTING */
  617. #ifdef CONFIG_INTERWORKING
  618. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  619. int val = atoi(value);
  620. if (val <= 0)
  621. ret = -1;
  622. else
  623. hapd->gas_frag_limit = val;
  624. #endif /* CONFIG_INTERWORKING */
  625. } else {
  626. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  627. }
  628. return ret;
  629. }
  630. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  631. char *buf, size_t buflen)
  632. {
  633. int res;
  634. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  635. if (os_strcmp(cmd, "version") == 0) {
  636. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  637. if (res < 0 || (unsigned int) res >= buflen)
  638. return -1;
  639. return res;
  640. }
  641. return -1;
  642. }
  643. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  644. {
  645. if (hostapd_enable_iface(iface) < 0) {
  646. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  647. return -1;
  648. }
  649. return 0;
  650. }
  651. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  652. {
  653. if (hostapd_reload_iface(iface) < 0) {
  654. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  655. return -1;
  656. }
  657. return 0;
  658. }
  659. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  660. {
  661. if (hostapd_disable_iface(iface) < 0) {
  662. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  663. return -1;
  664. }
  665. return 0;
  666. }
  667. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  668. void *sock_ctx)
  669. {
  670. struct hostapd_data *hapd = eloop_ctx;
  671. char buf[256];
  672. int res;
  673. struct sockaddr_un from;
  674. socklen_t fromlen = sizeof(from);
  675. char *reply;
  676. const int reply_size = 4096;
  677. int reply_len;
  678. int level = MSG_DEBUG;
  679. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  680. (struct sockaddr *) &from, &fromlen);
  681. if (res < 0) {
  682. perror("recvfrom(ctrl_iface)");
  683. return;
  684. }
  685. buf[res] = '\0';
  686. if (os_strcmp(buf, "PING") == 0)
  687. level = MSG_EXCESSIVE;
  688. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  689. reply = os_malloc(reply_size);
  690. if (reply == NULL) {
  691. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  692. fromlen);
  693. return;
  694. }
  695. os_memcpy(reply, "OK\n", 3);
  696. reply_len = 3;
  697. if (os_strcmp(buf, "PING") == 0) {
  698. os_memcpy(reply, "PONG\n", 5);
  699. reply_len = 5;
  700. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  701. if (wpa_debug_reopen_file() < 0)
  702. reply_len = -1;
  703. } else if (os_strcmp(buf, "MIB") == 0) {
  704. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  705. if (reply_len >= 0) {
  706. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  707. reply_size - reply_len);
  708. if (res < 0)
  709. reply_len = -1;
  710. else
  711. reply_len += res;
  712. }
  713. if (reply_len >= 0) {
  714. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  715. reply_size - reply_len);
  716. if (res < 0)
  717. reply_len = -1;
  718. else
  719. reply_len += res;
  720. }
  721. #ifndef CONFIG_NO_RADIUS
  722. if (reply_len >= 0) {
  723. res = radius_client_get_mib(hapd->radius,
  724. reply + reply_len,
  725. reply_size - reply_len);
  726. if (res < 0)
  727. reply_len = -1;
  728. else
  729. reply_len += res;
  730. }
  731. #endif /* CONFIG_NO_RADIUS */
  732. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  733. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  734. reply_size);
  735. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  736. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  737. reply_size);
  738. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  739. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  740. reply_size);
  741. } else if (os_strcmp(buf, "ATTACH") == 0) {
  742. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  743. reply_len = -1;
  744. } else if (os_strcmp(buf, "DETACH") == 0) {
  745. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  746. reply_len = -1;
  747. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  748. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  749. buf + 6))
  750. reply_len = -1;
  751. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  752. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  753. reply_len = -1;
  754. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  755. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  756. reply_len = -1;
  757. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  758. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  759. reply_len = -1;
  760. #ifdef CONFIG_IEEE80211W
  761. #ifdef NEED_AP_MLME
  762. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  763. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  764. reply_len = -1;
  765. #endif /* NEED_AP_MLME */
  766. #endif /* CONFIG_IEEE80211W */
  767. #ifdef CONFIG_WPS
  768. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  769. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  770. reply_len = -1;
  771. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  772. reply_len = hostapd_ctrl_iface_wps_check_pin(
  773. hapd, buf + 14, reply, reply_size);
  774. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  775. if (hostapd_wps_button_pushed(hapd, NULL))
  776. reply_len = -1;
  777. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  778. if (hostapd_wps_cancel(hapd))
  779. reply_len = -1;
  780. #ifdef CONFIG_WPS_OOB
  781. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  782. if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
  783. reply_len = -1;
  784. #endif /* CONFIG_WPS_OOB */
  785. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  786. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  787. reply, reply_size);
  788. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  789. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  790. reply_len = -1;
  791. #ifdef CONFIG_WPS_NFC
  792. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  793. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  794. reply_len = -1;
  795. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  796. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  797. hapd, buf + 21, reply, reply_size);
  798. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  799. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  800. hapd, buf + 14, reply, reply_size);
  801. #endif /* CONFIG_WPS_NFC */
  802. #endif /* CONFIG_WPS */
  803. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  804. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  805. reply_len = -1;
  806. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  807. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  808. reply_size);
  809. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  810. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  811. reply_len = -1;
  812. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  813. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  814. reply_size);
  815. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  816. if (hostapd_ctrl_iface_enable(hapd->iface))
  817. reply_len = -1;
  818. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  819. if (hostapd_ctrl_iface_reload(hapd->iface))
  820. reply_len = -1;
  821. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  822. if (hostapd_ctrl_iface_disable(hapd->iface))
  823. reply_len = -1;
  824. } else {
  825. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  826. reply_len = 16;
  827. }
  828. if (reply_len < 0) {
  829. os_memcpy(reply, "FAIL\n", 5);
  830. reply_len = 5;
  831. }
  832. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  833. os_free(reply);
  834. }
  835. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  836. {
  837. char *buf;
  838. size_t len;
  839. if (hapd->conf->ctrl_interface == NULL)
  840. return NULL;
  841. len = os_strlen(hapd->conf->ctrl_interface) +
  842. os_strlen(hapd->conf->iface) + 2;
  843. buf = os_malloc(len);
  844. if (buf == NULL)
  845. return NULL;
  846. os_snprintf(buf, len, "%s/%s",
  847. hapd->conf->ctrl_interface, hapd->conf->iface);
  848. buf[len - 1] = '\0';
  849. return buf;
  850. }
  851. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  852. const char *txt, size_t len)
  853. {
  854. struct hostapd_data *hapd = ctx;
  855. if (hapd == NULL)
  856. return;
  857. hostapd_ctrl_iface_send(hapd, level, txt, len);
  858. }
  859. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  860. {
  861. struct sockaddr_un addr;
  862. int s = -1;
  863. char *fname = NULL;
  864. if (hapd->ctrl_sock > -1) {
  865. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  866. return 0;
  867. }
  868. if (hapd->conf->ctrl_interface == NULL)
  869. return 0;
  870. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  871. if (errno == EEXIST) {
  872. wpa_printf(MSG_DEBUG, "Using existing control "
  873. "interface directory.");
  874. } else {
  875. perror("mkdir[ctrl_interface]");
  876. goto fail;
  877. }
  878. }
  879. if (hapd->conf->ctrl_interface_gid_set &&
  880. chown(hapd->conf->ctrl_interface, 0,
  881. hapd->conf->ctrl_interface_gid) < 0) {
  882. perror("chown[ctrl_interface]");
  883. return -1;
  884. }
  885. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  886. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  887. goto fail;
  888. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  889. if (s < 0) {
  890. perror("socket(PF_UNIX)");
  891. goto fail;
  892. }
  893. os_memset(&addr, 0, sizeof(addr));
  894. #ifdef __FreeBSD__
  895. addr.sun_len = sizeof(addr);
  896. #endif /* __FreeBSD__ */
  897. addr.sun_family = AF_UNIX;
  898. fname = hostapd_ctrl_iface_path(hapd);
  899. if (fname == NULL)
  900. goto fail;
  901. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  902. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  903. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  904. strerror(errno));
  905. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  906. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  907. " allow connections - assuming it was left"
  908. "over from forced program termination");
  909. if (unlink(fname) < 0) {
  910. perror("unlink[ctrl_iface]");
  911. wpa_printf(MSG_ERROR, "Could not unlink "
  912. "existing ctrl_iface socket '%s'",
  913. fname);
  914. goto fail;
  915. }
  916. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  917. 0) {
  918. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  919. goto fail;
  920. }
  921. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  922. "ctrl_iface socket '%s'", fname);
  923. } else {
  924. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  925. "be in use - cannot override it");
  926. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  927. "not used anymore", fname);
  928. os_free(fname);
  929. fname = NULL;
  930. goto fail;
  931. }
  932. }
  933. if (hapd->conf->ctrl_interface_gid_set &&
  934. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  935. perror("chown[ctrl_interface/ifname]");
  936. goto fail;
  937. }
  938. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  939. perror("chmod[ctrl_interface/ifname]");
  940. goto fail;
  941. }
  942. os_free(fname);
  943. hapd->ctrl_sock = s;
  944. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  945. NULL);
  946. hapd->msg_ctx = hapd;
  947. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  948. return 0;
  949. fail:
  950. if (s >= 0)
  951. close(s);
  952. if (fname) {
  953. unlink(fname);
  954. os_free(fname);
  955. }
  956. return -1;
  957. }
  958. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  959. {
  960. struct wpa_ctrl_dst *dst, *prev;
  961. if (hapd->ctrl_sock > -1) {
  962. char *fname;
  963. eloop_unregister_read_sock(hapd->ctrl_sock);
  964. close(hapd->ctrl_sock);
  965. hapd->ctrl_sock = -1;
  966. fname = hostapd_ctrl_iface_path(hapd);
  967. if (fname)
  968. unlink(fname);
  969. os_free(fname);
  970. if (hapd->conf->ctrl_interface &&
  971. rmdir(hapd->conf->ctrl_interface) < 0) {
  972. if (errno == ENOTEMPTY) {
  973. wpa_printf(MSG_DEBUG, "Control interface "
  974. "directory not empty - leaving it "
  975. "behind");
  976. } else {
  977. perror("rmdir[ctrl_interface]");
  978. }
  979. }
  980. }
  981. dst = hapd->ctrl_dst;
  982. while (dst) {
  983. prev = dst;
  984. dst = dst->next;
  985. os_free(prev);
  986. }
  987. }
  988. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  989. char *buf)
  990. {
  991. if (hostapd_add_iface(interfaces, buf) < 0) {
  992. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  993. return -1;
  994. }
  995. return 0;
  996. }
  997. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  998. char *buf)
  999. {
  1000. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1001. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1002. return -1;
  1003. }
  1004. return 0;
  1005. }
  1006. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1007. void *sock_ctx)
  1008. {
  1009. void *interfaces = eloop_ctx;
  1010. char buf[256];
  1011. int res;
  1012. struct sockaddr_un from;
  1013. socklen_t fromlen = sizeof(from);
  1014. char reply[24];
  1015. int reply_len;
  1016. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1017. (struct sockaddr *) &from, &fromlen);
  1018. if (res < 0) {
  1019. perror("recvfrom(ctrl_iface)");
  1020. return;
  1021. }
  1022. buf[res] = '\0';
  1023. os_memcpy(reply, "OK\n", 3);
  1024. reply_len = 3;
  1025. if (os_strcmp(buf, "PING") == 0) {
  1026. os_memcpy(reply, "PONG\n", 5);
  1027. reply_len = 5;
  1028. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1029. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1030. reply_len = -1;
  1031. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1032. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1033. reply_len = -1;
  1034. } else {
  1035. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1036. "ignored");
  1037. reply_len = -1;
  1038. }
  1039. if (reply_len < 0) {
  1040. os_memcpy(reply, "FAIL\n", 5);
  1041. reply_len = 5;
  1042. }
  1043. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1044. }
  1045. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1046. {
  1047. char *buf;
  1048. size_t len;
  1049. if (interface->global_iface_path == NULL)
  1050. return NULL;
  1051. len = os_strlen(interface->global_iface_path) +
  1052. os_strlen(interface->global_iface_name) + 2;
  1053. buf = os_malloc(len);
  1054. if (buf == NULL)
  1055. return NULL;
  1056. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1057. interface->global_iface_name);
  1058. buf[len - 1] = '\0';
  1059. return buf;
  1060. }
  1061. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1062. {
  1063. struct sockaddr_un addr;
  1064. int s = -1;
  1065. char *fname = NULL;
  1066. if (interface->global_iface_path == NULL) {
  1067. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1068. return 0;
  1069. }
  1070. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1071. if (errno == EEXIST) {
  1072. wpa_printf(MSG_DEBUG, "Using existing control "
  1073. "interface directory.");
  1074. } else {
  1075. perror("mkdir[ctrl_interface]");
  1076. goto fail;
  1077. }
  1078. }
  1079. if (os_strlen(interface->global_iface_path) + 1 +
  1080. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1081. goto fail;
  1082. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1083. if (s < 0) {
  1084. perror("socket(PF_UNIX)");
  1085. goto fail;
  1086. }
  1087. os_memset(&addr, 0, sizeof(addr));
  1088. #ifdef __FreeBSD__
  1089. addr.sun_len = sizeof(addr);
  1090. #endif /* __FreeBSD__ */
  1091. addr.sun_family = AF_UNIX;
  1092. fname = hostapd_global_ctrl_iface_path(interface);
  1093. if (fname == NULL)
  1094. goto fail;
  1095. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1096. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1097. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1098. strerror(errno));
  1099. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1100. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1101. " allow connections - assuming it was left"
  1102. "over from forced program termination");
  1103. if (unlink(fname) < 0) {
  1104. perror("unlink[ctrl_iface]");
  1105. wpa_printf(MSG_ERROR, "Could not unlink "
  1106. "existing ctrl_iface socket '%s'",
  1107. fname);
  1108. goto fail;
  1109. }
  1110. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1111. 0) {
  1112. perror("bind(PF_UNIX)");
  1113. goto fail;
  1114. }
  1115. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1116. "ctrl_iface socket '%s'", fname);
  1117. } else {
  1118. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1119. "be in use - cannot override it");
  1120. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1121. "not used anymore", fname);
  1122. os_free(fname);
  1123. fname = NULL;
  1124. goto fail;
  1125. }
  1126. }
  1127. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1128. perror("chmod[ctrl_interface/ifname]");
  1129. goto fail;
  1130. }
  1131. os_free(fname);
  1132. interface->global_ctrl_sock = s;
  1133. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1134. interface, NULL);
  1135. return 0;
  1136. fail:
  1137. if (s >= 0)
  1138. close(s);
  1139. if (fname) {
  1140. unlink(fname);
  1141. os_free(fname);
  1142. }
  1143. return -1;
  1144. }
  1145. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1146. {
  1147. char *fname = NULL;
  1148. if (interfaces->global_ctrl_sock > -1) {
  1149. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1150. close(interfaces->global_ctrl_sock);
  1151. interfaces->global_ctrl_sock = -1;
  1152. fname = hostapd_global_ctrl_iface_path(interfaces);
  1153. if (fname) {
  1154. unlink(fname);
  1155. os_free(fname);
  1156. }
  1157. if (interfaces->global_iface_path &&
  1158. rmdir(interfaces->global_iface_path) < 0) {
  1159. if (errno == ENOTEMPTY) {
  1160. wpa_printf(MSG_DEBUG, "Control interface "
  1161. "directory not empty - leaving it "
  1162. "behind");
  1163. } else {
  1164. perror("rmdir[ctrl_interface]");
  1165. }
  1166. }
  1167. os_free(interfaces->global_iface_path);
  1168. interfaces->global_iface_path = NULL;
  1169. }
  1170. }
  1171. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1172. const char *buf, size_t len)
  1173. {
  1174. struct wpa_ctrl_dst *dst, *next;
  1175. struct msghdr msg;
  1176. int idx;
  1177. struct iovec io[2];
  1178. char levelstr[10];
  1179. dst = hapd->ctrl_dst;
  1180. if (hapd->ctrl_sock < 0 || dst == NULL)
  1181. return;
  1182. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1183. io[0].iov_base = levelstr;
  1184. io[0].iov_len = os_strlen(levelstr);
  1185. io[1].iov_base = (char *) buf;
  1186. io[1].iov_len = len;
  1187. os_memset(&msg, 0, sizeof(msg));
  1188. msg.msg_iov = io;
  1189. msg.msg_iovlen = 2;
  1190. idx = 0;
  1191. while (dst) {
  1192. next = dst->next;
  1193. if (level >= dst->debug_level) {
  1194. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1195. (u8 *) dst->addr.sun_path, dst->addrlen -
  1196. offsetof(struct sockaddr_un, sun_path));
  1197. msg.msg_name = &dst->addr;
  1198. msg.msg_namelen = dst->addrlen;
  1199. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1200. int _errno = errno;
  1201. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1202. "%d - %s",
  1203. idx, errno, strerror(errno));
  1204. dst->errors++;
  1205. if (dst->errors > 10 || _errno == ENOENT) {
  1206. hostapd_ctrl_iface_detach(
  1207. hapd, &dst->addr,
  1208. dst->addrlen);
  1209. }
  1210. } else
  1211. dst->errors = 0;
  1212. }
  1213. idx++;
  1214. dst = next;
  1215. }
  1216. }
  1217. #endif /* CONFIG_NATIVE_WINDOWS */