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 i
= sizeof raw
.file
;
1091 memcpy (raw
.file
, d1
.data
, i
);
1092 data_string_forget (&d1
, MDL
);
1095 /* Choose a server name as above. */
1096 oc
= lookup_option (&server_universe
, options
, SV_SERVER_NAME
);
1098 evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
1099 (struct client_state
*)0,
1100 packet
-> options
, (struct option_state
*)0,
1101 &global_scope
, oc
, MDL
)) {
1103 if (i
> sizeof raw
.sname
)
1104 i
= sizeof raw
.sname
;
1107 memcpy (raw
.sname
, d1
.data
, i
);
1108 data_string_forget (&d1
, MDL
);
1111 /* Set a flag if this client is a lame Microsoft client that NUL
1112 terminates string options and expects us to do likewise. */
1114 if ((oc
= lookup_option (&dhcp_universe
, packet
-> options
,
1116 if (!oc
->expression
)
1117 nulltp
= oc
->flags
& OPTION_HAD_NULLS
;
1120 /* Put in DHCP-specific options. */
1121 i
= DHO_DHCP_MESSAGE_TYPE
;
1122 oc
= (struct option_cache
*)0;
1123 if (option_cache_allocate (&oc
, MDL
)) {
1124 if (make_const_data (&oc
-> expression
,
1125 &dhcpack
, 1, 0, 0, MDL
)) {
1126 option_code_hash_lookup(&oc
->option
,
1127 dhcp_universe
.code_hash
,
1129 save_option (&dhcp_universe
, options
, oc
);
1131 option_cache_dereference (&oc
, MDL
);
1134 get_server_source_address(&from
, options
, packet
);
1136 /* Use the subnet mask from the subnet declaration if no other
1137 mask has been provided. */
1138 i
= DHO_SUBNET_MASK
;
1139 if (subnet
&& !lookup_option (&dhcp_universe
, options
, i
)) {
1140 oc
= (struct option_cache
*)0;
1141 if (option_cache_allocate (&oc
, MDL
)) {
1142 if (make_const_data (&oc
-> expression
,
1143 subnet
-> netmask
.iabuf
,
1144 subnet
-> netmask
.len
,
1146 option_code_hash_lookup(&oc
->option
,
1147 dhcp_universe
.code_hash
,
1149 save_option (&dhcp_universe
, options
, oc
);
1151 option_cache_dereference (&oc
, MDL
);
1155 /* If a site option space has been specified, use that for
1156 site option codes. */
1157 i
= SV_SITE_OPTION_SPACE
;
1158 if ((oc
= lookup_option (&server_universe
, options
, i
)) &&
1159 evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
1160 (struct client_state
*)0,
1161 packet
-> options
, options
,
1162 &global_scope
, oc
, MDL
)) {
1163 struct universe
*u
= (struct universe
*)0;
1165 if (!universe_hash_lookup (&u
, universe_hash
,
1166 (const char *)d1
.data
, d1
.len
,
1168 log_error ("unknown option space %s.", d1
.data
);
1169 option_state_dereference (&options
, MDL
);
1171 subnet_dereference (&subnet
, MDL
);
1175 options
-> site_universe
= u
-> index
;
1176 options
->site_code_min
= find_min_site_code(u
);
1177 data_string_forget (&d1
, MDL
);
1179 options
-> site_universe
= dhcp_universe
.index
;
1180 options
-> site_code_min
= 0; /* Trust me, it works. */
1183 memset (&prl
, 0, sizeof prl
);
1185 /* Use the parameter list from the scope if there is one. */
1186 oc
= lookup_option (&dhcp_universe
, options
,
1187 DHO_DHCP_PARAMETER_REQUEST_LIST
);
1189 /* Otherwise, if the client has provided a list of options
1190 that it wishes returned, use it to prioritize. Otherwise,
1191 prioritize based on the default priority list. */
1194 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
1195 DHO_DHCP_PARAMETER_REQUEST_LIST
);
1198 evaluate_option_cache (&prl
, packet
, (struct lease
*)0,
1199 (struct client_state
*)0,
1200 packet
-> options
, options
,
1201 &global_scope
, oc
, MDL
);
1204 dump_packet (packet
);
1205 dump_raw ((unsigned char *)packet
-> raw
, packet
-> packet_length
);
1208 log_info ("%s", msgbuf
);
1210 /* Figure out the address of the boot file server. */
1212 lookup_option (&server_universe
, options
, SV_NEXT_SERVER
))) {
1213 if (evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
1214 (struct client_state
*)0,
1215 packet
-> options
, options
,
1216 &global_scope
, oc
, MDL
)) {
1217 /* If there was more than one answer,
1219 if (d1
.len
>= 4 && d1
.data
)
1220 memcpy (&raw
.siaddr
, d1
.data
, 4);
1221 data_string_forget (&d1
, MDL
);
1225 /* Set up the option buffer... */
1226 outgoing
.packet_length
=
1227 cons_options (packet
, outgoing
.raw
, (struct lease
*)0,
1228 (struct client_state
*)0,
1229 0, packet
-> options
, options
, &global_scope
,
1231 prl
.len
? &prl
: (struct data_string
*)0,
1233 option_state_dereference (&options
, MDL
);
1234 data_string_forget (&prl
, MDL
);
1236 /* Make sure that the packet is at least as big as a BOOTP packet. */
1237 if (outgoing
.packet_length
< BOOTP_MIN_LEN
)
1238 outgoing
.packet_length
= BOOTP_MIN_LEN
;
1240 raw
.giaddr
= packet
-> raw
-> giaddr
;
1241 raw
.ciaddr
= packet
-> raw
-> ciaddr
;
1242 memcpy (raw
.chaddr
, packet
-> raw
-> chaddr
, sizeof raw
.chaddr
);
1243 raw
.hlen
= packet
-> raw
-> hlen
;
1244 raw
.htype
= packet
-> raw
-> htype
;
1246 raw
.xid
= packet
-> raw
-> xid
;
1247 raw
.secs
= packet
-> raw
-> secs
;
1248 raw
.flags
= packet
-> raw
-> flags
;
1249 raw
.hops
= packet
-> raw
-> hops
;
1253 dump_packet (&outgoing
);
1254 dump_raw ((unsigned char *)&raw
, outgoing
.packet_length
);
1257 /* Set up the common stuff... */
1258 to
.sin_family
= AF_INET
;
1260 to
.sin_len
= sizeof to
;
1262 memset (to
.sin_zero
, 0, sizeof to
.sin_zero
);
1264 /* RFC2131 states the server SHOULD unciast to ciaddr.
1265 * There are two wrinkles - relays, and when ciaddr is zero.
1266 * There's actually no mention of relays at all in rfc2131 in
1267 * regard to DHCPINFORM, except to say we might get packets from
1268 * clients via them. Note: relays unicast to clients to the
1269 * "yiaddr" address, which servers are forbidden to set when
1270 * answering an inform.
1272 * The solution: If ciaddr is zero, and giaddr is set, go via the
1273 * relay with the broadcast flag set to help the relay (with no
1274 * yiaddr and very likely no chaddr, it will have no idea where to
1277 * If the ciaddr is zero and giaddr is not set, go via the source
1278 * IP address (but you are permitted to barf on their shoes).
1280 * If ciaddr is not zero, send the packet there always.
1282 if (!raw
.ciaddr
.s_addr
&& gip
.len
) {
1283 memcpy(&to
.sin_addr
, gip
.iabuf
, 4);
1284 to
.sin_port
= local_port
;
1285 raw
.flags
|= htons(BOOTP_BROADCAST
);
1288 memcpy(&to
.sin_addr
, cip
.iabuf
, 4);
1289 to
.sin_port
= remote_port
;
1292 /* Report what we're sending. */
1293 snprintf(msgbuf
, sizeof msgbuf
, "DHCPACK to %s (%s) via", piaddr(cip
),
1294 (packet
->raw
->htype
&& packet
->raw
->hlen
) ?
1295 print_hw_addr(packet
->raw
->htype
, packet
->raw
->hlen
,
1296 packet
->raw
->chaddr
) :
1297 "<no client hardware address>");
1298 log_info("%s %s", msgbuf
, gip
.len
? piaddr(gip
) :
1299 packet
->interface
->name
);
1302 send_packet ((fallback_interface
1303 ? fallback_interface
: packet
-> interface
),
1304 &outgoing
, &raw
, outgoing
.packet_length
,
1305 from
, &to
, (struct hardware
*)0);
1307 subnet_dereference (&subnet
, MDL
);
1310 void nak_lease (packet
, cip
)
1311 struct packet
*packet
;
1314 struct sockaddr_in to
;
1315 struct in_addr from
;
1317 struct dhcp_packet raw
;
1318 unsigned char nak
= DHCPNAK
;
1319 struct packet outgoing
;
1321 struct option_state
*options
= (struct option_state
*)0;
1322 struct option_cache
*oc
= (struct option_cache
*)0;
1324 option_state_allocate (&options
, MDL
);
1325 memset (&outgoing
, 0, sizeof outgoing
);
1326 memset (&raw
, 0, sizeof raw
);
1327 outgoing
.raw
= &raw
;
1329 /* Set DHCP_MESSAGE_TYPE to DHCPNAK */
1330 if (!option_cache_allocate (&oc
, MDL
)) {
1331 log_error ("No memory for DHCPNAK message type.");
1332 option_state_dereference (&options
, MDL
);
1335 if (!make_const_data (&oc
-> expression
, &nak
, sizeof nak
,
1337 log_error ("No memory for expr_const expression.");
1338 option_cache_dereference (&oc
, MDL
);
1339 option_state_dereference (&options
, MDL
);
1342 i
= DHO_DHCP_MESSAGE_TYPE
;
1343 option_code_hash_lookup(&oc
->option
, dhcp_universe
.code_hash
,
1345 save_option (&dhcp_universe
, options
, oc
);
1346 option_cache_dereference (&oc
, MDL
);
1348 /* Set DHCP_MESSAGE to whatever the message is */
1349 if (!option_cache_allocate (&oc
, MDL
)) {
1350 log_error ("No memory for DHCPNAK message type.");
1351 option_state_dereference (&options
, MDL
);
1354 if (!make_const_data (&oc
-> expression
,
1355 (unsigned char *)dhcp_message
,
1356 strlen (dhcp_message
), 1, 0, MDL
)) {
1357 log_error ("No memory for expr_const expression.");
1358 option_cache_dereference (&oc
, MDL
);
1359 option_state_dereference (&options
, MDL
);
1362 i
= DHO_DHCP_MESSAGE
;
1363 option_code_hash_lookup(&oc
->option
, dhcp_universe
.code_hash
,
1365 save_option (&dhcp_universe
, options
, oc
);
1366 option_cache_dereference (&oc
, MDL
);
1368 get_server_source_address(&from
, options
, packet
);
1370 /* If there were agent options in the incoming packet, return
1371 * them. We do not check giaddr to detect the presence of a
1372 * relay, as this excludes "l2" relay agents which have no
1375 if (packet
->options
->universe_count
> agent_universe
.index
&&
1376 packet
->options
->universes
[agent_universe
.index
]) {
1377 option_chain_head_reference
1378 ((struct option_chain_head
**)
1379 &(options
-> universes
[agent_universe
.index
]),
1380 (struct option_chain_head
*)
1381 packet
-> options
-> universes
[agent_universe
.index
],
1385 /* Do not use the client's requested parameter list. */
1386 delete_option (&dhcp_universe
, packet
-> options
,
1387 DHO_DHCP_PARAMETER_REQUEST_LIST
);
1389 /* Set up the option buffer... */
1390 outgoing
.packet_length
=
1391 cons_options (packet
, outgoing
.raw
, (struct lease
*)0,
1392 (struct client_state
*)0,
1393 0, packet
-> options
, options
, &global_scope
,
1394 0, 0, 0, (struct data_string
*)0, (char *)0);
1395 option_state_dereference (&options
, MDL
);
1397 /* memset (&raw.ciaddr, 0, sizeof raw.ciaddr);*/
1398 if (packet
->interface
->address_count
)
1399 raw
.siaddr
= packet
->interface
->addresses
[0];
1400 raw
.giaddr
= packet
-> raw
-> giaddr
;
1401 memcpy (raw
.chaddr
, packet
-> raw
-> chaddr
, sizeof raw
.chaddr
);
1402 raw
.hlen
= packet
-> raw
-> hlen
;
1403 raw
.htype
= packet
-> raw
-> htype
;
1405 raw
.xid
= packet
-> raw
-> xid
;
1406 raw
.secs
= packet
-> raw
-> secs
;
1407 raw
.flags
= packet
-> raw
-> flags
| htons (BOOTP_BROADCAST
);
1408 raw
.hops
= packet
-> raw
-> hops
;
1411 /* Report what we're sending... */
1412 log_info ("DHCPNAK on %s to %s via %s",
1414 print_hw_addr (packet
-> raw
-> htype
,
1415 packet
-> raw
-> hlen
,
1416 packet
-> raw
-> chaddr
),
1417 packet
-> raw
-> giaddr
.s_addr
1418 ? inet_ntoa (packet
-> raw
-> giaddr
)
1419 : packet
-> interface
-> name
);
1422 dump_packet (packet
);
1423 dump_raw ((unsigned char *)packet
-> raw
, packet
-> packet_length
);
1424 dump_packet (&outgoing
);
1425 dump_raw ((unsigned char *)&raw
, outgoing
.packet_length
);
1428 /* Set up the common stuff... */
1429 to
.sin_family
= AF_INET
;
1431 to
.sin_len
= sizeof to
;
1433 memset (to
.sin_zero
, 0, sizeof to
.sin_zero
);
1435 /* Make sure that the packet is at least as big as a BOOTP packet. */
1436 if (outgoing
.packet_length
< BOOTP_MIN_LEN
)
1437 outgoing
.packet_length
= BOOTP_MIN_LEN
;
1439 /* If this was gatewayed, send it back to the gateway.
1440 Otherwise, broadcast it on the local network. */
1441 if (raw
.giaddr
.s_addr
) {
1442 to
.sin_addr
= raw
.giaddr
;
1443 if (raw
.giaddr
.s_addr
!= htonl (INADDR_LOOPBACK
))
1444 to
.sin_port
= local_port
;
1446 to
.sin_port
= remote_port
; /* for testing. */
1448 if (fallback_interface
) {
1449 result
= send_packet(fallback_interface
, packet
, &raw
,
1450 outgoing
.packet_length
, from
, &to
,
1455 to
.sin_addr
= limited_broadcast
;
1456 to
.sin_port
= remote_port
;
1460 result
= send_packet(packet
->interface
, packet
, &raw
,
1461 outgoing
.packet_length
, from
, &to
, NULL
);
1464 void ack_lease (packet
, lease
, offer
, when
, msg
, ms_nulltp
, hp
)
1465 struct packet
*packet
;
1466 struct lease
*lease
;
1471 struct host_decl
*hp
;
1474 struct lease_state
*state
;
1476 struct host_decl
*host
= (struct host_decl
*)0;
1478 TIME offered_lease_time
;
1479 struct data_string d1
;
1480 TIME min_lease_time
;
1481 TIME max_lease_time
;
1482 TIME default_lease_time
;
1483 struct option_cache
*oc
;
1484 isc_result_t result
;
1487 struct in_addr from
;
1488 TIME remaining_time
;
1496 /* If we're already acking this lease, don't do it again. */
1500 /* Save original cltt for comparison later. */
1501 lease_cltt
= lease
->cltt
;
1503 /* If the lease carries a host record, remember it. */
1505 host_reference (&host
, hp
, MDL
);
1506 else if (lease
-> host
)
1507 host_reference (&host
, lease
-> host
, MDL
);
1509 /* Allocate a lease state structure... */
1510 state
= new_lease_state (MDL
);
1512 log_fatal ("unable to allocate lease state!");
1513 state
-> got_requested_address
= packet
-> got_requested_address
;
1514 shared_network_reference (&state
-> shared_network
,
1515 packet
-> interface
-> shared_network
, MDL
);
1517 /* See if we got a server identifier option. */
1518 if (lookup_option (&dhcp_universe
,
1519 packet
-> options
, DHO_DHCP_SERVER_IDENTIFIER
))
1520 state
-> got_server_identifier
= 1;
1522 maybe_return_agent_options(packet
, state
->options
);
1524 /* If we are offering a lease that is still currently valid, preserve
1525 the events. We need to do this because if the client does not
1526 REQUEST our offer, it will expire in 2 minutes, overriding the
1527 expire time in the currently in force lease. We want the expire
1528 events to be executed at that point. */
1529 if (lease
-> ends
<= cur_time
&& offer
!= DHCPOFFER
) {
1530 /* Get rid of any old expiry or release statements - by
1531 executing the statements below, we will be inserting new
1532 ones if there are any to insert. */
1533 if (lease
-> on_expiry
)
1534 executable_statement_dereference (&lease
-> on_expiry
,
1536 if (lease
-> on_commit
)
1537 executable_statement_dereference (&lease
-> on_commit
,
1539 if (lease
-> on_release
)
1540 executable_statement_dereference (&lease
-> on_release
,
1544 /* Execute statements in scope starting with the subnet scope. */
1545 execute_statements_in_scope ((struct binding_value
**)0,
1546 packet
, lease
, (struct client_state
*)0,
1548 state
-> options
, &lease
-> scope
,
1549 lease
-> subnet
-> group
,
1552 /* If the lease is from a pool, run the pool scope. */
1554 (execute_statements_in_scope
1555 ((struct binding_value
**)0, packet
, lease
,
1556 (struct client_state
*)0, packet
-> options
,
1557 state
-> options
, &lease
-> scope
, lease
-> pool
-> group
,
1558 lease
-> pool
-> shared_network
-> group
));
1560 /* Execute statements from class scopes. */
1561 for (i
= packet
-> class_count
; i
> 0; i
--) {
1562 execute_statements_in_scope
1563 ((struct binding_value
**)0,
1564 packet
, lease
, (struct client_state
*)0,
1565 packet
-> options
, state
-> options
,
1566 &lease
-> scope
, packet
-> classes
[i
- 1] -> group
,
1568 ? lease
-> pool
-> group
1569 : lease
-> subnet
-> group
));
1572 /* See if the client is only supposed to have one lease at a time,
1573 and if so, find its other leases and release them. We can only
1574 do this on DHCPREQUEST. It's a little weird to do this before
1575 looking at permissions, because the client might not actually
1576 _get_ a lease after we've done the permission check, but the
1577 assumption for this option is that the client has exactly one
1578 network interface, and will only ever remember one lease. So
1579 if it sends a DHCPREQUEST, and doesn't get the lease, it's already
1580 forgotten about its old lease, so we can too. */
1581 if (packet
-> packet_type
== DHCPREQUEST
&&
1582 (oc
= lookup_option (&server_universe
, state
-> options
,
1583 SV_ONE_LEASE_PER_CLIENT
)) &&
1584 evaluate_boolean_option_cache (&ignorep
,
1586 (struct client_state
*)0,
1588 state
-> options
, &lease
-> scope
,
1591 if (lease
-> uid_len
) {
1593 seek
= (struct lease
*)0;
1594 find_lease_by_uid (&seek
, lease
-> uid
,
1595 lease
-> uid_len
, MDL
);
1598 if (seek
== lease
&& !seek
-> n_uid
) {
1599 lease_dereference (&seek
, MDL
);
1602 next
= (struct lease
*)0;
1604 /* Don't release expired leases, and don't
1605 release the lease we're going to assign. */
1606 next
= (struct lease
*)0;
1609 lease_reference (&next
, seek
-> n_uid
, MDL
);
1610 if (seek
!= lease
&&
1611 seek
-> binding_state
!= FTS_RELEASED
&&
1612 seek
-> binding_state
!= FTS_EXPIRED
&&
1613 seek
-> binding_state
!= FTS_RESET
&&
1614 seek
-> binding_state
!= FTS_FREE
&&
1615 seek
-> binding_state
!= FTS_BACKUP
)
1617 lease_dereference (&seek
, MDL
);
1619 lease_reference (&seek
, next
, MDL
);
1620 lease_dereference (&next
, MDL
);
1624 lease_dereference (&next
, MDL
);
1626 release_lease (seek
, packet
);
1627 lease_dereference (&seek
, MDL
);
1632 if (!lease
-> uid_len
||
1634 !host
-> client_identifier
.len
&&
1635 (oc
= lookup_option (&server_universe
, state
-> options
,
1637 !evaluate_boolean_option_cache (&ignorep
, packet
, lease
,
1638 (struct client_state
*)0,
1644 seek
= (struct lease
*)0;
1645 find_lease_by_hw_addr
1646 (&seek
, lease
-> hardware_addr
.hbuf
,
1647 lease
-> hardware_addr
.hlen
, MDL
);
1650 if (seek
== lease
&& !seek
-> n_hw
) {
1651 lease_dereference (&seek
, MDL
);
1654 next
= (struct lease
*)0;
1657 lease_reference (&next
, seek
-> n_hw
, MDL
);
1658 if (seek
!= lease
&&
1659 seek
-> binding_state
!= FTS_RELEASED
&&
1660 seek
-> binding_state
!= FTS_EXPIRED
&&
1661 seek
-> binding_state
!= FTS_RESET
&&
1662 seek
-> binding_state
!= FTS_FREE
&&
1663 seek
-> binding_state
!= FTS_BACKUP
)
1665 lease_dereference (&seek
, MDL
);
1667 lease_reference (&seek
, next
, MDL
);
1668 lease_dereference (&next
, MDL
);
1672 lease_dereference (&next
, MDL
);
1674 release_lease (seek
, packet
);
1675 lease_dereference (&seek
, MDL
);
1683 /* Make sure this packet satisfies the configured minimum
1684 number of seconds. */
1685 memset (&d1
, 0, sizeof d1
);
1686 if (offer
== DHCPOFFER
&&
1687 (oc
= lookup_option (&server_universe
, state
-> options
,
1689 if (evaluate_option_cache (&d1
, packet
, lease
,
1690 (struct client_state
*)0,
1691 packet
-> options
, state
-> options
,
1692 &lease
-> scope
, oc
, MDL
)) {
1694 ntohs (packet
-> raw
-> secs
) < d1
.data
[0]) {
1695 log_info("%s: configured min-secs value (%d) "
1696 "is greater than secs field (%d). "
1697 "message dropped.", msg
, d1
.data
[0],
1698 ntohs(packet
->raw
->secs
));
1699 data_string_forget (&d1
, MDL
);
1700 free_lease_state (state
, MDL
);
1702 host_dereference (&host
, MDL
);
1705 data_string_forget (&d1
, MDL
);
1709 /* Try to find a matching host declaration for this lease.
1712 struct host_decl
*hp
= (struct host_decl
*)0;
1713 struct host_decl
*h
;
1715 /* Try to find a host_decl that matches the client
1716 identifier or hardware address on the packet, and
1717 has no fixed IP address. If there is one, hang
1718 it off the lease so that its option definitions
1720 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
1721 DHO_DHCP_CLIENT_IDENTIFIER
);
1723 evaluate_option_cache (&d1
, packet
, lease
,
1724 (struct client_state
*)0,
1725 packet
-> options
, state
-> options
,
1726 &lease
-> scope
, oc
, MDL
)) {
1727 find_hosts_by_uid (&hp
, d1
.data
, d1
.len
, MDL
);
1728 data_string_forget (&d1
, MDL
);
1729 for (h
= hp
; h
; h
= h
-> n_ipaddr
) {
1730 if (!h
-> fixed_addr
)
1734 host_reference (&host
, h
, MDL
);
1736 host_dereference(&hp
, MDL
);
1739 find_hosts_by_haddr (&hp
,
1740 packet
-> raw
-> htype
,
1741 packet
-> raw
-> chaddr
,
1742 packet
-> raw
-> hlen
,
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_option(&hp
, packet
, packet
->options
, MDL
);
1755 for (h
= hp
; h
; h
= h
-> n_ipaddr
) {
1756 if (!h
-> fixed_addr
)
1760 host_reference (&host
, h
, MDL
);
1762 host_dereference(&hp
, MDL
);
1766 /* If we have a host_decl structure, run the options associated
1767 with its group. Whether the host decl struct is old or not. */
1769 execute_statements_in_scope ((struct binding_value
**)0,
1771 (struct client_state
*)0,
1773 state
-> options
, &lease
-> scope
,
1776 ? lease
-> pool
-> group
1777 : lease
-> subnet
-> group
));
1779 /* Drop the request if it's not allowed for this client. By
1780 default, unknown clients are allowed. */
1782 (oc
= lookup_option (&server_universe
, state
-> options
,
1783 SV_BOOT_UNKNOWN_CLIENTS
)) &&
1784 !evaluate_boolean_option_cache (&ignorep
,
1786 (struct client_state
*)0,
1789 &lease
-> scope
, oc
, MDL
)) {
1791 log_info ("%s: unknown client", msg
);
1792 free_lease_state (state
, MDL
);
1794 host_dereference (&host
, MDL
);
1798 /* Drop the request if it's not allowed for this client. */
1800 (oc
= lookup_option (&server_universe
, state
-> options
,
1802 !evaluate_boolean_option_cache (&ignorep
,
1804 (struct client_state
*)0,
1807 &lease
-> scope
, oc
, MDL
)) {
1809 log_info ("%s: bootp disallowed", msg
);
1810 free_lease_state (state
, MDL
);
1812 host_dereference (&host
, MDL
);
1816 /* Drop the request if booting is specifically denied. */
1817 oc
= lookup_option (&server_universe
, state
-> options
,
1820 !evaluate_boolean_option_cache (&ignorep
,
1822 (struct client_state
*)0,
1825 &lease
-> scope
, oc
, MDL
)) {
1827 log_info ("%s: booting disallowed", msg
);
1828 free_lease_state (state
, MDL
);
1830 host_dereference (&host
, MDL
);
1834 /* If we are configured to do per-class billing, do it. */
1835 if (have_billing_classes
&& !(lease
-> flags
& STATIC_LEASE
)) {
1836 /* See if the lease is currently being billed to a
1837 class, and if so, whether or not it can continue to
1838 be billed to that class. */
1839 if (lease
-> billing_class
) {
1840 for (i
= 0; i
< packet
-> class_count
; i
++)
1841 if (packet
-> classes
[i
] ==
1842 lease
-> billing_class
)
1844 if (i
== packet
-> class_count
)
1845 unbill_class (lease
, lease
-> billing_class
);
1848 /* If we don't have an active billing, see if we need
1849 one, and if we do, try to do so. */
1850 if (lease
->billing_class
== NULL
) {
1852 for (i
= 0; i
< packet
->class_count
; i
++) {
1853 if (packet
->classes
[i
]->lease_limit
) {
1855 if (bill_class(lease
,
1856 packet
->classes
[i
]))
1860 if (bill
!= 0 && i
== packet
->class_count
) {
1861 log_info("%s: no available billing: lease "
1862 "limit reached in all matching "
1864 free_lease_state(state
, MDL
);
1866 host_dereference(&host
, MDL
);
1870 /* If this is an offer, undo the billing. We go
1871 * through all the steps above to bill a class so
1872 * we can hit the 'no available billing' mark and
1873 * abort without offering. But it just doesn't make
1874 * sense to permanently bill a class for a non-active
1875 * lease. This means on REQUEST, we will bill this
1876 * lease again (if there is a REQUEST).
1878 if (offer
== DHCPOFFER
&&
1879 lease
->billing_class
!= NULL
&&
1880 lease
->binding_state
!= FTS_ACTIVE
)
1881 unbill_class(lease
, lease
->billing_class
);
1885 /* Figure out the filename. */
1886 oc
= lookup_option (&server_universe
, state
-> options
, SV_FILENAME
);
1888 evaluate_option_cache (&state
-> filename
, packet
, lease
,
1889 (struct client_state
*)0,
1890 packet
-> options
, state
-> options
,
1891 &lease
-> scope
, oc
, MDL
);
1893 /* Choose a server name as above. */
1894 oc
= lookup_option (&server_universe
, state
-> options
,
1897 evaluate_option_cache (&state
-> server_name
, packet
, lease
,
1898 (struct client_state
*)0,
1899 packet
-> options
, state
-> options
,
1900 &lease
-> scope
, oc
, MDL
);
1902 /* At this point, we have a lease that we can offer the client.
1903 Now we construct a lease structure that contains what we want,
1904 and call supersede_lease to do the right thing with it. */
1905 lt
= (struct lease
*)0;
1906 result
= lease_allocate (<
, MDL
);
1907 if (result
!= ISC_R_SUCCESS
) {
1908 log_info ("%s: can't allocate temporary lease structure: %s",
1909 msg
, isc_result_totext (result
));
1910 free_lease_state (state
, MDL
);
1912 host_dereference (&host
, MDL
);
1916 /* Use the ip address of the lease that we finally found in
1918 lt
-> ip_addr
= lease
-> ip_addr
;
1921 lt
-> starts
= cur_time
;
1923 /* Figure out how long a lease to assign. If this is a
1924 dynamic BOOTP lease, its duration must be infinite. */
1926 lt
->flags
&= ~BOOTP_LEASE
;
1928 default_lease_time
= DEFAULT_DEFAULT_LEASE_TIME
;
1929 if ((oc
= lookup_option (&server_universe
, state
-> options
,
1930 SV_DEFAULT_LEASE_TIME
))) {
1931 if (evaluate_option_cache (&d1
, packet
, lease
,
1932 (struct client_state
*)0,
1935 &lease
-> scope
, oc
, MDL
)) {
1936 if (d1
.len
== sizeof (u_int32_t
))
1937 default_lease_time
=
1939 data_string_forget (&d1
, MDL
);
1943 if ((oc
= lookup_option (&dhcp_universe
, packet
-> options
,
1944 DHO_DHCP_LEASE_TIME
)))
1945 s1
= evaluate_option_cache (&d1
, packet
, lease
,
1946 (struct client_state
*)0,
1949 &lease
-> scope
, oc
, MDL
);
1953 if (s1
&& (d1
.len
== 4)) {
1954 u_int32_t ones
= 0xffffffff;
1956 /* One potential use of reserved leases is to allow
1957 * clients to signal reservation of their lease. They
1958 * can kinda sorta do this, if you squint hard enough,
1959 * by supplying an 'infinite' requested-lease-time
1960 * option. This is generally bad practice...you want
1961 * clients to return to the server on at least some
1962 * period (days, months, years) to get up-to-date
1965 * 1) A client requests 0xffffffff lease-time.
1966 * 2) The server reserves the lease, and assigns a
1967 * <= max_lease_time lease-time to the client, which
1968 * we presume is much smaller than 0xffffffff.
1969 * 3) The client ultimately fails to renew its lease
1970 * (all clients go offline at some point).
1971 * 4) The server retains the reservation, although
1972 * the lease expires and passes through those states
1973 * as normal, it's placed in the 'reserved' queue,
1974 * and is under no circumstances allocated to any
1977 * Whether the client knows its reserving its lease or
1978 * not, this can be a handy tool for a sysadmin.
1980 if ((memcmp(d1
.data
, &ones
, 4) == 0) &&
1981 (oc
= lookup_option(&server_universe
,
1983 SV_RESERVE_INFINITE
)) &&
1984 evaluate_boolean_option_cache(&ignorep
, packet
,
1985 lease
, NULL
, packet
->options
,
1986 state
->options
, &lease
->scope
,
1988 lt
->flags
|= RESERVED_LEASE
;
1990 log_info("Infinite-leasetime "
1991 "reservation made on %s.",
1992 piaddr(lt
->ip_addr
));
1995 lease_time
= getULong (d1
.data
);
1997 lease_time
= default_lease_time
;
2000 data_string_forget(&d1
, MDL
);
2002 /* See if there's a maximum lease time. */
2003 max_lease_time
= DEFAULT_MAX_LEASE_TIME
;
2004 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2005 SV_MAX_LEASE_TIME
))) {
2006 if (evaluate_option_cache (&d1
, packet
, lease
,
2007 (struct client_state
*)0,
2010 &lease
-> scope
, oc
, MDL
)) {
2011 if (d1
.len
== sizeof (u_int32_t
))
2014 data_string_forget (&d1
, MDL
);
2018 /* Enforce the maximum lease length. */
2019 if (lease_time
< 0 /* XXX */
2020 || lease_time
> max_lease_time
)
2021 lease_time
= max_lease_time
;
2023 min_lease_time
= DEFAULT_MIN_LEASE_TIME
;
2024 if (min_lease_time
> max_lease_time
)
2025 min_lease_time
= max_lease_time
;
2027 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2028 SV_MIN_LEASE_TIME
))) {
2029 if (evaluate_option_cache (&d1
, packet
, lease
,
2030 (struct client_state
*)0,
2033 &lease
-> scope
, oc
, MDL
)) {
2034 if (d1
.len
== sizeof (u_int32_t
))
2035 min_lease_time
= getULong (d1
.data
);
2036 data_string_forget (&d1
, MDL
);
2040 /* CC: If there are less than
2041 adaptive-lease-time-threshold % free leases,
2042 hand out only short term leases */
2044 memset(&d1
, 0, sizeof(d1
));
2046 (oc
= lookup_option(&server_universe
, state
->options
,
2047 SV_ADAPTIVE_LEASE_TIME_THRESHOLD
)) &&
2048 evaluate_option_cache(&d1
, packet
, lease
, NULL
,
2049 packet
->options
, state
->options
,
2050 &lease
->scope
, oc
, MDL
)) {
2051 if (d1
.len
== 1 && d1
.data
[0] > 0 &&
2054 int poolfilled
, total
, count
;
2057 adaptive_time
= min_lease_time
;
2059 adaptive_time
= DEFAULT_MIN_LEASE_TIME
;
2061 /* Allow the client to keep its lease. */
2062 if (lease
->ends
- cur_time
> adaptive_time
)
2063 adaptive_time
= lease
->ends
- cur_time
;
2065 count
= lease
->pool
->lease_count
;
2066 total
= count
- (lease
->pool
->free_leases
+
2067 lease
->pool
->backup_leases
);
2069 poolfilled
= (total
> (INT_MAX
/ 100)) ?
2070 total
/ (count
/ 100) :
2071 (total
* 100) / count
;
2073 log_debug("Adap-lease: Total: %d, Free: %d, "
2074 "Ends: %d, Adaptive: %d, Fill: %d, "
2076 lease
->pool
->lease_count
,
2077 lease
->pool
->free_leases
,
2078 (int)(lease
->ends
- cur_time
),
2079 (int)adaptive_time
, poolfilled
,
2082 if (poolfilled
>= d1
.data
[0] &&
2083 lease_time
> adaptive_time
) {
2084 log_info("Pool over threshold, time "
2085 "for %s reduced from %d to "
2086 "%d.", piaddr(lease
->ip_addr
),
2088 (int)adaptive_time
);
2090 lease_time
= adaptive_time
;
2093 data_string_forget(&d1
, MDL
);
2096 /* a client requests an address which is not yet active*/
2097 if (lease
->pool
&& lease
->pool
->valid_from
&&
2098 cur_time
< lease
->pool
->valid_from
) {
2099 /* NAK leases before pool activation date */
2101 memcpy (cip
.iabuf
, <
->ip_addr
.iabuf
, 4);
2102 nak_lease(packet
, &cip
);
2103 free_lease_state (state
, MDL
);
2104 lease_dereference (<
, MDL
);
2106 host_dereference (&host
, MDL
);
2112 a) NAK current lease if past the expiration date
2113 b) extend lease only up to the expiration date, but not
2114 below min-lease-time
2115 Setting min-lease-time is essential for this to work!
2116 The value of min-lease-time determines the lenght
2117 of the transition window:
2118 A client renewing a second before the deadline will
2119 get a min-lease-time lease. Since the current ip might not
2120 be routable after the deadline, the client will
2121 be offline until it DISCOVERS again. Otherwise it will
2122 receive a NAK at T/2.
2123 A min-lease-time of 6 seconds effectively switches over
2124 all clients in this pool very quickly.
2127 if (lease
->pool
&& lease
->pool
->valid_until
) {
2128 if (cur_time
>= lease
->pool
->valid_until
) {
2129 /* NAK leases after pool expiration date */
2131 memcpy (cip
.iabuf
, <
->ip_addr
.iabuf
, 4);
2132 nak_lease(packet
, &cip
);
2133 free_lease_state (state
, MDL
);
2134 lease_dereference (<
, MDL
);
2136 host_dereference (&host
, MDL
);
2139 remaining_time
= lease
->pool
->valid_until
- cur_time
;
2140 if (lease_time
> remaining_time
)
2141 lease_time
= remaining_time
;
2144 if (lease_time
< min_lease_time
) {
2146 lease_time
= min_lease_time
;
2148 lease_time
= default_lease_time
;
2152 #if defined (FAILOVER_PROTOCOL)
2153 /* Okay, we know the lease duration. Now check the
2154 failover state, if any. */
2155 if (lease
-> pool
&& lease
-> pool
-> failover_peer
) {
2156 TIME new_lease_time
= lease_time
;
2157 dhcp_failover_state_t
*peer
=
2158 lease
-> pool
-> failover_peer
;
2160 /* Copy previous lease failover ack-state. */
2161 lt
->tsfp
= lease
->tsfp
;
2162 lt
->atsfp
= lease
->atsfp
;
2164 /* cltt set below */
2166 /* Lease times less than MCLT are not a concern. */
2167 if (lease_time
> peer
->mclt
) {
2168 /* Each server can only offer a lease time
2169 * that is either equal to MCLT (at least),
2170 * or up to TSFP+MCLT. Only if the desired
2171 * lease time falls within TSFP+MCLT, can
2172 * the server allow it.
2174 if (lt
->tsfp
<= cur_time
)
2175 new_lease_time
= peer
->mclt
;
2176 else if ((cur_time
+ lease_time
) >
2177 (lt
->tsfp
+ peer
->mclt
))
2178 new_lease_time
= (lt
->tsfp
- cur_time
)
2182 /* Update potential expiry. Allow for the desired
2183 * lease time plus one half the actual (whether
2184 * modified downward or not) lease time, which is
2185 * actually an estimate of when the client will
2186 * renew. This way, the client will be able to get
2187 * the desired lease time upon renewal.
2189 if (offer
== DHCPACK
) {
2190 lt
->tstp
= cur_time
+ lease_time
+
2191 (new_lease_time
/ 2);
2193 /* If we reduced the potential expiry time,
2194 * make sure we don't offer an old-expiry-time
2195 * lease for this lease before the change is
2198 if (lt
->tstp
< lt
->tsfp
)
2199 lt
->tsfp
= lt
->tstp
;
2201 lt
->tstp
= lease
->tstp
;
2203 /* Use failover-modified lease time. */
2204 lease_time
= new_lease_time
;
2206 #endif /* FAILOVER_PROTOCOL */
2208 /* If the lease duration causes the time value to wrap,
2209 use the maximum expiry time. */
2210 if (cur_time
+ lease_time
< cur_time
)
2211 state
-> offered_expiry
= MAX_TIME
- 1;
2213 state
-> offered_expiry
= cur_time
+ lease_time
;
2217 lt
-> ends
= state
-> offered_expiry
;
2219 /* Don't make lease active until we actually get a
2221 if (offer
== DHCPACK
)
2222 lt
-> next_binding_state
= FTS_ACTIVE
;
2224 lt
-> next_binding_state
= lease
-> binding_state
;
2226 lt
->flags
|= BOOTP_LEASE
;
2228 lease_time
= MAX_TIME
- cur_time
;
2230 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2231 SV_BOOTP_LEASE_LENGTH
))) {
2232 if (evaluate_option_cache (&d1
, packet
, lease
,
2233 (struct client_state
*)0,
2236 &lease
-> scope
, oc
, MDL
)) {
2237 if (d1
.len
== sizeof (u_int32_t
))
2238 lease_time
= getULong (d1
.data
);
2239 data_string_forget (&d1
, MDL
);
2243 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2244 SV_BOOTP_LEASE_CUTOFF
))) {
2245 if (evaluate_option_cache (&d1
, packet
, lease
,
2246 (struct client_state
*)0,
2249 &lease
-> scope
, oc
, MDL
)) {
2250 if (d1
.len
== sizeof (u_int32_t
))
2251 lease_time
= (getULong (d1
.data
) -
2253 data_string_forget (&d1
, MDL
);
2257 lt
-> ends
= state
-> offered_expiry
= cur_time
+ lease_time
;
2258 lt
-> next_binding_state
= FTS_ACTIVE
;
2261 /* Update Client Last Transaction Time. */
2262 lt
->cltt
= cur_time
;
2264 /* Record the uid, if given... */
2265 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
2266 DHO_DHCP_CLIENT_IDENTIFIER
);
2268 evaluate_option_cache (&d1
, packet
, lease
,
2269 (struct client_state
*)0,
2270 packet
-> options
, state
-> options
,
2271 &lease
-> scope
, oc
, MDL
)) {
2272 if (d1
.len
<= sizeof lt
-> uid_buf
) {
2273 memcpy (lt
-> uid_buf
, d1
.data
, d1
.len
);
2274 lt
-> uid
= lt
-> uid_buf
;
2275 lt
-> uid_max
= sizeof lt
-> uid_buf
;
2276 lt
-> uid_len
= d1
.len
;
2278 unsigned char *tuid
;
2279 lt
-> uid_max
= d1
.len
;
2280 lt
-> uid_len
= d1
.len
;
2281 tuid
= (unsigned char *)dmalloc (lt
-> uid_max
, MDL
);
2284 log_fatal ("no memory for large uid.");
2285 memcpy (tuid
, d1
.data
, lt
-> uid_len
);
2288 data_string_forget (&d1
, MDL
);
2292 host_reference (<
-> host
, host
, MDL
);
2293 host_dereference (&host
, MDL
);
2295 if (lease
-> subnet
)
2296 subnet_reference (<
-> subnet
, lease
-> subnet
, MDL
);
2297 if (lease
-> billing_class
)
2298 class_reference (<
-> billing_class
,
2299 lease
-> billing_class
, MDL
);
2301 /* Set a flag if this client is a broken client that NUL
2302 terminates string options and expects us to do likewise. */
2304 lease
-> flags
|= MS_NULL_TERMINATION
;
2306 lease
-> flags
&= ~MS_NULL_TERMINATION
;
2308 /* Save any bindings. */
2309 if (lease
-> scope
) {
2310 binding_scope_reference (<
-> scope
, lease
-> scope
, MDL
);
2311 binding_scope_dereference (&lease
-> scope
, MDL
);
2313 if (lease
-> agent_options
)
2314 option_chain_head_reference (<
-> agent_options
,
2315 lease
-> agent_options
, MDL
);
2317 /* If we got relay agent information options from the packet, then
2318 * cache them for renewal in case the relay agent can't supply them
2319 * when the client unicasts. The options may be from an addressed
2320 * "l3" relay, or from an unaddressed "l2" relay which does not set
2323 if (!packet
->agent_options_stashed
&&
2324 packet
->options
->universe_count
> agent_universe
.index
&&
2325 packet
->options
->universes
[agent_universe
.index
] != NULL
) {
2326 oc
= lookup_option (&server_universe
, state
-> options
,
2327 SV_STASH_AGENT_OPTIONS
);
2329 evaluate_boolean_option_cache (&ignorep
, packet
, lease
,
2330 (struct client_state
*)0,
2333 &lease
-> scope
, oc
, MDL
)) {
2334 if (lt
-> agent_options
)
2335 option_chain_head_dereference (<
-> agent_options
, MDL
);
2336 option_chain_head_reference
2337 (<
-> agent_options
,
2338 (struct option_chain_head
*)
2339 packet
-> options
-> universes
[agent_universe
.index
],
2344 /* Replace the old lease hostname with the new one, if it's changed. */
2345 oc
= lookup_option (&dhcp_universe
, packet
-> options
, DHO_HOST_NAME
);
2347 s1
= evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
2348 (struct client_state
*)0,
2350 (struct option_state
*)0,
2351 &global_scope
, oc
, MDL
);
2356 lease
-> client_hostname
&&
2357 strlen (lease
-> client_hostname
) == d1
.len
&&
2358 !memcmp (lease
-> client_hostname
, d1
.data
, d1
.len
)) {
2359 /* Hasn't changed. */
2360 data_string_forget (&d1
, MDL
);
2361 lt
-> client_hostname
= lease
-> client_hostname
;
2362 lease
-> client_hostname
= (char *)0;
2363 } else if (oc
&& s1
) {
2364 lt
-> client_hostname
= dmalloc (d1
.len
+ 1, MDL
);
2365 if (!lt
-> client_hostname
)
2366 log_error ("no memory for client hostname.");
2368 memcpy (lt
-> client_hostname
, d1
.data
, d1
.len
);
2369 lt
-> client_hostname
[d1
.len
] = 0;
2371 data_string_forget (&d1
, MDL
);
2374 /* Record the hardware address, if given... */
2375 lt
-> hardware_addr
.hlen
= packet
-> raw
-> hlen
+ 1;
2376 lt
-> hardware_addr
.hbuf
[0] = packet
-> raw
-> htype
;
2377 memcpy (<
-> hardware_addr
.hbuf
[1], packet
-> raw
-> chaddr
,
2378 sizeof packet
-> raw
-> chaddr
);
2380 lt
-> flags
= lease
-> flags
& ~PERSISTENT_FLAGS
;
2382 /* If there are statements to execute when the lease is
2383 committed, execute them. */
2384 if (lease
-> on_commit
&& (!offer
|| offer
== DHCPACK
)) {
2385 execute_statements ((struct binding_value
**)0,
2386 packet
, lt
, (struct client_state
*)0,
2388 state
-> options
, <
-> scope
,
2389 lease
-> on_commit
);
2390 if (lease
-> on_commit
)
2391 executable_statement_dereference (&lease
-> on_commit
,
2396 /* Perform DDNS updates, if configured to. */
2397 if ((!offer
|| offer
== DHCPACK
) &&
2398 (!(oc
= lookup_option (&server_universe
, state
-> options
,
2399 SV_DDNS_UPDATES
)) ||
2400 evaluate_boolean_option_cache (&ignorep
, packet
, lt
,
2401 (struct client_state
*)0,
2404 <
-> scope
, oc
, MDL
))) {
2405 ddns_updates(packet
, lt
, lease
, NULL
, NULL
, state
->options
);
2407 #endif /* NSUPDATE */
2409 /* Don't call supersede_lease on a mocked-up lease. */
2410 if (lease
-> flags
& STATIC_LEASE
) {
2411 /* Copy the hardware address into the static lease
2413 lease
-> hardware_addr
.hlen
= packet
-> raw
-> hlen
+ 1;
2414 lease
-> hardware_addr
.hbuf
[0] = packet
-> raw
-> htype
;
2415 memcpy (&lease
-> hardware_addr
.hbuf
[1],
2416 packet
-> raw
-> chaddr
,
2417 sizeof packet
-> raw
-> chaddr
); /* XXX */
2419 #if !defined(DELAYED_ACK)
2420 /* Install the new information on 'lt' onto the lease at
2421 * 'lease'. If this is a DHCPOFFER, it is a 'soft' promise,
2422 * if it is a DHCPACK, it is a 'hard' binding, so it needs
2423 * to be recorded and propogated immediately. If the update
2424 * fails, don't ACK it (or BOOTREPLY) either; we may give
2425 * the same lease to another client later, and that would be
2428 if (!supersede_lease(lease
, lt
, !offer
|| (offer
== DHCPACK
),
2429 offer
== DHCPACK
, offer
== DHCPACK
)) {
2430 #else /* defined(DELAYED_ACK) */
2431 /* Install the new information on 'lt' onto the lease at
2432 * 'lease'. Â We will not 'commit' this information to disk
2433 * yet (fsync()), we will 'propogate' the information if
2434 * this is BOOTP or a DHCPACK, but we will not 'pimmediate'ly
2435 * transmit failover binding updates (this is delayed until
2436 * after the fsync()). If the update fails, don't ACK it (or
2437 * BOOTREPLY either); we may give the same lease out to a
2438 * different client, and that would be a conflict.
2440 if (!supersede_lease(lease
, lt
, 0, !offer
|| offer
== DHCPACK
,
2443 log_info ("%s: database update failed", msg
);
2444 free_lease_state (state
, MDL
);
2445 lease_dereference (<
, MDL
);
2449 lease_dereference (<
, MDL
);
2451 /* Remember the interface on which the packet arrived. */
2452 state
-> ip
= packet
-> interface
;
2454 /* Remember the giaddr, xid, secs, flags and hops. */
2455 state
-> giaddr
= packet
-> raw
-> giaddr
;
2456 state
-> ciaddr
= packet
-> raw
-> ciaddr
;
2457 state
-> xid
= packet
-> raw
-> xid
;
2458 state
-> secs
= packet
-> raw
-> secs
;
2459 state
-> bootp_flags
= packet
-> raw
-> flags
;
2460 state
-> hops
= packet
-> raw
-> hops
;
2461 state
-> offer
= offer
;
2463 /* If we're always supposed to broadcast to this client, set
2464 the broadcast bit in the bootp flags field. */
2465 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2466 SV_ALWAYS_BROADCAST
)) &&
2467 evaluate_boolean_option_cache (&ignorep
, packet
, lease
,
2468 (struct client_state
*)0,
2469 packet
-> options
, state
-> options
,
2470 &lease
-> scope
, oc
, MDL
))
2471 state
-> bootp_flags
|= htons (BOOTP_BROADCAST
);
2473 /* Get the Maximum Message Size option from the packet, if one
2475 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
2476 DHO_DHCP_MAX_MESSAGE_SIZE
);
2478 evaluate_option_cache (&d1
, packet
, lease
,
2479 (struct client_state
*)0,
2480 packet
-> options
, state
-> options
,
2481 &lease
-> scope
, oc
, MDL
)) {
2482 if (d1
.len
== sizeof (u_int16_t
))
2483 state
-> max_message_size
= getUShort (d1
.data
);
2484 data_string_forget (&d1
, MDL
);
2486 oc
= lookup_option (&dhcp_universe
, state
-> options
,
2487 DHO_DHCP_MAX_MESSAGE_SIZE
);
2489 evaluate_option_cache (&d1
, packet
, lease
,
2490 (struct client_state
*)0,
2491 packet
-> options
, state
-> options
,
2492 &lease
-> scope
, oc
, MDL
)) {
2493 if (d1
.len
== sizeof (u_int16_t
))
2494 state
-> max_message_size
=
2495 getUShort (d1
.data
);
2496 data_string_forget (&d1
, MDL
);
2500 /* Get the Subnet Selection option from the packet, if one
2502 if ((oc
= lookup_option (&dhcp_universe
, packet
-> options
,
2503 DHO_SUBNET_SELECTION
))) {
2505 /* Make a copy of the data. */
2506 struct option_cache
*noc
= (struct option_cache
*)0;
2507 if (option_cache_allocate (&noc
, MDL
)) {
2509 data_string_copy (&noc
-> data
,
2511 if (oc
-> expression
)
2512 expression_reference (&noc
-> expression
,
2513 oc
-> expression
, MDL
);
2515 option_reference(&(noc
->option
), oc
->option
,
2519 save_option (&dhcp_universe
, state
-> options
, noc
);
2520 option_cache_dereference (&noc
, MDL
);
2523 /* Now, if appropriate, put in DHCP-specific options that
2525 if (state
-> offer
) {
2526 i
= DHO_DHCP_MESSAGE_TYPE
;
2527 oc
= (struct option_cache
*)0;
2528 if (option_cache_allocate (&oc
, MDL
)) {
2529 if (make_const_data (&oc
-> expression
,
2530 &state
-> offer
, 1, 0, 0, MDL
)) {
2531 option_code_hash_lookup(&oc
->option
,
2532 dhcp_universe
.code_hash
,
2534 save_option (&dhcp_universe
,
2535 state
-> options
, oc
);
2537 option_cache_dereference (&oc
, MDL
);
2540 get_server_source_address(&from
, state
->options
, packet
);
2541 memcpy(state
->from
.iabuf
, &from
, sizeof(from
));
2542 state
->from
.len
= sizeof(from
);
2544 offered_lease_time
=
2545 state
-> offered_expiry
- cur_time
;
2547 putULong(state
->expiry
, (u_int32_t
)offered_lease_time
);
2548 i
= DHO_DHCP_LEASE_TIME
;
2549 oc
= (struct option_cache
*)0;
2550 if (option_cache_allocate (&oc
, MDL
)) {
2551 if (make_const_data(&oc
->expression
, state
->expiry
,
2553 option_code_hash_lookup(&oc
->option
,
2554 dhcp_universe
.code_hash
,
2556 save_option (&dhcp_universe
,
2557 state
-> options
, oc
);
2559 option_cache_dereference (&oc
, MDL
);
2563 * Validate any configured renew or rebinding times against
2564 * the determined lease time. Do rebinding first so that
2565 * the renew time can be validated against the rebind time.
2567 if ((oc
= lookup_option(&dhcp_universe
, state
->options
,
2568 DHO_DHCP_REBINDING_TIME
)) != NULL
&&
2569 evaluate_option_cache(&d1
, packet
, lease
, NULL
,
2570 packet
->options
, state
->options
,
2571 &lease
->scope
, oc
, MDL
)) {
2572 TIME rebind_time
= getULong(d1
.data
);
2574 /* Drop the configured (invalid) rebinding time. */
2575 if (rebind_time
>= offered_lease_time
)
2576 delete_option(&dhcp_universe
, state
->options
,
2577 DHO_DHCP_REBINDING_TIME
);
2578 else /* XXX: variable is reused. */
2579 offered_lease_time
= rebind_time
;
2581 data_string_forget(&d1
, MDL
);
2584 if ((oc
= lookup_option(&dhcp_universe
, state
->options
,
2585 DHO_DHCP_RENEWAL_TIME
)) != NULL
&&
2586 evaluate_option_cache(&d1
, packet
, lease
, NULL
,
2587 packet
->options
, state
->options
,
2588 &lease
->scope
, oc
, MDL
)) {
2589 if (getULong(d1
.data
) >= offered_lease_time
)
2590 delete_option(&dhcp_universe
, state
->options
,
2591 DHO_DHCP_RENEWAL_TIME
);
2593 data_string_forget(&d1
, MDL
);
2596 /* XXXSK: should we use get_server_source_address() here? */
2597 if (state
-> ip
-> address_count
) {
2599 sizeof state
-> ip
-> addresses
[0];
2600 memcpy (state
-> from
.iabuf
,
2601 &state
-> ip
-> addresses
[0],
2606 /* Figure out the address of the boot file server. */
2607 memset (&state
-> siaddr
, 0, sizeof state
-> siaddr
);
2609 lookup_option (&server_universe
,
2610 state
-> options
, SV_NEXT_SERVER
))) {
2611 if (evaluate_option_cache (&d1
, packet
, lease
,
2612 (struct client_state
*)0,
2613 packet
-> options
, state
-> options
,
2614 &lease
-> scope
, oc
, MDL
)) {
2615 /* If there was more than one answer,
2617 if (d1
.len
>= 4 && d1
.data
)
2618 memcpy (&state
-> siaddr
, d1
.data
, 4);
2619 data_string_forget (&d1
, MDL
);
2623 /* Use the subnet mask from the subnet declaration if no other
2624 mask has been provided. */
2625 i
= DHO_SUBNET_MASK
;
2626 if (!lookup_option (&dhcp_universe
, state
-> options
, i
)) {
2627 oc
= (struct option_cache
*)0;
2628 if (option_cache_allocate (&oc
, MDL
)) {
2629 if (make_const_data (&oc
-> expression
,
2630 lease
-> subnet
-> netmask
.iabuf
,
2631 lease
-> subnet
-> netmask
.len
,
2633 option_code_hash_lookup(&oc
->option
,
2634 dhcp_universe
.code_hash
,
2636 save_option (&dhcp_universe
,
2637 state
-> options
, oc
);
2639 option_cache_dereference (&oc
, MDL
);
2643 /* Use the hostname from the host declaration if there is one
2644 and no hostname has otherwise been provided, and if the
2645 use-host-decl-name flag is set. */
2647 j
= SV_USE_HOST_DECL_NAMES
;
2648 if (!lookup_option (&dhcp_universe
, state
-> options
, i
) &&
2649 lease
-> host
&& lease
-> host
-> name
&&
2650 (evaluate_boolean_option_cache
2651 (&ignorep
, packet
, lease
, (struct client_state
*)0,
2652 packet
-> options
, state
-> options
, &lease
-> scope
,
2653 lookup_option (&server_universe
, state
-> options
, j
), MDL
))) {
2654 oc
= (struct option_cache
*)0;
2655 if (option_cache_allocate (&oc
, MDL
)) {
2656 if (make_const_data (&oc
-> expression
,
2658 lease
-> host
-> name
),
2659 strlen (lease
-> host
-> name
),
2661 option_code_hash_lookup(&oc
->option
,
2662 dhcp_universe
.code_hash
,
2664 save_option (&dhcp_universe
,
2665 state
-> options
, oc
);
2667 option_cache_dereference (&oc
, MDL
);
2671 /* If we don't have a hostname yet, and we've been asked to do
2672 a reverse lookup to find the hostname, do it. */
2673 j
= SV_GET_LEASE_HOSTNAMES
;
2674 if (!lookup_option (&server_universe
, state
-> options
, i
) &&
2675 (evaluate_boolean_option_cache
2676 (&ignorep
, packet
, lease
, (struct client_state
*)0,
2677 packet
-> options
, state
-> options
, &lease
-> scope
,
2678 lookup_option (&server_universe
, state
-> options
, j
), MDL
))) {
2682 memcpy (&ia
, lease
-> ip_addr
.iabuf
, 4);
2684 h
= gethostbyaddr ((char *)&ia
, sizeof ia
, AF_INET
);
2686 log_error ("No hostname for %s", inet_ntoa (ia
));
2688 oc
= (struct option_cache
*)0;
2689 if (option_cache_allocate (&oc
, MDL
)) {
2690 if (make_const_data (&oc
-> expression
,
2693 strlen (h
-> h_name
) + 1,
2695 option_code_hash_lookup(&oc
->option
,
2696 dhcp_universe
.code_hash
,
2698 save_option (&dhcp_universe
,
2699 state
-> options
, oc
);
2701 option_cache_dereference (&oc
, MDL
);
2706 /* If so directed, use the leased IP address as the router address.
2707 This supposedly makes Win95 machines ARP for all IP addresses,
2708 so if the local router does proxy arp, you win. */
2710 if (evaluate_boolean_option_cache
2711 (&ignorep
, packet
, lease
, (struct client_state
*)0,
2712 packet
-> options
, state
-> options
, &lease
-> scope
,
2713 lookup_option (&server_universe
, state
-> options
,
2714 SV_USE_LEASE_ADDR_FOR_DEFAULT_ROUTE
), MDL
)) {
2716 oc
= lookup_option (&dhcp_universe
, state
-> options
, i
);
2718 oc
= (struct option_cache
*)0;
2719 if (option_cache_allocate (&oc
, MDL
)) {
2720 if (make_const_data (&oc
-> expression
,
2721 lease
-> ip_addr
.iabuf
,
2722 lease
-> ip_addr
.len
,
2724 option_code_hash_lookup(&oc
->option
,
2725 dhcp_universe
.code_hash
,
2727 save_option (&dhcp_universe
,
2728 state
-> options
, oc
);
2730 option_cache_dereference (&oc
, MDL
);
2735 /* If a site option space has been specified, use that for
2736 site option codes. */
2737 i
= SV_SITE_OPTION_SPACE
;
2738 if ((oc
= lookup_option (&server_universe
, state
-> options
, i
)) &&
2739 evaluate_option_cache (&d1
, packet
, lease
,
2740 (struct client_state
*)0,
2741 packet
-> options
, state
-> options
,
2742 &lease
-> scope
, oc
, MDL
)) {
2743 struct universe
*u
= (struct universe
*)0;
2745 if (!universe_hash_lookup (&u
, universe_hash
,
2746 (const char *)d1
.data
, d1
.len
,
2748 log_error ("unknown option space %s.", d1
.data
);
2752 state
-> options
-> site_universe
= u
-> index
;
2753 state
->options
->site_code_min
= find_min_site_code(u
);
2754 data_string_forget (&d1
, MDL
);
2756 state
-> options
-> site_code_min
= 0;
2757 state
-> options
-> site_universe
= dhcp_universe
.index
;
2760 /* If the client has provided a list of options that it wishes
2761 returned, use it to prioritize. If there's a parameter
2762 request list in scope, use that in preference. Otherwise
2763 use the default priority list. */
2765 oc
= lookup_option (&dhcp_universe
, state
-> options
,
2766 DHO_DHCP_PARAMETER_REQUEST_LIST
);
2769 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
2770 DHO_DHCP_PARAMETER_REQUEST_LIST
);
2772 evaluate_option_cache (&state
-> parameter_request_list
,
2773 packet
, lease
, (struct client_state
*)0,
2774 packet
-> options
, state
-> options
,
2775 &lease
-> scope
, oc
, MDL
);
2778 dump_packet (packet
);
2779 dump_raw ((unsigned char *)packet
-> raw
, packet
-> packet_length
);
2782 lease
-> state
= state
;
2784 log_info ("%s", msg
);
2786 /* Hang the packet off the lease state. */
2787 packet_reference (&lease
-> state
-> packet
, packet
, MDL
);
2789 /* If this is a DHCPOFFER, ping the lease address before actually
2790 sending the offer. */
2791 if (offer
== DHCPOFFER
&& !(lease
-> flags
& STATIC_LEASE
) &&
2792 ((cur_time
- lease_cltt
) > 60) &&
2793 (!(oc
= lookup_option (&server_universe
, state
-> options
,
2795 evaluate_boolean_option_cache (&ignorep
, packet
, lease
,
2796 (struct client_state
*)0,
2799 &lease
-> scope
, oc
, MDL
))) {
2800 icmp_echorequest (&lease
-> ip_addr
);
2802 /* Determine whether to use configured or default ping timeout.
2804 if ((oc
= lookup_option (&server_universe
, state
-> options
,
2805 SV_PING_TIMEOUT
)) &&
2806 evaluate_option_cache (&d1
, packet
, lease
, NULL
,
2809 &lease
-> scope
, oc
, MDL
)) {
2810 if (d1
.len
== sizeof (u_int32_t
))
2811 ping_timeout
= getULong (d1
.data
);
2813 ping_timeout
= DEFAULT_PING_TIMEOUT
;
2815 data_string_forget (&d1
, MDL
);
2817 ping_timeout
= DEFAULT_PING_TIMEOUT
;
2820 log_debug ("Ping timeout: %ld", (long)ping_timeout
);
2823 tv
. tv_sec
= cur_time
+ ping_timeout
;
2825 add_timeout (&tv
, lease_ping_timeout
, lease
,
2826 (tvref_t
)lease_reference
,
2827 (tvunref_t
)lease_dereference
);
2828 ++outstanding_pings
;
2830 lease
->cltt
= cur_time
;
2831 #if defined(DELAYED_ACK)
2832 if (!(lease
->flags
& STATIC_LEASE
) &&
2833 (!offer
|| (offer
== DHCPACK
)))
2834 delayed_ack_enqueue(lease
);
2842 * CC: queue single ACK:
2843 * - write the lease (but do not fsync it yet)
2844 * - add to double linked list
2845 * - commit if more than xx ACKs pending
2846 * - if necessary set the max timer and bump the next timer
2847 * but only up to the max timer value.
2851 delayed_ack_enqueue(struct lease
*lease
)
2853 struct leasequeue
*q
;
2855 if (!write_lease(lease
))
2857 if (free_ackqueue
) {
2859 free_ackqueue
= q
->next
;
2861 q
= ((struct leasequeue
*)
2862 dmalloc(sizeof(struct leasequeue
), MDL
));
2864 log_fatal("delayed_ack_enqueue: no memory!");
2866 memset(q
, 0, sizeof *q
);
2867 /* prepend to ackqueue*/
2868 lease_reference(&q
->lease
, lease
, MDL
);
2869 q
->next
= ackqueue_head
;
2877 if (outstanding_acks
> max_outstanding_acks
) {
2880 /* Reset max_fsync and cancel any pending timeout. */
2881 memset(&max_fsync
, 0, sizeof(max_fsync
));
2882 cancel_timeout(commit_leases_ackout
, NULL
);
2884 struct timeval next_fsync
;
2886 if (max_fsync
.tv_sec
== 0 && max_fsync
.tv_usec
== 0) {
2887 /* set the maximum time we'll wait */
2888 max_fsync
.tv_sec
= cur_tv
.tv_sec
+ max_ack_delay_secs
;
2889 max_fsync
.tv_usec
= cur_tv
.tv_usec
+
2890 max_ack_delay_usecs
;
2892 if (max_fsync
.tv_usec
>= 1000000) {
2894 max_fsync
.tv_usec
-= 1000000;
2898 /* Set the timeout */
2899 next_fsync
.tv_sec
= cur_tv
.tv_sec
;
2900 next_fsync
.tv_usec
= cur_tv
.tv_usec
+ min_ack_delay_usecs
;
2901 if (next_fsync
.tv_usec
>= 1000000) {
2902 next_fsync
.tv_sec
++;
2903 next_fsync
.tv_usec
-= 1000000;
2905 /* but not more than the max */
2906 if ((next_fsync
.tv_sec
> max_fsync
.tv_sec
) ||
2907 ((next_fsync
.tv_sec
== max_fsync
.tv_sec
) &&
2908 (next_fsync
.tv_usec
> max_fsync
.tv_usec
))) {
2909 next_fsync
.tv_sec
= max_fsync
.tv_sec
;
2910 next_fsync
.tv_usec
= max_fsync
.tv_usec
;
2913 add_timeout(&next_fsync
, commit_leases_ackout
, NULL
,
2914 (tvref_t
) NULL
, (tvunref_t
) NULL
);
2919 commit_leases_ackout(void *foo
)
2921 if (outstanding_acks
) {
2924 memset(&max_fsync
, 0, sizeof(max_fsync
));
2928 /* CC: process the delayed ACK responses:
2929 - send out the ACK packets
2930 - move the queue slots to the free list
2933 flush_ackqueue(void *foo
)
2935 struct leasequeue
*ack
, *p
;
2936 /* process from bottom to retain packet order */
2937 for (ack
= ackqueue_tail
; ack
; ack
= p
) {
2940 /* dhcp_reply() requires that the reply state still be valid */
2941 if (ack
->lease
->state
== NULL
)
2942 log_error("delayed ack for %s has gone stale",
2943 piaddr(ack
->lease
->ip_addr
));
2945 dhcp_reply(ack
->lease
);
2947 lease_dereference(&ack
->lease
, MDL
);
2948 ack
->next
= free_ackqueue
;
2949 free_ackqueue
= ack
;
2951 ackqueue_head
= NULL
;
2952 ackqueue_tail
= NULL
;
2953 outstanding_acks
= 0;
2956 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
2958 relinquish_ackqueue(void)
2960 struct leasequeue
*q
, *n
;
2962 for (q
= ackqueue_head
; q
; q
= n
) {
2966 for (q
= free_ackqueue
; q
; q
= n
) {
2973 void dhcp_reply (lease
)
2974 struct lease
*lease
;
2977 unsigned packet_length
;
2978 struct dhcp_packet raw
;
2979 struct sockaddr_in to
;
2980 struct in_addr from
;
2981 struct hardware hto
;
2983 struct lease_state
*state
= lease
-> state
;
2984 int nulltp
, bootpp
, unicastp
= 1;
2985 struct data_string d1
;
2989 log_fatal ("dhcp_reply was supplied lease with no state!");
2991 /* Compose a response for the client... */
2992 memset (&raw
, 0, sizeof raw
);
2993 memset (&d1
, 0, sizeof d1
);
2995 /* Copy in the filename if given; otherwise, flag the filename
2996 buffer as available for options. */
2997 if (state
-> filename
.len
&& state
-> filename
.data
) {
2999 state
-> filename
.data
,
3000 state
-> filename
.len
> sizeof raw
.file
3001 ? sizeof raw
.file
: state
-> filename
.len
);
3002 if (sizeof raw
.file
> state
-> filename
.len
)
3003 memset (&raw
.file
[state
-> filename
.len
], 0,
3004 (sizeof raw
.file
) - state
-> filename
.len
);
3008 /* Copy in the server name if given; otherwise, flag the
3009 server_name buffer as available for options. */
3010 if (state
-> server_name
.len
&& state
-> server_name
.data
) {
3012 state
-> server_name
.data
,
3013 state
-> server_name
.len
> sizeof raw
.sname
3014 ? sizeof raw
.sname
: state
-> server_name
.len
);
3015 if (sizeof raw
.sname
> state
-> server_name
.len
)
3016 memset (&raw
.sname
[state
-> server_name
.len
], 0,
3017 (sizeof raw
.sname
) - state
-> server_name
.len
);
3019 bufs
|= 2; /* XXX */
3022 &lease
-> hardware_addr
.hbuf
[1], sizeof raw
.chaddr
);
3023 raw
.hlen
= lease
-> hardware_addr
.hlen
- 1;
3024 raw
.htype
= lease
-> hardware_addr
.hbuf
[0];
3026 /* See if this is a Microsoft client that NUL-terminates its
3027 strings and expects us to do likewise... */
3028 if (lease
-> flags
& MS_NULL_TERMINATION
)
3033 /* See if this is a bootp client... */
3039 /* Insert such options as will fit into the buffer. */
3040 packet_length
= cons_options (state
-> packet
, &raw
, lease
,
3041 (struct client_state
*)0,
3042 state
-> max_message_size
,
3043 state
-> packet
-> options
,
3044 state
-> options
, &global_scope
,
3045 bufs
, nulltp
, bootpp
,
3046 &state
-> parameter_request_list
,
3049 memcpy (&raw
.ciaddr
, &state
-> ciaddr
, sizeof raw
.ciaddr
);
3050 memcpy (&raw
.yiaddr
, lease
-> ip_addr
.iabuf
, 4);
3051 raw
.siaddr
= state
-> siaddr
;
3052 raw
.giaddr
= state
-> giaddr
;
3054 raw
.xid
= state
-> xid
;
3055 raw
.secs
= state
-> secs
;
3056 raw
.flags
= state
-> bootp_flags
;
3057 raw
.hops
= state
-> hops
;
3060 if (lease
-> client_hostname
) {
3061 if ((strlen (lease
-> client_hostname
) <= 64) &&
3062 db_printable((unsigned char *)lease
->client_hostname
))
3063 s
= lease
-> client_hostname
;
3065 s
= "Hostname Unsuitable for Printing";
3069 /* Say what we're doing... */
3070 log_info ("%s on %s to %s %s%s%svia %s",
3072 ? (state
-> offer
== DHCPACK
? "DHCPACK" : "DHCPOFFER")
3074 piaddr (lease
-> ip_addr
),
3075 (lease
-> hardware_addr
.hlen
3076 ? print_hw_addr (lease
-> hardware_addr
.hbuf
[0],
3077 lease
-> hardware_addr
.hlen
- 1,
3078 &lease
-> hardware_addr
.hbuf
[1])
3079 : print_hex_1(lease
->uid_len
, lease
->uid
, 60)),
3080 s
? "(" : "", s
? s
: "", s
? ") " : "",
3081 (state
-> giaddr
.s_addr
3082 ? inet_ntoa (state
-> giaddr
)
3083 : state
-> ip
-> name
));
3085 /* Set up the hardware address... */
3086 hto
.hlen
= lease
-> hardware_addr
.hlen
;
3087 memcpy (hto
.hbuf
, lease
-> hardware_addr
.hbuf
, hto
.hlen
);
3089 to
.sin_family
= AF_INET
;
3091 to
.sin_len
= sizeof to
;
3093 memset (to
.sin_zero
, 0, sizeof to
.sin_zero
);
3096 dump_raw ((unsigned char *)&raw
, packet_length
);
3099 /* Make sure outgoing packets are at least as big
3100 as a BOOTP packet. */
3101 if (packet_length
< BOOTP_MIN_LEN
)
3102 packet_length
= BOOTP_MIN_LEN
;
3104 /* If this was gatewayed, send it back to the gateway... */
3105 if (raw
.giaddr
.s_addr
) {
3106 to
.sin_addr
= raw
.giaddr
;
3107 if (raw
.giaddr
.s_addr
!= htonl (INADDR_LOOPBACK
))
3108 to
.sin_port
= local_port
;
3110 to
.sin_port
= remote_port
; /* For debugging. */
3112 if (fallback_interface
) {
3113 result
= send_packet (fallback_interface
,
3115 &raw
, packet_length
,
3117 (struct hardware
*)0);
3119 free_lease_state (state
, MDL
);
3120 lease
-> state
= (struct lease_state
*)0;
3124 /* If the client is RENEWING, unicast to the client using the
3125 regular IP stack. Some clients, particularly those that
3126 follow RFC1541, are buggy, and send both ciaddr and server
3127 identifier. We deal with this situation by assuming that
3128 if we got both dhcp-server-identifier and ciaddr, and
3129 giaddr was not set, then the client is on the local
3130 network, and we can therefore unicast or broadcast to it
3131 successfully. A client in REQUESTING state on another
3132 network that's making this mistake will have set giaddr,
3133 and will therefore get a relayed response from the above
3135 } else if (raw
.ciaddr
.s_addr
&&
3136 !((state
-> got_server_identifier
||
3137 (raw
.flags
& htons (BOOTP_BROADCAST
))) &&
3138 /* XXX This won't work if giaddr isn't zero, but it is: */
3139 (state
-> shared_network
==
3140 lease
-> subnet
-> shared_network
)) &&
3141 state
-> offer
== DHCPACK
) {
3142 to
.sin_addr
= raw
.ciaddr
;
3143 to
.sin_port
= remote_port
;
3145 if (fallback_interface
) {
3146 result
= send_packet (fallback_interface
,
3148 &raw
, packet_length
,
3150 (struct hardware
*)0);
3151 free_lease_state (state
, MDL
);
3152 lease
-> state
= (struct lease_state
*)0;
3156 /* If it comes from a client that already knows its address
3157 and is not requesting a broadcast response, and we can
3158 unicast to a client without using the ARP protocol, sent it
3159 directly to that client. */
3160 } else if (!(raw
.flags
& htons (BOOTP_BROADCAST
)) &&
3161 can_unicast_without_arp (state
-> ip
)) {
3162 to
.sin_addr
= raw
.yiaddr
;
3163 to
.sin_port
= remote_port
;
3165 /* Otherwise, broadcast it on the local network. */
3167 to
.sin_addr
= limited_broadcast
;
3168 to
.sin_port
= remote_port
;
3169 if (!(lease
-> flags
& UNICAST_BROADCAST_HACK
))
3173 memcpy (&from
, state
-> from
.iabuf
, sizeof from
);
3175 result
= send_packet (state
-> ip
,
3176 (struct packet
*)0, &raw
, packet_length
,
3178 unicastp
? &hto
: (struct hardware
*)0);
3180 /* Free all of the entries in the option_state structure
3181 now that we're done with them. */
3183 free_lease_state (state
, MDL
);
3184 lease
-> state
= (struct lease_state
*)0;
3187 int find_lease (struct lease
**lp
,
3188 struct packet
*packet
, struct shared_network
*share
, int *ours
,
3189 int *peer_has_leases
, struct lease
*ip_lease_in
,
3190 const char *file
, int line
)
3192 struct lease
*uid_lease
= (struct lease
*)0;
3193 struct lease
*ip_lease
= (struct lease
*)0;
3194 struct lease
*hw_lease
= (struct lease
*)0;
3195 struct lease
*lease
= (struct lease
*)0;
3197 struct host_decl
*hp
= (struct host_decl
*)0;
3198 struct host_decl
*host
= (struct host_decl
*)0;
3199 struct lease
*fixed_lease
= (struct lease
*)0;
3200 struct lease
*next
= (struct lease
*)0;
3201 struct option_cache
*oc
;
3202 struct data_string d1
;
3203 int have_client_identifier
= 0;
3204 struct data_string client_identifier
;
3207 #if defined(FAILOVER_PROTOCOL)
3208 /* Quick check to see if the peer has leases. */
3209 if (peer_has_leases
) {
3212 for (pool
= share
->pools
; pool
; pool
= pool
->next
) {
3213 dhcp_failover_state_t
*peer
= pool
->failover_peer
;
3216 ((peer
->i_am
== primary
&& pool
->backup_leases
) ||
3217 (peer
->i_am
== secondary
&& pool
->free_leases
))) {
3218 *peer_has_leases
= 1;
3223 #endif /* FAILOVER_PROTOCOL */
3225 if (packet
-> raw
-> ciaddr
.s_addr
) {
3227 memcpy (cip
.iabuf
, &packet
-> raw
-> ciaddr
, 4);
3229 /* Look up the requested address. */
3230 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
3231 DHO_DHCP_REQUESTED_ADDRESS
);
3232 memset (&d1
, 0, sizeof d1
);
3234 evaluate_option_cache (&d1
, packet
, (struct lease
*)0,
3235 (struct client_state
*)0,
3237 (struct option_state
*)0,
3238 &global_scope
, oc
, MDL
)) {
3239 packet
-> got_requested_address
= 1;
3241 memcpy (cip
.iabuf
, d1
.data
, cip
.len
);
3242 data_string_forget (&d1
, MDL
);
3247 /* Try to find a host or lease that's been assigned to the
3248 specified unique client identifier. */
3249 oc
= lookup_option (&dhcp_universe
, packet
-> options
,
3250 DHO_DHCP_CLIENT_IDENTIFIER
);
3251 memset (&client_identifier
, 0, sizeof client_identifier
);
3253 evaluate_option_cache (&client_identifier
,
3254 packet
, (struct lease
*)0,
3255 (struct client_state
*)0,
3256 packet
-> options
, (struct option_state
*)0,
3257 &global_scope
, oc
, MDL
)) {
3258 /* Remember this for later. */
3259 have_client_identifier
= 1;
3261 /* First, try to find a fixed host entry for the specified
3262 client identifier... */
3263 if (find_hosts_by_uid (&hp
, client_identifier
.data
,
3264 client_identifier
.len
, MDL
)) {
3265 /* Remember if we know of this client. */
3266 packet
-> known
= 1;
3267 mockup_lease (&fixed_lease
, packet
, share
, hp
);
3270 #if defined (DEBUG_FIND_LEASE)
3272 log_info ("Found host for client identifier: %s.",
3273 piaddr (fixed_lease
-> ip_addr
));
3277 if (!fixed_lease
) /* Save the host if we found one. */
3278 host_reference (&host
, hp
, MDL
);
3279 host_dereference (&hp
, MDL
);
3282 find_lease_by_uid (&uid_lease
, client_identifier
.data
,
3283 client_identifier
.len
, MDL
);
3286 /* If we didn't find a fixed lease using the uid, try doing
3287 it with the hardware address... */
3288 if (!fixed_lease
&& !host
) {
3289 if (find_hosts_by_haddr (&hp
, packet
-> raw
-> htype
,
3290 packet
-> raw
-> chaddr
,
3291 packet
-> raw
-> hlen
, MDL
)) {
3292 /* Remember if we know of this client. */
3293 packet
-> known
= 1;
3295 host_dereference (&host
, MDL
);
3296 host_reference (&host
, hp
, MDL
);
3297 host_dereference (&hp
, MDL
);
3298 mockup_lease (&fixed_lease
, packet
, share
, host
);
3299 #if defined (DEBUG_FIND_LEASE)
3301 log_info ("Found host for link address: %s.",
3302 piaddr (fixed_lease
-> ip_addr
));
3308 /* Finally, if we haven't found anything yet try again with the
3309 * host-identifier option ... */
3310 if (!fixed_lease
&& !host
) {
3311 if (find_hosts_by_option(&hp
, packet
,
3312 packet
->options
, MDL
) == 1) {
3315 host_dereference(&host
, MDL
);
3316 host_reference(&host
, hp
, MDL
);
3317 host_dereference(&hp
, MDL
);
3318 mockup_lease (&fixed_lease
, packet
, share
, host
);
3319 #if defined (DEBUG_FIND_LEASE)
3321 log_info ("Found host via host-identifier");
3327 /* If fixed_lease is present but does not match the requested
3328 IP address, and this is a DHCPREQUEST, then we can't return
3329 any other lease, so we might as well return now. */
3330 if (packet
-> packet_type
== DHCPREQUEST
&& fixed_lease
&&
3331 (fixed_lease
-> ip_addr
.len
!= cip
.len
||
3332 memcmp (fixed_lease
-> ip_addr
.iabuf
,
3333 cip
.iabuf
, cip
.len
))) {
3336 strcpy (dhcp_message
, "requested address is incorrect");
3337 #if defined (DEBUG_FIND_LEASE)
3338 log_info ("Client's fixed-address %s doesn't match %s%s",
3339 piaddr (fixed_lease
-> ip_addr
), "request ",
3340 print_dotted_quads (cip
.len
, cip
.iabuf
));
3346 * If we found leases matching the client identifier, loop through
3347 * the n_uid pointer looking for one that's actually valid. We
3348 * can't do this until we get here because we depend on
3349 * packet -> known, which may be set by either the uid host
3350 * lookup or the haddr host lookup.
3352 * Note that the n_uid lease chain is sorted in order of
3353 * preference, so the first one is the best one.
3356 #if defined (DEBUG_FIND_LEASE)
3357 log_info ("trying next lease matching client id: %s",
3358 piaddr (uid_lease
-> ip_addr
));
3361 #if defined (FAILOVER_PROTOCOL)
3363 * When we lookup a lease by uid, we know the client identifier
3364 * matches the lease's record. If it is active, or was last
3365 * active with the same client, we can trivially extend it.
3366 * If is not or was not active, we can allocate it to this
3367 * client if it matches the usual free/backup criteria (which
3368 * is contained in lease_mine_to_reallocate()).
3370 if (uid_lease
->binding_state
!= FTS_ACTIVE
&&
3371 uid_lease
->rewind_binding_state
!= FTS_ACTIVE
&&
3372 !lease_mine_to_reallocate(uid_lease
)) {
3373 #if defined (DEBUG_FIND_LEASE)
3374 log_info("not active or not mine to allocate: %s",
3375 piaddr(uid_lease
->ip_addr
));
3381 if (uid_lease
-> subnet
-> shared_network
!= share
) {
3382 #if defined (DEBUG_FIND_LEASE)
3383 log_info ("wrong network segment: %s",
3384 piaddr (uid_lease
-> ip_addr
));
3389 if ((uid_lease
-> pool
-> prohibit_list
&&
3390 permitted (packet
, uid_lease
-> pool
-> prohibit_list
)) ||
3391 (uid_lease
-> pool
-> permit_list
&&
3392 !permitted (packet
, uid_lease
-> pool
-> permit_list
))) {
3393 #if defined (DEBUG_FIND_LEASE)
3394 log_info ("not permitted: %s",
3395 piaddr (uid_lease
-> ip_addr
));
3398 if (uid_lease
-> n_uid
)
3399 lease_reference (&next
,
3400 uid_lease
-> n_uid
, MDL
);
3401 if (!packet
-> raw
-> ciaddr
.s_addr
)
3402 release_lease (uid_lease
, packet
);
3403 lease_dereference (&uid_lease
, MDL
);
3405 lease_reference (&uid_lease
, next
, MDL
);
3406 lease_dereference (&next
, MDL
);
3412 #if defined (DEBUG_FIND_LEASE)
3414 log_info ("Found lease for client id: %s.",
3415 piaddr (uid_lease
-> ip_addr
));
3418 /* Find a lease whose hardware address matches, whose client
3419 * identifier matches (or equally doesn't have one), that's
3420 * permitted, and that's on the correct subnet.
3422 * Note that the n_hw chain is sorted in order of preference, so
3423 * the first one found is the best one.
3425 h
.hlen
= packet
-> raw
-> hlen
+ 1;
3426 h
.hbuf
[0] = packet
-> raw
-> htype
;
3427 memcpy (&h
.hbuf
[1], packet
-> raw
-> chaddr
, packet
-> raw
-> hlen
);
3428 find_lease_by_hw_addr (&hw_lease
, h
.hbuf
, h
.hlen
, MDL
);
3430 #if defined (DEBUG_FIND_LEASE)
3431 log_info ("trying next lease matching hw addr: %s",
3432 piaddr (hw_lease
-> ip_addr
));
3434 #if defined (FAILOVER_PROTOCOL)
3436 * When we lookup a lease by chaddr, we know the MAC address
3437 * matches the lease record (we will check if the lease has a
3438 * client-id the client does not next). If the lease is
3439 * currently active or was last active with this client, we can
3440 * trivially extend it. Otherwise, there are a set of rules
3441 * that govern if we can reallocate this lease to any client
3442 * ("lease_mine_to_reallocate()") including this one.
3444 if (hw_lease
->binding_state
!= FTS_ACTIVE
&&
3445 hw_lease
->rewind_binding_state
!= FTS_ACTIVE
&&
3446 !lease_mine_to_reallocate(hw_lease
)) {
3447 #if defined (DEBUG_FIND_LEASE)
3448 log_info("not active or not mine to allocate: %s",
3449 piaddr(hw_lease
->ip_addr
));
3456 * This conditional skips "potentially active" leases (leases
3457 * we think are expired may be extended by the peer, etc) that
3458 * may be assigned to a differently /client-identified/ client
3459 * with the same MAC address.
3461 if (hw_lease
-> binding_state
!= FTS_FREE
&&
3462 hw_lease
-> binding_state
!= FTS_BACKUP
&&
3464 (!have_client_identifier
||
3465 hw_lease
-> uid_len
!= client_identifier
.len
||
3466 memcmp (hw_lease
-> uid
, client_identifier
.data
,
3467 hw_lease
-> uid_len
))) {
3468 #if defined (DEBUG_FIND_LEASE)
3469 log_info ("wrong client identifier: %s",
3470 piaddr (hw_lease
-> ip_addr
));
3474 if (hw_lease
-> subnet
-> shared_network
!= share
) {
3475 #if defined (DEBUG_FIND_LEASE)
3476 log_info ("wrong network segment: %s",
3477 piaddr (hw_lease
-> ip_addr
));
3481 if ((hw_lease
-> pool
-> prohibit_list
&&
3482 permitted (packet
, hw_lease
-> pool
-> prohibit_list
)) ||
3483 (hw_lease
-> pool
-> permit_list
&&
3484 !permitted (packet
, hw_lease
-> pool
-> permit_list
))) {
3485 #if defined (DEBUG_FIND_LEASE)
3486 log_info ("not permitted: %s",
3487 piaddr (hw_lease
-> ip_addr
));
3489 if (!packet
-> raw
-> ciaddr
.s_addr
)
3490 release_lease (hw_lease
, packet
);
3492 if (hw_lease
-> n_hw
)
3493 lease_reference (&next
, hw_lease
-> n_hw
, MDL
);
3494 lease_dereference (&hw_lease
, MDL
);
3496 lease_reference (&hw_lease
, next
, MDL
);
3497 lease_dereference (&next
, MDL
);
3503 #if defined (DEBUG_FIND_LEASE)
3505 log_info ("Found lease for hardware address: %s.",
3506 piaddr (hw_lease
-> ip_addr
));
3509 /* Try to find a lease that's been allocated to the client's
3512 lease_reference (&ip_lease
, ip_lease_in
, MDL
);
3514 find_lease_by_ip_addr (&ip_lease
, cip
, MDL
);
3516 #if defined (DEBUG_FIND_LEASE)
3518 log_info ("Found lease for requested address: %s.",
3519 piaddr (ip_lease
-> ip_addr
));
3522 /* If ip_lease is valid at this point, set ours to one, so that
3523 even if we choose a different lease, we know that the address
3524 the client was requesting was ours, and thus we can NAK it. */
3525 if (ip_lease
&& ours
)
3528 /* If the requested IP address isn't on the network the packet
3529 came from, don't use it. Allow abandoned leases to be matched
3530 here - if the client is requesting it, there's a decent chance
3531 that it's because the lease database got trashed and a client
3532 that thought it had this lease answered an ARP or PING, causing the
3533 lease to be abandoned. If so, this request probably came from
3535 if (ip_lease
&& (ip_lease
-> subnet
-> shared_network
!= share
)) {
3538 #if defined (DEBUG_FIND_LEASE)
3539 log_info ("...but it was on the wrong shared network.");
3541 strcpy (dhcp_message
, "requested address on bad subnet");
3542 lease_dereference (&ip_lease
, MDL
);
3546 * If the requested address is in use (or potentially in use) by
3547 * a different client, it can't be granted.
3549 * This first conditional only detects if the lease is currently
3550 * identified to a different client (client-id and/or chaddr
3551 * mismatch). In this case we may not want to give the client the
3552 * lease, if doing so may potentially be an addressing conflict.
3556 (!have_client_identifier
||
3557 ip_lease
-> uid_len
!= client_identifier
.len
||
3558 memcmp (ip_lease
-> uid
, client_identifier
.data
,
3559 ip_lease
-> uid_len
)) :
3560 (ip_lease
-> hardware_addr
.hbuf
[0] != packet
-> raw
-> htype
||
3561 ip_lease
-> hardware_addr
.hlen
!= packet
-> raw
-> hlen
+ 1 ||
3562 memcmp (&ip_lease
-> hardware_addr
.hbuf
[1],
3563 packet
-> raw
-> chaddr
,
3564 (unsigned)(ip_lease
-> hardware_addr
.hlen
- 1))))) {
3566 * A lease is unavailable for allocation to a new client if
3567 * it is not in the FREE or BACKUP state. There may be
3568 * leases that are in the expired state with a rewinding
3569 * state that is free or backup, but these will be processed
3570 * into the free or backup states by expiration processes, so
3571 * checking for them here is superfluous.
3573 if (ip_lease
-> binding_state
!= FTS_FREE
&&
3574 ip_lease
-> binding_state
!= FTS_BACKUP
) {
3575 #if defined (DEBUG_FIND_LEASE)
3576 log_info ("rejecting lease for requested address.");
3578 /* If we're rejecting it because the peer has
3579 it, don't set "ours", because we shouldn't NAK. */
3580 if (ours
&& ip_lease
-> binding_state
!= FTS_ACTIVE
)
3582 lease_dereference (&ip_lease
, MDL
);
3587 * If we got an ip_lease and a uid_lease or hw_lease, and ip_lease
3588 * is/was not active, and is not ours to reallocate, forget about it.
3590 if (ip_lease
&& (uid_lease
|| hw_lease
) &&
3591 ip_lease
->binding_state
!= FTS_ACTIVE
&&
3592 ip_lease
->rewind_binding_state
!= FTS_ACTIVE
&&
3593 #if defined(FAILOVER_PROTOCOL)
3594 !lease_mine_to_reallocate(ip_lease
) &&
3596 packet
->packet_type
== DHCPDISCOVER
) {
3597 #if defined (DEBUG_FIND_LEASE)
3598 log_info("ip lease not active or not ours to offer.");
3600 lease_dereference(&ip_lease
, MDL
);
3603 /* If for some reason the client has more than one lease
3604 on the subnet that matches its uid, pick the one that
3605 it asked for and (if we can) free the other. */
3606 if (ip_lease
&& ip_lease
->binding_state
== FTS_ACTIVE
&&
3607 ip_lease
->uid
&& ip_lease
!= uid_lease
) {
3608 if (have_client_identifier
&&
3609 (ip_lease
-> uid_len
== client_identifier
.len
) &&
3610 !memcmp (client_identifier
.data
,
3611 ip_lease
-> uid
, ip_lease
-> uid_len
)) {
3613 if (uid_lease
->binding_state
== FTS_ACTIVE
) {
3614 log_error ("client %s has duplicate%s on %s",
3616 (packet
-> raw
-> htype
,
3617 packet
-> raw
-> hlen
,
3618 packet
-> raw
-> chaddr
)),
3620 (ip_lease
-> subnet
->
3621 shared_network
-> name
));
3623 /* If the client is REQUESTing the lease,
3624 it shouldn't still be using the old
3625 one, so we can free it for allocation. */
3627 uid_lease
->binding_state
== FTS_ACTIVE
&&
3628 !packet
-> raw
-> ciaddr
.s_addr
&&
3630 uid_lease
-> subnet
-> shared_network
) &&
3631 packet
-> packet_type
== DHCPREQUEST
)
3632 release_lease (uid_lease
, packet
);
3634 lease_dereference (&uid_lease
, MDL
);
3635 lease_reference (&uid_lease
, ip_lease
, MDL
);
3639 /* If we get to here and fixed_lease is not null, that means
3640 that there are both a dynamic lease and a fixed-address
3641 declaration for the same IP address. */
3642 if (packet
-> packet_type
== DHCPREQUEST
&& fixed_lease
) {
3643 lease_dereference (&fixed_lease
, MDL
);
3645 log_error ("Dynamic and static leases present for %s.",
3647 log_error ("Remove host declaration %s or remove %s",
3648 (fixed_lease
&& fixed_lease
-> host
3649 ? (fixed_lease
-> host
-> name
3650 ? fixed_lease
-> host
-> name
3654 log_error ("from the dynamic address pool for %s",
3655 ip_lease
-> subnet
-> shared_network
-> name
3658 lease_dereference (&ip_lease
, MDL
);
3659 strcpy (dhcp_message
,
3660 "database conflict - call for help!");
3663 if (ip_lease
&& ip_lease
!= uid_lease
) {
3664 #if defined (DEBUG_FIND_LEASE)
3665 log_info ("requested address not available.");
3667 lease_dereference (&ip_lease
, MDL
);
3671 /* If we get to here with both fixed_lease and ip_lease not
3672 null, then we have a configuration file bug. */
3673 if (packet
-> packet_type
== DHCPREQUEST
&& fixed_lease
&& ip_lease
)
3676 /* Toss extra pointers to the same lease... */
3677 if (hw_lease
&& hw_lease
== uid_lease
) {
3678 #if defined (DEBUG_FIND_LEASE)
3679 log_info ("hardware lease and uid lease are identical.");
3681 lease_dereference (&hw_lease
, MDL
);
3683 if (ip_lease
&& ip_lease
== hw_lease
) {
3684 lease_dereference (&hw_lease
, MDL
);
3685 #if defined (DEBUG_FIND_LEASE)
3686 log_info ("hardware lease and ip lease are identical.");
3689 if (ip_lease
&& ip_lease
== uid_lease
) {
3690 lease_dereference (&uid_lease
, MDL
);
3691 #if defined (DEBUG_FIND_LEASE)
3692 log_info ("uid lease and ip lease are identical.");
3696 /* Make sure the client is permitted to use the requested lease. */
3698 ((ip_lease
-> pool
-> prohibit_list
&&
3699 permitted (packet
, ip_lease
-> pool
-> prohibit_list
)) ||
3700 (ip_lease
-> pool
-> permit_list
&&
3701 !permitted (packet
, ip_lease
-> pool
-> permit_list
)))) {
3702 if (!packet
->raw
->ciaddr
.s_addr
&&
3703 (ip_lease
->binding_state
== FTS_ACTIVE
))
3704 release_lease (ip_lease
, packet
);
3706 lease_dereference (&ip_lease
, MDL
);
3710 ((uid_lease
-> pool
-> prohibit_list
&&
3711 permitted (packet
, uid_lease
-> pool
-> prohibit_list
)) ||
3712 (uid_lease
-> pool
-> permit_list
&&
3713 !permitted (packet
, uid_lease
-> pool
-> permit_list
)))) {
3714 if (!packet
-> raw
-> ciaddr
.s_addr
)
3715 release_lease (uid_lease
, packet
);
3716 lease_dereference (&uid_lease
, MDL
);
3720 ((hw_lease
-> pool
-> prohibit_list
&&
3721 permitted (packet
, hw_lease
-> pool
-> prohibit_list
)) ||
3722 (hw_lease
-> pool
-> permit_list
&&
3723 !permitted (packet
, hw_lease
-> pool
-> permit_list
)))) {
3724 if (!packet
-> raw
-> ciaddr
.s_addr
)
3725 release_lease (hw_lease
, packet
);
3726 lease_dereference (&hw_lease
, MDL
);
3729 /* If we've already eliminated the lease, it wasn't there to
3730 begin with. If we have come up with a matching lease,
3731 set the message to bad network in case we have to throw it out. */
3733 strcpy (dhcp_message
, "requested address not available");
3736 /* If this is a DHCPREQUEST, make sure the lease we're going to return
3737 matches the requested IP address. If it doesn't, don't return a
3739 if (packet
-> packet_type
== DHCPREQUEST
&&
3740 !ip_lease
&& !fixed_lease
) {
3741 #if defined (DEBUG_FIND_LEASE)
3742 log_info ("no applicable lease found for DHCPREQUEST.");
3747 /* At this point, if fixed_lease is nonzero, we can assign it to
3750 lease_reference (&lease
, fixed_lease
, MDL
);
3751 lease_dereference (&fixed_lease
, MDL
);
3752 #if defined (DEBUG_FIND_LEASE)
3753 log_info ("choosing fixed address.");
3757 /* If we got a lease that matched the ip address and don't have
3758 a better offer, use that; otherwise, release it. */
3761 if (!packet
-> raw
-> ciaddr
.s_addr
)
3762 release_lease (ip_lease
, packet
);
3763 #if defined (DEBUG_FIND_LEASE)
3764 log_info ("not choosing requested address (!).");
3767 #if defined (DEBUG_FIND_LEASE)
3768 log_info ("choosing lease on requested address.");
3770 lease_reference (&lease
, ip_lease
, MDL
);
3772 host_dereference (&lease
-> host
, MDL
);
3774 lease_dereference (&ip_lease
, MDL
);
3777 /* If we got a lease that matched the client identifier, we may want
3778 to use it, but if we already have a lease we like, we must free
3779 the lease that matched the client identifier. */
3782 log_error("uid lease %s for client %s is duplicate "
3784 piaddr(uid_lease
->ip_addr
),
3785 print_hw_addr(packet
->raw
->htype
,
3787 packet
->raw
->chaddr
),
3788 uid_lease
->subnet
->shared_network
->name
);
3790 if (!packet
-> raw
-> ciaddr
.s_addr
&&
3791 packet
-> packet_type
== DHCPREQUEST
&&
3792 uid_lease
-> binding_state
== FTS_ACTIVE
)
3793 release_lease(uid_lease
, packet
);
3794 #if defined (DEBUG_FIND_LEASE)
3795 log_info ("not choosing uid lease.");
3798 lease_reference (&lease
, uid_lease
, MDL
);
3800 host_dereference (&lease
-> host
, MDL
);
3801 #if defined (DEBUG_FIND_LEASE)
3802 log_info ("choosing uid lease.");
3805 lease_dereference (&uid_lease
, MDL
);
3808 /* The lease that matched the hardware address is treated likewise. */
3811 #if defined (DEBUG_FIND_LEASE)
3812 log_info ("not choosing hardware lease.");
3815 /* We're a little lax here - if the client didn't
3816 send a client identifier and it's a bootp client,
3817 but the lease has a client identifier, we still
3818 let the client have a lease. */
3819 if (!hw_lease
-> uid_len
||
3820 (have_client_identifier
3821 ? (hw_lease
-> uid_len
==
3822 client_identifier
.len
&&
3823 !memcmp (hw_lease
-> uid
,
3824 client_identifier
.data
,
3825 client_identifier
.len
))
3826 : packet
-> packet_type
== 0)) {
3827 lease_reference (&lease
, hw_lease
, MDL
);
3829 host_dereference (&lease
-> host
, MDL
);
3830 #if defined (DEBUG_FIND_LEASE)
3831 log_info ("choosing hardware lease.");
3834 #if defined (DEBUG_FIND_LEASE)
3835 log_info ("not choosing hardware lease: %s.",
3840 lease_dereference (&hw_lease
, MDL
);
3843 /* If we found a host_decl but no matching address, try to
3844 find a host_decl that has no address, and if there is one,
3845 hang it off the lease so that we can use the supplied
3847 if (lease
&& host
&& !lease
-> host
) {
3848 struct host_decl
*p
= (struct host_decl
*)0;
3849 struct host_decl
*n
= (struct host_decl
*)0;
3850 host_reference (&p
, host
, MDL
);
3852 if (!p
-> fixed_addr
) {
3853 host_reference (&lease
-> host
, p
, MDL
);
3854 host_dereference (&p
, MDL
);
3858 host_reference (&n
, p
-> n_ipaddr
, MDL
);
3859 host_dereference (&p
, MDL
);
3861 host_reference (&p
, n
, MDL
);
3862 host_dereference (&n
, MDL
);
3867 /* If we find an abandoned lease, but it's the one the client
3868 requested, we assume that previous bugginess on the part
3869 of the client, or a server database loss, caused the lease to
3870 be abandoned, so we reclaim it and let the client have it. */
3872 (lease
-> binding_state
== FTS_ABANDONED
) &&
3873 lease
== ip_lease
&&
3874 packet
-> packet_type
== DHCPREQUEST
) {
3875 log_error ("Reclaiming REQUESTed abandoned IP address %s.",
3876 piaddr (lease
-> ip_addr
));
3877 } else if (lease
&& (lease
-> binding_state
== FTS_ABANDONED
)) {
3878 /* Otherwise, if it's not the one the client requested, we do not
3879 return it - instead, we claim it's ours, causing a DHCPNAK to be
3880 sent if this lookup is for a DHCPREQUEST, and force the client
3881 to go back through the allocation process. */
3884 lease_dereference (&lease
, MDL
);
3888 if (have_client_identifier
)
3889 data_string_forget (&client_identifier
, MDL
);
3892 lease_dereference (&fixed_lease
, MDL
);
3894 lease_dereference (&hw_lease
, MDL
);
3896 lease_dereference (&uid_lease
, MDL
);
3898 lease_dereference (&ip_lease
, MDL
);
3900 host_dereference (&host
, MDL
);
3903 #if defined (DEBUG_FIND_LEASE)
3904 log_info ("Returning lease: %s.",
3905 piaddr (lease
-> ip_addr
));
3907 lease_reference (lp
, lease
, file
, line
);
3908 lease_dereference (&lease
, MDL
);
3911 #if defined (DEBUG_FIND_LEASE)
3912 log_info ("Not returning a lease.");
3917 /* Search the provided host_decl structure list for an address that's on
3918 the specified shared network. If one is found, mock up and return a
3919 lease structure for it; otherwise return the null pointer. */
3921 int mockup_lease (struct lease
**lp
, struct packet
*packet
,
3922 struct shared_network
*share
, struct host_decl
*hp
)
3924 struct lease
*lease
= (struct lease
*)0;
3925 struct host_decl
*rhp
= (struct host_decl
*)0;
3927 if (lease_allocate (&lease
, MDL
) != ISC_R_SUCCESS
)
3929 if (host_reference (&rhp
, hp
, MDL
) != ISC_R_SUCCESS
) {
3930 lease_dereference (&lease
, MDL
);
3933 if (!find_host_for_network (&lease
-> subnet
,
3934 &rhp
, &lease
-> ip_addr
, share
)) {
3935 lease_dereference (&lease
, MDL
);
3936 host_dereference (&rhp
, MDL
);
3939 host_reference (&lease
-> host
, rhp
, MDL
);
3940 if (rhp
-> client_identifier
.len
> sizeof lease
-> uid_buf
)
3941 lease
-> uid
= dmalloc (rhp
-> client_identifier
.len
, MDL
);
3943 lease
-> uid
= lease
-> uid_buf
;
3944 if (!lease
-> uid
) {
3945 lease_dereference (&lease
, MDL
);
3946 host_dereference (&rhp
, MDL
);
3949 memcpy (lease
-> uid
, rhp
-> client_identifier
.data
,
3950 rhp
-> client_identifier
.len
);
3951 lease
-> uid_len
= rhp
-> client_identifier
.len
;
3952 lease
-> hardware_addr
= rhp
-> interface
;
3953 lease
-> starts
= lease
-> cltt
= lease
-> ends
= MIN_TIME
;
3954 lease
-> flags
= STATIC_LEASE
;
3955 lease
-> binding_state
= FTS_FREE
;
3957 lease_reference (lp
, lease
, MDL
);
3959 lease_dereference (&lease
, MDL
);
3960 host_dereference (&rhp
, MDL
);
3964 /* Look through all the pools in a list starting with the specified pool
3965 for a free lease. We try to find a virgin lease if we can. If we
3966 don't find a virgin lease, we try to find a non-virgin lease that's
3967 free. If we can't find one of those, we try to reclaim an abandoned
3968 lease. If all of these possibilities fail to pan out, we don't return
3971 int allocate_lease (struct lease
**lp
, struct packet
*packet
,
3972 struct pool
*pool
, int *peer_has_leases
)
3974 struct lease
*lease
= (struct lease
*)0;
3975 struct lease
*candl
= (struct lease
*)0;
3977 for (; pool
; pool
= pool
-> next
) {
3978 if ((pool
-> prohibit_list
&&
3979 permitted (packet
, pool
-> prohibit_list
)) ||
3980 (pool
-> permit_list
&&
3981 !permitted (packet
, pool
-> permit_list
)))
3984 #if defined (FAILOVER_PROTOCOL)
3985 /* Peer_has_leases just says that we found at least one
3986 free lease. If no free lease is returned, the caller
3987 can deduce that this means the peer is hogging all the
3988 free leases, so we can print a better error message. */
3989 /* XXX Do we need code here to ignore PEER_IS_OWNER and
3990 * XXX just check tstp if we're in, e.g., PARTNER_DOWN?
3991 * XXX Where do we deal with CONFLICT_DETECTED, et al? */
3992 /* XXX This should be handled by the lease binding "state
3993 * XXX machine" - that is, when we get here, if a lease
3994 * XXX could be allocated, it will have the correct
3995 * XXX binding state so that the following code will
3996 * XXX result in its being allocated. */
3997 /* Skip to the most expired lease in the pool that is not
3998 * owned by a failover peer. */
3999 if (pool
->failover_peer
!= NULL
) {
4000 if (pool
->failover_peer
->i_am
== primary
) {
4004 * In normal operation, we never want to touch
4005 * the peer's leases. In partner-down
4006 * operation, we need to be able to pick up
4007 * the peer's leases after STOS+MCLT.
4009 if (pool
->backup
!= NULL
) {
4010 if (((candl
== NULL
) ||
4012 pool
->backup
->ends
)) &&
4013 lease_mine_to_reallocate(
4015 candl
= pool
->backup
;
4017 *peer_has_leases
= 1;
4021 candl
= pool
->backup
;
4023 if (pool
->free
!= NULL
) {
4024 if (((candl
== NULL
) ||
4026 pool
->free
->ends
)) &&
4027 lease_mine_to_reallocate(
4031 *peer_has_leases
= 1;
4036 /* Try abandoned leases as a last resort. */
4037 if ((candl
== NULL
) &&
4038 (pool
->abandoned
!= NULL
) &&
4039 lease_mine_to_reallocate(pool
->abandoned
))
4040 candl
= pool
->abandoned
;
4045 candl
= pool
-> free
;
4047 candl
= pool
-> abandoned
;
4051 * XXX: This may not match with documented expectation.
4052 * It's expected that when we OFFER a lease, we set its
4053 * ends time forward 2 minutes so that it gets sorted to
4054 * the end of its free list (avoiding a similar allocation
4055 * to another client). It is not expected that we issue a
4056 * "no free leases" error when the last lease has been
4057 * offered, but it's not exactly broken either.
4059 if (!candl
|| (candl
-> ends
> cur_time
))
4068 * There are tiers of lease state preference, listed here in
4069 * reverse order (least to most preferential):
4074 * If the selected lease and candidate are both of the same
4075 * state, select the oldest (longest ago) expiration time
4076 * between the two. If the candidate lease is of a higher
4077 * preferred grade over the selected lease, use it.
4079 if ((lease
-> binding_state
== FTS_ABANDONED
) &&
4080 ((candl
-> binding_state
!= FTS_ABANDONED
) ||
4081 (candl
-> ends
< lease
-> ends
))) {
4084 } else if (candl
-> binding_state
== FTS_ABANDONED
)
4087 if ((lease
-> uid_len
|| lease
-> hardware_addr
.hlen
) &&
4088 ((!candl
-> uid_len
&& !candl
-> hardware_addr
.hlen
) ||
4089 (candl
-> ends
< lease
-> ends
))) {
4092 } else if (candl
-> uid_len
|| candl
-> hardware_addr
.hlen
)
4095 if (candl
-> ends
< lease
-> ends
)
4100 if (lease
-> binding_state
== FTS_ABANDONED
)
4101 log_error ("Reclaiming abandoned lease %s.",
4102 piaddr (lease
-> ip_addr
));
4104 lease_reference (lp
, lease
, MDL
);
4111 /* Determine whether or not a permit exists on a particular permit list
4112 that matches the specified packet, returning nonzero if so, zero if
4115 int permitted (packet
, permit_list
)
4116 struct packet
*packet
;
4117 struct permit
*permit_list
;
4122 for (p
= permit_list
; p
; p
= p
-> next
) {
4123 switch (p
-> type
) {
4124 case permit_unknown_clients
:
4125 if (!packet
-> known
)
4129 case permit_known_clients
:
4130 if (packet
-> known
)
4134 case permit_authenticated_clients
:
4135 if (packet
-> authenticated
)
4139 case permit_unauthenticated_clients
:
4140 if (!packet
-> authenticated
)
4144 case permit_all_clients
:
4147 case permit_dynamic_bootp_clients
:
4148 if (!packet
-> options_valid
||
4149 !packet
-> packet_type
)
4154 for (i
= 0; i
< packet
-> class_count
; i
++) {
4155 if (p
-> class == packet
-> classes
[i
])
4157 if (packet
-> classes
[i
] &&
4158 packet
-> classes
[i
] -> superclass
&&
4159 (packet
-> classes
[i
] -> superclass
==
4166 if (cur_time
> p
->after
)
4174 int locate_network (packet
)
4175 struct packet
*packet
;
4178 struct data_string data
;
4179 struct subnet
*subnet
= (struct subnet
*)0;
4180 struct option_cache
*oc
;
4182 /* See if there's a Relay Agent Link Selection Option, or a
4183 * Subnet Selection Option. The Link-Select and Subnet-Select
4184 * are formatted and used precisely the same, but we must prefer
4185 * the link-select over the subnet-select.
4187 if ((oc
= lookup_option(&agent_universe
, packet
->options
,
4188 RAI_LINK_SELECT
)) == NULL
)
4189 oc
= lookup_option(&dhcp_universe
, packet
->options
,
4190 DHO_SUBNET_SELECTION
);
4192 /* If there's no SSO and no giaddr, then use the shared_network
4193 from the interface, if there is one. If not, fail. */
4194 if (!oc
&& !packet
-> raw
-> giaddr
.s_addr
) {
4195 if (packet
-> interface
-> shared_network
) {
4196 shared_network_reference
4197 (&packet
-> shared_network
,
4198 packet
-> interface
-> shared_network
, MDL
);
4204 /* If there's an option indicating link connection, and it's valid,
4205 * use it to figure out the subnet. If it's not valid, fail.
4208 memset (&data
, 0, sizeof data
);
4209 if (!evaluate_option_cache (&data
, packet
, (struct lease
*)0,
4210 (struct client_state
*)0,
4212 (struct option_state
*)0,
4213 &global_scope
, oc
, MDL
)) {
4216 if (data
.len
!= 4) {
4220 memcpy (ia
.iabuf
, data
.data
, 4);
4221 data_string_forget (&data
, MDL
);
4224 memcpy (ia
.iabuf
, &packet
-> raw
-> giaddr
, 4);
4227 /* If we know the subnet on which the IP address lives, use it. */
4228 if (find_subnet (&subnet
, ia
, MDL
)) {
4229 shared_network_reference (&packet
-> shared_network
,
4230 subnet
-> shared_network
, MDL
);
4231 subnet_dereference (&subnet
, MDL
);
4235 /* Otherwise, fail. */
4240 * Try to figure out the source address to send packets from.
4242 * If the packet we received specified the server address, then we
4245 * Otherwise, use the first address from the interface. If we do
4246 * this, we also save this into the option cache as the server
4250 get_server_source_address(struct in_addr
*from
,
4251 struct option_state
*options
,
4252 struct packet
*packet
) {
4253 unsigned option_num
;
4254 struct option_cache
*oc
;
4255 struct data_string d
;
4258 memset(&d
, 0, sizeof(d
));
4260 option_num
= DHO_DHCP_SERVER_IDENTIFIER
;
4261 oc
= lookup_option(&dhcp_universe
, options
, option_num
);
4263 evaluate_option_cache(&d
, packet
, NULL
, NULL
, packet
->options
,
4264 options
, &global_scope
, oc
, MDL
)) {
4265 if (d
.len
== sizeof(*from
)) {
4266 memcpy(from
, d
.data
, sizeof(*from
));
4267 data_string_forget(&d
, MDL
);
4270 data_string_forget(&d
, MDL
);
4273 if (packet
->interface
->address_count
> 0) {
4274 if (option_cache_allocate(&oc
, MDL
)) {
4275 a
= &packet
->interface
->addresses
[0];
4276 if (make_const_data(&oc
->expression
,
4277 (unsigned char *)a
, sizeof(*a
),
4279 option_code_hash_lookup(&oc
->option
,
4280 dhcp_universe
.code_hash
,
4281 &option_num
, 0, MDL
);
4282 save_option(&dhcp_universe
, options
, oc
);
4284 option_cache_dereference(&oc
, MDL
);
4286 *from
= packet
->interface
->addresses
[0];
4288 memset(from
, 0, sizeof(*from
));
4293 * Look for the lowest numbered site code number and
4294 * apply a log warning if it is less than 224. Do not
4295 * permit site codes less than 128 (old code never did).
4297 * Note that we could search option codes 224 down to 128
4298 * on the hash table, but the table is (probably) smaller
4299 * than that if it was declared as a standalone table with
4300 * defaults. So we traverse the option code hash.
4303 find_min_site_code(struct universe
*u
)
4305 if (u
->site_code_min
)
4306 return u
->site_code_min
;
4309 * Note that site_code_min has to be global as we can't pass an
4310 * argument through hash_foreach(). The value 224 is taken from
4313 site_code_min
= 224;
4314 option_code_hash_foreach(u
->code_hash
, lowest_site_code
);
4316 if (site_code_min
< 224) {
4317 log_error("WARNING: site-local option codes less than 224 have "
4318 "been deprecated by RFC3942. You have options "
4319 "listed in site local space %s that number as low as "
4320 "%d. Please investigate if these should be declared "
4321 "as regular options rather than site-local options, "
4322 "or migrated up past 224.",
4323 u
->name
, site_code_min
);
4327 * don't even bother logging, this is just silly, and never worked
4328 * on any old version of software.
4330 if (site_code_min
< 128)
4331 site_code_min
= 128;
4334 * Cache the determined minimum site code on the universe structure.
4335 * Note that due to the < 128 check above, a value of zero is
4338 u
->site_code_min
= site_code_min
;
4340 return site_code_min
;
4344 lowest_site_code(const void *key
, unsigned len
, void *object
)
4346 struct option
*option
= object
;
4348 if (option
->code
< site_code_min
)
4349 site_code_min
= option
->code
;
4351 return ISC_R_SUCCESS
;
4355 maybe_return_agent_options(struct packet
*packet
, struct option_state
*options
)
4357 /* If there were agent options in the incoming packet, return
4358 * them. Do not return the agent options if they were stashed
4359 * on the lease. We do not check giaddr to detect the presence of
4360 * a relay, as this excludes "l2" relay agents which have no giaddr
4363 * XXX: If the user configures options for the relay agent information
4364 * (state->options->universes[agent_universe.index] is not NULL),
4365 * we're still required to duplicate other values provided by the
4366 * relay agent. So we need to merge the old values not configured
4367 * by the user into the new state, not just give up.
4369 if (!packet
->agent_options_stashed
&&
4370 packet
->options
->universe_count
> agent_universe
.index
&&
4371 packet
->options
->universes
[agent_universe
.index
] != NULL
&&
4372 (options
->universe_count
<= agent_universe
.index
||
4373 options
->universes
[agent_universe
.index
] == NULL
)) {
4374 option_chain_head_reference
4375 ((struct option_chain_head
**)
4376 &(options
->universes
[agent_universe
.index
]),
4377 (struct option_chain_head
*)
4378 packet
->options
->universes
[agent_universe
.index
], MDL
);
4380 if (options
->universe_count
<= agent_universe
.index
)
4381 options
->universe_count
= agent_universe
.index
+ 1;