hostapd: Replace UDP ctrl_iface global cookies with per-instance ones

The cookie values for UDP control interface commands was defined as a
static global array. This did not allow multi-BSS test cases to be
executed with UDP control interface. For example, after
    hapd1 = hostapd.add_bss(apdev[0], ifname1, 'bss-1.conf')
    hapd2 = hostapd.add_bss(apdev[0], ifname2, 'bss-2.conf')

hapd1->ping() did not work.

Move those cookie values to per-instance location in struct
hapd_interfaces and struct hostapd_data to fix this.

Signed-off-by: Janusz Dziedzic <janusz.dziedzic@gmail.com>
This commit is contained in:
Janusz Dziedzic 2020-01-12 23:02:23 +01:00 committed by Jouni Malinen
parent 4d14838421
commit 4b04223f24
2 changed files with 34 additions and 20 deletions

View file

@ -69,9 +69,6 @@
#define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256 #define HOSTAPD_CLI_DUP_VALUE_MAX_LEN 256
#ifdef CONFIG_CTRL_IFACE_UDP #ifdef CONFIG_CTRL_IFACE_UDP
#define COOKIE_LEN 8
static unsigned char cookie[COOKIE_LEN];
static unsigned char gcookie[COOKIE_LEN];
#define HOSTAPD_CTRL_IFACE_PORT 8877 #define HOSTAPD_CTRL_IFACE_PORT 8877
#define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50 #define HOSTAPD_CTRL_IFACE_PORT_LIMIT 50
#define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878 #define HOSTAPD_GLOBAL_CTRL_IFACE_PORT 8878
@ -3529,7 +3526,7 @@ static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
int reply_len; int reply_len;
int level = MSG_DEBUG; int level = MSG_DEBUG;
#ifdef CONFIG_CTRL_IFACE_UDP #ifdef CONFIG_CTRL_IFACE_UDP
unsigned char lcookie[COOKIE_LEN]; unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
#endif /* CONFIG_CTRL_IFACE_UDP */ #endif /* CONFIG_CTRL_IFACE_UDP */
res = recvfrom(sock, buf, sizeof(buf) - 1, 0, res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
@ -3554,28 +3551,30 @@ static void hostapd_ctrl_iface_receive(int sock, void *eloop_ctx,
#ifdef CONFIG_CTRL_IFACE_UDP #ifdef CONFIG_CTRL_IFACE_UDP
if (os_strcmp(buf, "GET_COOKIE") == 0) { if (os_strcmp(buf, "GET_COOKIE") == 0) {
os_memcpy(reply, "COOKIE=", 7); os_memcpy(reply, "COOKIE=", 7);
wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1, wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
cookie, COOKIE_LEN); hapd->ctrl_iface_cookie,
reply_len = 7 + 2 * COOKIE_LEN; CTRL_IFACE_COOKIE_LEN);
reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
goto done; goto done;
} }
if (os_strncmp(buf, "COOKIE=", 7) != 0 || if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) { hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
wpa_printf(MSG_DEBUG, wpa_printf(MSG_DEBUG,
"CTRL: No cookie in the request - drop request"); "CTRL: No cookie in the request - drop request");
os_free(reply); os_free(reply);
return; return;
} }
if (os_memcmp(cookie, lcookie, COOKIE_LEN) != 0) { if (os_memcmp(hapd->ctrl_iface_cookie, lcookie,
CTRL_IFACE_COOKIE_LEN) != 0) {
wpa_printf(MSG_DEBUG, wpa_printf(MSG_DEBUG,
"CTRL: Invalid cookie in the request - drop request"); "CTRL: Invalid cookie in the request - drop request");
os_free(reply); os_free(reply);
return; return;
} }
pos = buf + 7 + 2 * COOKIE_LEN; pos = buf + 7 + 2 * CTRL_IFACE_COOKIE_LEN;
while (*pos == ' ') while (*pos == ' ')
pos++; pos++;
#endif /* CONFIG_CTRL_IFACE_UDP */ #endif /* CONFIG_CTRL_IFACE_UDP */
@ -3664,7 +3663,7 @@ int hostapd_ctrl_iface_init(struct hostapd_data *hapd)
dl_list_init(&hapd->ctrl_dst); dl_list_init(&hapd->ctrl_dst);
hapd->ctrl_sock = -1; hapd->ctrl_sock = -1;
os_get_random(cookie, COOKIE_LEN); os_get_random(hapd->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
@ -4243,7 +4242,7 @@ static int hostapd_global_ctrl_iface_ifname(struct hapd_interfaces *interfaces,
static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx, static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
void *sock_ctx) void *sock_ctx)
{ {
void *interfaces = eloop_ctx; struct hapd_interfaces *interfaces = eloop_ctx;
char buffer[256], *buf = buffer; char buffer[256], *buf = buffer;
int res; int res;
struct sockaddr_storage from; struct sockaddr_storage from;
@ -4252,7 +4251,7 @@ static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
int reply_len; int reply_len;
const int reply_size = 4096; const int reply_size = 4096;
#ifdef CONFIG_CTRL_IFACE_UDP #ifdef CONFIG_CTRL_IFACE_UDP
unsigned char lcookie[COOKIE_LEN]; unsigned char lcookie[CTRL_IFACE_COOKIE_LEN];
#endif /* CONFIG_CTRL_IFACE_UDP */ #endif /* CONFIG_CTRL_IFACE_UDP */
res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0, res = recvfrom(sock, buffer, sizeof(buffer) - 1, 0,
@ -4281,28 +4280,30 @@ static void hostapd_global_ctrl_iface_receive(int sock, void *eloop_ctx,
#ifdef CONFIG_CTRL_IFACE_UDP #ifdef CONFIG_CTRL_IFACE_UDP
if (os_strcmp(buf, "GET_COOKIE") == 0) { if (os_strcmp(buf, "GET_COOKIE") == 0) {
os_memcpy(reply, "COOKIE=", 7); os_memcpy(reply, "COOKIE=", 7);
wpa_snprintf_hex(reply + 7, 2 * COOKIE_LEN + 1, wpa_snprintf_hex(reply + 7, 2 * CTRL_IFACE_COOKIE_LEN + 1,
gcookie, COOKIE_LEN); interfaces->ctrl_iface_cookie,
reply_len = 7 + 2 * COOKIE_LEN; CTRL_IFACE_COOKIE_LEN);
reply_len = 7 + 2 * CTRL_IFACE_COOKIE_LEN;
goto send_reply; goto send_reply;
} }
if (os_strncmp(buf, "COOKIE=", 7) != 0 || if (os_strncmp(buf, "COOKIE=", 7) != 0 ||
hexstr2bin(buf + 7, lcookie, COOKIE_LEN) < 0) { hexstr2bin(buf + 7, lcookie, CTRL_IFACE_COOKIE_LEN) < 0) {
wpa_printf(MSG_DEBUG, wpa_printf(MSG_DEBUG,
"CTRL: No cookie in the request - drop request"); "CTRL: No cookie in the request - drop request");
os_free(reply); os_free(reply);
return; return;
} }
if (os_memcmp(gcookie, lcookie, COOKIE_LEN) != 0) { if (os_memcmp(interfaces->ctrl_iface_cookie, lcookie,
CTRL_IFACE_COOKIE_LEN) != 0) {
wpa_printf(MSG_DEBUG, wpa_printf(MSG_DEBUG,
"CTRL: Invalid cookie in the request - drop request"); "CTRL: Invalid cookie in the request - drop request");
os_free(reply); os_free(reply);
return; return;
} }
buf += 7 + 2 * COOKIE_LEN; buf += 7 + 2 * CTRL_IFACE_COOKIE_LEN;
while (*buf == ' ') while (*buf == ' ')
buf++; buf++;
#endif /* CONFIG_CTRL_IFACE_UDP */ #endif /* CONFIG_CTRL_IFACE_UDP */
@ -4446,7 +4447,7 @@ int hostapd_global_ctrl_iface_init(struct hapd_interfaces *interface)
} }
} }
os_get_random(gcookie, COOKIE_LEN); os_get_random(interface->ctrl_iface_cookie, CTRL_IFACE_COOKIE_LEN);
#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE #ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;

