Skip to content

Commit ec526a2

Browse files
committed
dhcpv6: implement RFC9686
Registering Self-Generated IPv6 Addresses Using DHCPv6 This functionality registers a client SLAAC or static address in the DHCPv6 pool, primarily for diagnostic purposes as outlined in §1. The address registration mechanism overview: ``` +--------+ +------------------+ +---------------+ | CLIENT | | FIRST-HOP ROUTER | | DHCPv6 SERVER | +--------+ +---------+--------+ +-------+-------+ | SLAAC | | |<--------------------> | | | | | | | | src: link-local address | | --------------------------------------------> | | INFORMATION-REQUEST or SOLICIT/... | | - OPTION REQUEST OPTION | | -- OPTION_ADDR_REG_ENABLE | | | | ... | | | | | |<--------------------------------------------- | | REPLY or ADVERTISE MESSAGE | | - OPTION_ADDR_REG_ENABLE | | | | | | src: address being registered | | --------------------------------------------> | | ADDR-REG-INFORM MESSAGE |Register/ | |log addresses | | | | | <-------------------------------------------- | | ADDR-REG-REPLY MESSAGE | | | ``` Signed-off-by: Paul Donald <newtwen+github@gmail.com> Link: openwrt#353
1 parent 3fda5f8 commit ec526a2

7 files changed

Lines changed: 419 additions & 5 deletions

File tree

