Fix calculation of preferred and valid times
[project/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
114 uint8_t zero[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
115 struct ifreq ifs[100], *ifp, *ifend;
116 struct ifconf ifc;
117 ifc.ifc_req = ifs;
118 ifc.ifc_len = sizeof(ifs);
119
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,
130 ETHER_ADDR_LEN);
131 }
132 }
133
134 odhcp6c_add_state(STATE_CLIENT_ID, duid, sizeof(duid));
135 }
136
137 // Create ORO
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));
144
145
146 // Configure IPv6-options
147 int val = 1;
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));
151
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)))
155 return -1;
156
157 return 0;
158 }
159
160
161 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode)
162 {
163 na_mode = mode;
164 }
165
166
167 void dhcpv6_remove_addrs(void)
168 {
169 size_t ia_na_len;
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);
175 }
176 }
177
178
179 static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
180 {
181 // Build FQDN
182 char fqdn_buf[256];
183 gethostname(fqdn_buf, sizeof(fqdn_buf));
184 struct {
185 uint16_t type;
186 uint16_t len;
187 uint8_t flags;
188 uint8_t data[256];
189 } fqdn;
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);
194 fqdn.flags = 0;
195
196
197 // Build Client ID
198 size_t cl_id_len;
199 void *cl_id = odhcp6c_get_state(STATE_CLIENT_ID, &cl_id_len);
200
201 // Get Server ID
202 size_t srv_id_len;
203 void *srv_id = odhcp6c_get_state(STATE_SERVER_ID, &srv_id_len);
204
205 // Build IA_PDs
206 size_t ia_pd_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),
211 1, 0, 0
212 };
213
214 struct dhcpv6_ia_prefix pref = {
215 .type = htons(DHCPV6_OPT_IA_PREFIX),
216 .len = htons(25), .prefix = request_prefix
217 };
218
219 if (ia_pd_len == 0 && request_prefix > 0 &&
220 (type == DHCPV6_MSG_SOLICIT ||
221 type == DHCPV6_MSG_REQUEST)) {
222 ia_pd = &pref;
223 ia_pd_len = sizeof(pref);
224 }
225
226 // Build IA_NAs
227 size_t ia_na_len;
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),
232 1, 0, 0
233 };
234
235 // Reconfigure Accept
236 struct {
237 uint16_t type;
238 uint16_t length;
239 } reconf_accept = {htons(DHCPV6_OPT_RECONF_ACCEPT), 0};
240
241 // Request Information Refresh
242 uint16_t oro_refresh = htons(DHCPV6_OPT_INFO_REFRESH);
243
244 // Prepare Header
245 size_t oro_len;
246 void *oro = odhcp6c_get_state(STATE_ORO, &oro_len);
247 struct {
248 uint8_t type;
249 uint8_t trid[3];
250 uint16_t elapsed_type;
251 uint16_t elapsed_len;
252 uint16_t elapsed_value;
253 uint16_t oro_type;
254 uint16_t oro_len;
255 } hdr = {
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),
260 };
261
262 struct iovec iov[] = {
263 {&hdr, sizeof(hdr)},
264 {oro, oro_len},
265 {&oro_refresh, 0},
266 {cl_id, cl_id_len},
267 {srv_id, srv_id_len},
268 {&reconf_accept, 0},
269 {&fqdn, fqdn_len},
270 {&hdr_ia_na, sizeof(hdr_ia_na)},
271 {ia_na, ia_na_len},
272 {&hdr_ia_pd, sizeof(hdr_ia_pd)},
273 {ia_pd, ia_pd_len},
274 };
275
276 size_t cnt = ARRAY_SIZE(iov);
277 if (type == DHCPV6_MSG_INFO_REQ) {
278 cnt = 5;
279 iov[2].iov_len = sizeof(oro_refresh);
280 hdr.oro_len = htons(oro_len + sizeof(oro_refresh));
281 } else if (!request_prefix) {
282 cnt = 9;
283 }
284
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) {
289 if (ia_na_len == 0)
290 iov[7].iov_len = 0;
291 if (ia_pd_len == 0)
292 iov[9].iov_len = 0;
293 }
294
295 if (na_mode == IA_MODE_NONE)
296 iov[7].iov_len = 0;
297
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};
301
302 sendmsg(sock, &msg, 0);
303 }
304
305
306 static int64_t dhcpv6_rand_delay(int64_t time)
307 {
308 int random;
309 read(urandom_fd, &random, sizeof(random));
310 return (time * (random % 1000)) / 10000;
311 }
312
313
314 int dhcpv6_request(enum dhcpv6_msg type)
315 {
316 uint8_t buf[1536];
317 uint32_t timeout = UINT32_MAX;
318 struct dhcpv6_retx *retx = &dhcpv6_retx[type];
319
320 if (retx->delay) {
321 struct timespec ts = {0, 0};
322 ts.tv_nsec = dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY);
323 nanosleep(&ts, NULL);
324 }
325
326 if (type == DHCPV6_MSG_RELEASE || type == DHCPV6_MSG_DECLINE)
327 timeout = 3;
328 else if (type == DHCPV6_MSG_UNKNOWN)
329 timeout = t1;
330 else if (type == DHCPV6_MSG_RENEW)
331 timeout = t2 - t1;
332 else if (type == DHCPV6_MSG_REBIND)
333 timeout = t3 - t2;
334
335 if (timeout == 0)
336 return -1;
337
338 syslog(LOG_NOTICE, "Sending %s (timeout %us)", retx->name, timeout);
339
340 uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
341
342 // Generate transaction ID
343 uint8_t trid[3];
344 read(urandom_fd, trid, sizeof(trid));
345 ssize_t len = -1;
346 int64_t rto = 0;
347
348 do {
349 rto = (rto == 0) ? (retx->init_timeo * 1000 +
350 dhcpv6_rand_delay(retx->init_timeo * 1000)) :
351 (2 * rto + dhcpv6_rand_delay(rto));
352
353 if (rto >= retx->max_timeo * 1000)
354 rto = retx->max_timeo * 1000 +
355 dhcpv6_rand_delay(retx->max_timeo * 1000);
356
357 // Calculate end for this round and elapsed time
358 uint64_t round_end = round_start + rto;
359 elapsed = round_start - start;
360
361 // Don't wait too long
362 if (round_end - start > timeout * 1000)
363 round_end = timeout * 1000 + start;
364
365 // Built and send package
366 if (type != DHCPV6_MSG_UNKNOWN)
367 dhcpv6_send(type, trid, elapsed / 10);
368
369 // Receive rounds
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())
374 return -1;
375
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));
381
382 // Receive cycle
383 len = recv(sock, buf, sizeof(buf), 0);
384
385 if (!dhcpv6_response_is_valid(buf, len, trid, type))
386 len = -1;
387
388 if (len > 0) {
389 uint8_t *opt = &buf[4];
390 uint8_t *opt_end = opt + len - 4;
391
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);
396
397 if (retx->handler_reply)
398 len = retx->handler_reply(type,
399 opt, opt_end, elapsed / 1000);
400 }
401 }
402
403 // Allow
404 if (retx->handler_finish)
405 len = retx->handler_finish(elapsed / 1000);
406 } while (len < 0 && elapsed / 1000 < timeout);
407
408 return len;
409 }
410
411
412 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
413 const uint8_t transaction[3], enum dhcpv6_msg type)
414 {
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
419
420 if (type == DHCPV6_MSG_SOLICIT) {
421 if (rep->msg_type != DHCPV6_MSG_ADVERT &&
422 rep->msg_type != DHCPV6_MSG_REPLY)
423 return false;
424 } else if (type == DHCPV6_MSG_UNKNOWN) {
425 if (!accept_reconfig || rep->msg_type != DHCPV6_MSG_RECONF)
426 return false;
427 } else if (rep->msg_type != DHCPV6_MSG_REPLY) {
428 return false;
429 }
430
431 uint8_t *end = ((uint8_t*)buf) + len, *odata;
432 uint16_t otype, olen;
433 bool clientid_ok = false, serverid_ok = false;
434
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);
438
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);
446
447 return clientid_ok && (serverid_ok || server_id_len == 0);
448 }
449
450
451 int dhcpv6_poll_reconfigure(void)
452 {
453 int ret = dhcpv6_request(DHCPV6_MSG_UNKNOWN);
454 if (ret != -1)
455 ret = dhcpv6_request(ret);
456
457 return ret;
458 }
459
460
461 static int dhcpv6_handle_reconfigure(_unused enum dhcpv6_msg orig,
462 const void *opt, const void *end, uint32_t elapsed)
463 {
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))
471 msg = odata[0];
472
473 t1 -= elapsed;
474 t2 -= elapsed;
475 t3 -= elapsed;
476
477 if (t1 < 0)
478 t1 = 0;
479
480 if (t2 < 0)
481 t2 = 0;
482
483 if (t3 < 0)
484 t3 = 0;
485
486 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL, elapsed);
487 return msg;
488 }
489
490
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)
494 {
495 uint16_t olen, otype;
496 uint8_t *odata;
497 struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}};
498
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) {
506 return -1;
507 } else {
508 cand.has_noaddravail = true;
509 cand.preference -= 1000;
510 }
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;
529 }
530 }
531 }
532
533 if (cand.duid_len > 0)
534 odhcp6c_add_state(STATE_SERVER_CAND, &cand, sizeof(cand));
535
536 return -1;
537 }
538
539
540 static int dhcpv6_commit_advert(_unused uint32_t elapsed)
541 {
542 size_t cand_len;
543 struct dhcpv6_server_cand *c = NULL, *cand =
544 odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
545
546 bool retry = false;
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
550
551 if (!c || c->preference < cand[i].preference)
552 c = &cand[i];
553 }
554
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);
559 }
560
561 if (c) {
562 uint16_t hdr[2] = {htons(DHCPV6_OPT_SERVERID),
563 htons(c->duid_len)};
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;
567 }
568
569 odhcp6c_clear_state(STATE_SERVER_CAND);
570
571 if (!c)
572 return -1;
573 else if (request_prefix || na_mode != IA_MODE_NONE)
574 return DHCPV6_STATEFUL;
575 else
576 return DHCPV6_STATELESS;
577 }
578
579
580 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig,
581 const void *opt, const void *end, uint32_t elapsed)
582 {
583 dhcpv6_handle_advert(orig, opt, end, elapsed);
584 if (dhcpv6_commit_advert(elapsed) < 0)
585 return -1;
586
587 return dhcpv6_handle_reply(orig, opt, end, elapsed);
588 }
589
590
591 static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
592 const void *opt, const void *end, uint32_t elapsed)
593 {
594 uint16_t otype, olen;
595 uint8_t *odata;
596 bool have_update = false;
597
598 t1 = t2 = t3 = 86400;
599
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);
603 uint8_t *ia_end;
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);
610
611 // Decrease valid and preferred lifetime of prefixes
612 size_t ia_pd_len;
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 if (valid != UINT32_MAX)
618 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
619
620 uint32_t pref = ntohl(p->preferred);
621 if (pref != UINT32_MAX)
622 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
623 }
624
625 // Decrease valid and preferred lifetime of addresses
626 dhcpv6_for_each_option(ia_na, ia_na + ia_na_len, otype, olen, odata) {
627 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
628 uint32_t valid = ntohl(p->valid);
629 if (valid != UINT32_MAX)
630 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
631
632 uint32_t pref = ntohl(p->preferred);
633 if (pref != UINT32_MAX)
634 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
635 }
636
637 // Parse and find all matching IAs
638 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
639 if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)
640 && olen > sizeof(struct dhcpv6_ia_hdr)) {
641 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
642 time_t l_t1 = ntohl(ia_hdr->t1);
643 time_t l_t2 = ntohl(ia_hdr->t2);
644
645 // Test ID and T1-T2 validity
646 if (ia_hdr->iaid != 1 || l_t2 < l_t1)
647 continue;
648
649 uint16_t stype, slen;
650 uint8_t *sdata;
651 // Test status and bail if error
652 dhcpv6_for_each_option(&ia_hdr[1], odata + olen,
653 stype, slen, sdata)
654 if (stype == DHCPV6_OPT_STATUS && slen >= 2 &&
655 (sdata[0] || sdata[1]))
656 continue;
657
658 // Update times
659 if (l_t1 > 0 && t1 > l_t1)
660 t1 = l_t1;
661
662 if (l_t2 > 0 && t2 > l_t2)
663 t2 = l_t2;
664
665 // Always report update in case we have IA_PDs so that
666 // the state-script is called with updated times
667 if (otype == DHCPV6_OPT_IA_PD && request_prefix)
668 have_update = true;
669
670 time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
671
672 if (n < t1)
673 t1 = n;
674
675 if (n < t2)
676 t2 = n;
677
678 if (n < t3)
679 t3 = n;
680
681 } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
682 if (olen % 16 == 0)
683 odhcp6c_add_state(STATE_DNS, odata, olen);
684 } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
685 odhcp6c_add_state(STATE_SEARCH, odata, olen);
686 } else if (otype == DHCPV6_OPT_NTP_SERVER) {
687 uint16_t stype, slen;
688 uint8_t *sdata;
689 // Test status and bail if error
690 dhcpv6_for_each_option(odata, odata + olen,
691 stype, slen, sdata) {
692 if (slen == 16 && (stype == NTP_MC_ADDR ||
693 stype == NTP_SRV_ADDR))
694 odhcp6c_add_state(STATE_SNTP_IP,
695 sdata, slen);
696 else if (slen > 0 && stype == NTP_SRV_FQDN)
697 odhcp6c_add_state(STATE_SNTP_FQDN,
698 sdata, slen);
699 }
700 } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
701 if (olen == 16)
702 odhcp6c_add_state(STATE_SIP_IP, odata, olen);
703 } else if (otype == DHCPV6_OPT_SIP_SERVER_D) {
704 odhcp6c_add_state(STATE_SIP_FQDN, odata, olen);
705 } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
706 uint32_t refresh = ntohl(*((uint32_t*)odata));
707 if (refresh < (uint32_t)t1)
708 t1 = refresh;
709 } else if (otype != DHCPV6_OPT_CLIENTID &&
710 otype != DHCPV6_OPT_SERVERID) {
711 odhcp6c_add_state(STATE_CUSTOM_OPTS,
712 &odata[-4], olen + 4);
713 }
714 }
715
716 if (opt) {
717 have_update |= odhcp6c_commit_state(STATE_DNS, dns_len);
718 have_update |= odhcp6c_commit_state(STATE_SEARCH, search_len);
719 have_update |= odhcp6c_commit_state(STATE_SNTP_IP,
720 sntp_ip_len);
721 have_update |= odhcp6c_commit_state(STATE_SNTP_FQDN,
722 sntp_dns_len);
723 have_update |= odhcp6c_commit_state(STATE_SIP_IP, sip_ip_len);
724 have_update |= odhcp6c_commit_state(STATE_SIP_FQDN, sip_fqdn_len);
725 size_t new_ia_pd_len, new_ia_na_len;
726 odhcp6c_get_state(STATE_IA_PD, &new_ia_pd_len);
727 odhcp6c_get_state(STATE_IA_NA, &new_ia_na_len);
728 have_update |= (new_ia_pd_len != ia_pd_len) ||
729 (new_ia_na_len != ia_na_len);
730 }
731
732 // Delete prefixes with 0 valid-time
733 ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
734 ia_end = ia_pd + ia_pd_len;
735 dhcpv6_for_each_option(ia_pd, ia_end, otype, olen, odata) {
736 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
737 while (!p->valid) {
738 ia_end = ia_pd + odhcp6c_remove_state(STATE_IA_PD,
739 (uint8_t*)p - ia_pd, olen + 4);
740 have_update = true;
741 }
742 }
743
744
745 // Delete addresses with 0 valid-time
746 ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
747 ia_end = ia_na + ia_na_len;
748 dhcpv6_for_each_option(ia_na, ia_end, otype, olen, odata) {
749 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
750 while (!p->valid) {
751 ia_end = ia_na + odhcp6c_remove_state(STATE_IA_NA,
752 (uint8_t*)p - ia_na, olen + 4);
753 have_update = true;
754 }
755 }
756
757 return have_update;
758 }
759
760
761 static time_t dhcpv6_parse_ia(void *opt, void *end)
762 {
763 uint32_t timeout = UINT32_MAX; // Minimum timeout
764 uint16_t otype, olen, stype, slen;
765 uint8_t *odata, *sdata;
766
767 // Update address IA
768 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
769 if (otype == DHCPV6_OPT_IA_PREFIX) {
770 struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
771 if (olen + 4U < sizeof(*prefix))
772 continue;
773
774 olen = sizeof(*prefix); // Normalize length
775 uint32_t valid = ntohl(prefix->valid);
776 uint32_t pref = ntohl(prefix->preferred);
777
778 if (pref > valid)
779 continue;
780
781 // Search matching IA
782 struct dhcpv6_ia_prefix *local = NULL;
783 size_t pd_len;
784 uint8_t *pd = odhcp6c_get_state(STATE_IA_PD, &pd_len);
785 dhcpv6_for_each_option(pd, pd + pd_len,
786 stype, slen, sdata)
787 if (!memcmp(sdata + 8, odata + 8,
788 sizeof(local->addr) + 1))
789 local = (void*)&sdata[-4];
790
791 if (local) { // Already know that IA
792 local->preferred = prefix->preferred;
793 local->valid = prefix->valid;
794 } else { // New IA
795 odhcp6c_add_state(STATE_IA_PD, prefix, olen);
796 }
797
798 if (timeout > valid)
799 timeout = valid;
800 } else if (otype == DHCPV6_OPT_IA_ADDR) {
801 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
802 if (olen + 4U < sizeof(*addr))
803 continue;
804
805 olen = sizeof(*addr); // Normalize length
806 uint32_t pref = ntohl(addr->preferred);
807 uint32_t valid = ntohl(addr->valid);
808
809 if (pref > valid)
810 continue;
811
812 // Search matching IA
813 struct dhcpv6_ia_addr *local = NULL;
814 size_t na_len;
815 uint8_t *na = odhcp6c_get_state(STATE_IA_NA, &na_len);
816 dhcpv6_for_each_option(na, na + na_len,
817 stype, slen, sdata)
818 if (!memcmp(sdata, odata, sizeof(local->addr)))
819 local = (void*)&sdata[-4];
820
821
822 if (local) { // Already know that IA
823 local->preferred = addr->preferred;
824 local->valid = addr->valid;
825 } else { // New IA
826 odhcp6c_add_state(STATE_IA_NA, addr, olen);
827 }
828
829
830 if (timeout > valid)
831 timeout = valid;
832
833 if (set_rtnetlink_addr(ifindex, &addr->addr,
834 pref, valid) == -EADDRNOTAVAIL) {
835 dhcpv6_request(DHCPV6_MSG_DECLINE);
836 raise(SIGUSR2);
837 }
838 }
839 }
840
841 return timeout;
842 }