From e9b02067eb584c2ac796a5e50c74fa787ae3dd04 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Mon, 24 Jul 2017 20:20:43 +0200 Subject: [PATCH] Add metric for deleting clients --- src/include/datastorage.h | 96 ++++++++++++++++---------------- src/include/ubus.h | 2 +- src/network/networksocket.c | 7 ++- src/storage/datastorage.c | 107 +++++++++++++++++++++++++++++++++--- src/utils/ubus.c | 37 +++++++------ 5 files changed, 171 insertions(+), 78 deletions(-) diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 81df38b..fdf585a 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -14,60 +14,66 @@ #endif #define SORT_NUM 5 -#define TIME_THRESHOLD 5 // every minute +#define TIME_THRESHOLD 600 // every minute // Probe entrys typedef struct probe_entry_s { - uint8_t bssid_addr[ETH_ALEN]; - uint8_t client_addr[ETH_ALEN]; - uint8_t target_addr[ETH_ALEN]; - uint32_t signal; - uint32_t freq; - uint8_t ht_support; - uint8_t vht_support; - time_t time; - int counter; + uint8_t bssid_addr[ETH_ALEN]; + uint8_t client_addr[ETH_ALEN]; + uint8_t target_addr[ETH_ALEN]; + uint32_t signal; + uint32_t freq; + uint8_t ht_support; + uint8_t vht_support; + time_t time; + int counter; } probe_entry; typedef struct { - uint32_t freq; + uint32_t freq; } client_request; typedef struct client_s { - uint8_t bssid_addr[ETH_ALEN]; - uint8_t client_addr[ETH_ALEN]; - uint8_t ht_supported; - uint8_t vht_supported; - uint32_t freq; - uint8_t auth; - uint8_t assoc; - uint8_t authorized; - uint8_t preauth; - uint8_t wds; - uint8_t wmm; - uint8_t ht; - uint8_t vht; - uint8_t wps; - uint8_t mfp; - time_t time; - uint32_t aid; + uint8_t bssid_addr[ETH_ALEN]; + uint8_t client_addr[ETH_ALEN]; + uint8_t ht_supported; + uint8_t vht_supported; + uint32_t freq; + uint8_t auth; + uint8_t assoc; + uint8_t authorized; + uint8_t preauth; + uint8_t wds; + uint8_t wmm; + uint8_t ht; + uint8_t vht; + uint8_t wps; + uint8_t mfp; + time_t time; + uint32_t aid; } client; // Array #define ARRAY_CLIENT_LEN 1000 -#define TIME_THRESHOLD_CLIENT 5 +#define TIME_THRESHOLD_CLIENT 10 +#define TIME_THRESHOLD_CLIENT_UPDATE 5 struct client_s client_array[ARRAY_CLIENT_LEN]; pthread_mutex_t client_array_mutex; void insert_client_to_array(client entry); + void kick_clients(uint8_t bssid[]); void client_array_insert(client entry); -client* client_array_delete(client entry); + +client *client_array_delete(client entry); + void print_client_array(); + void print_client_entry(client entry); + void *remove_client_array_thread(void *arg); #define ARRAY_LEN 1000 @@ -76,38 +82,30 @@ struct probe_entry_s probe_array[ARRAY_LEN]; pthread_mutex_t probe_array_mutex; void insert_to_array(probe_entry entry, int inc_counter); + void probe_array_insert(probe_entry entry); -probe_entry* probe_array_delete(probe_entry entry); + +probe_entry *probe_array_delete(probe_entry entry); + void print_array(); + void *remove_array_thread(void *arg); - - - - - - - - - - - - - - - - // List typedef struct node { - probe_entry data; - struct node *ptr; + probe_entry data; + struct node *ptr; } node; node *insert(node *head, probe_entry entry); + void free_list(node *head); + void print_list(); + void insert_to_list(probe_entry entry, int inc_counter); + int mac_first_in_probe_list(uint8_t bssid_addr[], uint8_t client_addr[]); void *remove_thread(void *arg); diff --git a/src/include/ubus.h b/src/include/ubus.h index da018b8..54f246f 100644 --- a/src/include/ubus.h +++ b/src/include/ubus.h @@ -8,7 +8,7 @@ int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir); int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req); -int parse_to_clients(struct blob_attr *msg); +int parse_to_clients(struct blob_attr *msg, int do_kick); void del_client(const uint8_t* client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time); void *update_clients_thread(void *arg); diff --git a/src/network/networksocket.c b/src/network/networksocket.c index 07ab8e3..b88f25f 100644 --- a/src/network/networksocket.c +++ b/src/network/networksocket.c @@ -69,7 +69,7 @@ void *receive_msg(void *args) { } recv_string[recv_string_len] = '\0'; - printf("[WC] Network-Received: %s\n", recv_string); + //printf("[WC] Network-Received: %s\n", recv_string); probe_entry prob_req; struct blob_buf b; @@ -84,6 +84,7 @@ void *receive_msg(void *args) { /* TODO: REFACTOR THIS!!! (just workaround) OTHERWISE NULLPOINTER?! + * MAYBE THIS IS UNNECESSARY :O */ @@ -100,7 +101,7 @@ void *receive_msg(void *args) { */ if (strstr(str, "clients") != NULL) { - parse_to_clients(b.head); + parse_to_clients(b.head, 0); } else if (strstr(str, "target") != NULL) { if (parse_to_probe_req(b.head, &prob_req) == 0) { insert_to_array(prob_req, 0); @@ -121,7 +122,7 @@ void *receive_msg(void *args) { int send_string(char *msg) { pthread_mutex_lock(&send_mutex); int msglen = strlen(msg); - printf("Sending string! %s\n", msg); + //printf("Sending string! %s\n", msg); if (sendto(sock, msg, msglen, diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index 01242bf..6a44e82 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -26,19 +26,110 @@ int client_array_go_next_help(char sort_order[], int i, client entry, void remove_old_client_entries(time_t current_time, long long int threshold); -int kick_client(uint8_t bssid[], uint8_t client[]); +int eval_probe_metric(struct client_s client_entry, struct probe_entry_s probe_entry); + +struct probe_metric_s +{ + int ht_support; + int vht_support; + int n_ht_support; + int n_vht_support; + int rssi; + int freq; +}; + +int kick_client(struct client_s client_entry); int probe_entry_last = -1; int client_entry_last = -1; -int kick_client(uint8_t bssid[], uint8_t client[]) { +int eval_probe_metric(struct client_s client_entry, struct probe_entry_s probe_entry) { + + struct probe_metric_s metric = { + .ht_support = 0, + .vht_support = 100, + .n_ht_support = 0, + .n_ht_support = 0, + .rssi = 0, + .freq = 0}; // this is for testing + + int score = 0; + + uint8_t client_supports_ht; + uint8_t client_supports_vht; + + uint8_t ap_supports_ht; + uint8_t ap_supports_vht; + + ap_supports_ht = client_entry.ht_supported; + ap_supports_vht = client_entry.vht_supported; + + client_supports_ht = probe_entry.ht_support; + client_supports_vht = probe_entry.vht_support; + + printf("Checking if client supports: AP_VHT: %d, CL_VHT: %d\n", ap_supports_vht, client_supports_vht); + if(ap_supports_vht && client_supports_vht){ + printf("AAAHHHHHHHHHHH IDEAL!!!\n"); + } + + score += (ap_supports_vht && client_supports_vht) ? metric.vht_support : 0; + score += (ap_supports_ht && client_supports_ht) ? metric.ht_support : 0; + + score += (!ap_supports_vht && !client_supports_vht) ? metric.n_vht_support : 0; + score += (!ap_supports_ht && !client_supports_ht) ? metric.n_ht_support : 0; + + score += (client_entry.freq > 5000) ? metric.freq : 0; + + //score += (client_entry.signal > -60) ? metric.freq : 0; + + printf("SCORE: %d\n",score); + + return score; +} + +//int kick_client(uint8_t bssid[], uint8_t client[]) { +int kick_client(struct client_s client_entry) { + + print_array(); + + int own_score = 0; + + // find first client entry in probe array int i; - for (i = 0; i <= client_entry_last; i++) { - if (mac_is_equal(probe_array[i].client_addr, client)) { - // check if bssid is first in list... - return (mac_is_equal(bssid, probe_array[i].bssid_addr)); + for (i = 0; i <= probe_entry_last; i++) { + if (mac_is_equal(probe_array[i].client_addr, client_entry.client_addr)) { + break; } } + printf("Found probe [i] : %d\n",i); + + // find own probe entry and calculate score + int j; + for (j = i; j <= probe_entry_last; j++) { + printf("[j] : %d\n",j); + if (!mac_is_equal(probe_array[j].client_addr, client_entry.client_addr)) { + // this shouldn't happen! + return 1; // kick client! + } + if (mac_is_equal(client_entry.bssid_addr, probe_array[j].bssid_addr)){ + own_score = eval_probe_metric(client_entry, probe_array[j]); + break; + } + } + + int k; + for (k = i; k <= probe_entry_last; k++) { + printf("[k] : %d\n",k); + if (!mac_is_equal(probe_array[k].client_addr, client_entry.client_addr)) { + break; + } + if(!mac_is_equal(client_entry.bssid_addr, probe_array[k].bssid_addr) && + own_score < eval_probe_metric(client_entry, probe_array[k])) + { + return 1; + } + } + return 0; } @@ -57,12 +148,12 @@ void kick_clients(uint8_t bssid[]) { if (!mac_is_equal(client_array[j].bssid_addr, bssid)) { break; } - if (kick_client(bssid, client_array[j].client_addr)) { + if (kick_client(client_array[j])) { /* TODO: KICK ONLY FROM ONE BSSID? */ printf("KICKING CLIENT!!!!!!!!!!!!!\n"); - //del_client(client_array[j].client_addr, 5, 1, 60000); + del_client(client_array[j].client_addr, 5, 1, 60000); } else { printf("STAAAY CLIENT!!!!!!!!!!!!!\n"); } diff --git a/src/utils/ubus.c b/src/utils/ubus.c index 201e3f7..00ab43b 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -98,6 +98,18 @@ static int subscribe_to_hostapd_interfaces(char *hostapd_dir); static int ubus_get_clients(); +/* hostapd function */ +#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" + +static void +blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr) { + char *s; + + s = blobmsg_alloc_string_buffer(buf, name, 20); + sprintf(s, MACSTR, MAC2STR(addr)); + blobmsg_add_string_buffer(buf); +} + /* static int decide_function(probe_entry *prob_req) { // TODO: Refactor... @@ -166,9 +178,9 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, str = blobmsg_format_json(msg, true); send_string(str); - printf("[WC] Hostapd-Probe: %s : %s\n", method, str); + //printf("[WC] Hostapd-Probe: %s : %s\n", method, str); - print_array(); + //print_array(); // sleep(2); // sleep for 2s @@ -331,7 +343,7 @@ dump_client_table(struct blob_attr *head, int len, const char *bssid_addr, uint3 } } -int parse_to_clients(struct blob_attr *msg) { +int parse_to_clients(struct blob_attr *msg, int do_kick) { struct blob_attr *tb[__CLIENT_TABLE_MAX]; blobmsg_parse(client_table_policy, __CLIENT_TABLE_MAX, tb, blob_data(msg), blob_len(msg)); @@ -349,7 +361,10 @@ int parse_to_clients(struct blob_attr *msg) { */ uint8_t bssid[ETH_ALEN]; hwaddr_aton(blobmsg_data(tb[CLIENT_TABLE_BSSID]), bssid); - kick_clients(bssid); + + if(do_kick){ + kick_clients(bssid); + } } return 0; @@ -359,7 +374,7 @@ static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_ if (!msg) return; - parse_to_clients(msg); + parse_to_clients(msg, 1); char *str = blobmsg_format_json(msg, true); send_string(str); @@ -395,18 +410,6 @@ void *update_clients_thread(void *arg) { return 0; } -/* hostapd function */ -#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" - -static void -bblobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr) { - char *s; - - s = blobmsg_alloc_string_buffer(buf, name, 20); - sprintf(s, MACSTR, MAC2STR(addr)); - blobmsg_add_string_buffer(buf); -} - void del_client(const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) { /* Problem: On which interface is the client?