src/dhcpv6-ia.c

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,3 +1326,277 @@ ssize_t dhcpv6_ia_handle_IAs(uint8_t *buf, size_t buflen, struct interface *ifac
13261326
out:
13271327
return response_len;
13281328
}
1329+
1330+
/* Register or update address registration in the lease database */
1331+
static bool register_ia_addr_in_lease_db(struct sockaddr_in6 *source,
1332+
const uint8_t *clientid_data, uint16_t clientid_len,
1333+
const struct dhcpv6_ia_addr *ia_addr,
1334+
struct interface *iface)
1335+
{
1336+
/* RFC9686 §4.2.1: Lease lifetime is the valid-lifetime from IA Address option */
1337+
time_t now = odhcpd_time();
1338+
uint32_t valid_lt = ntohl(ia_addr->valid_lt);
1339+
time_t lease_end = now + valid_lt;
1340+
bool onlink = false;
1341+
1342+
/* Search for existing binding with this address */
1343+
struct dhcpv6_lease *binding = NULL, *lease = NULL;
1344+
list_for_each_entry(binding, &iface->ia_assignments, head) {
1345+
/* Check if this address is already registered by another client */
1346+
if ((binding->flags & OAF_DHCPV6_ADDR_REG) &&
1347+
memcmp(&binding->peer.sin6_addr, &ia_addr->addr, sizeof(struct in6_addr)) == 0) {
1348+
/* Found address registration for this address */
1349+
if (binding->duid_len == clientid_len &&
1350+
memcmp(binding->duid, clientid_data, clientid_len) == 0) {
1351+
/* Same client, update the lease */
1352+
lease = binding;
1353+
break;
1354+
} else {
1355+
/* RFC9686 §4.2.1: Different client, should log and update binding
1356+
* We'll update to the new client */
1357+
char addrbuf[INET6_ADDRSTRLEN];
1358+
inet_ntop(AF_INET6, &ia_addr->addr, addrbuf, sizeof(addrbuf));
1359+
debug("ADDR-REG: address collision for %s: was bound to "
1360+
"different client, updating", addrbuf);
1361+
lease = binding;
1362+
break;
1363+
}
1364+
}
1365+
}
1366+
1367+
if (!lease) {
1368+
/* Create new lease for this address registration */
1369+
lease = dhcpv6_alloc_lease(clientid_len);
1370+
if (!lease) {
1371+
char addrbuf[INET6_ADDRSTRLEN];
1372+
inet_ntop(AF_INET6, &ia_addr->addr, addrbuf, sizeof(addrbuf));
1373+
error("ADDR-REG: failed to allocate lease for %s", addrbuf);
1374+
return false;
1375+
}
1376+
1377+
/* Initialize lease structure */
1378+
lease->iface = iface;
1379+
lease->peer = *source;
1380+
lease->peer.sin6_addr = ia_addr->addr; /* Store full registered address */
1381+
lease->duid_len = clientid_len;
1382+
memcpy(lease->duid, clientid_data, clientid_len);
1383+
lease->length = 128;
1384+
1385+
/* Try to find matching interface prefix and extract host ID */
1386+
for (size_t i = 0; i < iface->addr6_len; i++) {
1387+
if (!valid_addr(&iface->addr6[i], now))
1388+
continue;
1389+
1390+
if (ADDR_MATCH_PIO_FILTER(&iface->addr6[i], iface))
1391+
continue;
1392+
1393+
/* RFC9686 §4.2.1: the server SHOULD verify that the address
1394+
* is "appropriate to the link" */
1395+
if (odhcpd_bmemcmp(&ia_addr->addr, &iface->addr6[i].addr.in6,
1396+
iface->addr6[i].prefix_len) == 0) {
1397+
/* Address is within this prefix - extract host ID portion */
1398+
onlink = true;
1399+
uint64_t host_id = 0;
1400+
memcpy(&host_id, &ia_addr->addr.s6_addr[8], 8);
1401+
lease->assigned_host_id = be64toh(host_id);
1402+
break;
1403+
}
1404+
}
1405+
1406+
if (onlink) {
1407+
/* Add to interface's lease list */
1408+
list_add(&lease->head, &iface->ia_assignments);
1409+
} else {
1410+
dhcpv6_free_lease(lease);
1411+
lease = NULL;
1412+
return onlink;
1413+
}
1414+
1415+
} else {
1416+
/* Update existing lease */
1417+
lease->peer = *source;
1418+
lease->peer.sin6_addr = ia_addr->addr; /* Update registered address */
1419+
lease->duid_len = clientid_len;
1420+
memcpy(lease->duid, clientid_data, clientid_len);
1421+
}
1422+
1423+
/* Update lifetimes */
1424+
lease->valid_until = lease_end;
1425+
lease->preferred_until = now + ntohl(ia_addr->preferred_lt);
1426+
lease->bound = true;
1427+
1428+
/* Mark flags - RFC9686 Address Registration lease */
1429+
lease->flags = OAF_DHCPV6_ADDR_REG;
1430+
1431+
char addrbuf[INET6_ADDRSTRLEN];
1432+
inet_ntop(AF_INET6, &ia_addr->addr, addrbuf, sizeof(addrbuf));
1433+
debug("ADDR-REG: registered %s for client with DUID length %d, "
1434+
"valid until %ld (in %u seconds)", addrbuf, clientid_len,
1435+
lease_end, valid_lt);
1436+
1437+
return true;
1438+
}
1439+
1440+
/* Send RFC9686 ADDR-REG-REPLY message to client */
1441+
static void send_ia_addr_reg_reply(struct sockaddr_in6 *source,
1442+
const struct dhcpv6_client_header *hdr,
1443+
const uint8_t *clientid_data,
1444+
uint16_t clientid_len,
1445+
const struct dhcpv6_ia_addr *ia_addr,
1446+
struct interface *iface)
1447+
{
1448+
/* RFC9686 §4.3: Reply MUST contain:
1449+
* - msg_type: ADDR-REG-REPLY (37)
1450+
* - transaction_id: copied from ADDR-REG-INFORM
1451+
* - IA Address option: identical to the one in the request
1452+
*/
1453+
1454+
struct {
1455+
uint8_t msg_type;
1456+
uint8_t tr_id[3];
1457+
} _o_packed reply = {
1458+
.msg_type = DHCPV6_MSG_ADDR_REG_REPLY,
1459+
};
1460+
/* Copy transaction ID from request */
1461+
memcpy(reply.tr_id, hdr->transaction_id, sizeof(reply.tr_id));
1462+
1463+
struct {
1464+
uint16_t code;
1465+
uint16_t len;
1466+
uint8_t data[DUID_MAX_LEN];
1467+
} _o_packed serverid = {
1468+
.code = htons(DHCPV6_OPT_SERVERID),
1469+
.len = 0,
1470+
.data = { 0 },
1471+
};
1472+
struct {
1473+
uint16_t code;
1474+
uint16_t len;
1475+
uint8_t data[DUID_MAX_LEN];
1476+
} _o_packed clientid = {
1477+
.code = htons(DHCPV6_OPT_CLIENTID),
1478+
.len = htons(clientid_len),
1479+
.data = { 0 },
1480+
};
1481+
memcpy(clientid.data, clientid_data, clientid_len);
1482+
1483+
if (config.default_duid_len > 0) {
1484+
memcpy(serverid.data, config.default_duid, config.default_duid_len);
1485+
serverid.len = htons(config.default_duid_len);
1486+
} else {
1487+
uint16_t duid_ll_hdr[] = {
1488+
htons(DUID_TYPE_LL),
1489+
htons(ARPHRD_ETHER)
1490+
};
1491+
memcpy(serverid.data, duid_ll_hdr, sizeof(duid_ll_hdr));
1492+
odhcpd_get_mac(iface, &serverid.data[sizeof(duid_ll_hdr)]);
1493+
serverid.len = htons(sizeof(duid_ll_hdr) + ETH_ALEN);
1494+
}
1495+
1496+
struct iovec iov[] = {
1497+
{ &reply, sizeof(reply) },
1498+
{ &serverid, sizeof(serverid) },
1499+
{ &clientid, sizeof(clientid) },
1500+
{ (void *)ia_addr, sizeof(struct dhcpv6_ia_addr) },
1501+
};
1502+
1503+
size_t serverid_len, clientid_opt_len;
1504+
1505+
serverid_len = sizeof(serverid.code) + sizeof(serverid.len) + ntohs(serverid.len);
1506+
clientid_opt_len = sizeof(clientid.code) + sizeof(clientid.len) + clientid_len;
1507+
1508+
iov[1].iov_len = serverid_len;
1509+
iov[2].iov_len = clientid_opt_len;
1510+
1511+
/* RFC9686 §4.3: If not relayed, destination is the address being registered.
1512+
* If relayed, we would construct Relay-reply (handled separately in relay_server_response).
1513+
* For direct replies, source is already the registered address. */
1514+
1515+
char addrbuf[INET6_ADDRSTRLEN];
1516+
inet_ntop(AF_INET6, &ia_addr->addr, addrbuf, sizeof(addrbuf));
1517+
debug("Sending ADDR-REG-REPLY for %s on %s", addrbuf, iface->name);
1518+
1519+
odhcpd_send(iface->dhcpv6_event.uloop.fd, source, iov, ARRAY_SIZE(iov), iface);
1520+
}
1521+
1522+
/* RFC9686 Address Registration Message Handler */
1523+
void handle_ia_addr_reg_inform(struct sockaddr_in6 *source,
1524+
const void *data, size_t len, struct interface *iface)
1525+
{
1526+
const struct dhcpv6_client_header *hdr = data;
1527+
uint8_t *opts = (uint8_t *)&hdr[1], *opts_end = (uint8_t *)data + len;
1528+
uint16_t otype, olen;
1529+
uint8_t *odata;
1530+
1531+
uint8_t *clientid_data = NULL;
1532+
uint16_t clientid_len = 0;
1533+
struct dhcpv6_ia_addr *ia_addr = NULL;
1534+
1535+
if (len < sizeof(*hdr)) {
1536+
debug("ADDR-REG-INFORM: message too short");
1537+
return;
1538+
}
1539+
1540+
/* RFC9686 §4.2: Client MUST include Client Identifier option */
1541+
/* RFC9686 §4.2: ADDR-REG-INFORM MUST NOT contain Server Identifier */
1542+
/* RFC9686 §4.2: MUST contain exactly one IA Address option */
1543+
1544+
dhcpv6_for_each_option(opts, opts_end, otype, olen, odata) {
1545+
switch (otype) {
1546+
case DHCPV6_OPT_CLIENTID:
1547+
if (olen > 0) {
1548+
clientid_data = odata;
1549+
clientid_len = olen;
1550+
}
1551+
break;
1552+
case DHCPV6_OPT_SERVERID:
1553+
/* RFC9686 §4.2: Server MUST discard messages with Server ID */
1554+
debug("ADDR-REG-INFORM: message contains Server Identifier, discarding");
1555+
return;
1556+
case DHCPV6_OPT_IA_ADDR:
1557+
if (olen == sizeof(struct dhcpv6_ia_addr) - 4) {
1558+
if (ia_addr != NULL) {
1559+
debug("ADDR-REG-INFORM: message contains more than one IA_ADDR, discarding");
1560+
return;
1561+
}
1562+
ia_addr = (struct dhcpv6_ia_addr *)&odata[-4];
1563+
}
1564+
break;
1565+
case DHCPV6_OPT_ORO:
1566+
/* RFC9686 §4.2: ADDR-REG-INFORM MUST NOT contain Option Request option */
1567+
debug("ADDR-REG-INFORM: message contains Option Request, discarding");
1568+
return;
1569+
default:
1570+
break;
1571+
}
1572+
}
1573+
1574+
/* Validate message contents */
1575+
if (!clientid_data || clientid_len == 0) {
1576+
debug("ADDR-REG-INFORM: missing Client Identifier option, discarding");
1577+
return;
1578+
}
1579+
1580+
if (!ia_addr) {
1581+
debug("ADDR-REG-INFORM: missing or invalid IA Address option, discarding");
1582+
return;
1583+
}
1584+
1585+
/* RFC9686 §4.2.1: Verify source address matches the IA Address option
1586+
* The message MUST be sent from the address being registered */
1587+
if (memcmp(&source->sin6_addr, &ia_addr->addr, sizeof(struct in6_addr)) != 0) {
1588+
debug("ADDR-REG-INFORM: source address does not match IA Address option");
1589+
return;
1590+
}
1591+
1592+
char addrbuf[INET6_ADDRSTRLEN];
1593+
inet_ntop(AF_INET6, &ia_addr->addr, addrbuf, sizeof(addrbuf));
1594+
debug("Got ADDR-REG-INFORM for %s on %s", addrbuf, iface->name);
1595+
1596+
/* Register address in lease database */
1597+
if(!register_ia_addr_in_lease_db(source, clientid_data, clientid_len, ia_addr, iface))
1598+
return;
1599+
1600+
/* Send ADDR-REG-REPLY response */
1601+
send_ia_addr_reg_reply(source, hdr, clientid_data, clientid_len, ia_addr, iface);
1602+
}

0 commit comments

Comments
 (0)