652 |
richard |
1 |
#!/bin/sh
|
826 |
franck |
2 |
# $Id: alcasar-iptables-qos.sh 1469 2014-10-30 21:58:47Z richard $
|
827 |
franck |
3 |
|
652 |
richard |
4 |
# script de mise en place des regles du parefeu d'Alcasar (mode QOS)
|
|
|
5 |
# 3abtux - Rexy
|
|
|
6 |
# Non adapté à ALCASAR car valable pour serveurs disposés dans le réseau LAN internet qui n'est pas notre besoin.
|
|
|
7 |
|
|
|
8 |
# Un peu de config
|
1469 |
richard |
9 |
CONF_FILE="/usr/local/etc/alcasar.conf"
|
|
|
10 |
EXTIF=`grep ^EXTIF= $CONF_FILE|cut -d"=" -f2` # EXTernal InterFace
|
|
|
11 |
INTIF=`grep ^INTIF= $CONF_FILE|cut -d"=" -f2` # INTernal InterFace
|
|
|
12 |
TUNIF="tun0"
|
|
|
13 |
private_ip_mask=`grep ^PRIVATE_IP= $CONF_FILE|cut -d"=" -f2`
|
|
|
14 |
private_ip_mask=${private_ip_mask:=192.168.182.1/24}
|
|
|
15 |
PRIVATE_IP=`echo $private_ip_mask | cut -d"/" -f1` # ALCASAR LAN IP address
|
|
|
16 |
private_network=`/bin/ipcalc -n $private_ip_mask|cut -d"=" -f2` # LAN IP address (ie.: 192.168.182.0)
|
|
|
17 |
private_prefix=`/bin/ipcalc -p $private_ip_mask|cut -d"=" -f2` # LAN prefix (ie. 24)
|
|
|
18 |
PRIVATE_NETWORK_MASK=$private_network/$private_prefix # Lan IP address + prefix (192.168.182.0/24)
|
652 |
richard |
19 |
TC="/sbin/tc"
|
|
|
20 |
IPTABLES="/sbin/iptables"
|
|
|
21 |
|
|
|
22 |
# Vitesse max de la connexion internet en Kbit/s
|
1322 |
franck |
23 |
MAX_UPLOAD="100000"
|
|
|
24 |
MAX_DOWNLOAD="100000"
|
652 |
richard |
25 |
|
|
|
26 |
FTP="50"
|
|
|
27 |
WEB="100"
|
|
|
28 |
SMTP="40"
|
|
|
29 |
POP="60"
|
|
|
30 |
DEFAULT="100"
|
|
|
31 |
SPEED=$MAX_UPLOAD
|
|
|
32 |
SLOW="10"
|
|
|
33 |
|
|
|
34 |
BP_MAX=100
|
|
|
35 |
BP_MIN=10
|
|
|
36 |
BP_WEB=$WEB*$MAX_UPLOAD/100
|
|
|
37 |
BP_FTP=$FTP*$MAX_UPLOAD/100
|
|
|
38 |
BP_POP=$POP*$MAX_UPLOAD/100
|
|
|
39 |
BP_SMTP=$SMTP*$MAX_UPLOAD/100
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
################################################
|
|
|
43 |
# Local protection rules sur SSH prioritaire #
|
|
|
44 |
################################################
|
|
|
45 |
$IPTABLES -A PREROUTING -t mangle -p tcp --sport ssh -j TOS --set-tos Minimize-Delay
|
|
|
46 |
|
|
|
47 |
# Netoyage
|
|
|
48 |
$TC qdisc del dev $EXTIF root >/dev/null 2>&1
|
|
|
49 |
$TC qdisc del dev $EXTIF ingress >/dev/null 2>&1
|
|
|
50 |
$TC qdisc del dev $TUNIF root >/dev/null 2>&1
|
|
|
51 |
$TC qdisc del dev $TUNIF ingress >/dev/null 2>&1
|
|
|
52 |
|
|
|
53 |
# Création de la classe parent:
|
|
|
54 |
$TC qdisc add dev $EXTIF root handle 1: htb default 20
|
|
|
55 |
$TC class add dev $EXTIF parent 1: classid 1:1 htb rate ${MAX_UPLOAD}kbit ceil ${MAX_UPLOAD}kbit burst 6k
|
|
|
56 |
|
|
|
57 |
# Download
|
|
|
58 |
$TC qdisc add dev $TUNIF root handle 2: htb
|
|
|
59 |
$TC class add dev $TUNIF parent 2: classid 2:1 htb rate ${MAX_DOWNLOAD}kbit ceil ${MAX_DOWNLOAD}kbit burst 6k
|
|
|
60 |
|
|
|
61 |
# Classe Download LIMIT
|
|
|
62 |
$TC class add dev $TUNIF parent 2:1 classid 2:10 htb rate ${MAX_DOWNLOAD}kbit ceil ${MAX_DOWNLOAD}kbit burst 6k prio 9
|
|
|
63 |
$TC filter add dev $TUNIF parent 2:0 protocol ip prio 9 handle 100 fw flowid 2:10
|
1322 |
franck |
64 |
$IPTABLES -t mangle -A FORWARD -s $PRIVATE_NETWORK_MASK -j MARK --set-mark 100
|
|
|
65 |
#$IPTABLES -t mangle -A FORWARD -d $LAN_2 -j MARK --set-mark 100
|
|
|
66 |
#$IPTABLES -t mangle -A FORWARD -d $LAN_3 -j MARK --set-mark 100
|
|
|
67 |
#$IPTABLES -t mangle -A FORWARD -d $LAN_4 -j MARK --set-mark 100
|
|
|
68 |
#$IPTABLES -t mangle -A FORWARD -d $LAN_5 -j MARK --set-mark 100
|
652 |
richard |
69 |
|
|
|
70 |
|
|
|
71 |
# Classe par défaut
|
|
|
72 |
$TC class add dev ${EXTIF} parent 1:1 classid 1:20 htb rate ${DEFAULT}kbit ceil ${MAX_UPLOAD}kbit burst 6k prio 3
|
|
|
73 |
|
|
|
74 |
# Classe rapide
|
|
|
75 |
$TC class add dev ${EXTIF} parent 1:20 classid 1:10 htb rate ${SPEED}kbit ceil ${MAX_UPLOAD}kbit burst 6k prio 1
|
|
|
76 |
$TC filter add dev ${EXTIF} parent 1:0 protocol ip prio 1 handle 10 fw flowid 1:10
|
1322 |
franck |
77 |
#$IPTABLES -t mangle -A OUTPUT -p tcp --dport 6667 -j MARK --set-mark 10
|
|
|
78 |
#$IPTABLES -t mangle -A FORWARD -p tcp --dport 6667 -j MARK --set-mark 10
|
|
|
79 |
#$IPTABLES -t mangle -A FORWARD -p tcp --sport 6667 -j MARK --set-mark 10
|
|
|
80 |
#$IPTABLES -t mangle -A FORWARD -p tcp --sport 7000 -j MARK --set-mark 10
|
|
|
81 |
#$IPTABLES -t mangle -A FORWARD -p tcp --sport 6668 -j MARK --set-mark 10
|
|
|
82 |
#$IPTABLES -t mangle -A FORWARD -p tcp --sport 6669 -j MARK --set-mark 10
|
652 |
richard |
83 |
$IPTABLES -t mangle -A FORWARD -p icmp -j MARK --set-mark 10
|
|
|
84 |
$IPTABLES -t mangle -A OUTPUT -p icmp -j MARK --set-mark 10
|
|
|
85 |
$IPTABLES -t mangle -A OUTPUT -p tcp --dport 22 -j MARK --set-mark 10
|
|
|
86 |
$IPTABLES -t mangle -A OUTPUT -p tcp --sport 22 -j MARK --set-mark 10
|
|
|
87 |
$IPTABLES -t mangle -A FORWARD -p tcp --dport 22 -j MARK --set-mark 10
|
|
|
88 |
# cs
|
1322 |
franck |
89 |
#$IPTABLES -t mangle -A FORWARD -p udp --sport 27005 -j MARK --set-mark 10
|
652 |
richard |
90 |
|
|
|
91 |
# Classe LENTE
|
|
|
92 |
$TC class add dev $EXTIF parent 1:1 classid 1:30 htb rate ${SLOW}kbit prio 5
|
|
|
93 |
$TC filter add dev $EXTIF parent 1:0 protocol ip prio 5 handle 30 fw flowid 1:30
|
|
|
94 |
#$IPTABLES -t mangle -A FORWARD -p tcp --sport 4662 -j MARK --set-mark 30
|
|
|
95 |
#$IPTABLES -t mangle -A FORWARD -p udp --sport 4665 -j MARK --set-mark 30
|
|
|
96 |
#$IPTABLES -t mangle -A FORWARD -p tcp --dport 4662 -j MARK --set-mark 30
|
|
|
97 |
#$IPTABLES -t mangle -A FORWARD -p udp --dport 4665 -j MARK --set-mark 30
|
|
|
98 |
|
|
|
99 |
# Classe WEB
|
|
|
100 |
$TC class add dev $EXTIF parent 1:20 classid 1:21 htb rate ${WEB}kbit prio 2
|
|
|
101 |
$TC filter add dev $EXTIF parent 1:0 protocol ip prio 2 handle 21 fw flowid 1:21
|
|
|
102 |
$IPTABLES -t mangle -A OUTPUT -p tcp --sport 80 -j MARK --set-mark 21
|
|
|
103 |
$IPTABLES -t mangle -A FORWARD -p tcp --sport 80 -j MARK --set-mark 21
|
1322 |
franck |
104 |
$IPTABLES -t mangle -A OUTPUT -p tcp --sport 443 -j MARK --set-mark 21
|
|
|
105 |
$IPTABLES -t mangle -A FORWARD -p tcp --sport 443 -j MARK --set-mark 21
|
652 |
richard |
106 |
|
|
|
107 |
# Classe FTP
|
|
|
108 |
$TC class add dev $EXTIF parent 1:20 classid 1:22 htb rate ${FTP}kbit prio 4
|
|
|
109 |
$TC filter add dev $EXTIF parent 1:0 protocol ip prio 3 handle 22 fw flowid 1:22
|
|
|
110 |
$IPTABLES -t mangle -A OUTPUT -p tcp --sport 21 -j MARK --set-mark 22
|
|
|
111 |
$IPTABLES -t mangle -A FORWARD -p tcp --dport 21 -j MARK --set-mark 22
|
|
|
112 |
|
|
|
113 |
# Classe SMTP
|
|
|
114 |
$TC class add dev $EXTIF parent 1:20 classid 1:23 htb rate ${SMTP}kbit prio 4
|
|
|
115 |
$TC filter add dev $EXTIF parent 1:0 protocol ip prio 4 handle 23 fw flowid 1:23
|
|
|
116 |
$IPTABLES -t mangle -A OUTPUT -p tcp --sport 25 -j MARK --set-mark 23
|
|
|
117 |
$IPTABLES -t mangle -A OUTPUT -p tcp --dport 25 -j MARK --set-mark 23
|
|
|
118 |
$IPTABLES -t mangle -A FORWARD -p tcp --dport 25 -j MARK --set-mark 23
|
|
|
119 |
$IPTABLES -t mangle -A FORWARD -p tcp --sport 25 -j MARK --set-mark 23
|
|
|
120 |
|
|
|
121 |
$TC class add dev $EXTIF parent 1:20 classid 1:210 htb rate ${POP}kbit prio 4
|
|
|
122 |
$TC filter add dev $EXTIF parent 1:0 protocol ip prio 4 handle 210 fw flowid 1:210
|
|
|
123 |
|
|
|
124 |
$IPTABLES -t mangle -A FORWARD -p tcp --sport 110 -j MARK --set-mark 210
|
|
|
125 |
|
|
|
126 |
# End of script
|