diff --git a/files/dawn.config b/files/dawn.config index 4009ec7..5feddda 100644 --- a/files/dawn.config +++ b/files/dawn.config @@ -1,12 +1,12 @@ config network - option broadcast_ip '225.0.0.37' + option broadcast_ip '10.0.0.255' option broadcast_port '1025' - option multicast '1' + option multicast '0' option shared_key 'Niiiiiiiiiiiiiik' option iv 'Niiiiiiiiiiiiiik' config ordering - option sort_order 'csfb' + option sort_order 'cbfs' config hostapd option hostapd_dir '/var/run/hostapd' diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 8d1267e..58d83c4 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -109,6 +109,8 @@ typedef struct auth_entry_s assoc_entry; // ---------------- Defines ---------------- #define PROBE_ARRAY_LEN 1000 +#define SSID_MAX_LEN 32 + // ---------------- Global variables ---------------- struct probe_entry_s probe_array[PROBE_ARRAY_LEN]; pthread_mutex_t probe_array_mutex; @@ -161,6 +163,7 @@ typedef struct ap_s { uint32_t channel_utilization; time_t time; uint32_t station_count; + uint8_t ssid[SSID_MAX_LEN]; } ap; // ---------------- Defines ---------------- diff --git a/src/include/dawn_iwinfo.h b/src/include/dawn_iwinfo.h index 215d817..36cec7a 100644 --- a/src/include/dawn_iwinfo.h +++ b/src/include/dawn_iwinfo.h @@ -10,4 +10,6 @@ int get_rssi_iwinfo(__uint8_t *client_addr); int get_bandwidth_iwinfo(__uint8_t *client_addr, float *rx_rate, float *tx_rate); +int compare_essid_iwinfo(__uint8_t *bssid_addr, __uint8_t *bssid_addr_to_compare); + #endif //DAWN_RSSI_H diff --git a/src/include/utils.h b/src/include/utils.h index e07a776..efc874b 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -17,4 +17,6 @@ int convert_mac(char *in, char *out); void write_mac_to_file(char* path, uint8_t addr[]); +int string_is_greater(uint8_t* str, uint8_t* str_2); + #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 4252b4f..7fc15e5 100644 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,7 @@ #include "ubus.h" #include "dawn_uci.h" #include "crypto.h" +#include "dawn_iwinfo.h" void daemon_shutdown(); diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index dfc36b8..c7765fc 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -43,7 +43,10 @@ int is_connected(uint8_t bssid_addr[], uint8_t client_addr[]); int mac_in_maclist(uint8_t mac[]); -int compare_station_count(uint8_t *bssid_addr_own, uint8_t *bssid_addr_to_compare, int automatic_kick); +int compare_station_count(uint8_t *bssid_addr_own, uint8_t *bssid_addr_to_compare, uint8_t *client_addr, + int automatic_kick); + +int compare_ssid(uint8_t *bssid_addr_own, uint8_t *bssid_addr_to_compare); int probe_entry_last = -1; int client_entry_last = -1; @@ -70,45 +73,85 @@ struct uloop_timeout ap_timeout = { int build_hearing_map_sort_client(struct blob_buf *b) { + print_probe_array(); + pthread_mutex_lock(&probe_array_mutex); - void *client_list, *ap_list; + void *client_list, *ap_list, *ssid_list; char ap_mac_buf[20]; char client_mac_buf[20]; blob_buf_init(b, 0); - int i; - for (i = 0; i <= probe_entry_last; i++) { - int k; - sprintf(client_mac_buf, MACSTR, MAC2STR(probe_array[i].client_addr)); - client_list = blobmsg_open_table(b, client_mac_buf); - for (k = i; i <= probe_entry_last; k++){ - if(!mac_is_equal(probe_array[k].client_addr, probe_array[i].client_addr)) + int m; + for (m = 0; m <= ap_entry_last; m++) { + printf("COMPARING!\n"); + if(m > 0) + { + if(strcmp((char*)ap_array[m].ssid, (char*)ap_array[m-1].ssid) == 0) { - i = k - 1; - break; + continue; } - sprintf(ap_mac_buf, MACSTR, MAC2STR(probe_array[k].bssid_addr)); - ap_list = blobmsg_open_table(b, ap_mac_buf); - blobmsg_add_u32(b, "signal", probe_array[k].signal); - blobmsg_add_u32(b, "freq", probe_array[k].freq); - blobmsg_add_u8(b, "ht_support", probe_array[k].ht_support); - blobmsg_add_u8(b, "vht_support", probe_array[k].vht_support); + } + printf("OPEN TABLE!!!\n"); + ssid_list = blobmsg_open_table(b, (char*)ap_array[m].ssid); - ap ap_entry = ap_array_get_ap(probe_array[k].bssid_addr); + int i; + for (i = 0; i <= probe_entry_last; i++) { + /*if(!mac_is_equal(ap_array[m].bssid_addr, probe_array[i].bssid_addr)) + { + continue; + }*/ - // check if ap entry is available - if (mac_is_equal(ap_entry.bssid_addr, probe_array[k].bssid_addr)) { + ap ap_entry_i = ap_array_get_ap(probe_array[i].bssid_addr); + + if (!mac_is_equal(ap_entry_i.bssid_addr, probe_array[i].bssid_addr)) { + continue; + } + + if(strcmp((char*)ap_entry_i.ssid, (char*)ap_array[m].ssid) != 0) + { + continue; + } + + int k; + sprintf(client_mac_buf, MACSTR, MAC2STR(probe_array[i].client_addr)); + client_list = blobmsg_open_table(b, client_mac_buf); + for (k = i; i <= probe_entry_last; k++) { + ap ap_entry = ap_array_get_ap(probe_array[k].bssid_addr); + + if (!mac_is_equal(ap_entry.bssid_addr, probe_array[k].bssid_addr)) { + continue; + } + + if(strcmp((char*)ap_entry.ssid, (char*)ap_array[m].ssid) != 0) + { + continue; + } + + if (!mac_is_equal(probe_array[k].client_addr, probe_array[i].client_addr)) { + i = k - 1; + break; + } + sprintf(ap_mac_buf, MACSTR, MAC2STR(probe_array[k].bssid_addr)); + ap_list = blobmsg_open_table(b, ap_mac_buf); + blobmsg_add_u32(b, "signal", probe_array[k].signal); + blobmsg_add_u32(b, "freq", probe_array[k].freq); + blobmsg_add_u8(b, "ht_support", probe_array[k].ht_support); + blobmsg_add_u8(b, "vht_support", probe_array[k].vht_support); + + + // check if ap entry is available blobmsg_add_u32(b, "channel_utilization", ap_entry.channel_utilization); blobmsg_add_u32(b, "num_sta", ap_entry.station_count); blobmsg_add_u32(b, "ht", ap_entry.ht); blobmsg_add_u32(b, "vht", ap_entry.vht); - } - blobmsg_add_u32(b, "score", eval_probe_metric(probe_array[k])); - blobmsg_close_table(b, ap_list); + blobmsg_add_u32(b, "score", eval_probe_metric(probe_array[k])); + blobmsg_close_table(b, ap_list); + } + blobmsg_close_table(b, client_list); } - blobmsg_close_table(b, client_list); + blobmsg_close_table(b, ssid_list); } pthread_mutex_unlock(&probe_array_mutex); return 0; @@ -173,7 +216,20 @@ int eval_probe_metric(struct probe_entry_s probe_entry) { return score; } -int compare_station_count(uint8_t *bssid_addr_own, uint8_t *bssid_addr_to_compare, int automatic_kick) { +int compare_ssid(uint8_t *bssid_addr_own, uint8_t *bssid_addr_to_compare) { + ap ap_entry_own = ap_array_get_ap(bssid_addr_own); + ap ap_entry_to_compre = ap_array_get_ap(bssid_addr_to_compare); + + if (mac_is_equal(ap_entry_own.bssid_addr, bssid_addr_own) && + mac_is_equal(ap_entry_to_compre.bssid_addr, bssid_addr_to_compare)) + { + return (strcmp((char*)ap_entry_own.ssid, (char*)ap_entry_to_compre.ssid) == 0); + } + return 0; +} + +int compare_station_count(uint8_t *bssid_addr_own, uint8_t *bssid_addr_to_compare, uint8_t *client_addr, + int automatic_kick) { ap ap_entry_own = ap_array_get_ap(bssid_addr_own); ap ap_entry_to_compre = ap_array_get_ap(bssid_addr_to_compare); @@ -188,6 +244,19 @@ int compare_station_count(uint8_t *bssid_addr_own, uint8_t *bssid_addr_to_compar } else { return ap_entry_own.station_count > ap_entry_to_compre.station_count; } + + /* + int own_count = ap_entry_own.station_count; + if(automatic_kick) + { + own_count--; + } + if (is_connected(bssid_addr_to_compare, client_addr)) + { + own_count--; + } + + return own_count > ap_entry_to_compre.station_count;*/ } return 0; @@ -240,6 +309,13 @@ int better_ap_available(uint8_t bssid_addr[], uint8_t client_addr[], int automat continue; } + // check if same ssid! + if(!compare_ssid(bssid_addr, probe_array[k].bssid_addr)) + { + continue; + } + + printf("Calculating score to compare!\n"); score_to_compare = eval_probe_metric(probe_array[k]); @@ -247,10 +323,13 @@ int better_ap_available(uint8_t bssid_addr[], uint8_t client_addr[], int automat return 1; } if (dawn_metric.use_station_count && own_score == score_to_compare) { + // if ap have same value but station count is different... - if (compare_station_count(bssid_addr, probe_array[k].bssid_addr, automatic_kick)) { + if (compare_station_count(bssid_addr, probe_array[k].bssid_addr, NULL, automatic_kick)) { return 1; } + + } } return 0; @@ -591,12 +670,12 @@ ap ap_array_get_ap(uint8_t bssid_addr[]) { return ret; } - pthread_mutex_lock(&ap_array_mutex); int i; for (i = 0; i <= ap_entry_last; i++) { - if (mac_is_equal(bssid_addr, ap_array[i].bssid_addr) || mac_is_greater(ap_array[i].bssid_addr, bssid_addr)) { + if (mac_is_equal(bssid_addr, ap_array[i].bssid_addr)){ + //|| mac_is_greater(ap_array[i].bssid_addr, bssid_addr)) { break; } } @@ -615,9 +694,15 @@ void ap_array_insert(ap entry) { int i; for (i = 0; i <= ap_entry_last; i++) { - if (!mac_is_greater(entry.bssid_addr, ap_array[i].bssid_addr)) { + if (mac_is_greater(entry.bssid_addr, ap_array[i].bssid_addr) && + strcmp((char*)entry.ssid, (char*)ap_array[i].ssid) == 0) { + continue; + } + + if(!string_is_greater(entry.ssid, ap_array[i].ssid)){ break; } + } for (int j = ap_entry_last; j >= i; j--) { if (j + 1 <= ARRAY_AP_LEN) { @@ -659,7 +744,7 @@ ap ap_array_delete(ap entry) { } void remove_old_client_entries(time_t current_time, long long int threshold) { - for (int i = 0; i < probe_entry_last; i++) { + for (int i = 0; i < client_entry_last; i++) { if (client_array[i].time < current_time - threshold) { client_array_delete(client_array[i]); } @@ -676,7 +761,7 @@ void remove_old_probe_entries(time_t current_time, long long int threshold) { } void remove_old_ap_entries(time_t current_time, long long int threshold) { - for (int i = 0; i < probe_entry_last; i++) { + for (int i = 0; i < ap_entry_last; i++) { if (ap_array[i].time < current_time - threshold) { ap_array_delete(ap_array[i]); } @@ -1160,8 +1245,8 @@ void print_ap_entry(ap entry) { char mac_buf_ap[20]; sprintf(mac_buf_ap, MACSTR, MAC2STR(entry.bssid_addr)); - printf("bssid_addr: %s, freq: %d, ht: %d, vht: %d, chan_utilz: %d\n", - mac_buf_ap, entry.freq, entry.ht, entry.vht, entry.channel_utilization); + printf("ssid: %s, bssid_addr: %s, freq: %d, ht: %d, vht: %d, chan_utilz: %d\n", + entry.ssid, mac_buf_ap, entry.freq, entry.ht, entry.vht, entry.channel_utilization); } void print_ap_array() { diff --git a/src/utils/dawn_iwinfo.c b/src/utils/dawn_iwinfo.c index 7984f31..29b55cd 100644 --- a/src/utils/dawn_iwinfo.c +++ b/src/utils/dawn_iwinfo.c @@ -15,10 +15,78 @@ int parse_rssi(char *iwinfo_string); int get_rssi(const char *ifname, uint8_t *client_addr); -int get_bandwith(const char *ifname, uint8_t *client_addr, float *rx_rate, float *tx_rate); +int get_bandwidth(const char *ifname, uint8_t *client_addr, float *rx_rate, float *tx_rate); #define IWINFO_BUFSIZE 24 * 1024 +#define IWINFO_ESSID_MAX_SIZE 32 + + +int compare_essid_iwinfo(__uint8_t *bssid_addr, __uint8_t *bssid_addr_to_compare) +{ + const struct iwinfo_ops *iw; + + char mac_buf[20]; + char mac_buf_to_compare[20]; + sprintf(mac_buf, MACSTR, MAC2STR(bssid_addr)); + sprintf(mac_buf_to_compare, MACSTR, MAC2STR(bssid_addr_to_compare)); + + DIR *dirp; + struct dirent *entry; + dirp = opendir(hostapd_dir_glob); // error handling? + if (!dirp) { + fprintf(stderr, "No hostapd sockets!\n"); + return 0; + } + + char* essid = NULL; + char* essid_to_compare = NULL; + + char buf_essid[IWINFO_ESSID_MAX_SIZE+1] = { 0 }; + char buf_essid_to_compare[IWINFO_ESSID_MAX_SIZE+1] = { 0 }; + + while ((entry = readdir(dirp)) != NULL && (essid == NULL || essid_to_compare == NULL)) { + if (entry->d_type == DT_SOCK) { + + iw = iwinfo_backend(entry->d_name); + + static char buf_bssid[18] = { 0 }; + if (iw->bssid(entry->d_name, buf_bssid)) + snprintf(buf_bssid, sizeof(buf_bssid), "00:00:00:00:00:00"); + + if(strcmp(mac_buf, buf_bssid) == 0) + { + + if (iw->ssid(entry->d_name, buf_essid)) + memset(buf_essid, 0, sizeof(buf_essid)); + essid = buf_essid; + } + + if(strcmp(mac_buf_to_compare, buf_bssid) == 0) + { + if (iw->ssid(entry->d_name, buf_essid_to_compare)) + memset(buf_essid_to_compare, 0, sizeof(buf_essid_to_compare)); + essid_to_compare = buf_essid_to_compare; + } + } + } + closedir(dirp); + + printf("Comparing: %s with %s\n", essid, essid_to_compare); + + if(essid == NULL || essid_to_compare == NULL) + { + return -1; + } + + if(strcmp(essid, essid_to_compare) == 0) + { + return 0; + } + + return -1; +} + int get_bandwidth_iwinfo(__uint8_t *client_addr, float *rx_rate, float *tx_rate) { DIR *dirp; @@ -33,7 +101,7 @@ int get_bandwidth_iwinfo(__uint8_t *client_addr, float *rx_rate, float *tx_rate) while ((entry = readdir(dirp)) != NULL) { if (entry->d_type == DT_SOCK) { - if (get_bandwith(entry->d_name, client_addr, rx_rate, tx_rate)) { + if (get_bandwidth(entry->d_name, client_addr, rx_rate, tx_rate)) { sucess = 1; break; } @@ -43,7 +111,7 @@ int get_bandwidth_iwinfo(__uint8_t *client_addr, float *rx_rate, float *tx_rate) return sucess; } -int get_bandwith(const char *ifname, uint8_t *client_addr, float *rx_rate, float *tx_rate) { +int get_bandwidth(const char *ifname, uint8_t *client_addr, float *rx_rate, float *tx_rate) { int i, len; char buf[IWINFO_BUFSIZE]; diff --git a/src/utils/ubus.c b/src/utils/ubus.c index 5645f3d..d2b198e 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -105,6 +105,7 @@ static const struct blobmsg_policy prob_policy[__PROB_MAX] = { enum { CLIENT_TABLE, CLIENT_TABLE_BSSID, + CLIENT_TABLE_SSID, CLIENT_TABLE_FREQ, CLIENT_TABLE_HT, CLIENT_TABLE_VHT, @@ -116,6 +117,7 @@ enum { static const struct blobmsg_policy client_table_policy[__CLIENT_TABLE_MAX] = { [CLIENT_TABLE] = {.name = "clients", .type = BLOBMSG_TYPE_TABLE}, [CLIENT_TABLE_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING}, + [CLIENT_TABLE_SSID] = {.name = "ssid", .type = BLOBMSG_TYPE_STRING}, [CLIENT_TABLE_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32}, [CLIENT_TABLE_HT] = {.name = "ht_supported", .type = BLOBMSG_TYPE_INT8}, [CLIENT_TABLE_VHT] = {.name = "vht_supported", .type = BLOBMSG_TYPE_INT8}, @@ -246,13 +248,17 @@ void blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t * static int decide_function(probe_entry *prob_req) { - // TODO: Refactor... printf("COUNTER: %d\n", prob_req->counter); if (prob_req->counter < dawn_metric.min_probe_count) { return 0; } + if(!dawn_metric.eval_probe_req) + { + return 1; + } + if (better_ap_available(prob_req->bssid_addr, prob_req->client_addr, 0)) { return 0; } @@ -368,6 +374,9 @@ static int handle_auth_req(struct blob_attr *msg) { return UBUS_STATUS_UNKNOWN_ERROR; } + // maybe add here if a client is already connected... + // delay problems... + printf("ALLOW AUTH!\n"); return 0; } @@ -382,31 +391,13 @@ static int handle_assoc_req(struct blob_attr *msg) { static int handle_probe_req(struct blob_attr *msg) { //printf("[WC] Parse Probe Request\n"); probe_entry prob_req; + probe_entry tmp_prob_req; if(parse_to_probe_req(msg, &prob_req) == 0) { - insert_to_array(prob_req, 1); + tmp_prob_req = insert_to_array(prob_req, 1); //print_probe_array(); send_blob_attr_via_network(msg, "probe"); } - //insert_to_list(prob_req, 1); - //probe_entry tmp_probe = - probe_entry tmp_prob_req = insert_to_array(prob_req, 1); - - // send probe via network - /*char *str; - str = blobmsg_format_json(msg, true); - send_string_enc(str); - - printf("[WC] Hostapd-Probe: %s : %s\n", "probe", str);*/ - - - //print_probe_array(); - - // deny access - - if (!dawn_metric.eval_probe_req) { - return 0; - } if (!decide_function(&tmp_prob_req)) { //printf("MAC WILL BE DECLINED!!!\n"); @@ -777,6 +768,7 @@ int parse_to_clients(struct blob_attr *msg, int do_kick, uint32_t id) { ap_entry.ht = blobmsg_get_u8(tb[CLIENT_TABLE_HT]); ap_entry.vht = blobmsg_get_u8(tb[CLIENT_TABLE_VHT]); ap_entry.channel_utilization = blobmsg_get_u32(tb[CLIENT_TABLE_CHAN_UTIL]); + strcpy((char*)ap_entry.ssid, blobmsg_get_string(tb[CLIENT_TABLE_SSID])); if (tb[CLIENT_TABLE_NUM_STA]) { ap_entry.station_count = blobmsg_get_u32(tb[CLIENT_TABLE_NUM_STA]); diff --git a/src/utils/utils.c b/src/utils/utils.c index 8af0488..f49403e 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -1,6 +1,24 @@ #include "utils.h" #include "ubus.h" +int string_is_greater(uint8_t* str, uint8_t* str_2) { + + int length_1 = strlen((char*)str); + int length_2 = strlen((char*)str_2); + + int length = length_1 < length_2 ? length_1 : length_2; + + for (int i = 0; i < length; i++) { + if (str[i] > str_2[i]) { + return 1; + } + if (str[i] < str_2[i]) { + return 0; + } + } + return length_1 > length_2; +} + int hex_to_bin(char ch) { if ((ch >= '0') && (ch <= '9')) return ch - '0'; ch = tolower(ch);