ctrl_iface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 "hostapd.h"
  19. #include "eloop.h"
  20. #include "config.h"
  21. #include "ieee802_1x.h"
  22. #include "wpa.h"
  23. #include "radius/radius_client.h"
  24. #include "ieee802_11.h"
  25. #include "ctrl_iface.h"
  26. #include "sta_info.h"
  27. #include "accounting.h"
  28. struct wpa_ctrl_dst {
  29. struct wpa_ctrl_dst *next;
  30. struct sockaddr_un addr;
  31. socklen_t addrlen;
  32. int debug_level;
  33. int errors;
  34. };
  35. static int hostapd_ctrl_iface_attach(struct hostapd_data *hapd,
  36. struct sockaddr_un *from,
  37. socklen_t fromlen)
  38. {
  39. struct wpa_ctrl_dst *dst;
  40. dst = os_zalloc(sizeof(*dst));
  41. if (dst == NULL)
  42. return -1;
  43. os_memcpy(&dst->addr, from, sizeof(struct sockaddr_un));
  44. dst->addrlen = fromlen;
  45. dst->debug_level = MSG_INFO;
  46. dst->next = hapd->ctrl_dst;
  47. hapd->ctrl_dst = dst;
  48. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor attached",
  49. (u8 *) from->sun_path, fromlen);
  50. return 0;
  51. }
  52. static int hostapd_ctrl_iface_detach(struct hostapd_data *hapd,
  53. struct sockaddr_un *from,
  54. socklen_t fromlen)
  55. {
  56. struct wpa_ctrl_dst *dst, *prev = NULL;
  57. dst = hapd->ctrl_dst;
  58. while (dst) {
  59. if (fromlen == dst->addrlen &&
  60. os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
  61. 0) {
  62. if (prev == NULL)
  63. hapd->ctrl_dst = dst->next;
  64. else
  65. prev->next = dst->next;
  66. os_free(dst);
  67. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor detached",
  68. (u8 *) from->sun_path, fromlen);
  69. return 0;
  70. }
  71. prev = dst;
  72. dst = dst->next;
  73. }
  74. return -1;
  75. }
  76. static int hostapd_ctrl_iface_level(struct hostapd_data *hapd,
  77. struct sockaddr_un *from,
  78. socklen_t fromlen,
  79. char *level)
  80. {
  81. struct wpa_ctrl_dst *dst;
  82. wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
  83. dst = hapd->ctrl_dst;
  84. while (dst) {
  85. if (fromlen == dst->addrlen &&
  86. os_memcmp(from->sun_path, dst->addr.sun_path, fromlen) ==
  87. 0) {
  88. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE changed monitor "
  89. "level", (u8 *) from->sun_path, fromlen);
  90. dst->debug_level = atoi(level);
  91. return 0;
  92. }
  93. dst = dst->next;
  94. }
  95. return -1;
  96. }
  97. static int hostapd_ctrl_iface_sta_mib(struct hostapd_data *hapd,
  98. struct sta_info *sta,
  99. char *buf, size_t buflen)
  100. {
  101. int len, res, ret;
  102. if (sta == NULL) {
  103. ret = os_snprintf(buf, buflen, "FAIL\n");
  104. if (ret < 0 || (size_t) ret >= buflen)
  105. return 0;
  106. return ret;
  107. }
  108. len = 0;
  109. ret = os_snprintf(buf + len, buflen - len, MACSTR "\n",
  110. MAC2STR(sta->addr));
  111. if (ret < 0 || (size_t) ret >= buflen - len)
  112. return len;
  113. len += ret;
  114. res = ieee802_11_get_mib_sta(hapd, sta, buf + len, buflen - len);
  115. if (res >= 0)
  116. len += res;
  117. res = wpa_get_mib_sta(sta->wpa_sm, buf + len, buflen - len);
  118. if (res >= 0)
  119. len += res;
  120. res = ieee802_1x_get_mib_sta(hapd, sta, buf + len, buflen - len);
  121. if (res >= 0)
  122. len += res;
  123. return len;
  124. }
  125. static int hostapd_ctrl_iface_sta_first(struct hostapd_data *hapd,
  126. char *buf, size_t buflen)
  127. {
  128. return hostapd_ctrl_iface_sta_mib(hapd, hapd->sta_list, buf, buflen);
  129. }
  130. static int hostapd_ctrl_iface_sta(struct hostapd_data *hapd,
  131. const char *txtaddr,
  132. char *buf, size_t buflen)
  133. {
  134. u8 addr[ETH_ALEN];
  135. int ret;
  136. if (hwaddr_aton(txtaddr, addr)) {
  137. ret = os_snprintf(buf, buflen, "FAIL\n");
  138. if (ret < 0 || (size_t) ret >= buflen)
  139. return 0;
  140. return ret;
  141. }
  142. return hostapd_ctrl_iface_sta_mib(hapd, ap_get_sta(hapd, addr),
  143. buf, buflen);
  144. }
  145. static int hostapd_ctrl_iface_sta_next(struct hostapd_data *hapd,
  146. const char *txtaddr,
  147. char *buf, size_t buflen)
  148. {
  149. u8 addr[ETH_ALEN];
  150. struct sta_info *sta;
  151. int ret;
  152. if (hwaddr_aton(txtaddr, addr) ||
  153. (sta = ap_get_sta(hapd, addr)) == NULL) {
  154. ret = os_snprintf(buf, buflen, "FAIL\n");
  155. if (ret < 0 || (size_t) ret >= buflen)
  156. return 0;
  157. return ret;
  158. }
  159. return hostapd_ctrl_iface_sta_mib(hapd, sta->next, buf, buflen);
  160. }
  161. static int hostapd_ctrl_iface_new_sta(struct hostapd_data *hapd,
  162. const char *txtaddr)
  163. {
  164. u8 addr[ETH_ALEN];
  165. struct sta_info *sta;
  166. wpa_printf(MSG_DEBUG, "CTRL_IFACE NEW_STA %s", txtaddr);
  167. if (hwaddr_aton(txtaddr, addr))
  168. return -1;
  169. sta = ap_get_sta(hapd, addr);
  170. if (sta)
  171. return 0;
  172. wpa_printf(MSG_DEBUG, "Add new STA " MACSTR " based on ctrl_iface "
  173. "notification", MAC2STR(addr));
  174. sta = ap_sta_add(hapd, addr);
  175. if (sta == NULL)
  176. return -1;
  177. hostapd_new_assoc_sta(hapd, sta, 0);
  178. accounting_sta_get_id(hapd, sta);
  179. return 0;
  180. }
  181. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  182. void *sock_ctx)
  183. {
  184. struct hostapd_data *hapd = eloop_ctx;
  185. char buf[256];
  186. int res;
  187. struct sockaddr_un from;
  188. socklen_t fromlen = sizeof(from);
  189. char *reply;
  190. const int reply_size = 4096;
  191. int reply_len;
  192. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  193. (struct sockaddr *) &from, &fromlen);
  194. if (res < 0) {
  195. perror("recvfrom(ctrl_iface)");
  196. return;
  197. }
  198. buf[res] = '\0';
  199. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
  200. reply = os_malloc(reply_size);
  201. if (reply == NULL) {
  202. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  203. fromlen);
  204. return;
  205. }
  206. os_memcpy(reply, "OK\n", 3);
  207. reply_len = 3;
  208. if (os_strcmp(buf, "PING") == 0) {
  209. os_memcpy(reply, "PONG\n", 5);
  210. reply_len = 5;
  211. } else if (os_strcmp(buf, "MIB") == 0) {
  212. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  213. if (reply_len >= 0) {
  214. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  215. reply_size - reply_len);
  216. if (res < 0)
  217. reply_len = -1;
  218. else
  219. reply_len += res;
  220. }
  221. if (reply_len >= 0) {
  222. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  223. reply_size - reply_len);
  224. if (res < 0)
  225. reply_len = -1;
  226. else
  227. reply_len += res;
  228. }
  229. if (reply_len >= 0) {
  230. res = radius_client_get_mib(hapd->radius,
  231. reply + reply_len,
  232. reply_size - reply_len);
  233. if (res < 0)
  234. reply_len = -1;
  235. else
  236. reply_len += res;
  237. }
  238. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  239. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  240. reply_size);
  241. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  242. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  243. reply_size);
  244. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  245. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  246. reply_size);
  247. } else if (os_strcmp(buf, "ATTACH") == 0) {
  248. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  249. reply_len = -1;
  250. } else if (os_strcmp(buf, "DETACH") == 0) {
  251. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  252. reply_len = -1;
  253. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  254. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  255. buf + 6))
  256. reply_len = -1;
  257. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  258. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  259. reply_len = -1;
  260. } else {
  261. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  262. reply_len = 16;
  263. }
  264. if (reply_len < 0) {
  265. os_memcpy(reply, "FAIL\n", 5);
  266. reply_len = 5;
  267. }
  268. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  269. os_free(reply);
  270. }
  271. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  272. {
  273. char *buf;
  274. size_t len;
  275. if (hapd->conf->ctrl_interface == NULL)
  276. return NULL;
  277. len = os_strlen(hapd->conf->ctrl_interface) +
  278. os_strlen(hapd->conf->iface) + 2;
  279. buf = os_malloc(len);
  280. if (buf == NULL)
  281. return NULL;
  282. os_snprintf(buf, len, "%s/%s",
  283. hapd->conf->ctrl_interface, hapd->conf->iface);
  284. buf[len - 1] = '\0';
  285. return buf;
  286. }
  287. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  288. {
  289. struct sockaddr_un addr;
  290. int s = -1;
  291. char *fname = NULL;
  292. hapd->ctrl_sock = -1;
  293. if (hapd->conf->ctrl_interface == NULL)
  294. return 0;
  295. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  296. if (errno == EEXIST) {
  297. wpa_printf(MSG_DEBUG, "Using existing control "
  298. "interface directory.");
  299. } else {
  300. perror("mkdir[ctrl_interface]");
  301. goto fail;
  302. }
  303. }
  304. if (hapd->conf->ctrl_interface_gid_set &&
  305. chown(hapd->conf->ctrl_interface, 0,
  306. hapd->conf->ctrl_interface_gid) < 0) {
  307. perror("chown[ctrl_interface]");
  308. return -1;
  309. }
  310. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  311. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  312. goto fail;
  313. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  314. if (s < 0) {
  315. perror("socket(PF_UNIX)");
  316. goto fail;
  317. }
  318. os_memset(&addr, 0, sizeof(addr));
  319. addr.sun_family = AF_UNIX;
  320. fname = hostapd_ctrl_iface_path(hapd);
  321. if (fname == NULL)
  322. goto fail;
  323. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  324. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  325. perror("bind(PF_UNIX)");
  326. goto fail;
  327. }
  328. if (hapd->conf->ctrl_interface_gid_set &&
  329. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  330. perror("chown[ctrl_interface/ifname]");
  331. goto fail;
  332. }
  333. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  334. perror("chmod[ctrl_interface/ifname]");
  335. goto fail;
  336. }
  337. os_free(fname);
  338. hapd->ctrl_sock = s;
  339. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  340. NULL);
  341. return 0;
  342. fail:
  343. if (s >= 0)
  344. close(s);
  345. if (fname) {
  346. unlink(fname);
  347. os_free(fname);
  348. }
  349. return -1;
  350. }
  351. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  352. {
  353. struct wpa_ctrl_dst *dst, *prev;
  354. if (hapd->ctrl_sock > -1) {
  355. char *fname;
  356. eloop_unregister_read_sock(hapd->ctrl_sock);
  357. close(hapd->ctrl_sock);
  358. hapd->ctrl_sock = -1;
  359. fname = hostapd_ctrl_iface_path(hapd);
  360. if (fname)
  361. unlink(fname);
  362. os_free(fname);
  363. if (hapd->conf->ctrl_interface &&
  364. rmdir(hapd->conf->ctrl_interface) < 0) {
  365. if (errno == ENOTEMPTY) {
  366. wpa_printf(MSG_DEBUG, "Control interface "
  367. "directory not empty - leaving it "
  368. "behind");
  369. } else {
  370. perror("rmdir[ctrl_interface]");
  371. }
  372. }
  373. }
  374. dst = hapd->ctrl_dst;
  375. while (dst) {
  376. prev = dst;
  377. dst = dst->next;
  378. os_free(prev);
  379. }
  380. }
  381. void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  382. char *buf, size_t len)
  383. {
  384. struct wpa_ctrl_dst *dst, *next;
  385. struct msghdr msg;
  386. int idx;
  387. struct iovec io[2];
  388. char levelstr[10];
  389. dst = hapd->ctrl_dst;
  390. if (hapd->ctrl_sock < 0 || dst == NULL)
  391. return;
  392. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  393. io[0].iov_base = levelstr;
  394. io[0].iov_len = os_strlen(levelstr);
  395. io[1].iov_base = buf;
  396. io[1].iov_len = len;
  397. os_memset(&msg, 0, sizeof(msg));
  398. msg.msg_iov = io;
  399. msg.msg_iovlen = 2;
  400. idx = 0;
  401. while (dst) {
  402. next = dst->next;
  403. if (level >= dst->debug_level) {
  404. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  405. (u8 *) dst->addr.sun_path, dst->addrlen);
  406. msg.msg_name = &dst->addr;
  407. msg.msg_namelen = dst->addrlen;
  408. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  409. fprintf(stderr, "CTRL_IFACE monitor[%d]: ",
  410. idx);
  411. perror("sendmsg");
  412. dst->errors++;
  413. if (dst->errors > 10) {
  414. hostapd_ctrl_iface_detach(
  415. hapd, &dst->addr,
  416. dst->addrlen);
  417. }
  418. } else
  419. dst->errors = 0;
  420. }
  421. idx++;
  422. dst = next;
  423. }
  424. }
  425. #endif /* CONFIG_NATIVE_WINDOWS */