static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, void *sock_ctx) { struct wpa_supplicant *wpa_s = eloop_ctx; struct ctrl_iface_priv *priv = sock_ctx; char buf[256]; int res; struct sockaddr_un from; socklen_t fromlen = sizeof(from); char *reply = NULL; size_t reply_len = 0; int new_attached = 0; res = recvfrom(sock, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &from, &fromlen); if (res < 0) { perror("recvfrom(ctrl_iface)"); return; } buf[res] = '\0'; if (os_strcmp(buf, "ATTACH") == 0) { if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen)) reply_len = 1; else { new_attached = 1; reply_len = 2; } } else if (os_strcmp(buf, "DETACH") == 0) { if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen)) reply_len = 1; else reply_len = 2; } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen, buf + 6)) reply_len = 1; else reply_len = 2; } else { reply = wpa_supplicant_ctrl_iface_process(wpa_s, buf, &reply_len); } if (reply) { sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen); os_free(reply); } else if (reply_len == 1) { sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, fromlen); } else if (reply_len == 2) { sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from, fromlen); } if (new_attached) eapol_sm_notify_ctrl_attached(wpa_s->eapol); }
void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv) { char buf[256]; int res; struct sockaddr_un from; socklen_t fromlen = sizeof(from); for (;;) { wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor to " "attach", priv->wpa_s->ifname); eloop_wait_for_read_sock(priv->sock); res = recvfrom(priv->sock, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &from, &fromlen); if (res < 0) { perror("recvfrom(ctrl_iface)"); continue; } buf[res] = '\0'; if (os_strcmp(buf, "ATTACH") == 0) { /* handle ATTACH signal of first monitor interface */ if (!wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen)) { sendto(priv->sock, "OK\n", 3, 0, (struct sockaddr *) &from, fromlen); /* OK to continue */ return; } else { sendto(priv->sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, fromlen); } } else { /* return FAIL for all other signals */ sendto(priv->sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, fromlen); } } }
static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, void *sock_ctx) { struct wpa_supplicant *wpa_s = eloop_ctx; struct ctrl_iface_priv *priv = sock_ctx; char buf[4096]; int res; struct sockaddr_storage from; socklen_t fromlen = sizeof(from); char *reply = NULL, *reply_buf = NULL; size_t reply_len = 0; int new_attached = 0; res = recvfrom(sock, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &from, &fromlen); if (res < 0) { wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", strerror(errno)); return; } buf[res] = '\0'; if (os_strcmp(buf, "ATTACH") == 0) { if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from, fromlen, 0)) reply_len = 1; else { new_attached = 1; reply_len = 2; } } else if (os_strcmp(buf, "DETACH") == 0) { if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from, fromlen)) reply_len = 1; else reply_len = 2; } else if (os_strncmp(buf, "LEVEL ", 6) == 0) { if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen, buf + 6)) reply_len = 1; else reply_len = 2; } else { reply_buf = wpa_supplicant_ctrl_iface_process(wpa_s, buf, &reply_len); reply = reply_buf; /* * There could be some password/key material in the command, so * clear the buffer explicitly now that it is not needed * anymore. */ os_memset(buf, 0, res); } if (!reply && reply_len == 1) { reply = "FAIL\n"; reply_len = 5; } else if (!reply && reply_len == 2) { reply = "OK\n"; reply_len = 3; } if (reply) { wpas_ctrl_sock_debug("ctrl_sock-sendto", sock, reply, reply_len); if (sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen) < 0) { int _errno = errno; wpa_dbg(wpa_s, MSG_DEBUG, "ctrl_iface sendto failed: %d - %s", _errno, strerror(_errno)); if (_errno == ENOBUFS || _errno == EAGAIN) { /* * The socket send buffer could be full. This * may happen if client programs are not * receiving their pending messages. Close and * reopen the socket as a workaround to avoid * getting stuck being unable to send any new * responses. */ sock = wpas_ctrl_iface_reinit(wpa_s, priv); if (sock < 0) { wpa_dbg(wpa_s, MSG_DEBUG, "Failed to reinitialize ctrl_iface socket"); } } if (new_attached) { wpa_dbg(wpa_s, MSG_DEBUG, "Failed to send response to ATTACH - detaching"); new_attached = 0; wpa_supplicant_ctrl_iface_detach( &priv->ctrl_dst, &from, fromlen); } } } os_free(reply_buf); if (new_attached) eapol_sm_notify_ctrl_attached(wpa_s->eapol); }
static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv, #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 struct sockaddr_in6 *from, #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ struct sockaddr_in *from, #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ socklen_t fromlen) { struct wpa_ctrl_dst *dst, *prev = NULL; #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 char addr[INET6_ADDRSTRLEN]; #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ dst = priv->ctrl_dst; while (dst) { #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 if (from->sin6_port == dst->addr.sin6_port && !os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr, sizeof(from->sin6_addr))) { wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s:%d", inet_ntop(AF_INET6, &from->sin6_addr, addr, sizeof(*from)), ntohs(from->sin6_port)); #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr && from->sin_port == dst->addr.sin_port) { wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached " "%s:%d", inet_ntoa(from->sin_addr), ntohs(from->sin_port)); #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ if (prev == NULL) { priv->ctrl_dst = dst->next; } else { prev->next = dst->next; } os_free(dst); return 0; } prev = dst; dst = dst->next; } return -1; } static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv, #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 struct sockaddr_in6 *from, #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ struct sockaddr_in *from, #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ socklen_t fromlen, char *level) { struct wpa_ctrl_dst *dst; #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 char addr[INET6_ADDRSTRLEN]; #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level); dst = priv->ctrl_dst; while (dst) { #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 if (from->sin6_port == dst->addr.sin6_port && !os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr, sizeof(from->sin6_addr))) { wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level %s:%d", inet_ntop(AF_INET6, &from->sin6_addr, addr, sizeof(*from)), ntohs(from->sin6_port)); #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr && from->sin_port == dst->addr.sin_port) { wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor " "level %s:%d", inet_ntoa(from->sin_addr), ntohs(from->sin_port)); #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ dst->debug_level = atoi(level); return 0; } dst = dst->next; } return -1; } static char *wpa_supplicant_ctrl_iface_get_cookie(struct ctrl_iface_priv *priv, size_t *reply_len) { char *reply; reply = os_malloc(7 + 2 * COOKIE_LEN + 1); if (reply == NULL) { *reply_len = 1; return NULL; } os_memcpy(reply, "COOKIE=", 7); wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1, priv->cookie, COOKIE_LEN); *reply_len = 7 + 2 * COOKIE_LEN; return reply; } static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, void *sock_ctx) { struct wpa_supplicant *wpa_s = eloop_ctx; struct ctrl_iface_priv *priv = sock_ctx; char buf[256], *pos; int res; #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 struct sockaddr_in6 from; #ifndef CONFIG_CTRL_IFACE_UDP_REMOTE char addr[INET6_ADDRSTRLEN]; #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ struct sockaddr_in from; #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ socklen_t fromlen = sizeof(from); char *reply = NULL; size_t reply_len = 0; int new_attached = 0; u8 cookie[COOKIE_LEN]; res = wpa_ctrl_recvfrom(sock, buf, sizeof(buf) - 1, 0, (struct sockaddr *)&from, &fromlen); if (res < 0) { wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", strerror(errno)); return; } #ifndef CONFIG_CTRL_IFACE_UDP_REMOTE #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 inet_ntop(AF_INET6, &from.sin6_addr, addr, sizeof(from)); if (os_strcmp(addr, "::1")) { wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected source %s", addr); } #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) { /* * The OS networking stack is expected to drop this kind of * frames since the socket is bound to only localhost address. * Just in case, drop the frame if it is coming from any other * address. */ wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected " "source %s", inet_ntoa(from.sin_addr)); return; } #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ buf[res] = '\0'; if (os_strcmp(buf, "GET_COOKIE") == 0) { reply = wpa_supplicant_ctrl_iface_get_cookie(priv, &reply_len); goto done; } /* * Require that the client includes a prefix with the 'cookie' value * fetched with GET_COOKIE command. This is used to verify that the * client has access to a bidirectional link over UDP in order to * avoid attacks using forged localhost IP address even if the OS does * not block such frames from remote destinations. */ if (os_strncmp(buf, "COOKIE=", 7) != 0) { wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - " "drop request"); return; } if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) { wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the " "request - drop request"); return; } if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) { wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - " "drop request"); return; } pos = buf + 7 + 2 * COOKIE_LEN; while (*pos == ' ') { pos++; } if (os_strcmp(pos, "ATTACH") == 0) { if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen)) { reply_len = 1; } else { new_attached = 1; reply_len = 2; } } else if (os_strcmp(pos, "DETACH") == 0) { if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen)) { reply_len = 1; } else { reply_len = 2; } } else if (os_strncmp(pos, "LEVEL ", 6) == 0) { if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen, pos + 6)) { reply_len = 1; } else { reply_len = 2; } } else { reply = wpa_supplicant_ctrl_iface_process(wpa_s, pos, &reply_len); } done: if (reply) { wpa_ctrl_sendto(sock, reply, reply_len, 0, (struct sockaddr *)&from, fromlen); os_free(reply); } else if (reply_len == 1) { wpa_ctrl_sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *)&from, fromlen); } else if (reply_len == 2) { wpa_ctrl_sendto(sock, "OK\n", 3, 0, (struct sockaddr *)&from, fromlen); } if (new_attached) { eapol_sm_notify_ctrl_attached(wpa_s->eapol); } } static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level, enum wpa_msg_type type, const char *txt, size_t len) { struct wpa_supplicant *wpa_s = ctx; if (wpa_s == NULL || wpa_s->ctrl_iface == NULL) { return; } wpa_supplicant_ctrl_iface_send(wpa_s->ctrl_iface, level, txt, len); } struct ctrl_iface_priv *wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s) { struct ctrl_iface_priv *priv; int port = WPA_CTRL_IFACE_PORT; #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 struct sockaddr_in6 addr; int domain = PF_INET6; #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ struct sockaddr_in addr; int domain = PF_INET; #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ priv = os_zalloc(sizeof(*priv)); if (priv == NULL) { return NULL; } priv->wpa_s = wpa_s; priv->sock = -1; os_get_random(priv->cookie, COOKIE_LEN); if (wpa_s->conf->ctrl_interface == NULL) { return priv; } /* WARNING: UDP is unreliable (indirectly selected by use of SOCK_DRAM), hence not suitable * for API interfaces. * - change to use TCP (SOCK_STREAM), for a reliable transport. */ priv->sock = socket(domain, SOCK_DGRAM, 0); if (priv->sock < 0) { wpa_printf(MSG_ERROR, "socket(PF_INET): %s", strerror(errno)); goto fail; } os_memset(&addr, 0, sizeof(addr)); #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 addr.sin6_family = AF_INET6; #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE addr.sin6_addr = in6addr_any; #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */ inet_pton(AF_INET6, "::1", &addr.sin6_addr); #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ addr.sin_family = AF_INET; #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE addr.sin_addr.s_addr = INADDR_ANY; #else /* CONFIG_CTRL_IFACE_UDP_REMOTE */ addr.sin_addr.s_addr = htonl((127 << 24) | 1); #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ try_again: #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 addr.sin6_port = htons(port); #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ addr.sin_port = htons(port); #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ if (bind(priv->sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { port--; if ((WPA_CTRL_IFACE_PORT - port) < WPA_CTRL_IFACE_PORT_LIMIT) { goto try_again; } wpa_printf(MSG_ERROR, "bind(AF_INET): %s", strerror(errno)); goto fail; } #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE wpa_msg(wpa_s, MSG_DEBUG, "ctrl_iface_init UDP port: %d", port); #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ eloop_register_read_sock(priv->sock, wpa_supplicant_ctrl_iface_receive, wpa_s, priv); wpa_msg_register_cb(wpa_supplicant_ctrl_iface_msg_cb); return priv; fail: if (priv->sock >= 0) { close(priv->sock); } os_free(priv); return NULL; } void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv) { struct wpa_ctrl_dst *dst, *prev; if (priv->sock > -1) { eloop_unregister_read_sock(priv->sock); if (priv->ctrl_dst) { /* * Wait before closing the control socket if * there are any attached monitors in order to allow * them to receive any pending messages. */ wpa_printf(MSG_DEBUG, "CTRL_IFACE wait for attached " "monitors to receive messages"); os_sleep(0, 100000); } close(priv->sock); priv->sock = -1; } dst = priv->ctrl_dst; while (dst) { prev = dst; dst = dst->next; os_free(prev); } os_free(priv); } static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv, int level, const char *buf, size_t len) { struct wpa_ctrl_dst *dst, *next; char levelstr[10]; int idx; char *sbuf; int llen; #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 char addr[INET6_ADDRSTRLEN]; #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ dst = priv->ctrl_dst; if (priv->sock < 0 || dst == NULL) { return; } os_snprintf(levelstr, sizeof(levelstr), "<%d>", level); llen = os_strlen(levelstr); sbuf = os_malloc(llen + len); if (sbuf == NULL) { return; } os_memcpy(sbuf, levelstr, llen); os_memcpy(sbuf + llen, buf, len); idx = 0; while (dst) { next = dst->next; if (level >= dst->debug_level) { #ifdef CONFIG_CTRL_IFACE_UDP_IPV6 wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d", inet_ntop(AF_INET6, &dst->addr.sin6_addr, addr, sizeof(dst->addr)), ntohs(dst->addr.sin6_port)); #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */ wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d", inet_ntoa(dst->addr.sin_addr), ntohs(dst->addr.sin_port)); #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */ if (wpa_ctrl_sendto(priv->sock, sbuf, llen + len, 0, (struct sockaddr *)&dst->addr, sizeof(dst->addr)) < 0) { wpa_printf(MSG_ERROR, "sendto(CTRL_IFACE monitor): %s", strerror(errno)); dst->errors++; if (dst->errors > 10) { wpa_supplicant_ctrl_iface_detach(priv, &dst->addr, dst->addrlen); } } else { dst->errors = 0; } } idx++; dst = next; } os_free(sbuf); } void wpa_supplicant_ctrl_iface_wait(struct ctrl_iface_priv *priv) { wpa_printf(MSG_DEBUG, "CTRL_IFACE - %s - wait for monitor", priv->wpa_s->ifname); eloop_wait_for_read_sock(priv->sock); }
static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, void *sock_ctx) { struct wpa_supplicant *wpa_s = eloop_ctx; struct ctrl_iface_priv *priv = sock_ctx; char buf[256], *pos; int res; struct sockaddr_in from; socklen_t fromlen = sizeof(from); char *reply = NULL; size_t reply_len = 0; int new_attached = 0; u8 cookie[COOKIE_LEN]; res = recvfrom(sock, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &from, &fromlen); if (res < 0) { perror("recvfrom(ctrl_iface)"); return; } if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) { /* * The OS networking stack is expected to drop this kind of * frames since the socket is bound to only localhost address. * Just in case, drop the frame if it is coming from any other * address. */ wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected " "source %s", inet_ntoa(from.sin_addr)); return; } buf[res] = '\0'; if (os_strcmp(buf, "GET_COOKIE") == 0) { reply = wpa_supplicant_ctrl_iface_get_cookie(priv, &reply_len); goto done; } /* * Require that the client includes a prefix with the 'cookie' value * fetched with GET_COOKIE command. This is used to verify that the * client has access to a bidirectional link over UDP in order to * avoid attacks using forged localhost IP address even if the OS does * not block such frames from remote destinations. */ if (os_strncmp(buf, "COOKIE=", 7) != 0) { wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - " "drop request"); return; } if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) { wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the " "request - drop request"); return; } if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) { wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - " "drop request"); return; } pos = buf + 7 + 2 * COOKIE_LEN; while (*pos == ' ') pos++; if (os_strcmp(pos, "ATTACH") == 0) { if (wpa_supplicant_ctrl_iface_attach(priv, &from, fromlen)) reply_len = 1; else { new_attached = 1; reply_len = 2; } } else if (os_strcmp(pos, "DETACH") == 0) { if (wpa_supplicant_ctrl_iface_detach(priv, &from, fromlen)) reply_len = 1; else reply_len = 2; } else if (os_strncmp(pos, "LEVEL ", 6) == 0) { if (wpa_supplicant_ctrl_iface_level(priv, &from, fromlen, pos + 6)) reply_len = 1; else reply_len = 2; } else { reply = wpa_supplicant_ctrl_iface_process(wpa_s, pos, &reply_len); } done: if (reply) { sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen); os_free(reply); } else if (reply_len == 1) { sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, fromlen); } else if (reply_len == 2) { sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from, fromlen); } if (new_attached) eapol_sm_notify_ctrl_attached(wpa_s->eapol); }
static void wpa_supplicant_global_ctrl_iface_receive(int sock, void *eloop_ctx, void *sock_ctx) { struct wpa_global *global = eloop_ctx; struct ctrl_iface_global_priv *priv = sock_ctx; char buf[256], *pos; int res; struct sockaddr_in from; socklen_t fromlen = sizeof(from); char *reply = NULL; size_t reply_len; u8 cookie[COOKIE_LEN]; res = recvfrom(sock, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &from, &fromlen); if (res < 0) { wpa_printf(MSG_ERROR, "recvfrom(ctrl_iface): %s", strerror(errno)); return; } #ifndef CONFIG_CTRL_IFACE_UDP_REMOTE if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) { /* * The OS networking stack is expected to drop this kind of * frames since the socket is bound to only localhost address. * Just in case, drop the frame if it is coming from any other * address. */ wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected " "source %s", inet_ntoa(from.sin_addr)); return; } #endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */ buf[res] = '\0'; if (os_strcmp(buf, "GET_COOKIE") == 0) { reply = wpa_supplicant_global_get_cookie(priv, &reply_len); goto done; } if (os_strncmp(buf, "COOKIE=", 7) != 0) { wpa_printf(MSG_DEBUG, "CTLR: No cookie in the request - " "drop request"); return; } if (hexstr2bin(buf + 7, cookie, COOKIE_LEN) < 0) { wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie format in the " "request - drop request"); return; } if (os_memcmp(cookie, priv->cookie, COOKIE_LEN) != 0) { wpa_printf(MSG_DEBUG, "CTLR: Invalid cookie in the request - " "drop request"); return; } pos = buf + 7 + 2 * COOKIE_LEN; while (*pos == ' ') pos++; if (os_strcmp(pos, "ATTACH") == 0) { if (wpa_supplicant_ctrl_iface_attach(&priv->ctrl_dst, &from, fromlen)) reply_len = 1; else reply_len = 2; } else if (os_strcmp(pos, "DETACH") == 0) { if (wpa_supplicant_ctrl_iface_detach(&priv->ctrl_dst, &from, fromlen)) reply_len = 1; else reply_len = 2; } else { reply = wpa_supplicant_global_ctrl_iface_process(global, pos, &reply_len); } done: if (reply) { sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen); os_free(reply); } else if (reply_len == 1) { sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, fromlen); } else if (reply_len == 2) { sendto(sock, "OK\n", 3, 0, (struct sockaddr *) &from, fromlen); } }
static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx, void *sock_ctx) { struct wpa_supplicant *wpa_s = eloop_ctx; char buf[256]; int res; CTRL_IFACE_SOCK from; socklen_t fromlen = sizeof(from); char *reply; const int reply_size = 2048; int reply_len; int new_attached = 0, ctrl_rsp = 0; res = recvfrom(sock, buf, sizeof(buf) - 1, 0, (struct sockaddr *) &from, &fromlen); if (res < 0) { perror("recvfrom(ctrl_iface)"); return; } buf[res] = '\0'; if (strncmp(buf, "CTRL-RSP-", 9) == 0) { wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res); } else { wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res); } reply = malloc(reply_size); if (reply == NULL) { sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from, fromlen); return; } memcpy(reply, "OK\n", 3); reply_len = 3; if (strcmp(buf, "PING") == 0) { memcpy(reply, "PONG\n", 5); reply_len = 5; } else if (strcmp(buf, "MIB") == 0) { reply_len = wpa_get_mib(wpa_s, reply, reply_size); if (reply_len >= 0) { res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len, reply_size - reply_len); if (res < 0) reply_len = -1; else reply_len += res; } } else if (strncmp(buf, "STATUS", 6) == 0) { reply_len = wpa_supplicant_ctrl_iface_status( wpa_s, buf + 6, reply, reply_size); } else if (strcmp(buf, "PMKSA") == 0) { reply_len = pmksa_cache_list(wpa_s, reply, reply_size); } else if (strncmp(buf, "SET ", 4) == 0) { if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4)) reply_len = -1; } else if (strcmp(buf, "LOGON") == 0) { eapol_sm_notify_logoff(wpa_s->eapol, FALSE); } else if (strcmp(buf, "LOGOFF") == 0) { eapol_sm_notify_logoff(wpa_s->eapol, TRUE); } else if (strcmp(buf, "REASSOCIATE") == 0) { wpa_s->reassociate = 1; wpa_supplicant_req_scan(wpa_s, 0, 0); } else if (strncmp(buf, "PREAUTH ", 8) == 0) { if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8)) reply_len = -1; } else if (strcmp(buf, "ATTACH") == 0) { if (wpa_supplicant_ctrl_iface_attach(wpa_s, &from, fromlen)) reply_len = -1; else new_attached = 1; } else if (strcmp(buf, "DETACH") == 0) { if (wpa_supplicant_ctrl_iface_detach(wpa_s, &from, fromlen)) reply_len = -1; } else if (strncmp(buf, "LEVEL ", 6) == 0) { if (wpa_supplicant_ctrl_iface_level(wpa_s, &from, fromlen, buf + 6)) reply_len = -1; } else if (strncmp(buf, "CTRL-RSP-", 9) == 0) { if (wpa_supplicant_ctrl_iface_ctrl_rsp(wpa_s, buf + 9)) reply_len = -1; else ctrl_rsp = 1; } else if (strcmp(buf, "RECONFIGURE") == 0) { if (wpa_supplicant_reload_configuration(wpa_s)) reply_len = -1; } else if (strcmp(buf, "TERMINATE") == 0) { eloop_terminate(); } else { memcpy(reply, "UNKNOWN COMMAND\n", 16); reply_len = 16; } if (reply_len < 0) { memcpy(reply, "FAIL\n", 5); reply_len = 5; } sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen); free(reply); if (new_attached) eapol_sm_notify_ctrl_attached(wpa_s->eapol); if (ctrl_rsp) eapol_sm_notify_ctrl_response(wpa_s->eapol); }