ctrl_iface.c 29 KB

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