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