]> git.ipfire.org Git - thirdparty/dhcp.git/blob - server/dhcp.c
Update the fsync batching patch to work with the isc libraries - 21044
[thirdparty/dhcp.git] / server / dhcp.c
1 /* dhcp.c
2
3 DHCP Protocol engine. */
4
5 /*
6 * Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1995-2003 by Internet Software Consortium
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 * Internet Systems Consortium, Inc.
22 * 950 Charter Street
23 * Redwood City, CA 94063
24 * <info@isc.org>
25 * https://www.isc.org/
26 *
27 * This software has been written for Internet Systems Consortium
28 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29 * To learn more about Internet Systems Consortium, see
30 * ``https://www.isc.org/''. To learn more about Vixie Enterprises,
31 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
32 * ``http://www.nominum.com''.
33 */
34
35 #include "dhcpd.h"
36 #include <errno.h>
37 #include <limits.h>
38 #include <sys/time.h>
39
40 static void commit_leases_ackout(void *foo);
41 static void maybe_return_agent_options(struct packet *packet,
42 struct option_state *options);
43
44 int outstanding_pings;
45
46 struct leasequeue *ackqueue_head, *ackqueue_tail;
47 static struct leasequeue *free_ackqueue;
48 static struct timeval max_fsync;
49
50 int outstanding_acks;
51 int max_outstanding_acks = DEFAULT_DELAYED_ACK;
52 int max_ack_delay_secs = DEFAULT_ACK_DELAY_SECS;
53 int max_ack_delay_usecs = DEFAULT_ACK_DELAY_USECS;
54 int min_ack_delay_usecs = DEFAULT_MIN_ACK_DELAY_USECS;
55
56 static char dhcp_message [256];
57 static int site_code_min;
58
59 static int find_min_site_code(struct universe *);
60 static isc_result_t lowest_site_code(const void *, unsigned, void *);
61
62 static const char *dhcp_type_names [] = {
63 "DHCPDISCOVER",
64 "DHCPOFFER",
65 "DHCPREQUEST",
66 "DHCPDECLINE",
67 "DHCPACK",
68 "DHCPNAK",
69 "DHCPRELEASE",
70 "DHCPINFORM",
71 "type 9",
72 "DHCPLEASEQUERY",
73 "DHCPLEASEUNASSIGNED",
74 "DHCPLEASEUNKNOWN",
75 "DHCPLEASEACTIVE"
76 };
77 const int dhcp_type_name_max = ((sizeof dhcp_type_names) / sizeof (char *));
78
79 #if defined (TRACING)
80 # define send_packet trace_packet_send
81 #endif
82
83 void
84 dhcp (struct packet *packet) {
85 int ms_nulltp = 0;
86 struct option_cache *oc;
87 struct lease *lease = NULL;
88 const char *errmsg;
89 struct data_string data;
90
91 if (!locate_network(packet) &&
92 packet->packet_type != DHCPREQUEST &&
93 packet->packet_type != DHCPINFORM &&
94 packet->packet_type != DHCPLEASEQUERY) {
95 const char *s;
96 char typebuf[32];
97 errmsg = "unknown network segment";
98 bad_packet:
99
100 if (packet->packet_type > 0 &&
101 packet->packet_type <= dhcp_type_name_max) {
102 s = dhcp_type_names[packet->packet_type - 1];
103 } else {
104 /* %Audit% Cannot exceed 28 bytes. %2004.06.17,Safe% */
105 sprintf(typebuf, "type %d", packet->packet_type);
106 s = typebuf;
107 }
108
109 log_info("%s from %s via %s: %s", s,
110 (packet->raw->htype
111 ? print_hw_addr(packet->raw->htype,
112 packet->raw->hlen,
113 packet->raw->chaddr)
114 : "<no identifier>"),
115 packet->raw->giaddr.s_addr
116 ? inet_ntoa(packet->raw->giaddr)
117 : packet->interface->name, errmsg);
118 goto out;
119 }
120
121 /* There is a problem with the relay agent information option,
122 * which is that in order for a normal relay agent to append
123 * this option, the relay agent has to have been involved in
124 * getting the packet from the client to the server. Note
125 * that this is the software entity known as the relay agent,
126 * _not_ the hardware entity known as a router in which the
127 * relay agent may be running, so the fact that a router has
128 * forwarded a packet does not mean that the relay agent in
129 * the router was involved.
130 *
131 * So when the client broadcasts (DHCPDISCOVER, or giaddr is set),
132 * we can be sure that there are either agent options in the
133 * packet, or there aren't supposed to be. When the giaddr is not
134 * set, it's still possible that the client is on a directly
135 * attached subnet, and agent options are being appended by an l2
136 * device that has no address, and so sets no giaddr.
137 *
138 * But in either case it's possible that the packets we receive
139 * from the client in RENEW state may not include the agent options,
140 * so if they are not in the packet we must "pretend" the last values
141 * we observed were provided.
142 */
143 if (packet->packet_type == DHCPREQUEST &&
144 packet->raw->ciaddr.s_addr && !packet->raw->giaddr.s_addr &&
145 (packet->options->universe_count <= agent_universe.index ||
146 packet->options->universes[agent_universe.index] == NULL))
147 {
148 struct iaddr cip;
149
150 cip.len = sizeof packet -> raw -> ciaddr;
151 memcpy (cip.iabuf, &packet -> raw -> ciaddr,
152 sizeof packet -> raw -> ciaddr);
153 if (!find_lease_by_ip_addr (&lease, cip, MDL))
154 goto nolease;
155
156 /* If there are no agent options on the lease, it's not
157 interesting. */
158 if (!lease -> agent_options)
159 goto nolease;
160
161 /* The client should not be unicasting a renewal if its lease
162 has expired, so make it go through the process of getting
163 its agent options legally. */
164 if (lease -> ends < cur_time)
165 goto nolease;
166
167 if (lease -> uid_len) {
168 oc = lookup_option (&dhcp_universe, packet -> options,
169 DHO_DHCP_CLIENT_IDENTIFIER);
170 if (!oc)
171 goto nolease;
172
173 memset (&data, 0, sizeof data);
174 if (!evaluate_option_cache (&data,
175 packet, (struct lease *)0,
176 (struct client_state *)0,
177 packet -> options,
178 (struct option_state *)0,
179 &global_scope, oc, MDL))
180 goto nolease;
181 if (lease -> uid_len != data.len ||
182 memcmp (lease -> uid, data.data, data.len)) {
183 data_string_forget (&data, MDL);
184 goto nolease;
185 }
186 data_string_forget (&data, MDL);
187 } else
188 if ((lease -> hardware_addr.hbuf [0] !=
189 packet -> raw -> htype) ||
190 (lease -> hardware_addr.hlen - 1 !=
191 packet -> raw -> hlen) ||
192 memcmp (&lease -> hardware_addr.hbuf [1],
193 packet -> raw -> chaddr,
194 packet -> raw -> hlen))
195 goto nolease;
196
197 /* Okay, so we found a lease that matches the client. */
198 option_chain_head_reference ((struct option_chain_head **)
199 &(packet -> options -> universes
200 [agent_universe.index]),
201 lease -> agent_options, MDL);
202
203 if (packet->options->universe_count <= agent_universe.index)
204 packet->options->universe_count =
205 agent_universe.index + 1;
206
207 packet->agent_options_stashed = ISC_TRUE;
208 }
209 nolease:
210
211 /* If a client null terminates options it sends, it probably
212 * expects the server to reciprocate.
213 */
214 if ((oc = lookup_option (&dhcp_universe, packet -> options,
215 DHO_HOST_NAME))) {
216 if (!oc -> expression)
217 ms_nulltp = oc->flags & OPTION_HAD_NULLS;
218 }
219
220 /* Classify the client. */
221 classify_client (packet);
222
223 switch (packet -> packet_type) {
224 case DHCPDISCOVER:
225 dhcpdiscover (packet, ms_nulltp);
226 break;
227
228 case DHCPREQUEST:
229 dhcprequest (packet, ms_nulltp, lease);
230 break;
231
232 case DHCPRELEASE:
233 dhcprelease (packet, ms_nulltp);
234 break;
235
236 case DHCPDECLINE:
237 dhcpdecline (packet, ms_nulltp);
238 break;
239
240 case DHCPINFORM:
241 dhcpinform (packet, ms_nulltp);
242 break;
243
244 case DHCPLEASEQUERY:
245 dhcpleasequery(packet, ms_nulltp);
246 break;
247
248 case DHCPACK:
249 case DHCPOFFER:
250 case DHCPNAK:
251 case DHCPLEASEUNASSIGNED:
252 case DHCPLEASEUNKNOWN:
253 case DHCPLEASEACTIVE:
254 break;
255
256 default:
257 errmsg = "unknown packet type";
258 goto bad_packet;
259 }
260 out:
261 if (lease)
262 lease_dereference (&lease, MDL);
263 }
264
265 void dhcpdiscover (packet, ms_nulltp)
266 struct packet *packet;
267 int ms_nulltp;
268 {
269 struct lease *lease = (struct lease *)0;
270 char msgbuf [1024]; /* XXX */
271 TIME when;
272 const char *s;
273 int peer_has_leases = 0;
274 #if defined (FAILOVER_PROTOCOL)
275 dhcp_failover_state_t *peer;
276 #endif
277
278 find_lease (&lease, packet, packet -> shared_network,
279 0, &peer_has_leases, (struct lease *)0, MDL);
280
281 if (lease && lease -> client_hostname) {
282 if ((strlen (lease -> client_hostname) <= 64) &&
283 db_printable((unsigned char *)lease->client_hostname))
284 s = lease -> client_hostname;
285 else
286 s = "Hostname Unsuitable for Printing";
287 } else
288 s = (char *)0;
289
290 /* %Audit% This is log output. %2004.06.17,Safe%
291 * If we truncate we hope the user can get a hint from the log.
292 */
293 snprintf (msgbuf, sizeof msgbuf, "DHCPDISCOVER from %s %s%s%svia %s",
294 (packet -> raw -> htype
295 ? print_hw_addr (packet -> raw -> htype,
296 packet -> raw -> hlen,
297 packet -> raw -> chaddr)
298 : (lease
299 ? print_hex_1(lease->uid_len, lease->uid, 60)
300 : "<no identifier>")),
301 s ? "(" : "", s ? s : "", s ? ") " : "",
302 packet -> raw -> giaddr.s_addr
303 ? inet_ntoa (packet -> raw -> giaddr)
304 : packet -> interface -> name);
305
306 /* Sourceless packets don't make sense here. */
307 if (!packet -> shared_network) {
308 log_info ("Packet from unknown subnet: %s",
309 inet_ntoa (packet -> raw -> giaddr));
310 goto out;
311 }
312
313 #if defined (FAILOVER_PROTOCOL)
314 if (lease && lease -> pool && lease -> pool -> failover_peer) {
315 peer = lease -> pool -> failover_peer;
316
317 /*
318 * If the lease is ours to (re)allocate, then allocate it.
319 *
320 * If the lease is active, it belongs to the client. This
321 * is the right lease, if we are to offer one. We decide
322 * whether or not to offer later on.
323 *
324 * If the lease was last active, and we've reached this
325 * point, then it was last active with the same client. We
326 * can safely re-activate the lease with this client.
327 */
328 if (lease->binding_state == FTS_ACTIVE ||
329 lease->rewind_binding_state == FTS_ACTIVE ||
330 lease_mine_to_reallocate(lease)) {
331 ; /* This space intentionally left blank. */
332
333 /* Otherwise, we can't let the client have this lease. */
334 } else {
335 #if defined (DEBUG_FIND_LEASE)
336 log_debug ("discarding %s - %s",
337 piaddr (lease -> ip_addr),
338 binding_state_print (lease -> binding_state));
339 #endif
340 lease_dereference (&lease, MDL);
341 }
342 }
343 #endif
344
345 /* If we didn't find a lease, try to allocate one... */
346 if (!lease) {
347 if (!allocate_lease (&lease, packet,
348 packet -> shared_network -> pools,
349 &peer_has_leases)) {
350 if (peer_has_leases)
351 log_error ("%s: peer holds all free leases",
352 msgbuf);
353 else
354 log_error ("%s: network %s: no free leases",
355 msgbuf,
356 packet -> shared_network -> name);
357 return;
358 }
359 }
360
361 #if defined (FAILOVER_PROTOCOL)
362 if (lease && lease -> pool && lease -> pool -> failover_peer) {
363 peer = lease -> pool -> failover_peer;
364 if (peer -> service_state == not_responding ||
365 peer -> service_state == service_startup) {
366 log_info ("%s: not responding%s",
367 msgbuf, peer -> nrr);
368 goto out;
369 }
370 } else
371 peer = (dhcp_failover_state_t *)0;
372
373 /* Do load balancing if configured. */
374 if (peer && (peer -> service_state == cooperating) &&
375 !load_balance_mine (packet, peer)) {
376 if (peer_has_leases) {
377 log_debug ("%s: load balance to peer %s",
378 msgbuf, peer -> name);
379 goto out;
380 } else {
381 log_debug ("%s: cancel load balance to peer %s - %s",
382 msgbuf, peer -> name, "no free leases");
383 }
384 }
385 #endif
386
387 /* If it's an expired lease, get rid of any bindings. */
388 if (lease -> ends < cur_time && lease -> scope)
389 binding_scope_dereference (&lease -> scope, MDL);
390
391 /* Set the lease to really expire in 2 minutes, unless it has
392 not yet expired, in which case leave its expiry time alone. */
393 when = cur_time + 120;
394 if (when < lease -> ends)
395 when = lease -> ends;
396
397 ack_lease (packet, lease, DHCPOFFER, when, msgbuf, ms_nulltp,
398 (struct host_decl *)0);
399 out:
400 if (lease)
401 lease_dereference (&lease, MDL);
402 }
403
404 void dhcprequest (packet, ms_nulltp, ip_lease)
405 struct packet *packet;
406 int ms_nulltp;
407 struct lease *ip_lease;
408 {
409 struct lease *lease;
410 struct iaddr cip;
411 struct iaddr sip;
412 struct subnet *subnet;
413 int ours = 0;
414 struct option_cache *oc;
415 struct data_string data;
416 char msgbuf [1024]; /* XXX */
417 const char *s;
418 char smbuf [19];
419 #if defined (FAILOVER_PROTOCOL)
420 dhcp_failover_state_t *peer;
421 #endif
422 int have_server_identifier = 0;
423 int have_requested_addr = 0;
424
425 oc = lookup_option (&dhcp_universe, packet -> options,
426 DHO_DHCP_REQUESTED_ADDRESS);
427 memset (&data, 0, sizeof data);
428 if (oc &&
429 evaluate_option_cache (&data, packet, (struct lease *)0,
430 (struct client_state *)0,
431 packet -> options, (struct option_state *)0,
432 &global_scope, oc, MDL)) {
433 cip.len = 4;
434 memcpy (cip.iabuf, data.data, 4);
435 data_string_forget (&data, MDL);
436 have_requested_addr = 1;
437 } else {
438 oc = (struct option_cache *)0;
439 cip.len = 4;
440 memcpy (cip.iabuf, &packet -> raw -> ciaddr.s_addr, 4);
441 }
442
443 /* Find the lease that matches the address requested by the
444 client. */
445
446 subnet = (struct subnet *)0;
447 lease = (struct lease *)0;
448 if (find_subnet (&subnet, cip, MDL))
449 find_lease (&lease, packet,
450 subnet -> shared_network, &ours, 0, ip_lease, MDL);
451
452 if (lease && lease -> client_hostname) {
453 if ((strlen (lease -> client_hostname) <= 64) &&
454 db_printable((unsigned char *)lease->client_hostname))
455 s = lease -> client_hostname;
456 else
457 s = "Hostname Unsuitable for Printing";
458 } else
459 s = (char *)0;
460
461 oc = lookup_option (&dhcp_universe, packet -> options,
462 DHO_DHCP_SERVER_IDENTIFIER);
463 memset (&data, 0, sizeof data);
464 if (oc &&
465 evaluate_option_cache (&data, packet, (struct lease *)0,
466 (struct client_state *)0,
467 packet -> options, (struct option_state *)0,
468 &global_scope, oc, MDL)) {
469 sip.len = 4;
470 memcpy (sip.iabuf, data.data, 4);
471 data_string_forget (&data, MDL);
472 /* piaddr() should not return more than a 15 byte string.
473 * safe.
474 */
475 sprintf (smbuf, " (%s)", piaddr (sip));
476 have_server_identifier = 1;
477 } else
478 smbuf [0] = 0;
479
480 /* %Audit% This is log output. %2004.06.17,Safe%
481 * If we truncate we hope the user can get a hint from the log.
482 */
483 snprintf (msgbuf, sizeof msgbuf,
484 "DHCPREQUEST for %s%s from %s %s%s%svia %s",
485 piaddr (cip), smbuf,
486 (packet -> raw -> htype
487 ? print_hw_addr (packet -> raw -> htype,
488 packet -> raw -> hlen,
489 packet -> raw -> chaddr)
490 : (lease
491 ? print_hex_1(lease->uid_len, lease->uid, 60)
492 : "<no identifier>")),
493 s ? "(" : "", s ? s : "", s ? ") " : "",
494 packet -> raw -> giaddr.s_addr
495 ? inet_ntoa (packet -> raw -> giaddr)
496 : packet -> interface -> name);
497
498 #if defined (FAILOVER_PROTOCOL)
499 if (lease && lease -> pool && lease -> pool -> failover_peer) {
500 peer = lease -> pool -> failover_peer;
501 if (peer -> service_state == not_responding ||
502 peer -> service_state == service_startup) {
503 log_info ("%s: not responding%s",
504 msgbuf, peer -> nrr);
505 goto out;
506 }
507
508 /* "load balance to peer" - is not done at all for request.
509 *
510 * If it's RENEWING, we are the only server to hear it, so
511 * we have to serve it. If it's REBINDING, it's out of
512 * communication with the other server, so there's no point
513 * in waiting to serve it. However, if the lease we're
514 * offering is not a free lease, then we may be the only
515 * server that can offer it, so we can't load balance if
516 * the lease isn't in the free or backup state. If it is
517 * in the free or backup state, then that state is what
518 * mandates one server or the other should perform the
519 * allocation, not the LBA...we know the peer cannot
520 * allocate a request for an address in our free state.
521 *
522 * So our only compass is lease_mine_to_reallocate(). This
523 * effects both load balancing, and a sanity-check that we
524 * are not going to try to allocate a lease that isn't ours.
525 */
526 if ((lease -> binding_state == FTS_FREE ||
527 lease -> binding_state == FTS_BACKUP) &&
528 !lease_mine_to_reallocate (lease)) {
529 log_debug ("%s: lease owned by peer", msgbuf);
530 goto out;
531 }
532
533 /*
534 * If the lease is in a transitional state, we can't
535 * renew it unless we can rewind it to a non-transitional
536 * state (active, free, or backup). lease_mine_to_reallocate()
537 * checks for free/backup, so we only need to check for active.
538 */
539 if ((lease->binding_state == FTS_RELEASED ||
540 lease->binding_state == FTS_EXPIRED) &&
541 lease->rewind_binding_state != FTS_ACTIVE &&
542 !lease_mine_to_reallocate(lease)) {
543 log_debug("%s: lease in transition state %s", msgbuf,
544 (lease->binding_state == FTS_RELEASED)
545 ? "released" : "expired");
546 goto out;
547 }
548
549 /* It's actually very unlikely that we'll ever get here,
550 but if we do, tell the client to stop using the lease,
551 because the administrator reset it. */
552 if (lease -> binding_state == FTS_RESET &&
553 !lease_mine_to_reallocate (lease)) {
554 log_debug ("%s: lease reset by administrator", msgbuf);
555 nak_lease (packet, &cip);
556 goto out;
557 }
558
559 /* At this point it's possible that we will get a broadcast
560 DHCPREQUEST for a lease that we didn't offer, because
561 both we and the peer are in a position to offer it.
562 In that case, we probably shouldn't answer. In order
563 to not answer, we would have to compare the server
564 identifier sent by the client with the list of possible
565 server identifiers we can send, and if the client's
566 identifier isn't on the list, drop the DHCPREQUEST.
567 We aren't currently doing that for two reasons - first,
568 it's not clear that all clients do the right thing
569 with respect to sending the client identifier, which
570 could mean that we might simply not respond to a client
571 that is depending on us to respond. Secondly, we allow
572 the user to specify the server identifier to send, and
573 we don't enforce that the server identifier should be
574 one of our IP addresses. This is probably not a big
575 deal, but it's theoretically an issue.
576
577 The reason we care about this is that if both servers
578 send a DHCPACK to the DHCPREQUEST, they are then going
579 to send dueling BNDUPD messages, which could cause
580 trouble. I think it causes no harm, but it seems
581 wrong. */
582 } else
583 peer = (dhcp_failover_state_t *)0;
584 #endif
585
586 /* If a client on a given network REQUESTs a lease on an
587 address on a different network, NAK it. If the Requested
588 Address option was used, the protocol says that it must
589 have been broadcast, so we can trust the source network
590 information.
591
592 If ciaddr was specified and Requested Address was not, then
593 we really only know for sure what network a packet came from
594 if it came through a BOOTP gateway - if it came through an
595 IP router, we'll just have to assume that it's cool.
596
597 If we don't think we know where the packet came from, it
598 came through a gateway from an unknown network, so it's not
599 from a RENEWING client. If we recognize the network it
600 *thinks* it's on, we can NAK it even though we don't
601 recognize the network it's *actually* on; otherwise we just
602 have to ignore it.
603
604 We don't currently try to take advantage of access to the
605 raw packet, because it's not available on all platforms.
606 So a packet that was unicast to us through a router from a
607 RENEWING client is going to look exactly like a packet that
608 was broadcast to us from an INIT-REBOOT client.
609
610 Since we can't tell the difference between these two kinds
611 of packets, if the packet appears to have come in off the
612 local wire, we have to treat it as if it's a RENEWING
613 client. This means that we can't NAK a RENEWING client on
614 the local wire that has a bogus address. The good news is
615 that we won't ACK it either, so it should revert to INIT
616 state and send us a DHCPDISCOVER, which we *can* work with.
617
618 Because we can't detect that a RENEWING client is on the
619 wrong wire, it's going to sit there trying to renew until
620 it gets to the REBIND state, when we *can* NAK it because
621 the packet will get to us through a BOOTP gateway. We
622 shouldn't actually see DHCPREQUEST packets from RENEWING
623 clients on the wrong wire anyway, since their idea of their
624 local router will be wrong. In any case, the protocol
625 doesn't really allow us to NAK a DHCPREQUEST from a
626 RENEWING client, so we can punt on this issue. */
627
628 if (!packet -> shared_network ||
629 (packet -> raw -> ciaddr.s_addr &&
630 packet -> raw -> giaddr.s_addr) ||
631 (have_requested_addr && !packet -> raw -> ciaddr.s_addr)) {
632
633 /* If we don't know where it came from but we do know
634 where it claims to have come from, it didn't come
635 from there. */
636 if (!packet -> shared_network) {
637 if (subnet && subnet -> group -> authoritative) {
638 log_info ("%s: wrong network.", msgbuf);
639 nak_lease (packet, &cip);
640 goto out;
641 }
642 /* Otherwise, ignore it. */
643 log_info ("%s: ignored (%s).", msgbuf,
644 (subnet
645 ? "not authoritative" : "unknown subnet"));
646 goto out;
647 }
648
649 /* If we do know where it came from and it asked for an
650 address that is not on that shared network, nak it. */
651 if (subnet)
652 subnet_dereference (&subnet, MDL);
653 if (!find_grouped_subnet (&subnet, packet -> shared_network,
654 cip, MDL)) {
655 if (packet -> shared_network -> group -> authoritative)
656 {
657 log_info ("%s: wrong network.", msgbuf);
658 nak_lease (packet, &cip);
659 goto out;
660 }
661 log_info ("%s: ignored (not authoritative).", msgbuf);
662 return;
663 }
664 }
665
666 /* If the address the client asked for is ours, but it wasn't
667 available for the client, NAK it. */
668 if (!lease && ours) {
669 log_info ("%s: lease %s unavailable.", msgbuf, piaddr (cip));
670 nak_lease (packet, &cip);
671 goto out;
672 }
673
674 /* Otherwise, send the lease to the client if we found one. */
675 if (lease) {
676 ack_lease (packet, lease, DHCPACK, 0, msgbuf, ms_nulltp,
677 (struct host_decl *)0);
678 } else
679 log_info ("%s: unknown lease %s.", msgbuf, piaddr (cip));
680
681 out:
682 if (subnet)
683 subnet_dereference (&subnet, MDL);
684 if (lease)
685 lease_dereference (&lease, MDL);
686 return;
687 }
688
689 void dhcprelease (packet, ms_nulltp)
690 struct packet *packet;
691 int ms_nulltp;
692 {
693 struct lease *lease = (struct lease *)0, *next = (struct lease *)0;
694 struct iaddr cip;
695 struct option_cache *oc;
696 struct data_string data;
697 const char *s;
698 char msgbuf [1024], cstr[16]; /* XXX */
699
700
701 /* DHCPRELEASE must not specify address in requested-address
702 option, but old protocol specs weren't explicit about this,
703 so let it go. */
704 if ((oc = lookup_option (&dhcp_universe, packet -> options,
705 DHO_DHCP_REQUESTED_ADDRESS))) {
706 log_info ("DHCPRELEASE from %s specified requested-address.",
707 print_hw_addr (packet -> raw -> htype,
708 packet -> raw -> hlen,
709 packet -> raw -> chaddr));
710 }
711
712 oc = lookup_option (&dhcp_universe, packet -> options,
713 DHO_DHCP_CLIENT_IDENTIFIER);
714 memset (&data, 0, sizeof data);
715 if (oc &&
716 evaluate_option_cache (&data, packet, (struct lease *)0,
717 (struct client_state *)0,
718 packet -> options, (struct option_state *)0,
719 &global_scope, oc, MDL)) {
720 find_lease_by_uid (&lease, data.data, data.len, MDL);
721 data_string_forget (&data, MDL);
722
723 /* See if we can find a lease that matches the IP address
724 the client is claiming. */
725 while (lease) {
726 if (lease -> n_uid)
727 lease_reference (&next, lease -> n_uid, MDL);
728 if (!memcmp (&packet -> raw -> ciaddr,
729 lease -> ip_addr.iabuf, 4)) {
730 break;
731 }
732 lease_dereference (&lease, MDL);
733 if (next) {
734 lease_reference (&lease, next, MDL);
735 lease_dereference (&next, MDL);
736 }
737 }
738 if (next)
739 lease_dereference (&next, MDL);
740 }
741
742 /* The client is supposed to pass a valid client-identifier,
743 but the spec on this has changed historically, so try the
744 IP address in ciaddr if the client-identifier fails. */
745 if (!lease) {
746 cip.len = 4;
747 memcpy (cip.iabuf, &packet -> raw -> ciaddr, 4);
748 find_lease_by_ip_addr (&lease, cip, MDL);
749 }
750
751
752 /* If the hardware address doesn't match, don't do the release. */
753 if (lease &&
754 (lease -> hardware_addr.hlen != packet -> raw -> hlen + 1 ||
755 lease -> hardware_addr.hbuf [0] != packet -> raw -> htype ||
756 memcmp (&lease -> hardware_addr.hbuf [1],
757 packet -> raw -> chaddr, packet -> raw -> hlen)))
758 lease_dereference (&lease, MDL);
759
760 if (lease && lease -> client_hostname) {
761 if ((strlen (lease -> client_hostname) <= 64) &&
762 db_printable((unsigned char *)lease->client_hostname))
763 s = lease -> client_hostname;
764 else
765 s = "Hostname Unsuitable for Printing";
766 } else
767 s = (char *)0;
768
769 /* %Audit% Cannot exceed 16 bytes. %2004.06.17,Safe%
770 * We copy this out to stack because we actually want to log two
771 * inet_ntoa()'s in this message.
772 */
773 strncpy(cstr, inet_ntoa (packet -> raw -> ciaddr), 15);
774 cstr[15] = '\0';
775
776 /* %Audit% This is log output. %2004.06.17,Safe%
777 * If we truncate we hope the user can get a hint from the log.
778 */
779 snprintf (msgbuf, sizeof msgbuf,
780 "DHCPRELEASE of %s from %s %s%s%svia %s (%sfound)",
781 cstr,
782 (packet -> raw -> htype
783 ? print_hw_addr (packet -> raw -> htype,
784 packet -> raw -> hlen,
785 packet -> raw -> chaddr)
786 : (lease
787 ? print_hex_1(lease->uid_len, lease->uid, 60)
788 : "<no identifier>")),
789 s ? "(" : "", s ? s : "", s ? ") " : "",
790 packet -> raw -> giaddr.s_addr
791 ? inet_ntoa (packet -> raw -> giaddr)
792 : packet -> interface -> name,
793 lease ? "" : "not ");
794
795 #if defined (FAILOVER_PROTOCOL)
796 if (lease && lease -> pool && lease -> pool -> failover_peer) {
797 dhcp_failover_state_t *peer = lease -> pool -> failover_peer;
798 if (peer -> service_state == not_responding ||
799 peer -> service_state == service_startup) {
800 log_info ("%s: ignored%s",
801 peer -> name, peer -> nrr);
802 goto out;
803 }
804
805 /* DHCPRELEASE messages are unicast, so if the client
806 sent the DHCPRELEASE to us, it's not going to send it
807 to the peer. Not sure why this would happen, and
808 if it does happen I think we still have to change the
809 lease state, so that's what we're doing.
810 XXX See what it says in the draft about this. */
811 }
812 #endif
813
814 /* If we found a lease, release it. */
815 if (lease && lease -> ends > cur_time) {
816 release_lease (lease, packet);
817 }
818 log_info ("%s", msgbuf);
819 #if defined(FAILOVER_PROTOCOL)
820 out:
821 #endif
822 if (lease)
823 lease_dereference (&lease, MDL);
824 }
825
826 void dhcpdecline (packet, ms_nulltp)
827 struct packet *packet;
828 int ms_nulltp;
829 {
830 struct lease *lease = (struct lease *)0;
831 struct option_state *options = (struct option_state *)0;
832 int ignorep = 0;
833 int i;
834 const char *status;
835 const char *s;
836 char msgbuf [1024]; /* XXX */
837 struct iaddr cip;
838 struct option_cache *oc;
839 struct data_string data;
840
841 /* DHCPDECLINE must specify address. */
842 if (!(oc = lookup_option (&dhcp_universe, packet -> options,
843 DHO_DHCP_REQUESTED_ADDRESS)))
844 return;
845 memset (&data, 0, sizeof data);
846 if (!evaluate_option_cache (&data, packet, (struct lease *)0,
847 (struct client_state *)0,
848 packet -> options,
849 (struct option_state *)0,
850 &global_scope, oc, MDL))
851 return;
852
853 cip.len = 4;
854 memcpy (cip.iabuf, data.data, 4);
855 data_string_forget (&data, MDL);
856 find_lease_by_ip_addr (&lease, cip, MDL);
857
858 if (lease && lease -> client_hostname) {
859 if ((strlen (lease -> client_hostname) <= 64) &&
860 db_printable((unsigned char *)lease->client_hostname))
861 s = lease -> client_hostname;
862 else
863 s = "Hostname Unsuitable for Printing";
864 } else
865 s = (char *)0;
866
867 /* %Audit% This is log output. %2004.06.17,Safe%
868 * If we truncate we hope the user can get a hint from the log.
869 */
870 snprintf (msgbuf, sizeof msgbuf,
871 "DHCPDECLINE of %s from %s %s%s%svia %s",
872 piaddr (cip),
873 (packet -> raw -> htype
874 ? print_hw_addr (packet -> raw -> htype,
875 packet -> raw -> hlen,
876 packet -> raw -> chaddr)
877 : (lease
878 ? print_hex_1(lease->uid_len, lease->uid, 60)
879 : "<no identifier>")),
880 s ? "(" : "", s ? s : "", s ? ") " : "",
881 packet -> raw -> giaddr.s_addr
882 ? inet_ntoa (packet -> raw -> giaddr)
883 : packet -> interface -> name);
884
885 option_state_allocate (&options, MDL);
886
887 /* Execute statements in scope starting with the subnet scope. */
888 if (lease)
889 execute_statements_in_scope ((struct binding_value **)0,
890 packet, (struct lease *)0,
891 (struct client_state *)0,
892 packet -> options, options,
893 &global_scope,
894 lease -> subnet -> group,
895 (struct group *)0);
896
897 /* Execute statements in the class scopes. */
898 for (i = packet -> class_count; i > 0; i--) {
899 execute_statements_in_scope
900 ((struct binding_value **)0, packet, (struct lease *)0,
901 (struct client_state *)0, packet -> options, options,
902 &global_scope, packet -> classes [i - 1] -> group,
903 lease ? lease -> subnet -> group : (struct group *)0);
904 }
905
906 /* Drop the request if dhcpdeclines are being ignored. */
907 oc = lookup_option (&server_universe, options, SV_DECLINES);
908 if (!oc ||
909 evaluate_boolean_option_cache (&ignorep, packet, lease,
910 (struct client_state *)0,
911 packet -> options, options,
912 &lease -> scope, oc, MDL)) {
913 /* If we found a lease, mark it as unusable and complain. */
914 if (lease) {
915 #if defined (FAILOVER_PROTOCOL)
916 if (lease -> pool && lease -> pool -> failover_peer) {
917 dhcp_failover_state_t *peer =
918 lease -> pool -> failover_peer;
919 if (peer -> service_state == not_responding ||
920 peer -> service_state == service_startup) {
921 if (!ignorep)
922 log_info ("%s: ignored%s",
923 peer -> name, peer -> nrr);
924 goto out;
925 }
926
927 /* DHCPDECLINE messages are broadcast, so we can safely
928 ignore the DHCPDECLINE if the peer has the lease.
929 XXX Of course, at this point that information has been
930 lost. */
931 }
932 #endif
933
934 abandon_lease (lease, "declined.");
935 status = "abandoned";
936 } else {
937 status = "not found";
938 }
939 } else
940 status = "ignored";
941
942 if (!ignorep)
943 log_info ("%s: %s", msgbuf, status);
944
945 #if defined(FAILOVER_PROTOCOL)
946 out:
947 #endif
948 if (options)
949 option_state_dereference (&options, MDL);
950 if (lease)
951 lease_dereference (&lease, MDL);
952 }
953
954 void dhcpinform (packet, ms_nulltp)
955 struct packet *packet;
956 int ms_nulltp;
957 {
958 char msgbuf [1024];
959 struct data_string d1, prl;
960 struct option_cache *oc;
961 struct option_state *options = (struct option_state *)0;
962 struct dhcp_packet raw;
963 struct packet outgoing;
964 unsigned char dhcpack = DHCPACK;
965 struct subnet *subnet = NULL;
966 struct iaddr cip, gip;
967 unsigned i;
968 int nulltp;
969 struct sockaddr_in to;
970 struct in_addr from;
971 isc_boolean_t zeroed_ciaddr;
972
973 /* The client should set ciaddr to its IP address, but apparently
974 it's common for clients not to do this, so we'll use their IP
975 source address if they didn't set ciaddr. */
976 if (!packet -> raw -> ciaddr.s_addr) {
977 zeroed_ciaddr = ISC_TRUE;
978 cip.len = 4;
979 memcpy (cip.iabuf, &packet -> client_addr.iabuf, 4);
980 } else {
981 zeroed_ciaddr = ISC_FALSE;
982 cip.len = 4;
983 memcpy (cip.iabuf, &packet -> raw -> ciaddr, 4);
984 }
985
986 if (packet->raw->giaddr.s_addr) {
987 gip.len = 4;
988 memcpy(gip.iabuf, &packet->raw->giaddr, 4);
989 } else
990 gip.len = 0;
991
992 /* %Audit% This is log output. %2004.06.17,Safe%
993 * If we truncate we hope the user can get a hint from the log.
994 */
995 snprintf (msgbuf, sizeof msgbuf, "DHCPINFORM from %s via %s",
996 piaddr (cip), packet->raw->giaddr.s_addr ?
997 inet_ntoa(packet->raw->giaddr) :
998 packet -> interface -> name);
999
1000 /* If the IP source address is zero, don't respond. */
1001 if (!memcmp (cip.iabuf, "\0\0\0", 4)) {
1002 log_info ("%s: ignored (null source address).", msgbuf);
1003 return;
1004 }
1005
1006 /* Find the subnet that the client is on. */
1007 if (zeroed_ciaddr && (gip.len != 0)) {
1008 /* XXX - do subnet selection relay agent suboption here */
1009 find_subnet(&subnet, gip, MDL);
1010
1011 if (subnet == NULL) {
1012 log_info("%s: unknown subnet for relay address %s",
1013 msgbuf, piaddr(gip));
1014 return;
1015 }
1016 } else {
1017 /* XXX - do subnet selection (not relay agent) option here */
1018 find_subnet(&subnet, cip, MDL);
1019
1020 if (subnet == NULL) {
1021 log_info("%s: unknown subnet for %s address %s",
1022 msgbuf, zeroed_ciaddr ? "source" : "client",
1023 piaddr(cip));
1024 return;
1025 }
1026 }
1027
1028 /* We don't respond to DHCPINFORM packets if we're not authoritative.
1029 It would be nice if a per-host value could override this, but
1030 there's overhead involved in checking this, so let's see how people
1031 react first. */
1032 if (subnet && !subnet -> group -> authoritative) {
1033 static int eso = 0;
1034 log_info ("%s: not authoritative for subnet %s",
1035 msgbuf, piaddr (subnet -> net));
1036 if (!eso) {
1037 log_info ("If this DHCP server is authoritative for%s",
1038 " that subnet,");
1039 log_info ("please write an `authoritative;' directi%s",
1040 "ve either in the");
1041 log_info ("subnet declaration or in some scope that%s",
1042 " encloses the");
1043 log_info ("subnet declaration - for example, write %s",
1044 "it at the top");
1045 log_info ("of the dhcpd.conf file.");
1046 }
1047 if (eso++ == 100)
1048 eso = 0;
1049 subnet_dereference (&subnet, MDL);
1050 return;
1051 }
1052
1053 option_state_allocate (&options, MDL);
1054 memset (&outgoing, 0, sizeof outgoing);
1055 memset (&raw, 0, sizeof raw);
1056 outgoing.raw = &raw;
1057
1058 maybe_return_agent_options(packet, options);
1059
1060 /* Execute statements in scope starting with the subnet scope. */
1061 if (subnet)
1062 execute_statements_in_scope ((struct binding_value **)0,
1063 packet, (struct lease *)0,
1064 (struct client_state *)0,
1065 packet -> options, options,
1066 &global_scope, subnet -> group,
1067 (struct group *)0);
1068
1069 /* Execute statements in the class scopes. */
1070 for (i = packet -> class_count; i > 0; i--) {
1071 execute_statements_in_scope
1072 ((struct binding_value **)0, packet, (struct lease *)0,
1073 (struct client_state *)0, packet -> options, options,
1074 &global_scope, packet -> classes [i - 1] -> group,
1075 subnet ? subnet -> group : (struct group *)0);
1076 }
1077
1078 /* Figure out the filename. */
1079 memset (&d1, 0, sizeof d1);
1080 oc = lookup_option (&server_universe, options, SV_FILENAME);
1081 if (oc &&
1082 evaluate_option_cache (&d1, packet, (struct lease *)0,
1083 (struct client_state *)0,
1084 packet -> options, (struct option_state *)0,
1085 &global_scope, oc, MDL)) {
1086 i = d1.len;
1087 if (i > sizeof raw.file)
1088 i = sizeof raw.file;
1089 else
1090 raw.file [i] = 0;
1091 memcpy (raw.file, d1.data, i);
1092 data_string_forget (&d1, MDL);
1093 }
1094
1095 /* Choose a server name as above. */
1096 oc = lookup_option (&server_universe, options, SV_SERVER_NAME);
1097 if (oc &&
1098 evaluate_option_cache (&d1, packet, (struct lease *)0,
1099 (struct client_state *)0,
1100 packet -> options, (struct option_state *)0,
1101 &global_scope, oc, MDL)) {
1102 i = d1.len;
1103 if (i > sizeof raw.sname)
1104 i = sizeof raw.sname;
1105 else
1106 raw.sname [i] = 0;
1107 memcpy (raw.sname, d1.data, i);
1108 data_string_forget (&d1, MDL);
1109 }
1110
1111 /* Set a flag if this client is a lame Microsoft client that NUL
1112 terminates string options and expects us to do likewise. */
1113 nulltp = 0;
1114 if ((oc = lookup_option (&dhcp_universe, packet -> options,
1115 DHO_HOST_NAME))) {
1116 if (!oc->expression)
1117 nulltp = oc->flags & OPTION_HAD_NULLS;
1118 }
1119
1120 /* Put in DHCP-specific options. */
1121 i = DHO_DHCP_MESSAGE_TYPE;
1122 oc = (struct option_cache *)0;
1123 if (option_cache_allocate (&oc, MDL)) {
1124 if (make_const_data (&oc -> expression,
1125 &dhcpack, 1, 0, 0, MDL)) {
1126 option_code_hash_lookup(&oc->option,
1127 dhcp_universe.code_hash,
1128 &i, 0, MDL);
1129 save_option (&dhcp_universe, options, oc);
1130 }
1131 option_cache_dereference (&oc, MDL);
1132 }
1133
1134 get_server_source_address(&from, options, packet);
1135
1136 /* Use the subnet mask from the subnet declaration if no other
1137 mask has been provided. */
1138 i = DHO_SUBNET_MASK;
1139 if (subnet && !lookup_option (&dhcp_universe, options, i)) {
1140 oc = (struct option_cache *)0;
1141 if (option_cache_allocate (&oc, MDL)) {
1142 if (make_const_data (&oc -> expression,
1143 subnet -> netmask.iabuf,
1144 subnet -> netmask.len,
1145 0, 0, MDL)) {
1146 option_code_hash_lookup(&oc->option,
1147 dhcp_universe.code_hash,
1148 &i, 0, MDL);
1149 save_option (&dhcp_universe, options, oc);
1150 }
1151 option_cache_dereference (&oc, MDL);
1152 }
1153 }
1154
1155 /* If a site option space has been specified, use that for
1156 site option codes. */
1157 i = SV_SITE_OPTION_SPACE;
1158 if ((oc = lookup_option (&server_universe, options, i)) &&
1159 evaluate_option_cache (&d1, packet, (struct lease *)0,
1160 (struct client_state *)0,
1161 packet -> options, options,
1162 &global_scope, oc, MDL)) {
1163 struct universe *u = (struct universe *)0;
1164
1165 if (!universe_hash_lookup (&u, universe_hash,
1166 (const char *)d1.data, d1.len,
1167 MDL)) {
1168 log_error ("unknown option space %s.", d1.data);
1169 option_state_dereference (&options, MDL);
1170 if (subnet)
1171 subnet_dereference (&subnet, MDL);
1172 return;
1173 }
1174
1175 options -> site_universe = u -> index;
1176 options->site_code_min = find_min_site_code(u);
1177 data_string_forget (&d1, MDL);
1178 } else {
1179 options -> site_universe = dhcp_universe.index;
1180 options -> site_code_min = 0; /* Trust me, it works. */
1181 }
1182
1183 memset (&prl, 0, sizeof prl);
1184
1185 /* Use the parameter list from the scope if there is one. */
1186 oc = lookup_option (&dhcp_universe, options,
1187 DHO_DHCP_PARAMETER_REQUEST_LIST);
1188
1189 /* Otherwise, if the client has provided a list of options
1190 that it wishes returned, use it to prioritize. Otherwise,
1191 prioritize based on the default priority list. */
1192
1193 if (!oc)
1194 oc = lookup_option (&dhcp_universe, packet -> options,
1195 DHO_DHCP_PARAMETER_REQUEST_LIST);
1196
1197 if (oc)
1198 evaluate_option_cache (&prl, packet, (struct lease *)0,
1199 (struct client_state *)0,
1200 packet -> options, options,
1201 &global_scope, oc, MDL);
1202
1203 #ifdef DEBUG_PACKET
1204 dump_packet (packet);
1205 dump_raw ((unsigned char *)packet -> raw, packet -> packet_length);
1206 #endif
1207
1208 log_info ("%s", msgbuf);
1209
1210 /* Figure out the address of the boot file server. */
1211 if ((oc =
1212 lookup_option (&server_universe, options, SV_NEXT_SERVER))) {
1213 if (evaluate_option_cache (&d1, packet, (struct lease *)0,
1214 (struct client_state *)0,
1215 packet -> options, options,
1216 &global_scope, oc, MDL)) {
1217 /* If there was more than one answer,
1218 take the first. */
1219 if (d1.len >= 4 && d1.data)
1220 memcpy (&raw.siaddr, d1.data, 4);
1221 data_string_forget (&d1, MDL);
1222 }
1223 }
1224
1225 /* Set up the option buffer... */
1226 outgoing.packet_length =
1227 cons_options (packet, outgoing.raw, (struct lease *)0,
1228 (struct client_state *)0,
1229 0, packet -> options, options, &global_scope,
1230 0, nulltp, 0,
1231 prl.len ? &prl : (struct data_string *)0,
1232 (char *)0);
1233 option_state_dereference (&options, MDL);
1234 data_string_forget (&prl, MDL);
1235
1236 /* Make sure that the packet is at least as big as a BOOTP packet. */
1237 if (outgoing.packet_length < BOOTP_MIN_LEN)
1238 outgoing.packet_length = BOOTP_MIN_LEN;
1239
1240 raw.giaddr = packet -> raw -> giaddr;
1241 raw.ciaddr = packet -> raw -> ciaddr;
1242 memcpy (raw.chaddr, packet -> raw -> chaddr, sizeof raw.chaddr);
1243 raw.hlen = packet -> raw -> hlen;
1244 raw.htype = packet -> raw -> htype;
1245
1246 raw.xid = packet -> raw -> xid;
1247 raw.secs = packet -> raw -> secs;
1248 raw.flags = packet -> raw -> flags;
1249 raw.hops = packet -> raw -> hops;
1250 raw.op = BOOTREPLY;
1251
1252 #ifdef DEBUG_PACKET
1253 dump_packet (&outgoing);
1254 dump_raw ((unsigned char *)&raw, outgoing.packet_length);
1255 #endif
1256
1257 /* Set up the common stuff... */
1258 to.sin_family = AF_INET;
1259 #ifdef HAVE_SA_LEN
1260 to.sin_len = sizeof to;
1261 #endif
1262 memset (to.sin_zero, 0, sizeof to.sin_zero);
1263
1264 /* RFC2131 states the server SHOULD unciast to ciaddr.
1265 * There are two wrinkles - relays, and when ciaddr is zero.
1266 * There's actually no mention of relays at all in rfc2131 in
1267 * regard to DHCPINFORM, except to say we might get packets from
1268 * clients via them. Note: relays unicast to clients to the
1269 * "yiaddr" address, which servers are forbidden to set when
1270 * answering an inform.
1271 *
1272 * The solution: If ciaddr is zero, and giaddr is set, go via the
1273 * relay with the broadcast flag set to help the relay (with no
1274 * yiaddr and very likely no chaddr, it will have no idea where to
1275 * send the packet).
1276 *
1277 * If the ciaddr is zero and giaddr is not set, go via the source
1278 * IP address (but you are permitted to barf on their shoes).
1279 *
1280 * If ciaddr is not zero, send the packet there always.
1281 */
1282 if (!raw.ciaddr.s_addr && gip.len) {
1283 memcpy(&to.sin_addr, gip.iabuf, 4);
1284 to.sin_port = local_port;
1285 raw.flags |= htons(BOOTP_BROADCAST);
1286 } else {
1287 gip.len = 0;
1288 memcpy(&to.sin_addr, cip.iabuf, 4);
1289 to.sin_port = remote_port;
1290 }
1291
1292 /* Report what we're sending. */
1293 snprintf(msgbuf, sizeof msgbuf, "DHCPACK to %s (%s) via", piaddr(cip),
1294 (packet->raw->htype && packet->raw->hlen) ?
1295 print_hw_addr(packet->raw->htype, packet->raw->hlen,
1296 packet->raw->chaddr) :
1297 "<no client hardware address>");
1298 log_info("%s %s", msgbuf, gip.len ? piaddr(gip) :
1299 packet->interface->name);
1300
1301 errno = 0;
1302 send_packet ((fallback_interface
1303 ? fallback_interface : packet -> interface),
1304 &outgoing, &raw, outgoing.packet_length,
1305 from, &to, (struct hardware *)0);
1306 if (subnet)
1307 subnet_dereference (&subnet, MDL);
1308 }
1309
1310 void nak_lease (packet, cip)
1311 struct packet *packet;
1312 struct iaddr *cip;
1313 {
1314 struct sockaddr_in to;
1315 struct in_addr from;
1316 int result;
1317 struct dhcp_packet raw;
1318 unsigned char nak = DHCPNAK;
1319 struct packet outgoing;
1320 unsigned i;
1321 struct option_state *options = (struct option_state *)0;
1322 struct option_cache *oc = (struct option_cache *)0;
1323
1324 option_state_allocate (&options, MDL);
1325 memset (&outgoing, 0, sizeof outgoing);
1326 memset (&raw, 0, sizeof raw);
1327 outgoing.raw = &raw;
1328
1329 /* Set DHCP_MESSAGE_TYPE to DHCPNAK */
1330 if (!option_cache_allocate (&oc, MDL)) {
1331 log_error ("No memory for DHCPNAK message type.");
1332 option_state_dereference (&options, MDL);
1333 return;
1334 }
1335 if (!make_const_data (&oc -> expression, &nak, sizeof nak,
1336 0, 0, MDL)) {
1337 log_error ("No memory for expr_const expression.");
1338 option_cache_dereference (&oc, MDL);
1339 option_state_dereference (&options, MDL);
1340 return;
1341 }
1342 i = DHO_DHCP_MESSAGE_TYPE;
1343 option_code_hash_lookup(&oc->option, dhcp_universe.code_hash,
1344 &i, 0, MDL);
1345 save_option (&dhcp_universe, options, oc);
1346 option_cache_dereference (&oc, MDL);
1347
1348 /* Set DHCP_MESSAGE to whatever the message is */
1349 if (!option_cache_allocate (&oc, MDL)) {
1350 log_error ("No memory for DHCPNAK message type.");
1351 option_state_dereference (&options, MDL);
1352 return;
1353 }
1354 if (!make_const_data (&oc -> expression,
1355 (unsigned char *)dhcp_message,
1356 strlen (dhcp_message), 1, 0, MDL)) {
1357 log_error ("No memory for expr_const expression.");
1358 option_cache_dereference (&oc, MDL);
1359 option_state_dereference (&options, MDL);
1360 return;
1361 }
1362 i = DHO_DHCP_MESSAGE;
1363 option_code_hash_lookup(&oc->option, dhcp_universe.code_hash,
1364 &i, 0, MDL);
1365 save_option (&dhcp_universe, options, oc);
1366 option_cache_dereference (&oc, MDL);
1367
1368 get_server_source_address(&from, options, packet);
1369
1370 /* If there were agent options in the incoming packet, return
1371 * them. We do not check giaddr to detect the presence of a
1372 * relay, as this excludes "l2" relay agents which have no
1373 * giaddr to set.
1374 */
1375 if (packet->options->universe_count > agent_universe.index &&
1376 packet->options->universes [agent_universe.index]) {
1377 option_chain_head_reference
1378 ((struct option_chain_head **)
1379 &(options -> universes [agent_universe.index]),
1380 (struct option_chain_head *)
1381 packet -> options -> universes [agent_universe.index],
1382 MDL);
1383 }
1384
1385 /* Do not use the client's requested parameter list. */
1386 delete_option (&dhcp_universe, packet -> options,
1387 DHO_DHCP_PARAMETER_REQUEST_LIST);
1388
1389 /* Set up the option buffer... */
1390 outgoing.packet_length =
1391 cons_options (packet, outgoing.raw, (struct lease *)0,
1392 (struct client_state *)0,
1393 0, packet -> options, options, &global_scope,
1394 0, 0, 0, (struct data_string *)0, (char *)0);
1395 option_state_dereference (&options, MDL);
1396
1397 /* memset (&raw.ciaddr, 0, sizeof raw.ciaddr);*/
1398 if (packet->interface->address_count)
1399 raw.siaddr = packet->interface->addresses[0];
1400 raw.giaddr = packet -> raw -> giaddr;
1401 memcpy (raw.chaddr, packet -> raw -> chaddr, sizeof raw.chaddr);
1402 raw.hlen = packet -> raw -> hlen;
1403 raw.htype = packet -> raw -> htype;
1404
1405 raw.xid = packet -> raw -> xid;
1406 raw.secs = packet -> raw -> secs;
1407 raw.flags = packet -> raw -> flags | htons (BOOTP_BROADCAST);
1408 raw.hops = packet -> raw -> hops;
1409 raw.op = BOOTREPLY;
1410
1411 /* Report what we're sending... */
1412 log_info ("DHCPNAK on %s to %s via %s",
1413 piaddr (*cip),
1414 print_hw_addr (packet -> raw -> htype,
1415 packet -> raw -> hlen,
1416 packet -> raw -> chaddr),
1417 packet -> raw -> giaddr.s_addr
1418 ? inet_ntoa (packet -> raw -> giaddr)
1419 : packet -> interface -> name);
1420
1421 #ifdef DEBUG_PACKET
1422 dump_packet (packet);
1423 dump_raw ((unsigned char *)packet -> raw, packet -> packet_length);
1424 dump_packet (&outgoing);
1425 dump_raw ((unsigned char *)&raw, outgoing.packet_length);
1426 #endif
1427
1428 /* Set up the common stuff... */
1429 to.sin_family = AF_INET;
1430 #ifdef HAVE_SA_LEN
1431 to.sin_len = sizeof to;
1432 #endif
1433 memset (to.sin_zero, 0, sizeof to.sin_zero);
1434
1435 /* Make sure that the packet is at least as big as a BOOTP packet. */
1436 if (outgoing.packet_length < BOOTP_MIN_LEN)
1437 outgoing.packet_length = BOOTP_MIN_LEN;
1438
1439 /* If this was gatewayed, send it back to the gateway.
1440 Otherwise, broadcast it on the local network. */
1441 if (raw.giaddr.s_addr) {
1442 to.sin_addr = raw.giaddr;
1443 if (raw.giaddr.s_addr != htonl (INADDR_LOOPBACK))
1444 to.sin_port = local_port;
1445 else
1446 to.sin_port = remote_port; /* for testing. */
1447
1448 if (fallback_interface) {
1449 result = send_packet(fallback_interface, packet, &raw,
1450 outgoing.packet_length, from, &to,
1451 NULL);
1452 return;
1453 }
1454 } else {
1455 to.sin_addr = limited_broadcast;
1456 to.sin_port = remote_port;
1457 }
1458
1459 errno = 0;
1460 result = send_packet(packet->interface, packet, &raw,
1461 outgoing.packet_length, from, &to, NULL);
1462 }
1463
1464 void ack_lease (packet, lease, offer, when, msg, ms_nulltp, hp)
1465 struct packet *packet;
1466 struct lease *lease;
1467 unsigned int offer;
1468 TIME when;
1469 char *msg;
1470 int ms_nulltp;
1471 struct host_decl *hp;
1472 {
1473 struct lease *lt;
1474 struct lease_state *state;
1475 struct lease *next;
1476 struct host_decl *host = (struct host_decl *)0;
1477 TIME lease_time;
1478 TIME offered_lease_time;
1479 struct data_string d1;
1480 TIME min_lease_time;
1481 TIME max_lease_time;
1482 TIME default_lease_time;
1483 struct option_cache *oc;
1484 isc_result_t result;
1485 TIME ping_timeout;
1486 TIME lease_cltt;
1487 struct in_addr from;
1488 TIME remaining_time;
1489 struct iaddr cip;
1490
1491 unsigned i, j;
1492 int s1;
1493 int ignorep;
1494 struct timeval tv;
1495
1496 /* If we're already acking this lease, don't do it again. */
1497 if (lease -> state)
1498 return;
1499
1500 /* Save original cltt for comparison later. */
1501 lease_cltt = lease->cltt;
1502
1503 /* If the lease carries a host record, remember it. */
1504 if (hp)
1505 host_reference (&host, hp, MDL);
1506 else if (lease -> host)
1507 host_reference (&host, lease -> host, MDL);
1508
1509 /* Allocate a lease state structure... */
1510 state = new_lease_state (MDL);
1511 if (!state)
1512 log_fatal ("unable to allocate lease state!");
1513 state -> got_requested_address = packet -> got_requested_address;
1514 shared_network_reference (&state -> shared_network,
1515 packet -> interface -> shared_network, MDL);
1516
1517 /* See if we got a server identifier option. */
1518 if (lookup_option (&dhcp_universe,
1519 packet -> options, DHO_DHCP_SERVER_IDENTIFIER))
1520 state -> got_server_identifier = 1;
1521
1522 maybe_return_agent_options(packet, state->options);
1523
1524 /* If we are offering a lease that is still currently valid, preserve
1525 the events. We need to do this because if the client does not
1526 REQUEST our offer, it will expire in 2 minutes, overriding the
1527 expire time in the currently in force lease. We want the expire
1528 events to be executed at that point. */
1529 if (lease -> ends <= cur_time && offer != DHCPOFFER) {
1530 /* Get rid of any old expiry or release statements - by
1531 executing the statements below, we will be inserting new
1532 ones if there are any to insert. */
1533 if (lease -> on_expiry)
1534 executable_statement_dereference (&lease -> on_expiry,
1535 MDL);
1536 if (lease -> on_commit)
1537 executable_statement_dereference (&lease -> on_commit,
1538 MDL);
1539 if (lease -> on_release)
1540 executable_statement_dereference (&lease -> on_release,
1541 MDL);
1542 }
1543
1544 /* Execute statements in scope starting with the subnet scope. */
1545 execute_statements_in_scope ((struct binding_value **)0,
1546 packet, lease, (struct client_state *)0,
1547 packet -> options,
1548 state -> options, &lease -> scope,
1549 lease -> subnet -> group,
1550 (struct group *)0);
1551
1552 /* If the lease is from a pool, run the pool scope. */
1553 if (lease -> pool)
1554 (execute_statements_in_scope
1555 ((struct binding_value **)0, packet, lease,
1556 (struct client_state *)0, packet -> options,
1557 state -> options, &lease -> scope, lease -> pool -> group,
1558 lease -> pool -> shared_network -> group));
1559
1560 /* Execute statements from class scopes. */
1561 for (i = packet -> class_count; i > 0; i--) {
1562 execute_statements_in_scope
1563 ((struct binding_value **)0,
1564 packet, lease, (struct client_state *)0,
1565 packet -> options, state -> options,
1566 &lease -> scope, packet -> classes [i - 1] -> group,
1567 (lease -> pool
1568 ? lease -> pool -> group
1569 : lease -> subnet -> group));
1570 }
1571
1572 /* See if the client is only supposed to have one lease at a time,
1573 and if so, find its other leases and release them. We can only
1574 do this on DHCPREQUEST. It's a little weird to do this before
1575 looking at permissions, because the client might not actually
1576 _get_ a lease after we've done the permission check, but the
1577 assumption for this option is that the client has exactly one
1578 network interface, and will only ever remember one lease. So
1579 if it sends a DHCPREQUEST, and doesn't get the lease, it's already
1580 forgotten about its old lease, so we can too. */
1581 if (packet -> packet_type == DHCPREQUEST &&
1582 (oc = lookup_option (&server_universe, state -> options,
1583 SV_ONE_LEASE_PER_CLIENT)) &&
1584 evaluate_boolean_option_cache (&ignorep,
1585 packet, lease,
1586 (struct client_state *)0,
1587 packet -> options,
1588 state -> options, &lease -> scope,
1589 oc, MDL)) {
1590 struct lease *seek;
1591 if (lease -> uid_len) {
1592 do {
1593 seek = (struct lease *)0;
1594 find_lease_by_uid (&seek, lease -> uid,
1595 lease -> uid_len, MDL);
1596 if (!seek)
1597 break;
1598 if (seek == lease && !seek -> n_uid) {
1599 lease_dereference (&seek, MDL);
1600 break;
1601 }
1602 next = (struct lease *)0;
1603
1604 /* Don't release expired leases, and don't
1605 release the lease we're going to assign. */
1606 next = (struct lease *)0;
1607 while (seek) {
1608 if (seek -> n_uid)
1609 lease_reference (&next, seek -> n_uid, MDL);
1610 if (seek != lease &&
1611 seek -> binding_state != FTS_RELEASED &&
1612 seek -> binding_state != FTS_EXPIRED &&
1613 seek -> binding_state != FTS_RESET &&
1614 seek -> binding_state != FTS_FREE &&
1615 seek -> binding_state != FTS_BACKUP)
1616 break;
1617 lease_dereference (&seek, MDL);
1618 if (next) {
1619 lease_reference (&seek, next, MDL);
1620 lease_dereference (&next, MDL);
1621 }
1622 }
1623 if (next)
1624 lease_dereference (&next, MDL);
1625 if (seek) {
1626 release_lease (seek, packet);
1627 lease_dereference (&seek, MDL);
1628 } else
1629 break;
1630 } while (1);
1631 }
1632 if (!lease -> uid_len ||
1633 (host &&
1634 !host -> client_identifier.len &&
1635 (oc = lookup_option (&server_universe, state -> options,
1636 SV_DUPLICATES)) &&
1637 !evaluate_boolean_option_cache (&ignorep, packet, lease,
1638 (struct client_state *)0,
1639 packet -> options,
1640 state -> options,
1641 &lease -> scope,
1642 oc, MDL))) {
1643 do {
1644 seek = (struct lease *)0;
1645 find_lease_by_hw_addr
1646 (&seek, lease -> hardware_addr.hbuf,
1647 lease -> hardware_addr.hlen, MDL);
1648 if (!seek)
1649 break;
1650 if (seek == lease && !seek -> n_hw) {
1651 lease_dereference (&seek, MDL);
1652 break;
1653 }
1654 next = (struct lease *)0;
1655 while (seek) {
1656 if (seek -> n_hw)
1657 lease_reference (&next, seek -> n_hw, MDL);
1658 if (seek != lease &&
1659 seek -> binding_state != FTS_RELEASED &&
1660 seek -> binding_state != FTS_EXPIRED &&
1661 seek -> binding_state != FTS_RESET &&
1662 seek -> binding_state != FTS_FREE &&
1663 seek -> binding_state != FTS_BACKUP)
1664 break;
1665 lease_dereference (&seek, MDL);
1666 if (next) {
1667 lease_reference (&seek, next, MDL);
1668 lease_dereference (&next, MDL);
1669 }
1670 }
1671 if (next)
1672 lease_dereference (&next, MDL);
1673 if (seek) {
1674 release_lease (seek, packet);
1675 lease_dereference (&seek, MDL);
1676 } else
1677 break;
1678 } while (1);
1679 }
1680 }
1681
1682
1683 /* Make sure this packet satisfies the configured minimum
1684 number of seconds. */
1685 memset (&d1, 0, sizeof d1);
1686 if (offer == DHCPOFFER &&
1687 (oc = lookup_option (&server_universe, state -> options,
1688 SV_MIN_SECS))) {
1689 if (evaluate_option_cache (&d1, packet, lease,
1690 (struct client_state *)0,
1691 packet -> options, state -> options,
1692 &lease -> scope, oc, MDL)) {
1693 if (d1.len &&
1694 ntohs (packet -> raw -> secs) < d1.data [0]) {
1695 log_info("%s: configured min-secs value (%d) "
1696 "is greater than secs field (%d). "
1697 "message dropped.", msg, d1.data[0],
1698 ntohs(packet->raw->secs));
1699 data_string_forget (&d1, MDL);
1700 free_lease_state (state, MDL);
1701 if (host)
1702 host_dereference (&host, MDL);
1703 return;
1704 }
1705 data_string_forget (&d1, MDL);
1706 }
1707 }
1708
1709 /* Try to find a matching host declaration for this lease.
1710 */
1711 if (!host) {
1712 struct host_decl *hp = (struct host_decl *)0;
1713 struct host_decl *h;
1714
1715 /* Try to find a host_decl that matches the client
1716 identifier or hardware address on the packet, and
1717 has no fixed IP address. If there is one, hang
1718 it off the lease so that its option definitions
1719 can be used. */
1720 oc = lookup_option (&dhcp_universe, packet -> options,
1721 DHO_DHCP_CLIENT_IDENTIFIER);
1722 if (oc &&
1723 evaluate_option_cache (&d1, packet, lease,
1724 (struct client_state *)0,
1725 packet -> options, state -> options,
1726 &lease -> scope, oc, MDL)) {
1727 find_hosts_by_uid (&hp, d1.data, d1.len, MDL);
1728 data_string_forget (&d1, MDL);
1729 for (h = hp; h; h = h -> n_ipaddr) {
1730 if (!h -> fixed_addr)
1731 break;
1732 }
1733 if (h)
1734 host_reference (&host, h, MDL);
1735 if (hp != NULL)
1736 host_dereference(&hp, MDL);
1737 }
1738 if (!host) {
1739 find_hosts_by_haddr (&hp,
1740 packet -> raw -> htype,
1741 packet -> raw -> chaddr,
1742 packet -> raw -> hlen,
1743 MDL);
1744 for (h = hp; h; h = h -> n_ipaddr) {
1745 if (!h -> fixed_addr)
1746 break;
1747 }
1748 if (h)
1749 host_reference (&host, h, MDL);
1750 if (hp != NULL)
1751 host_dereference(&hp, MDL);
1752 }
1753 if (!host) {
1754 find_hosts_by_option(&hp, packet, packet->options, MDL);
1755 for (h = hp; h; h = h -> n_ipaddr) {
1756 if (!h -> fixed_addr)
1757 break;
1758 }
1759 if (h)
1760 host_reference (&host, h, MDL);
1761 if (hp != NULL)
1762 host_dereference(&hp, MDL);
1763 }
1764 }
1765
1766 /* If we have a host_decl structure, run the options associated
1767 with its group. Whether the host decl struct is old or not. */
1768 if (host)
1769 execute_statements_in_scope ((struct binding_value **)0,
1770 packet, lease,
1771 (struct client_state *)0,
1772 packet -> options,
1773 state -> options, &lease -> scope,
1774 host -> group,
1775 (lease -> pool
1776 ? lease -> pool -> group
1777 : lease -> subnet -> group));
1778
1779 /* Drop the request if it's not allowed for this client. By
1780 default, unknown clients are allowed. */
1781 if (!host &&
1782 (oc = lookup_option (&server_universe, state -> options,
1783 SV_BOOT_UNKNOWN_CLIENTS)) &&
1784 !evaluate_boolean_option_cache (&ignorep,
1785 packet, lease,
1786 (struct client_state *)0,
1787 packet -> options,
1788 state -> options,
1789 &lease -> scope, oc, MDL)) {
1790 if (!ignorep)
1791 log_info ("%s: unknown client", msg);
1792 free_lease_state (state, MDL);
1793 if (host)
1794 host_dereference (&host, MDL);
1795 return;
1796 }
1797
1798 /* Drop the request if it's not allowed for this client. */
1799 if (!offer &&
1800 (oc = lookup_option (&server_universe, state -> options,
1801 SV_ALLOW_BOOTP)) &&
1802 !evaluate_boolean_option_cache (&ignorep,
1803 packet, lease,
1804 (struct client_state *)0,
1805 packet -> options,
1806 state -> options,
1807 &lease -> scope, oc, MDL)) {
1808 if (!ignorep)
1809 log_info ("%s: bootp disallowed", msg);
1810 free_lease_state (state, MDL);
1811 if (host)
1812 host_dereference (&host, MDL);
1813 return;
1814 }
1815
1816 /* Drop the request if booting is specifically denied. */
1817 oc = lookup_option (&server_universe, state -> options,
1818 SV_ALLOW_BOOTING);
1819 if (oc &&
1820 !evaluate_boolean_option_cache (&ignorep,
1821 packet, lease,
1822 (struct client_state *)0,
1823 packet -> options,
1824 state -> options,
1825 &lease -> scope, oc, MDL)) {
1826 if (!ignorep)
1827 log_info ("%s: booting disallowed", msg);
1828 free_lease_state (state, MDL);
1829 if (host)
1830 host_dereference (&host, MDL);
1831 return;
1832 }
1833
1834 /* If we are configured to do per-class billing, do it. */
1835 if (have_billing_classes && !(lease -> flags & STATIC_LEASE)) {
1836 /* See if the lease is currently being billed to a
1837 class, and if so, whether or not it can continue to
1838 be billed to that class. */
1839 if (lease -> billing_class) {
1840 for (i = 0; i < packet -> class_count; i++)
1841 if (packet -> classes [i] ==
1842 lease -> billing_class)
1843 break;
1844 if (i == packet -> class_count)
1845 unbill_class (lease, lease -> billing_class);
1846 }
1847
1848 /* If we don't have an active billing, see if we need
1849 one, and if we do, try to do so. */
1850 if (lease->billing_class == NULL) {
1851 int bill = 0;
1852 for (i = 0; i < packet->class_count; i++) {
1853 if (packet->classes[i]->lease_limit) {
1854 bill++;
1855 if (bill_class(lease,
1856 packet->classes[i]))
1857 break;
1858 }
1859 }
1860 if (bill != 0 && i == packet->class_count) {
1861 log_info("%s: no available billing: lease "
1862 "limit reached in all matching "
1863 "classes", msg);
1864 free_lease_state(state, MDL);
1865 if (host)
1866 host_dereference(&host, MDL);
1867 return;
1868 }
1869
1870 /* If this is an offer, undo the billing. We go
1871 * through all the steps above to bill a class so
1872 * we can hit the 'no available billing' mark and
1873 * abort without offering. But it just doesn't make
1874 * sense to permanently bill a class for a non-active
1875 * lease. This means on REQUEST, we will bill this
1876 * lease again (if there is a REQUEST).
1877 */
1878 if (offer == DHCPOFFER &&
1879 lease->billing_class != NULL &&
1880 lease->binding_state != FTS_ACTIVE)
1881 unbill_class(lease, lease->billing_class);
1882 }
1883 }
1884
1885 /* Figure out the filename. */
1886 oc = lookup_option (&server_universe, state -> options, SV_FILENAME);
1887 if (oc)
1888 evaluate_option_cache (&state -> filename, packet, lease,
1889 (struct client_state *)0,
1890 packet -> options, state -> options,
1891 &lease -> scope, oc, MDL);
1892
1893 /* Choose a server name as above. */
1894 oc = lookup_option (&server_universe, state -> options,
1895 SV_SERVER_NAME);
1896 if (oc)
1897 evaluate_option_cache (&state -> server_name, packet, lease,
1898 (struct client_state *)0,
1899 packet -> options, state -> options,
1900 &lease -> scope, oc, MDL);
1901
1902 /* At this point, we have a lease that we can offer the client.
1903 Now we construct a lease structure that contains what we want,
1904 and call supersede_lease to do the right thing with it. */
1905 lt = (struct lease *)0;
1906 result = lease_allocate (&lt, MDL);
1907 if (result != ISC_R_SUCCESS) {
1908 log_info ("%s: can't allocate temporary lease structure: %s",
1909 msg, isc_result_totext (result));
1910 free_lease_state (state, MDL);
1911 if (host)
1912 host_dereference (&host, MDL);
1913 return;
1914 }
1915
1916 /* Use the ip address of the lease that we finally found in
1917 the database. */
1918 lt -> ip_addr = lease -> ip_addr;
1919
1920 /* Start now. */
1921 lt -> starts = cur_time;
1922
1923 /* Figure out how long a lease to assign. If this is a
1924 dynamic BOOTP lease, its duration must be infinite. */
1925 if (offer) {
1926 lt->flags &= ~BOOTP_LEASE;
1927
1928 default_lease_time = DEFAULT_DEFAULT_LEASE_TIME;
1929 if ((oc = lookup_option (&server_universe, state -> options,
1930 SV_DEFAULT_LEASE_TIME))) {
1931 if (evaluate_option_cache (&d1, packet, lease,
1932 (struct client_state *)0,
1933 packet -> options,
1934 state -> options,
1935 &lease -> scope, oc, MDL)) {
1936 if (d1.len == sizeof (u_int32_t))
1937 default_lease_time =
1938 getULong (d1.data);
1939 data_string_forget (&d1, MDL);
1940 }
1941 }
1942
1943 if ((oc = lookup_option (&dhcp_universe, packet -> options,
1944 DHO_DHCP_LEASE_TIME)))
1945 s1 = evaluate_option_cache (&d1, packet, lease,
1946 (struct client_state *)0,
1947 packet -> options,
1948 state -> options,
1949 &lease -> scope, oc, MDL);
1950 else
1951 s1 = 0;
1952
1953 if (s1 && (d1.len == 4)) {
1954 u_int32_t ones = 0xffffffff;
1955
1956 /* One potential use of reserved leases is to allow
1957 * clients to signal reservation of their lease. They
1958 * can kinda sorta do this, if you squint hard enough,
1959 * by supplying an 'infinite' requested-lease-time
1960 * option. This is generally bad practice...you want
1961 * clients to return to the server on at least some
1962 * period (days, months, years) to get up-to-date
1963 * config state. So;
1964 *
1965 * 1) A client requests 0xffffffff lease-time.
1966 * 2) The server reserves the lease, and assigns a
1967 * <= max_lease_time lease-time to the client, which
1968 * we presume is much smaller than 0xffffffff.
1969 * 3) The client ultimately fails to renew its lease
1970 * (all clients go offline at some point).
1971 * 4) The server retains the reservation, although
1972 * the lease expires and passes through those states
1973 * as normal, it's placed in the 'reserved' queue,
1974 * and is under no circumstances allocated to any
1975 * clients.
1976 *
1977 * Whether the client knows its reserving its lease or
1978 * not, this can be a handy tool for a sysadmin.
1979 */
1980 if ((memcmp(d1.data, &ones, 4) == 0) &&
1981 (oc = lookup_option(&server_universe,
1982 state->options,
1983 SV_RESERVE_INFINITE)) &&
1984 evaluate_boolean_option_cache(&ignorep, packet,
1985 lease, NULL, packet->options,
1986 state->options, &lease->scope,
1987 oc, MDL)) {
1988 lt->flags |= RESERVED_LEASE;
1989 if (!ignorep)
1990 log_info("Infinite-leasetime "
1991 "reservation made on %s.",
1992 piaddr(lt->ip_addr));
1993 }
1994
1995 lease_time = getULong (d1.data);
1996 } else
1997 lease_time = default_lease_time;
1998
1999 if (s1)
2000 data_string_forget(&d1, MDL);
2001
2002 /* See if there's a maximum lease time. */
2003 max_lease_time = DEFAULT_MAX_LEASE_TIME;
2004 if ((oc = lookup_option (&server_universe, state -> options,
2005 SV_MAX_LEASE_TIME))) {
2006 if (evaluate_option_cache (&d1, packet, lease,
2007 (struct client_state *)0,
2008 packet -> options,
2009 state -> options,
2010 &lease -> scope, oc, MDL)) {
2011 if (d1.len == sizeof (u_int32_t))
2012 max_lease_time =
2013 getULong (d1.data);
2014 data_string_forget (&d1, MDL);
2015 }
2016 }
2017
2018 /* Enforce the maximum lease length. */
2019 if (lease_time < 0 /* XXX */
2020 || lease_time > max_lease_time)
2021 lease_time = max_lease_time;
2022
2023 min_lease_time = DEFAULT_MIN_LEASE_TIME;
2024 if (min_lease_time > max_lease_time)
2025 min_lease_time = max_lease_time;
2026
2027 if ((oc = lookup_option (&server_universe, state -> options,
2028 SV_MIN_LEASE_TIME))) {
2029 if (evaluate_option_cache (&d1, packet, lease,
2030 (struct client_state *)0,
2031 packet -> options,
2032 state -> options,
2033 &lease -> scope, oc, MDL)) {
2034 if (d1.len == sizeof (u_int32_t))
2035 min_lease_time = getULong (d1.data);
2036 data_string_forget (&d1, MDL);
2037 }
2038 }
2039
2040 /* CC: If there are less than
2041 adaptive-lease-time-threshold % free leases,
2042 hand out only short term leases */
2043
2044 memset(&d1, 0, sizeof(d1));
2045 if (lease->pool &&
2046 (oc = lookup_option(&server_universe, state->options,
2047 SV_ADAPTIVE_LEASE_TIME_THRESHOLD)) &&
2048 evaluate_option_cache(&d1, packet, lease, NULL,
2049 packet->options, state->options,
2050 &lease->scope, oc, MDL)) {
2051 if (d1.len == 1 && d1.data[0] > 0 &&
2052 d1.data[0] < 100) {
2053 TIME adaptive_time;
2054 int poolfilled, total, count;
2055
2056 if (min_lease_time)
2057 adaptive_time = min_lease_time;
2058 else
2059 adaptive_time = DEFAULT_MIN_LEASE_TIME;
2060
2061 /* Allow the client to keep its lease. */
2062 if (lease->ends - cur_time > adaptive_time)
2063 adaptive_time = lease->ends - cur_time;
2064
2065 count = lease->pool->lease_count;
2066 total = count - (lease->pool->free_leases +
2067 lease->pool->backup_leases);
2068
2069 poolfilled = (total > (INT_MAX / 100)) ?
2070 total / (count / 100) :
2071 (total * 100) / count;
2072
2073 log_debug("Adap-lease: Total: %d, Free: %d, "
2074 "Ends: %d, Adaptive: %d, Fill: %d, "
2075 "Threshold: %d",
2076 lease->pool->lease_count,
2077 lease->pool->free_leases,
2078 (int)(lease->ends - cur_time),
2079 (int)adaptive_time, poolfilled,
2080 d1.data[0]);
2081
2082 if (poolfilled >= d1.data[0] &&
2083 lease_time > adaptive_time) {
2084 log_info("Pool over threshold, time "
2085 "for %s reduced from %d to "
2086 "%d.", piaddr(lease->ip_addr),
2087 (int)lease_time,
2088 (int)adaptive_time);
2089
2090 lease_time = adaptive_time;
2091 }
2092 }
2093 data_string_forget(&d1, MDL);
2094 }
2095
2096 /* a client requests an address which is not yet active*/
2097 if (lease->pool && lease->pool->valid_from &&
2098 cur_time < lease->pool->valid_from) {
2099 /* NAK leases before pool activation date */
2100 cip.len = 4;
2101 memcpy (cip.iabuf, &lt->ip_addr.iabuf, 4);
2102 nak_lease(packet, &cip);
2103 free_lease_state (state, MDL);
2104 lease_dereference (&lt, MDL);
2105 if (host)
2106 host_dereference (&host, MDL);
2107 return;
2108
2109 }
2110
2111 /* CC:
2112 a) NAK current lease if past the expiration date
2113 b) extend lease only up to the expiration date, but not
2114 below min-lease-time
2115 Setting min-lease-time is essential for this to work!
2116 The value of min-lease-time determines the lenght
2117 of the transition window:
2118 A client renewing a second before the deadline will
2119 get a min-lease-time lease. Since the current ip might not
2120 be routable after the deadline, the client will
2121 be offline until it DISCOVERS again. Otherwise it will
2122 receive a NAK at T/2.
2123 A min-lease-time of 6 seconds effectively switches over
2124 all clients in this pool very quickly.
2125 */
2126
2127 if (lease->pool && lease->pool->valid_until) {
2128 if (cur_time >= lease->pool->valid_until) {
2129 /* NAK leases after pool expiration date */
2130 cip.len = 4;
2131 memcpy (cip.iabuf, &lt->ip_addr.iabuf, 4);
2132 nak_lease(packet, &cip);
2133 free_lease_state (state, MDL);
2134 lease_dereference (&lt, MDL);
2135 if (host)
2136 host_dereference (&host, MDL);
2137 return;
2138 }
2139 remaining_time = lease->pool->valid_until - cur_time;
2140 if (lease_time > remaining_time)
2141 lease_time = remaining_time;
2142 }
2143
2144 if (lease_time < min_lease_time) {
2145 if (min_lease_time)
2146 lease_time = min_lease_time;
2147 else
2148 lease_time = default_lease_time;
2149 }
2150
2151
2152 #if defined (FAILOVER_PROTOCOL)
2153 /* Okay, we know the lease duration. Now check the
2154 failover state, if any. */
2155 if (lease -> pool && lease -> pool -> failover_peer) {
2156 TIME new_lease_time = lease_time;
2157 dhcp_failover_state_t *peer =
2158 lease -> pool -> failover_peer;
2159
2160 /* Copy previous lease failover ack-state. */
2161 lt->tsfp = lease->tsfp;
2162 lt->atsfp = lease->atsfp;
2163
2164 /* cltt set below */
2165
2166 /* Lease times less than MCLT are not a concern. */
2167 if (lease_time > peer->mclt) {
2168 /* Each server can only offer a lease time
2169 * that is either equal to MCLT (at least),
2170 * or up to TSFP+MCLT. Only if the desired
2171 * lease time falls within TSFP+MCLT, can
2172 * the server allow it.
2173 */
2174 if (lt->tsfp <= cur_time)
2175 new_lease_time = peer->mclt;
2176 else if ((cur_time + lease_time) >
2177 (lt->tsfp + peer->mclt))
2178 new_lease_time = (lt->tsfp - cur_time)
2179 + peer->mclt;
2180 }
2181
2182 /* Update potential expiry. Allow for the desired
2183 * lease time plus one half the actual (whether
2184 * modified downward or not) lease time, which is
2185 * actually an estimate of when the client will
2186 * renew. This way, the client will be able to get
2187 * the desired lease time upon renewal.
2188 */
2189 if (offer == DHCPACK) {
2190 lt->tstp = cur_time + lease_time +
2191 (new_lease_time / 2);
2192
2193 /* If we reduced the potential expiry time,
2194 * make sure we don't offer an old-expiry-time
2195 * lease for this lease before the change is
2196 * ack'd.
2197 */
2198 if (lt->tstp < lt->tsfp)
2199 lt->tsfp = lt->tstp;
2200 } else
2201 lt->tstp = lease->tstp;
2202
2203 /* Use failover-modified lease time. */
2204 lease_time = new_lease_time;
2205 }
2206 #endif /* FAILOVER_PROTOCOL */
2207
2208 /* If the lease duration causes the time value to wrap,
2209 use the maximum expiry time. */
2210 if (cur_time + lease_time < cur_time)
2211 state -> offered_expiry = MAX_TIME - 1;
2212 else
2213 state -> offered_expiry = cur_time + lease_time;
2214 if (when)
2215 lt -> ends = when;
2216 else
2217 lt -> ends = state -> offered_expiry;
2218
2219 /* Don't make lease active until we actually get a
2220 DHCPREQUEST. */
2221 if (offer == DHCPACK)
2222 lt -> next_binding_state = FTS_ACTIVE;
2223 else
2224 lt -> next_binding_state = lease -> binding_state;
2225 } else {
2226 lt->flags |= BOOTP_LEASE;
2227
2228 lease_time = MAX_TIME - cur_time;
2229
2230 if ((oc = lookup_option (&server_universe, state -> options,
2231 SV_BOOTP_LEASE_LENGTH))) {
2232 if (evaluate_option_cache (&d1, packet, lease,
2233 (struct client_state *)0,
2234 packet -> options,
2235 state -> options,
2236 &lease -> scope, oc, MDL)) {
2237 if (d1.len == sizeof (u_int32_t))
2238 lease_time = getULong (d1.data);
2239 data_string_forget (&d1, MDL);
2240 }
2241 }
2242
2243 if ((oc = lookup_option (&server_universe, state -> options,
2244 SV_BOOTP_LEASE_CUTOFF))) {
2245 if (evaluate_option_cache (&d1, packet, lease,
2246 (struct client_state *)0,
2247 packet -> options,
2248 state -> options,
2249 &lease -> scope, oc, MDL)) {
2250 if (d1.len == sizeof (u_int32_t))
2251 lease_time = (getULong (d1.data) -
2252 cur_time);
2253 data_string_forget (&d1, MDL);
2254 }
2255 }
2256
2257 lt -> ends = state -> offered_expiry = cur_time + lease_time;
2258 lt -> next_binding_state = FTS_ACTIVE;
2259 }
2260
2261 /* Update Client Last Transaction Time. */
2262 lt->cltt = cur_time;
2263
2264 /* Record the uid, if given... */
2265 oc = lookup_option (&dhcp_universe, packet -> options,
2266 DHO_DHCP_CLIENT_IDENTIFIER);
2267 if (oc &&
2268 evaluate_option_cache (&d1, packet, lease,
2269 (struct client_state *)0,
2270 packet -> options, state -> options,
2271 &lease -> scope, oc, MDL)) {
2272 if (d1.len <= sizeof lt -> uid_buf) {
2273 memcpy (lt -> uid_buf, d1.data, d1.len);
2274 lt -> uid = lt -> uid_buf;
2275 lt -> uid_max = sizeof lt -> uid_buf;
2276 lt -> uid_len = d1.len;
2277 } else {
2278 unsigned char *tuid;
2279 lt -> uid_max = d1.len;
2280 lt -> uid_len = d1.len;
2281 tuid = (unsigned char *)dmalloc (lt -> uid_max, MDL);
2282 /* XXX inelegant */
2283 if (!tuid)
2284 log_fatal ("no memory for large uid.");
2285 memcpy (tuid, d1.data, lt -> uid_len);
2286 lt -> uid = tuid;
2287 }
2288 data_string_forget (&d1, MDL);
2289 }
2290
2291 if (host) {
2292 host_reference (&lt -> host, host, MDL);
2293 host_dereference (&host, MDL);
2294 }
2295 if (lease -> subnet)
2296 subnet_reference (&lt -> subnet, lease -> subnet, MDL);
2297 if (lease -> billing_class)
2298 class_reference (&lt -> billing_class,
2299 lease -> billing_class, MDL);
2300
2301 /* Set a flag if this client is a broken client that NUL
2302 terminates string options and expects us to do likewise. */
2303 if (ms_nulltp)
2304 lease -> flags |= MS_NULL_TERMINATION;
2305 else
2306 lease -> flags &= ~MS_NULL_TERMINATION;
2307
2308 /* Save any bindings. */
2309 if (lease -> scope) {
2310 binding_scope_reference (&lt -> scope, lease -> scope, MDL);
2311 binding_scope_dereference (&lease -> scope, MDL);
2312 }
2313 if (lease -> agent_options)
2314 option_chain_head_reference (&lt -> agent_options,
2315 lease -> agent_options, MDL);
2316
2317 /* If we got relay agent information options from the packet, then
2318 * cache them for renewal in case the relay agent can't supply them
2319 * when the client unicasts. The options may be from an addressed
2320 * "l3" relay, or from an unaddressed "l2" relay which does not set
2321 * giaddr.
2322 */
2323 if (!packet->agent_options_stashed &&
2324 packet->options->universe_count > agent_universe.index &&
2325 packet->options->universes[agent_universe.index] != NULL) {
2326 oc = lookup_option (&server_universe, state -> options,
2327 SV_STASH_AGENT_OPTIONS);
2328 if (!oc ||
2329 evaluate_boolean_option_cache (&ignorep, packet, lease,
2330 (struct client_state *)0,
2331 packet -> options,
2332 state -> options,
2333 &lease -> scope, oc, MDL)) {
2334 if (lt -> agent_options)
2335 option_chain_head_dereference (&lt -> agent_options, MDL);
2336 option_chain_head_reference
2337 (&lt -> agent_options,
2338 (struct option_chain_head *)
2339 packet -> options -> universes [agent_universe.index],
2340 MDL);
2341 }
2342 }
2343
2344 /* Replace the old lease hostname with the new one, if it's changed. */
2345 oc = lookup_option (&dhcp_universe, packet -> options, DHO_HOST_NAME);
2346 if (oc)
2347 s1 = evaluate_option_cache (&d1, packet, (struct lease *)0,
2348 (struct client_state *)0,
2349 packet -> options,
2350 (struct option_state *)0,
2351 &global_scope, oc, MDL);
2352 else
2353 s1 = 0;
2354
2355 if (oc && s1 &&
2356 lease -> client_hostname &&
2357 strlen (lease -> client_hostname) == d1.len &&
2358 !memcmp (lease -> client_hostname, d1.data, d1.len)) {
2359 /* Hasn't changed. */
2360 data_string_forget (&d1, MDL);
2361 lt -> client_hostname = lease -> client_hostname;
2362 lease -> client_hostname = (char *)0;
2363 } else if (oc && s1) {
2364 lt -> client_hostname = dmalloc (d1.len + 1, MDL);
2365 if (!lt -> client_hostname)
2366 log_error ("no memory for client hostname.");
2367 else {
2368 memcpy (lt -> client_hostname, d1.data, d1.len);
2369 lt -> client_hostname [d1.len] = 0;
2370 }
2371 data_string_forget (&d1, MDL);
2372 }
2373
2374 /* Record the hardware address, if given... */
2375 lt -> hardware_addr.hlen = packet -> raw -> hlen + 1;
2376 lt -> hardware_addr.hbuf [0] = packet -> raw -> htype;
2377 memcpy (&lt -> hardware_addr.hbuf [1], packet -> raw -> chaddr,
2378 sizeof packet -> raw -> chaddr);
2379
2380 lt -> flags = lease -> flags & ~PERSISTENT_FLAGS;
2381
2382 /* If there are statements to execute when the lease is
2383 committed, execute them. */
2384 if (lease -> on_commit && (!offer || offer == DHCPACK)) {
2385 execute_statements ((struct binding_value **)0,
2386 packet, lt, (struct client_state *)0,
2387 packet -> options,
2388 state -> options, &lt -> scope,
2389 lease -> on_commit);
2390 if (lease -> on_commit)
2391 executable_statement_dereference (&lease -> on_commit,
2392 MDL);
2393 }
2394
2395 #ifdef NSUPDATE
2396 /* Perform DDNS updates, if configured to. */
2397 if ((!offer || offer == DHCPACK) &&
2398 (!(oc = lookup_option (&server_universe, state -> options,
2399 SV_DDNS_UPDATES)) ||
2400 evaluate_boolean_option_cache (&ignorep, packet, lt,
2401 (struct client_state *)0,
2402 packet -> options,
2403 state -> options,
2404 &lt -> scope, oc, MDL))) {
2405 ddns_updates(packet, lt, lease, NULL, NULL, state->options);
2406 }
2407 #endif /* NSUPDATE */
2408
2409 /* Don't call supersede_lease on a mocked-up lease. */
2410 if (lease -> flags & STATIC_LEASE) {
2411 /* Copy the hardware address into the static lease
2412 structure. */
2413 lease -> hardware_addr.hlen = packet -> raw -> hlen + 1;
2414 lease -> hardware_addr.hbuf [0] = packet -> raw -> htype;
2415 memcpy (&lease -> hardware_addr.hbuf [1],
2416 packet -> raw -> chaddr,
2417 sizeof packet -> raw -> chaddr); /* XXX */
2418 } else {
2419 #if !defined(DELAYED_ACK)
2420 /* Install the new information on 'lt' onto the lease at
2421 * 'lease'. If this is a DHCPOFFER, it is a 'soft' promise,
2422 * if it is a DHCPACK, it is a 'hard' binding, so it needs
2423 * to be recorded and propogated immediately. If the update
2424 * fails, don't ACK it (or BOOTREPLY) either; we may give
2425 * the same lease to another client later, and that would be
2426 * a conflict.
2427 */
2428 if (!supersede_lease(lease, lt, !offer || (offer == DHCPACK),
2429 offer == DHCPACK, offer == DHCPACK)) {
2430 #else /* defined(DELAYED_ACK) */
2431 /* Install the new information on 'lt' onto the lease at
2432 * 'lease'.  We will not 'commit' this information to disk
2433 * yet (fsync()), we will 'propogate' the information if
2434 * this is BOOTP or a DHCPACK, but we will not 'pimmediate'ly
2435 * transmit failover binding updates (this is delayed until
2436 * after the fsync()). If the update fails, don't ACK it (or
2437 * BOOTREPLY either); we may give the same lease out to a
2438 * different client, and that would be a conflict.
2439 */
2440 if (!supersede_lease(lease, lt, 0, !offer || offer == DHCPACK,
2441 0)) {
2442 #endif
2443 log_info ("%s: database update failed", msg);
2444 free_lease_state (state, MDL);
2445 lease_dereference (&lt, MDL);
2446 return;
2447 }
2448 }
2449 lease_dereference (&lt, MDL);
2450
2451 /* Remember the interface on which the packet arrived. */
2452 state -> ip = packet -> interface;
2453
2454 /* Remember the giaddr, xid, secs, flags and hops. */
2455 state -> giaddr = packet -> raw -> giaddr;
2456 state -> ciaddr = packet -> raw -> ciaddr;
2457 state -> xid = packet -> raw -> xid;
2458 state -> secs = packet -> raw -> secs;
2459 state -> bootp_flags = packet -> raw -> flags;
2460 state -> hops = packet -> raw -> hops;
2461 state -> offer = offer;
2462
2463 /* If we're always supposed to broadcast to this client, set
2464 the broadcast bit in the bootp flags field. */
2465 if ((oc = lookup_option (&server_universe, state -> options,
2466 SV_ALWAYS_BROADCAST)) &&
2467 evaluate_boolean_option_cache (&ignorep, packet, lease,
2468 (struct client_state *)0,
2469 packet -> options, state -> options,
2470 &lease -> scope, oc, MDL))
2471 state -> bootp_flags |= htons (BOOTP_BROADCAST);
2472
2473 /* Get the Maximum Message Size option from the packet, if one
2474 was sent. */
2475 oc = lookup_option (&dhcp_universe, packet -> options,
2476 DHO_DHCP_MAX_MESSAGE_SIZE);
2477 if (oc &&
2478 evaluate_option_cache (&d1, packet, lease,
2479 (struct client_state *)0,
2480 packet -> options, state -> options,
2481 &lease -> scope, oc, MDL)) {
2482 if (d1.len == sizeof (u_int16_t))
2483 state -> max_message_size = getUShort (d1.data);
2484 data_string_forget (&d1, MDL);
2485 } else {
2486 oc = lookup_option (&dhcp_universe, state -> options,
2487 DHO_DHCP_MAX_MESSAGE_SIZE);
2488 if (oc &&
2489 evaluate_option_cache (&d1, packet, lease,
2490 (struct client_state *)0,
2491 packet -> options, state -> options,
2492 &lease -> scope, oc, MDL)) {
2493 if (d1.len == sizeof (u_int16_t))
2494 state -> max_message_size =
2495 getUShort (d1.data);
2496 data_string_forget (&d1, MDL);
2497 }
2498 }
2499
2500 /* Get the Subnet Selection option from the packet, if one
2501 was sent. */
2502 if ((oc = lookup_option (&dhcp_universe, packet -> options,
2503 DHO_SUBNET_SELECTION))) {
2504
2505 /* Make a copy of the data. */
2506 struct option_cache *noc = (struct option_cache *)0;
2507 if (option_cache_allocate (&noc, MDL)) {
2508 if (oc -> data.len)
2509 data_string_copy (&noc -> data,
2510 &oc -> data, MDL);
2511 if (oc -> expression)
2512 expression_reference (&noc -> expression,
2513 oc -> expression, MDL);
2514 if (oc -> option)
2515 option_reference(&(noc->option), oc->option,
2516 MDL);
2517 }
2518
2519 save_option (&dhcp_universe, state -> options, noc);
2520 option_cache_dereference (&noc, MDL);
2521 }
2522
2523 /* Now, if appropriate, put in DHCP-specific options that
2524 override those. */
2525 if (state -> offer) {
2526 i = DHO_DHCP_MESSAGE_TYPE;
2527 oc = (struct option_cache *)0;
2528 if (option_cache_allocate (&oc, MDL)) {
2529 if (make_const_data (&oc -> expression,
2530 &state -> offer, 1, 0, 0, MDL)) {
2531 option_code_hash_lookup(&oc->option,
2532 dhcp_universe.code_hash,
2533 &i, 0, MDL);
2534 save_option (&dhcp_universe,
2535 state -> options, oc);
2536 }
2537 option_cache_dereference (&oc, MDL);
2538 }
2539
2540 get_server_source_address(&from, state->options, packet);
2541 memcpy(state->from.iabuf, &from, sizeof(from));
2542 state->from.len = sizeof(from);
2543
2544 offered_lease_time =
2545 state -> offered_expiry - cur_time;
2546
2547 putULong(state->expiry, (u_int32_t)offered_lease_time);
2548 i = DHO_DHCP_LEASE_TIME;
2549 oc = (struct option_cache *)0;
2550 if (option_cache_allocate (&oc, MDL)) {
2551 if (make_const_data(&oc->expression, state->expiry,
2552 4, 0, 0, MDL)) {
2553 option_code_hash_lookup(&oc->option,
2554 dhcp_universe.code_hash,
2555 &i, 0, MDL);
2556 save_option (&dhcp_universe,
2557 state -> options, oc);
2558 }
2559 option_cache_dereference (&oc, MDL);
2560 }
2561
2562 /*
2563 * Validate any configured renew or rebinding times against
2564 * the determined lease time. Do rebinding first so that
2565 * the renew time can be validated against the rebind time.
2566 */
2567 if ((oc = lookup_option(&dhcp_universe, state->options,
2568 DHO_DHCP_REBINDING_TIME)) != NULL &&
2569 evaluate_option_cache(&d1, packet, lease, NULL,
2570 packet->options, state->options,
2571 &lease->scope, oc, MDL)) {
2572 TIME rebind_time = getULong(d1.data);
2573
2574 /* Drop the configured (invalid) rebinding time. */
2575 if (rebind_time >= offered_lease_time)
2576 delete_option(&dhcp_universe, state->options,
2577 DHO_DHCP_REBINDING_TIME);
2578 else /* XXX: variable is reused. */
2579 offered_lease_time = rebind_time;
2580
2581 data_string_forget(&d1, MDL);
2582 }
2583
2584 if ((oc = lookup_option(&dhcp_universe, state->options,
2585 DHO_DHCP_RENEWAL_TIME)) != NULL &&
2586 evaluate_option_cache(&d1, packet, lease, NULL,
2587 packet->options, state->options,
2588 &lease->scope, oc, MDL)) {
2589 if (getULong(d1.data) >= offered_lease_time)
2590 delete_option(&dhcp_universe, state->options,
2591 DHO_DHCP_RENEWAL_TIME);
2592
2593 data_string_forget(&d1, MDL);
2594 }
2595 } else {
2596 /* XXXSK: should we use get_server_source_address() here? */
2597 if (state -> ip -> address_count) {
2598 state -> from.len =
2599 sizeof state -> ip -> addresses [0];
2600 memcpy (state -> from.iabuf,
2601 &state -> ip -> addresses [0],
2602 state -> from.len);
2603 }
2604 }
2605
2606 /* Figure out the address of the boot file server. */
2607 memset (&state -> siaddr, 0, sizeof state -> siaddr);
2608 if ((oc =
2609 lookup_option (&server_universe,
2610 state -> options, SV_NEXT_SERVER))) {
2611 if (evaluate_option_cache (&d1, packet, lease,
2612 (struct client_state *)0,
2613 packet -> options, state -> options,
2614 &lease -> scope, oc, MDL)) {
2615 /* If there was more than one answer,
2616 take the first. */
2617 if (d1.len >= 4 && d1.data)
2618 memcpy (&state -> siaddr, d1.data, 4);
2619 data_string_forget (&d1, MDL);
2620 }
2621 }
2622
2623 /* Use the subnet mask from the subnet declaration if no other
2624 mask has been provided. */
2625 i = DHO_SUBNET_MASK;
2626 if (!lookup_option (&dhcp_universe, state -> options, i)) {
2627 oc = (struct option_cache *)0;
2628 if (option_cache_allocate (&oc, MDL)) {
2629 if (make_const_data (&oc -> expression,
2630 lease -> subnet -> netmask.iabuf,
2631 lease -> subnet -> netmask.len,
2632 0, 0, MDL)) {
2633 option_code_hash_lookup(&oc->option,
2634 dhcp_universe.code_hash,
2635 &i, 0, MDL);
2636 save_option (&dhcp_universe,
2637 state -> options, oc);
2638 }
2639 option_cache_dereference (&oc, MDL);
2640 }
2641 }
2642
2643 /* Use the hostname from the host declaration if there is one
2644 and no hostname has otherwise been provided, and if the
2645 use-host-decl-name flag is set. */
2646 i = DHO_HOST_NAME;
2647 j = SV_USE_HOST_DECL_NAMES;
2648 if (!lookup_option (&dhcp_universe, state -> options, i) &&
2649 lease -> host && lease -> host -> name &&
2650 (evaluate_boolean_option_cache
2651 (&ignorep, packet, lease, (struct client_state *)0,
2652 packet -> options, state -> options, &lease -> scope,
2653 lookup_option (&server_universe, state -> options, j), MDL))) {
2654 oc = (struct option_cache *)0;
2655 if (option_cache_allocate (&oc, MDL)) {
2656 if (make_const_data (&oc -> expression,
2657 ((unsigned char *)
2658 lease -> host -> name),
2659 strlen (lease -> host -> name),
2660 1, 0, MDL)) {
2661 option_code_hash_lookup(&oc->option,
2662 dhcp_universe.code_hash,
2663 &i, 0, MDL);
2664 save_option (&dhcp_universe,
2665 state -> options, oc);
2666 }
2667 option_cache_dereference (&oc, MDL);
2668 }
2669 }
2670
2671 /* If we don't have a hostname yet, and we've been asked to do
2672 a reverse lookup to find the hostname, do it. */
2673 j = SV_GET_LEASE_HOSTNAMES;
2674 if (!lookup_option (&server_universe, state -> options, i) &&
2675 (evaluate_boolean_option_cache
2676 (&ignorep, packet, lease, (struct client_state *)0,
2677 packet -> options, state -> options, &lease -> scope,
2678 lookup_option (&server_universe, state -> options, j), MDL))) {
2679 struct in_addr ia;
2680 struct hostent *h;
2681
2682 memcpy (&ia, lease -> ip_addr.iabuf, 4);
2683
2684 h = gethostbyaddr ((char *)&ia, sizeof ia, AF_INET);
2685 if (!h)
2686 log_error ("No hostname for %s", inet_ntoa (ia));
2687 else {
2688 oc = (struct option_cache *)0;
2689 if (option_cache_allocate (&oc, MDL)) {
2690 if (make_const_data (&oc -> expression,
2691 ((unsigned char *)
2692 h -> h_name),
2693 strlen (h -> h_name) + 1,
2694 1, 1, MDL)) {
2695 option_code_hash_lookup(&oc->option,
2696 dhcp_universe.code_hash,
2697 &i, 0, MDL);
2698 save_option (&dhcp_universe,
2699 state -> options, oc);
2700 }
2701 option_cache_dereference (&oc, MDL);
2702 }
2703 }
2704 }
2705
2706 /* If so directed, use the leased IP address as the router address.
2707 This supposedly makes Win95 machines ARP for all IP addresses,
2708 so if the local router does proxy arp, you win. */
2709
2710 if (evaluate_boolean_option_cache
2711 (&ignorep, packet, lease, (struct client_state *)0,
2712 packet -> options, state -> options, &lease -> scope,
2713 lookup_option (&server_universe, state -> options,
2714 SV_USE_LEASE_ADDR_FOR_DEFAULT_ROUTE), MDL)) {
2715 i = DHO_ROUTERS;
2716 oc = lookup_option (&dhcp_universe, state -> options, i);
2717 if (!oc) {
2718 oc = (struct option_cache *)0;
2719 if (option_cache_allocate (&oc, MDL)) {
2720 if (make_const_data (&oc -> expression,
2721 lease -> ip_addr.iabuf,
2722 lease -> ip_addr.len,
2723 0, 0, MDL)) {
2724 option_code_hash_lookup(&oc->option,
2725 dhcp_universe.code_hash,
2726 &i, 0, MDL);
2727 save_option (&dhcp_universe,
2728 state -> options, oc);
2729 }
2730 option_cache_dereference (&oc, MDL);
2731 }
2732 }
2733 }
2734
2735 /* If a site option space has been specified, use that for
2736 site option codes. */
2737 i = SV_SITE_OPTION_SPACE;
2738 if ((oc = lookup_option (&server_universe, state -> options, i)) &&
2739 evaluate_option_cache (&d1, packet, lease,
2740 (struct client_state *)0,
2741 packet -> options, state -> options,
2742 &lease -> scope, oc, MDL)) {
2743 struct universe *u = (struct universe *)0;
2744
2745 if (!universe_hash_lookup (&u, universe_hash,
2746 (const char *)d1.data, d1.len,
2747 MDL)) {
2748 log_error ("unknown option space %s.", d1.data);
2749 return;
2750 }
2751
2752 state -> options -> site_universe = u -> index;
2753 state->options->site_code_min = find_min_site_code(u);
2754 data_string_forget (&d1, MDL);
2755 } else {
2756 state -> options -> site_code_min = 0;
2757 state -> options -> site_universe = dhcp_universe.index;
2758 }
2759
2760 /* If the client has provided a list of options that it wishes
2761 returned, use it to prioritize. If there's a parameter
2762 request list in scope, use that in preference. Otherwise
2763 use the default priority list. */
2764
2765 oc = lookup_option (&dhcp_universe, state -> options,
2766 DHO_DHCP_PARAMETER_REQUEST_LIST);
2767
2768 if (!oc)
2769 oc = lookup_option (&dhcp_universe, packet -> options,
2770 DHO_DHCP_PARAMETER_REQUEST_LIST);
2771 if (oc)
2772 evaluate_option_cache (&state -> parameter_request_list,
2773 packet, lease, (struct client_state *)0,
2774 packet -> options, state -> options,
2775 &lease -> scope, oc, MDL);
2776
2777 #ifdef DEBUG_PACKET
2778 dump_packet (packet);
2779 dump_raw ((unsigned char *)packet -> raw, packet -> packet_length);
2780 #endif
2781
2782 lease -> state = state;
2783
2784 log_info ("%s", msg);
2785
2786 /* Hang the packet off the lease state. */
2787 packet_reference (&lease -> state -> packet, packet, MDL);
2788
2789 /* If this is a DHCPOFFER, ping the lease address before actually
2790 sending the offer. */
2791 if (offer == DHCPOFFER && !(lease -> flags & STATIC_LEASE) &&
2792 ((cur_time - lease_cltt) > 60) &&
2793 (!(oc = lookup_option (&server_universe, state -> options,
2794 SV_PING_CHECKS)) ||
2795 evaluate_boolean_option_cache (&ignorep, packet, lease,
2796 (struct client_state *)0,
2797 packet -> options,
2798 state -> options,
2799 &lease -> scope, oc, MDL))) {
2800 icmp_echorequest (&lease -> ip_addr);
2801
2802 /* Determine whether to use configured or default ping timeout.
2803 */
2804 if ((oc = lookup_option (&server_universe, state -> options,
2805 SV_PING_TIMEOUT)) &&
2806 evaluate_option_cache (&d1, packet, lease, NULL,
2807 packet -> options,
2808 state -> options,
2809 &lease -> scope, oc, MDL)) {
2810 if (d1.len == sizeof (u_int32_t))
2811 ping_timeout = getULong (d1.data);
2812 else
2813 ping_timeout = DEFAULT_PING_TIMEOUT;
2814
2815 data_string_forget (&d1, MDL);
2816 } else
2817 ping_timeout = DEFAULT_PING_TIMEOUT;
2818
2819 #ifdef DEBUG
2820 log_debug ("Ping timeout: %ld", (long)ping_timeout);
2821 #endif
2822
2823 tv . tv_sec = cur_time + ping_timeout;
2824 tv . tv_usec = 0;
2825 add_timeout (&tv, lease_ping_timeout, lease,
2826 (tvref_t)lease_reference,
2827 (tvunref_t)lease_dereference);
2828 ++outstanding_pings;
2829 } else {
2830 lease->cltt = cur_time;
2831 #if defined(DELAYED_ACK)
2832 if (!(lease->flags & STATIC_LEASE) &&
2833 (!offer || (offer == DHCPACK)))
2834 delayed_ack_enqueue(lease);
2835 else
2836 #endif
2837 dhcp_reply(lease);
2838 }
2839 }
2840
2841 /*
2842 * CC: queue single ACK:
2843 * - write the lease (but do not fsync it yet)
2844 * - add to double linked list
2845 * - commit if more than xx ACKs pending
2846 * - if necessary set the max timer and bump the next timer
2847 * but only up to the max timer value.
2848 */
2849
2850 void
2851 delayed_ack_enqueue(struct lease *lease)
2852 {
2853 struct leasequeue *q;
2854
2855 if (!write_lease(lease))
2856 return;
2857 if (free_ackqueue) {
2858 q = free_ackqueue;
2859 free_ackqueue = q->next;
2860 } else {
2861 q = ((struct leasequeue *)
2862 dmalloc(sizeof(struct leasequeue), MDL));
2863 if (!q)
2864 log_fatal("delayed_ack_enqueue: no memory!");
2865 }
2866 memset(q, 0, sizeof *q);
2867 /* prepend to ackqueue*/
2868 lease_reference(&q->lease, lease, MDL);
2869 q->next = ackqueue_head;
2870 ackqueue_head = q;
2871 if (!ackqueue_tail)
2872 ackqueue_tail = q;
2873 else
2874 q->next->prev = q;
2875
2876 outstanding_acks++;
2877 if (outstanding_acks > max_outstanding_acks) {
2878 commit_leases();
2879
2880 /* Reset max_fsync and cancel any pending timeout. */
2881 memset(&max_fsync, 0, sizeof(max_fsync));
2882 cancel_timeout(commit_leases_ackout, NULL);
2883 } else {
2884 struct timeval next_fsync;
2885
2886 if (max_fsync.tv_sec == 0 && max_fsync.tv_usec == 0) {
2887 /* set the maximum time we'll wait */
2888 max_fsync.tv_sec = cur_tv.tv_sec + max_ack_delay_secs;
2889 max_fsync.tv_usec = cur_tv.tv_usec +
2890 max_ack_delay_usecs;
2891
2892 if (max_fsync.tv_usec >= 1000000) {
2893 max_fsync.tv_sec++;
2894 max_fsync.tv_usec -= 1000000;
2895 }
2896 }
2897
2898 /* Set the timeout */
2899 next_fsync.tv_sec = cur_tv.tv_sec;
2900 next_fsync.tv_usec = cur_tv.tv_usec + min_ack_delay_usecs;
2901 if (next_fsync.tv_usec >= 1000000) {
2902 next_fsync.tv_sec++;
2903 next_fsync.tv_usec -= 1000000;
2904 }
2905 /* but not more than the max */
2906 if ((next_fsync.tv_sec > max_fsync.tv_sec) ||
2907 ((next_fsync.tv_sec == max_fsync.tv_sec) &&
2908 (next_fsync.tv_usec > max_fsync.tv_usec))) {
2909 next_fsync.tv_sec = max_fsync.tv_sec;
2910 next_fsync.tv_usec = max_fsync.tv_usec;
2911 }
2912
2913 add_timeout(&next_fsync, commit_leases_ackout, NULL,
2914 (tvref_t) NULL, (tvunref_t) NULL);
2915 }
2916 }
2917
2918 static void
2919 commit_leases_ackout(void *foo)
2920 {
2921 if (outstanding_acks) {
2922 commit_leases();
2923
2924 memset(&max_fsync, 0, sizeof(max_fsync));
2925 }
2926 }
2927
2928 /* CC: process the delayed ACK responses:
2929 - send out the ACK packets
2930 - move the queue slots to the free list
2931 */
2932 void
2933 flush_ackqueue(void *foo)
2934 {
2935 struct leasequeue *ack, *p;
2936 /* process from bottom to retain packet order */
2937 for (ack = ackqueue_tail ; ack ; ack = p) {
2938 p = ack->prev;
2939
2940 /* dhcp_reply() requires that the reply state still be valid */
2941 if (ack->lease->state == NULL)
2942 log_error("delayed ack for %s has gone stale",
2943 piaddr(ack->lease->ip_addr));
2944 else
2945 dhcp_reply(ack->lease);
2946
2947 lease_dereference(&ack->lease, MDL);
2948 ack->next = free_ackqueue;
2949 free_ackqueue = ack;
2950 }
2951 ackqueue_head = NULL;
2952 ackqueue_tail = NULL;
2953 outstanding_acks = 0;
2954 }
2955
2956 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
2957 void
2958 relinquish_ackqueue(void)
2959 {
2960 struct leasequeue *q, *n;
2961
2962 for (q = ackqueue_head ; q ; q = n) {
2963 n = q->next;
2964 dfree(q, MDL);
2965 }
2966 for (q = free_ackqueue ; q ; q = n) {
2967 n = q->next;
2968 dfree(q, MDL);
2969 }
2970 }
2971 #endif
2972
2973 void dhcp_reply (lease)
2974 struct lease *lease;
2975 {
2976 int bufs = 0;
2977 unsigned packet_length;
2978 struct dhcp_packet raw;
2979 struct sockaddr_in to;
2980 struct in_addr from;
2981 struct hardware hto;
2982 int result;
2983 struct lease_state *state = lease -> state;
2984 int nulltp, bootpp, unicastp = 1;
2985 struct data_string d1;
2986 const char *s;
2987
2988 if (!state)
2989 log_fatal ("dhcp_reply was supplied lease with no state!");
2990
2991 /* Compose a response for the client... */
2992 memset (&raw, 0, sizeof raw);
2993 memset (&d1, 0, sizeof d1);
2994
2995 /* Copy in the filename if given; otherwise, flag the filename
2996 buffer as available for options. */
2997 if (state -> filename.len && state -> filename.data) {
2998 memcpy (raw.file,
2999 state -> filename.data,
3000 state -> filename.len > sizeof raw.file
3001 ? sizeof raw.file : state -> filename.len);
3002 if (sizeof raw.file > state -> filename.len)
3003 memset (&raw.file [state -> filename.len], 0,
3004 (sizeof raw.file) - state -> filename.len);
3005 } else
3006 bufs |= 1;
3007
3008 /* Copy in the server name if given; otherwise, flag the
3009 server_name buffer as available for options. */
3010 if (state -> server_name.len && state -> server_name.data) {
3011 memcpy (raw.sname,
3012 state -> server_name.data,
3013 state -> server_name.len > sizeof raw.sname
3014 ? sizeof raw.sname : state -> server_name.len);
3015 if (sizeof raw.sname > state -> server_name.len)
3016 memset (&raw.sname [state -> server_name.len], 0,
3017 (sizeof raw.sname) - state -> server_name.len);
3018 } else
3019 bufs |= 2; /* XXX */
3020
3021 memcpy (raw.chaddr,
3022 &lease -> hardware_addr.hbuf [1], sizeof raw.chaddr);
3023 raw.hlen = lease -> hardware_addr.hlen - 1;
3024 raw.htype = lease -> hardware_addr.hbuf [0];
3025
3026 /* See if this is a Microsoft client that NUL-terminates its
3027 strings and expects us to do likewise... */
3028 if (lease -> flags & MS_NULL_TERMINATION)
3029 nulltp = 1;
3030 else
3031 nulltp = 0;
3032
3033 /* See if this is a bootp client... */
3034 if (state -> offer)
3035 bootpp = 0;
3036 else
3037 bootpp = 1;
3038
3039 /* Insert such options as will fit into the buffer. */
3040 packet_length = cons_options (state -> packet, &raw, lease,
3041 (struct client_state *)0,
3042 state -> max_message_size,
3043 state -> packet -> options,
3044 state -> options, &global_scope,
3045 bufs, nulltp, bootpp,
3046 &state -> parameter_request_list,
3047 (char *)0);
3048
3049 memcpy (&raw.ciaddr, &state -> ciaddr, sizeof raw.ciaddr);
3050 memcpy (&raw.yiaddr, lease -> ip_addr.iabuf, 4);
3051 raw.siaddr = state -> siaddr;
3052 raw.giaddr = state -> giaddr;
3053
3054 raw.xid = state -> xid;
3055 raw.secs = state -> secs;
3056 raw.flags = state -> bootp_flags;
3057 raw.hops = state -> hops;
3058 raw.op = BOOTREPLY;
3059
3060 if (lease -> client_hostname) {
3061 if ((strlen (lease -> client_hostname) <= 64) &&
3062 db_printable((unsigned char *)lease->client_hostname))
3063 s = lease -> client_hostname;
3064 else
3065 s = "Hostname Unsuitable for Printing";
3066 } else
3067 s = (char *)0;
3068
3069 /* Say what we're doing... */
3070 log_info ("%s on %s to %s %s%s%svia %s",
3071 (state -> offer
3072 ? (state -> offer == DHCPACK ? "DHCPACK" : "DHCPOFFER")
3073 : "BOOTREPLY"),
3074 piaddr (lease -> ip_addr),
3075 (lease -> hardware_addr.hlen
3076 ? print_hw_addr (lease -> hardware_addr.hbuf [0],
3077 lease -> hardware_addr.hlen - 1,
3078 &lease -> hardware_addr.hbuf [1])
3079 : print_hex_1(lease->uid_len, lease->uid, 60)),
3080 s ? "(" : "", s ? s : "", s ? ") " : "",
3081 (state -> giaddr.s_addr
3082 ? inet_ntoa (state -> giaddr)
3083 : state -> ip -> name));
3084
3085 /* Set up the hardware address... */
3086 hto.hlen = lease -> hardware_addr.hlen;
3087 memcpy (hto.hbuf, lease -> hardware_addr.hbuf, hto.hlen);
3088
3089 to.sin_family = AF_INET;
3090 #ifdef HAVE_SA_LEN
3091 to.sin_len = sizeof to;
3092 #endif
3093 memset (to.sin_zero, 0, sizeof to.sin_zero);
3094
3095 #ifdef DEBUG_PACKET
3096 dump_raw ((unsigned char *)&raw, packet_length);
3097 #endif
3098
3099 /* Make sure outgoing packets are at least as big
3100 as a BOOTP packet. */
3101 if (packet_length < BOOTP_MIN_LEN)
3102 packet_length = BOOTP_MIN_LEN;
3103
3104 /* If this was gatewayed, send it back to the gateway... */
3105 if (raw.giaddr.s_addr) {
3106 to.sin_addr = raw.giaddr;
3107 if (raw.giaddr.s_addr != htonl (INADDR_LOOPBACK))
3108 to.sin_port = local_port;
3109 else
3110 to.sin_port = remote_port; /* For debugging. */
3111
3112 if (fallback_interface) {
3113 result = send_packet (fallback_interface,
3114 (struct packet *)0,
3115 &raw, packet_length,
3116 raw.siaddr, &to,
3117 (struct hardware *)0);
3118
3119 free_lease_state (state, MDL);
3120 lease -> state = (struct lease_state *)0;
3121 return;
3122 }
3123
3124 /* If the client is RENEWING, unicast to the client using the
3125 regular IP stack. Some clients, particularly those that
3126 follow RFC1541, are buggy, and send both ciaddr and server
3127 identifier. We deal with this situation by assuming that
3128 if we got both dhcp-server-identifier and ciaddr, and
3129 giaddr was not set, then the client is on the local
3130 network, and we can therefore unicast or broadcast to it
3131 successfully. A client in REQUESTING state on another
3132 network that's making this mistake will have set giaddr,
3133 and will therefore get a relayed response from the above
3134 code. */
3135 } else if (raw.ciaddr.s_addr &&
3136 !((state -> got_server_identifier ||
3137 (raw.flags & htons (BOOTP_BROADCAST))) &&
3138 /* XXX This won't work if giaddr isn't zero, but it is: */
3139 (state -> shared_network ==
3140 lease -> subnet -> shared_network)) &&
3141 state -> offer == DHCPACK) {
3142 to.sin_addr = raw.ciaddr;
3143 to.sin_port = remote_port;
3144
3145 if (fallback_interface) {
3146 result = send_packet (fallback_interface,
3147 (struct packet *)0,
3148 &raw, packet_length,
3149 raw.siaddr, &to,
3150 (struct hardware *)0);
3151 free_lease_state (state, MDL);
3152 lease -> state = (struct lease_state *)0;
3153 return;
3154 }
3155
3156 /* If it comes from a client that already knows its address
3157 and is not requesting a broadcast response, and we can
3158 unicast to a client without using the ARP protocol, sent it
3159 directly to that client. */
3160 } else if (!(raw.flags & htons (BOOTP_BROADCAST)) &&
3161 can_unicast_without_arp (state -> ip)) {
3162 to.sin_addr = raw.yiaddr;
3163 to.sin_port = remote_port;
3164
3165 /* Otherwise, broadcast it on the local network. */
3166 } else {
3167 to.sin_addr = limited_broadcast;
3168 to.sin_port = remote_port;
3169 if (!(lease -> flags & UNICAST_BROADCAST_HACK))
3170 unicastp = 0;
3171 }
3172
3173 memcpy (&from, state -> from.iabuf, sizeof from);
3174
3175 result = send_packet (state -> ip,
3176 (struct packet *)0, &raw, packet_length,
3177 from, &to,
3178 unicastp ? &hto : (struct hardware *)0);
3179
3180 /* Free all of the entries in the option_state structure
3181 now that we're done with them. */
3182
3183 free_lease_state (state, MDL);
3184 lease -> state = (struct lease_state *)0;
3185 }
3186
3187 int find_lease (struct lease **lp,
3188 struct packet *packet, struct shared_network *share, int *ours,
3189 int *peer_has_leases, struct lease *ip_lease_in,
3190 const char *file, int line)
3191 {
3192 struct lease *uid_lease = (struct lease *)0;
3193 struct lease *ip_lease = (struct lease *)0;
3194 struct lease *hw_lease = (struct lease *)0;
3195 struct lease *lease = (struct lease *)0;
3196 struct iaddr cip;
3197 struct host_decl *hp = (struct host_decl *)0;
3198 struct host_decl *host = (struct host_decl *)0;
3199 struct lease *fixed_lease = (struct lease *)0;
3200 struct lease *next = (struct lease *)0;
3201 struct option_cache *oc;
3202 struct data_string d1;
3203 int have_client_identifier = 0;
3204 struct data_string client_identifier;
3205 struct hardware h;
3206
3207 #if defined(FAILOVER_PROTOCOL)
3208 /* Quick check to see if the peer has leases. */
3209 if (peer_has_leases) {
3210 struct pool *pool;
3211
3212 for (pool = share->pools ; pool ; pool = pool->next) {
3213 dhcp_failover_state_t *peer = pool->failover_peer;
3214
3215 if (peer &&
3216 ((peer->i_am == primary && pool->backup_leases) ||
3217 (peer->i_am == secondary && pool->free_leases))) {
3218 *peer_has_leases = 1;
3219 break;
3220 }
3221 }
3222 }
3223 #endif /* FAILOVER_PROTOCOL */
3224
3225 if (packet -> raw -> ciaddr.s_addr) {
3226 cip.len = 4;
3227 memcpy (cip.iabuf, &packet -> raw -> ciaddr, 4);
3228 } else {
3229 /* Look up the requested address. */
3230 oc = lookup_option (&dhcp_universe, packet -> options,
3231 DHO_DHCP_REQUESTED_ADDRESS);
3232 memset (&d1, 0, sizeof d1);
3233 if (oc &&
3234 evaluate_option_cache (&d1, packet, (struct lease *)0,
3235 (struct client_state *)0,
3236 packet -> options,
3237 (struct option_state *)0,
3238 &global_scope, oc, MDL)) {
3239 packet -> got_requested_address = 1;
3240 cip.len = 4;
3241 memcpy (cip.iabuf, d1.data, cip.len);
3242 data_string_forget (&d1, MDL);
3243 } else
3244 cip.len = 0;
3245 }
3246
3247 /* Try to find a host or lease that's been assigned to the
3248 specified unique client identifier. */
3249 oc = lookup_option (&dhcp_universe, packet -> options,
3250 DHO_DHCP_CLIENT_IDENTIFIER);
3251 memset (&client_identifier, 0, sizeof client_identifier);
3252 if (oc &&
3253 evaluate_option_cache (&client_identifier,
3254 packet, (struct lease *)0,
3255 (struct client_state *)0,
3256 packet -> options, (struct option_state *)0,
3257 &global_scope, oc, MDL)) {
3258 /* Remember this for later. */
3259 have_client_identifier = 1;
3260
3261 /* First, try to find a fixed host entry for the specified
3262 client identifier... */
3263 if (find_hosts_by_uid (&hp, client_identifier.data,
3264 client_identifier.len, MDL)) {
3265 /* Remember if we know of this client. */
3266 packet -> known = 1;
3267 mockup_lease (&fixed_lease, packet, share, hp);
3268 }
3269
3270 #if defined (DEBUG_FIND_LEASE)
3271 if (fixed_lease) {
3272 log_info ("Found host for client identifier: %s.",
3273 piaddr (fixed_lease -> ip_addr));
3274 }
3275 #endif
3276 if (hp) {
3277 if (!fixed_lease) /* Save the host if we found one. */
3278 host_reference (&host, hp, MDL);
3279 host_dereference (&hp, MDL);
3280 }
3281
3282 find_lease_by_uid (&uid_lease, client_identifier.data,
3283 client_identifier.len, MDL);
3284 }
3285
3286 /* If we didn't find a fixed lease using the uid, try doing
3287 it with the hardware address... */
3288 if (!fixed_lease && !host) {
3289 if (find_hosts_by_haddr (&hp, packet -> raw -> htype,
3290 packet -> raw -> chaddr,
3291 packet -> raw -> hlen, MDL)) {
3292 /* Remember if we know of this client. */
3293 packet -> known = 1;
3294 if (host)
3295 host_dereference (&host, MDL);
3296 host_reference (&host, hp, MDL);
3297 host_dereference (&hp, MDL);
3298 mockup_lease (&fixed_lease, packet, share, host);
3299 #if defined (DEBUG_FIND_LEASE)
3300 if (fixed_lease) {
3301 log_info ("Found host for link address: %s.",
3302 piaddr (fixed_lease -> ip_addr));
3303 }
3304 #endif
3305 }
3306 }
3307
3308 /* Finally, if we haven't found anything yet try again with the
3309 * host-identifier option ... */
3310 if (!fixed_lease && !host) {
3311 if (find_hosts_by_option(&hp, packet,
3312 packet->options, MDL) == 1) {
3313 packet->known = 1;
3314 if (host)
3315 host_dereference(&host, MDL);
3316 host_reference(&host, hp, MDL);
3317 host_dereference(&hp, MDL);
3318 mockup_lease (&fixed_lease, packet, share, host);
3319 #if defined (DEBUG_FIND_LEASE)
3320 if (fixed_lease) {
3321 log_info ("Found host via host-identifier");
3322 }
3323 #endif
3324 }
3325 }
3326
3327 /* If fixed_lease is present but does not match the requested
3328 IP address, and this is a DHCPREQUEST, then we can't return
3329 any other lease, so we might as well return now. */
3330 if (packet -> packet_type == DHCPREQUEST && fixed_lease &&
3331 (fixed_lease -> ip_addr.len != cip.len ||
3332 memcmp (fixed_lease -> ip_addr.iabuf,
3333 cip.iabuf, cip.len))) {
3334 if (ours)
3335 *ours = 1;
3336 strcpy (dhcp_message, "requested address is incorrect");
3337 #if defined (DEBUG_FIND_LEASE)
3338 log_info ("Client's fixed-address %s doesn't match %s%s",
3339 piaddr (fixed_lease -> ip_addr), "request ",
3340 print_dotted_quads (cip.len, cip.iabuf));
3341 #endif
3342 goto out;
3343 }
3344
3345 /*
3346 * If we found leases matching the client identifier, loop through
3347 * the n_uid pointer looking for one that's actually valid. We
3348 * can't do this until we get here because we depend on
3349 * packet -> known, which may be set by either the uid host
3350 * lookup or the haddr host lookup.
3351 *
3352 * Note that the n_uid lease chain is sorted in order of
3353 * preference, so the first one is the best one.
3354 */
3355 while (uid_lease) {
3356 #if defined (DEBUG_FIND_LEASE)
3357 log_info ("trying next lease matching client id: %s",
3358 piaddr (uid_lease -> ip_addr));
3359 #endif
3360
3361 #if defined (FAILOVER_PROTOCOL)
3362 /*
3363 * When we lookup a lease by uid, we know the client identifier
3364 * matches the lease's record. If it is active, or was last
3365 * active with the same client, we can trivially extend it.
3366 * If is not or was not active, we can allocate it to this
3367 * client if it matches the usual free/backup criteria (which
3368 * is contained in lease_mine_to_reallocate()).
3369 */
3370 if (uid_lease->binding_state != FTS_ACTIVE &&
3371 uid_lease->rewind_binding_state != FTS_ACTIVE &&
3372 !lease_mine_to_reallocate(uid_lease)) {
3373 #if defined (DEBUG_FIND_LEASE)
3374 log_info("not active or not mine to allocate: %s",
3375 piaddr(uid_lease->ip_addr));
3376 #endif
3377 goto n_uid;
3378 }
3379 #endif
3380
3381 if (uid_lease -> subnet -> shared_network != share) {
3382 #if defined (DEBUG_FIND_LEASE)
3383 log_info ("wrong network segment: %s",
3384 piaddr (uid_lease -> ip_addr));
3385 #endif
3386 goto n_uid;
3387 }
3388
3389 if ((uid_lease -> pool -> prohibit_list &&
3390 permitted (packet, uid_lease -> pool -> prohibit_list)) ||
3391 (uid_lease -> pool -> permit_list &&
3392 !permitted (packet, uid_lease -> pool -> permit_list))) {
3393 #if defined (DEBUG_FIND_LEASE)
3394 log_info ("not permitted: %s",
3395 piaddr (uid_lease -> ip_addr));
3396 #endif
3397 n_uid:
3398 if (uid_lease -> n_uid)
3399 lease_reference (&next,
3400 uid_lease -> n_uid, MDL);
3401 if (!packet -> raw -> ciaddr.s_addr)
3402 release_lease (uid_lease, packet);
3403 lease_dereference (&uid_lease, MDL);
3404 if (next) {
3405 lease_reference (&uid_lease, next, MDL);
3406 lease_dereference (&next, MDL);
3407 }
3408 continue;
3409 }
3410 break;
3411 }
3412 #if defined (DEBUG_FIND_LEASE)
3413 if (uid_lease)
3414 log_info ("Found lease for client id: %s.",
3415 piaddr (uid_lease -> ip_addr));
3416 #endif
3417
3418 /* Find a lease whose hardware address matches, whose client
3419 * identifier matches (or equally doesn't have one), that's
3420 * permitted, and that's on the correct subnet.
3421 *
3422 * Note that the n_hw chain is sorted in order of preference, so
3423 * the first one found is the best one.
3424 */
3425 h.hlen = packet -> raw -> hlen + 1;
3426 h.hbuf [0] = packet -> raw -> htype;
3427 memcpy (&h.hbuf [1], packet -> raw -> chaddr, packet -> raw -> hlen);
3428 find_lease_by_hw_addr (&hw_lease, h.hbuf, h.hlen, MDL);
3429 while (hw_lease) {
3430 #if defined (DEBUG_FIND_LEASE)
3431 log_info ("trying next lease matching hw addr: %s",
3432 piaddr (hw_lease -> ip_addr));
3433 #endif
3434 #if defined (FAILOVER_PROTOCOL)
3435 /*
3436 * When we lookup a lease by chaddr, we know the MAC address
3437 * matches the lease record (we will check if the lease has a
3438 * client-id the client does not next). If the lease is
3439 * currently active or was last active with this client, we can
3440 * trivially extend it. Otherwise, there are a set of rules
3441 * that govern if we can reallocate this lease to any client
3442 * ("lease_mine_to_reallocate()") including this one.
3443 */
3444 if (hw_lease->binding_state != FTS_ACTIVE &&
3445 hw_lease->rewind_binding_state != FTS_ACTIVE &&
3446 !lease_mine_to_reallocate(hw_lease)) {
3447 #if defined (DEBUG_FIND_LEASE)
3448 log_info("not active or not mine to allocate: %s",
3449 piaddr(hw_lease->ip_addr));
3450 #endif
3451 goto n_hw;
3452 }
3453 #endif
3454
3455 /*
3456 * This conditional skips "potentially active" leases (leases
3457 * we think are expired may be extended by the peer, etc) that
3458 * may be assigned to a differently /client-identified/ client
3459 * with the same MAC address.
3460 */
3461 if (hw_lease -> binding_state != FTS_FREE &&
3462 hw_lease -> binding_state != FTS_BACKUP &&
3463 hw_lease -> uid &&
3464 (!have_client_identifier ||
3465 hw_lease -> uid_len != client_identifier.len ||
3466 memcmp (hw_lease -> uid, client_identifier.data,
3467 hw_lease -> uid_len))) {
3468 #if defined (DEBUG_FIND_LEASE)
3469 log_info ("wrong client identifier: %s",
3470 piaddr (hw_lease -> ip_addr));
3471 #endif
3472 goto n_hw;
3473 }
3474 if (hw_lease -> subnet -> shared_network != share) {
3475 #if defined (DEBUG_FIND_LEASE)
3476 log_info ("wrong network segment: %s",
3477 piaddr (hw_lease -> ip_addr));
3478 #endif
3479 goto n_hw;
3480 }
3481 if ((hw_lease -> pool -> prohibit_list &&
3482 permitted (packet, hw_lease -> pool -> prohibit_list)) ||
3483 (hw_lease -> pool -> permit_list &&
3484 !permitted (packet, hw_lease -> pool -> permit_list))) {
3485 #if defined (DEBUG_FIND_LEASE)
3486 log_info ("not permitted: %s",
3487 piaddr (hw_lease -> ip_addr));
3488 #endif
3489 if (!packet -> raw -> ciaddr.s_addr)
3490 release_lease (hw_lease, packet);
3491 n_hw:
3492 if (hw_lease -> n_hw)
3493 lease_reference (&next, hw_lease -> n_hw, MDL);
3494 lease_dereference (&hw_lease, MDL);
3495 if (next) {
3496 lease_reference (&hw_lease, next, MDL);
3497 lease_dereference (&next, MDL);
3498 }
3499 continue;
3500 }
3501 break;
3502 }
3503 #if defined (DEBUG_FIND_LEASE)
3504 if (hw_lease)
3505 log_info ("Found lease for hardware address: %s.",
3506 piaddr (hw_lease -> ip_addr));
3507 #endif
3508
3509 /* Try to find a lease that's been allocated to the client's
3510 IP address. */
3511 if (ip_lease_in)
3512 lease_reference (&ip_lease, ip_lease_in, MDL);
3513 else if (cip.len)
3514 find_lease_by_ip_addr (&ip_lease, cip, MDL);
3515
3516 #if defined (DEBUG_FIND_LEASE)
3517 if (ip_lease)
3518 log_info ("Found lease for requested address: %s.",
3519 piaddr (ip_lease -> ip_addr));
3520 #endif
3521
3522 /* If ip_lease is valid at this point, set ours to one, so that
3523 even if we choose a different lease, we know that the address
3524 the client was requesting was ours, and thus we can NAK it. */
3525 if (ip_lease && ours)
3526 *ours = 1;
3527
3528 /* If the requested IP address isn't on the network the packet
3529 came from, don't use it. Allow abandoned leases to be matched
3530 here - if the client is requesting it, there's a decent chance
3531 that it's because the lease database got trashed and a client
3532 that thought it had this lease answered an ARP or PING, causing the
3533 lease to be abandoned. If so, this request probably came from
3534 that client. */
3535 if (ip_lease && (ip_lease -> subnet -> shared_network != share)) {
3536 if (ours)
3537 *ours = 1;
3538 #if defined (DEBUG_FIND_LEASE)
3539 log_info ("...but it was on the wrong shared network.");
3540 #endif
3541 strcpy (dhcp_message, "requested address on bad subnet");
3542 lease_dereference (&ip_lease, MDL);
3543 }
3544
3545 /*
3546 * If the requested address is in use (or potentially in use) by
3547 * a different client, it can't be granted.
3548 *
3549 * This first conditional only detects if the lease is currently
3550 * identified to a different client (client-id and/or chaddr
3551 * mismatch). In this case we may not want to give the client the
3552 * lease, if doing so may potentially be an addressing conflict.
3553 */
3554 if (ip_lease &&
3555 (ip_lease -> uid ?
3556 (!have_client_identifier ||
3557 ip_lease -> uid_len != client_identifier.len ||
3558 memcmp (ip_lease -> uid, client_identifier.data,
3559 ip_lease -> uid_len)) :
3560 (ip_lease -> hardware_addr.hbuf [0] != packet -> raw -> htype ||
3561 ip_lease -> hardware_addr.hlen != packet -> raw -> hlen + 1 ||
3562 memcmp (&ip_lease -> hardware_addr.hbuf [1],
3563 packet -> raw -> chaddr,
3564 (unsigned)(ip_lease -> hardware_addr.hlen - 1))))) {
3565 /*
3566 * A lease is unavailable for allocation to a new client if
3567 * it is not in the FREE or BACKUP state. There may be
3568 * leases that are in the expired state with a rewinding
3569 * state that is free or backup, but these will be processed
3570 * into the free or backup states by expiration processes, so
3571 * checking for them here is superfluous.
3572 */
3573 if (ip_lease -> binding_state != FTS_FREE &&
3574 ip_lease -> binding_state != FTS_BACKUP) {
3575 #if defined (DEBUG_FIND_LEASE)
3576 log_info ("rejecting lease for requested address.");
3577 #endif
3578 /* If we're rejecting it because the peer has
3579 it, don't set "ours", because we shouldn't NAK. */
3580 if (ours && ip_lease -> binding_state != FTS_ACTIVE)
3581 *ours = 0;
3582 lease_dereference (&ip_lease, MDL);
3583 }
3584 }
3585
3586 /*
3587 * If we got an ip_lease and a uid_lease or hw_lease, and ip_lease
3588 * is/was not active, and is not ours to reallocate, forget about it.
3589 */
3590 if (ip_lease && (uid_lease || hw_lease) &&
3591 ip_lease->binding_state != FTS_ACTIVE &&
3592 ip_lease->rewind_binding_state != FTS_ACTIVE &&
3593 #if defined(FAILOVER_PROTOCOL)
3594 !lease_mine_to_reallocate(ip_lease) &&
3595 #endif
3596 packet->packet_type == DHCPDISCOVER) {
3597 #if defined (DEBUG_FIND_LEASE)
3598 log_info("ip lease not active or not ours to offer.");
3599 #endif
3600 lease_dereference(&ip_lease, MDL);
3601 }
3602
3603 /* If for some reason the client has more than one lease
3604 on the subnet that matches its uid, pick the one that
3605 it asked for and (if we can) free the other. */
3606 if (ip_lease && ip_lease->binding_state == FTS_ACTIVE &&
3607 ip_lease->uid && ip_lease != uid_lease) {
3608 if (have_client_identifier &&
3609 (ip_lease -> uid_len == client_identifier.len) &&
3610 !memcmp (client_identifier.data,
3611 ip_lease -> uid, ip_lease -> uid_len)) {
3612 if (uid_lease) {
3613 if (uid_lease->binding_state == FTS_ACTIVE) {
3614 log_error ("client %s has duplicate%s on %s",
3615 (print_hw_addr
3616 (packet -> raw -> htype,
3617 packet -> raw -> hlen,
3618 packet -> raw -> chaddr)),
3619 " leases",
3620 (ip_lease -> subnet ->
3621 shared_network -> name));
3622
3623 /* If the client is REQUESTing the lease,
3624 it shouldn't still be using the old
3625 one, so we can free it for allocation. */
3626 if (uid_lease &&
3627 uid_lease->binding_state == FTS_ACTIVE &&
3628 !packet -> raw -> ciaddr.s_addr &&
3629 (share ==
3630 uid_lease -> subnet -> shared_network) &&
3631 packet -> packet_type == DHCPREQUEST)
3632 release_lease (uid_lease, packet);
3633 }
3634 lease_dereference (&uid_lease, MDL);
3635 lease_reference (&uid_lease, ip_lease, MDL);
3636 }
3637 }
3638
3639 /* If we get to here and fixed_lease is not null, that means
3640 that there are both a dynamic lease and a fixed-address
3641 declaration for the same IP address. */
3642 if (packet -> packet_type == DHCPREQUEST && fixed_lease) {
3643 lease_dereference (&fixed_lease, MDL);
3644 db_conflict:
3645 log_error ("Dynamic and static leases present for %s.",
3646 piaddr (cip));
3647 log_error ("Remove host declaration %s or remove %s",
3648 (fixed_lease && fixed_lease -> host
3649 ? (fixed_lease -> host -> name
3650 ? fixed_lease -> host -> name
3651 : piaddr (cip))
3652 : piaddr (cip)),
3653 piaddr (cip));
3654 log_error ("from the dynamic address pool for %s",
3655 ip_lease -> subnet -> shared_network -> name
3656 );
3657 if (fixed_lease)
3658 lease_dereference (&ip_lease, MDL);
3659 strcpy (dhcp_message,
3660 "database conflict - call for help!");
3661 }
3662
3663 if (ip_lease && ip_lease != uid_lease) {
3664 #if defined (DEBUG_FIND_LEASE)
3665 log_info ("requested address not available.");
3666 #endif
3667 lease_dereference (&ip_lease, MDL);
3668 }
3669 }
3670
3671 /* If we get to here with both fixed_lease and ip_lease not
3672 null, then we have a configuration file bug. */
3673 if (packet -> packet_type == DHCPREQUEST && fixed_lease && ip_lease)
3674 goto db_conflict;
3675
3676 /* Toss extra pointers to the same lease... */
3677 if (hw_lease && hw_lease == uid_lease) {
3678 #if defined (DEBUG_FIND_LEASE)
3679 log_info ("hardware lease and uid lease are identical.");
3680 #endif
3681 lease_dereference (&hw_lease, MDL);
3682 }
3683 if (ip_lease && ip_lease == hw_lease) {
3684 lease_dereference (&hw_lease, MDL);
3685 #if defined (DEBUG_FIND_LEASE)
3686 log_info ("hardware lease and ip lease are identical.");
3687 #endif
3688 }
3689 if (ip_lease && ip_lease == uid_lease) {
3690 lease_dereference (&uid_lease, MDL);
3691 #if defined (DEBUG_FIND_LEASE)
3692 log_info ("uid lease and ip lease are identical.");
3693 #endif
3694 }
3695
3696 /* Make sure the client is permitted to use the requested lease. */
3697 if (ip_lease &&
3698 ((ip_lease -> pool -> prohibit_list &&
3699 permitted (packet, ip_lease -> pool -> prohibit_list)) ||
3700 (ip_lease -> pool -> permit_list &&
3701 !permitted (packet, ip_lease -> pool -> permit_list)))) {
3702 if (!packet->raw->ciaddr.s_addr &&
3703 (ip_lease->binding_state == FTS_ACTIVE))
3704 release_lease (ip_lease, packet);
3705
3706 lease_dereference (&ip_lease, MDL);
3707 }
3708
3709 if (uid_lease &&
3710 ((uid_lease -> pool -> prohibit_list &&
3711 permitted (packet, uid_lease -> pool -> prohibit_list)) ||
3712 (uid_lease -> pool -> permit_list &&
3713 !permitted (packet, uid_lease -> pool -> permit_list)))) {
3714 if (!packet -> raw -> ciaddr.s_addr)
3715 release_lease (uid_lease, packet);
3716 lease_dereference (&uid_lease, MDL);
3717 }
3718
3719 if (hw_lease &&
3720 ((hw_lease -> pool -> prohibit_list &&
3721 permitted (packet, hw_lease -> pool -> prohibit_list)) ||
3722 (hw_lease -> pool -> permit_list &&
3723 !permitted (packet, hw_lease -> pool -> permit_list)))) {
3724 if (!packet -> raw -> ciaddr.s_addr)
3725 release_lease (hw_lease, packet);
3726 lease_dereference (&hw_lease, MDL);
3727 }
3728
3729 /* If we've already eliminated the lease, it wasn't there to
3730 begin with. If we have come up with a matching lease,
3731 set the message to bad network in case we have to throw it out. */
3732 if (!ip_lease) {
3733 strcpy (dhcp_message, "requested address not available");
3734 }
3735
3736 /* If this is a DHCPREQUEST, make sure the lease we're going to return
3737 matches the requested IP address. If it doesn't, don't return a
3738 lease at all. */
3739 if (packet -> packet_type == DHCPREQUEST &&
3740 !ip_lease && !fixed_lease) {
3741 #if defined (DEBUG_FIND_LEASE)
3742 log_info ("no applicable lease found for DHCPREQUEST.");
3743 #endif
3744 goto out;
3745 }
3746
3747 /* At this point, if fixed_lease is nonzero, we can assign it to
3748 this client. */
3749 if (fixed_lease) {
3750 lease_reference (&lease, fixed_lease, MDL);
3751 lease_dereference (&fixed_lease, MDL);
3752 #if defined (DEBUG_FIND_LEASE)
3753 log_info ("choosing fixed address.");
3754 #endif
3755 }
3756
3757 /* If we got a lease that matched the ip address and don't have
3758 a better offer, use that; otherwise, release it. */
3759 if (ip_lease) {
3760 if (lease) {
3761 if (!packet -> raw -> ciaddr.s_addr)
3762 release_lease (ip_lease, packet);
3763 #if defined (DEBUG_FIND_LEASE)
3764 log_info ("not choosing requested address (!).");
3765 #endif
3766 } else {
3767 #if defined (DEBUG_FIND_LEASE)
3768 log_info ("choosing lease on requested address.");
3769 #endif
3770 lease_reference (&lease, ip_lease, MDL);
3771 if (lease -> host)
3772 host_dereference (&lease -> host, MDL);
3773 }
3774 lease_dereference (&ip_lease, MDL);
3775 }
3776
3777 /* If we got a lease that matched the client identifier, we may want
3778 to use it, but if we already have a lease we like, we must free
3779 the lease that matched the client identifier. */
3780 if (uid_lease) {
3781 if (lease) {
3782 log_error("uid lease %s for client %s is duplicate "
3783 "on %s",
3784 piaddr(uid_lease->ip_addr),
3785 print_hw_addr(packet->raw->htype,
3786 packet->raw->hlen,
3787 packet->raw->chaddr),
3788 uid_lease->subnet->shared_network->name);
3789
3790 if (!packet -> raw -> ciaddr.s_addr &&
3791 packet -> packet_type == DHCPREQUEST &&
3792 uid_lease -> binding_state == FTS_ACTIVE)
3793 release_lease(uid_lease, packet);
3794 #if defined (DEBUG_FIND_LEASE)
3795 log_info ("not choosing uid lease.");
3796 #endif
3797 } else {
3798 lease_reference (&lease, uid_lease, MDL);
3799 if (lease -> host)
3800 host_dereference (&lease -> host, MDL);
3801 #if defined (DEBUG_FIND_LEASE)
3802 log_info ("choosing uid lease.");
3803 #endif
3804 }
3805 lease_dereference (&uid_lease, MDL);
3806 }
3807
3808 /* The lease that matched the hardware address is treated likewise. */
3809 if (hw_lease) {
3810 if (lease) {
3811 #if defined (DEBUG_FIND_LEASE)
3812 log_info ("not choosing hardware lease.");
3813 #endif
3814 } else {
3815 /* We're a little lax here - if the client didn't
3816 send a client identifier and it's a bootp client,
3817 but the lease has a client identifier, we still
3818 let the client have a lease. */
3819 if (!hw_lease -> uid_len ||
3820 (have_client_identifier
3821 ? (hw_lease -> uid_len ==
3822 client_identifier.len &&
3823 !memcmp (hw_lease -> uid,
3824 client_identifier.data,
3825 client_identifier.len))
3826 : packet -> packet_type == 0)) {
3827 lease_reference (&lease, hw_lease, MDL);
3828 if (lease -> host)
3829 host_dereference (&lease -> host, MDL);
3830 #if defined (DEBUG_FIND_LEASE)
3831 log_info ("choosing hardware lease.");
3832 #endif
3833 } else {
3834 #if defined (DEBUG_FIND_LEASE)
3835 log_info ("not choosing hardware lease: %s.",
3836 "uid mismatch");
3837 #endif
3838 }
3839 }
3840 lease_dereference (&hw_lease, MDL);
3841 }
3842
3843 /* If we found a host_decl but no matching address, try to
3844 find a host_decl that has no address, and if there is one,
3845 hang it off the lease so that we can use the supplied
3846 options. */
3847 if (lease && host && !lease -> host) {
3848 struct host_decl *p = (struct host_decl *)0;
3849 struct host_decl *n = (struct host_decl *)0;
3850 host_reference (&p, host, MDL);
3851 while (p) {
3852 if (!p -> fixed_addr) {
3853 host_reference (&lease -> host, p, MDL);
3854 host_dereference (&p, MDL);
3855 break;
3856 }
3857 if (p -> n_ipaddr)
3858 host_reference (&n, p -> n_ipaddr, MDL);
3859 host_dereference (&p, MDL);
3860 if (n) {
3861 host_reference (&p, n, MDL);
3862 host_dereference (&n, MDL);
3863 }
3864 }
3865 }
3866
3867 /* If we find an abandoned lease, but it's the one the client
3868 requested, we assume that previous bugginess on the part
3869 of the client, or a server database loss, caused the lease to
3870 be abandoned, so we reclaim it and let the client have it. */
3871 if (lease &&
3872 (lease -> binding_state == FTS_ABANDONED) &&
3873 lease == ip_lease &&
3874 packet -> packet_type == DHCPREQUEST) {
3875 log_error ("Reclaiming REQUESTed abandoned IP address %s.",
3876 piaddr (lease -> ip_addr));
3877 } else if (lease && (lease -> binding_state == FTS_ABANDONED)) {
3878 /* Otherwise, if it's not the one the client requested, we do not
3879 return it - instead, we claim it's ours, causing a DHCPNAK to be
3880 sent if this lookup is for a DHCPREQUEST, and force the client
3881 to go back through the allocation process. */
3882 if (ours)
3883 *ours = 1;
3884 lease_dereference (&lease, MDL);
3885 }
3886
3887 out:
3888 if (have_client_identifier)
3889 data_string_forget (&client_identifier, MDL);
3890
3891 if (fixed_lease)
3892 lease_dereference (&fixed_lease, MDL);
3893 if (hw_lease)
3894 lease_dereference (&hw_lease, MDL);
3895 if (uid_lease)
3896 lease_dereference (&uid_lease, MDL);
3897 if (ip_lease)
3898 lease_dereference (&ip_lease, MDL);
3899 if (host)
3900 host_dereference (&host, MDL);
3901
3902 if (lease) {
3903 #if defined (DEBUG_FIND_LEASE)
3904 log_info ("Returning lease: %s.",
3905 piaddr (lease -> ip_addr));
3906 #endif
3907 lease_reference (lp, lease, file, line);
3908 lease_dereference (&lease, MDL);
3909 return 1;
3910 }
3911 #if defined (DEBUG_FIND_LEASE)
3912 log_info ("Not returning a lease.");
3913 #endif
3914 return 0;
3915 }
3916
3917 /* Search the provided host_decl structure list for an address that's on
3918 the specified shared network. If one is found, mock up and return a
3919 lease structure for it; otherwise return the null pointer. */
3920
3921 int mockup_lease (struct lease **lp, struct packet *packet,
3922 struct shared_network *share, struct host_decl *hp)
3923 {
3924 struct lease *lease = (struct lease *)0;
3925 struct host_decl *rhp = (struct host_decl *)0;
3926
3927 if (lease_allocate (&lease, MDL) != ISC_R_SUCCESS)
3928 return 0;
3929 if (host_reference (&rhp, hp, MDL) != ISC_R_SUCCESS) {
3930 lease_dereference (&lease, MDL);
3931 return 0;
3932 }
3933 if (!find_host_for_network (&lease -> subnet,
3934 &rhp, &lease -> ip_addr, share)) {
3935 lease_dereference (&lease, MDL);
3936 host_dereference (&rhp, MDL);
3937 return 0;
3938 }
3939 host_reference (&lease -> host, rhp, MDL);
3940 if (rhp -> client_identifier.len > sizeof lease -> uid_buf)
3941 lease -> uid = dmalloc (rhp -> client_identifier.len, MDL);
3942 else
3943 lease -> uid = lease -> uid_buf;
3944 if (!lease -> uid) {
3945 lease_dereference (&lease, MDL);
3946 host_dereference (&rhp, MDL);
3947 return 0;
3948 }
3949 memcpy (lease -> uid, rhp -> client_identifier.data,
3950 rhp -> client_identifier.len);
3951 lease -> uid_len = rhp -> client_identifier.len;
3952 lease -> hardware_addr = rhp -> interface;
3953 lease -> starts = lease -> cltt = lease -> ends = MIN_TIME;
3954 lease -> flags = STATIC_LEASE;
3955 lease -> binding_state = FTS_FREE;
3956
3957 lease_reference (lp, lease, MDL);
3958
3959 lease_dereference (&lease, MDL);
3960 host_dereference (&rhp, MDL);
3961 return 1;
3962 }
3963
3964 /* Look through all the pools in a list starting with the specified pool
3965 for a free lease. We try to find a virgin lease if we can. If we
3966 don't find a virgin lease, we try to find a non-virgin lease that's
3967 free. If we can't find one of those, we try to reclaim an abandoned
3968 lease. If all of these possibilities fail to pan out, we don't return
3969 a lease at all. */
3970
3971 int allocate_lease (struct lease **lp, struct packet *packet,
3972 struct pool *pool, int *peer_has_leases)
3973 {
3974 struct lease *lease = (struct lease *)0;
3975 struct lease *candl = (struct lease *)0;
3976
3977 for (; pool ; pool = pool -> next) {
3978 if ((pool -> prohibit_list &&
3979 permitted (packet, pool -> prohibit_list)) ||
3980 (pool -> permit_list &&
3981 !permitted (packet, pool -> permit_list)))
3982 continue;
3983
3984 #if defined (FAILOVER_PROTOCOL)
3985 /* Peer_has_leases just says that we found at least one
3986 free lease. If no free lease is returned, the caller
3987 can deduce that this means the peer is hogging all the
3988 free leases, so we can print a better error message. */
3989 /* XXX Do we need code here to ignore PEER_IS_OWNER and
3990 * XXX just check tstp if we're in, e.g., PARTNER_DOWN?
3991 * XXX Where do we deal with CONFLICT_DETECTED, et al? */
3992 /* XXX This should be handled by the lease binding "state
3993 * XXX machine" - that is, when we get here, if a lease
3994 * XXX could be allocated, it will have the correct
3995 * XXX binding state so that the following code will
3996 * XXX result in its being allocated. */
3997 /* Skip to the most expired lease in the pool that is not
3998 * owned by a failover peer. */
3999 if (pool->failover_peer != NULL) {
4000 if (pool->failover_peer->i_am == primary) {
4001 candl = pool->free;
4002
4003 /*
4004 * In normal operation, we never want to touch
4005 * the peer's leases. In partner-down
4006 * operation, we need to be able to pick up
4007 * the peer's leases after STOS+MCLT.
4008 */
4009 if (pool->backup != NULL) {
4010 if (((candl == NULL) ||
4011 (candl->ends >
4012 pool->backup->ends)) &&
4013 lease_mine_to_reallocate(
4014 pool->backup)) {
4015 candl = pool->backup;
4016 } else {
4017 *peer_has_leases = 1;
4018 }
4019 }
4020 } else {
4021 candl = pool->backup;
4022
4023 if (pool->free != NULL) {
4024 if (((candl == NULL) ||
4025 (candl->ends >
4026 pool->free->ends)) &&
4027 lease_mine_to_reallocate(
4028 pool->free)) {
4029 candl = pool->free;
4030 } else {
4031 *peer_has_leases = 1;
4032 }
4033 }
4034 }
4035
4036 /* Try abandoned leases as a last resort. */
4037 if ((candl == NULL) &&
4038 (pool->abandoned != NULL) &&
4039 lease_mine_to_reallocate(pool->abandoned))
4040 candl = pool->abandoned;
4041 } else
4042 #endif
4043 {
4044 if (pool -> free)
4045 candl = pool -> free;
4046 else
4047 candl = pool -> abandoned;
4048 }
4049
4050 /*
4051 * XXX: This may not match with documented expectation.
4052 * It's expected that when we OFFER a lease, we set its
4053 * ends time forward 2 minutes so that it gets sorted to
4054 * the end of its free list (avoiding a similar allocation
4055 * to another client). It is not expected that we issue a
4056 * "no free leases" error when the last lease has been
4057 * offered, but it's not exactly broken either.
4058 */
4059 if (!candl || (candl -> ends > cur_time))
4060 continue;
4061
4062 if (!lease) {
4063 lease = candl;
4064 continue;
4065 }
4066
4067 /*
4068 * There are tiers of lease state preference, listed here in
4069 * reverse order (least to most preferential):
4070 *
4071 * ABANDONED
4072 * FREE/BACKUP
4073 *
4074 * If the selected lease and candidate are both of the same
4075 * state, select the oldest (longest ago) expiration time
4076 * between the two. If the candidate lease is of a higher
4077 * preferred grade over the selected lease, use it.
4078 */
4079 if ((lease -> binding_state == FTS_ABANDONED) &&
4080 ((candl -> binding_state != FTS_ABANDONED) ||
4081 (candl -> ends < lease -> ends))) {
4082 lease = candl;
4083 continue;
4084 } else if (candl -> binding_state == FTS_ABANDONED)
4085 continue;
4086
4087 if ((lease -> uid_len || lease -> hardware_addr.hlen) &&
4088 ((!candl -> uid_len && !candl -> hardware_addr.hlen) ||
4089 (candl -> ends < lease -> ends))) {
4090 lease = candl;
4091 continue;
4092 } else if (candl -> uid_len || candl -> hardware_addr.hlen)
4093 continue;
4094
4095 if (candl -> ends < lease -> ends)
4096 lease = candl;
4097 }
4098
4099 if (lease) {
4100 if (lease -> binding_state == FTS_ABANDONED)
4101 log_error ("Reclaiming abandoned lease %s.",
4102 piaddr (lease -> ip_addr));
4103
4104 lease_reference (lp, lease, MDL);
4105 return 1;
4106 }
4107
4108 return 0;
4109 }
4110
4111 /* Determine whether or not a permit exists on a particular permit list
4112 that matches the specified packet, returning nonzero if so, zero if
4113 not. */
4114
4115 int permitted (packet, permit_list)
4116 struct packet *packet;
4117 struct permit *permit_list;
4118 {
4119 struct permit *p;
4120 int i;
4121
4122 for (p = permit_list; p; p = p -> next) {
4123 switch (p -> type) {
4124 case permit_unknown_clients:
4125 if (!packet -> known)
4126 return 1;
4127 break;
4128
4129 case permit_known_clients:
4130 if (packet -> known)
4131 return 1;
4132 break;
4133
4134 case permit_authenticated_clients:
4135 if (packet -> authenticated)
4136 return 1;
4137 break;
4138
4139 case permit_unauthenticated_clients:
4140 if (!packet -> authenticated)
4141 return 1;
4142 break;
4143
4144 case permit_all_clients:
4145 return 1;
4146
4147 case permit_dynamic_bootp_clients:
4148 if (!packet -> options_valid ||
4149 !packet -> packet_type)
4150 return 1;
4151 break;
4152
4153 case permit_class:
4154 for (i = 0; i < packet -> class_count; i++) {
4155 if (p -> class == packet -> classes [i])
4156 return 1;
4157 if (packet -> classes [i] &&
4158 packet -> classes [i] -> superclass &&
4159 (packet -> classes [i] -> superclass ==
4160 p -> class))
4161 return 1;
4162 }
4163 break;
4164
4165 case permit_after:
4166 if (cur_time > p->after)
4167 return 1;
4168 break;
4169 }
4170 }
4171 return 0;
4172 }
4173
4174 int locate_network (packet)
4175 struct packet *packet;
4176 {
4177 struct iaddr ia;
4178 struct data_string data;
4179 struct subnet *subnet = (struct subnet *)0;
4180 struct option_cache *oc;
4181
4182 /* See if there's a Relay Agent Link Selection Option, or a
4183 * Subnet Selection Option. The Link-Select and Subnet-Select
4184 * are formatted and used precisely the same, but we must prefer
4185 * the link-select over the subnet-select.
4186 */
4187 if ((oc = lookup_option(&agent_universe, packet->options,
4188 RAI_LINK_SELECT)) == NULL)
4189 oc = lookup_option(&dhcp_universe, packet->options,
4190 DHO_SUBNET_SELECTION);
4191
4192 /* If there's no SSO and no giaddr, then use the shared_network
4193 from the interface, if there is one. If not, fail. */
4194 if (!oc && !packet -> raw -> giaddr.s_addr) {
4195 if (packet -> interface -> shared_network) {
4196 shared_network_reference
4197 (&packet -> shared_network,
4198 packet -> interface -> shared_network, MDL);
4199 return 1;
4200 }
4201 return 0;
4202 }
4203
4204 /* If there's an option indicating link connection, and it's valid,
4205 * use it to figure out the subnet. If it's not valid, fail.
4206 */
4207 if (oc) {
4208 memset (&data, 0, sizeof data);
4209 if (!evaluate_option_cache (&data, packet, (struct lease *)0,
4210 (struct client_state *)0,
4211 packet -> options,
4212 (struct option_state *)0,
4213 &global_scope, oc, MDL)) {
4214 return 0;
4215 }
4216 if (data.len != 4) {
4217 return 0;
4218 }
4219 ia.len = 4;
4220 memcpy (ia.iabuf, data.data, 4);
4221 data_string_forget (&data, MDL);
4222 } else {
4223 ia.len = 4;
4224 memcpy (ia.iabuf, &packet -> raw -> giaddr, 4);
4225 }
4226
4227 /* If we know the subnet on which the IP address lives, use it. */
4228 if (find_subnet (&subnet, ia, MDL)) {
4229 shared_network_reference (&packet -> shared_network,
4230 subnet -> shared_network, MDL);
4231 subnet_dereference (&subnet, MDL);
4232 return 1;
4233 }
4234
4235 /* Otherwise, fail. */
4236 return 0;
4237 }
4238
4239 /*
4240 * Try to figure out the source address to send packets from.
4241 *
4242 * If the packet we received specified the server address, then we
4243 * will use that.
4244 *
4245 * Otherwise, use the first address from the interface. If we do
4246 * this, we also save this into the option cache as the server
4247 * address.
4248 */
4249 void
4250 get_server_source_address(struct in_addr *from,
4251 struct option_state *options,
4252 struct packet *packet) {
4253 unsigned option_num;
4254 struct option_cache *oc;
4255 struct data_string d;
4256 struct in_addr *a;
4257
4258 memset(&d, 0, sizeof(d));
4259
4260 option_num = DHO_DHCP_SERVER_IDENTIFIER;
4261 oc = lookup_option(&dhcp_universe, options, option_num);
4262 if ((oc != NULL) &&
4263 evaluate_option_cache(&d, packet, NULL, NULL, packet->options,
4264 options, &global_scope, oc, MDL)) {
4265 if (d.len == sizeof(*from)) {
4266 memcpy(from, d.data, sizeof(*from));
4267 data_string_forget(&d, MDL);
4268 return;
4269 }
4270 data_string_forget(&d, MDL);
4271 }
4272
4273 if (packet->interface->address_count > 0) {
4274 if (option_cache_allocate(&oc, MDL)) {
4275 a = &packet->interface->addresses[0];
4276 if (make_const_data(&oc->expression,
4277 (unsigned char *)a, sizeof(*a),
4278 0, 0, MDL)) {
4279 option_code_hash_lookup(&oc->option,
4280 dhcp_universe.code_hash,
4281 &option_num, 0, MDL);
4282 save_option(&dhcp_universe, options, oc);
4283 }
4284 option_cache_dereference(&oc, MDL);
4285 }
4286 *from = packet->interface->addresses[0];
4287 } else {
4288 memset(from, 0, sizeof(*from));
4289 }
4290 }
4291
4292 /*
4293 * Look for the lowest numbered site code number and
4294 * apply a log warning if it is less than 224. Do not
4295 * permit site codes less than 128 (old code never did).
4296 *
4297 * Note that we could search option codes 224 down to 128
4298 * on the hash table, but the table is (probably) smaller
4299 * than that if it was declared as a standalone table with
4300 * defaults. So we traverse the option code hash.
4301 */
4302 static int
4303 find_min_site_code(struct universe *u)
4304 {
4305 if (u->site_code_min)
4306 return u->site_code_min;
4307
4308 /*
4309 * Note that site_code_min has to be global as we can't pass an
4310 * argument through hash_foreach(). The value 224 is taken from
4311 * RFC 3942.
4312 */
4313 site_code_min = 224;
4314 option_code_hash_foreach(u->code_hash, lowest_site_code);
4315
4316 if (site_code_min < 224) {
4317 log_error("WARNING: site-local option codes less than 224 have "
4318 "been deprecated by RFC3942. You have options "
4319 "listed in site local space %s that number as low as "
4320 "%d. Please investigate if these should be declared "
4321 "as regular options rather than site-local options, "
4322 "or migrated up past 224.",
4323 u->name, site_code_min);
4324 }
4325
4326 /*
4327 * don't even bother logging, this is just silly, and never worked
4328 * on any old version of software.
4329 */
4330 if (site_code_min < 128)
4331 site_code_min = 128;
4332
4333 /*
4334 * Cache the determined minimum site code on the universe structure.
4335 * Note that due to the < 128 check above, a value of zero is
4336 * impossible.
4337 */
4338 u->site_code_min = site_code_min;
4339
4340 return site_code_min;
4341 }
4342
4343 static isc_result_t
4344 lowest_site_code(const void *key, unsigned len, void *object)
4345 {
4346 struct option *option = object;
4347
4348 if (option->code < site_code_min)
4349 site_code_min = option->code;
4350
4351 return ISC_R_SUCCESS;
4352 }
4353
4354 static void
4355 maybe_return_agent_options(struct packet *packet, struct option_state *options)
4356 {
4357 /* If there were agent options in the incoming packet, return
4358 * them. Do not return the agent options if they were stashed
4359 * on the lease. We do not check giaddr to detect the presence of
4360 * a relay, as this excludes "l2" relay agents which have no giaddr
4361 * to set.
4362 *
4363 * XXX: If the user configures options for the relay agent information
4364 * (state->options->universes[agent_universe.index] is not NULL),
4365 * we're still required to duplicate other values provided by the
4366 * relay agent. So we need to merge the old values not configured
4367 * by the user into the new state, not just give up.
4368 */
4369 if (!packet->agent_options_stashed &&
4370 packet->options->universe_count > agent_universe.index &&
4371 packet->options->universes[agent_universe.index] != NULL &&
4372 (options->universe_count <= agent_universe.index ||
4373 options->universes[agent_universe.index] == NULL)) {
4374 option_chain_head_reference
4375 ((struct option_chain_head **)
4376 &(options->universes[agent_universe.index]),
4377 (struct option_chain_head *)
4378 packet->options->universes[agent_universe.index], MDL);
4379
4380 if (options->universe_count <= agent_universe.index)
4381 options->universe_count = agent_universe.index + 1;
4382 }
4383 }