With the iPhone tethered via USB to a Windows XP virtual machine, it was easy to make the host (Ubuntu) use the Windows XP virtual machine as an internet gateway.

In Ubuntu / the host:

We want to create a virtual network interface to establish a LAN with the Windows XP virtual machine, without touching any of our other interfaces at all.

sudo chmod 0666 /dev/net/tun
sudo tunctl -t tap0 -u USERNAME

Now in Virtual Box, we set the Windows XP virtual machine to use our new TAP interface;

Open Virtual Box > Settings of the Windows XP VM > Network;

  • Attached to: Host interface
  • Host Interfaces: tap0

Now boot the Windows XP virtual machine.

In the Windows XP virtual machine:

  1. Establish a connection with a USB tethered iPhone using PdaNet.
  2. Enable ICS on the 'PdaNet Modem' connection - Right-click 'PdaNet Modem' connection (in system tray) > Status > Properties > Advanced - Tick "Allow other network users to connect through... blah blah blah".
  3. Let Windows set the LAN interface IP to 192.168.0.1 - not that you have any choice hahaha.

Back in Ubuntu / the host:

We can make a DHCP request from our TAP interface to the Windows XP “gateway”:

sudo dhclient tap0

And now the tap0 interface should have an IP from the gateway, which you can check:

sudo ifconfig tap0

And a route should have been created automagically by dhclient:

sudo route -n

ie; 0.0.0.0   192.168.0.1   0.0.0.0  UG  0  0  0  tap0

 

Now you’ve essentially got a iPhone-USB-tethered Ubuntu :-D

But, the really cool thing I recon; other virtual machines using NAT networking can access the interwebs too! Without any trickery at all.

For practicality you’d want to wrap the TAP into a nice script, or assign a fixed IP address to the TAP interface & create the gateway route.

Ooh yeah :-) I do suspect a performance hit though…

UPDATE - Wrapped in a nice script

Firstly, I have not noticed any significant performance hit on any machine in the virtual “network”, internet transfer speeds are pretty much the same, whether on the virtual machine, the host, or another virtual machine using NAT. Everything seems to come down to the reliability of the iPhone + PdaNet app.

I just made 2 very simple scripts; gateway_up and gateway_down, launched from nice buttons on my top gnome panel.

gateway_up

#!/bin/bash
# gateway_up
# starts gateway VM and gets an IP

# create a TAP0 interface for gateway virtual machine to use
sudo /bin/chmod 0666 /dev/net/tun
sudo /usr/sbin/tunctl -t tap0 -u james

# start VM
/usr/bin/VBoxManage startvm JlGatewayWinXp

# wait...
/bin/sleep 4

# read iphone's address
IPHONE_ADDRESS=`VBoxManage list usbhost | grep 'iPhone' -A 2 | grep 'Address' | awk '{ print $2}'`

# re-attach iPhone USB device
/usr/bin/VBoxManage controlvm JlGatewayWinXp usbdetach $IPHONE_ADDRESS
/usr/bin/VBoxManage controlvm JlGatewayWinXp usbattach $IPHONE_ADDRESS

# get an IP
sudo /sbin/dhclient tap0

gateway_down

#!/bin/bash
# gateway_down
# stops gateway VM

# read iphone's address
IPHONE_ADDRESS=`VBoxManage list usbhost | grep 'iPhone' -A 2 | grep 'Address' | awk '{ print $2}'`

# detach iPhone USB device
/usr/bin/VBoxManage controlvm JlGatewayWinXp usbdetach $IPHONE_ADDRESS

# stop VM
/usr/bin/VBoxManage controlvm JlGatewayWinXp savestate

# stop TAP interface
sudo /sbin/ifconfig tap0 down

Not much to ‘em really. Sure we could put them into a single “gateway” script and not duplicate the IPHONE_ADDRESS grep, but I’m not sure if the ‘detach iPhone’ step on gateway_down is really needed.

Once the Virtual Machine has started (hopefuly restored from a saved state so it’s quick) and iPhone has paired, connect using the PdaNet Desktop Client (double-click it’s tray icon or ‘connect’ on the menu).

Sometimes things get messed up, requiring the iPhone to be manually detached/attached to the Virtual Machine.

I also get an mount error (twice) from Ubuntu if the iPhone is mounted as a camera when it attaches to the Virtual Machine. This would probably be solved by unmounting the iPhone in the script, but it’s not a big deal.