]> git.ipfire.org Git - thirdparty/dhcp.git/blobdiff - server/bootp.c
Update RELNOTES
[thirdparty/dhcp.git] / server / bootp.c
index 6062a4366c28cdb01d15c72817074b11ec7b1aee..d81f4f3680d07a3887684ccacfdd129bf1837eb3 100644 (file)
@@ -3,56 +3,41 @@
    BOOTP Protocol support. */
 
 /*
- * Copyright (c) 1995-2000 Internet Software Consortium.
- * All rights reserved.
+ * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
+ * Copyright (c) 1995-2003 by Internet Software Consortium
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of The Internet Software Consortium nor the names
- *    of its contributors may be used to endorse or promote products derived
- *    from this software without specific prior written permission.
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+ * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  *
- * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
- * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ *   Internet Systems Consortium, Inc.
+ *   PO Box 360
+ *   Newmarket, NH 03857 USA
+ *   <info@isc.org>
+ *   https://www.isc.org/
  *
- * This software has been written for the Internet Software Consortium
- * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
- * To learn more about the Internet Software Consortium, see
- * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
- * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
- * ``http://www.nominum.com''.
  */
 
-#ifndef lint
-static char copyright[] =
-"$Id: bootp.c,v 1.62 2000/05/03 22:57:42 mellon Exp $ Copyright (c) 1995-2000 The Internet Software Consortium.  All rights reserved.\n";
-#endif /* not lint */
-
 #include "dhcpd.h"
