ctrl_iface.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * hostapd / UNIX domain socket -based control interface
  3. * Copyright (c) 2004-2008, 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 "includes.h"
  15. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include <sys/un.h>
  17. #include <sys/stat.h>
  18. #include <stddef.h>
  19. #include "hostapd.h"
  20. #include "eloop.h"
  21. #include "config.h"
  22. #include "ieee802_1x.h"
  23. #include "wpa.h"
  24. #include "radius/radius_client.h"
  25. #include "ieee802_11.h"
  26. #include "ctrl_iface.h"
  27. #include "sta_info.h"
  28. #include "accounting.h"
  29. #include "wps_hostapd.h"
  30. #include "drivers/driver.h"
  31. struct wpa_ctrl_dst {
  32. struct wpa_ctrl_dst *next;
  33. struct sockaddr_un addr;
  34. socklen_t addrlen;
  35. int debug_level;
  36. int errors;
  37. };
  38. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  39. const char *buf, size_t len);
  40. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  41. struct sockaddr_un *from,
  42. socklen_t fromlen)
  43. {
  44. struct wpa_ctrl_dst *dst;
  45. dst = os_zalloc(sizeof(*dst));
  46. if (dst == NULL)
  47. return -1;
  48. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  49. dst->addrlen = fromlen;
  50. dst->debug_level = MSG_INFO;
  51. dst->next = hapd->ctrl_dst;
  52. hapd->ctrl_dst = dst;
  53. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  54. (u8 *) from->sun_path,
  55. fromlen - offsetof(struct sockaddr_un, sun_path));
  56. return 0;
  57. }
  58. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  59. struct sockaddr_un *from,
  60. socklen_t fromlen)
  61. {
  62. struct wpa_ctrl_dst *dst, *prev = NULL;
  63. dst = hapd->ctrl_dst;
  64. while (dst) {
  65. if (fromlen == dst->addrlen &&
  66. os_memcmp(from->sun_path, dst->addr.sun_path,
  67. fromlen - offsetof(struct sockaddr_un, sun_path))
  68. == 0) {
  69. if (prev == NULL)
  70. hapd->ctrl_dst = dst->next;
  71. else
  72. prev->next = dst->next;
  73. os_free(dst);
  74. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  75. (u8 *) from->sun_path,
  76. fromlen -
  77. offsetof(struct sockaddr_un, sun_path));
  78. return 0;
  79. }
  80. prev = dst;
  81. dst = dst->next;
  82. }
  83. return -1;
  84. }
  85. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  86. struct sockaddr_un *from,
  87. socklen_t fromlen,
  88. char *level)
  89. {
  90. struct wpa_ctrl_dst *dst;
  91. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  92. dst = hapd->ctrl_dst;
  93. while (dst) {
  94. if (fromlen == dst->addrlen &&
  95. os_memcmp(from->sun_path, dst->addr.sun_path,
  96. fromlen - offsetof(struct sockaddr_un, sun_path))
  97. == 0) {
  98. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  99. "level", (u8 *) from->sun_path, fromlen -
  100. offsetof(struct sockaddr_un, sun_path));
  101. dst->debug_level = atoi(level);
  102. return 0;
  103. }
  104. dst = dst->next;
  105. }
  106. return -1;
  107. }
  108. static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
  109. struct sta_info *sta,
  110. char *buf, size_t buflen)
  111. {
  112. int len, res, ret;
  113. if (sta == NULL) {
  114. ret = os_snprintf(buf, buflen, "FAIL\n");
  115. if (ret < 0 || (size_t) ret >= buflen)
  116. return 0;
  117. return ret;
  118. }
  119. len = 0;
  120. ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
  121. MAC2STR(sta->addr));
  122. if (ret < 0 || (size_t) ret >= buflen - len)
  123. return len;
  124. len += ret;
  125. res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
  126. if (res >= 0)
  127. len += res;
  128. res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
  129. if (res >= 0)
  130. len += res;
  131. res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
  132. if (res >= 0)
  133. len += res;
  134. return len;
  135. }
  136. static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
  137. char *buf, size_t buflen)
  138. {
  139. return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
  140. }
  141. static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
  142. const char *txtaddr,
  143. char *buf, size_t buflen)
  144. {
  145. u8 addr[ETH_ALEN];
  146. int ret;
  147. if (hwaddr_aton(txtaddr, addr)) {
  148. ret = os_snprintf(buf, buflen, "FAIL\n");
  149. if (ret < 0 || (size_t) ret >= buflen)
  150. return 0;
  151. return ret;
  152. }
  153. return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
  154. buf, buflen);
  155. }
  156. static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
  157. const char *txtaddr,
  158. char *buf, size_t buflen)
  159. {
  160. u8 addr[ETH_ALEN];
  161. struct sta_info *sta;
  162. int ret;
  163. if (hwaddr_aton(txtaddr, addr) ||
  164. (sta = ap_get_sta(hapd, addr)) == NULL) {
  165. ret = os_snprintf(buf, buflen, "FAIL\n");
  166. if (ret < 0 || (size_t) ret >= buflen)
  167. return 0;
  168. return ret;
  169. }
  170. return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
  171. }
  172. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  173. const char *txtaddr)
  174. {
  175. u8 addr[ETH_ALEN];
  176. struct sta_info *sta;
  177. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  178. if (hwaddr_aton(txtaddr, addr))
  179. return -1;
  180. sta = ap_get_sta(hapd, addr);
  181. if (sta)
  182. return 0;
  183. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  184. "notification", MAC2STR(addr));
  185. sta = ap_sta_add(hapd, addr);
  186. if (sta == NULL)
  187. return -1;
  188. hostapd_new_assoc_sta(hapd, sta, 0);
  189. return 0;
  190. }
  191. #ifdef CONFIG_IEEE80211W
  192. #ifdef NEED_MLME
  193. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  194. const char *txtaddr)
  195. {
  196. u8 addr[ETH_ALEN];
  197. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  198. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  199. if (hwaddr_aton(txtaddr, addr))
  200. return -1;
  201. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  202. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  203. return 0;
  204. }
  205. #endif /* NEED_MLME */
  206. #endif /* CONFIG_IEEE80211W */
  207. #ifdef CONFIG_WPS
  208. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  209. {
  210. char *pin = os_strchr(txt, ' ');
  211. if (pin == NULL)
  212. return -1;
  213. *pin++ = '\0';
  214. return hostapd_wps_add_pin(hapd, txt, pin);
  215. }
  216. #ifdef CONFIG_WPS_OOB
  217. static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
  218. {
  219. char *path, *method, *name;
  220. path = os_strchr(txt, ' ');
  221. if (path == NULL)
  222. return -1;
  223. *path++ = '\0';
  224. method = os_strchr(path, ' ');
  225. if (method == NULL)
  226. return -1;
  227. *method++ = '\0';
  228. name = os_strchr(method, ' ');
  229. if (name != NULL)
  230. *name++ = '\0';
  231. return hostapd_wps_start_oob(hapd, txt, path, method, name);
  232. }
  233. #endif /* CONFIG_WPS_OOB */
  234. #endif /* CONFIG_WPS */
  235. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  236. void *sock_ctx)
  237. {
  238. struct hostapd_data *hapd = eloop_ctx;
  239. char buf[256];
  240. int res;
  241. struct sockaddr_un from;
  242. socklen_t fromlen = sizeof(from);
  243. char *reply;
  244. const int reply_size = 4096;
  245. int reply_len;
  246. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  247. (struct sockaddr *) &from, &fromlen);
  248. if (res < 0) {
  249. perror("recvfrom(ctrl_iface)");
  250. return;
  251. }
  252. buf[res] = '\0';
  253. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
  254. reply = os_malloc(reply_size);
  255. if (reply == NULL) {
  256. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  257. fromlen);
  258. return;
  259. }
  260. os_memcpy(reply, "OK\n", 3);
  261. reply_len = 3;
  262. if (os_strcmp(buf, "PING") == 0) {
  263. os_memcpy(reply, "PONG\n", 5);
  264. reply_len = 5;
  265. } else if (os_strcmp(buf, "MIB") == 0) {
  266. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  267. if (reply_len >= 0) {
  268. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  269. reply_size - reply_len);
  270. if (res < 0)
  271. reply_len = -1;
  272. else
  273. reply_len += res;
  274. }
  275. if (reply_len >= 0) {
  276. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  277. reply_size - reply_len);
  278. if (res < 0)
  279. reply_len = -1;
  280. else
  281. reply_len += res;
  282. }
  283. if (reply_len >= 0) {
  284. res = radius_client_get_mib(hapd->radius,
  285. reply + reply_len,
  286. reply_size - reply_len);
  287. if (res < 0)
  288. reply_len = -1;
  289. else
  290. reply_len += res;
  291. }
  292. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  293. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  294. reply_size);
  295. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  296. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  297. reply_size);
  298. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  299. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  300. reply_size);
  301. } else if (os_strcmp(buf, "ATTACH") == 0) {
  302. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  303. reply_len = -1;
  304. } else if (os_strcmp(buf, "DETACH") == 0) {
  305. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  306. reply_len = -1;
  307. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  308. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  309. buf + 6))
  310. reply_len = -1;
  311. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  312. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  313. reply_len = -1;
  314. #ifdef CONFIG_IEEE80211W
  315. #ifdef NEED_MLME
  316. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  317. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  318. reply_len = -1;
  319. #endif /* NEED_MLME */
  320. #endif /* CONFIG_IEEE80211W */
  321. #ifdef CONFIG_WPS
  322. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  323. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  324. reply_len = -1;
  325. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  326. if (hostapd_wps_button_pushed(hapd))
  327. reply_len = -1;
  328. #ifdef CONFIG_WPS_OOB
  329. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  330. if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
  331. reply_len = -1;
  332. #endif /* CONFIG_WPS_OOB */
  333. #endif /* CONFIG_WPS */
  334. } else {
  335. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  336. reply_len = 16;
  337. }
  338. if (reply_len < 0) {
  339. os_memcpy(reply, "FAIL\n", 5);
  340. reply_len = 5;
  341. }
  342. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  343. os_free(reply);
  344. }
  345. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  346. {
  347. char *buf;
  348. size_t len;
  349. if (hapd->conf->ctrl_interface == NULL)
  350. return NULL;
  351. len = os_strlen(hapd->conf->ctrl_interface) +
  352. os_strlen(hapd->conf->iface) + 2;
  353. buf = os_malloc(len);
  354. if (buf == NULL)
  355. return NULL;
  356. os_snprintf(buf, len, "%s/%s",
  357. hapd->conf->ctrl_interface, hapd->conf->iface);
  358. buf[len - 1] = '\0';
  359. return buf;
  360. }
  361. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  362. const char *txt, size_t len)
  363. {
  364. struct hostapd_data *hapd = ctx;
  365. if (hapd == NULL)
  366. return;
  367. hostapd_ctrl_iface_send(hapd, level, txt, len);
  368. }
  369. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  370. {
  371. struct sockaddr_un addr;
  372. int s = -1;
  373. char *fname = NULL;
  374. hapd->ctrl_sock = -1;
  375. if (hapd->conf->ctrl_interface == NULL)
  376. return 0;
  377. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  378. if (errno == EEXIST) {
  379. wpa_printf(MSG_DEBUG, "Using existing control "
  380. "interface directory.");
  381. } else {
  382. perror("mkdir[ctrl_interface]");
  383. goto fail;
  384. }
  385. }
  386. if (hapd->conf->ctrl_interface_gid_set &&
  387. chown(hapd->conf->ctrl_interface, 0,
  388. hapd->conf->ctrl_interface_gid) < 0) {
  389. perror("chown[ctrl_interface]");
  390. return -1;
  391. }
  392. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  393. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  394. goto fail;
  395. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  396. if (s < 0) {
  397. perror("socket(PF_UNIX)");
  398. goto fail;
  399. }
  400. os_memset(&addr, 0, sizeof(addr));
  401. #ifdef __FreeBSD__
  402. addr.sun_len = sizeof(addr);
  403. #endif /* __FreeBSD__ */
  404. addr.sun_family = AF_UNIX;
  405. fname = hostapd_ctrl_iface_path(hapd);
  406. if (fname == NULL)
  407. goto fail;
  408. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  409. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  410. wpa_printf(MSG_DEBUG, "ctrl_iface bind(PF_UNIX) failed: %s",
  411. strerror(errno));
  412. if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  413. wpa_printf(MSG_DEBUG, "ctrl_iface exists, but does not"
  414. " allow connections - assuming it was left"
  415. "over from forced program termination");
  416. if (unlink(fname) < 0) {
  417. perror("unlink[ctrl_iface]");
  418. wpa_printf(MSG_ERROR, "Could not unlink "
  419. "existing ctrl_iface socket '%s'",
  420. fname);
  421. goto fail;
  422. }
  423. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) <
  424. 0) {
  425. perror("bind(PF_UNIX)");
  426. goto fail;
  427. }
  428. wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
  429. "ctrl_iface socket '%s'", fname);
  430. } else {
  431. wpa_printf(MSG_INFO, "ctrl_iface exists and seems to "
  432. "be in use - cannot override it");
  433. wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
  434. "not used anymore", fname);
  435. os_free(fname);
  436. fname = NULL;
  437. goto fail;
  438. }
  439. }
  440. if (hapd->conf->ctrl_interface_gid_set &&
  441. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  442. perror("chown[ctrl_interface/ifname]");
  443. goto fail;
  444. }
  445. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  446. perror("chmod[ctrl_interface/ifname]");
  447. goto fail;
  448. }
  449. os_free(fname);
  450. hapd->ctrl_sock = s;
  451. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  452. NULL);
  453. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  454. return 0;
  455. fail:
  456. if (s >= 0)
  457. close(s);
  458. if (fname) {
  459. unlink(fname);
  460. os_free(fname);
  461. }
  462. return -1;
  463. }
  464. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  465. {
  466. struct wpa_ctrl_dst *dst, *prev;
  467. if (hapd->ctrl_sock > -1) {
  468. char *fname;
  469. eloop_unregister_read_sock(hapd->ctrl_sock);
  470. close(hapd->ctrl_sock);
  471. hapd->ctrl_sock = -1;
  472. fname = hostapd_ctrl_iface_path(hapd);
  473. if (fname)
  474. unlink(fname);
  475. os_free(fname);
  476. if (hapd->conf->ctrl_interface &&
  477. rmdir(hapd->conf->ctrl_interface) < 0) {
  478. if (errno == ENOTEMPTY) {
  479. wpa_printf(MSG_DEBUG, "Control interface "
  480. "directory not empty - leaving it "
  481. "behind");
  482. } else {
  483. perror("rmdir[ctrl_interface]");
  484. }
  485. }
  486. }
  487. dst = hapd->ctrl_dst;
  488. while (dst) {
  489. prev = dst;
  490. dst = dst->next;
  491. os_free(prev);
  492. }
  493. }
  494. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  495. const char *buf, size_t len)
  496. {
  497. struct wpa_ctrl_dst *dst, *next;
  498. struct msghdr msg;
  499. int idx;
  500. struct iovec io[2];
  501. char levelstr[10];
  502. dst = hapd->ctrl_dst;
  503. if (hapd->ctrl_sock < 0 || dst == NULL)
  504. return;
  505. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  506. io[0].iov_base = levelstr;
  507. io[0].iov_len = os_strlen(levelstr);
  508. io[1].iov_base = (char *) buf;
  509. io[1].iov_len = len;
  510. os_memset(&msg, 0, sizeof(msg));
  511. msg.msg_iov = io;
  512. msg.msg_iovlen = 2;
  513. idx = 0;
  514. while (dst) {
  515. next = dst->next;
  516. if (level >= dst->debug_level) {
  517. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  518. (u8 *) dst->addr.sun_path, dst->addrlen -
  519. offsetof(struct sockaddr_un, sun_path));
  520. msg.msg_name = &dst->addr;
  521. msg.msg_namelen = dst->addrlen;
  522. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  523. int _errno = errno;
  524. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  525. "%d - %s",
  526. idx, errno, strerror(errno));
  527. dst->errors++;
  528. if (dst->errors > 10 || _errno == ENOENT) {
  529. hostapd_ctrl_iface_detach(
  530. hapd, &dst->addr,
  531. dst->addrlen);
  532. }
  533. } else
  534. dst->errors = 0;
  535. }
  536. idx++;
  537. dst = next;
  538. }
  539. }
  540. #endif /* CONFIG_NATIVE_WINDOWS */