ISC DHCP  4.4.1
A reference DHCPv4 and DHCPv6 implementation
dhcp.c
Go to the documentation of this file.
1 /* dhcp.c
2 
3  DHCP Protocol engine. */
4 
5 /*
6  * Copyright (c) 2004-2018 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 1995-2003 by Internet Software Consortium
8  *
9  * This Source Code Form is subject to the terms of the Mozilla Public
10  * License, v. 2.0. If a copy of the MPL was not distributed with this
11  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  *
21  * Internet Systems Consortium, Inc.
22  * 950 Charter Street
23  * Redwood City, CA 94063
24  * <info@isc.org>
25  * https://www.isc.org/
26  *
27  */
28 
29 #include "dhcpd.h"
30 #include <errno.h>
31 #include <limits.h>
32 #include <sys/time.h>
33 
34 static void maybe_return_agent_options(struct packet *packet,
35  struct option_state *options);
36 static int reuse_lease (struct packet* packet, struct lease* new_lease,
37  struct lease* lease, struct lease_state *state,
38  int offer);
39 #if defined(DHCPv6) && defined(DHCP4o6)
40 static int locate_network6(struct packet *packet);
41 #endif
42 
44 
45 #if defined(DELAYED_ACK)
46 static void delayed_ack_enqueue(struct lease *);
47 static void delayed_acks_timer(void *);
48 
49 
50 struct leasequeue *ackqueue_head, *ackqueue_tail;
51 static struct leasequeue *free_ackqueue;
52 static struct timeval max_fsync;
53 
54 int outstanding_acks;
58 int min_ack_delay_usecs = DEFAULT_MIN_ACK_DELAY_USECS;
59 #endif
60 
61 static char dhcp_message [256];
62 static int site_code_min;
63 
64 static int find_min_site_code(struct universe *);
65 static isc_result_t lowest_site_code(const void *, unsigned, void *);
66 
67 static const char *dhcp_type_names [] = {
68  "DHCPDISCOVER",
69  "DHCPOFFER",
70  "DHCPREQUEST",
71  "DHCPDECLINE",
72  "DHCPACK",
73  "DHCPNAK",
74  "DHCPRELEASE",
75  "DHCPINFORM",
76  "type 9",
77  "DHCPLEASEQUERY",
78  "DHCPLEASEUNASSIGNED",
79  "DHCPLEASEUNKNOWN",
80  "DHCPLEASEACTIVE"
81 };
82 const int dhcp_type_name_max = ((sizeof dhcp_type_names) / sizeof (char *));
83 
84 #if defined (TRACING)
85 # define send_packet trace_packet_send
86 #endif
87 
88 static TIME leaseTimeCheck(TIME calculated, TIME alternate);
89 
91  struct packet *packet;
92 {
93  struct option_cache *oc;
94  struct data_string client_identifier;
95  char *ci;
96 
97  memset (&client_identifier, 0, sizeof client_identifier);
98 
99  oc = lookup_option (&dhcp_universe, packet -> options,
101  if (oc &&
102  evaluate_option_cache (&client_identifier,
103  packet, (struct lease *)0,
104  (struct client_state *)0,
105  packet -> options,
106  (struct option_state *)0,
107  &global_scope, oc, MDL)) {
108  ci = print_hw_addr (HTYPE_INFINIBAND, client_identifier.len, client_identifier.data);
109  data_string_forget (&client_identifier, MDL);
110  return ci;
111  } else
112  return "\"no client id\"";
113 }
114 
116  struct packet *packet;
117 {
118  if (packet -> raw -> htype == HTYPE_INFINIBAND)
120  else
121  return print_hw_addr (packet -> raw -> htype,
122  packet -> raw -> hlen,
123  packet -> raw -> chaddr);
124 }
125 
126 void
127 dhcp (struct packet *packet) {
128  int ms_nulltp = 0;
129  struct option_cache *oc;
130  struct lease *lease = NULL;
131  const char *errmsg;
132  struct data_string data;
133 
134  if (!locate_network(packet) &&
138  const char *s;
139  char typebuf[32];
140  errmsg = "unknown network segment";
141  bad_packet:
142 
143  if (packet->packet_type > 0 &&
145  s = dhcp_type_names[packet->packet_type - 1];
146  } else {
147  /* %Audit% Cannot exceed 28 bytes. %2004.06.17,Safe% */
148  sprintf(typebuf, "type %d", packet->packet_type);
149  s = typebuf;
150  }
151 
152 #if defined(DHCPv6) && defined(DHCP4o6)
153  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
154  log_info("DHCP4o6 %s from %s via %s: %s", s,
155  (packet->raw->htype
157  packet->raw->hlen,
158  packet->raw->chaddr)
159  : "<no identifier>"),
161  errmsg);
162  goto out;
163  }
164 #endif
165 
166  log_info("%s from %s via %s: %s", s,
167  (packet->raw->htype
169  : "<no identifier>"),
170  packet->raw->giaddr.s_addr
171  ? inet_ntoa(packet->raw->giaddr)
172  : packet->interface->name, errmsg);
173  goto out;
174  }
175 
176  /* There is a problem with the relay agent information option,
177  * which is that in order for a normal relay agent to append
178  * this option, the relay agent has to have been involved in
179  * getting the packet from the client to the server. Note
180  * that this is the software entity known as the relay agent,
181  * _not_ the hardware entity known as a router in which the
182  * relay agent may be running, so the fact that a router has
183  * forwarded a packet does not mean that the relay agent in
184  * the router was involved.
185  *
186  * So when the client broadcasts (DHCPDISCOVER, or giaddr is set),
187  * we can be sure that there are either agent options in the
188  * packet, or there aren't supposed to be. When the giaddr is not
189  * set, it's still possible that the client is on a directly
190  * attached subnet, and agent options are being appended by an l2
191  * device that has no address, and so sets no giaddr.
192  *
193  * But in either case it's possible that the packets we receive
194  * from the client in RENEW state may not include the agent options,
195  * so if they are not in the packet we must "pretend" the last values
196  * we observed were provided.
197  */
198  if (packet->packet_type == DHCPREQUEST &&
199  packet->raw->ciaddr.s_addr && !packet->raw->giaddr.s_addr &&
202  {
203  struct iaddr cip;
204 
205  cip.len = sizeof packet -> raw -> ciaddr;
206  memcpy (cip.iabuf, &packet -> raw -> ciaddr,
207  sizeof packet -> raw -> ciaddr);
208  if (!find_lease_by_ip_addr (&lease, cip, MDL))
209  goto nolease;
210 
211  /* If there are no agent options on the lease, it's not
212  interesting. */
213  if (!lease -> agent_options)
214  goto nolease;
215 
216  /* The client should not be unicasting a renewal if its lease
217  has expired, so make it go through the process of getting
218  its agent options legally. */
219  if (lease -> ends < cur_time)
220  goto nolease;
221 
222  if (lease -> uid_len) {
223  oc = lookup_option (&dhcp_universe, packet -> options,
225  if (!oc)
226  goto nolease;
227 
228  memset (&data, 0, sizeof data);
229  if (!evaluate_option_cache (&data,
230  packet, (struct lease *)0,
231  (struct client_state *)0,
232  packet -> options,
233  (struct option_state *)0,
234  &global_scope, oc, MDL))
235  goto nolease;
236  if (lease -> uid_len != data.len ||
237  memcmp (lease -> uid, data.data, data.len)) {
238  data_string_forget (&data, MDL);
239  goto nolease;
240  }
241  data_string_forget (&data, MDL);
242  } else
243  if ((lease -> hardware_addr.hbuf [0] !=
244  packet -> raw -> htype) ||
245  (lease -> hardware_addr.hlen - 1 !=
246  packet -> raw -> hlen) ||
247  memcmp (&lease -> hardware_addr.hbuf [1],
248  packet -> raw -> chaddr,
249  packet -> raw -> hlen))
250  goto nolease;
251 
252  /* Okay, so we found a lease that matches the client. */
254  &(packet -> options -> universes
256  lease -> agent_options, MDL);
257 
260  agent_universe.index + 1;
261 
262  packet->agent_options_stashed = ISC_TRUE;
263  }
264  nolease:
265 
266  /* If a client null terminates options it sends, it probably
267  * expects the server to reciprocate.
268  */
269  if ((oc = lookup_option (&dhcp_universe, packet -> options,
270  DHO_HOST_NAME))) {
271  if (!oc -> expression)
272  ms_nulltp = oc->flags & OPTION_HAD_NULLS;
273  }
274 
275  /* Classify the client. */
277 
278  switch (packet -> packet_type) {
279  case DHCPDISCOVER:
280  dhcpdiscover (packet, ms_nulltp);
281  break;
282 
283  case DHCPREQUEST:
284  dhcprequest (packet, ms_nulltp, lease);
285  break;
286 
287  case DHCPRELEASE:
288  dhcprelease (packet, ms_nulltp);
289  break;
290 
291  case DHCPDECLINE:
292  dhcpdecline (packet, ms_nulltp);
293  break;
294 
295  case DHCPINFORM:
296  dhcpinform (packet, ms_nulltp);
297  break;
298 
299  case DHCPLEASEQUERY:
300  dhcpleasequery(packet, ms_nulltp);
301  break;
302 
303  case DHCPACK:
304  case DHCPOFFER:
305  case DHCPNAK:
306  case DHCPLEASEUNASSIGNED:
307  case DHCPLEASEUNKNOWN:
308  case DHCPLEASEACTIVE:
309  break;
310 
311  default:
312  errmsg = "unknown packet type";
313  goto bad_packet;
314  }
315  out:
316  if (lease)
317  lease_dereference (&lease, MDL);
318 }
319 
320 void dhcpdiscover (packet, ms_nulltp)
321  struct packet *packet;
322  int ms_nulltp;
323 {
324  struct lease *lease = (struct lease *)0;
325  char msgbuf [1024]; /* XXX */
326  TIME when;
327  const char *s;
328  int peer_has_leases = 0;
329 #if defined (FAILOVER_PROTOCOL)
330  dhcp_failover_state_t *peer;
331 #endif
332 
334  0, &peer_has_leases, (struct lease *)0, MDL);
335 
336  if (lease && lease -> client_hostname) {
337  if ((strlen (lease -> client_hostname) <= 64) &&
338  db_printable((unsigned char *)lease->client_hostname))
339  s = lease -> client_hostname;
340  else
341  s = "Hostname Unsuitable for Printing";
342  } else
343  s = (char *)0;
344 
345  /* %Audit% This is log output. %2004.06.17,Safe%
346  * If we truncate we hope the user can get a hint from the log.
347  */
348 #if defined(DHCPv6) && defined(DHCP4o6)
349  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
350  snprintf (msgbuf, sizeof msgbuf,
351  "DHCP4o6 DHCPDISCOVER from %s %s%s%svia %s",
352  (packet -> raw -> htype
353  ? print_hw_addr (packet -> raw -> htype,
354  packet -> raw -> hlen,
355  packet -> raw -> chaddr)
356  : (lease
357  ? print_hex_1(lease->uid_len, lease->uid, 60)
358  : "<no identifier>")),
359  s ? "(" : "", s ? s : "", s ? ") " : "",
361  } else
362 #endif
363  snprintf (msgbuf, sizeof msgbuf, "DHCPDISCOVER from %s %s%s%svia %s",
364  (packet -> raw -> htype
366  : (lease
367  ? print_hex_1(lease->uid_len, lease->uid, 60)
368  : "<no identifier>")),
369  s ? "(" : "", s ? s : "", s ? ") " : "",
370  packet -> raw -> giaddr.s_addr
371  ? inet_ntoa (packet -> raw -> giaddr)
372  : packet -> interface -> name);
373 
374  /* Sourceless packets don't make sense here. */
375  if (!packet -> shared_network) {
376 #if defined(DHCPv6) && defined(DHCP4o6)
377  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
378  log_info ("DHCP4o6 packet from unknown subnet: %s",
380  } else
381 #endif
382  log_info ("Packet from unknown subnet: %s",
383  inet_ntoa (packet -> raw -> giaddr));
384  goto out;
385  }
386 
387 #if defined (FAILOVER_PROTOCOL)
388  if (lease && lease -> pool && lease -> pool -> failover_peer) {
389  peer = lease -> pool -> failover_peer;
390 
391  /*
392  * If the lease is ours to (re)allocate, then allocate it.
393  *
394  * If the lease is active, it belongs to the client. This
395  * is the right lease, if we are to offer one. We decide
396  * whether or not to offer later on.
397  *
398  * If the lease was last active, and we've reached this
399  * point, then it was last active with the same client. We
400  * can safely re-activate the lease with this client.
401  */
402  if (lease->binding_state == FTS_ACTIVE ||
405  ; /* This space intentionally left blank. */
406 
407  /* Otherwise, we can't let the client have this lease. */
408  } else {
409 #if defined (DEBUG_FIND_LEASE)
410  log_debug ("discarding %s - %s",
411  piaddr (lease -> ip_addr),
413 #endif
414  lease_dereference (&lease, MDL);
415  }
416  }
417 #endif
418 
419  /* If we didn't find a lease, try to allocate one... */
420  if (!lease) {
421  if (!allocate_lease (&lease, packet,
422  packet -> shared_network -> pools,
423  &peer_has_leases)) {
424  if (peer_has_leases)
425  log_error ("%s: peer holds all free leases",
426  msgbuf);
427  else
428  log_error ("%s: network %s: no free leases",
429  msgbuf,
430  packet -> shared_network -> name);
431  return;
432  }
433  }
434 
435 #if defined (FAILOVER_PROTOCOL)
436  if (lease && lease -> pool && lease -> pool -> failover_peer) {
437  peer = lease -> pool -> failover_peer;
438  if (peer -> service_state == not_responding ||
439  peer -> service_state == service_startup) {
440  log_info ("%s: not responding%s",
441  msgbuf, peer -> nrr);
442  goto out;
443  }
444  } else
445  peer = (dhcp_failover_state_t *)0;
446 
447  /* Do load balancing if configured. */
448  if (peer && (peer -> service_state == cooperating) &&
449  !load_balance_mine (packet, peer)) {
450  if (peer_has_leases) {
451  log_debug ("%s: load balance to peer %s",
452  msgbuf, peer -> name);
453  goto out;
454  } else {
455  log_debug ("%s: cancel load balance to peer %s - %s",
456  msgbuf, peer -> name, "no free leases");
457  }
458  }
459 #endif
460 
461  /* If it's an expired lease, get rid of any bindings. */
462  if (lease -> ends < cur_time && lease -> scope)
464 
465  /* Set the lease to really expire in 2 minutes, unless it has
466  not yet expired, in which case leave its expiry time alone. */
467  when = cur_time + 120;
468  if (when < lease -> ends)
469  when = lease -> ends;
470 
471  ack_lease (packet, lease, DHCPOFFER, when, msgbuf, ms_nulltp,
472  (struct host_decl *)0);
473  out:
474  if (lease)
475  lease_dereference (&lease, MDL);
476 }
477 
478 void dhcprequest (packet, ms_nulltp, ip_lease)
479  struct packet *packet;
480  int ms_nulltp;
481  struct lease *ip_lease;
482 {
483  struct lease *lease;
484  struct iaddr cip;
485  struct iaddr sip;
486  struct subnet *subnet;
487  int ours = 0;
488  struct option_cache *oc;
489  struct data_string data;
490  char msgbuf [1024]; /* XXX */
491  const char *s;
492  char smbuf [19];
493 #if defined (FAILOVER_PROTOCOL)
494  dhcp_failover_state_t *peer;
495 #endif
496  int have_requested_addr = 0;
497 
498  oc = lookup_option (&dhcp_universe, packet -> options,
500  memset (&data, 0, sizeof data);
501  if (oc &&
502  evaluate_option_cache (&data, packet, (struct lease *)0,
503  (struct client_state *)0,
504  packet -> options, (struct option_state *)0,
505  &global_scope, oc, MDL)) {
506  cip.len = 4;
507  memcpy (cip.iabuf, data.data, 4);
509  have_requested_addr = 1;
510  } else {
511  oc = (struct option_cache *)0;
512  cip.len = 4;
513  memcpy (cip.iabuf, &packet -> raw -> ciaddr.s_addr, 4);
514  }
515 
516  /* Find the lease that matches the address requested by the
517  client. */
518 
519  subnet = (struct subnet *)0;
520  lease = (struct lease *)0;
521  if (find_subnet (&subnet, cip, MDL))
523  subnet -> shared_network, &ours, 0, ip_lease, MDL);
524 
525  if (lease && lease -> client_hostname) {
526  if ((strlen (lease -> client_hostname) <= 64) &&
527  db_printable((unsigned char *)lease->client_hostname))
528  s = lease -> client_hostname;
529  else
530  s = "Hostname Unsuitable for Printing";
531  } else
532  s = (char *)0;
533 
534  oc = lookup_option (&dhcp_universe, packet -> options,
536  memset (&data, 0, sizeof data);
537  if (oc &&
538  evaluate_option_cache (&data, packet, (struct lease *)0,
539  (struct client_state *)0,
540  packet -> options, (struct option_state *)0,
541  &global_scope, oc, MDL)) {
542  sip.len = 4;
543  memcpy (sip.iabuf, data.data, 4);
544  data_string_forget (&data, MDL);
545  /* piaddr() should not return more than a 15 byte string.
546  * safe.
547  */
548  sprintf (smbuf, " (%s)", piaddr (sip));
549  } else {
550  smbuf [0] = 0;
551  sip.len = 0;
552  }
553 
554  /* %Audit% This is log output. %2004.06.17,Safe%
555  * If we truncate we hope the user can get a hint from the log.
556  */
557 #if defined(DHCPv6) && defined(DHCP4o6)
558  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
559  snprintf (msgbuf, sizeof msgbuf,
560  "DHCP4o6 DHCPREQUEST for %s%s from %s %s%s%svia %s",
561  piaddr (cip), smbuf,
562  (packet -> raw -> htype
563  ? print_hw_addr (packet -> raw -> htype,
564  packet -> raw -> hlen,
565  packet -> raw -> chaddr)
566  : (lease
567  ? print_hex_1(lease->uid_len, lease->uid, 60)
568  : "<no identifier>")),
569  s ? "(" : "", s ? s : "", s ? ") " : "",
571  } else
572 #endif
573  snprintf (msgbuf, sizeof msgbuf,
574  "DHCPREQUEST for %s%s from %s %s%s%svia %s",
575  piaddr (cip), smbuf,
576  (packet -> raw -> htype
578  : (lease
579  ? print_hex_1(lease->uid_len, lease->uid, 60)
580  : "<no identifier>")),
581  s ? "(" : "", s ? s : "", s ? ") " : "",
582  packet -> raw -> giaddr.s_addr
583  ? inet_ntoa (packet -> raw -> giaddr)
584  : packet -> interface -> name);
585 
586 #if defined (FAILOVER_PROTOCOL)
587  if (lease && lease -> pool && lease -> pool -> failover_peer) {
588  peer = lease -> pool -> failover_peer;
589  if (peer -> service_state == not_responding ||
590  peer -> service_state == service_startup) {
591  log_info ("%s: not responding%s",
592  msgbuf, peer -> nrr);
593  goto out;
594  }
595 
596  /* "load balance to peer" - is not done at all for request.
597  *
598  * If it's RENEWING, we are the only server to hear it, so
599  * we have to serve it. If it's REBINDING, it's out of
600  * communication with the other server, so there's no point
601  * in waiting to serve it. However, if the lease we're
602  * offering is not a free lease, then we may be the only
603  * server that can offer it, so we can't load balance if
604  * the lease isn't in the free or backup state. If it is
605  * in the free or backup state, then that state is what
606  * mandates one server or the other should perform the
607  * allocation, not the LBA...we know the peer cannot
608  * allocate a request for an address in our free state.
609  *
610  * So our only compass is lease_mine_to_reallocate(). This
611  * effects both load balancing, and a sanity-check that we
612  * are not going to try to allocate a lease that isn't ours.
613  */
614  if ((lease -> binding_state == FTS_FREE ||
615  lease -> binding_state == FTS_BACKUP) &&
617  log_debug ("%s: lease owned by peer", msgbuf);
618  goto out;
619  }
620 
621  /*
622  * If the lease is in a transitional state, we can't
623  * renew it unless we can rewind it to a non-transitional
624  * state (active, free, or backup). lease_mine_to_reallocate()
625  * checks for free/backup, so we only need to check for active.
626  */
627  if ((lease->binding_state == FTS_RELEASED ||
631  log_debug("%s: lease in transition state %s", msgbuf,
633  ? "released" : "expired");
634  goto out;
635  }
636 
637  /* It's actually very unlikely that we'll ever get here,
638  but if we do, tell the client to stop using the lease,
639  because the administrator reset it. */
640  if (lease -> binding_state == FTS_RESET &&
642  log_debug ("%s: lease reset by administrator", msgbuf);
643  nak_lease (packet, &cip, lease->subnet->group);
644  goto out;
645  }
646 
647  /* If server-id-check is enabled, verify that the client's
648  * server source address (sip from incoming packet) is ours.
649  * To avoid problems with confused clients we do some sanity
650  * checks to verify sip's length and that it isn't all zeros.
651  * We then get the server id we would likely use for this
652  * packet and compare them. If they don't match it we assume
653  * we didn't send the offer and so we don't process the
654  * request. */
655  if ((server_id_check == 1) && (sip.len == 4) &&
656  (memcmp(sip.iabuf, "\0\0\0\0", sip.len) != 0)) {
657  struct in_addr from;
658  struct option_state *eval_options = NULL;
659 
660  eval_network_statements(&eval_options, packet, NULL);
661  get_server_source_address(&from, eval_options,
662  NULL, packet);
663  option_state_dereference (&eval_options, MDL);
664  if (memcmp(sip.iabuf, &from, sip.len) != 0) {
665  log_debug("%s: not our server id", msgbuf);
666  goto out;
667  }
668  }
669 
670  /* At this point it's possible that we will get a broadcast
671  DHCPREQUEST for a lease that we didn't offer, because
672  both we and the peer are in a position to offer it.
673  In that case, we probably shouldn't answer. In order
674  to not answer, we would have to compare the server
675  identifier sent by the client with the list of possible
676  server identifiers we can send, and if the client's
677  identifier isn't on the list, drop the DHCPREQUEST.
678  We aren't currently doing that for two reasons - first,
679  it's not clear that all clients do the right thing
680  with respect to sending the client identifier, which
681  could mean that we might simply not respond to a client
682  that is depending on us to respond. Secondly, we allow
683  the user to specify the server identifier to send, and
684  we don't enforce that the server identifier should be
685  one of our IP addresses. This is probably not a big
686  deal, but it's theoretically an issue.
687 
688  The reason we care about this is that if both servers
689  send a DHCPACK to the DHCPREQUEST, they are then going
690  to send dueling BNDUPD messages, which could cause
691  trouble. I think it causes no harm, but it seems
692  wrong. */
693  } else
694  peer = (dhcp_failover_state_t *)0;
695 #endif
696 
697  /* If a client on a given network REQUESTs a lease on an
698  address on a different network, NAK it. If the Requested
699  Address option was used, the protocol says that it must
700  have been broadcast, so we can trust the source network
701  information.
702 
703  If ciaddr was specified and Requested Address was not, then
704  we really only know for sure what network a packet came from
705  if it came through a BOOTP gateway - if it came through an
706  IP router, we'll just have to assume that it's cool.
707 
708  If we don't think we know where the packet came from, it
709  came through a gateway from an unknown network, so it's not
710  from a RENEWING client. If we recognize the network it
711  *thinks* it's on, we can NAK it even though we don't
712  recognize the network it's *actually* on; otherwise we just
713  have to ignore it.
714 
715  We don't currently try to take advantage of access to the
716  raw packet, because it's not available on all platforms.
717  So a packet that was unicast to us through a router from a
718  RENEWING client is going to look exactly like a packet that
719  was broadcast to us from an INIT-REBOOT client.
720 
721  Since we can't tell the difference between these two kinds
722  of packets, if the packet appears to have come in off the
723  local wire, we have to treat it as if it's a RENEWING
724  client. This means that we can't NAK a RENEWING client on
725  the local wire that has a bogus address. The good news is
726  that we won't ACK it either, so it should revert to INIT
727  state and send us a DHCPDISCOVER, which we *can* work with.
728 
729  Because we can't detect that a RENEWING client is on the
730  wrong wire, it's going to sit there trying to renew until
731  it gets to the REBIND state, when we *can* NAK it because
732  the packet will get to us through a BOOTP gateway. We
733  shouldn't actually see DHCPREQUEST packets from RENEWING
734  clients on the wrong wire anyway, since their idea of their
735  local router will be wrong. In any case, the protocol
736  doesn't really allow us to NAK a DHCPREQUEST from a
737  RENEWING client, so we can punt on this issue. */
738 
739  if (!packet -> shared_network ||
740  (packet -> raw -> ciaddr.s_addr &&
741  packet -> raw -> giaddr.s_addr) ||
742  (have_requested_addr && !packet -> raw -> ciaddr.s_addr)) {
743 
744  /* If we don't know where it came from but we do know
745  where it claims to have come from, it didn't come
746  from there. */
747  if (!packet -> shared_network) {
748  if (subnet && subnet -> group -> authoritative) {
749  log_info ("%s: wrong network.", msgbuf);
750  nak_lease (packet, &cip, NULL);
751  goto out;
752  }
753  /* Otherwise, ignore it. */
754  log_info ("%s: ignored (%s).", msgbuf,
755  (subnet
756  ? "not authoritative" : "unknown subnet"));
757  goto out;
758  }
759 
760  /* If we do know where it came from and it asked for an
761  address that is not on that shared network, nak it. */
762  if (subnet)
763  subnet_dereference (&subnet, MDL);
765  cip, MDL)) {
766  if (packet -> shared_network -> group -> authoritative)
767  {
768  log_info ("%s: wrong network.", msgbuf);
769  nak_lease (packet, &cip, NULL);
770  goto out;
771  }
772  log_info ("%s: ignored (not authoritative).", msgbuf);
773  return;
774  }
775  }
776 
777  /* If the address the client asked for is ours, but it wasn't
778  available for the client, NAK it. */
779  if (!lease && ours) {
780  log_info ("%s: lease %s unavailable.", msgbuf, piaddr (cip));
781  nak_lease (packet, &cip, (subnet ? subnet->group : NULL));
782  goto out;
783  }
784 
785  /* Otherwise, send the lease to the client if we found one. */
786  if (lease) {
787  ack_lease (packet, lease, DHCPACK, 0, msgbuf, ms_nulltp,
788  (struct host_decl *)0);
789  } else
790  log_info ("%s: unknown lease %s.", msgbuf, piaddr (cip));
791 
792  out:
793  if (subnet)
794  subnet_dereference (&subnet, MDL);
795  if (lease)
796  lease_dereference (&lease, MDL);
797  return;
798 }
799 
800 void dhcprelease (packet, ms_nulltp)
801  struct packet *packet;
802  int ms_nulltp;
803 {
804  struct lease *lease = (struct lease *)0, *next = (struct lease *)0;
805  struct iaddr cip;
806  struct option_cache *oc;
807  struct data_string data;
808  const char *s;
809  char msgbuf [1024], cstr[16]; /* XXX */
810 
811 
812  /* DHCPRELEASE must not specify address in requested-address
813  option, but old protocol specs weren't explicit about this,
814  so let it go. */
815  if ((oc = lookup_option (&dhcp_universe, packet -> options,
817  log_info ("DHCPRELEASE from %s specified requested-address.",
819  }
820 
821  oc = lookup_option (&dhcp_universe, packet -> options,
823  memset (&data, 0, sizeof data);
824  if (oc &&
825  evaluate_option_cache (&data, packet, (struct lease *)0,
826  (struct client_state *)0,
827  packet -> options, (struct option_state *)0,
828  &global_scope, oc, MDL)) {
829  find_lease_by_uid (&lease, data.data, data.len, MDL);
831 
832  /* See if we can find a lease that matches the IP address
833  the client is claiming. */
834  while (lease) {
835  if (lease -> n_uid)
836  lease_reference (&next, lease -> n_uid, MDL);
837  if (!memcmp (&packet -> raw -> ciaddr,
838  lease -> ip_addr.iabuf, 4)) {
839  break;
840  }
841  lease_dereference (&lease, MDL);
842  if (next) {
843  lease_reference (&lease, next, MDL);
844  lease_dereference (&next, MDL);
845  }
846  }
847  if (next)
848  lease_dereference (&next, MDL);
849  }
850 
851  /* The client is supposed to pass a valid client-identifier,
852  but the spec on this has changed historically, so try the
853  IP address in ciaddr if the client-identifier fails. */
854  if (!lease) {
855  cip.len = 4;
856  memcpy (cip.iabuf, &packet -> raw -> ciaddr, 4);
858  }
859 
860 
861  /* If the hardware address doesn't match, don't do the release. */
862  if (lease &&
863  (lease -> hardware_addr.hlen != packet -> raw -> hlen + 1 ||
864  lease -> hardware_addr.hbuf [0] != packet -> raw -> htype ||
865  memcmp (&lease -> hardware_addr.hbuf [1],
866  packet -> raw -> chaddr, packet -> raw -> hlen)))
867  lease_dereference (&lease, MDL);
868 
869  if (lease && lease -> client_hostname) {
870  if ((strlen (lease -> client_hostname) <= 64) &&
871  db_printable((unsigned char *)lease->client_hostname))
872  s = lease -> client_hostname;
873  else
874  s = "Hostname Unsuitable for Printing";
875  } else
876  s = (char *)0;
877 
878  /* %Audit% Cannot exceed 16 bytes. %2004.06.17,Safe%
879  * We copy this out to stack because we actually want to log two
880  * inet_ntoa()'s in this message.
881  */
882  strncpy(cstr, inet_ntoa (packet -> raw -> ciaddr), 15);
883  cstr[15] = '\0';
884 
885  /* %Audit% This is log output. %2004.06.17,Safe%
886  * If we truncate we hope the user can get a hint from the log.
887  */
888 #if defined(DHCPv6) && defined(DHCP4o6)
889  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
890  snprintf (msgbuf, sizeof msgbuf,
891  "DHCP4o6 DHCPRELEASE of %s from %s %s%s%svia "
892  "%s (%sfound)",
893  cstr,
894  (packet -> raw -> htype
895  ? print_hw_addr (packet -> raw -> htype,
896  packet -> raw -> hlen,
897  packet -> raw -> chaddr)
898  : (lease
899  ? print_hex_1(lease->uid_len, lease->uid, 60)
900  : "<no identifier>")),
901  s ? "(" : "", s ? s : "", s ? ") " : "",
903  lease ? "" : "not ");
904  } else
905 #endif
906  snprintf (msgbuf, sizeof msgbuf,
907  "DHCPRELEASE of %s from %s %s%s%svia %s (%sfound)",
908  cstr,
909  (packet -> raw -> htype
911  : (lease
912  ? print_hex_1(lease->uid_len, lease->uid, 60)
913  : "<no identifier>")),
914  s ? "(" : "", s ? s : "", s ? ") " : "",
915  packet -> raw -> giaddr.s_addr
916  ? inet_ntoa (packet -> raw -> giaddr)
917  : packet -> interface -> name,
918  lease ? "" : "not ");
919 
920 #if defined (FAILOVER_PROTOCOL)
921  if (lease && lease -> pool && lease -> pool -> failover_peer) {
922  dhcp_failover_state_t *peer = lease -> pool -> failover_peer;
923  if (peer -> service_state == not_responding ||
924  peer -> service_state == service_startup) {
925  log_info ("%s: ignored%s",
926  peer -> name, peer -> nrr);
927  goto out;
928  }
929 
930  /* DHCPRELEASE messages are unicast, so if the client
931  sent the DHCPRELEASE to us, it's not going to send it
932  to the peer. Not sure why this would happen, and
933  if it does happen I think we still have to change the
934  lease state, so that's what we're doing.
935  XXX See what it says in the draft about this. */
936  }
937 #endif
938 
939  /* If we found a lease, release it. */
940  if (lease && lease -> ends > cur_time) {
942  }
943  log_info ("%s", msgbuf);
944 #if defined(FAILOVER_PROTOCOL)
945  out:
946 #endif
947  if (lease)
948  lease_dereference (&lease, MDL);
949 }
950 
951 void dhcpdecline (packet, ms_nulltp)
952  struct packet *packet;
953  int ms_nulltp;
954 {
955  struct lease *lease = (struct lease *)0;
956  struct option_state *options = (struct option_state *)0;
957  int ignorep = 0;
958  int i;
959  const char *status;
960  const char *s;
961  char msgbuf [1024]; /* XXX */
962  struct iaddr cip;
963  struct option_cache *oc;
964  struct data_string data;
965 
966  /* DHCPDECLINE must specify address. */
967  if (!(oc = lookup_option (&dhcp_universe, packet -> options,
969  return;
970  memset (&data, 0, sizeof data);
971  if (!evaluate_option_cache (&data, packet, (struct lease *)0,
972  (struct client_state *)0,
973  packet -> options,
974  (struct option_state *)0,
975  &global_scope, oc, MDL))
976  return;
977 
978  cip.len = 4;
979  memcpy (cip.iabuf, data.data, 4);
982 
983  if (lease && lease -> client_hostname) {
984  if ((strlen (lease -> client_hostname) <= 64) &&
985  db_printable((unsigned char *)lease->client_hostname))
986  s = lease -> client_hostname;
987  else
988  s = "Hostname Unsuitable for Printing";
989  } else
990  s = (char *)0;
991 
992  /* %Audit% This is log output. %2004.06.17,Safe%
993  * If we truncate we hope the user can get a hint from the log.
994  */
995 #if defined(DHCPv6) && defined(DHCP4o6)
996  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
997  snprintf (msgbuf, sizeof msgbuf,
998  "DHCP4o6 DHCPDECLINE of %s from %s %s%s%svia %s",
999  piaddr (cip),
1000  (packet -> raw -> htype
1001  ? print_hw_addr (packet -> raw -> htype,
1002  packet -> raw -> hlen,
1003  packet -> raw -> chaddr)
1004  : (lease
1005  ? print_hex_1(lease->uid_len, lease->uid, 60)
1006  : "<no identifier>")),
1007  s ? "(" : "", s ? s : "", s ? ") " : "",
1009  } else
1010 #endif
1011  snprintf (msgbuf, sizeof msgbuf,
1012  "DHCPDECLINE of %s from %s %s%s%svia %s",
1013  piaddr (cip),
1014  (packet -> raw -> htype
1016  : (lease
1017  ? print_hex_1(lease->uid_len, lease->uid, 60)
1018  : "<no identifier>")),
1019  s ? "(" : "", s ? s : "", s ? ") " : "",
1020  packet -> raw -> giaddr.s_addr
1021  ? inet_ntoa (packet -> raw -> giaddr)
1022  : packet -> interface -> name);
1023 
1024  option_state_allocate (&options, MDL);
1025 
1026  /* Execute statements in scope starting with the subnet scope. */
1027  if (lease)
1028  execute_statements_in_scope(NULL, packet, NULL, NULL,
1029  packet->options, options,
1030  &global_scope,
1031  lease->subnet->group,
1032  NULL, NULL);
1033 
1034  /* Execute statements in the class scopes. */
1035  for (i = packet -> class_count; i > 0; i--) {
1037  (NULL, packet, NULL, NULL, packet->options, options,
1038  &global_scope, packet->classes[i - 1]->group,
1039  lease ? lease->subnet->group : NULL, NULL);
1040  }
1041 
1042  /* Drop the request if dhcpdeclines are being ignored. */
1043  oc = lookup_option (&server_universe, options, SV_DECLINES);
1044  if (!oc ||
1046  (struct client_state *)0,
1047  packet -> options, options,
1048  &lease -> scope, oc, MDL)) {
1049  /* If we found a lease, mark it as unusable and complain. */
1050  if (lease) {
1051 #if defined (FAILOVER_PROTOCOL)
1052  if (lease -> pool && lease -> pool -> failover_peer) {
1053  dhcp_failover_state_t *peer =
1054  lease -> pool -> failover_peer;
1055  if (peer -> service_state == not_responding ||
1056  peer -> service_state == service_startup) {
1057  if (!ignorep)
1058  log_info ("%s: ignored%s",
1059  peer -> name, peer -> nrr);
1060  goto out;
1061  }
1062 
1063  /* DHCPDECLINE messages are broadcast, so we can safely
1064  ignore the DHCPDECLINE if the peer has the lease.
1065  XXX Of course, at this point that information has been
1066  lost. */
1067  }
1068 #endif
1069 
1070  abandon_lease (lease, "declined.");
1071  status = "abandoned";
1072  } else {
1073  status = "not found";
1074  }
1075  } else
1076  status = "ignored";
1077 
1078  if (!ignorep)
1079  log_info ("%s: %s", msgbuf, status);
1080 
1081 #if defined(FAILOVER_PROTOCOL)
1082  out:
1083 #endif
1084  if (options)
1085  option_state_dereference (&options, MDL);
1086  if (lease)
1087  lease_dereference (&lease, MDL);
1088 }
1089 
1090 #if defined(RELAY_PORT)
1091 u_int16_t dhcp_check_relayport(packet)
1092  struct packet *packet;
1093 {
1095  packet->options,
1096  RAI_RELAY_PORT) != NULL) {
1097  return (packet->client_port);
1098  }
1099 
1100  return (0);
1101 }
1102 #endif
1103 
1104 void dhcpinform (packet, ms_nulltp)
1105  struct packet *packet;
1106  int ms_nulltp;
1107 {
1108  char msgbuf[1024], *addr_type;
1109  struct data_string d1, prl, fixed_addr;
1110  struct option_cache *oc;
1111  struct option_state *options = NULL;
1112  struct dhcp_packet raw;
1113  struct packet outgoing;
1114  unsigned char dhcpack = DHCPACK;
1115  struct subnet *subnet = NULL;
1116  struct iaddr cip, gip, sip;
1117  unsigned i;
1118  int nulltp;
1119  struct sockaddr_in to;
1120  struct in_addr from;
1121  isc_boolean_t zeroed_ciaddr;
1122  struct interface_info *interface;
1123  int result, h_m_client_ip = 0;
1124  struct host_decl *host = NULL, *hp = NULL, *h;
1125 #if defined(RELAY_PORT)
1126  u_int16_t relay_port = 0;
1127 #endif
1128 #if defined (DEBUG_INFORM_HOST)
1129  int h_w_fixed_addr = 0;
1130 #endif
1131 
1132  /* The client should set ciaddr to its IP address, but apparently
1133  it's common for clients not to do this, so we'll use their IP
1134  source address if they didn't set ciaddr. */
1135  if (!packet->raw->ciaddr.s_addr) {
1136  zeroed_ciaddr = ISC_TRUE;
1137  /* With DHCPv4-over-DHCPv6 it can be an IPv6 address
1138  so we check its length. */
1139  if (packet->client_addr.len == 4) {
1140  cip.len = 4;
1141  memcpy(cip.iabuf, &packet->client_addr.iabuf, 4);
1142  addr_type = "source";
1143  } else {
1144  cip.len = 0;
1145  memset(cip.iabuf, 0, 4);
1146  addr_type = "v4o6";
1147  }
1148  } else {
1149  zeroed_ciaddr = ISC_FALSE;
1150  cip.len = 4;
1151  memcpy(cip.iabuf, &packet->raw->ciaddr, 4);
1152  addr_type = "client";
1153  }
1154  sip.len = 4;
1155  memcpy(sip.iabuf, cip.iabuf, 4);
1156 
1157  if (packet->raw->giaddr.s_addr) {
1158  gip.len = 4;
1159  memcpy(gip.iabuf, &packet->raw->giaddr, 4);
1160  if (zeroed_ciaddr == ISC_TRUE) {
1161  addr_type = "relay";
1162  memcpy(sip.iabuf, gip.iabuf, 4);
1163  }
1164  } else
1165  gip.len = 0;
1166 
1167  /* %Audit% This is log output. %2004.06.17,Safe%
1168  * If we truncate we hope the user can get a hint from the log.
1169  */
1170 #if defined(DHCPv6) && defined(DHCP4o6)
1171  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
1172  snprintf(msgbuf, sizeof(msgbuf),
1173  "DHCP4o6 DHCPINFORM from %s via %s",
1174  piaddr(cip),
1176  } else
1177 #endif
1178  snprintf(msgbuf, sizeof(msgbuf), "DHCPINFORM from %s via %s",
1179  piaddr(cip),
1180  packet->raw->giaddr.s_addr ?
1181  inet_ntoa(packet->raw->giaddr) :
1182  packet->interface->name);
1183 
1184  /* If the IP source address is zero, don't respond. */
1185  if (!memcmp(cip.iabuf, "\0\0\0", 4)) {
1186  log_info("%s: ignored (null source address).", msgbuf);
1187  return;
1188  }
1189 
1190 #if defined(RELAY_PORT)
1192 #endif
1193 
1194  /* Find the subnet that the client is on.
1195  * CC: Do the link selection / subnet selection
1196  */
1197 
1198  option_state_allocate(&options, MDL);
1199 
1201  RAI_LINK_SELECT)) == NULL)
1204 
1205  memset(&d1, 0, sizeof d1);
1206  if (oc && evaluate_option_cache(&d1, packet, NULL, NULL,
1207  packet->options, NULL,
1208  &global_scope, oc, MDL)) {
1209  struct option_cache *noc = NULL;
1210 
1211  if (d1.len != 4) {
1212  log_info("%s: ignored (invalid subnet selection option).", msgbuf);
1213  option_state_dereference(&options, MDL);
1214  return;
1215  }
1216 
1217  memcpy(sip.iabuf, d1.data, 4);
1218  data_string_forget(&d1, MDL);
1219 
1220  /* Make a copy of the data. */
1221  if (option_cache_allocate(&noc, MDL)) {
1222  if (oc->data.len)
1223  data_string_copy(&noc->data, &oc->data, MDL);
1224  if (oc->expression)
1226  oc->expression, MDL);
1227  if (oc->option)
1228  option_reference(&(noc->option), oc->option,
1229  MDL);
1230  }
1231  save_option(&dhcp_universe, options, noc);
1233 
1234  if ((zeroed_ciaddr == ISC_TRUE) && (gip.len != 0))
1235  addr_type = "relay link select";
1236  else
1237  addr_type = "selected";
1238  }
1239 
1240  find_subnet(&subnet, sip, MDL);
1241 
1242  if (subnet == NULL) {
1243  log_info("%s: unknown subnet for %s address %s",
1244  msgbuf, addr_type, piaddr(sip));
1245  option_state_dereference(&options, MDL);
1246  return;
1247  }
1248 
1249  /* We don't respond to DHCPINFORM packets if we're not authoritative.
1250  It would be nice if a per-host value could override this, but
1251  there's overhead involved in checking this, so let's see how people
1252  react first. */
1253  if (!subnet->group->authoritative) {
1254  static int eso = 0;
1255  log_info("%s: not authoritative for subnet %s",
1256  msgbuf, piaddr (subnet -> net));
1257  if (!eso) {
1258  log_info("If this DHCP server is authoritative for%s",
1259  " that subnet,");
1260  log_info("please write an `authoritative;' directi%s",
1261  "ve either in the");
1262  log_info("subnet declaration or in some scope that%s",
1263  " encloses the");
1264  log_info("subnet declaration - for example, write %s",
1265  "it at the top");
1266  log_info("of the dhcpd.conf file.");
1267  }
1268  if (eso++ == 100)
1269  eso = 0;
1270  subnet_dereference(&subnet, MDL);
1271  option_state_dereference(&options, MDL);
1272  return;
1273  }
1274 
1275  memset(&outgoing, 0, sizeof outgoing);
1276  memset(&raw, 0, sizeof raw);
1277  outgoing.raw = &raw;
1278 
1279  maybe_return_agent_options(packet, options);
1280 
1281  /* Execute statements network statements starting at the subnet level */
1282  execute_statements_in_scope(NULL, packet, NULL, NULL,
1283  packet->options, options,
1285  NULL, NULL);
1286 
1287  /* If we have ciaddr, find its lease so we can find its pool. */
1288  if (zeroed_ciaddr == ISC_FALSE) {
1289  struct lease* cip_lease = NULL;
1290 
1291  find_lease_by_ip_addr (&cip_lease, cip, MDL);
1292 
1293  /* Overlay with pool options if ciaddr mapped to a lease. */
1294  if (cip_lease) {
1295  if (cip_lease->pool && cip_lease->pool->group) {
1297  NULL, packet, NULL, NULL,
1298  packet->options, options,
1299  &global_scope,
1300  cip_lease->pool->group,
1301  cip_lease->pool->shared_network->group,
1302  NULL);
1303  }
1304 
1305  lease_dereference (&cip_lease, MDL);
1306  }
1307  }
1308 
1309  /* Execute statements in the class scopes. */
1310  for (i = packet->class_count; i > 0; i--) {
1311  execute_statements_in_scope(NULL, packet, NULL, NULL,
1312  packet->options, options,
1313  &global_scope,
1314  packet->classes[i - 1]->group,
1315  subnet->group,
1316  NULL);
1317  }
1318 
1319  /*
1320  * Process host declarations during DHCPINFORM,
1321  * Try to find a matching host declaration by cli ID or HW addr.
1322  *
1323  * Look through the host decls for one that matches the
1324  * client identifer or the hardware address. The preference
1325  * order is:
1326  * client id with matching ip address
1327  * hardware address with matching ip address
1328  * client id without a ip fixed address
1329  * hardware address without a fixed ip address
1330  * If found, set host to use its option definitions.
1331  */
1334  memset(&d1, 0, sizeof(d1));
1335  if (oc &&
1336  evaluate_option_cache(&d1, packet, NULL, NULL,
1337  packet->options, NULL,
1338  &global_scope, oc, MDL)) {
1339  find_hosts_by_uid(&hp, d1.data, d1.len, MDL);
1340  data_string_forget(&d1, MDL);
1341 
1342 #if defined (DEBUG_INFORM_HOST)
1343  if (hp)
1344  log_debug ("dhcpinform: found host by ID "
1345  "-- checking fixed-address match");
1346 #endif
1347  /* check if we have one with fixed-address
1348  * matching the client ip first */
1349  for (h = hp; !h_m_client_ip && h; h = h->n_ipaddr) {
1350  if (!h->fixed_addr)
1351  continue;
1352 
1353  memset(&fixed_addr, 0, sizeof(fixed_addr));
1354  if (!evaluate_option_cache (&fixed_addr, NULL,
1355  NULL, NULL, NULL, NULL,
1356  &global_scope,
1357  h->fixed_addr, MDL))
1358  continue;
1359 
1360 #if defined (DEBUG_INFORM_HOST)
1361  h_w_fixed_addr++;
1362 #endif
1363  for (i = 0;
1364  (i + cip.len) <= fixed_addr.len;
1365  i += cip.len) {
1366  if (memcmp(fixed_addr.data + i,
1367  cip.iabuf, cip.len) == 0) {
1368 #if defined (DEBUG_INFORM_HOST)
1369  log_debug ("dhcpinform: found "
1370  "host with matching "
1371  "fixed-address by ID");
1372 #endif
1373  host_reference(&host, h, MDL);
1374  h_m_client_ip = 1;
1375  break;
1376  }
1377  }
1378  data_string_forget(&fixed_addr, MDL);
1379  }
1380 
1381  /* fallback to a host without fixed-address */
1382  for (h = hp; !host && h; h = h->n_ipaddr) {
1383  if (h->fixed_addr)
1384  continue;
1385 
1386 #if defined (DEBUG_INFORM_HOST)
1387  log_debug ("dhcpinform: found host "
1388  "without fixed-address by ID");
1389 #endif
1390  host_reference(&host, h, MDL);
1391  break;
1392  }
1393  if (hp)
1394  host_dereference (&hp, MDL);
1395  }
1396  if (!host || !h_m_client_ip) {
1398  packet->raw->chaddr,
1399  packet->raw->hlen, MDL);
1400 
1401 #if defined (DEBUG_INFORM_HOST)
1402  if (hp)
1403  log_debug ("dhcpinform: found host by HW "
1404  "-- checking fixed-address match");
1405 #endif
1406 
1407  /* check if we have one with fixed-address
1408  * matching the client ip first */
1409  for (h = hp; !h_m_client_ip && h; h = h->n_ipaddr) {
1410  if (!h->fixed_addr)
1411  continue;
1412 
1413  memset (&fixed_addr, 0, sizeof(fixed_addr));
1414  if (!evaluate_option_cache (&fixed_addr, NULL,
1415  NULL, NULL, NULL, NULL,
1416  &global_scope,
1417  h->fixed_addr, MDL))
1418  continue;
1419 
1420 #if defined (DEBUG_INFORM_HOST)
1421  h_w_fixed_addr++;
1422 #endif
1423  for (i = 0;
1424  (i + cip.len) <= fixed_addr.len;
1425  i += cip.len) {
1426  if (memcmp(fixed_addr.data + i,
1427  cip.iabuf, cip.len) == 0) {
1428 #if defined (DEBUG_INFORM_HOST)
1429  log_debug ("dhcpinform: found "
1430  "host with matching "
1431  "fixed-address by HW");
1432 #endif
1433  /*
1434  * Hmm.. we've found one
1435  * without IP by ID and now
1436  * (better) one with IP by HW.
1437  */
1438  if(host)
1439  host_dereference(&host, MDL);
1440  host_reference(&host, h, MDL);
1441  h_m_client_ip = 1;
1442  break;
1443  }
1444  }
1445  data_string_forget(&fixed_addr, MDL);
1446  }
1447  /* fallback to a host without fixed-address */
1448  for (h = hp; !host && h; h = h->n_ipaddr) {
1449  if (h->fixed_addr)
1450  continue;
1451 
1452 #if defined (DEBUG_INFORM_HOST)
1453  log_debug ("dhcpinform: found host without "
1454  "fixed-address by HW");
1455 #endif
1456  host_reference (&host, h, MDL);
1457  break;
1458  }
1459 
1460  if (hp)
1461  host_dereference (&hp, MDL);
1462  }
1463 
1464 #if defined (DEBUG_INFORM_HOST)
1465  /* Hmm..: what when there is a host with a fixed-address,
1466  * that matches by hw or id, but the fixed-addresses
1467  * didn't match client ip?
1468  */
1469  if (h_w_fixed_addr && !h_m_client_ip) {
1470  log_info ("dhcpinform: matching host with "
1471  "fixed-address different than "
1472  "client IP detected?!");
1473  }
1474 #endif
1475 
1476  /* If we have a host_decl structure, run the options
1477  * associated with its group. Whether the host decl
1478  * struct is old or not. */
1479  if (host) {
1480 #if defined (DEBUG_INFORM_HOST)
1481  log_info ("dhcpinform: applying host (group) options");
1482 #endif
1483  execute_statements_in_scope(NULL, packet, NULL, NULL,
1484  packet->options, options,
1485  &global_scope, host->group,
1486  subnet->group,
1487  NULL);
1488  host_dereference (&host, MDL);
1489  }
1490 
1491  /* CC: end of host entry processing.... */
1492 
1493  /* Figure out the filename. */
1494  memset (&d1, 0, sizeof d1);
1495  oc = lookup_option (&server_universe, options, SV_FILENAME);
1496  if (oc &&
1497  evaluate_option_cache (&d1, packet, (struct lease *)0,
1498  (struct client_state *)0,
1499  packet -> options, (struct option_state *)0,
1500  &global_scope, oc, MDL)) {
1501  i = d1.len;
1502  if (i >= sizeof(raw.file)) {
1503  log_info("file name longer than packet field "
1504  "truncated - field: %lu name: %d %.*s",
1505  (unsigned long)sizeof(raw.file), i,
1506  (int)i, d1.data);
1507  i = sizeof(raw.file);
1508  } else
1509  raw.file[i] = 0;
1510  memcpy (raw.file, d1.data, i);
1511  data_string_forget (&d1, MDL);
1512  }
1513 
1514  /* Choose a server name as above. */
1515  oc = lookup_option (&server_universe, options, SV_SERVER_NAME);
1516  if (oc &&
1517  evaluate_option_cache (&d1, packet, (struct lease *)0,
1518  (struct client_state *)0,
1519  packet -> options, (struct option_state *)0,
1520  &global_scope, oc, MDL)) {
1521  i = d1.len;
1522  if (i >= sizeof(raw.sname)) {
1523  log_info("server name longer than packet field "
1524  "truncated - field: %lu name: %d %.*s",
1525  (unsigned long)sizeof(raw.sname), i,
1526  (int)i, d1.data);
1527  i = sizeof(raw.sname);
1528  } else
1529  raw.sname[i] = 0;
1530  memcpy (raw.sname, d1.data, i);
1531  data_string_forget (&d1, MDL);
1532  }
1533 
1534  /* Set a flag if this client is a lame Microsoft client that NUL
1535  terminates string options and expects us to do likewise. */
1536  nulltp = 0;
1537  if ((oc = lookup_option (&dhcp_universe, packet -> options,
1538  DHO_HOST_NAME))) {
1539  if (!oc->expression)
1540  nulltp = oc->flags & OPTION_HAD_NULLS;
1541  }
1542 
1543  /* Put in DHCP-specific options. */
1545  oc = (struct option_cache *)0;
1546  if (option_cache_allocate (&oc, MDL)) {
1547  if (make_const_data (&oc -> expression,
1548  &dhcpack, 1, 0, 0, MDL)) {
1549  option_code_hash_lookup(&oc->option,
1551  &i, 0, MDL);
1552  save_option (&dhcp_universe, options, oc);
1553  }
1555  }
1556 
1557  get_server_source_address(&from, options, options, packet);
1558 
1559  /* Use the subnet mask from the subnet declaration if no other
1560  mask has been provided. */
1561  i = DHO_SUBNET_MASK;
1562  if (subnet && !lookup_option (&dhcp_universe, options, i)) {
1563  oc = (struct option_cache *)0;
1564  if (option_cache_allocate (&oc, MDL)) {
1565  if (make_const_data (&oc -> expression,
1566  subnet -> netmask.iabuf,
1567  subnet -> netmask.len,
1568  0, 0, MDL)) {
1569  option_code_hash_lookup(&oc->option,
1571  &i, 0, MDL);
1572  save_option (&dhcp_universe, options, oc);
1573  }
1575  }
1576  }
1577 
1578  /* If a site option space has been specified, use that for
1579  site option codes. */
1581  if ((oc = lookup_option (&server_universe, options, i)) &&
1582  evaluate_option_cache (&d1, packet, (struct lease *)0,
1583  (struct client_state *)0,
1584  packet -> options, options,
1585  &global_scope, oc, MDL)) {
1586  struct universe *u = (struct universe *)0;
1587 
1588  if (!universe_hash_lookup (&u, universe_hash,
1589  (const char *)d1.data, d1.len,
1590  MDL)) {
1591  log_error ("unknown option space %s.", d1.data);
1592  option_state_dereference (&options, MDL);
1593  if (subnet)
1594  subnet_dereference (&subnet, MDL);
1595  return;
1596  }
1597 
1598  options -> site_universe = u -> index;
1599  options->site_code_min = find_min_site_code(u);
1600  data_string_forget (&d1, MDL);
1601  } else {
1602  options -> site_universe = dhcp_universe.index;
1603  options -> site_code_min = 0; /* Trust me, it works. */
1604  }
1605 
1606  memset (&prl, 0, sizeof prl);
1607 
1608  /* Use the parameter list from the scope if there is one. */
1609  oc = lookup_option (&dhcp_universe, options,
1611 
1612  /* Otherwise, if the client has provided a list of options
1613  that it wishes returned, use it to prioritize. Otherwise,
1614  prioritize based on the default priority list. */
1615 
1616  if (!oc)
1617  oc = lookup_option (&dhcp_universe, packet -> options,
1619 
1620  if (oc)
1621  evaluate_option_cache (&prl, packet, (struct lease *)0,
1622  (struct client_state *)0,
1623  packet -> options, options,
1624  &global_scope, oc, MDL);
1625 
1626 #ifdef DEBUG_PACKET
1627  dump_packet (packet);
1628  dump_raw ((unsigned char *)packet -> raw, packet -> packet_length);
1629 #endif
1630 
1631  log_info ("%s", msgbuf);
1632 
1633  /* Figure out the address of the boot file server. */
1634  if ((oc =
1636  if (evaluate_option_cache (&d1, packet, (struct lease *)0,
1637  (struct client_state *)0,
1638  packet -> options, options,
1639  &global_scope, oc, MDL)) {
1640  /* If there was more than one answer,
1641  take the first. */
1642  if (d1.len >= 4 && d1.data)
1643  memcpy (&raw.siaddr, d1.data, 4);
1644  data_string_forget (&d1, MDL);
1645  }
1646  }
1647 
1648  /*
1649  * Remove any time options, per section 3.4 RFC 2131
1650  */
1654 
1655  /* Set up the option buffer... */
1656  outgoing.packet_length =
1657  cons_options (packet, outgoing.raw, (struct lease *)0,
1658  (struct client_state *)0,
1659  0, packet -> options, options, &global_scope,
1660  0, nulltp, 0,
1661  prl.len ? &prl : (struct data_string *)0,
1662  (char *)0);
1663  option_state_dereference (&options, MDL);
1664  data_string_forget (&prl, MDL);
1665 
1666  /* Make sure that the packet is at least as big as a BOOTP packet. */
1667  if (outgoing.packet_length < BOOTP_MIN_LEN)
1668  outgoing.packet_length = BOOTP_MIN_LEN;
1669 
1670  raw.giaddr = packet -> raw -> giaddr;
1671  raw.ciaddr = packet -> raw -> ciaddr;
1672  memcpy (raw.chaddr, packet -> raw -> chaddr, sizeof raw.chaddr);
1673  raw.hlen = packet -> raw -> hlen;
1674  raw.htype = packet -> raw -> htype;
1675 
1676  raw.xid = packet -> raw -> xid;
1677  raw.secs = packet -> raw -> secs;
1678  raw.flags = packet -> raw -> flags;
1679  raw.hops = packet -> raw -> hops;
1680  raw.op = BOOTREPLY;
1681 
1682 #ifdef DEBUG_PACKET
1683  dump_packet (&outgoing);
1684  dump_raw ((unsigned char *)&raw, outgoing.packet_length);
1685 #endif
1686 
1687 #if defined(DHCPv6) && defined(DHCP4o6)
1688  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
1689  /* Report what we're sending. */
1690  snprintf(msgbuf, sizeof msgbuf,
1691  "DHCP4o6 DHCPACK to %s (%s) via", piaddr(cip),
1692  (packet->raw->htype && packet->raw->hlen) ?
1694  packet->raw->chaddr) :
1695  "<no client hardware address>");
1696  log_info("%s %s", msgbuf, piaddr(packet->client_addr));
1697 
1698  /* fill dhcp4o6_response */
1700  packet->dhcp4o6_response->buffer = NULL;
1702  outgoing.packet_length, MDL)) {
1703  log_fatal("No memory to store DHCP4o6 reply.");
1704  }
1707  memcpy(packet->dhcp4o6_response->buffer->data,
1708  outgoing.raw, outgoing.packet_length);
1709 
1710  /* done */
1711  if (subnet)
1712  subnet_dereference (&subnet, MDL);
1713  return;
1714  }
1715 #endif
1716 
1717  /* Set up the common stuff... */
1718  to.sin_family = AF_INET;
1719 #ifdef HAVE_SA_LEN
1720  to.sin_len = sizeof to;
1721 #endif
1722  memset (to.sin_zero, 0, sizeof to.sin_zero);
1723 
1724  /* RFC2131 states the server SHOULD unicast to ciaddr.
1725  * There are two wrinkles - relays, and when ciaddr is zero.
1726  * There's actually no mention of relays at all in rfc2131 in
1727  * regard to DHCPINFORM, except to say we might get packets from
1728  * clients via them. Note: relays unicast to clients to the
1729  * "yiaddr" address, which servers are forbidden to set when
1730  * answering an inform.
1731  *
1732  * The solution: If ciaddr is zero, and giaddr is set, go via the
1733  * relay with the broadcast flag set to help the relay (with no
1734  * yiaddr and very likely no chaddr, it will have no idea where to
1735  * send the packet).
1736  *
1737  * If the ciaddr is zero and giaddr is not set, go via the source
1738  * IP address (but you are permitted to barf on their shoes).
1739  *
1740  * If ciaddr is not zero, send the packet there always.
1741  */
1742  if (!raw.ciaddr.s_addr && gip.len) {
1743  memcpy(&to.sin_addr, gip.iabuf, 4);
1744 #if defined(RELAY_PORT)
1745  to.sin_port = relay_port ? relay_port : local_port;
1746 #else
1747  to.sin_port = local_port;
1748 #endif
1749  raw.flags |= htons(BOOTP_BROADCAST);
1750  } else {
1751  gip.len = 0;
1752  memcpy(&to.sin_addr, cip.iabuf, 4);
1753  to.sin_port = remote_port;
1754  }
1755 
1756  /* Report what we're sending. */
1757  snprintf(msgbuf, sizeof msgbuf, "DHCPACK to %s (%s) via", piaddr(cip),
1758  (packet->raw->htype && packet->raw->hlen) ?
1760  "<no client hardware address>");
1761  log_info("%s %s", msgbuf, gip.len ? piaddr(gip) :
1762  packet->interface->name);
1763 
1764  errno = 0;
1765  interface = (fallback_interface ? fallback_interface
1766  : packet -> interface);
1767  result = send_packet(interface, &outgoing, &raw,
1768  outgoing.packet_length, from, &to, NULL);
1769  if (result < 0) {
1770  log_error ("%s:%d: Failed to send %d byte long packet over %s "
1771  "interface.", MDL, outgoing.packet_length,
1772  interface->name);
1773  }
1774 
1775 
1776  if (subnet)
1777  subnet_dereference (&subnet, MDL);
1778 }
1779 
1792 void nak_lease (packet, cip, network_group)
1793  struct packet *packet;
1794  struct iaddr *cip;
1795  struct group *network_group; /* scope to use for options */
1796 {
1797  struct sockaddr_in to;
1798  struct in_addr from;
1799  int result;
1800  struct dhcp_packet raw;
1801  unsigned char nak = DHCPNAK;
1802  struct packet outgoing;
1803  unsigned i;
1804 #if defined(RELAY_PORT)
1805  u_int16_t relay_port = 0;
1806 #endif
1807  struct option_state *options = (struct option_state *)0;
1808  struct option_cache *oc = (struct option_cache *)0;
1809  struct option_state *eval_options = NULL;
1810 
1811  option_state_allocate (&options, MDL);
1812  memset (&outgoing, 0, sizeof outgoing);
1813  memset (&raw, 0, sizeof raw);
1814  outgoing.raw = &raw;
1815 
1816  /* Set DHCP_MESSAGE_TYPE to DHCPNAK */
1817  if (!option_cache_allocate (&oc, MDL)) {
1818  log_error ("No memory for DHCPNAK message type.");
1819  option_state_dereference (&options, MDL);
1820  return;
1821  }
1822  if (!make_const_data (&oc -> expression, &nak, sizeof nak,
1823  0, 0, MDL)) {
1824  log_error ("No memory for expr_const expression.");
1826  option_state_dereference (&options, MDL);
1827  return;
1828  }
1830  option_code_hash_lookup(&oc->option, dhcp_universe.code_hash,
1831  &i, 0, MDL);
1832  save_option (&dhcp_universe, options, oc);
1834 
1835 #if defined(RELAY_PORT)
1837 #endif
1838 
1839  /* Set DHCP_MESSAGE to whatever the message is */
1840  if (!option_cache_allocate (&oc, MDL)) {
1841  log_error ("No memory for DHCPNAK message type.");
1842  option_state_dereference (&options, MDL);
1843  return;
1844  }
1845  if (!make_const_data (&oc -> expression,
1846  (unsigned char *)dhcp_message,
1847  strlen (dhcp_message), 1, 0, MDL)) {
1848  log_error ("No memory for expr_const expression.");
1850  option_state_dereference (&options, MDL);
1851  return;
1852  }
1853  i = DHO_DHCP_MESSAGE;
1854  option_code_hash_lookup(&oc->option, dhcp_universe.code_hash,
1855  &i, 0, MDL);
1856  save_option (&dhcp_universe, options, oc);
1858 
1859  /* Setup the options at the global and subnet scopes. These
1860  * may be used to locate sever id option if enabled as well
1861  * for echo-client-id further on. (This allocates eval_options). */
1862  eval_network_statements(&eval_options, packet, network_group);
1863 
1864 #if defined(SERVER_ID_FOR_NAK)
1865  /* Pass in the evaluated options so they can be searched for
1866  * server-id, otherwise source address comes from the interface
1867  * address. */
1868  get_server_source_address(&from, eval_options, options, packet);
1869 #else
1870  /* Get server source address from the interface address */
1871  get_server_source_address(&from, NULL, options, packet);
1872 #endif /* if defined(SERVER_ID_FOR_NAK) */
1873 
1874  /* If there were agent options in the incoming packet, return
1875  * them. We do not check giaddr to detect the presence of a
1876  * relay, as this excludes "l2" relay agents which have no
1877  * giaddr to set.
1878  */
1882  ((struct option_chain_head **)
1883  &(options -> universes [agent_universe.index]),
1884  (struct option_chain_head *)
1885  packet -> options -> universes [agent_universe.index],
1886  MDL);
1887  }
1888 
1889  /* echo-client-id can specified at the class level so add class-scoped
1890  * options into eval_options. */
1891  for (i = packet->class_count; i > 0; i--) {
1892  execute_statements_in_scope(NULL, packet, NULL, NULL,
1893  packet->options, eval_options,
1894  &global_scope,
1895  packet->classes[i - 1]->group,
1896  NULL, NULL);
1897  }
1898 
1899  /* Echo client id if we received and it's enabled */
1900  echo_client_id(packet, NULL, eval_options, options);
1901  option_state_dereference (&eval_options, MDL);
1902 
1903  /* Do not use the client's requested parameter list. */
1904  delete_option (&dhcp_universe, packet -> options,
1906 
1907  /* Set up the option buffer... */
1908  outgoing.packet_length =
1909  cons_options (packet, outgoing.raw, (struct lease *)0,
1910  (struct client_state *)0,
1911  0, packet -> options, options, &global_scope,
1912  0, 0, 0, (struct data_string *)0, (char *)0);
1913  option_state_dereference (&options, MDL);
1914 
1915 /* memset (&raw.ciaddr, 0, sizeof raw.ciaddr);*/
1916  raw.giaddr = packet -> raw -> giaddr;
1917  memcpy (raw.chaddr, packet -> raw -> chaddr, sizeof raw.chaddr);
1918  raw.hlen = packet -> raw -> hlen;
1919  raw.htype = packet -> raw -> htype;
1920 
1921  raw.xid = packet -> raw -> xid;
1922  raw.secs = packet -> raw -> secs;
1923  raw.flags = packet -> raw -> flags | htons (BOOTP_BROADCAST);
1924  raw.hops = packet -> raw -> hops;
1925  raw.op = BOOTREPLY;
1926 
1927  /* Make sure that the packet is at least as big as a BOOTP packet. */
1928  if (outgoing.packet_length < BOOTP_MIN_LEN)
1929  outgoing.packet_length = BOOTP_MIN_LEN;
1930 
1931  /* Report what we're sending... */
1932 #if defined(DHCPv6) && defined(DHCP4o6)
1933  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
1934  log_info ("DHCP4o6 DHCPNAK on %s to %s via %s",
1935  piaddr (*cip),
1936  print_hw_addr (packet -> raw -> htype,
1937  packet -> raw -> hlen,
1938  packet -> raw -> chaddr),
1940  } else
1941 #endif
1942  log_info ("DHCPNAK on %s to %s via %s",
1943  piaddr (*cip),
1945  packet -> raw -> giaddr.s_addr
1946  ? inet_ntoa (packet -> raw -> giaddr)
1947  : packet -> interface -> name);
1948 
1949 #ifdef DEBUG_PACKET
1950  dump_packet (packet);
1951  dump_raw ((unsigned char *)packet -> raw, packet -> packet_length);
1952  dump_packet (&outgoing);
1953  dump_raw ((unsigned char *)&raw, outgoing.packet_length);
1954 #endif
1955 
1956 #if defined(DHCPv6) && defined(DHCP4o6)
1957  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
1958  /* fill dhcp4o6_response */
1960  packet->dhcp4o6_response->buffer = NULL;
1962  outgoing.packet_length, MDL)) {
1963  log_fatal("No memory to store DHCP4o6 reply.");
1964  }
1967  memcpy(packet->dhcp4o6_response->buffer->data,
1968  outgoing.raw, outgoing.packet_length);
1969  return;
1970  }
1971 #endif
1972 
1973  /* Set up the common stuff... */
1974  to.sin_family = AF_INET;
1975 #ifdef HAVE_SA_LEN
1976  to.sin_len = sizeof to;
1977 #endif
1978  memset (to.sin_zero, 0, sizeof to.sin_zero);
1979 
1980  /* If this was gatewayed, send it back to the gateway.
1981  Otherwise, broadcast it on the local network. */
1982  if (raw.giaddr.s_addr) {
1983  to.sin_addr = raw.giaddr;
1984  if (raw.giaddr.s_addr != htonl (INADDR_LOOPBACK))
1985 #if defined(RELAY_PORT)
1986  to.sin_port = relay_port ? relay_port : local_port;
1987 #else
1988  to.sin_port = local_port;
1989 #endif
1990  else
1991  to.sin_port = remote_port; /* for testing. */
1992 
1993  if (fallback_interface) {
1994  result = send_packet(fallback_interface, packet, &raw,
1995  outgoing.packet_length, from, &to,
1996  NULL);
1997  if (result < 0) {
1998  log_error ("%s:%d: Failed to send %d byte long "
1999  "packet over %s interface.", MDL,
2000  outgoing.packet_length,
2002  }
2003 
2004  return;
2005  }
2006  } else {
2007  to.sin_addr = limited_broadcast;
2008  to.sin_port = remote_port;
2009  }
2010 
2011  errno = 0;
2012  result = send_packet(packet->interface, packet, &raw,
2013  outgoing.packet_length, from, &to, NULL);
2014  if (result < 0) {
2015  log_error ("%s:%d: Failed to send %d byte long packet over %s "
2016  "interface.", MDL, outgoing.packet_length,
2017  packet->interface->name);
2018  }
2019 
2020 }
2021 
2040 void echo_client_id(packet, lease, in_options, out_options)
2041  struct packet *packet;
2042  struct lease *lease;
2043  struct option_state *in_options;
2044  struct option_state *out_options;
2045 {
2046  struct option_cache *oc;
2047  int ignorep;
2048 
2049  /* Check if echo-client-id is enabled */
2050  oc = lookup_option(&server_universe, in_options, SV_ECHO_CLIENT_ID);
2051  if (oc && evaluate_boolean_option_cache(&ignorep, packet, lease,
2052  NULL, packet->options,
2053  in_options,
2054  (lease ? &lease->scope : NULL),
2055  oc, MDL)) {
2056  struct data_string client_id;
2057  unsigned int opcode = DHO_DHCP_CLIENT_IDENTIFIER;
2058 
2059  /* Save knowledge that echo is enabled to the packet */
2060  packet->sv_echo_client_id = ISC_TRUE;
2061 
2062  /* Now see if inbound packet contains client-id */
2063  oc = lookup_option(&dhcp_universe, packet->options, opcode);
2064  memset(&client_id, 0, sizeof client_id);
2065  if (oc && evaluate_option_cache(&client_id,
2066  packet, NULL, NULL,
2067  packet->options, NULL,
2068  (lease ? &lease->scope : NULL),
2069  oc, MDL)) {
2070  /* Packet contained client-id, add it to out_options. */
2071  oc = NULL;
2072  if (option_cache_allocate(&oc, MDL)) {
2073  if (make_const_data(&oc->expression,
2074  client_id.data,
2075  client_id.len,
2076  1, 0, MDL)) {
2077  option_code_hash_lookup(&oc->option,
2078  dhcp_universe.
2079  code_hash,
2080  &opcode,
2081  0, MDL);
2083  out_options, oc);
2084  }
2086  }
2087  }
2088  }
2089 }
2090 
2092  struct packet *packet;
2093  struct lease *lease;
2094  struct lease_state *state;
2095 
2096 {
2097 
2098  struct pool *pool = lease->pool;
2099  int used, count, high_threshold, poolhigh = 0, poollow = 0;
2100  char *shared_name = "no name";
2101 
2102  if (pool == NULL)
2103  return;
2104 
2105  /* get a pointer to the name if we have one */
2106  if ((pool->shared_network != NULL) &&
2107  (pool->shared_network->name != NULL)) {
2108  shared_name = pool->shared_network->name;
2109  }
2110 
2111  count = pool->lease_count;
2112  used = count - (pool->free_leases + pool->backup_leases);
2113 
2114  /* The logged flag indicates if we have already crossed the high
2115  * threshold and emitted a log message. If it is set we check to
2116  * see if we have re-crossed the low threshold and need to reset
2117  * things. When we cross the high threshold we determine what
2118  * the low threshold is and save it into the low_threshold value.
2119  * When we cross that threshold we reset the logged flag and
2120  * the low_threshold to 0 which allows the high threshold message
2121  * to be emitted once again.
2122  * if we haven't recrossed the boundry we don't need to do anything.
2123  */
2124  if (pool->logged !=0) {
2125  if (used <= pool->low_threshold) {
2126  pool->low_threshold = 0;
2127  pool->logged = 0;
2128  log_error("Pool threshold reset - shared subnet: %s; "
2129  "address: %s; low threshold %d/%d.",
2130  shared_name, piaddr(lease->ip_addr),
2131  used, count);
2132  }
2133  return;
2134  }
2135 
2136  /* find the high threshold */
2137  if (get_option_int(&poolhigh, &server_universe, packet, lease, NULL,
2138  packet->options, state->options, state->options,
2139  &lease->scope, SV_LOG_THRESHOLD_HIGH, MDL) == 0) {
2140  /* no threshold bail out */
2141  return;
2142  }
2143 
2144  /* We do have a threshold for this pool, see if its valid */
2145  if ((poolhigh <= 0) || (poolhigh > 100)) {
2146  /* not valid */
2147  return;
2148  }
2149 
2150  /* we have a valid value, have we exceeded it */
2151  high_threshold = FIND_PERCENT(count, poolhigh);
2152  if (used < high_threshold) {
2153  /* nope, no more to do */
2154  return;
2155  }
2156 
2157  /* we've exceeded it, output a message */
2158  log_error("Pool threshold exceeded - shared subnet: %s; "
2159  "address: %s; high threshold %d%% %d/%d.",
2160  shared_name, piaddr(lease->ip_addr),
2161  poolhigh, used, count);
2162 
2163  /* handle the low threshold now, if we don't
2164  * have a valid one we default to 0. */
2165  if ((get_option_int(&poollow, &server_universe, packet, lease, NULL,
2166  packet->options, state->options, state->options,
2167  &lease->scope, SV_LOG_THRESHOLD_LOW, MDL) == 0) ||
2168  (poollow > 100)) {
2169  poollow = 0;
2170  }
2171 
2172  /*
2173  * If the low theshold is higher than the high threshold we continue to log
2174  * If it isn't then we set the flag saying we already logged and determine
2175  * what the reset threshold is.
2176  */
2177  if (poollow < poolhigh) {
2178  pool->logged = 1;
2179  pool->low_threshold = FIND_PERCENT(count, poollow);
2180  }
2181 }
2182 
2183 void ack_lease (packet, lease, offer, when, msg, ms_nulltp, hp)
2184  struct packet *packet;
2185  struct lease *lease;
2186  unsigned int offer;
2187  TIME when;
2188  char *msg;
2189  int ms_nulltp;
2190  struct host_decl *hp;
2191 {
2192  struct lease *lt;
2193  struct lease_state *state;
2194  struct lease *next;
2195  struct host_decl *host = (struct host_decl *)0;
2196  TIME lease_time;
2197  TIME offered_lease_time;
2198  struct data_string d1;
2199  TIME min_lease_time;
2202  struct option_cache *oc;
2203  isc_result_t result;
2204  TIME ping_timeout;
2205  TIME lease_cltt;
2206  struct in_addr from;
2207  TIME remaining_time;
2208  struct iaddr cip;
2209 #if defined(DELAYED_ACK)
2210  /* By default we don't do the enqueue */
2211  isc_boolean_t enqueue = ISC_FALSE;
2212 #endif
2213  int use_old_lease = 0;
2214 
2215  unsigned i, j;
2216  int s1;
2217  int ignorep;
2218  struct timeval tv;
2219 
2220  /* If we're already acking this lease, don't do it again. */
2221  if (lease -> state)
2222  return;
2223 
2224  /* Save original cltt for comparison later. */
2225  lease_cltt = lease->cltt;
2226 
2227  /* If the lease carries a host record, remember it. */
2228  if (hp)
2229  host_reference (&host, hp, MDL);
2230  else if (lease -> host)
2231  host_reference (&host, lease -> host, MDL);
2232 
2233  /* Allocate a lease state structure... */
2234  state = new_lease_state (MDL);
2235  if (!state)
2236  log_fatal ("unable to allocate lease state!");
2237  state -> got_requested_address = packet -> got_requested_address;
2238  shared_network_reference (&state -> shared_network,
2239  packet -> interface -> shared_network, MDL);
2240 
2241  /* See if we got a server identifier option. */
2243  packet -> options, DHO_DHCP_SERVER_IDENTIFIER))
2244  state -> got_server_identifier = 1;
2245 
2246  maybe_return_agent_options(packet, state->options);
2247 
2248  /* If we are offering a lease that is still currently valid, preserve
2249  the events. We need to do this because if the client does not
2250  REQUEST our offer, it will expire in 2 minutes, overriding the
2251  expire time in the currently in force lease. We want the expire
2252  events to be executed at that point. */
2253  if (lease->ends <= cur_time && offer != DHCPOFFER) {
2254  /* Get rid of any old expiry or release statements - by
2255  executing the statements below, we will be inserting new
2256  ones if there are any to insert. */
2257  if (lease->on_star.on_expiry)
2259  (&lease->on_star.on_expiry, MDL);
2260  if (lease->on_star.on_commit)
2262  (&lease->on_star.on_commit, MDL);
2263  if (lease->on_star.on_release)
2265  (&lease->on_star.on_release, MDL);
2266  }
2267 
2268  /* Execute statements in scope starting with the subnet scope. */
2270  NULL, packet->options,
2271  state->options, &lease->scope,
2272  lease->subnet->group, NULL, NULL);
2273 
2274  /* If the lease is from a pool, run the pool scope. */
2275  if (lease->pool)
2277  packet->options, state->options,
2278  &lease->scope, lease->pool->group,
2279  lease->pool->
2281  NULL));
2282 
2283  /* Execute statements from class scopes. */
2284  for (i = packet -> class_count; i > 0; i--) {
2286  packet->options, state->options,
2287  &lease->scope,
2288  packet->classes[i - 1]->group,
2289  (lease->pool ? lease->pool->group
2290  : lease->subnet->group),
2291  NULL);
2292  }
2293 
2294  /* See if the client is only supposed to have one lease at a time,
2295  and if so, find its other leases and release them. We can only
2296  do this on DHCPREQUEST. It's a little weird to do this before
2297  looking at permissions, because the client might not actually
2298  _get_ a lease after we've done the permission check, but the
2299  assumption for this option is that the client has exactly one
2300  network interface, and will only ever remember one lease. So
2301  if it sends a DHCPREQUEST, and doesn't get the lease, it's already
2302  forgotten about its old lease, so we can too. */
2303  if (packet -> packet_type == DHCPREQUEST &&
2304  (oc = lookup_option (&server_universe, state -> options,
2307  packet, lease,
2308  (struct client_state *)0,
2309  packet -> options,
2310  state -> options, &lease -> scope,
2311  oc, MDL)) {
2312  struct lease *seek;
2313  if (lease -> uid_len) {
2314  do {
2315  seek = (struct lease *)0;
2316  find_lease_by_uid (&seek, lease -> uid,
2317  lease -> uid_len, MDL);
2318  if (!seek)
2319  break;
2320  if (seek == lease && !seek -> n_uid) {
2321  lease_dereference (&seek, MDL);
2322  break;
2323  }
2324  next = (struct lease *)0;
2325 
2326  /* Don't release expired leases, and don't
2327  release the lease we're going to assign. */
2328  next = (struct lease *)0;
2329  while (seek) {
2330  if (seek -> n_uid)
2331  lease_reference (&next, seek -> n_uid, MDL);
2332  if (seek != lease &&
2333  seek -> binding_state != FTS_RELEASED &&
2334  seek -> binding_state != FTS_EXPIRED &&
2335  seek -> binding_state != FTS_RESET &&
2336  seek -> binding_state != FTS_FREE &&
2337  seek -> binding_state != FTS_BACKUP)
2338  break;
2339  lease_dereference (&seek, MDL);
2340  if (next) {
2341  lease_reference (&seek, next, MDL);
2342  lease_dereference (&next, MDL);
2343  }
2344  }
2345  if (next)
2346  lease_dereference (&next, MDL);
2347  if (seek) {
2348  release_lease (seek, packet);
2349  lease_dereference (&seek, MDL);
2350  } else
2351  break;
2352  } while (1);
2353  }
2354  if (!lease -> uid_len ||
2355  (host &&
2356  !host -> client_identifier.len &&
2357  (oc = lookup_option (&server_universe, state -> options,
2358  SV_DUPLICATES)) &&
2360  (struct client_state *)0,
2361  packet -> options,
2362  state -> options,
2363  &lease -> scope,
2364  oc, MDL))) {
2365  do {
2366  seek = (struct lease *)0;
2368  (&seek, lease -> hardware_addr.hbuf,
2369  lease -> hardware_addr.hlen, MDL);
2370  if (!seek)
2371  break;
2372  if (seek == lease && !seek -> n_hw) {
2373  lease_dereference (&seek, MDL);
2374  break;
2375  }
2376  next = (struct lease *)0;
2377  while (seek) {
2378  if (seek -> n_hw)
2379  lease_reference (&next, seek -> n_hw, MDL);
2380  if (seek != lease &&
2381  seek -> binding_state != FTS_RELEASED &&
2382  seek -> binding_state != FTS_EXPIRED &&
2383  seek -> binding_state != FTS_RESET &&
2384  seek -> binding_state != FTS_FREE &&
2385  seek -> binding_state != FTS_BACKUP)
2386  break;
2387  lease_dereference (&seek, MDL);
2388  if (next) {
2389  lease_reference (&seek, next, MDL);
2390  lease_dereference (&next, MDL);
2391  }
2392  }
2393  if (next)
2394  lease_dereference (&next, MDL);
2395  if (seek) {
2396  release_lease (seek, packet);
2397  lease_dereference (&seek, MDL);
2398  } else
2399  break;
2400  } while (1);
2401  }
2402  }
2403 
2404 
2405  /* Make sure this packet satisfies the configured minimum
2406  number of seconds. */
2407  memset (&d1, 0, sizeof d1);
2408  if (offer == DHCPOFFER &&
2409  (oc = lookup_option (&server_universe, state -> options,
2410  SV_MIN_SECS))) {
2411  if (evaluate_option_cache (&d1, packet, lease,
2412  (struct client_state *)0,
2413  packet -> options, state -> options,
2414  &lease -> scope, oc, MDL)) {
2415  if (d1.len &&
2416  ntohs (packet -> raw -> secs) < d1.data [0]) {
2417  log_info("%s: configured min-secs value (%d) "
2418  "is greater than secs field (%d). "
2419  "message dropped.", msg, d1.data[0],
2420  ntohs(packet->raw->secs));
2421  data_string_forget (&d1, MDL);
2423  if (host)
2424  host_dereference (&host, MDL);
2425  return;
2426  }
2427  data_string_forget (&d1, MDL);
2428  }
2429  }
2430 
2431  /* Try to find a matching host declaration for this lease.
2432  */
2433  if (!host) {
2434  struct host_decl *hp = (struct host_decl *)0;
2435  struct host_decl *h;
2436 
2437  /* Try to find a host_decl that matches the client
2438  identifier or hardware address on the packet, and
2439  has no fixed IP address. If there is one, hang
2440  it off the lease so that its option definitions
2441  can be used. */
2442  oc = lookup_option (&dhcp_universe, packet -> options,
2444  if (oc &&
2446  (struct client_state *)0,
2447  packet -> options, state -> options,
2448  &lease -> scope, oc, MDL)) {
2449  find_hosts_by_uid (&hp, d1.data, d1.len, MDL);
2450  data_string_forget (&d1, MDL);
2451  for (h = hp; h; h = h -> n_ipaddr) {
2452  if (!h -> fixed_addr)
2453  break;
2454  }
2455  if (h)
2456  host_reference (&host, h, MDL);
2457  if (hp != NULL)
2458  host_dereference(&hp, MDL);
2459  }
2460  if (!host) {
2461  find_hosts_by_haddr (&hp,
2462  packet -> raw -> htype,
2463  packet -> raw -> chaddr,
2464  packet -> raw -> hlen,
2465  MDL);
2466  for (h = hp; h; h = h -> n_ipaddr) {
2467  if (!h -> fixed_addr)
2468  break;
2469  }
2470  if (h)
2471  host_reference (&host, h, MDL);
2472  if (hp != NULL)
2473  host_dereference(&hp, MDL);
2474  }
2475  if (!host) {
2477  packet->options, MDL);
2478  for (h = hp; h; h = h -> n_ipaddr) {
2479  if (!h -> fixed_addr)
2480  break;
2481  }
2482  if (h)
2483  host_reference (&host, h, MDL);
2484  if (hp != NULL)
2485  host_dereference(&hp, MDL);
2486  }
2487  }
2488 
2489  /* If we have a host_decl structure, run the options associated
2490  with its group. Whether the host decl struct is old or not. */
2491  if (host)
2493  packet->options, state->options,
2494  &lease->scope, host->group,
2495  (lease->pool
2496  ? lease->pool->group
2497  : lease->subnet->group),
2498  NULL);
2499 
2500  /* Drop the request if it's not allowed for this client. By
2501  default, unknown clients are allowed. */
2502  if (!host &&
2503  (oc = lookup_option (&server_universe, state -> options,
2505  !evaluate_boolean_option_cache (&ignorep,
2506  packet, lease,
2507  (struct client_state *)0,
2508  packet -> options,
2509  state -> options,
2510  &lease -> scope, oc, MDL)) {
2511  if (!ignorep)
2512  log_info ("%s: unknown client", msg);
2513  free_lease_state (state, MDL);
2514  if (host)
2515  host_dereference (&host, MDL);
2516  return;
2517  }
2518 
2519  /* Drop the request if it's not allowed for this client. */
2520  if (!offer &&
2521  (oc = lookup_option (&server_universe, state -> options,
2522  SV_ALLOW_BOOTP)) &&
2523  !evaluate_boolean_option_cache (&ignorep,
2524  packet, lease,
2525  (struct client_state *)0,
2526  packet -> options,
2527  state -> options,
2528  &lease -> scope, oc, MDL)) {
2529  if (!ignorep)
2530  log_info ("%s: bootp disallowed", msg);
2531  free_lease_state (state, MDL);
2532  if (host)
2533  host_dereference (&host, MDL);
2534  return;
2535  }
2536 
2537  /* Drop the request if booting is specifically denied. */
2538  oc = lookup_option (&server_universe, state -> options,
2540  if (oc &&
2541  !evaluate_boolean_option_cache (&ignorep,
2542  packet, lease,
2543  (struct client_state *)0,
2544  packet -> options,
2545  state -> options,
2546  &lease -> scope, oc, MDL)) {
2547  if (!ignorep)
2548  log_info ("%s: booting disallowed", msg);
2549  free_lease_state (state, MDL);
2550  if (host)
2551  host_dereference (&host, MDL);
2552  return;
2553  }
2554 
2555  /* If we are configured to do per-class billing, do it. */
2556  if (have_billing_classes && !(lease -> flags & STATIC_LEASE)) {
2557  /* See if the lease is currently being billed to a
2558  class, and if so, whether or not it can continue to
2559  be billed to that class. */
2560  if (lease -> billing_class) {
2561  for (i = 0; i < packet -> class_count; i++)
2562  if (packet -> classes [i] ==
2563  lease -> billing_class)
2564  break;
2565  if (i == packet -> class_count) {
2567  /* Active lease billing change negates reuse */
2568  if (lease->binding_state == FTS_ACTIVE) {
2569  lease->cannot_reuse = 1;
2570  }
2571  }
2572  }
2573 
2574  /* If we don't have an active billing, see if we need
2575  one, and if we do, try to do so. */
2576  if (lease->billing_class == NULL) {
2577  char *cname = "";
2578  int bill = 0;
2579 
2580  for (i = 0; i < packet->class_count; i++) {
2581  struct class *billclass, *subclass;
2582 
2583  billclass = packet->classes[i];
2584  if (billclass->lease_limit) {
2585  bill++;
2586  if (bill_class(lease, billclass))
2587  break;
2588 
2589  subclass = billclass->superclass;
2590  if (subclass == NULL)
2591  cname = subclass->name;
2592  else
2593  cname = billclass->name;
2594  }
2595  }
2596  if (bill != 0 && i == packet->class_count) {
2597  log_info("%s: no available billing: lease "
2598  "limit reached in all matching "
2599  "classes (last: '%s')", msg, cname);
2600  free_lease_state(state, MDL);
2601  if (host)
2602  host_dereference(&host, MDL);
2603  return;
2604  }
2605 
2606  /*
2607  * If this is an offer, undo the billing. We go
2608  * through all the steps above to bill a class so
2609  * we can hit the 'no available billing' mark and
2610  * abort without offering. But it just doesn't make
2611  * sense to permanently bill a class for a non-active
2612  * lease. This means on REQUEST, we will bill this
2613  * lease again (if there is a REQUEST).
2614  */
2615  if (offer == DHCPOFFER &&
2616  lease->billing_class != NULL &&
2619 
2620  /* Lease billing change negates reuse */
2621  if (lease->billing_class != NULL) {
2622  lease->cannot_reuse = 1;
2623  }
2624  }
2625  }
2626 
2627  /* Figure out the filename. */
2628  oc = lookup_option (&server_universe, state -> options, SV_FILENAME);
2629  if (oc)
2630  evaluate_option_cache (&state -> filename, packet, lease,
2631  (struct client_state *)0,
2632  packet -> options, state -> options,
2633  &lease -> scope, oc, MDL);
2634 
2635  /* Choose a server name as above. */
2636  oc = lookup_option (&server_universe, state -> options,
2637  SV_SERVER_NAME);
2638  if (oc)
2639  evaluate_option_cache (&state -> server_name, packet, lease,
2640  (struct client_state *)0,
2641  packet -> options, state -> options,
2642  &lease -> scope, oc, MDL);
2643 
2644  /* At this point, we have a lease that we can offer the client.
2645  Now we construct a lease structure that contains what we want,
2646  and call supersede_lease to do the right thing with it. */
2647  lt = (struct lease *)0;
2648  result = lease_allocate (&lt, MDL);
2649  if (result != ISC_R_SUCCESS) {
2650  log_info ("%s: can't allocate temporary lease structure: %s",
2651  msg, isc_result_totext (result));
2653  if (host)
2654  host_dereference (&host, MDL);
2655  return;
2656  }
2657 
2658  /* Use the ip address of the lease that we finally found in
2659  the database. */
2660  lt -> ip_addr = lease -> ip_addr;
2661 
2662  /* Start now. */
2663  lt -> starts = cur_time;
2664 
2665  /* Figure out how long a lease to assign. If this is a
2666  dynamic BOOTP lease, its duration must be infinite. */
2667  if (offer) {
2668  lt->flags &= ~BOOTP_LEASE;
2669 
2671  if ((oc = lookup_option (&server_universe, state -> options,
2673  if (evaluate_option_cache (&d1, packet, lease,
2674  (struct client_state *)0,
2675  packet -> options,
2676  state -> options,
2677  &lease -> scope, oc, MDL)) {
2678  if (d1.len == sizeof (u_int32_t))
2680  getULong (d1.data);
2681  data_string_forget (&d1, MDL);
2682  }
2683  }
2684 
2685  if ((oc = lookup_option (&dhcp_universe, packet -> options,
2687  s1 = evaluate_option_cache (&d1, packet, lease,
2688  (struct client_state *)0,
2689  packet -> options,
2690  state -> options,
2691  &lease -> scope, oc, MDL);
2692  else
2693  s1 = 0;
2694 
2695  if (s1 && (d1.len == 4)) {
2696  u_int32_t ones = 0xffffffff;
2697 
2698  /* One potential use of reserved leases is to allow
2699  * clients to signal reservation of their lease. They
2700  * can kinda sorta do this, if you squint hard enough,
2701  * by supplying an 'infinite' requested-lease-time
2702  * option. This is generally bad practice...you want
2703  * clients to return to the server on at least some
2704  * period (days, months, years) to get up-to-date
2705  * config state. So;
2706  *
2707  * 1) A client requests 0xffffffff lease-time.
2708  * 2) The server reserves the lease, and assigns a
2709  * <= max_lease_time lease-time to the client, which
2710  * we presume is much smaller than 0xffffffff.
2711  * 3) The client ultimately fails to renew its lease
2712  * (all clients go offline at some point).
2713  * 4) The server retains the reservation, although
2714  * the lease expires and passes through those states
2715  * as normal, it's placed in the 'reserved' queue,
2716  * and is under no circumstances allocated to any
2717  * clients.
2718  *
2719  * Whether the client knows its reserving its lease or
2720  * not, this can be a handy tool for a sysadmin.
2721  */
2722  if ((memcmp(d1.data, &ones, 4) == 0) &&
2724  state->options,
2725  SV_RESERVE_INFINITE)) &&
2727  lease, NULL, packet->options,
2728  state->options, &lease->scope,
2729  oc, MDL)) {
2730  lt->flags |= RESERVED_LEASE;
2731  if (!ignorep)
2732  log_info("Infinite-leasetime "
2733  "reservation made on %s.",
2734  piaddr(lt->ip_addr));
2735  }
2736 
2737  lease_time = getULong (d1.data);
2738  } else
2739  lease_time = default_lease_time;
2740 
2741  if (s1)
2742  data_string_forget(&d1, MDL);
2743 
2744  /* See if there's a maximum lease time. */
2746  if ((oc = lookup_option (&server_universe, state -> options,
2747  SV_MAX_LEASE_TIME))) {
2748  if (evaluate_option_cache (&d1, packet, lease,
2749  (struct client_state *)0,
2750  packet -> options,
2751  state -> options,
2752  &lease -> scope, oc, MDL)) {
2753  if (d1.len == sizeof (u_int32_t))
2754  max_lease_time =
2755  getULong (d1.data);
2756  data_string_forget (&d1, MDL);
2757  }
2758  }
2759 
2760  /* Enforce the maximum lease length. */
2761  if (lease_time < 0 /* XXX */
2762  || lease_time > max_lease_time)
2763  lease_time = max_lease_time;
2764 
2765  min_lease_time = DEFAULT_MIN_LEASE_TIME;
2766  if (min_lease_time > max_lease_time)
2767  min_lease_time = max_lease_time;
2768 
2769  if ((oc = lookup_option (&server_universe, state -> options,
2770  SV_MIN_LEASE_TIME))) {
2771  if (evaluate_option_cache (&d1, packet, lease,
2772  (struct client_state *)0,
2773  packet -> options,
2774  state -> options,
2775  &lease -> scope, oc, MDL)) {
2776  if (d1.len == sizeof (u_int32_t))
2777  min_lease_time = getULong (d1.data);
2778  data_string_forget (&d1, MDL);
2779  }
2780  }
2781 
2782  /* CC: If there are less than
2783  adaptive-lease-time-threshold % free leases,
2784  hand out only short term leases */
2785 
2786  memset(&d1, 0, sizeof(d1));
2787  if (lease->pool &&
2790  evaluate_option_cache(&d1, packet, lease, NULL,
2792  &lease->scope, oc, MDL)) {
2793  if (d1.len == 1 && d1.data[0] > 0 &&
2794  d1.data[0] < 100) {
2795  TIME adaptive_time;
2796  int poolfilled, total, count;
2797 
2798  if (min_lease_time)
2799  adaptive_time = min_lease_time;
2800  else
2801  adaptive_time = DEFAULT_MIN_LEASE_TIME;
2802 
2803  /* Allow the client to keep its lease. */
2804  if (lease->ends - cur_time > adaptive_time)
2805  adaptive_time = lease->ends - cur_time;
2806 
2807  count = lease->pool->lease_count;
2808  total = count - (lease->pool->free_leases +
2810 
2811  poolfilled = (total > (INT_MAX / 100)) ?
2812  total / (count / 100) :
2813  (total * 100) / count;
2814 
2815  log_debug("Adap-lease: Total: %d, Free: %d, "
2816  "Ends: %d, Adaptive: %d, Fill: %d, "
2817  "Threshold: %d",
2820  (int)(lease->ends - cur_time),
2821  (int)adaptive_time, poolfilled,
2822  d1.data[0]);
2823 
2824  if (poolfilled >= d1.data[0] &&
2825  lease_time > adaptive_time) {
2826  log_info("Pool over threshold, time "
2827  "for %s reduced from %d to "
2828  "%d.", piaddr(lease->ip_addr),
2829  (int)lease_time,
2830  (int)adaptive_time);
2831 
2832  lease_time = adaptive_time;
2833  }
2834  }
2835  data_string_forget(&d1, MDL);
2836  }
2837 
2838 
2839  /*
2840  * If this is an ack check to see if we have used enough of
2841  * the pool to want to log a message
2842  */
2843  if (offer == DHCPACK)
2845 
2846  /* a client requests an address which is not yet active*/
2847  if (lease->pool && lease->pool->valid_from &&
2848  cur_time < lease->pool->valid_from) {
2849  /* NAK leases before pool activation date */
2850  cip.len = 4;
2851  memcpy (cip.iabuf, &lt->ip_addr.iabuf, 4);
2852  nak_lease(packet, &cip, lease->subnet->group);
2854  lease_dereference (&lt, MDL);
2855  if (host)
2856  host_dereference (&host, MDL);
2857  return;
2858 
2859  }
2860 
2861  /* CC:
2862  a) NAK current lease if past the expiration date
2863  b) extend lease only up to the expiration date, but not
2864  below min-lease-time
2865  Setting min-lease-time is essential for this to work!
2866  The value of min-lease-time determines the length
2867  of the transition window:
2868  A client renewing a second before the deadline will
2869  get a min-lease-time lease. Since the current ip might not
2870  be routable after the deadline, the client will
2871  be offline until it DISCOVERS again. Otherwise it will
2872  receive a NAK at T/2.
2873  A min-lease-time of 6 seconds effectively switches over
2874  all clients in this pool very quickly.
2875  */
2876 
2877  if (lease->pool && lease->pool->valid_until) {
2878  if (cur_time >= lease->pool->valid_until) {
2879  /* NAK leases after pool expiration date */
2880  cip.len = 4;
2881  memcpy (cip.iabuf, &lt->ip_addr.iabuf, 4);
2882  nak_lease(packet, &cip, lease->subnet->group);
2884  lease_dereference (&lt, MDL);
2885  if (host)
2886  host_dereference (&host, MDL);
2887  return;
2888  }
2889  remaining_time = lease->pool->valid_until - cur_time;
2890  if (lease_time > remaining_time)
2891  lease_time = remaining_time;
2892  }
2893 
2894  if (lease_time < min_lease_time) {
2895  if (min_lease_time)
2896  lease_time = min_lease_time;
2897  else
2898  lease_time = default_lease_time;
2899  }
2900 
2901 
2902 #if defined (FAILOVER_PROTOCOL)
2903  /* Okay, we know the lease duration. Now check the
2904  failover state, if any. */
2905  if (lease -> pool && lease -> pool -> failover_peer) {
2906  TIME new_lease_time = lease_time;
2907  dhcp_failover_state_t *peer =
2908  lease -> pool -> failover_peer;
2909 
2910  /* Copy previous lease failover ack-state. */
2911  lt->tsfp = lease->tsfp;
2912  lt->atsfp = lease->atsfp;
2913 
2914  /* cltt set below */
2915 
2916  /* Lease times less than MCLT are not a concern. */
2917  if (lease_time > peer->mclt) {
2918  /* Each server can only offer a lease time
2919  * that is either equal to MCLT (at least),
2920  * or up to TSFP+MCLT. Only if the desired
2921  * lease time falls within TSFP+MCLT, can
2922  * the server allow it.
2923  */
2924  if (lt->tsfp <= cur_time)
2925  new_lease_time = peer->mclt;
2926  else if ((cur_time + lease_time) >
2927  (lt->tsfp + peer->mclt))
2928  new_lease_time = (lt->tsfp - cur_time)
2929  + peer->mclt;
2930  }
2931 
2932  /* Update potential expiry. Allow for the desired
2933  * lease time plus one half the actual (whether
2934  * modified downward or not) lease time, which is
2935  * actually an estimate of when the client will
2936  * renew. This way, the client will be able to get
2937  * the desired lease time upon renewal.
2938  */
2939  if (offer == DHCPACK) {
2940  if (lease_time == INFINITE_TIME) {
2941  lt->tstp = MAX_TIME;
2942  } else {
2943  lt->tstp =
2944  leaseTimeCheck(
2945  (cur_time + lease_time
2946  + (new_lease_time / 2)),
2947  MAX_TIME - 1);
2948  }
2949 
2950  /* If we reduced the potential expiry time,
2951  * make sure we don't offer an old-expiry-time
2952  * lease for this lease before the change is
2953  * ack'd.
2954  */
2955  if (lt->tstp < lt->tsfp)
2956  lt->tsfp = lt->tstp;
2957  } else
2958  lt->tstp = lease->tstp;
2959 
2960  /* Use failover-modified lease time. */
2961  lease_time = new_lease_time;
2962  }
2963 #endif /* FAILOVER_PROTOCOL */
2964 
2965  if (lease_time == INFINITE_TIME) {
2967  } else {
2968  /* If the lease duration causes the time value to wrap,
2969  use the maximum expiry time. */
2971  = leaseTimeCheck(cur_time + lease_time,
2972  MAX_TIME - 1);
2973  }
2974 
2975  if (when)
2976  lt -> ends = when;
2977  else
2978  lt -> ends = state -> offered_expiry;
2979 
2980  /* Don't make lease active until we actually get a
2981  DHCPREQUEST. */
2982  if (offer == DHCPACK)
2984  else
2986  } else {
2987  lt->flags |= BOOTP_LEASE;
2988 
2989  lease_time = MAX_TIME - cur_time;
2990 
2991  if ((oc = lookup_option (&server_universe, state -> options,
2993  if (evaluate_option_cache (&d1, packet, lease,
2994  (struct client_state *)0,
2995  packet -> options,
2996  state -> options,
2997  &lease -> scope, oc, MDL)) {
2998  if (d1.len == sizeof (u_int32_t))
2999  lease_time = getULong (d1.data);
3000  data_string_forget (&d1, MDL);
3001  }
3002  }
3003 
3004  if ((oc = lookup_option (&server_universe, state -> options,
3006  if (evaluate_option_cache (&d1, packet, lease,
3007  (struct client_state *)0,
3008  packet -> options,
3009  state -> options,
3010  &lease -> scope, oc, MDL)) {
3011  if (d1.len == sizeof (u_int32_t))
3012  lease_time = (getULong (d1.data) -
3013  cur_time);
3014  data_string_forget (&d1, MDL);
3015  }
3016  }
3017 
3018  lt -> ends = state -> offered_expiry = cur_time + lease_time;
3020  }
3021 
3022  /* Update Client Last Transaction Time. */
3023  lt->cltt = cur_time;
3024 
3025  /* See if we want to record the uid for this client */
3028  if ((oc == NULL) ||
3029  !evaluate_boolean_option_cache(&ignorep, packet, lease, NULL,
3031  &lease->scope, oc, MDL)) {
3032 
3033  /* Record the uid, if given... */
3034  oc = lookup_option (&dhcp_universe, packet -> options,
3036  if (oc &&
3037  evaluate_option_cache(&d1, packet, lease, NULL,
3039  &lease->scope, oc, MDL)) {
3040  if (d1.len <= sizeof(lt->uid_buf)) {
3041  memcpy(lt->uid_buf, d1.data, d1.len);
3042  lt->uid = lt->uid_buf;
3043  lt->uid_max = sizeof(lt->uid_buf);
3044  lt->uid_len = d1.len;
3045  } else {
3046  unsigned char *tuid;
3047  lt->uid_max = d1.len;
3048  lt->uid_len = d1.len;
3049  tuid = (unsigned char *)dmalloc(lt->uid_max,
3050  MDL);
3051  /* XXX inelegant */
3052  if (!tuid)
3053  log_fatal ("no memory for large uid.");
3054  memcpy(tuid, d1.data, lt->uid_len);
3055  lt->uid = tuid;
3056  }
3057  data_string_forget (&d1, MDL);
3058  }
3059  }
3060 
3061  if (host) {
3062  host_reference (&lt -> host, host, MDL);
3063  host_dereference (&host, MDL);
3064  }
3065  if (lease -> subnet)
3066  subnet_reference (&lt -> subnet, lease -> subnet, MDL);
3067  if (lease -> billing_class)
3068  class_reference (&lt -> billing_class,
3069  lease -> billing_class, MDL);
3070 
3071  /* Set a flag if this client is a broken client that NUL
3072  terminates string options and expects us to do likewise. */
3073  if (ms_nulltp)
3075  else
3077 
3078  /* Save any bindings. */
3079  if (lease -> scope) {
3082  }
3083  if (lease -> agent_options)
3085  lease -> agent_options, MDL);
3086 
3087  /* Save the vendor-class-identifier for DHCPLEASEQUERY. */
3090  if (oc != NULL &&
3091  evaluate_option_cache(&d1, packet, NULL, NULL, packet->options,
3092  NULL, &lt->scope, oc, MDL)) {
3093  if (d1.len != 0) {
3094  bind_ds_value(&lt->scope, "vendor-class-identifier",
3095  &d1);
3096  }
3097 
3098  data_string_forget(&d1, MDL);
3099  }
3100 
3101  /* If we got relay agent information options from the packet, then
3102  * cache them for renewal in case the relay agent can't supply them
3103  * when the client unicasts. The options may be from an addressed
3104  * "l3" relay, or from an unaddressed "l2" relay which does not set
3105  * giaddr.
3106  */
3107  if (!packet->agent_options_stashed &&
3108  (packet->options != NULL) &&
3111  oc = lookup_option (&server_universe, state -> options,
3113  if (!oc ||
3115  (struct client_state *)0,
3116  packet -> options,
3117  state -> options,
3118  &lease -> scope, oc, MDL)) {
3119  if (lt -> agent_options)
3122  (&lt -> agent_options,
3123  (struct option_chain_head *)
3124  packet -> options -> universes [agent_universe.index],
3125  MDL);
3126  }
3127  }
3128 
3129  /* Replace the old lease hostname with the new one, if it's changed. */
3130  oc = lookup_option (&dhcp_universe, packet -> options, DHO_HOST_NAME);
3131  if (oc)
3132  s1 = evaluate_option_cache (&d1, packet, (struct lease *)0,
3133  (struct client_state *)0,
3134  packet -> options,
3135  (struct option_state *)0,
3136  &global_scope, oc, MDL);
3137  else
3138  s1 = 0;
3139 
3140  if (oc && s1 &&
3141  lease -> client_hostname &&
3142  strlen (lease -> client_hostname) == d1.len &&
3143  !memcmp (lease -> client_hostname, d1.data, d1.len)) {
3144  /* Hasn't changed. */
3145  data_string_forget (&d1, MDL);
3147  lease -> client_hostname = (char *)0;
3148  } else if (oc && s1) {
3149  lt -> client_hostname = dmalloc (d1.len + 1, MDL);
3150  if (!lt -> client_hostname)
3151  log_error ("no memory for client hostname.");
3152  else {
3153  memcpy (lt -> client_hostname, d1.data, d1.len);
3154  lt -> client_hostname [d1.len] = 0;
3155  }
3156  data_string_forget (&d1, MDL);
3157  /* hostname changed, can't reuse lease */
3158  lease->cannot_reuse = 1;
3159  }
3160 
3161  /* Record the hardware address, if given... */
3162  lt -> hardware_addr.hlen = packet -> raw -> hlen + 1;
3163  lt -> hardware_addr.hbuf [0] = packet -> raw -> htype;
3164  memcpy (&lt -> hardware_addr.hbuf [1], packet -> raw -> chaddr,
3165  sizeof packet -> raw -> chaddr);
3166 
3167  /*
3168  * If client has requested the lease become infinite, then it
3169  * doens't qualify for reuse even if it's younger than the
3170  * dhcp-cache-threshold.
3171  */
3172  if ((lt->flags & RESERVED_LEASE) && !(lease->flags & RESERVED_LEASE)) {
3173  log_debug ("Cannot reuse: lease is changing to RESERVED");
3174  lease->cannot_reuse = 1;
3175  }
3176 
3177  lt->flags |= lease->flags & ~PERSISTENT_FLAGS;
3178 
3179  /* If there are statements to execute when the lease is
3180  committed, execute them. */
3181  if (lease->on_star.on_commit && (!offer || offer == DHCPACK)) {
3182  execute_statements (NULL, packet, lt, NULL, packet->options,
3183  state->options, &lt->scope,
3184  lease->on_star.on_commit, NULL);
3185  if (lease->on_star.on_commit)
3187  (&lease->on_star.on_commit, MDL);
3188  }
3189 
3190 #ifdef NSUPDATE
3191  /* Perform DDNS updates, if configured to. */
3192  if ((!offer || offer == DHCPACK) &&
3193  (!(oc = lookup_option (&server_universe, state -> options,
3194  SV_DDNS_UPDATES)) ||
3195  evaluate_boolean_option_cache (&ignorep, packet, lt,
3196  (struct client_state *)0,
3197  packet -> options,
3198  state -> options,
3199  &lt -> scope, oc, MDL))) {
3200  ddns_updates(packet, lt, lease, NULL, NULL, state->options);
3201  }
3202 #endif /* NSUPDATE */
3203 
3204  /* Don't call supersede_lease on a mocked-up lease. */
3205  if (lease -> flags & STATIC_LEASE) {
3206  /* Copy the hardware address into the static lease
3207  structure. */
3208  lease -> hardware_addr.hlen = packet -> raw -> hlen + 1;
3209  lease -> hardware_addr.hbuf [0] = packet -> raw -> htype;
3210  memcpy (&lease -> hardware_addr.hbuf [1],
3211  packet -> raw -> chaddr,
3212  sizeof packet -> raw -> chaddr); /* XXX */
3213  } else {
3214  int commit = (!offer || (offer == DHCPACK));
3215 
3216  /* If dhcp-cache-threshold is enabled, see if "lease" can
3217  * be reused. */
3218  use_old_lease = reuse_lease(packet, lt, lease, state, offer);
3219  if (use_old_lease == 1) {
3220  commit = 0;
3221  }
3222 
3223 #if !defined(DELAYED_ACK)
3224  /* Install the new information on 'lt' onto the lease at
3225  * 'lease'. If this is a DHCPOFFER, it is a 'soft' promise,
3226  * if it is a DHCPACK, it is a 'hard' binding, so it needs
3227  * to be recorded and propogated immediately. If the update
3228  * fails, don't ACK it (or BOOTREPLY) either; we may give
3229  * the same lease to another client later, and that would be
3230  * a conflict.
3231  */
3232  if ((use_old_lease == 0) &&
3233  !supersede_lease(lease, lt, commit,
3234  offer == DHCPACK, offer == DHCPACK, 0)) {
3235 #else /* defined(DELAYED_ACK) */
3236  /*
3237  * If there already isn't a need for a lease commit, and we
3238  * can just answer right away, set a flag to indicate this.
3239  */
3240  if (commit)
3241  enqueue = ISC_TRUE;
3242 
3243  /* Install the new information on 'lt' onto the lease at
3244  * 'lease'. We will not 'commit' this information to disk
3245  * yet (fsync()), we will 'propogate' the information if
3246  * this is BOOTP or a DHCPACK, but we will not 'pimmediate'ly
3247  * transmit failover binding updates (this is delayed until
3248  * after the fsync()). If the update fails, don't ACK it (or
3249  * BOOTREPLY either); we may give the same lease out to a
3250  * different client, and that would be a conflict.
3251  */
3252  if ((use_old_lease == 0) &&
3253  !supersede_lease(lease, lt, 0,
3254  !offer || offer == DHCPACK, 0, 0)) {
3255 #endif
3256  log_info ("%s: database update failed", msg);
3258  lease_dereference (&lt, MDL);
3259  return;
3260  }
3261  }
3262  lease_dereference (&lt, MDL);
3263 
3264  /* Remember the interface on which the packet arrived. */
3265  state -> ip = packet -> interface;
3266 
3267  /* Remember the giaddr, xid, secs, flags and hops. */
3268  state -> giaddr = packet -> raw -> giaddr;
3269  state -> ciaddr = packet -> raw -> ciaddr;
3270  state -> xid = packet -> raw -> xid;
3271  state -> secs = packet -> raw -> secs;
3272  state -> bootp_flags = packet -> raw -> flags;
3273  state -> hops = packet -> raw -> hops;
3274  state -> offer = offer;
3275 
3276  /* If we're always supposed to broadcast to this client, set
3277  the broadcast bit in the bootp flags field. */
3278  if ((oc = lookup_option (&server_universe, state -> options,
3279  SV_ALWAYS_BROADCAST)) &&
3281  (struct client_state *)0,
3282  packet -> options, state -> options,
3283  &lease -> scope, oc, MDL))
3284  state -> bootp_flags |= htons (BOOTP_BROADCAST);
3285 
3286  /* Get the Maximum Message Size option from the packet, if one
3287  was sent. */
3288  oc = lookup_option (&dhcp_universe, packet -> options,
3290  if (oc &&
3292  (struct client_state *)0,
3293  packet -> options, state -> options,
3294  &lease -> scope, oc, MDL)) {
3295  if (d1.len == sizeof (u_int16_t))
3296  state -> max_message_size = getUShort (d1.data);
3297  data_string_forget (&d1, MDL);
3298  } else {
3299  oc = lookup_option (&dhcp_universe, state -> options,
3301  if (oc &&
3303  (struct client_state *)0,
3304  packet -> options, state -> options,
3305  &lease -> scope, oc, MDL)) {
3306  if (d1.len == sizeof (u_int16_t))
3307  state -> max_message_size =
3308  getUShort (d1.data);
3309  data_string_forget (&d1, MDL);
3310  }
3311  }
3312 
3313  /* Get the Subnet Selection option from the packet, if one
3314  was sent. */
3315  if ((oc = lookup_option (&dhcp_universe, packet -> options,
3317 
3318  /* Make a copy of the data. */
3319  struct option_cache *noc = (struct option_cache *)0;
3320  if (option_cache_allocate (&noc, MDL)) {
3321  if (oc -> data.len)
3322  data_string_copy (&noc -> data,
3323  &oc -> data, MDL);
3324  if (oc -> expression)
3326  oc -> expression, MDL);
3327  if (oc -> option)
3328  option_reference(&(noc->option), oc->option,
3329  MDL);
3330 
3331  save_option (&dhcp_universe, state -> options, noc);
3332  option_cache_dereference (&noc, MDL);
3333  }
3334  }
3335 
3336  /* Now, if appropriate, put in DHCP-specific options that
3337  override those. */
3338  if (state -> offer) {
3340  oc = (struct option_cache *)0;
3341  if (option_cache_allocate (&oc, MDL)) {
3342  if (make_const_data (&oc -> expression,
3343  &state -> offer, 1, 0, 0, MDL)) {
3344  option_code_hash_lookup(&oc->option,
3346  &i, 0, MDL);
3348  state -> options, oc);
3349  }
3351  }
3352 
3353  get_server_source_address(&from, state->options,
3354  state->options, packet);
3355  memcpy(state->from.iabuf, &from, sizeof(from));
3356  state->from.len = sizeof(from);
3357 
3358  offered_lease_time =
3359  state -> offered_expiry - cur_time;
3360 
3361  putULong(state->expiry, (u_int32_t)offered_lease_time);
3362  i = DHO_DHCP_LEASE_TIME;
3363  oc = (struct option_cache *)0;
3364  if (option_cache_allocate (&oc, MDL)) {
3365  if (make_const_data(&oc->expression, state->expiry,
3366  4, 0, 0, MDL)) {
3367  option_code_hash_lookup(&oc->option,
3369  &i, 0, MDL);
3371  state -> options, oc);
3372  }
3374  }
3375 
3376  /*
3377  * Validate any configured renew or rebinding times against
3378  * the determined lease time. Do rebinding first so that
3379  * the renew time can be validated against the rebind time.
3380  */
3381  if ((oc = lookup_option(&dhcp_universe, state->options,
3382  DHO_DHCP_REBINDING_TIME)) != NULL &&
3383  evaluate_option_cache(&d1, packet, lease, NULL,
3384  packet->options, state->options,
3385  &lease->scope, oc, MDL)) {
3386  TIME rebind_time = getULong(d1.data);
3387 
3388  /* Drop the configured (invalid) rebinding time. */
3389  if (rebind_time >= offered_lease_time)
3392  else /* XXX: variable is reused. */
3393  offered_lease_time = rebind_time;
3394 
3395  data_string_forget(&d1, MDL);
3396  }
3397 
3398  if ((oc = lookup_option(&dhcp_universe, state->options,
3399  DHO_DHCP_RENEWAL_TIME)) != NULL &&
3400  evaluate_option_cache(&d1, packet, lease, NULL,
3401  packet->options, state->options,
3402  &lease->scope, oc, MDL)) {
3403  if (getULong(d1.data) >= offered_lease_time)
3406 
3407  data_string_forget(&d1, MDL);
3408  }
3409  } else {
3410  /* XXXSK: should we use get_server_source_address() here? */
3411  if (state -> ip -> address_count) {
3412  state -> from.len =
3413  sizeof state -> ip -> addresses [0];
3414  memcpy (state -> from.iabuf,
3415  &state -> ip -> addresses [0],
3416  state -> from.len);
3417  }
3418  }
3419 
3420  /* Figure out the address of the boot file server. */
3421  memset (&state -> siaddr, 0, sizeof state -> siaddr);
3422  if ((oc =
3424  state -> options, SV_NEXT_SERVER))) {
3425  if (evaluate_option_cache (&d1, packet, lease,
3426  (struct client_state *)0,
3427  packet -> options, state -> options,
3428  &lease -> scope, oc, MDL)) {
3429  /* If there was more than one answer,
3430  take the first. */
3431  if (d1.len >= 4 && d1.data)
3432  memcpy (&state -> siaddr, d1.data, 4);
3433  data_string_forget (&d1, MDL);
3434  }
3435  }
3436 
3437  /* Use the subnet mask from the subnet declaration if no other
3438  mask has been provided. */
3439  i = DHO_SUBNET_MASK;
3440  if (!lookup_option (&dhcp_universe, state -> options, i)) {
3441  oc = (struct option_cache *)0;
3442  if (option_cache_allocate (&oc, MDL)) {
3443  if (make_const_data (&oc -> expression,
3444  lease -> subnet -> netmask.iabuf,
3445  lease -> subnet -> netmask.len,
3446  0, 0, MDL)) {
3447  option_code_hash_lookup(&oc->option,
3449  &i, 0, MDL);
3451  state -> options, oc);
3452  }
3454  }
3455  }
3456 
3457  /* Use the name of the host declaration if there is one
3458  and no hostname has otherwise been provided, and if the
3459  use-host-decl-name flag is set. */
3461 
3462  /* Send client_id back if we received it and echo-client-id is on. */
3463  echo_client_id(packet, lease, state->options, state->options);
3464 
3465  /* If we don't have a hostname yet, and we've been asked to do
3466  a reverse lookup to find the hostname, do it. */
3467  i = DHO_HOST_NAME;
3469  if (!lookup_option(&dhcp_universe, state->options, i) &&
3471  (&ignorep, packet, lease, NULL,
3472  packet->options, state->options, &lease->scope,
3473  lookup_option (&server_universe, state->options, j), MDL)) {
3474  struct in_addr ia;
3475  struct hostent *h;
3476 
3477  memcpy (&ia, lease -> ip_addr.iabuf, 4);
3478 
3479  h = gethostbyaddr ((char *)&ia, sizeof ia, AF_INET);
3480  if (!h)
3481  log_error ("No hostname for %s", inet_ntoa (ia));
3482  else {
3483  oc = (struct option_cache *)0;
3484  if (option_cache_allocate (&oc, MDL)) {
3485  if (make_const_data (&oc -> expression,
3486  ((unsigned char *)
3487  h -> h_name),
3488  strlen (h -> h_name) + 1,
3489  1, 1, MDL)) {
3490  option_code_hash_lookup(&oc->option,
3492  &i, 0, MDL);
3494  state -> options, oc);
3495  }
3497  }
3498  }
3499  }
3500 
3501  /* If so directed, use the leased IP address as the router address.
3502  This supposedly makes Win95 machines ARP for all IP addresses,
3503  so if the local router does proxy arp, you win. */
3504 
3506  (&ignorep, packet, lease, (struct client_state *)0,
3507  packet -> options, state -> options, &lease -> scope,
3508  lookup_option (&server_universe, state -> options,
3510  i = DHO_ROUTERS;
3511  oc = lookup_option (&dhcp_universe, state -> options, i);
3512  if (!oc) {
3513  oc = (struct option_cache *)0;
3514  if (option_cache_allocate (&oc, MDL)) {
3515  if (make_const_data (&oc -> expression,
3516  lease -> ip_addr.iabuf,
3517  lease -> ip_addr.len,
3518  0, 0, MDL)) {
3519  option_code_hash_lookup(&oc->option,
3521  &i, 0, MDL);
3523  state -> options, oc);
3524  }
3525  option_cache_dereference (&oc, MDL);
3526  }
3527  }
3528  }
3529 
3530  /* If a site option space has been specified, use that for
3531  site option codes. */
3533  if ((oc = lookup_option (&server_universe, state -> options, i)) &&
3535  (struct client_state *)0,
3536  packet -> options, state -> options,
3537  &lease -> scope, oc, MDL)) {
3538  struct universe *u = (struct universe *)0;
3539 
3540  if (!universe_hash_lookup (&u, universe_hash,
3541  (const char *)d1.data, d1.len,
3542  MDL)) {
3543  log_error ("unknown option space %s.", d1.data);
3544  return;
3545  }
3546 
3547  state -> options -> site_universe = u -> index;
3548  state->options->site_code_min = find_min_site_code(u);
3549  data_string_forget (&d1, MDL);
3550  } else {
3551  state -> options -> site_code_min = 0;
3552  state -> options -> site_universe = dhcp_universe.index;
3553  }
3554 
3555  /* If the client has provided a list of options that it wishes
3556  returned, use it to prioritize. If there's a parameter
3557  request list in scope, use that in preference. Otherwise
3558  use the default priority list. */
3559 
3560  oc = lookup_option (&dhcp_universe, state -> options,
3562 
3563  if (!oc)
3564  oc = lookup_option (&dhcp_universe, packet -> options,
3566  if (oc)
3567  evaluate_option_cache (&state -> parameter_request_list,
3568  packet, lease, (struct client_state *)0,
3569  packet -> options, state -> options,
3570  &lease -> scope, oc, MDL);
3571 
3572 #ifdef DEBUG_PACKET
3573  dump_packet (packet);
3574  dump_raw ((unsigned char *)packet -> raw, packet -> packet_length);
3575 #endif
3576 
3577  lease -> state = state;
3578 
3579  log_info ("%s", msg);
3580 
3581  /* Hang the packet off the lease state. */
3582  packet_reference (&lease -> state -> packet, packet, MDL);
3583 
3584  /* If this is a DHCPOFFER, ping the lease address before actually
3585  sending the offer. */
3586  if (offer == DHCPOFFER && !(lease -> flags & STATIC_LEASE) &&
3587  (((cur_time - lease_cltt) > 60) ||
3589  (!(oc = lookup_option (&server_universe, state -> options,
3590  SV_PING_CHECKS)) ||
3592  (struct client_state *)0,
3593  packet -> options,
3594  state -> options,
3595  &lease -> scope, oc, MDL))) {
3597 
3598  /* Determine whether to use configured or default ping timeout.
3599  */
3600  if ((oc = lookup_option (&server_universe, state -> options,
3601  SV_PING_TIMEOUT)) &&
3602  evaluate_option_cache (&d1, packet, lease, NULL,
3603  packet -> options,
3604  state -> options,
3605  &lease -> scope, oc, MDL)) {
3606  if (d1.len == sizeof (u_int32_t))
3607  ping_timeout = getULong (d1.data);
3608  else
3609  ping_timeout = DEFAULT_PING_TIMEOUT;
3610 
3611  data_string_forget (&d1, MDL);
3612  } else
3613  ping_timeout = DEFAULT_PING_TIMEOUT;
3614 
3615 #ifdef DEBUG
3616  log_debug ("Ping timeout: %ld", (long)ping_timeout);
3617 #endif
3618 
3619  /*
3620  * Set a timeout for 'ping-timeout' seconds from NOW, including
3621  * current microseconds. As ping-timeout defaults to 1, the
3622  * exclusion of current microseconds causes a value somewhere
3623  * /between/ zero and one.
3624  */
3625  tv.tv_sec = cur_tv.tv_sec + ping_timeout;
3626  tv.tv_usec = cur_tv.tv_usec;
3628  (tvref_t)lease_reference,
3629  (tvunref_t)lease_dereference);
3631  } else {
3632  lease->cltt = cur_time;
3633 #if defined(DELAYED_ACK)
3634  if (enqueue)
3635  delayed_ack_enqueue(lease);
3636  else
3637 #endif
3638  dhcp_reply(lease);
3639  }
3640 }
3641 
3642 #if defined(DELAYED_ACK)
3643 
3644 /*
3645  * CC: queue single ACK:
3646  * - write the lease (but do not fsync it yet)
3647  * - add to double linked list
3648  * - commit if more than xx ACKs pending
3649  * - if necessary set the max timer and bump the next timer
3650  * but only up to the max timer value.
3651  */
3652 
3653 static void
3654 delayed_ack_enqueue(struct lease *lease)
3655 {
3656  struct leasequeue *q;
3657 
3658  if (!write_lease(lease))
3659  return;
3660  if (free_ackqueue) {
3661  q = free_ackqueue;
3662  free_ackqueue = q->next;
3663  } else {
3664  q = ((struct leasequeue *)
3665  dmalloc(sizeof(struct leasequeue), MDL));
3666  if (!q)
3667  log_fatal("delayed_ack_enqueue: no memory!");
3668  }
3669  memset(q, 0, sizeof *q);
3670  /* prepend to ackqueue*/
3671  lease_reference(&q->lease, lease, MDL);
3672  q->next = ackqueue_head;
3673  ackqueue_head = q;
3674  if (!ackqueue_tail)
3675  ackqueue_tail = q;
3676  else
3677  q->next->prev = q;
3678 
3679  outstanding_acks++;
3680  if (outstanding_acks > max_outstanding_acks) {
3681  /* Cancel any pending timeout and call handler directly */
3682  cancel_timeout(delayed_acks_timer, NULL);
3683  delayed_acks_timer(NULL);
3684  } else {
3685  struct timeval next_fsync;
3686 
3687  if (max_fsync.tv_sec == 0 && max_fsync.tv_usec == 0) {
3688  /* set the maximum time we'll wait */
3689  max_fsync.tv_sec = cur_tv.tv_sec + max_ack_delay_secs;
3690  max_fsync.tv_usec = cur_tv.tv_usec +
3692 
3693  if (max_fsync.tv_usec >= 1000000) {
3694  max_fsync.tv_sec++;
3695  max_fsync.tv_usec -= 1000000;
3696  }
3697  }
3698 
3699  /* Set the timeout */
3700  next_fsync.tv_sec = cur_tv.tv_sec;
3701  next_fsync.tv_usec = cur_tv.tv_usec + min_ack_delay_usecs;
3702  if (next_fsync.tv_usec >= 1000000) {
3703  next_fsync.tv_sec++;
3704  next_fsync.tv_usec -= 1000000;
3705  }
3706  /* but not more than the max */
3707  if ((next_fsync.tv_sec > max_fsync.tv_sec) ||
3708  ((next_fsync.tv_sec == max_fsync.tv_sec) &&
3709  (next_fsync.tv_usec > max_fsync.tv_usec))) {
3710  next_fsync.tv_sec = max_fsync.tv_sec;
3711  next_fsync.tv_usec = max_fsync.tv_usec;
3712  }
3713 
3714  add_timeout(&next_fsync, delayed_acks_timer, NULL,
3715  (tvref_t) NULL, (tvunref_t) NULL);
3716  }
3717 }
3718 
3719 /* Processes any delayed acks:
3720  * Commits the leases and then for each delayed ack:
3721  * - Update the failover peer if we're in failover
3722  * - Send the REPLY to the client
3723  */
3724 static void
3725 delayed_acks_timer(void *foo)
3726 {
3727  struct leasequeue *ack, *p;
3728 
3729  /* Reset max fsync */
3730  memset(&max_fsync, 0, sizeof(max_fsync));
3731 
3732  if (!outstanding_acks) {
3733  /* Nothing to do, so punt, shouldn't happen? */
3734  return;
3735  }
3736 
3737  /* Commit the leases first */
3738  commit_leases();
3739 
3740  /* Now process the delayed ACKs
3741  - update failover peer
3742  - send out the ACK packets
3743  - move the queue slots to the free list
3744  */
3745 
3746  /* process from bottom to retain packet order */
3747  for (ack = ackqueue_tail ; ack ; ack = p) {
3748  p = ack->prev;
3749 
3750 #if defined(FAILOVER_PROTOCOL)
3751  /* If we're in failover we need to send any deferred
3752  * bind updates as well as the replies */
3753  if (ack->lease->pool) {
3754  dhcp_failover_state_t *fpeer;
3755 
3756  fpeer = ack->lease->pool->failover_peer;
3757  if (fpeer && fpeer->link_to_peer) {
3759  }
3760  }
3761 #endif
3762 
3763  /* dhcp_reply() requires that the reply state still be valid */
3764  if (ack->lease->state == NULL)
3765  log_error("delayed ack for %s has gone stale",
3766  piaddr(ack->lease->ip_addr));
3767  else {
3768  dhcp_reply(ack->lease);
3769  }
3770 
3771  lease_dereference(&ack->lease, MDL);
3772  ack->next = free_ackqueue;
3773  free_ackqueue = ack;
3774  }
3775 
3776  ackqueue_head = NULL;
3777  ackqueue_tail = NULL;
3778  outstanding_acks = 0;
3779 }
3780 
3781 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
3782 void
3783 relinquish_ackqueue(void)
3784 {
3785  struct leasequeue *q, *n;
3786 
3787  for (q = ackqueue_head ; q ; q = n) {
3788  n = q->next;
3789  dfree(q, MDL);
3790  }
3791  for (q = free_ackqueue ; q ; q = n) {
3792  n = q->next;
3793  dfree(q, MDL);
3794  }
3795 }
3796 #endif
3797 
3798 #endif /* defined(DELAYED_ACK) */
3799 
3801  struct lease *lease;
3802 {
3803  int bufs = 0;
3804  unsigned packet_length;
3805  struct dhcp_packet raw;
3806  struct sockaddr_in to;
3807  struct in_addr from;
3808  struct hardware hto;
3809  int result;
3810  struct lease_state *state = lease -> state;
3811  int nulltp, bootpp, unicastp = 1;
3812 #if defined(RELAY_PORT)
3813  u_int16_t relay_port = 0;
3814 #endif
3815  struct data_string d1;
3816  const char *s;
3817 
3818  if (!state)
3819  log_fatal ("dhcp_reply was supplied lease with no state!");
3820 
3821  /* Compose a response for the client... */
3822  memset (&raw, 0, sizeof raw);
3823  memset (&d1, 0, sizeof d1);
3824 
3825  /* Copy in the filename if given; otherwise, flag the filename
3826  buffer as available for options. */
3827  if (state -> filename.len && state -> filename.data) {
3828  memcpy (raw.file,
3829  state -> filename.data,
3830  state -> filename.len > sizeof raw.file
3831  ? sizeof raw.file : state -> filename.len);
3832  if (sizeof raw.file > state -> filename.len)
3833  memset (&raw.file [state -> filename.len], 0,
3834  (sizeof raw.file) - state -> filename.len);
3835  else
3836  log_info("file name longer than packet field "
3837  "truncated - field: %lu name: %d %.*s",
3838  (unsigned long)sizeof(raw.file),
3839  state->filename.len, (int)state->filename.len,
3840  state->filename.data);
3841  } else
3842  bufs |= 1;
3843 
3844  /* Copy in the server name if given; otherwise, flag the
3845  server_name buffer as available for options. */
3846  if (state -> server_name.len && state -> server_name.data) {
3847  memcpy (raw.sname,
3848  state -> server_name.data,
3849  state -> server_name.len > sizeof raw.sname
3850  ? sizeof raw.sname : state -> server_name.len);
3851  if (sizeof raw.sname > state -> server_name.len)
3852  memset (&raw.sname [state -> server_name.len], 0,
3853  (sizeof raw.sname) - state -> server_name.len);
3854  else
3855  log_info("server name longer than packet field "
3856  "truncated - field: %lu name: %d %.*s",
3857  (unsigned long)sizeof(raw.sname),
3858  state->server_name.len,
3859  (int)state->server_name.len,
3860  state->server_name.data);
3861  } else
3862  bufs |= 2; /* XXX */
3863 
3864  memcpy (raw.chaddr,
3865  &lease -> hardware_addr.hbuf [1], sizeof raw.chaddr);
3866  raw.hlen = lease -> hardware_addr.hlen - 1;
3867  raw.htype = lease -> hardware_addr.hbuf [0];
3868 
3869  /* See if this is a Microsoft client that NUL-terminates its
3870  strings and expects us to do likewise... */
3871  if (lease -> flags & MS_NULL_TERMINATION)
3872  nulltp = 1;
3873  else
3874  nulltp = 0;
3875 
3876  /* See if this is a bootp client... */
3877  if (state -> offer)
3878  bootpp = 0;
3879  else
3880  bootpp = 1;
3881 
3882  /* Insert such options as will fit into the buffer. */
3883  packet_length = cons_options (state -> packet, &raw, lease,
3884  (struct client_state *)0,
3885  state -> max_message_size,
3886  state -> packet -> options,
3887  state -> options, &global_scope,
3888  bufs, nulltp, bootpp,
3889  &state -> parameter_request_list,
3890  (char *)0);
3891 
3892  memcpy (&raw.ciaddr, &state -> ciaddr, sizeof raw.ciaddr);
3893  memcpy (&raw.yiaddr, lease -> ip_addr.iabuf, 4);
3894  raw.siaddr = state -> siaddr;
3895  raw.giaddr = state -> giaddr;
3896 
3897  raw.xid = state -> xid;
3898  raw.secs = state -> secs;
3899  raw.flags = state -> bootp_flags;
3900  raw.hops = state -> hops;
3901  raw.op = BOOTREPLY;
3902 
3903  if (lease -> client_hostname) {
3904  if ((strlen (lease -> client_hostname) <= 64) &&
3905  db_printable((unsigned char *)lease->client_hostname))
3906  s = lease -> client_hostname;
3907  else
3908  s = "Hostname Unsuitable for Printing";
3909  } else
3910  s = (char *)0;
3911 
3912  /* Make sure outgoing packets are at least as big
3913  as a BOOTP packet. */
3914  if (packet_length < BOOTP_MIN_LEN)
3915  packet_length = BOOTP_MIN_LEN;
3916 
3917 #if defined(DHCPv6) && defined(DHCP4o6)
3918  if (dhcpv4_over_dhcpv6 && (state->packet->dhcp4o6_response != NULL)) {
3919  /* Say what we're doing... */
3920  log_info ("DHCP4o6 %s on %s to %s %s%s%svia %s",
3921  (state -> offer
3922  ? (state -> offer == DHCPACK
3923  ? "DHCPACK" : "DHCPOFFER")
3924  : "BOOTREPLY"),
3925  piaddr (lease -> ip_addr),
3926  (lease -> hardware_addr.hlen
3927  ? print_hw_addr (lease -> hardware_addr.hbuf [0],
3928  lease -> hardware_addr.hlen - 1,
3929  &lease -> hardware_addr.hbuf [1])
3930  : print_hex_1(lease->uid_len, lease->uid, 60)),
3931  s ? "(" : "", s ? s : "", s ? ") " : "",
3932  piaddr(state->packet->client_addr));
3933 
3934  /* fill dhcp4o6_response */
3935  state->packet->dhcp4o6_response->len = packet_length;
3936  state->packet->dhcp4o6_response->buffer = NULL;
3938  packet_length, MDL)) {
3939  log_fatal("No memory to store DHCP4o6 reply.");
3940  }
3941  state->packet->dhcp4o6_response->data =
3942  state->packet->dhcp4o6_response->buffer->data;
3943  memcpy(state->packet->dhcp4o6_response->buffer->data,
3944  &raw, packet_length);
3945 
3946  /* done */
3947  free_lease_state (state, MDL);
3948  lease -> state = (struct lease_state *)0;
3949 
3950  return;
3951  }
3952 #endif
3953 
3954  /* Say what we're doing... */
3955  log_info ("%s on %s to %s %s%s%svia %s",
3956  (state -> offer
3957  ? (state -> offer == DHCPACK ? "DHCPACK" : "DHCPOFFER")
3958  : "BOOTREPLY"),
3959  piaddr (lease -> ip_addr),
3960  (lease -> hardware_addr.hlen > 1
3961  ? print_hw_addr (lease -> hardware_addr.hbuf [0],
3962  lease -> hardware_addr.hlen - 1,
3963  &lease -> hardware_addr.hbuf [1])
3964  : print_hex_1(lease->uid_len, lease->uid, 60)),
3965  s ? "(" : "", s ? s : "", s ? ") " : "",
3966  (state -> giaddr.s_addr
3967  ? inet_ntoa (state -> giaddr)
3968  : state -> ip -> name));
3969 
3970 #ifdef DEBUG_PACKET
3971  dump_raw ((unsigned char *)&raw, packet_length);
3972 #endif
3973 
3974  /* Set up the hardware address... */
3975  hto.hlen = lease -> hardware_addr.hlen;
3976  memcpy (hto.hbuf, lease -> hardware_addr.hbuf, hto.hlen);
3977 
3978  to.sin_family = AF_INET;
3979 #ifdef HAVE_SA_LEN
3980  to.sin_len = sizeof to;
3981 #endif
3982  memset (to.sin_zero, 0, sizeof to.sin_zero);
3983 
3984 #if defined(RELAY_PORT)
3986 #endif
3987 
3988  /* If this was gatewayed, send it back to the gateway... */
3989  if (raw.giaddr.s_addr) {
3990  to.sin_addr = raw.giaddr;
3991  if (raw.giaddr.s_addr != htonl (INADDR_LOOPBACK))
3992 #if defined(RELAY_PORT)
3993  to.sin_port = relay_port ? relay_port : local_port;
3994 #else
3995  to.sin_port = local_port;
3996 #endif
3997  else
3998  to.sin_port = remote_port; /* For debugging. */
3999 
4000  if (fallback_interface) {
4001  result = send_packet(fallback_interface, NULL, &raw,
4002  packet_length, raw.siaddr, &to,
4003  NULL);
4004  if (result < 0) {
4005  log_error ("%s:%d: Failed to send %d byte long "
4006  "packet over %s interface.", MDL,
4007  packet_length,
4009  }
4010 
4011 
4012  free_lease_state (state, MDL);
4013  lease -> state = (struct lease_state *)0;
4014  return;
4015  }
4016 
4017  /* If the client is RENEWING, unicast to the client using the
4018  regular IP stack. Some clients, particularly those that
4019  follow RFC1541, are buggy, and send both ciaddr and server
4020  identifier. We deal with this situation by assuming that
4021  if we got both dhcp-server-identifier and ciaddr, and
4022  giaddr was not set, then the client is on the local
4023  network, and we can therefore unicast or broadcast to it
4024  successfully. A client in REQUESTING state on another
4025  network that's making this mistake will have set giaddr,
4026  and will therefore get a relayed response from the above
4027  code. */
4028  } else if (raw.ciaddr.s_addr &&
4029  !((state -> got_server_identifier ||
4030  (raw.flags & htons (BOOTP_BROADCAST))) &&
4031  /* XXX This won't work if giaddr isn't zero, but it is: */
4032  (state -> shared_network ==
4033  lease -> subnet -> shared_network)) &&
4034  state -> offer == DHCPACK) {
4035  to.sin_addr = raw.ciaddr;
4036  to.sin_port = remote_port;
4037 
4038  if (fallback_interface) {
4039  result = send_packet(fallback_interface, NULL, &raw,
4040  packet_length, raw.siaddr, &to,
4041  NULL);
4042  if (result < 0) {
4043  log_error("%s:%d: Failed to send %d byte long"
4044  " packet over %s interface.", MDL,
4045  packet_length,
4047  }
4048 
4049  free_lease_state (state, MDL);
4050  lease -> state = (struct lease_state *)0;
4051  return;
4052  }
4053 
4054  /* If it comes from a client that already knows its address
4055  and is not requesting a broadcast response, and we can
4056  unicast to a client without using the ARP protocol, sent it
4057  directly to that client. */
4058  } else if (!(raw.flags & htons (BOOTP_BROADCAST)) &&
4059  can_unicast_without_arp (state -> ip)) {
4060  to.sin_addr = raw.yiaddr;
4061  to.sin_port = remote_port;
4062 
4063  /* Otherwise, broadcast it on the local network. */
4064  } else {
4065  to.sin_addr = limited_broadcast;
4066  to.sin_port = remote_port;
4067  if (!(lease -> flags & UNICAST_BROADCAST_HACK))
4068  unicastp = 0;
4069  }
4070 
4071  memcpy (&from, state -> from.iabuf, sizeof from);
4072 
4073  result = send_packet(state->ip, NULL, &raw, packet_length,
4074  from, &to, unicastp ? &hto : NULL);
4075  if (result < 0) {
4076  log_error ("%s:%d: Failed to send %d byte long "
4077  "packet over %s interface.", MDL,
4078  packet_length, state->ip->name);
4079  }
4080 
4081 
4082  /* Free all of the entries in the option_state structure
4083  now that we're done with them. */
4084 
4085  free_lease_state (state, MDL);
4086  lease -> state = (struct lease_state *)0;
4087 }
4088 
4089 int find_lease (struct lease **lp,
4090  struct packet *packet, struct shared_network *share, int *ours,
4091  int *peer_has_leases, struct lease *ip_lease_in,
4092  const char *file, int line)
4093 {
4094  struct lease *uid_lease = (struct lease *)0;
4095  struct lease *ip_lease = (struct lease *)0;
4096  struct lease *hw_lease = (struct lease *)0;
4097  struct lease *lease = (struct lease *)0;
4098  struct iaddr cip;
4099  struct host_decl *hp = (struct host_decl *)0;
4100  struct host_decl *host = (struct host_decl *)0;
4101  struct lease *fixed_lease = (struct lease *)0;
4102  struct lease *next = (struct lease *)0;
4103  struct option_cache *oc;
4104  struct data_string d1;
4105  int have_client_identifier = 0;
4106  struct data_string client_identifier;
4107  struct hardware h;
4108 
4109 #if defined(FAILOVER_PROTOCOL)
4110  /* Quick check to see if the peer has leases. */
4111  if (peer_has_leases) {
4112  struct pool *pool;
4113 
4114  for (pool = share->pools ; pool ; pool = pool->next) {
4115  dhcp_failover_state_t *peer = pool->failover_peer;
4116 
4117  if (peer &&
4118  ((peer->i_am == primary && pool->backup_leases) ||
4119  (peer->i_am == secondary && pool->free_leases))) {
4120  *peer_has_leases = 1;
4121  break;
4122  }
4123  }
4124  }
4125 #endif /* FAILOVER_PROTOCOL */
4126 
4127  if (packet -> raw -> ciaddr.s_addr) {
4128  cip.len = 4;
4129  memcpy (cip.iabuf, &packet -> raw -> ciaddr, 4);
4130  } else {
4131  /* Look up the requested address. */
4132  oc = lookup_option (&dhcp_universe, packet -> options,
4134  memset (&d1, 0, sizeof d1);
4135  if (oc &&
4136  evaluate_option_cache (&d1, packet, (struct lease *)0,
4137  (struct client_state *)0,
4138  packet -> options,
4139  (struct option_state *)0,
4140  &global_scope, oc, MDL)) {
4141  packet -> got_requested_address = 1;
4142  cip.len = 4;
4143  memcpy (cip.iabuf, d1.data, cip.len);
4144  data_string_forget (&d1, MDL);
4145  } else
4146  cip.len = 0;
4147  }
4148 
4149  /* Try to find a host or lease that's been assigned to the
4150  specified unique client identifier. */
4151  oc = lookup_option (&dhcp_universe, packet -> options,
4153  memset (&client_identifier, 0, sizeof client_identifier);
4154  if (oc &&
4155  evaluate_option_cache (&client_identifier,
4156  packet, (struct lease *)0,
4157  (struct client_state *)0,
4158  packet -> options, (struct option_state *)0,
4159  &global_scope, oc, MDL)) {
4160  /* Remember this for later. */
4161  have_client_identifier = 1;
4162 
4163  /* First, try to find a fixed host entry for the specified
4164  client identifier... */
4165  if (find_hosts_by_uid (&hp, client_identifier.data,
4166  client_identifier.len, MDL)) {
4167  /* Remember if we know of this client. */
4168  packet -> known = 1;
4169  mockup_lease (&fixed_lease, packet, share, hp);
4170  }
4171 
4172 #if defined (DEBUG_FIND_LEASE)
4173  if (fixed_lease) {
4174  log_info ("Found host for client identifier: %s.",
4175  piaddr (fixed_lease -> ip_addr));
4176  }
4177 #endif
4178  if (hp) {
4179  if (!fixed_lease) /* Save the host if we found one. */
4180  host_reference (&host, hp, MDL);
4181  host_dereference (&hp, MDL);
4182  }
4183 
4184  find_lease_by_uid (&uid_lease, client_identifier.data,
4185  client_identifier.len, MDL);
4186  }
4187 
4188  /* If we didn't find a fixed lease using the uid, try doing
4189  it with the hardware address... */
4190  if (!fixed_lease && !host) {
4191  if (find_hosts_by_haddr (&hp, packet -> raw -> htype,
4192  packet -> raw -> chaddr,
4193  packet -> raw -> hlen, MDL)) {
4194  /* Remember if we know of this client. */
4195  packet -> known = 1;
4196  if (host)
4197  host_dereference (&host, MDL);
4198  host_reference (&host, hp, MDL);
4199  host_dereference (&hp, MDL);
4200  mockup_lease (&fixed_lease, packet, share, host);
4201 #if defined (DEBUG_FIND_LEASE)
4202  if (fixed_lease) {
4203  log_info ("Found host for link address: %s.",
4204  piaddr (fixed_lease -> ip_addr));
4205  }
4206 #endif
4207  }
4208  }
4209 
4210  /* Finally, if we haven't found anything yet try again with the
4211  * host-identifier option ... */
4212  if (!fixed_lease && !host) {
4213  if (find_hosts_by_option(&hp, packet,
4214  packet->options, MDL) == 1) {
4215  packet->known = 1;
4216  if (host)
4217  host_dereference(&host, MDL);
4218  host_reference(&host, hp, MDL);
4219  host_dereference(&hp, MDL);
4220  mockup_lease (&fixed_lease, packet, share, host);
4221 #if defined (DEBUG_FIND_LEASE)
4222  if (fixed_lease) {
4223  log_info ("Found host via host-identifier");
4224  }
4225 #endif
4226  }
4227  }
4228 
4229  /* If fixed_lease is present but does not match the requested
4230  IP address, and this is a DHCPREQUEST, then we can't return
4231  any other lease, so we might as well return now. */
4232  if (packet -> packet_type == DHCPREQUEST && fixed_lease &&
4233  (fixed_lease -> ip_addr.len != cip.len ||
4234  memcmp (fixed_lease -> ip_addr.iabuf,
4235  cip.iabuf, cip.len))) {
4236  if (ours)
4237  *ours = 1;
4238  strcpy (dhcp_message, "requested address is incorrect");
4239 #if defined (DEBUG_FIND_LEASE)
4240  log_info ("Client's fixed-address %s doesn't match %s%s",
4241  piaddr (fixed_lease -> ip_addr), "request ",
4242  print_dotted_quads (cip.len, cip.iabuf));
4243 #endif
4244  goto out;
4245  }
4246 
4247  /*
4248  * If we found leases matching the client identifier, loop through
4249  * the n_uid pointer looking for one that's actually valid. We
4250  * can't do this until we get here because we depend on
4251  * packet -> known, which may be set by either the uid host
4252  * lookup or the haddr host lookup.
4253  *
4254  * Note that the n_uid lease chain is sorted in order of
4255  * preference, so the first one is the best one.
4256  */
4257  while (uid_lease) {
4258  isc_boolean_t do_release = !packet->raw->ciaddr.s_addr;
4259 #if defined (DEBUG_FIND_LEASE)
4260  log_info ("trying next lease matching client id: %s",
4261  piaddr (uid_lease -> ip_addr));
4262 #endif
4263 
4264 #if defined (FAILOVER_PROTOCOL)
4265  /*
4266  * When we lookup a lease by uid, we know the client identifier
4267  * matches the lease's record. If it is active, or was last
4268  * active with the same client, we can trivially extend it.
4269  * If is not or was not active, we can allocate it to this
4270  * client if it matches the usual free/backup criteria (which
4271  * is contained in lease_mine_to_reallocate()).
4272  */
4273  if (uid_lease->binding_state != FTS_ACTIVE &&
4274  uid_lease->rewind_binding_state != FTS_ACTIVE &&
4275  !lease_mine_to_reallocate(uid_lease)) {
4276 #if defined (DEBUG_FIND_LEASE)
4277  log_info("not active or not mine to allocate: %s",
4278  piaddr(uid_lease->ip_addr));
4279 #endif
4280  goto n_uid;
4281  }
4282 #endif
4283 
4284  if (uid_lease -> subnet -> shared_network != share) {
4285 #if defined (DEBUG_FIND_LEASE)
4286  log_info ("wrong network segment: %s",
4287  piaddr (uid_lease -> ip_addr));
4288 #endif
4289  /* Allow multiple leases using the same UID
4290  on different subnetworks. */
4291  do_release = ISC_FALSE;
4292  goto n_uid;
4293  }
4294 
4295  if ((uid_lease -> pool -> prohibit_list &&
4296  permitted (packet, uid_lease -> pool -> prohibit_list)) ||
4297  (uid_lease -> pool -> permit_list &&
4298  !permitted (packet, uid_lease -> pool -> permit_list))) {
4299 #if defined (DEBUG_FIND_LEASE)
4300  log_info ("not permitted: %s",
4301  piaddr (uid_lease -> ip_addr));
4302 #endif
4303  n_uid:
4304  if (uid_lease -> n_uid)
4305  lease_reference (&next,
4306  uid_lease -> n_uid, MDL);
4307  if (do_release)
4308  release_lease (uid_lease, packet);
4309  lease_dereference (&uid_lease, MDL);
4310  if (next) {
4311  lease_reference (&uid_lease, next, MDL);
4312  lease_dereference (&next, MDL);
4313  }
4314  continue;
4315  }
4316  break;
4317  }
4318 #if defined (DEBUG_FIND_LEASE)
4319  if (uid_lease)
4320  log_info ("Found lease for client id: %s.",
4321  piaddr (uid_lease -> ip_addr));
4322 #endif
4323 
4324  /* Find a lease whose hardware address matches, whose client
4325  * identifier matches (or equally doesn't have one), that's
4326  * permitted, and that's on the correct subnet.
4327  *
4328  * Note that the n_hw chain is sorted in order of preference, so
4329  * the first one found is the best one.
4330  */
4331  h.hlen = packet -> raw -> hlen + 1;
4332  h.hbuf [0] = packet -> raw -> htype;
4333  memcpy (&h.hbuf [1], packet -> raw -> chaddr, packet -> raw -> hlen);
4334  find_lease_by_hw_addr (&hw_lease, h.hbuf, h.hlen, MDL);
4335  while (hw_lease) {
4336 #if defined (DEBUG_FIND_LEASE)
4337  log_info ("trying next lease matching hw addr: %s",
4338  piaddr (hw_lease -> ip_addr));
4339 #endif
4340 #if defined (FAILOVER_PROTOCOL)
4341  /*
4342  * When we lookup a lease by chaddr, we know the MAC address
4343  * matches the lease record (we will check if the lease has a
4344  * client-id the client does not next). If the lease is
4345  * currently active or was last active with this client, we can
4346  * trivially extend it. Otherwise, there are a set of rules
4347  * that govern if we can reallocate this lease to any client
4348  * ("lease_mine_to_reallocate()") including this one.
4349  */
4350  if (hw_lease->binding_state != FTS_ACTIVE &&
4351  hw_lease->rewind_binding_state != FTS_ACTIVE &&
4352  !lease_mine_to_reallocate(hw_lease)) {
4353 #if defined (DEBUG_FIND_LEASE)
4354  log_info("not active or not mine to allocate: %s",
4355  piaddr(hw_lease->ip_addr));
4356 #endif
4357  goto n_hw;
4358  }
4359 #endif
4360 
4361  /*
4362  * This conditional skips "potentially active" leases (leases
4363  * we think are expired may be extended by the peer, etc) that
4364  * may be assigned to a differently /client-identified/ client
4365  * with the same MAC address.
4366  */
4367  if (hw_lease -> binding_state != FTS_FREE &&
4368  hw_lease -> binding_state != FTS_BACKUP &&
4369  hw_lease -> uid &&
4370  (!have_client_identifier ||
4371  hw_lease -> uid_len != client_identifier.len ||
4372  memcmp (hw_lease -> uid, client_identifier.data,
4373  hw_lease -> uid_len))) {
4374 #if defined (DEBUG_FIND_LEASE)
4375  log_info ("wrong client identifier: %s",
4376  piaddr (hw_lease -> ip_addr));
4377 #endif
4378  goto n_hw;
4379  }
4380  if (hw_lease -> subnet -> shared_network != share) {
4381 #if defined (DEBUG_FIND_LEASE)
4382  log_info ("wrong network segment: %s",
4383  piaddr (hw_lease -> ip_addr));
4384 #endif
4385  goto n_hw;
4386  }
4387  if ((hw_lease -> pool -> prohibit_list &&
4388  permitted (packet, hw_lease -> pool -> prohibit_list)) ||
4389  (hw_lease -> pool -> permit_list &&
4390  !permitted (packet, hw_lease -> pool -> permit_list))) {
4391 #if defined (DEBUG_FIND_LEASE)
4392  log_info ("not permitted: %s",
4393  piaddr (hw_lease -> ip_addr));
4394 #endif
4395  if (!packet -> raw -> ciaddr.s_addr)
4396  release_lease (hw_lease, packet);
4397  n_hw:
4398  if (hw_lease -> n_hw)
4399  lease_reference (&next, hw_lease -> n_hw, MDL);
4400  lease_dereference (&hw_lease, MDL);
4401  if (next) {
4402  lease_reference (&hw_lease, next, MDL);
4403  lease_dereference (&next, MDL);
4404  }
4405  continue;
4406  }
4407  break;
4408  }
4409 #if defined (DEBUG_FIND_LEASE)
4410  if (hw_lease)
4411  log_info ("Found lease for hardware address: %s.",
4412  piaddr (hw_lease -> ip_addr));
4413 #endif
4414 
4415  /* Try to find a lease that's been allocated to the client's
4416  IP address. */
4417  if (ip_lease_in)
4418  lease_reference (&ip_lease, ip_lease_in, MDL);
4419  else if (cip.len)
4420  find_lease_by_ip_addr (&ip_lease, cip, MDL);
4421 
4422 #if defined (DEBUG_FIND_LEASE)
4423  if (ip_lease)
4424  log_info ("Found lease for requested address: %s.",
4425  piaddr (ip_lease -> ip_addr));
4426 #endif
4427 
4428  /* If ip_lease is valid at this point, set ours to one, so that
4429  even if we choose a different lease, we know that the address
4430  the client was requesting was ours, and thus we can NAK it. */
4431  if (ip_lease && ours)
4432  *ours = 1;
4433 
4434  /* If the requested IP address isn't on the network the packet
4435  came from, don't use it. Allow abandoned leases to be matched
4436  here - if the client is requesting it, there's a decent chance
4437  that it's because the lease database got trashed and a client
4438  that thought it had this lease answered an ARP or PING, causing the
4439  lease to be abandoned. If so, this request probably came from
4440  that client. */
4441  if (ip_lease && (ip_lease -> subnet -> shared_network != share)) {
4442  if (ours)
4443  *ours = 1;
4444 #if defined (DEBUG_FIND_LEASE)
4445  log_info ("...but it was on the wrong shared network.");
4446 #endif
4447  strcpy (dhcp_message, "requested address on bad subnet");
4448  lease_dereference (&ip_lease, MDL);
4449  }
4450 
4451  /*
4452  * If the requested address is in use (or potentially in use) by
4453  * a different client, it can't be granted.
4454  *
4455  * This first conditional only detects if the lease is currently
4456  * identified to a different client (client-id and/or chaddr
4457  * mismatch). In this case we may not want to give the client the
4458  * lease, if doing so may potentially be an addressing conflict.
4459  */
4460  if (ip_lease &&
4461  (ip_lease -> uid ?
4462  (!have_client_identifier ||
4463  ip_lease -> uid_len != client_identifier.len ||
4464  memcmp (ip_lease -> uid, client_identifier.data,
4465  ip_lease -> uid_len)) :
4466  (ip_lease -> hardware_addr.hbuf [0] != packet -> raw -> htype ||
4467  ip_lease -> hardware_addr.hlen != packet -> raw -> hlen + 1 ||
4468  memcmp (&ip_lease -> hardware_addr.hbuf [1],
4469  packet -> raw -> chaddr,
4470  (unsigned)(ip_lease -> hardware_addr.hlen - 1))))) {
4471  /*
4472  * A lease is unavailable for allocation to a new client if
4473  * it is not in the FREE or BACKUP state. There may be
4474  * leases that are in the expired state with a rewinding
4475  * state that is free or backup, but these will be processed
4476  * into the free or backup states by expiration processes, so
4477  * checking for them here is superfluous.
4478  */
4479  if (ip_lease -> binding_state != FTS_FREE &&
4480  ip_lease -> binding_state != FTS_BACKUP) {
4481 #if defined (DEBUG_FIND_LEASE)
4482  log_info ("rejecting lease for requested address.");
4483 #endif
4484  /* If we're rejecting it because the peer has
4485  it, don't set "ours", because we shouldn't NAK. */
4486  if (ours && ip_lease -> binding_state != FTS_ACTIVE)
4487  *ours = 0;
4488  lease_dereference (&ip_lease, MDL);
4489  }
4490  }
4491 
4492  /*
4493  * If we got an ip_lease and a uid_lease or hw_lease, and ip_lease
4494  * is/was not active, and is not ours to reallocate, forget about it.
4495  */
4496  if (ip_lease && (uid_lease || hw_lease) &&
4497  ip_lease->binding_state != FTS_ACTIVE &&
4498  ip_lease->rewind_binding_state != FTS_ACTIVE &&
4499 #if defined(FAILOVER_PROTOCOL)
4500  !lease_mine_to_reallocate(ip_lease) &&
4501 #endif
4503 #if defined (DEBUG_FIND_LEASE)
4504  log_info("ip lease not active or not ours to offer.");
4505 #endif
4506  lease_dereference(&ip_lease, MDL);
4507  }
4508 
4509  /* If for some reason the client has more than one lease
4510  on the subnet that matches its uid, pick the one that
4511  it asked for and (if we can) free the other. */
4512  if (ip_lease && ip_lease->binding_state == FTS_ACTIVE &&
4513  ip_lease->uid && ip_lease != uid_lease) {
4514  if (have_client_identifier &&
4515  (ip_lease -> uid_len == client_identifier.len) &&
4516  !memcmp (client_identifier.data,
4517  ip_lease -> uid, ip_lease -> uid_len)) {
4518  if (uid_lease) {
4519  if (uid_lease->binding_state == FTS_ACTIVE) {
4520  log_error ("client %s has duplicate%s on %s",
4522  " leases",
4523  (ip_lease -> subnet ->
4524  shared_network -> name));
4525 
4526  /* If the client is REQUESTing the lease,
4527  it shouldn't still be using the old
4528  one, so we can free it for allocation. */
4529  if (uid_lease &&
4530  uid_lease->binding_state == FTS_ACTIVE &&
4531  !packet -> raw -> ciaddr.s_addr &&
4532  (share ==
4533  uid_lease -> subnet -> shared_network) &&
4534  packet -> packet_type == DHCPREQUEST)
4535  release_lease (uid_lease, packet);
4536  }
4537  lease_dereference (&uid_lease, MDL);
4538  lease_reference (&uid_lease, ip_lease, MDL);
4539  }
4540  }
4541 
4542  /* If we get to here and fixed_lease is not null, that means
4543  that there are both a dynamic lease and a fixed-address
4544  declaration for the same IP address. */
4545  if (packet -> packet_type == DHCPREQUEST && fixed_lease) {
4546  lease_dereference (&fixed_lease, MDL);
4547  db_conflict:
4548  log_error ("Dynamic and static leases present for %s.",
4549  piaddr (cip));
4550  log_error ("Remove host declaration %s or remove %s",
4551  (fixed_lease && fixed_lease -> host
4552  ? (fixed_lease -> host -> name
4553  ? fixed_lease -> host -> name
4554  : piaddr (cip))
4555  : piaddr (cip)),
4556  piaddr (cip));
4557  log_error ("from the dynamic address pool for %s",
4558  ip_lease -> subnet -> shared_network -> name
4559  );
4560  if (fixed_lease)
4561  lease_dereference (&ip_lease, MDL);
4562  strcpy (dhcp_message,
4563  "database conflict - call for help!");
4564  }
4565 
4566  if (ip_lease && ip_lease != uid_lease) {
4567 #if defined (DEBUG_FIND_LEASE)
4568  log_info ("requested address not available.");
4569 #endif
4570  lease_dereference (&ip_lease, MDL);
4571  }
4572  }
4573 
4574  /* If we get to here with both fixed_lease and ip_lease not
4575  null, then we have a configuration file bug. */
4576  if (packet -> packet_type == DHCPREQUEST && fixed_lease && ip_lease)
4577  goto db_conflict;
4578 
4579  /* Toss extra pointers to the same lease... */
4580  if (hw_lease && hw_lease == uid_lease) {
4581 #if defined (DEBUG_FIND_LEASE)
4582  log_info ("hardware lease and uid lease are identical.");
4583 #endif
4584  lease_dereference (&hw_lease, MDL);
4585  }
4586  if (ip_lease && ip_lease == hw_lease) {
4587  lease_dereference (&hw_lease, MDL);
4588 #if defined (DEBUG_FIND_LEASE)
4589  log_info ("hardware lease and ip lease are identical.");
4590 #endif
4591  }
4592  if (ip_lease && ip_lease == uid_lease) {
4593  lease_dereference (&uid_lease, MDL);
4594 #if defined (DEBUG_FIND_LEASE)
4595  log_info ("uid lease and ip lease are identical.");
4596 #endif
4597  }
4598 
4599  /* Make sure the client is permitted to use the requested lease. */
4600  if (ip_lease &&
4601  ((ip_lease -> pool -> prohibit_list &&
4602  permitted (packet, ip_lease -> pool -> prohibit_list)) ||
4603  (ip_lease -> pool -> permit_list &&
4604  !permitted (packet, ip_lease -> pool -> permit_list)))) {
4605  if (!packet->raw->ciaddr.s_addr &&
4606  (ip_lease->binding_state == FTS_ACTIVE))
4607  release_lease (ip_lease, packet);
4608 
4609  lease_dereference (&ip_lease, MDL);
4610  }
4611 
4612  if (uid_lease &&
4613  ((uid_lease -> pool -> prohibit_list &&
4614  permitted (packet, uid_lease -> pool -> prohibit_list)) ||
4615  (uid_lease -> pool -> permit_list &&
4616  !permitted (packet, uid_lease -> pool -> permit_list)))) {
4617  if (!packet -> raw -> ciaddr.s_addr)
4618  release_lease (uid_lease, packet);
4619  lease_dereference (&uid_lease, MDL);
4620  }
4621 
4622  if (hw_lease &&
4623  ((hw_lease -> pool -> prohibit_list &&
4624  permitted (packet, hw_lease -> pool -> prohibit_list)) ||
4625  (hw_lease -> pool -> permit_list &&
4626  !permitted (packet, hw_lease -> pool -> permit_list)))) {
4627  if (!packet -> raw -> ciaddr.s_addr)
4628  release_lease (hw_lease, packet);
4629  lease_dereference (&hw_lease, MDL);
4630  }
4631 
4632  /* If we've already eliminated the lease, it wasn't there to
4633  begin with. If we have come up with a matching lease,
4634  set the message to bad network in case we have to throw it out. */
4635  if (!ip_lease) {
4636  strcpy (dhcp_message, "requested address not available");
4637  }
4638 
4639  /* If this is a DHCPREQUEST, make sure the lease we're going to return
4640  matches the requested IP address. If it doesn't, don't return a
4641  lease at all. */
4642  if (packet -> packet_type == DHCPREQUEST &&
4643  !ip_lease && !fixed_lease) {
4644 #if defined (DEBUG_FIND_LEASE)
4645  log_info ("no applicable lease found for DHCPREQUEST.");
4646 #endif
4647  goto out;
4648  }
4649 
4650  /* At this point, if fixed_lease is nonzero, we can assign it to
4651  this client. */
4652  if (fixed_lease) {
4653  lease_reference (&lease, fixed_lease, MDL);
4654  lease_dereference (&fixed_lease, MDL);
4655 #if defined (DEBUG_FIND_LEASE)
4656  log_info ("choosing fixed address.");
4657 #endif
4658  }
4659 
4660  /* If we got a lease that matched the ip address and don't have
4661  a better offer, use that; otherwise, release it. */
4662  if (ip_lease) {
4663  if (lease) {
4664  if (!packet -> raw -> ciaddr.s_addr)
4665  release_lease (ip_lease, packet);
4666 #if defined (DEBUG_FIND_LEASE)
4667  log_info ("not choosing requested address (!).");
4668 #endif
4669  lease_dereference (&ip_lease, MDL);
4670  } else {
4671 #if defined (DEBUG_FIND_LEASE)
4672  log_info ("choosing lease on requested address.");
4673 #endif
4674  lease_reference (&lease, ip_lease, MDL);
4675  if (lease -> host)
4676  host_dereference (&lease -> host, MDL);
4677  }
4678  }
4679 
4680  /* If we got a lease that matched the client identifier, we may want
4681  to use it, but if we already have a lease we like, we must free
4682  the lease that matched the client identifier. */
4683  if (uid_lease) {
4684  if (lease) {
4685  log_error("uid lease %s for client %s is duplicate "
4686  "on %s",
4687  piaddr(uid_lease->ip_addr),
4689  uid_lease->subnet->shared_network->name);
4690 
4691  if (!packet -> raw -> ciaddr.s_addr &&
4692  packet -> packet_type == DHCPREQUEST &&
4693  uid_lease -> binding_state == FTS_ACTIVE)
4694  release_lease(uid_lease, packet);
4695 #if defined (DEBUG_FIND_LEASE)
4696  log_info ("not choosing uid lease.");
4697 #endif
4698  } else {
4699  lease_reference (&lease, uid_lease, MDL);
4700  if (lease -> host)
4701  host_dereference (&lease -> host, MDL);
4702 #if defined (DEBUG_FIND_LEASE)
4703  log_info ("choosing uid lease.");
4704 #endif
4705  }
4706  lease_dereference (&uid_lease, MDL);
4707  }
4708 
4709  /* The lease that matched the hardware address is treated likewise. */
4710  if (hw_lease) {
4711  if (lease) {
4712 #if defined (DEBUG_FIND_LEASE)
4713  log_info ("not choosing hardware lease.");
4714 #endif
4715  } else {
4716  /* We're a little lax here - if the client didn't
4717  send a client identifier and it's a bootp client,
4718  but the lease has a client identifier, we still
4719  let the client have a lease. */
4720  if (!hw_lease -> uid_len ||
4721  (have_client_identifier
4722  ? (hw_lease -> uid_len ==
4723  client_identifier.len &&
4724  !memcmp (hw_lease -> uid,
4725  client_identifier.data,
4726  client_identifier.len))
4727  : packet -> packet_type == 0)) {
4728  lease_reference (&lease, hw_lease, MDL);
4729  if (lease -> host)
4730  host_dereference (&lease -> host, MDL);
4731 #if defined (DEBUG_FIND_LEASE)
4732  log_info ("choosing hardware lease.");
4733 #endif
4734  } else {
4735 #if defined (DEBUG_FIND_LEASE)
4736  log_info ("not choosing hardware lease: %s.",
4737  "uid mismatch");
4738 #endif
4739  }
4740  }
4741  lease_dereference (&hw_lease, MDL);
4742  }
4743 
4744  /*
4745  * If we found a host_decl but no matching address, try to
4746  * find a host_decl that has no address, and if there is one,
4747  * hang it off the lease so that we can use the supplied
4748  * options.
4749  */
4750  if (lease && host && !lease->host) {
4751  struct host_decl *p = NULL;
4752  struct host_decl *n = NULL;
4753 
4754  host_reference(&p, host, MDL);
4755  while (p != NULL) {
4756  if (!p->fixed_addr) {
4757  /*
4758  * If the lease is currently active, then it
4759  * must be allocated to the present client.
4760  * We store a reference to the host record on
4761  * the lease to save a lookup later (in
4762  * ack_lease()). We mustn't refer to the host
4763  * record on non-active leases because the
4764  * client may be denied later.
4765  *
4766  * XXX: Not having this reference (such as in
4767  * DHCPDISCOVER/INIT) means ack_lease will have
4768  * to perform this lookup a second time. This
4769  * hopefully isn't a problem as DHCPREQUEST is
4770  * more common than DHCPDISCOVER.
4771  */
4772  if (lease->binding_state == FTS_ACTIVE)
4773  host_reference(&lease->host, p, MDL);
4774 
4775  host_dereference(&p, MDL);
4776  break;
4777  }
4778  if (p->n_ipaddr != NULL)
4779  host_reference(&n, p->n_ipaddr, MDL);
4780  host_dereference(&p, MDL);
4781  if (n != NULL) {
4782  host_reference(&p, n, MDL);
4783  host_dereference(&n, MDL);
4784  }
4785  }
4786  }
4787 
4788  /* If we find an abandoned lease, but it's the one the client
4789  requested, we assume that previous bugginess on the part
4790  of the client, or a server database loss, caused the lease to
4791  be abandoned, so we reclaim it and let the client have it. */
4792  if (lease &&
4793  (lease -> binding_state == FTS_ABANDONED) &&
4794  lease == ip_lease &&
4795  packet -> packet_type == DHCPREQUEST) {
4796  log_error ("Reclaiming REQUESTed abandoned IP address %s.",
4797  piaddr (lease -> ip_addr));
4798  } else if (lease && (lease -> binding_state == FTS_ABANDONED)) {
4799  /* Otherwise, if it's not the one the client requested, we do not
4800  return it - instead, we claim it's ours, causing a DHCPNAK to be
4801  sent if this lookup is for a DHCPREQUEST, and force the client
4802  to go back through the allocation process. */
4803  if (ours)
4804  *ours = 1;
4805  lease_dereference (&lease, MDL);
4806  }
4807 
4808  out:
4809  if (have_client_identifier)
4811 
4812  if (fixed_lease)
4813  lease_dereference (&fixed_lease, MDL);
4814  if (hw_lease)
4815  lease_dereference (&hw_lease, MDL);
4816  if (uid_lease)
4817  lease_dereference (&uid_lease, MDL);
4818  if (ip_lease)
4819  lease_dereference (&ip_lease, MDL);
4820  if (host)
4821  host_dereference (&host, MDL);
4822 
4823  if (lease) {
4824 #if defined (DEBUG_FIND_LEASE)
4825  log_info ("Returning lease: %s.",
4826  piaddr (lease -> ip_addr));
4827 #endif
4828  lease_reference (lp, lease, file, line);
4829  lease_dereference (&lease, MDL);
4830  return 1;
4831  }
4832 #if defined (DEBUG_FIND_LEASE)
4833  log_info ("Not returning a lease.");
4834 #endif
4835  return 0;
4836 }
4837 
4838 /* Search the provided host_decl structure list for an address that's on
4839  the specified shared network. If one is found, mock up and return a
4840  lease structure for it; otherwise return the null pointer. */
4841 
4842 int mockup_lease (struct lease **lp, struct packet *packet,
4843  struct shared_network *share, struct host_decl *hp)
4844 {
4845  struct lease *lease = (struct lease *)0;
4846  struct host_decl *rhp = (struct host_decl *)0;
4847 
4848  if (lease_allocate (&lease, MDL) != ISC_R_SUCCESS)
4849  return 0;
4850  if (host_reference (&rhp, hp, MDL) != ISC_R_SUCCESS) {
4851  lease_dereference (&lease, MDL);
4852  return 0;
4853  }
4855  &rhp, &lease -> ip_addr, share)) {
4856  lease_dereference (&lease, MDL);
4857  host_dereference (&rhp, MDL);
4858  return 0;
4859  }
4860  host_reference (&lease -> host, rhp, MDL);
4861  if (rhp -> client_identifier.len > sizeof lease -> uid_buf)
4862  lease -> uid = dmalloc (rhp -> client_identifier.len, MDL);
4863  else
4864  lease -> uid = lease -> uid_buf;
4865  if (!lease -> uid) {
4866  lease_dereference (&lease, MDL);
4867  host_dereference (&rhp, MDL);
4868  return 0;
4869  }
4870  memcpy (lease -> uid, rhp -> client_identifier.data,
4871  rhp -> client_identifier.len);
4872  lease -> uid_len = rhp -> client_identifier.len;
4873  lease -> hardware_addr = rhp -> interface;
4874  lease -> starts = lease -> cltt = lease -> ends = MIN_TIME;
4875  lease -> flags = STATIC_LEASE;
4876  lease -> binding_state = FTS_FREE;
4877 
4878  lease_reference (lp, lease, MDL);
4879 
4880  lease_dereference (&lease, MDL);
4881  host_dereference (&rhp, MDL);
4882  return 1;
4883 }
4884 
4885 /* Look through all the pools in a list starting with the specified pool
4886  for a free lease. We try to find a virgin lease if we can. If we
4887  don't find a virgin lease, we try to find a non-virgin lease that's
4888  free. If we can't find one of those, we try to reclaim an abandoned
4889  lease. If all of these possibilities fail to pan out, we don't return
4890  a lease at all. */
4891 
4892 int allocate_lease (struct lease **lp, struct packet *packet,
4893  struct pool *pool, int *peer_has_leases)
4894 {
4895  struct lease *lease = NULL;
4896  struct lease *candl = NULL;
4897 
4898  for (; pool ; pool = pool -> next) {
4899  if ((pool -> prohibit_list &&
4900  permitted (packet, pool -> prohibit_list)) ||
4901  (pool -> permit_list &&
4902  !permitted (packet, pool -> permit_list)))
4903  continue;
4904 
4905 #if defined (FAILOVER_PROTOCOL)
4906  /* Peer_has_leases just says that we found at least one
4907  free lease. If no free lease is returned, the caller
4908  can deduce that this means the peer is hogging all the
4909  free leases, so we can print a better error message. */
4910  /* XXX Do we need code here to ignore PEER_IS_OWNER and
4911  * XXX just check tstp if we're in, e.g., PARTNER_DOWN?
4912  * XXX Where do we deal with CONFLICT_DETECTED, et al? */
4913  /* XXX This should be handled by the lease binding "state
4914  * XXX machine" - that is, when we get here, if a lease
4915  * XXX could be allocated, it will have the correct
4916  * XXX binding state so that the following code will
4917  * XXX result in its being allocated. */
4918  /* Skip to the most expired lease in the pool that is not
4919  * owned by a failover peer. */
4920  if (pool->failover_peer != NULL) {
4921  struct lease *peerl = NULL;
4922  if (pool->failover_peer->i_am == primary) {
4923  candl = LEASE_GET_FIRST(pool->free);
4924 
4925  /*
4926  * In normal operation, we never want to touch
4927  * the peer's leases. In partner-down
4928  * operation, we need to be able to pick up
4929  * the peer's leases after STOS+MCLT.
4930  */
4931  peerl = LEASE_GET_FIRST(pool->backup);
4932  if (peerl != NULL) {
4933  if (((candl == NULL) ||
4934  (candl->ends > peerl->ends)) &&
4935  lease_mine_to_reallocate(peerl)) {
4936  candl = peerl;
4937  } else {
4938  *peer_has_leases = 1;
4939  }
4940  }
4941  } else {
4942  candl = LEASE_GET_FIRST(pool->backup);
4943 
4944  peerl = LEASE_GET_FIRST(pool->free);
4945  if (peerl != NULL) {
4946  if (((candl == NULL) ||
4947  (candl->ends > peerl->ends)) &&
4948  lease_mine_to_reallocate(peerl)) {
4949  candl = peerl;
4950  } else {
4951  *peer_has_leases = 1;
4952  }
4953  }
4954  }
4955 
4956  /* Try abandoned leases as a last resort. */
4957  peerl = LEASE_GET_FIRST(pool->abandoned);
4958  if ((candl == NULL) && (peerl != NULL) &&
4959  lease_mine_to_reallocate(peerl))
4960  candl = peerl;
4961  } else
4962 #endif
4963  {
4964  if (LEASE_NOT_EMPTY(pool->free))
4965  candl = LEASE_GET_FIRST(pool->free);
4966  else
4967  candl = LEASE_GET_FIRST(pool->abandoned);
4968  }
4969 
4970  /*
4971  * XXX: This may not match with documented expectation.
4972  * It's expected that when we OFFER a lease, we set its
4973  * ends time forward 2 minutes so that it gets sorted to
4974  * the end of its free list (avoiding a similar allocation
4975  * to another client). It is not expected that we issue a
4976  * "no free leases" error when the last lease has been
4977  * offered, but it's not exactly broken either.
4978  */
4979  if (!candl ||
4980  (candl->binding_state != FTS_ABANDONED &&
4981  (candl->ends > cur_time))) {
4982  continue;
4983  }
4984 
4985  if (!lease) {
4986  lease = candl;
4987  continue;
4988  }
4989 
4990  /*
4991  * There are tiers of lease state preference, listed here in
4992  * reverse order (least to most preferential):
4993  *
4994  * ABANDONED
4995  * FREE/BACKUP
4996  *
4997  * If the selected lease and candidate are both of the same
4998  * state, select the oldest (longest ago) expiration time
4999  * between the two. If the candidate lease is of a higher
5000  * preferred grade over the selected lease, use it.
5001  */
5002  if ((lease -> binding_state == FTS_ABANDONED) &&
5003  ((candl -> binding_state != FTS_ABANDONED) ||
5004  (candl -> ends < lease -> ends))) {
5005  lease = candl;
5006  continue;
5007  } else if (candl -> binding_state == FTS_ABANDONED)
5008  continue;
5009 
5010  if ((lease -> uid_len || lease -> hardware_addr.hlen) &&
5011  ((!candl -> uid_len && !candl -> hardware_addr.hlen) ||
5012  (candl -> ends < lease -> ends))) {
5013  lease = candl;
5014  continue;
5015  } else if (candl -> uid_len || candl -> hardware_addr.hlen)
5016  continue;
5017 
5018  if (candl -> ends < lease -> ends)
5019  lease = candl;
5020  }
5021 
5022  if (lease != NULL) {
5024  log_error("Reclaiming abandoned lease %s.",
5025  piaddr(lease->ip_addr));
5026 
5027  /*
5028  * XXX: For reliability, we go ahead and remove the host
5029  * record and try to move on. For correctness, if there
5030  * are any other stale host vectors, we want to find them.
5031  */
5032  if (lease->host != NULL) {
5033  log_debug("soft impossible condition (%s:%d): stale "
5034  "host \"%s\" found on lease %s", MDL,
5035  lease->host->name,
5036  piaddr(lease->ip_addr));
5037  host_dereference(&lease->host, MDL);
5038  }
5039 
5040  lease_reference (lp, lease, MDL);
5041  return 1;
5042  }
5043 
5044  return 0;
5045 }
5046 
5047 /* Determine whether or not a permit exists on a particular permit list
5048  that matches the specified packet, returning nonzero if so, zero if
5049  not. */
5050 
5051 int permitted (packet, permit_list)
5052  struct packet *packet;
5053  struct permit *permit_list;
5054 {
5055  struct permit *p;
5056  int i;
5057 
5058  for (p = permit_list; p; p = p -> next) {
5059  switch (p -> type) {
5061  if (!packet -> known)
5062  return 1;
5063  break;
5064 
5065  case permit_known_clients:
5066  if (packet -> known)
5067  return 1;
5068  break;
5069 
5071  if (packet -> authenticated)
5072  return 1;
5073  break;
5074 
5076  if (!packet -> authenticated)
5077  return 1;
5078  break;
5079 
5080  case permit_all_clients:
5081  return 1;
5082 
5084  if (!packet -> options_valid ||
5085  !packet -> packet_type)
5086  return 1;
5087  break;
5088 
5089  case permit_class:
5090  for (i = 0; i < packet -> class_count; i++) {
5091  if (p -> class == packet -> classes [i])
5092  return 1;
5093  if (packet -> classes [i] &&
5094  packet -> classes [i] -> superclass &&
5095  (packet -> classes [i] -> superclass ==
5096  p -> class))
5097  return 1;
5098  }
5099  break;
5100 
5101  case permit_after:
5102  if (cur_time > p->after)
5103  return 1;
5104  break;
5105  }
5106  }
5107  return 0;
5108 }
5109 
5110 #if defined(DHCPv6) && defined(DHCP4o6)
5111 static int locate_network6 (packet)
5112  struct packet *packet;
5113 {
5114  const struct packet *chk_packet;
5115  const struct in6_addr *link_addr, *first_link_addr;
5116  struct iaddr ia;
5117  struct data_string data;
5118  struct subnet *subnet = NULL;
5119  struct option_cache *oc;
5120 
5121  /* from locate_network() */
5122 
5123  /* See if there's a Relay Agent Link Selection Option, or a
5124  * Subnet Selection Option. The Link-Select and Subnet-Select
5125  * are formatted and used precisely the same, but we must prefer
5126  * the link-select over the subnet-select.
5127  * BTW in DHCPv4 over DHCPv6 no cross version relay was specified
5128  * so it is unlikely to see a link-select.
5129  */
5131  RAI_LINK_SELECT)) == NULL)
5134 
5135  /* If there's an option indicating link connection or subnet
5136  * selection, and it's valid, use it to figure out the subnet.
5137  * If it's not valid, fail.
5138  */
5139  if (oc) {
5140  memset(&data, 0, sizeof data);
5141  if (!evaluate_option_cache(&data, packet, NULL, NULL,
5142  packet->options, NULL,
5143  &global_scope, oc, MDL)) {
5144  return (0);
5145  }
5146  if (data.len == 0) {
5147  return (0);
5148  }
5149  if (data.len != 4) {
5151  return (0);
5152  }
5153  ia.len = 4;
5154  memcpy(ia.iabuf, data.data, 4);
5156 
5157  if (find_subnet(&subnet, ia, MDL)) {
5158  shared_network_reference(&packet->shared_network,
5160  subnet_dereference(&subnet, MDL);
5161  return (1);
5162  }
5163  return (0);
5164  }
5165 
5166  /* See if there is a giaddr (still unlikely), if there is one
5167  * use it to figure out the subnet. If it's not valid, fail.
5168  */
5169  if (packet->raw->giaddr.s_addr) {
5170  ia.len = 4;
5171  memcpy(ia.iabuf, &packet->raw->giaddr, 4);
5172 
5173  if (find_subnet(&subnet, ia, MDL)) {
5174  shared_network_reference(&packet->shared_network,
5176  subnet_dereference(&subnet, MDL);
5177  return (1);
5178  }
5179  return (0);
5180  }
5181 
5182  /* from shared_network_from_packet6() */
5183 
5184  /* First, find the link address where the packet from the client
5185  * first appeared (if this packet was relayed).
5186  */
5187  first_link_addr = NULL;
5188  chk_packet = packet->dhcpv6_container_packet;
5189  while (chk_packet != NULL) {
5190  link_addr = &chk_packet->dhcpv6_link_address;
5191  if (!IN6_IS_ADDR_UNSPECIFIED(link_addr) &&
5192  !IN6_IS_ADDR_LINKLOCAL(link_addr)) {
5193  first_link_addr = link_addr;
5194  break;
5195  }
5196  chk_packet = chk_packet->dhcpv6_container_packet;
5197  }
5198 
5199  /* If there is a relayed link address, find the subnet associated
5200  * with that, and use that to get the appropriate shared_network.
5201  */
5202  if (first_link_addr != NULL) {
5203  ia.len = sizeof(*first_link_addr);
5204  memcpy(ia.iabuf, first_link_addr, sizeof(*first_link_addr));
5205  if (find_subnet (&subnet, ia, MDL)) {
5206  shared_network_reference(&packet->shared_network,
5208  subnet_dereference(&subnet, MDL);
5209  return (1);
5210  }
5211  return (0);
5212  }
5213 
5214  /* If there is no link address, we will use the interface
5215  * that this packet came in on to pick the shared_network.
5216  */
5217  if (packet->interface != NULL) {
5218  if (packet->interface->shared_network == NULL)
5219  return (0);
5220  shared_network_reference(&packet->shared_network,
5222  MDL);
5223  return (1);
5224  }
5225 
5226  /* We shouldn't be able to get here but if there is no link
5227  * address and no interface we don't know where to get the
5228  * shared_network from, log an error and return an error.
5229  */
5230  log_error("No interface and no link address "
5231  "can't determine DHCP4o6 shared network");
5232  return (0);
5233 }
5234 #endif
5235 
5237  struct packet *packet;
5238 {
5239  struct iaddr ia;
5240  struct data_string data;
5241  struct subnet *subnet = (struct subnet *)0;
5242  struct option_cache *oc;
5243  int norelay = 0;
5244 
5245 #if defined(DHCPv6) && defined(DHCP4o6)
5246  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
5247  return (locate_network6 (packet));
5248  }
5249 #endif
5250 
5251  /* See if there's a Relay Agent Link Selection Option, or a
5252  * Subnet Selection Option. The Link-Select and Subnet-Select
5253  * are formatted and used precisely the same, but we must prefer
5254  * the link-select over the subnet-select.
5255  */
5257  RAI_LINK_SELECT)) == NULL)
5260 
5261  /* If there's no SSO and no giaddr, then use the shared_network
5262  from the interface, if there is one. If not, fail. */
5263  if (!oc && !packet -> raw -> giaddr.s_addr) {
5264  if (packet -> interface -> shared_network) {
5265  struct in_addr any_addr;
5266  any_addr.s_addr = INADDR_ANY;
5267 
5268  if (!packet -> packet_type && memcmp(&packet -> raw -> ciaddr, &any_addr, 4)) {
5269  struct iaddr cip;
5270  memcpy(cip.iabuf, &packet -> raw -> ciaddr, 4);
5271  cip.len = 4;
5273  norelay = 2;
5274  }
5275 
5276  if (!norelay) {
5277  shared_network_reference(&packet -> shared_network, packet -> interface -> shared_network, MDL);
5278  return 1;
5279  }
5280  } else {
5281  return 0;
5282  }
5283  }
5284 
5285  /* If there's an option indicating link connection, and it's valid,
5286  * use it to figure out the subnet. If it's not valid, fail.
5287  */
5288  if (oc) {
5289  memset (&data, 0, sizeof data);
5290  if (!evaluate_option_cache (&data, packet, (struct lease *)0,
5291  (struct client_state *)0,
5292  packet -> options,
5293  (struct option_state *)0,
5294  &global_scope, oc, MDL)) {
5295  return 0;
5296  }
5297  if (data.len == 0) {
5298  return 0;
5299  }
5300  if (data.len != 4) {
5301  data_string_forget (&data, MDL);
5302  return 0;
5303  }
5304  ia.len = 4;
5305  memcpy (ia.iabuf, data.data, 4);
5306  data_string_forget (&data, MDL);
5307  } else {
5308  ia.len = 4;
5309  if (norelay)
5310  memcpy (ia.iabuf, &packet->raw->ciaddr, 4);
5311  else
5312  memcpy (ia.iabuf, &packet->raw->giaddr, 4);
5313  }
5314 
5315  /* If we know the subnet on which the IP address lives, use it. */
5316  if (find_subnet (&subnet, ia, MDL)) {
5317  shared_network_reference (&packet -> shared_network,
5318  subnet -> shared_network, MDL);
5319  subnet_dereference (&subnet, MDL);
5320  if (norelay)
5321  return norelay;
5322  else
5323  return 1;
5324  }
5325 
5326  /* Otherwise, fail. */
5327  return 0;
5328 }
5329 
5330 /*
5331  * Try to figure out the source address to send packets from.
5332  *
5333  * from is the address structure we use to return any address
5334  * we find.
5335  *
5336  * options is the option cache to search. This may include
5337  * options from the incoming packet and configuration information.
5338  *
5339  * out_options is the outgoing option cache. This cache
5340  * may be the same as options. If out_options isn't NULL
5341  * we may save the server address option into it. We do so
5342  * if out_options is different than options or if the option
5343  * wasn't in options and we needed to find the address elsewhere.
5344  *
5345  * packet is the state structure for the incoming packet
5346  *
5347  * When finding the address we first check to see if it is
5348  * in the options list. If it isn't we use the first address
5349  * from the interface.
5350  *
5351  * While this is slightly more complicated than I'd like it allows
5352  * us to use the same code in several different places. ack,
5353  * inform and lease query use it to find the address and fill
5354  * in the options if we get the address from the interface.
5355  * nack uses it to find the address and copy it to the outgoing
5356  * cache. dhcprequest uses it to find the address for comparison
5357  * and doesn't need to add it to an outgoing list.
5358  */
5359 
5360 void
5361 get_server_source_address(struct in_addr *from,
5362  struct option_state *options,
5363  struct option_state *out_options,
5364  struct packet *packet) {
5365  unsigned option_num;
5366  struct option_cache *oc = NULL;
5367  struct data_string d;
5368  struct in_addr *a = NULL;
5369  isc_boolean_t found = ISC_FALSE;
5370  int allocate = 0;
5371 
5372  memset(&d, 0, sizeof(d));
5373  memset(from, 0, sizeof(*from));
5374 
5375  option_num = DHO_DHCP_SERVER_IDENTIFIER;
5376  oc = lookup_option(&dhcp_universe, options, option_num);
5377  if (oc != NULL) {
5378  if (evaluate_option_cache(&d, packet, NULL, NULL,
5379  packet->options, options,
5380  &global_scope, oc, MDL)) {
5381  if (d.len == sizeof(*from)) {
5382  found = ISC_TRUE;
5383  memcpy(from, d.data, sizeof(*from));
5384 
5385  /*
5386  * Arrange to save a copy of the data
5387  * to the outgoing list.
5388  */
5389  if ((out_options != NULL) &&
5390  (options != out_options)) {
5391  a = from;
5392  allocate = 1;
5393  }
5394  }
5395  data_string_forget(&d, MDL);
5396  }
5397  oc = NULL;
5398  }
5399 
5400  if ((found == ISC_FALSE) &&
5401  (packet->interface->address_count > 0)) {
5402  *from = packet->interface->addresses[0];
5403 
5404  if (out_options != NULL) {
5405  a = &packet->interface->addresses[0];
5406  }
5407  }
5408 
5409  if ((a != NULL) &&
5410  (option_cache_allocate(&oc, MDL))) {
5411  if (make_const_data(&oc->expression,
5412  (unsigned char *)a, sizeof(*a),
5413  0, allocate, MDL)) {
5414  option_code_hash_lookup(&oc->option,
5416  &option_num, 0, MDL);
5417  save_option(&dhcp_universe, out_options, oc);
5418  }
5420  }
5421 
5422  return;
5423 }
5424 
5446 void
5447 eval_network_statements(struct option_state **network_options,
5448  struct packet *packet,
5449  struct group *network_group) {
5450 
5451  if (*network_options == NULL) {
5452  option_state_allocate (network_options, MDL);
5453  }
5454 
5455  /* Use the packet's shared_network if it has one. If not use
5456  * network_group and if it is null then use global scope. */
5457  if (packet->shared_network != NULL) {
5458  /*
5459  * If we have a subnet and group start with that else start
5460  * with the shared network group. The first will recurse and
5461  * include the second.
5462  */
5463  if ((packet->shared_network->subnets != NULL) &&
5464  (packet->shared_network->subnets->group != NULL)) {
5465  execute_statements_in_scope(NULL, packet, NULL, NULL,
5466  packet->options, *network_options,
5467  &global_scope,
5469  NULL, NULL);
5470  } else {
5471  execute_statements_in_scope(NULL, packet, NULL, NULL,
5472  packet->options, *network_options,
5473  &global_scope,
5475  NULL, NULL);
5476  }
5477 
5478  /* do the pool if there is one */
5479  if (packet->shared_network->pools != NULL) {
5480  execute_statements_in_scope(NULL, packet, NULL, NULL,
5481  packet->options, *network_options,
5482  &global_scope,
5485  NULL);
5486  }
5487  } else if (network_group != NULL) {
5488  execute_statements_in_scope(NULL, packet, NULL, NULL,
5489  packet->options, *network_options,
5490  &global_scope, network_group,
5491  NULL, NULL);
5492  } else {
5493  execute_statements_in_scope(NULL, packet, NULL, NULL,
5494  packet->options, *network_options,
5496  NULL, NULL);
5497  }
5498 }
5499 
5500 /*
5501  * Look for the lowest numbered site code number and
5502  * apply a log warning if it is less than 224. Do not
5503  * permit site codes less than 128 (old code never did).
5504  *
5505  * Note that we could search option codes 224 down to 128
5506  * on the hash table, but the table is (probably) smaller
5507  * than that if it was declared as a standalone table with
5508  * defaults. So we traverse the option code hash.
5509  */
5510 static int
5511 find_min_site_code(struct universe *u)
5512 {
5513  if (u->site_code_min)
5514  return u->site_code_min;
5515 
5516  /*
5517  * Note that site_code_min has to be global as we can't pass an
5518  * argument through hash_foreach(). The value 224 is taken from
5519  * RFC 3942.
5520  */
5521  site_code_min = 224;
5522  option_code_hash_foreach(u->code_hash, lowest_site_code);
5523 
5524  if (site_code_min < 224) {
5525  log_error("WARNING: site-local option codes less than 224 have "
5526  "been deprecated by RFC3942. You have options "
5527  "listed in site local space %s that number as low as "
5528  "%d. Please investigate if these should be declared "
5529  "as regular options rather than site-local options, "
5530  "or migrated up past 224.",
5531  u->name, site_code_min);
5532  }
5533 
5534  /*
5535  * don't even bother logging, this is just silly, and never worked
5536  * on any old version of software.
5537  */
5538  if (site_code_min < 128)
5539  site_code_min = 128;
5540 
5541  /*
5542  * Cache the determined minimum site code on the universe structure.
5543  * Note that due to the < 128 check above, a value of zero is
5544  * impossible.
5545  */
5546  u->site_code_min = site_code_min;
5547 
5548  return site_code_min;
5549 }
5550 
5551 static isc_result_t
5552 lowest_site_code(const void *key, unsigned len, void *object)
5553 {
5554  struct option *option = object;
5555 
5556  if (option->code < site_code_min)
5557  site_code_min = option->code;
5558 
5559  return ISC_R_SUCCESS;
5560 }
5561 
5562 static void
5563 maybe_return_agent_options(struct packet *packet, struct option_state *options)
5564 {
5565  /* If there were agent options in the incoming packet, return
5566  * them. Do not return the agent options if they were stashed
5567  * on the lease. We do not check giaddr to detect the presence of
5568  * a relay, as this excludes "l2" relay agents which have no giaddr
5569  * to set.
5570  *
5571  * XXX: If the user configures options for the relay agent information
5572  * (state->options->universes[agent_universe.index] is not NULL),
5573  * we're still required to duplicate other values provided by the
5574  * relay agent. So we need to merge the old values not configured
5575  * by the user into the new state, not just give up.
5576  */
5577  if (!packet->agent_options_stashed &&
5578  (packet->options != NULL) &&
5581  (options->universe_count <= agent_universe.index ||
5582  options->universes[agent_universe.index] == NULL)) {
5584  ((struct option_chain_head **)
5585  &(options->universes[agent_universe.index]),
5586  (struct option_chain_head *)
5588 
5589  if (options->universe_count <= agent_universe.index)
5590  options->universe_count = agent_universe.index + 1;
5591  }
5592 }
5593 
5607  struct lease *lease,
5608  struct option_state *options) {
5609  unsigned int ocode = SV_USE_HOST_DECL_NAMES;
5610  if ((lease->host && lease->host->name) &&
5611  !lookup_option(&dhcp_universe, options, DHO_HOST_NAME) &&
5613  packet->options, options,
5614  &lease->scope,
5616  options, ocode),
5617  MDL))) {
5618  struct option_cache *oc = NULL;
5619  if (option_cache_allocate (&oc, MDL)) {
5620  if (make_const_data(&oc -> expression,
5621  ((unsigned char*)lease->host->name),
5622  strlen(lease->host->name),
5623  1, 0, MDL)) {
5624  ocode = DHO_HOST_NAME;
5625  option_code_hash_lookup(&oc->option,
5627  &ocode, 0, MDL);
5628  save_option(&dhcp_universe, options, oc);
5629  }
5631  }
5632  }
5633 }
5634 
5670 int
5671 reuse_lease (struct packet* packet,
5672  struct lease* new_lease,
5673  struct lease* lease,
5674  struct lease_state *state,
5675  int offer) {
5676  int reusable = 0;
5677 
5678  /* To even consider reuse all of the following must be true:
5679  * 1 - reuse hasn't already disqualified
5680  * 2 - current lease is active
5681  * 3 - DNS info hasn't changed
5682  * 4 - the host declaration hasn't changed
5683  * 5 - the uid hasn't changed
5684  * 6 - the hardware address hasn't changed */
5685  if ((lease->cannot_reuse == 0) &&
5686  (lease->binding_state == FTS_ACTIVE) &&
5687  (new_lease->ddns_cb == NULL) &&
5688  (lease->host == new_lease->host) &&
5689  (lease->uid_len == new_lease->uid_len) &&
5690  (memcmp(lease->uid, new_lease->uid, lease->uid_len) == 0) &&
5691  (lease->hardware_addr.hlen == new_lease->hardware_addr.hlen) &&
5692  (memcmp(&lease->hardware_addr.hbuf[0],
5693  &new_lease->hardware_addr.hbuf[0],
5694  lease->hardware_addr.hlen) == 0)) {
5695  int thresh = DEFAULT_CACHE_THRESHOLD;
5696  struct option_cache* oc = NULL;
5697  struct data_string d1;
5698 
5699  /* Look up threshold value */
5700  memset(&d1, 0, sizeof(struct data_string));
5701  if ((oc = lookup_option(&server_universe, state->options,
5702  SV_CACHE_THRESHOLD)) &&
5703  (evaluate_option_cache(&d1, packet, new_lease, NULL,
5704  packet->options, state->options,
5705  &new_lease->scope, oc, MDL))) {
5706  if (d1.len == 1 && (d1.data[0] < 100))
5707  thresh = d1.data[0];
5708 
5709  data_string_forget(&d1, MDL);
5710  }
5711 
5712  /* If threshold is enabled, check lease age */
5713  if (thresh > 0) {
5714  int limit = 0;
5715  int lease_length = 0;
5716  long lease_age = 0;
5717 
5718  /* Calculate limit in seconds */
5719  lease_length = lease->ends - lease->starts;
5720  if (lease_length <= (INT_MAX / thresh))
5721  limit = lease_length * thresh / 100;
5722  else
5723  limit = lease_length / 100 * thresh;
5724 
5725  /* Note new_lease->starts is really just cur_time */
5726  lease_age = new_lease->starts - lease->starts;
5727 
5728  /* Is the lease young enough to reuse? */
5729  if (lease_age <= limit) {
5730  /* Restore expiry to its original value */
5731  state->offered_expiry = lease->ends;
5732 
5733  /* Restore bindings. This fixes 37368. */
5734  if (new_lease->scope != NULL) {
5735  if (lease->scope != NULL) {
5737  &lease->scope,
5738  MDL);
5739  }
5740 
5742  new_lease->scope, MDL);
5743  }
5744 
5745  /* restore client hostname, fixes 42849. */
5746  if (new_lease->client_hostname) {
5748  new_lease->client_hostname;
5749  new_lease->client_hostname = NULL;
5750  }
5751 
5752  /* We're cleared to reuse it */
5753  log_debug("reuse_lease: lease age %ld (secs)"
5754  " under %d%% threshold, reply with "
5755  "unaltered, existing lease for %s",
5756  lease_age, thresh, piaddr(lease->ip_addr));
5757 
5758  reusable = 1;
5759  }
5760  }
5761  }
5762 
5763  /* If we can't reuse it and this is an offer disqualify reuse for
5764  * ensuing REQUEST, otherwise clear the flag. */
5765  lease->cannot_reuse = (!reusable && offer == DHCPOFFER);
5766  return (reusable);
5767 }
5768 
5769 /* \brief Validates a proposed value for use as a lease time
5770  *
5771  * Convenience function used for catching calculeated lease
5772  * times that overflow 4-byte times used in v4 protocol.
5773  *
5774  * We use variables of type TIME in lots of places, which on
5775  * 64-bit systems is 8 bytes while on 32-bit OSs it is int32_t,
5776  * so we have all sorts of fun places to mess things up.
5777  * This function checks a calculated lease time for and if it
5778  * is unsuitable for use as a lease time, the given alternate
5779  * value is returned.
5780  * \param calculated
5781  * \param alternate
5782  *
5783  * \returen either the calculated value if it is valid, or
5784  * the alternate value supplied
5785  */
5786 TIME leaseTimeCheck(TIME calculated, TIME alternate) {
5787  if ((sizeof(TIME) > 4 && calculated >= INFINITE_TIME) ||
5788  (calculated < cur_time)) {
5789  return (alternate);
5790  }
5791 
5792  return (calculated);
5793 }
const char * name
Definition: tree.h:302
#define FTS_ABANDONED
Definition: dhcpd.h:540
#define BOOTREPLY
Definition: dhcp.h:69
int supersede_lease(struct lease *, struct lease *, int, int, int, int)
Definition: mdb.c:1132
service_state
Definition: failover.h:315
int find_grouped_subnet(struct subnet **, struct shared_network *, struct iaddr, const char *, int)
Definition: mdb.c:930
char file[DHCP_FILE_LEN]
Definition: dhcp.h:61
void unbill_class(struct lease *lease)
Definition: dhclient.c:1531
const char int line
Definition: dhcpd.h:3781
char sname[DHCP_SNAME_LEN]
Definition: dhcp.h:60
u_int32_t flags
Definition: dhcpd.h:393
struct binding_scope * global_scope
Definition: tree.c:38
#define DEFAULT_MIN_ACK_DELAY_USECS
Definition: dhcpd.h:839
#define SV_ALLOW_BOOTING
Definition: dhcpd.h:718
#define SV_MAX_LEASE_TIME
Definition: dhcpd.h:711
#define SV_USE_HOST_DECL_NAMES
Definition: dhcpd.h:721
struct on_star on_star
Definition: dhcpd.h:582
#define SV_MIN_LEASE_TIME
Definition: dhcpd.h:712
struct subnet * subnets
Definition: dhcpd.h:1050
void dhcpleasequery(struct packet *, int)
Definition: dhcpd.h:559
void save_option(struct universe *universe, struct option_state *options, struct option_cache *oc)
Definition: options.c:2780
unsigned len
Definition: tree.h:79
int executable_statement_dereference(struct executable_statement **ptr, const char *file, int line)
Definition: execute.c:623
int find_host_for_network(struct subnet **, struct host_decl **, struct iaddr *, struct shared_network *)
Definition: mdb.c:713
void do_release(struct client_state *client)
Definition: dhclient.c:5072
struct shared_network * shared_network
Definition: dhcpd.h:1368
struct group * group
Definition: dhcpd.h:1017
const char * piaddr(const struct iaddr addr)
Definition: inet.c:579
u_int8_t hlen
Definition: dhcpd.h:492
#define FTS_FREE
Definition: dhcpd.h:536
struct dhcp_ddns_cb * ddns_cb
Definition: dhcpd.h:649
unsigned char * uid
Definition: dhcpd.h:584
char name[IFNAMSIZ]
Definition: dhcpd.h:1392
#define DHCPINFORM
Definition: dhcp.h:177
struct lease_state * state
Definition: dhcpd.h:627
struct class * superclass
Definition: dhcpd.h:1090
int option_cache_dereference(struct option_cache **ptr, const char *file, int line)
Definition: options.c:2915
u_int16_t secs
Definition: dhcp.h:53
Definition: dhcpd.h:1060
#define DEFAULT_MIN_LEASE_TIME
Definition: dhcpd.h:851
struct universe server_universe
Definition: stables.c:176
int execute_statements(struct binding_value **result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *out_options, struct binding_scope **scope, struct executable_statement *statements, struct on_star *on_star)
Definition: execute.c:35
struct iaddr ip_addr(struct iaddr subnet, struct iaddr mask, u_int32_t host_address)
Definition: inet.c:63
#define SV_DUPLICATES
Definition: dhcpd.h:737
#define SV_MIN_SECS
Definition: dhcpd.h:723
int max_outstanding_acks
#define SV_IGNORE_CLIENT_UIDS
Definition: dhcpd.h:800
#define MDL
Definition: omapip.h:567
void cancel_timeout(void(*)(void *) where, void *what)
Definition: dispatch.c:381
int find_hosts_by_option(struct host_decl **, struct packet *, struct option_state *, const char *, int)
Definition: mdb.c:637
unsigned char iabuf[16]
Definition: inet.h:33
#define print_hex_1(len, data, limit)
Definition: dhcpd.h:2621
#define DHO_DHCP_PARAMETER_REQUEST_LIST
Definition: dhcp.h:146
u_int8_t hlen
Definition: dhcp.h:50
#define FTS_RELEASED
Definition: dhcpd.h:539
#define DEFAULT_ACK_DELAY_USECS
Definition: dhcpd.h:835
int int int log_debug(const char *,...) __attribute__((__format__(__printf__
#define SV_BOOTP_LEASE_LENGTH
Definition: dhcpd.h:714
struct executable_statement * on_release
Definition: dhcpd.h:555
void lease_ping_timeout(void *)
Definition: dhcpd.c:1543
#define DHO_DHCP_LEASE_TIME
Definition: dhcp.h:142
struct group * group
Definition: dhcpd.h:1054
struct in_addr * addresses
Definition: dhcpd.h:1372
int option_reference(struct option **dest, struct option *src, const char *file, int line)
Definition: tables.c:984
void dhcpack(struct packet *packet)
Definition: dhclient.c:1758
void dhcpdecline(struct packet *packet, int ms_nulltp)
Definition: dhcp.c:951
struct universe dhcp_universe
struct interface_info * ip
Definition: dhcpd.h:658
int dhcpv4_over_dhcpv6
Definition: discover.c:48
#define SV_SITE_OPTION_SPACE
Definition: dhcpd.h:730
void data_string_forget(struct data_string *data, const char *file, int line)
Definition: alloc.c:1339
#define FIND_PERCENT(count, percent)
Definition: dhcpd.h:3898
#define DHCPACK
Definition: dhcp.h:174
struct option_cache * fixed_addr
Definition: dhcpd.h:971
struct class * billing_class
Definition: dhcpd.h:578
struct group * root_group
Definition: memory.c:31
int mockup_lease(struct lease **lp, struct packet *packet, struct shared_network *share, struct host_decl *hp)
Definition: dhcp.c:4842
#define DHO_SUBNET_MASK
Definition: dhcp.h:92
void delete_option(struct universe *universe, struct option_state *options, int code)
Definition: options.c:2868
#define BOOTP_BROADCAST
Definition: dhcp.h:72
int log_error(const char *,...) __attribute__((__format__(__printf__
#define FTS_EXPIRED
Definition: dhcpd.h:538
int binding_scope_dereference(struct binding_scope **ptr, const char *file, int line)
Definition: tree.c:3786
struct in_addr siaddr
Definition: dhcp.h:57
int known
Definition: dhcpd.h:457
void add_timeout(struct timeval *when, void(*)(void *) where, void *what, tvref_t ref, tvunref_t unref)
Definition: dispatch.c:197
void dump_packet(struct packet *)
unsigned short uid_max
Definition: dhcpd.h:586
void(* tvunref_t)(void *, const char *, int)
Definition: dhcpd.h:1437
#define DHO_DHCP_REBINDING_TIME
Definition: dhcp.h:150
int site_code_min
Definition: dhcpd.h:400
unsigned len
Definition: inet.h:32
#define SV_SERVER_NAME
Definition: dhcpd.h:725
dhcp_failover_state_t * failover_peer
Definition: dhcpd.h:1036
void release_lease(struct lease *, struct packet *)
Definition: mdb.c:1732
int find_hosts_by_haddr(struct host_decl **, int, const unsigned char *, unsigned, const char *, int)
Definition: mdb.c:609
unsigned site_code_min
Definition: tree.h:335
void dhcprelease(struct packet *packet, int ms_nulltp)
Definition: dhcp.c:800
struct expression * expression
Definition: dhcpd.h:388
struct data_string client_identifier
Definition: dhcpd.h:965
#define DHCPRELEASE
Definition: dhcp.h:176
#define SV_FILENAME
Definition: dhcpd.h:724
u_int16_t flags
Definition: dhcp.h:54
void(* tvref_t)(void *, void *, const char *, int)
Definition: dhcpd.h:1436
const char * binding_state_print(enum failover_state state)
Definition: failover.c:6502
struct option_state * options
Definition: dhcpd.h:449
#define LEASE_NOT_EMPTY(LQ)
Definition: dhcpd.h:264
#define BOOTP_MIN_LEN
Definition: dhcp.h:39
int outstanding_pings
Definition: dhcp.c:43
Definition: tree.h:301
char * name
Definition: dhcpd.h:1091
#define RAI_LINK_SELECT
Definition: dhcp.h:188
LEASE_STRUCT free
Definition: dhcpd.h:1023
int low_threshold
Definition: dhcpd.h:1039
#define INFINITE_TIME
Definition: dhcpd.h:1613
#define SV_ALLOW_BOOTP
Definition: dhcpd.h:717
void nak_lease(struct packet *packet, struct iaddr *cip, struct group *network_group)
Constructs and sends a DHCP Nak.
Definition: dhcp.c:1792
#define DHO_DHCP_SERVER_IDENTIFIER
Definition: dhcp.h:145
void log_fatal(const char *,...) __attribute__((__format__(__printf__
void dhcpdiscover(struct packet *packet, int ms_nulltp)
Definition: dhcp.c:320
int client_port
Definition: dhcpd.h:431
struct option_state * options
Definition: dhcpd.h:664
#define DEFAULT_DELAYED_ACK
Definition: dhcpd.h:827
int option_cache_allocate(struct option_cache **cptr, const char *file, int line)
Definition: alloc.c:630
#define SV_LOG_THRESHOLD_HIGH
Definition: dhcpd.h:802
#define DEFAULT_ACK_DELAY_SECS
Definition: dhcpd.h:831
struct dhcp_packet * raw
Definition: dhcpd.h:406
int locate_network(struct packet *packet)
Definition: dhcp.c:5236
void free_lease_state(struct lease_state *, const char *, int)
Definition: salloc.c:198
universe_hash_t * universe_hash
Definition: tables.c:966
void dhcp_reply(struct lease *lease)
Definition: dhcp.c:3800
struct hardware hardware_addr
Definition: dhcpd.h:588
#define SV_DDNS_UPDATES
Definition: dhcpd.h:739
#define SV_BOOTP_LEASE_CUTOFF
Definition: dhcpd.h:713
int find_subnet(struct subnet **sp, struct iaddr addr, const char *file, int line)
Definition: dhclient.c:1536
void execute_statements_in_scope(struct binding_value **result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *out_options, struct binding_scope **scope, struct group *group, struct group *limiting_group, struct on_star *on_star)
Definition: execute.c:563
u_int8_t htype
Definition: dhcp.h:49
struct interface_info * fallback_interface
Definition: discover.c:42
#define DHCPLEASEACTIVE
Definition: dhcp.h:181
#define FAILOVER_PROTOCOL
Definition: config.h:33
int option_state_allocate(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:846
int evaluate_option_cache(struct data_string *result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct option_cache *oc, const char *file, int line)
Definition: tree.c:2699
struct permit * prohibit_list
Definition: dhcpd.h:1020
struct lease * lease
Definition: dhcpd.h:1433
Definition: tree.h:345
unsigned char chaddr[16]
Definition: dhcp.h:59
int option_chain_head_dereference(struct option_chain_head **ptr, const char *file, int line)
Definition: alloc.c:95
void dhcp(struct packet *packet)
Definition: dhcp.c:127
#define DHCPNAK
Definition: dhcp.h:175
#define SV_NEXT_SERVER
Definition: dhcpd.h:726
TIME after
Definition: dhcpd.h:999
struct leasequeue * prev
Definition: dhcpd.h:1431
#define MIN_TIME
Definition: dhcpd.h:1615
int max_ack_delay_secs
#define MS_NULL_TERMINATION
Definition: dhcpd.h:594
int packet_reference(struct packet **ptr, struct packet *bp, const char *file, int line)
Definition: alloc.c:1053
u_int32_t xid
Definition: dhcp.h:52
#define SV_DECLINES
Definition: dhcpd.h:738
#define SV_ALWAYS_BROADCAST
Definition: dhcpd.h:731
Definition: dhcpd.h:1014
void abandon_lease(struct lease *, const char *)
Definition: mdb.c:1807
binding_state_t binding_state
Definition: dhcpd.h:622
u_int8_t offer
Definition: dhcpd.h:681
#define HTYPE_INFINIBAND
Definition: dhcp.h:78
#define DEFAULT_MAX_LEASE_TIME
Definition: dhcpd.h:855
int buffer_allocate(struct buffer **ptr, unsigned len, const char *file, int line)
Definition: alloc.c:679
struct class * classes[PACKET_MAX_CLASSES]
Definition: dhcpd.h:455
char * print_hw_addr_or_client_id(struct packet *packet)
Definition: dhcp.c:115
struct interface_info * interface
Definition: dhcpd.h:433
char * print_client_identifier_from_packet(struct packet *packet)
Definition: dhcp.c:90
unsigned code
Definition: tree.h:349
int write_lease(struct lease *lease)
Definition: dhclient.c:2084
TIME valid_until
Definition: dhcpd.h:1033
void putULong(unsigned char *, u_int32_t)
Definition: convert.c:70
#define SV_ECHO_CLIENT_ID
Definition: dhcpd.h:803
#define SV_USE_LEASE_ADDR_FOR_DEFAULT_ROUTE
Definition: dhcpd.h:722
#define DEFAULT_CACHE_THRESHOLD
Definition: dhcpd.h:843
u_int16_t local_port
Definition: dhclient.c:94
#define FTS_BACKUP
Definition: dhcpd.h:542
Definition: dhcpd.h:405
struct pool * pool
Definition: dhcpd.h:577
char * name
Definition: dhcpd.h:963
int permitted(struct packet *packet, struct permit *permit_list)
Definition: dhcp.c:5051
int find_lease_by_hw_addr(struct lease **, const unsigned char *, unsigned, const char *, int)
Definition: mdb.c:2044
void eval_network_statements(struct option_state **network_options, struct packet *packet, struct group *network_group)
Builds option set from statements at the global and network scope.
Definition: dhcp.c:5447
#define DEFAULT_DEFAULT_LEASE_TIME
Definition: dhcpd.h:847
TIME atsfp
Definition: dhcpd.h:638
void check_pool_threshold(struct packet *packet, struct lease *lease, struct lease_state *state)
Definition: dhcp.c:2091
int authoritative
Definition: dhcpd.h:954
struct in_addr yiaddr
Definition: dhcp.h:56
#define cur_time
Definition: dhcpd.h:2109
struct lease * n_hw
Definition: dhcpd.h:566
int free_leases
Definition: dhcpd.h:1029
Definition: ip.h:47
#define SV_GET_LEASE_HOSTNAMES
Definition: dhcpd.h:720
ssize_t send_packet(struct interface_info *, struct packet *, struct dhcp_packet *, size_t, struct in_addr, struct sockaddr_in *, struct hardware *)
struct lease * n_uid
Definition: dhcpd.h:566
int cons_options(struct packet *inpacket, struct dhcp_packet *outpacket, struct lease *lease, struct client_state *client_state, int mms, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, int overload_avail, int terminate, int bootpp, struct data_string *prl, const char *vuname)
Definition: options.c:533
int index
Definition: tree.h:339
TIME starts
Definition: dhcpd.h:569
void get_server_source_address(struct in_addr *from, struct option_state *options, struct option_state *out_options, struct packet *packet)
Definition: dhcp.c:5361
u_int8_t flags
Definition: dhcpd.h:590
u_int32_t getUShort(const unsigned char *)
struct in_addr giaddr
Definition: dhclient.c:75
void dfree(void *, const char *, int)
Definition: alloc.c:145
struct permit * next
Definition: dhcpd.h:987
int lease_count
Definition: dhcpd.h:1028
struct host_decl * n_ipaddr
Definition: dhcpd.h:961
enum permit::@0 type
int option_chain_head_reference(struct option_chain_head **ptr, struct option_chain_head *bp, const char *file, int line)
Definition: alloc.c:67
int load_balance_mine(struct packet *, dhcp_failover_state_t *)
int packet_type
Definition: dhcpd.h:409
struct option_cache * lookup_option(struct universe *universe, struct option_state *options, unsigned code)
Definition: options.c:2465
#define DHCPDECLINE
Definition: dhcp.h:173
#define FTS_RESET
Definition: dhcpd.h:541
struct option * option
Definition: dhcpd.h:389
void echo_client_id(struct packet *packet, struct lease *lease, struct option_state *in_options, struct option_state *out_options)
Adds a dhcp-client-id option to a set of options Given a set of input options, it searches for echo-c...
Definition: dhcp.c:2040
int lease_limit
Definition: dhcpd.h:1094
int max_ack_delay_usecs
struct in_addr limited_broadcast
Definition: discover.c:54
int int log_info(const char *,...) __attribute__((__format__(__printf__
struct subnet * subnet
Definition: dhcpd.h:576
void * dmalloc(size_t, const char *, int)
Definition: alloc.c:57
unsigned short cannot_reuse
Definition: dhcpd.h:652
u_int16_t dhcp_check_relayport(struct packet *packet)
u_int32_t getULong(const unsigned char *)
struct shared_network * shared_network
Definition: dhcpd.h:1064
#define DHO_SUBNET_SELECTION
Definition: dhcp.h:161
#define PERSISTENT_FLAGS
Definition: dhcpd.h:602
int find_hosts_by_uid(struct host_decl **, const unsigned char *, unsigned, const char *, int)
Definition: mdb.c:629
int allocate_lease(struct lease **lp, struct packet *packet, struct pool *pool, int *peer_has_leases)
Definition: dhcp.c:4892
#define DHCPDISCOVER
Definition: dhcp.h:170
struct group * group
Definition: dhcpd.h:1070
#define DHO_DHCP_MAX_MESSAGE_SIZE
Definition: dhcp.h:148
TIME cltt
Definition: dhcpd.h:639
int server_id_check
Definition: dhcpd.c:82
struct universe ** universes
Definition: tables.c:967
Definition: inet.h:31
TIME valid_from
Definition: dhcpd.h:1032
Definition: dhcpd.h:986
int sv_echo_client_id
Definition: dhcpd.h:474
TIME max_lease_time
Definition: dhclient.c:55
int have_billing_classes
Definition: class.c:33
unsigned short uid_len
Definition: dhcpd.h:585
#define DHO_ROUTERS
Definition: dhcp.h:94
struct iaddr ip_addr
Definition: dhcpd.h:568
struct in_addr giaddr
Definition: dhcp.h:58
struct data_string * dhcp4o6_response
Definition: dhcpd.h:428
struct iaddr from
Definition: dhcpd.h:682
int option_state_dereference(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:911
Definition: dhcpd.h:947
void ack_lease(struct packet *packet, struct lease *lease, unsigned int offer, TIME when, char *msg, int ms_nulltp, struct host_decl *hp)
Definition: dhcp.c:2183
#define RESERVED_LEASE
Definition: dhcpd.h:593
struct timeval cur_tv
Definition: dispatch.c:35
struct shared_network * shared_network
Definition: dhcpd.h:448
int binding_scope_reference(struct binding_scope **ptr, struct binding_scope *bp, const char *file, int line)
Definition: alloc.c:1227
int got_server_identifier
Definition: dhcpd.h:670
#define SV_PING_TIMEOUT
Definition: dhcpd.h:755
#define LEASE_GET_FIRST(LQ)
Definition: dhcpd.h:258
binding_state_t rewind_binding_state
Definition: dhcpd.h:625
#define OPTION_HAD_NULLS
Definition: dhcpd.h:392
TIME tstp
Definition: dhcpd.h:636
int evaluate_boolean_option_cache(int *ignorep, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct option_cache *oc, const char *file, int line)
Definition: tree.c:2733
int make_const_data(struct expression **expr, const unsigned char *data, unsigned len, int terminated, int allocate, const char *file, int line)
Definition: tree.c:219
int ddns_updates(struct packet *, struct lease *, struct lease *, struct iasubopt *, struct iasubopt *, struct option_state *)
struct host_decl * host
Definition: dhcpd.h:575
#define SV_BOOT_UNKNOWN_CLIENTS
Definition: dhcpd.h:715
#define DHCPLEASEUNASSIGNED
Definition: dhcp.h:179
#define DEFAULT_PING_TIMEOUT
Definition: dhcpd.h:823
#define SV_LOG_THRESHOLD_LOW
Definition: dhcpd.h:801
int db_printable(const unsigned char *)
void use_host_decl_name(struct packet *packet, struct lease *lease, struct option_state *options)
Adds hostname option when use-host-decl-names is enabled.
Definition: dhcp.c:5606
int universe_count
Definition: dhcpd.h:398
time_t TIME
Definition: dhcpd.h:85
isc_boolean_t agent_options_stashed
Definition: dhcpd.h:464
int commit_leases()
Definition: dhclient.c:2079
unsigned char data[1]
Definition: tree.h:62
int find_lease(struct lease **lp, struct packet *packet, struct shared_network *share, int *ours, int *peer_has_leases, struct lease *ip_lease_in, const char *file, int line)
Definition: dhcp.c:4089
#define SV_CACHE_THRESHOLD
Definition: dhcpd.h:796
int find_lease_by_uid(struct lease **, const unsigned char *, unsigned, const char *, int)
Definition: mdb.c:2036
void dhcpinform(struct packet *packet, int ms_nulltp)
Definition: dhcp.c:1104
#define DHCPLEASEQUERY
Definition: dhcp.h:178
TIME tsfp
Definition: dhcpd.h:637
int expression_reference(struct expression **ptr, struct expression *src, const char *file, int line)
Definition: alloc.c:446
#define SV_DEFAULT_LEASE_TIME
Definition: dhcpd.h:710
#define SV_ADAPTIVE_LEASE_TIME_THRESHOLD
Definition: dhcpd.h:759
#define SV_ONE_LEASE_PER_CLIENT
Definition: dhcpd.h:719
#define STATIC_LEASE
Definition: dhcpd.h:591
#define UNICAST_BROADCAST_HACK
Definition: dhcpd.h:598
u_int8_t hbuf[HARDWARE_ADDR_LEN+1]
Definition: dhcpd.h:493
int class_count
Definition: dhcpd.h:454
int address_count
Definition: dhcpd.h:1375
u_int8_t hops
Definition: dhcp.h:51
int icmp_echorequest(struct iaddr *addr)
Definition: icmp.c:128
struct lease * next
Definition: dhcpd.h:561
#define DHO_VENDOR_CLASS_IDENTIFIER
Definition: dhcp.h:151
#define MAX_TIME
Definition: dhcpd.h:1614
struct iaddr client_addr
Definition: dhcpd.h:432
struct data_string data
Definition: dhcpd.h:390
struct universe agent_universe
Definition: stables.c:165
#define DHCPREQUEST
Definition: dhcp.h:172
struct ipv6_pool ** pools
const int dhcp_type_name_max
Definition: dhcp.c:82
option_code_hash_t * code_hash
Definition: tree.h:337
int flags
Definition: dhcpd.h:976
u_int16_t remote_port
Definition: dhclient.c:95
#define DHO_DHCP_RENEWAL_TIME
Definition: dhcp.h:149
unsigned char uid_buf[7]
Definition: dhcpd.h:587
u_int16_t relay_port
Definition: discover.c:47
#define DHO_DHCP_MESSAGE
Definition: dhcp.h:147
struct executable_statement * on_expiry
Definition: dhcpd.h:553
#define BOOTP_LEASE
Definition: dhcpd.h:592
struct shared_network * shared_network
Definition: dhcpd.h:1018
const char * file
Definition: dhcpd.h:3781
#define DHO_DHCP_CLIENT_IDENTIFIER
Definition: dhcp.h:152
char * name
Definition: dhcpd.h:1045
struct in_addr ciaddr
Definition: dhcpd.h:677
struct permit * permit_list
Definition: dhcpd.h:1019
struct data_string filename server_name
Definition: dhcpd.h:668
#define DHCPLEASEUNKNOWN
Definition: dhcp.h:180
TIME default_lease_time
Definition: dhclient.c:54
struct leasequeue * next
Definition: dhcpd.h:1432
int can_unicast_without_arp(struct interface_info *)
struct lease_state * new_lease_state(const char *, int)
int bill_class(struct lease *, struct class *)
Definition: class.c:295
isc_result_t dhcp_failover_send_updates(dhcp_failover_state_t *)
struct executable_statement * on_commit
Definition: dhcpd.h:554
void * universes[1]
Definition: dhcpd.h:401
#define RAI_RELAY_PORT
Definition: dhcp.h:190
Definition: dhcpd.h:1087
const unsigned char * data
Definition: tree.h:78
struct in_addr ciaddr
Definition: dhcp.h:55
int get_option_int(int *result, struct universe *universe, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct option_state *options, struct binding_scope **scope, unsigned code, const char *file, int line)
Definition: options.c:2320
#define DHO_DHCP_MESSAGE_TYPE
Definition: dhcp.h:144
int bind_ds_value(struct binding_scope **scope, const char *name, struct data_string *value)
Definition: tree.c:4080
TIME ends
Definition: dhcpd.h:569
struct binding_scope * scope
Definition: dhcpd.h:574
void data_string_copy(struct data_string *dest, const struct data_string *src, const char *file, int line)
Definition: alloc.c:1323
unsigned packet_length
Definition: dhcpd.h:408
struct hardware interface
Definition: dhcpd.h:964
LEASE_STRUCT backup
Definition: dhcpd.h:1024
TIME offered_expiry
Definition: dhcpd.h:662
int find_lease_by_ip_addr(struct lease **, struct iaddr, const char *, int)
Definition: mdb.c:2029
void dhcprequest(struct packet *packet, int ms_nulltp, struct lease *ip_lease)
Definition: dhcp.c:478
#define SV_STASH_AGENT_OPTIONS
Definition: dhcpd.h:746
struct in6_addr dhcpv6_link_address
Definition: dhcpd.h:418
struct packet * packet
Definition: dhcpd.h:660
unsigned char expiry[4]
Definition: dhcpd.h:667
u_int8_t op
Definition: dhcp.h:48
binding_state_t next_binding_state
Definition: dhcpd.h:623
#define DHCPOFFER
Definition: dhcp.h:171
LEASE_STRUCT abandoned
Definition: dhcpd.h:1025
void classify_client(struct packet *)
Definition: class.c:55
struct pool * pools
Definition: dhcpd.h:1052
int lease_mine_to_reallocate(struct lease *)
#define DHO_HOST_NAME
Definition: dhcp.h:103
struct group * group
Definition: dhcpd.h:973
struct buffer * buffer
Definition: tree.h:77
struct pool * next
Definition: dhcpd.h:1016
int logged
Definition: dhcpd.h:1038
char * client_hostname
Definition: dhcpd.h:573
struct group * group
Definition: dhcpd.h:1114
#define DHO_DHCP_REQUESTED_ADDRESS
Definition: dhcp.h:141
int backup_leases
Definition: dhcpd.h:1030
struct packet * dhcpv6_container_packet
Definition: dhcpd.h:422
#define FTS_ACTIVE
Definition: dhcpd.h:537
#define SV_PING_CHECKS
Definition: dhcpd.h:751
#define SV_RESERVE_INFINITE
Definition: dhcpd.h:756