ctrl_iface.c 13 KB

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