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