]> git.ipfire.org Git - thirdparty/wireguard-tools.git/commitdiff
socket: enable setting of fwmark
authorJason A. Donenfeld <Jason@zx2c4.com>
Tue, 24 Jan 2017 16:43:35 +0000 (17:43 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Mon, 13 Feb 2017 20:55:18 +0000 (21:55 +0100)
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
src/completion/wg.bash-completion
src/config.c
src/set.c
src/show.c
src/showconf.c
src/wg-quick.bash
src/wg.8

index 76a832a8dbde69192e5ef8e67f8f734b513fe288..8822d01adc6f9092883f06781a2984d03baebed0 100644 (file)
@@ -19,7 +19,7 @@ _wg_completion() {
        fi
 
        if [[ $COMP_CWORD -eq 3 && ${COMP_WORDS[1]} == show && ${COMP_WORDS[2]} != interfaces ]]; then
-               COMPREPLY+=( $(compgen -W "public-key private-key preshared-key listen-port peers endpoints allowed-ips latest-handshakes persistent-keepalive transfer" -- "${COMP_WORDS[3]}") )
+               COMPREPLY+=( $(compgen -W "public-key private-key preshared-key listen-port peers endpoints allowed-ips fwmark latest-handshakes persistent-keepalive transfer" -- "${COMP_WORDS[3]}") )
                return
        fi
 
@@ -31,9 +31,10 @@ _wg_completion() {
 
        [[ ${COMP_WORDS[1]} == set ]] || return
 
-       local has_listen_port=0 has_private_key=0 has_preshared_key=0 has_peer=0 has_remove=0 has_endpoint=0 has_persistent_keepalive=0 has_allowed_ips=0 words=() i j
+       local has_listen_port=0 has_fwmark=0 has_private_key=0 has_preshared_key=0 has_peer=0 has_remove=0 has_endpoint=0 has_persistent_keepalive=0 has_allowed_ips=0 words=() i j
        for ((i=3;i<COMP_CWORD;i+=2)); do
                [[ ${COMP_WORDS[i]} == listen-port ]] && has_listen_port=1
+               [[ ${COMP_WORDS[i]} == fwmark ]] && has_fwmark=1
                [[ ${COMP_WORDS[i]} == private-key ]] && has_private_key=1
                [[ ${COMP_WORDS[i]} == preshared-key ]] && has_preshared_key=1
                [[ ${COMP_WORDS[i]} == peer ]] && { has_peer=$i; break; }
@@ -41,6 +42,7 @@ _wg_completion() {
        if [[ $has_peer -eq 0 ]]; then
                if ((COMP_CWORD % 2 != 0)); then
                        [[ $has_listen_port -eq 1 ]] || words+=( listen-port )
+                       [[ $has_fwmark -eq 1 ]] || words+=( fwmark )
                        [[ $has_private_key -eq 1 ]] || words+=( private-key )
                        [[ $has_preshared_key -eq 1 ]] || words+=( preshared-key )
                        words+=( peer )
index da19cc32c642c735fe6b042e67410b5a49e52d9a..b3384da77f47f1be2602a40bae212531ea9e2f09 100644 (file)
@@ -91,6 +91,25 @@ static inline uint16_t parse_port(const char *value)
        return port;
 }
 
+static inline bool parse_fwmark(uint32_t *fwmark, unsigned int *flags, const char *value)
+{
+       unsigned long ret;
+       char *end;
+       int base = 10;
+
+       if (value[0] == '0' && value[1] == 'x') {
+               value += 2;
+               base = 16;
+       }
+       ret = strtoul(value, &end, base);
+       if (!*value || *end || ret > UINT32_MAX)
+               return false;
+       *fwmark = ret;
+       if (!ret)
+               *flags |= WGDEVICE_REMOVE_FWMARK;
+       return true;
+}
+
 static inline bool parse_key(uint8_t key[static WG_KEY_LEN], const char *value)
 {
        uint8_t tmp[WG_KEY_LEN + 1];
@@ -281,6 +300,8 @@ static bool process_line(struct config_ctx *ctx, const char *line)
        if (ctx->is_device_section) {
                if (key_match("ListenPort"))
                        ret = !!(ctx->buf.dev->port = parse_port(value));
+               else if (key_match("FwMark"))
+                       ret = parse_fwmark(&ctx->buf.dev->fwmark, &ctx->buf.dev->flags, value);
                else if (key_match("PrivateKey")) {
                        ret = parse_key(ctx->buf.dev->private_key, value);
                        if (!ret)
@@ -372,6 +393,8 @@ bool config_read_finish(struct config_ctx *ctx)
        }
        if (ctx->buf.dev->flags & WGDEVICE_REPLACE_PEERS && !key_is_valid(ctx->buf.dev->preshared_key))
                ctx->buf.dev->flags |= WGDEVICE_REMOVE_PRESHARED_KEY;
+       if (ctx->buf.dev->flags & WGDEVICE_REPLACE_PEERS && !ctx->buf.dev->fwmark)
+               ctx->buf.dev->flags |= WGDEVICE_REMOVE_FWMARK;
 
        for_each_wgpeer(ctx->buf.dev, peer, i) {
                if (!key_is_valid(peer->public_key)) {
@@ -448,6 +471,11 @@ bool config_read_cmd(struct wgdevice **device, char *argv[], int argc)
                                goto error;
                        argv += 2;
                        argc -= 2;
+               } else if (!strcmp(argv[0], "fwmark") && argc >= 2 && !buf.dev->num_peers) {
+                       if (!parse_fwmark(&buf.dev->fwmark, &buf.dev->flags, argv[1]))
+                               goto error;
+                       argv += 2;
+                       argc -= 2;
                } else if (!strcmp(argv[0], "private-key") && argc >= 2 && !buf.dev->num_peers) {
                        char *line;
                        int ret = read_line(&line, argv[1]);
index 48b005097ab56aa78f39ea7c0533ea6d566ec5e4..5e4291ffe4fd2db4d07e26fa69b36f06128ffd2e 100644 (file)
--- a/src/set.c
+++ b/src/set.c
@@ -13,7 +13,7 @@ int set_main(int argc, char *argv[])
        int ret = 1;
 
        if (argc < 3) {
-               fprintf(stderr, "Usage: %s %s <interface> [listen-port <port>] [private-key <file path>] [peer <base64 public key> [remove] [endpoint <ip>:<port>] [persistent-keepalive <interval seconds>] [allowed-ips <ip1>/<cidr1>[,<ip2>/<cidr2>]...] ]...\n", PROG_NAME, argv[0]);
+               fprintf(stderr, "Usage: %s %s <interface> [listen-port <port>] [fwmark <mark>] [private-key <file path>] [peer <base64 public key> [remove] [endpoint <ip>:<port>] [persistent-keepalive <interval seconds>] [allowed-ips <ip1>/<cidr1>[,<ip2>/<cidr2>]...] ]...\n", PROG_NAME, argv[0]);
                return 1;
        }
 
index 124e32304fe2167afe627672fdb24ed60f91864d..c20d858c82590916233287dfbea1f665b95da6c8 100644 (file)
@@ -203,7 +203,7 @@ static char *bytes(uint64_t b)
 static const char *COMMAND_NAME = NULL;
 static void show_usage(void)
 {
-       fprintf(stderr, "Usage: %s %s { <interface> | all | interfaces } [public-key | private-key | preshared-key | listen-port | peers | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive]\n", PROG_NAME, COMMAND_NAME);
+       fprintf(stderr, "Usage: %s %s { <interface> | all | interfaces } [public-key | private-key | preshared-key | listen-port | fwmark | peers | endpoints | allowed-ips | latest-handshakes | transfer | persistent-keepalive]\n", PROG_NAME, COMMAND_NAME);
 }
 
 static void pretty_print(struct wgdevice *device)
@@ -222,6 +222,8 @@ static void pretty_print(struct wgdevice *device)
                terminal_printf("  " TERMINAL_BOLD "pre-shared key" TERMINAL_RESET ": %s\n", masked_key(device->preshared_key));
        if (device->port)
                terminal_printf("  " TERMINAL_BOLD "listening port" TERMINAL_RESET ": %u\n", device->port);
+       if (device->fwmark)
+               terminal_printf("  " TERMINAL_BOLD "fwmark" TERMINAL_RESET ": 0x%x\n", device->fwmark);
        if (device->num_peers) {
                sort_peers(device);
                terminal_printf("\n");
@@ -271,6 +273,10 @@ static bool ugly_print(struct wgdevice *device, const char *param, bool with_int
                if (with_interface)
                        printf("%s\t", device->interface);
                printf("%u\n", device->port);
+       } else if (!strcmp(param, "fwmark")) {
+               if (with_interface)
+                       printf("%s\t", device->interface);
+               printf("0x%x\n", device->fwmark);
        } else if (!strcmp(param, "endpoints")) {
                if (with_interface)
                        printf("%s\t", device->interface);
index d2f36e0d06bb8dd35711b07aade71e73fe5bb595..585b08db13b74679ec3aa523668734f66fcb2b52 100644 (file)
@@ -44,7 +44,9 @@ int showconf_main(int argc, char *argv[])
 
        printf("[Interface]\n");
        if (device->port)
-               printf("ListenPort = %d\n", device->port);
+               printf("ListenPort = %u\n", device->port);
+       if (device->fwmark)
+               printf("FwMark = 0x%x\n", device->fwmark);
        if (memcmp(device->private_key, zero, WG_KEY_LEN)) {
                b64_ntop(device->private_key, WG_KEY_LEN, b64, b64_len(WG_KEY_LEN));
                printf("PrivateKey = %s\n", b64);
index 1eb71b2c45876fc50f48ded6e007883b5d961b79..5db34859bbea1cdd1d1a37d3fcbcbaa9beedbb84 100755 (executable)
@@ -78,10 +78,15 @@ add_if() {
 }
 
 del_if() {
-       if [[ $(ip route show table all) =~ .*\ dev\ $INTERFACE\ table\ ([0-9]+)\ .* ]]; then
-               while [[ -n $(ip rule show table ${BASH_REMATCH[1]}) ]]; do
-                       cmd ip rule delete table "${BASH_REMATCH[1]}"
-                       [[ $(ip rule show table main) == *"from all lookup main suppress_prefixlength 0"* ]] && cmd ip rule delete table main suppress_prefixlength 0
+       DEFAULT_TABLE=$(("$(wg show "$INTERFACE" fwmark)"))
+       if [[ $DEFAULT_TABLE -ne 0 ]]; then
+               while [[ -n $(ip -4 rule show table $DEFAULT_TABLE) ]]; do
+                       cmd ip -4 rule delete table $DEFAULT_TABLE
+                       [[ $(ip -4 rule show table main) == *"from all lookup main suppress_prefixlength 0"* ]] && cmd ip -4 rule delete table main suppress_prefixlength 0
+               done
+               while [[ -n $(ip -6 rule show table $DEFAULT_TABLE) ]]; do
+                       cmd ip -6 rule delete table $DEFAULT_TABLE
+                       [[ $(ip -6 rule show table main) == *"from all lookup main suppress_prefixlength 0"* ]] && cmd ip -6 rule delete table main suppress_prefixlength 0
                done
        fi
        cmd ip link delete dev "$INTERFACE"
@@ -104,22 +109,22 @@ add_route() {
 }
 
 DEFAULT_TABLE=
-PREVIOUS_ENDPOINT=
 add_default() {
-       [[ $(join <(wg show "$INTERFACE" allowed-ips) <(wg show "$INTERFACE" endpoints)) =~ ([A-Za-z0-9/+=]{44})\ ([0-9a-f/.:]+ )*${1//./\\.}\ ([0-9a-f/.:]+ )*\[?([0-9.:a-f]+)\]?:[0-9]+ ]] && local endpoint="${BASH_REMATCH[4]}"
-       [[ -n $endpoint ]] || return 0
-       local first=0
        if [[ -z $DEFAULT_TABLE ]]; then
-               first=1
                DEFAULT_TABLE=51820
                while [[ -n $(ip route show table $DEFAULT_TABLE) ]]; do ((DEFAULT_TABLE++)); done
        fi
-       cmd ip route add "$1" dev "$INTERFACE" table $DEFAULT_TABLE
-       [[ $PREVIOUS_ENDPOINT == "$endpoint" ]] && return 0
-       PREVIOUS_ENDPOINT="$endpoint"
-       cmd ip rule add not to "$endpoint" table $DEFAULT_TABLE
-       [[ $first -eq 1 ]] || return 0
-       cmd ip rule add table main suppress_prefixlength 0
+       local proto=-4
+       [[ $1 == *:* ]] && proto=-6
+       cmd wg set "$INTERFACE" fwmark $DEFAULT_TABLE
+       cmd ip $proto route add "$1" dev "$INTERFACE" table $DEFAULT_TABLE
+       cmd ip $proto rule add not fwmark $DEFAULT_TABLE table $DEFAULT_TABLE
+       cmd ip $proto rule add table main suppress_prefixlength 0
+       local key equals value
+       while read -r key equals value; do
+               [[ $value -eq 1 ]] && sysctl -q "$key=2"
+       done < <(sysctl -a -r 'net\.ipv4.conf\..+\.rp_filter')
+       return 0
 }
 
 set_config() {
index be05acf14c64a8a1483848f9f1b32e0a82900ba1..9aa76cfa033118669b3ff1ff4e15c33087f2072f 100644 (file)
--- a/src/wg.8
+++ b/src/wg.8
@@ -36,7 +36,7 @@ Sub-commands that take an INTERFACE must be passed a WireGuard interface.
 .SH COMMANDS
 
 .TP
-\fBshow\fP { \fI<interface>\fP | \fIall\fP | \fIinterfaces\fP } [\fIpublic-key\fP | \fIprivate-key\fP | \fIpreshared-key\fP | \fIlisten-port\fP | \fIpeers\fP | \fIendpoints\fP | \fIallowed-ips\fP | \fIlatest-handshakes\fP | \fIpersistent-keepalive\fP | \fItransfer\fP]
+\fBshow\fP { \fI<interface>\fP | \fIall\fP | \fIinterfaces\fP } [\fIpublic-key\fP | \fIprivate-key\fP | \fIpreshared-key\fP | \fIlisten-port\fP | \fIfwmark\fP | \fIpeers\fP | \fIendpoints\fP | \fIallowed-ips\fP | \fIlatest-handshakes\fP | \fIpersistent-keepalive\fP | \fItransfer\fP]
 Shows current WireGuard configuration of specified \fI<interface>\fP.
 If no \fI<interface>\fP is specified, \fI<interface>\fP defaults to \fIall\fP.
 If \fIinterfaces\fP is specified, prints a list of all WireGuard interfaces,
@@ -49,7 +49,7 @@ newlines and tabs, meant to be used in scripts.
 Shows the current configuration of \fI<interface>\fP in the format described
 by \fICONFIGURATION FILE FORMAT\fP below.
 .TP
-\fBset\fP \fI<interface>\fP [\fIlisten-port\fP \fI<port>\fP] [\fIprivate-key\fP \fI<file-path>\fP] [\fIpreshared-key\fP \fI<file-path>\fP] [\fIpeer\fP \fI<base64-public-key>\fP [\fIremove\fP] [\fIendpoint\fP \fI<ip>:<port>\fP] [\fIpersistent-keepalive\fP \fI<interval seconds>\fP] [\fIallowed-ips\fP \fI<ip1>/<cidr1>\fP[,\fI<ip2>/<cidr2>\fP]...] ]...
+\fBset\fP \fI<interface>\fP [\fIlisten-port\fP \fI<port>\fP] [\fIfwmark\fP \fI<fwmark>\fP] [\fIprivate-key\fP \fI<file-path>\fP] [\fIpreshared-key\fP \fI<file-path>\fP] [\fIpeer\fP \fI<base64-public-key>\fP [\fIremove\fP] [\fIendpoint\fP \fI<ip>:<port>\fP] [\fIpersistent-keepalive\fP \fI<interval seconds>\fP] [\fIallowed-ips\fP \fI<ip1>/<cidr1>\fP[,\fI<ip2>/<cidr2>\fP]...] ]...
 Sets configuration values for the specified \fI<interface>\fP. Multiple
 \fIpeer\fPs may be specified, and if the \fIremove\fP argument is given
 for a peer, that peer is removed, not configured. If \fIlisten-port\fP
@@ -125,6 +125,8 @@ for post-quantum resistance.
 .IP \(bu
 ListenPort \(em a 16-bit port for listening. Optional; if not specified, chosen
 randomly.
+.IP \(bu
+FwMark \(em a 32-bit fwmark for outgoing packets. Optional.
 .P
 The \fIPeer\fP sections may contain the following fields:
 .IP \(bu