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