ctrl_iface.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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 "wps/wps.h"
  35. #include "ctrl_iface.h"
  36. struct wpa_ctrl_dst {
  37. struct wpa_ctrl_dst *next;
  38. struct sockaddr_un addr;
  39. socklen_t addrlen;
  40. int debug_level;
  41. int errors;
  42. };
  43. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  44. const char *buf, size_t len);
  45. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  46. struct sockaddr_un *from,
  47. socklen_t fromlen)
  48. {
  49. struct wpa_ctrl_dst *dst;
  50. dst = os_zalloc(sizeof(*dst));
  51. if (dst == NULL)
  52. return -1;
  53. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  54. dst->addrlen = fromlen;
  55. dst->debug_level = MSG_INFO;
  56. dst->next = hapd->ctrl_dst;
  57. hapd->ctrl_dst = dst;
  58. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  59. (u8 *) from->sun_path,
  60. fromlen - offsetof(struct sockaddr_un, sun_path));
  61. return 0;
  62. }
  63. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  64. struct sockaddr_un *from,
  65. socklen_t fromlen)
  66. {
  67. struct wpa_ctrl_dst *dst, *prev = NULL;
  68. dst = hapd->ctrl_dst;
  69. while (dst) {
  70. if (fromlen == dst->addrlen &&
  71. os_memcmp(from->sun_path, dst->addr.sun_path,
  72. fromlen - offsetof(struct sockaddr_un, sun_path))
  73. == 0) {
  74. if (prev == NULL)
  75. hapd->ctrl_dst = dst->next;
  76. else
  77. prev->next = dst->next;
  78. os_free(dst);
  79. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  80. (u8 *) from->sun_path,
  81. fromlen -
  82. offsetof(struct sockaddr_un, sun_path));
  83. return 0;
  84. }
  85. prev = dst;
  86. dst = dst->next;
  87. }
  88. return -1;
  89. }
  90. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  91. struct sockaddr_un *from,
  92. socklen_t fromlen,
  93. char *level)
  94. {
  95. struct wpa_ctrl_dst *dst;
  96. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  97. dst = hapd->ctrl_dst;
  98. while (dst) {
  99. if (fromlen == dst->addrlen &&
  100. os_memcmp(from->sun_path, dst->addr.sun_path,
  101. fromlen - offsetof(struct sockaddr_un, sun_path))
  102. == 0) {
  103. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  104. "level", (u8 *) from->sun_path, fromlen -
  105. offsetof(struct sockaddr_un, sun_path));
  106. dst->debug_level = atoi(level);
  107. return 0;
  108. }
  109. dst = dst->next;
  110. }
  111. return -1;
  112. }
  113. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  114. const char *txtaddr)
  115. {
  116. u8 addr[ETH_ALEN];
  117. struct sta_info *sta;
  118. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  119. if (hwaddr_aton(txtaddr, addr))
  120. return -1;
  121. sta = ap_get_sta(hapd, addr);
  122. if (sta)
  123. return 0;
  124. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  125. "notification", MAC2STR(addr));
  126. sta = ap_sta_add(hapd, addr);
  127. if (sta == NULL)
  128. return -1;
  129. hostapd_new_assoc_sta(hapd, sta, 0);
  130. return 0;
  131. }
  132. #ifdef CONFIG_P2P_MANAGER
  133. static int p2p_manager_disconnect(struct hostapd_data *hapd, u16 stype,
  134. u8 minor_reason_code, const u8 *addr)
  135. {
  136. struct ieee80211_mgmt *mgmt;
  137. int ret;
  138. u8 *pos;
  139. if (hapd->driver->send_frame == NULL)
  140. return -1;
  141. mgmt = os_zalloc(sizeof(*mgmt) + 100);
  142. if (mgmt == NULL)
  143. return -1;
  144. wpa_printf(MSG_DEBUG, "P2P: Disconnect STA " MACSTR " with minor "
  145. "reason code %u (stype=%u)",
  146. MAC2STR(addr), minor_reason_code, stype);
  147. mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, stype);
  148. os_memcpy(mgmt->da, addr, ETH_ALEN);
  149. os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
  150. os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
  151. if (stype == WLAN_FC_STYPE_DEAUTH) {
  152. mgmt->u.deauth.reason_code =
  153. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  154. pos = (u8 *) (&mgmt->u.deauth.reason_code + 1);
  155. } else {
  156. mgmt->u.disassoc.reason_code =
  157. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  158. pos = (u8 *) (&mgmt->u.disassoc.reason_code + 1);
  159. }
  160. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  161. *pos++ = 4 + 3 + 1;
  162. WPA_PUT_BE24(pos, OUI_WFA);
  163. pos += 3;
  164. *pos++ = P2P_OUI_TYPE;
  165. *pos++ = P2P_ATTR_MINOR_REASON_CODE;
  166. WPA_PUT_LE16(pos, 1);
  167. pos += 2;
  168. *pos++ = minor_reason_code;
  169. ret = hapd->driver->send_frame(hapd->drv_priv, (u8 *) mgmt,
  170. pos - (u8 *) mgmt, 1);
  171. os_free(mgmt);
  172. return ret < 0 ? -1 : 0;
  173. }
  174. #endif /* CONFIG_P2P_MANAGER */
  175. static int hostapd_ctrl_iface_deauthenticate(struct hostapd_data *hapd,
  176. const char *txtaddr)
  177. {
  178. u8 addr[ETH_ALEN];
  179. struct sta_info *sta;
  180. const char *pos;
  181. wpa_printf(MSG_DEBUG, "CTRL_IFACE DEAUTHENTICATE %s", txtaddr);
  182. if (hwaddr_aton(txtaddr, addr))
  183. return -1;
  184. pos = os_strstr(txtaddr, " test=");
  185. if (pos) {
  186. struct ieee80211_mgmt mgmt;
  187. int encrypt;
  188. if (hapd->driver->send_frame == NULL)
  189. return -1;
  190. pos += 6;
  191. encrypt = atoi(pos);
  192. os_memset(&mgmt, 0, sizeof(mgmt));
  193. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  194. WLAN_FC_STYPE_DEAUTH);
  195. os_memcpy(mgmt.da, addr, ETH_ALEN);
  196. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  197. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  198. mgmt.u.deauth.reason_code =
  199. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  200. if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
  201. IEEE80211_HDRLEN +
  202. sizeof(mgmt.u.deauth),
  203. encrypt) < 0)
  204. return -1;
  205. return 0;
  206. }
  207. #ifdef CONFIG_P2P_MANAGER
  208. pos = os_strstr(txtaddr, " p2p=");
  209. if (pos) {
  210. return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DEAUTH,
  211. atoi(pos + 5), addr);
  212. }
  213. #endif /* CONFIG_P2P_MANAGER */
  214. hapd->drv.sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  215. sta = ap_get_sta(hapd, addr);
  216. if (sta)
  217. ap_sta_deauthenticate(hapd, sta,
  218. WLAN_REASON_PREV_AUTH_NOT_VALID);
  219. return 0;
  220. }
  221. static int hostapd_ctrl_iface_disassociate(struct hostapd_data *hapd,
  222. const char *txtaddr)
  223. {
  224. u8 addr[ETH_ALEN];
  225. struct sta_info *sta;
  226. const char *pos;
  227. wpa_printf(MSG_DEBUG, "CTRL_IFACE DISASSOCIATE %s", txtaddr);
  228. if (hwaddr_aton(txtaddr, addr))
  229. return -1;
  230. pos = os_strstr(txtaddr, " test=");
  231. if (pos) {
  232. struct ieee80211_mgmt mgmt;
  233. int encrypt;
  234. if (hapd->driver->send_frame == NULL)
  235. return -1;
  236. pos += 6;
  237. encrypt = atoi(pos);
  238. os_memset(&mgmt, 0, sizeof(mgmt));
  239. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  240. WLAN_FC_STYPE_DISASSOC);
  241. os_memcpy(mgmt.da, addr, ETH_ALEN);
  242. os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
  243. os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
  244. mgmt.u.disassoc.reason_code =
  245. host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
  246. if (hapd->driver->send_frame(hapd->drv_priv, (u8 *) &mgmt,
  247. IEEE80211_HDRLEN +
  248. sizeof(mgmt.u.deauth),
  249. encrypt) < 0)
  250. return -1;
  251. return 0;
  252. }
  253. #ifdef CONFIG_P2P_MANAGER
  254. pos = os_strstr(txtaddr, " p2p=");
  255. if (pos) {
  256. return p2p_manager_disconnect(hapd, WLAN_FC_STYPE_DISASSOC,
  257. atoi(pos + 5), addr);
  258. }
  259. #endif /* CONFIG_P2P_MANAGER */
  260. hapd->drv.sta_disassoc(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
  261. sta = ap_get_sta(hapd, addr);
  262. if (sta)
  263. ap_sta_disassociate(hapd, sta,
  264. WLAN_REASON_PREV_AUTH_NOT_VALID);
  265. return 0;
  266. }
  267. #ifdef CONFIG_IEEE80211W
  268. #ifdef NEED_AP_MLME
  269. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  270. const char *txtaddr)
  271. {
  272. u8 addr[ETH_ALEN];
  273. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  274. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  275. if (hwaddr_aton(txtaddr, addr) ||
  276. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0)
  277. return -1;
  278. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  279. return 0;
  280. }
  281. #endif /* NEED_AP_MLME */
  282. #endif /* CONFIG_IEEE80211W */
  283. #ifdef CONFIG_WPS
  284. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  285. {
  286. char *pin = os_strchr(txt, ' ');
  287. char *timeout_txt;
  288. int timeout;
  289. u8 addr_buf[ETH_ALEN], *addr = NULL;
  290. char *pos;
  291. if (pin == NULL)
  292. return -1;
  293. *pin++ = '\0';
  294. timeout_txt = os_strchr(pin, ' ');
  295. if (timeout_txt) {
  296. *timeout_txt++ = '\0';
  297. timeout = atoi(timeout_txt);
  298. pos = os_strchr(timeout_txt, ' ');
  299. if (pos) {
  300. *pos++ = '\0';
  301. if (hwaddr_aton(pos, addr_buf) == 0)
  302. addr = addr_buf;
  303. }
  304. } else
  305. timeout = 0;
  306. return hostapd_wps_add_pin(hapd, addr, txt, pin, timeout);
  307. }
  308. static int hostapd_ctrl_iface_wps_check_pin(
  309. struct hostapd_data *hapd, char *cmd, char *buf, size_t buflen)
  310. {
  311. char pin[9];
  312. size_t len;
  313. char *pos;
  314. int ret;
  315. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  316. (u8 *) cmd, os_strlen(cmd));
  317. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  318. if (*pos < '0' || *pos > '9')
  319. continue;
  320. pin[len++] = *pos;
  321. if (len == 9) {
  322. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  323. return -1;
  324. }
  325. }
  326. if (len != 4 && len != 8) {
  327. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  328. return -1;
  329. }
  330. pin[len] = '\0';
  331. if (len == 8) {
  332. unsigned int pin_val;
  333. pin_val = atoi(pin);
  334. if (!wps_pin_valid(pin_val)) {
  335. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  336. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  337. if (ret < 0 || (size_t) ret >= buflen)
  338. return -1;
  339. return ret;
  340. }
  341. }
  342. ret = os_snprintf(buf, buflen, "%s", pin);
  343. if (ret < 0 || (size_t) ret >= buflen)
  344. return -1;
  345. return ret;
  346. }
  347. #ifdef CONFIG_WPS_OOB
  348. static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
  349. {
  350. char *path, *method, *name;
  351. path = os_strchr(txt, ' ');
  352. if (path == NULL)
  353. return -1;
  354. *path++ = '\0';
  355. method = os_strchr(path, ' ');
  356. if (method == NULL)
  357. return -1;
  358. *method++ = '\0';
  359. name = os_strchr(method, ' ');
  360. if (name != NULL)
  361. *name++ = '\0';
  362. return hostapd_wps_start_oob(hapd, txt, path, method, name);
  363. }
  364. #endif /* CONFIG_WPS_OOB */
  365. static int hostapd_ctrl_iface_wps_ap_pin(struct hostapd_data *hapd, char *txt,
  366. char *buf, size_t buflen)
  367. {
  368. int timeout = 300;
  369. char *pos;
  370. const char *pin_txt;
  371. pos = os_strchr(txt, ' ');
  372. if (pos)
  373. *pos++ = '\0';
  374. if (os_strcmp(txt, "disable") == 0) {
  375. hostapd_wps_ap_pin_disable(hapd);
  376. return os_snprintf(buf, buflen, "OK\n");
  377. }
  378. if (os_strcmp(txt, "random") == 0) {
  379. if (pos)
  380. timeout = atoi(pos);
  381. pin_txt = hostapd_wps_ap_pin_random(hapd, timeout);
  382. if (pin_txt == NULL)
  383. return -1;
  384. return os_snprintf(buf, buflen, "%s", pin_txt);
  385. }
  386. if (os_strcmp(txt, "get") == 0) {
  387. pin_txt = hostapd_wps_ap_pin_get(hapd);
  388. if (pin_txt == NULL)
  389. return -1;
  390. return os_snprintf(buf, buflen, "%s", pin_txt);
  391. }
  392. if (os_strcmp(txt, "set") == 0) {
  393. char *pin;
  394. if (pos == NULL)
  395. return -1;
  396. pin = pos;
  397. pos = os_strchr(pos, ' ');
  398. if (pos) {
  399. *pos++ = '\0';
  400. timeout = atoi(pos);
  401. }
  402. if (os_strlen(pin) > buflen)
  403. return -1;
  404. if (hostapd_wps_ap_pin_set(hapd, pin, timeout) < 0)
  405. return -1;
  406. return os_snprintf(buf, buflen, "%s", pin);
  407. }
  408. return -1;
  409. }
  410. #endif /* CONFIG_WPS */
  411. static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
  412. char *buf, size_t buflen)
  413. {
  414. int ret;
  415. char *pos, *end;
  416. pos = buf;
  417. end = buf + buflen;
  418. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n"
  419. "ssid=%s\n",
  420. MAC2STR(hapd->own_addr),
  421. hapd->conf->ssid.ssid);
  422. if (ret < 0 || ret >= end - pos)
  423. return pos - buf;
  424. pos += ret;
  425. #ifdef CONFIG_WPS
  426. ret = os_snprintf(pos, end - pos, "wps_state=%s\n",
  427. hapd->conf->wps_state == 0 ? "disabled" :
  428. (hapd->conf->wps_state == 1 ? "not configured" :
  429. "configured"));
  430. if (ret < 0 || ret >= end - pos)
  431. return pos - buf;
  432. pos += ret;
  433. if (hapd->conf->wps_state && hapd->conf->wpa &&
  434. hapd->conf->ssid.wpa_passphrase) {
  435. ret = os_snprintf(pos, end - pos, "passphrase=%s\n",
  436. hapd->conf->ssid.wpa_passphrase);
  437. if (ret < 0 || ret >= end - pos)
  438. return pos - buf;
  439. pos += ret;
  440. }
  441. if (hapd->conf->wps_state && hapd->conf->wpa &&
  442. hapd->conf->ssid.wpa_psk &&
  443. hapd->conf->ssid.wpa_psk->group) {
  444. char hex[PMK_LEN * 2 + 1];
  445. wpa_snprintf_hex(hex, sizeof(hex),
  446. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  447. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  448. if (ret < 0 || ret >= end - pos)
  449. return pos - buf;
  450. pos += ret;
  451. }
  452. #endif /* CONFIG_WPS */
  453. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  454. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  455. if (ret < 0 || ret >= end - pos)
  456. return pos - buf;
  457. pos += ret;
  458. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  459. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  460. if (ret < 0 || ret >= end - pos)
  461. return pos - buf;
  462. pos += ret;
  463. }
  464. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  465. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  466. if (ret < 0 || ret >= end - pos)
  467. return pos - buf;
  468. pos += ret;
  469. }
  470. #ifdef CONFIG_IEEE80211R
  471. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  472. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  473. if (ret < 0 || ret >= end - pos)
  474. return pos - buf;
  475. pos += ret;
  476. }
  477. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  478. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  479. if (ret < 0 || ret >= end - pos)
  480. return pos - buf;
  481. pos += ret;
  482. }
  483. #endif /* CONFIG_IEEE80211R */
  484. #ifdef CONFIG_IEEE80211W
  485. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  486. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  487. if (ret < 0 || ret >= end - pos)
  488. return pos - buf;
  489. pos += ret;
  490. }
  491. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  492. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  493. if (ret < 0 || ret >= end - pos)
  494. return pos - buf;
  495. pos += ret;
  496. }
  497. #endif /* CONFIG_IEEE80211W */
  498. ret = os_snprintf(pos, end - pos, "\n");
  499. if (ret < 0 || ret >= end - pos)
  500. return pos - buf;
  501. pos += ret;
  502. }
  503. if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
  504. ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n");
  505. if (ret < 0 || ret >= end - pos)
  506. return pos - buf;
  507. pos += ret;
  508. } else if (hapd->conf->wpa &&
  509. hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
  510. ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
  511. if (ret < 0 || ret >= end - pos)
  512. return pos - buf;
  513. pos += ret;
  514. }
  515. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  516. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  517. if (ret < 0 || ret >= end - pos)
  518. return pos - buf;
  519. pos += ret;
  520. if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
  521. ret = os_snprintf(pos, end - pos, "CCMP ");
  522. if (ret < 0 || ret >= end - pos)
  523. return pos - buf;
  524. pos += ret;
  525. }
  526. if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
  527. ret = os_snprintf(pos, end - pos, "TKIP ");
  528. if (ret < 0 || ret >= end - pos)
  529. return pos - buf;
  530. pos += ret;
  531. }
  532. ret = os_snprintf(pos, end - pos, "\n");
  533. if (ret < 0 || ret >= end - pos)
  534. return pos - buf;
  535. pos += ret;
  536. }
  537. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  538. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  539. if (ret < 0 || ret >= end - pos)
  540. return pos - buf;
  541. pos += ret;
  542. if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
  543. ret = os_snprintf(pos, end - pos, "CCMP ");
  544. if (ret < 0 || ret >= end - pos)
  545. return pos - buf;
  546. pos += ret;
  547. }
  548. if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
  549. ret = os_snprintf(pos, end - pos, "TKIP ");
  550. if (ret < 0 || ret >= end - pos)
  551. return pos - buf;
  552. pos += ret;
  553. }
  554. ret = os_snprintf(pos, end - pos, "\n");
  555. if (ret < 0 || ret >= end - pos)
  556. return pos - buf;
  557. pos += ret;
  558. }
  559. return pos - buf;
  560. }
  561. static int hostapd_ctrl_iface_set(struct hostapd_data *hapd, char *cmd)
  562. {
  563. char *value;
  564. int ret = 0;
  565. value = os_strchr(cmd, ' ');
  566. if (value == NULL)
  567. return -1;
  568. *value++ = '\0';
  569. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  570. if (0) {
  571. #ifdef CONFIG_WPS_TESTING
  572. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  573. long int val;
  574. val = strtol(value, NULL, 0);
  575. if (val < 0 || val > 0xff) {
  576. ret = -1;
  577. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  578. "wps_version_number %ld", val);
  579. } else {
  580. wps_version_number = val;
  581. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  582. "version %u.%u",
  583. (wps_version_number & 0xf0) >> 4,
  584. wps_version_number & 0x0f);
  585. hostapd_wps_update_ie(hapd);
  586. }
  587. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  588. wps_testing_dummy_cred = atoi(value);
  589. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  590. wps_testing_dummy_cred);
  591. #endif /* CONFIG_WPS_TESTING */
  592. } else {
  593. ret = -1;
  594. }
  595. return ret;
  596. }
  597. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  598. void *sock_ctx)
  599. {
  600. struct hostapd_data *hapd = eloop_ctx;
  601. char buf[256];
  602. int res;
  603. struct sockaddr_un from;
  604. socklen_t fromlen = sizeof(from);
  605. char *reply;
  606. const int reply_size = 4096;
  607. int reply_len;
  608. int level = MSG_DEBUG;
  609. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  610. (struct sockaddr *) &from, &fromlen);
  611. if (res < 0) {
  612. perror("recvfrom(ctrl_iface)");
  613. return;
  614. }
  615. buf[res] = '\0';
  616. if (os_strcmp(buf, "PING") == 0)
  617. level = MSG_EXCESSIVE;
  618. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  619. reply = os_malloc(reply_size);
  620. if (reply == NULL) {
  621. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  622. fromlen);
  623. return;
  624. }
  625. os_memcpy(reply, "OK\n", 3);
  626. reply_len = 3;
  627. if (os_strcmp(buf, "PING") == 0) {
  628. os_memcpy(reply, "PONG\n", 5);
  629. reply_len = 5;
  630. } else if (os_strcmp(buf, "MIB") == 0) {
  631. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  632. if (reply_len >= 0) {
  633. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  634. reply_size - reply_len);
  635. if (res < 0)
  636. reply_len = -1;
  637. else
  638. reply_len += res;
  639. }
  640. if (reply_len >= 0) {
  641. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  642. reply_size - reply_len);
  643. if (res < 0)
  644. reply_len = -1;
  645. else
  646. reply_len += res;
  647. }
  648. #ifndef CONFIG_NO_RADIUS
  649. if (reply_len >= 0) {
  650. res = radius_client_get_mib(hapd->radius,
  651. reply + reply_len,
  652. reply_size - reply_len);
  653. if (res < 0)
  654. reply_len = -1;
  655. else
  656. reply_len += res;
  657. }
  658. #endif /* CONFIG_NO_RADIUS */
  659. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  660. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  661. reply_size);
  662. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  663. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  664. reply_size);
  665. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  666. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  667. reply_size);
  668. } else if (os_strcmp(buf, "ATTACH") == 0) {
  669. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  670. reply_len = -1;
  671. } else if (os_strcmp(buf, "DETACH") == 0) {
  672. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  673. reply_len = -1;
  674. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  675. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  676. buf + 6))
  677. reply_len = -1;
  678. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  679. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  680. reply_len = -1;
  681. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  682. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  683. reply_len = -1;
  684. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  685. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  686. reply_len = -1;
  687. #ifdef CONFIG_IEEE80211W
  688. #ifdef NEED_AP_MLME
  689. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  690. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  691. reply_len = -1;
  692. #endif /* NEED_AP_MLME */
  693. #endif /* CONFIG_IEEE80211W */
  694. #ifdef CONFIG_WPS
  695. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  696. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  697. reply_len = -1;
  698. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  699. reply_len = hostapd_ctrl_iface_wps_check_pin(
  700. hapd, buf + 14, reply, reply_size);
  701. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  702. if (hostapd_wps_button_pushed(hapd))
  703. reply_len = -1;
  704. #ifdef CONFIG_WPS_OOB
  705. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  706. if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
  707. reply_len = -1;
  708. #endif /* CONFIG_WPS_OOB */
  709. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  710. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  711. reply, reply_size);
  712. #endif /* CONFIG_WPS */
  713. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  714. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  715. reply_size);
  716. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  717. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  718. reply_len = -1;
  719. } else {
  720. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  721. reply_len = 16;
  722. }
  723. if (reply_len < 0) {
  724. os_memcpy(reply, "FAIL\n", 5);
  725. reply_len = 5;
  726. }
  727. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  728. os_free(reply);
  729. }
  730. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  731. {
  732. char *buf;
  733. size_t len;
  734. if (hapd->conf->ctrl_interface == NULL)
  735. return NULL;
  736. len = os_strlen(hapd->conf->ctrl_interface) +
  737. os_strlen(hapd->conf->iface) + 2;
  738. buf = os_malloc(len);
  739. if (buf == NULL)
  740. return NULL;
  741. os_snprintf(buf, len, "%s/%s",
  742. hapd->conf->ctrl_interface, hapd->conf->iface);
  743. buf[len - 1] = '\0';
  744. return buf;
  745. }
  746. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  747. const char *txt, size_t len)
  748. {
  749. struct hostapd_data *hapd = ctx;
  750. if (hapd == NULL)
  751. return;
  752. hostapd_ctrl_iface_send(hapd, level, txt, len);
  753. }
  754. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  755. {
  756. struct sockaddr_un addr;
  757. int s = -1;
  758. char *fname = NULL;
  759. hapd->ctrl_sock = -1;
  760. if (hapd->conf->ctrl_interface == NULL)
  761. return 0;
  762. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  763. if (errno == EEXIST) {
  764. wpa_printf(MSG_DEBUG, "Using existing control "
  765. "interface directory.");
  766. } else {
  767. perror("mkdir[ctrl_interface]");
  768. goto fail;
  769. }
  770. }
  771. if (hapd->conf->ctrl_interface_gid_set &&
  772. chown(hapd->conf->ctrl_interface, 0,
  773. hapd->conf->ctrl_interface_gid) < 0) {
  774. perror("chown[ctrl_interface]");
  775. return -1;
  776. }
  777. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  778. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  779. goto fail;
  780. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  781. if (s < 0) {
  782. perror("socket(PF_UNIX)");
  783. goto fail;
  784. }
  785. os_memset(&addr, 0, sizeof(addr));
  786. #ifdef __FreeBSD__
  787. addr.sun_len = sizeof(addr);
  788. #endif /* __FreeBSD__ */
  789. addr.sun_family = AF_UNIX;
  790. fname = hostapd_ctrl_iface_path(hapd);
  791. if (fname == NULL)
  792. goto fail;
  793. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  794. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  795. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  796. strerror(errno));
  797. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  798. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  799. " allow connections - assuming it was left"
  800. "over from forced program termination");
  801. if (unlink(fname) < 0) {
  802. perror("unlink[ctrl_iface]");
  803. wpa_printf(MSG_ERROR, "Could not unlink "
  804. "existing ctrl_iface socket '%s'",
  805. fname);
  806. goto fail;
  807. }
  808. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  809. 0) {
  810. perror("bind(PF_UNIX)");
  811. goto fail;
  812. }
  813. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  814. "ctrl_iface socket '%s'", fname);
  815. } else {
  816. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  817. "be in use - cannot override it");
  818. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  819. "not used anymore", fname);
  820. os_free(fname);
  821. fname = NULL;
  822. goto fail;
  823. }
  824. }
  825. if (hapd->conf->ctrl_interface_gid_set &&
  826. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  827. perror("chown[ctrl_interface/ifname]");
  828. goto fail;
  829. }
  830. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  831. perror("chmod[ctrl_interface/ifname]");
  832. goto fail;
  833. }
  834. os_free(fname);
  835. hapd->ctrl_sock = s;
  836. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  837. NULL);
  838. hapd->msg_ctx = hapd;
  839. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  840. return 0;
  841. fail:
  842. if (s >= 0)
  843. close(s);
  844. if (fname) {
  845. unlink(fname);
  846. os_free(fname);
  847. }
  848. return -1;
  849. }
  850. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  851. {
  852. struct wpa_ctrl_dst *dst, *prev;
  853. if (hapd->ctrl_sock > -1) {
  854. char *fname;
  855. eloop_unregister_read_sock(hapd->ctrl_sock);
  856. close(hapd->ctrl_sock);
  857. hapd->ctrl_sock = -1;
  858. fname = hostapd_ctrl_iface_path(hapd);
  859. if (fname)
  860. unlink(fname);
  861. os_free(fname);
  862. if (hapd->conf->ctrl_interface &&
  863. rmdir(hapd->conf->ctrl_interface) < 0) {
  864. if (errno == ENOTEMPTY) {
  865. wpa_printf(MSG_DEBUG, "Control interface "
  866. "directory not empty - leaving it "
  867. "behind");
  868. } else {
  869. perror("rmdir[ctrl_interface]");
  870. }
  871. }
  872. }
  873. dst = hapd->ctrl_dst;
  874. while (dst) {
  875. prev = dst;
  876. dst = dst->next;
  877. os_free(prev);
  878. }
  879. }
  880. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  881. const char *buf, size_t len)
  882. {
  883. struct wpa_ctrl_dst *dst, *next;
  884. struct msghdr msg;
  885. int idx;
  886. struct iovec io[2];
  887. char levelstr[10];
  888. dst = hapd->ctrl_dst;
  889. if (hapd->ctrl_sock < 0 || dst == NULL)
  890. return;
  891. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  892. io[0].iov_base = levelstr;
  893. io[0].iov_len = os_strlen(levelstr);
  894. io[1].iov_base = (char *) buf;
  895. io[1].iov_len = len;
  896. os_memset(&msg, 0, sizeof(msg));
  897. msg.msg_iov = io;
  898. msg.msg_iovlen = 2;
  899. idx = 0;
  900. while (dst) {
  901. next = dst->next;
  902. if (level >= dst->debug_level) {
  903. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  904. (u8 *) dst->addr.sun_path, dst->addrlen -
  905. offsetof(struct sockaddr_un, sun_path));
  906. msg.msg_name = &dst->addr;
  907. msg.msg_namelen = dst->addrlen;
  908. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  909. int _errno = errno;
  910. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  911. "%d - %s",
  912. idx, errno, strerror(errno));
  913. dst->errors++;
  914. if (dst->errors > 10 || _errno == ENOENT) {
  915. hostapd_ctrl_iface_detach(
  916. hapd, &dst->addr,
  917. dst->addrlen);
  918. }
  919. } else
  920. dst->errors = 0;
  921. }
  922. idx++;
  923. dst = next;
  924. }
  925. }
  926. #endif /* CONFIG_NATIVE_WINDOWS */