linux-tor-prio.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.
  6. ############################### README #################################
  7. # This script provides prioritization of Tor traffic below other
  8. # traffic on a Linux server. It has two modes of operation: UID based
  9. # and IP based.
  10. # UID BASED PRIORITIZATION
  11. #
  12. # The UID based method requires that Tor be launched from
  13. # a specific user ID. The "User" Tor config setting is
  14. # insufficient, as it sets the UID after the socket is created.
  15. # Here is a C wrapper you can use to execute Tor and drop privs before
  16. # it creates any sockets.
  17. #
  18. # Compile with:
  19. # gcc -DUID=`id -u tor` -DGID=`id -g tor` tor_wrap.c -o tor_wrap
  20. #
  21. # #include <unistd.h>
  22. # int main(int argc, char **argv) {
  23. # if(setresgid(GID, GID, GID) == -1) { perror("setresgid"); return 1; }
  24. # if(setresuid(UID, UID, UID) == -1) { perror("setresuid"); return 1; }
  25. # execl("/bin/tor", "/bin/tor", "-f", "/etc/tor/torrc", NULL);
  26. # perror("execl"); return 1;
  27. # }
  28. # IP BASED PRIORITIZATION
  29. #
  30. # The IP setting requires that a separate IP address be dedicated to Tor.
  31. # Your Torrc should be set to bind to this IP for "OutboundBindAddress",
  32. # "ListenAddress", and "Address".
  33. # GENERAL USAGE
  34. #
  35. # You should also tune the individual connection rate parameters below
  36. # to your individual connection. In particular, you should leave *some*
  37. # minimum amount of bandwidth for Tor, so that Tor users are not
  38. # completely choked out when you use your server's bandwidth. 30% is
  39. # probably a reasonable choice. More is better of course.
  40. #
  41. # To start the shaping, run it as:
  42. # ./linux-tor-prio.sh
  43. #
  44. # To get status information (useful to verify packets are getting marked
  45. # and prioritized), run:
  46. # ./linux-tor-prio.sh status
  47. #
  48. # And to stop prioritization:
  49. # ./linux-tor-prio.sh stop
  50. #
  51. ########################################################################
  52. # BEGIN USER TUNABLE PARAMETERS
  53. DEV=eth0
  54. # NOTE! You must START Tor under this UID. Using the Tor User
  55. # config setting is NOT sufficient. See above.
  56. TOR_UID=$(id -u tor)
  57. # If the UID mechanism doesn't work for you, you can set this parameter
  58. # instead. If set, it will take precedence over the UID setting. Note that
  59. # you need multiple IPs with one specifically devoted to Tor for this to
  60. # work.
  61. #TOR_IP="42.42.42.42"
  62. # Average ping to most places on the net, milliseconds
  63. 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
  70. # RATE_UP_TOR is the minimum speed your Tor connections will have in
  71. # kbits/sec. They will have at least this much bandwidth for upload.
  72. # In general, you probably shouldn't set this too low, or else Tor
  73. # users who use your node will be completely choked out whenever your
  74. # machine does any other network activity. That is not very fun.
  75. RATE_UP_TOR=1500
  76. # RATE_UP_TOR_CEIL is the maximum rate allowed for all Tor trafic in
  77. # kbits/sec.
  78. RATE_UP_TOR_CEIL=5000
  79. CHAIN=OUTPUT
  80. #CHAIN=PREROUTING
  81. #CHAIN=POSTROUTING
  82. MTU=1500
  83. AVG_PKT=900 # should be more like 600 for non-exit nodes
  84. # END USER TUNABLE PARAMETERS
  85. # The queue size should be no larger than your bandwidth-delay
  86. # product. This is RT latency*bandwidth/MTU/2
  87. BDP=$(expr $RTT_LATENCY \* $RATE_UP / $AVG_PKT)
  88. # Further research indicates that the BDP calculations should use
  89. # RTT/sqrt(n) where n is the expected number of active connections..
  90. BDP=$(expr $BDP / 4)
  91. if [ "$1" = "status" ]
  92. then
  93. echo "[qdisc]"
  94. tc -s qdisc show dev $DEV
  95. tc -s qdisc show dev imq0
  96. echo "[class]"
  97. tc -s class show dev $DEV
  98. tc -s class show dev imq0
  99. echo "[filter]"
  100. tc -s filter show dev $DEV
  101. tc -s filter show dev imq0
  102. echo "[iptables]"
  103. iptables -t mangle -L TORSHAPER-OUT -v -x 2> /dev/null
  104. exit
  105. fi
  106. # Reset everything to a known state (cleared)
  107. tc qdisc del dev $DEV root 2> /dev/null > /dev/null
  108. tc qdisc del dev imq0 root 2> /dev/null > /dev/null
  109. iptables -t mangle -D POSTROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  110. iptables -t mangle -D PREROUTING -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  111. iptables -t mangle -D OUTPUT -o $DEV -j TORSHAPER-OUT 2> /dev/null > /dev/null
  112. iptables -t mangle -F TORSHAPER-OUT 2> /dev/null > /dev/null
  113. iptables -t mangle -X TORSHAPER-OUT 2> /dev/null > /dev/null
  114. ip link set imq0 down 2> /dev/null > /dev/null
  115. rmmod imq 2> /dev/null > /dev/null
  116. if [ "$1" = "stop" ]
  117. then
  118. echo "Shaping removed on $DEV."
  119. exit
  120. fi
  121. # Outbound Shaping (limits total bandwidth to RATE_UP)
  122. ip link set dev $DEV qlen $BDP
  123. # Add HTB root qdisc, default is high prio
  124. tc qdisc add dev $DEV root handle 1: htb default 20
  125. # Add main rate limit class
  126. tc class add dev $DEV parent 1: classid 1:1 htb rate ${RATE_UP}kbit
  127. # Create the two classes, giving Tor at least RATE_UP_TOR kbit and capping
  128. # total upstream at RATE_UP so the queue is under our control.
  129. 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
  130. 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
  131. # Start up pfifo
  132. tc qdisc add dev $DEV parent 1:20 handle 20: pfifo limit $BDP
  133. tc qdisc add dev $DEV parent 1:21 handle 21: pfifo limit $BDP
  134. # filter traffic into classes by fwmark
  135. tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20
  136. tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 21 fw flowid 1:21
  137. # add TORSHAPER-OUT chain to the mangle table in iptables
  138. iptables -t mangle -N TORSHAPER-OUT
  139. iptables -t mangle -I $CHAIN -o $DEV -j TORSHAPER-OUT
  140. # Set firewall marks
  141. # Low priority to Tor
  142. if [ ""$TOR_IP == "" ]
  143. then
  144. echo "Using UID-based QoS. UID $TOR_UID marked as low priority."
  145. iptables -t mangle -A TORSHAPER-OUT -m owner --uid-owner $TOR_UID -j MARK --set-mark 21
  146. else
  147. echo "Using IP-based QoS. $TOR_IP marked as low priority."
  148. iptables -t mangle -A TORSHAPER-OUT -s $TOR_IP -j MARK --set-mark 21
  149. fi
  150. # High prio for everything else
  151. iptables -t mangle -A TORSHAPER-OUT -m mark --mark 0 -j MARK --set-mark 20
  152. echo "Outbound shaping added to $DEV. Rate for Tor upload at least: ${RATE_UP_TOR}Kbyte/sec."