aboutsummaryrefslogtreecommitdiff
path: root/net/wireguard-tools/files/wireguard_lite.in
blob: 791a83c3ce555667bb5e68a134483f3b76fe5163 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/sh

# PROVIDE: wireguard
# REQUIRE: NETWORKING
# KEYWORD: shutdown
#
# wireguard_enable (bool):        Set to "YES" to enable wireguard.
#                                 (default: "NO")
#
# wireguard_interfaces (str):     List of interfaces to bring up/down
#                                 on start/stop. (eg: "wg0 wg1")
#                                 (default: "")
# wireguard_confdir (str):        Config directory that contains wg0.conf
#                                 (default: "%%PREFIX%%/etc/wireguard")
# wireguard_<iface>_ips (str):    List of IP Addresses for iface
# wireguard_<iface>_routes (str): List of Routes for this iface
# wireguard_<iface>_mtu (str):    MTU for iface (default: "1500")

. /etc/rc.subr

name=wireguard
rcvar=wireguard_enable
extra_commands="reload"

start_cmd="${name}_start"
stop_cmd="${name}_stop"
reload_cmd="${name}_reload"

wireguard_start()
{
	for interface in ${wireguard_interfaces}; do
		load_rc_config wireguard_${interface}

		eval wireguard_ips="\${wireguard_${interface}_ips}"
		eval wireguard_routes="\${wireguard_${interface}_routes}"
		eval wireguard_mtu="\${wireguard_${interface}_mtu}"

		ifconfig ${interface} create
		%%PREFIX%%/bin/wg setconf ${interface} ${wireguard_confdir}/${interface}.conf

		for ip in ${wireguard_ips}; do
			if [ "${ip#*:}" != "${ip}" ]; then
				ifconfig ${interface} inet6 ${ip} alias
			else
				ifconfig ${interface} inet ${ip} alias
			fi
		done

		if [ ! -z "${wireguard_mtu}" ]; then
			ifconfig ${interface} mtu ${wireguard_mtu}
		fi

		ifconfig ${interface} up

		for route in ${wireguard_routes}; do
			if [ "${route#*:}" != "${route}" ]; then
				route -q -n add -inet6 ${route} -interface ${interface}
			else
				route -q -n add -inet ${route} -interface ${interface}
			fi
		done
	done
}

wireguard_stop()
{
	for interface in ${wireguard_interfaces}; do
		load_rc_config wireguard_${interface}

		eval wireguard_routes="\${wireguard_${interface}_routes}"

		for route in ${wireguard_routes}; do
			if [ "${route#*:}" != "${route}" ]; then
				route -q -n delete -inet6 ${route} -interface ${interface}
			else
				route -q -n delete -inet ${route} -interface ${interface}
			fi
		done

		ifconfig ${interface} down

		ifconfig ${interface} destroy
	done

	if kldstat -q -n if_wg; then
		if ! kldunload if_wg > /dev/null 2>&1; then
			warn "Can't unload if_wg module."
			return 1
		fi
	fi
}

wireguard_reload()
{
	for interface in ${wireguard_interfaces}; do
		%%PREFIX%%/bin/wg syncconf ${interface} ${wireguard_confdir}/${interface}.conf
	done
}

load_rc_config $name

: ${wireguard_enable="NO"}
: ${wireguard_interfaces=""}
: ${wireguard_confdir="%%PREFIX%%/etc/wireguard"}

run_rc_command "$1"