Tchchains is a user-land tool to manage the rules that the patched kernel used to perform connection classification and forwarding. CONFIG_TCP_FW option must be enabled when compiling the kernel. Refer to here for how to compile the kernel.
Connection classification and forwarding is controlled by rules. A rule consists of an incoming port and several target outgoing ports, one of which is designated as default target. The incoming port, usually the well-known service port, is the port the client actually contact. The target outgoing ports are the ones the server instances are listening to. Conditions are associated with each outgoing ports. When an incoming connection satisfies the condition associated with one particular outgoing port, the connection will be forwarded to that port. If none of the conditions are satisfied, the default target port is used.
An outgoing port in one rule can be the incoming port of another rule. Thus, it is possible to connect several rules into a forwarding chain, which is a very powerful feature. That's reason that the tool is named tcpchains. For instance, the first rule with port X as its incoming port may specify that when condition A is true, connections to port X will be forwarded to port Y. Another rule with port Y as its incoming port may further specify that when condition B is true, connections to port Y will be forwarded to port Z. Therefore, when condition A and B are both true, a connection to port X will finally be forwarded to port Z.
2. What rules are supported
Three types of rules are supported:
3. How to Create a rule
Two steps are needed to create a rule:
tcpchains -A <rule type> -p <incoming port> -o <default target port>
<chain type> can be:
d: probabilistic rule
a: address-based rule
c: content-based ruleexample:
tcpchains -A d -p 143 -o 1143
create a probabilistic rule for IMAP service, default target port is 1143tcpchains -A a -p 21 -o 1021
create a address-based rule for FTP service, default target port is 1021tcpchains -A c -p 80 -o 8080
create a content-based rule for HTTP service, default target port is 8080
tcpchains -a <chain type> -p <incoming
port> <branch option>
<incoming port> is the port used in step 1
the syntax of <branch option> depends on the rule type. The underline part is the <branch option>
tcpchains -a d -p
<incoming port> -r <target port> -c <forward chance>
<forward chance> is the probability to forward to the <target
port>, the range is 1 - 10000
example:
tcpchains -a d -p 143 -r 2143 -c 3000
tcpchains -a d -p 143 -r 3143 -c 4000
30% IMAP
connections will be forwarded to port 2143, 40% to port 3143 with probability.
The remaining 30% connections will be forwarded to 1143, since it's the
default target port.
tcpchains -a a -p <incoming port> -r <target port> -i <source IP address> -m <subnet mask>
example:
tcpchains -a a -p 21 -r 2021 -i bobbidi.cs.virginia.edu
-m 255.255.255.255
tcpchains -a a -p 21 -r 2021 -i
tarek8.cs.virginia.edu -m 255.255.255.255
FTP connections from machine bobibdi
and tarek8 will be
forwarded to port 2021, the others will be forwarded to port 1021, since it's
the default target port.
tcpchains -a c -p <incoming port> -r <target port> -s <matching string>
example:
tcpchains -a c -p 80 -r 8081 -s
'gif'
tcpchains -a c -p 80 -r 8081 -s 'gif'
HTTP requests for 'gif' or 'jpg' will be forwarded
to port 8081, and the others will be forwarded to port 8080 since it's the
default target port
4. Other operations
tcpchains -D <rule type> -p <incoming port>
to delete the three rules created above, use:
tcpchains -D d -p 143
tcpchains -D a -p 21
tcpchains -D c -p 80
tcpchains -F <rule type>
another way to delete the three rules created above, use:
tcpchains -F d
tcpchains -F a
tcpchains -F c
tcpchains -C <rule type> -p <incoming port> -o <new default port>
tcpchains -X <rule type> -p <incoming port>
tcpchains -L <rule type>