4483a087871126c2a6e524e58f12871ea9ef6d28
[openwrt/openwrt.git] / package / network / config / gre / files / gre.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . /lib/functions/network.sh
6 . ../netifd-proto.sh
7 init_proto "$@"
8 }
9
10 gre_generic_setup() {
11 local cfg="$1"
12 local mode="$2"
13 local local="$3"
14 local remote="$4"
15 local link="$5"
16 local mtu ttl tos zone ikey okey icsum ocsum iseqno oseqno
17 json_get_vars mtu ttl tos zone ikey okey icsum ocsum iseqno oseqno
18
19 [ -z "$zone" ] && zone="wan"
20
21 proto_init_update "$link" 1
22
23 proto_add_tunnel
24 json_add_string mode "$mode"
25 json_add_int mtu "${mtu:-1280}"
26 [ -n "$df" ] && json_add_boolean df "$df"
27 json_add_int ttl "${ttl:-64}"
28 [ -n "$tos" ] && json_add_string tos "$tos"
29 json_add_string local "$local"
30 json_add_string remote "$remote"
31 [ -n "$tunlink" ] && json_add_string link "$tunlink"
32 json_add_string info "${ikey:-0},${okey:-0},${icsum:-0},${ocsum:-0},${iseqno:-0},${oseqno:-0}"
33 proto_close_tunnel
34
35 proto_add_data
36 [ -n "$zone" ] && json_add_string zone "$zone"
37 proto_close_data
38
39 proto_send_update "$cfg"
40 }
41
42 gre_setup() {
43 local cfg="$1"
44 local mode="$2"
45
46 local ipaddr peeraddr
47 json_get_vars df ipaddr peeraddr tunlink
48
49 [ -z "$peeraddr" ] && {
50 proto_notify_error "$cfg" "MISSING_ADDRESS"
51 proto_block_restart "$cfg"
52 exit
53 }
54
55 ( proto_add_host_dependency "$cfg" "$peeraddr" "$tunlink" )
56
57 [ -z "$ipaddr" ] && {
58 local wanif="$tunlink"
59 if [ -z $wanif ] && ! network_find_wan wanif; then
60 proto_notify_error "$cfg" "NO_WAN_LINK"
61 exit
62 fi
63
64 if ! network_get_ipaddr ipaddr "$wanif"; then
65 proto_notify_error "$cfg" "NO_WAN_LINK"
66 exit
67 fi
68 }
69
70 [ -z "$df" ] && df="1"
71
72 gre_generic_setup $cfg $mode $ipaddr $peeraddr "gre-$cfg"
73 }
74
75 proto_gre_setup() {
76 local cfg="$1"
77
78 gre_setup $cfg "greip"
79 }
80
81 proto_gretap_setup() {
82 local cfg="$1"
83
84 local network
85 json_get_vars network
86
87 gre_setup $cfg "gretapip"
88
89 json_init
90 json_add_string name "gre-$cfg"
91 json_add_boolean link-ext 0
92 json_close_object
93
94 for i in $network; do
95 ubus call network.interface."$i" add_device "$(json_dump)"
96 done
97 }
98
99 grev6_setup() {
100 local cfg="$1"
101 local mode="$2"
102
103 local ip6addr peer6addr weakif
104 json_get_vars ip6addr peer6addr tunlink weakif
105
106 [ -z "$peer6addr" ] && {
107 proto_notify_error "$cfg" "MISSING_ADDRESS"
108 proto_block_restart "$cfg"
109 exit
110 }
111
112 ( proto_add_host_dependency "$cfg" "$peer6addr" "$tunlink" )
113
114 [ -z "$ip6addr" ] && {
115 local wanif="$tunlink"
116 if [ -z $wanif ] && ! network_find_wan6 wanif; then
117 proto_notify_error "$cfg" "NO_WAN_LINK"
118 exit
119 fi
120
121 if ! network_get_ipaddr6 ip6addr "$wanif"; then
122 [ -z "$weakif" ] && weakif="lan"
123 if ! network_get_ipaddr6 ip6addr "$weakif"; then
124 proto_notify_error "$cfg" "NO_WAN_LINK"
125 exit
126 fi
127 fi
128 }
129
130 gre_generic_setup $cfg $mode $ip6addr $peer6addr "grev6-$cfg"
131 }
132
133 proto_grev6_setup() {
134 local cfg="$1"
135
136 grev6_setup $cfg "greip6"
137 }
138
139 proto_grev6tap_setup() {
140 local cfg="$1"
141
142 local network
143 json_get_vars network
144
145 grev6_setup $cfg "gretapip6"
146
147 json_init
148 json_add_string name "grev6-$cfg"
149 json_add_boolean link-ext 0
150 json_close_object
151
152 for i in $network; do
153 ubus call network.interface."$i" add_device "$(json_dump)"
154 done
155 }
156
157 gretap_generic_teardown() {
158 local network
159 json_get_vars network
160
161 json_init
162 json_add_string name "$1"
163 json_add_boolean link-ext 0
164 json_close_object
165
166 for i in $network; do
167 ubus call network.interface."$i" remove_device "$(json_dump)"
168 done
169 }
170
171 proto_gre_teardown() {
172 local cfg="$1"
173 }
174
175 proto_gretap_teardown() {
176 local cfg="$1"
177
178 gretap_generic_teardown "gre-$cfg"
179 }
180
181 proto_grev6_teardown() {
182 local cfg="$1"
183 }
184
185 proto_grev6tap_teardown() {
186 local cfg="$1"
187
188 gretap_generic_teardown "grev6-$cfg"
189 }
190
191 gre_generic_init_config() {
192 no_device=1
193 available=1
194
195 proto_config_add_int "mtu"
196 proto_config_add_int "ttl"
197 proto_config_add_string "tos"
198 proto_config_add_string "tunlink"
199 proto_config_add_string "zone"
200 proto_config_add_int "ikey"
201 proto_config_add_int "okey"
202 proto_config_add_boolean "icsum"
203 proto_config_add_boolean "ocsum"
204 proto_config_add_boolean "iseqno"
205 proto_config_add_boolean "oseqno"
206 }
207
208 proto_gre_init_config() {
209 gre_generic_init_config
210 proto_config_add_string "ipaddr"
211 proto_config_add_string "peeraddr"
212 proto_config_add_boolean "df"
213 }
214
215 proto_gretap_init_config() {
216 proto_gre_init_config
217 proto_config_add_string "network"
218 }
219
220 proto_grev6_init_config() {
221 gre_generic_init_config
222 proto_config_add_string "ip6addr"
223 proto_config_add_string "peer6addr"
224 proto_config_add_string "weakif"
225 }
226
227 proto_grev6tap_init_config() {
228 proto_grev6_init_config
229 proto_config_add_string "network"
230 }
231
232 [ -n "$INCLUDE_ONLY" ] || {
233 [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gre
234 [ -f /lib/modules/$(uname -r)/gre.ko ] && add_protocol gretap
235 [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6
236 [ -f /lib/modules/$(uname -r)/ip6_gre.ko ] && add_protocol grev6tap
237 }