2007-11-15 19:35:53 +08:00
|
|
|
/*
|
2007-11-08 00:26:41 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2011-01-03 19:42:43 +08:00
|
|
|
* Copyright (c) 2006-2011 Roy Marples <roy@marples.name>
|
2007-11-15 19:35:53 +08:00
|
|
|
* All rights reserved
|
|
|
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2006-11-28 04:23:22 +08:00
|
|
|
*/
|
|
|
|
|
2011-01-03 19:42:43 +08:00
|
|
|
const char copyright[] = "Copyright (c) 2006-2011 Roy Marples";
|
2007-11-08 18:38:54 +08:00
|
|
|
|
2007-05-13 20:28:54 +08:00
|
|
|
#include <sys/file.h>
|
2009-04-17 21:42:44 +08:00
|
|
|
#include <sys/socket.h>
|
2008-08-13 23:09:13 +08:00
|
|
|
#include <sys/stat.h>
|
2009-07-14 21:59:30 +08:00
|
|
|
#include <sys/time.h>
|
2006-12-15 19:23:32 +08:00
|
|
|
#include <sys/types.h>
|
2009-01-26 23:15:28 +08:00
|
|
|
#include <sys/uio.h>
|
2008-03-21 00:47:51 +08:00
|
|
|
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <arpa/inet.h>
|
2009-03-20 01:52:12 +08:00
|
|
|
#include <net/route.h>
|
2008-03-21 00:47:51 +08:00
|
|
|
|
2009-03-20 03:17:34 +08:00
|
|
|
#ifdef __linux__
|
2010-03-11 18:02:03 +08:00
|
|
|
# include <asm/types.h> /* for systems with broken headers */
|
2009-03-20 03:17:34 +08:00
|
|
|
# include <linux/rtnetlink.h>
|
|
|
|
#endif
|
|
|
|
|
2008-07-03 01:05:41 +08:00
|
|
|
#include <ctype.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
2008-09-02 21:28:11 +08:00
|
|
|
#include <limits.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <paths.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-09-06 02:24:34 +08:00
|
|
|
#include <syslog.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <unistd.h>
|
2008-08-12 20:20:25 +08:00
|
|
|
#include <time.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "arp.h"
|
|
|
|
#include "bind.h"
|
2007-05-11 00:08:49 +08:00
|
|
|
#include "config.h"
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "configure.h"
|
2008-09-04 00:49:28 +08:00
|
|
|
#include "control.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
#include "dhcpcd.h"
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "duid.h"
|
|
|
|
#include "eloop.h"
|
|
|
|
#include "if-options.h"
|
2008-09-10 01:07:48 +08:00
|
|
|
#include "if-pref.h"
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "ipv4ll.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "signals.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
/* We should define a maximum for the NAK exponential backoff */
|
|
|
|
#define NAKOFF_MAX 60
|
2007-10-11 21:26:16 +08:00
|
|
|
|
2009-12-03 21:30:09 +08:00
|
|
|
/* Wait N nanoseconds between sending a RELEASE and dropping the address.
|
|
|
|
* This gives the kernel enough time to actually send it. */
|
|
|
|
#define RELEASE_DELAY_S 0
|
|
|
|
#define RELEASE_DELAY_NS 10000000
|
|
|
|
|
2008-09-05 15:22:03 +08:00
|
|
|
int options = 0;
|
2008-09-02 21:28:11 +08:00
|
|
|
int pidfd = -1;
|
2008-09-10 01:07:48 +08:00
|
|
|
struct interface *ifaces = NULL;
|
2008-09-11 17:38:02 +08:00
|
|
|
int ifac = 0;
|
|
|
|
char **ifav = NULL;
|
|
|
|
int ifdc = 0;
|
|
|
|
char **ifdv = NULL;
|
2008-09-10 01:07:48 +08:00
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
static char **margv;
|
|
|
|
static int margc;
|
2010-08-24 17:34:21 +08:00
|
|
|
static struct if_options *if_options;
|
2008-10-06 20:45:32 +08:00
|
|
|
static char **ifv;
|
|
|
|
static int ifc;
|
|
|
|
static char *cffile;
|
2008-09-05 22:16:53 +08:00
|
|
|
static char *pidfile;
|
2009-01-28 02:09:02 +08:00
|
|
|
static int linkfd = -1;
|
2008-04-19 07:12:44 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
struct dhcp_op {
|
|
|
|
uint8_t value;
|
|
|
|
const char *name;
|
|
|
|
};
|
2007-11-16 01:58:39 +08:00
|
|
|
|
2008-11-10 19:15:27 +08:00
|
|
|
static const struct dhcp_op dhcp_ops[] = {
|
2010-04-30 16:20:01 +08:00
|
|
|
{ DHCP_DISCOVER, "DISCOVER" },
|
|
|
|
{ DHCP_OFFER, "OFFER" },
|
|
|
|
{ DHCP_REQUEST, "REQUEST" },
|
|
|
|
{ DHCP_DECLINE, "DECLINE" },
|
|
|
|
{ DHCP_ACK, "ACK" },
|
|
|
|
{ DHCP_NAK, "NAK" },
|
|
|
|
{ DHCP_RELEASE, "RELEASE" },
|
|
|
|
{ DHCP_INFORM, "INFORM" },
|
2008-09-02 21:28:11 +08:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
2008-09-16 21:17:23 +08:00
|
|
|
static void send_release(struct interface *);
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
static const char *
|
|
|
|
get_dhcp_op(uint8_t type)
|
|
|
|
{
|
|
|
|
const struct dhcp_op *d;
|
2007-11-16 01:57:10 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
for (d = dhcp_ops; d->name; d++)
|
|
|
|
if (d->value == type)
|
|
|
|
return d->name;
|
|
|
|
return NULL;
|
2007-11-16 01:57:10 +08:00
|
|
|
}
|
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
static pid_t
|
2008-09-02 21:28:11 +08:00
|
|
|
read_pid(void)
|
2006-11-28 04:23:22 +08:00
|
|
|
{
|
2007-04-11 21:18:33 +08:00
|
|
|
FILE *fp;
|
2009-01-29 21:01:29 +08:00
|
|
|
pid_t pid;
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
if ((fp = fopen(pidfile, "r")) == NULL) {
|
2007-04-11 21:18:33 +08:00
|
|
|
errno = ENOENT;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-29 21:01:29 +08:00
|
|
|
if (fscanf(fp, "%d", &pid) != 1)
|
|
|
|
pid = 0;
|
2008-03-21 00:47:51 +08:00
|
|
|
fclose(fp);
|
|
|
|
return pid;
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
static void
|
|
|
|
usage(void)
|
2006-11-28 04:23:22 +08:00
|
|
|
{
|
2010-01-29 04:12:54 +08:00
|
|
|
printf("usage: "PACKAGE" [-dgknpqwxyADEGHJKLOTV] [-c script] [-f file]"
|
2009-07-26 07:37:20 +08:00
|
|
|
" [-e var=val]\n"
|
|
|
|
" [-h hostname] [-i classID ] [-l leasetime]"
|
|
|
|
" [-m metric] [-o option]\n"
|
|
|
|
" [-r ipaddr] [-s ipaddr] [-t timeout]"
|
|
|
|
" [-u userclass]\n"
|
|
|
|
" [-F none|ptr|both] [-I clientID] [-C hookscript]"
|
|
|
|
" [-Q option]\n"
|
|
|
|
" [-X ipaddr] <interface>\n");
|
2008-05-21 00:05:15 +08:00
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
static void
|
|
|
|
cleanup(void)
|
2008-05-21 00:05:15 +08:00
|
|
|
{
|
2008-09-02 21:28:11 +08:00
|
|
|
#ifdef DEBUG_MEMORY
|
|
|
|
struct interface *iface;
|
2008-09-11 17:38:02 +08:00
|
|
|
int i;
|
2008-09-02 21:28:11 +08:00
|
|
|
|
2010-08-24 17:34:21 +08:00
|
|
|
free_options(if_options);
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
while (ifaces) {
|
|
|
|
iface = ifaces;
|
|
|
|
ifaces = iface->next;
|
|
|
|
free_interface(iface);
|
2008-05-21 00:05:15 +08:00
|
|
|
}
|
2008-09-11 17:38:02 +08:00
|
|
|
|
|
|
|
for (i = 0; i < ifac; i++)
|
|
|
|
free(ifav[i]);
|
|
|
|
free(ifav);
|
|
|
|
for (i = 0; i < ifdc; i++)
|
|
|
|
free(ifdv[i]);
|
|
|
|
free(ifdv);
|
2008-09-02 21:28:11 +08:00
|
|
|
#endif
|
2008-05-21 00:05:15 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
if (linkfd != -1)
|
|
|
|
close(linkfd);
|
|
|
|
if (pidfd > -1) {
|
2008-09-05 15:22:03 +08:00
|
|
|
if (options & DHCPCD_MASTER) {
|
2008-09-04 00:49:28 +08:00
|
|
|
if (stop_control() == -1)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "stop_control: %m");
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
close(pidfd);
|
|
|
|
unlink(pidfile);
|
|
|
|
}
|
2008-09-05 22:16:53 +08:00
|
|
|
#ifdef DEBUG_MEMORY
|
|
|
|
free(pidfile);
|
|
|
|
#endif
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2008-11-10 19:15:27 +08:00
|
|
|
/* ARGSUSED */
|
2009-10-09 03:56:52 +08:00
|
|
|
void
|
2008-09-04 00:49:28 +08:00
|
|
|
handle_exit_timeout(_unused void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2009-10-09 03:56:52 +08:00
|
|
|
int timeout;
|
|
|
|
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "timed out");
|
2010-06-26 17:17:54 +08:00
|
|
|
if (!(options & DHCPCD_TIMEOUT_IPV4LL)) {
|
|
|
|
if (options & DHCPCD_MASTER) {
|
|
|
|
daemonise();
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2009-10-09 03:56:52 +08:00
|
|
|
options &= ~DHCPCD_TIMEOUT_IPV4LL;
|
|
|
|
timeout = (PROBE_NUM * PROBE_MAX) + PROBE_WAIT + 1;
|
|
|
|
syslog(LOG_WARNING, "allowing %d seconds for IPv4LL timeout", timeout);
|
|
|
|
add_timeout_sec(timeout, handle_exit_timeout, NULL);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
drop_config(struct interface *iface, const char *reason)
|
|
|
|
{
|
2009-01-15 21:31:46 +08:00
|
|
|
free(iface->state->old);
|
|
|
|
iface->state->old = iface->state->new;
|
|
|
|
iface->state->new = NULL;
|
|
|
|
iface->state->reason = reason;
|
|
|
|
configure(iface);
|
2009-01-20 00:17:18 +08:00
|
|
|
free(iface->state->old);
|
|
|
|
iface->state->old = NULL;
|
2008-09-02 21:28:11 +08:00
|
|
|
iface->state->lease.addr.s_addr = 0;
|
|
|
|
}
|
|
|
|
|
2009-01-28 02:09:02 +08:00
|
|
|
struct interface *
|
|
|
|
find_interface(const char *ifname)
|
|
|
|
{
|
|
|
|
struct interface *ifp;
|
|
|
|
|
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
|
|
|
if (strcmp(ifp->name, ifname) == 0)
|
|
|
|
return ifp;
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-16 21:17:23 +08:00
|
|
|
|
2008-09-04 17:43:52 +08:00
|
|
|
static void
|
2009-01-21 00:33:11 +08:00
|
|
|
stop_interface(struct interface *iface)
|
2008-09-04 17:43:52 +08:00
|
|
|
{
|
|
|
|
struct interface *ifp, *ifl = NULL;
|
|
|
|
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_INFO, "%s: removing interface", iface->name);
|
2009-01-21 00:33:11 +08:00
|
|
|
if (strcmp(iface->state->reason, "RELEASE") != 0)
|
|
|
|
drop_config(iface, "STOP");
|
2008-09-04 17:43:52 +08:00
|
|
|
close_sockets(iface);
|
|
|
|
delete_timeout(NULL, iface);
|
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next) {
|
|
|
|
if (ifp == iface)
|
|
|
|
break;
|
|
|
|
ifl = ifp;
|
|
|
|
}
|
|
|
|
if (ifl)
|
2008-09-12 06:55:27 +08:00
|
|
|
ifl->next = ifp->next;
|
2008-09-04 17:43:52 +08:00
|
|
|
else
|
2008-09-12 06:55:27 +08:00
|
|
|
ifaces = ifp->next;
|
2008-09-04 17:43:52 +08:00
|
|
|
free_interface(ifp);
|
2009-07-12 02:54:43 +08:00
|
|
|
if (!(options & (DHCPCD_MASTER | DHCPCD_TEST)))
|
2008-09-04 17:43:52 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2010-01-14 10:04:41 +08:00
|
|
|
static uint32_t
|
|
|
|
dhcp_xid(struct interface *iface)
|
|
|
|
{
|
|
|
|
uint32_t xid;
|
|
|
|
|
|
|
|
if (iface->state->options->options & DHCPCD_XID_HWADDR &&
|
|
|
|
iface->hwlen >= sizeof(xid))
|
|
|
|
/* The lower bits are probably more unique on the network */
|
|
|
|
memcpy(&xid, (iface->hwaddr + iface->hwlen) - sizeof(xid),
|
|
|
|
sizeof(xid));
|
|
|
|
else
|
|
|
|
xid = arc4random();
|
|
|
|
|
|
|
|
return xid;
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
static void
|
|
|
|
send_message(struct interface *iface, int type,
|
2009-02-12 01:56:22 +08:00
|
|
|
void (*callback)(void *))
|
2008-07-03 01:05:41 +08:00
|
|
|
{
|
2008-09-02 21:28:11 +08:00
|
|
|
struct if_state *state = iface->state;
|
2009-03-24 04:10:02 +08:00
|
|
|
struct if_options *ifo = state->options;
|
2008-09-02 21:28:11 +08:00
|
|
|
struct dhcp_message *dhcp;
|
|
|
|
uint8_t *udp;
|
|
|
|
ssize_t len, r;
|
|
|
|
struct in_addr from, to;
|
|
|
|
in_addr_t a = 0;
|
|
|
|
struct timeval tv;
|
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
if (!callback)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_DEBUG, "%s: sending %s with xid 0x%x",
|
2009-02-12 01:56:22 +08:00
|
|
|
iface->name, get_dhcp_op(type), state->xid);
|
2008-09-02 21:28:11 +08:00
|
|
|
else {
|
|
|
|
if (state->interval == 0)
|
|
|
|
state->interval = 4;
|
|
|
|
else {
|
|
|
|
state->interval *= 2;
|
|
|
|
if (state->interval > 64)
|
|
|
|
state->interval = 64;
|
|
|
|
}
|
|
|
|
tv.tv_sec = state->interval + DHCP_RAND_MIN;
|
|
|
|
tv.tv_usec = arc4random() % (DHCP_RAND_MAX_U - DHCP_RAND_MIN_U);
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_DEBUG,
|
2009-02-12 01:56:22 +08:00
|
|
|
"%s: sending %s (xid 0x%x), next in %0.2f seconds",
|
|
|
|
iface->name, get_dhcp_op(type), state->xid,
|
|
|
|
timeval_to_double(&tv));
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2010-04-19 20:36:28 +08:00
|
|
|
|
|
|
|
/* Ensure sockets are open. */
|
|
|
|
open_sockets(iface);
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
/* If we couldn't open a UDP port for our IP address
|
|
|
|
* then we cannot renew.
|
2008-11-20 17:57:46 +08:00
|
|
|
* This could happen if our IP was pulled out from underneath us.
|
|
|
|
* Also, we should not unicast from a BOOTP lease. */
|
2009-03-24 04:10:02 +08:00
|
|
|
if (iface->udp_fd == -1 ||
|
|
|
|
(!(ifo->options & DHCPCD_INFORM) && is_bootp(iface->state->new)))
|
|
|
|
{
|
2008-09-02 21:28:11 +08:00
|
|
|
a = iface->addr.s_addr;
|
|
|
|
iface->addr.s_addr = 0;
|
|
|
|
}
|
|
|
|
len = make_message(&dhcp, iface, type);
|
2008-11-20 17:57:46 +08:00
|
|
|
if (a)
|
2008-09-02 21:28:11 +08:00
|
|
|
iface->addr.s_addr = a;
|
|
|
|
from.s_addr = dhcp->ciaddr;
|
|
|
|
if (from.s_addr)
|
|
|
|
to.s_addr = state->lease.server.s_addr;
|
|
|
|
else
|
|
|
|
to.s_addr = 0;
|
|
|
|
if (to.s_addr && to.s_addr != INADDR_BROADCAST) {
|
|
|
|
r = send_packet(iface, to, (uint8_t *)dhcp, len);
|
2010-04-19 20:36:28 +08:00
|
|
|
if (r == -1) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "%s: send_packet: %m", iface->name);
|
2010-04-19 20:36:28 +08:00
|
|
|
close_sockets(iface);
|
|
|
|
}
|
2008-07-03 01:05:41 +08:00
|
|
|
} else {
|
2008-09-02 21:28:11 +08:00
|
|
|
len = make_udp_packet(&udp, (uint8_t *)dhcp, len, from, to);
|
|
|
|
r = send_raw_packet(iface, ETHERTYPE_IP, udp, len);
|
|
|
|
free(udp);
|
2009-03-23 18:09:10 +08:00
|
|
|
/* If we failed to send a raw packet this normally means
|
|
|
|
* we don't have the ability to work beneath the IP layer
|
|
|
|
* for this interface.
|
|
|
|
* As such we remove it from consideration without actually
|
|
|
|
* stopping the interface. */
|
|
|
|
if (r == -1) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "%s: send_raw_packet: %m", iface->name);
|
2009-07-20 04:03:58 +08:00
|
|
|
if (!(options & DHCPCD_TEST))
|
|
|
|
drop_config(iface, "FAIL");
|
2009-03-23 18:09:10 +08:00
|
|
|
close_sockets(iface);
|
|
|
|
delete_timeout(NULL, iface);
|
|
|
|
callback = NULL;
|
|
|
|
}
|
2008-07-03 01:05:41 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
free(dhcp);
|
2010-04-19 20:36:28 +08:00
|
|
|
|
2009-02-20 16:33:03 +08:00
|
|
|
/* Even if we fail to send a packet we should continue as we are
|
|
|
|
* as our failure timeouts will change out codepath when needed. */
|
|
|
|
if (callback)
|
|
|
|
add_timeout_tv(&tv, callback, iface);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2008-10-19 00:41:19 +08:00
|
|
|
static void
|
|
|
|
send_inform(void *arg)
|
|
|
|
{
|
|
|
|
send_message((struct interface *)arg, DHCP_INFORM, send_inform);
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
static void
|
2008-09-04 00:49:28 +08:00
|
|
|
send_discover(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
send_message((struct interface *)arg, DHCP_DISCOVER, send_discover);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2008-12-18 18:25:52 +08:00
|
|
|
static void
|
2008-09-04 00:49:28 +08:00
|
|
|
send_request(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
send_message((struct interface *)arg, DHCP_REQUEST, send_request);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2008-07-03 01:05:41 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
static void
|
2008-09-04 00:49:28 +08:00
|
|
|
send_renew(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
send_message((struct interface *)arg, DHCP_REQUEST, send_renew);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-04 00:49:28 +08:00
|
|
|
send_rebind(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
send_message((struct interface *)arg, DHCP_REQUEST, send_rebind);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-04 00:49:28 +08:00
|
|
|
start_expire(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
struct interface *iface = arg;
|
2008-09-15 23:23:46 +08:00
|
|
|
|
2009-03-04 07:41:53 +08:00
|
|
|
iface->state->interval = 0;
|
2008-09-15 23:23:46 +08:00
|
|
|
if (iface->addr.s_addr == 0) {
|
|
|
|
/* We failed to reboot, so enter discovery. */
|
2010-01-26 16:07:21 +08:00
|
|
|
iface->state->lease.addr.s_addr = 0;
|
2008-09-15 23:23:46 +08:00
|
|
|
start_discover(iface);
|
|
|
|
return;
|
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "%s: lease expired", iface->name);
|
2008-09-02 21:28:11 +08:00
|
|
|
delete_timeout(NULL, iface);
|
|
|
|
drop_config(iface, "EXPIRE");
|
2008-09-16 21:17:23 +08:00
|
|
|
unlink(iface->leasefile);
|
2008-11-20 17:57:46 +08:00
|
|
|
if (iface->carrier != LINK_DOWN)
|
|
|
|
start_interface(iface);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
log_dhcp(int lvl, const char *msg,
|
2010-04-30 11:16:39 +08:00
|
|
|
const struct interface *iface, const struct dhcp_message *dhcp,
|
|
|
|
const struct in_addr *from)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2010-05-01 02:22:48 +08:00
|
|
|
const char *tfrom;
|
2008-09-02 21:28:11 +08:00
|
|
|
char *a;
|
|
|
|
struct in_addr addr;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (strcmp(msg, "NAK:") == 0)
|
|
|
|
a = get_option_string(dhcp, DHO_MESSAGE);
|
2009-05-14 03:17:21 +08:00
|
|
|
else if (dhcp->yiaddr != 0) {
|
2008-09-02 21:28:11 +08:00
|
|
|
addr.s_addr = dhcp->yiaddr;
|
|
|
|
a = xstrdup(inet_ntoa(addr));
|
2009-05-14 03:17:21 +08:00
|
|
|
} else
|
|
|
|
a = NULL;
|
2010-05-01 02:22:48 +08:00
|
|
|
|
|
|
|
tfrom = "from";
|
2009-07-09 01:01:38 +08:00
|
|
|
r = get_option_addr(&addr, dhcp, DHO_SERVERID);
|
2008-09-02 21:28:11 +08:00
|
|
|
if (dhcp->servername[0] && r == 0)
|
2010-05-01 02:22:48 +08:00
|
|
|
syslog(lvl, "%s: %s %s %s %s `%s'", iface->name, msg, a,
|
|
|
|
tfrom, inet_ntoa(addr), dhcp->servername);
|
2010-04-30 11:16:39 +08:00
|
|
|
else {
|
2010-05-01 02:22:48 +08:00
|
|
|
if (r != 0) {
|
|
|
|
tfrom = "via";
|
2010-04-30 11:16:39 +08:00
|
|
|
addr = *from;
|
2010-05-01 02:22:48 +08:00
|
|
|
}
|
2009-07-11 23:14:31 +08:00
|
|
|
if (a == NULL)
|
2010-05-01 02:22:48 +08:00
|
|
|
syslog(lvl, "%s: %s %s %s",
|
|
|
|
iface->name, msg, tfrom, inet_ntoa(addr));
|
2009-07-11 23:14:31 +08:00
|
|
|
else
|
2010-05-01 02:22:48 +08:00
|
|
|
syslog(lvl, "%s: %s %s %s %s",
|
|
|
|
iface->name, msg, a, tfrom, inet_ntoa(addr));
|
2010-04-30 11:16:39 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
free(a);
|
2008-07-03 01:05:41 +08:00
|
|
|
}
|
|
|
|
|
2009-03-11 01:28:18 +08:00
|
|
|
static int
|
|
|
|
blacklisted_ip(const struct if_options *ifo, in_addr_t addr)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ifo->blacklist_len; i += 2)
|
|
|
|
if (ifo->blacklist[i] == (addr & ifo->blacklist[i + 1]))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-05 19:33:46 +08:00
|
|
|
static int
|
|
|
|
whitelisted_ip(const struct if_options *ifo, in_addr_t addr)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (ifo->whitelist_len == 0)
|
|
|
|
return -1;
|
|
|
|
for (i = 0; i < ifo->whitelist_len; i += 2)
|
|
|
|
if (ifo->whitelist[i] == (addr & ifo->whitelist[i + 1]))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
static void
|
2010-04-30 11:03:23 +08:00
|
|
|
handle_dhcp(struct interface *iface, struct dhcp_message **dhcpp, const struct in_addr *from)
|
2008-04-19 07:12:44 +08:00
|
|
|
{
|
2008-09-02 21:28:11 +08:00
|
|
|
struct if_state *state = iface->state;
|
|
|
|
struct if_options *ifo = state->options;
|
|
|
|
struct dhcp_message *dhcp = *dhcpp;
|
|
|
|
struct dhcp_lease *lease = &state->lease;
|
|
|
|
uint8_t type, tmp;
|
2009-03-11 01:28:18 +08:00
|
|
|
struct in_addr addr;
|
2008-09-02 21:28:11 +08:00
|
|
|
size_t i;
|
2008-04-19 07:12:44 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
/* reset the message counter */
|
|
|
|
state->interval = 0;
|
|
|
|
|
2008-11-19 23:48:18 +08:00
|
|
|
/* We may have found a BOOTP server */
|
|
|
|
if (get_option_uint8(&type, dhcp, DHO_MESSAGETYPE) == -1)
|
|
|
|
type = 0;
|
2008-09-02 21:28:11 +08:00
|
|
|
|
|
|
|
if (type == DHCP_NAK) {
|
2009-05-14 03:17:21 +08:00
|
|
|
/* For NAK, only check if we require the ServerID */
|
|
|
|
if (has_option_mask(ifo->requiremask, DHO_SERVERID) &&
|
2009-07-09 01:01:38 +08:00
|
|
|
get_option_addr(&addr, dhcp, DHO_SERVERID) == -1)
|
2009-05-14 03:17:21 +08:00
|
|
|
{
|
2010-04-30 11:03:23 +08:00
|
|
|
log_dhcp(LOG_WARNING, "reject NAK", iface, dhcp, from);
|
2009-05-14 03:17:21 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* We should restart on a NAK */
|
2010-04-30 11:03:23 +08:00
|
|
|
log_dhcp(LOG_WARNING, "NAK:", iface, dhcp, from);
|
2009-07-12 04:00:03 +08:00
|
|
|
if (!(options & DHCPCD_TEST)) {
|
|
|
|
drop_config(iface, "NAK");
|
|
|
|
unlink(iface->leasefile);
|
|
|
|
}
|
2010-04-19 20:36:28 +08:00
|
|
|
close_sockets(iface);
|
2008-09-02 21:28:11 +08:00
|
|
|
/* If we constantly get NAKS then we should slowly back off */
|
|
|
|
add_timeout_sec(state->nakoff, start_interface, iface);
|
|
|
|
state->nakoff *= 2;
|
|
|
|
if (state->nakoff > NAKOFF_MAX)
|
|
|
|
state->nakoff = NAKOFF_MAX;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that all required options are present */
|
|
|
|
for (i = 1; i < 255; i++) {
|
|
|
|
if (has_option_mask(ifo->requiremask, i) &&
|
|
|
|
get_option_uint8(&tmp, dhcp, i) != 0)
|
|
|
|
{
|
2009-05-14 03:17:21 +08:00
|
|
|
/* If we are bootp, then ignore the need for serverid.
|
|
|
|
* To ignore bootp, require dhcp_message_type instead. */
|
|
|
|
if (type == 0 && i == DHO_SERVERID)
|
|
|
|
continue;
|
2010-04-30 11:03:23 +08:00
|
|
|
log_dhcp(LOG_WARNING, "reject DHCP", iface, dhcp, from);
|
2008-09-02 21:28:11 +08:00
|
|
|
return;
|
2008-04-19 07:12:44 +08:00
|
|
|
}
|
2011-03-24 09:55:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that the address offered is valid */
|
|
|
|
if ((type == 0 || type == DHCP_OFFER || type == DHCP_ACK) &&
|
|
|
|
(dhcp->ciaddr == INADDR_ANY || dhcp->ciaddr == INADDR_BROADCAST) &&
|
|
|
|
(dhcp->yiaddr == INADDR_ANY || dhcp->yiaddr == INADDR_BROADCAST))
|
|
|
|
{
|
|
|
|
log_dhcp(LOG_WARNING, "reject invalid address",
|
|
|
|
iface, dhcp, from);
|
|
|
|
return;
|
|
|
|
}
|
2009-05-14 03:17:21 +08:00
|
|
|
|
|
|
|
/* No NAK, so reset the backoff */
|
|
|
|
state->nakoff = 1;
|
2008-09-02 21:28:11 +08:00
|
|
|
|
2008-11-19 23:48:18 +08:00
|
|
|
if ((type == 0 || type == DHCP_OFFER) &&
|
|
|
|
state->state == DHS_DISCOVER)
|
|
|
|
{
|
2008-09-15 23:23:46 +08:00
|
|
|
lease->frominfo = 0;
|
2008-09-02 21:28:11 +08:00
|
|
|
lease->addr.s_addr = dhcp->yiaddr;
|
2010-02-18 06:23:17 +08:00
|
|
|
lease->cookie = dhcp->cookie;
|
2009-11-20 22:35:36 +08:00
|
|
|
if (type == 0 ||
|
|
|
|
get_option_addr(&lease->server, dhcp, DHO_SERVERID) != 0)
|
|
|
|
lease->server.s_addr = INADDR_ANY;
|
2010-04-30 11:03:23 +08:00
|
|
|
log_dhcp(LOG_INFO, "offered", iface, dhcp, from);
|
2008-09-02 21:28:11 +08:00
|
|
|
free(state->offer);
|
|
|
|
state->offer = dhcp;
|
|
|
|
*dhcpp = NULL;
|
2008-09-05 15:31:09 +08:00
|
|
|
if (options & DHCPCD_TEST) {
|
|
|
|
free(state->old);
|
|
|
|
state->old = state->new;
|
|
|
|
state->new = state->offer;
|
|
|
|
state->offer = NULL;
|
2009-01-14 01:04:28 +08:00
|
|
|
state->reason = "TEST";
|
|
|
|
run_script(iface);
|
2008-09-05 15:31:09 +08:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
delete_timeout(send_discover, iface);
|
2008-11-19 23:48:18 +08:00
|
|
|
/* We don't request BOOTP addresses */
|
|
|
|
if (type) {
|
2009-02-02 01:13:19 +08:00
|
|
|
/* We used to ARP check here, but that seems to be in
|
|
|
|
* violation of RFC2131 where it only describes
|
|
|
|
* DECLINE after REQUEST.
|
|
|
|
* It also seems that some MS DHCP servers actually
|
|
|
|
* ignore DECLINE if no REQUEST, ie we decline a
|
|
|
|
* DISCOVER. */
|
2008-12-18 18:23:40 +08:00
|
|
|
start_request(iface);
|
2008-11-19 23:48:18 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2008-11-19 23:48:18 +08:00
|
|
|
if (type) {
|
|
|
|
if (type == DHCP_OFFER) {
|
2010-04-30 11:16:39 +08:00
|
|
|
log_dhcp(LOG_INFO, "ignoring offer of",
|
|
|
|
iface, dhcp, from);
|
2008-11-19 23:48:18 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
|
2008-11-19 23:48:18 +08:00
|
|
|
/* We should only be dealing with acks */
|
|
|
|
if (type != DHCP_ACK) {
|
2010-04-30 11:16:39 +08:00
|
|
|
log_dhcp(LOG_ERR, "not ACK or OFFER",
|
|
|
|
iface, dhcp, from);
|
2008-11-19 23:48:18 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ifo->options & DHCPCD_INFORM))
|
2010-04-30 11:03:23 +08:00
|
|
|
log_dhcp(LOG_INFO, "acknowledged", iface, dhcp, from);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2008-11-20 17:57:46 +08:00
|
|
|
/* BOOTP could have already assigned this above, so check we still
|
|
|
|
* have a pointer. */
|
|
|
|
if (*dhcpp) {
|
|
|
|
free(state->offer);
|
|
|
|
state->offer = dhcp;
|
|
|
|
*dhcpp = NULL;
|
|
|
|
}
|
2009-11-20 05:53:25 +08:00
|
|
|
|
2008-09-15 23:23:46 +08:00
|
|
|
lease->frominfo = 0;
|
2009-02-02 01:13:19 +08:00
|
|
|
delete_timeout(NULL, iface);
|
2009-11-20 22:35:36 +08:00
|
|
|
|
2009-07-04 08:06:25 +08:00
|
|
|
/* We now have an offer, so close the DHCP sockets.
|
|
|
|
* This allows us to safely ARP when broken DHCP servers send an ACK
|
|
|
|
* follows by an invalid NAK. */
|
|
|
|
close_sockets(iface);
|
|
|
|
|
2009-02-02 01:13:19 +08:00
|
|
|
if (ifo->options & DHCPCD_ARP &&
|
|
|
|
iface->addr.s_addr != state->offer->yiaddr)
|
|
|
|
{
|
|
|
|
/* If the interface already has the address configured
|
|
|
|
* then we can't ARP for duplicate detection. */
|
|
|
|
addr.s_addr = state->offer->yiaddr;
|
2009-04-27 22:00:39 +08:00
|
|
|
if (has_address(iface->name, &addr, NULL) != 1) {
|
2009-02-02 01:13:19 +08:00
|
|
|
state->claims = 0;
|
|
|
|
state->probes = 0;
|
|
|
|
state->conflicts = 0;
|
|
|
|
state->state = DHS_PROBE;
|
|
|
|
send_arp_probe(iface);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
bind_interface(iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-09-04 00:49:28 +08:00
|
|
|
handle_dhcp_packet(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
struct interface *iface = arg;
|
2008-09-02 21:28:11 +08:00
|
|
|
uint8_t *packet;
|
|
|
|
struct dhcp_message *dhcp = NULL;
|
|
|
|
const uint8_t *pp;
|
|
|
|
ssize_t bytes;
|
2009-03-11 01:28:18 +08:00
|
|
|
struct in_addr from;
|
2011-03-25 01:59:20 +08:00
|
|
|
int i, partialcsum = 0;
|
2008-09-02 21:28:11 +08:00
|
|
|
|
|
|
|
/* We loop through until our buffer is empty.
|
|
|
|
* The benefit is that if we get >1 DHCP packet in our buffer and
|
|
|
|
* the first one fails for any reason, we can use the next. */
|
|
|
|
packet = xmalloc(udp_dhcp_len);
|
|
|
|
for(;;) {
|
|
|
|
bytes = get_raw_packet(iface, ETHERTYPE_IP,
|
2011-03-25 01:59:20 +08:00
|
|
|
packet, udp_dhcp_len, &partialcsum);
|
2008-09-02 21:28:11 +08:00
|
|
|
if (bytes == 0 || bytes == -1)
|
2008-04-19 07:12:44 +08:00
|
|
|
break;
|
2011-03-25 01:59:20 +08:00
|
|
|
if (valid_udp_packet(packet, bytes, &from, partialcsum) == -1) {
|
2009-03-11 01:28:18 +08:00
|
|
|
syslog(LOG_ERR, "%s: invalid UDP packet from %s",
|
|
|
|
iface->name, inet_ntoa(from));
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
2009-03-11 01:28:18 +08:00
|
|
|
}
|
2009-07-05 19:33:46 +08:00
|
|
|
i = whitelisted_ip(iface->state->options, from.s_addr);
|
|
|
|
if (i == 0) {
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"%s: non whitelisted DHCP packet from %s",
|
|
|
|
iface->name, inet_ntoa(from));
|
|
|
|
continue;
|
|
|
|
} else if (i != 1 &&
|
|
|
|
blacklisted_ip(iface->state->options, from.s_addr) == 1)
|
|
|
|
{
|
2009-03-11 01:28:18 +08:00
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"%s: blacklisted DHCP packet from %s",
|
|
|
|
iface->name, inet_ntoa(from));
|
|
|
|
continue;
|
|
|
|
}
|
2009-03-24 04:10:02 +08:00
|
|
|
if (iface->flags & IFF_POINTOPOINT &&
|
2009-03-24 06:02:37 +08:00
|
|
|
iface->dst.s_addr != from.s_addr)
|
2009-03-24 04:10:02 +08:00
|
|
|
{
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"%s: server %s is not destination",
|
|
|
|
iface->name, inet_ntoa(from));
|
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
bytes = get_udp_data(&pp, packet);
|
|
|
|
if ((size_t)bytes > sizeof(*dhcp)) {
|
2009-03-11 01:28:18 +08:00
|
|
|
syslog(LOG_ERR,
|
|
|
|
"%s: packet greater than DHCP size from %s",
|
|
|
|
iface->name, inet_ntoa(from));
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
2008-04-19 07:12:44 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
if (!dhcp)
|
2009-01-31 04:18:35 +08:00
|
|
|
dhcp = xzalloc(sizeof(*dhcp));
|
2008-09-02 21:28:11 +08:00
|
|
|
memcpy(dhcp, pp, bytes);
|
|
|
|
if (dhcp->cookie != htonl(MAGIC_COOKIE)) {
|
2009-03-11 01:28:18 +08:00
|
|
|
syslog(LOG_DEBUG, "%s: bogus cookie from %s",
|
|
|
|
iface->name, inet_ntoa(from));
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
2008-04-19 07:12:44 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
/* Ensure it's the right transaction */
|
|
|
|
if (iface->state->xid != dhcp->xid) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_DEBUG,
|
2009-03-11 01:28:18 +08:00
|
|
|
"%s: wrong xid 0x%x (expecting 0x%x) from %s",
|
|
|
|
iface->name, dhcp->xid, iface->state->xid,
|
|
|
|
inet_ntoa(from));
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
2008-07-09 00:02:51 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
/* Ensure packet is for us */
|
|
|
|
if (iface->hwlen <= sizeof(dhcp->chaddr) &&
|
|
|
|
memcmp(dhcp->chaddr, iface->hwaddr, iface->hwlen))
|
2008-08-18 20:43:05 +08:00
|
|
|
{
|
2009-03-11 01:28:18 +08:00
|
|
|
syslog(LOG_DEBUG, "%s: xid 0x%x is not for hwaddr %s",
|
2009-02-12 01:56:22 +08:00
|
|
|
iface->name, dhcp->xid,
|
|
|
|
hwaddr_ntoa(dhcp->chaddr, sizeof(dhcp->chaddr)));
|
2008-09-02 21:28:11 +08:00
|
|
|
continue;
|
2008-07-28 21:21:47 +08:00
|
|
|
}
|
2010-04-30 11:03:23 +08:00
|
|
|
handle_dhcp(iface, &dhcp, &from);
|
2008-09-02 21:28:11 +08:00
|
|
|
if (iface->raw_fd == -1)
|
|
|
|
break;
|
2008-04-19 07:12:44 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
free(packet);
|
|
|
|
free(dhcp);
|
2008-04-19 07:12:44 +08:00
|
|
|
}
|
|
|
|
|
2008-09-16 21:17:23 +08:00
|
|
|
static void
|
|
|
|
send_release(struct interface *iface)
|
|
|
|
{
|
2009-11-24 09:15:08 +08:00
|
|
|
struct timespec ts;
|
|
|
|
|
2010-02-18 06:23:17 +08:00
|
|
|
if (iface->state->new != NULL &&
|
|
|
|
iface->state->new->cookie == htonl(MAGIC_COOKIE))
|
2008-09-16 21:17:23 +08:00
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "%s: releasing lease of %s",
|
2009-02-12 01:56:22 +08:00
|
|
|
iface->name, inet_ntoa(iface->state->lease.addr));
|
2010-01-14 10:04:41 +08:00
|
|
|
iface->state->xid = dhcp_xid(iface);
|
2008-09-16 21:17:23 +08:00
|
|
|
send_message(iface, DHCP_RELEASE, NULL);
|
2009-11-24 09:15:08 +08:00
|
|
|
/* Give the packet a chance to go before dropping the ip */
|
2009-12-03 21:30:09 +08:00
|
|
|
ts.tv_sec = RELEASE_DELAY_S;
|
|
|
|
ts.tv_nsec = RELEASE_DELAY_NS;
|
2009-11-24 09:15:08 +08:00
|
|
|
nanosleep(&ts, NULL);
|
2009-01-21 00:33:11 +08:00
|
|
|
drop_config(iface, "RELEASE");
|
2008-09-16 21:17:23 +08:00
|
|
|
}
|
2009-07-12 05:19:42 +08:00
|
|
|
unlink(iface->leasefile);
|
2008-09-16 21:17:23 +08:00
|
|
|
}
|
|
|
|
|
2009-10-10 03:00:34 +08:00
|
|
|
void
|
|
|
|
send_decline(struct interface *iface)
|
|
|
|
{
|
|
|
|
send_message(iface, DHCP_DECLINE, NULL);
|
|
|
|
}
|
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
static void
|
2009-03-31 16:35:38 +08:00
|
|
|
configure_interface1(struct interface *iface)
|
2009-01-02 04:51:04 +08:00
|
|
|
{
|
|
|
|
struct if_state *ifs = iface->state;
|
2009-03-31 16:35:38 +08:00
|
|
|
struct if_options *ifo = ifs->options;
|
2009-01-02 04:51:04 +08:00
|
|
|
uint8_t *duid;
|
|
|
|
size_t len = 0, ifl;
|
|
|
|
|
2010-03-05 23:47:46 +08:00
|
|
|
/* Do any platform specific configuration */
|
|
|
|
if_conf(iface);
|
|
|
|
|
2009-03-24 04:10:02 +08:00
|
|
|
if (iface->flags & IFF_POINTOPOINT && !(ifo->options & DHCPCD_INFORM))
|
|
|
|
ifo->options |= DHCPCD_STATIC;
|
2009-03-24 06:02:37 +08:00
|
|
|
if (iface->flags & IFF_NOARP ||
|
|
|
|
ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))
|
|
|
|
ifo->options &= ~(DHCPCD_ARP | DHCPCD_IPV4LL);
|
2009-10-14 23:16:07 +08:00
|
|
|
if (ifo->options & DHCPCD_LINK && carrier_status(iface) == -1)
|
2009-03-20 01:52:12 +08:00
|
|
|
ifo->options &= ~DHCPCD_LINK;
|
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
if (ifo->metric != -1)
|
|
|
|
iface->metric = ifo->metric;
|
|
|
|
|
|
|
|
/* If we haven't specified a ClientID and our hardware address
|
|
|
|
* length is greater than DHCP_CHADDR_LEN then we enforce a ClientID
|
|
|
|
* of the hardware address family and the hardware address. */
|
|
|
|
if (iface->hwlen > DHCP_CHADDR_LEN)
|
|
|
|
ifo->options |= DHCPCD_CLIENTID;
|
|
|
|
|
2010-01-29 04:12:54 +08:00
|
|
|
/* Firewire and InfiniBand interfaces require ClientID and
|
|
|
|
* the broadcast option being set. */
|
|
|
|
switch (iface->family) {
|
|
|
|
case ARPHRD_IEEE1394: /* FALLTHROUGH */
|
|
|
|
case ARPHRD_INFINIBAND:
|
|
|
|
ifo->options |= DHCPCD_CLIENTID | DHCPCD_BROADCAST;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
free(iface->clientid);
|
2010-06-04 07:28:13 +08:00
|
|
|
iface->clientid = NULL;
|
2009-01-02 04:51:04 +08:00
|
|
|
if (*ifo->clientid) {
|
|
|
|
iface->clientid = xmalloc(ifo->clientid[0] + 1);
|
|
|
|
memcpy(iface->clientid, ifo->clientid, ifo->clientid[0] + 1);
|
|
|
|
} else if (ifo->options & DHCPCD_CLIENTID) {
|
|
|
|
if (ifo->options & DHCPCD_DUID) {
|
|
|
|
duid = xmalloc(DUID_LEN);
|
|
|
|
if ((len = get_duid(duid, iface)) == 0)
|
|
|
|
syslog(LOG_ERR, "get_duid: %m");
|
|
|
|
}
|
|
|
|
if (len > 0) {
|
|
|
|
iface->clientid = xmalloc(len + 6);
|
|
|
|
iface->clientid[0] = len + 5;
|
|
|
|
iface->clientid[1] = 255; /* RFC 4361 */
|
|
|
|
ifl = strlen(iface->name);
|
|
|
|
if (ifl < 5) {
|
|
|
|
memcpy(iface->clientid + 2, iface->name, ifl);
|
|
|
|
if (ifl < 4)
|
|
|
|
memset(iface->clientid + 2 + ifl,
|
2009-02-12 01:56:22 +08:00
|
|
|
0, 4 - ifl);
|
2009-01-02 04:51:04 +08:00
|
|
|
} else {
|
|
|
|
ifl = htonl(if_nametoindex(iface->name));
|
|
|
|
memcpy(iface->clientid + 2, &ifl, 4);
|
|
|
|
}
|
|
|
|
} else if (len == 0) {
|
|
|
|
len = iface->hwlen + 1;
|
|
|
|
iface->clientid = xmalloc(len + 1);
|
|
|
|
iface->clientid[0] = len;
|
|
|
|
iface->clientid[1] = iface->family;
|
|
|
|
memcpy(iface->clientid + 2, iface->hwaddr,
|
2009-02-12 01:56:22 +08:00
|
|
|
iface->hwlen);
|
2009-01-02 04:51:04 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-26 16:00:25 +08:00
|
|
|
if (ifo->options & DHCPCD_CLIENTID)
|
|
|
|
syslog(LOG_DEBUG, "%s: using ClientID %s", iface->name,
|
|
|
|
hwaddr_ntoa(iface->clientid + 1, *iface->clientid));
|
2010-11-09 08:35:40 +08:00
|
|
|
else
|
|
|
|
syslog(LOG_DEBUG, "%s: using hwaddr %s", iface->name,
|
|
|
|
hwaddr_ntoa(iface->hwaddr, iface->hwlen));
|
2009-01-02 04:51:04 +08:00
|
|
|
}
|
|
|
|
|
2009-03-31 16:35:38 +08:00
|
|
|
int
|
|
|
|
select_profile(struct interface *iface, const char *profile)
|
|
|
|
{
|
|
|
|
struct if_options *ifo;
|
2010-11-09 08:34:38 +08:00
|
|
|
int ret;
|
2009-03-31 16:35:38 +08:00
|
|
|
|
2010-11-09 08:34:38 +08:00
|
|
|
ret = 0;
|
2009-03-31 16:35:38 +08:00
|
|
|
ifo = read_config(cffile, iface->name, iface->ssid, profile);
|
|
|
|
if (ifo == NULL) {
|
|
|
|
syslog(LOG_DEBUG, "%s: no profile %s", iface->name, profile);
|
2010-11-09 08:34:38 +08:00
|
|
|
ret = -1;
|
|
|
|
goto exit;
|
2009-03-31 16:35:38 +08:00
|
|
|
}
|
|
|
|
if (profile != NULL) {
|
|
|
|
strlcpy(iface->state->profile, profile,
|
|
|
|
sizeof(iface->state->profile));
|
|
|
|
syslog(LOG_INFO, "%s: selected profile %s",
|
|
|
|
iface->name, profile);
|
|
|
|
} else
|
|
|
|
*iface->state->profile = '\0';
|
|
|
|
free_options(iface->state->options);
|
|
|
|
iface->state->options = ifo;
|
2010-11-09 08:34:38 +08:00
|
|
|
|
|
|
|
exit:
|
|
|
|
if (profile)
|
|
|
|
configure_interface1(iface);
|
|
|
|
return ret;
|
2009-03-31 16:35:38 +08:00
|
|
|
}
|
|
|
|
|
2009-04-19 05:43:23 +08:00
|
|
|
static void
|
|
|
|
start_fallback(void *arg)
|
|
|
|
{
|
|
|
|
struct interface *iface;
|
|
|
|
|
|
|
|
iface = (struct interface *)arg;
|
|
|
|
select_profile(iface, iface->state->options->fallback);
|
|
|
|
start_interface(iface);
|
|
|
|
}
|
|
|
|
|
2009-03-31 16:35:38 +08:00
|
|
|
static void
|
|
|
|
configure_interface(struct interface *iface, int argc, char **argv)
|
|
|
|
{
|
|
|
|
select_profile(iface, NULL);
|
|
|
|
add_options(iface->state->options, argc, argv);
|
|
|
|
configure_interface1(iface);
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
static void
|
2008-09-04 22:08:14 +08:00
|
|
|
handle_carrier(const char *ifname)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 22:08:14 +08:00
|
|
|
struct interface *iface;
|
2009-10-14 23:16:07 +08:00
|
|
|
int carrier;
|
2008-09-04 22:08:14 +08:00
|
|
|
|
2009-04-19 05:43:23 +08:00
|
|
|
if (!(options & DHCPCD_LINK))
|
|
|
|
return;
|
2008-09-04 22:08:14 +08:00
|
|
|
for (iface = ifaces; iface; iface = iface->next)
|
|
|
|
if (strcmp(iface->name, ifname) == 0)
|
|
|
|
break;
|
|
|
|
if (!iface || !(iface->state->options->options & DHCPCD_LINK))
|
2008-09-02 21:28:11 +08:00
|
|
|
return;
|
2009-10-14 23:16:07 +08:00
|
|
|
carrier = carrier_status(iface);
|
|
|
|
if (carrier == -1)
|
|
|
|
syslog(LOG_ERR, "%s: carrier_status: %m", ifname);
|
|
|
|
else if (carrier == 0 || !(iface->flags & IFF_RUNNING)) {
|
2008-09-10 01:07:48 +08:00
|
|
|
if (iface->carrier != LINK_DOWN) {
|
|
|
|
iface->carrier = LINK_DOWN;
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_INFO, "%s: carrier lost", iface->name);
|
2008-09-04 19:30:11 +08:00
|
|
|
close_sockets(iface);
|
|
|
|
delete_timeouts(iface, start_expire, NULL);
|
2009-01-15 21:31:46 +08:00
|
|
|
drop_config(iface, "NOCARRIER");
|
2008-09-04 19:30:11 +08:00
|
|
|
}
|
2009-10-14 23:16:07 +08:00
|
|
|
} else if (carrier == 1 && (iface->flags & IFF_RUNNING)) {
|
2008-09-10 01:07:48 +08:00
|
|
|
if (iface->carrier != LINK_UP) {
|
|
|
|
iface->carrier = LINK_UP;
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_INFO, "%s: carrier acquired", iface->name);
|
2009-03-31 16:35:38 +08:00
|
|
|
if (iface->wireless)
|
|
|
|
getifssid(iface->name, iface->ssid);
|
|
|
|
configure_interface(iface, margc, margv);
|
2009-03-03 07:24:34 +08:00
|
|
|
iface->state->interval = 0;
|
2009-01-15 21:31:46 +08:00
|
|
|
iface->state->reason = "CARRIER";
|
2009-02-24 23:41:15 +08:00
|
|
|
run_script(iface);
|
2008-09-04 19:30:11 +08:00
|
|
|
start_interface(iface);
|
2008-04-21 17:40:14 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
}
|
2008-04-21 17:40:14 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
void
|
2008-09-04 00:49:28 +08:00
|
|
|
start_discover(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
struct interface *iface = arg;
|
2008-09-02 21:28:11 +08:00
|
|
|
struct if_options *ifo = iface->state->options;
|
|
|
|
|
2008-09-15 23:23:46 +08:00
|
|
|
iface->state->state = DHS_DISCOVER;
|
2010-01-14 10:04:41 +08:00
|
|
|
iface->state->xid = dhcp_xid(iface);
|
2008-09-02 21:28:11 +08:00
|
|
|
delete_timeout(NULL, iface);
|
2009-04-19 05:43:23 +08:00
|
|
|
if (ifo->fallback)
|
|
|
|
add_timeout_sec(ifo->timeout, start_fallback, iface);
|
|
|
|
else if (ifo->options & DHCPCD_IPV4LL &&
|
2008-09-02 21:28:11 +08:00
|
|
|
!IN_LINKLOCAL(htonl(iface->addr.s_addr)))
|
|
|
|
{
|
|
|
|
if (IN_LINKLOCAL(htonl(iface->state->fail.s_addr)))
|
|
|
|
add_timeout_sec(RATE_LIMIT_INTERVAL, start_ipv4ll, iface);
|
|
|
|
else
|
|
|
|
add_timeout_sec(ifo->timeout, start_ipv4ll, iface);
|
2008-04-21 17:40:14 +08:00
|
|
|
}
|
2011-03-26 04:19:03 +08:00
|
|
|
if (ifo->options & DHCPCD_REQUEST)
|
|
|
|
syslog(LOG_INFO, "%s: broadcasting for a lease (requesting %s)",
|
|
|
|
iface->name, inet_ntoa(ifo->req_addr));
|
|
|
|
else
|
|
|
|
syslog(LOG_INFO, "%s: broadcasting for a lease", iface->name);
|
2008-09-02 21:28:11 +08:00
|
|
|
send_discover(iface);
|
|
|
|
}
|
|
|
|
|
2008-12-18 18:23:40 +08:00
|
|
|
void
|
|
|
|
start_request(void *arg)
|
|
|
|
{
|
|
|
|
struct interface *iface = arg;
|
|
|
|
|
|
|
|
iface->state->state = DHS_REQUEST;
|
|
|
|
send_request(iface);
|
|
|
|
}
|
2008-09-08 19:28:12 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
start_renew(void *arg)
|
|
|
|
{
|
|
|
|
struct interface *iface = arg;
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "%s: renewing lease of %s",
|
2009-02-12 01:56:22 +08:00
|
|
|
iface->name, inet_ntoa(iface->state->lease.addr));
|
2008-09-15 23:23:46 +08:00
|
|
|
iface->state->state = DHS_RENEW;
|
2010-01-14 10:04:41 +08:00
|
|
|
iface->state->xid = dhcp_xid(iface);
|
2008-09-08 19:28:12 +08:00
|
|
|
send_renew(iface);
|
|
|
|
}
|
|
|
|
|
2009-05-14 03:17:21 +08:00
|
|
|
void
|
|
|
|
start_rebind(void *arg)
|
|
|
|
{
|
|
|
|
struct interface *iface = arg;
|
|
|
|
|
2009-10-25 06:36:56 +08:00
|
|
|
syslog(LOG_ERR, "%s: failed to renew, attempting to rebind",
|
2009-05-14 03:17:21 +08:00
|
|
|
iface->name);
|
|
|
|
iface->state->state = DHS_REBIND;
|
|
|
|
delete_timeout(send_renew, iface);
|
|
|
|
iface->state->lease.server.s_addr = 0;
|
|
|
|
send_rebind(iface);
|
|
|
|
}
|
|
|
|
|
2008-09-15 23:23:46 +08:00
|
|
|
static void
|
|
|
|
start_timeout(void *arg)
|
|
|
|
{
|
|
|
|
struct interface *iface = arg;
|
|
|
|
|
|
|
|
bind_interface(iface);
|
|
|
|
iface->state->interval = 0;
|
|
|
|
start_discover(iface);
|
|
|
|
}
|
|
|
|
|
2009-03-06 00:35:03 +08:00
|
|
|
static struct dhcp_message *
|
|
|
|
dhcp_message_new(struct in_addr *addr, struct in_addr *mask)
|
|
|
|
{
|
|
|
|
struct dhcp_message *dhcp;
|
|
|
|
uint8_t *p;
|
|
|
|
|
|
|
|
dhcp = xzalloc(sizeof(*dhcp));
|
|
|
|
dhcp->yiaddr = addr->s_addr;
|
|
|
|
p = dhcp->options;
|
|
|
|
if (mask && mask->s_addr != INADDR_ANY) {
|
|
|
|
*p++ = DHO_SUBNETMASK;
|
|
|
|
*p++ = sizeof(mask->s_addr);
|
|
|
|
memcpy(p, &mask->s_addr, sizeof(mask->s_addr));
|
2009-03-20 01:52:12 +08:00
|
|
|
p+= sizeof(mask->s_addr);
|
2009-03-06 00:35:03 +08:00
|
|
|
}
|
|
|
|
*p++ = DHO_END;
|
|
|
|
return dhcp;
|
|
|
|
}
|
|
|
|
|
2009-03-20 01:52:12 +08:00
|
|
|
static int
|
|
|
|
handle_3rdparty(struct interface *iface)
|
|
|
|
{
|
|
|
|
struct if_options *ifo;
|
|
|
|
struct in_addr addr, net, dst;
|
|
|
|
|
|
|
|
ifo = iface->state->options;
|
|
|
|
if (ifo->req_addr.s_addr != INADDR_ANY)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (get_address(iface->name, &addr, &net, &dst) == 1)
|
|
|
|
handle_ifa(RTM_NEWADDR, iface->name, &addr, &net, &dst);
|
|
|
|
else {
|
|
|
|
syslog(LOG_INFO,
|
2009-03-24 04:10:02 +08:00
|
|
|
"%s: waiting for 3rd party to configure IP address",
|
|
|
|
iface->name);
|
2009-03-20 01:52:12 +08:00
|
|
|
iface->state->reason = "3RDPARTY";
|
|
|
|
run_script(iface);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-03-06 00:35:03 +08:00
|
|
|
static void
|
|
|
|
start_static(struct interface *iface)
|
|
|
|
{
|
2009-03-20 01:52:12 +08:00
|
|
|
struct if_options *ifo;
|
2009-03-06 00:35:03 +08:00
|
|
|
|
2009-03-20 01:52:12 +08:00
|
|
|
if (handle_3rdparty(iface))
|
|
|
|
return;
|
|
|
|
ifo = iface->state->options;
|
|
|
|
iface->state->offer =
|
|
|
|
dhcp_message_new(&ifo->req_addr, &ifo->req_mask);
|
2009-03-06 00:35:03 +08:00
|
|
|
delete_timeout(NULL, iface);
|
|
|
|
bind_interface(iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
start_inform(struct interface *iface)
|
|
|
|
{
|
2009-03-20 01:52:12 +08:00
|
|
|
if (handle_3rdparty(iface))
|
|
|
|
return;
|
2009-03-06 00:35:03 +08:00
|
|
|
|
2009-07-12 02:54:43 +08:00
|
|
|
if (options & DHCPCD_TEST) {
|
|
|
|
iface->addr.s_addr = iface->state->options->req_addr.s_addr;
|
|
|
|
iface->net.s_addr = iface->state->options->req_mask.s_addr;
|
|
|
|
} else {
|
|
|
|
iface->state->options->options |= DHCPCD_STATIC;
|
|
|
|
start_static(iface);
|
|
|
|
}
|
2009-03-06 00:35:03 +08:00
|
|
|
|
|
|
|
iface->state->state = DHS_INFORM;
|
2010-01-14 10:04:41 +08:00
|
|
|
iface->state->xid = dhcp_xid(iface);
|
2009-03-06 00:35:03 +08:00
|
|
|
send_inform(iface);
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
void
|
|
|
|
start_reboot(struct interface *iface)
|
|
|
|
{
|
|
|
|
struct if_options *ifo = iface->state->options;
|
|
|
|
|
2008-09-10 01:07:48 +08:00
|
|
|
if (ifo->options & DHCPCD_LINK && iface->carrier == LINK_DOWN) {
|
2008-09-07 03:41:05 +08:00
|
|
|
syslog(LOG_INFO, "%s: waiting for carrier", iface->name);
|
|
|
|
return;
|
|
|
|
}
|
2009-03-06 00:35:03 +08:00
|
|
|
if (ifo->options & DHCPCD_STATIC) {
|
|
|
|
start_static(iface);
|
|
|
|
return;
|
|
|
|
}
|
2010-02-18 06:23:17 +08:00
|
|
|
if (ifo->reboot == 0 || iface->state->offer == NULL) {
|
2008-09-15 23:23:46 +08:00
|
|
|
start_discover(iface);
|
|
|
|
return;
|
|
|
|
}
|
2010-02-18 06:23:17 +08:00
|
|
|
if (ifo->options & DHCPCD_INFORM) {
|
|
|
|
syslog(LOG_INFO, "%s: informing address of %s",
|
|
|
|
iface->name, inet_ntoa(iface->state->lease.addr));
|
|
|
|
} else if (iface->state->offer->cookie == 0) {
|
2008-09-15 23:23:46 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV4LL) {
|
|
|
|
iface->state->claims = 0;
|
|
|
|
send_arp_announce(iface);
|
|
|
|
} else
|
|
|
|
start_discover(iface);
|
|
|
|
return;
|
2008-10-19 00:41:19 +08:00
|
|
|
} else {
|
|
|
|
syslog(LOG_INFO, "%s: rebinding lease of %s",
|
2009-02-12 01:56:22 +08:00
|
|
|
iface->name, inet_ntoa(iface->state->lease.addr));
|
2008-10-19 00:41:19 +08:00
|
|
|
}
|
2008-09-15 23:23:46 +08:00
|
|
|
iface->state->state = DHS_REBOOT;
|
2010-01-14 10:04:41 +08:00
|
|
|
iface->state->xid = dhcp_xid(iface);
|
2008-09-02 21:28:11 +08:00
|
|
|
iface->state->lease.server.s_addr = 0;
|
|
|
|
delete_timeout(NULL, iface);
|
2009-04-19 05:43:23 +08:00
|
|
|
if (ifo->fallback)
|
|
|
|
add_timeout_sec(ifo->reboot, start_fallback, iface);
|
|
|
|
else if (ifo->options & DHCPCD_LASTLEASE &&
|
|
|
|
iface->state->lease.frominfo)
|
2008-09-15 23:23:46 +08:00
|
|
|
add_timeout_sec(ifo->reboot, start_timeout, iface);
|
2009-03-06 00:35:03 +08:00
|
|
|
else if (!(ifo->options & DHCPCD_INFORM &&
|
|
|
|
options & (DHCPCD_MASTER | DHCPCD_DAEMONISED)))
|
2008-09-15 23:23:46 +08:00
|
|
|
add_timeout_sec(ifo->reboot, start_expire, iface);
|
2009-02-02 01:13:19 +08:00
|
|
|
/* Don't bother ARP checking as the server could NAK us first. */
|
|
|
|
if (ifo->options & DHCPCD_INFORM)
|
2008-10-19 00:41:19 +08:00
|
|
|
send_inform(iface);
|
|
|
|
else
|
2008-09-15 23:23:46 +08:00
|
|
|
send_request(iface);
|
2008-04-21 17:40:14 +08:00
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
void
|
2008-09-04 00:49:28 +08:00
|
|
|
start_interface(void *arg)
|
2006-11-28 04:23:22 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
struct interface *iface = arg;
|
2008-10-18 18:52:01 +08:00
|
|
|
struct if_options *ifo = iface->state->options;
|
2008-09-15 23:23:46 +08:00
|
|
|
struct stat st;
|
|
|
|
struct timeval now;
|
|
|
|
uint32_t l;
|
2010-09-10 02:27:00 +08:00
|
|
|
int nolease;
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2009-03-20 01:52:12 +08:00
|
|
|
handle_carrier(iface->name);
|
2008-09-10 01:07:48 +08:00
|
|
|
if (iface->carrier == LINK_DOWN) {
|
|
|
|
syslog(LOG_INFO, "%s: waiting for carrier", iface->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
iface->start_uptime = uptime();
|
2008-11-20 17:57:46 +08:00
|
|
|
free(iface->state->offer);
|
|
|
|
iface->state->offer = NULL;
|
|
|
|
|
2009-03-31 16:35:38 +08:00
|
|
|
if (iface->state->arping_index < ifo->arping_len) {
|
|
|
|
start_arping(iface);
|
|
|
|
return;
|
|
|
|
}
|
2009-01-01 22:23:36 +08:00
|
|
|
if (ifo->options & DHCPCD_STATIC) {
|
|
|
|
start_static(iface);
|
|
|
|
return;
|
|
|
|
}
|
2009-03-06 00:35:03 +08:00
|
|
|
if (ifo->options & DHCPCD_INFORM) {
|
|
|
|
start_inform(iface);
|
|
|
|
return;
|
|
|
|
}
|
2009-03-20 01:52:12 +08:00
|
|
|
if (iface->hwlen == 0 && ifo->clientid[0] == '\0') {
|
|
|
|
syslog(LOG_WARNING, "%s: needs a clientid to configure",
|
2009-03-23 19:15:35 +08:00
|
|
|
iface->name);
|
|
|
|
drop_config(iface, "FAIL");
|
|
|
|
close_sockets(iface);
|
|
|
|
delete_timeout(NULL, iface);
|
2009-03-20 01:52:12 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-12-24 06:18:56 +08:00
|
|
|
/* We don't want to read the old lease if we NAK an old test */
|
|
|
|
nolease = iface->state->offer && options & DHCPCD_TEST;
|
|
|
|
if (!nolease)
|
2008-11-14 17:39:09 +08:00
|
|
|
iface->state->offer = read_lease(iface);
|
2008-09-15 23:23:46 +08:00
|
|
|
if (iface->state->offer) {
|
|
|
|
get_lease(&iface->state->lease, iface->state->offer);
|
|
|
|
iface->state->lease.frominfo = 1;
|
2010-02-18 06:23:17 +08:00
|
|
|
if (iface->state->offer->cookie == 0) {
|
2009-02-12 01:56:22 +08:00
|
|
|
if (iface->state->offer->yiaddr ==
|
|
|
|
iface->addr.s_addr)
|
|
|
|
{
|
2008-11-14 17:39:09 +08:00
|
|
|
free(iface->state->offer);
|
|
|
|
iface->state->offer = NULL;
|
|
|
|
}
|
2010-01-14 10:23:19 +08:00
|
|
|
} else if (iface->state->lease.leasetime != ~0U &&
|
|
|
|
stat(iface->leasefile, &st) == 0)
|
2008-09-15 23:23:46 +08:00
|
|
|
{
|
2008-11-14 17:39:09 +08:00
|
|
|
/* Offset lease times and check expiry */
|
2008-09-15 23:23:46 +08:00
|
|
|
gettimeofday(&now, NULL);
|
2010-01-14 10:23:19 +08:00
|
|
|
if ((time_t)iface->state->lease.leasetime <
|
|
|
|
now.tv_sec - st.st_mtime)
|
|
|
|
{
|
2010-01-26 16:07:21 +08:00
|
|
|
syslog(LOG_DEBUG,
|
2010-04-30 11:16:39 +08:00
|
|
|
"%s: discarding expired lease",
|
|
|
|
iface->name);
|
2008-09-15 23:23:46 +08:00
|
|
|
free(iface->state->offer);
|
|
|
|
iface->state->offer = NULL;
|
2010-01-27 19:51:30 +08:00
|
|
|
iface->state->lease.addr.s_addr = 0;
|
2008-09-15 23:23:46 +08:00
|
|
|
} else {
|
|
|
|
l = now.tv_sec - st.st_mtime;
|
|
|
|
iface->state->lease.leasetime -= l;
|
|
|
|
iface->state->lease.renewaltime -= l;
|
|
|
|
iface->state->lease.rebindtime -= l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-18 06:23:17 +08:00
|
|
|
if (iface->state->offer == NULL)
|
2008-09-02 21:28:11 +08:00
|
|
|
start_discover(iface);
|
2010-02-18 06:23:17 +08:00
|
|
|
else if (iface->state->offer->cookie == 0 &&
|
2009-02-12 01:56:22 +08:00
|
|
|
iface->state->options->options & DHCPCD_IPV4LL)
|
2008-09-02 21:28:11 +08:00
|
|
|
start_ipv4ll(iface);
|
|
|
|
else
|
|
|
|
start_reboot(iface);
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
static void
|
|
|
|
init_state(struct interface *iface, int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct if_state *ifs;
|
|
|
|
|
2009-02-12 01:56:22 +08:00
|
|
|
if (iface->state)
|
2008-09-04 00:49:28 +08:00
|
|
|
ifs = iface->state;
|
2009-02-12 01:56:22 +08:00
|
|
|
else
|
2008-09-04 00:49:28 +08:00
|
|
|
ifs = iface->state = xzalloc(sizeof(*ifs));
|
|
|
|
|
|
|
|
ifs->state = DHS_INIT;
|
2009-01-14 01:04:28 +08:00
|
|
|
ifs->reason = "PREINIT";
|
2008-09-04 00:49:28 +08:00
|
|
|
ifs->nakoff = 1;
|
|
|
|
configure_interface(iface, argc, argv);
|
2009-01-15 21:31:46 +08:00
|
|
|
if (!(options & DHCPCD_TEST))
|
2009-02-24 23:41:15 +08:00
|
|
|
run_script(iface);
|
2010-12-24 06:18:56 +08:00
|
|
|
/* We need to drop the leasefile so that start_interface
|
|
|
|
* doesn't load it. */
|
|
|
|
if (ifs->options->options & DHCPCD_REQUEST)
|
|
|
|
unlink(iface->leasefile);
|
2008-09-04 00:49:28 +08:00
|
|
|
|
|
|
|
if (ifs->options->options & DHCPCD_LINK) {
|
2009-10-14 23:16:07 +08:00
|
|
|
switch (carrier_status(iface)) {
|
2008-04-24 21:18:59 +08:00
|
|
|
case 0:
|
2008-09-10 01:07:48 +08:00
|
|
|
iface->carrier = LINK_DOWN;
|
2009-01-15 21:31:46 +08:00
|
|
|
ifs->reason = "NOCARRIER";
|
2008-04-24 21:18:59 +08:00
|
|
|
break;
|
2008-09-02 21:28:11 +08:00
|
|
|
case 1:
|
2008-09-10 01:07:48 +08:00
|
|
|
iface->carrier = LINK_UP;
|
2009-01-15 21:31:46 +08:00
|
|
|
ifs->reason = "CARRIER";
|
2008-09-02 21:28:11 +08:00
|
|
|
break;
|
|
|
|
default:
|
2008-09-10 01:07:48 +08:00
|
|
|
iface->carrier = LINK_UNKNOWN;
|
2009-01-15 21:31:46 +08:00
|
|
|
return;
|
2008-04-24 21:18:59 +08:00
|
|
|
}
|
2009-01-15 21:31:46 +08:00
|
|
|
if (!(options & DHCPCD_TEST))
|
2009-02-24 23:41:15 +08:00
|
|
|
run_script(iface);
|
2008-09-10 01:07:48 +08:00
|
|
|
} else
|
|
|
|
iface->carrier = LINK_UNKNOWN;
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2008-04-24 21:18:59 +08:00
|
|
|
|
2009-03-20 01:52:12 +08:00
|
|
|
void
|
|
|
|
handle_interface(int action, const char *ifname)
|
2008-09-04 19:30:11 +08:00
|
|
|
{
|
2008-09-04 22:08:14 +08:00
|
|
|
struct interface *ifs, *ifp, *ifn, *ifl = NULL;
|
2009-09-01 15:45:11 +08:00
|
|
|
const char * const argv[] = { ifname };
|
2008-09-04 19:30:11 +08:00
|
|
|
int i;
|
|
|
|
|
2009-03-20 01:52:12 +08:00
|
|
|
if (action == -1) {
|
|
|
|
ifp = find_interface(ifname);
|
|
|
|
if (ifp != NULL)
|
|
|
|
stop_interface(ifp);
|
|
|
|
return;
|
|
|
|
} else if (action == 0) {
|
|
|
|
handle_carrier(ifname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-04 19:30:11 +08:00
|
|
|
/* If running off an interface list, check it's in it. */
|
|
|
|
if (ifc) {
|
|
|
|
for (i = 0; i < ifc; i++)
|
2008-09-04 22:08:14 +08:00
|
|
|
if (strcmp(ifv[i], ifname) == 0)
|
2008-09-04 19:30:11 +08:00
|
|
|
break;
|
|
|
|
if (i >= ifc)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-01 15:45:11 +08:00
|
|
|
ifs = discover_interfaces(-1, UNCONST(argv));
|
2009-09-01 05:51:17 +08:00
|
|
|
for (ifp = ifs; ifp; ifp = ifp->next) {
|
|
|
|
if (strcmp(ifp->name, ifname) != 0)
|
|
|
|
continue;
|
|
|
|
/* Check if we already have the interface */
|
|
|
|
for (ifn = ifaces; ifn; ifn = ifn->next) {
|
|
|
|
if (strcmp(ifn->name, ifp->name) == 0)
|
|
|
|
break;
|
|
|
|
ifl = ifn;
|
2008-09-04 22:08:14 +08:00
|
|
|
}
|
2009-10-15 15:33:24 +08:00
|
|
|
if (ifn) {
|
|
|
|
/* The flags and hwaddr could have changed */
|
|
|
|
ifn->flags = ifp->flags;
|
|
|
|
ifn->hwlen = ifp->hwlen;
|
|
|
|
if (ifp->hwlen != 0)
|
|
|
|
memcpy(ifn->hwaddr, ifp->hwaddr, ifn->hwlen);
|
|
|
|
} else {
|
|
|
|
if (ifl)
|
|
|
|
ifl->next = ifp;
|
|
|
|
else
|
|
|
|
ifaces = ifp;
|
|
|
|
}
|
2009-09-01 05:51:17 +08:00
|
|
|
init_state(ifp, 0, NULL);
|
|
|
|
start_interface(ifp);
|
2008-09-04 19:30:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 04:39:29 +08:00
|
|
|
#ifdef RTM_CHGADDR
|
2010-11-13 00:33:45 +08:00
|
|
|
void
|
|
|
|
handle_hwaddr(const char *ifname, unsigned char *hwaddr, size_t hwlen)
|
|
|
|
{
|
|
|
|
struct interface *ifp;
|
|
|
|
struct if_options *ifo;
|
|
|
|
|
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
|
|
|
if (strcmp(ifp->name, ifname) == 0 && ifp->hwlen <= hwlen) {
|
|
|
|
ifo = ifp->state->options;
|
|
|
|
if (!(ifo->options &
|
|
|
|
(DHCPCD_INFORM | DHCPCD_STATIC | DHCPCD_CLIENTID))
|
|
|
|
&& ifp->state->new != NULL &&
|
|
|
|
ifp->state->new->cookie == htonl(MAGIC_COOKIE))
|
|
|
|
{
|
|
|
|
syslog(LOG_INFO,
|
|
|
|
"%s: expiring for new hardware address",
|
|
|
|
ifp->name);
|
|
|
|
drop_config(ifp, "EXPIRE");
|
|
|
|
}
|
|
|
|
memcpy(ifp->hwaddr, hwaddr, hwlen);
|
|
|
|
ifp->hwlen = hwlen;
|
|
|
|
if (!(ifo->options &
|
|
|
|
(DHCPCD_INFORM | DHCPCD_STATIC | DHCPCD_CLIENTID)))
|
|
|
|
{
|
|
|
|
syslog(LOG_DEBUG, "%s: using hwaddr %s",
|
|
|
|
ifp->name,
|
|
|
|
hwaddr_ntoa(ifp->hwaddr, ifp->hwlen));
|
|
|
|
ifp->state->interval = 0;
|
|
|
|
ifp->state->nakoff = 1;
|
|
|
|
start_interface(ifp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(hwaddr);
|
|
|
|
}
|
2010-11-13 04:39:29 +08:00
|
|
|
#endif
|
2010-11-13 00:33:45 +08:00
|
|
|
|
2009-03-20 01:52:12 +08:00
|
|
|
void
|
|
|
|
handle_ifa(int type, const char *ifname,
|
|
|
|
struct in_addr *addr, struct in_addr *net, struct in_addr *dst)
|
|
|
|
{
|
|
|
|
struct interface *ifp;
|
|
|
|
struct if_options *ifo;
|
|
|
|
int i;
|
2008-09-04 22:08:14 +08:00
|
|
|
|
2009-03-20 01:52:12 +08:00
|
|
|
if (addr->s_addr == INADDR_ANY)
|
|
|
|
return;
|
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
|
|
|
if (strcmp(ifp->name, ifname) == 0)
|
|
|
|
break;
|
|
|
|
if (ifp == NULL)
|
|
|
|
return;
|
|
|
|
ifo = ifp->state->options;
|
|
|
|
if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)) == 0 ||
|
|
|
|
ifo->req_addr.s_addr != INADDR_ANY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case RTM_DELADDR:
|
|
|
|
if (ifp->state->new &&
|
|
|
|
ifp->state->new->yiaddr == addr->s_addr)
|
|
|
|
drop_config(ifp, "EXPIRE");
|
|
|
|
break;
|
|
|
|
case RTM_NEWADDR:
|
2009-03-24 04:10:02 +08:00
|
|
|
free(ifp->state->old);
|
|
|
|
ifp->state->old = ifp->state->new;
|
|
|
|
ifp->state->new = dhcp_message_new(addr, net);
|
2009-03-24 06:02:37 +08:00
|
|
|
ifp->dst.s_addr = dst ? dst->s_addr : INADDR_ANY;
|
2009-03-24 04:10:02 +08:00
|
|
|
if (dst) {
|
|
|
|
for (i = 1; i < 255; i++)
|
2009-03-24 06:02:37 +08:00
|
|
|
if (i != DHO_ROUTER &&
|
|
|
|
has_option_mask(ifo->dstmask, i))
|
2009-03-24 04:10:02 +08:00
|
|
|
dhcp_message_add_addr(
|
|
|
|
ifp->state->new,
|
2009-03-24 06:02:37 +08:00
|
|
|
i, *dst);
|
2009-03-24 04:10:02 +08:00
|
|
|
}
|
|
|
|
ifp->state->reason = "STATIC";
|
|
|
|
build_routes();
|
|
|
|
run_script(ifp);
|
2009-03-20 01:52:12 +08:00
|
|
|
if (ifo->options & DHCPCD_INFORM) {
|
|
|
|
ifp->state->state = DHS_INFORM;
|
2010-01-14 10:04:41 +08:00
|
|
|
ifp->state->xid = dhcp_xid(ifp);
|
2009-03-24 06:02:37 +08:00
|
|
|
ifp->state->lease.server.s_addr =
|
2009-03-31 16:35:38 +08:00
|
|
|
dst ? dst->s_addr : INADDR_ANY;
|
2009-03-20 01:52:12 +08:00
|
|
|
ifp->addr = *addr;
|
|
|
|
ifp->net = *net;
|
|
|
|
send_inform(ifp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-09-04 22:08:14 +08:00
|
|
|
}
|
|
|
|
|
2008-11-10 19:15:27 +08:00
|
|
|
/* ARGSUSED */
|
2008-09-04 19:30:11 +08:00
|
|
|
static void
|
|
|
|
handle_link(_unused void *arg)
|
|
|
|
{
|
2009-03-20 06:27:13 +08:00
|
|
|
if (manage_link(linkfd) == -1)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "manage_link: %m");
|
2008-09-04 19:30:11 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 23:26:55 +08:00
|
|
|
static void
|
2010-06-10 18:38:37 +08:00
|
|
|
if_reboot(struct interface *iface, int argc, char **argv)
|
2010-06-09 23:26:55 +08:00
|
|
|
{
|
|
|
|
const struct if_options *ifo;
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
ifo = iface->state->options;
|
|
|
|
opt = ifo->options;
|
|
|
|
configure_interface(iface, argc, argv);
|
|
|
|
ifo = iface->state->options;
|
|
|
|
iface->state->interval = 0;
|
|
|
|
if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC) &&
|
|
|
|
iface->addr.s_addr != ifo->req_addr.s_addr) ||
|
|
|
|
(opt & (DHCPCD_INFORM | DHCPCD_STATIC) &&
|
|
|
|
!(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))))
|
|
|
|
{
|
|
|
|
drop_config(iface, "EXPIRE");
|
|
|
|
} else {
|
|
|
|
free(iface->state->offer);
|
|
|
|
iface->state->offer = NULL;
|
|
|
|
}
|
|
|
|
start_interface(iface);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reconf_reboot(int action, int argc, char **argv, int oi)
|
|
|
|
{
|
|
|
|
struct interface *ifl, *ifn, *ifp, *ifs, *ift;
|
|
|
|
|
|
|
|
ifs = discover_interfaces(argc - oi, argv + oi);
|
|
|
|
if (ifs == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Remove any old interfaces */
|
|
|
|
if (ifaces) {
|
|
|
|
for (ifl = NULL; ifl != ifaces;) {
|
|
|
|
/* Work our way backwards */
|
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
|
|
|
if (ifp->next == ifl) {
|
|
|
|
ifl = ifp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (ifn = ifs; ifn; ifn = ifn->next)
|
|
|
|
if (strcmp(ifn->name, ifp->name) == 0)
|
|
|
|
break;
|
2010-06-10 00:51:40 +08:00
|
|
|
if (ifn == NULL) {
|
|
|
|
ifl = ifp->next;
|
2010-06-09 23:26:55 +08:00
|
|
|
stop_interface(ifp);
|
2010-06-10 00:51:40 +08:00
|
|
|
}
|
2010-06-09 23:26:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ifp = ifs; ifp && (ift = ifp->next, 1); ifp = ift) {
|
|
|
|
ifl = NULL;
|
|
|
|
for (ifn = ifaces; ifn; ifn = ifn->next) {
|
|
|
|
if (strcmp(ifn->name, ifp->name) == 0)
|
|
|
|
break;
|
|
|
|
ifl = ifn;
|
|
|
|
}
|
|
|
|
if (ifn) {
|
|
|
|
if (action)
|
2010-06-10 18:38:37 +08:00
|
|
|
if_reboot(ifn, argc, argv);
|
2010-06-09 23:26:55 +08:00
|
|
|
else if (ifn->state->new)
|
|
|
|
configure(ifn);
|
|
|
|
free_interface(ifp);
|
|
|
|
} else {
|
|
|
|
ifp->next = NULL;
|
|
|
|
init_state(ifp, argc, argv);
|
|
|
|
start_interface(ifp);
|
|
|
|
if (ifl)
|
|
|
|
ifl->next = ifp;
|
|
|
|
else
|
|
|
|
ifaces = ifp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort_interfaces();
|
|
|
|
}
|
|
|
|
|
2008-11-10 19:15:27 +08:00
|
|
|
/* ARGSUSED */
|
2008-09-02 21:28:11 +08:00
|
|
|
static void
|
2008-09-04 00:49:28 +08:00
|
|
|
handle_signal(_unused void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2010-06-09 23:26:55 +08:00
|
|
|
struct interface *ifp, *ifl;
|
|
|
|
struct if_options *ifo;
|
2008-09-02 21:28:11 +08:00
|
|
|
int sig = signal_read();
|
2010-06-10 00:51:40 +08:00
|
|
|
int do_release, do_rebind, i;
|
2008-04-24 21:18:59 +08:00
|
|
|
|
2010-06-09 23:26:55 +08:00
|
|
|
do_rebind = do_release = 0;
|
2008-09-02 21:28:11 +08:00
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_INFO, "received SIGINT, stopping");
|
2008-09-02 21:28:11 +08:00
|
|
|
break;
|
|
|
|
case SIGTERM:
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_INFO, "received SIGTERM, stopping");
|
2008-09-02 21:28:11 +08:00
|
|
|
break;
|
2008-09-04 00:49:28 +08:00
|
|
|
case SIGALRM:
|
2010-06-09 23:26:55 +08:00
|
|
|
syslog(LOG_INFO, "received SIGALRM, rebinding");
|
2010-06-10 00:51:40 +08:00
|
|
|
for (i = 0; i < ifac; i++)
|
|
|
|
free(ifav[i]);
|
|
|
|
free(ifav);
|
|
|
|
ifav = NULL;
|
|
|
|
ifac = 0;
|
|
|
|
for (i = 0; i < ifdc; i++)
|
|
|
|
free(ifdv[i]);
|
|
|
|
free(ifdv);
|
|
|
|
ifdc = 0;
|
|
|
|
ifdv = NULL;
|
2010-06-09 23:26:55 +08:00
|
|
|
ifo = read_config(cffile, NULL, NULL, NULL);
|
2010-06-10 00:51:40 +08:00
|
|
|
add_options(ifo, margc, margv);
|
2010-06-09 23:26:55 +08:00
|
|
|
/* We need to preserve these two options. */
|
|
|
|
if (options & DHCPCD_MASTER)
|
|
|
|
ifo->options |= DHCPCD_MASTER;
|
|
|
|
if (options & DHCPCD_DAEMONISED)
|
|
|
|
ifo->options |= DHCPCD_DAEMONISED;
|
|
|
|
options = ifo->options;
|
|
|
|
free_options(ifo);
|
|
|
|
reconf_reboot(1, 0, NULL, 0);
|
2008-09-15 23:23:46 +08:00
|
|
|
return;
|
2008-09-04 00:49:28 +08:00
|
|
|
case SIGHUP:
|
2010-06-09 23:26:55 +08:00
|
|
|
syslog(LOG_INFO, "received SIGHUP, releasing");
|
2008-09-04 00:49:28 +08:00
|
|
|
do_release = 1;
|
|
|
|
break;
|
2009-03-20 18:21:12 +08:00
|
|
|
case SIGUSR1:
|
|
|
|
syslog(LOG_INFO, "received SIGUSR, reconfiguring");
|
2010-06-09 23:26:55 +08:00
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
|
|
|
if (ifp->state->new)
|
|
|
|
configure(ifp);
|
2009-03-20 18:33:09 +08:00
|
|
|
return;
|
2009-01-21 00:33:11 +08:00
|
|
|
case SIGPIPE:
|
|
|
|
syslog(LOG_WARNING, "received SIGPIPE");
|
|
|
|
return;
|
2008-09-02 21:28:11 +08:00
|
|
|
default:
|
2008-09-15 23:23:46 +08:00
|
|
|
syslog(LOG_ERR,
|
2009-02-12 01:56:22 +08:00
|
|
|
"received signal %d, but don't know what to do with it",
|
|
|
|
sig);
|
2008-09-02 21:28:11 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-04-24 21:18:59 +08:00
|
|
|
|
2009-07-12 02:54:43 +08:00
|
|
|
if (options & DHCPCD_TEST)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2008-09-10 01:07:48 +08:00
|
|
|
/* As drop_config could re-arrange the order, we do it like this. */
|
|
|
|
for (;;) {
|
2008-09-12 06:55:27 +08:00
|
|
|
/* Be sane and drop the last config first */
|
|
|
|
ifl = NULL;
|
2010-06-09 23:26:55 +08:00
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next) {
|
|
|
|
if (ifp->next == NULL)
|
2009-01-21 00:33:11 +08:00
|
|
|
break;
|
2010-06-09 23:26:55 +08:00
|
|
|
ifl = ifp;
|
2008-10-19 00:41:19 +08:00
|
|
|
}
|
2010-06-09 23:26:55 +08:00
|
|
|
if (ifp == NULL)
|
2009-01-21 00:33:11 +08:00
|
|
|
break;
|
2010-06-09 23:26:55 +08:00
|
|
|
if (ifp->carrier != LINK_DOWN &&
|
2009-01-21 00:33:11 +08:00
|
|
|
(do_release ||
|
2010-06-09 23:26:55 +08:00
|
|
|
ifp->state->options->options & DHCPCD_RELEASE))
|
|
|
|
send_release(ifp);
|
|
|
|
stop_interface(ifp);
|
2008-04-24 21:18:59 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2008-04-24 21:18:59 +08:00
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
int
|
2009-01-13 00:39:01 +08:00
|
|
|
handle_args(struct fd_list *fd, int argc, char **argv)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2010-06-09 23:26:55 +08:00
|
|
|
struct interface *ifp;
|
2009-03-20 18:21:12 +08:00
|
|
|
int do_exit = 0, do_release = 0, do_reboot = 0, do_reconf = 0;
|
|
|
|
int opt, oi = 0;
|
2009-01-15 22:22:40 +08:00
|
|
|
ssize_t len;
|
2009-02-06 05:06:57 +08:00
|
|
|
size_t l;
|
2009-01-14 01:04:28 +08:00
|
|
|
struct iovec iov[2];
|
2009-02-06 05:06:57 +08:00
|
|
|
char *tmp, *p;
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2009-01-13 00:39:01 +08:00
|
|
|
if (fd != NULL) {
|
2009-01-14 01:04:28 +08:00
|
|
|
/* Special commands for our control socket */
|
2009-01-13 00:39:01 +08:00
|
|
|
if (strcmp(*argv, "--version") == 0) {
|
2009-01-15 22:22:40 +08:00
|
|
|
len = strlen(VERSION) + 1;
|
|
|
|
iov[0].iov_base = &len;
|
2009-01-14 02:03:12 +08:00
|
|
|
iov[0].iov_len = sizeof(ssize_t);
|
|
|
|
iov[1].iov_base = UNCONST(VERSION);
|
2009-01-15 22:22:40 +08:00
|
|
|
iov[1].iov_len = len;
|
2009-07-04 08:58:03 +08:00
|
|
|
if (writev(fd->fd, iov, 2) == -1) {
|
|
|
|
syslog(LOG_ERR, "writev: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-13 00:39:01 +08:00
|
|
|
return 0;
|
2009-02-13 23:39:34 +08:00
|
|
|
} else if (strcmp(*argv, "--getconfigfile") == 0) {
|
|
|
|
len = strlen(cffile ? cffile : CONFIG) + 1;
|
|
|
|
iov[0].iov_base = &len;
|
|
|
|
iov[0].iov_len = sizeof(ssize_t);
|
|
|
|
iov[1].iov_base = cffile ? cffile : UNCONST(CONFIG);
|
|
|
|
iov[1].iov_len = len;
|
2009-07-04 08:58:03 +08:00
|
|
|
if (writev(fd->fd, iov, 2) == -1) {
|
|
|
|
syslog(LOG_ERR, "writev: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-02-13 23:39:34 +08:00
|
|
|
return 0;
|
2009-01-14 01:04:28 +08:00
|
|
|
} else if (strcmp(*argv, "--getinterfaces") == 0) {
|
2009-01-15 22:22:40 +08:00
|
|
|
len = 0;
|
2009-01-17 08:43:08 +08:00
|
|
|
if (argc == 1) {
|
2009-01-15 22:22:40 +08:00
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
|
|
|
len++;
|
2009-01-29 21:01:29 +08:00
|
|
|
len = write(fd->fd, &len, sizeof(len));
|
|
|
|
if (len != sizeof(len))
|
|
|
|
return -1;
|
2009-01-15 22:22:40 +08:00
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
|
|
|
send_interface(fd->fd, ifp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
opt = 0;
|
|
|
|
while (argv[++opt] != NULL) {
|
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
|
|
|
if (strcmp(argv[opt], ifp->name) == 0)
|
|
|
|
len++;
|
|
|
|
}
|
2009-01-29 21:01:29 +08:00
|
|
|
len = write(fd->fd, &len, sizeof(len));
|
|
|
|
if (len != sizeof(len))
|
|
|
|
return -1;
|
2009-01-15 22:22:40 +08:00
|
|
|
opt = 0;
|
|
|
|
while (argv[++opt] != NULL) {
|
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
|
|
|
if (strcmp(argv[opt], ifp->name) == 0)
|
|
|
|
send_interface(fd->fd, ifp);
|
2009-01-13 00:39:01 +08:00
|
|
|
}
|
2009-01-14 01:04:28 +08:00
|
|
|
return 0;
|
2009-01-13 00:39:01 +08:00
|
|
|
} else if (strcmp(*argv, "--listen") == 0) {
|
|
|
|
fd->listener = 1;
|
2009-01-05 16:20:53 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-06 05:06:57 +08:00
|
|
|
/* Log the command */
|
|
|
|
len = 0;
|
|
|
|
for (opt = 0; opt < argc; opt++)
|
|
|
|
len += strlen(argv[opt]) + 1;
|
|
|
|
tmp = p = xmalloc(len + 1);
|
|
|
|
for (opt = 0; opt < argc; opt++) {
|
|
|
|
l = strlen(argv[opt]);
|
|
|
|
strlcpy(p, argv[opt], l + 1);
|
|
|
|
p += l;
|
|
|
|
*p++ = ' ';
|
|
|
|
}
|
|
|
|
*--p = '\0';
|
|
|
|
syslog(LOG_INFO, "control command: %s", tmp);
|
|
|
|
free(tmp);
|
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
optind = 0;
|
|
|
|
while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
|
|
|
|
{
|
|
|
|
switch (opt) {
|
2009-07-26 07:22:10 +08:00
|
|
|
case 'g':
|
2009-03-20 18:21:12 +08:00
|
|
|
do_reconf = 1;
|
|
|
|
break;
|
2008-09-04 00:49:28 +08:00
|
|
|
case 'k':
|
|
|
|
do_release = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
do_reboot = 1;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
do_exit = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-05 23:50:14 +08:00
|
|
|
/* We need at least one interface */
|
2008-09-04 00:49:28 +08:00
|
|
|
if (optind == argc) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "handle_args: no interface");
|
2008-09-04 00:49:28 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-06 16:37:02 +08:00
|
|
|
if (do_release || do_exit) {
|
2008-09-04 16:27:38 +08:00
|
|
|
for (oi = optind; oi < argc; oi++) {
|
2008-09-12 06:55:27 +08:00
|
|
|
for (ifp = ifaces; ifp; ifp = ifp->next)
|
2008-09-04 16:27:38 +08:00
|
|
|
if (strcmp(ifp->name, argv[oi]) == 0)
|
|
|
|
break;
|
|
|
|
if (!ifp)
|
|
|
|
continue;
|
|
|
|
if (do_release)
|
2008-09-16 21:17:23 +08:00
|
|
|
ifp->state->options->options |= DHCPCD_RELEASE;
|
2009-01-21 00:33:11 +08:00
|
|
|
if (ifp->state->options->options & DHCPCD_RELEASE &&
|
|
|
|
ifp->carrier != LINK_DOWN)
|
|
|
|
send_release(ifp);
|
|
|
|
stop_interface(ifp);
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-06-09 23:26:55 +08:00
|
|
|
reconf_reboot(do_reboot, argc, argv, optind);
|
2008-09-04 00:49:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-19 20:36:28 +08:00
|
|
|
void
|
|
|
|
open_sockets(struct interface *iface)
|
|
|
|
{
|
|
|
|
if (iface->raw_fd == -1) {
|
|
|
|
if (open_socket(iface, ETHERTYPE_IP) == -1)
|
|
|
|
syslog(LOG_ERR, "%s: open_socket: %m", iface->name);
|
|
|
|
else
|
|
|
|
add_event(iface->raw_fd, handle_dhcp_packet, iface);
|
|
|
|
}
|
|
|
|
if (iface->udp_fd == -1 &&
|
|
|
|
iface->addr.s_addr != 0 &&
|
|
|
|
iface->state->new != NULL &&
|
2010-11-12 05:07:50 +08:00
|
|
|
(iface->state->new->cookie == htonl(MAGIC_COOKIE) ||
|
|
|
|
iface->state->options->options & DHCPCD_INFORM))
|
2010-04-19 20:36:28 +08:00
|
|
|
{
|
|
|
|
if (open_udp_socket(iface) == -1 && errno != EADDRINUSE)
|
|
|
|
syslog(LOG_ERR, "%s: open_udp_socket: %m", iface->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
close_sockets(struct interface *iface)
|
|
|
|
{
|
|
|
|
if (iface->arp_fd != -1) {
|
|
|
|
delete_event(iface->arp_fd);
|
|
|
|
close(iface->arp_fd);
|
|
|
|
iface->arp_fd = -1;
|
|
|
|
}
|
|
|
|
if (iface->raw_fd != -1) {
|
|
|
|
delete_event(iface->raw_fd);
|
|
|
|
close(iface->raw_fd);
|
|
|
|
iface->raw_fd = -1;
|
|
|
|
}
|
|
|
|
if (iface->udp_fd != -1) {
|
|
|
|
/* we don't listen to events on the udp */
|
|
|
|
close(iface->udp_fd);
|
|
|
|
iface->udp_fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2009-02-25 03:17:19 +08:00
|
|
|
struct interface *iface;
|
2009-10-11 00:25:06 +08:00
|
|
|
int opt, oi = 0, signal_fd, sig = 0, i, control_fd;
|
2008-09-05 22:16:53 +08:00
|
|
|
size_t len;
|
2008-09-02 21:28:11 +08:00
|
|
|
pid_t pid;
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
closefrom(3);
|
2010-04-30 11:02:29 +08:00
|
|
|
openlog(PACKAGE, LOG_PERROR | LOG_PID, LOG_DAEMON);
|
2008-09-06 02:24:34 +08:00
|
|
|
setlogmask(LOG_UPTO(LOG_INFO));
|
2008-09-02 21:28:11 +08:00
|
|
|
|
|
|
|
/* Test for --help and --version */
|
|
|
|
if (argc > 1) {
|
|
|
|
if (strcmp(argv[1], "--help") == 0) {
|
2008-04-21 17:40:14 +08:00
|
|
|
usage();
|
2008-09-02 21:28:11 +08:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (strcmp(argv[1], "--version") == 0) {
|
|
|
|
printf(""PACKAGE" "VERSION"\n%s\n", copyright);
|
|
|
|
exit(EXIT_SUCCESS);
|
2008-04-19 07:12:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-11 17:38:02 +08:00
|
|
|
i = 0;
|
2008-09-02 21:28:11 +08:00
|
|
|
while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1)
|
2007-05-11 18:51:32 +08:00
|
|
|
{
|
2007-05-14 21:34:36 +08:00
|
|
|
switch (opt) {
|
2008-03-21 00:47:51 +08:00
|
|
|
case 'f':
|
2008-09-05 21:28:44 +08:00
|
|
|
cffile = optarg;
|
2008-03-21 00:47:51 +08:00
|
|
|
break;
|
2009-07-26 07:22:10 +08:00
|
|
|
case 'g':
|
|
|
|
sig = SIGUSR1;
|
|
|
|
break;
|
2008-09-04 00:49:28 +08:00
|
|
|
case 'k':
|
|
|
|
sig = SIGHUP;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
sig = SIGALRM;
|
|
|
|
break;
|
2008-03-21 00:47:51 +08:00
|
|
|
case 'x':
|
|
|
|
sig = SIGTERM;
|
|
|
|
break;
|
2008-04-19 07:12:44 +08:00
|
|
|
case 'T':
|
2008-09-11 17:38:02 +08:00
|
|
|
i = 1;
|
2008-03-21 00:47:51 +08:00
|
|
|
break;
|
2010-08-24 17:16:07 +08:00
|
|
|
case 'U':
|
|
|
|
i = 2;
|
|
|
|
break;
|
2008-09-02 21:28:11 +08:00
|
|
|
case 'V':
|
|
|
|
print_options();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
2007-05-11 18:51:32 +08:00
|
|
|
}
|
2008-05-13 05:03:38 +08:00
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
margv = argv;
|
|
|
|
margc = argc;
|
2010-08-24 17:34:21 +08:00
|
|
|
if_options = read_config(cffile, NULL, NULL, NULL);
|
|
|
|
opt = add_options(if_options, argc, argv);
|
2008-09-02 21:28:11 +08:00
|
|
|
if (opt != 1) {
|
|
|
|
if (opt == 0)
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-08-24 17:34:21 +08:00
|
|
|
options = if_options->options;
|
2009-07-12 02:54:43 +08:00
|
|
|
if (i != 0) {
|
2010-08-24 17:16:07 +08:00
|
|
|
if (i == 1)
|
|
|
|
options |= DHCPCD_TEST;
|
|
|
|
else
|
|
|
|
options |= DHCPCD_DUMPLEASE;
|
|
|
|
options |= DHCPCD_PERSISTENT;
|
2009-07-12 02:54:43 +08:00
|
|
|
options &= ~DHCPCD_DAEMONISE;
|
|
|
|
}
|
|
|
|
|
2008-09-11 16:28:39 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
options &= ~DHCPCD_DAEMONISE;
|
|
|
|
#endif
|
|
|
|
|
2009-02-24 07:52:21 +08:00
|
|
|
if (options & DHCPCD_DEBUG)
|
|
|
|
setlogmask(LOG_UPTO(LOG_DEBUG));
|
2010-04-27 18:46:44 +08:00
|
|
|
if (options & DHCPCD_QUIET)
|
2009-11-20 05:31:41 +08:00
|
|
|
close(STDERR_FILENO);
|
2008-09-11 16:28:39 +08:00
|
|
|
|
2010-08-24 17:16:07 +08:00
|
|
|
if (!(options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) {
|
2009-07-12 02:54:43 +08:00
|
|
|
/* If we have any other args, we should run as a single dhcpcd
|
|
|
|
* instance for that interface. */
|
|
|
|
len = strlen(PIDFILE) + IF_NAMESIZE + 2;
|
|
|
|
pidfile = xmalloc(len);
|
|
|
|
if (optind == argc - 1)
|
|
|
|
snprintf(pidfile, len, PIDFILE, "-", argv[optind]);
|
|
|
|
else {
|
|
|
|
snprintf(pidfile, len, PIDFILE, "", "");
|
|
|
|
options |= DHCPCD_MASTER;
|
|
|
|
}
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2009-01-29 21:01:29 +08:00
|
|
|
if (chdir("/") == -1)
|
2009-01-29 21:18:57 +08:00
|
|
|
syslog(LOG_ERR, "chdir `/': %m");
|
2008-09-02 21:28:11 +08:00
|
|
|
atexit(cleanup);
|
2008-08-15 00:16:06 +08:00
|
|
|
|
2010-08-24 17:16:07 +08:00
|
|
|
if (options & DHCPCD_DUMPLEASE) {
|
|
|
|
if (optind != argc - 1) {
|
|
|
|
syslog(LOG_ERR, "dumplease requires an interface");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-08-24 17:34:21 +08:00
|
|
|
ifaces = iface = xzalloc(sizeof(*iface));
|
2010-08-24 17:16:07 +08:00
|
|
|
strlcpy(iface->name, argv[optind], sizeof(iface->name));
|
|
|
|
snprintf(iface->leasefile, sizeof(iface->leasefile),
|
|
|
|
LEASEFILE, iface->name);
|
|
|
|
iface->state = xzalloc(sizeof(*iface->state));
|
2010-08-24 18:04:10 +08:00
|
|
|
iface->state->options = xzalloc(sizeof(*iface->state->options));
|
|
|
|
strlcpy(iface->state->options->script, if_options->script,
|
|
|
|
sizeof(iface->state->options->script));
|
2010-08-24 17:16:07 +08:00
|
|
|
iface->state->new = read_lease(iface);
|
2010-08-24 18:36:20 +08:00
|
|
|
if (iface->state->new == NULL && errno == ENOENT) {
|
|
|
|
strlcpy(iface->leasefile, argv[optind],
|
|
|
|
sizeof(iface->leasefile));
|
|
|
|
iface->state->new = read_lease(iface);
|
|
|
|
}
|
2010-08-24 18:25:32 +08:00
|
|
|
if (iface->state->new == NULL) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
syslog(LOG_ERR, "%s: no lease to dump",
|
|
|
|
iface->name);
|
2010-08-24 17:16:07 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
iface->state->reason = "DUMP";
|
|
|
|
run_script(iface);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2010-08-24 21:35:15 +08:00
|
|
|
if (!(options & (DHCPCD_MASTER | DHCPCD_TEST))) {
|
2008-09-04 00:49:28 +08:00
|
|
|
control_fd = open_control();
|
|
|
|
if (control_fd != -1) {
|
2009-02-12 01:56:22 +08:00
|
|
|
syslog(LOG_INFO,
|
|
|
|
"sending commands to master dhcpcd process");
|
2008-09-04 00:49:28 +08:00
|
|
|
i = send_control(argc, argv);
|
|
|
|
if (i > 0) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_DEBUG, "send OK");
|
2008-09-04 00:49:28 +08:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "failed to send commands");
|
2008-09-04 00:49:28 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2008-09-05 21:28:44 +08:00
|
|
|
} else {
|
2008-09-05 23:46:02 +08:00
|
|
|
if (errno != ENOENT)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "open_control: %m");
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (geteuid())
|
2009-02-12 01:56:22 +08:00
|
|
|
syslog(LOG_WARNING,
|
|
|
|
PACKAGE " will not work correctly unless run as root");
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
if (sig != 0) {
|
|
|
|
pid = read_pid();
|
2007-07-12 20:52:27 +08:00
|
|
|
if (pid != 0)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_INFO, "sending signal %d to pid %d",
|
2009-02-12 01:56:22 +08:00
|
|
|
sig, pid);
|
2009-05-09 22:15:31 +08:00
|
|
|
if (pid == 0 || kill(pid, sig) != 0) {
|
2008-08-15 00:00:37 +08:00
|
|
|
if (sig != SIGALRM)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, ""PACKAGE" not running");
|
2009-10-14 23:16:07 +08:00
|
|
|
if (pid != 0 && errno != ESRCH) {
|
|
|
|
syslog(LOG_ERR, "kill: %m");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
unlink(pidfile);
|
2009-05-09 22:15:31 +08:00
|
|
|
if (sig != SIGALRM)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else {
|
|
|
|
if (sig == SIGALRM)
|
2008-09-02 21:28:11 +08:00
|
|
|
exit(EXIT_SUCCESS);
|
2009-05-09 22:15:31 +08:00
|
|
|
/* Spin until it exits */
|
|
|
|
syslog(LOG_INFO, "waiting for pid %d to exit", pid);
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 100000000; /* 10th of a second */
|
|
|
|
for(i = 0; i < 100; i++) {
|
|
|
|
nanosleep(&ts, NULL);
|
|
|
|
if (read_pid() == 0)
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
syslog(LOG_ERR, "pid %d failed to exit", pid);
|
|
|
|
exit(EXIT_FAILURE);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2007-05-13 20:28:54 +08:00
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-09-05 15:22:03 +08:00
|
|
|
if (!(options & DHCPCD_TEST)) {
|
2008-09-02 21:28:11 +08:00
|
|
|
if ((pid = read_pid()) > 0 &&
|
2008-03-21 00:47:51 +08:00
|
|
|
kill(pid, 0) == 0)
|
2008-01-17 00:38:47 +08:00
|
|
|
{
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, ""PACKAGE
|
2009-02-12 01:56:22 +08:00
|
|
|
" already running on pid %d (%s)",
|
|
|
|
pid, pidfile);
|
2008-09-02 21:28:11 +08:00
|
|
|
exit(EXIT_FAILURE);
|
2007-07-12 20:52:27 +08:00
|
|
|
}
|
|
|
|
|
2009-07-09 03:22:31 +08:00
|
|
|
/* Ensure we have the needed directories */
|
|
|
|
if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST) {
|
|
|
|
syslog(LOG_ERR, "mkdir `%s': %m", RUNDIR);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST) {
|
|
|
|
syslog(LOG_ERR, "mkdir `%s': %m", DBDIR);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
pidfd = open(pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0664);
|
|
|
|
if (pidfd == -1) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "open `%s': %m", pidfile);
|
2008-09-02 21:28:11 +08:00
|
|
|
exit(EXIT_FAILURE);
|
2007-07-12 20:52:27 +08:00
|
|
|
}
|
2008-01-17 00:38:47 +08:00
|
|
|
/* Lock the file so that only one instance of dhcpcd runs
|
|
|
|
* on an interface */
|
2008-09-02 21:28:11 +08:00
|
|
|
if (flock(pidfd, LOCK_EX | LOCK_NB) == -1) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "flock `%s': %m", pidfile);
|
2008-09-02 21:28:11 +08:00
|
|
|
exit(EXIT_FAILURE);
|
2007-07-12 20:52:27 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
if (set_cloexec(pidfd) == -1)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
writepid(pidfd, getpid());
|
2007-07-12 20:52:27 +08:00
|
|
|
}
|
|
|
|
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_INFO, "version " VERSION " starting");
|
2008-07-01 00:02:56 +08:00
|
|
|
|
2009-01-28 02:09:02 +08:00
|
|
|
if ((signal_fd = signal_init()) == -1)
|
2008-09-02 21:28:11 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
if (signal_setup() == -1)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
add_event(signal_fd, handle_signal, NULL);
|
2007-09-04 20:48:40 +08:00
|
|
|
|
2008-09-05 15:22:03 +08:00
|
|
|
if (options & DHCPCD_MASTER) {
|
2008-09-04 00:49:28 +08:00
|
|
|
if (start_control() == -1) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "start_control: %m");
|
2008-09-04 00:49:28 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-15 08:30:54 +08:00
|
|
|
if (init_sockets() == -1) {
|
2009-01-28 03:40:05 +08:00
|
|
|
syslog(LOG_ERR, "init_socket: %m");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2010-08-24 17:34:21 +08:00
|
|
|
if (if_options->options & DHCPCD_LINK) {
|
2008-09-02 21:28:11 +08:00
|
|
|
linkfd = open_link_socket();
|
|
|
|
if (linkfd == -1)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "open_link_socket: %m");
|
2008-09-02 21:28:11 +08:00
|
|
|
else
|
|
|
|
add_event(linkfd, handle_link, NULL);
|
2007-10-21 00:58:13 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
|
2009-02-25 15:52:07 +08:00
|
|
|
ifc = argc - optind;
|
|
|
|
ifv = argv + optind;
|
2008-09-02 21:28:11 +08:00
|
|
|
|
2009-10-25 18:56:54 +08:00
|
|
|
/* When running dhcpcd against a single interface, we need to retain
|
|
|
|
* the old behaviour of waiting for an IP address */
|
|
|
|
if (ifc == 1)
|
|
|
|
options |= DHCPCD_WAITIP;
|
|
|
|
|
2008-09-04 19:30:11 +08:00
|
|
|
ifaces = discover_interfaces(ifc, ifv);
|
2008-11-15 04:38:40 +08:00
|
|
|
for (i = 0; i < ifc; i++) {
|
|
|
|
for (iface = ifaces; iface; iface = iface->next)
|
|
|
|
if (strcmp(iface->name, ifv[i]) == 0)
|
|
|
|
break;
|
|
|
|
if (!iface)
|
2008-11-15 19:24:26 +08:00
|
|
|
syslog(LOG_ERR, "%s: interface not found or invalid",
|
2009-02-12 01:56:22 +08:00
|
|
|
ifv[i]);
|
2008-11-15 04:38:40 +08:00
|
|
|
}
|
|
|
|
if (!ifaces) {
|
|
|
|
if (ifc == 0)
|
|
|
|
syslog(LOG_ERR, "no valid interfaces found");
|
2009-10-10 03:24:22 +08:00
|
|
|
else
|
|
|
|
exit(EXIT_FAILURE);
|
2009-02-25 15:52:07 +08:00
|
|
|
if (!(options & DHCPCD_LINK)) {
|
2009-10-14 23:16:07 +08:00
|
|
|
syslog(LOG_ERR,
|
|
|
|
"aborting as link detection is disabled");
|
2008-11-15 19:24:26 +08:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2008-09-04 19:30:11 +08:00
|
|
|
}
|
2008-11-15 04:38:40 +08:00
|
|
|
|
2009-10-14 23:16:07 +08:00
|
|
|
if (options & DHCPCD_BACKGROUND)
|
|
|
|
daemonise();
|
|
|
|
|
|
|
|
opt = 0;
|
|
|
|
for (iface = ifaces; iface; iface = iface->next) {
|
2008-09-10 01:07:48 +08:00
|
|
|
init_state(iface, argc, argv);
|
2009-10-14 23:16:07 +08:00
|
|
|
if (iface->carrier != LINK_DOWN)
|
|
|
|
opt = 1;
|
|
|
|
}
|
2009-11-20 23:14:27 +08:00
|
|
|
|
|
|
|
if (!(options & DHCPCD_BACKGROUND)) {
|
|
|
|
/* If we don't have a carrier, we may have to wait for a second
|
|
|
|
* before one becomes available if we brought an interface up. */
|
|
|
|
if (opt == 0 &&
|
|
|
|
options & DHCPCD_LINK &&
|
|
|
|
options & DHCPCD_WAITUP &&
|
|
|
|
!(options & DHCPCD_WAITIP))
|
|
|
|
{
|
|
|
|
ts.tv_sec = 1;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
nanosleep(&ts, NULL);
|
|
|
|
for (iface = ifaces; iface; iface = iface->next) {
|
|
|
|
handle_carrier(iface->name);
|
|
|
|
if (iface->carrier != LINK_DOWN) {
|
|
|
|
opt = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-29 18:36:17 +08:00
|
|
|
if (options & DHCPCD_MASTER)
|
|
|
|
i = if_options->timeout;
|
|
|
|
else
|
|
|
|
i = ifaces->state->options->timeout;
|
2009-11-20 23:14:27 +08:00
|
|
|
if (opt == 0 &&
|
|
|
|
options & DHCPCD_LINK &&
|
|
|
|
!(options & DHCPCD_WAITIP))
|
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "no interfaces have a carrier");
|
|
|
|
daemonise();
|
2011-03-29 18:36:17 +08:00
|
|
|
} else if (i > 0) {
|
2009-11-20 23:14:27 +08:00
|
|
|
if (options & DHCPCD_IPV4LL)
|
|
|
|
options |= DHCPCD_TIMEOUT_IPV4LL;
|
2011-03-29 18:36:17 +08:00
|
|
|
add_timeout_sec(i, handle_exit_timeout, NULL);
|
2009-11-20 23:14:27 +08:00
|
|
|
}
|
2009-10-14 23:16:07 +08:00
|
|
|
}
|
2010-08-24 17:34:21 +08:00
|
|
|
free_options(if_options);
|
|
|
|
if_options = NULL;
|
2009-10-14 23:16:07 +08:00
|
|
|
|
2008-09-10 01:07:48 +08:00
|
|
|
sort_interfaces();
|
2009-02-25 03:17:19 +08:00
|
|
|
for (iface = ifaces; iface; iface = iface->next)
|
|
|
|
add_timeout_sec(0, start_interface, iface);
|
2009-02-20 05:15:25 +08:00
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
start_eloop();
|
2008-11-24 19:21:31 +08:00
|
|
|
exit(EXIT_SUCCESS);
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|