eiwd

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

control.c (8410B)


      1 #include "config.h"
      2 #include "src/control.h"
      3 #include "shared/control.h"
      4 
      5 #include <ell/ell.h>
      6 #include <errno.h>
      7 #include <stdio.h>
      8 #include <string.h>
      9 #include <sys/socket.h>
     10 #include <sys/stat.h>
     11 #include <sys/un.h>
     12 #include <unistd.h>
     13 
     14 #ifndef CONTROL_SOCKET_GID
     15 #define CONTROL_SOCKET_GID 10
     16 #endif
     17 
     18 #include "src/common.h"
     19 #include "src/netdev.h"
     20 #include "src/network.h"
     21 #include "src/station.h"
     22 
     23 struct ControlClient {
     24 	struct l_io *io;
     25 	struct l_timeout *timeout;
     26 };
     27 
     28 static struct l_io *listener;
     29 static struct l_queue *clients;
     30 static char *socket_path;
     31 
     32 static void
     33 client_free(void *data)
     34 {
     35 	struct ControlClient *client = data;
     36 
     37 	l_io_set_disconnect_handler(client->io, NULL, NULL, NULL);
     38 	l_io_destroy(client->io);
     39 	l_timeout_remove(client->timeout);
     40 	l_free(client);
     41 }
     42 
     43 static void
     44 client_disconnect(struct l_io *io, void *data)
     45 {
     46 	l_queue_remove(clients, data);
     47 	client_free(data);
     48 }
     49 
     50 static void
     51 client_timeout(struct l_timeout *timeout, void *data)
     52 {
     53 	l_queue_remove(clients, data);
     54 	client_free(data);
     55 }
     56 
     57 static bool
     58 send_text(int fd, const char *text)
     59 {
     60 	size_t size = strlen(text) + 1;
     61 
     62 	return L_TFR(send(fd, text, size, MSG_NOSIGNAL)) == (ssize_t)size;
     63 }
     64 
     65 static bool
     66 send_name(const char *name, void *data)
     67 {
     68 	return send_text(*(int *)data, name);
     69 }
     70 
     71 /* every request is answered for the first wireless interface there is */
     72 static void
     73 first_station_cb(struct station *station, void *data)
     74 {
     75 	struct station **first = data;
     76 
     77 	if (!*first)
     78 		*first = station;
     79 }
     80 
     81 static struct station *
     82 first_station(void)
     83 {
     84 	struct station *station = NULL;
     85 
     86 	station_foreach(first_station_cb, &station);
     87 	return station;
     88 }
     89 
     90 static const char *
     91 state_name(enum station_state state)
     92 {
     93 	switch (state) {
     94 	case STATION_STATE_CONNECTED:
     95 	case STATION_STATE_NETCONFIG:
     96 		return "connected";
     97 	case STATION_STATE_CONNECTING:
     98 	case STATION_STATE_CONNECTING_AUTO:
     99 		return "connecting";
    100 	case STATION_STATE_DISCONNECTING:
    101 		return "disconnecting";
    102 	case STATION_STATE_ROAMING:
    103 	case STATION_STATE_FT_ROAMING:
    104 	case STATION_STATE_FW_ROAMING:
    105 		return "roaming";
    106 	default:
    107 		return "disconnected";
    108 	}
    109 }
    110 
    111 static const char *
    112 security_name(enum security security)
    113 {
    114 	switch (security) {
    115 	case SECURITY_NONE:
    116 		return "open";
    117 	case SECURITY_WEP:
    118 		return "wep";
    119 	case SECURITY_PSK:
    120 		return "psk";
    121 	default:
    122 		return "8021x";
    123 	}
    124 }
    125 
    126 static void
    127 send_status(int fd, struct station *station)
    128 {
    129 	char line[CONTROL_REPLY_MAX];
    130 	struct network *network = station_get_connected_network(station);
    131 
    132 	snprintf(line, sizeof(line), "%s\t%s\t%d",
    133 			state_name(station_get_state(station)),
    134 			network ? network_get_ssid(network) : "",
    135 			network ? network_get_signal_strength(network) / 100 : 0);
    136 	send_text(fd, line);
    137 }
    138 
    139 struct list_state {
    140 	int fd;
    141 	struct network *connected;
    142 };
    143 
    144 static void
    145 send_network(struct network *network, void *data)
    146 {
    147 	struct list_state *list = data;
    148 	char line[CONTROL_REPLY_MAX];
    149 
    150 	snprintf(line, sizeof(line), "%s\t%s\t%d\t%s",
    151 			network_get_ssid(network),
    152 			security_name(network_get_security(network)),
    153 			network_get_signal_strength(network) / 100,
    154 			network == list->connected ? "connected" :
    155 			network_get_settings(network) ? "known" : "");
    156 	send_text(list->fd, line);
    157 }
    158 
    159 struct find_state {
    160 	const char *ssid;
    161 	struct network *found;
    162 };
    163 
    164 static void
    165 find_network(struct network *network, void *data)
    166 {
    167 	struct find_state *find = data;
    168 
    169 	if (!find->found && !strcmp(network_get_ssid(network), find->ssid))
    170 		find->found = network;
    171 }
    172 
    173 /* connect <ssid> [passphrase] */
    174 static void
    175 do_connect(int fd, struct station *station, char *args)
    176 {
    177 	struct find_state find = { .ssid = args };
    178 	char *passphrase = strchr(args, '\t');
    179 	struct scan_bss *bss;
    180 
    181 	if (passphrase)
    182 		*passphrase++ = '\0';
    183 	station_foreach_network(station, find_network, &find);
    184 	if (!find.found) {
    185 		send_text(fd, "error no such network");
    186 		return;
    187 	}
    188 	if (network_get_security(find.found) == SECURITY_8021X ||
    189 			network_get_security(find.found) == SECURITY_WEP) {
    190 		send_text(fd, "error that kind of security is not supported");
    191 		return;
    192 	}
    193 	if (passphrase && *passphrase &&
    194 			network_get_security(find.found) == SECURITY_PSK &&
    195 			!network_set_passphrase(find.found, passphrase)) {
    196 		send_text(fd, "error not a usable passphrase");
    197 		return;
    198 	}
    199 	if (network_get_security(find.found) == SECURITY_PSK &&
    200 			!network_get_settings(find.found) &&
    201 			(!passphrase || !*passphrase)) {
    202 		send_text(fd, "error that network needs a passphrase");
    203 		return;
    204 	}
    205 	bss = network_bss_select(find.found, true);
    206 	if (!bss) {
    207 		send_text(fd, "error the network is not in range");
    208 		return;
    209 	}
    210 	station_connect_network(station, find.found, bss, NULL);
    211 	send_text(fd, "ok");
    212 }
    213 
    214 static bool
    215 client_read(struct l_io *io, void *data)
    216 {
    217 	char request[CONTROL_REQUEST_MAX + 1];
    218 	ssize_t len;
    219 	int fd = l_io_get_fd(io);
    220 	struct station *station;
    221 
    222 	len = recv(fd, request, CONTROL_REQUEST_MAX, MSG_TRUNC);
    223 	if (len < 0 && (errno == EINTR || errno == EAGAIN))
    224 		return true;
    225 	if (len < 0 || len > CONTROL_REQUEST_MAX)
    226 		goto done;
    227 	request[len] = '\0';
    228 	station = first_station();
    229 	if (!strcmp(request, CONTROL_PING)) {
    230 		send_text(fd, CONTROL_PONG);
    231 		goto done;
    232 	}
    233 	if (!strcmp(request, CONTROL_DEVICE_LIST)) {
    234 		if (!netdev_foreach_name(send_name, &fd))
    235 			goto done;
    236 	} else if (!station) {
    237 		send_text(fd, "error no wireless interface");
    238 	} else if (!strcmp(request, CONTROL_STATUS)) {
    239 		send_status(fd, station);
    240 	} else if (!strcmp(request, CONTROL_NETWORKS)) {
    241 		struct list_state list = { fd, station_get_connected_network(station) };
    242 
    243 		station_foreach_network(station, send_network, &list);
    244 	} else if (!strncmp(request, CONTROL_CONNECT "\t",
    245 				sizeof(CONTROL_CONNECT))) {
    246 		do_connect(fd, station, request + sizeof(CONTROL_CONNECT));
    247 	} else if (!strcmp(request, CONTROL_DISCONNECT)) {
    248 		int err = station_disconnect(station);
    249 
    250 		send_text(fd, err < 0 && err != -ENOTCONN ? "error not connected" : "ok");
    251 	} else {
    252 		send_text(fd, "error unknown request");
    253 	}
    254 	send_text(fd, "");
    255 
    256 done:
    257 	/* Let the disconnect handler release this client's watch. */
    258 	shutdown(fd, SHUT_RDWR);
    259 	return false;
    260 }
    261 
    262 static bool
    263 accept_client(struct l_io *io, void *data)
    264 {
    265 	struct ControlClient *client;
    266 	int fd;
    267 
    268 	fd = accept4(l_io_get_fd(io), NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
    269 	if (fd < 0)
    270 		return true;
    271 	if (l_queue_length(clients) >= 16) {
    272 		close(fd);
    273 		return true;
    274 	}
    275 
    276 	client = l_new(struct ControlClient, 1);
    277 	client->io = l_io_new(fd);
    278 	if (!client->io) {
    279 		close(fd);
    280 		l_free(client);
    281 		return true;
    282 	}
    283 	l_io_set_close_on_destroy(client->io, true);
    284 	if (!l_io_set_read_handler(client->io, client_read, client, NULL)) {
    285 		client_free(client);
    286 		return true;
    287 	}
    288 	l_io_set_disconnect_handler(client->io, client_disconnect, client, NULL);
    289 	client->timeout = l_timeout_create(5, client_timeout, client, NULL);
    290 	if (!client->timeout) {
    291 		client_free(client);
    292 		return true;
    293 	}
    294 	l_queue_push_tail(clients, client);
    295 	return true;
    296 }
    297 
    298 int
    299 control_init(const char *path)
    300 {
    301 	struct sockaddr_un address = { .sun_family = AF_UNIX };
    302 	mode_t mask;
    303 	int fd, err;
    304 
    305 	if (listener)
    306 		return -EALREADY;
    307 	if (!*path)
    308 		return -EINVAL;
    309 	if (strlen(path) >= sizeof(address.sun_path))
    310 		return -ENAMETOOLONG;
    311 	memcpy(address.sun_path, path, strlen(path) + 1);
    312 	fd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
    313 	if (fd < 0)
    314 		return -errno;
    315 
    316 	mask = umask(0177);
    317 	err = bind(fd, (struct sockaddr *)&address, sizeof(address));
    318 	if (err < 0)
    319 		err = -errno;
    320 	umask(mask);
    321 	if (err < 0)
    322 		goto close_fd;
    323 	/*
    324 	 * Whoever may use the network is whoever is in the group that runs the
    325 	 * machine, wheel (10) by default: the socket is root's alone only
    326 	 * until here.
    327 	 */
    328 	if (chown(path, (uid_t)-1, CONTROL_SOCKET_GID) == 0)
    329 		chmod(path, 0660);
    330 	if (listen(fd, 16) < 0) {
    331 		err = -errno;
    332 		goto unlink_socket;
    333 	}
    334 	listener = l_io_new(fd);
    335 	if (!listener) {
    336 		err = -EIO;
    337 		goto unlink_socket;
    338 	}
    339 	l_io_set_close_on_destroy(listener, true);
    340 	if (!l_io_set_read_handler(listener, accept_client, NULL, NULL)) {
    341 		l_io_destroy(listener);
    342 		listener = NULL;
    343 		unlink(path);
    344 		return -EIO;
    345 	}
    346 	clients = l_queue_new();
    347 	socket_path = l_strdup(path);
    348 	return 0;
    349 
    350 unlink_socket:
    351 	unlink(path);
    352 close_fd:
    353 	close(fd);
    354 	return err;
    355 }
    356 
    357 void
    358 control_exit(void)
    359 {
    360 	l_io_destroy(listener);
    361 	listener = NULL;
    362 	l_queue_destroy(clients, client_free);
    363 	clients = NULL;
    364 	if (socket_path)
    365 		unlink(socket_path);
    366 	l_free(socket_path);
    367 	socket_path = NULL;
    368 }