IP address alias assigned by DHCP
This is only possible if the two DHCP clients use different MAC addresses. Which means they cannot run both on the same physical network interface, which has a single MAC address.
The solution is Linux's virtual MAC-VLAN network interfaces. MAC-VLAN interfaces are virtual network interfaces backed by a physical interface, but using with a different MAC address, which is randomly generated when you create a MAC-VLAN device.
Once you create a MAC-VLAN interface using a physical interface, you use it the same way like you would the physical one. In your particular case, you can run one DHCP client on the physical interface, and another on the MAC-VLAN device. Each device (physical and MAC-VLAN) can then have its own IP address.
A new MAC-VLAN device macvlan0
can be created from eth0
by running
ip link add dev macvlan0 link eth0 type macvlan
It can be deleted by running
ip link delete dev macvlan0
Using NCD, my network configuration software ( http://code.google.com/p/badvpn/wiki/NCD ), the following NCD program will create two MAC-VLAN devices from eth0
(macvlan0
and macvlan1
), run DHCP on each of them, and assign them the obtained IP addresses. You easily do other stuff like add routes and DNS servers; read the NCD page if you're interested.
process lan {
# Set device.
var("eth0") dev;
# Wait for device to appear, set it up, and wait for cable to be plugged in.
net.backend.waitdevice(dev);
net.up(dev);
net.backend.waitlink(dev);
# Start DHCP's.
provide("lan-link");
}
process lan_dhcp1 {
# Wait for link.
depend("lan-link") linkdep;
# Choose virtual device name.
var("macvlan0") vdev;
# Create virtual MAC-VLAN device.
list("/sbin/ip", "link", "add", "dev", vdev, "link", linkdep.dev, "type", "macvlan") do;
list("/sbin/ip", "link", "delete", "dev", vdev) undo;
run(do, undo);
# Set virtual device up.
net.up(vdev);
# DHCP configuration on virtual device.
net.ipv4.dhcp(vdev) dhcp;
ip_in_network(dhcp.addr, "127.0.0.0", "8") test_local;
ifnot(test_local);
println(vdev, ": got address ", dhcp.addr);
# Assign address to virtual device.
net.ipv4.addr(vdev, dhcp.addr, dhcp.prefix);
}
# This differs from above only in interface name (macvlan1).
process lan_dhcp2 {
# Wait for link.
depend("lan-link") linkdep;
# Choose virtual device name.
var("macvlan1") vdev;
# Create virtual MAC-VLAN device.
list("/sbin/ip", "link", "add", "dev", vdev, "link", linkdep.dev, "type", "macvlan") do;
list("/sbin/ip", "link", "delete", "dev", vdev) undo;
run(do, undo);
# Set virtual device up.
net.up(vdev);
# DHCP configuration on virtual device.
net.ipv4.dhcp(vdev) dhcp;
ip_in_network(dhcp.addr, "127.0.0.0", "8") test_local;
ifnot(test_local);
println(vdev, ": got address ", dhcp.addr);
# Assign address to virtual device.
net.ipv4.addr(vdev, dhcp.addr, dhcp.prefix);
}
You'll end up with macvlan0
and macvlan1
each with its own DHCP-obtained IP address, and eth0
which is up but does not have an IP address. Alternatively, you can create just one MAC-VLAN interface, and run one DHCP instance on eth0
itself.