Browse Source

Add fastd, a very small VPN daemon

Matthias Schiffer 10 years ago
parent
commit
605d0884bc
5 changed files with 809 additions and 0 deletions
  1. 82
    0
      net/fastd/Config.in
  2. 163
    0
      net/fastd/Makefile
  3. 143
    0
      net/fastd/files/fastd.config
  4. 420
    0
      net/fastd/files/fastd.init
  5. 1
    0
      net/fastd/files/fastd.upgrade

+ 82
- 0
net/fastd/Config.in View File

@@ -0,0 +1,82 @@
1
+menu "Configuration"
2
+	depends on PACKAGE_fastd
3
+
4
+config FASTD_ENABLE_METHOD_CIPHER_TEST
5
+	bool "Enable cipher-test method provider"
6
+	depends on PACKAGE_fastd
7
+	default n
8
+
9
+config FASTD_ENABLE_METHOD_COMPOSED_GMAC
10
+	bool "Enable composed-gmac method provider"
11
+	depends on PACKAGE_fastd
12
+	default y
13
+
14
+config FASTD_ENABLE_METHOD_GENERIC_GMAC
15
+	bool "Enable generic-gmac method provider"
16
+	depends on PACKAGE_fastd
17
+	default y
18
+
19
+config FASTD_ENABLE_METHOD_GENERIC_POLY1305
20
+	bool "Enable generic-poly1305 method provider"
21
+	depends on PACKAGE_fastd
22
+	default n
23
+
24
+config FASTD_ENABLE_METHOD_NULL
25
+	bool "Enable null method"
26
+	depends on PACKAGE_fastd
27
+	default y
28
+
29
+config FASTD_ENABLE_METHOD_XSALSA20_POLY1305
30
+	bool "Enable xsalsa20-poly1305 method"
31
+	depends on PACKAGE_fastd
32
+	default n
33
+
34
+
35
+config FASTD_ENABLE_CIPHER_AES128_CTR
36
+	bool "Enable the AES128-CTR cipher"
37
+	depends on PACKAGE_fastd
38
+	default n
39
+
40
+config FASTD_ENABLE_CIPHER_NULL
41
+	bool "Enable the null cipher"
42
+	depends on PACKAGE_fastd
43
+	default y
44
+
45
+config FASTD_ENABLE_CIPHER_SALSA20
46
+	bool "Enable the Salsa20 cipher"
47
+	depends on PACKAGE_fastd
48
+	default n
49
+
50
+config FASTD_ENABLE_CIPHER_SALSA2012
51
+	bool "Enable the Salsa20/12 cipher"
52
+	depends on PACKAGE_fastd
53
+	default y
54
+
55
+
56
+config FASTD_ENABLE_MAC_GHASH
57
+	bool "Enable the GHASH message authentication code"
58
+	depends on PACKAGE_fastd
59
+	default y
60
+
61
+
62
+config FASTD_WITH_CMDLINE_USER
63
+	bool "Include support for setting user/group related options on the command line"
64
+	depends on PACKAGE_fastd
65
+	default n
66
+
67
+config FASTD_WITH_CMDLINE_LOGGING
68
+	bool "Include support for setting logging related options on the command line"
69
+	depends on PACKAGE_fastd
70
+	default n
71
+
72
+config FASTD_WITH_CMDLINE_OPERATION
73
+	bool "Include support for setting options related to the VPN operation (like mode, interface, encryption method) on the command line"
74
+	depends on PACKAGE_fastd
75
+	default n
76
+
77
+config FASTD_WITH_CMDLINE_COMMANDS
78
+	bool "Include support for setting handler scripts (e.g. --on-up) on the command line"
79
+	depends on PACKAGE_fastd
80
+	default n
81
+
82
+endmenu

+ 163
- 0
net/fastd/Makefile View File

