ctrl_iface.c 12 KB

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