NetworkManager/src/dhcp-manager/nm-dhcp-client.c

1133 lines
29 KiB
C
Raw Normal View History

2010-01-12 22:09:28 -08:00
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright (C) 2005 - 2010 Red Hat, Inc.
*
*/
#include <glib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include "nm-utils.h"
2010-04-06 18:06:47 -07:00
#include "nm-logging.h"
2010-01-12 22:09:28 -08:00
#include "nm-dbus-glib-types.h"
#include "nm-dhcp-client.h"
typedef struct {
char * iface;
2010-01-13 16:51:20 -08:00
gboolean ipv6;
char * uuid;
guint32 timeout;
2010-01-12 22:09:28 -08:00
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,
2010-01-13 16:51:20 -08:00
PROP_IPV6,
PROP_UUID,
PROP_TIMEOUT,
2010-01-12 22:09:28 -08:00
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;
}
2010-01-13 16:51:20 -08:00
gboolean
nm_dhcp_client_get_ipv6 (NMDHCPClient *self)
{
g_return_val_if_fail (self != NULL, FALSE);
g_return_val_if_fail (NM_IS_DHCP_CLIENT (self), FALSE);
return NM_DHCP_CLIENT_GET_PRIVATE (self)->ipv6;
}
const char *
nm_dhcp_client_get_uuid (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)->uuid;
}
2010-01-12 22:09:28 -08:00
/********************************************/
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) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP, "(%s): DHCP client pid %d didn't exit, will kill it.",
iface, pid);
2010-01-12 22:09:28 -08:00
}
kill (pid, SIGKILL);
2010-04-06 18:06:47 -07:00
nm_log_dbg (LOGD_DHCP, "waiting for DHCP client pid %d to exit", pid);
2010-01-12 22:09:28 -08:00
waitpid (pid, NULL, 0);
2010-04-06 18:06:47 -07:00
nm_log_dbg (LOGD_DHCP, "DHCP client pid %d cleaned up", pid);
2010-01-12 22:09:28 -08:00
}
}
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);
2010-04-06 18:06:47 -07:00
if (priv->ipv6) {
nm_log_warn (LOGD_DHCP6, "(%s): DHCPv6 request timed out.", priv->iface);
} else {
nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 request timed out.", priv->iface);
}
2010-01-12 22:09:28 -08:00
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;
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP, "DHCP client died abnormally");
2010-01-12 22:09:28 -08:00
}
priv->pid = 0;
watch_cleanup (self);
timeout_cleanup (self);
g_signal_emit (G_OBJECT (self), signals[STATE_CHANGED], 0, priv->state);
}
2010-01-13 16:51:20 -08:00
static void
start_monitor (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv = NM_DHCP_CLIENT_GET_PRIVATE (self);
g_return_if_fail (priv->pid > 0);
/* Set up a timeout on the transaction to kill it after the timeout */
priv->timeout_id = g_timeout_add_seconds (priv->timeout,
daemon_timeout,
self);
priv->watch_id = g_child_watch_add (priv->pid,
(GChildWatchFunc) daemon_watch_cb,
self);
}
2010-01-12 22:09:28 -08:00
gboolean
2010-01-13 16:51:20 -08:00
nm_dhcp_client_start_ip4 (NMDHCPClient *self,
NMSettingIP4Config *s_ip4,
guint8 *dhcp_anycast_addr)
2010-01-12 22:09:28 -08:00
{
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);
2010-01-13 16:51:20 -08:00
g_return_val_if_fail (priv->ipv6 == FALSE, FALSE);
g_return_val_if_fail (priv->uuid != NULL, FALSE);
2010-01-12 22:09:28 -08:00
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP, "Activation (%s) Beginning DHCPv4 transaction (timeout in %d seconds)",
priv->iface, priv->timeout);
2010-01-12 22:09:28 -08:00
2010-01-13 16:51:20 -08:00
priv->pid = NM_DHCP_CLIENT_GET_CLASS (self)->ip4_start (self, s_ip4, dhcp_anycast_addr);
if (priv->pid)
start_monitor (self);
2010-01-12 22:09:28 -08:00
2010-01-13 16:51:20 -08:00
return priv->pid ? TRUE : FALSE;
}
2010-01-12 22:09:28 -08:00
2010-01-13 16:51:20 -08:00
gboolean
nm_dhcp_client_start_ip6 (NMDHCPClient *self,
NMSettingIP6Config *s_ip6,
guint8 *dhcp_anycast_addr,
gboolean info_only)
2010-01-13 16:51:20 -08:00
{
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);
g_return_val_if_fail (priv->ipv6 == TRUE, FALSE);
g_return_val_if_fail (priv->uuid != NULL, FALSE);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP, "Activation (%s) Beginning DHCPv6 transaction (timeout in %d seconds)",
priv->iface, priv->timeout);
2010-01-13 16:51:20 -08:00
priv->pid = NM_DHCP_CLIENT_GET_CLASS (self)->ip6_start (self, s_ip6, dhcp_anycast_addr, info_only);
2010-01-13 16:51:20 -08:00
if (priv->pid > 0)
start_monitor (self);
return priv->pid ? TRUE : FALSE;
2010-01-12 22:09:28 -08:00
}
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);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP, "(%s): canceled DHCP transaction, DHCP client pid %d",
priv->iface, priv->pid);
2010-01-12 22:09:28 -08:00
}
/* 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_PREINIT6,"preinit6" },
2010-01-12 22:09:28 -08:00
{ 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))
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP, "DHCP option '%s' couldn't be converted to UTF-8", key);
2010-01-12 22:09:28 -08:00
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) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP, "unexpected key %s value type was not "
"DBUS_TYPE_G_UCHAR_ARRAY",
str_key);
2010-01-12 22:09:28 -08:00
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;
2010-04-06 18:06:47 -07:00
if (priv->ipv6) {
nm_log_info (LOGD_DHCP6, "(%s): DHCPv6 state changed %s -> %s",
priv->iface,
state_to_string (old_state),
state_to_string (priv->state));
} else {
nm_log_info (LOGD_DHCP4, "(%s): DHCPv4 state changed %s -> %s",
priv->iface,
state_to_string (old_state),
state_to_string (priv->state));
}
2010-01-12 22:09:28 -08:00
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;
} DhcpForeachInfo;
2010-01-12 22:09:28 -08:00
static void
iterate_dhcp_config_option (gpointer key,
gpointer value,
gpointer user_data)
2010-01-12 22:09:28 -08:00
{
DhcpForeachInfo *info = (DhcpForeachInfo *) user_data;
2010-01-12 22:09:28 -08:00
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_option (NMDHCPClient *self,
GHFunc func,
gpointer user_data)
2010-01-12 22:09:28 -08:00
{
NMDHCPClientPrivate *priv;
DhcpForeachInfo info = { NULL, NULL };
2010-01-12 22:09:28 -08:00
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)) {
2010-04-06 18:06:47 -07:00
if (priv->ipv6) {
nm_log_warn (LOGD_DHCP6, "(%s): DHCPv6 client didn't bind to a lease.", priv->iface);
} else {
nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 client didn't bind to a lease.", priv->iface);
}
2010-01-12 22:09:28 -08:00
return FALSE;
}
info.func = func;
info.user_data = user_data;
g_hash_table_foreach (priv->options, iterate_dhcp_config_option, &info);
2010-01-12 22:09:28 -08:00
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)) {
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP, " static routes provided, but invalid");
2010-01-12 22:09:28 -08:00
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) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP, "DHCP provided invalid static route address: '%s'", *s);
2010-01-12 22:09:28 -08:00
continue;
}
if (inet_pton (AF_INET, *(s + 1), &rt_route) <= 0) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP, "DHCP provided invalid static route gateway: '%s'", *(s + 1));
2010-01-12 22:09:28 -08:00
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);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP, " static route %s gw %s", *s, *(s + 1));
2010-01-12 22:09:28 -08:00
}
out:
g_strfreev (searches);
}
static void
process_domain_search (const char *str, GFunc add_func, gpointer user_data)
2010-01-12 22:09:28 -08:00
{
char **searches, **s;
char *unescaped, *p;
int i;
g_return_if_fail (str != NULL);
g_return_if_fail (add_func != NULL);
2010-01-12 22:09:28 -08:00
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, '\\')) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP, " invalid domain search: '%s'", unescaped);
2010-01-12 22:09:28 -08:00
goto out;
}
searches = g_strsplit (unescaped, " ", 0);
for (s = searches; *s; s++) {
if (strlen (*s)) {
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP, " domain search '%s'", *s);
add_func (*s, user_data);
2010-01-12 22:09:28 -08:00
}
}
g_strfreev (searches);
out:
g_free (unescaped);
}
static void
ip4_add_domain_search (gpointer data, gpointer user_data)
{
nm_ip4_config_add_search (NM_IP4_CONFIG (user_data), (const char *) data);
}
2010-01-12 22:09:28 -08:00
/* 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, prefix = 0;
2010-01-12 22:09:28 -08:00
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) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP4, "(%s): couldn't allocate memory for an IP4Config!", priv->iface);
2010-01-12 22:09:28 -08:00
return NULL;
}
addr = nm_ip4_address_new ();
if (!addr) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP4, "(%s): couldn't allocate memory for an IP4 Address!", priv->iface);
2010-01-12 22:09:28 -08:00
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);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP4, " address %s", str);
2010-01-12 22:09:28 -08:00
} else
goto error;
str = g_hash_table_lookup (priv->options, "new_subnet_mask");
if (str && (inet_pton (AF_INET, str, &tmp_addr) > 0)) {
prefix = nm_utils_ip4_netmask_to_prefix (tmp_addr.s_addr);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP4, " prefix %d (%s)", prefix, str);
} else {
/* Get default netmask for the IP according to appropriate class. */
prefix = nm_utils_ip4_get_default_prefix (nm_ip4_address_get_address (addr));
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP4, " prefix %d (default)", prefix);
2010-01-12 22:09:28 -08:00
}
nm_ip4_address_set_prefix (addr, prefix);
2010-01-12 22:09:28 -08:00
/* 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));
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP4, " gateway %s", buf);
2010-01-12 22:09:28 -08:00
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);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP4, " gateway %s", *s);
2010-01-12 22:09:28 -08:00
break;
} else
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP4, "ignoring invalid gateway '%s'", *s);
2010-01-12 22:09:28 -08:00
}
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)
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP4, " hostname '%s'", str);
2010-01-12 22:09:28 -08:00
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);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP4, " nameserver '%s'", *s);
2010-01-12 22:09:28 -08:00
} else
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP4, "ignoring invalid nameserver '%s'", *s);
2010-01-12 22:09:28 -08:00
}
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++) {
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP4, " domain name '%s'", *s);
2010-01-12 22:09:28 -08:00
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 (str, ip4_add_domain_search, ip4_config);
2010-01-12 22:09:28 -08:00
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);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP4, " wins '%s'", *s);
2010-01-12 22:09:28 -08:00
} else
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP4, "ignoring invalid WINS server '%s'", *s);
2010-01-12 22:09:28 -08:00
}
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)) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP4, "(%s): DHCP client didn't bind to a lease.", priv->iface);
2010-01-12 22:09:28 -08:00
return NULL;
}
return ip4_options_to_config (self);
}
/********************************************/
static void
ip6_add_domain_search (gpointer data, gpointer user_data)
{
nm_ip6_config_add_search (NM_IP6_CONFIG (user_data), (const char *) data);
}
/* Given a table of DHCP options from the client, convert into an IP6Config */
static NMIP6Config *
ip6_options_to_config (NMDHCPClient *self)
{
NMDHCPClientPrivate *priv;
NMIP6Config *ip6_config = NULL;
struct in6_addr tmp_addr;
NMIP6Address *addr = NULL;
char *str = NULL;
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);
ip6_config = nm_ip6_config_new ();
if (!ip6_config) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP6, "(%s): couldn't allocate memory for an IP6Config!", priv->iface);
return NULL;
}
addr = nm_ip6_address_new ();
if (!addr) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP6, "(%s): couldn't allocate memory for an IP6 Address!", priv->iface);
goto error;
}
str = g_hash_table_lookup (priv->options, "new_ip6_address");
if (str && (inet_pton (AF_INET6, str, &tmp_addr) > 0)) {
nm_ip6_address_set_address (addr, &tmp_addr);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP6, " address %s", str);
} else
goto error;
str = g_hash_table_lookup (priv->options, "new_ip6_prefixlen");
if (str) {
long unsigned int prefix;
errno = 0;
prefix = strtoul (str, NULL, 10);
if (errno != 0 || prefix > 128)
goto error;
nm_ip6_address_set_prefix (addr, (guint32) prefix);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP6, " prefix %lu", prefix);
}
nm_ip6_config_take_address (ip6_config, addr);
addr = NULL;
str = g_hash_table_lookup (priv->options, "new_host_name");
if (str)
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP6, " hostname '%s'", str);
str = g_hash_table_lookup (priv->options, "new_dhcp6_name_servers");
if (str) {
char **searches = g_strsplit (str, " ", 0);
char **s;
for (s = searches; *s; s++) {
if (inet_pton (AF_INET6, *s, &tmp_addr) > 0) {
nm_ip6_config_add_nameserver (ip6_config, &tmp_addr);
2010-04-06 18:06:47 -07:00
nm_log_info (LOGD_DHCP6, " nameserver '%s'", *s);
} else
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP6, "ignoring invalid nameserver '%s'", *s);
}
g_strfreev (searches);
}
str = g_hash_table_lookup (priv->options, "new_dhcp6_domain_search");
if (str)
process_domain_search (str, ip6_add_domain_search, ip6_config);
return ip6_config;
error:
if (addr)
nm_ip6_address_unref (addr);
g_object_unref (ip6_config);
return NULL;
}
NMIP6Config *
nm_dhcp_client_get_ip6_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)) {
2010-04-06 18:06:47 -07:00
nm_log_warn (LOGD_DHCP6, "(%s): dhcp client didn't bind to a lease.", priv->iface);
return NULL;
}
return ip6_options_to_config (self);
}
/********************************************/
2010-01-12 22:09:28 -08:00
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;
2010-01-13 16:51:20 -08:00
case PROP_IPV6:
g_value_set_boolean (value, priv->ipv6);
break;
case PROP_UUID:
g_value_set_string (value, priv->uuid);
break;
case PROP_TIMEOUT:
g_value_set_uint (value, priv->timeout);
break;
2010-01-12 22:09:28 -08:00
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;
2010-01-13 16:51:20 -08:00
case PROP_IPV6:
/* construct-only */
priv->ipv6 = g_value_get_boolean (value);
break;
case PROP_UUID:
/* construct-only */
priv->uuid = g_value_dup_string (value);
break;
case PROP_TIMEOUT:
priv->timeout = g_value_get_uint (value);
break;
2010-01-12 22:09:28 -08:00
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));
2010-01-13 16:51:20 -08:00
g_object_class_install_property
(object_class, PROP_IPV6,
g_param_spec_boolean (NM_DHCP_CLIENT_IPV6,
"ipv6",
"IPv6",
FALSE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class, PROP_UUID,
g_param_spec_string (NM_DHCP_CLIENT_UUID,
"uuid",
"UUID",
NULL,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property
(object_class, PROP_TIMEOUT,
g_param_spec_uint (NM_DHCP_CLIENT_TIMEOUT,
"timeout",
"Timeout",
0, G_MAXUINT, 45,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
2010-01-12 22:09:28 -08:00
/* 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);
}