9a451d0568232896c084ed33d3138f46a35f5e59
[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 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 odhcp6c_add_state(STATE_ORO, oro, sizeof(oro));
120
121
122 // Configure IPv6-options
123 int val = 1;
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));
127
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)))
131 return -1;
132
133 return 0;
134 }
135
136
137 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode)
138 {
139 na_mode = mode;
140 }
141
142
143 void dhcpv6_remove_addrs(void)
144 {
145 size_t ia_na_len;
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);
151 }
152 }
153
154
155 static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
156 {
157 // Build FQDN
158 char fqdn_buf[256];
159 gethostname(fqdn_buf, sizeof(fqdn_buf));
160 struct {
161 uint16_t type;
162 uint16_t len;
163 uint8_t flags;
164 uint8_t data[256];
165 } fqdn;
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);
170 fqdn.flags = 0;
171
172
173 // Build Client ID
174 size_t cl_id_len;
175 void *cl_id = odhcp6c_get_state(STATE_CLIENT_ID, &cl_id_len);
176
177 // Get Server ID
178 size_t srv_id_len;
179 void *srv_id = odhcp6c_get_state(STATE_SERVER_ID, &srv_id_len);
180
181 // Build IA_PDs
182 size_t ia_pd_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),
187 1, 0, 0
188 };
189
190 struct dhcpv6_ia_prefix pref = {
191 .type = htons(DHCPV6_OPT_IA_PREFIX),
192 .len = htons(25), .prefix = request_prefix
193 };
194
195 if (ia_pd_len == 0 && request_prefix > 0 &&
196 (type == DHCPV6_MSG_SOLICIT ||
197 type == DHCPV6_MSG_REQUEST)) {
198 ia_pd = &pref;
199 ia_pd_len = sizeof(pref);
200 }
201
202 // Build IA_NAs
203 size_t ia_na_len;
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),
208 1, 0, 0
209 };
210
211 // Reconfigure Accept
212 struct {
213 uint16_t type;
214 uint16_t length;
215 } reconf_accept = {htons(DHCPV6_OPT_RECONF_ACCEPT), 0};
216
217 // Request Information Refresh
218 uint16_t oro_refresh = htons(DHCPV6_OPT_INFO_REFRESH);
219
220 // Prepare Header
221 size_t oro_len;
222 void *oro = odhcp6c_get_state(STATE_ORO, &oro_len);
223 struct {
224 uint8_t type;
225 uint8_t trid[3];
226 uint16_t elapsed_type;
227 uint16_t elapsed_len;
228 uint16_t elapsed_value;
229 uint16_t oro_type;
230 uint16_t oro_len;
231 } hdr = {
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),
236 };
237
238 struct iovec iov[] = {
239 {&hdr, sizeof(hdr)},
240 {oro, oro_len},
241 {&oro_refresh, 0},
242 {cl_id, cl_id_len},
243 {srv_id, srv_id_len},
244 {&reconf_accept, 0},
245 {&fqdn, fqdn_len},
246 {&hdr_ia_na, sizeof(hdr_ia_na)},
247 {ia_na, ia_na_len},
248 {&hdr_ia_pd, sizeof(hdr_ia_pd)},
249 {ia_pd, ia_pd_len},
250 };
251
252 size_t cnt = ARRAY_SIZE(iov);
253 if (type == DHCPV6_MSG_INFO_REQ) {
254 cnt = 5;
255 iov[2].iov_len = sizeof(oro_refresh);
256 hdr.oro_len = htons(oro_len + sizeof(oro_refresh));
257 } else if (!request_prefix) {
258 cnt = 9;
259 }
260
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) {
265 if (ia_na_len == 0)
266 iov[7].iov_len = 0;
267 if (ia_pd_len == 0)
268 iov[9].iov_len = 0;
269 }
270
271 if (na_mode == IA_MODE_NONE)
272 iov[7].iov_len = 0;
273
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};
277
278 sendmsg(sock, &msg, 0);
279 }
280
281
282 static int64_t dhcpv6_rand_delay(int64_t time)
283 {
284 int random;
285 read(urandom_fd, &random, sizeof(random));
286 return (time * (random % 1000)) / 10000;
287 }
288
289
290 int dhcpv6_request(enum dhcpv6_msg type)
291 {
292 uint8_t buf[1536];
293 uint32_t timeout = UINT32_MAX;
294 struct dhcpv6_retx *retx = &dhcpv6_retx[type];
295
296 if (retx->delay) {
297 struct timespec ts = {0, 0};
298 ts.tv_nsec = dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY);
299 nanosleep(&ts, NULL);
300 }
301
302 if (type == DHCPV6_MSG_RELEASE || type == DHCPV6_MSG_DECLINE)
303 timeout = 3;
304 else if (type == DHCPV6_MSG_UNKNOWN)
305 timeout = t1;
306 else if (type == DHCPV6_MSG_RENEW)
307 timeout = t2 - t1;
308 else if (type == DHCPV6_MSG_REBIND)
309 timeout = t3 - t2;
310
311 if (timeout == 0)
312 return -1;
313
314 syslog(LOG_NOTICE, "Sending %s (timeout %us)", retx->name, timeout);
315
316 uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
317
318 // Generate transaction ID
319 uint8_t trid[3];
320 read(urandom_fd, trid, sizeof(trid));
321 ssize_t len = -1;
322 int64_t rto = 0;
323
324 do {
325 rto = (rto == 0) ? (retx->init_timeo * 1000 +
326 dhcpv6_rand_delay(retx->init_timeo * 1000)) :
327 (2 * rto + dhcpv6_rand_delay(rto));
328
329 if (rto >= retx->max_timeo * 1000)
330 rto = retx->max_timeo * 1000 +
331 dhcpv6_rand_delay(retx->max_timeo * 1000);
332
333 // Calculate end for this round and elapsed time
334 uint64_t round_end = round_start + rto;
335 elapsed = round_start - start;
336
337 // Don't wait too long
338 if (round_end - start > timeout * 1000)
339 round_end = timeout * 1000 + start;
340
341 // Built and send package
342 if (type != DHCPV6_MSG_UNKNOWN)
343 dhcpv6_send(type, trid, elapsed / 10);
344
345 // Receive rounds
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())
350 return -1;
351
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));
357
358 // Receive cycle
359 len = recv(sock, buf, sizeof(buf), 0);
360
361 if (!dhcpv6_response_is_valid(buf, len, trid, type))
362 len = -1;
363
364 if (len > 0) {
365 uint8_t *opt = &buf[4];
366 uint8_t *opt_end = opt + len - 4;
367
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);
372
373 if (retx->handler_reply)
374 len = retx->handler_reply(type,
375 opt, opt_end, elapsed / 1000);
376 }
377 }
378
379 // Allow
380 if (retx->handler_finish)
381 len = retx->handler_finish(elapsed / 1000);
382 } while (len < 0 && elapsed / 1000 < timeout);
383
384 return len;
385 }
386
387
388 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
389 const uint8_t transaction[3], enum dhcpv6_msg type)
390 {
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
395
396 if (type == DHCPV6_MSG_SOLICIT) {
397 if (rep->msg_type != DHCPV6_MSG_ADVERT &&
398 rep->msg_type != DHCPV6_MSG_REPLY)
399 return false;
400 } else if (type == DHCPV6_MSG_UNKNOWN) {
401 if (!accept_reconfig || rep->msg_type != DHCPV6_MSG_RECONF)
402 return false;
403 } else if (rep->msg_type != DHCPV6_MSG_REPLY) {
404 return false;
405 }
406
407 uint8_t *end = ((uint8_t*)buf) + len, *odata;
408 uint16_t otype, olen;
409 bool clientid_ok = false, serverid_ok = false;
410
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);
414
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);
422
423 return clientid_ok && (serverid_ok || server_id_len == 0);
424 }
425
426
427 int dhcpv6_poll_reconfigure(void)
428 {
429 int ret = dhcpv6_request(DHCPV6_MSG_UNKNOWN);
430 if (ret != -1)
431 ret = dhcpv6_request(ret);
432
433 return ret;
434 }
435
436
437 static int dhcpv6_handle_reconfigure(_unused enum dhcpv6_msg orig,
438 const void *opt, const void *end, uint32_t elapsed)
439 {
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))
447 msg = odata[0];
448
449 t1 -= elapsed;
450 t2 -= elapsed;
451 t3 -= elapsed;
452
453 if (t1 < 0)
454 t1 = 0;
455
456 if (t2 < 0)
457 t2 = 0;
458
459 if (t3 < 0)
460 t3 = 0;
461
462 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL, elapsed);
463 return msg;
464 }
465
466
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)
470 {
471 uint16_t olen, otype;
472 uint8_t *odata;
473 struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}};
474
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) {
482 return -1;
483 } else {
484 cand.has_noaddravail = true;
485 cand.preference -= 1000;
486 }
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;
505 }
506 }
507 }
508
509 if (cand.duid_len > 0)
510 odhcp6c_add_state(STATE_SERVER_CAND, &cand, sizeof(cand));
511
512 return -1;
513 }
514
515
516 static int dhcpv6_commit_advert(_unused uint32_t elapsed)
517 {
518 size_t cand_len;
519 struct dhcpv6_server_cand *c = NULL, *cand =
520 odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
521
522 bool retry = false;
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
526
527 if (!c || c->preference < cand[i].preference)
528 c = &cand[i];
529 }
530
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);
535 }
536
537 if (c) {
538 uint16_t hdr[2] = {htons(DHCPV6_OPT_SERVERID),
539 htons(c->duid_len)};
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;
543 }
544
545 odhcp6c_clear_state(STATE_SERVER_CAND);
546
547 if (!c)
548 return -1;
549 else if (request_prefix || na_mode != IA_MODE_NONE)
550 return DHCPV6_STATEFUL;
551 else
552 return DHCPV6_STATELESS;
553 }
554
555
556 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig,
557 const void *opt, const void *end, uint32_t elapsed)
558 {
559 dhcpv6_handle_advert(orig, opt, end, elapsed);
560 if (dhcpv6_commit_advert(elapsed) < 0)
561 return -1;
562
563 return dhcpv6_handle_reply(orig, opt, end, elapsed);
564 }
565
566
567 static int dhcpv6_handle_reply(_unused enum dhcpv6_msg orig,
568 const void *opt, const void *end, uint32_t elapsed)
569 {
570 uint16_t otype, olen;
571 uint8_t *odata;
572 bool have_update = false;
573
574 t1 = t2 = t3 = 86400;
575
576 size_t ia_na_len, dns_len, search_len, sntp_ip_len, sntp_dns_len;
577 size_t sip_ip_len, sip_fqdn_len;
578 uint8_t *ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
579 uint8_t *ia_end;
580 odhcp6c_get_state(STATE_DNS, &dns_len);
581 odhcp6c_get_state(STATE_SEARCH, &search_len);
582 odhcp6c_get_state(STATE_SNTP_IP, &sntp_ip_len);
583 odhcp6c_get_state(STATE_SNTP_FQDN, &sntp_dns_len);
584 odhcp6c_get_state(STATE_SIP_IP, &sip_ip_len);
585 odhcp6c_get_state(STATE_SIP_FQDN, &sip_fqdn_len);
586
587 // Decrease valid and preferred lifetime of prefixes
588 size_t ia_pd_len;
589 uint8_t *ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
590 dhcpv6_for_each_option(ia_pd, ia_pd + ia_pd_len, otype, olen, odata) {
591 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
592 uint32_t valid = ntohl(p->valid);
593 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
594
595 uint32_t pref = ntohl(p->preferred);
596 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
597 }
598
599 // Decrease valid and preferred lifetime of addresses
600 dhcpv6_for_each_option(ia_na, ia_na + ia_na_len, otype, olen, odata) {
601 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
602 uint32_t valid = ntohl(p->valid);
603 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
604
605 uint32_t pref = ntohl(p->preferred);
606 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
607 }
608
609 // Parse and find all matching IAs
610 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
611 if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)
612 && olen > sizeof(struct dhcpv6_ia_hdr)) {
613 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
614 time_t l_t1 = ntohl(ia_hdr->t1);
615 time_t l_t2 = ntohl(ia_hdr->t2);
616
617 // Test ID and T1-T2 validity
618 if (ia_hdr->iaid != 1 || l_t2 < l_t1)
619 continue;
620
621 uint16_t stype, slen;
622 uint8_t *sdata;
623 // Test status and bail if error
624 dhcpv6_for_each_option(&ia_hdr[1], odata + olen,
625 stype, slen, sdata)
626 if (stype == DHCPV6_OPT_STATUS && slen >= 2 &&
627 (sdata[0] || sdata[1]))
628 continue;
629
630 // Update times
631 if (l_t1 > 0 && t1 > l_t1)
632 t1 = l_t1;
633
634 if (l_t2 > 0 && t2 > l_t2)
635 t2 = l_t2;
636
637 // Always report update in case we have IA_PDs so that
638 // the state-script is called with updated times
639 if (otype == DHCPV6_OPT_IA_PD && request_prefix)
640 have_update = true;
641
642 time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
643
644 if (n < t1)
645 t1 = n;
646
647 if (n < t2)
648 t2 = n;
649
650 if (n < t3)
651 t3 = n;
652
653 } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
654 if (olen == 16)
655 odhcp6c_add_state(STATE_DNS, odata, olen);
656 } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
657 odhcp6c_add_state(STATE_SEARCH, odata, olen);
658 } else if (otype == DHCPV6_OPT_NTP_SERVER) {
659 uint16_t stype, slen;
660 uint8_t *sdata;
661 // Test status and bail if error
662 dhcpv6_for_each_option(odata, odata + olen,
663 stype, slen, sdata) {
664 if (slen == 16 && (stype == NTP_MC_ADDR ||
665 stype == NTP_SRV_ADDR))
666 odhcp6c_add_state(STATE_SNTP_IP,
667 sdata, slen);
668 else if (slen > 0 && stype == NTP_SRV_FQDN)
669 odhcp6c_add_state(STATE_SNTP_FQDN,
670 sdata, slen);
671 }
672 } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
673 if (olen == 16)
674 odhcp6c_add_state(STATE_SIP_IP, odata, olen);
675 } else if (otype == DHCPV6_OPT_SIP_SERVER_D) {
676 odhcp6c_add_state(STATE_SIP_FQDN, odata, olen);
677 } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
678 uint32_t refresh = ntohl(*((uint32_t*)odata));
679 if (refresh < (uint32_t)t1)
680 t1 = refresh;
681 } else if (otype != DHCPV6_OPT_CLIENTID &&
682 otype != DHCPV6_OPT_SERVERID) {
683 odhcp6c_add_state(STATE_CUSTOM_OPTS,
684 &odata[-4], olen + 4);
685 }
686 }
687
688 if (opt) {
689 have_update |= odhcp6c_commit_state(STATE_DNS, dns_len);
690 have_update |= odhcp6c_commit_state(STATE_SEARCH, search_len);
691 have_update |= odhcp6c_commit_state(STATE_SNTP_IP,
692 sntp_ip_len);
693 have_update |= odhcp6c_commit_state(STATE_SNTP_FQDN,
694 sntp_dns_len);
695 have_update |= odhcp6c_commit_state(STATE_SIP_IP, sip_ip_len);
696 have_update |= odhcp6c_commit_state(STATE_SIP_FQDN, sip_fqdn_len);
697 size_t new_ia_pd_len, new_ia_na_len;
698 odhcp6c_get_state(STATE_IA_PD, &new_ia_pd_len);
699 odhcp6c_get_state(STATE_IA_NA, &new_ia_na_len);
700 have_update |= (new_ia_pd_len != ia_pd_len) ||
701 (new_ia_na_len != ia_na_len);
702 }
703
704 // Delete prefixes with 0 valid-time
705 ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
706 ia_end = ia_pd + ia_pd_len;
707 dhcpv6_for_each_option(ia_pd, ia_end, otype, olen, odata) {
708 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
709 while (!p->valid) {
710 ia_end = ia_pd + odhcp6c_remove_state(STATE_IA_PD,
711 (uint8_t*)p - ia_pd, olen + 4);
712 have_update = true;
713 }
714 }
715
716
717 // Delete addresses with 0 valid-time
718 ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
719 ia_end = ia_na + ia_na_len;
720 dhcpv6_for_each_option(ia_na, ia_end, otype, olen, odata) {
721 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
722 while (!p->valid) {
723 ia_end = ia_na + odhcp6c_remove_state(STATE_IA_NA,
724 (uint8_t*)p - ia_na, olen + 4);
725 have_update = true;
726 }
727 }
728
729 return have_update;
730 }
731
732
733 static time_t dhcpv6_parse_ia(void *opt, void *end)
734 {
735 uint32_t timeout = UINT32_MAX; // Minimum timeout
736 uint16_t otype, olen, stype, slen;
737 uint8_t *odata, *sdata;
738
739 // Update address IA
740 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
741 if (otype == DHCPV6_OPT_IA_PREFIX) {
742 struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
743 if (olen + 4U < sizeof(*prefix))
744 continue;
745
746 olen = sizeof(*prefix); // Normalize length
747 uint32_t valid = ntohl(prefix->valid);
748 uint32_t pref = ntohl(prefix->preferred);
749
750 if (pref > valid)
751 continue;
752
753 // Search matching IA
754 struct dhcpv6_ia_prefix *local = NULL;
755 size_t pd_len;
756 uint8_t *pd = odhcp6c_get_state(STATE_IA_PD, &pd_len);
757 dhcpv6_for_each_option(pd, pd + pd_len,
758 stype, slen, sdata)
759 if (!memcmp(sdata + 8, odata + 8,
760 sizeof(local->addr) + 1))
761 local = (void*)&sdata[-4];
762
763 if (local) { // Already know that IA
764 local->preferred = prefix->preferred;
765 local->valid = prefix->valid;
766 } else { // New IA
767 odhcp6c_add_state(STATE_IA_PD, prefix, olen);
768 }
769
770 if (timeout > valid)
771 timeout = valid;
772
773 if (prefix->valid == 0) // We probably lost that prefix
774 odhcp6c_add_state(STATE_IA_PD_LOST,
775 prefix, olen);
776 } else if (otype == DHCPV6_OPT_IA_ADDR) {
777 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
778 if (olen + 4U < sizeof(*addr))
779 continue;
780
781 olen = sizeof(*addr); // Normalize length
782 uint32_t pref = ntohl(addr->preferred);
783 uint32_t valid = ntohl(addr->valid);
784
785 if (pref > valid)
786 continue;
787
788 // Search matching IA
789 struct dhcpv6_ia_addr *local = NULL;
790 size_t na_len;
791 uint8_t *na = odhcp6c_get_state(STATE_IA_NA, &na_len);
792 dhcpv6_for_each_option(na, na + na_len,
793 stype, slen, sdata)
794 if (!memcmp(sdata, odata, sizeof(local->addr)))
795 local = (void*)&sdata[-4];
796
797
798 if (local) { // Already know that IA
799 local->preferred = addr->preferred;
800 local->valid = addr->valid;
801 } else { // New IA
802 odhcp6c_add_state(STATE_IA_NA, addr, olen);
803 }
804
805
806 if (timeout > valid)
807 timeout = valid;
808
809 if (set_rtnetlink_addr(ifindex, &addr->addr,
810 pref, valid) == -EADDRNOTAVAIL) {
811 dhcpv6_request(DHCPV6_MSG_DECLINE);
812 raise(SIGUSR2);
813 }
814 }
815 }
816
817 return timeout;
818 }