Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/networking/udhcp/dhcpd.c

    r1765 r2725  
    11/* vi: set sw=4 ts=4: */
    2 /* dhcpd.c
    3  *
    4  * udhcp Server
     2/*
     3 * udhcp server
    54 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
    65 *          Chris Trew <ctrew@moreton.com.au>
     
    87 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
    98 *
    10  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     9 * This program is free software; you can redistribute it and/or modify
     10 * it under the terms of the GNU General Public License as published by
     11 * the Free Software Foundation; either version 2 of the License, or
     12 * (at your option) any later version.
     13 *
     14 * This program is distributed in the hope that it will be useful,
     15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 * GNU General Public License for more details.
     18 *
     19 * You should have received a copy of the GNU General Public License
     20 * along with this program; if not, write to the Free Software
     21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    1122 */
    12 
    1323#include <syslog.h>
    1424#include "common.h"
     25#include "dhcpc.h"
    1526#include "dhcpd.h"
    16 #include "options.h"
     27
     28
     29/* Send a packet to a specific mac address and ip address by creating our own ip packet */
     30static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
     31{
     32    const uint8_t *chaddr;
     33    uint32_t ciaddr;
     34
     35    // Was:
     36    //if (force_broadcast) { /* broadcast */ }
     37    //else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
     38    //else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
     39    //else { /* unicast to dhcp_pkt->yiaddr */ }
     40    // But this is wrong: yiaddr is _our_ idea what client's IP is
     41    // (for example, from lease file). Client may not know that,
     42    // and may not have UDP socket listening on that IP!
     43    // We should never unicast to dhcp_pkt->yiaddr!
     44    // dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
     45    // and can be used.
     46
     47    if (force_broadcast
     48     || (dhcp_pkt->flags & htons(BROADCAST_FLAG))
     49     || dhcp_pkt->ciaddr == 0
     50    ) {
     51        log1("Broadcasting packet to client");
     52        ciaddr = INADDR_BROADCAST;
     53        chaddr = MAC_BCAST_ADDR;
     54    } else {
     55        log1("Unicasting packet to client ciaddr");
     56        ciaddr = dhcp_pkt->ciaddr;
     57        chaddr = dhcp_pkt->chaddr;
     58    }
     59
     60    udhcp_send_raw_packet(dhcp_pkt,
     61        /*src*/ server_config.server_nip, SERVER_PORT,
     62        /*dst*/ ciaddr, CLIENT_PORT, chaddr,
     63        server_config.ifindex);
     64}
     65
     66/* Send a packet to gateway_nip using the kernel ip stack */
     67static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
     68{
     69    log1("Forwarding packet to relay");
     70
     71    udhcp_send_kernel_packet(dhcp_pkt,
     72            server_config.server_nip, SERVER_PORT,
     73            dhcp_pkt->gateway_nip, SERVER_PORT);
     74}
     75
     76static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
     77{
     78    if (dhcp_pkt->gateway_nip)
     79        send_packet_to_relay(dhcp_pkt);
     80    else
     81        send_packet_to_client(dhcp_pkt, force_broadcast);
     82}
     83
     84static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
     85{
     86    /* Sets op, htype, hlen, cookie fields
     87     * and adds DHCP_MESSAGE_TYPE option */
     88    udhcp_init_header(packet, type);
     89
     90    packet->xid = oldpacket->xid;
     91    memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
     92    packet->flags = oldpacket->flags;
     93    packet->gateway_nip = oldpacket->gateway_nip;
     94    packet->ciaddr = oldpacket->ciaddr;
     95    udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_config.server_nip);
     96}
     97
     98/* Fill options field, siaddr_nip, and sname and boot_file fields.
     99 * TODO: teach this code to use overload option.
     100 */
     101static void add_server_options(struct dhcp_packet *packet)
     102{
     103    struct option_set *curr = server_config.options;
     104
     105    while (curr) {
     106        if (curr->data[OPT_CODE] != DHCP_LEASE_TIME)
     107            udhcp_add_binary_option(packet, curr->data);
     108        curr = curr->next;
     109    }
     110
     111    packet->siaddr_nip = server_config.siaddr_nip;
     112
     113    if (server_config.sname)
     114        strncpy((char*)packet->sname, server_config.sname, sizeof(packet->sname) - 1);
     115    if (server_config.boot_file)
     116        strncpy((char*)packet->file, server_config.boot_file, sizeof(packet->file) - 1);
     117}
     118
     119static uint32_t select_lease_time(struct dhcp_packet *packet)
     120{
     121    uint32_t lease_time_sec = server_config.max_lease_sec;
     122    uint8_t *lease_time_opt = udhcp_get_option(packet, DHCP_LEASE_TIME);
     123    if (lease_time_opt) {
     124        move_from_unaligned32(lease_time_sec, lease_time_opt);
     125        lease_time_sec = ntohl(lease_time_sec);
     126        if (lease_time_sec > server_config.max_lease_sec)
     127            lease_time_sec = server_config.max_lease_sec;
     128        if (lease_time_sec < server_config.min_lease_sec)
     129            lease_time_sec = server_config.min_lease_sec;
     130    }
     131    return lease_time_sec;
     132}
     133
     134/* We got a DHCP DISCOVER. Send an OFFER. */
     135/* NOINLINE: limit stack usage in caller */
     136static NOINLINE void send_offer(struct dhcp_packet *oldpacket, uint32_t static_lease_nip, struct dyn_lease *lease)
     137{
     138    struct dhcp_packet packet;
     139    uint32_t lease_time_sec;
     140    struct in_addr addr;
     141
     142    init_packet(&packet, oldpacket, DHCPOFFER);
     143
     144    /* If it is a static lease, use its IP */
     145    packet.yiaddr = static_lease_nip;
     146    /* Else: */
     147    if (!static_lease_nip) {
     148        /* We have no static lease for client's chaddr */
     149        uint32_t req_nip;
     150        uint8_t *req_ip_opt;
     151        const char *p_host_name;
     152
     153        if (lease) {
     154            /* We have a dynamic lease for client's chaddr.
     155             * Reuse its IP (even if lease is expired).
     156             * Note that we ignore requested IP in this case.
     157             */
     158            packet.yiaddr = lease->lease_nip;
     159        }
     160        /* Or: if client has requested an IP */
     161        else if ((req_ip_opt = udhcp_get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL
     162         /* (read IP) */
     163         && (move_from_unaligned32(req_nip, req_ip_opt), 1)
     164         /* and the IP is in the lease range */
     165         && ntohl(req_nip) >= server_config.start_ip
     166         && ntohl(req_nip) <= server_config.end_ip
     167         /* and */
     168         && (  !(lease = find_lease_by_nip(req_nip)) /* is not already taken */
     169            || is_expired_lease(lease) /* or is taken, but expired */
     170            )
     171        ) {
     172            packet.yiaddr = req_nip;
     173        }
     174        else {
     175            /* Otherwise, find a free IP */
     176            packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr);
     177        }
     178
     179        if (!packet.yiaddr) {
     180            bb_error_msg("no free IP addresses. OFFER abandoned");
     181            return;
     182        }
     183        /* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
     184        p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
     185        lease = add_lease(packet.chaddr, packet.yiaddr,
     186                server_config.offer_time,
     187                p_host_name,
     188                p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
     189        );
     190        if (!lease) {
     191            bb_error_msg("no free IP addresses. OFFER abandoned");
     192            return;
     193        }
     194    }
     195
     196    lease_time_sec = select_lease_time(oldpacket);
     197    udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
     198    add_server_options(&packet);
     199
     200    addr.s_addr = packet.yiaddr;
     201    bb_info_msg("Sending OFFER of %s", inet_ntoa(addr));
     202    /* send_packet emits error message itself if it detects failure */
     203    send_packet(&packet, /*force_bcast:*/ 0);
     204}
     205
     206/* NOINLINE: limit stack usage in caller */
     207static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
     208{
     209    struct dhcp_packet packet;
     210
     211    init_packet(&packet, oldpacket, DHCPNAK);
     212
     213    log1("Sending NAK");
     214    send_packet(&packet, /*force_bcast:*/ 1);
     215}
     216
     217/* NOINLINE: limit stack usage in caller */
     218static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
     219{
     220    struct dhcp_packet packet;
     221    uint32_t lease_time_sec;
     222    struct in_addr addr;
     223    const char *p_host_name;
     224
     225    init_packet(&packet, oldpacket, DHCPACK);
     226    packet.yiaddr = yiaddr;
     227
     228    lease_time_sec = select_lease_time(oldpacket);
     229    udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
     230
     231    add_server_options(&packet);
     232
     233    addr.s_addr = yiaddr;
     234    bb_info_msg("Sending ACK to %s", inet_ntoa(addr));
     235    send_packet(&packet, /*force_bcast:*/ 0);
     236
     237    p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
     238    add_lease(packet.chaddr, packet.yiaddr,
     239        lease_time_sec,
     240        p_host_name,
     241        p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
     242    );
     243    if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
     244        /* rewrite the file with leases at every new acceptance */
     245        write_leases();
     246    }
     247}
     248
     249/* NOINLINE: limit stack usage in caller */
     250static NOINLINE void send_inform(struct dhcp_packet *oldpacket)
     251{
     252    struct dhcp_packet packet;
     253
     254    /* "If a client has obtained a network address through some other means
     255     * (e.g., manual configuration), it may use a DHCPINFORM request message
     256     * to obtain other local configuration parameters.  Servers receiving a
     257     * DHCPINFORM message construct a DHCPACK message with any local
     258     * configuration parameters appropriate for the client without:
     259     * allocating a new address, checking for an existing binding, filling
     260     * in 'yiaddr' or including lease time parameters.  The servers SHOULD
     261     * unicast the DHCPACK reply to the address given in the 'ciaddr' field
     262     * of the DHCPINFORM message.
     263     * ...
     264     * The server responds to a DHCPINFORM message by sending a DHCPACK
     265     * message directly to the address given in the 'ciaddr' field
     266     * of the DHCPINFORM message.  The server MUST NOT send a lease
     267     * expiration time to the client and SHOULD NOT fill in 'yiaddr'."
     268     */
     269//TODO: do a few sanity checks: is ciaddr set?
     270//Better yet: is ciaddr == IP source addr?
     271    init_packet(&packet, oldpacket, DHCPACK);
     272    add_server_options(&packet);
     273
     274    send_packet(&packet, /*force_bcast:*/ 0);
     275}
    17276
    18277
    19278/* globals */
    20 struct dhcpOfferedAddr *leases;
    21 struct server_config_t server_config;
    22 
    23 
    24 int udhcpd_main(int argc, char **argv);
    25 int udhcpd_main(int argc, char **argv)
     279struct dyn_lease *g_leases;
     280/* struct server_config_t server_config is in bb_common_bufsiz1 */
     281
     282
     283int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     284int udhcpd_main(int argc UNUSED_PARAM, char **argv)
    26285{
    27286    fd_set rfds;
    28     struct timeval tv;
    29     int server_socket = -1, bytes, retval, max_sock;
    30     struct dhcpMessage packet;
    31     uint8_t *state, *server_id, *requested;
    32     uint32_t server_id_align, requested_align, static_lease_ip;
     287    int server_socket = -1, retval, max_sock;
     288    struct dhcp_packet packet;
     289    uint8_t *state;
     290    uint32_t static_lease_nip;
    33291    unsigned timeout_end;
    34292    unsigned num_ips;
    35293    unsigned opt;
    36294    struct option_set *option;
    37     struct dhcpOfferedAddr *lease, static_lease;
    38 
    39     opt = getopt32(argv, "fS");
    40     argv += optind;
    41 
     295    struct dyn_lease *lease, fake_lease;
     296    IF_FEATURE_UDHCP_PORT(char *str_P;)
     297
     298#if ENABLE_FEATURE_UDHCP_PORT
     299    SERVER_PORT = 67;
     300    CLIENT_PORT = 68;
     301#endif
     302
     303#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
     304    opt_complementary = "vv";
     305#endif
     306    opt = getopt32(argv, "fSv"
     307        IF_FEATURE_UDHCP_PORT("P:", &str_P)
     308#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
     309        , &dhcp_verbose
     310#endif
     311        );
    42312    if (!(opt & 1)) { /* no -f */
    43313        bb_daemonize_or_rexec(0, argv);
    44         logmode &= ~LOGMODE_STDIO;
    45     }
    46 
     314        logmode = LOGMODE_NONE;
     315    }
     316    /* update argv after the possible vfork+exec in daemonize */
     317    argv += optind;
    47318    if (opt & 2) { /* -S */
    48         openlog(applet_name, LOG_PID, LOG_LOCAL0);
     319        openlog(applet_name, LOG_PID, LOG_DAEMON);
    49320        logmode |= LOGMODE_SYSLOG;
    50321    }
    51 
     322#if ENABLE_FEATURE_UDHCP_PORT
     323    if (opt & 8) { /* -P */
     324        SERVER_PORT = xatou16(str_P);
     325        CLIENT_PORT = SERVER_PORT + 1;
     326    }
     327#endif
    52328    /* Would rather not do read_config before daemonization -
    53329     * otherwise NOMMU machines will parse config twice */
     
    61337    /* Create pidfile */
    62338    write_pidfile(server_config.pidfile);
    63     /* if (!..) bb_perror_msg("cannot create pidfile %s", pidfile); */
    64 
    65     bb_info_msg("%s (v%s) started", applet_name, BB_VER);
    66 
    67     option = find_option(server_config.options, DHCP_LEASE_TIME);
    68     server_config.lease = LEASE_TIME;
     339    /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
     340
     341    bb_info_msg("%s (v"BB_VER") started", applet_name);
     342
     343    option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME);
     344    server_config.max_lease_sec = DEFAULT_LEASE_TIME;
    69345    if (option) {
    70         memcpy(&server_config.lease, option->data + 2, 4);
    71         server_config.lease = ntohl(server_config.lease);
     346        move_from_unaligned32(server_config.max_lease_sec, option->data + OPT_DATA);
     347        server_config.max_lease_sec = ntohl(server_config.max_lease_sec);
    72348    }
    73349
     
    80356    }
    81357
    82     leases = xzalloc(server_config.max_leases * sizeof(*leases));
     358    g_leases = xzalloc(server_config.max_leases * sizeof(g_leases[0]));
    83359    read_leases(server_config.lease_file);
    84360
    85     if (read_interface(server_config.interface, &server_config.ifindex,
    86                &server_config.server, server_config.arp)) {
     361    if (udhcp_read_interface(server_config.interface,
     362            &server_config.ifindex,
     363            &server_config.server_nip,
     364            server_config.server_mac)
     365    ) {
    87366        retval = 1;
    88367        goto ret;
     
    94373    timeout_end = monotonic_sec() + server_config.auto_time;
    95374    while (1) { /* loop until universe collapses */
     375        int bytes;
     376        struct timeval tv;
     377        uint8_t *server_id_opt;
     378        uint8_t *requested_opt;
     379        uint32_t requested_nip = requested_nip; /* for compiler */
    96380
    97381        if (server_socket < 0) {
    98             server_socket = listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
     382            server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
    99383                    server_config.interface);
    100384        }
     
    116400        }
    117401        if (retval < 0 && errno != EINTR) {
    118             DEBUG("error on select");
     402            log1("Error on select");
    119403            continue;
    120404        }
     
    122406        switch (udhcp_sp_read(&rfds)) {
    123407        case SIGUSR1:
    124             bb_info_msg("Received a SIGUSR1");
     408            bb_info_msg("Received SIGUSR1");
    125409            write_leases();
    126410            /* why not just reset the timeout, eh */
     
    128412            continue;
    129413        case SIGTERM:
    130             bb_info_msg("Received a SIGTERM");
     414            bb_info_msg("Received SIGTERM");
    131415            goto ret0;
    132         case 0: break;      /* no signal */
    133         default: continue;  /* signal or error (probably EINTR) */
    134         }
    135 
    136         bytes = udhcp_get_packet(&packet, server_socket); /* this waits for a packet - idle */
     416        case 0: /* no signal: read a packet */
     417            break;
     418        default: /* signal or error (probably EINTR): back to select */
     419            continue;
     420        }
     421
     422        bytes = udhcp_recv_kernel_packet(&packet, server_socket);
    137423        if (bytes < 0) {
     424            /* bytes can also be -2 ("bad packet data") */
    138425            if (bytes == -1 && errno != EINTR) {
    139                 DEBUG("error on read, %s, reopening socket", strerror(errno));
     426                log1("Read error: %s, reopening socket", strerror(errno));
    140427                close(server_socket);
    141428                server_socket = -1;
     
    143430            continue;
    144431        }
    145 
    146         state = get_option(&packet, DHCP_MESSAGE_TYPE);
    147         if (state == NULL) {
    148             bb_error_msg("cannot get option from packet, ignoring");
    149             continue;
    150         }
    151 
    152         /* Look for a static lease */
    153         static_lease_ip = getIpByMac(server_config.static_leases, &packet.chaddr);
    154 
    155         if (static_lease_ip) {
    156             bb_info_msg("Found static lease: %x", static_lease_ip);
    157 
    158             memcpy(&static_lease.chaddr, &packet.chaddr, 16);
    159             static_lease.yiaddr = static_lease_ip;
    160             static_lease.expires = 0;
    161 
    162             lease = &static_lease;
     432        if (packet.hlen != 6) {
     433            bb_error_msg("MAC length != 6, ignoring packet");
     434            continue;
     435        }
     436        if (packet.op != BOOTREQUEST) {
     437            bb_error_msg("not a REQUEST, ignoring packet");
     438            continue;
     439        }
     440        state = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
     441        if (state == NULL || state[0] < DHCP_MINTYPE || state[0] > DHCP_MAXTYPE) {
     442            bb_error_msg("no or bad message type option, ignoring packet");
     443            continue;
     444        }
     445
     446        /* Look for a static/dynamic lease */
     447        static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr);
     448        if (static_lease_nip) {
     449            bb_info_msg("Found static lease: %x", static_lease_nip);
     450            memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
     451            fake_lease.lease_nip = static_lease_nip;
     452            fake_lease.expires = 0;
     453            lease = &fake_lease;
    163454        } else {
    164             lease = find_lease_by_chaddr(packet.chaddr);
     455            lease = find_lease_by_mac(packet.chaddr);
     456        }
     457
     458        /* Get REQUESTED_IP and SERVER_ID if present */
     459        server_id_opt = udhcp_get_option(&packet, DHCP_SERVER_ID);
     460        if (server_id_opt) {
     461            uint32_t server_id_net;
     462            move_from_unaligned32(server_id_net, server_id_opt);
     463            if (server_id_net != server_config.server_nip) {
     464                /* client talks to somebody else */
     465                log1("server ID doesn't match, ignoring");
     466                continue;
     467            }
     468        }
     469        requested_opt = udhcp_get_option(&packet, DHCP_REQUESTED_IP);
     470        if (requested_opt) {
     471            move_from_unaligned32(requested_nip, requested_opt);
    165472        }
    166473
    167474        switch (state[0]) {
     475
    168476        case DHCPDISCOVER:
    169             DEBUG("Received DISCOVER");
    170 
    171             if (sendOffer(&packet) < 0) {
    172                 bb_error_msg("send OFFER failed");
     477            log1("Received DISCOVER");
     478
     479            send_offer(&packet, static_lease_nip, lease);
     480            break;
     481
     482        case DHCPREQUEST:
     483            log1("Received REQUEST");
     484/* RFC 2131:
     485
     486o DHCPREQUEST generated during SELECTING state:
     487
     488   Client inserts the address of the selected server in 'server
     489   identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
     490   filled in with the yiaddr value from the chosen DHCPOFFER.
     491
     492   Note that the client may choose to collect several DHCPOFFER
     493   messages and select the "best" offer.  The client indicates its
     494   selection by identifying the offering server in the DHCPREQUEST
     495   message.  If the client receives no acceptable offers, the client
     496   may choose to try another DHCPDISCOVER message.  Therefore, the
     497   servers may not receive a specific DHCPREQUEST from which they can
     498   decide whether or not the client has accepted the offer.
     499
     500o DHCPREQUEST generated during INIT-REBOOT state:
     501
     502   'server identifier' MUST NOT be filled in, 'requested IP address'
     503   option MUST be filled in with client's notion of its previously
     504   assigned address. 'ciaddr' MUST be zero. The client is seeking to
     505   verify a previously allocated, cached configuration. Server SHOULD
     506   send a DHCPNAK message to the client if the 'requested IP address'
     507   is incorrect, or is on the wrong network.
     508
     509   Determining whether a client in the INIT-REBOOT state is on the
     510   correct network is done by examining the contents of 'giaddr', the
     511   'requested IP address' option, and a database lookup. If the DHCP
     512   server detects that the client is on the wrong net (i.e., the
     513   result of applying the local subnet mask or remote subnet mask (if
     514   'giaddr' is not zero) to 'requested IP address' option value
     515   doesn't match reality), then the server SHOULD send a DHCPNAK
     516   message to the client.
     517
     518   If the network is correct, then the DHCP server should check if
     519   the client's notion of its IP address is correct. If not, then the
     520   server SHOULD send a DHCPNAK message to the client. If the DHCP
     521   server has no record of this client, then it MUST remain silent,
     522   and MAY output a warning to the network administrator. This
     523   behavior is necessary for peaceful coexistence of non-
     524   communicating DHCP servers on the same wire.
     525
     526   If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
     527   the same subnet as the server.  The server MUST broadcast the
     528   DHCPNAK message to the 0xffffffff broadcast address because the
     529   client may not have a correct network address or subnet mask, and
     530   the client may not be answering ARP requests.
     531
     532   If 'giaddr' is set in the DHCPREQUEST message, the client is on a
     533   different subnet.  The server MUST set the broadcast bit in the
     534   DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
     535   client, because the client may not have a correct network address
     536   or subnet mask, and the client may not be answering ARP requests.
     537
     538o DHCPREQUEST generated during RENEWING state:
     539
     540   'server identifier' MUST NOT be filled in, 'requested IP address'
     541   option MUST NOT be filled in, 'ciaddr' MUST be filled in with
     542   client's IP address. In this situation, the client is completely
     543   configured, and is trying to extend its lease. This message will
     544   be unicast, so no relay agents will be involved in its
     545   transmission.  Because 'giaddr' is therefore not filled in, the
     546   DHCP server will trust the value in 'ciaddr', and use it when
     547   replying to the client.
     548
     549   A client MAY choose to renew or extend its lease prior to T1.  The
     550   server may choose not to extend the lease (as a policy decision by
     551   the network administrator), but should return a DHCPACK message
     552   regardless.
     553
     554o DHCPREQUEST generated during REBINDING state:
     555
     556   'server identifier' MUST NOT be filled in, 'requested IP address'
     557   option MUST NOT be filled in, 'ciaddr' MUST be filled in with
     558   client's IP address. In this situation, the client is completely
     559   configured, and is trying to extend its lease. This message MUST
     560   be broadcast to the 0xffffffff IP broadcast address.  The DHCP
     561   server SHOULD check 'ciaddr' for correctness before replying to
     562   the DHCPREQUEST.
     563
     564   The DHCPREQUEST from a REBINDING client is intended to accommodate
     565   sites that have multiple DHCP servers and a mechanism for
     566   maintaining consistency among leases managed by multiple servers.
     567   A DHCP server MAY extend a client's lease only if it has local
     568   administrative authority to do so.
     569*/
     570            if (!requested_opt) {
     571                requested_nip = packet.ciaddr;
     572                if (requested_nip == 0) {
     573                    log1("no requested IP and no ciaddr, ignoring");
     574                    break;
     575                }
     576            }
     577            if (lease && requested_nip == lease->lease_nip) {
     578                /* client requested or configured IP matches the lease.
     579                 * ACK it, and bump lease expiration time. */
     580                send_ACK(&packet, lease->lease_nip);
     581                break;
     582            }
     583            if (server_id_opt) {
     584                /* client was talking specifically to us.
     585                 * "No, we don't have this IP for you". */
     586                send_NAK(&packet);
    173587            }
    174588            break;
    175         case DHCPREQUEST:
    176             DEBUG("received REQUEST");
    177 
    178             requested = get_option(&packet, DHCP_REQUESTED_IP);
    179             server_id = get_option(&packet, DHCP_SERVER_ID);
    180 
    181             if (requested) memcpy(&requested_align, requested, 4);
    182             if (server_id) memcpy(&server_id_align, server_id, 4);
    183 
    184             if (lease) {
    185                 if (server_id) {
    186                     /* SELECTING State */
    187                     DEBUG("server_id = %08x", ntohl(server_id_align));
    188                     if (server_id_align == server_config.server && requested
    189                      && requested_align == lease->yiaddr
    190                     ) {
    191                         sendACK(&packet, lease->yiaddr);
    192                     }
    193                 } else if (requested) {
    194                     /* INIT-REBOOT State */
    195                     if (lease->yiaddr == requested_align)
    196                         sendACK(&packet, lease->yiaddr);
    197                     else
    198                         sendNAK(&packet);
    199                 } else if (lease->yiaddr == packet.ciaddr) {
    200                     /* RENEWING or REBINDING State */
    201                     sendACK(&packet, lease->yiaddr);
    202                 } else {
    203                     /* don't know what to do!!!! */
    204                     sendNAK(&packet);
    205                 }
    206 
    207             /* what to do if we have no record of the client */
    208             } else if (server_id) {
    209                 /* SELECTING State */
    210 
    211             } else if (requested) {
    212                 /* INIT-REBOOT State */
    213                 lease = find_lease_by_yiaddr(requested_align);
    214                 if (lease) {
    215                     if (lease_expired(lease)) {
    216                         /* probably best if we drop this lease */
    217                         memset(lease->chaddr, 0, 16);
    218                     /* make some contention for this address */
    219                     } else
    220                         sendNAK(&packet);
    221                 } else {
    222                     uint32_t r = ntohl(requested_align);
    223                     if (r < server_config.start_ip
    224                          || r > server_config.end_ip
    225                     ) {
    226                         sendNAK(&packet);
    227                     }
    228                     /* else remain silent */
    229                 }
    230 
    231             } else {
    232                 /* RENEWING or REBINDING State */
     589
     590        case DHCPDECLINE:
     591            /* RFC 2131:
     592             * "If the server receives a DHCPDECLINE message,
     593             * the client has discovered through some other means
     594             * that the suggested network address is already
     595             * in use. The server MUST mark the network address
     596             * as not available and SHOULD notify the local
     597             * sysadmin of a possible configuration problem."
     598             *
     599             * SERVER_ID must be present,
     600             * REQUESTED_IP must be present,
     601             * chaddr must be filled in,
     602             * ciaddr must be 0 (we do not check this)
     603             */
     604            log1("Received DECLINE");
     605            if (server_id_opt
     606             && requested_opt
     607             && lease  /* chaddr matches this lease */
     608             && requested_nip == lease->lease_nip
     609            ) {
     610                memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
     611                lease->expires = time(NULL) + server_config.decline_time;
    233612            }
    234613            break;
    235         case DHCPDECLINE:
    236             DEBUG("Received DECLINE");
    237             if (lease) {
    238                 memset(lease->chaddr, 0, 16);
    239                 lease->expires = time(0) + server_config.decline_time;
     614
     615        case DHCPRELEASE:
     616            /* "Upon receipt of a DHCPRELEASE message, the server
     617             * marks the network address as not allocated."
     618             *
     619             * SERVER_ID must be present,
     620             * REQUESTED_IP must not be present (we do not check this),
     621             * chaddr must be filled in,
     622             * ciaddr must be filled in
     623             */
     624            log1("Received RELEASE");
     625            if (server_id_opt
     626             && lease  /* chaddr matches this lease */
     627             && packet.ciaddr == lease->lease_nip
     628            ) {
     629                lease->expires = time(NULL);
    240630            }
    241631            break;
    242         case DHCPRELEASE:
    243             DEBUG("Received RELEASE");
    244             if (lease)
    245                 lease->expires = time(0);
    246             break;
     632
    247633        case DHCPINFORM:
    248             DEBUG("Received INFORM");
     634            log1("Received INFORM");
    249635            send_inform(&packet);
    250636            break;
    251         default:
    252             bb_info_msg("Unsupported DHCP message (%02x) - ignoring", state[0]);
    253637        }
    254638    }
Note: See TracChangeset for help on using the changeset viewer.