Fix wifi messages

This commit is contained in:
vemax78
2013-07-27 22:46:17 +02:00
parent 47b86f2ea9
commit 73c3354e50
9 changed files with 4104 additions and 112 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,7 @@
#include "wifi_drivers.h"
#include <unistd.h>
#include <sys/ioctl.h>
#include "capwap.h"
#include "capwap_array.h"
#include "wifi_drivers.h"
/* Declare enable wifi driver */
#ifdef ENABLE_WIFI_DRIVERS_NL80211
@ -106,6 +105,25 @@ int wifi_create_device(int radioid, char* ifname, char* driver) {
return result;
}
/* */
struct wifi_capability* wifi_get_capability_device(int radioid) {
struct wifi_device* device;
ASSERT(radioid > 0);
if (wifi_device->count <= radioid) {
return NULL;
}
/* Retrieve capability */
device = (struct wifi_device*)capwap_array_get_item_pointer(wifi_device, radioid);
if (device->handle && device->instance->ops->device_deinit) {
return device->instance->ops->get_capability(device->handle);
}
return NULL;
}
/* */
void wifi_iface_updown(int sock, const char* ifname, int up) {
int localsock = -1;
@ -142,3 +160,16 @@ void wifi_iface_updown(int sock, const char* ifname, int up) {
close(localsock);
}
}
/* */
unsigned long wifi_frequency_to_channel(unsigned long freq) {
if ((freq >= 2412) && (freq <= 2472)) {
return (freq - 2407) / 5;
} else if (freq == 2484) {
return 14;
} else if ((freq >= 5035) && (freq <= 5825)) {
return freq / 5 - 1000;
}
return 0;
}

View File

