Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How to route only specific subnet (source ip) to a particular interface?
OS: Linux

I know I can do easily by destination IP by using something like

route add 1.2.3.4/24 dev eth4

but I do not see how can route based on source IP.

share|improve this question
    
This is called policy based routing, and is implementation dependent. What device/OS are you wanting to do this on? –  Paul Jan 10 '12 at 2:38
    
@Paul: the OS is Linux, I edited the question –  Alex Jan 10 '12 at 3:33
add comment

3 Answers

up vote 8 down vote accepted

You need to use policy based routing. Something kind of like

ip rule add from <source>/<mask> table <name>
ip route add 1.2.3.4/24 table <name> dev eth4

<name> is either table name specified in /etc/iproute2/rt_tables or you can use numeric id ...

This pretty much says, that all traffic from 1.2.3.4/24 will be routed using routing table <name>. IIRC it doesen't use the default table after going through this, so if you need other routes (ie. default gateway), you need to add them to the table as well.

(can't test it ATM but i believe it's pretty much right :) )

share|improve this answer
    
I was about to add my own answer after some research and testing and Fox, you beat me to it with exactly what I found :) –  Alex Jan 12 '12 at 2:19
1  
one correction (but not sure if your one will also work. for the second command: ip route add default dev eth4 table <name> (table directive should go at the end I think, just like in the first command... also default can be replaced by a subnet, just like in your example) –  Alex Jan 12 '12 at 2:23
add comment

You must use the route command. For example if you want to route all ip of 192.168.1.xxx to a particular device you have to use the following command:

route add -net 192.168.1.0 netmask 255.255.255.0 dev eth0

With that command you say that all the packets sent from that subnet must be sent to the eth0 interface. If you need to add also a gateway for that connection you can use that:

route add -net 192.168.1.0 netmask 255.255.255.0  gw 192.168.1.1
share|improve this answer
    
-net 192.168.1.0 specifies source IP address, not destination IP? –  Alex Jan 10 '12 at 9:13
    
is the source network, if you want to specify a host you must use -host. –  Ivan Jan 10 '12 at 9:41
1  
you seem to be wrong. I have tried this and both -net and -host create dest IP routes. The difference is -net can be used with netmask while -host creates a route with netmask of 255.255.255.255. However, both commands create dest IP routes, not source routes! –  Alex Jan 10 '12 at 10:13
1  
ooops sorry!!! I misunderstand your question :( Yeah it is totally wrong. –  Ivan Jan 10 '12 at 10:36
    
maybe you can try to play with iptables. linux.die.net/man/8/iptables here the man page. –  Ivan Jan 10 '12 at 10:38
show 1 more comment

http://www.wlug.org.nz/SourceBasedRouting

This site has a nice example of source based routing.

share|improve this answer
    
yes, nice and detailed article, a little bit complex for my simple need –  Alex Jan 12 '12 at 2:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.