Add RA-handling support
[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(void);
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 ifindex = -1;
79 static time_t t1 = 0, t2 = 0, t3 = 0;
80
81 // IA states
82 static int request_prefix = -1;
83 static enum odhcp6c_ia_mode na_mode = IA_MODE_NONE;
84 static bool accept_reconfig = false;
85
86
87
88 int init_dhcpv6(const char *ifname, int request_pd)
89 {
90 request_prefix = request_pd;
91
92 sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_UDP);
93
94 // Detect interface
95 struct ifreq ifr;
96 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
97 if (ioctl(sock, SIOCGIFINDEX, &ifr))
98 return -1;
99 ifindex = ifr.ifr_ifindex;
100
101 // Create client DUID
102 size_t client_id_len;
103 odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
104 if (client_id_len == 0) {
105 ioctl(sock, SIOCGIFHWADDR, &ifr);
106 uint8_t duid[14] = {0, DHCPV6_OPT_CLIENTID, 0, 10, 0,
107 DHCPV6_DUID_LLADDR, 0, 1};
108 memcpy(&duid[8], ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
109
110 uint8_t zero[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0};
111 struct ifreq ifs[100], *ifp, *ifend;
112 struct ifconf ifc;
113 ifc.ifc_req = ifs;
114 ifc.ifc_len = sizeof(ifs);
115
116 if (!memcmp(&duid[8], zero, ETHER_ADDR_LEN) &&
117 ioctl(sock, SIOCGIFCONF, &ifc) >= 0) {
118 // If our interface doesn't have an address...
119 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
120 for (ifp = ifc.ifc_req; ifp < ifend &&
121 !memcmp(&duid[8], zero, 6); ifp++) {
122 memcpy(ifr.ifr_name, ifp->ifr_name,
123 sizeof(ifr.ifr_name));
124 ioctl(sock, SIOCGIFHWADDR, &ifr);
125 memcpy(&duid[8], ifr.ifr_hwaddr.sa_data,
126 ETHER_ADDR_LEN);
127 }
128 }
129
130 odhcp6c_add_state(STATE_CLIENT_ID, duid, sizeof(duid));
131 }
132
133 // Create ORO
134 uint16_t oro[] = {htons(DHCPV6_OPT_DNS_SERVERS),
135 htons(DHCPV6_OPT_DNS_DOMAIN),
136 htons(DHCPV6_OPT_NTP_SERVER),
137 htons(DHCPV6_OPT_SIP_SERVER_A),
138 htons(DHCPV6_OPT_SIP_SERVER_D)};
139 odhcp6c_add_state(STATE_ORO, oro, sizeof(oro));
140
141
142 // Configure IPv6-options
143 int val = 1;
144 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
145 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
146 setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
147
148 struct sockaddr_in6 client_addr = { .sin6_family = AF_INET6,
149 .sin6_port = htons(DHCPV6_CLIENT_PORT), .sin6_flowinfo = 0 };
150 if (bind(sock, (struct sockaddr*)&client_addr, sizeof(client_addr)))
151 return -1;
152
153 return 0;
154 }
155
156
157 void dhcpv6_set_ia_na_mode(enum odhcp6c_ia_mode mode)
158 {
159 na_mode = mode;
160 }
161
162
163 static void dhcpv6_send(enum dhcpv6_msg type, uint8_t trid[3], uint32_t ecs)
164 {
165 // Build FQDN
166 char fqdn_buf[256];
167 gethostname(fqdn_buf, sizeof(fqdn_buf));
168 struct {
169 uint16_t type;
170 uint16_t len;
171 uint8_t flags;
172 uint8_t data[256];
173 } fqdn;
174 size_t fqdn_len = 5 + dn_comp(fqdn_buf, fqdn.data,
175 sizeof(fqdn.data), NULL, NULL);
176 fqdn.type = htons(DHCPV6_OPT_FQDN);
177 fqdn.len = htons(fqdn_len - 4);
178 fqdn.flags = 0;
179
180
181 // Build Client ID
182 size_t cl_id_len;
183 void *cl_id = odhcp6c_get_state(STATE_CLIENT_ID, &cl_id_len);
184
185 // Get Server ID
186 size_t srv_id_len;
187 void *srv_id = odhcp6c_get_state(STATE_SERVER_ID, &srv_id_len);
188
189 // Build IA_PDs
190 size_t ia_pd_entry_len, ia_pd_len = 0;
191 void *ia_pd = NULL;
192 void *ia_pd_entries = odhcp6c_get_state(STATE_IA_PD, &ia_pd_entry_len);
193 struct dhcpv6_ia_hdr hdr_ia_pd = {
194 htons(DHCPV6_OPT_IA_PD),
195 htons(sizeof(hdr_ia_pd) - 4 + ia_pd_len),
196 1, 0, 0
197 };
198
199 struct dhcpv6_ia_prefix pref = {
200 .type = htons(DHCPV6_OPT_IA_PREFIX),
201 .len = htons(25), .prefix = request_prefix
202 };
203
204 if (ia_pd_entry_len > 0) {
205 struct odhcp6c_entry *e = ia_pd_entries;
206 size_t entries = ia_pd_entry_len / sizeof(*e);
207 struct dhcpv6_ia_prefix p[entries];
208 for (size_t i = 0; i < entries; ++i) {
209 p[i].type = htons(DHCPV6_OPT_IA_PREFIX);
210 p[i].len = htons(sizeof(p[i]) - 4U);
211 p[i].preferred = htonl(e[i].preferred);
212 p[i].valid = htonl(e[i].valid);
213 p[i].prefix = e[i].length;
214 p[i].addr = e[i].target;
215 }
216 ia_pd = p;
217 ia_pd_len = sizeof(p);
218 } else if (request_prefix > 0 &&
219 (type == DHCPV6_MSG_SOLICIT ||
220 type == DHCPV6_MSG_REQUEST)) {
221 ia_pd = &pref;
222 ia_pd_len = sizeof(pref);
223 }
224
225 // Build IA_NAs
226 size_t ia_na_entry_len, ia_na_len = 0;
227 void *ia_na = NULL;
228 void *ia_na_entries = odhcp6c_get_state(STATE_IA_NA, &ia_na_entry_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 if (ia_na_entry_len > 0) {
236 struct odhcp6c_entry *e = ia_na_entries;
237 size_t entries = ia_na_entry_len / sizeof(*e);
238 struct dhcpv6_ia_addr p[entries];
239 for (size_t i = 0; i < entries; ++i) {
240 p[i].type = htons(DHCPV6_OPT_IA_ADDR);
241 p[i].len = htons(sizeof(p[i]) - 4U);
242 p[i].addr = e[i].target;
243 p[i].preferred = htonl(e[i].preferred);
244 p[i].valid = htonl(e[i].valid);
245 }
246 ia_na = p;
247 ia_na_len = sizeof(p);
248 }
249
250 // Reconfigure Accept
251 struct {
252 uint16_t type;
253 uint16_t length;
254 } reconf_accept = {htons(DHCPV6_OPT_RECONF_ACCEPT), 0};
255
256 // Request Information Refresh
257 uint16_t oro_refresh = htons(DHCPV6_OPT_INFO_REFRESH);
258
259 // Prepare Header
260 size_t oro_len;
261 void *oro = odhcp6c_get_state(STATE_ORO, &oro_len);
262 struct {
263 uint8_t type;
264 uint8_t trid[3];
265 uint16_t elapsed_type;
266 uint16_t elapsed_len;
267 uint16_t elapsed_value;
268 uint16_t oro_type;
269 uint16_t oro_len;
270 } hdr = {
271 type, {trid[0], trid[1], trid[2]},
272 htons(DHCPV6_OPT_ELAPSED), htons(2),
273 htons((ecs > 0xffff) ? 0xffff : ecs),
274 htons(DHCPV6_OPT_ORO), htons(oro_len),
275 };
276
277 struct iovec iov[] = {
278 {&hdr, sizeof(hdr)},
279 {oro, oro_len},
280 {&oro_refresh, 0},
281 {cl_id, cl_id_len},
282 {srv_id, srv_id_len},
283 {&reconf_accept, 0},
284 {&fqdn, fqdn_len},
285 {&hdr_ia_na, sizeof(hdr_ia_na)},
286 {ia_na, ia_na_len},
287 {&hdr_ia_pd, sizeof(hdr_ia_pd)},
288 {ia_pd, ia_pd_len},
289 };
290
291 size_t cnt = ARRAY_SIZE(iov);
292 if (type == DHCPV6_MSG_INFO_REQ) {
293 cnt = 5;
294 iov[2].iov_len = sizeof(oro_refresh);
295 hdr.oro_len = htons(oro_len + sizeof(oro_refresh));
296 } else if (!request_prefix) {
297 cnt = 9;
298 }
299
300 // Disable IAs if not used
301 if (type == DHCPV6_MSG_SOLICIT) {
302 iov[5].iov_len = sizeof(reconf_accept);
303 } else if (type != DHCPV6_MSG_REQUEST) {
304 if (ia_na_len == 0)
305 iov[7].iov_len = 0;
306 if (ia_pd_len == 0)
307 iov[9].iov_len = 0;
308 }
309
310 if (na_mode == IA_MODE_NONE)
311 iov[7].iov_len = 0;
312
313 struct sockaddr_in6 srv = {AF_INET6, htons(DHCPV6_SERVER_PORT),
314 0, ALL_DHCPV6_RELAYS, ifindex};
315 struct msghdr msg = {&srv, sizeof(srv), iov, cnt, NULL, 0, 0};
316
317 sendmsg(sock, &msg, 0);
318 }
319
320
321 static int64_t dhcpv6_rand_delay(int64_t time)
322 {
323 int random;
324 odhcp6c_random(&random, sizeof(random));
325 return (time * (random % 1000)) / 10000;
326 }
327
328
329 int dhcpv6_request(enum dhcpv6_msg type)
330 {
331 uint8_t buf[1536];
332 uint32_t timeout = UINT32_MAX;
333 struct dhcpv6_retx *retx = &dhcpv6_retx[type];
334
335 if (retx->delay) {
336 struct timespec ts = {0, 0};
337 ts.tv_nsec = dhcpv6_rand_delay(10 * DHCPV6_REQ_DELAY);
338 nanosleep(&ts, NULL);
339 }
340
341 if (type == DHCPV6_MSG_RELEASE || type == DHCPV6_MSG_DECLINE)
342 timeout = 3;
343 else if (type == DHCPV6_MSG_UNKNOWN)
344 timeout = t1;
345 else if (type == DHCPV6_MSG_RENEW)
346 timeout = t2 - t1;
347 else if (type == DHCPV6_MSG_REBIND)
348 timeout = t3 - t2;
349
350 if (timeout == 0)
351 return -1;
352
353 syslog(LOG_NOTICE, "Sending %s (timeout %us)", retx->name, timeout);
354
355 uint64_t start = odhcp6c_get_milli_time(), round_start = start, elapsed;
356
357 // Generate transaction ID
358 uint8_t trid[3];
359 odhcp6c_random(trid, sizeof(trid));
360 ssize_t len = -1;
361 int64_t rto = 0;
362
363 do {
364 rto = (rto == 0) ? (retx->init_timeo * 1000 +
365 dhcpv6_rand_delay(retx->init_timeo * 1000)) :
366 (2 * rto + dhcpv6_rand_delay(rto));
367
368 if (rto >= retx->max_timeo * 1000)
369 rto = retx->max_timeo * 1000 +
370 dhcpv6_rand_delay(retx->max_timeo * 1000);
371
372 // Calculate end for this round and elapsed time
373 uint64_t round_end = round_start + rto;
374 elapsed = round_start - start;
375
376 // Don't wait too long
377 if (round_end - start > timeout * 1000)
378 round_end = timeout * 1000 + start;
379
380 // Built and send package
381 if (type != DHCPV6_MSG_UNKNOWN)
382 dhcpv6_send(type, trid, elapsed / 10);
383
384 // Receive rounds
385 for (; len < 0 && round_start < round_end;
386 round_start = odhcp6c_get_milli_time()) {
387 // Check for pending signal
388 if (odhcp6c_signal_process())
389 return -1;
390
391 // Set timeout for receiving
392 uint64_t t = round_end - round_start;
393 struct timeval timeout = {t / 1000, (t % 1000) * 1000};
394 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
395 &timeout, sizeof(timeout));
396
397 // Receive cycle
398 len = recv(sock, buf, sizeof(buf), 0);
399
400 if (!dhcpv6_response_is_valid(buf, len, trid, type))
401 len = -1;
402
403 if (len > 0) {
404 uint8_t *opt = &buf[4];
405 uint8_t *opt_end = opt + len - 4;
406
407 round_start = odhcp6c_get_milli_time();
408 elapsed = round_start - start;
409 syslog(LOG_NOTICE, "Got a valid reply after "
410 "%ums", (unsigned)elapsed);
411
412 if (retx->handler_reply)
413 len = retx->handler_reply(
414 type, opt, opt_end);
415 }
416 }
417
418 // Allow
419 if (retx->handler_finish)
420 len = retx->handler_finish();
421 } while (len < 0 && elapsed / 1000 < timeout);
422
423 return len;
424 }
425
426
427 static bool dhcpv6_response_is_valid(const void *buf, ssize_t len,
428 const uint8_t transaction[3], enum dhcpv6_msg type)
429 {
430 const struct dhcpv6_header *rep = buf;
431 if (len < (ssize_t)sizeof(*rep) || memcmp(rep->tr_id,
432 transaction, sizeof(rep->tr_id)))
433 return false; // Invalid reply
434
435 if (type == DHCPV6_MSG_SOLICIT) {
436 if (rep->msg_type != DHCPV6_MSG_ADVERT &&
437 rep->msg_type != DHCPV6_MSG_REPLY)
438 return false;
439 } else if (type == DHCPV6_MSG_UNKNOWN) {
440 if (!accept_reconfig || rep->msg_type != DHCPV6_MSG_RECONF)
441 return false;
442 } else if (rep->msg_type != DHCPV6_MSG_REPLY) {
443 return false;
444 }
445
446 uint8_t *end = ((uint8_t*)buf) + len, *odata;
447 uint16_t otype, olen;
448 bool clientid_ok = false, serverid_ok = false;
449
450 size_t client_id_len, server_id_len;
451 void *client_id = odhcp6c_get_state(STATE_CLIENT_ID, &client_id_len);
452 void *server_id = odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
453
454 dhcpv6_for_each_option(&rep[1], end, otype, olen, odata)
455 if (otype == DHCPV6_OPT_CLIENTID)
456 clientid_ok = (olen + 4U == client_id_len) && !memcmp(
457 &odata[-4], client_id, client_id_len);
458 else if (otype == DHCPV6_OPT_SERVERID)
459 serverid_ok = (olen + 4U == server_id_len) && !memcmp(
460 &odata[-4], server_id, server_id_len);
461
462 return clientid_ok && (serverid_ok || server_id_len == 0);
463 }
464
465
466 int dhcpv6_poll_reconfigure(void)
467 {
468 int ret = dhcpv6_request(DHCPV6_MSG_UNKNOWN);
469 if (ret != -1)
470 ret = dhcpv6_request(ret);
471
472 return ret;
473 }
474
475
476 static int dhcpv6_handle_reconfigure(_unused enum dhcpv6_msg orig,
477 const void *opt, const void *end)
478 {
479 // TODO: should verify the reconfigure message
480 uint16_t otype, olen;
481 uint8_t *odata, msg = DHCPV6_MSG_RENEW;
482 dhcpv6_for_each_option(opt, end, otype, olen, odata)
483 if (otype == DHCPV6_OPT_RECONF_MESSAGE && olen == 1 && (
484 odata[0] == DHCPV6_MSG_RENEW ||
485 odata[0] == DHCPV6_MSG_INFO_REQ))
486 msg = odata[0];
487
488 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL);
489 return msg;
490 }
491
492
493 // Collect all advertised servers
494 static int dhcpv6_handle_advert(_unused enum dhcpv6_msg orig,
495 const void *opt, const void *end)
496 {
497 uint16_t olen, otype;
498 uint8_t *odata;
499 struct dhcpv6_server_cand cand = {false, false, 0, 0, {0}};
500
501 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
502 if (otype == DHCPV6_OPT_SERVERID && olen <= 130) {
503 memcpy(cand.duid, odata, olen);
504 cand.duid_len = olen;
505 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
506 && odata[1] == DHCPV6_NoAddrsAvail) {
507 if (na_mode == IA_MODE_FORCE) {
508 return -1;
509 } else {
510 cand.has_noaddravail = true;
511 cand.preference -= 1000;
512 }
513 } else if (otype == DHCPV6_OPT_STATUS && olen >= 2 && !odata[0]
514 && odata[1] == DHCPV6_NoPrefixAvail) {
515 cand.preference -= 2000;
516 } else if (otype == DHCPV6_OPT_PREF && olen >= 1 &&
517 cand.preference >= 0) {
518 cand.preference = odata[1];
519 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT) {
520 cand.wants_reconfigure = true;
521 } else if (otype == DHCPV6_OPT_IA_PD && request_prefix) {
522 struct dhcpv6_ia_hdr *h = (void*)odata;
523 uint8_t *oend = odata + olen, *d;
524 dhcpv6_for_each_option(&h[1], oend, otype, olen, d) {
525 if (otype == DHCPV6_OPT_IA_PREFIX)
526 cand.preference += 2000;
527 else if (otype == DHCPV6_OPT_STATUS &&
528 olen >= 2 && d[0] == 0 &&
529 d[1] == DHCPV6_NoPrefixAvail)
530 cand.preference -= 2000;
531 }
532 }
533 }
534
535 if (cand.duid_len > 0)
536 odhcp6c_add_state(STATE_SERVER_CAND, &cand, sizeof(cand));
537
538 return -1;
539 }
540
541
542 static int dhcpv6_commit_advert(void)
543 {
544 size_t cand_len;
545 struct dhcpv6_server_cand *c = NULL, *cand =
546 odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
547
548 bool retry = false;
549 for (size_t i = 0; i < cand_len / sizeof(*c); ++i) {
550 if (cand[i].has_noaddravail)
551 retry = true; // We want to try again
552
553 if (!c || c->preference < cand[i].preference)
554 c = &cand[i];
555 }
556
557 if (retry && na_mode == IA_MODE_TRY) {
558 // We give it a second try without the IA_NA
559 na_mode = IA_MODE_NONE;
560 return dhcpv6_request(DHCPV6_MSG_SOLICIT);
561 }
562
563 if (c) {
564 uint16_t hdr[2] = {htons(DHCPV6_OPT_SERVERID),
565 htons(c->duid_len)};
566 odhcp6c_add_state(STATE_SERVER_ID, hdr, sizeof(hdr));
567 odhcp6c_add_state(STATE_SERVER_ID, c->duid, c->duid_len);
568 accept_reconfig = c->wants_reconfigure;
569 }
570
571 odhcp6c_clear_state(STATE_SERVER_CAND);
572
573 if (!c)
574 return -1;
575 else if (request_prefix || na_mode != IA_MODE_NONE)
576 return DHCPV6_STATEFUL;
577 else
578 return DHCPV6_STATELESS;
579 }
580
581
582 static int dhcpv6_handle_rebind_reply(enum dhcpv6_msg orig,
583 const void *opt, const void *end)
584 {
585 dhcpv6_handle_advert(orig, opt, end);
586 if (dhcpv6_commit_advert() < 0) {
587 dhcpv6_handle_reply(DHCPV6_MSG_UNKNOWN, NULL, NULL);
588 return -1;
589 }
590
591 return dhcpv6_handle_reply(orig, opt, end);
592 }
593
594
595 static int dhcpv6_handle_reply(enum dhcpv6_msg orig,
596 const void *opt, const void *end)
597 {
598 uint8_t *odata;
599 uint16_t otype, olen;
600
601 static time_t last_update = 0;
602 time_t now = odhcp6c_get_milli_time() / 1000;
603
604 uint32_t elapsed = now - last_update;
605 odhcp6c_expire();
606
607 if (orig == DHCPV6_MSG_UNKNOWN) {
608 t1 -= elapsed;
609 t2 -= elapsed;
610 t3 -= elapsed;
611
612 if (t1 < 0)
613 t1 = 0;
614
615 if (t2 < 0)
616 t2 = 0;
617
618 if (t3 < 0)
619 t3 = 0;
620 } else {
621 t1 = t2 = t3 = 86400;
622 }
623
624 if (opt) {
625 odhcp6c_clear_state(STATE_DNS);
626 odhcp6c_clear_state(STATE_SEARCH);
627 odhcp6c_clear_state(STATE_SNTP_IP);
628 odhcp6c_clear_state(STATE_SNTP_FQDN);
629 odhcp6c_clear_state(STATE_SIP_IP);
630 odhcp6c_clear_state(STATE_SIP_FQDN);
631 }
632
633 // Parse and find all matching IAs
634 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
635 if ((otype == DHCPV6_OPT_IA_PD || otype == DHCPV6_OPT_IA_NA)
636 && olen > sizeof(struct dhcpv6_ia_hdr)) {
637 struct dhcpv6_ia_hdr *ia_hdr = (void*)(&odata[-4]);
638 time_t l_t1 = ntohl(ia_hdr->t1);
639 time_t l_t2 = ntohl(ia_hdr->t2);
640
641 // Test ID and T1-T2 validity
642 if (ia_hdr->iaid != 1 || l_t2 < l_t1)
643 continue;
644
645 uint16_t stype, slen;
646 uint8_t *sdata;
647 // Test status and bail if error
648 dhcpv6_for_each_option(&ia_hdr[1], odata + olen,
649 stype, slen, sdata)
650 if (stype == DHCPV6_OPT_STATUS && slen >= 2 &&
651 (sdata[0] || sdata[1]))
652 continue;
653
654 // Update times
655 if (l_t1 > 0 && t1 > l_t1)
656 t1 = l_t1;
657
658 if (l_t2 > 0 && t2 > l_t2)
659 t2 = l_t2;
660
661 time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
662
663 if (n < t1)
664 t1 = n;
665
666 if (n < t2)
667 t2 = n;
668
669 if (n < t3)
670 t3 = n;
671
672 } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
673 if (olen % 16 == 0)
674 odhcp6c_add_state(STATE_DNS, odata, olen);
675 } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
676 odhcp6c_add_state(STATE_SEARCH, odata, olen);
677 } else if (otype == DHCPV6_OPT_NTP_SERVER) {
678 uint16_t stype, slen;
679 uint8_t *sdata;
680 // Test status and bail if error
681 dhcpv6_for_each_option(odata, odata + olen,
682 stype, slen, sdata) {
683 if (slen == 16 && (stype == NTP_MC_ADDR ||
684 stype == NTP_SRV_ADDR))
685 odhcp6c_add_state(STATE_SNTP_IP,
686 sdata, slen);
687 else if (slen > 0 && stype == NTP_SRV_FQDN)
688 odhcp6c_add_state(STATE_SNTP_FQDN,
689 sdata, slen);
690 }
691 } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
692 if (olen == 16)
693 odhcp6c_add_state(STATE_SIP_IP, odata, olen);
694 } else if (otype == DHCPV6_OPT_SIP_SERVER_D) {
695 odhcp6c_add_state(STATE_SIP_FQDN, odata, olen);
696 } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
697 uint32_t refresh = ntohl(*((uint32_t*)odata));
698 if (refresh < (uint32_t)t1)
699 t1 = refresh;
700 } else if (otype != DHCPV6_OPT_CLIENTID &&
701 otype != DHCPV6_OPT_SERVERID) {
702 odhcp6c_add_state(STATE_CUSTOM_OPTS,
703 &odata[-4], olen + 4);
704 }
705 }
706
707 return true;
708 }
709
710
711 static time_t dhcpv6_parse_ia(void *opt, void *end)
712 {
713 uint32_t timeout = UINT32_MAX; // Minimum timeout
714 uint16_t otype, olen;
715 uint8_t *odata;
716
717 struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT,
718 0, 0, IN6ADDR_ANY_INIT, 0, 0};
719
720 // Update address IA
721 dhcpv6_for_each_option(opt, end, otype, olen, odata) {
722 if (otype == DHCPV6_OPT_IA_PREFIX) {
723 struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
724 if (olen + 4U < sizeof(*prefix))
725 continue;
726
727 entry.valid = ntohl(prefix->valid);
728 entry.preferred = ntohl(prefix->preferred);
729
730 if (entry.preferred > entry.valid)
731 continue;
732
733 entry.length = prefix->prefix;
734 entry.target = prefix->addr;
735
736 odhcp6c_update_entry(STATE_IA_PD, &entry);
737 } else if (otype == DHCPV6_OPT_IA_ADDR) {
738 struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
739 if (olen + 4U < sizeof(*addr))
740 continue;
741
742 entry.preferred = ntohl(addr->preferred);
743 entry.valid = ntohl(addr->valid);
744
745 if (entry.preferred > entry.valid)
746 continue;
747
748 entry.length = 128;
749 entry.target = addr->addr;
750
751 odhcp6c_update_entry(STATE_IA_NA, &entry);
752 }
753
754 if (entry.valid > 0 && timeout > entry.valid)
755 timeout = entry.valid;
756 }
757
758 return timeout;
759 }