blob: 04fc18933c27955767f02a86de33ae9b440e62de [file] [log] [blame]
Simon Kelleyc49778d2016-01-06 18:52:33 +00001/* dnsmasq is Copyright (c) 2000-2016 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 Kelleyc72daea2012-01-05 21:33:27 +000027 struct 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)
63 return 0;
64
65 if (inet_pton(AF_INET, daemon->namebuff, &addr.addr.addr4))
66 {
67 if ((lease = lease4_allocate(addr.addr.addr4)))
68 domain = get_domain(lease->addr);
69
70 hw_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, DHCP_CHADDR_MAX, NULL, &hw_type);
71 /* For backwards compatibility, no explicit MAC address type means ether. */
72 if (hw_type == 0 && hw_len != 0)
73 hw_type = ARPHRD_ETHER;
74 }
75#ifdef HAVE_DHCP6
76 else if (inet_pton(AF_INET6, daemon->namebuff, &addr.addr.addr6))
77 {
78 char *s = daemon->dhcp_buff2;
79 int lease_type = LEASE_NA;
80
81 if (s[0] == 'T')
82 {
83 lease_type = LEASE_TA;
84 s++;
85 }
86
87 if ((lease = lease6_allocate(&addr.addr.addr6, lease_type)))
88 {
89 lease_set_iaid(lease, strtoul(s, NULL, 10));
90 domain = get_domain6((struct in6_addr *)lease->hwaddr);
91 }
92 }
93#endif
94 else
95 return 0;
96
97 if (!lease)
98 die (_("too many stored leases"), NULL, EC_MISC);
99
100 if (strcmp(daemon->packet, "*") != 0)
101 clid_len = parse_hex(daemon->packet, (unsigned char *)daemon->packet, 255, NULL, NULL);
102
103 lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned char *)daemon->packet,
104 hw_len, hw_type, clid_len, now, 0);
105
106 if (strcmp(daemon->dhcp_buff, "*") != 0)
107 lease_set_hostname(lease, daemon->dhcp_buff, 0, domain, NULL);
108
109 ei = atol(daemon->dhcp_buff3);
110
111#ifdef HAVE_BROKEN_RTC
112 if (ei != 0)
113 lease->expires = (time_t)ei + now;
114 else
115 lease->expires = (time_t)0;
116 lease->length = ei;
117#else
118 /* strictly time_t is opaque, but this hack should work on all sane systems,
119 even when sizeof(time_t) == 8 */
120 lease->expires = (time_t)ei;
121#endif
122
123 /* set these correctly: the "old" events are generated later from
124 the startup synthesised SIGHUP. */
125 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED);
126
127 *daemon->dhcp_buff3 = *daemon->dhcp_buff2 = '\0';
128 }
129
130 return (items == 0 || items == EOF);
131}
132
133void lease_init(time_t now)
134{
Simon Kelley208b65c2006-08-05 21:41:37 +0100135 FILE *leasestream;
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100136 int readok = 0;
137
Simon Kelley3be34542004-09-11 19:12:13 +0100138 leases_left = daemon->dhcp_max;
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100139
Simon Kelley28866e92011-02-14 20:19:14 +0000140 if (option_bool(OPT_LEASE_RO))
Simon Kelley208b65c2006-08-05 21:41:37 +0100141 {
142 /* run "<lease_change_script> init" once to get the
143 initial state of the database. If leasefile-ro is
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100144 set without a script, we just do without any
Simon Kelley208b65c2006-08-05 21:41:37 +0100145 lease database. */
Simon Kelley1f15b812009-10-13 17:49:32 +0100146#ifdef HAVE_SCRIPT
147 if (daemon->lease_change_command)
Simon Kelley208b65c2006-08-05 21:41:37 +0100148 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100149 strcpy(daemon->dhcp_buff, daemon->lease_change_command);
150 strcat(daemon->dhcp_buff, " init");
151 leasestream = popen(daemon->dhcp_buff, "r");
Simon Kelley208b65c2006-08-05 21:41:37 +0100152 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100153 else
154#endif
155 {
156 file_dirty = dns_dirty = 0;
157 return;
158 }
159
Simon Kelley208b65c2006-08-05 21:41:37 +0100160 }
161 else
162 {
163 /* NOTE: need a+ mode to create file if it doesn't exist */
164 leasestream = daemon->lease_stream = fopen(daemon->lease_file, "a+");
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100165
Simon Kelley208b65c2006-08-05 21:41:37 +0100166 if (!leasestream)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100167 die(_("cannot open or create lease file %s: %s"), daemon->lease_file, EC_FILE);
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100168
Simon Kelley7622fc02009-06-04 20:32:05 +0100169 /* a+ mode leaves pointer at end. */
Simon Kelley208b65c2006-08-05 21:41:37 +0100170 rewind(leasestream);
171 }
Simon Kelleybf4e62c2016-07-22 21:37:59 +0100172
Simon Kelley208b65c2006-08-05 21:41:37 +0100173 if (leasestream)
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100174 {
175 readok = read_leases(now, leasestream);
176 if (ferror(leasestream))
177 die(_("failed to read lease file %s: %s"), daemon->lease_file, EC_FILE);
Simon Kelley3d7b5502012-02-10 17:35:37 +0000178
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100179 }
Simon Kelley208b65c2006-08-05 21:41:37 +0100180
Simon Kelley1f15b812009-10-13 17:49:32 +0100181#ifdef HAVE_SCRIPT
Simon Kelley208b65c2006-08-05 21:41:37 +0100182 if (!daemon->lease_stream)
183 {
184 int rc = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000185
Simon Kelley208b65c2006-08-05 21:41:37 +0100186 /* shell returns 127 for "command not found", 126 for bad permissions. */
187 if (!leasestream || (rc = pclose(leasestream)) == -1 || WEXITSTATUS(rc) == 127 || WEXITSTATUS(rc) == 126)
188 {
189 if (WEXITSTATUS(rc) == 127)
190 errno = ENOENT;
191 else if (WEXITSTATUS(rc) == 126)
192 errno = EACCES;
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100193
Simon Kelley5aabfc72007-08-29 11:24:47 +0100194 die(_("cannot run lease-init script %s: %s"), daemon->lease_change_command, EC_FILE);
Simon Kelley208b65c2006-08-05 21:41:37 +0100195 }
196
197 if (WEXITSTATUS(rc) != 0)
198 {
199 sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100200 die(_("lease-init script returned exit code %s"), daemon->dhcp_buff, WEXITSTATUS(rc) + EC_INIT_OFFSET);
Simon Kelley208b65c2006-08-05 21:41:37 +0100201 }
Petr Menšík3a8b0f62017-04-23 14:12:37 +0100202
203 /* Only complain if we stopped reading due to a non-parsed line when running script,
204 this is expected behaviour when reading from a file, if the file was written with IPv6 data
205 and we are not compiled to understand that. */
206 if (!readok)
207 {
208 my_syslog(MS_DHCP | LOG_ERR, _("aborting lease-init script, invalid line: %s %s %s %s ..."),
209 daemon->dhcp_buff3, daemon->dhcp_buff2,
210 daemon->namebuff, daemon->dhcp_buff);
211 die(_("failed to parse lease-init script output"), NULL, EC_FILE);
212 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000213 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100214#endif
Simon Kelley7cebd202006-05-06 14:13:33 +0100215
216 /* Some leases may have expired */
217 file_dirty = 0;
218 lease_prune(NULL, now);
219 dns_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000220}
221
Simon Kelley5aabfc72007-08-29 11:24:47 +0100222void lease_update_from_configs(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000223{
224 /* changes to the config may change current leases. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000225
Simon Kelley44a2a312004-03-10 20:04:35 +0000226 struct dhcp_lease *lease;
227 struct dhcp_config *config;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000228 char *name;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000229
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000230 for (lease = leases; lease; lease = lease->next)
Simon Kelley89500e32013-09-20 16:29:20 +0100231 if (lease->flags & (LEASE_TA | LEASE_NA))
232 continue;
233 else if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len,
234 lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) &&
235 (config->flags & CONFIG_NAME) &&
236 (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000237 lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NULL);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100238 else if ((name = host_from_dns(lease->addr)))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000239 lease_set_hostname(lease, name, 1, get_domain(lease->addr), NULL); /* updates auth flag only */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000240}
Simon Kelley4cb1b322012-02-06 14:30:41 +0000241
Simon Kelley5aabfc72007-08-29 11:24:47 +0100242static void ourprintf(int *errp, char *format, ...)
Simon Kelley7cebd202006-05-06 14:13:33 +0100243{
244 va_list ap;
245
246 va_start(ap, format);
247 if (!(*errp) && vfprintf(daemon->lease_stream, format, ap) < 0)
248 *errp = errno;
249 va_end(ap);
250}
251
Simon Kelley5aabfc72007-08-29 11:24:47 +0100252void lease_update_file(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000253{
254 struct dhcp_lease *lease;
Simon Kelley7cebd202006-05-06 14:13:33 +0100255 time_t next_event;
256 int i, err = 0;
257
Simon Kelley208b65c2006-08-05 21:41:37 +0100258 if (file_dirty != 0 && daemon->lease_stream)
Simon Kelley44a2a312004-03-10 20:04:35 +0000259 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000260 errno = 0;
261 rewind(daemon->lease_stream);
262 if (errno != 0 || ftruncate(fileno(daemon->lease_stream), 0) != 0)
Simon Kelley7cebd202006-05-06 14:13:33 +0100263 err = errno;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000264
265 for (lease = leases; lease; lease = lease->next)
266 {
Simon Kelleyc72daea2012-01-05 21:33:27 +0000267
268#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000269 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000270 continue;
271#endif
272
Simon Kelley44a2a312004-03-10 20:04:35 +0000273#ifdef HAVE_BROKEN_RTC
Simon Kelley5aabfc72007-08-29 11:24:47 +0100274 ourprintf(&err, "%u ", lease->length);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100275#else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100276 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100277#endif
Simon Kelleyc72daea2012-01-05 21:33:27 +0000278
Simon Kelley7cebd202006-05-06 14:13:33 +0100279 if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100280 ourprintf(&err, "%.2x-", lease->hwaddr_type);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100281 for (i = 0; i < lease->hwaddr_len; i++)
282 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100283 ourprintf(&err, "%.2x", lease->hwaddr[i]);
Simon Kelley7cebd202006-05-06 14:13:33 +0100284 if (i != lease->hwaddr_len - 1)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100285 ourprintf(&err, ":");
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100286 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000287
288 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100289
Simon Kelleyc72daea2012-01-05 21:33:27 +0000290 ourprintf(&err, " %s ", daemon->addrbuff);
Simon Kelley1f15b812009-10-13 17:49:32 +0100291 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
292
Simon Kelley0a852542005-03-23 20:28:59 +0000293 if (lease->clid && lease->clid_len != 0)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000294 {
295 for (i = 0; i < lease->clid_len - 1; i++)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100296 ourprintf(&err, "%.2x:", lease->clid[i]);
297 ourprintf(&err, "%.2x\n", lease->clid[i]);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000298 }
299 else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100300 ourprintf(&err, "*\n");
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000301 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100302
Simon Kelleyc72daea2012-01-05 21:33:27 +0000303#ifdef HAVE_DHCP6
304 if (daemon->duid)
305 {
306 ourprintf(&err, "duid ");
307 for (i = 0; i < daemon->duid_len - 1; i++)
308 ourprintf(&err, "%.2x:", daemon->duid[i]);
309 ourprintf(&err, "%.2x\n", daemon->duid[i]);
310
311 for (lease = leases; lease; lease = lease->next)
312 {
313
Simon Kelley4cb1b322012-02-06 14:30:41 +0000314 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000315 continue;
316
317#ifdef HAVE_BROKEN_RTC
318 ourprintf(&err, "%u ", lease->length);
319#else
320 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
321#endif
322
Simon Kelley89500e32013-09-20 16:29:20 +0100323 inet_ntop(AF_INET6, &lease->addr6, daemon->addrbuff, ADDRSTRLEN);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000324
Simon Kelley4cb1b322012-02-06 14:30:41 +0000325 ourprintf(&err, "%s%u %s ", (lease->flags & LEASE_TA) ? "T" : "",
Simon Kelley89500e32013-09-20 16:29:20 +0100326 lease->iaid, daemon->addrbuff);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000327 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
328
329 if (lease->clid && lease->clid_len != 0)
330 {
331 for (i = 0; i < lease->clid_len - 1; i++)
332 ourprintf(&err, "%.2x:", lease->clid[i]);
333 ourprintf(&err, "%.2x\n", lease->clid[i]);
334 }
335 else
336 ourprintf(&err, "*\n");
337 }
338 }
339#endif
340
Simon Kelley7cebd202006-05-06 14:13:33 +0100341 if (fflush(daemon->lease_stream) != 0 ||
342 fsync(fileno(daemon->lease_stream)) < 0)
343 err = errno;
344
345 if (!err)
346 file_dirty = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000347 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100348
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000349 /* Set alarm for when the first lease expires. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000350 next_event = 0;
351
352#ifdef HAVE_DHCP6
Simon Kelley353ae4d2012-03-19 20:07:51 +0000353 /* do timed RAs and determine when the next is, also pings to potential SLAAC addresses */
Simon Kelley1f776932012-12-16 19:46:08 +0000354 if (daemon->doing_ra)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000355 {
Simon Kelley919dd7c2012-05-12 15:23:09 +0100356 time_t event;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000357
Simon Kelley919dd7c2012-05-12 15:23:09 +0100358 if ((event = periodic_slaac(now, leases)) != 0)
359 {
360 if (next_event == 0 || difftime(next_event, event) > 0.0)
361 next_event = event;
362 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000363
Simon Kelley919dd7c2012-05-12 15:23:09 +0100364 if ((event = periodic_ra(now)) != 0)
365 {
366 if (next_event == 0 || difftime(next_event, event) > 0.0)
367 next_event = event;
368 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000369 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000370#endif
371
372 for (lease = leases; lease; lease = lease->next)
Simon Kelley7cebd202006-05-06 14:13:33 +0100373 if (lease->expires != 0 &&
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000374 (next_event == 0 || difftime(next_event, lease->expires) > 0.0))
375 next_event = lease->expires;
Simon Kelley7cebd202006-05-06 14:13:33 +0100376
377 if (err)
378 {
379 if (next_event == 0 || difftime(next_event, LEASE_RETRY + now) > 0.0)
380 next_event = LEASE_RETRY + now;
381
Simon Kelley7622fc02009-06-04 20:32:05 +0100382 my_syslog(MS_DHCP | LOG_ERR, _("failed to write %s: %s (retry in %us)"),
Simon Kelleyf2621c72007-04-29 19:47:21 +0100383 daemon->lease_file, strerror(err),
384 (unsigned int)difftime(next_event, now));
Simon Kelley7cebd202006-05-06 14:13:33 +0100385 }
386
Simon Kelley353ae4d2012-03-19 20:07:51 +0000387 send_alarm(next_event, now);
Simon Kelley44a2a312004-03-10 20:04:35 +0000388}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000389
Simon Kelley801ca9a2012-03-06 19:30:17 +0000390
Simon Kelley3f2873d2013-05-14 11:28:47 +0100391static int find_interface_v4(struct in_addr local, int if_index, char *label,
Simon Kelley801ca9a2012-03-06 19:30:17 +0000392 struct in_addr netmask, struct in_addr broadcast, void *vparam)
Simon Kelley44a2a312004-03-10 20:04:35 +0000393{
394 struct dhcp_lease *lease;
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100395 int prefix = netmask_length(netmask);
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800396
Simon Kelley3f2873d2013-05-14 11:28:47 +0100397 (void) label;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000398 (void) broadcast;
399 (void) vparam;
400
401 for (lease = leases; lease; lease = lease->next)
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100402 if (!(lease->flags & (LEASE_TA | LEASE_NA)) &&
403 is_same_net(local, lease->addr, netmask) &&
404 prefix > lease->new_prefixlen)
405 {
406 lease->new_interface = if_index;
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800407 lease->new_prefixlen = prefix;
408 }
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800409
Simon Kelley801ca9a2012-03-06 19:30:17 +0000410 return 1;
411}
412
413#ifdef HAVE_DHCP6
414static int find_interface_v6(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000415 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000416 int preferred, int valid, void *vparam)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000417{
418 struct dhcp_lease *lease;
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800419
Simon Kelley353ae4d2012-03-19 20:07:51 +0000420 (void)scope;
Simon Kelleybad7b872012-12-20 22:00:39 +0000421 (void)flags;
Simon Kelley1f776932012-12-16 19:46:08 +0000422 (void)preferred;
423 (void)valid;
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800424 (void)vparam;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000425
426 for (lease = leases; lease; lease = lease->next)
427 if ((lease->flags & (LEASE_TA | LEASE_NA)))
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800428 if (is_same_net6(local, &lease->addr6, prefix) && prefix > lease->new_prefixlen) {
429 /* save prefix length for comparison, as we might get shorter matching
430 * prefix in upcoming netlink GETADDR responses
431 * */
432 lease->new_interface = if_index;
433 lease->new_prefixlen = prefix;
434 }
435
Simon Kelley801ca9a2012-03-06 19:30:17 +0000436 return 1;
437}
Simon Kelley353ae4d2012-03-19 20:07:51 +0000438
439void lease_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface)
440{
Simon Kelley5ef33272012-03-30 15:10:28 +0100441 /* We may be doing RA but not DHCPv4, in which case the lease
442 database may not exist and we have nothing to do anyway */
443 if (daemon->dhcp)
444 slaac_ping_reply(sender, packet, interface, leases);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000445}
446
Simon Kelley0c050242012-12-22 22:13:19 +0000447void lease_update_slaac(time_t now)
448{
Josh Soref730c6742017-02-06 16:14:04 +0000449 /* Called when we construct a new RA-names context, to add putative
Simon Kelley0c050242012-12-22 22:13:19 +0000450 new SLAAC addresses to existing leases. */
451
452 struct dhcp_lease *lease;
453
454 if (daemon->dhcp)
455 for (lease = leases; lease; lease = lease->next)
456 slaac_add_addrs(lease, now, 0);
457}
458
Simon Kelley801ca9a2012-03-06 19:30:17 +0000459#endif
460
461
462/* Find interfaces associated with leases at start-up. This gets updated as
463 we do DHCP transactions, but information about directly-connected subnets
464 is useful from scrips and necessary for determining SLAAC addresses from
465 start-time. */
Simon Kelley8b372702012-03-09 17:45:10 +0000466void lease_find_interfaces(time_t now)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000467{
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100468 struct dhcp_lease *lease;
469
470 for (lease = leases; lease; lease = lease->next)
471 lease->new_prefixlen = lease->new_interface = 0;
472
Simon Kelley353ae4d2012-03-19 20:07:51 +0000473 iface_enumerate(AF_INET, &now, find_interface_v4);
474#ifdef HAVE_DHCP6
475 iface_enumerate(AF_INET6, &now, find_interface_v6);
Simon Kelley3511a922013-11-07 10:28:11 +0000476#endif
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100477
478 for (lease = leases; lease; lease = lease->next)
479 if (lease->new_interface != 0)
480 lease_set_interface(lease, lease->new_interface, now);
Simon Kelley3511a922013-11-07 10:28:11 +0000481}
Simon Kelley8b372702012-03-09 17:45:10 +0000482
Simon Kelley3511a922013-11-07 10:28:11 +0000483#ifdef HAVE_DHCP6
484void lease_make_duid(time_t now)
485{
Simon Kelley8b372702012-03-09 17:45:10 +0000486 /* 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 +0000487 if (!daemon->duid && daemon->doing_dhcp6)
Simon Kelley8b372702012-03-09 17:45:10 +0000488 {
489 file_dirty = 1;
490 make_duid(now);
491 }
Simon Kelley801ca9a2012-03-06 19:30:17 +0000492}
Simon Kelley3511a922013-11-07 10:28:11 +0000493#endif
494
Simon Kelley801ca9a2012-03-06 19:30:17 +0000495
496
497
Simon Kelley353ae4d2012-03-19 20:07:51 +0000498void lease_update_dns(int force)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000499{
500 struct dhcp_lease *lease;
501
Simon Kelley353ae4d2012-03-19 20:07:51 +0000502 if (daemon->port != 0 && (dns_dirty || force))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000503 {
Simon Kelley8ff55672012-12-09 21:09:01 +0000504#ifndef HAVE_BROKEN_RTC
Simon Kelleye1ff4192012-12-09 17:08:47 +0000505 /* force transfer to authoritative secondaries */
506 daemon->soa_sn++;
Simon Kelley8ff55672012-12-09 21:09:01 +0000507#endif
Simon Kelleye1ff4192012-12-09 17:08:47 +0000508
Simon Kelley801ca9a2012-03-06 19:30:17 +0000509 cache_unhash_dhcp();
510
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000511 for (lease = leases; lease; lease = lease->next)
512 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000513 int prot = AF_INET;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000514
Simon Kelley4cb1b322012-02-06 14:30:41 +0000515#ifdef HAVE_DHCP6
516 if (lease->flags & (LEASE_TA | LEASE_NA))
517 prot = AF_INET6;
Simon Kelleyf444cdd2012-03-07 10:15:57 +0000518 else if (lease->hostname || lease->fqdn)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000519 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000520 struct slaac_address *slaac;
521
522 for (slaac = lease->slaac_address; slaac; slaac = slaac->next)
523 if (slaac->backoff == 0)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000524 {
Simon Kelley801ca9a2012-03-06 19:30:17 +0000525 if (lease->fqdn)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000526 cache_add_dhcp_entry(lease->fqdn, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000527 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000528 cache_add_dhcp_entry(lease->hostname, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000529 }
530 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000531
Simon Kelley9009d742008-11-14 20:04:27 +0000532 if (lease->fqdn)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000533 cache_add_dhcp_entry(lease->fqdn, prot,
Simon Kelley89500e32013-09-20 16:29:20 +0100534 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->addr6,
Simon Kelley4cb1b322012-02-06 14:30:41 +0000535 lease->expires);
Simon Kelley9009d742008-11-14 20:04:27 +0000536
Simon Kelley28866e92011-02-14 20:19:14 +0000537 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000538 cache_add_dhcp_entry(lease->hostname, prot,
Simon Kelley89500e32013-09-20 16:29:20 +0100539 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->addr6,
Simon Kelley4cb1b322012-02-06 14:30:41 +0000540 lease->expires);
Simon Kelley91543f42013-09-23 12:41:20 +0100541
542#else
543 if (lease->fqdn)
544 cache_add_dhcp_entry(lease->fqdn, prot, (struct all_addr *)&lease->addr, lease->expires);
545
546 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
547 cache_add_dhcp_entry(lease->hostname, prot, (struct all_addr *)&lease->addr, lease->expires);
548#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000549 }
550
551 dns_dirty = 0;
552 }
553}
554
555void lease_prune(struct dhcp_lease *target, time_t now)
556{
557 struct dhcp_lease *lease, *tmp, **up;
558
559 for (lease = leases, up = &leases; lease; lease = tmp)
560 {
561 tmp = lease->next;
562 if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target)
563 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100564 file_dirty = 1;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000565 if (lease->hostname)
Simon Kelley7cebd202006-05-06 14:13:33 +0100566 dns_dirty = 1;
567
Simon Kelleyc72daea2012-01-05 21:33:27 +0000568 *up = lease->next; /* unlink */
Simon Kelley7cebd202006-05-06 14:13:33 +0100569
570 /* Put on old_leases list 'till we
571 can run the script */
572 lease->next = old_leases;
573 old_leases = lease;
574
Simon Kelley44a2a312004-03-10 20:04:35 +0000575 leases_left++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000576 }
577 else
578 up = &lease->next;
579 }
580}
581
582
Simon Kelleycdeda282006-03-16 20:16:06 +0000583struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,
Simon Kelley0a852542005-03-23 20:28:59 +0000584 unsigned char *clid, int clid_len)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000585{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000586 struct dhcp_lease *lease;
587
Simon Kelley0a852542005-03-23 20:28:59 +0000588 if (clid)
589 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000590 {
591#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000592 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000593 continue;
594#endif
595 if (lease->clid && clid_len == lease->clid_len &&
596 memcmp(clid, lease->clid, clid_len) == 0)
597 return lease;
598 }
Simon Kelley0a852542005-03-23 20:28:59 +0000599
600 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000601 {
602#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000603 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000604 continue;
605#endif
606 if ((!lease->clid || !clid) &&
607 hw_len != 0 &&
608 lease->hwaddr_len == hw_len &&
609 lease->hwaddr_type == hw_type &&
610 memcmp(hwaddr, lease->hwaddr, hw_len) == 0)
611 return lease;
612 }
613
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000614 return NULL;
615}
616
617struct dhcp_lease *lease_find_by_addr(struct in_addr addr)
618{
619 struct dhcp_lease *lease;
620
621 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000622 {
623#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000624 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000625 continue;
626#endif
627 if (lease->addr.s_addr == addr.s_addr)
628 return lease;
629 }
630
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000631 return NULL;
632}
633
Simon Kelley52b92f42012-01-22 16:05:15 +0000634#ifdef HAVE_DHCP6
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000635/* find address for {CLID, IAID, address} */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000636struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len,
637 int lease_type, int iaid, struct in6_addr *addr)
Simon Kelley52b92f42012-01-22 16:05:15 +0000638{
639 struct dhcp_lease *lease;
640
641 for (lease = leases; lease; lease = lease->next)
642 {
Simon Kelley89500e32013-09-20 16:29:20 +0100643 if (!(lease->flags & lease_type) || lease->iaid != iaid)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000644 continue;
Simon Kelley52b92f42012-01-22 16:05:15 +0000645
Simon Kelley89500e32013-09-20 16:29:20 +0100646 if (!IN6_ARE_ADDR_EQUAL(&lease->addr6, addr))
Simon Kelley4cb1b322012-02-06 14:30:41 +0000647 continue;
648
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000649 if ((clid_len != lease->clid_len ||
Simon Kelley4cb1b322012-02-06 14:30:41 +0000650 memcmp(clid, lease->clid, clid_len) != 0))
651 continue;
652
Simon Kelley8b460612012-09-08 21:47:28 +0100653 return lease;
Simon Kelley52b92f42012-01-22 16:05:15 +0000654 }
655
656 return NULL;
657}
658
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000659/* reset "USED flags */
660void lease6_reset(void)
Simon Kelley8b460612012-09-08 21:47:28 +0100661{
662 struct dhcp_lease *lease;
663
664 for (lease = leases; lease; lease = lease->next)
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000665 lease->flags &= ~LEASE_USED;
666}
667
668/* enumerate all leases belonging to {CLID, IAID} */
669struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_type, unsigned char *clid, int clid_len, int iaid)
670{
671 struct dhcp_lease *lease;
672
673 if (!first)
674 first = leases;
Simon Kelley27cb3142013-04-02 20:06:39 +0100675 else
676 first = first->next;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000677
678 for (lease = first; lease; lease = lease->next)
Simon Kelley8b460612012-09-08 21:47:28 +0100679 {
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000680 if (lease->flags & LEASE_USED)
681 continue;
682
Simon Kelley89500e32013-09-20 16:29:20 +0100683 if (!(lease->flags & lease_type) || lease->iaid != iaid)
Simon Kelley8b460612012-09-08 21:47:28 +0100684 continue;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000685
686 if ((clid_len != lease->clid_len ||
687 memcmp(clid, lease->clid, clid_len) != 0))
688 continue;
689
690 return lease;
Simon Kelley8b460612012-09-08 21:47:28 +0100691 }
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000692
693 return NULL;
Simon Kelley8b460612012-09-08 21:47:28 +0100694}
695
Simon Kelley52b92f42012-01-22 16:05:15 +0000696struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr)
697{
698 struct dhcp_lease *lease;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000699
Simon Kelley52b92f42012-01-22 16:05:15 +0000700 for (lease = leases; lease; lease = lease->next)
701 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000702 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelley52b92f42012-01-22 16:05:15 +0000703 continue;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000704
Simon Kelley89500e32013-09-20 16:29:20 +0100705 if (is_same_net6(&lease->addr6, net, prefix) &&
706 (prefix == 128 || addr6part(&lease->addr6) == addr))
Simon Kelley52b92f42012-01-22 16:05:15 +0000707 return lease;
708 }
709
710 return NULL;
Simon Kelley07933802012-02-14 20:55:25 +0000711}
712
713/* Find largest assigned address in context */
714u64 lease_find_max_addr6(struct dhcp_context *context)
715{
716 struct dhcp_lease *lease;
717 u64 addr = addr6part(&context->start6);
718
719 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
720 for (lease = leases; lease; lease = lease->next)
721 {
Simon Kelley07933802012-02-14 20:55:25 +0000722 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
723 continue;
Simon Kelley6caacac2012-02-15 21:58:33 +0000724
Simon Kelley89500e32013-09-20 16:29:20 +0100725 if (is_same_net6(&lease->addr6, &context->start6, 64) &&
726 addr6part(&lease->addr6) > addr6part(&context->start6) &&
727 addr6part(&lease->addr6) <= addr6part(&context->end6) &&
728 addr6part(&lease->addr6) > addr)
729 addr = addr6part(&lease->addr6);
Simon Kelley07933802012-02-14 20:55:25 +0000730 }
731
732 return addr;
733}
734
Simon Kelley52b92f42012-01-22 16:05:15 +0000735#endif
736
Simon Kelley7de060b2011-08-26 17:24:52 +0100737/* Find largest assigned address in context */
738struct in_addr lease_find_max_addr(struct dhcp_context *context)
739{
740 struct dhcp_lease *lease;
741 struct in_addr addr = context->start;
742
743 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
744 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000745 {
746#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000747 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000748 continue;
749#endif
750 if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) &&
751 ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) &&
752 ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr)))
753 addr = lease->addr;
754 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100755
756 return addr;
757}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000758
Simon Kelleyc72daea2012-01-05 21:33:27 +0000759static struct dhcp_lease *lease_allocate(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000760{
761 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100762 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000763 return NULL;
764
Simon Kelley7cebd202006-05-06 14:13:33 +0100765 memset(lease, 0, sizeof(struct dhcp_lease));
Simon Kelley4cb1b322012-02-06 14:30:41 +0000766 lease->flags = LEASE_NEW;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000767 lease->expires = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100768#ifdef HAVE_BROKEN_RTC
769 lease->length = 0xffffffff; /* illegal value */
770#endif
Simon Kelley89500e32013-09-20 16:29:20 +0100771 lease->hwaddr_len = 256; /* illegal value */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000772 lease->next = leases;
773 leases = lease;
774
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100775 file_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000776 leases_left--;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000777
778 return lease;
779}
780
Simon Kelley52b92f42012-01-22 16:05:15 +0000781struct dhcp_lease *lease4_allocate(struct in_addr addr)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000782{
783 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100784 if (lease)
Simon Kelley89500e32013-09-20 16:29:20 +0100785 lease->addr = addr;
786
Simon Kelleyc72daea2012-01-05 21:33:27 +0000787 return lease;
788}
789
790#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000791struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000792{
793 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100794
795 if (lease)
796 {
Simon Kelley89500e32013-09-20 16:29:20 +0100797 lease->addr6 = *addrp;
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100798 lease->flags |= lease_type;
Simon Kelley89500e32013-09-20 16:29:20 +0100799 lease->iaid = 0;
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100800 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000801
802 return lease;
803}
804#endif
805
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100806void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000807{
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000808 time_t exp;
809
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100810 if (len == 0xffffffff)
811 {
812 exp = 0;
813 len = 0;
814 }
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000815 else
816 {
817 exp = now + (time_t)len;
818 /* Check for 2038 overflow. Make the lease
Josh Soref730c6742017-02-06 16:14:04 +0000819 infinite in that case, as the least disruptive
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000820 thing we can do. */
821 if (difftime(exp, now) <= 0.0)
822 exp = 0;
823 }
824
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000825 if (exp != lease->expires)
Simon Kelley0a852542005-03-23 20:28:59 +0000826 {
Simon Kelley0a852542005-03-23 20:28:59 +0000827 dns_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100828 lease->expires = exp;
829#ifndef HAVE_BROKEN_RTC
Simon Kelley4cb1b322012-02-06 14:30:41 +0000830 lease->flags |= LEASE_AUX_CHANGED;
831 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100832#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000833 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100834
835#ifdef HAVE_BROKEN_RTC
836 if (len != lease->length)
837 {
838 lease->length = len;
Simon Kelleyb7f40202012-02-29 21:43:37 +0000839 lease->flags |= LEASE_AUX_CHANGED;
840 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100841 }
842#endif
843}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000844
Simon Kelley89500e32013-09-20 16:29:20 +0100845#ifdef HAVE_DHCP6
846void lease_set_iaid(struct dhcp_lease *lease, int iaid)
847{
848 if (lease->iaid != iaid)
849 {
850 lease->iaid = iaid;
851 lease->flags |= LEASE_CHANGED;
852 }
853}
854#endif
855
Nicolas Cavallari64bcff12015-04-28 21:55:18 +0100856void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr,
857 const unsigned char *clid, int hw_len, int hw_type,
858 int clid_len, time_t now, int force)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000859{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000860#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100861 int change = force;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000862 lease->flags |= LEASE_HAVE_HWADDR;
863#endif
864
Simon Kelleya9ab7322012-04-28 11:29:37 +0100865 (void)force;
Vladislav Grishenko408c3682013-09-24 16:18:49 +0100866 (void)now;
Simon Kelleya9ab7322012-04-28 11:29:37 +0100867
Simon Kelleycdeda282006-03-16 20:16:06 +0000868 if (hw_len != lease->hwaddr_len ||
869 hw_type != lease->hwaddr_type ||
Simon Kelley7cebd202006-05-06 14:13:33 +0100870 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000871 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000872 if (hw_len != 0)
873 memcpy(lease->hwaddr, hwaddr, hw_len);
Simon Kelleycdeda282006-03-16 20:16:06 +0000874 lease->hwaddr_len = hw_len;
875 lease->hwaddr_type = hw_type;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000876 lease->flags |= LEASE_CHANGED;
877 file_dirty = 1; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000878 }
Simon Kelley0a852542005-03-23 20:28:59 +0000879
880 /* only update clid when one is available, stops packets
881 without a clid removing the record. Lease init uses
882 clid_len == 0 for no clid. */
883 if (clid_len != 0 && clid)
884 {
885 if (!lease->clid)
886 lease->clid_len = 0;
887
888 if (lease->clid_len != clid_len)
889 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000890 lease->flags |= LEASE_AUX_CHANGED;
891 file_dirty = 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100892 free(lease->clid);
893 if (!(lease->clid = whine_malloc(clid_len)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100894 return;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000895#ifdef HAVE_DHCP6
896 change = 1;
897#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000898 }
899 else if (memcmp(lease->clid, clid, clid_len) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000900 {
901 lease->flags |= LEASE_AUX_CHANGED;
902 file_dirty = 1;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000903#ifdef HAVE_DHCP6
904 change = 1;
905#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +0000906 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000907
Simon Kelley0a852542005-03-23 20:28:59 +0000908 lease->clid_len = clid_len;
909 memcpy(lease->clid, clid, clid_len);
910 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000911
912#ifdef HAVE_DHCP6
913 if (change)
Simon Kelleya9ab7322012-04-28 11:29:37 +0100914 slaac_add_addrs(lease, now, force);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000915#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000916}
917
Simon Kelley9009d742008-11-14 20:04:27 +0000918static void kill_name(struct dhcp_lease *lease)
919{
920 /* run script to say we lost our old name */
921
922 /* this shouldn't happen unless updates are very quick and the
923 script very slow, we just avoid a memory leak if it does. */
924 free(lease->old_hostname);
925
926 /* If we know the fqdn, pass that. The helper will derive the
Simon Kelley4cb1b322012-02-06 14:30:41 +0000927 unqualified name from it, free the unqualified name here. */
Simon Kelley9009d742008-11-14 20:04:27 +0000928
929 if (lease->fqdn)
930 {
931 lease->old_hostname = lease->fqdn;
932 free(lease->hostname);
933 }
934 else
935 lease->old_hostname = lease->hostname;
936
937 lease->hostname = lease->fqdn = NULL;
938}
939
Nicolas Cavallari64bcff12015-04-28 21:55:18 +0100940void lease_set_hostname(struct dhcp_lease *lease, const char *name, int auth, char *domain, char *config_domain)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000941{
942 struct dhcp_lease *lease_tmp;
943 char *new_name = NULL, *new_fqdn = NULL;
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000944
945 if (config_domain && (!domain || !hostname_isequal(domain, config_domain)))
946 my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, name);
Simon Kelley9009d742008-11-14 20:04:27 +0000947
Simon Kelleya2226412004-05-13 20:27:08 +0100948 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000949 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000950 if (auth)
951 lease->flags |= LEASE_AUTH_NAME;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000952 return;
953 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100954
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000955 if (!name && !lease->hostname)
956 return;
957
958 /* If a machine turns up on a new net without dropping the old lease,
959 or two machines claim the same name, then we end up with two interfaces with
Simon Kelleyb8187c82005-11-26 21:46:27 +0000960 the same name. Check for that here and remove the name from the old lease.
Simon Kelley4cb1b322012-02-06 14:30:41 +0000961 Note that IPv6 leases are different. All the leases to the same DUID are
962 allowed the same name.
963
Simon Kelleyb8187c82005-11-26 21:46:27 +0000964 Don't allow a name from the client to override a name from dnsmasq config. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000965
966 if (name)
967 {
Simon Kelley9009d742008-11-14 20:04:27 +0000968 if ((new_name = whine_malloc(strlen(name) + 1)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000969 {
Simon Kelley9009d742008-11-14 20:04:27 +0000970 strcpy(new_name, name);
Simon Kelley4cb1b322012-02-06 14:30:41 +0000971 if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2)))
Simon Kelley9009d742008-11-14 20:04:27 +0000972 {
973 strcpy(new_fqdn, name);
974 strcat(new_fqdn, ".");
Simon Kelley4cb1b322012-02-06 14:30:41 +0000975 strcat(new_fqdn, domain);
Simon Kelley9009d742008-11-14 20:04:27 +0000976 }
977 }
978
979 /* Depending on mode, we check either unqualified name or FQDN. */
980 for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next)
981 {
Simon Kelley28866e92011-02-14 20:19:14 +0000982 if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +0000983 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000984 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn))
Simon Kelley9009d742008-11-14 20:04:27 +0000985 continue;
986 }
987 else
988 {
989 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
990 continue;
991 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000992
993 if (lease->flags & (LEASE_TA | LEASE_NA))
994 {
995 if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA)))
996 continue;
997
Simon Kelleyceae00d2012-02-09 21:28:14 +0000998 /* another lease for the same DUID is OK for IPv6 */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000999 if (lease->clid_len == lease_tmp->clid_len &&
1000 lease->clid && lease_tmp->clid &&
1001 memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0)
1002 continue;
1003 }
1004 else if (lease_tmp->flags & (LEASE_TA | LEASE_NA))
1005 continue;
1006
1007 if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth)
Simon Kelley9009d742008-11-14 20:04:27 +00001008 {
1009 free(new_name);
1010 free(new_fqdn);
1011 return;
1012 }
1013
1014 kill_name(lease_tmp);
1015 break;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001016 }
1017 }
1018
1019 if (lease->hostname)
Simon Kelley9009d742008-11-14 20:04:27 +00001020 kill_name(lease);
Simon Kelley16972692006-10-16 20:04:18 +01001021
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001022 lease->hostname = new_name;
1023 lease->fqdn = new_fqdn;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001024
1025 if (auth)
1026 lease->flags |= LEASE_AUTH_NAME;
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001027
Simon Kelley5e9e0ef2006-04-17 14:24:29 +01001028 file_dirty = 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001029 dns_dirty = 1;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001030 lease->flags |= LEASE_CHANGED; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001031}
1032
Simon Kelley353ae4d2012-03-19 20:07:51 +00001033void lease_set_interface(struct dhcp_lease *lease, int interface, time_t now)
Simon Kelley824af852008-02-12 20:43:05 +00001034{
Vladislav Grishenko408c3682013-09-24 16:18:49 +01001035 (void)now;
1036
Simon Kelley824af852008-02-12 20:43:05 +00001037 if (lease->last_interface == interface)
1038 return;
1039
1040 lease->last_interface = interface;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001041 lease->flags |= LEASE_CHANGED;
Simon Kelley353ae4d2012-03-19 20:07:51 +00001042
1043#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +01001044 slaac_add_addrs(lease, now, 0);
Simon Kelley353ae4d2012-03-19 20:07:51 +00001045#endif
Simon Kelley824af852008-02-12 20:43:05 +00001046}
1047
Simon Kelley5aabfc72007-08-29 11:24:47 +01001048void rerun_scripts(void)
1049{
1050 struct dhcp_lease *lease;
1051
1052 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001053 lease->flags |= LEASE_CHANGED;
Simon Kelley5aabfc72007-08-29 11:24:47 +01001054}
1055
Simon Kelley7cebd202006-05-06 14:13:33 +01001056/* deleted leases get transferred to the old_leases list.
1057 remove them here, after calling the lease change
Simon Kelley16972692006-10-16 20:04:18 +01001058 script. Also run the lease change script on new/modified leases.
1059
1060 Return zero if nothing to do. */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001061int do_script_run(time_t now)
Simon Kelley7cebd202006-05-06 14:13:33 +01001062{
1063 struct dhcp_lease *lease;
1064
Vladislav Grishenko408c3682013-09-24 16:18:49 +01001065 (void)now;
1066
Simon Kelley9009d742008-11-14 20:04:27 +00001067#ifdef HAVE_DBUS
1068 /* If we're going to be sending DBus signals, but the connection is not yet up,
1069 delay everything until it is. */
Simon Kelley28866e92011-02-14 20:19:14 +00001070 if (option_bool(OPT_DBUS) && !daemon->dbus)
Simon Kelley9009d742008-11-14 20:04:27 +00001071 return 0;
1072#endif
1073
Simon Kelley16972692006-10-16 20:04:18 +01001074 if (old_leases)
Simon Kelley7cebd202006-05-06 14:13:33 +01001075 {
Simon Kelley7cebd202006-05-06 14:13:33 +01001076 lease = old_leases;
Simon Kelley16972692006-10-16 20:04:18 +01001077
1078 /* If the lease still has an old_hostname, do the "old" action on that first */
1079 if (lease->old_hostname)
1080 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001081#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001082 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1083#endif
Simon Kelley16972692006-10-16 20:04:18 +01001084 free(lease->old_hostname);
1085 lease->old_hostname = NULL;
1086 return 1;
1087 }
1088 else
1089 {
Simon Kelley353ae4d2012-03-19 20:07:51 +00001090#ifdef HAVE_DHCP6
1091 struct slaac_address *slaac, *tmp;
1092 for (slaac = lease->slaac_address; slaac; slaac = tmp)
1093 {
1094 tmp = slaac->next;
1095 free(slaac);
1096 }
1097#endif
Simon Kelley9009d742008-11-14 20:04:27 +00001098 kill_name(lease);
Simon Kelley1f15b812009-10-13 17:49:32 +01001099#ifdef HAVE_SCRIPT
Simon Kelley9009d742008-11-14 20:04:27 +00001100 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001101#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001102#ifdef HAVE_DBUS
1103 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
1104#endif
Simon Kelley16972692006-10-16 20:04:18 +01001105 old_leases = lease->next;
1106
Simon Kelley9009d742008-11-14 20:04:27 +00001107 free(lease->old_hostname);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001108 free(lease->clid);
Simon Kelley316e2732010-01-22 20:16:09 +00001109 free(lease->extradata);
Simon Kelley16972692006-10-16 20:04:18 +01001110 free(lease);
1111
1112 return 1;
1113 }
Simon Kelley7cebd202006-05-06 14:13:33 +01001114 }
Simon Kelley16972692006-10-16 20:04:18 +01001115
1116 /* make sure we announce the loss of a hostname before its new location. */
1117 for (lease = leases; lease; lease = lease->next)
1118 if (lease->old_hostname)
1119 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001120#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001121 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1122#endif
Simon Kelley16972692006-10-16 20:04:18 +01001123 free(lease->old_hostname);
1124 lease->old_hostname = NULL;
1125 return 1;
1126 }
1127
Simon Kelley7cebd202006-05-06 14:13:33 +01001128 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001129 if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
1130 ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)))
Simon Kelley7cebd202006-05-06 14:13:33 +01001131 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001132#ifdef HAVE_SCRIPT
Simon Kelley4cb1b322012-02-06 14:30:41 +00001133 queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley9009d742008-11-14 20:04:27 +00001134 lease->fqdn ? lease->fqdn : lease->hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001135#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001136#ifdef HAVE_DBUS
Simon Kelley4cb1b322012-02-06 14:30:41 +00001137 emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley1f15b812009-10-13 17:49:32 +01001138 lease->fqdn ? lease->fqdn : lease->hostname);
1139#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +00001140 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED);
Simon Kelley16972692006-10-16 20:04:18 +01001141
Simon Kelley316e2732010-01-22 20:16:09 +00001142 /* this is used for the "add" call, then junked, since they're not in the database */
1143 free(lease->extradata);
1144 lease->extradata = NULL;
Simon Kelley16972692006-10-16 20:04:18 +01001145
1146 return 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001147 }
Simon Kelley16972692006-10-16 20:04:18 +01001148
1149 return 0; /* nothing to do */
Simon Kelley7cebd202006-05-06 14:13:33 +01001150}
Simon Kelley7622fc02009-06-04 20:32:05 +01001151
Simon Kelleyceae00d2012-02-09 21:28:14 +00001152#ifdef HAVE_SCRIPT
Josh Soref730c6742017-02-06 16:14:04 +00001153/* delim == -1 -> delim = 0, but embedded 0s, creating extra records, are OK. */
Simon Kelleyceae00d2012-02-09 21:28:14 +00001154void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim)
1155{
1156 unsigned int i;
1157
Simon Kelleya93bd4b2016-03-01 18:58:01 +00001158 if (delim == -1)
1159 delim = 0;
1160 else
Josh Soref730c6742017-02-06 16:14:04 +00001161 /* check for embedded NULLs */
Simon Kelleya93bd4b2016-03-01 18:58:01 +00001162 for (i = 0; i < len; i++)
1163 if (data[i] == 0)
1164 {
1165 len = i;
1166 break;
1167 }
1168
Simon Kelleyceae00d2012-02-09 21:28:14 +00001169 if ((lease->extradata_size - lease->extradata_len) < (len + 1))
1170 {
1171 size_t newsz = lease->extradata_len + len + 100;
1172 unsigned char *new = whine_malloc(newsz);
1173
1174 if (!new)
1175 return;
1176
1177 if (lease->extradata)
1178 {
1179 memcpy(new, lease->extradata, lease->extradata_len);
1180 free(lease->extradata);
1181 }
1182
1183 lease->extradata = new;
1184 lease->extradata_size = newsz;
1185 }
1186
1187 if (len != 0)
1188 memcpy(lease->extradata + lease->extradata_len, data, len);
1189 lease->extradata[lease->extradata_len + len] = delim;
1190 lease->extradata_len += len + 1;
1191}
1192#endif
1193
Simon Kelley7622fc02009-06-04 20:32:05 +01001194#endif
Simon Kelley7cebd202006-05-06 14:13:33 +01001195
1196
1197
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001198