eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

ap.c (13481B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2018-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #include <ell/ell.h>
     28 
     29 #include "client/command.h"
     30 #include "client/dbus-proxy.h"
     31 #include "client/device.h"
     32 #include "client/display.h"
     33 #include "client/diagnostic.h"
     34 
     35 struct ap {
     36 	bool started;
     37 	char *name;
     38 	bool scanning;
     39 	uint32_t freq;
     40 	char *pairwise;
     41 	char *group;
     42 };
     43 
     44 static void *ap_create(void)
     45 {
     46 	return l_new(struct ap, 1);
     47 }
     48 
     49 static void ap_destroy(void *data)
     50 {
     51 	struct ap *ap = data;
     52 
     53 	if (ap->name)
     54 		l_free(ap->name);
     55 
     56 	if (ap->pairwise)
     57 		l_free(ap->pairwise);
     58 
     59 	if (ap->group)
     60 		l_free(ap->group);
     61 
     62 	l_free(ap);
     63 }
     64 
     65 static const struct proxy_interface_type_ops ap_ops = {
     66 	.create = ap_create,
     67 	.destroy = ap_destroy,
     68 };
     69 
     70 static const char *get_started_tostr(const void *data)
     71 {
     72 	const struct ap *ap = data;
     73 
     74 	return ap->started ? "yes" : "no";
     75 }
     76 
     77 static void update_started(void *data, struct l_dbus_message_iter *variant)
     78 {
     79 	struct ap *ap = data;
     80 	bool value;
     81 
     82 	if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
     83 		ap->started = false;
     84 
     85 		return;
     86 	}
     87 
     88 	ap->started = value;
     89 }
     90 
     91 static const char *get_name_tostr(const void *data)
     92 {
     93 	const struct ap *ap = data;
     94 
     95 	if (!ap->name)
     96 		return "";
     97 
     98 	return ap->name;
     99 }
    100 
    101 static void update_name(void *data, struct l_dbus_message_iter *variant)
    102 {
    103 	struct ap *ap = data;
    104 	const char *name;
    105 
    106 	if (ap->name)
    107 		l_free(ap->name);
    108 
    109 	if (!l_dbus_message_iter_get_variant(variant, "s", &name)) {
    110 		ap->name = NULL;
    111 		return;
    112 	}
    113 
    114 	ap->name = l_strdup(name);
    115 }
    116 
    117 static void update_scanning(void *data, struct l_dbus_message_iter *variant)
    118 {
    119 	struct ap *ap = data;
    120 	bool value;
    121 
    122 	if (!l_dbus_message_iter_get_variant(variant, "b", &value)) {
    123 		ap->scanning = false;
    124 
    125 		return;
    126 	}
    127 
    128 	ap->scanning = value;
    129 }
    130 
    131 static const char *get_scanning_tostr(const void *data)
    132 {
    133 	const struct ap *ap = data;
    134 
    135 	return ap->scanning ? "yes" : "no";
    136 }
    137 
    138 static void update_freq(void *data, struct l_dbus_message_iter *variant)
    139 {
    140 	struct ap *ap = data;
    141 	uint32_t value;
    142 
    143 	if (!l_dbus_message_iter_get_variant(variant, "u", &value)) {
    144 		ap->freq = 0;
    145 
    146 		return;
    147 	}
    148 
    149 	ap->freq = value;
    150 }
    151 
    152 static const char *get_freq_tostr(const void *data)
    153 {
    154 	const struct ap *ap = data;
    155 	static char str[5];
    156 
    157 	sprintf(str, "%u", ap->freq);
    158 
    159 	return str;
    160 }
    161 
    162 static void update_pairwise(void *data, struct l_dbus_message_iter *variant)
    163 {
    164 	struct ap *ap = data;
    165 	struct l_dbus_message_iter array;
    166 	char *value;
    167 	char **strv;
    168 
    169 
    170 	if (ap->pairwise)
    171 		l_free(ap->pairwise);
    172 
    173 	if (!l_dbus_message_iter_get_variant(variant, "as", &array)) {
    174 		ap->pairwise = NULL;
    175 
    176 		return;
    177 	}
    178 
    179 	strv = l_strv_new();
    180 
    181 	while (l_dbus_message_iter_next_entry(&array, &value))
    182 		strv = l_strv_append(strv, value);
    183 
    184 	ap->pairwise = l_strjoinv(strv, ' ');
    185 	l_strv_free(strv);
    186 }
    187 
    188 static const char *get_pairwise_tostr(const void *data)
    189 {
    190 	const struct ap *ap = data;
    191 
    192 	if (!ap->pairwise)
    193 		return "";
    194 
    195 	return ap->pairwise;
    196 }
    197 
    198 static void update_group(void *data, struct l_dbus_message_iter *variant)
    199 {
    200 	struct ap *ap = data;
    201 	char *value;
    202 
    203 	if (ap->group)
    204 		l_free(ap->group);
    205 
    206 	if (!l_dbus_message_iter_get_variant(variant, "s", &value)) {
    207 		ap->group = NULL;
    208 
    209 		return;
    210 	}
    211 
    212 	ap->group = l_strdup(value);
    213 }
    214 
    215 static const char *get_group_tostr(const void *data)
    216 {
    217 	const struct ap *ap = data;
    218 
    219 	if (!ap->group)
    220 		return "";
    221 
    222 	return ap->group;
    223 }
    224 
    225 static const struct proxy_interface_property ap_properties[] = {
    226 	{ "Started",  "b", update_started,  get_started_tostr },
    227 	{ "Name",     "s", update_name, get_name_tostr },
    228 	{ "Scanning", "b", update_scanning, get_scanning_tostr },
    229 	{ "Frequency", "u", update_freq, get_freq_tostr },
    230 	{ "PairwiseCiphers", "s", update_pairwise, get_pairwise_tostr },
    231 	{ "GroupCipher", "s", update_group, get_group_tostr },
    232 	{ }
    233 };
    234 
    235 static struct proxy_interface_type ap_interface_type = {
    236 	.interface = IWD_ACCESS_POINT_INTERFACE,
    237 	.properties = ap_properties,
    238 	.ops = &ap_ops,
    239 };
    240 
    241 static void check_errors_method_callback(struct l_dbus_message *message,
    242 								void *user_data)
    243 {
    244 	dbus_message_has_error(message);
    245 }
    246 
    247 static void display_ap_inline(const char *margin, const void *data)
    248 {
    249 	const struct proxy_interface *ap_i = data;
    250 	const struct ap *ap = proxy_interface_get_data(ap_i);
    251 	struct proxy_interface *device_i =
    252 		proxy_interface_find(IWD_DEVICE_INTERFACE,
    253 					proxy_interface_get_path(ap_i));
    254 	const char *identity;
    255 
    256 	if (!device_i)
    257 		return;
    258 
    259 	identity = proxy_interface_get_identity_str(device_i);
    260 	if (!identity)
    261 		return;
    262 
    263 	display_table_row(margin, 2, 20, identity, 8, get_started_tostr(ap));
    264 }
    265 
    266 static enum cmd_status cmd_list(const char *device_name, char **argv, int argc)
    267 {
    268 	const struct l_queue_entry *entry;
    269 	struct l_queue *match =
    270 		proxy_interface_find_all(IWD_ACCESS_POINT_INTERFACE,
    271 						NULL, NULL);
    272 
    273 	display_table_header("Devices in Access Point Mode",
    274 				MARGIN "%-*s  %-*s", 20, "Name", 8, "Started");
    275 
    276 	if (!match) {
    277 		display("No devices in access point mode available.\n");
    278 		display_table_footer();
    279 
    280 		return CMD_STATUS_DONE;
    281 	}
    282 
    283 	for (entry = l_queue_get_entries(match); entry; entry = entry->next) {
    284 		const struct proxy_interface *ap = entry->data;
    285 		display_ap_inline(MARGIN, ap);
    286 	}
    287 
    288 	display_table_footer();
    289 
    290 	l_queue_destroy(match, NULL);
    291 
    292 	return CMD_STATUS_DONE;
    293 }
    294 
    295 static enum cmd_status cmd_start(const char *device_name, char **argv, int argc)
    296 {
    297 	const struct proxy_interface *ap_i;
    298 
    299 	if (argc < 2)
    300 		return CMD_STATUS_INVALID_ARGS;
    301 
    302 	if (strlen(argv[0]) > 32) {
    303 		display("Network name cannot exceed 32 characters.\n");
    304 
    305 		return CMD_STATUS_INVALID_VALUE;
    306 	}
    307 
    308 	if (strlen(argv[1]) < 8) {
    309 		display("Passphrase cannot be shorted than 8 characters.\n");
    310 
    311 		return CMD_STATUS_INVALID_VALUE;
    312 	}
    313 
    314 	ap_i = device_proxy_find(device_name, IWD_ACCESS_POINT_INTERFACE);
    315 	if (!ap_i) {
    316 		display("No ap on device: '%s'\n", device_name);
    317 		return CMD_STATUS_INVALID_VALUE;
    318 	}
    319 
    320 	proxy_interface_method_call(ap_i, "Start", "ss",
    321 						check_errors_method_callback,
    322 						argv[0], argv[1]);
    323 
    324 	return CMD_STATUS_TRIGGERED;
    325 }
    326 
    327 static enum cmd_status cmd_stop(const char *device_name, char **argv, int argc)
    328 {
    329 	const struct proxy_interface *ap_i =
    330 		device_proxy_find(device_name, IWD_ACCESS_POINT_INTERFACE);
    331 
    332 	if (!ap_i) {
    333 		display("No ap on device: '%s'\n", device_name);
    334 		return CMD_STATUS_INVALID_VALUE;
    335 	}
    336 
    337 	proxy_interface_method_call(ap_i, "Stop", "",
    338 						check_errors_method_callback);
    339 
    340 	return CMD_STATUS_TRIGGERED;
    341 }
    342 
    343 static struct proxy_interface_type ap_diagnostic_interface_type = {
    344 	.interface = IWD_AP_DIAGNOSTIC_INTERFACE,
    345 };
    346 
    347 static void ap_get_diagnostics_callback(struct l_dbus_message *message,
    348 					void *user_data)
    349 {
    350 	struct l_dbus_message_iter array;
    351 	struct l_dbus_message_iter iter;
    352 	uint16_t idx = 0;
    353 	char client_num[15];
    354 
    355 	if (dbus_message_has_error(message))
    356 		return;
    357 
    358 	if (!l_dbus_message_get_arguments(message, "aa{sv}", &array)) {
    359 		display("Failed to parse GetDiagnostics message");
    360 		return;
    361 	}
    362 
    363 	while (l_dbus_message_iter_next_entry(&array, &iter)) {
    364 		sprintf(client_num, "STA %u", idx++);
    365 		display_table_header("", MARGIN "%-*s  %-*s  %-*s", 8, client_num,
    366 					20, "Property", 20, "Value");
    367 		diagnostic_display(&iter, MARGIN, 20, 20);
    368 		display_table_footer();
    369 	}
    370 }
    371 
    372 static enum cmd_status cmd_show(const char *device_name, char **argv, int argc)
    373 {
    374 	const struct proxy_interface *ap_diagnostic =
    375 		device_proxy_find(device_name, IWD_AP_DIAGNOSTIC_INTERFACE);
    376 	const struct proxy_interface *ap_i =
    377 		device_proxy_find(device_name, IWD_ACCESS_POINT_INTERFACE);
    378 
    379 	if (!ap_i) {
    380 		display("No ap on device: '%s'\n", device_name);
    381 		return CMD_STATUS_INVALID_VALUE;
    382 	}
    383 
    384 	proxy_properties_display(ap_i, "Access Point Interface", MARGIN, 20, 20);
    385 
    386 	if (!ap_diagnostic) {
    387 		display_table_footer();
    388 		return CMD_STATUS_DONE;
    389 	}
    390 
    391 	proxy_interface_method_call(ap_diagnostic, "GetDiagnostics", "",
    392 					ap_get_diagnostics_callback);
    393 
    394 	return CMD_STATUS_TRIGGERED;
    395 }
    396 
    397 static enum cmd_status cmd_start_profile(const char *device_name,
    398 						char **argv, int argc)
    399 {
    400 	const struct proxy_interface *ap_i;
    401 
    402 	if (argc < 1)
    403 		return CMD_STATUS_INVALID_ARGS;
    404 
    405 	if (strlen(argv[0]) > 32) {
    406 		display("Network name cannot exceed 32 characters.\n");
    407 
    408 		return CMD_STATUS_INVALID_VALUE;
    409 	}
    410 
    411 	ap_i = device_proxy_find(device_name, IWD_ACCESS_POINT_INTERFACE);
    412 	if (!ap_i) {
    413 		display("No ap on device: '%s'\n", device_name);
    414 		return CMD_STATUS_INVALID_VALUE;
    415 	}
    416 
    417 	proxy_interface_method_call(ap_i, "StartProfile", "s",
    418 						check_errors_method_callback,
    419 						argv[0]);
    420 
    421 	return CMD_STATUS_TRIGGERED;
    422 }
    423 
    424 static enum cmd_status cmd_scan(const char *device_name, char **argv, int argc)
    425 {
    426 	const struct proxy_interface *ap_i;
    427 
    428 	ap_i = device_proxy_find(device_name, IWD_ACCESS_POINT_INTERFACE);
    429 	if (!ap_i) {
    430 		display("No ap on device: '%s'\n", device_name);
    431 		return CMD_STATUS_INVALID_VALUE;
    432 	}
    433 
    434 	proxy_interface_method_call(ap_i, "Scan", "",
    435 						check_errors_method_callback);
    436 
    437 	return CMD_STATUS_TRIGGERED;
    438 }
    439 
    440 static void ap_display_network(struct l_dbus_message_iter *iter,
    441 				const char *margin, int name_width,
    442 				int value_width)
    443 {
    444 	const char *key;
    445 	struct l_dbus_message_iter variant;
    446 
    447 	while (l_dbus_message_iter_next_entry(iter, &key, &variant)) {
    448 		const char *s;
    449 		int16_t n;
    450 
    451 		if (!strcmp(key, "Name") || !strcmp(key, "Type")) {
    452 			if (!l_dbus_message_iter_get_variant(&variant, "s", &s))
    453 				goto parse_error;
    454 
    455 			display_table_row(margin, 2, name_width, key,
    456 						value_width, s);
    457 		} else if (!strcmp(key, "SignalStrength")) {
    458 			char signal[7];
    459 
    460 			if (!l_dbus_message_iter_get_variant(&variant, "n", &n))
    461 				goto parse_error;
    462 
    463 			snprintf(signal, sizeof(signal), "%i", n);
    464 
    465 			display_table_row(margin, 2, name_width, key,
    466 						value_width, signal);
    467 		}
    468 	}
    469 
    470 	return;
    471 
    472 parse_error:
    473 	display("Error displaying network results");
    474 }
    475 
    476 static void ap_get_networks_callback(struct l_dbus_message *message,
    477 					void *user_data)
    478 {
    479 	struct l_dbus_message_iter array;
    480 	struct l_dbus_message_iter iter;
    481 
    482 	if (dbus_message_has_error(message))
    483 		return;
    484 
    485 	if (!l_dbus_message_get_arguments(message, "aa{sv}", &array)) {
    486 		display("Failed to parse GetDiagnostics message");
    487 		return;
    488 	}
    489 
    490 	display_table_header("Networks", "            %-*s  %-*s",
    491 					20, "Property", 20, "Value");
    492 	while (l_dbus_message_iter_next_entry(&array, &iter)) {
    493 		ap_display_network(&iter, "            ", 20, 20);
    494 		display("\n");
    495 	}
    496 
    497 	display_table_footer();
    498 }
    499 
    500 static enum cmd_status cmd_get_networks(const char *device_name, char **argv,
    501 					int argc)
    502 {
    503 	const struct proxy_interface *ap_i;
    504 
    505 	ap_i = device_proxy_find(device_name, IWD_ACCESS_POINT_INTERFACE);
    506 	if (!ap_i) {
    507 		display("No ap on device: '%s'\n", device_name);
    508 		return CMD_STATUS_INVALID_VALUE;
    509 	}
    510 
    511 	proxy_interface_method_call(ap_i, "GetOrderedNetworks", "",
    512 						ap_get_networks_callback);
    513 
    514 	return CMD_STATUS_TRIGGERED;
    515 }
    516 
    517 static const struct command ap_commands[] = {
    518 	{ NULL, "list", NULL, cmd_list, "List devices in AP mode", true },
    519 	{ "<wlan>", "start", "<\"network name\"> <passphrase>", cmd_start,
    520 		"Start an access point called \"network "
    521 		"name\" with a passphrase" },
    522 	{ "<wlan>", "start-profile", "<\"network name\">", cmd_start_profile,
    523 		"Start an access point based on a disk profile" },
    524 	{ "<wlan>", "stop", NULL,   cmd_stop, "Stop a started access point" },
    525 	{ "<wlan>", "show", NULL, cmd_show, "Show AP info", false },
    526 	{ "<wlan>", "scan", NULL, cmd_scan, "Start an AP scan", false },
    527 	{ "<wlan>", "get-networks", NULL, cmd_get_networks,
    528 				"Get network list after scanning", false },
    529 	{ }
    530 };
    531 
    532 static char *family_arg_completion(const char *text, int state)
    533 {
    534 	return device_arg_completion(text, state, ap_commands,
    535 						IWD_ACCESS_POINT_INTERFACE);
    536 }
    537 
    538 static char *entity_arg_completion(const char *text, int state)
    539 {
    540 	return command_entity_arg_completion(text, state, ap_commands);
    541 }
    542 
    543 static struct command_family ap_command_family = {
    544 	.caption = "Access Point",
    545 	.name = "ap",
    546 	.command_list = ap_commands,
    547 	.family_arg_completion = family_arg_completion,
    548 	.entity_arg_completion = entity_arg_completion,
    549 };
    550 
    551 static int ap_command_family_init(void)
    552 {
    553 	command_family_register(&ap_command_family);
    554 
    555 	return 0;
    556 }
    557 
    558 static void ap_command_family_exit(void)
    559 {
    560 	command_family_unregister(&ap_command_family);
    561 }
    562 
    563 COMMAND_FAMILY(ap_command_family, ap_command_family_init,
    564 							ap_command_family_exit)
    565 
    566 static int ap_interface_init(void)
    567 {
    568 	proxy_interface_type_register(&ap_interface_type);
    569 	proxy_interface_type_register(&ap_diagnostic_interface_type);
    570 
    571 	return 0;
    572 }
    573 
    574 static void ap_interface_exit(void)
    575 {
    576 	proxy_interface_type_unregister(&ap_interface_type);
    577 	proxy_interface_type_unregister(&ap_diagnostic_interface_type);
    578 }
    579 
    580 INTERFACE_TYPE(ap_interface_type, ap_interface_init, ap_interface_exit)