ctrl_iface.c 13 KB

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