ctrl_iface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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 "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. static int hostapd_ctrl_iface_sa_query(struct hostapd_data *hapd,
  193. const char *txtaddr)
  194. {
  195. u8 addr[ETH_ALEN];
  196. u8 trans_id[WLAN_SA_QUERY_TR_ID_LEN];
  197. wpa_printf(MSG_DEBUG, "CTRL_IFACE SA_QUERY %s", txtaddr);
  198. if (hwaddr_aton(txtaddr, addr))
  199. return -1;
  200. os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
  201. ieee802_11_send_sa_query_req(hapd, addr, trans_id);
  202. return 0;
  203. }
  204. #endif /* CONFIG_IEEE80211W */
  205. #ifdef CONFIG_WPS
  206. static int hostapd_ctrl_iface_wps_pin(struct hostapd_data *hapd, char *txt)
  207. {
  208. char *pin = os_strchr(txt, ' ');
  209. if (pin == NULL)
  210. return -1;
  211. *pin++ = '\0';
  212. return hostapd_wps_add_pin(hapd, txt, pin);
  213. }
  214. #ifdef CONFIG_WPS_OOB
  215. static int hostapd_ctrl_iface_wps_oob(struct hostapd_data *hapd, char *txt)
  216. {
  217. char *path, *method, *name;
  218. path = os_strchr(txt, ' ');
  219. if (path == NULL)
  220. return -1;
  221. *path++ = '\0';
  222. method = os_strchr(path, ' ');
  223. if (method == NULL)
  224. return -1;
  225. *method++ = '\0';
  226. name = os_strchr(method, ' ');
  227. if (name != NULL)
  228. *name++ = '\0';
  229. return hostapd_wps_start_oob(hapd, txt, path, method, name);
  230. }
  231. #endif /* CONFIG_WPS_OOB */
  232. #endif /* CONFIG_WPS */
  233. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  234. void *sock_ctx)
  235. {
  236. struct hostapd_data *hapd = eloop_ctx;
  237. char buf[256];
  238. int res;
  239. struct sockaddr_un from;
  240. socklen_t fromlen = sizeof(from);
  241. char *reply;
  242. const int reply_size = 4096;
  243. int reply_len;
  244. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  245. (struct sockaddr *) &from, &fromlen);
  246. if (res < 0) {
  247. perror("recvfrom(ctrl_iface)");
  248. return;
  249. }
  250. buf[res] = '\0';
  251. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
  252. reply = os_malloc(reply_size);
  253. if (reply == NULL) {
  254. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  255. fromlen);
  256. return;
  257. }
  258. os_memcpy(reply, "OK\n", 3);
  259. reply_len = 3;
  260. if (os_strcmp(buf, "PING") == 0) {
  261. os_memcpy(reply, "PONG\n", 5);
  262. reply_len = 5;
  263. } else if (os_strcmp(buf, "MIB") == 0) {
  264. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  265. if (reply_len >= 0) {
  266. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  267. reply_size - reply_len);
  268. if (res < 0)
  269. reply_len = -1;
  270. else
  271. reply_len += res;
  272. }
  273. if (reply_len >= 0) {
  274. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  275. reply_size - reply_len);
  276. if (res < 0)
  277. reply_len = -1;
  278. else
  279. reply_len += res;
  280. }
  281. if (reply_len >= 0) {
  282. res = radius_client_get_mib(hapd->radius,
  283. reply + reply_len,
  284. reply_size - reply_len);
  285. if (res < 0)
  286. reply_len = -1;
  287. else
  288. reply_len += res;
  289. }
  290. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  291. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  292. reply_size);
  293. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  294. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  295. reply_size);
  296. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  297. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  298. reply_size);
  299. } else if (os_strcmp(buf, "ATTACH") == 0) {
  300. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  301. reply_len = -1;
  302. } else if (os_strcmp(buf, "DETACH") == 0) {
  303. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  304. reply_len = -1;
  305. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  306. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  307. buf + 6))
  308. reply_len = -1;
  309. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  310. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  311. reply_len = -1;
  312. #ifdef CONFIG_IEEE80211W
  313. } else if (os_strncmp(buf, "SA_QUERY ", 9) == 0) {
  314. if (hostapd_ctrl_iface_sa_query(hapd, buf + 9))
  315. reply_len = -1;
  316. #endif /* CONFIG_IEEE80211W */
  317. #ifdef CONFIG_WPS
  318. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  319. if (hostapd_ctrl_iface_wps_pin(hapd, buf + 8))
  320. reply_len = -1;
  321. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  322. if (hostapd_wps_button_pushed(hapd))
  323. reply_len = -1;
  324. #ifdef CONFIG_WPS_OOB
  325. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  326. if (hostapd_ctrl_iface_wps_oob(hapd, buf + 8))
  327. reply_len = -1;
  328. #endif /* CONFIG_WPS_OOB */
  329. #endif /* CONFIG_WPS */
  330. } else {
  331. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  332. reply_len = 16;
  333. }
  334. if (reply_len < 0) {
  335. os_memcpy(reply, "FAIL\n", 5);
  336. reply_len = 5;
  337. }
  338. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  339. os_free(reply);
  340. }
  341. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  342. {
  343. char *buf;
  344. size_t len;
  345. if (hapd->conf->ctrl_interface == NULL)
  346. return NULL;
  347. len = os_strlen(hapd->conf->ctrl_interface) +
  348. os_strlen(hapd->conf->iface) + 2;
  349. buf = os_malloc(len);
  350. if (buf == NULL)
  351. return NULL;
  352. os_snprintf(buf, len, "%s/%s",
  353. hapd->conf->ctrl_interface, hapd->conf->iface);
  354. buf[len - 1] = '\0';
  355. return buf;
  356. }
  357. static void hostapd_ctrl_iface_msg_cb(void *ctx, int level,
  358. const char *txt, size_t len)
  359. {
  360. struct hostapd_data *hapd = ctx;
  361. if (hapd == NULL)
  362. return;
  363. hostapd_ctrl_iface_send(hapd, level, txt, len);
  364. }
  365. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  366. {
  367. struct sockaddr_un addr;
  368. int s = -1;
  369. char *fname = NULL;
  370. hapd->ctrl_sock = -1;
  371. if (hapd->conf->ctrl_interface == NULL)
  372. return 0;
  373. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  374. if (errno == EEXIST) {
  375. wpa_printf(MSG_DEBUG, "Using existing control "
  376. "interface directory.");
  377. } else {
  378. perror("mkdir[ctrl_interface]");
  379. goto fail;
  380. }
  381. }
  382. if (hapd->conf->ctrl_interface_gid_set &&
  383. chown(hapd->conf->ctrl_interface, 0,
  384. hapd->conf->ctrl_interface_gid) < 0) {
  385. perror("chown[ctrl_interface]");
  386. return -1;
  387. }
  388. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  389. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  390. goto fail;
  391. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  392. if (s < 0) {
  393. perror("socket(PF_UNIX)");
  394. goto fail;
  395. }
  396. os_memset(&addr, 0, sizeof(addr));
  397. #ifdef __FreeBSD__
  398. addr.sun_len = sizeof(addr);
  399. #endif /* __FreeBSD__ */
  400. addr.sun_family = AF_UNIX;
  401. fname = hostapd_ctrl_iface_path(hapd);
  402. if (fname == NULL)
  403. goto fail;
  404. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  405. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  406. perror("bind(PF_UNIX)");
  407. goto fail;
  408. }
  409. if (hapd->conf->ctrl_interface_gid_set &&
  410. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  411. perror("chown[ctrl_interface/ifname]");
  412. goto fail;
  413. }
  414. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  415. perror("chmod[ctrl_interface/ifname]");
  416. goto fail;
  417. }
  418. os_free(fname);
  419. hapd->ctrl_sock = s;
  420. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  421. NULL);
  422. wpa_msg_register_cb(hostapd_ctrl_iface_msg_cb);
  423. return 0;
  424. fail:
  425. if (s >= 0)
  426. close(s);
  427. if (fname) {
  428. unlink(fname);
  429. os_free(fname);
  430. }
  431. return -1;
  432. }
  433. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  434. {
  435. struct wpa_ctrl_dst *dst, *prev;
  436. if (hapd->ctrl_sock > -1) {
  437. char *fname;
  438. eloop_unregister_read_sock(hapd->ctrl_sock);
  439. close(hapd->ctrl_sock);
  440. hapd->ctrl_sock = -1;
  441. fname = hostapd_ctrl_iface_path(hapd);
  442. if (fname)
  443. unlink(fname);
  444. os_free(fname);
  445. if (hapd->conf->ctrl_interface &&
  446. rmdir(hapd->conf->ctrl_interface) < 0) {
  447. if (errno == ENOTEMPTY) {
  448. wpa_printf(MSG_DEBUG, "Control interface "
  449. "directory not empty - leaving it "
  450. "behind");
  451. } else {
  452. perror("rmdir[ctrl_interface]");
  453. }
  454. }
  455. }
  456. dst = hapd->ctrl_dst;
  457. while (dst) {
  458. prev = dst;
  459. dst = dst->next;
  460. os_free(prev);
  461. }
  462. }
  463. static void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  464. const char *buf, size_t len)
  465. {
  466. struct wpa_ctrl_dst *dst, *next;
  467. struct msghdr msg;
  468. int idx;
  469. struct iovec io[2];
  470. char levelstr[10];
  471. dst = hapd->ctrl_dst;
  472. if (hapd->ctrl_sock < 0 || dst == NULL)
  473. return;
  474. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  475. io[0].iov_base = levelstr;
  476. io[0].iov_len = os_strlen(levelstr);
  477. io[1].iov_base = (char *) buf;
  478. io[1].iov_len = len;
  479. os_memset(&msg, 0, sizeof(msg));
  480. msg.msg_iov = io;
  481. msg.msg_iovlen = 2;
  482. idx = 0;
  483. while (dst) {
  484. next = dst->next;
  485. if (level >= dst->debug_level) {
  486. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  487. (u8 *) dst->addr.sun_path, dst->addrlen -
  488. offsetof(struct sockaddr_un, sun_path));
  489. msg.msg_name = &dst->addr;
  490. msg.msg_namelen = dst->addrlen;
  491. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  492. int _errno = errno;
  493. wpa_printf(MSG_INFO, "CTRL_IFACE monitor[%d]: "
  494. "%d - %s",
  495. idx, errno, strerror(errno));
  496. dst->errors++;
  497. if (dst->errors > 10 || _errno == ENOENT) {
  498. hostapd_ctrl_iface_detach(
  499. hapd, &dst->addr,
  500. dst->addrlen);
  501. }
  502. } else
  503. dst->errors = 0;
  504. }
  505. idx++;
  506. dst = next;
  507. }
  508. }
  509. #endif /* CONFIG_NATIVE_WINDOWS */