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