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