ctrl_iface.c 13 KB

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