View file

@ -38,6 +38,10 @@ union wps_event_data;
struct mesh_conf; struct mesh_conf;
#endif /* CONFIG_MESH */ #endif /* CONFIG_MESH */
#ifdef CONFIG_CTRL_IFACE_UDP
#define CTRL_IFACE_COOKIE_LEN 8
#endif /* CONFIG_CTRL_IFACE_UDP */
struct hostapd_iface; struct hostapd_iface;
struct hapd_interfaces { struct hapd_interfaces {
@ -72,6 +76,11 @@ struct hapd_interfaces {
#ifdef CONFIG_DPP #ifdef CONFIG_DPP
struct dpp_global *dpp; struct dpp_global *dpp;
#endif /* CONFIG_DPP */ #endif /* CONFIG_DPP */
#ifdef CONFIG_CTRL_IFACE_UDP
unsigned char ctrl_iface_cookie[CTRL_IFACE_COOKIE_LEN];
#endif /* CONFIG_CTRL_IFACE_UDP */
}; };
enum hostapd_chan_status { enum hostapd_chan_status {
@ -395,6 +404,10 @@ struct hostapd_data {
#ifdef CONFIG_SQLITE #ifdef CONFIG_SQLITE
sqlite3 *rad_attr_db; sqlite3 *rad_attr_db;
#endif /* CONFIG_SQLITE */ #endif /* CONFIG_SQLITE */
#ifdef CONFIG_CTRL_IFACE_UDP
unsigned char ctrl_iface_cookie[CTRL_IFACE_COOKIE_LEN];
#endif /* CONFIG_CTRL_IFACE_UDP */
}; };