tests: Increase connection timeouts for remote tests
[mech_eap.git] / wlantest / ctrl.c
index 01449cb..7de0a8a 100644 (file)
@@ -1,15 +1,9 @@
 /*
  * wlantest control interface
- * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2010-2015, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  */
 
 #include "utils/includes.h"
@@ -192,6 +186,33 @@ static struct wlantest_sta * ctrl_get_sta(struct wlantest *wt, int sock,
 }
 
 
+static struct wlantest_sta * ctrl_get_sta2(struct wlantest *wt, int sock,
+                                          u8 *cmd, size_t clen,
+                                          struct wlantest_bss *bss)
+{
+       struct wlantest_sta *sta;
+       u8 *pos;
+       size_t len;
+
+       if (bss == NULL)
+               return NULL;
+
+       pos = attr_get(cmd, clen, WLANTEST_ATTR_STA2_ADDR, &len);
+       if (pos == NULL || len != ETH_ALEN) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
+               return NULL;
+       }
+
+       sta = sta_find(bss, pos);
+       if (sta == NULL) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
+               return NULL;
+       }
+
+       return sta;
+}
+
+
 static void ctrl_list_bss(struct wlantest *wt, int sock)
 {
        u8 buf[WLANTEST_CTRL_MAX_RESP_LEN], *pos, *len;
@@ -263,10 +284,14 @@ static void ctrl_clear_sta_counters(struct wlantest *wt, int sock, u8 *cmd,
 
        bss = ctrl_get_bss(wt, sock, cmd, clen);
        sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
-       if (sta == NULL)
+       if (sta == NULL) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
                return;
+       }
 
        os_memset(sta->counters, 0, sizeof(sta->counters));
+       os_memset(sta->tx_tid, 0, sizeof(sta->tx_tid));
+       os_memset(sta->rx_tid, 0, sizeof(sta->rx_tid));
        ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
 }
 
@@ -277,14 +302,41 @@ static void ctrl_clear_bss_counters(struct wlantest *wt, int sock, u8 *cmd,
        struct wlantest_bss *bss;
 
        bss = ctrl_get_bss(wt, sock, cmd, clen);
-       if (bss == NULL)
+       if (bss == NULL) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
                return;
+       }
 
        os_memset(bss->counters, 0, sizeof(bss->counters));
        ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
 }
 
 
+static void ctrl_clear_tdls_counters(struct wlantest *wt, int sock, u8 *cmd,
+                                    size_t clen)
+{
+       struct wlantest_bss *bss;
+       struct wlantest_sta *sta;
+       struct wlantest_sta *sta2;
+       struct wlantest_tdls *tdls;
+
+       bss = ctrl_get_bss(wt, sock, cmd, clen);
+       sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
+       sta2 = ctrl_get_sta2(wt, sock, cmd, clen, bss);
+       if (sta == NULL || sta2 == NULL) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
+               return;
+       }
+
+       dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
+               if ((tdls->init == sta && tdls->resp == sta2) ||
+                   (tdls->init == sta2 && tdls->resp == sta))
+                       os_memset(tdls->counters, 0, sizeof(tdls->counters));
+       }
+       ctrl_send_simple(wt, sock, WLANTEST_CTRL_SUCCESS);
+}
+
+
 static void ctrl_get_sta_counter(struct wlantest *wt, int sock, u8 *cmd,
                                 size_t clen)
 {
@@ -355,6 +407,60 @@ static void ctrl_get_bss_counter(struct wlantest *wt, int sock, u8 *cmd,
 }
 
 
+static void ctrl_get_tdls_counter(struct wlantest *wt, int sock, u8 *cmd,
+                                 size_t clen)
+{
+       u8 *addr;
+       size_t addr_len;
+       struct wlantest_bss *bss;
+       struct wlantest_sta *sta;
+       struct wlantest_sta *sta2;
+       struct wlantest_tdls *tdls;
+       u32 counter;
+       u8 buf[4 + 12], *end, *pos;
+       int found = 0;
+
+       bss = ctrl_get_bss(wt, sock, cmd, clen);
+       sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
+       sta2 = ctrl_get_sta2(wt, sock, cmd, clen, bss);
+       if (sta == NULL || sta2 == NULL) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
+               return;
+       }
+
+       addr = attr_get(cmd, clen, WLANTEST_ATTR_TDLS_COUNTER, &addr_len);
+       if (addr == NULL || addr_len != 4) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
+               return;
+       }
+       counter = WPA_GET_BE32(addr);
+       if (counter >= NUM_WLANTEST_TDLS_COUNTER) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
+               return;
+       }
+
+       dl_list_for_each(tdls, &bss->tdls, struct wlantest_tdls, list) {
+               if (tdls->init == sta && tdls->resp == sta2) {
+                       found = 1;
+                       break;
+               }
+       }
+
+       if (!found) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
+               return;
+       }
+
+       pos = buf;
+       end = buf + sizeof(buf);
+       WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
+       pos += 4;
+       pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
+                           tdls->counters[counter]);
+       ctrl_send(wt, sock, buf, pos - buf);
+}
+
+
 static void build_mgmt_hdr(struct ieee80211_mgmt *mgmt,
                           struct wlantest_bss *bss, struct wlantest_sta *sta,
                           int sender_ap, int stype)
