ctrl_iface.c 12 KB

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