blob: b85cf574a72ddef47d889303e41dcbd2cad661e4 [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
Simon Kelley9de1aa92013-04-10 11:15:58 +0100118 hw_type = strtoul(s, NULL, 10);
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
Simon Kelley3f2873d2013-05-14 11:28:47 +0100348static int find_interface_v4(struct in_addr local, int if_index, char *label,
Simon Kelley801ca9a2012-03-06 19:30:17 +0000349 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 Kelley3f2873d2013-05-14 11:28:47 +0100353 (void) label;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000354 (void) broadcast;
355 (void) vparam;
356
357 for (lease = leases; lease; lease = lease->next)
358 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
359 if (is_same_net(local, lease->addr, netmask))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000360 lease_set_interface(lease, if_index, *((time_t *)vparam));
Simon Kelley801ca9a2012-03-06 19:30:17 +0000361
362 return 1;
363}
364
365#ifdef HAVE_DHCP6
366static int find_interface_v6(struct in6_addr *local, int prefix,
Simon Kelleybad7b872012-12-20 22:00:39 +0000367 int scope, int if_index, int flags,
Simon Kelley1f776932012-12-16 19:46:08 +0000368 int preferred, int valid, void *vparam)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000369{
370 struct dhcp_lease *lease;
371
Simon Kelley353ae4d2012-03-19 20:07:51 +0000372 (void)scope;
Simon Kelleybad7b872012-12-20 22:00:39 +0000373 (void)flags;
Simon Kelley1f776932012-12-16 19:46:08 +0000374 (void)preferred;
375 (void)valid;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000376
377 for (lease = leases; lease; lease = lease->next)
378 if ((lease->flags & (LEASE_TA | LEASE_NA)))
379 if (is_same_net6(local, (struct in6_addr *)&lease->hwaddr, prefix))
Simon Kelley353ae4d2012-03-19 20:07:51 +0000380 lease_set_interface(lease, if_index, *((time_t *)vparam));
Simon Kelley801ca9a2012-03-06 19:30:17 +0000381
382 return 1;
383}
Simon Kelley353ae4d2012-03-19 20:07:51 +0000384
385void lease_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface)
386{
Simon Kelley5ef33272012-03-30 15:10:28 +0100387 /* We may be doing RA but not DHCPv4, in which case the lease
388 database may not exist and we have nothing to do anyway */
389 if (daemon->dhcp)
390 slaac_ping_reply(sender, packet, interface, leases);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000391}
392
Simon Kelley0c050242012-12-22 22:13:19 +0000393void lease_update_slaac(time_t now)
394{
395 /* Called when we contruct a new RA-names context, to add putative
396 new SLAAC addresses to existing leases. */
397
398 struct dhcp_lease *lease;
399
400 if (daemon->dhcp)
401 for (lease = leases; lease; lease = lease->next)
402 slaac_add_addrs(lease, now, 0);
403}
404
Simon Kelley801ca9a2012-03-06 19:30:17 +0000405#endif
406
407
408/* Find interfaces associated with leases at start-up. This gets updated as
409 we do DHCP transactions, but information about directly-connected subnets
410 is useful from scrips and necessary for determining SLAAC addresses from
411 start-time. */
Simon Kelley8b372702012-03-09 17:45:10 +0000412void lease_find_interfaces(time_t now)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000413{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000414 iface_enumerate(AF_INET, &now, find_interface_v4);
415#ifdef HAVE_DHCP6
416 iface_enumerate(AF_INET6, &now, find_interface_v6);
Simon Kelley8b372702012-03-09 17:45:10 +0000417
418 /* If we're not doing DHCPv6, and there are not v6 leases, don't add the DUID to the database */
419 if (!daemon->duid && daemon->dhcp6)
420 {
421 file_dirty = 1;
422 make_duid(now);
423 }
Simon Kelley801ca9a2012-03-06 19:30:17 +0000424#endif
425}
426
427
428
Simon Kelley353ae4d2012-03-19 20:07:51 +0000429void lease_update_dns(int force)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000430{
431 struct dhcp_lease *lease;
432
Simon Kelley353ae4d2012-03-19 20:07:51 +0000433 if (daemon->port != 0 && (dns_dirty || force))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000434 {
Simon Kelley8ff55672012-12-09 21:09:01 +0000435#ifndef HAVE_BROKEN_RTC
Simon Kelleye1ff4192012-12-09 17:08:47 +0000436 /* force transfer to authoritative secondaries */
437 daemon->soa_sn++;
Simon Kelley8ff55672012-12-09 21:09:01 +0000438#endif
Simon Kelleye1ff4192012-12-09 17:08:47 +0000439
Simon Kelley801ca9a2012-03-06 19:30:17 +0000440 cache_unhash_dhcp();
441
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000442 for (lease = leases; lease; lease = lease->next)
443 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000444 int prot = AF_INET;
Simon Kelley801ca9a2012-03-06 19:30:17 +0000445
Simon Kelley4cb1b322012-02-06 14:30:41 +0000446#ifdef HAVE_DHCP6
447 if (lease->flags & (LEASE_TA | LEASE_NA))
448 prot = AF_INET6;
Simon Kelleyf444cdd2012-03-07 10:15:57 +0000449 else if (lease->hostname || lease->fqdn)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000450 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000451 struct slaac_address *slaac;
452
453 for (slaac = lease->slaac_address; slaac; slaac = slaac->next)
454 if (slaac->backoff == 0)
Simon Kelley801ca9a2012-03-06 19:30:17 +0000455 {
Simon Kelley801ca9a2012-03-06 19:30:17 +0000456 if (lease->fqdn)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000457 cache_add_dhcp_entry(lease->fqdn, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000458 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley353ae4d2012-03-19 20:07:51 +0000459 cache_add_dhcp_entry(lease->hostname, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires);
Simon Kelley801ca9a2012-03-06 19:30:17 +0000460 }
461 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000462#endif
463
Simon Kelley9009d742008-11-14 20:04:27 +0000464 if (lease->fqdn)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000465 cache_add_dhcp_entry(lease->fqdn, prot,
466 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr,
467 lease->expires);
Simon Kelley9009d742008-11-14 20:04:27 +0000468
Simon Kelley28866e92011-02-14 20:19:14 +0000469 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000470 cache_add_dhcp_entry(lease->hostname, prot,
471 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr,
472 lease->expires);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000473 }
474
475 dns_dirty = 0;
476 }
477}
478
479void lease_prune(struct dhcp_lease *target, time_t now)
480{
481 struct dhcp_lease *lease, *tmp, **up;
482
483 for (lease = leases, up = &leases; lease; lease = tmp)
484 {
485 tmp = lease->next;
486 if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target)
487 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100488 file_dirty = 1;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000489 if (lease->hostname)
Simon Kelley7cebd202006-05-06 14:13:33 +0100490 dns_dirty = 1;
491
Simon Kelleyc72daea2012-01-05 21:33:27 +0000492 *up = lease->next; /* unlink */
Simon Kelley7cebd202006-05-06 14:13:33 +0100493
494 /* Put on old_leases list 'till we
495 can run the script */
496 lease->next = old_leases;
497 old_leases = lease;
498
Simon Kelley44a2a312004-03-10 20:04:35 +0000499 leases_left++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000500 }
501 else
502 up = &lease->next;
503 }
504}
505
506
Simon Kelleycdeda282006-03-16 20:16:06 +0000507struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,
Simon Kelley0a852542005-03-23 20:28:59 +0000508 unsigned char *clid, int clid_len)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000509{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000510 struct dhcp_lease *lease;
511
Simon Kelley0a852542005-03-23 20:28:59 +0000512 if (clid)
513 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000514 {
515#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000516 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000517 continue;
518#endif
519 if (lease->clid && clid_len == lease->clid_len &&
520 memcmp(clid, lease->clid, clid_len) == 0)
521 return lease;
522 }
Simon Kelley0a852542005-03-23 20:28:59 +0000523
524 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000525 {
526#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000527 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000528 continue;
529#endif
530 if ((!lease->clid || !clid) &&
531 hw_len != 0 &&
532 lease->hwaddr_len == hw_len &&
533 lease->hwaddr_type == hw_type &&
534 memcmp(hwaddr, lease->hwaddr, hw_len) == 0)
535 return lease;
536 }
537
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000538 return NULL;
539}
540
541struct dhcp_lease *lease_find_by_addr(struct in_addr addr)
542{
543 struct dhcp_lease *lease;
544
545 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000546 {
547#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000548 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000549 continue;
550#endif
551 if (lease->addr.s_addr == addr.s_addr)
552 return lease;
553 }
554
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000555 return NULL;
556}
557
Simon Kelley52b92f42012-01-22 16:05:15 +0000558#ifdef HAVE_DHCP6
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000559/* find address for {CLID, IAID, address} */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000560struct 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 Kelleya6ebfac2013-03-06 20:52:35 +0000570 if (memcmp(lease->hwaddr, addr, IN6ADDRSZ) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000571 continue;
572
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000573 if ((clid_len != lease->clid_len ||
Simon Kelley4cb1b322012-02-06 14:30:41 +0000574 memcmp(clid, lease->clid, clid_len) != 0))
575 continue;
576
Simon Kelley8b460612012-09-08 21:47:28 +0100577 return lease;
Simon Kelley52b92f42012-01-22 16:05:15 +0000578 }
579
580 return NULL;
581}
582
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000583/* reset "USED flags */
584void lease6_reset(void)
Simon Kelley8b460612012-09-08 21:47:28 +0100585{
586 struct dhcp_lease *lease;
587
588 for (lease = leases; lease; lease = lease->next)
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000589 lease->flags &= ~LEASE_USED;
590}
591
592/* enumerate all leases belonging to {CLID, IAID} */
593struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_type, unsigned char *clid, int clid_len, int iaid)
594{
595 struct dhcp_lease *lease;
596
597 if (!first)
598 first = leases;
Simon Kelley27cb3142013-04-02 20:06:39 +0100599 else
600 first = first->next;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000601
602 for (lease = first; lease; lease = lease->next)
Simon Kelley8b460612012-09-08 21:47:28 +0100603 {
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000604 if (lease->flags & LEASE_USED)
605 continue;
606
Simon Kelley8b460612012-09-08 21:47:28 +0100607 if (!(lease->flags & lease_type) || lease->hwaddr_type != iaid)
608 continue;
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000609
610 if ((clid_len != lease->clid_len ||
611 memcmp(clid, lease->clid, clid_len) != 0))
612 continue;
613
614 return lease;
Simon Kelley8b460612012-09-08 21:47:28 +0100615 }
Simon Kelleya6ebfac2013-03-06 20:52:35 +0000616
617 return NULL;
Simon Kelley8b460612012-09-08 21:47:28 +0100618}
619
Simon Kelley52b92f42012-01-22 16:05:15 +0000620struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr)
621{
622 struct dhcp_lease *lease;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000623
Simon Kelley52b92f42012-01-22 16:05:15 +0000624 for (lease = leases; lease; lease = lease->next)
625 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000626 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelley52b92f42012-01-22 16:05:15 +0000627 continue;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000628
Simon Kelley52b92f42012-01-22 16:05:15 +0000629 if (is_same_net6((struct in6_addr *)lease->hwaddr, net, prefix) &&
630 (prefix == 128 || addr6part((struct in6_addr *)lease->hwaddr) == addr))
631 return lease;
632 }
633
634 return NULL;
Simon Kelley07933802012-02-14 20:55:25 +0000635}
636
637/* Find largest assigned address in context */
638u64 lease_find_max_addr6(struct dhcp_context *context)
639{
640 struct dhcp_lease *lease;
641 u64 addr = addr6part(&context->start6);
642
643 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
644 for (lease = leases; lease; lease = lease->next)
645 {
Simon Kelley07933802012-02-14 20:55:25 +0000646 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
647 continue;
Simon Kelley6caacac2012-02-15 21:58:33 +0000648
Simon Kelley07933802012-02-14 20:55:25 +0000649 if (is_same_net6((struct in6_addr *)lease->hwaddr, &context->start6, 64) &&
650 addr6part((struct in6_addr *)lease->hwaddr) > addr6part(&context->start6) &&
651 addr6part((struct in6_addr *)lease->hwaddr) <= addr6part(&context->end6) &&
652 addr6part((struct in6_addr *)lease->hwaddr) > addr)
653 addr = addr6part((struct in6_addr *)lease->hwaddr);
654 }
655
656 return addr;
657}
658
Simon Kelley52b92f42012-01-22 16:05:15 +0000659#endif
660
Simon Kelley7de060b2011-08-26 17:24:52 +0100661/* Find largest assigned address in context */
662struct in_addr lease_find_max_addr(struct dhcp_context *context)
663{
664 struct dhcp_lease *lease;
665 struct in_addr addr = context->start;
666
667 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
668 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000669 {
670#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000671 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000672 continue;
673#endif
674 if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) &&
675 ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) &&
676 ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr)))
677 addr = lease->addr;
678 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100679
680 return addr;
681}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000682
Simon Kelleyc72daea2012-01-05 21:33:27 +0000683static struct dhcp_lease *lease_allocate(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000684{
685 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100686 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000687 return NULL;
688
Simon Kelley7cebd202006-05-06 14:13:33 +0100689 memset(lease, 0, sizeof(struct dhcp_lease));
Simon Kelley4cb1b322012-02-06 14:30:41 +0000690 lease->flags = LEASE_NEW;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000691 lease->expires = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100692#ifdef HAVE_BROKEN_RTC
693 lease->length = 0xffffffff; /* illegal value */
694#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000695 lease->next = leases;
696 leases = lease;
697
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100698 file_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000699 leases_left--;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000700
701 return lease;
702}
703
Simon Kelley52b92f42012-01-22 16:05:15 +0000704struct dhcp_lease *lease4_allocate(struct in_addr addr)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000705{
706 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100707 if (lease)
708 {
709 lease->addr = addr;
710 lease->hwaddr_len = 256; /* illegal value */
711 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000712
713 return lease;
714}
715
716#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000717struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000718{
719 struct dhcp_lease *lease = lease_allocate();
Simon Kelley0b0a73c2013-04-11 14:07:02 +0100720
721 if (lease)
722 {
723 memcpy(lease->hwaddr, addrp, sizeof(*addrp)) ;
724 lease->flags |= lease_type;
725 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000726
727 return lease;
728}
729#endif
730
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100731void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000732{
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100733 time_t exp = now + (time_t)len;
734
735 if (len == 0xffffffff)
736 {
737 exp = 0;
738 len = 0;
739 }
740
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000741 if (exp != lease->expires)
Simon Kelley0a852542005-03-23 20:28:59 +0000742 {
Simon Kelley0a852542005-03-23 20:28:59 +0000743 dns_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100744 lease->expires = exp;
745#ifndef HAVE_BROKEN_RTC
Simon Kelley4cb1b322012-02-06 14:30:41 +0000746 lease->flags |= LEASE_AUX_CHANGED;
747 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100748#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000749 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100750
751#ifdef HAVE_BROKEN_RTC
752 if (len != lease->length)
753 {
754 lease->length = len;
Simon Kelleyb7f40202012-02-29 21:43:37 +0000755 lease->flags |= LEASE_AUX_CHANGED;
756 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100757 }
758#endif
759}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000760
Simon Kelley7cebd202006-05-06 14:13:33 +0100761void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
Simon Kelleya9ab7322012-04-28 11:29:37 +0100762 unsigned char *clid, int hw_len, int hw_type, int clid_len,
763 time_t now, int force)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000764{
Simon Kelley353ae4d2012-03-19 20:07:51 +0000765#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100766 int change = force;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000767 lease->flags |= LEASE_HAVE_HWADDR;
768#endif
769
Simon Kelleya9ab7322012-04-28 11:29:37 +0100770 (void)force;
771
Simon Kelleycdeda282006-03-16 20:16:06 +0000772 if (hw_len != lease->hwaddr_len ||
773 hw_type != lease->hwaddr_type ||
Simon Kelley7cebd202006-05-06 14:13:33 +0100774 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000775 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000776 if (hw_len != 0)
777 memcpy(lease->hwaddr, hwaddr, hw_len);
Simon Kelleycdeda282006-03-16 20:16:06 +0000778 lease->hwaddr_len = hw_len;
779 lease->hwaddr_type = hw_type;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000780 lease->flags |= LEASE_CHANGED;
781 file_dirty = 1; /* run script on change */
Simon Kelley353ae4d2012-03-19 20:07:51 +0000782#ifdef HAVE_DHCP6
783 change = 1;
784#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000785 }
Simon Kelley0a852542005-03-23 20:28:59 +0000786
787 /* only update clid when one is available, stops packets
788 without a clid removing the record. Lease init uses
789 clid_len == 0 for no clid. */
790 if (clid_len != 0 && clid)
791 {
792 if (!lease->clid)
793 lease->clid_len = 0;
794
795 if (lease->clid_len != clid_len)
796 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000797 lease->flags |= LEASE_AUX_CHANGED;
798 file_dirty = 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100799 free(lease->clid);
800 if (!(lease->clid = whine_malloc(clid_len)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100801 return;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000802#ifdef HAVE_DHCP6
803 change = 1;
804#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000805 }
806 else if (memcmp(lease->clid, clid, clid_len) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000807 {
808 lease->flags |= LEASE_AUX_CHANGED;
809 file_dirty = 1;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000810#ifdef HAVE_DHCP6
811 change = 1;
812#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +0000813 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000814
Simon Kelley0a852542005-03-23 20:28:59 +0000815 lease->clid_len = clid_len;
816 memcpy(lease->clid, clid, clid_len);
817 }
Simon Kelley353ae4d2012-03-19 20:07:51 +0000818
819#ifdef HAVE_DHCP6
820 if (change)
Simon Kelleya9ab7322012-04-28 11:29:37 +0100821 slaac_add_addrs(lease, now, force);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000822#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000823}
824
Simon Kelley9009d742008-11-14 20:04:27 +0000825static void kill_name(struct dhcp_lease *lease)
826{
827 /* run script to say we lost our old name */
828
829 /* this shouldn't happen unless updates are very quick and the
830 script very slow, we just avoid a memory leak if it does. */
831 free(lease->old_hostname);
832
833 /* If we know the fqdn, pass that. The helper will derive the
Simon Kelley4cb1b322012-02-06 14:30:41 +0000834 unqualified name from it, free the unqualified name here. */
Simon Kelley9009d742008-11-14 20:04:27 +0000835
836 if (lease->fqdn)
837 {
838 lease->old_hostname = lease->fqdn;
839 free(lease->hostname);
840 }
841 else
842 lease->old_hostname = lease->hostname;
843
844 lease->hostname = lease->fqdn = NULL;
845}
846
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000847void lease_set_hostname(struct dhcp_lease *lease, char *name, int auth, char *domain, char *config_domain)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000848{
849 struct dhcp_lease *lease_tmp;
850 char *new_name = NULL, *new_fqdn = NULL;
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000851
852 if (config_domain && (!domain || !hostname_isequal(domain, config_domain)))
853 my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, name);
Simon Kelley9009d742008-11-14 20:04:27 +0000854
Simon Kelleya2226412004-05-13 20:27:08 +0100855 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000856 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000857 if (auth)
858 lease->flags |= LEASE_AUTH_NAME;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000859 return;
860 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100861
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000862 if (!name && !lease->hostname)
863 return;
864
865 /* If a machine turns up on a new net without dropping the old lease,
866 or two machines claim the same name, then we end up with two interfaces with
Simon Kelleyb8187c82005-11-26 21:46:27 +0000867 the same name. Check for that here and remove the name from the old lease.
Simon Kelley4cb1b322012-02-06 14:30:41 +0000868 Note that IPv6 leases are different. All the leases to the same DUID are
869 allowed the same name.
870
Simon Kelleyb8187c82005-11-26 21:46:27 +0000871 Don't allow a name from the client to override a name from dnsmasq config. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000872
873 if (name)
874 {
Simon Kelley9009d742008-11-14 20:04:27 +0000875 if ((new_name = whine_malloc(strlen(name) + 1)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000876 {
Simon Kelley9009d742008-11-14 20:04:27 +0000877 strcpy(new_name, name);
Simon Kelley4cb1b322012-02-06 14:30:41 +0000878 if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2)))
Simon Kelley9009d742008-11-14 20:04:27 +0000879 {
880 strcpy(new_fqdn, name);
881 strcat(new_fqdn, ".");
Simon Kelley4cb1b322012-02-06 14:30:41 +0000882 strcat(new_fqdn, domain);
Simon Kelley9009d742008-11-14 20:04:27 +0000883 }
884 }
885
886 /* Depending on mode, we check either unqualified name or FQDN. */
887 for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next)
888 {
Simon Kelley28866e92011-02-14 20:19:14 +0000889 if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +0000890 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000891 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn))
Simon Kelley9009d742008-11-14 20:04:27 +0000892 continue;
893 }
894 else
895 {
896 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
897 continue;
898 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000899
900 if (lease->flags & (LEASE_TA | LEASE_NA))
901 {
902 if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA)))
903 continue;
904
Simon Kelleyceae00d2012-02-09 21:28:14 +0000905 /* another lease for the same DUID is OK for IPv6 */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000906 if (lease->clid_len == lease_tmp->clid_len &&
907 lease->clid && lease_tmp->clid &&
908 memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0)
909 continue;
910 }
911 else if (lease_tmp->flags & (LEASE_TA | LEASE_NA))
912 continue;
913
914 if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth)
Simon Kelley9009d742008-11-14 20:04:27 +0000915 {
916 free(new_name);
917 free(new_fqdn);
918 return;
919 }
920
921 kill_name(lease_tmp);
922 break;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000923 }
924 }
925
926 if (lease->hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000927 kill_name(lease);
Simon Kelley16972692006-10-16 20:04:18 +0100928
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000929 lease->hostname = new_name;
930 lease->fqdn = new_fqdn;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000931
932 if (auth)
933 lease->flags |= LEASE_AUTH_NAME;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000934
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100935 file_dirty = 1;
Simon Kelley7cebd202006-05-06 14:13:33 +0100936 dns_dirty = 1;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000937 lease->flags |= LEASE_CHANGED; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000938}
939
Simon Kelley353ae4d2012-03-19 20:07:51 +0000940void lease_set_interface(struct dhcp_lease *lease, int interface, time_t now)
Simon Kelley824af852008-02-12 20:43:05 +0000941{
942 if (lease->last_interface == interface)
943 return;
944
945 lease->last_interface = interface;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000946 lease->flags |= LEASE_CHANGED;
Simon Kelley353ae4d2012-03-19 20:07:51 +0000947
948#ifdef HAVE_DHCP6
Simon Kelleya9ab7322012-04-28 11:29:37 +0100949 slaac_add_addrs(lease, now, 0);
Simon Kelley353ae4d2012-03-19 20:07:51 +0000950#endif
Simon Kelley824af852008-02-12 20:43:05 +0000951}
952
Simon Kelley5aabfc72007-08-29 11:24:47 +0100953void rerun_scripts(void)
954{
955 struct dhcp_lease *lease;
956
957 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000958 lease->flags |= LEASE_CHANGED;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100959}
960
Simon Kelley7cebd202006-05-06 14:13:33 +0100961/* deleted leases get transferred to the old_leases list.
962 remove them here, after calling the lease change
Simon Kelley16972692006-10-16 20:04:18 +0100963 script. Also run the lease change script on new/modified leases.
964
965 Return zero if nothing to do. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100966int do_script_run(time_t now)
Simon Kelley7cebd202006-05-06 14:13:33 +0100967{
968 struct dhcp_lease *lease;
969
Simon Kelley9009d742008-11-14 20:04:27 +0000970#ifdef HAVE_DBUS
971 /* If we're going to be sending DBus signals, but the connection is not yet up,
972 delay everything until it is. */
Simon Kelley28866e92011-02-14 20:19:14 +0000973 if (option_bool(OPT_DBUS) && !daemon->dbus)
Simon Kelley9009d742008-11-14 20:04:27 +0000974 return 0;
975#endif
976
Simon Kelley16972692006-10-16 20:04:18 +0100977 if (old_leases)
Simon Kelley7cebd202006-05-06 14:13:33 +0100978 {
Simon Kelley7cebd202006-05-06 14:13:33 +0100979 lease = old_leases;
Simon Kelley16972692006-10-16 20:04:18 +0100980
981 /* If the lease still has an old_hostname, do the "old" action on that first */
982 if (lease->old_hostname)
983 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100984#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +0100985 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
986#endif
Simon Kelley16972692006-10-16 20:04:18 +0100987 free(lease->old_hostname);
988 lease->old_hostname = NULL;
989 return 1;
990 }
991 else
992 {
Simon Kelley353ae4d2012-03-19 20:07:51 +0000993#ifdef HAVE_DHCP6
994 struct slaac_address *slaac, *tmp;
995 for (slaac = lease->slaac_address; slaac; slaac = tmp)
996 {
997 tmp = slaac->next;
998 free(slaac);
999 }
1000#endif
Simon Kelley9009d742008-11-14 20:04:27 +00001001 kill_name(lease);
Simon Kelley1f15b812009-10-13 17:49:32 +01001002#ifdef HAVE_SCRIPT
Simon Kelley9009d742008-11-14 20:04:27 +00001003 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001004#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001005#ifdef HAVE_DBUS
1006 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
1007#endif
Simon Kelley16972692006-10-16 20:04:18 +01001008 old_leases = lease->next;
1009
Simon Kelley9009d742008-11-14 20:04:27 +00001010 free(lease->old_hostname);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001011 free(lease->clid);
Simon Kelley316e2732010-01-22 20:16:09 +00001012 free(lease->extradata);
Simon Kelley16972692006-10-16 20:04:18 +01001013 free(lease);
1014
1015 return 1;
1016 }
Simon Kelley7cebd202006-05-06 14:13:33 +01001017 }
Simon Kelley16972692006-10-16 20:04:18 +01001018
1019 /* make sure we announce the loss of a hostname before its new location. */
1020 for (lease = leases; lease; lease = lease->next)
1021 if (lease->old_hostname)
1022 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001023#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +01001024 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
1025#endif
Simon Kelley16972692006-10-16 20:04:18 +01001026 free(lease->old_hostname);
1027 lease->old_hostname = NULL;
1028 return 1;
1029 }
1030
Simon Kelley7cebd202006-05-06 14:13:33 +01001031 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +00001032 if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
1033 ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)))
Simon Kelley7cebd202006-05-06 14:13:33 +01001034 {
Simon Kelley1f15b812009-10-13 17:49:32 +01001035#ifdef HAVE_SCRIPT
Simon Kelley4cb1b322012-02-06 14:30:41 +00001036 queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley9009d742008-11-14 20:04:27 +00001037 lease->fqdn ? lease->fqdn : lease->hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +01001038#endif
Simon Kelley1f15b812009-10-13 17:49:32 +01001039#ifdef HAVE_DBUS
Simon Kelley4cb1b322012-02-06 14:30:41 +00001040 emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley1f15b812009-10-13 17:49:32 +01001041 lease->fqdn ? lease->fqdn : lease->hostname);
1042#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +00001043 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED);
Simon Kelley16972692006-10-16 20:04:18 +01001044
Simon Kelley316e2732010-01-22 20:16:09 +00001045 /* this is used for the "add" call, then junked, since they're not in the database */
1046 free(lease->extradata);
1047 lease->extradata = NULL;
Simon Kelley16972692006-10-16 20:04:18 +01001048
1049 return 1;
Simon Kelley7cebd202006-05-06 14:13:33 +01001050 }
Simon Kelley16972692006-10-16 20:04:18 +01001051
1052 return 0; /* nothing to do */
Simon Kelley7cebd202006-05-06 14:13:33 +01001053}
Simon Kelley7622fc02009-06-04 20:32:05 +01001054
Simon Kelleyceae00d2012-02-09 21:28:14 +00001055#ifdef HAVE_SCRIPT
1056void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim)
1057{
1058 unsigned int i;
1059
1060 /* check for embeded NULLs */
1061 for (i = 0; i < len; i++)
1062 if (data[i] == 0)
1063 {
1064 len = i;
1065 break;
1066 }
1067
1068 if ((lease->extradata_size - lease->extradata_len) < (len + 1))
1069 {
1070 size_t newsz = lease->extradata_len + len + 100;
1071 unsigned char *new = whine_malloc(newsz);
1072
1073 if (!new)
1074 return;
1075
1076 if (lease->extradata)
1077 {
1078 memcpy(new, lease->extradata, lease->extradata_len);
1079 free(lease->extradata);
1080 }
1081
1082 lease->extradata = new;
1083 lease->extradata_size = newsz;
1084 }
1085
1086 if (len != 0)
1087 memcpy(lease->extradata + lease->extradata_len, data, len);
1088 lease->extradata[lease->extradata_len + len] = delim;
1089 lease->extradata_len += len + 1;
1090}
1091#endif
1092
Simon Kelley7622fc02009-06-04 20:32:05 +01001093#endif
Simon Kelley7cebd202006-05-06 14:13:33 +01001094
1095
1096
Simon Kelley9e4abcb2004-01-22 19:47:41 +00001097