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