ctrl_iface.c 24 KB

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