Fix hostapd ht and vht

This commit is contained in:
PolynomialDivision 2017-07-27 16:46:17 +02:00
parent bfa99147cd
commit 3fe77d4ad0
6 changed files with 77 additions and 53 deletions

View file

@ -23,8 +23,8 @@ typedef struct probe_entry_s {
uint8_t target_addr[ETH_ALEN]; uint8_t target_addr[ETH_ALEN];
uint32_t signal; uint32_t signal;
uint32_t freq; uint32_t freq;
uint8_t ht_support; //uint8_t ht_support;
uint8_t vht_support; //uint8_t vht_support;
time_t time; time_t time;
int counter; int counter;
} probe_entry; } probe_entry;
@ -83,11 +83,11 @@ void *remove_client_array_thread(void *arg);
struct probe_entry_s probe_array[ARRAY_LEN]; struct probe_entry_s probe_array[ARRAY_LEN];
pthread_mutex_t probe_array_mutex; pthread_mutex_t probe_array_mutex;
void insert_to_array(probe_entry entry, int inc_counter); probe_entry insert_to_array(probe_entry entry, int inc_counter);
void probe_array_insert(probe_entry entry); 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 print_array();

View file

@ -4,7 +4,7 @@
#include <libubox/blobmsg_json.h> #include <libubox/blobmsg_json.h>
#include "datastorage.h" #include "datastorage.h"
#define MIN_PROBE_REQ 2 // TODO: Parse from config file... #define MIN_PROBE_REQ 5 // TODO: Parse from config file...
int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir); 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_probe_req(struct blob_attr *msg, probe_entry *prob_req);

View file

@ -59,13 +59,13 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
//init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1); init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1);
//pthread_t tid_probe; pthread_t tid_probe;
//pthread_create(&tid_probe, NULL, &remove_array_thread, NULL); pthread_create(&tid_probe, NULL, &remove_array_thread, NULL);
//pthread_t tid_client; //pthread_t tid_client;
// pthread_create(&tid_client, NULL, &remove_client_array_thread, NULL); //pthread_create(&tid_client, NULL, &remove_client_array_thread, NULL);
pthread_t tid_get_client; pthread_t tid_get_client;
pthread_create(&tid_get_client, NULL, &update_clients_thread, NULL); pthread_create(&tid_get_client, NULL, &update_clients_thread, NULL);

View file

