multiple update
This commit is contained in:
@ -1,14 +1,17 @@
|
||||
#include "wifi_drivers.h"
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <net/if.h>
|
||||
#include <netlink/genl/genl.h>
|
||||
#include <netlink/genl/family.h>
|
||||
#include <netlink/genl/ctrl.h>
|
||||
#include <linux/nl80211.h>
|
||||
|
||||
#include "capwap_list.h"
|
||||
#include "wifi_nl80211.h"
|
||||
|
||||
/* Compatibility functions */
|
||||
#if !defined(HAVE_LIBNL20) && !defined(HAVE_LIBNL30)
|
||||
#define nl_sock nl_handle
|
||||
static uint32_t port_bitmap[32] = { 0 };
|
||||
|
||||
static struct nl_sock* nl_socket_alloc_cb(void* cb) {
|
||||
@ -42,21 +45,275 @@ static void nl_socket_free(struct nl_sock* handle) {
|
||||
#endif
|
||||
|
||||
/* */
|
||||
wifi_device_handle nl80211_device_init(wifi_global_handle handle, struct device_init_params* params) {
|
||||
return NULL;
|
||||
static struct nl_sock* nl_create_handle(struct nl_cb* cb) {
|
||||
struct nl_sock* handle;
|
||||
|
||||
handle = nl_socket_alloc_cb(cb);
|
||||
if (!handle) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (genl_connect(handle)) {
|
||||
nl_socket_free(handle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
/* */
|
||||
void nl80211_device_deinit(wifi_device_handle handle) {
|
||||
static int nl80211_no_seq_check(struct nl_msg* msg, void* arg) {
|
||||
return NL_OK;
|
||||
}
|
||||
|
||||
/* */
|
||||
static int nl80211_error_handler(struct sockaddr_nl* nla, struct nlmsgerr* err, void* arg) {
|
||||
*((int*)arg) = err->error;
|
||||
return NL_STOP;
|
||||
}
|
||||
|
||||
/* */
|
||||
static int nl80211_finish_handler(struct nl_msg* msg, void* arg) {
|
||||
*((int*)arg) = 0;
|
||||
return NL_SKIP;
|
||||
}
|
||||
|
||||
/* */
|
||||
static int nl80211_ack_handler(struct nl_msg* msg, void* arg) {
|
||||
*((int*)arg) = 0;
|
||||
return NL_STOP;
|
||||
}
|
||||
|
||||
/* */
|
||||
static int nl80211_send_and_recv(struct nl_sock* nl, struct nl_cb* nl_cb, struct nl_msg* msg, nl_valid_cb valid_cb, void* data) {
|
||||
int result;
|
||||
struct nl_cb* cb;
|
||||
|
||||
/* Clone netlink callback */
|
||||
cb = nl_cb_clone(nl_cb);
|
||||
if (!cb) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Complete send message */
|
||||
result = nl_send_auto_complete(nl, msg);
|
||||
if (result < 0) {
|
||||
nl_cb_put(cb);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Customize message callback */
|
||||
nl_cb_err(cb, NL_CB_CUSTOM, nl80211_error_handler, &result);
|
||||
nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, nl80211_finish_handler, &result);
|
||||
nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, nl80211_ack_handler, &result);
|
||||
|
||||
if (valid_cb) {
|
||||
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_cb, data);
|
||||
}
|
||||
|
||||
result = 1;
|
||||
while (result > 0) {
|
||||
nl_recvmsgs(nl, cb);
|
||||
}
|
||||
|
||||
nl_cb_put(cb);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* */
|
||||
static int nl80211_send_and_recv_msg(struct nl80211_global_handle* globalhandle, struct nl_msg* msg, nl_valid_cb valid_cb, void* data) {
|
||||
return nl80211_send_and_recv(globalhandle->nl, globalhandle->nl_cb, msg, valid_cb, data);
|
||||
}
|
||||
|
||||
/* */
|
||||
static int cb_get_phydevice_list(struct nl_msg* msg, void* data) {
|
||||
struct nlattr* tb_msg[NL80211_ATTR_MAX + 1];
|
||||
struct genlmsghdr* gnlh = nlmsg_data(nlmsg_hdr(msg));
|
||||
struct capwap_list* list = (struct capwap_list*)data;
|
||||
|
||||
nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
|
||||
|
||||
if (tb_msg[NL80211_ATTR_WIPHY_NAME] && tb_msg[NL80211_ATTR_WIPHY]) {
|
||||
struct capwap_list_item* item = capwap_itemlist_create(sizeof(struct nl80211_phydevice_item));
|
||||
struct nl80211_phydevice_item* phyitem = (struct nl80211_phydevice_item*)item->item;
|
||||
|
||||
/* Add physical device info */
|
||||
phyitem->index = nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]);
|
||||
strcpy(phyitem->name, nla_get_string(tb_msg[NL80211_ATTR_WIPHY_NAME]));
|
||||
capwap_itemlist_insert_after(list, NULL, item);
|
||||
}
|
||||
|
||||
return NL_SKIP;
|
||||
}
|
||||
|
||||
/* */
|
||||
static int nl80211_get_phydevice_list(struct nl80211_global_handle* globalhandle, struct capwap_list* list) {
|
||||
int result;
|
||||
struct nl_msg* msg;
|
||||
|
||||
ASSERT(globalhandle != NULL);
|
||||
ASSERT(list != NULL);
|
||||
|
||||
/* */
|
||||
msg = nlmsg_alloc();
|
||||
if (!msg) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Retrieve list of physical device */
|
||||
genlmsg_put(msg, 0, 0, globalhandle->nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_WIPHY, 0);
|
||||
|
||||
/* Retrieve all physical interface */
|
||||
result = nl80211_send_and_recv_msg(globalhandle, msg, cb_get_phydevice_list, list);
|
||||
|
||||
/* */
|
||||
nlmsg_free(msg);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* */
|
||||
static wifi_device_handle nl80211_device_init(wifi_global_handle handle, struct device_init_params* params) {
|
||||
int result;
|
||||
struct capwap_list* list;
|
||||
struct capwap_list_item* item;
|
||||
struct nl80211_device_handle* devicehandle = NULL;
|
||||
struct nl80211_global_handle* globalhandle = (struct nl80211_global_handle*)handle;
|
||||
|
||||
ASSERT(params != NULL);
|
||||
|
||||
if (!handle) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Retrieve physical device info */
|
||||
list = capwap_list_create();
|
||||
result = nl80211_get_phydevice_list(globalhandle, list);
|
||||
if (!result) {
|
||||
item = list->first;
|
||||
while (item) {
|
||||
struct nl80211_phydevice_item* phyitem = (struct nl80211_phydevice_item*)item->item;
|
||||
|
||||
if (!strcmp(phyitem->name, params->ifname)) {
|
||||
/* Create device */
|
||||
devicehandle = (struct nl80211_device_handle*)capwap_alloc(sizeof(struct nl80211_device_handle));
|
||||
memset(devicehandle, 0, sizeof(struct nl80211_device_handle));
|
||||
|
||||
/* */
|
||||
strcpy(devicehandle->phyname, phyitem->name);
|
||||
devicehandle->phyindex = phyitem->index;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
/* Next */
|
||||
item = item->next;
|
||||
}
|
||||
} else {
|
||||
/* Error get physical devices */
|
||||
capwap_logging_error("Unable retrieve physical device info, error code: %d", result);
|
||||
}
|
||||
|
||||
/* */
|
||||
capwap_list_free(list);
|
||||
if (!devicehandle) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* */
|
||||
devicehandle->globalhandle = globalhandle;
|
||||
|
||||
/* Save device handle into global handle */
|
||||
item = capwap_itemlist_create_with_item(devicehandle, sizeof(struct nl80211_device_handle));
|
||||
capwap_itemlist_insert_after(globalhandle->devicelist, NULL, item);
|
||||
|
||||
return devicehandle;
|
||||
}
|
||||
|
||||
/* */
|
||||
static void nl80211_device_deinit(wifi_device_handle handle) {
|
||||
struct nl80211_device_handle* devicehandle = (struct nl80211_device_handle*)handle;
|
||||
|
||||
if (devicehandle) {
|
||||
struct capwap_list_item* search;
|
||||
|
||||
/* Remove device handle from global handle*/
|
||||
search = devicehandle->globalhandle->devicelist->first;
|
||||
while (search) {
|
||||
/* Remove item from list without destroy */
|
||||
if ((struct nl80211_device_handle*)search->item == devicehandle) {
|
||||
capwap_itemlist_remove(devicehandle->globalhandle->devicelist, search);
|
||||
break;
|
||||
}
|
||||
|
||||
search = search->next;
|
||||
}
|
||||
|
||||
/* */
|
||||
capwap_free(devicehandle);
|
||||
}
|
||||
}
|
||||
|
||||
/* */
|
||||
static wifi_global_handle nl80211_global_init(void) {
|
||||
return NULL;
|
||||
struct nl80211_global_handle* globalhandle;
|
||||
|
||||
/* */
|
||||
globalhandle = (struct nl80211_global_handle*)capwap_alloc(sizeof(struct nl80211_global_handle));
|
||||
memset(globalhandle, 0, sizeof(struct nl80211_global_handle));
|
||||
|
||||
/* Configure global netlink callback */
|
||||
globalhandle->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
|
||||
if (!globalhandle->nl_cb) {
|
||||
capwap_free(globalhandle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Create netlink socket */
|
||||
globalhandle->nl = nl_create_handle(globalhandle->nl_cb);
|
||||
if (!globalhandle->nl) {
|
||||
nl_cb_put(globalhandle->nl_cb);
|
||||
capwap_free(globalhandle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Get nl80211 netlink family */
|
||||
globalhandle->nl80211_id = genl_ctrl_resolve(globalhandle->nl, "nl80211");
|
||||
if (globalhandle->nl80211_id < 0) {
|
||||
capwap_logging_warning("Unable to found mac80211 kernel module");
|
||||
nl_socket_free(globalhandle->nl);
|
||||
nl_cb_put(globalhandle->nl_cb);
|
||||
capwap_free(globalhandle);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Configure global callback function */
|
||||
nl_cb_set(globalhandle->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, nl80211_no_seq_check, NULL);
|
||||
/* TODO */
|
||||
|
||||
/* Device list */
|
||||
globalhandle->devicelist = capwap_list_create();
|
||||
|
||||
return (wifi_global_handle)globalhandle;
|
||||
}
|
||||
|
||||
/* */
|
||||
void nl80211_global_deinit(wifi_global_handle handle) {
|
||||
static void nl80211_global_deinit(wifi_global_handle handle) {
|
||||
struct nl80211_global_handle* globalhandle = (struct nl80211_global_handle*)handle;
|
||||
|
||||
if (globalhandle) {
|
||||
ASSERT(globalhandle->devicelist->count == 0);
|
||||
|
||||
if (globalhandle->nl) {
|
||||
nl_socket_free(globalhandle->nl);
|
||||
}
|
||||
|
||||
if (globalhandle->nl_cb) {
|
||||
nl_cb_put(globalhandle->nl_cb);
|
||||
}
|
||||
|
||||
capwap_list_free(globalhandle->devicelist);
|
||||
capwap_free(globalhandle);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver function */
|
||||
|
Reference in New Issue
Block a user