nm-dhcp-dhcpcd.c 8.13 KB
Newer Older
Dan Williams's avatar
Dan Williams committed
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 3 4
/* nm-dhcp-dhcpcd.c - dhcpcd specific hooks for NetworkManager
 *
 * Copyright (C) 2008 Roy Marples
5
 * Copyright (C) 2010 Dan Williams <dcbw@redhat.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 *
 */

23
#include "nm-default.h"
24

25
#if WITH_DHCPCD
26

27 28 29
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
30 31
#include <netinet/in.h>
#include <arpa/inet.h>
32

33
#include "nm-dhcp-manager.h"
34
#include "nm-utils.h"
35
#include "NetworkManagerUtils.h"
36
#include "nm-dhcp-listener.h"
37
#include "nm-dhcp-client-logging.h"
38

39
/*****************************************************************************/
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54
#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 ((klass), NM_TYPE_DHCP_DHCPCD))
#define NM_DHCP_DHCPCD_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DHCP_DHCPCD, NMDhcpDhcpcdClass))

typedef struct _NMDhcpDhcpcd NMDhcpDhcpcd;
typedef struct _NMDhcpDhcpcdClass NMDhcpDhcpcdClass;

static GType nm_dhcp_dhcpcd_get_type (void);

/*****************************************************************************/

55 56
typedef struct {
	char *pid_file;
57
	NMDhcpListener *dhcp_listener;
58
} NMDhcpDhcpcdPrivate;
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
struct _NMDhcpDhcpcd {
	NMDhcpClient parent;
	NMDhcpDhcpcdPrivate _priv;
};

struct _NMDhcpDhcpcdClass {
	NMDhcpClientClass parent;
};

G_DEFINE_TYPE (NMDhcpDhcpcd, nm_dhcp_dhcpcd, NM_TYPE_DHCP_CLIENT)

#define NM_DHCP_DHCPCD_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMDhcpDhcpcd, NM_IS_DHCP_DHCPCD)

/*****************************************************************************/

75
static const char *
76
nm_dhcp_dhcpcd_get_path (void)
77
{
78
	return nm_utils_find_helper ("dhcpcd", DHCPCD_PATH, NULL);
79
}
80

81
static gboolean
82 83 84 85
ip4_start (NMDhcpClient *client,
           const char *dhcp_anycast_addr,
           const char *last_ip4_address,
           GError **error)
86
{
87 88
	NMDhcpDhcpcd *self = NM_DHCP_DHCPCD (client);
	NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (self);
89
	gs_unref_ptrarray GPtrArray *argv = NULL;
90
	pid_t pid = -1;
91 92 93 94 95 96
	GError *local = NULL;
	gs_free char *cmd_str = NULL;
	gs_free char *binary_name = NULL;
	const char *iface;
	const char *dhcpcd_path;
	const char *hostname;
97

98
	g_return_val_if_fail (priv->pid_file == NULL, FALSE);
99

100
	iface = nm_dhcp_client_get_iface (client);
101

102 103 104 105
	/* dhcpcd does not allow custom pidfiles; the pidfile is always
	 * RUNDIR "dhcpcd-<ifname>.pid".
	 */
	priv->pid_file = g_strdup_printf (RUNDIR "/dhcpcd-%s.pid", iface);
106

107 108
	dhcpcd_path = nm_dhcp_dhcpcd_get_path ();
	if (!dhcpcd_path) {
109
		nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcd binary not found");
110
		return FALSE;
111 112
	}

113
	/* Kill any existing dhcpcd from the pidfile */
114
	binary_name = g_path_get_basename (dhcpcd_path);
115 116
	nm_dhcp_client_stop_existing (priv->pid_file, binary_name);

117
	argv = g_ptr_array_new ();
118
	g_ptr_array_add (argv, (gpointer) dhcpcd_path);
119

120
	g_ptr_array_add (argv, (gpointer) "-B");    /* Don't background on lease (disable fork()) */
121

122
	g_ptr_array_add (argv, (gpointer) "-K");    /* Disable built-in carrier detection */
123

124
	g_ptr_array_add (argv, (gpointer) "-L");    /* Disable built-in IPv4LL */
125

126 127 128
	/* --noarp. Don't request or claim the address by ARP; this also disables IPv4LL. */
	g_ptr_array_add (argv, (gpointer) "-A");

129
	g_ptr_array_add (argv, (gpointer) "-G");    /* Let NM handle routing */
130

131
	g_ptr_array_add (argv, (gpointer) "-c");    /* Set script file */
132
	g_ptr_array_add (argv, (gpointer) nm_dhcp_helper_path);
133

134 135 136 137 138 139
	/* IPv4-only for now.  NetworkManager knows better than dhcpcd when to
	 * run IPv6, and dhcpcd's automatic Router Solicitations cause problems
	 * with devices that don't expect them.
	 */
	g_ptr_array_add (argv, (gpointer) "-4");

140
	hostname = nm_dhcp_client_get_hostname (client);
141 142 143 144 145 146 147 148

	if (hostname) {
		if (nm_dhcp_client_get_use_fqdn (client)) {
			g_ptr_array_add (argv, (gpointer) "-h");
			g_ptr_array_add (argv, (gpointer) hostname);
			g_ptr_array_add (argv, (gpointer) "-F");
			g_ptr_array_add (argv, (gpointer) "both");
		} else {
149 150
			g_ptr_array_add (argv, (gpointer) "-h");
			g_ptr_array_add (argv, (gpointer) hostname);
151
		}
152 153
	}

154
	g_ptr_array_add (argv, (gpointer) iface);
155 156
	g_ptr_array_add (argv, NULL);

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	_LOGD ("running: %s",
	       (cmd_str = g_strjoinv (" ", (char **) argv->pdata)));

	if (!g_spawn_async (NULL,
	                    (char **) argv->pdata, NULL,
	                      G_SPAWN_DO_NOT_REAP_CHILD
	                    | G_SPAWN_STDOUT_TO_DEV_NULL
	                    | G_SPAWN_STDERR_TO_DEV_NULL,
	                    nm_utils_setpgid,
	                    NULL,
	                    &pid,
	                    &local)) {
		nm_utils_error_set (error,
		                    NM_UTILS_ERROR_UNKNOWN,
		                    "dhcpcd failed to start: %s",
		                    local->message);
		g_error_free (local);
		return FALSE;
175
	}
176

177 178 179 180
	nm_assert (pid > 0);
	_LOGI ("dhcpcd started with pid %d", pid);
	nm_dhcp_client_watch_child (client, pid);
	return TRUE;
181
}
182

