ctrl_iface.c 37 KB

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