2013-04-05 04:31:04 +08:00
|
|
|
/*
|
2007-11-08 00:26:41 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2014-01-04 05:12:19 +08:00
|
|
|
* Copyright (c) 2006-2014 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
|
|
|
*/
|
|
|
|
|
2014-01-24 19:00:38 +08:00
|
|
|
const char dhcpcd_copyright[] = "Copyright (c) 2006-2014 Roy Marples";
|
2007-11-08 18:38:54 +08:00
|
|
|
|
2014-02-13 21:23:43 +08:00
|
|
|
#define _WITH_DPRINTF /* Stop FreeBSD bitching */
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2007-05-11 00:08:49 +08:00
|
|
|
#include "config.h"
|
2014-01-24 19:09:39 +08:00
|
|
|
#include "arp.h"
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "common.h"
|
2008-09-04 00:49:28 +08:00
|
|
|
#include "control.h"
|
2013-09-12 23:43:20 +08:00
|
|
|
#include "dev.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
#include "dhcpcd.h"
|
2012-10-12 18:31:51 +08:00
|
|
|
#include "dhcp6.h"
|
2013-11-15 21:43:41 +08:00
|
|
|
#include "duid.h"
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "eloop.h"
|
|
|
|
#include "if-options.h"
|
2008-09-10 01:07:48 +08:00
|
|
|
#include "if-pref.h"
|
2013-02-02 22:05:55 +08:00
|
|
|
#include "ipv4.h"
|
2012-07-06 00:37:41 +08:00
|
|
|
#include "ipv6.h"
|
2013-08-20 18:29:43 +08:00
|
|
|
#include "ipv6nd.h"
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "net.h"
|
2012-02-04 23:15:25 +08:00
|
|
|
#include "platform.h"
|
2013-02-02 22:05:55 +08:00
|
|
|
#include "script.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
#ifdef USE_SIGNALS
|
2013-06-07 20:44:19 +08:00
|
|
|
const int handle_sigs[] = {
|
|
|
|
SIGALRM,
|
|
|
|
SIGHUP,
|
|
|
|
SIGINT,
|
|
|
|
SIGPIPE,
|
|
|
|
SIGTERM,
|
|
|
|
SIGUSR1,
|
|
|
|
0
|
|
|
|
};
|
2012-11-11 00:38:52 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
/* Handling signals needs *some* context */
|
|
|
|
static struct dhcpcd_ctx *dhcpcd_ctx;
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
2008-04-19 07:12:44 +08:00
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
#if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK)
|
2008-03-21 00:47:51 +08:00
|
|
|
static pid_t
|
2014-02-04 22:39:26 +08:00
|
|
|
read_pid(const char *pidfile)
|
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
|
|
|
}
|
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
static inline int
|
|
|
|
write_pid(int fd, pid_t pid)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (ftruncate(fd, (off_t)0) == -1)
|
|
|
|
return -1;
|
|
|
|
lseek(fd, (off_t)0, SEEK_SET);
|
|
|
|
return dprintf(fd, "%d\n", (int)pid);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
static void
|
|
|
|
usage(void)
|
2006-11-28 04:23:22 +08:00
|
|
|
{
|
2012-03-24 18:41:40 +08:00
|
|
|
|
2012-10-12 18:31:51 +08:00
|
|
|
printf("usage: "PACKAGE"\t[-46ABbDdEGgHJKkLnpqTVw]\n"
|
2012-03-24 18:41:40 +08:00
|
|
|
"\t\t[-C, --nohook hook] [-c, --script script]\n"
|
|
|
|
"\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
|
|
|
|
"\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
|
|
|
|
"\t\t[-i, --vendorclassid vendorclassid] [-l, --leasetime seconds]\n"
|
|
|
|
"\t\t[-m, --metric metric] [-O, --nooption option]\n"
|
|
|
|
"\t\t[-o, --option option] [-Q, --require option]\n"
|
|
|
|
"\t\t[-r, --request address] [-S, --static value]\n"
|
|
|
|
"\t\t[-s, --inform address[/cidr]] [-t, --timeout seconds]\n"
|
|
|
|
"\t\t[-u, --userclass class] [-v, --vendor code, value]\n"
|
|
|
|
"\t\t[-W, --whitelist address[/cidr]] [-y, --reboot seconds]\n"
|
|
|
|
"\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n"
|
|
|
|
"\t\t[-z, --allowinterfaces pattern] [interface] [...]\n"
|
|
|
|
" "PACKAGE"\t-k, --release [interface]\n"
|
|
|
|
" "PACKAGE"\t-U, --dumplease interface\n"
|
2012-08-18 21:58:53 +08:00
|
|
|
" "PACKAGE"\t--version\n"
|
2012-03-24 18:41:40 +08:00
|
|
|
" "PACKAGE"\t-x, --exit [interface]\n");
|
2008-05-21 00:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-11-29 19:15:03 +08:00
|
|
|
static void
|
2014-02-12 08:39:46 +08:00
|
|
|
free_globals(struct dhcpcd_ctx *ctx)
|
2013-11-29 19:15:03 +08:00
|
|
|
{
|
2013-12-03 04:45:19 +08:00
|
|
|
struct dhcp_opt *opt;
|
2013-11-29 19:15:03 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->ifac) {
|
|
|
|
for (ctx->ifac--; ctx->ifac >= 0; ctx->ifac--)
|
|
|
|
free(ctx->ifav[ctx->ifac]);
|
|
|
|
free(ctx->ifav);
|
|
|
|
ctx->ifav = NULL;
|
2014-02-08 18:36:38 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->ifdc) {
|
|
|
|
for (ctx->ifdc--; ctx->ifdc >= 0; ctx->ifdc--)
|
|
|
|
free(ctx->ifdv[ctx->ifac]);
|
|
|
|
free(ctx->ifdv);
|
|
|
|
ctx->ifdv = NULL;
|
2014-02-08 18:36:38 +08:00
|
|
|
}
|
2013-11-29 19:15:03 +08:00
|
|
|
|
|
|
|
#ifdef INET
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->dhcp_opts) {
|
|
|
|
for (opt = ctx->dhcp_opts;
|
|
|
|
ctx->dhcp_opts_len > 0;
|
|
|
|
opt++, ctx->dhcp_opts_len--)
|
2014-02-08 18:36:38 +08:00
|
|
|
free_dhcp_opt_embenc(opt);
|
2014-02-12 08:39:46 +08:00
|
|
|
free(ctx->dhcp_opts);
|
|
|
|
ctx->dhcp_opts = NULL;
|
2014-02-08 18:36:38 +08:00
|
|
|
}
|
2013-11-29 19:15:03 +08:00
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->dhcp6_opts) {
|
|
|
|
for (opt = ctx->dhcp6_opts;
|
|
|
|
ctx->dhcp6_opts_len > 0;
|
|
|
|
opt++, ctx->dhcp6_opts_len--)
|
2014-02-08 18:36:38 +08:00
|
|
|
free_dhcp_opt_embenc(opt);
|
2014-02-12 08:39:46 +08:00
|
|
|
free(ctx->dhcp6_opts);
|
|
|
|
ctx->dhcp6_opts = NULL;
|
2014-02-08 18:36:38 +08:00
|
|
|
}
|
2013-11-29 19:15:03 +08:00
|
|
|
#endif
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->vivso) {
|
|
|
|
for (opt = ctx->vivso;
|
|
|
|
ctx->vivso_len > 0;
|
|
|
|
opt++, ctx->vivso_len--)
|
2014-02-08 18:36:38 +08:00
|
|
|
free_dhcp_opt_embenc(opt);
|
2014-02-12 08:39:46 +08:00
|
|
|
free(ctx->vivso);
|
|
|
|
ctx->vivso = NULL;
|
2014-02-08 18:36:38 +08:00
|
|
|
}
|
2013-11-29 19:15:03 +08:00
|
|
|
}
|
|
|
|
|
2013-02-02 22:05:55 +08:00
|
|
|
static void
|
2014-02-12 08:39:46 +08:00
|
|
|
handle_exit_timeout(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2014-02-12 08:39:46 +08:00
|
|
|
struct dhcpcd_ctx *ctx;
|
2009-10-09 03:56:52 +08:00
|
|
|
int timeout;
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx = arg;
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "timed out");
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ctx->options & DHCPCD_IPV4) ||
|
|
|
|
!(ctx->options & DHCPCD_TIMEOUT_IPV4LL))
|
|
|
|
{
|
|
|
|
if (ctx->options & DHCPCD_MASTER) {
|
2013-10-06 00:28:33 +08:00
|
|
|
/* We've timed out, so remove the waitip requirements.
|
|
|
|
* If the user doesn't like this they can always set
|
|
|
|
* an infinite timeout. */
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx->options &=
|
2013-10-06 00:28:33 +08:00
|
|
|
~(DHCPCD_WAITIP | DHCPCD_WAITIP4 | DHCPCD_WAITIP6);
|
2014-02-12 08:39:46 +08:00
|
|
|
daemonise(ctx);
|
2010-06-26 17:17:54 +08:00
|
|
|
} else
|
2014-02-12 08:39:46 +08:00
|
|
|
eloop_exit(ctx->eloop, EXIT_FAILURE);
|
2014-02-04 22:39:26 +08:00
|
|
|
return;
|
2010-06-26 17:17:54 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx->options &= ~DHCPCD_TIMEOUT_IPV4LL;
|
2011-11-23 07:44:32 +08:00
|
|
|
timeout = (PROBE_NUM * PROBE_MAX) + (PROBE_WAIT * 2);
|
2009-10-09 03:56:52 +08:00
|
|
|
syslog(LOG_WARNING, "allowing %d seconds for IPv4LL timeout", timeout);
|
2014-02-12 08:39:46 +08:00
|
|
|
eloop_timeout_add_sec(ctx->eloop, timeout, handle_exit_timeout, ctx);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2014-02-04 23:53:56 +08:00
|
|
|
/* Returns the pid of the child, otherwise 0. */
|
2013-02-02 22:05:55 +08:00
|
|
|
pid_t
|
2014-02-12 08:39:46 +08:00
|
|
|
daemonise(struct dhcpcd_ctx *ctx)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2013-02-02 22:05:55 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
2014-02-22 00:10:55 +08:00
|
|
|
eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
|
2014-02-04 22:39:26 +08:00
|
|
|
errno = ENOSYS;
|
2014-02-04 23:53:56 +08:00
|
|
|
return 0;
|
2013-02-02 22:05:55 +08:00
|
|
|
#else
|
|
|
|
pid_t pid;
|
|
|
|
char buf = '\0';
|
|
|
|
int sidpipe[2], fd;
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->options & DHCPCD_DAEMONISE &&
|
|
|
|
!(ctx->options & DHCPCD_DAEMONISED))
|
|
|
|
{
|
|
|
|
if (ctx->options & DHCPCD_WAITIP4 &&
|
|
|
|
!ipv4_addrexists(ctx, NULL))
|
2014-02-04 23:53:56 +08:00
|
|
|
return 0;
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->options & DHCPCD_WAITIP6 &&
|
|
|
|
!ipv6nd_addrexists(ctx, NULL) &&
|
|
|
|
!dhcp6_addrexists(ctx, NULL))
|
2014-02-04 23:53:56 +08:00
|
|
|
return 0;
|
2014-02-12 08:39:46 +08:00
|
|
|
if ((ctx->options &
|
2013-09-01 00:48:39 +08:00
|
|
|
(DHCPCD_WAITIP | DHCPCD_WAITIP4 | DHCPCD_WAITIP6)) ==
|
2014-02-12 08:39:46 +08:00
|
|
|
(DHCPCD_WAITIP | DHCPCD_WAITIP4 | DHCPCD_WAITIP6) &&
|
|
|
|
!ipv4_addrexists(ctx, NULL) &&
|
|
|
|
!ipv6nd_addrexists(ctx, NULL) &&
|
|
|
|
!dhcp6_addrexists(ctx, NULL))
|
2014-02-04 23:53:56 +08:00
|
|
|
return 0;
|
2013-09-01 00:48:39 +08:00
|
|
|
}
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
eloop_timeout_delete(ctx->eloop, handle_exit_timeout, ctx);
|
|
|
|
if (ctx->options & DHCPCD_DAEMONISED ||
|
|
|
|
!(ctx->options & DHCPCD_DAEMONISE))
|
2013-02-02 22:05:55 +08:00
|
|
|
return 0;
|
|
|
|
/* Setup a signal pipe so parent knows when to exit. */
|
|
|
|
if (pipe(sidpipe) == -1) {
|
|
|
|
syslog(LOG_ERR, "pipe: %m");
|
2014-02-04 23:53:56 +08:00
|
|
|
return 0;
|
2013-02-02 22:05:55 +08:00
|
|
|
}
|
|
|
|
syslog(LOG_DEBUG, "forking to background");
|
|
|
|
switch (pid = fork()) {
|
|
|
|
case -1:
|
|
|
|
syslog(LOG_ERR, "fork: %m");
|
2014-02-04 23:53:56 +08:00
|
|
|
return 0;
|
2013-02-02 22:05:55 +08:00
|
|
|
case 0:
|
|
|
|
setsid();
|
|
|
|
/* Notify parent it's safe to exit as we've detached. */
|
|
|
|
close(sidpipe[0]);
|
|
|
|
if (write(sidpipe[1], &buf, 1) == -1)
|
|
|
|
syslog(LOG_ERR, "failed to notify parent: %m");
|
|
|
|
close(sidpipe[1]);
|
|
|
|
if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
|
|
|
|
dup2(fd, STDIN_FILENO);
|
|
|
|
dup2(fd, STDOUT_FILENO);
|
|
|
|
dup2(fd, STDERR_FILENO);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Wait for child to detach */
|
|
|
|
close(sidpipe[1]);
|
|
|
|
if (read(sidpipe[0], &buf, 1) == -1)
|
|
|
|
syslog(LOG_ERR, "failed to read child: %m");
|
|
|
|
close(sidpipe[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Done with the fd now */
|
|
|
|
if (pid != 0) {
|
2014-02-04 23:46:38 +08:00
|
|
|
syslog(LOG_INFO, "forked to background, child pid %d", pid);
|
2014-02-12 08:39:46 +08:00
|
|
|
write_pid(ctx->pid_fd, pid);
|
|
|
|
close(ctx->pid_fd);
|
|
|
|
ctx->pid_fd = -1;
|
|
|
|
ctx->options |= DHCPCD_FORKED;
|
|
|
|
eloop_exit(ctx->eloop, EXIT_SUCCESS);
|
2014-02-04 22:39:26 +08:00
|
|
|
return pid;
|
2013-02-02 22:05:55 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx->options |= DHCPCD_DAEMONISED;
|
2013-02-02 22:05:55 +08:00
|
|
|
return pid;
|
|
|
|
#endif
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2009-01-28 02:09:02 +08:00
|
|
|
struct interface *
|
2014-02-12 08:39:46 +08:00
|
|
|
find_interface(struct dhcpcd_ctx *ctx, const char *ifname)
|
2009-01-28 02:09:02 +08:00
|
|
|
{
|
|
|
|
struct interface *ifp;
|
2013-02-19 21:37:42 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
2009-01-28 02:09:02 +08:00
|
|
|
if (strcmp(ifp->name, ifname) == 0)
|
|
|
|
return ifp;
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
2009-01-28 02:09:02 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-16 21:17:23 +08:00
|
|
|
|
2008-09-04 17:43:52 +08:00
|
|
|
static void
|
2013-02-19 21:37:42 +08:00
|
|
|
stop_interface(struct interface *ifp)
|
2008-09-04 17:43:52 +08:00
|
|
|
{
|
2014-02-12 08:39:46 +08:00
|
|
|
struct dhcpcd_ctx *ctx;
|
2008-09-04 17:43:52 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx = ifp->ctx;
|
2013-02-19 21:37:42 +08:00
|
|
|
syslog(LOG_INFO, "%s: removing interface", ifp->name);
|
2013-06-18 17:17:28 +08:00
|
|
|
ifp->options->options |= DHCPCD_STOPPING;
|
2012-07-06 00:37:41 +08:00
|
|
|
|
|
|
|
// Remove the interface from our list
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_REMOVE(ifp->ctx->ifaces, ifp, next);
|
2013-02-19 21:37:42 +08:00
|
|
|
dhcp6_drop(ifp, NULL);
|
2013-08-20 18:29:43 +08:00
|
|
|
ipv6nd_drop(ifp);
|
2013-04-05 07:57:12 +08:00
|
|
|
dhcp_drop(ifp, "STOP");
|
2014-02-12 08:39:46 +08:00
|
|
|
eloop_timeout_delete(ctx->eloop, NULL, ifp);
|
2013-06-13 02:24:16 +08:00
|
|
|
if (ifp->options->options & DHCPCD_DEPARTED)
|
|
|
|
script_runreason(ifp, "DEPARTED");
|
2008-09-04 17:43:52 +08:00
|
|
|
free_interface(ifp);
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ctx->options & (DHCPCD_MASTER | DHCPCD_TEST)))
|
|
|
|
eloop_exit(ctx->eloop, EXIT_FAILURE);
|
2008-09-04 17:43:52 +08:00
|
|
|
}
|
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
static void
|
2013-02-03 18:35:59 +08:00
|
|
|
configure_interface1(struct interface *ifp)
|
2009-01-02 04:51:04 +08:00
|
|
|
{
|
2013-02-03 18:35:59 +08:00
|
|
|
struct if_options *ifo = ifp->options;
|
2013-07-02 03:23:19 +08:00
|
|
|
int ra_global, ra_iface;
|
2013-11-16 00:44:46 +08:00
|
|
|
|
2010-03-05 23:47:46 +08:00
|
|
|
/* Do any platform specific configuration */
|
2013-02-03 18:35:59 +08:00
|
|
|
if_conf(ifp);
|
2010-03-05 23:47:46 +08:00
|
|
|
|
2013-11-20 19:37:25 +08:00
|
|
|
/* If we want to release a lease, we can't really persist the
|
|
|
|
* address either. */
|
|
|
|
if (ifo->options & DHCPCD_RELEASE)
|
|
|
|
ifo->options &= ~DHCPCD_PERSISTENT;
|
|
|
|
|
2013-02-03 18:35:59 +08:00
|
|
|
if (ifp->flags & IFF_POINTOPOINT && !(ifo->options & DHCPCD_INFORM))
|
2009-03-24 04:10:02 +08:00
|
|
|
ifo->options |= DHCPCD_STATIC;
|
2013-02-03 18:35:59 +08:00
|
|
|
if (ifp->flags & IFF_NOARP ||
|
2009-03-24 06:02:37 +08:00
|
|
|
ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))
|
|
|
|
ifo->options &= ~(DHCPCD_ARP | DHCPCD_IPV4LL);
|
2013-02-03 18:35:59 +08:00
|
|
|
if (!(ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK | IFF_MULTICAST)))
|
2011-12-15 11:37:27 +08:00
|
|
|
ifo->options &= ~DHCPCD_IPV6RS;
|
2013-05-30 15:51:02 +08:00
|
|
|
if (ifo->options & DHCPCD_LINK && carrier_status(ifp) == LINK_UNKNOWN)
|
2009-03-20 01:52:12 +08:00
|
|
|
ifo->options &= ~DHCPCD_LINK;
|
2013-04-01 20:15:47 +08:00
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
if (ifo->metric != -1)
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->metric = ifo->metric;
|
2009-01-02 04:51:04 +08:00
|
|
|
|
2013-11-15 21:43:41 +08:00
|
|
|
if (!(ifo->options & DHCPCD_IPV6))
|
|
|
|
ifo->options &= ~DHCPCD_IPV6RS;
|
|
|
|
|
2012-07-13 16:22:04 +08:00
|
|
|
/* We want to disable kernel interface RA as early as possible. */
|
2013-02-20 00:05:12 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV6RS) {
|
2014-02-12 08:39:46 +08:00
|
|
|
ra_global = check_ipv6(ifp->ctx, NULL,
|
|
|
|
ifp->ctx->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
|
|
|
|
ra_iface = check_ipv6(ifp->ctx, ifp->name,
|
2013-07-02 03:34:54 +08:00
|
|
|
ifp->options->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
|
2013-07-02 03:23:19 +08:00
|
|
|
if (ra_global == -1 || ra_iface == -1)
|
2012-07-13 16:22:04 +08:00
|
|
|
ifo->options &= ~DHCPCD_IPV6RS;
|
2013-07-02 03:23:19 +08:00
|
|
|
else if (ra_iface == 0)
|
|
|
|
ifo->options |= DHCPCD_IPV6RA_OWN;
|
2012-07-13 16:22:04 +08:00
|
|
|
}
|
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
/* 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. */
|
2013-02-03 18:35:59 +08:00
|
|
|
if (ifp->hwlen > DHCP_CHADDR_LEN)
|
2009-01-02 04:51:04 +08:00
|
|
|
ifo->options |= DHCPCD_CLIENTID;
|
|
|
|
|
2010-01-29 04:12:54 +08:00
|
|
|
/* Firewire and InfiniBand interfaces require ClientID and
|
|
|
|
* the broadcast option being set. */
|
2013-02-03 18:35:59 +08:00
|
|
|
switch (ifp->family) {
|
2010-01-29 04:12:54 +08:00
|
|
|
case ARPHRD_IEEE1394: /* FALLTHROUGH */
|
|
|
|
case ARPHRD_INFINIBAND:
|
|
|
|
ifo->options |= DHCPCD_CLIENTID | DHCPCD_BROADCAST;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-15 21:43:41 +08:00
|
|
|
|
|
|
|
if (!(ifo->options & DHCPCD_IAID)) {
|
2013-11-16 00:44:46 +08:00
|
|
|
/*
|
|
|
|
* An IAID is for identifying a unqiue interface within
|
|
|
|
* the client. It is 4 bytes long. Working out a default
|
|
|
|
* value is problematic.
|
|
|
|
*
|
|
|
|
* Interface name and number are not stable
|
|
|
|
* between different OS's. Some OS's also cannot make
|
|
|
|
* up their mind what the interface should be called
|
|
|
|
* (yes, udev, I'm looking at you).
|
|
|
|
* Also, the name could be longer than 4 bytes.
|
|
|
|
* Also, with pluggable interfaces the name and index
|
|
|
|
* could easily get swapped per actual interface.
|
|
|
|
*
|
|
|
|
* The MAC address is 6 bytes long, the final 3
|
|
|
|
* being unique to the manufacturer and the initial 3
|
|
|
|
* being unique to the organisation which makes it.
|
|
|
|
* We could use the last 4 bytes of the MAC address
|
|
|
|
* as the IAID as it's the most stable part given the
|
|
|
|
* above, but equally it's not guaranteed to be
|
|
|
|
* unique.
|
|
|
|
*
|
|
|
|
* Given the above, and our need to reliably work
|
|
|
|
* between reboots without persitent storage,
|
|
|
|
* generating the IAID from the MAC address is the only
|
|
|
|
* logical default.
|
|
|
|
*
|
|
|
|
* dhclient uses the last 4 bytes of the MAC address.
|
|
|
|
* dibbler uses an increamenting counter.
|
|
|
|
* wide-dhcpv6 uses 0 or a configured value.
|
|
|
|
* odhcp6c uses 1.
|
|
|
|
* Windows 7 uses the first 3 bytes of the MAC address
|
|
|
|
* and an unknown byte.
|
|
|
|
* dhcpcd-6.1.0 and earlier used the interface name,
|
|
|
|
* falling back to interface index if name > 4.
|
|
|
|
*/
|
|
|
|
memcpy(ifo->iaid, ifp->hwaddr + ifp->hwlen - sizeof(ifo->iaid),
|
|
|
|
sizeof(ifo->iaid));
|
|
|
|
#if 0
|
2013-11-15 21:43:41 +08:00
|
|
|
len = strlen(ifp->name);
|
|
|
|
if (len <= sizeof(ifo->iaid)) {
|
|
|
|
memcpy(ifo->iaid, ifp->name, len);
|
|
|
|
memset(ifo->iaid + len, 0, sizeof(ifo->iaid) - len);
|
|
|
|
} else {
|
|
|
|
/* IAID is the same size as a uint32_t */
|
|
|
|
len = htonl(ifp->index);
|
|
|
|
memcpy(ifo->iaid, &len, sizeof(len));
|
|
|
|
}
|
2013-11-16 00:44:46 +08:00
|
|
|
#endif
|
2013-11-15 21:43:41 +08:00
|
|
|
ifo->options |= DHCPCD_IAID;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef INET6
|
|
|
|
if (ifo->ia == NULL && ifo->options & DHCPCD_IPV6) {
|
|
|
|
ifo->ia = malloc(sizeof(*ifo->ia));
|
|
|
|
if (ifo->ia == NULL)
|
|
|
|
syslog(LOG_ERR, "%s: %m", __func__);
|
|
|
|
else {
|
|
|
|
if (ifo->ia_type == 0)
|
|
|
|
ifo->ia_type = D6_OPTION_IA_NA;
|
|
|
|
memcpy(ifo->ia->iaid, ifo->iaid, sizeof(ifo->iaid));
|
|
|
|
ifo->ia_len = 1;
|
|
|
|
ifo->ia->sla = NULL;
|
|
|
|
ifo->ia->sla_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-25 09:35:53 +08:00
|
|
|
|
|
|
|
/* If we are not sending an authentication option, don't require it */
|
|
|
|
if (!(ifo->auth.options & DHCPCD_AUTH_SEND))
|
|
|
|
ifo->auth.options &= ~DHCPCD_AUTH_REQUIRE;
|
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
}
|
|
|
|
|
2009-03-31 16:35:38 +08:00
|
|
|
int
|
2013-02-03 18:35:59 +08:00
|
|
|
select_profile(struct interface *ifp, const char *profile)
|
2009-03-31 16:35:38 +08:00
|
|
|
{
|
|
|
|
struct if_options *ifo;
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ifo = read_config(ifp->ctx, ifp->name, ifp->ssid, profile);
|
2009-03-31 16:35:38 +08:00
|
|
|
if (ifo == NULL) {
|
2013-02-03 18:35:59 +08:00
|
|
|
syslog(LOG_DEBUG, "%s: no profile %s", ifp->name, profile);
|
2014-02-24 22:40:29 +08:00
|
|
|
return -1;
|
2009-03-31 16:35:38 +08:00
|
|
|
}
|
|
|
|
if (profile != NULL) {
|
2013-02-04 06:55:45 +08:00
|
|
|
strlcpy(ifp->profile, profile, sizeof(ifp->profile));
|
2009-03-31 16:35:38 +08:00
|
|
|
syslog(LOG_INFO, "%s: selected profile %s",
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->name, profile);
|
2009-03-31 16:35:38 +08:00
|
|
|
} else
|
2013-02-04 06:55:45 +08:00
|
|
|
*ifp->profile = '\0';
|
2013-02-03 18:35:59 +08:00
|
|
|
free_options(ifp->options);
|
|
|
|
ifp->options = ifo;
|
2010-11-09 08:34:38 +08:00
|
|
|
if (profile)
|
2013-02-03 18:35:59 +08:00
|
|
|
configure_interface1(ifp);
|
2014-02-24 22:40:29 +08:00
|
|
|
return 1;
|
2009-03-31 16:35:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-02-03 18:35:59 +08:00
|
|
|
configure_interface(struct interface *ifp, int argc, char **argv)
|
2009-03-31 16:35:38 +08:00
|
|
|
{
|
2013-02-03 18:35:59 +08:00
|
|
|
|
|
|
|
select_profile(ifp, NULL);
|
2014-02-12 08:39:46 +08:00
|
|
|
add_options(ifp->ctx, ifp->name, ifp->options, argc, argv);
|
2013-02-03 18:35:59 +08:00
|
|
|
configure_interface1(ifp);
|
2009-03-31 16:35:38 +08:00
|
|
|
}
|
|
|
|
|
2011-11-23 17:03:24 +08:00
|
|
|
void
|
2014-02-12 08:39:46 +08:00
|
|
|
handle_carrier(struct dhcpcd_ctx *ctx, int carrier, int flags,
|
|
|
|
const char *ifname)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2013-02-03 18:35:59 +08:00
|
|
|
struct interface *ifp;
|
2008-09-04 22:08:14 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ifp = find_interface(ctx, ifname);
|
2013-09-10 04:22:12 +08:00
|
|
|
if (ifp == NULL || !(ifp->options->options & DHCPCD_LINK))
|
2008-09-02 21:28:11 +08:00
|
|
|
return;
|
2012-02-06 03:32:17 +08:00
|
|
|
|
2013-05-30 15:51:02 +08:00
|
|
|
if (carrier == LINK_UNKNOWN)
|
|
|
|
carrier = carrier_status(ifp); /* will set ifp->flags */
|
|
|
|
else
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->flags = flags;
|
2012-02-06 03:32:17 +08:00
|
|
|
|
2013-05-30 15:51:02 +08:00
|
|
|
if (carrier == LINK_UNKNOWN)
|
2009-10-14 23:16:07 +08:00
|
|
|
syslog(LOG_ERR, "%s: carrier_status: %m", ifname);
|
2013-05-30 15:51:02 +08:00
|
|
|
/* IFF_RUNNING is checked, if needed, earlier and is OS dependant */
|
|
|
|
else if (carrier == LINK_DOWN || (ifp->flags & IFF_UP) == 0) {
|
2013-02-03 18:35:59 +08:00
|
|
|
if (ifp->carrier != LINK_DOWN) {
|
2013-06-07 02:39:12 +08:00
|
|
|
if (ifp->carrier == LINK_UP)
|
|
|
|
syslog(LOG_INFO, "%s: carrier lost", ifp->name);
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->carrier = LINK_DOWN;
|
|
|
|
dhcp6_drop(ifp, "EXPIRE6");
|
2013-08-20 18:29:43 +08:00
|
|
|
ipv6nd_drop(ifp);
|
2013-06-03 14:49:34 +08:00
|
|
|
/* Don't blindly delete our knowledge of LL addresses.
|
|
|
|
* We need to listen to what the kernel does with
|
|
|
|
* them as some OS's will remove, mark tentative or
|
|
|
|
* do nothing. */
|
|
|
|
ipv6_free_ll_callbacks(ifp);
|
2013-02-03 18:35:59 +08:00
|
|
|
dhcp_drop(ifp, "NOCARRIER");
|
2008-09-04 19:30:11 +08:00
|
|
|
}
|
2013-05-30 15:51:02 +08:00
|
|
|
} else if (carrier == LINK_UP && ifp->flags & IFF_UP) {
|
2013-02-03 18:35:59 +08:00
|
|
|
if (ifp->carrier != LINK_UP) {
|
|
|
|
syslog(LOG_INFO, "%s: carrier acquired", ifp->name);
|
2013-06-07 02:39:12 +08:00
|
|
|
ifp->carrier = LINK_UP;
|
2013-06-25 16:31:11 +08:00
|
|
|
#if !defined(__linux__) && !defined(__NetBSD__)
|
|
|
|
/* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the
|
|
|
|
* hardware address changes so we have to go
|
|
|
|
* through the disovery process to work it out. */
|
2014-02-12 08:39:46 +08:00
|
|
|
handle_interface(ctx, 0, ifp->name);
|
2013-06-25 16:31:11 +08:00
|
|
|
#endif
|
2013-02-03 18:35:59 +08:00
|
|
|
if (ifp->wireless)
|
|
|
|
getifssid(ifp->name, ifp->ssid);
|
2014-02-12 08:39:46 +08:00
|
|
|
configure_interface(ifp, ctx->argc, ctx->argv);
|
2013-02-04 06:55:45 +08:00
|
|
|
script_runreason(ifp, "CARRIER");
|
2013-02-03 18:35:59 +08:00
|
|
|
start_interface(ifp);
|
2008-04-21 17:40:14 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
}
|
2008-04-21 17:40:14 +08:00
|
|
|
|
2013-11-16 00:44:46 +08:00
|
|
|
static void
|
|
|
|
warn_iaid_conflict(struct interface *ifp, uint8_t *iaid)
|
|
|
|
{
|
|
|
|
struct interface *ifn;
|
|
|
|
size_t i;
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifn, ifp->ctx->ifaces, next) {
|
2013-11-16 00:44:46 +08:00
|
|
|
if (ifn == ifp)
|
|
|
|
continue;
|
|
|
|
if (memcmp(ifn->options->iaid, iaid,
|
|
|
|
sizeof(ifn->options->iaid)) == 0)
|
|
|
|
break;
|
|
|
|
for (i = 0; i < ifn->options->ia_len; i++) {
|
|
|
|
if (memcmp(&ifn->options->ia[i].iaid, iaid,
|
|
|
|
sizeof(ifn->options->ia[i].iaid)) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is only a problem if the interfaces are on the same network. */
|
|
|
|
if (ifn)
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"%s: IAID conflicts with one assigned to %s",
|
|
|
|
ifp->name, ifn->name);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-02-03 18:35:59 +08:00
|
|
|
struct interface *ifp = arg;
|
|
|
|
struct if_options *ifo = ifp->options;
|
2010-09-10 02:27:00 +08:00
|
|
|
int nolease;
|
2013-11-15 21:43:41 +08:00
|
|
|
size_t i;
|
2014-02-12 08:39:46 +08:00
|
|
|
char buf[DUID_LEN * 3];
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
handle_carrier(ifp->ctx, LINK_UNKNOWN, 0, ifp->name);
|
2013-02-03 18:35:59 +08:00
|
|
|
if (ifp->carrier == LINK_DOWN) {
|
|
|
|
syslog(LOG_INFO, "%s: waiting for carrier", ifp->name);
|
2008-09-10 01:07:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-15 21:43:41 +08:00
|
|
|
if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6)) {
|
|
|
|
/* Report client DUID */
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ifp->ctx->duid == NULL) {
|
2013-11-15 21:43:41 +08:00
|
|
|
if (duid_init(ifp) == 0)
|
|
|
|
return;
|
|
|
|
syslog(LOG_INFO, "DUID %s",
|
2014-02-12 08:39:46 +08:00
|
|
|
hwaddr_ntoa(ifp->ctx->duid, ifp->ctx->duid_len,
|
|
|
|
buf, sizeof(buf)));
|
2013-11-15 21:43:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Report IAIDs */
|
|
|
|
syslog(LOG_INFO, "%s: IAID %s", ifp->name,
|
2014-02-12 08:39:46 +08:00
|
|
|
hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid),
|
|
|
|
buf, sizeof(buf)));
|
2013-11-16 00:44:46 +08:00
|
|
|
warn_iaid_conflict(ifp, ifo->iaid);
|
2013-11-15 21:43:41 +08:00
|
|
|
for (i = 0; i < ifo->ia_len; i++) {
|
|
|
|
if (memcmp(ifo->iaid, ifo->ia[i].iaid,
|
|
|
|
sizeof(ifo->iaid)))
|
2013-11-16 00:44:46 +08:00
|
|
|
{
|
2013-11-15 21:43:41 +08:00
|
|
|
syslog(LOG_INFO, "%s: IAID %s", ifp->name,
|
|
|
|
hwaddr_ntoa(ifo->ia[i].iaid,
|
2014-02-12 08:39:46 +08:00
|
|
|
sizeof(ifo->ia[i].iaid),
|
|
|
|
buf, sizeof(buf)));
|
2013-11-16 00:44:46 +08:00
|
|
|
warn_iaid_conflict(ifp, ifo->ia[i].iaid);
|
|
|
|
}
|
2013-11-15 21:43:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-07 07:40:15 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV6) {
|
2013-04-05 07:25:31 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV6RS &&
|
|
|
|
!(ifo->options & DHCPCD_INFORM))
|
2013-08-20 18:29:43 +08:00
|
|
|
ipv6nd_startrs(ifp);
|
2013-04-05 07:25:31 +08:00
|
|
|
|
2013-04-02 15:01:11 +08:00
|
|
|
if (!(ifo->options & DHCPCD_IPV6RS)) {
|
|
|
|
if (ifo->options & DHCPCD_IA_FORCED)
|
2013-05-31 06:39:17 +08:00
|
|
|
nolease = dhcp6_start(ifp, DH6S_INIT);
|
2013-05-24 17:08:23 +08:00
|
|
|
else {
|
|
|
|
nolease = dhcp6_find_delegates(ifp);
|
|
|
|
/* Enabling the below doesn't really make
|
|
|
|
* sense as there is currently no standard
|
|
|
|
* to push routes via DHCPv6.
|
|
|
|
* (There is an expired working draft,
|
|
|
|
* maybe abandoned?)
|
|
|
|
* You can also get it to work by forcing
|
|
|
|
* an IA as shown above. */
|
|
|
|
#if 0
|
|
|
|
/* With no RS or delegates we might
|
|
|
|
* as well try and solicit a DHCPv6 address */
|
|
|
|
if (nolease == 0)
|
2013-05-31 06:39:17 +08:00
|
|
|
nolease = dhcp6_start(ifp, DH6S_INIT);
|
2013-05-24 17:08:23 +08:00
|
|
|
#endif
|
|
|
|
}
|
2013-04-02 15:01:11 +08:00
|
|
|
if (nolease == -1)
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"%s: dhcp6_start: %m", ifp->name);
|
2013-04-01 20:15:47 +08:00
|
|
|
}
|
2012-11-07 07:40:15 +08:00
|
|
|
}
|
2012-10-12 18:31:51 +08:00
|
|
|
|
2013-02-02 22:05:55 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV4)
|
2013-02-03 18:35:59 +08:00
|
|
|
dhcp_start(ifp);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2013-02-16 04:33:13 +08:00
|
|
|
static void
|
2014-02-12 08:39:46 +08:00
|
|
|
handle_link(void *arg)
|
2013-02-16 04:33:13 +08:00
|
|
|
{
|
2014-02-12 08:39:46 +08:00
|
|
|
struct dhcpcd_ctx *ctx;
|
2013-02-16 04:33:13 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx = arg;
|
|
|
|
if (manage_link(ctx) == -1 && errno != ENXIO && errno != ENODEV)
|
2013-02-16 04:33:13 +08:00
|
|
|
syslog(LOG_ERR, "manage_link: %m");
|
|
|
|
}
|
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
static void
|
2013-02-03 18:35:59 +08:00
|
|
|
init_state(struct interface *ifp, int argc, char **argv)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2013-02-16 04:33:13 +08:00
|
|
|
struct if_options *ifo;
|
2013-02-04 06:55:45 +08:00
|
|
|
const char *reason = NULL;
|
2013-02-03 22:52:24 +08:00
|
|
|
|
2013-02-03 18:35:59 +08:00
|
|
|
configure_interface(ifp, argc, argv);
|
2013-02-16 04:33:13 +08:00
|
|
|
ifo = ifp->options;
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV4 && ipv4_init(ifp->ctx) == -1) {
|
2013-02-19 23:23:53 +08:00
|
|
|
syslog(LOG_ERR, "ipv4_init: %m");
|
|
|
|
ifo->options &= ~DHCPCD_IPV4;
|
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV6 && ipv6_init(ifp->ctx) == NULL) {
|
2013-02-16 04:33:13 +08:00
|
|
|
syslog(LOG_ERR, "ipv6_init: %m");
|
2013-02-19 23:23:53 +08:00
|
|
|
ifo->options &= ~DHCPCD_IPV6RS;
|
2013-02-16 04:33:13 +08:00
|
|
|
}
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ifp->ctx->options & DHCPCD_TEST))
|
2013-02-04 06:55:45 +08:00
|
|
|
script_runreason(ifp, "PREINIT");
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2013-02-16 04:33:13 +08:00
|
|
|
if (ifo->options & DHCPCD_LINK) {
|
2013-02-03 18:35:59 +08:00
|
|
|
switch (carrier_status(ifp)) {
|
2013-09-05 21:27:52 +08:00
|
|
|
case LINK_DOWN:
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->carrier = LINK_DOWN;
|
2013-02-04 06:55:45 +08:00
|
|
|
reason = "NOCARRIER";
|
2008-04-24 21:18:59 +08:00
|
|
|
break;
|
2013-09-05 21:27:52 +08:00
|
|
|
case LINK_UP:
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->carrier = LINK_UP;
|
2013-02-04 06:55:45 +08:00
|
|
|
reason = "CARRIER";
|
2008-09-02 21:28:11 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->carrier = LINK_UNKNOWN;
|
2009-01-15 21:31:46 +08:00
|
|
|
return;
|
2008-04-24 21:18:59 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
if (reason && !(ifp->ctx->options & DHCPCD_TEST))
|
2013-02-04 06:55:45 +08:00
|
|
|
script_runreason(ifp, reason);
|
2008-09-10 01:07:48 +08:00
|
|
|
} else
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->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
|
2014-02-12 08:39:46 +08:00
|
|
|
handle_interface(void *arg, int action, const char *ifname)
|
2008-09-04 19:30:11 +08:00
|
|
|
{
|
2014-02-12 08:39:46 +08:00
|
|
|
struct dhcpcd_ctx *ctx;
|
2013-02-19 21:37:42 +08:00
|
|
|
struct if_head *ifs;
|
|
|
|
struct interface *ifp, *ifn, *ifl = NULL;
|
2013-04-01 20:15:47 +08:00
|
|
|
const char * const argv[] = { ifname };
|
2008-09-04 19:30:11 +08:00
|
|
|
int i;
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx = arg;
|
2009-03-20 01:52:12 +08:00
|
|
|
if (action == -1) {
|
2014-02-12 08:39:46 +08:00
|
|
|
ifp = find_interface(ctx, ifname);
|
2013-06-06 20:38:15 +08:00
|
|
|
if (ifp != NULL) {
|
2013-06-13 02:24:16 +08:00
|
|
|
ifp->options->options |= DHCPCD_DEPARTED;
|
2009-03-20 01:52:12 +08:00
|
|
|
stop_interface(ifp);
|
2013-06-06 20:38:15 +08:00
|
|
|
}
|
2009-03-20 01:52:12 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-04 19:30:11 +08:00
|
|
|
/* If running off an interface list, check it's in it. */
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->ifc) {
|
|
|
|
for (i = 0; i < ctx->ifc; i++)
|
|
|
|
if (strcmp(ctx->ifv[i], ifname) == 0)
|
2008-09-04 19:30:11 +08:00
|
|
|
break;
|
2014-02-12 08:39:46 +08:00
|
|
|
if (i >= ctx->ifc)
|
2008-09-04 19:30:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ifs = discover_interfaces(ctx, -1, UNCONST(argv));
|
2013-02-19 21:37:42 +08:00
|
|
|
TAILQ_FOREACH_SAFE(ifp, ifs, next, ifn) {
|
2009-09-01 05:51:17 +08:00
|
|
|
if (strcmp(ifp->name, ifname) != 0)
|
|
|
|
continue;
|
|
|
|
/* Check if we already have the interface */
|
2014-02-12 08:39:46 +08:00
|
|
|
ifl = find_interface(ctx, ifp->name);
|
2013-02-19 21:37:42 +08:00
|
|
|
if (ifl) {
|
2009-10-15 15:33:24 +08:00
|
|
|
/* The flags and hwaddr could have changed */
|
2013-02-19 21:37:42 +08:00
|
|
|
ifl->flags = ifp->flags;
|
|
|
|
ifl->hwlen = ifp->hwlen;
|
2009-10-15 15:33:24 +08:00
|
|
|
if (ifp->hwlen != 0)
|
2013-02-19 21:37:42 +08:00
|
|
|
memcpy(ifl->hwaddr, ifp->hwaddr, ifl->hwlen);
|
2009-10-15 15:33:24 +08:00
|
|
|
} else {
|
2013-02-19 21:37:42 +08:00
|
|
|
TAILQ_REMOVE(ifs, ifp, next);
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next);
|
2009-10-15 15:33:24 +08:00
|
|
|
}
|
2013-06-25 16:31:11 +08:00
|
|
|
if (action == 1) {
|
2014-02-12 08:39:46 +08:00
|
|
|
init_state(ifp, ctx->argc, ctx->argv);
|
2013-06-25 16:31:11 +08:00
|
|
|
start_interface(ifp);
|
|
|
|
}
|
2008-09-04 19:30:11 +08:00
|
|
|
}
|
2013-02-19 21:37:42 +08:00
|
|
|
|
|
|
|
/* Free our discovered list */
|
|
|
|
while ((ifp = TAILQ_FIRST(ifs))) {
|
|
|
|
TAILQ_REMOVE(ifs, ifp, next);
|
|
|
|
free_interface(ifp);
|
|
|
|
}
|
|
|
|
free(ifs);
|
2008-09-04 19:30:11 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 00:33:45 +08:00
|
|
|
void
|
2014-02-12 08:39:46 +08:00
|
|
|
handle_hwaddr(struct dhcpcd_ctx *ctx, const char *ifname,
|
|
|
|
const uint8_t *hwaddr, size_t hwlen)
|
2010-11-13 00:33:45 +08:00
|
|
|
{
|
|
|
|
struct interface *ifp;
|
2014-02-12 08:39:46 +08:00
|
|
|
char buf[hwlen * 3];
|
2010-11-13 00:33:45 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ifp = find_interface(ctx, ifname);
|
2013-06-25 16:31:11 +08:00
|
|
|
if (ifp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (hwlen > sizeof(ifp->hwaddr)) {
|
|
|
|
errno = ENOBUFS;
|
|
|
|
syslog(LOG_ERR, "%s: %s: %m", ifp->name, __func__);
|
|
|
|
return;
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
2013-06-25 16:31:11 +08:00
|
|
|
|
|
|
|
if (ifp->hwlen == hwlen && memcmp(ifp->hwaddr, hwaddr, hwlen) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
syslog(LOG_INFO, "%s: new hardware address: %s", ifp->name,
|
2014-02-12 08:39:46 +08:00
|
|
|
hwaddr_ntoa(hwaddr, hwlen, buf, sizeof(buf)));
|
2013-06-25 16:31:11 +08:00
|
|
|
ifp->hwlen = hwlen;
|
|
|
|
memcpy(ifp->hwaddr, hwaddr, hwlen);
|
2010-11-13 00:33:45 +08:00
|
|
|
}
|
|
|
|
|
2010-06-09 23:26:55 +08:00
|
|
|
static void
|
2013-02-03 18:35:59 +08:00
|
|
|
if_reboot(struct interface *ifp, int argc, char **argv)
|
2010-06-09 23:26:55 +08:00
|
|
|
{
|
2013-02-04 06:55:45 +08:00
|
|
|
int oldopts;
|
|
|
|
|
|
|
|
oldopts = ifp->options->options;
|
2013-06-08 04:03:28 +08:00
|
|
|
script_runreason(ifp, "RECONFIGURE");
|
2013-02-03 18:35:59 +08:00
|
|
|
configure_interface(ifp, argc, argv);
|
2013-03-26 18:42:30 +08:00
|
|
|
dhcp_reboot_newopts(ifp, oldopts);
|
2013-06-07 20:44:19 +08:00
|
|
|
dhcp6_reboot(ifp);
|
2013-02-03 18:35:59 +08:00
|
|
|
start_interface(ifp);
|
2010-06-09 23:26:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-02-12 08:39:46 +08:00
|
|
|
reconf_reboot(struct dhcpcd_ctx *ctx, int action, int argc, char **argv, int oi)
|
2010-06-09 23:26:55 +08:00
|
|
|
{
|
2013-02-19 21:37:42 +08:00
|
|
|
struct if_head *ifs;
|
|
|
|
struct interface *ifn, *ifp;
|
2013-04-01 20:15:47 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ifs = discover_interfaces(ctx, argc - oi, argv + oi);
|
2010-06-09 23:26:55 +08:00
|
|
|
if (ifs == NULL)
|
|
|
|
return;
|
|
|
|
|
2013-02-19 21:37:42 +08:00
|
|
|
while ((ifp = TAILQ_FIRST(ifs))) {
|
|
|
|
TAILQ_REMOVE(ifs, ifp, next);
|
2014-02-12 08:39:46 +08:00
|
|
|
ifn = find_interface(ctx, ifp->name);
|
2010-06-09 23:26:55 +08:00
|
|
|
if (ifn) {
|
|
|
|
if (action)
|
2010-06-10 18:38:37 +08:00
|
|
|
if_reboot(ifn, argc, argv);
|
2013-02-04 06:55:45 +08:00
|
|
|
else
|
2013-02-02 22:05:55 +08:00
|
|
|
ipv4_applyaddr(ifn);
|
2010-06-09 23:26:55 +08:00
|
|
|
free_interface(ifp);
|
|
|
|
} else {
|
|
|
|
init_state(ifp, argc, argv);
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_INSERT_TAIL(ctx->ifaces, ifp, next);
|
2010-06-09 23:26:55 +08:00
|
|
|
start_interface(ifp);
|
|
|
|
}
|
|
|
|
}
|
2013-02-19 21:37:42 +08:00
|
|
|
free(ifs);
|
2010-06-09 23:26:55 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
sort_interfaces(ctx);
|
2010-06-09 23:26:55 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
static void
|
|
|
|
stop_all_interfaces(struct dhcpcd_ctx *ctx, int do_release)
|
|
|
|
{
|
|
|
|
struct interface *ifp;
|
|
|
|
|
|
|
|
/* drop_dhcp could change the order, so we do it like this. */
|
|
|
|
for (;;) {
|
|
|
|
/* Be sane and drop the last config first */
|
|
|
|
ifp = TAILQ_LAST(ctx->ifaces, if_head);
|
|
|
|
if (ifp == NULL)
|
|
|
|
break;
|
|
|
|
if (do_release) {
|
|
|
|
ifp->options->options |= DHCPCD_RELEASE;
|
|
|
|
ifp->options->options &= ~DHCPCD_PERSISTENT;
|
|
|
|
}
|
|
|
|
ifp->options->options |= DHCPCD_EXITING;
|
|
|
|
stop_interface(ifp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_SIGNALS
|
2014-02-07 23:49:33 +08:00
|
|
|
struct dhcpcd_siginfo {
|
|
|
|
int signo;
|
|
|
|
pid_t pid;
|
|
|
|
} dhcpcd_siginfo;
|
|
|
|
|
2013-06-07 20:44:19 +08:00
|
|
|
static void
|
2014-02-07 22:39:22 +08:00
|
|
|
handle_signal1(void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2014-02-12 08:39:46 +08:00
|
|
|
struct dhcpcd_ctx *ctx;
|
2014-02-07 23:49:33 +08:00
|
|
|
struct dhcpcd_siginfo *si;
|
2012-04-01 15:38:52 +08:00
|
|
|
struct interface *ifp;
|
2014-02-07 22:39:22 +08:00
|
|
|
struct if_options *ifo;
|
2013-02-18 18:35:47 +08:00
|
|
|
int do_release;
|
2008-04-24 21:18:59 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx = dhcpcd_ctx;
|
2014-02-07 22:39:22 +08:00
|
|
|
si = arg;
|
2012-04-01 15:38:52 +08:00
|
|
|
do_release = 0;
|
2014-02-07 23:49:33 +08:00
|
|
|
switch (si->signo) {
|
2008-09-02 21:28:11 +08:00
|
|
|
case SIGINT:
|
2013-06-07 20:44:19 +08:00
|
|
|
syslog(LOG_INFO, "received SIGINT from PID %d, stopping",
|
2014-02-07 23:49:33 +08:00
|
|
|
(int)si->pid);
|
2008-09-02 21:28:11 +08:00
|
|
|
break;
|
|
|
|
case SIGTERM:
|
2013-06-07 20:44:19 +08:00
|
|
|
syslog(LOG_INFO, "received SIGTERM from PID %d, stopping",
|
2014-02-07 23:49:33 +08:00
|
|
|
(int)si->pid);
|
2008-09-02 21:28:11 +08:00
|
|
|
break;
|
2008-09-04 00:49:28 +08:00
|
|
|
case SIGALRM:
|
2014-02-07 22:39:22 +08:00
|
|
|
syslog(LOG_INFO, "received SIGALRM from PID %d, rebinding",
|
2014-02-07 23:49:33 +08:00
|
|
|
(int)si->pid);
|
2014-02-07 22:39:22 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
free_globals(ctx);
|
|
|
|
ifo = read_config(ctx, NULL, NULL, NULL);
|
|
|
|
add_options(ctx, NULL, ifo, ctx->argc, ctx->argv);
|
2014-02-07 22:39:22 +08:00
|
|
|
/* We need to preserve these two options. */
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->options & DHCPCD_MASTER)
|
2014-02-07 22:39:22 +08:00
|
|
|
ifo->options |= DHCPCD_MASTER;
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx->options & DHCPCD_DAEMONISED)
|
2014-02-07 22:39:22 +08:00
|
|
|
ifo->options |= DHCPCD_DAEMONISED;
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx->options = ifo->options;
|
2014-02-07 22:39:22 +08:00
|
|
|
free_options(ifo);
|
2014-02-25 18:24:01 +08:00
|
|
|
/* Preserve any options passed on the commandline
|
|
|
|
* when we were started. */
|
|
|
|
reconf_reboot(ctx, 1, ctx->argc, ctx->argv,
|
|
|
|
ctx->argc - ctx->ifc);
|
2008-09-15 23:23:46 +08:00
|
|
|
return;
|
2008-09-04 00:49:28 +08:00
|
|
|
case SIGHUP:
|
2013-06-07 20:44:19 +08:00
|
|
|
syslog(LOG_INFO, "received SIGHUP from PID %d, releasing",
|
2014-02-07 23:49:33 +08:00
|
|
|
(int)si->pid);
|
2008-09-04 00:49:28 +08:00
|
|
|
do_release = 1;
|
|
|
|
break;
|
2009-03-20 18:21:12 +08:00
|
|
|
case SIGUSR1:
|
2014-02-07 22:39:22 +08:00
|
|
|
syslog(LOG_INFO, "received SIGUSR from PID %d, reconfiguring",
|
2014-02-07 23:49:33 +08:00
|
|
|
(int)si->pid);
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
|
|
|
ipv4_applyaddr(ifp);
|
2014-02-07 22:39:22 +08:00
|
|
|
}
|
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,
|
2013-06-07 20:44:19 +08:00
|
|
|
"received signal %d from PID %d, "
|
|
|
|
"but don't know what to do with it",
|
2014-02-07 23:49:33 +08:00
|
|
|
si->signo, (int)si->pid);
|
2008-09-02 21:28:11 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-04-24 21:18:59 +08:00
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
if (!(ctx->options & DHCPCD_TEST))
|
|
|
|
stop_all_interfaces(ctx, do_release);
|
2014-02-12 08:39:46 +08:00
|
|
|
eloop_exit(ctx->eloop, EXIT_FAILURE);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2008-04-24 21:18:59 +08:00
|
|
|
|
2014-02-07 22:39:22 +08:00
|
|
|
static void
|
|
|
|
handle_signal(__unused int sig, siginfo_t *siginfo, __unused void *context)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* So that we can operate safely under a signal we instruct
|
|
|
|
* eloop to pass a copy of the siginfo structure to handle_signal1
|
|
|
|
* as the very first thing to do. */
|
2014-02-07 23:49:33 +08:00
|
|
|
dhcpcd_siginfo.signo = siginfo->si_signo;
|
|
|
|
dhcpcd_siginfo.pid = siginfo->si_pid;
|
2014-02-12 08:39:46 +08:00
|
|
|
eloop_timeout_add_now(dhcpcd_ctx->eloop,
|
|
|
|
handle_signal1, &dhcpcd_siginfo);
|
2014-02-07 22:39:22 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
static int
|
|
|
|
signal_init(void (*func)(int, siginfo_t *, void *), sigset_t *oldset)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
struct sigaction sa;
|
|
|
|
sigset_t newset;
|
|
|
|
|
|
|
|
sigfillset(&newset);
|
|
|
|
if (sigprocmask(SIG_SETMASK, &newset, oldset) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_sigaction = func;
|
|
|
|
sa.sa_flags = SA_SIGINFO;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
|
|
|
|
for (i = 0; handle_sigs[i]; i++) {
|
|
|
|
if (sigaction(handle_sigs[i], &sa, NULL) == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
int
|
2014-02-12 08:39:46 +08:00
|
|
|
handle_args(struct dhcpcd_ctx *ctx, 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;
|
2012-04-01 15:38:52 +08:00
|
|
|
int do_exit = 0, do_release = 0, do_reboot = 0;
|
2009-03-20 18:21:12 +08:00
|
|
|
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) {
|
2014-02-12 08:39:46 +08:00
|
|
|
len = strlen(ctx->cffile) + 1;
|
2009-02-13 23:39:34 +08:00
|
|
|
iov[0].iov_base = &len;
|
|
|
|
iov[0].iov_len = sizeof(ssize_t);
|
2014-02-12 08:39:46 +08:00
|
|
|
iov[1].iov_base = UNCONST(ctx->cffile);
|
2009-02-13 23:39:34 +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-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) {
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
2009-01-15 22:22:40 +08:00
|
|
|
len++;
|
2012-10-13 03:10:04 +08:00
|
|
|
if (D6_STATE_RUNNING(ifp))
|
|
|
|
len++;
|
2013-08-20 18:29:43 +08:00
|
|
|
if (ipv6nd_has_ra(ifp))
|
2012-02-03 00:22:40 +08:00
|
|
|
len++;
|
|
|
|
}
|
2009-01-29 21:01:29 +08:00
|
|
|
len = write(fd->fd, &len, sizeof(len));
|
|
|
|
if (len != sizeof(len))
|
|
|
|
return -1;
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
2009-01-15 22:22:40 +08:00
|
|
|
send_interface(fd->fd, ifp);
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
2009-01-15 22:22:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
opt = 0;
|
|
|
|
while (argv[++opt] != NULL) {
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
2012-02-03 00:22:40 +08:00
|
|
|
if (strcmp(argv[opt], ifp->name) == 0) {
|
2009-01-15 22:22:40 +08:00
|
|
|
len++;
|
2012-10-13 03:10:04 +08:00
|
|
|
if (D6_STATE_RUNNING(ifp))
|
|
|
|
len++;
|
2013-08-20 18:29:43 +08:00
|
|
|
if (ipv6nd_has_ra(ifp))
|
2012-02-03 00:22:40 +08:00
|
|
|
len++;
|
|
|
|
}
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
2009-01-15 22:22:40 +08:00
|
|
|
}
|
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) {
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
2009-01-15 22:22:40 +08:00
|
|
|
if (strcmp(argv[opt], ifp->name) == 0)
|
|
|
|
send_interface(fd->fd, ifp);
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
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 */
|
2014-02-22 00:10:55 +08:00
|
|
|
len = 1;
|
2009-02-06 05:06:57 +08:00
|
|
|
for (opt = 0; opt < argc; opt++)
|
|
|
|
len += strlen(argv[opt]) + 1;
|
2014-02-22 00:10:55 +08:00
|
|
|
tmp = malloc(len);
|
2013-02-16 21:21:35 +08:00
|
|
|
if (tmp == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-22 00:10:55 +08:00
|
|
|
p = tmp;
|
2009-02-06 05:06:57 +08:00
|
|
|
for (opt = 0; opt < argc; opt++) {
|
|
|
|
l = strlen(argv[opt]);
|
2014-02-22 00:10:55 +08:00
|
|
|
strlcpy(p, argv[opt], len);
|
|
|
|
len -= l + 1;
|
2009-02-06 05:06:57 +08:00
|
|
|
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':
|
2012-04-01 15:38:52 +08:00
|
|
|
/* Assumed if below not set */
|
2009-03-20 18:21:12 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-06 16:37:02 +08:00
|
|
|
if (do_release || do_exit) {
|
2014-02-22 00:10:55 +08:00
|
|
|
if (optind == argc) {
|
|
|
|
stop_all_interfaces(ctx, do_release);
|
|
|
|
eloop_exit(ctx->eloop, EXIT_SUCCESS);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-04 16:27:38 +08:00
|
|
|
for (oi = optind; oi < argc; oi++) {
|
2014-02-12 08:39:46 +08:00
|
|
|
if ((ifp = find_interface(ctx, argv[oi])) == NULL)
|
2008-09-04 16:27:38 +08:00
|
|
|
continue;
|
2013-11-20 19:37:25 +08:00
|
|
|
if (do_release) {
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->options->options |= DHCPCD_RELEASE;
|
2013-11-20 19:37:25 +08:00
|
|
|
ifp->options->options &= ~DHCPCD_PERSISTENT;
|
|
|
|
}
|
2013-08-25 18:22:48 +08:00
|
|
|
ifp->options->options |= DHCPCD_EXITING;
|
2009-01-21 00:33:11 +08:00
|
|
|
stop_interface(ifp);
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-25 18:24:01 +08:00
|
|
|
/* XXX: Respect initial commandline options? */
|
2014-02-12 08:39:46 +08:00
|
|
|
reconf_reboot(ctx, do_reboot, argc, argv, optind);
|
2008-09-04 00:49:28 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-02 21:28:11 +08:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2014-02-12 08:39:46 +08:00
|
|
|
struct dhcpcd_ctx ctx;
|
2014-02-12 19:55:43 +08:00
|
|
|
char pidfile[sizeof(PIDFILE) + IF_NAMESIZE];
|
2014-02-12 08:39:46 +08:00
|
|
|
struct if_options *ifo;
|
2013-02-19 21:37:42 +08:00
|
|
|
struct interface *ifp;
|
2012-10-12 19:26:20 +08:00
|
|
|
uint16_t family = 0;
|
2014-02-22 00:10:55 +08:00
|
|
|
int opt, oi = 0, i;
|
2008-09-05 22:16:53 +08:00
|
|
|
size_t len;
|
2014-02-22 00:10:55 +08:00
|
|
|
#if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK)
|
2008-09-02 21:28:11 +08:00
|
|
|
pid_t pid;
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
|
|
|
#ifdef USE_SIGNALS
|
|
|
|
int sig;
|
|
|
|
#endif
|
2008-09-02 21:28:11 +08:00
|
|
|
struct timespec ts;
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
2014-02-22 00:10:55 +08:00
|
|
|
#ifdef USE_SIGNALS
|
|
|
|
dhcpcd_ctx = &ctx;
|
|
|
|
sig = 0;
|
|
|
|
#endif
|
2008-09-02 21:28:11 +08:00
|
|
|
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();
|
2014-02-04 22:39:26 +08:00
|
|
|
return EXIT_SUCCESS;
|
2008-09-02 21:28:11 +08:00
|
|
|
} else if (strcmp(argv[1], "--version") == 0) {
|
2014-01-24 19:00:38 +08:00
|
|
|
printf(""PACKAGE" "VERSION"\n%s\n", dhcpcd_copyright);
|
2014-02-04 22:39:26 +08:00
|
|
|
return EXIT_SUCCESS;
|
2008-04-19 07:12:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ifo = NULL;
|
|
|
|
ctx.cffile = CONFIG;
|
2014-02-12 17:25:58 +08:00
|
|
|
ctx.pid_fd = ctx.control_fd = ctx.link_fd = -1;
|
|
|
|
#ifdef PLUGIN_DEV
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.dev_fd = -1;
|
2014-02-12 17:25:58 +08:00
|
|
|
#endif
|
|
|
|
#ifdef INET
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.udp_fd = -1;
|
2014-02-12 17:25:58 +08:00
|
|
|
#endif
|
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) {
|
2012-10-12 18:31:51 +08:00
|
|
|
case '4':
|
|
|
|
family = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
family = AF_INET6;
|
|
|
|
break;
|
2008-03-21 00:47:51 +08:00
|
|
|
case 'f':
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.cffile = optarg;
|
2008-03-21 00:47:51 +08:00
|
|
|
break;
|
2014-02-22 00:10:55 +08:00
|
|
|
#ifdef USE_SIGNALS
|
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;
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
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':
|
2013-12-04 07:10:21 +08:00
|
|
|
i = 3;
|
|
|
|
break;
|
2008-09-02 21:28:11 +08:00
|
|
|
case '?':
|
|
|
|
usage();
|
2014-02-04 22:39:26 +08:00
|
|
|
goto 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
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.argv = argv;
|
|
|
|
ctx.argc = argc;
|
|
|
|
ifo = read_config(&ctx, NULL, NULL, NULL);
|
|
|
|
opt = add_options(&ctx, NULL, ifo, argc, argv);
|
2008-09-02 21:28:11 +08:00
|
|
|
if (opt != 1) {
|
|
|
|
if (opt == 0)
|
|
|
|
usage();
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2013-12-04 07:10:21 +08:00
|
|
|
if (i == 3) {
|
|
|
|
printf("Interface options:\n");
|
|
|
|
if_printoptions();
|
|
|
|
#ifdef INET
|
|
|
|
if (family == 0 || family == AF_INET) {
|
|
|
|
printf("\nDHCPv4 options:\n");
|
2014-02-12 08:39:46 +08:00
|
|
|
dhcp_printoptions(&ctx);
|
2013-12-04 07:10:21 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
if (family == 0 || family == AF_INET6) {
|
|
|
|
printf("\nDHCPv6 options:\n");
|
2014-02-12 08:39:46 +08:00
|
|
|
dhcp6_printoptions(&ctx);
|
2013-12-04 07:10:21 +08:00
|
|
|
}
|
|
|
|
#endif
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_success;
|
2013-12-04 07:10:21 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.options = ifo->options;
|
2009-07-12 02:54:43 +08:00
|
|
|
if (i != 0) {
|
2010-08-24 17:16:07 +08:00
|
|
|
if (i == 1)
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.options |= DHCPCD_TEST;
|
2010-08-24 17:16:07 +08:00
|
|
|
else
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.options |= DHCPCD_DUMPLEASE;
|
|
|
|
ctx.options |= DHCPCD_PERSISTENT;
|
|
|
|
ctx.options &= ~DHCPCD_DAEMONISE;
|
2009-07-12 02:54:43 +08:00
|
|
|
}
|
2013-04-01 20:15:47 +08:00
|
|
|
|
2008-09-11 16:28:39 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.options &= ~DHCPCD_DAEMONISE;
|
2008-09-11 16:28:39 +08:00
|
|
|
#endif
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.options & DHCPCD_DEBUG)
|
2009-02-24 07:52:21 +08:00
|
|
|
setlogmask(LOG_UPTO(LOG_DEBUG));
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.options & DHCPCD_QUIET) {
|
2013-05-24 02:58:28 +08:00
|
|
|
i = open(_PATH_DEVNULL, O_RDWR);
|
|
|
|
if (i == -1)
|
|
|
|
syslog(LOG_ERR, "%s: open: %m", __func__);
|
|
|
|
else {
|
|
|
|
dup2(i, STDERR_FILENO);
|
|
|
|
close(i);
|
|
|
|
}
|
|
|
|
}
|
2008-09-11 16:28:39 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ctx.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. */
|
2014-02-12 19:55:43 +08:00
|
|
|
if (optind == argc - 1) {
|
|
|
|
if (strlen(argv[optind]) > IF_NAMESIZE) {
|
|
|
|
syslog(LOG_ERR, "%s: interface name too long",
|
|
|
|
argv[optind]);
|
|
|
|
goto exit_failure;
|
|
|
|
}
|
|
|
|
snprintf(pidfile, sizeof(pidfile),
|
|
|
|
PIDFILE, "-", argv[optind]);
|
2013-02-16 21:21:35 +08:00
|
|
|
}
|
2009-07-12 02:54:43 +08:00
|
|
|
else {
|
2014-02-12 19:55:43 +08:00
|
|
|
snprintf(pidfile, sizeof(pidfile), PIDFILE, "", "");
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.options |= DHCPCD_MASTER;
|
2009-07-12 02:54:43 +08:00
|
|
|
}
|
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-08-15 00:16:06 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.options & DHCPCD_DUMPLEASE) {
|
2010-08-24 17:16:07 +08:00
|
|
|
if (optind != argc - 1) {
|
|
|
|
syslog(LOG_ERR, "dumplease requires an interface");
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2010-08-24 17:16:07 +08:00
|
|
|
}
|
2013-02-04 06:55:45 +08:00
|
|
|
if (dhcp_dump(argv[optind]) == -1)
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
|
|
|
goto exit_success;
|
2010-08-24 17:16:07 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
#ifdef USE_SIGNALS
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ctx.options & (DHCPCD_MASTER | DHCPCD_TEST))) {
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
|
|
|
if (ctx.options & DHCPCD_MASTER)
|
|
|
|
i = -1;
|
|
|
|
else
|
|
|
|
i = control_open(&ctx, argv[optind]);
|
|
|
|
if (i == -1)
|
|
|
|
i = control_open(&ctx, NULL);
|
|
|
|
if (i != -1) {
|
2009-02-12 01:56:22 +08:00
|
|
|
syslog(LOG_INFO,
|
|
|
|
"sending commands to master dhcpcd process");
|
2014-02-12 08:39:46 +08:00
|
|
|
len = control_send(&ctx, argc, argv);
|
2014-02-22 00:10:55 +08:00
|
|
|
control_close(&ctx);
|
2014-02-04 22:39:26 +08:00
|
|
|
if (len > 0) {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_DEBUG, "send OK");
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_success;
|
2008-09-04 00:49:28 +08:00
|
|
|
} else {
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "failed to send commands");
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
2008-09-05 21:28:44 +08:00
|
|
|
} else {
|
2008-09-05 23:46:02 +08:00
|
|
|
if (errno != ENOENT)
|
2012-11-13 18:17:19 +08:00
|
|
|
syslog(LOG_ERR, "control_open: %m");
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
2014-02-22 00:10:55 +08:00
|
|
|
#ifdef USE_SIGNALS
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
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
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
ctx.eloop = eloop_init();
|
|
|
|
if (ctx.eloop == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", __func__);
|
|
|
|
goto exit_failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_SIGNALS
|
2008-09-02 21:28:11 +08:00
|
|
|
if (sig != 0) {
|
2014-02-04 22:39:26 +08:00
|
|
|
pid = read_pid(pidfile);
|
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) {
|
2012-07-06 00:37:41 +08:00
|
|
|
if (sig != SIGALRM && errno != EPERM)
|
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");
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2009-10-14 23:16:07 +08:00
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
unlink(pidfile);
|
2009-05-09 22:15:31 +08:00
|
|
|
if (sig != SIGALRM)
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2009-05-09 22:15:31 +08:00
|
|
|
} else {
|
2011-12-18 20:11:08 +08:00
|
|
|
if (sig == SIGALRM || sig == SIGUSR1)
|
2014-02-04 22:39:26 +08:00
|
|
|
goto 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);
|
2014-02-04 22:39:26 +08:00
|
|
|
if (read_pid(pidfile) == 0)
|
|
|
|
goto exit_success;
|
2009-05-09 22:15:31 +08:00
|
|
|
}
|
|
|
|
syslog(LOG_ERR, "pid %d failed to exit", pid);
|
2014-02-04 22:39:26 +08:00
|
|
|
goto 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
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ctx.options & DHCPCD_TEST)) {
|
2014-02-04 22:39:26 +08:00
|
|
|
if ((pid = read_pid(pidfile)) > 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);
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2007-07-12 20:52:27 +08:00
|
|
|
}
|
|
|
|
|
2009-07-09 03:22:31 +08:00
|
|
|
/* Ensure we have the needed directories */
|
2012-01-30 01:28:05 +08:00
|
|
|
if (mkdir(RUNDIR, 0755) == -1 && errno != EEXIST)
|
2009-07-09 03:22:31 +08:00
|
|
|
syslog(LOG_ERR, "mkdir `%s': %m", RUNDIR);
|
2012-01-30 01:28:05 +08:00
|
|
|
if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST)
|
2009-07-09 03:22:31 +08:00
|
|
|
syslog(LOG_ERR, "mkdir `%s': %m", DBDIR);
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.pid_fd = open(pidfile,
|
2014-02-08 18:36:38 +08:00
|
|
|
O_WRONLY | O_CREAT | O_CLOEXEC | O_NONBLOCK,
|
2014-02-08 01:32:08 +08:00
|
|
|
0664);
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.pid_fd == -1)
|
2008-09-06 02:24:34 +08:00
|
|
|
syslog(LOG_ERR, "open `%s': %m", pidfile);
|
2012-01-30 01:28:05 +08:00
|
|
|
else {
|
|
|
|
/* Lock the file so that only one instance of dhcpcd
|
|
|
|
* runs on an interface */
|
2014-02-12 08:39:46 +08:00
|
|
|
if (flock(ctx.pid_fd, LOCK_EX | LOCK_NB) == -1) {
|
2012-01-30 01:28:05 +08:00
|
|
|
syslog(LOG_ERR, "flock `%s': %m", pidfile);
|
2014-02-12 08:39:46 +08:00
|
|
|
close(ctx.pid_fd);
|
|
|
|
ctx.pid_fd = -1;
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2012-01-30 01:28:05 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
write_pid(ctx.pid_fd, getpid());
|
2007-07-12 20:52:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
|
|
|
|
if (ctx.options & DHCPCD_MASTER) {
|
|
|
|
if (control_start(&ctx, NULL) == -1)
|
|
|
|
syslog(LOG_ERR, "control_start: %m");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (control_start(&ctx,
|
|
|
|
ctx.options & DHCPCD_MASTER ? NULL : argv[optind]) == -1)
|
|
|
|
{
|
|
|
|
syslog(LOG_ERR, "control_start: %m");
|
2014-02-12 08:39:46 +08:00
|
|
|
goto exit_failure;
|
|
|
|
}
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
2008-07-01 00:02:56 +08:00
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
syslog(LOG_INFO, "version " VERSION " starting");
|
|
|
|
ctx.options |= DHCPCD_STARTED;
|
|
|
|
#ifdef USE_SIGNALS
|
2013-02-19 04:56:55 +08:00
|
|
|
/* Save signal mask, block and redirect signals to our handler */
|
2014-02-12 08:39:46 +08:00
|
|
|
if (signal_init(handle_signal, &ctx.sigset) == -1) {
|
2012-11-11 00:38:52 +08:00
|
|
|
syslog(LOG_ERR, "signal_setup: %m");
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2012-11-11 00:38:52 +08:00
|
|
|
}
|
2011-12-15 10:35:47 +08:00
|
|
|
#endif
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.ifc = argc - optind;
|
|
|
|
ctx.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 */
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.ifc == 1 && !(ctx.options & DHCPCD_BACKGROUND))
|
|
|
|
ctx.options |= DHCPCD_WAITIP;
|
2009-10-25 18:56:54 +08:00
|
|
|
|
2013-05-18 07:09:36 +08:00
|
|
|
/* RTM_NEWADDR goes through the link socket as well which we
|
|
|
|
* need for IPv6 DAD, so we check for DHCPCD_LINK in handle_carrier
|
|
|
|
* instead.
|
|
|
|
* We also need to open this before checking for interfaces below
|
|
|
|
* so that we pickup any new addresses during the discover phase. */
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.link_fd = open_link_socket();
|
|
|
|
if (ctx.link_fd == -1)
|
|
|
|
syslog(LOG_ERR, "open_link_socket: %m");
|
|
|
|
else
|
|
|
|
eloop_event_add(ctx.eloop, ctx.link_fd, handle_link, &ctx);
|
2013-05-18 07:09:36 +08:00
|
|
|
|
2013-09-13 05:35:33 +08:00
|
|
|
/* Start any dev listening plugin which may want to
|
|
|
|
* change the interface name provided by the kernel */
|
2014-02-12 08:39:46 +08:00
|
|
|
if ((ctx.options & (DHCPCD_MASTER | DHCPCD_DEV)) ==
|
2013-09-13 05:35:33 +08:00
|
|
|
(DHCPCD_MASTER | DHCPCD_DEV))
|
2014-02-12 08:39:46 +08:00
|
|
|
dev_start(&ctx);
|
2013-09-13 05:35:33 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.ifaces = discover_interfaces(&ctx, ctx.ifc, ctx.ifv);
|
|
|
|
for (i = 0; i < ctx.ifc; i++) {
|
|
|
|
if (find_interface(&ctx, ctx.ifv[i]) == NULL)
|
2008-11-15 19:24:26 +08:00
|
|
|
syslog(LOG_ERR, "%s: interface not found or invalid",
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.ifv[i]);
|
2008-11-15 04:38:40 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.ifaces == NULL || TAILQ_FIRST(ctx.ifaces) == NULL) {
|
|
|
|
if (ctx.ifc == 0)
|
2008-11-15 04:38:40 +08:00
|
|
|
syslog(LOG_ERR, "no valid interfaces found");
|
2009-10-10 03:24:22 +08:00
|
|
|
else
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ctx.options & DHCPCD_LINK)) {
|
2009-10-14 23:16:07 +08:00
|
|
|
syslog(LOG_ERR,
|
|
|
|
"aborting as link detection is disabled");
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2008-11-15 19:24:26 +08:00
|
|
|
}
|
2008-09-04 19:30:11 +08:00
|
|
|
}
|
2008-11-15 04:38:40 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.options & DHCPCD_BACKGROUND && daemonise(&ctx))
|
2014-02-08 18:36:38 +08:00
|
|
|
goto exit_success;
|
2009-10-14 23:16:07 +08:00
|
|
|
|
|
|
|
opt = 0;
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx.ifaces, next) {
|
2013-02-19 21:37:42 +08:00
|
|
|
init_state(ifp, argc, argv);
|
|
|
|
if (ifp->carrier != LINK_DOWN)
|
2009-10-14 23:16:07 +08:00
|
|
|
opt = 1;
|
|
|
|
}
|
2009-11-20 23:14:27 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ctx.options & DHCPCD_BACKGROUND)) {
|
2009-11-20 23:14:27 +08:00
|
|
|
/* If we don't have a carrier, we may have to wait for a second
|
2012-01-30 01:28:05 +08:00
|
|
|
* before one becomes available if we brought an interface up */
|
2009-11-20 23:14:27 +08:00
|
|
|
if (opt == 0 &&
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.options & DHCPCD_LINK &&
|
|
|
|
ctx.options & DHCPCD_WAITUP &&
|
|
|
|
!(ctx.options & DHCPCD_WAITIP))
|
2009-11-20 23:14:27 +08:00
|
|
|
{
|
|
|
|
ts.tv_sec = 1;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
nanosleep(&ts, NULL);
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx.ifaces, next) {
|
|
|
|
handle_carrier(&ctx, LINK_UNKNOWN, 0,
|
|
|
|
ifp->name);
|
2013-02-19 21:37:42 +08:00
|
|
|
if (ifp->carrier != LINK_DOWN) {
|
2009-11-20 23:14:27 +08:00
|
|
|
opt = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.options & DHCPCD_MASTER)
|
|
|
|
i = ifo->timeout;
|
|
|
|
else if ((ifp = TAILQ_FIRST(ctx.ifaces)))
|
2013-02-19 21:37:42 +08:00
|
|
|
i = ifp->options->timeout;
|
2011-03-29 18:36:17 +08:00
|
|
|
else
|
2013-02-03 18:35:59 +08:00
|
|
|
i = 0;
|
2009-11-20 23:14:27 +08:00
|
|
|
if (opt == 0 &&
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.options & DHCPCD_LINK &&
|
|
|
|
!(ctx.options & DHCPCD_WAITIP))
|
2009-11-20 23:14:27 +08:00
|
|
|
{
|
|
|
|
syslog(LOG_WARNING, "no interfaces have a carrier");
|
2014-02-12 08:39:46 +08:00
|
|
|
if (daemonise(&ctx))
|
2014-02-07 17:33:32 +08:00
|
|
|
goto exit_success;
|
2011-03-29 18:36:17 +08:00
|
|
|
} else if (i > 0) {
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.options & DHCPCD_IPV4LL)
|
|
|
|
ctx.options |= DHCPCD_TIMEOUT_IPV4LL;
|
|
|
|
eloop_timeout_add_sec(ctx.eloop, i,
|
|
|
|
handle_exit_timeout, &ctx);
|
2009-11-20 23:14:27 +08:00
|
|
|
}
|
2009-10-14 23:16:07 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
free_options(ifo);
|
|
|
|
ifo = NULL;
|
2009-10-14 23:16:07 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
sort_interfaces(&ctx);
|
|
|
|
TAILQ_FOREACH(ifp, ctx.ifaces, next) {
|
|
|
|
eloop_timeout_add_sec(ctx.eloop, 0, start_interface, ifp);
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
2009-02-20 05:15:25 +08:00
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
i = eloop_start(&ctx);
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit1;
|
|
|
|
|
|
|
|
exit_success:
|
|
|
|
i = EXIT_SUCCESS;
|
|
|
|
goto exit1;
|
|
|
|
|
|
|
|
exit_failure:
|
|
|
|
i = EXIT_FAILURE;
|
|
|
|
|
|
|
|
exit1:
|
2014-02-12 08:39:46 +08:00
|
|
|
/* Free memory and close fd's */
|
|
|
|
if (ctx.ifaces) {
|
|
|
|
while ((ifp = TAILQ_FIRST(ctx.ifaces))) {
|
|
|
|
TAILQ_REMOVE(ctx.ifaces, ifp, next);
|
2014-02-04 22:39:26 +08:00
|
|
|
free_interface(ifp);
|
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
free(ctx.ifaces);
|
|
|
|
}
|
|
|
|
free(ctx.duid);
|
2014-02-12 18:29:06 +08:00
|
|
|
if (ctx.link_fd != -1) {
|
|
|
|
eloop_event_delete(ctx.eloop, ctx.link_fd);
|
2014-02-12 08:39:46 +08:00
|
|
|
close(ctx.link_fd);
|
2014-02-12 18:29:06 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
|
|
|
|
free_options(ifo);
|
|
|
|
free_globals(&ctx);
|
|
|
|
restore_kernel_ra(&ctx);
|
|
|
|
ipv4_ctxfree(&ctx);
|
|
|
|
ipv6_ctxfree(&ctx);
|
|
|
|
dev_stop(&ctx, !(ctx.options & DHCPCD_FORKED));
|
2014-02-22 00:10:55 +08:00
|
|
|
if (!(ctx.options & DHCPCD_FORKED) && control_stop(&ctx) == -1)
|
|
|
|
syslog(LOG_ERR, "control_stop: %m:");
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.pid_fd != -1) {
|
|
|
|
close(ctx.pid_fd);
|
2014-02-04 22:39:26 +08:00
|
|
|
unlink(pidfile);
|
2014-02-08 18:36:38 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
eloop_free(ctx.eloop);
|
2014-02-04 22:39:26 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.options & DHCPCD_STARTED && !(ctx.options & DHCPCD_FORKED))
|
2014-02-04 22:39:26 +08:00
|
|
|
syslog(LOG_INFO, "exited");
|
2014-02-13 04:51:57 +08:00
|
|
|
closelog();
|
2014-02-04 22:39:26 +08:00
|
|
|
return i;
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|