2007-11-15 19:35:53 +08:00
|
|
|
/*
|
2007-11-08 00:26:41 +08:00
|
|
|
* dhcpcd - DHCP client daemon
|
2008-01-08 17:51:23 +08:00
|
|
|
* Copyright 2006-2008 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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2008-04-13 07:00:23 +08:00
|
|
|
#include <sys/wait.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
#include <netinet/in.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
2008-02-03 05:56:12 +08:00
|
|
|
#include <signal.h>
|
2006-11-28 04:23:22 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2007-05-10 17:39:26 +08:00
|
|
|
#include "config.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
#include "common.h"
|
2006-12-15 07:17:27 +08:00
|
|
|
#include "configure.h"
|
2006-11-28 04:23:22 +08:00
|
|
|
#include "dhcp.h"
|
|
|
|
#include "dhcpcd.h"
|
|
|
|
#include "logger.h"
|
2008-04-12 00:14:55 +08:00
|
|
|
#include "net.h"
|
2008-02-03 03:17:08 +08:00
|
|
|
#include "signal.h"
|
2008-02-03 01:28:22 +08:00
|
|
|
|
2008-04-13 19:27:41 +08:00
|
|
|
static int
|
|
|
|
exec_script(const char *cmd, const char *arg1, const char *arg2)
|
2007-01-17 01:54:07 +08:00
|
|
|
{
|
2008-04-13 19:27:41 +08:00
|
|
|
char *const argv[4] = { (char *)cmd, (char *)arg1, (char *)arg2, NULL};
|
2008-02-20 01:43:39 +08:00
|
|
|
int ret = 0;
|
2008-02-03 02:08:49 +08:00
|
|
|
pid_t pid;
|
2008-04-13 07:00:23 +08:00
|
|
|
pid_t wpid;
|
|
|
|
int status = 0;
|
2008-02-03 03:17:08 +08:00
|
|
|
sigset_t full;
|
|
|
|
sigset_t old;
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-13 19:27:41 +08:00
|
|
|
logger(LOG_DEBUG, "exec `%s' `%s' `%s'", cmd, arg1, arg2);
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-02-03 03:17:08 +08:00
|
|
|
/* OK, we need to block signals */
|
2008-03-21 00:47:51 +08:00
|
|
|
sigfillset(&full);
|
|
|
|
sigprocmask(SIG_SETMASK, &full, &old);
|
2008-02-03 03:17:08 +08:00
|
|
|
|
2008-02-03 05:56:12 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
2008-03-21 00:47:51 +08:00
|
|
|
signal_reset();
|
|
|
|
pid = vfork();
|
2008-02-03 05:56:12 +08:00
|
|
|
#else
|
|
|
|
pid = fork();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (pid) {
|
2008-03-21 00:47:51 +08:00
|
|
|
case -1:
|
2008-04-13 19:27:41 +08:00
|
|
|
logger(LOG_ERR, "fork: %s", strerror(errno));
|
2008-03-21 00:47:51 +08:00
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
case 0:
|
2008-02-03 05:56:12 +08:00
|
|
|
#ifndef THERE_IS_NO_FORK
|
2008-03-21 00:47:51 +08:00
|
|
|
signal_reset();
|
|
|
|
#endif
|
2008-04-13 19:27:41 +08:00
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
|
|
|
execvp(cmd, argv);
|
|
|
|
logger(LOG_ERR, "%s: %s", cmd, strerror(errno));
|
2008-03-21 00:47:51 +08:00
|
|
|
_exit(111);
|
|
|
|
/* NOTREACHED */
|
2008-02-03 02:08:49 +08:00
|
|
|
}
|
2008-02-03 03:17:08 +08:00
|
|
|
|
2008-02-03 05:56:12 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
2008-03-21 00:47:51 +08:00
|
|
|
signal_setup();
|
2008-02-03 05:56:12 +08:00
|
|
|
#endif
|
|
|
|
|
2008-02-03 03:17:08 +08:00
|
|
|
/* Restore our signals */
|
2008-03-21 00:47:51 +08:00
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
2008-04-13 07:00:23 +08:00
|
|
|
|
|
|
|
/* Wait for the script to finish */
|
|
|
|
do {
|
|
|
|
wpid = waitpid(pid, &status, 0);
|
|
|
|
if (wpid < 1)
|
|
|
|
logger(LOG_ERR, "waitpid: %s", strerror(errno));
|
|
|
|
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
|
|
|
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
ret = WEXITSTATUS(status);
|
|
|
|
else
|
|
|
|
ret = -1;
|
2008-03-21 00:47:51 +08:00
|
|
|
return ret;
|
2007-01-17 01:54:07 +08:00
|
|
|
}
|
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
static struct rt *
|
|
|
|
reverse_routes(struct rt *routes)
|
|
|
|
{
|
|
|
|
struct rt *rt;
|
|
|
|
struct rt *rtn = NULL;
|
|
|
|
|
|
|
|
while (routes) {
|
|
|
|
rt = routes->next;
|
|
|
|
routes->next = rtn;
|
|
|
|
rtn = routes;
|
|
|
|
routes = rt;
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
static int
|
|
|
|
delete_route(const char *iface, struct rt *rt, int metric)
|
|
|
|
{
|
|
|
|
char *addr;
|
|
|
|
int retval;
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
addr = xstrdup(inet_ntoa(rt->dest));
|
|
|
|
logger(LOG_DEBUG, "removing route %s/%d via %s",
|
|
|
|
addr, inet_ntocidr(rt->net), inet_ntoa(rt->gate));
|
|
|
|
free(addr);
|
|
|
|
retval = del_route(iface, &rt->dest, &rt->net, &rt->gate, metric);
|
|
|
|
if (retval != 0)
|
|
|
|
logger(LOG_ERR," del_route: %s", strerror(errno));
|
|
|
|
return retval;
|
2007-05-14 01:43:56 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
static int
|
|
|
|
delete_routes(struct interface *iface, int metric)
|
|
|
|
{
|
|
|
|
struct rt *rt;
|
|
|
|
struct rt *rtn;
|
|
|
|
int retval = 0;
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
rt = reverse_routes(iface->routes);
|
|
|
|
while (rt) {
|
|
|
|
rtn = rt->next;
|
|
|
|
retval += delete_route(iface->name, rt, metric);
|
|
|
|
free(rt);
|
|
|
|
rt = rtn;
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
iface->routes = NULL;
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
return retval;
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
static int
|
|
|
|
in_routes(const struct rt *routes, const struct rt *rt)
|
|
|
|
{
|
|
|
|
while (routes) {
|
|
|
|
if (routes->dest.s_addr == rt->dest.s_addr &&
|
|
|
|
routes->net.s_addr == rt->net.s_addr &&
|
|
|
|
routes->gate.s_addr == rt->gate.s_addr)
|
|
|
|
return 0;
|
|
|
|
routes = routes->next;
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
static int
|
|
|
|
configure_routes(struct interface *iface, const struct dhcp_message *dhcp,
|
|
|
|
const struct options *options)
|
|
|
|
{
|
|
|
|
struct rt *rt, *ort;
|
|
|
|
struct rt *rtn = NULL, *nr = NULL;
|
|
|
|
int remember;
|
|
|
|
int retval = 0;
|
|
|
|
char *addr;
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2007-10-04 17:57:50 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
2008-04-12 00:14:55 +08:00
|
|
|
char *skipp;
|
|
|
|
size_t skiplen;
|
|
|
|
int skip = 0;
|
|
|
|
|
2008-03-21 00:47:51 +08:00
|
|
|
free(dhcpcd_skiproutes);
|
2008-01-08 04:52:49 +08:00
|
|
|
/* We can never have more than 255 routes. So we need space
|
|
|
|
* for 255 3 digit numbers and commas */
|
|
|
|
skiplen = 255 * 4 + 1;
|
2008-03-21 00:47:51 +08:00
|
|
|
skipp = dhcpcd_skiproutes = xmalloc(sizeof(char) * skiplen);
|
2008-01-08 04:52:49 +08:00
|
|
|
*skipp = '\0';
|
2007-10-04 17:57:50 +08:00
|
|
|
#endif
|
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
ort = get_option_routes(dhcp);
|
|
|
|
|
2007-10-12 19:19:29 +08:00
|
|
|
#ifdef ENABLE_IPV4LL
|
2008-04-12 00:14:55 +08:00
|
|
|
if (options->options & DHCPCD_IPV4LL &&
|
|
|
|
IN_PRIVATE(ntohl(dhcp->yiaddr)))
|
|
|
|
{
|
|
|
|
for (rt = ort; rt; rt = rt->next) {
|
|
|
|
/* Check if we have already got a link locale route dished
|
|
|
|
* out by the DHCP server */
|
|
|
|
if (rt->dest.s_addr == htonl(LINKLOCAL_ADDR) &&
|
|
|
|
rt->net.s_addr == htonl(LINKLOCAL_MASK))
|
|
|
|
break;
|
|
|
|
rtn = rt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rt) {
|
|
|
|
rt = xmalloc(sizeof(*rt));
|
|
|
|
rt->dest.s_addr = htonl(LINKLOCAL_ADDR);
|
|
|
|
rt->net.s_addr = htonl(LINKLOCAL_MASK);
|
|
|
|
rt->gate.s_addr = 0;
|
|
|
|
rt->next = NULL;
|
|
|
|
if (rtn)
|
|
|
|
rtn->next = rt;
|
|
|
|
else
|
|
|
|
ort = rt;
|
|
|
|
}
|
|
|
|
}
|
2007-10-12 19:19:29 +08:00
|
|
|
#endif
|
2008-04-12 00:14:55 +08:00
|
|
|
|
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
if (dhcpcd_skiproutes) {
|
|
|
|
int i = -1;
|
|
|
|
char *sk, *skp, *token;
|
|
|
|
free_routes(iface->routes);
|
|
|
|
for (rt = ort; rt; rt = rt->next) {
|
|
|
|
i++;
|
|
|
|
/* Check that we did add this route or not */
|
|
|
|
sk = skp = xstrdup(dhcpcd_skiproutes);
|
|
|
|
while ((token = strsep(&skp, ","))) {
|
|
|
|
if (isdigit(*token) && atoi(token) == i)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free(sk);
|
|
|
|
if (token)
|
|
|
|
continue;
|
|
|
|
if (nr) {
|
|
|
|
rtn->next = xmalloc(sizeof(*rtn));
|
|
|
|
rtn = rtn->next;
|
|
|
|
} else {
|
|
|
|
nr = rtn = xmalloc(sizeof(*rtn));
|
|
|
|
}
|
|
|
|
rtn->dest.s_addr = rt->dest.s_addr;
|
|
|
|
rtn->net.s_addr = rt->net.s_addr;
|
|
|
|
rtn->gate.s_addr = rt->gate.s_addr;
|
|
|
|
rtn->next = NULL;
|
|
|
|
}
|
|
|
|
iface->routes = nr;
|
|
|
|
nr = NULL;
|
|
|
|
|
|
|
|
/* We no longer need this */
|
|
|
|
free(dhcpcd_skiproutes);
|
|
|
|
dhcpcd_skiproutes = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now remove old routes we no longer use.
|
|
|
|
* We should do this in reverse order. */
|
|
|
|
iface->routes = reverse_routes(iface->routes);
|
|
|
|
for (rt = iface->routes; rt; rt = rt->next)
|
|
|
|
if (in_routes(ort, rt) != 0)
|
|
|
|
delete_route(iface->name, rt, options->metric);
|
|
|
|
|
|
|
|
for (rt = ort; rt; rt = rt->next) {
|
2008-01-17 00:38:47 +08:00
|
|
|
/* Don't set default routes if not asked to */
|
2008-04-12 00:14:55 +08:00
|
|
|
if (rt->dest.s_addr == 0 &&
|
|
|
|
rt->net.s_addr == 0 &&
|
2008-03-30 03:14:52 +08:00
|
|
|
!(options->options & DHCPCD_GATEWAY))
|
2008-01-17 00:38:47 +08:00
|
|
|
continue;
|
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
addr = xstrdup(inet_ntoa(rt->dest));
|
|
|
|
logger(LOG_DEBUG, "adding route to %s/%d via %s",
|
|
|
|
addr, inet_ntocidr(rt->net), inet_ntoa(rt->gate));
|
|
|
|
free(addr);
|
|
|
|
remember = add_route(iface->name, &rt->dest,
|
|
|
|
&rt->net, &rt->gate,
|
|
|
|
options->metric);
|
|
|
|
retval += remember;
|
|
|
|
|
2008-01-17 00:38:47 +08:00
|
|
|
/* If we failed to add the route, we may have already added it
|
|
|
|
ourselves. If so, remember it again. */
|
2008-04-12 00:14:55 +08:00
|
|
|
if (remember < 0) {
|
|
|
|
if (errno != EEXIST)
|
|
|
|
logger(LOG_ERR, "add_route: %s",
|
|
|
|
strerror(errno));
|
|
|
|
if (in_routes(iface->routes, rt) == 0)
|
|
|
|
remember = 1;
|
|
|
|
}
|
2008-01-17 00:38:47 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
/* This login is split from above due to the #ifdef below */
|
2008-01-17 00:38:47 +08:00
|
|
|
if (remember >= 0) {
|
2008-04-12 00:14:55 +08:00
|
|
|
if (nr) {
|
|
|
|
rtn->next = xmalloc(sizeof(*rtn));
|
|
|
|
rtn = rtn->next;
|
|
|
|
} else {
|
|
|
|
nr = rtn = xmalloc(sizeof(*rtn));
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
rtn->dest.s_addr = rt->dest.s_addr;
|
|
|
|
rtn->net.s_addr = rt->net.s_addr;
|
|
|
|
rtn->gate.s_addr = rt->gate.s_addr;
|
|
|
|
rtn->next = NULL;
|
2008-01-17 00:38:47 +08:00
|
|
|
}
|
2007-10-04 17:57:50 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
2008-01-17 00:38:47 +08:00
|
|
|
/* If we have daemonised yet we need to record which routes
|
|
|
|
* we failed to add so we can skip them */
|
2008-04-12 00:14:55 +08:00
|
|
|
else if (!(options->options & DHCPCD_DAEMONISED)) {
|
2008-01-17 00:38:47 +08:00
|
|
|
/* We can never have more than 255 / 4 routes,
|
|
|
|
* so 3 chars is plently */
|
|
|
|
if (*skipp)
|
|
|
|
*skipp++ = ',';
|
2008-03-21 00:47:51 +08:00
|
|
|
skipp += snprintf(skipp,
|
|
|
|
dhcpcd_skiproutes + skiplen - skipp,
|
|
|
|
"%d", skip);
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
2008-01-17 00:38:47 +08:00
|
|
|
skip++;
|
|
|
|
#endif
|
2008-01-08 04:52:49 +08:00
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
free_routes(ort);
|
|
|
|
free_routes(iface->routes);
|
|
|
|
iface->routes = nr;
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-01-08 04:52:49 +08:00
|
|
|
#ifdef THERE_IS_NO_FORK
|
|
|
|
if (*dhcpcd_skiproutes)
|
|
|
|
*skipp = '\0';
|
|
|
|
else {
|
2008-03-21 00:47:51 +08:00
|
|
|
free(dhcpcd_skiproutes);
|
2008-01-08 04:52:49 +08:00
|
|
|
dhcpcd_skiproutes = NULL;
|
2007-10-12 19:19:29 +08:00
|
|
|
}
|
2008-01-08 04:52:49 +08:00
|
|
|
#endif
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2007-10-12 19:19:29 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_clean(FILE *f, const char *name, const char *value)
|
|
|
|
{
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "%s=", name);
|
|
|
|
if (value)
|
|
|
|
write_string(f, (const uint8_t*)value, strlen(value));
|
|
|
|
fputc('\n', f);
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
write_info(const struct interface *iface, const struct dhcp_message *dhcp,
|
|
|
|
const struct dhcp_lease *lease, const struct options *options,
|
|
|
|
int overwrite)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
struct rt *rt, *ort;
|
|
|
|
struct stat sb;
|
|
|
|
struct in_addr addr;
|
|
|
|
int doneone;
|
|
|
|
|
|
|
|
if (options->options & DHCPCD_TEST)
|
|
|
|
f = stdout;
|
|
|
|
else {
|
|
|
|
if (!overwrite && stat(iface->infofile, &sb) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((f = fopen(iface->infofile, "w")) == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dhcp->yiaddr) {
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "IPADDR=%s\n", inet_ntoa(iface->addr));
|
|
|
|
fprintf(f, "NETMASK=%s\n", inet_ntoa(iface->net));
|
2008-04-12 00:14:55 +08:00
|
|
|
addr.s_addr = dhcp->yiaddr & iface->net.s_addr;
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "NETWORK=%s\n", inet_ntoa(addr));
|
2008-04-12 00:14:55 +08:00
|
|
|
if (get_option_addr(&addr.s_addr, dhcp, DHCP_BROADCAST) == -1)
|
|
|
|
addr.s_addr = dhcp->yiaddr | ~iface->net.s_addr;
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "BROADCAST=%s\n", inet_ntoa(addr));
|
2008-04-12 00:14:55 +08:00
|
|
|
|
|
|
|
ort = get_option_routes(dhcp);
|
|
|
|
doneone = 0;
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "ROUTES=");
|
2008-04-12 00:14:55 +08:00
|
|
|
for (rt = ort; rt; rt = rt->next) {
|
|
|
|
if (rt->dest.s_addr == 0)
|
|
|
|
continue;
|
|
|
|
if (doneone)
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "\\ ");
|
2008-04-14 18:02:44 +08:00
|
|
|
else
|
2008-04-12 00:14:55 +08:00
|
|
|
doneone = 1;
|
|
|
|
fprintf(f, "%s", inet_ntoa(rt->dest));
|
|
|
|
fprintf(f, ",%s", inet_ntoa(rt->net));
|
|
|
|
fprintf(f, ",%s", inet_ntoa(rt->gate));
|
2007-10-12 19:19:29 +08:00
|
|
|
}
|
2008-04-14 23:08:46 +08:00
|
|
|
fputc('\n', f);
|
2008-04-12 00:14:55 +08:00
|
|
|
|
|
|
|
doneone = 0;
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "GATEWAYS=");
|
2008-04-12 00:14:55 +08:00
|
|
|
for (rt = ort; rt; rt = rt->next) {
|
|
|
|
if (rt->dest.s_addr != 0)
|
|
|
|
continue;
|
|
|
|
if (doneone)
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "\\ ");
|
2008-04-14 18:02:44 +08:00
|
|
|
else
|
2008-04-12 00:14:55 +08:00
|
|
|
doneone = 1;
|
|
|
|
fprintf(f, "%s", inet_ntoa(rt->gate));
|
|
|
|
}
|
2008-04-14 23:08:46 +08:00
|
|
|
fputc('\n', f);
|
2008-04-12 00:14:55 +08:00
|
|
|
free_routes(ort);
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
2007-10-12 19:19:29 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
write_options(f, dhcp);
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
/* FIXME
|
|
|
|
if (dhcp->fqdn) {
|
|
|
|
fprintf(f, "FQDNFLAGS='%u'\n", dhcp->fqdn->flags);
|
|
|
|
fprintf(f, "FQDNRCODE1='%u'\n", dhcp->fqdn->r1);
|
|
|
|
fprintf(f, "FQDNRCODE2='%u'\n", dhcp->fqdn->r2);
|
|
|
|
print_clean(f, "FQDNHOSTNAME", dhcp->fqdn->name);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
if (dhcp->siaddr) {
|
|
|
|
addr.s_addr = dhcp->siaddr;
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "DHCPSID=%s\n", inet_ntoa(addr));
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
|
|
|
if (dhcp->servername[0])
|
|
|
|
print_clean(f, "DHCPSNAME", (const char *)dhcp->servername);
|
|
|
|
|
|
|
|
if (!(options->options & DHCPCD_INFORM) && dhcp->yiaddr) {
|
|
|
|
if (!(options->options & DHCPCD_TEST))
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "LEASEDFROM=%u\n", lease->leasedfrom);
|
|
|
|
fprintf(f, "LEASETIME=%u\n", lease->leasetime);
|
|
|
|
fprintf(f, "RENEWALTIME=%u\n", lease->renewaltime);
|
|
|
|
fprintf(f, "REBINDTIME=%u\n", lease->rebindtime);
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
|
|
|
print_clean(f, "INTERFACE", iface->name);
|
|
|
|
print_clean(f, "CLASSID", options->classid);
|
|
|
|
if (iface->clientid_len > 0) {
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "CLIENTID=%s\n",
|
2008-04-12 00:14:55 +08:00
|
|
|
hwaddr_ntoa(iface->clientid, iface->clientid_len));
|
|
|
|
}
|
2008-04-14 23:08:46 +08:00
|
|
|
fprintf(f, "DHCPCHADDR=%s\n",
|
2008-04-12 00:14:55 +08:00
|
|
|
hwaddr_ntoa(iface->hwaddr, iface->hwlen));
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
if (!(options->options & DHCPCD_TEST))
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
int
|
|
|
|
configure(struct interface *iface, const struct dhcp_message *dhcp,
|
|
|
|
const struct dhcp_lease *lease, const struct options *options,
|
|
|
|
int up)
|
|
|
|
{
|
|
|
|
struct in_addr addr;
|
|
|
|
struct in_addr net;
|
|
|
|
struct in_addr brd;
|
|
|
|
#ifdef __linux__
|
|
|
|
struct in_addr dest;
|
|
|
|
struct in_addr gate;
|
2007-04-03 15:03:04 +08:00
|
|
|
#endif
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
/* Grab our IP config */
|
|
|
|
if (dhcp == NULL || dhcp->yiaddr == 0)
|
|
|
|
up = 0;
|
|
|
|
else {
|
|
|
|
addr.s_addr = dhcp->yiaddr;
|
|
|
|
/* Ensure we have all the needed values */
|
|
|
|
if (get_option_addr(&net.s_addr, dhcp, DHCP_NETMASK) == -1)
|
|
|
|
net.s_addr = get_netmask(addr.s_addr);
|
|
|
|
if (get_option_addr(&brd.s_addr, dhcp, DHCP_BROADCAST) == -1)
|
|
|
|
brd.s_addr = addr.s_addr | ~net.s_addr;
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
/* If we aren't up, then reset the interface as much as we can */
|
|
|
|
if (!up) {
|
|
|
|
/* If we haven't created an info file, do so now */
|
|
|
|
if (!lease->frominfo) {
|
|
|
|
if (write_info(iface, dhcp, lease, options, 0) == -1)
|
|
|
|
logger(LOG_ERR, "write_info: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2008-01-17 00:38:47 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
/* Only reset things if we had set them before */
|
|
|
|
if (iface->addr.s_addr != 0) {
|
|
|
|
if (!(options->options & DHCPCD_KEEPADDRESS)) {
|
|
|
|
delete_routes(iface, options->metric);
|
|
|
|
logger(LOG_DEBUG, "deleting IP address %s/%d",
|
|
|
|
inet_ntoa(iface->addr),
|
|
|
|
inet_ntocidr(iface->net));
|
|
|
|
if (del_address(iface->name, &iface->addr,
|
|
|
|
&iface->net) == -1 &&
|
|
|
|
errno != ENOENT)
|
|
|
|
logger(LOG_ERR, "del_address: %s",
|
|
|
|
strerror(errno));
|
|
|
|
iface->addr.s_addr = 0;
|
|
|
|
iface->net.s_addr = 0;
|
|
|
|
}
|
|
|
|
}
|
2007-04-11 21:18:33 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
exec_script(options->script, iface->infofile, "down");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This also changes netmask */
|
|
|
|
if (!(options->options & DHCPCD_INFORM) ||
|
|
|
|
!has_address(iface->name, &addr, &net)) {
|
|
|
|
logger(LOG_DEBUG, "adding IP address %s/%d",
|
|
|
|
inet_ntoa(addr), inet_ntocidr(net));
|
|
|
|
if (add_address(iface->name, &addr, &net, &brd) == -1 &&
|
|
|
|
errno != EEXIST)
|
|
|
|
{
|
|
|
|
logger(LOG_ERR, "add_address: %s", strerror(errno));
|
|
|
|
return -1;
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now delete the old address if different */
|
|
|
|
if (iface->addr.s_addr != addr.s_addr &&
|
|
|
|
iface->addr.s_addr != 0 &&
|
|
|
|
!(options->options & DHCPCD_KEEPADDRESS))
|
|
|
|
del_address(iface->name, &iface->addr, &iface->net);
|
2008-01-17 00:38:47 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
#ifdef __linux__
|
|
|
|
/* On linux, we need to change the subnet route to have our metric. */
|
|
|
|
if (iface->addr.s_addr != lease->addr.s_addr &&
|
|
|
|
options->metric > 0 && net.s_addr != INADDR_BROADCAST)
|
|
|
|
{
|
|
|
|
dest.s_addr = addr.s_addr & net.s_addr;
|
|
|
|
gate.s_addr = 0;
|
|
|
|
add_route(iface->name, &dest, &net, &gate, options->metric);
|
|
|
|
del_route(iface->name, &dest, &net, &gate, 0);
|
2007-04-11 21:18:33 +08:00
|
|
|
}
|
2008-04-12 00:14:55 +08:00
|
|
|
#endif
|
2008-01-17 00:38:47 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
configure_routes(iface, dhcp, options);
|
|
|
|
up = (iface->addr.s_addr != addr.s_addr ||
|
|
|
|
iface->net.s_addr != net.s_addr);
|
|
|
|
iface->addr.s_addr = addr.s_addr;
|
|
|
|
iface->net.s_addr = net.s_addr;
|
2006-11-28 04:23:22 +08:00
|
|
|
|
2008-04-13 07:00:23 +08:00
|
|
|
if (!lease->frominfo)
|
2008-04-12 00:14:55 +08:00
|
|
|
write_info(iface, dhcp, lease, options, 1);
|
|
|
|
if (write_lease(iface, dhcp) == -1)
|
|
|
|
logger(LOG_ERR, "write_lease: %s", strerror(errno));
|
2008-04-13 07:00:23 +08:00
|
|
|
|
2008-04-12 00:14:55 +08:00
|
|
|
exec_script(options->script, iface->infofile, up ? "new" : "up");
|
2008-03-21 00:47:51 +08:00
|
|
|
return 0;
|
2006-11-28 04:23:22 +08:00
|
|
|
}
|