diff --git a/contrib/json/wg-json b/contrib/json/wg-json index 3778b1d..bd15f03 100755 --- a/contrib/json/wg-json +++ b/contrib/json/wg-json @@ -24,10 +24,10 @@ while read -r -d $'\t' device; do [[ $s2 == "0" ]] || { printf '%s\t\t"s2": %u' "$delim" $(( $s2 )); delim=$',\n'; } [[ $s3 == "0" ]] || { printf '%s\t\t"s3": %u' "$delim" $(( $s3 )); delim=$',\n'; } [[ $s4 == "0" ]] || { printf '%s\t\t"s4": %u' "$delim" $(( $s4 )); delim=$',\n'; } - [[ $h1 == "1" ]] || { printf '%s\t\t"h1": %s' "$delim" $(( $h1 )); delim=$',\n'; } - [[ $h2 == "2" ]] || { printf '%s\t\t"h2": %s' "$delim" $(( $h2 )); delim=$',\n'; } - [[ $h3 == "3" ]] || { printf '%s\t\t"h3": %s' "$delim" $(( $h3 )); delim=$',\n'; } - [[ $h4 == "4" ]] || { printf '%s\t\t"h4": %s' "$delim" $(( $h4 )); delim=$',\n'; } + [[ $h1 == "1" ]] || { printf '%s\t\t"h1": "%s"' "$delim" "$h1"; delim=$',\n'; } + [[ $h2 == "2" ]] || { printf '%s\t\t"h2": "%s"' "$delim" "$h2"; delim=$',\n'; } + [[ $h3 == "3" ]] || { printf '%s\t\t"h3": "%s"' "$delim" "$h3"; delim=$',\n'; } + [[ $h4 == "4" ]] || { printf '%s\t\t"h4": "%s"' "$delim" "$h4"; delim=$',\n'; } [[ $i1 == "(none)" ]] || { printf '%s\t\t"i1": "%s"' "$delim" "$i1"; delim=$',\n'; } [[ $i2 == "(none)" ]] || { printf '%s\t\t"i2": "%s"' "$delim" "$i2"; delim=$',\n'; } [[ $i3 == "(none)" ]] || { printf '%s\t\t"i3": "%s"' "$delim" "$i3"; delim=$',\n'; } diff --git a/contrib/peer-approver/approve.sh b/contrib/peer-approver/approve.sh index e160856..09ae90d 100755 --- a/contrib/peer-approver/approve.sh +++ b/contrib/peer-approver/approve.sh @@ -19,7 +19,7 @@ PSK=$(echo ${ACCOUNT[2]}|tr -d '"') PSK_FILE=$(tempfile) echo "${PSK}" > "${PSK_FILE}" -awg set "${INTERFACE_NAME}" peer "${PUBLIC_KEY}" allowed-ips "${ALLOWED_IPS}" endpoint "${ENDPOINT}" allowed-ips "${ALLOWED_IPS}" preshared-key "${PSK_FILE}" awg "${AWG}" +awg set "${INTERFACE_NAME}" peer "${PUBLIC_KEY}" allowed-ips "${ALLOWED_IPS}" endpoint "${ENDPOINT}" preshared-key "${PSK_FILE}" advanced-security "${AWG}" EXIT_CODE=$? rm -f "{$PSK_FILE}" diff --git a/src/config.c b/src/config.c index b8e56da..e02694f 100644 --- a/src/config.c +++ b/src/config.c @@ -22,7 +22,7 @@ #define COMMENT_CHAR '#' -// Keys that should return empty string instead of NULL when not found +// Keys that should be not stripped of whitespace static const char *awg_special_handshake_keys[] = { "I1", "I2", "I3", "I4", "I5", "J1", "J2", "J3", @@ -431,6 +431,11 @@ static inline bool parse_awg_string(char **device_value, const char *name, const } *device_value = strdup(value); + if (*device_value == NULL) { + perror("strdup"); + return false; + } + return true; } @@ -912,56 +917,56 @@ struct wgdevice *config_read_cmd(const char *argv[], int argc) device->flags |= WGDEVICE_HAS_I2; argv += 2; - argc -=2; + argc -= 2; } else if (!strcmp(argv[0], "i3") && argc >= 2 && !peer) { if (!parse_awg_string(&device->i3, "i3", argv[1])) goto error; device->flags |= WGDEVICE_HAS_I3; argv += 2; - argc -=2; + argc -= 2; } else if (!strcmp(argv[0], "i4") && argc >= 2 && !peer) { if (!parse_awg_string(&device->i4, "i4", argv[1])) goto error; device->flags |= WGDEVICE_HAS_I4; argv += 2; - argc -=2; + argc -= 2; } else if (!strcmp(argv[0], "i5") && argc >= 2 && !peer) { if (!parse_awg_string(&device->i5, "i5", argv[1])) goto error; device->flags |= WGDEVICE_HAS_I5; argv += 2; - argc -=2; + argc -= 2; } else if (!strcmp(argv[0], "j1") && argc >= 2 && !peer) { if (!parse_awg_string(&device->j1, "j1", argv[1])) goto error; device->flags |= WGDEVICE_HAS_J1; argv += 2; - argc -=2; + argc -= 2; } else if (!strcmp(argv[0], "j2") && argc >= 2 && !peer) { if (!parse_awg_string(&device->j2, "j2", argv[1])) goto error; device->flags |= WGDEVICE_HAS_J2; argv += 2; - argc -=2; + argc -= 2; } else if (!strcmp(argv[0], "j3") && argc >= 2 && !peer) { if (!parse_awg_string(&device->j3, "j3", argv[1])) goto error; device->flags |= WGDEVICE_HAS_J3; argv += 2; - argc -=2; + argc -= 2; } else if (!strcmp(argv[0], "itime") && argc >= 2 && !peer) { if (!parse_uint32(&device->itime, "itime", argv[1])) goto error; device->flags |= WGDEVICE_HAS_ITIME; argv += 2; - argc -=2; + argc -= 2; } else if (!strcmp(argv[0], "peer") && argc >= 2) { struct wgpeer *new_peer = calloc(1, sizeof(*new_peer)); diff --git a/src/containers.h b/src/containers.h index 7ba9fac..ec5c2e2 100644 --- a/src/containers.h +++ b/src/containers.h @@ -88,18 +88,18 @@ enum { WGDEVICE_HAS_S3 = 1U << 10, WGDEVICE_HAS_S4 = 1U << 11, WGDEVICE_HAS_H1 = 1U << 12, - WGDEVICE_HAS_H2 = 1U << 12, - WGDEVICE_HAS_H3 = 1U << 13, - WGDEVICE_HAS_H4 = 1U << 14, - WGDEVICE_HAS_I1 = 1U << 15, - WGDEVICE_HAS_I2 = 1U << 16, - WGDEVICE_HAS_I3 = 1U << 17, - WGDEVICE_HAS_I4 = 1U << 18, - WGDEVICE_HAS_I5 = 1U << 19, - WGDEVICE_HAS_J1 = 1U << 20, - WGDEVICE_HAS_J2 = 1U << 21, - WGDEVICE_HAS_J3 = 1U << 22, - WGDEVICE_HAS_ITIME = 1U << 23 + WGDEVICE_HAS_H2 = 1U << 13, + WGDEVICE_HAS_H3 = 1U << 14, + WGDEVICE_HAS_H4 = 1U << 15, + WGDEVICE_HAS_I1 = 1U << 16, + WGDEVICE_HAS_I2 = 1U << 17, + WGDEVICE_HAS_I3 = 1U << 18, + WGDEVICE_HAS_I4 = 1U << 19, + WGDEVICE_HAS_I5 = 1U << 20, + WGDEVICE_HAS_J1 = 1U << 21, + WGDEVICE_HAS_J2 = 1U << 22, + WGDEVICE_HAS_J3 = 1U << 23, + WGDEVICE_HAS_ITIME = 1U << 24 }; struct wgdevice { diff --git a/src/ipc-freebsd.h b/src/ipc-freebsd.h index 52f9f34..119bc20 100644 --- a/src/ipc-freebsd.h +++ b/src/ipc-freebsd.h @@ -145,6 +145,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->init_packet_magic_header = strdup((const char*)binary); + if (!dev->init_packet_magic_header) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_H1; } } @@ -153,6 +157,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->response_packet_magic_header = strdup((const char*)binary); + if (!dev->response_packet_magic_header) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_H2; } } @@ -161,6 +169,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->underload_packet_magic_header = strdup((const char*)binary); + if (!dev->underload_packet_magic_header) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_H3; } } @@ -169,6 +181,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->transport_packet_magic_header = strdup((const char*)binary); + if (!dev->transport_packet_magic_header) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_H4; } } @@ -178,6 +194,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->i1 = strdup((const char*)binary); + if (!dev->i1) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_I1; } } @@ -187,6 +207,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->i2 = strdup((const char*)binary); + if (!dev->i2) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_I2; } } @@ -196,6 +220,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->i3 = strdup((const char*)binary); + if (!dev->i3) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_I3; } } @@ -205,6 +233,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->i4 = strdup((const char*)binary); + if (!dev->i4) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_I4; } } @@ -214,6 +246,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->i5 = strdup((const char*)binary); + if (!dev->i5) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_I5; } } @@ -223,6 +259,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->j1 = strdup((const char*)binary); + if (!dev->j1) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_J1; } } @@ -232,6 +272,10 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->j2 = strdup((const char*)binary); + if (!dev->j2) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_J2; } } @@ -241,10 +285,14 @@ static int kernel_get_device(struct wgdevice **device, const char *ifname) if (binary && size < MAX_AWG_STRING_LEN) { dev->j3 = strdup((const char*)binary); + if (!dev->j3) { + ret = ENOMEM; + goto err; + } dev->flags |= WGDEVICE_HAS_J3; } } - if (nvlist_exists_binary(nvl_device, "itime")) + if (nvlist_exists_number(nvl_device, "itime")) { number = nvlist_get_number(nvl_device, "itime"); if (number <= UINT32_MAX) @@ -450,13 +498,13 @@ static int kernel_set_device(struct wgdevice *dev) if (dev->flags & WGDEVICE_HAS_S4) nvlist_add_number(nvl_device, "s4", dev->transport_packet_junk_size); if (dev->flags & WGDEVICE_HAS_H1) - nvlist_add_binary(nvl_device, "h1", dev->init_packet_magic_header, strlen(dev->h1) + 1); + nvlist_add_binary(nvl_device, "h1", dev->init_packet_magic_header, strlen(dev->init_packet_magic_header) + 1); if (dev->flags & WGDEVICE_HAS_H2) - nvlist_add_binary(nvl_device, "h2", dev->response_packet_magic_header, strlen(dev->h2) + 1); + nvlist_add_binary(nvl_device, "h2", dev->response_packet_magic_header, strlen(dev->response_packet_magic_header) + 1); if (dev->flags & WGDEVICE_HAS_H3) - nvlist_add_binary(nvl_device, "h3", dev->underload_packet_magic_header, strlen(dev->h3) + 1); + nvlist_add_binary(nvl_device, "h3", dev->underload_packet_magic_header, strlen(dev->underload_packet_magic_header) + 1); if (dev->flags & WGDEVICE_HAS_H4) - nvlist_add_binary(nvl_device, "h4", dev->transport_packet_magic_header, strlen(dev->h4) + 1); + nvlist_add_binary(nvl_device, "h4", dev->transport_packet_magic_header, strlen(dev->transport_packet_magic_header) + 1); if (dev->flags & WGDEVICE_HAS_I1) nvlist_add_binary(nvl_device, "i1", dev->i1, strlen(dev->i1) + 1); if (dev->flags & WGDEVICE_HAS_I2) diff --git a/src/ipc-linux.h b/src/ipc-linux.h index 8c2e682..be2855b 100644 --- a/src/ipc-linux.h +++ b/src/ipc-linux.h @@ -550,86 +550,137 @@ static int parse_device(const struct nlattr *attr, void *data) case WGDEVICE_A_H1: if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->init_packet_magic_header = strdup(mnl_attr_get_str(attr)); + if (!device->init_packet_magic_header) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_H1; } break; case WGDEVICE_A_H2: if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->response_packet_magic_header = strdup(mnl_attr_get_str(attr)); + if (!device->response_packet_magic_header) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_H2; } break; case WGDEVICE_A_H3: if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->underload_packet_magic_header = strdup(mnl_attr_get_str(attr)); + if (!device->underload_packet_magic_header) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_H3; } break; case WGDEVICE_A_H4: if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->transport_packet_magic_header = strdup(mnl_attr_get_str(attr)); + if (!device->transport_packet_magic_header) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_H4; } break; case WGDEVICE_A_I1: - if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) - { + if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->i1 = strdup(mnl_attr_get_str(attr)); + if (!device->i1) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_I1; } break; case WGDEVICE_A_I2: - if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) - { + if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->i2 = strdup(mnl_attr_get_str(attr)); + if (!device->i2) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_I2; } break; case WGDEVICE_A_I3: - if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) - { + if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->i3 = strdup(mnl_attr_get_str(attr)); + if (!device->i3) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_I3; } break; case WGDEVICE_A_I4: - if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) - { + if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->i4 = strdup(mnl_attr_get_str(attr)); + if (!device->i4) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_I4; } break; case WGDEVICE_A_I5: - if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) - { + if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->i5 = strdup(mnl_attr_get_str(attr)); + if (!device->i5) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_I5; } break; case WGDEVICE_A_J1: - if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) - { + if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->j1 = strdup(mnl_attr_get_str(attr)); + if (!device->j1) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_J1; } break; case WGDEVICE_A_J2: - if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) - { + if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->j2 = strdup(mnl_attr_get_str(attr)); + if (!device->j2) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_J2; } break; case WGDEVICE_A_J3: - if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) - { + if (!mnl_attr_validate(attr, MNL_TYPE_STRING)) { device->j3 = strdup(mnl_attr_get_str(attr)); + if (!device->j3) { + perror("strdup"); + return MNL_CB_ERROR; + } + device->flags |= WGDEVICE_HAS_J3; } break; case WGDEVICE_A_ITIME: - if (!mnl_attr_validate(attr, MNL_TYPE_U32)) - { + if (!mnl_attr_validate(attr, MNL_TYPE_U32)) { device->itime = mnl_attr_get_u32(attr); device->flags |= WGDEVICE_HAS_ITIME; } diff --git a/src/ipc-openbsd.h b/src/ipc-openbsd.h index 231ff77..c3606f6 100644 --- a/src/ipc-openbsd.h +++ b/src/ipc-openbsd.h @@ -147,69 +147,105 @@ static int kernel_get_device(struct wgdevice **device, const char *iface) if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_H1) { dev->init_packet_magic_header = strdup(wg_iface->i_init_packet_magic_header); + if (!dev->init_packet_magic_header) + goto out; + dev->flags |= WGDEVICE_HAS_H1; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_H2) { dev->response_packet_magic_header = strdup(wg_iface->i_response_packet_magic_header); + if (!dev->response_packet_magic_header) + goto out; + dev->flags |= WGDEVICE_HAS_H2; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_H3) { dev->underload_packet_magic_header = strdup(wg_iface->i_underload_packet_magic_header); + if (!dev->underload_packet_magic_header) + goto out; + dev->flags |= WGDEVICE_HAS_H3; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_H4) { dev->transport_packet_magic_header = strdup(wg_iface->i_transport_packet_magic_header); + if (!dev->transport_packet_magic_header) + goto out; + dev->flags |= WGDEVICE_HAS_H4; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_I1) { dev->i1 = strdup(wg_iface->i_i1); + if (!dev->i1) + goto out; + dev->flags |= WGDEVICE_HAS_I1; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_I2) { dev->i2 = strdup(wg_iface->i_i2); + if (!dev->i2) + goto out; + dev->flags |= WGDEVICE_HAS_I2; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_I3) { dev->i3 = strdup(wg_iface->i_i3); + if (!dev->i3) + goto out; + dev->flags |= WGDEVICE_HAS_I3; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_I4) { dev->i4 = strdup(wg_iface->i_i4); + if (!dev->i4) + goto out; + dev->flags |= WGDEVICE_HAS_I4; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_I5) { dev->i5 = strdup(wg_iface->i_i5); + if (!dev->i5) + goto out; + dev->flags |= WGDEVICE_HAS_I5; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_J1) { dev->j1 = strdup(wg_iface->i_j1); + if (!dev->j1) + goto out; + dev->flags |= WGDEVICE_HAS_J1; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_J2) { dev->j2 = strdup(wg_iface->i_j2); + if (!dev->j2) + goto out; + dev->flags |= WGDEVICE_HAS_J2; } if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_J3) { dev->j3 = strdup(wg_iface->i_j3); + if (!dev->j3) + goto out; + dev->flags |= WGDEVICE_HAS_J3; } @@ -368,69 +404,105 @@ static int kernel_set_device(struct wgdevice *dev) if (dev->flags & WGDEVICE_HAS_H1) { wg_iface->i_init_packet_magic_header = strdup(dev->init_packet_magic_header); + if (!wg_iface->i_init_packet_magic_header) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_H1; } if (dev->flags & WGDEVICE_HAS_H2) { wg_iface->i_response_packet_magic_header = strdup(dev->response_packet_magic_header); + if (!wg_iface->i_response_packet_magic_header) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_H2; } if (dev->flags & WGDEVICE_HAS_H3) { wg_iface->i_underload_packet_magic_header = strdup(dev->underload_packet_magic_header); + if (!wg_iface->i_underload_packet_magic_header) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_H3; } if (dev->flags & WGDEVICE_HAS_H4) { wg_iface->i_transport_packet_magic_header = strdup(dev->transport_packet_magic_header); + if (!wg_iface->i_transport_packet_magic_header) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_H4; } if (dev->flags & WGDEVICE_HAS_I1) { wg_iface->i_i1 = strdup(dev->i1); + if (!wg_iface->i_i1) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_I1; } if (dev->flags & WGDEVICE_HAS_I2) { wg_iface->i_i2 = strdup(dev->i2); + if (!wg_iface->i_i2) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_I2; } if (dev->flags & WGDEVICE_HAS_I3) { wg_iface->i_i3 = strdup(dev->i3); + if (!wg_iface->i_i3) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_I3; } if (dev->flags & WGDEVICE_HAS_I4) { wg_iface->i_i4 = strdup(dev->i4); + if (!wg_iface->i_i4) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_I4; } if (dev->flags & WGDEVICE_HAS_I5) { wg_iface->i_i5 = strdup(dev->i5); + if (!wg_iface->i_i5) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_I5; } if (dev->flags & WGDEVICE_HAS_J1) { wg_iface->i_j1 = strdup(dev->j1); + if (!wg_iface->i_j1) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_J1; } if (dev->flags & WGDEVICE_HAS_J2) { wg_iface->i_j2 = strdup(dev->j2); + if (!wg_iface->i_j2) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_J2; } if (dev->flags & WGDEVICE_HAS_J3) { wg_iface->i_j3 = strdup(dev->j3); + if (!wg_iface->i_j3) + goto out; + wg_iface->i_flags |= WG_INTERFACE_DEVICE_HAS_J3; } @@ -495,7 +567,7 @@ static int kernel_set_device(struct wgdevice *dev) out: ret = -errno; - if (wgdata.wgd_interface) { + if (wg_iface) { if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_H1) free(wg_iface->i_init_packet_magic_header); if (wg_iface->i_flags & WG_INTERFACE_DEVICE_HAS_H2) diff --git a/src/ipc-uapi.h b/src/ipc-uapi.h index b6cfafb..fb44f10 100644 --- a/src/ipc-uapi.h +++ b/src/ipc-uapi.h @@ -251,49 +251,101 @@ static int userspace_get_device(struct wgdevice **out, const char *iface) dev->flags |= WGDEVICE_HAS_S4; } else if(!peer && !strcmp(key, "h1")) { dev->init_packet_magic_header = strdup(value); + if (!dev->init_packet_magic_header) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_H1; } else if(!peer && !strcmp(key, "h2")) { dev->response_packet_magic_header = strdup(value); + if (!dev->response_packet_magic_header) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_H2; } else if(!peer && !strcmp(key, "h3")) { dev->underload_packet_magic_header = strdup(value); + if (!dev->underload_packet_magic_header) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_H3; } else if(!peer && !strcmp(key, "h4")) { dev->transport_packet_magic_header = strdup(value); + if (!dev->transport_packet_magic_header) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_H4; } else if (!peer && !strcmp(key, "i1")) { dev->i1 = strdup(value); + if (!dev->i1) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_I1; - } - else if (!peer && !strcmp(key, "i2")) { + } else if (!peer && !strcmp(key, "i2")) { dev->i2 = strdup(value); + if (!dev->i2) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_I2; - } - else if (!peer && !strcmp(key, "i3")) { + } else if (!peer && !strcmp(key, "i3")) { dev->i3 = strdup(value); + if (!dev->i3) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_I3; - } - else if (!peer && !strcmp(key, "i4")) { + } else if (!peer && !strcmp(key, "i4")) { dev->i4 = strdup(value); + if (!dev->i4) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_I4; - } - else if (!peer && !strcmp(key, "i5")) { + } else if (!peer && !strcmp(key, "i5")) { dev->i5 = strdup(value); + if (!dev->i5) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_I5; - } - else if (!peer && !strcmp(key, "j1")) { + } else if (!peer && !strcmp(key, "j1")) { dev->j1 = strdup(value); + if (!dev->j1) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_J1; - } - else if (!peer && !strcmp(key, "j2")) { + } else if (!peer && !strcmp(key, "j2")) { dev->j2 = strdup(value); + if (!dev->j2) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_J2; - } - else if (!peer && !strcmp(key, "j3")) { + } else if (!peer && !strcmp(key, "j3")) { dev->j3 = strdup(value); + if (!dev->j3) { + ret = -ENOMEM; + goto err; + } + dev->flags |= WGDEVICE_HAS_J3; - } - else if (!peer && !strcmp(key, "itime")) { + } else if (!peer && !strcmp(key, "itime")) { dev->itime = NUM(0xffffffffU); dev->flags |= WGDEVICE_HAS_ITIME; } else if (!strcmp(key, "public_key")) { diff --git a/src/ipc-windows.h b/src/ipc-windows.h index c4da037..c5488cd 100644 --- a/src/ipc-windows.h +++ b/src/ipc-windows.h @@ -673,18 +673,30 @@ static int kernel_set_device(struct wgdevice *dev) out: ret = -errno; if (wg_iface) { - free(wg_iface->InitPacketMagicHeader); - free(wg_iface->ResponsePacketMagicHeader); - free(wg_iface->UnderloadPacketMagicHeader); - free(wg_iface->TransportPacketMagicHeader); - free(wg_iface->I1); - free(wg_iface->I2); - free(wg_iface->I3); - free(wg_iface->I4); - free(wg_iface->I5); - free(wg_iface->J1); - free(wg_iface->J2); - free(wg_iface->J3); + if (wg_iface->InitPacketMagicHeader) + free(wg_iface->InitPacketMagicHeader); + if (wg_iface->ResponsePacketMagicHeader) + free(wg_iface->ResponsePacketMagicHeader); + if (wg_iface->UnderloadPacketMagicHeader) + free(wg_iface->UnderloadPacketMagicHeader); + if (wg_iface->TransportPacketMagicHeader) + free(wg_iface->TransportPacketMagicHeader); + if (wg_iface->I1) + free(wg_iface->I1); + if (wg_iface->I2) + free(wg_iface->I2); + if (wg_iface->I3) + free(wg_iface->I3); + if (wg_iface->I4) + free(wg_iface->I4); + if (wg_iface->I5) + free(wg_iface->I5); + if (wg_iface->J1) + free(wg_iface->J1); + if (wg_iface->J2) + free(wg_iface->J2); + if (wg_iface->J3) + free(wg_iface->J3); } free(wg_iface); CloseHandle(handle); diff --git a/src/show.c b/src/show.c index dbecafb..1175faf 100644 --- a/src/show.c +++ b/src/show.c @@ -308,18 +308,30 @@ static void dump_print(struct wgdevice *device, bool with_interface) printf("%u\t", device->response_packet_junk_size); printf("%u\t", device->cookie_reply_packet_junk_size); printf("%u\t", device->transport_packet_junk_size); - printf("%s\t", device->init_packet_magic_header); - printf("%s\t", device->response_packet_magic_header); - printf("%s\t", device->underload_packet_magic_header); - printf("%s\t", device->transport_packet_magic_header); - printf("%s\t", device->i1); - printf("%s\t", device->i2); - printf("%s\t", device->i3); - printf("%s\t", device->i4); - printf("%s\t", device->i5); - printf("%s\t", device->j1); - printf("%s\t", device->j2); - printf("%s\t", device->j3); + fputs(device->init_packet_magic_header ? device->init_packet_magic_header : "(null)", stdout); + fputc('\t', stdout); + fputs(device->response_packet_magic_header ? device->response_packet_magic_header : "(null)", stdout); + fputc('\t', stdout); + fputs(device->underload_packet_magic_header ? device->underload_packet_magic_header : "(null)", stdout); + fputc('\t', stdout); + fputs(device->transport_packet_magic_header ? device->transport_packet_magic_header : "(null)", stdout); + fputc('\t', stdout); + fputs(device->i1 ? device->i1 : "(null)", stdout); + fputc('\t', stdout); + fputs(device->i2 ? device->i2 : "(null)", stdout); + fputc('\t', stdout); + fputs(device->i3 ? device->i3 : "(null)", stdout); + fputc('\t', stdout); + fputs(device->i4 ? device->i4 : "(null)", stdout); + fputc('\t', stdout); + fputs(device->i5 ? device->i5 : "(null)", stdout); + fputc('\t', stdout); + fputs(device->j1 ? device->j1 : "(null)", stdout); + fputc('\t', stdout); + fputs(device->j2 ? device->j2 : "(null)", stdout); + fputc('\t', stdout); + fputs(device->j3 ? device->j3 : "(null)", stdout); + fputc('\t', stdout); printf("%u\t", device->itime); if (device->fwmark) @@ -373,83 +385,83 @@ 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, "jc")) { + } 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")) { + } 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")) { + } 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")) { + } 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")) { + } 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, "s3")) { + } else if (!strcmp(param, "s3")) { if (with_interface) printf("%s\t", device->name); printf("%u\n", device->cookie_reply_packet_junk_size); - } else if(!strcmp(param, "s4")) { + } else if (!strcmp(param, "s4")) { if (with_interface) printf("%s\t", device->name); printf("%u\n", device->transport_packet_junk_size); - } else if(!strcmp(param, "h1")) { + } else if (!strcmp(param, "h1")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->init_packet_magic_header); - } else if(!strcmp(param, "h2")) { + } else if (!strcmp(param, "h2")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->response_packet_magic_header); - } else if(!strcmp(param, "h3")) { + } else if (!strcmp(param, "h3")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->underload_packet_magic_header); - } else if(!strcmp(param, "h4")) { + } else if (!strcmp(param, "h4")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->transport_packet_magic_header); - } else if(!strcmp(param, "i1")) { + } else if (!strcmp(param, "i1")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->i1); - } else if(!strcmp(param, "i2")) { + } else if (!strcmp(param, "i2")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->i2); - } else if(!strcmp(param, "i3")) { + } else if (!strcmp(param, "i3")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->i3); - } else if(!strcmp(param, "i4")) { + } else if (!strcmp(param, "i4")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->i4); - } else if(!strcmp(param, "i5")) { + } else if (!strcmp(param, "i5")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->i5); - } else if(!strcmp(param, "j1")) { + } else if (!strcmp(param, "j1")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->j1); - } else if(!strcmp(param, "j2")) { + } else if (!strcmp(param, "j2")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->j2); - } else if(!strcmp(param, "j3")) { + } else if (!strcmp(param, "j3")) { if (with_interface) printf("%s\t", device->name); printf("%s\n", device->j3); - } else if(!strcmp(param, "itime")) { + } else if (!strcmp(param, "itime")) { if (with_interface) printf("%s\t", device->name); printf("%u\n", device->itime); diff --git a/src/uapi/windows/wireguard.h b/src/uapi/windows/wireguard.h index 0ddcde6..bfe1caa 100644 --- a/src/uapi/windows/wireguard.h +++ b/src/uapi/windows/wireguard.h @@ -66,18 +66,18 @@ typedef enum WG_IOCTL_INTERFACE_S3 = 1 << 10, WG_IOCTL_INTERFACE_S4 = 1 << 11, WG_IOCTL_INTERFACE_H1 = 1 << 12, - WG_IOCTL_INTERFACE_H2 = 1 << 12, - WG_IOCTL_INTERFACE_H3 = 1 << 13, - WG_IOCTL_INTERFACE_H4 = 1 << 14, - WG_IOCTL_INTERFACE_I1 = 1U << 15, - WG_IOCTL_INTERFACE_I2 = 1U << 16, - WG_IOCTL_INTERFACE_I3 = 1U << 17, - WG_IOCTL_INTERFACE_I4 = 1U << 18, - WG_IOCTL_INTERFACE_I5 = 1U << 19, - WG_IOCTL_INTERFACE_J1 = 1U << 20, - WG_IOCTL_INTERFACE_J2 = 1U << 21, - WG_IOCTL_INTERFACE_J3 = 1U << 22, - WG_IOCTL_INTERFACE_ITIME = 1U << 23 + WG_IOCTL_INTERFACE_H2 = 1 << 13, + WG_IOCTL_INTERFACE_H3 = 1 << 14, + WG_IOCTL_INTERFACE_H4 = 1 << 15, + WG_IOCTL_INTERFACE_I1 = 1U << 16, + WG_IOCTL_INTERFACE_I2 = 1U << 17, + WG_IOCTL_INTERFACE_I3 = 1U << 18, + WG_IOCTL_INTERFACE_I4 = 1U << 19, + WG_IOCTL_INTERFACE_I5 = 1U << 20, + WG_IOCTL_INTERFACE_J1 = 1U << 21, + WG_IOCTL_INTERFACE_J2 = 1U << 22, + WG_IOCTL_INTERFACE_J3 = 1U << 23, + WG_IOCTL_INTERFACE_ITIME = 1U << 24 } WG_IOCTL_INTERFACE_FLAG; typedef struct _WG_IOCTL_INTERFACE diff --git a/src/wg-quick/freebsd.bash b/src/wg-quick/freebsd.bash index 60e7ca6..6213309 100755 --- a/src/wg-quick/freebsd.bash +++ b/src/wg-quick/freebsd.bash @@ -111,7 +111,7 @@ parse_options() { H3);& H4);& I1);& - i2);& + I2);& I3);& I4);& I5);& diff --git a/src/wg-quick/openbsd.bash b/src/wg-quick/openbsd.bash index 210570b..902737c 100755 --- a/src/wg-quick/openbsd.bash +++ b/src/wg-quick/openbsd.bash @@ -82,7 +82,7 @@ parse_options() { H3);& H4);& I1);& - i2);& + I2);& I3);& I4);& I5);&