Getting 2 IP addresses on one network card, using DHCP

7

7

Given: A computer running Linux (CentOS 5.x), one network card connected to LAN switch, a second one to cable modem via crossover cable. This computer acts as a router/firewall/traffic shaper for the local network.

Computer gets its public IP address from an ISP via DHCP. I know my ISP can supply 2 IP addresses to clients (when cable modem is connected directly to a hub, etc.). I would like to get 2 IP addresses on one computer that is connected to the ISP. It will simplify traffic segregation, NAT and shaping.

I know I can probably "fake" DHCP requests and bang together some scripts that will do this, but I wonder if somebody knows an easier and proper way.

Question: How can I make a Linux computer request two IP addresses via DHCP and assign both of them to one network card?

haimg

Posted 2011-10-09T21:02:50.913

Reputation: 19 503

Answers

7

As a DHCP reply is based on the MAC address of the requesting interface, with a single physical interface, the answer is "you can't". The only way to do this would be with a script.

Perhaps something like (with a subinterface defined on the primary):

  1. Primary interface issues DHCP and gets IP address
  2. macchanger changes MAC address of interface
  3. Sub interface issues DHCP and gets IP address
  4. Revert mac address with macchanger

Kill the dhcp client, so that it doesn't automatically run later. Work out the lease time of the IP address you are given, and schedule this script to run again before the lease expires.

Update

For this you will need iproute2 installed. The following command adds a virtual interface bound to an existing eth0 interface:

ip link add link eth0 address 00:11:22:33:44:55 virtual0 type macvlan

Replace the mac and "virtual0" name of the interface to whatever you like. Turn it on:

ip link set virtual0 up

Then configure using dhcpd or dhclient or ifconfig as needed. I have tested this on Debian squeeze - your distro may not have everything needed enabled in the kernel (macvlan particularly).

Paul

Posted 2011-10-09T21:02:50.913

Reputation: 52 173

2re: DHCP reply is based on the MAC address - Actually it is BOOTP (the predecessor of DHCP) that is strictly based on the MAC. DHCP has a Client ID option field that is supposed to be used instead. If the Client ID is not present, then DHCP uses the MAC by itself. So, in theory one could craft DHCP requests with the same MAC but different Client ID to get 2 IP from the same DHCP server. -- I have not actually tried this, just reading the DHCP spec. – Jesse Chisholm – 2015-04-09T19:41:54.227

@JesseChisholm: yes, it work to use the Client id. I can get two IP address using DHCP for my eth0. Example for OpenWRT: # udhcpc -i eth0:1 -x 0x3d:0100BEEFC0FFEE (-x 0x3d:0100BEEFC0FFEE - option 61 (client id)) – Peter Senna – 2016-06-03T21:41:38.667

I can confirm that you can get two addresses, one with MAC address and one with Client ID. Too bad that is not what I wanted ... – Brenda J. Butler – 2019-07-25T01:37:58.570

Thanks for your answer. Nothing prevents me putting an interface into promiscuous mode and sending a DHCP request with "fake" MAC address. I'll get a reply back because interface is in promiscuous mode, and assign an IP it to a subinterface. But it is fragile and a lot of work, I was hoping for something more robust and simple and standard (subinterface with its own MAC? "Virtual" network card?) – haimg – 2011-10-09T21:31:24.013

1Yes, I hve added a possible solution to my original answer – Paul – 2011-10-10T23:56:13.167

Thanks! This is what I've looking for. Although it is not working on CentOS 5.5, it should work on CentOS 6.x (time to upgrade). – haimg – 2011-10-11T00:27:06.327

6

As @JesseChisholm suggested, it is way easier to ask the right thing to the DHCP server instead of making complex network setups.

For OpenWRT I could simply run:

 # udhcpc -i eth0:1 -x 0x3d:0100BEEFC0FFEE

Which resulted in having two ip addresses from the same dhcp server.

The 0x36 is option 61 which is the client id option. After the : there is an hex byte option. This came from the help option of udhcpc:

 # udhcp --help
 BusyBox v1.22.1 (2014-10-08 16:34:50 HKT) multi-call binary.

 Usage: udhcpc [-fbqRB] [-t N] [-T SEC] [-A SEC/-n]
 [-i IFACE] [-s PROG] [-p PIDFILE]
 [-oC] [-r IP] [-V VENDOR] [-F NAME] [-x OPT:VAL]... [-O OPT]...

 ...
 -x OPT:VAL     Include option OPT in sent packets (cumulative)
                Examples of string, numeric, and hex byte opts:
                -x hostname:bbox - option 12
                -x lease:3600 - option 51 (lease time)
                -x 0x3d:0100BEEFC0FFEE - option 61 (client id)

Peter Senna

Posted 2011-10-09T21:02:50.913

Reputation: 161