Skip to contentSkip to conversation

Routing Incoming and Outgoing Packets Differently

DISCUSSION Jim Doble 2 participants 2 messages Jul 20, 2006

lore
Share & tools
lore

Jim Doble

author
#0 lore raw

Is there a way to use one set of routing rules for incoming packets and a different set of routing rules for outgoing packets? Or is there a way to set a socket option so that packets sent on a given socket are routed using a different routing table?

Jim Doble
Tavve SoftwareCompany

#1 lore raw

Hello,

Quoted 1 line · Jim Doble

Is there a way to use one set of routing rules for incoming packets and a different set of routing rules for outgoing packets?

Yes, with advanced routing.

Quote from ip manpage (http://lartc.org/manpages/ip.txt) :

ip rule add - insert a new rule
[...]
iif NAME
select the incoming device to match. If the inter- face is loopback, the rule only matches packets originating from this host. This means that you may create separate routing tables for forwarded and local packets and, hence, completely segregate them.
[end of quote]

So you can create an alternate routing table for locally-generated packets. Incoming packets will use the default "main" routing table.

ip rule add iif lo lookup <output_table_number> prio 32765 ip route add <destination1> gw <gateway> table <output_table_number> ip route add <destination2> dev <interface> table <output_table_number>
...

You need to have the following options enabled in your kernel to use this feature. CONFIG_IP_ADVANCED_ROUTER=y
CONFIG_IP_MULTIPLE_TABLES=y