blob: 23e6fe0a248d893ba472a1ee43908076238a5321 [file] [log] [blame]
Simon Kelley2a8710a2020-01-05 16:40:06 +00001/* dnsmasq is Copyright (c) 2000-2020 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,
Simon Kelley52ec7832020-02-07 21:05:54 +0000233 lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL, NULL)) &&
Simon Kelley89500e32013-09-20 16:29:20 +0100234 (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;
Florent Fourcotdf6636b2019-02-11 17:04:44 +0100561 if ((lease->expires != 0 && difftime(now, lease->expires) >= 0) || lease == target)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000562 {
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,
Dominik DL6ER456a3192019-10-20 18:51:52 +0200638 int lease_type, unsigned int iaid,
639 struct in6_addr *addr)
Simon Kelley52b92f42012-01-22 16:05:15 +0000640{
641 struct dhcp_lease *lease;
642
643 for (lease = leases; lease; lease = lease->next)
644 {
Simon Kelley89500e32013-09-20 16:29:20 +0100645 if (!(lease->flags & lease_type) || lease->iaid != iaid)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000646 continue;
Simon Kelley52b92f42012-01-22 16:05:15 +0000647
Simon Kelley89500e32013-09-20 16:29:20 +0100648 if (!IN6_ARE_ADDR_EQUAL(&lease->addr6, addr))
Simon Kelley4cb1b322012-02-06 14:30:41 +0000649 continue;
650
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000651 if ((clid_len != lease->clid_len ||
Simon Kelley4cb1b322012-02-06 14:30:41 +0000652 memcmp(clid, lease->clid, clid_len) != 0))
653 continue;
654
Simon Kelley8b460612012-09-08 21:47:28 +0100655 return lease;
Simon Kelley52b92f42012-01-22 16:05:15 +0000656 }
657
658 return NULL;
659}
660
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000661/* reset "USED flags */
662void lease6_reset(void)
Simon Kelley8b460612012-09-08 21:47:28 +0100663{
664 struct dhcp_lease *lease;
665
666 for (lease = leases; lease; lease = lease->next)
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000667 lease->flags &= ~LEASE_USED;
668}
669
670/* enumerate all leases belonging to {CLID, IAID} */
Dominik DL6ER456a3192019-10-20 18:51:52 +0200671struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_type,
672 unsigned char *clid, int clid_len,
673 unsigned int iaid)
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000674{
675 struct dhcp_lease *lease;
676
677 if (!first)
678 first = leases;
Simon Kelley27cb3142013-04-02 20:06:39 +0100679 else
680 first = first->next;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000681
682 for (lease = first; lease; lease = lease->next)
Simon Kelley8b460612012-09-08 21:47:28 +0100683 {
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000684 if (lease->flags & LEASE_USED)
685 continue;
686
Simon Kelley89500e32013-09-20 16:29:20 +0100687 if (!(lease->flags & lease_type) || lease->iaid != iaid)
Simon Kelley8b460612012-09-08 21:47:28 +0100688 continue;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000689
690 if ((clid_len != lease->clid_len ||
691 memcmp(clid, lease->clid, clid_len) != 0))
692 continue;
693
694 return lease;
Simon Kelley8b460612012-09-08 21:47:28 +0100695 }
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000696
697 return NULL;
Simon Kelley8b460612012-09-08 21:47:28 +0100698}
699
Simon Kelley52b92f42012-01-22 16:05:15 +0000700struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr)
701{
702 struct dhcp_lease *lease;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000703
Simon Kelley52b92f42012-01-22 16:05:15 +0000704 for (lease = leases; lease; lease = lease->next)
705 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000706 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelley52b92f42012-01-22 16:05:15 +0000707 continue;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000708
Simon Kelley89500e32013-09-20 16:29:20 +0100709 if (is_same_net6(&lease->addr6, net, prefix) &&
710 (prefix == 128 || addr6part(&lease->addr6) == addr))
Simon Kelley52b92f42012-01-22 16:05:15 +0000711 return lease;
712 }
713
714 return NULL;
Simon Kelley07933802012-02-14 20:55:25 +0000715}
716
717/* Find largest assigned address in context */
718u64 lease_find_max_addr6(struct dhcp_context *context)
719{
720 struct dhcp_lease *lease;
721 u64 addr = addr6part(&context->start6);
722
723 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
724 for (lease = leases; lease; lease = lease->next)
725 {
Simon Kelley07933802012-02-14 20:55:25 +0000726 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
727 continue;
Simon Kelley6caacac2012-02-15 21:58:33 +0000728
Simon Kelley89500e32013-09-20 16:29:20 +0100729 if (is_same_net6(&lease->addr6, &context->start6, 64) &&
730 addr6part(&lease->addr6) > addr6part(&context->start6) &&
731 addr6part(&lease->addr6) <= addr6part(&context->end6) &&
732 addr6part(&lease->addr6) > addr)
733 addr = addr6part(&lease->addr6);
Simon Kelley07933802012-02-14 20:55:25 +0000734 }
735
736 return addr;
737}
738
Simon Kelley52b92f42012-01-22 16:05:15 +0000739#endif
740
Simon Kelley7de060b2011-08-26 17:24:52 +0100741/* Find largest assigned address in context */
742struct in_addr lease_find_max_addr(struct dhcp_context *context)
743{
744 struct dhcp_lease *lease;
745 struct in_addr addr = context->start;
746
747 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
748 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000749 {
750#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000751 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000752 continue;
753#endif
754 if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) &&
755 ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) &&
756 ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr)))
757 addr = lease->addr;
758 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100759
760 return addr;
761}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000762
Simon Kelleyc72daea2012-01-05 21:33:27 +0000763static struct dhcp_lease *lease_allocate(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000764{
765 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100766 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000767 return NULL;
768
Simon Kelley7cebd202006-05-06 14:13:33 +0100769 memset(lease, 0, sizeof(struct dhcp_lease));
Simon Kelley4cb1b322012-02-06 14:30:41 +0000770 lease->flags = LEASE_NEW;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000771 lease->expires = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100772#ifdef HAVE_BROKEN_RTC
773 lease->length = 0xffffffff; /* illegal value */
774#endif
Simon Kelley89500e32013-09-20 16:29:20 +0100775 lease->hwaddr_len = 256; /* illegal value */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000776 lease->next = leases;
777 leases = lease;
778
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100779 file_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000780 leases_left--;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000781
782 return lease;
783}
784
Simon Kelley52b92f42012-01-22 16:05:15 +0000785struct dhcp_lease *lease4_allocate(struct in_addr addr)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000786{
787 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100788 if (lease)
Julian Kornbergeraba8bbb2018-07-21 21:55:08 +0100789 {
790 lease->addr = addr;
791 daemon->metrics[METRIC_LEASES_ALLOCATED_4]++;
792 }
Simon Kelley89500e32013-09-20 16:29:20 +0100793
Simon Kelleyc72daea2012-01-05 21:33:27 +0000794 return lease;
795}
796
797#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000798struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000799{
800 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100801
802 if (lease)
803 {
Simon Kelley89500e32013-09-20 16:29:20 +0100804 lease->addr6 = *addrp;
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100805 lease->flags |= lease_type;
Simon Kelley89500e32013-09-20 16:29:20 +0100806 lease->iaid = 0;
Julian Kornbergeraba8bbb2018-07-21 21:55:08 +0100807
808 daemon->metrics[METRIC_LEASES_ALLOCATED_6]++;
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100809 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000810
811 return lease;
812}
813#endif
814
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100815void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000816{
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000817 time_t exp;
818
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100819 if (len == 0xffffffff)
820 {
821 exp = 0;
822 len = 0;
823 }
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000824 else
825 {
826 exp = now + (time_t)len;
827 /* Check for 2038 overflow. Make the lease
Josh Soref730c6742017-02-06 16:14:04 +0000828 infinite in that case, as the least disruptive
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000829 thing we can do. */
830 if (difftime(exp, now) <= 0.0)
831 exp = 0;
832 }
833
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000834 if (exp != lease->expires)
Simon Kelley0a852542005-03-23 20:28:59 +0000835 {
Simon Kelley0a852542005-03-23 20:28:59 +0000836 dns_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100837 lease->expires = exp;
838#ifndef HAVE_BROKEN_RTC
Simon Kelleyee645822020-02-27 16:34:14 +0000839 lease->flags |= LEASE_AUX_CHANGED | LEASE_EXP_CHANGED;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000840 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100841#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000842 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100843
844#ifdef HAVE_BROKEN_RTC
845 if (len != lease->length)
846 {
847 lease->length = len;
Simon Kelleyb7f40202012-02-29 21:43:37 +0000848 lease->flags |= LEASE_AUX_CHANGED;
849 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100850 }
851#endif
852}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000853
Simon Kelley89500e32013-09-20 16:29:20 +0100854#ifdef HAVE_DHCP6
Dominik DL6ER456a3192019-10-20 18:51:52 +0200855void lease_set_iaid(struct dhcp_lease *lease, unsigned int iaid)
Simon Kelley89500e32013-09-20 16:29:20 +0100856{
857 if (lease->iaid != iaid)
858 {
859 lease->iaid = iaid;
860 lease->flags |= LEASE_CHANGED;
861 }
862}
863#endif
864
Nicolas Cavallari64bcff12015-04-28 21:55:18 +0100865void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr,
866 const unsigned char *clid, int hw_len, int hw_type,
867 int clid_len, time_t now, int force)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000868{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000869#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100870 int change = force;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000871 lease->flags |= LEASE_HAVE_HWADDR;
872#endif
873
Simon Kelleya9ab7322012-04-28 11:29:37 +0100874 (void)force;
Vladislav Grishenko408c3682013-09-24 16:18:49 +0100875 (void)now;
Simon Kelleya9ab7322012-04-28 11:29:37 +0100876
Simon Kelleycdeda282006-03-16 20:16:06 +0000877 if (hw_len != lease->hwaddr_len ||
878 hw_type != lease->hwaddr_type ||
Simon Kelley7cebd202006-05-06 14:13:33 +0100879 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000880 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000881 if (hw_len != 0)
882 memcpy(lease->hwaddr, hwaddr, hw_len);
Simon Kelleycdeda282006-03-16 20:16:06 +0000883 lease->hwaddr_len = hw_len;
884 lease->hwaddr_type = hw_type;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000885 lease->flags |= LEASE_CHANGED;
886 file_dirty = 1; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000887 }
Simon Kelley0a852542005-03-23 20:28:59 +0000888
889 /* only update clid when one is available, stops packets
890 without a clid removing the record. Lease init uses
891 clid_len == 0 for no clid. */
892 if (clid_len != 0 && clid)
893 {
894 if (!lease->clid)
895 lease->clid_len = 0;
896
897 if (lease->clid_len != clid_len)
898 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000899 lease->flags |= LEASE_AUX_CHANGED;
900 file_dirty = 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100901 free(lease->clid);
902 if (!(lease->clid = whine_malloc(clid_len)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100903 return;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000904#ifdef HAVE_DHCP6
905 change = 1;
906#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000907 }
908 else if (memcmp(lease->clid, clid, clid_len) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000909 {
910 lease->flags |= LEASE_AUX_CHANGED;
911 file_dirty = 1;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000912#ifdef HAVE_DHCP6
913 change = 1;
914#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +0000915 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000916
Simon Kelley0a852542005-03-23 20:28:59 +0000917 lease->clid_len = clid_len;
918 memcpy(lease->clid, clid, clid_len);
919 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000920
921#ifdef HAVE_DHCP6
922 if (change)
Simon Kelleya9ab7322012-04-28 11:29:37 +0100923 slaac_add_addrs(lease, now, force);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000924#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000925}
926
Simon Kelley9009d742008-11-14 20:04:27 +0000927static void kill_name(struct dhcp_lease *lease)
928{
929 /* run script to say we lost our old name */
930
931 /* this shouldn't happen unless updates are very quick and the
932 script very slow, we just avoid a memory leak if it does. */
933 free(lease->old_hostname);
934
935 /* If we know the fqdn, pass that. The helper will derive the
Simon Kelley4cb1b322012-02-06 14:30:41 +0000936 unqualified name from it, free the unqualified name here. */
Simon Kelley9009d742008-11-14 20:04:27 +0000937
938 if (lease->fqdn)
939 {
940 lease->old_hostname = lease->fqdn;
941 free(lease->hostname);
942 }
943 else
944 lease->old_hostname = lease->hostname;
945
946 lease->hostname = lease->fqdn = NULL;
947}
948
Nicolas Cavallari64bcff12015-04-28 21:55:18 +0100949void lease_set_hostname(struct dhcp_lease *lease, const char *name, int auth, char *domain, char *config_domain)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000950{
951 struct dhcp_lease *lease_tmp;
952 char *new_name = NULL, *new_fqdn = NULL;
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000953
954 if (config_domain && (!domain || !hostname_isequal(domain, config_domain)))
955 my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, name);
Simon Kelley9009d742008-11-14 20:04:27 +0000956
Simon Kelleya2226412004-05-13 20:27:08 +0100957 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000958 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000959 if (auth)
960 lease->flags |= LEASE_AUTH_NAME;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000961 return;
962 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100963
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000964 if (!name && !lease->hostname)
965 return;
966
967 /* If a machine turns up on a new net without dropping the old lease,
968 or two machines claim the same name, then we end up with two interfaces with
Simon Kelleyb8187c82005-11-26 21:46:27 +0000969 the same name. Check for that here and remove the name from the old lease.
Simon Kelley4cb1b322012-02-06 14:30:41 +0000970 Note that IPv6 leases are different. All the leases to the same DUID are
971 allowed the same name.
972
Simon Kelleyb8187c82005-11-26 21:46:27 +0000973 Don't allow a name from the client to override a name from dnsmasq config. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000974
975 if (name)
976 {
Simon Kelley9009d742008-11-14 20:04:27 +0000977 if ((new_name = whine_malloc(strlen(name) + 1)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000978 {
Simon Kelley9009d742008-11-14 20:04:27 +0000979 strcpy(new_name, name);
Simon Kelley4cb1b322012-02-06 14:30:41 +0000980 if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2)))
Simon Kelley9009d742008-11-14 20:04:27 +0000981 {
982 strcpy(new_fqdn, name);
983 strcat(new_fqdn, ".");
Simon Kelley4cb1b322012-02-06 14:30:41 +0000984 strcat(new_fqdn, domain);
Simon Kelley9009d742008-11-14 20:04:27 +0000985 }
986 }
987
988 /* Depending on mode, we check either unqualified name or FQDN. */
989 for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next)
990 {
Simon Kelley28866e92011-02-14 20:19:14 +0000991 if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +0000992 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000993 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn))
Simon Kelley9009d742008-11-14 20:04:27 +0000994 continue;
995 }
996 else
997 {
998 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
999 continue;
1000 }
Simon Kelley4cb1b322012-02-06 14:30:41 +00001001
1002 if (lease->flags & (LEASE_TA | LEASE_NA))
1003 {
1004 if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA)))
1005 continue;
1006
Simon Kelleyceae00d2012-02-09 21:28:14 +00001007 /* another lease for the same DUID is OK for IPv6 */
Simon Kelley4cb1b322012-02-06 14:30:41 +00001008 if (lease->clid_len == lease_tmp->clid_len &&
1009 lease->clid && lease_tmp->clid &&
1010 memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0)
1011 continue;
1012 }
1013 else if (lease_tmp->flags & (LEASE_TA | LEASE_NA))
1014 continue;
1015
1016 if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth)
Simon Kelley9009d742008-11-14 20:04:27 +00001017 {
1018 free(new_name);
1019 free(new_fqdn);
1020 return;
1021 }
1022
1023 kill_name(lease_tmp);
1024 break;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001025 }
1026 }
1027
1028 if (lease->hostname)
Simon Kelley9009d742008-11-14 20:04:27 +00001029 kill_name(lease);
Simon Kelley16972692006-10-16 20:04:18 +01001030
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001031 lease->hostname = new_name;
1032 lease->fqdn = new_fqdn;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001033
1034 if (auth)
1035 lease->flags |= LEASE_AUTH_NAME;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001036
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001037 file_dirty = 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001038 dns_dirty = 1;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001039 lease->flags |= LEASE_CHANGED; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001040}
1041
Simon Kelley353ae4d2012-03-19 20:07:51 +00001042void lease_set_interface(struct dhcp_lease *lease, int interface, time_t now)
Simon Kelley824af852008-02-12 20:43:05 +00001043{
Vladislav Grishenko408c3682013-09-24 16:18:49 +01001044 (void)now;
1045
Simon Kelley824af852008-02-12 20:43:05 +00001046 if (lease->last_interface == interface)
1047 return;
1048
1049 lease->last_interface = interface;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001050 lease->flags |= LEASE_CHANGED;
Simon Kelley353ae4d2012-03-19 20:07:51 +00001051
1052#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +01001053 slaac_add_addrs(lease, now, 0);
Simon Kelley353ae4d2012-03-19 20:07:51 +00001054#endif
Simon Kelley824af852008-02-12 20:43:05 +00001055}
1056
Simon Kelley5aabfc72007-08-29 11:24:47 +01001057void rerun_scripts(void)
1058{
1059 struct dhcp_lease *lease;
1060
1061 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001062 lease->flags |= LEASE_CHANGED;
Simon Kelley5aabfc72007-08-29 11:24:47 +01001063}
1064
Simon Kelley7cebd202006-05-06 14:13:33 +01001065/* deleted leases get transferred to the old_leases list.
1066 remove them here, after calling the lease change
Simon Kelley16972692006-10-16 20:04:18 +01001067 script. Also run the lease change script on new/modified leases.
1068
1069 Return zero if nothing to do. */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001070int do_script_run(time_t now)
Simon Kelley7cebd202006-05-06 14:13:33 +01001071{
1072 struct dhcp_lease *lease;
1073
Vladislav Grishenko408c3682013-09-24 16:18:49 +01001074 (void)now;
1075
Simon Kelley9009d742008-11-14 20:04:27 +00001076#ifdef HAVE_DBUS
1077 /* If we're going to be sending DBus signals, but the connection is not yet up,
1078 delay everything until it is. */
Simon Kelley28866e92011-02-14 20:19:14 +00001079 if (option_bool(OPT_DBUS) && !daemon->dbus)
Simon Kelley9009d742008-11-14 20:04:27 +00001080 return 0;
1081#endif
1082
Simon Kelley16972692006-10-16 20:04:18 +01001083 if (old_leases)
Simon Kelley7cebd202006-05-06 14:13:33 +01001084 {
Simon Kelley7cebd202006-05-06 14:13:33 +01001085 lease = old_leases;
Simon Kelley16972692006-10-16 20:04:18 +01001086
1087 /* If the lease still has an old_hostname, do the "old" action on that first */
1088 if (lease->old_hostname)
1089 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001090#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001091 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1092#endif
Simon Kelley16972692006-10-16 20:04:18 +01001093 free(lease->old_hostname);
1094 lease->old_hostname = NULL;
1095 return 1;
1096 }
1097 else
1098 {
Simon Kelley353ae4d2012-03-19 20:07:51 +00001099#ifdef HAVE_DHCP6
1100 struct slaac_address *slaac, *tmp;
1101 for (slaac = lease->slaac_address; slaac; slaac = tmp)
1102 {
1103 tmp = slaac->next;
1104 free(slaac);
1105 }
1106#endif
Simon Kelley9009d742008-11-14 20:04:27 +00001107 kill_name(lease);
Simon Kelley1f15b812009-10-13 17:49:32 +01001108#ifdef HAVE_SCRIPT
Simon Kelley9009d742008-11-14 20:04:27 +00001109 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001110#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001111#ifdef HAVE_DBUS
1112 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
1113#endif
Simon Kelley16972692006-10-16 20:04:18 +01001114 old_leases = lease->next;
1115
Simon Kelley9009d742008-11-14 20:04:27 +00001116 free(lease->old_hostname);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001117 free(lease->clid);
Simon Kelley316e2732010-01-22 20:16:09 +00001118 free(lease->extradata);
Simon Kelley16972692006-10-16 20:04:18 +01001119 free(lease);
1120
1121 return 1;
1122 }
Simon Kelley7cebd202006-05-06 14:13:33 +01001123 }
Simon Kelley16972692006-10-16 20:04:18 +01001124
1125 /* make sure we announce the loss of a hostname before its new location. */
1126 for (lease = leases; lease; lease = lease->next)
1127 if (lease->old_hostname)
1128 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001129#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001130 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1131#endif
Simon Kelley16972692006-10-16 20:04:18 +01001132 free(lease->old_hostname);
1133 lease->old_hostname = NULL;
1134 return 1;
1135 }
1136
Simon Kelley7cebd202006-05-06 14:13:33 +01001137 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001138 if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
Simon Kelleyee645822020-02-27 16:34:14 +00001139 ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)) ||
1140 ((lease->flags & LEASE_EXP_CHANGED) && option_bool(OPT_LEASE_RENEW)))
Simon Kelley7cebd202006-05-06 14:13:33 +01001141 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001142#ifdef HAVE_SCRIPT
Simon Kelley4cb1b322012-02-06 14:30:41 +00001143 queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley9009d742008-11-14 20:04:27 +00001144 lease->fqdn ? lease->fqdn : lease->hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001145#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001146#ifdef HAVE_DBUS
Simon Kelley4cb1b322012-02-06 14:30:41 +00001147 emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley1f15b812009-10-13 17:49:32 +01001148 lease->fqdn ? lease->fqdn : lease->hostname);
1149#endif
Simon Kelleyee645822020-02-27 16:34:14 +00001150 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED | LEASE_EXP_CHANGED);
Simon Kelley16972692006-10-16 20:04:18 +01001151
Simon Kelley316e2732010-01-22 20:16:09 +00001152 /* this is used for the "add" call, then junked, since they're not in the database */
1153 free(lease->extradata);
1154 lease->extradata = NULL;
Simon Kelley16972692006-10-16 20:04:18 +01001155
1156 return 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001157 }
Simon Kelley16972692006-10-16 20:04:18 +01001158
1159 return 0; /* nothing to do */
Simon Kelley7cebd202006-05-06 14:13:33 +01001160}
Simon Kelley7622fc02009-06-04 20:32:05 +01001161
Simon Kelleyceae00d2012-02-09 21:28:14 +00001162#ifdef HAVE_SCRIPT
Josh Soref730c6742017-02-06 16:14:04 +00001163/* delim == -1 -> delim = 0, but embedded 0s, creating extra records, are OK. */
Simon Kelleyceae00d2012-02-09 21:28:14 +00001164void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim)
1165{
1166 unsigned int i;
1167
Simon Kelleya93bd4b2016-03-01 18:58:01 +00001168 if (delim == -1)
1169 delim = 0;
1170 else
Josh Soref730c6742017-02-06 16:14:04 +00001171 /* check for embedded NULLs */
Simon Kelleya93bd4b2016-03-01 18:58:01 +00001172 for (i = 0; i < len; i++)
1173 if (data[i] == 0)
1174 {
1175 len = i;
1176 break;
1177 }
1178
Simon Kelleyceae00d2012-02-09 21:28:14 +00001179 if ((lease->extradata_size - lease->extradata_len) < (len + 1))
1180 {
1181 size_t newsz = lease->extradata_len + len + 100;
1182 unsigned char *new = whine_malloc(newsz);
1183
1184 if (!new)
1185 return;
1186
1187 if (lease->extradata)
1188 {
1189 memcpy(new, lease->extradata, lease->extradata_len);
1190 free(lease->extradata);
1191 }
1192
1193 lease->extradata = new;
1194 lease->extradata_size = newsz;
1195 }
1196
1197 if (len != 0)
1198 memcpy(lease->extradata + lease->extradata_len, data, len);
1199 lease->extradata[lease->extradata_len + len] = delim;
1200 lease->extradata_len += len + 1;
1201}
1202#endif
1203
Simon Kelley7622fc02009-06-04 20:32:05 +01001204#endif
Simon Kelley7cebd202006-05-06 14:13:33 +01001205
1206
1207
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001208