Today Alex Fiestas told me about one problem he has and that I had long ago.
Everybody uses Internet nowadays and probably most of you know what IP address, netmask, gateway address mean. For example, my notebook is using this configuration right now:
IP address: 192.168.1.10
IP address: 192.168.1.12
Yes, two devices in the same local network, but that does not invalidate this post :-)
Although I have the same IP as gateway for my two devices the Linux kernel sees 192.168.1.1/eth0 and 192.168.1.1/wlan0, not 192.168.1.1 alone. When there is only one active device there is no problem, but what happens if you have two, like my notebook?
Only one of those two IP address/device pairs is the default route, that is, the IP address/device pair the Linux resorts to when it cannot find the computer to connect to. Now enters NetworkManager. NetworkManager sorts devices by type when deciding which one to configure as default route. If I am not mistaken it sorts like this: wired, wifi, mobile broadband.
Now the problem:
Suppose you are connected using wlan0 only, with Kmail, Kopete, Konversation, etc up and running. Then you decide to plug the ethernet cable (eth0) and keep wlan0 also active. NetworkManager will switch the default route from wlan0 to eth0. Now what happens with the connections Kmail, Kopete, Konversation opened when eth0 was the default route? Well, if you do not have a proper routing table configuration they will stop working because the Linux kernel, by default, does not expect receiving IP packets from the old gateway's IP address/device pair anymore, only from the new gateway's IP address/device pair.
When the connections time out (in a couple of minutes or so) the Linux kernel will close them, but not before you get annoyed by seeing your connections closing in front you even though you have two, not only one, active connections.
Long ago I created a script similar to this one to solve this problem for me. I have even forgotten that I had this configuration in my notebook. If you have this same problem just do:
$ wget http://kde-mg.org/wp-content/uploads/2011/11/per_device_routing_tables.txt $ sudo chown root:root per_device_routing_tables.txt $ sudo chmod 700 per_device_routing_tables.txt $ sudo mv per_device_routing_tables.txt \ /etc/NetworkManager/dispatcher.d/per_device_routing_tables.sh
Add one line per device to file /etc/iproute2/rt_tables like this:
100 eth0 101 wlan0
The first column contains the table's id, they just need to be different from each other. The second column contains the table's name. The script above assumes the table's name is equal to the device's interface name.
Finally, restart all your connections. When when NM switches the default route all existing connections will keep working.
What the script does is create one routing table for each device (one for eth0 and one for wlan0 in the example above). There is one default route in each routing table and the global default route (the one NM changes) still exists. Even when the global default route is changed the per device default routes continue intact and let the existing connections to reach the Internet using the correct IP address/device pair.