2 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
31 #include <net/ethernet.h>
36 #define ALL_DHCPV6_RELAYS {{{0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
37 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02}}}
38 #define DHCPV6_CLIENT_PORT 546
39 #define DHCPV6_SERVER_PORT 547
40 #define DHCPV6_DUID_LLADDR 3
41 #define DHCPV6_REQ_DELAY 1
44 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
45 const uint8_t transaction[3], enum dhcpv6_msg type);
47 static time_t dhcpv6_parse_ia(void *opt, void *end);
49 static reply_handler dhcpv6_handle_reply;
50 static reply_handler dhcpv6_handle_advert;
51 static reply_handler dhcpv6_handle_rebind_reply;
52 static reply_handler dhcpv6_handle_reconfigure;
53 static int dhcpv6_commit_advert(uint32_t elapsed);
57 // RFC 3315 - 5.5 Timeout and Delay values
58 static struct dhcpv6_retx dhcpv6_retx[_DHCPV6_MSG_MAX] = {
59 [DHCPV6_MSG_UNKNOWN] = {false, 1, 120, "<POLL>",
60 dhcpv6_handle_reconfigure, NULL},
61 [DHCPV6_MSG_SOLICIT] = {true, 1, 120, "SOLICIT",
62 dhcpv6_handle_advert, dhcpv6_commit_advert},
63 [DHCPV6_MSG_REQUEST] = {true, 30, 10, "REQUEST",
64 dhcpv6_handle_reply, NULL},
65 [DHCPV6_MSG_RENEW] = {false, 10, 600, "RENEW",
66 dhcpv6_handle_reply, NULL},
67 [DHCPV6_MSG_REBIND] = {false, 10, 600, "REBIND",
68 dhcpv6_handle_rebind_reply, NULL},
69 [DHCPV6_MSG_RELEASE] = {false, 1, 600, "RELEASE", NULL, NULL},
70 [DHCPV6_MSG_DECLINE] = {false, 1, 3, "DECLINE", NULL, NULL},
71 [DHCPV6_MSG_INFO_REQ] = {true, 1, 120, "INFOREQ",
72 dhcpv6_handle_reply, NULL},
78 static int urandom_fd = -1;
79 static int ifindex = -1;
80 static time_t t1 = 0, t2 = 0, t3 = 0;
83 static int request_prefix = -1;
84 static enum odhcp6c_ia_mode na_mode = IA_MODE_NONE;
85 static bool accept_reconfig = false;
89 int init_dhcpv6(const char *ifname, int request_pd)
91 request_prefix = request_pd;
92 urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY);
96 sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
100 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
101 if (ioctl(sock, SIOCGIFINDEX, &ifr))
103 ifindex = ifr.ifr_ifindex;
105 // Create client DUID
106 size_t client_id_len;
107 odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
108 if (client_id_len == 0) {
109 ioctl(sock, SIOCGIFHWADDR, &ifr);
110 uint8_t duid[14] = {0, DHCPV6_OPT_CLIENTID, 0, 10, 0,
111 DHCPV6_DUID_LLADDR, 0, 1};
112 memcpy(&duid[8], ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
113 odhcp6c_add_state(STATE_CLIENT_ID, duid, sizeof(duid));
117 uint16_t oro[] = {htons(DHCPV6_OPT_DNS_SERVERS),
118 htons(DHCPV6_OPT_DNS_DOMAIN),
119 htons(DHCPV6_OPT_NTP_SERVER),
120 htons(DHCPV6_OPT_SIP_SERVER_A),
121 htons(DHCPV6_OPT_SIP_SERVER_D)};
122 odhcp6c_add_state(STATE_ORO, oro, sizeof(oro));
125 // Configure IPv6-options
127 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
128 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
129 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
131 struct sockaddr_in6 client_addr = { .sin6_family = AF_INET6,
132 .sin6_port = htons(DHCPV6_CLIENT_PORT), .sin6_flowinfo = 0 };
133 if (bind(sock, (struct sockaddr*)&client_addr, sizeof(client_addr)))
140 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode)
146 void dhcpv6_remove_addrs(void)
149 uint8_t *odata, *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
150 uint16_t otype, olen;
151 dhcpv6_for_each_option(ia_na, ia_na + ia_na_len, otype, olen, odata) {
152 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
153 set_rtnetlink_addr(ifindex, &addr->addr, 0, 0);
158 static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
162 gethostname(fqdn_buf, sizeof(fqdn_buf));
169 size_t fqdn_len = 5 + dn_comp(fqdn_buf, fqdn.data,
170 sizeof(fqdn.data), NULL, NULL);
171 fqdn.type = htons(DHCPV6_OPT_FQDN);
172 fqdn.len = htons(fqdn_len - 4);
178 void *cl_id = odhcp6c_get_state(STATE_CLIENT_ID, &cl_id_len);
182 void *srv_id = odhcp6c_get_state(STATE_SERVER_ID, &srv_id_len);
186 void *ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
187 struct dhcpv6_ia_hdr hdr_ia_pd = {
188 htons(DHCPV6_OPT_IA_PD),
189 htons(sizeof(hdr_ia_pd) - 4 + ia_pd_len),
193 struct dhcpv6_ia_prefix pref = {
194 .type = htons(DHCPV6_OPT_IA_PREFIX),
195 .len = htons(25), .prefix = request_prefix
198 if (ia_pd_len == 0 && request_prefix > 0 &&
199 (type == DHCPV6_MSG_SOLICIT ||
200 type == DHCPV6_MSG_REQUEST)) {
202 ia_pd_len = sizeof(pref);
207 void *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
208 struct dhcpv6_ia_hdr hdr_ia_na = {
209 htons(DHCPV6_OPT_IA_NA),
210 htons(sizeof(hdr_ia_na) - 4 + ia_na_len),
214 // Reconfigure Accept
218 } reconf_accept = {htons(DHCPV6_OPT_RECONF_ACCEPT), 0};
220 // Request Information Refresh
221 uint16_t oro_refresh = htons(DHCPV6_OPT_INFO_REFRESH);
225 void *oro = odhcp6c_get_state(STATE_ORO, &oro_len);
229 uint16_t elapsed_type;
230 uint16_t elapsed_len;
231 uint16_t elapsed_value;
235 type, {trid[0], trid[1], trid[2]},
236 htons(DHCPV6_OPT_ELAPSED), htons(2),
237 htons((ecs > 0xffff) ? 0xffff : ecs),
238 htons(DHCPV6_OPT_ORO), htons(oro_len),
241 struct iovec iov[] = {
246 {srv_id, srv_id_len},
249 {&hdr_ia_na, sizeof(hdr_ia_na)},
251 {&hdr_ia_pd, sizeof(hdr_ia_pd)},
255 size_t cnt = ARRAY_SIZE(iov);
256 if (type == DHCPV6_MSG_INFO_REQ) {
258 iov[2].iov_len = sizeof(oro_refresh);
259 hdr.oro_len = htons(oro_len + sizeof(oro_refresh));
260 } else if (!request_prefix) {
264 // Disable IAs if not used
265 if (type == DHCPV6_MSG_SOLICIT) {
266 iov[5].iov_len = sizeof(reconf_accept);
267 } else if (type != DHCPV6_MSG_REQUEST) {
274 if (na_mode == IA_MODE_NONE)
277 struct sockaddr_in6 srv = {AF_INET6, htons(DHCPV6_SERVER_PORT),
278 0, ALL_DHCPV6_RELAYS, ifindex};
279 struct msghdr msg = {&srv, sizeof(srv), iov, cnt, NULL, 0, 0};
281 sendmsg(sock, &msg, 0);
285 static int64_t dhcpv6_rand_delay(int64_t time)
288 read(urandom_fd, &random, sizeof(random));
289 return (time * (random % 1000)) / 10000;
293 int dhcpv6_request(enum dhcpv6_msg type)
296 uint32_t timeout = UINT32_MAX;
297 struct dhcpv6_retx *retx = &dhcpv6_retx[type];
300 struct timespec ts = {0, 0};
301 ts.tv_nsec = dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY);
302 nanosleep(&ts, NULL);
305 if (type == DHCPV6_MSG_RELEASE || type == DHCPV6_MSG_DECLINE)
307 else if (type == DHCPV6_MSG_UNKNOWN)
309 else if (type == DHCPV6_MSG_RENEW)
311 else if (type == DHCPV6_MSG_REBIND)
317 syslog(LOG_NOTICE, "Sending %s (timeout %us)", retx->name, timeout);
319 uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
321 // Generate transaction ID
323 read(urandom_fd, trid, sizeof(trid));
328 rto = (rto == 0) ? (retx->init_timeo * 1000 +
329 dhcpv6_rand_delay(retx->init_timeo * 1000)) :
330 (2 * rto + dhcpv6_rand_delay(rto));
332 if (rto >= retx->max_timeo * 1000)
333 rto = retx->max_timeo * 1000 +
334 dhcpv6_rand_delay(retx->max_timeo * 1000);
336 // Calculate end for this round and elapsed time
337 uint64_t round_end = round_start + rto;
338 elapsed = round_start - start;
340 // Don't wait too long
341 if (round_end - start > timeout * 1000)
342 round_end = timeout * 1000 + start;
344 // Built and send package
345 if (type != DHCPV6_MSG_UNKNOWN)
346 dhcpv6_send(type, trid, elapsed / 10);
349 for (; len < 0 && round_start < round_end;
350 round_start = odhcp6c_get_milli_time()) {
351 // Check for pending signal
352 if (odhcp6c_signal_is_pending())
355 // Set timeout for receiving
356 uint64_t t = round_end - round_start;
357 struct timeval timeout = {t / 1000, (t % 1000) * 1000};
358 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
359 &timeout, sizeof(timeout));
362 len = recv(sock, buf, sizeof(buf), 0);
364 if (!dhcpv6_response_is_valid(buf, len, trid, type))
368 uint8_t *opt = &buf[4];
369 uint8_t *opt_end = opt + len - 4;
371 round_start = odhcp6c_get_milli_time();
372 elapsed = round_start - start;
373 syslog(LOG_NOTICE, "Got a valid reply after "
374 "%ums", (unsigned)elapsed);
376 if (retx->handler_reply)
377 len = retx->handler_reply(type,
378 opt, opt_end, elapsed / 1000);
383 if (retx->handler_finish)
384 len = retx->handler_finish(elapsed / 1000);
385 } while (len < 0 && elapsed / 1000 < timeout);
391 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
392 const uint8_t transaction[3], enum dhcpv6_msg type)
394 const struct dhcpv6_header *rep = buf;
395 if (len < (ssize_t)sizeof(*rep) || memcmp(rep->tr_id,
396 transaction, sizeof(rep->tr_id)))
397 return false; // Invalid reply
399 if (type == DHCPV6_MSG_SOLICIT) {
400 if (rep->msg_type != DHCPV6_MSG_ADVERT &&
401 rep->msg_type != DHCPV6_MSG_REPLY)
403 } else if (type == DHCPV6_MSG_UNKNOWN) {
404 if (!accept_reconfig || rep->msg_type != DHCPV6_MSG_RECONF)
406 } else if (rep->msg_type != DHCPV6_MSG_REPLY) {
410 uint8_t *end = ((uint8_t*)buf) + len, *odata;
411 uint16_t otype, olen;
412 bool clientid_ok = false, serverid_ok = false;
414 size_t client_id_len, server_id_len;
415 void *client_id = odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
416 void *server_id = odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
418 dhcpv6_for_each_option(&rep[1], end, otype, olen, odata)
419 if (otype == DHCPV6_OPT_CLIENTID)
420 clientid_ok = (olen + 4U == client_id_len) && !memcmp(
421 &odata[-4], client_id, client_id_len);
422 else if (otype == DHCPV6_OPT_SERVERID)
423 serverid_ok = (olen + 4U == server_id_len) && !memcmp(
424 &odata[-4], server_id, server_id_len);
426 return clientid_ok && (serverid_ok || server_id_len == 0);
430 int dhcpv6_poll_reconfigure(void)
432 int ret = dhcpv6_request(DHCPV6_MSG_UNKNOWN);
434 ret = dhcpv6_request(ret);
440 static int dhcpv6_handle_reconfigure(_unused enum dhcpv6_msg orig,
441 const void *opt, const void *end, uint32_t elapsed)
443 // TODO: should verify the reconfigure message
444 uint16_t otype, olen;
445 uint8_t *odata, msg = DHCPV6_MSG_RENEW;
446 dhcpv6_for_each_option(opt, end, otype, olen, odata)
447 if (otype == DHCPV6_OPT_RECONF_MESSAGE && olen == 1 && (
448 odata[0] == DHCPV6_MSG_RENEW ||
449 odata[0] == DHCPV6_MSG_INFO_REQ))
465 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL, elapsed);
470 // Collect all advertised servers
471 static int dhcpv6_handle_advert(_unused enum dhcpv6_msg orig,
472 const void *opt, const void *end, _unused uint32_t elapsed)
474 uint16_t olen, otype;
476 struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}};
478 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
479 if (otype == DHCPV6_OPT_SERVERID && olen <= 130) {
480 memcpy(cand.duid, odata, olen);
481 cand.duid_len = olen;
482 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
483 && odata[1] == DHCPV6_NoAddrsAvail) {
484 if (na_mode == IA_MODE_FORCE) {
487 cand.has_noaddravail = true;
488 cand.preference -= 1000;
490 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
491 && odata[1] == DHCPV6_NoPrefixAvail) {
492 cand.preference -= 2000;
493 } else if (otype == DHCPV6_OPT_PREF && olen >= 1 &&
494 cand.preference >= 0) {
495 cand.preference = odata[1];
496 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
497 cand.wants_reconfigure = true;
498 } else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
499 struct dhcpv6_ia_hdr *h = (void*)odata;
500 uint8_t *oend = odata + olen, *d;
501 dhcpv6_for_each_option(&h[1], oend, otype, olen, d) {
502 if (otype == DHCPV6_OPT_IA_PREFIX)
503 cand.preference += 2000;
504 else if (otype == DHCPV6_OPT_STATUS &&
505 olen >= 2 && d[0] == 0 &&
506 d[1] == DHCPV6_NoPrefixAvail)
507 cand.preference -= 2000;
512 if (cand.duid_len > 0)
513 odhcp6c_add_state(STATE_SERVER_CAND, &cand, sizeof(cand));
519 static int dhcpv6_commit_advert(_unused uint32_t elapsed)
522 struct dhcpv6_server_cand *c = NULL, *cand =
523 odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
526 for (size_t i = 0; i < cand_len / sizeof(*c); ++i) {
527 if (cand[i].has_noaddravail)
528 retry = true; // We want to try again
530 if (!c || c->preference < cand[i].preference)
534 if (retry && na_mode == IA_MODE_TRY) {
535 // We give it a second try without the IA_NA
536 na_mode = IA_MODE_NONE;
537 return dhcpv6_request(DHCPV6_MSG_SOLICIT);
541 uint16_t hdr[2] = {htons(DHCPV6_OPT_SERVERID),
543 odhcp6c_add_state(STATE_SERVER_ID, hdr, sizeof(hdr));
544 odhcp6c_add_state(STATE_SERVER_ID, c->duid, c->duid_len);
545 accept_reconfig = c->wants_reconfigure;
548 odhcp6c_clear_state(STATE_SERVER_CAND);
552 else if (request_prefix || na_mode != IA_MODE_NONE)
553 return DHCPV6_STATEFUL;
555 return DHCPV6_STATELESS;
559 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig,
560 const void *opt, const void *end, uint32_t elapsed)
562 dhcpv6_handle_advert(orig, opt, end, elapsed);
563 if (dhcpv6_commit_advert(elapsed) < 0)
566 return dhcpv6_handle_reply(orig, opt, end, elapsed);
570 static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
571 const void *opt, const void *end, uint32_t elapsed)
573 uint16_t otype, olen;
575 bool have_update = false;
577 t1 = t2 = t3 = 86400;
579 size_t ia_na_len, dns_len, search_len, sntp_ip_len, sntp_dns_len;
580 size_t sip_ip_len, sip_fqdn_len;
581 uint8_t *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
583 odhcp6c_get_state(STATE_DNS, &dns_len);
584 odhcp6c_get_state(STATE_SEARCH, &search_len);
585 odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len);
586 odhcp6c_get_state(STATE_SNTP_FQDN, &sntp_dns_len);
587 odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len);
588 odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len);
590 // Decrease valid and preferred lifetime of prefixes
592 uint8_t *ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
593 dhcpv6_for_each_option(ia_pd, ia_pd + ia_pd_len, otype, olen, odata) {
594 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
595 uint32_t valid = ntohl(p->valid);
596 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
598 uint32_t pref = ntohl(p->preferred);
599 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
602 // Decrease valid and preferred lifetime of addresses
603 dhcpv6_for_each_option(ia_na, ia_na + ia_na_len, otype, olen, odata) {
604 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
605 uint32_t valid = ntohl(p->valid);
606 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
608 uint32_t pref = ntohl(p->preferred);
609 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
612 // Parse and find all matching IAs
613 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
614 if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)
615 && olen > sizeof(struct dhcpv6_ia_hdr)) {
616 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
617 time_t l_t1 = ntohl(ia_hdr->t1);
618 time_t l_t2 = ntohl(ia_hdr->t2);
620 // Test ID and T1-T2 validity
621 if (ia_hdr->iaid != 1 || l_t2 < l_t1)
624 uint16_t stype, slen;
626 // Test status and bail if error
627 dhcpv6_for_each_option(&ia_hdr[1], odata + olen,
629 if (stype == DHCPV6_OPT_STATUS && slen >= 2 &&
630 (sdata[0] || sdata[1]))
634 if (l_t1 > 0 && t1 > l_t1)
637 if (l_t2 > 0 && t2 > l_t2)
640 // Always report update in case we have IA_PDs so that
641 // the state-script is called with updated times
642 if (otype == DHCPV6_OPT_IA_PD && request_prefix)
645 time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
656 } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
658 odhcp6c_add_state(STATE_DNS, odata, olen);
659 } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
660 odhcp6c_add_state(STATE_SEARCH, odata, olen);
661 } else if (otype == DHCPV6_OPT_NTP_SERVER) {
662 uint16_t stype, slen;
664 // Test status and bail if error
665 dhcpv6_for_each_option(odata, odata + olen,
666 stype, slen, sdata) {
667 if (slen == 16 && (stype == NTP_MC_ADDR ||
668 stype == NTP_SRV_ADDR))
669 odhcp6c_add_state(STATE_SNTP_IP,
671 else if (slen > 0 && stype == NTP_SRV_FQDN)
672 odhcp6c_add_state(STATE_SNTP_FQDN,
675 } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
677 odhcp6c_add_state(STATE_SIP_IP, odata, olen);
678 } else if (otype == DHCPV6_OPT_SIP_SERVER_D) {
679 odhcp6c_add_state(STATE_SIP_FQDN, odata, olen);
680 } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
681 uint32_t refresh = ntohl(*((uint32_t*)odata));
682 if (refresh < (uint32_t)t1)
684 } else if (otype != DHCPV6_OPT_CLIENTID &&
685 otype != DHCPV6_OPT_SERVERID) {
686 odhcp6c_add_state(STATE_CUSTOM_OPTS,
687 &odata[-4], olen + 4);
692 have_update |= odhcp6c_commit_state(STATE_DNS, dns_len);
693 have_update |= odhcp6c_commit_state(STATE_SEARCH, search_len);
694 have_update |= odhcp6c_commit_state(STATE_SNTP_IP,
696 have_update |= odhcp6c_commit_state(STATE_SNTP_FQDN,
698 have_update |= odhcp6c_commit_state(STATE_SIP_IP, sip_ip_len);
699 have_update |= odhcp6c_commit_state(STATE_SIP_FQDN, sip_fqdn_len);
700 size_t new_ia_pd_len, new_ia_na_len;
701 odhcp6c_get_state(STATE_IA_PD, &new_ia_pd_len);
702 odhcp6c_get_state(STATE_IA_NA, &new_ia_na_len);
703 have_update |= (new_ia_pd_len != ia_pd_len) ||
704 (new_ia_na_len != ia_na_len);
707 // Delete prefixes with 0 valid-time
708 ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
709 ia_end = ia_pd + ia_pd_len;
710 dhcpv6_for_each_option(ia_pd, ia_end, otype, olen, odata) {
711 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
713 ia_end = ia_pd + odhcp6c_remove_state(STATE_IA_PD,
714 (uint8_t*)p - ia_pd, olen + 4);
720 // Delete addresses with 0 valid-time
721 ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
722 ia_end = ia_na + ia_na_len;
723 dhcpv6_for_each_option(ia_na, ia_end, otype, olen, odata) {
724 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
726 ia_end = ia_na + odhcp6c_remove_state(STATE_IA_NA,
727 (uint8_t*)p - ia_na, olen + 4);
736 static time_t dhcpv6_parse_ia(void *opt, void *end)
738 uint32_t timeout = UINT32_MAX; // Minimum timeout
739 uint16_t otype, olen, stype, slen;
740 uint8_t *odata, *sdata;
743 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
744 if (otype == DHCPV6_OPT_IA_PREFIX) {
745 struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
746 if (olen + 4U < sizeof(*prefix))
749 olen = sizeof(*prefix); // Normalize length
750 uint32_t valid = ntohl(prefix->valid);
751 uint32_t pref = ntohl(prefix->preferred);
756 // Search matching IA
757 struct dhcpv6_ia_prefix *local = NULL;
759 uint8_t *pd = odhcp6c_get_state(STATE_IA_PD, &pd_len);
760 dhcpv6_for_each_option(pd, pd + pd_len,
762 if (!memcmp(sdata + 8, odata + 8,
763 sizeof(local->addr) + 1))
764 local = (void*)&sdata[-4];
766 if (local) { // Already know that IA
767 local->preferred = prefix->preferred;
768 local->valid = prefix->valid;
770 odhcp6c_add_state(STATE_IA_PD, prefix, olen);
776 if (prefix->valid == 0) // We probably lost that prefix
777 odhcp6c_add_state(STATE_IA_PD_LOST,
779 } else if (otype == DHCPV6_OPT_IA_ADDR) {
780 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
781 if (olen + 4U < sizeof(*addr))
784 olen = sizeof(*addr); // Normalize length
785 uint32_t pref = ntohl(addr->preferred);
786 uint32_t valid = ntohl(addr->valid);
791 // Search matching IA
792 struct dhcpv6_ia_addr *local = NULL;
794 uint8_t *na = odhcp6c_get_state(STATE_IA_NA, &na_len);
795 dhcpv6_for_each_option(na, na + na_len,
797 if (!memcmp(sdata, odata, sizeof(local->addr)))
798 local = (void*)&sdata[-4];
801 if (local) { // Already know that IA
802 local->preferred = addr->preferred;
803 local->valid = addr->valid;
805 odhcp6c_add_state(STATE_IA_NA, addr, olen);
812 if (set_rtnetlink_addr(ifindex, &addr->addr,
813 pref, valid) == -EADDRNOTAVAIL) {
814 dhcpv6_request(DHCPV6_MSG_DECLINE);