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