diff --git a/net/iproute2.sh b/net/iproute2.sh index b7152d9..bb1534b 100644 --- a/net/iproute2.sh +++ b/net/iproute2.sh @@ -204,6 +204,23 @@ _add_address() return $rc } +_filter_metric() +{ + # ip route show does not allow to filter by metric + # so we need to filter metric manually + + local metric_value="$1" + shift + + if [ "${metric_value}" -eq 0 ]; then + # metric 0 is not displayed + ip -o "$@" | fgrep -vsq " metric " + else + # add trailing space for matching + echo "$(ip -o "$@") " | fgrep -sq " metric ${metric_value} " + fi +} + _add_route() { local family= @@ -222,33 +239,31 @@ _add_route() if [ $# -eq 3 ]; then set -- "$1" "$2" via "$3" elif [ "$3" = "gw" ]; then - local one=$1 two=$2 + local one="$1" two="$2" shift; shift; shift set -- "${one}" "${two}" via "$@" fi - local cmd_add= cmd_show= have_metric=false + local cmd_add= cmd_show= metric_value= have_metric=false while [ -n "$1" ]; do case "$1" in - metric) metric=$2 ; cmd_add="${cmd_add} metric $2" ; shift ; have_metric=true ;; + metric|preference) metric="$2" ; cmd_add="${cmd_add} metric ${2}" ; metric_value="$2" ; shift ; have_metric=true ;; netmask) x="/$(_netmask2cidr "$2")" ; cmd_add="${cmd_add}${x}" ; cmd_show="${cmd_show}${x}" ; shift;; + table|tos) cmd_add="${cmd_add} ${1} ${2}" ; cmd_show="${cmd_show} ${1} ${2}" ; shift;; -host|-net);; - *) cmd_add="${cmd_add} ${1}" ; cmd_show="${cmd_show} ${1}" ;; + *) cmd_add="${cmd_add} ${1}" ;; esac shift done - # We cannot use a metric if we're using a nexthop - if ! ${have_metric} && \ - [ -n "${metric}" -a \ - "${cmd_add##* nexthop }" = "${cmd_add}" ] - then + # Add interface's metric by default + if ! ${have_metric} && [ -n "${metric}" ]; then cmd_add="${cmd_add} metric ${metric}" + metric_value="${metric}" fi # Check for route already existing: - ip ${family} route show ${cmd_show} dev "${IFACE}" 2>/dev/null | \ - fgrep -sq "${cmd_add%% *}" + _filter_metric "${metric_value}" ${family} route show ${cmd_show} dev "${IFACE}" 2>/dev/null route_already_exists=$? veinfo ip ${family} route append ${cmd_add} dev "${IFACE}" @@ -268,7 +283,7 @@ _add_route() *) msgfunc=eerror rc=1 ; eerror "Unknown error behavior: $eh_behavior" ;; esac eval $msgfunc "Route '$cmd_show' already existed:" - eval $msgfunc \"$(ip $family route show ${cmd_show} dev "${IFACE}" 2>&1)\" + eval $msgfunc "\"$(_filter_metric "${metric_value}" $family route show ${cmd_show} dev "${IFACE}" 2>&1)\"" else : # TODO: Handle other errors fi