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);
114 uint8_t zero[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
115 struct ifreq ifs[100], *ifp, *ifend;
118 ifc.ifc_len = sizeof(ifs);
120 if (!memcmp(&duid[8], zero, ETHER_ADDR_LEN) &&
121 ioctl(sock, SIOCGIFCONF, &ifc) >= 0) {
122 // If our interface doesn't have an address...
123 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
124 for (ifp = ifc.ifc_req; ifp < ifend &&
125 !memcmp(&duid[8], zero, 6); ifp++) {
126 memcpy(ifr.ifr_name, ifp->ifr_name,
127 sizeof(ifr.ifr_name));
128 ioctl(sock, SIOCGIFHWADDR, &ifr);
129 memcpy(&duid[8], ifr.ifr_hwaddr.sa_data,
134 odhcp6c_add_state(STATE_CLIENT_ID, duid, sizeof(duid));
138 uint16_t oro[] = {htons(DHCPV6_OPT_DNS_SERVERS),
139 htons(DHCPV6_OPT_DNS_DOMAIN),
140 htons(DHCPV6_OPT_NTP_SERVER),
141 htons(DHCPV6_OPT_SIP_SERVER_A),
142 htons(DHCPV6_OPT_SIP_SERVER_D)};
143 odhcp6c_add_state(STATE_ORO, oro, sizeof(oro));
146 // Configure IPv6-options
148 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
149 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
150 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
152 struct sockaddr_in6 client_addr = { .sin6_family = AF_INET6,
153 .sin6_port = htons(DHCPV6_CLIENT_PORT), .sin6_flowinfo = 0 };
154 if (bind(sock, (struct sockaddr*)&client_addr, sizeof(client_addr)))
161 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode)
167 void dhcpv6_remove_addrs(void)
170 uint8_t *odata, *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
171 uint16_t otype, olen;
172 dhcpv6_for_each_option(ia_na, ia_na + ia_na_len, otype, olen, odata) {
173 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
174 set_rtnetlink_addr(ifindex, &addr->addr, 0, 0);
179 static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
183 gethostname(fqdn_buf, sizeof(fqdn_buf));
190 size_t fqdn_len = 5 + dn_comp(fqdn_buf, fqdn.data,
191 sizeof(fqdn.data), NULL, NULL);
192 fqdn.type = htons(DHCPV6_OPT_FQDN);
193 fqdn.len = htons(fqdn_len - 4);
199 void *cl_id = odhcp6c_get_state(STATE_CLIENT_ID, &cl_id_len);
203 void *srv_id = odhcp6c_get_state(STATE_SERVER_ID, &srv_id_len);
207 void *ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
208 struct dhcpv6_ia_hdr hdr_ia_pd = {
209 htons(DHCPV6_OPT_IA_PD),
210 htons(sizeof(hdr_ia_pd) - 4 + ia_pd_len),
214 struct dhcpv6_ia_prefix pref = {
215 .type = htons(DHCPV6_OPT_IA_PREFIX),
216 .len = htons(25), .prefix = request_prefix
219 if (ia_pd_len == 0 && request_prefix > 0 &&
220 (type == DHCPV6_MSG_SOLICIT ||
221 type == DHCPV6_MSG_REQUEST)) {
223 ia_pd_len = sizeof(pref);
228 void *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
229 struct dhcpv6_ia_hdr hdr_ia_na = {
230 htons(DHCPV6_OPT_IA_NA),
231 htons(sizeof(hdr_ia_na) - 4 + ia_na_len),
235 // Reconfigure Accept
239 } reconf_accept = {htons(DHCPV6_OPT_RECONF_ACCEPT), 0};
241 // Request Information Refresh
242 uint16_t oro_refresh = htons(DHCPV6_OPT_INFO_REFRESH);
246 void *oro = odhcp6c_get_state(STATE_ORO, &oro_len);
250 uint16_t elapsed_type;
251 uint16_t elapsed_len;
252 uint16_t elapsed_value;
256 type, {trid[0], trid[1], trid[2]},
257 htons(DHCPV6_OPT_ELAPSED), htons(2),
258 htons((ecs > 0xffff) ? 0xffff : ecs),
259 htons(DHCPV6_OPT_ORO), htons(oro_len),
262 struct iovec iov[] = {
267 {srv_id, srv_id_len},
270 {&hdr_ia_na, sizeof(hdr_ia_na)},
272 {&hdr_ia_pd, sizeof(hdr_ia_pd)},
276 size_t cnt = ARRAY_SIZE(iov);
277 if (type == DHCPV6_MSG_INFO_REQ) {
279 iov[2].iov_len = sizeof(oro_refresh);
280 hdr.oro_len = htons(oro_len + sizeof(oro_refresh));
281 } else if (!request_prefix) {
285 // Disable IAs if not used
286 if (type == DHCPV6_MSG_SOLICIT) {
287 iov[5].iov_len = sizeof(reconf_accept);
288 } else if (type != DHCPV6_MSG_REQUEST) {
295 if (na_mode == IA_MODE_NONE)
298 struct sockaddr_in6 srv = {AF_INET6, htons(DHCPV6_SERVER_PORT),
299 0, ALL_DHCPV6_RELAYS, ifindex};
300 struct msghdr msg = {&srv, sizeof(srv), iov, cnt, NULL, 0, 0};
302 sendmsg(sock, &msg, 0);
306 static int64_t dhcpv6_rand_delay(int64_t time)
309 read(urandom_fd, &random, sizeof(random));
310 return (time * (random % 1000)) / 10000;
314 int dhcpv6_request(enum dhcpv6_msg type)
317 uint32_t timeout = UINT32_MAX;
318 struct dhcpv6_retx *retx = &dhcpv6_retx[type];
321 struct timespec ts = {0, 0};
322 ts.tv_nsec = dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY);
323 nanosleep(&ts, NULL);
326 if (type == DHCPV6_MSG_RELEASE || type == DHCPV6_MSG_DECLINE)
328 else if (type == DHCPV6_MSG_UNKNOWN)
330 else if (type == DHCPV6_MSG_RENEW)
332 else if (type == DHCPV6_MSG_REBIND)
338 syslog(LOG_NOTICE, "Sending %s (timeout %us)", retx->name, timeout);
340 uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
342 // Generate transaction ID
344 read(urandom_fd, trid, sizeof(trid));
349 rto = (rto == 0) ? (retx->init_timeo * 1000 +
350 dhcpv6_rand_delay(retx->init_timeo * 1000)) :
351 (2 * rto + dhcpv6_rand_delay(rto));
353 if (rto >= retx->max_timeo * 1000)
354 rto = retx->max_timeo * 1000 +
355 dhcpv6_rand_delay(retx->max_timeo * 1000);
357 // Calculate end for this round and elapsed time
358 uint64_t round_end = round_start + rto;
359 elapsed = round_start - start;
361 // Don't wait too long
362 if (round_end - start > timeout * 1000)
363 round_end = timeout * 1000 + start;
365 // Built and send package
366 if (type != DHCPV6_MSG_UNKNOWN)
367 dhcpv6_send(type, trid, elapsed / 10);
370 for (; len < 0 && round_start < round_end;
371 round_start = odhcp6c_get_milli_time()) {
372 // Check for pending signal
373 if (odhcp6c_signal_is_pending())
376 // Set timeout for receiving
377 uint64_t t = round_end - round_start;
378 struct timeval timeout = {t / 1000, (t % 1000) * 1000};
379 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
380 &timeout, sizeof(timeout));
383 len = recv(sock, buf, sizeof(buf), 0);
385 if (!dhcpv6_response_is_valid(buf, len, trid, type))
389 uint8_t *opt = &buf[4];
390 uint8_t *opt_end = opt + len - 4;
392 round_start = odhcp6c_get_milli_time();
393 elapsed = round_start - start;
394 syslog(LOG_NOTICE, "Got a valid reply after "
395 "%ums", (unsigned)elapsed);
397 if (retx->handler_reply)
398 len = retx->handler_reply(type,
399 opt, opt_end, elapsed / 1000);
404 if (retx->handler_finish)
405 len = retx->handler_finish(elapsed / 1000);
406 } while (len < 0 && elapsed / 1000 < timeout);
412 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
413 const uint8_t transaction[3], enum dhcpv6_msg type)
415 const struct dhcpv6_header *rep = buf;
416 if (len < (ssize_t)sizeof(*rep) || memcmp(rep->tr_id,
417 transaction, sizeof(rep->tr_id)))
418 return false; // Invalid reply
420 if (type == DHCPV6_MSG_SOLICIT) {
421 if (rep->msg_type != DHCPV6_MSG_ADVERT &&
422 rep->msg_type != DHCPV6_MSG_REPLY)
424 } else if (type == DHCPV6_MSG_UNKNOWN) {
425 if (!accept_reconfig || rep->msg_type != DHCPV6_MSG_RECONF)
427 } else if (rep->msg_type != DHCPV6_MSG_REPLY) {
431 uint8_t *end = ((uint8_t*)buf) + len, *odata;
432 uint16_t otype, olen;
433 bool clientid_ok = false, serverid_ok = false;
435 size_t client_id_len, server_id_len;
436 void *client_id = odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
437 void *server_id = odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
439 dhcpv6_for_each_option(&rep[1], end, otype, olen, odata)
440 if (otype == DHCPV6_OPT_CLIENTID)
441 clientid_ok = (olen + 4U == client_id_len) && !memcmp(
442 &odata[-4], client_id, client_id_len);
443 else if (otype == DHCPV6_OPT_SERVERID)
444 serverid_ok = (olen + 4U == server_id_len) && !memcmp(
445 &odata[-4], server_id, server_id_len);
447 return clientid_ok && (serverid_ok || server_id_len == 0);
451 int dhcpv6_poll_reconfigure(void)
453 int ret = dhcpv6_request(DHCPV6_MSG_UNKNOWN);
455 ret = dhcpv6_request(ret);
461 static int dhcpv6_handle_reconfigure(_unused enum dhcpv6_msg orig,
462 const void *opt, const void *end, uint32_t elapsed)
464 // TODO: should verify the reconfigure message
465 uint16_t otype, olen;
466 uint8_t *odata, msg = DHCPV6_MSG_RENEW;
467 dhcpv6_for_each_option(opt, end, otype, olen, odata)
468 if (otype == DHCPV6_OPT_RECONF_MESSAGE && olen == 1 && (
469 odata[0] == DHCPV6_MSG_RENEW ||
470 odata[0] == DHCPV6_MSG_INFO_REQ))
486 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL, elapsed);
491 // Collect all advertised servers
492 static int dhcpv6_handle_advert(_unused enum dhcpv6_msg orig,
493 const void *opt, const void *end, _unused uint32_t elapsed)
495 uint16_t olen, otype;
497 struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}};
499 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
500 if (otype == DHCPV6_OPT_SERVERID && olen <= 130) {
501 memcpy(cand.duid, odata, olen);
502 cand.duid_len = olen;
503 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
504 && odata[1] == DHCPV6_NoAddrsAvail) {
505 if (na_mode == IA_MODE_FORCE) {
508 cand.has_noaddravail = true;
509 cand.preference -= 1000;
511 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
512 && odata[1] == DHCPV6_NoPrefixAvail) {
513 cand.preference -= 2000;
514 } else if (otype == DHCPV6_OPT_PREF && olen >= 1 &&
515 cand.preference >= 0) {
516 cand.preference = odata[1];
517 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
518 cand.wants_reconfigure = true;
519 } else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
520 struct dhcpv6_ia_hdr *h = (void*)odata;
521 uint8_t *oend = odata + olen, *d;
522 dhcpv6_for_each_option(&h[1], oend, otype, olen, d) {
523 if (otype == DHCPV6_OPT_IA_PREFIX)
524 cand.preference += 2000;
525 else if (otype == DHCPV6_OPT_STATUS &&
526 olen >= 2 && d[0] == 0 &&
527 d[1] == DHCPV6_NoPrefixAvail)
528 cand.preference -= 2000;
533 if (cand.duid_len > 0)
534 odhcp6c_add_state(STATE_SERVER_CAND, &cand, sizeof(cand));
540 static int dhcpv6_commit_advert(_unused uint32_t elapsed)
543 struct dhcpv6_server_cand *c = NULL, *cand =
544 odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
547 for (size_t i = 0; i < cand_len / sizeof(*c); ++i) {
548 if (cand[i].has_noaddravail)
549 retry = true; // We want to try again
551 if (!c || c->preference < cand[i].preference)
555 if (retry && na_mode == IA_MODE_TRY) {
556 // We give it a second try without the IA_NA
557 na_mode = IA_MODE_NONE;
558 return dhcpv6_request(DHCPV6_MSG_SOLICIT);
562 uint16_t hdr[2] = {htons(DHCPV6_OPT_SERVERID),
564 odhcp6c_add_state(STATE_SERVER_ID, hdr, sizeof(hdr));
565 odhcp6c_add_state(STATE_SERVER_ID, c->duid, c->duid_len);
566 accept_reconfig = c->wants_reconfigure;
569 odhcp6c_clear_state(STATE_SERVER_CAND);
573 else if (request_prefix || na_mode != IA_MODE_NONE)
574 return DHCPV6_STATEFUL;
576 return DHCPV6_STATELESS;
580 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig,
581 const void *opt, const void *end, uint32_t elapsed)
583 dhcpv6_handle_advert(orig, opt, end, elapsed);
584 if (dhcpv6_commit_advert(elapsed) < 0)
587 return dhcpv6_handle_reply(orig, opt, end, elapsed);
591 static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
592 const void *opt, const void *end, uint32_t elapsed)
594 uint16_t otype, olen;
596 bool have_update = false;
598 t1 = t2 = t3 = 86400;
600 size_t ia_na_len, dns_len, search_len, sntp_ip_len, sntp_dns_len;
601 size_t sip_ip_len, sip_fqdn_len;
602 uint8_t *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
604 odhcp6c_get_state(STATE_DNS, &dns_len);
605 odhcp6c_get_state(STATE_SEARCH, &search_len);
606 odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len);
607 odhcp6c_get_state(STATE_SNTP_FQDN, &sntp_dns_len);
608 odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len);
609 odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len);
611 // Decrease valid and preferred lifetime of prefixes
613 uint8_t *ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
614 dhcpv6_for_each_option(ia_pd, ia_pd + ia_pd_len, otype, olen, odata) {
615 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
616 uint32_t valid = ntohl(p->valid);
617 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
619 uint32_t pref = ntohl(p->preferred);
620 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
623 // Decrease valid and preferred lifetime of addresses
624 dhcpv6_for_each_option(ia_na, ia_na + ia_na_len, otype, olen, odata) {
625 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
626 uint32_t valid = ntohl(p->valid);
627 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
629 uint32_t pref = ntohl(p->preferred);
630 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
633 // Parse and find all matching IAs
634 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
635 if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)
636 && olen > sizeof(struct dhcpv6_ia_hdr)) {
637 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
638 time_t l_t1 = ntohl(ia_hdr->t1);
639 time_t l_t2 = ntohl(ia_hdr->t2);
641 // Test ID and T1-T2 validity
642 if (ia_hdr->iaid != 1 || l_t2 < l_t1)
645 uint16_t stype, slen;
647 // Test status and bail if error
648 dhcpv6_for_each_option(&ia_hdr[1], odata + olen,
650 if (stype == DHCPV6_OPT_STATUS && slen >= 2 &&
651 (sdata[0] || sdata[1]))
655 if (l_t1 > 0 && t1 > l_t1)
658 if (l_t2 > 0 && t2 > l_t2)
661 // Always report update in case we have IA_PDs so that
662 // the state-script is called with updated times
663 if (otype == DHCPV6_OPT_IA_PD && request_prefix)
666 time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
677 } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
679 odhcp6c_add_state(STATE_DNS, odata, olen);
680 } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
681 odhcp6c_add_state(STATE_SEARCH, odata, olen);
682 } else if (otype == DHCPV6_OPT_NTP_SERVER) {
683 uint16_t stype, slen;
685 // Test status and bail if error
686 dhcpv6_for_each_option(odata, odata + olen,
687 stype, slen, sdata) {
688 if (slen == 16 && (stype == NTP_MC_ADDR ||
689 stype == NTP_SRV_ADDR))
690 odhcp6c_add_state(STATE_SNTP_IP,
692 else if (slen > 0 && stype == NTP_SRV_FQDN)
693 odhcp6c_add_state(STATE_SNTP_FQDN,
696 } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
698 odhcp6c_add_state(STATE_SIP_IP, odata, olen);
699 } else if (otype == DHCPV6_OPT_SIP_SERVER_D) {
700 odhcp6c_add_state(STATE_SIP_FQDN, odata, olen);
701 } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
702 uint32_t refresh = ntohl(*((uint32_t*)odata));
703 if (refresh < (uint32_t)t1)
705 } else if (otype != DHCPV6_OPT_CLIENTID &&
706 otype != DHCPV6_OPT_SERVERID) {
707 odhcp6c_add_state(STATE_CUSTOM_OPTS,
708 &odata[-4], olen + 4);
713 have_update |= odhcp6c_commit_state(STATE_DNS, dns_len);
714 have_update |= odhcp6c_commit_state(STATE_SEARCH, search_len);
715 have_update |= odhcp6c_commit_state(STATE_SNTP_IP,
717 have_update |= odhcp6c_commit_state(STATE_SNTP_FQDN,
719 have_update |= odhcp6c_commit_state(STATE_SIP_IP, sip_ip_len);
720 have_update |= odhcp6c_commit_state(STATE_SIP_FQDN, sip_fqdn_len);
721 size_t new_ia_pd_len, new_ia_na_len;
722 odhcp6c_get_state(STATE_IA_PD, &new_ia_pd_len);
723 odhcp6c_get_state(STATE_IA_NA, &new_ia_na_len);
724 have_update |= (new_ia_pd_len != ia_pd_len) ||
725 (new_ia_na_len != ia_na_len);
728 // Delete prefixes with 0 valid-time
729 ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
730 ia_end = ia_pd + ia_pd_len;
731 dhcpv6_for_each_option(ia_pd, ia_end, otype, olen, odata) {
732 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
734 ia_end = ia_pd + odhcp6c_remove_state(STATE_IA_PD,
735 (uint8_t*)p - ia_pd, olen + 4);
741 // Delete addresses with 0 valid-time
742 ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
743 ia_end = ia_na + ia_na_len;
744 dhcpv6_for_each_option(ia_na, ia_end, otype, olen, odata) {
745 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
747 ia_end = ia_na + odhcp6c_remove_state(STATE_IA_NA,
748 (uint8_t*)p - ia_na, olen + 4);
757 static time_t dhcpv6_parse_ia(void *opt, void *end)
759 uint32_t timeout = UINT32_MAX; // Minimum timeout
760 uint16_t otype, olen, stype, slen;
761 uint8_t *odata, *sdata;
764 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
765 if (otype == DHCPV6_OPT_IA_PREFIX) {
766 struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
767 if (olen + 4U < sizeof(*prefix))
770 olen = sizeof(*prefix); // Normalize length
771 uint32_t valid = ntohl(prefix->valid);
772 uint32_t pref = ntohl(prefix->preferred);
777 // Search matching IA
778 struct dhcpv6_ia_prefix *local = NULL;
780 uint8_t *pd = odhcp6c_get_state(STATE_IA_PD, &pd_len);
781 dhcpv6_for_each_option(pd, pd + pd_len,
783 if (!memcmp(sdata + 8, odata + 8,
784 sizeof(local->addr) + 1))
785 local = (void*)&sdata[-4];
787 if (local) { // Already know that IA
788 local->preferred = prefix->preferred;
789 local->valid = prefix->valid;
791 odhcp6c_add_state(STATE_IA_PD, prefix, olen);
796 } else if (otype == DHCPV6_OPT_IA_ADDR) {
797 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
798 if (olen + 4U < sizeof(*addr))
801 olen = sizeof(*addr); // Normalize length
802 uint32_t pref = ntohl(addr->preferred);
803 uint32_t valid = ntohl(addr->valid);
808 // Search matching IA
809 struct dhcpv6_ia_addr *local = NULL;
811 uint8_t *na = odhcp6c_get_state(STATE_IA_NA, &na_len);
812 dhcpv6_for_each_option(na, na + na_len,
814 if (!memcmp(sdata, odata, sizeof(local->addr)))
815 local = (void*)&sdata[-4];
818 if (local) { // Already know that IA
819 local->preferred = addr->preferred;
820 local->valid = addr->valid;
822 odhcp6c_add_state(STATE_IA_NA, addr, olen);
829 if (set_rtnetlink_addr(ifindex, &addr->addr,
830 pref, valid) == -EADDRNOTAVAIL) {
831 dhcpv6_request(DHCPV6_MSG_DECLINE);