ctrl_iface.c 13 KB

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