• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/odhcpd/src/dhcpv6-reg.c

  1 /*
  2  * SPDX-FileCopyrightText: 2026 David Härdeman <david@hardeman.nu>
  3  *
  4  * SPDX-License-Identifier: GPL2.0-only
  5  */
  6 
  7 /*
  8  * RFC9686: registering self-generated IPv6 addresses using DHCPv6.
  9  *
 10  * Clients inform the server about addresses they configured themselves
 11  * (e.g. via SLAAC) with ADDR-REG-INFORM messages, one per address, sent from
 12  * that address. The server records the address and the client's DUID in the
 13  * interface's databases and acknowledges with an ADDR-REG-REPLY.
 14  */
 15 
 16 #include <stdlib.h>
 17 #include <string.h>
 18 #include <arpa/inet.h>
 19 #include <sys/uio.h>
 20 
 21 #include "odhcpd.h"
 22 #include "dhcpv6.h"
 23 #include "dhcpv6-ia.h"
 24 
 25 struct reg_match {
 26         const struct in6_addr *addr6;
 27         bool found;
 28 };
 29 
 30 static void reg_match_cb(struct dhcpv6_lease *lease, struct in6_addr *addr6, uint8_t prefix_len,
 31                          _o_unused uint32_t pref_lt, _o_unused uint32_t valid_lt, void *arg)
 32 {
 33         struct reg_match *m = arg;
 34 
 35         if (lease->flags & OAF_DHCPV6_NA) {
 36                 if (IN6_ARE_ADDR_EQUAL(addr6, m->addr6))
 37                         m->found = true;
 38         } else if (!odhcpd_bmemcmp(addr6, m->addr6, prefix_len)) {
 39                 m->found = true;
 40         }
 41 }
 42 
 43 /* Is addr6 part of a bound lease (of the client with this DUID, if given) */
 44 static bool reg_addr6_in_lease(struct interface *iface, const struct in6_addr *addr6,
 45                               unsigned int lease_flag, const uint8_t *duid, size_t duid_len,
 46                               time_t now)
 47 {
 48         struct reg_match m = { .addr6 = addr6, .found = false };
 49         struct dhcpv6_lease *a;
 50 
 51         list_for_each_entry(a, &iface->ia_assignments, head) {
 52                 if (!(a->flags & lease_flag) || !a->bound)
 53                         continue;
 54 
 55                 if (!INFINITE_VALID(a->valid_until) && a->valid_until <= now)
 56                         continue;
 57 
 58                 if (duid && (a->duid_len != duid_len || memcmp(a->duid, duid, duid_len)))
 59                         continue;
 60 
 61                 odhcpd_enum_addr6(iface, a, now, reg_match_cb, &m);
 62                 if (m.found)
 63                         return true;
 64         }
 65 
 66         return false;
 67 }
 68 
 69 /* Is addr6 within an on-link prefix (as announced in the PIOs, deprecated or not) */
 70 static bool reg_addr6_on_link(struct interface *iface, const struct in6_addr *addr6, time_t now)
 71 {
 72         for (size_t i = 0; i < iface->addr6_len; i++) {
 73                 struct odhcpd_ipaddr *a = &iface->addr6[i];
 74 
 75                 if (a->prefix_len > 96 || a->valid_lt <= (uint32_t)now ||
 76                     ADDR_MATCH_PIO_FILTER(a, iface))
 77                         continue;
 78 
 79                 if (!odhcpd_bmemcmp(addr6, &a->addr.in6, a->prefix_len < 64 ? 64 : a->prefix_len))
 80                         return true;
 81         }
 82 
 83         return false;
 84 }
 85 
 86 /*
 87  * Remove expired registrations, and those no longer on-link or delegated,
 88  * return true if any were removed
 89  */
 90 bool dhcpv6_reg_timer(struct interface *iface, time_t now)
 91 {
 92         struct addr6_rec *rec, *tmp;
 93         bool check = iface->addr6_db_check, changed = false;
 94         char addr6buf[INET6_ADDRSTRLEN];
 95 
 96         iface->addr6_db_check = false;
 97 
 98         avl_for_each_element_safe(&iface->addr6_db, rec, avl, tmp) {
 99                 const char *why;
100 
101                 if (!INFINITE_VALID(rec->valid_until) && rec->valid_until < now)
102                         why = "expired";
103                 else if (check && !reg_addr6_on_link(iface, &rec->addr6, now) &&
104                          !reg_addr6_in_lease(iface, &rec->addr6, OAF_DHCPV6_PD,
105                                             rec->duid_rec->duid, rec->duid_rec->duid_len, now))
106                         why = "no longer appropriate";
107                 else
108                         continue;
109 
110                 info("Registration of %s on %s %s", inet_ntop(AF_INET6, &rec->addr6, addr6buf,
111                      sizeof(addr6buf)), iface->name, why);
112                 addr6_rec_del(iface, rec);
113                 changed = true;
114         }
115 
116         return changed;
117 }
118 
119 /*
120  * Get the hostname of a client DUID: from its host section, its Client FQDN
121  * option, a DHCPv4 lease with the same MAC, or else "dyn-<DUID>" (in buf)
122  */
123 const char *dhcpv6_reg_hostname(const struct duid_rec *rec, char buf[DHCPV6_REG_HOSTNAME_LEN],
124                                 time_t now)
125 {
126         struct dhcpv4_lease *lease4;
127         uint8_t mac[6];
128         size_t off;
129 
130         if (rec->lease_cfg && rec->lease_cfg->hostname)
131                 return rec->lease_cfg->hostname;
132 
133         if (rec->hostname)
134                 return rec->hostname;
135 
136         if (dhcpv6_duid_to_mac(rec->duid, rec->duid_len, mac)) {
137                 lease4 = dhcpv4_find_lease_by_hwaddr(rec->iface, mac);
138                 if (lease4 && lease4->bound && lease4->hostname && lease4->hostname_valid &&
139                     (INFINITE_VALID(lease4->valid_until) || lease4->valid_until > now))
140                         return lease4->hostname;
141         }
142 
143         /* DNS labels have at most 63 characters */
144         off = rec->duid_len > 29 ? rec->duid_len - 29 : 0;
145         memcpy(buf, "dyn-", 4);
146         odhcpd_hexlify(&buf[4], &rec->duid[off], rec->duid_len - off);
147 
148         return buf;
149 }
150 
151 static time_t reg_lifetime_to_time(uint32_t lt, time_t now)
152 {
153         /* 0 = infinite */
154         return lt == UINT32_MAX ? 0 : now + lt;
155 }
156 
157 static void reg_send_reply(struct interface *iface, struct sockaddr_in6 *dest,
158                            const struct dhcpv6_client_header *req,
159                            const uint8_t *duid, size_t duid_len,
160                            const uint8_t *ia_addr_opt, size_t ia_addr_opt_len)
161 {
162         struct dhcpv6_client_header hdr = { .msg_type = DHCPV6_MSG_ADDR_REG_REPLY };
163         struct _o_packed {
164                 uint16_t type;
165                 uint16_t len;
166                 uint8_t buf[DUID_MAX_LEN];
167         } serverid = { .type = htons(DHCPV6_OPT_SERVERID) },
168           clientid = { .type = htons(DHCPV6_OPT_CLIENTID) };
169 
170         memcpy(hdr.transaction_id, req->transaction_id, sizeof(hdr.transaction_id));
171         serverid.len = htons(dhcpv6_get_server_duid(iface, serverid.buf));
172         clientid.len = htons(duid_len);
173         memcpy(clientid.buf, duid, duid_len);
174 
175         /*
176          * RFC9686 §4.3 only requires the IA Address, but Android drops replies
177          * without its Client ID
178          */
179         struct iovec iov[] = {
180                 { &hdr, sizeof(hdr) },
181                 { &clientid, DHCPV6_OPT_HDR_SIZE + duid_len },
182                 { &serverid, DHCPV6_OPT_HDR_SIZE + ntohs(serverid.len) },
183                 { (void *)ia_addr_opt, ia_addr_opt_len },
184         };
185 
186         /* §4.3: reply to the registered address */
187         odhcpd_send(iface->dhcpv6_event.uloop.fd, dest, iov, ARRAY_SIZE(iov), iface);
188 }
189 
190 /* Handle a non-relayed ADDR-REG-INFORM message (RFC9686 §4.2) */
191 void dhcpv6_reg_handle_inform(struct interface *iface, struct sockaddr_in6 *src,
192                               const void *data, size_t len)
193 {
194         const struct dhcpv6_client_header *hdr = data;
195         const uint8_t *end = (const uint8_t *)data + len;
196         uint8_t *duid = NULL, *ia_addr_opt = NULL, *odata;
197         uint16_t otype, olen, duid_len = 0, ia_addr_len = 0;
198         size_t ia_addr_cnt = 0;
199         char hostname[256] = "";
200         char addr6buf[INET6_ADDRSTRLEN], duidbuf[DUID_HEXSTRLEN];
201         struct dhcpv6_ia_addr ia;
202         struct in6_addr addr6;
203         uint32_t preferred_lt, valid_lt;
204         struct lease_cfg *lease_cfg;
205         struct duid_rec *duid_rec;
206         struct addr6_rec *rec;
207         uint8_t mac[6];
208         time_t now = odhcpd_time();
209         bool on_link, changed = false;
210 
211         if (len < sizeof(*hdr))
212                 return;
213 
214         /* §4.2.1: check the options */
215         dhcpv6_for_each_option(&hdr[1], end, otype, olen, odata) {
216                 switch (otype) {
217                 case DHCPV6_OPT_CLIENTID:
218                         duid = odata;
219                         duid_len = olen;
220                         break;
221 
222                 case DHCPV6_OPT_SERVERID:
223                 case DHCPV6_OPT_ORO:
224                         debug("Ignoring ADDR-REG-INFORM with option %u on %s", otype, iface->name);
225                         return;
226 
227                 case DHCPV6_OPT_IA_ADDR:
228                         ia_addr_opt = odata - DHCPV6_OPT_HDR_SIZE;
229                         ia_addr_len = olen;
230                         ia_addr_cnt++;
231                         break;
232 
233                 case DHCPV6_OPT_FQDN:
234                         dhcpv6_parse_fqdn(odata, olen, hostname);
235                         break;
236 
237                 default:
238                         break;
239                 }
240         }
241 
242         if (!duid || duid_len < DUID_MIN_LEN || duid_len > DUID_MAX_LEN) {
243                 debug("Ignoring ADDR-REG-INFORM without a valid Client Identifier on %s",
244                       iface->name);
245                 return;
246         }
247 
248         if (ia_addr_cnt != 1 || ia_addr_len < sizeof(ia) - DHCPV6_OPT_HDR_SIZE) {
249                 debug("Ignoring ADDR-REG-INFORM without exactly one IA Address on %s", iface->name);
250                 return;
251         }
252 
253         memcpy(&ia, ia_addr_opt, sizeof(ia));
254         addr6 = ia.addr;
255         preferred_lt = ntohl(ia.preferred_lt);
256         valid_lt = ntohl(ia.valid_lt);
257         odhcpd_hexlify(duidbuf, duid, duid_len);
258         inet_ntop(AF_INET6, &addr6, addr6buf, sizeof(addr6buf));
259 
260         /* §4.2.1: the address must be the source address */
261         if (!IN6_ARE_ADDR_EQUAL(&addr6, &src->sin6_addr)) {
262                 debug("Ignoring ADDR-REG-INFORM for %s from another address on %s",
263                       addr6buf, iface->name);
264                 return;
265         }
266 
267         /* RFC8415 §21.6: discard addresses with preferred > valid lifetime */
268         if (preferred_lt > valid_lt) {
269                 debug("Ignoring ADDR-REG-INFORM for %s with invalid lifetimes on %s",
270                       addr6buf, iface->name);
271                 return;
272         }
273 
274         /* Ignore hosts ignored for DHCPv6 */
275         lease_cfg = config_find_lease_cfg_by_duid(duid, duid_len);
276         if (!lease_cfg && dhcpv6_duid_to_mac(duid, duid_len, mac))
277                 lease_cfg = config_find_lease_cfg_by_mac(mac);
278 
279         if (lease_cfg && lease_cfg->ignore6) {
280                 debug("Ignoring ADDR-REG-INFORM for %s from ignored host %s on %s",
281                       addr6buf, duidbuf, iface->name);
282                 return;
283         }
284 
285         /* §4.2.1: on-link, or delegated to the client */
286         on_link = reg_addr6_on_link(iface, &addr6, now);
287         if (!on_link &&
288             !reg_addr6_in_lease(iface, &addr6, OAF_DHCPV6_PD, duid, duid_len, now)) {
289                 info("Ignoring ADDR-REG-INFORM for %s from %s: not appropriate to %s",
290                      addr6buf, duidbuf, iface->name);
291                 return;
292         }
293 
294         /* §4.2.1: not assigned via DHCPv6 */
295         if (on_link && reg_addr6_in_lease(iface, &addr6, OAF_DHCPV6_NA, NULL, 0, now)) {
296                 info("Ignoring ADDR-REG-INFORM for %s from %s: assigned via DHCPv6 on %s",
297                      addr6buf, duidbuf, iface->name);
298                 return;
299         }
300 
301         rec = addr6_rec_find(iface, &addr6);
302 
303         if (valid_lt == 0) {
304                 /* §4.7: no longer used */
305                 if (rec && rec->duid_rec->duid_len == duid_len &&
306                     !memcmp(rec->duid_rec->duid, duid, duid_len)) {
307                         info("Address %s no longer registered by %s on %s",
308                              addr6buf, duidbuf, iface->name);
309                         addr6_rec_del(iface, rec);
310                         changed = true;
311                 }
312                 goto reply;
313         }
314 
315         duid_rec = duid_rec_get(iface, duid, duid_len);
316         if (!duid_rec)
317                 return;
318 
319         if (!rec) {
320                 rec = addr6_rec_add(iface, &addr6, duid_rec);
321                 if (!rec) {
322                         duid_rec_put(duid_rec);
323                         return;
324                 }
325 
326                 info("Address %s registered by %s on %s", addr6buf, duidbuf, iface->name);
327                 changed = true;
328         } else if (rec->duid_rec != duid_rec) {
329                 char olddbuf[DUID_HEXSTRLEN];
330 
331                 odhcpd_hexlify(olddbuf, rec->duid_rec->duid, rec->duid_rec->duid_len);
332                 info("Address %s registered by %s on %s, was registered by %s",
333                      addr6buf, duidbuf, iface->name, olddbuf);
334                 addr6_rec_set_duid_rec(rec, duid_rec);
335                 changed = true;
336         } else {
337                 debug("Address %s registration refreshed by %s on %s",
338                       addr6buf, duidbuf, iface->name);
339         }
340 
341         rec->preferred_until = reg_lifetime_to_time(preferred_lt, now);
342         rec->valid_until = reg_lifetime_to_time(valid_lt, now);
343 
344         if (hostname[0] && odhcpd_hostname_valid(hostname) &&
345             (!duid_rec->hostname || strcmp(duid_rec->hostname, hostname))) {
346                 char *tmp = strdup(hostname);
347 
348                 if (tmp) {
349                         free(duid_rec->hostname);
350                         duid_rec->hostname = tmp;
351                         changed = true;
352                 }
353         }
354 
355         duid_rec_put(duid_rec);
356 
357 reply:
358         if (changed)
359                 iface->update_statefile = true;
360 
361         reg_send_reply(iface, src, hdr, duid, duid_len, ia_addr_opt,
362                        DHCPV6_OPT_HDR_SIZE + ia_addr_len);
363 }
364 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt