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