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