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