gabelmoo has a new IP address.
[tor/rransom.git] / contrib / linux-tor-prio.sh
blob473511131beea74cdab36d2b6f996cbe410ece1d
1 #!/bin/bash
2 # Written by Marco Bonetti & Mike Perry
3 # Based on instructions from Dan Singletary's ADSL BW Management HOWTO:
4 # http://www.faqs.org/docs/Linux-HOWTO/ADSL-Bandwidth-Management-HOWTO.html
5 # This script is Public Domain.
7 ############################### README #################################
9 # This script provides prioritization of Tor traffic below other
10 # traffic on a Linux server. It has two modes of operation: UID based
11 # and IP based. The UID based method requires that Tor be launched from
12 # a specific user ID. The "User" and "Group" Tor config settings are
13 # insufficient, as they set the UID after the socket is created.
14 # Here is a three line C wrapper you can use to execute Tor and drop
15 # privs to UID 501 before it creates any sockets. Change the UID
16 # to the UID for your tor server user, and compile with
17 # 'gcc tor_wrap.c -o tor_wrap':
19 # #include <unistd.h>
20 # int main(int argc, char **argv) {
21 # if(setresuid(501, 501, 501) == -1) { perror("setresuid"); return 1; }
22 # execl("/bin/tor", "/bin/tor", "-f", "/etc/tor/torrc", NULL);
23 # perror("execl"); return 1;
24 # }
26 # The IP setting requires that a separate IP address be dedicated to Tor.
27 # Your Torrc should be set to bind to this IP for "OutboundBindAddress",
28 # "ListenAddress", and "Address".
30 # You should also tune the individual connection rate parameters below
31 # to your individual connection. In particular, you should leave *some*
32 # minimum amount of bandwidth for Tor, so that Tor users are not
33 # completely choked out when you use your server's bandwidth. 30% is
34 # probably a reasonable choice. More is better of course.
36 # To start the shaping, run it as:
37 # ./linux-tor-prio.sh
39 # To get status information (useful to verify packets are getting marked
40 # and prioritized), run:
41 # ./linux-tor-prio.sh status
43 # And to stop prioritization:
44 # ./linux-tor-prio.sh stop
46 ########################################################################
48 # BEGIN USER TUNABLE PARAMETERS
50 DEV=eth0
52 # NOTE! You must START Tor under this UID. Using the Tor User/Group
53 # config setting is NOT sufficient.
54 TOR_UID=$(id -u tor)
56 # If the UID mechanism doesn't work for you, you can set this parameter
57 # instead. If set, it will take precedence over the UID setting. Note that
58 # you need multiple IPs for this to work.
59 #TOR_IP="42.42.42.42"
61 # Average ping to most places on the net, milliseconds
62 RTT_LATENCY=40
64 # RATE_UP must be less than your connection's upload capacity in
65 # kbits/sec. If it is larger, then the bottleneck will be at your
66 # router's queue, which you do not control. This will cause congestion
67 # and a revert to normal TCP fairness no matter what the queing
68 # priority is.
69 RATE_UP=5000
71 # RATE_UP_TOR is the minimum speed your Tor connections will have in
72 # kbits/sec. They will have at least this much bandwidth for upload.
73 # In general, you probably shouldn't set this too low, or else Tor
74 # users who use your node will be completely choked out whenever your
75 # machine does any other network activity. That is not very fun.
76 RATE_UP_TOR=1500
78 # RATE_UP_TOR_CEIL is the maximum rate allowed for all Tor trafic in
79 # kbits/sec.
80 RATE_UP_TOR_CEIL=5000
82 CHAIN=OUTPUT
83 #CHAIN=PREROUTING
84 #CHAIN=POSTROUTING
86 MTU=1500
87 AVG_PKT=900 # should be more like 600 for non-exit nodes
89 # END USER TUNABLE PARAMETERS
91 # The queue size should be no larger than your bandwidth-delay
92 # product. This is RT latency*bandwidth/MTU/2
94 BDP=$(expr $RTT_LATENCY \* $RATE_UP / $AVG_PKT)
96 # Further research indicates that the BDP calculations should use
97 # RTT/sqrt(n) where n is the expected number of active connections..
99 BDP=$(expr $BDP / 4)
101 if [ "$1" = "status" ]
102 then
103 echo "[qdisc]"
104 tc -s qdisc show dev $DEV
105 tc -s qdisc show dev imq0
106 echo "[class]"
107 tc -s class show dev $DEV
108 tc -s class show dev imq0
109 echo "[filter]"
110 tc -s filter show dev $DEV
111 tc -s filter show dev imq0
112 echo "[iptables]"
113 iptables -t mangle -L TORSHAPER-OUT -v -x 2> /dev/null
114 exit
118 # Reset everything to a known state (cleared)
119 tc qdisc del dev $DEV root 2> /dev/null > /dev/null
120 tc qdisc del dev imq0 root 2> /dev/null > /dev/null
121 iptables -t mangle -D POSTROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
122 iptables -t mangle -D PREROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
123 iptables -t mangle -D OUTPUT -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
124 iptables -t mangle -F TORSHAPER-OUT 2> /dev/null > /dev/null
125 iptables -t mangle -X TORSHAPER-OUT 2> /dev/null > /dev/null
126 ip link set imq0 down 2> /dev/null > /dev/null
127 rmmod imq 2> /dev/null > /dev/null
129 if [ "$1" = "stop" ]
130 then
131 echo "Shaping removed on $DEV."
132 exit
135 # Outbound Shaping (limits total bandwidth to RATE_UP)
137 ip link set dev $DEV qlen $BDP
139 # Add HTB root qdisc, default is high prio
140 tc qdisc add dev $DEV root handle 1: htb default 20
142 # Add main rate limit class
143 tc class add dev $DEV parent 1: classid 1:1 htb rate ${RATE_UP}kbit
145 # Create the two classes, giving Tor at least RATE_UP_TOR kbit and capping
146 # total upstream at RATE_UP so the queue is under our control.
147 tc class add dev $DEV parent 1:1 classid 1:20 htb rate $(expr $RATE_UP - $RATE_UP_TOR)kbit ceil ${RATE_UP}kbit prio 0
148 tc class add dev $DEV parent 1:1 classid 1:21 htb rate $[$RATE_UP_TOR]kbit ceil ${RATE_UP_TOR_CEIL}kbit prio 10
150 # Start up pfifo
151 tc qdisc add dev $DEV parent 1:20 handle 20: pfifo limit $BDP
152 tc qdisc add dev $DEV parent 1:21 handle 21: pfifo limit $BDP
154 # filter traffic into classes by fwmark
155 tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20
156 tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 21 fw flowid 1:21
158 # add TORSHAPER-OUT chain to the mangle table in iptables
159 iptables -t mangle -N TORSHAPER-OUT
160 iptables -t mangle -I $CHAIN -o $DEV -j TORSHAPER-OUT
163 # Set firewall marks
164 # Low priority to Tor
165 if [ ""$TOR_IP == "" ]
166 then
167 echo "Using UID-based QoS. UID $TOR_UID marked as low priority."
168 iptables -t mangle -A TORSHAPER-OUT -m owner --uid-owner $TOR_UID -j MARK --set-mark 21
169 else
170 echo "Using IP-based QoS. $TOR_IP marked as low priority."
171 iptables -t mangle -A TORSHAPER-OUT -s $TOR_IP -j MARK --set-mark 21
174 # High prio for everything else
175 iptables -t mangle -A TORSHAPER-OUT -m mark --mark 0 -j MARK --set-mark 20
177 echo "Outbound shaping added to $DEV. Rate for Tor upload at least: ${RATE_UP_TOR}Kbyte/sec."