aboutsummaryrefslogtreecommitdiffstats
path: root/package/network/config/gre/files/gre.sh
blob: b57d5d4cdb3434467184aa08da77e6d0cdf3637c (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/bin/sh

[ -n "$INCLUDE_ONLY" ] || {
	. /lib/functions.sh
	. /lib/functions/network.sh
	. ../netifd-proto.sh
	init_proto "$@"
}

gre_generic_setup() {
	local cfg="$1"
	local mode="$2"
	local local="$3"
	local remote="$4"
	local link="$5"
	local mtu ipv6 ttl tos zone ikey okey icsum ocsum iseqno oseqno multicast
	json_get_vars mtu ipv6 ttl tos zone ikey okey icsum ocsum iseqno oseqno multicast

	[ -z "$multicast" ] && multicast=1

	proto_init_update "$link" 1

	proto_add_tunnel
	json_add_string mode "$mode"
	json_add_int mtu "${mtu:-1280}"
	json_add_boolean ipv6 "${ipv6:-1}"
	[ -n "$df" ] && json_add_boolean df "$df"
	[ -n "$ttl" ] && json_add_int ttl "$ttl"
	[ -n "$tos" ] && json_add_string tos "$tos"
	json_add_boolean multicast "$multicast"
	json_add_string local "$local"
	json_add_string remote "$remote"
	[ -n "$tunlink" ] && json_add_string link "$tunlink"

	json_add_object 'data'
	[ -n "$ikey" ] && json_add_int ikey "$ikey"
	[ -n "$okey" ] && json_add_int okey "$okey"
	[ -n "$icsum" ] && json_add_boolean icsum "$icsum"
	[ -n "$ocsum" ] && json_add_boolean ocsum "$ocsum"
	[ -n "$iseqno" ] && json_add_boolean iseqno "$iseqno"
	[ -n "$oseqno" ] && json_add_boolean oseqno "$oseqno"
	[ -n "$encaplimit" ] && json_add_string encaplimit "$encaplimit"
	json_close_object

	proto_close_tunnel

	proto_add_data
	[ -n "$zone" ] && json_add_string zone "$zone"
	proto_close_data

	proto_send_update "$cfg"
}

gre_setup() {
	local cfg="$1"
	local mode="$2"
	local remoteip

	local ipaddr peeraddr
	json_get_vars df ipaddr peeraddr tunlink nohostroute

	[ -z "$peeraddr" ] && {
		proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
		proto_block_restart "$cfg"
		exit
	}

	remoteip=$(resolveip -t 10 -4 "$peeraddr")

	if [ -z "$remoteip" ]; then
		proto_notify_error "$cfg" "PEER_RESOLVE_FAIL"
		exit
	fi

	for ip in $remoteip; do
		peeraddr=$ip
		break
	done

	if [ "${nohostroute}" != "1" ]; then
		( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
	fi

	[ -z "$ipaddr" ] && {
		local wanif="$tunlink"
		if [ -z $wanif ] && ! network_find_wan wanif; then
			proto_notify_error "$cfg" "NO_WAN_LINK"
			exit
		fi

		if ! network_get_ipaddr ipaddr "$wanif"; then
			proto_notify_error "$cfg" "NO_WAN_LINK"
			exit
		fi
	}

	[ -z "$df" ] && df="1"

	case "$mode" in
		gretapip)
			gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4t-$cfg"
			;;
		*)
			gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre4-$cfg"
			;;
	esac
}

proto_gre_setup() {
	local cfg="$1"

	gre_setup $cfg "greip"
}

proto_gretap_setup() {
	local cfg="$1"

	local network
	json_get_vars network

	gre_setup $cfg "gretapip"

	json_init
	json_add_string name "gre4t-$cfg"
	json_add_boolean link-ext 0
	json_close_object

	for i in $network; do
		ubus call network.interface."$i" add_device "$(json_dump)"
	done
}

