# Destination IP based routing

## musv

Hello, 

I'm trying to understand the Linux routing, but until now I feel quite like a newbie. I'm quite confused about the rules, routes and routing tables. In several tutorials I found a lot about Source IP assignment to tables and devices and connection marking, which I didn't fully understand.

What's the situation

2 network devices:

eth0: IP 10.170.53.113/24, GW: 10.170.53.254

wlan0: IP 10.180.11.232/30, GW: 10.180.11.234

What do I want

Outgoing traffic for certain IP ranges should use wlan0, everything else eth0. More exactly:

Destination IPs: 66.0.0.0/8, 192.168.0.0/16, 87.0.0.0/16 should be routed via wlan0

all the other Destination IPs: eth0

How can I realize this?

----------

## devilheart

I believe this is enough

```

route add 66.0.0.0/8 gw 10.180.11.234

route add 192.168.0.0/16 gw 10.180.11.234

route add 87.0.0.0/16 gw 10.180.11.234

route add default gw 10.170.53.254

```

----------

## NeddySeagoon

musv,

Set static routes in your net file for 66.0.0.0/8

 192.168.0.0/16

  87.0.0.0/16

using 10.180.11.234 as a gateway.

Heres an example from my net file ... currently commented out

```
#routes_eth0="default via 62.x.y.z"

#             192.168.10.0/24 via 192.168.100.1"
```

For a single static route.  

If wlan0 is started some other way, you need to tell your network control tool to add the routes at startup and take them down at shutdown.

Then the traffic (if any) will be sent to your default gateway when wlan0 is down.

If that's not acceptable, you need a route to send it somewhere useless when wlan0 is down.

----------

## Maitreya

/etc/conf.d/net:

```

routes_wlan0="66.0.0.0/8 via 10.180.11.234

                      192.168.0.0/16 via 10.180.11.234

                      87.0.0.0/16 via 10.180.11.234"

routes_eth0="default via 10.170.53.254"

```

----------

## musv

Thanks a lot. Got it basically working. 

I'm using Systemd with networkd. But there are similar options. And with iproute2 it's working via:

```
ip route add 192.168.0.0/8 via 10.180.11.234
```

There was another small caveat. If proxy is specified in the browser settings, de facto it disables the routing rules.  :Smile: 

----------

## musv

Just to add some new ideas. Maybe someone else can use it.

While searching about this topic I stumbled into Network Namespaces. This solution has some advantages. You can realize disjunct networks, that don't see each other. And with this solution it's also easy to bind an application to a specific device.

Example: We have 2 devices: eth0 and wlan0. 

```

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

…

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000

…

3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
```

Now we want to separate wlan0 from the common network and create therefore a Network Namespace "wifins":

```
ip netns add wifins

ip link set wlan0 netns wifins

ip netns exec wifins /etc/init.d/wpa_supplicant start

ip netns exec wifins dhclient wlan0
```

As a result we have:

```

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

…

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
```

And inside the wifi-Namespace:

```

1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN group default qlen 1000

…

3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
```

To use this context we start a shell within this namespace:

```
ip netns exec wifins su - myuser -c xterm

firefox
```

Every command inside the started xterm (e.g. Firefox) runs inside this Network Namespace bound to wlan0.

----------

