Squeeze: Virtualbox – routing on vboxnet0

I Installed Virtualbox recently on my Thinkpad to setup 3 Linux guests for a lab environment.
The guests are configured with the Host-Only adapter. The Host-Only adapter adds a virtual network interface vboxnet0 to the host system (default ip: 192.168.56.1) and provides dhcp for the guests.
NAT mode was no option since I need to access the guests from the host network. Bridge-Mode was also no option since my thinkpad is configured for dhcp (office and home use). In bridge mode my guests would get a different IP depending on if I’m at work or at home. I’d have to reconfigure my external tools everytime I switch networks or go for static IPs which would led to routing problems.
Host-network seemed to be the most viable option here.

While the guest can communicate through the host-only adapter there’s the problem that they have no internet access because vboxnet0 is not routed.
To get this working, enable ip_forwarding and proxy-arp.

echo 1 > /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/conf/all/proxy_arp

Now add add default route in your guests pointing to the vboxnet0 iface:

route add default gw 192.168.56.1

And finally enable NAT on the host:

iptables -A POSTROUTING -t nat -s 192.168.56.0/24 -j MASQUERADE

You now have a working DHCP guest network even if your not connected to the LAN and you additionally have internet when your connected to the lan.