ip rule and route doesn't get respected

I'm trying to route packets based on their source address, and have added the following:

# ip rule add from 10.10.10.0/16 dev eth0 table foobar
# ip route add default via 100.100.100.1 dev eth0 table foobar

Testing the routing however gives me wrong via address:

# ip route get 4.3.2.1 from 10.10.10.1
4.3.2.1 from 10.10.10.1 via 100.0.0.1 dev eth0

Why doesn't this get respected?

This is my regular routes

# ip route list
default via 100.0.0.1 dev eth0

and

# ip route show table foobar
default via 100.100.100.1 dev eth0

and

# ip rule list
0:  from all lookup local
32765:  from 10.10.10.0/16 iif eth0 lookup foobar
32766:  from all lookup main
32767:  from all lookup default

Solution 1:

Your issue isn't issue. In rule you use not only source address, but also input interface match. So, there are two ways to solve your "problem":

  1. Don't use the dev eth0 in the rule
  2. Add iif eth0 in the ip route get... command. The iif option allows you use non-local addresses in the ip route get command, so you can use something like: ip route get 4.3.2.1 from 10.10.20.253 iif eth0