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