2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2021-03-18 16:06:01 +01:00
|
|
|
* apps/netutils/dhcpd/dhcpd.c
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_DHCPD_HOST
|
2014-03-04 00:46:56 +01:00
|
|
|
# include <stdio.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2014-03-04 00:46:56 +01:00
|
|
|
# define HTONS(a) htons(a)
|
|
|
|
# define HTONL(a) htonl(a)
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2014-03-04 00:46:56 +01:00
|
|
|
# define CONFIG_CPP_HAVE_WARNING 1
|
|
|
|
# define FAR
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
# define nerr(...) printf(__VA_ARGS__)
|
2016-06-11 19:55:38 +02:00
|
|
|
# define ninfo(...) printf(__VA_ARGS__)
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2014-03-04 00:46:56 +01:00
|
|
|
# define ERROR (-1)
|
|
|
|
# define OK (0)
|
2011-03-19 22:04:13 +01:00
|
|
|
#else
|
2014-03-04 00:46:56 +01:00
|
|
|
# include <nuttx/config.h> /* NuttX configuration */
|
2016-06-11 23:51:27 +02:00
|
|
|
# include <debug.h> /* For nerr, info */
|
2014-03-04 00:46:56 +01:00
|
|
|
# include <nuttx/compiler.h> /* For CONFIG_CPP_HAVE_WARNING */
|
2016-07-11 18:11:18 +02:00
|
|
|
# include "netutils/dhcpd.h" /* Advertised DHCPD APIs */
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2020-11-26 03:56:25 +01:00
|
|
|
#include <inttypes.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
2021-06-16 10:11:34 +02:00
|
|
|
#include <netinet/udp.h>
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
2016-02-08 19:07:47 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define DHCP_SERVER_PORT 67
|
|
|
|
#define DHCP_CLIENT_PORT 68
|
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
/* Option codes understood in this file
|
|
|
|
* Code Data Description
|
|
|
|
* Length
|
|
|
|
*/
|
2014-03-31 20:33:52 +02:00
|
|
|
#define DHCP_OPTION_PAD 0 /* 1 Pad */
|
2014-11-18 14:20:18 +01:00
|
|
|
#define DHCP_OPTION_SUBNET_MASK 1 /* 1 Subnet Mask */
|
|
|
|
#define DHCP_OPTION_ROUTER 3 /* 4 Router */
|
|
|
|
#define DHCP_OPTION_DNS_SERVER 6 /* 4N DNS */
|
2011-03-19 22:04:13 +01:00
|
|
|
#define DHCP_OPTION_REQ_IPADDR 50 /* 4 Requested IP Address */
|
|
|
|
#define DHCP_OPTION_LEASE_TIME 51 /* 4 IP address lease time */
|
|
|
|
#define DHCP_OPTION_OVERLOAD 52 /* 1 Option overload */
|
|
|
|
#define DHCP_OPTION_MSG_TYPE 53 /* 1 DHCP message type */
|
|
|
|
#define DHCP_OPTION_SERVER_ID 54 /* 4 Server identifier */
|
|
|
|
#define DHCP_OPTION_END 255 /* 0 End */
|
|
|
|
|
|
|
|
/* Values for the dhcp msg 'op' field */
|
|
|
|
|
|
|
|
#define DHCP_REQUEST 1
|
|
|
|
#define DHCP_REPLY 2
|
|
|
|
|
|
|
|
/* DHCP message types understood in this file */
|
|
|
|
|
|
|
|
#define DHCPDISCOVER 1 /* Received from client only */
|
|
|
|
#define DHCPOFFER 2 /* Sent from server only */
|
|
|
|
#define DHCPREQUEST 3 /* Received from client only */
|
|
|
|
#define DHCPDECLINE 4 /* Received from client only */
|
|
|
|
#define DHCPACK 5 /* Sent from server only */
|
|
|
|
#define DHCPNAK 6 /* Sent from server only */
|
|
|
|
#define DHCPRELEASE 7 /* Received from client only */
|
|
|
|
#define DHCPINFORM 8 /* Not used */
|
|
|
|
|
|
|
|
/* The form of an option is:
|
|
|
|
* code - 1 byte
|
|
|
|
* length - 1 byte
|
|
|
|
* data - variable number of bytes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DHCPD_OPTION_CODE 0
|
|
|
|
#define DHCPD_OPTION_LENGTH 1
|
|
|
|
#define DHCPD_OPTION_DATA 2
|
|
|
|
|
|
|
|
/* Size of options in DHCP message */
|
|
|
|
|
|
|
|
#define DHCPD_OPTIONS_SIZE 312
|
|
|
|
|
|
|
|
/* Values for htype and hlen field */
|
|
|
|
|
|
|
|
#define DHCP_HTYPE_ETHERNET 1
|
|
|
|
#define DHCP_HLEN_ETHERNET 6
|
|
|
|
|
|
|
|
/* Values for flags field */
|
|
|
|
|
|
|
|
#define BOOTP_BROADCAST 0x8000
|
|
|
|
|
|
|
|
/* Legal values for this option are:
|
|
|
|
*
|
|
|
|
* 1 the 'file' field is used to hold options
|
|
|
|
* 2 the 'sname' field is used to hold options
|
|
|
|
* 3 both fields are used to hold options
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DHCPD_OPTION_FIELD 0
|
|
|
|
#define DHCPD_FILE_FIELD 1
|
|
|
|
#define DHCPD_SNAME_FIELD 2
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_DHCPD_LEASETIME
|
2014-03-04 00:46:56 +01:00
|
|
|
# define CONFIG_NETUTILS_DHCPD_LEASETIME (60*60*24*10) /* 10 days */
|
|
|
|
# undef CONFIG_NETUTILS_DHCPD_MINLEASETIME
|
|
|
|
# undef CONFIG_NETUTILS_DHCPD_MAXLEASETIME
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_DHCPD_MINLEASETIME
|
2014-03-04 00:46:56 +01:00
|
|
|
# define CONFIG_NETUTILS_DHCPD_MINLEASETIME (60*60*24*1) /* 1 days */
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_DHCPD_MAXLEASETIME
|
2014-03-04 00:46:56 +01:00
|
|
|
# define CONFIG_NETUTILS_DHCPD_MAXLEASETIME (60*60*24*30) /* 30 days */
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_DHCPD_MAXLEASES
|
2014-03-04 00:46:56 +01:00
|
|
|
# define CONFIG_NETUTILS_DHCPD_MAXLEASES 16
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_DHCPD_STARTIP
|
2014-03-04 00:46:56 +01:00
|
|
|
# define CONFIG_NETUTILS_DHCPD_STARTIP (10L<<24|0L<<16|0L<<16|2L)
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef CONFIG_NETUTILS_DHCP_OPTION_ENDIP
|
|
|
|
#define CONFIG_NETUTILS_DHCP_OPTION_ENDIP \
|
|
|
|
(CONFIG_NETUTILS_DHCPD_STARTIP + CONFIG_NETUTILS_DHCPD_MAXLEASES - 1)
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_DHCPD_OFFERTIME
|
2014-03-04 00:46:56 +01:00
|
|
|
# define CONFIG_NETUTILS_DHCPD_OFFERTIME (60*60) /* 1 hour */
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_DHCPD_DECLINETIME
|
2014-03-04 00:46:56 +01:00
|
|
|
# define CONFIG_NETUTILS_DHCPD_DECLINETIME (60*60) /* 1 hour */
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
2014-11-18 14:20:18 +01:00
|
|
|
#undef HAVE_ROUTERIP
|
|
|
|
#if defined(CONFIG_NETUTILS_DHCPD_ROUTERIP) && CONFIG_NETUTILS_DHCPD_ROUTERIP
|
|
|
|
# define HAVE_ROUTERIP 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef HAVE_NETMASK
|
|
|
|
#if defined(CONFIG_NETUTILS_DHCPD_NETMASK) && CONFIG_NETUTILS_DHCPD_NETMASK
|
|
|
|
# define HAVE_NETMASK 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef HAVE_DNSIP
|
|
|
|
#if defined(CONFIG_NETUTILS_DHCPD_DNSIP) && CONFIG_NETUTILS_DHCPD_DNSIP
|
|
|
|
# define HAVE_DNSIP 1
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
#undef HAVE_LEASE_TIME
|
|
|
|
#if defined(CONFIG_NETUTILS_DHCPD_HOST) || !defined(CONFIG_DISABLE_POSIX_TIMERS)
|
2014-03-04 00:46:56 +01:00
|
|
|
# define HAVE_LEASE_TIME 1
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
/* This structure describes one element in the lease table. There is one
|
|
|
|
* slot in the lease table for each assign-able IP address (hence, the IP
|
|
|
|
* address itself does not have to be in the table.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct lease_s
|
|
|
|
{
|
|
|
|
uint8_t mac[DHCP_HLEN_ETHERNET]; /* MAC address (network order) -- could be larger! */
|
|
|
|
bool allocated; /* true: IP address is allocated */
|
|
|
|
#ifdef HAVE_LEASE_TIME
|
|
|
|
time_t expiry; /* Lease expiration time (seconds past Epoch) */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dhcpmsg_s
|
|
|
|
{
|
|
|
|
uint8_t op;
|
|
|
|
uint8_t htype;
|
|
|
|
uint8_t hlen;
|
|
|
|
uint8_t hops;
|
|
|
|
uint8_t xid[4];
|
|
|
|
uint16_t secs;
|
|
|
|
uint16_t flags;
|
|
|
|
uint8_t ciaddr[4];
|
|
|
|
uint8_t yiaddr[4];
|
|
|
|
uint8_t siaddr[4];
|
|
|
|
uint8_t giaddr[4];
|
|
|
|
uint8_t chaddr[16];
|
|
|
|
#ifndef CONFIG_NET_DHCP_LIGHT
|
|
|
|
uint8_t sname[64];
|
|
|
|
uint8_t file[128];
|
|
|
|
#endif
|
|
|
|
uint8_t options[312];
|
|
|
|
};
|
|
|
|
|
2020-05-08 08:30:04 +02:00
|
|
|
/* This enumeration describes the state of the DHCPD daemon */
|
|
|
|
|
|
|
|
enum dhcpd_daemon_e
|
|
|
|
{
|
|
|
|
DHCPD_NOT_RUNNING = 0,
|
|
|
|
DHCPD_STARTED,
|
|
|
|
DHCPD_RUNNING,
|
|
|
|
DHCPD_STOP_REQUESTED,
|
|
|
|
DHCPD_STOPPED
|
|
|
|
};
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
struct dhcpd_state_s
|
|
|
|
{
|
|
|
|
/* Server configuration */
|
|
|
|
|
|
|
|
in_addr_t ds_serverip; /* The server IP address */
|
|
|
|
|
|
|
|
/* Message buffers */
|
|
|
|
|
|
|
|
struct dhcpmsg_s ds_inpacket; /* Holds the incoming DHCP client message */
|
|
|
|
struct dhcpmsg_s ds_outpacket; /* Holds the outgoing DHCP server message */
|
|
|
|
|
|
|
|
/* Parsed options from the incoming DHCP client message */
|
|
|
|
|
|
|
|
uint8_t ds_optmsgtype; /* Incoming DHCP message type */
|
|
|
|
in_addr_t ds_optreqip; /* Requested IP address (host order) */
|
|
|
|
in_addr_t ds_optserverip; /* Serverip IP address (host order) */
|
|
|
|
time_t ds_optleasetime; /* Requested lease time (host order) */
|
|
|
|
|
|
|
|
/* End option pointer for outgoing DHCP server message */
|
|
|
|
|
|
|
|
uint8_t *ds_optend;
|
|
|
|
|
|
|
|
/* Leases */
|
|
|
|
|
|
|
|
struct lease_s ds_leases[CONFIG_NETUTILS_DHCPD_MAXLEASES];
|
|
|
|
};
|
|
|
|
|
2020-05-08 08:30:04 +02:00
|
|
|
/* This type describes the state of the DHCPD client daemon. Only one
|
|
|
|
* instance of the DHCPD daemon is permitted in this implementation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct dhcpd_daemon_s
|
|
|
|
{
|
|
|
|
uint8_t ds_state; /* See enum dhcpd_daemon_e */
|
|
|
|
sem_t ds_lock; /* Used to protect the whole structure */
|
|
|
|
sem_t ds_sync; /* Used to synchronize start and stop events */
|
|
|
|
pid_t ds_pid; /* Task ID of the DHCPD daemon */
|
|
|
|
};
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
static const uint8_t g_magiccookie[4] =
|
|
|
|
{
|
|
|
|
99, 130, 83, 99
|
|
|
|
};
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
static struct dhcpd_state_s g_state;
|
|
|
|
|
2020-05-08 08:30:04 +02:00
|
|
|
/* This type describes the state of the DHCPD client daemon. Only one
|
|
|
|
* instance of the DHCPD daemon is permitted in this implementation. This
|
|
|
|
* limitation is due only to this global data structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct dhcpd_daemon_s g_dhcpd_daemon =
|
|
|
|
{
|
|
|
|
DHCPD_NOT_RUNNING,
|
|
|
|
SEM_INITIALIZER(1),
|
|
|
|
SEM_INITIALIZER(0),
|
|
|
|
-1
|
|
|
|
};
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_arpupdate
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NETUTILS_DHCPD_HOST
|
2016-02-08 19:07:47 +01:00
|
|
|
static inline void dhcpd_arpupdate(FAR uint8_t *ipaddr, FAR uint8_t *hwaddr)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2016-02-08 19:07:47 +01:00
|
|
|
struct sockaddr_in inaddr;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2019-09-29 20:53:39 +02:00
|
|
|
/* Put the protocol address in a standard form. ipaddr is assumed to be in
|
2016-02-08 19:07:47 +01:00
|
|
|
* network order by the memcpy.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
2016-02-08 19:07:47 +01:00
|
|
|
inaddr.sin_family = AF_INET;
|
2016-02-09 18:24:30 +01:00
|
|
|
inaddr.sin_port = 0;
|
2019-07-26 03:52:57 +02:00
|
|
|
memcpy(&inaddr.sin_addr.s_addr, ipaddr, sizeof(in_addr_t));
|
2016-02-08 19:07:47 +01:00
|
|
|
|
|
|
|
/* Update the ARP table */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
netlib_set_arpmapping(&inaddr, hwaddr);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
#else
|
2016-02-08 19:07:47 +01:00
|
|
|
# define dhcpd_arpupdate(ipaddr,hwaddr)
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_time
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_DHCPD_HOST
|
|
|
|
# define dhcpd_time() time(0)
|
|
|
|
#elif defined(HAVE_LEASE_TIME)
|
|
|
|
static time_t dhcpd_time(void)
|
|
|
|
{
|
2011-12-13 18:25:23 +01:00
|
|
|
struct timespec ts;
|
2011-03-19 22:04:13 +01:00
|
|
|
time_t ret = 0;
|
|
|
|
|
2011-12-13 18:25:23 +01:00
|
|
|
if (clock_gettime(CLOCK_REALTIME, &ts) == OK)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2011-12-13 18:25:23 +01:00
|
|
|
ret = ts.tv_sec;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define dhcpd_time() (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_leaseexpired
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef HAVE_LEASE_TIME
|
|
|
|
static inline bool dhcpd_leaseexpired(struct lease_s *lease)
|
|
|
|
{
|
2019-12-19 14:35:52 +01:00
|
|
|
if (lease->expiry > dhcpd_time())
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(lease, 0, sizeof(struct lease_s));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define dhcpd_leaseexpired(lease) (false)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_setlease
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
struct lease_s *dhcpd_setlease(const uint8_t *mac,
|
|
|
|
in_addr_t ipaddr, time_t expiry)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2011-12-13 18:25:23 +01:00
|
|
|
/* Calculate the offset from the first IP address managed by DHCPD.
|
|
|
|
* ipaddr must be in host order!
|
|
|
|
*/
|
|
|
|
|
|
|
|
int ndx = ipaddr - CONFIG_NETUTILS_DHCPD_STARTIP;
|
2011-03-19 22:04:13 +01:00
|
|
|
struct lease_s *ret = NULL;
|
|
|
|
|
2020-11-26 03:56:25 +01:00
|
|
|
ninfo("ipaddr: %08" PRIx32 " ipaddr: %08" PRIx32 " ndx: %d MAX: %d\n",
|
|
|
|
(uint32_t)ipaddr, (uint32_t)CONFIG_NETUTILS_DHCPD_STARTIP, ndx,
|
2011-12-13 18:25:23 +01:00
|
|
|
CONFIG_NETUTILS_DHCPD_MAXLEASES);
|
|
|
|
|
|
|
|
/* Verify that the address offset is within the supported range */
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ndx >= 0 && ndx < CONFIG_NETUTILS_DHCPD_MAXLEASES)
|
|
|
|
{
|
2014-03-04 00:46:56 +01:00
|
|
|
ret = &g_state.ds_leases[ndx];
|
|
|
|
memcpy(ret->mac, mac, DHCP_HLEN_ETHERNET);
|
|
|
|
ret->allocated = true;
|
2011-03-19 22:04:13 +01:00
|
|
|
#ifdef HAVE_LEASE_TIME
|
2014-03-04 00:46:56 +01:00
|
|
|
ret->expiry = dhcpd_time() + expiry;
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
}
|
2011-12-13 18:25:23 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcp_leaseipaddr
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
static inline in_addr_t dhcp_leaseipaddr(FAR struct lease_s *lease)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2011-12-13 18:25:23 +01:00
|
|
|
/* Return IP address in host order */
|
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
return (in_addr_t)(lease - g_state.ds_leases) +
|
|
|
|
CONFIG_NETUTILS_DHCPD_STARTIP;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_findbymac
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
static FAR struct lease_s *dhcpd_findbymac(FAR const uint8_t *mac)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_NETUTILS_DHCPD_MAXLEASES; i++)
|
|
|
|
{
|
|
|
|
if (memcmp(g_state.ds_leases[i].mac, mac, DHCP_HLEN_ETHERNET) == 0)
|
|
|
|
{
|
|
|
|
return &(g_state.ds_leases[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_findbyipaddr
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
static FAR struct lease_s *dhcpd_findbyipaddr(in_addr_t ipaddr)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
if (ipaddr >= CONFIG_NETUTILS_DHCPD_STARTIP &&
|
|
|
|
ipaddr <= CONFIG_NETUTILS_DHCP_OPTION_ENDIP)
|
|
|
|
{
|
2020-01-16 19:01:18 +01:00
|
|
|
FAR struct lease_s *lease =
|
|
|
|
&g_state.ds_leases[ipaddr - CONFIG_NETUTILS_DHCPD_STARTIP];
|
2011-03-19 22:04:13 +01:00
|
|
|
if (lease->allocated > 0)
|
|
|
|
{
|
|
|
|
return lease;
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_allocipaddr
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-03-04 00:46:56 +01:00
|
|
|
static in_addr_t dhcpd_allocipaddr(void)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
struct lease_s *lease = NULL;
|
2020-05-08 04:58:42 +02:00
|
|
|
in_addr_t ipaddr, startaddr;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
ipaddr = startaddr = CONFIG_NETUTILS_DHCPD_STARTIP;
|
2011-03-19 22:04:13 +01:00
|
|
|
for (; ipaddr <= CONFIG_NETUTILS_DHCP_OPTION_ENDIP; ipaddr++)
|
|
|
|
{
|
2014-03-04 00:46:56 +01:00
|
|
|
/* Skip over address ending in 0 or 255 */
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
if ((ipaddr & 0xff) == 0 || (ipaddr & 0xff) == 0xff)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-04 00:46:56 +01:00
|
|
|
/* Is there already a lease on this address? If so, has it expired? */
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
lease = dhcpd_findbyipaddr(ipaddr);
|
|
|
|
if ((!lease || dhcpd_leaseexpired(lease)))
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_CPP_HAVE_WARNING
|
|
|
|
# warning "FIXME: Should check if anything responds to an ARP request or ping"
|
|
|
|
# warning " to verify that there is no other user of this IP address"
|
|
|
|
#endif
|
2020-05-08 04:58:42 +02:00
|
|
|
memset(g_state.ds_leases[ipaddr - startaddr].mac,
|
|
|
|
0, DHCP_HLEN_ETHERNET);
|
|
|
|
g_state.ds_leases[ipaddr - startaddr].allocated = true;
|
2011-03-19 22:04:13 +01:00
|
|
|
#ifdef HAVE_LEASE_TIME
|
2020-05-08 04:58:42 +02:00
|
|
|
g_state.ds_leases[ipaddr - startaddr].expiry =
|
2020-01-16 19:01:18 +01:00
|
|
|
dhcpd_time() + CONFIG_NETUTILS_DHCPD_OFFERTIME;
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
2014-03-04 00:46:56 +01:00
|
|
|
/* Return the address in host order */
|
|
|
|
|
|
|
|
return ipaddr;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_parseoptions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline bool dhcpd_parseoptions(void)
|
|
|
|
{
|
|
|
|
uint32_t tmp;
|
|
|
|
uint8_t *ptr;
|
2014-11-25 22:26:30 +01:00
|
|
|
#ifndef CONFIG_NET_DHCP_LIGHT
|
2011-03-19 22:04:13 +01:00
|
|
|
uint8_t overloaded;
|
|
|
|
uint8_t currfield;
|
2014-11-25 22:26:30 +01:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
int optlen = 0;
|
|
|
|
int remaining;
|
|
|
|
|
|
|
|
/* Verify that the option field starts with a valid magic number */
|
|
|
|
|
|
|
|
ptr = g_state.ds_inpacket.options;
|
|
|
|
if (memcmp(ptr, g_magiccookie, 4) != 0)
|
|
|
|
{
|
|
|
|
/* Bad magic number... skip g_state.ds_outpacket */
|
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Bad magic: %d,%d,%d,%d\n",
|
|
|
|
ptr[0], ptr[1], ptr[2], ptr[3]);
|
2011-03-19 22:04:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up to parse the options */
|
|
|
|
|
2014-03-04 00:46:56 +01:00
|
|
|
ptr += 4;
|
2011-03-19 22:04:13 +01:00
|
|
|
remaining = DHCPD_OPTIONS_SIZE - 4;
|
2014-11-25 22:26:30 +01:00
|
|
|
#ifndef CONFIG_NET_DHCP_LIGHT
|
2011-03-19 22:04:13 +01:00
|
|
|
overloaded = DHCPD_OPTION_FIELD;
|
|
|
|
currfield = DHCPD_OPTION_FIELD;
|
2014-11-25 22:26:30 +01:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Set all options to the default value */
|
|
|
|
|
|
|
|
g_state.ds_optmsgtype = 0; /* Incoming DHCP message type */
|
|
|
|
g_state.ds_optreqip = 0; /* Requested IP address (host order) */
|
|
|
|
g_state.ds_optserverip = 0; /* Serverip IP address (host order) */
|
|
|
|
g_state.ds_optleasetime = 0; /* Requested lease time (host order) */
|
|
|
|
g_state.ds_optend = NULL;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* The form of an option is:
|
|
|
|
* code - 1 byte
|
|
|
|
* length - 1 byte
|
|
|
|
* data - variable number of bytes
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (ptr[DHCPD_OPTION_CODE])
|
|
|
|
{
|
|
|
|
/* Skip over any padding bytes */
|
|
|
|
|
|
|
|
case DHCP_OPTION_PAD:
|
|
|
|
optlen = 1;
|
|
|
|
break;
|
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
/* the Overload option is used to indicate that the DHCP 'sname'
|
|
|
|
* or 'file' fields are being overloaded by using them to carry
|
|
|
|
* DHCP options. A DHCP server inserts this option if the
|
|
|
|
* returned parameters will exceed the usual space allotted for
|
|
|
|
* options.
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2020-01-16 19:01:18 +01:00
|
|
|
* If this option is present, the client interprets the specified
|
2020-05-08 04:58:42 +02:00
|
|
|
* additional fields after it concludes interpretation of the
|
|
|
|
* standard option fields.
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* Legal values for this option are:
|
|
|
|
*
|
|
|
|
* 1 the 'file' field is used to hold options
|
|
|
|
* 2 the 'sname' field is used to hold options
|
|
|
|
* 3 both fields are used to hold options
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_NET_DHCP_LIGHT
|
|
|
|
case DHCP_OPTION_OVERLOAD:
|
|
|
|
optlen = ptr[DHCPD_OPTION_LENGTH] + 2;
|
2020-05-14 14:29:22 +02:00
|
|
|
if (optlen >= 3 && optlen < remaining)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
overloaded = ptr[DHCPD_OPTION_DATA];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case DHCP_OPTION_END:
|
|
|
|
#ifndef CONFIG_NET_DHCP_LIGHT
|
|
|
|
if (currfield == DHCPD_OPTION_FIELD &&
|
|
|
|
(overloaded & DHCPD_FILE_FIELD) != 0)
|
|
|
|
{
|
|
|
|
ptr = g_state.ds_inpacket.file;
|
|
|
|
remaining = sizeof(g_state.ds_inpacket.file);
|
|
|
|
currfield = DHCPD_FILE_FIELD;
|
|
|
|
}
|
|
|
|
else if (currfield == DHCPD_FILE_FIELD &&
|
|
|
|
(overloaded & DHCPD_SNAME_FIELD) != 0)
|
|
|
|
{
|
|
|
|
ptr = g_state.ds_inpacket.sname;
|
|
|
|
remaining = sizeof(g_state.ds_inpacket.sname);
|
|
|
|
currfield = DHCPD_SNAME_FIELD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case DHCP_OPTION_REQ_IPADDR: /* Requested IP Address */
|
|
|
|
optlen = ptr[DHCPD_OPTION_LENGTH] + 2;
|
2020-05-14 14:29:22 +02:00
|
|
|
if (optlen >= 6 && optlen < remaining)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
memcpy(&tmp, &ptr[DHCPD_OPTION_DATA], 4);
|
|
|
|
g_state.ds_optreqip = (in_addr_t)ntohl(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_OPTION_LEASE_TIME: /* IP address lease time */
|
|
|
|
optlen = ptr[DHCPD_OPTION_LENGTH] + 2;
|
2020-05-14 14:29:22 +02:00
|
|
|
if (optlen >= 6 && optlen < remaining)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
memcpy(&tmp, &ptr[DHCPD_OPTION_DATA], 4);
|
|
|
|
g_state.ds_optleasetime = (time_t)ntohl(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_OPTION_MSG_TYPE: /* DHCP message type */
|
|
|
|
optlen = ptr[DHCPD_OPTION_LENGTH] + 2;
|
2020-05-14 14:29:22 +02:00
|
|
|
if (optlen >= 3 && optlen < remaining)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
g_state.ds_optmsgtype = ptr[DHCPD_OPTION_DATA];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCP_OPTION_SERVER_ID: /* Server identifier */
|
|
|
|
optlen = ptr[DHCPD_OPTION_LENGTH] + 2;
|
2020-05-14 14:29:22 +02:00
|
|
|
if (optlen >= 6 && optlen < remaining)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
memcpy(&tmp, &ptr[DHCPD_OPTION_DATA], 4);
|
|
|
|
g_state.ds_optserverip = (in_addr_t)ntohl(tmp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-01-16 19:01:18 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Skip over unsupported options */
|
|
|
|
|
|
|
|
optlen = ptr[DHCPD_OPTION_LENGTH] + 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance to the next option */
|
|
|
|
|
|
|
|
ptr += optlen;
|
|
|
|
remaining -= optlen;
|
|
|
|
}
|
|
|
|
while (remaining > 0);
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_verifyreqip
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline bool dhcpd_verifyreqip(void)
|
|
|
|
{
|
|
|
|
struct lease_s *lease;
|
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
/* Verify that the requested IP address is within the supported lease
|
|
|
|
* range
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2014-11-25 22:26:30 +01:00
|
|
|
if (g_state.ds_optreqip >= CONFIG_NETUTILS_DHCPD_STARTIP &&
|
2011-03-19 22:04:13 +01:00
|
|
|
g_state.ds_optreqip <= CONFIG_NETUTILS_DHCP_OPTION_ENDIP)
|
|
|
|
{
|
|
|
|
/* And verify that the lease has not already been taken or offered
|
|
|
|
* (unless the lease/offer is expired, then the address is free game).
|
|
|
|
*/
|
|
|
|
|
|
|
|
lease = dhcpd_findbyipaddr(g_state.ds_optreqip);
|
|
|
|
if (!lease || dhcpd_leaseexpired(lease))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_verifyreqleasetime
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline bool dhcpd_verifyreqleasetime(uint32_t *leasetime)
|
|
|
|
{
|
|
|
|
uint32_t tmp = g_state.ds_optleasetime;
|
|
|
|
|
|
|
|
/* Did the client request a specific lease time? */
|
|
|
|
|
|
|
|
if (tmp != 0)
|
|
|
|
{
|
2020-05-08 04:58:42 +02:00
|
|
|
/* Yes.. Verify that the requested lease time is within a
|
|
|
|
* valid range
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
if (tmp > CONFIG_NETUTILS_DHCPD_MAXLEASETIME)
|
|
|
|
{
|
|
|
|
tmp = CONFIG_NETUTILS_DHCPD_MAXLEASETIME;
|
|
|
|
}
|
|
|
|
else if (tmp < CONFIG_NETUTILS_DHCPD_MINLEASETIME)
|
|
|
|
{
|
|
|
|
tmp = CONFIG_NETUTILS_DHCPD_MINLEASETIME;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the clipped lease time */
|
|
|
|
|
|
|
|
*leasetime = tmp;
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_addoption
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int dhcpd_addoption(uint8_t *option)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int len = 4;
|
|
|
|
|
|
|
|
if (g_state.ds_optend)
|
|
|
|
{
|
2017-08-25 14:50:37 +02:00
|
|
|
offset = g_state.ds_optend - g_state.ds_outpacket.options;
|
2011-03-19 22:04:13 +01:00
|
|
|
len = option[DHCPD_OPTION_LENGTH] + 2;
|
|
|
|
|
|
|
|
/* Check if the option will fit into the options array */
|
|
|
|
|
|
|
|
if (offset + len + 1 < DHCPD_OPTIONS_SIZE)
|
|
|
|
{
|
|
|
|
/* Copy the option into the option array */
|
|
|
|
|
|
|
|
memcpy(g_state.ds_optend, option, len);
|
|
|
|
g_state.ds_optend += len;
|
|
|
|
*g_state.ds_optend = DHCP_OPTION_END;
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_addoption8
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int dhcpd_addoption8(uint8_t code, uint8_t value)
|
|
|
|
{
|
|
|
|
uint8_t option[3];
|
|
|
|
|
|
|
|
/* Construct the option sequence */
|
|
|
|
|
|
|
|
option[DHCPD_OPTION_CODE] = code;
|
|
|
|
option[DHCPD_OPTION_LENGTH] = 1;
|
|
|
|
option[DHCPD_OPTION_DATA] = value;
|
|
|
|
|
|
|
|
/* Add the option sequence to the response */
|
|
|
|
|
|
|
|
return dhcpd_addoption(option);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_addoption32
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int dhcpd_addoption32(uint8_t code, uint32_t value)
|
|
|
|
{
|
|
|
|
uint8_t option[6];
|
|
|
|
|
|
|
|
/* Construct the option sequence */
|
|
|
|
|
|
|
|
option[DHCPD_OPTION_CODE] = code;
|
|
|
|
option[DHCPD_OPTION_LENGTH] = 4;
|
|
|
|
memcpy(&option[DHCPD_OPTION_DATA], &value, 4);
|
|
|
|
|
|
|
|
/* Add the option sequence to the response */
|
|
|
|
|
|
|
|
return dhcpd_addoption(option);
|
|
|
|
}
|
|
|
|
|
2014-11-18 14:43:34 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcp_addoption32p
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-14 14:25:43 +02:00
|
|
|
#ifdef HAVE_DNSIP
|
2014-11-18 14:43:34 +01:00
|
|
|
static int dhcp_addoption32p(uint8_t code, FAR uint8_t *value)
|
|
|
|
{
|
|
|
|
uint8_t option[6];
|
|
|
|
|
|
|
|
/* Construct the option sequence */
|
|
|
|
|
|
|
|
option[DHCPD_OPTION_CODE] = code;
|
|
|
|
option[DHCPD_OPTION_LENGTH] = 4;
|
|
|
|
memcpy(&option[DHCPD_OPTION_DATA], value, 4);
|
|
|
|
|
|
|
|
/* Add the option sequence to the response */
|
|
|
|
|
|
|
|
return dhcpd_addoption(option);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2020-05-14 14:25:43 +02:00
|
|
|
* Name: dhcpd_socket
|
2011-03-19 22:04:13 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-16 10:11:34 +02:00
|
|
|
static inline int dhcpd_socket(FAR const char *interface)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
int sockfd;
|
|
|
|
#if defined(HAVE_SO_REUSEADDR) || defined(HAVE_SO_BROADCAST)
|
|
|
|
int optval;
|
|
|
|
int ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create a socket to listen for requests from DHCP clients */
|
|
|
|
|
2021-03-18 16:06:01 +01:00
|
|
|
sockfd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: socket failed: %d\n", errno);
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure the socket */
|
|
|
|
|
|
|
|
#ifdef HAVE_SO_REUSEADDR
|
|
|
|
optval = 1;
|
2020-01-16 19:01:18 +01:00
|
|
|
ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (FAR void *)&optval,
|
|
|
|
sizeof(int));
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: setsockopt SO_REUSEADDR failed: %d\n", errno);
|
2011-03-19 22:04:13 +01:00
|
|
|
close(sockfd);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SO_BROADCAST
|
|
|
|
optval = 1;
|
2020-01-16 19:01:18 +01:00
|
|
|
ret = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (FAR void *)&optval,
|
|
|
|
sizeof(int));
|
2011-03-19 22:04:13 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: setsockopt SO_BROADCAST failed: %d\n", errno);
|
2011-03-19 22:04:13 +01:00
|
|
|
close(sockfd);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-06-16 10:11:34 +02:00
|
|
|
#ifdef CONFIG_NET_UDP_BINDTODEVICE
|
|
|
|
/* Bind socket to interface, because UDP packets have to be sent to the
|
|
|
|
* broadcast address at a moment when it is not possible to decide the
|
|
|
|
* target network device using the local or remote address (which is,
|
|
|
|
* by definition and purpose of DHCP, undefined yet).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (setsockopt(sockfd, IPPROTO_UDP, UDP_BINDTODEVICE,
|
|
|
|
interface, strlen(interface)) < 0)
|
|
|
|
{
|
|
|
|
ninfo("ERROR: setsockopt UDP_BINDTODEVICE failed: %d\n", errno);
|
|
|
|
close(sockfd);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_initpacket
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void dhcpd_initpacket(uint8_t mtype)
|
|
|
|
{
|
|
|
|
uint32_t nulladdr = 0;
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Set up the generic parts of the DHCP server message */
|
|
|
|
|
|
|
|
memset(&g_state.ds_outpacket, 0, sizeof(struct dhcpmsg_s));
|
|
|
|
|
|
|
|
g_state.ds_outpacket.op = DHCP_REPLY;
|
|
|
|
g_state.ds_outpacket.htype = g_state.ds_inpacket.htype;
|
|
|
|
g_state.ds_outpacket.hlen = g_state.ds_inpacket.hlen;
|
|
|
|
|
|
|
|
memcpy(&g_state.ds_outpacket.xid, &g_state.ds_inpacket.xid, 4);
|
|
|
|
memcpy(g_state.ds_outpacket.chaddr, g_state.ds_inpacket.chaddr, 16);
|
|
|
|
|
|
|
|
if (memcmp(g_state.ds_outpacket.giaddr, &nulladdr, 4) != 0)
|
|
|
|
{
|
|
|
|
g_state.ds_outpacket.flags = g_state.ds_inpacket.flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the generic options */
|
|
|
|
|
|
|
|
memcpy(g_state.ds_outpacket.options, g_magiccookie, 4);
|
|
|
|
g_state.ds_optend = &g_state.ds_outpacket.options[4];
|
|
|
|
*g_state.ds_optend = DHCP_OPTION_END;
|
|
|
|
dhcpd_addoption8(DHCP_OPTION_MSG_TYPE, mtype);
|
|
|
|
dhcpd_addoption32(DHCP_OPTION_SERVER_ID, g_state.ds_serverip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_sendpacket
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
static int dhcpd_sendpacket(int sockfd, int bbroadcast)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
in_addr_t ipaddr;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_DHCPD_IGNOREBROADCAST
|
|
|
|
/* This is a hack. I've had problems with Windows machines responding
|
|
|
|
* to unicast. I think this is associated with a Windows registry key in
|
2020-05-08 04:58:42 +02:00
|
|
|
* HKEY_LOCAL_MACHINE\
|
|
|
|
* SYSTEM\CurrentControlSet\Services\DHCPServer\Parameters:
|
2011-03-19 22:04:13 +01:00
|
|
|
* The IgnoreBroadcastFlag value controls this behavior: A value of 1 will
|
|
|
|
* cause the server to ignore the client broadcast flag and always respond
|
|
|
|
* with multicast; the value 0 to allows clients to request unicast.
|
|
|
|
*/
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
ipaddr = INADDR_BROADCAST;
|
2011-03-19 22:04:13 +01:00
|
|
|
#else
|
2020-01-16 19:01:18 +01:00
|
|
|
const uint8_t anyipaddr[4] =
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Determine which address to respond to (or if we need to broadcast the
|
|
|
|
* response)
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2020-01-16 19:01:18 +01:00
|
|
|
* (1) If he caller know that it needs to multicast the response, it will
|
|
|
|
* set broadcast.
|
|
|
|
* (2) Otherwise, if the client already has and address (ciaddr), then use
|
|
|
|
* that for unicast
|
2020-05-08 04:58:42 +02:00
|
|
|
* (3) Broadcast if the client says it can't handle uni-cast
|
|
|
|
* (BOOTP_BROADCAST set)
|
2020-01-16 19:01:18 +01:00
|
|
|
* (4) Otherwise, the client claims it can handle the uni-casst response
|
|
|
|
* and we will uni-cast to the offered address (yiaddr).
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2020-01-16 19:01:18 +01:00
|
|
|
* NOTE: We really should also check the giaddr field. If no zero, the
|
|
|
|
* server should send any return messages to the 'DHCP server' port on the
|
|
|
|
* BOOTP relay agent whose address appears in 'giaddr'.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (bbroadcast)
|
|
|
|
{
|
|
|
|
ipaddr = INADDR_BROADCAST;
|
|
|
|
}
|
2020-01-16 19:01:18 +01:00
|
|
|
else if (memcmp(g_state.ds_outpacket.ciaddr, anyipaddr, 4) != 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2020-05-08 04:58:42 +02:00
|
|
|
dhcpd_arpupdate(g_state.ds_outpacket.ciaddr,
|
|
|
|
g_state.ds_outpacket.chaddr);
|
2011-03-19 22:04:13 +01:00
|
|
|
memcpy(&ipaddr, g_state.ds_outpacket.ciaddr, 4);
|
|
|
|
}
|
|
|
|
else if (g_state.ds_outpacket.flags & HTONS(BOOTP_BROADCAST))
|
|
|
|
{
|
|
|
|
ipaddr = INADDR_BROADCAST;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-08 04:58:42 +02:00
|
|
|
dhcpd_arpupdate(g_state.ds_outpacket.yiaddr,
|
|
|
|
g_state.ds_outpacket.chaddr);
|
2011-03-19 22:04:13 +01:00
|
|
|
memcpy(&ipaddr, g_state.ds_outpacket.yiaddr, 4);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create a socket to respond with a packet to the client. We
|
|
|
|
* cannot re-use the listener socket because it is not bound correctly
|
|
|
|
*/
|
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
/* Then send the response to the DHCP client port at that address */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
memset(&addr, 0, sizeof(struct sockaddr_in));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = HTONS(DHCP_CLIENT_PORT);
|
|
|
|
addr.sin_addr.s_addr = ipaddr;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
/* Send the minimum sized packet that includes the END option */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
len = (g_state.ds_optend - (FAR uint8_t *)&g_state.ds_outpacket) + 1;
|
|
|
|
ninfo("sendto %08lx:%04x len=%d\n",
|
|
|
|
(long)ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port), len);
|
2011-12-13 18:25:23 +01:00
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
return sendto(sockfd, &g_state.ds_outpacket, len, 0,
|
|
|
|
(struct sockaddr *)&addr, sizeof(struct sockaddr_in));
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_sendoffer
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
static inline int dhcpd_sendoffer(int sockfd, in_addr_t ipaddr,
|
|
|
|
uint32_t leasetime)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2011-12-13 18:25:23 +01:00
|
|
|
in_addr_t netaddr;
|
2020-05-14 14:25:43 +02:00
|
|
|
#ifdef HAVE_DNSIP
|
2014-11-18 14:20:18 +01:00
|
|
|
uint32_t dnsaddr;
|
|
|
|
dnsaddr = htonl(CONFIG_NETUTILS_DHCPD_DNSIP);
|
|
|
|
#endif
|
2011-12-13 18:25:23 +01:00
|
|
|
/* IP address is in host order */
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Sending offer: %08lx\n", (long)ipaddr);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Initialize the outgoing packet */
|
|
|
|
|
|
|
|
dhcpd_initpacket(DHCPOFFER);
|
|
|
|
|
2011-12-13 18:25:23 +01:00
|
|
|
/* Add the address offered to the client (converting to network order) */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2011-12-13 18:25:23 +01:00
|
|
|
netaddr = htonl(ipaddr);
|
|
|
|
memcpy(g_state.ds_outpacket.yiaddr, &netaddr, 4);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Add the leasetime to the response options */
|
|
|
|
|
|
|
|
dhcpd_addoption32(DHCP_OPTION_LEASE_TIME, htonl(leasetime));
|
2015-09-08 17:20:49 +02:00
|
|
|
#ifdef HAVE_NETMASK
|
2020-05-08 04:58:42 +02:00
|
|
|
dhcpd_addoption32(DHCP_OPTION_SUBNET_MASK,
|
|
|
|
htonl(CONFIG_NETUTILS_DHCPD_NETMASK));
|
2014-11-18 14:20:18 +01:00
|
|
|
#endif
|
2015-09-08 17:20:49 +02:00
|
|
|
#ifdef HAVE_ROUTERIP
|
2020-05-08 04:58:42 +02:00
|
|
|
dhcpd_addoption32(DHCP_OPTION_ROUTER,
|
|
|
|
htonl(CONFIG_NETUTILS_DHCPD_ROUTERIP));
|
2014-11-18 14:20:18 +01:00
|
|
|
#endif
|
2020-05-14 14:25:43 +02:00
|
|
|
#ifdef HAVE_DNSIP
|
2020-01-16 19:01:18 +01:00
|
|
|
dhcp_addoption32p(DHCP_OPTION_DNS_SERVER, (FAR uint8_t *)&dnsaddr);
|
2014-11-18 14:20:18 +01:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Send the offer response */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_DHCPD_IGNOREBROADCAST
|
2021-02-24 16:09:12 +01:00
|
|
|
return dhcpd_sendpacket(sockfd, true);
|
2011-03-19 22:04:13 +01:00
|
|
|
#else
|
2021-02-24 16:09:12 +01:00
|
|
|
return dhcpd_sendpacket(sockfd, false);
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_sendnak
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
static int dhcpd_sendnak(int sockfd)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
/* Initialize and send the NAK response */
|
|
|
|
|
|
|
|
dhcpd_initpacket(DHCPNAK);
|
|
|
|
memcpy(g_state.ds_outpacket.ciaddr, g_state.ds_inpacket.ciaddr, 4);
|
2021-02-24 16:09:12 +01:00
|
|
|
return dhcpd_sendpacket(sockfd, true);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_sendack
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
int dhcpd_sendack(int sockfd, in_addr_t ipaddr)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
uint32_t leasetime = CONFIG_NETUTILS_DHCPD_LEASETIME;
|
2011-12-13 18:25:23 +01:00
|
|
|
in_addr_t netaddr;
|
2020-05-14 14:25:43 +02:00
|
|
|
#ifdef HAVE_DNSIP
|
2014-11-18 14:20:18 +01:00
|
|
|
uint32_t dnsaddr;
|
|
|
|
dnsaddr = htonl(CONFIG_NETUTILS_DHCPD_DNSIP);
|
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Initialize the ACK response */
|
|
|
|
|
|
|
|
dhcpd_initpacket(DHCPACK);
|
|
|
|
memcpy(g_state.ds_outpacket.ciaddr, g_state.ds_inpacket.ciaddr, 4);
|
|
|
|
|
|
|
|
/* Add the IP address assigned to the client */
|
|
|
|
|
2011-12-13 18:25:23 +01:00
|
|
|
netaddr = htonl(ipaddr);
|
|
|
|
memcpy(g_state.ds_outpacket.yiaddr, &netaddr, 4);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Did the client request a specific lease time? */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
dhcpd_verifyreqleasetime(&leasetime);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Add the lease time to the response */
|
|
|
|
|
|
|
|
dhcpd_addoption32(DHCP_OPTION_LEASE_TIME, htonl(leasetime));
|
2015-09-08 17:20:49 +02:00
|
|
|
#ifdef HAVE_NETMASK
|
2020-05-08 04:58:42 +02:00
|
|
|
dhcpd_addoption32(DHCP_OPTION_SUBNET_MASK,
|
|
|
|
htonl(CONFIG_NETUTILS_DHCPD_NETMASK));
|
2014-11-18 14:20:18 +01:00
|
|
|
#endif
|
2015-09-08 17:20:49 +02:00
|
|
|
#ifdef HAVE_ROUTERIP
|
2020-05-08 04:58:42 +02:00
|
|
|
dhcpd_addoption32(DHCP_OPTION_ROUTER,
|
|
|
|
htonl(CONFIG_NETUTILS_DHCPD_ROUTERIP));
|
2014-11-18 14:20:18 +01:00
|
|
|
#endif
|
2020-05-14 14:25:43 +02:00
|
|
|
#ifdef HAVE_DNSIP
|
2020-01-16 19:01:18 +01:00
|
|
|
dhcp_addoption32p(DHCP_OPTION_DNS_SERVER, (FAR uint8_t *)&dnsaddr);
|
2014-11-18 14:20:18 +01:00
|
|
|
#endif
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_DHCPD_IGNOREBROADCAST
|
2021-02-24 16:09:12 +01:00
|
|
|
if (dhcpd_sendpacket(sockfd, true) < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
#else
|
2021-02-24 16:09:12 +01:00
|
|
|
if (dhcpd_sendpacket(sockfd, false) < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
dhcpd_setlease(g_state.ds_inpacket.chaddr, ipaddr, leasetime);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_discover
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
static inline int dhcpd_discover(int sockfd)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
struct lease_s *lease;
|
|
|
|
in_addr_t ipaddr;
|
|
|
|
uint32_t leasetime = CONFIG_NETUTILS_DHCPD_LEASETIME;
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Check if the client is already in the lease table */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
lease = dhcpd_findbymac(g_state.ds_inpacket.chaddr);
|
|
|
|
if (lease)
|
|
|
|
{
|
|
|
|
/* Yes... get the remaining time on the lease */
|
|
|
|
|
|
|
|
#ifdef HAVE_LEASE_TIME
|
|
|
|
if (!dhcpd_leaseexpired(lease))
|
|
|
|
{
|
|
|
|
leasetime = lease->expiry - dhcpd_time();
|
|
|
|
if (leasetime < CONFIG_NETUTILS_DHCPD_MINLEASETIME)
|
|
|
|
{
|
|
|
|
leasetime = CONFIG_NETUTILS_DHCPD_MINLEASETIME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-01-16 19:01:18 +01:00
|
|
|
|
2011-12-13 18:25:23 +01:00
|
|
|
/* Get the IP address associated with the lease (host order) */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
ipaddr = dhcp_leaseipaddr(lease);
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Already have lease for IP %08lx\n", (long)ipaddr);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the client has requested a specific IP address */
|
|
|
|
|
|
|
|
else if (dhcpd_verifyreqip())
|
|
|
|
{
|
2011-12-13 18:25:23 +01:00
|
|
|
/* Use the requested IP address (host order) */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
ipaddr = g_state.ds_optreqip;
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("User requested IP %08lx\n", (long)ipaddr);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-08 04:58:42 +02:00
|
|
|
/* No... allocate a new IP address (host order) */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
ipaddr = dhcpd_allocipaddr();
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Allocated IP %08lx\n", (long)ipaddr);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Did we get any IP address? */
|
|
|
|
|
|
|
|
if (!ipaddr)
|
|
|
|
{
|
|
|
|
/* Nope... return failure */
|
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to get IP address\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reserve the leased IP for a shorter time for the offer */
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
if (!dhcpd_setlease(g_state.ds_inpacket.chaddr, ipaddr,
|
|
|
|
CONFIG_NETUTILS_DHCPD_OFFERTIME))
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to set lease\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the client has requested a specific lease time */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
dhcpd_verifyreqleasetime(&leasetime);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Send the offer response */
|
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
return dhcpd_sendoffer(sockfd, ipaddr, leasetime);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_request
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-02-24 16:09:12 +01:00
|
|
|
static inline int dhcpd_request(int sockfd)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
struct lease_s *lease;
|
2011-12-13 18:25:23 +01:00
|
|
|
in_addr_t ipaddr = 0;
|
2011-03-19 22:04:13 +01:00
|
|
|
uint8_t response = 0;
|
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
/* Check if this client already holds a lease. This can happen when the
|
|
|
|
* client (1) the IP is reserved for the client from a previous offer,
|
|
|
|
* or (2) the client is re-initializing or rebooting while the lease is
|
|
|
|
* still valid.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
lease = dhcpd_findbymac(g_state.ds_inpacket.chaddr);
|
|
|
|
if (lease)
|
|
|
|
{
|
2020-01-16 19:01:18 +01:00
|
|
|
/* Yes.. the client already holds a lease. Verify that the request is
|
|
|
|
* consistent* with the existing lease (host order).
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
ipaddr = dhcp_leaseipaddr(lease);
|
2020-11-26 03:56:25 +01:00
|
|
|
ninfo("Lease ipaddr: %08" PRIx32 " Server IP: %08" PRIx32
|
|
|
|
" Requested IP: %08" PRIx32 "\n",
|
|
|
|
(uint32_t)ipaddr,
|
|
|
|
(uint32_t)g_state.ds_optserverip,
|
|
|
|
(uint32_t)g_state.ds_optreqip);
|
2011-12-13 18:25:23 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
if (g_state.ds_optserverip)
|
|
|
|
{
|
2020-01-16 19:01:18 +01:00
|
|
|
/* ACK if the serverip is correct and the requested IP address is
|
|
|
|
* the one already offered to the client.
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_state.ds_optserverip == ntohl(g_state.ds_serverip) &&
|
|
|
|
(g_state.ds_optreqip != 0 || g_state.ds_optreqip == ipaddr))
|
|
|
|
{
|
|
|
|
response = DHCPACK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response = DHCPNAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
/* We have the lease and no server IP was requested. Was a specific IP
|
|
|
|
* address requested? (host order)
|
2011-03-19 22:04:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
else if (g_state.ds_optreqip)
|
|
|
|
{
|
2011-12-13 18:25:23 +01:00
|
|
|
/* Yes..ACK if the requested IP address is the one already leased.
|
|
|
|
* Both addresses are in host order.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
if (ipaddr == g_state.ds_optreqip)
|
|
|
|
{
|
|
|
|
response = DHCPACK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response = DHCPNAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
/* The client has specified neither a server IP nor requested
|
|
|
|
* IP address
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-05-08 04:58:42 +02:00
|
|
|
/* ACK if the IP used by the client is the one already assigned
|
|
|
|
* to it. NOTE ipaddr is in host order; ciaddr is network order!
|
2011-12-13 18:25:23 +01:00
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2011-12-13 18:25:23 +01:00
|
|
|
uint32_t tmp = htonl(ipaddr);
|
|
|
|
if (memcmp(&tmp, g_state.ds_inpacket.ciaddr, 4) == 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
response = DHCPACK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response = DHCPNAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The client does not hold a lease (referenced by its MAC address) and is
|
|
|
|
* requesting a specific IP address that was, apparently, never offered to
|
|
|
|
* to the client. Perform some sanity checks before sending the NAK.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (g_state.ds_optreqip && !g_state.ds_optserverip)
|
|
|
|
{
|
2020-11-26 03:56:25 +01:00
|
|
|
ninfo("Server IP: %08" PRIx32 " Requested IP: %08" PRIx32 "\n",
|
|
|
|
(uint32_t)g_state.ds_optserverip,
|
|
|
|
(uint32_t)g_state.ds_optreqip);
|
2011-12-13 18:25:23 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Is this IP address already assigned? */
|
|
|
|
|
|
|
|
lease = dhcpd_findbyipaddr(g_state.ds_optreqip);
|
|
|
|
if (lease)
|
|
|
|
{
|
|
|
|
/* Yes.. Send NAK unless the lease has expired */
|
|
|
|
|
|
|
|
if (!dhcpd_leaseexpired(lease))
|
|
|
|
{
|
|
|
|
response = DHCPNAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
/* DHCPREQUEST without DHCPDISCOVER:
|
|
|
|
* The request IP address is in the range but has not been leased,
|
|
|
|
* maybe requested before the last shutdown, lease again.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
else if (g_state.ds_optreqip >= CONFIG_NETUTILS_DHCPD_STARTIP &&
|
|
|
|
g_state.ds_optreqip <= CONFIG_NETUTILS_DHCP_OPTION_ENDIP)
|
|
|
|
{
|
|
|
|
ipaddr = g_state.ds_optreqip;
|
|
|
|
response = DHCPACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The requested IP address out of range, negative */
|
|
|
|
|
|
|
|
else
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
response = DHCPNAK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-13 18:25:23 +01:00
|
|
|
/* Otherwise, the client does not hold a lease and is not requesting any
|
|
|
|
* specific IP address.
|
|
|
|
*/
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Finally, either (1) send the ACK, (2) send a NAK, or (3) remain silent
|
|
|
|
* based on the checks above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (response == DHCPACK)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("ACK IP %08lx\n", (long)ipaddr);
|
2021-02-24 16:09:12 +01:00
|
|
|
dhcpd_sendack(sockfd, ipaddr);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
else if (response == DHCPNAK)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("NAK IP %08lx\n", (long)ipaddr);
|
2021-02-24 16:09:12 +01:00
|
|
|
dhcpd_sendnak(sockfd);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2011-12-13 18:25:23 +01:00
|
|
|
else
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Remaining silent IP %08lx\n", (long)ipaddr);
|
2011-12-13 18:25:23 +01:00
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_decline
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int dhcpd_decline(void)
|
|
|
|
{
|
|
|
|
struct lease_s *lease;
|
|
|
|
|
|
|
|
/* Find the lease associated with this hardware address */
|
|
|
|
|
|
|
|
lease = dhcpd_findbymac(g_state.ds_inpacket.chaddr);
|
|
|
|
if (lease)
|
|
|
|
{
|
2020-01-16 19:01:18 +01:00
|
|
|
/* Disassociate the IP from the MAC, but prevent re-used of this
|
|
|
|
* address for a period of time.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
memset(lease->mac, 0, DHCP_HLEN_ETHERNET);
|
2011-03-19 22:04:13 +01:00
|
|
|
#ifdef HAVE_LEASE_TIME
|
2020-01-16 19:01:18 +01:00
|
|
|
lease->expiry = dhcpd_time() + CONFIG_NETUTILS_DHCPD_DECLINETIME;
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
2020-01-16 19:01:18 +01:00
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int dhcpd_release(void)
|
|
|
|
{
|
|
|
|
struct lease_s *lease;
|
|
|
|
|
|
|
|
/* Find the lease associated with this hardware address */
|
|
|
|
|
|
|
|
lease = dhcpd_findbymac(g_state.ds_inpacket.chaddr);
|
|
|
|
if (lease)
|
|
|
|
{
|
|
|
|
/* Release the IP address now */
|
|
|
|
|
|
|
|
memset(lease, 0, sizeof(struct lease_s));
|
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_openlistener
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
static inline int dhcpd_openlistener(FAR const char *interface)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct ifreq req;
|
|
|
|
int sockfd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Create a socket to listen for requests from DHCP clients */
|
|
|
|
|
2021-06-16 10:11:34 +02:00
|
|
|
sockfd = dhcpd_socket(interface);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: socket failed: %d\n", errno);
|
2011-03-19 22:04:13 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the IP address of the selected device */
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
strncpy(req.ifr_name, interface, IFNAMSIZ);
|
2011-03-19 22:04:13 +01:00
|
|
|
ret = ioctl(sockfd, SIOCGIFADDR, (unsigned long)&req);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: setsockopt SIOCGIFADDR failed: %d\n", errno);
|
2011-03-19 22:04:13 +01:00
|
|
|
close(sockfd);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
g_state.ds_serverip = ((FAR struct sockaddr_in *)
|
|
|
|
&req.ifr_addr)->sin_addr.s_addr;
|
|
|
|
|
2021-03-05 09:33:42 +01:00
|
|
|
ninfo("serverip: %08" PRIx32 "\n", ntohl(g_state.ds_serverip));
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Bind the socket to a local port. We have to bind to INADDRY_ANY to
|
|
|
|
* receive broadcast messages.
|
|
|
|
*/
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(DHCP_SERVER_PORT);
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
|
|
|
|
if (ret < 0)
|
2014-03-04 00:46:56 +01:00
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: bind failed, port=%d addr=%08lx: %d\n",
|
2011-03-19 22:04:13 +01:00
|
|
|
addr.sin_port, (long)addr.sin_addr.s_addr, errno);
|
2014-03-04 00:46:56 +01:00
|
|
|
close(sockfd);
|
|
|
|
return ERROR;
|
|
|
|
}
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:30:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_task_run
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int dhcpd_task_run(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return dhcpd_run(argv[1]);
|
|
|
|
}
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_run
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
int dhcpd_run(FAR const char *interface)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
int sockfd;
|
|
|
|
int nbytes;
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Started\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2020-05-08 08:30:04 +02:00
|
|
|
/* Exit if the dhcpd is already executed */
|
|
|
|
|
|
|
|
if (g_dhcpd_daemon.ds_state == DHCPD_RUNNING)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the pid if running in daemon mode */
|
|
|
|
|
|
|
|
g_dhcpd_daemon.ds_pid = getpid();
|
|
|
|
|
|
|
|
/* Indicate that we have started */
|
|
|
|
|
|
|
|
g_dhcpd_daemon.ds_state = DHCPD_RUNNING;
|
|
|
|
|
|
|
|
sem_post(&g_dhcpd_daemon.ds_sync);
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
/* Initialize everything to zero */
|
|
|
|
|
|
|
|
memset(&g_state, 0, sizeof(struct dhcpd_state_s));
|
|
|
|
|
|
|
|
/* Now loop indefinitely, reading packets from the DHCP server socket */
|
|
|
|
|
|
|
|
sockfd = -1;
|
2020-05-08 08:30:04 +02:00
|
|
|
while (g_dhcpd_daemon.ds_state != DHCPD_STOP_REQUESTED)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
/* Create a socket to listen for requests from DHCP clients */
|
|
|
|
|
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
2020-01-16 19:01:18 +01:00
|
|
|
sockfd = dhcpd_openlistener(interface);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Failed to create socket\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the next g_state.ds_outpacket */
|
|
|
|
|
2020-05-08 04:58:42 +02:00
|
|
|
nbytes = recv(sockfd, &g_state.ds_inpacket,
|
|
|
|
sizeof(struct dhcpmsg_s), 0);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
/* On errors (other EINTR), close the socket and try again */
|
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: recv failed: %d\n", errno);
|
2020-05-08 08:30:04 +02:00
|
|
|
if (errno != EINTR && errno != EAGAIN)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
close(sockfd);
|
|
|
|
sockfd = -1;
|
|
|
|
}
|
2020-01-16 19:01:18 +01:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the incoming message options */
|
|
|
|
|
|
|
|
if (!dhcpd_parseoptions())
|
|
|
|
{
|
|
|
|
/* Failed to parse the message options */
|
|
|
|
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: No msg type\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_DHCPD_HOST
|
|
|
|
/* Get the poor little uC a change to get its recvfrom in place */
|
|
|
|
|
2020-01-16 19:01:18 +01:00
|
|
|
usleep(500 * 1000);
|
2011-03-19 22:04:13 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now process the incoming DHCP message by its message type */
|
|
|
|
|
|
|
|
switch (g_state.ds_optmsgtype)
|
|
|
|
{
|
|
|
|
case DHCPDISCOVER:
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("DHCPDISCOVER\n");
|
2021-02-24 16:09:12 +01:00
|
|
|
dhcpd_discover(sockfd);
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPREQUEST:
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("DHCPREQUEST\n");
|
2021-02-24 16:09:12 +01:00
|
|
|
dhcpd_request(sockfd);
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPDECLINE:
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("DHCPDECLINE\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
dhcpd_decline();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPRELEASE:
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("DHCPRELEASE\n");
|
2011-03-19 22:04:13 +01:00
|
|
|
dhcpd_release();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DHCPINFORM: /* Not supported */
|
|
|
|
default:
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: Unsupported message type: %d\n",
|
|
|
|
g_state.ds_optmsgtype);
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 00:46:56 +01:00
|
|
|
|
2020-05-08 08:30:04 +02:00
|
|
|
g_dhcpd_daemon.ds_pid = -1;
|
|
|
|
g_dhcpd_daemon.ds_state = DHCPD_STOPPED;
|
|
|
|
sem_post(&g_dhcpd_daemon.ds_sync);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_start
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start the DHCPD daemon
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, the non-negative task ID of the DHCPDC daemon is returned;
|
|
|
|
* On failure, a negated errno value is returned.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int dhcpd_start(FAR const char *interface)
|
|
|
|
{
|
|
|
|
FAR char *argv[2];
|
|
|
|
int pid;
|
|
|
|
|
|
|
|
argv[0] = (char *)interface;
|
|
|
|
argv[1] = NULL;
|
|
|
|
|
|
|
|
/* Is the DHCPD in a non-running state? */
|
|
|
|
|
|
|
|
sem_wait(&g_dhcpd_daemon.ds_lock);
|
|
|
|
if (g_dhcpd_daemon.ds_state == DHCPD_NOT_RUNNING ||
|
|
|
|
g_dhcpd_daemon.ds_state == DHCPD_STOPPED)
|
|
|
|
{
|
|
|
|
/* Start the DHCPD daemon */
|
|
|
|
|
|
|
|
g_dhcpd_daemon.ds_state = DHCPD_STARTED;
|
|
|
|
pid =
|
|
|
|
task_create("DHCPD daemon", CONFIG_NETUTILS_DHCPD_PRIORITY,
|
|
|
|
CONFIG_NETUTILS_DHCPD_STACKSIZE, dhcpd_task_run,
|
|
|
|
argv);
|
|
|
|
|
|
|
|
/* Handle failures to start the DHCPD daemon */
|
|
|
|
|
|
|
|
if (pid < 0)
|
|
|
|
{
|
|
|
|
int errval = errno;
|
|
|
|
|
|
|
|
g_dhcpd_daemon.ds_state = DHCPD_STOPPED;
|
2020-11-26 03:56:25 +01:00
|
|
|
nerr("ERROR: Failed to start the DHCPD daemon: %d\n", errval);
|
2020-05-08 08:30:04 +02:00
|
|
|
sem_post(&g_dhcpd_daemon.ds_lock);
|
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for any daemon state change */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sem_wait(&g_dhcpd_daemon.ds_sync);
|
|
|
|
}
|
|
|
|
while (g_dhcpd_daemon.ds_state == DHCPD_STARTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
sem_post(&g_dhcpd_daemon.ds_lock);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dhcpd_stop
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stop the DHCPD daemon
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero on success; a negated errno value on failure. The current
|
|
|
|
* implementation only returns success.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int dhcpd_stop(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Is the DHCPD in a running state? */
|
|
|
|
|
|
|
|
sem_wait(&g_dhcpd_daemon.ds_lock);
|
|
|
|
if (g_dhcpd_daemon.ds_state == DHCPD_STARTED ||
|
|
|
|
g_dhcpd_daemon.ds_state == DHCPD_RUNNING)
|
|
|
|
{
|
|
|
|
/* Yes.. request that the daemon stop. */
|
|
|
|
|
|
|
|
g_dhcpd_daemon.ds_state = DHCPD_STOP_REQUESTED;
|
|
|
|
|
|
|
|
/* Wait for any daemon state change */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Signal the DHCPD client */
|
|
|
|
|
|
|
|
ret = kill(g_dhcpd_daemon.ds_pid,
|
|
|
|
CONFIG_NETUTILS_DHCPD_SIGWAKEUP);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nerr("ERROR: kill pid %d failed: %d\n",
|
2020-08-07 15:56:56 +02:00
|
|
|
g_dhcpd_daemon.ds_pid, errno);
|
2020-05-08 08:30:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the DHCPD client to respond to the stop request */
|
|
|
|
|
|
|
|
sem_wait(&g_dhcpd_daemon.ds_sync);
|
|
|
|
}
|
|
|
|
while (g_dhcpd_daemon.ds_state == DHCPD_STOP_REQUESTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
sem_post(&g_dhcpd_daemon.ds_lock);
|
2011-03-19 22:04:13 +01:00
|
|
|
return OK;
|
|
|
|
}
|