@@ -678,7 +784,32 @@ static void ctrl_add_passphrase(struct wlantest *wt, int sock, u8 *cmd,
        u8 *bssid;
 
        passphrase = attr_get(cmd, clen, WLANTEST_ATTR_PASSPHRASE, &len);
-       if (passphrase == NULL || len < 8 || len > 63) {
+       if (passphrase == NULL) {
+               u8 *wepkey;
+               char *key;
+               enum wlantest_ctrl_cmd res;
+
+               wepkey = attr_get(cmd, clen, WLANTEST_ATTR_WEPKEY, &len);
+               if (wepkey == NULL) {
+                       ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
+                       return;
+               }
+               key = os_zalloc(len + 1);
+               if (key == NULL) {
+                       ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
+                       return;
+               }
+               os_memcpy(key, wepkey, len);
+               if (add_wep(wt, key) < 0)
+                       res = WLANTEST_CTRL_FAILURE;
+               else
+                       res = WLANTEST_CTRL_SUCCESS;
+               os_free(key);
+               ctrl_send_simple(wt, sock, res);
+               return;
+       }
+
+       if (len < 8 || len > 63) {
                ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
                return;
        }
@@ -775,6 +906,15 @@ static void info_print_cipher(char *buf, size_t len, int cipher)
        if (cipher & WPA_CIPHER_AES_128_CMAC)
                pos += os_snprintf(pos, end - pos, "%sBIP",
                                   pos == buf ? "" : " ");
+       if (cipher & WPA_CIPHER_BIP_GMAC_128)
+               pos += os_snprintf(pos, end - pos, "%sBIP-GMAC-128",
+                                  pos == buf ? "" : " ");
+       if (cipher & WPA_CIPHER_BIP_GMAC_256)
+               pos += os_snprintf(pos, end - pos, "%sBIP-GMAC-256",
+                                  pos == buf ? "" : " ");
+       if (cipher & WPA_CIPHER_BIP_CMAC_256)
+               pos += os_snprintf(pos, end - pos, "%sBIP-CMAC-256",
+                                  pos == buf ? "" : " ");
 }
 
 
@@ -811,6 +951,12 @@ static void info_print_key_mgmt(char *buf, size_t len, int key_mgmt)
        if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
                pos += os_snprintf(pos, end - pos, "%sPSK-SHA256",
                                   pos == buf ? "" : " ");
+       if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B)
+               pos += os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
+                                  pos == buf ? "" : " ");
+       if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
+               pos += os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
+                                  pos == buf ? "" : " ");
 }
 
 
@@ -855,6 +1001,15 @@ static void info_print_state(char *buf, size_t len, int state)
 }
 
 
