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