compiling version of new fields

This commit is contained in:
Mark Puha 2023-09-09 07:37:06 +02:00
parent 13f4ac4cb7
commit f632775430
8 changed files with 333 additions and 5 deletions

View file

@ -38,11 +38,12 @@ endif
PLATFORM ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
CFLAGS ?= -O3
ifneq ($(wildcard uapi/$(PLATFORM)/.),)
CFLAGS += -idirafter uapi/$(PLATFORM)
CFLAGS += -I uapi/$(PLATFORM)
endif
CFLAGS += -std=gnu99 -D_GNU_SOURCE
CFLAGS += -Wall -Wextra
CFLAGS += -Wall -Wextra
CFLAGS += -MMD -MP
CFLAGS += -DRUNSTATEDIR="\"$(RUNSTATEDIR)\""
ifeq ($(DEBUG),yes)

View file

@ -410,6 +410,43 @@ err:
return false;
}
static inline bool parse_uint16(uint16_t *device_value, const char *name, const char *value) {
if (!strlen(value)) {
fprintf(stderr, "Unable to parse empty string\n");
return false;
}
char *end;
uint32_t ret;
ret = strtoul(value, &end, 10);
if (*end || ret > UINT16_MAX) {
fprintf(stderr, "Unable to parse %s: `%s'\n", name, value);
exit(1);
}
*device_value = (uint16_t)ret;
return true;
}
static inline bool parse_uint32(uint32_t *device_value, const char *name, const char *value) {
if (!strlen(value)) {
fprintf(stderr, "Unable to parse empty string\n");
return false;
}
char *end;
uint64_t ret;
ret = strtoul(value, &end, 10);
if (*end || ret > UINT32_MAX) {
fprintf(stderr, "Unable to parse %s: `%s'\n", name, value);
exit(1);
}
*device_value = (uint32_t)ret;
return true;
}
static bool process_line(struct config_ctx *ctx, const char *line)
{
const char *value;
@ -450,6 +487,42 @@ static bool process_line(struct config_ctx *ctx, const char *line)
ret = parse_key(ctx->device->private_key, value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_PRIVATE_KEY;
} else if (key_match("Jc")) {
ret = parse_uint16(&ctx->device->junk_packet_count, "Jc", value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_JC;
} else if (key_match("Jmin")) {
ret = parse_uint16(&ctx->device->junk_packet_min_size, "Jmin", value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_JMIN;
} else if (key_match("Jmax")) {
ret = parse_uint16(&ctx->device->junk_packet_max_size, "Jmax", value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_JMAX;
} else if (key_match("S1")) {
ret = parse_uint16(&ctx->device->init_packet_junk_size, "S1", value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_S1;
} else if (key_match("S2")) {
ret = parse_uint16(&ctx->device->response_packet_junk_size, "S2", value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_S2;
} else if (key_match("H1")) {
ret = parse_uint32(&ctx->device->init_packet_magic_header, "H1", value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_H1;
} else if (key_match("H2")) {
ret = parse_uint32(&ctx->device->response_packet_magic_header, "H2", value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_H2;
} else if (key_match("H3")) {
ret = parse_uint32(&ctx->device->underload_packet_magic_header, "H3", value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_H3;
} else if (key_match("H4")) {
ret = parse_uint32(&ctx->device->transport_packet_magic_header, "H4", value);
if (ret)
ctx->device->flags |= WGDEVICE_HAS_H4;
} else
goto error;
} else if (ctx->is_peer_section) {
@ -523,7 +596,7 @@ bool config_read_init(struct config_ctx *ctx, bool append)
return false;
}
if (!append)
ctx->device->flags |= WGDEVICE_REPLACE_PEERS | WGDEVICE_HAS_PRIVATE_KEY | WGDEVICE_HAS_FWMARK | WGDEVICE_HAS_LISTEN_PORT;
ctx->device->flags |= WGDEVICE_REPLACE_PEERS | WGDEVICE_HAS_PRIVATE_KEY | WGDEVICE_HAS_FWMARK | WGDEVICE_HAS_LISTEN_PORT | WGDEVICE_HAS_JC | WGDEVICE_HAS_JMIN | WGDEVICE_HAS_JMAX | WGDEVICE_HAS_S1 | WGDEVICE_HAS_S2 | WGDEVICE_HAS_H1 | WGDEVICE_HAS_H2 | WGDEVICE_HAS_H3 | WGDEVICE_HAS_H4;
return true;
}
@ -588,6 +661,52 @@ struct wgdevice *config_read_cmd(const char *argv[], int argc)
device->flags |= WGDEVICE_HAS_PRIVATE_KEY;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "jc") && argc >= 2 && !peer) {
if (!parse_uint16(&device->junk_packet_count, "jc", argv[1]))
goto error;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "jmin") && argc >= 2 && !peer) {
if (!parse_uint16(&device->junk_packet_min_size, "jmin", argv[1]))
goto error;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "jmax") && argc >= 2 && !peer) {
if (!parse_uint16(&device->junk_packet_max_size, "jmax", argv[1]))
goto error;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "s1") && argc >= 2 && !peer) {
if (!parse_uint16(&device->init_packet_junk_size, "s1", argv[1]))
goto error;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "s2") && argc >= 2 && !peer) {
if (!parse_uint16(&device->response_packet_junk_size, "s2", argv[1]))
goto error;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "h1") && argc >= 2 && !peer) {
if (!parse_uint32(&device->init_packet_magic_header, "h1", argv[1]))
goto error;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "h2") && argc >= 2 && !peer) {
if (!parse_uint32(&device->response_packet_magic_header, "h2", argv[1]))
goto error;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "h3") && argc >= 2 && !peer) {
if (!parse_uint32(&device->underload_packet_magic_header, "h3", argv[1]))
goto error;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "h4") && argc >= 2 && !peer) {
if (!parse_uint32(&device->transport_packet_magic_header, "h4", argv[1]))
goto error;
argv += 2;
argc -= 2;
} else if (!strcmp(argv[0], "peer") && argc >= 2) {
struct wgpeer *new_peer = calloc(1, sizeof(*new_peer));

View file

@ -71,7 +71,16 @@ enum {
WGDEVICE_HAS_PRIVATE_KEY = 1U << 1,
WGDEVICE_HAS_PUBLIC_KEY = 1U << 2,
WGDEVICE_HAS_LISTEN_PORT = 1U << 3,
WGDEVICE_HAS_FWMARK = 1U << 4
WGDEVICE_HAS_FWMARK = 1U << 4,
WGDEVICE_HAS_JC = 1U << 5,
WGDEVICE_HAS_JMIN = 1U << 6,
WGDEVICE_HAS_JMAX = 1U << 7,
WGDEVICE_HAS_S1 = 1U << 8,
WGDEVICE_HAS_S2 = 1U << 9,
WGDEVICE_HAS_H1 = 1U << 10,
WGDEVICE_HAS_H2 = 1U << 11,
WGDEVICE_HAS_H3 = 1U << 12,
WGDEVICE_HAS_H4 = 1U << 13
};
struct wgdevice {
@ -85,6 +94,15 @@ struct wgdevice {
uint32_t fwmark;
uint16_t listen_port;
uint16_t junk_packet_count;
uint16_t junk_packet_min_size;
uint16_t junk_packet_max_size;
uint16_t init_packet_junk_size;
uint16_t response_packet_junk_size;
uint32_t init_packet_magic_header;
uint32_t response_packet_magic_header;
uint32_t underload_packet_magic_header;
uint32_t transport_packet_magic_header;
struct wgpeer *first_peer, *last_peer;
};

View file

@ -163,6 +163,24 @@ again:
mnl_attr_put(nlh, WGDEVICE_A_PRIVATE_KEY, sizeof(dev->private_key), dev->private_key);
if (dev->flags & WGDEVICE_HAS_LISTEN_PORT)
mnl_attr_put_u16(nlh, WGDEVICE_A_LISTEN_PORT, dev->listen_port);
if (dev->flags & WGDEVICE_HAS_JC)
mnl_attr_put_u16(nlh, WGDEVICE_A_JC, dev->junk_packet_count);
if (dev->flags & WGDEVICE_HAS_JMIN)
mnl_attr_put_u16(nlh, WGDEVICE_A_JMIN, dev->junk_packet_min_size);
if (dev->flags & WGDEVICE_HAS_JMAX)
mnl_attr_put_u16(nlh, WGDEVICE_A_JMAX, dev->junk_packet_max_size);
if (dev->flags & WGDEVICE_HAS_S1)
mnl_attr_put_u16(nlh, WGDEVICE_A_S1, dev->init_packet_junk_size);
if (dev->flags & WGDEVICE_HAS_S2)
mnl_attr_put_u16(nlh, WGDEVICE_A_S2, dev->response_packet_junk_size);
if (dev->flags & WGDEVICE_HAS_H1)
mnl_attr_put_u32(nlh, WGDEVICE_A_H1, dev->init_packet_magic_header);
if (dev->flags & WGDEVICE_HAS_H2)
mnl_attr_put_u32(nlh, WGDEVICE_A_H2, dev->response_packet_magic_header);
if (dev->flags & WGDEVICE_HAS_H3)
mnl_attr_put_u32(nlh, WGDEVICE_A_H3, dev->underload_packet_magic_header);
if (dev->flags & WGDEVICE_HAS_H4)
mnl_attr_put_u32(nlh, WGDEVICE_A_H4, dev->transport_packet_magic_header);
if (dev->flags & WGDEVICE_HAS_FWMARK)
mnl_attr_put_u32(nlh, WGDEVICE_A_FWMARK, dev->fwmark);
if (dev->flags & WGDEVICE_REPLACE_PEERS)
@ -441,6 +459,42 @@ static int parse_device(const struct nlattr *attr, void *data)
break;
case WGDEVICE_A_PEERS:
return mnl_attr_parse_nested(attr, parse_peers, device);
case WGDEVICE_HAS_JC:
if (!mnl_attr_validate(attr, MNL_TYPE_U16))
device->junk_packet_count = mnl_attr_get_u16(attr);
break;
case WGDEVICE_HAS_JMIN:
if (!mnl_attr_validate(attr, MNL_TYPE_U16))
device->junk_packet_min_size = mnl_attr_get_u16(attr);
break;
case WGDEVICE_HAS_JMAX:
if (!mnl_attr_validate(attr, MNL_TYPE_U16))
device->junk_packet_max_size = mnl_attr_get_u16(attr);
break;
case WGDEVICE_HAS_S1:
if (!mnl_attr_validate(attr, MNL_TYPE_U16))
device->init_packet_junk_size = mnl_attr_get_u16(attr);
break;
case WGDEVICE_HAS_S2:
if (!mnl_attr_validate(attr, MNL_TYPE_U16))
device->response_packet_junk_size = mnl_attr_get_u16(attr);
break;
case WGDEVICE_HAS_H1:
if (!mnl_attr_validate(attr, MNL_TYPE_U32))
device->init_packet_magic_header = mnl_attr_get_u32(attr);
break;
case WGDEVICE_HAS_H2:
if (!mnl_attr_validate(attr, MNL_TYPE_U32))
device->response_packet_magic_header = mnl_attr_get_u32(attr);
break;
case WGDEVICE_HAS_H3:
if (!mnl_attr_validate(attr, MNL_TYPE_U32))
device->underload_packet_magic_header = mnl_attr_get_u32(attr);
break;
case WGDEVICE_HAS_H4:
if (!mnl_attr_validate(attr, MNL_TYPE_U32))
device->transport_packet_magic_header = mnl_attr_get_u32(attr);
break;
}
return MNL_CB_OK;

View file

@ -51,6 +51,24 @@ static int userspace_set_device(struct wgdevice *dev)
fprintf(f, "fwmark=%u\n", dev->fwmark);
if (dev->flags & WGDEVICE_REPLACE_PEERS)
fprintf(f, "replace_peers=true\n");
if (dev->flags & WGDEVICE_HAS_JC)
fprintf(f, "jc=%u\n", dev->junk_packet_count);
if (dev->flags & WGDEVICE_HAS_JMIN)
fprintf(f, "jmin=%u\n", dev->junk_packet_min_size);
if (dev->flags & WGDEVICE_HAS_JMAX)
fprintf(f, "jmax=%u\n", dev->junk_packet_max_size);
if (dev->flags & WGDEVICE_HAS_S1)
fprintf(f, "s1=%u\n", dev->init_packet_junk_size);
if (dev->flags & WGDEVICE_HAS_S2)
fprintf(f, "s2=%u\n", dev->response_packet_junk_size);
if (dev->flags & WGDEVICE_HAS_H1)
fprintf(f, "h1=%u\n", dev->init_packet_magic_header);
if (dev->flags & WGDEVICE_HAS_H2)
fprintf(f, "h2=%u\n", dev->response_packet_magic_header);
if (dev->flags & WGDEVICE_HAS_H3)
fprintf(f, "h3=%u\n", dev->underload_packet_magic_header);
if (dev->flags & WGDEVICE_HAS_H4)
fprintf(f, "h4=%u\n", dev->transport_packet_magic_header);
for_each_wgpeer(dev, peer) {
key_to_hex(hex, peer->public_key);
@ -183,6 +201,33 @@ static int userspace_get_device(struct wgdevice **out, const char *iface)
} else if (!peer && !strcmp(key, "fwmark")) {
dev->fwmark = NUM(0xffffffffU);
dev->flags |= WGDEVICE_HAS_FWMARK;
} else if(!peer && !strcmp(key, "jc")) {
dev->junk_packet_count = NUM(0xffffU);
dev->flags |= WGDEVICE_HAS_JC;
} else if(!peer && !strcmp(key, "jmin")) {
dev->junk_packet_min_size = NUM(0xffffU);
dev->flags |= WGDEVICE_HAS_JMIN;
} else if(!peer && !strcmp(key, "jmax")) {
dev->junk_packet_max_size = NUM(0xffffU);
dev->flags |= WGDEVICE_HAS_JMAX;
} else if(!peer && !strcmp(key, "s1")) {
dev->init_packet_junk_size = NUM(0xffffU);
dev->flags |= WGDEVICE_HAS_S1;
} else if(!peer && !strcmp(key, "s2")) {
dev->response_packet_junk_size = NUM(0xffffU);
dev->flags |= WGDEVICE_HAS_S2;
} else if(!peer && !strcmp(key, "h1")) {
dev->init_packet_magic_header = NUM(0xffffffffU);
dev->flags |= WGDEVICE_HAS_H1;
} else if(!peer && !strcmp(key, "h2")) {
dev->response_packet_magic_header = NUM(0xffffffffU);
dev->flags |= WGDEVICE_HAS_H2;
} else if(!peer && !strcmp(key, "h3")) {
dev->underload_packet_magic_header = NUM(0xffffffffU);
dev->flags |= WGDEVICE_HAS_H3;
} else if(!peer && !strcmp(key, "h4")) {
dev->transport_packet_magic_header = NUM(0xffffffffU);
dev->flags |= WGDEVICE_HAS_H4;
} else if (!strcmp(key, "public_key")) {
struct wgpeer *new_peer = calloc(1, sizeof(*new_peer));

View file

@ -220,6 +220,24 @@ static void pretty_print(struct wgdevice *device)
terminal_printf(" " TERMINAL_BOLD "listening port" TERMINAL_RESET ": %u\n", device->listen_port);
if (device->fwmark)
terminal_printf(" " TERMINAL_BOLD "fwmark" TERMINAL_RESET ": 0x%x\n", device->fwmark);
if (device->junk_packet_count)
terminal_printf(" " TERMINAL_BOLD "jc" TERMINAL_RESET ": %u\n", device->junk_packet_count);
if (device->junk_packet_min_size)
terminal_printf(" " TERMINAL_BOLD "jmin" TERMINAL_RESET ": %u\n", device->junk_packet_min_size);
if (device->junk_packet_max_size)
terminal_printf(" " TERMINAL_BOLD "jmax" TERMINAL_RESET ": %u\n", device->junk_packet_max_size);
if (device->init_packet_junk_size)
terminal_printf(" " TERMINAL_BOLD "s1" TERMINAL_RESET ": %u\n", device->init_packet_junk_size);
if (device->response_packet_junk_size)
terminal_printf(" " TERMINAL_BOLD "s2" TERMINAL_RESET ": %u\n", device->response_packet_junk_size);
if (device->init_packet_magic_header)
terminal_printf(" " TERMINAL_BOLD "h1" TERMINAL_RESET ": %u\n", device->init_packet_magic_header);
if (device->response_packet_magic_header)
terminal_printf(" " TERMINAL_BOLD "h2" TERMINAL_RESET ": %u\n", device->response_packet_magic_header);
if (device->underload_packet_magic_header)
terminal_printf(" " TERMINAL_BOLD "h3" TERMINAL_RESET ": %u\n", device->underload_packet_magic_header);
if (device->transport_packet_magic_header)
terminal_printf(" " TERMINAL_BOLD "h4" TERMINAL_RESET ": %u\n", device->transport_packet_magic_header);
if (device->first_peer) {
sort_peers(device);
terminal_printf("\n");
@ -260,6 +278,15 @@ static void dump_print(struct wgdevice *device, bool with_interface)
printf("%s\t", maybe_key(device->private_key, device->flags & WGDEVICE_HAS_PRIVATE_KEY));
printf("%s\t", maybe_key(device->public_key, device->flags & WGDEVICE_HAS_PUBLIC_KEY));
printf("%u\t", device->listen_port);
printf("%u\t", device->junk_packet_count);
printf("%u\t", device->junk_packet_min_size);
printf("%u\t", device->junk_packet_max_size);
printf("%u\t", device->init_packet_junk_size);
printf("%u\t", device->response_packet_junk_size);
printf("%u\t", device->init_packet_magic_header);
printf("%u\t", device->response_packet_magic_header);
printf("%u\t", device->underload_packet_magic_header);
printf("%u\t", device->transport_packet_magic_header);
if (device->fwmark)
printf("0x%x\n", device->fwmark);
else
@ -311,7 +338,43 @@ static bool ugly_print(struct wgdevice *device, const char *param, bool with_int
printf("0x%x\n", device->fwmark);
else
printf("off\n");
} else if (!strcmp(param, "endpoints")) {
} else if(!strcmp(param, "jc")) {
if (with_interface)
printf("%s\t", device->name);
printf("%u\n", device->junk_packet_count);
} else if(!strcmp(param, "jmin")) {
if (with_interface)
printf("%s\t", device->name);
printf("%u\n", device->junk_packet_min_size);
} else if(!strcmp(param, "jmax")) {
if (with_interface)
printf("%s\t", device->name);
printf("%u\n", device->junk_packet_max_size);
} else if(!strcmp(param, "s1")) {
if (with_interface)
printf("%s\t", device->name);
printf("%u\n", device->init_packet_junk_size);
} else if(!strcmp(param, "s2")) {
if (with_interface)
printf("%s\t", device->name);
printf("%u\n", device->response_packet_junk_size);
} else if(!strcmp(param, "h1")) {
if (with_interface)
printf("%s\t", device->name);
printf("%u\n", device->init_packet_magic_header);
} else if(!strcmp(param, "h2")) {
if (with_interface)
printf("%s\t", device->name);
printf("%u\n", device->response_packet_magic_header);
} else if(!strcmp(param, "h3")) {
if (with_interface)
printf("%s\t", device->name);
printf("%u\n", device->underload_packet_magic_header);
} else if(!strcmp(param, "h4")) {
if (with_interface)
printf("%s\t", device->name);
printf("%u\n", device->transport_packet_magic_header);
} else if (!strcmp(param, "endpoints")) {
for_each_wgpeer(device, peer) {
if (with_interface)
printf("%s\t", device->name);

View file

@ -46,6 +46,25 @@ int showconf_main(int argc, const char *argv[])
key_to_base64(base64, device->private_key);
printf("PrivateKey = %s\n", base64);
}
if (device->flags & WGDEVICE_HAS_JC)
printf("Jc = %u", device->junk_packet_count);
if (device->flags & WGDEVICE_HAS_JMIN)
printf("Jmin = %u", device->junk_packet_min_size);
if (device->flags & WGDEVICE_HAS_JMAX)
printf("Jmax = %u", device->junk_packet_max_size);
if (device->flags & WGDEVICE_HAS_S1)
printf("S1 = %u", device->init_packet_junk_size);
if (device->flags & WGDEVICE_HAS_S2)
printf("S2 = %u", device->response_packet_junk_size);
if (device->flags & WGDEVICE_HAS_H1)
printf("H1 = %u", device->init_packet_magic_header);
if (device->flags & WGDEVICE_HAS_H2)
printf("H2 = %u", device->response_packet_magic_header);
if (device->flags & WGDEVICE_HAS_H3)
printf("H3 = %u", device->underload_packet_magic_header);
if (device->flags & WGDEVICE_HAS_H4)
printf("H4 = %u", device->transport_packet_magic_header);
printf("\n");
for_each_wgpeer(device, peer) {
key_to_base64(base64, peer->public_key);

View file

@ -157,6 +157,15 @@ enum wgdevice_attribute {
WGDEVICE_A_LISTEN_PORT,
WGDEVICE_A_FWMARK,
WGDEVICE_A_PEERS,
WGDEVICE_A_JC,
WGDEVICE_A_JMIN,
WGDEVICE_A_JMAX,
WGDEVICE_A_S1,
WGDEVICE_A_S2,
WGDEVICE_A_H1,
WGDEVICE_A_H2,
WGDEVICE_A_H3,
WGDEVICE_A_H4,
__WGDEVICE_A_LAST
};
#define WGDEVICE_A_MAX (__WGDEVICE_A_LAST - 1)