183
static gboolean
184
ip6_start (NMDhcpClient *client,
185
           const char *dhcp_anycast_addr,
186
           const struct in6_addr *ll_addr,
187
           NMSettingIP6ConfigPrivacy privacy,
188 189
           guint needed_prefixes,
           GError **error)
Dan Williams's avatar
Dan Williams committed
190
{
191
	nm_utils_error_set_literal (error, NM_UTILS_ERROR_UNKNOWN, "dhcpcd plugin does not support IPv6");
192
	return FALSE;
Dan Williams's avatar
Dan Williams committed
193 194
}

195
static void
196
stop (NMDhcpClient *client, gboolean release)
197
{
198 199
	NMDhcpDhcpcd *self = NM_DHCP_DHCPCD (client);
	NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (self);
200
	int errsv;
201

202
	NM_DHCP_CLIENT_CLASS (nm_dhcp_dhcpcd_parent_class)->stop (client, release);
203

204
	if (priv->pid_file) {
205 206 207 208
		if (remove (priv->pid_file) == -1) {
			errsv = errno;
			_LOGD ("could not remove dhcp pid file \"%s\": %d (%s)", priv->pid_file, errsv, g_strerror (errsv));
		}
209
	}
210 211

	/* FIXME: implement release... */
212 213
}

214
/*****************************************************************************/
215 216

static void
217
nm_dhcp_dhcpcd_init (NMDhcpDhcpcd *self)
218
{
219 220 221 222
	NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE (self);

	priv->dhcp_listener = g_object_ref (nm_dhcp_listener_get ());
	g_signal_connect (priv->dhcp_listener,
223 224 225
	                  NM_DHCP_LISTENER_EVENT,
	                  G_CALLBACK (nm_dhcp_client_handle_event),
	                  self);
226 227 228 229 230
}

static void
dispose (GObject *object)
{
231
	NMDhcpDhcpcdPrivate *priv = NM_DHCP_DHCPCD_GET_PRIVATE ((NMDhcpDhcpcd *) object);
232

233 234 235 236 237 238
	if (priv->dhcp_listener) {
		g_signal_handlers_disconnect_by_func (priv->dhcp_listener,
		                                      G_CALLBACK (nm_dhcp_client_handle_event),
		                                      NM_DHCP_DHCPCD (object));
		g_clear_object (&priv->dhcp_listener);
	}
239

240
	nm_clear_g_free (&priv->pid_file);
241 242 243 244 245

	G_OBJECT_CLASS (nm_dhcp_dhcpcd_parent_class)->dispose (object);
}

static void
246
nm_dhcp_dhcpcd_class_init (NMDhcpDhcpcdClass *dhcpcd_class)
247
{
248
	NMDhcpClientClass *client_class = NM_DHCP_CLIENT_CLASS (dhcpcd_class);
249 250 251 252
	GObjectClass *object_class = G_OBJECT_CLASS (dhcpcd_class);

	object_class->dispose = dispose;

253 254 255
	client_class->ip4_start = ip4_start;
	client_class->ip6_start = ip6_start;
	client_class->stop = stop;
256 257
}

258 259 260 261 262
const NMDhcpClientFactory _nm_dhcp_client_factory_dhcpcd = {
	.name = "dhcpcd",
	.get_type = nm_dhcp_dhcpcd_get_type,
	.get_path = nm_dhcp_dhcpcd_get_path,
};
263

264
#endif /* WITH_DHCPCD */