2010-11-13 06:13:16 +05:30
|
|
|
/* ndhc.c
|
2010-11-12 14:32:18 +05:30
|
|
|
*
|
2010-11-13 06:13:16 +05:30
|
|
|
* ndhc DHCP client, originally based on udhcpc
|
2010-11-12 14:32:18 +05:30
|
|
|
*
|
2010-11-13 01:03:17 +05:30
|
|
|
* Nicholas J. Kain <njkain at gmail dot com> 2004-2010
|
2010-11-12 14:32:18 +05:30
|
|
|
* Russ Dill <Russ.Dill@asu.edu> July 2001
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2010-11-13 01:03:17 +05:30
|
|
|
|
2010-11-12 14:32:18 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2010-12-01 23:41:09 +05:30
|
|
|
#include <sys/epoll.h>
|
2010-12-01 23:05:13 +05:30
|
|
|
#include <sys/signalfd.h>
|
2010-11-12 14:32:18 +05:30
|
|
|
#include <net/if.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
|
2010-11-13 05:14:49 +05:30
|
|
|
#include "ndhc-defines.h"
|
2010-11-12 14:32:18 +05:30
|
|
|
#include "dhcpd.h"
|
|
|
|
#include "dhcpc.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "clientpacket.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "script.h"
|
|
|
|
#include "socket.h"
|
2010-11-16 06:36:50 +05:30
|
|
|
#include "arpping.h"
|
2010-11-12 14:32:18 +05:30
|
|
|
#include "log.h"
|
2010-11-12 16:12:07 +05:30
|
|
|
#include "chroot.h"
|
2010-11-12 19:34:43 +05:30
|
|
|
#include "cap.h"
|
2010-11-12 16:12:07 +05:30
|
|
|
#include "strl.h"
|
2010-11-13 05:14:49 +05:30
|
|
|
#include "pidfile.h"
|
2010-11-12 22:49:52 +05:30
|
|
|
#include "malloc.h"
|
2010-11-12 14:32:18 +05:30
|
|
|
|
2010-11-12 15:14:25 +05:30
|
|
|
#define VERSION "1.0"
|
2010-11-13 04:49:19 +05:30
|
|
|
#define NUMPACKETS 3 /* number of packets to send before delay */
|
|
|
|
#define RETRY_DELAY 30 /* time in seconds to delay after sending NUMPACKETS */
|
2010-11-12 15:14:25 +05:30
|
|
|
|
2010-12-01 23:41:09 +05:30
|
|
|
static int epollfd, signalFd;
|
|
|
|
static struct epoll_event events[3];
|
2010-12-01 23:05:13 +05:30
|
|
|
|
2010-11-13 06:33:55 +05:30
|
|
|
static char pidfile[MAX_PATH_LENGTH] = PID_FILE_DEFAULT;
|
|
|
|
|
2010-11-13 17:12:52 +05:30
|
|
|
static uint32_t requested_ip, server_addr, timeout;
|
|
|
|
static uint32_t lease, t1, t2, xid, start;
|
2010-12-01 23:41:09 +05:30
|
|
|
static int state, packet_num, listenFd, listen_mode;
|
2010-11-12 14:32:18 +05:30
|
|
|
|
|
|
|
enum {
|
2010-11-13 01:03:17 +05:30
|
|
|
LISTEN_NONE,
|
|
|
|
LISTEN_KERNEL,
|
|
|
|
LISTEN_RAW
|
2010-11-12 14:32:18 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
struct client_config_t client_config = {
|
2010-11-13 01:03:17 +05:30
|
|
|
/* Default options. */
|
|
|
|
.abort_if_no_lease = 0,
|
|
|
|
.foreground = 0,
|
|
|
|
.quit_after_lease = 0,
|
|
|
|
.background_if_no_lease = 0,
|
|
|
|
.interface = "eth0",
|
|
|
|
.script = "none",
|
|
|
|
.clientid = NULL,
|
|
|
|
.hostname = NULL,
|
|
|
|
.ifindex = 0,
|
|
|
|
.arp = "\0",
|
2010-11-12 14:32:18 +05:30
|
|
|
};
|
|
|
|
|
2010-12-01 23:41:09 +05:30
|
|
|
static void epoll_add(int fd)
|
|
|
|
{
|
|
|
|
struct epoll_event ev;
|
|
|
|
int r;
|
|
|
|
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
|
|
|
|
ev.data.fd = fd;
|
|
|
|
r = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev);
|
|
|
|
if (r == -1)
|
|
|
|
suicide("epoll_add failed %s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void epoll_del(int fd)
|
|
|
|
{
|
|
|
|
struct epoll_event ev;
|
|
|
|
int r;
|
|
|
|
ev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP;
|
|
|
|
ev.data.fd = fd;
|
|
|
|
r = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev);
|
|
|
|
if (r == -1)
|
|
|
|
suicide("epoll_del failed %s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
2010-11-12 14:32:18 +05:30
|
|
|
static void show_usage(void)
|
|
|
|
{
|
2010-11-13 01:03:17 +05:30
|
|
|
printf(
|
2010-11-12 14:32:18 +05:30
|
|
|
"Usage: ndhc [OPTIONS]\n\n"
|
|
|
|
" -c, --clientid=CLIENTID Client identifier\n"
|
|
|
|
" -H, --hostname=HOSTNAME Client hostname\n"
|
|
|
|
" -h Alias for -H\n"
|
|
|
|
" -f, --foreground Do not fork after getting lease\n"
|
|
|
|
" -b, --background Fork to background if lease cannot be\n"
|
|
|
|
" immediately negotiated.\n"
|
2010-11-13 06:13:16 +05:30
|
|
|
" -p, --pidfile File to which the pid will be written\n"
|
2010-11-12 14:32:18 +05:30
|
|
|
" -i, --interface=INTERFACE Interface to use (default: eth0)\n"
|
|
|
|
" -n, --now Exit with failure if lease cannot be\n"
|
|
|
|
" immediately negotiated.\n"
|
|
|
|
" -q, --quit Quit after obtaining lease\n"
|
|
|
|
" -r, --request=IP IP address to request (default: none)\n"
|
|
|
|
" -u, --user Change privileges to this user\n"
|
|
|
|
" -C, --chroot Directory to which udhcp should chroot\n"
|
|
|
|
" -v, --version Display version\n"
|
2010-11-13 01:03:17 +05:30
|
|
|
);
|
|
|
|
exit(EXIT_SUCCESS);
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
2010-11-13 23:00:54 +05:30
|
|
|
/* Switch listen socket between raw (if-bound), kernel (ip-bound), and none */
|
|
|
|
static void change_listen_mode(int new_mode)
|
2010-11-12 14:32:18 +05:30
|
|
|
{
|
2010-11-13 01:03:17 +05:30
|
|
|
log_line("entering %s listen mode",
|
|
|
|
new_mode ? (new_mode == 1 ? "kernel" : "raw") : "none");
|
|
|
|
listen_mode = new_mode;
|
2010-12-01 23:41:09 +05:30
|
|
|
if (listenFd >= 0) {
|
|
|
|
epoll_del(listenFd);
|
|
|
|
close(listenFd);
|
|
|
|
listenFd = -1;
|
|
|
|
}
|
|
|
|
if (new_mode == LISTEN_KERNEL) {
|
|
|
|
listenFd = listen_socket(INADDR_ANY, CLIENT_PORT,
|
|
|
|
client_config.interface);
|
|
|
|
epoll_add(listenFd);
|
|
|
|
}
|
|
|
|
else if (new_mode == LISTEN_RAW) {
|
|
|
|
listenFd = raw_socket(client_config.ifindex);
|
|
|
|
epoll_add(listenFd);
|
2010-11-13 23:00:54 +05:30
|
|
|
}
|
2010-11-13 23:04:22 +05:30
|
|
|
else /* LISTEN_NONE */
|
|
|
|
return;
|
2010-12-01 23:41:09 +05:30
|
|
|
if (listenFd < 0) {
|
2010-11-13 23:04:22 +05:30
|
|
|
log_error("FATAL: couldn't listen on socket: %s.", strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* perform a renew */
|
|
|
|
static void perform_renew(void)
|
|
|
|
{
|
2010-11-13 01:03:17 +05:30
|
|
|
log_line("Performing a DHCP renew...");
|
|
|
|
switch (state) {
|
|
|
|
case BOUND:
|
2010-11-13 23:00:54 +05:30
|
|
|
change_listen_mode(LISTEN_KERNEL);
|
2010-11-13 01:03:17 +05:30
|
|
|
case RENEWING:
|
|
|
|
case REBINDING:
|
|
|
|
state = RENEW_REQUESTED;
|
|
|
|
break;
|
|
|
|
case RENEW_REQUESTED: /* impatient are we? fine, square 1 */
|
|
|
|
run_script(NULL, SCRIPT_DECONFIG);
|
|
|
|
case REQUESTING:
|
|
|
|
case RELEASED:
|
2010-11-13 23:00:54 +05:30
|
|
|
change_listen_mode(LISTEN_RAW);
|
2010-11-13 01:03:17 +05:30
|
|
|
state = INIT_SELECTING;
|
|
|
|
break;
|
|
|
|
case INIT_SELECTING:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start things over */
|
|
|
|
packet_num = 0;
|
|
|
|
|
|
|
|
/* Kill any timeouts because the user wants this to hurry along */
|
|
|
|
timeout = 0;
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* perform a release */
|
|
|
|
static void perform_release(void)
|
|
|
|
{
|
2010-11-14 05:19:46 +05:30
|
|
|
struct in_addr temp_saddr, temp_raddr;
|
2010-11-13 01:03:17 +05:30
|
|
|
|
|
|
|
/* send release packet */
|
|
|
|
if (state == BOUND || state == RENEWING || state == REBINDING) {
|
2010-11-14 05:19:46 +05:30
|
|
|
temp_saddr.s_addr = server_addr;
|
|
|
|
temp_raddr.s_addr = requested_ip;
|
2010-11-13 01:03:17 +05:30
|
|
|
log_line("Unicasting a release of %s to %s.",
|
2010-11-14 05:19:46 +05:30
|
|
|
inet_ntoa(temp_raddr), inet_ntoa(temp_saddr));
|
2010-11-13 01:03:17 +05:30
|
|
|
send_release(server_addr, requested_ip); /* unicast */
|
|
|
|
run_script(NULL, SCRIPT_DECONFIG);
|
|
|
|
}
|
|
|
|
log_line("Entering released state.");
|
|
|
|
|
2010-11-13 23:00:54 +05:30
|
|
|
change_listen_mode(LISTEN_NONE);
|
2010-11-13 01:03:17 +05:30
|
|
|
state = RELEASED;
|
|
|
|
timeout = 0x7fffffff;
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void background(void)
|
|
|
|
{
|
2010-11-13 06:20:04 +05:30
|
|
|
static char called;
|
|
|
|
if (!called && daemon(0, 0) == -1) {
|
2010-11-13 01:03:17 +05:30
|
|
|
perror("fork");
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2010-11-13 06:20:04 +05:30
|
|
|
called = 1; /* Do not fork again. */
|
2010-11-13 06:33:55 +05:30
|
|
|
if (file_exists(pidfile, "w") == -1) {
|
|
|
|
log_line("FATAL - cannot open pidfile for write!");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
write_pid(pidfile);
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
2010-11-14 06:07:08 +05:30
|
|
|
/* Handle select timeout dropping to zero */
|
2010-11-12 14:32:18 +05:30
|
|
|
static void handle_timeout(void)
|
|
|
|
{
|
2010-11-13 01:03:17 +05:30
|
|
|
time_t now = time(0);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case INIT_SELECTING:
|
2010-11-13 04:49:19 +05:30
|
|
|
if (packet_num < NUMPACKETS) {
|
2010-11-13 01:03:17 +05:30
|
|
|
if (packet_num == 0)
|
|
|
|
xid = random_xid();
|
2010-11-14 06:07:08 +05:30
|
|
|
/* broadcast */
|
|
|
|
send_discover(xid, requested_ip);
|
2010-11-13 01:03:17 +05:30
|
|
|
|
2010-11-13 04:49:19 +05:30
|
|
|
timeout = now + ((packet_num == NUMPACKETS - 1) ? 4 : 2);
|
2010-11-13 01:03:17 +05:30
|
|
|
packet_num++;
|
|
|
|
} else {
|
|
|
|
if (client_config.background_if_no_lease) {
|
|
|
|
log_line("No lease, going to background.");
|
|
|
|
background();
|
|
|
|
} else if (client_config.abort_if_no_lease) {
|
|
|
|
log_line("No lease, failing.");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
/* wait to try again */
|
|
|
|
packet_num = 0;
|
2010-11-13 04:49:19 +05:30
|
|
|
timeout = now + RETRY_DELAY;
|
2010-11-13 01:03:17 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RENEW_REQUESTED:
|
|
|
|
case REQUESTING:
|
2010-11-13 04:49:19 +05:30
|
|
|
if (packet_num < NUMPACKETS) {
|
2010-11-13 01:03:17 +05:30
|
|
|
/* send request packet */
|
|
|
|
if (state == RENEW_REQUESTED)
|
|
|
|
/* unicast */
|
|
|
|
send_renew(xid, server_addr, requested_ip);
|
|
|
|
else
|
|
|
|
/* broadcast */
|
|
|
|
send_selecting(xid, server_addr, requested_ip);
|
2010-11-13 04:49:19 +05:30
|
|
|
timeout = now + ((packet_num == NUMPACKETS - 1) ? 10 : 2);
|
2010-11-13 01:03:17 +05:30
|
|
|
packet_num++;
|
|
|
|
} else {
|
|
|
|
/* timed out, go back to init state */
|
|
|
|
if (state == RENEW_REQUESTED)
|
|
|
|
run_script(NULL, SCRIPT_DECONFIG);
|
|
|
|
state = INIT_SELECTING;
|
|
|
|
timeout = now;
|
|
|
|
packet_num = 0;
|
2010-11-13 23:00:54 +05:30
|
|
|
change_listen_mode(LISTEN_RAW);
|
2010-11-13 01:03:17 +05:30
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BOUND:
|
|
|
|
/* Lease is starting to run out, time to enter renewing state */
|
|
|
|
state = RENEWING;
|
2010-11-13 23:00:54 +05:30
|
|
|
change_listen_mode(LISTEN_KERNEL);
|
2010-11-13 01:03:17 +05:30
|
|
|
log_line("Entering renew state.");
|
|
|
|
/* fall right through */
|
|
|
|
case RENEWING:
|
|
|
|
/* Either set a new T1, or enter REBINDING state */
|
|
|
|
if ((t2 - t1) <= (lease / 14400 + 1)) {
|
|
|
|
/* timed out, enter rebinding state */
|
|
|
|
state = REBINDING;
|
|
|
|
timeout = now + (t2 - t1);
|
|
|
|
log_line("Entering rebinding state.");
|
|
|
|
} else {
|
|
|
|
/* send a request packet */
|
|
|
|
send_renew(xid, server_addr, requested_ip); /* unicast */
|
|
|
|
|
|
|
|
t1 = ((t2 - t1) >> 1) + t1;
|
|
|
|
timeout = t1 + start;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case REBINDING:
|
|
|
|
/* Either set a new T2, or enter INIT state */
|
|
|
|
if ((lease - t2) <= (lease / 14400 + 1)) {
|
|
|
|
/* timed out, enter init state */
|
|
|
|
state = INIT_SELECTING;
|
|
|
|
log_line("Lease lost, entering init state.");
|
|
|
|
run_script(NULL, SCRIPT_DECONFIG);
|
|
|
|
timeout = now;
|
|
|
|
packet_num = 0;
|
2010-11-13 23:00:54 +05:30
|
|
|
change_listen_mode(LISTEN_RAW);
|
2010-11-13 01:03:17 +05:30
|
|
|
} else {
|
|
|
|
/* send a request packet */
|
|
|
|
send_renew(xid, 0, requested_ip); /* broadcast */
|
|
|
|
|
|
|
|
t2 = ((lease - t2) >> 1) + t2;
|
|
|
|
timeout = t2 + start;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RELEASED:
|
|
|
|
/* yah, I know, *you* say it would never happen */
|
|
|
|
timeout = 0x7fffffff;
|
|
|
|
break;
|
|
|
|
}
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_packet(void)
|
|
|
|
{
|
2010-11-13 01:03:17 +05:30
|
|
|
unsigned char *temp = NULL, *message = NULL;
|
|
|
|
int len;
|
|
|
|
struct in_addr temp_addr;
|
|
|
|
struct dhcpMessage packet;
|
|
|
|
|
|
|
|
if (listen_mode == LISTEN_KERNEL)
|
2010-12-01 23:41:09 +05:30
|
|
|
len = get_packet(&packet, listenFd);
|
2010-11-13 23:00:54 +05:30
|
|
|
else if (listen_mode == LISTEN_RAW)
|
2010-12-01 23:41:09 +05:30
|
|
|
len = get_raw_packet(&packet, listenFd);
|
2010-11-13 23:00:54 +05:30
|
|
|
else /* LISTEN_NONE */
|
|
|
|
return;
|
2010-11-13 01:03:17 +05:30
|
|
|
|
|
|
|
if (len == -1 && errno != EINTR) {
|
2010-11-13 08:14:28 +05:30
|
|
|
log_error("reopening socket.");
|
2010-11-13 23:00:54 +05:30
|
|
|
change_listen_mode(listen_mode); /* just close and reopen */
|
2010-11-13 01:03:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (packet.xid != xid) {
|
|
|
|
log_line("Ignoring XID %lx (our xid is %lx).",
|
2010-11-13 17:12:52 +05:30
|
|
|
(uint32_t) packet.xid, xid);
|
2010-11-13 01:03:17 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((message = get_option(&packet, DHCP_MESSAGE_TYPE)) == NULL) {
|
|
|
|
log_line("couldnt get option from packet -- ignoring");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-13 23:00:54 +05:30
|
|
|
time_t now = time(0);
|
2010-11-13 01:03:17 +05:30
|
|
|
switch (state) {
|
|
|
|
case INIT_SELECTING:
|
|
|
|
/* Must be a DHCPOFFER to one of our xid's */
|
|
|
|
if (*message == DHCPOFFER) {
|
|
|
|
if ((temp = get_option(&packet, DHCP_SERVER_ID))) {
|
2010-11-14 06:07:08 +05:30
|
|
|
/* Memcpy to a temp buffer to force alignment */
|
2010-11-13 01:03:17 +05:30
|
|
|
memcpy(&server_addr, temp, 4);
|
|
|
|
xid = packet.xid;
|
|
|
|
requested_ip = packet.yiaddr;
|
|
|
|
|
|
|
|
/* enter requesting state */
|
|
|
|
state = REQUESTING;
|
|
|
|
timeout = now;
|
|
|
|
packet_num = 0;
|
|
|
|
} else {
|
|
|
|
log_line("No server ID in message");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RENEW_REQUESTED:
|
|
|
|
case REQUESTING:
|
|
|
|
case RENEWING:
|
|
|
|
case REBINDING:
|
|
|
|
if (*message == DHCPACK) {
|
|
|
|
if (!(temp = get_option(&packet, DHCP_LEASE_TIME))) {
|
|
|
|
log_line("No lease time received, assuming 1h.");
|
|
|
|
lease = 60 * 60;
|
|
|
|
} else {
|
2010-11-14 06:07:08 +05:30
|
|
|
/* Memcpy to a temp buffer to force alignment */
|
2010-11-13 01:03:17 +05:30
|
|
|
memcpy(&lease, temp, 4);
|
|
|
|
lease = ntohl(lease);
|
2010-11-14 06:07:08 +05:30
|
|
|
/* Enforce upper and lower bounds on lease. */
|
|
|
|
lease &= 0x0fffffff;
|
|
|
|
if (lease < RETRY_DELAY)
|
|
|
|
lease = RETRY_DELAY;
|
2010-11-13 01:03:17 +05:30
|
|
|
}
|
|
|
|
|
2010-11-16 06:36:50 +05:30
|
|
|
if (!arpping(packet.yiaddr, NULL, 0, client_config.arp,
|
|
|
|
client_config.interface)) {
|
|
|
|
log_line("Offered address is in use: declining.");
|
|
|
|
send_decline(xid, server_addr, packet.yiaddr);
|
|
|
|
|
|
|
|
if (state != REQUESTING)
|
|
|
|
run_script(NULL, SCRIPT_DECONFIG);
|
|
|
|
state = INIT_SELECTING;
|
|
|
|
requested_ip = 0;
|
|
|
|
timeout = now;
|
|
|
|
packet_num = 0;
|
|
|
|
change_listen_mode(LISTEN_RAW);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-11-13 01:03:17 +05:30
|
|
|
/* enter bound state */
|
|
|
|
t1 = lease >> 1;
|
|
|
|
|
|
|
|
/* little fixed point for n * .875 */
|
|
|
|
t2 = (lease * 0x7) >> 3;
|
|
|
|
temp_addr.s_addr = packet.yiaddr;
|
|
|
|
log_line("Lease of %s obtained, lease time %ld.",
|
|
|
|
inet_ntoa(temp_addr), lease);
|
|
|
|
start = now;
|
|
|
|
timeout = t1 + start;
|
|
|
|
requested_ip = packet.yiaddr;
|
|
|
|
run_script(&packet,
|
|
|
|
((state == RENEWING || state == REBINDING)
|
|
|
|
? SCRIPT_RENEW : SCRIPT_BOUND));
|
|
|
|
|
|
|
|
state = BOUND;
|
2010-11-13 23:00:54 +05:30
|
|
|
change_listen_mode(LISTEN_NONE);
|
2010-11-13 01:03:17 +05:30
|
|
|
if (client_config.quit_after_lease)
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
if (!client_config.foreground)
|
|
|
|
background();
|
|
|
|
|
|
|
|
} else if (*message == DHCPNAK) {
|
|
|
|
/* return to init state */
|
|
|
|
log_line("Received DHCP NAK.");
|
|
|
|
run_script(&packet, SCRIPT_NAK);
|
|
|
|
if (state != REQUESTING)
|
|
|
|
run_script(NULL, SCRIPT_DECONFIG);
|
|
|
|
state = INIT_SELECTING;
|
|
|
|
timeout = now;
|
|
|
|
requested_ip = 0;
|
|
|
|
packet_num = 0;
|
2010-11-13 23:00:54 +05:30
|
|
|
change_listen_mode(LISTEN_RAW);
|
2010-11-13 01:03:17 +05:30
|
|
|
sleep(3); /* avoid excessive network traffic */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BOUND:
|
|
|
|
case RELEASED:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
2010-12-01 23:05:13 +05:30
|
|
|
static void setup_signals()
|
2010-11-12 14:32:18 +05:30
|
|
|
{
|
2010-12-01 23:05:13 +05:30
|
|
|
sigset_t mask;
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGUSR1);
|
|
|
|
sigaddset(&mask, SIGUSR2);
|
|
|
|
sigaddset(&mask, SIGTERM);
|
|
|
|
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0)
|
|
|
|
suicide("sigprocmask failed");
|
|
|
|
signalFd = signalfd(-1, &mask, SFD_NONBLOCK);
|
|
|
|
if (signalFd < 0)
|
|
|
|
suicide("signalfd failed");
|
|
|
|
}
|
2010-11-13 01:03:17 +05:30
|
|
|
|
2010-12-01 23:05:13 +05:30
|
|
|
static void signal_dispatch()
|
|
|
|
{
|
|
|
|
int t, off = 0;
|
|
|
|
struct signalfd_siginfo si;
|
|
|
|
again:
|
|
|
|
t = read(signalFd, (char *)&si + off, sizeof si - off);
|
|
|
|
if (t < sizeof si - off) {
|
|
|
|
if (t < 0) {
|
|
|
|
if (t == EAGAIN || t == EWOULDBLOCK || t == EINTR)
|
|
|
|
goto again;
|
|
|
|
else
|
|
|
|
suicide("signalfd read error");
|
|
|
|
}
|
|
|
|
off += t;
|
|
|
|
}
|
|
|
|
switch (si.ssi_signo) {
|
|
|
|
case SIGUSR1:
|
2010-11-13 01:03:17 +05:30
|
|
|
perform_renew();
|
2010-12-01 23:05:13 +05:30
|
|
|
break;
|
|
|
|
case SIGUSR2:
|
2010-11-13 01:03:17 +05:30
|
|
|
perform_release();
|
2010-12-01 23:05:13 +05:30
|
|
|
break;
|
|
|
|
case SIGTERM:
|
2010-11-13 01:03:17 +05:30
|
|
|
log_line("Received SIGTERM. Exiting gracefully.");
|
|
|
|
exit(EXIT_SUCCESS);
|
2010-12-01 23:05:13 +05:30
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void do_work(void)
|
|
|
|
{
|
2010-12-01 23:41:09 +05:30
|
|
|
int timeoutms;
|
2010-11-13 01:03:17 +05:30
|
|
|
|
2010-12-01 23:41:09 +05:30
|
|
|
epollfd = epoll_create1(0);
|
|
|
|
if (epollfd == -1)
|
|
|
|
suicide("epoll_create1 failed");
|
|
|
|
epoll_add(signalFd);
|
|
|
|
change_listen_mode(LISTEN_RAW);
|
2010-11-13 01:03:17 +05:30
|
|
|
|
2010-12-01 23:41:09 +05:30
|
|
|
for (;;) {
|
|
|
|
timeoutms = (timeout - time(0)) * 1000;
|
|
|
|
if (timeoutms <= 0) {
|
2010-11-13 01:03:17 +05:30
|
|
|
handle_timeout();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-12-01 23:41:09 +05:30
|
|
|
int r = epoll_wait(epollfd, events, 3, timeoutms);
|
|
|
|
if (r == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
suicide("epoll_wait failed");
|
|
|
|
}
|
|
|
|
for (int i = 0; i < r; ++i) {
|
|
|
|
int fd = events[i].data.fd;
|
|
|
|
if (fd == signalFd)
|
|
|
|
signal_dispatch();
|
|
|
|
else if (fd == listenFd)
|
|
|
|
handle_packet();
|
|
|
|
else
|
|
|
|
suicide("epoll_wait: unknown fd");
|
2010-11-13 01:03:17 +05:30
|
|
|
}
|
|
|
|
}
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2010-11-13 06:13:16 +05:30
|
|
|
char chroot_dir[MAX_PATH_LENGTH] = "";
|
2010-11-13 01:03:17 +05:30
|
|
|
int c, len;
|
|
|
|
struct passwd *pwd;
|
|
|
|
uid_t uid = 0;
|
|
|
|
gid_t gid = 0;
|
|
|
|
static struct option arg_options[] = {
|
|
|
|
{"clientid", required_argument, 0, 'c'},
|
|
|
|
{"foreground", no_argument, 0, 'f'},
|
|
|
|
{"background", no_argument, 0, 'b'},
|
2010-11-13 05:14:49 +05:30
|
|
|
{"pidfile", required_argument, 0, 'p'},
|
2010-11-13 01:03:17 +05:30
|
|
|
{"hostname", required_argument, 0, 'H'},
|
|
|
|
{"hostname", required_argument, 0, 'h'},
|
|
|
|
{"interface", required_argument, 0, 'i'},
|
|
|
|
{"now", no_argument, 0, 'n'},
|
|
|
|
{"quit", no_argument, 0, 'q'},
|
|
|
|
{"request", required_argument, 0, 'r'},
|
|
|
|
{"version", no_argument, 0, 'v'},
|
|
|
|
{"user", required_argument, 0, 'u'},
|
|
|
|
{"chroot", required_argument, 0, 'C'},
|
|
|
|
{"help", no_argument, 0, '?'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* get options */
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
2010-11-13 05:14:49 +05:30
|
|
|
c = getopt_long(argc, argv, "c:fbp:H:h:i:np:qr:u:C:v", arg_options,
|
2010-11-13 01:03:17 +05:30
|
|
|
&option_index);
|
|
|
|
if (c == -1) break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'c':
|
|
|
|
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
|
|
|
|
if (client_config.clientid)
|
|
|
|
free(client_config.clientid);
|
|
|
|
client_config.clientid = xmalloc(len + 1);
|
|
|
|
client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
|
|
|
|
client_config.clientid[OPT_LEN] = len;
|
|
|
|
strlcpy((char *)client_config.clientid + OPT_DATA, optarg,
|
|
|
|
len + 1 - (OPT_DATA - OPT_CODE));
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
client_config.foreground = 1;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
client_config.background_if_no_lease = 1;
|
|
|
|
break;
|
2010-11-13 05:14:49 +05:30
|
|
|
case 'p':
|
2010-11-13 06:13:16 +05:30
|
|
|
strlcpy(pidfile, optarg, sizeof pidfile);
|
2010-11-13 05:14:49 +05:30
|
|
|
break;
|
2010-11-13 01:03:17 +05:30
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
len = strlen(optarg) > 255 ? 255 : strlen(optarg);
|
|
|
|
if (client_config.hostname)
|
|
|
|
free(client_config.hostname);
|
|
|
|
client_config.hostname = xmalloc(len + 1);
|
|
|
|
client_config.hostname[OPT_CODE] = DHCP_HOST_NAME;
|
|
|
|
client_config.hostname[OPT_LEN] = len;
|
|
|
|
strlcpy((char*)client_config.hostname + OPT_DATA, optarg,
|
|
|
|
len + 1 - (OPT_DATA - OPT_CODE));
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
client_config.interface = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
client_config.abort_if_no_lease = 1;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
client_config.quit_after_lease = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
requested_ip = inet_addr(optarg);
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
pwd = getpwnam(optarg);
|
|
|
|
if (pwd) {
|
|
|
|
uid = (int)pwd->pw_uid;
|
|
|
|
gid = (int)pwd->pw_gid;
|
|
|
|
} else {
|
|
|
|
printf("Bad username provided.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
strlcpy(chroot_dir, optarg, sizeof chroot_dir);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
printf("ndhc, version " VERSION "\n\n");
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
show_usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_line("ndhc client " VERSION " started.");
|
|
|
|
|
2010-11-13 06:38:16 +05:30
|
|
|
if (client_config.foreground && !client_config.background_if_no_lease) {
|
|
|
|
if (file_exists(pidfile, "w") == -1) {
|
|
|
|
log_line("FATAL - cannot open pidfile for write!");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
write_pid(pidfile);
|
|
|
|
}
|
|
|
|
|
2010-11-13 01:03:17 +05:30
|
|
|
if (read_interface(client_config.interface, &client_config.ifindex,
|
|
|
|
NULL, client_config.arp) < 0)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
if (!client_config.clientid) {
|
|
|
|
client_config.clientid = xmalloc(6 + 3);
|
|
|
|
client_config.clientid[OPT_CODE] = DHCP_CLIENT_ID;
|
|
|
|
client_config.clientid[OPT_LEN] = 7;
|
|
|
|
client_config.clientid[OPT_DATA] = 1;
|
|
|
|
memcpy(client_config.clientid + 3, client_config.arp, 6);
|
|
|
|
}
|
|
|
|
|
2010-12-01 23:05:13 +05:30
|
|
|
setup_signals();
|
2010-11-13 01:03:17 +05:30
|
|
|
|
|
|
|
if (chdir(chroot_dir)) {
|
|
|
|
printf("Failed to chdir(%s)!\n", chroot_dir);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chroot(chroot_dir)) {
|
|
|
|
printf("Failed to chroot(%s)!\n", chroot_dir);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_cap(uid, gid,
|
|
|
|
"cap_net_bind_service,cap_net_broadcast,cap_net_raw=ep");
|
|
|
|
drop_root(uid, gid);
|
|
|
|
|
|
|
|
state = INIT_SELECTING;
|
|
|
|
run_script(NULL, SCRIPT_DECONFIG);
|
|
|
|
|
|
|
|
do_work();
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2010-11-12 14:32:18 +05:30
|
|
|
}
|