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