qos-scripts: fix interface resolving
[openwrt/openwrt.git] / package / network / config / qos-scripts / files / usr / lib / qos / generate.sh
index 48c9f4a9df00119f5cf452e60c1895011a7ed2c1..024be0c1704a1297442a4106123e8529956a5a44 100755 (executable)
@@ -22,15 +22,20 @@ add_insmod() {
 [ -e /etc/config/network ] && {
        # only try to parse network config on openwrt
 
-       find_ifname() {(
-               reset_cb
-               include /lib/network
-               scan_interfaces
-               config_get "$1" ifname
-       )}
+       . /lib/functions/network.sh
+
+       find_ifname() {
+               local ifname
+               if network_get_device ifname "$1"; then
+                       echo "$ifname"
+               else
+                       echo "Device for interface $1 not found." >&2
+                       exit 1
+               fi
+       }
 } || {
        find_ifname() {
-               echo "Interface not found."
+               echo "Interface not found." >&2
                exit 1
        }
 }
@@ -118,21 +123,21 @@ parse_matching_rule() {
                                append "$var" "-m comment --comment '$value'"
                        ;;
                        *:tos)
-                                add_insmod xt_dscp
-                                case "$value" in
-                                        !*) append "$var" "-m tos ! --tos $value";;
-                                        *) append "$var" "-m tos --tos $value"
-                                esac
-                        ;;
+                               add_insmod xt_dscp
+                               case "$value" in
+                                       !*) append "$var" "-m tos ! --tos $value";;
+                                       *) append "$var" "-m tos --tos $value"
+                               esac
+                       ;;
                        *:dscp)
-                                add_insmod xt_dscp
+                               add_insmod xt_dscp
                                dscp_option="--dscp"
-                                [ -z "${value%%[EBCA]*}" ] && dscp_option="--dscp-class"
+                               [ -z "${value%%[EBCA]*}" ] && dscp_option="--dscp-class"
                                case "$value" in
-                                               !*) append "$var" "-m dscp ! $dscp_option $value";;
-                                               *) append "$var" "-m dscp $dscp_option $value"
-                                esac
-                        ;;
+                                       !*) append "$var" "-m dscp ! $dscp_option $value";;
+                                       *) append "$var" "-m dscp $dscp_option $value"
+                               esac
+                       ;;
                        *:direction)
                                value="$(echo "$value" | sed -e 's,-,:,g')"
                                if [ "$value" = "out" ]; then
@@ -218,6 +223,7 @@ qos_parse_config() {
                                config_get device "$1" device
                                [ -z "$device" ] && {
                                        device="$(find_ifname $1)"
+                                       [ -z "$device" ] && exit 1
                                        config_set "$1" device "$device"
                                }
                        }
@@ -428,7 +434,7 @@ start_cg() {
        cat <<EOF
 $INSMOD
 EOF
-  
+
 for command in $iptables; do
        cat <<EOF
        $command -w -t mangle -N qos_${cg} 
@@ -486,7 +492,7 @@ stop_firewall() {
                                -e 's/^-A/-D/' \
                                -e '${p;g}' |
                        # Make into proper iptables calls
-                       # Note:  awkward in previous call due to hold space usage
+                       # Note: awkward in previous call due to hold space usage
                        sed -n -e "s/^./${command} -w -t mangle &/p"
        done
 }