ctrl_iface.c 13 KB

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