2017-11-30 16:23:50 +01:00
|
|
|
# SPDX-License-Identifier: GPL-2.0
|
|
|
|
#
|
2020-01-02 19:52:25 +01:00
|
|
|
# Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2017-11-30 16:23:50 +01:00
|
|
|
|
2017-01-04 19:28:28 +01:00
|
|
|
PKG_CONFIG ?= pkg-config
|
2015-06-05 15:58:00 +02:00
|
|
|
PREFIX ?= /usr
|
|
|
|
DESTDIR ?=
|
2017-01-05 19:57:50 +01:00
|
|
|
SYSCONFDIR ?= /etc
|
2015-06-05 15:58:00 +02:00
|
|
|
BINDIR ?= $(PREFIX)/bin
|
|
|
|
LIBDIR ?= $(PREFIX)/lib
|
|
|
|
MANDIR ?= $(PREFIX)/share/man
|
2017-01-04 07:05:56 +01:00
|
|
|
BASHCOMPDIR ?= $(PREFIX)/share/bash-completion/completions
|
2017-01-04 19:28:28 +01:00
|
|
|
SYSTEMDUNITDIR ?= $(shell $(PKG_CONFIG) --variable=systemdsystemunitdir systemd 2>/dev/null || echo "$(PREFIX)/lib/systemd/system")
|
wg: first additions of userspace integration
This is designed to work with a server that follows this:
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
.sun_path = "/var/run/wireguard/wguserspace0.sock"
};
int fd, ret;
ssize_t len;
socklen_t socklen;
struct wgdevice *device;
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd < 0)
exit(1);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
exit(1);
for (;;) {
/* First we look at how big the next message is, so we know how much to
* allocate. Note on BSD you can instead use ioctl(fd, FIONREAD, &len). */
len = recv(fd, NULL, 0, MSG_PEEK | MSG_TRUNC);
if (len < 0) {
handle_error();
continue;
}
/* Next we allocate a buffer for the received data. */
device = NULL;
if (len) {
device = malloc(len);
if (!device) {
handle_error();
continue;
}
}
/* Finally we receive the data, storing too the return address. */
socklen = sizeof(addr);
len = recvfrom(fd, device, len, 0, (struct sockaddr *)&addr, (socklen_t *)&socklen);
if (len < 0) {
handle_error();
free(device);
continue;
}
if (!len) { /* If len is zero, it's a "get" request, so we send our device back. */
device = get_current_wireguard_device(&len);
sendto(fd, device, len, 0, (struct sockaddr *)&addr, socklen);
} else { /* Otherwise, we just received a wgdevice, so we should "set" and send back the return status. */
ret = set_current_wireguard_device(device);
sendto(fd, &ret, sizeof(ret), 0, (struct sockaddr *)&addr, socklen);
free(device);
}
}
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2016-07-19 15:26:56 +02:00
|
|
|
RUNSTATEDIR ?= /var/run
|
2017-01-04 19:28:28 +01:00
|
|
|
WITH_BASHCOMPLETION ?=
|
|
|
|
WITH_WGQUICK ?=
|
|
|
|
WITH_SYSTEMDUNITS ?=
|
|
|
|
|
|
|
|
ifeq ($(WITH_BASHCOMPLETION),)
|
2017-01-04 21:14:16 +01:00
|
|
|
ifneq ($(strip $(wildcard $(BASHCOMPDIR))),)
|
2017-01-04 19:28:28 +01:00
|
|
|
WITH_BASHCOMPLETION := yes
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
ifeq ($(WITH_WGQUICK),)
|
2017-01-04 21:14:16 +01:00
|
|
|
ifneq ($(strip $(wildcard $(BINDIR)/bash)),)
|
2017-01-04 19:28:28 +01:00
|
|
|
WITH_WGQUICK := yes
|
|
|
|
endif
|
|
|
|
ifneq ($(strip $(wildcard $(DESTDIR)/bin/bash)),)
|
|
|
|
WITH_WGQUICK := yes
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
ifeq ($(WITH_SYSTEMDUNITS),)
|
2017-01-04 21:14:16 +01:00
|
|
|
ifneq ($(strip $(wildcard $(SYSTEMDUNITDIR))),)
|
2017-01-04 19:28:28 +01:00
|
|
|
WITH_SYSTEMDUNITS := yes
|
|
|
|
endif
|
|
|
|
endif
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2018-05-14 18:14:55 +02:00
|
|
|
PLATFORM ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
|
|
|
|
|
2016-07-21 16:38:56 +02:00
|
|
|
CFLAGS ?= -O3
|
2019-12-26 12:09:53 +01:00
|
|
|
CFLAGS += -idirafter uapi
|
2018-10-09 15:23:42 +02:00
|
|
|
CFLAGS += -std=gnu99 -D_GNU_SOURCE
|
2017-01-10 04:50:42 +01:00
|
|
|
CFLAGS += -Wall -Wextra
|
2016-07-22 14:58:30 +02:00
|
|
|
CFLAGS += -MMD -MP
|
wg: first additions of userspace integration
This is designed to work with a server that follows this:
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
.sun_path = "/var/run/wireguard/wguserspace0.sock"
};
int fd, ret;
ssize_t len;
socklen_t socklen;
struct wgdevice *device;
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd < 0)
exit(1);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
exit(1);
for (;;) {
/* First we look at how big the next message is, so we know how much to
* allocate. Note on BSD you can instead use ioctl(fd, FIONREAD, &len). */
len = recv(fd, NULL, 0, MSG_PEEK | MSG_TRUNC);
if (len < 0) {
handle_error();
continue;
}
/* Next we allocate a buffer for the received data. */
device = NULL;
if (len) {
device = malloc(len);
if (!device) {
handle_error();
continue;
}
}
/* Finally we receive the data, storing too the return address. */
socklen = sizeof(addr);
len = recvfrom(fd, device, len, 0, (struct sockaddr *)&addr, (socklen_t *)&socklen);
if (len < 0) {
handle_error();
free(device);
continue;
}
if (!len) { /* If len is zero, it's a "get" request, so we send our device back. */
device = get_current_wireguard_device(&len);
sendto(fd, device, len, 0, (struct sockaddr *)&addr, socklen);
} else { /* Otherwise, we just received a wgdevice, so we should "set" and send back the return status. */
ret = set_current_wireguard_device(device);
sendto(fd, &ret, sizeof(ret), 0, (struct sockaddr *)&addr, socklen);
free(device);
}
}
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2016-07-19 15:26:56 +02:00
|
|
|
CFLAGS += -DRUNSTATEDIR="\"$(RUNSTATEDIR)\""
|
2019-12-26 16:50:55 +01:00
|
|
|
ifeq ($(DEBUG),yes)
|
2017-10-03 17:16:37 +02:00
|
|
|
CFLAGS += -g
|
|
|
|
endif
|
2020-01-08 23:59:58 +01:00
|
|
|
WIREGUARD_TOOLS_VERSION = $(patsubst v%,%,$(shell GIT_CEILING_DIRECTORIES="$(PWD)/../.." git describe --dirty 2>/dev/null))
|
|
|
|
ifneq ($(WIREGUARD_TOOLS_VERSION),)
|
|
|
|
CFLAGS += -D'WIREGUARD_TOOLS_VERSION="$(WIREGUARD_TOOLS_VERSION)"'
|
2019-12-27 18:17:00 +01:00
|
|
|
endif
|
2018-05-14 18:14:55 +02:00
|
|
|
ifeq ($(PLATFORM),linux)
|
2016-11-02 11:50:52 +01:00
|
|
|
LIBMNL_CFLAGS := $(shell $(PKG_CONFIG) --cflags libmnl 2>/dev/null)
|
|
|
|
LIBMNL_LDLIBS := $(shell $(PKG_CONFIG) --libs libmnl 2>/dev/null || echo -lmnl)
|
wg: first additions of userspace integration
This is designed to work with a server that follows this:
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
.sun_path = "/var/run/wireguard/wguserspace0.sock"
};
int fd, ret;
ssize_t len;
socklen_t socklen;
struct wgdevice *device;
fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd < 0)
exit(1);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
exit(1);
for (;;) {
/* First we look at how big the next message is, so we know how much to
* allocate. Note on BSD you can instead use ioctl(fd, FIONREAD, &len). */
len = recv(fd, NULL, 0, MSG_PEEK | MSG_TRUNC);
if (len < 0) {
handle_error();
continue;
}
/* Next we allocate a buffer for the received data. */
device = NULL;
if (len) {
device = malloc(len);
if (!device) {
handle_error();
continue;
}
}
/* Finally we receive the data, storing too the return address. */
socklen = sizeof(addr);
len = recvfrom(fd, device, len, 0, (struct sockaddr *)&addr, (socklen_t *)&socklen);
if (len < 0) {
handle_error();
free(device);
continue;
}
if (!len) { /* If len is zero, it's a "get" request, so we send our device back. */
device = get_current_wireguard_device(&len);
sendto(fd, device, len, 0, (struct sockaddr *)&addr, socklen);
} else { /* Otherwise, we just received a wgdevice, so we should "set" and send back the return status. */
ret = set_current_wireguard_device(device);
sendto(fd, &ret, sizeof(ret), 0, (struct sockaddr *)&addr, socklen);
free(device);
}
}
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2016-07-19 15:26:56 +02:00
|
|
|
CFLAGS += $(LIBMNL_CFLAGS)
|
|
|
|
LDLIBS += $(LIBMNL_LDLIBS)
|
|
|
|
endif
|
2019-02-28 17:23:45 +01:00
|
|
|
ifeq ($(PLATFORM),haiku)
|
|
|
|
LDLIBS += -lnetwork -lbsd
|
|
|
|
endif
|
2019-05-11 19:34:19 +02:00
|
|
|
ifeq ($(PLATFORM),windows)
|
|
|
|
CC := x86_64-w64-mingw32-gcc
|
|
|
|
CFLAGS += -Iwincompat/include -include wincompat/compat.h
|
|
|
|
LDLIBS += -lws2_32
|
|
|
|
wg: wincompat/libc.o wincompat/init.o
|
|
|
|
endif
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2017-10-03 17:16:37 +02:00
|
|
|
ifneq ($(V),1)
|
|
|
|
BUILT_IN_LINK.o := $(LINK.o)
|
2020-01-03 12:36:10 +01:00
|
|
|
LINK.o = @echo " LD $@";
|
2017-10-03 17:16:37 +02:00
|
|
|
LINK.o += $(BUILT_IN_LINK.o)
|
|
|
|
BUILT_IN_COMPILE.c := $(COMPILE.c)
|
2020-01-03 12:36:10 +01:00
|
|
|
COMPILE.c = @echo " CC $@";
|
2017-10-03 17:16:37 +02:00
|
|
|
COMPILE.c += $(BUILT_IN_COMPILE.c)
|
|
|
|
endif
|
|
|
|
|
2015-06-05 15:58:00 +02:00
|
|
|
wg: $(patsubst %.c,%.o,$(wildcard *.c))
|
|
|
|
|
2017-10-03 17:16:37 +02:00
|
|
|
ifneq ($(V),1)
|
2015-06-05 15:58:00 +02:00
|
|
|
clean:
|
2020-01-03 12:36:10 +01:00
|
|
|
@echo " CLEAN {wg,*.o,*.d}"
|
2017-10-03 17:16:37 +02:00
|
|
|
@$(RM) wg *.o *.d
|
|
|
|
else
|
|
|
|
clean:
|
|
|
|
$(RM) wg *.o *.d
|
|
|
|
endif
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
install: wg
|
2018-05-23 05:20:34 +02:00
|
|
|
@install -v -d "$(DESTDIR)$(BINDIR)" && install -v -m 0755 wg "$(DESTDIR)$(BINDIR)/wg"
|
|
|
|
@install -v -d "$(DESTDIR)$(MANDIR)/man8" && install -v -m 0644 man/wg.8 "$(DESTDIR)$(MANDIR)/man8/wg.8"
|
2017-01-04 19:28:28 +01:00
|
|
|
@[ "$(WITH_BASHCOMPLETION)" = "yes" ] || exit 0; \
|
2018-05-23 05:20:34 +02:00
|
|
|
install -v -d "$(DESTDIR)$(BASHCOMPDIR)" && install -v -m 0644 completion/wg.bash-completion "$(DESTDIR)$(BASHCOMPDIR)/wg"
|
2017-01-04 19:28:28 +01:00
|
|
|
@[ "$(WITH_WGQUICK)" = "yes" ] || exit 0; \
|
2018-05-23 05:20:34 +02:00
|
|
|
install -v -m 0755 wg-quick/$(PLATFORM).bash "$(DESTDIR)$(BINDIR)/wg-quick" && install -v -m 0700 -d "$(DESTDIR)$(SYSCONFDIR)/wireguard"
|
2017-01-04 19:28:28 +01:00
|
|
|
@[ "$(WITH_WGQUICK)" = "yes" ] || exit 0; \
|
2018-05-23 05:20:34 +02:00
|
|
|
install -v -m 0644 man/wg-quick.8 "$(DESTDIR)$(MANDIR)/man8/wg-quick.8"
|
2017-01-04 19:28:28 +01:00
|
|
|
@[ "$(WITH_WGQUICK)" = "yes" -a "$(WITH_BASHCOMPLETION)" = "yes" ] || exit 0; \
|
2018-05-23 05:20:34 +02:00
|
|
|
install -v -m 0644 completion/wg-quick.bash-completion "$(DESTDIR)$(BASHCOMPDIR)/wg-quick"
|
2017-01-04 19:28:28 +01:00
|
|
|
@[ "$(WITH_WGQUICK)" = "yes" -a "$(WITH_SYSTEMDUNITS)" = "yes" ] || exit 0; \
|
2018-05-23 05:20:34 +02:00
|
|
|
install -v -d "$(DESTDIR)$(SYSTEMDUNITDIR)" && install -v -m 0644 systemd/wg-quick@.service "$(DESTDIR)$(SYSTEMDUNITDIR)/wg-quick@.service"
|
2015-06-05 15:58:00 +02:00
|
|
|
|
2019-12-26 16:54:25 +01:00
|
|
|
check: clean
|
|
|
|
scan-build --html-title=wireguard-tools -maxloop 100 --view --keep-going $(MAKE) wg
|
|
|
|
|
2020-01-03 21:22:22 +01:00
|
|
|
all: wg
|
|
|
|
.DEFAULT_GOAL: all
|
2019-12-27 18:17:00 +01:00
|
|
|
.PHONY: clean install check
|
2015-06-05 15:58:00 +02:00
|
|
|
|
|
|
|
-include *.d
|