Gateway on a virtual network interface used by LXC guests
- by linkdd
I'm currently having some problems with configuring a gateway for a virtual network interface.
Here is what I've done :
I created a virtual network interface :
# brctl addbr lxc0
# brctl setfd lxc0 0
# ifconfig lxc0 192.168.0.1 promisc up
# route add -net default gw 192.168.0.1 lxc0
The output of ifconfig gave me what I wanted :
lxc0 Link encap:Ethernet HWaddr 22:4f:e4:40:89:bb
inet adr:192.168.0.1 Bcast:192.168.0.255 Masque:255.255.255.0
adr inet6: fe80::88cf:d4ff:fe47:3b6b/64 Scope:Lien
UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1
RX packets:623 errors:0 dropped:0 overruns:0 frame:0
TX packets:7412 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 lg file transmission:0
RX bytes:50329 (49.1 KiB) TX bytes:335738 (327.8 KiB)
I configured dnsmasq to provide a DNS server (using the default : 192.168.1.1) and a DHCP server.
Then, my LXC guest is configured like this :
lxc.network.type=veth
lxc.network.link=lxc0
lxc.network.flags=up
Every thing is working perfectly, my containers have an IP (192.168.0.57 and 192.168.0.98).
I can ping the host and the containers from the containers and from the host :
(host)# ping -c 3 192.168.0.114
PING 192.168.0.114 (192.168.0.114) 56(84) bytes of data.
64 bytes from 192.168.0.114: icmp_req=1 ttl=64 time=0.044 ms
64 bytes from 192.168.0.114: icmp_req=2 ttl=64 time=0.038 ms
64 bytes from 192.168.0.114: icmp_req=3 ttl=64 time=0.043 ms
--- 192.168.0.114 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.038/0.041/0.044/0.007 ms
(guest)# ping -c 3 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_req=1 ttl=64 time=0.048 ms
64 bytes from 192.168.0.1: icmp_req=2 ttl=64 time=0.042 ms
64 bytes from 192.168.0.1: icmp_req=3 ttl=64 time=0.042 ms
--- 192.168.0.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.042/0.044/0.048/0.003 ms
Now, it's time to configure the host as a gateway for the network 192.168.0.0/24 :
#!/bin/sh
# Clear rules
iptables -F
iptables -t nat -F
iptables -t mangle -F
iptables -X
iptables -A FORWARD -i lxc0 -o eth0 -j ACCEPT
iptables -A FORWARD -i eth0 -o lxc0 -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward
The final test failed completely, ping the outside :
(guest)# ping -c 3 google.fr
PING google.fr (173.194.67.94) 56(84) bytes of data.
From 192.168.0.1: icmp_seq=3 Redirect Host(New nexthop: wi-in-f94.1e100.net (173.194.67.94))
From 192.168.0.1 icmp_seq=1 Destination Host Unreachable
From 192.168.0.1 icmp_seq=2 Destination Host Unreachable
From 192.168.0.1 icmp_seq=3 Destination Host Unreachable
--- google.fr ping statistics ---
3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2017ms
Did I missed something ?