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