]> git.ipfire.org Git - people/ms/network.git/blobdiff - src/functions/functions.route
hostapd: Dump config file in debug mode
[people/ms/network.git] / src / functions / functions.route
index 640c8713aafbc457a0c22679ca9b29f229f7f242..e6ea2441ef0a69c10dda0a10c7808eab7912425d 100644 (file)
 # Functions for static routing.
 #
 
-route_init() {
-       # Apply configured static routes.
-       route_apply
+cli_route() {
+       if cli_help_requested "$@"; then
+               cli_show_man network-route
+               exit ${EXIT_OK}
+       fi
+
+       local action=${1}
+       shift
+
+       case "${action}" in
+               static)
+                       cli_route_static "$@"
+                       ;;
+               *)
+                       error "Unrecognized action: ${action}"
+                       cli_run_help network route
+
+                       exit ${EXIT_ERROR}
+                       ;;
+       esac
+
+       exit ${EXIT_OK}
 }
 
-init_register route_init
+cli_route_static() {
+       if cli_help_requested "$@"; then
+               cli_show_man network-route-static
+               exit ${EXIT_OK}
+       fi
+
+       local action=${1}
+       shift
+
+       case "${action}" in
+               # Add a new route.
+               add)
+                       route_add "$@"
+                       ;;
+               # Remove an existing route.
+               remove)
+                       route_remove "$@"
+                       ;;
+               # List all routes.
+               list)
+                       route_list "$@"
+                       return ${EXIT_OK}
+                       ;;
+               # Reload all routes
+               reload)
+                       route_apply "$@"
+                       ;;
+               *)
+                       error "Unrecognized action: ${action}"
+                       cli_run_help network route
+
+                       exit ${EXIT_ERROR}
+               ;;
+       esac
+
+       # Applying all routes.
+       route_apply
+
+       exit ${EXIT_OK}
+}
 
 route_add() {
        local ${NETWORK_CONFIG_ROUTES_PARAMS}
@@ -35,7 +93,7 @@ route_add() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --gateway=*)
-                               gateway=$(cli_get_val ${1})
+                               gateway=$(cli_get_val "${1}")
                                ;;
                        --unreachable)
                                unreachable="true"
@@ -47,7 +105,7 @@ route_add() {
                                blackhole="true"
                                ;;
                        --mtu=*)
-                               mtu=$(cli_get_val ${1})
+                               mtu=$(cli_get_val "${1}")
                                ;;
                        *)
                                if isset network; then
@@ -63,7 +121,7 @@ route_add() {
 
        assert isset network
 
-       if ! ip_is_network ${network} && ! ip_is_valid ${network}; then
+       if ! ip_net_is_valid ${network} && ! ip_is_valid ${network}; then
                error "The given network is invalid: ${network}"
                return ${EXIT_ERROR}
        fi
@@ -96,8 +154,17 @@ route_add() {
                        return ${EXIT_ERROR}
                fi
 
+               # Check if the gateway is part of the statically routed network
+               if ip_network_is_subset_of ${gateway} ${network}; then
+                       error "The gateway is in the routed network"
+                       return ${EXIT_ERROR}
+               fi
+
                local network_proto=$(ip_detect_protocol ${network})
+               assert isset network_proto
+
                local gateway_proto=$(ip_detect_protocol ${gateway})
+               assert isset gateway_proto
 
                if [ "${network_proto}" != "${gateway_proto}" ]; then
                        error "The IP protocol version of the given network and gateway did not match."
@@ -147,35 +214,44 @@ route_add() {
 }
 
 route_remove() {
-       local _network=${1}
-       assert isset _network
+       local _network
+       local error=${EXIT_OK}
+
+       for _network in "$@"; do
+               # Validate input
+               if ! ip_net_is_valid ${_network} && ! ip_is_valid ${_network}; then
+                       error "Invalid IP address or network: ${_network}"
+                       error=${EXIT_ERROR}
+                       continue
+               fi
 
-       local found="false"
+               local found="false"
 
-       local ${NETWORK_CONFIG_ROUTES_PARAMS}
-       local line
-       while read line; do
-               route_parse_line ${line}
-               [ $? -eq ${EXIT_OK} ] || continue
+               local ${NETWORK_CONFIG_ROUTES_PARAMS}
+               local line
+               while read line; do
+                       route_parse_line ${line}
+                       [ $? -eq ${EXIT_OK} ] || continue
 
-               # Skip the rule, we want to delete.
-               if [ "${network}" = "${_network}" ]; then
-                       found="true"
-                       continue
-               fi
+                       # Skip the rule, we want to delete.
+                       if [ "${network}" = "${_network}" ]; then
+                               found="true"
+                               continue
+                       fi
 
-               print "${line}"
-       done < ${NETWORK_CONFIG_ROUTES} > ${NETWORK_CONFIG_ROUTES}.tmp
-       mv ${NETWORK_CONFIG_ROUTES}{.tmp,}
+                       print "${line}"
+               done < ${NETWORK_CONFIG_ROUTES} > ${NETWORK_CONFIG_ROUTES}.tmp
+               mv ${NETWORK_CONFIG_ROUTES}{.tmp,}
 
-       if enabled found; then
-               log INFO "Route to network '${_network}' has been removed."
-       else
-               error "No route to network '${_network}' was found."
-               return ${EXIT_ERROR}
-       fi
+               if enabled found; then
+                       log INFO "Route to network '${_network}' has been removed."
+               else
+                       error "No route to network '${_network}' was found."
+                       error=${EXIT_ERROR}
+               fi
+       done
 
-       return ${EXIT_OK}
+       return ${error}
 }
 
 route_list() {
@@ -184,7 +260,7 @@ route_list() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --protocol=*)
-                               protocol=$(cli_get_val ${1})
+                               protocol=$(cli_get_val "${1}")
                                ;;
                        *)
                                warning "Unrecognized argument: ${1}"
@@ -259,25 +335,25 @@ route_parse_line() {
        while read arg; do
                case "${arg}" in
                        network=*)
-                               network=$(cli_get_val ${arg})
+                               network=$(cli_get_val "${arg}")
                                ;;
                        gateway=*)
