1 # Bird4-OpenWRT Library - Functions used in /etc/init.d/bird4 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 and write the $1 string into $BIRD_CONFIG file.
43 # Example: local N=0; write_bool $N
45 [ "$2" == 1 ] && writeToConfig
" $1;"
50 # $1 string. $2 string
51 # This function uses the external UCI function "config_get $result $section $option" to obtain a string value from UCI config file.
52 # To use this function, use the same name of the UCI option for the variable.
53 # Example: UCI (option id 'abcd'); local id; get id $section
59 # Function: get_bool $1 $2
60 # $1 boolean. $2 string
61 # This function uses the external UCI function "config_get_bool $result $section $option" to obtain a boolean value from UCI config file.
62 # To use this function, use the same name of the UCI option for the variable $1.
63 # Example: UCI (option use_ipv6 '1'); local use_ipv6; get use_ipv6 $section
65 config_get_bool
$1 $2 $1
69 # Function: multipath_list $1
71 # This function writes the $1 string in the multipath routes.
77 # Function: range_list $1
79 # This function writes the $1 string in the OSPF networks.
85 # Function: hidden_range_list $1
87 # This function writes the $1 string in the OSPF networks as hidden.
89 write " $1 hidden;" $1
93 # Function: prepare_tables $1
95 # This function gets each "table" section in the UCI configuration and sets each option in the bird4.conf file.
96 # $1 is set as the ID of the current UCI table section
98 local section
="$1"; local name
102 write "table ${name};" ${name}
106 # Function: prepare_global $1
108 # This function gets each "global" section in the UCI configuration and sets each option in the bird4.conf file.
109 # $1 is set as the ID of the current UCI global section. prepare_global is the first configuration set in the bird4.conf and removes the old file.
112 local log_file
; local log
; local debug
; local router_id
; local table
114 # Remove old configuration file
115 rm -f "${BIRD_CONFIG}"
117 get log_file
${section}
120 get router_id
${section}
123 # First line of the NEW configuration file
124 echo "#Bird4 configuration using UCI:" > ${BIRD_CONFIG}
126 #TODO: Set Syslog as receiver if empty
127 # LOGF="${log_file:-syslog]}"
128 #TODO: If $log/$debug are empty, set to off
129 if [ -n "${log_file}" -a -n "${log}" ]; then
130 firstEntry
="${log:0:3}"
131 if [ "${firstEntry}" = "all" -o "${firstEntry}" = "off" ]; then
132 writeToConfig
'log "'${log_file}'" '${firstEntry}';'
134 logEntries
=$
(echo ${log} |
tr " " ",")
135 writeToConfig
"log \"${log_file}\" { ${logEntries} };"
139 if [ -n "${debug}" ]; then
140 firstEntry
="${debug:0:3}"
141 if [ "${firstEntry}" = "all" -o "${firstEntry}" = "off" ]; then
142 writeToConfig
"debug protocols ${firstEntry};"
144 debugEntries
=$
(echo ${debug} |
tr " " ",")
145 writeToConfig
"debug protocols { ${debugEntries} };"
149 writeToConfig
"#Router ID"
150 write "router id ${router_id};" ${router_id}
152 writeToConfig
"#Secondary tables"
153 config_foreach prepare_tables
'table'
158 # Function: prepare_routes $1
160 # This function gets each "route" section in the UCI configuration and sets each option in the bird4.conf file.
161 # $1 is set as the ID of the current UCI route section. Each type of route has its own treatment.
163 local instance
; local prefix
; local via
; local type; local attribute
; local iface
165 local protoInstance
="$2"
167 get instance
${section}
169 get prefix
${section}
171 if [ "${instance}" = "${protoInstance}" ]; then
175 [ -n "${prefix}" -a -n "${via}" ] && writeToConfig " route ${prefix} via ${via};"
178 get attribute
${section}
179 [ -n "${prefix}" -a -n "${attribute}" ] && writeToConfig " route ${prefix} ${attribute};"
183 [ -n "${prefix}" -a -n "${iface}" ] && writeToConfig ' route '${prefix}' via "'${iface}'";'
186 write " route ${prefix} multipath" ${prefix}
187 config_list_foreach ${section} l_via multipath_list
195 # Function: prepare_kernel $1
197 # This function gets each "kernel" protocol section in the UCI configuration and sets each option in the bird4.conf file.
198 # $1 is set as the ID of the current UCI kernel section.
201 local disabled; local table; local kernel_table; local import; local export
202 local scan_time; local persist; local learn
204 get_bool disabled ${section}
206 get import ${section}
207 get export ${section}
208 get scan_time ${section}
209 get kernel_table ${section}
211 get persist ${section}
213 write "#${section} configuration:" ${section}
214 writeToConfig "protocol kernel ${section} {" ${section}
215 write_bool disabled ${disabled}
216 write " table ${table};" ${table}
217 write " kernel table ${kernel_table};" ${kernel_table}
218 write_bool learn ${learn}
219 write_bool persist ${persist}
220 write " scan time ${scan_time};" ${scan_time}
221 write " import ${import};" ${import}
222 write " export ${export};" ${export}
228 # Function: prepare_static $1
230 # This function gets each "static" protocol section in the UCI configuration and sets each option in the bird4.conf file.
231 # $1 is set as the ID of the current UCI static section.
234 local disabled; local table
236 get disabled ${section}
239 if [ "${disabled}" -eq 0 ]; then
240 writeToConfig "#${section} configration:" ${section}
241 writeToConfig "protocol static {"
242 write " table ${table};" ${table}
243 config_foreach prepare_routes 'route
' ${section}
250 # Function: prepare_direct $1
252 # This function gets each "direct" protocol section in the UCI configuration and sets each option in the bird4.conf file.
253 # $1 is set as the ID of the current UCI direct section.
256 local disabled; local interface
258 get disabled ${section}
259 get interface ${section}
261 write "#${section} configuration:" ${section}
262 writeToConfig "protocol direct {"
263 write_bool disabled ${disabled}
264 write " interface ${interface};" ${interface}
270 # Function: prepare_pipe $1
272 # This function gets each "pipe" protocol section in the UCI configuration an
273 # $1 is set as the ID of the current UCI direct section.
276 local disabled; local table; local peer_table; local mode; local import; local export
278 get disabled $section
279 get peer_table $section
285 write "#$section configuration:" $section
286 writeToConfig "protocol pipe $section {" $section
287 write_bool disabled $disabled
288 write " table $table;" $table
289 write " peer table $peer_table;" $peer_table
290 write " mode $mode;" $mode
291 write " import $import;" $import
292 write " export $export;" $export
298 # Function: prepare_device $1
300 # This function gets each "device" protocol section in the UCI configuration and sets each option in the bird4.conf file.
301 # $1 is set as the ID of the current UCI device section.
304 local disabled; local scan_time
306 get disabled ${section}
307 get scan_time ${section}
309 write "#${section} configuration:" ${section}
310 writeToConfig "protocol device {"
311 write_bool disabled ${disabled}
312 write " scan time ${scan_time};" ${scan_time}
318 # Function: prepare_bgp_template $1
320 # This function gets each "bgp_template" protocol section in the UCI configuration and sets each option in the bird4.conf file.
321 # $1 is set as the ID of the current UCI bgp_template section.
322 # Careful! Template options will be replaced by "instance" options if there is any match.
323 prepare_bgp_template() {
325 local disabled; local table; local import; local export
326 local local_as; local neighbor_address; local neighbor_as; local source_address
327 local next_hop_self; local next_hop_keep; local rr_client; local rr_cluster_id
328 local import_limit; local import_limit_action; local export_limit; local export_limit_action
329 local receive_limit; local receive_limit_action; local igp_table
331 get_bool disabled ${section}
333 get import ${section}
334 get export ${section}
335 get source_address ${section}
337 get local_as ${section}
338 get neighbor_address ${section}
339 get neighbor_as ${section}
341 get_bool next_hop_self ${section}
342 get_bool next_hop_keep ${section}
343 get rr_client ${section}
344 get rr_cluster_id ${section}
346 get import_limit ${section}
347 get import_limit_action ${section}
348 get export_limit ${section}
349 get export_limit_action ${section}
351 get receive_limit ${section}
352 get receive_limit_action ${section}
353 get igp_table ${section}
355 writeToConfig "#${section} template:"
356 writeToConfig "template bgp ${section} {"
357 [ -n "${disabled}" ] && write_bool disabled ${disabled}
358 [ -n "${table}" ] && writeToConfig " table ${table};"
359 [ -n "${igp_table}" ] && writeToConfig " igp table ${igp_table};"
360 [ -n "${local_as}" ] && writeToConfig " local as ${local_as};"
361 [ -n "${source_address}" ] && writeToConfig " source address ${source_address};"
362 [ -n "${import}" ] && writeToConfig " import ${import};"
363 [ -n "${export}" ] && writeToConfig " export ${export};"
364 [ -n "${neighbor_address}" -a -n "${neighbor_as}" ] && writeToConfig " neighbor ${neighbor_address} as ${neighbor_as};"
365 if [ -n "${import_limit}" -a "${import_limit}" > "0" ]; then
366 [ -z "${import_limit_action}" ] && ${import_limit_action} = "warn"
367 writeToConfig " import limit ${import_limit} action ${import_limit_action};"
369 if [ -n "${export_limit}" -a "${export_limit}" > "0" ]; then
370 [ -z "${export_limit_action}" ] && ${export_limit_action} = "warn"
371 writeToConfig " export limit ${export_limit} action ${export_limit_action};"
373 if [ -n "${receive_limit}" -a "${receive_limit}" > "0" ]; then
374 [ -z "${receive_limit_action}" ] && ${receive_limit_action} = "warn"
375 writeToConfig " receive limit ${receive_limit} action ${receive_limit_action};"
377 [ -n "${next_hop_self}" ] && write_bool " next hop self;" ${next_hop_self}
378 [ -n "${next_hop_keep}" ] && write_bool " next hop keep;" ${next_hop_keep}
379 [ -n "${rr_client}" ] && write_bool " rr client;" ${rr_client}
380 [ -n "${rr_cluster_id}" ] && writeToConfig " rr cluster id ${rr_cluster_id};"
386 # Function: prepare_bgp $1
388 # This function gets each "bgp" protocol section in the UCI configuration and sets each option in the bird4.conf file.
389 # $1 is set as the ID of the current UCI bgp section.
390 # Careful! The options set in bgp instances overlap bgp_template ones.
393 local disabled; local table; local template; local description; local igp_table; local passive
394 local import; local export; local source_address; local local_as; local neighbor_address
395 local neighbor_as; local rr_client; local rr_cluster_id; local import_limit
396 local import_limit_action; local export_limit; local export_limit_action
397 local receive_limit; local receive_limit_action; local igp_table
399 get disabled ${section}
401 get igp_table ${section}
402 get template ${section}
403 get description ${section}
404 get passive ${section}
406 get import ${section}
407 get export ${section}
408 get source_address ${section}
409 get local_as ${section}
410 get neighbor_address ${section}
412 get neighbor_as ${section}
413 get import_limit ${section}
414 get import_limit_action ${section}
415 get export_limit ${section}
416 get export_limit_action ${section}
418 get receive_limit ${section}
419 get receive_limit_action ${section}
420 get_bool next_hop_self ${section}
421 get_bool next_hop_keep ${section}
422 get rr_client ${section}
423 get rr_cluster_id ${section}
425 writeToConfig "#${section} configuration:"
426 [ -n "${template}" ] && writeToConfig "protocol bgp ${section} from ${template} {" \
427 || writeToConfig "protocol bgp ${section} {"
428 [ -n "${disabled}" ] && write_bool disabled ${disabled}
429 [ -n "${table}" ] && writeToConfig " table ${table};"
430 [ -n "${igp_table}" ] && writeToConfig " igp table ${igp_table};"
431 [ -n "${passive}" ] && writeToConfig " passive;" ${passive}
432 [ -n "${local_as}" ] && writeToConfig " local as ${local_as};"
433 [ -n "${source_address}" ] && writeToConfig " source address ${source_address};"
434 [ -n "${import}" ] && writeToConfig " import ${import};"
435 [ -n "${export}" ] && writeToConfig " export ${export};"
436 [ -n "${neighbor_address}" -a -n "${neighbor_as}" ] && writeToConfig " neighbor ${neighbor_address} as ${neighbor_as};"
437 if [ -n "${import_limit}" -a "${import_limit}" > "0" ]; then
438 [ -z "${import_limit_action}" ] && ${import_limit_action} = "warn"
439 writeToConfig " import limit ${import_limit} action ${import_limit_action};"
441 if [ -n "${export_limit}" -a "${export_limit}" > "0" ]; then
442 [ -z "${export_limit_action}" ] && ${export_limit_action} = "warn"
443 writeToConfig " export limit ${export_limit} action ${export_limit_action};"
445 if [ -n "${receive_limit}" -a "${receive_limit}" > "0" ]; then
446 [ -z "${receive_limit_action}" ] && ${receive_limit_action} = "warn"
447 writeToConfig " receive limit ${receive_limit} action ${receive_limit_action};"
449 [ -n "${next_hop_self}" ] && write_bool " next hop self;" ${next_hop_self}
450 [ -n "${next_hop_keep}" ] && write_bool " next hop keep;" ${next_hop_keep}
451 [ -n "${rr_client}" ] && write_bool " rr client;" ${rr_client}
452 [ -n "${rr_cluster_id}" ] && writeToConfig " rr cluster id ${rr_cluster_id};"
458 #Function: prepare_ospf_network $1
459 # $1 string $2 string
460 # This function gets each "ospf_network" protocol section in the UCI configuration, checks if its Area ID is the same as the one
461 # being configurated and finally sets the list of network ranges to be propagated, or not, by the OSPF protocol
462 # $1 is set as the ID of the action area of the internal networks.
463 # $2 is set as the ID of the current area being configurated.
464 prepare_ospf_networks() {
466 local current_area="$2"
468 if [ "${section}" = "${current_area}" ]; then
469 writeToConfig " networks {"
470 config_list_foreach ${section} range range_list
471 config_list_foreach ${section} hidden_range hidden_range_list
477 # Function: prepare_ospf_password $1 $2
478 prepare_ospf_passwords() {
480 local current_interface="$2"
481 local interface; local passphrase
483 get interface $section
484 get passphrase $section
486 [ "current_interface" = "${interface}" ] && write ' password
"$passphrase";' ${passphrase}
490 # Function: prepare_ospf_neighbors $1 $2
491 #prepare_ospf_neighbors() {
495 # Function: prepare_ospf_interface $1 $2
496 prepare_ospf_interface() {
498 local current_area="$2"
499 local area; local cost; local type; local hello; local priority; local retransmit; local authentication
505 get priority ${section}
506 get retransmit ${section}
508 if [ "${current_area}" = "${area}" ]; then
509 writeToConfig ' interface
"$section" {'
510 write " cost ${cost};" ${cost}
511 write " hello ${hello};" ${hello}
512 write " type ${type};" ${type}
513 write " retransmit ${retransmit};" ${retransmit}
514 write " authentication ${authentication};" ${authentication}
515 config_foreach prepare_ospf_passwords "ospf_password" ${section}
516 # config_foreach prepare_ospf_neighbors "ospf_neighbor" $section
522 # Function: prepare_ospf_area $1
523 prepare_ospf_area() {
525 local instance; local stub; local default_cost
527 get instance ${section}
529 get default_cost ${section}
531 writeToConfig " area ${section} {"
532 if [ -n "${instance}" -a "${instance}" = "${section}" ]; then
533 [ -n "${stub}" -a "${stub}" = "1" ] && writeToConfig " stub yes;"
534 [ -n "${default_cost}" ] && writeToConfig " default cost ${default_cost};"
535 config_foreach prepare_ospf_networks "ospf_networks" ${section}
536 config_foreach prepare_ospf_interface "ospf_interface" ${section}
542 # Function: prepare_ospf_instance $1
544 # This function gets each "ospf_area" protocol section in the UCI configuration and sets each option in the bird4.conf file.
545 # $1 is set as the ID of the current UCI ospf_area section.
546 prepare_ospf_instance() {
548 local cfg1583compat; local tick
550 get cfg1583compat ${section}
553 writeToConfig "protocol ospf ${section} {"
554 [ -n "${cfg1583compat}" ] && cfg1583State="yes" || cfg1583State="no"
555 writeToConfig " rfc1583compat ${cfg1583State};"
556 [ -n "${tick}" ] && writeToConfig " tick ${tick};"
557 config_foreach prepare_ospf_area 'ospf_area
'
562 # Function: gather_filters
563 # This function gets all the FILES under /filters folder and adds
564 # them into the config as %include elements on top of the file
565 # If there are no filters, the section will remain empty.
567 writeToConfig "#Filters Section:"
568 for filter in $(find /etc/${BIRD}/filters -type f); do
569 writeToConfig "include \"${filter}\";"
571 writeToConfig "#End of Filters --"
576 # Function: gather_functions
577 # This function gets all the FILES under /functions folder and adds
578 # them into the config as %include elements on top of the file
579 # If there are no filters, the section will remain empty.
581 writeToConfig "#Functions Section:"
582 for func in $(find /etc/${BIRD}/functions -type f); do
583 writeToConfig "include \"${func}\";"
585 writeToConfig "#End of Functions --"