@@ -0,0 +1,163 @@
1
+#
2
+# Copyright (C) 2012-2014 OpenWrt.org
3
+#
4
+# This is free software, licensed under the GNU General Public License v2.
5
+# See /LICENSE for more information.
6
+#
7
+
8
+include $(TOPDIR)/rules.mk
9
+
10
+PKG_NAME:=fastd
11
+PKG_VERSION:=12
12
+PKG_RELEASE:=1
13
+
14
+PKG_MAINTAINER:=Matthias Schiffer <mschiffer@universe-factory.net>
15
+PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.xz
16
+PKG_SOURCE_URL:=https://projects.universe-factory.net/attachments/download/73
17
+PKG_MD5SUM:=1dadc61f4d712a10844afcb9b9f49a41
18
+
19
+PKG_CONFIG_DEPENDS:=\
20
+	CONFIG_FASTD_ENABLE_METHOD_CIPHER_TEST \
21
+	CONFIG_FASTD_ENABLE_METHOD_COMPOSED_GMAC \
22
+	CONFIG_FASTD_ENABLE_METHOD_GENERIC_GMAC \
23
+	CONFIG_FASTD_ENABLE_METHOD_GENERIC_POLY1305 \
24
+	CONFIG_FASTD_ENABLE_METHOD_NULL \
25
+	CONFIG_FASTD_ENABLE_METHOD_XSALSA20_POLY1305 \
26
+	CONFIG_FASTD_ENABLE_CIPHER_AES128_CTR \
27
+	CONFIG_FASTD_ENABLE_CIPHER_NULL \
28
+	CONFIG_FASTD_ENABLE_CIPHER_SALSA20 \
29
+	CONFIG_FASTD_ENABLE_CIPHER_SALSA2012 \
30
+	CONFIG_FASTD_ENABLE_MAC_GHASH \
31
+	CONFIG_FASTD_WITH_CMDLINE_USER \
32
+	CONFIG_FASTD_WITH_CMDLINE_LOGGING \
33
+	CONFIG_FASTD_WITH_CMDLINE_OPERATION \
34
+	CONFIG_FASTD_WITH_CMDLINE_COMMANDS
35
+
36
+
37
+PKG_BUILD_DEPENDS:=nacl libuecc
38
+
39
+include $(INCLUDE_DIR)/package.mk
40
+include $(INCLUDE_DIR)/cmake.mk
41
+
42
+define Package/fastd
43
+  SECTION:=net
44
+  CATEGORY:=Network
45
+  DEPENDS:=+kmod-tun +librt +libpthread
46
+  TITLE:=Fast and Secure Tunneling Daemon
47
+  URL:=https://projects.universe-factory.net/projects/fastd
48
+  SUBMENU:=VPN
49
+endef
50
+
51
+define Package/fastd/config
52
+  source "$(SOURCE)/Config.in"
53
+endef
54
+
55
+TARGET_CFLAGS += -ffunction-sections -fdata-sections
56
+TARGET_LDFLAGS += -Wl,--gc-sections
57
+
58
+CMAKE_OPTIONS += \
59
+	-DCMAKE_BUILD_TYPE:STRING=MINSIZEREL \
60
+	-DWITH_METHOD_CIPHER_TEST:BOOL=FALSE \
61
+	-DWITH_METHOD_COMPOSED_GMAC:BOOL=FALSE \
62
+	-DWITH_METHOD_GENERIC_GMAC:BOOL=FALSE \
63
+	-DWITH_METHOD_GENERIC_POLY1305:BOOL=FALSE \
64
+	-DWITH_METHOD_NULL:BOOL=FALSE \
65
+	-DWITH_METHOD_XSALSA20_POLY1305:BOOL=FALSE \
66
+	-DWITH_CIPHER_AES128_CTR:BOOL=FALSE \
67
+	-DWITH_CIPHER_NULL:BOOL=FALSE \
68
+	-DWITH_CIPHER_SALSA20:BOOL=FALSE \
69
+	-DWITH_CIPHER_SALSA2012:BOOL=FALSE \
70
+	-DWITH_MAC_GHASH:BOOL=FALSE \
71
+	-DWITH_CMDLINE_USER:BOOL=FALSE \
72
+	-DWITH_CMDLINE_LOGGING:BOOL=FALSE \
73
+	-DWITH_CMDLINE_OPERATION:BOOL=FALSE \
74
+	-DWITH_CMDLINE_COMMANDS:BOOL=FALSE \
75
+	-DWITH_CAPABILITIES:BOOL=FALSE
76
+
77
+
78
+ifeq ($(CONFIG_FASTD_ENABLE_METHOD_CIPHER_TEST),y)
79
+CMAKE_OPTIONS += -DWITH_METHOD_CIPHER_TEST:BOOL=TRUE
80
+endif
81
+
82
+ifeq ($(CONFIG_FASTD_ENABLE_METHOD_COMPOSED_GMAC),y)
83
+CMAKE_OPTIONS += -DWITH_METHOD_COMPOSED_GMAC:BOOL=TRUE
84
+endif
85
+
86
+ifeq ($(CONFIG_FASTD_ENABLE_METHOD_GENERIC_GMAC),y)
87
+CMAKE_OPTIONS += -DWITH_METHOD_GENERIC_GMAC:BOOL=TRUE
88
+endif
89
+
90
+ifeq ($(CONFIG_FASTD_ENABLE_METHOD_GENERIC_POLY1305),y)
91
+CMAKE_OPTIONS += -DWITH_METHOD_GENERIC_POLY1305:BOOL=TRUE
92
+endif
93
+
94
+ifeq ($(CONFIG_FASTD_ENABLE_METHOD_NULL),y)
95
+CMAKE_OPTIONS += -DWITH_METHOD_NULL:BOOL=TRUE
96
+endif
97
+
98
+ifeq ($(CONFIG_FASTD_ENABLE_METHOD_XSALSA20_POLY1305),y)
99
+CMAKE_OPTIONS += -DWITH_METHOD_XSALSA20_POLY1305:BOOL=TRUE
100
+endif
101
+
102
+
103
+ifeq ($(CONFIG_FASTD_ENABLE_CIPHER_AES128_CTR),y)
104
+CMAKE_OPTIONS += -DWITH_CIPHER_AES128_CTR:BOOL=TRUE
105
+endif
106
+
107
+ifeq ($(CONFIG_FASTD_ENABLE_CIPHER_NULL),y)
108
+CMAKE_OPTIONS += -DWITH_CIPHER_NULL:BOOL=TRUE
109
+endif
110
+
111
+ifeq ($(CONFIG_FASTD_ENABLE_CIPHER_SALSA20),y)
112
+CMAKE_OPTIONS += -DWITH_CIPHER_SALSA20:BOOL=TRUE
113
+endif
114
+
115
+ifeq ($(CONFIG_FASTD_ENABLE_CIPHER_SALSA2012),y)
116
+CMAKE_OPTIONS += -DWITH_CIPHER_SALSA2012:BOOL=TRUE
117
+endif
118
+
119
+
120
+ifeq ($(CONFIG_FASTD_ENABLE_MAC_GHASH),y)
121
+CMAKE_OPTIONS += -DWITH_MAC_GHASH:BOOL=TRUE
122
+endif
123
+
124
+
125
+ifeq ($(CONFIG_FASTD_WITH_CMDLINE_USER),y)
126
+CMAKE_OPTIONS += -DWITH_CMDLINE_USER:BOOL=TRUE
127
+endif
128
+
129
+ifeq ($(CONFIG_FASTD_WITH_CMDLINE_LOGGING),y)
130
+CMAKE_OPTIONS += -DWITH_CMDLINE_LOGGING:BOOL=TRUE
131
+endif
132
+
133
+ifeq ($(CONFIG_FASTD_WITH_CMDLINE_OPERATION),y)
134
+CMAKE_OPTIONS += -DWITH_CMDLINE_OPERATION:BOOL=TRUE
135
+endif
136
+
137
+ifeq ($(CONFIG_FASTD_WITH_CMDLINE_COMMANDS),y)
138
+CMAKE_OPTIONS += -DWITH_CMDLINE_COMMANDS:BOOL=TRUE
139
+endif
140
+
141
+
142
+define Package/fastd/description
143
+ Fast and secure tunneling daemon, which is optimized on small code size and few dependencies
144
+endef
145
+
146
+define Package/fastd/conffiles
147
+/etc/config/fastd
148
+endef
149
+
150
+define Package/fastd/install
151
+	$(INSTALL_DIR) $(1)/usr/bin
152
+	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/fastd $(1)/usr/bin/
153
+
154
+	$(INSTALL_DIR) $(1)/etc/init.d/
155
+	$(INSTALL_BIN) files/fastd.init $(1)/etc/init.d/fastd
156
+	$(INSTALL_DIR) $(1)/etc/config
157
+	$(INSTALL_CONF) files/fastd.config $(1)/etc/config/fastd
158
+	$(INSTALL_DIR) $(1)/etc/fastd
159
+	$(INSTALL_DIR) $(1)/lib/upgrade/keep.d
160
+	$(INSTALL_DATA) files/fastd.upgrade $(1)/lib/upgrade/keep.d/fastd
161
+endef
162
+
163
+$(eval $(call BuildPackage,fastd))

