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