ctrl_iface.c 36 KB

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