wlantest: Add per-TID RX/TX counters
[mech_eap.git] / wlantest / ctrl.c
index e97448d..7507c2f 100644 (file)
@@ -1,15 +1,9 @@
 /*
  * wlantest control interface
- * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2010-2013, 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"
@@ -296,6 +290,8 @@ static void ctrl_clear_sta_counters(struct wlantest *wt, int sock, u8 *cmd,
        }
 
        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);
 }
 
@@ -788,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;
        }
@@ -965,6 +986,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;
@@ -1004,6 +1034,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;
@@ -1129,14 +1162,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;
@@ -1153,6 +1189,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;
@@ -1236,6 +1350,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;