grev6_setup() {
	local cfg="$1"
	local mode="$2"
	local remoteip6

	local ip6addr peer6addr weakif
	json_get_vars ip6addr peer6addr tunlink weakif encaplimit nohostroute

	[ -z "$peer6addr" ] && {
		proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
		proto_block_restart "$cfg"
		exit
	}

	remoteip6=$(resolveip -t 10 -6 "$peer6addr")

	if [ -z "$remoteip6" ]; then
		proto_notify_error "$cfg" "PEER_RESOLVE_FAIL"
		exit
	fi

	for ip6 in $remoteip6; do
		peer6addr=$ip6
		break
	done

	if [ "${nohostroute}" != "1" ]; then
		( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" )
	fi

	[ -z "$ip6addr" ] && {
		local wanif="$tunlink"
		if [ -z $wanif ] && ! network_find_wan6 wanif; then
			proto_notify_error "$cfg" "NO_WAN_LINK"
			exit
		fi

		if ! network_get_ipaddr6 ip6addr "$wanif"; then
			[ -z "$weakif" ] && weakif="lan"
			if ! network_get_ipaddr6 ip6addr "$weakif"; then
				proto_notify_error "$cfg" "NO_WAN_LINK"
				exit
			fi
		fi
	}

	case "$mode" in
		gretapip6)
			gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6t-$cfg"
			;;
		*)
			gre_generic_setup $cfg $mode $ip6addr $peer6addr "gre6-$cfg"
			;;
	esac
}

proto_grev6_setup() {
	local cfg="$1"

	grev6_setup $cfg "greip6"
}

proto_grev6tap_setup() {
	local cfg="$1"

	local network
	json_get_vars network

	grev6_setup $cfg "gretapip6"

	json_init
	json_add_string name "gre6t-$cfg"
	json_add_boolean link-ext 0
	json_close_object

	for i in $network; do
		ubus call network.interface."$i" add_device "$(json_dump)"
	done
}

gretap_generic_teardown() {
	local network
	json_get_vars network

	json_init
	json_add_string name "$1"
	json_add_boolean link-ext 0
	json_close_object

	for i in $network; do
		ubus call network.interface."$i" remove_device "$(json_dump)"
	done
}

proto_gre_teardown() {
	local cfg="$1"
}

proto_gretap_teardown() {
	local cfg="$1"

	gretap_generic_teardown "gre4t-$cfg"
}

proto_grev6_teardown() {
	local cfg="$1"
}

proto_grev6tap_teardown() {
	local cfg="$1"

	gretap_generic_teardown "gre6t-$cfg"
}

gre_generic_init_config() {
	no_device=1
	available=1

	proto_config_add_int "mtu"
	proto_config_add_boolean "ipv6"
	proto_config_add_int "ttl"
	proto_config_add_string "tos"
	proto_config_add_string "tunlink"
	proto_config_add_string "zone"
	proto_config_add_int "ikey"
	proto_config_add_int "okey"
	proto_config_add_boolean "icsum"
	proto_config_add_boolean "ocsum"
	proto_config_add_boolean "iseqno"
	proto_config_add_boolean "oseqno"
	proto_config_add_boolean "multicast"
}

proto_gre_init_config() {
	gre_generic_init_config
	proto_config_add_string "ipaddr"
	proto_config_add_string "peeraddr"
	proto_config_add_boolean "df"
	proto_config_add_boolean "nohostroute"
}

proto_gretap_init_config() {
	proto_gre_init_config
	proto_config_add_string "network"
}

proto_grev6_init_config() {
	gre_generic_init_config
	proto_config_add_string "ip6addr"
	proto_config_add_string "peer6addr"
	proto_config_add_string "weakif"
	proto_config_add_string "encaplimit"
	proto_config_add_boolean "nohostroute"
}

proto_grev6tap_init_config() {
	proto_grev6_init_config
	proto_config_add_string "network"
}

[ -n "$INCLUDE_ONLY" ] || {
	[ -d /sys/module/ip_gre ] && { add_protocol gre; add_protocol gretap; }
	[ -d /sys/module/ip6_gre ] && { add_protocol grev6; add_protocol grev6tap; }
}