+ 143
- 0
net/fastd/files/fastd.config View File

@@ -0,0 +1,143 @@
1
+package fastd
2
+
3
+config fastd sample_config
4
+
5
+	# Set to 1 to enable this instance:
6
+	option enabled 0
7
+
8
+	# Sets a static config file, optional
9
+	# Options set via UCI have higher priority that statically configured ones
10
+#	list config '/etc/fastd/sample_config/fastd.conf'
11
+
12
+	# Configures a single static peer from a configuration file
13
+#	list config_peer '/etc/fastd/sample_config/sample_peer.conf'
14
+
15
+	# Sets an additional directory from which peers configurations are read
16
+	# The peer list can be reloaded without restarting fastd
17
+	# Peer can either be configured via UCI (see examples below) or via peer dirs
18
+	# Can't be used in tun mode
19
+#	list config_peer_dir '/etc/fastd/sample_config/peers'
20
+
21
+	# Sets the log level
22
+	# Possible values: error, warn, info, verbose, debug
23
+	# Default: info
24
+	option syslog_level 'info'
25
+
26
+	# IP address and port of the local end, optional
27
+	# 'any' can be used to bind to both IPv4 and IPv6
28
+	# If no port is given fastd will bind to a random port
29
+#	list bind 'any:1337'
30
+#	list bind '0.0.0.0:1337'
31
+#	list bind '[::]:1337'
32
+
33
+	# "method null" uses no encryption or MAC
34
+	# "method xsalsa20-poly1305" uses the XSalsa20 encryption ad the Poly1305 MAC
35
+	list method 'xsalsa20-poly1305'
36
+
37
+	# "mode tap" will create an ethernet tunnel (tap device),
38
+	# "mode tun" will create an IP tunnel (tun device).
39
+	option mode 'tap'
40
+
41
+	# Set the name of the tunnel interface to use
42
+	option interface 'tap0'
43
+#	option interface 'tun0'
44
+#	option interface 'fastd0'
45
+
46
+	# Sets the MTU of the tunnel interface, default is 1500
47
+	# 1426 is a good value that avoids fragmentation for the xsalsa20-poly1305 method
48
+	# when the tunnel uses an IPv4 connection on a line with an MTU of 1492 or higher
49
+	option mtu 1426
50
+
51
+	# Enables direct forwaring of packets between peers
52
+	# WARNING: Only enable this if you know what you are doing, as this can lead to forwarding loops!
53
+	option forward 0
54
+
55
+	# Disable for compatiblity with fastd v10 and older
56
+	option secure_handshakes 1
57
+
58
+	# Set a packet mark to filter for with iptables or ip rules
59
+#	option packet_mark 42
60
+
61
+	# Limits the maximum number of connections, optional
62
+#	option peer_limit 5
63
+
64
+	# The secret key
65
+	# A keypair can be generated with `fastd --generate-key`
66
+	# When the corresponding public key is lost it can be recovered with `/etc/init.d/fastd show-key <config name>`
67
+#	option secret '0000000000000000000000000000000000000000000000000000000000000000'
68
+
69
+	# Sets the user to run fastd as. Defaults to root
70
+#	option user 'daemon'
71
+
72
+	# Sets the group to run fastd as. Defaults to the user's primary group
73
+#	option group 'daemon'
74
+
75
+	# If set to 1, the logs won't contain peers' IP addresses
76
+#	option hide_ip_addresses '0'
77
+
78
+	# If set to 1, the logs won't contain peers' MAC addresses
79
+#	option hide_mac_addresses '0'
80
+
81
+	# Read the documentation about this one. Only ever useful in severly broken networks.
82
+#	option pmtu ''
83
+
84
+	# command to configure IP addresses etc. after the tunnel interface is up; $1 will be the interface name (optional)
85
+#	option up ''
86
+
87
+	# command to execute before the tunnel interface is set down; $1 will be the interface name (optional)
88
+#	option down ''
89
+
90
+
91
+config peer sample_peer
92
+
93
+	# Set to 1 to enable this peer
94
+	# In tap mode peers can be reloaded dynamically
95
+	option enabled 0
96
+
97
+	# Controls which instance this peer is associated with
98
+	option net 'sample_config'
99
+
100
+	# Controls which peer group this peer belongs to, optional
101
+	# For most use cases peer groups aren't necessary
102
+#	option group 'sample_group'
103
+
104
+	# The peer's public key
105
+	option key '0000000000000000000000000000000000000000000000000000000000000000'
106
+
107
+	# A remote specification consists of an address or a hostname, and a port
108
+	# When a hostname is given, it is recommended to specify the address family to use
109
+	# It is possible to specify no, one or multiple remotes
110
+	# (but all entries must designate the same host as the public key must be unique)
111
+#	list remote '192.0.2.1:1337'
112
+#	list remote '[2001:db8::1]:1337'
113
+#	list remote '"example.com" port 1337'
114
+#	list remote 'ipv4 "example.com" port 1337'
115
+#	list remote 'ipv6 "example.com" port 1337'
116
+
117
+	# Setting float to 1 allow incoming connections with this key from other addresses/hostnames/ports than the specified remotes
118
+#	option float 0
119
+
120
+
121
+config peer_group sample_group
122
+
123
+	# Set to 1 to enable this peer group
124
+	option enabled 0
125
+
126
+	# Controls which instance this peer group is associated with
127
+	# Peer groups can't be used in tun mode
128
+	option net 'sample_config'
129
+
130
+	# Allows configuring nested groups
131
+#	option parent 'other_group'
132
+
133
+	# Includes another config file inside the peer group definition
134
+#	list config '/etc/fastd/sample_config/sample_group.conf'
135
+
136
+	# Configures a single static peer from a configuration file
137
+#	list config_peer '/etc/fastd/sample_config/sample_peer.conf'
138
+
139
+	# Configures an additional peer directory for this group
140
+#	list config_peer_dir '/etc/fastd/sample_config/peers2'
141
+
142
+	# Limits the maximum number of connections to peers in this group (optional)
143
+#	option peer_limit 5

