ctrl_iface.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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 == NULL)
  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. int timeout, beacon_int;
  489. /*
  490. * Prevent STA from reconnecting using cached PMKSA to force
  491. * full authentication with the authentication server (which may
  492. * decide to reject the connection),
  493. */
  494. wpa_auth_pmksa_remove(hapd->wpa_auth, addr);
  495. sta = ap_get_sta(hapd, addr);
  496. if (sta == NULL) {
  497. wpa_printf(MSG_DEBUG, "Station " MACSTR " not found "
  498. "for ESS disassociation imminent message",
  499. MAC2STR(addr));
  500. return -1;
  501. }
  502. beacon_int = hapd->iconf->beacon_int;
  503. if (beacon_int < 1)
  504. beacon_int = 100; /* best guess */
  505. /* Calculate timeout in ms based on beacon_int in TU */
  506. timeout = disassoc_timer * beacon_int * 128 / 125;
  507. wpa_printf(MSG_DEBUG, "Disassociation timer for " MACSTR
  508. " set to %d ms", MAC2STR(addr), timeout);
  509. sta->timeout_next = STA_DISASSOC_FROM_CLI;
  510. eloop_cancel_timeout(ap_handle_timer, hapd, sta);
  511. eloop_register_timeout(timeout / 1000,
  512. timeout % 1000 * 1000,
  513. ap_handle_timer, hapd, sta);
  514. }
  515. return 0;
  516. }
  517. #endif /* CONFIG_WNM */
  518. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  519. char *buf, size_t buflen)
  520. {
  521. int ret;
  522. char *pos, *end;
  523. pos = buf;
  524. end = buf + buflen;
  525. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  526. "ssid=%s\n",
  527. MAC2STR(hapd->own_addr),
  528. wpa_ssid_txt(hapd->conf->ssid.ssid,
  529. hapd->conf->ssid.ssid_len));
  530. if (ret < 0 || ret >= end - pos)
  531. return pos - buf;
  532. pos += ret;
  533. #ifdef CONFIG_WPS
  534. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  535. hapd->conf->wps_state == 0 ? "disabled" :
  536. (hapd->conf->wps_state == 1 ? "not configured" :
  537. "configured"));
  538. if (ret < 0 || ret >= end - pos)
  539. return pos - buf;
  540. pos += ret;
  541. if (hapd->conf->wps_state && hapd->conf->wpa &&
  542. hapd->conf->ssid.wpa_passphrase) {
  543. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  544. hapd->conf->ssid.wpa_passphrase);
  545. if (ret < 0 || ret >= end - pos)
  546. return pos - buf;
  547. pos += ret;
  548. }
  549. if (hapd->conf->wps_state && hapd->conf->wpa &&
  550. hapd->conf->ssid.wpa_psk &&
  551. hapd->conf->ssid.wpa_psk->group) {
  552. char hex[PMK_LEN * 2 + 1];
  553. wpa_snprintf_hex(hex, sizeof(hex),
  554. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  555. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  556. if (ret < 0 || ret >= end - pos)
  557. return pos - buf;
  558. pos += ret;
  559. }
  560. #endif /* CONFIG_WPS */
  561. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  562. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  563. if (ret < 0 || ret >= end - pos)
  564. return pos - buf;
  565. pos += ret;
  566. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  567. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  568. if (ret < 0 || ret >= end - pos)
  569. return pos - buf;
  570. pos += ret;
  571. }
  572. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  573. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  574. if (ret < 0 || ret >= end - pos)
  575. return pos - buf;
  576. pos += ret;
  577. }
  578. #ifdef CONFIG_IEEE80211R
  579. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  580. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  581. if (ret < 0 || ret >= end - pos)
  582. return pos - buf;
  583. pos += ret;
  584. }
  585. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  586. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  587. if (ret < 0 || ret >= end - pos)
  588. return pos - buf;
  589. pos += ret;
  590. }
  591. #endif /* CONFIG_IEEE80211R */
  592. #ifdef CONFIG_IEEE80211W
  593. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  594. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  595. if (ret < 0 || ret >= end - pos)
  596. return pos - buf;
  597. pos += ret;
  598. }
  599. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  600. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  601. if (ret < 0 || ret >= end - pos)
  602. return pos - buf;
  603. pos += ret;
  604. }
  605. #endif /* CONFIG_IEEE80211W */
  606. ret = os_snprintf(pos, end - pos, "\n");
  607. if (ret < 0 || ret >= end - pos)
  608. return pos - buf;
  609. pos += ret;
  610. }
  611. if (hapd->conf->wpa) {
  612. ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
  613. wpa_cipher_txt(hapd->conf->wpa_group));
  614. if (ret < 0 || ret >= end - pos)
  615. return pos - buf;
  616. pos += ret;
  617. }
  618. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  619. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  620. if (ret < 0 || ret >= end - pos)
  621. return pos - buf;
  622. pos += ret;
  623. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  624. " ");
  625. if (ret < 0)
  626. return pos - buf;
  627. pos += ret;
  628. ret = os_snprintf(pos, end - pos, "\n");
  629. if (ret < 0 || ret >= end - pos)
  630. return pos - buf;
  631. pos += ret;
  632. }
  633. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  634. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  635. if (ret < 0 || ret >= end - pos)
  636. return pos - buf;
  637. pos += ret;
  638. ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
  639. " ");
  640. if (ret < 0)
  641. return pos - buf;
  642. pos += ret;
  643. ret = os_snprintf(pos, end - pos, "\n");
  644. if (ret < 0 || ret >= end - pos)
  645. return pos - buf;
  646. pos += ret;
  647. }
  648. return pos - buf;
  649. }
  650. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  651. {
  652. char *value;
  653. int ret = 0;
  654. value = os_strchr(cmd, ' ');
  655. if (value == NULL)
  656. return -1;
  657. *value++ = '\0';
  658. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  659. if (0) {
  660. #ifdef CONFIG_WPS_TESTING
  661. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  662. long int val;
  663. val = strtol(value, NULL, 0);
  664. if (val < 0 || val > 0xff) {
  665. ret = -1;
  666. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  667. "wps_version_number %ld", val);
  668. } else {
  669. wps_version_number = val;
  670. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  671. "version %u.%u",
  672. (wps_version_number & 0xf0) >> 4,
  673. wps_version_number & 0x0f);
  674. hostapd_wps_update_ie(hapd);
  675. }
  676. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  677. wps_testing_dummy_cred = atoi(value);
  678. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  679. wps_testing_dummy_cred);
  680. #endif /* CONFIG_WPS_TESTING */
  681. #ifdef CONFIG_INTERWORKING
  682. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  683. int val = atoi(value);
  684. if (val <= 0)
  685. ret = -1;
  686. else
  687. hapd->gas_frag_limit = val;
  688. #endif /* CONFIG_INTERWORKING */
  689. } else {
  690. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  691. }
  692. return ret;
  693. }
  694. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  695. char *buf, size_t buflen)
  696. {
  697. int res;
  698. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  699. if (os_strcmp(cmd, "version") == 0) {
  700. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  701. if (res < 0 || (unsigned int) res >= buflen)
  702. return -1;
  703. return res;
  704. }
  705. return -1;
  706. }
  707. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  708. {
  709. if (hostapd_enable_iface(iface) < 0) {
  710. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  711. return -1;
  712. }
  713. return 0;
  714. }
  715. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  716. {
  717. if (hostapd_reload_iface(iface) < 0) {
  718. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  719. return -1;
  720. }
  721. return 0;
  722. }
  723. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  724. {
  725. if (hostapd_disable_iface(iface) < 0) {
  726. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  727. return -1;
  728. }
  729. return 0;
  730. }
  731. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  732. void *sock_ctx)
  733. {
  734. struct hostapd_data *hapd = eloop_ctx;
  735. char buf[256];
  736. int res;
  737. struct sockaddr_un from;
  738. socklen_t fromlen = sizeof(from);
  739. char *reply;
  740. const int reply_size = 4096;
  741. int reply_len;
  742. int level = MSG_DEBUG;
  743. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  744. (struct sockaddr *) &from, &fromlen);
  745. if (res < 0) {
  746. perror("recvfrom(ctrl_iface)");
  747. return;
  748. }
  749. buf[res] = '\0';
  750. if (os_strcmp(buf, "PING") == 0)
  751. level = MSG_EXCESSIVE;
  752. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  753. reply = os_malloc(reply_size);
  754. if (reply == NULL) {
  755. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  756. fromlen);
  757. return;
  758. }
  759. os_memcpy(reply, "OK\n", 3);
  760. reply_len = 3;
  761. if (os_strcmp(buf, "PING") == 0) {
  762. os_memcpy(reply, "PONG\n", 5);
  763. reply_len = 5;
  764. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  765. if (wpa_debug_reopen_file() < 0)
  766. reply_len = -1;
  767. } else if (os_strcmp(buf, "MIB") == 0) {
  768. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  769. if (reply_len >= 0) {
  770. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  771. reply_size - reply_len);
  772. if (res < 0)
  773. reply_len = -1;
  774. else
  775. reply_len += res;
  776. }
  777. if (reply_len >= 0) {
  778. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  779. reply_size - reply_len);
  780. if (res < 0)
  781. reply_len = -1;
  782. else
  783. reply_len += res;
  784. }
  785. #ifndef CONFIG_NO_RADIUS
  786. if (reply_len >= 0) {
  787. res = radius_client_get_mib(hapd->radius,
  788. reply + reply_len,
  789. reply_size - reply_len);
  790. if (res < 0)
  791. reply_len = -1;
  792. else
  793. reply_len += res;
  794. }
  795. #endif /* CONFIG_NO_RADIUS */
  796. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  797. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  798. reply_size);
  799. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  800. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  801. reply_size);
  802. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  803. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  804. reply_size);
  805. } else if (os_strcmp(buf, "ATTACH") == 0) {
  806. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  807. reply_len = -1;
  808. } else if (os_strcmp(buf, "DETACH") == 0) {
  809. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  810. reply_len = -1;
  811. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  812. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  813. buf + 6))
  814. reply_len = -1;
  815. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  816. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  817. reply_len = -1;
  818. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  819. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  820. reply_len = -1;
  821. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  822. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  823. reply_len = -1;
  824. #ifdef CONFIG_IEEE80211W
  825. #ifdef NEED_AP_MLME
  826. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  827. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  828. reply_len = -1;
  829. #endif /* NEED_AP_MLME */
  830. #endif /* CONFIG_IEEE80211W */
  831. #ifdef CONFIG_WPS
  832. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  833. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  834. reply_len = -1;
  835. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  836. reply_len = hostapd_ctrl_iface_wps_check_pin(
  837. hapd, buf + 14, reply, reply_size);
  838. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  839. if (hostapd_wps_button_pushed(hapd, NULL))
  840. reply_len = -1;
  841. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  842. if (hostapd_wps_cancel(hapd))
  843. reply_len = -1;
  844. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  845. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  846. reply, reply_size);
  847. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  848. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  849. reply_len = -1;
  850. #ifdef CONFIG_WPS_NFC
  851. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  852. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  853. reply_len = -1;
  854. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  855. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  856. hapd, buf + 21, reply, reply_size);
  857. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  858. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  859. hapd, buf + 14, reply, reply_size);
  860. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  861. reply_len = hostapd_ctrl_iface_nfc_get_handover_sel(
  862. hapd, buf + 21, reply, reply_size);
  863. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  864. if (hostapd_ctrl_iface_nfc_report_handover(hapd, buf + 20))
  865. reply_len = -1;
  866. #endif /* CONFIG_WPS_NFC */
  867. #endif /* CONFIG_WPS */
  868. #ifdef CONFIG_WNM
  869. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  870. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  871. reply_len = -1;
  872. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  873. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  874. reply_len = -1;
  875. #endif /* CONFIG_WNM */
  876. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  877. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  878. reply_size);
  879. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  880. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  881. reply_len = -1;
  882. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  883. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  884. reply_size);
  885. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  886. if (hostapd_ctrl_iface_enable(hapd->iface))
  887. reply_len = -1;
  888. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  889. if (hostapd_ctrl_iface_reload(hapd->iface))
  890. reply_len = -1;
  891. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  892. if (hostapd_ctrl_iface_disable(hapd->iface))
  893. reply_len = -1;
  894. } else {
  895. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  896. reply_len = 16;
  897. }
  898. if (reply_len < 0) {
  899. os_memcpy(reply, "FAIL\n", 5);
  900. reply_len = 5;
  901. }
  902. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  903. os_free(reply);
  904. }
  905. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  906. {
  907. char *buf;
  908. size_t len;
  909. if (hapd->conf->ctrl_interface == NULL)
  910. return NULL;
  911. len = os_strlen(hapd->conf->ctrl_interface) +
  912. os_strlen(hapd->conf->iface) + 2;
  913. buf = os_malloc(len);
  914. if (buf == NULL)
  915. return NULL;
  916. os_snprintf(buf, len, "%s/%s",
  917. hapd->conf->ctrl_interface, hapd->conf->iface);
  918. buf[len - 1] = '\0';
  919. return buf;
  920. }
  921. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level, int global,
  922. const char *txt, size_t len)
  923. {
  924. struct hostapd_data *hapd = ctx;
  925. if (hapd == NULL)
  926. return;
  927. hostapd_ctrl_iface_send(hapd, level, txt, len);
  928. }
  929. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  930. {
  931. struct sockaddr_un addr;
  932. int s = -1;
  933. char *fname = NULL;
  934. if (hapd->ctrl_sock > -1) {
  935. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  936. return 0;
  937. }
  938. if (hapd->conf->ctrl_interface == NULL)
  939. return 0;
  940. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  941. if (errno == EEXIST) {
  942. wpa_printf(MSG_DEBUG, "Using existing control "
  943. "interface directory.");
  944. } else {
  945. perror("mkdir[ctrl_interface]");
  946. goto fail;
  947. }
  948. }
  949. if (hapd->conf->ctrl_interface_gid_set &&
  950. chown(hapd->conf->ctrl_interface, -1,
  951. hapd->conf->ctrl_interface_gid) < 0) {
  952. perror("chown[ctrl_interface]");
  953. return -1;
  954. }
  955. if (!hapd->conf->ctrl_interface_gid_set &&
  956. hapd->iface->interfaces->ctrl_iface_group &&
  957. chown(hapd->conf->ctrl_interface, -1,
  958. hapd->iface->interfaces->ctrl_iface_group) < 0) {
  959. perror("chown[ctrl_interface]");
  960. return -1;
  961. }
  962. #ifdef ANDROID
  963. /*
  964. * Android is using umask 0077 which would leave the control interface
  965. * directory without group access. This breaks things since Wi-Fi
  966. * framework assumes that this directory can be accessed by other
  967. * applications in the wifi group. Fix this by adding group access even
  968. * if umask value would prevent this.
  969. */
  970. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  971. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  972. strerror(errno));
  973. /* Try to continue anyway */
  974. }
  975. #endif /* ANDROID */
  976. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  977. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  978. goto fail;
  979. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  980. if (s < 0) {
  981. perror("socket(PF_UNIX)");
  982. goto fail;
  983. }
  984. os_memset(&addr, 0, sizeof(addr));
  985. #ifdef __FreeBSD__
  986. addr.sun_len = sizeof(addr);
  987. #endif /* __FreeBSD__ */
  988. addr.sun_family = AF_UNIX;
  989. fname = hostapd_ctrl_iface_path(hapd);
  990. if (fname == NULL)
  991. goto fail;
  992. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  993. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  994. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  995. strerror(errno));
  996. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  997. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  998. " allow connections - assuming it was left"
  999. "over from forced program termination");
  1000. if (unlink(fname) < 0) {
  1001. perror("unlink[ctrl_iface]");
  1002. wpa_printf(MSG_ERROR, "Could not unlink "
  1003. "existing ctrl_iface socket '%s'",
  1004. fname);
  1005. goto fail;
  1006. }
  1007. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1008. 0) {
  1009. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  1010. goto fail;
  1011. }
  1012. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1013. "ctrl_iface socket '%s'", fname);
  1014. } else {
  1015. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1016. "be in use - cannot override it");
  1017. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1018. "not used anymore", fname);
  1019. os_free(fname);
  1020. fname = NULL;
  1021. goto fail;
  1022. }
  1023. }
  1024. if (hapd->conf->ctrl_interface_gid_set &&
  1025. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  1026. perror("chown[ctrl_interface/ifname]");
  1027. goto fail;
  1028. }
  1029. if (!hapd->conf->ctrl_interface_gid_set &&
  1030. hapd->iface->interfaces->ctrl_iface_group &&
  1031. chown(fname, -1, hapd->iface->interfaces->ctrl_iface_group) < 0) {
  1032. perror("chown[ctrl_interface/ifname]");
  1033. goto fail;
  1034. }
  1035. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1036. perror("chmod[ctrl_interface/ifname]");
  1037. goto fail;
  1038. }
  1039. os_free(fname);
  1040. hapd->ctrl_sock = s;
  1041. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  1042. NULL);
  1043. hapd->msg_ctx = hapd;
  1044. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  1045. return 0;
  1046. fail:
  1047. if (s >= 0)
  1048. close(s);
  1049. if (fname) {
  1050. unlink(fname);
  1051. os_free(fname);
  1052. }
  1053. return -1;
  1054. }
  1055. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  1056. {
  1057. struct wpa_ctrl_dst *dst, *prev;
  1058. if (hapd->ctrl_sock > -1) {
  1059. char *fname;
  1060. eloop_unregister_read_sock(hapd->ctrl_sock);
  1061. close(hapd->ctrl_sock);
  1062. hapd->ctrl_sock = -1;
  1063. fname = hostapd_ctrl_iface_path(hapd);
  1064. if (fname)
  1065. unlink(fname);
  1066. os_free(fname);
  1067. if (hapd->conf->ctrl_interface &&
  1068. rmdir(hapd->conf->ctrl_interface) < 0) {
  1069. if (errno == ENOTEMPTY) {
  1070. wpa_printf(MSG_DEBUG, "Control interface "
  1071. "directory not empty - leaving it "
  1072. "behind");
  1073. } else {
  1074. perror("rmdir[ctrl_interface]");
  1075. }
  1076. }
  1077. }
  1078. dst = hapd->ctrl_dst;
  1079. while (dst) {
  1080. prev = dst;
  1081. dst = dst->next;
  1082. os_free(prev);
  1083. }
  1084. }
  1085. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  1086. char *buf)
  1087. {
  1088. if (hostapd_add_iface(interfaces, buf) < 0) {
  1089. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  1090. return -1;
  1091. }
  1092. return 0;
  1093. }
  1094. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  1095. char *buf)
  1096. {
  1097. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1098. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1099. return -1;
  1100. }
  1101. return 0;
  1102. }
  1103. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1104. void *sock_ctx)
  1105. {
  1106. void *interfaces = eloop_ctx;
  1107. char buf[256];
  1108. int res;
  1109. struct sockaddr_un from;
  1110. socklen_t fromlen = sizeof(from);
  1111. char reply[24];
  1112. int reply_len;
  1113. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1114. (struct sockaddr *) &from, &fromlen);
  1115. if (res < 0) {
  1116. perror("recvfrom(ctrl_iface)");
  1117. return;
  1118. }
  1119. buf[res] = '\0';
  1120. os_memcpy(reply, "OK\n", 3);
  1121. reply_len = 3;
  1122. if (os_strcmp(buf, "PING") == 0) {
  1123. os_memcpy(reply, "PONG\n", 5);
  1124. reply_len = 5;
  1125. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1126. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1127. reply_len = -1;
  1128. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1129. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1130. reply_len = -1;
  1131. } else {
  1132. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1133. "ignored");
  1134. reply_len = -1;
  1135. }
  1136. if (reply_len < 0) {
  1137. os_memcpy(reply, "FAIL\n", 5);
  1138. reply_len = 5;
  1139. }
  1140. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1141. }
  1142. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1143. {
  1144. char *buf;
  1145. size_t len;
  1146. if (interface->global_iface_path == NULL)
  1147. return NULL;
  1148. len = os_strlen(interface->global_iface_path) +
  1149. os_strlen(interface->global_iface_name) + 2;
  1150. buf = os_malloc(len);
  1151. if (buf == NULL)
  1152. return NULL;
  1153. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1154. interface->global_iface_name);
  1155. buf[len - 1] = '\0';
  1156. return buf;
  1157. }
  1158. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1159. {
  1160. struct sockaddr_un addr;
  1161. int s = -1;
  1162. char *fname = NULL;
  1163. if (interface->global_iface_path == NULL) {
  1164. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1165. return 0;
  1166. }
  1167. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1168. if (errno == EEXIST) {
  1169. wpa_printf(MSG_DEBUG, "Using existing control "
  1170. "interface directory.");
  1171. } else {
  1172. perror("mkdir[ctrl_interface]");
  1173. goto fail;
  1174. }
  1175. } else if (interface->ctrl_iface_group &&
  1176. chown(interface->global_iface_path, -1,
  1177. interface->ctrl_iface_group) < 0) {
  1178. perror("chown[ctrl_interface]");
  1179. goto fail;
  1180. }
  1181. if (os_strlen(interface->global_iface_path) + 1 +
  1182. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1183. goto fail;
  1184. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1185. if (s < 0) {
  1186. perror("socket(PF_UNIX)");
  1187. goto fail;
  1188. }
  1189. os_memset(&addr, 0, sizeof(addr));
  1190. #ifdef __FreeBSD__
  1191. addr.sun_len = sizeof(addr);
  1192. #endif /* __FreeBSD__ */
  1193. addr.sun_family = AF_UNIX;
  1194. fname = hostapd_global_ctrl_iface_path(interface);
  1195. if (fname == NULL)
  1196. goto fail;
  1197. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1198. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1199. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1200. strerror(errno));
  1201. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1202. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1203. " allow connections - assuming it was left"
  1204. "over from forced program termination");
  1205. if (unlink(fname) < 0) {
  1206. perror("unlink[ctrl_iface]");
  1207. wpa_printf(MSG_ERROR, "Could not unlink "
  1208. "existing ctrl_iface socket '%s'",
  1209. fname);
  1210. goto fail;
  1211. }
  1212. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1213. 0) {
  1214. perror("bind(PF_UNIX)");
  1215. goto fail;
  1216. }
  1217. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1218. "ctrl_iface socket '%s'", fname);
  1219. } else {
  1220. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1221. "be in use - cannot override it");
  1222. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1223. "not used anymore", fname);
  1224. os_free(fname);
  1225. fname = NULL;
  1226. goto fail;
  1227. }
  1228. }
  1229. if (interface->ctrl_iface_group &&
  1230. chown(fname, -1, interface->ctrl_iface_group) < 0) {
  1231. perror("chown[ctrl_interface]");
  1232. goto fail;
  1233. }
  1234. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1235. perror("chmod[ctrl_interface/ifname]");
  1236. goto fail;
  1237. }
  1238. os_free(fname);
  1239. interface->global_ctrl_sock = s;
  1240. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1241. interface, NULL);
  1242. return 0;
  1243. fail:
  1244. if (s >= 0)
  1245. close(s);
  1246. if (fname) {
  1247. unlink(fname);
  1248. os_free(fname);
  1249. }
  1250. return -1;
  1251. }
  1252. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1253. {
  1254. char *fname = NULL;
  1255. if (interfaces->global_ctrl_sock > -1) {
  1256. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1257. close(interfaces->global_ctrl_sock);
  1258. interfaces->global_ctrl_sock = -1;
  1259. fname = hostapd_global_ctrl_iface_path(interfaces);
  1260. if (fname) {
  1261. unlink(fname);
  1262. os_free(fname);
  1263. }
  1264. if (interfaces->global_iface_path &&
  1265. rmdir(interfaces->global_iface_path) < 0) {
  1266. if (errno == ENOTEMPTY) {
  1267. wpa_printf(MSG_DEBUG, "Control interface "
  1268. "directory not empty - leaving it "
  1269. "behind");
  1270. } else {
  1271. perror("rmdir[ctrl_interface]");
  1272. }
  1273. }
  1274. os_free(interfaces->global_iface_path);
  1275. interfaces->global_iface_path = NULL;
  1276. }
  1277. }
  1278. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1279. const char *buf, size_t len)
  1280. {
  1281. struct wpa_ctrl_dst *dst, *next;
  1282. struct msghdr msg;
  1283. int idx;
  1284. struct iovec io[2];
  1285. char levelstr[10];
  1286. dst = hapd->ctrl_dst;
  1287. if (hapd->ctrl_sock < 0 || dst == NULL)
  1288. return;
  1289. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1290. io[0].iov_base = levelstr;
  1291. io[0].iov_len = os_strlen(levelstr);
  1292. io[1].iov_base = (char *) buf;
  1293. io[1].iov_len = len;
  1294. os_memset(&msg, 0, sizeof(msg));
  1295. msg.msg_iov = io;
  1296. msg.msg_iovlen = 2;
  1297. idx = 0;
  1298. while (dst) {
  1299. next = dst->next;
  1300. if (level >= dst->debug_level) {
  1301. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1302. (u8 *) dst->addr.sun_path, dst->addrlen -
  1303. offsetof(struct sockaddr_un, sun_path));
  1304. msg.msg_name = &dst->addr;
  1305. msg.msg_namelen = dst->addrlen;
  1306. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1307. int _errno = errno;
  1308. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1309. "%d - %s",
  1310. idx, errno, strerror(errno));
  1311. dst->errors++;
  1312. if (dst->errors > 10 || _errno == ENOENT) {
  1313. hostapd_ctrl_iface_detach(
  1314. hapd, &dst->addr,
  1315. dst->addrlen);
  1316. }
  1317. } else
  1318. dst->errors = 0;
  1319. }
  1320. idx++;
  1321. dst = next;
  1322. }
  1323. }
  1324. #endif /* CONFIG_NATIVE_WINDOWS */