3 DHCP Protocol engine. */
6 * Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1995-2003 by Internet Software Consortium
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.
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.
21 * Internet Systems Consortium, Inc.
23 * Redwood City, CA 94063
25 * https://www.isc.org/
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''.
40 static void commit_leases_ackout(void *foo
);
41 static void maybe_return_agent_options(struct packet
*packet
,
42 struct option_state
*options
);
44 int outstanding_pings
;
46 struct leasequeue
*ackqueue_head
, *ackqueue_tail
;
47 static struct leasequeue
*free_ackqueue
;
48 static struct timeval max_fsync
;
51 int max_outstanding_acks
= DEFAULT_DELAYED_ACK
;
52 int max_ack_delay_secs
= DEFAULT_ACK_DELAY_SECS
;
53 int max_ack_delay_usecs
= DEFAULT_ACK_DELAY_USECS
;
54 int min_ack_delay_usecs
= DEFAULT_MIN_ACK_DELAY_USECS
;
56 static char dhcp_message
[256];
57 static int site_code_min
;
59 static int find_min_site_code(struct universe
*);
60 static isc_result_t
lowest_site_code(const void *, unsigned, void *);
62 static const char *dhcp_type_names
[] = {
73 "DHCPLEASEUNASSIGNED",
77 const int dhcp_type_name_max
= ((sizeof dhcp_type_names
) / sizeof (char *));
80 # define send_packet trace_packet_send
84 dhcp (struct packet
*packet
) {
86 struct option_cache
*oc
;
87 struct lease
*lease
= NULL
;
89 struct data_string data
;
91 if (!locate_network(packet
) &&
92 packet
->packet_type
!= DHCPREQUEST
&&
93 packet
->packet_type
!= DHCPINFORM
&&
94 packet
->packet_type
!= DHCPLEASEQUERY
) {
97 errmsg
= "unknown network segment";
100 if (packet
->packet_type
> 0 &&
101 packet
->packet_type
<= dhcp_type_name_max
) {
102 s
= dhcp_type_names
[packet
->packet_type
- 1];
104 /* %Audit% Cannot exceed 28 bytes. %2004.06.17,Safe% */
105 sprintf(typebuf
, "type %d", packet
->packet_type
);
109 log_info("%s from %s via %s: %s", s
,
111 ? print_hw_addr(packet
->raw
->htype
,
114 : "<no identifier>"),
115 packet
->raw
->giaddr
.s_addr
116 ? inet_ntoa(packet
->raw
->giaddr
)
117 : packet
->interface
->name
, errmsg
);
121 /* There is a problem with the relay agent information option,
122 * which is that in order for a normal relay agent to append
123 * this option, the relay agent has to have been involved in
124 * getting the packet from the client to the server. Note
125 * that this is the software entity known as the relay agent,
126 * _not_ the hardware entity known as a router in which the
127 * relay agent may be running, so the fact that a router has
128 * forwarded a packet does not mean that the relay agent in
129 * the router was involved.
131 * So when the client broadcasts (DHCPDISCOVER, or giaddr is set),
132 * we can be sure that there are either agent options in the
133 * packet, or there aren't supposed to be. When the giaddr is not
134 * set, it's still possible that the client is on a directly
135 * attached subnet, and agent options are being appended by an l2
136 * device that has no address, and so sets no giaddr.
138 * But in either case it's possible that the packets we receive
139 * from the client in RENEW state may not include the agent options,
140 * so if they are not in the packet we must "pretend" the last values
141 * we observed were provided.
143 if (packet
->packet_type
== DHCPREQUEST
&&
144 packet
->raw
->ciaddr
.s_addr
&& !packet
->raw
->giaddr
.s_addr
&&
145 (packet
->options
->universe_count
<= agent_universe
.index
||
146 packet
->options
->universes
[agent_universe
.index
] == NULL
))
150 cip
.len
= sizeof packet
-> raw
-> ciaddr
;
151 memcpy (cip
.iabuf
, &packet
-> raw
-> ciaddr
,
152 sizeof packet
-> raw
-> ciaddr
);
153 if (!find_lease_by_ip_addr (&lease
, cip
, MDL
))
156 /* If there are no agent options on the lease, it's not
158 if (!lease
-> agent_options
)
161 /* The client should not be unicasting a renewal if its lease
162 has expired, so make it go through the process of getting
163 its agent options legally. */
164 if (lease
-> ends
< cur_time
)
167 if (lease
-> uid_len
) {
168 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
169 DHO_DHCP_CLIENT_IDENTIFIER
);
173 memset (&data
, 0, sizeof data
);
174 if (!evaluate_option_cache (&data
,
175 packet
, (struct lease
*)0,
176 (struct client_state
*)0,
178 (struct option_state
*)0,
179 &global_scope
, oc
, MDL
))
181 if (lease
-> uid_len
!= data
.len
||
182 memcmp (lease
-> uid
, data
.data
, data
.len
)) {
183 data_string_forget (&data
, MDL
);
186 data_string_forget (&data
, MDL
);
188 if ((lease
-> hardware_addr
.hbuf
[0] !=
189 packet
-> raw
-> htype
) ||
190 (lease
-> hardware_addr
.hlen
- 1 !=
191 packet
-> raw
-> hlen
) ||
192 memcmp (&lease
-> hardware_addr
.hbuf
[1],
193 packet
-> raw
-> chaddr
,
194 packet
-> raw
-> hlen
))
197 /* Okay, so we found a lease that matches the client. */
198 option_chain_head_reference ((struct option_chain_head
**)
199 &(packet
-> options
-> universes
200 [agent_universe
.index
]),
201 lease
-> agent_options
, MDL
);
203 if (packet
->options
->universe_count
<= agent_universe
.index
)
204 packet
->options
->universe_count
=
205 agent_universe
.index
+ 1;
207 packet
->agent_options_stashed
= ISC_TRUE
;
211 /* If a client null terminates options it sends, it probably
212 * expects the server to reciprocate.
214 if ((oc
= lookup_option (&dhcp_universe
, packet
-> options
,
216 if (!oc
-> expression
)
217 ms_nulltp
= oc
->flags
& OPTION_HAD_NULLS
;
220 /* Classify the client. */
221 classify_client (packet
);
223 switch (packet
-> packet_type
) {
225 dhcpdiscover (packet
, ms_nulltp
);
229 dhcprequest (packet
, ms_nulltp
, lease
);
233 dhcprelease (packet
, ms_nulltp
);
237 dhcpdecline (packet
, ms_nulltp
);
241 dhcpinform (packet
, ms_nulltp
);
245 dhcpleasequery(packet
, ms_nulltp
);
251 case DHCPLEASEUNASSIGNED
:
252 case DHCPLEASEUNKNOWN
:
253 case DHCPLEASEACTIVE
:
257 errmsg
= "unknown packet type";
262 lease_dereference (&lease
, MDL
);
265 void dhcpdiscover (packet
, ms_nulltp
)
266 struct packet
*packet
;
269 struct lease
*lease
= (struct lease
*)0;
270 char msgbuf
[1024]; /* XXX */
273 int peer_has_leases
= 0;
274 #if defined (FAILOVER_PROTOCOL)
275 dhcp_failover_state_t
*peer
;
278 find_lease (&lease
, packet
, packet
-> shared_network
,
279 0, &peer_has_leases
, (struct lease
*)0, MDL
);
281 if (lease
&& lease
-> client_hostname
) {
282 if ((strlen (lease
-> client_hostname
) <= 64) &&
283 db_printable((unsigned char *)lease
->client_hostname
))
284 s
= lease
-> client_hostname
;
286 s
= "Hostname Unsuitable for Printing";
290 /* %Audit% This is log output. %2004.06.17,Safe%
291 * If we truncate we hope the user can get a hint from the log.
293 snprintf (msgbuf
, sizeof msgbuf
, "DHCPDISCOVER from %s %s%s%svia %s",
294 (packet
-> raw
-> htype
295 ? print_hw_addr (packet
-> raw
-> htype
,
296 packet
-> raw
-> hlen
,
297 packet
-> raw
-> chaddr
)
299 ? print_hex_1(lease
->uid_len
, lease
->uid
, 60)
300 : "<no identifier>")),
301 s
? "(" : "", s
? s
: "", s
? ") " : "",
302 packet
-> raw
-> giaddr
.s_addr
303 ? inet_ntoa (packet
-> raw
-> giaddr
)
304 : packet
-> interface
-> name
);
306 /* Sourceless packets don't make sense here. */
307 if (!packet
-> shared_network
) {
308 log_info ("Packet from unknown subnet: %s",
309 inet_ntoa (packet
-> raw
-> giaddr
));
313 #if defined (FAILOVER_PROTOCOL)
314 if (lease
&& lease
-> pool
&& lease
-> pool
-> failover_peer
) {
315 peer
= lease
-> pool
-> failover_peer
;
318 * If the lease is ours to (re)allocate, then allocate it.
320 * If the lease is active, it belongs to the client. This
321 * is the right lease, if we are to offer one. We decide
322 * whether or not to offer later on.
324 * If the lease was last active, and we've reached this
325 * point, then it was last active with the same client. We
326 * can safely re-activate the lease with this client.
328 if (lease
->binding_state
== FTS_ACTIVE
||
329 lease
->rewind_binding_state
== FTS_ACTIVE
||
330 lease_mine_to_reallocate(lease
)) {
331 ; /* This space intentionally left blank. */
333 /* Otherwise, we can't let the client have this lease. */
335 #if defined (DEBUG_FIND_LEASE)
336 log_debug ("discarding %s - %s",
337 piaddr (lease
-> ip_addr
),
338 binding_state_print (lease
-> binding_state
));
340 lease_dereference (&lease
, MDL
);
345 /* If we didn't find a lease, try to allocate one... */
347 if (!allocate_lease (&lease
, packet
,
348 packet
-> shared_network
-> pools
,
351 log_error ("%s: peer holds all free leases",
354 log_error ("%s: network %s: no free leases",
356 packet
-> shared_network
-> name
);
361 #if defined (FAILOVER_PROTOCOL)
362 if (lease
&& lease
-> pool
&& lease
-> pool
-> failover_peer
) {
363 peer
= lease
-> pool
-> failover_peer
;
364 if (peer
-> service_state
== not_responding
||
365 peer
-> service_state
== service_startup
) {
366 log_info ("%s: not responding%s",
367 msgbuf
, peer
-> nrr
);
371 peer
= (dhcp_failover_state_t
*)0;
373 /* Do load balancing if configured. */
374 if (peer
&& (peer
-> service_state
== cooperating
) &&
375 !load_balance_mine (packet
, peer
)) {
376 if (peer_has_leases
) {
377 log_debug ("%s: load balance to peer %s",
378 msgbuf
, peer
-> name
);
381 log_debug ("%s: cancel load balance to peer %s - %s",
382 msgbuf
, peer
-> name
, "no free leases");
387 /* If it's an expired lease, get rid of any bindings. */
388 if (lease
-> ends
< cur_time
&& lease
-> scope
)
389 binding_scope_dereference (&lease
-> scope
, MDL
);
391 /* Set the lease to really expire in 2 minutes, unless it has
392 not yet expired, in which case leave its expiry time alone. */
393 when
= cur_time
+ 120;
394 if (when
< lease
-> ends
)
395 when
= lease
-> ends
;
397 ack_lease (packet
, lease
, DHCPOFFER
, when
, msgbuf
, ms_nulltp
,
398 (struct host_decl
*)0);
401 lease_dereference (&lease
, MDL
);
404 void dhcprequest (packet
, ms_nulltp
, ip_lease
)
405 struct packet
*packet
;
407 struct lease
*ip_lease
;
412 struct subnet
*subnet
;
414 struct option_cache
*oc
;
415 struct data_string data
;
416 char msgbuf
[1024]; /* XXX */
419 #if defined (FAILOVER_PROTOCOL)
420 dhcp_failover_state_t
*peer
;
422 int have_server_identifier
= 0;
423 int have_requested_addr
= 0;
425 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
426 DHO_DHCP_REQUESTED_ADDRESS
);
427 memset (&data
, 0, sizeof data
);
429 evaluate_option_cache (&data
, packet
, (struct lease
*)0,
430 (struct client_state
*)0,
431 packet
-> options
, (struct option_state
*)0,
432 &global_scope
, oc
, MDL
)) {
434 memcpy (cip
.iabuf
, data
.data
, 4);
435 data_string_forget (&data
, MDL
);
436 have_requested_addr
= 1;
438 oc
= (struct option_cache
*)0;
440 memcpy (cip
.iabuf
, &packet
-> raw
-> ciaddr
.s_addr
, 4);
443 /* Find the lease that matches the address requested by the
446 subnet
= (struct subnet
*)0;
447 lease
= (struct lease
*)0;
448 if (find_subnet (&subnet
, cip
, MDL
))
449 find_lease (&lease
, packet
,
450 subnet
-> shared_network
, &ours
, 0, ip_lease
, MDL
);
452 if (lease
&& lease
-> client_hostname
) {
453 if ((strlen (lease
-> client_hostname
) <= 64) &&
454 db_printable((unsigned char *)lease
->client_hostname
))
455 s
= lease
-> client_hostname
;
457 s
= "Hostname Unsuitable for Printing";
461 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
462 DHO_DHCP_SERVER_IDENTIFIER
);
463 memset (&data
, 0, sizeof data
);
465 evaluate_option_cache (&data
, packet
, (struct lease
*)0,
466 (struct client_state
*)0,
467 packet
-> options
, (struct option_state
*)0,
468 &global_scope
, oc
, MDL
)) {
470 memcpy (sip
.iabuf
, data
.data
, 4);
471 data_string_forget (&data
, MDL
);
472 /* piaddr() should not return more than a 15 byte string.
475 sprintf (smbuf
, " (%s)", piaddr (sip
));
476 have_server_identifier
= 1;
480 /* %Audit% This is log output. %2004.06.17,Safe%
481 * If we truncate we hope the user can get a hint from the log.
483 snprintf (msgbuf
, sizeof msgbuf
,
484 "DHCPREQUEST for %s%s from %s %s%s%svia %s",
486 (packet
-> raw
-> htype
487 ? print_hw_addr (packet
-> raw
-> htype
,
488 packet
-> raw
-> hlen
,
489 packet
-> raw
-> chaddr
)
491 ? print_hex_1(lease
->uid_len
, lease
->uid
, 60)
492 : "<no identifier>")),
493 s
? "(" : "", s
? s
: "", s
? ") " : "",
494 packet
-> raw
-> giaddr
.s_addr
495 ? inet_ntoa (packet
-> raw
-> giaddr
)
496 : packet
-> interface
-> name
);
498 #if defined (FAILOVER_PROTOCOL)
499 if (lease
&& lease
-> pool
&& lease
-> pool
-> failover_peer
) {
500 peer
= lease
-> pool
-> failover_peer
;
501 if (peer
-> service_state
== not_responding
||
502 peer
-> service_state
== service_startup
) {
503 log_info ("%s: not responding%s",
504 msgbuf
, peer
-> nrr
);
508 /* "load balance to peer" - is not done at all for request.
510 * If it's RENEWING, we are the only server to hear it, so
511 * we have to serve it. If it's REBINDING, it's out of
512 * communication with the other server, so there's no point
513 * in waiting to serve it. However, if the lease we're
514 * offering is not a free lease, then we may be the only
515 * server that can offer it, so we can't load balance if
516 * the lease isn't in the free or backup state. If it is
517 * in the free or backup state, then that state is what
518 * mandates one server or the other should perform the
519 * allocation, not the LBA...we know the peer cannot
520 * allocate a request for an address in our free state.
522 * So our only compass is lease_mine_to_reallocate(). This
523 * effects both load balancing, and a sanity-check that we
524 * are not going to try to allocate a lease that isn't ours.
526 if ((lease
-> binding_state
== FTS_FREE
||
527 lease
-> binding_state
== FTS_BACKUP
) &&
528 !lease_mine_to_reallocate (lease
)) {
529 log_debug ("%s: lease owned by peer", msgbuf
);
534 * If the lease is in a transitional state, we can't
535 * renew it unless we can rewind it to a non-transitional
536 * state (active, free, or backup). lease_mine_to_reallocate()
537 * checks for free/backup, so we only need to check for active.
539 if ((lease
->binding_state
== FTS_RELEASED
||
540 lease
->binding_state
== FTS_EXPIRED
) &&
541 lease
->rewind_binding_state
!= FTS_ACTIVE
&&
542 !lease_mine_to_reallocate(lease
)) {
543 log_debug("%s: lease in transition state %s", msgbuf
,
544 (lease
->binding_state
== FTS_RELEASED
)
545 ? "released" : "expired");
549 /* It's actually very unlikely that we'll ever get here,
550 but if we do, tell the client to stop using the lease,
551 because the administrator reset it. */
552 if (lease
-> binding_state
== FTS_RESET
&&
553 !lease_mine_to_reallocate (lease
)) {
554 log_debug ("%s: lease reset by administrator", msgbuf
);
555 nak_lease (packet
, &cip
);
559 /* At this point it's possible that we will get a broadcast
560 DHCPREQUEST for a lease that we didn't offer, because
561 both we and the peer are in a position to offer it.
562 In that case, we probably shouldn't answer. In order
563 to not answer, we would have to compare the server
564 identifier sent by the client with the list of possible
565 server identifiers we can send, and if the client's
566 identifier isn't on the list, drop the DHCPREQUEST.
567 We aren't currently doing that for two reasons - first,
568 it's not clear that all clients do the right thing
569 with respect to sending the client identifier, which
570 could mean that we might simply not respond to a client
571 that is depending on us to respond. Secondly, we allow
572 the user to specify the server identifier to send, and
573 we don't enforce that the server identifier should be
574 one of our IP addresses. This is probably not a big
575 deal, but it's theoretically an issue.
577 The reason we care about this is that if both servers
578 send a DHCPACK to the DHCPREQUEST, they are then going
579 to send dueling BNDUPD messages, which could cause
580 trouble. I think it causes no harm, but it seems
583 peer
= (dhcp_failover_state_t
*)0;
586 /* If a client on a given network REQUESTs a lease on an
587 address on a different network, NAK it. If the Requested
588 Address option was used, the protocol says that it must
589 have been broadcast, so we can trust the source network
592 If ciaddr was specified and Requested Address was not, then
593 we really only know for sure what network a packet came from
594 if it came through a BOOTP gateway - if it came through an
595 IP router, we'll just have to assume that it's cool.
597 If we don't think we know where the packet came from, it
598 came through a gateway from an unknown network, so it's not
599 from a RENEWING client. If we recognize the network it
600 *thinks* it's on, we can NAK it even though we don't
601 recognize the network it's *actually* on; otherwise we just
604 We don't currently try to take advantage of access to the
605 raw packet, because it's not available on all platforms.
606 So a packet that was unicast to us through a router from a
607 RENEWING client is going to look exactly like a packet that
608 was broadcast to us from an INIT-REBOOT client.
610 Since we can't tell the difference between these two kinds
611 of packets, if the packet appears to have come in off the
612 local wire, we have to treat it as if it's a RENEWING
613 client. This means that we can't NAK a RENEWING client on
614 the local wire that has a bogus address. The good news is
615 that we won't ACK it either, so it should revert to INIT
616 state and send us a DHCPDISCOVER, which we *can* work with.
618 Because we can't detect that a RENEWING client is on the
619 wrong wire, it's going to sit there trying to renew until
620 it gets to the REBIND state, when we *can* NAK it because
621 the packet will get to us through a BOOTP gateway. We
622 shouldn't actually see DHCPREQUEST packets from RENEWING
623 clients on the wrong wire anyway, since their idea of their
624 local router will be wrong. In any case, the protocol
625 doesn't really allow us to NAK a DHCPREQUEST from a
626 RENEWING client, so we can punt on this issue. */
628 if (!packet
-> shared_network
||
629 (packet
-> raw
-> ciaddr
.s_addr
&&
630 packet
-> raw
-> giaddr
.s_addr
) ||
631 (have_requested_addr
&& !packet
-> raw
-> ciaddr
.s_addr
)) {
633 /* If we don't know where it came from but we do know
634 where it claims to have come from, it didn't come
636 if (!packet
-> shared_network
) {
637 if (subnet
&& subnet
-> group
-> authoritative
) {
638 log_info ("%s: wrong network.", msgbuf
);
639 nak_lease (packet
, &cip
);
642 /* Otherwise, ignore it. */
643 log_info ("%s: ignored (%s).", msgbuf
,
645 ? "not authoritative" : "unknown subnet"));
649 /* If we do know where it came from and it asked for an
650 address that is not on that shared network, nak it. */
652 subnet_dereference (&subnet
, MDL
);
653 if (!find_grouped_subnet (&subnet
, packet
-> shared_network
,
655 if (packet
-> shared_network
-> group
-> authoritative
)
657 log_info ("%s: wrong network.", msgbuf
);
658 nak_lease (packet
, &cip
);
661 log_info ("%s: ignored (not authoritative).", msgbuf
);
666 /* If the address the client asked for is ours, but it wasn't
667 available for the client, NAK it. */
668 if (!lease
&& ours
) {
669 log_info ("%s: lease %s unavailable.", msgbuf
, piaddr (cip
));
670 nak_lease (packet
, &cip
);
674 /* Otherwise, send the lease to the client if we found one. */
676 ack_lease (packet
, lease
, DHCPACK
, 0, msgbuf
, ms_nulltp
,
677 (struct host_decl
*)0);
679 log_info ("%s: unknown lease %s.", msgbuf
, piaddr (cip
));
683 subnet_dereference (&subnet
, MDL
);
685 lease_dereference (&lease
, MDL
);
689 void dhcprelease (packet
, ms_nulltp
)
690 struct packet
*packet
;
693 struct lease
*lease
= (struct lease
*)0, *next
= (struct lease
*)0;
695 struct option_cache
*oc
;
696 struct data_string data
;
698 char msgbuf
[1024], cstr
[16]; /* XXX */
701 /* DHCPRELEASE must not specify address in requested-address
702 option, but old protocol specs weren't explicit about this,
704 if ((oc
= lookup_option (&dhcp_universe
, packet
-> options
,
705 DHO_DHCP_REQUESTED_ADDRESS
))) {
706 log_info ("DHCPRELEASE from %s specified requested-address.",
707 print_hw_addr (packet
-> raw
-> htype
,
708 packet
-> raw
-> hlen
,
709 packet
-> raw
-> chaddr
));
712 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
713 DHO_DHCP_CLIENT_IDENTIFIER
);
714 memset (&data
, 0, sizeof data
);
716 evaluate_option_cache (&data
, packet
, (struct lease
*)0,
717 (struct client_state
*)0,
718 packet
-> options
, (struct option_state
*)0,
719 &global_scope
, oc
, MDL
)) {
720 find_lease_by_uid (&lease
, data
.data
, data
.len
, MDL
);
721 data_string_forget (&data
, MDL
);
723 /* See if we can find a lease that matches the IP address
724 the client is claiming. */
727 lease_reference (&next
, lease
-> n_uid
, MDL
);
728 if (!memcmp (&packet
-> raw
-> ciaddr
,
729 lease
-> ip_addr
.iabuf
, 4)) {
732 lease_dereference (&lease
, MDL
);
734 lease_reference (&lease
, next
, MDL
);
735 lease_dereference (&next
, MDL
);
739 lease_dereference (&next
, MDL
);
742 /* The client is supposed to pass a valid client-identifier,
743 but the spec on this has changed historically, so try the
744 IP address in ciaddr if the client-identifier fails. */
747 memcpy (cip
.iabuf
, &packet
-> raw
-> ciaddr
, 4);
748 find_lease_by_ip_addr (&lease
, cip
, MDL
);
752 /* If the hardware address doesn't match, don't do the release. */
754 (lease
-> hardware_addr
.hlen
!= packet
-> raw
-> hlen
+ 1 ||
755 lease
-> hardware_addr
.hbuf
[0] != packet
-> raw
-> htype
||
756 memcmp (&lease
-> hardware_addr
.hbuf
[1],
757 packet
-> raw
-> chaddr
, packet
-> raw
-> hlen
)))
758 lease_dereference (&lease
, MDL
);
760 if (lease
&& lease
-> client_hostname
) {
761 if ((strlen (lease
-> client_hostname
) <= 64) &&
762 db_printable((unsigned char *)lease
->client_hostname
))
763 s
= lease
-> client_hostname
;
765 s
= "Hostname Unsuitable for Printing";
769 /* %Audit% Cannot exceed 16 bytes. %2004.06.17,Safe%
770 * We copy this out to stack because we actually want to log two
771 * inet_ntoa()'s in this message.
773 strncpy(cstr
, inet_ntoa (packet
-> raw
-> ciaddr
), 15);
776 /* %Audit% This is log output. %2004.06.17,Safe%
777 * If we truncate we hope the user can get a hint from the log.
779 snprintf (msgbuf
, sizeof msgbuf
,
780 "DHCPRELEASE of %s from %s %s%s%svia %s (%sfound)",
782 (packet
-> raw
-> htype
783 ? print_hw_addr (packet
-> raw
-> htype
,
784 packet
-> raw
-> hlen
,
785 packet
-> raw
-> chaddr
)
787 ? print_hex_1(lease
->uid_len
, lease
->uid
, 60)
788 : "<no identifier>")),
789 s
? "(" : "", s
? s
: "", s
? ") " : "",
790 packet
-> raw
-> giaddr
.s_addr
791 ? inet_ntoa (packet
-> raw
-> giaddr
)
792 : packet
-> interface
-> name
,
793 lease
? "" : "not ");
795 #if defined (FAILOVER_PROTOCOL)
796 if (lease
&& lease
-> pool
&& lease
-> pool
-> failover_peer
) {
797 dhcp_failover_state_t
*peer
= lease
-> pool
-> failover_peer
;
798 if (peer
-> service_state
== not_responding
||
799 peer
-> service_state
== service_startup
) {
800 log_info ("%s: ignored%s",
801 peer
-> name
, peer
-> nrr
);
805 /* DHCPRELEASE messages are unicast, so if the client
806 sent the DHCPRELEASE to us, it's not going to send it
807 to the peer. Not sure why this would happen, and
808 if it does happen I think we still have to change the
809 lease state, so that's what we're doing.
810 XXX See what it says in the draft about this. */
814 /* If we found a lease, release it. */
815 if (lease
&& lease
-> ends
> cur_time
) {
816 release_lease (lease
, packet
);
818 log_info ("%s", msgbuf
);
819 #if defined(FAILOVER_PROTOCOL)
823 lease_dereference (&lease
, MDL
);
826 void dhcpdecline (packet
, ms_nulltp
)
827 struct packet
*packet
;
830 struct lease
*lease
= (struct lease
*)0;
831 struct option_state
*options
= (struct option_state
*)0;
836 char msgbuf
[1024]; /* XXX */
838 struct option_cache
*oc
;
839 struct data_string data
;
841 /* DHCPDECLINE must specify address. */
842 if (!(oc
= lookup_option (&dhcp_universe
, packet
-> options
,
843 DHO_DHCP_REQUESTED_ADDRESS
)))
845 memset (&data
, 0, sizeof data
);
846 if (!evaluate_option_cache (&data
, packet
, (struct lease
*)0,
847 (struct client_state
*)0,
849 (struct option_state
*)0,
850 &global_scope
, oc
, MDL
))
854 memcpy (cip
.iabuf
, data
.data
, 4);
855 data_string_forget (&data
, MDL
);
856 find_lease_by_ip_addr (&lease
, cip
, MDL
);
858 if (lease
&& lease
-> client_hostname
) {
859 if ((strlen (lease
-> client_hostname
) <= 64) &&
860 db_printable((unsigned char *)lease
->client_hostname
))
861 s
= lease
-> client_hostname
;
863 s
= "Hostname Unsuitable for Printing";
867 /* %Audit% This is log output. %2004.06.17,Safe%
868 * If we truncate we hope the user can get a hint from the log.
870 snprintf (msgbuf
, sizeof msgbuf
,
871 "DHCPDECLINE of %s from %s %s%s%svia %s",
873 (packet
-> raw
-> htype
874 ? print_hw_addr (packet
-> raw
-> htype
,
875 packet
-> raw
-> hlen
,
876 packet
-> raw
-> chaddr
)
878 ? print_hex_1(lease
->uid_len
, lease
->uid
, 60)
879 : "<no identifier>")),
880 s
? "(" : "", s
? s
: "", s
? ") " : "",
881 packet
-> raw
-> giaddr
.s_addr
882 ? inet_ntoa (packet
-> raw
-> giaddr
)
883 : packet
-> interface
-> name
);
885 option_state_allocate (&options
, MDL
);
887 /* Execute statements in scope starting with the subnet scope. */
889 execute_statements_in_scope ((struct binding_value
**)0,
890 packet
, (struct lease
*)0,
891 (struct client_state
*)0,
892 packet
-> options
, options
,
894 lease
-> subnet
-> group
,
897 /* Execute statements in the class scopes. */
898 for (i
= packet
-> class_count
; i
> 0; i
--) {
899 execute_statements_in_scope
900 ((struct binding_value
**)0, packet
, (struct lease
*)0,
901 (struct client_state
*)0, packet
-> options
, options
,
902 &global_scope
, packet
-> classes
[i
- 1] -> group
,
903 lease
? lease
-> subnet
-> group
: (struct group
*)0);
906 /* Drop the request if dhcpdeclines are being ignored. */
907 oc
= lookup_option (&server_universe
, options
, SV_DECLINES
);
909 evaluate_boolean_option_cache (&ignorep
, packet
, lease
,
910 (struct client_state
*)0,
911 packet
-> options
, options
,
912 &lease
-> scope
, oc
, MDL
)) {
913 /* If we found a lease, mark it as unusable and complain. */
915 #if defined (FAILOVER_PROTOCOL)
916 if (lease
-> pool
&& lease
-> pool
-> failover_peer
) {
917 dhcp_failover_state_t
*peer
=
918 lease
-> pool
-> failover_peer
;
919 if (peer
-> service_state
== not_responding
||
920 peer
-> service_state
== service_startup
) {
922 log_info ("%s: ignored%s",
923 peer
-> name
, peer
-> nrr
);
927 /* DHCPDECLINE messages are broadcast, so we can safely
928 ignore the DHCPDECLINE if the peer has the lease.
929 XXX Of course, at this point that information has been
934 abandon_lease (lease
, "declined.");
935 status
= "abandoned";
937 status
= "not found";
943 log_info ("%s: %s", msgbuf
, status
);
945 #if defined(FAILOVER_PROTOCOL)
949 option_state_dereference (&options
, MDL
);
951 lease_dereference (&lease
, MDL
);
954 void dhcpinform (packet
, ms_nulltp
)
955 struct packet
*packet
;
959 struct data_string d1
, prl
;
960 struct option_cache
*oc
;
961 struct option_state
*options
= (struct option_state
*)0;
962 struct dhcp_packet raw
;
963 struct packet outgoing
;
964 unsigned char dhcpack
= DHCPACK
;
965 struct subnet
*subnet
= NULL
;
966 struct iaddr cip
, gip
;
969 struct sockaddr_in to
;
971 isc_boolean_t zeroed_ciaddr
;
973 /* The client should set ciaddr to its IP address, but apparently
974 it's common for clients not to do this, so we'll use their IP
975 source address if they didn't set ciaddr. */
976 if (!packet
-> raw
-> ciaddr
.s_addr
) {
977 zeroed_ciaddr
= ISC_TRUE
;
979 memcpy (cip
.iabuf
, &packet
-> client_addr
.iabuf
, 4);
981 zeroed_ciaddr
= ISC_FALSE
;
983 memcpy (cip
.iabuf
, &packet
-> raw
-> ciaddr
, 4);
986 if (packet
->raw
->giaddr
.s_addr
) {
988 memcpy(gip
.iabuf
, &packet
->raw
->giaddr
, 4);
992 /* %Audit% This is log output. %2004.06.17,Safe%
993 * If we truncate we hope the user can get a hint from the log.
995 snprintf (msgbuf
, sizeof msgbuf
, "DHCPINFORM from %s via %s",
996 piaddr (cip
), packet
->raw
->giaddr
.s_addr
?
997 inet_ntoa(packet
->raw
->giaddr
) :
998 packet
-> interface
-> name
);
1000 /* If the IP source address is zero, don't respond. */
1001 if (!memcmp (cip
.iabuf
, "\0\0\0", 4)) {
1002 log_info ("%s: ignored (null source address).", msgbuf
);
1006 /* Find the subnet that the client is on. */
1007 if (zeroed_ciaddr
&& (gip
.len
!= 0)) {
1008 /* XXX - do subnet selection relay agent suboption here */
1009 find_subnet(&subnet
, gip
, MDL
);
1011 if (subnet
== NULL
) {
1012 log_info("%s: unknown subnet for relay address %s",
1013 msgbuf
, piaddr(gip
));
1017 /* XXX - do subnet selection (not relay agent) option here */
1018 find_subnet(&subnet
, cip
, MDL
);
1020 if (subnet
== NULL
) {
1021 log_info("%s: unknown subnet for %s address %s",
1022 msgbuf
, zeroed_ciaddr
? "source" : "client",
1028 /* We don't respond to DHCPINFORM packets if we're not authoritative.
1029 It would be nice if a per-host value could override this, but
1030 there's overhead involved in checking this, so let's see how people
1032 if (subnet
&& !subnet
-> group
-> authoritative
) {
1034 log_info ("%s: not authoritative for subnet %s",
1035 msgbuf
, piaddr (subnet
-> net
));
1037 log_info ("If this DHCP server is authoritative for%s",
1039 log_info ("please write an `authoritative;' directi%s",
1040 "ve either in the");
1041 log_info ("subnet declaration or in some scope that%s",
1043 log_info ("subnet declaration - for example, write %s",
1045 log_info ("of the dhcpd.conf file.");
1049 subnet_dereference (&subnet
, MDL
);
1053 option_state_allocate (&options
, MDL
);
1054 memset (&outgoing
, 0, sizeof outgoing
);
1055 memset (&raw
, 0, sizeof raw
);
1056 outgoing
.raw
= &raw
;
1058 maybe_return_agent_options(packet
, options
);
1060 /* Execute statements in scope starting with the subnet scope. */
1062 execute_statements_in_scope ((struct binding_value
**)0,
1063 packet
, (struct lease
*)0,
1064 (struct client_state
*)0,
1065 packet
-> options
, options
,
1066 &global_scope
, subnet
-> group
,
1069 /* Execute statements in the class scopes. */
1070 for (i
= packet
-> class_count
; i
> 0; i
--) {
1071 execute_statements_in_scope
1072 ((struct binding_value
**)0, packet
, (struct lease
*)0,
1073 (struct client_state
*)0, packet
-> options
, options
,
1074 &global_scope
, packet
-> classes
[i
- 1] -> group
,
1075 subnet
? subnet
-> group
: (struct group
*)0);
1078 /* Figure out the filename. */
1079 memset (&d1
, 0, sizeof d1
);
1080 oc
= lookup_option (&server_universe
, options
, SV_FILENAME
);
1082 evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
1083 (struct client_state
*)0,
1084 packet
-> options
, (struct option_state
*)0,
1085 &global_scope
, oc
, MDL
)) {
1087 if (i
>= sizeof(raw
.file
)) {
1088 log_info("file name longer than packet field "
1089 "truncated - field: %lu name: %d %.*s",
1090 (unsigned long)sizeof(raw
.file
), i
,
1092 i
= sizeof(raw
.file
);
1095 memcpy (raw
.file
, d1
.data
, i
);
1096 data_string_forget (&d1
, MDL
);
1099 /* Choose a server name as above. */
1100 oc
= lookup_option (&server_universe
, options
, SV_SERVER_NAME
);
1102 evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
1103 (struct client_state
*)0,
1104 packet
-> options
, (struct option_state
*)0,
1105 &global_scope
, oc
, MDL
)) {
1107 if (i
>= sizeof(raw
.sname
)) {
1108 log_info("server name longer than packet field "
1109 "truncated - field: %lu name: %d %.*s",
1110 (unsigned long)sizeof(raw
.sname
), i
,
1112 i
= sizeof(raw
.sname
);
1115 memcpy (raw
.sname
, d1
.data
, i
);
1116 data_string_forget (&d1
, MDL
);
1119 /* Set a flag if this client is a lame Microsoft client that NUL
1120 terminates string options and expects us to do likewise. */
1122 if ((oc
= lookup_option (&dhcp_universe
, packet
-> options
,
1124 if (!oc
->expression
)
1125 nulltp
= oc
->flags
& OPTION_HAD_NULLS
;
1128 /* Put in DHCP-specific options. */
1129 i
= DHO_DHCP_MESSAGE_TYPE
;
1130 oc
= (struct option_cache
*)0;
1131 if (option_cache_allocate (&oc
, MDL
)) {
1132 if (make_const_data (&oc
-> expression
,
1133 &dhcpack
, 1, 0, 0, MDL
)) {
1134 option_code_hash_lookup(&oc
->option
,
1135 dhcp_universe
.code_hash
,
1137 save_option (&dhcp_universe
, options
, oc
);
1139 option_cache_dereference (&oc
, MDL
);
1142 get_server_source_address(&from
, options
, packet
);
1144 /* Use the subnet mask from the subnet declaration if no other
1145 mask has been provided. */
1146 i
= DHO_SUBNET_MASK
;
1147 if (subnet
&& !lookup_option (&dhcp_universe
, options
, i
)) {
1148 oc
= (struct option_cache
*)0;
1149 if (option_cache_allocate (&oc
, MDL
)) {
1150 if (make_const_data (&oc
-> expression
,
1151 subnet
-> netmask
.iabuf
,
1152 subnet
-> netmask
.len
,
1154 option_code_hash_lookup(&oc
->option
,
1155 dhcp_universe
.code_hash
,
1157 save_option (&dhcp_universe
, options
, oc
);
1159 option_cache_dereference (&oc
, MDL
);
1163 /* If a site option space has been specified, use that for
1164 site option codes. */
1165 i
= SV_SITE_OPTION_SPACE
;
1166 if ((oc
= lookup_option (&server_universe
, options
, i
)) &&
1167 evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
1168 (struct client_state
*)0,
1169 packet
-> options
, options
,
1170 &global_scope
, oc
, MDL
)) {
1171 struct universe
*u
= (struct universe
*)0;
1173 if (!universe_hash_lookup (&u
, universe_hash
,
1174 (const char *)d1
.data
, d1
.len
,
1176 log_error ("unknown option space %s.", d1
.data
);
1177 option_state_dereference (&options
, MDL
);
1179 subnet_dereference (&subnet
, MDL
);
1183 options
-> site_universe
= u
-> index
;
1184 options
->site_code_min
= find_min_site_code(u
);
1185 data_string_forget (&d1
, MDL
);
1187 options
-> site_universe
= dhcp_universe
.index
;
1188 options
-> site_code_min
= 0; /* Trust me, it works. */
1191 memset (&prl
, 0, sizeof prl
);
1193 /* Use the parameter list from the scope if there is one. */
1194 oc
= lookup_option (&dhcp_universe
, options
,
1195 DHO_DHCP_PARAMETER_REQUEST_LIST
);
1197 /* Otherwise, if the client has provided a list of options
1198 that it wishes returned, use it to prioritize. Otherwise,
1199 prioritize based on the default priority list. */
1202 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
1203 DHO_DHCP_PARAMETER_REQUEST_LIST
);
1206 evaluate_option_cache (&prl
, packet
, (struct lease
*)0,
1207 (struct client_state
*)0,
1208 packet
-> options
, options
,
1209 &global_scope
, oc
, MDL
);
1212 dump_packet (packet
);
1213 dump_raw ((unsigned char *)packet
-> raw
, packet
-> packet_length
);
1216 log_info ("%s", msgbuf
);
1218 /* Figure out the address of the boot file server. */
1220 lookup_option (&server_universe
, options
, SV_NEXT_SERVER
))) {
1221 if (evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
1222 (struct client_state
*)0,
1223 packet
-> options
, options
,
1224 &global_scope
, oc
, MDL
)) {
1225 /* If there was more than one answer,
1227 if (d1
.len
>= 4 && d1
.data
)
1228 memcpy (&raw
.siaddr
, d1
.data
, 4);
1229 data_string_forget (&d1
, MDL
);
1234 * Remove any time options, per section 3.4 RFC 2131
1236 delete_option(&dhcp_universe
, options
, DHO_DHCP_LEASE_TIME
);
1237 delete_option(&dhcp_universe
, options
, DHO_DHCP_RENEWAL_TIME
);
1238 delete_option(&dhcp_universe
, options
, DHO_DHCP_REBINDING_TIME
);
1240 /* Set up the option buffer... */
1241 outgoing
.packet_length
=
1242 cons_options (packet
, outgoing
.raw
, (struct lease
*)0,
1243 (struct client_state
*)0,
1244 0, packet
-> options
, options
, &global_scope
,
1246 prl
.len
? &prl
: (struct data_string
*)0,
1248 option_state_dereference (&options
, MDL
);
1249 data_string_forget (&prl
, MDL
);
1251 /* Make sure that the packet is at least as big as a BOOTP packet. */
1252 if (outgoing
.packet_length
< BOOTP_MIN_LEN
)
1253 outgoing
.packet_length
= BOOTP_MIN_LEN
;
1255 raw
.giaddr
= packet
-> raw
-> giaddr
;
1256 raw
.ciaddr
= packet
-> raw
-> ciaddr
;
1257 memcpy (raw
.chaddr
, packet
-> raw
-> chaddr
, sizeof raw
.chaddr
);
1258 raw
.hlen
= packet
-> raw
-> hlen
;
1259 raw
.htype
= packet
-> raw
-> htype
;
1261 raw
.xid
= packet
-> raw
-> xid
;
1262 raw
.secs
= packet
-> raw
-> secs
;
1263 raw
.flags
= packet
-> raw
-> flags
;
1264 raw
.hops
= packet
-> raw
-> hops
;
1268 dump_packet (&outgoing
);
1269 dump_raw ((unsigned char *)&raw
, outgoing
.packet_length
);
1272 /* Set up the common stuff... */
1273 to
.sin_family
= AF_INET
;
1275 to
.sin_len
= sizeof to
;
1277 memset (to
.sin_zero
, 0, sizeof to
.sin_zero
);
1279 /* RFC2131 states the server SHOULD unciast to ciaddr.
1280 * There are two wrinkles - relays, and when ciaddr is zero.
1281 * There's actually no mention of relays at all in rfc2131 in
1282 * regard to DHCPINFORM, except to say we might get packets from
1283 * clients via them. Note: relays unicast to clients to the
1284 * "yiaddr" address, which servers are forbidden to set when
1285 * answering an inform.
1287 * The solution: If ciaddr is zero, and giaddr is set, go via the
1288 * relay with the broadcast flag set to help the relay (with no
1289 * yiaddr and very likely no chaddr, it will have no idea where to
1292 * If the ciaddr is zero and giaddr is not set, go via the source
1293 * IP address (but you are permitted to barf on their shoes).
1295 * If ciaddr is not zero, send the packet there always.
1297 if (!raw
.ciaddr
.s_addr
&& gip
.len
) {
1298 memcpy(&to
.sin_addr
, gip
.iabuf
, 4);
1299 to
.sin_port
= local_port
;
1300 raw
.flags
|= htons(BOOTP_BROADCAST
);
1303 memcpy(&to
.sin_addr
, cip
.iabuf
, 4);
1304 to
.sin_port
= remote_port
;
1307 /* Report what we're sending. */
1308 snprintf(msgbuf
, sizeof msgbuf
, "DHCPACK to %s (%s) via", piaddr(cip
),
1309 (packet
->raw
->htype
&& packet
->raw
->hlen
) ?
1310 print_hw_addr(packet
->raw
->htype
, packet
->raw
->hlen
,
1311 packet
->raw
->chaddr
) :
1312 "<no client hardware address>");
1313 log_info("%s %s", msgbuf
, gip
.len
? piaddr(gip
) :
1314 packet
->interface
->name
);
1317 send_packet ((fallback_interface
1318 ? fallback_interface
: packet
-> interface
),
1319 &outgoing
, &raw
, outgoing
.packet_length
,
1320 from
, &to
, (struct hardware
*)0);
1322 subnet_dereference (&subnet
, MDL
);
1325 void nak_lease (packet
, cip
)
1326 struct packet
*packet
;
1329 struct sockaddr_in to
;
1330 struct in_addr from
;
1332 struct dhcp_packet raw
;
1333 unsigned char nak
= DHCPNAK
;
1334 struct packet outgoing
;
1336 struct option_state
*options
= (struct option_state
*)0;
1337 struct option_cache
*oc
= (struct option_cache
*)0;
1339 option_state_allocate (&options
, MDL
);
1340 memset (&outgoing
, 0, sizeof outgoing
);
1341 memset (&raw
, 0, sizeof raw
);
1342 outgoing
.raw
= &raw
;
1344 /* Set DHCP_MESSAGE_TYPE to DHCPNAK */
1345 if (!option_cache_allocate (&oc
, MDL
)) {
1346 log_error ("No memory for DHCPNAK message type.");
1347 option_state_dereference (&options
, MDL
);
1350 if (!make_const_data (&oc
-> expression
, &nak
, sizeof nak
,
1352 log_error ("No memory for expr_const expression.");
1353 option_cache_dereference (&oc
, MDL
);
1354 option_state_dereference (&options
, MDL
);
1357 i
= DHO_DHCP_MESSAGE_TYPE
;
1358 option_code_hash_lookup(&oc
->option
, dhcp_universe
.code_hash
,
1360 save_option (&dhcp_universe
, options
, oc
);
1361 option_cache_dereference (&oc
, MDL
);
1363 /* Set DHCP_MESSAGE to whatever the message is */
1364 if (!option_cache_allocate (&oc
, MDL
)) {
1365 log_error ("No memory for DHCPNAK message type.");
1366 option_state_dereference (&options
, MDL
);
1369 if (!make_const_data (&oc
-> expression
,
1370 (unsigned char *)dhcp_message
,
1371 strlen (dhcp_message
), 1, 0, MDL
)) {
1372 log_error ("No memory for expr_const expression.");
1373 option_cache_dereference (&oc
, MDL
);
1374 option_state_dereference (&options
, MDL
);
1377 i
= DHO_DHCP_MESSAGE
;
1378 option_code_hash_lookup(&oc
->option
, dhcp_universe
.code_hash
,
1380 save_option (&dhcp_universe
, options
, oc
);
1381 option_cache_dereference (&oc
, MDL
);
1383 get_server_source_address(&from
, options
, packet
);
1385 /* If there were agent options in the incoming packet, return
1386 * them. We do not check giaddr to detect the presence of a
1387 * relay, as this excludes "l2" relay agents which have no
1390 if (packet
->options
->universe_count
> agent_universe
.index
&&
1391 packet
->options
->universes
[agent_universe
.index
]) {
1392 option_chain_head_reference
1393 ((struct option_chain_head
**)
1394 &(options
-> universes
[agent_universe
.index
]),
1395 (struct option_chain_head
*)
1396 packet
-> options
-> universes
[agent_universe
.index
],
1400 /* Do not use the client's requested parameter list. */
1401 delete_option (&dhcp_universe
, packet
-> options
,
1402 DHO_DHCP_PARAMETER_REQUEST_LIST
);
1404 /* Set up the option buffer... */
1405 outgoing
.packet_length
=
1406 cons_options (packet
, outgoing
.raw
, (struct lease
*)0,
1407 (struct client_state
*)0,
1408 0, packet
-> options
, options
, &global_scope
,
1409 0, 0, 0, (struct data_string
*)0, (char *)0);
1410 option_state_dereference (&options
, MDL
);
1412 /* memset (&raw.ciaddr, 0, sizeof raw.ciaddr);*/
1413 if (packet
->interface
->address_count
)
1414 raw
.siaddr
= packet
->interface
->addresses
[0];
1415 raw
.giaddr
= packet
-> raw
-> giaddr
;
1416 memcpy (raw
.chaddr
, packet
-> raw
-> chaddr
, sizeof raw
.chaddr
);
1417 raw
.hlen
= packet
-> raw
-> hlen
;
1418 raw
.htype
= packet
-> raw
-> htype
;
1420 raw
.xid
= packet
-> raw
-> xid
;
1421 raw
.secs
= packet
-> raw
-> secs
;
1422 raw
.flags
= packet
-> raw
-> flags
| htons (BOOTP_BROADCAST
);
1423 raw
.hops
= packet
-> raw
-> hops
;
1426 /* Report what we're sending... */
1427 log_info ("DHCPNAK on %s to %s via %s",
1429 print_hw_addr (packet
-> raw
-> htype
,
1430 packet
-> raw
-> hlen
,
1431 packet
-> raw
-> chaddr
),
1432 packet
-> raw
-> giaddr
.s_addr
1433 ? inet_ntoa (packet
-> raw
-> giaddr
)
1434 : packet
-> interface
-> name
);
1437 dump_packet (packet
);
1438 dump_raw ((unsigned char *)packet
-> raw
, packet
-> packet_length
);
1439 dump_packet (&outgoing
);
1440 dump_raw ((unsigned char *)&raw
, outgoing
.packet_length
);
1443 /* Set up the common stuff... */
1444 to
.sin_family
= AF_INET
;
1446 to
.sin_len
= sizeof to
;
1448 memset (to
.sin_zero
, 0, sizeof to
.sin_zero
);
1450 /* Make sure that the packet is at least as big as a BOOTP packet. */
1451 if (outgoing
.packet_length
< BOOTP_MIN_LEN
)
1452 outgoing
.packet_length
= BOOTP_MIN_LEN
;
1454 /* If this was gatewayed, send it back to the gateway.
1455 Otherwise, broadcast it on the local network. */
1456 if (raw
.giaddr
.s_addr
) {
1457 to
.sin_addr
= raw
.giaddr
;
1458 if (raw
.giaddr
.s_addr
!= htonl (INADDR_LOOPBACK
))
1459 to
.sin_port
= local_port
;
1461 to
.sin_port
= remote_port
; /* for testing. */
1463 if (fallback_interface
) {
1464 result
= send_packet(fallback_interface
, packet
, &raw
,
1465 outgoing
.packet_length
, from
, &to
,
1470 to
.sin_addr
= limited_broadcast
;
1471 to
.sin_port
= remote_port
;
1475 result
= send_packet(packet
->interface
, packet
, &raw
,
1476 outgoing
.packet_length
, from
, &to
, NULL
);
1479 void ack_lease (packet
, lease
, offer
, when
, msg
, ms_nulltp
, hp
)
1480 struct packet
*packet
;
1481 struct lease
*lease
;
1486 struct host_decl
*hp
;
1489 struct lease_state
*state
;
1491 struct host_decl
*host
= (struct host_decl
*)0;
1493 TIME offered_lease_time
;
1494 struct data_string d1
;
1495 TIME min_lease_time
;
1496 TIME max_lease_time
;
1497 TIME default_lease_time
;
1498 struct option_cache
*oc
;
1499 isc_result_t result
;
1502 struct in_addr from
;
1503 TIME remaining_time
;
1511 /* If we're already acking this lease, don't do it again. */
1515 /* Save original cltt for comparison later. */
1516 lease_cltt
= lease
->cltt
;
1518 /* If the lease carries a host record, remember it. */
1520 host_reference (&host
, hp
, MDL
);
1521 else if (lease
-> host
)
1522 host_reference (&host
, lease
-> host
, MDL
);
1524 /* Allocate a lease state structure... */
1525 state
= new_lease_state (MDL
);
1527 log_fatal ("unable to allocate lease state!");
1528 state
-> got_requested_address
= packet
-> got_requested_address
;
1529 shared_network_reference (&state
-> shared_network
,
1530 packet
-> interface
-> shared_network
, MDL
);
1532 /* See if we got a server identifier option. */
1533 if (lookup_option (&dhcp_universe
,
1534 packet
-> options
, DHO_DHCP_SERVER_IDENTIFIER
))
1535 state
-> got_server_identifier
= 1;
1537 maybe_return_agent_options(packet
, state
->options
);
1539 /* If we are offering a lease that is still currently valid, preserve
1540 the events. We need to do this because if the client does not
1541 REQUEST our offer, it will expire in 2 minutes, overriding the
1542 expire time in the currently in force lease. We want the expire
1543 events to be executed at that point. */
1544 if (lease
-> ends
<= cur_time
&& offer
!= DHCPOFFER
) {
1545 /* Get rid of any old expiry or release statements - by
1546 executing the statements below, we will be inserting new
1547 ones if there are any to insert. */
1548 if (lease
-> on_expiry
)
1549 executable_statement_dereference (&lease
-> on_expiry
,
1551 if (lease
-> on_commit
)
1552 executable_statement_dereference (&lease
-> on_commit
,
1554 if (lease
-> on_release
)
1555 executable_statement_dereference (&lease
-> on_release
,
1559 /* Execute statements in scope starting with the subnet scope. */
1560 execute_statements_in_scope ((struct binding_value
**)0,
1561 packet
, lease
, (struct client_state
*)0,
1563 state
-> options
, &lease
-> scope
,
1564 lease
-> subnet
-> group
,
1567 /* If the lease is from a pool, run the pool scope. */
1569 (execute_statements_in_scope
1570 ((struct binding_value
**)0, packet
, lease
,
1571 (struct client_state
*)0, packet
-> options
,
1572 state
-> options
, &lease
-> scope
, lease
-> pool
-> group
,
1573 lease
-> pool
-> shared_network
-> group
));
1575 /* Execute statements from class scopes. */
1576 for (i
= packet
-> class_count
; i
> 0; i
--) {
1577 execute_statements_in_scope
1578 ((struct binding_value
**)0,
1579 packet
, lease
, (struct client_state
*)0,
1580 packet
-> options
, state
-> options
,
1581 &lease
-> scope
, packet
-> classes
[i
- 1] -> group
,
1583 ? lease
-> pool
-> group
1584 : lease
-> subnet
-> group
));
1587 /* See if the client is only supposed to have one lease at a time,
1588 and if so, find its other leases and release them. We can only
1589 do this on DHCPREQUEST. It's a little weird to do this before
1590 looking at permissions, because the client might not actually
1591 _get_ a lease after we've done the permission check, but the
1592 assumption for this option is that the client has exactly one
1593 network interface, and will only ever remember one lease. So
1594 if it sends a DHCPREQUEST, and doesn't get the lease, it's already
1595 forgotten about its old lease, so we can too. */
1596 if (packet
-> packet_type
== DHCPREQUEST
&&
1597 (oc
= lookup_option (&server_universe
, state
-> options
,
1598 SV_ONE_LEASE_PER_CLIENT
)) &&
1599 evaluate_boolean_option_cache (&ignorep
,
1601 (struct client_state
*)0,
1603 state
-> options
, &lease
-> scope
,
1606 if (lease
-> uid_len
) {
1608 seek
= (struct lease
*)0;
1609 find_lease_by_uid (&seek
, lease
-> uid
,
1610 lease
-> uid_len
, MDL
);
1613 if (seek
== lease
&& !seek
-> n_uid
) {
1614 lease_dereference (&seek
, MDL
);
1617 next
= (struct lease
*)0;
1619 /* Don't release expired leases, and don't
1620 release the lease we're going to assign. */
1621 next
= (struct lease
*)0;
1624 lease_reference (&next
, seek
-> n_uid
, MDL
);
1625 if (seek
!= lease
&&
1626 seek
-> binding_state
!= FTS_RELEASED
&&
1627 seek
-> binding_state
!= FTS_EXPIRED
&&
1628 seek
-> binding_state
!= FTS_RESET
&&
1629 seek
-> binding_state
!= FTS_FREE
&&
1630 seek
-> binding_state
!= FTS_BACKUP
)
1632 lease_dereference (&seek
, MDL
);
1634 lease_reference (&seek
, next
, MDL
);
1635 lease_dereference (&next
, MDL
);
1639 lease_dereference (&next
, MDL
);
1641 release_lease (seek
, packet
);
1642 lease_dereference (&seek
, MDL
);
1647 if (!lease
-> uid_len
||
1649 !host
-> client_identifier
.len
&&
1650 (oc
= lookup_option (&server_universe
, state
-> options
,
1652 !evaluate_boolean_option_cache (&ignorep
, packet
, lease
,
1653 (struct client_state
*)0,
1659 seek
= (struct lease
*)0;
1660 find_lease_by_hw_addr
1661 (&seek
, lease
-> hardware_addr
.hbuf
,
1662 lease
-> hardware_addr
.hlen
, MDL
);
1665 if (seek
== lease
&& !seek
-> n_hw
) {
1666 lease_dereference (&seek
, MDL
);
1669 next
= (struct lease
*)0;
1672 lease_reference (&next
, seek
-> n_hw
, MDL
);
1673 if (seek
!= lease
&&
1674 seek
-> binding_state
!= FTS_RELEASED
&&
1675 seek
-> binding_state
!= FTS_EXPIRED
&&
1676 seek
-> binding_state
!= FTS_RESET
&&
1677 seek
-> binding_state
!= FTS_FREE
&&
1678 seek
-> binding_state
!= FTS_BACKUP
)
1680 lease_dereference (&seek
, MDL
);
1682 lease_reference (&seek
, next
, MDL
);
1683 lease_dereference (&next
, MDL
);
1687 lease_dereference (&next
, MDL
);
1689 release_lease (seek
, packet
);
1690 lease_dereference (&seek
, MDL
);
1698 /* Make sure this packet satisfies the configured minimum
1699 number of seconds. */
1700 memset (&d1
, 0, sizeof d1
);
1701 if (offer
== DHCPOFFER
&&
1702 (oc
= lookup_option (&server_universe
, state
-> options
,
1704 if (evaluate_option_cache (&d1
, packet
, lease
,
1705 (struct client_state
*)0,
1706 packet
-> options
, state
-> options
,
1707 &lease
-> scope
, oc
, MDL
)) {
1709 ntohs (packet
-> raw
-> secs
) < d1
.data
[0]) {
1710 log_info("%s: configured min-secs value (%d) "
1711 "is greater than secs field (%d). "
1712 "message dropped.", msg
, d1
.data
[0],
1713 ntohs(packet
->raw
->secs
));
1714 data_string_forget (&d1
, MDL
);
1715 free_lease_state (state
, MDL
);
1717 host_dereference (&host
, MDL
);
1720 data_string_forget (&d1
, MDL
);
1724 /* Try to find a matching host declaration for this lease.
1727 struct host_decl
*hp
= (struct host_decl
*)0;
1728 struct host_decl
*h
;
1730 /* Try to find a host_decl that matches the client
1731 identifier or hardware address on the packet, and
1732 has no fixed IP address. If there is one, hang
1733 it off the lease so that its option definitions
1735 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
1736 DHO_DHCP_CLIENT_IDENTIFIER
);
1738 evaluate_option_cache (&d1
, packet
, lease
,
1739 (struct client_state
*)0,
1740 packet
-> options
, state
-> options
,
1741 &lease
-> scope
, oc
, MDL
)) {
1742 find_hosts_by_uid (&hp
, d1
.data
, d1
.len
, MDL
);
1743 data_string_forget (&d1
, MDL
);
1744 for (h
= hp
; h
; h
= h
-> n_ipaddr
) {
1745 if (!h
-> fixed_addr
)
1749 host_reference (&host
, h
, MDL
);
1751 host_dereference(&hp
, MDL
);
1754 find_hosts_by_haddr (&hp
,
1755 packet
-> raw
-> htype
,
1756 packet
-> raw
-> chaddr
,
1757 packet
-> raw
-> hlen
,
1759 for (h
= hp
; h
; h
= h
-> n_ipaddr
) {
1760 if (!h
-> fixed_addr
)
1764 host_reference (&host
, h
, MDL
);
1766 host_dereference(&hp
, MDL
);
1769 find_hosts_by_option(&hp
, packet
, packet
->options
, MDL
);
1770 for (h
= hp
; h
; h
= h
-> n_ipaddr
) {
1771 if (!h
-> fixed_addr
)
1775 host_reference (&host
, h
, MDL
);
1777 host_dereference(&hp
, MDL
);
1781 /* If we have a host_decl structure, run the options associated
1782 with its group. Whether the host decl struct is old or not. */
1784 execute_statements_in_scope ((struct binding_value
**)0,
1786 (struct client_state
*)0,
1788 state
-> options
, &lease
-> scope
,
1791 ? lease
-> pool
-> group
1792 : lease
-> subnet
-> group
));
1794 /* Drop the request if it's not allowed for this client. By
1795 default, unknown clients are allowed. */
1797 (oc
= lookup_option (&server_universe
, state
-> options
,
1798 SV_BOOT_UNKNOWN_CLIENTS
)) &&
1799 !evaluate_boolean_option_cache (&ignorep
,
1801 (struct client_state
*)0,
1804 &lease
-> scope
, oc
, MDL
)) {
1806 log_info ("%s: unknown client", msg
);
1807 free_lease_state (state
, MDL
);
1809 host_dereference (&host
, MDL
);
1813 /* Drop the request if it's not allowed for this client. */
1815 (oc
= lookup_option (&server_universe
, state
-> options
,
1817 !evaluate_boolean_option_cache (&ignorep
,
1819 (struct client_state
*)0,
1822 &lease
-> scope
, oc
, MDL
)) {
1824 log_info ("%s: bootp disallowed", msg
);
1825 free_lease_state (state
, MDL
);
1827 host_dereference (&host
, MDL
);
1831 /* Drop the request if booting is specifically denied. */
1832 oc
= lookup_option (&server_universe
, state
-> options
,
1835 !evaluate_boolean_option_cache (&ignorep
,
1837 (struct client_state
*)0,
1840 &lease
-> scope
, oc
, MDL
)) {
1842 log_info ("%s: booting disallowed", msg
);
1843 free_lease_state (state
, MDL
);
1845 host_dereference (&host
, MDL
);
1849 /* If we are configured to do per-class billing, do it. */
1850 if (have_billing_classes
&& !(lease
-> flags
& STATIC_LEASE
)) {
1851 /* See if the lease is currently being billed to a
1852 class, and if so, whether or not it can continue to
1853 be billed to that class. */
1854 if (lease
-> billing_class
) {
1855 for (i
= 0; i
< packet
-> class_count
; i
++)
1856 if (packet
-> classes
[i
] ==
1857 lease
-> billing_class
)
1859 if (i
== packet
-> class_count
)
1860 unbill_class (lease
, lease
-> billing_class
);
1863 /* If we don't have an active billing, see if we need
1864 one, and if we do, try to do so. */
1865 if (lease
->billing_class
== NULL
) {
1867 for (i
= 0; i
< packet
->class_count
; i
++) {
1868 if (packet
->classes
[i
]->lease_limit
) {
1870 if (bill_class(lease
,
1871 packet
->classes
[i
]))
1875 if (bill
!= 0 && i
== packet
->class_count
) {
1876 log_info("%s: no available billing: lease "
1877 "limit reached in all matching "
1879 free_lease_state(state
, MDL
);
1881 host_dereference(&host
, MDL
);
1885 /* If this is an offer, undo the billing. We go
1886 * through all the steps above to bill a class so
1887 * we can hit the 'no available billing' mark and
1888 * abort without offering. But it just doesn't make
1889 * sense to permanently bill a class for a non-active
1890 * lease. This means on REQUEST, we will bill this
1891 * lease again (if there is a REQUEST).
1893 if (offer
== DHCPOFFER
&&
1894 lease
->billing_class
!= NULL
&&
1895 lease
->binding_state
!= FTS_ACTIVE
)
1896 unbill_class(lease
, lease
->billing_class
);
1900 /* Figure out the filename. */
1901 oc
= lookup_option (&server_universe
, state
-> options
, SV_FILENAME
);
1903 evaluate_option_cache (&state
-> filename
, packet
, lease
,
1904 (struct client_state
*)0,
1905 packet
-> options
, state
-> options
,
1906 &lease
-> scope
, oc
, MDL
);
1908 /* Choose a server name as above. */
1909 oc
= lookup_option (&server_universe
, state
-> options
,
1912 evaluate_option_cache (&state
-> server_name
, packet
, lease
,
1913 (struct client_state
*)0,
1914 packet
-> options
, state
-> options
,
1915 &lease
-> scope
, oc
, MDL
);
1917 /* At this point, we have a lease that we can offer the client.
1918 Now we construct a lease structure that contains what we want,
1919 and call supersede_lease to do the right thing with it. */
1920 lt
= (struct lease
*)0;
1921 result
= lease_allocate (<
, MDL
);
1922 if (result
!= ISC_R_SUCCESS
) {
1923 log_info ("%s: can't allocate temporary lease structure: %s",
1924 msg
, isc_result_totext (result
));
1925 free_lease_state (state
, MDL
);
1927 host_dereference (&host
, MDL
);
1931 /* Use the ip address of the lease that we finally found in
1933 lt
-> ip_addr
= lease
-> ip_addr
;
1936 lt
-> starts
= cur_time
;
1938 /* Figure out how long a lease to assign. If this is a
1939 dynamic BOOTP lease, its duration must be infinite. */
1941 lt
->flags
&= ~BOOTP_LEASE
;
1943 default_lease_time
= DEFAULT_DEFAULT_LEASE_TIME
;
1944 if ((oc
= lookup_option (&server_universe
, state
-> options
,
1945 SV_DEFAULT_LEASE_TIME
))) {
1946 if (evaluate_option_cache (&d1
, packet
, lease
,
1947 (struct client_state
*)0,
1950 &lease
-> scope
, oc
, MDL
)) {
1951 if (d1
.len
== sizeof (u_int32_t
))
1952 default_lease_time
=
1954 data_string_forget (&d1
, MDL
);
1958 if ((oc
= lookup_option (&dhcp_universe
, packet
-> options
,
1959 DHO_DHCP_LEASE_TIME
)))
1960 s1
= evaluate_option_cache (&d1
, packet
, lease
,
1961 (struct client_state
*)0,
1964 &lease
-> scope
, oc
, MDL
);
1968 if (s1
&& (d1
.len
== 4)) {
1969 u_int32_t ones
= 0xffffffff;
1971 /* One potential use of reserved leases is to allow
1972 * clients to signal reservation of their lease. They
1973 * can kinda sorta do this, if you squint hard enough,
1974 * by supplying an 'infinite' requested-lease-time
1975 * option. This is generally bad practice...you want
1976 * clients to return to the server on at least some
1977 * period (days, months, years) to get up-to-date
1980 * 1) A client requests 0xffffffff lease-time.
1981 * 2) The server reserves the lease, and assigns a
1982 * <= max_lease_time lease-time to the client, which
1983 * we presume is much smaller than 0xffffffff.
1984 * 3) The client ultimately fails to renew its lease
1985 * (all clients go offline at some point).
1986 * 4) The server retains the reservation, although
1987 * the lease expires and passes through those states
1988 * as normal, it's placed in the 'reserved' queue,
1989 * and is under no circumstances allocated to any
1992 * Whether the client knows its reserving its lease or
1993 * not, this can be a handy tool for a sysadmin.
1995 if ((memcmp(d1
.data
, &ones
, 4) == 0) &&
1996 (oc
= lookup_option(&server_universe
,
1998 SV_RESERVE_INFINITE
)) &&
1999 evaluate_boolean_option_cache(&ignorep
, packet
,
2000 lease
, NULL
, packet
->options
,
2001 state
->options
, &lease
->scope
,
2003 lt
->flags
|= RESERVED_LEASE
;
2005 log_info("Infinite-leasetime "
2006 "reservation made on %s.",
2007 piaddr(lt
->ip_addr
));
2010 lease_time
= getULong (d1
.data
);
2012 lease_time
= default_lease_time
;
2015 data_string_forget(&d1
, MDL
);
2017 /* See if there's a maximum lease time. */
2018 max_lease_time
= DEFAULT_MAX_LEASE_TIME
;
2019 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2020 SV_MAX_LEASE_TIME
))) {
2021 if (evaluate_option_cache (&d1
, packet
, lease
,
2022 (struct client_state
*)0,
2025 &lease
-> scope
, oc
, MDL
)) {
2026 if (d1
.len
== sizeof (u_int32_t
))
2029 data_string_forget (&d1
, MDL
);
2033 /* Enforce the maximum lease length. */
2034 if (lease_time
< 0 /* XXX */
2035 || lease_time
> max_lease_time
)
2036 lease_time
= max_lease_time
;
2038 min_lease_time
= DEFAULT_MIN_LEASE_TIME
;
2039 if (min_lease_time
> max_lease_time
)
2040 min_lease_time
= max_lease_time
;
2042 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2043 SV_MIN_LEASE_TIME
))) {
2044 if (evaluate_option_cache (&d1
, packet
, lease
,
2045 (struct client_state
*)0,
2048 &lease
-> scope
, oc
, MDL
)) {
2049 if (d1
.len
== sizeof (u_int32_t
))
2050 min_lease_time
= getULong (d1
.data
);
2051 data_string_forget (&d1
, MDL
);
2055 /* CC: If there are less than
2056 adaptive-lease-time-threshold % free leases,
2057 hand out only short term leases */
2059 memset(&d1
, 0, sizeof(d1
));
2061 (oc
= lookup_option(&server_universe
, state
->options
,
2062 SV_ADAPTIVE_LEASE_TIME_THRESHOLD
)) &&
2063 evaluate_option_cache(&d1
, packet
, lease
, NULL
,
2064 packet
->options
, state
->options
,
2065 &lease
->scope
, oc
, MDL
)) {
2066 if (d1
.len
== 1 && d1
.data
[0] > 0 &&
2069 int poolfilled
, total
, count
;
2072 adaptive_time
= min_lease_time
;
2074 adaptive_time
= DEFAULT_MIN_LEASE_TIME
;
2076 /* Allow the client to keep its lease. */
2077 if (lease
->ends
- cur_time
> adaptive_time
)
2078 adaptive_time
= lease
->ends
- cur_time
;
2080 count
= lease
->pool
->lease_count
;
2081 total
= count
- (lease
->pool
->free_leases
+
2082 lease
->pool
->backup_leases
);
2084 poolfilled
= (total
> (INT_MAX
/ 100)) ?
2085 total
/ (count
/ 100) :
2086 (total
* 100) / count
;
2088 log_debug("Adap-lease: Total: %d, Free: %d, "
2089 "Ends: %d, Adaptive: %d, Fill: %d, "
2091 lease
->pool
->lease_count
,
2092 lease
->pool
->free_leases
,
2093 (int)(lease
->ends
- cur_time
),
2094 (int)adaptive_time
, poolfilled
,
2097 if (poolfilled
>= d1
.data
[0] &&
2098 lease_time
> adaptive_time
) {
2099 log_info("Pool over threshold, time "
2100 "for %s reduced from %d to "
2101 "%d.", piaddr(lease
->ip_addr
),
2103 (int)adaptive_time
);
2105 lease_time
= adaptive_time
;
2108 data_string_forget(&d1
, MDL
);
2111 /* a client requests an address which is not yet active*/
2112 if (lease
->pool
&& lease
->pool
->valid_from
&&
2113 cur_time
< lease
->pool
->valid_from
) {
2114 /* NAK leases before pool activation date */
2116 memcpy (cip
.iabuf
, <
->ip_addr
.iabuf
, 4);
2117 nak_lease(packet
, &cip
);
2118 free_lease_state (state
, MDL
);
2119 lease_dereference (<
, MDL
);
2121 host_dereference (&host
, MDL
);
2127 a) NAK current lease if past the expiration date
2128 b) extend lease only up to the expiration date, but not
2129 below min-lease-time
2130 Setting min-lease-time is essential for this to work!
2131 The value of min-lease-time determines the lenght
2132 of the transition window:
2133 A client renewing a second before the deadline will
2134 get a min-lease-time lease. Since the current ip might not
2135 be routable after the deadline, the client will
2136 be offline until it DISCOVERS again. Otherwise it will
2137 receive a NAK at T/2.
2138 A min-lease-time of 6 seconds effectively switches over
2139 all clients in this pool very quickly.
2142 if (lease
->pool
&& lease
->pool
->valid_until
) {
2143 if (cur_time
>= lease
->pool
->valid_until
) {
2144 /* NAK leases after pool expiration date */
2146 memcpy (cip
.iabuf
, <
->ip_addr
.iabuf
, 4);
2147 nak_lease(packet
, &cip
);
2148 free_lease_state (state
, MDL
);
2149 lease_dereference (<
, MDL
);
2151 host_dereference (&host
, MDL
);
2154 remaining_time
= lease
->pool
->valid_until
- cur_time
;
2155 if (lease_time
> remaining_time
)
2156 lease_time
= remaining_time
;
2159 if (lease_time
< min_lease_time
) {
2161 lease_time
= min_lease_time
;
2163 lease_time
= default_lease_time
;
2167 #if defined (FAILOVER_PROTOCOL)
2168 /* Okay, we know the lease duration. Now check the
2169 failover state, if any. */
2170 if (lease
-> pool
&& lease
-> pool
-> failover_peer
) {
2171 TIME new_lease_time
= lease_time
;
2172 dhcp_failover_state_t
*peer
=
2173 lease
-> pool
-> failover_peer
;
2175 /* Copy previous lease failover ack-state. */
2176 lt
->tsfp
= lease
->tsfp
;
2177 lt
->atsfp
= lease
->atsfp
;
2179 /* cltt set below */
2181 /* Lease times less than MCLT are not a concern. */
2182 if (lease_time
> peer
->mclt
) {
2183 /* Each server can only offer a lease time
2184 * that is either equal to MCLT (at least),
2185 * or up to TSFP+MCLT. Only if the desired
2186 * lease time falls within TSFP+MCLT, can
2187 * the server allow it.
2189 if (lt
->tsfp
<= cur_time
)
2190 new_lease_time
= peer
->mclt
;
2191 else if ((cur_time
+ lease_time
) >
2192 (lt
->tsfp
+ peer
->mclt
))
2193 new_lease_time
= (lt
->tsfp
- cur_time
)
2197 /* Update potential expiry. Allow for the desired
2198 * lease time plus one half the actual (whether
2199 * modified downward or not) lease time, which is
2200 * actually an estimate of when the client will
2201 * renew. This way, the client will be able to get
2202 * the desired lease time upon renewal.
2204 if (offer
== DHCPACK
) {
2205 lt
->tstp
= cur_time
+ lease_time
+
2206 (new_lease_time
/ 2);
2208 /* If we reduced the potential expiry time,
2209 * make sure we don't offer an old-expiry-time
2210 * lease for this lease before the change is
2213 if (lt
->tstp
< lt
->tsfp
)
2214 lt
->tsfp
= lt
->tstp
;
2216 lt
->tstp
= lease
->tstp
;
2218 /* Use failover-modified lease time. */
2219 lease_time
= new_lease_time
;
2221 #endif /* FAILOVER_PROTOCOL */
2223 /* If the lease duration causes the time value to wrap,
2224 use the maximum expiry time. */
2225 if (cur_time
+ lease_time
< cur_time
)
2226 state
-> offered_expiry
= MAX_TIME
- 1;
2228 state
-> offered_expiry
= cur_time
+ lease_time
;
2232 lt
-> ends
= state
-> offered_expiry
;
2234 /* Don't make lease active until we actually get a
2236 if (offer
== DHCPACK
)
2237 lt
-> next_binding_state
= FTS_ACTIVE
;
2239 lt
-> next_binding_state
= lease
-> binding_state
;
2241 lt
->flags
|= BOOTP_LEASE
;
2243 lease_time
= MAX_TIME
- cur_time
;
2245 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2246 SV_BOOTP_LEASE_LENGTH
))) {
2247 if (evaluate_option_cache (&d1
, packet
, lease
,
2248 (struct client_state
*)0,
2251 &lease
-> scope
, oc
, MDL
)) {
2252 if (d1
.len
== sizeof (u_int32_t
))
2253 lease_time
= getULong (d1
.data
);
2254 data_string_forget (&d1
, MDL
);
2258 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2259 SV_BOOTP_LEASE_CUTOFF
))) {
2260 if (evaluate_option_cache (&d1
, packet
, lease
,
2261 (struct client_state
*)0,
2264 &lease
-> scope
, oc
, MDL
)) {
2265 if (d1
.len
== sizeof (u_int32_t
))
2266 lease_time
= (getULong (d1
.data
) -
2268 data_string_forget (&d1
, MDL
);
2272 lt
-> ends
= state
-> offered_expiry
= cur_time
+ lease_time
;
2273 lt
-> next_binding_state
= FTS_ACTIVE
;
2276 /* Update Client Last Transaction Time. */
2277 lt
->cltt
= cur_time
;
2279 /* Record the uid, if given... */
2280 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
2281 DHO_DHCP_CLIENT_IDENTIFIER
);
2283 evaluate_option_cache (&d1
, packet
, lease
,
2284 (struct client_state
*)0,
2285 packet
-> options
, state
-> options
,
2286 &lease
-> scope
, oc
, MDL
)) {
2287 if (d1
.len
<= sizeof lt
-> uid_buf
) {
2288 memcpy (lt
-> uid_buf
, d1
.data
, d1
.len
);
2289 lt
-> uid
= lt
-> uid_buf
;
2290 lt
-> uid_max
= sizeof lt
-> uid_buf
;
2291 lt
-> uid_len
= d1
.len
;
2293 unsigned char *tuid
;
2294 lt
-> uid_max
= d1
.len
;
2295 lt
-> uid_len
= d1
.len
;
2296 tuid
= (unsigned char *)dmalloc (lt
-> uid_max
, MDL
);
2299 log_fatal ("no memory for large uid.");
2300 memcpy (tuid
, d1
.data
, lt
-> uid_len
);
2303 data_string_forget (&d1
, MDL
);
2307 host_reference (<
-> host
, host
, MDL
);
2308 host_dereference (&host
, MDL
);
2310 if (lease
-> subnet
)
2311 subnet_reference (<
-> subnet
, lease
-> subnet
, MDL
);
2312 if (lease
-> billing_class
)
2313 class_reference (<
-> billing_class
,
2314 lease
-> billing_class
, MDL
);
2316 /* Set a flag if this client is a broken client that NUL
2317 terminates string options and expects us to do likewise. */
2319 lease
-> flags
|= MS_NULL_TERMINATION
;
2321 lease
-> flags
&= ~MS_NULL_TERMINATION
;
2323 /* Save any bindings. */
2324 if (lease
-> scope
) {
2325 binding_scope_reference (<
-> scope
, lease
-> scope
, MDL
);
2326 binding_scope_dereference (&lease
-> scope
, MDL
);
2328 if (lease
-> agent_options
)
2329 option_chain_head_reference (<
-> agent_options
,
2330 lease
-> agent_options
, MDL
);
2332 /* If we got relay agent information options from the packet, then
2333 * cache them for renewal in case the relay agent can't supply them
2334 * when the client unicasts. The options may be from an addressed
2335 * "l3" relay, or from an unaddressed "l2" relay which does not set
2338 if (!packet
->agent_options_stashed
&&
2339 packet
->options
->universe_count
> agent_universe
.index
&&
2340 packet
->options
->universes
[agent_universe
.index
] != NULL
) {
2341 oc
= lookup_option (&server_universe
, state
-> options
,
2342 SV_STASH_AGENT_OPTIONS
);
2344 evaluate_boolean_option_cache (&ignorep
, packet
, lease
,
2345 (struct client_state
*)0,
2348 &lease
-> scope
, oc
, MDL
)) {
2349 if (lt
-> agent_options
)
2350 option_chain_head_dereference (<
-> agent_options
, MDL
);
2351 option_chain_head_reference
2352 (<
-> agent_options
,
2353 (struct option_chain_head
*)
2354 packet
-> options
-> universes
[agent_universe
.index
],
2359 /* Replace the old lease hostname with the new one, if it's changed. */
2360 oc
= lookup_option (&dhcp_universe
, packet
-> options
, DHO_HOST_NAME
);
2362 s1
= evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
2363 (struct client_state
*)0,
2365 (struct option_state
*)0,
2366 &global_scope
, oc
, MDL
);
2371 lease
-> client_hostname
&&
2372 strlen (lease
-> client_hostname
) == d1
.len
&&
2373 !memcmp (lease
-> client_hostname
, d1
.data
, d1
.len
)) {
2374 /* Hasn't changed. */
2375 data_string_forget (&d1
, MDL
);
2376 lt
-> client_hostname
= lease
-> client_hostname
;
2377 lease
-> client_hostname
= (char *)0;
2378 } else if (oc
&& s1
) {
2379 lt
-> client_hostname
= dmalloc (d1
.len
+ 1, MDL
);
2380 if (!lt
-> client_hostname
)
2381 log_error ("no memory for client hostname.");
2383 memcpy (lt
-> client_hostname
, d1
.data
, d1
.len
);
2384 lt
-> client_hostname
[d1
.len
] = 0;
2386 data_string_forget (&d1
, MDL
);
2389 /* Record the hardware address, if given... */
2390 lt
-> hardware_addr
.hlen
= packet
-> raw
-> hlen
+ 1;
2391 lt
-> hardware_addr
.hbuf
[0] = packet
-> raw
-> htype
;
2392 memcpy (<
-> hardware_addr
.hbuf
[1], packet
-> raw
-> chaddr
,
2393 sizeof packet
-> raw
-> chaddr
);
2395 lt
-> flags
= lease
-> flags
& ~PERSISTENT_FLAGS
;
2397 /* If there are statements to execute when the lease is
2398 committed, execute them. */
2399 if (lease
-> on_commit
&& (!offer
|| offer
== DHCPACK
)) {
2400 execute_statements ((struct binding_value
**)0,
2401 packet
, lt
, (struct client_state
*)0,
2403 state
-> options
, <
-> scope
,
2404 lease
-> on_commit
);
2405 if (lease
-> on_commit
)
2406 executable_statement_dereference (&lease
-> on_commit
,
2411 /* Perform DDNS updates, if configured to. */
2412 if ((!offer
|| offer
== DHCPACK
) &&
2413 (!(oc
= lookup_option (&server_universe
, state
-> options
,
2414 SV_DDNS_UPDATES
)) ||
2415 evaluate_boolean_option_cache (&ignorep
, packet
, lt
,
2416 (struct client_state
*)0,
2419 <
-> scope
, oc
, MDL
))) {
2420 ddns_updates(packet
, lt
, lease
, NULL
, NULL
, state
->options
);
2422 #endif /* NSUPDATE */
2424 /* Don't call supersede_lease on a mocked-up lease. */
2425 if (lease
-> flags
& STATIC_LEASE
) {
2426 /* Copy the hardware address into the static lease
2428 lease
-> hardware_addr
.hlen
= packet
-> raw
-> hlen
+ 1;
2429 lease
-> hardware_addr
.hbuf
[0] = packet
-> raw
-> htype
;
2430 memcpy (&lease
-> hardware_addr
.hbuf
[1],
2431 packet
-> raw
-> chaddr
,
2432 sizeof packet
-> raw
-> chaddr
); /* XXX */
2434 #if !defined(DELAYED_ACK)
2435 /* Install the new information on 'lt' onto the lease at
2436 * 'lease'. If this is a DHCPOFFER, it is a 'soft' promise,
2437 * if it is a DHCPACK, it is a 'hard' binding, so it needs
2438 * to be recorded and propogated immediately. If the update
2439 * fails, don't ACK it (or BOOTREPLY) either; we may give
2440 * the same lease to another client later, and that would be
2443 if (!supersede_lease(lease
, lt
, !offer
|| (offer
== DHCPACK
),
2444 offer
== DHCPACK
, offer
== DHCPACK
)) {
2445 #else /* defined(DELAYED_ACK) */
2446 /* Install the new information on 'lt' onto the lease at
2447 * 'lease'. Â We will not 'commit' this information to disk
2448 * yet (fsync()), we will 'propogate' the information if
2449 * this is BOOTP or a DHCPACK, but we will not 'pimmediate'ly
2450 * transmit failover binding updates (this is delayed until
2451 * after the fsync()). If the update fails, don't ACK it (or
2452 * BOOTREPLY either); we may give the same lease out to a
2453 * different client, and that would be a conflict.
2455 if (!supersede_lease(lease
, lt
, 0, !offer
|| offer
== DHCPACK
,
2458 log_info ("%s: database update failed", msg
);
2459 free_lease_state (state
, MDL
);
2460 lease_dereference (<
, MDL
);
2464 lease_dereference (<
, MDL
);
2466 /* Remember the interface on which the packet arrived. */
2467 state
-> ip
= packet
-> interface
;
2469 /* Remember the giaddr, xid, secs, flags and hops. */
2470 state
-> giaddr
= packet
-> raw
-> giaddr
;
2471 state
-> ciaddr
= packet
-> raw
-> ciaddr
;
2472 state
-> xid
= packet
-> raw
-> xid
;
2473 state
-> secs
= packet
-> raw
-> secs
;
2474 state
-> bootp_flags
= packet
-> raw
-> flags
;
2475 state
-> hops
= packet
-> raw
-> hops
;
2476 state
-> offer
= offer
;
2478 /* If we're always supposed to broadcast to this client, set
2479 the broadcast bit in the bootp flags field. */
2480 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2481 SV_ALWAYS_BROADCAST
)) &&
2482 evaluate_boolean_option_cache (&ignorep
, packet
, lease
,
2483 (struct client_state
*)0,
2484 packet
-> options
, state
-> options
,
2485 &lease
-> scope
, oc
, MDL
))
2486 state
-> bootp_flags
|= htons (BOOTP_BROADCAST
);
2488 /* Get the Maximum Message Size option from the packet, if one
2490 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
2491 DHO_DHCP_MAX_MESSAGE_SIZE
);
2493 evaluate_option_cache (&d1
, packet
, lease
,
2494 (struct client_state
*)0,
2495 packet
-> options
, state
-> options
,
2496 &lease
-> scope
, oc
, MDL
)) {
2497 if (d1
.len
== sizeof (u_int16_t
))
2498 state
-> max_message_size
= getUShort (d1
.data
);
2499 data_string_forget (&d1
, MDL
);
2501 oc
= lookup_option (&dhcp_universe
, state
-> options
,
2502 DHO_DHCP_MAX_MESSAGE_SIZE
);
2504 evaluate_option_cache (&d1
, packet
, lease
,
2505 (struct client_state
*)0,
2506 packet
-> options
, state
-> options
,
2507 &lease
-> scope
, oc
, MDL
)) {
2508 if (d1
.len
== sizeof (u_int16_t
))
2509 state
-> max_message_size
=
2510 getUShort (d1
.data
);
2511 data_string_forget (&d1
, MDL
);
2515 /* Get the Subnet Selection option from the packet, if one
2517 if ((oc
= lookup_option (&dhcp_universe
, packet
-> options
,
2518 DHO_SUBNET_SELECTION
))) {
2520 /* Make a copy of the data. */
2521 struct option_cache
*noc
= (struct option_cache
*)0;
2522 if (option_cache_allocate (&noc
, MDL
)) {
2524 data_string_copy (&noc
-> data
,
2526 if (oc
-> expression
)
2527 expression_reference (&noc
-> expression
,
2528 oc
-> expression
, MDL
);
2530 option_reference(&(noc
->option
), oc
->option
,
2534 save_option (&dhcp_universe
, state
-> options
, noc
);
2535 option_cache_dereference (&noc
, MDL
);
2538 /* Now, if appropriate, put in DHCP-specific options that
2540 if (state
-> offer
) {
2541 i
= DHO_DHCP_MESSAGE_TYPE
;
2542 oc
= (struct option_cache
*)0;
2543 if (option_cache_allocate (&oc
, MDL
)) {
2544 if (make_const_data (&oc
-> expression
,
2545 &state
-> offer
, 1, 0, 0, MDL
)) {
2546 option_code_hash_lookup(&oc
->option
,
2547 dhcp_universe
.code_hash
,
2549 save_option (&dhcp_universe
,
2550 state
-> options
, oc
);
2552 option_cache_dereference (&oc
, MDL
);
2555 get_server_source_address(&from
, state
->options
, packet
);
2556 memcpy(state
->from
.iabuf
, &from
, sizeof(from
));
2557 state
->from
.len
= sizeof(from
);
2559 offered_lease_time
=
2560 state
-> offered_expiry
- cur_time
;
2562 putULong(state
->expiry
, (u_int32_t
)offered_lease_time
);
2563 i
= DHO_DHCP_LEASE_TIME
;
2564 oc
= (struct option_cache
*)0;
2565 if (option_cache_allocate (&oc
, MDL
)) {
2566 if (make_const_data(&oc
->expression
, state
->expiry
,
2568 option_code_hash_lookup(&oc
->option
,
2569 dhcp_universe
.code_hash
,
2571 save_option (&dhcp_universe
,
2572 state
-> options
, oc
);
2574 option_cache_dereference (&oc
, MDL
);
2578 * Validate any configured renew or rebinding times against
2579 * the determined lease time. Do rebinding first so that
2580 * the renew time can be validated against the rebind time.
2582 if ((oc
= lookup_option(&dhcp_universe
, state
->options
,
2583 DHO_DHCP_REBINDING_TIME
)) != NULL
&&
2584 evaluate_option_cache(&d1
, packet
, lease
, NULL
,
2585 packet
->options
, state
->options
,
2586 &lease
->scope
, oc
, MDL
)) {
2587 TIME rebind_time
= getULong(d1
.data
);
2589 /* Drop the configured (invalid) rebinding time. */
2590 if (rebind_time
>= offered_lease_time
)
2591 delete_option(&dhcp_universe
, state
->options
,
2592 DHO_DHCP_REBINDING_TIME
);
2593 else /* XXX: variable is reused. */
2594 offered_lease_time
= rebind_time
;
2596 data_string_forget(&d1
, MDL
);
2599 if ((oc
= lookup_option(&dhcp_universe
, state
->options
,
2600 DHO_DHCP_RENEWAL_TIME
)) != NULL
&&
2601 evaluate_option_cache(&d1
, packet
, lease
, NULL
,
2602 packet
->options
, state
->options
,
2603 &lease
->scope
, oc
, MDL
)) {
2604 if (getULong(d1
.data
) >= offered_lease_time
)
2605 delete_option(&dhcp_universe
, state
->options
,
2606 DHO_DHCP_RENEWAL_TIME
);
2608 data_string_forget(&d1
, MDL
);
2611 /* XXXSK: should we use get_server_source_address() here? */
2612 if (state
-> ip
-> address_count
) {
2614 sizeof state
-> ip
-> addresses
[0];
2615 memcpy (state
-> from
.iabuf
,
2616 &state
-> ip
-> addresses
[0],
2621 /* Figure out the address of the boot file server. */
2622 memset (&state
-> siaddr
, 0, sizeof state
-> siaddr
);
2624 lookup_option (&server_universe
,
2625 state
-> options
, SV_NEXT_SERVER
))) {
2626 if (evaluate_option_cache (&d1
, packet
, lease
,
2627 (struct client_state
*)0,
2628 packet
-> options
, state
-> options
,
2629 &lease
-> scope
, oc
, MDL
)) {
2630 /* If there was more than one answer,
2632 if (d1
.len
>= 4 && d1
.data
)
2633 memcpy (&state
-> siaddr
, d1
.data
, 4);
2634 data_string_forget (&d1
, MDL
);
2638 /* Use the subnet mask from the subnet declaration if no other
2639 mask has been provided. */
2640 i
= DHO_SUBNET_MASK
;
2641 if (!lookup_option (&dhcp_universe
, state
-> options
, i
)) {
2642 oc
= (struct option_cache
*)0;
2643 if (option_cache_allocate (&oc
, MDL
)) {
2644 if (make_const_data (&oc
-> expression
,
2645 lease
-> subnet
-> netmask
.iabuf
,
2646 lease
-> subnet
-> netmask
.len
,
2648 option_code_hash_lookup(&oc
->option
,
2649 dhcp_universe
.code_hash
,
2651 save_option (&dhcp_universe
,
2652 state
-> options
, oc
);
2654 option_cache_dereference (&oc
, MDL
);
2658 /* Use the hostname from the host declaration if there is one
2659 and no hostname has otherwise been provided, and if the
2660 use-host-decl-name flag is set. */
2662 j
= SV_USE_HOST_DECL_NAMES
;
2663 if (!lookup_option (&dhcp_universe
, state
-> options
, i
) &&
2664 lease
-> host
&& lease
-> host
-> name
&&
2665 (evaluate_boolean_option_cache
2666 (&ignorep
, packet
, lease
, (struct client_state
*)0,
2667 packet
-> options
, state
-> options
, &lease
-> scope
,
2668 lookup_option (&server_universe
, state
-> options
, j
), MDL
))) {
2669 oc
= (struct option_cache
*)0;
2670 if (option_cache_allocate (&oc
, MDL
)) {
2671 if (make_const_data (&oc
-> expression
,
2673 lease
-> host
-> name
),
2674 strlen (lease
-> host
-> name
),
2676 option_code_hash_lookup(&oc
->option
,
2677 dhcp_universe
.code_hash
,
2679 save_option (&dhcp_universe
,
2680 state
-> options
, oc
);
2682 option_cache_dereference (&oc
, MDL
);
2686 /* If we don't have a hostname yet, and we've been asked to do
2687 a reverse lookup to find the hostname, do it. */
2689 j
= SV_GET_LEASE_HOSTNAMES
;
2690 if (!lookup_option(&dhcp_universe
, state
->options
, i
) &&
2691 evaluate_boolean_option_cache
2692 (&ignorep
, packet
, lease
, NULL
,
2693 packet
->options
, state
->options
, &lease
->scope
,
2694 lookup_option (&server_universe
, state
->options
, j
), MDL
)) {
2698 memcpy (&ia
, lease
-> ip_addr
.iabuf
, 4);
2700 h
= gethostbyaddr ((char *)&ia
, sizeof ia
, AF_INET
);
2702 log_error ("No hostname for %s", inet_ntoa (ia
));
2704 oc
= (struct option_cache
*)0;
2705 if (option_cache_allocate (&oc
, MDL
)) {
2706 if (make_const_data (&oc
-> expression
,
2709 strlen (h
-> h_name
) + 1,
2711 option_code_hash_lookup(&oc
->option
,
2712 dhcp_universe
.code_hash
,
2714 save_option (&dhcp_universe
,
2715 state
-> options
, oc
);
2717 option_cache_dereference (&oc
, MDL
);
2722 /* If so directed, use the leased IP address as the router address.
2723 This supposedly makes Win95 machines ARP for all IP addresses,
2724 so if the local router does proxy arp, you win. */
2726 if (evaluate_boolean_option_cache
2727 (&ignorep
, packet
, lease
, (struct client_state
*)0,
2728 packet
-> options
, state
-> options
, &lease
-> scope
,
2729 lookup_option (&server_universe
, state
-> options
,
2730 SV_USE_LEASE_ADDR_FOR_DEFAULT_ROUTE
), MDL
)) {
2732 oc
= lookup_option (&dhcp_universe
, state
-> options
, i
);
2734 oc
= (struct option_cache
*)0;
2735 if (option_cache_allocate (&oc
, MDL
)) {
2736 if (make_const_data (&oc
-> expression
,
2737 lease
-> ip_addr
.iabuf
,
2738 lease
-> ip_addr
.len
,
2740 option_code_hash_lookup(&oc
->option
,
2741 dhcp_universe
.code_hash
,
2743 save_option (&dhcp_universe
,
2744 state
-> options
, oc
);
2746 option_cache_dereference (&oc
, MDL
);
2751 /* If a site option space has been specified, use that for
2752 site option codes. */
2753 i
= SV_SITE_OPTION_SPACE
;
2754 if ((oc
= lookup_option (&server_universe
, state
-> options
, i
)) &&
2755 evaluate_option_cache (&d1
, packet
, lease
,
2756 (struct client_state
*)0,
2757 packet
-> options
, state
-> options
,
2758 &lease
-> scope
, oc
, MDL
)) {
2759 struct universe
*u
= (struct universe
*)0;
2761 if (!universe_hash_lookup (&u
, universe_hash
,
2762 (const char *)d1
.data
, d1
.len
,
2764 log_error ("unknown option space %s.", d1
.data
);
2768 state
-> options
-> site_universe
= u
-> index
;
2769 state
->options
->site_code_min
= find_min_site_code(u
);
2770 data_string_forget (&d1
, MDL
);
2772 state
-> options
-> site_code_min
= 0;
2773 state
-> options
-> site_universe
= dhcp_universe
.index
;
2776 /* If the client has provided a list of options that it wishes
2777 returned, use it to prioritize. If there's a parameter
2778 request list in scope, use that in preference. Otherwise
2779 use the default priority list. */
2781 oc
= lookup_option (&dhcp_universe
, state
-> options
,
2782 DHO_DHCP_PARAMETER_REQUEST_LIST
);
2785 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
2786 DHO_DHCP_PARAMETER_REQUEST_LIST
);
2788 evaluate_option_cache (&state
-> parameter_request_list
,
2789 packet
, lease
, (struct client_state
*)0,
2790 packet
-> options
, state
-> options
,
2791 &lease
-> scope
, oc
, MDL
);
2794 dump_packet (packet
);
2795 dump_raw ((unsigned char *)packet
-> raw
, packet
-> packet_length
);
2798 lease
-> state
= state
;
2800 log_info ("%s", msg
);
2802 /* Hang the packet off the lease state. */
2803 packet_reference (&lease
-> state
-> packet
, packet
, MDL
);
2805 /* If this is a DHCPOFFER, ping the lease address before actually
2806 sending the offer. */
2807 if (offer
== DHCPOFFER
&& !(lease
-> flags
& STATIC_LEASE
) &&
2808 ((cur_time
- lease_cltt
) > 60) &&
2809 (!(oc
= lookup_option (&server_universe
, state
-> options
,
2811 evaluate_boolean_option_cache (&ignorep
, packet
, lease
,
2812 (struct client_state
*)0,
2815 &lease
-> scope
, oc
, MDL
))) {
2816 icmp_echorequest (&lease
-> ip_addr
);
2818 /* Determine whether to use configured or default ping timeout.
2820 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2821 SV_PING_TIMEOUT
)) &&
2822 evaluate_option_cache (&d1
, packet
, lease
, NULL
,
2825 &lease
-> scope
, oc
, MDL
)) {
2826 if (d1
.len
== sizeof (u_int32_t
))
2827 ping_timeout
= getULong (d1
.data
);
2829 ping_timeout
= DEFAULT_PING_TIMEOUT
;
2831 data_string_forget (&d1
, MDL
);
2833 ping_timeout
= DEFAULT_PING_TIMEOUT
;
2836 log_debug ("Ping timeout: %ld", (long)ping_timeout
);
2839 tv
. tv_sec
= cur_time
+ ping_timeout
;
2841 add_timeout (&tv
, lease_ping_timeout
, lease
,
2842 (tvref_t
)lease_reference
,
2843 (tvunref_t
)lease_dereference
);
2844 ++outstanding_pings
;
2846 lease
->cltt
= cur_time
;
2847 #if defined(DELAYED_ACK)
2848 if (!(lease
->flags
& STATIC_LEASE
) &&
2849 (!offer
|| (offer
== DHCPACK
)))
2850 delayed_ack_enqueue(lease
);
2858 * CC: queue single ACK:
2859 * - write the lease (but do not fsync it yet)
2860 * - add to double linked list
2861 * - commit if more than xx ACKs pending
2862 * - if necessary set the max timer and bump the next timer
2863 * but only up to the max timer value.
2867 delayed_ack_enqueue(struct lease
*lease
)
2869 struct leasequeue
*q
;
2871 if (!write_lease(lease
))
2873 if (free_ackqueue
) {
2875 free_ackqueue
= q
->next
;
2877 q
= ((struct leasequeue
*)
2878 dmalloc(sizeof(struct leasequeue
), MDL
));
2880 log_fatal("delayed_ack_enqueue: no memory!");
2882 memset(q
, 0, sizeof *q
);
2883 /* prepend to ackqueue*/
2884 lease_reference(&q
->lease
, lease
, MDL
);
2885 q
->next
= ackqueue_head
;
2893 if (outstanding_acks
> max_outstanding_acks
) {
2896 /* Reset max_fsync and cancel any pending timeout. */
2897 memset(&max_fsync
, 0, sizeof(max_fsync
));
2898 cancel_timeout(commit_leases_ackout
, NULL
);
2900 struct timeval next_fsync
;
2902 if (max_fsync
.tv_sec
== 0 && max_fsync
.tv_usec
== 0) {
2903 /* set the maximum time we'll wait */
2904 max_fsync
.tv_sec
= cur_tv
.tv_sec
+ max_ack_delay_secs
;
2905 max_fsync
.tv_usec
= cur_tv
.tv_usec
+
2906 max_ack_delay_usecs
;
2908 if (max_fsync
.tv_usec
>= 1000000) {
2910 max_fsync
.tv_usec
-= 1000000;
2914 /* Set the timeout */
2915 next_fsync
.tv_sec
= cur_tv
.tv_sec
;
2916 next_fsync
.tv_usec
= cur_tv
.tv_usec
+ min_ack_delay_usecs
;
2917 if (next_fsync
.tv_usec
>= 1000000) {
2918 next_fsync
.tv_sec
++;
2919 next_fsync
.tv_usec
-= 1000000;
2921 /* but not more than the max */
2922 if ((next_fsync
.tv_sec
> max_fsync
.tv_sec
) ||
2923 ((next_fsync
.tv_sec
== max_fsync
.tv_sec
) &&
2924 (next_fsync
.tv_usec
> max_fsync
.tv_usec
))) {
2925 next_fsync
.tv_sec
= max_fsync
.tv_sec
;
2926 next_fsync
.tv_usec
= max_fsync
.tv_usec
;
2929 add_timeout(&next_fsync
, commit_leases_ackout
, NULL
,
2930 (tvref_t
) NULL
, (tvunref_t
) NULL
);
2935 commit_leases_ackout(void *foo
)
2937 if (outstanding_acks
) {
2940 memset(&max_fsync
, 0, sizeof(max_fsync
));
2944 /* CC: process the delayed ACK responses:
2945 - send out the ACK packets
2946 - move the queue slots to the free list
2949 flush_ackqueue(void *foo
)
2951 struct leasequeue
*ack
, *p
;
2952 /* process from bottom to retain packet order */
2953 for (ack
= ackqueue_tail
; ack
; ack
= p
) {
2956 /* dhcp_reply() requires that the reply state still be valid */
2957 if (ack
->lease
->state
== NULL
)
2958 log_error("delayed ack for %s has gone stale",
2959 piaddr(ack
->lease
->ip_addr
));
2961 dhcp_reply(ack
->lease
);
2963 lease_dereference(&ack
->lease
, MDL
);
2964 ack
->next
= free_ackqueue
;
2965 free_ackqueue
= ack
;
2967 ackqueue_head
= NULL
;
2968 ackqueue_tail
= NULL
;
2969 outstanding_acks
= 0;
2972 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
2974 relinquish_ackqueue(void)
2976 struct leasequeue
*q
, *n
;
2978 for (q
= ackqueue_head
; q
; q
= n
) {
2982 for (q
= free_ackqueue
; q
; q
= n
) {
2989 void dhcp_reply (lease
)
2990 struct lease
*lease
;
2993 unsigned packet_length
;
2994 struct dhcp_packet raw
;
2995 struct sockaddr_in to
;
2996 struct in_addr from
;
2997 struct hardware hto
;
2999 struct lease_state
*state
= lease
-> state
;
3000 int nulltp
, bootpp
, unicastp
= 1;
3001 struct data_string d1
;
3005 log_fatal ("dhcp_reply was supplied lease with no state!");
3007 /* Compose a response for the client... */
3008 memset (&raw
, 0, sizeof raw
);
3009 memset (&d1
, 0, sizeof d1
);
3011 /* Copy in the filename if given; otherwise, flag the filename
3012 buffer as available for options. */
3013 if (state
-> filename
.len
&& state
-> filename
.data
) {
3015 state
-> filename
.data
,
3016 state
-> filename
.len
> sizeof raw
.file
3017 ? sizeof raw
.file
: state
-> filename
.len
);
3018 if (sizeof raw
.file
> state
-> filename
.len
)
3019 memset (&raw
.file
[state
-> filename
.len
], 0,
3020 (sizeof raw
.file
) - state
-> filename
.len
);
3022 log_info("file name longer than packet field "
3023 "truncated - field: %lu name: %d %.*s",
3024 (unsigned long)sizeof(raw
.file
),
3025 state
->filename
.len
, (int)state
->filename
.len
,
3026 state
->filename
.data
);
3030 /* Copy in the server name if given; otherwise, flag the
3031 server_name buffer as available for options. */
3032 if (state
-> server_name
.len
&& state
-> server_name
.data
) {
3034 state
-> server_name
.data
,
3035 state
-> server_name
.len
> sizeof raw
.sname
3036 ? sizeof raw
.sname
: state
-> server_name
.len
);
3037 if (sizeof raw
.sname
> state
-> server_name
.len
)
3038 memset (&raw
.sname
[state
-> server_name
.len
], 0,
3039 (sizeof raw
.sname
) - state
-> server_name
.len
);
3041 log_info("server name longer than packet field "
3042 "truncated - field: %lu name: %d %.*s",
3043 (unsigned long)sizeof(raw
.sname
),
3044 state
->server_name
.len
,
3045 (int)state
->server_name
.len
,
3046 state
->server_name
.data
);
3048 bufs
|= 2; /* XXX */
3051 &lease
-> hardware_addr
.hbuf
[1], sizeof raw
.chaddr
);
3052 raw
.hlen
= lease
-> hardware_addr
.hlen
- 1;
3053 raw
.htype
= lease
-> hardware_addr
.hbuf
[0];
3055 /* See if this is a Microsoft client that NUL-terminates its
3056 strings and expects us to do likewise... */
3057 if (lease
-> flags
& MS_NULL_TERMINATION
)
3062 /* See if this is a bootp client... */
3068 /* Insert such options as will fit into the buffer. */
3069 packet_length
= cons_options (state
-> packet
, &raw
, lease
,
3070 (struct client_state
*)0,
3071 state
-> max_message_size
,
3072 state
-> packet
-> options
,
3073 state
-> options
, &global_scope
,
3074 bufs
, nulltp
, bootpp
,
3075 &state
-> parameter_request_list
,
3078 memcpy (&raw
.ciaddr
, &state
-> ciaddr
, sizeof raw
.ciaddr
);
3079 memcpy (&raw
.yiaddr
, lease
-> ip_addr
.iabuf
, 4);
3080 raw
.siaddr
= state
-> siaddr
;
3081 raw
.giaddr
= state
-> giaddr
;
3083 raw
.xid
= state
-> xid
;
3084 raw
.secs
= state
-> secs
;
3085 raw
.flags
= state
-> bootp_flags
;
3086 raw
.hops
= state
-> hops
;
3089 if (lease
-> client_hostname
) {
3090 if ((strlen (lease
-> client_hostname
) <= 64) &&
3091 db_printable((unsigned char *)lease
->client_hostname
))
3092 s
= lease
-> client_hostname
;
3094 s
= "Hostname Unsuitable for Printing";
3098 /* Say what we're doing... */
3099 log_info ("%s on %s to %s %s%s%svia %s",
3101 ? (state
-> offer
== DHCPACK
? "DHCPACK" : "DHCPOFFER")
3103 piaddr (lease
-> ip_addr
),
3104 (lease
-> hardware_addr
.hlen
3105 ? print_hw_addr (lease
-> hardware_addr
.hbuf
[0],
3106 lease
-> hardware_addr
.hlen
- 1,
3107 &lease
-> hardware_addr
.hbuf
[1])
3108 : print_hex_1(lease
->uid_len
, lease
->uid
, 60)),
3109 s
? "(" : "", s
? s
: "", s
? ") " : "",
3110 (state
-> giaddr
.s_addr
3111 ? inet_ntoa (state
-> giaddr
)
3112 : state
-> ip
-> name
));
3114 /* Set up the hardware address... */
3115 hto
.hlen
= lease
-> hardware_addr
.hlen
;
3116 memcpy (hto
.hbuf
, lease
-> hardware_addr
.hbuf
, hto
.hlen
);
3118 to
.sin_family
= AF_INET
;
3120 to
.sin_len
= sizeof to
;
3122 memset (to
.sin_zero
, 0, sizeof to
.sin_zero
);
3125 dump_raw ((unsigned char *)&raw
, packet_length
);
3128 /* Make sure outgoing packets are at least as big
3129 as a BOOTP packet. */
3130 if (packet_length
< BOOTP_MIN_LEN
)
3131 packet_length
= BOOTP_MIN_LEN
;
3133 /* If this was gatewayed, send it back to the gateway... */
3134 if (raw
.giaddr
.s_addr
) {
3135 to
.sin_addr
= raw
.giaddr
;
3136 if (raw
.giaddr
.s_addr
!= htonl (INADDR_LOOPBACK
))
3137 to
.sin_port
= local_port
;
3139 to
.sin_port
= remote_port
; /* For debugging. */
3141 if (fallback_interface
) {
3142 result
= send_packet (fallback_interface
,
3144 &raw
, packet_length
,
3146 (struct hardware
*)0);
3148 free_lease_state (state
, MDL
);
3149 lease
-> state
= (struct lease_state
*)0;
3153 /* If the client is RENEWING, unicast to the client using the
3154 regular IP stack. Some clients, particularly those that
3155 follow RFC1541, are buggy, and send both ciaddr and server
3156 identifier. We deal with this situation by assuming that
3157 if we got both dhcp-server-identifier and ciaddr, and
3158 giaddr was not set, then the client is on the local
3159 network, and we can therefore unicast or broadcast to it
3160 successfully. A client in REQUESTING state on another
3161 network that's making this mistake will have set giaddr,
3162 and will therefore get a relayed response from the above
3164 } else if (raw
.ciaddr
.s_addr
&&
3165 !((state
-> got_server_identifier
||
3166 (raw
.flags
& htons (BOOTP_BROADCAST
))) &&
3167 /* XXX This won't work if giaddr isn't zero, but it is: */
3168 (state
-> shared_network
==
3169 lease
-> subnet
-> shared_network
)) &&
3170 state
-> offer
== DHCPACK
) {
3171 to
.sin_addr
= raw
.ciaddr
;
3172 to
.sin_port
= remote_port
;
3174 if (fallback_interface
) {
3175 result
= send_packet (fallback_interface
,
3177 &raw
, packet_length
,
3179 (struct hardware
*)0);
3180 free_lease_state (state
, MDL
);
3181 lease
-> state
= (struct lease_state
*)0;
3185 /* If it comes from a client that already knows its address
3186 and is not requesting a broadcast response, and we can
3187 unicast to a client without using the ARP protocol, sent it
3188 directly to that client. */
3189 } else if (!(raw
.flags
& htons (BOOTP_BROADCAST
)) &&
3190 can_unicast_without_arp (state
-> ip
)) {
3191 to
.sin_addr
= raw
.yiaddr
;
3192 to
.sin_port
= remote_port
;
3194 /* Otherwise, broadcast it on the local network. */
3196 to
.sin_addr
= limited_broadcast
;
3197 to
.sin_port
= remote_port
;
3198 if (!(lease
-> flags
& UNICAST_BROADCAST_HACK
))
3202 memcpy (&from
, state
-> from
.iabuf
, sizeof from
);
3204 result
= send_packet (state
-> ip
,
3205 (struct packet
*)0, &raw
, packet_length
,
3207 unicastp
? &hto
: (struct hardware
*)0);
3209 /* Free all of the entries in the option_state structure
3210 now that we're done with them. */
3212 free_lease_state (state
, MDL
);
3213 lease
-> state
= (struct lease_state
*)0;
3216 int find_lease (struct lease
**lp
,
3217 struct packet
*packet
, struct shared_network
*share
, int *ours
,
3218 int *peer_has_leases
, struct lease
*ip_lease_in
,
3219 const char *file
, int line
)
3221 struct lease
*uid_lease
= (struct lease
*)0;
3222 struct lease
*ip_lease
= (struct lease
*)0;
3223 struct lease
*hw_lease
= (struct lease
*)0;
3224 struct lease
*lease
= (struct lease
*)0;
3226 struct host_decl
*hp
= (struct host_decl
*)0;
3227 struct host_decl
*host
= (struct host_decl
*)0;
3228 struct lease
*fixed_lease
= (struct lease
*)0;
3229 struct lease
*next
= (struct lease
*)0;
3230 struct option_cache
*oc
;
3231 struct data_string d1
;
3232 int have_client_identifier
= 0;
3233 struct data_string client_identifier
;
3236 #if defined(FAILOVER_PROTOCOL)
3237 /* Quick check to see if the peer has leases. */
3238 if (peer_has_leases
) {
3241 for (pool
= share
->pools
; pool
; pool
= pool
->next
) {
3242 dhcp_failover_state_t
*peer
= pool
->failover_peer
;
3245 ((peer
->i_am
== primary
&& pool
->backup_leases
) ||
3246 (peer
->i_am
== secondary
&& pool
->free_leases
))) {
3247 *peer_has_leases
= 1;
3252 #endif /* FAILOVER_PROTOCOL */
3254 if (packet
-> raw
-> ciaddr
.s_addr
) {
3256 memcpy (cip
.iabuf
, &packet
-> raw
-> ciaddr
, 4);
3258 /* Look up the requested address. */
3259 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
3260 DHO_DHCP_REQUESTED_ADDRESS
);
3261 memset (&d1
, 0, sizeof d1
);
3263 evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
3264 (struct client_state
*)0,
3266 (struct option_state
*)0,
3267 &global_scope
, oc
, MDL
)) {
3268 packet
-> got_requested_address
= 1;
3270 memcpy (cip
.iabuf
, d1
.data
, cip
.len
);
3271 data_string_forget (&d1
, MDL
);
3276 /* Try to find a host or lease that's been assigned to the
3277 specified unique client identifier. */
3278 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
3279 DHO_DHCP_CLIENT_IDENTIFIER
);
3280 memset (&client_identifier
, 0, sizeof client_identifier
);
3282 evaluate_option_cache (&client_identifier
,
3283 packet
, (struct lease
*)0,
3284 (struct client_state
*)0,
3285 packet
-> options
, (struct option_state
*)0,
3286 &global_scope
, oc
, MDL
)) {
3287 /* Remember this for later. */
3288 have_client_identifier
= 1;
3290 /* First, try to find a fixed host entry for the specified
3291 client identifier... */
3292 if (find_hosts_by_uid (&hp
, client_identifier
.data
,
3293 client_identifier
.len
, MDL
)) {
3294 /* Remember if we know of this client. */
3295 packet
-> known
= 1;
3296 mockup_lease (&fixed_lease
, packet
, share
, hp
);
3299 #if defined (DEBUG_FIND_LEASE)
3301 log_info ("Found host for client identifier: %s.",
3302 piaddr (fixed_lease
-> ip_addr
));
3306 if (!fixed_lease
) /* Save the host if we found one. */
3307 host_reference (&host
, hp
, MDL
);
3308 host_dereference (&hp
, MDL
);
3311 find_lease_by_uid (&uid_lease
, client_identifier
.data
,
3312 client_identifier
.len
, MDL
);
3315 /* If we didn't find a fixed lease using the uid, try doing
3316 it with the hardware address... */
3317 if (!fixed_lease
&& !host
) {
3318 if (find_hosts_by_haddr (&hp
, packet
-> raw
-> htype
,
3319 packet
-> raw
-> chaddr
,
3320 packet
-> raw
-> hlen
, MDL
)) {
3321 /* Remember if we know of this client. */
3322 packet
-> known
= 1;
3324 host_dereference (&host
, MDL
);
3325 host_reference (&host
, hp
, MDL
);
3326 host_dereference (&hp
, MDL
);
3327 mockup_lease (&fixed_lease
, packet
, share
, host
);
3328 #if defined (DEBUG_FIND_LEASE)
3330 log_info ("Found host for link address: %s.",
3331 piaddr (fixed_lease
-> ip_addr
));
3337 /* Finally, if we haven't found anything yet try again with the
3338 * host-identifier option ... */
3339 if (!fixed_lease
&& !host
) {
3340 if (find_hosts_by_option(&hp
, packet
,
3341 packet
->options
, MDL
) == 1) {
3344 host_dereference(&host
, MDL
);
3345 host_reference(&host
, hp
, MDL
);
3346 host_dereference(&hp
, MDL
);
3347 mockup_lease (&fixed_lease
, packet
, share
, host
);
3348 #if defined (DEBUG_FIND_LEASE)
3350 log_info ("Found host via host-identifier");
3356 /* If fixed_lease is present but does not match the requested
3357 IP address, and this is a DHCPREQUEST, then we can't return
3358 any other lease, so we might as well return now. */
3359 if (packet
-> packet_type
== DHCPREQUEST
&& fixed_lease
&&
3360 (fixed_lease
-> ip_addr
.len
!= cip
.len
||
3361 memcmp (fixed_lease
-> ip_addr
.iabuf
,
3362 cip
.iabuf
, cip
.len
))) {
3365 strcpy (dhcp_message
, "requested address is incorrect");
3366 #if defined (DEBUG_FIND_LEASE)
3367 log_info ("Client's fixed-address %s doesn't match %s%s",
3368 piaddr (fixed_lease
-> ip_addr
), "request ",
3369 print_dotted_quads (cip
.len
, cip
.iabuf
));
3375 * If we found leases matching the client identifier, loop through
3376 * the n_uid pointer looking for one that's actually valid. We
3377 * can't do this until we get here because we depend on
3378 * packet -> known, which may be set by either the uid host
3379 * lookup or the haddr host lookup.
3381 * Note that the n_uid lease chain is sorted in order of
3382 * preference, so the first one is the best one.
3385 #if defined (DEBUG_FIND_LEASE)
3386 log_info ("trying next lease matching client id: %s",
3387 piaddr (uid_lease
-> ip_addr
));
3390 #if defined (FAILOVER_PROTOCOL)
3392 * When we lookup a lease by uid, we know the client identifier
3393 * matches the lease's record. If it is active, or was last
3394 * active with the same client, we can trivially extend it.
3395 * If is not or was not active, we can allocate it to this
3396 * client if it matches the usual free/backup criteria (which
3397 * is contained in lease_mine_to_reallocate()).
3399 if (uid_lease
->binding_state
!= FTS_ACTIVE
&&
3400 uid_lease
->rewind_binding_state
!= FTS_ACTIVE
&&
3401 !lease_mine_to_reallocate(uid_lease
)) {
3402 #if defined (DEBUG_FIND_LEASE)
3403 log_info("not active or not mine to allocate: %s",
3404 piaddr(uid_lease
->ip_addr
));
3410 if (uid_lease
-> subnet
-> shared_network
!= share
) {
3411 #if defined (DEBUG_FIND_LEASE)
3412 log_info ("wrong network segment: %s",
3413 piaddr (uid_lease
-> ip_addr
));
3418 if ((uid_lease
-> pool
-> prohibit_list
&&
3419 permitted (packet
, uid_lease
-> pool
-> prohibit_list
)) ||
3420 (uid_lease
-> pool
-> permit_list
&&
3421 !permitted (packet
, uid_lease
-> pool
-> permit_list
))) {
3422 #if defined (DEBUG_FIND_LEASE)
3423 log_info ("not permitted: %s",
3424 piaddr (uid_lease
-> ip_addr
));
3427 if (uid_lease
-> n_uid
)
3428 lease_reference (&next
,
3429 uid_lease
-> n_uid
, MDL
);
3430 if (!packet
-> raw
-> ciaddr
.s_addr
)
3431 release_lease (uid_lease
, packet
);
3432 lease_dereference (&uid_lease
, MDL
);
3434 lease_reference (&uid_lease
, next
, MDL
);
3435 lease_dereference (&next
, MDL
);
3441 #if defined (DEBUG_FIND_LEASE)
3443 log_info ("Found lease for client id: %s.",
3444 piaddr (uid_lease
-> ip_addr
));
3447 /* Find a lease whose hardware address matches, whose client
3448 * identifier matches (or equally doesn't have one), that's
3449 * permitted, and that's on the correct subnet.
3451 * Note that the n_hw chain is sorted in order of preference, so
3452 * the first one found is the best one.
3454 h
.hlen
= packet
-> raw
-> hlen
+ 1;
3455 h
.hbuf
[0] = packet
-> raw
-> htype
;
3456 memcpy (&h
.hbuf
[1], packet
-> raw
-> chaddr
, packet
-> raw
-> hlen
);
3457 find_lease_by_hw_addr (&hw_lease
, h
.hbuf
, h
.hlen
, MDL
);
3459 #if defined (DEBUG_FIND_LEASE)
3460 log_info ("trying next lease matching hw addr: %s",
3461 piaddr (hw_lease
-> ip_addr
));
3463 #if defined (FAILOVER_PROTOCOL)
3465 * When we lookup a lease by chaddr, we know the MAC address
3466 * matches the lease record (we will check if the lease has a
3467 * client-id the client does not next). If the lease is
3468 * currently active or was last active with this client, we can
3469 * trivially extend it. Otherwise, there are a set of rules
3470 * that govern if we can reallocate this lease to any client
3471 * ("lease_mine_to_reallocate()") including this one.
3473 if (hw_lease
->binding_state
!= FTS_ACTIVE
&&
3474 hw_lease
->rewind_binding_state
!= FTS_ACTIVE
&&
3475 !lease_mine_to_reallocate(hw_lease
)) {
3476 #if defined (DEBUG_FIND_LEASE)
3477 log_info("not active or not mine to allocate: %s",
3478 piaddr(hw_lease
->ip_addr
));
3485 * This conditional skips "potentially active" leases (leases
3486 * we think are expired may be extended by the peer, etc) that
3487 * may be assigned to a differently /client-identified/ client
3488 * with the same MAC address.
3490 if (hw_lease
-> binding_state
!= FTS_FREE
&&
3491 hw_lease
-> binding_state
!= FTS_BACKUP
&&
3493 (!have_client_identifier
||
3494 hw_lease
-> uid_len
!= client_identifier
.len
||
3495 memcmp (hw_lease
-> uid
, client_identifier
.data
,
3496 hw_lease
-> uid_len
))) {
3497 #if defined (DEBUG_FIND_LEASE)
3498 log_info ("wrong client identifier: %s",
3499 piaddr (hw_lease
-> ip_addr
));
3503 if (hw_lease
-> subnet
-> shared_network
!= share
) {
3504 #if defined (DEBUG_FIND_LEASE)
3505 log_info ("wrong network segment: %s",
3506 piaddr (hw_lease
-> ip_addr
));
3510 if ((hw_lease
-> pool
-> prohibit_list
&&
3511 permitted (packet
, hw_lease
-> pool
-> prohibit_list
)) ||
3512 (hw_lease
-> pool
-> permit_list
&&
3513 !permitted (packet
, hw_lease
-> pool
-> permit_list
))) {
3514 #if defined (DEBUG_FIND_LEASE)
3515 log_info ("not permitted: %s",
3516 piaddr (hw_lease
-> ip_addr
));
3518 if (!packet
-> raw
-> ciaddr
.s_addr
)
3519 release_lease (hw_lease
, packet
);
3521 if (hw_lease
-> n_hw
)
3522 lease_reference (&next
, hw_lease
-> n_hw
, MDL
);
3523 lease_dereference (&hw_lease
, MDL
);
3525 lease_reference (&hw_lease
, next
, MDL
);
3526 lease_dereference (&next
, MDL
);
3532 #if defined (DEBUG_FIND_LEASE)
3534 log_info ("Found lease for hardware address: %s.",
3535 piaddr (hw_lease
-> ip_addr
));
3538 /* Try to find a lease that's been allocated to the client's
3541 lease_reference (&ip_lease
, ip_lease_in
, MDL
);
3543 find_lease_by_ip_addr (&ip_lease
, cip
, MDL
);
3545 #if defined (DEBUG_FIND_LEASE)
3547 log_info ("Found lease for requested address: %s.",
3548 piaddr (ip_lease
-> ip_addr
));
3551 /* If ip_lease is valid at this point, set ours to one, so that
3552 even if we choose a different lease, we know that the address
3553 the client was requesting was ours, and thus we can NAK it. */
3554 if (ip_lease
&& ours
)
3557 /* If the requested IP address isn't on the network the packet
3558 came from, don't use it. Allow abandoned leases to be matched
3559 here - if the client is requesting it, there's a decent chance
3560 that it's because the lease database got trashed and a client
3561 that thought it had this lease answered an ARP or PING, causing the
3562 lease to be abandoned. If so, this request probably came from
3564 if (ip_lease
&& (ip_lease
-> subnet
-> shared_network
!= share
)) {
3567 #if defined (DEBUG_FIND_LEASE)
3568 log_info ("...but it was on the wrong shared network.");
3570 strcpy (dhcp_message
, "requested address on bad subnet");
3571 lease_dereference (&ip_lease
, MDL
);
3575 * If the requested address is in use (or potentially in use) by
3576 * a different client, it can't be granted.
3578 * This first conditional only detects if the lease is currently
3579 * identified to a different client (client-id and/or chaddr
3580 * mismatch). In this case we may not want to give the client the
3581 * lease, if doing so may potentially be an addressing conflict.
3585 (!have_client_identifier
||
3586 ip_lease
-> uid_len
!= client_identifier
.len
||
3587 memcmp (ip_lease
-> uid
, client_identifier
.data
,
3588 ip_lease
-> uid_len
)) :
3589 (ip_lease
-> hardware_addr
.hbuf
[0] != packet
-> raw
-> htype
||
3590 ip_lease
-> hardware_addr
.hlen
!= packet
-> raw
-> hlen
+ 1 ||
3591 memcmp (&ip_lease
-> hardware_addr
.hbuf
[1],
3592 packet
-> raw
-> chaddr
,
3593 (unsigned)(ip_lease
-> hardware_addr
.hlen
- 1))))) {
3595 * A lease is unavailable for allocation to a new client if
3596 * it is not in the FREE or BACKUP state. There may be
3597 * leases that are in the expired state with a rewinding
3598 * state that is free or backup, but these will be processed
3599 * into the free or backup states by expiration processes, so
3600 * checking for them here is superfluous.
3602 if (ip_lease
-> binding_state
!= FTS_FREE
&&
3603 ip_lease
-> binding_state
!= FTS_BACKUP
) {
3604 #if defined (DEBUG_FIND_LEASE)
3605 log_info ("rejecting lease for requested address.");
3607 /* If we're rejecting it because the peer has
3608 it, don't set "ours", because we shouldn't NAK. */
3609 if (ours
&& ip_lease
-> binding_state
!= FTS_ACTIVE
)
3611 lease_dereference (&ip_lease
, MDL
);
3616 * If we got an ip_lease and a uid_lease or hw_lease, and ip_lease
3617 * is/was not active, and is not ours to reallocate, forget about it.
3619 if (ip_lease
&& (uid_lease
|| hw_lease
) &&
3620 ip_lease
->binding_state
!= FTS_ACTIVE
&&
3621 ip_lease
->rewind_binding_state
!= FTS_ACTIVE
&&
3622 #if defined(FAILOVER_PROTOCOL)
3623 !lease_mine_to_reallocate(ip_lease
) &&
3625 packet
->packet_type
== DHCPDISCOVER
) {
3626 #if defined (DEBUG_FIND_LEASE)
3627 log_info("ip lease not active or not ours to offer.");
3629 lease_dereference(&ip_lease
, MDL
);
3632 /* If for some reason the client has more than one lease
3633 on the subnet that matches its uid, pick the one that
3634 it asked for and (if we can) free the other. */
3635 if (ip_lease
&& ip_lease
->binding_state
== FTS_ACTIVE
&&
3636 ip_lease
->uid
&& ip_lease
!= uid_lease
) {
3637 if (have_client_identifier
&&
3638 (ip_lease
-> uid_len
== client_identifier
.len
) &&
3639 !memcmp (client_identifier
.data
,
3640 ip_lease
-> uid
, ip_lease
-> uid_len
)) {
3642 if (uid_lease
->binding_state
== FTS_ACTIVE
) {
3643 log_error ("client %s has duplicate%s on %s",
3645 (packet
-> raw
-> htype
,
3646 packet
-> raw
-> hlen
,
3647 packet
-> raw
-> chaddr
)),
3649 (ip_lease
-> subnet
->
3650 shared_network
-> name
));
3652 /* If the client is REQUESTing the lease,
3653 it shouldn't still be using the old
3654 one, so we can free it for allocation. */
3656 uid_lease
->binding_state
== FTS_ACTIVE
&&
3657 !packet
-> raw
-> ciaddr
.s_addr
&&
3659 uid_lease
-> subnet
-> shared_network
) &&
3660 packet
-> packet_type
== DHCPREQUEST
)
3661 release_lease (uid_lease
, packet
);
3663 lease_dereference (&uid_lease
, MDL
);
3664 lease_reference (&uid_lease
, ip_lease
, MDL
);
3668 /* If we get to here and fixed_lease is not null, that means
3669 that there are both a dynamic lease and a fixed-address
3670 declaration for the same IP address. */
3671 if (packet
-> packet_type
== DHCPREQUEST
&& fixed_lease
) {
3672 lease_dereference (&fixed_lease
, MDL
);
3674 log_error ("Dynamic and static leases present for %s.",
3676 log_error ("Remove host declaration %s or remove %s",
3677 (fixed_lease
&& fixed_lease
-> host
3678 ? (fixed_lease
-> host
-> name
3679 ? fixed_lease
-> host
-> name
3683 log_error ("from the dynamic address pool for %s",
3684 ip_lease
-> subnet
-> shared_network
-> name
3687 lease_dereference (&ip_lease
, MDL
);
3688 strcpy (dhcp_message
,
3689 "database conflict - call for help!");
3692 if (ip_lease
&& ip_lease
!= uid_lease
) {
3693 #if defined (DEBUG_FIND_LEASE)
3694 log_info ("requested address not available.");
3696 lease_dereference (&ip_lease
, MDL
);
3700 /* If we get to here with both fixed_lease and ip_lease not
3701 null, then we have a configuration file bug. */
3702 if (packet
-> packet_type
== DHCPREQUEST
&& fixed_lease
&& ip_lease
)
3705 /* Toss extra pointers to the same lease... */
3706 if (hw_lease
&& hw_lease
== uid_lease
) {
3707 #if defined (DEBUG_FIND_LEASE)
3708 log_info ("hardware lease and uid lease are identical.");
3710 lease_dereference (&hw_lease
, MDL
);
3712 if (ip_lease
&& ip_lease
== hw_lease
) {
3713 lease_dereference (&hw_lease
, MDL
);
3714 #if defined (DEBUG_FIND_LEASE)
3715 log_info ("hardware lease and ip lease are identical.");
3718 if (ip_lease
&& ip_lease
== uid_lease
) {
3719 lease_dereference (&uid_lease
, MDL
);
3720 #if defined (DEBUG_FIND_LEASE)
3721 log_info ("uid lease and ip lease are identical.");
3725 /* Make sure the client is permitted to use the requested lease. */
3727 ((ip_lease
-> pool
-> prohibit_list
&&
3728 permitted (packet
, ip_lease
-> pool
-> prohibit_list
)) ||
3729 (ip_lease
-> pool
-> permit_list
&&
3730 !permitted (packet
, ip_lease
-> pool
-> permit_list
)))) {
3731 if (!packet
->raw
->ciaddr
.s_addr
&&
3732 (ip_lease
->binding_state
== FTS_ACTIVE
))
3733 release_lease (ip_lease
, packet
);
3735 lease_dereference (&ip_lease
, MDL
);
3739 ((uid_lease
-> pool
-> prohibit_list
&&
3740 permitted (packet
, uid_lease
-> pool
-> prohibit_list
)) ||
3741 (uid_lease
-> pool
-> permit_list
&&
3742 !permitted (packet
, uid_lease
-> pool
-> permit_list
)))) {
3743 if (!packet
-> raw
-> ciaddr
.s_addr
)
3744 release_lease (uid_lease
, packet
);
3745 lease_dereference (&uid_lease
, MDL
);
3749 ((hw_lease
-> pool
-> prohibit_list
&&
3750 permitted (packet
, hw_lease
-> pool
-> prohibit_list
)) ||
3751 (hw_lease
-> pool
-> permit_list
&&
3752 !permitted (packet
, hw_lease
-> pool
-> permit_list
)))) {
3753 if (!packet
-> raw
-> ciaddr
.s_addr
)
3754 release_lease (hw_lease
, packet
);
3755 lease_dereference (&hw_lease
, MDL
);
3758 /* If we've already eliminated the lease, it wasn't there to
3759 begin with. If we have come up with a matching lease,
3760 set the message to bad network in case we have to throw it out. */
3762 strcpy (dhcp_message
, "requested address not available");
3765 /* If this is a DHCPREQUEST, make sure the lease we're going to return
3766 matches the requested IP address. If it doesn't, don't return a
3768 if (packet
-> packet_type
== DHCPREQUEST
&&
3769 !ip_lease
&& !fixed_lease
) {
3770 #if defined (DEBUG_FIND_LEASE)
3771 log_info ("no applicable lease found for DHCPREQUEST.");
3776 /* At this point, if fixed_lease is nonzero, we can assign it to
3779 lease_reference (&lease
, fixed_lease
, MDL
);
3780 lease_dereference (&fixed_lease
, MDL
);
3781 #if defined (DEBUG_FIND_LEASE)
3782 log_info ("choosing fixed address.");
3786 /* If we got a lease that matched the ip address and don't have
3787 a better offer, use that; otherwise, release it. */
3790 if (!packet
-> raw
-> ciaddr
.s_addr
)
3791 release_lease (ip_lease
, packet
);
3792 #if defined (DEBUG_FIND_LEASE)
3793 log_info ("not choosing requested address (!).");
3796 #if defined (DEBUG_FIND_LEASE)
3797 log_info ("choosing lease on requested address.");
3799 lease_reference (&lease
, ip_lease
, MDL
);
3801 host_dereference (&lease
-> host
, MDL
);
3803 lease_dereference (&ip_lease
, MDL
);
3806 /* If we got a lease that matched the client identifier, we may want
3807 to use it, but if we already have a lease we like, we must free
3808 the lease that matched the client identifier. */
3811 log_error("uid lease %s for client %s is duplicate "
3813 piaddr(uid_lease
->ip_addr
),
3814 print_hw_addr(packet
->raw
->htype
,
3816 packet
->raw
->chaddr
),
3817 uid_lease
->subnet
->shared_network
->name
);
3819 if (!packet
-> raw
-> ciaddr
.s_addr
&&
3820 packet
-> packet_type
== DHCPREQUEST
&&
3821 uid_lease
-> binding_state
== FTS_ACTIVE
)
3822 release_lease(uid_lease
, packet
);
3823 #if defined (DEBUG_FIND_LEASE)
3824 log_info ("not choosing uid lease.");
3827 lease_reference (&lease
, uid_lease
, MDL
);
3829 host_dereference (&lease
-> host
, MDL
);
3830 #if defined (DEBUG_FIND_LEASE)
3831 log_info ("choosing uid lease.");
3834 lease_dereference (&uid_lease
, MDL
);
3837 /* The lease that matched the hardware address is treated likewise. */
3840 #if defined (DEBUG_FIND_LEASE)
3841 log_info ("not choosing hardware lease.");
3844 /* We're a little lax here - if the client didn't
3845 send a client identifier and it's a bootp client,
3846 but the lease has a client identifier, we still
3847 let the client have a lease. */
3848 if (!hw_lease
-> uid_len
||
3849 (have_client_identifier
3850 ? (hw_lease
-> uid_len
==
3851 client_identifier
.len
&&
3852 !memcmp (hw_lease
-> uid
,
3853 client_identifier
.data
,
3854 client_identifier
.len
))
3855 : packet
-> packet_type
== 0)) {
3856 lease_reference (&lease
, hw_lease
, MDL
);
3858 host_dereference (&lease
-> host
, MDL
);
3859 #if defined (DEBUG_FIND_LEASE)
3860 log_info ("choosing hardware lease.");
3863 #if defined (DEBUG_FIND_LEASE)
3864 log_info ("not choosing hardware lease: %s.",
3869 lease_dereference (&hw_lease
, MDL
);
3873 * If we found a host_decl but no matching address, try to
3874 * find a host_decl that has no address, and if there is one,
3875 * hang it off the lease so that we can use the supplied
3878 if (lease
&& host
&& !lease
->host
) {
3879 struct host_decl
*p
= NULL
;
3880 struct host_decl
*n
= NULL
;
3882 host_reference(&p
, host
, MDL
);
3884 if (!p
->fixed_addr
) {
3886 * If the lease is currently active, then it
3887 * must be allocated to the present client.
3888 * We store a reference to the host record on
3889 * the lease to save a lookup later (in
3890 * ack_lease()). We mustn't refer to the host
3891 * record on non-active leases because the
3892 * client may be denied later.
3894 * XXX: Not having this reference (such as in
3895 * DHCPDISCOVER/INIT) means ack_lease will have
3896 * to perform this lookup a second time. This
3897 * hopefully isn't a problem as DHCPREQUEST is
3898 * more common than DHCPDISCOVER.
3900 if (lease
->binding_state
== FTS_ACTIVE
)
3901 host_reference(&lease
->host
, p
, MDL
);
3903 host_dereference(&p
, MDL
);
3906 if (p
->n_ipaddr
!= NULL
)
3907 host_reference(&n
, p
->n_ipaddr
, MDL
);
3908 host_dereference(&p
, MDL
);
3910 host_reference(&p
, n
, MDL
);
3911 host_dereference(&n
, MDL
);
3916 /* If we find an abandoned lease, but it's the one the client
3917 requested, we assume that previous bugginess on the part
3918 of the client, or a server database loss, caused the lease to
3919 be abandoned, so we reclaim it and let the client have it. */
3921 (lease
-> binding_state
== FTS_ABANDONED
) &&
3922 lease
== ip_lease
&&
3923 packet
-> packet_type
== DHCPREQUEST
) {
3924 log_error ("Reclaiming REQUESTed abandoned IP address %s.",
3925 piaddr (lease
-> ip_addr
));
3926 } else if (lease
&& (lease
-> binding_state
== FTS_ABANDONED
)) {
3927 /* Otherwise, if it's not the one the client requested, we do not
3928 return it - instead, we claim it's ours, causing a DHCPNAK to be
3929 sent if this lookup is for a DHCPREQUEST, and force the client
3930 to go back through the allocation process. */
3933 lease_dereference (&lease
, MDL
);
3937 if (have_client_identifier
)
3938 data_string_forget (&client_identifier
, MDL
);
3941 lease_dereference (&fixed_lease
, MDL
);
3943 lease_dereference (&hw_lease
, MDL
);
3945 lease_dereference (&uid_lease
, MDL
);
3947 lease_dereference (&ip_lease
, MDL
);
3949 host_dereference (&host
, MDL
);
3952 #if defined (DEBUG_FIND_LEASE)
3953 log_info ("Returning lease: %s.",
3954 piaddr (lease
-> ip_addr
));
3956 lease_reference (lp
, lease
, file
, line
);
3957 lease_dereference (&lease
, MDL
);
3960 #if defined (DEBUG_FIND_LEASE)
3961 log_info ("Not returning a lease.");
3966 /* Search the provided host_decl structure list for an address that's on
3967 the specified shared network. If one is found, mock up and return a
3968 lease structure for it; otherwise return the null pointer. */
3970 int mockup_lease (struct lease
**lp
, struct packet
*packet
,
3971 struct shared_network
*share
, struct host_decl
*hp
)
3973 struct lease
*lease
= (struct lease
*)0;
3974 struct host_decl
*rhp
= (struct host_decl
*)0;
3976 if (lease_allocate (&lease
, MDL
) != ISC_R_SUCCESS
)
3978 if (host_reference (&rhp
, hp
, MDL
) != ISC_R_SUCCESS
) {
3979 lease_dereference (&lease
, MDL
);
3982 if (!find_host_for_network (&lease
-> subnet
,
3983 &rhp
, &lease
-> ip_addr
, share
)) {
3984 lease_dereference (&lease
, MDL
);
3985 host_dereference (&rhp
, MDL
);
3988 host_reference (&lease
-> host
, rhp
, MDL
);
3989 if (rhp
-> client_identifier
.len
> sizeof lease
-> uid_buf
)
3990 lease
-> uid
= dmalloc (rhp
-> client_identifier
.len
, MDL
);
3992 lease
-> uid
= lease
-> uid_buf
;
3993 if (!lease
-> uid
) {
3994 lease_dereference (&lease
, MDL
);
3995 host_dereference (&rhp
, MDL
);
3998 memcpy (lease
-> uid
, rhp
-> client_identifier
.data
,
3999 rhp
-> client_identifier
.len
);
4000 lease
-> uid_len
= rhp
-> client_identifier
.len
;
4001 lease
-> hardware_addr
= rhp
-> interface
;
4002 lease
-> starts
= lease
-> cltt
= lease
-> ends
= MIN_TIME
;
4003 lease
-> flags
= STATIC_LEASE
;
4004 lease
-> binding_state
= FTS_FREE
;
4006 lease_reference (lp
, lease
, MDL
);
4008 lease_dereference (&lease
, MDL
);
4009 host_dereference (&rhp
, MDL
);
4013 /* Look through all the pools in a list starting with the specified pool
4014 for a free lease. We try to find a virgin lease if we can. If we
4015 don't find a virgin lease, we try to find a non-virgin lease that's
4016 free. If we can't find one of those, we try to reclaim an abandoned
4017 lease. If all of these possibilities fail to pan out, we don't return
4020 int allocate_lease (struct lease
**lp
, struct packet
*packet
,
4021 struct pool
*pool
, int *peer_has_leases
)
4023 struct lease
*lease
= (struct lease
*)0;
4024 struct lease
*candl
= (struct lease
*)0;
4026 for (; pool
; pool
= pool
-> next
) {
4027 if ((pool
-> prohibit_list
&&
4028 permitted (packet
, pool
-> prohibit_list
)) ||
4029 (pool
-> permit_list
&&
4030 !permitted (packet
, pool
-> permit_list
)))
4033 #if defined (FAILOVER_PROTOCOL)
4034 /* Peer_has_leases just says that we found at least one
4035 free lease. If no free lease is returned, the caller
4036 can deduce that this means the peer is hogging all the
4037 free leases, so we can print a better error message. */
4038 /* XXX Do we need code here to ignore PEER_IS_OWNER and
4039 * XXX just check tstp if we're in, e.g., PARTNER_DOWN?
4040 * XXX Where do we deal with CONFLICT_DETECTED, et al? */
4041 /* XXX This should be handled by the lease binding "state
4042 * XXX machine" - that is, when we get here, if a lease
4043 * XXX could be allocated, it will have the correct
4044 * XXX binding state so that the following code will
4045 * XXX result in its being allocated. */
4046 /* Skip to the most expired lease in the pool that is not
4047 * owned by a failover peer. */
4048 if (pool
->failover_peer
!= NULL
) {
4049 if (pool
->failover_peer
->i_am
== primary
) {
4053 * In normal operation, we never want to touch
4054 * the peer's leases. In partner-down
4055 * operation, we need to be able to pick up
4056 * the peer's leases after STOS+MCLT.
4058 if (pool
->backup
!= NULL
) {
4059 if (((candl
== NULL
) ||
4061 pool
->backup
->ends
)) &&
4062 lease_mine_to_reallocate(
4064 candl
= pool
->backup
;
4066 *peer_has_leases
= 1;
4070 candl
= pool
->backup
;
4072 if (pool
->free
!= NULL
) {
4073 if (((candl
== NULL
) ||
4075 pool
->free
->ends
)) &&
4076 lease_mine_to_reallocate(
4080 *peer_has_leases
= 1;
4085 /* Try abandoned leases as a last resort. */
4086 if ((candl
== NULL
) &&
4087 (pool
->abandoned
!= NULL
) &&
4088 lease_mine_to_reallocate(pool
->abandoned
))
4089 candl
= pool
->abandoned
;
4094 candl
= pool
-> free
;
4096 candl
= pool
-> abandoned
;
4100 * XXX: This may not match with documented expectation.
4101 * It's expected that when we OFFER a lease, we set its
4102 * ends time forward 2 minutes so that it gets sorted to
4103 * the end of its free list (avoiding a similar allocation
4104 * to another client). It is not expected that we issue a
4105 * "no free leases" error when the last lease has been
4106 * offered, but it's not exactly broken either.
4108 if (!candl
|| (candl
-> ends
> cur_time
))
4117 * There are tiers of lease state preference, listed here in
4118 * reverse order (least to most preferential):
4123 * If the selected lease and candidate are both of the same
4124 * state, select the oldest (longest ago) expiration time
4125 * between the two. If the candidate lease is of a higher
4126 * preferred grade over the selected lease, use it.
4128 if ((lease
-> binding_state
== FTS_ABANDONED
) &&
4129 ((candl
-> binding_state
!= FTS_ABANDONED
) ||
4130 (candl
-> ends
< lease
-> ends
))) {
4133 } else if (candl
-> binding_state
== FTS_ABANDONED
)
4136 if ((lease
-> uid_len
|| lease
-> hardware_addr
.hlen
) &&
4137 ((!candl
-> uid_len
&& !candl
-> hardware_addr
.hlen
) ||
4138 (candl
-> ends
< lease
-> ends
))) {
4141 } else if (candl
-> uid_len
|| candl
-> hardware_addr
.hlen
)
4144 if (candl
-> ends
< lease
-> ends
)
4148 if (lease
!= NULL
) {
4149 if (lease
->binding_state
== FTS_ABANDONED
)
4150 log_error("Reclaiming abandoned lease %s.",
4151 piaddr(lease
->ip_addr
));
4154 * XXX: For reliability, we go ahead and remove the host
4155 * record and try to move on. For correctness, if there
4156 * are any other stale host vectors, we want to find them.
4158 if (lease
->host
!= NULL
) {
4159 log_debug("soft impossible condition (%s:%d): stale "
4160 "host \"%s\" found on lease %s", MDL
,
4162 piaddr(lease
->ip_addr
));
4163 host_dereference(&lease
->host
, MDL
);
4166 lease_reference (lp
, lease
, MDL
);
4173 /* Determine whether or not a permit exists on a particular permit list
4174 that matches the specified packet, returning nonzero if so, zero if
4177 int permitted (packet
, permit_list
)
4178 struct packet
*packet
;
4179 struct permit
*permit_list
;
4184 for (p
= permit_list
; p
; p
= p
-> next
) {
4185 switch (p
-> type
) {
4186 case permit_unknown_clients
:
4187 if (!packet
-> known
)
4191 case permit_known_clients
:
4192 if (packet
-> known
)
4196 case permit_authenticated_clients
:
4197 if (packet
-> authenticated
)
4201 case permit_unauthenticated_clients
:
4202 if (!packet
-> authenticated
)
4206 case permit_all_clients
:
4209 case permit_dynamic_bootp_clients
:
4210 if (!packet
-> options_valid
||
4211 !packet
-> packet_type
)
4216 for (i
= 0; i
< packet
-> class_count
; i
++) {
4217 if (p
-> class == packet
-> classes
[i
])
4219 if (packet
-> classes
[i
] &&
4220 packet
-> classes
[i
] -> superclass
&&
4221 (packet
-> classes
[i
] -> superclass
==
4228 if (cur_time
> p
->after
)
4236 int locate_network (packet
)
4237 struct packet
*packet
;
4240 struct data_string data
;
4241 struct subnet
*subnet
= (struct subnet
*)0;
4242 struct option_cache
*oc
;
4244 /* See if there's a Relay Agent Link Selection Option, or a
4245 * Subnet Selection Option. The Link-Select and Subnet-Select
4246 * are formatted and used precisely the same, but we must prefer
4247 * the link-select over the subnet-select.
4249 if ((oc
= lookup_option(&agent_universe
, packet
->options
,
4250 RAI_LINK_SELECT
)) == NULL
)
4251 oc
= lookup_option(&dhcp_universe
, packet
->options
,
4252 DHO_SUBNET_SELECTION
);
4254 /* If there's no SSO and no giaddr, then use the shared_network
4255 from the interface, if there is one. If not, fail. */
4256 if (!oc
&& !packet
-> raw
-> giaddr
.s_addr
) {
4257 if (packet
-> interface
-> shared_network
) {
4258 shared_network_reference
4259 (&packet
-> shared_network
,
4260 packet
-> interface
-> shared_network
, MDL
);
4266 /* If there's an option indicating link connection, and it's valid,
4267 * use it to figure out the subnet. If it's not valid, fail.
4270 memset (&data
, 0, sizeof data
);
4271 if (!evaluate_option_cache (&data
, packet
, (struct lease
*)0,
4272 (struct client_state
*)0,
4274 (struct option_state
*)0,
4275 &global_scope
, oc
, MDL
)) {
4278 if (data
.len
!= 4) {
4282 memcpy (ia
.iabuf
, data
.data
, 4);
4283 data_string_forget (&data
, MDL
);
4286 memcpy (ia
.iabuf
, &packet
-> raw
-> giaddr
, 4);
4289 /* If we know the subnet on which the IP address lives, use it. */
4290 if (find_subnet (&subnet
, ia
, MDL
)) {
4291 shared_network_reference (&packet
-> shared_network
,
4292 subnet
-> shared_network
, MDL
);
4293 subnet_dereference (&subnet
, MDL
);
4297 /* Otherwise, fail. */
4302 * Try to figure out the source address to send packets from.
4304 * If the packet we received specified the server address, then we
4307 * Otherwise, use the first address from the interface. If we do
4308 * this, we also save this into the option cache as the server
4312 get_server_source_address(struct in_addr
*from
,
4313 struct option_state
*options
,
4314 struct packet
*packet
) {
4315 unsigned option_num
;
4316 struct option_cache
*oc
;
4317 struct data_string d
;
4320 memset(&d
, 0, sizeof(d
));
4322 option_num
= DHO_DHCP_SERVER_IDENTIFIER
;
4323 oc
= lookup_option(&dhcp_universe
, options
, option_num
);
4325 evaluate_option_cache(&d
, packet
, NULL
, NULL
, packet
->options
,
4326 options
, &global_scope
, oc
, MDL
)) {
4327 if (d
.len
== sizeof(*from
)) {
4328 memcpy(from
, d
.data
, sizeof(*from
));
4329 data_string_forget(&d
, MDL
);
4332 data_string_forget(&d
, MDL
);
4335 if (packet
->interface
->address_count
> 0) {
4336 if (option_cache_allocate(&oc
, MDL
)) {
4337 a
= &packet
->interface
->addresses
[0];
4338 if (make_const_data(&oc
->expression
,
4339 (unsigned char *)a
, sizeof(*a
),
4341 option_code_hash_lookup(&oc
->option
,
4342 dhcp_universe
.code_hash
,
4343 &option_num
, 0, MDL
);
4344 save_option(&dhcp_universe
, options
, oc
);
4346 option_cache_dereference(&oc
, MDL
);
4348 *from
= packet
->interface
->addresses
[0];
4350 memset(from
, 0, sizeof(*from
));
4355 * Look for the lowest numbered site code number and
4356 * apply a log warning if it is less than 224. Do not
4357 * permit site codes less than 128 (old code never did).
4359 * Note that we could search option codes 224 down to 128
4360 * on the hash table, but the table is (probably) smaller
4361 * than that if it was declared as a standalone table with
4362 * defaults. So we traverse the option code hash.
4365 find_min_site_code(struct universe
*u
)
4367 if (u
->site_code_min
)
4368 return u
->site_code_min
;
4371 * Note that site_code_min has to be global as we can't pass an
4372 * argument through hash_foreach(). The value 224 is taken from
4375 site_code_min
= 224;
4376 option_code_hash_foreach(u
->code_hash
, lowest_site_code
);
4378 if (site_code_min
< 224) {
4379 log_error("WARNING: site-local option codes less than 224 have "
4380 "been deprecated by RFC3942. You have options "
4381 "listed in site local space %s that number as low as "
4382 "%d. Please investigate if these should be declared "
4383 "as regular options rather than site-local options, "
4384 "or migrated up past 224.",
4385 u
->name
, site_code_min
);
4389 * don't even bother logging, this is just silly, and never worked
4390 * on any old version of software.
4392 if (site_code_min
< 128)
4393 site_code_min
= 128;
4396 * Cache the determined minimum site code on the universe structure.
4397 * Note that due to the < 128 check above, a value of zero is
4400 u
->site_code_min
= site_code_min
;
4402 return site_code_min
;
4406 lowest_site_code(const void *key
, unsigned len
, void *object
)
4408 struct option
*option
= object
;
4410 if (option
->code
< site_code_min
)
4411 site_code_min
= option
->code
;
4413 return ISC_R_SUCCESS
;
4417 maybe_return_agent_options(struct packet
*packet
, struct option_state
*options
)
4419 /* If there were agent options in the incoming packet, return
4420 * them. Do not return the agent options if they were stashed
4421 * on the lease. We do not check giaddr to detect the presence of
4422 * a relay, as this excludes "l2" relay agents which have no giaddr
4425 * XXX: If the user configures options for the relay agent information
4426 * (state->options->universes[agent_universe.index] is not NULL),
4427 * we're still required to duplicate other values provided by the
4428 * relay agent. So we need to merge the old values not configured
4429 * by the user into the new state, not just give up.
4431 if (!packet
->agent_options_stashed
&&
4432 packet
->options
->universe_count
> agent_universe
.index
&&
4433 packet
->options
->universes
[agent_universe
.index
] != NULL
&&
4434 (options
->universe_count
<= agent_universe
.index
||
4435 options
->universes
[agent_universe
.index
] == NULL
)) {
4436 option_chain_head_reference
4437 ((struct option_chain_head
**)
4438 &(options
->universes
[agent_universe
.index
]),
4439 (struct option_chain_head
*)
4440 packet
->options
->universes
[agent_universe
.index
], MDL
);
4442 if (options
->universe_count
<= agent_universe
.index
)
4443 options
->universe_count
= agent_universe
.index
+ 1;