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