blob: 3856649b2e1ec32a577cd8b49bf2260102a3220a [file] [log] [blame]
Simon Kelley59546082012-01-06 20:02:04 +00001/* dnsmasq is Copyright (c) 2000-2012 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 Kelley4cb1b322012-02-06 14:30:41 +0000558/* addr or clid may be NULL for "don't care, both NULL resets "USED" flags both
559 set activates USED check */
560struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len,
561 int lease_type, int iaid, struct in6_addr *addr)
Simon Kelley52b92f42012-01-22 16:05:15 +0000562{
563 struct dhcp_lease *lease;
564
565 for (lease = leases; lease; lease = lease->next)
566 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000567 if (!(lease->flags & lease_type) || lease->hwaddr_type != iaid)
568 continue;
Simon Kelley52b92f42012-01-22 16:05:15 +0000569
Simon Kelley4cb1b322012-02-06 14:30:41 +0000570 if (clid && addr && (lease->flags & LEASE_USED))
571 continue;
572
573 if (addr && memcmp(lease->hwaddr, addr, IN6ADDRSZ) != 0)
574 continue;
575
576 if (clid &&
577 (clid_len != lease->clid_len ||
578 memcmp(clid, lease->clid, clid_len) != 0))
579 continue;
580
Simon Kelley8b460612012-09-08 21:47:28 +0100581 lease->flags |= LEASE_USED;
582 return lease;
Simon Kelley52b92f42012-01-22 16:05:15 +0000583 }
584
585 return NULL;
586}
587
Simon Kelley8b460612012-09-08 21:47:28 +0100588void lease6_filter(int lease_type, int iaid, struct dhcp_context *context)
589{
590 struct dhcp_lease *lease;
591
592 for (lease = leases; lease; lease = lease->next)
593 {
594 /* reset "USED flag */
595 lease->flags &= ~LEASE_USED;
596
597 if (!(lease->flags & lease_type) || lease->hwaddr_type != iaid)
598 continue;
599
600 /* leases on the wrong interface get filtered out here */
Simon Kelleyb2692212012-09-16 22:15:56 +0100601 if (!is_addr_in_context6(context, (struct in6_addr *)&lease->hwaddr))
Simon Kelley8b460612012-09-08 21:47:28 +0100602 lease->flags |= LEASE_USED;
603 }
604}
605
Simon Kelley52b92f42012-01-22 16:05:15 +0000606struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr)
607{
608 struct dhcp_lease *lease;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000609
Simon Kelley52b92f42012-01-22 16:05:15 +0000610 for (lease = leases; lease; lease = lease->next)
611 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000612 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelley52b92f42012-01-22 16:05:15 +0000613 continue;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000614
Simon Kelley52b92f42012-01-22 16:05:15 +0000615 if (is_same_net6((struct in6_addr *)lease->hwaddr, net, prefix) &&
616 (prefix == 128 || addr6part((struct in6_addr *)lease->hwaddr) == addr))
617 return lease;
618 }
619
620 return NULL;
Simon Kelley07933802012-02-14 20:55:25 +0000621}
622
623/* Find largest assigned address in context */
624u64 lease_find_max_addr6(struct dhcp_context *context)
625{
626 struct dhcp_lease *lease;
627 u64 addr = addr6part(&context->start6);
628
629 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
630 for (lease = leases; lease; lease = lease->next)
631 {
Simon Kelley07933802012-02-14 20:55:25 +0000632 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
633 continue;
Simon Kelley6caacac2012-02-15 21:58:33 +0000634
Simon Kelley07933802012-02-14 20:55:25 +0000635 if (is_same_net6((struct in6_addr *)lease->hwaddr, &context->start6, 64) &&
636 addr6part((struct in6_addr *)lease->hwaddr) > addr6part(&context->start6) &&
637 addr6part((struct in6_addr *)lease->hwaddr) <= addr6part(&context->end6) &&
638 addr6part((struct in6_addr *)lease->hwaddr) > addr)
639 addr = addr6part((struct in6_addr *)lease->hwaddr);
640 }
641
642 return addr;
643}
644
Simon Kelley52b92f42012-01-22 16:05:15 +0000645#endif
646
Simon Kelley7de060b2011-08-26 17:24:52 +0100647/* Find largest assigned address in context */
648struct in_addr lease_find_max_addr(struct dhcp_context *context)
649{
650 struct dhcp_lease *lease;
651 struct in_addr addr = context->start;
652
653 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
654 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000655 {
656#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000657 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000658 continue;
659#endif
660 if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) &&
661 ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) &&
662 ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr)))
663 addr = lease->addr;
664 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100665
666 return addr;
667}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000668
Simon Kelleyc72daea2012-01-05 21:33:27 +0000669static struct dhcp_lease *lease_allocate(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000670{
671 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100672 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000673 return NULL;
674
Simon Kelley7cebd202006-05-06 14:13:33 +0100675 memset(lease, 0, sizeof(struct dhcp_lease));
Simon Kelley4cb1b322012-02-06 14:30:41 +0000676 lease->flags = LEASE_NEW;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000677 lease->expires = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100678#ifdef HAVE_BROKEN_RTC
679 lease->length = 0xffffffff; /* illegal value */
680#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000681 lease->next = leases;
682 leases = lease;
683
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100684 file_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000685 leases_left--;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000686
687 return lease;
688}
689
Simon Kelley52b92f42012-01-22 16:05:15 +0000690struct dhcp_lease *lease4_allocate(struct in_addr addr)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000691{
692 struct dhcp_lease *lease = lease_allocate();
693 lease->addr = addr;
694 lease->hwaddr_len = 256; /* illegal value */
695
696 return lease;
697}
698
699#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000700struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000701{
702 struct dhcp_lease *lease = lease_allocate();
703 memcpy(lease->hwaddr, addrp, sizeof(*addrp)) ;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000704 lease->flags |= lease_type;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000705
706 return lease;
707}
708#endif
709
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100710void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000711{
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100712 time_t exp = now + (time_t)len;
713
714 if (len == 0xffffffff)
715 {
716 exp = 0;
717 len = 0;
718 }
719
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000720 if (exp != lease->expires)
Simon Kelley0a852542005-03-23 20:28:59 +0000721 {
Simon Kelley0a852542005-03-23 20:28:59 +0000722 dns_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100723 lease->expires = exp;
724#ifndef HAVE_BROKEN_RTC
Simon Kelley4cb1b322012-02-06 14:30:41 +0000725 lease->flags |= LEASE_AUX_CHANGED;
726 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100727#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000728 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100729
730#ifdef HAVE_BROKEN_RTC
731 if (len != lease->length)
732 {
733 lease->length = len;
Simon Kelleyb7f40202012-02-29 21:43:37 +0000734 lease->flags |= LEASE_AUX_CHANGED;
735 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100736 }
737#endif
738}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000739
Simon Kelley7cebd202006-05-06 14:13:33 +0100740void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
Simon Kelleya9ab7322012-04-28 11:29:37 +0100741 unsigned char *clid, int hw_len, int hw_type, int clid_len,
742 time_t now, int force)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000743{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000744#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100745 int change = force;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000746 lease->flags |= LEASE_HAVE_HWADDR;
747#endif
748
Simon Kelleya9ab7322012-04-28 11:29:37 +0100749 (void)force;
750
Simon Kelleycdeda282006-03-16 20:16:06 +0000751 if (hw_len != lease->hwaddr_len ||
752 hw_type != lease->hwaddr_type ||
Simon Kelley7cebd202006-05-06 14:13:33 +0100753 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000754 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000755 if (hw_len != 0)
756 memcpy(lease->hwaddr, hwaddr, hw_len);
Simon Kelleycdeda282006-03-16 20:16:06 +0000757 lease->hwaddr_len = hw_len;
758 lease->hwaddr_type = hw_type;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000759 lease->flags |= LEASE_CHANGED;
760 file_dirty = 1; /* run script on change */
Simon Kelley353ae4d2012-03-19 20:07:51 +0000761#ifdef HAVE_DHCP6
762 change = 1;
763#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000764 }
Simon Kelley0a852542005-03-23 20:28:59 +0000765
766 /* only update clid when one is available, stops packets
767 without a clid removing the record. Lease init uses
768 clid_len == 0 for no clid. */
769 if (clid_len != 0 && clid)
770 {
771 if (!lease->clid)
772 lease->clid_len = 0;
773
774 if (lease->clid_len != clid_len)
775 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000776 lease->flags |= LEASE_AUX_CHANGED;
777 file_dirty = 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100778 free(lease->clid);
779 if (!(lease->clid = whine_malloc(clid_len)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100780 return;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000781#ifdef HAVE_DHCP6
782 change = 1;
783#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000784 }
785 else if (memcmp(lease->clid, clid, clid_len) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000786 {
787 lease->flags |= LEASE_AUX_CHANGED;
788 file_dirty = 1;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000789#ifdef HAVE_DHCP6
790 change = 1;
791#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +0000792 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000793
Simon Kelley0a852542005-03-23 20:28:59 +0000794 lease->clid_len = clid_len;
795 memcpy(lease->clid, clid, clid_len);
796 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000797
798#ifdef HAVE_DHCP6
799 if (change)
Simon Kelleya9ab7322012-04-28 11:29:37 +0100800 slaac_add_addrs(lease, now, force);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000801#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000802}
803
Simon Kelley9009d742008-11-14 20:04:27 +0000804static void kill_name(struct dhcp_lease *lease)
805{
806 /* run script to say we lost our old name */
807
808 /* this shouldn't happen unless updates are very quick and the
809 script very slow, we just avoid a memory leak if it does. */
810 free(lease->old_hostname);
811
812 /* If we know the fqdn, pass that. The helper will derive the
Simon Kelley4cb1b322012-02-06 14:30:41 +0000813 unqualified name from it, free the unqualified name here. */
Simon Kelley9009d742008-11-14 20:04:27 +0000814
815 if (lease->fqdn)
816 {
817 lease->old_hostname = lease->fqdn;
818 free(lease->hostname);
819 }
820 else
821 lease->old_hostname = lease->hostname;
822
823 lease->hostname = lease->fqdn = NULL;
824}
825
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000826void lease_set_hostname(struct dhcp_lease *lease, char *name, int auth, char *domain, char *config_domain)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000827{
828 struct dhcp_lease *lease_tmp;
829 char *new_name = NULL, *new_fqdn = NULL;
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000830
831 if (config_domain && (!domain || !hostname_isequal(domain, config_domain)))
832 my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, name);
Simon Kelley9009d742008-11-14 20:04:27 +0000833
Simon Kelleya2226412004-05-13 20:27:08 +0100834 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000835 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000836 if (auth)
837 lease->flags |= LEASE_AUTH_NAME;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000838 return;
839 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100840
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000841 if (!name && !lease->hostname)
842 return;
843
844 /* If a machine turns up on a new net without dropping the old lease,
845 or two machines claim the same name, then we end up with two interfaces with
Simon Kelleyb8187c82005-11-26 21:46:27 +0000846 the same name. Check for that here and remove the name from the old lease.
Simon Kelley4cb1b322012-02-06 14:30:41 +0000847 Note that IPv6 leases are different. All the leases to the same DUID are
848 allowed the same name.
849
Simon Kelleyb8187c82005-11-26 21:46:27 +0000850 Don't allow a name from the client to override a name from dnsmasq config. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000851
852 if (name)
853 {
Simon Kelley9009d742008-11-14 20:04:27 +0000854 if ((new_name = whine_malloc(strlen(name) + 1)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000855 {
Simon Kelley9009d742008-11-14 20:04:27 +0000856 strcpy(new_name, name);
Simon Kelley4cb1b322012-02-06 14:30:41 +0000857 if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2)))
Simon Kelley9009d742008-11-14 20:04:27 +0000858 {
859 strcpy(new_fqdn, name);
860 strcat(new_fqdn, ".");
Simon Kelley4cb1b322012-02-06 14:30:41 +0000861 strcat(new_fqdn, domain);
Simon Kelley9009d742008-11-14 20:04:27 +0000862 }
863 }
864
865 /* Depending on mode, we check either unqualified name or FQDN. */
866 for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next)
867 {
Simon Kelley28866e92011-02-14 20:19:14 +0000868 if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +0000869 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000870 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn))
Simon Kelley9009d742008-11-14 20:04:27 +0000871 continue;
872 }
873 else
874 {
875 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
876 continue;
877 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000878
879 if (lease->flags & (LEASE_TA | LEASE_NA))
880 {
881 if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA)))
882 continue;
883
Simon Kelleyceae00d2012-02-09 21:28:14 +0000884 /* another lease for the same DUID is OK for IPv6 */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000885 if (lease->clid_len == lease_tmp->clid_len &&
886 lease->clid && lease_tmp->clid &&
887 memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0)
888 continue;
889 }
890 else if (lease_tmp->flags & (LEASE_TA | LEASE_NA))
891 continue;
892
893 if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth)
Simon Kelley9009d742008-11-14 20:04:27 +0000894 {
895 free(new_name);
896 free(new_fqdn);
897 return;
898 }
899
900 kill_name(lease_tmp);
901 break;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000902 }
903 }
904
905 if (lease->hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000906 kill_name(lease);
Simon Kelley16972692006-10-16 20:04:18 +0100907
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000908 lease->hostname = new_name;
909 lease->fqdn = new_fqdn;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000910
911 if (auth)
912 lease->flags |= LEASE_AUTH_NAME;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000913
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100914 file_dirty = 1;
Simon Kelley7cebd202006-05-06 14:13:33 +0100915 dns_dirty = 1;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000916 lease->flags |= LEASE_CHANGED; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000917}
918
Simon Kelley353ae4d2012-03-19 20:07:51 +0000919void lease_set_interface(struct dhcp_lease *lease, int interface, time_t now)
Simon Kelley824af852008-02-12 20:43:05 +0000920{
921 if (lease->last_interface == interface)
922 return;
923
924 lease->last_interface = interface;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000925 lease->flags |= LEASE_CHANGED;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000926
927#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100928 slaac_add_addrs(lease, now, 0);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000929#endif
Simon Kelley824af852008-02-12 20:43:05 +0000930}
931
Simon Kelley5aabfc72007-08-29 11:24:47 +0100932void rerun_scripts(void)
933{
934 struct dhcp_lease *lease;
935
936 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000937 lease->flags |= LEASE_CHANGED;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100938}
939
Simon Kelley7cebd202006-05-06 14:13:33 +0100940/* deleted leases get transferred to the old_leases list.
941 remove them here, after calling the lease change
Simon Kelley16972692006-10-16 20:04:18 +0100942 script. Also run the lease change script on new/modified leases.
943
944 Return zero if nothing to do. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100945int do_script_run(time_t now)
Simon Kelley7cebd202006-05-06 14:13:33 +0100946{
947 struct dhcp_lease *lease;
948
Simon Kelley9009d742008-11-14 20:04:27 +0000949#ifdef HAVE_DBUS
950 /* If we're going to be sending DBus signals, but the connection is not yet up,
951 delay everything until it is. */
Simon Kelley28866e92011-02-14 20:19:14 +0000952 if (option_bool(OPT_DBUS) && !daemon->dbus)
Simon Kelley9009d742008-11-14 20:04:27 +0000953 return 0;
954#endif
955
Simon Kelley16972692006-10-16 20:04:18 +0100956 if (old_leases)
Simon Kelley7cebd202006-05-06 14:13:33 +0100957 {
Simon Kelley7cebd202006-05-06 14:13:33 +0100958 lease = old_leases;
Simon Kelley16972692006-10-16 20:04:18 +0100959
960 /* If the lease still has an old_hostname, do the "old" action on that first */
961 if (lease->old_hostname)
962 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100963#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +0100964 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
965#endif
Simon Kelley16972692006-10-16 20:04:18 +0100966 free(lease->old_hostname);
967 lease->old_hostname = NULL;
968 return 1;
969 }
970 else
971 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000972#ifdef HAVE_DHCP6
973 struct slaac_address *slaac, *tmp;
974 for (slaac = lease->slaac_address; slaac; slaac = tmp)
975 {
976 tmp = slaac->next;
977 free(slaac);
978 }
979#endif
Simon Kelley9009d742008-11-14 20:04:27 +0000980 kill_name(lease);
Simon Kelley1f15b812009-10-13 17:49:32 +0100981#ifdef HAVE_SCRIPT
Simon Kelley9009d742008-11-14 20:04:27 +0000982 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100983#endif
Simon Kelley1f15b812009-10-13 17:49:32 +0100984#ifdef HAVE_DBUS
985 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
986#endif
Simon Kelley16972692006-10-16 20:04:18 +0100987 old_leases = lease->next;
988
Simon Kelley9009d742008-11-14 20:04:27 +0000989 free(lease->old_hostname);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100990 free(lease->clid);
Simon Kelley316e2732010-01-22 20:16:09 +0000991 free(lease->extradata);
Simon Kelley16972692006-10-16 20:04:18 +0100992 free(lease);
993
994 return 1;
995 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100996 }
Simon Kelley16972692006-10-16 20:04:18 +0100997
998 /* make sure we announce the loss of a hostname before its new location. */
999 for (lease = leases; lease; lease = lease->next)
1000 if (lease->old_hostname)
1001 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001002#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001003 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1004#endif
Simon Kelley16972692006-10-16 20:04:18 +01001005 free(lease->old_hostname);
1006 lease->old_hostname = NULL;
1007 return 1;
1008 }
1009
Simon Kelley7cebd202006-05-06 14:13:33 +01001010 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001011 if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
1012 ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)))
Simon Kelley7cebd202006-05-06 14:13:33 +01001013 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001014#ifdef HAVE_SCRIPT
Simon Kelley4cb1b322012-02-06 14:30:41 +00001015 queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley9009d742008-11-14 20:04:27 +00001016 lease->fqdn ? lease->fqdn : lease->hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001017#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001018#ifdef HAVE_DBUS
Simon Kelley4cb1b322012-02-06 14:30:41 +00001019 emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley1f15b812009-10-13 17:49:32 +01001020 lease->fqdn ? lease->fqdn : lease->hostname);
1021#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +00001022 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED);
Simon Kelley16972692006-10-16 20:04:18 +01001023
Simon Kelley316e2732010-01-22 20:16:09 +00001024 /* this is used for the "add" call, then junked, since they're not in the database */
1025 free(lease->extradata);
1026 lease->extradata = NULL;
Simon Kelley16972692006-10-16 20:04:18 +01001027
1028 return 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001029 }
Simon Kelley16972692006-10-16 20:04:18 +01001030
1031 return 0; /* nothing to do */
Simon Kelley7cebd202006-05-06 14:13:33 +01001032}
Simon Kelley7622fc02009-06-04 20:32:05 +01001033
Simon Kelleyceae00d2012-02-09 21:28:14 +00001034#ifdef HAVE_SCRIPT
1035void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim)
1036{
1037 unsigned int i;
1038
1039 /* check for embeded NULLs */
1040 for (i = 0; i < len; i++)
1041 if (data[i] == 0)
1042 {
1043 len = i;
1044 break;
1045 }
1046
1047 if ((lease->extradata_size - lease->extradata_len) < (len + 1))
1048 {
1049 size_t newsz = lease->extradata_len + len + 100;
1050 unsigned char *new = whine_malloc(newsz);
1051
1052 if (!new)
1053 return;
1054
1055 if (lease->extradata)
1056 {
1057 memcpy(new, lease->extradata, lease->extradata_len);
1058 free(lease->extradata);
1059 }
1060
1061 lease->extradata = new;
1062 lease->extradata_size = newsz;
1063 }
1064
1065 if (len != 0)
1066 memcpy(lease->extradata + lease->extradata_len, data, len);
1067 lease->extradata[lease->extradata_len + len] = delim;
1068 lease->extradata_len += len + 1;
1069}
1070#endif
1071
Simon Kelley7622fc02009-06-04 20:32:05 +01001072#endif
Simon Kelley7cebd202006-05-06 14:13:33 +01001073
1074
1075
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001076