ctrl_iface.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include <sys/un.h>
  17. #include <sys/stat.h>
  18. #include <stddef.h>
  19. #include "utils/common.h"
  20. #include "utils/eloop.h"
  21. #include "common/ieee802_11_defs.h"
  22. #include "drivers/driver.h"
  23. #include "radius/radius_client.h"
  24. #include "ap/hostapd.h"
  25. #include "ap/ap_config.h"
  26. #include "ap/ieee802_1x.h"
  27. #include "ap/wpa_auth.h"
  28. #include "ap/ieee802_11.h"
  29. #include "ap/sta_info.h"
  30. #include "ap/accounting.h"
  31. #include "ap/wps_hostapd.h"
  32. #include "ap/ctrl_iface_ap.h"
  33. #include "wps/wps_defs.h"
  34. #include "ctrl_iface.h"
  35. struct wpa_ctrl_dst {
  36. struct wpa_ctrl_dst *next;
  37. struct sockaddr_un addr;
  38. socklen_t addrlen;
  39. int debug_level;
  40. int errors;
  41. };
  42. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  43. const char *buf, size_t len);
  44. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  45. struct sockaddr_un *from,
  46. socklen_t fromlen)
  47. {
  48. struct wpa_ctrl_dst *dst;
  49. dst = os_zalloc(sizeof(*dst));
  50. if (dst == NULL)
  51. return -1;
  52. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  53. dst->addrlen = fromlen;
  54. dst->debug_level = MSG_INFO;
  55. dst->next = hapd->ctrl_dst;
  56. hapd->ctrl_dst = dst;
  57. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  58. (u8 *) from->sun_path,
  59. fromlen - offsetof(struct sockaddr_un, sun_path));
  60. return 0;
  61. }
  62. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  63. struct sockaddr_un *from,
  64. socklen_t fromlen)
  65. {
  66. struct wpa_ctrl_dst *dst, *prev = NULL;
  67. dst = hapd->ctrl_dst;
  68. while (dst) {
  69. if (fromlen == dst->addrlen &&
  70. os_memcmp(from->sun_path, dst->addr.sun_path,
  71. fromlen - offsetof(struct sockaddr_un, sun_path))
  72. == 0) {
  73. if (prev == NULL)
  74. hapd->ctrl_dst = dst->next;
  75. else
  76. prev->next = dst->next;
  77. os_free(dst);
  78. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  79. (u8 *) from->sun_path,
  80. fromlen -
  81. offsetof(struct sockaddr_un, sun_path));
  82. return 0;
  83. }
  84. prev = dst;
  85. dst = dst->next;
  86. }
  87. return -1;
  88. }
  89. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  90. struct sockaddr_un *from,
  91. socklen_t fromlen,
  92. char *level)
  93. {
  94. struct wpa_ctrl_dst *dst;
  95. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  96. dst = hapd->ctrl_dst;
  97. while (dst) {
  98. if (fromlen == dst->addrlen &&
  99. os_memcmp(from->sun_path, dst->addr.sun_path,
  100. fromlen - offsetof(struct sockaddr_un, sun_path))
  101. == 0) {
  102. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  103. "level", (u8 *) from->sun_path, fromlen -
  104. offsetof(struct sockaddr_un, sun_path));
  105. dst->debug_level = atoi(level);
  106. return 0;
  107. }
  108. dst = dst->next;
  109. }
  110. return -1;
  111. }
  112. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  113. const char *txtaddr)
  114. {
  115. u8 addr[ETH_ALEN];
  116. struct sta_info *sta;
  117. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  118. if (hwaddr_aton(txtaddr, addr))
  119. return -1;
  120. sta = ap_get_sta(hapd, addr);
  121. if (sta)
  122. return 0;
  123. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  124. "notification", MAC2STR(addr));
  125. sta = ap_sta_add(hapd, addr);
  126. if (sta == NULL)
  127. return -1;
  128. hostapd_new_assoc_sta(hapd, sta, 0);
  129. return 0;
  130. }
  131. #ifdef CONFIG_P2P_MANAGER
  132. static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
  133. u8 minor_reason_code, const u8 *addr)
  134. {
  135. struct ieee80211_mgmt *mgmt;
  136. int ret;
  137. u8 *pos;
  138. if (hapd->driver->send_frame == NULL)
  139. return -1;
  140. mgmt = os_zalloc(sizeof(*mgmt) + 100);
  141. if (mgmt == NULL)
  142. return -1;
  143. wpa_printf(MSG_DEBUG, "P2P: Disconnect STA " MACSTR " with minor "
  144. "reason code %u (stype=%u)",
  145. MAC2STR(addr), minor_reason_code, stype);
  146. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
  147. os_memcpy(mgmt->da, addr, ETH_ALEN);
  148. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  149. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  150. if (stype == WLAN_FC_STYPE_DEAUTH) {
  151. mgmt->u.deauth.reason_code =
  152. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  153. pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
  154. } else {
  155. mgmt->u.disassoc.reason_code =
  156. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  157. pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
  158. }
  159. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  160. *pos++ = 4 + 3 + 1;
  161. WPA_PUT_BE24(pos, OUI_WFA);
  162. pos += 3;
  163. *pos++ = P2P_OUI_TYPE;
  164. *pos++ = P2P_ATTR_MINOR_REASON_CODE;
  165. WPA_PUT_LE16(pos, 1);
  166. pos += 2;
  167. *pos++ = minor_reason_code;
  168. ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
  169. pos - (u8 *) mgmt, 1);
  170. os_free(mgmt);
  171. return ret < 0 ? -1 : 0;
  172. }
  173. #endif /* CONFIG_P2P_MANAGER */
  174. static int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
  175. const char *txtaddr)
  176. {
  177. u8 addr[ETH_ALEN];
  178. struct sta_info *sta;
  179. const char *pos;
  180. wpa_printf(MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s", txtaddr);
  181. if (hwaddr_aton(txtaddr, addr))
  182. return -1;
  183. pos = os_strstr(txtaddr, " test=");
  184. if (pos) {
  185. struct ieee80211_mgmt mgmt;
  186. int encrypt;
  187. if (hapd->driver->send_frame == NULL)
  188. return -1;
  189. pos += 6;
  190. encrypt = atoi(pos);
  191. os_memset(&mgmt, 0, sizeof(mgmt));
  192. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  193. WLAN_FC_STYPE_DEAUTH);
  194. os_memcpy(mgmt.da, addr, ETH_ALEN);
  195. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  196. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  197. mgmt.u.deauth.reason_code =
  198. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  199. if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
  200. IEEE80211_HDRLEN +
  201. sizeof(mgmt.u.deauth),
  202. encrypt) < 0)
  203. return -1;
  204. return 0;
  205. }
  206. #ifdef CONFIG_P2P_MANAGER
  207. pos = os_strstr(txtaddr, " p2p=");
  208. if (pos) {
  209. return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
  210. atoi(pos + 5), addr);
  211. }
  212. #endif /* CONFIG_P2P_MANAGER */
  213. hapd->drv.sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  214. sta = ap_get_sta(hapd, addr);
  215. if (sta)
  216. ap_sta_deauthenticate(hapd, sta,
  217. WLAN_REASON_PREV_AUTH_NOT_VALID);
  218. return 0;
  219. }
  220. static int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
  221. const char *txtaddr)
  222. {
  223. u8 addr[ETH_ALEN];
  224. struct sta_info *sta;
  225. const char *pos;
  226. wpa_printf(MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s", txtaddr);
  227. if (hwaddr_aton(txtaddr, addr))
  228. return -1;
  229. pos = os_strstr(txtaddr, " test=");
  230. if (pos) {
  231. struct ieee80211_mgmt mgmt;
  232. int encrypt;
  233. if (hapd->driver->send_frame == NULL)
  234. return -1;
  235. pos += 6;
  236. encrypt = atoi(pos);
  237. os_memset(&mgmt, 0, sizeof(mgmt));
  238. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  239. WLAN_FC_STYPE_DISASSOC);
  240. os_memcpy(mgmt.da, addr, ETH_ALEN);
  241. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  242. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  243. mgmt.u.disassoc.reason_code =
  244. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  245. if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
  246. IEEE80211_HDRLEN +
  247. sizeof(mgmt.u.deauth),
  248. encrypt) < 0)
  249. return -1;
  250. return 0;
  251. }
  252. #ifdef CONFIG_P2P_MANAGER
  253. pos = os_strstr(txtaddr, " p2p=");
  254. if (pos) {
  255. return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
  256. atoi(pos + 5), addr);
  257. }
  258. #endif /* CONFIG_P2P_MANAGER */
  259. hapd->drv.sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  260. sta = ap_get_sta(hapd, addr);
  261. if (sta)
  262. ap_sta_disassociate(hapd, sta,
  263. WLAN_REASON_PREV_AUTH_NOT_VALID);
  264. return 0;
  265. }
  266. #ifdef CONFIG_IEEE80211W
  267. #ifdef NEED_AP_MLME
  268. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  269. const char *txtaddr)
  270. {
  271. u8 addr[ETH_ALEN];
  272. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  273. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  274. if (hwaddr_aton(txtaddr, addr) ||
  275. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  276. return -1;
  277. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  278. return 0;
  279. }
  280. #endif /* NEED_AP_MLME */
  281. #endif /* CONFIG_IEEE80211W */
  282. #ifdef CONFIG_WPS
  283. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  284. {
  285. char *pin = os_strchr(txt, ' ');
  286. char *timeout_txt;
  287. int timeout;
  288. u8 addr_buf[ETH_ALEN], *addr = NULL;
  289. char *pos;
  290. if (pin == NULL)
  291. return -1;
  292. *pin++ = '\0';
  293. timeout_txt = os_strchr(pin, ' ');
  294. if (timeout_txt) {
  295. *timeout_txt++ = '\0';
  296. timeout = atoi(timeout_txt);
  297. pos = os_strchr(timeout_txt, ' ');
  298. if (pos) {
  299. *pos++ = '\0';
  300. if (hwaddr_aton(pos, addr_buf) == 0)
  301. addr = addr_buf;
  302. }
  303. } else
  304. timeout = 0;
  305. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  306. }
  307. #ifdef CONFIG_WPS_OOB
  308. static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
  309. {
  310. char *path, *method, *name;
  311. path = os_strchr(txt, ' ');
  312. if (path == NULL)
  313. return -1;
  314. *path++ = '\0';
  315. method = os_strchr(path, ' ');
  316. if (method == NULL)
  317. return -1;
  318. *method++ = '\0';
  319. name = os_strchr(method, ' ');
  320. if (name != NULL)
  321. *name++ = '\0';
  322. return hostapd_wps_start_oob(hapd, txt, path, method, name);
  323. }
  324. #endif /* CONFIG_WPS_OOB */
  325. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  326. char *buf, size_t buflen)
  327. {
  328. int timeout = 300;
  329. char *pos;
  330. const char *pin_txt;
  331. pos = os_strchr(txt, ' ');
  332. if (pos)
  333. *pos++ = '\0';
  334. if (os_strcmp(txt, "disable") == 0) {
  335. hostapd_wps_ap_pin_disable(hapd);
  336. return os_snprintf(buf, buflen, "OK\n");
  337. }
  338. if (os_strcmp(txt, "random") == 0) {
  339. if (pos)
  340. timeout = atoi(pos);
  341. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  342. if (pin_txt == NULL)
  343. return -1;
  344. return os_snprintf(buf, buflen, "%s", pin_txt);
  345. }
  346. if (os_strcmp(txt, "get") == 0) {
  347. pin_txt = hostapd_wps_ap_pin_get(hapd);
  348. if (pin_txt == NULL)
  349. return -1;
  350. return os_snprintf(buf, buflen, "%s", pin_txt);
  351. }
  352. if (os_strcmp(txt, "set") == 0) {
  353. char *pin;
  354. if (pos == NULL)
  355. return -1;
  356. pin = pos;
  357. pos = os_strchr(pos, ' ');
  358. if (pos) {
  359. *pos++ = '\0';
  360. timeout = atoi(pos);
  361. }
  362. if (os_strlen(pin) > buflen)
  363. return -1;
  364. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  365. return -1;
  366. return os_snprintf(buf, buflen, "%s", pin);
  367. }
  368. return -1;
  369. }
  370. #endif /* CONFIG_WPS */
  371. static int hostapd_ctrl_iface_set(struct hostapd_data *wpa_s, char *cmd)
  372. {
  373. char *value;
  374. int ret = 0;
  375. value = os_strchr(cmd, ' ');
  376. if (value == NULL)
  377. return -1;
  378. *value++ = '\0';
  379. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  380. if (0) {
  381. #ifdef CONFIG_WPS_TESTING
  382. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  383. long int val;
  384. val = strtol(value, NULL, 0);
  385. if (val < 0 || val > 0xff) {
  386. ret = -1;
  387. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  388. "wps_version_number %ld", val);
  389. } else {
  390. wps_version_number = val;
  391. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  392. "version %u.%u",
  393. (wps_version_number & 0xf0) >> 4,
  394. wps_version_number & 0x0f);
  395. }
  396. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  397. wps_testing_dummy_cred = atoi(value);
  398. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  399. wps_testing_dummy_cred);
  400. #endif /* CONFIG_WPS_TESTING */
  401. } else {
  402. ret = -1;
  403. }
  404. return ret;
  405. }
  406. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  407. void *sock_ctx)
  408. {
  409. struct hostapd_data *hapd = eloop_ctx;
  410. char buf[256];
  411. int res;
  412. struct sockaddr_un from;
  413. socklen_t fromlen = sizeof(from);
  414. char *reply;
  415. const int reply_size = 4096;
  416. int reply_len;
  417. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  418. (struct sockaddr *) &from, &fromlen);
  419. if (res < 0) {
  420. perror("recvfrom(ctrl_iface)");
  421. return;
  422. }
  423. buf[res] = '\0';
  424. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
  425. reply = os_malloc(reply_size);
  426. if (reply == NULL) {
  427. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  428. fromlen);
  429. return;
  430. }
  431. os_memcpy(reply, "OK\n", 3);
  432. reply_len = 3;
  433. if (os_strcmp(buf, "PING") == 0) {
  434. os_memcpy(reply, "PONG\n", 5);
  435. reply_len = 5;
  436. } else if (os_strcmp(buf, "MIB") == 0) {
  437. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  438. if (reply_len >= 0) {
  439. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  440. reply_size - reply_len);
  441. if (res < 0)
  442. reply_len = -1;
  443. else
  444. reply_len += res;
  445. }
  446. if (reply_len >= 0) {
  447. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  448. reply_size - reply_len);
  449. if (res < 0)
  450. reply_len = -1;
  451. else
  452. reply_len += res;
  453. }
  454. #ifndef CONFIG_NO_RADIUS
  455. if (reply_len >= 0) {
  456. res = radius_client_get_mib(hapd->radius,
  457. reply + reply_len,
  458. reply_size - reply_len);
  459. if (res < 0)
  460. reply_len = -1;
  461. else
  462. reply_len += res;
  463. }
  464. #endif /* CONFIG_NO_RADIUS */
  465. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  466. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  467. reply_size);
  468. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  469. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  470. reply_size);
  471. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  472. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  473. reply_size);
  474. } else if (os_strcmp(buf, "ATTACH") == 0) {
  475. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  476. reply_len = -1;
  477. } else if (os_strcmp(buf, "DETACH") == 0) {
  478. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  479. reply_len = -1;
  480. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  481. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  482. buf + 6))
  483. reply_len = -1;
  484. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  485. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  486. reply_len = -1;
  487. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  488. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  489. reply_len = -1;
  490. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  491. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  492. reply_len = -1;
  493. #ifdef CONFIG_IEEE80211W
  494. #ifdef NEED_AP_MLME
  495. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  496. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  497. reply_len = -1;
  498. #endif /* NEED_AP_MLME */
  499. #endif /* CONFIG_IEEE80211W */
  500. #ifdef CONFIG_WPS
  501. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  502. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  503. reply_len = -1;
  504. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  505. if (hostapd_wps_button_pushed(hapd))
  506. reply_len = -1;
  507. #ifdef CONFIG_WPS_OOB
  508. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  509. if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
  510. reply_len = -1;
  511. #endif /* CONFIG_WPS_OOB */
  512. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  513. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  514. reply, reply_size);
  515. #endif /* CONFIG_WPS */
  516. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  517. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  518. reply_len = -1;
  519. } else {
  520. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  521. reply_len = 16;
  522. }
  523. if (reply_len < 0) {
  524. os_memcpy(reply, "FAIL\n", 5);
  525. reply_len = 5;
  526. }
  527. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  528. os_free(reply);
  529. }
  530. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  531. {
  532. char *buf;
  533. size_t len;
  534. if (hapd->conf->ctrl_interface == NULL)
  535. return NULL;
  536. len = os_strlen(hapd->conf->ctrl_interface) +
  537. os_strlen(hapd->conf->iface) + 2;
  538. buf = os_malloc(len);
  539. if (buf == NULL)
  540. return NULL;
  541. os_snprintf(buf, len, "%s/%s",
  542. hapd->conf->ctrl_interface, hapd->conf->iface);
  543. buf[len - 1] = '\0';
  544. return buf;
  545. }
  546. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  547. const char *txt, size_t len)
  548. {
  549. struct hostapd_data *hapd = ctx;
  550. if (hapd == NULL)
  551. return;
  552. hostapd_ctrl_iface_send(hapd, level, txt, len);
  553. }
  554. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  555. {
  556. struct sockaddr_un addr;
  557. int s = -1;
  558. char *fname = NULL;
  559. hapd->ctrl_sock = -1;
  560. if (hapd->conf->ctrl_interface == NULL)
  561. return 0;
  562. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  563. if (errno == EEXIST) {
  564. wpa_printf(MSG_DEBUG, "Using existing control "
  565. "interface directory.");
  566. } else {
  567. perror("mkdir[ctrl_interface]");
  568. goto fail;
  569. }
  570. }
  571. if (hapd->conf->ctrl_interface_gid_set &&
  572. chown(hapd->conf->ctrl_interface, 0,
  573. hapd->conf->ctrl_interface_gid) < 0) {
  574. perror("chown[ctrl_interface]");
  575. return -1;
  576. }
  577. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  578. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  579. goto fail;
  580. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  581. if (s < 0) {
  582. perror("socket(PF_UNIX)");
  583. goto fail;
  584. }
  585. os_memset(&addr, 0, sizeof(addr));
  586. #ifdef __FreeBSD__
  587. addr.sun_len = sizeof(addr);
  588. #endif /* __FreeBSD__ */
  589. addr.sun_family = AF_UNIX;
  590. fname = hostapd_ctrl_iface_path(hapd);
  591. if (fname == NULL)
  592. goto fail;
  593. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  594. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  595. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  596. strerror(errno));
  597. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  598. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  599. " allow connections - assuming it was left"
  600. "over from forced program termination");
  601. if (unlink(fname) < 0) {
  602. perror("unlink[ctrl_iface]");
  603. wpa_printf(MSG_ERROR, "Could not unlink "
  604. "existing ctrl_iface socket '%s'",
  605. fname);
  606. goto fail;
  607. }
  608. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  609. 0) {
  610. perror("bind(PF_UNIX)");
  611. goto fail;
  612. }
  613. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  614. "ctrl_iface socket '%s'", fname);
  615. } else {
  616. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  617. "be in use - cannot override it");
  618. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  619. "not used anymore", fname);
  620. os_free(fname);
  621. fname = NULL;
  622. goto fail;
  623. }
  624. }
  625. if (hapd->conf->ctrl_interface_gid_set &&
  626. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  627. perror("chown[ctrl_interface/ifname]");
  628. goto fail;
  629. }
  630. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  631. perror("chmod[ctrl_interface/ifname]");
  632. goto fail;
  633. }
  634. os_free(fname);
  635. hapd->ctrl_sock = s;
  636. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  637. NULL);
  638. hapd->msg_ctx = hapd;
  639. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  640. return 0;
  641. fail:
  642. if (s >= 0)
  643. close(s);
  644. if (fname) {
  645. unlink(fname);
  646. os_free(fname);
  647. }
  648. return -1;
  649. }
  650. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  651. {
  652. struct wpa_ctrl_dst *dst, *prev;
  653. if (hapd->ctrl_sock > -1) {
  654. char *fname;
  655. eloop_unregister_read_sock(hapd->ctrl_sock);
  656. close(hapd->ctrl_sock);
  657. hapd->ctrl_sock = -1;
  658. fname = hostapd_ctrl_iface_path(hapd);
  659. if (fname)
  660. unlink(fname);
  661. os_free(fname);
  662. if (hapd->conf->ctrl_interface &&
  663. rmdir(hapd->conf->ctrl_interface) < 0) {
  664. if (errno == ENOTEMPTY) {
  665. wpa_printf(MSG_DEBUG, "Control interface "
  666. "directory not empty - leaving it "
  667. "behind");
  668. } else {
  669. perror("rmdir[ctrl_interface]");
  670. }
  671. }
  672. }
  673. dst = hapd->ctrl_dst;
  674. while (dst) {
  675. prev = dst;
  676. dst = dst->next;
  677. os_free(prev);
  678. }
  679. }
  680. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  681. const char *buf, size_t len)
  682. {
  683. struct wpa_ctrl_dst *dst, *next;
  684. struct msghdr msg;
  685. int idx;
  686. struct iovec io[2];
  687. char levelstr[10];
  688. dst = hapd->ctrl_dst;
  689. if (hapd->ctrl_sock < 0 || dst == NULL)
  690. return;
  691. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  692. io[0].iov_base = levelstr;
  693. io[0].iov_len = os_strlen(levelstr);
  694. io[1].iov_base = (char *) buf;
  695. io[1].iov_len = len;
  696. os_memset(&msg, 0, sizeof(msg));
  697. msg.msg_iov = io;
  698. msg.msg_iovlen = 2;
  699. idx = 0;
  700. while (dst) {
  701. next = dst->next;
  702. if (level >= dst->debug_level) {
  703. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  704. (u8 *) dst->addr.sun_path, dst->addrlen -
  705. offsetof(struct sockaddr_un, sun_path));
  706. msg.msg_name = &dst->addr;
  707. msg.msg_namelen = dst->addrlen;
  708. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  709. int _errno = errno;
  710. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  711. "%d - %s",
  712. idx, errno, strerror(errno));
  713. dst->errors++;
  714. if (dst->errors > 10 || _errno == ENOENT) {
  715. hostapd_ctrl_iface_detach(
  716. hapd, &dst->addr,
  717. dst->addrlen);
  718. }
  719. } else
  720. dst->errors = 0;
  721. }
  722. idx++;
  723. dst = next;
  724. }
  725. }
  726. #endif /* CONFIG_NATIVE_WINDOWS */