2013-04-05 04:31:04 +08:00
|
|
|
/*
|
2007-11-08 00:26:41 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2016-01-05 01:15:15 +08:00
|
|
|
* Copyright (c) 2006-2016 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
|
|
|
*/
|
|
|
|
|
2016-01-05 01:15:15 +08:00
|
|
|
const char dhcpcd_copyright[] = "Copyright (c) 2006-2016 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
|
|
|
|
2008-07-03 01:05:41 +08:00
|
|
|
#include <ctype.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <errno.h>
|
2014-04-18 21:17:42 +08:00
|
|
|
#include <fcntl.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#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>
|
|
|
|
#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"
|
2014-04-25 18:42:37 +08:00
|
|
|
#include "if.h"
|
2008-09-02 21:28:11 +08:00
|
|
|
#include "if-options.h"
|
2013-02-02 22:05:55 +08:00
|
|
|
#include "ipv4.h"
|
2015-06-13 03:21:32 +08:00
|
|
|
#include "ipv4ll.h"
|
2012-07-06 00:37:41 +08:00
|
|
|
#include "ipv6.h"
|
2013-08-20 18:29:43 +08:00
|
|
|
#include "ipv6nd.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
|
2015-05-09 18:09:34 +08:00
|
|
|
const int dhcpcd_signals[] = {
|
2015-03-04 23:47:04 +08:00
|
|
|
SIGTERM,
|
|
|
|
SIGINT,
|
2013-06-07 20:44:19 +08:00
|
|
|
SIGALRM,
|
|
|
|
SIGHUP,
|
|
|
|
SIGUSR1,
|
2015-03-18 07:46:38 +08:00
|
|
|
SIGUSR2,
|
2015-05-13 07:47:25 +08:00
|
|
|
SIGPIPE
|
2013-06-07 20:44:19 +08:00
|
|
|
};
|
2015-05-13 07:47:25 +08:00
|
|
|
const size_t dhcpcd_signals_len = __arraycount(dhcpcd_signals);
|
2015-05-09 18:09:34 +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-09-18 23:12:45 +08:00
|
|
|
static int
|
2014-02-22 00:10:55 +08:00
|
|
|
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
|
|
|
|
2016-01-08 03:52:54 +08:00
|
|
|
printf("usage: "PACKAGE"\t[-46ABbDdEGgHJKkLnPpqTVw]\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) {
|
2014-07-03 18:07:35 +08:00
|
|
|
for (; ctx->ifac > 0; ctx->ifac--)
|
|
|
|
free(ctx->ifav[ctx->ifac - 1]);
|
2014-02-12 08:39:46 +08:00
|
|
|
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) {
|
2014-07-03 18:07:35 +08:00
|
|
|
for (; ctx->ifdc > 0; ctx->ifdc--)
|
|
|
|
free(ctx->ifdv[ctx->ifdc - 1]);
|
2014-02-12 08:39:46 +08:00
|
|
|
free(ctx->ifdv);
|
|
|
|
ctx->ifdv = NULL;
|
2014-02-08 18:36:38 +08:00
|
|
|
}
|
2014-11-18 20:06:08 +08:00
|
|
|
if (ctx->ifcc) {
|
|
|
|
for (; ctx->ifcc > 0; ctx->ifcc--)
|
|
|
|
free(ctx->ifcv[ctx->ifcc - 1]);
|
|
|
|
free(ctx->ifcv);
|
|
|
|
ctx->ifcv = NULL;
|
|
|
|
}
|
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
|
2015-05-15 03:46:21 +08:00
|
|
|
if (ctx->nd_opts) {
|
|
|
|
for (opt = ctx->nd_opts;
|
|
|
|
ctx->nd_opts_len > 0;
|
|
|
|
opt++, ctx->nd_opts_len--)
|
|
|
|
free_dhcp_opt_embenc(opt);
|
|
|
|
free(ctx->nd_opts);
|
|
|
|
ctx->nd_opts = NULL;
|
|
|
|
}
|
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
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx = arg;
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR, "timed out");
|
2014-10-24 03:50:37 +08:00
|
|
|
if (!(ctx->options & DHCPCD_MASTER)) {
|
2014-10-24 03:07:35 +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-10-24 03:07:35 +08:00
|
|
|
ctx->options |= DHCPCD_NOWAITIP;
|
|
|
|
dhcpcd_daemonise(ctx);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
|
|
|
|
2015-06-10 02:50:17 +08:00
|
|
|
static const char *
|
|
|
|
dhcpcd_af(int af)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (af) {
|
|
|
|
case AF_UNSPEC:
|
|
|
|
return "IP";
|
|
|
|
case AF_INET:
|
|
|
|
return "IPv4";
|
|
|
|
case AF_INET6:
|
|
|
|
return "IPv6";
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 03:47:53 +08:00
|
|
|
int
|
|
|
|
dhcpcd_ifafwaiting(const struct interface *ifp)
|
2014-09-08 03:03:41 +08:00
|
|
|
{
|
2015-06-10 03:47:53 +08:00
|
|
|
unsigned long long opts;
|
2014-09-08 03:03:41 +08:00
|
|
|
|
2016-02-04 18:43:16 +08:00
|
|
|
if (ifp->active != IF_ACTIVE_USER)
|
2015-12-17 10:34:36 +08:00
|
|
|
return AF_MAX;
|
|
|
|
|
2015-06-10 03:47:53 +08:00
|
|
|
opts = ifp->options->options;
|
2015-05-20 18:25:10 +08:00
|
|
|
if (opts & DHCPCD_WAITIP4 && !ipv4_hasaddr(ifp))
|
2015-06-10 02:50:17 +08:00
|
|
|
return AF_INET;
|
2015-05-20 18:25:10 +08:00
|
|
|
if (opts & DHCPCD_WAITIP6 && !ipv6_hasaddr(ifp))
|
2015-06-10 02:50:17 +08:00
|
|
|
return AF_INET6;
|
2015-05-14 17:17:37 +08:00
|
|
|
if (opts & DHCPCD_WAITIP &&
|
|
|
|
!(opts & (DHCPCD_WAITIP4 | DHCPCD_WAITIP6)) &&
|
2015-06-10 02:50:17 +08:00
|
|
|
!ipv4_hasaddr(ifp) && !ipv6_hasaddr(ifp))
|
|
|
|
return AF_UNSPEC;
|
|
|
|
return AF_MAX;
|
2014-09-08 03:03:41 +08:00
|
|
|
}
|
|
|
|
|
2015-06-10 03:47:53 +08:00
|
|
|
int
|
|
|
|
dhcpcd_afwaiting(const struct dhcpcd_ctx *ctx)
|
2015-05-14 17:17:37 +08:00
|
|
|
{
|
2015-06-10 02:50:17 +08:00
|
|
|
unsigned long long opts;
|
2015-06-10 03:47:53 +08:00
|
|
|
const struct interface *ifp;
|
|
|
|
int af;
|
2015-05-14 17:17:37 +08:00
|
|
|
|
|
|
|
if (!(ctx->options & DHCPCD_WAITOPTS))
|
2015-06-10 03:47:53 +08:00
|
|
|
return AF_MAX;
|
2015-05-14 17:17:37 +08:00
|
|
|
|
2015-06-10 02:50:17 +08:00
|
|
|
opts = ctx->options;
|
2015-05-14 17:17:37 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
2015-06-10 02:50:17 +08:00
|
|
|
if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP4) &&
|
|
|
|
ipv4_hasaddr(ifp))
|
|
|
|
opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP4);
|
|
|
|
if (opts & (DHCPCD_WAITIP | DHCPCD_WAITIP6) &&
|
|
|
|
ipv6_hasaddr(ifp))
|
|
|
|
opts &= ~(DHCPCD_WAITIP | DHCPCD_WAITIP6);
|
|
|
|
if (!(opts & DHCPCD_WAITOPTS))
|
|
|
|
break;
|
2015-05-14 17:17:37 +08:00
|
|
|
}
|
2015-06-10 02:50:17 +08:00
|
|
|
if (opts & DHCPCD_WAITIP)
|
|
|
|
af = AF_UNSPEC;
|
|
|
|
else if (opts & DHCPCD_WAITIP4)
|
|
|
|
af = AF_INET;
|
|
|
|
else if (opts & DHCPCD_WAITIP6)
|
|
|
|
af = AF_INET6;
|
|
|
|
else
|
2015-06-10 03:47:53 +08:00
|
|
|
return AF_MAX;
|
|
|
|
return af;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dhcpcd_ipwaited(struct dhcpcd_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct interface *ifp;
|
|
|
|
int af;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
|
|
|
if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
|
|
|
|
logger(ctx, LOG_DEBUG,
|
|
|
|
"%s: waiting for an %s address",
|
|
|
|
ifp->name, dhcpcd_af(af));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((af = dhcpcd_afwaiting(ctx)) != AF_MAX) {
|
|
|
|
logger(ctx, LOG_DEBUG,
|
|
|
|
"waiting for an %s address",
|
|
|
|
dhcpcd_af(af));
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-14 17:17:37 +08:00
|
|
|
|
2015-06-10 03:47:53 +08:00
|
|
|
return 1;
|
2015-05-14 17:17:37 +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-04-28 20:02:12 +08:00
|
|
|
dhcpcd_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 &&
|
2014-10-24 03:07:35 +08:00
|
|
|
!(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_NOWAITIP)))
|
2014-02-12 08:39:46 +08:00
|
|
|
{
|
2014-09-08 03:03:41 +08:00
|
|
|
if (!dhcpcd_ipwaited(ctx))
|
2014-02-04 23:53:56 +08:00
|
|
|
return 0;
|
2013-09-01 00:48:39 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 01:05:16 +08:00
|
|
|
if (ctx->options & DHCPCD_ONESHOT) {
|
|
|
|
logger(ctx, LOG_INFO, "exiting due to oneshot");
|
|
|
|
eloop_exit(ctx->eloop, EXIT_SUCCESS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2016-01-06 18:54:36 +08:00
|
|
|
logger(ctx, LOG_DEBUG, "forking to background");
|
|
|
|
|
2013-02-02 22:05:55 +08:00
|
|
|
/* Setup a signal pipe so parent knows when to exit. */
|
|
|
|
if (pipe(sidpipe) == -1) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR, "pipe: %m");
|
2014-02-04 23:53:56 +08:00
|
|
|
return 0;
|
2013-02-02 22:05:55 +08:00
|
|
|
}
|
2016-01-06 18:54:36 +08:00
|
|
|
|
|
|
|
/* Store the pid and routing message seq number so we can identify
|
|
|
|
* the last message successfully sent to the kernel.
|
|
|
|
* This allows us to ignore all messages we sent after forking
|
|
|
|
* and detaching. */
|
|
|
|
ctx->ppid = getpid();
|
|
|
|
ctx->pseq = ctx->sseq;
|
|
|
|
|
2013-02-02 22:05:55 +08:00
|
|
|
switch (pid = fork()) {
|
|
|
|
case -1:
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, 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();
|
2016-01-06 18:54:36 +08:00
|
|
|
/* Notify parent it's safe to exit as we've detached. */
|
|
|
|
close(sidpipe[0]);
|
|
|
|
if (write(sidpipe[1], &buf, 1) == -1)
|
|
|
|
logger(ctx, LOG_ERR, "failed to notify parent: %m");
|
|
|
|
close(sidpipe[1]);
|
2015-03-05 03:29:17 +08:00
|
|
|
/* Some polling methods don't survive after forking,
|
|
|
|
* so ensure we can requeue all our events. */
|
|
|
|
if (eloop_requeue(ctx->eloop) == -1) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR, "eloop_requeue: %m");
|
2015-03-05 03:29:17 +08:00
|
|
|
eloop_exit(ctx->eloop, EXIT_FAILURE);
|
|
|
|
}
|
2013-02-02 22:05:55 +08:00
|
|
|
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)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR, "failed to read child: %m");
|
2013-02-02 22:05:55 +08:00
|
|
|
close(sidpipe[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Done with the fd now */
|
|
|
|
if (pid != 0) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, 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
|
|
|
}
|
|
|
|
|
2015-04-24 17:18:42 +08:00
|
|
|
static void
|
|
|
|
dhcpcd_drop(struct interface *ifp, int stop)
|
|
|
|
{
|
|
|
|
|
|
|
|
dhcp6_drop(ifp, stop ? NULL : "EXPIRE6");
|
|
|
|
ipv6nd_drop(ifp);
|
|
|
|
ipv6_drop(ifp);
|
2015-06-13 03:21:32 +08:00
|
|
|
ipv4ll_drop(ifp);
|
2015-04-24 17:18:42 +08:00
|
|
|
dhcp_drop(ifp, stop ? "STOP" : "EXPIRE");
|
|
|
|
arp_close(ifp);
|
|
|
|
}
|
|
|
|
|
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;
|
2015-12-17 10:34:36 +08:00
|
|
|
if (!ifp->active)
|
|
|
|
goto stop;
|
|
|
|
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, 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
|
|
|
|
2015-04-24 17:18:42 +08:00
|
|
|
dhcpcd_drop(ifp, 1);
|
2013-06-13 02:24:16 +08:00
|
|
|
if (ifp->options->options & DHCPCD_DEPARTED)
|
|
|
|
script_runreason(ifp, "DEPARTED");
|
2014-05-06 22:42:24 +08:00
|
|
|
else
|
|
|
|
script_runreason(ifp, "STOPPED");
|
2014-05-16 23:03:28 +08:00
|
|
|
|
2014-11-11 03:23:22 +08:00
|
|
|
/* Delete all timeouts for the interfaces */
|
|
|
|
eloop_q_timeout_delete(ctx->eloop, 0, NULL, ifp);
|
|
|
|
|
2015-12-17 20:37:07 +08:00
|
|
|
/* De-activate the interface */
|
2016-02-04 18:43:16 +08:00
|
|
|
ifp->active = IF_INACTIVE;
|
2014-05-16 23:03:28 +08:00
|
|
|
|
2015-12-17 20:37:07 +08:00
|
|
|
stop:
|
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
|
2015-05-01 20:39:06 +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;
|
2014-07-02 19:42:08 +08:00
|
|
|
#ifdef INET6
|
2014-07-02 04:34:19 +08:00
|
|
|
size_t i;
|
2014-07-02 19:42:08 +08:00
|
|
|
#endif
|
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))
|
2015-09-03 23:06:43 +08:00
|
|
|
ifo->options &= ~DHCPCD_IPV4LL;
|
2014-11-18 19:47:45 +08:00
|
|
|
if (ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK) ||
|
|
|
|
!(ifp->flags & IFF_MULTICAST))
|
2011-12-15 11:37:27 +08:00
|
|
|
ifo->options &= ~DHCPCD_IPV6RS;
|
2013-04-01 20:15:47 +08:00
|
|
|
|
2009-01-02 04:51:04 +08:00
|
|
|
if (ifo->metric != -1)
|
2014-03-28 06:14:52 +08:00
|
|
|
ifp->metric = (unsigned int)ifo->metric;
|
2009-01-02 04:51:04 +08:00
|
|
|
|
2014-10-22 16:54:09 +08:00
|
|
|
if (!(ifo->options & DHCPCD_IPV4))
|
2015-05-31 16:19:34 +08:00
|
|
|
ifo->options &= ~(DHCPCD_DHCP | DHCPCD_IPV4LL | DHCPCD_WAITIP4);
|
2014-10-22 16:54:09 +08:00
|
|
|
|
2013-11-15 21:43:41 +08:00
|
|
|
if (!(ifo->options & DHCPCD_IPV6))
|
2015-05-31 16:19:34 +08:00
|
|
|
ifo->options &=
|
|
|
|
~(DHCPCD_IPV6RS | DHCPCD_DHCP6 | DHCPCD_WAITIP6);
|
2013-11-15 21:43:41 +08:00
|
|
|
|
2014-12-18 18:09:54 +08:00
|
|
|
if (ifo->options & DHCPCD_SLAACPRIVATE &&
|
2015-04-02 05:29:13 +08:00
|
|
|
!(ifp->ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST)))
|
2014-06-05 19:38:04 +08:00
|
|
|
ifo->options |= DHCPCD_IPV6RA_OWN;
|
|
|
|
|
2012-07-13 16:22:04 +08:00
|
|
|
/* We want to disable kernel interface RA as early as possible. */
|
2015-09-13 04:19:31 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV6 &&
|
2015-04-02 06:06:33 +08:00
|
|
|
!(ifp->ctx->options & DHCPCD_DUMPLEASE))
|
2015-04-02 06:02:34 +08:00
|
|
|
{
|
2014-10-22 16:54:09 +08:00
|
|
|
/* If not doing any DHCP, disable the RDNSS requirement. */
|
|
|
|
if (!(ifo->options & (DHCPCD_DHCP | DHCPCD_DHCP6)))
|
|
|
|
ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS;
|
2014-04-25 18:42:37 +08:00
|
|
|
ra_global = if_checkipv6(ifp->ctx, NULL,
|
2014-02-12 08:39:46 +08:00
|
|
|
ifp->ctx->options & DHCPCD_IPV6RA_OWN ? 1 : 0);
|
2014-09-18 08:35:03 +08:00
|
|
|
ra_iface = if_checkipv6(ifp->ctx, ifp,
|
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;
|
2015-04-02 05:29:13 +08:00
|
|
|
else if (ra_iface == 0 &&
|
2015-04-02 06:02:34 +08:00
|
|
|
!(ifp->ctx->options & DHCPCD_TEST))
|
2013-07-02 03:23:19 +08:00
|
|
|
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
|
2014-11-18 19:28:14 +08:00
|
|
|
* of the hardware address family and the hardware address.
|
|
|
|
* If there is no hardware address and no ClientID set,
|
|
|
|
* force a DUID based ClientID. */
|
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;
|
2014-11-18 19:28:14 +08:00
|
|
|
else if (ifp->hwlen == 0 && !(ifo->options & DHCPCD_CLIENTID))
|
|
|
|
ifo->options |= DHCPCD_CLIENTID | DHCPCD_DUID;
|
2009-01-02 04:51:04 +08:00
|
|
|
|
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.
|
|
|
|
*/
|
2014-11-18 18:51:00 +08:00
|
|
|
if (ifp->hwlen >= sizeof(ifo->iaid))
|
|
|
|
memcpy(ifo->iaid,
|
|
|
|
ifp->hwaddr + ifp->hwlen - sizeof(ifo->iaid),
|
|
|
|
sizeof(ifo->iaid));
|
|
|
|
else {
|
|
|
|
uint32_t len;
|
2014-12-02 17:25:14 +08:00
|
|
|
|
2014-11-18 18:51:00 +08:00
|
|
|
len = (uint32_t)strlen(ifp->name);
|
|
|
|
if (len <= sizeof(ifo->iaid)) {
|
|
|
|
memcpy(ifo->iaid, ifp->name, len);
|
2014-12-02 17:25:14 +08:00
|
|
|
if (len < sizeof(ifo->iaid))
|
|
|
|
memset(ifo->iaid + len, 0,
|
|
|
|
sizeof(ifo->iaid) - len);
|
2014-11-18 18:51:00 +08:00
|
|
|
} else {
|
|
|
|
/* IAID is the same size as a uint32_t */
|
|
|
|
len = htonl(ifp->index);
|
|
|
|
memcpy(ifo->iaid, &len, sizeof(len));
|
|
|
|
}
|
2013-11-15 21:43:41 +08:00
|
|
|
}
|
|
|
|
ifo->options |= DHCPCD_IAID;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef INET6
|
2015-02-20 16:28:04 +08:00
|
|
|
if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 &&
|
|
|
|
ifp->name[0] != '\0')
|
|
|
|
{
|
2014-09-27 05:50:52 +08:00
|
|
|
ifo->ia = malloc(sizeof(*ifo->ia));
|
|
|
|
if (ifo->ia == NULL)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: %m", __func__);
|
2014-09-27 05:50:52 +08:00
|
|
|
else {
|
|
|
|
ifo->ia_len = 1;
|
|
|
|
ifo->ia->ia_type = D6_OPTION_IA_NA;
|
|
|
|
memcpy(ifo->ia->iaid, ifo->iaid, sizeof(ifo->iaid));
|
|
|
|
memset(&ifo->ia->addr, 0, sizeof(ifo->ia->addr));
|
|
|
|
ifo->ia->sla = NULL;
|
|
|
|
ifo->ia->sla_len = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < ifo->ia_len; i++) {
|
2014-09-27 10:22:34 +08:00
|
|
|
if (!ifo->ia[i].iaid_set) {
|
|
|
|
memcpy(&ifo->ia[i].iaid, ifo->iaid,
|
|
|
|
sizeof(ifo->ia[i].iaid));
|
|
|
|
ifo->ia[i].iaid_set = 1;
|
|
|
|
}
|
2013-11-15 21:43:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2009-01-02 04:51:04 +08:00
|
|
|
}
|
|
|
|
|
2009-03-31 16:35:38 +08:00
|
|
|
int
|
2014-04-28 20:02:12 +08:00
|
|
|
dhcpcd_selectprofile(struct interface *ifp, const char *profile)
|
2009-03-31 16:35:38 +08:00
|
|
|
{
|
|
|
|
struct if_options *ifo;
|
2014-09-10 18:00:08 +08:00
|
|
|
char pssid[PROFILE_LEN];
|
|
|
|
|
|
|
|
if (ifp->ssid_len) {
|
|
|
|
ssize_t r;
|
2014-09-29 04:26:49 +08:00
|
|
|
|
2014-10-02 02:32:03 +08:00
|
|
|
r = print_string(pssid, sizeof(pssid), ESCSTRING,
|
2014-09-29 04:26:49 +08:00
|
|
|
ifp->ssid, ifp->ssid_len);
|
2014-09-10 18:00:08 +08:00
|
|
|
if (r == -1) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR,
|
|
|
|
"%s: %s: %m", ifp->name, __func__);
|
2014-09-10 18:00:08 +08:00
|
|
|
pssid[0] = '\0';
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
pssid[0] = '\0';
|
|
|
|
ifo = read_config(ifp->ctx, ifp->name, pssid, profile);
|
2009-03-31 16:35:38 +08:00
|
|
|
if (ifo == NULL) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, 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));
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, 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';
|
2015-05-01 17:53:45 +08:00
|
|
|
|
2015-05-01 20:39:06 +08:00
|
|
|
free_options(ifp->options);
|
|
|
|
ifp->options = ifo;
|
|
|
|
if (profile)
|
|
|
|
configure_interface1(ifp);
|
2014-02-24 22:40:29 +08:00
|
|
|
return 1;
|
2009-03-31 16:35:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-03-14 19:06:19 +08:00
|
|
|
configure_interface(struct interface *ifp, int argc, char **argv,
|
|
|
|
unsigned long long options)
|
2009-03-31 16:35:38 +08:00
|
|
|
{
|
2015-05-01 20:39:06 +08:00
|
|
|
time_t old;
|
2013-02-03 18:35:59 +08:00
|
|
|
|
2015-05-01 20:39:06 +08:00
|
|
|
old = ifp->options ? ifp->options->mtime : 0;
|
2014-04-28 20:02:12 +08:00
|
|
|
dhcpcd_selectprofile(ifp, NULL);
|
2014-02-12 08:39:46 +08:00
|
|
|
add_options(ifp->ctx, ifp->name, ifp->options, argc, argv);
|
2015-03-14 19:06:19 +08:00
|
|
|
ifp->options->options |= options;
|
2015-05-01 20:39:06 +08:00
|
|
|
configure_interface1(ifp);
|
|
|
|
|
|
|
|
/* If the mtime has changed drop any old lease */
|
|
|
|
if (ifp->options && old != 0 && ifp->options->mtime != old) {
|
|
|
|
logger(ifp->ctx, LOG_WARNING,
|
|
|
|
"%s: confile file changed, expiring leases", ifp->name);
|
|
|
|
dhcpcd_drop(ifp, 0);
|
|
|
|
}
|
2009-03-31 16:35:38 +08:00
|
|
|
}
|
|
|
|
|
2014-12-09 17:39:30 +08:00
|
|
|
static void
|
|
|
|
dhcpcd_pollup(void *arg)
|
|
|
|
{
|
|
|
|
struct interface *ifp = arg;
|
|
|
|
int carrier;
|
|
|
|
|
|
|
|
carrier = if_carrier(ifp); /* will set ifp->flags */
|
|
|
|
if (carrier == LINK_UP && !(ifp->flags & IFF_UP)) {
|
2015-03-04 01:27:25 +08:00
|
|
|
struct timespec tv;
|
2014-12-09 17:39:30 +08:00
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
2015-05-13 04:03:36 +08:00
|
|
|
tv.tv_nsec = IF_POLL_UP * NSEC_PER_MSEC;
|
2014-12-09 17:39:30 +08:00
|
|
|
eloop_timeout_add_tv(ifp->ctx->eloop, &tv, dhcpcd_pollup, ifp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dhcpcd_handlecarrier(ifp->ctx, carrier, ifp->flags, ifp->name);
|
|
|
|
}
|
|
|
|
|
2016-01-21 03:08:12 +08:00
|
|
|
static void
|
2016-02-05 05:42:14 +08:00
|
|
|
dhcpcd_initstate2(struct interface *ifp, unsigned long long options)
|
2016-01-21 03:08:12 +08:00
|
|
|
{
|
|
|
|
struct if_options *ifo;
|
|
|
|
|
2016-02-05 05:42:14 +08:00
|
|
|
if (options) {
|
|
|
|
if ((ifo = default_config(ifp->ctx)) == NULL) {
|
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: %s: %m",
|
|
|
|
ifp->name, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ifo->options |= options;
|
|
|
|
free(ifp->options);
|
|
|
|
ifp->options = ifo;
|
|
|
|
} else
|
|
|
|
ifo = ifp->options;
|
2016-01-21 03:08:12 +08:00
|
|
|
|
|
|
|
if (ifo->options & DHCPCD_IPV4 && ipv4_init(ifp->ctx) == -1) {
|
|
|
|
logger(ifp->ctx, LOG_ERR, "ipv4_init: %m");
|
|
|
|
ifo->options &= ~DHCPCD_IPV4;
|
|
|
|
}
|
|
|
|
if (ifo->options & DHCPCD_IPV6 && ipv6_init(ifp->ctx) == NULL) {
|
|
|
|
logger(ifp->ctx, LOG_ERR, "ipv6_init: %m");
|
|
|
|
ifo->options &= ~DHCPCD_IPV6RS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add our link-local address before upping the interface
|
|
|
|
* so our RFC7217 address beats the hwaddr based one.
|
|
|
|
* This needs to happen before PREINIT incase a hook script
|
|
|
|
* inadvertently ups the interface. */
|
|
|
|
if (ifo->options & DHCPCD_IPV6 && ipv6_start(ifp) == -1) {
|
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: ipv6_start: %m", ifp->name);
|
|
|
|
ifo->options &= ~DHCPCD_IPV6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 05:42:14 +08:00
|
|
|
static void
|
|
|
|
dhcpcd_initstate1(struct interface *ifp, int argc, char **argv,
|
|
|
|
unsigned long long options)
|
|
|
|
{
|
|
|
|
|
|
|
|
configure_interface(ifp, argc, argv, options);
|
|
|
|
dhcpcd_initstate2(ifp, 0);
|
|
|
|
}
|
|
|
|
|
2016-01-21 03:08:12 +08:00
|
|
|
static void
|
|
|
|
dhcpcd_initstate(struct interface *ifp, unsigned long long options)
|
|
|
|
{
|
|
|
|
|
|
|
|
dhcpcd_initstate1(ifp, ifp->ctx->argc, ifp->ctx->argv, options);
|
|
|
|
}
|
|
|
|
|
2011-11-23 17:03:24 +08:00
|
|
|
void
|
2014-04-28 20:02:12 +08:00
|
|
|
dhcpcd_handlecarrier(struct dhcpcd_ctx *ctx, int carrier, unsigned int flags,
|
2014-02-12 08:39:46 +08:00
|
|
|
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
|
|
|
|
2015-04-30 18:16:06 +08:00
|
|
|
ifp = if_find(ctx->ifaces, ifname);
|
2015-12-17 10:34:36 +08:00
|
|
|
if (ifp == NULL ||
|
|
|
|
ifp->options == NULL || !(ifp->options->options & DHCPCD_LINK) ||
|
|
|
|
!ifp->active)
|
2008-09-02 21:28:11 +08:00
|
|
|
return;
|
2012-02-06 03:32:17 +08:00
|
|
|
|
2014-07-02 05:06:07 +08:00
|
|
|
switch(carrier) {
|
|
|
|
case LINK_UNKNOWN:
|
2014-04-25 18:42:37 +08:00
|
|
|
carrier = if_carrier(ifp); /* will set ifp->flags */
|
2014-07-02 05:06:07 +08:00
|
|
|
break;
|
|
|
|
case LINK_UP:
|
2014-12-09 17:39:30 +08:00
|
|
|
/* we have a carrier! Still need to check for IFF_UP */
|
|
|
|
if (flags & IFF_UP)
|
|
|
|
ifp->flags = flags;
|
|
|
|
else {
|
|
|
|
/* So we need to poll for IFF_UP as there is no
|
|
|
|
* kernel notification when it's set. */
|
|
|
|
dhcpcd_pollup(ifp);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-02 05:06:07 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->flags = flags;
|
2014-07-02 05:06:07 +08:00
|
|
|
}
|
2012-02-06 03:32:17 +08:00
|
|
|
|
2014-12-09 17:45:56 +08:00
|
|
|
/* If we here, we don't need to poll for IFF_UP any longer
|
|
|
|
* if generated by a kernel event. */
|
|
|
|
eloop_timeout_delete(ifp->ctx->eloop, dhcpcd_pollup, ifp);
|
|
|
|
|
2014-09-30 02:32:15 +08:00
|
|
|
if (carrier == LINK_UNKNOWN) {
|
|
|
|
if (errno != ENOTTY) /* For example a PPP link on BSD */
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR, "%s: carrier_status: %m", ifname);
|
2014-09-30 02:32:15 +08:00
|
|
|
} 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)
|
2015-05-01 17:53:45 +08:00
|
|
|
logger(ctx, LOG_INFO, "%s: carrier lost",
|
|
|
|
ifp->name);
|
2013-02-03 18:35:59 +08:00
|
|
|
ifp->carrier = LINK_DOWN;
|
2014-05-07 17:22:26 +08:00
|
|
|
script_runreason(ifp, "NOCARRIER");
|
2015-05-01 22:02:36 +08:00
|
|
|
#ifdef NOCARRIER_PRESERVE_IP
|
|
|
|
arp_close(ifp);
|
2015-08-07 20:54:21 +08:00
|
|
|
dhcp_abort(ifp);
|
2015-05-17 04:42:05 +08:00
|
|
|
if_sortinterfaces(ctx);
|
|
|
|
ipv4_preferanother(ifp);
|
2015-05-01 23:52:34 +08:00
|
|
|
ipv6nd_expire(ifp, 0);
|
2015-05-01 22:02:36 +08:00
|
|
|
#else
|
2015-04-24 17:18:42 +08:00
|
|
|
dhcpcd_drop(ifp, 0);
|
2015-05-01 17:53:45 +08:00
|
|
|
#endif
|
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) {
|
2015-05-01 17:53:45 +08:00
|
|
|
logger(ctx, 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-04-28 20:02:12 +08:00
|
|
|
dhcpcd_handleinterface(ctx, 0, ifp->name);
|
2013-06-25 16:31:11 +08:00
|
|
|
#endif
|
2015-05-01 17:53:45 +08:00
|
|
|
if (ifp->wireless) {
|
|
|
|
uint8_t ossid[IF_SSIDSIZE];
|
2015-05-02 00:06:39 +08:00
|
|
|
#ifdef NOCARRIER_PRESERVE_IP
|
|
|
|
size_t olen;
|
2015-05-01 17:53:45 +08:00
|
|
|
|
|
|
|
olen = ifp->ssid_len;
|
2015-05-02 00:06:39 +08:00
|
|
|
#endif
|
2015-05-01 17:53:45 +08:00
|
|
|
memcpy(ossid, ifp->ssid, ifp->ssid_len);
|
2014-09-10 09:46:58 +08:00
|
|
|
if_getssid(ifp);
|
2015-05-01 17:53:45 +08:00
|
|
|
#ifdef NOCARRIER_PRESERVE_IP
|
|
|
|
/* If we changed SSID network, drop leases */
|
|
|
|
if (ifp->ssid_len != olen ||
|
|
|
|
memcmp(ifp->ssid, ossid, ifp->ssid_len))
|
|
|
|
dhcpcd_drop(ifp, 0);
|
|
|
|
#endif
|
|
|
|
}
|
2015-03-14 19:06:19 +08:00
|
|
|
dhcpcd_initstate(ifp, 0);
|
2013-02-04 06:55:45 +08:00
|
|
|
script_runreason(ifp, "CARRIER");
|
2015-05-01 23:52:34 +08:00
|
|
|
#ifdef NOCARRIER_PRESERVE_IP
|
|
|
|
/* Set any IPv6 Routers we remembered to expire
|
|
|
|
* faster than they would normally as we
|
|
|
|
* maybe on a new network. */
|
|
|
|
ipv6nd_expire(ifp, RTR_CARRIER_EXPIRE);
|
|
|
|
#endif
|
2015-01-17 10:29:54 +08:00
|
|
|
/* RFC4941 Section 3.5 */
|
|
|
|
if (ifp->options->options & DHCPCD_IPV6RA_OWN)
|
|
|
|
ipv6_gentempifid(ifp);
|
2014-04-28 20:02:12 +08:00
|
|
|
dhcpcd_startinterface(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) {
|
2015-12-17 10:34:36 +08:00
|
|
|
if (ifn == ifp || !ifn->active)
|
2013-11-16 00:44:46 +08:00
|
|
|
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. */
|
2015-06-06 06:06:05 +08:00
|
|
|
if (ifn)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR,
|
2013-11-16 00:44:46 +08:00
|
|
|
"%s: IAID conflicts with one assigned to %s",
|
|
|
|
ifp->name, ifn->name);
|
|
|
|
}
|
|
|
|
|
2014-06-30 23:43:58 +08:00
|
|
|
static void
|
|
|
|
pre_start(struct interface *ifp)
|
2006-11-28 04:23:22 +08:00
|
|
|
{
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2014-06-05 07:54:15 +08:00
|
|
|
/* Add our link-local address before upping the interface
|
2014-06-05 19:38:04 +08:00
|
|
|
* so our RFC7217 address beats the hwaddr based one.
|
|
|
|
* This is also a safety check incase it was ripped out
|
|
|
|
* from under us. */
|
2014-06-30 23:43:58 +08:00
|
|
|
if (ifp->options->options & DHCPCD_IPV6 && ipv6_start(ifp) == -1) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: ipv6_start: %m", ifp->name);
|
2014-11-03 19:50:11 +08:00
|
|
|
ifp->options->options &= ~DHCPCD_IPV6;
|
2014-06-05 07:54:15 +08:00
|
|
|
}
|
2014-06-30 23:43:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dhcpcd_startinterface(void *arg)
|
|
|
|
{
|
|
|
|
struct interface *ifp = arg;
|
|
|
|
struct if_options *ifo = ifp->options;
|
|
|
|
size_t i;
|
|
|
|
char buf[DUID_LEN * 3];
|
2014-10-08 20:30:27 +08:00
|
|
|
int carrier;
|
2015-03-04 01:27:25 +08:00
|
|
|
struct timespec tv;
|
2014-06-05 07:54:15 +08:00
|
|
|
|
2014-07-23 02:03:34 +08:00
|
|
|
if (ifo->options & DHCPCD_LINK) {
|
|
|
|
switch (ifp->carrier) {
|
|
|
|
case LINK_UP:
|
|
|
|
break;
|
|
|
|
case LINK_DOWN:
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_INFO, "%s: waiting for carrier",
|
|
|
|
ifp->name);
|
2014-07-23 02:03:34 +08:00
|
|
|
return;
|
|
|
|
case LINK_UNKNOWN:
|
2014-09-30 02:32:15 +08:00
|
|
|
/* No media state available.
|
2014-10-08 20:30:27 +08:00
|
|
|
* Loop until both IFF_UP and IFF_RUNNING are set */
|
|
|
|
if ((carrier = if_carrier(ifp)) == LINK_UNKNOWN) {
|
|
|
|
tv.tv_sec = 0;
|
2015-05-13 04:03:36 +08:00
|
|
|
tv.tv_nsec = IF_POLL_UP * NSEC_PER_MSEC;
|
2014-10-08 20:30:27 +08:00
|
|
|
eloop_timeout_add_tv(ifp->ctx->eloop,
|
|
|
|
&tv, dhcpcd_startinterface, ifp);
|
|
|
|
} else
|
|
|
|
dhcpcd_handlecarrier(ifp->ctx, carrier,
|
|
|
|
ifp->flags, ifp->name);
|
2014-07-23 02:03:34 +08:00
|
|
|
return;
|
|
|
|
}
|
2008-09-10 01:07:48 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2015-06-06 06:06:05 +08:00
|
|
|
logger(ifp->ctx, LOG_INFO, "DUID %s",
|
|
|
|
hwaddr_ntoa(ifp->ctx->duid,
|
|
|
|
ifp->ctx->duid_len,
|
|
|
|
buf, sizeof(buf)));
|
2013-11-15 21:43:41 +08:00
|
|
|
}
|
2014-07-08 01:23:16 +08:00
|
|
|
}
|
2013-11-15 21:43:41 +08:00
|
|
|
|
2015-06-06 06:06:05 +08:00
|
|
|
if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6)) {
|
2013-11-15 21:43:41 +08:00
|
|
|
/* Report IAIDs */
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, 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
|
|
|
{
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, 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) {
|
2015-09-30 17:02:13 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV6RS)
|
2013-08-20 18:29:43 +08:00
|
|
|
ipv6nd_startrs(ifp);
|
2013-04-05 07:25:31 +08:00
|
|
|
|
2014-10-17 21:46:19 +08:00
|
|
|
if (ifo->options & DHCPCD_DHCP6)
|
|
|
|
dhcp6_find_delegates(ifp);
|
|
|
|
|
2014-07-02 04:34:19 +08:00
|
|
|
if (!(ifo->options & DHCPCD_IPV6RS) ||
|
2015-09-30 17:02:13 +08:00
|
|
|
ifo->options & (DHCPCD_IA_FORCED | DHCPCD_INFORM6))
|
2014-07-02 04:34:19 +08:00
|
|
|
{
|
2014-03-28 06:14:52 +08:00
|
|
|
ssize_t nolease;
|
|
|
|
|
2013-04-02 15:01:11 +08:00
|
|
|
if (ifo->options & DHCPCD_IA_FORCED)
|
2013-05-31 06:39:17 +08:00
|
|
|
nolease = dhcp6_start(ifp, DH6S_INIT);
|
2015-09-30 17:02:13 +08:00
|
|
|
else if (ifo->options & DHCPCD_INFORM6)
|
|
|
|
nolease = dhcp6_start(ifp, DH6S_INFORM);
|
2013-05-24 17:08:23 +08:00
|
|
|
else {
|
2014-03-28 06:14:52 +08:00
|
|
|
nolease = 0;
|
2013-05-24 17:08:23 +08:00
|
|
|
/* 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)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR,
|
2013-04-02 15:01:11 +08:00
|
|
|
"%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
|
|
|
|
2016-01-22 18:11:16 +08:00
|
|
|
#ifdef INET
|
2015-06-20 20:09:55 +08:00
|
|
|
if (ifo->options & DHCPCD_IPV4) {
|
|
|
|
/* Ensure we have an IPv4 state before starting DHCP */
|
|
|
|
if (ipv4_getstate(ifp) != NULL)
|
|
|
|
dhcp_start(ifp);
|
|
|
|
}
|
2016-01-22 18:11:16 +08:00
|
|
|
#endif
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2014-10-08 20:30:27 +08:00
|
|
|
static void
|
|
|
|
dhcpcd_prestartinterface(void *arg)
|
|
|
|
{
|
|
|
|
struct interface *ifp = arg;
|
|
|
|
|
|
|
|
pre_start(ifp);
|
2015-07-14 01:40:54 +08:00
|
|
|
if ((!(ifp->ctx->options & DHCPCD_MASTER) ||
|
|
|
|
ifp->options->options & DHCPCD_IF_UP) &&
|
|
|
|
if_up(ifp) == -1)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR, "%s: if_up: %m", ifp->name);
|
2014-10-08 20:30:27 +08:00
|
|
|
|
|
|
|
if (ifp->options->options & DHCPCD_LINK &&
|
|
|
|
ifp->carrier == LINK_UNKNOWN)
|
|
|
|
{
|
|
|
|
int carrier;
|
|
|
|
|
|
|
|
if ((carrier = if_carrier(ifp)) != LINK_UNKNOWN) {
|
|
|
|
dhcpcd_handlecarrier(ifp->ctx, carrier,
|
|
|
|
ifp->flags, ifp->name);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_INFO,
|
2014-10-08 20:30:27 +08:00
|
|
|
"%s: unknown carrier, waiting for interface flags",
|
|
|
|
ifp->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
dhcpcd_startinterface(ifp);
|
|
|
|
}
|
|
|
|
|
2013-02-16 04:33:13 +08:00
|
|
|
static void
|
2016-01-21 03:08:12 +08:00
|
|
|
run_preinit(struct interface *ifp)
|
2008-09-04 00:49:28 +08:00
|
|
|
{
|
2013-02-03 22:52:24 +08:00
|
|
|
|
2016-01-21 03:08:12 +08:00
|
|
|
pre_start(ifp);
|
|
|
|
if (ifp->ctx->options & DHCPCD_TEST)
|
|
|
|
return;
|
2013-02-16 04:33:13 +08:00
|
|
|
|
2016-01-21 03:08:12 +08:00
|
|
|
script_runreason(ifp, "PREINIT");
|
2013-02-16 04:33:13 +08:00
|
|
|
|
2016-01-21 03:08:12 +08:00
|
|
|
if (ifp->options->options & DHCPCD_LINK && ifp->carrier != LINK_UNKNOWN)
|
|
|
|
script_runreason(ifp,
|
|
|
|
ifp->carrier == LINK_UP ? "CARRIER" : "NOCARRIER");
|
2014-06-05 21:03:49 +08:00
|
|
|
}
|
|
|
|
|
2014-07-07 22:41:18 +08:00
|
|
|
void
|
2016-02-05 05:42:14 +08:00
|
|
|
dhcpcd_activateinterface(struct interface *ifp, unsigned long long options)
|
2014-07-07 22:41:18 +08:00
|
|
|
{
|
|
|
|
|
2016-01-21 03:08:12 +08:00
|
|
|
if (!ifp->active) {
|
2016-02-04 18:43:16 +08:00
|
|
|
ifp->active = IF_ACTIVE;
|
2016-02-05 05:42:14 +08:00
|
|
|
dhcpcd_initstate2(ifp, options);
|
|
|
|
configure_interface1(ifp);
|
2016-01-21 03:08:12 +08:00
|
|
|
run_preinit(ifp);
|
|
|
|
dhcpcd_prestartinterface(ifp);
|
|
|
|
}
|
2014-07-07 22:41:18 +08:00
|
|
|
}
|
|
|
|
|
2014-06-05 21:03:49 +08:00
|
|
|
static void
|
2016-01-21 03:08:12 +08:00
|
|
|
handle_link(void *arg)
|
2014-06-05 21:03:49 +08:00
|
|
|
{
|
2016-01-21 03:08:12 +08:00
|
|
|
struct dhcpcd_ctx *ctx;
|
2014-06-05 19:38:04 +08:00
|
|
|
|
2016-01-21 03:08:12 +08:00
|
|
|
ctx = arg;
|
|
|
|
if (if_managelink(ctx) == -1) {
|
|
|
|
logger(ctx, LOG_ERR, "if_managelink: %m");
|
|
|
|
eloop_event_delete(ctx->eloop, ctx->link_fd);
|
|
|
|
close(ctx->link_fd);
|
|
|
|
ctx->link_fd = -1;
|
|
|
|
}
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2008-04-24 21:18:59 +08:00
|
|
|
|
2014-04-28 20:02:12 +08:00
|
|
|
int
|
|
|
|
dhcpcd_handleinterface(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;
|
2014-06-30 23:55:28 +08:00
|
|
|
struct interface *ifp, *iff, *ifn;
|
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) {
|
2015-04-30 18:16:06 +08:00
|
|
|
ifp = if_find(ctx->ifaces, ifname);
|
2014-04-28 20:02:12 +08:00
|
|
|
if (ifp == NULL) {
|
|
|
|
errno = ESRCH;
|
|
|
|
return -1;
|
2013-06-06 20:38:15 +08:00
|
|
|
}
|
2016-01-11 22:02:01 +08:00
|
|
|
if (ifp->active) {
|
2015-12-17 10:34:36 +08:00
|
|
|
logger(ctx, LOG_DEBUG, "%s: interface departed",
|
|
|
|
ifp->name);
|
2016-01-11 22:02:01 +08:00
|
|
|
ifp->options->options |= DHCPCD_DEPARTED;
|
|
|
|
stop_interface(ifp);
|
|
|
|
}
|
2015-12-17 20:37:07 +08:00
|
|
|
TAILQ_REMOVE(ctx->ifaces, ifp, next);
|
|
|
|
if_free(ifp);
|
2014-04-28 20:02:12 +08:00
|
|
|
return 0;
|
2009-03-20 01:52:12 +08:00
|
|
|
}
|
|
|
|
|
2014-04-28 20:02:12 +08:00
|
|
|
i = -1;
|
2014-04-25 18:42:37 +08:00
|
|
|
ifs = if_discover(ctx, -1, UNCONST(argv));
|
2014-12-20 22:09:26 +08:00
|
|
|
if (ifs == NULL) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR, "%s: if_discover: %m", __func__);
|
2014-12-20 22:09:26 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-02-19 21:37:42 +08:00
|
|
|
TAILQ_FOREACH_SAFE(ifp, ifs, next, ifn) {
|
2016-01-14 21:43:15 +08:00
|
|
|
if (strcmp(ifp->name, ifname) != 0)
|
2009-09-01 05:51:17 +08:00
|
|
|
continue;
|
2016-01-14 21:43:15 +08:00
|
|
|
|
|
|
|
/* If running off an interface list, check it's in it. */
|
|
|
|
if (ctx->ifc) {
|
|
|
|
for (i = 0; i < ctx->ifc; i++)
|
|
|
|
if (strcmp(ctx->ifv[i], ifname) == 0)
|
|
|
|
break;
|
|
|
|
if (i >= ctx->ifc)
|
2016-02-04 18:43:16 +08:00
|
|
|
ifp->active = IF_INACTIVE;
|
2016-01-14 21:43:15 +08:00
|
|
|
}
|
|
|
|
|
2014-04-28 20:02:12 +08:00
|
|
|
i = 0;
|
2009-09-01 05:51:17 +08:00
|
|
|
/* Check if we already have the interface */
|
2015-04-30 18:16:06 +08:00
|
|
|
iff = if_find(ctx->ifaces, ifp->name);
|
2014-06-30 23:55:28 +08:00
|
|
|
if (iff) {
|
2016-01-14 21:43:15 +08:00
|
|
|
if (iff->active)
|
|
|
|
logger(ctx, LOG_DEBUG, "%s: interface updated",
|
|
|
|
iff->name);
|
2009-10-15 15:33:24 +08:00
|
|
|
/* The flags and hwaddr could have changed */
|
2014-06-30 23:55:28 +08:00
|
|
|
iff->flags = ifp->flags;
|
|
|
|
iff->hwlen = ifp->hwlen;
|
2009-10-15 15:33:24 +08:00
|
|
|
if (ifp->hwlen != 0)
|
2014-06-30 23:55:28 +08:00
|
|
|
memcpy(iff->hwaddr, ifp->hwaddr, iff->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);
|
2016-01-14 21:43:15 +08:00
|
|
|
if (!ifp->active)
|
|
|
|
continue;
|
|
|
|
logger(ctx, LOG_DEBUG, "%s: interface added",
|
|
|
|
ifp->name);
|
2015-03-14 19:06:19 +08:00
|
|
|
dhcpcd_initstate(ifp, 0);
|
2014-06-05 21:03:49 +08:00
|
|
|
run_preinit(ifp);
|
2014-06-30 23:55:28 +08:00
|
|
|
iff = ifp;
|
2013-06-25 16:31:11 +08:00
|
|
|
}
|
2016-01-14 21:43:15 +08:00
|
|
|
if (action > 0 && iff->active)
|
2014-10-08 20:30:27 +08:00
|
|
|
dhcpcd_prestartinterface(iff);
|
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);
|
2014-04-25 18:42:37 +08:00
|
|
|
if_free(ifp);
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
|
|
|
free(ifs);
|
2014-04-28 20:02:12 +08:00
|
|
|
|
|
|
|
if (i == -1)
|
|
|
|
errno = ENOENT;
|
|
|
|
return i;
|
2008-09-04 19:30:11 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 00:33:45 +08:00
|
|
|
void
|
2014-04-28 20:02:12 +08:00
|
|
|
dhcpcd_handlehwaddr(struct dhcpcd_ctx *ctx, const char *ifname,
|
2014-03-28 06:14:52 +08:00
|
|
|
const uint8_t *hwaddr, uint8_t hwlen)
|
2010-11-13 00:33:45 +08:00
|
|
|
{
|
|
|
|
struct interface *ifp;
|
2014-02-25 22:11:35 +08:00
|
|
|
char buf[sizeof(ifp->hwaddr) * 3];
|
2010-11-13 00:33:45 +08:00
|
|
|
|
2015-04-30 18:16:06 +08:00
|
|
|
ifp = if_find(ctx->ifaces, ifname);
|
2013-06-25 16:31:11 +08:00
|
|
|
if (ifp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (hwlen > sizeof(ifp->hwaddr)) {
|
|
|
|
errno = ENOBUFS;
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR, "%s: %s: %m", ifp->name, __func__);
|
2013-06-25 16:31:11 +08:00
|
|
|
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;
|
|
|
|
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, 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
|
|
|
{
|
2014-03-28 06:14:52 +08:00
|
|
|
unsigned long long oldopts;
|
2013-02-04 06:55:45 +08:00
|
|
|
|
|
|
|
oldopts = ifp->options->options;
|
2013-06-08 04:03:28 +08:00
|
|
|
script_runreason(ifp, "RECONFIGURE");
|
2015-03-14 19:06:19 +08:00
|
|
|
dhcpcd_initstate1(ifp, argc, argv, 0);
|
2013-03-26 18:42:30 +08:00
|
|
|
dhcp_reboot_newopts(ifp, oldopts);
|
2013-06-07 20:44:19 +08:00
|
|
|
dhcp6_reboot(ifp);
|
2014-10-08 20:30:27 +08:00
|
|
|
dhcpcd_prestartinterface(ifp);
|
2010-06-09 23:26:55 +08:00
|
|
|
}
|
|
|
|
|
2014-11-18 12:23:40 +08:00
|
|
|
static void
|
|
|
|
reload_config(struct dhcpcd_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct if_options *ifo;
|
|
|
|
|
|
|
|
free_globals(ctx);
|
|
|
|
ifo = read_config(ctx, NULL, NULL, NULL);
|
|
|
|
add_options(ctx, NULL, ifo, ctx->argc, ctx->argv);
|
|
|
|
/* We need to preserve these two options. */
|
|
|
|
if (ctx->options & DHCPCD_MASTER)
|
|
|
|
ifo->options |= DHCPCD_MASTER;
|
|
|
|
if (ctx->options & DHCPCD_DAEMONISED)
|
|
|
|
ifo->options |= DHCPCD_DAEMONISED;
|
|
|
|
ctx->options = ifo->options;
|
|
|
|
free_options(ifo);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-12-17 20:37:07 +08:00
|
|
|
int i;
|
|
|
|
struct interface *ifp;
|
2010-06-09 23:26:55 +08:00
|
|
|
|
2015-12-17 20:37:07 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
|
|
|
for (i = oi; i < argc; i++) {
|
|
|
|
if (strcmp(ifp->name, argv[i]) == 0)
|
|
|
|
break;
|
2015-12-17 10:34:36 +08:00
|
|
|
}
|
2015-12-17 20:37:07 +08:00
|
|
|
if (oi != argc && i == argc)
|
|
|
|
continue;
|
|
|
|
if (ifp->active) {
|
2010-06-09 23:26:55 +08:00
|
|
|
if (action)
|
2015-12-17 20:37:07 +08:00
|
|
|
if_reboot(ifp, argc, argv);
|
2013-02-04 06:55:45 +08:00
|
|
|
else
|
2015-12-17 20:37:07 +08:00
|
|
|
ipv4_applyaddr(ifp);
|
|
|
|
} else if (i != argc) {
|
2016-02-04 18:43:16 +08:00
|
|
|
ifp->active = IF_ACTIVE_USER;
|
2015-03-14 19:06:19 +08:00
|
|
|
dhcpcd_initstate1(ifp, argc, argv, 0);
|
2014-06-05 21:03:49 +08:00
|
|
|
run_preinit(ifp);
|
2014-10-08 20:30:27 +08:00
|
|
|
dhcpcd_prestartinterface(ifp);
|
2010-06-09 23:26:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
static void
|
2015-09-16 05:37:36 +08:00
|
|
|
stop_all_interfaces(struct dhcpcd_ctx *ctx, unsigned long long opts)
|
2014-02-22 00:10:55 +08:00
|
|
|
{
|
2014-11-08 02:57:35 +08:00
|
|
|
struct interface *ifp;
|
2014-02-22 00:10:55 +08:00
|
|
|
|
2015-06-06 06:06:05 +08:00
|
|
|
/* Drop the last interface first */
|
2015-12-17 20:37:07 +08:00
|
|
|
TAILQ_FOREACH_REVERSE(ifp, ctx->ifaces, if_head, next) {
|
2015-12-17 10:34:36 +08:00
|
|
|
if (ifp->options) {
|
|
|
|
ifp->options->options |= opts;
|
|
|
|
if (ifp->options->options & DHCPCD_RELEASE)
|
|
|
|
ifp->options->options &= ~DHCPCD_PERSISTENT;
|
|
|
|
ifp->options->options |= DHCPCD_EXITING;
|
|
|
|
}
|
2014-02-22 00:10:55 +08:00
|
|
|
stop_interface(ifp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 18:37:27 +08:00
|
|
|
static void
|
|
|
|
dhcpcd_ifrenew(struct interface *ifp)
|
|
|
|
{
|
|
|
|
|
|
|
|
#define DHCPCD_RARENEW (DHCPCD_IPV6 | DHCPCD_IPV6RS)
|
|
|
|
if (ifp->options->options & DHCPCD_LINK &&
|
|
|
|
ifp->carrier != LINK_DOWN)
|
|
|
|
{
|
|
|
|
dhcp_renew(ifp);
|
|
|
|
if ((ifp->options->options & DHCPCD_RARENEW) == DHCPCD_RARENEW)
|
|
|
|
ipv6nd_startrs(ifp);
|
|
|
|
dhcp6_renew(ifp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dhcpcd_renew(struct dhcpcd_ctx *ctx)
|
|
|
|
{
|
|
|
|
struct interface *ifp;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(ifp, ctx->ifaces, next) {
|
|
|
|
dhcpcd_ifrenew(ifp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
#ifdef USE_SIGNALS
|
2015-03-18 07:46:38 +08:00
|
|
|
#define sigmsg "received %s, %s"
|
2015-05-09 18:09:34 +08:00
|
|
|
static void
|
2015-05-12 22:14:32 +08:00
|
|
|
signal_cb(int sig, void *arg)
|
2008-09-02 21:28:11 +08:00
|
|
|
{
|
2015-05-09 18:09:34 +08:00
|
|
|
struct dhcpcd_ctx *ctx = arg;
|
2015-09-16 05:37:36 +08:00
|
|
|
unsigned long long opts;
|
|
|
|
int exit_code;
|
2008-04-24 21:18:59 +08:00
|
|
|
|
2015-09-16 05:37:36 +08:00
|
|
|
opts = 0;
|
2015-02-19 04:29:42 +08:00
|
|
|
exit_code = EXIT_FAILURE;
|
2015-05-09 18:09:34 +08:00
|
|
|
switch (sig) {
|
2008-09-02 21:28:11 +08:00
|
|
|
case SIGINT:
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_INFO, sigmsg, "SIGINT", "stopping");
|
2008-09-02 21:28:11 +08:00
|
|
|
break;
|
|
|
|
case SIGTERM:
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_INFO, sigmsg, "SIGTERM", "stopping");
|
2015-02-19 04:29:42 +08:00
|
|
|
exit_code = EXIT_SUCCESS;
|
2008-09-02 21:28:11 +08:00
|
|
|
break;
|
2008-09-04 00:49:28 +08:00
|
|
|
case SIGALRM:
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_INFO, sigmsg, "SIGALRM", "releasing");
|
2015-09-16 05:37:36 +08:00
|
|
|
opts |= DHCPCD_RELEASE;
|
2015-02-19 04:29:42 +08:00
|
|
|
exit_code = EXIT_SUCCESS;
|
2014-03-07 18:08:58 +08:00
|
|
|
break;
|
|
|
|
case SIGHUP:
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_INFO, sigmsg, "SIGHUP", "rebinding");
|
2014-11-18 12:23:40 +08:00
|
|
|
reload_config(ctx);
|
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;
|
2009-03-20 18:21:12 +08:00
|
|
|
case SIGUSR1:
|
2015-11-13 18:37:27 +08:00
|
|
|
logger(ctx, LOG_INFO, sigmsg, "SIGUSR1", "renewing");
|
|
|
|
dhcpcd_renew(ctx);
|
2009-03-20 18:33:09 +08:00
|
|
|
return;
|
2015-03-18 07:46:38 +08:00
|
|
|
case SIGUSR2:
|
|
|
|
logger_close(ctx);
|
|
|
|
logger_open(ctx);
|
|
|
|
logger(ctx, LOG_INFO, sigmsg, "SIGUSR2", "reopened logfile");
|
|
|
|
return;
|
2009-01-21 00:33:11 +08:00
|
|
|
case SIGPIPE:
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_WARNING, "received SIGPIPE");
|
2009-01-21 00:33:11 +08:00
|
|
|
return;
|
2008-09-02 21:28:11 +08:00
|
|
|
default:
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_ERR,
|
2015-03-04 23:47:04 +08:00
|
|
|
"received signal %d, "
|
2013-06-07 20:44:19 +08:00
|
|
|
"but don't know what to do with it",
|
2015-05-09 18:09:34 +08:00
|
|
|
sig);
|
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))
|
2015-09-16 05:37:36 +08:00
|
|
|
stop_all_interfaces(ctx, opts);
|
2015-02-19 04:29:42 +08:00
|
|
|
eloop_exit(ctx->eloop, exit_code);
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
|
|
|
|
2014-09-05 03:30:47 +08:00
|
|
|
static void
|
|
|
|
dhcpcd_getinterfaces(void *arg)
|
|
|
|
{
|
|
|
|
struct fd_list *fd = arg;
|
|
|
|
struct interface *ifp;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
TAILQ_FOREACH(ifp, fd->ctx->ifaces, next) {
|
2015-12-17 10:34:36 +08:00
|
|
|
if (!ifp->active)
|
|
|
|
continue;
|
2014-09-05 03:30:47 +08:00
|
|
|
len++;
|
|
|
|
if (D_STATE_RUNNING(ifp))
|
|
|
|
len++;
|
2015-06-29 23:31:33 +08:00
|
|
|
if (IPV4LL_STATE_RUNNING(ifp))
|
|
|
|
len++;
|
2014-09-05 03:55:37 +08:00
|
|
|
if (RS_STATE_RUNNING(ifp))
|
2014-09-05 03:30:47 +08:00
|
|
|
len++;
|
2014-09-05 03:55:37 +08:00
|
|
|
if (D6_STATE_RUNNING(ifp))
|
2014-09-05 03:30:47 +08:00
|
|
|
len++;
|
|
|
|
}
|
|
|
|
if (write(fd->fd, &len, sizeof(len)) != sizeof(len))
|
|
|
|
return;
|
2015-05-14 15:35:49 +08:00
|
|
|
eloop_event_remove_writecb(fd->ctx->eloop, fd->fd);
|
2014-09-05 03:30:47 +08:00
|
|
|
TAILQ_FOREACH(ifp, fd->ctx->ifaces, next) {
|
2015-12-17 10:34:36 +08:00
|
|
|
if (!ifp->active)
|
|
|
|
continue;
|
2014-09-05 20:28:05 +08:00
|
|
|
if (send_interface(fd, ifp) == -1)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ifp->ctx, LOG_ERR,
|
|
|
|
"send_interface %d: %m", fd->fd);
|
2014-09-05 03:30:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
int
|
2014-04-28 20:02:12 +08:00
|
|
|
dhcpcd_handleargs(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;
|
2015-09-16 05:37:36 +08:00
|
|
|
unsigned long long opts;
|
2015-11-13 18:37:27 +08:00
|
|
|
int opt, oi, do_reboot, do_renew;
|
2014-03-28 06:14:52 +08:00
|
|
|
size_t len, l;
|
2009-02-06 05:06:57 +08:00
|
|
|
char *tmp, *p;
|
2008-09-04 00:49:28 +08:00
|
|
|
|
2014-09-15 03:12:57 +08:00
|
|
|
/* Special commands for our control socket
|
|
|
|
* as the other end should be blocking until it gets the
|
|
|
|
* expected reply we should be safely able just to change the
|
|
|
|
* write callback on the fd */
|
|
|
|
if (strcmp(*argv, "--version") == 0) {
|
|
|
|
return control_queue(fd, UNCONST(VERSION),
|
|
|
|
strlen(VERSION) + 1, 0);
|
|
|
|
} else if (strcmp(*argv, "--getconfigfile") == 0) {
|
|
|
|
return control_queue(fd, UNCONST(fd->ctx->cffile),
|
|
|
|
strlen(fd->ctx->cffile) + 1, 0);
|
|
|
|
} else if (strcmp(*argv, "--getinterfaces") == 0) {
|
|
|
|
eloop_event_add(fd->ctx->eloop, fd->fd, NULL, NULL,
|
|
|
|
dhcpcd_getinterfaces, fd);
|
|
|
|
return 0;
|
|
|
|
} else if (strcmp(*argv, "--listen") == 0) {
|
|
|
|
fd->flags |= FD_LISTEN;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only priviledged users can control dhcpcd via the socket. */
|
|
|
|
if (fd->flags & FD_UNPRIV) {
|
|
|
|
errno = EPERM;
|
|
|
|
return -1;
|
2009-01-05 16:20:53 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2014-09-05 20:28:05 +08:00
|
|
|
if (tmp == NULL)
|
2013-02-16 21:21:35 +08:00
|
|
|
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';
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(ctx, LOG_INFO, "control command: %s", tmp);
|
2009-02-06 05:06:57 +08:00
|
|
|
free(tmp);
|
|
|
|
|
2008-09-04 00:49:28 +08:00
|
|
|
optind = 0;
|
2015-09-16 05:37:36 +08:00
|
|
|
oi = 0;
|
|
|
|
opts = 0;
|
2015-11-13 18:37:27 +08:00
|
|
|
do_reboot = do_renew = 0;
|
2008-09-04 00:49:28 +08:00
|
|
|
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':
|
2015-09-16 05:37:36 +08:00
|
|
|
opts |= DHCPCD_RELEASE;
|
2008-09-04 00:49:28 +08:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
do_reboot = 1;
|
|
|
|
break;
|
2015-09-16 05:37:36 +08:00
|
|
|
case 'p':
|
|
|
|
opts |= DHCPCD_PERSISTENT;
|
|
|
|
break;
|
2008-09-04 00:49:28 +08:00
|
|
|
case 'x':
|
2015-09-16 05:37:36 +08:00
|
|
|
opts |= DHCPCD_EXITING;
|
2008-09-04 00:49:28 +08:00
|
|
|
break;
|
2015-11-13 18:37:27 +08:00
|
|
|
case 'N':
|
|
|
|
do_renew = 1;
|
|
|
|
break;
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 05:37:36 +08:00
|
|
|
if (opts & (DHCPCD_EXITING | DHCPCD_RELEASE)) {
|
2014-02-22 00:10:55 +08:00
|
|
|
if (optind == argc) {
|
2015-09-16 05:37:36 +08:00
|
|
|
stop_all_interfaces(ctx, opts);
|
2014-02-22 00:10:55 +08:00
|
|
|
eloop_exit(ctx->eloop, EXIT_SUCCESS);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-04 16:27:38 +08:00
|
|
|
for (oi = optind; oi < argc; oi++) {
|
2015-04-30 18:16:06 +08:00
|
|
|
if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
|
2008-09-04 16:27:38 +08:00
|
|
|
continue;
|
2015-09-16 05:37:36 +08:00
|
|
|
ifp->options->options |= opts;
|
|
|
|
if (opts & DHCPCD_RELEASE)
|
2013-11-20 19:37:25 +08:00
|
|
|
ifp->options->options &= ~DHCPCD_PERSISTENT;
|
2009-01-21 00:33:11 +08:00
|
|
|
stop_interface(ifp);
|
2008-09-04 00:49:28 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-13 18:37:27 +08:00
|
|
|
if (do_renew) {
|
|
|
|
if (optind == argc) {
|
|
|
|
dhcpcd_renew(ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (oi = optind; oi < argc; oi++) {
|
|
|
|
if ((ifp = if_find(ctx->ifaces, argv[oi])) == NULL)
|
|
|
|
continue;
|
|
|
|
dhcpcd_ifrenew(ifp);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-18 12:23:40 +08:00
|
|
|
reload_config(ctx);
|
2014-02-25 18:24:01 +08:00
|
|
|
/* XXX: Respect initial commandline options? */
|
2015-05-17 05:16:59 +08:00
|
|
|
reconf_reboot(ctx, do_reboot, argc, argv, optind - 1);
|
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;
|
|
|
|
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;
|
2014-03-28 06:14:52 +08:00
|
|
|
time_t t;
|
|
|
|
ssize_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
|
2015-05-13 07:47:25 +08:00
|
|
|
int sig = 0;
|
|
|
|
const char *siga = NULL;
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 07:46:38 +08:00
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
|
|
closefrom(3);
|
|
|
|
|
|
|
|
ctx.log_fd = -1;
|
|
|
|
logger_open(&ctx);
|
|
|
|
logger_mask(&ctx, LOG_UPTO(LOG_INFO));
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ifo = NULL;
|
|
|
|
ctx.cffile = CONFIG;
|
2014-09-15 03:12:57 +08:00
|
|
|
ctx.pid_fd = ctx.control_fd = ctx.control_unpriv_fd = ctx.link_fd = -1;
|
2015-08-06 21:07:15 +08:00
|
|
|
ctx.pf_inet_fd = -1;
|
|
|
|
#if defined(INET6) && defined(BSD)
|
|
|
|
ctx.pf_inet6_fd = -1;
|
|
|
|
#endif
|
|
|
|
#ifdef IFLR_ACTIVE
|
|
|
|
ctx.pf_link_fd = -1;
|
|
|
|
#endif
|
|
|
|
|
2014-09-05 20:28:05 +08:00
|
|
|
TAILQ_INIT(&ctx.control_fds);
|
2014-02-12 17:25:58 +08:00
|
|
|
#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;
|
2016-01-08 03:52:54 +08:00
|
|
|
while ((opt = getopt_long(argc, argv,
|
|
|
|
ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : 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;
|
2015-03-18 07:46:38 +08:00
|
|
|
case 'j':
|
|
|
|
ctx.logfile = strdup(optarg);
|
|
|
|
logger_close(&ctx);
|
|
|
|
logger_open(&ctx);
|
|
|
|
break;
|
2015-12-21 19:07:04 +08:00
|
|
|
#ifdef USE_SIGNALS
|
2008-09-04 00:49:28 +08:00
|
|
|
case 'k':
|
2014-03-07 18:08:58 +08:00
|
|
|
sig = SIGALRM;
|
|
|
|
siga = "ARLM";
|
2008-09-04 00:49:28 +08:00
|
|
|
break;
|
|
|
|
case 'n':
|
2014-03-07 18:08:58 +08:00
|
|
|
sig = SIGHUP;
|
|
|
|
siga = "HUP";
|
2008-09-04 00:49:28 +08:00
|
|
|
break;
|
2015-12-21 19:07:04 +08:00
|
|
|
case 'g':
|
2015-09-16 05:37:36 +08:00
|
|
|
case 'p':
|
|
|
|
/* Force going via command socket as we're
|
|
|
|
* out of user definable signals. */
|
|
|
|
i = 4;
|
|
|
|
break;
|
2008-03-21 00:47:51 +08:00
|
|
|
case 'x':
|
|
|
|
sig = SIGTERM;
|
2015-09-16 05:37:36 +08:00
|
|
|
siga = "TERM";
|
2008-03-21 00:47:51 +08:00
|
|
|
break;
|
2015-11-13 18:37:27 +08:00
|
|
|
case 'N':
|
|
|
|
sig = SIGUSR1;
|
|
|
|
siga = "USR1";
|
|
|
|
break;
|
2014-02-22 00:10:55 +08:00
|
|
|
#endif
|
2016-01-08 03:52:54 +08:00
|
|
|
case 'P':
|
|
|
|
ctx.options |= DHCPCD_PRINT_PIDFILE;
|
|
|
|
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':
|
2015-06-06 06:06:05 +08:00
|
|
|
i = 3;
|
2010-08-24 17:16:07 +08:00
|
|
|
break;
|
2008-09-02 21:28:11 +08:00
|
|
|
case 'V':
|
2014-07-07 22:41:18 +08:00
|
|
|
i = 2;
|
2013-12-04 07:10:21 +08:00
|
|
|
break;
|
2008-09-02 21:28:11 +08:00
|
|
|
case '?':
|
2016-01-08 03:52:54 +08:00
|
|
|
if (ctx.options & DHCPCD_PRINT_PIDFILE)
|
|
|
|
continue;
|
2008-09-02 21:28:11 +08:00
|
|
|
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;
|
2014-03-11 18:41:27 +08:00
|
|
|
ctx.ifc = argc - optind;
|
|
|
|
ctx.ifv = argv + optind;
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
ifo = read_config(&ctx, NULL, NULL, NULL);
|
2016-01-08 03:52:54 +08:00
|
|
|
if (ifo == NULL) {
|
|
|
|
if (ctx.options & DHCPCD_PRINT_PIDFILE)
|
|
|
|
goto printpidfile;
|
2014-07-04 07:11:00 +08:00
|
|
|
goto exit_failure;
|
2016-01-08 03:52:54 +08:00
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
opt = add_options(&ctx, NULL, ifo, argc, argv);
|
2008-09-02 21:28:11 +08:00
|
|
|
if (opt != 1) {
|
2016-01-08 03:52:54 +08:00
|
|
|
if (ctx.options & DHCPCD_PRINT_PIDFILE)
|
|
|
|
goto printpidfile;
|
2008-09-02 21:28:11 +08:00
|
|
|
if (opt == 0)
|
|
|
|
usage();
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2008-09-02 21:28:11 +08:00
|
|
|
}
|
2014-07-07 22:41:18 +08:00
|
|
|
if (i == 2) {
|
2013-12-04 07:10:21 +08:00
|
|
|
printf("Interface options:\n");
|
2014-07-04 07:11:00 +08:00
|
|
|
if (optind == argc - 1) {
|
|
|
|
free_options(ifo);
|
|
|
|
ifo = read_config(&ctx, argv[optind], NULL, NULL);
|
|
|
|
if (ifo == NULL)
|
|
|
|
goto exit_failure;
|
|
|
|
add_options(&ctx, NULL, ifo, argc, argv);
|
|
|
|
}
|
2013-12-04 07:10:21 +08:00
|
|
|
if_printoptions();
|
|
|
|
#ifdef INET
|
|
|
|
if (family == 0 || family == AF_INET) {
|
|
|
|
printf("\nDHCPv4 options:\n");
|
2014-07-04 07:11:00 +08:00
|
|
|
dhcp_printoptions(&ctx,
|
|
|
|
ifo->dhcp_override, ifo->dhcp_override_len);
|
2013-12-04 07:10:21 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
if (family == 0 || family == AF_INET6) {
|
2015-05-15 03:46:21 +08:00
|
|
|
printf("\nND options:\n");
|
|
|
|
ipv6nd_printoptions(&ctx,
|
|
|
|
ifo->nd_override, ifo->nd_override_len);
|
2013-12-04 07:10:21 +08:00
|
|
|
printf("\nDHCPv6 options:\n");
|
2014-07-04 07:11:00 +08:00
|
|
|
dhcp6_printoptions(&ctx,
|
|
|
|
ifo->dhcp6_override, ifo->dhcp6_override_len);
|
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
|
|
|
}
|
2016-01-08 03:52:54 +08:00
|
|
|
ctx.options |= ifo->options;
|
2015-09-16 05:37:36 +08:00
|
|
|
if (i == 1 || i == 3) {
|
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
|
|
|
|
|
2015-03-18 07:46:38 +08:00
|
|
|
if (ctx.options & DHCPCD_DEBUG)
|
|
|
|
logger_mask(&ctx, LOG_UPTO(LOG_DEBUG));
|
2008-09-11 16:28:39 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ctx.options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) {
|
2016-01-08 03:52:54 +08:00
|
|
|
printpidfile:
|
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-03-07 18:22:37 +08:00
|
|
|
if (optind == argc - 1 && !(ctx.options & DHCPCD_MASTER)) {
|
2014-05-29 20:52:27 +08:00
|
|
|
const char *per;
|
|
|
|
|
2014-02-12 19:55:43 +08:00
|
|
|
if (strlen(argv[optind]) > IF_NAMESIZE) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR,
|
|
|
|
"%s: interface name too long",
|
2014-02-12 19:55:43 +08:00
|
|
|
argv[optind]);
|
|
|
|
goto exit_failure;
|
|
|
|
}
|
2014-05-29 20:52:27 +08:00
|
|
|
/* Allow a dhcpcd interface per address family */
|
|
|
|
switch(family) {
|
|
|
|
case AF_INET:
|
|
|
|
per = "-4";
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
per = "-6";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
per = "";
|
|
|
|
}
|
2015-02-20 16:28:04 +08:00
|
|
|
snprintf(ctx.pidfile, sizeof(ctx.pidfile),
|
2014-05-29 20:52:27 +08:00
|
|
|
PIDFILE, "-", argv[optind], per);
|
2014-07-04 19:53:56 +08:00
|
|
|
} else {
|
2015-02-20 16:28:04 +08:00
|
|
|
snprintf(ctx.pidfile, sizeof(ctx.pidfile),
|
|
|
|
PIDFILE, "", "", "");
|
2014-02-12 08:39:46 +08:00
|
|
|
ctx.options |= DHCPCD_MASTER;
|
2009-07-12 02:54:43 +08:00
|
|
|
}
|
2016-01-08 03:52:54 +08:00
|
|
|
if (ctx.options & DHCPCD_PRINT_PIDFILE) {
|
|
|
|
printf("%s\n", ctx.pidfile);
|
|
|
|
goto exit_success;
|
|
|
|
}
|
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)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "chdir `/': %m");
|
2008-08-15 00:16:06 +08:00
|
|
|
|
2014-06-22 21:05:05 +08:00
|
|
|
/* Freeing allocated addresses from dumping leases can trigger
|
|
|
|
* eloop removals as well, so init here. */
|
2015-05-12 22:14:32 +08:00
|
|
|
if ((ctx.eloop = eloop_new()) == NULL) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "%s: eloop_init: %m", __func__);
|
2014-06-22 21:05:05 +08:00
|
|
|
goto exit_failure;
|
|
|
|
}
|
|
|
|
|
2015-10-27 20:46:48 +08:00
|
|
|
/* Open our persistent sockets.
|
|
|
|
* This is needed early for dumping leases on valid interfaces. */
|
|
|
|
if (if_opensockets(&ctx) == -1) {
|
|
|
|
logger(&ctx, LOG_ERR, "if_opensockets: %m");
|
|
|
|
goto exit_failure;
|
|
|
|
}
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.options & DHCPCD_DUMPLEASE) {
|
2015-11-18 19:59:11 +08:00
|
|
|
if (optind != argc) {
|
|
|
|
/* We need to try and find the interface so we can load
|
|
|
|
* the hardware address to compare automated IAID */
|
|
|
|
ctx.ifaces = if_discover(&ctx,
|
|
|
|
argc - optind, argv + optind);
|
|
|
|
} else {
|
2015-11-18 20:06:47 +08:00
|
|
|
if ((ctx.ifaces = malloc(sizeof(*ctx.ifaces))) != NULL)
|
2015-11-18 19:59:11 +08:00
|
|
|
TAILQ_INIT(ctx.ifaces);
|
2010-08-24 17:16:07 +08:00
|
|
|
}
|
2014-12-20 22:09:26 +08:00
|
|
|
if (ctx.ifaces == NULL) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "if_discover: %m");
|
2014-07-03 17:47:42 +08:00
|
|
|
goto exit_failure;
|
2014-12-20 22:09:26 +08:00
|
|
|
}
|
2015-12-17 10:34:36 +08:00
|
|
|
ifp = if_find(ctx.ifaces, argv[optind]);
|
2014-07-03 17:47:42 +08:00
|
|
|
if (ifp == NULL) {
|
|
|
|
ifp = calloc(1, sizeof(*ifp));
|
|
|
|
if (ifp == NULL) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "%s: %m", __func__);
|
2014-06-22 21:05:05 +08:00
|
|
|
goto exit_failure;
|
|
|
|
}
|
2015-11-18 19:59:11 +08:00
|
|
|
if (optind != argc)
|
|
|
|
strlcpy(ctx.pidfile, argv[optind],
|
|
|
|
sizeof(ctx.pidfile));
|
2014-07-03 17:47:42 +08:00
|
|
|
ifp->ctx = &ctx;
|
|
|
|
TAILQ_INSERT_HEAD(ctx.ifaces, ifp, next);
|
2015-02-20 16:28:04 +08:00
|
|
|
if (family == 0) {
|
|
|
|
if (ctx.pidfile[strlen(ctx.pidfile) - 1] == '6')
|
|
|
|
family = AF_INET6;
|
|
|
|
else
|
|
|
|
family = AF_INET;
|
|
|
|
}
|
2014-06-22 21:05:05 +08:00
|
|
|
}
|
2015-03-14 19:06:19 +08:00
|
|
|
configure_interface(ifp, ctx.argc, ctx.argv, 0);
|
2015-11-18 19:59:11 +08:00
|
|
|
i = 0;
|
2014-06-22 21:05:05 +08:00
|
|
|
if (family == 0 || family == AF_INET) {
|
|
|
|
if (dhcp_dump(ifp) == -1)
|
2015-11-19 19:00:34 +08:00
|
|
|
i = -1;
|
2014-06-22 21:05:05 +08:00
|
|
|
}
|
|
|
|
if (family == 0 || family == AF_INET6) {
|
|
|
|
if (dhcp6_dump(ifp) == -1)
|
2015-11-19 19:00:34 +08:00
|
|
|
i = -1;
|
2014-06-22 21:05:05 +08:00
|
|
|
}
|
|
|
|
if (i == -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
|
2015-12-21 19:07:32 +08:00
|
|
|
/* Test against siga instead of sig to avoid gcc
|
|
|
|
* warning about a bogus potential signed overflow.
|
|
|
|
* The end result will be the same. */
|
|
|
|
if ((siga == NULL || i == 4 || ctx.ifc != 0) &&
|
|
|
|
!(ctx.options & DHCPCD_TEST))
|
2014-03-11 18:41:27 +08:00
|
|
|
{
|
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) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_INFO,
|
2009-02-12 01:56:22 +08:00
|
|
|
"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) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_DEBUG, "send OK");
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_success;
|
2008-09-04 00:49:28 +08:00
|
|
|
} else {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, 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)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, 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
|
|
|
|
2014-02-22 00:10:55 +08:00
|
|
|
#ifdef USE_SIGNALS
|
2008-09-02 21:28:11 +08:00
|
|
|
if (sig != 0) {
|
2015-02-20 16:28:04 +08:00
|
|
|
pid = read_pid(ctx.pidfile);
|
2007-07-12 20:52:27 +08:00
|
|
|
if (pid != 0)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_INFO, "sending signal %s to pid %d",
|
2014-03-07 18:08:58 +08:00
|
|
|
siga, pid);
|
2009-05-09 22:15:31 +08:00
|
|
|
if (pid == 0 || kill(pid, sig) != 0) {
|
2015-11-13 18:37:27 +08:00
|
|
|
if (sig != SIGHUP && sig != SIGUSR1 && errno != EPERM)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, ""PACKAGE" not running");
|
2009-10-14 23:16:07 +08:00
|
|
|
if (pid != 0 && errno != ESRCH) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "kill: %m");
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2009-10-14 23:16:07 +08:00
|
|
|
}
|
2015-02-20 16:28:04 +08:00
|
|
|
unlink(ctx.pidfile);
|
2015-11-13 18:37:27 +08:00
|
|
|
if (sig != SIGHUP && sig != SIGUSR1)
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2009-05-09 22:15:31 +08:00
|
|
|
} else {
|
2015-03-04 19:41:53 +08:00
|
|
|
struct timespec ts;
|
|
|
|
|
2014-03-07 18:08:58 +08:00
|
|
|
if (sig == SIGHUP || 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 */
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_INFO,
|
|
|
|
"waiting for pid %d to exit", pid);
|
2009-05-09 22:15:31 +08:00
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 100000000; /* 10th of a second */
|
|
|
|
for(i = 0; i < 100; i++) {
|
|
|
|
nanosleep(&ts, NULL);
|
2015-02-20 16:28:04 +08:00
|
|
|
if (read_pid(ctx.pidfile) == 0)
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_success;
|
2009-05-09 22:15:31 +08:00
|
|
|
}
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, 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)) {
|
2015-02-20 16:28:04 +08:00
|
|
|
if ((pid = read_pid(ctx.pidfile)) > 0 &&
|
2008-03-21 00:47:51 +08:00
|
|
|
kill(pid, 0) == 0)
|
2008-01-17 00:38:47 +08:00
|
|
|
{
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, ""PACKAGE
|
2009-02-12 01:56:22 +08:00
|
|
|
" already running on pid %d (%s)",
|
2015-02-20 16:28:04 +08:00
|
|
|
pid, ctx.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)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "mkdir `%s': %m", RUNDIR);
|
2012-01-30 01:28:05 +08:00
|
|
|
if (mkdir(DBDIR, 0755) == -1 && errno != EEXIST)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "mkdir `%s': %m", DBDIR);
|
2009-07-09 03:22:31 +08:00
|
|
|
|
2014-04-18 21:17:42 +08:00
|
|
|
opt = O_WRONLY | O_CREAT | O_NONBLOCK;
|
|
|
|
#ifdef O_CLOEXEC
|
|
|
|
opt |= O_CLOEXEC;
|
|
|
|
#endif
|
2015-02-20 16:28:04 +08:00
|
|
|
ctx.pid_fd = open(ctx.pidfile, opt, 0664);
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.pid_fd == -1)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "open `%s': %m", ctx.pidfile);
|
2012-01-30 01:28:05 +08:00
|
|
|
else {
|
2014-05-22 07:07:52 +08:00
|
|
|
#ifdef LOCK_EX
|
2012-01-30 01:28:05 +08:00
|
|
|
/* 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) {
|
2015-10-16 18:33:48 +08:00
|
|
|
logger(&ctx, LOG_ERR, "flock `%s': %m",
|
|
|
|
ctx.pidfile);
|
|
|
|
/* We don't want to unlink the pidfile as
|
|
|
|
* another dhcpcd instance could be using it. */
|
|
|
|
ctx.pidfile[0] = '\0';
|
2014-02-04 22:39:26 +08:00
|
|
|
goto exit_failure;
|
2012-01-30 01:28:05 +08:00
|
|
|
}
|
2014-05-22 07:07:52 +08:00
|
|
|
#endif
|
2014-04-18 21:17:42 +08:00
|
|
|
#ifndef O_CLOEXEC
|
|
|
|
if (fcntl(ctx.pid_fd, F_GETFD, &opt) == -1 ||
|
|
|
|
fcntl(ctx.pid_fd, F_SETFD, opt | FD_CLOEXEC) == -1)
|
|
|
|
{
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "fcntl: %m");
|
2015-10-16 18:33:48 +08:00
|
|
|
/* We don't want to unlink the pidfile as
|
|
|
|
* another dhcpcd instance could be using it. */
|
|
|
|
ctx.pidfile[0] = '\0';
|
2014-04-18 21:17:42 +08:00
|
|
|
goto exit_failure;
|
|
|
|
}
|
|
|
|
#endif
|
2014-02-12 08:39:46 +08:00
|
|
|
write_pid(ctx.pid_fd, getpid());
|
2007-07-12 20:52:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-07 22:41:18 +08:00
|
|
|
if (ctx.options & DHCPCD_MASTER) {
|
2014-02-22 00:10:55 +08:00
|
|
|
if (control_start(&ctx, NULL) == -1)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "control_start: %m");
|
2014-02-22 00:10:55 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (control_start(&ctx,
|
|
|
|
ctx.options & DHCPCD_MASTER ? NULL : argv[optind]) == -1)
|
|
|
|
{
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, 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
|
|
|
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_DEBUG, PACKAGE "-" VERSION " starting");
|
2014-02-22 00:10:55 +08:00
|
|
|
ctx.options |= DHCPCD_STARTED;
|
|
|
|
#ifdef USE_SIGNALS
|
2015-05-13 07:47:25 +08:00
|
|
|
if (eloop_signal_set_cb(ctx.eloop,
|
|
|
|
dhcpcd_signals, dhcpcd_signals_len,
|
2015-05-12 22:14:32 +08:00
|
|
|
signal_cb, &ctx) == -1)
|
|
|
|
{
|
2016-01-06 18:21:22 +08:00
|
|
|
logger(&ctx, LOG_ERR, "eloop_signal_set_cb: %m");
|
2015-05-12 22:14:32 +08:00
|
|
|
goto exit_failure;
|
|
|
|
}
|
2013-02-19 04:56:55 +08:00
|
|
|
/* Save signal mask, block and redirect signals to our handler */
|
2015-05-12 22:14:32 +08:00
|
|
|
if (eloop_signal_mask(ctx.eloop, &ctx.sigset) == -1) {
|
|
|
|
logger(&ctx, LOG_ERR, "eloop_signal_mask: %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
|
|
|
|
|
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
|
|
|
|
2015-10-27 20:46:48 +08:00
|
|
|
/* Start handling kernel messages for interfaces, addreses and
|
|
|
|
* routes. */
|
2015-08-06 21:07:15 +08:00
|
|
|
eloop_event_add(ctx.eloop, ctx.link_fd, handle_link, &ctx, NULL, NULL);
|
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-04-25 18:42:37 +08:00
|
|
|
ctx.ifaces = if_discover(&ctx, ctx.ifc, ctx.ifv);
|
2014-12-20 22:09:26 +08:00
|
|
|
if (ctx.ifaces == NULL) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "if_discover: %m");
|
2014-12-20 22:09:26 +08:00
|
|
|
goto exit_failure;
|
|
|
|
}
|
2014-02-12 08:39:46 +08:00
|
|
|
for (i = 0; i < ctx.ifc; i++) {
|
2015-12-17 10:34:36 +08:00
|
|
|
if ((ifp = if_find(ctx.ifaces, ctx.ifv[i])) == NULL ||
|
|
|
|
!ifp->active)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, 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
|
|
|
}
|
2015-12-17 10:34:36 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx.ifaces, next) {
|
2016-02-04 18:43:16 +08:00
|
|
|
if (ifp->active == IF_ACTIVE_USER)
|
2015-12-17 10:34:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ifp == NULL) {
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.ifc == 0)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, 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)) {
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR,
|
2009-10-14 23:16:07 +08:00
|
|
|
"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-06-05 21:03:49 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx.ifaces, next) {
|
2015-12-17 10:34:36 +08:00
|
|
|
if (ifp->active)
|
|
|
|
dhcpcd_initstate1(ifp, argc, argv, 0);
|
2014-06-05 21:03:49 +08:00
|
|
|
}
|
|
|
|
|
2014-04-28 20:02:12 +08:00
|
|
|
if (ctx.options & DHCPCD_BACKGROUND && dhcpcd_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) {
|
2015-12-17 10:34:36 +08:00
|
|
|
if (ifp->active) {
|
|
|
|
run_preinit(ifp);
|
|
|
|
if (!(ifp->options->options & DHCPCD_LINK) ||
|
|
|
|
ifp->carrier != LINK_DOWN)
|
|
|
|
opt = 1;
|
|
|
|
}
|
2009-10-14 23:16:07 +08:00
|
|
|
}
|
2009-11-20 23:14:27 +08:00
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
if (!(ctx.options & DHCPCD_BACKGROUND)) {
|
|
|
|
if (ctx.options & DHCPCD_MASTER)
|
2014-03-28 06:14:52 +08:00
|
|
|
t = ifo->timeout;
|
2015-12-17 10:34:36 +08:00
|
|
|
else {
|
2014-03-28 06:14:52 +08:00
|
|
|
t = 0;
|
2015-12-17 10:34:36 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx.ifaces, next) {
|
|
|
|
if (ifp->active) {
|
|
|
|
t = ifp->options->timeout;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_WARNING,
|
|
|
|
"no interfaces have a carrier");
|
2014-04-28 20:02:12 +08:00
|
|
|
if (dhcpcd_daemonise(&ctx))
|
2014-02-07 17:33:32 +08:00
|
|
|
goto exit_success;
|
2015-02-03 17:00:36 +08:00
|
|
|
} else if (t > 0 &&
|
2015-02-03 17:37:21 +08:00
|
|
|
/* Test mode removes the daemonise bit, so check for both */
|
2015-02-03 17:00:36 +08:00
|
|
|
ctx.options & (DHCPCD_DAEMONISE | DHCPCD_TEST))
|
|
|
|
{
|
2014-03-28 06:14:52 +08:00
|
|
|
eloop_timeout_add_sec(ctx.eloop, t,
|
2014-02-12 08:39:46 +08:00
|
|
|
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
|
|
|
|
2015-03-07 20:08:32 +08:00
|
|
|
if_sortinterfaces(&ctx);
|
2014-02-12 08:39:46 +08:00
|
|
|
TAILQ_FOREACH(ifp, ctx.ifaces, next) {
|
2015-12-17 10:34:36 +08:00
|
|
|
if (ifp->active)
|
|
|
|
eloop_timeout_add_sec(ctx.eloop, 0,
|
|
|
|
dhcpcd_prestartinterface, ifp);
|
2013-02-19 21:37:42 +08:00
|
|
|
}
|
2009-02-20 05:15:25 +08:00
|
|
|
|
2015-05-09 18:09:34 +08:00
|
|
|
i = eloop_start(ctx.eloop, &ctx.sigset);
|
2015-05-13 01:24:29 +08:00
|
|
|
if (i < 0) {
|
|
|
|
syslog(LOG_ERR, "eloop_start: %m");
|
|
|
|
goto exit_failure;
|
|
|
|
}
|
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-04-25 18:42:37 +08:00
|
|
|
if_free(ifp);
|
2014-02-04 22:39:26 +08:00
|
|
|
}
|
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) {
|
2015-05-14 15:35:49 +08:00
|
|
|
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
|
|
|
}
|
2015-08-06 21:07:15 +08:00
|
|
|
if (ctx.pf_inet_fd != -1)
|
|
|
|
close(ctx.pf_inet_fd);
|
|
|
|
#if defined(INET6) && defined(BSD)
|
|
|
|
if (ctx.pf_inet6_fd != -1)
|
|
|
|
close(ctx.pf_inet6_fd);
|
|
|
|
#endif
|
|
|
|
#ifdef IFLR_ACTIVE
|
|
|
|
if (ctx.pf_link_fd != -1)
|
|
|
|
close(ctx.pf_link_fd);
|
|
|
|
#endif
|
|
|
|
|
2014-02-12 08:39:46 +08:00
|
|
|
free_options(ifo);
|
|
|
|
free_globals(&ctx);
|
|
|
|
ipv4_ctxfree(&ctx);
|
|
|
|
ipv6_ctxfree(&ctx);
|
2014-10-04 16:26:15 +08:00
|
|
|
dev_stop(&ctx);
|
2014-10-04 16:22:09 +08:00
|
|
|
if (control_stop(&ctx) == -1)
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_ERR, "control_stop: %m:");
|
2014-02-12 08:39:46 +08:00
|
|
|
if (ctx.pid_fd != -1) {
|
|
|
|
close(ctx.pid_fd);
|
2015-10-16 18:33:48 +08:00
|
|
|
if (ctx.pidfile[0] != '\0')
|
|
|
|
unlink(ctx.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))
|
2015-03-18 07:46:38 +08:00
|
|
|
logger(&ctx, LOG_INFO, PACKAGE " exited");
|
|
|
|
logger_close(&ctx);
|
|
|
|
free(ctx.logfile);
|
2014-02-04 22:39:26 +08:00
|
|
|
return i;
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|