blob: a29cd9e5ba59e388aed6280c9497c7fc41f0eef9 [file] [log] [blame]
Simon Kelley61744352013-01-31 14:34:40 +00001/* dnsmasq is Copyright (c) 2000-2013 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;
111
Simon Kelley4cb1b322012-02-06 14:30:41 +0000112 if (s[0] == 'T')
113 {
114 lease_type = LEASE_TA;
115 s++;
116 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000117
118 hw_type = atoi(s);
Simon Kelleyceae00d2012-02-09 21:28:14 +0000119
120 if ((lease = lease6_allocate(&addr.addr.addr6, lease_type)))
121 {
Simon Kelleya9ab7322012-04-28 11:29:37 +0100122 lease_set_hwaddr(lease, NULL, (unsigned char *)daemon->packet, 0, hw_type, clid_len, now, 0);
Simon Kelleyceae00d2012-02-09 21:28:14 +0000123
124 if (strcmp(daemon->dhcp_buff, "*") != 0)
125 lease_set_hostname(lease, daemon->dhcp_buff, 0, get_domain6((struct in6_addr *)lease->hwaddr), NULL);
126 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000127 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000128#endif
Simon Kelleyc72daea2012-01-05 21:33:27 +0000129 else
Simon Kelleyceae00d2012-02-09 21:28:14 +0000130 break;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000131
Simon Kelleyc72daea2012-01-05 21:33:27 +0000132 if (!lease)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100133 die (_("too many stored leases"), NULL, EC_MISC);
134
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100135#ifdef HAVE_BROKEN_RTC
Simon Kelley208b65c2006-08-05 21:41:37 +0100136 if (ei != 0)
137 lease->expires = (time_t)ei + now;
138 else
139 lease->expires = (time_t)0;
140 lease->length = ei;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100141#else
Simon Kelley208b65c2006-08-05 21:41:37 +0100142 /* strictly time_t is opaque, but this hack should work on all sane systems,
143 even when sizeof(time_t) == 8 */
144 lease->expires = (time_t)ei;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100145#endif
Simon Kelley208b65c2006-08-05 21:41:37 +0100146
Simon Kelley5aabfc72007-08-29 11:24:47 +0100147 /* set these correctly: the "old" events are generated later from
148 the startup synthesised SIGHUP. */
Simon Kelleyceae00d2012-02-09 21:28:14 +0000149 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED);
Simon Kelley208b65c2006-08-05 21:41:37 +0100150 }
151
Simon Kelley1f15b812009-10-13 17:49:32 +0100152#ifdef HAVE_SCRIPT
Simon Kelley208b65c2006-08-05 21:41:37 +0100153 if (!daemon->lease_stream)
154 {
155 int rc = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000156
Simon Kelley208b65c2006-08-05 21:41:37 +0100157 /* shell returns 127 for "command not found", 126 for bad permissions. */
158 if (!leasestream || (rc = pclose(leasestream)) == -1 || WEXITSTATUS(rc) == 127 || WEXITSTATUS(rc) == 126)
159 {
160 if (WEXITSTATUS(rc) == 127)
161 errno = ENOENT;
162 else if (WEXITSTATUS(rc) == 126)
163 errno = EACCES;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100164 die(_("cannot run lease-init script %s: %s"), daemon->lease_change_command, EC_FILE);
Simon Kelley208b65c2006-08-05 21:41:37 +0100165 }
166
167 if (WEXITSTATUS(rc) != 0)
168 {
169 sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100170 die(_("lease-init script returned exit code %s"), daemon->dhcp_buff, WEXITSTATUS(rc) + EC_INIT_OFFSET);
Simon Kelley208b65c2006-08-05 21:41:37 +0100171 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000172 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100173#endif
Simon Kelley7cebd202006-05-06 14:13:33 +0100174
175 /* Some leases may have expired */
176 file_dirty = 0;
177 lease_prune(NULL, now);
178 dns_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000179}
180
Simon Kelley5aabfc72007-08-29 11:24:47 +0100181void lease_update_from_configs(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000182{
183 /* changes to the config may change current leases. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000184
Simon Kelley44a2a312004-03-10 20:04:35 +0000185 struct dhcp_lease *lease;
186 struct dhcp_config *config;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000187 char *name;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000188
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000189 for (lease = leases; lease; lease = lease->next)
Simon Kelleycdeda282006-03-16 20:16:06 +0000190 if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len,
191 lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) &&
Simon Kelleyb8187c82005-11-26 21:46:27 +0000192 (config->flags & CONFIG_NAME) &&
193 (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000194 lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NULL);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100195 else if ((name = host_from_dns(lease->addr)))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000196 lease_set_hostname(lease, name, 1, get_domain(lease->addr), NULL); /* updates auth flag only */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000197}
Simon Kelley4cb1b322012-02-06 14:30:41 +0000198
Simon Kelley5aabfc72007-08-29 11:24:47 +0100199static void ourprintf(int *errp, char *format, ...)
Simon Kelley7cebd202006-05-06 14:13:33 +0100200{
201 va_list ap;
202
203 va_start(ap, format);
204 if (!(*errp) && vfprintf(daemon->lease_stream, format, ap) < 0)
205 *errp = errno;
206 va_end(ap);
207}
208
Simon Kelley5aabfc72007-08-29 11:24:47 +0100209void lease_update_file(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000210{
211 struct dhcp_lease *lease;
Simon Kelley7cebd202006-05-06 14:13:33 +0100212 time_t next_event;
213 int i, err = 0;
214
Simon Kelley208b65c2006-08-05 21:41:37 +0100215 if (file_dirty != 0 && daemon->lease_stream)
Simon Kelley44a2a312004-03-10 20:04:35 +0000216 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000217 errno = 0;
218 rewind(daemon->lease_stream);
219 if (errno != 0 || ftruncate(fileno(daemon->lease_stream), 0) != 0)
Simon Kelley7cebd202006-05-06 14:13:33 +0100220 err = errno;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000221
222 for (lease = leases; lease; lease = lease->next)
223 {
Simon Kelleyc72daea2012-01-05 21:33:27 +0000224
225#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000226 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000227 continue;
228#endif
229
Simon Kelley44a2a312004-03-10 20:04:35 +0000230#ifdef HAVE_BROKEN_RTC
Simon Kelley5aabfc72007-08-29 11:24:47 +0100231 ourprintf(&err, "%u ", lease->length);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100232#else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100233 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100234#endif
Simon Kelleyc72daea2012-01-05 21:33:27 +0000235
Simon Kelley7cebd202006-05-06 14:13:33 +0100236 if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100237 ourprintf(&err, "%.2x-", lease->hwaddr_type);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100238 for (i = 0; i < lease->hwaddr_len; i++)
239 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100240 ourprintf(&err, "%.2x", lease->hwaddr[i]);
Simon Kelley7cebd202006-05-06 14:13:33 +0100241 if (i != lease->hwaddr_len - 1)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100242 ourprintf(&err, ":");
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100243 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000244
245 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100246
Simon Kelleyc72daea2012-01-05 21:33:27 +0000247 ourprintf(&err, " %s ", daemon->addrbuff);
Simon Kelley1f15b812009-10-13 17:49:32 +0100248 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
249
Simon Kelley0a852542005-03-23 20:28:59 +0000250 if (lease->clid && lease->clid_len != 0)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000251 {
252 for (i = 0; i < lease->clid_len - 1; i++)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100253 ourprintf(&err, "%.2x:", lease->clid[i]);
254 ourprintf(&err, "%.2x\n", lease->clid[i]);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000255 }
256 else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100257 ourprintf(&err, "*\n");
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000258 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100259
Simon Kelleyc72daea2012-01-05 21:33:27 +0000260#ifdef HAVE_DHCP6
261 if (daemon->duid)
262 {
263 ourprintf(&err, "duid ");
264 for (i = 0; i < daemon->duid_len - 1; i++)
265 ourprintf(&err, "%.2x:", daemon->duid[i]);
266 ourprintf(&err, "%.2x\n", daemon->duid[i]);
267
268 for (lease = leases; lease; lease = lease->next)
269 {
270
Simon Kelley4cb1b322012-02-06 14:30:41 +0000271 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000272 continue;
273
274#ifdef HAVE_BROKEN_RTC
275 ourprintf(&err, "%u ", lease->length);
276#else
277 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
278#endif
279
280 inet_ntop(AF_INET6, lease->hwaddr, daemon->addrbuff, ADDRSTRLEN);
281
Simon Kelley4cb1b322012-02-06 14:30:41 +0000282 ourprintf(&err, "%s%u %s ", (lease->flags & LEASE_TA) ? "T" : "",
283 lease->hwaddr_type, daemon->addrbuff);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000284 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
285
286 if (lease->clid && lease->clid_len != 0)
287 {
288 for (i = 0; i < lease->clid_len - 1; i++)
289 ourprintf(&err, "%.2x:", lease->clid[i]);
290 ourprintf(&err, "%.2x\n", lease->clid[i]);
291 }
292 else
293 ourprintf(&err, "*\n");
294 }
295 }
296#endif
297
Simon Kelley7cebd202006-05-06 14:13:33 +0100298 if (fflush(daemon->lease_stream) != 0 ||
299 fsync(fileno(daemon->lease_stream)) < 0)
300 err = errno;
301
302 if (!err)
303 file_dirty = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000304 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100305
306 /* Set alarm for when the first lease expires + slop. */
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000307 next_event = 0;
308
309#ifdef HAVE_DHCP6
Simon Kelley353ae4d2012-03-19 20:07:51 +0000310 /* do timed RAs and determine when the next is, also pings to potential SLAAC addresses */
Simon Kelley1f776932012-12-16 19:46:08 +0000311 if (daemon->doing_ra)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000312 {
Simon Kelley919dd7c2012-05-12 15:23:09 +0100313 time_t event;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000314
Simon Kelley919dd7c2012-05-12 15:23:09 +0100315 if ((event = periodic_slaac(now, leases)) != 0)
316 {
317 if (next_event == 0 || difftime(next_event, event) > 0.0)
318 next_event = event;
319 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000320
Simon Kelley919dd7c2012-05-12 15:23:09 +0100321 if ((event = periodic_ra(now)) != 0)
322 {
323 if (next_event == 0 || difftime(next_event, event) > 0.0)
324 next_event = event;
325 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000326 }
Simon Kelleyc5ad4e72012-02-24 16:06:20 +0000327#endif
328
329 for (lease = leases; lease; lease = lease->next)
Simon Kelley7cebd202006-05-06 14:13:33 +0100330 if (lease->expires != 0 &&
331 (next_event == 0 || difftime(next_event, lease->expires + 10) > 0.0))
332 next_event = lease->expires + 10;
333
334 if (err)
335 {
336 if (next_event == 0 || difftime(next_event, LEASE_RETRY + now) > 0.0)
337 next_event = LEASE_RETRY + now;
338
Simon Kelley7622fc02009-06-04 20:32:05 +0100339 my_syslog(MS_DHCP | LOG_ERR, _("failed to write %s: %s (retry in %us)"),
Simon Kelleyf2621c72007-04-29 19:47:21 +0100340 daemon->lease_file, strerror(err),
341 (unsigned int)difftime(next_event, now));
Simon Kelley7cebd202006-05-06 14:13:33 +0100342 }
343
Simon Kelley353ae4d2012-03-19 20:07:51 +0000344 send_alarm(next_event, now);
Simon Kelley44a2a312004-03-10 20:04:35 +0000345}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000346
Simon Kelley801ca9a2012-03-06 19:30:17 +0000347
348static int find_interface_v4(struct in_addr local, int if_index,
349 struct in_addr netmask, struct in_addr broadcast, void *vparam)
Simon Kelley44a2a312004-03-10 20:04:35 +0000350{
351 struct dhcp_lease *lease;
352
Simon Kelley801ca9a2012-03-06 19:30:17 +0000353 (void) broadcast;
354 (void) vparam;
355
356 for (lease = leases; lease; lease = lease->next)
357 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
358 if (is_same_net(local, lease->addr, netmask))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000359 lease_set_interface(lease, if_index, *((time_t *)vparam));
Simon Kelley801ca9a2012-03-06 19:30:17 +0000360
361 return 1;
362}
363
364#ifdef HAVE_DHCP6
365static int find_interface_v6(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000366 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000367 int preferred, int valid, void *vparam)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000368{
369 struct dhcp_lease *lease;
370
Simon Kelley353ae4d2012-03-19 20:07:51 +0000371 (void)scope;
Simon Kelleybad7b872012-12-20 22:00:39 +0000372 (void)flags;
Simon Kelley1f776932012-12-16 19:46:08 +0000373 (void)preferred;
374 (void)valid;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000375
376 for (lease = leases; lease; lease = lease->next)
377 if ((lease->flags & (LEASE_TA | LEASE_NA)))
378 if (is_same_net6(local, (struct in6_addr *)&lease->hwaddr, prefix))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000379 lease_set_interface(lease, if_index, *((time_t *)vparam));
Simon Kelley801ca9a2012-03-06 19:30:17 +0000380
381 return 1;
382}
Simon Kelley353ae4d2012-03-19 20:07:51 +0000383
384void lease_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface)
385{
Simon Kelley5ef33272012-03-30 15:10:28 +0100386 /* We may be doing RA but not DHCPv4, in which case the lease
387 database may not exist and we have nothing to do anyway */
388 if (daemon->dhcp)
389 slaac_ping_reply(sender, packet, interface, leases);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000390}
391
Simon Kelley0c050242012-12-22 22:13:19 +0000392void lease_update_slaac(time_t now)
393{
394 /* Called when we contruct a new RA-names context, to add putative
395 new SLAAC addresses to existing leases. */
396
397 struct dhcp_lease *lease;
398
399 if (daemon->dhcp)
400 for (lease = leases; lease; lease = lease->next)
401 slaac_add_addrs(lease, now, 0);
402}
403
Simon Kelley801ca9a2012-03-06 19:30:17 +0000404#endif
405
406
407/* Find interfaces associated with leases at start-up. This gets updated as
408 we do DHCP transactions, but information about directly-connected subnets
409 is useful from scrips and necessary for determining SLAAC addresses from
410 start-time. */
Simon Kelley8b372702012-03-09 17:45:10 +0000411void lease_find_interfaces(time_t now)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000412{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000413 iface_enumerate(AF_INET, &now, find_interface_v4);
414#ifdef HAVE_DHCP6
415 iface_enumerate(AF_INET6, &now, find_interface_v6);
Simon Kelley8b372702012-03-09 17:45:10 +0000416
417 /* If we're not doing DHCPv6, and there are not v6 leases, don't add the DUID to the database */
418 if (!daemon->duid && daemon->dhcp6)
419 {
420 file_dirty = 1;
421 make_duid(now);
422 }
Simon Kelley801ca9a2012-03-06 19:30:17 +0000423#endif
424}
425
426
427
Simon Kelley353ae4d2012-03-19 20:07:51 +0000428void lease_update_dns(int force)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000429{
430 struct dhcp_lease *lease;
431
Simon Kelley353ae4d2012-03-19 20:07:51 +0000432 if (daemon->port != 0 && (dns_dirty || force))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000433 {
Simon Kelley8ff55672012-12-09 21:09:01 +0000434#ifndef HAVE_BROKEN_RTC
Simon Kelleye1ff4192012-12-09 17:08:47 +0000435 /* force transfer to authoritative secondaries */
436 daemon->soa_sn++;
Simon Kelley8ff55672012-12-09 21:09:01 +0000437#endif
Simon Kelleye1ff4192012-12-09 17:08:47 +0000438
Simon Kelley801ca9a2012-03-06 19:30:17 +0000439 cache_unhash_dhcp();
440
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000441 for (lease = leases; lease; lease = lease->next)
442 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000443 int prot = AF_INET;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000444
Simon Kelley4cb1b322012-02-06 14:30:41 +0000445#ifdef HAVE_DHCP6
446 if (lease->flags & (LEASE_TA | LEASE_NA))
447 prot = AF_INET6;
Simon Kelleyf444cdd2012-03-07 10:15:57 +0000448 else if (lease->hostname || lease->fqdn)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000449 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000450 struct slaac_address *slaac;
451
452 for (slaac = lease->slaac_address; slaac; slaac = slaac->next)
453 if (slaac->backoff == 0)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000454 {
Simon Kelley801ca9a2012-03-06 19:30:17 +0000455 if (lease->fqdn)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000456 cache_add_dhcp_entry(lease->fqdn, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000457 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000458 cache_add_dhcp_entry(lease->hostname, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000459 }
460 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000461#endif
462
Simon Kelley9009d742008-11-14 20:04:27 +0000463 if (lease->fqdn)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000464 cache_add_dhcp_entry(lease->fqdn, prot,
465 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr,
466 lease->expires);
Simon Kelley9009d742008-11-14 20:04:27 +0000467
Simon Kelley28866e92011-02-14 20:19:14 +0000468 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000469 cache_add_dhcp_entry(lease->hostname, prot,
470 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr,
471 lease->expires);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000472 }
473
474 dns_dirty = 0;
475 }
476}
477
478void lease_prune(struct dhcp_lease *target, time_t now)
479{
480 struct dhcp_lease *lease, *tmp, **up;
481
482 for (lease = leases, up = &leases; lease; lease = tmp)
483 {
484 tmp = lease->next;
485 if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target)
486 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100487 file_dirty = 1;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000488 if (lease->hostname)
Simon Kelley7cebd202006-05-06 14:13:33 +0100489 dns_dirty = 1;
490
Simon Kelleyc72daea2012-01-05 21:33:27 +0000491 *up = lease->next; /* unlink */
Simon Kelley7cebd202006-05-06 14:13:33 +0100492
493 /* Put on old_leases list 'till we
494 can run the script */
495 lease->next = old_leases;
496 old_leases = lease;
497
Simon Kelley44a2a312004-03-10 20:04:35 +0000498 leases_left++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000499 }
500 else
501 up = &lease->next;
502 }
503}
504
505
Simon Kelleycdeda282006-03-16 20:16:06 +0000506struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,
Simon Kelley0a852542005-03-23 20:28:59 +0000507 unsigned char *clid, int clid_len)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000508{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000509 struct dhcp_lease *lease;
510
Simon Kelley0a852542005-03-23 20:28:59 +0000511 if (clid)
512 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000513 {
514#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000515 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000516 continue;
517#endif
518 if (lease->clid && clid_len == lease->clid_len &&
519 memcmp(clid, lease->clid, clid_len) == 0)
520 return lease;
521 }
Simon Kelley0a852542005-03-23 20:28:59 +0000522
523 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000524 {
525#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000526 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000527 continue;
528#endif
529 if ((!lease->clid || !clid) &&
530 hw_len != 0 &&
531 lease->hwaddr_len == hw_len &&
532 lease->hwaddr_type == hw_type &&
533 memcmp(hwaddr, lease->hwaddr, hw_len) == 0)
534 return lease;
535 }
536
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000537 return NULL;
538}
539
540struct dhcp_lease *lease_find_by_addr(struct in_addr addr)
541{
542 struct dhcp_lease *lease;
543
544 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000545 {
546#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000547 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000548 continue;
549#endif
550 if (lease->addr.s_addr == addr.s_addr)
551 return lease;
552 }
553
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000554 return NULL;
555}
556
Simon Kelley52b92f42012-01-22 16:05:15 +0000557#ifdef HAVE_DHCP6
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000558/* find address for {CLID, IAID, address} */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000559struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len,
560 int lease_type, int iaid, struct in6_addr *addr)
Simon Kelley52b92f42012-01-22 16:05:15 +0000561{
562 struct dhcp_lease *lease;
563
564 for (lease = leases; lease; lease = lease->next)
565 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000566 if (!(lease->flags & lease_type) || lease->hwaddr_type != iaid)
567 continue;
Simon Kelley52b92f42012-01-22 16:05:15 +0000568
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000569 if (memcmp(lease->hwaddr, addr, IN6ADDRSZ) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000570 continue;
571
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000572 if ((clid_len != lease->clid_len ||
Simon Kelley4cb1b322012-02-06 14:30:41 +0000573 memcmp(clid, lease->clid, clid_len) != 0))
574 continue;
575
Simon Kelley8b460612012-09-08 21:47:28 +0100576 return lease;
Simon Kelley52b92f42012-01-22 16:05:15 +0000577 }
578
579 return NULL;
580}
581
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000582/* reset "USED flags */
583void lease6_reset(void)
Simon Kelley8b460612012-09-08 21:47:28 +0100584{
585 struct dhcp_lease *lease;
586
587 for (lease = leases; lease; lease = lease->next)
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000588 lease->flags &= ~LEASE_USED;
589}
590
591/* enumerate all leases belonging to {CLID, IAID} */
592struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_type, unsigned char *clid, int clid_len, int iaid)
593{
594 struct dhcp_lease *lease;
595
596 if (!first)
597 first = leases;
598
599 for (lease = first; lease; lease = lease->next)
Simon Kelley8b460612012-09-08 21:47:28 +0100600 {
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000601 if (lease->flags & LEASE_USED)
602 continue;
603
Simon Kelley8b460612012-09-08 21:47:28 +0100604 if (!(lease->flags & lease_type) || lease->hwaddr_type != iaid)
605 continue;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000606
607 if ((clid_len != lease->clid_len ||
608 memcmp(clid, lease->clid, clid_len) != 0))
609 continue;
610
611 return lease;
Simon Kelley8b460612012-09-08 21:47:28 +0100612 }
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000613
614 return NULL;
Simon Kelley8b460612012-09-08 21:47:28 +0100615}
616
Simon Kelley52b92f42012-01-22 16:05:15 +0000617struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr)
618{
619 struct dhcp_lease *lease;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000620
Simon Kelley52b92f42012-01-22 16:05:15 +0000621 for (lease = leases; lease; lease = lease->next)
622 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000623 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelley52b92f42012-01-22 16:05:15 +0000624 continue;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000625
Simon Kelley52b92f42012-01-22 16:05:15 +0000626 if (is_same_net6((struct in6_addr *)lease->hwaddr, net, prefix) &&
627 (prefix == 128 || addr6part((struct in6_addr *)lease->hwaddr) == addr))
628 return lease;
629 }
630
631 return NULL;
Simon Kelley07933802012-02-14 20:55:25 +0000632}
633
634/* Find largest assigned address in context */
635u64 lease_find_max_addr6(struct dhcp_context *context)
636{
637 struct dhcp_lease *lease;
638 u64 addr = addr6part(&context->start6);
639
640 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
641 for (lease = leases; lease; lease = lease->next)
642 {
Simon Kelley07933802012-02-14 20:55:25 +0000643 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
644 continue;
Simon Kelley6caacac2012-02-15 21:58:33 +0000645
Simon Kelley07933802012-02-14 20:55:25 +0000646 if (is_same_net6((struct in6_addr *)lease->hwaddr, &context->start6, 64) &&
647 addr6part((struct in6_addr *)lease->hwaddr) > addr6part(&context->start6) &&
648 addr6part((struct in6_addr *)lease->hwaddr) <= addr6part(&context->end6) &&
649 addr6part((struct in6_addr *)lease->hwaddr) > addr)
650 addr = addr6part((struct in6_addr *)lease->hwaddr);
651 }
652
653 return addr;
654}
655
Simon Kelley52b92f42012-01-22 16:05:15 +0000656#endif
657
Simon Kelley7de060b2011-08-26 17:24:52 +0100658/* Find largest assigned address in context */
659struct in_addr lease_find_max_addr(struct dhcp_context *context)
660{
661 struct dhcp_lease *lease;
662 struct in_addr addr = context->start;
663
664 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
665 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000666 {
667#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000668 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000669 continue;
670#endif
671 if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) &&
672 ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) &&
673 ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr)))
674 addr = lease->addr;
675 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100676
677 return addr;
678}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000679
Simon Kelleyc72daea2012-01-05 21:33:27 +0000680static struct dhcp_lease *lease_allocate(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000681{
682 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100683 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000684 return NULL;
685
Simon Kelley7cebd202006-05-06 14:13:33 +0100686 memset(lease, 0, sizeof(struct dhcp_lease));
Simon Kelley4cb1b322012-02-06 14:30:41 +0000687 lease->flags = LEASE_NEW;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000688 lease->expires = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100689#ifdef HAVE_BROKEN_RTC
690 lease->length = 0xffffffff; /* illegal value */
691#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000692 lease->next = leases;
693 leases = lease;
694
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100695 file_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000696 leases_left--;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000697
698 return lease;
699}
700
Simon Kelley52b92f42012-01-22 16:05:15 +0000701struct dhcp_lease *lease4_allocate(struct in_addr addr)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000702{
703 struct dhcp_lease *lease = lease_allocate();
704 lease->addr = addr;
705 lease->hwaddr_len = 256; /* illegal value */
706
707 return lease;
708}
709
710#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000711struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000712{
713 struct dhcp_lease *lease = lease_allocate();
714 memcpy(lease->hwaddr, addrp, sizeof(*addrp)) ;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000715 lease->flags |= lease_type;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000716
717 return lease;
718}
719#endif
720
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100721void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000722{
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100723 time_t exp = now + (time_t)len;
724
725 if (len == 0xffffffff)
726 {
727 exp = 0;
728 len = 0;
729 }
730
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000731 if (exp != lease->expires)
Simon Kelley0a852542005-03-23 20:28:59 +0000732 {
Simon Kelley0a852542005-03-23 20:28:59 +0000733 dns_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100734 lease->expires = exp;
735#ifndef HAVE_BROKEN_RTC
Simon Kelley4cb1b322012-02-06 14:30:41 +0000736 lease->flags |= LEASE_AUX_CHANGED;
737 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100738#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000739 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100740
741#ifdef HAVE_BROKEN_RTC
742 if (len != lease->length)
743 {
744 lease->length = len;
Simon Kelleyb7f40202012-02-29 21:43:37 +0000745 lease->flags |= LEASE_AUX_CHANGED;
746 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100747 }
748#endif
749}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000750
Simon Kelley7cebd202006-05-06 14:13:33 +0100751void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
Simon Kelleya9ab7322012-04-28 11:29:37 +0100752 unsigned char *clid, int hw_len, int hw_type, int clid_len,
753 time_t now, int force)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000754{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000755#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100756 int change = force;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000757 lease->flags |= LEASE_HAVE_HWADDR;
758#endif
759
Simon Kelleya9ab7322012-04-28 11:29:37 +0100760 (void)force;
761
Simon Kelleycdeda282006-03-16 20:16:06 +0000762 if (hw_len != lease->hwaddr_len ||
763 hw_type != lease->hwaddr_type ||
Simon Kelley7cebd202006-05-06 14:13:33 +0100764 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000765 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000766 if (hw_len != 0)
767 memcpy(lease->hwaddr, hwaddr, hw_len);
Simon Kelleycdeda282006-03-16 20:16:06 +0000768 lease->hwaddr_len = hw_len;
769 lease->hwaddr_type = hw_type;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000770 lease->flags |= LEASE_CHANGED;
771 file_dirty = 1; /* run script on change */
Simon Kelley353ae4d2012-03-19 20:07:51 +0000772#ifdef HAVE_DHCP6
773 change = 1;
774#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000775 }
Simon Kelley0a852542005-03-23 20:28:59 +0000776
777 /* only update clid when one is available, stops packets
778 without a clid removing the record. Lease init uses
779 clid_len == 0 for no clid. */
780 if (clid_len != 0 && clid)
781 {
782 if (!lease->clid)
783 lease->clid_len = 0;
784
785 if (lease->clid_len != clid_len)
786 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000787 lease->flags |= LEASE_AUX_CHANGED;
788 file_dirty = 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100789 free(lease->clid);
790 if (!(lease->clid = whine_malloc(clid_len)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100791 return;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000792#ifdef HAVE_DHCP6
793 change = 1;
794#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000795 }
796 else if (memcmp(lease->clid, clid, clid_len) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000797 {
798 lease->flags |= LEASE_AUX_CHANGED;
799 file_dirty = 1;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000800#ifdef HAVE_DHCP6
801 change = 1;
802#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +0000803 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000804
Simon Kelley0a852542005-03-23 20:28:59 +0000805 lease->clid_len = clid_len;
806 memcpy(lease->clid, clid, clid_len);
807 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000808
809#ifdef HAVE_DHCP6
810 if (change)
Simon Kelleya9ab7322012-04-28 11:29:37 +0100811 slaac_add_addrs(lease, now, force);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000812#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000813}
814
Simon Kelley9009d742008-11-14 20:04:27 +0000815static void kill_name(struct dhcp_lease *lease)
816{
817 /* run script to say we lost our old name */
818
819 /* this shouldn't happen unless updates are very quick and the
820 script very slow, we just avoid a memory leak if it does. */
821 free(lease->old_hostname);
822
823 /* If we know the fqdn, pass that. The helper will derive the
Simon Kelley4cb1b322012-02-06 14:30:41 +0000824 unqualified name from it, free the unqualified name here. */
Simon Kelley9009d742008-11-14 20:04:27 +0000825
826 if (lease->fqdn)
827 {
828 lease->old_hostname = lease->fqdn;
829 free(lease->hostname);
830 }
831 else
832 lease->old_hostname = lease->hostname;
833
834 lease->hostname = lease->fqdn = NULL;
835}
836
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000837void lease_set_hostname(struct dhcp_lease *lease, char *name, int auth, char *domain, char *config_domain)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000838{
839 struct dhcp_lease *lease_tmp;
840 char *new_name = NULL, *new_fqdn = NULL;
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000841
842 if (config_domain && (!domain || !hostname_isequal(domain, config_domain)))
843 my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, name);
Simon Kelley9009d742008-11-14 20:04:27 +0000844
Simon Kelleya2226412004-05-13 20:27:08 +0100845 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000846 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000847 if (auth)
848 lease->flags |= LEASE_AUTH_NAME;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000849 return;
850 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100851
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000852 if (!name && !lease->hostname)
853 return;
854
855 /* If a machine turns up on a new net without dropping the old lease,
856 or two machines claim the same name, then we end up with two interfaces with
Simon Kelleyb8187c82005-11-26 21:46:27 +0000857 the same name. Check for that here and remove the name from the old lease.
Simon Kelley4cb1b322012-02-06 14:30:41 +0000858 Note that IPv6 leases are different. All the leases to the same DUID are
859 allowed the same name.
860
Simon Kelleyb8187c82005-11-26 21:46:27 +0000861 Don't allow a name from the client to override a name from dnsmasq config. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000862
863 if (name)
864 {
Simon Kelley9009d742008-11-14 20:04:27 +0000865 if ((new_name = whine_malloc(strlen(name) + 1)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000866 {
Simon Kelley9009d742008-11-14 20:04:27 +0000867 strcpy(new_name, name);
Simon Kelley4cb1b322012-02-06 14:30:41 +0000868 if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2)))
Simon Kelley9009d742008-11-14 20:04:27 +0000869 {
870 strcpy(new_fqdn, name);
871 strcat(new_fqdn, ".");
Simon Kelley4cb1b322012-02-06 14:30:41 +0000872 strcat(new_fqdn, domain);
Simon Kelley9009d742008-11-14 20:04:27 +0000873 }
874 }
875
876 /* Depending on mode, we check either unqualified name or FQDN. */
877 for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next)
878 {
Simon Kelley28866e92011-02-14 20:19:14 +0000879 if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +0000880 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000881 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn))
Simon Kelley9009d742008-11-14 20:04:27 +0000882 continue;
883 }
884 else
885 {
886 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
887 continue;
888 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000889
890 if (lease->flags & (LEASE_TA | LEASE_NA))
891 {
892 if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA)))
893 continue;
894
Simon Kelleyceae00d2012-02-09 21:28:14 +0000895 /* another lease for the same DUID is OK for IPv6 */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000896 if (lease->clid_len == lease_tmp->clid_len &&
897 lease->clid && lease_tmp->clid &&
898 memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0)
899 continue;
900 }
901 else if (lease_tmp->flags & (LEASE_TA | LEASE_NA))
902 continue;
903
904 if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth)
Simon Kelley9009d742008-11-14 20:04:27 +0000905 {
906 free(new_name);
907 free(new_fqdn);
908 return;
909 }
910
911 kill_name(lease_tmp);
912 break;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000913 }
914 }
915
916 if (lease->hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000917 kill_name(lease);
Simon Kelley16972692006-10-16 20:04:18 +0100918
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000919 lease->hostname = new_name;
920 lease->fqdn = new_fqdn;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000921
922 if (auth)
923 lease->flags |= LEASE_AUTH_NAME;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000924
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100925 file_dirty = 1;
Simon Kelley7cebd202006-05-06 14:13:33 +0100926 dns_dirty = 1;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000927 lease->flags |= LEASE_CHANGED; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000928}
929
Simon Kelley353ae4d2012-03-19 20:07:51 +0000930void lease_set_interface(struct dhcp_lease *lease, int interface, time_t now)
Simon Kelley824af852008-02-12 20:43:05 +0000931{
932 if (lease->last_interface == interface)
933 return;
934
935 lease->last_interface = interface;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000936 lease->flags |= LEASE_CHANGED;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000937
938#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100939 slaac_add_addrs(lease, now, 0);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000940#endif
Simon Kelley824af852008-02-12 20:43:05 +0000941}
942
Simon Kelley5aabfc72007-08-29 11:24:47 +0100943void rerun_scripts(void)
944{
945 struct dhcp_lease *lease;
946
947 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000948 lease->flags |= LEASE_CHANGED;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100949}
950
Simon Kelley7cebd202006-05-06 14:13:33 +0100951/* deleted leases get transferred to the old_leases list.
952 remove them here, after calling the lease change
Simon Kelley16972692006-10-16 20:04:18 +0100953 script. Also run the lease change script on new/modified leases.
954
955 Return zero if nothing to do. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100956int do_script_run(time_t now)
Simon Kelley7cebd202006-05-06 14:13:33 +0100957{
958 struct dhcp_lease *lease;
959
Simon Kelley9009d742008-11-14 20:04:27 +0000960#ifdef HAVE_DBUS
961 /* If we're going to be sending DBus signals, but the connection is not yet up,
962 delay everything until it is. */
Simon Kelley28866e92011-02-14 20:19:14 +0000963 if (option_bool(OPT_DBUS) && !daemon->dbus)
Simon Kelley9009d742008-11-14 20:04:27 +0000964 return 0;
965#endif
966
Simon Kelley16972692006-10-16 20:04:18 +0100967 if (old_leases)
Simon Kelley7cebd202006-05-06 14:13:33 +0100968 {
Simon Kelley7cebd202006-05-06 14:13:33 +0100969 lease = old_leases;
Simon Kelley16972692006-10-16 20:04:18 +0100970
971 /* If the lease still has an old_hostname, do the "old" action on that first */
972 if (lease->old_hostname)
973 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100974#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +0100975 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
976#endif
Simon Kelley16972692006-10-16 20:04:18 +0100977 free(lease->old_hostname);
978 lease->old_hostname = NULL;
979 return 1;
980 }
981 else
982 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000983#ifdef HAVE_DHCP6
984 struct slaac_address *slaac, *tmp;
985 for (slaac = lease->slaac_address; slaac; slaac = tmp)
986 {
987 tmp = slaac->next;
988 free(slaac);
989 }
990#endif
Simon Kelley9009d742008-11-14 20:04:27 +0000991 kill_name(lease);
Simon Kelley1f15b812009-10-13 17:49:32 +0100992#ifdef HAVE_SCRIPT
Simon Kelley9009d742008-11-14 20:04:27 +0000993 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100994#endif
Simon Kelley1f15b812009-10-13 17:49:32 +0100995#ifdef HAVE_DBUS
996 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
997#endif
Simon Kelley16972692006-10-16 20:04:18 +0100998 old_leases = lease->next;
999
Simon Kelley9009d742008-11-14 20:04:27 +00001000 free(lease->old_hostname);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001001 free(lease->clid);
Simon Kelley316e2732010-01-22 20:16:09 +00001002 free(lease->extradata);
Simon Kelley16972692006-10-16 20:04:18 +01001003 free(lease);
1004
1005 return 1;
1006 }
Simon Kelley7cebd202006-05-06 14:13:33 +01001007 }
Simon Kelley16972692006-10-16 20:04:18 +01001008
1009 /* make sure we announce the loss of a hostname before its new location. */
1010 for (lease = leases; lease; lease = lease->next)
1011 if (lease->old_hostname)
1012 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001013#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001014 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1015#endif
Simon Kelley16972692006-10-16 20:04:18 +01001016 free(lease->old_hostname);
1017 lease->old_hostname = NULL;
1018 return 1;
1019 }
1020
Simon Kelley7cebd202006-05-06 14:13:33 +01001021 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001022 if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
1023 ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)))
Simon Kelley7cebd202006-05-06 14:13:33 +01001024 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001025#ifdef HAVE_SCRIPT
Simon Kelley4cb1b322012-02-06 14:30:41 +00001026 queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley9009d742008-11-14 20:04:27 +00001027 lease->fqdn ? lease->fqdn : lease->hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001028#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001029#ifdef HAVE_DBUS
Simon Kelley4cb1b322012-02-06 14:30:41 +00001030 emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley1f15b812009-10-13 17:49:32 +01001031 lease->fqdn ? lease->fqdn : lease->hostname);
1032#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +00001033 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED);
Simon Kelley16972692006-10-16 20:04:18 +01001034
Simon Kelley316e2732010-01-22 20:16:09 +00001035 /* this is used for the "add" call, then junked, since they're not in the database */
1036 free(lease->extradata);
1037 lease->extradata = NULL;
Simon Kelley16972692006-10-16 20:04:18 +01001038
1039 return 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001040 }
Simon Kelley16972692006-10-16 20:04:18 +01001041
1042 return 0; /* nothing to do */
Simon Kelley7cebd202006-05-06 14:13:33 +01001043}
Simon Kelley7622fc02009-06-04 20:32:05 +01001044
Simon Kelleyceae00d2012-02-09 21:28:14 +00001045#ifdef HAVE_SCRIPT
1046void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim)
1047{
1048 unsigned int i;
1049
1050 /* check for embeded NULLs */
1051 for (i = 0; i < len; i++)
1052 if (data[i] == 0)
1053 {
1054 len = i;
1055 break;
1056 }
1057
1058 if ((lease->extradata_size - lease->extradata_len) < (len + 1))
1059 {
1060 size_t newsz = lease->extradata_len + len + 100;
1061 unsigned char *new = whine_malloc(newsz);
1062
1063 if (!new)
1064 return;
1065
1066 if (lease->extradata)
1067 {
1068 memcpy(new, lease->extradata, lease->extradata_len);
1069 free(lease->extradata);
1070 }
1071
1072 lease->extradata = new;
1073 lease->extradata_size = newsz;
1074 }
1075
1076 if (len != 0)
1077 memcpy(lease->extradata + lease->extradata_len, data, len);
1078 lease->extradata[lease->extradata_len + len] = delim;
1079 lease->extradata_len += len + 1;
1080}
1081#endif
1082
Simon Kelley7622fc02009-06-04 20:32:05 +01001083#endif
Simon Kelley7cebd202006-05-06 14:13:33 +01001084
1085
1086
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001087