From 0a5787976dd85db212fc5046c85d2aad6c64da5c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 16 Dec 2018 17:47:57 +0000 Subject: [PATCH] bird: Apply static routes instead of doing that manually with ip Signed-off-by: Michael Tremer --- src/functions/functions.bird | 52 ++++++++++++++++++++++++++++++++- src/functions/functions.route | 38 +++--------------------- src/functions/functions.routing | 3 -- 3 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/functions/functions.bird b/src/functions/functions.bird index 9c8b0068..c6fea321 100644 --- a/src/functions/functions.bird +++ b/src/functions/functions.bird @@ -60,7 +60,7 @@ bird_generate_config() { print print "# Export all routes to kernel" - for proto in ipv6 ipv4; do + for proto in ${IP_SUPPORTED_PROTOCOLS}; do print "protocol kernel {" print " ${proto} {" print " table ${proto/ipv/master};" @@ -71,4 +71,54 @@ bird_generate_config() { print done ) >> ${BIRD_CONF} + + # Static routes + for proto in ${IP_SUPPORTED_PROTOCOLS}; do + print "protocol static {" + print " ${proto};" + print + + # Read routes for this protocol from configuration + __bird_static_routes "${proto}" + + print "}" + print + done >> ${BIRD_CONF} +} + +__bird_static_routes() { + local proto="${1}" + assert isset proto + + local ${NETWORK_CONFIG_ROUTES_PARAMS} + local line + while read line; do + route_parse_line "${line}" + [ $? -eq ${EXIT_OK} ] || continue + + local type + local arg + for arg in unreachable prohibit blackhole; do + if enabled "${arg}"; then + type="${arg}" + break + fi + done + + # Skip all routes of another protocol + local _proto="$(ip_detect_protocol "${network}")" + if [ "${proto}" != "${_proto}" ]; then + continue + fi + + case "${type}" in + unreachable|prohibit|blackhole) + print " route ${network} ${type};" + ;; + + *) + print " route ${network} via ${gateway};" + ;; + esac + done < ${NETWORK_CONFIG_ROUTES} } diff --git a/src/functions/functions.route b/src/functions/functions.route index 7ca4f59d..e6ea2441 100644 --- a/src/functions/functions.route +++ b/src/functions/functions.route @@ -393,41 +393,11 @@ route_parse_line() { } route_apply() { - local table="static" - local type + # Re-generate BIRD configuration + bird_generate_config - log DEBUG "Applying static routes..." - - # Flush the routing table. - route_table_flush ${table} - - local ${NETWORK_CONFIG_ROUTES_PARAMS} - local line - while read line; do - route_parse_line ${line} - [ $? -eq ${EXIT_OK} ] || continue - - type="unicast" - local arg - for arg in unreachable prohibit blackhole; do - if enabled ${arg}; then - type="${arg}" - break - fi - done - - # Add the route. - route_entry_add ${network} --table="static" --proto="static" \ - --type="${type}" --gateway="${gateway}" --mtu="${mtu}" - local ret=$? - - if [ ${ret} -ne ${EXIT_OK} ]; then - log WARNING "Could not set route '${network}'." - fi - done < ${NETWORK_CONFIG_ROUTES} - - # Create a lookup rule for the static routing table. - route_rule_add --lookup="static" --priority=1000 + # Reload the daemon + bird_reload } route_entry_add() { diff --git a/src/functions/functions.routing b/src/functions/functions.routing index 24365856..c7aac094 100644 --- a/src/functions/functions.routing +++ b/src/functions/functions.routing @@ -181,7 +181,4 @@ routing_update() { cmd ${routing_cmd} cmd ${ip_cmd} rule add from ${local_ip_address} lookup ${table} - - # Apply all static routes - route_apply } -- 2.39.2