ctrl_iface.c 27 KB

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