#!/bin/sh
#
# Shell-Skript fuer Quality of Service mit HTB
#
EXTIF=ppp0
INTIF=br0
TC=/sbin/tc
IPTABLES=/sbin/iptables
IP=/sbin/ip
if [ "$1" = "status" ]
then
# $TC -s qdisc ls dev $INTIF
$TC -s qdisc ls dev $EXTIF
iptables -t mangle -L SHAPER-OUT -v -n
# iptables -t mangle -L SHAPER-IN -v -n
exit
fi
if [ "$1" = "pollbuckets" ]
then
watch -n1 "$TC -s class show dev $EXTIF; \
echo \"--------\"; $TC -s qdisc show dev $EXTIF"
exit
fi
if [ "$1" = "pollrules" ]
then
watch -n1 "iptables -t mangle -L SHAPER-OUT -v -n; \
# echo \"--------\"; iptables -t mangle -L SHAPER-IN -v -n"
exit
fi
if [ "$1" = "pollstats" ]
then
exit
fi
#Löschen der Root-QDisc und der Einträge in Postrouting-Tabelle
$TC qdisc del dev $EXTIF root &> /dev/null
iptables -t mangle -D POSTROUTING -o $EXTIF -j SHAPER-OUT &> /dev/null
#tc qdisc del dev ppp0 root 2> /dev/null > /dev/null
#tc qdisc del dev ppp0 ingress 2> /dev/null > /dev/null
#tc qdisc del dev eth0 root 2> /dev/null > /dev/null
#tc qdisc del dev eth1 root 2> /dev/null > /dev/null
#tc qdisc del dev lo root 2> /dev/null > /dev/null
#Löschen der SHAPER-OUT Tabelle
iptables -t mangle -F SHAPER-OUT &> /dev/null
iptables -t mangle -X SHAPER-OUT &> /dev/null
if ( [ "$1" = "stop" ] )
then
exit
fi
################# Link Einstellungen #####################
#MTU=1454
MTU=1492
#ip link show dev $EXTIF # Anzeige Queue Länge
ip link set dev $EXTIF qlen 50 # setzt Packetwarteschlange von 3 auf 50
#ip link set dev $EXTIF mtu $MTU # setzt MTU von 1454 auf $MTU
############# configuring QoS system ######################
iptables -t mangle -N SHAPER-OUT
iptables -t mangle -I POSTROUTING -o $EXTIF -j SHAPER-OUT
#sleep 30
############
# Outgoing
############
RATE=500
MPU=0
OVERHEAD=0
## Root
$TC qdisc add dev $EXTIF root handle 1: htb default 12
## Hauptklasse
$TC class add dev $EXTIF parent 1: classid 1:1 htb rate ${RATE}kbit ceil ${RATE}kbit quantum 1500 mtu $MTU
## Klasse fuer ACK
$TC class add dev $EXTIF parent 1:1 classid 1:10 htb rate $[20*$RATE/100]kbit ceil ${RATE}kbit quantum 1500 burst 6k prio 0 mtu $MTU
## Klasse fuer VPN/SSH/VoIP/VNC
$TC class add dev $EXTIF parent 1:1 classid 1:11 htb rate $[40*$RATE/100]kbit ceil ${RATE}kbit quantum 1500 burst 6k prio 1 mtu $MTU
## Klasse fuer normalen Traffic
$TC class add dev $EXTIF parent 1:1 classid 1:12 htb rate $[20*$RATE/100]kbit ceil ${RATE}kbit quantum 1500 prio 2 mtu $MTU
## Klasse fuer Bulk
$TC class add dev $EXTIF parent 1:1 classid 1:13 htb rate $[20*$RATE/100]kbit ceil ${RATE}kbit quantum 1500 prio 3 mtu $MTU
# fehler
$TC filter add dev $EXTIF parent 1:0 prio 0 protocol ip handle 10 fw flowid 1:10 # alle ursprünglich: prio 0
$TC filter add dev $EXTIF parent 1:0 prio 0 protocol ip handle 11 fw flowid 1:11
# default: 1:20
$TC filter add dev $EXTIF parent 1:0 prio 0 protocol ip handle 13 fw flowid 1:13
#Anlegen einer pfifo QDisc mit max. 5 Packeten an eine bestehende HTB-Klasse
#$TC qdisc add dev $EXTIF parent 1:10 handle 20: pfifo limit 5 #vielleicht 10
#$TC qdisc add dev $EXTIF parent 1:11 handle 21: pfifo limit 5
#########
# SFQ
#########
$TC qdisc add dev $EXTIF parent 1:10 handle 20: sfq perturb 10
$TC qdisc add dev $EXTIF parent 1:11 handle 21: sfq perturb 10
$TC qdisc add dev $EXTIF parent 1:12 handle 22: sfq perturb 10
$TC qdisc add dev $EXTIF parent 1:13 handle 23: sfq perturb 10
################## finish QoS configuration #########################
################# begin packet classification #######################
# N.B.
# shaping only works for OUTGOING packets simply so think which interface to use
# when you apply shaping to incoming packets you really are saying which ones
# are safe to drop (without generating more traffic, FTP data) and which are
# unsafe (UDP as it will not slow down the data transfer and has already
# consumed your bandwidth and TCP ACK's are examples).
#
# Useful Info
# IPTables TOS Fields:
# Minimize-Delay 16 (0x10)
# Maximize-Throughput 8 (0x08)
# Maximize-Reliability 4 (0x04)
# Minimize-Cost 2 (0x02),
# Normal-Service 0 (0x00).
#ACKs
iptables -t mangle -A SHAPER-OUT -p tcp -m length --length :128 -j MARK --set-mark 10
# 'normal' tcp control flags
iptables -t mangle -A SHAPER-OUT -p tcp -m tcp --tcp-flags ! SYN,RST,ACK ACK -j MARK --set-mark 10
# regular ACK
#iptables -t mangle -A SHAPER-OUT -p tcp -m tcp --tcp-flags SYN,RST,ACK ACK -m length --length :128 -m tos --tos ! Normal-Service -j MARK --set-mark 10
#CONNMARK wiederherstellen
iptables -t mangle -A SHAPER-OUT -m connmark --mark 13 -j CONNMARK --restore-mark # P2P Connections
iptables -t mangle -A SHAPER-OUT -m connmark --mark 11 -j CONNMARK --restore-mark # bevorzugte Connections
#iptables -t mangle -A SHAPER-OUT -p tcp -m connmark --mark 13 -j CONNMARK --restore-mark # P2P Connections
#iptables -t mangle -A SHAPER-OUT -p tcp -m connmark --mark 11 -j CONNMARK --restore-mark # bevorzugte Connections
#bestehende Marks nicht überschreiben
iptables -t mangle -A SHAPER-OUT -m mark ! --mark 0 -j ACCEPT
#iptables -t mangle -A SHAPER-OUT -p tcp -m mark ! --mark 0 -j ACCEPT
#P2P-Traffic mit ipp2p erkennen
iptables -t mangle -A SHAPER-OUT -m ipp2p --ipp2p -j CONNMARK --set-mark 13
#iptables -t mangle -A SHAPER-OUT -p tcp -m ipp2p --ipp2p -j CONNMARK --set-mark 13
# P2P traffic ACK
iptables -t mangle -A SHAPER-OUT -p tcp -m tcp --tcp-flags SYN,RST,ACK ACK -m length --length 128: -j MARK --set-mark 13
#ICMPs
iptables -t mangle -A SHAPER-OUT -p icmp -j MARK --set-mark 10
#HTTP
iptables -t mangle -A SHAPER-OUT -p tcp --dport 80 -j MARK --set-mark 12
iptables -t mangle -A SHAPER-OUT -p tcp --dport 443 -j MARK --set-mark 12
#VPN/IPsec
iptables -t mangle -A SHAPER-OUT -p 50 -j MARK --set-mark 11
iptables -t mangle -A SHAPER-OUT -p 51 -j MARK --set-mark 11
iptables -t mangle -A SHAPER-OUT -p udp --dport 500 --sport 500 -j MARK --set-mark 11
#iptables -t mangle -A SHAPER-OUT -p gre -j MARK --set-mark 11 # PTPP
#VoIP
iptables -t mangle -A SHAPER-OUT -p udp -m length --length :256 -j MARK --set-mark 11 # alles VoIP
iptables -t mangle -A SHAPER-OUT -p udp --dport 5060 -j MARK --set-mark 11 # SIP
iptables -t mangle -A SHAPER-OUT -p udp --dport 7070:7080 -j MARK --set-mark 11 # siproxd
#iptables -t mangle -A SHAPER-OUT -p udp --dport 30000:40000 -j MARK --set-mark 11 # SIPPS ehemals : 30010
#iptables -t mangle -A SHAPER-OUT -p udp --dport 5000:5016 -j MARK --set-mark 11 # Gnomemeeting
iptables -t mangle -A SHAPER-OUT -p udp --sport 5060 -j MARK --set-mark 11 # SIP
iptables -t mangle -A SHAPER-OUT -p udp --sport 7070:7080 -j MARK --set-mark 11 # siproxd
#iptables -t mangle -A SHAPER-OUT -p udp --sport 30000:40000 -j MARK --set-mark 11 # RTP
#iptables -t mangle -A SHAPER-OUT -p udp --sport 5000:5016 -j MARK --set-mark 11 # Gnomemeeting
#Quake 3
iptables -t mangle -A SHAPER-OUT -p udp --dport 27960 -j MARK --set-mark 11
iptables -t mangle -A SHAPER-OUT -p udp --sport 27960 -j MARK --set-mark 11
# strike2death
iptables -t mangle -A SHAPER-OUT -p tcp --dport 7666 -j MARK --set-mark 11
iptables -t mangle -A SHAPER-OUT -p udp --dport 7666 -j MARK --set-mark 11
iptables -t mangle -A SHAPER-OUT -p tcp --sport 7665 -j MARK --set-mark 11
iptables -t mangle -A SHAPER-OUT -p udp --sport 7665 -j MARK --set-mark 11
# WoW
iptables -t mangle -A SHAPER-OUT -p tcp --dport 3724 -j MARK --set-mark 11
iptables -t mangle -A SHAPER-OUT -p tcp --dport 6112 -j MARK --set-mark 11
iptables -t mangle -A SHAPER-OUT -p tcp --dport 6881:6999 -j MARK --set-mark 11
# low latency traffic
iptables -t mangle -A SHAPER-OUT -p tcp -m tos --tos Minimize-Delay -j MARK --set-mark 11
# get bulk traffic z.B. sftp
iptables -t mangle -A SHAPER-OUT -p tcp -m tos --tos Maximize-Throughput -j MARK --set-mark 13
iptables -t mangle -A SHAPER-OUT -p tcp -m tos --tos Minimize-Cost -j MARK --set-mark 13
#VNC
iptables -t mangle -A SHAPER-OUT -p tcp --dport 666 -j MARK --set-mark 11
#SSH
iptables -t mangle -A SHAPER-OUT -p tcp --dport 22 -j MARK --set-mark 11
iptables -t mangle -A SHAPER-OUT -p tcp --sport 22 -j MARK --set-mark 11
#SMTP
iptables -t mangle -A SHAPER-OUT -p tcp --dport 25 -j MARK --set-mark 11
#ICQ
iptables -t mangle -A SHAPER-OUT -p tcp --dport 5190 -j MARK --set-mark 11
#ftp
iptables -t mangle -A SHAPER-OUT -p tcp --dport 21 -j MARK --set-mark 12
#Bittorrent
#iptables -t mangle -A SHAPER-OUT -p tcp --dport 6881:6889 -j MARK --set-mark 13 #Reguläre Bittorrent-Ports
#iptables -t mangle -A SHAPER-OUT -p tcp --sport 6881:6889 -j MARK --set-mark 13 #Bittorrent auf Papaschlumpf
#iptables -t mangle -A SHAPER-OUT -p tcp --sport 6821:6829 -j MARK --set-mark 13 #Bittorrent auf Electron
#iptables -t mangle -A SHAPER-OUT -p tcp --sport 6831:6839 -j MARK --set-mark 13 #Bittorrent auf Positron
# MLDonkey
#iptables -t mangle -A SHAPER-OUT -p tcp --sport 4662 -j MARK --set-mark 13 #eDonkey
#iptables -t mangle -A SHAPER-OUT -p udp --sport 4666 -j MARK --set-mark 13 #eDonkey udp-port (tcp + 4)
#iptables -t mangle -A SHAPER-OUT -p udp --sport 11996 -j MARK --set-mark 13 #Overnet udp-port
#iptables -t mangle -A SHAPER-OUT -p tcp --sport 11996 -j MARK --set-mark 13 #Overnet
#iptables -t mangle -A SHAPER-OUT -p tcp --dport 4662 -j MARK --set-mark 13 # Standard eDonkey-port
# von c't
#tc qdisc add dev $INTIF parent 2:20 handle 20: sfq perturb 10
#tc qdisc add dev $INTIF parent 2:21 handle 21: sfq perturb 10
#tc qdisc add dev $INTIF parent 2:22 handle 22: sfq perturb 10
###########
# Incoming
###########
#$TC qdisc add dev $INTIF root handle 2:0 htb default 20
#$TC class add dev $INTIF parent 2:0 classid 2:2 htb rate 750kbit ceil 750kbit
#$TC class add dev $INTIF parent 2:2 classid 2:20 htb rate 500kbit ceil 700kbit prio 1
#$TC class add dev $INTIF parent 2:2 classid 2:21 htb rate 150kbit ceil 750kbit prio 0
#$TC class add dev $INTIF parent 2:2 classid 2:22 htb rate 100kbit ceil 500kbit prio 3
# ACKs
#iptables -A SHAPER-OUT -t mangle -o $INTIF -m length --length :200 -j MARK --set-mark 21
# SSH
#iptables -A SHAPER-OUT -t mangle -o $INTIF -p tcp --sport 22 -j MARK --set-mark 21
# eDonkey
#iptables -A SHAPER-OUT -t mangle -o $INTIF -p tcp --dport 4662 -j MARK --set-mark 22
#iptables -A SHAPER-OUT -t mangle -o $INTIF -p tcp --sport 4662 -j MARK --set-mark 22
# zu drosselnder Rechner
#iptables -A SHAPER-OUT -t mangle -o $INTIF -d 192.168.111.1 -j MARK --set-mark 22
#tc filter add dev $INTIF parent 2:0 prio 0 protocol ip handle 21 fw flowid 2:21
#tc filter add dev $INTIF parent 2:0 prio 0 protocol ip handle 22 fw flowid 2:22
# ACKs
# prioritize small packets (<64 bytes)
#$TC filter add dev $EXTIF parent 1:0 protocol ip prio 0 u32 \
# match ip protocol 6 0xff \
# match u8 0x05 0x0f at 0 \
# match u16 0x0000 0xffc0 at 2 \
# flowid 1:10
#
# ICMP (ip protocol 1) in the interactive class 1:10 so we
# can do measurements & impress our friends:
#$TC filter add dev $EXTIF parent 1:0 protocol ip prio 0 u32 \
# match ip protocol 1 0xff flowid 1:10
#
# TOS Minimum Delay (ssh, NOT scp) in 1:10:
#$TC filter add dev $EXTIF parent 1:0 protocol ip prio 0 u32 \
# match ip tos 0x10 0xff flowid 1:10