31.3. Interactions with Other Kernel Subsystems
Between the time a packet makes its appearance in the system, because it was either received on one interface or generated locally, and the time it is delivered to the next hop (if forwarded) or locally (if addressed to the local host), several network subsystems may place their hands on it. Among them are the Firewall and Traffic Control subsystems. Both of them can classify traffic based on various databases of information and store the result of their classification into a field of the buffer descriptor. The routing subsystem code can also classify traffic and store the result in the buffer descriptor.
Figure 31-6 is a simplified overview of how routing, Firewall, and Traffic Control interact, and when a given subsystem comes into the picture. The figure shows how an input packet goes through the various subsystems and gets its firewall and routing tags initialized.
31.3.1. Routing Table Based Classifier
Among the many classifiers available to the Traffic Control subsystem is one called the routing table based classifier that can classify routes based on realms. Realms are numerical tags that can be assigned to both policies and routes. Each route and policy can be assigned up to two realms: an ingress realm and an egress realm.
In the following subsections, we will first see how realms are configured to get more familiar with the feature. Then I'll describe the logic used by the routing code to derive the classification tag (which will be used by Traffic Control) from the realms' configuration.
The file ip-cref.ps included with the IPROUTE2 package offers some examples of the purpose and use of realms. In this book, we will consider only how realms are configured via IPROUTE2 commands.
Both routes and policies are configured with the ip command. The first keyword that follows ip determines the object type you want to configure. The route keyword denotes a route, and the rule keyword denotes a policy.
Figure 31-6. Interactions among routing, Traffic Control, and Firewall (Netfilter)
22.214.171.124. Configuring policy realms
Routing policies are configured with the IPROUTE2's ip rule command. Its syntax is:
ip rule add...realms [source_realm/]destination_realm
As you can see, the source realm is optional while the destination realm is not, which means that if you provide only one value, you configure the destination realm.
Here are a couple of examples of commands that configure policy realms. The following associates the policy destination realm 128 with all traffic that originates in the subnet 10.0.1.0/24:
ip rule add from 10.0.1.0/24 realms 128
The following command associates the policy source realm 64 and the policy destination realm 128 with traffic that originates in the 10.0.1.0/24 subnet and that is addressed to the 10.0.2.0/24 subnet:
ip rule add from 10.0.1.0/24 to 10.0.2.0/24 realms 64/128
126.96.36.199. Configuring route realms
A route's realms are configured very similarly to policy realms. The syntax of the IPROUTE2 command for this purpose is:
ip route add...realms [source_realm/]destination_realm
Note that even though the command's help message does not show both the source and destination realms, the syntax is just the same as for policies.
Here is an example command for traffic directed to the 10.0.1.0/24 subnet that forwards the traffic to the gateway with address 10.0.0.3 and assigns to the destination realm 100:
ip route add 10.0.1.0/24 via 10.0.0.3 realms 100
In the following command, traffic directed to the 10.0.1.0/24 subnet is forwarded to the gateway with address 10.0.0.3 and assigned to the source realm 100 and the destination realm 200:
ip route add 10.0.1.0/24 via 10.0.0.3 realms 100/200
188.8.131.52. Computing the routing tag
Because realms can be assigned to both individual routes and whole policies, a routing decision can come up with two realms for a single direction: for instance, an ingress destination realm derived from the policy and another ingress destination realm derived from the route. In such a case, the realm derived from the route is given higher priority. Usually, such a decision is necessary only for a destination realm; administrators rarely define source realms on the basis of the route.
If a realm is missingnot provided by either route or policythe kernel computes the reverse route (from the local host back to the source of the packet being classified) and checks whether it can use the associated realms. For instance, if the kernel cannot derive a source realm for ingress traffic, it figures out and uses the destination realm for egress traffic on the reverse path. This heuristic assumes that the realm configurations on the two directions should be symmetric.
Let's look at a simple example using the topology in Figure 31-7, which shows a router between two networks. The policy routing configuration says that traffic coming from subnet 10.0.1.0/24 belongs to Realm A, and that traffic coming from subnet 10.0.2.0/24 belongs to Realm B. Assume that no route realm is configured; only the two policy realm configurations shown in Figure 31-7. Both of those policies provide only the source realmso when forwarding, a realm is specified for ingress but not for egress. Let's suppose now that the router receives a packet from host 10.0.1.100 (Realm A) directed to the destination address 10.0.2.200 (Realm B). When the routing subsystem makes a lookup to route the packet, it also computes the routing tag. The following list explains what happens.
In the end, the routing tag is initialized to source realm A and destination realm B. When the QoS layer is traversed later, it can use those two realms to correctly classify the packet.
Figure 31-7. Example of realm configuration
Figure 31-8 summarizes the logic used to compute the tag.
31.3.2. Policy Routing and Firewall-Based Classifier
The Netfilter firewall software can classify traffic to see whether, based on its filtering criteria, it needs to drop or mangle packets. The firewall can also be configured to simply classify a packet using its powerful classification engine just to provide a service to other kernel subsystems. The firewall has multiple hooks in the network stack. If Routing or Traffic Control runs after one of the hooks that places a tag, those subsystems can see and act on the tag. Figure 31-6, earlier in the chapter, showed the sequence in which various subsystems and firewall hooks access packets.
Figure 31-8. Logic used to compute the routing tag