#!/bin/sh
# for openwrt
. /etc/functions.sh
# Source:
# http://www.ipcop-forum.de/forum/viewtopic.php?t=4749&postdays=0&postorder=asc&start=120
# http://voip-info.org/tiki-index.php?page=QoS%20Linux%20with%20HFSC
# **********************************
# DON'T FORGET INSMOD / modprobe !
# **********************************
###############################
### Defaults ###
###############################
UPDEV=$(nvram get wan_ifname)
RATEUP=364kbit
MYUMAX=1492b
# for the iptables rules:
# Your Asterisk (or similar) Server, single Ip
VOIPADR="192.168.1.150"
VOIPPORTS="4569 5060 30000:30021"
###############################
### Init Modules ###
###############################
# test:
# if [ -e /tmp/do_voips_init ]; then $ACTION="init"; fi
if [ "$1" = "init" ]
then
insmod sch_hfsc
insmod sch_csz
insmod ipt_owner
insmod ipt_tos
insmod cls_fw
insmod ipt_length
insmod ipt_layer7
exit
fi
###############################
### Status Info ###
###############################
if [ "$1" = "status" ]
then
echo "[qdisc]"
/usr/sbin/tc -s qdisc show dev $UPDEV
echo ""
echo "[class]"
/usr/sbin/tc -s class show dev $UPDEV
echo ""
echo "[filter]"
/usr/sbin/tc -s filter show dev $UPDEV
echo ""
echo "[iptables]"
iptables -t mangle -L QOS -v -x 2> /dev/null
exit
fi
#####################
# verify $1 (e.g. to avoid reload when called with "voipshaper staus" ...
#####################
if [ "$1" != "start" ] && [ "$1" != "stop" ]
then
echo "Usage: $0 {start|stop|init|status}" >&2
exit 1
fi
###############################
### Start script ###
###############################
# Reset everything to a known state (cleared)
/usr/sbin/tc qdisc del dev $UPDEV root 2> /dev/null > /dev/null
# Flush and delete tables
iptables -t mangle --delete POSTROUTING -o $UPDEV -j QOS 2> /dev/null > /dev/null
iptables -t mangle --flush QOS 2> /dev/null > /dev/null
iptables -t mangle --delete-chain QOS 2> /dev/null > /dev/null
######################
# stop it and exit
######################
if [ "$1" = "stop" ]
then
echo "Shaping removed on $UPDEV."
exit
fi
### Rootqueue einrichten
/usr/sbin/tc qdisc add dev $UPDEV root handle 1: hfsc default 10
### Parentclass einrichten mit $RATEUP/s
/usr/sbin/tc class add dev $UPDEV parent 1: classid 1:1 hfsc sc rate $RATEUP ul rate $RATEUP
### Einzelne classes
## Class 1:10 mit Prio 8, 1kbps garantiert, maxdelay 150ms
/usr/sbin/tc class add dev $UPDEV parent 1:1 classid 1:10 hfsc sc umax $MYUMAX dmax 150ms rate 1kbit ul rate $RATEUP
## Class 1:11 mit Prio 1, 100kbps garantiert, maxdelay 30ms
/usr/sbin/tc class add dev $UPDEV parent 1:1 classid 1:11 hfsc sc umax $MYUMAX dmax 30ms rate 170kbit ul rate $RATEUP
## Class 1:12 mit Prio 7, 4kbps garantiert, maxdelay 150ms, max 100kbps
/usr/sbin/tc class add dev $UPDEV parent 1:1 classid 1:12 hfsc sc umax $MYUMAX dmax 150ms rate 1kbit ul rate 130kbit
## Class 1:13 mit Prio 3, 7kbps garantiert, maxdelay 40ms
/usr/sbin/tc class add dev $UPDEV parent 1:1 classid 1:13 hfsc sc umax $MYUMAX dmax 40ms rate 5kbit ul rate $RATEUP
## Class 1:14 mit Prio 4, 7kbps garantiert, maxdelay 40ms
/usr/sbin/tc class add dev $UPDEV parent 1:1 classid 1:14 hfsc sc umax $MYUMAX dmax 40ms rate 4kbit ul rate $RATEUP
## Class 1:19 mit Prio 2, 10kbps garantiert, maxdelay 30ms
/usr/sbin/tc class add dev $UPDEV parent 1:1 classid 1:19 hfsc sc umax $MYUMAX dmax 30ms rate 10kbit ul rate $RATEUP
###############################
### iptablechain einrichten ###
###############################
### QOS-Chain für $UPDEV
iptables -t mangle --new-chain QOS
iptables -t mangle --insert POSTROUTING -o $UPDEV -j QOS
######################
### tc Zuordnungen ###
######################
### Match für 1:10 auf Mark 10
/usr/sbin/tc filter add dev $UPDEV parent 1: prio 8 protocol ip handle 10 fw flowid 1:10
### Match für 1:11 auf Mark 11
/usr/sbin/tc filter add dev $UPDEV parent 1: prio 1 protocol ip handle 11 fw flowid 1:11
### Match für 1:12 auf Mark 12
/usr/sbin/tc filter add dev $UPDEV parent 1: prio 7 protocol ip handle 12 fw flowid 1:12
### Match für 1:13 auf Mark 13
/usr/sbin/tc filter add dev $UPDEV parent 1: prio 3 protocol ip handle 13 fw flowid 1:13
### Match für 1:14 auf Mark 14
/usr/sbin/tc filter add dev $UPDEV parent 1: prio 4 protocol ip handle 14 fw flowid 1:14
### Match für 1:19 auf Mark 19
/usr/sbin/tc filter add dev $UPDEV parent 1: prio 2 protocol ip handle 19 fw flowid 1:19
########################
### QoS-Markierungen ###
########################
##########################
## HFC-S-Markierungen ##
##########################
#################
## L7-Filter ##
#################
### Bittorrent :: Pattern quality: great veryfast
iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto bittorrent -j MARK --set-mark 12
### eDonkey :: Pattern quality: good veryfast overmatch!!!!
iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto edonkey -j MARK --set-mark 12
### DNS :: Pattern quality: great notsofast
iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto dns -j MARK --set-mark 13
### SSH :: Pattern quality: great veryfast
iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto ssh -j MARK --set-mark 13
### SIP :: Pattern quality: ok veryfast :: vermutlich nur Signalisierung
iptables -t mangle -A QOS -o $UPDEV -s $VOIPADR -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto sip -j MARK --set-mark 11
### HTTP :: Pattern quality: great notsofast :: soll moeglichst nur auf http server zutreffen nich auf andere die auch http nutzten
iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto http -j MARK --set-mark 14
### FTP :: Pattern quality: great fast
iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto ftp -j MARK --set-mark 14
### NNTP :: Pattern quality: good veryfast
iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto nntp -j MARK --set-mark 14
### SMTP :: Pattern quality: great fast
iptables -t mangle -A QOS -o $UPDEV -s 192.168.1.99 -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto smtp -j MARK --set-mark 14
### AIM :: Pattern quality: good notsofast :: kann vermutlich auch schneller via port markiert werden
#iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto aim -j MARK --set-mark 13
### IRC :: Pattern quality: good veryfast
iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto irc -j MARK --set-mark 13
#----
###RTP
# iptables -t mangle -A QOS -o $UPDEV -m layer7 --l7dir /etc/l7-protocols/protocols --l7proto rtp -j MARK --set-mark 11
###############################
### Mark 11 ###
# Prio 1 mit 100kbps Garantie #
###############################
# VoIP ports as defined above
for port in $VOIPPORTS
do
iptables -t mangle -A QOS -o $UPDEV -s $VOIPADR -p udp --sport $port -j MARK --set-mark 11
iptables -t mangle -A QOS -o $UPDEV -s $VOIPADR -p udp --dport $port -j MARK --set-mark 11
done
iptables -t mangle -A QOS -o $UPDEV -s $VOIPADR -j MARK --set-mark 11
###################
### Mark 12 ###
# Prio 7 #
###################
### Bittorrent
iptables -t mangle -A QOS -o $UPDEV -p tcp --sport 6881:6889 -j MARK --set-mark 12
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 6881:6889 -j MARK --set-mark 12
# trackerless uses also udp
iptables -t mangle -A QOS -o $UPDEV -p udp --sport 6881:6889 -j MARK --set-mark 12
iptables -t mangle -A QOS -o $UPDEV -p udp --dport 6881:6889 -j MARK --set-mark 12
### eDonkey TCP
iptables -t mangle -A QOS -o $UPDEV -p tcp --sport 4661:4669 -j MARK --set-mark 12
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 4661:4669 -j MARK --set-mark 12
### eDonkey UDP
iptables -t mangle -A QOS -o $UPDEV -p udp --sport 4672 -j MARK --set-mark 12
iptables -t mangle -A QOS -o $UPDEV -p udp --dport 4672 -j MARK --set-mark 12
### eDonkey Kademilia
#iptables -t mangle -A QOS -o $UPDEV -p udp --sport 4670 -j MARK --set-mark 12
### Overnet TCP
iptables -t mangle -A QOS -o $UPDEV -p tcp --sport 9129 -j MARK --set-mark 12
### Overnet UDP
iptables -t mangle -A QOS -o $UPDEV -p udp --sport 9789 -j MARK --set-mark 12
##############################
### Mark 13 ###
# Prio 3 mit 7kbps Garantie #
##############################
### AIM
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 5190 -j MARK --set-mark 13
### IRC
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 6667 -j MARK --set-mark 13
### NTP
iptables -t mangle -A QOS -o $UPDEV -p udp --dport 123 -j MARK --set-mark 13
### DNS
iptables -t mangle -A QOS -o $UPDEV -p udp --dport 53 -j MARK --set-mark 13
### SSH
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 22 -j MARK --set-mark 13
#iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 222 -j MARK --set-mark 13
### ICMP
iptables -t mangle -A QOS -o $UPDEV -p icmp -j MARK --set-mark 13
##############################
### Mark 14 ###
# Prio 4 mit 6kbps Garantie #
##############################
### HTTP/HTTPS
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 80 -j MARK --set-mark 14
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 443 -j MARK --set-mark 14
### FTP
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 21 -j MARK --set-mark 14
### UUCP
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 540 -j MARK --set-mark 14
### SMTP
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 25 -j MARK --set-mark 14
iptables -t mangle -A QOS -o $UPDEV -p tcp --sport 25 -j MARK --set-mark 14
### NNTP
iptables -t mangle -A QOS -o $UPDEV -p tcp --dport 119 -j MARK --set-mark 14
##############################
### Mark 19 ###
# Prio 2 mit 10kbps Garantie #
##############################
### TCP ACKs
iptables -t mangle -A QOS -o $UPDEV -p tcp --tcp-flags ACK ACK -m length --length :64 -j MARK --set-mark 19