+ 420
- 0
net/fastd/files/fastd.init View File

@@ -0,0 +1,420 @@
1
+#!/bin/sh /etc/rc.common
2
+# Copyright (C) 2012-2013 OpenWrt.org
3
+
4
+START=95
5
+
6
+EXTRA_COMMANDS="up down show_key generate_key"
7
+
8
+LIST_SEP="
9
+"
10
+TMP_FASTD=/tmp/fastd
11
+FASTD_COMMAND=/usr/bin/fastd
12
+
13
+
14
+section_enabled() {
15
+	config_get_bool enabled "$1" 'enabled' 0
16
+	[ $enabled -gt 0 ]
17
+}
18
+
19
+error() {
20
+	echo "${initscript}:" "$@" 1>&2
21
+}
22
+
23
+get_key_instance() {
24
+	local s="$1"
25
+
26
+	config_get secret "$s" secret
27
+	if [ "$secret" = 'generate' ]; then
28
+		secret=`"$FASTD_COMMAND" --generate-key --machine-readable`
29
+		uci -q set fastd."$s".secret="$secret" && uci -q commit fastd
30
+	fi
31
+
32
+	echo "$secret"
33
+}
34
+
35
+
36
+escape_string() {
37
+	local t=${1//\\/\\\\}
38
+	echo -n "\"${t//\"/\\\"}\""
39
+}
40
+
41
+guard_value() {
42
+	local t=${1//[^-a-z0-9\[\].:]/}
43
+	echo -n "$t"
44
+}
45
+
46
+guard_remote() {
47
+	local t=${1//[^-a-zA-Z0-9\[\].:\"% ]/}
48
+	local quotes=${t//[^\"]/}
49
+	if [ "${#quotes}" = 0 -o "${#quotes}" = 2 ]; then
50
+		echo -n "$t"
51
+	fi
52
+}
53
+
54
+yes_no() {
55
+	case "$1" in
56
+		0|no|off|false|disabled) echo -n no;;
57
+		*) echo -n yes;;
58
+	esac
59
+}
60
+
61
+config_string_config='include $(escape_string "$value");'
62
+config_string_config_peer='include peer $(escape_string "$value");'
63
+config_string_config_peer_dir='include peers from $(escape_string "$value");'
64
+config_string_bind='bind $(guard_value "$value");'
65
+config_string_method='method $(escape_string "$value");'
66
+config_string_syslog_level='log to syslog level $(guard_value "$value");'
67
+config_string_mode='mode $(guard_value "$value");'
68
+config_string_interface='interface $(escape_string "$value");'
69
+config_string_mtu='mtu $(guard_value "$value");'
70
+config_string_peer_limit='peer limit $(guard_value "$value");'
71
+config_string_user='user $(escape_string "$value");'
72
+config_string_group='group $(escape_string "$value");'
73
+config_string_pmtu='pmtu $(yes_no "$value");'
74
+config_string_forward='forward $(yes_no "$value");'
75
+config_string_hide_ip_addresses='hide ip addresses $(yes_no "$value");'
76
+config_string_hide_mac_addresses='hide mac addresses $(yes_no "$value");'
77
+config_string_secure_handshakes='secure handshakes $(yes_no "$value");'
78
+config_string_packet_mark='packet mark $(guard_value "$value");'
79
+
80
+config_string_peer='peer $(escape_string "$value") {'
81
+config_string_peer_group='peer group $(escape_string "$value") {'
82
+
83
+peer_string_key='key $(escape_string "$value");'
84
+peer_string_float='float $(yes_no "$value");'
85
+peer_string_remote='remote $(guard_remote "$value");'
86
+
87
+generate_option() {
88
+	local __string=$(eval echo \"\$$2\")
89
+	local value="$1";
90
+	eval echo "\"$__string\""
91
+}
92
+
93
+append_option() {
94
+	local v; local len; local s="$1"; local prefix="$2"; local p="$3"
95
+
96
+	config_get len "$s" "${p}_LENGTH"
97
+
98
+	if [ -z "$len" ]; then
99
+		config_get v "$s" "$p"
100
+		[ -n "$v" ] && generate_option "$v" "${prefix}_string_${p}"
101
+	else
102
+		config_list_foreach "$s" "$p" generate_option "${prefix}_string_${p}"
103
+	fi
104
+}
105
+
106
+append_options() {
107
+	local p; local s="$1"; local prefix="$2"; shift; shift
108
+	for p in $*; do
109
+		append_option "$s" "$prefix" "$p"
110
+	done
111
+}
112
+
113
+
114
+generate_config_secret() {
115
+	echo "secret $(escape_string "$1");"
116
+}
117
+
118
+
119
+generate_peer_config() {
120
+	local peer="$1"
121
+
122
+	# These options are deprecated
123
+	config_get address "$peer" address
124
+	config_get hostname "$peer" hostname
125
+	config_get address_family "$peer" address_family
126
+	config_get port "$peer" port
127
+
128
+	if [ "$address" -o "$hostname" ]; then
129
+		if [ -z "$port" ]; then
130
+			error "peer $peer: address or hostname, but no port given"
131
+			return 1
132
+		fi
133
+
134
+		if [ "$hostname" ]; then
135
+			generate_option peer_string_remote "$address_family \"$hostname\" port $port"
136
+		fi
137
+
138
+		if [ "$address" ]; then
139
+			generate_option peer_string_remote "$address port $port"
140
+		fi
141
+	fi
142
+
143
+	append_options "$peer" peer \
144
+		key float remote
145
+}
146
+
147
+generate_single_peer_config() {
148
+	local peer="$1"; local net="$2"
149
+
150
+	config_get peer_net "$peer" net
151
+	config_get peer_group "$peer" group
152
+	[ "$net" = "$peer_net" -a "$peer_group" = '' ] || return 0
153
+
154
+	section_enabled "$peer" || return 0
155
+
156
+	generate_option "$peer" config_string_peer
157
+	generate_peer_config "$peer"
158
+	echo '}'
159
+}
160
+
161
+create_peer_config() {
162
+	local peer="$1"; local net="$2"; local group="$3"; local path="$4"
163
+
164
+	config_get peer_net "$peer" net
165
+	config_get peer_group "$peer" group
166
+	[ "$group" = "$peer_group" ] || return 0
167
+
168
+	if [ "$net" != "$peer_net" ]; then
169
+		[ -z "$group" ] || error "warning: the peer group of peer '$peer' doesn't match its net, the peer will be ignored"
170
+		return 0
171
+	fi
172
+
173
+	section_enabled "$peer" || return 0
174
+
175
+	generate_peer_config "$peer" >"$path/$peer"
176
+}
177
+
178
+update_peer_group() {
179
+	local net="$1"; local group_dir="$2"; local group="$3"; local update_only="$4"
180
+	local path="$TMP_FASTD/fastd.$net/$group_dir"
181
+
182
+	rm -rf "$path"
183
+	mkdir -p "$path"
184
+
185
+	config_foreach create_peer_config 'peer' "$net" "$group" "$path"
186
+
187
+	if [ -z "$update_only" ]; then
188
+		generate_option "$path" config_string_config_peer_dir
189
+	fi
190
+
191
+	config_foreach generate_peer_group_config 'peer_group' "$net" "$group_dir" "$update_only" "$group"
192
+}
193
+
194
+generate_peer_group_config() {
195
+	local group="$1"; local net="$2"; local group_dir="$3%$group"; local update_only="$4"; local parent="$5"
196
+
197
+	config_get group_net "$group" net
198
+	config_get group_parent "$group" parent
199
+	[ "$parent" = "$group_parent" ] || return 0
200
+
201
+	if [ "$net" != "$peer_net" ]; then
202
+		[ -z "$parent" ] || error "warning: the parent of peer group '$group' doesn't match its net, the peer group will be ignored"
203
+		return 0
204
+	fi
205
+
206
+	section_enabled "$group" || return 0
207
+
208
+	if [ -z "$update_only" ]; then
209
+		generate_option "$group" config_string_peer_group
210
+		append_options "$group" config \
211
+			config config_peer config_peer_dir peer_limit
212
+	fi
213
+
214
+	update_peer_group "$net" "$group_dir" "$group" "$update_only"
215
+
216
+	if [ -z "$update_only" ]; then
217
+		echo '}'
218
+	fi
219
+}
220
+
221
+update_peer_groups() {
222
+	local net="$1"; local update_only="$2"
223
+
224
+	update_peer_group "$net" 'peers' '' "$update_only"
225
+}
226
+
227
+generate_config() {
228
+	local s="$1"
229
+
230
+	generate_option 'info' config_string_syslog_level
231
+
232
+	append_options "$s" config \
233
+		config config_peer config_peer_dir bind method syslog_level mode interface mtu peer_limit \
234
+		user group pmtu forward hide_ip_addresses hide_mac_addresses secure_handshakes packet_mark
235
+
236
+	config_get mode "$s" mode
237
+
238
+	if [ "$mode" = "tun" ]; then
239
+		config_foreach generate_single_peer_config 'peer' "$s"
240
+	else
241
+		update_peer_groups "$s"
242
+	fi
243
+}
244
+
245
+
246
+generate_key_instance() {
247
+	local s="$1"
248
+
249
+	config_get secret "$s" secret
250
+	if [ -z "$secret" -o "$secret" = 'generate' ]; then
251
+		secret=`fastd --generate-key --machine-readable`
252
+		uci -q set fastd."$s".secret="$secret" && uci -q commit fastd
253
+	fi
254
+
255
+	generate_config_secret "$secret" | "$FASTD_COMMAND" --config - --show-key --machine-readable
256
+}
257
+
258
+show_key_instance() {
259
+	local s="$1"
260
+
261
+	local secret=`get_key_instance "$s"`
262
+	if [ -z "$secret" ]; then
263
+		error "$s: secret is not set"
264
+		return 1
265
+	fi
266
+
267
+	generate_config_secret "$secret" | "$FASTD_COMMAND" --config - --show-key --machine-readable
268
+}
269
+
270
+start_instance() {
271
+	local s="$1"
272
+
273
+	section_enabled "$s" || return 1
274
+
275
+	SERVICE_PID_FILE="/var/run/fastd.$s.pid"
276
+
277
+	config_get interface "$s" interface
278
+	if [ -z "$interface" ]; then
279
+		error "$s: interface is not set"
280
+		return 1
281
+	fi
282
+
283
+	if ifconfig "$interface" &>/dev/null; then
284
+		error "$s: interface '$interface' is already in use"
285
+		return 1
286
+	fi
287
+
288
+	config_get mode "$s" mode
289
+	if [ -z "$mode" ]; then
290
+		error "$s: mode is not set"
291
+		return 1
292
+	fi
293
+
294
+	local secret=`get_key_instance "$s"`
295
+	if [ -z "$secret" ]; then
296
+		error "$s: secret is not set"
297
+		return 1
298
+	fi
299
+
300
+	rm -f "$SERVICE_PID_FILE"
301
+	touch "$SERVICE_PID_FILE"
302
+
303
+	config_get user "$s" user
304
+	if [ "$user" ]; then
305
+		chown "$user" "$SERVICE_PID_FILE"
306
+	fi
307
+
308
+	(generate_config_secret "$secret"; generate_config "$s") | service_start "$FASTD_COMMAND" --config - --daemon --pid-file "$SERVICE_PID_FILE"
309
+
310
+	if ! ifconfig "$interface" >/dev/null 2>&1; then
311
+		error "$s: startup failed"
312
+		return 1
313
+	fi
314
+
315
+	config_get up "$s" up
316
+	[ -n "$up" ] && sh -c "$up" - "$interface"
317
+}
318
+
319
+stop_instance() {
320
+	local s="$1"
321
+
322
+	section_enabled "$s" || return 1
323
+
324
+	SERVICE_PID_FILE="/var/run/fastd.$s.pid"
325
+
326
+	config_get interface "$s" interface
327
+	if [ -z "$interface" ]; then
328
+		error "$s: interface is not set"
329
+		return 1
330
+	fi
331
+
332
+	if ! ifconfig "$interface" &>/dev/null; then
333
+		error "$s: interface '$interface' does not exist"
334
+		return 1
335
+	fi
336
+
337
+	config_get down "$s" down
338
+	[ -n "$down" ] && sh -c "$down" - "$interface"
339
+
340
+	service_stop "$FASTD_COMMAND"
341
+
342
+	rm -rf "$TMP_FASTD/fastd.$s"
343
+}
344
+
345
+reload_instance() {
346
+	local s="$1"
347
+
348
+	section_enabled "$s" || return 1
349
+
350
+	config_get mode "$s" mode
351
+	[ "$mode" = "tun" ] && return 1
352
+
353
+	update_peer_groups "$s" true
354
+
355
+	SERVICE_PID_FILE="/var/run/fastd.$s.pid"
356
+	service_reload "$FASTD_COMMAND"
357
+}
358
+
359
+start() {
360
+	config_load 'fastd'
361
+	config_foreach start_instance 'fastd'
362
+}
363
+
364
+stop() {
365
+	config_load 'fastd'
366
+	config_foreach stop_instance 'fastd'
367
+}
368
+
369
+reload() {
370
+	config_load 'fastd'
371
+	config_foreach reload_instance 'fastd'
372
+}
373
+
374
+up() {
375
+	local exists
376
+	local instance
377
+	config_load 'fastd'
378
+	for instance in "$@"; do
379
+		config_get exists "$instance" 'TYPE'
380
+		if [ "$exists" = 'fastd' ]; then
381
+			start_instance "$instance"
382
+		fi
383
+	done
384
+}
385
+
386
+down() {
387
+	local exists
388
+	local instance
389
+	config_load 'fastd'
390
+	for instance in "$@"; do
391
+		config_get exists "$instance" 'TYPE'
392
+		if [ "$exists" = 'fastd' ]; then
393
+			stop_instance "$instance"
394
+		fi
395
+	done
396
+}
397
+
398
+show_key() {
399
+	local exists
400
+	local instance
401
+	config_load 'fastd'
402
+	for instance in "$@"; do
403
+		config_get exists "$instance" 'TYPE'
404
+		if [ "$exists" = 'fastd' ]; then
405
+			show_key_instance "$instance"
406
+		fi
407
+	done
408
+}
409
+
410
+generate_key() {
411
+	local exists
412
+	local instance
413
+	config_load 'fastd'
414
+	for instance in "$@"; do
415
+		config_get exists "$instance" 'TYPE'
416
+		if [ "$exists" = 'fastd' ]; then
417
+			generate_key_instance "$instance"
418
+		fi
419
+	done
420
+}

+ 1
- 0
net/fastd/files/fastd.upgrade View File

@@ -0,0 +1 @@
1
+/etc/fastd/