dhcp: convert dhcp backends to classes

This commit is contained in:
Dan Williams 2010-01-12 22:09:28 -08:00
parent d997785db3
commit 1806235049
13 changed files with 1709 additions and 1210 deletions

View file

@ -46,6 +46,7 @@ libtest_dhcp_la_CPPFLAGS = \
$(LIBNL_CFLAGS)
libtest_dhcp_la_LIBADD = \
$(top_builddir)/marshallers/libmarshallers.la \
$(top_builddir)/libnm-util/libnm-util.la \
$(GLIB_LIBS) \
$(DBUS_LIBS) \

View file

@ -9,9 +9,14 @@ INCLUDES = \
noinst_LTLIBRARIES = libdhcp-manager.la
libdhcp_manager_la_SOURCES = \
nm-dhcp-client.c \
nm-dhcp-client.h \
nm-dhcp-manager.c \
nm-dhcp-manager.h \
nm-dhcp-@DHCP_CLIENT@.c
nm-dhcp-dhclient.h \
nm-dhcp-dhclient.c \
nm-dhcp-dhcpcd.h \
nm-dhcp-dhcpcd.c
libdhcp_manager_la_CPPFLAGS = \
$(DBUS_CFLAGS) \
@ -29,6 +34,3 @@ libdhcp_manager_la_LIBADD = \
$(DBUS_LIBS) \
$(GLIB_LIBS)
EXTRA_DIST = \
nm-dhcp-dhclient.c \
nm-dhcp-dhcpcd.c

View file

