2 [ -e /etc
/functions.sh
] && .
/etc
/functions.sh || . .
/functions.sh
3 [ -x /sbin
/modprobe
] && insmod
="modprobe" || insmod
="insmod"
6 eval "export isset=\${insmod_$1}"
9 *) append INSMOD
"$insmod $* >&- 2>&-" "$N"; export insmod_
$1=1;;
13 [ -e /etc
/config
/network
] && {
14 # only try to parse network config on openwrt
20 config_get
"$1" ifname
24 echo "Interface not found."
29 parse_matching_rule
() {
39 append
"$var" "$prefix" "$N"
40 for option
in $options; do
42 proto
) config_get value
"$section" proto
; proto
="${proto:-$value}";;
45 config_get
type "$section" TYPE
47 classify
) unset pkt
; append
"$var" "-m mark --mark 0";;
48 default
) pkt
=1; append
"$var" "-m mark --mark 0";;
51 append
"$var" "${proto:+-p $proto}"
52 for option
in $options; do
53 config_get value
"$section" "$option"
55 case "$pkt:$option" in
57 append
"$var" "-s $value"
60 append
"$var" "-d $value"
64 append
"$var" "-m ipp2p"
66 all
) append
"$var" "--edk --dc --kazaa --gnu --bit";;
67 *) append
"$var" "--$value";;
72 append
"$var" "-m layer7 --l7proto $value${pkt:+ --l7pkt}"
74 *:ports|
*:srcports|
*:dstports
)
75 value
="$(echo "$value" | sed -e 's,-,:,g')"
78 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} -m multiport";;
79 *) unset "$var"; return 0;;
83 config_set
"$section" srcports
""
84 config_set
"$section" dstports
""
85 config_set
"$section" portrange
""
86 append
"$var" "--ports $value"
89 config_set
"$section" ports
""
90 config_set
"$section" dstports
""
91 config_set
"$section" portrange
""
92 append
"$var" "--sports $value"
95 config_set
"$section" ports
""
96 config_set
"$section" srcports
""
97 config_set
"$section" portrange
""
98 append
"$var" "--dports $value"
104 config_set
"$section" ports
""
105 config_set
"$section" srcports
""
106 config_set
"$section" dstports
""
107 value
="$(echo "$value" | sed -e 's,-,:,g')"
109 ""|tcp|udp
) append
"$var" "-m ${proto:-tcp -p tcp} --sport $value --dport $value";;
110 *) unset "$var"; return 0;;
115 value
="$(echo "$value" | sed -e 's,-,:,g')"
116 add_insmod ipt_connbytes
117 append
"$var" "-m connbytes --connbytes $value --connbytes-dir both --connbytes-mode bytes"
120 value
="$(echo "$value" | sed -e 's,-,:,g')"
121 if [ "$value" = "out" ]; then
122 append
"$var" "-o $device"
123 elif [ "$value" = "in" ]; then
124 append
"$var" "-i $device"
128 value
="$(echo "$value" | sed -e 's,-,:,g')"
129 add_insmod ipt_length
130 append
"$var" "-m length --length $value"
134 append
"$var" "-m limit --limit $value"
138 tcp
) append
"$var" "-m tcp --tcp-flags ALL $value";;
139 *) unset $var; return 0;;
143 config_get class
"${value##!}" classnr
144 [ -z "$class" ] && continue;
146 !*) append
"$var" "-m mark ! --mark $class";;
147 *) append
"$var" "-m mark --mark $class";;
151 append
"$var" "$suffix"
152 case "$ports:$proto" in
153 1:) parse_matching_rule
"$var" "$section" "$options" "$prefix" "$suffix" "udp";;
165 config_set
"$2" "classgroup" "Default"
166 config_set
"$2" "upload" "128"
168 classify|default|reclassify
)
176 config_get TYPE
"$CONFIG_SECTION" TYPE
179 config_get_bool enabled
"$CONFIG_SECTION" enabled
1
180 [ 1 -eq "$enabled" ] ||
return 0
181 config_get classgroup
"$CONFIG_SECTION" classgroup
182 config_set
"$CONFIG_SECTION" imqdev
"$C"
184 append INTERFACES
"$CONFIG_SECTION"
185 config_set
"$classgroup" enabled
1
186 config_get device
"$CONFIG_SECTION" device
187 [ -z "$device" ] && {
188 device
="$(find_ifname ${CONFIG_SECTION})"
189 config_set
"$CONFIG_SECTION" device
"${device:-eth0}"
192 classgroup
) append CG
"$CONFIG_SECTION";;
193 classify|default|reclassify
)
195 classify
) var
="ctrules";;
198 config_get target
"$CONFIG_SECTION" target
199 config_set
"$CONFIG_SECTION" options
"$options"
200 append
"$var" "$CONFIG_SECTION"
209 config_get classes
"$1" classes
210 config_get default
"$1" default
211 for class
in $classes; do
213 config_set
"${class}" classnr
$c
215 $default) class_default
=$c;;
218 class_default
="${class_default:-$c}"
228 config_get tmp1
"$class" "$name"
229 config_get tmp2
"${class}_${type}" "$name"
232 export ${varname}="${tmp:-$default}"
237 [ -e $dir/tcrules.
awk ] || dir
=.
240 -v linespeed
="$rate" \
247 config_get device
"$iface" device
248 config_get_bool enabled
"$iface" enabled
1
249 [ -z "$device" -o 1 -ne "$enabled" ] && {
252 config_get upload
"$iface" upload
253 config_get halfduplex
"$iface" halfduplex
254 config_get download
"$iface" download
255 config_get classgroup
"$iface" classgroup
256 config_get_bool overhead
"$iface" overhead
0
258 download
="${download:-${halfduplex:+$upload}}"
259 enum_classes
"$classgroup"
260 for dir
in up
${halfduplex} ${download:+down}; do
263 [ "$overhead" = 1 ] && upload
=$
(($upload * 98 / 100 - (15 * 128 / $upload)))
270 add_insmod imq numdevs
="$num_imq"
271 config_get imqdev
"$iface" imqdev
272 [ "$overhead" = 1 ] && download
=$
(($download * 98 / 100 - (80 * 1024 / $download)))
281 for class
in $classes; do
282 cls_var pktsize
"$class" packetsize
$dir 1500
283 cls_var pktdelay
"$class" packetdelay
$dir 0
284 cls_var maxrate
"$class" limitrate
$dir 100
285 cls_var prio
"$class" priority
$dir 1
286 cls_var avgrate
"$class" avgrate
$dir 0
287 config_get classnr
"$class" classnr
288 append cstr
"$classnr:$prio:$avgrate:$pktsize:$pktdelay:$maxrate" "$N"
290 append
${prefix}q
"$(tcrules)" "$N"
291 export dev_
${dir}="ifconfig $dev up txqueuelen 5 >&- 2>&-
292 tc qdisc del dev $dev root >&- 2>&-
293 tc qdisc add dev $dev root handle 1: hfsc default ${class_default}0
294 tc class add dev $dev parent 1: classid 1:1 hfsc sc rate ${rate}kbit ul rate ${rate}kbit"
302 ${INSMOD:+$INSMOD$N}${dev_up:+$dev_up
310 unset INSMOD clsq clsf clsl d_clsq d_clsl d_clsf dev_up dev_down
315 for iface
in $INTERFACES; do
316 start_interface
"$iface" "$C"
325 for rule
in $rules; do
327 config_get target
"$rule" target
328 config_get target
"$target" classnr
329 config_get options
"$rule" options
330 parse_matching_rule iptrule
"$rule" "$options" "$prefix" "-j MARK --set-mark $target"
331 append
"$var" "$iptrule" "$N"
342 add_rules iptrules
"$ctrules" "iptables -t mangle -A ${cg}_ct"
343 config_get classes
"$cg" classes
344 for class
in $classes; do
345 config_get mark
"$class" classnr
346 config_get maxsize
"$class" maxsize
347 [ -z "$maxsize" -o -z "$mark" ] ||
{
348 add_insmod ipt_length
349 append pktrules
"iptables -t mangle -A ${cg} -m mark --mark $mark -m length --length $maxsize: -j MARK --set-mark 0" "$N"
352 add_rules pktrules
"$rules" "iptables -t mangle -A ${cg}"
353 for iface
in $INTERFACES; do
354 config_get classgroup
"$iface" classgroup
355 config_get device
"$iface" device
356 config_get imqdev
"$iface" imqdev
357 config_get dl
"$iface" download
358 config_get halfduplex
"$iface" halfduplex
360 append up
"iptables -t mangle -A OUTPUT -o $device -j ${cg}" "$N"
361 append up
"iptables -t mangle -A FORWARD -o $device -j ${cg}" "$N"
363 [ -z "$halfduplex" ] ||
{
364 append down
"iptables -t mangle -A POSTROUTING -o $device -j IMQ --todev $imqdev" "$N"
366 append down
"iptables -t mangle -A PREROUTING -i $device -j ${cg}" "$N"
367 append down
"iptables -t mangle -A POSTROUTING -o $device -j ${cg}" "$N"
368 append down
"iptables -t mangle -A PREROUTING -i $device -j IMQ --todev $imqdev" "$N"
373 iptables -t mangle -N ${cg} >&- 2>&-
374 iptables -t mangle -N ${cg}_ct >&- 2>&-
375 ${iptrules:+${iptrules}${N}iptables -t mangle -A ${cg}_ct -j CONNMARK --save-mark}
376 iptables -t mangle -A ${cg} -j CONNMARK --restore-mark
377 iptables -t mangle -A ${cg} -m mark --mark 0 -j ${cg}_ct
379 $up$N${down:+${down}$N}
385 add_insmod ipt_multiport
386 add_insmod ipt_CONNMARK
388 iptables -t mangle -F
389 iptables -t mangle -X
398 [ -e .
/qos.conf
] && {
404 for iface
in $INTERFACES; do
405 export C
="$(($C + 1))"
410 start_interfaces
"$C"
414 start_interface
"$2" "$C"