ctrl_iface.c 13 KB

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