ctrl_iface.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. return 0;
  179. }
  180. static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
  181. void *sock_ctx)
  182. {
  183. struct hostapd_data *hapd = eloop_ctx;
  184. char buf[256];
  185. int res;
  186. struct sockaddr_un from;
  187. socklen_t fromlen = sizeof(from);
  188. char *reply;
  189. const int reply_size = 4096;
  190. int reply_len;
  191. res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
  192. (struct sockaddr *) &from, &fromlen);
  193. if (res < 0) {
  194. perror("recvfrom(ctrl_iface)");
  195. return;
  196. }
  197. buf[res] = '\0';
  198. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);
  199. reply = os_malloc(reply_size);
  200. if (reply == NULL) {
  201. sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,
  202. fromlen);
  203. return;
  204. }
  205. os_memcpy(reply, "OK\n", 3);
  206. reply_len = 3;
  207. if (os_strcmp(buf, "PING") == 0) {
  208. os_memcpy(reply, "PONG\n", 5);
  209. reply_len = 5;
  210. } else if (os_strcmp(buf, "MIB") == 0) {
  211. reply_len = ieee802_11_get_mib(hapd, reply, reply_size);
  212. if (reply_len >= 0) {
  213. res = wpa_get_mib(hapd->wpa_auth, reply + reply_len,
  214. reply_size - reply_len);
  215. if (res < 0)
  216. reply_len = -1;
  217. else
  218. reply_len += res;
  219. }
  220. if (reply_len >= 0) {
  221. res = ieee802_1x_get_mib(hapd, reply + reply_len,
  222. reply_size - reply_len);
  223. if (res < 0)
  224. reply_len = -1;
  225. else
  226. reply_len += res;
  227. }
  228. if (reply_len >= 0) {
  229. res = radius_client_get_mib(hapd->radius,
  230. reply + reply_len,
  231. reply_size - reply_len);
  232. if (res < 0)
  233. reply_len = -1;
  234. else
  235. reply_len += res;
  236. }
  237. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  238. reply_len = hostapd_ctrl_iface_sta_first(hapd, reply,
  239. reply_size);
  240. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  241. reply_len = hostapd_ctrl_iface_sta(hapd, buf + 4, reply,
  242. reply_size);
  243. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  244. reply_len = hostapd_ctrl_iface_sta_next(hapd, buf + 9, reply,
  245. reply_size);
  246. } else if (os_strcmp(buf, "ATTACH") == 0) {
  247. if (hostapd_ctrl_iface_attach(hapd, &from, fromlen))
  248. reply_len = -1;
  249. } else if (os_strcmp(buf, "DETACH") == 0) {
  250. if (hostapd_ctrl_iface_detach(hapd, &from, fromlen))
  251. reply_len = -1;
  252. } else if (os_strncmp(buf, "LEVEL ", 6) == 0) {
  253. if (hostapd_ctrl_iface_level(hapd, &from, fromlen,
  254. buf + 6))
  255. reply_len = -1;
  256. } else if (os_strncmp(buf, "NEW_STA ", 8) == 0) {
  257. if (hostapd_ctrl_iface_new_sta(hapd, buf + 8))
  258. reply_len = -1;
  259. } else {
  260. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  261. reply_len = 16;
  262. }
  263. if (reply_len < 0) {
  264. os_memcpy(reply, "FAIL\n", 5);
  265. reply_len = 5;
  266. }
  267. sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);
  268. os_free(reply);
  269. }
  270. static char * hostapd_ctrl_iface_path(struct hostapd_data *hapd)
  271. {
  272. char *buf;
  273. size_t len;
  274. if (hapd->conf->ctrl_interface == NULL)
  275. return NULL;
  276. len = os_strlen(hapd->conf->ctrl_interface) +
  277. os_strlen(hapd->conf->iface) + 2;
  278. buf = os_malloc(len);
  279. if (buf == NULL)
  280. return NULL;
  281. os_snprintf(buf, len, "%s/%s",
  282. hapd->conf->ctrl_interface, hapd->conf->iface);
  283. buf[len - 1] = '\0';
  284. return buf;
  285. }
  286. int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
  287. {
  288. struct sockaddr_un addr;
  289. int s = -1;
  290. char *fname = NULL;
  291. hapd->ctrl_sock = -1;
  292. if (hapd->conf->ctrl_interface == NULL)
  293. return 0;
  294. if (mkdir(hapd->conf->ctrl_interface, S_IRWXU | S_IRWXG) < 0) {
  295. if (errno == EEXIST) {
  296. wpa_printf(MSG_DEBUG, "Using existing control "
  297. "interface directory.");
  298. } else {
  299. perror("mkdir[ctrl_interface]");
  300. goto fail;
  301. }
  302. }
  303. if (hapd->conf->ctrl_interface_gid_set &&
  304. chown(hapd->conf->ctrl_interface, 0,
  305. hapd->conf->ctrl_interface_gid) < 0) {
  306. perror("chown[ctrl_interface]");
  307. return -1;
  308. }
  309. if (os_strlen(hapd->conf->ctrl_interface) + 1 +
  310. os_strlen(hapd->conf->iface) >= sizeof(addr.sun_path))
  311. goto fail;
  312. s = socket(PF_UNIX, SOCK_DGRAM, 0);
  313. if (s < 0) {
  314. perror("socket(PF_UNIX)");
  315. goto fail;
  316. }
  317. os_memset(&addr, 0, sizeof(addr));
  318. addr.sun_family = AF_UNIX;
  319. fname = hostapd_ctrl_iface_path(hapd);
  320. if (fname == NULL)
  321. goto fail;
  322. os_strlcpy(addr.sun_path, fname, sizeof(addr.sun_path));
  323. if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  324. perror("bind(PF_UNIX)");
  325. goto fail;
  326. }
  327. if (hapd->conf->ctrl_interface_gid_set &&
  328. chown(fname, 0, hapd->conf->ctrl_interface_gid) < 0) {
  329. perror("chown[ctrl_interface/ifname]");
  330. goto fail;
  331. }
  332. if (chmod(fname, S_IRWXU | S_IRWXG) < 0) {
  333. perror("chmod[ctrl_interface/ifname]");
  334. goto fail;
  335. }
  336. os_free(fname);
  337. hapd->ctrl_sock = s;
  338. eloop_register_read_sock(s, hostapd_ctrl_iface_receive, hapd,
  339. NULL);
  340. return 0;
  341. fail:
  342. if (s >= 0)
  343. close(s);
  344. if (fname) {
  345. unlink(fname);
  346. os_free(fname);
  347. }
  348. return -1;
  349. }
  350. void hostapd_ctrl_iface_deinit(struct hostapd_data *hapd)
  351. {
  352. struct wpa_ctrl_dst *dst, *prev;
  353. if (hapd->ctrl_sock > -1) {
  354. char *fname;
  355. eloop_unregister_read_sock(hapd->ctrl_sock);
  356. close(hapd->ctrl_sock);
  357. hapd->ctrl_sock = -1;
  358. fname = hostapd_ctrl_iface_path(hapd);
  359. if (fname)
  360. unlink(fname);
  361. os_free(fname);
  362. if (hapd->conf->ctrl_interface &&
  363. rmdir(hapd->conf->ctrl_interface) < 0) {
  364. if (errno == ENOTEMPTY) {
  365. wpa_printf(MSG_DEBUG, "Control interface "
  366. "directory not empty - leaving it "
  367. "behind");
  368. } else {
  369. perror("rmdir[ctrl_interface]");
  370. }
  371. }
  372. }
  373. dst = hapd->ctrl_dst;
  374. while (dst) {
  375. prev = dst;
  376. dst = dst->next;
  377. os_free(prev);
  378. }
  379. }
  380. void hostapd_ctrl_iface_send(struct hostapd_data *hapd, int level,
  381. char *buf, size_t len)
  382. {
  383. struct wpa_ctrl_dst *dst, *next;
  384. struct msghdr msg;
  385. int idx;
  386. struct iovec io[2];
  387. char levelstr[10];
  388. dst = hapd->ctrl_dst;
  389. if (hapd->ctrl_sock < 0 || dst == NULL)
  390. return;
  391. os_snprintf(levelstr, sizeof(levelstr), "<%d>", level);
  392. io[0].iov_base = levelstr;
  393. io[0].iov_len = os_strlen(levelstr);
  394. io[1].iov_base = buf;
  395. io[1].iov_len = len;
  396. os_memset(&msg, 0, sizeof(msg));
  397. msg.msg_iov = io;
  398. msg.msg_iovlen = 2;
  399. idx = 0;
  400. while (dst) {
  401. next = dst->next;
  402. if (level >= dst->debug_level) {
  403. wpa_hexdump(MSG_DEBUG, "CTRL_IFACE monitor send",
  404. (u8 *) dst->addr.sun_path, dst->addrlen);
  405. msg.msg_name = &dst->addr;
  406. msg.msg_namelen = dst->addrlen;
  407. if (sendmsg(hapd->ctrl_sock, &msg, 0) < 0) {
  408. fprintf(stderr, "CTRL_IFACE monitor[%d]: ",
  409. idx);
  410. perror("sendmsg");
  411. dst->errors++;
  412. if (dst->errors > 10) {
  413. hostapd_ctrl_iface_detach(
  414. hapd, &dst->addr,
  415. dst->addrlen);
  416. }
  417. } else
  418. dst->errors = 0;
  419. }
  420. idx++;
  421. dst = next;
  422. }
  423. }
  424. #endif /* CONFIG_NATIVE_WINDOWS */