2014-08-09 00:40:43 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Wireless daemon for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <ell/ell.h>
|
|
|
|
|
|
|
|
enum {
|
|
|
|
HWSIM_CMD_UNSPEC,
|
|
|
|
HWSIM_CMD_REGISTER,
|
|
|
|
HWSIM_CMD_FRAME,
|
|
|
|
HWSIM_CMD_TX_INFO_FRAME,
|
2014-10-31 14:42:40 +01:00
|
|
|
HWSIM_CMD_NEW_RADIO,
|
|
|
|
HWSIM_CMD_DEL_RADIO,
|
2014-08-09 00:40:43 +02:00
|
|
|
__HWSIM_CMD_MAX,
|
|
|
|
};
|
|
|
|
#define HWSIM_CMD_MAX (_HWSIM_CMD_MAX - 1)
|
|
|
|
|
|
|
|
enum {
|
|
|
|
HWSIM_ATTR_UNSPEC,
|
|
|
|
HWSIM_ATTR_ADDR_RECEIVER,
|
|
|
|
HWSIM_ATTR_ADDR_TRANSMITTER,
|
|
|
|
HWSIM_ATTR_FRAME,
|
|
|
|
HWSIM_ATTR_FLAGS,
|
|
|
|
HWSIM_ATTR_RX_RATE,
|
|
|
|
HWSIM_ATTR_SIGNAL,
|
|
|
|
HWSIM_ATTR_TX_INFO,
|
|
|
|
HWSIM_ATTR_COOKIE,
|
|
|
|
HWSIM_ATTR_CHANNELS,
|
|
|
|
HWSIM_ATTR_RADIO_ID,
|
|
|
|
HWSIM_ATTR_REG_HINT_ALPHA2,
|
|
|
|
HWSIM_ATTR_REG_CUSTOM_REG,
|
|
|
|
HWSIM_ATTR_REG_STRICT_REG,
|
|
|
|
HWSIM_ATTR_SUPPORT_P2P_DEVICE,
|
|
|
|
HWSIM_ATTR_USE_CHANCTX,
|
2014-10-10 09:47:19 +02:00
|
|
|
HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE,
|
2014-08-09 00:40:43 +02:00
|
|
|
__HWSIM_ATTR_MAX,
|
|
|
|
};
|
|
|
|
#define HWSIM_ATTR_MAX (__HWSIM_ATTR_MAX - 1)
|
|
|
|
|
|
|
|
static struct l_genl_family *hwsim;
|
|
|
|
|
2014-10-10 09:47:19 +02:00
|
|
|
static bool keep_radios = false;
|
2014-08-09 00:40:43 +02:00
|
|
|
static bool create_action = false;
|
|
|
|
static const char *destroy_action = NULL;
|
|
|
|
|
|
|
|
static void do_debug(const char *str, void *user_data)
|
|
|
|
{
|
|
|
|
const char *prefix = user_data;
|
|
|
|
|
|
|
|
l_info("%s%s", prefix, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void create_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint32_t radio_id = 0;
|
|
|
|
|
2014-10-08 04:36:10 +02:00
|
|
|
/*
|
|
|
|
* Note that the radio id is returned in the error field of
|
2014-10-07 14:07:15 +02:00
|
|
|
* the returned message.
|
|
|
|
*/
|
|
|
|
if (!l_genl_attr_init(&attr, msg)) {
|
|
|
|
int err = l_genl_msg_get_error(msg);
|
|
|
|
if (err < 0) {
|
|
|
|
l_warn("Failed to initialize create return attributes"
|
|
|
|
" [%d/%s]", -err, strerror(-err));
|
|
|
|
goto done;
|
2014-08-09 00:40:43 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 14:07:15 +02:00
|
|
|
radio_id = err;
|
|
|
|
|
|
|
|
l_info("Created new radio with id %u", radio_id);
|
|
|
|
} else {
|
|
|
|
l_warn("Failed to get create return value");
|
|
|
|
goto done;
|
|
|
|
}
|
2014-08-09 00:40:43 +02:00
|
|
|
|
|
|
|
done:
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_callback(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg)) {
|
2014-10-07 14:07:15 +02:00
|
|
|
int err = l_genl_msg_get_error(msg);
|
|
|
|
if (err < 0) {
|
|
|
|
l_warn("Failed to destroy radio [%d/%s]",
|
|
|
|
-err, strerror(-err));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_info("Destroyed radio");
|
2014-08-09 00:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
2014-10-31 14:42:40 +01:00
|
|
|
static void hwsim_config(struct l_genl_msg *msg, void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_attr attr;
|
|
|
|
uint16_t type, len;
|
|
|
|
const void *data;
|
|
|
|
uint8_t cmd;
|
|
|
|
|
|
|
|
cmd = l_genl_msg_get_command(msg);
|
|
|
|
|
|
|
|
l_debug("Config changed cmd %u", cmd);
|
|
|
|
|
|
|
|
if (!l_genl_attr_init(&attr, msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (l_genl_attr_next(&attr, &type, &len, &data)) {
|
|
|
|
l_debug("\tattr type %d len %d", type, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
static void hwsim_ready(void *user_data)
|
|
|
|
{
|
|
|
|
struct l_genl_msg *msg;
|
2014-10-31 14:42:40 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = l_genl_family_register(hwsim, "config", hwsim_config,
|
2014-11-02 03:27:51 +01:00
|
|
|
NULL, NULL);
|
2014-10-31 14:42:40 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Failed to create hwsim config listener\n");
|
|
|
|
l_main_quit();
|
|
|
|
return;
|
|
|
|
}
|
2014-08-09 00:40:43 +02:00
|
|
|
|
|
|
|
if (create_action) {
|
2014-10-31 14:42:40 +01:00
|
|
|
msg = l_genl_msg_new_sized(HWSIM_CMD_NEW_RADIO,
|
2014-10-10 09:47:19 +02:00
|
|
|
keep_radios ? 0 : 4);
|
2014-10-13 19:56:27 +02:00
|
|
|
|
2014-10-10 09:47:19 +02:00
|
|
|
if (!keep_radios)
|
|
|
|
l_genl_msg_append_attr(msg,
|
|
|
|
HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE,
|
|
|
|
0, NULL);
|
|
|
|
|
2014-08-09 00:40:43 +02:00
|
|
|
l_genl_family_send(hwsim, msg, create_callback, NULL, NULL);
|
|
|
|
l_genl_msg_unref(msg);
|
|
|
|
return;
|
|
|
|
} else if (destroy_action) {
|
|
|
|
uint32_t id = atoi(destroy_action);
|
|
|
|
|
2014-10-31 14:42:40 +01:00
|
|
|
msg = l_genl_msg_new_sized(HWSIM_CMD_DEL_RADIO, 8);
|
2014-08-09 00:40:43 +02:00
|
|
|
l_genl_msg_append_attr(msg, HWSIM_ATTR_RADIO_ID, 4, &id);
|
|
|
|
l_genl_family_send(hwsim, msg, destroy_callback, NULL, NULL);
|
|
|
|
l_genl_msg_unref(msg);
|
|
|
|
} else
|
|
|
|
l_main_quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_handler(struct l_signal *signal, uint32_t signo,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
switch (signo) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
l_main_quit();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
printf("hwsim - Wireless simulator\n"
|
|
|
|
"Usage:\n");
|
|
|
|
printf("\thwsim [options]\n");
|
|
|
|
printf("Options:\n"
|
|
|
|
"\t-C, --create Create new simulated radio\n"
|
|
|
|
"\t-D, --destroy <id> Destroy existing radio\n"
|
2014-10-10 09:47:19 +02:00
|
|
|
"\t-k, --keep Do not destroy radios when "
|
|
|
|
"program exits\n"
|
2014-08-09 00:40:43 +02:00
|
|
|
"\t-h, --help Show help options\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct option main_options[] = {
|
|
|
|
{ "create", no_argument, NULL, 'C' },
|
|
|
|
{ "destroy", required_argument, NULL, 'D' },
|
2014-10-10 09:47:19 +02:00
|
|
|
{ "keep", no_argument, NULL, 'k' },
|
2014-08-09 00:40:43 +02:00
|
|
|
{ "version", no_argument, NULL, 'v' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct l_signal *signal;
|
|
|
|
struct l_genl *genl;
|
|
|
|
sigset_t mask;
|
|
|
|
int exit_status;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int opt;
|
|
|
|
|
2014-10-10 09:47:19 +02:00
|
|
|
opt = getopt_long(argc, argv, "CD:vhk", main_options, NULL);
|
2014-08-09 00:40:43 +02:00
|
|
|
if (opt < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (opt) {
|
|
|
|
case 'C':
|
|
|
|
create_action = true;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
destroy_action = optarg;
|
|
|
|
break;
|
2014-10-10 09:47:19 +02:00
|
|
|
case 'k':
|
|
|
|
keep_radios = true;
|
|
|
|
break;
|
2014-08-09 00:40:43 +02:00
|
|
|
case 'v':
|
|
|
|
printf("%s\n", VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
default:
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc - optind > 0) {
|
|
|
|
fprintf(stderr, "Invalid command line parameters\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create_action && destroy_action) {
|
|
|
|
fprintf(stderr, "Only one action can be specified\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!create_action && !destroy_action) {
|
|
|
|
fprintf(stderr, "No action has been specified\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
sigaddset(&mask, SIGTERM);
|
|
|
|
|
|
|
|
signal = l_signal_create(&mask, signal_handler, NULL, NULL);
|
|
|
|
|
|
|
|
l_log_set_stderr();
|
|
|
|
|
|
|
|
printf("Wireless simulator ver %s\n", VERSION);
|
|
|
|
|
|
|
|
genl = l_genl_new_default();
|
|
|
|
if (!genl) {
|
|
|
|
fprintf(stderr, "Failed to initialize generic netlink\n");
|
|
|
|
exit_status = EXIT_FAILURE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getenv("HWSIM_DEBUG"))
|
|
|
|
l_genl_set_debug(genl, do_debug, "[GENL] ", NULL);
|
|
|
|
|
|
|
|
hwsim = l_genl_family_new(genl, "MAC80211_HWSIM");
|
|
|
|
if (!hwsim) {
|
|
|
|
fprintf(stderr, "Failed to create generic netlink family\n");
|
|
|
|
l_genl_unref(genl);
|
|
|
|
exit_status = EXIT_FAILURE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
l_genl_family_set_watches(hwsim, hwsim_ready, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
l_main_run();
|
|
|
|
|
|
|
|
l_genl_family_unref(hwsim);
|
|
|
|
l_genl_unref(genl);
|
|
|
|
|
|
|
|
exit_status = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
done:
|
|
|
|
l_signal_remove(signal);
|
|
|
|
|
|
|
|
return exit_status;
|
|
|
|
}
|