@ -1,21 +1,13 @@
#ifndef __WIFI_DRIVERS_HEADER__
#define __WIFI_DRIVERS_HEADER__
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include "capwap_debug.h"
#include "capwap_logging.h"
/* */
#define WIFI_DRIVER_NAME_SIZE 16
/* */
#define IS_IEEE80211_FREQ_BG(x) ((x >= 2412) && (x <= 2484) ? 1 : 0)
#define IS_IEEE80211_FREQ_A(x) ((x >= 5035) && (x <= 5825) ? 1 : 0)
/* */
#define WIFI_CAPABILITY_AP_SUPPORTED 0x00000001
#define WIFI_CAPABILITY_AP_VLAN_SUPPORTED 0x00000002
@ -23,6 +15,28 @@
#define WIFI_CAPABILITY_MONITOR_SUPPORTED 0x00000008
#define WIFI_CAPABILITY_WDS_SUPPORTED 0x00000010
#define FREQ_CAPABILITY_DISABLED 0x00000001
#define FREQ_CAPABILITY_PASSIVE_SCAN 0x00000002
#define FREQ_CAPABILITY_NO_IBBS 0x00000004
#define FREQ_CAPABILITY_RADAR 0x00000008
#define FREQ_CAPABILITY_DFS_STATE 0x00000010
#define FREQ_CAPABILITY_DFS_TIME 0x00000020
#define RATE_CAPABILITY_SHORTPREAMBLE 0x00000001
#define CIPHER_CAPABILITY_UNKNOWN 0
#define CIPHER_CAPABILITY_WEP40 1
#define CIPHER_CAPABILITY_WEP104 2
#define CIPHER_CAPABILITY_TKIP 3
#define CIPHER_CAPABILITY_CCMP 4
#define CIPHER_CAPABILITY_CMAC 5
#define CIPHER_CAPABILITY_GCMP 6
#define CIPHER_CAPABILITY_WPI_SMS4 7
#define IEEE80211_DFS_USABLE 0
#define IEEE80211_DFS_UNAVAILABLE 1
#define IEEE80211_DFS_AVAILABLE 2
/* */
typedef void* wifi_global_handle;
typedef void* wifi_device_handle;
@ -32,6 +46,44 @@ struct device_init_params {
char* ifname;
};
/* Interface capability */
struct wifi_freq_capability {
unsigned long flags;
unsigned long frequency; /* MHz */
unsigned long channel;
unsigned long maxtxpower; /* mBm = 100 * dBm */
unsigned long dfsstate;
unsigned long dfstime; /* ms */
};
struct wifi_rate_capability {
unsigned long flags;
unsigned long bitrate; /* Kbps */
};
struct wifi_band_capability {
unsigned long htcapability;
struct capwap_array* freq;
struct capwap_array* rate;
};
struct wifi_cipher_capability {
unsigned long cipher;
};
struct wifi_capability {
unsigned long radiosupported;
unsigned long radiotype;
struct capwap_array* bands;
struct capwap_array* ciphers;
};
/* */
struct wifi_driver_ops {
const char* name; /* Name of wifi driver */
@ -44,6 +96,9 @@ struct wifi_driver_ops {
/* Initialize device */
wifi_device_handle (*device_init)(wifi_global_handle handle, struct device_init_params* params);
void (*device_deinit)(wifi_device_handle handle);
/* Capability */
struct wifi_capability* (*get_capability)(wifi_device_handle handle);
};
/* */
@ -64,8 +119,10 @@ void wifi_free_driver(void);
/* */
int wifi_create_device(int radioid, char* ifname, char* driver);
struct wifi_capability* wifi_get_capability_device(int radioid);
/* Util functions */
void wifi_iface_updown(int sock, const char* ifname, int up);
unsigned long wifi_frequency_to_channel(unsigned long freq);
#endif /* __WIFI_DRIVERS_HEADER__ */

View File

@ -1,13 +1,15 @@
#include "wifi_drivers.h"
#include <sys/types.h>
#include <unistd.h>
#include <net/if.h>
#include "capwap.h"
#include "capwap_array.h"
#include "capwap_list.h"
#include "capwap_element.h"
#include <netlink/genl/genl.h>
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
#include <linux/nl80211.h>
#include "capwap_list.h"
/* Local version of nl80211 with all feature to remove the problem of frag version of nl80211 */
#include "nl80211_v3_10.h"
#include "wifi_drivers.h"
#include "wifi_nl80211.h"
/* Compatibility functions */
@ -125,6 +127,41 @@ static int nl80211_send_and_recv_msg(struct nl80211_global_handle* globalhandle,
return nl80211_send_and_recv(globalhandle->nl, globalhandle->nl_cb, msg, valid_cb, data);
}
/* */
static unsigned long nl80211_get_cipher(uint32_t chiper) {
switch (chiper) {
case 0x000fac01: {
return CIPHER_CAPABILITY_WEP40;
}
case 0x000fac05: {
return CIPHER_CAPABILITY_WEP104;
}
case 0x000fac02: {
return CIPHER_CAPABILITY_TKIP;
}
case 0x000fac04: {
return CIPHER_CAPABILITY_CCMP;
}
case 0x000fac06: {
return CIPHER_CAPABILITY_CMAC;
}
case 0x000fac08: {
return CIPHER_CAPABILITY_GCMP;
}
case 0x00147201: {
return CIPHER_CAPABILITY_WPI_SMS4;
}
}
return CIPHER_CAPABILITY_UNKNOWN;
}
/* */
static int cb_get_virtdevice_list(struct nl_msg* msg, void* data) {
struct nlattr* tb_msg[NL80211_ATTR_MAX + 1];
@ -218,48 +255,179 @@ static int nl80211_get_phydevice_list(struct nl80211_global_handle* globalhandle
/* */
static int cb_get_phydevice_capability(struct nl_msg* msg, void* data) {
int i, j;
struct nlattr* tb_msg[NL80211_ATTR_MAX + 1];
struct genlmsghdr* gnlh = nlmsg_data(nlmsg_hdr(msg));
struct nl80211_device_handle* devicehandle = (struct nl80211_device_handle*)data;
int radio80211bg = 0;
int radio80211a = 0;
nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
if (tb_msg[NL80211_ATTR_WIPHY] && (nla_get_u32(tb_msg[NL80211_ATTR_WIPHY]) == devicehandle->phyindex)) {
/* Interface supported */
if (tb_msg[NL80211_ATTR_SUPPORTED_IFTYPES]) {
int i;
struct nlattr* nl_mode;
devicehandle->physupported = 0;
nla_for_each_nested(nl_mode, tb_msg[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
switch (nla_type(nl_mode)) {
case NL80211_IFTYPE_AP: {
devicehandle->physupported |= WIFI_CAPABILITY_AP_SUPPORTED;
devicehandle->capability.radiosupported |= WIFI_CAPABILITY_AP_SUPPORTED;
break;
}
case NL80211_IFTYPE_AP_VLAN: {
devicehandle->physupported |= WIFI_CAPABILITY_AP_VLAN_SUPPORTED;
devicehandle->capability.radiosupported |= WIFI_CAPABILITY_AP_VLAN_SUPPORTED;
break;
}
case NL80211_IFTYPE_ADHOC: {
devicehandle->physupported |= WIFI_CAPABILITY_ADHOC_SUPPORTED;
devicehandle->capability.radiosupported |= WIFI_CAPABILITY_ADHOC_SUPPORTED;
break;
}
case NL80211_IFTYPE_WDS: {
devicehandle->physupported |= WIFI_CAPABILITY_WDS_SUPPORTED;
devicehandle->capability.radiosupported |= WIFI_CAPABILITY_WDS_SUPPORTED;
break;
}
case NL80211_IFTYPE_MONITOR: {
devicehandle->physupported |= WIFI_CAPABILITY_MONITOR_SUPPORTED;
devicehandle->capability.radiosupported |= WIFI_CAPABILITY_MONITOR_SUPPORTED;
break;
}
}
}
}
/* Cipher supported */
if (tb_msg[NL80211_ATTR_CIPHER_SUITES]) {
int count;
uint32_t* ciphers;
struct wifi_cipher_capability* ciphercap;
/* */
count = nla_len(tb_msg[NL80211_ATTR_CIPHER_SUITES]) / sizeof(uint32_t);
if (count > 0) {
ciphers = (uint32_t*)nla_data(tb_msg[NL80211_ATTR_CIPHER_SUITES]);
for (j = 0; j < count; j++) {
ciphercap = (struct wifi_cipher_capability*)capwap_array_get_item_pointer(devicehandle->capability.ciphers, devicehandle->capability.ciphers->count);
ciphercap->cipher = nl80211_get_cipher(ciphers[j]);
}
}
}
/* Band and datarate supported */
if (tb_msg[NL80211_ATTR_WIPHY_BANDS]) {
struct nlattr* nl_band;
struct nlattr* tb_band[NL80211_BAND_ATTR_MAX + 1];
nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], i) {
struct wifi_band_capability* bandcap;
nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band), nla_len(nl_band), NULL);
/* Init band */
bandcap = (struct wifi_band_capability*)capwap_array_get_item_pointer(devicehandle->capability.bands, devicehandle->capability.bands->count);
bandcap->freq = capwap_array_create(sizeof(struct wifi_freq_capability), 0, 1);
bandcap->rate = capwap_array_create(sizeof(struct wifi_rate_capability), 0, 1);
/* Check High Throughput capability */
if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
bandcap->htcapability = (unsigned long)nla_get_u16(tb_band[NL80211_BAND_ATTR_HT_CAPA]);
devicehandle->capability.radiotype |= CAPWAP_RADIO_TYPE_80211N;
}
/* Frequency */
if (tb_band[NL80211_BAND_ATTR_FREQS]) {
struct nlattr* nl_freq;
struct nlattr* tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
[NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
[NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
[NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
[NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
};
nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], j) {
nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq), nla_len(nl_freq), freq_policy);
if (tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) {
struct wifi_freq_capability* freq = (struct wifi_freq_capability*)capwap_array_get_item_pointer(bandcap->freq, bandcap->freq->count);
/* Retrieve frequency and channel */
freq->frequency = (unsigned long)nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
freq->channel = wifi_frequency_to_channel(freq->frequency);
if (!radio80211bg && IS_IEEE80211_FREQ_BG(freq->frequency)) {
radio80211bg = 1;
devicehandle->capability.radiotype |= (CAPWAP_RADIO_TYPE_80211B | CAPWAP_RADIO_TYPE_80211G);
} else if (!radio80211a && IS_IEEE80211_FREQ_A(freq->frequency)) {
radio80211a = 1;
devicehandle->capability.radiotype |= CAPWAP_RADIO_TYPE_80211A;
}
/* Get max tx power */
if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) {
freq->maxtxpower = (unsigned long)nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]);
}
/* Get flags */
if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) {
freq->flags |= FREQ_CAPABILITY_DISABLED;
} else {
if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]) {
freq->flags |= FREQ_CAPABILITY_PASSIVE_SCAN;
}
if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS]) {
freq->flags |= FREQ_CAPABILITY_NO_IBBS;
}
if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR]) {
freq->flags |= FREQ_CAPABILITY_RADAR;
}
if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]) {
freq->flags |= FREQ_CAPABILITY_DFS_STATE;
freq->dfsstate = (unsigned long)nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_STATE]);
if (tb_freq[NL80211_FREQUENCY_ATTR_DFS_TIME]) {
freq->flags |= FREQ_CAPABILITY_DFS_TIME;
freq->dfstime = (unsigned long)nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_DFS_TIME]);
}
}
}
}
}
}
/* Rate */
if (tb_band[NL80211_BAND_ATTR_RATES]) {
struct nlattr* nl_rate;
struct nlattr* tb_rate[NL80211_FREQUENCY_ATTR_MAX + 1];
struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
[NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
};
nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], j) {
nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate), nla_len(nl_rate), rate_policy);
if (tb_rate[NL80211_BITRATE_ATTR_RATE]) {
struct wifi_rate_capability* rate = (struct wifi_rate_capability*)capwap_array_get_item_pointer(bandcap->rate, bandcap->rate->count);
rate->bitrate = (unsigned long)nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]) * 100;
if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE]) {
rate->flags |= RATE_CAPABILITY_SHORTPREAMBLE;
}
}
}
}
}
}
}
return NL_SKIP;
@ -370,10 +538,14 @@ static wifi_device_handle nl80211_device_init(wifi_global_handle handle, struct
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));
if (!devicehandle) {
capwap_outofmemory();
}
memset(devicehandle, 0, sizeof(struct nl80211_device_handle));
/* */
@ -404,6 +576,9 @@ static wifi_device_handle nl80211_device_init(wifi_global_handle handle, struct
nl80211_destroy_all_virtdevice(globalhandle, devicehandle->phyindex);
/* Retrieve wifi device capability */
devicehandle->capability.bands = capwap_array_create(sizeof(struct wifi_band_capability), 0, 1);
devicehandle->capability.ciphers = capwap_array_create(sizeof(struct wifi_cipher_capability), 0, 1);
result = nl80211_get_phydevice_capability(devicehandle);
if (result) {
capwap_logging_error("Unable retrieve physical device capability, error code: %d", result);
@ -417,8 +592,16 @@ static wifi_device_handle nl80211_device_init(wifi_global_handle handle, struct
return devicehandle;
}
/* */
static struct wifi_capability* nl80211_get_capability(wifi_device_handle handle) {
struct nl80211_device_handle* devicehandle = (struct nl80211_device_handle*)handle;
return &devicehandle->capability;
}
/* */
static void nl80211_device_deinit(wifi_device_handle handle) {
int i;
struct nl80211_device_handle* devicehandle = (struct nl80211_device_handle*)handle;
if (devicehandle) {
@ -439,6 +622,27 @@ static void nl80211_device_deinit(wifi_device_handle handle) {
search = search->next;
}
/* Free memory */
if (devicehandle->capability.bands) {
for (i = 0; i < devicehandle->capability.bands->count; i++) {
struct wifi_band_capability* bandcap = (struct wifi_band_capability*)capwap_array_get_item_pointer(devicehandle->capability.bands, i);
if (bandcap->freq) {
capwap_array_free(bandcap->freq);
}
if (bandcap->rate) {
capwap_array_free(bandcap->rate);
}
}
capwap_array_free(devicehandle->capability.bands);
}
if (devicehandle->capability.ciphers) {
capwap_array_free(devicehandle->capability.ciphers);
}
/* */
capwap_free(devicehandle);
}
@ -476,6 +680,10 @@ static wifi_global_handle nl80211_global_init(void) {
/* */
globalhandle = (struct nl80211_global_handle*)capwap_alloc(sizeof(struct nl80211_global_handle));
if (!globalhandle) {
capwap_outofmemory();
}
memset(globalhandle, 0, sizeof(struct nl80211_global_handle));
globalhandle->sock_util = -1;
@ -525,5 +733,6 @@ const struct wifi_driver_ops wifi_driver_nl80211_ops = {
.global_init = nl80211_global_init,
.global_deinit = nl80211_global_deinit,
.device_init = nl80211_device_init,
.device_deinit = nl80211_device_deinit
.device_deinit = nl80211_device_deinit,
.get_capability = nl80211_get_capability
};

View File

@ -28,7 +28,7 @@ struct nl80211_device_handle {
char phyname[IFNAMSIZ];
/* Capability */
unsigned long physupported;
struct wifi_capability capability;
};
/* Physical device info */