nodogsplash2: update to version 2.1.1 (#376)
[feed/routing.git] / bird-openwrt / bird6-openwrt / src / init.d / bird6-lib.sh
1 # Bird6-OpenWRT Library - Functions used in /etc/init.d/bird6 script.
2 #
3 #
4 # Copyright (C) 2014-2017 - Eloi Carbo
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19
20
21 # Function: writeToConfig $1
22 # $1 string.
23 # Allows to write in the $BIRD_CONFIG file, the string $1. This function does not check the $1 string.
24 # Example: writeToConfig "value: $N"
25 writeToConfig() {
26 echo "$1" >> ${BIRD_CONFIG}
27 }
28
29
30 # Function: write $1 $2
31 # $1 string. $2 string.
32 # This function checks if $2 is empty. If not, it writes the string $1 in the $BIRD_CONFIG file.
33 # Use write function to check if $1, value found inside $2, is not empty and can be written in the configuration file.
34 # Example: N=""; write "value: $N" $N;
35 write() {
36 [ -n "$2" ] && writeToConfig "$1"
37 }
38
39
40 #Function: write_bool $1 $2
41 # $1 string; $2 boolean
42 # This function checks if $2 is true or false and write the $1 string into $BIRD_CONFIG file.
43 # The function writes a # before the $2 string if its false.
44 # Example: local N=0; write_bool $N
45 write_bool() {
46 [ "$2" == 0 ] && writeToConfig "# $1;" || writeToConfig " $1;"
47 }
48
49
50 # Function: get $1 $2
51 # $1 string. $2 string
52 # This function uses the external UCI function "config_get $result $section $option" to obtain a string value from UCI config file.
53 # To use this function, use the same name of the UCI option for the variable.
54 # Example: UCI (option id 'abcd'); local id; get id $section
55 get() {
56 config_get $1 $2 $1
57 }
58
59
60 # Function: get_bool $1 $2
61 # $1 boolean. $2 string
62 # This function uses the external UCI function "config_get_bool $result $section $option" to obtain a boolean value from UCI config file.
63 # To use this function, use the same name of the UCI option for the variable $1.
64 # Example: UCI (option use_ipv6 '1'); local use_ipv6; get use_ipv6 $section
65 get_bool() {
66 config_get_bool $1 $2 $1
67 }
68
69
70 # Function: multipath_list $1
71 # $1 string
72 # This function writes the $1 string in the multipath routes.
73 multipath_list() {
74 write " via $1" $1
75 }
76
77
78 # Function: prepare_tables $1
79 # $1 string
80 # This function gets each "table" section in the UCI configuration and sets each option in the bird6.conf file.
81 # $1 is set as the ID of the current UCI table section
82 prepare_tables() {
83 local section="$1"; local name
84
85 get name ${section}
86
87 write "table ${name};" ${name}
88 }
89
90
91 # Function: prepare_global $1
92 # $1 string
93 # This function gets each "global" section in the UCI configuration and sets each option in the bird6.conf file.
94 # $1 is set as the ID of the current UCI global section. prepare_global is the first configuration set in the bird6.conf and removes the old file.
95 prepare_global () {
96 local section="$1"
97 local log_file; local log; local debug; local router_id; local table
98 local listen_bgp_addr; local listen_bgp_port; local listen_bgp_dual
99
100 # Remove old configuration file
101 rm -f "${BIRD_CONFIG}"
102
103 get log_file ${section}
104 get log ${section}
105 get debug ${section}
106 get router_id ${section}
107 get table ${section}
108 get listen_bgp_addr ${section}
109 get listen_bgp_port ${section}
110 get listen_bgp_dual ${section}
111
112 # First line of the NEW configuration file
113 echo "#Bird6 configuration using UCI:" > ${BIRD_CONFIG}
114 writeToConfig " "
115 #TODO: Set Syslog as receiver if empty
116 # LOGF="${log_file:-syslog]}"
117 #TODO: If $log/$debug are empty, set to off
118 if [ -n "${log_file}" -a -n "${log}" ]; then
119 firstEntry="${log:0:3}"
120 if [ "${firstEntry}" = "all" -o "${firstEntry}" = "off" ]; then
121 writeToConfig 'log "'${log_file}'" '${firstEntry}';'
122 else
123 logEntries=$(echo ${log} | tr " " ",")
124 writeToConfig "log \"${log_file}\" { ${logEntries} };"
125 fi
126 fi
127
128 if [ -n "${debug}" ]; then
129 firstEntry="${debug:0:3}"
130 if [ "${firstEntry}" = "all" -o "${firstEntry}" = "off" ]; then
131 writeToConfig "debug protocols ${firstEntry};"
132 else
133 debugEntries=$(echo ${debug} | tr " " ",")
134 writeToConfig "debug protocols { ${debugEntries} };"
135 fi
136 fi
137 writeToConfig " "
138 writeToConfig "#Router ID"
139 write "router id ${router_id};" ${router_id}
140 writeToConfig " "
141 writeToConfig "#Secondary tables"
142 config_foreach prepare_tables 'table'
143 if [ -n "${listen_bgp_dual}" -o "${listen_bgp_dual}" = "0" ]; then
144 writeToConfig "listen bgp ${listen_bgp_addr} ${listen_bgp_port} v6only;"
145 else
146 writeToConfig "listen bgp ${listen_bgp_addr} ${listen_bgp_port} dual;"
147 fi
148 writeToConfig " "
149 }
150
151
152 # Function: prepare_routes $1
153 # $1 string
154 # This function gets each "route" section in the UCI configuration and sets each option in the bird6.conf file.
155 # $1 is set as the ID of the current UCI route section. Each type of route has its own treatment.
156 prepare_routes() {
157 local instance; local prefix; local via; local type
158 local section="$1"
159 local protoInstance="$2"
160
161 get instance ${section}
162 get type ${section}
163 get prefix ${section}
164
165 if [ "${instance}" = "${protoInstance}" ]; then
166 case "${type}" in
167 "router")
168 get via ${section}
169 [ -n "${prefix}" -a -n "${via}" ] && writeToConfig " route ${prefix} via ${via};"
170 ;;
171 "special")
172 get attribute ${section}
173 [ -n "${prefix}" -a -n "${attribute}" ] && writeToConfig " route ${prefix} ${attribute};"
174 ;;
175 "iface")
176 get iface ${section}
177 [ -n "${prefix}" -a -n "${iface}" ] && writeToConfig ' route '${prefix}' via "'${iface}'";'
178 ;;
179 "multipath")
180 write " route ${prefix} multipath" ${prefix}
181 config_list_foreach ${section} l_via multipath_list
182 writeToConfig " ;"
183 ;;
184 esac
185 fi
186 }
187
188
189 # Function: prepare_kernel $1
190 # $1 string
191 # This function gets each "kernel" protocol section in the UCI configuration and sets each option in the bird6.conf file.
192 # $1 is set as the ID of the current UCI kernel section.
193 prepare_kernel() {
194 local section="$1"
195 local disabled; local table; local kernel_table; local import; local export
196 local scan_time; local persist; local learn
197
198 get_bool disabled ${section}
199 get table ${section}
200 get import ${section}
201 get export ${section}
202 get scan_time ${section}
203 get kernel_table ${section}
204 get learn ${section}
205 get persist ${section}
206
207 write "#${section} configuration:" ${section}
208 writeToConfig "protocol kernel ${section} {" ${section}
209 write_bool disabled ${disabled}
210 write " table ${table};" ${table}
211 write " kernel table ${kernel_table};" ${kernel_table}
212 write_bool learn ${learn}
213 write_bool persist ${persist}
214 write " scan time ${scan_time};" ${scan_time}
215 write " import ${import};" ${import}
216 write " export ${export};" ${export}
217 writeToConfig "}"
218 writeToConfig " "
219 }
220
221
222 # Function: prepare_static $1
223 # $1 string
224 # This function gets each "static" protocol section in the UCI configuration and sets each option in the bird6.conf file.
225 # $1 is set as the ID of the current UCI static section.
226 prepare_static() {
227 local section="$1"
228 local disabled; local table
229
230 get disabled ${section}
231 get table ${section}
232
233 if [ "${disabled}" -eq 0 ]; then
234 writeToConfig "#${section} configration:" ${section}
235 writeToConfig "protocol static {"
236 write " table ${table};" ${table}
237 config_foreach prepare_routes 'route' ${section}
238 writeToConfig "}"
239 writeToConfig " "
240 fi
241 }
242
243
244 # Function: prepare_direct $1
245 # $1 string
246 # This function gets each "direct" protocol section in the UCI configuration and sets each option in the bird6.conf file.
247 # $1 is set as the ID of the current UCI direct section.
248 prepare_direct() {
249 local section="$1"
250 local disabled; local interface
251
252 get disabled ${section}
253 get interface ${section}
254
255 write "#${section} configuration:" ${section}
256 writeToConfig "protocol direct {"
257 write_bool disabled ${disabled}
258 write " interface ${interface};" ${interface}
259 writeToConfig "}"
260 writeToConfig " "
261 }
262
263
264 # Function: prepare_pipe $1
265 # $1 string
266 # This function gets each "pipe" protocol section in the UCI configuration and sets each option in the bird6.conf file.
267 # $1 is set as the ID of the current UCI direct section.
268 prepare_pipe() {
269 local section="$1"
270 local disabled; local table; local peer_table; local mode; local import; local export
271
272 get disabled ${section}
273 get peer_table ${section}
274 get mode ${section}
275 get table ${section}
276 get import ${section}
277 get export ${section}
278
279 write "#${section} configuration:" ${section}
280 writeToConfig "protocol pipe ${section} {" ${section}
281 write_bool disabled ${disabled}
282 write " table ${table};" ${table}
283 write " peer table ${peer_table};" ${peer_table}
284 write " mode ${mode};" ${mode}
285 write " import ${import};" ${import}
286 write " export ${export};" ${export}
287 writeToConfig "}"
288 writeToConfig " "
289 }
290
291
292 # Function: prepare_device $1
293 # $1 string
294 # This function gets each "device" protocol section in the UCI configuration and sets each option in the bird6.conf file.
295 # $1 is set as the ID of the current UCI device section.
296 prepare_device() {
297 local section="$1"
298 local disabled; local scan_time
299
300 get disabled ${section}
301 get scan_time ${section}
302
303 write "#${section} configuration:" ${section}
304 writeToConfig "protocol device {"
305 write_bool disabled ${disabled}
306 write " scan time ${scan_time};" ${scan_time}
307 writeToConfig "}"
308 writeToConfig " "
309 }
310
311
312 # Function: prepare_bgp_template $1
313 # $1 string
314 # This function gets each "bgp_template" protocol section in the UCI configuration and sets each option in the bird6.conf file.
315 # $1 is set as the ID of the current UCI bgp_template section.
316 # Careful! Template options will be replaced by "instance" options if there is any match.
317 prepare_bgp_template() {
318 local section="$1"
319 local disabled; local table; local import; local export; local local_address
320 local local_as; local neighbor_address; local neighbor_as; local source_address
321 local next_hop_self; local next_hop_keep; local rr_client; local rr_cluster_id
322 local import_limit; local import_limit_action; local export_limit; local export_limit_action
323 local receive_limit; local receive_limit_action; local igp_table
324
325 get_bool disabled ${section}
326 get_bool next_hop_self ${section}
327 get_bool next_hop_keep ${section}
328 get table ${section}
329 get import ${section}
330 get export ${section}
331 get local_address ${section}
332 get local_as ${section}
333 get igp_table ${section}
334 get rr_client ${section}
335 get rr_cluster_id ${section}
336 get import_limit ${section}
337 get import_limit_action ${section}
338 get export_limit ${section}
339 get export_limit_action ${section}
340 get receive_limit ${section}
341 get receive_limit_action ${section}
342 get neighbor_address ${section}
343 get neighbor_as ${section}
344
345 writeToConfig "#${section} template:"
346 writeToConfig "template bgp ${section} {"
347 [ -n "${disabled}" ] && write_bool disabled ${disabled}
348 write " table ${table};" ${table}
349 write " local as ${local_as};" ${local_as}
350 write " source address ${local_address};" ${local_address}
351 write " import ${import};" ${import}
352 write " export ${export};" ${export}
353 if [ -n "${next_hop_self}" ]; then
354 [ "${next_hop_self}" = "1" ] && writeToConfig " next hop self;" || writeToConfig "# next hop self;"
355 fi
356 if [ -n "${next_hop_keep}" ]; then
357 [ "${next_hop_keep}" = "1" ] && writeToConfig " next hop keep;" || writeToConfig "# next hop keep;"
358 fi
359 [ -n "${igp_table}" ] && writeToConfig " igp table ${igp_table};"
360 [ "${rr_client}" = "1" ] && writeToConfig " rr client;" || writeToConfig "# rr client;"
361 write " rr cluster id ${rr_cluster_id};" ${rr_cluster_id}
362 if [ -n "${import_limit}" -a "${import_limit}" > "0" ]; then
363 [ -z "${import_limit_action}" ] && ${import_limit_action} = "warn"
364 writeToConfig " import limit ${import_limit} action ${import_limit_action};"
365 fi
366 if [ -n "${export_limit}" -a "${export_limit}" > "0" ]; then
367 [ -z "${export_limit_action}" ] && ${export_limit_action} = "warn"
368 writeToConfig " export limit ${export_limit} action ${export_limit_action};"
369 fi
370 if [ -n "${receive_limit}" -a "${receive_limit}" > "0" ]; then
371 [ -z "${receive_limit_action}" ] && ${receive_limit_action} = "warn"
372 writeToConfig " receive limit ${receive_limit} action ${receive_limit_action};"
373 fi
374 [ -n "${neighbor_address}" -a -n "${neighbor_as}" ] && writeToConfig " neighbor ${neighbor_address} as ${neighbor_as};"
375 writeToConfig "}"
376 writeToConfig " "
377 }
378
379
380 # Function: prepare_bgp $1
381 # $1 string
382 # This function gets each "bgp" protocol section in the UCI configuration and sets each option in the bird6.conf file.
383 # $1 is set as the ID of the current UCI bgp section.
384 # Careful! The options set in bgp instances overlap bgp_template ones.
385 prepare_bgp() {
386 local section="$1"
387 local disabled; local table; local template; local description; local import
388 local export; local local_address; local local_as; local neighbor_address
389 local neighbor_as; local rr_client; local rr_cluster_id; local import_limit
390 local import_limit_action; local export_limit; local export_limit_action
391 local receive_limit; local receive_limit_action; local igp_table
392
393 get disabled ${section}
394 get table ${section}
395 get template ${section}
396 get description ${section}
397 get import ${section}
398 get export ${section}
399 get local_address ${section}
400 get local_as ${section}
401 get igp_table ${section}
402 get rr_client ${section}
403 get rr_cluster_id ${section}
404 get import_limit ${section}
405 get import_limit_action ${section}
406 get export_limit ${section}
407 get export_limit_action ${section}
408 get receive_limit ${section}
409 get receive_limit_action ${section}
410 get neighbor_address ${section}
411 get neighbor_as ${section}
412
413 writeToConfig "#${section} configuration:"
414 [ -n "${template}" ] && writeToConfig "protocol bgp ${section} from ${template} {" || writeToConfig "protocol bgp ${section} {"
415 [ -n "${disabled}" ] && write_bool disabled ${disabled}
416 write " table ${table};" ${table}
417 write " local as ${local_as};" ${local_as}
418 write " source address ${local_address};" ${local_address}
419 write " import ${import};" ${import}
420 write " export ${export};" ${export}
421 if [ -n "${next_hop_self}" ]; then
422 [ "${next_hop_self}" = "1" ] && writeToConfig " next hop self;" || writeToConfig "# next hop self;"
423 fi
424 if [ -n "${next_hop_keep}" ]; then
425 [ "${next_hop_keep}" = "1" ] && writeToConfig " next hop keep;" || writeToConfig "# next hop keep;"
426 fi
427 [ -n "${igp_table}" ] && writeToConfig " igp table ${igp_table};"
428 [ "${rr_client}" = "1" ] && writeToConfig " rr client;" || writeToConfig "# rr client;"
429 write " rr cluster id ${rr_cluster_id};" ${rr_cluster_id}
430 if [ -n "${import_limit}" -a "${import_limit}" > "0" ]; then
431 [ -z "${import_limit_action}" ] && ${import_limit_action} = "warn"
432 writeToConfig " import limit ${import_limit} action ${import_limit_action};"
433 fi
434 if [ -n "${export_limit}" -a "${export_limit}" > "0" ]; then
435 [ -z "${export_limit_action}" ] && ${export_limit_action} = "warn"
436 writeToConfig " export limit ${export_limit} action ${export_limit_action};"
437 fi
438 if [ -n "${receive_limit}" -a "${receive_limit}" > "0" ]; then
439 [ -z "${receive_limit_action}" ] && ${receive_limit_action} = "warn"
440 writeToConfig " receive limit ${receive_limit} action ${receive_limit_action};"
441 fi
442 [ -n "${neighbor_address}" -a -n "${neighbor_as}" ] && writeToConfig " neighbor ${neighbor_address} as ${neighbor_as};"
443 writeToConfig "}"
444 writeToConfig " "
445 }
446
447
448 # Function: gather_filters
449 # This function gets all the FILES under /filters folder and adds
450 # them into the config as %include elements on top of the file
451 # If there are no filters, the section will remain empty.
452 gather_filters() {
453 writeToConfig "#Filters Section:"
454 for filter in $(find /etc/${BIRD}/filters -type f); do
455 writeToConfig "include \"${filter}\";"
456 done
457 writeToConfig "#End of Filters --"
458 writeToConfig " "
459 }
460
461
462 # Function: gather_functions
463 # This function gets all the FILES under /functions folder and adds
464 # them into the config as %include elements on top of the file
465 # If there are no filters, the section will remain empty.
466 gather_functions() {
467 writeToConfig "#Functions Section:"
468 for func in $(find /etc/${BIRD}/functions -type f); do
469 writeToConfig "include \"${func}\";"
470 done
471 writeToConfig "#End of Functions --"
472 writeToConfig " "
473 }