ctrl_iface.c 12 KB

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