# 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}
while [ $# -gt 0 ]; do
case "${1}" in
--gateway=*)
- gateway=$(cli_get_val ${1})
+ gateway=$(cli_get_val "${1}")
;;
--unreachable)
unreachable="true"
blackhole="true"
;;
--mtu=*)
- mtu=$(cli_get_val ${1})
+ mtu=$(cli_get_val "${1}")
;;
*)
if isset network; then
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
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."
}
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() {
while [ $# -gt 0 ]; do
case "${1}" in
--protocol=*)
- protocol=$(cli_get_val ${1})
+ protocol=$(cli_get_val "${1}")
;;
*)
warning "Unrecognized argument: ${1}"
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.
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
# 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})
}
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() {
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
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
list_append command "mtu ${mtu}"
fi
- cmd_quiet "${command}"
+ cmd "${command}"
}
route_table_create() {
while [ $# -gt 0 ]; do
case "${1}" in
--protocol=*)
- protocol=$(cli_get_val ${1})
+ protocol=$(cli_get_val "${1}")
;;
*)
table="${1}"
while [ $# -gt 0 ]; do
case "${1}" in
--id=*)
- _id=$(cli_get_val ${1})
+ _id=$(cli_get_val "${1}")
;;
*)
_table=${1}
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}
;;
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}"