dhcp: fall back to other clients when an invalid one is specified
[NetworkManager.git] / src / dhcp-manager / nm-dhcp-manager.c
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
3  *
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)
7  * any later version.
8  *
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.
13  *
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.
17  *
18  * Copyright (C) 2005 - 2013 Red Hat, Inc.
19  * Copyright (C) 2006 - 2008 Novell, Inc.
20  *
21  */
22
23 #include "nm-default.h"
24
25 #include <sys/socket.h>
26 #include <sys/wait.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34
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"
41
42 #define DHCP_TIMEOUT 45 /* default DHCP timeout, in seconds */
43
44 /* default to installed helper, but can be modified for testing */
45 const char *nm_dhcp_helper_path = LIBEXECDIR "/nm-dhcp-helper";
46
47 typedef GSList * (*GetLeaseConfigFunc) (const char *iface, const char *uuid, gboolean ipv6);
48
49 typedef struct {
50         GType               client_type;
51         GHashTable *        clients;
52         char *              default_hostname;
53 } NMDhcpManagerPrivate;
54
55 #define NM_DHCP_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_DHCP_MANAGER, NMDhcpManagerPrivate))
56
57 G_DEFINE_TYPE (NMDhcpManager, nm_dhcp_manager, G_TYPE_OBJECT)
58
59 /***************************************************/
60
61 typedef struct {
62         GType gtype;
63         const char *name;
64         NMDhcpClientGetPathFunc get_path_func;
65         NMDhcpClientGetLeaseConfigsFunc get_lease_configs_func;
66 } ClientDesc;
67
68 static GSList *client_descs = NULL;
69
70 void
71 _nm_dhcp_client_register (GType gtype,
72                           const char *name,
73                           NMDhcpClientGetPathFunc get_path_func,
74                           NMDhcpClientGetLeaseConfigsFunc get_lease_configs_func)
75 {
76         ClientDesc *desc;
77         GSList *iter;
78
79         g_return_if_fail (gtype != G_TYPE_INVALID);
80         g_return_if_fail (name != NULL);
81
82         for (iter = client_descs; iter; iter = iter->next) {
83                 desc = iter->data;
84                 g_return_if_fail (desc->gtype != gtype);
85                 g_return_if_fail (strcmp (desc->name, name) != 0);
86         }
87
88         desc = g_slice_new0 (ClientDesc);
89         desc->gtype = gtype;
90         desc->name = name;
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);
94 }
95
96 static ClientDesc *
97 find_client_desc (const char *name, GType gtype)
98 {
99         GSList *iter;
100
101         g_return_val_if_fail (name || gtype, NULL);
102
103         for (iter = client_descs; iter; iter = iter->next) {
104                 ClientDesc *desc = iter->data;
105
106                 if (name && strcmp (desc->name, name) != 0)
107                         continue;
108                 if (gtype && desc->gtype != gtype)
109                         continue;
110                 return desc;
111         }
112         return NULL;
113 }
114
115 static GType
116 is_client_enabled (const char *name)
117 {
118         ClientDesc *desc;
119
120         desc = find_client_desc (name, G_TYPE_INVALID);
121         if (desc && (!desc->get_path_func || desc->get_path_func()))
122                 return desc->gtype;
123
124         return G_TYPE_INVALID;
125 }
126
127 /***************************************************/
128
129 static NMDhcpClient *
130 get_client_for_ifindex (NMDhcpManager *manager, int ifindex, gboolean ip6)
131 {
132         NMDhcpManagerPrivate *priv;
133         GHashTableIter iter;
134         gpointer value;
135
136         g_return_val_if_fail (NM_IS_DHCP_MANAGER (manager), NULL);
137         g_return_val_if_fail (ifindex > 0, NULL);
138
139         priv = NM_DHCP_MANAGER_GET_PRIVATE (manager);
140
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);
144
145                 if (   nm_dhcp_client_get_ifindex (candidate) == ifindex
146                     && nm_dhcp_client_get_ipv6 (candidate) == ip6)
147                         return candidate;
148         }
149
150         return NULL;
151 }
152
153 static void client_state_changed (NMDhcpClient *client,
154                                   NMDhcpState state,
155                                   GObject *ip_config,
156                                   GVariant *options,
157                                   const char *event_id,
158                                   NMDhcpManager *self);
159
160 static void
161 remove_client (NMDhcpManager *self, NMDhcpClient *client)
162 {
163         g_signal_handlers_disconnect_by_func (client, client_state_changed, self);
164
165         /* Stopping the client is left up to the controlling device
166          * explicitly since we may want to quit NetworkManager but not terminate
167          * the DHCP client.
168          */
169
170         g_hash_table_remove (NM_DHCP_MANAGER_GET_PRIVATE (self)->clients, client);
171 }
172
173 static void
174 client_state_changed (NMDhcpClient *client,
175                       NMDhcpState state,
176                       GObject *ip_config,
177                       GVariant *options,
178                       const char *event_id,
179                       NMDhcpManager *self)
180 {
181         if (state >= NM_DHCP_STATE_TIMEOUT)
182                 remove_client (self, client);
183 }
184
185 static NMDhcpClient *
186 client_start (NMDhcpManager *self,
187               const char *iface,
188               int ifindex,
189               const GByteArray *hwaddr,
190               const char *uuid,
191               guint32 priority,
192               gboolean ipv6,
193               const struct in6_addr *ipv6_ll_addr,
194               const char *dhcp_client_id,
195               guint32 timeout,
196               const char *dhcp_anycast_addr,
197               const char *hostname,
198               const char *fqdn,
199               gboolean info_only,
200               NMSettingIP6ConfigPrivacy privacy,
201               const char *last_ip4_address)
202 {
203         NMDhcpManagerPrivate *priv;
204         NMDhcpClient *client;
205         gboolean success = FALSE;
206
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);
211
212         priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
213
214         /* Ensure we have a usable DHCP client */
215         g_return_val_if_fail (priv->client_type != 0, NULL);
216
217         /* Kill any old client instance */
218         client = get_client_for_ifindex (self, ifindex, ipv6);
219         if (client) {
220                 g_object_ref (client);
221                 remove_client (self, client);
222                 nm_dhcp_client_stop (client, FALSE);
223                 g_object_unref (client);
224         }
225
226         /* And make a new one */
227         client = g_object_new (priv->client_type,
228                                NM_DHCP_CLIENT_INTERFACE, iface,
229                                NM_DHCP_CLIENT_IFINDEX, ifindex,
230                                NM_DHCP_CLIENT_HWADDR, hwaddr,
231                                NM_DHCP_CLIENT_IPV6, ipv6,
232                                NM_DHCP_CLIENT_UUID, uuid,
233                                NM_DHCP_CLIENT_PRIORITY, priority,
234                                NM_DHCP_CLIENT_TIMEOUT, timeout ? timeout : DHCP_TIMEOUT,
235                                NULL);
236         g_hash_table_insert (NM_DHCP_MANAGER_GET_PRIVATE (self)->clients, client, g_object_ref (client));
237         g_signal_connect (client, NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED, G_CALLBACK (client_state_changed), self);
238
239         if (ipv6)
240                 success = nm_dhcp_client_start_ip6 (client, dhcp_anycast_addr, ipv6_ll_addr, hostname, info_only, privacy);
241         else
242                 success = nm_dhcp_client_start_ip4 (client, dhcp_client_id, dhcp_anycast_addr, hostname, fqdn, last_ip4_address);
243
244         if (!success) {
245                 remove_client (self, client);
246                 client = NULL;
247         }
248
249         return client;
250 }
251
252 static const char *
253 get_send_hostname (NMDhcpManager *self, const char *setting_hostname)
254 {
255         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
256
257         /* Always prefer the explicit dhcp-send-hostname if given */
258         return setting_hostname ? setting_hostname : priv->default_hostname;
259 }
260
261 /* Caller owns a reference to the NMDhcpClient on return */
262 NMDhcpClient *
263 nm_dhcp_manager_start_ip4 (NMDhcpManager *self,
264                            const char *iface,
265                            int ifindex,
266                            const GByteArray *hwaddr,
267                            const char *uuid,
268                            guint32 priority,
269                            gboolean send_hostname,
270                            const char *dhcp_hostname,
271                            const char *dhcp_fqdn,
272                            const char *dhcp_client_id,
273                            guint32 timeout,
274                            const char *dhcp_anycast_addr,
275                            const char *last_ip_address)
276 {
277         const char *hostname = NULL;
278         const char *fqdn = NULL;
279
280         g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
281
282         if (send_hostname) {
283                 hostname = get_send_hostname (self, dhcp_hostname);
284                 fqdn = dhcp_fqdn;
285         }
286         return client_start (self, iface, ifindex, hwaddr, uuid, priority, FALSE, NULL,
287                              dhcp_client_id, timeout, dhcp_anycast_addr, hostname,
288                              fqdn, FALSE, 0, last_ip_address);
289 }
290
291 /* Caller owns a reference to the NMDhcpClient on return */
292 NMDhcpClient *
293 nm_dhcp_manager_start_ip6 (NMDhcpManager *self,
294                            const char *iface,
295                            int ifindex,
296                            const GByteArray *hwaddr,
297                            const struct in6_addr *ll_addr,
298                            const char *uuid,
299                            guint32 priority,
300                            gboolean send_hostname,
301                            const char *dhcp_hostname,
302                            guint32 timeout,
303                            const char *dhcp_anycast_addr,
304                            gboolean info_only,
305                            NMSettingIP6ConfigPrivacy privacy)
306 {
307         const char *hostname = NULL;
308
309         g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
310
311         if (send_hostname)
312                 hostname = get_send_hostname (self, dhcp_hostname);
313         return client_start (self, iface, ifindex, hwaddr, uuid, priority, TRUE,
314                              ll_addr, NULL, timeout, dhcp_anycast_addr, hostname, NULL, info_only,
315                              privacy, NULL);
316 }
317
318 void
319 nm_dhcp_manager_set_default_hostname (NMDhcpManager *manager, const char *hostname)
320 {
321         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (manager);
322
323         g_clear_pointer (&priv->default_hostname, g_free);
324
325         /* Never send 'localhost'-type names to the DHCP server */
326         if (!nm_utils_is_specific_hostname (hostname))
327                 return;
328
329         priv->default_hostname = g_strdup (hostname);
330 }
331
332 GSList *
333 nm_dhcp_manager_get_lease_ip_configs (NMDhcpManager *self,
334                                       const char *iface,
335                                       int ifindex,
336                                       const char *uuid,
337                                       gboolean ipv6,
338                                       guint32 default_route_metric)
339 {
340         ClientDesc *desc;
341
342         g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL);
343         g_return_val_if_fail (iface != NULL, NULL);
344         g_return_val_if_fail (ifindex >= -1, NULL);
345         g_return_val_if_fail (uuid != NULL, NULL);
346
347         desc = find_client_desc (NULL, NM_DHCP_MANAGER_GET_PRIVATE (self)->client_type);
348         if (desc && desc->get_lease_configs_func)
349                 return desc->get_lease_configs_func (iface, ifindex, uuid, ipv6, default_route_metric);
350         return NULL;
351 }
352
353 /***************************************************/
354
355 NM_DEFINE_SINGLETON_GETTER (NMDhcpManager, nm_dhcp_manager_get, NM_TYPE_DHCP_MANAGER);
356
357 static void
358 nm_dhcp_manager_init (NMDhcpManager *self)
359 {
360         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self);
361         NMConfig *config = nm_config_get ();
362         const char *client;
363         GSList *iter;
364         GType type = G_TYPE_INVALID;
365
366         for (iter = client_descs; iter; iter = iter->next) {
367                 ClientDesc *desc = iter->data;
368
369                 nm_log_dbg (LOGD_DHCP, "Registered DHCP client '%s' (%s)",
370                             desc->name, g_type_name (desc->gtype));
371         }
372
373         /* Client-specific setup */
374         client = nm_config_get_dhcp_client (config);
375         if (nm_config_get_configure_and_quit (config)) {
376                 if (g_strcmp0 (client, "internal") != 0)
377                         nm_log_warn (LOGD_DHCP, "Using internal DHCP client since configure-and-quit is set.");
378                 client = "internal";
379         }
380
381         if (client)
382                 type = is_client_enabled (client);
383
384         if (type == G_TYPE_INVALID) {
385                 if (client)
386                         nm_log_warn (LOGD_DHCP, "DHCP client '%s' not available", client);
387
388                 type = is_client_enabled ("dhclient");
389                 if (type == G_TYPE_INVALID)
390                         type = is_client_enabled ("dhcpcd");
391                 if (type == G_TYPE_INVALID)
392                         type = is_client_enabled ("internal");
393         }
394
395         if (type == G_TYPE_INVALID)
396                 nm_log_warn (LOGD_DHCP, "No usable DHCP client found! DHCP configurations will fail");
397         else
398                 nm_log_info (LOGD_DHCP, "Using DHCP client '%s'", find_client_desc (NULL, type)->name);
399
400         priv->client_type = type;
401         priv->clients = g_hash_table_new_full (g_direct_hash, g_direct_equal,
402                                                NULL,
403                                                (GDestroyNotify) g_object_unref);
404 }
405
406 static void
407 dispose (GObject *object)
408 {
409         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (object);
410         GList *values, *iter;
411
412         if (priv->clients) {
413                 values = g_hash_table_get_values (priv->clients);
414                 for (iter = values; iter; iter = g_list_next (iter))
415                         remove_client (NM_DHCP_MANAGER (object), NM_DHCP_CLIENT (iter->data));
416                 g_list_free (values);
417         }
418
419         G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->dispose (object);
420 }
421
422 static void
423 finalize (GObject *object)
424 {
425         NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (object);
426
427         g_free (priv->default_hostname);
428
429         if (priv->clients)
430                 g_hash_table_destroy (priv->clients);
431
432         G_OBJECT_CLASS (nm_dhcp_manager_parent_class)->finalize (object);
433 }
434
435 static void
436 nm_dhcp_manager_class_init (NMDhcpManagerClass *manager_class)
437 {
438         GObjectClass *object_class = G_OBJECT_CLASS (manager_class);
439
440         g_type_class_add_private (manager_class, sizeof (NMDhcpManagerPrivate));
441
442         /* virtual methods */
443         object_class->finalize = finalize;
444         object_class->dispose = dispose;
445 }