2018-07-31 10:05:32 +02:00
|
|
|
// Basic Config
|
|
|
|
#include "globals.h"
|
|
|
|
#include "wifiscan.h"
|
|
|
|
|
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = "wifi";
|
|
|
|
|
2019-02-02 21:35:40 +01:00
|
|
|
TimerHandle_t WifiChanTimer;
|
|
|
|
|
2018-07-31 10:05:32 +02:00
|
|
|
static wifi_country_t wifi_country = {WIFI_MY_COUNTRY, WIFI_CHANNEL_MIN,
|
2018-08-16 21:10:13 +02:00
|
|
|
WIFI_CHANNEL_MAX, 100,
|
2018-07-31 10:05:32 +02:00
|
|
|
WIFI_COUNTRY_POLICY_MANUAL};
|
|
|
|
|
2018-11-25 11:48:03 +01:00
|
|
|
typedef struct {
|
|
|
|
unsigned frame_ctrl : 16;
|
|
|
|
unsigned duration_id : 16;
|
|
|
|
uint8_t addr1[6]; // receiver address
|
|
|
|
uint8_t addr2[6]; // sender address
|
|
|
|
uint8_t addr3[6]; // filtering address
|
|
|
|
unsigned sequence_ctrl : 16;
|
|
|
|
uint8_t addr4[6]; // optional
|
|
|
|
} wifi_ieee80211_mac_hdr_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
wifi_ieee80211_mac_hdr_t hdr;
|
|
|
|
uint8_t payload[0]; // network data ended with 4 bytes csum (CRC32)
|
|
|
|
} wifi_ieee80211_packet_t;
|
|
|
|
|
2018-07-31 10:05:32 +02:00
|
|
|
// using IRAM_:ATTR here to speed up callback function
|
2019-12-25 23:21:31 +01:00
|
|
|
IRAM_ATTR void wifi_sniffer_packet_handler(void *buff,
|
|
|
|
wifi_promiscuous_pkt_type_t type) {
|
2018-12-02 14:08:50 +01:00
|
|
|
|
2018-07-31 10:05:32 +02:00
|
|
|
const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff;
|
|
|
|
const wifi_ieee80211_packet_t *ipkt =
|
|
|
|
(wifi_ieee80211_packet_t *)ppkt->payload;
|
|
|
|
const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr;
|
|
|
|
|
2020-11-08 22:14:28 +01:00
|
|
|
// process seen MAC
|
|
|
|
mac_add((uint8_t *)hdr->addr2, ppkt->rx_ctrl.rssi, MAC_SNIFF_WIFI);
|
2018-07-31 10:05:32 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 19:11:01 +02:00
|
|
|
// Software-timer driven Wifi channel rotation callback function
|
2019-02-02 21:35:40 +01:00
|
|
|
void switchWifiChannel(TimerHandle_t xTimer) {
|
2020-10-30 12:24:16 +01:00
|
|
|
_ASSERT(xTimer != NULL);
|
2019-07-13 18:27:56 +02:00
|
|
|
channel =
|
|
|
|
(channel % WIFI_CHANNEL_MAX) + 1; // rotate channel 1..WIFI_CHANNEL_MAX
|
|
|
|
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
|
|
|
|
}
|
2019-02-02 21:35:40 +01:00
|
|
|
|
2018-07-31 10:05:32 +02:00
|
|
|
void wifi_sniffer_init(void) {
|
2019-02-02 21:35:40 +01:00
|
|
|
wifi_init_config_t wificfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
wificfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM
|
|
|
|
wificfg.wifi_task_core_id = 0; // we want wifi task running on core 0
|
2018-09-27 14:01:23 +02:00
|
|
|
|
2019-08-28 10:49:25 +02:00
|
|
|
// wifi_promiscuous_filter_t filter = {
|
2019-07-23 21:06:44 +02:00
|
|
|
// .filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // only MGMT frames
|
2019-07-13 18:27:56 +02:00
|
|
|
// .filter_mask = WIFI_PROMIS_FILTER_MASK_ALL}; // we use all frames
|
|
|
|
|
2019-07-23 21:06:44 +02:00
|
|
|
wifi_promiscuous_filter_t filter = {.filter_mask =
|
|
|
|
WIFI_PROMIS_FILTER_MASK_MGMT |
|
|
|
|
WIFI_PROMIS_FILTER_MASK_DATA};
|
2018-12-09 13:48:03 +01:00
|
|
|
|
2019-02-02 21:35:40 +01:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&wificfg)); // configure Wifi with cfg
|
2018-07-31 10:05:32 +02:00
|
|
|
ESP_ERROR_CHECK(
|
|
|
|
esp_wifi_set_country(&wifi_country)); // set locales for RF and channels
|
|
|
|
ESP_ERROR_CHECK(
|
|
|
|
esp_wifi_set_storage(WIFI_STORAGE_RAM)); // we don't need NVRAM
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
|
2019-09-07 15:02:43 +02:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE)); // no modem power saving
|
2019-08-28 10:49:25 +02:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_promiscuous_filter(&filter)); // set frame filter
|
2018-07-31 10:05:32 +02:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler));
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); // now switch on monitor mode
|
|
|
|
|
2019-02-02 21:35:40 +01:00
|
|
|
// setup wifi channel rotation timer
|
|
|
|
WifiChanTimer =
|
|
|
|
xTimerCreate("WifiChannelTimer", pdMS_TO_TICKS(cfg.wifichancycle * 10),
|
|
|
|
pdTRUE, (void *)0, switchWifiChannel);
|
2019-11-11 12:28:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void switch_wifi_sniffer(uint8_t state) {
|
2020-10-30 12:24:16 +01:00
|
|
|
_ASSERT(WifiChanTimer != NULL);
|
2019-11-17 14:13:48 +01:00
|
|
|
if (state) {
|
2019-11-11 12:28:31 +01:00
|
|
|
// switch wifi sniffer on
|
2019-11-17 14:13:48 +01:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
2019-11-11 12:28:31 +01:00
|
|
|
xTimerStart(WifiChanTimer, 0);
|
|
|
|
esp_wifi_set_promiscuous(true);
|
|
|
|
} else {
|
|
|
|
// switch wifi sniffer off
|
|
|
|
xTimerStop(WifiChanTimer, 0);
|
|
|
|
esp_wifi_set_promiscuous(false);
|
2019-11-17 14:13:48 +01:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_stop());
|
2019-11-11 12:28:31 +01:00
|
|
|
macs_wifi = 0; // clear WIFI counter
|
|
|
|
}
|
2019-02-02 21:35:40 +01:00
|
|
|
}
|