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