ctrl_iface.c 27 KB

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