ctrl_iface.c 37 KB

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