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