2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2010-03-20 08:18:11 +05:30
|
|
|
/*
|
2010-03-26 14:39:34 +05:30
|
|
|
* udhcp server
|
2006-05-08 08:50:50 +05:30
|
|
|
* Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
|
|
|
|
* Chris Trew <ctrew@moreton.com.au>
|
|
|
|
*
|
|
|
|
* Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
|
|
|
|
*
|
2010-03-20 08:18:11 +05:30
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2006-05-08 08:50:50 +05:30
|
|
|
*/
|
2016-11-23 23:24:59 +05:30
|
|
|
//applet:IF_UDHCPD(APPLET(udhcpd, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_UDHCPD) += common.o packet.o signalpipe.o socket.o
|
|
|
|
//kbuild:lib-$(CONFIG_UDHCPD) += dhcpd.o arpping.o
|
|
|
|
//kbuild:lib-$(CONFIG_FEATURE_UDHCP_RFC3397) += domain_codec.o
|
2011-04-11 06:59:49 +05:30
|
|
|
|
|
|
|
//usage:#define udhcpd_trivial_usage
|
2021-09-02 17:29:39 +05:30
|
|
|
//usage: "[-fS] [-I ADDR] [-a MSEC]" IF_FEATURE_UDHCP_PORT(" [-P PORT]") " [CONFFILE]"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define udhcpd_full_usage "\n\n"
|
|
|
|
//usage: "DHCP server\n"
|
|
|
|
//usage: "\n -f Run in foreground"
|
|
|
|
//usage: "\n -S Log to syslog too"
|
2013-03-14 02:57:37 +05:30
|
|
|
//usage: "\n -I ADDR Local address"
|
2014-10-30 16:29:04 +05:30
|
|
|
//usage: "\n -a MSEC Timeout for ARP ping (default 2000)"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: IF_FEATURE_UDHCP_PORT(
|
2020-12-19 03:21:46 +05:30
|
|
|
//usage: "\n -P PORT Use PORT (default 67)"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: )
|
2019-06-01 03:09:22 +05:30
|
|
|
//usage: "\nSignals:"
|
|
|
|
//usage: "\n USR1 Update lease file"
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2016-10-04 04:07:50 +05:30
|
|
|
#include <netinet/ether.h>
|
2007-03-26 18:52:35 +05:30
|
|
|
#include <syslog.h>
|
2006-11-19 01:21:32 +05:30
|
|
|
#include "common.h"
|
2008-02-04 18:42:16 +05:30
|
|
|
#include "dhcpc.h"
|
2006-05-08 08:50:50 +05:30
|
|
|
#include "dhcpd.h"
|
|
|
|
|
2020-02-15 02:46:11 +05:30
|
|
|
#if ENABLE_PID_FILE_PATH
|
2020-02-15 02:46:10 +05:30
|
|
|
#define PID_FILE_PATH CONFIG_PID_FILE_PATH
|
|
|
|
#else
|
|
|
|
#define PID_FILE_PATH "/var/run"
|
|
|
|
#endif
|
|
|
|
|
2016-10-04 04:21:38 +05:30
|
|
|
/* globals */
|
2018-02-01 15:11:14 +05:30
|
|
|
#define g_leases ((struct dyn_lease*)ptr_to_globals)
|
2019-05-30 19:53:34 +05:30
|
|
|
/* struct server_data_t server_data is in bb_common_bufsiz1 */
|
2016-10-04 04:21:38 +05:30
|
|
|
|
2019-05-16 14:48:49 +05:30
|
|
|
struct static_lease {
|
|
|
|
struct static_lease *next;
|
|
|
|
uint32_t nip;
|
|
|
|
uint8_t mac[6];
|
|
|
|
uint8_t opt[1];
|
|
|
|
};
|
|
|
|
|
2016-10-04 04:13:14 +05:30
|
|
|
/* Takes the address of the pointer to the static_leases linked list,
|
|
|
|
* address to a 6 byte mac address,
|
|
|
|
* 4 byte IP address */
|
|
|
|
static void add_static_lease(struct static_lease **st_lease_pp,
|
|
|
|
uint8_t *mac,
|
2019-05-16 14:48:49 +05:30
|
|
|
uint32_t nip,
|
|
|
|
const char *opts)
|
2016-10-04 04:13:14 +05:30
|
|
|
{
|
|
|
|
struct static_lease *st_lease;
|
2019-05-16 14:48:49 +05:30
|
|
|
unsigned optlen;
|
2016-10-04 04:13:14 +05:30
|
|
|
|
2019-05-21 19:36:34 +05:30
|
|
|
optlen = (opts ? 1+1+strnlen(opts, 120) : 0);
|
|
|
|
|
2016-10-04 04:13:14 +05:30
|
|
|
/* Find the tail of the list */
|
|
|
|
while ((st_lease = *st_lease_pp) != NULL) {
|
|
|
|
st_lease_pp = &st_lease->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new node */
|
2019-05-16 14:48:49 +05:30
|
|
|
*st_lease_pp = st_lease = xzalloc(sizeof(*st_lease) + optlen);
|
2016-10-04 04:13:14 +05:30
|
|
|
memcpy(st_lease->mac, mac, 6);
|
|
|
|
st_lease->nip = nip;
|
|
|
|
/*st_lease->next = NULL;*/
|
2019-05-16 14:48:49 +05:30
|
|
|
if (optlen) {
|
|
|
|
st_lease->opt[OPT_CODE] = DHCP_HOST_NAME;
|
|
|
|
optlen -= 2;
|
|
|
|
st_lease->opt[OPT_LEN] = optlen;
|
|
|
|
memcpy(&st_lease->opt[OPT_DATA], opts, optlen);
|
|
|
|
}
|
2019-05-21 19:36:34 +05:30
|
|
|
|
|
|
|
#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 2
|
|
|
|
/* Print out static leases just to check what's going on */
|
|
|
|
if (dhcp_verbose >= 2) {
|
|
|
|
bb_info_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x",
|
|
|
|
st_lease->mac[0], st_lease->mac[1], st_lease->mac[2],
|
|
|
|
st_lease->mac[3], st_lease->mac[4], st_lease->mac[5],
|
|
|
|
st_lease->nip
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-04 04:13:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Find static lease IP by mac */
|
2019-05-10 19:25:12 +05:30
|
|
|
static uint32_t get_static_nip_by_mac(void *mac)
|
2016-10-04 04:13:14 +05:30
|
|
|
{
|
2019-05-30 19:53:34 +05:30
|
|
|
struct static_lease *st_lease = server_data.static_leases;
|
2019-05-10 19:25:12 +05:30
|
|
|
|
2016-10-04 04:13:14 +05:30
|
|
|
while (st_lease) {
|
|
|
|
if (memcmp(st_lease->mac, mac, 6) == 0)
|
|
|
|
return st_lease->nip;
|
|
|
|
st_lease = st_lease->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-10 19:25:12 +05:30
|
|
|
static int is_nip_reserved_as_static(uint32_t nip)
|
2016-10-04 04:21:38 +05:30
|
|
|
{
|
2019-05-30 19:53:34 +05:30
|
|
|
struct static_lease *st_lease = server_data.static_leases;
|
2019-05-10 19:25:12 +05:30
|
|
|
|
2016-10-04 04:21:38 +05:30
|
|
|
while (st_lease) {
|
|
|
|
if (st_lease->nip == nip)
|
|
|
|
return 1;
|
|
|
|
st_lease = st_lease->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the oldest expired lease, NULL if there are no expired leases */
|
|
|
|
static struct dyn_lease *oldest_expired_lease(void)
|
|
|
|
{
|
|
|
|
struct dyn_lease *oldest_lease = NULL;
|
|
|
|
leasetime_t oldest_time = time(NULL);
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
/* Unexpired leases have g_leases[i].expires >= current time
|
|
|
|
* and therefore can't ever match */
|
2019-05-30 19:53:34 +05:30
|
|
|
for (i = 0; i < server_data.max_leases; i++) {
|
2016-10-04 04:21:38 +05:30
|
|
|
if (g_leases[i].expires == 0 /* empty entry */
|
|
|
|
|| g_leases[i].expires < oldest_time
|
|
|
|
) {
|
|
|
|
oldest_time = g_leases[i].expires;
|
|
|
|
oldest_lease = &g_leases[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oldest_lease;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear out all leases with matching nonzero chaddr OR yiaddr.
|
|
|
|
* If chaddr == NULL, this is a conflict lease.
|
|
|
|
*/
|
|
|
|
static void clear_leases(const uint8_t *chaddr, uint32_t yiaddr)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
for (i = 0; i < server_data.max_leases; i++) {
|
2016-10-04 04:21:38 +05:30
|
|
|
if ((chaddr && memcmp(g_leases[i].lease_mac, chaddr, 6) == 0)
|
|
|
|
|| (yiaddr && g_leases[i].lease_nip == yiaddr)
|
|
|
|
) {
|
|
|
|
memset(&g_leases[i], 0, sizeof(g_leases[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a lease into the table, clearing out any old ones.
|
|
|
|
* If chaddr == NULL, this is a conflict lease.
|
|
|
|
*/
|
|
|
|
static struct dyn_lease *add_lease(
|
|
|
|
const uint8_t *chaddr, uint32_t yiaddr,
|
|
|
|
leasetime_t leasetime,
|
|
|
|
const char *hostname, int hostname_len)
|
|
|
|
{
|
|
|
|
struct dyn_lease *oldest;
|
|
|
|
|
|
|
|
/* clean out any old ones */
|
|
|
|
clear_leases(chaddr, yiaddr);
|
|
|
|
|
|
|
|
oldest = oldest_expired_lease();
|
|
|
|
|
|
|
|
if (oldest) {
|
|
|
|
memset(oldest, 0, sizeof(*oldest));
|
|
|
|
if (hostname) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
hostname_len++; /* include NUL */
|
|
|
|
if (hostname_len > sizeof(oldest->hostname))
|
|
|
|
hostname_len = sizeof(oldest->hostname);
|
|
|
|
p = safe_strncpy(oldest->hostname, hostname, hostname_len);
|
|
|
|
/*
|
|
|
|
* Sanitization (s/bad_char/./g).
|
|
|
|
* The intent is not to allow only "DNS-valid" hostnames,
|
|
|
|
* but merely make dumpleases output safe for shells to use.
|
|
|
|
* We accept "0-9A-Za-z._-", all other chars turn to dots.
|
|
|
|
*/
|
2020-01-14 21:35:48 +05:30
|
|
|
if (*p == '-')
|
|
|
|
*p = '.'; /* defeat "-option" attacks too */
|
2016-10-04 04:21:38 +05:30
|
|
|
while (*p) {
|
|
|
|
if (!isalnum(*p) && *p != '-' && *p != '_')
|
|
|
|
*p = '.';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chaddr)
|
|
|
|
memcpy(oldest->lease_mac, chaddr, 6);
|
|
|
|
oldest->lease_nip = yiaddr;
|
|
|
|
oldest->expires = time(NULL) + leasetime;
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* True if a lease has expired */
|
|
|
|
static int is_expired_lease(struct dyn_lease *lease)
|
|
|
|
{
|
|
|
|
return (lease->expires < (leasetime_t) time(NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the first lease that matches MAC, NULL if no match */
|
|
|
|
static struct dyn_lease *find_lease_by_mac(const uint8_t *mac)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
for (i = 0; i < server_data.max_leases; i++)
|
2016-10-04 04:21:38 +05:30
|
|
|
if (memcmp(g_leases[i].lease_mac, mac, 6) == 0)
|
|
|
|
return &g_leases[i];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the first lease that matches IP, NULL is no match */
|
|
|
|
static struct dyn_lease *find_lease_by_nip(uint32_t nip)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
for (i = 0; i < server_data.max_leases; i++)
|
2016-10-04 04:21:38 +05:30
|
|
|
if (g_leases[i].lease_nip == nip)
|
|
|
|
return &g_leases[i];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the IP is taken; if it is, add it to the lease table */
|
|
|
|
static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigned arpping_ms)
|
|
|
|
{
|
|
|
|
struct in_addr temp;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = arpping(nip, safe_mac,
|
2019-05-30 19:53:34 +05:30
|
|
|
server_data.server_nip,
|
|
|
|
server_data.server_mac,
|
|
|
|
server_data.interface,
|
2016-10-04 04:21:38 +05:30
|
|
|
arpping_ms);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
temp.s_addr = nip;
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("%s belongs to someone, reserving it for %u seconds",
|
2019-05-30 19:53:34 +05:30
|
|
|
inet_ntoa(temp), (unsigned)server_data.conflict_time);
|
|
|
|
add_lease(NULL, nip, server_data.conflict_time, NULL, 0);
|
2016-10-04 04:21:38 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find a new usable (we think) address */
|
|
|
|
static uint32_t find_free_or_expired_nip(const uint8_t *safe_mac, unsigned arpping_ms)
|
|
|
|
{
|
|
|
|
uint32_t addr;
|
|
|
|
struct dyn_lease *oldest_lease = NULL;
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
|
|
|
|
uint32_t stop;
|
|
|
|
unsigned i, hash;
|
|
|
|
|
|
|
|
/* hash hwaddr: use the SDBM hashing algorithm. Seems to give good
|
|
|
|
* dispersal even with similarly-valued "strings".
|
|
|
|
*/
|
|
|
|
hash = 0;
|
|
|
|
for (i = 0; i < 6; i++)
|
|
|
|
hash += safe_mac[i] + (hash << 6) + (hash << 16) - hash;
|
|
|
|
|
|
|
|
/* pick a seed based on hwaddr then iterate until we find a free address. */
|
2019-05-30 19:53:34 +05:30
|
|
|
addr = server_data.start_ip
|
|
|
|
+ (hash % (1 + server_data.end_ip - server_data.start_ip));
|
2016-10-04 04:21:38 +05:30
|
|
|
stop = addr;
|
|
|
|
#else
|
2019-05-30 19:53:34 +05:30
|
|
|
addr = server_data.start_ip;
|
|
|
|
#define stop (server_data.end_ip + 1)
|
2016-10-04 04:21:38 +05:30
|
|
|
#endif
|
|
|
|
do {
|
|
|
|
uint32_t nip;
|
|
|
|
struct dyn_lease *lease;
|
|
|
|
|
2021-02-04 05:49:18 +05:30
|
|
|
/* (Addresses ending in .0 or .255 can legitimately be allocated
|
|
|
|
* in various situations, so _don't_ skip these. The user needs
|
|
|
|
* to choose start_ip and end_ip correctly for a particular
|
|
|
|
* network environment.) */
|
|
|
|
|
2016-10-04 04:21:38 +05:30
|
|
|
nip = htonl(addr);
|
|
|
|
/* skip our own address */
|
2019-05-30 19:53:34 +05:30
|
|
|
if (nip == server_data.server_nip)
|
2016-10-04 04:21:38 +05:30
|
|
|
goto next_addr;
|
|
|
|
/* is this a static lease addr? */
|
2019-05-10 19:25:12 +05:30
|
|
|
if (is_nip_reserved_as_static(nip))
|
2016-10-04 04:21:38 +05:30
|
|
|
goto next_addr;
|
|
|
|
|
|
|
|
lease = find_lease_by_nip(nip);
|
|
|
|
if (!lease) {
|
|
|
|
//TODO: DHCP servers do not always sit on the same subnet as clients: should *ping*, not arp-ping!
|
|
|
|
if (nobody_responds_to_arp(nip, safe_mac, arpping_ms))
|
|
|
|
return nip;
|
|
|
|
} else {
|
|
|
|
if (!oldest_lease || lease->expires < oldest_lease->expires)
|
|
|
|
oldest_lease = lease;
|
|
|
|
}
|
|
|
|
|
|
|
|
next_addr:
|
|
|
|
addr++;
|
|
|
|
#if ENABLE_FEATURE_UDHCPD_BASE_IP_ON_MAC
|
2019-05-30 19:53:34 +05:30
|
|
|
if (addr > server_data.end_ip)
|
|
|
|
addr = server_data.start_ip;
|
2016-10-04 04:21:38 +05:30
|
|
|
#endif
|
|
|
|
} while (addr != stop);
|
|
|
|
|
|
|
|
if (oldest_lease
|
|
|
|
&& is_expired_lease(oldest_lease)
|
|
|
|
&& nobody_responds_to_arp(oldest_lease->lease_nip, safe_mac, arpping_ms)
|
|
|
|
) {
|
|
|
|
return oldest_lease->lease_nip;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-04 04:13:14 +05:30
|
|
|
/* On these functions, make sure your datatype matches */
|
2016-10-04 04:07:50 +05:30
|
|
|
static int FAST_FUNC read_str(const char *line, void *arg)
|
|
|
|
{
|
|
|
|
char **dest = arg;
|
|
|
|
|
|
|
|
free(*dest);
|
|
|
|
*dest = xstrdup(line);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int FAST_FUNC read_u32(const char *line, void *arg)
|
|
|
|
{
|
|
|
|
*(uint32_t*)arg = bb_strtou32(line, NULL, 10);
|
|
|
|
return errno == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
|
|
|
|
{
|
|
|
|
char *line;
|
|
|
|
char *mac_string;
|
|
|
|
char *ip_string;
|
2019-05-16 14:48:49 +05:30
|
|
|
char *opts;
|
2016-10-04 04:07:50 +05:30
|
|
|
struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */
|
|
|
|
uint32_t nip;
|
|
|
|
|
|
|
|
/* Read mac */
|
|
|
|
line = (char *) const_line;
|
|
|
|
mac_string = strtok_r(line, " \t", &line);
|
|
|
|
if (!mac_string || !ether_aton_r(mac_string, &mac_bytes))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Read ip */
|
|
|
|
ip_string = strtok_r(NULL, " \t", &line);
|
|
|
|
if (!ip_string || !udhcp_str2nip(ip_string, &nip))
|
|
|
|
return 0;
|
|
|
|
|
2019-05-16 14:48:49 +05:30
|
|
|
opts = strtok_r(NULL, " \t", &line);
|
|
|
|
/* opts might be NULL, that's not an error */
|
|
|
|
|
|
|
|
add_static_lease(arg, (uint8_t*) &mac_bytes, nip, opts);
|
2016-10-04 04:07:50 +05:30
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:56:47 +05:30
|
|
|
static int FAST_FUNC read_optset(const char *line, void *arg)
|
|
|
|
{
|
2018-05-14 14:36:35 +05:30
|
|
|
return udhcp_str2optset(line, arg,
|
|
|
|
dhcp_optflags, dhcp_option_strings,
|
|
|
|
/*dhcpv6:*/ 0
|
|
|
|
);
|
2017-06-28 22:48:17 +05:30
|
|
|
}
|
|
|
|
|
2016-10-04 04:07:50 +05:30
|
|
|
struct config_keyword {
|
|
|
|
const char *keyword;
|
|
|
|
int (*handler)(const char *line, void *var) FAST_FUNC;
|
|
|
|
unsigned ofs;
|
|
|
|
const char *def;
|
|
|
|
};
|
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
#define OFS(field) offsetof(struct server_data_t, field)
|
2016-10-04 04:07:50 +05:30
|
|
|
|
2020-11-30 17:33:03 +05:30
|
|
|
static const struct config_keyword keywords[] ALIGN_PTR = {
|
2018-12-27 22:33:20 +05:30
|
|
|
/* keyword handler variable address default */
|
2016-10-04 04:07:50 +05:30
|
|
|
{"start" , udhcp_str2nip , OFS(start_ip ), "192.168.0.20"},
|
|
|
|
{"end" , udhcp_str2nip , OFS(end_ip ), "192.168.0.254"},
|
|
|
|
{"interface" , read_str , OFS(interface ), "eth0"},
|
|
|
|
/* Avoid "max_leases value not sane" warning by setting default
|
|
|
|
* to default_end_ip - default_start_ip + 1: */
|
|
|
|
{"max_leases" , read_u32 , OFS(max_leases ), "235"},
|
|
|
|
{"auto_time" , read_u32 , OFS(auto_time ), "7200"},
|
|
|
|
{"decline_time" , read_u32 , OFS(decline_time ), "3600"},
|
|
|
|
{"conflict_time", read_u32 , OFS(conflict_time), "3600"},
|
|
|
|
{"offer_time" , read_u32 , OFS(offer_time ), "60"},
|
|
|
|
{"min_lease" , read_u32 , OFS(min_lease_sec), "60"},
|
|
|
|
{"lease_file" , read_str , OFS(lease_file ), LEASES_FILE},
|
2020-02-15 02:46:10 +05:30
|
|
|
{"pidfile" , read_str , OFS(pidfile ), PID_FILE_PATH "/udhcpd.pid"},
|
2016-10-04 04:07:50 +05:30
|
|
|
{"siaddr" , udhcp_str2nip , OFS(siaddr_nip ), "0.0.0.0"},
|
|
|
|
/* keywords with no defaults must be last! */
|
2017-06-28 22:48:17 +05:30
|
|
|
{"option" , read_optset , OFS(options ), ""},
|
|
|
|
{"opt" , read_optset , OFS(options ), ""},
|
2016-10-04 04:07:50 +05:30
|
|
|
{"notify_file" , read_str , OFS(notify_file ), NULL},
|
|
|
|
{"sname" , read_str , OFS(sname ), NULL},
|
|
|
|
{"boot_file" , read_str , OFS(boot_file ), NULL},
|
|
|
|
{"static_lease" , read_staticlease, OFS(static_leases), ""},
|
|
|
|
};
|
|
|
|
enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
|
|
|
|
|
|
|
|
static NOINLINE void read_config(const char *file)
|
|
|
|
{
|
|
|
|
parser_t *parser;
|
|
|
|
const struct config_keyword *k;
|
|
|
|
unsigned i;
|
|
|
|
char *token[2];
|
|
|
|
|
|
|
|
for (i = 0; i < KWS_WITH_DEFAULTS; i++)
|
2019-05-30 19:53:34 +05:30
|
|
|
keywords[i].handler(keywords[i].def, (char*)&server_data + keywords[i].ofs);
|
2016-10-04 04:07:50 +05:30
|
|
|
|
|
|
|
parser = config_open(file);
|
|
|
|
while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
|
|
|
|
for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
|
|
|
|
if (strcasecmp(token[0], k->keyword) == 0) {
|
2019-05-30 19:53:34 +05:30
|
|
|
if (!k->handler(token[1], (char*)&server_data + k->ofs)) {
|
2016-10-04 04:07:50 +05:30
|
|
|
bb_error_msg("can't parse line %u in %s",
|
|
|
|
parser->lineno, file);
|
|
|
|
/* reset back to the default value */
|
2019-05-30 19:53:34 +05:30
|
|
|
k->handler(k->def, (char*)&server_data + k->ofs);
|
2016-10-04 04:07:50 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config_close(parser);
|
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
server_data.start_ip = ntohl(server_data.start_ip);
|
|
|
|
server_data.end_ip = ntohl(server_data.end_ip);
|
2021-09-02 18:10:54 +05:30
|
|
|
if (server_data.start_ip > server_data.end_ip)
|
|
|
|
bb_error_msg_and_die("bad start/end IP range in %s", file);
|
2016-10-04 04:07:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void write_leases(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
unsigned i;
|
|
|
|
leasetime_t curr;
|
|
|
|
int64_t written_at;
|
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
fd = open_or_warn(server_data.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
|
2016-10-04 04:07:50 +05:30
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
curr = written_at = time(NULL);
|
|
|
|
|
|
|
|
written_at = SWAP_BE64(written_at);
|
|
|
|
full_write(fd, &written_at, sizeof(written_at));
|
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
for (i = 0; i < server_data.max_leases; i++) {
|
2016-10-04 04:07:50 +05:30
|
|
|
leasetime_t tmp_time;
|
|
|
|
|
|
|
|
if (g_leases[i].lease_nip == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Screw with the time in the struct, for easier writing */
|
|
|
|
tmp_time = g_leases[i].expires;
|
|
|
|
|
|
|
|
g_leases[i].expires -= curr;
|
|
|
|
if ((signed_leasetime_t) g_leases[i].expires < 0)
|
|
|
|
g_leases[i].expires = 0;
|
|
|
|
g_leases[i].expires = htonl(g_leases[i].expires);
|
|
|
|
|
|
|
|
/* No error check. If the file gets truncated,
|
|
|
|
* we lose some leases on restart. Oh well. */
|
|
|
|
full_write(fd, &g_leases[i], sizeof(g_leases[i]));
|
|
|
|
|
|
|
|
/* Then restore it when done */
|
|
|
|
g_leases[i].expires = tmp_time;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
if (server_data.notify_file) {
|
2016-10-04 04:07:50 +05:30
|
|
|
char *argv[3];
|
2019-05-30 19:53:34 +05:30
|
|
|
argv[0] = server_data.notify_file;
|
|
|
|
argv[1] = server_data.lease_file;
|
2016-10-04 04:07:50 +05:30
|
|
|
argv[2] = NULL;
|
|
|
|
spawn_and_wait(argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static NOINLINE void read_leases(const char *file)
|
|
|
|
{
|
|
|
|
struct dyn_lease lease;
|
|
|
|
int64_t written_at, time_passed;
|
|
|
|
int fd;
|
|
|
|
#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
|
|
|
|
unsigned i = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fd = open_or_warn(file, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
|
|
|
|
goto ret;
|
|
|
|
written_at = SWAP_BE64(written_at);
|
|
|
|
|
|
|
|
time_passed = time(NULL) - written_at;
|
|
|
|
/* Strange written_at, or lease file from old version of udhcpd
|
|
|
|
* which had no "written_at" field? */
|
|
|
|
if ((uint64_t)time_passed > 12 * 60 * 60)
|
|
|
|
goto ret;
|
|
|
|
|
|
|
|
while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
|
|
|
|
uint32_t y = ntohl(lease.lease_nip);
|
2019-05-30 19:53:34 +05:30
|
|
|
if (y >= server_data.start_ip && y <= server_data.end_ip) {
|
2016-10-04 04:07:50 +05:30
|
|
|
signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
|
|
|
|
uint32_t static_nip;
|
|
|
|
|
|
|
|
if (expires <= 0)
|
|
|
|
/* We keep expired leases: add_lease() will add
|
|
|
|
* a lease with 0 seconds remaining.
|
|
|
|
* Fewer IP address changes this way for mass reboot scenario.
|
|
|
|
*/
|
|
|
|
expires = 0;
|
|
|
|
|
|
|
|
/* Check if there is a different static lease for this IP or MAC */
|
2019-05-10 19:25:12 +05:30
|
|
|
static_nip = get_static_nip_by_mac(lease.lease_mac);
|
2016-10-04 04:07:50 +05:30
|
|
|
if (static_nip) {
|
|
|
|
/* NB: we do not add lease even if static_nip == lease.lease_nip.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-10 19:25:12 +05:30
|
|
|
if (is_nip_reserved_as_static(lease.lease_nip))
|
2016-10-04 04:07:50 +05:30
|
|
|
continue;
|
|
|
|
|
|
|
|
/* NB: add_lease takes "relative time", IOW,
|
|
|
|
* lease duration, not lease deadline. */
|
|
|
|
if (add_lease(lease.lease_mac, lease.lease_nip,
|
|
|
|
expires,
|
|
|
|
lease.hostname, sizeof(lease.hostname)
|
|
|
|
) == 0
|
|
|
|
) {
|
|
|
|
bb_error_msg("too many leases while loading %s", file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
|
|
|
|
i++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log1("read %d leases", i);
|
|
|
|
ret:
|
|
|
|
close(fd);
|
|
|
|
}
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
/* Send a packet to a specific mac address and ip address by creating our own ip packet */
|
|
|
|
static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadcast)
|
2010-03-20 08:18:11 +05:30
|
|
|
{
|
|
|
|
const uint8_t *chaddr;
|
|
|
|
uint32_t ciaddr;
|
|
|
|
|
|
|
|
// Was:
|
|
|
|
//if (force_broadcast) { /* broadcast */ }
|
|
|
|
//else if (dhcp_pkt->ciaddr) { /* unicast to dhcp_pkt->ciaddr */ }
|
|
|
|
//else if (dhcp_pkt->flags & htons(BROADCAST_FLAG)) { /* broadcast */ }
|
|
|
|
//else { /* unicast to dhcp_pkt->yiaddr */ }
|
|
|
|
// But this is wrong: yiaddr is _our_ idea what client's IP is
|
|
|
|
// (for example, from lease file). Client may not know that,
|
|
|
|
// and may not have UDP socket listening on that IP!
|
|
|
|
// We should never unicast to dhcp_pkt->yiaddr!
|
|
|
|
// dhcp_pkt->ciaddr, OTOH, comes from client's request packet,
|
|
|
|
// and can be used.
|
|
|
|
|
|
|
|
if (force_broadcast
|
|
|
|
|| (dhcp_pkt->flags & htons(BROADCAST_FLAG))
|
2010-03-21 11:16:09 +05:30
|
|
|
|| dhcp_pkt->ciaddr == 0
|
2010-03-20 08:18:11 +05:30
|
|
|
) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
log1s("broadcasting packet to client");
|
2010-03-20 08:18:11 +05:30
|
|
|
ciaddr = INADDR_BROADCAST;
|
|
|
|
chaddr = MAC_BCAST_ADDR;
|
|
|
|
} else {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
log1s("unicasting packet to client ciaddr");
|
2010-03-20 08:18:11 +05:30
|
|
|
ciaddr = dhcp_pkt->ciaddr;
|
|
|
|
chaddr = dhcp_pkt->chaddr;
|
|
|
|
}
|
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
udhcp_send_raw_packet(dhcp_pkt,
|
2019-05-30 19:53:34 +05:30
|
|
|
/*src*/ server_data.server_nip, SERVER_PORT,
|
2010-03-20 08:18:11 +05:30
|
|
|
/*dst*/ ciaddr, CLIENT_PORT, chaddr,
|
2019-05-30 19:53:34 +05:30
|
|
|
server_data.ifindex);
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
/* Send a packet to gateway_nip using the kernel ip stack */
|
|
|
|
static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt)
|
|
|
|
{
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
log1s("forwarding packet to relay");
|
2010-03-21 10:45:28 +05:30
|
|
|
|
|
|
|
udhcp_send_kernel_packet(dhcp_pkt,
|
2019-05-30 19:53:34 +05:30
|
|
|
server_data.server_nip, SERVER_PORT,
|
2020-12-15 15:23:40 +05:30
|
|
|
dhcp_pkt->gateway_nip, SERVER_PORT,
|
2021-09-02 19:53:24 +05:30
|
|
|
/* Yes, relay agents receive (and send) all their packets on SERVER_PORT,
|
|
|
|
* even those which are clients' requests and would normally
|
|
|
|
* (i.e. without relay) use CLIENT_PORT. See RFC 1542.
|
|
|
|
*/
|
2020-12-15 15:23:40 +05:30
|
|
|
server_data.interface);
|
2010-03-21 10:45:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void send_packet(struct dhcp_packet *dhcp_pkt, int force_broadcast)
|
2010-03-20 08:18:11 +05:30
|
|
|
{
|
|
|
|
if (dhcp_pkt->gateway_nip)
|
2010-03-21 10:45:28 +05:30
|
|
|
send_packet_to_relay(dhcp_pkt);
|
|
|
|
else
|
|
|
|
send_packet_to_client(dhcp_pkt, force_broadcast);
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
|
2019-05-16 14:57:28 +05:30
|
|
|
static void send_packet_verbose(struct dhcp_packet *dhcp_pkt, const char *fmt)
|
|
|
|
{
|
|
|
|
struct in_addr addr;
|
|
|
|
addr.s_addr = dhcp_pkt->yiaddr;
|
|
|
|
bb_info_msg(fmt, inet_ntoa(addr));
|
|
|
|
/* send_packet emits error message itself if it detects failure */
|
|
|
|
send_packet(dhcp_pkt, /*force_bcast:*/ 0);
|
|
|
|
}
|
|
|
|
|
2010-03-20 08:18:11 +05:30
|
|
|
static void init_packet(struct dhcp_packet *packet, struct dhcp_packet *oldpacket, char type)
|
|
|
|
{
|
2010-03-21 10:45:28 +05:30
|
|
|
/* Sets op, htype, hlen, cookie fields
|
|
|
|
* and adds DHCP_MESSAGE_TYPE option */
|
2010-03-20 08:18:11 +05:30
|
|
|
udhcp_init_header(packet, type);
|
2010-03-21 10:45:28 +05:30
|
|
|
|
2010-03-20 08:18:11 +05:30
|
|
|
packet->xid = oldpacket->xid;
|
|
|
|
memcpy(packet->chaddr, oldpacket->chaddr, sizeof(oldpacket->chaddr));
|
|
|
|
packet->flags = oldpacket->flags;
|
|
|
|
packet->gateway_nip = oldpacket->gateway_nip;
|
|
|
|
packet->ciaddr = oldpacket->ciaddr;
|
2019-05-30 19:53:34 +05:30
|
|
|
udhcp_add_simple_option(packet, DHCP_SERVER_ID, server_data.server_nip);
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
|
2010-03-21 05:13:11 +05:30
|
|
|
/* Fill options field, siaddr_nip, and sname and boot_file fields.
|
|
|
|
* TODO: teach this code to use overload option.
|
|
|
|
*/
|
|
|
|
static void add_server_options(struct dhcp_packet *packet)
|
2010-03-20 08:18:11 +05:30
|
|
|
{
|
2019-05-16 14:48:49 +05:30
|
|
|
struct option_set *config_opts;
|
|
|
|
uint8_t *client_hostname_opt;
|
|
|
|
|
|
|
|
client_hostname_opt = NULL;
|
|
|
|
if (packet->yiaddr) { /* if we aren't from send_inform()... */
|
2019-05-30 19:53:34 +05:30
|
|
|
struct static_lease *st_lease = server_data.static_leases;
|
2019-05-16 14:48:49 +05:30
|
|
|
while (st_lease) {
|
|
|
|
if (st_lease->nip == packet->yiaddr) {
|
|
|
|
if (st_lease->opt[0] != 0)
|
|
|
|
client_hostname_opt = st_lease->opt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
st_lease = st_lease->next;
|
|
|
|
}
|
|
|
|
}
|
2010-03-21 05:13:11 +05:30
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
config_opts = server_data.options;
|
2019-05-16 14:48:49 +05:30
|
|
|
while (config_opts) {
|
|
|
|
if (config_opts->data[OPT_CODE] != DHCP_LEASE_TIME) {
|
|
|
|
/* ^^^^
|
|
|
|
* DHCP_LEASE_TIME is already filled, or in case of
|
|
|
|
* send_inform(), should not be filled at all.
|
|
|
|
*/
|
|
|
|
if (config_opts->data[OPT_CODE] != DHCP_HOST_NAME
|
|
|
|
|| !client_hostname_opt
|
|
|
|
) {
|
|
|
|
/* Why "!client_hostname_opt":
|
|
|
|
* add hostname only if client has no hostname
|
|
|
|
* on its static lease line.
|
|
|
|
* (Not that "opt hostname HOST"
|
|
|
|
* makes much sense in udhcpd.conf,
|
|
|
|
* that'd give all clients the same hostname,
|
|
|
|
* but it's a valid configuration).
|
|
|
|
*/
|
|
|
|
udhcp_add_binary_option(packet, config_opts->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config_opts = config_opts->next;
|
2010-03-21 05:13:11 +05:30
|
|
|
}
|
|
|
|
|
2019-05-16 14:48:49 +05:30
|
|
|
if (client_hostname_opt)
|
|
|
|
udhcp_add_binary_option(packet, client_hostname_opt);
|
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
packet->siaddr_nip = server_data.siaddr_nip;
|
2010-03-21 05:13:11 +05:30
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
if (server_data.sname)
|
|
|
|
strncpy((char*)packet->sname, server_data.sname, sizeof(packet->sname) - 1);
|
|
|
|
if (server_data.boot_file)
|
|
|
|
strncpy((char*)packet->file, server_data.boot_file, sizeof(packet->file) - 1);
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t select_lease_time(struct dhcp_packet *packet)
|
|
|
|
{
|
2019-05-30 19:53:34 +05:30
|
|
|
uint32_t lease_time_sec = server_data.max_lease_sec;
|
2018-12-17 22:37:18 +05:30
|
|
|
uint8_t *lease_time_opt = udhcp_get_option32(packet, DHCP_LEASE_TIME);
|
2010-03-20 08:18:11 +05:30
|
|
|
if (lease_time_opt) {
|
|
|
|
move_from_unaligned32(lease_time_sec, lease_time_opt);
|
|
|
|
lease_time_sec = ntohl(lease_time_sec);
|
2019-05-30 19:53:34 +05:30
|
|
|
if (lease_time_sec > server_data.max_lease_sec)
|
|
|
|
lease_time_sec = server_data.max_lease_sec;
|
|
|
|
if (lease_time_sec < server_data.min_lease_sec)
|
|
|
|
lease_time_sec = server_data.min_lease_sec;
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
return lease_time_sec;
|
|
|
|
}
|
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
/* We got a DHCP DISCOVER. Send an OFFER. */
|
2010-10-21 16:03:10 +05:30
|
|
|
/* NOINLINE: limit stack usage in caller */
|
2010-11-28 05:40:51 +05:30
|
|
|
static NOINLINE void send_offer(struct dhcp_packet *oldpacket,
|
|
|
|
uint32_t static_lease_nip,
|
|
|
|
struct dyn_lease *lease,
|
2019-05-15 16:38:48 +05:30
|
|
|
uint32_t requested_nip,
|
2014-10-30 16:29:04 +05:30
|
|
|
unsigned arpping_ms)
|
2010-03-20 08:18:11 +05:30
|
|
|
{
|
|
|
|
struct dhcp_packet packet;
|
2010-03-21 10:45:28 +05:30
|
|
|
uint32_t lease_time_sec;
|
2010-03-20 08:18:11 +05:30
|
|
|
|
|
|
|
init_packet(&packet, oldpacket, DHCPOFFER);
|
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
/* If it is a static lease, use its IP */
|
|
|
|
packet.yiaddr = static_lease_nip;
|
|
|
|
/* Else: */
|
2010-03-20 08:19:27 +05:30
|
|
|
if (!static_lease_nip) {
|
2010-03-21 10:45:28 +05:30
|
|
|
/* We have no static lease for client's chaddr */
|
|
|
|
const char *p_host_name;
|
2010-03-21 05:13:11 +05:30
|
|
|
|
2010-03-20 08:18:11 +05:30
|
|
|
if (lease) {
|
2010-03-21 10:45:28 +05:30
|
|
|
/* We have a dynamic lease for client's chaddr.
|
|
|
|
* Reuse its IP (even if lease is expired).
|
|
|
|
* Note that we ignore requested IP in this case.
|
|
|
|
*/
|
2010-03-20 08:18:11 +05:30
|
|
|
packet.yiaddr = lease->lease_nip;
|
|
|
|
}
|
2010-03-21 10:45:28 +05:30
|
|
|
/* Or: if client has requested an IP */
|
2019-05-15 16:38:48 +05:30
|
|
|
else if (requested_nip != 0
|
2010-03-20 08:18:11 +05:30
|
|
|
/* and the IP is in the lease range */
|
2019-05-30 19:53:34 +05:30
|
|
|
&& ntohl(requested_nip) >= server_data.start_ip
|
|
|
|
&& ntohl(requested_nip) <= server_data.end_ip
|
2010-03-21 05:13:11 +05:30
|
|
|
/* and */
|
2019-05-15 16:38:48 +05:30
|
|
|
&& ( !(lease = find_lease_by_nip(requested_nip)) /* is not already taken */
|
2010-03-21 05:13:11 +05:30
|
|
|
|| is_expired_lease(lease) /* or is taken, but expired */
|
|
|
|
)
|
2010-03-20 08:18:11 +05:30
|
|
|
) {
|
2019-05-15 16:38:48 +05:30
|
|
|
packet.yiaddr = requested_nip;
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
else {
|
2010-03-21 10:45:28 +05:30
|
|
|
/* Otherwise, find a free IP */
|
2014-10-30 16:29:04 +05:30
|
|
|
packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr, arpping_ms);
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (!packet.yiaddr) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg("no free IP addresses. OFFER abandoned");
|
2010-03-21 10:45:28 +05:30
|
|
|
return;
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
2010-03-21 10:45:28 +05:30
|
|
|
/* Reserve the IP for a short time hoping to get DHCPREQUEST soon */
|
2010-03-22 18:59:13 +05:30
|
|
|
p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
|
2010-03-21 10:45:28 +05:30
|
|
|
lease = add_lease(packet.chaddr, packet.yiaddr,
|
2019-05-30 19:53:34 +05:30
|
|
|
server_data.offer_time,
|
2010-03-20 08:18:11 +05:30
|
|
|
p_host_name,
|
|
|
|
p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
|
2010-03-21 10:45:28 +05:30
|
|
|
);
|
|
|
|
if (!lease) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_error_msg("no free IP addresses. OFFER abandoned");
|
2010-03-21 10:45:28 +05:30
|
|
|
return;
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
lease_time_sec = select_lease_time(oldpacket);
|
2010-03-26 14:02:09 +05:30
|
|
|
udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
|
2010-03-21 05:13:11 +05:30
|
|
|
add_server_options(&packet);
|
2010-03-20 08:18:11 +05:30
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
/* send_packet emits error message itself if it detects failure */
|
2019-05-16 14:57:28 +05:30
|
|
|
send_packet_verbose(&packet, "sending OFFER to %s");
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
|
2010-10-21 16:03:10 +05:30
|
|
|
/* NOINLINE: limit stack usage in caller */
|
|
|
|
static NOINLINE void send_NAK(struct dhcp_packet *oldpacket)
|
2010-03-20 08:18:11 +05:30
|
|
|
{
|
|
|
|
struct dhcp_packet packet;
|
|
|
|
|
|
|
|
init_packet(&packet, oldpacket, DHCPNAK);
|
|
|
|
|
2016-03-30 22:14:52 +05:30
|
|
|
log1("sending %s", "NAK");
|
2010-03-21 10:45:28 +05:30
|
|
|
send_packet(&packet, /*force_bcast:*/ 1);
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
|
2010-10-21 16:03:10 +05:30
|
|
|
/* NOINLINE: limit stack usage in caller */
|
|
|
|
static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr)
|
2010-03-20 08:18:11 +05:30
|
|
|
{
|
|
|
|
struct dhcp_packet packet;
|
|
|
|
uint32_t lease_time_sec;
|
|
|
|
const char *p_host_name;
|
|
|
|
|
|
|
|
init_packet(&packet, oldpacket, DHCPACK);
|
|
|
|
packet.yiaddr = yiaddr;
|
|
|
|
|
|
|
|
lease_time_sec = select_lease_time(oldpacket);
|
2010-03-26 14:02:09 +05:30
|
|
|
udhcp_add_simple_option(&packet, DHCP_LEASE_TIME, htonl(lease_time_sec));
|
2010-03-21 05:13:11 +05:30
|
|
|
add_server_options(&packet);
|
2010-03-20 08:18:11 +05:30
|
|
|
|
2019-05-16 14:57:28 +05:30
|
|
|
send_packet_verbose(&packet, "sending ACK to %s");
|
2010-03-20 08:18:11 +05:30
|
|
|
|
2010-03-22 18:59:13 +05:30
|
|
|
p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME);
|
2010-03-20 08:18:11 +05:30
|
|
|
add_lease(packet.chaddr, packet.yiaddr,
|
|
|
|
lease_time_sec,
|
|
|
|
p_host_name,
|
|
|
|
p_host_name ? (unsigned char)p_host_name[OPT_LEN - OPT_DATA] : 0
|
|
|
|
);
|
|
|
|
if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) {
|
|
|
|
/* rewrite the file with leases at every new acceptance */
|
|
|
|
write_leases();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-21 16:03:10 +05:30
|
|
|
/* NOINLINE: limit stack usage in caller */
|
|
|
|
static NOINLINE void send_inform(struct dhcp_packet *oldpacket)
|
2010-03-20 08:18:11 +05:30
|
|
|
{
|
|
|
|
struct dhcp_packet packet;
|
|
|
|
|
2010-04-05 02:06:34 +05:30
|
|
|
/* "If a client has obtained a network address through some other means
|
|
|
|
* (e.g., manual configuration), it may use a DHCPINFORM request message
|
|
|
|
* to obtain other local configuration parameters. Servers receiving a
|
|
|
|
* DHCPINFORM message construct a DHCPACK message with any local
|
|
|
|
* configuration parameters appropriate for the client without:
|
|
|
|
* allocating a new address, checking for an existing binding, filling
|
|
|
|
* in 'yiaddr' or including lease time parameters. The servers SHOULD
|
|
|
|
* unicast the DHCPACK reply to the address given in the 'ciaddr' field
|
|
|
|
* of the DHCPINFORM message.
|
|
|
|
* ...
|
|
|
|
* The server responds to a DHCPINFORM message by sending a DHCPACK
|
2010-03-21 10:45:28 +05:30
|
|
|
* message directly to the address given in the 'ciaddr' field
|
|
|
|
* of the DHCPINFORM message. The server MUST NOT send a lease
|
|
|
|
* expiration time to the client and SHOULD NOT fill in 'yiaddr'."
|
|
|
|
*/
|
2010-04-05 02:06:34 +05:30
|
|
|
//TODO: do a few sanity checks: is ciaddr set?
|
|
|
|
//Better yet: is ciaddr == IP source addr?
|
2010-03-20 08:18:11 +05:30
|
|
|
init_packet(&packet, oldpacket, DHCPACK);
|
2010-03-21 05:13:11 +05:30
|
|
|
add_server_options(&packet);
|
2010-03-20 08:18:11 +05:30
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
send_packet(&packet, /*force_bcast:*/ 0);
|
2019-05-16 14:57:28 +05:30
|
|
|
// or maybe? send_packet_verbose(&packet, "sending ACK to %s");
|
2010-03-20 08:18:11 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int udhcpd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int udhcpd_main(int argc UNUSED_PARAM, char **argv)
|
2006-05-08 08:50:50 +05:30
|
|
|
{
|
2017-02-17 03:55:44 +05:30
|
|
|
int server_socket = -1, retval;
|
2007-07-03 21:17:50 +05:30
|
|
|
unsigned timeout_end;
|
|
|
|
unsigned num_ips;
|
2007-08-14 22:15:29 +05:30
|
|
|
unsigned opt;
|
2006-05-08 08:50:50 +05:30
|
|
|
struct option_set *option;
|
2013-03-14 02:57:37 +05:30
|
|
|
char *str_I = str_I;
|
2014-10-30 16:29:04 +05:30
|
|
|
const char *str_a = "2000";
|
|
|
|
unsigned arpping_ms;
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_FEATURE_UDHCP_PORT(char *str_P;)
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2016-04-21 22:24:36 +05:30
|
|
|
setup_common_bufsiz();
|
|
|
|
|
|
|
|
IF_FEATURE_UDHCP_PORT(SERVER_PORT = 67;)
|
|
|
|
IF_FEATURE_UDHCP_PORT(CLIENT_PORT = 68;)
|
2008-02-04 18:42:16 +05:30
|
|
|
|
2019-06-01 03:09:22 +05:30
|
|
|
/* Make sure fd 0,1,2 are open */
|
|
|
|
/* Setup the signal pipe on fds 3,4 - must be before openlog() */
|
|
|
|
udhcp_sp_setup();
|
|
|
|
|
2021-09-02 17:29:39 +05:30
|
|
|
#define OPT_f (1 << 0)
|
|
|
|
#define OPT_S (1 << 1)
|
|
|
|
#define OPT_I (1 << 2)
|
|
|
|
#define OPT_v (1 << 3)
|
|
|
|
#define OPT_a (1 << 4)
|
|
|
|
#define OPT_P (1 << 5)
|
2017-08-09 01:25:02 +05:30
|
|
|
opt = getopt32(argv, "^"
|
|
|
|
"fSI:va:"IF_FEATURE_UDHCP_PORT("P:")
|
|
|
|
"\0"
|
2009-06-17 15:24:52 +05:30
|
|
|
#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
|
2017-08-09 01:25:02 +05:30
|
|
|
"vv"
|
2009-06-17 15:24:52 +05:30
|
|
|
#endif
|
2013-03-14 02:57:37 +05:30
|
|
|
, &str_I
|
2014-10-30 16:29:04 +05:30
|
|
|
, &str_a
|
2013-03-14 02:57:37 +05:30
|
|
|
IF_FEATURE_UDHCP_PORT(, &str_P)
|
2011-10-18 04:05:47 +05:30
|
|
|
IF_UDHCP_VERBOSE(, &dhcp_verbose)
|
2021-09-02 17:29:39 +05:30
|
|
|
);
|
|
|
|
if (!(opt & OPT_f)) { /* no -f */
|
2007-07-01 22:35:57 +05:30
|
|
|
bb_daemonize_or_rexec(0, argv);
|
2009-03-11 20:10:00 +05:30
|
|
|
logmode = LOGMODE_NONE;
|
2007-03-26 18:52:35 +05:30
|
|
|
}
|
2010-06-04 22:54:50 +05:30
|
|
|
/* update argv after the possible vfork+exec in daemonize */
|
|
|
|
argv += optind;
|
2021-09-02 17:29:39 +05:30
|
|
|
if (opt & OPT_S) {
|
2009-03-09 05:16:48 +05:30
|
|
|
openlog(applet_name, LOG_PID, LOG_DAEMON);
|
2007-03-26 18:52:35 +05:30
|
|
|
logmode |= LOGMODE_SYSLOG;
|
|
|
|
}
|
2021-09-02 17:29:39 +05:30
|
|
|
if (opt & OPT_I) {
|
2013-03-14 02:57:37 +05:30
|
|
|
len_and_sockaddr *lsa = xhost_and_af2sockaddr(str_I, 0, AF_INET);
|
2019-05-30 19:53:34 +05:30
|
|
|
server_data.server_nip = lsa->u.sin.sin_addr.s_addr;
|
2013-03-14 02:57:37 +05:30
|
|
|
free(lsa);
|
|
|
|
}
|
2008-02-04 18:42:16 +05:30
|
|
|
#if ENABLE_FEATURE_UDHCP_PORT
|
2021-09-02 17:29:39 +05:30
|
|
|
if (opt & OPT_P) {
|
2008-02-04 18:42:16 +05:30
|
|
|
SERVER_PORT = xatou16(str_P);
|
|
|
|
CLIENT_PORT = SERVER_PORT + 1;
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-30 16:29:04 +05:30
|
|
|
arpping_ms = xatou(str_a);
|
|
|
|
|
2007-03-26 18:52:35 +05:30
|
|
|
/* Would rather not do read_config before daemonization -
|
|
|
|
* otherwise NOMMU machines will parse config twice */
|
2007-08-16 01:33:36 +05:30
|
|
|
read_config(argv[0] ? argv[0] : DHCPD_CONF_FILE);
|
2018-03-11 16:04:44 +05:30
|
|
|
/* prevent poll timeout overflow */
|
2019-05-30 19:53:34 +05:30
|
|
|
if (server_data.auto_time > INT_MAX / 1000)
|
|
|
|
server_data.auto_time = INT_MAX / 1000;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2007-08-03 04:01:05 +05:30
|
|
|
/* Create pidfile */
|
2019-05-30 19:53:34 +05:30
|
|
|
write_pidfile(server_data.pidfile);
|
2009-11-13 13:38:27 +05:30
|
|
|
/* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */
|
2007-08-03 04:01:05 +05:30
|
|
|
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_info_msg("started, v"BB_VER);
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2021-12-12 21:43:54 +05:30
|
|
|
option = udhcp_find_option(server_data.options, DHCP_LEASE_TIME, /*dhcpv6:*/ 0);
|
2019-05-30 19:53:34 +05:30
|
|
|
server_data.max_lease_sec = DEFAULT_LEASE_TIME;
|
2007-03-26 18:52:35 +05:30
|
|
|
if (option) {
|
2019-05-30 19:53:34 +05:30
|
|
|
move_from_unaligned32(server_data.max_lease_sec, option->data + OPT_DATA);
|
|
|
|
server_data.max_lease_sec = ntohl(server_data.max_lease_sec);
|
2007-05-04 05:09:35 +05:30
|
|
|
}
|
2006-05-08 08:50:50 +05:30
|
|
|
|
|
|
|
/* Sanity check */
|
2019-05-30 19:53:34 +05:30
|
|
|
num_ips = server_data.end_ip - server_data.start_ip + 1;
|
|
|
|
if (server_data.max_leases > num_ips) {
|
2007-07-03 21:17:50 +05:30
|
|
|
bb_error_msg("max_leases=%u is too big, setting to %u",
|
2019-05-30 19:53:34 +05:30
|
|
|
(unsigned)server_data.max_leases, num_ips);
|
|
|
|
server_data.max_leases = num_ips;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
2018-02-01 15:11:14 +05:30
|
|
|
/* this sets g_leases */
|
2019-05-30 19:53:34 +05:30
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(server_data.max_leases * sizeof(g_leases[0])));
|
2018-02-01 15:11:14 +05:30
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
read_leases(server_data.lease_file);
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2019-05-30 19:53:34 +05:30
|
|
|
if (udhcp_read_interface(server_data.interface,
|
|
|
|
&server_data.ifindex,
|
|
|
|
(server_data.server_nip == 0 ? &server_data.server_nip : NULL),
|
|
|
|
server_data.server_mac)
|
2009-06-16 15:34:23 +05:30
|
|
|
) {
|
2007-05-04 05:09:35 +05:30
|
|
|
retval = 1;
|
|
|
|
goto ret;
|
|
|
|
}
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2012-07-24 20:51:26 +05:30
|
|
|
continue_with_autotime:
|
2019-05-30 19:53:34 +05:30
|
|
|
timeout_end = monotonic_sec() + server_data.auto_time;
|
2006-11-19 01:21:32 +05:30
|
|
|
while (1) { /* loop until universe collapses */
|
2017-02-17 03:55:44 +05:30
|
|
|
struct pollfd pfds[2];
|
2010-11-28 05:40:51 +05:30
|
|
|
struct dhcp_packet packet;
|
2009-01-01 23:22:09 +05:30
|
|
|
int bytes;
|
2017-02-17 03:55:44 +05:30
|
|
|
int tv;
|
2021-09-02 18:10:54 +05:30
|
|
|
uint8_t *msg_type;
|
2010-03-21 10:45:28 +05:30
|
|
|
uint8_t *server_id_opt;
|
2010-11-28 05:40:51 +05:30
|
|
|
uint8_t *requested_ip_opt;
|
2019-05-15 16:38:48 +05:30
|
|
|
uint32_t requested_nip;
|
2010-11-28 05:40:51 +05:30
|
|
|
uint32_t static_lease_nip;
|
|
|
|
struct dyn_lease *lease, fake_lease;
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2006-11-28 05:13:28 +05:30
|
|
|
if (server_socket < 0) {
|
2008-09-26 15:04:59 +05:30
|
|
|
server_socket = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT,
|
2019-05-30 19:53:34 +05:30
|
|
|
server_data.interface);
|
2006-11-28 05:13:28 +05:30
|
|
|
}
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2017-02-17 03:55:44 +05:30
|
|
|
udhcp_sp_fd_set(pfds, server_socket);
|
2018-03-11 16:04:44 +05:30
|
|
|
|
|
|
|
new_tv:
|
|
|
|
tv = -1;
|
2019-05-30 19:53:34 +05:30
|
|
|
if (server_data.auto_time) {
|
2018-03-11 16:04:44 +05:30
|
|
|
tv = timeout_end - monotonic_sec();
|
|
|
|
if (tv <= 0) {
|
|
|
|
write_leases:
|
udhcpd: fix "not dying on SIGTERM"
Fixes:
commit 52a515d18724bbb34e3ccbbb0218efcc4eccc0a8
"udhcp: use poll() instead of select()"
Feb 16 2017
udhcp_sp_read() is meant to check whether signal pipe indeed has some data to read.
In the above commit, it was changed as follows:
- if (!FD_ISSET(signal_pipe.rd, rfds))
+ if (!pfds[0].revents)
return 0;
The problem is, the check was working for select() purely by accident.
Caught signal interrupts select()/poll() syscalls, they return with EINTR
(regardless of SA_RESTART flag in sigaction). _Then_ signal handler is invoked.
IOW: they can't see any changes to fd state caused by signal haldler
(in our case, signal handler makes signal pipe ready to be read).
For select(), it means that rfds[] bit array is unmodified, bit of signal
pipe's read fd is still set, and the above check "works": it thinks select()
says there is data to read.
This accident does not work for poll(): .revents stays clear, and we do not
try reading signal pipe as we should. In udhcpd, we fall through and block
in socket read. Further SIGTERM signals simply cause socket read to be
interrupted and then restarted (since SIGTERM handler has SA_RESTART=1).
Fixing this as follows: remove the check altogether. Set signal pipe read fd
to nonblocking mode. Always read it in udhcp_sp_read().
If read fails, assume it's EAGAIN and return 0 ("no signal seen").
udhcpd avoids reading signal pipe on every recvd packet by looping if EINTR
(using safe_poll()) - thus ensuring we have correct .revents for all fds -
and calling udhcp_sp_read() only if pfds[0].revents!=0.
udhcpc performs much fewer reads (typically it sleeps >99.999% of the time),
there is no need to optimize it: can call udhcp_sp_read() after each poll
unconditionally.
To robustify socket reads, unconditionally set pfds[1].revents=0
in udhcp_sp_fd_set() (which is before poll), and check it before reading
network socket in udhcpd.
TODO:
This might still fail: if pfds[1].revents=POLLIN, socket read may still block.
There are rare cases when select/poll indicates that data can be read,
but then actual read still blocks (one such case is UDP packets with
wrong checksum). General advise is, if you use a poll/select loop,
keep all your fds nonblocking.
Maybe we should also do that to our network sockets?
function old new delta
udhcp_sp_setup 55 65 +10
udhcp_sp_fd_set 54 60 +6
udhcp_sp_read 46 36 -10
udhcpd_main 1451 1437 -14
udhcpc_main 2723 2708 -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/3 up/down: 16/-39) Total: -23 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-10 23:31:48 +05:30
|
|
|
write_leases();
|
|
|
|
goto continue_with_autotime;
|
|
|
|
}
|
2018-03-11 16:04:44 +05:30
|
|
|
tv *= 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Block here waiting for either signal or packet */
|
|
|
|
retval = poll(pfds, 2, tv);
|
|
|
|
if (retval <= 0) {
|
|
|
|
if (retval == 0)
|
|
|
|
goto write_leases;
|
|
|
|
if (errno == EINTR)
|
|
|
|
goto new_tv;
|
udhcpd: fix "not dying on SIGTERM"
Fixes:
commit 52a515d18724bbb34e3ccbbb0218efcc4eccc0a8
"udhcp: use poll() instead of select()"
Feb 16 2017
udhcp_sp_read() is meant to check whether signal pipe indeed has some data to read.
In the above commit, it was changed as follows:
- if (!FD_ISSET(signal_pipe.rd, rfds))
+ if (!pfds[0].revents)
return 0;
The problem is, the check was working for select() purely by accident.
Caught signal interrupts select()/poll() syscalls, they return with EINTR
(regardless of SA_RESTART flag in sigaction). _Then_ signal handler is invoked.
IOW: they can't see any changes to fd state caused by signal haldler
(in our case, signal handler makes signal pipe ready to be read).
For select(), it means that rfds[] bit array is unmodified, bit of signal
pipe's read fd is still set, and the above check "works": it thinks select()
says there is data to read.
This accident does not work for poll(): .revents stays clear, and we do not
try reading signal pipe as we should. In udhcpd, we fall through and block
in socket read. Further SIGTERM signals simply cause socket read to be
interrupted and then restarted (since SIGTERM handler has SA_RESTART=1).
Fixing this as follows: remove the check altogether. Set signal pipe read fd
to nonblocking mode. Always read it in udhcp_sp_read().
If read fails, assume it's EAGAIN and return 0 ("no signal seen").
udhcpd avoids reading signal pipe on every recvd packet by looping if EINTR
(using safe_poll()) - thus ensuring we have correct .revents for all fds -
and calling udhcp_sp_read() only if pfds[0].revents!=0.
udhcpc performs much fewer reads (typically it sleeps >99.999% of the time),
there is no need to optimize it: can call udhcp_sp_read() after each poll
unconditionally.
To robustify socket reads, unconditionally set pfds[1].revents=0
in udhcp_sp_fd_set() (which is before poll), and check it before reading
network socket in udhcpd.
TODO:
This might still fail: if pfds[1].revents=POLLIN, socket read may still block.
There are rare cases when select/poll indicates that data can be read,
but then actual read still blocks (one such case is UDP packets with
wrong checksum). General advise is, if you use a poll/select loop,
keep all your fds nonblocking.
Maybe we should also do that to our network sockets?
function old new delta
udhcp_sp_setup 55 65 +10
udhcp_sp_fd_set 54 60 +6
udhcp_sp_read 46 36 -10
udhcpd_main 1451 1437 -14
udhcpc_main 2723 2708 -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/3 up/down: 16/-39) Total: -23 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-10 23:31:48 +05:30
|
|
|
/* < 0 and not EINTR: should not happen */
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg_and_die("poll");
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
udhcpd: fix "not dying on SIGTERM"
Fixes:
commit 52a515d18724bbb34e3ccbbb0218efcc4eccc0a8
"udhcp: use poll() instead of select()"
Feb 16 2017
udhcp_sp_read() is meant to check whether signal pipe indeed has some data to read.
In the above commit, it was changed as follows:
- if (!FD_ISSET(signal_pipe.rd, rfds))
+ if (!pfds[0].revents)
return 0;
The problem is, the check was working for select() purely by accident.
Caught signal interrupts select()/poll() syscalls, they return with EINTR
(regardless of SA_RESTART flag in sigaction). _Then_ signal handler is invoked.
IOW: they can't see any changes to fd state caused by signal haldler
(in our case, signal handler makes signal pipe ready to be read).
For select(), it means that rfds[] bit array is unmodified, bit of signal
pipe's read fd is still set, and the above check "works": it thinks select()
says there is data to read.
This accident does not work for poll(): .revents stays clear, and we do not
try reading signal pipe as we should. In udhcpd, we fall through and block
in socket read. Further SIGTERM signals simply cause socket read to be
interrupted and then restarted (since SIGTERM handler has SA_RESTART=1).
Fixing this as follows: remove the check altogether. Set signal pipe read fd
to nonblocking mode. Always read it in udhcp_sp_read().
If read fails, assume it's EAGAIN and return 0 ("no signal seen").
udhcpd avoids reading signal pipe on every recvd packet by looping if EINTR
(using safe_poll()) - thus ensuring we have correct .revents for all fds -
and calling udhcp_sp_read() only if pfds[0].revents!=0.
udhcpc performs much fewer reads (typically it sleeps >99.999% of the time),
there is no need to optimize it: can call udhcp_sp_read() after each poll
unconditionally.
To robustify socket reads, unconditionally set pfds[1].revents=0
in udhcp_sp_fd_set() (which is before poll), and check it before reading
network socket in udhcpd.
TODO:
This might still fail: if pfds[1].revents=POLLIN, socket read may still block.
There are rare cases when select/poll indicates that data can be read,
but then actual read still blocks (one such case is UDP packets with
wrong checksum). General advise is, if you use a poll/select loop,
keep all your fds nonblocking.
Maybe we should also do that to our network sockets?
function old new delta
udhcp_sp_setup 55 65 +10
udhcp_sp_fd_set 54 60 +6
udhcp_sp_read 46 36 -10
udhcpd_main 1451 1437 -14
udhcpc_main 2723 2708 -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/3 up/down: 16/-39) Total: -23 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-10 23:31:48 +05:30
|
|
|
if (pfds[0].revents) switch (udhcp_sp_read()) {
|
2006-05-08 08:50:50 +05:30
|
|
|
case SIGUSR1:
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("received %s", "SIGUSR1");
|
2006-05-08 08:50:50 +05:30
|
|
|
write_leases();
|
|
|
|
/* why not just reset the timeout, eh */
|
2012-07-24 20:51:26 +05:30
|
|
|
goto continue_with_autotime;
|
2006-05-08 08:50:50 +05:30
|
|
|
case SIGTERM:
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("received %s", "SIGTERM");
|
2012-07-24 20:51:26 +05:30
|
|
|
write_leases();
|
2007-05-04 05:09:35 +05:30
|
|
|
goto ret0;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
udhcpd: fix "not dying on SIGTERM"
Fixes:
commit 52a515d18724bbb34e3ccbbb0218efcc4eccc0a8
"udhcp: use poll() instead of select()"
Feb 16 2017
udhcp_sp_read() is meant to check whether signal pipe indeed has some data to read.
In the above commit, it was changed as follows:
- if (!FD_ISSET(signal_pipe.rd, rfds))
+ if (!pfds[0].revents)
return 0;
The problem is, the check was working for select() purely by accident.
Caught signal interrupts select()/poll() syscalls, they return with EINTR
(regardless of SA_RESTART flag in sigaction). _Then_ signal handler is invoked.
IOW: they can't see any changes to fd state caused by signal haldler
(in our case, signal handler makes signal pipe ready to be read).
For select(), it means that rfds[] bit array is unmodified, bit of signal
pipe's read fd is still set, and the above check "works": it thinks select()
says there is data to read.
This accident does not work for poll(): .revents stays clear, and we do not
try reading signal pipe as we should. In udhcpd, we fall through and block
in socket read. Further SIGTERM signals simply cause socket read to be
interrupted and then restarted (since SIGTERM handler has SA_RESTART=1).
Fixing this as follows: remove the check altogether. Set signal pipe read fd
to nonblocking mode. Always read it in udhcp_sp_read().
If read fails, assume it's EAGAIN and return 0 ("no signal seen").
udhcpd avoids reading signal pipe on every recvd packet by looping if EINTR
(using safe_poll()) - thus ensuring we have correct .revents for all fds -
and calling udhcp_sp_read() only if pfds[0].revents!=0.
udhcpc performs much fewer reads (typically it sleeps >99.999% of the time),
there is no need to optimize it: can call udhcp_sp_read() after each poll
unconditionally.
To robustify socket reads, unconditionally set pfds[1].revents=0
in udhcp_sp_fd_set() (which is before poll), and check it before reading
network socket in udhcpd.
TODO:
This might still fail: if pfds[1].revents=POLLIN, socket read may still block.
There are rare cases when select/poll indicates that data can be read,
but then actual read still blocks (one such case is UDP packets with
wrong checksum). General advise is, if you use a poll/select loop,
keep all your fds nonblocking.
Maybe we should also do that to our network sockets?
function old new delta
udhcp_sp_setup 55 65 +10
udhcp_sp_fd_set 54 60 +6
udhcp_sp_read 46 36 -10
udhcpd_main 1451 1437 -14
udhcpc_main 2723 2708 -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/3 up/down: 16/-39) Total: -23 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-10 23:31:48 +05:30
|
|
|
/* Is it a packet? */
|
|
|
|
if (!pfds[1].revents)
|
|
|
|
continue; /* no */
|
|
|
|
|
|
|
|
/* Note: we do not block here, we block on poll() instead.
|
|
|
|
* Blocking here would prevent SIGTERM from working:
|
|
|
|
* socket read inside this call is restarted on caught signals.
|
|
|
|
*/
|
2009-01-01 23:22:09 +05:30
|
|
|
bytes = udhcp_recv_kernel_packet(&packet, server_socket);
|
2021-09-02 19:53:24 +05:30
|
|
|
//NB: we do not check source port here. Should we?
|
|
|
|
//It should be CLIENT_PORT for clients,
|
|
|
|
//or SERVER_PORT for relay agents (in which case giaddr must be != 0.0.0.0)
|
2007-03-26 18:52:35 +05:30
|
|
|
if (bytes < 0) {
|
2009-01-01 23:22:09 +05:30
|
|
|
/* bytes can also be -2 ("bad packet data") */
|
2006-05-08 08:50:50 +05:30
|
|
|
if (bytes == -1 && errno != EINTR) {
|
2017-09-29 21:47:25 +05:30
|
|
|
log1("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
|
2006-05-08 08:50:50 +05:30
|
|
|
close(server_socket);
|
|
|
|
server_socket = -1;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2009-06-17 15:27:09 +05:30
|
|
|
if (packet.hlen != 6) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_info_msg("MAC length != 6%s", ", ignoring packet");
|
2009-06-17 15:27:09 +05:30
|
|
|
continue;
|
|
|
|
}
|
2010-03-21 10:45:28 +05:30
|
|
|
if (packet.op != BOOTREQUEST) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_info_msg("not a REQUEST%s", ", ignoring packet");
|
2010-03-21 10:45:28 +05:30
|
|
|
continue;
|
|
|
|
}
|
2021-09-02 18:10:54 +05:30
|
|
|
msg_type = udhcp_get_option(&packet, DHCP_MESSAGE_TYPE);
|
|
|
|
if (!msg_type || msg_type[0] < DHCP_MINTYPE || msg_type[0] > DHCP_MAXTYPE) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_info_msg("no or bad message type option%s", ", ignoring packet");
|
2006-05-08 08:50:50 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-11-28 05:40:51 +05:30
|
|
|
/* Get SERVER_ID if present */
|
2018-12-17 22:37:18 +05:30
|
|
|
server_id_opt = udhcp_get_option32(&packet, DHCP_SERVER_ID);
|
2010-11-28 05:40:51 +05:30
|
|
|
if (server_id_opt) {
|
2010-11-29 02:21:44 +05:30
|
|
|
uint32_t server_id_network_order;
|
|
|
|
move_from_unaligned32(server_id_network_order, server_id_opt);
|
2019-05-30 19:53:34 +05:30
|
|
|
if (server_id_network_order != server_data.server_nip) {
|
2010-11-28 05:40:51 +05:30
|
|
|
/* client talks to somebody else */
|
2021-02-21 21:02:07 +05:30
|
|
|
log1("server ID doesn't match%s", ", ignoring packet");
|
2010-11-28 05:40:51 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
/* Look for a static/dynamic lease */
|
2019-05-10 19:25:12 +05:30
|
|
|
static_lease_nip = get_static_nip_by_mac(&packet.chaddr);
|
2010-03-20 08:19:27 +05:30
|
|
|
if (static_lease_nip) {
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("found static lease: %x", static_lease_nip);
|
2009-06-17 16:54:03 +05:30
|
|
|
memcpy(&fake_lease.lease_mac, &packet.chaddr, 6);
|
2010-03-20 08:19:27 +05:30
|
|
|
fake_lease.lease_nip = static_lease_nip;
|
2009-06-17 16:54:03 +05:30
|
|
|
fake_lease.expires = 0;
|
|
|
|
lease = &fake_lease;
|
2006-11-19 01:21:32 +05:30
|
|
|
} else {
|
2009-06-17 16:54:03 +05:30
|
|
|
lease = find_lease_by_mac(packet.chaddr);
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
|
2010-11-28 05:40:51 +05:30
|
|
|
/* Get REQUESTED_IP if present */
|
2019-05-15 16:38:48 +05:30
|
|
|
requested_nip = 0;
|
2018-12-17 22:37:18 +05:30
|
|
|
requested_ip_opt = udhcp_get_option32(&packet, DHCP_REQUESTED_IP);
|
2010-11-28 05:40:51 +05:30
|
|
|
if (requested_ip_opt) {
|
|
|
|
move_from_unaligned32(requested_nip, requested_ip_opt);
|
2010-03-21 10:45:28 +05:30
|
|
|
}
|
|
|
|
|
2021-09-02 18:10:54 +05:30
|
|
|
switch (msg_type[0]) {
|
2010-03-21 10:45:28 +05:30
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
case DHCPDISCOVER:
|
2016-03-30 22:11:23 +05:30
|
|
|
log1("received %s", "DISCOVER");
|
2006-05-08 08:50:50 +05:30
|
|
|
|
2019-05-15 16:38:48 +05:30
|
|
|
send_offer(&packet, static_lease_nip, lease, requested_nip, arpping_ms);
|
2006-05-08 08:50:50 +05:30
|
|
|
break;
|
|
|
|
|
2010-03-21 10:45:28 +05:30
|
|
|
case DHCPREQUEST:
|
2016-03-30 22:11:23 +05:30
|
|
|
log1("received %s", "REQUEST");
|
2010-03-21 11:16:09 +05:30
|
|
|
/* RFC 2131:
|
|
|
|
|
|
|
|
o DHCPREQUEST generated during SELECTING state:
|
|
|
|
|
|
|
|
Client inserts the address of the selected server in 'server
|
|
|
|
identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be
|
|
|
|
filled in with the yiaddr value from the chosen DHCPOFFER.
|
|
|
|
|
|
|
|
Note that the client may choose to collect several DHCPOFFER
|
|
|
|
messages and select the "best" offer. The client indicates its
|
|
|
|
selection by identifying the offering server in the DHCPREQUEST
|
|
|
|
message. If the client receives no acceptable offers, the client
|
|
|
|
may choose to try another DHCPDISCOVER message. Therefore, the
|
|
|
|
servers may not receive a specific DHCPREQUEST from which they can
|
|
|
|
decide whether or not the client has accepted the offer.
|
|
|
|
|
|
|
|
o DHCPREQUEST generated during INIT-REBOOT state:
|
|
|
|
|
|
|
|
'server identifier' MUST NOT be filled in, 'requested IP address'
|
|
|
|
option MUST be filled in with client's notion of its previously
|
|
|
|
assigned address. 'ciaddr' MUST be zero. The client is seeking to
|
|
|
|
verify a previously allocated, cached configuration. Server SHOULD
|
|
|
|
send a DHCPNAK message to the client if the 'requested IP address'
|
|
|
|
is incorrect, or is on the wrong network.
|
|
|
|
|
|
|
|
Determining whether a client in the INIT-REBOOT state is on the
|
|
|
|
correct network is done by examining the contents of 'giaddr', the
|
|
|
|
'requested IP address' option, and a database lookup. If the DHCP
|
|
|
|
server detects that the client is on the wrong net (i.e., the
|
|
|
|
result of applying the local subnet mask or remote subnet mask (if
|
|
|
|
'giaddr' is not zero) to 'requested IP address' option value
|
|
|
|
doesn't match reality), then the server SHOULD send a DHCPNAK
|
|
|
|
message to the client.
|
|
|
|
|
|
|
|
If the network is correct, then the DHCP server should check if
|
|
|
|
the client's notion of its IP address is correct. If not, then the
|
|
|
|
server SHOULD send a DHCPNAK message to the client. If the DHCP
|
|
|
|
server has no record of this client, then it MUST remain silent,
|
|
|
|
and MAY output a warning to the network administrator. This
|
|
|
|
behavior is necessary for peaceful coexistence of non-
|
|
|
|
communicating DHCP servers on the same wire.
|
|
|
|
|
|
|
|
If 'giaddr' is 0x0 in the DHCPREQUEST message, the client is on
|
|
|
|
the same subnet as the server. The server MUST broadcast the
|
|
|
|
DHCPNAK message to the 0xffffffff broadcast address because the
|
|
|
|
client may not have a correct network address or subnet mask, and
|
|
|
|
the client may not be answering ARP requests.
|
|
|
|
|
|
|
|
If 'giaddr' is set in the DHCPREQUEST message, the client is on a
|
|
|
|
different subnet. The server MUST set the broadcast bit in the
|
|
|
|
DHCPNAK, so that the relay agent will broadcast the DHCPNAK to the
|
|
|
|
client, because the client may not have a correct network address
|
|
|
|
or subnet mask, and the client may not be answering ARP requests.
|
|
|
|
|
|
|
|
o DHCPREQUEST generated during RENEWING state:
|
|
|
|
|
|
|
|
'server identifier' MUST NOT be filled in, 'requested IP address'
|
|
|
|
option MUST NOT be filled in, 'ciaddr' MUST be filled in with
|
|
|
|
client's IP address. In this situation, the client is completely
|
|
|
|
configured, and is trying to extend its lease. This message will
|
|
|
|
be unicast, so no relay agents will be involved in its
|
|
|
|
transmission. Because 'giaddr' is therefore not filled in, the
|
|
|
|
DHCP server will trust the value in 'ciaddr', and use it when
|
|
|
|
replying to the client.
|
|
|
|
|
|
|
|
A client MAY choose to renew or extend its lease prior to T1. The
|
|
|
|
server may choose not to extend the lease (as a policy decision by
|
|
|
|
the network administrator), but should return a DHCPACK message
|
|
|
|
regardless.
|
|
|
|
|
|
|
|
o DHCPREQUEST generated during REBINDING state:
|
|
|
|
|
|
|
|
'server identifier' MUST NOT be filled in, 'requested IP address'
|
|
|
|
option MUST NOT be filled in, 'ciaddr' MUST be filled in with
|
|
|
|
client's IP address. In this situation, the client is completely
|
|
|
|
configured, and is trying to extend its lease. This message MUST
|
|
|
|
be broadcast to the 0xffffffff IP broadcast address. The DHCP
|
|
|
|
server SHOULD check 'ciaddr' for correctness before replying to
|
|
|
|
the DHCPREQUEST.
|
|
|
|
|
|
|
|
The DHCPREQUEST from a REBINDING client is intended to accommodate
|
|
|
|
sites that have multiple DHCP servers and a mechanism for
|
|
|
|
maintaining consistency among leases managed by multiple servers.
|
|
|
|
A DHCP server MAY extend a client's lease only if it has local
|
|
|
|
administrative authority to do so.
|
|
|
|
*/
|
2010-11-28 05:40:51 +05:30
|
|
|
if (!requested_ip_opt) {
|
2010-03-21 11:16:09 +05:30
|
|
|
requested_nip = packet.ciaddr;
|
|
|
|
if (requested_nip == 0) {
|
2021-02-21 21:02:07 +05:30
|
|
|
log1("no requested IP and no ciaddr%s", ", ignoring packet");
|
2010-03-21 11:16:09 +05:30
|
|
|
break;
|
|
|
|
}
|
2010-03-21 10:45:28 +05:30
|
|
|
}
|
|
|
|
if (lease && requested_nip == lease->lease_nip) {
|
2010-03-21 11:16:09 +05:30
|
|
|
/* client requested or configured IP matches the lease.
|
2010-03-21 10:45:28 +05:30
|
|
|
* ACK it, and bump lease expiration time. */
|
|
|
|
send_ACK(&packet, lease->lease_nip);
|
|
|
|
break;
|
|
|
|
}
|
2010-11-29 02:21:44 +05:30
|
|
|
/* No lease for this MAC, or lease IP != requested IP */
|
|
|
|
|
|
|
|
if (server_id_opt /* client is in SELECTING state */
|
|
|
|
|| requested_ip_opt /* client is in INIT-REBOOT state */
|
|
|
|
) {
|
|
|
|
/* "No, we don't have this IP for you" */
|
2010-03-21 10:45:28 +05:30
|
|
|
send_NAK(&packet);
|
2010-11-29 02:21:44 +05:30
|
|
|
} /* else: client is in RENEWING or REBINDING, do not answer */
|
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
break;
|
2010-03-21 10:45:28 +05:30
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
case DHCPDECLINE:
|
2010-03-21 10:45:28 +05:30
|
|
|
/* RFC 2131:
|
|
|
|
* "If the server receives a DHCPDECLINE message,
|
|
|
|
* the client has discovered through some other means
|
|
|
|
* that the suggested network address is already
|
|
|
|
* in use. The server MUST mark the network address
|
|
|
|
* as not available and SHOULD notify the local
|
|
|
|
* sysadmin of a possible configuration problem."
|
|
|
|
*
|
|
|
|
* SERVER_ID must be present,
|
|
|
|
* REQUESTED_IP must be present,
|
|
|
|
* chaddr must be filled in,
|
|
|
|
* ciaddr must be 0 (we do not check this)
|
|
|
|
*/
|
2016-03-30 22:11:23 +05:30
|
|
|
log1("received %s", "DECLINE");
|
2010-03-21 10:45:28 +05:30
|
|
|
if (server_id_opt
|
2010-11-28 05:40:51 +05:30
|
|
|
&& requested_ip_opt
|
2010-03-21 10:45:28 +05:30
|
|
|
&& lease /* chaddr matches this lease */
|
|
|
|
&& requested_nip == lease->lease_nip
|
|
|
|
) {
|
2009-06-17 15:27:09 +05:30
|
|
|
memset(lease->lease_mac, 0, sizeof(lease->lease_mac));
|
2019-05-30 19:53:34 +05:30
|
|
|
lease->expires = time(NULL) + server_data.decline_time;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|
|
|
|
break;
|
2010-03-21 10:45:28 +05:30
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
case DHCPRELEASE:
|
2010-03-21 10:45:28 +05:30
|
|
|
/* "Upon receipt of a DHCPRELEASE message, the server
|
|
|
|
* marks the network address as not allocated."
|
|
|
|
*
|
|
|
|
* SERVER_ID must be present,
|
|
|
|
* REQUESTED_IP must not be present (we do not check this),
|
|
|
|
* chaddr must be filled in,
|
|
|
|
* ciaddr must be filled in
|
|
|
|
*/
|
2016-03-30 22:11:23 +05:30
|
|
|
log1("received %s", "RELEASE");
|
2010-03-21 10:45:28 +05:30
|
|
|
if (server_id_opt
|
|
|
|
&& lease /* chaddr matches this lease */
|
|
|
|
&& packet.ciaddr == lease->lease_nip
|
|
|
|
) {
|
2009-02-02 16:18:06 +05:30
|
|
|
lease->expires = time(NULL);
|
2010-03-21 10:45:28 +05:30
|
|
|
}
|
2006-05-08 08:50:50 +05:30
|
|
|
break;
|
2010-03-21 10:45:28 +05:30
|
|
|
|
2006-05-08 08:50:50 +05:30
|
|
|
case DHCPINFORM:
|
2016-03-30 22:11:23 +05:30
|
|
|
log1("received %s", "INFORM");
|
2006-05-08 08:50:50 +05:30
|
|
|
send_inform(&packet);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-05-04 05:09:35 +05:30
|
|
|
ret0:
|
|
|
|
retval = 0;
|
|
|
|
ret:
|
2019-05-30 19:53:34 +05:30
|
|
|
/*if (server_data.pidfile) - server_data.pidfile is never NULL */
|
|
|
|
remove_pidfile(server_data.pidfile);
|
2007-05-04 05:09:35 +05:30
|
|
|
return retval;
|
2006-05-08 08:50:50 +05:30
|
|
|
}
|