]> git.decadent.org.uk Git - odhcp6c.git/blob - src/dhcpv6.c
Fix DNS-server parsing with more than one server
[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                 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
618
619                 uint32_t pref = ntohl(p->preferred);
620                 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
621         }
622
623         // Decrease valid and preferred lifetime of addresses
624         dhcpv6_for_each_option(ia_na, ia_na + ia_na_len, otype, olen, odata) {
625                 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
626                 uint32_t valid = ntohl(p->valid);
627                 p->valid = (valid < elapsed) ? 0 : htonl(valid - elapsed);
628
629                 uint32_t pref = ntohl(p->preferred);
630                 p->preferred = (pref < elapsed) ? 0 : htonl(pref - elapsed);
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                         // Always report update in case we have IA_PDs so that
662                         // the state-script is called with updated times
663                         if (otype == DHCPV6_OPT_IA_PD && request_prefix)
664                                 have_update = true;
665
666                         time_t n = dhcpv6_parse_ia(&ia_hdr[1], odata + olen);
667
668                         if (n < t1)
669                                 t1 = n;
670
671                         if (n < t2)
672                                 t2 = n;
673
674                         if (n < t3)
675                                 t3 = n;
676
677                 } else if (otype == DHCPV6_OPT_DNS_SERVERS) {
678                         if (olen % 16 == 0)
679                                 odhcp6c_add_state(STATE_DNS, odata, olen);
680                 } else if (otype == DHCPV6_OPT_DNS_DOMAIN) {
681                         odhcp6c_add_state(STATE_SEARCH, odata, olen);
682                 } else if (otype == DHCPV6_OPT_NTP_SERVER) {
683                         uint16_t stype, slen;
684                         uint8_t *sdata;
685                         // Test status and bail if error
686                         dhcpv6_for_each_option(odata, odata + olen,
687                                         stype, slen, sdata) {
688                                 if (slen == 16 && (stype == NTP_MC_ADDR ||
689                                                 stype == NTP_SRV_ADDR))
690                                         odhcp6c_add_state(STATE_SNTP_IP,
691                                                         sdata, slen);
692                                 else if (slen > 0 && stype == NTP_SRV_FQDN)
693                                         odhcp6c_add_state(STATE_SNTP_FQDN,
694                                                         sdata, slen);
695                         }
696                 } else if (otype == DHCPV6_OPT_SIP_SERVER_A) {
697                         if (olen == 16)
698                                 odhcp6c_add_state(STATE_SIP_IP, odata, olen);
699                 } else if (otype == DHCPV6_OPT_SIP_SERVER_D) {
700                         odhcp6c_add_state(STATE_SIP_FQDN, odata, olen);
701                 } else if (otype == DHCPV6_OPT_INFO_REFRESH && olen >= 4) {
702                         uint32_t refresh = ntohl(*((uint32_t*)odata));
703                         if (refresh < (uint32_t)t1)
704                                 t1 = refresh;
705                 } else if (otype != DHCPV6_OPT_CLIENTID &&
706                                 otype != DHCPV6_OPT_SERVERID) {
707                         odhcp6c_add_state(STATE_CUSTOM_OPTS,
708                                         &odata[-4], olen + 4);
709                 }
710         }
711
712         if (opt) {
713                 have_update |= odhcp6c_commit_state(STATE_DNS, dns_len);
714                 have_update |= odhcp6c_commit_state(STATE_SEARCH, search_len);
715                 have_update |= odhcp6c_commit_state(STATE_SNTP_IP,
716                                 sntp_ip_len);
717                 have_update |= odhcp6c_commit_state(STATE_SNTP_FQDN,
718                                 sntp_dns_len);
719                 have_update |= odhcp6c_commit_state(STATE_SIP_IP, sip_ip_len);
720                 have_update |= odhcp6c_commit_state(STATE_SIP_FQDN, sip_fqdn_len);
721                 size_t new_ia_pd_len, new_ia_na_len;
722                 odhcp6c_get_state(STATE_IA_PD, &new_ia_pd_len);
723                 odhcp6c_get_state(STATE_IA_NA, &new_ia_na_len);
724                 have_update |= (new_ia_pd_len != ia_pd_len) ||
725                                 (new_ia_na_len != ia_na_len);
726         }
727
728         // Delete prefixes with 0 valid-time
729         ia_pd = odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
730         ia_end = ia_pd + ia_pd_len;
731         dhcpv6_for_each_option(ia_pd, ia_end, otype, olen, odata) {
732                 struct dhcpv6_ia_prefix *p = (void*)&odata[-4];
733                 while (!p->valid) {
734                         ia_end = ia_pd + odhcp6c_remove_state(STATE_IA_PD,
735                                         (uint8_t*)p - ia_pd, olen + 4);
736                         have_update = true;
737                 }
738         }
739
740
741         // Delete addresses with 0 valid-time
742         ia_na = odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
743         ia_end = ia_na + ia_na_len;
744         dhcpv6_for_each_option(ia_na, ia_end, otype, olen, odata) {
745                 struct dhcpv6_ia_addr *p = (void*)&odata[-4];
746                 while (!p->valid) {
747                         ia_end = ia_na + odhcp6c_remove_state(STATE_IA_NA,
748                                         (uint8_t*)p - ia_na, olen + 4);
749                         have_update = true;
750                 }
751         }
752
753         return have_update;
754 }
755
756
757 static time_t dhcpv6_parse_ia(void *opt, void *end)
758 {
759         uint32_t timeout = UINT32_MAX; // Minimum timeout
760         uint16_t otype, olen, stype, slen;
761         uint8_t *odata, *sdata;
762
763         // Update address IA
764         dhcpv6_for_each_option(opt, end, otype, olen, odata) {
765                 if (otype == DHCPV6_OPT_IA_PREFIX) {
766                         struct dhcpv6_ia_prefix *prefix = (void*)&odata[-4];
767                         if (olen + 4U < sizeof(*prefix))
768                                 continue;
769
770                         olen = sizeof(*prefix); // Normalize length
771                         uint32_t valid = ntohl(prefix->valid);
772                         uint32_t pref = ntohl(prefix->preferred);
773
774                         if (pref > valid)
775                                 continue;
776
777                         // Search matching IA
778                         struct dhcpv6_ia_prefix *local = NULL;
779                         size_t pd_len;
780                         uint8_t *pd = odhcp6c_get_state(STATE_IA_PD, &pd_len);
781                         dhcpv6_for_each_option(pd, pd + pd_len,
782                                         stype, slen, sdata)
783                                 if (!memcmp(sdata + 8, odata + 8,
784                                                 sizeof(local->addr) + 1))
785                                         local = (void*)&sdata[-4];
786
787                         if (local) { // Already know that IA
788                                 local->preferred = prefix->preferred;
789                                 local->valid = prefix->valid;
790                         } else { // New IA
791                                 odhcp6c_add_state(STATE_IA_PD, prefix, olen);
792                         }
793
794                         if (timeout > valid)
795                                 timeout = valid;
796                 } else if (otype == DHCPV6_OPT_IA_ADDR) {
797                         struct dhcpv6_ia_addr *addr = (void*)&odata[-4];
798                         if (olen + 4U < sizeof(*addr))
799                                 continue;
800
801                         olen = sizeof(*addr); // Normalize length
802                         uint32_t pref = ntohl(addr->preferred);
803                         uint32_t valid = ntohl(addr->valid);
804
805                         if (pref > valid)
806                                 continue;
807
808                         // Search matching IA
809                         struct dhcpv6_ia_addr *local = NULL;
810                         size_t na_len;
811                         uint8_t *na = odhcp6c_get_state(STATE_IA_NA, &na_len);
812                         dhcpv6_for_each_option(na, na + na_len,
813                                         stype, slen, sdata)
814                                 if (!memcmp(sdata, odata, sizeof(local->addr)))
815                                         local = (void*)&sdata[-4];
816
817
818                         if (local) { // Already know that IA
819                                 local->preferred = addr->preferred;
820                                 local->valid = addr->valid;
821                         } else { // New IA
822                                 odhcp6c_add_state(STATE_IA_NA, addr, olen);
823                         }
824
825
826                         if (timeout > valid)
827                                 timeout = valid;
828
829                         if (set_rtnetlink_addr(ifindex, &addr->addr,
830                                         pref, valid) == -EADDRNOTAVAIL) {
831                                 dhcpv6_request(DHCPV6_MSG_DECLINE);
832                                 raise(SIGUSR2);
833                         }
834                 }
835         }
836
837         return timeout;
838 }