Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have multiple network adapters, and need to be able to specify which adapter my socket uses for outbound connections, so that I can have one or more sockets bound to each network. I`ve looked at the other answers to this questions but can't seem to make it work.

Binding the socket to the IP address of the adapter I want to connect via does not work. SO_BINDTODEVICE does not appear to work either, have tried passing either the adapter name, or the adapter index, the call succeeds but the socket can't connect. I can't find a complete example that uses SO_BINDTODEVICE to clarify the correct usage.

The symptoms using the above methods are that the connection is received by the server, but the connect() fails on the client with error code 2 (?). I read that ip route is needed to ensure that reply packets are sent via the same adapter they were received, but can not seem to get the ip route commands right.

The tables Teth0 and Twlan0 have been added.  

For eth0, the IP address is 192.168.1.23, the gateway is 192.168.1.1
For wlan0, the IP address is 192.168.2.100, the gateway is 192.168.2.1

ip route add 192.168.1.1/32 dev eth0 src 192.168.1.23 table Teth0
ip route add default via 192.168.1.1 table Teth0
ip rule add from 192.168.1.23 table Teth0

ip route add 192.168.2.1/32 dev wlan0 src 192.168.2.100 table Twlan0
ip route add default via 192.168.2.1 table Twlan0
ip rule add from 192.168.2.100 table Twlan0

ip route flush cache

Im usingOpen Embedded Linux 2.6.36 on a GumStix Overo (Omap3)`.

Any tips would be greatly appreciated as I`m going nuts here ;)

share|improve this question

migrated from stackoverflow.com Mar 24 '11 at 0:53

This question came from our site for professional and enthusiast programmers.

    
This is an iproute2 question - you need to ip rule add in order to select the correct routing table based on the local ip. –  Erik Mar 21 '11 at 17:48
    
I`m doing ip rule add for each network as shown above, am I doing it wrong? Thanks! –  Paul Nolan Mar 21 '11 at 17:53
    
@Paul Nolan: You're supposed to route add for the net on each interface - looks like you're adding routes for your own IP only. See lartc.org/howto/lartc.rpdb.multiple-links.html –  Erik Mar 21 '11 at 17:59
    
Do you mean it should be: ip rule add from 192.168.2.100/32 table Twlan0 ? –  Paul Nolan Mar 21 '11 at 18:12
    
Some progress, I used the script at the link you posted, and it now connects via both adapters, hurray, thank you! However, after sending 2.1MiB via wlan0, wlan0 disassociates from the access point and will not rejoin, argh. Tested 3 times, it cuts out at 2.1MiB each time. Is some kind of traffic wlan0 needs now being blocked perhaps? Or is this a completely unrelated problem. –  Paul Nolan Mar 21 '11 at 21:23

1 Answer 1

You're trying to define two default routes to the outside world. You should only ever have one default gateway/route.

Odds are this is what is happening:

  • you're using the wireless adapter as your indicated outbound connection/source ip address
  • the kernel checks if there is a route from wlan0 to your destination
  • Since the wireless device has a default gateway/route there is a viable route, and the 3way handshake starts from wlan0
  • After some traffic (apparently 2mb into your transfer) the kernel looks at the connection and decides that although this traffic has been trans-versing the wireless network, the first (and only relevant) default gateway/route in the routing table is via eth0
  • the kernel starts sending via eth0
  • The far side starts receiving traffic from eth0 with either the wrong source IP address or does not know how to return data for the wlan0 IP via the eth0 network and disregards it
  • There are some moderate quality tools for linux to take care of this for you, but this will not work with static IP address, and most definitely not with a bad routing table.

    share|improve this answer

    Your Answer

     
    discard

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