Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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