+#include <errno.h>
+
+#if defined (TRACING)
+# define send_packet trace_packet_send
+#endif
 
 void bootp (packet)
        struct packet *packet;
 {
        int result;
-       struct host_decl *hp;
+       struct host_decl *hp = (struct host_decl *)0;
        struct host_decl *host = (struct host_decl *)0;
        struct packet outgoing;
        struct dhcp_packet raw;
@@ -60,9 +45,7 @@ void bootp (packet)
        struct in_addr from;
        struct hardware hto;
        struct option_state *options = (struct option_state *)0;
-       struct subnet *subnet;
-       struct lease *lease;
-       struct iaddr ip_address;
+       struct lease *lease = (struct lease *)0;
        unsigned i;
        struct data_string d1;
        struct option_cache *oc;
@@ -73,7 +56,10 @@ void bootp (packet)
        if (packet -> raw -> op != BOOTREQUEST)
                return;
 
-       sprintf (msgbuf, "BOOTREQUEST from %s via %s",
+       /* %Audit% This is log output. %2004.06.17,Safe%
+        * If we truncate we hope the user can get a hint from the log.
+        */
+       snprintf (msgbuf, sizeof msgbuf, "BOOTREQUEST from %s via %s",
                 print_hw_addr (packet -> raw -> htype,
                                packet -> raw -> hlen,
                                packet -> raw -> chaddr),
@@ -81,105 +67,134 @@ void bootp (packet)
                 ? inet_ntoa (packet -> raw -> giaddr)
                 : packet -> interface -> name);
 
-
-
        if (!locate_network (packet)) {
                log_info ("%s: network unknown", msgbuf);
                return;
        }
 
-       hp = find_hosts_by_haddr (packet -> raw -> htype,
-                                 packet -> raw -> chaddr,
-                                 packet -> raw -> hlen);
+       find_lease (&lease, packet, packet -> shared_network,
+                   0, 0, (struct lease *)0, MDL);
 
-       lease = find_lease (packet, packet -> shared_network, 0);
+       if (lease && lease->host)
+               host_reference(&hp, lease->host, MDL);
 
-       /* Find an IP address in the host_decl that matches the
-          specified network. */
-       if (hp)
-               subnet = find_host_for_network (&hp, &ip_address,
-                                               packet -> shared_network);
-       else
-               subnet = (struct subnet *)0;
-
-       if (!subnet) {
-               /* We didn't find an applicable host declaration.
-                  Just in case we may be able to dynamically assign
-                  an address, see if there's a host declaration
+       if (!lease || ((lease->flags & STATIC_LEASE) == 0)) {
+               struct host_decl *h;
+
+               /* We didn't find an applicable fixed-address host
+                  declaration.  Just in case we may be able to dynamically
+                  assign an address, see if there's a host declaration
                   that doesn't have an ip address associated with it. */
-               if (hp) {
-                       for (; hp; hp = hp -> n_ipaddr) {
-                               if (!hp -> fixed_addr) {
-                                       host = hp;
-                                       break;
-                               }
+
+               if (!hp)
+                       find_hosts_by_haddr(&hp, packet->raw->htype,
+                                           packet->raw->chaddr,
+                                           packet->raw->hlen, MDL);
+
+               for (h = hp; h; h = h -> n_ipaddr) {
+                       if (!h -> fixed_addr) {
+                               host_reference(&host, h, MDL);
+                               break;
                        }
                }
 
-               /* If a lease has already been assigned to this client,
-                  use it. */
-               if (lease) {
-                       ack_lease (packet, lease, 0, 0, msgbuf, 0);
-                       return;
+               if (hp)
+                       host_dereference(&hp, MDL);
+
+               if (host) {
+                       host_reference(&hp, host, MDL);
+                       host_dereference(&host, MDL);
                }
 
-               /* Otherwise, try to allocate one. */
-               lease = allocate_lease (packet,
-                                       packet -> shared_network -> pools, 0,
+               /* Allocate a lease if we have not yet found one. */
+               if (!lease)
+                       allocate_lease (&lease, packet,
+                                       packet -> shared_network -> pools,
                                        &peer_has_leases);
-               if (lease) {
-                       lease -> host = host;
-                       ack_lease (packet, lease, 0, 0, msgbuf, 0);
-                       return;
+
+               if (lease == NULL) {
+                       log_info("%s: BOOTP from dynamic client and no "
+                                "dynamic leases", msgbuf);
+                       goto out;
                }
-               log_info ("%s: no available leases", msgbuf);
-               return;
+
+#if defined(FAILOVER_PROTOCOL)
+               if ((lease->pool != NULL) &&
+                   (lease->pool->failover_peer != NULL)) {
+                       dhcp_failover_state_t *peer;
+
+                       peer = lease->pool->failover_peer;
+
+                       /* If we are in a failover state that bars us from
+                        * answering, do not do so.
+                        * If we are in a cooperative state, load balance
+                        * (all) responses.
+                        */
+                       if ((peer->service_state == not_responding) ||
+                           (peer->service_state == service_startup)) {
+                               log_info("%s: not responding%s",
+                                        msgbuf, peer->nrr);
+                               goto out;
+                       } else if((peer->service_state == cooperating) &&
+                                 !load_balance_mine(packet, peer)) {
+                               log_info("%s: load balance to peer %s",
+                                        msgbuf, peer->name);
+                               goto out;
+                       }
+               }
+#endif
+
+               ack_lease (packet, lease, 0, 0, msgbuf, 0, hp);
+               goto out;
        }
 
        /* Run the executable statements to compute the client and server
           options. */
        option_state_allocate (&options, MDL);
-       
+
        /* Execute the subnet statements. */
-       execute_statements_in_scope (packet, lease, packet -> options, options,
-                                    &lease -> scope, lease -> subnet -> group,
-                                    (struct group *)0);
-       
+       execute_statements_in_scope (NULL, packet, lease, NULL,
+                                    packet->options, options,
+                                    &lease->scope, lease->subnet->group,
+                                    NULL, NULL);
+
        /* Execute statements from class scopes. */
        for (i = packet -> class_count; i > 0; i--) {
-               execute_statements_in_scope
-                       (packet, lease, packet -> options, options,
-                        &lease -> scope, packet -> classes [i - 1] -> group,
-                        lease -> subnet -> group);
+               execute_statements_in_scope(NULL, packet, lease, NULL,
+                                           packet->options, options,
+                                           &lease->scope,
+                                           packet->classes[i - 1]->group,
+                                           lease->subnet->group, NULL);
        }
 
        /* Execute the host statements. */
-       execute_statements_in_scope (packet, lease, packet -> options, options,
-                                    &lease -> scope,
-                                    hp -> group, subnet -> group);
-       
+       if (hp != NULL) {
+               execute_statements_in_scope(NULL, packet, lease, NULL,
+                                           packet->options, options,
+                                           &lease->scope, hp->group,
+                                           lease->subnet->group, NULL);
+       }
+
        /* Drop the request if it's not allowed for this client. */
        if ((oc = lookup_option (&server_universe, options, SV_ALLOW_BOOTP)) &&
-           !evaluate_boolean_option_cache (&ignorep, packet, lease,
-                                           packet -> options, options,
-                                           &lease -> scope, oc, MDL)) {
+           !evaluate_boolean_option_cache(&ignorep, packet, lease,
+                                          NULL,
+                                          packet->options, options,
+                                          &lease->scope, oc, MDL)) {
                if (!ignorep)
                        log_info ("%s: bootp disallowed", msgbuf);
-               option_state_dereference (&options, MDL);
-               static_lease_dereference (lease, MDL);
-               return;
-       } 
+               goto out;
+       }
 
-       if ((oc = lookup_option (&server_universe,
+       if ((oc = lookup_option(&server_universe,
                                 options, SV_ALLOW_BOOTING)) &&
-           !evaluate_boolean_option_cache (&ignorep, packet, lease,
-                                           packet -> options, options,
-                                           &lease -> scope, oc, MDL)) {
+           !evaluate_boolean_option_cache(&ignorep, packet, lease,
+                                          NULL,
+                                          packet->options, options,
+                                          &lease->scope, oc, MDL)) {
                if (!ignorep)
                        log_info ("%s: booting disallowed", msgbuf);
-               option_state_dereference (&options, MDL);
-               static_lease_dereference (lease, MDL);
-               return;
+               goto out;
        }
 
        /* Set up the outgoing packet... */
@@ -189,20 +204,26 @@ void bootp (packet)
 
        /* If we didn't get a known vendor magic number on the way in,
           just copy the input options to the output. */
-       if (!packet -> options_valid &&
-           !(evaluate_boolean_option_cache
-             (&ignorep, packet, lease, packet -> options, options,
-              &lease -> scope,
-              lookup_option (&server_universe, options,
-                             SV_ALWAYS_REPLY_RFC1048), MDL))) {
-               memcpy (outgoing.raw -> options,
-                       packet -> raw -> options, DHCP_OPTION_LEN);
-               outgoing.packet_length = BOOTP_MIN_LEN;
+       i = SV_ALWAYS_REPLY_RFC1048;
+       if (!packet->options_valid &&
+           !(evaluate_boolean_option_cache(&ignorep, packet, lease, NULL,
+                                           packet->options, options,
+                                           &lease->scope,
+                                           lookup_option (&server_universe,
+                                                          options, i), MDL))) {
+               if (packet->packet_length > DHCP_FIXED_NON_UDP) {
+                       memcpy(outgoing.raw->options, packet->raw->options,
+                       packet->packet_length - DHCP_FIXED_NON_UDP);
+               }
+
+               outgoing.packet_length =
+                       (packet->packet_length < BOOTP_MIN_LEN)
+                                              ? BOOTP_MIN_LEN
+                                              : packet->packet_length;
        } else {
 
                /* Use the subnet mask from the subnet declaration if no other
                   mask has been provided. */
-
                oc = (struct option_cache *)0;
                i = DHO_SUBNET_MASK;
                if (!lookup_option (&dhcp_universe, options, i)) {
@@ -210,9 +231,11 @@ void bootp (packet)
                                if (make_const_data
                                    (&oc -> expression,
                                     lease -> subnet -> netmask.iabuf,
-                                    lease -> subnet -> netmask.len, 0, 0)) {
-                                       oc -> option =
-                                               dhcp_universe.options [i];
+                                    lease -> subnet -> netmask.len,
+                                    0, 0, MDL)) {
+                                       option_code_hash_lookup(&oc->option,
+                                                       dhcp_universe.code_hash,
+                                                               &i, 0, MDL);
                                        save_option (&dhcp_universe,
                                                     options, oc);
                                }
@@ -220,15 +243,22 @@ void bootp (packet)
                        }
                }
 
+               /* If use-host-decl-names is enabled and there is a hostname
+                * defined in the host delcartion, send it back in hostname
+                * option */
+               use_host_decl_name(packet, lease, options);
+
                /* Pack the options into the buffer.  Unlike DHCP, we
                   can't pack options into the filename and server
                   name buffers. */
 
                outgoing.packet_length =
-                       cons_options (packet, outgoing.raw, lease, 0,
+                       cons_options (packet, outgoing.raw, lease,
+                                     (struct client_state *)0, 0,
                                      packet -> options, options,
                                      &lease -> scope,
-                                     0, 0, 1, (struct data_string *)0);
+                                     0, 0, 1, (struct data_string *)0,
+                                     (const char *)0);
                if (outgoing.packet_length < BOOTP_MIN_LEN)
                        outgoing.packet_length = BOOTP_MIN_LEN;
        }
@@ -243,13 +273,16 @@ void bootp (packet)
        raw.secs = packet -> raw -> secs;
        raw.flags = packet -> raw -> flags;
        raw.ciaddr = packet -> raw -> ciaddr;
-       memcpy (&raw.yiaddr, ip_address.iabuf, sizeof raw.yiaddr);
+
+       /* yiaddr is an ipv4 address, it must be 4 octets. */
+       memcpy (&raw.yiaddr, lease->ip_addr.iabuf, 4);
 
        /* If we're always supposed to broadcast to this client, set
           the broadcast bit in the bootp flags field. */
        if ((oc = lookup_option (&server_universe,
                                options, SV_ALWAYS_BROADCAST)) &&
            evaluate_boolean_option_cache (&ignorep, packet, lease,
+                                          (struct client_state *)0,
                                           packet -> options, options,
                                           &lease -> scope, oc, MDL))
                raw.flags |= htons (BOOTP_BROADCAST);
@@ -259,6 +292,7 @@ void bootp (packet)
        oc = lookup_option (&server_universe, options, SV_NEXT_SERVER);
        if (oc &&
            evaluate_option_cache (&d1, packet, lease,
+                                  (struct client_state *)0,
                                   packet -> options, options,
                                   &lease -> scope, oc, MDL)) {
                /* If there was more than one answer, take the first. */
@@ -266,11 +300,12 @@ void bootp (packet)
                        memcpy (&raw.siaddr, d1.data, 4);
                data_string_forget (&d1, MDL);
        } else {
-               if (lease -> subnet -> shared_network -> interface)
-                       raw.siaddr = (lease -> subnet -> shared_network ->
-                                     interface -> primary_address);
-               else
-                       raw.siaddr = packet -> interface -> primary_address;
+               if ((lease->subnet->shared_network->interface != NULL) &&
+                   lease->subnet->shared_network->interface->address_count)
+                   raw.siaddr =
+                       lease->subnet->shared_network->interface->addresses[0];
+               else if (packet->interface->address_count)
+                       raw.siaddr = packet->interface->addresses[0];
        }
 
        raw.giaddr = packet -> raw -> giaddr;
@@ -279,6 +314,7 @@ void bootp (packet)
        oc = lookup_option (&server_universe, options, SV_FILENAME);
        if (oc &&
            evaluate_option_cache (&d1, packet, lease,
+                                  (struct client_state *)0,
                                   packet -> options, options,
                                   &lease -> scope, oc, MDL)) {
                memcpy (raw.file, d1.data,
@@ -294,6 +330,7 @@ void bootp (packet)
        oc = lookup_option (&server_universe, options, SV_SERVER_NAME);
        if (oc &&
            evaluate_option_cache (&d1, packet, lease,
+                                  (struct client_state *)0,
                                   packet -> options, options,
                                   &lease -> scope, oc, MDL)) {
                memcpy (raw.sname, d1.data,
@@ -305,30 +342,66 @@ void bootp (packet)
        }
 
        /* Execute the commit statements, if there are any. */
-       execute_statements (packet, lease, packet -> options,
-                           options, &lease -> scope, lease -> on_commit);
+       execute_statements (NULL, packet, lease, NULL, packet->options,
+                           options, &lease->scope, lease->on_star.on_commit,
+                           NULL);
 
        /* We're done with the option state. */
        option_state_dereference (&options, MDL);
-       static_lease_dereference (lease, MDL);
+
+#if defined(DHCPv6) && defined(DHCP4o6)
+       if (dhcpv4_over_dhcpv6 && (packet->dhcp4o6_response != NULL)) {
+               /* Report what we're doing... */
+               log_info("%s", msgbuf);
+               log_info("DHCP4o6 BOOTREPLY for %s to %s (%s) via %s",
+                        piaddr(lease->ip_addr),
+                        ((hp != NULL) && (hp->name != NULL)) ?
+                               hp -> name : "unknown",
+                        print_hw_addr (packet->raw->htype,
+                                       packet->raw->hlen,
+                                       packet->raw->chaddr),
+                        piaddr(packet->client_addr));
+
+               /* fill dhcp4o6_response */
+               packet->dhcp4o6_response->len = outgoing.packet_length;
+               packet->dhcp4o6_response->buffer = NULL;
+               if (!buffer_allocate(&packet->dhcp4o6_response->buffer,
+                                    outgoing.packet_length, MDL)) {
+                       log_fatal("No memory to store DHCP4o6 reply.");
+               }
+               packet->dhcp4o6_response->data =
+                       packet->dhcp4o6_response->buffer->data;
+               memcpy(packet->dhcp4o6_response->buffer->data,
+                      outgoing.raw, outgoing.packet_length);
+               goto out;
+       }
+#endif
 
        /* Set up the hardware destination address... */
        hto.hbuf [0] = packet -> raw -> htype;
        hto.hlen = packet -> raw -> hlen + 1;
-       memcpy (hto.hbuf, packet -> raw -> chaddr, packet -> raw -> hlen);
+       memcpy (&hto.hbuf [1], packet -> raw -> chaddr, packet -> raw -> hlen);
 
-       from = packet -> interface -> primary_address;
+       if (packet->interface->address_count) {
+               from = packet->interface->addresses[0];
+       } else {
+               log_error("%s: Interface %s appears to have no IPv4 "
+                         "addresses, and so dhcpd cannot select a source "
+                         "address.", msgbuf, packet->interface->name);
+               goto out;
+       }
 
        /* Report what we're doing... */
-       log_info ("%s", msgbuf);
-       log_info ("BOOTREPLY for %s to %s (%s) via %s",
-             piaddr (ip_address), hp -> name,
-             print_hw_addr (packet -> raw -> htype,
-                            packet -> raw -> hlen,
-                            packet -> raw -> chaddr),
-             packet -> raw -> giaddr.s_addr
-             ? inet_ntoa (packet -> raw -> giaddr)
-             : packet -> interface -> name);
+       log_info("%s", msgbuf);
+       log_info("BOOTREPLY for %s to %s (%s) via %s",
+                piaddr(lease->ip_addr),
+                ((hp != NULL) && (hp->name != NULL)) ? hp -> name : "unknown",
+                print_hw_addr (packet->raw->htype,
+                               packet->raw->hlen,
+                               packet->raw->chaddr),
+                packet->raw->giaddr.s_addr
+                ? inet_ntoa (packet->raw->giaddr)
+                : packet->interface->name);
 
        /* Set up the parts of the address that are in common. */
        to.sin_family = AF_INET;
@@ -343,11 +416,17 @@ void bootp (packet)
                to.sin_port = local_port;
 
                if (fallback_interface) {
-                       result = send_packet (fallback_interface,
-                                             (struct packet *)0,
-                                             &raw, outgoing.packet_length,
-                                             from, &to, &hto);
-                       return;
+                       result = send_packet (fallback_interface, NULL, &raw,
+                                             outgoing.packet_length, from,
+                                             &to, &hto);
+                       if (result < 0) {
+                               log_error ("%s:%d: Failed to send %d byte long "
+                                          "packet over %s interface.", MDL,
+                                          outgoing.packet_length,
+                                          fallback_interface->name);
+                       }
+
+                       goto out;
                }
 
        /* If it comes from a client that already knows its address
@@ -366,7 +445,22 @@ void bootp (packet)
        }
 
        errno = 0;
-       result = send_packet (packet -> interface,
-                             packet, &raw, outgoing.packet_length,
-                             from, &to, &hto);
+       result = send_packet(packet->interface, packet, &raw,
+                            outgoing.packet_length, from, &to, &hto);
+       if (result < 0) {
+               log_error ("%s:%d: Failed to send %d byte long packet over %s"
+                          " interface.", MDL, outgoing.packet_length,
+                          packet->interface->name);
+       }
+
+      out:
+
+       if (options)
+               option_state_dereference (&options, MDL);
+       if (lease)
+               lease_dereference (&lease, MDL);
+       if (hp)
+               host_dereference (&hp, MDL);
+       if (host)
+               host_dereference (&host, MDL);
 }