blob: 86862ccaf525a8cc3eb7167c4b897d0e6f67a7e6 [file] [log] [blame]
Simon Kelleyd1ced3a2018-01-01 22:18:03 +00001/* dnsmasq is Copyright (c) 2000-2018 Simon Kelley
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
Simon Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley9e4abcb2004-01-22 19:47:41 +00008 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
Simon Kelley824af852008-02-12 20:43:05 +000012
Simon Kelley73a08a22009-02-05 20:28:08 +000013 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
Simon Kelley9e4abcb2004-01-22 19:47:41 +000015*/
16
Simon Kelley9e4abcb2004-01-22 19:47:41 +000017#include "dnsmasq.h"
18
Simon Kelley7622fc02009-06-04 20:32:05 +010019#ifdef HAVE_DHCP
20
Simon Kelley5aabfc72007-08-29 11:24:47 +010021static struct dhcp_lease *leases = NULL, *old_leases = NULL;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010022static int dns_dirty, file_dirty, leases_left;
Simon Kelley9e4abcb2004-01-22 19:47:41 +000023
Petr Menšík3a8b0f62017-04-23 14:12:37 +010024static int read_leases(time_t now, FILE *leasestream)
Simon Kelley9e4abcb2004-01-22 19:47:41 +000025{
Simon Kelley9e4abcb2004-01-22 19:47:41 +000026 unsigned long ei;
Simon Kelleycc921df2019-01-02 22:48:59 +000027 union all_addr addr;
Simon Kelley9e4abcb2004-01-22 19:47:41 +000028 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +010029 int clid_len, hw_len, hw_type;
Petr Menšík3a8b0f62017-04-23 14:12:37 +010030 int items;
31 char *domain = NULL;
32
33 *daemon->dhcp_buff3 = *daemon->dhcp_buff2 = '\0';
34
35 /* client-id max length is 255 which is 255*2 digits + 254 colons
36 borrow DNS packet buffer which is always larger than 1000 bytes
37
38 Check various buffers are big enough for the code below */
39
40#if (DHCP_BUFF_SZ < 255) || (MAXDNAME < 64) || (PACKETSZ+MAXDNAME+RRFIXEDSZ < 764)
41# error Buffer size breakage in leasefile parsing.
42#endif
43
44 while ((items=fscanf(leasestream, "%255s %255s", daemon->dhcp_buff3, daemon->dhcp_buff2)) == 2)
45 {
46 *daemon->namebuff = *daemon->dhcp_buff = *daemon->packet = '\0';
47 hw_len = hw_type = clid_len = 0;
48
49#ifdef HAVE_DHCP6
50 if (strcmp(daemon->dhcp_buff3, "duid") == 0)
51 {
52 daemon->duid_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, 130, NULL, NULL);
53 if (daemon->duid_len < 0)
54 return 0;
55 daemon->duid = safe_malloc(daemon->duid_len);
56 memcpy(daemon->duid, daemon->dhcp_buff2, daemon->duid_len);
57 continue;
58 }
59#endif
60
61 if (fscanf(leasestream, " %64s %255s %764s",
62 daemon->namebuff, daemon->dhcp_buff, daemon->packet) != 3)
Brian Haley28cfe362019-01-17 23:21:23 +000063 {
64 my_syslog(MS_DHCP | LOG_WARNING, _("ignoring invalid line in lease database: %s %s %s %s ..."),
65 daemon->dhcp_buff3, daemon->dhcp_buff2,
66 daemon->namebuff, daemon->dhcp_buff);
67 continue;
68 }
69
Simon Kelleycc921df2019-01-02 22:48:59 +000070 if (inet_pton(AF_INET, daemon->namebuff, &addr.addr4))
Petr Menšík3a8b0f62017-04-23 14:12:37 +010071 {
Simon Kelleycc921df2019-01-02 22:48:59 +000072 if ((lease = lease4_allocate(addr.addr4)))
Petr Menšík3a8b0f62017-04-23 14:12:37 +010073 domain = get_domain(lease->addr);
74
75 hw_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, DHCP_CHADDR_MAX, NULL, &hw_type);
76 /* For backwards compatibility, no explicit MAC address type means ether. */
77 if (hw_type == 0 && hw_len != 0)
78 hw_type = ARPHRD_ETHER;
79 }
80#ifdef HAVE_DHCP6
Simon Kelleycc921df2019-01-02 22:48:59 +000081 else if (inet_pton(AF_INET6, daemon->namebuff, &addr.addr6))
Petr Menšík3a8b0f62017-04-23 14:12:37 +010082 {
83 char *s = daemon->dhcp_buff2;
84 int lease_type = LEASE_NA;
85
86 if (s[0] == 'T')
87 {
88 lease_type = LEASE_TA;
89 s++;
90 }
91
Simon Kelleycc921df2019-01-02 22:48:59 +000092 if ((lease = lease6_allocate(&addr.addr6, lease_type)))
Petr Menšík3a8b0f62017-04-23 14:12:37 +010093 {
94 lease_set_iaid(lease, strtoul(s, NULL, 10));
Paul Maddock51e4eee2018-06-12 16:37:40 +010095 domain = get_domain6(&lease->addr6);
Petr Menšík3a8b0f62017-04-23 14:12:37 +010096 }
97 }
98#endif
99 else
Brian Haley28cfe362019-01-17 23:21:23 +0000100 {
101 my_syslog(MS_DHCP | LOG_WARNING, _("ignoring invalid line in lease database, bad address: %s"),
102 daemon->namebuff);
103 continue;
104 }
105
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100106
107 if (!lease)
108 die (_("too many stored leases"), NULL, EC_MISC);
109
110 if (strcmp(daemon->packet, "*") != 0)
111 clid_len = parse_hex(daemon->packet, (unsigned char *)daemon->packet, 255, NULL, NULL);
112
113 lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned char *)daemon->packet,
114 hw_len, hw_type, clid_len, now, 0);
115
116 if (strcmp(daemon->dhcp_buff, "*") != 0)
117 lease_set_hostname(lease, daemon->dhcp_buff, 0, domain, NULL);
118
119 ei = atol(daemon->dhcp_buff3);
120
121#ifdef HAVE_BROKEN_RTC
122 if (ei != 0)
123 lease->expires = (time_t)ei + now;
124 else
125 lease->expires = (time_t)0;
126 lease->length = ei;
127#else
128 /* strictly time_t is opaque, but this hack should work on all sane systems,
129 even when sizeof(time_t) == 8 */
130 lease->expires = (time_t)ei;
131#endif
132
133 /* set these correctly: the "old" events are generated later from
134 the startup synthesised SIGHUP. */
135 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED);
136
137 *daemon->dhcp_buff3 = *daemon->dhcp_buff2 = '\0';
138 }
139
140 return (items == 0 || items == EOF);
141}
142
143void lease_init(time_t now)
144{
Simon Kelley208b65c2006-08-05 21:41:37 +0100145 FILE *leasestream;
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100146
Simon Kelley3be34542004-09-11 19:12:13 +0100147 leases_left = daemon->dhcp_max;
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100148
Simon Kelley28866e92011-02-14 20:19:14 +0000149 if (option_bool(OPT_LEASE_RO))
Simon Kelley208b65c2006-08-05 21:41:37 +0100150 {
151 /* run "<lease_change_script> init" once to get the
152 initial state of the database. If leasefile-ro is
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100153 set without a script, we just do without any
Simon Kelley208b65c2006-08-05 21:41:37 +0100154 lease database. */
Simon Kelley1f15b812009-10-13 17:49:32 +0100155#ifdef HAVE_SCRIPT
156 if (daemon->lease_change_command)
Simon Kelley208b65c2006-08-05 21:41:37 +0100157 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100158 strcpy(daemon->dhcp_buff, daemon->lease_change_command);
159 strcat(daemon->dhcp_buff, " init");
160 leasestream = popen(daemon->dhcp_buff, "r");
Simon Kelley208b65c2006-08-05 21:41:37 +0100161 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100162 else
163#endif
164 {
165 file_dirty = dns_dirty = 0;
166 return;
167 }
168
Simon Kelley208b65c2006-08-05 21:41:37 +0100169 }
170 else
171 {
172 /* NOTE: need a+ mode to create file if it doesn't exist */
173 leasestream = daemon->lease_stream = fopen(daemon->lease_file, "a+");
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100174
Simon Kelley208b65c2006-08-05 21:41:37 +0100175 if (!leasestream)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100176 die(_("cannot open or create lease file %s: %s"), daemon->lease_file, EC_FILE);
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100177
Simon Kelley7622fc02009-06-04 20:32:05 +0100178 /* a+ mode leaves pointer at end. */
Simon Kelley208b65c2006-08-05 21:41:37 +0100179 rewind(leasestream);
180 }
Simon Kelleybf4e62c2016-07-22 21:37:59 +0100181
Simon Kelley208b65c2006-08-05 21:41:37 +0100182 if (leasestream)
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100183 {
Simon Kelley05f76da2017-05-09 22:57:04 +0100184 if (!read_leases(now, leasestream))
Brian Haley28cfe362019-01-17 23:21:23 +0000185 my_syslog(MS_DHCP | LOG_ERR, _("failed to parse lease database cleanly"));
186
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100187 if (ferror(leasestream))
188 die(_("failed to read lease file %s: %s"), daemon->lease_file, EC_FILE);
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100189 }
Simon Kelley208b65c2006-08-05 21:41:37 +0100190
Simon Kelley1f15b812009-10-13 17:49:32 +0100191#ifdef HAVE_SCRIPT
Simon Kelley208b65c2006-08-05 21:41:37 +0100192 if (!daemon->lease_stream)
193 {
194 int rc = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000195
Simon Kelley208b65c2006-08-05 21:41:37 +0100196 /* shell returns 127 for "command not found", 126 for bad permissions. */
197 if (!leasestream || (rc = pclose(leasestream)) == -1 || WEXITSTATUS(rc) == 127 || WEXITSTATUS(rc) == 126)
198 {
199 if (WEXITSTATUS(rc) == 127)
200 errno = ENOENT;
201 else if (WEXITSTATUS(rc) == 126)
202 errno = EACCES;
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100203
Simon Kelley5aabfc72007-08-29 11:24:47 +0100204 die(_("cannot run lease-init script %s: %s"), daemon->lease_change_command, EC_FILE);
Simon Kelley208b65c2006-08-05 21:41:37 +0100205 }
206
207 if (WEXITSTATUS(rc) != 0)
208 {
209 sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100210 die(_("lease-init script returned exit code %s"), daemon->dhcp_buff, WEXITSTATUS(rc) + EC_INIT_OFFSET);
Simon Kelley208b65c2006-08-05 21:41:37 +0100211 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000212 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100213#endif
Simon Kelley7cebd202006-05-06 14:13:33 +0100214
215 /* Some leases may have expired */
216 file_dirty = 0;
217 lease_prune(NULL, now);
218 dns_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000219}
220
Simon Kelley5aabfc72007-08-29 11:24:47 +0100221void lease_update_from_configs(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000222{
223 /* changes to the config may change current leases. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000224
Simon Kelley44a2a312004-03-10 20:04:35 +0000225 struct dhcp_lease *lease;
226 struct dhcp_config *config;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000227 char *name;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000228
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000229 for (lease = leases; lease; lease = lease->next)
Simon Kelley89500e32013-09-20 16:29:20 +0100230 if (lease->flags & (LEASE_TA | LEASE_NA))
231 continue;
232 else if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len,
233 lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) &&
234 (config->flags & CONFIG_NAME) &&
235 (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000236 lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NULL);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100237 else if ((name = host_from_dns(lease->addr)))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000238 lease_set_hostname(lease, name, 1, get_domain(lease->addr), NULL); /* updates auth flag only */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000239}
Rosen Penev50a28412017-06-27 22:27:02 +0100240
Simon Kelley5aabfc72007-08-29 11:24:47 +0100241static void ourprintf(int *errp, char *format, ...)
Simon Kelley7cebd202006-05-06 14:13:33 +0100242{
243 va_list ap;
244
245 va_start(ap, format);
246 if (!(*errp) && vfprintf(daemon->lease_stream, format, ap) < 0)
247 *errp = errno;
248 va_end(ap);
249}
250
Simon Kelley5aabfc72007-08-29 11:24:47 +0100251void lease_update_file(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000252{
253 struct dhcp_lease *lease;
Simon Kelley7cebd202006-05-06 14:13:33 +0100254 time_t next_event;
255 int i, err = 0;
256
Simon Kelley208b65c2006-08-05 21:41:37 +0100257 if (file_dirty != 0 && daemon->lease_stream)
Simon Kelley44a2a312004-03-10 20:04:35 +0000258 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000259 errno = 0;
260 rewind(daemon->lease_stream);
261 if (errno != 0 || ftruncate(fileno(daemon->lease_stream), 0) != 0)
Simon Kelley7cebd202006-05-06 14:13:33 +0100262 err = errno;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000263
264 for (lease = leases; lease; lease = lease->next)
265 {
Simon Kelleyc72daea2012-01-05 21:33:27 +0000266
267#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000268 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000269 continue;
270#endif
271
Simon Kelley44a2a312004-03-10 20:04:35 +0000272#ifdef HAVE_BROKEN_RTC
Simon Kelley5aabfc72007-08-29 11:24:47 +0100273 ourprintf(&err, "%u ", lease->length);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100274#else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100275 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100276#endif
Simon Kelleyc72daea2012-01-05 21:33:27 +0000277
Simon Kelley7cebd202006-05-06 14:13:33 +0100278 if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100279 ourprintf(&err, "%.2x-", lease->hwaddr_type);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100280 for (i = 0; i < lease->hwaddr_len; i++)
281 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100282 ourprintf(&err, "%.2x", lease->hwaddr[i]);
Simon Kelley7cebd202006-05-06 14:13:33 +0100283 if (i != lease->hwaddr_len - 1)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100284 ourprintf(&err, ":");
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100285 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000286
287 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100288
Simon Kelleyc72daea2012-01-05 21:33:27 +0000289 ourprintf(&err, " %s ", daemon->addrbuff);
Simon Kelley1f15b812009-10-13 17:49:32 +0100290 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
291
Simon Kelley0a852542005-03-23 20:28:59 +0000292 if (lease->clid && lease->clid_len != 0)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000293 {
294 for (i = 0; i < lease->clid_len - 1; i++)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100295 ourprintf(&err, "%.2x:", lease->clid[i]);
296 ourprintf(&err, "%.2x\n", lease->clid[i]);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000297 }
298 else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100299 ourprintf(&err, "*\n");
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000300 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100301
Simon Kelleyc72daea2012-01-05 21:33:27 +0000302#ifdef HAVE_DHCP6
303 if (daemon->duid)
304 {
305 ourprintf(&err, "duid ");
306 for (i = 0; i < daemon->duid_len - 1; i++)
307 ourprintf(&err, "%.2x:", daemon->duid[i]);
308 ourprintf(&err, "%.2x\n", daemon->duid[i]);
309
310 for (lease = leases; lease; lease = lease->next)
311 {
312
Simon Kelley4cb1b322012-02-06 14:30:41 +0000313 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000314 continue;
315
316#ifdef HAVE_BROKEN_RTC
317 ourprintf(&err, "%u ", lease->length);
318#else
319 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
320#endif
321
Simon Kelley89500e32013-09-20 16:29:20 +0100322 inet_ntop(AF_INET6, &lease->addr6, daemon->addrbuff, ADDRSTRLEN);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000323
Simon Kelley4cb1b322012-02-06 14:30:41 +0000324 ourprintf(&err, "%s%u %s ", (lease->flags & LEASE_TA) ? "T" : "",
Simon Kelley89500e32013-09-20 16:29:20 +0100325 lease->iaid, daemon->addrbuff);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000326 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
327
328 if (lease->clid && lease->clid_len != 0)
329 {
330 for (i = 0; i < lease->clid_len - 1; i++)
331 ourprintf(&err, "%.2x:", lease->clid[i]);
332 ourprintf(&err, "%.2x\n", lease->clid[i]);
333 }
334 else
335 ourprintf(&err, "*\n");
336 }
337 }
338#endif
339
Simon Kelley7cebd202006-05-06 14:13:33 +0100340 if (fflush(daemon->lease_stream) != 0 ||
341 fsync(fileno(daemon->lease_stream)) < 0)
342 err = errno;
343
344 if (!err)
345 file_dirty = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000346 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100347
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000348 /* Set alarm for when the first lease expires. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000349 next_event = 0;
350
351#ifdef HAVE_DHCP6
Simon Kelley353ae4d2012-03-19 20:07:51 +0000352 /* do timed RAs and determine when the next is, also pings to potential SLAAC addresses */
Simon Kelley1f776932012-12-16 19:46:08 +0000353 if (daemon->doing_ra)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000354 {
Simon Kelley919dd7c2012-05-12 15:23:09 +0100355 time_t event;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000356
Simon Kelley919dd7c2012-05-12 15:23:09 +0100357 if ((event = periodic_slaac(now, leases)) != 0)
358 {
359 if (next_event == 0 || difftime(next_event, event) > 0.0)
360 next_event = event;
361 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000362
Simon Kelley919dd7c2012-05-12 15:23:09 +0100363 if ((event = periodic_ra(now)) != 0)
364 {
365 if (next_event == 0 || difftime(next_event, event) > 0.0)
366 next_event = event;
367 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000368 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000369#endif
370
371 for (lease = leases; lease; lease = lease->next)
Simon Kelley7cebd202006-05-06 14:13:33 +0100372 if (lease->expires != 0 &&
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000373 (next_event == 0 || difftime(next_event, lease->expires) > 0.0))
374 next_event = lease->expires;
Simon Kelley7cebd202006-05-06 14:13:33 +0100375
376 if (err)
377 {
378 if (next_event == 0 || difftime(next_event, LEASE_RETRY + now) > 0.0)
379 next_event = LEASE_RETRY + now;
380
Simon Kelley7622fc02009-06-04 20:32:05 +0100381 my_syslog(MS_DHCP | LOG_ERR, _("failed to write %s: %s (retry in %us)"),
Simon Kelleyf2621c72007-04-29 19:47:21 +0100382 daemon->lease_file, strerror(err),
383 (unsigned int)difftime(next_event, now));
Simon Kelley7cebd202006-05-06 14:13:33 +0100384 }
385
Simon Kelley353ae4d2012-03-19 20:07:51 +0000386 send_alarm(next_event, now);
Simon Kelley44a2a312004-03-10 20:04:35 +0000387}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000388
Simon Kelley801ca9a2012-03-06 19:30:17 +0000389
Simon Kelley3f2873d2013-05-14 11:28:47 +0100390static int find_interface_v4(struct in_addr local, int if_index, char *label,
Simon Kelley801ca9a2012-03-06 19:30:17 +0000391 struct in_addr netmask, struct in_addr broadcast, void *vparam)
Simon Kelley44a2a312004-03-10 20:04:35 +0000392{
393 struct dhcp_lease *lease;
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100394 int prefix = netmask_length(netmask);
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800395
Simon Kelley3f2873d2013-05-14 11:28:47 +0100396 (void) label;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000397 (void) broadcast;
398 (void) vparam;
399
400 for (lease = leases; lease; lease = lease->next)
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100401 if (!(lease->flags & (LEASE_TA | LEASE_NA)) &&
402 is_same_net(local, lease->addr, netmask) &&
403 prefix > lease->new_prefixlen)
404 {
405 lease->new_interface = if_index;
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800406 lease->new_prefixlen = prefix;
407 }
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800408
Simon Kelley801ca9a2012-03-06 19:30:17 +0000409 return 1;
410}
411
412#ifdef HAVE_DHCP6
413static int find_interface_v6(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000414 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000415 int preferred, int valid, void *vparam)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000416{
417 struct dhcp_lease *lease;
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800418
Simon Kelley353ae4d2012-03-19 20:07:51 +0000419 (void)scope;
Simon Kelleybad7b872012-12-20 22:00:39 +0000420 (void)flags;
Simon Kelley1f776932012-12-16 19:46:08 +0000421 (void)preferred;
422 (void)valid;
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800423 (void)vparam;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000424
425 for (lease = leases; lease; lease = lease->next)
426 if ((lease->flags & (LEASE_TA | LEASE_NA)))
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800427 if (is_same_net6(local, &lease->addr6, prefix) && prefix > lease->new_prefixlen) {
428 /* save prefix length for comparison, as we might get shorter matching
429 * prefix in upcoming netlink GETADDR responses
430 * */
431 lease->new_interface = if_index;
432 lease->new_prefixlen = prefix;
433 }
434
Simon Kelley801ca9a2012-03-06 19:30:17 +0000435 return 1;
436}
Simon Kelley353ae4d2012-03-19 20:07:51 +0000437
438void lease_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface)
439{
Simon Kelley5ef33272012-03-30 15:10:28 +0100440 /* We may be doing RA but not DHCPv4, in which case the lease
441 database may not exist and we have nothing to do anyway */
442 if (daemon->dhcp)
443 slaac_ping_reply(sender, packet, interface, leases);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000444}
445
Simon Kelley0c050242012-12-22 22:13:19 +0000446void lease_update_slaac(time_t now)
447{
Josh Soref730c6742017-02-06 16:14:04 +0000448 /* Called when we construct a new RA-names context, to add putative
Simon Kelley0c050242012-12-22 22:13:19 +0000449 new SLAAC addresses to existing leases. */
450
451 struct dhcp_lease *lease;
452
453 if (daemon->dhcp)
454 for (lease = leases; lease; lease = lease->next)
455 slaac_add_addrs(lease, now, 0);
456}
457
Simon Kelley801ca9a2012-03-06 19:30:17 +0000458#endif
459
460
461/* Find interfaces associated with leases at start-up. This gets updated as
462 we do DHCP transactions, but information about directly-connected subnets
463 is useful from scrips and necessary for determining SLAAC addresses from
464 start-time. */
Simon Kelley8b372702012-03-09 17:45:10 +0000465void lease_find_interfaces(time_t now)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000466{
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100467 struct dhcp_lease *lease;
468
469 for (lease = leases; lease; lease = lease->next)
470 lease->new_prefixlen = lease->new_interface = 0;
471
Simon Kelley353ae4d2012-03-19 20:07:51 +0000472 iface_enumerate(AF_INET, &now, find_interface_v4);
473#ifdef HAVE_DHCP6
474 iface_enumerate(AF_INET6, &now, find_interface_v6);
Simon Kelley3511a922013-11-07 10:28:11 +0000475#endif
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100476
477 for (lease = leases; lease; lease = lease->next)
478 if (lease->new_interface != 0)
479 lease_set_interface(lease, lease->new_interface, now);
Simon Kelley3511a922013-11-07 10:28:11 +0000480}
Simon Kelley8b372702012-03-09 17:45:10 +0000481
Simon Kelley3511a922013-11-07 10:28:11 +0000482#ifdef HAVE_DHCP6
483void lease_make_duid(time_t now)
484{
Simon Kelley8b372702012-03-09 17:45:10 +0000485 /* If we're not doing DHCPv6, and there are not v6 leases, don't add the DUID to the database */
Simon Kelley3511a922013-11-07 10:28:11 +0000486 if (!daemon->duid && daemon->doing_dhcp6)
Simon Kelley8b372702012-03-09 17:45:10 +0000487 {
488 file_dirty = 1;
489 make_duid(now);
490 }
Simon Kelley801ca9a2012-03-06 19:30:17 +0000491}
Simon Kelley3511a922013-11-07 10:28:11 +0000492#endif
493
Simon Kelley801ca9a2012-03-06 19:30:17 +0000494
495
496
Simon Kelley353ae4d2012-03-19 20:07:51 +0000497void lease_update_dns(int force)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000498{
499 struct dhcp_lease *lease;
500
Simon Kelley353ae4d2012-03-19 20:07:51 +0000501 if (daemon->port != 0 && (dns_dirty || force))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000502 {
Simon Kelley8ff55672012-12-09 21:09:01 +0000503#ifndef HAVE_BROKEN_RTC
Simon Kelleye1ff4192012-12-09 17:08:47 +0000504 /* force transfer to authoritative secondaries */
505 daemon->soa_sn++;
Simon Kelley8ff55672012-12-09 21:09:01 +0000506#endif
Simon Kelleye1ff4192012-12-09 17:08:47 +0000507
Simon Kelley801ca9a2012-03-06 19:30:17 +0000508 cache_unhash_dhcp();
509
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000510 for (lease = leases; lease; lease = lease->next)
511 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000512 int prot = AF_INET;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000513
Simon Kelley4cb1b322012-02-06 14:30:41 +0000514#ifdef HAVE_DHCP6
515 if (lease->flags & (LEASE_TA | LEASE_NA))
516 prot = AF_INET6;
Simon Kelleyf444cdd2012-03-07 10:15:57 +0000517 else if (lease->hostname || lease->fqdn)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000518 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000519 struct slaac_address *slaac;
520
521 for (slaac = lease->slaac_address; slaac; slaac = slaac->next)
522 if (slaac->backoff == 0)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000523 {
Simon Kelley801ca9a2012-03-06 19:30:17 +0000524 if (lease->fqdn)
Simon Kelleycc921df2019-01-02 22:48:59 +0000525 cache_add_dhcp_entry(lease->fqdn, AF_INET6, (union all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000526 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelleycc921df2019-01-02 22:48:59 +0000527 cache_add_dhcp_entry(lease->hostname, AF_INET6, (union all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000528 }
529 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000530
Simon Kelley9009d742008-11-14 20:04:27 +0000531 if (lease->fqdn)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000532 cache_add_dhcp_entry(lease->fqdn, prot,
Simon Kelleycc921df2019-01-02 22:48:59 +0000533 prot == AF_INET ? (union all_addr *)&lease->addr : (union all_addr *)&lease->addr6,
Simon Kelley4cb1b322012-02-06 14:30:41 +0000534 lease->expires);
Simon Kelley9009d742008-11-14 20:04:27 +0000535
Simon Kelley28866e92011-02-14 20:19:14 +0000536 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000537 cache_add_dhcp_entry(lease->hostname, prot,
Simon Kelleycc921df2019-01-02 22:48:59 +0000538 prot == AF_INET ? (union all_addr *)&lease->addr : (union all_addr *)&lease->addr6,
Simon Kelley4cb1b322012-02-06 14:30:41 +0000539 lease->expires);
Simon Kelley91543f42013-09-23 12:41:20 +0100540
541#else
542 if (lease->fqdn)
Simon Kelleycc921df2019-01-02 22:48:59 +0000543 cache_add_dhcp_entry(lease->fqdn, prot, (union all_addr *)&lease->addr, lease->expires);
Simon Kelley91543f42013-09-23 12:41:20 +0100544
545 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelleycc921df2019-01-02 22:48:59 +0000546 cache_add_dhcp_entry(lease->hostname, prot, (union all_addr *)&lease->addr, lease->expires);
Simon Kelley91543f42013-09-23 12:41:20 +0100547#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000548 }
549
550 dns_dirty = 0;
551 }
552}
553
554void lease_prune(struct dhcp_lease *target, time_t now)
555{
556 struct dhcp_lease *lease, *tmp, **up;
557
558 for (lease = leases, up = &leases; lease; lease = tmp)
559 {
560 tmp = lease->next;
561 if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target)
562 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100563 file_dirty = 1;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000564 if (lease->hostname)
Simon Kelley7cebd202006-05-06 14:13:33 +0100565 dns_dirty = 1;
Julian Kornbergeraba8bbb2018-07-21 21:55:08 +0100566
567 daemon->metrics[lease->addr.s_addr ? METRIC_LEASES_PRUNED_4 : METRIC_LEASES_PRUNED_6]++;
568
Simon Kelleyc72daea2012-01-05 21:33:27 +0000569 *up = lease->next; /* unlink */
Simon Kelley7cebd202006-05-06 14:13:33 +0100570
571 /* Put on old_leases list 'till we
572 can run the script */
573 lease->next = old_leases;
574 old_leases = lease;
575
Simon Kelley44a2a312004-03-10 20:04:35 +0000576 leases_left++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000577 }
578 else
579 up = &lease->next;
580 }
581}
582
583
Simon Kelleycdeda282006-03-16 20:16:06 +0000584struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,
Simon Kelley0a852542005-03-23 20:28:59 +0000585 unsigned char *clid, int clid_len)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000586{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000587 struct dhcp_lease *lease;
588
Simon Kelley0a852542005-03-23 20:28:59 +0000589 if (clid)
590 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000591 {
592#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000593 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000594 continue;
595#endif
596 if (lease->clid && clid_len == lease->clid_len &&
597 memcmp(clid, lease->clid, clid_len) == 0)
598 return lease;
599 }
Simon Kelley0a852542005-03-23 20:28:59 +0000600
601 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000602 {
603#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000604 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000605 continue;
606#endif
607 if ((!lease->clid || !clid) &&
608 hw_len != 0 &&
609 lease->hwaddr_len == hw_len &&
610 lease->hwaddr_type == hw_type &&
611 memcmp(hwaddr, lease->hwaddr, hw_len) == 0)
612 return lease;
613 }
614
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000615 return NULL;
616}
617
618struct dhcp_lease *lease_find_by_addr(struct in_addr addr)
619{
620 struct dhcp_lease *lease;
621
622 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000623 {
624#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000625 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000626 continue;
627#endif
628 if (lease->addr.s_addr == addr.s_addr)
629 return lease;
630 }
631
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000632 return NULL;
633}
634
Simon Kelley52b92f42012-01-22 16:05:15 +0000635#ifdef HAVE_DHCP6
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000636/* find address for {CLID, IAID, address} */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000637struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len,
638 int lease_type, int iaid, struct in6_addr *addr)
Simon Kelley52b92f42012-01-22 16:05:15 +0000639{
640 struct dhcp_lease *lease;
641
642 for (lease = leases; lease; lease = lease->next)
643 {
Simon Kelley89500e32013-09-20 16:29:20 +0100644 if (!(lease->flags & lease_type) || lease->iaid != iaid)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000645 continue;
Simon Kelley52b92f42012-01-22 16:05:15 +0000646
Simon Kelley89500e32013-09-20 16:29:20 +0100647 if (!IN6_ARE_ADDR_EQUAL(&lease->addr6, addr))
Simon Kelley4cb1b322012-02-06 14:30:41 +0000648 continue;
649
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000650 if ((clid_len != lease->clid_len ||
Simon Kelley4cb1b322012-02-06 14:30:41 +0000651 memcmp(clid, lease->clid, clid_len) != 0))
652 continue;
653
Simon Kelley8b460612012-09-08 21:47:28 +0100654 return lease;
Simon Kelley52b92f42012-01-22 16:05:15 +0000655 }
656
657 return NULL;
658}
659
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000660/* reset "USED flags */
661void lease6_reset(void)
Simon Kelley8b460612012-09-08 21:47:28 +0100662{
663 struct dhcp_lease *lease;
664
665 for (lease = leases; lease; lease = lease->next)
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000666 lease->flags &= ~LEASE_USED;
667}
668
669/* enumerate all leases belonging to {CLID, IAID} */
670struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_type, unsigned char *clid, int clid_len, int iaid)
671{
672 struct dhcp_lease *lease;
673
674 if (!first)
675 first = leases;
Simon Kelley27cb3142013-04-02 20:06:39 +0100676 else
677 first = first->next;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000678
679 for (lease = first; lease; lease = lease->next)
Simon Kelley8b460612012-09-08 21:47:28 +0100680 {
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000681 if (lease->flags & LEASE_USED)
682 continue;
683
Simon Kelley89500e32013-09-20 16:29:20 +0100684 if (!(lease->flags & lease_type) || lease->iaid != iaid)
Simon Kelley8b460612012-09-08 21:47:28 +0100685 continue;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000686
687 if ((clid_len != lease->clid_len ||
688 memcmp(clid, lease->clid, clid_len) != 0))
689 continue;
690
691 return lease;
Simon Kelley8b460612012-09-08 21:47:28 +0100692 }
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000693
694 return NULL;
Simon Kelley8b460612012-09-08 21:47:28 +0100695}
696
Simon Kelley52b92f42012-01-22 16:05:15 +0000697struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr)
698{
699 struct dhcp_lease *lease;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000700
Simon Kelley52b92f42012-01-22 16:05:15 +0000701 for (lease = leases; lease; lease = lease->next)
702 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000703 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelley52b92f42012-01-22 16:05:15 +0000704 continue;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000705
Simon Kelley89500e32013-09-20 16:29:20 +0100706 if (is_same_net6(&lease->addr6, net, prefix) &&
707 (prefix == 128 || addr6part(&lease->addr6) == addr))
Simon Kelley52b92f42012-01-22 16:05:15 +0000708 return lease;
709 }
710
711 return NULL;
Simon Kelley07933802012-02-14 20:55:25 +0000712}
713
714/* Find largest assigned address in context */
715u64 lease_find_max_addr6(struct dhcp_context *context)
716{
717 struct dhcp_lease *lease;
718 u64 addr = addr6part(&context->start6);
719
720 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
721 for (lease = leases; lease; lease = lease->next)
722 {
Simon Kelley07933802012-02-14 20:55:25 +0000723 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
724 continue;
Simon Kelley6caacac2012-02-15 21:58:33 +0000725
Simon Kelley89500e32013-09-20 16:29:20 +0100726 if (is_same_net6(&lease->addr6, &context->start6, 64) &&
727 addr6part(&lease->addr6) > addr6part(&context->start6) &&
728 addr6part(&lease->addr6) <= addr6part(&context->end6) &&
729 addr6part(&lease->addr6) > addr)
730 addr = addr6part(&lease->addr6);
Simon Kelley07933802012-02-14 20:55:25 +0000731 }
732
733 return addr;
734}
735
Simon Kelley52b92f42012-01-22 16:05:15 +0000736#endif
737
Simon Kelley7de060b2011-08-26 17:24:52 +0100738/* Find largest assigned address in context */
739struct in_addr lease_find_max_addr(struct dhcp_context *context)
740{
741 struct dhcp_lease *lease;
742 struct in_addr addr = context->start;
743
744 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
745 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000746 {
747#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000748 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000749 continue;
750#endif
751 if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) &&
752 ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) &&
753 ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr)))
754 addr = lease->addr;
755 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100756
757 return addr;
758}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000759
Simon Kelleyc72daea2012-01-05 21:33:27 +0000760static struct dhcp_lease *lease_allocate(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000761{
762 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100763 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000764 return NULL;
765
Simon Kelley7cebd202006-05-06 14:13:33 +0100766 memset(lease, 0, sizeof(struct dhcp_lease));
Simon Kelley4cb1b322012-02-06 14:30:41 +0000767 lease->flags = LEASE_NEW;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000768 lease->expires = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100769#ifdef HAVE_BROKEN_RTC
770 lease->length = 0xffffffff; /* illegal value */
771#endif
Simon Kelley89500e32013-09-20 16:29:20 +0100772 lease->hwaddr_len = 256; /* illegal value */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000773 lease->next = leases;
774 leases = lease;
775
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100776 file_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000777 leases_left--;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000778
779 return lease;
780}
781
Simon Kelley52b92f42012-01-22 16:05:15 +0000782struct dhcp_lease *lease4_allocate(struct in_addr addr)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000783{
784 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100785 if (lease)
Julian Kornbergeraba8bbb2018-07-21 21:55:08 +0100786 {
787 lease->addr = addr;
788 daemon->metrics[METRIC_LEASES_ALLOCATED_4]++;
789 }
Simon Kelley89500e32013-09-20 16:29:20 +0100790
Simon Kelleyc72daea2012-01-05 21:33:27 +0000791 return lease;
792}
793
794#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000795struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000796{
797 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100798
799 if (lease)
800 {
Simon Kelley89500e32013-09-20 16:29:20 +0100801 lease->addr6 = *addrp;
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100802 lease->flags |= lease_type;
Simon Kelley89500e32013-09-20 16:29:20 +0100803 lease->iaid = 0;
Julian Kornbergeraba8bbb2018-07-21 21:55:08 +0100804
805 daemon->metrics[METRIC_LEASES_ALLOCATED_6]++;
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100806 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000807
808 return lease;
809}
810#endif
811
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100812void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000813{
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000814 time_t exp;
815
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100816 if (len == 0xffffffff)
817 {
818 exp = 0;
819 len = 0;
820 }
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000821 else
822 {
823 exp = now + (time_t)len;
824 /* Check for 2038 overflow. Make the lease
Josh Soref730c6742017-02-06 16:14:04 +0000825 infinite in that case, as the least disruptive
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000826 thing we can do. */
827 if (difftime(exp, now) <= 0.0)
828 exp = 0;
829 }
830
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000831 if (exp != lease->expires)
Simon Kelley0a852542005-03-23 20:28:59 +0000832 {
Simon Kelley0a852542005-03-23 20:28:59 +0000833 dns_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100834 lease->expires = exp;
835#ifndef HAVE_BROKEN_RTC
Simon Kelley4cb1b322012-02-06 14:30:41 +0000836 lease->flags |= LEASE_AUX_CHANGED;
837 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100838#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000839 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100840
841#ifdef HAVE_BROKEN_RTC
842 if (len != lease->length)
843 {
844 lease->length = len;
Simon Kelleyb7f40202012-02-29 21:43:37 +0000845 lease->flags |= LEASE_AUX_CHANGED;
846 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100847 }
848#endif
849}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000850
Simon Kelley89500e32013-09-20 16:29:20 +0100851#ifdef HAVE_DHCP6
852void lease_set_iaid(struct dhcp_lease *lease, int iaid)
853{
854 if (lease->iaid != iaid)
855 {
856 lease->iaid = iaid;
857 lease->flags |= LEASE_CHANGED;
858 }
859}
860#endif
861
Nicolas Cavallari64bcff12015-04-28 21:55:18 +0100862void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr,
863 const unsigned char *clid, int hw_len, int hw_type,
864 int clid_len, time_t now, int force)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000865{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000866#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100867 int change = force;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000868 lease->flags |= LEASE_HAVE_HWADDR;
869#endif
870
Simon Kelleya9ab7322012-04-28 11:29:37 +0100871 (void)force;
Vladislav Grishenko408c3682013-09-24 16:18:49 +0100872 (void)now;
Simon Kelleya9ab7322012-04-28 11:29:37 +0100873
Simon Kelleycdeda282006-03-16 20:16:06 +0000874 if (hw_len != lease->hwaddr_len ||
875 hw_type != lease->hwaddr_type ||
Simon Kelley7cebd202006-05-06 14:13:33 +0100876 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000877 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000878 if (hw_len != 0)
879 memcpy(lease->hwaddr, hwaddr, hw_len);
Simon Kelleycdeda282006-03-16 20:16:06 +0000880 lease->hwaddr_len = hw_len;
881 lease->hwaddr_type = hw_type;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000882 lease->flags |= LEASE_CHANGED;
883 file_dirty = 1; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000884 }
Simon Kelley0a852542005-03-23 20:28:59 +0000885
886 /* only update clid when one is available, stops packets
887 without a clid removing the record. Lease init uses
888 clid_len == 0 for no clid. */
889 if (clid_len != 0 && clid)
890 {
891 if (!lease->clid)
892 lease->clid_len = 0;
893
894 if (lease->clid_len != clid_len)
895 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000896 lease->flags |= LEASE_AUX_CHANGED;
897 file_dirty = 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100898 free(lease->clid);
899 if (!(lease->clid = whine_malloc(clid_len)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100900 return;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000901#ifdef HAVE_DHCP6
902 change = 1;
903#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000904 }
905 else if (memcmp(lease->clid, clid, clid_len) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000906 {
907 lease->flags |= LEASE_AUX_CHANGED;
908 file_dirty = 1;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000909#ifdef HAVE_DHCP6
910 change = 1;
911#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +0000912 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000913
Simon Kelley0a852542005-03-23 20:28:59 +0000914 lease->clid_len = clid_len;
915 memcpy(lease->clid, clid, clid_len);
916 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000917
918#ifdef HAVE_DHCP6
919 if (change)
Simon Kelleya9ab7322012-04-28 11:29:37 +0100920 slaac_add_addrs(lease, now, force);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000921#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000922}
923
Simon Kelley9009d742008-11-14 20:04:27 +0000924static void kill_name(struct dhcp_lease *lease)
925{
926 /* run script to say we lost our old name */
927
928 /* this shouldn't happen unless updates are very quick and the
929 script very slow, we just avoid a memory leak if it does. */
930 free(lease->old_hostname);
931
932 /* If we know the fqdn, pass that. The helper will derive the
Simon Kelley4cb1b322012-02-06 14:30:41 +0000933 unqualified name from it, free the unqualified name here. */
Simon Kelley9009d742008-11-14 20:04:27 +0000934
935 if (lease->fqdn)
936 {
937 lease->old_hostname = lease->fqdn;
938 free(lease->hostname);
939 }
940 else
941 lease->old_hostname = lease->hostname;
942
943 lease->hostname = lease->fqdn = NULL;
944}
945
Nicolas Cavallari64bcff12015-04-28 21:55:18 +0100946void lease_set_hostname(struct dhcp_lease *lease, const char *name, int auth, char *domain, char *config_domain)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000947{
948 struct dhcp_lease *lease_tmp;
949 char *new_name = NULL, *new_fqdn = NULL;
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000950
951 if (config_domain && (!domain || !hostname_isequal(domain, config_domain)))
952 my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, name);
Simon Kelley9009d742008-11-14 20:04:27 +0000953
Simon Kelleya2226412004-05-13 20:27:08 +0100954 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000955 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000956 if (auth)
957 lease->flags |= LEASE_AUTH_NAME;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000958 return;
959 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100960
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000961 if (!name && !lease->hostname)
962 return;
963
964 /* If a machine turns up on a new net without dropping the old lease,
965 or two machines claim the same name, then we end up with two interfaces with
Simon Kelleyb8187c82005-11-26 21:46:27 +0000966 the same name. Check for that here and remove the name from the old lease.
Simon Kelley4cb1b322012-02-06 14:30:41 +0000967 Note that IPv6 leases are different. All the leases to the same DUID are
968 allowed the same name.
969
Simon Kelleyb8187c82005-11-26 21:46:27 +0000970 Don't allow a name from the client to override a name from dnsmasq config. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000971
972 if (name)
973 {
Simon Kelley9009d742008-11-14 20:04:27 +0000974 if ((new_name = whine_malloc(strlen(name) + 1)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000975 {
Simon Kelley9009d742008-11-14 20:04:27 +0000976 strcpy(new_name, name);
Simon Kelley4cb1b322012-02-06 14:30:41 +0000977 if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2)))
Simon Kelley9009d742008-11-14 20:04:27 +0000978 {
979 strcpy(new_fqdn, name);
980 strcat(new_fqdn, ".");
Simon Kelley4cb1b322012-02-06 14:30:41 +0000981 strcat(new_fqdn, domain);
Simon Kelley9009d742008-11-14 20:04:27 +0000982 }
983 }
984
985 /* Depending on mode, we check either unqualified name or FQDN. */
986 for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next)
987 {
Simon Kelley28866e92011-02-14 20:19:14 +0000988 if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +0000989 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000990 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn))
Simon Kelley9009d742008-11-14 20:04:27 +0000991 continue;
992 }
993 else
994 {
995 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
996 continue;
997 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000998
999 if (lease->flags & (LEASE_TA | LEASE_NA))
1000 {
1001 if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA)))
1002 continue;
1003
Simon Kelleyceae00d2012-02-09 21:28:14 +00001004 /* another lease for the same DUID is OK for IPv6 */
Simon Kelley4cb1b322012-02-06 14:30:41 +00001005 if (lease->clid_len == lease_tmp->clid_len &&
1006 lease->clid && lease_tmp->clid &&
1007 memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0)
1008 continue;
1009 }
1010 else if (lease_tmp->flags & (LEASE_TA | LEASE_NA))
1011 continue;
1012
1013 if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth)
Simon Kelley9009d742008-11-14 20:04:27 +00001014 {
1015 free(new_name);
1016 free(new_fqdn);
1017 return;
1018 }
1019
1020 kill_name(lease_tmp);
1021 break;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001022 }
1023 }
1024
1025 if (lease->hostname)
Simon Kelley9009d742008-11-14 20:04:27 +00001026 kill_name(lease);
Simon Kelley16972692006-10-16 20:04:18 +01001027
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001028 lease->hostname = new_name;
1029 lease->fqdn = new_fqdn;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001030
1031 if (auth)
1032 lease->flags |= LEASE_AUTH_NAME;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001033
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001034 file_dirty = 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001035 dns_dirty = 1;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001036 lease->flags |= LEASE_CHANGED; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001037}
1038
Simon Kelley353ae4d2012-03-19 20:07:51 +00001039void lease_set_interface(struct dhcp_lease *lease, int interface, time_t now)
Simon Kelley824af852008-02-12 20:43:05 +00001040{
Vladislav Grishenko408c3682013-09-24 16:18:49 +01001041 (void)now;
1042
Simon Kelley824af852008-02-12 20:43:05 +00001043 if (lease->last_interface == interface)
1044 return;
1045
1046 lease->last_interface = interface;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001047 lease->flags |= LEASE_CHANGED;
Simon Kelley353ae4d2012-03-19 20:07:51 +00001048
1049#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +01001050 slaac_add_addrs(lease, now, 0);
Simon Kelley353ae4d2012-03-19 20:07:51 +00001051#endif
Simon Kelley824af852008-02-12 20:43:05 +00001052}
1053
Simon Kelley5aabfc72007-08-29 11:24:47 +01001054void rerun_scripts(void)
1055{
1056 struct dhcp_lease *lease;
1057
1058 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001059 lease->flags |= LEASE_CHANGED;
Simon Kelley5aabfc72007-08-29 11:24:47 +01001060}
1061
Simon Kelley7cebd202006-05-06 14:13:33 +01001062/* deleted leases get transferred to the old_leases list.
1063 remove them here, after calling the lease change
Simon Kelley16972692006-10-16 20:04:18 +01001064 script. Also run the lease change script on new/modified leases.
1065
1066 Return zero if nothing to do. */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001067int do_script_run(time_t now)
Simon Kelley7cebd202006-05-06 14:13:33 +01001068{
1069 struct dhcp_lease *lease;
1070
Vladislav Grishenko408c3682013-09-24 16:18:49 +01001071 (void)now;
1072
Simon Kelley9009d742008-11-14 20:04:27 +00001073#ifdef HAVE_DBUS
1074 /* If we're going to be sending DBus signals, but the connection is not yet up,
1075 delay everything until it is. */
Simon Kelley28866e92011-02-14 20:19:14 +00001076 if (option_bool(OPT_DBUS) && !daemon->dbus)
Simon Kelley9009d742008-11-14 20:04:27 +00001077 return 0;
1078#endif
1079
Simon Kelley16972692006-10-16 20:04:18 +01001080 if (old_leases)
Simon Kelley7cebd202006-05-06 14:13:33 +01001081 {
Simon Kelley7cebd202006-05-06 14:13:33 +01001082 lease = old_leases;
Simon Kelley16972692006-10-16 20:04:18 +01001083
1084 /* If the lease still has an old_hostname, do the "old" action on that first */
1085 if (lease->old_hostname)
1086 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001087#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001088 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1089#endif
Simon Kelley16972692006-10-16 20:04:18 +01001090 free(lease->old_hostname);
1091 lease->old_hostname = NULL;
1092 return 1;
1093 }
1094 else
1095 {
Simon Kelley353ae4d2012-03-19 20:07:51 +00001096#ifdef HAVE_DHCP6
1097 struct slaac_address *slaac, *tmp;
1098 for (slaac = lease->slaac_address; slaac; slaac = tmp)
1099 {
1100 tmp = slaac->next;
1101 free(slaac);
1102 }
1103#endif
Simon Kelley9009d742008-11-14 20:04:27 +00001104 kill_name(lease);
Simon Kelley1f15b812009-10-13 17:49:32 +01001105#ifdef HAVE_SCRIPT
Simon Kelley9009d742008-11-14 20:04:27 +00001106 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001107#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001108#ifdef HAVE_DBUS
1109 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
1110#endif
Simon Kelley16972692006-10-16 20:04:18 +01001111 old_leases = lease->next;
1112
Simon Kelley9009d742008-11-14 20:04:27 +00001113 free(lease->old_hostname);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001114 free(lease->clid);
Simon Kelley316e2732010-01-22 20:16:09 +00001115 free(lease->extradata);
Simon Kelley16972692006-10-16 20:04:18 +01001116 free(lease);
1117
1118 return 1;
1119 }
Simon Kelley7cebd202006-05-06 14:13:33 +01001120 }
Simon Kelley16972692006-10-16 20:04:18 +01001121
1122 /* make sure we announce the loss of a hostname before its new location. */
1123 for (lease = leases; lease; lease = lease->next)
1124 if (lease->old_hostname)
1125 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001126#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001127 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1128#endif
Simon Kelley16972692006-10-16 20:04:18 +01001129 free(lease->old_hostname);
1130 lease->old_hostname = NULL;
1131 return 1;
1132 }
1133
Simon Kelley7cebd202006-05-06 14:13:33 +01001134 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001135 if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
1136 ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)))
Simon Kelley7cebd202006-05-06 14:13:33 +01001137 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001138#ifdef HAVE_SCRIPT
Simon Kelley4cb1b322012-02-06 14:30:41 +00001139 queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley9009d742008-11-14 20:04:27 +00001140 lease->fqdn ? lease->fqdn : lease->hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001141#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001142#ifdef HAVE_DBUS
Simon Kelley4cb1b322012-02-06 14:30:41 +00001143 emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley1f15b812009-10-13 17:49:32 +01001144 lease->fqdn ? lease->fqdn : lease->hostname);
1145#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +00001146 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED);
Simon Kelley16972692006-10-16 20:04:18 +01001147
Simon Kelley316e2732010-01-22 20:16:09 +00001148 /* this is used for the "add" call, then junked, since they're not in the database */
1149 free(lease->extradata);
1150 lease->extradata = NULL;
Simon Kelley16972692006-10-16 20:04:18 +01001151
1152 return 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001153 }
Simon Kelley16972692006-10-16 20:04:18 +01001154
1155 return 0; /* nothing to do */
Simon Kelley7cebd202006-05-06 14:13:33 +01001156}
Simon Kelley7622fc02009-06-04 20:32:05 +01001157
Simon Kelleyceae00d2012-02-09 21:28:14 +00001158#ifdef HAVE_SCRIPT
Josh Soref730c6742017-02-06 16:14:04 +00001159/* delim == -1 -> delim = 0, but embedded 0s, creating extra records, are OK. */
Simon Kelleyceae00d2012-02-09 21:28:14 +00001160void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim)
1161{
1162 unsigned int i;
1163
Simon Kelleya93bd4b2016-03-01 18:58:01 +00001164 if (delim == -1)
1165 delim = 0;
1166 else
Josh Soref730c6742017-02-06 16:14:04 +00001167 /* check for embedded NULLs */
Simon Kelleya93bd4b2016-03-01 18:58:01 +00001168 for (i = 0; i < len; i++)
1169 if (data[i] == 0)
1170 {
1171 len = i;
1172 break;
1173 }
1174
Simon Kelleyceae00d2012-02-09 21:28:14 +00001175 if ((lease->extradata_size - lease->extradata_len) < (len + 1))
1176 {
1177 size_t newsz = lease->extradata_len + len + 100;
1178 unsigned char *new = whine_malloc(newsz);
1179
1180 if (!new)
1181 return;
1182
1183 if (lease->extradata)
1184 {
1185 memcpy(new, lease->extradata, lease->extradata_len);
1186 free(lease->extradata);
1187 }
1188
1189 lease->extradata = new;
1190 lease->extradata_size = newsz;
1191 }
1192
1193 if (len != 0)
1194 memcpy(lease->extradata + lease->extradata_len, data, len);
1195 lease->extradata[lease->extradata_len + len] = delim;
1196 lease->extradata_len += len + 1;
1197}
1198#endif
1199
Simon Kelley7622fc02009-06-04 20:32:05 +01001200#endif
Simon Kelley7cebd202006-05-06 14:13:33 +01001201
1202
1203
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001204