dhcpv6-ia: fix dereference after freeing assignment
[project/odhcpd.git] / src / dhcpv6-ia.c
1 /**
2  * Copyright (C) 2013 Steven Barth <steven@midlink.org>
3  * Copyright (C) 2016 Hans Dedecker <dedeckeh@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License v2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include "odhcpd.h"
17 #include "dhcpv6.h"
18 #include "dhcpv4.h"
19 #include "libubox/md5.h"
20 #include "libubox/usock.h"
21
22 #include <time.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <poll.h>
27 #include <alloca.h>
28 #include <resolv.h>
29 #include <limits.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <stdbool.h>
34 #include <arpa/inet.h>
35 #include <sys/timerfd.h>
36
37 #define ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) \
38     ((iface)->managed == RELAYD_MANAGED_NO_AFLAG || (i) == (m) || \
39      (addrs)[(i)].prefix > 64)
40
41 static void free_dhcpv6_assignment(struct dhcpv6_assignment *c);
42 static void reconf_timer(struct uloop_timeout *event);
43 static struct uloop_timeout reconf_event = {.cb = reconf_timer};
44 static uint32_t serial = 0;
45 static uint8_t statemd5[16];
46
47 int dhcpv6_ia_init(void)
48 {
49         uloop_timeout_set(&reconf_event, 2000);
50         return 0;
51 }
52
53 int setup_dhcpv6_ia_interface(struct interface *iface, bool enable)
54 {
55         if (!enable && iface->ia_assignments.next) {
56                 struct dhcpv6_assignment *c;
57
58                 while (!list_empty(&iface->ia_assignments)) {
59                         c = list_first_entry(&iface->ia_assignments, struct dhcpv6_assignment, head);
60                         free_dhcpv6_assignment(c);
61                 }
62         }
63
64         if (enable && iface->dhcpv6 == RELAYD_SERVER) {
65                 if (!iface->ia_assignments.next)
66                         INIT_LIST_HEAD(&iface->ia_assignments);
67
68                 if (list_empty(&iface->ia_assignments)) {
69                         struct dhcpv6_assignment *border = calloc(1, sizeof(*border));
70                         if (!border) {
71                                 syslog(LOG_ERR, "Calloc failed for border on interface %s", iface->ifname);
72                                 return -1;
73                         }
74
75                         border->length = 64;
76                         list_add(&border->head, &iface->ia_assignments);
77                 }
78
79                 /* Parse static entries */
80                 struct lease *lease;
81                 list_for_each_entry(lease, &leases, head) {
82                         /* Construct entry */
83                         size_t duid_len = lease->duid_len ? lease->duid_len : 14;
84                         struct dhcpv6_assignment *a = calloc(1, sizeof(*a) + duid_len);
85                         if (!a) {
86                                 syslog(LOG_ERR, "Calloc failed for static lease assignment on interface %s",
87                                         iface->ifname);
88                                 return -1;
89                         }
90
91                         if (lease->dhcpv4_leasetime > 0)
92                                 a->leasetime = lease->dhcpv4_leasetime;
93
94                         a->clid_len = duid_len;
95                         a->length = 128;
96                         if (lease->hostid) {
97                                 a->assigned = lease->hostid;
98                         } else {
99                                 uint32_t i4a = ntohl(lease->ipaddr.s_addr) & 0xff;
100                                 a->assigned = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
101                         }
102
103                         odhcpd_urandom(a->key, sizeof(a->key));
104                         memcpy(a->clid_data, lease->duid, lease->duid_len);
105                         memcpy(a->mac, lease->mac.ether_addr_octet, sizeof(a->mac));
106                         /* Static assignment */
107                         a->flags |= OAF_STATIC;
108                         /* Infinite valid */
109                         a->valid_until = 0;
110
111                         /* Assign to all interfaces */
112                         struct dhcpv6_assignment *c;
113                         list_for_each_entry(c, &iface->ia_assignments, head) {
114                                 if (c->length != 128 || c->assigned > a->assigned) {
115                                         list_add_tail(&a->head, &c->head);
116                                         break;
117                                 } else if (c->assigned == a->assigned)
118                                         /* Already an assignment with that number */
119                                         break;
120                         }
121
122                         if (a->head.next) {
123                                 if (lease->hostname[0]) {
124                                         free(a->hostname);
125                                         a->hostname = strdup(lease->hostname);
126                                 }
127                         } else
128                                 free_dhcpv6_assignment(a);
129                 }
130         }
131         return 0;
132 }
133
134 static void free_dhcpv6_assignment(struct dhcpv6_assignment *c)
135 {
136         if (c->managed_sock.fd.registered) {
137                 ustream_free(&c->managed_sock.stream);
138                 close(c->managed_sock.fd.fd);
139         }
140
141         if (c->head.next)
142                 list_del(&c->head);
143
144         free(c->managed);
145         free(c->hostname);
146         free(c);
147 }
148
149 static inline bool valid_addr(const struct odhcpd_ipaddr *addr, time_t now)
150 {
151         return (addr->prefix <= 96 && addr->preferred > (uint32_t)now);
152 }
153
154 static size_t elect_addr(const struct odhcpd_ipaddr *addrs, const size_t addrlen)
155 {
156         size_t i, m;
157
158         for (i = 0, m = 0; i < addrlen; ++i) {
159                 if (addrs[i].preferred > addrs[m].preferred ||
160                         (addrs[i].preferred == addrs[m].preferred &&
161                          memcmp(&addrs[i].addr, &addrs[m].addr, 16) > 0))
162                         m = i;
163         }
164
165         return m;
166 }
167
168 static int send_reconf(struct interface *iface, struct dhcpv6_assignment *assign)
169 {
170         struct {
171                 struct dhcpv6_client_header hdr;
172                 uint16_t srvid_type;
173                 uint16_t srvid_len;
174                 uint16_t duid_type;
175                 uint16_t hardware_type;
176                 uint8_t mac[6];
177                 uint16_t msg_type;
178                 uint16_t msg_len;
179                 uint8_t msg_id;
180                 struct dhcpv6_auth_reconfigure auth;
181                 uint16_t clid_type;
182                 uint16_t clid_len;
183                 uint8_t clid_data[128];
184         } __attribute__((packed)) reconf_msg = {
185                 .hdr = {DHCPV6_MSG_RECONFIGURE, {0, 0, 0}},
186                 .srvid_type = htons(DHCPV6_OPT_SERVERID),
187                 .srvid_len = htons(10),
188                 .duid_type = htons(3),
189                 .hardware_type = htons(1),
190                 .msg_type = htons(DHCPV6_OPT_RECONF_MSG),
191                 .msg_len = htons(1),
192                 .msg_id = DHCPV6_MSG_RENEW,
193                 .auth = {htons(DHCPV6_OPT_AUTH),
194                                 htons(sizeof(reconf_msg.auth) - 4), 3, 1, 0,
195                                 {htonl(time(NULL)), htonl(++serial)}, 2, {0}},
196                 .clid_type = htons(DHCPV6_OPT_CLIENTID),
197                 .clid_len = htons(assign->clid_len),
198                 .clid_data = {0},
199         };
200
201         odhcpd_get_mac(iface, reconf_msg.mac);
202         memcpy(reconf_msg.clid_data, assign->clid_data, assign->clid_len);
203         struct iovec iov = {&reconf_msg, sizeof(reconf_msg) - 128 + assign->clid_len};
204
205         md5_ctx_t md5;
206         uint8_t secretbytes[64];
207         memset(secretbytes, 0, sizeof(secretbytes));
208         memcpy(secretbytes, assign->key, sizeof(assign->key));
209
210         for (size_t i = 0; i < sizeof(secretbytes); ++i)
211                 secretbytes[i] ^= 0x36;
212
213         md5_begin(&md5);
214         md5_hash(secretbytes, sizeof(secretbytes), &md5);
215         md5_hash(iov.iov_base, iov.iov_len, &md5);
216         md5_end(reconf_msg.auth.key, &md5);
217
218         for (size_t i = 0; i < sizeof(secretbytes); ++i) {
219                 secretbytes[i] ^= 0x36;
220                 secretbytes[i] ^= 0x5c;
221         }
222
223         md5_begin(&md5);
224         md5_hash(secretbytes, sizeof(secretbytes), &md5);
225         md5_hash(reconf_msg.auth.key, 16, &md5);
226         md5_end(reconf_msg.auth.key, &md5);
227
228         return odhcpd_send(iface->dhcpv6_event.uloop.fd, &assign->peer, &iov, 1, iface);
229 }
230
231 void dhcpv6_write_statefile(void)
232 {
233         md5_ctx_t md5;
234         md5_begin(&md5);
235
236         if (config.dhcp_statefile) {
237                 time_t now = odhcpd_time(), wall_time = time(NULL);
238                 int fd = open(config.dhcp_statefile, O_CREAT | O_WRONLY | O_CLOEXEC, 0644);
239                 if (fd < 0)
240                         return;
241
242                 lockf(fd, F_LOCK, 0);
243                 if (ftruncate(fd, 0) < 0) {}
244
245                 FILE *fp = fdopen(fd, "w");
246                 if (!fp) {
247                         close(fd);
248                         return;
249                 }
250
251                 struct interface *iface;
252                 list_for_each_entry(iface, &interfaces, head) {
253                         if (iface->dhcpv6 != RELAYD_SERVER && iface->dhcpv4 != RELAYD_SERVER)
254                                 continue;
255
256                         if (iface->dhcpv6 == RELAYD_SERVER && iface->ia_assignments.next) {
257                                 struct dhcpv6_assignment *c;
258                                 list_for_each_entry(c, &iface->ia_assignments, head) {
259                                         if (!(c->flags & OAF_BOUND) || c->managed_size < 0)
260                                                 continue;
261
262                                         char ipbuf[INET6_ADDRSTRLEN];
263                                         char leasebuf[512];
264                                         char duidbuf[264];
265                                         odhcpd_hexlify(duidbuf, c->clid_data, c->clid_len);
266
267                                         /* iface DUID iaid hostname lifetime assigned length [addrs...] */
268                                         int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s %x %s %ld %x %u ",
269                                                         iface->ifname, duidbuf, ntohl(c->iaid),
270                                                         (c->hostname ? c->hostname : "-"),
271                                                         (c->valid_until > now ?
272                                                                 (c->valid_until - now + wall_time) :
273                                                                 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
274                                                         c->assigned, (unsigned)c->length);
275
276                                         struct in6_addr addr;
277                                         struct odhcpd_ipaddr *addrs = (c->managed) ? c->managed : iface->ia_addr;
278                                         size_t addrlen = (c->managed) ? (size_t)c->managed_size : iface->ia_addr_len;
279                                         size_t m = elect_addr(addrs, addrlen);
280
281                                         for (size_t i = 0; i < addrlen; ++i) {
282                                                 if (!valid_addr(&addrs[i], now) ||
283                                                             (!INFINITE_VALID(c->valid_until) && c->valid_until <= now) ||
284                                                             !ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs))
285                                                         continue;
286
287                                                 addr = addrs[i].addr;
288                                                 if (c->length == 128)
289                                                         addr.s6_addr32[3] = htonl(c->assigned);
290                                                 else
291                                                         addr.s6_addr32[1] |= htonl(c->assigned);
292
293                                                 inet_ntop(AF_INET6, &addr, ipbuf, sizeof(ipbuf) - 1);
294
295                                                 if (c->length == 128 && c->hostname) {
296                                                         fputs(ipbuf, fp);
297
298                                                         char b[256];
299                                                         if (dn_expand(iface->search, iface->search + iface->search_len,
300                                                                         iface->search, b, sizeof(b)) > 0)
301                                                                 fprintf(fp, "\t%s.%s", c->hostname, b);
302
303                                                         fprintf(fp, "\t%s\n", c->hostname);
304                                                         md5_hash(ipbuf, strlen(ipbuf), &md5);
305                                                         md5_hash(c->hostname, strlen(c->hostname), &md5);
306                                                 }
307
308                                                 l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/%d ", ipbuf,
309                                                                 (c->managed_size) ? addrs[i].prefix : c->length);
310                                         }
311                                         leasebuf[l - 1] = '\n';
312                                         fwrite(leasebuf, 1, l, fp);
313                                 }
314                         }
315
316                         if (iface->dhcpv4 == RELAYD_SERVER && iface->dhcpv4_assignments.next) {
317                                 struct dhcpv4_assignment *c;
318                                 list_for_each_entry(c, &iface->dhcpv4_assignments, head) {
319                                         if (!(c->flags & OAF_BOUND))
320                                                 continue;
321
322                                         char ipbuf[INET6_ADDRSTRLEN];
323                                         char leasebuf[512];
324                                         char duidbuf[16];
325                                         odhcpd_hexlify(duidbuf, c->hwaddr, sizeof(c->hwaddr));
326
327                                         /* iface DUID iaid hostname lifetime assigned length [addrs...] */
328                                         int l = snprintf(leasebuf, sizeof(leasebuf), "# %s %s ipv4 %s %ld %x 32 ",
329                                                         iface->ifname, duidbuf,
330                                                         (c->hostname ? c->hostname : "-"),
331                                                         (c->valid_until > now ?
332                                                                 (c->valid_until - now + wall_time) :
333                                                                 (INFINITE_VALID(c->valid_until) ? -1 : 0)),
334                                                         c->addr);
335
336                                         struct in_addr addr = {htonl(c->addr)};
337                                         inet_ntop(AF_INET, &addr, ipbuf, sizeof(ipbuf) - 1);
338
339                                         if (c->hostname) {
340                                                 fputs(ipbuf, fp);
341
342                                                 char b[256];
343                                                 if (dn_expand(iface->search, iface->search + iface->search_len,
344                                                                 iface->search, b, sizeof(b)) > 0)
345                                                         fprintf(fp, "\t%s.%s", c->hostname, b);
346
347                                                 fprintf(fp, "\t%s\n", c->hostname);
348                                                 md5_hash(ipbuf, strlen(ipbuf), &md5);
349                                                 md5_hash(c->hostname, strlen(c->hostname), &md5);
350                                         }
351
352                                         l += snprintf(leasebuf + l, sizeof(leasebuf) - l, "%s/32 ", ipbuf);
353                                         leasebuf[l - 1] = '\n';
354                                         fwrite(leasebuf, 1, l, fp);
355                                 }
356                         }
357                 }
358
359                 fclose(fp);
360         }
361
362         uint8_t newmd5[16];
363         md5_end(newmd5, &md5);
364
365         if (config.dhcp_cb && memcmp(newmd5, statemd5, sizeof(newmd5))) {
366                 memcpy(statemd5, newmd5, sizeof(statemd5));
367                 char *argv[2] = {config.dhcp_cb, NULL};
368                 if (!vfork()) {
369                         execv(argv[0], argv);
370                         _exit(128);
371                 }
372         }
373 }
374
375
376 static void apply_lease(struct interface *iface, struct dhcpv6_assignment *a, bool add)
377 {
378         if (a->length > 64 || a->managed_size < 0)
379                 return;
380
381         struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
382         size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
383
384         for (size_t i = 0; i < addrlen; ++i) {
385                 struct in6_addr prefix = addrs[i].addr;
386                 prefix.s6_addr32[1] |= htonl(a->assigned);
387                 odhcpd_setup_route(&prefix, (a->managed_size) ? addrs[i].prefix : a->length,
388                                 iface, &a->peer.sin6_addr, 1024, add);
389         }
390 }
391
392 /* More data was received from TCP connection */
393 static void managed_handle_pd_data(struct ustream *s, _unused int bytes_new)
394 {
395         struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
396         time_t now = odhcpd_time();
397         bool first = c->managed_size < 0;
398
399         for (;;) {
400                 int pending;
401                 char *data = ustream_get_read_buf(s, &pending);
402                 char *end = memmem(data, pending, "\n\n", 2);
403
404                 if (!end)
405                         break;
406
407                 end += 2;
408                 end[-1] = 0;
409
410                 c->managed_size = 0;
411                 if (c->accept_reconf)
412                         c->reconf_cnt = 1;
413
414                 char *saveptr;
415                 for (char *line = strtok_r(data, "\n", &saveptr); line; line = strtok_r(NULL, "\n", &saveptr)) {
416                         c->managed = realloc(c->managed, (c->managed_size + 1) * sizeof(*c->managed));
417                         struct odhcpd_ipaddr *n = &c->managed[c->managed_size];
418
419                         char *saveptr2, *x = strtok_r(line, "/", &saveptr2);
420                         if (!x || inet_pton(AF_INET6, x, &n->addr) < 1)
421                                 continue;
422
423                         x = strtok_r(NULL, ",", &saveptr2);
424                         if (sscanf(x, "%hhu", &n->prefix) < 1)
425                                 continue;
426
427                         x = strtok_r(NULL, ",", &saveptr2);
428                         if (sscanf(x, "%u", &n->preferred) < 1)
429                                 continue;
430
431                         x = strtok_r(NULL, ",", &saveptr2);
432                         if (sscanf(x, "%u", &n->valid) < 1)
433                                 continue;
434
435                         if (n->preferred > n->valid)
436                                 continue;
437
438                         if (UINT32_MAX - now < n->preferred)
439                                 n->preferred = UINT32_MAX;
440                         else
441                                 n->preferred += now;
442
443                         if (UINT32_MAX - now < n->valid)
444                                 n->valid = UINT32_MAX;
445                         else
446                                 n->valid += now;
447
448                         n->dprefix = 0;
449
450                         ++c->managed_size;
451                 }
452
453                 ustream_consume(s, end - data);
454         }
455
456         if (first && c->managed_size == 0)
457                 free_dhcpv6_assignment(c);
458         else if (first && !(c->flags & OAF_STATIC))
459                 c->valid_until = now + 150;
460 }
461
462
463 /* TCP transmission has ended, either because of success or timeout or other error */
464 static void managed_handle_pd_done(struct ustream *s)
465 {
466         struct dhcpv6_assignment *c = container_of(s, struct dhcpv6_assignment, managed_sock);
467
468         if (!(c->flags & OAF_STATIC))
469                 c->valid_until = odhcpd_time() + 15;
470
471         c->managed_size = 0;
472
473         if (c->accept_reconf)
474                 c->reconf_cnt = 1;
475 }
476
477 static bool assign_pd(struct interface *iface, struct dhcpv6_assignment *assign)
478 {
479         struct dhcpv6_assignment *c;
480
481         if (iface->dhcpv6_pd_manager[0]) {
482                 int fd = usock(USOCK_UNIX | USOCK_TCP, iface->dhcpv6_pd_manager, NULL);
483                 if (fd >= 0) {
484                         char iaidbuf[298];
485                         odhcpd_hexlify(iaidbuf, assign->clid_data, assign->clid_len);
486
487                         assign->managed_sock.stream.notify_read = managed_handle_pd_data;
488                         assign->managed_sock.stream.notify_state = managed_handle_pd_done;
489                         ustream_fd_init(&assign->managed_sock, fd);
490                         ustream_printf(&assign->managed_sock.stream, "%s,%x\n::/%d,0,0\n\n",
491                                         iaidbuf, assign->iaid, assign->length);
492                         ustream_write_pending(&assign->managed_sock.stream);
493                         assign->managed_size = -1;
494
495                         if (!(assign->flags & OAF_STATIC))
496                                 assign->valid_until = odhcpd_time() + 15;
497
498                         list_add(&assign->head, &iface->ia_assignments);
499
500                         /* Wait initial period of up to 250ms for immediate assignment */
501                         struct pollfd pfd = { .fd = fd, .events = POLLIN };
502                         poll(&pfd, 1, 250);
503                         managed_handle_pd_data(&assign->managed_sock.stream, 0);
504
505                         if (fcntl(fd, F_GETFL) >= 0 && assign->managed_size > 0)
506                                 return true;
507                 }
508
509                 return false;
510         } else if (iface->ia_addr_len < 1)
511                 return false;
512
513         /* Try honoring the hint first */
514         uint32_t current = 1, asize = (1 << (64 - assign->length)) - 1;
515         if (assign->assigned) {
516                 list_for_each_entry(c, &iface->ia_assignments, head) {
517                         if (c->length == 128 || c->length == 0)
518                                 continue;
519
520                         if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
521                                 list_add_tail(&assign->head, &c->head);
522                                 apply_lease(iface, assign, true);
523                                 return true;
524                         }
525
526                         if (c->assigned != 0)
527                                 current = (c->assigned + (1 << (64 - c->length)));
528                 }
529         }
530
531         /* Fallback to a variable assignment */
532         current = 1;
533         list_for_each_entry(c, &iface->ia_assignments, head) {
534                 if (c->length == 128 || c->length == 0)
535                         continue;
536
537                 current = (current + asize) & (~asize);
538                 if (current + asize < c->assigned) {
539                         assign->assigned = current;
540                         list_add_tail(&assign->head, &c->head);
541                         apply_lease(iface, assign, true);
542                         return true;
543                 }
544
545                 if (c->assigned != 0)
546                         current = (c->assigned + (1 << (64 - c->length)));
547         }
548
549         return false;
550 }
551
552 static bool assign_na(struct interface *iface, struct dhcpv6_assignment *assign)
553 {
554         /* Seed RNG with checksum of DUID */
555         uint32_t seed = 0;
556         for (size_t i = 0; i < assign->clid_len; ++i)
557                 seed += assign->clid_data[i];
558         srand(seed);
559
560         /* Try to assign up to 100x */
561         for (size_t i = 0; i < 100; ++i) {
562                 uint32_t try;
563                 do try = ((uint32_t)rand()) % 0x0fff; while (try < 0x100);
564
565                 struct dhcpv6_assignment *c;
566                 list_for_each_entry(c, &iface->ia_assignments, head) {
567                         if (c->length == 0)
568                                 continue;
569
570                         if (c->assigned > try || c->length != 128) {
571                                 assign->assigned = try;
572                                 list_add_tail(&assign->head, &c->head);
573                                 return true;
574                         } else if (c->assigned == try)
575                                 break;
576                 }
577         }
578
579         return false;
580 }
581
582 void dhcpv6_ia_preupdate(struct interface *iface)
583 {
584         if (iface->dhcpv6 != RELAYD_SERVER)
585                 return;
586
587         struct dhcpv6_assignment *c, *border = list_last_entry(
588                         &iface->ia_assignments, struct dhcpv6_assignment, head);
589
590         list_for_each_entry(c, &iface->ia_assignments, head)
591                 if (c != border && !iface->managed)
592                         apply_lease(iface, c, false);
593 }
594
595 void dhcpv6_ia_postupdate(struct interface *iface, time_t now)
596 {
597         if (iface->dhcpv6 != RELAYD_SERVER)
598                 return;
599
600         int minprefix = -1;
601         for (size_t i = 0; i < iface->ia_addr_len; ++i) {
602                 if (iface->ia_addr[i].preferred > (uint32_t)now &&
603                                 iface->ia_addr[i].prefix < 64 &&
604                                 iface->ia_addr[i].prefix > minprefix)
605                         minprefix = iface->ia_addr[i].prefix;
606         }
607
608         struct dhcpv6_assignment *border = list_last_entry(
609                         &iface->ia_assignments, struct dhcpv6_assignment, head);
610
611         if (minprefix > 32 && minprefix <= 64)
612                 border->assigned = 1U << (64 - minprefix);
613         else
614                 border->assigned = 0;
615
616         struct list_head reassign = LIST_HEAD_INIT(reassign);
617         struct dhcpv6_assignment *c, *d;
618         list_for_each_entry_safe(c, d, &iface->ia_assignments, head) {
619                 if (c->clid_len == 0 || (!INFINITE_VALID(c->valid_until) && c->valid_until < now) ||
620                                 c->managed_size)
621                         continue;
622
623                 if (c->length < 128 && c->assigned >= border->assigned && c != border)
624                         list_move(&c->head, &reassign);
625                 else if (c != border)
626                         apply_lease(iface, c, true);
627
628                 if (c->accept_reconf && c->reconf_cnt == 0) {
629                         c->reconf_cnt = 1;
630                         c->reconf_sent = now;
631                         send_reconf(iface, c);
632
633                         /* Leave all other assignments of that client alone */
634                         struct dhcpv6_assignment *a;
635                         list_for_each_entry(a, &iface->ia_assignments, head)
636                                 if (a != c && a->clid_len == c->clid_len &&
637                                                 !memcmp(a->clid_data, c->clid_data, a->clid_len))
638                                         c->reconf_cnt = INT_MAX;
639                 }
640         }
641
642         while (!list_empty(&reassign)) {
643                 c = list_first_entry(&reassign, struct dhcpv6_assignment, head);
644                 list_del(&c->head);
645                 if (!assign_pd(iface, c)) {
646                         c->assigned = 0;
647                         list_add(&c->head, &iface->ia_assignments);
648                 }
649         }
650
651         dhcpv6_write_statefile();
652 }
653
654 static void reconf_timer(struct uloop_timeout *event)
655 {
656         time_t now = odhcpd_time();
657         struct interface *iface;
658         list_for_each_entry(iface, &interfaces, head) {
659                 if (iface->dhcpv6 != RELAYD_SERVER || iface->ia_assignments.next == NULL)
660                         continue;
661
662                 struct dhcpv6_assignment *a, *n;
663                 list_for_each_entry_safe(a, n, &iface->ia_assignments, head) {
664                         if (!INFINITE_VALID(a->valid_until) && a->valid_until < now) {
665                                 if ((a->length < 128 && a->clid_len > 0) ||
666                                                 (a->length == 128 && a->clid_len == 0))
667                                         free_dhcpv6_assignment(a);
668
669                         } else if (a->reconf_cnt > 0 && a->reconf_cnt < 8 &&
670                                         now > a->reconf_sent + (1 << a->reconf_cnt)) {
671                                 ++a->reconf_cnt;
672                                 a->reconf_sent = now;
673                                 send_reconf(iface, a);
674                         }
675                 }
676         }
677         uloop_timeout_set(event, 2000);
678 }
679
680 static size_t append_reply(uint8_t *buf, size_t buflen, uint16_t status,
681                 const struct dhcpv6_ia_hdr *ia, struct dhcpv6_assignment *a,
682                 struct interface *iface, bool request)
683 {
684         if (buflen < sizeof(*ia) + sizeof(struct dhcpv6_ia_prefix))
685                 return 0;
686
687         struct dhcpv6_ia_hdr out = {ia->type, 0, ia->iaid, 0, 0};
688         size_t datalen = sizeof(out);
689         time_t now = odhcpd_time();
690
691         if (status) {
692                 struct __attribute__((packed)) {
693                         uint16_t type;
694                         uint16_t len;
695                         uint16_t value;
696                 } stat = {htons(DHCPV6_OPT_STATUS), htons(sizeof(stat) - 4),
697                                 htons(status)};
698
699                 memcpy(buf + datalen, &stat, sizeof(stat));
700                 datalen += sizeof(stat);
701         } else {
702                 if (a) {
703                         uint32_t leasetime;
704                         if (a->leasetime > 0)
705                                 leasetime = a->leasetime;
706                         else
707                                 leasetime = iface->dhcpv4_leasetime;
708
709                         if (leasetime == 0)
710                                 leasetime = 3600;
711                         else if (leasetime < 60)
712                                 leasetime = 60;
713
714                         uint32_t pref = leasetime;
715                         uint32_t valid = leasetime;
716
717                         struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
718                         size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
719                         size_t m = elect_addr(addrs, addrlen);
720
721                         for (size_t i = 0; i < addrlen; ++i) {
722                                 uint32_t prefix_pref = addrs[i].preferred;
723                                 uint32_t prefix_valid = addrs[i].valid;
724
725                                 if (!valid_addr(&addrs[i], now))
726                                         continue;
727
728                                 if (prefix_pref != UINT32_MAX)
729                                         prefix_pref -= now;
730
731                                 if (prefix_valid != UINT32_MAX)
732                                         prefix_valid -= now;
733
734                                 if (a->length < 128) {
735                                         struct dhcpv6_ia_prefix p = {
736                                                 .type = htons(DHCPV6_OPT_IA_PREFIX),
737                                                 .len = htons(sizeof(p) - 4),
738                                                 .preferred = htonl(prefix_pref),
739                                                 .valid = htonl(prefix_valid),
740                                                 .prefix = (a->managed_size) ? addrs[i].prefix : a->length,
741                                                 .addr = addrs[i].addr
742                                         };
743                                         p.addr.s6_addr32[1] |= htonl(a->assigned);
744
745                                         size_t entrlen = sizeof(p) - 4;
746
747                                         if (datalen + entrlen + 4 > buflen ||
748                                                         (a->assigned == 0 && a->managed_size == 0) ||
749                                                         (!a->managed_size && a->length <= addrs[i].prefix))
750                                                 continue;
751
752                                         memcpy(buf + datalen, &p, sizeof(p));
753                                         datalen += entrlen + 4;
754                                 } else {
755                                         struct dhcpv6_ia_addr n = {
756                                                 .type = htons(DHCPV6_OPT_IA_ADDR),
757                                                 .len = htons(sizeof(n) - 4),
758                                                 .addr = addrs[i].addr,
759                                                 .preferred = htonl(prefix_pref),
760                                                 .valid = htonl(prefix_valid)
761                                         };
762                                         n.addr.s6_addr32[3] = htonl(a->assigned);
763                                         size_t entrlen = sizeof(n) - 4;
764
765                                         if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) ||
766                                                         a->assigned == 0 ||
767                                                         datalen + entrlen + 4 > buflen)
768                                                 continue;
769
770                                         memcpy(buf + datalen, &n, sizeof(n));
771                                         datalen += entrlen + 4;
772                                 }
773
774                                 /* Calculate T1 / T2 based on non-deprecated addresses */
775                                 if (prefix_pref > 0) {
776                                         if (prefix_pref < pref)
777                                                 pref = prefix_pref;
778
779                                         if (prefix_valid < valid)
780                                                 valid = prefix_valid;
781                                 }
782                         }
783
784                         if (!INFINITE_VALID(a->valid_until))
785                                 /* UINT32_MAX is considered as infinite leasetime */
786                                 a->valid_until = (valid == UINT32_MAX) ? 0 : valid + now;
787
788                         out.t1 = htonl((pref == UINT32_MAX) ? pref : pref * 5 / 10);
789                         out.t2 = htonl((pref == UINT32_MAX) ? pref : pref * 8 / 10);
790
791                         if (!out.t1)
792                                 out.t1 = htonl(1);
793
794                         if (!out.t2)
795                                 out.t2 = htonl(1);
796                 }
797
798                 if (!request) {
799                         uint8_t *odata, *end = ((uint8_t*)ia) + htons(ia->len) + 4;
800                         uint16_t otype, olen;
801                         dhcpv6_for_each_option((uint8_t*)&ia[1], end, otype, olen, odata) {
802                                 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&odata[-4];
803                                 struct dhcpv6_ia_addr *n = (struct dhcpv6_ia_addr*)&odata[-4];
804                                 if ((otype != DHCPV6_OPT_IA_PREFIX || olen < sizeof(*p) - 4) &&
805                                                 (otype != DHCPV6_OPT_IA_ADDR || olen < sizeof(*n) - 4))
806                                         continue;
807
808                                 bool found = false;
809                                 if (a) {
810                                         struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
811                                         size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
812
813                                         for (size_t i = 0; i < addrlen; ++i) {
814                                                 if (!valid_addr(&addrs[i], now))
815                                                         continue;
816
817                                                 struct in6_addr addr = addrs[i].addr;
818                                                 if (ia->type == htons(DHCPV6_OPT_IA_PD)) {
819                                                         addr.s6_addr32[1] |= htonl(a->assigned);
820
821                                                         if (!memcmp(&p->addr, &addr, sizeof(addr)) &&
822                                                                         p->prefix == ((a->managed) ? addrs[i].prefix : a->length))
823                                                                 found = true;
824                                                 } else {
825                                                         addr.s6_addr32[3] = htonl(a->assigned);
826
827                                                         if (!memcmp(&n->addr, &addr, sizeof(addr)))
828                                                                 found = true;
829                                                 }
830                                         }
831                                 }
832
833                                 if (!found) {
834                                         if (otype == DHCPV6_OPT_IA_PREFIX) {
835                                                 struct dhcpv6_ia_prefix inv = {
836                                                         .type = htons(DHCPV6_OPT_IA_PREFIX),
837                                                         .len = htons(sizeof(inv) - 4),
838                                                         .preferred = 0,
839                                                         .valid = 0,
840                                                         .prefix = p->prefix,
841                                                         .addr = p->addr
842                                                 };
843
844                                                 if (datalen + sizeof(inv) > buflen)
845                                                         continue;
846
847                                                 memcpy(buf + datalen, &inv, sizeof(inv));
848                                                 datalen += sizeof(inv);
849                                         } else {
850                                                 struct dhcpv6_ia_addr inv = {
851                                                         .type = htons(DHCPV6_OPT_IA_ADDR),
852                                                         .len = htons(sizeof(inv) - 4),
853                                                         .addr = n->addr,
854                                                         .preferred = 0,
855                                                         .valid = 0
856                                                 };
857
858                                                 if (datalen + sizeof(inv) > buflen)
859                                                         continue;
860
861                                                 memcpy(buf + datalen, &inv, sizeof(inv));
862                                                 datalen += sizeof(inv);
863                                         }
864                                 }
865                         }
866                 }
867         }
868
869         out.len = htons(datalen - 4);
870         memcpy(buf, &out, sizeof(out));
871         return datalen;
872 }
873
874 static void dhcpv6_log(uint8_t msgtype, struct interface *iface, time_t now,
875                 const char *duidbuf, bool is_pd, struct dhcpv6_assignment *a, int code)
876 {
877         const char *type = "UNKNOWN";
878         const char *status = "UNKNOWN";
879
880         if (msgtype == DHCPV6_MSG_RENEW)
881                 return;
882
883         switch (msgtype) {
884         case DHCPV6_MSG_SOLICIT:
885                 type = "SOLICIT";
886                 break;
887         case DHCPV6_MSG_REQUEST:
888                 type = "REQUEST";
889                 break;
890         case DHCPV6_MSG_CONFIRM:
891                 type = "CONFIRM";
892                 break;
893         case DHCPV6_MSG_RENEW:
894                 type = "RENEW";
895                 break;
896         case DHCPV6_MSG_REBIND:
897                 type = "REBIND";
898                 break;
899         case DHCPV6_MSG_RELEASE:
900                 type = "RELEASE";
901                 break;
902         case DHCPV6_MSG_DECLINE:
903                 type = "DECLINE";
904                 break;
905         }
906
907         switch (code) {
908         case DHCPV6_STATUS_OK:
909                 status = "ok";
910                 break;
911         case DHCPV6_STATUS_NOADDRSAVAIL:
912                 status = "no addresses available";
913                 break;
914         case DHCPV6_STATUS_NOBINDING:
915                 status = "no binding";
916                 break;
917         case DHCPV6_STATUS_NOTONLINK:
918                 status = "not on-link";
919                 break;
920         case DHCPV6_STATUS_NOPREFIXAVAIL:
921                 status = "no prefix available";
922                 break;
923         }
924
925         char leasebuf[256] = "";
926
927         if (a) {
928                 struct odhcpd_ipaddr *addrs = (a->managed) ? a->managed : iface->ia_addr;
929                 size_t addrlen = (a->managed) ? (size_t)a->managed_size : iface->ia_addr_len;
930                 size_t lbsize = 0;
931                 size_t m = elect_addr(addrs, addrlen);
932                 char addrbuf[INET6_ADDRSTRLEN];
933
934                 for (size_t i = 0; i < addrlen; ++i) {
935                         if (!valid_addr(&addrs[i], now))
936                                 continue;
937
938                         struct in6_addr addr = addrs[i].addr;
939                         int prefix = a->managed ? addrs[i].prefix : a->length;
940                         if (prefix == 128) {
941                                 if (!ADDR_ENTRY_VALID_IA_ADDR(iface, i, m, addrs) ||
942                                                 a->assigned == 0)
943                                         continue;
944
945                                 addr.s6_addr32[3] = htonl(a->assigned);
946                         }
947                         else
948                                 addr.s6_addr32[1] |= htonl(a->assigned);
949
950                         inet_ntop(AF_INET6, &addr, addrbuf, sizeof(addrbuf));
951                         lbsize += snprintf(leasebuf + lbsize, sizeof(leasebuf) - lbsize, "%s/%d ", addrbuf, prefix);
952                 }
953         }
954
955         syslog(LOG_WARNING, "DHCPV6 %s %s from %s on %s: %s %s", type, (is_pd) ? "IA_PD" : "IA_NA",
956                         duidbuf, iface->ifname, status, leasebuf);
957 }
958
959 ssize_t dhcpv6_handle_ia(uint8_t *buf, size_t buflen, struct interface *iface,
960                 const struct sockaddr_in6 *addr, const void *data, const uint8_t *end)
961 {
962         time_t now = odhcpd_time();
963         size_t response_len = 0;
964         const struct dhcpv6_client_header *hdr = data;
965         uint8_t *start = (uint8_t*)&hdr[1], *odata;
966         uint16_t otype, olen;
967         /* Find and parse client-id and hostname */
968         bool accept_reconf = false;
969         uint8_t *clid_data = NULL, clid_len = 0, mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
970         char hostname[256];
971         size_t hostname_len = 0;
972         bool notonlink = false;
973         char duidbuf[261];
974
975         dhcpv6_for_each_option(start, end, otype, olen, odata) {
976                 if (otype == DHCPV6_OPT_CLIENTID) {
977                         clid_data = odata;
978                         clid_len = olen;
979
980                         if (olen == 14 && odata[0] == 0 && odata[1] == 1)
981                                 memcpy(mac, &odata[8], sizeof(mac));
982                         else if (olen == 10 && odata[0] == 0 && odata[1] == 3)
983                                 memcpy(mac, &odata[4], sizeof(mac));
984
985                         if (olen <= 130)
986                                 odhcpd_hexlify(duidbuf, odata, olen);
987                 } else if (otype == DHCPV6_OPT_FQDN && olen >= 2 && olen <= 255) {
988                         uint8_t fqdn_buf[256];
989                         memcpy(fqdn_buf, odata, olen);
990                         fqdn_buf[olen++] = 0;
991
992                         if (dn_expand(&fqdn_buf[1], &fqdn_buf[olen], &fqdn_buf[1], hostname, sizeof(hostname)) > 0)
993                                 hostname_len = strcspn(hostname, ".");
994                 } else if (otype == DHCPV6_OPT_RECONF_ACCEPT)
995                         accept_reconf = true;
996         }
997
998         if (!clid_data || !clid_len || clid_len > 130)
999                 goto out;
1000
1001         struct dhcpv6_assignment *first = NULL;
1002         dhcpv6_for_each_option(start, end, otype, olen, odata) {
1003                 bool is_pd = (otype == DHCPV6_OPT_IA_PD);
1004                 bool is_na = (otype == DHCPV6_OPT_IA_NA);
1005                 bool ia_addr_present = false;
1006                 if (!is_pd && !is_na)
1007                         continue;
1008
1009                 struct dhcpv6_ia_hdr *ia = (struct dhcpv6_ia_hdr*)&odata[-4];
1010                 size_t ia_response_len = 0;
1011                 uint8_t reqlen = (is_pd) ? 62 : 128;
1012                 uint32_t reqhint = 0;
1013
1014                 /* Parse request hint for IA-PD */
1015                 if (is_pd) {
1016                         uint8_t *sdata;
1017                         uint16_t stype, slen;
1018                         dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1019                                 if (stype != DHCPV6_OPT_IA_PREFIX || slen < sizeof(struct dhcpv6_ia_prefix) - 4)
1020                                         continue;
1021
1022                                 struct dhcpv6_ia_prefix *p = (struct dhcpv6_ia_prefix*)&sdata[-4];
1023                                 if (p->prefix) {
1024                                         reqlen = p->prefix;
1025                                         reqhint = ntohl(p->addr.s6_addr32[1]);
1026                                         if (reqlen > 32 && reqlen <= 64)
1027                                                 reqhint &= (1U << (64 - reqlen)) - 1;
1028                                 }
1029                         }
1030
1031                         if (reqlen > 64)
1032                                 reqlen = 64;
1033                 } else if (is_na) {
1034                         uint8_t *sdata;
1035                         uint16_t stype, slen;
1036                         dhcpv6_for_each_option(&ia[1], odata + olen, stype, slen, sdata) {
1037                                 if (stype != DHCPV6_OPT_IA_ADDR || slen < sizeof(struct dhcpv6_ia_addr) - 4)
1038                                         continue;
1039
1040                                 ia_addr_present = true;
1041                         }
1042                 }
1043
1044                 /* Find assignment */
1045                 struct dhcpv6_assignment *c, *a = NULL;
1046                 list_for_each_entry(c, &iface->ia_assignments, head) {
1047                         if (((c->clid_len == clid_len && !memcmp(c->clid_data, clid_data, clid_len)) ||
1048                                         (c->clid_len >= clid_len && !c->clid_data[0] && !c->clid_data[1]
1049                                                 && !memcmp(c->mac, mac, sizeof(mac)))) &&
1050                                         (c->iaid == ia->iaid || INFINITE_VALID(c->valid_until) || now < c->valid_until) &&
1051                                         ((is_pd && c->length <= 64) || (is_na && c->length == 128))) {
1052                                 a = c;
1053
1054                                 /* Reset state */
1055                                 apply_lease(iface, a, false);
1056                                 memcpy(a->clid_data, clid_data, clid_len);
1057                                 a->clid_len = clid_len;
1058                                 a->iaid = ia->iaid;
1059                                 a->peer = *addr;
1060                                 a->reconf_cnt = 0;
1061                                 a->reconf_sent = 0;
1062                                 break;
1063                         }
1064                 }
1065
1066                 /* Generic message handling */
1067                 uint16_t status = DHCPV6_STATUS_OK;
1068                 if (a && a->managed_size < 0)
1069                         return -1;
1070
1071                 if (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_REQUEST) {
1072                         bool assigned = !!a;
1073
1074                         if (!a && !iface->no_dynamic_dhcp) {
1075                                 /* Create new binding */
1076                                 a = calloc(1, sizeof(*a) + clid_len);
1077                                 if (a) {
1078                                         a->clid_len = clid_len;
1079                                         a->iaid = ia->iaid;
1080                                         a->length = reqlen;
1081                                         a->peer = *addr;
1082                                         a->assigned = reqhint;
1083                                         /* Set valid time to current time indicating  */
1084                                         /* assignment is not having infinite lifetime */
1085                                         a->valid_until = now;
1086
1087                                         if (first)
1088                                                 memcpy(a->key, first->key, sizeof(a->key));
1089                                         else
1090                                                 odhcpd_urandom(a->key, sizeof(a->key));
1091                                         memcpy(a->clid_data, clid_data, clid_len);
1092
1093                                         if (is_pd)
1094                                                 while (!(assigned = assign_pd(iface, a)) &&
1095                                                                 !a->managed_size && ++a->length <= 64);
1096                                         else
1097                                                 assigned = assign_na(iface, a);
1098
1099                                         if (a->managed_size && !assigned)
1100                                                 return -1;
1101                                 }
1102                         }
1103
1104                         if (!assigned || iface->ia_addr_len == 0)
1105                                 /* Set error status */
1106                                 status = (is_pd) ? DHCPV6_STATUS_NOPREFIXAVAIL : DHCPV6_STATUS_NOADDRSAVAIL;
1107                         else if (assigned && !first) {
1108                                 size_t handshake_len = 4;
1109                                 buf[0] = 0;
1110                                 buf[1] = DHCPV6_OPT_RECONF_ACCEPT;
1111                                 buf[2] = 0;
1112                                 buf[3] = 0;
1113
1114                                 if (hdr->msg_type == DHCPV6_MSG_REQUEST) {
1115                                         struct dhcpv6_auth_reconfigure auth = {
1116                                                 htons(DHCPV6_OPT_AUTH),
1117                                                 htons(sizeof(auth) - 4),
1118                                                 3, 1, 0,
1119                                                 {htonl(time(NULL)), htonl(++serial)},
1120                                                 1,
1121                                                 {0}
1122                                         };
1123                                         memcpy(auth.key, a->key, sizeof(a->key));
1124                                         memcpy(buf + handshake_len, &auth, sizeof(auth));
1125                                         handshake_len += sizeof(auth);
1126                                 }
1127
1128                                 buf += handshake_len;
1129                                 buflen -= handshake_len;
1130                                 response_len += handshake_len;
1131
1132                                 first = a;
1133                         }
1134
1135                         ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1136
1137                         /* Was only a solicitation: mark binding for removal */
1138                         if (assigned && hdr->msg_type == DHCPV6_MSG_SOLICIT) {
1139                                 a->flags &= ~OAF_BOUND;
1140
1141                                 if (!(a->flags & OAF_STATIC))
1142                                         a->valid_until = now;
1143                         } else if (assigned && hdr->msg_type == DHCPV6_MSG_REQUEST) {
1144                                 if (hostname_len > 0) {
1145                                         a->hostname = realloc(a->hostname, hostname_len + 1);
1146                                         if (a->hostname) {
1147                                                 memcpy(a->hostname, hostname, hostname_len);
1148                                                 a->hostname[hostname_len] = 0;
1149                                         }
1150                                 }
1151                                 a->accept_reconf = accept_reconf;
1152                                 a->flags |= OAF_BOUND;
1153                                 apply_lease(iface, a, true);
1154                         } else if (!assigned && a && a->managed_size == 0) {
1155                                 /* Cleanup failed assignment */
1156                                 free_dhcpv6_assignment(a);
1157                                 a = NULL;
1158                         }
1159                 } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1160                                 hdr->msg_type == DHCPV6_MSG_RELEASE ||
1161                                 hdr->msg_type == DHCPV6_MSG_REBIND ||
1162                                 hdr->msg_type == DHCPV6_MSG_DECLINE) {
1163                         if (!a && hdr->msg_type != DHCPV6_MSG_REBIND) {
1164                                 status = DHCPV6_STATUS_NOBINDING;
1165                                 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1166                         } else if (hdr->msg_type == DHCPV6_MSG_RENEW ||
1167                                         hdr->msg_type == DHCPV6_MSG_REBIND) {
1168                                 ia_response_len = append_reply(buf, buflen, status, ia, a, iface, false);
1169                                 if (a) {
1170                                         a->flags |= OAF_BOUND;
1171                                         apply_lease(iface, a, true);
1172                                 }
1173                         } else if (hdr->msg_type == DHCPV6_MSG_RELEASE) {
1174                                 if (!(a->flags & OAF_STATIC))
1175                                         a->valid_until = now - 1;
1176
1177                                 a->flags &= ~OAF_BOUND;
1178                                 apply_lease(iface, a, false);
1179                         } else if (hdr->msg_type == DHCPV6_MSG_DECLINE && a->length == 128) {
1180                                 a->flags &= ~OAF_BOUND;
1181
1182                                 if (!(a->flags & OAF_STATIC)) {
1183                                         a->clid_len = 0;
1184                                         a->valid_until = now + 3600; /* Block address for 1h */
1185                                 }
1186                         }
1187                 } else if (hdr->msg_type == DHCPV6_MSG_CONFIRM && ia_addr_present) {
1188                         /* Send NOTONLINK for CONFIRM with addr present so that clients restart connection */
1189                         status = DHCPV6_STATUS_NOTONLINK;
1190                         ia_response_len = append_reply(buf, buflen, status, ia, a, iface, true);
1191                         notonlink = true;
1192                 }
1193
1194                 buf += ia_response_len;
1195                 buflen -= ia_response_len;
1196                 response_len += ia_response_len;
1197                 dhcpv6_log(hdr->msg_type, iface, now, duidbuf, is_pd, a, status);
1198         }
1199
1200         if ((hdr->msg_type == DHCPV6_MSG_RELEASE || hdr->msg_type == DHCPV6_MSG_DECLINE || notonlink) &&
1201                         response_len + 6 < buflen) {
1202                 buf[0] = 0;
1203                 buf[1] = DHCPV6_OPT_STATUS;
1204                 buf[2] = 0;
1205                 buf[3] = 2;
1206                 buf[4] = 0;
1207                 buf[5] = (notonlink) ? DHCPV6_STATUS_NOTONLINK : DHCPV6_STATUS_OK;
1208                 response_len += 6;
1209         }
1210
1211         dhcpv6_write_statefile();
1212
1213 out:
1214         return response_len;
1215 }