ctrl_iface.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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. #endif /* CONFIG_WPS_NFC */
  288. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  289. char *buf, size_t buflen)
  290. {
  291. int timeout = 300;
  292. char *pos;
  293. const char *pin_txt;
  294. pos = os_strchr(txt, ' ');
  295. if (pos)
  296. *pos++ = '\0';
  297. if (os_strcmp(txt, "disable") == 0) {
  298. hostapd_wps_ap_pin_disable(hapd);
  299. return os_snprintf(buf, buflen, "OK\n");
  300. }
  301. if (os_strcmp(txt, "random") == 0) {
  302. if (pos)
  303. timeout = atoi(pos);
  304. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  305. if (pin_txt == NULL)
  306. return -1;
  307. return os_snprintf(buf, buflen, "%s", pin_txt);
  308. }
  309. if (os_strcmp(txt, "get") == 0) {
  310. pin_txt = hostapd_wps_ap_pin_get(hapd);
  311. if (pin_txt == NULL)
  312. return -1;
  313. return os_snprintf(buf, buflen, "%s", pin_txt);
  314. }
  315. if (os_strcmp(txt, "set") == 0) {
  316. char *pin;
  317. if (pos == NULL)
  318. return -1;
  319. pin = pos;
  320. pos = os_strchr(pos, ' ');
  321. if (pos) {
  322. *pos++ = '\0';
  323. timeout = atoi(pos);
  324. }
  325. if (os_strlen(pin) > buflen)
  326. return -1;
  327. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  328. return -1;
  329. return os_snprintf(buf, buflen, "%s", pin);
  330. }
  331. return -1;
  332. }
  333. static int hostapd_ctrl_iface_wps_config(struct hostapd_data *hapd, char *txt)
  334. {
  335. char *pos;
  336. char *ssid, *auth, *encr = NULL, *key = NULL;
  337. ssid = txt;
  338. pos = os_strchr(txt, ' ');
  339. if (!pos)
  340. return -1;
  341. *pos++ = '\0';
  342. auth = pos;
  343. pos = os_strchr(pos, ' ');
  344. if (pos) {
  345. *pos++ = '\0';
  346. encr = pos;
  347. pos = os_strchr(pos, ' ');
  348. if (pos) {
  349. *pos++ = '\0';
  350. key = pos;
  351. }
  352. }
  353. return hostapd_wps_config_ap(hapd, ssid, auth, encr, key);
  354. }
  355. #endif /* CONFIG_WPS */
  356. #ifdef CONFIG_WNM
  357. static int hostapd_ctrl_iface_disassoc_imminent(struct hostapd_data *hapd,
  358. const char *cmd)
  359. {
  360. u8 addr[ETH_ALEN];
  361. u8 buf[1000], *pos;
  362. struct ieee80211_mgmt *mgmt;
  363. int disassoc_timer;
  364. if (hwaddr_aton(cmd, addr))
  365. return -1;
  366. if (cmd[17] != ' ')
  367. return -1;
  368. disassoc_timer = atoi(cmd + 17);
  369. os_memset(buf, 0, sizeof(buf));
  370. mgmt = (struct ieee80211_mgmt *) buf;
  371. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  372. WLAN_FC_STYPE_ACTION);
  373. os_memcpy(mgmt->da, addr, ETH_ALEN);
  374. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  375. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  376. mgmt->u.action.category = WLAN_ACTION_WNM;
  377. mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
  378. mgmt->u.action.u.bss_tm_req.dialog_token = 1;
  379. mgmt->u.action.u.bss_tm_req.req_mode =
  380. WNM_BSS_TM_REQ_DISASSOC_IMMINENT;
  381. mgmt->u.action.u.bss_tm_req.disassoc_timer =
  382. host_to_le16(disassoc_timer);
  383. mgmt->u.action.u.bss_tm_req.validity_interval = 0;
  384. pos = mgmt->u.action.u.bss_tm_req.variable;
  385. if (hostapd_drv_send_mlme(hapd, buf, pos - buf, 0) < 0) {
  386. wpa_printf(MSG_DEBUG, "Failed to send BSS Transition "
  387. "Management Request frame");
  388. return -1;
  389. }
  390. return 0;
  391. }
  392. static int hostapd_ctrl_iface_ess_disassoc(struct hostapd_data *hapd,
  393. const char *cmd)
  394. {
  395. u8 addr[ETH_ALEN];
  396. const char *url;
  397. u8 buf[1000], *pos;
  398. struct ieee80211_mgmt *mgmt;
  399. size_t url_len;
  400. if (hwaddr_aton(cmd, addr))
  401. return -1;
  402. url = cmd + 17;
  403. if (*url != ' ')
  404. return -1;
  405. url++;
  406. url_len = os_strlen(url);
  407. if (url_len > 255)
  408. return -1;
  409. os_memset(buf, 0, sizeof(buf));
  410. mgmt = (struct ieee80211_mgmt *) buf;
  411. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  412. WLAN_FC_STYPE_ACTION);
  413. os_memcpy(mgmt->da, addr, ETH_ALEN);
  414. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  415. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  416. mgmt->u.action.category = WLAN_ACTION_WNM;
  417. mgmt->u.action.u.bss_tm_req.action = WNM_BSS_TRANS_MGMT_REQ;
  418. mgmt->u.action.u.bss_tm_req.dialog_token = 1;
  419. mgmt->u.action.u.bss_tm_req.req_mode =
  420. WNM_BSS_TM_REQ_ESS_DISASSOC_IMMINENT;
  421. mgmt->u.action.u.bss_tm_req.disassoc_timer = host_to_le16(0);
  422. mgmt->u.action.u.bss_tm_req.validity_interval = 0;
  423. pos = mgmt->u.action.u.bss_tm_req.variable;
  424. /* Session Information URL */
  425. *pos++ = url_len;
  426. os_memcpy(pos, url, url_len);
  427. pos += url_len;
  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. #endif /* CONFIG_WNM */
  436. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  437. char *buf, size_t buflen)
  438. {
  439. int ret;
  440. char *pos, *end;
  441. pos = buf;
  442. end = buf + buflen;
  443. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  444. "ssid=%s\n",
  445. MAC2STR(hapd->own_addr),
  446. wpa_ssid_txt(hapd->conf->ssid.ssid,
  447. hapd->conf->ssid.ssid_len));
  448. if (ret < 0 || ret >= end - pos)
  449. return pos - buf;
  450. pos += ret;
  451. #ifdef CONFIG_WPS
  452. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  453. hapd->conf->wps_state == 0 ? "disabled" :
  454. (hapd->conf->wps_state == 1 ? "not configured" :
  455. "configured"));
  456. if (ret < 0 || ret >= end - pos)
  457. return pos - buf;
  458. pos += ret;
  459. if (hapd->conf->wps_state && hapd->conf->wpa &&
  460. hapd->conf->ssid.wpa_passphrase) {
  461. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  462. hapd->conf->ssid.wpa_passphrase);
  463. if (ret < 0 || ret >= end - pos)
  464. return pos - buf;
  465. pos += ret;
  466. }
  467. if (hapd->conf->wps_state && hapd->conf->wpa &&
  468. hapd->conf->ssid.wpa_psk &&
  469. hapd->conf->ssid.wpa_psk->group) {
  470. char hex[PMK_LEN * 2 + 1];
  471. wpa_snprintf_hex(hex, sizeof(hex),
  472. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  473. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  474. if (ret < 0 || ret >= end - pos)
  475. return pos - buf;
  476. pos += ret;
  477. }
  478. #endif /* CONFIG_WPS */
  479. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  480. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  481. if (ret < 0 || ret >= end - pos)
  482. return pos - buf;
  483. pos += ret;
  484. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  485. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  486. if (ret < 0 || ret >= end - pos)
  487. return pos - buf;
  488. pos += ret;
  489. }
  490. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  491. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  492. if (ret < 0 || ret >= end - pos)
  493. return pos - buf;
  494. pos += ret;
  495. }
  496. #ifdef CONFIG_IEEE80211R
  497. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  498. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  499. if (ret < 0 || ret >= end - pos)
  500. return pos - buf;
  501. pos += ret;
  502. }
  503. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  504. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  505. if (ret < 0 || ret >= end - pos)
  506. return pos - buf;
  507. pos += ret;
  508. }
  509. #endif /* CONFIG_IEEE80211R */
  510. #ifdef CONFIG_IEEE80211W
  511. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  512. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  513. if (ret < 0 || ret >= end - pos)
  514. return pos - buf;
  515. pos += ret;
  516. }
  517. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  518. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  519. if (ret < 0 || ret >= end - pos)
  520. return pos - buf;
  521. pos += ret;
  522. }
  523. #endif /* CONFIG_IEEE80211W */
  524. ret = os_snprintf(pos, end - pos, "\n");
  525. if (ret < 0 || ret >= end - pos)
  526. return pos - buf;
  527. pos += ret;
  528. }
  529. if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
  530. ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n");
  531. if (ret < 0 || ret >= end - pos)
  532. return pos - buf;
  533. pos += ret;
  534. } else if (hapd->conf->wpa &&
  535. hapd->conf->wpa_group == WPA_CIPHER_GCMP) {
  536. ret = os_snprintf(pos, end - pos, "group_cipher=GCMP\n");
  537. if (ret < 0 || ret >= end - pos)
  538. return pos - buf;
  539. pos += ret;
  540. } else if (hapd->conf->wpa &&
  541. hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
  542. ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
  543. if (ret < 0 || ret >= end - pos)
  544. return pos - buf;
  545. pos += ret;
  546. }
  547. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  548. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  549. if (ret < 0 || ret >= end - pos)
  550. return pos - buf;
  551. pos += ret;
  552. if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
  553. ret = os_snprintf(pos, end - pos, "CCMP ");
  554. if (ret < 0 || ret >= end - pos)
  555. return pos - buf;
  556. pos += ret;
  557. }
  558. if (hapd->conf->rsn_pairwise & WPA_CIPHER_GCMP) {
  559. ret = os_snprintf(pos, end - pos, "GCMP ");
  560. if (ret < 0 || ret >= end - pos)
  561. return pos - buf;
  562. pos += ret;
  563. }
  564. if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
  565. ret = os_snprintf(pos, end - pos, "TKIP ");
  566. if (ret < 0 || ret >= end - pos)
  567. return pos - buf;
  568. pos += ret;
  569. }
  570. ret = os_snprintf(pos, end - pos, "\n");
  571. if (ret < 0 || ret >= end - pos)
  572. return pos - buf;
  573. pos += ret;
  574. }
  575. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  576. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  577. if (ret < 0 || ret >= end - pos)
  578. return pos - buf;
  579. pos += ret;
  580. if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
  581. ret = os_snprintf(pos, end - pos, "CCMP ");
  582. if (ret < 0 || ret >= end - pos)
  583. return pos - buf;
  584. pos += ret;
  585. }
  586. if (hapd->conf->wpa_pairwise & WPA_CIPHER_GCMP) {
  587. ret = os_snprintf(pos, end - pos, "GCMP ");
  588. if (ret < 0 || ret >= end - pos)
  589. return pos - buf;
  590. pos += ret;
  591. }
  592. if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
  593. ret = os_snprintf(pos, end - pos, "TKIP ");
  594. if (ret < 0 || ret >= end - pos)
  595. return pos - buf;
  596. pos += ret;
  597. }
  598. ret = os_snprintf(pos, end - pos, "\n");
  599. if (ret < 0 || ret >= end - pos)
  600. return pos - buf;
  601. pos += ret;
  602. }
  603. return pos - buf;
  604. }
  605. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  606. {
  607. char *value;
  608. int ret = 0;
  609. value = os_strchr(cmd, ' ');
  610. if (value == NULL)
  611. return -1;
  612. *value++ = '\0';
  613. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  614. if (0) {
  615. #ifdef CONFIG_WPS_TESTING
  616. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  617. long int val;
  618. val = strtol(value, NULL, 0);
  619. if (val < 0 || val > 0xff) {
  620. ret = -1;
  621. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  622. "wps_version_number %ld", val);
  623. } else {
  624. wps_version_number = val;
  625. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  626. "version %u.%u",
  627. (wps_version_number & 0xf0) >> 4,
  628. wps_version_number & 0x0f);
  629. hostapd_wps_update_ie(hapd);
  630. }
  631. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  632. wps_testing_dummy_cred = atoi(value);
  633. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  634. wps_testing_dummy_cred);
  635. #endif /* CONFIG_WPS_TESTING */
  636. #ifdef CONFIG_INTERWORKING
  637. } else if (os_strcasecmp(cmd, "gas_frag_limit") == 0) {
  638. int val = atoi(value);
  639. if (val <= 0)
  640. ret = -1;
  641. else
  642. hapd->gas_frag_limit = val;
  643. #endif /* CONFIG_INTERWORKING */
  644. } else {
  645. ret = hostapd_set_iface(hapd->iconf, hapd->conf, cmd, value);
  646. }
  647. return ret;
  648. }
  649. static int hostapd_ctrl_iface_get(struct hostapd_data *hapd, char *cmd,
  650. char *buf, size_t buflen)
  651. {
  652. int res;
  653. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  654. if (os_strcmp(cmd, "version") == 0) {
  655. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  656. if (res < 0 || (unsigned int) res >= buflen)
  657. return -1;
  658. return res;
  659. }
  660. return -1;
  661. }
  662. static int hostapd_ctrl_iface_enable(struct hostapd_iface *iface)
  663. {
  664. if (hostapd_enable_iface(iface) < 0) {
  665. wpa_printf(MSG_ERROR, "Enabling of interface failed");
  666. return -1;
  667. }
  668. return 0;
  669. }
  670. static int hostapd_ctrl_iface_reload(struct hostapd_iface *iface)
  671. {
  672. if (hostapd_reload_iface(iface) < 0) {
  673. wpa_printf(MSG_ERROR, "Reloading of interface failed");
  674. return -1;
  675. }
  676. return 0;
  677. }
  678. static int hostapd_ctrl_iface_disable(struct hostapd_iface *iface)
  679. {
  680. if (hostapd_disable_iface(iface) < 0) {
  681. wpa_printf(MSG_ERROR, "Disabling of interface failed");
  682. return -1;
  683. }
  684. return 0;
  685. }
  686. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  687. void *sock_ctx)
  688. {
  689. struct hostapd_data *hapd = eloop_ctx;
  690. char buf[256];
  691. int res;
  692. struct sockaddr_un from;
  693. socklen_t fromlen = sizeof(from);
  694. char *reply;
  695. const int reply_size = 4096;
  696. int reply_len;
  697. int level = MSG_DEBUG;
  698. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  699. (struct sockaddr *) &from, &fromlen);
  700. if (res < 0) {
  701. perror("recvfrom(ctrl_iface)");
  702. return;
  703. }
  704. buf[res] = '\0';
  705. if (os_strcmp(buf, "PING") == 0)
  706. level = MSG_EXCESSIVE;
  707. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  708. reply = os_malloc(reply_size);
  709. if (reply == NULL) {
  710. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  711. fromlen);
  712. return;
  713. }
  714. os_memcpy(reply, "OK\n", 3);
  715. reply_len = 3;
  716. if (os_strcmp(buf, "PING") == 0) {
  717. os_memcpy(reply, "PONG\n", 5);
  718. reply_len = 5;
  719. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  720. if (wpa_debug_reopen_file() < 0)
  721. reply_len = -1;
  722. } else if (os_strcmp(buf, "MIB") == 0) {
  723. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  724. if (reply_len >= 0) {
  725. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  726. reply_size - reply_len);
  727. if (res < 0)
  728. reply_len = -1;
  729. else
  730. reply_len += res;
  731. }
  732. if (reply_len >= 0) {
  733. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  734. reply_size - reply_len);
  735. if (res < 0)
  736. reply_len = -1;
  737. else
  738. reply_len += res;
  739. }
  740. #ifndef CONFIG_NO_RADIUS
  741. if (reply_len >= 0) {
  742. res = radius_client_get_mib(hapd->radius,
  743. reply + reply_len,
  744. reply_size - reply_len);
  745. if (res < 0)
  746. reply_len = -1;
  747. else
  748. reply_len += res;
  749. }
  750. #endif /* CONFIG_NO_RADIUS */
  751. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  752. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  753. reply_size);
  754. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  755. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  756. reply_size);
  757. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  758. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  759. reply_size);
  760. } else if (os_strcmp(buf, "ATTACH") == 0) {
  761. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  762. reply_len = -1;
  763. } else if (os_strcmp(buf, "DETACH") == 0) {
  764. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  765. reply_len = -1;
  766. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  767. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  768. buf + 6))
  769. reply_len = -1;
  770. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  771. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  772. reply_len = -1;
  773. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  774. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  775. reply_len = -1;
  776. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  777. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  778. reply_len = -1;
  779. #ifdef CONFIG_IEEE80211W
  780. #ifdef NEED_AP_MLME
  781. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  782. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  783. reply_len = -1;
  784. #endif /* NEED_AP_MLME */
  785. #endif /* CONFIG_IEEE80211W */
  786. #ifdef CONFIG_WPS
  787. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  788. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  789. reply_len = -1;
  790. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  791. reply_len = hostapd_ctrl_iface_wps_check_pin(
  792. hapd, buf + 14, reply, reply_size);
  793. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  794. if (hostapd_wps_button_pushed(hapd, NULL))
  795. reply_len = -1;
  796. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  797. if (hostapd_wps_cancel(hapd))
  798. reply_len = -1;
  799. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  800. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  801. reply, reply_size);
  802. } else if (os_strncmp(buf, "WPS_CONFIG ", 11) == 0) {
  803. if (hostapd_ctrl_iface_wps_config(hapd, buf + 11) < 0)
  804. reply_len = -1;
  805. #ifdef CONFIG_WPS_NFC
  806. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  807. if (hostapd_ctrl_iface_wps_nfc_tag_read(hapd, buf + 17))
  808. reply_len = -1;
  809. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  810. reply_len = hostapd_ctrl_iface_wps_nfc_config_token(
  811. hapd, buf + 21, reply, reply_size);
  812. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  813. reply_len = hostapd_ctrl_iface_wps_nfc_token(
  814. hapd, buf + 14, reply, reply_size);
  815. #endif /* CONFIG_WPS_NFC */
  816. #endif /* CONFIG_WPS */
  817. #ifdef CONFIG_WNM
  818. } else if (os_strncmp(buf, "DISASSOC_IMMINENT ", 18) == 0) {
  819. if (hostapd_ctrl_iface_disassoc_imminent(hapd, buf + 18))
  820. reply_len = -1;
  821. } else if (os_strncmp(buf, "ESS_DISASSOC ", 13) == 0) {
  822. if (hostapd_ctrl_iface_ess_disassoc(hapd, buf + 13))
  823. reply_len = -1;
  824. #endif /* CONFIG_WNM */
  825. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  826. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  827. reply_size);
  828. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  829. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  830. reply_len = -1;
  831. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  832. reply_len = hostapd_ctrl_iface_get(hapd, buf + 4, reply,
  833. reply_size);
  834. } else if (os_strncmp(buf, "ENABLE", 6) == 0) {
  835. if (hostapd_ctrl_iface_enable(hapd->iface))
  836. reply_len = -1;
  837. } else if (os_strncmp(buf, "RELOAD", 6) == 0) {
  838. if (hostapd_ctrl_iface_reload(hapd->iface))
  839. reply_len = -1;
  840. } else if (os_strncmp(buf, "DISABLE", 7) == 0) {
  841. if (hostapd_ctrl_iface_disable(hapd->iface))
  842. reply_len = -1;
  843. } else {
  844. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  845. reply_len = 16;
  846. }
  847. if (reply_len < 0) {
  848. os_memcpy(reply, "FAIL\n", 5);
  849. reply_len = 5;
  850. }
  851. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  852. os_free(reply);
  853. }
  854. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  855. {
  856. char *buf;
  857. size_t len;
  858. if (hapd->conf->ctrl_interface == NULL)
  859. return NULL;
  860. len = os_strlen(hapd->conf->ctrl_interface) +
  861. os_strlen(hapd->conf->iface) + 2;
  862. buf = os_malloc(len);
  863. if (buf == NULL)
  864. return NULL;
  865. os_snprintf(buf, len, "%s/%s",
  866. hapd->conf->ctrl_interface, hapd->conf->iface);
  867. buf[len - 1] = '\0';
  868. return buf;
  869. }
  870. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  871. const char *txt, size_t len)
  872. {
  873. struct hostapd_data *hapd = ctx;
  874. if (hapd == NULL)
  875. return;
  876. hostapd_ctrl_iface_send(hapd, level, txt, len);
  877. }
  878. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  879. {
  880. struct sockaddr_un addr;
  881. int s = -1;
  882. char *fname = NULL;
  883. if (hapd->ctrl_sock > -1) {
  884. wpa_printf(MSG_DEBUG, "ctrl_iface already exists!");
  885. return 0;
  886. }
  887. if (hapd->conf->ctrl_interface == NULL)
  888. return 0;
  889. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  890. if (errno == EEXIST) {
  891. wpa_printf(MSG_DEBUG, "Using existing control "
  892. "interface directory.");
  893. } else {
  894. perror("mkdir[ctrl_interface]");
  895. goto fail;
  896. }
  897. }
  898. if (hapd->conf->ctrl_interface_gid_set &&
  899. chown(hapd->conf->ctrl_interface, -1,
  900. hapd->conf->ctrl_interface_gid) < 0) {
  901. perror("chown[ctrl_interface]");
  902. return -1;
  903. }
  904. #ifdef ANDROID
  905. /*
  906. * Android is using umask 0077 which would leave the control interface
  907. * directory without group access. This breaks things since Wi-Fi
  908. * framework assumes that this directory can be accessed by other
  909. * applications in the wifi group. Fix this by adding group access even
  910. * if umask value would prevent this.
  911. */
  912. if (chmod(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  913. wpa_printf(MSG_ERROR, "CTRL: Could not chmod directory: %s",
  914. strerror(errno));
  915. /* Try to continue anyway */
  916. }
  917. #endif /* ANDROID */
  918. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  919. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  920. goto fail;
  921. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  922. if (s < 0) {
  923. perror("socket(PF_UNIX)");
  924. goto fail;
  925. }
  926. os_memset(&addr, 0, sizeof(addr));
  927. #ifdef __FreeBSD__
  928. addr.sun_len = sizeof(addr);
  929. #endif /* __FreeBSD__ */
  930. addr.sun_family = AF_UNIX;
  931. fname = hostapd_ctrl_iface_path(hapd);
  932. if (fname == NULL)
  933. goto fail;
  934. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  935. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  936. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  937. strerror(errno));
  938. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  939. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  940. " allow connections - assuming it was left"
  941. "over from forced program termination");
  942. if (unlink(fname) < 0) {
  943. perror("unlink[ctrl_iface]");
  944. wpa_printf(MSG_ERROR, "Could not unlink "
  945. "existing ctrl_iface socket '%s'",
  946. fname);
  947. goto fail;
  948. }
  949. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  950. 0) {
  951. perror("hostapd-ctrl-iface: bind(PF_UNIX)");
  952. goto fail;
  953. }
  954. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  955. "ctrl_iface socket '%s'", fname);
  956. } else {
  957. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  958. "be in use - cannot override it");
  959. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  960. "not used anymore", fname);
  961. os_free(fname);
  962. fname = NULL;
  963. goto fail;
  964. }
  965. }
  966. if (hapd->conf->ctrl_interface_gid_set &&
  967. chown(fname, -1, hapd->conf->ctrl_interface_gid) < 0) {
  968. perror("chown[ctrl_interface/ifname]");
  969. goto fail;
  970. }
  971. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  972. perror("chmod[ctrl_interface/ifname]");
  973. goto fail;
  974. }
  975. os_free(fname);
  976. hapd->ctrl_sock = s;
  977. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  978. NULL);
  979. hapd->msg_ctx = hapd;
  980. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  981. return 0;
  982. fail:
  983. if (s >= 0)
  984. close(s);
  985. if (fname) {
  986. unlink(fname);
  987. os_free(fname);
  988. }
  989. return -1;
  990. }
  991. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  992. {
  993. struct wpa_ctrl_dst *dst, *prev;
  994. if (hapd->ctrl_sock > -1) {
  995. char *fname;
  996. eloop_unregister_read_sock(hapd->ctrl_sock);
  997. close(hapd->ctrl_sock);
  998. hapd->ctrl_sock = -1;
  999. fname = hostapd_ctrl_iface_path(hapd);
  1000. if (fname)
  1001. unlink(fname);
  1002. os_free(fname);
  1003. if (hapd->conf->ctrl_interface &&
  1004. rmdir(hapd->conf->ctrl_interface) < 0) {
  1005. if (errno == ENOTEMPTY) {
  1006. wpa_printf(MSG_DEBUG, "Control interface "
  1007. "directory not empty - leaving it "
  1008. "behind");
  1009. } else {
  1010. perror("rmdir[ctrl_interface]");
  1011. }
  1012. }
  1013. }
  1014. dst = hapd->ctrl_dst;
  1015. while (dst) {
  1016. prev = dst;
  1017. dst = dst->next;
  1018. os_free(prev);
  1019. }
  1020. }
  1021. static int hostapd_ctrl_iface_add(struct hapd_interfaces *interfaces,
  1022. char *buf)
  1023. {
  1024. if (hostapd_add_iface(interfaces, buf) < 0) {
  1025. wpa_printf(MSG_ERROR, "Adding interface %s failed", buf);
  1026. return -1;
  1027. }
  1028. return 0;
  1029. }
  1030. static int hostapd_ctrl_iface_remove(struct hapd_interfaces *interfaces,
  1031. char *buf)
  1032. {
  1033. if (hostapd_remove_iface(interfaces, buf) < 0) {
  1034. wpa_printf(MSG_ERROR, "Removing interface %s failed", buf);
  1035. return -1;
  1036. }
  1037. return 0;
  1038. }
  1039. static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
  1040. void *sock_ctx)
  1041. {
  1042. void *interfaces = eloop_ctx;
  1043. char buf[256];
  1044. int res;
  1045. struct sockaddr_un from;
  1046. socklen_t fromlen = sizeof(from);
  1047. char reply[24];
  1048. int reply_len;
  1049. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  1050. (struct sockaddr *) &from, &fromlen);
  1051. if (res < 0) {
  1052. perror("recvfrom(ctrl_iface)");
  1053. return;
  1054. }
  1055. buf[res] = '\0';
  1056. os_memcpy(reply, "OK\n", 3);
  1057. reply_len = 3;
  1058. if (os_strcmp(buf, "PING") == 0) {
  1059. os_memcpy(reply, "PONG\n", 5);
  1060. reply_len = 5;
  1061. } else if (os_strncmp(buf, "ADD ", 4) == 0) {
  1062. if (hostapd_ctrl_iface_add(interfaces, buf + 4) < 0)
  1063. reply_len = -1;
  1064. } else if (os_strncmp(buf, "REMOVE ", 7) == 0) {
  1065. if (hostapd_ctrl_iface_remove(interfaces, buf + 7) < 0)
  1066. reply_len = -1;
  1067. } else {
  1068. wpa_printf(MSG_DEBUG, "Unrecognized global ctrl_iface command "
  1069. "ignored");
  1070. reply_len = -1;
  1071. }
  1072. if (reply_len < 0) {
  1073. os_memcpy(reply, "FAIL\n", 5);
  1074. reply_len = 5;
  1075. }
  1076. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  1077. }
  1078. static char * hostapd_global_ctrl_iface_path(struct hapd_interfaces *interface)
  1079. {
  1080. char *buf;
  1081. size_t len;
  1082. if (interface->global_iface_path == NULL)
  1083. return NULL;
  1084. len = os_strlen(interface->global_iface_path) +
  1085. os_strlen(interface->global_iface_name) + 2;
  1086. buf = os_malloc(len);
  1087. if (buf == NULL)
  1088. return NULL;
  1089. os_snprintf(buf, len, "%s/%s", interface->global_iface_path,
  1090. interface->global_iface_name);
  1091. buf[len - 1] = '\0';
  1092. return buf;
  1093. }
  1094. int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
  1095. {
  1096. struct sockaddr_un addr;
  1097. int s = -1;
  1098. char *fname = NULL;
  1099. if (interface->global_iface_path == NULL) {
  1100. wpa_printf(MSG_DEBUG, "ctrl_iface not configured!");
  1101. return 0;
  1102. }
  1103. if (mkdir(interface->global_iface_path, S_IRWXU | S_IRWXG) < 0) {
  1104. if (errno == EEXIST) {
  1105. wpa_printf(MSG_DEBUG, "Using existing control "
  1106. "interface directory.");
  1107. } else {
  1108. perror("mkdir[ctrl_interface]");
  1109. goto fail;
  1110. }
  1111. }
  1112. if (os_strlen(interface->global_iface_path) + 1 +
  1113. os_strlen(interface->global_iface_name) >= sizeof(addr.sun_path))
  1114. goto fail;
  1115. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  1116. if (s < 0) {
  1117. perror("socket(PF_UNIX)");
  1118. goto fail;
  1119. }
  1120. os_memset(&addr, 0, sizeof(addr));
  1121. #ifdef __FreeBSD__
  1122. addr.sun_len = sizeof(addr);
  1123. #endif /* __FreeBSD__ */
  1124. addr.sun_family = AF_UNIX;
  1125. fname = hostapd_global_ctrl_iface_path(interface);
  1126. if (fname == NULL)
  1127. goto fail;
  1128. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  1129. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1130. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  1131. strerror(errno));
  1132. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  1133. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  1134. " allow connections - assuming it was left"
  1135. "over from forced program termination");
  1136. if (unlink(fname) < 0) {
  1137. perror("unlink[ctrl_iface]");
  1138. wpa_printf(MSG_ERROR, "Could not unlink "
  1139. "existing ctrl_iface socket '%s'",
  1140. fname);
  1141. goto fail;
  1142. }
  1143. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  1144. 0) {
  1145. perror("bind(PF_UNIX)");
  1146. goto fail;
  1147. }
  1148. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  1149. "ctrl_iface socket '%s'", fname);
  1150. } else {
  1151. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  1152. "be in use - cannot override it");
  1153. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  1154. "not used anymore", fname);
  1155. os_free(fname);
  1156. fname = NULL;
  1157. goto fail;
  1158. }
  1159. }
  1160. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  1161. perror("chmod[ctrl_interface/ifname]");
  1162. goto fail;
  1163. }
  1164. os_free(fname);
  1165. interface->global_ctrl_sock = s;
  1166. eloop_register_read_sock(s, hostapd_global_ctrl_iface_receive,
  1167. interface, NULL);
  1168. return 0;
  1169. fail:
  1170. if (s >= 0)
  1171. close(s);
  1172. if (fname) {
  1173. unlink(fname);
  1174. os_free(fname);
  1175. }
  1176. return -1;
  1177. }
  1178. void hostapd_global_ctrl_iface_deinit(struct hapd_interfaces *interfaces)
  1179. {
  1180. char *fname = NULL;
  1181. if (interfaces->global_ctrl_sock > -1) {
  1182. eloop_unregister_read_sock(interfaces->global_ctrl_sock);
  1183. close(interfaces->global_ctrl_sock);
  1184. interfaces->global_ctrl_sock = -1;
  1185. fname = hostapd_global_ctrl_iface_path(interfaces);
  1186. if (fname) {
  1187. unlink(fname);
  1188. os_free(fname);
  1189. }
  1190. if (interfaces->global_iface_path &&
  1191. rmdir(interfaces->global_iface_path) < 0) {
  1192. if (errno == ENOTEMPTY) {
  1193. wpa_printf(MSG_DEBUG, "Control interface "
  1194. "directory not empty - leaving it "
  1195. "behind");
  1196. } else {
  1197. perror("rmdir[ctrl_interface]");
  1198. }
  1199. }
  1200. os_free(interfaces->global_iface_path);
  1201. interfaces->global_iface_path = NULL;
  1202. }
  1203. }
  1204. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  1205. const char *buf, size_t len)
  1206. {
  1207. struct wpa_ctrl_dst *dst, *next;
  1208. struct msghdr msg;
  1209. int idx;
  1210. struct iovec io[2];
  1211. char levelstr[10];
  1212. dst = hapd->ctrl_dst;
  1213. if (hapd->ctrl_sock < 0 || dst == NULL)
  1214. return;
  1215. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  1216. io[0].iov_base = levelstr;
  1217. io[0].iov_len = os_strlen(levelstr);
  1218. io[1].iov_base = (char *) buf;
  1219. io[1].iov_len = len;
  1220. os_memset(&msg, 0, sizeof(msg));
  1221. msg.msg_iov = io;
  1222. msg.msg_iovlen = 2;
  1223. idx = 0;
  1224. while (dst) {
  1225. next = dst->next;
  1226. if (level >= dst->debug_level) {
  1227. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  1228. (u8 *) dst->addr.sun_path, dst->addrlen -
  1229. offsetof(struct sockaddr_un, sun_path));
  1230. msg.msg_name = &dst->addr;
  1231. msg.msg_namelen = dst->addrlen;
  1232. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  1233. int _errno = errno;
  1234. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  1235. "%d - %s",
  1236. idx, errno, strerror(errno));
  1237. dst->errors++;
  1238. if (dst->errors > 10 || _errno == ENOENT) {
  1239. hostapd_ctrl_iface_detach(
  1240. hapd, &dst->addr,
  1241. dst->addrlen);
  1242. }
  1243. } else
  1244. dst->errors = 0;
  1245. }
  1246. idx++;
  1247. dst = next;
  1248. }
  1249. }
  1250. #endif /* CONFIG_NATIVE_WINDOWS */