ctrl_iface.c 27 KB

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