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