ctrl_iface.c 35 KB

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