ctrl_iface.c 29 KB

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