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 odhcp6c_add_state(STATE_ORO, oro, sizeof(oro));
122 // Configure IPv6-options
124 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
125 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
126 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
128 struct sockaddr_in6 client_addr = { .sin6_family = AF_INET6,
129 .sin6_port = htons(DHCPV6_CLIENT_PORT), .sin6_flowinfo = 0 };
130 if (bind(sock, (struct sockaddr*)&client_addr, sizeof(client_addr)))
137 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode)
143 void dhcpv6_remove_addrs(void)
146 uint8_t *odata, *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
147 uint16_t otype, olen;
148 dhcpv6_for_each_option(ia_na, ia_na + ia_na_len, otype, olen, odata) {
149 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
150 set_rtnetlink_addr(ifindex, &addr->addr, 0, 0);
155 static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
159 gethostname(fqdn_buf, sizeof(fqdn_buf));
166 size_t fqdn_len = 5 + dn_comp(fqdn_buf, fqdn.data,
167 sizeof(fqdn.data), NULL, NULL);
168 fqdn.type = htons(DHCPV6_OPT_FQDN);
169 fqdn.len = htons(fqdn_len - 4);
175 void *cl_id = odhcp6c_get_state(STATE_CLIENT_ID, &cl_id_len);
179 void *srv_id = odhcp6c_get_state(STATE_SERVER_ID, &srv_id_len);
183 void *ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
184 struct dhcpv6_ia_hdr hdr_ia_pd = {
185 htons(DHCPV6_OPT_IA_PD),
186 htons(sizeof(hdr_ia_pd) - 4 + ia_pd_len),
190 struct dhcpv6_ia_prefix pref = {
191 .type = htons(DHCPV6_OPT_IA_PREFIX),
192 .len = htons(25), .prefix = request_prefix
195 if (ia_pd_len == 0 && request_prefix > 0 &&
196 (type == DHCPV6_MSG_SOLICIT ||
197 type == DHCPV6_MSG_REQUEST)) {
199 ia_pd_len = sizeof(pref);
204 void *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
205 struct dhcpv6_ia_hdr hdr_ia_na = {
206 htons(DHCPV6_OPT_IA_NA),
207 htons(sizeof(hdr_ia_na) - 4 + ia_na_len),
211 // Reconfigure Accept
215 } reconf_accept = {htons(DHCPV6_OPT_RECONF_ACCEPT), 0};
217 // Request Information Refresh
218 uint16_t oro_refresh = htons(DHCPV6_OPT_INFO_REFRESH);
222 void *oro = odhcp6c_get_state(STATE_ORO, &oro_len);
226 uint16_t elapsed_type;
227 uint16_t elapsed_len;
228 uint16_t elapsed_value;
232 type, {trid[0], trid[1], trid[2]},
233 htons(DHCPV6_OPT_ELAPSED), htons(2),
234 htons((ecs > 0xffff) ? 0xffff : ecs),
235 htons(DHCPV6_OPT_ORO), htons(oro_len),
238 struct iovec iov[] = {
243 {srv_id, srv_id_len},
246 {&hdr_ia_na, sizeof(hdr_ia_na)},
248 {&hdr_ia_pd, sizeof(hdr_ia_pd)},
252 size_t cnt = ARRAY_SIZE(iov);
253 if (type == DHCPV6_MSG_INFO_REQ) {
255 iov[2].iov_len = sizeof(oro_refresh);
256 hdr.oro_len = htons(oro_len + sizeof(oro_refresh));
257 } else if (!request_prefix) {
261 // Disable IAs if not used
262 if (type == DHCPV6_MSG_SOLICIT) {
263 iov[5].iov_len = sizeof(reconf_accept);
264 } else if (type != DHCPV6_MSG_REQUEST) {
271 if (na_mode == IA_MODE_NONE)
274 struct sockaddr_in6 srv = {AF_INET6, htons(DHCPV6_SERVER_PORT),
275 0, ALL_DHCPV6_RELAYS, ifindex};
276 struct msghdr msg = {&srv, sizeof(srv), iov, cnt, NULL, 0, 0};
278 sendmsg(sock, &msg, 0);
282 static int64_t dhcpv6_rand_delay(int64_t time)
285 read(urandom_fd, &random, sizeof(random));
286 return (time * (random % 1000)) / 10000;
290 int dhcpv6_request(enum dhcpv6_msg type)
293 uint32_t timeout = UINT32_MAX;
294 struct dhcpv6_retx *retx = &dhcpv6_retx[type];
297 struct timespec ts = {0, 0};
298 ts.tv_nsec = dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY);
299 nanosleep(&ts, NULL);
302 if (type == DHCPV6_MSG_RELEASE || type == DHCPV6_MSG_DECLINE)
304 else if (type == DHCPV6_MSG_UNKNOWN)
306 else if (type == DHCPV6_MSG_RENEW)
308 else if (type == DHCPV6_MSG_REBIND)
314 syslog(LOG_NOTICE, "Sending %s (timeout %us)", retx->name, timeout);
316 uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
318 // Generate transaction ID
320 read(urandom_fd, trid, sizeof(trid));
325 rto = (rto == 0) ? (retx->init_timeo * 1000 +
326 dhcpv6_rand_delay(retx->init_timeo * 1000)) :
327 (2 * rto + dhcpv6_rand_delay(rto));
329 if (rto >= retx->max_timeo * 1000)
330 rto = retx->max_timeo * 1000 +
331 dhcpv6_rand_delay(retx->max_timeo * 1000);
333 // Calculate end for this round and elapsed time
334 uint64_t round_end = round_start + rto;
335 elapsed = round_start - start;
337 // Don't wait too long
338 if (round_end - start > timeout * 1000)
339 round_end = timeout * 1000 + start;
341 // Built and send package
342 if (type != DHCPV6_MSG_UNKNOWN)
343 dhcpv6_send(type, trid, elapsed / 10);
346 for (; len < 0 && round_start < round_end;
347 round_start = odhcp6c_get_milli_time()) {
348 // Check for pending signal
349 if (odhcp6c_signal_is_pending())
352 // Set timeout for receiving
353 uint64_t t = round_end - round_start;
354 struct timeval timeout = {t / 1000, (t % 1000) * 1000};
355 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
356 &timeout, sizeof(timeout));
359 len = recv(sock, buf, sizeof(buf), 0);
361 if (!dhcpv6_response_is_valid(buf, len, trid, type))
365 uint8_t *opt = &buf[4];
366 uint8_t *opt_end = opt + len - 4;
368 round_start = odhcp6c_get_milli_time();
369 elapsed = round_start - start;
370 syslog(LOG_NOTICE, "Got a valid reply after "
371 "%ums", (unsigned)elapsed);
373 if (retx->handler_reply)
374 len = retx->handler_reply(type,
375 opt, opt_end, elapsed / 1000);
380 if (retx->handler_finish)
381 len = retx->handler_finish(elapsed / 1000);
382 } while (len < 0 && elapsed / 1000 < timeout);
388 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
389 const uint8_t transaction[3], enum dhcpv6_msg type)
391 const struct dhcpv6_header *rep = buf;
392 if (len < (ssize_t)sizeof(*rep) || memcmp(rep->tr_id,
393 transaction, sizeof(rep->tr_id)))
394 return false; // Invalid reply
396 if (type == DHCPV6_MSG_SOLICIT) {
397 if (rep->msg_type != DHCPV6_MSG_ADVERT &&
398 rep->msg_type != DHCPV6_MSG_REPLY)
400 } else if (type == DHCPV6_MSG_UNKNOWN) {
401 if (!accept_reconfig || rep->msg_type != DHCPV6_MSG_RECONF)
403 } else if (rep->msg_type != DHCPV6_MSG_REPLY) {
407 uint8_t *end = ((uint8_t*)buf) + len, *odata;
408 uint16_t otype, olen;
409 bool clientid_ok = false, serverid_ok = false;
411 size_t client_id_len, server_id_len;
412 void *client_id = odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
413 void *server_id = odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
415 dhcpv6_for_each_option(&rep[1], end, otype, olen, odata)
416 if (otype == DHCPV6_OPT_CLIENTID)
417 clientid_ok = (olen + 4U == client_id_len) && !memcmp(
418 &odata[-4], client_id, client_id_len);
419 else if (otype == DHCPV6_OPT_SERVERID)
420 serverid_ok = (olen + 4U == server_id_len) && !memcmp(
421 &odata[-4], server_id, server_id_len);
423 return clientid_ok && (serverid_ok || server_id_len == 0);
427 int dhcpv6_poll_reconfigure(void)
429 int ret = dhcpv6_request(DHCPV6_MSG_UNKNOWN);
431 ret = dhcpv6_request(ret);
437 static int dhcpv6_handle_reconfigure(_unused enum dhcpv6_msg orig,
438 const void *opt, const void *end, uint32_t elapsed)
440 // TODO: should verify the reconfigure message
441 uint16_t otype, olen;
442 uint8_t *odata, msg = DHCPV6_MSG_RENEW;
443 dhcpv6_for_each_option(opt, end, otype, olen, odata)
444 if (otype == DHCPV6_OPT_RECONF_MESSAGE && olen == 1 && (
445 odata[0] == DHCPV6_MSG_RENEW ||
446 odata[0] == DHCPV6_MSG_INFO_REQ))
462 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL, elapsed);
467 // Collect all advertised servers
468 static int dhcpv6_handle_advert(_unused enum dhcpv6_msg orig,
469 const void *opt, const void *end, _unused uint32_t elapsed)
471 uint16_t olen, otype;
473 struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}};
475 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
476 if (otype == DHCPV6_OPT_SERVERID && olen <= 130) {
477 memcpy(cand.duid, odata, olen);
478 cand.duid_len = olen;
479 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
480 && odata[1] == DHCPV6_NoAddrsAvail) {
481 if (na_mode == IA_MODE_FORCE) {
484 cand.has_noaddravail = true;
485 cand.preference -= 1000;
487 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
488 && odata[1] == DHCPV6_NoPrefixAvail) {
489 cand.preference -= 2000;
490 } else if (otype == DHCPV6_OPT_PREF && olen >= 1 &&
491 cand.preference >= 0) {
492 cand.preference = odata[1];
493 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
494 cand.wants_reconfigure = true;
495 } else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
496 struct dhcpv6_ia_hdr *h = (void*)odata;
497 uint8_t *oend = odata + olen, *d;
498 dhcpv6_for_each_option(&h[1], oend, otype, olen, d) {
499 if (otype == DHCPV6_OPT_IA_PREFIX)
500 cand.preference += 2000;
501 else if (otype == DHCPV6_OPT_STATUS &&
502 olen >= 2 && d[0] == 0 &&
503 d[1] == DHCPV6_NoPrefixAvail)
504 cand.preference -= 2000;
509 if (cand.duid_len > 0)
510 odhcp6c_add_state(STATE_SERVER_CAND, &cand, sizeof(cand));
516 static int dhcpv6_commit_advert(_unused uint32_t elapsed)
519 struct dhcpv6_server_cand *c = NULL, *cand =
520 odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
523 for (size_t i = 0; i < cand_len / sizeof(*c); ++i) {
524 if (cand[i].has_noaddravail)
525 retry = true; // We want to try again
527 if (!c || c->preference < cand[i].preference)
531 if (retry && na_mode == IA_MODE_TRY) {
532 // We give it a second try without the IA_NA
533 na_mode = IA_MODE_NONE;
534 return dhcpv6_request(DHCPV6_MSG_SOLICIT);
538 uint16_t hdr[2] = {htons(DHCPV6_OPT_SERVERID),
540 odhcp6c_add_state(STATE_SERVER_ID, hdr, sizeof(hdr));
541 odhcp6c_add_state(STATE_SERVER_ID, c->duid, c->duid_len);
542 accept_reconfig = c->wants_reconfigure;
545 odhcp6c_clear_state(STATE_SERVER_CAND);
549 else if (request_prefix || na_mode != IA_MODE_NONE)
550 return DHCPV6_STATEFUL;
552 return DHCPV6_STATELESS;
556 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig,
557 const void *opt, const void *end, uint32_t elapsed)
559 dhcpv6_handle_advert(orig, opt, end, elapsed);
560 if (dhcpv6_commit_advert(elapsed) < 0)
563 return dhcpv6_handle_reply(orig, opt, end, elapsed);
567 static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
568 const void *opt, const void *end, uint32_t elapsed)
570 uint16_t otype, olen;
572 bool have_update = false;
574 t1 = t2 = t3 = 86400;
576 size_t ia_na_len, dns_len, search_len, sntp_ip_len, sntp_dns_len;
577 uint8_t *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
579 odhcp6c_get_state(STATE_DNS, &dns_len);
580 odhcp6c_get_state(STATE_SEARCH, &search_len);
581 odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len);
582 odhcp6c_get_state(STATE_SNTP_FQDN, &sntp_dns_len);
584 // Decrease valid and preferred lifetime of prefixes
586 uint8_t *ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
587 dhcpv6_for_each_option(ia_pd, ia_pd + ia_pd_len, otype, olen, odata) {
588 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
589 uint32_t valid = ntohl(p->valid);
590 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
592 uint32_t pref = ntohl(p->preferred);
593 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
596 // Decrease valid and preferred lifetime of addresses
597 dhcpv6_for_each_option(ia_na, ia_na + ia_na_len, otype, olen, odata) {
598 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
599 uint32_t valid = ntohl(p->valid);
600 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
602 uint32_t pref = ntohl(p->preferred);
603 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
606 // Parse and find all matching IAs
607 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
608 if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)
609 && olen > sizeof(struct dhcpv6_ia_hdr)) {
610 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
611 time_t l_t1 = ntohl(ia_hdr->t1);
612 time_t l_t2 = ntohl(ia_hdr->t2);
614 // Test ID and T1-T2 validity
615 if (ia_hdr->iaid != 1 || l_t2 < l_t1)
618 uint16_t stype, slen;
620 // Test status and bail if error
621 dhcpv6_for_each_option(&ia_hdr[1], odata + olen,
623 if (stype == DHCPV6_OPT_STATUS && slen >= 2 &&
624 (sdata[0] || sdata[1]))
628 if (l_t1 > 0 && t1 > l_t1)
631 if (l_t2 > 0 && t2 > l_t2)
634 // Always report update in case we have IA_PDs so that
635 // the state-script is called with updated times
636 if (otype == DHCPV6_OPT_IA_PD && request_prefix)
639 time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
650 } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
651 odhcp6c_add_state(STATE_DNS, odata, olen);
652 } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
653 odhcp6c_add_state(STATE_SEARCH, odata, olen);
654 } else if (otype == DHCPV6_OPT_NTP_SERVER) {
655 uint16_t stype, slen;
657 // Test status and bail if error
658 dhcpv6_for_each_option(odata, odata + olen,
659 stype, slen, sdata) {
660 if (slen == 16 && (stype == NTP_MC_ADDR ||
661 stype == NTP_SRV_ADDR))
662 odhcp6c_add_state(STATE_SNTP_IP,
664 else if (slen > 0 && stype == NTP_SRV_FQDN)
665 odhcp6c_add_state(STATE_SNTP_FQDN,
668 } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
669 uint32_t refresh = ntohl(*((uint32_t*)odata));
670 if (refresh < (uint32_t)t1)
672 } else if (otype != DHCPV6_OPT_CLIENTID &&
673 otype != DHCPV6_OPT_SERVERID) {
674 odhcp6c_add_state(STATE_CUSTOM_OPTS,
675 &odata[-4], olen + 4);
680 have_update |= odhcp6c_commit_state(STATE_DNS, dns_len);
681 have_update |= odhcp6c_commit_state(STATE_SEARCH, search_len);
682 have_update |= odhcp6c_commit_state(STATE_SNTP_IP,
684 have_update |= odhcp6c_commit_state(STATE_SNTP_FQDN,
686 size_t new_ia_pd_len, new_ia_na_len;
687 odhcp6c_get_state(STATE_IA_PD, &new_ia_pd_len);
688 odhcp6c_get_state(STATE_IA_NA, &new_ia_na_len);
689 have_update |= (new_ia_pd_len != ia_pd_len) ||
690 (new_ia_na_len != ia_na_len);
693 // Delete prefixes with 0 valid-time
694 ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
695 ia_end = ia_pd + ia_pd_len;
696 dhcpv6_for_each_option(ia_pd, ia_end, otype, olen, odata) {
697 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
699 ia_end = ia_pd + odhcp6c_remove_state(STATE_IA_PD,
700 (uint8_t*)p - ia_pd, olen + 4);
706 // Delete addresses with 0 valid-time
707 ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
708 ia_end = ia_na + ia_na_len;
709 dhcpv6_for_each_option(ia_na, ia_end, otype, olen, odata) {
710 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
712 ia_end = ia_na + odhcp6c_remove_state(STATE_IA_NA,
713 (uint8_t*)p - ia_na, olen + 4);
722 static time_t dhcpv6_parse_ia(void *opt, void *end)
724 uint32_t timeout = UINT32_MAX; // Minimum timeout
725 uint16_t otype, olen, stype, slen;
726 uint8_t *odata, *sdata;
729 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
730 if (otype == DHCPV6_OPT_IA_PREFIX) {
731 struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
732 if (olen + 4U < sizeof(*prefix))
735 olen = sizeof(*prefix); // Normalize length
736 uint32_t valid = ntohl(prefix->valid);
737 uint32_t pref = ntohl(prefix->preferred);
742 // Search matching IA
743 struct dhcpv6_ia_prefix *local = NULL;
745 uint8_t *pd = odhcp6c_get_state(STATE_IA_PD, &pd_len);
746 dhcpv6_for_each_option(pd, pd + pd_len,
748 if (!memcmp(sdata + 8, odata + 8,
749 sizeof(local->addr) + 1))
750 local = (void*)&sdata[-4];
752 if (local) { // Already know that IA
753 local->preferred = prefix->preferred;
754 local->valid = prefix->valid;
756 odhcp6c_add_state(STATE_IA_PD, prefix, olen);
762 if (prefix->valid == 0) // We probably lost that prefix
763 odhcp6c_add_state(STATE_IA_PD_LOST,
765 } else if (otype == DHCPV6_OPT_IA_ADDR) {
766 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
767 if (olen + 4U < sizeof(*addr))
770 olen = sizeof(*addr); // Normalize length
771 uint32_t pref = ntohl(addr->preferred);
772 uint32_t valid = ntohl(addr->valid);
777 // Search matching IA
778 struct dhcpv6_ia_addr *local = NULL;
780 uint8_t *na = odhcp6c_get_state(STATE_IA_NA, &na_len);
781 dhcpv6_for_each_option(na, na + na_len,
783 if (!memcmp(sdata, odata, sizeof(local->addr)))
784 local = (void*)&sdata[-4];
787 if (local) { // Already know that IA
788 local->preferred = addr->preferred;
789 local->valid = addr->valid;
791 odhcp6c_add_state(STATE_IA_NA, addr, olen);
798 if (set_rtnetlink_addr(ifindex, &addr->addr,
799 pref, valid) == -EADDRNOTAVAIL) {
800 dhcpv6_request(DHCPV6_MSG_DECLINE);