Adv Routing HOWTO PDF
Adv Routing HOWTO PDF
Adv Routing HOWTO PDF
Table of Contents
Table of Contents
6.IPsec: secure IP over the internet........................................................................................................17
7.Multicast routing.................................................................................................................................17
8.Using Class Based Queueing for bandwidth management.................................................................18
8.1 What is queueing?............................................................................................................................18
8.2 First attempt at bandwidth division.................................................................................................19
8.3 What to do with excess bandwidth..................................................................................................22
8.4 Class subdivisions............................................................................................................................22
8.5 Loadsharing over multiple interfaces...............................................................................................22
9.More queueing disciplines..................................................................................................................23
9.1 pfifo_fast..........................................................................................................................................23
9.2 Stochastic Fairness Queueing..........................................................................................................23
9.3 Token Bucket Filter.........................................................................................................................23
9.4 Random Early Detect.......................................................................................................................24
9.5 Ingress policer qdisc........................................................................................................................24
9.6 WRR................................................................................................................................................25
10.Netfilter & iproute marking packets..............................................................................................25
11.More classifiers.................................................................................................................................26
11.1 The "fw" classifier.........................................................................................................................28
11.2 The "u32" classifier........................................................................................................................28
U32 selector..............................................................................................................................29
General selectors.......................................................................................................................30
Specific selectors......................................................................................................................31
11.3 The "route" classifier.....................................................................................................................31
11.4 The "rsvp" classifier.......................................................................................................................32
11.5 The "tcindex" classifier..................................................................................................................33
12.Kernel network parameters...............................................................................................................33
12.1 Reverse Path Filtering....................................................................................................................33
12.2 Obscure settings.............................................................................................................................34
Generic ipv4..............................................................................................................................34
Per device settings....................................................................................................................38
Neighbor pollicy.......................................................................................................................39
Routing settings........................................................................................................................40
13.Backbone applications of traffic control...........................................................................................42
13.1 Router queues.................................................................................................................................42
14.Cookbook..........................................................................................................................................43
14.1 Running multiple sites with different SLAs..................................................................................43
14.2 Protecting your host from SYN floods .........................................................................................44
14.3 Ratelimit ICMP to prevent dDoS...................................................................................................45
14.4 Prioritizing interactive traffic.........................................................................................................46
14.5 Transparent webcaching using netfilter, iproute2, ipchains and squid........................................47
Traffic flow diagram after implementation..............................................................................50
15.Advanced Linux Routing..................................................................................................................51
15.1 How does packet queueing really work?.......................................................................................51
15.2 Advanced uses of the packet queueing system..............................................................................52
15.3 Other packet shaping systems........................................................................................................52
16.Dynamic routing OSPF and BGP..................................................................................................52
17.Further reading..................................................................................................................................53
ii
Table of Contents
18.Acknowledgements ..........................................................................................................................54
iii
1.Dedication
2.Introduction
2.1 Disclaimer & License
2.2 Prior knowledge
2.3 What Linux can do for you
2.4 Housekeeping notes
2.5 Access, CVS & submitting updates
2.6 Mailing list
2.7 Layout of this document
3.Introduction to iproute2
3.1 Why iproute2?
3.2 iproute2 tour
3.3 Prerequisites
3.4 Exploring your current configuration
3.5 ARP
11.More classifiers
11.1 The "fw" classifier
11.2 The "u32" classifier
11.3 The "route" classifier
11.4 The "rsvp" classifier
11.5 The "tcindex" classifier
14.Cookbook
14.1 Running multiple sites with different SLAs
14.2 Protecting your host from SYN floods
14.3 Ratelimit ICMP to prevent dDoS
14.4 Prioritizing interactive traffic
14.5 Transparent webcaching using netfilter, iproute2, ipchains and squid
11.More classifiers
17.Further reading
18.Acknowledgements
1.Dedication
This document is dedicated to lots of people, and is my attempt to do something back. To list but a few:
Rusty Russell
Alexey N. Kuznetsov
The good folks from Google
The staff of Casema Internet
2.Introduction
Welcome, gentle reader.
This document hopes to enlighten you on how to do more with Linux 2.2/2.4 routing. Unbeknownst to most
users, you already run tools which allow you to do spectacular things. Commands like 'route' and 'ifconfig'
are actually very thin wrappers for the very powerful iproute2 infrastructure
I hope that this HOWTO will become as readable as the ones by Rusty Russell of (amongst other things)
netfilter fame.
You can always reach us by writing the HOWTO team.
$ export CVSROOT=:pserver:anon@outpost.ds9a.nl:/var/cvsroot
$ cvs login
CVS password: [enter 'cvs' (without 's)]
$ cvs co 2.4routing
cvs server: Updating 2.4routing
U 2.4routing/2.4routing.sgml
If you spot an error, or want to add something, just fix it locally, and run cvs diff u, and send the result off to
us.
A Makefile is supplied which should help you create postscript, dvi, pdf, html and plain text. You may need
to install sgmltools, ghostscript and tetex to get all formats.
3.Introduction to iproute2
3.3 Prerequisites
You should make sure that you have the userland tools installed. This package is called 'iproute' on both
RedHat and Debian, and may otherwise be found at
ftp://ftp.inr.ac.ru/iprouting/iproute22.2.4nowss??????.tar.gz". Some
parts of iproute require you to have certain kernel options enabled.
You can also try here for the latest version.
Your mileage may vary, but this is what it shows on my NAT router at home. I'll only explain part of the
output as not everything is directly relevant.
We first see the loopback interface. While your computer may function somewhat without one, I'd advise
3.2 iproute2 tour
This contains more information. It shows all our addresses, and to which cards they belong. 'inet' stands for
Internet (IPv4). There are lots of other address families, but these don't concern us right now.
Let's examine eth0 somewhat closer. It says that it is related to the inet address '10.0.0.1/8'. What does this
mean? The /8 stands for the number of bits that are in the Network Address. There are 32 bits, so we have 24
bits left that are part of our network. The first 8 bits of 10.0.0.1 correspond to 10.0.0.0, our Network Address,
and our netmask is 255.0.0.0.
The other bits are connected to this interface, so 10.250.3.13 is directly available on eth0, as is 10.0.0.1 for
example.
With ppp0, the same concept goes, though the numbers are different. Its address is 212.64.94.251, without a
subnet mask. This means that we have a pointtopoint connection and that every address, with the exception
of 212.64.94.251, is remote. There is more information however, it tells us that on the other side of the link is
yet again only one address, 212.64.94.1. The /32 tells us that there are no 'network bits'.
It is absolutely vital that you grasp these concepts. Refer to the documentation mentioned at the beginning of
this HOWTO if you have trouble.
You may also note 'qdisc', which stands for Queueing Discipline. This will become vital later on.
This is pretty much self explanatory. The first 4 lines of output explicitly state what was already implied by
ip address show, the last line tells us that the rest of the world can be found via 212.64.94.1, our default
gateway. We can see that it is a gateway because of the word via, which tells us that we need to send packets
to 212.64.94.1, and that it will take care of things.
For reference, this is what the old 'route' utility shows us:
[ahu@home ahu]$ route n
Kernel IP routing table
Destination
Gateway
Iface
212.64.94.1
0.0.0.0
10.0.0.0
0.0.0.0
127.0.0.0
0.0.0.0
0.0.0.0
212.64.94.1
Genmask
255.255.255.255
255.0.0.0
255.0.0.0
0.0.0.0
UH
U
U
UG
0
0
0
0
0
0
0
0
Use
0
0
0
0
ppp0
eth0
lo
ppp0
3.5 ARP
ARP is the Address Resolution Protocol as described in RFC 826. ARP is used by a networked machine to
resolve the hardware location/address of another machine on the same local network. Machines on the
Internet are generally known by their names which resolve to IP addresses. This is how a machine on the
foo.com network is able to communicate with another machine which is on the bar.net network. An IP
address, though, cannot tell you the physical location of a machine. This is where ARP comes into the
picture.
Let's take a very simple example. Suppose I have a network composed of several machines. Two of the
machines which are currently on my network are foo with an IP address of 10.0.0.1 and bar with an IP
address of 10.0.0.2. Now foo wants to ping bar to see that he is alive, but alas, foo has no idea where bar is.
So when foo decides to ping bar he will need to send out an ARP request. This ARP request is akin to foo
shouting out on the network "Bar (10.0.0.2)! Where are you?" As a result of this every machine on the
network will hear foo shouting, but only bar (10.0.0.2) will respond. Bar will then send an ARP reply directly
back to foo which is akin bar saying, "Foo (10.0.0.1) I am here at 00:60:94:E9:08:12." After this simple
transaction used to locate his friend on the network foo is able to communicate with bar until he (his arp
ip shows us our routes
10
As you can see my machine espa041 (9.3.76.41) knows where to find espa042 (9.3.76.42) and espagate
(9.3.76.1). Now let's add another machine to the arp cache.
As a result of espa041 trying to contact espa043, espa043's hardware address/location has now been added to
the arp/nieghbor cache. So until the entry for espa043 times out (as a result of no communication between the
two) espa041 knows where to find espa043 and has no need to send an ARP request.
Now let's delete espa043 from our arp cache:
Now espa041 has again forgotten where to find espa043 and will need to send another ARP request the next
time he needs to communicate with espa043. You can also see from the above output that espagate (9.3.76.1)
has been changed to the "stale" state. This means that the location shown is still valid, but it will have to be
confirmed at the first transaction to that machine.
11
list
local
main
default
This lists the priority of all rules. We see that all rules apply to all packets ('from all'). We've seen the 'main'
table before, it's output by ip route ls, but the 'local' and 'default' table are new.
If we want to do fancy things, we generate rules which point to different tables which allow us to override
system wide routing rules.
For the exact semantics on what the kernel does when there are more matching rules, see Alexey's ipcref
documentation.
12
proto kernel
scope host
src 127.0.0.1
Lots of obvious things, but things that need to specified somewhere. Well, here they are. The default table is
empty.
Let's view the 'main' table:
[ahu@home ahu]$ ip route list table main
195.96.98.253 dev ppp2 proto kernel scope link src 212.64.78.148
212.64.94.1 dev ppp0 proto kernel scope link src 212.64.94.251
10.0.0.0/8 dev eth0 proto kernel scope link src 10.0.0.1
127.0.0.0/8 dev lo scope link
default via 212.64.94.1 dev ppp0
We now generate a new rule which we call 'John', for our hypothetical house mate. Although we can work
with pure numbers, it's far easier if we add our tables to /etc/iproute2/rt_tables.
Now all that is left is to generate Johns table, and flush the route cache:
# ip route add default via 195.96.98.253 dev ppp2 table John
# ip route flush cache
And we are done. It is left as an exercise for the reader to implement this in ipup.
13
5.2 IP in IP tunneling
This kind of tunneling has been available in Linux for a long time. It requires 2 kernel modules, ipip.o and
new_tunnel.o.
Let's say you have 3 networks: Internal networks A and B, and intermediate network C (or let's say, Internet).
So we have network A:
network 10.0.1.0
netmask 255.255.255.0
router 10.0.1.1
insmod ipip.o
insmod new_tunnel.o
5.2 IP in IP tunneling
14
Presto, you're done. You can't forward broadcast or IPv6 traffic through an IPinIP tunnel, though. You just
connect 2 IPv4 networks that normally wouldn't be able to talk to each other, that's all. As far as compatibility
goes, this code has been around a long time, so it's compatible all the way back to 1.3 kernels. Linux
IPinIP tunneling doesn't work with other Operating Systems or routers, as far as I know. It's simple, it
works. Use it if you have to, otherwise use GRE.
IPv4 Tunneling
Let's do IPv4 tunneling first:
Let's say you have 3 networks: Internal networks A and B, and intermediate network C (or let's say, Internet).
So we have network A:
network 10.0.1.0
netmask 255.255.255.0
router 10.0.1.1
The router has address 172.16.17.18 on network C. Let's call this network neta (ok, hardly original)
and network B:
network 10.0.2.0
netmask 255.255.255.0
router 10.0.2.1
The router has address 172.19.20.21 on network C. Let's call this network netb (still not original)
As far as network C is concerned, we assume that it will pass any packet sent from A to B and vice versa.
How and why, we do not care.
On the router of network A, you do the following:
ip tunnel add netb mode gre remote 172.19.20.21 local 172.16.17.18 ttl 255
15
Let's discuss this for a bit. In line 1, we added a tunnel device, and called it netb (which is kind of obvious
because that's where we want it to go). Furthermore we told it to use the GRE protocol (mode gre), that the
remote address is 172.19.20.21 (the router at the other end), that our tunneling packets should originate from
172.16.17.18 (which allows your router to have several IP addresses on network C and let you decide which
one to use for tunneling) and that the TTL field of the packet should be set to 255 (ttl 255).
In the second line we gave the newly born interface netb the address 10.0.1.1. This is OK for smaller
networks, but when you're starting up a mining expedition (LOTS of tunnels), you might want to consider
using another IP range for tunneling interfaces (in this example, you could use 10.0.3.0).
In the third line we set the route for network B. Note the different notation for the netmask. If you're not
familiar with this notation, here's how it works: you write out the netmask in binary form, and you count all
the ones. If you don't know how to do that, just remember that 255.0.0.0 is /8, 255.255.0.0 is /16 and
255.255.255.0 is /24. Oh, and 255.255.254.0 is /23, in case you were wondering.
But enough about this, let's go on with the router of network B.
ip tunnel add neta mode gre remote 172.16.17.18 local 172.19.20.21 ttl 255
ip addr add 10.0.2.1 dev neta
ip route add 10.0.1.0/24 dev neta
IPv6 Tunneling
A short bit about IPv6 addresses:
IPv6 addresses are, compared to IPv4 addresses, monstrously big. An example:
3ffe:2502:200:40:281:48fe:dcfe:d9bc
So, to make writing them down easier, there are a few rules:
Don't use leading zeroes. Same as in IPv4.
Use colons to separate every 16 bits or two bytes.
When you have lots of consecutive zeroes, you can write this down as ::. You can only do this once
in an address and only for quantities of 16 bits, though.
Using these rules, the address 3ffe:0000:0000:0000:0000:0020:34A1:F32C can be written down as
IPv6 Tunneling
16
Network 3ffe:406:5:1:5:a:2:1/96
Your IPv4 address is 172.16.17.18, and the 6bone router has IPv4 address 172.22.23.24.
ip
ip
ip
ip
tunnel add sixbone mode sit remote 172.22.23.24 local 172.16.17.18 ttl 255
link set sixbone up
addr add 3ffe:406:5:1:5:a:2:1/96 dev sixbone
route add 3ffe::/15 dev sixbone
Let's discuss this. In the first line, we created a tunnel device called sixbone. We gave it mode sit (which is
IPv6 in IPv4 tunneling) and told it where to go to (remote) and where to come from (local). TTL is set to
maximum, 255. Next, we made the device active (up). After that, we added our own network address, and set
a route for 3ffe::/15 (which is currently all of 6bone) through the tunnel.
GRE tunnels are currently the preferred type of tunneling. It's a standard that's also widely adopted outside
the Linux community and therefore a Good Thing.
7.Multicast routing
FIXME: Editor Vacancy! (somebody is working on it, though)
17
18
Now, our Linux router has two interfaces which I shall dub eth0 and eth1. eth1 is connected to our router
which moves packets from to and from our fibre link.
eth0 is connected to a subnet which contains both the corporate firewall and our network head ends, through
which we can connect to our customers.
Because we can only limit what we send, we need two separate but possibly very similar sets of rules. By
modifying queueing on eth0, we determine how fast data gets sent to our customers, and therefore how much
downstream bandwidth is available for them. Their 'download speed' in short.
On eth1, we determine how fast we send data to The Internet, how fast our users, both corporate and
commercial can upload data.
19
Even more numbers to worry about the Linux CBQ implementation is very generic. With 'parent 10:0' we
indicate that this class descends from the root of qdisc handle '10:' we generated earlier. With 'classid 10:1'
we name this class.
We really don't tell the kernel a lot more, we just generate a class that completely fills the available device.
We also specify that the MTU (plus some overhead) is 1514 octets. We also 'weigh' this class with 1Mbit a
tuning parameter.
We now generate our ISP class:
# tc class add dev eth0 parent 10:1 classid 10:100 cbq bandwidth 10Mbit rate \
8Mbit allot 1514 weight 800Kbit prio 5 maxburst 20 avpkt 1000 \
bounded
We allocate 8Mbit, and indicate that this class must not exceed this by adding the 'bounded' parameter.
Otherwise this class would have started borrowing bandwidth from other classes, something we will discuss
later on.
To top it off, we generate the root Office class:
# tc class add dev eth0 parent 10:1 classid 10:200 cbq bandwidth 10Mbit rate \
2Mbit allot 1514 weight 200Kbit prio 5 maxburst 20 avpkt 1000 \
bounded
+[10: 10Mbit]+
|+[10:1 root 10Mbit]+|
||
||
|| +[10:100 8Mbit]+ [10:200 2Mbit] ||
|| |
| |
| ||
|| |
ISP
| |
Office
| ||
|| |
| |
| ||
|| ++ ++ ||
||
||
|++|
++
20
# tc qdisc add dev eth0 parent 10:100 sfq quantum 1514b perturb 15
# tc qdisc add dev eth0 parent 10:200 sfq quantum 1514b perturb 15
In this case we install the Stochastic Fairness Queueing discipline (sfq), which is not quite fair, but works
well up to high bandwidths without burning up CPU cycles. There are other queueing disciplines available
which are better, but need more CPU. The Token Bucket Filter is often used.
Now there is only one thing left to do and that is to explain to the kernel which packets belong to which class.
Initially we will do this natively with iproute2, but more interesting applications are possible in combination
with netfilter.
# tc filter add dev eth0 parent 10:0 protocol ip prio 100 u32 match ip dst \
150.151.23.24 flowid 10:200
# tc filter add dev eth0 parent 10:0 protocol ip prio 25 u32 match ip dst \
150.151.0.0/16 flowid 10:100
Here is is assumed that our office hides behind a firewall with IP address 150.151.23.24 and that all our other
IP addresses should be considered to be part of the ISP.
The u32 match is a very simple one more sophisticated matching rules are possible when using netfilter to
mark our packets, which we can than match on in tc.
Now we have fairly divided the downstream bandwidth, we need to do the same for the upstream. For
brevity's sake, all in one go:
# tc qdisc add dev eth1 root handle 20: cbq bandwidth 10Mbit avpkt 1000
# tc class add dev eth1 parent 20:0 classid 20:1 cbq bandwidth 10Mbit rate \
10Mbit allot 1514 weight 1Mbit prio 8 maxburst 20 avpkt 1000
# tc class add dev eth1 parent 20:1 classid 20:100 cbq bandwidth 10Mbit rate \
8Mbit allot 1514 weight 800Kbit prio 5 maxburst 20 avpkt 1000 \
bounded
# tc class add dev eth1 parent 20:1 classid 20:200 cbq bandwidth 10Mbit rate \
2Mbit allot 1514 weight 200Kbit prio 5 maxburst 20 avpkt 1000 \
bounded
# tc qdisc add dev eth1 parent 20:100 sfq quantum 1514b perturb 15
# tc qdisc add dev eth1 parent 20:200 sfq quantum 1514b perturb 15
# tc filter add dev eth1 parent 20:0 protocol ip prio 100 u32 match ip src \
150.151.23.24 flowid 20:200
21
22
9.1 pfifo_fast
This queue is, as the name says, First In, First Out, which means that no packet receives special treatment. At
least, not quite. This queue has 3 so called 'bands'. Within each band, FIFO rules apply. However, as long as
there are packets waiting in band 0, band 1 won't be processed. Same goes for band 1 and band 2.
23
The last scenario is very important, because it allows to administratively shape the bandwidth available to
data, passing the filter. The accumulation of tokens allows short burst of overlimit data to be still passed
without loss, but any lasting overload will cause packets to be constantly dropped.
The Linux kernel seems to go beyond this specification, and also allows us to limit the speed of the burst
transmission. However, Alexey warns us:
Note that the peak rate TBF is much more tough: with MTU 1500
P_crit = 150Kbytes/sec. So, if you need greater peak rates,
use alpha with HZ=1000 :)
24
In short: you can use this to limit how fast your computer downloads files, thus leaving more of the available
bandwidth for others.
See the section on protecting your host from SYN floods for an example on how this works.
9.6 WRR
This qdisc is not included in the standard kernels but can be downloaded from http://wiplwrr.dkik.dk/wrr/.
Currently the qdisc is only tested with Linux 2.2 kernels but it will probably work with 2.4 kernels too.
The WRR qdisc distributes bandwidth between its classes using the weighted round robin scheme. That is,
like the CBQ qdisc it contains classes into which arbitrary qdiscs can be plugged. All classes which have
sufficient demand will get bandwidth proportional to the weights associated with the classes. The weights can
be set manually using the tc program. But they can also be made automatically decreasing for classes
transferring much data.
The qdisc has a buildin classifier which assigns packets coming from or sent to different machines to
different classes. Either the MAC or IP and either source or destination addresses can be used. The MAC
address can only be used when the Linux box is acting as an ethernet bridge, however. The classes are
automatically assigned to machines based on the packets seen.
The qdisc can be very useful at sites such as dorms where a lot of unrelated individuals share an Internet
connection. A set of scripts setting up a relevant behavior for such a site is a central part of the WRR
distribution.
9.6 WRR
25
Now we generate the mail.out table with a route to the slow but cheap link:
# /sbin/ip route add default via 195.96.98.253 dev ppp0 table mail.out
And we are done. Should we want to make exceptions, there are lots of ways to achieve this. We can modify
the netfilter statement to exclude certain hosts, or we can insert a rule with a lower priority that points to the
main table for our excepted hosts.
We can also use this feature to honour TOS bits by marking packets with a different type of service with
different numbers, and creating rules to act on that. This way you can even dedicate, say, an ISDN line to
interactive sessions.
Needless to say, this also works fine on a host that's doing NAT ('masquerading').
Note: for this to work, you need to have some options enabled in your kernel:
See also Transparent webcaching using netfilter, iproute2, ipchains and squid in the Cookbook.
11.More classifiers
Classifiers are the way by which the kernel decides which queue a packet should be placed into. There are
various different classifiers, each of which can be used for different purposes.
fw
11.More classifiers
26
protocol
The protocol this classifier will accept. Generally you will only be accepting only IP traffic.
Required.
parent
The handle this classifier is to be attached to. This handle must be an already existing class.
Required.
prio
The priority of this classifier. Higher numbers get tested first.
handle
This handle means different things to different filters.
FIXME: Add more
All the following sections will assume you are trying to shape the traffic going to HostA. They will assume
that the root class has been configured on 1: and that the class you want to send the selected traffic to is 1:1.
11.More classifiers
27
Now all packets to that machine are tagged with the mark 1. Now we build the packet shaping rules to
actually shape the packets. Now we just need to indicate that we want the packets that are tagged with the
mark 1 to go to class 1:1. This is accomplished with the command:
# tc filter add dev eth1 protocol ip parent 1:0 prio 1 handle 1 fw classid 1:1
This should be fairly selfexplanatory. Attach to the 1:0 class a filter with priority 1 to filter all packet
marked with 1 in the firewall to class 1:1. Note how the handle here is used to indicate what the mark should
be.
That's all there is to it! This is the (IMHO) easy way, the other ways are I think harder to understand. Note
that you can apply the full power of the firewalling code with this classifier, including matching MAC
addresses, user IDs and anything else the firewall can match.
The protocol field describes protocol that the filter will be applied to. We will only discuss case of
ip protocol. The preference field (priority can be used alternatively) sets the priority of currently
11.1 The "fw" classifier
28
U32 selector
The U32 selector contains definition of the pattern, that will be matched to the currently processed packet.
Precisely, it defines which bits are to be matched in the packet header and nothing more, but this simple
method is very powerful. Let's take a look at the following examples, taken directly from a pretty complex,
realworld filter:
# filter parent 1: protocol ip pref 10 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1
match 00100000/00ff0000 at 0
For now, leave the first line alone all these parameters describe the filter's hash tables. Focus on the selector
line, containing match keyword. This selector will match to IP headers, whose second byte will be 0x10
(0010). As you can guess, the 00ff number is the match mask, telling the filter exactly which bits to match.
Here it's 0xff, so the byte will match if it's exactly 0x10. The at keyword means that the match is to be
started at specified offset (in bytes) in this case it's beginning of the packet. Translating all that to human
language, the packet will match if its Type of Service field will have `low delay' bits set. Let's analyze
another rule:
# filter parent 1: protocol ip pref 10 u32 fh 800::803 order 2051 key ht 800 bkt 0 flowid 1
match 00000016/0000ffff at nexthdr+0
The nexthdr option means next header encapsulated in the IP packet, i.e. header of upperlayer protocol.
The match will also start here at the beginning of the next header. The match should occur in the second,
32bit word of the header. In TCP and UDP protocols this field contains packet's destination port. The
number is given in bigendian format, i.e. older bits first, so we simply read 0x0016 as 22 decimal, which
stands for SSH service if this was TCP. As you guess, this match is ambigous without a context, and we will
discuss this later.
Having understood all the above, we will find the following selector quite easy to read: match
c0a80100/ffffff00 at 16. What we got here is a three byte match at 17th byte, counting from the
IP header start. This will match for packets with destination address anywhere in 192.168.1/24 network. After
analyzing the examples, we can summarize what we have learnt.
U32 selector
29
General selectors
General selectors define the pattern, mask and offset the pattern will be matched to the packet contents. Using
the general selectors you can match virtually any single bit in the IP (or upper layer) header. They are more
difficult to write and read, though, than specific selectors that described below. The general selector syntax is:
One of the keywords u32, u16 or u8 specifies length of the pattern in bits. PATTERN and MASK should
follow, of length defined by the previous keyword. The OFFSET parameter is the offset, in bytes, to start
matching. If nexthdr+ keyword is given, the offset is relative to start of the upper layer header.
Some examples:
Packet will match to this rule, if its time to live (TTL) is 64. TTL is the field starting just after 8th byte of
the IP header.
This rule will only match TCP packets with ACK bit set. Here we can see an example of using two selectors,
the final result will be logical AND of their results. If we take a look at TCP header diagram, we can see that
the ACK bit is second older bit (0x10) in the 14th byte of the TCP header (at nexthdr+13). As for the
second selector, if we'd like to make our life harder, we could write match u8 0x06 0xff at
9 instead if using the specific selector protocol tcp, because 6 is the number of TCP protocol, present in
10th byte of the IP header. On the other hand, in this example we couldn't use any specific selector for the
first match simply because there's no specific selector to match TCP ACK bits.
General selectors
30
Specific selectors
The following table contains a list of all specific selectors the author of this section has found in the
tc program source code. They simply make your life easier and increase readability of your filter's
configuration.
FIXME: table placeholder the table is in separate file ,,selector.html''
FIXME: it's also still in Polish :(
FIXME: must be sgml'ized
Some examples:
The above rule will match packets, which have the TOS field set to 0x10. The TOS field starts at second byte
of the packet and is one byte big, so we coul write an equivalent general selector: match u8 0x10 0xff
at 1. This gives us hint to the internals of U32 filter the specific rules are always translated to general
ones, and in this form they are stored in the kernel memory. This leads to another conclusion the tcp and
udp selectors are exactly the same and this is why you can't use single match tcp dst 53
0xffff selector to match TCP packets sent to given port they will also match UDP packets sent to this
port. You must remember to also specify the protocol and end up with the following rule:
# tc filter add dev eth1 parent 1:0 protocol ip prio 100 route
Specific selectors
31
For instance, we can define our destination network 192.168.10.0 with a realm number 10:
When adding route filters, we can use realm numbers to represent the networks or hosts and specify how the
routes match the filters.
The above rule says packets going to the network 192.168.10.0 match class id 1:10.
Route filter can also be used to match source routes. For example, there is a subnetwork attached to the Linux
router on eth2.
Here the filter specifies that packets from the subnetwork 192.168.2.0 (realm 2) will match class id 1:2.
32
# for i in /proc/sys/net/ipv4/conf/*/rp_filter ; do
> echo 2 > $i
> done
Going by the example above, if a packet arrived on the Linux router on eth1 claiming to come from the
Office+ISP subnet, it would be dropped. Similarly, if a packet came from the Office subnet, claiming to be
from somewhere outside your firewall, it would be dropped also.
The above is full reverse path filtering. The default is to only filter based on IPs that are on directly connected
networks. This is because the full filtering breaks in the case of asymmetric routing (where packets come in
one way and go out another, like satellite traffic, or if you have dynamic (bgp, ospf, rip) routes in your
network. The data comes down through the satellite dish and replies go back through normal landlines).
If this exception applies to you (and you'll probably know if it does) you can simply turn off the rp_filter on
the interface where the satellite data comes in. If you want to see if any packets are being dropped, the
log_martians file in the same directory will tell the kernel to log them to your syslog.
33
Generic ipv4
As a generic note, most rate limiting features don't work on loopback, so don't test them locally. The limits
are supplied in 'jiffies', and are enforced using the earlier mentioned token bucket filter.
The kernel has an internal clock which runs at 'HZ' ticks (or 'jiffies') per second. On intel, 'HZ' is mostly 100.
So setting a *_rate file to, say 50, would allow for 2 packets per second. The token bucket filter is also
configured to allow for a burst of at most 6 packets, if enough tokens have been earned.
/proc/sys/net/ipv4/icmp_destunreach_rate
If the kernel decides that it can't deliver a packet, it will drop it, and send the source of the
packet an ICMP notice to this effect.
/proc/sys/net/ipv4/icmp_echo_ignore_all
Don't act on echo packets at all. Please don't set this by default, but if you are used as a relay
in a DoS attack, it may be useful.
/proc/sys/net/ipv4/icmp_echo_ignore_broadcasts [Useful]
If you ping the broadcast address of a network, all hosts are supposed to respond. This makes
for a dandy denialofservice tool. Set this to 1 to ignore these broadcast messages.
/proc/sys/net/ipv4/icmp_echoreply_rate
The rate at which echo replies are sent to any one destination.
/proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
FIXME: fill this in
/proc/sys/net/ipv4/icmp_paramprob_rate
12.2 Obscure settings
34
35
36
37
38
Neighbor pollicy
Dev can either stand for a real interface, or for 'all' or 'default'. Default also changes settings for interfaces yet
to be created.
/proc/sys/net/ipv4/neigh/DEV/anycast_delay
FIXME: fill this in
/proc/sys/net/ipv4/neigh/DEV/app_solicit
FIXME: fill this in
/proc/sys/net/ipv4/neigh/DEV/base_reachable_time
FIXME: fill this in
/proc/sys/net/ipv4/neigh/DEV/delay_first_probe_time
FIXME: fill this in
/proc/sys/net/ipv4/neigh/DEV/gc_stale_time
FIXME: fill this in
/proc/sys/net/ipv4/neigh/DEV/locktime
FIXME: fill this in
/proc/sys/net/ipv4/neigh/DEV/mcast_solicit
FIXME: fill this in
Neighbor pollicy
39
Routing settings
/proc/sys/net/ipv4/route/error_burst
FIXME: fill this in
/proc/sys/net/ipv4/route/error_cost
FIXME: fill this in
/proc/sys/net/ipv4/route/flush
FIXME: fill this in
/proc/sys/net/ipv4/route/gc_elasticity
FIXME: fill this in
/proc/sys/net/ipv4/route/gc_interval
FIXME: fill this in
/proc/sys/net/ipv4/route/gc_min_interval
FIXME: fill this in
/proc/sys/net/ipv4/route/gc_thresh
Routing settings
40
Routing settings
41
42
14.Cookbook
This section contains 'cookbook' entries which may help you solve problems. A cookbook is no replacement
for understanding however, so try and comprehend what is going on.
It is up to you to attach the different servers to the right IP address. All popular daemons have support for
this.
We first attach a CBQ qdisc to eth0:
# tc qdisc add dev eth0 root handle 1: cbq bandwidth 10Mbit cell 8 avpkt 1000 \
mpu 64
# tc class add dev eth0 parent 1:0 classid 1:1 cbq bandwidth 10Mbit rate \
14.Cookbook
43
#! /bin/sh x
#
# sample script on using the ingress capabilities
# this script shows how one can rate limit incoming SYNs
# Useful for TCPSYN attack protection. You can use
# IPchains to have more powerful additions to the SYN (eg
# in addition the subnet)
#
#path to various utilities;
#change to reflect yours.
#
TC=/sbin/tc
IP=/sbin/ip
IPTABLES=/sbin/iptables
INDEV=eth2
#
# tag all incoming SYN packets through $INDEV as mark value 1
############################################################
$iptables A PREROUTING i $INDEV t mangle p tcp syn \
j MARK setmark 1
############################################################
#
# install the ingress qdisc on the ingress interface
############################################################
$TC qdisc add dev $INDEV handle ffff: ingress
44
#
echo " qdisc parameters Ingress "
$TC qdisc ls dev $INDEV
echo " Class parameters Ingress "
$TC class ls dev $INDEV
echo " filter parameters Ingress "
$TC filter ls dev $INDEV parent ffff:
#deleting the ingress qdisc
#$TC qdisc del $INDEV ingress
Rate limiting goes much as shown earlier. To refresh your memory, our ASCIIgram again:
# tc qdisc add dev eth0 root handle 10: cbq bandwidth 10Mbit avpkt 1000
# tc class add dev eth0 parent 10:0 classid 10:1 cbq bandwidth 10Mbit rate \
10Mbit allot 1514 prio 5 maxburst 20 avpkt 1000
If you have 100Mbit, or more, interfaces, adjust these numbers. Now you need to determine how much ICMP
14.3 Ratelimit ICMP to prevent dDoS
45
This limits at 100Kbit. Now we need a filter to assign ICMP traffic to this class:
# tc filter add dev eth0 parent 10:0 protocol ip prio 100 u32 match ip
protocol 1 0xFF flowid 10:100
The most common use is to set telnet & ftp control connections to "Minimum Delay" and FTP data to
"Maximum Throughput". This would be done as follows, on your upstream router:
14.4 Prioritizing interactive traffic
46
Now, this only works for data going from your telnet foreign host to your local computer. The other way
around appears to be done for you, ie, telnet, ssh & friends all set the TOS field on outgoing packets
automatically.
Should you have a client that does not do this, you can always do it with netfilter. On your local box:
47
First, make all traffic pass through naret by making sure it is the default gateway except for silom. Silom's
default gateway has to be donmuang (10.0.0.3) or this would create web traffic loop.
(all servers on my network had 10.0.0.1 as the default gateway which was the former IP address of donmuang
router so what I did was changed the IP address of donmuang to 10.0.0.3 and gave naret ip address of
10.0.0.1)
Silom
48
For more information on setting Squid server please refer to Squid faq page on http://squid.nlanr.net).
Make sure ip forwarding is enabled on this server and the default gateway for this server is donmuang router
(NOT naret).
Naret
49
On naret:
naret# iptables t mangle L
Chain PREROUTING (policy ACCEPT)
target
prot opt source
MARK
tcp anywhere
destination
anywhere
destination
naret# ip rule ls
0:
from all lookup local
32765: from all fwmark
2 lookup www.out
32766: from all lookup main
32767: from all lookup default
naret# ip route list table www.out
default via 203.114.224.8 dev eth0
naret# ip route
10.0.0.1 dev eth0 scope link
10.0.0.0/24 dev eth0 proto kernel
127.0.0.0/8 dev lo scope link
default via 10.0.0.3 dev eth0
scope link
src 10.0.0.1
(make sure silom belongs to one of the above lines, in this case
it's the line with 10.0.0.0/24)
||
|DONE|
||
||
|Traffic flow diagram after implementation|
||
INTERNET
/\
||
50
Note that the network is asymmetric as there is one extra hop on general outgoing path.
Here is run down for packet traversing the network from kaosarn
to and from the internet.
For web/http traffic:
kaosarn http request>naret>silom>donmuang>internet
http replies from internet>donmuang>silom>kaosarn
For nonweb/http requests(eg. telnet):
kaosarn outgoing data>naret>donmuang>internet
incoming data from internet>donmuang>kaosarn
51
For OSPF:
Moy, John T. "OSPF. The anatomy of an Internet routing protocol" Addison Wesley. Reading, MA. 1998.
Halabi has also written a good guide to OSPF routing design, but this appears to have been dropped from the
Cisco web site.
For BGP:
Halabi, Bassam "Internet routing architectures" Cisco Press (New Riders Publishing). Indianapolis, IN. 1997.
52
Although the examples are Ciscospecific, they are remarkably similar to the configuration language in
Zebra :)
17.Further reading
http://snafu.freedom.org/linux2.2/iproutenotes.html
Contains lots of technical information, comments from the kernel
http://www.davin.ottawa.on.ca/ols/
Slides by Jamal Hadi, one of the authors of Linux traffic control
http://defiant.coinet.com/iproute2/ipcref/
HTML version of Alexeys LaTeX documentation explains part of iproute2 in great detail
http://www.aciri.org/floyd/cbq.html
Sally Floyd has a good page on CBQ, including her original papers. None of it is Linux
specific, but it does a fair job discussing the theory and uses of CBQ. Very technical stuff,
but good reading for those so inclined.
http://ceti.pl/~kravietz/cbq/NET4_tc.html
Yet another HOWTO, this time in Polish! You can copy/paste command lines however, they
work just the same in every language. The author is cooperating with us and may soon author
sections of this HOWTO.
Differentiated Services on Linux
Discussion on how to use Linux in a diffserv compliant environment. Pretty far removed
from your everyday routing needs, but very interesting none the less. We may include a
section on this at a later date.
IOS Committed Access Rate
>From the helpful folks of Cisco who have the laudable habit of putting their documentation
online. Cisco syntax is different but the concepts are the same, except that we can do more
and do it without routers the price of cars :)
17.Further reading
53
18.Acknowledgements
It is our goal to list everybody who has contributed to this HOWTO, or helped us demystify how things work.
While there are currently no plans for a Netfilter type scoreboard, we do like to recognise the people who are
helping.
Jamal Hadi <hadi%cyberus.ca>
Nadeem Hasan <nhasan@usa.net>
Philippe Latu <philippe.latu%linuxfrance.org>
Jason Lunz <j@cc.gatech.edu>
Alexey Mahotkin <alexm@formulabez.ru>
Pawel Krawczyk <kravietz%alfa.ceti.pl>
Wim van der Most
Ram Narula <ram@princess1.net>
Rusty Rusell (with apologies for always misspelling your name)
Charles Tassell <ctassell%isn.net>
Glen Turner <glen.turner%aarnet.edu.au>
Song Wang <wsong@ece.uci.edu>
18.Acknowledgements
54