ctrl_iface.c 26 KB

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