ISC DHCP  4.3.4
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  }
3103 
3104  /* Record the hardware address, if given... */
3105  lt -> hardware_addr.hlen = packet -> raw -> hlen + 1;
3106  lt -> hardware_addr.hbuf [0] = packet -> raw -> htype;
3107  memcpy (&lt -> hardware_addr.hbuf [1], packet -> raw -> chaddr,
3108  sizeof packet -> raw -> chaddr);
3109 
3110  /*
3111  * If client has requested the lease become infinite, then it
3112  * doens't qualify for reuse even if it's younger than the
3113  * dhcp-cache-threshold.
3114  */
3115  if ((lt->flags & RESERVED_LEASE) && !(lease->flags & RESERVED_LEASE)) {
3116  log_debug ("Cannot reuse: lease is changing to RESERVED");
3117  lease->cannot_reuse = 1;
3118  }
3119 
3120  lt->flags |= lease->flags & ~PERSISTENT_FLAGS;
3121 
3122  /* If there are statements to execute when the lease is
3123  committed, execute them. */
3124  if (lease->on_star.on_commit && (!offer || offer == DHCPACK)) {
3125  execute_statements (NULL, packet, lt, NULL, packet->options,
3126  state->options, &lt->scope,
3127  lease->on_star.on_commit, NULL);
3128  if (lease->on_star.on_commit)
3130  (&lease->on_star.on_commit, MDL);
3131  }
3132 
3133 #ifdef NSUPDATE
3134  /* Perform DDNS updates, if configured to. */
3135  if ((!offer || offer == DHCPACK) &&
3136  (!(oc = lookup_option (&server_universe, state -> options,
3137  SV_DDNS_UPDATES)) ||
3138  evaluate_boolean_option_cache (&ignorep, packet, lt,
3139  (struct client_state *)0,
3140  packet -> options,
3141  state -> options,
3142  &lt -> scope, oc, MDL))) {
3143  ddns_updates(packet, lt, lease, NULL, NULL, state->options);
3144  }
3145 #endif /* NSUPDATE */
3146 
3147  /* Don't call supersede_lease on a mocked-up lease. */
3148  if (lease -> flags & STATIC_LEASE) {
3149  /* Copy the hardware address into the static lease
3150  structure. */
3151  lease -> hardware_addr.hlen = packet -> raw -> hlen + 1;
3152  lease -> hardware_addr.hbuf [0] = packet -> raw -> htype;
3153  memcpy (&lease -> hardware_addr.hbuf [1],
3154  packet -> raw -> chaddr,
3155  sizeof packet -> raw -> chaddr); /* XXX */
3156  } else {
3157  int commit = (!offer || (offer == DHCPACK));
3158 
3159  /* If dhcp-cache-threshold is enabled, see if "lease" can
3160  * be reused. */
3161  use_old_lease = reuse_lease(packet, lt, lease, state, offer);
3162  if (use_old_lease == 1) {
3163  commit = 0;
3164  }
3165 
3166 #if !defined(DELAYED_ACK) || defined(DHCP4o6)
3167  /* Install the new information on 'lt' onto the lease at
3168  * 'lease'. If this is a DHCPOFFER, it is a 'soft' promise,
3169  * if it is a DHCPACK, it is a 'hard' binding, so it needs
3170  * to be recorded and propogated immediately. If the update
3171  * fails, don't ACK it (or BOOTREPLY) either; we may give
3172  * the same lease to another client later, and that would be
3173  * a conflict.
3174  */
3175  if ((use_old_lease == 0) &&
3176  !supersede_lease(lease, lt, commit,
3177  offer == DHCPACK, offer == DHCPACK, 0)) {
3178 #else /* defined(DELAYED_ACK) && !defined(DHCP4o6) */
3179  /*
3180  * If there already isn't a need for a lease commit, and we
3181  * can just answer right away, set a flag to indicate this.
3182  */
3183  if (commit)
3184  enqueue = ISC_TRUE;
3185 
3186  /* Install the new information on 'lt' onto the lease at
3187  * 'lease'. We will not 'commit' this information to disk
3188  * yet (fsync()), we will 'propogate' the information if
3189  * this is BOOTP or a DHCPACK, but we will not 'pimmediate'ly
3190  * transmit failover binding updates (this is delayed until
3191  * after the fsync()). If the update fails, don't ACK it (or
3192  * BOOTREPLY either); we may give the same lease out to a
3193  * different client, and that would be a conflict.
3194  */
3195  if ((use_old_lease == 0) &&
3196  !supersede_lease(lease, lt, 0,
3197  !offer || offer == DHCPACK, 0, 0)) {
3198 #endif
3199  log_info ("%s: database update failed", msg);
3200  free_lease_state (state, MDL);
3201  lease_dereference (&lt, MDL);
3202  return;
3203  }
3204  }
3205  lease_dereference (&lt, MDL);
3206 
3207  /* Remember the interface on which the packet arrived. */
3208  state -> ip = packet -> interface;
3209 
3210  /* Remember the giaddr, xid, secs, flags and hops. */
3211  state -> giaddr = packet -> raw -> giaddr;
3212  state -> ciaddr = packet -> raw -> ciaddr;
3213  state -> xid = packet -> raw -> xid;
3214  state -> secs = packet -> raw -> secs;
3215  state -> bootp_flags = packet -> raw -> flags;
3216  state -> hops = packet -> raw -> hops;
3217  state -> offer = offer;
3218 
3219  /* If we're always supposed to broadcast to this client, set
3220  the broadcast bit in the bootp flags field. */
3221  if ((oc = lookup_option (&server_universe, state -> options,
3222  SV_ALWAYS_BROADCAST)) &&
3223  evaluate_boolean_option_cache (&ignorep, packet, lease,
3224  (struct client_state *)0,
3225  packet -> options, state -> options,
3226  &lease -> scope, oc, MDL))
3227  state -> bootp_flags |= htons (BOOTP_BROADCAST);
3228 
3229  /* Get the Maximum Message Size option from the packet, if one
3230  was sent. */
3231  oc = lookup_option (&dhcp_universe, packet -> options,
3233  if (oc &&
3234  evaluate_option_cache (&d1, packet, lease,
3235  (struct client_state *)0,
3236  packet -> options, state -> options,
3237  &lease -> scope, oc, MDL)) {
3238  if (d1.len == sizeof (u_int16_t))
3239  state -> max_message_size = getUShort (d1.data);
3240  data_string_forget (&d1, MDL);
3241  } else {
3242  oc = lookup_option (&dhcp_universe, state -> options,
3244  if (oc &&
3245  evaluate_option_cache (&d1, packet, lease,
3246  (struct client_state *)0,
3247  packet -> options, state -> options,
3248  &lease -> scope, oc, MDL)) {
3249  if (d1.len == sizeof (u_int16_t))
3250  state -> max_message_size =
3251  getUShort (d1.data);
3252  data_string_forget (&d1, MDL);
3253  }
3254  }
3255 
3256  /* Get the Subnet Selection option from the packet, if one
3257  was sent. */
3258  if ((oc = lookup_option (&dhcp_universe, packet -> options,
3260 
3261  /* Make a copy of the data. */
3262  struct option_cache *noc = (struct option_cache *)0;
3263  if (option_cache_allocate (&noc, MDL)) {
3264  if (oc -> data.len)
3265  data_string_copy (&noc -> data,
3266  &oc -> data, MDL);
3267  if (oc -> expression)
3269  oc -> expression, MDL);
3270  if (oc -> option)
3271  option_reference(&(noc->option), oc->option,
3272  MDL);
3273 
3274  save_option (&dhcp_universe, state -> options, noc);
3275  option_cache_dereference (&noc, MDL);
3276  }
3277  }
3278 
3279  /* Now, if appropriate, put in DHCP-specific options that
3280  override those. */
3281  if (state -> offer) {
3283  oc = (struct option_cache *)0;
3284  if (option_cache_allocate (&oc, MDL)) {
3285  if (make_const_data (&oc -> expression,
3286  &state -> offer, 1, 0, 0, MDL)) {
3287  option_code_hash_lookup(&oc->option,
3289  &i, 0, MDL);
3291  state -> options, oc);
3292  }
3294  }
3295 
3296  get_server_source_address(&from, state->options,
3297  state->options, packet);
3298  memcpy(state->from.iabuf, &from, sizeof(from));
3299  state->from.len = sizeof(from);
3300 
3301  offered_lease_time =
3302  state -> offered_expiry - cur_time;
3303 
3304  putULong(state->expiry, (u_int32_t)offered_lease_time);
3305  i = DHO_DHCP_LEASE_TIME;
3306  oc = (struct option_cache *)0;
3307  if (option_cache_allocate (&oc, MDL)) {
3308  if (make_const_data(&oc->expression, state->expiry,
3309  4, 0, 0, MDL)) {
3310  option_code_hash_lookup(&oc->option,
3312  &i, 0, MDL);
3314  state -> options, oc);
3315  }
3317  }
3318 
3319  /*
3320  * Validate any configured renew or rebinding times against
3321  * the determined lease time. Do rebinding first so that
3322  * the renew time can be validated against the rebind time.
3323  */
3324  if ((oc = lookup_option(&dhcp_universe, state->options,
3325  DHO_DHCP_REBINDING_TIME)) != NULL &&
3326  evaluate_option_cache(&d1, packet, lease, NULL,
3327  packet->options, state->options,
3328  &lease->scope, oc, MDL)) {
3329  TIME rebind_time = getULong(d1.data);
3330 
3331  /* Drop the configured (invalid) rebinding time. */
3332  if (rebind_time >= offered_lease_time)
3335  else /* XXX: variable is reused. */
3336  offered_lease_time = rebind_time;
3337 
3338  data_string_forget(&d1, MDL);
3339  }
3340 
3341  if ((oc = lookup_option(&dhcp_universe, state->options,
3342  DHO_DHCP_RENEWAL_TIME)) != NULL &&
3343  evaluate_option_cache(&d1, packet, lease, NULL,
3344  packet->options, state->options,
3345  &lease->scope, oc, MDL)) {
3346  if (getULong(d1.data) >= offered_lease_time)
3349 
3350  data_string_forget(&d1, MDL);
3351  }
3352  } else {
3353  /* XXXSK: should we use get_server_source_address() here? */
3354  if (state -> ip -> address_count) {
3355  state -> from.len =
3356  sizeof state -> ip -> addresses [0];
3357  memcpy (state -> from.iabuf,
3358  &state -> ip -> addresses [0],
3359  state -> from.len);
3360  }
3361  }
3362 
3363  /* Figure out the address of the boot file server. */
3364  memset (&state -> siaddr, 0, sizeof state -> siaddr);
3365  if ((oc =
3367  state -> options, SV_NEXT_SERVER))) {
3368  if (evaluate_option_cache (&d1, packet, lease,
3369  (struct client_state *)0,
3370  packet -> options, state -> options,
3371  &lease -> scope, oc, MDL)) {
3372  /* If there was more than one answer,
3373  take the first. */
3374  if (d1.len >= 4 && d1.data)
3375  memcpy (&state -> siaddr, d1.data, 4);
3376  data_string_forget (&d1, MDL);
3377  }
3378  }
3379 
3380  /* Use the subnet mask from the subnet declaration if no other
3381  mask has been provided. */
3382  i = DHO_SUBNET_MASK;
3383  if (!lookup_option (&dhcp_universe, state -> options, i)) {
3384  oc = (struct option_cache *)0;
3385  if (option_cache_allocate (&oc, MDL)) {
3386  if (make_const_data (&oc -> expression,
3387  lease -> subnet -> netmask.iabuf,
3388  lease -> subnet -> netmask.len,
3389  0, 0, MDL)) {
3390  option_code_hash_lookup(&oc->option,
3392  &i, 0, MDL);
3394  state -> options, oc);
3395  }
3397  }
3398  }
3399 
3400  /* Use the name of the host declaration if there is one
3401  and no hostname has otherwise been provided, and if the
3402  use-host-decl-name flag is set. */
3403  use_host_decl_name(packet, lease, state->options);
3404 
3405  /* Send client_id back if we received it and echo-client-id is on. */
3406  echo_client_id(packet, lease, state->options, state->options);
3407 
3408  /* If we don't have a hostname yet, and we've been asked to do
3409  a reverse lookup to find the hostname, do it. */
3410  i = DHO_HOST_NAME;
3412  if (!lookup_option(&dhcp_universe, state->options, i) &&
3414  (&ignorep, packet, lease, NULL,
3415  packet->options, state->options, &lease->scope,
3416  lookup_option (&server_universe, state->options, j), MDL)) {
3417  struct in_addr ia;
3418  struct hostent *h;
3419 
3420  memcpy (&ia, lease -> ip_addr.iabuf, 4);
3421 
3422  h = gethostbyaddr ((char *)&ia, sizeof ia, AF_INET);
3423  if (!h)
3424  log_error ("No hostname for %s", inet_ntoa (ia));
3425  else {
3426  oc = (struct option_cache *)0;
3427  if (option_cache_allocate (&oc, MDL)) {
3428  if (make_const_data (&oc -> expression,
3429  ((unsigned char *)
3430  h -> h_name),
3431  strlen (h -> h_name) + 1,
3432  1, 1, MDL)) {
3433  option_code_hash_lookup(&oc->option,
3435  &i, 0, MDL);
3437  state -> options, oc);
3438  }
3440  }
3441  }
3442  }
3443 
3444  /* If so directed, use the leased IP address as the router address.
3445  This supposedly makes Win95 machines ARP for all IP addresses,
3446  so if the local router does proxy arp, you win. */
3447 
3449  (&ignorep, packet, lease, (struct client_state *)0,
3450  packet -> options, state -> options, &lease -> scope,
3451  lookup_option (&server_universe, state -> options,
3453  i = DHO_ROUTERS;
3454  oc = lookup_option (&dhcp_universe, state -> options, i);
3455  if (!oc) {
3456  oc = (struct option_cache *)0;
3457  if (option_cache_allocate (&oc, MDL)) {
3458  if (make_const_data (&oc -> expression,
3459  lease -> ip_addr.iabuf,
3460  lease -> ip_addr.len,
3461  0, 0, MDL)) {
3462  option_code_hash_lookup(&oc->option,
3464  &i, 0, MDL);
3466  state -> options, oc);
3467  }
3468  option_cache_dereference (&oc, MDL);
3469  }
3470  }
3471  }
3472 
3473  /* If a site option space has been specified, use that for
3474  site option codes. */
3476  if ((oc = lookup_option (&server_universe, state -> options, i)) &&
3477  evaluate_option_cache (&d1, packet, lease,
3478  (struct client_state *)0,
3479  packet -> options, state -> options,
3480  &lease -> scope, oc, MDL)) {
3481  struct universe *u = (struct universe *)0;
3482 
3483  if (!universe_hash_lookup (&u, universe_hash,
3484  (const char *)d1.data, d1.len,
3485  MDL)) {
3486  log_error ("unknown option space %s.", d1.data);
3487  return;
3488  }
3489 
3490  state -> options -> site_universe = u -> index;
3491  state->options->site_code_min = find_min_site_code(u);
3492  data_string_forget (&d1, MDL);
3493  } else {
3494  state -> options -> site_code_min = 0;
3495  state -> options -> site_universe = dhcp_universe.index;
3496  }
3497 
3498  /* If the client has provided a list of options that it wishes
3499  returned, use it to prioritize. If there's a parameter
3500  request list in scope, use that in preference. Otherwise
3501  use the default priority list. */
3502 
3503  oc = lookup_option (&dhcp_universe, state -> options,
3505 
3506  if (!oc)
3507  oc = lookup_option (&dhcp_universe, packet -> options,
3509  if (oc)
3510  evaluate_option_cache (&state -> parameter_request_list,
3511  packet, lease, (struct client_state *)0,
3512  packet -> options, state -> options,
3513  &lease -> scope, oc, MDL);
3514 
3515 #ifdef DEBUG_PACKET
3516  dump_packet (packet);
3517  dump_raw ((unsigned char *)packet -> raw, packet -> packet_length);
3518 #endif
3519 
3520  lease -> state = state;
3521 
3522  log_info ("%s", msg);
3523 
3524  /* Hang the packet off the lease state. */
3525  packet_reference (&lease -> state -> packet, packet, MDL);
3526 
3527  /* If this is a DHCPOFFER, ping the lease address before actually
3528  sending the offer. */
3529  if (offer == DHCPOFFER && !(lease -> flags & STATIC_LEASE) &&
3530  ((cur_time - lease_cltt) > 60) &&
3531  (!(oc = lookup_option (&server_universe, state -> options,
3532  SV_PING_CHECKS)) ||
3533  evaluate_boolean_option_cache (&ignorep, packet, lease,
3534  (struct client_state *)0,
3535  packet -> options,
3536  state -> options,
3537  &lease -> scope, oc, MDL))) {
3538  icmp_echorequest (&lease -> ip_addr);
3539 
3540  /* Determine whether to use configured or default ping timeout.
3541  */
3542  if ((oc = lookup_option (&server_universe, state -> options,
3543  SV_PING_TIMEOUT)) &&
3544  evaluate_option_cache (&d1, packet, lease, NULL,
3545  packet -> options,
3546  state -> options,
3547  &lease -> scope, oc, MDL)) {
3548  if (d1.len == sizeof (u_int32_t))
3549  ping_timeout = getULong (d1.data);
3550  else
3551  ping_timeout = DEFAULT_PING_TIMEOUT;
3552 
3553  data_string_forget (&d1, MDL);
3554  } else
3555  ping_timeout = DEFAULT_PING_TIMEOUT;
3556 
3557 #ifdef DEBUG
3558  log_debug ("Ping timeout: %ld", (long)ping_timeout);
3559 #endif
3560 
3561  /*
3562  * Set a timeout for 'ping-timeout' seconds from NOW, including
3563  * current microseconds. As ping-timeout defaults to 1, the
3564  * exclusion of current microseconds causes a value somewhere
3565  * /between/ zero and one.
3566  */
3567  tv.tv_sec = cur_tv.tv_sec + ping_timeout;
3568  tv.tv_usec = cur_tv.tv_usec;
3569  add_timeout (&tv, lease_ping_timeout, lease,
3570  (tvref_t)lease_reference,
3571  (tvunref_t)lease_dereference);
3573  } else {
3574  lease->cltt = cur_time;
3575 #if defined(DELAYED_ACK) && !defined(DHCP4o6)
3576  if (enqueue)
3577  delayed_ack_enqueue(lease);
3578  else
3579 #endif
3580  dhcp_reply(lease);
3581  }
3582 }
3583 
3584 #if defined(DELAYED_ACK)
3585 
3586 /*
3587  * CC: queue single ACK:
3588  * - write the lease (but do not fsync it yet)
3589  * - add to double linked list
3590  * - commit if more than xx ACKs pending
3591  * - if necessary set the max timer and bump the next timer
3592  * but only up to the max timer value.
3593  */
3594 
3595 static void
3596 delayed_ack_enqueue(struct lease *lease)
3597 {
3598  struct leasequeue *q;
3599 
3600  if (!write_lease(lease))
3601  return;
3602  if (free_ackqueue) {
3603  q = free_ackqueue;
3604  free_ackqueue = q->next;
3605  } else {
3606  q = ((struct leasequeue *)
3607  dmalloc(sizeof(struct leasequeue), MDL));
3608  if (!q)
3609  log_fatal("delayed_ack_enqueue: no memory!");
3610  }
3611  memset(q, 0, sizeof *q);
3612  /* prepend to ackqueue*/
3613  lease_reference(&q->lease, lease, MDL);
3614  q->next = ackqueue_head;
3615  ackqueue_head = q;
3616  if (!ackqueue_tail)
3617  ackqueue_tail = q;
3618  else
3619  q->next->prev = q;
3620 
3621  outstanding_acks++;
3622  if (outstanding_acks > max_outstanding_acks) {
3623  /* Cancel any pending timeout and call handler directly */
3624  cancel_timeout(delayed_acks_timer, NULL);
3625  delayed_acks_timer(NULL);
3626  } else {
3627  struct timeval next_fsync;
3628 
3629  if (max_fsync.tv_sec == 0 && max_fsync.tv_usec == 0) {
3630  /* set the maximum time we'll wait */
3631  max_fsync.tv_sec = cur_tv.tv_sec + max_ack_delay_secs;
3632  max_fsync.tv_usec = cur_tv.tv_usec +
3634 
3635  if (max_fsync.tv_usec >= 1000000) {
3636  max_fsync.tv_sec++;
3637  max_fsync.tv_usec -= 1000000;
3638  }
3639  }
3640 
3641  /* Set the timeout */
3642  next_fsync.tv_sec = cur_tv.tv_sec;
3643  next_fsync.tv_usec = cur_tv.tv_usec + min_ack_delay_usecs;
3644  if (next_fsync.tv_usec >= 1000000) {
3645  next_fsync.tv_sec++;
3646  next_fsync.tv_usec -= 1000000;
3647  }
3648  /* but not more than the max */
3649  if ((next_fsync.tv_sec > max_fsync.tv_sec) ||
3650  ((next_fsync.tv_sec == max_fsync.tv_sec) &&
3651  (next_fsync.tv_usec > max_fsync.tv_usec))) {
3652  next_fsync.tv_sec = max_fsync.tv_sec;
3653  next_fsync.tv_usec = max_fsync.tv_usec;
3654  }
3655 
3656  add_timeout(&next_fsync, delayed_acks_timer, NULL,
3657  (tvref_t) NULL, (tvunref_t) NULL);
3658  }
3659 }
3660 
3661 /* Processes any delayed acks:
3662  * Commits the leases and then for each delayed ack:
3663  * - Update the failover peer if we're in failover
3664  * - Send the REPLY to the client
3665  */
3666 static void
3667 delayed_acks_timer(void *foo)
3668 {
3669  struct leasequeue *ack, *p;
3670 
3671  /* Reset max fsync */
3672  memset(&max_fsync, 0, sizeof(max_fsync));
3673 
3674  if (!outstanding_acks) {
3675  /* Nothing to do, so punt, shouldn't happen? */
3676  return;
3677  }
3678 
3679  /* Commit the leases first */
3680  commit_leases();
3681 
3682  /* Now process the delayed ACKs
3683  - update failover peer
3684  - send out the ACK packets
3685  - move the queue slots to the free list
3686  */
3687 
3688  /* process from bottom to retain packet order */
3689  for (ack = ackqueue_tail ; ack ; ack = p) {
3690  p = ack->prev;
3691 
3692 #if defined(FAILOVER_PROTOCOL)
3693  /* If we're in failover we need to send any deferred
3694  * bind updates as well as the replies */
3695  if (ack->lease->pool) {
3696  dhcp_failover_state_t *fpeer;
3697 
3698  fpeer = ack->lease->pool->failover_peer;
3699  if (fpeer && fpeer->link_to_peer) {
3701  }
3702  }
3703 #endif
3704 
3705  /* dhcp_reply() requires that the reply state still be valid */
3706  if (ack->lease->state == NULL)
3707  log_error("delayed ack for %s has gone stale",
3708  piaddr(ack->lease->ip_addr));
3709  else {
3710  dhcp_reply(ack->lease);
3711  }
3712 
3713  lease_dereference(&ack->lease, MDL);
3714  ack->next = free_ackqueue;
3715  free_ackqueue = ack;
3716  }
3717 
3718  ackqueue_head = NULL;
3719  ackqueue_tail = NULL;
3720  outstanding_acks = 0;
3721 }
3722 
3723 #if defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
3724 void
3725 relinquish_ackqueue(void)
3726 {
3727  struct leasequeue *q, *n;
3728 
3729  for (q = ackqueue_head ; q ; q = n) {
3730  n = q->next;
3731  dfree(q, MDL);
3732  }
3733  for (q = free_ackqueue ; q ; q = n) {
3734  n = q->next;
3735  dfree(q, MDL);
3736  }
3737 }
3738 #endif
3739 
3740 #endif /* defined(DELAYED_ACK) */
3741 
3742 void dhcp_reply (lease)
3743  struct lease *lease;
3744 {
3745  int bufs = 0;
3746  unsigned packet_length;
3747  struct dhcp_packet raw;
3748  struct sockaddr_in to;
3749  struct in_addr from;
3750  struct hardware hto;
3751  int result;
3752  struct lease_state *state = lease -> state;
3753  int nulltp, bootpp, unicastp = 1;
3754  struct data_string d1;
3755  const char *s;
3756 
3757  if (!state)
3758  log_fatal ("dhcp_reply was supplied lease with no state!");
3759 
3760  /* Compose a response for the client... */
3761  memset (&raw, 0, sizeof raw);
3762  memset (&d1, 0, sizeof d1);
3763 
3764  /* Copy in the filename if given; otherwise, flag the filename
3765  buffer as available for options. */
3766  if (state -> filename.len && state -> filename.data) {
3767  memcpy (raw.file,
3768  state -> filename.data,
3769  state -> filename.len > sizeof raw.file
3770  ? sizeof raw.file : state -> filename.len);
3771  if (sizeof raw.file > state -> filename.len)
3772  memset (&raw.file [state -> filename.len], 0,
3773  (sizeof raw.file) - state -> filename.len);
3774  else
3775  log_info("file name longer than packet field "
3776  "truncated - field: %lu name: %d %.*s",
3777  (unsigned long)sizeof(raw.file),
3778  state->filename.len, (int)state->filename.len,
3779  state->filename.data);
3780  } else
3781  bufs |= 1;
3782 
3783  /* Copy in the server name if given; otherwise, flag the
3784  server_name buffer as available for options. */
3785  if (state -> server_name.len && state -> server_name.data) {
3786  memcpy (raw.sname,
3787  state -> server_name.data,
3788  state -> server_name.len > sizeof raw.sname
3789  ? sizeof raw.sname : state -> server_name.len);
3790  if (sizeof raw.sname > state -> server_name.len)
3791  memset (&raw.sname [state -> server_name.len], 0,
3792  (sizeof raw.sname) - state -> server_name.len);
3793  else
3794  log_info("server name longer than packet field "
3795  "truncated - field: %lu name: %d %.*s",
3796  (unsigned long)sizeof(raw.sname),
3797  state->server_name.len,
3798  (int)state->server_name.len,
3799  state->server_name.data);
3800  } else
3801  bufs |= 2; /* XXX */
3802 
3803  memcpy (raw.chaddr,
3804  &lease -> hardware_addr.hbuf [1], sizeof raw.chaddr);
3805  raw.hlen = lease -> hardware_addr.hlen - 1;
3806  raw.htype = lease -> hardware_addr.hbuf [0];
3807 
3808  /* See if this is a Microsoft client that NUL-terminates its
3809  strings and expects us to do likewise... */
3810  if (lease -> flags & MS_NULL_TERMINATION)
3811  nulltp = 1;
3812  else
3813  nulltp = 0;
3814 
3815  /* See if this is a bootp client... */
3816  if (state -> offer)
3817  bootpp = 0;
3818  else
3819  bootpp = 1;
3820 
3821  /* Insert such options as will fit into the buffer. */
3822  packet_length = cons_options (state -> packet, &raw, lease,
3823  (struct client_state *)0,
3824  state -> max_message_size,
3825  state -> packet -> options,
3826  state -> options, &global_scope,
3827  bufs, nulltp, bootpp,
3828  &state -> parameter_request_list,
3829  (char *)0);
3830 
3831  memcpy (&raw.ciaddr, &state -> ciaddr, sizeof raw.ciaddr);
3832  memcpy (&raw.yiaddr, lease -> ip_addr.iabuf, 4);
3833  raw.siaddr = state -> siaddr;
3834  raw.giaddr = state -> giaddr;
3835 
3836  raw.xid = state -> xid;
3837  raw.secs = state -> secs;
3838  raw.flags = state -> bootp_flags;
3839  raw.hops = state -> hops;
3840  raw.op = BOOTREPLY;
3841 
3842  if (lease -> client_hostname) {
3843  if ((strlen (lease -> client_hostname) <= 64) &&
3844  db_printable((unsigned char *)lease->client_hostname))
3845  s = lease -> client_hostname;
3846  else
3847  s = "Hostname Unsuitable for Printing";
3848  } else
3849  s = (char *)0;
3850 
3851  /* Make sure outgoing packets are at least as big
3852  as a BOOTP packet. */
3853  if (packet_length < BOOTP_MIN_LEN)
3854  packet_length = BOOTP_MIN_LEN;
3855 
3856 #if defined(DHCPv6) && defined(DHCP4o6)
3857  if (dhcpv4_over_dhcpv6 && (state->packet->dhcp4o6_response != NULL)) {
3858  /* Say what we're doing... */
3859  log_info ("DHCP4o6 %s on %s to %s %s%s%svia %s",
3860  (state -> offer
3861  ? (state -> offer == DHCPACK
3862  ? "DHCPACK" : "DHCPOFFER")
3863  : "BOOTREPLY"),
3864  piaddr (lease -> ip_addr),
3865  (lease -> hardware_addr.hlen
3866  ? print_hw_addr (lease -> hardware_addr.hbuf [0],
3867  lease -> hardware_addr.hlen - 1,
3868  &lease -> hardware_addr.hbuf [1])
3869  : print_hex_1(lease->uid_len, lease->uid, 60)),
3870  s ? "(" : "", s ? s : "", s ? ") " : "",
3871  piaddr(state->packet->client_addr));
3872 
3873  /* fill dhcp4o6_response */
3875  state->packet->dhcp4o6_response->buffer = NULL;
3877  packet_length, MDL)) {
3878  log_fatal("No memory to store DHCP4o6 reply.");
3879  }
3880  state->packet->dhcp4o6_response->data =
3881  state->packet->dhcp4o6_response->buffer->data;
3882  memcpy(state->packet->dhcp4o6_response->buffer->data,
3883  &raw, packet_length);
3884 
3885  /* done */
3886  free_lease_state (state, MDL);
3887  lease -> state = (struct lease_state *)0;
3888 
3889  return;
3890  }
3891 #endif
3892 
3893  /* Say what we're doing... */
3894  log_info ("%s on %s to %s %s%s%svia %s",
3895  (state -> offer
3896  ? (state -> offer == DHCPACK ? "DHCPACK" : "DHCPOFFER")
3897  : "BOOTREPLY"),
3898  piaddr (lease -> ip_addr),
3899  (lease -> hardware_addr.hlen > 1
3900  ? print_hw_addr (lease -> hardware_addr.hbuf [0],
3901  lease -> hardware_addr.hlen - 1,
3902  &lease -> hardware_addr.hbuf [1])
3903  : print_hex_1(lease->uid_len, lease->uid, 60)),
3904  s ? "(" : "", s ? s : "", s ? ") " : "",
3905  (state -> giaddr.s_addr
3906  ? inet_ntoa (state -> giaddr)
3907  : state -> ip -> name));
3908 
3909 #ifdef DEBUG_PACKET
3910  dump_raw ((unsigned char *)&raw, packet_length);
3911 #endif
3912 
3913  /* Set up the hardware address... */
3914  hto.hlen = lease -> hardware_addr.hlen;
3915  memcpy (hto.hbuf, lease -> hardware_addr.hbuf, hto.hlen);
3916 
3917  to.sin_family = AF_INET;
3918 #ifdef HAVE_SA_LEN
3919  to.sin_len = sizeof to;
3920 #endif
3921  memset (to.sin_zero, 0, sizeof to.sin_zero);
3922 
3923  /* If this was gatewayed, send it back to the gateway... */
3924  if (raw.giaddr.s_addr) {
3925  to.sin_addr = raw.giaddr;
3926  if (raw.giaddr.s_addr != htonl (INADDR_LOOPBACK))
3927  to.sin_port = local_port;
3928  else
3929  to.sin_port = remote_port; /* For debugging. */
3930 
3931  if (fallback_interface) {
3932  result = send_packet(fallback_interface, NULL, &raw,
3933  packet_length, raw.siaddr, &to,
3934  NULL);
3935  if (result < 0) {
3936  log_error ("%s:%d: Failed to send %d byte long "
3937  "packet over %s interface.", MDL,
3938  packet_length,
3939  fallback_interface->name);
3940  }
3941 
3942 
3943  free_lease_state (state, MDL);
3944  lease -> state = (struct lease_state *)0;
3945  return;
3946  }
3947 
3948  /* If the client is RENEWING, unicast to the client using the
3949  regular IP stack. Some clients, particularly those that
3950  follow RFC1541, are buggy, and send both ciaddr and server
3951  identifier. We deal with this situation by assuming that
3952  if we got both dhcp-server-identifier and ciaddr, and
3953  giaddr was not set, then the client is on the local
3954  network, and we can therefore unicast or broadcast to it
3955  successfully. A client in REQUESTING state on another
3956  network that's making this mistake will have set giaddr,
3957  and will therefore get a relayed response from the above
3958  code. */
3959  } else if (raw.ciaddr.s_addr &&
3960  !((state -> got_server_identifier ||
3961  (raw.flags & htons (BOOTP_BROADCAST))) &&
3962  /* XXX This won't work if giaddr isn't zero, but it is: */
3963  (state -> shared_network ==
3964  lease -> subnet -> shared_network)) &&
3965  state -> offer == DHCPACK) {
3966  to.sin_addr = raw.ciaddr;
3967  to.sin_port = remote_port;
3968 
3969  if (fallback_interface) {
3970  result = send_packet(fallback_interface, NULL, &raw,
3971  packet_length, raw.siaddr, &to,
3972  NULL);
3973  if (result < 0) {
3974  log_error("%s:%d: Failed to send %d byte long"
3975  " packet over %s interface.", MDL,
3976  packet_length,
3977  fallback_interface->name);
3978  }
3979 
3980  free_lease_state (state, MDL);
3981  lease -> state = (struct lease_state *)0;
3982  return;
3983  }
3984 
3985  /* If it comes from a client that already knows its address
3986  and is not requesting a broadcast response, and we can
3987  unicast to a client without using the ARP protocol, sent it
3988  directly to that client. */
3989  } else if (!(raw.flags & htons (BOOTP_BROADCAST)) &&
3990  can_unicast_without_arp (state -> ip)) {
3991  to.sin_addr = raw.yiaddr;
3992  to.sin_port = remote_port;
3993 
3994  /* Otherwise, broadcast it on the local network. */
3995  } else {
3996  to.sin_addr = limited_broadcast;
3997  to.sin_port = remote_port;
3998  if (!(lease -> flags & UNICAST_BROADCAST_HACK))
3999  unicastp = 0;
4000  }
4001 
4002  memcpy (&from, state -> from.iabuf, sizeof from);
4003 
4004  result = send_packet(state->ip, NULL, &raw, packet_length,
4005  from, &to, unicastp ? &hto : NULL);
4006  if (result < 0) {
4007  log_error ("%s:%d: Failed to send %d byte long "
4008  "packet over %s interface.", MDL,
4009  packet_length, state->ip->name);
4010  }
4011 
4012 
4013  /* Free all of the entries in the option_state structure
4014  now that we're done with them. */
4015 
4016  free_lease_state (state, MDL);
4017  lease -> state = (struct lease_state *)0;
4018 }
4019 
4020 int find_lease (struct lease **lp,
4021  struct packet *packet, struct shared_network *share, int *ours,
4022  int *peer_has_leases, struct lease *ip_lease_in,
4023  const char *file, int line)
4024 {
4025  struct lease *uid_lease = (struct lease *)0;
4026  struct lease *ip_lease = (struct lease *)0;
4027  struct lease *hw_lease = (struct lease *)0;
4028  struct lease *lease = (struct lease *)0;
4029  struct iaddr cip;
4030  struct host_decl *hp = (struct host_decl *)0;
4031  struct host_decl *host = (struct host_decl *)0;
4032  struct lease *fixed_lease = (struct lease *)0;
4033  struct lease *next = (struct lease *)0;
4034  struct option_cache *oc;
4035  struct data_string d1;
4036  int have_client_identifier = 0;
4037  struct data_string client_identifier;
4038  struct hardware h;
4039 
4040 #if defined(FAILOVER_PROTOCOL)
4041  /* Quick check to see if the peer has leases. */
4042  if (peer_has_leases) {
4043  struct pool *pool;
4044 
4045  for (pool = share->pools ; pool ; pool = pool->next) {
4046  dhcp_failover_state_t *peer = pool->failover_peer;
4047 
4048  if (peer &&
4049  ((peer->i_am == primary && pool->backup_leases) ||
4050  (peer->i_am == secondary && pool->free_leases))) {
4051  *peer_has_leases = 1;
4052  break;
4053  }
4054  }
4055  }
4056 #endif /* FAILOVER_PROTOCOL */
4057 
4058  if (packet -> raw -> ciaddr.s_addr) {
4059  cip.len = 4;
4060  memcpy (cip.iabuf, &packet -> raw -> ciaddr, 4);
4061  } else {
4062  /* Look up the requested address. */
4063  oc = lookup_option (&dhcp_universe, packet -> options,
4065  memset (&d1, 0, sizeof d1);
4066  if (oc &&
4067  evaluate_option_cache (&d1, packet, (struct lease *)0,
4068  (struct client_state *)0,
4069  packet -> options,
4070  (struct option_state *)0,
4071  &global_scope, oc, MDL)) {
4072  packet -> got_requested_address = 1;
4073  cip.len = 4;
4074  memcpy (cip.iabuf, d1.data, cip.len);
4075  data_string_forget (&d1, MDL);
4076  } else
4077  cip.len = 0;
4078  }
4079 
4080  /* Try to find a host or lease that's been assigned to the
4081  specified unique client identifier. */
4082  oc = lookup_option (&dhcp_universe, packet -> options,
4084  if (!oc)
4085  oc = lookup_option (&dhcp_universe, packet -> options,
4087  memset (&client_identifier, 0, sizeof client_identifier);
4088  if (oc &&
4089  evaluate_option_cache (&client_identifier,
4090  packet, (struct lease *)0,
4091  (struct client_state *)0,
4092  packet -> options, (struct option_state *)0,
4093  &global_scope, oc, MDL)) {
4094  /* Remember this for later. */
4095  have_client_identifier = 1;
4096 
4097  /* First, try to find a fixed host entry for the specified
4098  client identifier... */
4099  if (find_hosts_by_uid (&hp, client_identifier.data,
4100  client_identifier.len, MDL)) {
4101  /* Remember if we know of this client. */
4102  packet -> known = 1;
4103  mockup_lease (&fixed_lease, packet, share, hp);
4104  }
4105 
4106 #if defined (DEBUG_FIND_LEASE)
4107  if (fixed_lease) {
4108  log_info ("Found host for client identifier: %s.",
4109  piaddr (fixed_lease -> ip_addr));
4110  }
4111 #endif
4112  if (hp) {
4113  if (!fixed_lease) /* Save the host if we found one. */
4114  host_reference (&host, hp, MDL);
4115  host_dereference (&hp, MDL);
4116  }
4117 
4118  find_lease_by_uid (&uid_lease, client_identifier.data,
4119  client_identifier.len, MDL);
4120  }
4121 
4122  /* If we didn't find a fixed lease using the uid, try doing
4123  it with the hardware address... */
4124  if (!fixed_lease && !host) {
4125  if (find_hosts_by_haddr (&hp, packet -> raw -> htype,
4126  packet -> raw -> chaddr,
4127  packet -> raw -> hlen, MDL)) {
4128  /* Remember if we know of this client. */
4129  packet -> known = 1;
4130  if (host)
4131  host_dereference (&host, MDL);
4132  host_reference (&host, hp, MDL);
4133  host_dereference (&hp, MDL);
4134  mockup_lease (&fixed_lease, packet, share, host);
4135 #if defined (DEBUG_FIND_LEASE)
4136  if (fixed_lease) {
4137  log_info ("Found host for link address: %s.",
4138  piaddr (fixed_lease -> ip_addr));
4139  }
4140 #endif
4141  }
4142  }
4143 
4144  /* Finally, if we haven't found anything yet try again with the
4145  * host-identifier option ... */
4146  if (!fixed_lease && !host) {
4147  if (find_hosts_by_option(&hp, packet,
4148  packet->options, MDL) == 1) {
4149  packet->known = 1;
4150  if (host)
4151  host_dereference(&host, MDL);
4152  host_reference(&host, hp, MDL);
4153  host_dereference(&hp, MDL);
4154  mockup_lease (&fixed_lease, packet, share, host);
4155 #if defined (DEBUG_FIND_LEASE)
4156  if (fixed_lease) {
4157  log_info ("Found host via host-identifier");
4158  }
4159 #endif
4160  }
4161  }
4162 
4163  /* If fixed_lease is present but does not match the requested
4164  IP address, and this is a DHCPREQUEST, then we can't return
4165  any other lease, so we might as well return now. */
4166  if (packet -> packet_type == DHCPREQUEST && fixed_lease &&
4167  (fixed_lease -> ip_addr.len != cip.len ||
4168  memcmp (fixed_lease -> ip_addr.iabuf,
4169  cip.iabuf, cip.len))) {
4170  if (ours)
4171  *ours = 1;
4172  strcpy (dhcp_message, "requested address is incorrect");
4173 #if defined (DEBUG_FIND_LEASE)
4174  log_info ("Client's fixed-address %s doesn't match %s%s",
4175  piaddr (fixed_lease -> ip_addr), "request ",
4176  print_dotted_quads (cip.len, cip.iabuf));
4177 #endif
4178  goto out;
4179  }
4180 
4181  /*
4182  * If we found leases matching the client identifier, loop through
4183  * the n_uid pointer looking for one that's actually valid. We
4184  * can't do this until we get here because we depend on
4185  * packet -> known, which may be set by either the uid host
4186  * lookup or the haddr host lookup.
4187  *
4188  * Note that the n_uid lease chain is sorted in order of
4189  * preference, so the first one is the best one.
4190  */
4191  while (uid_lease) {
4192 #if defined (DEBUG_FIND_LEASE)
4193  log_info ("trying next lease matching client id: %s",
4194  piaddr (uid_lease -> ip_addr));
4195 #endif
4196 
4197 #if defined (FAILOVER_PROTOCOL)
4198  /*
4199  * When we lookup a lease by uid, we know the client identifier
4200  * matches the lease's record. If it is active, or was last
4201  * active with the same client, we can trivially extend it.
4202  * If is not or was not active, we can allocate it to this
4203  * client if it matches the usual free/backup criteria (which
4204  * is contained in lease_mine_to_reallocate()).
4205  */
4206  if (uid_lease->binding_state != FTS_ACTIVE &&
4207  uid_lease->rewind_binding_state != FTS_ACTIVE &&
4208  !lease_mine_to_reallocate(uid_lease)) {
4209 #if defined (DEBUG_FIND_LEASE)
4210  log_info("not active or not mine to allocate: %s",
4211  piaddr(uid_lease->ip_addr));
4212 #endif
4213  goto n_uid;
4214  }
4215 #endif
4216 
4217  if (uid_lease -> subnet -> shared_network != share) {
4218 #if defined (DEBUG_FIND_LEASE)
4219  log_info ("wrong network segment: %s",
4220  piaddr (uid_lease -> ip_addr));
4221 #endif
4222  goto n_uid;
4223  }
4224 
4225  if ((uid_lease -> pool -> prohibit_list &&
4226  permitted (packet, uid_lease -> pool -> prohibit_list)) ||
4227  (uid_lease -> pool -> permit_list &&
4228  !permitted (packet, uid_lease -> pool -> permit_list))) {
4229 #if defined (DEBUG_FIND_LEASE)
4230  log_info ("not permitted: %s",
4231  piaddr (uid_lease -> ip_addr));
4232 #endif
4233  n_uid:
4234  if (uid_lease -> n_uid)
4235  lease_reference (&next,
4236  uid_lease -> n_uid, MDL);
4237  if (!packet -> raw -> ciaddr.s_addr)
4238  release_lease (uid_lease, packet);
4239  lease_dereference (&uid_lease, MDL);
4240  if (next) {
4241  lease_reference (&uid_lease, next, MDL);
4242  lease_dereference (&next, MDL);
4243  }
4244  continue;
4245  }
4246  break;
4247  }
4248 #if defined (DEBUG_FIND_LEASE)
4249  if (uid_lease)
4250  log_info ("Found lease for client id: %s.",
4251  piaddr (uid_lease -> ip_addr));
4252 #endif
4253 
4254  /* Find a lease whose hardware address matches, whose client
4255  * identifier matches (or equally doesn't have one), that's
4256  * permitted, and that's on the correct subnet.
4257  *
4258  * Note that the n_hw chain is sorted in order of preference, so
4259  * the first one found is the best one.
4260  */
4261  h.hlen = packet -> raw -> hlen + 1;
4262  h.hbuf [0] = packet -> raw -> htype;
4263  memcpy (&h.hbuf [1], packet -> raw -> chaddr, packet -> raw -> hlen);
4264  find_lease_by_hw_addr (&hw_lease, h.hbuf, h.hlen, MDL);
4265  while (hw_lease) {
4266 #if defined (DEBUG_FIND_LEASE)
4267  log_info ("trying next lease matching hw addr: %s",
4268  piaddr (hw_lease -> ip_addr));
4269 #endif
4270 #if defined (FAILOVER_PROTOCOL)
4271  /*
4272  * When we lookup a lease by chaddr, we know the MAC address
4273  * matches the lease record (we will check if the lease has a
4274  * client-id the client does not next). If the lease is
4275  * currently active or was last active with this client, we can
4276  * trivially extend it. Otherwise, there are a set of rules
4277  * that govern if we can reallocate this lease to any client
4278  * ("lease_mine_to_reallocate()") including this one.
4279  */
4280  if (hw_lease->binding_state != FTS_ACTIVE &&
4281  hw_lease->rewind_binding_state != FTS_ACTIVE &&
4282  !lease_mine_to_reallocate(hw_lease)) {
4283 #if defined (DEBUG_FIND_LEASE)
4284  log_info("not active or not mine to allocate: %s",
4285  piaddr(hw_lease->ip_addr));
4286 #endif
4287  goto n_hw;
4288  }
4289 #endif
4290 
4291  /*
4292  * This conditional skips "potentially active" leases (leases
4293  * we think are expired may be extended by the peer, etc) that
4294  * may be assigned to a differently /client-identified/ client
4295  * with the same MAC address.
4296  */
4297  if (hw_lease -> binding_state != FTS_FREE &&
4298  hw_lease -> binding_state != FTS_BACKUP &&
4299  hw_lease -> uid &&
4300  (!have_client_identifier ||
4301  hw_lease -> uid_len != client_identifier.len ||
4302  memcmp (hw_lease -> uid, client_identifier.data,
4303  hw_lease -> uid_len))) {
4304 #if defined (DEBUG_FIND_LEASE)
4305  log_info ("wrong client identifier: %s",
4306  piaddr (hw_lease -> ip_addr));
4307 #endif
4308  goto n_hw;
4309  }
4310  if (hw_lease -> subnet -> shared_network != share) {
4311 #if defined (DEBUG_FIND_LEASE)
4312  log_info ("wrong network segment: %s",
4313  piaddr (hw_lease -> ip_addr));
4314 #endif
4315  goto n_hw;
4316  }
4317  if ((hw_lease -> pool -> prohibit_list &&
4318  permitted (packet, hw_lease -> pool -> prohibit_list)) ||
4319  (hw_lease -> pool -> permit_list &&
4320  !permitted (packet, hw_lease -> pool -> permit_list))) {
4321 #if defined (DEBUG_FIND_LEASE)
4322  log_info ("not permitted: %s",
4323  piaddr (hw_lease -> ip_addr));
4324 #endif
4325  if (!packet -> raw -> ciaddr.s_addr)
4326  release_lease (hw_lease, packet);
4327  n_hw:
4328  if (hw_lease -> n_hw)
4329  lease_reference (&next, hw_lease -> n_hw, MDL);
4330  lease_dereference (&hw_lease, MDL);
4331  if (next) {
4332  lease_reference (&hw_lease, next, MDL);
4333  lease_dereference (&next, MDL);
4334  }
4335  continue;
4336  }
4337  break;
4338  }
4339 #if defined (DEBUG_FIND_LEASE)
4340  if (hw_lease)
4341  log_info ("Found lease for hardware address: %s.",
4342  piaddr (hw_lease -> ip_addr));
4343 #endif
4344 
4345  /* Try to find a lease that's been allocated to the client's
4346  IP address. */
4347  if (ip_lease_in)
4348  lease_reference (&ip_lease, ip_lease_in, MDL);
4349  else if (cip.len)
4350  find_lease_by_ip_addr (&ip_lease, cip, MDL);
4351 
4352 #if defined (DEBUG_FIND_LEASE)
4353  if (ip_lease)
4354  log_info ("Found lease for requested address: %s.",
4355  piaddr (ip_lease -> ip_addr));
4356 #endif
4357 
4358  /* If ip_lease is valid at this point, set ours to one, so that
4359  even if we choose a different lease, we know that the address
4360  the client was requesting was ours, and thus we can NAK it. */
4361  if (ip_lease && ours)
4362  *ours = 1;
4363 
4364  /* If the requested IP address isn't on the network the packet
4365  came from, don't use it. Allow abandoned leases to be matched
4366  here - if the client is requesting it, there's a decent chance
4367  that it's because the lease database got trashed and a client
4368  that thought it had this lease answered an ARP or PING, causing the
4369  lease to be abandoned. If so, this request probably came from
4370  that client. */
4371  if (ip_lease && (ip_lease -> subnet -> shared_network != share)) {
4372  if (ours)
4373  *ours = 1;
4374 #if defined (DEBUG_FIND_LEASE)
4375  log_info ("...but it was on the wrong shared network.");
4376 #endif
4377  strcpy (dhcp_message, "requested address on bad subnet");
4378  lease_dereference (&ip_lease, MDL);
4379  }
4380 
4381  /*
4382  * If the requested address is in use (or potentially in use) by
4383  * a different client, it can't be granted.
4384  *
4385  * This first conditional only detects if the lease is currently
4386  * identified to a different client (client-id and/or chaddr
4387  * mismatch). In this case we may not want to give the client the
4388  * lease, if doing so may potentially be an addressing conflict.
4389  */
4390  if (ip_lease &&
4391  (ip_lease -> uid ?
4392  (!have_client_identifier ||
4393  ip_lease -> uid_len != client_identifier.len ||
4394  memcmp (ip_lease -> uid, client_identifier.data,
4395  ip_lease -> uid_len)) :
4396  (ip_lease -> hardware_addr.hbuf [0] != packet -> raw -> htype ||
4397  ip_lease -> hardware_addr.hlen != packet -> raw -> hlen + 1 ||
4398  memcmp (&ip_lease -> hardware_addr.hbuf [1],
4399  packet -> raw -> chaddr,
4400  (unsigned)(ip_lease -> hardware_addr.hlen - 1))))) {
4401  /*
4402  * A lease is unavailable for allocation to a new client if
4403  * it is not in the FREE or BACKUP state. There may be
4404  * leases that are in the expired state with a rewinding
4405  * state that is free or backup, but these will be processed
4406  * into the free or backup states by expiration processes, so
4407  * checking for them here is superfluous.
4408  */
4409  if (ip_lease -> binding_state != FTS_FREE &&
4410  ip_lease -> binding_state != FTS_BACKUP) {
4411 #if defined (DEBUG_FIND_LEASE)
4412  log_info ("rejecting lease for requested address.");
4413 #endif
4414  /* If we're rejecting it because the peer has
4415  it, don't set "ours", because we shouldn't NAK. */
4416  if (ours && ip_lease -> binding_state != FTS_ACTIVE)
4417  *ours = 0;
4418  lease_dereference (&ip_lease, MDL);
4419  }
4420  }
4421 
4422  /*
4423  * If we got an ip_lease and a uid_lease or hw_lease, and ip_lease
4424  * is/was not active, and is not ours to reallocate, forget about it.
4425  */
4426  if (ip_lease && (uid_lease || hw_lease) &&
4427  ip_lease->binding_state != FTS_ACTIVE &&
4428  ip_lease->rewind_binding_state != FTS_ACTIVE &&
4429 #if defined(FAILOVER_PROTOCOL)
4430  !lease_mine_to_reallocate(ip_lease) &&
4431 #endif
4432  packet->packet_type == DHCPDISCOVER) {
4433 #if defined (DEBUG_FIND_LEASE)
4434  log_info("ip lease not active or not ours to offer.");
4435 #endif
4436  lease_dereference(&ip_lease, MDL);
4437  }
4438 
4439  /* If for some reason the client has more than one lease
4440  on the subnet that matches its uid, pick the one that
4441  it asked for and (if we can) free the other. */
4442  if (ip_lease && ip_lease->binding_state == FTS_ACTIVE &&
4443  ip_lease->uid && ip_lease != uid_lease) {
4444  if (have_client_identifier &&
4445  (ip_lease -> uid_len == client_identifier.len) &&
4446  !memcmp (client_identifier.data,
4447  ip_lease -> uid, ip_lease -> uid_len)) {
4448  if (uid_lease) {
4449  if (uid_lease->binding_state == FTS_ACTIVE) {
4450  log_error ("client %s has duplicate%s on %s",
4451  (print_hw_addr_or_client_id(packet)),
4452  " leases",
4453  (ip_lease -> subnet ->
4454  shared_network -> name));
4455 
4456  /* If the client is REQUESTing the lease,
4457  it shouldn't still be using the old
4458  one, so we can free it for allocation. */
4459  if (uid_lease &&
4460  uid_lease->binding_state == FTS_ACTIVE &&
4461  !packet -> raw -> ciaddr.s_addr &&
4462  (share ==
4463  uid_lease -> subnet -> shared_network) &&
4464  packet -> packet_type == DHCPREQUEST)
4465  release_lease (uid_lease, packet);
4466  }
4467  lease_dereference (&uid_lease, MDL);
4468  lease_reference (&uid_lease, ip_lease, MDL);
4469  }
4470  }
4471 
4472  /* If we get to here and fixed_lease is not null, that means
4473  that there are both a dynamic lease and a fixed-address
4474  declaration for the same IP address. */
4475  if (packet -> packet_type == DHCPREQUEST && fixed_lease) {
4476  lease_dereference (&fixed_lease, MDL);
4477  db_conflict:
4478  log_error ("Dynamic and static leases present for %s.",
4479  piaddr (cip));
4480  log_error ("Remove host declaration %s or remove %s",
4481  (fixed_lease && fixed_lease -> host
4482  ? (fixed_lease -> host -> name
4483  ? fixed_lease -> host -> name
4484  : piaddr (cip))
4485  : piaddr (cip)),
4486  piaddr (cip));
4487  log_error ("from the dynamic address pool for %s",
4488  ip_lease -> subnet -> shared_network -> name
4489  );
4490  if (fixed_lease)
4491  lease_dereference (&ip_lease, MDL);
4492  strcpy (dhcp_message,
4493  "database conflict - call for help!");
4494  }
4495 
4496  if (ip_lease && ip_lease != uid_lease) {
4497 #if defined (DEBUG_FIND_LEASE)
4498  log_info ("requested address not available.");
4499 #endif
4500  lease_dereference (&ip_lease, MDL);
4501  }
4502  }
4503 
4504  /* If we get to here with both fixed_lease and ip_lease not
4505  null, then we have a configuration file bug. */
4506  if (packet -> packet_type == DHCPREQUEST && fixed_lease && ip_lease)
4507  goto db_conflict;
4508 
4509  /* Toss extra pointers to the same lease... */
4510  if (hw_lease && hw_lease == uid_lease) {
4511 #if defined (DEBUG_FIND_LEASE)
4512  log_info ("hardware lease and uid lease are identical.");
4513 #endif
4514  lease_dereference (&hw_lease, MDL);
4515  }
4516  if (ip_lease && ip_lease == hw_lease) {
4517  lease_dereference (&hw_lease, MDL);
4518 #if defined (DEBUG_FIND_LEASE)
4519  log_info ("hardware lease and ip lease are identical.");
4520 #endif
4521  }
4522  if (ip_lease && ip_lease == uid_lease) {
4523  lease_dereference (&uid_lease, MDL);
4524 #if defined (DEBUG_FIND_LEASE)
4525  log_info ("uid lease and ip lease are identical.");
4526 #endif
4527  }
4528 
4529  /* Make sure the client is permitted to use the requested lease. */
4530  if (ip_lease &&
4531  ((ip_lease -> pool -> prohibit_list &&
4532  permitted (packet, ip_lease -> pool -> prohibit_list)) ||
4533  (ip_lease -> pool -> permit_list &&
4534  !permitted (packet, ip_lease -> pool -> permit_list)))) {
4535  if (!packet->raw->ciaddr.s_addr &&
4536  (ip_lease->binding_state == FTS_ACTIVE))
4537  release_lease (ip_lease, packet);
4538 
4539  lease_dereference (&ip_lease, MDL);
4540  }
4541 
4542  if (uid_lease &&
4543  ((uid_lease -> pool -> prohibit_list &&
4544  permitted (packet, uid_lease -> pool -> prohibit_list)) ||
4545  (uid_lease -> pool -> permit_list &&
4546  !permitted (packet, uid_lease -> pool -> permit_list)))) {
4547  if (!packet -> raw -> ciaddr.s_addr)
4548  release_lease (uid_lease, packet);
4549  lease_dereference (&uid_lease, MDL);
4550  }
4551 
4552  if (hw_lease &&
4553  ((hw_lease -> pool -> prohibit_list &&
4554  permitted (packet, hw_lease -> pool -> prohibit_list)) ||
4555  (hw_lease -> pool -> permit_list &&
4556  !permitted (packet, hw_lease -> pool -> permit_list)))) {
4557  if (!packet -> raw -> ciaddr.s_addr)
4558  release_lease (hw_lease, packet);
4559  lease_dereference (&hw_lease, MDL);
4560  }
4561 
4562  /* If we've already eliminated the lease, it wasn't there to
4563  begin with. If we have come up with a matching lease,
4564  set the message to bad network in case we have to throw it out. */
4565  if (!ip_lease) {
4566  strcpy (dhcp_message, "requested address not available");
4567  }
4568 
4569  /* If this is a DHCPREQUEST, make sure the lease we're going to return
4570  matches the requested IP address. If it doesn't, don't return a
4571  lease at all. */
4572  if (packet -> packet_type == DHCPREQUEST &&
4573  !ip_lease && !fixed_lease) {
4574 #if defined (DEBUG_FIND_LEASE)
4575  log_info ("no applicable lease found for DHCPREQUEST.");
4576 #endif
4577  goto out;
4578  }
4579 
4580  /* At this point, if fixed_lease is nonzero, we can assign it to
4581  this client. */
4582  if (fixed_lease) {
4583  lease_reference (&lease, fixed_lease, MDL);
4584  lease_dereference (&fixed_lease, MDL);
4585 #if defined (DEBUG_FIND_LEASE)
4586  log_info ("choosing fixed address.");
4587 #endif
4588  }
4589 
4590  /* If we got a lease that matched the ip address and don't have
4591  a better offer, use that; otherwise, release it. */
4592  if (ip_lease) {
4593  if (lease) {
4594  if (!packet -> raw -> ciaddr.s_addr)
4595  release_lease (ip_lease, packet);
4596 #if defined (DEBUG_FIND_LEASE)
4597  log_info ("not choosing requested address (!).");
4598 #endif
4599  } else {
4600 #if defined (DEBUG_FIND_LEASE)
4601  log_info ("choosing lease on requested address.");
4602 #endif
4603  lease_reference (&lease, ip_lease, MDL);
4604  if (lease -> host)
4605  host_dereference (&lease -> host, MDL);
4606  }
4607  lease_dereference (&ip_lease, MDL);
4608  }
4609 
4610  /* If we got a lease that matched the client identifier, we may want
4611  to use it, but if we already have a lease we like, we must free
4612  the lease that matched the client identifier. */
4613  if (uid_lease) {
4614  if (lease) {
4615  log_error("uid lease %s for client %s is duplicate "
4616  "on %s",
4617  piaddr(uid_lease->ip_addr),
4619  uid_lease->subnet->shared_network->name);
4620 
4621  if (!packet -> raw -> ciaddr.s_addr &&
4622  packet -> packet_type == DHCPREQUEST &&
4623  uid_lease -> binding_state == FTS_ACTIVE)
4624  release_lease(uid_lease, packet);
4625 #if defined (DEBUG_FIND_LEASE)
4626  log_info ("not choosing uid lease.");
4627 #endif
4628  } else {
4629  lease_reference (&lease, uid_lease, MDL);
4630  if (lease -> host)
4631  host_dereference (&lease -> host, MDL);
4632 #if defined (DEBUG_FIND_LEASE)
4633  log_info ("choosing uid lease.");
4634 #endif
4635  }
4636  lease_dereference (&uid_lease, MDL);
4637  }
4638 
4639  /* The lease that matched the hardware address is treated likewise. */
4640  if (hw_lease) {
4641  if (lease) {
4642 #if defined (DEBUG_FIND_LEASE)
4643  log_info ("not choosing hardware lease.");
4644 #endif
4645  } else {
4646  /* We're a little lax here - if the client didn't
4647  send a client identifier and it's a bootp client,
4648  but the lease has a client identifier, we still
4649  let the client have a lease. */
4650  if (!hw_lease -> uid_len ||
4651  (have_client_identifier
4652  ? (hw_lease -> uid_len ==
4653  client_identifier.len &&
4654  !memcmp (hw_lease -> uid,
4655  client_identifier.data,
4656  client_identifier.len))
4657  : packet -> packet_type == 0)) {
4658  lease_reference (&lease, hw_lease, MDL);
4659  if (lease -> host)
4660  host_dereference (&lease -> host, MDL);
4661 #if defined (DEBUG_FIND_LEASE)
4662  log_info ("choosing hardware lease.");
4663 #endif
4664  } else {
4665 #if defined (DEBUG_FIND_LEASE)
4666  log_info ("not choosing hardware lease: %s.",
4667  "uid mismatch");
4668 #endif
4669  }
4670  }
4671  lease_dereference (&hw_lease, MDL);
4672  }
4673 
4674  /*
4675  * If we found a host_decl but no matching address, try to
4676  * find a host_decl that has no address, and if there is one,
4677  * hang it off the lease so that we can use the supplied
4678  * options.
4679  */
4680  if (lease && host && !lease->host) {
4681  struct host_decl *p = NULL;
4682  struct host_decl *n = NULL;
4683 
4684  host_reference(&p, host, MDL);
4685  while (p != NULL) {
4686  if (!p->fixed_addr) {
4687  /*
4688  * If the lease is currently active, then it
4689  * must be allocated to the present client.
4690  * We store a reference to the host record on
4691  * the lease to save a lookup later (in
4692  * ack_lease()). We mustn't refer to the host
4693  * record on non-active leases because the
4694  * client may be denied later.
4695  *
4696  * XXX: Not having this reference (such as in
4697  * DHCPDISCOVER/INIT) means ack_lease will have
4698  * to perform this lookup a second time. This
4699  * hopefully isn't a problem as DHCPREQUEST is
4700  * more common than DHCPDISCOVER.
4701  */
4702  if (lease->binding_state == FTS_ACTIVE)
4703  host_reference(&lease->host, p, MDL);
4704 
4705  host_dereference(&p, MDL);
4706  break;
4707  }
4708  if (p->n_ipaddr != NULL)
4709  host_reference(&n, p->n_ipaddr, MDL);
4710  host_dereference(&p, MDL);
4711  if (n != NULL) {
4712  host_reference(&p, n, MDL);
4713  host_dereference(&n, MDL);
4714  }
4715  }
4716  }
4717 
4718  /* If we find an abandoned lease, but it's the one the client
4719  requested, we assume that previous bugginess on the part
4720  of the client, or a server database loss, caused the lease to
4721  be abandoned, so we reclaim it and let the client have it. */
4722  if (lease &&
4723  (lease -> binding_state == FTS_ABANDONED) &&
4724  lease == ip_lease &&
4725  packet -> packet_type == DHCPREQUEST) {
4726  log_error ("Reclaiming REQUESTed abandoned IP address %s.",
4727  piaddr (lease -> ip_addr));
4728  } else if (lease && (lease -> binding_state == FTS_ABANDONED)) {
4729  /* Otherwise, if it's not the one the client requested, we do not
4730  return it - instead, we claim it's ours, causing a DHCPNAK to be
4731  sent if this lookup is for a DHCPREQUEST, and force the client
4732  to go back through the allocation process. */
4733  if (ours)
4734  *ours = 1;
4735  lease_dereference (&lease, MDL);
4736  }
4737 
4738  out:
4739  if (have_client_identifier)
4740  data_string_forget (&client_identifier, MDL);
4741 
4742  if (fixed_lease)
4743  lease_dereference (&fixed_lease, MDL);
4744  if (hw_lease)
4745  lease_dereference (&hw_lease, MDL);
4746  if (uid_lease)
4747  lease_dereference (&uid_lease, MDL);
4748  if (ip_lease)
4749  lease_dereference (&ip_lease, MDL);
4750  if (host)
4751  host_dereference (&host, MDL);
4752 
4753  if (lease) {
4754 #if defined (DEBUG_FIND_LEASE)
4755  log_info ("Returning lease: %s.",
4756  piaddr (lease -> ip_addr));
4757 #endif
4758  lease_reference (lp, lease, file, line);
4759  lease_dereference (&lease, MDL);
4760  return 1;
4761  }
4762 #if defined (DEBUG_FIND_LEASE)
4763  log_info ("Not returning a lease.");
4764 #endif
4765  return 0;
4766 }
4767 
4768 /* Search the provided host_decl structure list for an address that's on
4769  the specified shared network. If one is found, mock up and return a
4770  lease structure for it; otherwise return the null pointer. */
4771 
4772 int mockup_lease (struct lease **lp, struct packet *packet,
4773  struct shared_network *share, struct host_decl *hp)
4774 {
4775  struct lease *lease = (struct lease *)0;
4776  struct host_decl *rhp = (struct host_decl *)0;
4777 
4778  if (lease_allocate (&lease, MDL) != ISC_R_SUCCESS)
4779  return 0;
4780  if (host_reference (&rhp, hp, MDL) != ISC_R_SUCCESS) {
4781  lease_dereference (&lease, MDL);
4782  return 0;
4783  }
4784  if (!find_host_for_network (&lease -> subnet,
4785  &rhp, &lease -> ip_addr, share)) {
4786  lease_dereference (&lease, MDL);
4787  host_dereference (&rhp, MDL);
4788  return 0;
4789  }
4790  host_reference (&lease -> host, rhp, MDL);
4791  if (rhp -> client_identifier.len > sizeof lease -> uid_buf)
4792  lease -> uid = dmalloc (rhp -> client_identifier.len, MDL);
4793  else
4794  lease -> uid = lease -> uid_buf;
4795  if (!lease -> uid) {
4796  lease_dereference (&lease, MDL);
4797  host_dereference (&rhp, MDL);
4798  return 0;
4799  }
4800  memcpy (lease -> uid, rhp -> client_identifier.data,
4801  rhp -> client_identifier.len);
4802  lease -> uid_len = rhp -> client_identifier.len;
4803  lease -> hardware_addr = rhp -> interface;
4804  lease -> starts = lease -> cltt = lease -> ends = MIN_TIME;
4805  lease -> flags = STATIC_LEASE;
4806  lease -> binding_state = FTS_FREE;
4807 
4808  lease_reference (lp, lease, MDL);
4809 
4810  lease_dereference (&lease, MDL);
4811  host_dereference (&rhp, MDL);
4812  return 1;
4813 }
4814 
4815 /* Look through all the pools in a list starting with the specified pool
4816  for a free lease. We try to find a virgin lease if we can. If we
4817  don't find a virgin lease, we try to find a non-virgin lease that's
4818  free. If we can't find one of those, we try to reclaim an abandoned
4819  lease. If all of these possibilities fail to pan out, we don't return
4820  a lease at all. */
4821 
4822 int allocate_lease (struct lease **lp, struct packet *packet,
4823  struct pool *pool, int *peer_has_leases)
4824 {
4825  struct lease *lease = NULL;
4826  struct lease *candl = NULL;
4827 
4828  for (; pool ; pool = pool -> next) {
4829  if ((pool -> prohibit_list &&
4830  permitted (packet, pool -> prohibit_list)) ||
4831  (pool -> permit_list &&
4832  !permitted (packet, pool -> permit_list)))
4833  continue;
4834 
4835 #if defined (FAILOVER_PROTOCOL)
4836  /* Peer_has_leases just says that we found at least one
4837  free lease. If no free lease is returned, the caller
4838  can deduce that this means the peer is hogging all the
4839  free leases, so we can print a better error message. */
4840  /* XXX Do we need code here to ignore PEER_IS_OWNER and
4841  * XXX just check tstp if we're in, e.g., PARTNER_DOWN?
4842  * XXX Where do we deal with CONFLICT_DETECTED, et al? */
4843  /* XXX This should be handled by the lease binding "state
4844  * XXX machine" - that is, when we get here, if a lease
4845  * XXX could be allocated, it will have the correct
4846  * XXX binding state so that the following code will
4847  * XXX result in its being allocated. */
4848  /* Skip to the most expired lease in the pool that is not
4849  * owned by a failover peer. */
4850  if (pool->failover_peer != NULL) {
4851  struct lease *peerl = NULL;
4852  if (pool->failover_peer->i_am == primary) {
4853  candl = LEASE_GET_FIRST(pool->free);
4854 
4855  /*
4856  * In normal operation, we never want to touch
4857  * the peer's leases. In partner-down
4858  * operation, we need to be able to pick up
4859  * the peer's leases after STOS+MCLT.
4860  */
4861  peerl = LEASE_GET_FIRST(pool->backup);
4862  if (peerl != NULL) {
4863  if (((candl == NULL) ||
4864  (candl->ends > peerl->ends)) &&
4865  lease_mine_to_reallocate(peerl)) {
4866  candl = peerl;
4867  } else {
4868  *peer_has_leases = 1;
4869  }
4870  }
4871  } else {
4872  candl = LEASE_GET_FIRST(pool->backup);
4873 
4874  peerl = LEASE_GET_FIRST(pool->free);
4875  if (peerl != NULL) {
4876  if (((candl == NULL) ||
4877  (candl->ends > peerl->ends)) &&
4878  lease_mine_to_reallocate(peerl)) {
4879  candl = peerl;
4880  } else {
4881  *peer_has_leases = 1;
4882  }
4883  }
4884  }
4885 
4886  /* Try abandoned leases as a last resort. */
4887  peerl = LEASE_GET_FIRST(pool->abandoned);
4888  if ((candl == NULL) && (peerl != NULL) &&
4889  lease_mine_to_reallocate(peerl))
4890  candl = peerl;
4891  } else
4892 #endif
4893  {
4894  if (LEASE_NOT_EMPTY(pool->free))
4895  candl = LEASE_GET_FIRST(pool->free);
4896  else
4897  candl = LEASE_GET_FIRST(pool->abandoned);
4898  }
4899 
4900  /*
4901  * XXX: This may not match with documented expectation.
4902  * It's expected that when we OFFER a lease, we set its
4903  * ends time forward 2 minutes so that it gets sorted to
4904  * the end of its free list (avoiding a similar allocation
4905  * to another client). It is not expected that we issue a
4906  * "no free leases" error when the last lease has been
4907  * offered, but it's not exactly broken either.
4908  */
4909  if (!candl || (candl -> ends > cur_time))
4910  continue;
4911 
4912  if (!lease) {
4913  lease = candl;
4914  continue;
4915  }
4916 
4917  /*
4918  * There are tiers of lease state preference, listed here in
4919  * reverse order (least to most preferential):
4920  *
4921  * ABANDONED
4922  * FREE/BACKUP
4923  *
4924  * If the selected lease and candidate are both of the same
4925  * state, select the oldest (longest ago) expiration time
4926  * between the two. If the candidate lease is of a higher
4927  * preferred grade over the selected lease, use it.
4928  */
4929  if ((lease -> binding_state == FTS_ABANDONED) &&
4930  ((candl -> binding_state != FTS_ABANDONED) ||
4931  (candl -> ends < lease -> ends))) {
4932  lease = candl;
4933  continue;
4934  } else if (candl -> binding_state == FTS_ABANDONED)
4935  continue;
4936 
4937  if ((lease -> uid_len || lease -> hardware_addr.hlen) &&
4938  ((!candl -> uid_len && !candl -> hardware_addr.hlen) ||
4939  (candl -> ends < lease -> ends))) {
4940  lease = candl;
4941  continue;
4942  } else if (candl -> uid_len || candl -> hardware_addr.hlen)
4943  continue;
4944 
4945  if (candl -> ends < lease -> ends)
4946  lease = candl;
4947  }
4948 
4949  if (lease != NULL) {
4950  if (lease->binding_state == FTS_ABANDONED)
4951  log_error("Reclaiming abandoned lease %s.",
4952  piaddr(lease->ip_addr));
4953 
4954  /*
4955  * XXX: For reliability, we go ahead and remove the host
4956  * record and try to move on. For correctness, if there
4957  * are any other stale host vectors, we want to find them.
4958  */
4959  if (lease->host != NULL) {
4960  log_debug("soft impossible condition (%s:%d): stale "
4961  "host \"%s\" found on lease %s", MDL,
4962  lease->host->name,
4963  piaddr(lease->ip_addr));
4964  host_dereference(&lease->host, MDL);
4965  }
4966 
4967  lease_reference (lp, lease, MDL);
4968  return 1;
4969  }
4970 
4971  return 0;
4972 }
4973 
4974 /* Determine whether or not a permit exists on a particular permit list
4975  that matches the specified packet, returning nonzero if so, zero if
4976  not. */
4977 
4978 int permitted (packet, permit_list)
4979  struct packet *packet;
4980  struct permit *permit_list;
4981 {
4982  struct permit *p;
4983  int i;
4984 
4985  for (p = permit_list; p; p = p -> next) {
4986  switch (p -> type) {
4988  if (!packet -> known)
4989  return 1;
4990  break;
4991 
4992  case permit_known_clients:
4993  if (packet -> known)
4994  return 1;
4995  break;
4996 
4998  if (packet -> authenticated)
4999  return 1;
5000  break;
5001 
5003  if (!packet -> authenticated)
5004  return 1;
5005  break;
5006 
5007  case permit_all_clients:
5008  return 1;
5009 
5011  if (!packet -> options_valid ||
5012  !packet -> packet_type)
5013  return 1;
5014  break;
5015 
5016  case permit_class:
5017  for (i = 0; i < packet -> class_count; i++) {
5018  if (p -> class == packet -> classes [i])
5019  return 1;
5020  if (packet -> classes [i] &&
5021  packet -> classes [i] -> superclass &&
5022  (packet -> classes [i] -> superclass ==
5023  p -> class))
5024  return 1;
5025  }
5026  break;
5027 
5028  case permit_after:
5029  if (cur_time > p->after)
5030  return 1;
5031  break;
5032  }
5033  }
5034  return 0;
5035 }
5036 
5037 #if defined(DHCPv6) && defined(DHCP4o6)
5038 static int locate_network6 (packet)
5039  struct packet *packet;
5040 {
5041  const struct packet *chk_packet;
5042  const struct in6_addr *link_addr, *first_link_addr;
5043  struct iaddr ia;
5044  struct data_string data;
5045  struct subnet *subnet = NULL;
5046  struct option_cache *oc;
5047 
5048  /* from locate_network() */
5049 
5050  /* See if there's a Relay Agent Link Selection Option, or a
5051  * Subnet Selection Option. The Link-Select and Subnet-Select
5052  * are formatted and used precisely the same, but we must prefer
5053  * the link-select over the subnet-select.
5054  * BTW in DHCPv4 over DHCPv6 no cross version relay was specified
5055  * so it is unlikely to see a link-select.
5056  */
5057  if ((oc = lookup_option(&agent_universe, packet->options,
5058  RAI_LINK_SELECT)) == NULL)
5059  oc = lookup_option(&dhcp_universe, packet->options,
5061 
5062  /* If there's an option indicating link connection or subnet
5063  * selection, and it's valid, use it to figure out the subnet.
5064  * If it's not valid, fail.
5065  */
5066  if (oc) {
5067  memset(&data, 0, sizeof data);
5068  if (!evaluate_option_cache(&data, packet, NULL, NULL,
5069  packet->options, NULL,
5070  &global_scope, oc, MDL)) {
5071  return (0);
5072  }
5073  if (data.len == 0) {
5074  return (0);
5075  }
5076  if (data.len != 4) {
5077  data_string_forget(&data, MDL);
5078  return (0);
5079  }
5080  ia.len = 4;
5081  memcpy(ia.iabuf, data.data, 4);
5082  data_string_forget(&data, MDL);
5083 
5084  if (find_subnet(&subnet, ia, MDL)) {
5085  shared_network_reference(&packet->shared_network,
5086  subnet->shared_network, MDL);
5087  subnet_dereference(&subnet, MDL);
5088  return (1);
5089  }
5090  return (0);
5091  }
5092 
5093  /* See if there is a giaddr (still unlikely), if there is one
5094  * use it to figure out the subnet. If it's not valid, fail.
5095  */
5096  if (packet->raw->giaddr.s_addr) {
5097  ia.len = 4;
5098  memcpy(ia.iabuf, &packet->raw->giaddr, 4);
5099 
5100  if (find_subnet(&subnet, ia, MDL)) {
5101  shared_network_reference(&packet->shared_network,
5102  subnet->shared_network, MDL);
5103  subnet_dereference(&subnet, MDL);
5104  return (1);
5105  }
5106  return (0);
5107  }
5108 
5109  /* from shared_network_from_packet6() */
5110 
5111  /* First, find the link address where the packet from the client
5112  * first appeared (if this packet was relayed).
5113  */
5114  first_link_addr = NULL;
5115  chk_packet = packet->dhcpv6_container_packet;
5116  while (chk_packet != NULL) {
5117  link_addr = &chk_packet->dhcpv6_link_address;
5118  if (!IN6_IS_ADDR_UNSPECIFIED(link_addr) &&
5119  !IN6_IS_ADDR_LINKLOCAL(link_addr)) {
5120  first_link_addr = link_addr;
5121  break;
5122  }
5123  chk_packet = chk_packet->dhcpv6_container_packet;
5124  }
5125 
5126  /* If there is a relayed link address, find the subnet associated
5127  * with that, and use that to get the appropriate shared_network.
5128  */
5129  if (first_link_addr != NULL) {
5130  ia.len = sizeof(*first_link_addr);
5131  memcpy(ia.iabuf, first_link_addr, sizeof(*first_link_addr));
5132  if (find_subnet (&subnet, ia, MDL)) {
5133  shared_network_reference(&packet->shared_network,
5134  subnet->shared_network, MDL);
5135  subnet_dereference(&subnet, MDL);
5136  return (1);
5137  }
5138  return (0);
5139  }
5140 
5141  /* If there is no link address, we will use the interface
5142  * that this packet came in on to pick the shared_network.
5143  */
5144  if (packet->interface != NULL) {
5145  if (packet->interface->shared_network == NULL)
5146  return (0);
5147  shared_network_reference(&packet->shared_network,
5148  packet->interface->shared_network,
5149  MDL);
5150  return (1);
5151  }
5152 
5153  /* We shouldn't be able to get here but if there is no link
5154  * address and no interface we don't know where to get the
5155  * shared_network from, log an error and return an error.
5156  */
5157  log_error("No interface and no link address "
5158  "can't determine DHCP4o6 shared network");
5159  return (0);
5160 }
5161 #endif
5162 
5163 int locate_network (packet)
5164  struct packet *packet;
5165 {
5166  struct iaddr ia;
5167  struct data_string data;
5168  struct subnet *subnet = (struct subnet *)0;
5169  struct option_cache *oc;
5170  int norelay = 0;
5171 
5172 #if defined(DHCPv6) && defined(DHCP4o6)
5173  if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
5174  return (locate_network6 (packet));
5175  }
5176 #endif
5177 
5178  /* See if there's a Relay Agent Link Selection Option, or a
5179  * Subnet Selection Option. The Link-Select and Subnet-Select
5180  * are formatted and used precisely the same, but we must prefer
5181  * the link-select over the subnet-select.
5182  */
5183  if ((oc = lookup_option(&agent_universe, packet->options,
5184  RAI_LINK_SELECT)) == NULL)
5185  oc = lookup_option(&dhcp_universe, packet->options,
5187 
5188  /* If there's no SSO and no giaddr, then use the shared_network
5189  from the interface, if there is one. If not, fail. */
5190  if (!oc && !packet -> raw -> giaddr.s_addr) {
5191  if (packet -> interface -> shared_network) {
5192  struct in_addr any_addr;
5193  any_addr.s_addr = INADDR_ANY;
5194 
5195  if (!packet -> packet_type && memcmp(&packet -> raw -> ciaddr, &any_addr, 4)) {
5196  struct iaddr cip;
5197  memcpy(cip.iabuf, &packet -> raw -> ciaddr, 4);
5198  cip.len = 4;
5199  if (!find_grouped_subnet(&subnet, packet->interface->shared_network, cip, MDL))
5200  norelay = 2;
5201  }
5202 
5203  if (!norelay) {
5204  shared_network_reference(&packet -> shared_network, packet -> interface -> shared_network, MDL);
5205  return 1;
5206  }
5207  } else {
5208  return 0;
5209  }
5210  }
5211 
5212  /* If there's an option indicating link connection, and it's valid,
5213  * use it to figure out the subnet. If it's not valid, fail.
5214  */
5215  if (oc) {
5216  memset (&data, 0, sizeof data);
5217  if (!evaluate_option_cache (&data, packet, (struct lease *)0,
5218  (struct client_state *)0,
5219  packet -> options,
5220  (struct option_state *)0,
5221  &global_scope, oc, MDL)) {
5222  return 0;
5223  }
5224  if (data.len == 0) {
5225  return 0;
5226  }
5227  if (data.len != 4) {
5228  data_string_forget (&data, MDL);
5229  return 0;
5230  }
5231  ia.len = 4;
5232  memcpy (ia.iabuf, data.data, 4);
5233  data_string_forget (&data, MDL);
5234  } else {
5235  ia.len = 4;
5236  if (norelay)
5237  memcpy (ia.iabuf, &packet->raw->ciaddr, 4);
5238  else
5239  memcpy (ia.iabuf, &packet->raw->giaddr, 4);
5240  }
5241 
5242  /* If we know the subnet on which the IP address lives, use it. */
5243  if (find_subnet (&subnet, ia, MDL)) {
5244  shared_network_reference (&packet -> shared_network,
5245  subnet -> shared_network, MDL);
5246  subnet_dereference (&subnet, MDL);
5247  if (norelay)
5248  return norelay;
5249  else
5250  return 1;
5251  }
5252 
5253  /* Otherwise, fail. */
5254  return 0;
5255 }
5256 
5257 /*
5258  * Try to figure out the source address to send packets from.
5259  *
5260  * from is the address structure we use to return any address
5261  * we find.
5262  *
5263  * options is the option cache to search. This may include
5264  * options from the incoming packet and configuration information.
5265  *
5266  * out_options is the outgoing option cache. This cache
5267  * may be the same as options. If out_options isn't NULL
5268  * we may save the server address option into it. We do so
5269  * if out_options is different than options or if the option
5270  * wasn't in options and we needed to find the address elsewhere.
5271  *
5272  * packet is the state structure for the incoming packet
5273  *
5274  * When finding the address we first check to see if it is
5275  * in the options list. If it isn't we use the first address
5276  * from the interface.
5277  *
5278  * While this is slightly more complicated than I'd like it allows
5279  * us to use the same code in several different places. ack,
5280  * inform and lease query use it to find the address and fill
5281  * in the options if we get the address from the interface.
5282  * nack uses it to find the address and copy it to the outgoing
5283  * cache. dhcprequest uses it to find the address for comparison
5284  * and doesn't need to add it to an outgoing list.
5285  */
5286 
5287 void
5288 get_server_source_address(struct in_addr *from,
5289  struct option_state *options,
5290  struct option_state *out_options,
5291  struct packet *packet) {
5292  unsigned option_num;
5293  struct option_cache *oc = NULL;
5294  struct data_string d;
5295  struct in_addr *a = NULL;
5296  isc_boolean_t found = ISC_FALSE;
5297  int allocate = 0;
5298 
5299  memset(&d, 0, sizeof(d));
5300  memset(from, 0, sizeof(*from));
5301 
5302  option_num = DHO_DHCP_SERVER_IDENTIFIER;
5303  oc = lookup_option(&dhcp_universe, options, option_num);
5304  if (oc != NULL) {
5305  if (evaluate_option_cache(&d, packet, NULL, NULL,
5306  packet->options, options,
5307  &global_scope, oc, MDL)) {
5308  if (d.len == sizeof(*from)) {
5309  found = ISC_TRUE;
5310  memcpy(from, d.data, sizeof(*from));
5311 
5312  /*
5313  * Arrange to save a copy of the data
5314  * to the outgoing list.
5315  */
5316  if ((out_options != NULL) &&
5317  (options != out_options)) {
5318  a = from;
5319  allocate = 1;
5320  }
5321  }
5322  data_string_forget(&d, MDL);
5323  }
5324  oc = NULL;
5325  }
5326 
5327  if ((found == ISC_FALSE) &&
5328  (packet->interface->address_count > 0)) {
5329  *from = packet->interface->addresses[0];
5330 
5331  if (out_options != NULL) {
5332  a = &packet->interface->addresses[0];
5333  }
5334  }
5335 
5336  if ((a != NULL) &&
5337  (option_cache_allocate(&oc, MDL))) {
5338  if (make_const_data(&oc->expression,
5339  (unsigned char *)a, sizeof(*a),
5340  0, allocate, MDL)) {
5341  option_code_hash_lookup(&oc->option,
5343  &option_num, 0, MDL);
5344  save_option(&dhcp_universe, out_options, oc);
5345  }
5347  }
5348 
5349  return;
5350 }
5351 
5373 void
5374 eval_network_statements(struct option_state **network_options,
5375  struct packet *packet,
5376  struct group *network_group) {
5377 
5378  if (*network_options == NULL) {
5379  option_state_allocate (network_options, MDL);
5380  }
5381 
5382  /* Use the packet's shared_network if it has one. If not use
5383  * network_group and if it is null then use global scope. */
5384  if (packet->shared_network != NULL) {
5385  /*
5386  * If we have a subnet and group start with that else start
5387  * with the shared network group. The first will recurse and
5388  * include the second.
5389  */
5390  if ((packet->shared_network->subnets != NULL) &&
5391  (packet->shared_network->subnets->group != NULL)) {
5392  execute_statements_in_scope(NULL, packet, NULL, NULL,
5393  packet->options, *network_options,
5394  &global_scope,
5395  packet->shared_network->subnets->group,
5396  NULL, NULL);
5397  } else {
5398  execute_statements_in_scope(NULL, packet, NULL, NULL,
5399  packet->options, *network_options,
5400  &global_scope,
5401  packet->shared_network->group,
5402  NULL, NULL);
5403  }
5404 
5405  /* do the pool if there is one */
5406  if (packet->shared_network->pools != NULL) {
5407  execute_statements_in_scope(NULL, packet, NULL, NULL,
5408  packet->options, *network_options,
5409  &global_scope,
5410  packet->shared_network->pools->group,
5411  packet->shared_network->group,
5412  NULL);
5413  }
5414  } else if (network_group != NULL) {
5415  execute_statements_in_scope(NULL, packet, NULL, NULL,
5416  packet->options, *network_options,
5417  &global_scope, network_group,
5418  NULL, NULL);
5419  } else {
5420  execute_statements_in_scope(NULL, packet, NULL, NULL,
5421  packet->options, *network_options,
5423  NULL, NULL);
5424  }
5425 }
5426 
5427 /*
5428  * Look for the lowest numbered site code number and
5429  * apply a log warning if it is less than 224. Do not
5430  * permit site codes less than 128 (old code never did).
5431  *
5432  * Note that we could search option codes 224 down to 128
5433  * on the hash table, but the table is (probably) smaller
5434  * than that if it was declared as a standalone table with
5435  * defaults. So we traverse the option code hash.
5436  */
5437 static int
5438 find_min_site_code(struct universe *u)
5439 {
5440  if (u->site_code_min)
5441  return u->site_code_min;
5442 
5443  /*
5444  * Note that site_code_min has to be global as we can't pass an
5445  * argument through hash_foreach(). The value 224 is taken from
5446  * RFC 3942.
5447  */
5448  site_code_min = 224;
5449  option_code_hash_foreach(u->code_hash, lowest_site_code);
5450 
5451  if (site_code_min < 224) {
5452  log_error("WARNING: site-local option codes less than 224 have "
5453  "been deprecated by RFC3942. You have options "
5454  "listed in site local space %s that number as low as "
5455  "%d. Please investigate if these should be declared "
5456  "as regular options rather than site-local options, "
5457  "or migrated up past 224.",
5458  u->name, site_code_min);
5459  }
5460 
5461  /*
5462  * don't even bother logging, this is just silly, and never worked
5463  * on any old version of software.
5464  */
5465  if (site_code_min < 128)
5466  site_code_min = 128;
5467 
5468  /*
5469  * Cache the determined minimum site code on the universe structure.
5470  * Note that due to the < 128 check above, a value of zero is
5471  * impossible.
5472  */
5473  u->site_code_min = site_code_min;
5474 
5475  return site_code_min;
5476 }
5477 
5478 static isc_result_t
5479 lowest_site_code(const void *key, unsigned len, void *object)
5480 {
5481  struct option *option = object;
5482 
5483  if (option->code < site_code_min)
5484  site_code_min = option->code;
5485 
5486  return ISC_R_SUCCESS;
5487 }
5488 
5489 static void
5490 maybe_return_agent_options(struct packet *packet, struct option_state *options)
5491 {
5492  /* If there were agent options in the incoming packet, return
5493  * them. Do not return the agent options if they were stashed
5494  * on the lease. We do not check giaddr to detect the presence of
5495  * a relay, as this excludes "l2" relay agents which have no giaddr
5496  * to set.
5497  *
5498  * XXX: If the user configures options for the relay agent information
5499  * (state->options->universes[agent_universe.index] is not NULL),
5500  * we're still required to duplicate other values provided by the
5501  * relay agent. So we need to merge the old values not configured
5502  * by the user into the new state, not just give up.
5503  */
5504  if (!packet->agent_options_stashed &&
5505  (packet->options != NULL) &&
5507  packet->options->universes[agent_universe.index] != NULL &&
5508  (options->universe_count <= agent_universe.index ||
5509  options->universes[agent_universe.index] == NULL)) {
5511  ((struct option_chain_head **)
5512  &(options->universes[agent_universe.index]),
5513  (struct option_chain_head *)
5515 
5516  if (options->universe_count <= agent_universe.index)
5517  options->universe_count = agent_universe.index + 1;
5518  }
5519 }
5520 
5533 void use_host_decl_name(struct packet* packet,
5534  struct lease *lease,
5535  struct option_state *options) {
5536  unsigned int ocode = SV_USE_HOST_DECL_NAMES;
5537  if ((lease->host && lease->host->name) &&
5538  !lookup_option(&dhcp_universe, options, DHO_HOST_NAME) &&
5539  (evaluate_boolean_option_cache(NULL, packet, lease, NULL,
5540  packet->options, options,
5541  &lease->scope,
5543  options, ocode),
5544  MDL))) {
5545  struct option_cache *oc = NULL;
5546  if (option_cache_allocate (&oc, MDL)) {
5547  if (make_const_data(&oc -> expression,
5548  ((unsigned char*)lease->host->name),
5549  strlen(lease->host->name),
5550  1, 0, MDL)) {
5551  ocode = DHO_HOST_NAME;
5552  option_code_hash_lookup(&oc->option,
5554  &ocode, 0, MDL);
5555  save_option(&dhcp_universe, options, oc);
5556  }
5558  }
5559  }
5560 }
5561 
5592 int
5593 reuse_lease (struct packet* packet,
5594  struct lease* new_lease,
5595  struct lease* lease,
5596  struct lease_state *state,
5597  int offer) {
5598  int reusable = 0;
5599 
5600  /* To even consider reuse all of the following must be true:
5601  * 1 - reuse hasn't already disqualified
5602  * 2 - current lease is active
5603  * 3 - DNS info hasn't changed */
5604  if ((lease->cannot_reuse == 0) &&
5605  (lease->binding_state == FTS_ACTIVE) &&
5606  (new_lease->ddns_cb == NULL)) {
5607  int thresh = DEFAULT_CACHE_THRESHOLD;
5608  struct option_cache* oc = NULL;
5609  struct data_string d1;
5610 
5611  /* Look up threshold value */
5612  memset(&d1, 0, sizeof(struct data_string));
5613  if ((oc = lookup_option(&server_universe, state->options,
5614  SV_CACHE_THRESHOLD)) &&
5615  (evaluate_option_cache(&d1, packet, new_lease, NULL,
5616  packet->options, state->options,
5617  &new_lease->scope, oc, MDL))) {
5618  if (d1.len == 1 && (d1.data[0] < 100))
5619  thresh = d1.data[0];
5620 
5621  data_string_forget(&d1, MDL);
5622  }
5623 
5624  /* If threshold is enabled, check lease age */
5625  if (thresh > 0) {
5626  int limit = 0;
5627  int lease_length = 0;
5628  long lease_age = 0;
5629 
5630  /* Calculate limit in seconds */
5631  lease_length = lease->ends - lease->starts;
5632  if (lease_length <= (INT_MAX / thresh))
5633  limit = lease_length * thresh / 100;
5634  else
5635  limit = lease_length / 100 * thresh;
5636 
5637  /* Note new_lease->starts is really just cur_time */
5638  lease_age = new_lease->starts - lease->starts;
5639 
5640  /* Is the lease is young enough to reuse? */
5641  if (lease_age <= limit) {
5642  /* Restore expiry to its original value */
5643  state->offered_expiry = lease->ends;
5644 
5645  /* Restore bindings. This fixes 37368. */
5646  if (new_lease->scope != NULL) {
5647  if (lease->scope != NULL) {
5649  &lease->scope,
5650  MDL);
5651  }
5652 
5654  new_lease->scope, MDL);
5655  }
5656 
5657  /* We're cleared to reuse it */
5658  log_debug("reuse_lease: lease age %ld (secs)"
5659  " under %d%% threshold, reply with "
5660  "unaltered, existing lease for %s",
5661  lease_age, thresh, piaddr(lease->ip_addr));
5662 
5663  reusable = 1;
5664  }
5665  }
5666  }
5667 
5668  /* If we can't reuse it and this is an offer disqualify reuse for
5669  * ensuing REQUEST, otherwise clear the flag. */
5670  lease->cannot_reuse = (!reusable && offer == DHCPOFFER);
5671  return (reusable);
5672 }
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:3717
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:822
#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:1029
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:2663
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:1346
struct group * group
Definition: dhcpd.h:996
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:1370
#define DHCPINFORM
Definition: dhcp.h:179
void * dmalloc(unsigned, const char *, int)
Definition: alloc.c:56
struct lease_state * state
Definition: dhcpd.h:624
struct class * superclass
Definition: dhcpd.h:1069
int option_cache_dereference(struct option_cache **ptr, const char *file, int line)
Definition: options.c:2798
u_int16_t secs
Definition: dhcp.h:54
Definition: dhcpd.h:1039
#define DEFAULT_MIN_LEASE_TIME
Definition: dhcpd.h:834
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:2568
#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:818
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:1333
#define DHO_DHCP_LEASE_TIME
Definition: dhcp.h:143
struct group * group
Definition: dhcpd.h:1033
struct in_addr * addresses
Definition: dhcpd.h:1350
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:3825
#define DHCPACK
Definition: dhcp.h:176
struct option_cache * fixed_addr
Definition: dhcpd.h:950
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:4772
#define DHO_SUBNET_MASK
Definition: dhcp.h:93
void delete_option(struct universe *universe, struct option_state *options, int code)
Definition: options.c:2751
#define BOOTP_BROADCAST
Definition: dhcp.h:73
int log_error(const char *,...) __attribute__((__format__(__printf__
#define FTS_EXPIRED
Definition: dhcpd.h: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:1415
#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:1015
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:944
#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:1414
const char * binding_state_print(enum failover_state state)
Definition: failover.c:6472
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:1070
#define RAI_LINK_SELECT
Definition: dhcp.h:190
LEASE_STRUCT free
Definition: dhcpd.h:1002
int low_threshold
Definition: dhcpd.h:1018
#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:810
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:814
struct dhcp_packet * raw
Definition: dhcpd.h:406
int locate_network(struct packet *packet)
Definition: dhcp.c:5163
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:3742
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:999
struct lease * lease
Definition: dhcpd.h:1411
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:978
struct leasequeue * prev
Definition: dhcpd.h:1409
#define MIN_TIME
Definition: dhcpd.h:1592
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:993
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:838
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:1012
struct group * next
Definition: dhcpd.h:927
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:826
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:942
int permitted(struct packet *packet, struct permit *permit_list)
Definition: dhcp.c:4978
int find_lease_by_hw_addr(struct lease **, const unsigned char *, unsigned, const char *, int)
Definition: mdb.c:2035
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:5374
#define DEFAULT_DEFAULT_LEASE_TIME
Definition: dhcpd.h:830
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:933
struct in_addr yiaddr
Definition: dhcp.h:57
#define cur_time
Definition: dhcpd.h:2071
struct lease * n_hw
Definition: dhcpd.h:563
int free_leases
Definition: dhcpd.h:1008
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:5288
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:1007
struct host_decl * n_ipaddr
Definition: dhcpd.h:940
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:2348
#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:1073
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
unsigned short cannot_reuse
Definition: dhcpd.h:649
u_int32_t getULong(const unsigned char *)
struct shared_network * shared_network
Definition: dhcpd.h:1043
#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:4822
#define DHCPDISCOVER
Definition: dhcp.h:172
struct group * group
Definition: dhcpd.h:1049
#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:1011
Definition: dhcpd.h:965
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:926
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:806
#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:5533
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:4020
#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:2027
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:1353
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:1591
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:955
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:997
const char * file
Definition: dhcpd.h:3717
#define DHO_DHCP_CLIENT_IDENTIFIER
Definition: dhcp.h:153
char * name
Definition: dhcpd.h:1024
struct permit * permit_list
Definition: dhcpd.h:998
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:1410
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:1066
const unsigned char * data
Definition: tree.h:79
struct in_addr ciaddr
Definition: dhcp.h:56
int get_option_int(int *result, struct universe *universe, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct option_state *options, struct binding_scope **scope, unsigned code, const char *file, int line)
Definition: options.c:2203
#define DHO_DHCP_MESSAGE_TYPE
Definition: dhcp.h:145
int bind_ds_value(struct binding_scope **scope, const char *name, struct data_string *value)
Definition: tree.c: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:943
LEASE_STRUCT backup
Definition: dhcpd.h:1003
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:2020
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:1044
LEASE_STRUCT abandoned
Definition: dhcpd.h:1004
void classify_client(struct packet *)
Definition: class.c:63
struct group_object * object
Definition: dhcpd.h:930
struct pool * pools
Definition: dhcpd.h:1031
int lease_mine_to_reallocate(struct lease *)
#define DHO_HOST_NAME
Definition: dhcp.h:104
struct group * group
Definition: dhcpd.h:952
struct buffer * buffer
Definition: tree.h:78
struct pool * next
Definition: dhcpd.h:995
int logged
Definition: dhcpd.h:1017
char * client_hostname
Definition: dhcpd.h:570
#define DHO_DHCP_REQUESTED_ADDRESS
Definition: dhcp.h:142
int backup_leases
Definition: dhcpd.h:1009
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