-                               gateway=$(cli_get_val ${arg})
+                               gateway=$(cli_get_val "${arg}")
                                ;;
                        unreachable=*)
-                               unreachable=$(cli_get_val ${arg})
+                               unreachable=$(cli_get_val "${arg}")
                                ;;
                        prohibit=*)
-                               prohibit=$(cli_get_val ${arg})
+                               prohibit=$(cli_get_val "${arg}")
                                ;;
                        blackhole=*)
-                               blackhole=$(cli_get_val ${arg})
+                               blackhole=$(cli_get_val "${arg}")
                                ;;
                        mtu=*)
-                               mtu=$(cli_get_val ${arg})
+                               mtu=$(cli_get_val "${arg}")
                                ;;
                esac
-       done <<< "$(args $@)"
+       done <<< "$(args "$@")"
 
        ### Check if all values are correctly set.
 
@@ -285,7 +361,7 @@ route_parse_line() {
        isset network || return ${EXIT_ERROR}
 
        # Is network or IP valid?
-       if ! ip_is_network ${network} && ! ip_is_valid ${network}; then
+       if ! ip_net_is_valid ${network} && ! ip_is_valid ${network}; then
                error "The given network is invalid: ${network}"
                return ${EXIT_ERROR}
        fi
@@ -297,6 +373,11 @@ route_parse_line() {
 
                # Must be a valid IP address.
                ip_is_valid ${gateway} || return ${EXIT_ERROR}
+
+               # Check if the gateway is part of the statically routed network
+               if ip_network_is_subset_of ${gateway} ${network}; then
+                       return ${EXIT_ERROR}
+               fi
        else
                # Check if exactly one of unreachable, prohibit or blackhole is set.
                local counter=$(list_count true ${unreachable} ${prohibit} ${blackhole})
@@ -312,41 +393,11 @@ route_parse_line() {
 }
 
 route_apply() {
-       local table="static"
-       local type
-
-       log INFO "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}
+       # Re-generate BIRD configuration
+       bird_generate_config
 
-       # Create a lookup rule for the static routing table.
-       route_rule_add --lookup="static" --priority=1000
+       # Reload the daemon
+       bird_reload
 }
 
 route_entry_add() {
@@ -362,19 +413,19 @@ route_entry_add() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --gateway=*)
-                               gateway=$(cli_get_val ${1})
+                               gateway=$(cli_get_val "${1}")
                                ;;
                        --table=*)
-                               table=$(cli_get_val ${1})
+                               table=$(cli_get_val "${1}")
                                ;;
                        --type=*)
-                               type=$(cli_get_val ${1})
+                               type=$(cli_get_val "${1}")
                                ;;
                        --proto=*)
-                               proto=$(cli_get_val ${1})
+                               proto=$(cli_get_val "${1}")
                                ;;
                        --mtu=*)
-                               mtu=$(cli_get_val ${1})
+                               mtu=$(cli_get_val "${1}")
                                ;;
                        *)
                                if isset network; then
@@ -392,7 +443,7 @@ route_entry_add() {
 
        assert isset network
 
-       if ! ip_is_network ${network} && ! ip_is_valid ${network}; then
+       if ! ip_net_is_valid ${network} && ! ip_is_valid ${network}; then
                error "The given network is invalid: ${network}"
                return ${EXIT_ERROR}
        fi
@@ -448,7 +499,7 @@ route_entry_add() {
                list_append command "mtu ${mtu}"
        fi
 
-       cmd_quiet "${command}"
+       cmd "${command}"
 }
 
 route_table_create() {
@@ -493,7 +544,7 @@ route_table_flush() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --protocol=*)
-                               protocol=$(cli_get_val ${1})
+                               protocol=$(cli_get_val "${1}")
                                ;;
                        *)
                                table="${1}"
@@ -540,7 +591,7 @@ route_table_exists() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --id=*)
-                               _id=$(cli_get_val ${1})
+                               _id=$(cli_get_val "${1}")
                                ;;
                        *)
                                _table=${1}
@@ -572,13 +623,13 @@ route_rule_add() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --lookup=*)
-                               lookup=$(cli_get_val ${1})
+                               lookup=$(cli_get_val "${1}")
                                ;;
                        --priority=*)
-                               priority=$(cli_get_val ${1})
+                               priority=$(cli_get_val "${1}")
                                ;;
                        --protocol=*)
-                               protocols=$(cli_get_val ${1})
+                               protocols=$(cli_get_val "${1}")
 
                                assert isoneof protocols ${IP_SUPPORTED_PROTOCOLS}
                                ;;
@@ -636,16 +687,16 @@ route_rule_exists() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --from=*)
-                               from=$(cli_get_val ${1})
+                               from=$(cli_get_val "${1}")
                                ;;
                        --lookup=*)
-                               lookup=$(cli_get_val ${1})
+                               lookup=$(cli_get_val "${1}")
                                ;;
                        --priority=*)
-                               prio=$(cli_get_val ${1})
+                               prio=$(cli_get_val "${1}")
                                ;;
                        --protocol=*)
-                               proto=$(cli_get_val ${1})
+                               proto=$(cli_get_val "${1}")
                                ;;
                        *)
                                warning "Unrecognized argument: ${1}"