@ -121,7 +121,7 @@ void *receive_msg(void *args) {
int send_string(char *msg) { int send_string(char *msg) {
pthread_mutex_lock(&send_mutex); pthread_mutex_lock(&send_mutex);
/*int msglen = strlen(msg); size_t msglen = strlen(msg);
//printf("Sending string! %s\n", msg); //printf("Sending string! %s\n", msg);
if (sendto(sock, if (sendto(sock,
msg, msg,
@ -130,8 +130,9 @@ int send_string(char *msg) {
(struct sockaddr *) &addr, (struct sockaddr *) &addr,
sizeof(addr)) < 0) { sizeof(addr)) < 0) {
perror("sendto()"); perror("sendto()");
pthread_mutex_unlock(&send_mutex);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}*/ }
pthread_mutex_unlock(&send_mutex); pthread_mutex_unlock(&send_mutex);

View file

@ -67,8 +67,8 @@ int eval_probe_metric(struct client_s client_entry, struct probe_entry_s probe_e
printf("AP Supports HT: %d\n", ap_supports_ht); printf("AP Supports HT: %d\n", ap_supports_ht);
printf("AP Supports VHT: %d\n", ap_supports_vht); printf("AP Supports VHT: %d\n", ap_supports_vht);
client_supports_ht = probe_entry.ht_support; client_supports_ht = client_entry.ht;
client_supports_vht = probe_entry.vht_support; client_supports_vht = client_entry.vht;
printf("Clients Supports HT: %d\n", client_supports_ht); printf("Clients Supports HT: %d\n", client_supports_ht);
printf("Clients Supports VHT: %d\n", client_supports_vht); printf("Clients Supports VHT: %d\n", client_supports_vht);
@ -299,20 +299,20 @@ void probe_array_insert(probe_entry entry) {
} }
} }
probe_entry *probe_array_delete(probe_entry entry) { probe_entry probe_array_delete(probe_entry entry) {
int i; int i;
int found_in_array = 0; int found_in_array = 0;
probe_entry *tmp = NULL; probe_entry tmp;
if (probe_entry_last == -1) { if (probe_entry_last == -1) {
return NULL; return tmp;
} }
for (i = 0; i <= probe_entry_last; i++) { for (i = 0; i <= probe_entry_last; i++) {
if (mac_is_equal(entry.bssid_addr, probe_array[i].bssid_addr) && if (mac_is_equal(entry.bssid_addr, probe_array[i].bssid_addr) &&
mac_is_equal(entry.client_addr, probe_array[i].client_addr)) { mac_is_equal(entry.client_addr, probe_array[i].client_addr)) {
found_in_array = 1; found_in_array = 1;
tmp = &probe_array[i]; tmp = probe_array[i];
break; break;
} }
} }
@ -336,24 +336,35 @@ void print_array() {
printf("------------------\n"); printf("------------------\n");
} }
void insert_to_array(probe_entry entry, int inc_counter) { probe_entry insert_to_array(probe_entry entry, int inc_counter) {
pthread_mutex_lock(&probe_array_mutex); pthread_mutex_lock(&probe_array_mutex);
entry.time = time(0); entry.time = time(0);
entry.counter = 0; entry.counter = 0;
probe_entry *tmp = probe_array_delete(entry); probe_entry tmp = probe_array_delete(entry);
if (tmp != NULL) { //if (tmp != NULL) {
entry.counter = tmp->counter; if(mac_is_equal(entry.bssid_addr,tmp.bssid_addr)
} && mac_is_equal(entry.client_addr, tmp.client_addr)){
entry.counter = tmp.counter;
printf("MAC IS EQUAL EUQAL AND CORRECT!\n");
printf("TMP ENTRY IS NOT NULL!!!!!!!!!!!!!!!!!!!!!!\nCounter: %d\n", tmp.counter);
}
//else
// printf("MAC IS NOT EQUAL AND THIS IS INCORRECT!\n");
// printf("TMP ENTRY IS NOT NULL!!!!!!!!!!!!!!!!!!!!!!\nCounter: %d\n", tmp->counter);
//}
if (inc_counter) { if (inc_counter) {
entry.counter++; entry.counter++;
//entry.counter = 5;
} }
probe_array_insert(entry); probe_array_insert(entry);
pthread_mutex_unlock(&probe_array_mutex); pthread_mutex_unlock(&probe_array_mutex);
return entry;
} }
void remove_old_client_entries(time_t current_time, long long int threshold) { void remove_old_client_entries(time_t current_time, long long int threshold) {
@ -724,9 +735,8 @@ void print_probe_entry(probe_entry entry) {
printf( printf(
"bssid_addr: %s, client_addr: %s, signal: %d, freq: " "bssid_addr: %s, client_addr: %s, signal: %d, freq: "
"%d, ht: %d, vht: %d, counter: %d\n", "%d, counter: %d\n",
mac_buf_ap, mac_buf_client, entry.signal, entry.freq, entry.ht_support, entry.vht_support, mac_buf_ap, mac_buf_client, entry.signal, entry.freq, entry.counter);
entry.counter);
} }
void print_client_entry(client entry) { void print_client_entry(client entry) {

View file

@ -14,6 +14,7 @@
#include "utils.h" #include "utils.h"
static struct ubus_context *ctx; static struct ubus_context *ctx;
static struct ubus_context *ctx_clients;
static struct ubus_subscriber hostapd_event; static struct ubus_subscriber hostapd_event;
static struct blob_buf b; static struct blob_buf b;
@ -34,8 +35,8 @@ static const struct blobmsg_policy prob_policy[__PROB_MAX] = {
[PROB_TARGET_ADDR] = {.name = "target", .type = BLOBMSG_TYPE_STRING}, [PROB_TARGET_ADDR] = {.name = "target", .type = BLOBMSG_TYPE_STRING},
[PROB_SIGNAL] = {.name = "signal", .type = BLOBMSG_TYPE_INT32}, [PROB_SIGNAL] = {.name = "signal", .type = BLOBMSG_TYPE_INT32},
[PROB_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32}, [PROB_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
[PROB_HT_SUPPORT] = {.name = "ht_support", .type = BLOBMSG_TYPE_INT8}, //[PROB_HT_SUPPORT] = {.name = "ht_support", .type = BLOBMSG_TYPE_INT8},
[PROB_VHT_SUPPORT] = {.name = "vht_support", .type = BLOBMSG_TYPE_INT8}, //[PROB_VHT_SUPPORT] = {.name = "vht_support", .type = BLOBMSG_TYPE_INT8},
}; };
enum { enum {
@ -110,23 +111,28 @@ blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr)
blobmsg_add_string_buffer(buf); blobmsg_add_string_buffer(buf);
} }
/*
static int decide_function(probe_entry *prob_req) {
// TODO: Refactor...
if (prob_req->counter < MIN_PROBE_REQ) {
return 0;
}
int ret = static int decide_function(probe_entry *prob_req) {
// TODO: Refactor...
printf("COUNTER: %d\n", prob_req->counter);
if (prob_req->counter < MIN_PROBE_REQ) {
return 0;
}
/*int ret =
mac_first_in_probe_list(prob_req->bssid_addr, prob_req->client_addr); mac_first_in_probe_list(prob_req->bssid_addr, prob_req->client_addr);
if (ret) { if (ret) {
printf("Mac will be accepted!\n"); printf("Mac will be accepted!\n");
} else { } else {
printf("Mac will be declined!\n"); printf("Mac will be declined!\n");
} }
return ret; return ret;
*/
// allow access
return 1;
} }
*/
static void hostapd_handle_remove(struct ubus_context *ctx, static void hostapd_handle_remove(struct ubus_context *ctx,
struct ubus_subscriber *s, uint32_t id) { struct ubus_subscriber *s, uint32_t id) {
fprintf(stderr, "Object %08x went away\n", id); fprintf(stderr, "Object %08x went away\n", id);
@ -153,7 +159,7 @@ int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req) {
if (tb[PROB_FREQ]) { if (tb[PROB_FREQ]) {
prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]); prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]);
} }
/*
if (tb[PROB_HT_SUPPORT]) { if (tb[PROB_HT_SUPPORT]) {
prob_req->ht_support = blobmsg_get_u8(tb[PROB_HT_SUPPORT]); prob_req->ht_support = blobmsg_get_u8(tb[PROB_HT_SUPPORT]);
} }
@ -161,24 +167,26 @@ int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req) {
if (tb[PROB_VHT_SUPPORT]) { if (tb[PROB_VHT_SUPPORT]) {
prob_req->vht_support = blobmsg_get_u8(tb[PROB_VHT_SUPPORT]); prob_req->vht_support = blobmsg_get_u8(tb[PROB_VHT_SUPPORT]);
} }
*/
return 0; return 0;
} }
static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct ubus_request_data *req, const char *method,
struct blob_attr *msg) { struct blob_attr *msg) {
//return UBUS_STATUS_UNKNOWN_ERROR;
// TODO: Only handle probe request and NOT assoc, ... // TODO: Only handle probe request and NOT assoc, ...
if (strncmp(method, "probe", 5) != 0) //if (strncmp(method, "probe", 5) != 0)
return 0; // return 0;
printf("[WC] Parse Probe Request\n"); printf("[WC] Parse Probe Request\n");
probe_entry prob_req; probe_entry prob_req;
parse_to_probe_req(msg, &prob_req); parse_to_probe_req(msg, &prob_req);
//insert_to_list(prob_req, 1); //insert_to_list(prob_req, 1);
insert_to_array(prob_req, 1); probe_entry tmp_probe = insert_to_array(prob_req, 1);
// send probe via network // send probe via network
char *str; char *str;
@ -188,15 +196,19 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
printf("[WC] Hostapd-Probe: %s : %s\n", method, str); printf("[WC] Hostapd-Probe: %s : %s\n", method, str);
printf("[WC] ParsED Probe Request\n"); printf("[WC] ParsED Probe Request\n");
//print_array();
//print_array(); //print_array();
// sleep(2); // sleep for 2s // sleep(2); // sleep for 2s
// deny access // deny access
//if (!decide_function(&prob_req)) { if (!decide_function(&tmp_probe)) {
// return UBUS_STATUS_UNKNOWN_ERROR; printf("MAC WILL BE DECLINED!!!");
//} return UBUS_STATUS_UNKNOWN_ERROR;
}
printf("MAC WILL BE ACCEPDTED!!!");
// allow access // allow access
return 0; return 0;
@ -260,8 +272,6 @@ int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir) {
subscribe_to_hostapd_interfaces(hostapd_dir); subscribe_to_hostapd_interfaces(hostapd_dir);
ubus_get_clients();
uloop_run(); uloop_run();
close_socket(); close_socket();
@ -404,10 +414,10 @@ static int ubus_get_clients() {
char hostapd_iface[256]; char hostapd_iface[256];
uint32_t id; uint32_t id;
sprintf(hostapd_iface, "hostapd.%s", entry->d_name); sprintf(hostapd_iface, "hostapd.%s", entry->d_name);
int ret = ubus_lookup_id(ctx, hostapd_iface, &id); int ret = ubus_lookup_id(ctx_clients, hostapd_iface, &id);
if (!ret) { if (!ret) {
int timeout = 1; int timeout = 1;
ubus_invoke(ctx, id, "get_clients", NULL, ubus_get_clients_cb, NULL, timeout * 1000); ubus_invoke(ctx_clients, id, "get_clients", NULL, ubus_get_clients_cb, NULL, timeout * 1000);
} }
} }
} }
@ -415,6 +425,9 @@ static int ubus_get_clients() {
} }
void *update_clients_thread(void *arg) { void *update_clients_thread(void *arg) {
const char *ubus_socket = NULL;
ctx_clients = ubus_connect(ubus_socket);
while (1) { while (1) {
sleep(TIME_THRESHOLD_CLIENT_UPDATE); sleep(TIME_THRESHOLD_CLIENT_UPDATE);
printf("[Thread] : Kicking clients!\n"); printf("[Thread] : Kicking clients!\n");
@ -470,10 +483,10 @@ void del_client(const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uin
char hostapd_iface[256]; char hostapd_iface[256];
uint32_t id; uint32_t id;
sprintf(hostapd_iface, "hostapd.%s", entry->d_name); sprintf(hostapd_iface, "hostapd.%s", entry->d_name);
int ret = ubus_lookup_id(ctx, hostapd_iface, &id); int ret = ubus_lookup_id(ctx_clients, hostapd_iface, &id);
if (!ret) { if (!ret) {
int timeout = 1; int timeout = 1;
ubus_invoke(ctx, id, "del_client", b.head, NULL, NULL, timeout * 1000); ubus_invoke(ctx_clients, id, "del_client", b.head, NULL, NULL, timeout * 1000);
} }
} }
} }