@ -0,0 +1,900 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2005 - 2010 Red Hat, Inc.
*
*/
#include <glib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include "nm-utils.h"
#include "nm-dbus-glib-types.h"
#include "nm-dhcp-client.h"
#define NM_DHCP_TIMEOUT 45 /* DHCP timeout, in seconds */
typedef struct {
char * iface;
guchar state;
GPid pid;
guint timeout_id;
guint watch_id;
GHashTable * options;
} NMDHCPClientPrivate;
#define NM_DHCP_CLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_CLIENT, NMDHCPClientPrivate))
G_DEFINE_TYPE_EXTENDED (NMDHCPClient, nm_dhcp_client, G_TYPE_OBJECT, G_TYPE_FLAG_ABSTRACT, {})
enum {
STATE_CHANGED,
TIMEOUT,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_IFACE,
LAST_PROP
};
/********************************************/
GPid
nm_dhcp_client_get_pid (NMDHCPClient *self)
{
g_return_val_if_fail (self != NULL, -1);
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), -1);
return NM_DHCP_CLIENT_GET_PRIVATE (self)->pid;
}
const char *
nm_dhcp_client_get_iface (NMDHCPClient *self)
{
g_return_val_if_fail (self != NULL, NULL);
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
return NM_DHCP_CLIENT_GET_PRIVATE (self)->iface;
}
/********************************************/
static void
timeout_cleanup (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (priv->timeout_id) {
g_source_remove (priv->timeout_id);
priv->timeout_id = 0;
}
}
static void
watch_cleanup (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (priv->watch_id) {
g_source_remove (priv->watch_id);
priv->watch_id = 0;
}
}
static void
stop_process (GPid pid, const char *iface)
{
int i = 15; /* 3 seconds */
g_return_if_fail (pid > 0);
/* Tell it to quit; maybe it wants to send out a RELEASE message */
kill (pid, SIGTERM);
while (i-- > 0) {
gint child_status;
int ret;
ret = waitpid (pid, &child_status, WNOHANG);
if (ret > 0)
break;
if (ret == -1) {
/* Child already exited */
if (errno == ECHILD)
break;
/* Took too long; shoot it in the head */
i = 0;
break;
}
g_usleep (G_USEC_PER_SEC / 5);
}
if (i <= 0) {
if (iface) {
g_warning ("%s: dhcp client pid %d didn't exit, will kill it.",
iface, pid);
}
kill (pid, SIGKILL);
g_warning ("waiting for dhcp client pid %d to exit", pid);
waitpid (pid, NULL, 0);
g_warning ("dhcp client pid %d cleaned up", pid);
}
}
static void
real_stop (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv;
g_return_if_fail (self != NULL);
g_return_if_fail (NM_IS_DHCP_CLIENT (self));
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_if_fail (priv->pid > 0);
/* Clean up the watch handler since we're explicitly killing the daemon */
watch_cleanup (self);
stop_process (priv->pid, priv->iface);
}
static gboolean
daemon_timeout (gpointer user_data)
{
NMDHCPClient *self = NM_DHCP_CLIENT (user_data);
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_message ("(%s): DHCP transaction took too long, stopping it.", priv->iface);
g_signal_emit (G_OBJECT (self), signals[TIMEOUT], 0);
return FALSE;
}
static void
daemon_watch_cb (GPid pid, gint status, gpointer user_data)
{
NMDHCPClient *self = NM_DHCP_CLIENT (user_data);
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (!WIFEXITED (status)) {
priv->state = DHC_ABEND;
g_warning ("dhcp client died abnormally");
}
priv->pid = 0;
watch_cleanup (self);
timeout_cleanup (self);
g_signal_emit (G_OBJECT (self), signals[STATE_CHANGED], 0, priv->state);
}
gboolean
nm_dhcp_client_start (NMDHCPClient *self,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint32 timeout_secs,
guint8 *dhcp_anycast_addr)
{
NMDHCPClientPrivate *priv;
g_return_val_if_fail (self != NULL, FALSE);
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_val_if_fail (priv->pid == -1, FALSE);
if (timeout_secs == 0)
timeout_secs = NM_DHCP_TIMEOUT;
g_message ("Activation (%s) Beginning DHCP transaction (timeout in %d seconds)",
priv->iface, timeout_secs);
priv->pid = NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self,
uuid,
s_ip4,
dhcp_anycast_addr);
if (priv->pid <= 0)
return FALSE;
/* Set up a timeout on the transaction to kill it after the timeout */
priv->timeout_id = g_timeout_add_seconds (timeout_secs,
daemon_timeout,
self);
priv->watch_id = g_child_watch_add (priv->pid,
(GChildWatchFunc) daemon_watch_cb,
self);
return TRUE;
}
void
nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name)
{
char *pid_contents = NULL, *proc_contents = NULL, *proc_path = NULL;
long int tmp;
/* Check for an existing instance and stop it */
if (!g_file_get_contents (pid_file, &pid_contents, NULL, NULL))
return;
errno = 0;
tmp = strtol (pid_contents, NULL, 10);
if ((errno == 0) && (tmp > 1)) {
const char *exe;
/* Ensure the process is a DHCP client */
proc_path = g_strdup_printf ("/proc/%ld/cmdline", tmp);
if (g_file_get_contents (proc_path, &proc_contents, NULL, NULL)) {
exe = strrchr (proc_contents, '/');
if (exe)
exe++;
else
exe = proc_contents;
if (!strcmp (exe, binary_name))
stop_process ((GPid) tmp, NULL);
}
}
remove (pid_file);
g_free (proc_path);
g_free (pid_contents);
g_free (proc_contents);
}
void
nm_dhcp_client_stop (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv;
g_return_if_fail (self != NULL);
g_return_if_fail (NM_IS_DHCP_CLIENT (self));
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
/* Kill the DHCP client */
if (priv->pid > 0) {
NM_DHCP_CLIENT_GET_CLASS (self)->stop (self);
g_message ("(%s): canceled DHCP transaction, dhcp client pid %d",
priv->iface,
priv->pid);
}
/* And clean stuff up */
priv->pid = -1;
priv->state = DHC_END;
g_hash_table_remove_all (priv->options);
timeout_cleanup (self);
watch_cleanup (self);
}
/********************************************/
static gboolean
state_is_bound (guint32 state)
{
if ( (state == DHC_BOUND4)
|| (state == DHC_BOUND6)
|| (state == DHC_RENEW4)
|| (state == DHC_RENEW6)
|| (state == DHC_REBOOT)
|| (state == DHC_REBIND4)
|| (state == DHC_REBIND6)
|| (state == DHC_IPV4LL))
return TRUE;
return FALSE;
}
typedef struct {
NMDHCPState state;
const char *name;
} DhcState;
#define STATE_TABLE_SIZE (sizeof (state_table) / sizeof (state_table[0]))
static DhcState state_table[] = {
{ DHC_NBI, "nbi" },
{ DHC_PREINIT, "preinit" },
{ DHC_BOUND4, "bound" },
{ DHC_BOUND6, "bound6" },
{ DHC_IPV4LL, "ipv4ll" },
{ DHC_RENEW4, "renew" },
{ DHC_RENEW6, "renew6" },
{ DHC_REBOOT, "reboot" },
{ DHC_REBIND4, "rebind" },
{ DHC_REBIND6, "rebind6" },
{ DHC_STOP, "stop" },
{ DHC_MEDIUM, "medium" },
{ DHC_TIMEOUT, "timeout" },
{ DHC_FAIL, "fail" },
{ DHC_EXPIRE, "expire" },
{ DHC_RELEASE, "release" },
{ DHC_START, "start" },
{ DHC_ABEND, "abend" },
{ DHC_END, "end" },
{ DHC_DEPREF6, "depref6" },
};
static inline const char *
state_to_string (guint32 state)
{
int i;
for (i = 0; i < STATE_TABLE_SIZE; i++) {
if (state == state_table[i].state)
return state_table[i].name;
}
return NULL;
}
static inline NMDHCPState
string_to_state (const char *name)
{
int i;
for (i = 0; i < STATE_TABLE_SIZE; i++) {
if (!strcasecmp (name, state_table[i].name))
return state_table[i].state;
}
return 255;
}
static char *
garray_to_string (GArray *array, const char *key)
{
GString *str;
int i;
unsigned char c;
char *converted = NULL;
g_return_val_if_fail (array != NULL, NULL);
/* Since the DHCP options come through environment variables, they should
* already be UTF-8 safe, but just make sure.
*/
str = g_string_sized_new (array->len);
for (i = 0; i < array->len; i++) {
c = array->data[i];
/* Convert NULLs to spaces and non-ASCII characters to ? */
if (c == '\0')
c = ' ';
else if (c > 127)
c = '?';
str = g_string_append_c (str, c);
}
str = g_string_append_c (str, '\0');
converted = str->str;
if (!g_utf8_validate (converted, -1, NULL))
g_warning ("%s: DHCP option '%s' couldn't be converted to UTF-8", __func__, key);
g_string_free (str, FALSE);
return converted;
}
static void
copy_option (gpointer key,
gpointer value,
gpointer user_data)
{
GHashTable *hash = user_data;
const char *str_key = (const char *) key;
char *str_value = NULL;
if (G_VALUE_TYPE (value) != DBUS_TYPE_G_UCHAR_ARRAY) {
g_warning ("Unexpected key %s value type was not "
"DBUS_TYPE_G_UCHAR_ARRAY",
str_key);
return;
}
str_value = garray_to_string ((GArray *) g_value_get_boxed (value), str_key);
if (str_value)
g_hash_table_insert (hash, g_strdup (str_key), str_value);
}
void
nm_dhcp_client_new_options (NMDHCPClient *self,
GHashTable *options,
const char *reason)
{
NMDHCPClientPrivate *priv;
guint32 old_state;
guint32 new_state;
g_return_if_fail (self != NULL);
g_return_if_fail (NM_IS_DHCP_CLIENT (self));
g_return_if_fail (options != NULL);
g_return_if_fail (reason != NULL);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
old_state = priv->state;
new_state = string_to_state (reason);
/* Clear old and save new DHCP options */
g_hash_table_remove_all (priv->options);
g_hash_table_foreach (options, copy_option, priv->options);
if (old_state == new_state)
return;
/* Handle changed device state */
if (state_is_bound (new_state)) {
/* Cancel the timeout if the DHCP client is now bound */
timeout_cleanup (self);
}
priv->state = new_state;
g_message ("DHCP: device %s state changed %s -> %s",
priv->iface,
state_to_string (old_state),
state_to_string (priv->state));
g_signal_emit (G_OBJECT (self),
signals[STATE_CHANGED],
0,
priv->state);
}
#define NEW_TAG "new_"
#define OLD_TAG "old_"
typedef struct {
GHFunc func;
gpointer user_data;
} Dhcp4ForeachInfo;
static void
iterate_dhcp4_config_option (gpointer key,
gpointer value,
gpointer user_data)
{
Dhcp4ForeachInfo *info = (Dhcp4ForeachInfo *) user_data;
char *tmp_key = NULL;
const char **p;
static const char *filter_options[] = {
"interface", "pid", "reason", "dhcp_message_type", NULL
};
/* Filter out stuff that's not actually new DHCP options */
for (p = filter_options; *p; p++) {
if (!strcmp (*p, (const char *) key))
return;
if (!strncmp ((const char *) key, OLD_TAG, strlen (OLD_TAG)))
return;
}
/* Remove the "new_" prefix that dhclient passes back */
if (!strncmp ((const char *) key, NEW_TAG, strlen (NEW_TAG)))
tmp_key = g_strdup ((const char *) (key + strlen (NEW_TAG)));
else
tmp_key = g_strdup ((const char *) key);
(*info->func) ((gpointer) tmp_key, value, info->user_data);
g_free (tmp_key);
}
gboolean
nm_dhcp_client_foreach_dhcp4_option (NMDHCPClient *self,
GHFunc func,
gpointer user_data)
{
NMDHCPClientPrivate *priv;
Dhcp4ForeachInfo info = { NULL, NULL };
g_return_val_if_fail (self != NULL, FALSE);
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE);
g_return_val_if_fail (func != NULL, FALSE);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (!state_is_bound (priv->state)) {
g_warning ("%s: dhclient didn't bind to a lease.", priv->iface);
return FALSE;
}
info.func = func;
info.user_data = user_data;
g_hash_table_foreach (priv->options, iterate_dhcp4_config_option, &info);
return TRUE;
}
/********************************************/
static void
process_classful_routes (GHashTable *options, NMIP4Config *ip4_config)
{
const char *str;
char **searches, **s;
str = g_hash_table_lookup (options, "new_static_routes");
if (!str)
return;
searches = g_strsplit (str, " ", 0);
if ((g_strv_length (searches) % 2)) {
g_message (" static routes provided, but invalid");
goto out;
}
for (s = searches; *s; s += 2) {
NMIP4Route *route;
struct in_addr rt_addr;
struct in_addr rt_route;
if (inet_pton (AF_INET, *s, &rt_addr) <= 0) {
g_warning ("DHCP provided invalid static route address: '%s'", *s);
continue;
}
if (inet_pton (AF_INET, *(s + 1), &rt_route) <= 0) {
g_warning ("DHCP provided invalid static route gateway: '%s'", *(s + 1));
continue;
}
// FIXME: ensure the IP addresse and route are sane
route = nm_ip4_route_new ();
nm_ip4_route_set_dest (route, (guint32) rt_addr.s_addr);
nm_ip4_route_set_prefix (route, 32); /* 255.255.255.255 */
nm_ip4_route_set_next_hop (route, (guint32) rt_route.s_addr);
nm_ip4_config_take_route (ip4_config, route);
g_message (" static route %s gw %s", *s, *(s + 1));
}
out:
g_strfreev (searches);
}
static void
process_domain_search (NMIP4Config *ip4_config, const char *str)
{
char **searches, **s;
char *unescaped, *p;
int i;
g_return_if_fail (str != NULL);
g_return_if_fail (ip4_config != NULL);
p = unescaped = g_strdup (str);
do {
p = strstr (p, "\\032");
if (!p)
break;
/* Clear the escaped space with real spaces */
for (i = 0; i < 4; i++)
*p++ = ' ';
} while (*p++);
if (strchr (unescaped, '\\')) {
g_message (" invalid domain search: '%s'", unescaped);
goto out;
}
searches = g_strsplit (unescaped, " ", 0);
for (s = searches; *s; s++) {
if (strlen (*s)) {
g_message (" domain search '%s'", *s);
nm_ip4_config_add_search (ip4_config, *s);
}
}
g_strfreev (searches);
out:
g_free (unescaped);
}
/* Given a table of DHCP options from the client, convert into an IP4Config */
static NMIP4Config *
ip4_options_to_config (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv;
NMIP4Config *ip4_config = NULL;
struct in_addr tmp_addr;
NMIP4Address *addr = NULL;
char *str = NULL;
guint32 gwaddr = 0;
gboolean have_classless = FALSE;
g_return_val_if_fail (self != NULL, NULL);
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_val_if_fail (priv->options != NULL, NULL);
ip4_config = nm_ip4_config_new ();
if (!ip4_config) {
g_warning ("%s: couldn't allocate memory for an IP4Config!", priv->iface);
return NULL;
}
addr = nm_ip4_address_new ();
if (!addr) {
g_warning ("%s: couldn't allocate memory for an IP4 Address!", priv->iface);
goto error;
}
str = g_hash_table_lookup (priv->options, "new_ip_address");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
nm_ip4_address_set_address (addr, tmp_addr.s_addr);
g_message (" address %s", str);
} else
goto error;
str = g_hash_table_lookup (priv->options, "new_subnet_mask");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
nm_ip4_address_set_prefix (addr, nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr));
g_message (" prefix %d (%s)", nm_ip4_address_get_prefix (addr), str);
}
/* Routes: if the server returns classless static routes, we MUST ignore
* the 'static_routes' option.
*/
if (NM_DHCP_CLIENT_GET_CLASS (self)->ip4_process_classless_routes) {
have_classless = NM_DHCP_CLIENT_GET_CLASS (self)->ip4_process_classless_routes (self,
priv->options,
ip4_config,
&gwaddr);
}
if (!have_classless) {
gwaddr = 0; /* Ensure client code doesn't lie */
process_classful_routes (priv->options, ip4_config);
}
if (gwaddr) {
char buf[INET_ADDRSTRLEN + 1];
inet_ntop (AF_INET, &gwaddr, buf, sizeof (buf));
g_message (" gateway %s", buf);
nm_ip4_address_set_gateway (addr, gwaddr);
} else {
/* If the gateway wasn't provided as a classless static route with a
* subnet length of 0, try to find it using the old-style 'routers' option.
*/
str = g_hash_table_lookup (priv->options, "new_routers");
if (str) {
char **routers = g_strsplit (str, " ", 0);
char **s;
for (s = routers; *s; s++) {
/* FIXME: how to handle multiple routers? */
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_address_set_gateway (addr, tmp_addr.s_addr);
g_message (" gateway %s", *s);
break;
} else
g_warning ("Ignoring invalid gateway '%s'", *s);
}
g_strfreev (routers);
}
}
nm_ip4_config_take_address (ip4_config, addr);
addr = NULL;
str = g_hash_table_lookup (priv->options, "new_host_name");
if (str)
g_message (" hostname '%s'", str);
str = g_hash_table_lookup (priv->options, "new_domain_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_nameserver (ip4_config, tmp_addr.s_addr);
g_message (" nameserver '%s'", *s);
} else
g_warning ("Ignoring invalid nameserver '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (priv->options, "new_domain_name");
if (str) {
char **domains = g_strsplit (str, " ", 0);
char **s;
for (s = domains; *s; s++) {
g_message (" domain name '%s'", *s);
nm_ip4_config_add_domain (ip4_config, *s);
}
g_strfreev (domains);
}
str = g_hash_table_lookup (priv->options, "new_domain_search");
if (str)
process_domain_search (ip4_config, str);
str = g_hash_table_lookup (priv->options, "new_netbios_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
nm_ip4_config_add_wins (ip4_config, tmp_addr.s_addr);
g_message (" wins '%s'", *s);
} else
g_warning ("Ignoring invalid WINS server '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (priv->options, "new_interface_mtu");
if (str) {
int int_mtu;
errno = 0;
int_mtu = strtol (str, NULL, 10);
if ((errno == EINVAL) || (errno == ERANGE))
goto error;
if (int_mtu > 576)
nm_ip4_config_set_mtu (ip4_config, int_mtu);
}
return ip4_config;
error:
if (addr)
nm_ip4_address_unref (addr);
g_object_unref (ip4_config);
return NULL;
}
NMIP4Config *
nm_dhcp_client_get_ip4_config (NMDHCPClient *self, gboolean test)
{
NMDHCPClientPrivate *priv;
g_return_val_if_fail (self != NULL, NULL);
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), NULL);
priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
if (test && !state_is_bound (priv->state)) {
g_warning ("%s: dhcp client didn't bind to a lease.", priv->iface);
return NULL;
}
return ip4_options_to_config (self);
}
/********************************************/
static void
nm_dhcp_client_init (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
priv->pid = -1;
}
static void
get_property (GObject *object, guint prop_id,
GValue *value, GParamSpec *pspec)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (object);
switch (prop_id) {
case PROP_IFACE:
g_value_set_string (value, priv->iface);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (object);
switch (prop_id) {
case PROP_IFACE:
/* construct-only */
priv->iface = g_strdup (g_value_get_string (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
dispose (GObject *object)
{
NMDHCPClient *self = NM_DHCP_CLIENT (object);
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
/* Stopping the client is left up to the controlling device
* explicitly since we may want to quit NetworkManager but not terminate
* the DHCP client.
*/
g_hash_table_destroy (priv->options);
g_free (priv->iface);
G_OBJECT_CLASS (nm_dhcp_client_parent_class)->dispose (object);
}
static void
nm_dhcp_client_class_init (NMDHCPClientClass *client_class)
{
GObjectClass *object_class = G_OBJECT_CLASS (client_class);
g_type_class_add_private (client_class, sizeof (NMDHCPClientPrivate));
/* virtual methods */
object_class->dispose = dispose;
object_class->get_property = get_property;
object_class->set_property = set_property;
client_class->stop = real_stop;
g_object_class_install_property
(object_class, PROP_IFACE,
g_param_spec_string (NM_DHCP_CLIENT_INTERFACE,
"iface",
"Interface",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
/* signals */
signals[STATE_CHANGED] =
g_signal_new ("state-changed",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMDHCPClientClass, state_changed),
NULL, NULL,
g_cclosure_marshal_VOID__UINT,
G_TYPE_NONE, 1, G_TYPE_UINT);
signals[TIMEOUT] =
g_signal_new ("timeout",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (NMDHCPClientClass, timeout),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}

View file

@ -0,0 +1,122 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2005 - 2010 Red Hat, Inc.
*/
#ifndef NM_DHCP_CLIENT_H
#define NM_DHCP_CLIENT_H
#include <glib.h>
#include <glib-object.h>
#include <nm-ip4-config.h>
#define NM_TYPE_DHCP_CLIENT (nm_dhcp_client_get_type ())
#define NM_DHCP_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_CLIENT, NMDHCPClient))
#define NM_DHCP_CLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DHCP_CLIENT, NMDHCPClientClass))
#define NM_IS_DHCP_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP_CLIENT))
#define NM_IS_DHCP_CLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_DHCP_CLIENT))
#define NM_DHCP_CLIENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_CLIENT, NMDHCPClientClass))
#define NM_DHCP_CLIENT_INTERFACE "iface"
typedef enum {
DHC_NBI = 0, /* no broadcast interfaces found */
DHC_PREINIT, /* configuration started */
DHC_BOUND4, /* IPv4 lease obtained */
DHC_BOUND6, /* IPv6 lease obtained */
DHC_IPV4LL, /* IPv4LL address obtained */
DHC_RENEW4, /* IPv4 lease renewed */
DHC_RENEW6, /* IPv6 lease renewed */
DHC_REBOOT, /* have valid lease, but now obtained a different one */
DHC_REBIND4, /* IPv4 new/different lease */
DHC_REBIND6, /* IPv6 new/different lease */
DHC_DEPREF6, /* IPv6 lease depreferred */
DHC_STOP, /* remove old lease */
DHC_MEDIUM, /* media selection begun */
DHC_TIMEOUT, /* timed out contacting DHCP server */
DHC_FAIL, /* all attempts to contact server timed out, sleeping */
DHC_EXPIRE, /* lease has expired, renewing */
DHC_RELEASE, /* releasing lease */
DHC_START, /* sent when dhclient started OK */
DHC_ABEND, /* dhclient exited abnormally */
DHC_END, /* dhclient exited normally */
DHC_END_OPTIONS, /* last option in subscription sent */
} NMDHCPState;
typedef struct {
GObject parent;
} NMDHCPClient;
typedef struct {
GObjectClass parent;
/* Methods */
/* Given the options table, extract any classless routes, add them to
* the IP4 config and return TRUE if any existed. If a gateway was sent
* as a classless route return that in out_gwaddr.
*/
gboolean (*ip4_process_classless_routes) (NMDHCPClient *self,
GHashTable *options,
NMIP4Config *ip4_config,
guint32 *out_gwaddr);
GPid (*ip4_start) (NMDHCPClient *self,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint8 *anycast_addr);
void (*stop) (NMDHCPClient *self);
/* Signals */
void (*state_changed) (NMDHCPClient *self, NMDHCPState state);
void (*timeout) (NMDHCPClient *self);
} NMDHCPClientClass;
GType nm_dhcp_client_get_type (void);
GPid nm_dhcp_client_get_pid (NMDHCPClient *self);
const char *nm_dhcp_client_get_iface (NMDHCPClient *self);
gboolean nm_dhcp_client_start (NMDHCPClient *self,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint32 timeout_secs,
guint8 *dhcp_anycast_addr);
void nm_dhcp_client_stop (NMDHCPClient *self);
void nm_dhcp_client_new_options (NMDHCPClient *self,
GHashTable *options,
const char *reason);
gboolean nm_dhcp_client_foreach_dhcp4_option (NMDHCPClient *self,
GHFunc func,
gpointer user_data);
NMIP4Config *nm_dhcp_client_get_ip4_config (NMDHCPClient *self, gboolean test);
/* Backend helpers */
void nm_dhcp_client_stop_existing (const char *pid_file, const char *binary_name);
/* Implemented by the backends */
GSList *nm_dhcp_backend_get_lease_config (const char *iface,
const char *uuid);
#endif /* NM_DHCP_CLIENT_H */

View file

@ -36,46 +36,37 @@
#include <config.h>
#include "nm-dhcp-manager.h"
#include "nm-dhcp-dhclient.h"
#include "nm-utils.h"
G_DEFINE_TYPE (NMDHCPDhclient, nm_dhcp_dhclient, NM_TYPE_DHCP_CLIENT)
#define NM_DHCP_MANAGER_PID_FILENAME "dhclient"
#define NM_DHCP_MANAGER_PID_FILE_EXT "pid"
#define NM_DHCP_DHCLIENT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_DHCLIENT, NMDHCPDhclientPrivate))
#if defined(TARGET_DEBIAN)
#define NM_DHCP_MANAGER_LEASE_DIR LOCALSTATEDIR "/lib/dhcp3"
#define NM_DHCLIENT_LEASE_DIR LOCALSTATEDIR "/lib/dhcp3"
#elif defined(TARGET_SUSE) || defined(TARGET_MANDRIVA)
#define NM_DHCP_MANAGER_LEASE_DIR LOCALSTATEDIR "/lib/dhcp"
#define NM_DHCLIENT_LEASE_DIR LOCALSTATEDIR "/lib/dhcp"
#else
#define NM_DHCP_MANAGER_LEASE_DIR LOCALSTATEDIR "/lib/dhclient"
#define NM_DHCLIENT_LEASE_DIR LOCALSTATEDIR "/lib/dhclient"
#endif
#define NM_DHCP_MANAGER_LEASE_FILENAME "dhclient"
#define NM_DHCP_MANAGER_LEASE_FILE_EXT "lease"
#define ACTION_SCRIPT_PATH LIBEXECDIR "/nm-dhcp-client.action"
static char *
get_pidfile_for_iface (const char * iface)
{
return g_strdup_printf ("%s/%s-%s.%s",
NM_DHCP_MANAGER_RUN_DIR,
NM_DHCP_MANAGER_PID_FILENAME,
iface,
NM_DHCP_MANAGER_PID_FILE_EXT);
}
typedef struct {
char *conf_file;
char *lease_file;
char *pid_file;
} NMDHCPDhclientPrivate;
static char *
get_leasefile_for_iface (const char * iface, const char *uuid)
{
return g_strdup_printf ("%s/%s-%s-%s.%s",
NM_DHCP_MANAGER_LEASE_DIR,
NM_DHCP_MANAGER_LEASE_FILENAME,
return g_strdup_printf ("%s/dhclient-%s-%s.lease",
NM_DHCLIENT_LEASE_DIR,
uuid,
iface,
NM_DHCP_MANAGER_LEASE_FILE_EXT);
iface);
}
static void
@ -119,7 +110,7 @@ add_lease_option (GHashTable *hash, char *line)
}
GSList *
nm_dhcp4_client_get_lease_config (const char *iface, const char *uuid)
nm_dhcp_backend_get_lease_config (const char *iface, const char *uuid)
{
GSList *parsed = NULL, *iter, *leases = NULL;
char *contents = NULL;
@ -288,28 +279,39 @@ out:
#define DHCP_HOSTNAME_FORMAT DHCP_HOSTNAME_TAG " \"%s\"; # added by NetworkManager"
static gboolean
merge_dhclient_config (NMDHCPClient *client,
merge_dhclient_config (const char *iface,
const char *conf_file,
NMSettingIP4Config *s_ip4,
guint8 *anycast_addr,
const char *contents,
const char *orig,
const char *orig_path,
GError **error)
{
GString *new_contents;
char *orig_contents = NULL;
gboolean success = FALSE;
g_return_val_if_fail (client != NULL, FALSE);
g_return_val_if_fail (client->iface != NULL, FALSE);
g_return_val_if_fail (iface != NULL, FALSE);
g_return_val_if_fail (conf_file != NULL, FALSE);
new_contents = g_string_new (_("# Created by NetworkManager\n"));
if (g_file_test (orig_path, G_FILE_TEST_EXISTS)) {
GError *read_error = NULL;
if (!g_file_get_contents (orig_path, &orig_contents, NULL, &read_error)) {
nm_warning ("%s: error reading dhclient configuration %s: %s",
iface, orig_path, read_error->message);
g_error_free (read_error);
}
}
/* Add existing options, if any, but ignore stuff NM will replace. */
if (contents) {
if (orig_contents) {
char **lines = NULL, **line;
g_string_append_printf (new_contents, _("# Merged from %s\n\n"), orig);
g_string_append_printf (new_contents, _("# Merged from %s\n\n"), orig_path);
lines = g_strsplit_set (contents, "\n\r", 0);
lines = g_strsplit_set (orig_contents, "\n\r", 0);
for (line = lines; lines && *line; line++) {
gboolean ignore = FALSE;
@ -334,6 +336,7 @@ merge_dhclient_config (NMDHCPClient *client,
if (lines)
g_strfreev (lines);
g_free (orig_contents);
} else
g_string_append_c (new_contents, '\n');
@ -374,14 +377,13 @@ merge_dhclient_config (NMDHCPClient *client,
" initial-interval 1; \n"
" anycast-mac ethernet %02x:%02x:%02x:%02x:%02x:%02x;\n"
"}\n",
client->iface,
iface,
anycast_addr[0], anycast_addr[1],
anycast_addr[2], anycast_addr[3],
anycast_addr[4], anycast_addr[5]);
}
if (g_file_set_contents (client->conf_file, new_contents->str, -1, error))
success = TRUE;
success = g_file_set_contents (conf_file, new_contents->str, -1, error);
g_string_free (new_contents, TRUE);
return success;
@ -393,18 +395,16 @@ merge_dhclient_config (NMDHCPClient *client,
* read their single config file and merge that into a custom per-interface
* config file along with the NM options.
*/
static gboolean
create_dhclient_config (NMDHCPClient *client,
static char *
create_dhclient_config (const char *iface,
NMSettingIP4Config *s_ip4,
guint8 *dhcp_anycast_addr)
{
char *orig = NULL, *contents = NULL;
char *orig = NULL, *tmp, *conf_file = NULL;
GError *error = NULL;
gboolean success = FALSE;
char *tmp;
g_return_val_if_fail (client != NULL, FALSE);
g_return_val_if_fail (client->iface != NULL, FALSE);
g_return_val_if_fail (iface != NULL, FALSE);
#if defined(TARGET_SUSE)
orig = g_strdup (SYSCONFDIR "/dhclient.conf");
@ -413,41 +413,28 @@ create_dhclient_config (NMDHCPClient *client,
#elif defined(TARGET_GENTOO)
orig = g_strdup (SYSCONFDIR "/dhcp/dhclient.conf");
#else
orig = g_strdup_printf (SYSCONFDIR "/dhclient-%s.conf", client->iface);
orig = g_strdup_printf (SYSCONFDIR "/dhclient-%s.conf", iface);
#endif
if (!orig) {
nm_warning ("%s: not enough memory for dhclient options.", client->iface);
nm_warning ("%s: not enough memory for dhclient options.", iface);
return FALSE;
}
tmp = g_strdup_printf ("nm-dhclient-%s.conf", client->iface);
client->conf_file = g_build_filename ("/var", "run", tmp, NULL);
tmp = g_strdup_printf ("nm-dhclient-%s.conf", iface);
conf_file = g_build_filename ("/var", "run", tmp, NULL);
g_free (tmp);
if (!g_file_test (orig, G_FILE_TEST_EXISTS))
goto out;
if (!g_file_get_contents (orig, &contents, NULL, &error)) {
nm_warning ("%s: error reading dhclient configuration %s: %s",
client->iface, orig, error->message);
g_error_free (error);
goto out;
}
out:
error = NULL;
if (merge_dhclient_config (client, s_ip4, dhcp_anycast_addr, contents, orig, &error))
success = TRUE;
else {
success = merge_dhclient_config (iface, conf_file, s_ip4, dhcp_anycast_addr, orig, &error);
if (!success) {
nm_warning ("%s: error creating dhclient configuration: %s",
client->iface, error->message);
iface, error->message);
g_error_free (error);
}
g_free (contents);
g_free (orig);
return success;
return conf_file;
}
@ -459,45 +446,49 @@ dhclient_child_setup (gpointer user_data G_GNUC_UNUSED)
setpgid (pid, pid);
}
GPid
nm_dhcp4_client_start (NMDHCPClient *client,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint8 *dhcp_anycast_addr)
static GPid
real_ip4_start (NMDHCPClient *client,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint8 *dhcp_anycast_addr)
{
NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client);
GPtrArray *argv = NULL;
GPid pid = 0;
GError *error = NULL;
char *pid_contents = NULL;
const char *iface;
char *binary_name;
g_return_val_if_fail (priv->pid_file == NULL, -1);
iface = nm_dhcp_client_get_iface (client);
priv->pid_file = g_strdup_printf (LOCALSTATEDIR "/run/dhclient-%s.pid", iface);
if (!priv->pid_file) {
nm_warning ("%s: not enough memory for dhcpcd options.", iface);
return -1;
}
if (!g_file_test (DHCP_CLIENT_PATH, G_FILE_TEST_EXISTS)) {
nm_warning (DHCP_CLIENT_PATH " does not exist.");
goto out;
return -1;
}
client->pid_file = get_pidfile_for_iface (client->iface);
if (!client->pid_file) {
nm_warning ("%s: not enough memory for dhclient options.", client->iface);
goto out;
/* Kill any existing dhclient from the pidfile */
binary_name = g_path_get_basename (DHCP_CLIENT_PATH);
nm_dhcp_client_stop_existing (priv->pid_file, binary_name);
g_free (binary_name);
priv->conf_file = create_dhclient_config (iface, s_ip4, dhcp_anycast_addr);
if (!priv->conf_file) {
nm_warning ("%s: error creating dhclient configuration file.", iface);
return -1;
}
client->lease_file = get_leasefile_for_iface (client->iface, uuid);
if (!client->lease_file) {
nm_warning ("%s: not enough memory for dhclient options.", client->iface);
goto out;
}
if (!create_dhclient_config (client, s_ip4, dhcp_anycast_addr))
goto out;
/* Kill any existing dhclient bound to this interface */
if (g_file_get_contents (client->pid_file, &pid_contents, NULL, NULL)) {
unsigned long int tmp = strtoul (pid_contents, NULL, 10);
if (!((tmp == ULONG_MAX) && (errno == ERANGE)))
nm_dhcp_client_stop (client, (pid_t) tmp);
remove (client->pid_file);
priv->lease_file = get_leasefile_for_iface (iface, uuid);
if (!priv->lease_file) {
nm_warning ("%s: not enough memory for dhclient options.", iface);
return -1;
}
argv = g_ptr_array_new ();
@ -509,32 +500,43 @@ nm_dhcp4_client_start (NMDHCPClient *client,
g_ptr_array_add (argv, (gpointer) ACTION_SCRIPT_PATH );
g_ptr_array_add (argv, (gpointer) "-pf"); /* Set pid file */
g_ptr_array_add (argv, (gpointer) client->pid_file);
g_ptr_array_add (argv, (gpointer) priv->pid_file);
g_ptr_array_add (argv, (gpointer) "-lf"); /* Set lease file */
g_ptr_array_add (argv, (gpointer) client->lease_file);
g_ptr_array_add (argv, (gpointer) priv->lease_file);
g_ptr_array_add (argv, (gpointer) "-cf"); /* Set interface config file */
g_ptr_array_add (argv, (gpointer) client->conf_file);
g_ptr_array_add (argv, (gpointer) priv->conf_file);
g_ptr_array_add (argv, (gpointer) client->iface);
g_ptr_array_add (argv, (gpointer) iface);
g_ptr_array_add (argv, NULL);
if (!g_spawn_async (NULL, (char **) argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
&dhclient_child_setup, NULL, &pid, &error)) {
nm_warning ("dhclient failed to start. error: '%s'", error->message);
g_error_free (error);
goto out;
}
pid = -1;
} else
nm_info ("dhclient started with pid %d", pid);
nm_info ("dhclient started with pid %d", pid);
out:
g_free (pid_contents);
g_ptr_array_free (argv, TRUE);
return pid;
}
static void
real_stop (NMDHCPClient *client)
{
NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (client);
/* Chain up to parent */
NM_DHCP_CLIENT_CLASS (nm_dhcp_dhclient_parent_class)->stop (client);
if (priv->conf_file)
remove (priv->conf_file);
if (priv->pid_file)
remove (priv->pid_file);
}
static const char **
process_rfc3442_route (const char **octets, NMIP4Route **out_route)
{
@ -596,10 +598,11 @@ error:
return o;
}
gboolean
nm_dhcp4_client_process_classless_routes (GHashTable *options,
NMIP4Config *ip4_config,
guint32 *gwaddr)
static gboolean
real_ip4_process_classless_routes (NMDHCPClient *client,
GHashTable *options,
NMIP4Config *ip4_config,
guint32 *gwaddr)
{
const char *str;
char **octets, **o;
@ -664,3 +667,38 @@ out:
return have_routes;
}
/***************************************************/
static void
nm_dhcp_dhclient_init (NMDHCPDhclient *self)
{
}
static void
dispose (GObject *object)
{
NMDHCPDhclientPrivate *priv = NM_DHCP_DHCLIENT_GET_PRIVATE (object);
g_free (priv->pid_file);
g_free (priv->conf_file);
g_free (priv->lease_file);
G_OBJECT_CLASS (nm_dhcp_dhclient_parent_class)->dispose (object);
}
static void
nm_dhcp_dhclient_class_init (NMDHCPDhclientClass *dhclient_class)
{
NMDHCPClientClass *client_class = NM_DHCP_CLIENT_CLASS (dhclient_class);
GObjectClass *object_class = G_OBJECT_CLASS (dhclient_class);
g_type_class_add_private (dhclient_class, sizeof (NMDHCPDhclientPrivate));
/* virtual methods */
object_class->dispose = dispose;
client_class->ip4_start = real_ip4_start;
client_class->stop = real_stop;
client_class->ip4_process_classless_routes = real_ip4_process_classless_routes;
}

View file

@ -0,0 +1,45 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2005 - 2010 Red Hat, Inc.
*/
#ifndef NM_DHCP_DHCLIENT_H
#define NM_DHCP_DHCLIENT_H
#include <glib.h>
#include <glib-object.h>
#include "nm-dhcp-client.h"
#define NM_TYPE_DHCP_DHCLIENT (nm_dhcp_dhclient_get_type ())
#define NM_DHCP_DHCLIENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_DHCLIENT, NMDHCPDhclient))
#define NM_DHCP_DHCLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DHCP_DHCLIENT, NMDHCPDhclientClass))
#define NM_IS_DHCP_DHCLIENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP_DHCLIENT))
#define NM_IS_DHCP_DHCLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_DHCP_DHCLIENT))
#define NM_DHCP_DHCLIENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_DHCLIENT, NMDHCPDhclientClass))
typedef struct {
NMDHCPClient parent;
} NMDHCPDhclient;
typedef struct {
NMDHCPClientClass parent;
} NMDHCPDhclientClass;
GType nm_dhcp_dhclient_get_type (void);
#endif /* NM_DHCP_DHCLIENT_H */

View file

@ -2,6 +2,7 @@
/* nm-dhcp-dhcpcd.c - dhcpcd specific hooks for NetworkManager
*
* Copyright (C) 2008 Roy Marples
* Copyright (C) 2010 Dan Williams <dcbw@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -31,26 +32,22 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include "nm-dhcp-manager.h"
#include "nm-dhcp-dhcpcd.h"
#include "nm-utils.h"
#define NM_DHCP_MANAGER_PID_FILENAME "dhcpcd"
#define NM_DHCP_MANAGER_PID_FILE_EXT "pid"
G_DEFINE_TYPE (NMDHCPDhcpcd, nm_dhcp_dhcpcd, NM_TYPE_DHCP_DHCPCD)
#define NM_DHCP_DHCPCD_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_DHCPCD, NMDHCPDhcpcdPrivate))
#define ACTION_SCRIPT_PATH LIBEXECDIR "/nm-dhcp-client.action"
typedef struct {
char *pid_file;
} NMDHCPDhcpcdPrivate;
static char *
get_pidfile_for_iface (const char * iface)
{
return g_strdup_printf ("/var/run/%s-%s.%s",
NM_DHCP_MANAGER_PID_FILENAME,
iface,
NM_DHCP_MANAGER_PID_FILE_EXT);
}
GSList *
nm_dhcp4_client_get_lease_config (const char *iface, const char *uuid)
nm_dhcp_backend_get_lease_config (const char *iface, const char *uuid)
{
return NULL;
}
@ -63,37 +60,38 @@ dhcpcd_child_setup (gpointer user_data G_GNUC_UNUSED)
setpgid (pid, pid);
}
GPid
nm_dhcp4_client_start (NMDHCPClient *client,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint8 *dhcp_anycast_addr)
static GPid
real_ip4_start (NMDHCPClient *client,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint8 *dhcp_anycast_addr)
{
NMDHCPDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (client);
GPtrArray *argv = NULL;
GPid pid = 0;
GError *error = NULL;
char *pid_contents = NULL;
char *pid_contents = NULL, *binary_name;
const char *iface;
g_return_val_if_fail (priv->pid_file == NULL, -1);
iface = nm_dhcp_client_get_iface (client);
priv->pid_file = g_strdup_printf (LOCALSTATEDIR "/run/dhcpcd-%s.pid", iface);
if (!priv->pid_file) {
nm_warning ("%s: not enough memory for dhcpcd options.", iface);
return -1;
}
if (!g_file_test (DHCP_CLIENT_PATH, G_FILE_TEST_EXISTS)) {
nm_warning (DHCP_CLIENT_PATH " does not exist.");
goto out;
return -1;
}
client->pid_file = get_pidfile_for_iface (client->iface);
if (!client->pid_file) {
nm_warning ("%s: not enough memory for dhcpcd options.", client->iface);
goto out;
}
/* Kill any existing dhcpcd bound to this interface */
if (g_file_get_contents (client->pid_file, &pid_contents, NULL, NULL)) {
unsigned long int tmp = strtoul (pid_contents, NULL, 10);
if (!((tmp == ULONG_MAX) && (errno == ERANGE)))
nm_dhcp_client_stop (client, (pid_t) tmp);
remove (client->pid_file);
}
/* Kill any existing dhclient from the pidfile */
binary_name = g_path_get_basename (DHCP_CLIENT_PATH);
nm_dhcp_client_stop_existing (priv->pid_file, binary_name);
g_free (binary_name);
argv = g_ptr_array_new ();
g_ptr_array_add (argv, (gpointer) DHCP_CLIENT_PATH);
@ -107,28 +105,38 @@ nm_dhcp4_client_start (NMDHCPClient *client,
g_ptr_array_add (argv, (gpointer) "-c"); /* Set script file */
g_ptr_array_add (argv, (gpointer) ACTION_SCRIPT_PATH );
g_ptr_array_add (argv, (gpointer) client->iface);
g_ptr_array_add (argv, (gpointer) iface);
g_ptr_array_add (argv, NULL);
if (!g_spawn_async (NULL, (char **) argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
&dhcpcd_child_setup, NULL, &pid, &error)) {
nm_warning ("dhcpcd failed to start. error: '%s'", error->message);
g_error_free (error);
goto out;
}
} else
nm_info ("dhcpcd started with pid %d", pid);
nm_info ("dhcpcd started with pid %d", pid);
out:
g_free (pid_contents);
g_ptr_array_free (argv, TRUE);
return pid;
}
gboolean
nm_dhcp4_client_process_classless_routes (GHashTable *options,
NMIP4Config *ip4_config,
guint32 *gwaddr)
static void
real_stop (NMDHCPClient *client)
{
NMDHCPDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (client);
/* Chain up to parent */
NM_DHCP_CLIENT_CLASS (nm_dhcp_dhcpcd_parent_class)->stop (client);
if (priv->pid_file)
remove (priv->pid_file);
}
static gboolean
real_ip4_process_classless_routes (NMDHCPClient *client,
GHashTable *options,
NMIP4Config *ip4_config,
guint32 *gwaddr)
{
const char *str;
char **routes, **r;
@ -201,3 +209,36 @@ out:
return have_routes;
}
/***************************************************/
static void
nm_dhcp_dhcpcd_init (NMDHCPDhcpcd *self)
{
}
static void
dispose (GObject *object)
{
NMDHCPDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (object);
g_free (priv->pid_file);
G_OBJECT_CLASS (nm_dhcp_dhcpcd_parent_class)->dispose (object);
}
static void
nm_dhcp_dhcpcd_class_init (NMDHCPDhcpcdClass *dhcpcd_class)
{
NMDHCPClientClass *client_class = NM_DHCP_CLIENT_CLASS (dhcpcd_class);
GObjectClass *object_class = G_OBJECT_CLASS (dhcpcd_class);
g_type_class_add_private (dhcpcd_class, sizeof (NMDHCPDhcpcdPrivate));
/* virtual methods */
object_class->dispose = dispose;
client_class->ip4_start = real_ip4_start;
client_class->stop = real_stop;
client_class->ip4_process_classless_routes = real_ip4_process_classless_routes;
}

View file

@ -0,0 +1,45 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2005 - 2010 Red Hat, Inc.
*/
#ifndef NM_DHCP_DHCPCD_H
#define NM_DHCP_DHCPCD_H
#include <glib.h>
#include <glib-object.h>
#include "nm-dhcp-client.h"
#define NM_TYPE_DHCP_DHCPCD (nm_dhcp_dhcpcd_get_type ())
#define NM_DHCP_DHCPCD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_DHCPCD, NMDHCPDhcpcd))
#define NM_DHCP_DHCPCD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DHCP_DHCPCD, NMDHCPDhcpcdClass))
#define NM_IS_DHCP_DHCPCD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DHCP_DHCPCD))
#define NM_IS_DHCP_DHCPCD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_DHCP_DHCPCD))
#define NM_DHCP_DHCPCD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_DHCPCD, NMDHCPDhcpcdClass))
typedef struct {
NMDHCPClient parent;
} NMDHCPDhcpcd;
typedef struct {
NMDHCPClientClass parent;
} NMDHCPDhcpcdClass;
GType nm_dhcp_dhcpcd_get_type (void);
#endif /* NM_DHCP_DHCPCD_H */

File diff suppressed because it is too large Load diff

View file

@ -27,12 +27,11 @@
#include <nm-setting-ip4-config.h>
#include "nm-dhcp-client.h"
#include "nm-ip4-config.h"
#include "nm-dhcp4-config.h"
#include "nm-hostname-provider.h"
#define NM_DHCP_MANAGER_RUN_DIR LOCALSTATEDIR "/run"
#define NM_TYPE_DHCP_MANAGER (nm_dhcp_manager_get_type ())
#define NM_DHCP_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DHCP_MANAGER, NMDHCPManager))
#define NM_DHCP_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DHCP_MANAGER, NMDHCPManagerClass))
@ -40,98 +39,35 @@
#define NM_IS_DHCP_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_DHCP_MANAGER))
#define NM_DHCP_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_MANAGER, NMDHCPManagerClass))
typedef enum {
DHC_NBI = 0, /* no broadcast interfaces found */
DHC_PREINIT, /* configuration started */
DHC_BOUND4, /* IPv4 lease obtained */
DHC_BOUND6, /* IPv6 lease obtained */
DHC_IPV4LL, /* IPv4LL address obtained */
DHC_RENEW4, /* IPv4 lease renewed */
DHC_RENEW6, /* IPv6 lease renewed */
DHC_REBOOT, /* have valid lease, but now obtained a different one */
DHC_REBIND4, /* IPv4 new/different lease */
DHC_REBIND6, /* IPv6 new/different lease */
DHC_DEPREF6, /* IPv6 lease depreferred */
DHC_STOP, /* remove old lease */
DHC_MEDIUM, /* media selection begun */
DHC_TIMEOUT, /* timed out contacting DHCP server */
DHC_FAIL, /* all attempts to contact server timed out, sleeping */
DHC_EXPIRE, /* lease has expired, renewing */
DHC_RELEASE, /* releasing lease */
DHC_START, /* sent when dhclient started OK */
DHC_ABEND, /* dhclient exited abnormally */
DHC_END, /* dhclient exited normally */
DHC_END_OPTIONS, /* last option in subscription sent */
} NMDHCPState;
typedef struct {
GObject parent;
} NMDHCPManager;
typedef struct {
GObjectClass parent;
/* Signals */
void (*state_changed) (NMDHCPManager *manager, guint32 id, NMDHCPState state);
void (*timeout) (NMDHCPManager *manager, guint32 id);
} NMDHCPManagerClass;
typedef struct {
GPid id;
char * iface;
guchar state;
GPid pid;
char * pid_file;
char * conf_file;
char * lease_file;
guint timeout_id;
guint watch_id;
NMDHCPManager * manager;
GHashTable * options;
} NMDHCPClient;
GType nm_dhcp_manager_get_type (void);
NMDHCPManager *nm_dhcp_manager_get (void);
void nm_dhcp_manager_set_hostname_provider(NMDHCPManager *manager,
NMHostnameProvider *provider);
guint32 nm_dhcp_manager_begin_transaction (NMDHCPManager *manager,
const char *iface,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint32 timeout,
guint8 *dhcp_anycast_addr);
void nm_dhcp_manager_cancel_transaction (NMDHCPManager *manager,
guint32 id);
NMIP4Config * nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, guint32 id);
NMDHCPState nm_dhcp_manager_get_client_state (NMDHCPManager *manager, guint32 id);
NMDHCPClient * nm_dhcp_manager_start_client (NMDHCPManager *manager,
const char *iface,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint32 timeout,
guint8 *dhcp_anycast_addr);
gboolean nm_dhcp_manager_foreach_dhcp4_option (NMDHCPManager *self,
guint32 id,
GHFunc func,
gpointer user_data);
GSList * nm_dhcp_manager_get_lease_config (NMDHCPManager *self,
const char *iface,
const char *uuid);
GSList * nm_dhcp4_manager_get_lease_config (NMDHCPManager *self,
const char *iface,
const char *uuid);
/* The following are implemented by the DHCP client backends */
GPid nm_dhcp4_client_start (NMDHCPClient *client,
const char *uuid,
NMSettingIP4Config *s_ip4,
guint8 *anycast_addr);
void nm_dhcp_client_stop (NMDHCPClient *client, pid_t pid);
gboolean nm_dhcp4_client_process_classless_routes (GHashTable *options,
NMIP4Config *ip4_config,
guint32 *gwaddr);
GSList * nm_dhcp4_client_get_lease_config (const char *iface,
const char *uuid);
/* Test functions */
NMIP4Config *nm_dhcp4_manager_options_to_config (const char *iface,
GHashTable *options);
/* For testing only */
NMIP4Config *nm_dhcp_manager_test_ip4_options_to_config (const char *iface,
GHashTable *options,
const char *reason);
#endif /* NM_DHCP_MANAGER_H */

View file

@ -1608,9 +1608,9 @@ ip4_match_config (NMDevice *self, NMConnection *connection)
/* Get any saved leases that apply to this connection */
dhcp_mgr = nm_dhcp_manager_get ();
leases = nm_dhcp4_manager_get_lease_config (dhcp_mgr,
nm_device_get_iface (self),
nm_setting_connection_get_uuid (s_con));
leases = nm_dhcp_manager_get_lease_config (dhcp_mgr,
nm_device_get_iface (self),
nm_setting_connection_get_uuid (s_con));
g_object_unref (dhcp_mgr);
method = nm_setting_ip4_config_get_method (s_ip4);

View file

@ -105,7 +105,7 @@ typedef struct {
/* IP4 configuration info */
NMIP4Config * ip4_config; /* Config from DHCP, PPP, or system config files */
NMDHCPManager * dhcp_manager;
guint32 dhcp4_id;
NMDHCPClient * dhcp4_client;
guint32 dhcp_timeout;
gulong dhcp_state_sigid;
gulong dhcp_timeout_sigid;
@ -1058,6 +1058,120 @@ aipd_exec (NMDevice *self, GError **error)
return TRUE;
}
static void
dhcp4_add_option_cb (gpointer key, gpointer value, gpointer user_data)
{
nm_dhcp4_config_add_option (NM_DHCP4_CONFIG (user_data),
(const char *) key,
(const char *) value);
}
static void
handle_dhcp_lease_change (NMDevice *device)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device);
NMIP4Config *config;
NMSettingIP4Config *s_ip4;
NMConnection *connection;
NMActRequest *req;
NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
gboolean assumed;
if (!nm_device_get_use_dhcp (device)) {
nm_warning ("got DHCP rebind for device that wasn't using DHCP.");
return;
}
config = nm_dhcp_client_get_ip4_config (priv->dhcp4_client, FALSE);
if (!config) {
nm_warning ("failed to get DHCP config for rebind");
nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED);
return;
}
req = nm_device_get_act_request (device);
g_assert (req);
connection = nm_act_request_get_connection (req);
g_assert (connection);
s_ip4 = NM_SETTING_IP4_CONFIG (nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG));
nm_utils_merge_ip4_config (config, s_ip4);
g_object_set_data (G_OBJECT (req), NM_ACT_REQUEST_IP4_CONFIG, config);
assumed = nm_act_request_get_assumed (req);
if (nm_device_set_ip4_config (device, config, assumed, &reason)) {
nm_dhcp4_config_reset (priv->dhcp4_config);
nm_dhcp_client_foreach_dhcp4_option (priv->dhcp4_client,
dhcp4_add_option_cb,
priv->dhcp4_config);
} else {
nm_warning ("Failed to update IP4 config in response to DHCP event.");
nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason);
}
}
static void
dhcp_state_changed (NMDHCPClient *client,
NMDHCPState state,
gpointer user_data)
{
NMDevice *device = NM_DEVICE (user_data);
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device);
NMDeviceState dev_state;
if (!nm_device_get_act_request (device))
return;
dev_state = nm_device_get_state (device);
switch (state) {
case DHC_BOUND4: /* lease obtained */
case DHC_RENEW4: /* lease renewed */
case DHC_REBOOT: /* have valid lease, but now obtained a different one */
case DHC_REBIND4: /* new, different lease */
if (dev_state == NM_DEVICE_STATE_IP_CONFIG)
nm_device_activate_schedule_stage4_ip4_config_get (device);
else if (dev_state == NM_DEVICE_STATE_ACTIVATED)
handle_dhcp_lease_change (device);
break;
case DHC_TIMEOUT: /* timed out contacting DHCP server */
nm_dhcp4_config_reset (priv->dhcp4_config);
if (nm_device_get_state (device) == NM_DEVICE_STATE_IP_CONFIG)
nm_device_activate_schedule_stage4_ip4_config_timeout (device);
break;
case DHC_FAIL: /* all attempts to contact server timed out, sleeping */
case DHC_ABEND: /* dhclient exited abnormally */
case DHC_END: /* dhclient exited normally */
nm_dhcp4_config_reset (priv->dhcp4_config);
if (nm_device_get_state (device) == NM_DEVICE_STATE_IP_CONFIG) {
nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_DHCP_FAILED);
} else if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) {
if (nm_device_get_use_dhcp (device)) {
/* dhclient quit and therefore can't renew our lease, kill the conneciton */
nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED);
}
}
break;
default:
break;
}
}
static void
dhcp_timeout (NMDHCPClient *client, gpointer user_data)
{
NMDevice *device = NM_DEVICE (user_data);
if (!nm_device_get_act_request (device))
return;
if (nm_device_get_state (device) == NM_DEVICE_STATE_IP_CONFIG)
nm_device_activate_schedule_stage4_ip4_config_timeout (device);
}
static NMActStageReturn
real_act_stage3_ip4_config_start (NMDevice *self, NMDeviceStateReason *reason)
{
@ -1101,16 +1215,22 @@ real_act_stage3_ip4_config_start (NMDevice *self, NMDeviceStateReason *reason)
/* DHCP manager will cancel any transaction already in progress and we do not
want to cancel this activation if we get "down" state from that. */
g_signal_handler_block (priv->dhcp_manager, priv->dhcp_state_sigid);
priv->dhcp4_id = nm_dhcp_manager_begin_transaction (priv->dhcp_manager,
ip_iface,
uuid,
s_ip4,
priv->dhcp_timeout,
anycast);
g_signal_handler_unblock (priv->dhcp_manager, priv->dhcp_state_sigid);
priv->dhcp4_client = nm_dhcp_manager_start_client (priv->dhcp_manager,
ip_iface,
uuid,
s_ip4,
priv->dhcp_timeout,
anycast);
if (priv->dhcp4_client) {
priv->dhcp_state_sigid = g_signal_connect (priv->dhcp4_client,
"state-changed",
G_CALLBACK (dhcp_state_changed),
self);
priv->dhcp_timeout_sigid = g_signal_connect (priv->dhcp4_client,
"timeout",
G_CALLBACK (dhcp_timeout),
self);
if (priv->dhcp4_id) {
/* DHCP devices will be notified by the DHCP manager when
* stuff happens.
*/
@ -1288,14 +1408,6 @@ nm_device_new_ip4_shared_config (NMDevice *self, NMDeviceStateReason *reason)
return config;
}
static void
dhcp4_add_option_cb (gpointer key, gpointer value, gpointer user_data)
{
nm_dhcp4_config_add_option (NM_DHCP4_CONFIG (user_data),
(const char *) key,
(const char *) value);
}
static NMActStageReturn
real_act_stage4_get_ip4_config (NMDevice *self,
NMIP4Config **config,
@ -1320,16 +1432,15 @@ real_act_stage4_get_ip4_config (NMDevice *self,
s_ip4 = (NMSettingIP4Config *) nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG);
if (nm_device_get_use_dhcp (self)) {
*config = nm_dhcp_manager_get_ip4_config (priv->dhcp_manager, priv->dhcp4_id);
*config = nm_dhcp_client_get_ip4_config (priv->dhcp4_client, FALSE);
if (*config) {
/* Merge user-defined overrides into the IP4Config to be applied */
nm_utils_merge_ip4_config (*config, s_ip4);
nm_dhcp4_config_reset (priv->dhcp4_config);
nm_dhcp_manager_foreach_dhcp4_option (priv->dhcp_manager,
priv->dhcp4_id,
dhcp4_add_option_cb,
priv->dhcp4_config);
nm_dhcp_client_foreach_dhcp4_option (priv->dhcp4_client,
dhcp4_add_option_cb,
priv->dhcp4_config);
/* Notify of new DHCP4 config */
g_object_notify (G_OBJECT (self), NM_DEVICE_INTERFACE_DHCP4_CONFIG);
@ -2015,14 +2126,14 @@ nm_device_deactivate_quickly (NMDevice *self)
/* Clear any delayed transitions */
delayed_transitions_clear (self);
/* Stop any ongoing DHCP transaction on this device */
if (nm_device_get_act_request (self)) {
if (nm_device_get_use_dhcp (self)) {
nm_dhcp_manager_cancel_transaction (priv->dhcp_manager, priv->dhcp4_id);
nm_device_set_use_dhcp (self, FALSE);
/* Notify of invalid DHCP4 config */
g_object_notify (G_OBJECT (self), NM_DEVICE_INTERFACE_DHCP4_CONFIG);
} else if (priv->dnsmasq_manager) {
/* Or any shared connection */
if (priv->dnsmasq_state_id) {
g_signal_handler_disconnect (priv->dnsmasq_manager, priv->dnsmasq_state_id);
priv->dnsmasq_state_id = 0;
@ -2032,8 +2143,6 @@ nm_device_deactivate_quickly (NMDevice *self)
g_object_unref (priv->dnsmasq_manager);
priv->dnsmasq_manager = NULL;
}
priv->dhcp4_id = 0;
}
aipd_cleanup (self);
@ -2253,119 +2362,6 @@ nm_device_can_interrupt_activation (NMDevice *self)
/* IP Configuration stuff */
static void
handle_dhcp_lease_change (NMDevice *device)
{
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device);
NMIP4Config *config;
NMSettingIP4Config *s_ip4;
NMConnection *connection;
NMActRequest *req;
NMDeviceStateReason reason = NM_DEVICE_STATE_REASON_NONE;
gboolean assumed;
if (!nm_device_get_use_dhcp (device)) {
nm_warning ("got DHCP rebind for device that wasn't using DHCP.");
return;
}
config = nm_dhcp_manager_get_ip4_config (priv->dhcp_manager, priv->dhcp4_id);
if (!config) {
nm_warning ("failed to get DHCP config for rebind");
nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED);
return;
}
req = nm_device_get_act_request (device);
g_assert (req);
connection = nm_act_request_get_connection (req);
g_assert (connection);
s_ip4 = NM_SETTING_IP4_CONFIG (nm_connection_get_setting (connection, NM_TYPE_SETTING_IP4_CONFIG));
nm_utils_merge_ip4_config (config, s_ip4);
g_object_set_data (G_OBJECT (req), NM_ACT_REQUEST_IP4_CONFIG, config);
assumed = nm_act_request_get_assumed (req);
if (nm_device_set_ip4_config (device, config, assumed, &reason)) {
nm_dhcp4_config_reset (priv->dhcp4_config);
nm_dhcp_manager_foreach_dhcp4_option (priv->dhcp_manager,
priv->dhcp4_id,
dhcp4_add_option_cb,
priv->dhcp4_config);
} else {
nm_warning ("Failed to update IP4 config in response to DHCP event.");
nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, reason);
}
}
static void
dhcp_state_changed (NMDHCPManager *dhcp_manager,
guint32 id,
NMDHCPState state,
gpointer user_data)
{
NMDevice *device = NM_DEVICE (user_data);
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device);
NMDeviceState dev_state;
if ( !nm_device_get_act_request (device)
|| (priv->dhcp4_id != id))
return;
dev_state = nm_device_get_state (device);
switch (state) {
case DHC_BOUND4: /* lease obtained */
case DHC_RENEW4: /* lease renewed */
case DHC_REBOOT: /* have valid lease, but now obtained a different one */
case DHC_REBIND4: /* new, different lease */
if (dev_state == NM_DEVICE_STATE_IP_CONFIG)
nm_device_activate_schedule_stage4_ip4_config_get (device);
else if (dev_state == NM_DEVICE_STATE_ACTIVATED)
handle_dhcp_lease_change (device);
break;
case DHC_TIMEOUT: /* timed out contacting DHCP server */
nm_dhcp4_config_reset (priv->dhcp4_config);
if (nm_device_get_state (device) == NM_DEVICE_STATE_IP_CONFIG)
nm_device_activate_schedule_stage4_ip4_config_timeout (device);
break;
case DHC_FAIL: /* all attempts to contact server timed out, sleeping */
case DHC_ABEND: /* dhclient exited abnormally */
case DHC_END: /* dhclient exited normally */
nm_dhcp4_config_reset (priv->dhcp4_config);
if (nm_device_get_state (device) == NM_DEVICE_STATE_IP_CONFIG) {
nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_DHCP_FAILED);
} else if (nm_device_get_state (device) == NM_DEVICE_STATE_ACTIVATED) {
if (nm_device_get_use_dhcp (device)) {
/* dhclient quit and therefore can't renew our lease, kill the conneciton */
nm_device_state_changed (device, NM_DEVICE_STATE_FAILED, NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED);
}
}
break;
default:
break;
}
}
static void
dhcp_timeout (NMDHCPManager *dhcp_manager,
guint32 id,
gpointer user_data)
{
NMDevice *device = NM_DEVICE (user_data);
NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (device);
if ( !nm_device_get_act_request (device)
|| (priv->dhcp4_id != id))
return;
if (nm_device_get_state (device) == NM_DEVICE_STATE_IP_CONFIG)
nm_device_activate_schedule_stage4_ip4_config_timeout (device);
}
gboolean
nm_device_get_use_dhcp (NMDevice *self)
{
@ -2390,17 +2386,8 @@ nm_device_set_use_dhcp (NMDevice *self,
g_object_unref (priv->dhcp4_config);
priv->dhcp4_config = nm_dhcp4_config_new ();
if (!priv->dhcp_manager) {
if (!priv->dhcp_manager)
priv->dhcp_manager = nm_dhcp_manager_get ();
priv->dhcp_state_sigid = g_signal_connect (priv->dhcp_manager,
"state-changed",
G_CALLBACK (dhcp_state_changed),
self);
priv->dhcp_timeout_sigid = g_signal_connect (priv->dhcp_manager,
"timeout",
G_CALLBACK (dhcp_timeout),
self);
}
} else {
if (priv->dhcp4_config) {
g_object_notify (G_OBJECT (self), NM_DEVICE_INTERFACE_DHCP4_CONFIG);
@ -2408,11 +2395,25 @@ nm_device_set_use_dhcp (NMDevice *self,
priv->dhcp4_config = NULL;
}
if (priv->dhcp4_client) {
/* Stop any ongoing DHCP transaction on this device */
if (priv->dhcp_state_sigid) {
g_signal_handler_disconnect (priv->dhcp4_client, priv->dhcp_state_sigid);
priv->dhcp_state_sigid = 0;
}
if (priv->dhcp_timeout_sigid) {
g_signal_handler_disconnect (priv->dhcp4_client, priv->dhcp_timeout_sigid);
priv->dhcp_timeout_sigid = 0;
}
nm_dhcp_client_stop (priv->dhcp4_client);
g_object_unref (priv->dhcp4_client);
priv->dhcp4_client = NULL;
}
if (priv->dhcp_manager) {
g_signal_handler_disconnect (priv->dhcp_manager, priv->dhcp_state_sigid);
priv->dhcp_state_sigid = 0;
g_signal_handler_disconnect (priv->dhcp_manager, priv->dhcp_timeout_sigid);
priv->dhcp_timeout_sigid = 0;
g_object_unref (priv->dhcp_manager);
priv->dhcp_manager = NULL;
}
@ -2770,6 +2771,7 @@ dispose (GObject *object)
nm_device_take_down (self, FALSE, NM_DEVICE_STATE_REASON_REMOVED);
nm_device_set_ip4_config (self, NULL, FALSE, &ignored);
nm_device_set_use_dhcp (self, FALSE);
}
clear_act_request (self);
@ -2777,10 +2779,7 @@ dispose (GObject *object)
activation_source_clear (self, TRUE, AF_INET);
activation_source_clear (self, TRUE, AF_INET6);
if (!take_down) {
nm_device_set_use_dhcp (self, FALSE);
nm_device_cleanup_ip6 (self);
}
nm_device_cleanup_ip6 (self);
if (priv->dnsmasq_manager) {
if (priv->dnsmasq_state_id) {

View file

@ -35,15 +35,43 @@ typedef struct {
const char *value;
} Option;
static void
destroy_gvalue (gpointer data)
{
GValue *value = (GValue *) data;
g_value_unset (value);
g_slice_free (GValue, value);
}
static GValue *
string_to_byte_array_gvalue (const char *str)
{
GByteArray *array;
GValue *val;
array = g_byte_array_sized_new (strlen (str));
g_byte_array_append (array, (const guint8 *) str, strlen (str));
val = g_slice_new0 (GValue);
g_value_init (val, DBUS_TYPE_G_UCHAR_ARRAY);
g_value_take_boxed (val, array);
return val;
}
static GHashTable *
fill_table (Option *test_options, GHashTable *table)
{
Option *opt;
if (!table)
table = g_hash_table_new (g_str_hash, g_str_equal);
for (opt = test_options; opt->name; opt++)
g_hash_table_insert (table, (gpointer) opt->name, (gpointer) opt->value);
table = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, destroy_gvalue);
for (opt = test_options; opt->name; opt++) {
g_hash_table_insert (table,
(gpointer) opt->name,
string_to_byte_array_gvalue (opt->value));
}
return table;
}
@ -88,7 +116,7 @@ test_generic_options (void)
const char *expected_route2_gw = "10.1.1.1";
options = fill_table (generic_options, NULL);
ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options);
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
ASSERT (ip4_config != NULL,
"dhcp-generic", "failed to parse DHCP4 options");
@ -199,7 +227,7 @@ test_wins_options (void)
options = fill_table (generic_options, NULL);
options = fill_table (wins_options, options);
ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options);
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
ASSERT (ip4_config != NULL,
"dhcp-wins", "failed to parse DHCP4 options");
@ -245,7 +273,7 @@ test_classless_static_routes (void)
options = fill_table (generic_options, NULL);
options = fill_table (classless_routes_options, options);
ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options);
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
ASSERT (ip4_config != NULL,
"dhcp-rfc3442", "failed to parse DHCP4 options");
@ -311,7 +339,7 @@ test_invalid_classless_routes1 (void)
options = fill_table (generic_options, NULL);
options = fill_table (invalid_classless_routes1, options);
ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options);
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
ASSERT (ip4_config != NULL,
"dhcp-rfc3442-invalid-1", "failed to parse DHCP4 options");
@ -362,7 +390,7 @@ test_invalid_classless_routes2 (void)
options = fill_table (generic_options, NULL);
options = fill_table (invalid_classless_routes2, options);
ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options);
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
ASSERT (ip4_config != NULL,
"dhcp-rfc3442-invalid-2", "failed to parse DHCP4 options");
@ -432,7 +460,7 @@ test_invalid_classless_routes3 (void)
options = fill_table (generic_options, NULL);
options = fill_table (invalid_classless_routes3, options);
ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options);
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
ASSERT (ip4_config != NULL,
"dhcp-rfc3442-invalid-3", "failed to parse DHCP4 options");
@ -483,7 +511,7 @@ test_gateway_in_classless_routes (void)
options = fill_table (generic_options, NULL);
options = fill_table (gw_in_classless_routes, options);
ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options);
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
ASSERT (ip4_config != NULL,
"dhcp-rfc3442-gateway", "failed to parse DHCP4 options");
@ -537,7 +565,7 @@ test_escaped_domain_searches (void)
options = fill_table (generic_options, NULL);
options = fill_table (escaped_searches_options, options);
ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options);
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
ASSERT (ip4_config != NULL,
"dhcp-escaped-domain-searches", "failed to parse DHCP4 options");
@ -568,7 +596,7 @@ test_invalid_escaped_domain_searches (void)
options = fill_table (generic_options, NULL);
options = fill_table (invalid_escaped_searches_options, options);
ip4_config = nm_dhcp4_manager_options_to_config ("eth0", options);
ip4_config = nm_dhcp_manager_test_ip4_options_to_config ("eth0", options, "rebind");
ASSERT (ip4_config != NULL,
"dhcp-invalid-escaped-domain-searches", "failed to parse DHCP4 options");