]> git.decadent.org.uk Git - odhcp6c.git/blob - src/dhcpv6.c
Update requested options
[odhcp6c.git] / src / dhcpv6.c
1 /**
2  * Copyright (C) 2012 Steven Barth <steven@midlink.org>
3  *
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.
7  *
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.
12  *
13  */
14
15 #include <time.h>
16 #include <fcntl.h>
17 #include <errno.h>
18 #include <signal.h>
19 #include <limits.h>
20 #include <resolv.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <syslog.h>
24 #include <stdbool.h>
25 #include <sys/time.h>
26 #include <sys/ioctl.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29
30 #include <net/if.h>
31 #include <net/ethernet.h>
32
33 #include "odhcp6c.h"
34
35
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
42
43
44 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
45                 const uint8_t transaction[3], enum dhcpv6_msg type);
46
47 static time_t dhcpv6_parse_ia(void *opt, void *end);
48
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);
54
55
56
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},
73 };
74
75
76 // Sockets
77 static int sock = -1;
78 static int urandom_fd = -1;
79 static int ifindex = -1;
80 static time_t t1 = 0, t2 = 0, t3 = 0;
81
82 // IA states
83 static int request_prefix = -1;
84 static enum odhcp6c_ia_mode na_mode = IA_MODE_NONE;
85 static bool accept_reconfig = false;
86
87
88
89 int init_dhcpv6(const char *ifname, int request_pd)
90 {
91         request_prefix = request_pd;
92         urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY);
93         if (urandom_fd < 0)
94                 return -1;
95
96         sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
97
98         // Detect interface
99         struct ifreq ifr;
100         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
101         if (ioctl(sock, SIOCGIFINDEX, &ifr))
102                 return -1;
103         ifindex = ifr.ifr_ifindex;
104
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));
114         }
115
116         // Create ORO
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));
123
124
125         // Configure IPv6-options
126         int val = 1;
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));
130
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)))
134                 return -1;
135
136         return 0;
137 }
138
139
140 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode)
141 {
142         na_mode = mode;
143 }
144
145
146 void dhcpv6_remove_addrs(void)
147 {
148         size_t ia_na_len;
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);
154         }
155 }
156
157
158 static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
159 {
160         // Build FQDN
161         char fqdn_buf[256];
162         gethostname(fqdn_buf, sizeof(fqdn_buf));
163         struct {
164                 uint16_t type;
165                 uint16_t len;
166                 uint8_t flags;
167                 uint8_t data[256];
168         } fqdn;
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);
173         fqdn.flags = 0;
174
175
176         // Build Client ID
177         size_t cl_id_len;
178         void *cl_id = odhcp6c_get_state(STATE_CLIENT_ID, &cl_id_len);
179
180         // Get Server ID
181         size_t srv_id_len;
182         void *srv_id = odhcp6c_get_state(STATE_SERVER_ID, &srv_id_len);
183
184         // Build IA_PDs
185         size_t ia_pd_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),
190                 1, 0, 0
191         };
192
193         struct dhcpv6_ia_prefix pref = {
194                 .type = htons(DHCPV6_OPT_IA_PREFIX),
195                 .len = htons(25), .prefix = request_prefix
196         };
197
198         if (ia_pd_len == 0 && request_prefix > 0 &&
199                         (type == DHCPV6_MSG_SOLICIT ||
200                         type == DHCPV6_MSG_REQUEST)) {
201                 ia_pd = &pref;
202                 ia_pd_len = sizeof(pref);
203         }
204
205         // Build IA_NAs
206         size_t ia_na_len;
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),
211                 1, 0, 0
212         };
213
214         // Reconfigure Accept
215         struct {
216                 uint16_t type;
217                 uint16_t length;
218         } reconf_accept = {htons(DHCPV6_OPT_RECONF_ACCEPT), 0};
219
220         // Request Information Refresh
221         uint16_t oro_refresh = htons(DHCPV6_OPT_INFO_REFRESH);
222
223         // Prepare Header
224         size_t oro_len;
225         void *oro = odhcp6c_get_state(STATE_ORO, &oro_len);
226         struct {
227                 uint8_t type;
228                 uint8_t trid[3];
229                 uint16_t elapsed_type;
230                 uint16_t elapsed_len;
231                 uint16_t elapsed_value;
232                 uint16_t oro_type;
233                 uint16_t oro_len;
234         } hdr = {
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),
239         };
240
241         struct iovec iov[] = {
242                 {&hdr, sizeof(hdr)},
243                 {oro, oro_len},
244                 {&oro_refresh, 0},
245                 {cl_id, cl_id_len},
246                 {srv_id, srv_id_len},
247                 {&reconf_accept, 0},
248                 {&fqdn, fqdn_len},
249                 {&hdr_ia_na, sizeof(hdr_ia_na)},
250                 {ia_na, ia_na_len},
251                 {&hdr_ia_pd, sizeof(hdr_ia_pd)},
252                 {ia_pd, ia_pd_len},
253         };
254
255         size_t cnt = ARRAY_SIZE(iov);
256         if (type == DHCPV6_MSG_INFO_REQ) {
257                 cnt = 5;
258                 iov[2].iov_len = sizeof(oro_refresh);
259                 hdr.oro_len = htons(oro_len + sizeof(oro_refresh));
260         } else if (!request_prefix) {
261                 cnt = 9;
262         }
263
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) {
268                 if (ia_na_len == 0)
269                         iov[7].iov_len = 0;
270                 if (ia_pd_len == 0)
271                         iov[9].iov_len = 0;
272         }
273
274         if (na_mode == IA_MODE_NONE)
275                 iov[7].iov_len = 0;
276
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};
280
281         sendmsg(sock, &msg, 0);
282 }
283
284
285 static int64_t dhcpv6_rand_delay(int64_t time)
286 {
287         int random;
288         read(urandom_fd, &random, sizeof(random));
289         return (time * (random % 1000)) / 10000;
290 }
291
292
293 int dhcpv6_request(enum dhcpv6_msg type)
294 {
295         uint8_t buf[1536];
296         uint32_t timeout = UINT32_MAX;
297         struct dhcpv6_retx *retx = &dhcpv6_retx[type];
298
299         if (retx->delay) {
300                 struct timespec ts = {0, 0};
301                 ts.tv_nsec = dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY);
302                 nanosleep(&ts, NULL);
303         }
304
305         if (type == DHCPV6_MSG_RELEASE || type == DHCPV6_MSG_DECLINE)
306                 timeout = 3;
307         else if (type == DHCPV6_MSG_UNKNOWN)
308                 timeout = t1;
309         else if (type == DHCPV6_MSG_RENEW)
310                 timeout = t2 - t1;
311         else if (type == DHCPV6_MSG_REBIND)
312                 timeout = t3 - t2;
313
314         if (timeout == 0)
315                 return -1;
316
317         syslog(LOG_NOTICE, "Sending %s (timeout %us)", retx->name, timeout);
318
319         uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
320
321         // Generate transaction ID
322         uint8_t trid[3];
323         read(urandom_fd, trid, sizeof(trid));
324         ssize_t len = -1;
325         int64_t rto = 0;
326
327         do {
328                 rto = (rto == 0) ? (retx->init_timeo * 1000 +
329                                 dhcpv6_rand_delay(retx->init_timeo * 1000)) :
330                                 (2 * rto + dhcpv6_rand_delay(rto));
331
332                 if (rto >= retx->max_timeo * 1000)
333                         rto = retx->max_timeo * 1000 +
334                                 dhcpv6_rand_delay(retx->max_timeo * 1000);
335
336                 // Calculate end for this round and elapsed time
337                 uint64_t round_end = round_start + rto;
338                 elapsed = round_start - start;
339
340                 // Don't wait too long
341                 if (round_end - start > timeout * 1000)
342                         round_end = timeout * 1000 + start;
343
344                 // Built and send package
345                 if (type != DHCPV6_MSG_UNKNOWN)
346                         dhcpv6_send(type, trid, elapsed / 10);
347
348                 // Receive rounds
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())
353                                 return -1;
354
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));
360
361                         // Receive cycle
362                         len = recv(sock, buf, sizeof(buf), 0);
363
364                         if (!dhcpv6_response_is_valid(buf, len, trid, type))
365                                 len = -1;
366
367                         if (len > 0) {
368                                 uint8_t *opt = &buf[4];
369                                 uint8_t *opt_end = opt + len - 4;
370
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);
375
376                                 if (retx->handler_reply)
377                                         len = retx->handler_reply(type,
378                                                 opt, opt_end, elapsed / 1000);
379                         }
380                 }
381
382                 // Allow
383                 if (retx->handler_finish)
384                         len = retx->handler_finish(elapsed / 1000);
385         } while (len < 0 && elapsed / 1000 < timeout);
386
387         return len;
388 }
389
390
391 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
392                 const uint8_t transaction[3], enum dhcpv6_msg type)
393 {
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
398
399         if (type == DHCPV6_MSG_SOLICIT) {
400                 if (rep->msg_type != DHCPV6_MSG_ADVERT &&
401                                 rep->msg_type != DHCPV6_MSG_REPLY)
402                         return false;
403         } else if (type == DHCPV6_MSG_UNKNOWN) {
404                 if (!accept_reconfig || rep->msg_type != DHCPV6_MSG_RECONF)
405                         return false;
406         } else if (rep->msg_type != DHCPV6_MSG_REPLY) {
407                 return false;
408         }
409
410         uint8_t *end = ((uint8_t*)buf) + len, *odata;
411         uint16_t otype, olen;
412         bool clientid_ok = false, serverid_ok = false;
413
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);
417
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);
425
426         return clientid_ok && (serverid_ok || server_id_len == 0);
427 }
428
429
430 int dhcpv6_poll_reconfigure(void)
431 {
432         int ret = dhcpv6_request(DHCPV6_MSG_UNKNOWN);
433         if (ret != -1)
434                 ret = dhcpv6_request(ret);
435
436         return ret;
437 }
438
439
440 static int dhcpv6_handle_reconfigure(_unused enum dhcpv6_msg orig,
441                 const void *opt, const void *end, uint32_t elapsed)
442 {
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))
450                         msg = odata[0];
451
452         t1 -= elapsed;
453         t2 -= elapsed;
454         t3 -= elapsed;
455
456         if (t1 < 0)
457                 t1 = 0;
458
459         if (t2 < 0)
460                 t2 = 0;
461
462         if (t3 < 0)
463                 t3 = 0;
464
465         dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL, elapsed);
466         return msg;
467 }
468
469
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)
473 {
474         uint16_t olen, otype;
475         uint8_t *odata;
476         struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}};
477
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) {
485                                 return -1;
486                         } else {
487                                 cand.has_noaddravail = true;
488                                 cand.preference -= 1000;
489                         }
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;
508                         }
509                 }
510         }
511
512         if (cand.duid_len > 0)
513                 odhcp6c_add_state(STATE_SERVER_CAND, &cand, sizeof(cand));
514
515         return -1;
516 }
517
518
519 static int dhcpv6_commit_advert(_unused uint32_t elapsed)
520 {
521         size_t cand_len;
522         struct dhcpv6_server_cand *c = NULL, *cand =
523                         odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
524
525         bool retry = false;
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
529
530                 if (!c || c->preference < cand[i].preference)
531                         c = &cand[i];
532         }
533
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);
538         }
539
540         if (c) {
541                 uint16_t hdr[2] = {htons(DHCPV6_OPT_SERVERID),
542                                 htons(c->duid_len)};
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;
546         }
547
548         odhcp6c_clear_state(STATE_SERVER_CAND);
549
550         if (!c)
551                 return -1;
552         else if (request_prefix || na_mode != IA_MODE_NONE)
553                 return DHCPV6_STATEFUL;
554         else
555                 return DHCPV6_STATELESS;
556 }
557
558
559 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig,
560                 const void *opt, const void *end, uint32_t elapsed)
561 {
562         dhcpv6_handle_advert(orig, opt, end, elapsed);
563         if (dhcpv6_commit_advert(elapsed) < 0)
564                 return -1;
565
566         return dhcpv6_handle_reply(orig, opt, end, elapsed);
567 }
568
569
570 static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
571                 const void *opt, const void *end, uint32_t elapsed)
572 {
573         uint16_t otype, olen;
574         uint8_t *odata;
575         bool have_update = false;
576
577         t1 = t2 = t3 = 86400;
578
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);
582         uint8_t *ia_end;
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);
589
590         // Decrease valid and preferred lifetime of prefixes
591         size_t ia_pd_len;
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);
597
598                 uint32_t pref = ntohl(p->preferred);
599                 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
600         }
601
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);
607
608                 uint32_t pref = ntohl(p->preferred);
609                 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
610         }
611
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);
619
620                         // Test ID and T1-T2 validity
621                         if (ia_hdr->iaid != 1 || l_t2 < l_t1)
622                                 continue;
623
624                         uint16_t stype, slen;
625                         uint8_t *sdata;
626                         // Test status and bail if error
627                         dhcpv6_for_each_option(&ia_hdr[1], odata + olen,
628                                         stype, slen, sdata)
629                                 if (stype == DHCPV6_OPT_STATUS && slen >= 2 &&
630                                                 (sdata[0] || sdata[1]))
631                                         continue;
632
633                         // Update times
634                         if (l_t1 > 0 && t1 > l_t1)
635                                 t1 = l_t1;
636
637                         if (l_t2 > 0 && t2 > l_t2)
638                                 t2 = l_t2;
639
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)
643                                 have_update = true;
644
645                         time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
646
647                         if (n < t1)
648                                 t1 = n;
649
650                         if (n < t2)
651                                 t2 = n;
652
653                         if (n < t3)
654                                 t3 = n;
655
656                 } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
657                         if (olen == 16)
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;
663                         uint8_t *sdata;
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,
670                                                         sdata, slen);
671                                 else if (slen > 0 && stype == NTP_SRV_FQDN)
672                                         odhcp6c_add_state(STATE_SNTP_FQDN,
673                                                         sdata, slen);
674                         }
675                 } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
676                         if (olen == 16)
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)
683                                 t1 = refresh;
684                 } else if (otype != DHCPV6_OPT_CLIENTID &&
685                                 otype != DHCPV6_OPT_SERVERID) {
686                         odhcp6c_add_state(STATE_CUSTOM_OPTS,
687                                         &odata[-4], olen + 4);
688                 }
689         }
690
691         if (opt) {
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,
695                                 sntp_ip_len);
696                 have_update |= odhcp6c_commit_state(STATE_SNTP_FQDN,
697                                 sntp_dns_len);
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);
705         }
706
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];
712                 while (!p->valid) {
713                         ia_end = ia_pd + odhcp6c_remove_state(STATE_IA_PD,
714                                         (uint8_t*)p - ia_pd, olen + 4);
715                         have_update = true;
716                 }
717         }
718
719
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];
725                 while (!p->valid) {
726                         ia_end = ia_na + odhcp6c_remove_state(STATE_IA_NA,
727                                         (uint8_t*)p - ia_na, olen + 4);
728                         have_update = true;
729                 }
730         }
731
732         return have_update;
733 }
734
735
736 static time_t dhcpv6_parse_ia(void *opt, void *end)
737 {
738         uint32_t timeout = UINT32_MAX; // Minimum timeout
739         uint16_t otype, olen, stype, slen;
740         uint8_t *odata, *sdata;
741
742         // Update address IA
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))
747                                 continue;
748
749                         olen = sizeof(*prefix); // Normalize length
750                         uint32_t valid = ntohl(prefix->valid);
751                         uint32_t pref = ntohl(prefix->preferred);
752
753                         if (pref > valid)
754                                 continue;
755
756                         // Search matching IA
757                         struct dhcpv6_ia_prefix *local = NULL;
758                         size_t pd_len;
759                         uint8_t *pd = odhcp6c_get_state(STATE_IA_PD, &pd_len);
760                         dhcpv6_for_each_option(pd, pd + pd_len,
761                                         stype, slen, sdata)
762                                 if (!memcmp(sdata + 8, odata + 8,
763                                                 sizeof(local->addr) + 1))
764                                         local = (void*)&sdata[-4];
765
766                         if (local) { // Already know that IA
767                                 local->preferred = prefix->preferred;
768                                 local->valid = prefix->valid;
769                         } else { // New IA
770                                 odhcp6c_add_state(STATE_IA_PD, prefix, olen);
771                         }
772
773                         if (timeout > valid)
774                                 timeout = valid;
775
776                         if (prefix->valid == 0) // We probably lost that prefix
777                                 odhcp6c_add_state(STATE_IA_PD_LOST,
778                                                 prefix, olen);
779                 } else if (otype == DHCPV6_OPT_IA_ADDR) {
780                         struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
781                         if (olen + 4U < sizeof(*addr))
782                                 continue;
783
784                         olen = sizeof(*addr); // Normalize length
785                         uint32_t pref = ntohl(addr->preferred);
786                         uint32_t valid = ntohl(addr->valid);
787
788                         if (pref > valid)
789                                 continue;
790
791                         // Search matching IA
792                         struct dhcpv6_ia_addr *local = NULL;
793                         size_t na_len;
794                         uint8_t *na = odhcp6c_get_state(STATE_IA_NA, &na_len);
795                         dhcpv6_for_each_option(na, na + na_len,
796                                         stype, slen, sdata)
797                                 if (!memcmp(sdata, odata, sizeof(local->addr)))
798                                         local = (void*)&sdata[-4];
799
800
801                         if (local) { // Already know that IA
802                                 local->preferred = addr->preferred;
803                                 local->valid = addr->valid;
804                         } else { // New IA
805                                 odhcp6c_add_state(STATE_IA_NA, addr, olen);
806                         }
807
808
809                         if (timeout > valid)
810                                 timeout = valid;
811
812                         if (set_rtnetlink_addr(ifindex, &addr->addr,
813                                         pref, valid) == -EADDRNOTAVAIL) {
814                                 dhcpv6_request(DHCPV6_MSG_DECLINE);
815                                 raise(SIGUSR2);
816                         }
817                 }
818         }
819
820         return timeout;
821 }