2017-01-10 05:36:19 +00:00
|
|
|
/* Copyright (C) 2015-2017 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. */
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
2017-09-25 02:22:09 +00:00
|
|
|
#include "containers.h"
|
2016-07-20 19:24:27 +00:00
|
|
|
#include "ipc.h"
|
2015-06-05 13:58:00 +00:00
|
|
|
#include "terminal.h"
|
2017-05-15 21:24:48 +00:00
|
|
|
#include "encoding.h"
|
2017-09-25 02:22:09 +00:00
|
|
|
#include "subcommands.h"
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
static int peer_cmp(const void *first, const void *second)
|
|
|
|
{
|
|
|
|
time_t diff;
|
|
|
|
const struct wgpeer *a = *(const void **)first, *b = *(const void **)second;
|
|
|
|
if (!a->last_handshake_time.tv_sec && !a->last_handshake_time.tv_usec && (b->last_handshake_time.tv_sec || b->last_handshake_time.tv_usec))
|
|
|
|
return 1;
|
|
|
|
if (!b->last_handshake_time.tv_sec && !b->last_handshake_time.tv_usec && (a->last_handshake_time.tv_sec || a->last_handshake_time.tv_usec))
|
|
|
|
return -1;
|
|
|
|
diff = a->last_handshake_time.tv_sec - b->last_handshake_time.tv_sec;
|
|
|
|
if (!diff)
|
|
|
|
diff = a->last_handshake_time.tv_usec - b->last_handshake_time.tv_usec;
|
|
|
|
if (diff < 0)
|
|
|
|
return 1;
|
|
|
|
if (diff > 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:22:09 +00:00
|
|
|
/* This, hilariously, is not the right way to sort a linked list... */
|
2015-06-05 13:58:00 +00:00
|
|
|
static void sort_peers(struct wgdevice *device)
|
|
|
|
{
|
2017-09-25 02:22:09 +00:00
|
|
|
size_t peer_count = 0, i = 0;
|
|
|
|
struct wgpeer *peer, **peers;
|
2015-06-05 13:58:00 +00:00
|
|
|
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer)
|
2017-09-25 02:22:09 +00:00
|
|
|
++peer_count;
|
|
|
|
if (!peer_count)
|
2015-06-05 13:58:00 +00:00
|
|
|
return;
|
2017-09-25 02:22:09 +00:00
|
|
|
peers = calloc(peer_count, sizeof(struct wgpeer *));
|
|
|
|
if (!peers)
|
2015-06-05 13:58:00 +00:00
|
|
|
return;
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer)
|
2017-09-25 02:22:09 +00:00
|
|
|
peers[i++] = peer;
|
|
|
|
qsort(peers, peer_count, sizeof(struct wgpeer *), peer_cmp);
|
|
|
|
device->first_peer = peers[0];
|
|
|
|
peers[0]->next_peer = NULL;
|
|
|
|
for (i = 1; i < peer_count; ++i) {
|
|
|
|
peers[i - 1]->next_peer = peers[i];
|
|
|
|
peers[i]->next_peer = NULL;
|
2015-06-05 13:58:00 +00:00
|
|
|
}
|
|
|
|
free(peers);
|
|
|
|
}
|
|
|
|
|
2017-04-15 23:20:43 +00:00
|
|
|
static char *key(const uint8_t key[static WG_KEY_LEN])
|
2015-06-05 13:58:00 +00:00
|
|
|
{
|
2017-04-15 23:20:43 +00:00
|
|
|
static char base64[WG_KEY_LEN_BASE64];
|
2017-09-22 02:04:00 +00:00
|
|
|
if (key_is_zero(key))
|
2015-06-05 13:58:00 +00:00
|
|
|
return "(none)";
|
2017-04-15 23:20:43 +00:00
|
|
|
key_to_base64(base64, key);
|
|
|
|
return base64;
|
2015-06-05 13:58:00 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 23:20:43 +00:00
|
|
|
static char *masked_key(const uint8_t masked_key[static WG_KEY_LEN])
|
2016-07-28 15:18:17 +00:00
|
|
|
{
|
|
|
|
const char *var = getenv("WG_HIDE_KEYS");
|
|
|
|
if (var && !strcmp(var, "never"))
|
|
|
|
return key(masked_key);
|
|
|
|
return "(hidden)";
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:22:09 +00:00
|
|
|
static char *ip(const struct wgallowedip *ip)
|
2015-06-05 13:58:00 +00:00
|
|
|
{
|
|
|
|
static char buf[INET6_ADDRSTRLEN + 1];
|
|
|
|
memset(buf, 0, INET6_ADDRSTRLEN + 1);
|
|
|
|
if (ip->family == AF_INET)
|
|
|
|
inet_ntop(AF_INET, &ip->ip4, buf, INET6_ADDRSTRLEN);
|
|
|
|
else if (ip->family == AF_INET6)
|
|
|
|
inet_ntop(AF_INET6, &ip->ip6, buf, INET6_ADDRSTRLEN);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:52:06 +00:00
|
|
|
static char *endpoint(const struct sockaddr *addr)
|
2015-06-05 13:58:00 +00:00
|
|
|
{
|
|
|
|
char host[4096 + 1];
|
|
|
|
char service[512 + 1];
|
|
|
|
static char buf[sizeof(host) + sizeof(service) + 4];
|
|
|
|
int ret;
|
|
|
|
socklen_t addr_len = 0;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2017-01-10 04:52:06 +00:00
|
|
|
if (addr->sa_family == AF_INET)
|
2015-06-05 13:58:00 +00:00
|
|
|
addr_len = sizeof(struct sockaddr_in);
|
2017-01-10 04:52:06 +00:00
|
|
|
else if (addr->sa_family == AF_INET6)
|
2015-06-05 13:58:00 +00:00
|
|
|
addr_len = sizeof(struct sockaddr_in6);
|
|
|
|
|
2017-01-10 04:52:06 +00:00
|
|
|
ret = getnameinfo(addr, addr_len, host, sizeof(host), service, sizeof(service), NI_DGRAM | NI_NUMERICSERV | NI_NUMERICHOST);
|
2015-06-05 13:58:00 +00:00
|
|
|
if (ret)
|
|
|
|
strncpy(buf, gai_strerror(ret), sizeof(buf) - 1);
|
|
|
|
else
|
2017-01-10 04:52:06 +00:00
|
|
|
snprintf(buf, sizeof(buf) - 1, (addr->sa_family == AF_INET6 && strchr(host, ':')) ? "[%s]:%s" : "%s:%s", host, service);
|
2015-06-05 13:58:00 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-07-08 00:30:03 +00:00
|
|
|
static size_t pretty_time(char *buf, const size_t len, unsigned long long left)
|
2015-06-05 13:58:00 +00:00
|
|
|
{
|
|
|
|
size_t offset = 0;
|
2016-07-08 00:30:03 +00:00
|
|
|
unsigned long long years, days, hours, minutes, seconds;
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
years = left / (365 * 24 * 60 * 60);
|
|
|
|
left = left % (365 * 24 * 60 * 60);
|
|
|
|
days = left / (24 * 60 * 60);
|
|
|
|
left = left % (24 * 60 * 60);
|
|
|
|
hours = left / (60 * 60);
|
|
|
|
left = left % (60 * 60);
|
|
|
|
minutes = left / 60;
|
|
|
|
seconds = left % 60;
|
|
|
|
|
|
|
|
if (years)
|
2016-11-29 12:38:59 +00:00
|
|
|
offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "year%s" TERMINAL_RESET, offset ? ", " : "", years, years == 1 ? "" : "s");
|
2015-06-05 13:58:00 +00:00
|
|
|
if (days)
|
2016-11-29 12:38:59 +00:00
|
|
|
offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "day%s" TERMINAL_RESET, offset ? ", " : "", days, days == 1 ? "" : "s");
|
2015-06-05 13:58:00 +00:00
|
|
|
if (hours)
|
2016-11-29 12:38:59 +00:00
|
|
|
offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "hour%s" TERMINAL_RESET, offset ? ", " : "", hours, hours == 1 ? "" : "s");
|
2015-06-05 13:58:00 +00:00
|
|
|
if (minutes)
|
2016-11-29 12:38:59 +00:00
|
|
|
offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "minute%s" TERMINAL_RESET, offset ? ", " : "", minutes, minutes == 1 ? "" : "s");
|
2015-06-05 13:58:00 +00:00
|
|
|
if (seconds)
|
2016-11-29 12:38:59 +00:00
|
|
|
offset += snprintf(buf + offset, len - offset - 1, "%s%llu " TERMINAL_FG_CYAN "second%s" TERMINAL_RESET, offset ? ", " : "", seconds, seconds == 1 ? "" : "s");
|
2016-07-08 00:30:03 +00:00
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *ago(const struct timeval *t)
|
|
|
|
{
|
|
|
|
static char buf[1024];
|
|
|
|
size_t offset;
|
2016-11-29 12:38:59 +00:00
|
|
|
time_t now = time(NULL);
|
|
|
|
|
|
|
|
if (now == t->tv_sec)
|
|
|
|
strncpy(buf, "Now", sizeof(buf) - 1);
|
|
|
|
else if (now < t->tv_sec)
|
|
|
|
strncpy(buf, "(" TERMINAL_FG_RED "System clock wound backward; connection problems may ensue." TERMINAL_RESET ")", sizeof(buf) - 1);
|
|
|
|
else {
|
|
|
|
offset = pretty_time(buf, sizeof(buf), now - t->tv_sec);
|
|
|
|
strncpy(buf + offset, " ago", sizeof(buf) - offset - 1);
|
|
|
|
}
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-07-08 00:30:03 +00:00
|
|
|
static char *every(uint16_t seconds)
|
|
|
|
{
|
|
|
|
static char buf[1024] = "every ";
|
2016-11-29 12:38:59 +00:00
|
|
|
pretty_time(buf + strlen("every "), sizeof(buf) - strlen("every ") - 1, seconds);
|
2016-07-08 00:30:03 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-06-05 13:58:00 +00:00
|
|
|
static char *bytes(uint64_t b)
|
|
|
|
{
|
|
|
|
static char buf[1024];
|
|
|
|
|
|
|
|
if (b < 1024ULL)
|
2016-11-29 12:38:59 +00:00
|
|
|
snprintf(buf, sizeof(buf) - 1, "%u " TERMINAL_FG_CYAN "B" TERMINAL_RESET, (unsigned)b);
|
2015-06-05 13:58:00 +00:00
|
|
|
else if (b < 1024ULL * 1024ULL)
|
2016-11-29 12:38:59 +00:00
|
|
|
snprintf(buf, sizeof(buf) - 1, "%.2f " TERMINAL_FG_CYAN "KiB" TERMINAL_RESET, (double)b / 1024);
|
2015-06-05 13:58:00 +00:00
|
|
|
else if (b < 1024ULL * 1024ULL * 1024ULL)
|
2016-11-29 12:38:59 +00:00
|
|
|
snprintf(buf, sizeof(buf) - 1, "%.2f " TERMINAL_FG_CYAN "MiB" TERMINAL_RESET, (double)b / (1024 * 1024));
|
2015-06-05 13:58:00 +00:00
|
|
|
else if (b < 1024ULL * 1024ULL * 1024ULL * 1024ULL)
|
2016-11-29 12:38:59 +00:00
|
|
|
snprintf(buf, sizeof(buf) - 1, "%.2f " TERMINAL_FG_CYAN "GiB" TERMINAL_RESET, (double)b / (1024 * 1024 * 1024));
|
2015-06-05 13:58:00 +00:00
|
|
|
else
|
2016-11-29 12:38:59 +00:00
|
|
|
snprintf(buf, sizeof(buf) - 1, "%.2f " TERMINAL_FG_CYAN "TiB" TERMINAL_RESET, (double)b / (1024 * 1024 * 1024) / 1024);
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *COMMAND_NAME = NULL;
|
|
|
|
static void show_usage(void)
|
|
|
|
{
|
2017-04-27 09:10:50 +00:00
|
|
|
fprintf(stderr, "Usage: %s %s { <interface> | all | interfaces } [public-key | private-key | listen-port | fwmark | peers | preshared-keys | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive | dump]\n", PROG_NAME, COMMAND_NAME);
|
2015-06-05 13:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pretty_print(struct wgdevice *device)
|
|
|
|
{
|
|
|
|
struct wgpeer *peer;
|
2017-09-25 02:22:09 +00:00
|
|
|
struct wgallowedip *allowedip;
|
2015-06-05 13:58:00 +00:00
|
|
|
|
|
|
|
terminal_printf(TERMINAL_RESET);
|
2017-09-25 02:22:09 +00:00
|
|
|
terminal_printf(TERMINAL_FG_GREEN TERMINAL_BOLD "interface" TERMINAL_RESET ": " TERMINAL_FG_GREEN "%s" TERMINAL_RESET "\n", device->name);
|
2017-09-22 02:04:00 +00:00
|
|
|
if (!key_is_zero(device->public_key))
|
2015-06-05 13:58:00 +00:00
|
|
|
terminal_printf(" " TERMINAL_BOLD "public key" TERMINAL_RESET ": %s\n", key(device->public_key));
|
2017-09-22 02:04:00 +00:00
|
|
|
if (!key_is_zero(device->private_key))
|
2016-07-28 15:18:17 +00:00
|
|
|
terminal_printf(" " TERMINAL_BOLD "private key" TERMINAL_RESET ": %s\n", masked_key(device->private_key));
|
2017-09-25 02:22:09 +00:00
|
|
|
if (device->listen_port)
|
|
|
|
terminal_printf(" " TERMINAL_BOLD "listening port" TERMINAL_RESET ": %u\n", device->listen_port);
|
2017-01-24 16:43:35 +00:00
|
|
|
if (device->fwmark)
|
|
|
|
terminal_printf(" " TERMINAL_BOLD "fwmark" TERMINAL_RESET ": 0x%x\n", device->fwmark);
|
2017-09-25 02:22:09 +00:00
|
|
|
if (device->first_peer) {
|
2015-06-05 13:58:00 +00:00
|
|
|
sort_peers(device);
|
|
|
|
terminal_printf("\n");
|
|
|
|
}
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer) {
|
2015-06-05 13:58:00 +00:00
|
|
|
terminal_printf(TERMINAL_FG_YELLOW TERMINAL_BOLD "peer" TERMINAL_RESET ": " TERMINAL_FG_YELLOW "%s" TERMINAL_RESET "\n", key(peer->public_key));
|
2017-09-22 02:04:00 +00:00
|
|
|
if (!key_is_zero(peer->preshared_key))
|
2017-04-27 09:10:50 +00:00
|
|
|
terminal_printf(" " TERMINAL_BOLD "preshared key" TERMINAL_RESET ": %s\n", masked_key(peer->preshared_key));
|
2017-01-10 04:52:06 +00:00
|
|
|
if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6)
|
|
|
|
terminal_printf(" " TERMINAL_BOLD "endpoint" TERMINAL_RESET ": %s\n", endpoint(&peer->endpoint.addr));
|
2015-06-05 13:58:00 +00:00
|
|
|
terminal_printf(" " TERMINAL_BOLD "allowed ips" TERMINAL_RESET ": ");
|
2017-09-25 02:22:09 +00:00
|
|
|
if (peer->first_allowedip) {
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgallowedip(peer, allowedip)
|
2017-09-25 02:22:09 +00:00
|
|
|
terminal_printf("%s" TERMINAL_FG_CYAN "/" TERMINAL_RESET "%u%s", ip(allowedip), allowedip->cidr, allowedip->next_allowedip ? ", " : "\n");
|
2015-06-05 13:58:00 +00:00
|
|
|
} else
|
|
|
|
terminal_printf("(none)\n");
|
|
|
|
if (peer->last_handshake_time.tv_sec)
|
|
|
|
terminal_printf(" " TERMINAL_BOLD "latest handshake" TERMINAL_RESET ": %s\n", ago(&peer->last_handshake_time));
|
|
|
|
if (peer->rx_bytes || peer->tx_bytes) {
|
2016-12-23 19:25:55 +00:00
|
|
|
terminal_printf(" " TERMINAL_BOLD "transfer" TERMINAL_RESET ": ");
|
2015-06-05 13:58:00 +00:00
|
|
|
terminal_printf("%s received, ", bytes(peer->rx_bytes));
|
|
|
|
terminal_printf("%s sent\n", bytes(peer->tx_bytes));
|
|
|
|
}
|
2016-07-08 00:30:03 +00:00
|
|
|
if (peer->persistent_keepalive_interval)
|
|
|
|
terminal_printf(" " TERMINAL_BOLD "persistent keepalive" TERMINAL_RESET ": %s\n", every(peer->persistent_keepalive_interval));
|
2017-09-25 02:22:09 +00:00
|
|
|
if (peer->next_peer)
|
2015-06-05 13:58:00 +00:00
|
|
|
terminal_printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 20:53:55 +00:00
|
|
|
static void dump_print(struct wgdevice *device, bool with_interface)
|
|
|
|
{
|
|
|
|
struct wgpeer *peer;
|
2017-09-25 02:22:09 +00:00
|
|
|
struct wgallowedip *allowedip;
|
2017-02-22 20:53:55 +00:00
|
|
|
|
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2017-02-22 20:53:55 +00:00
|
|
|
printf("%s\t", key(device->private_key));
|
|
|
|
printf("%s\t", key(device->public_key));
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%u\t", device->listen_port);
|
2017-02-22 20:53:55 +00:00
|
|
|
if (device->fwmark)
|
|
|
|
printf("0x%x\n", device->fwmark);
|
|
|
|
else
|
|
|
|
printf("off\n");
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer) {
|
2017-02-22 20:53:55 +00:00
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2017-02-22 20:53:55 +00:00
|
|
|
printf("%s\t", key(peer->public_key));
|
2017-04-27 09:10:50 +00:00
|
|
|
printf("%s\t", key(peer->preshared_key));
|
2017-02-22 20:53:55 +00:00
|
|
|
if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6)
|
|
|
|
printf("%s\t", endpoint(&peer->endpoint.addr));
|
|
|
|
else
|
|
|
|
printf("(none)\t");
|
2017-09-25 02:22:09 +00:00
|
|
|
if (peer->first_allowedip) {
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgallowedip(peer, allowedip)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s/%u%c", ip(allowedip), allowedip->cidr, allowedip->next_allowedip ? ',' : '\t');
|
2017-02-22 20:53:55 +00:00
|
|
|
} else
|
|
|
|
printf("(none)\t");
|
|
|
|
printf("%llu\t", (unsigned long long)peer->last_handshake_time.tv_sec);
|
|
|
|
printf("%" PRIu64 "\t%" PRIu64 "\t", (uint64_t)peer->rx_bytes, (uint64_t)peer->tx_bytes);
|
|
|
|
if (peer->persistent_keepalive_interval)
|
|
|
|
printf("%u\n", peer->persistent_keepalive_interval);
|
|
|
|
else
|
|
|
|
printf("off\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 13:58:00 +00:00
|
|
|
static bool ugly_print(struct wgdevice *device, const char *param, bool with_interface)
|
|
|
|
{
|
|
|
|
struct wgpeer *peer;
|
2017-09-25 02:22:09 +00:00
|
|
|
struct wgallowedip *allowedip;
|
2015-06-05 13:58:00 +00:00
|
|
|
if (!strcmp(param, "public-key")) {
|
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2015-06-05 13:58:00 +00:00
|
|
|
printf("%s\n", key(device->public_key));
|
|
|
|
} else if (!strcmp(param, "private-key")) {
|
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2015-06-05 13:58:00 +00:00
|
|
|
printf("%s\n", key(device->private_key));
|
|
|
|
} else if (!strcmp(param, "listen-port")) {
|
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
|
|
|
printf("%u\n", device->listen_port);
|
2017-01-24 16:43:35 +00:00
|
|
|
} else if (!strcmp(param, "fwmark")) {
|
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2017-02-22 20:45:03 +00:00
|
|
|
if (device->fwmark)
|
|
|
|
printf("0x%x\n", device->fwmark);
|
|
|
|
else
|
|
|
|
printf("off\n");
|
2015-06-05 13:58:00 +00:00
|
|
|
} else if (!strcmp(param, "endpoints")) {
|
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer) {
|
2015-06-05 13:58:00 +00:00
|
|
|
printf("%s\t", key(peer->public_key));
|
2017-01-10 04:52:06 +00:00
|
|
|
if (peer->endpoint.addr.sa_family == AF_INET || peer->endpoint.addr.sa_family == AF_INET6)
|
|
|
|
printf("%s\n", endpoint(&peer->endpoint.addr));
|
2015-06-05 13:58:00 +00:00
|
|
|
else
|
|
|
|
printf("(none)\n");
|
|
|
|
}
|
|
|
|
} else if (!strcmp(param, "allowed-ips")) {
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer) {
|
2015-06-05 13:58:00 +00:00
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2015-06-05 13:58:00 +00:00
|
|
|
printf("%s\t", key(peer->public_key));
|
2017-09-25 02:22:09 +00:00
|
|
|
if (peer->first_allowedip) {
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgallowedip(peer, allowedip)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s/%u%c", ip(allowedip), allowedip->cidr, allowedip->next_allowedip ? ' ' : '\n');
|
2015-06-05 13:58:00 +00:00
|
|
|
} else
|
|
|
|
printf("(none)\n");
|
|
|
|
}
|
|
|
|
} else if (!strcmp(param, "latest-handshakes")) {
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer) {
|
2015-06-05 13:58:00 +00:00
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2015-06-05 13:58:00 +00:00
|
|
|
printf("%s\t%llu\n", key(peer->public_key), (unsigned long long)peer->last_handshake_time.tv_sec);
|
|
|
|
}
|
2016-12-23 19:25:55 +00:00
|
|
|
} else if (!strcmp(param, "transfer")) {
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer) {
|
2015-06-05 13:58:00 +00:00
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2015-06-05 13:58:00 +00:00
|
|
|
printf("%s\t%" PRIu64 "\t%" PRIu64 "\n", key(peer->public_key), (uint64_t)peer->rx_bytes, (uint64_t)peer->tx_bytes);
|
|
|
|
}
|
2016-07-08 00:30:03 +00:00
|
|
|
} else if (!strcmp(param, "persistent-keepalive")) {
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer) {
|
2016-07-08 00:30:03 +00:00
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2016-07-08 00:30:03 +00:00
|
|
|
if (peer->persistent_keepalive_interval)
|
|
|
|
printf("%s\t%u\n", key(peer->public_key), peer->persistent_keepalive_interval);
|
|
|
|
else
|
|
|
|
printf("%s\toff\n", key(peer->public_key));
|
|
|
|
}
|
2017-04-27 09:10:50 +00:00
|
|
|
} else if (!strcmp(param, "preshared-keys")) {
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer) {
|
2017-04-27 09:10:50 +00:00
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2017-04-27 09:10:50 +00:00
|
|
|
printf("%s\t", key(peer->public_key));
|
|
|
|
printf("%s\n", key(peer->preshared_key));
|
|
|
|
}
|
2015-06-05 13:58:00 +00:00
|
|
|
} else if (!strcmp(param, "peers")) {
|
2017-10-25 15:13:46 +00:00
|
|
|
for_each_wgpeer(device, peer) {
|
2015-06-05 13:58:00 +00:00
|
|
|
if (with_interface)
|
2017-09-25 02:22:09 +00:00
|
|
|
printf("%s\t", device->name);
|
2015-06-05 13:58:00 +00:00
|
|
|
printf("%s\n", key(peer->public_key));
|
|
|
|
}
|
2017-02-22 20:53:55 +00:00
|
|
|
} else if (!strcmp(param, "dump"))
|
|
|
|
dump_print(device, with_interface);
|
|
|
|
else {
|
2017-10-15 20:52:38 +00:00
|
|
|
fprintf(stderr, "Invalid parameter: `%s'\n", param);
|
2015-06-05 13:58:00 +00:00
|
|
|
show_usage();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int show_main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
COMMAND_NAME = argv[0];
|
|
|
|
|
|
|
|
if (argc > 3) {
|
|
|
|
show_usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 1 || !strcmp(argv[1], "all")) {
|
2016-07-20 19:24:27 +00:00
|
|
|
char *interfaces = ipc_list_devices(), *interface;
|
2015-06-05 13:58:00 +00:00
|
|
|
if (!interfaces) {
|
|
|
|
perror("Unable to get devices");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
interface = interfaces;
|
|
|
|
for (size_t len = 0; (len = strlen(interface)); interface += len + 1) {
|
|
|
|
struct wgdevice *device = NULL;
|
2016-07-20 19:24:27 +00:00
|
|
|
if (ipc_get_device(&device, interface) < 0) {
|
2015-06-05 13:58:00 +00:00
|
|
|
perror("Unable to get device");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (argc == 3) {
|
|
|
|
if (!ugly_print(device, argv[2], true)) {
|
|
|
|
ret = 1;
|
2017-09-25 02:22:09 +00:00
|
|
|
free_wgdevice(device);
|
2015-06-05 13:58:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pretty_print(device);
|
|
|
|
if (strlen(interface + len + 1))
|
|
|
|
printf("\n");
|
|
|
|
}
|
2017-09-25 02:22:09 +00:00
|
|
|
free_wgdevice(device);
|
2015-06-05 13:58:00 +00:00
|
|
|
}
|
|
|
|
free(interfaces);
|
|
|
|
} else if (!strcmp(argv[1], "interfaces")) {
|
|
|
|
char *interfaces, *interface;
|
|
|
|
if (argc > 2) {
|
|
|
|
show_usage();
|
|
|
|
return 1;
|
|
|
|
}
|
2016-07-20 19:24:27 +00:00
|
|
|
interfaces = ipc_list_devices();
|
2015-06-05 13:58:00 +00:00
|
|
|
if (!interfaces) {
|
|
|
|
perror("Unable to get devices");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
interface = interfaces;
|
|
|
|
for (size_t len = 0; (len = strlen(interface)); interface += len + 1)
|
|
|
|
printf("%s%c", interface, strlen(interface + len + 1) ? ' ' : '\n');
|
|
|
|
free(interfaces);
|
|
|
|
} else if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !strcmp(argv[1], "help")))
|
|
|
|
show_usage();
|
|
|
|
else {
|
|
|
|
struct wgdevice *device = NULL;
|
2016-07-20 19:24:27 +00:00
|
|
|
if (ipc_get_device(&device, argv[1]) < 0) {
|
2015-06-05 13:58:00 +00:00
|
|
|
perror("Unable to get device");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (argc == 3) {
|
|
|
|
if (!ugly_print(device, argv[2], false))
|
|
|
|
ret = 1;
|
|
|
|
} else
|
|
|
|
pretty_print(device);
|
2017-09-25 02:22:09 +00:00
|
|
|
free_wgdevice(device);
|
2015-06-05 13:58:00 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|