ctrl_iface.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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 &&
  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->ssid.wpa_psk &&
  442. hapd->conf->ssid.wpa_psk->group) {
  443. char hex[PMK_LEN * 2 + 1];
  444. wpa_snprintf_hex(hex, sizeof(hex),
  445. hapd->conf->ssid.wpa_psk->psk, PMK_LEN);
  446. ret = os_snprintf(pos, end - pos, "psk=%s\n", hex);
  447. if (ret < 0 || ret >= end - pos)
  448. return pos - buf;
  449. pos += ret;
  450. }
  451. #endif /* CONFIG_WPS */
  452. if (hapd->conf->wpa && hapd->conf->wpa_key_mgmt) {
  453. ret = os_snprintf(pos, end - pos, "key_mgmt=");
  454. if (ret < 0 || ret >= end - pos)
  455. return pos - buf;
  456. pos += ret;
  457. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
  458. ret = os_snprintf(pos, end - pos, "WPA-PSK ");
  459. if (ret < 0 || ret >= end - pos)
  460. return pos - buf;
  461. pos += ret;
  462. }
  463. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  464. ret = os_snprintf(pos, end - pos, "WPA-EAP ");
  465. if (ret < 0 || ret >= end - pos)
  466. return pos - buf;
  467. pos += ret;
  468. }
  469. #ifdef CONFIG_IEEE80211R
  470. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  471. ret = os_snprintf(pos, end - pos, "FT-PSK ");
  472. if (ret < 0 || ret >= end - pos)
  473. return pos - buf;
  474. pos += ret;
  475. }
  476. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  477. ret = os_snprintf(pos, end - pos, "FT-EAP ");
  478. if (ret < 0 || ret >= end - pos)
  479. return pos - buf;
  480. pos += ret;
  481. }
  482. #endif /* CONFIG_IEEE80211R */
  483. #ifdef CONFIG_IEEE80211W
  484. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  485. ret = os_snprintf(pos, end - pos, "WPA-PSK-SHA256 ");
  486. if (ret < 0 || ret >= end - pos)
  487. return pos - buf;
  488. pos += ret;
  489. }
  490. if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  491. ret = os_snprintf(pos, end - pos, "WPA-EAP-SHA256 ");
  492. if (ret < 0 || ret >= end - pos)
  493. return pos - buf;
  494. pos += ret;
  495. }
  496. #endif /* CONFIG_IEEE80211W */
  497. ret = os_snprintf(pos, end - pos, "\n");
  498. if (ret < 0 || ret >= end - pos)
  499. return pos - buf;
  500. pos += ret;
  501. }
  502. if (hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
  503. ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n");
  504. if (ret < 0 || ret >= end - pos)
  505. return pos - buf;
  506. pos += ret;
  507. } else if (hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
  508. ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
  509. if (ret < 0 || ret >= end - pos)
  510. return pos - buf;
  511. pos += ret;
  512. }
  513. if ((hapd->conf->wpa & WPA_PROTO_RSN) && hapd->conf->rsn_pairwise) {
  514. ret = os_snprintf(pos, end - pos, "rsn_pairwise_cipher=");
  515. if (ret < 0 || ret >= end - pos)
  516. return pos - buf;
  517. pos += ret;
  518. if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
  519. ret = os_snprintf(pos, end - pos, "CCMP ");
  520. if (ret < 0 || ret >= end - pos)
  521. return pos - buf;
  522. pos += ret;
  523. }
  524. if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
  525. ret = os_snprintf(pos, end - pos, "TKIP ");
  526. if (ret < 0 || ret >= end - pos)
  527. return pos - buf;
  528. pos += ret;
  529. }
  530. ret = os_snprintf(pos, end - pos, "\n");
  531. if (ret < 0 || ret >= end - pos)
  532. return pos - buf;
  533. pos += ret;
  534. }
  535. if ((hapd->conf->wpa & WPA_PROTO_WPA) && hapd->conf->wpa_pairwise) {
  536. ret = os_snprintf(pos, end - pos, "wpa_pairwise_cipher=");
  537. if (ret < 0 || ret >= end - pos)
  538. return pos - buf;
  539. pos += ret;
  540. if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
  541. ret = os_snprintf(pos, end - pos, "CCMP ");
  542. if (ret < 0 || ret >= end - pos)
  543. return pos - buf;
  544. pos += ret;
  545. }
  546. if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
  547. ret = os_snprintf(pos, end - pos, "TKIP ");
  548. if (ret < 0 || ret >= end - pos)
  549. return pos - buf;
  550. pos += ret;
  551. }
  552. ret = os_snprintf(pos, end - pos, "\n");
  553. if (ret < 0 || ret >= end - pos)
  554. return pos - buf;
  555. pos += ret;
  556. }
  557. return pos - buf;
  558. }
  559. static int hostapd_ctrl_iface_set(struct hostapd_data *wpa_s, char *cmd)
  560. {
  561. char *value;
  562. int ret = 0;
  563. value = os_strchr(cmd, ' ');
  564. if (value == NULL)
  565. return -1;
  566. *value++ = '\0';
  567. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  568. if (0) {
  569. #ifdef CONFIG_WPS_TESTING
  570. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  571. long int val;
  572. val = strtol(value, NULL, 0);
  573. if (val < 0 || val > 0xff) {
  574. ret = -1;
  575. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  576. "wps_version_number %ld", val);
  577. } else {
  578. wps_version_number = val;
  579. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  580. "version %u.%u",
  581. (wps_version_number & 0xf0) >> 4,
  582. wps_version_number & 0x0f);
  583. }
  584. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  585. wps_testing_dummy_cred = atoi(value);
  586. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  587. wps_testing_dummy_cred);
  588. #endif /* CONFIG_WPS_TESTING */
  589. } else {
  590. ret = -1;
  591. }
  592. return ret;
  593. }
  594. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  595. void *sock_ctx)
  596. {
  597. struct hostapd_data *hapd = eloop_ctx;
  598. char buf[256];
  599. int res;
  600. struct sockaddr_un from;
  601. socklen_t fromlen = sizeof(from);
  602. char *reply;
  603. const int reply_size = 4096;
  604. int reply_len;
  605. int level = MSG_DEBUG;
  606. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  607. (struct sockaddr *) &from, &fromlen);
  608. if (res < 0) {
  609. perror("recvfrom(ctrl_iface)");
  610. return;
  611. }
  612. buf[res] = '\0';
  613. if (os_strcmp(buf, "PING") == 0)
  614. level = MSG_EXCESSIVE;
  615. wpa_hexdump_ascii(level, "RX ctrl_iface", (u8 *) buf, res);
  616. reply = os_malloc(reply_size);
  617. if (reply == NULL) {
  618. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  619. fromlen);
  620. return;
  621. }
  622. os_memcpy(reply, "OK\n", 3);
  623. reply_len = 3;
  624. if (os_strcmp(buf, "PING") == 0) {
  625. os_memcpy(reply, "PONG\n", 5);
  626. reply_len = 5;
  627. } else if (os_strcmp(buf, "MIB") == 0) {
  628. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  629. if (reply_len >= 0) {
  630. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  631. reply_size - reply_len);
  632. if (res < 0)
  633. reply_len = -1;
  634. else
  635. reply_len += res;
  636. }
  637. if (reply_len >= 0) {
  638. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  639. reply_size - reply_len);
  640. if (res < 0)
  641. reply_len = -1;
  642. else
  643. reply_len += res;
  644. }
  645. #ifndef CONFIG_NO_RADIUS
  646. if (reply_len >= 0) {
  647. res = radius_client_get_mib(hapd->radius,
  648. reply + reply_len,
  649. reply_size - reply_len);
  650. if (res < 0)
  651. reply_len = -1;
  652. else
  653. reply_len += res;
  654. }
  655. #endif /* CONFIG_NO_RADIUS */
  656. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  657. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  658. reply_size);
  659. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  660. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  661. reply_size);
  662. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  663. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  664. reply_size);
  665. } else if (os_strcmp(buf, "ATTACH") == 0) {
  666. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  667. reply_len = -1;
  668. } else if (os_strcmp(buf, "DETACH") == 0) {
  669. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  670. reply_len = -1;
  671. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  672. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  673. buf + 6))
  674. reply_len = -1;
  675. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  676. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  677. reply_len = -1;
  678. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  679. if (hostapd_ctrl_iface_deauthenticate(hapd, buf + 15))
  680. reply_len = -1;
  681. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  682. if (hostapd_ctrl_iface_disassociate(hapd, buf + 13))
  683. reply_len = -1;
  684. #ifdef CONFIG_IEEE80211W
  685. #ifdef NEED_AP_MLME
  686. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  687. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  688. reply_len = -1;
  689. #endif /* NEED_AP_MLME */
  690. #endif /* CONFIG_IEEE80211W */
  691. #ifdef CONFIG_WPS
  692. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  693. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  694. reply_len = -1;
  695. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  696. reply_len = hostapd_ctrl_iface_wps_check_pin(
  697. hapd, buf + 14, reply, reply_size);
  698. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  699. if (hostapd_wps_button_pushed(hapd))
  700. reply_len = -1;
  701. #ifdef CONFIG_WPS_OOB
  702. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  703. if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
  704. reply_len = -1;
  705. #endif /* CONFIG_WPS_OOB */
  706. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  707. reply_len = hostapd_ctrl_iface_wps_ap_pin(hapd, buf + 11,
  708. reply, reply_size);
  709. #endif /* CONFIG_WPS */
  710. } else if (os_strcmp(buf, "GET_CONFIG") == 0) {
  711. reply_len = hostapd_ctrl_iface_get_config(hapd, reply,
  712. reply_size);
  713. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  714. if (hostapd_ctrl_iface_set(hapd, buf + 4))
  715. reply_len = -1;
  716. } else {
  717. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  718. reply_len = 16;
  719. }
  720. if (reply_len < 0) {
  721. os_memcpy(reply, "FAIL\n", 5);
  722. reply_len = 5;
  723. }
  724. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  725. os_free(reply);
  726. }
  727. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  728. {
  729. char *buf;
  730. size_t len;
  731. if (hapd->conf->ctrl_interface == NULL)
  732. return NULL;
  733. len = os_strlen(hapd->conf->ctrl_interface) +
  734. os_strlen(hapd->conf->iface) + 2;
  735. buf = os_malloc(len);
  736. if (buf == NULL)
  737. return NULL;
  738. os_snprintf(buf, len, "%s/%s",
  739. hapd->conf->ctrl_interface, hapd->conf->iface);
  740. buf[len - 1] = '\0';
  741. return buf;
  742. }
  743. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  744. const char *txt, size_t len)
  745. {
  746. struct hostapd_data *hapd = ctx;
  747. if (hapd == NULL)
  748. return;
  749. hostapd_ctrl_iface_send(hapd, level, txt, len);
  750. }
  751. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  752. {
  753. struct sockaddr_un addr;
  754. int s = -1;
  755. char *fname = NULL;
  756. hapd->ctrl_sock = -1;
  757. if (hapd->conf->ctrl_interface == NULL)
  758. return 0;
  759. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  760. if (errno == EEXIST) {
  761. wpa_printf(MSG_DEBUG, "Using existing control "
  762. "interface directory.");
  763. } else {
  764. perror("mkdir[ctrl_interface]");
  765. goto fail;
  766. }
  767. }
  768. if (hapd->conf->ctrl_interface_gid_set &&
  769. chown(hapd->conf->ctrl_interface, 0,
  770. hapd->conf->ctrl_interface_gid) < 0) {
  771. perror("chown[ctrl_interface]");
  772. return -1;
  773. }
  774. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  775. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  776. goto fail;
  777. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  778. if (s < 0) {
  779. perror("socket(PF_UNIX)");
  780. goto fail;
  781. }
  782. os_memset(&addr, 0, sizeof(addr));
  783. #ifdef __FreeBSD__
  784. addr.sun_len = sizeof(addr);
  785. #endif /* __FreeBSD__ */
  786. addr.sun_family = AF_UNIX;
  787. fname = hostapd_ctrl_iface_path(hapd);
  788. if (fname == NULL)
  789. goto fail;
  790. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  791. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  792. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  793. strerror(errno));
  794. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  795. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  796. " allow connections - assuming it was left"
  797. "over from forced program termination");
  798. if (unlink(fname) < 0) {
  799. perror("unlink[ctrl_iface]");
  800. wpa_printf(MSG_ERROR, "Could not unlink "
  801. "existing ctrl_iface socket '%s'",
  802. fname);
  803. goto fail;
  804. }
  805. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  806. 0) {
  807. perror("bind(PF_UNIX)");
  808. goto fail;
  809. }
  810. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  811. "ctrl_iface socket '%s'", fname);
  812. } else {
  813. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  814. "be in use - cannot override it");
  815. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  816. "not used anymore", fname);
  817. os_free(fname);
  818. fname = NULL;
  819. goto fail;
  820. }
  821. }
  822. if (hapd->conf->ctrl_interface_gid_set &&
  823. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  824. perror("chown[ctrl_interface/ifname]");
  825. goto fail;
  826. }
  827. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  828. perror("chmod[ctrl_interface/ifname]");
  829. goto fail;
  830. }
  831. os_free(fname);
  832. hapd->ctrl_sock = s;
  833. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  834. NULL);
  835. hapd->msg_ctx = hapd;
  836. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  837. return 0;
  838. fail:
  839. if (s >= 0)
  840. close(s);
  841. if (fname) {
  842. unlink(fname);
  843. os_free(fname);
  844. }
  845. return -1;
  846. }
  847. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  848. {
  849. struct wpa_ctrl_dst *dst, *prev;
  850. if (hapd->ctrl_sock > -1) {
  851. char *fname;
  852. eloop_unregister_read_sock(hapd->ctrl_sock);
  853. close(hapd->ctrl_sock);
  854. hapd->ctrl_sock = -1;
  855. fname = hostapd_ctrl_iface_path(hapd);
  856. if (fname)
  857. unlink(fname);
  858. os_free(fname);
  859. if (hapd->conf->ctrl_interface &&
  860. rmdir(hapd->conf->ctrl_interface) < 0) {
  861. if (errno == ENOTEMPTY) {
  862. wpa_printf(MSG_DEBUG, "Control interface "
  863. "directory not empty - leaving it "
  864. "behind");
  865. } else {
  866. perror("rmdir[ctrl_interface]");
  867. }
  868. }
  869. }
  870. dst = hapd->ctrl_dst;
  871. while (dst) {
  872. prev = dst;
  873. dst = dst->next;
  874. os_free(prev);
  875. }
  876. }
  877. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  878. const char *buf, size_t len)
  879. {
  880. struct wpa_ctrl_dst *dst, *next;
  881. struct msghdr msg;
  882. int idx;
  883. struct iovec io[2];
  884. char levelstr[10];
  885. dst = hapd->ctrl_dst;
  886. if (hapd->ctrl_sock < 0 || dst == NULL)
  887. return;
  888. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  889. io[0].iov_base = levelstr;
  890. io[0].iov_len = os_strlen(levelstr);
  891. io[1].iov_base = (char *) buf;
  892. io[1].iov_len = len;
  893. os_memset(&msg, 0, sizeof(msg));
  894. msg.msg_iov = io;
  895. msg.msg_iovlen = 2;
  896. idx = 0;
  897. while (dst) {
  898. next = dst->next;
  899. if (level >= dst->debug_level) {
  900. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  901. (u8 *) dst->addr.sun_path, dst->addrlen -
  902. offsetof(struct sockaddr_un, sun_path));
  903. msg.msg_name = &dst->addr;
  904. msg.msg_namelen = dst->addrlen;
  905. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  906. int _errno = errno;
  907. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  908. "%d - %s",
  909. idx, errno, strerror(errno));
  910. dst->errors++;
  911. if (dst->errors > 10 || _errno == ENOENT) {
  912. hostapd_ctrl_iface_detach(
  913. hapd, &dst->addr,
  914. dst->addrlen);
  915. }
  916. } else
  917. dst->errors = 0;
  918. }
  919. idx++;
  920. dst = next;
  921. }
  922. }
  923. #endif /* CONFIG_NATIVE_WINDOWS */