+static void info_print_gtk(char *buf, size_t len, struct wlantest_sta *sta)
+{
+       size_t pos;
+
+       pos = os_snprintf(buf, len, "IDX=%d,GTK=", sta->gtk_idx);
+       wpa_snprintf_hex(buf + pos, len - pos, sta->gtk, sta->gtk_len);
+}
+
+
 static void ctrl_info_sta(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
 {
        u8 *addr;
@@ -894,6 +1049,9 @@ static void ctrl_info_sta(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
        case WLANTEST_STA_INFO_STATE:
                info_print_state(resp, sizeof(resp), sta->state);
                break;
+       case WLANTEST_STA_INFO_GTK:
+               info_print_gtk(resp, sizeof(resp), sta);
+               break;
        default:
                ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
                return;
@@ -1019,14 +1177,17 @@ static void ctrl_send_(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
        }
 
        bss = bss_find(wt, bssid);
-       if (bss == NULL) {
+       if (bss == NULL && prot != WLANTEST_INJECT_UNPROTECTED) {
                wpa_printf(MSG_INFO, "Unknown BSSID");
                ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
                return;
        }
 
-       sta = sta_find(bss, sta_addr);
-       if (sta == NULL) {
+       if (bss)
+               sta = sta_find(bss, sta_addr);
+       else
+               sta = NULL;
+       if (sta == NULL && prot != WLANTEST_INJECT_UNPROTECTED) {
                wpa_printf(MSG_INFO, "Unknown STA address");
                ctrl_send_simple(wt, sock, WLANTEST_CTRL_FAILURE);
                return;
@@ -1043,6 +1204,84 @@ static void ctrl_send_(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
 }
 
 
+static void ctrl_relog(struct wlantest *wt, int sock)
+{
+       int res = wlantest_relog(wt);
+       ctrl_send_simple(wt, sock, res ? WLANTEST_CTRL_FAILURE :
+                        WLANTEST_CTRL_SUCCESS);
+}
+
+
+static void ctrl_get_tx_tid(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
+{
+       u8 *addr;
+       size_t addr_len;
+       struct wlantest_bss *bss;
+       struct wlantest_sta *sta;
+       u32 counter;
+       u8 buf[4 + 12], *end, *pos;
+
+       bss = ctrl_get_bss(wt, sock, cmd, clen);
+       sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
+       if (sta == NULL)
+               return;
+
+       addr = attr_get(cmd, clen, WLANTEST_ATTR_TID, &addr_len);
+       if (addr == NULL || addr_len != 4) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
+               return;
+       }
+       counter = WPA_GET_BE32(addr);
+       if (counter >= 16 + 1) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
+               return;
+       }
+
+       pos = buf;
+       end = buf + sizeof(buf);
+       WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
+       pos += 4;
+       pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
+                           sta->tx_tid[counter]);
+       ctrl_send(wt, sock, buf, pos - buf);
+}
+
+
+static void ctrl_get_rx_tid(struct wlantest *wt, int sock, u8 *cmd, size_t clen)
+{
+       u8 *addr;
+       size_t addr_len;
+       struct wlantest_bss *bss;
+       struct wlantest_sta *sta;
+       u32 counter;
+       u8 buf[4 + 12], *end, *pos;
+
+       bss = ctrl_get_bss(wt, sock, cmd, clen);
+       sta = ctrl_get_sta(wt, sock, cmd, clen, bss);
+       if (sta == NULL)
+               return;
+
+       addr = attr_get(cmd, clen, WLANTEST_ATTR_TID, &addr_len);
+       if (addr == NULL || addr_len != 4) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
+               return;
+       }
+       counter = WPA_GET_BE32(addr);
+       if (counter >= 16 + 1) {
+               ctrl_send_simple(wt, sock, WLANTEST_CTRL_INVALID_CMD);
+               return;
+       }
+
+       pos = buf;
+       end = buf + sizeof(buf);
+       WPA_PUT_BE32(pos, WLANTEST_CTRL_SUCCESS);
+       pos += 4;
+       pos = attr_add_be32(pos, end, WLANTEST_ATTR_COUNTER,
+                           sta->rx_tid[counter]);
+       ctrl_send(wt, sock, buf, pos - buf);
+}
+
+
 static void ctrl_read(int sock, void *eloop_ctx, void *sock_ctx)
 {
        struct wlantest *wt = eloop_ctx;
@@ -1096,12 +1335,18 @@ static void ctrl_read(int sock, void *eloop_ctx, void *sock_ctx)
        case WLANTEST_CTRL_CLEAR_BSS_COUNTERS:
                ctrl_clear_bss_counters(wt, sock, buf + 4, len - 4);
                break;
+       case WLANTEST_CTRL_CLEAR_TDLS_COUNTERS:
+               ctrl_clear_tdls_counters(wt, sock, buf + 4, len - 4);
+               break;
        case WLANTEST_CTRL_GET_STA_COUNTER:
                ctrl_get_sta_counter(wt, sock, buf + 4, len - 4);
                break;
        case WLANTEST_CTRL_GET_BSS_COUNTER:
                ctrl_get_bss_counter(wt, sock, buf + 4, len - 4);
                break;
+       case WLANTEST_CTRL_GET_TDLS_COUNTER:
+               ctrl_get_tdls_counter(wt, sock, buf + 4, len - 4);
+               break;
        case WLANTEST_CTRL_INJECT:
                ctrl_inject(wt, sock, buf + 4, len - 4);
                break;
@@ -1120,6 +1365,15 @@ static void ctrl_read(int sock, void *eloop_ctx, void *sock_ctx)
        case WLANTEST_CTRL_SEND:
                ctrl_send_(wt, sock, buf + 4, len - 4);
                break;
+       case WLANTEST_CTRL_RELOG:
+               ctrl_relog(wt, sock);
+               break;
+       case WLANTEST_CTRL_GET_TX_TID:
+               ctrl_get_tx_tid(wt, sock, buf + 4, len - 4);
+               break;
+       case WLANTEST_CTRL_GET_RX_TID:
+               ctrl_get_rx_tid(wt, sock, buf + 4, len - 4);
+               break;
        default:
                ctrl_send_simple(wt, sock, WLANTEST_CTRL_UNKNOWN_CMD);
                break;