Detecting ethernet gadget connections

Daniel Benoy daniel at
Fri Mar 6 16:11:33 CET 2009

> Is this necessary?
> Just set a higher metric on usb0, then routes thorough eth0 will be 
> preferred when available. The default route through usb0 will only be 
> used as a last resort when there is no other way.

Yes.  Packets will route out that interface whether it's connected or not because it never disappears even when the cable is completely unplugged, and I want (obviously) the usb0 interface to be a higher priority than my gprs interface, so every packet would go out usb0 if I set it to a preferable metric.

Unless I brought the interface up and down manually when the cable is disconnected, which brings us back to the original problem.

Also, I'm setting up routing based on firewall rules, so it complicates things.  When I'm writing my firewall script it needs to know whether usb is up or not to decide where web and ssh and instant messaging should be routed.

> Helge Hafting
> _______________________________________________
> Openmoko community mailing list
> community at

I eventually did come up with a solution to this.  I check the charger state.  If it detects a host it should be charging at 500mA.  This doesn't prove that the network interface is successfully configured, but it's good enough for my purposes.

If anyone's interested in seeing the convoluted script I had to make, I've attached it to this e-mail.

Here's basically what it does for me:
1) Automatically brings up and down network interfaces.  (Only for usb0 now.  I took out eth0 wifi scanning because I found a way to do it outside of the script.  And ppp0 I'm still handling manually.. may change that in the future)
2) Creates a routing table for each device with a default route out that device automatically by detecting changes to the main table made by ifup and other connection software.
3) Creates iptables rules automatically based on which interfaces are up and which are down, in accordance with my preferences.  (xmpp and ssh always go out GPRS, because I want the connections to be unbroken when I roam away from my usb or wifi connection, and they're low bandwidth)

The effect is that web browsing and such will switch over seamlessly when I come within range of my home network, while my instant messager never has its connection broken so long as it's in cell phone range.

If you want to try the script as well you'll need some firewall rules, such as:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 
iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE 
iptables -t nat -A POSTROUTING -o ppp+ -j MASQUERADE 
iptables -t mangle -A OUTPUT -j CUSTOMROUTE

Someone let me know if there's a better way to achieve this.  I thought about using the ROUTE iptables target, but it seemed pretty complicated to get installed.

One problem I've encountered is that when software determines its source address automatically (There's rarely an option to set it by hand, and I wouldn't want to be doing that anyway) then it will examine the routing table for its source address *without sending a packet* so therefore firewall never marks the packet, and the routing table thinks its supposed to use the main rule chain.  My solution for this was the MASQUERADE lines above.

Daniel Benoy
-------------- next part --------------
A non-text attachment was scrubbed...
Type: application/x-perl
Size: 15031 bytes
Desc: not available
Url : 

More information about the community mailing list