1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* nm-dhcp-manager.c - Handle the DHCP daemon for NetworkManager
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Copyright (C) 2005 - 2013 Red Hat, Inc.
19 * Copyright (C) 2006 - 2008 Novell, Inc.
23 #include "nm-default.h"
25 #include <sys/socket.h>
35 #include "nm-dhcp-manager.h"
36 #include "nm-dhcp-dhclient.h"
37 #include "nm-dhcp-dhcpcd.h"
38 #include "nm-dhcp-systemd.h"
39 #include "nm-config.h"
40 #include "NetworkManagerUtils.h"
42 #define DHCP_TIMEOUT 45 /* default DHCP timeout, in seconds */
44 /* default to installed helper, but can be modified for testing */
45 const char *nm_dhcp_helper_path = LIBEXECDIR "/nm-dhcp-helper";
47 typedef GSList * (*GetLeaseConfigFunc) (const char *iface, const char *uuid, gboolean ipv6);
52 char * default_hostname;
53 } NMDhcpManagerPrivate;
55 #define NM_DHCP_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_MANAGER, NMDhcpManagerPrivate))
57 G_DEFINE_TYPE (NMDhcpManager, nm_dhcp_manager, G_TYPE_OBJECT)
59 /***************************************************/
64 NMDhcpClientGetPathFunc get_path_func;
65 NMDhcpClientGetLeaseConfigsFunc get_lease_configs_func;
68 static GSList *client_descs = NULL;
71 _nm_dhcp_client_register (GType gtype,
73 NMDhcpClientGetPathFunc get_path_func,
74 NMDhcpClientGetLeaseConfigsFunc get_lease_configs_func)
79 g_return_if_fail (gtype != G_TYPE_INVALID);
80 g_return_if_fail (name != NULL);
82 for (iter = client_descs; iter; iter = iter->next) {
84 g_return_if_fail (desc->gtype != gtype);
85 g_return_if_fail (strcmp (desc->name, name) != 0);
88 desc = g_slice_new0 (ClientDesc);
91 desc->get_path_func = get_path_func;
92 desc->get_lease_configs_func = get_lease_configs_func;
93 client_descs = g_slist_prepend (client_descs, desc);
97 find_client_desc (const char *name, GType gtype)
101 g_return_val_if_fail (name || gtype, NULL);
103 for (iter = client_descs; iter; iter = iter->next) {
104 ClientDesc *desc = iter->data;
106 if (name && strcmp (desc->name, name) != 0)
108 if (gtype && desc->gtype != gtype)
116 is_client_enabled (const char *name)
120 desc = find_client_desc (name, G_TYPE_INVALID);
121 if (desc && (!desc->get_path_func || desc->get_path_func()))
124 return G_TYPE_INVALID;
127 /***************************************************/
129 static NMDhcpClient *
130 get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6)
132 NMDhcpManagerPrivate *priv;
136 g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL);
137 g_return_val_if_fail (ifindex > 0, NULL);
139 priv = NM_DHCP_MANAGER_GET_PRIVATE (manager);
141 g_hash_table_iter_init (&iter, priv->clients);
142 while (g_hash_table_iter_next (&iter, NULL, &value)) {
143 NMDhcpClient *candidate = NM_DHCP_CLIENT (value);
145 if ( nm_dhcp_client_get_ifindex (candidate) == ifindex
146 && nm_dhcp_client_get_ipv6 (candidate) == ip6)
153 static void client_state_changed (NMDhcpClient *client,
157 const char *event_id,
158 NMDhcpManager *self);
161 remove_client (NMDhcpManager *self, NMDhcpClient *client)
163 g_signal_handlers_disconnect_by_func (client, client_state_changed, self);
165 /* Stopping the client is left up to the controlling device
166 * explicitly since we may want to quit NetworkManager but not terminate
170 g_hash_table_remove (NM_DHCP_MANAGER_GET_PRIVATE (self)->clients, client);
174 client_state_changed (NMDhcpClient *client,
178 const char *event_id,
181 if (state >= NM_DHCP_STATE_TIMEOUT)
182 remove_client (self, client);
185 static NMDhcpClient *
186 client_start (NMDhcpManager *self,
189 const GByteArray *hwaddr,
193 const struct in6_addr *ipv6_ll_addr,
194 const char *dhcp_client_id,
196 const char *dhcp_anycast_addr,
197 const char *hostname,
200 NMSettingIP6ConfigPrivacy privacy,
201 const char *last_ip4_address)
203 NMDhcpManagerPrivate *priv;
204 NMDhcpClient *client;
205 gboolean success = FALSE;
207 g_return_val_if_fail (self, NULL);
208 g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
209 g_return_val_if_fail (ifindex > 0, NULL);
210 g_return_val_if_fail (uuid != NULL, NULL);
212 priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
214 /* Ensure we have a usable DHCP client */
215 if (priv->client_type == G_TYPE_INVALID)
218 /* Kill any old client instance */
219 client = get_client_for_ifindex (self, ifindex, ipv6);
221 g_object_ref (client);
222 remove_client (self, client);
223 nm_dhcp_client_stop (client, FALSE);
224 g_object_unref (client);
227 /* And make a new one */
228 client = g_object_new (priv->client_type,
229 NM_DHCP_CLIENT_INTERFACE, iface,
230 NM_DHCP_CLIENT_IFINDEX, ifindex,
231 NM_DHCP_CLIENT_HWADDR, hwaddr,
232 NM_DHCP_CLIENT_IPV6, ipv6,
233 NM_DHCP_CLIENT_UUID, uuid,
234 NM_DHCP_CLIENT_PRIORITY, priority,
235 NM_DHCP_CLIENT_TIMEOUT, timeout ? timeout : DHCP_TIMEOUT,
237 g_hash_table_insert (NM_DHCP_MANAGER_GET_PRIVATE (self)->clients, client, g_object_ref (client));
238 g_signal_connect (client, NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED, G_CALLBACK (client_state_changed), self);
241 success = nm_dhcp_client_start_ip6 (client, dhcp_anycast_addr, ipv6_ll_addr, hostname, info_only, privacy);
243 success = nm_dhcp_client_start_ip4 (client, dhcp_client_id, dhcp_anycast_addr, hostname, fqdn, last_ip4_address);
246 remove_client (self, client);
254 get_send_hostname (NMDhcpManager *self, const char *setting_hostname)
256 NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
258 /* Always prefer the explicit dhcp-send-hostname if given */
259 return setting_hostname ? setting_hostname : priv->default_hostname;
262 /* Caller owns a reference to the NMDhcpClient on return */
264 nm_dhcp_manager_start_ip4 (NMDhcpManager *self,
267 const GByteArray *hwaddr,
270 gboolean send_hostname,
271 const char *dhcp_hostname,
272 const char *dhcp_fqdn,
273 const char *dhcp_client_id,
275 const char *dhcp_anycast_addr,
276 const char *last_ip_address)
278 const char *hostname = NULL;
279 const char *fqdn = NULL;
281 g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
284 hostname = get_send_hostname (self, dhcp_hostname);
287 return client_start (self, iface, ifindex, hwaddr, uuid, priority, FALSE, NULL,
288 dhcp_client_id, timeout, dhcp_anycast_addr, hostname,
289 fqdn, FALSE, 0, last_ip_address);
292 /* Caller owns a reference to the NMDhcpClient on return */
294 nm_dhcp_manager_start_ip6 (NMDhcpManager *self,
297 const GByteArray *hwaddr,
298 const struct in6_addr *ll_addr,
301 gboolean send_hostname,
302 const char *dhcp_hostname,
304 const char *dhcp_anycast_addr,
306 NMSettingIP6ConfigPrivacy privacy)
308 const char *hostname = NULL;
310 g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
313 hostname = get_send_hostname (self, dhcp_hostname);
314 return client_start (self, iface, ifindex, hwaddr, uuid, priority, TRUE,
315 ll_addr, NULL, timeout, dhcp_anycast_addr, hostname, NULL, info_only,
320 nm_dhcp_manager_set_default_hostname (NMDhcpManager *manager, const char *hostname)
322 NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (manager);
324 g_clear_pointer (&priv->default_hostname, g_free);
326 /* Never send 'localhost'-type names to the DHCP server */
327 if (!nm_utils_is_specific_hostname (hostname))
330 priv->default_hostname = g_strdup (hostname);
334 nm_dhcp_manager_get_lease_ip_configs (NMDhcpManager *self,
339 guint32 default_route_metric)
341 NMDhcpManagerPrivate *priv;
344 g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
345 g_return_val_if_fail (iface != NULL, NULL);
346 g_return_val_if_fail (ifindex >= -1, NULL);
347 g_return_val_if_fail (uuid != NULL, NULL);
349 priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
350 if (priv->client_type == G_TYPE_INVALID)
353 desc = find_client_desc (NULL, priv->client_type);
354 if (desc && desc->get_lease_configs_func)
355 return desc->get_lease_configs_func (iface, ifindex, uuid, ipv6, default_route_metric);
359 /***************************************************/
361 NM_DEFINE_SINGLETON_GETTER (NMDhcpManager, nm_dhcp_manager_get, NM_TYPE_DHCP_MANAGER);
364 nm_dhcp_manager_init (NMDhcpManager *self)
366 NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
367 NMConfig *config = nm_config_get ();
370 GType type = G_TYPE_INVALID;
372 for (iter = client_descs; iter; iter = iter->next) {
373 ClientDesc *desc = iter->data;
375 nm_log_dbg (LOGD_DHCP, "Registered DHCP client '%s' (%s)",
376 desc->name, g_type_name (desc->gtype));
379 /* Client-specific setup */
380 client = nm_config_get_dhcp_client (config);
381 if (nm_config_get_configure_and_quit (config)) {
382 if (g_strcmp0 (client, "internal") != 0)
383 nm_log_warn (LOGD_DHCP, "Using internal DHCP client since configure-and-quit is set.");
388 type = is_client_enabled (client);
390 if (type == G_TYPE_INVALID) {
392 nm_log_warn (LOGD_DHCP, "DHCP client '%s' not available", client);
394 type = is_client_enabled ("dhclient");
395 if (type == G_TYPE_INVALID)
396 type = is_client_enabled ("dhcpcd");
397 if (type == G_TYPE_INVALID)
398 type = is_client_enabled ("internal");
401 if (type == G_TYPE_INVALID)
402 nm_log_warn (LOGD_DHCP, "No usable DHCP client found! DHCP configurations will fail");
404 nm_log_info (LOGD_DHCP, "Using DHCP client '%s'", find_client_desc (NULL, type)->name);
406 priv->client_type = type;
407 priv->clients = g_hash_table_new_full (g_direct_hash, g_direct_equal,
409 (GDestroyNotify) g_object_unref);
413 dispose (GObject *object)
415 NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (object);
416 GList *values, *iter;
419 values = g_hash_table_get_values (priv->clients);
420 for (iter = values; iter; iter = g_list_next (iter))
421 remove_client (NM_DHCP_MANAGER (object), NM_DHCP_CLIENT (iter->data));
422 g_list_free (values);
425 G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->dispose (object);
429 finalize (GObject *object)
431 NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (object);
433 g_free (priv->default_hostname);
436 g_hash_table_destroy (priv->clients);
438 G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->finalize (object);
442 nm_dhcp_manager_class_init (NMDhcpManagerClass *manager_class)
444 GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
446 g_type_class_add_private (manager_class, sizeof (NMDhcpManagerPrivate));
448 /* virtual methods */
449 object_class->finalize = finalize;
450 object_class->dispose = dispose;