diff --git a/src/Makefile.am b/src/Makefile.am index f351c787d1..60aa93ee15 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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) \ diff --git a/src/dhcp-manager/Makefile.am b/src/dhcp-manager/Makefile.am index b7b0c6581c..711c2a5058 100644 --- a/src/dhcp-manager/Makefile.am +++ b/src/dhcp-manager/Makefile.am @@ -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 diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c new file mode 100644 index 0000000000..e39c9bf312 --- /dev/null +++ b/src/dhcp-manager/nm-dhcp-client.c @@ -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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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); +} + diff --git a/src/dhcp-manager/nm-dhcp-client.h b/src/dhcp-manager/nm-dhcp-client.h new file mode 100644 index 0000000000..0e5d81229d --- /dev/null +++ b/src/dhcp-manager/nm-dhcp-client.h @@ -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 +#include + +#include + +#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 */ + diff --git a/src/dhcp-manager/nm-dhcp-dhclient.c b/src/dhcp-manager/nm-dhcp-dhclient.c index e0deabb4a8..c47d155b0f 100644 --- a/src/dhcp-manager/nm-dhcp-dhclient.c +++ b/src/dhcp-manager/nm-dhcp-dhclient.c @@ -36,46 +36,37 @@ #include -#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; +} + diff --git a/src/dhcp-manager/nm-dhcp-dhclient.h b/src/dhcp-manager/nm-dhcp-dhclient.h new file mode 100644 index 0000000000..bad6452f79 --- /dev/null +++ b/src/dhcp-manager/nm-dhcp-dhclient.h @@ -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 +#include + +#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 */ + diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.c b/src/dhcp-manager/nm-dhcp-dhcpcd.c index 6ba3f0a91c..ba210eda63 100644 --- a/src/dhcp-manager/nm-dhcp-dhcpcd.c +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.c @@ -2,6 +2,7 @@ /* nm-dhcp-dhcpcd.c - dhcpcd specific hooks for NetworkManager * * Copyright (C) 2008 Roy Marples + * Copyright (C) 2010 Dan Williams * * 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 #include -#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; +} + diff --git a/src/dhcp-manager/nm-dhcp-dhcpcd.h b/src/dhcp-manager/nm-dhcp-dhcpcd.h new file mode 100644 index 0000000000..f564bc7f71 --- /dev/null +++ b/src/dhcp-manager/nm-dhcp-dhcpcd.h @@ -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 +#include + +#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 */ + diff --git a/src/dhcp-manager/nm-dhcp-manager.c b/src/dhcp-manager/nm-dhcp-manager.c index 2b48a2651d..f1a84cdd46 100644 --- a/src/dhcp-manager/nm-dhcp-manager.c +++ b/src/dhcp-manager/nm-dhcp-manager.c @@ -25,8 +25,6 @@ #include #include #include -#include -#include #include #include #include @@ -37,6 +35,7 @@ #include #include "nm-dhcp-manager.h" +#include "nm-dhcp-dhclient.h" #include "nm-marshal.h" #include "nm-utils.h" #include "nm-dbus-manager.h" @@ -47,13 +46,11 @@ #define NM_DHCP_CLIENT_DBUS_SERVICE "org.freedesktop.nm_dhcp_client" #define NM_DHCP_CLIENT_DBUS_IFACE "org.freedesktop.nm_dhcp_client" -#define NM_DHCP_TIMEOUT 45 /* DHCP timeout, in seconds */ - typedef struct { - guint32 next_id; - NMDBusManager * dbus_mgr; - GHashTable * clients; - DBusGProxy * proxy; + guint32 next_id; + NMDBusManager * dbus_mgr; + GHashTable * clients; + DBusGProxy * proxy; NMHostnameProvider *hostname_provider; } NMDHCPManagerPrivate; @@ -62,131 +59,6 @@ typedef struct { G_DEFINE_TYPE (NMDHCPManager, nm_dhcp_manager, G_TYPE_OBJECT) -enum { - STATE_CHANGED, - TIMEOUT, - LAST_SIGNAL -}; - -static guint signals[LAST_SIGNAL] = { 0 }; - -static void nm_dhcp_manager_cancel_transaction_real (NMDHCPClient *client); - -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; -} - - -static void -nm_dhcp_client_timeout_cleanup (NMDHCPClient *client) -{ - if (client->timeout_id) { - g_source_remove (client->timeout_id); - client->timeout_id = 0; - } -} - -static void -nm_dhcp_client_watch_cleanup (NMDHCPClient *client) -{ - if (client->watch_id) { - g_source_remove (client->watch_id); - client->watch_id = 0; - } -} - -static void -nm_dhcp_client_destroy (NMDHCPClient *client) -{ - int ignored; - - nm_dhcp_client_timeout_cleanup (client); - - if (client->pid) - nm_dhcp_client_stop (client, client->pid); - - if (client->options) - g_hash_table_destroy (client->options); - - if (client->conf_file) { - ignored = unlink (client->conf_file); - g_free (client->conf_file); - } - - g_free (client->pid_file); - g_free (client->lease_file); - g_free (client->iface); - - g_slice_free (NMDHCPClient, client); -} - -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) { @@ -220,81 +92,6 @@ garray_to_string (GArray *array, const char *key) return converted; } -static char * -get_option (GHashTable *hash, const char *key) -{ - GValue *value; - - value = g_hash_table_lookup (hash, key); - if (value == NULL) - return NULL; - - if (G_VALUE_TYPE (value) != DBUS_TYPE_G_UCHAR_ARRAY) { - nm_warning ("Unexpected key %s value type was not " - "DBUS_TYPE_G_UCHAR_ARRAY", - (char *) key); - return NULL; - } - - return garray_to_string ((GArray *) g_value_get_boxed (value), key); -} - -static void -copy_option (gpointer key, - gpointer value, - gpointer user_data) -{ - NMDHCPClient *client = user_data; - const char *str_key = (const char *) key; - char *str_value = NULL; - - if (G_VALUE_TYPE (value) != DBUS_TYPE_G_UCHAR_ARRAY) { - nm_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 (client->options, g_strdup (str_key), str_value); -} - -static void -handle_options (NMDHCPManager *manager, - NMDHCPClient *client, - GHashTable *options, - const char *reason) -{ - guint32 old_state = client->state; - guint32 new_state = string_to_state (reason); - - /* Clear old and save new DHCP options */ - g_hash_table_remove_all (client->options); - g_hash_table_foreach (options, copy_option, client); - - 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 */ - nm_dhcp_client_timeout_cleanup (client); - } - - client->state = new_state; - nm_info ("DHCP: device %s state changed %s -> %s", - client->iface, - state_to_string (old_state), - state_to_string (client->state)); - - g_signal_emit (G_OBJECT (client->manager), - signals[STATE_CHANGED], - 0, - client->id, - client->state); -} - static NMDHCPClient * get_client_for_pid (NMDHCPManager *manager, GPid pid) { @@ -309,9 +106,9 @@ get_client_for_pid (NMDHCPManager *manager, GPid pid) g_hash_table_iter_init (&iter, priv->clients); while (g_hash_table_iter_next (&iter, NULL, &value)) { - NMDHCPClient *candidate = value; + NMDHCPClient *candidate = NM_DHCP_CLIENT (value); - if (candidate->pid == pid) + if (nm_dhcp_client_get_pid (candidate) == pid) return candidate; } @@ -319,7 +116,8 @@ get_client_for_pid (NMDHCPManager *manager, GPid pid) } static NMDHCPClient * -get_client_for_iface (NMDHCPManager *manager, const char *iface) +get_client_for_iface (NMDHCPManager *manager, + const char *iface) { NMDHCPManagerPrivate *priv; GHashTableIter iter; @@ -333,24 +131,32 @@ get_client_for_iface (NMDHCPManager *manager, const char *iface) g_hash_table_iter_init (&iter, priv->clients); while (g_hash_table_iter_next (&iter, NULL, &value)) { - NMDHCPClient *candidate = value; + NMDHCPClient *candidate = NM_DHCP_CLIENT (value); - if (!strcmp (iface, candidate->iface)) + if (!strcmp (iface, nm_dhcp_client_get_iface (candidate))) return candidate; } return NULL; } -static NMDHCPClient * -get_client_for_id (NMDHCPManager *manager, guint32 id) +static char * +get_option (GHashTable *hash, const char *key) { - g_return_val_if_fail (manager != NULL, NULL); - g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL); - g_return_val_if_fail (id > 0, NULL); + GValue *value; - return g_hash_table_lookup (NM_DHCP_MANAGER_GET_PRIVATE (manager)->clients, - GUINT_TO_POINTER (id)); + value = g_hash_table_lookup (hash, key); + if (value == NULL) + return 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", + (char *) key); + return NULL; + } + + return garray_to_string ((GArray *) g_value_get_boxed (value), key); } static void @@ -393,9 +199,9 @@ nm_dhcp_manager_handle_event (DBusGProxy *proxy, goto out; } - if (strcmp (iface, client->iface)) { + if (strcmp (iface, nm_dhcp_client_get_iface (client))) { nm_warning ("Received DHCP event from unexpected interface '%s' (expected '%s')", - iface, client->iface); + iface, nm_dhcp_client_get_iface (client)); goto out; } @@ -405,7 +211,7 @@ nm_dhcp_manager_handle_event (DBusGProxy *proxy, goto out; } - handle_options (manager, client, options, reason); + nm_dhcp_client_new_options (client, options, reason); out: g_free (iface); @@ -425,7 +231,7 @@ nm_dhcp_manager_new (void) priv->clients = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, - (GDestroyNotify) nm_dhcp_client_destroy); + (GDestroyNotify) g_object_unref); if (!priv->clients) { nm_warning ("Error: not enough memory to initialize DHCP manager tables"); g_object_unref (manager); @@ -457,599 +263,120 @@ nm_dhcp_manager_new (void) return manager; } +#define STATE_ID_TAG "state-id" +#define TIMEOUT_ID_TAG "timeout-id" -/* - * nm_dhcp_manager_handle_timeout - * - * Called after timeout of a DHCP transaction to notify device of the failure. - * - */ -static gboolean -nm_dhcp_manager_handle_timeout (gpointer user_data) +static void +remove_client (NMDHCPManager *self, NMDHCPClient *client) { - NMDHCPClient *client = user_data; + NMDHCPManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self); + guint id; - nm_info ("(%s): DHCP transaction took too long, stopping it.", client->iface); + id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (client), STATE_ID_TAG)); + if (id) + g_signal_handler_disconnect (client, id); - nm_dhcp_manager_cancel_transaction (client->manager, client->id); + id = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (client), TIMEOUT_ID_TAG)); + if (id) + g_signal_handler_disconnect (client, id); - g_signal_emit (G_OBJECT (client->manager), signals[TIMEOUT], 0, client->id); + /* Stopping the client is left up to the controlling device + * explicitly since we may want to quit NetworkManager but not terminate + * the DHCP client. + */ - return FALSE; + g_hash_table_remove (priv->clients, client); } -static NMDHCPClient * -nm_dhcp_client_new (NMDHCPManager *manager, const char *iface) +static void +client_state_changed (NMDHCPClient *client, NMDHCPState new_state, gpointer user_data) { - NMDHCPManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); - NMDHCPClient *client; - - client = g_slice_new0 (NMDHCPClient); - if (!client) { - nm_warning ("%s: Out of memory creating DHCP transaction object.", iface); - return NULL; - } - - client->id = priv->next_id++; - client->iface = g_strdup (iface); - if (!client) { - nm_warning ("%s: Out of memory creating DHCP transaction object " - "property 'iface'.", - iface); - goto error; - } - - client->manager = manager; - - /* Do this after the transaction cancel since that clears options out */ - client->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - if (!client->options) { - nm_warning ("%s: Out of memory creating DHCP transaction object " - "property 'options'.", - iface); - goto error; - } - - g_hash_table_insert (priv->clients, GUINT_TO_POINTER (client->id), client); - return client; - -error: - nm_dhcp_client_destroy (client); - return NULL; + if (new_state == DHC_ABEND || new_state == DHC_END) + remove_client (NM_DHCP_MANAGER (user_data), client); } - -/* - * dhcp_watch_cb - * - * Watch our child dhclient process and get notified of events from it. - * - */ -static void dhcp_watch_cb (GPid pid, gint status, gpointer user_data) +static void +client_timeout (NMDHCPClient *client, gpointer user_data) { - NMDHCPClient *client = user_data; - - if (!WIFEXITED (status)) { - client->state = DHC_ABEND; - nm_warning ("dhcp client died abnormally"); - } - client->pid = 0; - - nm_dhcp_client_watch_cleanup (client); - nm_dhcp_client_timeout_cleanup (client); - - g_signal_emit (G_OBJECT (client->manager), signals[STATE_CHANGED], 0, client->id, client->state); + remove_client (NM_DHCP_MANAGER (user_data), client); } -guint32 -nm_dhcp_manager_begin_transaction (NMDHCPManager *manager, - const char *iface, - const char *uuid, - NMSettingIP4Config *s_ip4, - guint32 timeout, - guint8 *dhcp_anycast_addr) +static void +add_client (NMDHCPManager *self, NMDHCPClient *client) +{ + NMDHCPManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self); + guint id; + + id = g_signal_connect (client, "state-changed", G_CALLBACK (client_state_changed), self); + g_object_set_data (G_OBJECT (client), STATE_ID_TAG, GUINT_TO_POINTER (id)); + + id = g_signal_connect (client, "timeout", G_CALLBACK (client_timeout), self); + g_object_set_data (G_OBJECT (client), TIMEOUT_ID_TAG, GUINT_TO_POINTER (id)); + + g_hash_table_insert (priv->clients, client, g_object_ref (client)); +} + +/* Caller owns a reference to the NMDHCPClient on return */ +NMDHCPClient * +nm_dhcp_manager_start_client (NMDHCPManager *self, + const char *iface, + const char *uuid, + NMSettingIP4Config *s_ip4, + guint32 timeout, + guint8 *dhcp_anycast_addr) { NMDHCPManagerPrivate *priv; NMDHCPClient *client; - NMSettingIP4Config *setting; + NMSettingIP4Config *setting = NULL; + gboolean success = FALSE; - g_return_val_if_fail (manager, 0); - g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), 0); - g_return_val_if_fail (iface != NULL, 0); + g_return_val_if_fail (self, NULL); + g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL); + g_return_val_if_fail (iface != NULL, NULL); - priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); + priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - client = get_client_for_iface (manager, iface); - if (!client) - client = nm_dhcp_client_new (manager, iface); - - if (client->pid && (client->state < DHC_ABEND)) { - /* Cancel any DHCP transaction already in progress */ - nm_dhcp_manager_cancel_transaction_real (client); + /* Kill any old client instance */ + client = get_client_for_iface (self, iface); + if (client) { + nm_dhcp_client_stop (client); + remove_client (self, client); } - if (s_ip4 && - nm_setting_ip4_config_get_dhcp_send_hostname (s_ip4) && - nm_setting_ip4_config_get_dhcp_hostname (s_ip4) == NULL && - priv->hostname_provider != NULL) { - /* We're asked to send the hostname to DHCP server, - the hostname isn't specified, - and a hostname provider is registered: use that */ + /* And make a new one */ + client = g_object_new (NM_TYPE_DHCP_DHCLIENT, + NM_DHCP_CLIENT_INTERFACE, iface, + NULL); + g_return_val_if_fail (client != NULL, NULL); + add_client (self, client); + if ( s_ip4 + && nm_setting_ip4_config_get_dhcp_send_hostname (s_ip4) + && (nm_setting_ip4_config_get_dhcp_hostname (s_ip4) == NULL) + && priv->hostname_provider != NULL) { + + /* We're asked to send the hostname to DHCP server, the hostname + * isn't specified, and a hostname provider is registered: use that + */ setting = NM_SETTING_IP4_CONFIG (nm_setting_duplicate (NM_SETTING (s_ip4))); g_object_set (G_OBJECT (setting), NM_SETTING_IP4_CONFIG_DHCP_HOSTNAME, nm_hostname_provider_get_hostname (priv->hostname_provider), NULL); - } else - setting = s_ip4 ? g_object_ref (s_ip4) : NULL; + } - if (timeout == 0) - timeout = NM_DHCP_TIMEOUT; - - nm_info ("Activation (%s) Beginning DHCP transaction (timeout in %d seconds)", - iface, timeout); - client->pid = nm_dhcp4_client_start (client, uuid, setting, dhcp_anycast_addr); + success = nm_dhcp_client_start (client, uuid, setting, timeout, dhcp_anycast_addr); if (setting) g_object_unref (setting); - if (client->pid == 0) - return FALSE; - - /* Set up a timeout on the transaction to kill it after the timeout */ - client->timeout_id = g_timeout_add_seconds (timeout, - nm_dhcp_manager_handle_timeout, - client); - client->watch_id = g_child_watch_add (client->pid, - (GChildWatchFunc) dhcp_watch_cb, - client); - return client->id; -} - -void -nm_dhcp_client_stop (NMDHCPClient *client, GPid pid) -{ - int i = 15; /* 3 seconds */ - - g_return_if_fail (client != NULL); - g_return_if_fail (pid > 0); - - /* Clean up the watch handler since we're explicitly killing the daemon */ - nm_dhcp_client_watch_cleanup (client); - - /* 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 (!success) { + remove_client (self, client); + g_object_unref (client); + client = NULL; } - if (i <= 0) { - nm_warning ("%s: dhcp client pid %d didn't exit, will kill it.", client->iface, pid); - kill (pid, SIGKILL); - - nm_debug ("waiting for dhcp client pid %d to exit", pid); - waitpid (pid, NULL, 0); - nm_debug ("dhcp client pid %d cleaned up", pid); - } -} - -static void -nm_dhcp_manager_cancel_transaction_real (NMDHCPClient *client) -{ - g_return_if_fail (client != NULL); - g_return_if_fail (client->pid > 0); - - nm_dhcp_client_stop (client, client->pid); - - nm_info ("(%s): canceled DHCP transaction, dhcp client pid %d", - client->iface, - client->pid); - - client->pid = 0; - client->state = DHC_END; - - /* Clean up the pidfile if it got left around */ - if (client->pid_file) { - remove (client->pid_file); - g_free (client->pid_file); - client->pid_file = NULL; - } - - /* Free leasefile (but don't delete) */ - if (client->lease_file) { - g_free (client->lease_file); - client->lease_file = NULL; - } - - /* Clean up config file if it got left around */ - if (client->conf_file) { - remove (client->conf_file); - g_free (client->conf_file); - client->conf_file = NULL; - } - - nm_dhcp_client_timeout_cleanup (client); - g_hash_table_remove_all (client->options); -} - - -/* - * nm_dhcp_manager_cancel_transaction - * - * Stop any in-progress DHCP transaction on a particular device. - * - */ -void -nm_dhcp_manager_cancel_transaction (NMDHCPManager *manager, guint32 id) -{ - NMDHCPClient *client; - NMDHCPManagerPrivate *priv; - - g_return_if_fail (manager != NULL); - g_return_if_fail (NM_IS_DHCP_MANAGER (manager)); - g_return_if_fail (id > 0); - - priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); - - client = get_client_for_id (manager, id); - if (client && client->pid) - nm_dhcp_manager_cancel_transaction_real (client); -} - -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)) { - nm_info (" 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) { - nm_warning ("DHCP provided invalid static route address: '%s'", *s); - continue; - } - if (inet_pton (AF_INET, *(s + 1), &rt_route) <= 0) { - nm_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); - nm_info (" 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, '\\')) { - nm_info (" invalid domain search: '%s'", unescaped); - goto out; - } - - searches = g_strsplit (unescaped, " ", 0); - for (s = searches; *s; s++) { - if (strlen (*s)) { - nm_info (" 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 */ -NMIP4Config * -nm_dhcp4_manager_options_to_config (const char *iface, GHashTable *options) -{ - 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 (iface != NULL, NULL); - g_return_val_if_fail (options != NULL, NULL); - - ip4_config = nm_ip4_config_new (); - if (!ip4_config) { - nm_warning ("%s: couldn't allocate memory for an IP4Config!", iface); - return NULL; - } - - addr = nm_ip4_address_new (); - if (!addr) { - nm_warning ("%s: couldn't allocate memory for an IP4 Address!", iface); - goto error; - } - - str = g_hash_table_lookup (options, "new_ip_address"); - if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) { - nm_ip4_address_set_address (addr, tmp_addr.s_addr); - nm_info (" address %s", str); - } else - goto error; - - str = g_hash_table_lookup (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)); - nm_info (" 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. - */ - have_classless = nm_dhcp4_client_process_classless_routes (options, ip4_config, &gwaddr); - if (!have_classless) { - gwaddr = 0; /* Ensure client code doesn't lie */ - process_classful_routes (options, ip4_config); - } - - if (gwaddr) { - char buf[INET_ADDRSTRLEN + 1]; - - inet_ntop (AF_INET, &gwaddr, buf, sizeof (buf)); - nm_info (" 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 (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); - nm_info (" gateway %s", *s); - break; - } else - nm_warning ("Ignoring invalid gateway '%s'", *s); - } - g_strfreev (routers); - } - } - - nm_ip4_config_take_address (ip4_config, addr); - addr = NULL; - - str = g_hash_table_lookup (options, "new_host_name"); - if (str) - nm_info (" hostname '%s'", str); - - str = g_hash_table_lookup (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); - nm_info (" nameserver '%s'", *s); - } else - nm_warning ("Ignoring invalid nameserver '%s'", *s); - } - g_strfreev (searches); - } - - str = g_hash_table_lookup (options, "new_domain_name"); - if (str) { - char **domains = g_strsplit (str, " ", 0); - char **s; - - for (s = domains; *s; s++) { - nm_info (" domain name '%s'", *s); - nm_ip4_config_add_domain (ip4_config, *s); - } - g_strfreev (domains); - } - - str = g_hash_table_lookup (options, "new_domain_search"); - if (str) - process_domain_search (ip4_config, str); - - str = g_hash_table_lookup (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); - nm_info (" wins '%s'", *s); - } else - nm_warning ("Ignoring invalid WINS server '%s'", *s); - } - g_strfreev (searches); - } - - str = g_hash_table_lookup (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; -} - -/* - * nm_dhcp_manager_get_ip4_config - * - * Get IP4 configuration values from the DHCP daemon - * - */ -NMIP4Config * -nm_dhcp_manager_get_ip4_config (NMDHCPManager *manager, guint32 id) -{ - NMDHCPManagerPrivate *priv; - NMDHCPClient *client; - - g_return_val_if_fail (manager != NULL, NULL); - g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL); - g_return_val_if_fail (id > 0, NULL); - - priv = NM_DHCP_MANAGER_GET_PRIVATE (manager); - - client = get_client_for_id (manager, id); - if (!client) { - nm_warning ("Device DHCP transaction %d not started.", id); - return NULL; - } - - if (!state_is_bound (client->state)) { - nm_warning ("%s: dhcp client didn't bind to a lease.", client->iface); - return NULL; - } - - return nm_dhcp4_manager_options_to_config (client->iface, client->options); -} - -#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_manager_foreach_dhcp4_option (NMDHCPManager *self, - guint32 id, - GHFunc func, - gpointer user_data) -{ - NMDHCPManagerPrivate *priv; - NMDHCPClient *client; - Dhcp4ForeachInfo info = { NULL, NULL }; - - g_return_val_if_fail (self != NULL, FALSE); - g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), FALSE); - g_return_val_if_fail (id > 0, FALSE); - g_return_val_if_fail (func != NULL, FALSE); - - priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - - client = get_client_for_id (self, id); - if (!client) { - nm_warning ("Device DHCP transaction %d not started.", id); - return FALSE; - } - - if (!state_is_bound (client->state)) { - nm_warning ("%s: dhclient didn't bind to a lease.", client->iface); - return FALSE; - } - - info.func = func; - info.user_data = user_data; - g_hash_table_foreach (client->options, iterate_dhcp4_config_option, &info); - return TRUE; + return client; } static void @@ -1080,16 +407,35 @@ nm_dhcp_manager_set_hostname_provider (NMDHCPManager *manager, } GSList * -nm_dhcp4_manager_get_lease_config (NMDHCPManager *self, - const char *iface, - const char *uuid) +nm_dhcp_manager_get_lease_config (NMDHCPManager *self, + const char *iface, + const char *uuid) { g_return_val_if_fail (self != NULL, NULL); g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL); g_return_val_if_fail (iface != NULL, NULL); g_return_val_if_fail (uuid != NULL, NULL); - return nm_dhcp4_client_get_lease_config (iface, uuid); + return nm_dhcp_backend_get_lease_config (iface, uuid); +} + +NMIP4Config * +nm_dhcp_manager_test_ip4_options_to_config (const char *iface, + GHashTable *options, + const char *reason) +{ + NMDHCPClient *client; + NMIP4Config *config; + + client = (NMDHCPClient *) g_object_new (NM_TYPE_DHCP_DHCLIENT, + NM_DHCP_CLIENT_INTERFACE, iface, + NULL); + g_return_val_if_fail (client != NULL, NULL); + nm_dhcp_client_new_options (client, options, reason); + config = nm_dhcp_client_get_ip4_config (client, TRUE); + g_object_unref (client); + + return config; } /***************************************************/ @@ -1116,6 +462,20 @@ nm_dhcp_manager_init (NMDHCPManager *manager) priv->next_id = 1; } +static void +dispose (GObject *object) +{ + NMDHCPManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (object); + GList *values, *iter; + + values = g_hash_table_get_values (priv->clients); + for (iter = values; iter; iter = g_list_next (iter)) + remove_client (NM_DHCP_MANAGER (object), NM_DHCP_CLIENT (iter->data)); + g_list_free (values); + + G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->dispose (object); +} + static void finalize (GObject *object) { @@ -1142,24 +502,6 @@ nm_dhcp_manager_class_init (NMDHCPManagerClass *manager_class) /* virtual methods */ object_class->finalize = finalize; - - /* signals */ - signals[STATE_CHANGED] = - g_signal_new ("state-changed", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (NMDHCPManagerClass, state_changed), - NULL, NULL, - _nm_marshal_VOID__UINT_UINT, - G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_UINT); - - signals[TIMEOUT] = - g_signal_new ("timeout", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_FIRST, - G_STRUCT_OFFSET (NMDHCPManagerClass, timeout), - NULL, NULL, - g_cclosure_marshal_VOID__UINT, - G_TYPE_NONE, 1, G_TYPE_UINT); + object_class->dispose = dispose; } diff --git a/src/dhcp-manager/nm-dhcp-manager.h b/src/dhcp-manager/nm-dhcp-manager.h index 3b8a6a410e..7c82ca59ac 100644 --- a/src/dhcp-manager/nm-dhcp-manager.h +++ b/src/dhcp-manager/nm-dhcp-manager.h @@ -27,12 +27,11 @@ #include +#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 */ diff --git a/src/nm-device-ethernet.c b/src/nm-device-ethernet.c index 72073e4150..f20afcc45c 100644 --- a/src/nm-device-ethernet.c +++ b/src/nm-device-ethernet.c @@ -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); diff --git a/src/nm-device.c b/src/nm-device.c index 4be3fc23e3..586c93949c 100644 --- a/src/nm-device.c +++ b/src/nm-device.c @@ -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) { diff --git a/src/tests/test-dhcp-options.c b/src/tests/test-dhcp-options.c index a99feea9db..6f5fb011f0 100644 --- a/src/tests/test-dhcp-options.c +++ b/src/tests/test-dhcp-options.c @@ -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");