1 # Bird6-OpenWRT Library - Functions used in /etc/init.d/bird6 script.
4 # Copyright (C) 2014-2017 - Eloi Carbo
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.
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.
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/>.
21 # Function: writeToConfig $1
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"
26 echo "$1" >> ${BIRD_CONFIG}
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;
36 [ -n "$2" ] && writeToConfig
"$1"
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
46 [ "$2" == 0 ] && writeToConfig
"# $1;" || writeToConfig
" $1;"
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
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
66 config_get_bool
$1 $2 $1
70 # Function: multipath_list $1
72 # This function writes the $1 string in the multipath routes.
78 # Function: prepare_tables $1
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
83 local section
="$1"; local name
87 write "table ${name};" ${name}
91 # Function: prepare_global $1
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.
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
100 # Remove old configuration file
101 rm -f "${BIRD_CONFIG}"
103 get log_file
${section}
106 get router_id
${section}
108 get listen_bgp_addr
${section}
109 get listen_bgp_port
${section}
110 get listen_bgp_dual
${section}
112 # First line of the NEW configuration file
113 echo "#Bird6 configuration using UCI:" > ${BIRD_CONFIG}
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}';'
123 logEntries
=$
(echo ${log} |
tr " " ",")
124 writeToConfig
"log \"${log_file}\" { ${logEntries} };"
128 if [ -n "${debug}" ]; then
129 firstEntry
="${debug:0:3}"
130 if [ "${firstEntry}" = "all" -o "${firstEntry}" = "off" ]; then
131 writeToConfig
"debug protocols ${firstEntry};"
133 debugEntries
=$
(echo ${debug} |
tr " " ",")
134 writeToConfig
"debug protocols { ${debugEntries} };"
138 writeToConfig
"#Router ID"
139 write "router id ${router_id};" ${router_id}
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;"
146 writeToConfig
"listen bgp ${listen_bgp_addr} ${listen_bgp_port} dual;"
152 # Function: prepare_routes $1
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.
157 local instance
; local prefix
; local via
; local type
159 local protoInstance
="$2"
161 get instance
${section}
163 get prefix
${section}
165 if [ "${instance}" = "${protoInstance}" ]; then
169 [ -n "${prefix}" -a -n "${via}" ] && writeToConfig " route ${prefix} via ${via};"
172 get attribute
${section}
173 [ -n "${prefix}" -a -n "${attribute}" ] && writeToConfig " route ${prefix} ${attribute};"
177 [ -n "${prefix}" -a -n "${iface}" ] && writeToConfig ' route '${prefix}' via "'${iface}'";'
180 write " route ${prefix} multipath" ${prefix}
181 config_list_foreach ${section} l_via multipath_list
189 # Function: prepare_kernel $1
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.
195 local disabled; local table; local kernel_table; local import; local export
196 local scan_time; local persist; local learn
198 get_bool disabled ${section}
200 get import ${section}
201 get export ${section}
202 get scan_time ${section}
203 get kernel_table ${section}
205 get persist ${section}
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}
222 # Function: prepare_static $1
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.
228 local disabled; local table
230 get disabled ${section}
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}
244 # Function: prepare_direct $1
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.
250 local disabled; local interface
252 get disabled ${section}
253 get interface ${section}
255 write "#${section} configuration:" ${section}
256 writeToConfig "protocol direct {"
257 write_bool disabled ${disabled}
258 write " interface ${interface};" ${interface}
264 # Function: prepare_pipe $1
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.
270 local disabled; local table; local peer_table; local mode; local import; local export
272 get disabled ${section}
273 get peer_table ${section}
276 get import ${section}
277 get export ${section}
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}
292 # Function: prepare_device $1
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.
298 local disabled; local scan_time
300 get disabled ${section}
301 get scan_time ${section}
303 write "#${section} configuration:" ${section}
304 writeToConfig "protocol device {"
305 write_bool disabled ${disabled}
306 write " scan time ${scan_time};" ${scan_time}
312 # Function: prepare_bgp_template $1
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() {
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
325 get_bool disabled ${section}
326 get_bool next_hop_self ${section}
327 get_bool next_hop_keep ${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}
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;"
356 if [ -n "${next_hop_keep}" ]; then
357 [ "${next_hop_keep}" = "1" ] && writeToConfig " next hop keep;" || writeToConfig "# next hop keep;"
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};"
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};"
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};"
374 [ -n "${neighbor_address}" -a -n "${neighbor_as}" ] && writeToConfig " neighbor ${neighbor_address} as ${neighbor_as};"
380 # Function: prepare_bgp $1
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.
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
393 get disabled ${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}
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;"
424 if [ -n "${next_hop_keep}" ]; then
425 [ "${next_hop_keep}" = "1" ] && writeToConfig " next hop keep;" || writeToConfig "# next hop keep;"
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};"
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};"
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};"
442 [ -n "${neighbor_address}" -a -n "${neighbor_as}" ] && writeToConfig " neighbor ${neighbor_address} as ${neighbor_as};"
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.
453 writeToConfig "#Filters Section:"
454 for filter in $(find /etc/${BIRD}/filters -type f); do
455 writeToConfig "include \"${filter}\";"
457 writeToConfig "#End of Filters --"
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.
467 writeToConfig "#Functions Section:"
468 for func in $(find /etc/${BIRD}/functions -type f); do
469 writeToConfig "include \"${func}\";"
471 writeToConfig "#End of Functions --"