blob: 8adb60588671324d9ddf00d7dab40474d40d4393 [file] [log] [blame]
Simon Kelleyaff33962015-01-31 20:13:40 +00001/* dnsmasq is Copyright (c) 2000-2015 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
Simon Kelley5aabfc72007-08-29 11:24:47 +010024void lease_init(time_t now)
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;
Simon Kelley208b65c2006-08-05 21:41:37 +010030 FILE *leasestream;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010031
Simon Kelley3be34542004-09-11 19:12:13 +010032 leases_left = daemon->dhcp_max;
Simon Kelleyceae00d2012-02-09 21:28:14 +000033
Simon Kelley28866e92011-02-14 20:19:14 +000034 if (option_bool(OPT_LEASE_RO))
Simon Kelley208b65c2006-08-05 21:41:37 +010035 {
36 /* run "<lease_change_script> init" once to get the
37 initial state of the database. If leasefile-ro is
38 set without a script, we just do without any
39 lease database. */
Simon Kelley1f15b812009-10-13 17:49:32 +010040#ifdef HAVE_SCRIPT
41 if (daemon->lease_change_command)
Simon Kelley208b65c2006-08-05 21:41:37 +010042 {
Simon Kelley1f15b812009-10-13 17:49:32 +010043 strcpy(daemon->dhcp_buff, daemon->lease_change_command);
44 strcat(daemon->dhcp_buff, " init");
45 leasestream = popen(daemon->dhcp_buff, "r");
Simon Kelley208b65c2006-08-05 21:41:37 +010046 }
Simon Kelley1f15b812009-10-13 17:49:32 +010047 else
48#endif
49 {
50 file_dirty = dns_dirty = 0;
51 return;
52 }
53
Simon Kelley208b65c2006-08-05 21:41:37 +010054 }
55 else
56 {
57 /* NOTE: need a+ mode to create file if it doesn't exist */
58 leasestream = daemon->lease_stream = fopen(daemon->lease_file, "a+");
59
60 if (!leasestream)
Simon Kelley5aabfc72007-08-29 11:24:47 +010061 die(_("cannot open or create lease file %s: %s"), daemon->lease_file, EC_FILE);
Simon Kelley208b65c2006-08-05 21:41:37 +010062
Simon Kelley7622fc02009-06-04 20:32:05 +010063 /* a+ mode leaves pointer at end. */
Simon Kelley208b65c2006-08-05 21:41:37 +010064 rewind(leasestream);
65 }
Simon Kelley7cebd202006-05-06 14:13:33 +010066
Simon Kelley0a852542005-03-23 20:28:59 +000067 /* client-id max length is 255 which is 255*2 digits + 254 colons
68 borrow DNS packet buffer which is always larger than 1000 bytes */
Simon Kelley208b65c2006-08-05 21:41:37 +010069 if (leasestream)
Simon Kelleyceae00d2012-02-09 21:28:14 +000070 while (fscanf(leasestream, "%255s %255s", daemon->dhcp_buff3, daemon->dhcp_buff2) == 2)
Simon Kelley208b65c2006-08-05 21:41:37 +010071 {
Simon Kelley3d7b5502012-02-10 17:35:37 +000072#ifdef HAVE_DHCP6
Simon Kelleyceae00d2012-02-09 21:28:14 +000073 if (strcmp(daemon->dhcp_buff3, "duid") == 0)
74 {
75 daemon->duid_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, 130, NULL, NULL);
76 daemon->duid = safe_malloc(daemon->duid_len);
77 memcpy(daemon->duid, daemon->dhcp_buff2, daemon->duid_len);
78 continue;
79 }
Simon Kelley3d7b5502012-02-10 17:35:37 +000080#endif
81
Simon Kelleyceae00d2012-02-09 21:28:14 +000082 ei = atol(daemon->dhcp_buff3);
83
84 if (fscanf(leasestream, " %64s %255s %764s",
85 daemon->namebuff, daemon->dhcp_buff, daemon->packet) != 3)
86 break;
87
88 clid_len = 0;
89 if (strcmp(daemon->packet, "*") != 0)
90 clid_len = parse_hex(daemon->packet, (unsigned char *)daemon->packet, 255, NULL, NULL);
91
92 if (inet_pton(AF_INET, daemon->namebuff, &addr.addr.addr4) &&
93 (lease = lease4_allocate(addr.addr.addr4)))
94 {
95 hw_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, DHCP_CHADDR_MAX, NULL, &hw_type);
96 /* For backwards compatibility, no explict MAC address type means ether. */
97 if (hw_type == 0 && hw_len != 0)
98 hw_type = ARPHRD_ETHER;
99
Simon Kelley353ae4d2012-03-19 20:07:51 +0000100 lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned char *)daemon->packet,
Simon Kelleya9ab7322012-04-28 11:29:37 +0100101 hw_len, hw_type, clid_len, now, 0);
Simon Kelleyceae00d2012-02-09 21:28:14 +0000102
103 if (strcmp(daemon->dhcp_buff, "*") != 0)
104 lease_set_hostname(lease, daemon->dhcp_buff, 0, get_domain(lease->addr), NULL);
105 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000106#ifdef HAVE_DHCP6
Simon Kelleyceae00d2012-02-09 21:28:14 +0000107 else if (inet_pton(AF_INET6, daemon->namebuff, &addr.addr.addr6))
Simon Kelley4cb1b322012-02-06 14:30:41 +0000108 {
109 char *s = daemon->dhcp_buff2;
Simon Kelleyceae00d2012-02-09 21:28:14 +0000110 int lease_type = LEASE_NA;
Simon Kelley89500e32013-09-20 16:29:20 +0100111 int iaid;
Simon Kelleyceae00d2012-02-09 21:28:14 +0000112
Simon Kelley4cb1b322012-02-06 14:30:41 +0000113 if (s[0] == 'T')
114 {
115 lease_type = LEASE_TA;
116 s++;
117 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000118
Simon Kelley89500e32013-09-20 16:29:20 +0100119 iaid = strtoul(s, NULL, 10);
Simon Kelleyceae00d2012-02-09 21:28:14 +0000120
121 if ((lease = lease6_allocate(&addr.addr.addr6, lease_type)))
122 {
Simon Kelley89500e32013-09-20 16:29:20 +0100123 lease_set_hwaddr(lease, NULL, (unsigned char *)daemon->packet, 0, 0, clid_len, now, 0);
124 lease_set_iaid(lease, iaid);
Simon Kelleyceae00d2012-02-09 21:28:14 +0000125 if (strcmp(daemon->dhcp_buff, "*") != 0)
126 lease_set_hostname(lease, daemon->dhcp_buff, 0, get_domain6((struct in6_addr *)lease->hwaddr), NULL);
127 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000128 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000129#endif
Simon Kelleyc72daea2012-01-05 21:33:27 +0000130 else
Simon Kelleyceae00d2012-02-09 21:28:14 +0000131 break;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000132
Simon Kelleyc72daea2012-01-05 21:33:27 +0000133 if (!lease)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100134 die (_("too many stored leases"), NULL, EC_MISC);
135
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100136#ifdef HAVE_BROKEN_RTC
Simon Kelley208b65c2006-08-05 21:41:37 +0100137 if (ei != 0)
138 lease->expires = (time_t)ei + now;
139 else
140 lease->expires = (time_t)0;
141 lease->length = ei;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100142#else
Simon Kelley208b65c2006-08-05 21:41:37 +0100143 /* strictly time_t is opaque, but this hack should work on all sane systems,
144 even when sizeof(time_t) == 8 */
145 lease->expires = (time_t)ei;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100146#endif
Simon Kelley208b65c2006-08-05 21:41:37 +0100147
Simon Kelley5aabfc72007-08-29 11:24:47 +0100148 /* set these correctly: the "old" events are generated later from
149 the startup synthesised SIGHUP. */
Simon Kelleyceae00d2012-02-09 21:28:14 +0000150 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED);
Simon Kelley208b65c2006-08-05 21:41:37 +0100151 }
152
Simon Kelley1f15b812009-10-13 17:49:32 +0100153#ifdef HAVE_SCRIPT
Simon Kelley208b65c2006-08-05 21:41:37 +0100154 if (!daemon->lease_stream)
155 {
156 int rc = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000157
Simon Kelley208b65c2006-08-05 21:41:37 +0100158 /* shell returns 127 for "command not found", 126 for bad permissions. */
159 if (!leasestream || (rc = pclose(leasestream)) == -1 || WEXITSTATUS(rc) == 127 || WEXITSTATUS(rc) == 126)
160 {
161 if (WEXITSTATUS(rc) == 127)
162 errno = ENOENT;
163 else if (WEXITSTATUS(rc) == 126)
164 errno = EACCES;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100165 die(_("cannot run lease-init script %s: %s"), daemon->lease_change_command, EC_FILE);
Simon Kelley208b65c2006-08-05 21:41:37 +0100166 }
167
168 if (WEXITSTATUS(rc) != 0)
169 {
170 sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100171 die(_("lease-init script returned exit code %s"), daemon->dhcp_buff, WEXITSTATUS(rc) + EC_INIT_OFFSET);
Simon Kelley208b65c2006-08-05 21:41:37 +0100172 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000173 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100174#endif
Simon Kelley7cebd202006-05-06 14:13:33 +0100175
176 /* Some leases may have expired */
177 file_dirty = 0;
178 lease_prune(NULL, now);
179 dns_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000180}
181
Simon Kelley5aabfc72007-08-29 11:24:47 +0100182void lease_update_from_configs(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000183{
184 /* changes to the config may change current leases. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000185
Simon Kelley44a2a312004-03-10 20:04:35 +0000186 struct dhcp_lease *lease;
187 struct dhcp_config *config;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000188 char *name;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000189
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000190 for (lease = leases; lease; lease = lease->next)
Simon Kelley89500e32013-09-20 16:29:20 +0100191 if (lease->flags & (LEASE_TA | LEASE_NA))
192 continue;
193 else if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len,
194 lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) &&
195 (config->flags & CONFIG_NAME) &&
196 (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000197 lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NULL);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100198 else if ((name = host_from_dns(lease->addr)))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000199 lease_set_hostname(lease, name, 1, get_domain(lease->addr), NULL); /* updates auth flag only */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000200}
Simon Kelley4cb1b322012-02-06 14:30:41 +0000201
Simon Kelley5aabfc72007-08-29 11:24:47 +0100202static void ourprintf(int *errp, char *format, ...)
Simon Kelley7cebd202006-05-06 14:13:33 +0100203{
204 va_list ap;
205
206 va_start(ap, format);
207 if (!(*errp) && vfprintf(daemon->lease_stream, format, ap) < 0)
208 *errp = errno;
209 va_end(ap);
210}
211
Simon Kelley5aabfc72007-08-29 11:24:47 +0100212void lease_update_file(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000213{
214 struct dhcp_lease *lease;
Simon Kelley7cebd202006-05-06 14:13:33 +0100215 time_t next_event;
216 int i, err = 0;
217
Simon Kelley208b65c2006-08-05 21:41:37 +0100218 if (file_dirty != 0 && daemon->lease_stream)
Simon Kelley44a2a312004-03-10 20:04:35 +0000219 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000220 errno = 0;
221 rewind(daemon->lease_stream);
222 if (errno != 0 || ftruncate(fileno(daemon->lease_stream), 0) != 0)
Simon Kelley7cebd202006-05-06 14:13:33 +0100223 err = errno;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000224
225 for (lease = leases; lease; lease = lease->next)
226 {
Simon Kelleyc72daea2012-01-05 21:33:27 +0000227
228#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000229 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000230 continue;
231#endif
232
Simon Kelley44a2a312004-03-10 20:04:35 +0000233#ifdef HAVE_BROKEN_RTC
Simon Kelley5aabfc72007-08-29 11:24:47 +0100234 ourprintf(&err, "%u ", lease->length);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100235#else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100236 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100237#endif
Simon Kelleyc72daea2012-01-05 21:33:27 +0000238
Simon Kelley7cebd202006-05-06 14:13:33 +0100239 if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100240 ourprintf(&err, "%.2x-", lease->hwaddr_type);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100241 for (i = 0; i < lease->hwaddr_len; i++)
242 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100243 ourprintf(&err, "%.2x", lease->hwaddr[i]);
Simon Kelley7cebd202006-05-06 14:13:33 +0100244 if (i != lease->hwaddr_len - 1)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100245 ourprintf(&err, ":");
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100246 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000247
248 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100249
Simon Kelleyc72daea2012-01-05 21:33:27 +0000250 ourprintf(&err, " %s ", daemon->addrbuff);
Simon Kelley1f15b812009-10-13 17:49:32 +0100251 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
252
Simon Kelley0a852542005-03-23 20:28:59 +0000253 if (lease->clid && lease->clid_len != 0)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000254 {
255 for (i = 0; i < lease->clid_len - 1; i++)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100256 ourprintf(&err, "%.2x:", lease->clid[i]);
257 ourprintf(&err, "%.2x\n", lease->clid[i]);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000258 }
259 else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100260 ourprintf(&err, "*\n");
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000261 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100262
Simon Kelleyc72daea2012-01-05 21:33:27 +0000263#ifdef HAVE_DHCP6
264 if (daemon->duid)
265 {
266 ourprintf(&err, "duid ");
267 for (i = 0; i < daemon->duid_len - 1; i++)
268 ourprintf(&err, "%.2x:", daemon->duid[i]);
269 ourprintf(&err, "%.2x\n", daemon->duid[i]);
270
271 for (lease = leases; lease; lease = lease->next)
272 {
273
Simon Kelley4cb1b322012-02-06 14:30:41 +0000274 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000275 continue;
276
277#ifdef HAVE_BROKEN_RTC
278 ourprintf(&err, "%u ", lease->length);
279#else
280 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
281#endif
282
Simon Kelley89500e32013-09-20 16:29:20 +0100283 inet_ntop(AF_INET6, &lease->addr6, daemon->addrbuff, ADDRSTRLEN);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000284
Simon Kelley4cb1b322012-02-06 14:30:41 +0000285 ourprintf(&err, "%s%u %s ", (lease->flags & LEASE_TA) ? "T" : "",
Simon Kelley89500e32013-09-20 16:29:20 +0100286 lease->iaid, daemon->addrbuff);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000287 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
288
289 if (lease->clid && lease->clid_len != 0)
290 {
291 for (i = 0; i < lease->clid_len - 1; i++)
292 ourprintf(&err, "%.2x:", lease->clid[i]);
293 ourprintf(&err, "%.2x\n", lease->clid[i]);
294 }
295 else
296 ourprintf(&err, "*\n");
297 }
298 }
299#endif
300
Simon Kelley7cebd202006-05-06 14:13:33 +0100301 if (fflush(daemon->lease_stream) != 0 ||
302 fsync(fileno(daemon->lease_stream)) < 0)
303 err = errno;
304
305 if (!err)
306 file_dirty = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000307 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100308
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000309 /* Set alarm for when the first lease expires. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000310 next_event = 0;
311
312#ifdef HAVE_DHCP6
Simon Kelley353ae4d2012-03-19 20:07:51 +0000313 /* do timed RAs and determine when the next is, also pings to potential SLAAC addresses */
Simon Kelley1f776932012-12-16 19:46:08 +0000314 if (daemon->doing_ra)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000315 {
Simon Kelley919dd7c2012-05-12 15:23:09 +0100316 time_t event;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000317
Simon Kelley919dd7c2012-05-12 15:23:09 +0100318 if ((event = periodic_slaac(now, leases)) != 0)
319 {
320 if (next_event == 0 || difftime(next_event, event) > 0.0)
321 next_event = event;
322 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000323
Simon Kelley919dd7c2012-05-12 15:23:09 +0100324 if ((event = periodic_ra(now)) != 0)
325 {
326 if (next_event == 0 || difftime(next_event, event) > 0.0)
327 next_event = event;
328 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000329 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000330#endif
331
332 for (lease = leases; lease; lease = lease->next)
Simon Kelley7cebd202006-05-06 14:13:33 +0100333 if (lease->expires != 0 &&
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000334 (next_event == 0 || difftime(next_event, lease->expires) > 0.0))
335 next_event = lease->expires;
Simon Kelley7cebd202006-05-06 14:13:33 +0100336
337 if (err)
338 {
339 if (next_event == 0 || difftime(next_event, LEASE_RETRY + now) > 0.0)
340 next_event = LEASE_RETRY + now;
341
Simon Kelley7622fc02009-06-04 20:32:05 +0100342 my_syslog(MS_DHCP | LOG_ERR, _("failed to write %s: %s (retry in %us)"),
Simon Kelleyf2621c72007-04-29 19:47:21 +0100343 daemon->lease_file, strerror(err),
344 (unsigned int)difftime(next_event, now));
Simon Kelley7cebd202006-05-06 14:13:33 +0100345 }
346
Simon Kelley353ae4d2012-03-19 20:07:51 +0000347 send_alarm(next_event, now);
Simon Kelley44a2a312004-03-10 20:04:35 +0000348}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000349
Simon Kelley801ca9a2012-03-06 19:30:17 +0000350
Simon Kelley3f2873d2013-05-14 11:28:47 +0100351static int find_interface_v4(struct in_addr local, int if_index, char *label,
Simon Kelley801ca9a2012-03-06 19:30:17 +0000352 struct in_addr netmask, struct in_addr broadcast, void *vparam)
Simon Kelley44a2a312004-03-10 20:04:35 +0000353{
354 struct dhcp_lease *lease;
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100355 int prefix = netmask_length(netmask);
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800356
Simon Kelley3f2873d2013-05-14 11:28:47 +0100357 (void) label;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000358 (void) broadcast;
359 (void) vparam;
360
361 for (lease = leases; lease; lease = lease->next)
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100362 if (!(lease->flags & (LEASE_TA | LEASE_NA)) &&
363 is_same_net(local, lease->addr, netmask) &&
364 prefix > lease->new_prefixlen)
365 {
366 lease->new_interface = if_index;
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800367 lease->new_prefixlen = prefix;
368 }
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800369
Simon Kelley801ca9a2012-03-06 19:30:17 +0000370 return 1;
371}
372
373#ifdef HAVE_DHCP6
374static int find_interface_v6(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000375 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000376 int preferred, int valid, void *vparam)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000377{
378 struct dhcp_lease *lease;
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800379
Simon Kelley353ae4d2012-03-19 20:07:51 +0000380 (void)scope;
Simon Kelleybad7b872012-12-20 22:00:39 +0000381 (void)flags;
Simon Kelley1f776932012-12-16 19:46:08 +0000382 (void)preferred;
383 (void)valid;
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800384 (void)vparam;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000385
386 for (lease = leases; lease; lease = lease->next)
387 if ((lease->flags & (LEASE_TA | LEASE_NA)))
Lung-Pin Changdc8a1b12014-07-02 10:48:05 +0800388 if (is_same_net6(local, &lease->addr6, prefix) && prefix > lease->new_prefixlen) {
389 /* save prefix length for comparison, as we might get shorter matching
390 * prefix in upcoming netlink GETADDR responses
391 * */
392 lease->new_interface = if_index;
393 lease->new_prefixlen = prefix;
394 }
395
Simon Kelley801ca9a2012-03-06 19:30:17 +0000396 return 1;
397}
Simon Kelley353ae4d2012-03-19 20:07:51 +0000398
399void lease_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface)
400{
Simon Kelley5ef33272012-03-30 15:10:28 +0100401 /* We may be doing RA but not DHCPv4, in which case the lease
402 database may not exist and we have nothing to do anyway */
403 if (daemon->dhcp)
404 slaac_ping_reply(sender, packet, interface, leases);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000405}
406
Simon Kelley0c050242012-12-22 22:13:19 +0000407void lease_update_slaac(time_t now)
408{
409 /* Called when we contruct a new RA-names context, to add putative
410 new SLAAC addresses to existing leases. */
411
412 struct dhcp_lease *lease;
413
414 if (daemon->dhcp)
415 for (lease = leases; lease; lease = lease->next)
416 slaac_add_addrs(lease, now, 0);
417}
418
Simon Kelley801ca9a2012-03-06 19:30:17 +0000419#endif
420
421
422/* Find interfaces associated with leases at start-up. This gets updated as
423 we do DHCP transactions, but information about directly-connected subnets
424 is useful from scrips and necessary for determining SLAAC addresses from
425 start-time. */
Simon Kelley8b372702012-03-09 17:45:10 +0000426void lease_find_interfaces(time_t now)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000427{
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100428 struct dhcp_lease *lease;
429
430 for (lease = leases; lease; lease = lease->next)
431 lease->new_prefixlen = lease->new_interface = 0;
432
Simon Kelley353ae4d2012-03-19 20:07:51 +0000433 iface_enumerate(AF_INET, &now, find_interface_v4);
434#ifdef HAVE_DHCP6
435 iface_enumerate(AF_INET6, &now, find_interface_v6);
Simon Kelley3511a922013-11-07 10:28:11 +0000436#endif
Simon Kelley6d8e8ac2014-07-13 22:12:45 +0100437
438 for (lease = leases; lease; lease = lease->next)
439 if (lease->new_interface != 0)
440 lease_set_interface(lease, lease->new_interface, now);
Simon Kelley3511a922013-11-07 10:28:11 +0000441}
Simon Kelley8b372702012-03-09 17:45:10 +0000442
Simon Kelley3511a922013-11-07 10:28:11 +0000443#ifdef HAVE_DHCP6
444void lease_make_duid(time_t now)
445{
Simon Kelley8b372702012-03-09 17:45:10 +0000446 /* 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 +0000447 if (!daemon->duid && daemon->doing_dhcp6)
Simon Kelley8b372702012-03-09 17:45:10 +0000448 {
449 file_dirty = 1;
450 make_duid(now);
451 }
Simon Kelley801ca9a2012-03-06 19:30:17 +0000452}
Simon Kelley3511a922013-11-07 10:28:11 +0000453#endif
454
Simon Kelley801ca9a2012-03-06 19:30:17 +0000455
456
457
Simon Kelley353ae4d2012-03-19 20:07:51 +0000458void lease_update_dns(int force)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000459{
460 struct dhcp_lease *lease;
461
Simon Kelley353ae4d2012-03-19 20:07:51 +0000462 if (daemon->port != 0 && (dns_dirty || force))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000463 {
Simon Kelley8ff55672012-12-09 21:09:01 +0000464#ifndef HAVE_BROKEN_RTC
Simon Kelleye1ff4192012-12-09 17:08:47 +0000465 /* force transfer to authoritative secondaries */
466 daemon->soa_sn++;
Simon Kelley8ff55672012-12-09 21:09:01 +0000467#endif
Simon Kelleye1ff4192012-12-09 17:08:47 +0000468
Simon Kelley801ca9a2012-03-06 19:30:17 +0000469 cache_unhash_dhcp();
470
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000471 for (lease = leases; lease; lease = lease->next)
472 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000473 int prot = AF_INET;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000474
Simon Kelley4cb1b322012-02-06 14:30:41 +0000475#ifdef HAVE_DHCP6
476 if (lease->flags & (LEASE_TA | LEASE_NA))
477 prot = AF_INET6;
Simon Kelleyf444cdd2012-03-07 10:15:57 +0000478 else if (lease->hostname || lease->fqdn)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000479 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000480 struct slaac_address *slaac;
481
482 for (slaac = lease->slaac_address; slaac; slaac = slaac->next)
483 if (slaac->backoff == 0)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000484 {
Simon Kelley801ca9a2012-03-06 19:30:17 +0000485 if (lease->fqdn)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000486 cache_add_dhcp_entry(lease->fqdn, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000487 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000488 cache_add_dhcp_entry(lease->hostname, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000489 }
490 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000491
Simon Kelley9009d742008-11-14 20:04:27 +0000492 if (lease->fqdn)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000493 cache_add_dhcp_entry(lease->fqdn, prot,
Simon Kelley89500e32013-09-20 16:29:20 +0100494 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->addr6,
Simon Kelley4cb1b322012-02-06 14:30:41 +0000495 lease->expires);
Simon Kelley9009d742008-11-14 20:04:27 +0000496
Simon Kelley28866e92011-02-14 20:19:14 +0000497 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000498 cache_add_dhcp_entry(lease->hostname, prot,
Simon Kelley89500e32013-09-20 16:29:20 +0100499 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->addr6,
Simon Kelley4cb1b322012-02-06 14:30:41 +0000500 lease->expires);
Simon Kelley91543f42013-09-23 12:41:20 +0100501
502#else
503 if (lease->fqdn)
504 cache_add_dhcp_entry(lease->fqdn, prot, (struct all_addr *)&lease->addr, lease->expires);
505
506 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
507 cache_add_dhcp_entry(lease->hostname, prot, (struct all_addr *)&lease->addr, lease->expires);
508#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000509 }
510
511 dns_dirty = 0;
512 }
513}
514
515void lease_prune(struct dhcp_lease *target, time_t now)
516{
517 struct dhcp_lease *lease, *tmp, **up;
518
519 for (lease = leases, up = &leases; lease; lease = tmp)
520 {
521 tmp = lease->next;
522 if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target)
523 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100524 file_dirty = 1;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000525 if (lease->hostname)
Simon Kelley7cebd202006-05-06 14:13:33 +0100526 dns_dirty = 1;
527
Simon Kelleyc72daea2012-01-05 21:33:27 +0000528 *up = lease->next; /* unlink */
Simon Kelley7cebd202006-05-06 14:13:33 +0100529
530 /* Put on old_leases list 'till we
531 can run the script */
532 lease->next = old_leases;
533 old_leases = lease;
534
Simon Kelley44a2a312004-03-10 20:04:35 +0000535 leases_left++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000536 }
537 else
538 up = &lease->next;
539 }
540}
541
542
Simon Kelleycdeda282006-03-16 20:16:06 +0000543struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,
Simon Kelley0a852542005-03-23 20:28:59 +0000544 unsigned char *clid, int clid_len)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000545{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000546 struct dhcp_lease *lease;
547
Simon Kelley0a852542005-03-23 20:28:59 +0000548 if (clid)
549 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000550 {
551#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000552 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000553 continue;
554#endif
555 if (lease->clid && clid_len == lease->clid_len &&
556 memcmp(clid, lease->clid, clid_len) == 0)
557 return lease;
558 }
Simon Kelley0a852542005-03-23 20:28:59 +0000559
560 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000561 {
562#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000563 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000564 continue;
565#endif
566 if ((!lease->clid || !clid) &&
567 hw_len != 0 &&
568 lease->hwaddr_len == hw_len &&
569 lease->hwaddr_type == hw_type &&
570 memcmp(hwaddr, lease->hwaddr, hw_len) == 0)
571 return lease;
572 }
573
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000574 return NULL;
575}
576
577struct dhcp_lease *lease_find_by_addr(struct in_addr addr)
578{
579 struct dhcp_lease *lease;
580
581 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000582 {
583#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000584 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000585 continue;
586#endif
587 if (lease->addr.s_addr == addr.s_addr)
588 return lease;
589 }
590
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000591 return NULL;
592}
593
Simon Kelley52b92f42012-01-22 16:05:15 +0000594#ifdef HAVE_DHCP6
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000595/* find address for {CLID, IAID, address} */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000596struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len,
597 int lease_type, int iaid, struct in6_addr *addr)
Simon Kelley52b92f42012-01-22 16:05:15 +0000598{
599 struct dhcp_lease *lease;
600
601 for (lease = leases; lease; lease = lease->next)
602 {
Simon Kelley89500e32013-09-20 16:29:20 +0100603 if (!(lease->flags & lease_type) || lease->iaid != iaid)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000604 continue;
Simon Kelley52b92f42012-01-22 16:05:15 +0000605
Simon Kelley89500e32013-09-20 16:29:20 +0100606 if (!IN6_ARE_ADDR_EQUAL(&lease->addr6, addr))
Simon Kelley4cb1b322012-02-06 14:30:41 +0000607 continue;
608
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000609 if ((clid_len != lease->clid_len ||
Simon Kelley4cb1b322012-02-06 14:30:41 +0000610 memcmp(clid, lease->clid, clid_len) != 0))
611 continue;
612
Simon Kelley8b460612012-09-08 21:47:28 +0100613 return lease;
Simon Kelley52b92f42012-01-22 16:05:15 +0000614 }
615
616 return NULL;
617}
618
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000619/* reset "USED flags */
620void lease6_reset(void)
Simon Kelley8b460612012-09-08 21:47:28 +0100621{
622 struct dhcp_lease *lease;
623
624 for (lease = leases; lease; lease = lease->next)
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000625 lease->flags &= ~LEASE_USED;
626}
627
628/* enumerate all leases belonging to {CLID, IAID} */
629struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_type, unsigned char *clid, int clid_len, int iaid)
630{
631 struct dhcp_lease *lease;
632
633 if (!first)
634 first = leases;
Simon Kelley27cb3142013-04-02 20:06:39 +0100635 else
636 first = first->next;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000637
638 for (lease = first; lease; lease = lease->next)
Simon Kelley8b460612012-09-08 21:47:28 +0100639 {
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000640 if (lease->flags & LEASE_USED)
641 continue;
642
Simon Kelley89500e32013-09-20 16:29:20 +0100643 if (!(lease->flags & lease_type) || lease->iaid != iaid)
Simon Kelley8b460612012-09-08 21:47:28 +0100644 continue;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000645
646 if ((clid_len != lease->clid_len ||
647 memcmp(clid, lease->clid, clid_len) != 0))
648 continue;
649
650 return lease;
Simon Kelley8b460612012-09-08 21:47:28 +0100651 }
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000652
653 return NULL;
Simon Kelley8b460612012-09-08 21:47:28 +0100654}
655
Simon Kelley52b92f42012-01-22 16:05:15 +0000656struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr)
657{
658 struct dhcp_lease *lease;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000659
Simon Kelley52b92f42012-01-22 16:05:15 +0000660 for (lease = leases; lease; lease = lease->next)
661 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000662 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelley52b92f42012-01-22 16:05:15 +0000663 continue;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000664
Simon Kelley89500e32013-09-20 16:29:20 +0100665 if (is_same_net6(&lease->addr6, net, prefix) &&
666 (prefix == 128 || addr6part(&lease->addr6) == addr))
Simon Kelley52b92f42012-01-22 16:05:15 +0000667 return lease;
668 }
669
670 return NULL;
Simon Kelley07933802012-02-14 20:55:25 +0000671}
672
673/* Find largest assigned address in context */
674u64 lease_find_max_addr6(struct dhcp_context *context)
675{
676 struct dhcp_lease *lease;
677 u64 addr = addr6part(&context->start6);
678
679 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
680 for (lease = leases; lease; lease = lease->next)
681 {
Simon Kelley07933802012-02-14 20:55:25 +0000682 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
683 continue;
Simon Kelley6caacac2012-02-15 21:58:33 +0000684
Simon Kelley89500e32013-09-20 16:29:20 +0100685 if (is_same_net6(&lease->addr6, &context->start6, 64) &&
686 addr6part(&lease->addr6) > addr6part(&context->start6) &&
687 addr6part(&lease->addr6) <= addr6part(&context->end6) &&
688 addr6part(&lease->addr6) > addr)
689 addr = addr6part(&lease->addr6);
Simon Kelley07933802012-02-14 20:55:25 +0000690 }
691
692 return addr;
693}
694
Simon Kelley52b92f42012-01-22 16:05:15 +0000695#endif
696
Simon Kelley7de060b2011-08-26 17:24:52 +0100697/* Find largest assigned address in context */
698struct in_addr lease_find_max_addr(struct dhcp_context *context)
699{
700 struct dhcp_lease *lease;
701 struct in_addr addr = context->start;
702
703 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
704 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000705 {
706#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000707 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000708 continue;
709#endif
710 if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) &&
711 ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) &&
712 ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr)))
713 addr = lease->addr;
714 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100715
716 return addr;
717}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000718
Simon Kelleyc72daea2012-01-05 21:33:27 +0000719static struct dhcp_lease *lease_allocate(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000720{
721 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100722 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000723 return NULL;
724
Simon Kelley7cebd202006-05-06 14:13:33 +0100725 memset(lease, 0, sizeof(struct dhcp_lease));
Simon Kelley4cb1b322012-02-06 14:30:41 +0000726 lease->flags = LEASE_NEW;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000727 lease->expires = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100728#ifdef HAVE_BROKEN_RTC
729 lease->length = 0xffffffff; /* illegal value */
730#endif
Simon Kelley89500e32013-09-20 16:29:20 +0100731 lease->hwaddr_len = 256; /* illegal value */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000732 lease->next = leases;
733 leases = lease;
734
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100735 file_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000736 leases_left--;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000737
738 return lease;
739}
740
Simon Kelley52b92f42012-01-22 16:05:15 +0000741struct dhcp_lease *lease4_allocate(struct in_addr addr)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000742{
743 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100744 if (lease)
Simon Kelley89500e32013-09-20 16:29:20 +0100745 lease->addr = addr;
746
Simon Kelleyc72daea2012-01-05 21:33:27 +0000747 return lease;
748}
749
750#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000751struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000752{
753 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100754
755 if (lease)
756 {
Simon Kelley89500e32013-09-20 16:29:20 +0100757 lease->addr6 = *addrp;
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100758 lease->flags |= lease_type;
Simon Kelley89500e32013-09-20 16:29:20 +0100759 lease->iaid = 0;
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100760 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000761
762 return lease;
763}
764#endif
765
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100766void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000767{
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000768 time_t exp;
769
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100770 if (len == 0xffffffff)
771 {
772 exp = 0;
773 len = 0;
774 }
Simon Kelleydd0e0a32014-01-22 11:16:59 +0000775 else
776 {
777 exp = now + (time_t)len;
778 /* Check for 2038 overflow. Make the lease
779 inifinite in that case, as the least disruptive
780 thing we can do. */
781 if (difftime(exp, now) <= 0.0)
782 exp = 0;
783 }
784
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000785 if (exp != lease->expires)
Simon Kelley0a852542005-03-23 20:28:59 +0000786 {
Simon Kelley0a852542005-03-23 20:28:59 +0000787 dns_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100788 lease->expires = exp;
789#ifndef HAVE_BROKEN_RTC
Simon Kelley4cb1b322012-02-06 14:30:41 +0000790 lease->flags |= LEASE_AUX_CHANGED;
791 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100792#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000793 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100794
795#ifdef HAVE_BROKEN_RTC
796 if (len != lease->length)
797 {
798 lease->length = len;
Simon Kelleyb7f40202012-02-29 21:43:37 +0000799 lease->flags |= LEASE_AUX_CHANGED;
800 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100801 }
802#endif
803}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000804
Simon Kelley89500e32013-09-20 16:29:20 +0100805#ifdef HAVE_DHCP6
806void lease_set_iaid(struct dhcp_lease *lease, int iaid)
807{
808 if (lease->iaid != iaid)
809 {
810 lease->iaid = iaid;
811 lease->flags |= LEASE_CHANGED;
812 }
813}
814#endif
815
Nicolas Cavallari64bcff12015-04-28 21:55:18 +0100816void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr,
817 const unsigned char *clid, int hw_len, int hw_type,
818 int clid_len, time_t now, int force)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000819{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000820#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100821 int change = force;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000822 lease->flags |= LEASE_HAVE_HWADDR;
823#endif
824
Simon Kelleya9ab7322012-04-28 11:29:37 +0100825 (void)force;
Vladislav Grishenko408c3682013-09-24 16:18:49 +0100826 (void)now;
Simon Kelleya9ab7322012-04-28 11:29:37 +0100827
Simon Kelleycdeda282006-03-16 20:16:06 +0000828 if (hw_len != lease->hwaddr_len ||
829 hw_type != lease->hwaddr_type ||
Simon Kelley7cebd202006-05-06 14:13:33 +0100830 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000831 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000832 if (hw_len != 0)
833 memcpy(lease->hwaddr, hwaddr, hw_len);
Simon Kelleycdeda282006-03-16 20:16:06 +0000834 lease->hwaddr_len = hw_len;
835 lease->hwaddr_type = hw_type;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000836 lease->flags |= LEASE_CHANGED;
837 file_dirty = 1; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000838 }
Simon Kelley0a852542005-03-23 20:28:59 +0000839
840 /* only update clid when one is available, stops packets
841 without a clid removing the record. Lease init uses
842 clid_len == 0 for no clid. */
843 if (clid_len != 0 && clid)
844 {
845 if (!lease->clid)
846 lease->clid_len = 0;
847
848 if (lease->clid_len != clid_len)
849 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000850 lease->flags |= LEASE_AUX_CHANGED;
851 file_dirty = 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100852 free(lease->clid);
853 if (!(lease->clid = whine_malloc(clid_len)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100854 return;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000855#ifdef HAVE_DHCP6
856 change = 1;
857#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000858 }
859 else if (memcmp(lease->clid, clid, clid_len) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000860 {
861 lease->flags |= LEASE_AUX_CHANGED;
862 file_dirty = 1;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000863#ifdef HAVE_DHCP6
864 change = 1;
865#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +0000866 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000867
Simon Kelley0a852542005-03-23 20:28:59 +0000868 lease->clid_len = clid_len;
869 memcpy(lease->clid, clid, clid_len);
870 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000871
872#ifdef HAVE_DHCP6
873 if (change)
Simon Kelleya9ab7322012-04-28 11:29:37 +0100874 slaac_add_addrs(lease, now, force);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000875#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000876}
877
Simon Kelley9009d742008-11-14 20:04:27 +0000878static void kill_name(struct dhcp_lease *lease)
879{
880 /* run script to say we lost our old name */
881
882 /* this shouldn't happen unless updates are very quick and the
883 script very slow, we just avoid a memory leak if it does. */
884 free(lease->old_hostname);
885
886 /* If we know the fqdn, pass that. The helper will derive the
Simon Kelley4cb1b322012-02-06 14:30:41 +0000887 unqualified name from it, free the unqualified name here. */
Simon Kelley9009d742008-11-14 20:04:27 +0000888
889 if (lease->fqdn)
890 {
891 lease->old_hostname = lease->fqdn;
892 free(lease->hostname);
893 }
894 else
895 lease->old_hostname = lease->hostname;
896
897 lease->hostname = lease->fqdn = NULL;
898}
899
Nicolas Cavallari64bcff12015-04-28 21:55:18 +0100900void lease_set_hostname(struct dhcp_lease *lease, const char *name, int auth, char *domain, char *config_domain)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000901{
902 struct dhcp_lease *lease_tmp;
903 char *new_name = NULL, *new_fqdn = NULL;
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000904
905 if (config_domain && (!domain || !hostname_isequal(domain, config_domain)))
906 my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, name);
Simon Kelley9009d742008-11-14 20:04:27 +0000907
Simon Kelleya2226412004-05-13 20:27:08 +0100908 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000909 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000910 if (auth)
911 lease->flags |= LEASE_AUTH_NAME;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000912 return;
913 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100914
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000915 if (!name && !lease->hostname)
916 return;
917
918 /* If a machine turns up on a new net without dropping the old lease,
919 or two machines claim the same name, then we end up with two interfaces with
Simon Kelleyb8187c82005-11-26 21:46:27 +0000920 the same name. Check for that here and remove the name from the old lease.
Simon Kelley4cb1b322012-02-06 14:30:41 +0000921 Note that IPv6 leases are different. All the leases to the same DUID are
922 allowed the same name.
923
Simon Kelleyb8187c82005-11-26 21:46:27 +0000924 Don't allow a name from the client to override a name from dnsmasq config. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000925
926 if (name)
927 {
Simon Kelley9009d742008-11-14 20:04:27 +0000928 if ((new_name = whine_malloc(strlen(name) + 1)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000929 {
Simon Kelley9009d742008-11-14 20:04:27 +0000930 strcpy(new_name, name);
Simon Kelley4cb1b322012-02-06 14:30:41 +0000931 if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2)))
Simon Kelley9009d742008-11-14 20:04:27 +0000932 {
933 strcpy(new_fqdn, name);
934 strcat(new_fqdn, ".");
Simon Kelley4cb1b322012-02-06 14:30:41 +0000935 strcat(new_fqdn, domain);
Simon Kelley9009d742008-11-14 20:04:27 +0000936 }
937 }
938
939 /* Depending on mode, we check either unqualified name or FQDN. */
940 for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next)
941 {
Simon Kelley28866e92011-02-14 20:19:14 +0000942 if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +0000943 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000944 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn))
Simon Kelley9009d742008-11-14 20:04:27 +0000945 continue;
946 }
947 else
948 {
949 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
950 continue;
951 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000952
953 if (lease->flags & (LEASE_TA | LEASE_NA))
954 {
955 if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA)))
956 continue;
957
Simon Kelleyceae00d2012-02-09 21:28:14 +0000958 /* another lease for the same DUID is OK for IPv6 */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000959 if (lease->clid_len == lease_tmp->clid_len &&
960 lease->clid && lease_tmp->clid &&
961 memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0)
962 continue;
963 }
964 else if (lease_tmp->flags & (LEASE_TA | LEASE_NA))
965 continue;
966
967 if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth)
Simon Kelley9009d742008-11-14 20:04:27 +0000968 {
969 free(new_name);
970 free(new_fqdn);
971 return;
972 }
973
974 kill_name(lease_tmp);
975 break;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000976 }
977 }
978
979 if (lease->hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000980 kill_name(lease);
Simon Kelley16972692006-10-16 20:04:18 +0100981
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000982 lease->hostname = new_name;
983 lease->fqdn = new_fqdn;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000984
985 if (auth)
986 lease->flags |= LEASE_AUTH_NAME;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000987
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100988 file_dirty = 1;
Simon Kelley7cebd202006-05-06 14:13:33 +0100989 dns_dirty = 1;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000990 lease->flags |= LEASE_CHANGED; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000991}
992
Simon Kelley353ae4d2012-03-19 20:07:51 +0000993void lease_set_interface(struct dhcp_lease *lease, int interface, time_t now)
Simon Kelley824af852008-02-12 20:43:05 +0000994{
Vladislav Grishenko408c3682013-09-24 16:18:49 +0100995 (void)now;
996
Simon Kelley824af852008-02-12 20:43:05 +0000997 if (lease->last_interface == interface)
998 return;
999
1000 lease->last_interface = interface;
Simon Kelley4cb1b322012-02-06 14:30:41 +00001001 lease->flags |= LEASE_CHANGED;
Simon Kelley353ae4d2012-03-19 20:07:51 +00001002
1003#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +01001004 slaac_add_addrs(lease, now, 0);
Simon Kelley353ae4d2012-03-19 20:07:51 +00001005#endif
Simon Kelley824af852008-02-12 20:43:05 +00001006}
1007
Simon Kelley5aabfc72007-08-29 11:24:47 +01001008void rerun_scripts(void)
1009{
1010 struct dhcp_lease *lease;
1011
1012 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001013 lease->flags |= LEASE_CHANGED;
Simon Kelley5aabfc72007-08-29 11:24:47 +01001014}
1015
Simon Kelley7cebd202006-05-06 14:13:33 +01001016/* deleted leases get transferred to the old_leases list.
1017 remove them here, after calling the lease change
Simon Kelley16972692006-10-16 20:04:18 +01001018 script. Also run the lease change script on new/modified leases.
1019
1020 Return zero if nothing to do. */
Simon Kelley5aabfc72007-08-29 11:24:47 +01001021int do_script_run(time_t now)
Simon Kelley7cebd202006-05-06 14:13:33 +01001022{
1023 struct dhcp_lease *lease;
1024
Vladislav Grishenko408c3682013-09-24 16:18:49 +01001025 (void)now;
1026
Simon Kelley9009d742008-11-14 20:04:27 +00001027#ifdef HAVE_DBUS
1028 /* If we're going to be sending DBus signals, but the connection is not yet up,
1029 delay everything until it is. */
Simon Kelley28866e92011-02-14 20:19:14 +00001030 if (option_bool(OPT_DBUS) && !daemon->dbus)
Simon Kelley9009d742008-11-14 20:04:27 +00001031 return 0;
1032#endif
1033
Simon Kelley16972692006-10-16 20:04:18 +01001034 if (old_leases)
Simon Kelley7cebd202006-05-06 14:13:33 +01001035 {
Simon Kelley7cebd202006-05-06 14:13:33 +01001036 lease = old_leases;
Simon Kelley16972692006-10-16 20:04:18 +01001037
1038 /* If the lease still has an old_hostname, do the "old" action on that first */
1039 if (lease->old_hostname)
1040 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001041#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001042 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1043#endif
Simon Kelley16972692006-10-16 20:04:18 +01001044 free(lease->old_hostname);
1045 lease->old_hostname = NULL;
1046 return 1;
1047 }
1048 else
1049 {
Simon Kelley353ae4d2012-03-19 20:07:51 +00001050#ifdef HAVE_DHCP6
1051 struct slaac_address *slaac, *tmp;
1052 for (slaac = lease->slaac_address; slaac; slaac = tmp)
1053 {
1054 tmp = slaac->next;
1055 free(slaac);
1056 }
1057#endif
Simon Kelley9009d742008-11-14 20:04:27 +00001058 kill_name(lease);
Simon Kelley1f15b812009-10-13 17:49:32 +01001059#ifdef HAVE_SCRIPT
Simon Kelley9009d742008-11-14 20:04:27 +00001060 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001061#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001062#ifdef HAVE_DBUS
1063 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
1064#endif
Simon Kelley16972692006-10-16 20:04:18 +01001065 old_leases = lease->next;
1066
Simon Kelley9009d742008-11-14 20:04:27 +00001067 free(lease->old_hostname);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001068 free(lease->clid);
Simon Kelley316e2732010-01-22 20:16:09 +00001069 free(lease->extradata);
Simon Kelley16972692006-10-16 20:04:18 +01001070 free(lease);
1071
1072 return 1;
1073 }
Simon Kelley7cebd202006-05-06 14:13:33 +01001074 }
Simon Kelley16972692006-10-16 20:04:18 +01001075
1076 /* make sure we announce the loss of a hostname before its new location. */
1077 for (lease = leases; lease; lease = lease->next)
1078 if (lease->old_hostname)
1079 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001080#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001081 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1082#endif
Simon Kelley16972692006-10-16 20:04:18 +01001083 free(lease->old_hostname);
1084 lease->old_hostname = NULL;
1085 return 1;
1086 }
1087
Simon Kelley7cebd202006-05-06 14:13:33 +01001088 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001089 if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
1090 ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)))
Simon Kelley7cebd202006-05-06 14:13:33 +01001091 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001092#ifdef HAVE_SCRIPT
Simon Kelley4cb1b322012-02-06 14:30:41 +00001093 queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley9009d742008-11-14 20:04:27 +00001094 lease->fqdn ? lease->fqdn : lease->hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001095#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001096#ifdef HAVE_DBUS
Simon Kelley4cb1b322012-02-06 14:30:41 +00001097 emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley1f15b812009-10-13 17:49:32 +01001098 lease->fqdn ? lease->fqdn : lease->hostname);
1099#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +00001100 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED);
Simon Kelley16972692006-10-16 20:04:18 +01001101
Simon Kelley316e2732010-01-22 20:16:09 +00001102 /* this is used for the "add" call, then junked, since they're not in the database */
1103 free(lease->extradata);
1104 lease->extradata = NULL;
Simon Kelley16972692006-10-16 20:04:18 +01001105
1106 return 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001107 }
Simon Kelley16972692006-10-16 20:04:18 +01001108
1109 return 0; /* nothing to do */
Simon Kelley7cebd202006-05-06 14:13:33 +01001110}
Simon Kelley7622fc02009-06-04 20:32:05 +01001111
Simon Kelleyceae00d2012-02-09 21:28:14 +00001112#ifdef HAVE_SCRIPT
1113void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim)
1114{
1115 unsigned int i;
1116
1117 /* check for embeded NULLs */
1118 for (i = 0; i < len; i++)
1119 if (data[i] == 0)
1120 {
1121 len = i;
1122 break;
1123 }
1124
1125 if ((lease->extradata_size - lease->extradata_len) < (len + 1))
1126 {
1127 size_t newsz = lease->extradata_len + len + 100;
1128 unsigned char *new = whine_malloc(newsz);
1129
1130 if (!new)
1131 return;
1132
1133 if (lease->extradata)
1134 {
1135 memcpy(new, lease->extradata, lease->extradata_len);
1136 free(lease->extradata);
1137 }
1138
1139 lease->extradata = new;
1140 lease->extradata_size = newsz;
1141 }
1142
1143 if (len != 0)
1144 memcpy(lease->extradata + lease->extradata_len, data, len);
1145 lease->extradata[lease->extradata_len + len] = delim;
1146 lease->extradata_len += len + 1;
1147}
1148#endif
1149
Simon Kelley7622fc02009-06-04 20:32:05 +01001150#endif
Simon Kelley7cebd202006-05-06 14:13:33 +01001151
1152
1153
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001154