How do routers assign IP addresses?
How does a router assign an IP address? Is this random or is there a set method?
It seems that the same device – regardless of the time it was connected – seems to get the same IP assigned. Does the router know the MAC address and assign the same IP or is there something else going on?
IPv4 addresses are usually assigned using the DHCP protocol. How this happens, depends on the particular DHCP server running on the router...
-
With DHCP, addresses are leased for a certain time period, so if a device reboots requests an address before the old lease expires, the router usually gives the same old address (based on DHCP client ID, or on the MAC address).
-
Some DHCP servers remember which address was issued even for a while after the lease expires, so they always give the same address to the same device.
-
If the device wasn't seen previously, it depends on the implementation – usually the new address is chosen randomly, but sometimes sequentially, and sometimes based on some sort of a hash on the MAC address so that the router at least tries to give the old address again.
For example, to quote the manual page dhcpd.conf(5)
of the ISC DHCP server dhcpd:
The DHCP server generates the list of available IP addresses from a hash table. This means that the addresses are not sorted in any par‐ ticular order, and so it is not possible to predict the order in which the DHCP server will allocate IP addresses. Users of previous versions of the ISC DHCP server may have become accustomed to the DHCP server allocating IP addresses in ascending order, but this is no longer pos‐ sible, and there is no way to configure this behavior with version 3 of the ISC DHCP server.
For IPv6, there is a similar protocol DHCPv6, to which the answer above still applies.
However, many networks use a simpler "stateless autoconfiguration" protocol in which the router only broadcasts the prefix (aka network address), and devices assign their own IP addresses. The assignment is done in two ways:
-
With normal "Stateless Autoconfiguration" (RFC 4862), the 'host' part of IP addresses is based on the device's hardware address. For 48-bit MAC addresses, the second bit is flipped, and
ff:fe
is inserted in the middle (to pad to 64 bits)For example, prefix
2001:470:1f0b:915::/64
plus MAC address48:5d:60:e8:65:8f
results in IP address2001:470:1f0b:915:4a5d:60ff:fee8:658f
. -
With "Privacy Extensions" (RFC 4941), the 'host' part is chosen randomly – and a new address is added every 10 hours, too.
Note that "Privacy Extensions" are almost always used in addition to the normal "stateless" MAC-based IP address.
I think it is important to point out that routers do not assign IP addresses: routeurs receive an IP packet on an interface and send it almost unchanged on another interface (the changes I can think of is decreasing the TTL and updating the checksum of the IP packet; even changing the IP address in the packet (NAT) is beyond the task of a router).
The devices that are called home routers are much more than routers, they also include some of the following functionalitys/services:
- DHCP Server (this is the service that assigns IP addresses),
- ADSL Modem (to negotiate a connection to the ISP),
- Firewall (to filter incoming and outgoing connections),
- Inbound and outbound NAT device (to share the single IPv4 address allocated by the ISP for all the devices on the home network),
- Router,
- Switch,
- WiFi Antenna,
- Web Server,
- Console (web, ssh or telnet) to configure the device,
- Others...
(Note: firewalls can usually do Inbound and outbound NAT and routing, even though those 4 functions can be done using dedicated devices.)
In large Enterprise deployment, the DHCP Server is often hosted on dedicated Linux or Windows servers (usually a cluster) that does not have anything to do with routers.